mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-02 00:32:15 +09:00
Merge branch 'rhymix:develop' into develop
This commit is contained in:
commit
10e2116dd9
13 changed files with 350 additions and 296 deletions
|
|
@ -1,13 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\SMS;
|
||||
use Rhymix\Framework\HTTP;
|
||||
use Rhymix\Framework\Security;
|
||||
|
||||
/**
|
||||
* The Solapi SMS driver.
|
||||
*/
|
||||
class SolAPI extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
||||
{
|
||||
const appId = 'PAOe9c8ftH8R';
|
||||
const BASEURL = 'https://api.solapi.com';
|
||||
const TIMEOUT = 5;
|
||||
const APPID = 'PAOe9c8ftH8R';
|
||||
|
||||
/**
|
||||
* API specifications.
|
||||
|
|
@ -30,7 +34,7 @@ class SolAPI extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
|||
'mms_subject_max_length' => 40,
|
||||
'image_allowed_types' => array('jpg', 'gif', 'png'),
|
||||
'image_max_dimensions' => array(2048, 2048),
|
||||
'image_max_filesize' => 300000,
|
||||
'image_max_filesize' => 200000,
|
||||
'delay_supported' => true,
|
||||
);
|
||||
|
||||
|
|
@ -38,7 +42,7 @@ class SolAPI extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
|||
* Config keys used by this driver are stored here.
|
||||
*/
|
||||
protected static $_required_config = array('api_key', 'api_secret');
|
||||
protected static $_optional_config = array('sender_key');
|
||||
protected static $_optional_config = array();
|
||||
|
||||
/**
|
||||
* Check if the current SMS driver is supported on this server.
|
||||
|
|
@ -63,166 +67,141 @@ class SolAPI extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
|||
*/
|
||||
public function send(array $messages, \Rhymix\Framework\SMS $original)
|
||||
{
|
||||
$groupArray = array();
|
||||
$groupMessage = false;
|
||||
if(count($messages) > 1)
|
||||
{
|
||||
$groupMessage = true;
|
||||
}
|
||||
$data = [
|
||||
'messages' => [],
|
||||
'agent' => [
|
||||
'appId' => self::APPID,
|
||||
]
|
||||
];
|
||||
$images = [];
|
||||
$schedule = null;
|
||||
$status = true;
|
||||
|
||||
foreach ($messages as $i => $message)
|
||||
{
|
||||
if (count($message->to) > 1 && !$groupMessage)
|
||||
{
|
||||
$groupMessage = true;
|
||||
}
|
||||
$options = new \stdClass;
|
||||
if ($this->_config['sender_key'])
|
||||
{
|
||||
$options->sender_key = $this->_config['sender_key'];
|
||||
$options->type = 'CTA';
|
||||
}
|
||||
else
|
||||
foreach ($message->to as $recipient)
|
||||
{
|
||||
$options = new \stdClass;
|
||||
$options->to = $recipient;
|
||||
$options->from = $message->from;
|
||||
$options->text = $message->content ?: $message->type;
|
||||
$options->type = $message->type;
|
||||
}
|
||||
$options->from = $message->from;
|
||||
$options->to = $message->to;
|
||||
$options->text = $message->content ?: $message->type;
|
||||
if ($message->delay && $message->delay > time())
|
||||
{
|
||||
$options->datetime = gmdate('YmdHis', $message->delay + (3600 * 9));
|
||||
}
|
||||
if ($message->country && $message->country != 82)
|
||||
{
|
||||
$options->country = $message->country;
|
||||
}
|
||||
if ($message->subject)
|
||||
{
|
||||
$options->subject = $message->subject;
|
||||
}
|
||||
else
|
||||
{
|
||||
if($message->type != 'SMS')
|
||||
|
||||
if ($message->delay && $message->delay > time())
|
||||
{
|
||||
$schedule = date('c', $message->delay);
|
||||
}
|
||||
|
||||
if ($message->country && $message->country != 82)
|
||||
{
|
||||
$options->country = $message->country;
|
||||
}
|
||||
|
||||
if ($message->subject)
|
||||
{
|
||||
$options->subject = $message->subject;
|
||||
}
|
||||
elseif ($message->type != 'SMS')
|
||||
{
|
||||
// 문자 전송 타입이 SMS이 아닐경우 subjext가 필수
|
||||
$options->subject = cut_str($message->content, 20);
|
||||
}
|
||||
|
||||
if ($message->image)
|
||||
{
|
||||
if (isset($images[$message->image]))
|
||||
{
|
||||
$imageId = $images[$message->image];
|
||||
}
|
||||
else
|
||||
{
|
||||
$output = $this->_uploadImage($message->image, $message->type);
|
||||
if (!empty($output->fileId))
|
||||
{
|
||||
$imageId = $images[$message->image] = $output->fileId;
|
||||
}
|
||||
else
|
||||
{
|
||||
$imageId = null;
|
||||
}
|
||||
}
|
||||
|
||||
if ($imageId)
|
||||
{
|
||||
$options->imageId = $imageId;
|
||||
}
|
||||
}
|
||||
|
||||
$data['messages'][] = $options;
|
||||
}
|
||||
if ($message->image)
|
||||
{
|
||||
$output = $this->uploadImage($message->image, $message->type);
|
||||
$options->imageId = $output->fileId;
|
||||
}
|
||||
$groupArray[] = $options;
|
||||
}
|
||||
|
||||
if($groupMessage)
|
||||
if ($schedule)
|
||||
{
|
||||
$jsonObject = new \stdClass();
|
||||
$jsonObject->messages = json_encode($groupArray);
|
||||
$groupId = $this->createGroup();
|
||||
if(!$groupId)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = json_decode($this->request("PUT", "messages/v4/groups/{$groupId}/messages", $jsonObject));
|
||||
if(!$result || $result->errorCode)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$result = json_decode($this->request("POST", "messages/v4/groups/{$groupId}/send"));
|
||||
if (!$result || $result->status != 'SENDING')
|
||||
{
|
||||
return false;
|
||||
}
|
||||
$data['scheduledDate'] = $schedule;
|
||||
}
|
||||
else
|
||||
|
||||
// Send all messages, and record failed messages.
|
||||
$result = json_decode($this->_request('POST', '/messages/v4/send-many/detail', $data));
|
||||
if (isset($result->failedMessageList) && is_array($result->failedMessageList) && count($result->failedMessageList))
|
||||
{
|
||||
// simpleMessage 를 사용 할 경우 to가 array 타입이면 문자 전송이 되지 않아 string 으로 요청
|
||||
$groupArray[0]->to = $groupArray[0]->to[0];
|
||||
$simpleObject = new \stdClass();
|
||||
$simpleObject->message = $groupArray[0];
|
||||
$simpleObject->agent = new \stdClass();
|
||||
$simpleObject->agent->appId = self::appId;
|
||||
|
||||
$result = json_decode($this->request("POST", "messages/v4/send", $simpleObject));
|
||||
if(!$result || $result->errorCode)
|
||||
foreach ($result->failedMessageList as $fail)
|
||||
{
|
||||
return false;
|
||||
$original->addError('Error while sending message to ' . $fail->to . ': ' . trim($fail->statusCode . ' ' . $fail->statusMessage));
|
||||
}
|
||||
$status = false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create header string for http protocol
|
||||
* Create header string for authorization.
|
||||
*
|
||||
* @param $config
|
||||
* @return string
|
||||
*/
|
||||
private function getHeader()
|
||||
protected function _getHeader()
|
||||
{
|
||||
date_default_timezone_set('Asia/Seoul');
|
||||
$date = date('Y-m-d\TH:i:s.Z\Z', time());
|
||||
$salt = uniqid();
|
||||
$date = gmdate('Y-m-d\TH:i:s\Z');
|
||||
$salt = Security::getRandom(32);
|
||||
$signature = hash_hmac('sha256', $date . $salt, $this->_config['api_secret']);
|
||||
return "HMAC-SHA256 apiKey={$this->_config['api_key']}, date={$date}, salt={$salt}, signature={$signature}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Create message group
|
||||
* @return string : group id
|
||||
*/
|
||||
private function createGroup()
|
||||
{
|
||||
$args = new \stdClass();
|
||||
$args->appId = self::appId;
|
||||
$result = $this->request("POST", 'messages/v4/groups', $args);
|
||||
$groupId = json_decode($result)->groupId;
|
||||
return $groupId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload to image for MMS message.
|
||||
* @param $imageDir
|
||||
* Upload an image for MMS message.
|
||||
*
|
||||
* @param $path
|
||||
* @param $type
|
||||
* @return mixed
|
||||
*/
|
||||
private function uploadImage($imageDir, $type)
|
||||
protected function _uploadImage($path, $type)
|
||||
{
|
||||
$path = $imageDir;
|
||||
$data = file_get_contents($path);
|
||||
$imageData = base64_encode($data);
|
||||
$jsonData = new \stdClass();
|
||||
$jsonData->file = $imageData;
|
||||
$jsonData = new \stdClass;
|
||||
$jsonData->file = base64_encode(file_get_contents($path));
|
||||
$jsonData->type = $type;
|
||||
$url = "storage/v1/files";
|
||||
return json_decode($this->request('POST', $url, $jsonData));
|
||||
$url = '/storage/v1/files';
|
||||
return json_decode($this->_request('POST', $url, $jsonData));
|
||||
}
|
||||
|
||||
/**
|
||||
* Request string message.
|
||||
* @param $method
|
||||
* @param $url
|
||||
* @param bool $data
|
||||
* @return bool|string
|
||||
* @param array|object|null $data
|
||||
* @return string
|
||||
*/
|
||||
private function request($method, $url, $data = false)
|
||||
protected function _request($method, $url, $data = null)
|
||||
{
|
||||
$url = 'https://api.solapi.com/' . $url;
|
||||
$url = self::BASEURL . $url;
|
||||
$data = $data ? json_encode($data) : null;
|
||||
$headers = [
|
||||
'Authorization' => $this->_getHeader(),
|
||||
'Content-Type' => 'application/json',
|
||||
];
|
||||
|
||||
if(!$data)
|
||||
{
|
||||
$data = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
$data = json_encode($data);
|
||||
}
|
||||
$result = \FileHandler::getRemoteResource($url, $data, 3, $method, 'application/json', array('Authorization' => $this->getHeader()));
|
||||
|
||||
return $result;
|
||||
$result = HTTP::request($url, $method, $data, $headers, [], ['timeout' => self::TIMEOUT]);
|
||||
return $result->getBody()->getContents();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
130
common/framework/drivers/sms/twilio.php
Normal file
130
common/framework/drivers/sms/twilio.php
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\SMS;
|
||||
use Rhymix\Framework\HTTP;
|
||||
use Rhymix\Framework\Storage;
|
||||
use RHymix\Framework\URL;
|
||||
|
||||
/**
|
||||
* The Twilio SMS driver.
|
||||
*/
|
||||
class Twilio extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
||||
{
|
||||
/**
|
||||
* API Base URL.
|
||||
*/
|
||||
const BASEURL = 'https://api.twilio.com/2010-04-01';
|
||||
|
||||
/**
|
||||
* API specifications.
|
||||
*/
|
||||
protected static $_spec = array(
|
||||
'max_recipients' => 100,
|
||||
'sms_max_length' => 70,
|
||||
'sms_max_length_in_charset' => 'UTF-16LE',
|
||||
'lms_supported' => true,
|
||||
'lms_supported_country_codes' => array(),
|
||||
'lms_max_length' => 1600,
|
||||
'lms_max_length_in_charset' => 'UTF-16LE',
|
||||
'lms_subject_supported' => false,
|
||||
'lms_subject_max_length' => 0,
|
||||
'mms_supported' => true,
|
||||
'mms_supported_country_codes' => array(),
|
||||
'mms_max_length' => 1600,
|
||||
'mms_max_length_in_charset' => 'UTF-16LE',
|
||||
'mms_subject_supported' => false,
|
||||
'mms_subject_max_length' => 0,
|
||||
'image_allowed_types' => array('jpg', 'gif', 'png'),
|
||||
'image_max_dimensions' => array(2048, 2048),
|
||||
'image_max_filesize' => 5000000,
|
||||
'delay_supported' => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* Config keys used by this driver are stored here.
|
||||
*/
|
||||
protected static $_required_config = ['account_sid', 'auth_token'];
|
||||
protected static $_optional_config = [];
|
||||
|
||||
/**
|
||||
* 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;
|
||||
$url = sprintf('%s/Accounts/%s/Messages.json', self::BASEURL, $this->_config['account_sid']);
|
||||
$settings = [
|
||||
'auth' => [$this->_config['account_sid'], $this->_config['auth_token']],
|
||||
'timeout => 10',
|
||||
];
|
||||
|
||||
foreach ($messages as $i => $message)
|
||||
{
|
||||
$from = '+' . preg_replace('/[^0-9]/', '', $message->from);
|
||||
foreach ($message->to as $recipient)
|
||||
{
|
||||
$data = [];
|
||||
$data['To'] = sprintf('+%s%s', $message->country ?: 82, preg_replace('/[^0-9]/', '', $recipient));
|
||||
$data['From'] = $from;
|
||||
$data['Body'] = $message->content;
|
||||
if ($message->delay && $message->delay > time())
|
||||
{
|
||||
$data['SendAt'] = gmdate('Y-m-d\TH:i:s\Z', $message->delay);
|
||||
}
|
||||
if ($message->type === 'MMS')
|
||||
{
|
||||
$data['SendAsMms'] = true;
|
||||
}
|
||||
if ($message->image && Storage::isFile($message->image))
|
||||
{
|
||||
$media_url = URL::fromServerPath($message->image);
|
||||
if ($media_url)
|
||||
{
|
||||
$data['MediaUrl'][] = $media_url;
|
||||
}
|
||||
}
|
||||
|
||||
$request = HTTP::request($url, 'POST', $data, [], [], $settings);
|
||||
$status_code = $request->getStatusCode();
|
||||
if ($status_code < 200 || $status_code >= 400)
|
||||
{
|
||||
$response = $request->getBody()->getContents();
|
||||
if ($response)
|
||||
{
|
||||
$response = json_decode($response);
|
||||
$error_code = $response->error_code;
|
||||
$error_message = $response->error_message;
|
||||
}
|
||||
else
|
||||
{
|
||||
$error_code = null;
|
||||
$error_message = null;
|
||||
}
|
||||
$original->addError('Error (' . $status_code . ') while sending message ' . ($i + 1) . ' of ' . count($messages) .
|
||||
' to ' . $data['To'] . ': ' . trim($error_code . ' ' . $error_message));
|
||||
$status = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue