api.pbxnum.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Universal PBX incoming calls processing class
  4. */
  5. class PBXNum {
  6. /**
  7. * Telepathy object placeholder
  8. *
  9. * @var object
  10. */
  11. protected $telepathy = '';
  12. /**
  13. * Mobile number placeholder
  14. *
  15. * @var string
  16. */
  17. protected $number = '';
  18. /**
  19. * System cache object placeholder
  20. *
  21. * @var object
  22. */
  23. protected $cache = '';
  24. /**
  25. * Incoming calls database abstraction layer
  26. *
  27. * @var object
  28. */
  29. protected $callsDb = '';
  30. /**
  31. * Userdata cahe key name
  32. */
  33. const CACHE_KEY = 'PBXUSERDATA';
  34. /**
  35. * Userdata caching time in seconds
  36. */
  37. const CACHE_TIME = 3600;
  38. /**
  39. * Log path
  40. */
  41. const LOG_PATH = 'content/documents/incallsnum.log';
  42. /**
  43. * Default incoming calls logging table
  44. */
  45. const LOG_TABLE = 'callshist';
  46. /**
  47. * Creates new PBXNum instance
  48. *
  49. * @return void
  50. */
  51. public function __construct() {
  52. $this->initTelepathy();
  53. $this->initCache();
  54. $this->initDb();
  55. }
  56. /**
  57. * Sets current mobile number
  58. *
  59. * @param string $number
  60. *
  61. * @return void
  62. */
  63. public function setNumber($number) {
  64. $this->number = $number;
  65. }
  66. /**
  67. * Inits telepathy object instance
  68. *
  69. * @return void
  70. */
  71. protected function initTelepathy() {
  72. $this->telepathy = new Telepathy(false, true, false, true, false);
  73. $this->telepathy->usePhones();
  74. }
  75. /**
  76. * Inits system cache object instance for further usage
  77. *
  78. * @return void
  79. */
  80. protected function initCache() {
  81. $this->cache = new UbillingCache();
  82. }
  83. /**
  84. * Inits incoming calls database abstraction layer
  85. *
  86. * @return void
  87. */
  88. protected function initDb() {
  89. $this->callsDb = new NyanORM(self::LOG_TABLE);
  90. }
  91. /**
  92. * Saves incoming call into database
  93. *
  94. * @return void
  95. */
  96. protected function saveCallsHist($date, $number, $login = '') {
  97. $login = ubRouting::filters($login, 'mres');
  98. $number = ubRouting::filters($number, 'mres');
  99. $number = trim($number);
  100. $login = trim($login);
  101. $this->callsDb->data('date', $date);
  102. $this->callsDb->data('number', $number);
  103. $this->callsDb->data('login', $login);
  104. $this->callsDb->create();
  105. }
  106. /**
  107. * Saves some data to log
  108. *
  109. * @param int $reply
  110. * @param string $login
  111. *
  112. * @return void
  113. */
  114. protected function log($reply, $login) {
  115. $curdateTime = curdatetime();
  116. $logData = $curdateTime . ' NUMBER: ' . $this->number . ' REPLY: ' . $reply . ' LOGIN: ' . $login . PHP_EOL;
  117. file_put_contents(self::LOG_PATH, $logData, FILE_APPEND);
  118. $this->saveCallsHist($curdateTime, $this->number, $login);
  119. }
  120. /**
  121. * Returns some state int for some user if he is detected by mobile phone
  122. * Can return cash balance also. In that case a serialized and base64 encoded array
  123. * with user acc state and acc cash balance is returned
  124. *
  125. * @param $ignoreCache
  126. * @param $getMoney
  127. *
  128. * 0 - user not found
  129. * 1 - user found and have positive balance
  130. * 2 - user found and have negative balance
  131. * 3 - user found and account is frozen or something like that
  132. *
  133. * @return mixed
  134. */
  135. protected function getReply($ignoreCache = false, $getMoney = false) {
  136. $detectedLogin = $this->telepathy->getByPhone($this->number, true, true);
  137. $askReply = '0';
  138. $askReplyArr = array();
  139. $nonEncodedReply = '0';
  140. if (!empty($detectedLogin)) {
  141. $userData = $this->cache->get(self::CACHE_KEY, self::CACHE_TIME);
  142. if (empty($userData) or $ignoreCache) {
  143. $userData = array();
  144. $userDataRaw = simple_queryall("SELECT `login`,`Cash`,`Credit`,`Passive`,`Down`,`AlwaysOnline`,`Fee` from `users` LEFT JOIN (SELECT `name`,`Fee` FROM `tariffs`) as T on (`users`.`Tariff`=`T`.`name`)");
  145. if (!empty($userDataRaw)) {
  146. foreach ($userDataRaw as $io => $each) {
  147. $userData[$each['login']] = $each;
  148. }
  149. }
  150. $this->cache->set(self::CACHE_KEY, $userData, self::CACHE_TIME);
  151. }
  152. if (isset($userData[$detectedLogin])) {
  153. $userData = $userData[$detectedLogin];
  154. if ($userData['Cash'] >= '-' . $userData['Credit']) {
  155. $askReply = '1';
  156. } else {
  157. $askReply = '2';
  158. }
  159. if (($userData['Passive'] == 1) OR ( $userData['Down'] == 1) OR ( $userData['AlwaysOnline'] == 0)) {
  160. $askReply = '3';
  161. }
  162. $nonEncodedReply = $askReply;
  163. if ($getMoney) {
  164. $askReplyArr[] = $askReply;
  165. $askReplyArr[] = round($userData['Cash'], 2);
  166. $askReplyArr[] = round($userData['Fee'], 2);
  167. $askReply = base64_encode(serialize($askReplyArr));
  168. }
  169. }
  170. }
  171. $this->log($nonEncodedReply, $detectedLogin);
  172. return ($askReply);
  173. }
  174. /**
  175. * Returns parsed incoming calls log.
  176. *
  177. * Log format example:
  178. * 2023-01-18 11:42:42 NUMBER: 380931234567 REPLY: 0 LOGIN:
  179. * 2023-01-18 11:43:51 NUMBER: 380937654321 REPLY: 1 LOGIN: someuserlogin
  180. *
  181. * @return array
  182. */
  183. public function parseLog() {
  184. $result = array();
  185. if (file_exists(self::LOG_PATH)) {
  186. $rawData = file_get_contents(self::LOG_PATH);
  187. $rawData = explodeRows($rawData);
  188. $count = 0;
  189. if (!empty($rawData)) {
  190. foreach ($rawData as $io => $line) {
  191. if (!empty($line)) {
  192. $line = explode(' ', $line);
  193. $result[$count]['date'] = $line[0];
  194. $result[$count]['time'] = $line[1];
  195. $result[$count]['number'] = $line[3];
  196. $result[$count]['reply'] = $line[5];
  197. $result[$count]['login'] = @$line[7];
  198. $count++;
  199. }
  200. }
  201. }
  202. }
  203. return ($result);
  204. }
  205. /**
  206. * Renders reply for PBX external AGI application
  207. *
  208. * @return void
  209. */
  210. public function renderReply($asteriskGet = false, $ignoreCache = false, $getMoney = false) {
  211. if (!$asteriskGet) {
  212. die($this->getReply($ignoreCache, $getMoney));
  213. } else {
  214. $this->getReply($ignoreCache, $getMoney);
  215. }
  216. }
  217. }