123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584 |
- <?php
- /**
- * Oll.tv ispAPI class
- * @author Prakapas Andriy <prakapas@general-servers.com>
- * @copyright 2016 GeneralServers LLC
- * @license http://www.apache.org/licenses/LICENSE-2.0
- * @version 2.1.0 - for same ispAPI version 2.1.0
- * @link https://general-servers.com
- * @link https://github.com/General-Servers/Oll.tv
- */
- class OllTv {
- /* Oll.tv constants */
- const OTV_URL = 'http://oll.tv/'; // main production url
- const OTV_URL_DEV = 'http://dev.oll.tv/'; // development url for test
- const OTV_URL_API = 'ispAPI'; // main api url
- const OTV_URL_AUTH = 'auth2'; // auth url
- /* log constants */
- const ERROR = 1;
- const WARNING = 2;
- const INFO = 3;
- /**
- * Client ispAPI login
- * Assign in constructor
- * @var string
- */
- protected $_login;
- /**
- * Client ispAPI password
- * Assign in constructor
- * @var string
- */
- protected $_password;
- /**
- * ispAPI hash
- * Hash return ispAPI; using in all requests
- * @var string
- */
- protected $_hash;
- /**
- * Result variable
- * Return ispAPI
- * @var string
- */
- protected $_result;
- /**
- * ispAPI url
- * @var string
- */
- protected $_url;
- /* log vars */
- /**
- * Path to log file
- * @var string
- */
- protected $_log;
- /**
- * Log level variable
- * 0 - not show any messages
- * 1 - only Errors - DEFAULT
- * 2 - Errors, Warnings
- * 3 - Errors, Warnings and Informations
- * @var integer
- */
- protected $_logLevel = 1;
- /**
- * Log message type
- * Related with $_logLevel var
- * @var array
- */
- protected $_logType = array(
- 0 => '',
- 1 => 'Error',
- 2 => 'Warning',
- 3 => 'Info'
- );
- /* --end log vars */
- /* error vars */
- /**
- * ispAPI errors array:
- * key - error status
- * value has short message and full description
- * @var array
- */
- protected $_errors = array(
- 109 => array(
- 'message' => 'Hash expired',
- 'description' => 'Время действия хеша истекло или хеш не верен'
- ),
- 110 => array(
- 'message' => 'Authorization missed',
- 'description' => 'Хеш не указан'
- ),
- 111 => array(
- 'message' => 'Auth failed',
- 'description' => 'Неверный логин или пароль'
- ),
- 112 => array(
- 'message' => 'Login empty',
- 'description' => 'Не указан логин'
- ),
- 113 => array(
- 'message' => 'Password empty',
- 'description' => 'Не указан пароль'
- ),
- 115 => array(
- 'message' => 'Email already exists',
- 'description' => 'Указанный имейл уже есть в БД'
- ),
- 116 => array(
- 'message' => 'Email validation failed',
- 'description' => 'Формат указанного имейла неверен'
- ),
- 117 => array(
- 'message' => 'Result user account does not match provided',
- 'description' => 'Не указан аккаунт или он не совпадает с аккаунтом на который подвязано устройство'
- ),
- 119 => array(
- 'message' => 'Device with provided mac or/and serial number already exist',
- 'description' => 'Устройство с указанным мак-адресом и/или серийным номером уже присуствует в БД и за кем-то закреплено'
- ),
- 120 => array(
- 'message' => 'Wrong date format',
- 'description' => "Неверный формат даты"
- ),
- 200 => array(
- 'message' => 'Required fields missed',
- 'description' => "Остутствуют необходимые параметры"
- ),
- 201 => array(
- 'message' => 'Field email is required',
- 'description' => "Отсутствует необходимый параметр email"
- ),
- 203 => array(
- 'message' => 'Neither mac nor serial_number was found in your request',
- 'description' => "Отсутствуют параметры mac и serial_number"
- ),
- 205 => array(
- 'message' => 'Field new_email is required',
- 'description' => "Отсутствует необходимый параметр new_email"
- ),
- 301 => array(
- 'message' => 'Registration failed. Contact technical support',
- 'description' => "Ошибка добавления устройства пользователю или регистрации нового пользователя"
- ),
- 302 => array(
- 'message' => 'Wrong MAC address',
- 'description' => "Неверный формат мак-адреса"
- ),
- 303 => array(
- 'message' => 'Wrong Serial number',
- 'description' => "Неверный формат серийного номера"
- ),
- 304 => array(
- 'message' => 'Invalid binding code',
- 'description' => "Неверный код привязки устройства"
- ),
- 305 => array(
- 'message' => 'No devices can be binded by this code',
- 'description' => "Достигнут лимит кол-ва устройств, которые можно привязать по указанному коду привязки"
- ),
- 404 => array(
- 'message' => "Account not found",
- 'description' => "Пользователь не найден в БД"
- ),
- 405 => array(
- 'message' => "Not eligible device_type",
- 'description' => "Недопустимое значение в параметре device_type"
- ),
- 406 => array(
- 'message' => "Device not found in our DB",
- 'description' => "Устройство не найдено в БД или оно отвязано от пользователя",
- ),
- 407 => array(
- 'message' => "Subscription not found",
- 'description' => "Подписка, указанная в параметрах sub_id, new_sub_id или old_sub_id, не найдена"
- ),
- 408 => array(
- 'message' => "Subscription order violation",
- 'description' => "Нарушение очерёдности отключения или включения услуги согласно подписке"
- ),
- 501 => array(
- 'message' => "Access denied",
- 'description' => "Устройство привязано к пользователю другого провайдера"
- ),
- 504 => array(
- 'message' => "User already deactivated",
- 'description' => "Услуга была уже выключена ранее"
- ),
- 505 => array(
- 'message' => "User is attached to another operator",
- 'description' => "Пользователь привязан к другому провайдеру"
- ),
- 506 => array(
- 'message' => "Account is not active",
- 'description' => "Аккаунт пользователя не активен"
- )
- );
- /**
- * Last message array
- * You can get last message by type even when log has disabled
- * @var array
- */
- protected $_lastMessage = array(
- 0 => '', // empty
- 1 => '', // last error message
- 2 => '', // last warning message
- 3 => '' // last information message
- );
- /* --end error vars */
- /**
- * Assign last message
- * @param string $message message text
- * @param integer $type message type
- * @return boolean
- */
- protected function _setLastMessage($message, $type) {
- // verify arguments
- if (!is_string($message) || !is_numeric($type)) {
- return false;
- }
- // verify type in last message array
- if (!isset($this->_lastMessage[$type])) {
- return false;
- }
- // assign message by type
- $this->_lastMessage[$type] = $message;
- return true;
- }
- /**
- * Write to log file
- * @param string $message message text
- * @param integer $type type of message
- * @return boolean
- */
- protected function _toLog($message, $type = 1) {
- // verify arguments
- if (empty($message) || !is_string($message)) {
- return false;
- }
- // assign last message variable
- $this->_setLastMessage($message, $type);
- // verify log level
- if ($this->_logLevel == 0 || $type == 0 || !isset($this->_logType[$type])) {
- return false;
- }
- // compare $_logLevel and message type
- if ($type > $this->_logLevel) {
- return false;
- }
- // prepare message type
- $type = $this->_logType[$type] . ': ';
- // append write to file and get result
- $res = file_put_contents($this->_log, date('Y-m-d H:i:s') . ' ' . $type . $message . ";\n", FILE_APPEND);
- return (bool) $res;
- }
- /**
- * Method prepare default account data
- * @param array $params parametters array
- * accept parametters:
- * account OR email OR id OR ds_account
- * examples:
- * array('account' => 'test')
- * array('email' => 'test@test.com')
- * array('id' => 42)
- * array('ds_account' => 'test')
- *
- * @return array
- */
- protected function _prepareAccountDefaultData($params) {
- // init return array
- $args = array();
- // try find 'account'
- if (!empty($params['account'])) {
- $args = array(
- 'account' => $params['account']
- );
- } else {
- $this->_toLog('[' . __FUNCTION__ . '] - `account` not found in parametter array', self::INFO);
- }
- // try find 'email'
- if (!empty($params['email']) && filter_var($params['email'], FILTER_VALIDATE_EMAIL)) {
- $args = array(
- 'email' => $params['email']
- );
- } else {
- $this->_toLog('[' . __FUNCTION__ . '] - `email` not found in parametter array or `email` is not valid email', self::INFO);
- }
- // try find 'id'
- if (!empty($params['id']) && is_numeric($params['id'])) {
- $args = array(
- 'id' => $params['id']
- );
- } else {
- $this->_toLog('[' . __FUNCTION__ . '] - `id` not found in parametter array', self::INFO);
- }
- // try find 'ds_account'
- if (!empty($params['ds_account'])) {
- $args = array(
- 'ds_account' => $params['ds_account']
- );
- } else {
- $this->_toLog('[' . __FUNCTION__ . '] - `ds_account` not found in parametter array', self::INFO);
- }
- return $args;
- }
- /**
- * Method prepare purchase type
- * @param string $type
- * accept parametters:
- * subs_free_device — new contract - 24 months and equipment for 1 uah
- * subs_buy_device — new contract - buy equipment
- * subs_rent_device — new contract - rent equipment
- * subs_no_device — new contract - no equipment
- * subs_renew — restore the current contract
- * subs_negative_balance - money stop
- *
- * @return string|false
- */
- protected function _preparePurchaseType($type) {
- // init types array
- $typeArray = array('subs_free_device', 'subs_buy_device', 'subs_rent_device', 'subs_no_device', 'subs_renew', 'subs_negative_balance');
- // verify argument
- if (empty($type) || !in_array($type, $typeArray)) {
- $this->_toLog('[' . __FUNCTION__ . '] - purchase type is not correct');
- return false;
- }
- // return type
- return $type;
- }
- /**
- * Method prepare device type
- * @param string $type
- * accept parametters:
- * device_free — new contract - 24 months and equipment for 1 uah
- * device_buy — new contract - buy equipment
- * device_rent — new contract - rent equipment
- * device_change — service replace the current equipment
- *
- * @return string|false
- */
- protected function _prepareDeviceType($type) {
- // init types array
- $typeArray = array('device_free', 'device_buy', 'device_rent', 'device_change');
- // verify argument
- if (empty($type) || !in_array($type, $typeArray)) {
- $this->_toLog('[' . __FUNCTION__ . '] - device type is not correct');
- return false;
- }
- // return type
- return $type;
- }
- /**
- * Read result from ispAPI
- * @param string $result result string
- * @return boolean
- */
- protected function _readResult($result) {
- // verify result var
- if (empty($result) || !is_string($result)) {
- $this->_toLog('[' . __FUNCTION__ . '] - cannot read result; maybe empty or not string value: ' . var_export($result, true));
- return false;
- }
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - read result: ' . var_export($result, true), self::INFO);
- // decode from string
- $json = json_decode($result);
- // check json errors
- // commented due old PHP compatibility reasons
- //$error = json_last_error_msg();
- $error = print_r($json, true);
- if (!$json) {
- $this->_toLog('[' . __FUNCTION__ . '] - has json error: ' . $error);
- return false;
- }
- // all looks good
- $this->_result = $json;
- return true;
- }
- /**
- * Return ispAPI result
- * @return object|false
- */
- protected function _return() {
- // verify result
- if (empty($this->_result) || !is_object($this->_result) || !isset($this->_result->status)) {
- $this->_toLog('[' . __FUNCTION__ . '] - API result is bad type: ' . var_export($this->_result, true));
- return false;
- }
- // log warnings
- if (!empty($this->_result->warnings)) {
- $this->_toLog('[' . __FUNCTION__ . '] - API warnings: ' . print_r($this->_result->warnings, true));
- }
- // verify result status
- if ($this->_result->status !== 0) {
- // prepare error
- $error = '';
- if (!isset($this->_errors[$this->_result->status])) {
- if (isset($this->_result->message)) {
- $error = $this->_result->message;
- } else {
- $error = '[' . __FUNCTION__ . '] - API return false status';
- }
- } else {
- // prepare error string
- $error = 'Code #' . $this->_result->status;
- $error .= ' ' . $this->_errors[$this->_result->status]['message'];
- $error .= ' - ' . $this->_errors[$this->_result->status]['description'];
- }
- // log error
- $this->_toLog($error);
- // return false
- return false;
- }
- // all looks good
- else {
- // log action
- $this->_toLog('[' . __FUNCTION__ . '] - return API result', self::INFO);
- // return result
- return $this->_result;
- }
- }
- /**
- * Create ispAPI url
- * @param boolean $testMode testing mode flag, default - false
- * @return boolean
- */
- protected function _createUrl($testMode = false) {
- // verify argument
- if (!is_bool($testMode)) {
- $this->_toLog('[' . __FUNCTION__ . '] - $testMode not is boolean type; set to default `false`', self::WARNING);
- $testMode = false;
- }
- // assign url
- if ($testMode) {
- $this->_toLog('[' . __FUNCTION__ . '] - $testMode is `true`; set url to: ' . self::OTV_URL_DEV, self::INFO);
- $this->_url = self::OTV_URL_DEV;
- } else {
- $this->_toLog('[' . __FUNCTION__ . '] - $testMode is `false`; set url to: ' . self::OTV_URL, self::INFO);
- $this->_url = self::OTV_URL;
- }
- // add ispAPI link to url
- $this->_url .= self::OTV_URL_API;
- return true;
- }
- /**
- * Authenticate to ispAPI and assign hash
- * @return boolean
- */
- protected function _auth() {
- // verify login and pass
- if (empty($this->_login)) {
- $this->_toLog('[' . __FUNCTION__ . '] - login is empty');
- return false;
- }
- if (empty($this->_password)) {
- $this->_toLog('[' . __FUNCTION__ . '] - password is empty');
- return false;
- }
- // try to connect
- $res = $this->_sendToAPI(self::OTV_URL_AUTH, array(
- 'login' => $this->_login,
- 'password' => $this->_password
- ));
- // verify result and hash
- if (!$res || empty($this->_result->hash)) {
- $this->_toLog('[' . __FUNCTION__ . '] - false authenticate');
- return false;
- }
- // assign hash
- $this->_hash = $this->_result->hash;
- return true;
- }
- /**
- * Method send ispAPI action by CURL
- * @param string $method ispAPI method
- * @param array $args POST arguments
- * @return object|false
- */
- protected function _sendToAPI($method, $args) {
- // verify arguments
- if (!is_string($method)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$method` must to be string;');
- return false;
- }
- if (!is_array($args)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$args` is not array;');
- return false;
- }
- // add hash to arguments
- if ($method !== self::OTV_URL_AUTH) {
- $args['hash'] = $this->_hash;
- }
- // create curl link
- $curlLink = $this->_url . '/' . $method;
- // prepare data for log
- $logArgs = $args;
- // disable password - not show in logs!
- if (!empty($logArgs['password'])) {
- $logArgs['password'] = '*************';
- }
- // lof info
- $this->_toLog('[' . __FUNCTION__ . '] - send request to url: ' . $curlLink, self::INFO);
- $this->_toLog('[' . __FUNCTION__ . '] - send request data: ' . var_export($logArgs, true), self::INFO);
- // create and send curl
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $curlLink);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/html;charset=utf-8'));
- // send post if need
- if (!empty($args)) {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($args));
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // execute curl
- $response = curl_exec($ch);
- curl_close($ch);
- // read result
- $this->_readResult($response);
- // return formed result by function _return()
- return $this->_return();
- }
- /**
- * Constructor
- * @depends _createUrl, _auth
- * @param string $login client login
- * @param string $pass client password
- * @param boolean $testMode test mode flag
- * @param string $log path to log file
- */
- public function __construct($login, $pass, $testMode = false, $log = '', $logLevel = 1) {
- // assign log file
- $this->_log = $log;
- // assign log level
- $logLevel = (int) $logLevel;
- $this->_logLevel = ($logLevel < 0 || $logLevel > 3) ? 1 : $logLevel;
- // assign login and password
- $this->_login = $login;
- $this->_password = $pass;
- // create API url
- $this->_createUrl($testMode);
- // try authenticate
- $this->_auth();
- }
- /**
- * Return last message
- * @param integer $type message type, default - 1 mean Error
- * @return string|false
- */
- public function getLastMessage($type = 1) {
- // verify arguments
- if (!is_numeric($type) || !isset($this->_lastMessage[$type])) {
- return false;
- }
- // return message
- return $this->_lastMessage[$type];
- }
- /* ispAPI functions */
- /* users functions */
- /**
- * Verify user email
- * @param string $email user email
- * @return integer|false false - smth is wrong; 0 - not exist; 1 - exist
- */
- public function emailExists($email) {
- // verify argument
- if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$email` is not valid email address');
- return false;
- }
- // prepare arguments array
- $args = array(
- 'email' => $email
- );
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return result
- if (isset($res->data)) {
- return $res->data;
- } else {
- return false;
- }
- }
- /**
- * Return account object
- * @param string $account user account in provider base
- * @return mixed return object - user account or 0
- */
- public function accountExists($account) {
- // verify argument
- if (empty($account)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$account` is empty');
- return 0;
- }
- // prepare arguments array
- $args = array(
- 'account' => $account
- );
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return 0;
- }
- }
- /**
- * Add new user
- * @param string $email user email
- * @param string $account user account in provider base
- * @param array $addParams additional params
- * birth_date (YYYY-MM-DD or DD.MM.YYYY)
- * gender (M or F, default: M)
- * firstname (default: «Гость»/«Гостья»)
- * password (will generate automatic - if not set, must be longer 8 chars)
- * lastname
- * phone (example: 0501234567)
- * region
- * receive_news (value 1 or 0, default: 1)
- * send_registration_email (whether send the user a registration message, value 1 or 0, default: 1)
- * index (zip code or other identifier binding regional subscriber)
- *
- * @return string|false string - user ID; false - smth is wrong
- */
- public function addUser($email, $account, $addParams = array()) {
- // verify arguments
- if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$email` is not valid email address');
- return false;
- }
- // verify argument
- if (empty($account)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$account` is empty');
- return false;
- }
- // prepare arguments array
- $args = array(
- 'email' => $email,
- 'account' => $account,
- );
- // prepare birth date
- if (!empty($addParams['birth_date']) && strtotime($addParams['birth_date'])) {
- $args['birth_date'] = date('Y-m-d', strtotime($addParams['birth_date']));
- }
- // prepare gender
- if (!empty($addParams['gender']) && in_array($addParams['gender'], array('M', 'F'))) {
- $args['gender'] = $addParams['gender'];
- }
- // prepare firstname
- if (!empty($addParams['firstname'])) {
- $args['firstname'] = $addParams['firstname'];
- } else {
- $args['firstname'] = (isset($args['gender']) && $args['gender'] === 'F') ? 'Гостья' : 'Гость';
- }
- // prepare lastname
- if (!empty($addParams['lastname'])) {
- $args['lastname'] = $addParams['lastname'];
- }
- // prepare password
- if (!empty($addParams['password']) && strlen($addParams['password']) >= 8) {
- $args['password'] = $addParams['password'];
- } else {
- $this->_toLog('[' . __FUNCTION__ . '] - user password is empty or shorter than 8 chars; generate automatic', self::WARNING);
- }
- // prepare phone
- if (!empty($addParams['phone']) && is_string($addParams['phone']) &&
- preg_match('/^\d{10,}$/', $addParams['phone']) && $addParams['phone'][0] === '0'
- ) {
- $args['phone'] = $addParams['phone'];
- }
- // prepare region
- if (!empty($addParams['region'])) {
- $args['region'] = $addParams['region'];
- }
- // prepare receive_news
- if (!empty($addParams['receive_news']) && is_numeric($addParams['receive_news'])) {
- $args['receive_news'] = (int) (bool) $addParams['receive_news'];
- }
- // prepare send_registration_email
- if (!empty($addParams['send_registration_email']) && is_numeric($addParams['send_registration_email'])) {
- $args['send_registration_email'] = (int) (bool) $addParams['send_registration_email'];
- }
- // prepare index
- if (!empty($addParams['index'])) {
- $args['index'] = $addParams['index'];
- }
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- debarr($res);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return 0;
- }
- }
- /**
- * Return user list bound to provider
- * @param integer $offset offset
- * @param integer $limit limit
- * @return array
- */
- public function getUserList($offset = 0, $limit = 1000) {
- // verify arguments
- if (!is_numeric($offset) || $offset < 0) {
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - `$offset` is incorrect, set to 0', self::WARNING);
- $offset = 0;
- }
- if (!is_numeric($limit) || $limit <= 0 || $limit > 1000) {
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - `$limit` is incorrect, set to default 1000', self::WARNING);
- $limit = 1000;
- }
- // prepare arguments array
- $args = array(
- 'offset' => $offset,
- 'limit' => $limit
- );
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return array();
- }
- }
- /**
- * Assign provider account and bind user to account
- * @param string $email user email
- * @param string $account account
- * @return integer|false integer - status
- */
- public function changeAccount($email, $account) {
- // verify arguments
- if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$email` is not valid email address');
- return false;
- }
- // verify argument
- if (empty($account)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$account` is empty');
- return false;
- }
- // prepare arguments array
- $args = array(
- 'email' => $email,
- 'account' => $account,
- );
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->status)) {
- return $res->status;
- } else {
- return false;
- }
- }
- /**
- * Unbind user from provider by params
- * @param array $params
- * accept parametters:
- * account OR email OR id OR ds_account
- * examples:
- * array('account' => 'test')
- * array('email' => 'test@test.com')
- * array('id' => 42)
- * array('ds_account' => 'test')
- *
- * @return integer|false integer - status
- */
- public function deleteAccount($params) {
- // verify parametters
- if (empty($params)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$params` is empty');
- return false;
- }
- // prepare arguments array
- $args = $this->_prepareAccountDefaultData($params);
- // verify arguments
- if (empty($args)) {
- $this->_toLog('[' . __FUNCTION__ . '] - account parametter not found in `$params`');
- return false;
- }
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->status)) {
- return $res->status;
- } else {
- return false;
- }
- }
- /**
- * Change user email
- * @param string $email current user email
- * @param string $newEmail new user email
- * @return integer|false integer - status
- */
- public function changeEmail($email, $newEmail) {
- // verify arguments
- if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$email` is not valid email address');
- return false;
- }
- if (empty($newEmail) || !filter_var($newEmail, FILTER_VALIDATE_EMAIL)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$newEmail` is not valid email address');
- return false;
- }
- // prepare arguments array
- $args = array(
- 'email' => $email,
- 'new_email' => $newEmail
- );
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->status)) {
- return $res->status;
- } else {
- return false;
- }
- }
- /**
- * Return user information
- * @param array $params
- * accept parametters:
- * account OR email OR id OR ds_account
- * examples:
- * array('account' => 'test')
- * array('email' => 'test@test.com')
- * array('id' => 42)
- * array('ds_account' => 'test')
- *
- * @return object|false
- */
- public function getUserInfo($params) {
- // verify parametters
- if (empty($params)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$params` is empty');
- return false;
- }
- // prepare arguments array
- $args = $this->_prepareAccountDefaultData($params);
- // verify arguments
- if (empty($args)) {
- $this->_toLog('[' . __FUNCTION__ . '] - account parametter not found in `$params`');
- return false;
- }
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return false;
- }
- }
- /**
- * Return user information
- * @param array $params parametters array
- * accept parametters:
- *
- * one from following is required:
- * account OR email OR id OR ds_account
- * examples:
- * array('account' => 'test')
- * array('email' => 'test@test.com')
- * array('id' => 42)
- * array('ds_account' => 'test')
- *
- * additionals:
- * birth_date (YYYY-MM-DD or DD.MM.YYYY)
- * gender (M or F, default: M)
- * password (must be longer than 8 chars)
- * firstname
- * lastname
- * phone
- * region
- * index (zip code or other identifier binding regional subscriber)
- *
- * @return integer|false integer - status
- */
- public function changeUserInfo($params) {
- // verify parametters
- if (empty($params)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$params` is empty');
- return false;
- }
- // prepare arguments array
- // required parametters
- $args = $this->_prepareAccountDefaultData($params);
- // verify arguments
- if (empty($args)) {
- $this->_toLog('[' . __FUNCTION__ . '] - account parametter not found in `$params`');
- return false;
- }
- // additional parametters
- // prepare birth date
- if (!empty($addParams['birth_date']) && strtotime($addParams['birth_date'])) {
- $args['birth_date'] = date('Y-m-d', strtotime($addParams['birth_date']));
- }
- // prepare gender
- if (!empty($addParams['gender']) && in_array($addParams['gender'], array('M', 'F'))) {
- $args['gender'] = $addParams['gender'];
- }
- // prepare firstname
- if (!empty($addParams['firstname'])) {
- $args['firstname'] = $addParams['firstname'];
- } else {
- $args['firstname'] = (isset($args['gender']) && $args['gender'] === 'F') ? 'Гостья' : 'Гость';
- }
- // prepare lastname
- if (!empty($addParams['lastname'])) {
- $args['lastname'] = $addParams['lastname'];
- }
- // prepare password
- if (!empty($addParams['password']) && strlen($addParams['password']) >= 8) {
- $args['password'] = $addParams['password'];
- }
- // prepare phone
- if (!empty($addParams['phone']) && is_string($addParams['phone']) &&
- preg_match('/^\d{10,}$/', $addParams['phone']) && $addParams['phone']{0} === '0'
- ) {
- $args['phone'] = $addParams['phone'];
- }
- // prepare region
- if (!empty($addParams['region'])) {
- $args['region'] = $addParams['region'];
- }
- // prepare index
- if (!empty($addParams['index'])) {
- $args['index'] = $addParams['index'];
- }
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return false;
- }
- }
- /**
- * Reset parent control
- * @param array $params
- * accept parametters:
- * account OR email OR id OR ds_account
- * examples:
- * array('account' => 'test')
- * array('email' => 'test@test.com')
- * array('id' => 42)
- * array('ds_account' => 'test')
- *
- * @return integer|false false or integer - status
- */
- public function resetParentControl($params) {
- // verify parametters
- if (empty($params)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$params` is empty');
- return false;
- }
- // prepare arguments array
- $args = $this->_prepareAccountDefaultData($params);
- // verify arguments
- if (empty($args)) {
- $this->_toLog('[' . __FUNCTION__ . '] - account parametter not found in `$params`');
- return false;
- }
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->status)) {
- return $res->status;
- } else {
- return false;
- }
- }
- /* --end users functions */
- /* purchases functions */
- /**
- * Enable bundle
- * @param array $params
- * accept parametters:
- * account OR email OR id OR ds_account
- * examples:
- * array('account' => 'test')
- * array('email' => 'test@test.com')
- * array('id' => 42)
- * array('ds_account' => 'test')
- *
- * @param string $subId purchase identificator
- * @param string $type
- * accept parametters:
- * subs_free_device — new contract - 24 months and equipment for 1 uah
- * subs_buy_device — new contract - - buy equipment
- * subs_rent_device — new contract - rent equipment
- * subs_no_device — new contract - no equipment
- * subs_renew — restore the current contract
- *
- * @return integer|false
- */
- public function enableBundle($params, $subId, $type) {
- // verify arguments
- if (empty($params)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$params` is empty');
- return false;
- }
- if (empty($subId)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$subId` is empty');
- return false;
- }
- if (empty($type)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$type` is empty');
- return false;
- }
- // prepare arguments array
- $args = $this->_prepareAccountDefaultData($params);
- // verify arguments
- if (empty($args)) {
- $this->_toLog('[' . __FUNCTION__ . '] - account parametter not found in `$params`');
- return false;
- }
- // assign subId
- $args['sub_id'] = $subId;
- // prepare type
- $type = $this->_preparePurchaseType($type);
- // verify type
- if (!$type) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$type` is not correct');
- return false;
- }
- // assign type
- $args['type'] = $type;
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return false;
- }
- }
- /**
- * Disable bundle
- * @param array $params
- * accept parametters:
- * account OR email OR id OR ds_account
- * examples:
- * array('account' => 'test')
- * array('email' => 'test@test.com')
- * array('id' => 42)
- * array('ds_account' => 'test')
- *
- * @param string $subId description
- * @param string $type
- * accept parametters:
- * subs_free_device — new contract - 24 months and equipment for 1 uah
- * subs_buy_device — new contract - - buy equipment
- * subs_rent_device — new contract - rent equipment
- * subs_no_device — new contract - no equipment
- * subs_renew — restore the current contract
- *
- * @return integer|false
- */
- public function disableBundle($params, $subId, $type) {
- // verify parametters
- if (empty($params)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$params` is empty;');
- return false;
- }
- if (empty($subId)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$subId` is empty');
- return false;
- }
- if (empty($type)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$type` is empty');
- return false;
- }
- // prepare arguments array
- $args = $this->_prepareAccountDefaultData($params);
- // verify arguments
- if (empty($args)) {
- $this->_toLog('[' . __FUNCTION__ . '] - account parametter not found in `$params`');
- return false;
- }
- // assign subId
- $args['sub_id'] = $subId;
- // prepare type
- $type = $this->_preparePurchaseType($type);
- // verify type
- if (!$type) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$type` is not correct');
- return false;
- }
- // assign type
- $args['type'] = $type;
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return false;
- }
- }
- /**
- * Check user bundle
- * @param array $params
- * accept parametters:
- * account OR email OR id OR ds_account
- * examples:
- * array('account' => 'test')
- * array('email' => 'test@test.com')
- * array('id' => 42)
- * array('ds_account' => 'test')
- *
- * @param string $subId description
- *
- * @return integer|false
- */
- public function checkBundle($params, $subId) {
- // verify parametters
- if (empty($params)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$params` is empty');
- return false;
- }
- if (empty($subId)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$subId` is empty');
- return false;
- }
- // prepare arguments array
- $args = $this->_prepareAccountDefaultData($params);
- // verify arguments
- if (empty($args)) {
- $this->_toLog('[' . __FUNCTION__ . '] - account parametter not found in `$params`');
- return false;
- }
- // assign subId
- $args['sub_id'] = $subId;
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return false;
- }
- }
- /**
- * Change use bundle-subscription
- * @param array $params account params array
- * accept parametters:
- * account OR email OR id OR ds_account
- * examples:
- * array('account' => 'test')
- * array('email' => 'test@test.com')
- * array('id' => 42)
- * array('ds_account' => 'test')
- *
- * @param string $oldSubId old subscription
- * @param string $newSubId new subscription
- * @return integer|false false or integer - status
- */
- public function changeBundle($params, $oldSubId, $newSubId) {
- // verify parametters
- if (empty($params)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$params` is empty');
- return false;
- }
- if (empty($oldSubId)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$oldSubId` is empty');
- return false;
- }
- if (empty($newSubId)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$newSubId` is empty');
- return false;
- }
- // prepare arguments array
- $args = $this->_prepareAccountDefaultData($params);
- // verify arguments
- if (empty($args)) {
- $this->_toLog('[' . __FUNCTION__ . '] - account parametter not found in `$params`');
- return false;
- }
- // assign subscriptions
- $args['old_sub_id'] = $oldSubId;
- $args['new_sub_id'] = $newSubId;
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->status)) {
- return $res->status;
- } else {
- return false;
- }
- }
- /**
- * Get active provider's purchases
- * @param string $startDate start date of reporting period
- * @param integer $page page number
- * @return object|false
- */
- public function getAllPurchases($startDate, $page = 1) {
- // verify arguments
- if (empty($startDate) || !strtotime($startDate)) {
- $this->_toLog('[' . __FUNCTION__ . '] - parametter `$startDate` is not valida date type');
- return false;
- }
- if (!is_numeric($page) || $page <= 0) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$page` is not numeric os less than 0');
- return false;
- }
- // prepare arguments
- $args = array(
- 'start_date' => $startDate,
- 'page' => $page
- );
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return false;
- }
- }
- /* --end purchases functions */
- /* devices functions */
- /**
- * Add device and bind to user
- * $account is required
- * $serialNumber or $mac is required, but advisable required TWO
- * $binding_code is required for providers that work with purchases for access to additional devices
- * $addParams may assigns 'device_type', 'device_model', 'type'
- *
- * @param string $account user account
- * @param string $serialNumber device serial number
- * @param string $mac mac address
- * @param string $binding_code code for binding
- * @param array $addParams additional params
- * @return integer|false false or integer - status
- */
- public function addDevice($account, $serialNumber = null, $mac = null, $binding_code = null, $addParams = array()) {
- // verify arguments
- if (empty($account)) {
- $this->_toLog('[' . __FUNCTION__ . '] - `$account` not set');
- return false;
- }
- if (empty($serialNumber) && empty($mac)) {
- $this->_toLog('[' . __FUNCTION__ . '] - must set `$serialNumber` or `$mac` or both');
- return false;
- }
- // prepare arguments
- $args = array(
- 'account' => $account
- );
- if (!empty($serialNumber)) {
- $args['serial_number'] = $serialNumber; // assign serial number
- }
- if (!empty($mac)) {
- $args['mac'] = $mac; // assign mac
- }
- if (!empty($binding_code)) {
- $args['binding_code'] = $binding_code; // assign binding code
- }
- // assign additional params
- if (!empty($addParams['device_type'])) {
- $args['device_type'] = $addParams['device_type']; // assign device type
- }
- if (!empty($addParams['device_model'])) {
- $args['device_model'] = $addParams['device_model']; // assign device model
- }
- // assign device type
- if (!empty($addParams['type'])) {
- $args['type'] = $this->_prepareDeviceType($addParams['type']);
- }
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->status)) {
- return $res->status;
- } else {
- return false;
- }
- }
- /**
- * Unbind device from user
- * $serialNumber or $mac is required, but advisable required TWO
- * $account is NOT required
- * $type may assigns:
- * device_break_contract - end of contract
- * device_change - equipment problem
- *
- * @param string $serialNumber device serial number
- * @param string $mac device mac address
- * @param string $account user account
- * @param string $type device type
- * @return mixed false or result
- */
- public function delDevice($serialNumber = null, $mac = null, $account = null, $type = null) {
- // verify arguments
- if (empty($serialNumber) && empty($mac)) {
- $this->_toLog('[' . __FUNCTION__ . '] - must set `$serialNumber` or `$mac` or both');
- return false;
- }
- // init arguments array
- $args = array();
- if (!empty($serialNumber)) {
- $args['serial_number'] = $serialNumber; // assign serial number
- }
- if (!empty($mac)) {
- $args['mac'] = $mac; // assign mac
- }
- if (!empty($account)) {
- $args['account'] = $account; // assign account
- }
- if (!empty($type) && in_array($type, array('device_break_contract', 'device_change'))) {
- $args['type'] = $type;
- }
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->status)) {
- return $res->status;
- } else {
- return false;
- }
- }
- /**
- * Check device
- * $serialNumber or $mac is required, but advisable required TWO
- *
- * @param string $serialNumber device serial number
- * @param string $mac device mac address
- * @return mixed false or 0 or object
- */
- public function deviceExists($serialNumber = null, $mac = null) {
- // verify arguments
- if (empty($serialNumber) && empty($mac)) {
- $this->_toLog('[' . __FUNCTION__ . '] - must set `$serialNumber` or `$mac` or both');
- return false;
- }
- // init arguments array
- $args = array();
- if (!empty($serialNumber)) {
- $args['serial_number'] = $serialNumber; // assign serial number
- }
- if (!empty($mac)) {
- $args['mac'] = $mac; // assign mac
- }
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return false;
- }
- }
- /**
- * Return device list
- * @param string $account user account
- * @param string $email user email
- * @param integer $offset offset parametter
- * @param integer $limit limit parametter no more than 1000
- * @return array|false
- */
- public function getDeviceList($account = null, $email = null, $offset = 0, $limit = 1000) {
- // verify arguments
- if (!is_numeric($offset) || $offset < 0) {
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - `$offset` is incorrect, set to 0', self::WARNING);
- $offset = 0;
- }
- if (!is_numeric($limit) || $limit <= 0 || $limit > 1000) {
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - `$limit` is incorrect, set to default 1000', self::WARNING);
- $limit = 1000;
- }
- // init arguments array
- $args = array(
- 'offset' => $offset,
- 'limit' => $limit
- );
- if (!empty($account)) {
- $args['account'] = $account; // assign account
- }
- if (empty($args['account']) && !empty($email)) {
- $args['email'] = $email; // assign email
- }
- // log info
- $this->_toLog('[' . __FUNCTION__ . '] - send params: ' . var_export($args, true), self::INFO);
- // run request to API and get result
- $res = $this->_sendToAPI(__FUNCTION__, $args);
- // verify and return
- if (isset($res->data)) {
- return $res->data;
- } else {
- return false;
- }
- }
- /* --end devices functions */
- /* --end API functions */
- }
- // end class
|