From dcbd5ff9b6ec31a851e6290dcc08296f2df6c16c Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Wed, 24 Jun 2020 22:12:10 +0900 Subject: [PATCH] Add SMS drivers for apistore, cafe24, ppurio --- common/framework/drivers/sms/apistore.php | 141 ++++++++++++++++++++++ common/framework/drivers/sms/cafe24.php | 116 ++++++++++++++++++ common/framework/drivers/sms/ppurio.php | 103 ++++++++++++++++ tests/unit/framework/SMSTest.php | 7 ++ 4 files changed, 367 insertions(+) create mode 100644 common/framework/drivers/sms/apistore.php create mode 100644 common/framework/drivers/sms/cafe24.php create mode 100644 common/framework/drivers/sms/ppurio.php diff --git a/common/framework/drivers/sms/apistore.php b/common/framework/drivers/sms/apistore.php new file mode 100644 index 000000000..3e806f89e --- /dev/null +++ b/common/framework/drivers/sms/apistore.php @@ -0,0 +1,141 @@ + 500, + 'sms_max_length' => 90, + 'sms_max_length_in_charset' => 'CP949', + 'lms_supported' => true, + 'lms_supported_country_codes' => array(82), + 'lms_max_length' => 2000, + 'lms_max_length_in_charset' => 'CP949', + 'lms_subject_supported' => true, + 'lms_subject_max_length' => 60, + 'mms_supported' => false, + 'delay_supported' => true, + ); + + /** + * Config keys used by this driver are stored here. + */ + protected static $_required_config = array('api_user', 'api_key'); + + /** + * Check if the current SMS driver is supported on this server. + * + * This method returns true on success and false on failure. + * + * @return bool + */ + public static function isSupported() + { + return true; + } + + /** + * Store the last response. + */ + protected $_last_response = ''; + + /** + * Send a message. + * + * This method returns true on success and false on failure. + * + * @param array $messages + * @param object $original + * @return bool + */ + public function send(array $messages, \Rhymix\Framework\SMS $original) + { + $status = true; + + foreach ($messages as $i => $message) + { + $data = array(); + $data['send_phone'] = $message->from; + $data['dest_phone'] = implode(',', $message->to); + $data['msg_body'] = strval($message->content); + if ($message->type !== 'SMS' && $message->subject) + { + $data['subject'] = $message->subject; + } + + $result = $this->_apiCall(sprintf('message/%s', strtolower($message->type)), $data); + if (!$result) + { + $message->errors[] = 'ApiStore API returned invalid response: ' . $this->_getLastResponse(); + $status = false; + } + if ($result->result_message !== 'OK') + { + $message->errors[] = 'ApiStore API error: ' . $result->result_code . ' ' . $result->result_message; + } + } + + return $status; + } + + /** + * API call. + * + * @param string $url + * @param array $data + * @param string $method (optional) + * @return object|false + */ + protected function _apiCall(string $url, array $data, string $method = 'POST') + { + // Build the request URL. + if ($data['version']) + { + $version = $data['version']; + unset($data['version']); + } + else + { + $version = 1; + } + $url = sprintf('http://api.apistore.co.kr/ppurio/%d/%s/%s', $version, trim($url, '/'), $this->_config['api_user']); + + // Set the API key in the header. + $headers = array( + 'x-waple-authorization' => $this->_config['api_key'], + ); + + // Send the API reqeust. + if ($method === 'GET') + { + if ($data) + { + $url .= '?' . http_build_query($data); + } + $this->_last_response = \FileHandler::getRemoteResource($url, null, 5, $method, null, $headers) ?: ''; + } + else + { + $this->_last_response = \FileHandler::getRemoteResource($url, $data, 5, $method, null, $headers) ?: ''; + } + $result = @json_decode($this->_last_response); + return $result ?: false; + } + + /** + * Fetch the last API response. + * + * @return string + */ + protected function _getLastResponse() + { + return $this->_last_response; + } +} diff --git a/common/framework/drivers/sms/cafe24.php b/common/framework/drivers/sms/cafe24.php new file mode 100644 index 000000000..260ac39d6 --- /dev/null +++ b/common/framework/drivers/sms/cafe24.php @@ -0,0 +1,116 @@ + 1000, + 'sms_max_length' => 90, + 'sms_max_length_in_charset' => 'CP949', + 'lms_supported' => true, + 'lms_supported_country_codes' => array(82), + 'lms_max_length' => 2000, + 'lms_max_length_in_charset' => 'CP949', + 'lms_subject_supported' => true, + 'lms_subject_max_length' => 50, + 'mms_supported' => false, + 'delay_supported' => true, + ); + + /** + * Config keys used by this driver are stored here. + */ + protected static $_required_config = array('api_user', 'api_key'); + + /** + * Check if the current SMS driver is supported on this server. + * + * This method returns true on success and false on failure. + * + * @return bool + */ + public static function isSupported() + { + return true; + } + + /** + * Send a message. + * + * This method returns true on success and false on failure. + * + * @param array $messages + * @param object $original + * @return bool + */ + public function send(array $messages, \Rhymix\Framework\SMS $original) + { + $status = true; + + foreach ($messages as $i => $message) + { + // Authentication and basic information + $data = array(); + $data['user_id'] = $this->_config['api_user']; + $data['secure'] = $this->_config['api_key']; + $data['nointeractive'] = 1; + if ($message->type === 'LMS') + { + $data['smsType'] = 'L'; + } + + // Sender and recipient + $from = explode('-', \Rhymix\Framework\Korea::formatPhoneNumber($message->from)); + $data['sphone1'] = $from[0]; + $data['sphone2'] = $from[1]; + if (isset($from[2])) + { + $data['sphone3'] = $from[2]; + } + $data['rphone'] = implode(',', array_map(function($num) { + return \Rhymix\Framework\Korea::formatPhoneNumber($num); + }, $message->to)); + + // Subject and content + if ($message->type === 'LMS' && $message->subject) + { + $data['subject'] = $message->subject; + } + $data['msg'] = $message->content; + + // Set delay + if ($message->delay && $message->delay > time() + 600) + { + $data['rdate'] = gmdate('Ymd', $message->delay + (3600 * 9)); + $data['rtime'] = gmdate('His', $message->delay + (3600 * 9)); + } + + // Send! + $url = 'https://sslsms.cafe24.com/sms_sender.php'; + $result = \FileHandler::getRemoteResource($url, $data, 5, 'POST'); + if(strval($result) === '') + { + $original->addError('Unknown API error while sending message ' . ($i + 1) . ' of ' . count($messages)); + $status = false; + } + else + { + $result = explode(',', $result); + if ($result[0] !== 'success' && $result[0] !== 'reserved') + { + $original->addError('API error ' . $result[0] . ' while sending message ' . ($i + 1) . ' of ' . count($messages)); + $status = false; + } + } + } + + return $status; + } +} diff --git a/common/framework/drivers/sms/ppurio.php b/common/framework/drivers/sms/ppurio.php new file mode 100644 index 000000000..68f4d1307 --- /dev/null +++ b/common/framework/drivers/sms/ppurio.php @@ -0,0 +1,103 @@ + 1000, + 'sms_max_length' => 90, + 'sms_max_length_in_charset' => 'CP949', + 'lms_supported' => true, + 'lms_supported_country_codes' => array(82), + 'lms_max_length' => 2000, + 'lms_max_length_in_charset' => 'CP949', + 'lms_subject_supported' => true, + 'lms_subject_max_length' => 30, + 'mms_supported' => false, + 'delay_supported' => true, + ); + + /** + * Config keys used by this driver are stored here. + */ + protected static $_required_config = array('api_user'); + + /** + * Check if the current SMS driver is supported on this server. + * + * This method returns true on success and false on failure. + * + * @return bool + */ + public static function isSupported() + { + return true; + } + + /** + * Send a message. + * + * This method returns true on success and false on failure. + * + * @param array $messages + * @param object $original + * @return bool + */ + public function send(array $messages, \Rhymix\Framework\SMS $original) + { + $status = true; + + foreach ($messages as $i => $message) + { + // Authentication and basic information + $data = array(); + $data['userid'] = $this->_config['api_user']; + + // Sender and recipient + $data['callback'] = preg_replace('/[^0-9]/', '', $message->from); + $data['phone'] = implode('|', array_map(function($num) { + return preg_replace('/[^0-9]/', '', $num); + }, $message->to)); + + // Subject and content + if ($message->type === 'LMS' && $message->subject) + { + $data['subject'] = $message->subject; + } + $data['msg'] = $message->content; + + // Set delay + if ($message->delay && $message->delay > time() + 600) + { + $data['appdate'] = gmdate('YmdHis', $message->delay + (3600 * 9)); + } + + // Send! + $url = 'https://www.ppurio.com/api/send_utf8_json.php'; + $result = \FileHandler::getRemoteResource($url, $data, 5, 'POST'); + if(strval($result) === '') + { + $original->addError('Unknown API error while sending message ' . ($i + 1) . ' of ' . count($messages)); + $status = false; + } + else + { + $result = @json_decode($result); + if ($result->result !== 'ok') + { + $original->addError('API error (' . $result->result . ') while sending message ' . ($i + 1) . ' of ' . count($messages)); + $status = false; + } + } + } + + return $status; + } +} diff --git a/tests/unit/framework/SMSTest.php b/tests/unit/framework/SMSTest.php index a60ba5f61..d2d84fc51 100644 --- a/tests/unit/framework/SMSTest.php +++ b/tests/unit/framework/SMSTest.php @@ -16,15 +16,22 @@ class SMSTest extends \Codeception\TestCase\Test { $drivers = Rhymix\Framework\SMS::getSupportedDrivers(); $this->assertTrue(isset($drivers['dummy'])); + $this->assertTrue(isset($drivers['apistore'])); + $this->assertTrue(isset($drivers['cafe24'])); $this->assertTrue(isset($drivers['coolsms'])); $this->assertTrue(isset($drivers['solapi'])); + $this->assertTrue(isset($drivers['ppurio'])); $this->assertEquals('Dummy', $drivers['dummy']['name']); + $this->assertTrue(in_array('api_user', $drivers['apistore']['required'])); + $this->assertTrue(in_array('api_user', $drivers['cafe24']['required'])); $this->assertTrue(in_array('api_key', $drivers['coolsms']['required'])); + $this->assertTrue(in_array('api_user', $drivers['ppurio']['required'])); $this->assertTrue(in_array('api_key', $drivers['solapi']['required'])); $this->assertTrue($drivers['coolsms']['api_spec']['mms_supported']); $this->assertTrue($drivers['coolsms']['api_spec']['delay_supported']); $this->assertTrue($drivers['solapi']['api_spec']['mms_supported']); $this->assertTrue($drivers['solapi']['api_spec']['delay_supported']); + $this->assertFalse($drivers['cafe24']['api_spec']['mms_supported']); } public function testSenderAndRecipients()