mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-04 01:23:32 +09:00
Merge branch 'next' into next-db
This commit is contained in:
commit
99e74a0e20
49 changed files with 2269 additions and 240 deletions
87
common/framework/drivers/push/apns.php
Normal file
87
common/framework/drivers/push/apns.php
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Push;
|
||||
|
||||
/**
|
||||
* The APNs (Apple) Push driver.
|
||||
*/
|
||||
class APNs extends Base implements \Rhymix\Framework\Drivers\PushInterface
|
||||
{
|
||||
/**
|
||||
* Config keys used by this driver are stored here.
|
||||
*/
|
||||
protected static $_required_config = array('certificate', 'passphrase');
|
||||
protected static $_optional_config = array();
|
||||
|
||||
/**
|
||||
* Get the human-readable name of this Push driver.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return 'iOS (APNs)';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current Push driver is supported on this server.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @param object $message
|
||||
* @param array $tokens
|
||||
* @return object
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens)
|
||||
{
|
||||
$output = new \stdClass;
|
||||
$output->success = [];
|
||||
$output->invalid = [];
|
||||
$output->needUpdate = [];
|
||||
|
||||
// Set parameters
|
||||
$local_cert = $this->_config['certificate'];
|
||||
$passphrase = $this->_config['passphrase'];
|
||||
$alert = [];
|
||||
$alert['title'] = $message->getSubject();
|
||||
$alert['body'] = $message->getContent();
|
||||
|
||||
$body['aps'] = array('alert' => $alert);
|
||||
$payload = json_encode($body);
|
||||
|
||||
foreach($tokens as $token)
|
||||
{
|
||||
$ctx = stream_context_create();
|
||||
stream_context_set_option($ctx, 'ssl', 'local_cert', $local_cert);
|
||||
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
|
||||
|
||||
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 5, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
|
||||
if(!$fp)
|
||||
{
|
||||
$message->addError('Failed to connect socket - error code: '. $err .' - '. $errstr);
|
||||
}
|
||||
$msg = chr(0) . pack('n', 32) . pack('H*', $token) . pack('n', strlen($payload)) . $payload;
|
||||
$result = fwrite($fp, $msg, strlen($msg));
|
||||
if(!$result)
|
||||
{
|
||||
$message->addError('APNs return empty response.');
|
||||
}
|
||||
$output->success[] = $token;
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
97
common/framework/drivers/push/base.php
Normal file
97
common/framework/drivers/push/base.php
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Push;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* The base class for other Push drivers.
|
||||
*/
|
||||
abstract class Base implements \Rhymix\Framework\Drivers\PushInterface
|
||||
{
|
||||
/**
|
||||
* The configuration is stored here.
|
||||
*/
|
||||
protected $_config = null;
|
||||
|
||||
/**
|
||||
* Config keys used by this driver are stored here.
|
||||
*/
|
||||
protected static $_required_config = array();
|
||||
protected static $_optional_config = array();
|
||||
|
||||
/**
|
||||
* Direct invocation of the constructor is not permitted.
|
||||
*/
|
||||
protected function __construct(array $config)
|
||||
{
|
||||
$this->_config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of the current Push driver, using the given settings.
|
||||
*
|
||||
* @param array $config
|
||||
* @return object
|
||||
*/
|
||||
public static function getInstance(array $config): object
|
||||
{
|
||||
return new static($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the human-readable name of this Push driver.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return class_basename(get_called_class());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this Push driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig(): array
|
||||
{
|
||||
return static::$_required_config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields optionally used by this Push driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getOptionalConfig(): array
|
||||
{
|
||||
return static::$_optional_config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current Push driver is supported on this server.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @param object $message
|
||||
* @param array $tokens
|
||||
* @return object
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens)
|
||||
{
|
||||
return new \stdClass;
|
||||
}
|
||||
}
|
||||
114
common/framework/drivers/push/fcm.php
Normal file
114
common/framework/drivers/push/fcm.php
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\Push;
|
||||
|
||||
/**
|
||||
* The FCM (Google) Push driver.
|
||||
*/
|
||||
class FCM extends Base implements \Rhymix\Framework\Drivers\PushInterface
|
||||
{
|
||||
/**
|
||||
* Config keys used by this driver are stored here.
|
||||
*/
|
||||
protected static $_required_config = array('api_key');
|
||||
protected static $_optional_config = array();
|
||||
|
||||
/**
|
||||
* Get the human-readable name of this Push driver.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName(): string
|
||||
{
|
||||
return 'Android (FCM)';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current Push driver is supported on this server.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isSupported(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @param object $message
|
||||
* @param array $tokens
|
||||
* @return object
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens)
|
||||
{
|
||||
$output = new \stdClass;
|
||||
$output->success = [];
|
||||
$output->invalid = [];
|
||||
$output->needUpdate = [];
|
||||
|
||||
$url = 'https://fcm.googleapis.com/fcm/send';
|
||||
$api_key = $this->_config['api_key'];
|
||||
$headers = array(
|
||||
'Authorization' => 'key=' . $api_key,
|
||||
'Content-Type' => 'application/json',
|
||||
);
|
||||
|
||||
// Set notification
|
||||
$notification = [];
|
||||
$notification['title'] = $message->getSubject();
|
||||
$notification['body'] = $message->getContent();
|
||||
if($message->getClickAction())
|
||||
{
|
||||
$notification['click_action'] = $message->getClickAction();
|
||||
}
|
||||
|
||||
$chunked_token = array_chunk($tokens, 1000);
|
||||
foreach($chunked_token as $token_unit)
|
||||
{
|
||||
$data = json_encode(array(
|
||||
'registration_ids' => $token_unit,
|
||||
'notification' => $notification,
|
||||
'priority' => 'normal',
|
||||
'data' => $message->getData() ?: new \stdClass,
|
||||
));
|
||||
|
||||
$response = \FileHandler::getRemoteResource($url, $data, 5, 'POST', 'application/json', $headers);
|
||||
if($response)
|
||||
{
|
||||
$decoded_response = json_decode($response);
|
||||
if(!$decoded_response)
|
||||
{
|
||||
$message->addError('FCM return invalid json : '. $response);
|
||||
return $output;
|
||||
}
|
||||
$results = $decoded_response->results ?: [];
|
||||
foreach($results as $i => $result)
|
||||
{
|
||||
if($result->error)
|
||||
{
|
||||
$message->addError('FCM error code: '. $result->error);
|
||||
$output->invalid[$token_unit[$i]] = $token_unit[$i];
|
||||
}
|
||||
else if($result->message_id && $result->registration_id)
|
||||
{
|
||||
$output->needUpdate[$token_unit[$i]] = $result->registration_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
$output->success[$token_unit[$i]] = $result->message_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$message->addError('FCM return empty response.');
|
||||
}
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
58
common/framework/drivers/pushinterface.php
Normal file
58
common/framework/drivers/pushinterface.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers;
|
||||
|
||||
/**
|
||||
* The Push driver interface.
|
||||
*/
|
||||
interface PushInterface
|
||||
{
|
||||
/**
|
||||
* Create a new instance of the current Push driver, using the given settings.
|
||||
*
|
||||
* @param array $config
|
||||
* @return void
|
||||
*/
|
||||
public static function getInstance(array $config): object;
|
||||
|
||||
/**
|
||||
* Get the human-readable name of this Push driver.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName(): string;
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields required by this Push driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getRequiredConfig(): array;
|
||||
|
||||
/**
|
||||
* Get the list of configuration fields optionally used by this Push driver.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getOptionalConfig(): array;
|
||||
|
||||
/**
|
||||
* 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(): bool;
|
||||
|
||||
/**
|
||||
* Send a message.
|
||||
*
|
||||
* This method returns true on success and false on failure.
|
||||
*
|
||||
* @param object $message
|
||||
* @param array $tokens
|
||||
* @return object
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens);
|
||||
}
|
||||
141
common/framework/drivers/sms/apistore.php
Normal file
141
common/framework/drivers/sms/apistore.php
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\SMS;
|
||||
|
||||
/**
|
||||
* The ApiStore SMS driver.
|
||||
*/
|
||||
class ApiStore extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
||||
{
|
||||
/**
|
||||
* API specifications.
|
||||
*/
|
||||
protected static $_spec = array(
|
||||
'max_recipients' => 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;
|
||||
}
|
||||
}
|
||||
116
common/framework/drivers/sms/cafe24.php
Normal file
116
common/framework/drivers/sms/cafe24.php
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\SMS;
|
||||
|
||||
/**
|
||||
* The Cafe24 SMS driver.
|
||||
*/
|
||||
class Cafe24 extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
||||
{
|
||||
/**
|
||||
* API specifications.
|
||||
*/
|
||||
protected static $_spec = array(
|
||||
'max_recipients' => 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;
|
||||
}
|
||||
}
|
||||
103
common/framework/drivers/sms/ppurio.php
Normal file
103
common/framework/drivers/sms/ppurio.php
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Drivers\SMS;
|
||||
|
||||
/**
|
||||
* The Ppurio SMS driver.
|
||||
*/
|
||||
class Ppurio extends Base implements \Rhymix\Framework\Drivers\SMSInterface
|
||||
{
|
||||
/**
|
||||
* API specifications.
|
||||
*/
|
||||
protected static $_spec = array(
|
||||
'max_recipients' => 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;
|
||||
}
|
||||
}
|
||||
483
common/framework/push.php
Normal file
483
common/framework/push.php
Normal file
|
|
@ -0,0 +1,483 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework;
|
||||
|
||||
/**
|
||||
* The Push class.
|
||||
*/
|
||||
class Push
|
||||
{
|
||||
/**
|
||||
* Instance properties.
|
||||
*/
|
||||
protected $from = 0;
|
||||
protected $to = array();
|
||||
protected $subject = '';
|
||||
protected $content = '';
|
||||
protected $click_action = '';
|
||||
protected $data = [];
|
||||
protected $errors = array();
|
||||
protected $success_tokens = array();
|
||||
protected $deleted_tokens = array();
|
||||
protected $updated_tokens = array();
|
||||
protected $sent = false;
|
||||
|
||||
/**
|
||||
* Static properties.
|
||||
*/
|
||||
protected static $_drivers = array();
|
||||
|
||||
/**
|
||||
* Add a custom Push driver.
|
||||
*
|
||||
* @param string $name
|
||||
* @param object $driver
|
||||
* @return void
|
||||
*/
|
||||
public static function addDriver(string $name, Drivers\PushInterface $driver)
|
||||
{
|
||||
self::$_drivers[$name] = $driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the default driver.
|
||||
*
|
||||
* @param string $name
|
||||
* @return object|null
|
||||
*/
|
||||
public static function getDriver(string $name)
|
||||
{
|
||||
if (isset(self::$_drivers[$name]))
|
||||
{
|
||||
return self::$_drivers[$name];
|
||||
}
|
||||
|
||||
$driver_class = '\Rhymix\Framework\Drivers\Push\\' . $name;
|
||||
if (class_exists($driver_class))
|
||||
{
|
||||
$driver_config = config('push.' . $name) ?: array();
|
||||
return self::$_drivers[$name] = $driver_class::getInstance($driver_config);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of supported Push drivers.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function getSupportedDrivers()
|
||||
{
|
||||
$result = array();
|
||||
foreach (Storage::readDirectory(__DIR__ . '/drivers/push', false) as $filename)
|
||||
{
|
||||
$driver_name = substr($filename, 0, -4);
|
||||
$class_name = '\Rhymix\Framework\Drivers\Push\\' . $driver_name;
|
||||
if ($class_name::isSupported())
|
||||
{
|
||||
$result[$driver_name] = array(
|
||||
'name' => $class_name::getName(),
|
||||
'required' => $class_name::getRequiredConfig(),
|
||||
'optional' => $class_name::getOptionalConfig(),
|
||||
);
|
||||
}
|
||||
}
|
||||
foreach (self::$_drivers as $driver_name => $driver)
|
||||
{
|
||||
if ($driver->isSupported())
|
||||
{
|
||||
$result[$driver_name] = array(
|
||||
'name' => $driver->getName(),
|
||||
'required' => $driver->getRequiredConfig(),
|
||||
'optional' => $driver->getOptionalConfig(),
|
||||
);
|
||||
}
|
||||
}
|
||||
ksort($result);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the sender's member_srl.
|
||||
*
|
||||
* @param int $member_srl
|
||||
* @return bool
|
||||
*/
|
||||
public function setFrom(int $member_srl): bool
|
||||
{
|
||||
$this->from = $member_srl;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sender's phone number.
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
public function getFrom(): int
|
||||
{
|
||||
return intval($this->from);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a recipient.
|
||||
*
|
||||
* @param int $member_srl
|
||||
* @return bool
|
||||
*/
|
||||
public function addTo(int $member_srl): bool
|
||||
{
|
||||
$this->to[] = $member_srl;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of recipients without country codes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRecipients(): array
|
||||
{
|
||||
return $this->to;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the subject.
|
||||
*
|
||||
* @param string $subject
|
||||
* @return bool
|
||||
*/
|
||||
public function setSubject(string $subject): bool
|
||||
{
|
||||
$this->subject = utf8_trim(utf8_clean($subject));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the subject.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSubject(): string
|
||||
{
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the content.
|
||||
*
|
||||
* @param string $content
|
||||
* @return bool
|
||||
*/
|
||||
public function setContent(string $content): bool
|
||||
{
|
||||
$this->content = utf8_trim(utf8_clean($content));
|
||||
$this->content = strtr($this->content, array("\r\n" => "\n"));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getContent(): string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an click-action to associate with this push notification.
|
||||
*
|
||||
* @param string $click_action
|
||||
* @return bool
|
||||
*/
|
||||
public function setClickAction(string $click_action): bool
|
||||
{
|
||||
$this->click_action = utf8_trim(utf8_clean($click_action));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the click-action associated with this push notification.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getClickAction(): string
|
||||
{
|
||||
return $this->click_action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a data to associate with this push notification.
|
||||
*
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public function setData(array $data): bool
|
||||
{
|
||||
$this->data = $data;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data associated with this push notification.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a URL to associate with this push notification.
|
||||
*
|
||||
* @param string $url
|
||||
* @return bool
|
||||
*/
|
||||
public function setURL(string $url): bool
|
||||
{
|
||||
$this->data['url'] = $url;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URL associated with this push notification.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getURL(): string
|
||||
{
|
||||
return $this->data['url'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the message.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function send(): bool
|
||||
{
|
||||
// Get caller information.
|
||||
$backtrace = debug_backtrace(0);
|
||||
if(count($backtrace) && isset($backtrace[0]['file']))
|
||||
{
|
||||
$this->caller = $backtrace[0]['file'] . ($backtrace[0]['line'] ? (' line ' . $backtrace[0]['line']) : '');
|
||||
}
|
||||
|
||||
$output = \ModuleHandler::triggerCall('push.send', 'before', $this);
|
||||
if(!$output->toBool())
|
||||
{
|
||||
$this->errors[] = $output->getMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
$tokens = $this->_getDeviceTokens();
|
||||
$output = null;
|
||||
|
||||
// Android FCM
|
||||
if(count($tokens->android))
|
||||
{
|
||||
$fcm_driver = $this->getDriver('fcm');
|
||||
$output = $fcm_driver->send($this, $tokens->android);
|
||||
$this->sent = count($output->success) ? true : false;
|
||||
$this->success_tokens = $output ? $output->success : [];
|
||||
$this->deleted_tokens = $output ? $output->invalid : [];
|
||||
$this->updated_tokens = $output ? $output->needUpdate : [];
|
||||
$this->_deleteInvalidTokens($output->invalid);
|
||||
$this->_updateDeviceTokens($output->needUpdate);
|
||||
}
|
||||
|
||||
// iOS APNs
|
||||
if(count($tokens->ios))
|
||||
{
|
||||
$apns_driver =$this->getDriver('apns');
|
||||
$output = $apns_driver->send($this, $tokens->ios);
|
||||
$this->sent = count($output->success) ? true : false;
|
||||
$this->success_tokens += $output ? $output->success : [];
|
||||
$this->deleted_tokens += $output ? $output->invalid : [];
|
||||
$this->updated_tokens += $output ? $output->needUpdate : [];
|
||||
$this->_deleteInvalidTokens($output->invalid);
|
||||
$this->_updateDeviceTokens($output->needUpdate);
|
||||
}
|
||||
|
||||
}
|
||||
catch(\Exception $e)
|
||||
{
|
||||
$this->errors[] = class_basename($e) . ': ' . $e->getMessage();
|
||||
$this->sent = false;
|
||||
}
|
||||
|
||||
$output = \ModuleHandler::triggerCall('push.send', 'after', $this);
|
||||
if(!$output->toBool())
|
||||
{
|
||||
$this->errors[] = $output->getMessage();
|
||||
}
|
||||
|
||||
return $this->sent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the device token
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
*/
|
||||
protected function _getDeviceTokens()
|
||||
{
|
||||
$member_srl_list = $this->getRecipients();
|
||||
$result = new \stdClass;
|
||||
$result->android = [];
|
||||
$result->ios = [];
|
||||
|
||||
$args = new \stdClass;
|
||||
$args->member_srl = $member_srl_list;
|
||||
$args->device_type = [];
|
||||
$driver_types = config('push.types') ?: array();
|
||||
if(!count($driver_types))
|
||||
{
|
||||
return $result;
|
||||
}
|
||||
if(isset($driver_types['fcm']))
|
||||
{
|
||||
$args->device_type[] = 'android';
|
||||
}
|
||||
if(isset($driver_types['apns']))
|
||||
{
|
||||
$args->device_type[] = 'ios';
|
||||
}
|
||||
|
||||
$output = executeQueryArray('member.getMemberDeviceTokensByMemberSrl', $args);
|
||||
if(!$output->toBool() || !$output->data)
|
||||
{
|
||||
return $result;
|
||||
}
|
||||
|
||||
foreach($output->data as $row)
|
||||
{
|
||||
$result->{$row->device_type}[] = $row->device_token;
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the device toekn
|
||||
*
|
||||
* @param array
|
||||
* @return void
|
||||
*/
|
||||
protected function _deleteInvalidTokens(array $invalid_tokens)
|
||||
{
|
||||
if(!count($invalid_tokens))
|
||||
{
|
||||
return;
|
||||
}
|
||||
$args = new \stdClass;
|
||||
$args->device_token = $invalid_tokens;
|
||||
executeQueryArray('member.deleteMemberDevice', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the device toekn
|
||||
*
|
||||
* @param array
|
||||
* @return void
|
||||
*/
|
||||
protected function _updateDeviceTokens(array $update_tokens)
|
||||
{
|
||||
$args = new \stdClass;
|
||||
foreach($update_tokens as $key => $value)
|
||||
{
|
||||
$args->old_token = $key;
|
||||
$args->new_token = $value;
|
||||
executeQueryArray('member.updateMemberDevice', $args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the message was sent.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isSent(): bool
|
||||
{
|
||||
return $this->sent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get caller information.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCaller(): string
|
||||
{
|
||||
return $this->caller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get errors.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getErrors(): array
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get success tokens.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSuccessTokens(): array
|
||||
{
|
||||
return $this->success_tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get deleted tokens.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getDeletedTokens(): array
|
||||
{
|
||||
return $this->deleted_tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get updated tokens.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUpdatedTokens(): array
|
||||
{
|
||||
return $this->updated_tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an error message.
|
||||
*
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
public function addError(string $message)
|
||||
{
|
||||
$this->errors[] = $message;
|
||||
}
|
||||
}
|
||||
|
|
@ -14,6 +14,18 @@ class UA
|
|||
protected static $_tablet_cache = array();
|
||||
protected static $_robot_cache = array();
|
||||
|
||||
/**
|
||||
* Windows version lookup table.
|
||||
*/
|
||||
protected static $_windows_versions = array(
|
||||
'5.1' => 'XP',
|
||||
'5.2' => 'XP',
|
||||
'6.0' => 'Vista',
|
||||
'6.1' => '7',
|
||||
'6.2' => '8',
|
||||
'6.3' => '8.1',
|
||||
);
|
||||
|
||||
/**
|
||||
* Check whether the current visitor is using a mobile device.
|
||||
*
|
||||
|
|
@ -181,6 +193,8 @@ class UA
|
|||
'browser' => null,
|
||||
'version' => null,
|
||||
'os' => null,
|
||||
'os_version' => null,
|
||||
'device' => null,
|
||||
'is_mobile' => null,
|
||||
'is_tablet' => null,
|
||||
'is_webview' => null,
|
||||
|
|
@ -197,18 +211,36 @@ class UA
|
|||
if ($matches[1] === 'Linux' && strpos($ua, 'Android') !== false)
|
||||
{
|
||||
$result->os = 'Android';
|
||||
if (preg_match('#Android ([0-9\.]+);(?: ([^;]+) Build/)?#', $ua, $matches))
|
||||
{
|
||||
$result->os_version = $matches[1];
|
||||
$result->device = $matches[2] ?: null;
|
||||
}
|
||||
}
|
||||
elseif ($matches[1] === 'iPhone' || $matches[1] === 'iPad' || $matches[1] === 'iPod')
|
||||
{
|
||||
$result->os = 'iOS';
|
||||
if (preg_match('#(i(?:Phone|P[ao]d)); (?:.+?) OS ([0-9_\.]+)#s', $ua, $matches))
|
||||
{
|
||||
$result->os_version = str_replace('_', '.', $matches[2]);
|
||||
$result->device = $matches[1];
|
||||
}
|
||||
}
|
||||
elseif ($matches[1] === 'Macintosh' || $matches[1] === 'OS X')
|
||||
{
|
||||
$result->os = 'macOS';
|
||||
if (preg_match('#OS X ([0-9_\.]+)#', $ua, $matches))
|
||||
{
|
||||
$result->os_version = str_replace('_', '.', $matches[1]);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$result->os = $matches[1];
|
||||
if (preg_match('#Windows NT ([0-9\.]+)#', $ua, $matches))
|
||||
{
|
||||
$result->os_version = isset(self::$_windows_versions[$matches[1]]) ? self::$_windows_versions[$matches[1]] : $matches[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue