Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.29 KB | None | 0 0
  1. <?php
  2. require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
  3. global $wpdb;
  4. require_once('getstats.php');
  5.  
  6. class Logging {
  7. // declare log file and file pointer as private properties
  8. private $log_file, $fp;
  9. // set log file (path and name)
  10. public function lfile($path) {
  11. $this->log_file = $path;
  12. }
  13. // write message to the log file
  14. public function lwrite($message) {
  15. // if file pointer doesn't exist, then open log file
  16. if (!is_resource($this->fp)) {
  17. $this->lopen();
  18. }
  19. // define script name
  20. $script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
  21. // define current time and suppress E_WARNING if using the system TZ settings
  22. // (don't forget to set the INI setting date.timezone)
  23. $time = @date('[d/M/Y:H:i:s]');
  24. // write current time, script name and message to the log file
  25. fwrite($this->fp, "$time ($script_name) $message" . PHP_EOL);
  26. }
  27. // close log file (it's always a good idea to close a file when you're done with it)
  28. public function lclose() {
  29. fclose($this->fp);
  30. }
  31. // open log file (private method)
  32. private function lopen() {
  33. // in case of Windows set default log file
  34. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  35. $log_file_default = 'c:/php/logfile.txt';
  36. }
  37. // set default log file for Linux and other systems
  38. else {
  39. $log_file_default = '/tmp/logfile.txt';
  40. }
  41. // define log file from lfile method or use previously set default
  42. $lfile = $this->log_file ? $this->log_file : $log_file_default;
  43. // open log file for writing only and place file pointer at the end of the file
  44. // (if the file does not exist, try to create it)
  45. $this->fp = fopen($lfile, 'a') or exit("Can't open $lfile!");
  46. }
  47. };
  48. $log = new Logging();
  49.  
  50. // set path and name of log file (optional)
  51. $log->lfile('mylog_'.date('d_m_y', time()).'.txt');
  52.  
  53. $method = $_SERVER['REQUEST_METHOD'];
  54.  
  55. //$wpdb->show_errors();
  56.  
  57. if ($method === "POST"){
  58. $rawdata = file_get_contents("php://input");
  59. $log->lwrite("POST - ".print_r($rawdata, true));
  60. $xml = simplexml_load_string($rawdata);
  61. $xml_data = array();
  62. foreach($xml->xpath('//xsi:Event/*') as $child){
  63. switch ($child->getName()){
  64. case "httpContact":
  65. break;
  66. case "eventData":
  67. $xml_data[$child->getName()] = $child->xpath('./@xsi1:type')[0]->__toString();
  68. foreach($child->xpath('./xsi:call/*') as $child2){
  69. switch($child2->getName()){
  70. case "remoteParty":
  71. if (isset($child2->xpath('./xsi:address')[0])){
  72. //$log->lwrite("address - ".print_r($child2->xpath('./xsi:address')[0]->__toString(), true));
  73. $xml_data["address"] = str_replace('tel:', '', $child2->xpath('./xsi:address')[0]->__toString());
  74. };
  75. break;
  76. default:
  77. $xml_data[$child2->getName()] = $child2->__toString();
  78. };
  79. };
  80. break;
  81. default:
  82. $xml_data[$child->getName()] = $child->__toString();
  83. }
  84. };
  85. $log->lwrite(print_r($xml_data, true));
  86. $table_users_calls = $wpdb->prefix.users_calls;
  87. //Из базы берем только список ключей, которые будем оставлять
  88. //во входных данных
  89. $allowed_keys = $wpdb->get_row("SELECT * FROM $table_users_calls limit 1", "ARRAY_A");
  90. $xml_data = array_filter($xml_data);
  91. $xml_data = array_intersect_key($xml_data, $allowed_keys);
  92. if ($xml_data["eventData"] == "xsi:SubscriptionTerminatedEvent"){
  93. $log->lwrite("End subscription");
  94. } else {
  95. $log->lwrite("insert in table $table_users_calls");
  96. if ($wpdb->insert( $table_users_calls, $xml_data)){
  97. $log->lwrite("inserted");
  98. } else {
  99.  
  100. };
  101. $releaseTime = str_replace("'", "", $xml_data['releaseTime']);
  102. $answerTime = str_replace("'", "", $xml_data['answerTime']);
  103. $duration = (int)(($releaseTime - $answerTime)/1000);
  104. if ($duration > 0 and $answerTime != 0){
  105. $table_accounts = $wpdb->prefix.users_accounts;
  106. $tokens = $wpdb->get_results("SELECT user_id, account_token, custom_update FROM $table_accounts where account_token is not null", "ARRAY_A");
  107. $log->lwrite(print_r($tokens, true));
  108. foreach($tokens as $token){
  109. if ($curl = curl_init()){
  110. $headers = array(
  111. "X-MPBX-API-AUTH-TOKEN: ".$token["account_token"]
  112. );
  113. curl_setopt($curl, CURLOPT_URL,"https://cloudpbx.beeline.ru/apis/portal/abonents");
  114. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  115. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  116. $out = curl_exec($curl);
  117. $abonents = json_decode($out);
  118. foreach($abonents as $abonent){
  119. if (str_replace("'", "", $xml_data['targetId'])==$abonent->userId){
  120. $table_calls = $wpdb->prefix.users_calls;
  121. $calltype = $wpdb->get_row(
  122. $wpdb->prepare(
  123. "select ct.eventData,
  124. (select address from $table_calls as ad where ad.extTrackingId = ct.extTrackingId and ad.startTime = ct.startTime limit 1) as address
  125. from $table_calls as ct
  126. where ct.extTrackingId = '%s' and
  127. ct.eventData in ('xsi:CallOriginatedEvent', 'xsi:CallReceivedEvent')
  128. limit 1", $xml_data['extTrackingId']
  129. ),
  130. "ARRAY_A"
  131. );
  132. if (strlen($calltype['address']) == 3) {
  133. $calltype['eventData'] = 'inner';
  134. }
  135. $update_money = false;
  136. $log->lwrite("ID трека - ".print_r($xml_data['extTrackingId'], true));
  137. if ($token["custom_update"] == 1){
  138. $table_sips = $wpdb->prefix.users_accounts_sip;
  139. $array_sip = $wpdb->get_col($wpdb->prepare("
  140. SELECT sip
  141. FROM $table_sips
  142. where select_sip = 1 and
  143. users_accounts_id = (select id from $table_accounts where user_id = %d limit 1)", $token["user_id"]));
  144. $log->lwrite("Список сипов - ".print_r($array_sip, true));
  145. $update_money = in_array($xml_data['targetId'], $array_sip);
  146. } else {
  147. $update_money = true;
  148. }
  149. if ($update_money){
  150. $money = get_money($duration, $token["user_id"], $calltype['eventData']);
  151. $balance = (float)get_the_author_meta("balance", $token["user_id"]);
  152. $balance = round($balance, 2);
  153. update_user_meta($token["user_id"], "balance", $balance-$money);
  154. $user_info = get_userdata($token["user_id"]);
  155. $log->lwrite($user_info->user_login." Баланс $balance");
  156. $log->lwrite($user_info->user_login." Списание баланса $money");
  157. $notify_lt_1500 = (bool)get_the_author_meta("notify_lt_1500", $token["user_id"]);
  158. $notify_lt_500 = (bool)get_the_author_meta("notify_lt_500", $token["user_id"]);
  159. $notify_lt_10 = (bool)get_the_author_meta("notify_lt_10", $token["user_id"]);
  160. $message = "Здравствуйте. Ваш баланс менее %d руб. Пополнить баланс Вы можете по ссылке <a href='http://stat.onetelecom24.ru/пополнение-баланса/'>Пополнение баланса</a><br/>----------<br/>С уважением,<br/>Команда Onetelecom24.<br/>8(495)796-09-53";
  161. $headers = "content-type: text/html\r\n";
  162. if ($notify_lt_1500 && ($balance-$money) < 1500 && ($balance-$money) >= 500 ) {
  163. wp_mail('476566@mail.ru', 'Заканчиваются средства', 'Баланс пользователя '.$user_info->user_login.' менее 1500 руб.');
  164. wp_mail($user_info->user_email, 'Заканчиваются средства', sprintf($message, 1500), $headers);
  165. update_user_meta($token["user_id"], "notify_lt_1500", false);
  166. };
  167. if ($notify_lt_500 && ($balance-$money) < 500 && ($balance-$money) >= 10 ) {
  168. wp_mail('476566@mail.ru', 'Заканчиваются средства', 'Баланс пользователя '.$user_info->user_login.' менее 500 руб.');
  169. wp_mail($user_info->user_email, 'Заканчиваются средства', sprintf($message, 500), $headers);
  170. update_user_meta($token["user_id"], "notify_lt_500", false);
  171. };
  172. if ($notify_lt_10 && ($balance-$money) < 10) {
  173. wp_mail('476566@mail.ru', 'Заканчиваются средства', 'Баланс пользователя '.$user_info->user_login.' менее 10 руб.');
  174. wp_mail($user_info->user_email, 'Заканчиваются средства', sprintf($message, 10), $headers);
  175. update_user_meta($token["user_id"], "notify_lt_10", false);
  176. };
  177. };
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. } elseif ($method === "GET") {
  185. echo "test";
  186. }
  187.  
  188. // close log file
  189. $log->lclose();
  190. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement