asterisk.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /**
  3. * Ubilling remote API for Asterisk and other CRM
  4. * -----------------------------
  5. *
  6. * Format: /?module=remoteapi&key=[ubserial]&action=[action]&number=[+380XXXXXXXXX]&param=[parameter]
  7. *
  8. * Available parameter: login, swstatus, userstatus, setcredit, paycardpay,
  9. * getuserdatabylogin, getuserdatabymobile, getcontractsbymobile, addusermobile
  10. *
  11. * With "userstatus" param you may use pretty self explanationary "ignorecache" and "getmoney" params as well
  12. * With "setcredit" param you'll need to pass "login", "money" and "expiredays" params as well
  13. * With "paycardpay" param you'll need to pass "login", "paycardnum", "paycardcashtype" param as well
  14. * With "getuserdatabylogin" param you may pass "userpass" param as well to enable user + password verification
  15. * With "addusermobile" param you'll need to pass "login" param also. Optional "maxmobilesamnt" param can be passed to determine
  16. * the max mobiles count threshold per user.
  17. * "getuserdatabymobile" and "getcontractsbymobile" need no additional parameters except the mobile passed in "number" param
  18. * With "getvservicescount" param you may use "number" or "login" params to search user with one of those.
  19. * Returns number of virtual services assigned to a certain user.
  20. * With "getonlinedaysleft" param you may use "number" or "login" params to search user with one of those.
  21. * You may use "includevsrvs" param as well to involve virtual services cost into "online days left" calculations.
  22. * Returns number of "online days left" for a certain user.
  23. * With "getuserspends" param you may use "number" or "login" params to search user with one of those.
  24. * You may use "includevsrvs" param as well to get all virtual services in addition to main tariff.
  25. * Returns info about user's tariff and it's cost and, optionally, similar info about user's virtual services.
  26. * Format: array(TarrifName => array('price' => TariffCost, 'daysperiod' => TariffChargePeriod),
  27. * Vservice1 => array('price' => Vservice1Cost, 'daysperiod' => Vservice1ChargePeriod),
  28. * Vservice2 => array('price' => Vservice2Cost, 'daysperiod' => Vservice2ChargePeriod),
  29. * VserviceN => array('price' => VserviceNCost, 'daysperiod' => VserviceChargePeriod)
  30. * )
  31. * Tip: "TarrifName => TariffCost" - is always the first element
  32. */
  33. if (ubRouting::get('action') == 'asterisk') {
  34. if ($alterconf['ASTERISK_ENABLED']) {
  35. if (ubRouting::checkGet('number') or ubRouting::checkGet('login')) {
  36. if (ubRouting::checkGet('param')) {
  37. $ignoreCache = ubRouting::checkGet('ignorecache');
  38. $getMoney = ubRouting::checkGet('getmoney');
  39. $addMobile = ubRouting::checkGet('addmobile');
  40. $includeVservices = (ubRouting::checkGet('includevsrvs') ? true : $ubillingConfig->getAlterParam('FUNDSFLOW_CONSIDER_VSERVICES'));
  41. $maxMobilesAmount = (ubRouting::checkGet('maxmobilesamnt')) ? ubRouting::get('maxmobilesamnt') : 0;
  42. $userLogin = (ubRouting::checkGet('login')) ? ubRouting::get('login') : '';
  43. $userPasswd = (ubRouting::checkGet('userpass')) ? ubRouting::get('userpass') : '';
  44. $creditMoney = (ubRouting::checkGet('money')) ? ubRouting::get('money') : 0.00;
  45. $creditExpireDays = (ubRouting::checkGet('expiredays')) ? ubRouting::get('expiredays') : 0;
  46. $payCardNum = (ubRouting::checkGet('paycardnum')) ? ubRouting::get('paycardnum') : '';
  47. $payCardCashType = (ubRouting::checkGet('paycardcashtype')) ? ubRouting::get('paycardcashtype') : 1;
  48. $number = trim(ubRouting::get('number'));
  49. $apiParam = ubRouting::get('param');
  50. $userdata = (empty($userLogin)) ? array() : zb_ProfileGetStgData($userLogin);
  51. $asterisk = new Asterisk();
  52. // We do not need this data in the modules: callshist, ForWhomTheBellTolls
  53. if ($apiParam == 'swstatus') {
  54. $result = $asterisk->AsteriskGetInfoApi($number, 'swstatus');
  55. die($result);
  56. } else {
  57. global $billing;
  58. $askNum = new PBXNum();
  59. $askNum->setNumber($number);
  60. switch ($apiParam) {
  61. case 'setcredit':
  62. if (!empty($userdata)) {
  63. if (isset($userdata['Cash']) and $userdata['Cash'] < 0) {
  64. $creditCheckEnabled = (isset($alterconf['ASTERISK_SC_CHECK_ENABLED']) and $alterconf['ASTERISK_SC_CHECK_ENABLED']);
  65. if ($creditCheckEnabled) {
  66. $asterSCAllowedTariffs = '';
  67. $userTariff = $userdata['Tariff'];
  68. if (!zb_CreditLogCheckHack($userLogin)) {
  69. log_register('ASTERISK CREDIT GET TRY (' . $userLogin . '): NOT PAYED PREVIOUSLY');
  70. die('ASTERISK CREDIT NOT AVAILABLE: NOT PAYED PREVIOUSLY');
  71. }
  72. if (!zb_CreditLogCheckMonth($userLogin)) {
  73. log_register('ASTERISK CREDIT GET TRY (' . $userLogin . '): ALREADY TOOK');
  74. die('ASTERISK CREDIT NOT AVAILABLE: ALREADY TOOK');
  75. }
  76. if (isset($alterconf['ASTERISK_SC_TARIFFSALLOWED']) and !empty($alterconf['ASTERISK_SC_TARIFFSALLOWED'])) {
  77. $asterSCAllowedTariffs = explode(',', $ubillingConfig->getAlterParam('ASTERISK_SC_TARIFFSALLOWED'));
  78. $asterSCAllowedTariffs = array_flip($asterSCAllowedTariffs);
  79. if (!zb_CreditCheckAllowed($asterSCAllowedTariffs, $userTariff)) {
  80. log_register('ASTERISK CREDIT GET TRY (' . $userLogin . '): NOT ALLOWED FOR TARIFF ' . $userTariff);
  81. die('ASTERISK CREDIT NOT AVAILABLE: NOT ALLOWED FOR TARIFF');
  82. }
  83. }
  84. }
  85. if ($userdata['Cash'] > -$creditMoney) {
  86. if (curdate() < date("Y-m-d", $userdata['CreditExpire'])) {
  87. log_register('ASTERISK CREDIT GET TRY (' . $userLogin . '): CREDIT IS CURRENTLY ACTIVE');
  88. die('ASTERISK CREDIT NOT AVAILABLE: CREDIT IS CURRENTLY ACTIVE');
  89. } else {
  90. //set credit
  91. $billing->setcredit($userLogin, $creditMoney);
  92. log_register('ASTERISK CHANGE Credit (' . $userLogin . ') ON ' . $creditMoney);
  93. //set credit expire date
  94. $creditExpire = date('Y-m-d', strtotime("+" . $creditExpireDays . " days"));
  95. $billing->setcreditexpire($userLogin, $creditExpire);
  96. if ($creditCheckEnabled) {
  97. zb_CreditLogPush($userLogin);
  98. }
  99. log_register('ASTERISK CHANGE CreditExpire (' . $userLogin . ') ON ' . $creditExpire);
  100. die('ASTERISK CREDIT SET SUCCESSFULY');
  101. }
  102. } else {
  103. log_register('ASTERISK CREDIT TRY (' . $userLogin . '): BALANCE LOWER THAN CREDIT LIMIT');
  104. die('ASTERISK CREDIT NOT AVAILABLE: BALANCE LOWER THAN CREDIT LIMIT');
  105. }
  106. } else {
  107. log_register('ASTERISK CREDIT NOT SET (' . $userLogin . '): CASH > 0 OR NOT SET');
  108. die('ASTERISK CREDIT NOT SET: CASH > 0 OR NOT SET');
  109. }
  110. } else {
  111. log_register('ASTERISK CREDIT NOT SET: EMPTY USERDATA');
  112. die('ASTERISK CREDIT NOT SET: EMPTY USERDATA');
  113. }
  114. case 'paycardpay':
  115. if (empty($payCardNum)) {
  116. log_register('ASTERISK PAYCARD NUMBER IS EMPTY');
  117. die('ASTERISK PAYCARD NUMBER IS EMPTY');
  118. }
  119. if (!empty($userdata)) {
  120. $user_ip = $userdata['IP'];
  121. $ctime = curdatetime();
  122. $payCardNum = vf($payCardNum);
  123. $query = "SELECT `id` from `cardbank` WHERE `serial`='" . $payCardNum . "' AND `active`='1' AND `used`='0' AND `usedlogin` = ''";
  124. $cardcheck = simple_query($query);
  125. if (empty($cardcheck)) {
  126. $query = "INSERT INTO `cardbrute` (`id` , `serial` , `date` , `login` , `ip` )
  127. VALUES (NULL , '" . $payCardNum . "', '" . $ctime . "', '" . $userLogin . "', '" . $user_ip . "');";
  128. nr_query($query);
  129. log_register('ASTERISK PAYCARD NOT EXISTS');
  130. die('ASTERISK PAYCARD NOT EXISTS');
  131. } else {
  132. // mark paycard as used
  133. $query = "SELECT * from `cardbank` WHERE `serial`='" . $payCardNum . "'";
  134. $carddata = simple_query($query);
  135. $cardcash = $carddata['cash'];
  136. $carduse_q = "UPDATE `cardbank` SET
  137. `usedlogin` = '" . $userLogin . "',
  138. `usedip` = '" . $user_ip . "',
  139. `usedate`= '" . $ctime . "',
  140. `used`='1'
  141. WHERE `serial` ='" . $payCardNum . "';
  142. ";
  143. nr_query($carduse_q);
  144. // add some cash to user balance
  145. billing_addcash($userLogin, $cardcash);
  146. // write card payment to payments log
  147. $cashtypeid = vf($payCardCashType);
  148. $userdata = zb_ProfileGetStgData($userLogin);
  149. $balance = $userdata['Cash'];
  150. $note = mysql_real_escape_string("CARD:" . $payCardNum);
  151. $query = "INSERT INTO `payments` (`id` , `login` , `date` , `admin` , `balance` , `summ` , `cashtypeid` , `note` )
  152. VALUES (NULL , '" . $userLogin . "', '" . $ctime . "', 'external', '" . $balance . "', '" . $cardcash . "', '" . $cashtypeid . "', '" . $note . "'); ";
  153. nr_query($query);
  154. log_register('ASTERISK PAYCARD PAYMENT SUCCESSFUL');
  155. die('ASTERISK PAYCARD PAYMENT SUCCESSFUL');
  156. }
  157. } else {
  158. log_register('ASTERISK PAYCARD PAYMENT UNSUCCESSFUL: EMPTY USERDATA');
  159. die('ASTERISK PAYCARD PAYMENT UNSUCCESSFUL: EMPTY USERDATA');
  160. }
  161. case 'setpause':
  162. if ($userdata['Passive']) {
  163. log_register('ASTERISK SET PAUSE UNSUCCESSFUL: PAUSE IS CURRENTLY ACTIVE');
  164. die('ASTERISK SET PAUSE UNSUCCESSFUL: PAUSE IS CURRENTLY ACTIVE');
  165. } else {
  166. if (isset($alterconf['FREEZE_DAYS_CHARGE_ENABLED']) and $alterconf['FREEZE_DAYS_CHARGE_ENABLED']) {
  167. $frozenDataQuery = "SELECT * FROM `frozen_charge_days` WHERE `login` = '" . $userLogin . "';";
  168. $frozenData = simple_queryall($frozenDataQuery);
  169. if (!empty($frozenData)) {
  170. $frzDaysAmount = $frozenData[0]['freeze_days_amount'];
  171. $frzDaysUsed = $frozenData[0]['freeze_days_used'];
  172. if ($frzDaysUsed >= $frzDaysAmount) {
  173. log_register('ASTERISK SET PAUSE UNSUCCESSFUL: NO AVAILABLE FREEZE DAYS LEFT');
  174. die('ASTERISK SET PAUSE UNSUCCESSFUL: NO AVAILABLE FREEZE DAYS LEFT');
  175. }
  176. }
  177. }
  178. $billing->setpassive($userLogin, 1);
  179. log_register('ASTERISK SET PAUSE SUCCESSFUL FOR ' . $userLogin);
  180. die('ASTERISK SET PAUSE SUCCESSFUL FOR ' . $userLogin);
  181. }
  182. case 'setunpause':
  183. if ($userdata['Passive']) {
  184. $billing->setpassive($userLogin, 0);
  185. log_register('ASTERISK UNPAUSE SUCCESSFUL FOR ' . $userLogin . ' FROM MOBILE: ' . $number);
  186. die('ASTERISK UNPAUSE SUCCESSFUL FOR ' . $userLogin . ' FROM MOBILE: ' . $number);
  187. } else {
  188. log_register('ASTERISK UNPAUSE UNSUCCESSFUL FOR ' . $userLogin . ' FROM MOBILE: ' . $number . ': PAUSE IS NOT ACTIVE');
  189. die('ASTERISK UNPAUSE UNSUCCESSFUL FOR ' . $userLogin . ' FROM MOBILE: ' . $number . ': PAUSE IS NOT ACTIVE');
  190. }
  191. case 'userstatus':
  192. $askNum->renderReply(false, $ignoreCache, $getMoney);
  193. // no break or die() needed - previous line will call die() itself
  194. case 'getuserdatabylogin':
  195. $result = $asterisk->getUserData($userLogin, $userPasswd, true);
  196. die($result);
  197. case 'getuserdatabymobile':
  198. $logins = $asterisk->getLoginsByMobile($number, false);
  199. $result = $asterisk->getUserData($logins, '', true);
  200. die($result);
  201. case 'getcontractsbymobile':
  202. $result = $asterisk->getContractsByMobile($number);
  203. die($result);
  204. case 'addusermobile':
  205. $result = $asterisk->addUserMobile($userLogin, $number, $maxMobilesAmount);
  206. die($result);
  207. case 'getonlinedaysleft':
  208. case 'getvservicescount':
  209. case 'getuserspends':
  210. case 'getcontragentdata':
  211. if (empty($userLogin) and !empty($number)) {
  212. $logins = $asterisk->getLoginsByMobile($number, false);
  213. if (!empty($logins)) {
  214. $userLogin = $logins[0];
  215. }
  216. }
  217. if ($apiParam == 'getonlinedaysleft') {
  218. $ff = new FundsFlow();
  219. $ff->runDataLoders();
  220. $onlineDaysLeft = $ff->getOnlineLeftCountFast($userLogin, $includeVservices);
  221. die("$onlineDaysLeft");
  222. }
  223. if ($apiParam == 'getcontragentdata') {
  224. $contragent = zb_AgentAssignedGetData($userLogin);
  225. die(json_encode($contragent));
  226. }
  227. $userVsrvs = zb_VservicesGetUsersAll($userLogin, true, true);
  228. $userVsrvs = empty($userVsrvs[$userLogin]) ? array() : $userVsrvs[$userLogin];
  229. if ($apiParam == 'getvservicescount') {
  230. $userVsrvsCnt = (empty($userVsrvs) ? 0 : count($userVsrvs));
  231. die("$userVsrvsCnt");
  232. }
  233. $userSpends = array();
  234. $userData = $asterisk->getUserData($userLogin, '', false, false);
  235. $userData = empty($userData[$userLogin]) ? array() : $userData[$userLogin];
  236. if (!empty($userData)) {
  237. $userSpends[$userData['Tariff']] = array('price' => $userData['Fee'], 'daysperiod' => $userData['period']);
  238. }
  239. if (!empty($userVsrvs)) {
  240. foreach ($userVsrvs as $eachID => $eachSrv) {
  241. $vsrvName = $eachSrv['vsrvname'];
  242. $vsrvTmpArr = array('price' => $eachSrv['price'], 'daysperiod' => $eachSrv['daysperiod']);
  243. $userSpends[$vsrvName] = $vsrvTmpArr;
  244. }
  245. }
  246. die(json_encode($userSpends));
  247. default:
  248. $askNum->renderReply(true, $ignoreCache, $getMoney);
  249. $result = $asterisk->AsteriskGetInfoApi($number, $_GET['param']);
  250. die($result);
  251. }
  252. }
  253. } else {
  254. die('ERROR: NOT HAVE PARAMETR');
  255. }
  256. } else {
  257. die('ERROR: NOT HAVE NUMBER');
  258. }
  259. } else {
  260. die('ERROR: ASTERISK DISABLED');
  261. }
  262. }