Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
- global $wpdb;
- require_once('getstats.php');
- class Logging {
- // declare log file and file pointer as private properties
- private $log_file, $fp;
- // set log file (path and name)
- public function lfile($path) {
- $this->log_file = $path;
- }
- // write message to the log file
- public function lwrite($message) {
- // if file pointer doesn't exist, then open log file
- if (!is_resource($this->fp)) {
- $this->lopen();
- }
- // define script name
- $script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
- // define current time and suppress E_WARNING if using the system TZ settings
- // (don't forget to set the INI setting date.timezone)
- $time = @date('[d/M/Y:H:i:s]');
- // write current time, script name and message to the log file
- fwrite($this->fp, "$time ($script_name) $message" . PHP_EOL);
- }
- // close log file (it's always a good idea to close a file when you're done with it)
- public function lclose() {
- fclose($this->fp);
- }
- // open log file (private method)
- private function lopen() {
- // in case of Windows set default log file
- if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
- $log_file_default = 'c:/php/logfile.txt';
- }
- // set default log file for Linux and other systems
- else {
- $log_file_default = '/tmp/logfile.txt';
- }
- // define log file from lfile method or use previously set default
- $lfile = $this->log_file ? $this->log_file : $log_file_default;
- // open log file for writing only and place file pointer at the end of the file
- // (if the file does not exist, try to create it)
- $this->fp = fopen($lfile, 'a') or exit("Can't open $lfile!");
- }
- };
- $log = new Logging();
- // set path and name of log file (optional)
- $log->lfile('mylog_'.date('d_m_y', time()).'.txt');
- $method = $_SERVER['REQUEST_METHOD'];
- //$wpdb->show_errors();
- if ($method === "POST"){
- $rawdata = file_get_contents("php://input");
- $log->lwrite("POST - ".print_r($rawdata, true));
- $xml = simplexml_load_string($rawdata);
- $xml_data = array();
- foreach($xml->xpath('//xsi:Event/*') as $child){
- switch ($child->getName()){
- case "httpContact":
- break;
- case "eventData":
- $xml_data[$child->getName()] = $child->xpath('./@xsi1:type')[0]->__toString();
- foreach($child->xpath('./xsi:call/*') as $child2){
- switch($child2->getName()){
- case "remoteParty":
- if (isset($child2->xpath('./xsi:address')[0])){
- //$log->lwrite("address - ".print_r($child2->xpath('./xsi:address')[0]->__toString(), true));
- $xml_data["address"] = str_replace('tel:', '', $child2->xpath('./xsi:address')[0]->__toString());
- };
- break;
- default:
- $xml_data[$child2->getName()] = $child2->__toString();
- };
- };
- break;
- default:
- $xml_data[$child->getName()] = $child->__toString();
- }
- };
- $log->lwrite(print_r($xml_data, true));
- $table_users_calls = $wpdb->prefix.users_calls;
- //Из базы берем только список ключей, которые будем оставлять
- //во входных данных
- $allowed_keys = $wpdb->get_row("SELECT * FROM $table_users_calls limit 1", "ARRAY_A");
- $xml_data = array_filter($xml_data);
- $xml_data = array_intersect_key($xml_data, $allowed_keys);
- if ($xml_data["eventData"] == "xsi:SubscriptionTerminatedEvent"){
- $log->lwrite("End subscription");
- } else {
- $log->lwrite("insert in table $table_users_calls");
- if ($wpdb->insert( $table_users_calls, $xml_data)){
- $log->lwrite("inserted");
- } else {
- };
- $releaseTime = str_replace("'", "", $xml_data['releaseTime']);
- $answerTime = str_replace("'", "", $xml_data['answerTime']);
- $duration = (int)(($releaseTime - $answerTime)/1000);
- if ($duration > 0 and $answerTime != 0){
- $table_accounts = $wpdb->prefix.users_accounts;
- $tokens = $wpdb->get_results("SELECT user_id, account_token, custom_update FROM $table_accounts where account_token is not null", "ARRAY_A");
- $log->lwrite(print_r($tokens, true));
- foreach($tokens as $token){
- if ($curl = curl_init()){
- $headers = array(
- "X-MPBX-API-AUTH-TOKEN: ".$token["account_token"]
- );
- curl_setopt($curl, CURLOPT_URL,"https://cloudpbx.beeline.ru/apis/portal/abonents");
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- $out = curl_exec($curl);
- $abonents = json_decode($out);
- foreach($abonents as $abonent){
- if (str_replace("'", "", $xml_data['targetId'])==$abonent->userId){
- $table_calls = $wpdb->prefix.users_calls;
- $calltype = $wpdb->get_row(
- $wpdb->prepare(
- "select ct.eventData,
- (select address from $table_calls as ad where ad.extTrackingId = ct.extTrackingId and ad.startTime = ct.startTime limit 1) as address
- from $table_calls as ct
- where ct.extTrackingId = '%s' and
- ct.eventData in ('xsi:CallOriginatedEvent', 'xsi:CallReceivedEvent')
- limit 1", $xml_data['extTrackingId']
- ),
- "ARRAY_A"
- );
- if (strlen($calltype['address']) == 3) {
- $calltype['eventData'] = 'inner';
- }
- $update_money = false;
- $log->lwrite("ID трека - ".print_r($xml_data['extTrackingId'], true));
- if ($token["custom_update"] == 1){
- $table_sips = $wpdb->prefix.users_accounts_sip;
- $array_sip = $wpdb->get_col($wpdb->prepare("
- SELECT sip
- FROM $table_sips
- where select_sip = 1 and
- users_accounts_id = (select id from $table_accounts where user_id = %d limit 1)", $token["user_id"]));
- $log->lwrite("Список сипов - ".print_r($array_sip, true));
- $update_money = in_array($xml_data['targetId'], $array_sip);
- } else {
- $update_money = true;
- }
- if ($update_money){
- $money = get_money($duration, $token["user_id"], $calltype['eventData']);
- $balance = (float)get_the_author_meta("balance", $token["user_id"]);
- $balance = round($balance, 2);
- update_user_meta($token["user_id"], "balance", $balance-$money);
- $user_info = get_userdata($token["user_id"]);
- $log->lwrite($user_info->user_login." Баланс $balance");
- $log->lwrite($user_info->user_login." Списание баланса $money");
- $notify_lt_1500 = (bool)get_the_author_meta("notify_lt_1500", $token["user_id"]);
- $notify_lt_500 = (bool)get_the_author_meta("notify_lt_500", $token["user_id"]);
- $notify_lt_10 = (bool)get_the_author_meta("notify_lt_10", $token["user_id"]);
- $message = "Здравствуйте. Ваш баланс менее %d руб. Пополнить баланс Вы можете по ссылке <a href='http://stat.onetelecom24.ru/пополнение-баланса/'>Пополнение баланса</a><br/>----------<br/>С уважением,<br/>Команда Onetelecom24.<br/>8(495)796-09-53";
- $headers = "content-type: text/html\r\n";
- if ($notify_lt_1500 && ($balance-$money) < 1500 && ($balance-$money) >= 500 ) {
- wp_mail('476566@mail.ru', 'Заканчиваются средства', 'Баланс пользователя '.$user_info->user_login.' менее 1500 руб.');
- wp_mail($user_info->user_email, 'Заканчиваются средства', sprintf($message, 1500), $headers);
- update_user_meta($token["user_id"], "notify_lt_1500", false);
- };
- if ($notify_lt_500 && ($balance-$money) < 500 && ($balance-$money) >= 10 ) {
- wp_mail('476566@mail.ru', 'Заканчиваются средства', 'Баланс пользователя '.$user_info->user_login.' менее 500 руб.');
- wp_mail($user_info->user_email, 'Заканчиваются средства', sprintf($message, 500), $headers);
- update_user_meta($token["user_id"], "notify_lt_500", false);
- };
- if ($notify_lt_10 && ($balance-$money) < 10) {
- wp_mail('476566@mail.ru', 'Заканчиваются средства', 'Баланс пользователя '.$user_info->user_login.' менее 10 руб.');
- wp_mail($user_info->user_email, 'Заканчиваются средства', sprintf($message, 10), $headers);
- update_user_meta($token["user_id"], "notify_lt_10", false);
- };
- };
- }
- }
- }
- }
- }
- }
- } elseif ($method === "GET") {
- echo "test";
- }
- // close log file
- $log->lclose();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement