Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.37 KB | None | 0 0
  1. <?php
  2.  
  3. include __DIR__.DIRECTORY_SEPARATOR.'parser1.php';
  4. /**
  5.  * @param string &$output : The output of a print_r call; this parameter is DESTRUCTIVE, and will be set to the remainder
  6.  *                             of $output which is not parsed.
  7.  * @return mixed : the first parseable element of $output
  8.  */
  9. function print_r_reverse($output)
  10. {
  11.     $expecting = 0; // 0=nothing in particular, 1=array open paren '(', 2=array element or close paren ')'
  12.     $lines = explode("\n", $output);
  13.     $result = null;
  14.     $topArray = null;
  15.     $arrayStack = array();
  16.     $matches = null;
  17.     while (!empty($lines) && $result === null) {
  18.         $line = array_shift($lines);
  19.         $trim = trim($line);
  20.         if ($trim == 'Array') {
  21.             if ($expecting == 0) {
  22.                 $topArray = array();
  23.                 $expecting = 1;
  24.             } else {
  25.                 trigger_error("Unknown array.");
  26.             }
  27.         } else if ($expecting == 1 && $trim == '(') {
  28.             $expecting = 2;
  29.         } else if ($expecting == 2 && preg_match('/^\[(.+?)\] \=\>\s*(.*)$/', $trim, $matches)) // array element
  30.         {
  31.             list ($fullMatch, $key, $element) = $matches;
  32.             if (trim($element) == 'Array') {
  33.                 $topArray[$key] = array();
  34.                 $newTopArray =& $topArray[$key];
  35.                 $arrayStack[] =& $topArray;
  36.                 $topArray =& $newTopArray;
  37.                 $expecting = 1;
  38.             } else {
  39.                 $topArray[$key] = $element;
  40.             }
  41.         } else if ($expecting == 2 && $trim == ')') // end current array
  42.         {
  43.             if (empty($arrayStack)) {
  44.                 $result = $topArray;
  45.             } else // pop into parent array
  46.             {
  47.                 // safe array pop
  48.                 $keys = array_keys($arrayStack);
  49.                 $lastKey = array_pop($keys);
  50.                 $temp =& $arrayStack[$lastKey];
  51.                 unset($arrayStack[$lastKey]);
  52.                 $topArray =& $temp;
  53.             }
  54.         } else if (!empty($trim)) {
  55.             $result = $line;
  56.         }
  57.     }
  58.  
  59.     $output = implode("\n", $lines);
  60.     return $result;
  61. }
  62.  
  63.  
  64.  
  65. /**
  66.  * @param string $output : The output of a multiple print_r calls, separated by newlines
  67.  * @return mixed[] : parseable elements of $output
  68.  */
  69. function print_r_reverse_multiple($output)
  70. {
  71.     $result = array();
  72.     while (($reverse = print_r_reverse($output)) !== NULL) {
  73.         $result[] = $reverse;
  74.     }
  75.     return $result;
  76. }
  77.  
  78.  
  79. $data = file_get_contents('stat_04.log'); //@todo выбери файл
  80.  
  81. $data = preg_split('~-+\s+\d{4}\-\d{2}\-\d{2}\s+\d{2}:\d{2}:\d{2}\s-+~i', $data);
  82. $t = strtotime('2015-11-04 03:00:00');
  83. $i = 0;
  84. foreach ($data as $k => $v) {
  85.     $array = print_r_reverse($v);
  86.  
  87.     if (isset($array[1]['eventName']) && $array[1]['eventName'] == 'transaction') {
  88.         $array = $array[1];
  89.  
  90.         if(empty($array['eventParams']['area'])){
  91.             continue;
  92.         }
  93.         if (in_array($array['eventParams']['area'], ['LobbyScreen.CashShopDialogTypeC', 'GameScreen.CashShopDialogTypeC', 'DressUpScreen.CashShopDialogTypeC', 'GameScreen.PrefailDialog'])) {
  94.  
  95.             $time = strtotime($array['eventTimestamp']);
  96.  
  97.             if($time <= $t || 1==1) { //@todo укажи лимит времени
  98.  
  99.                 $array['eventParams']['areaName'] = $array['eventParams']['area'];
  100.                 unset($array['eventParams']['area']);
  101.  
  102.                 if(in_array($array['userID'],$a_testers)) {
  103.                     continue;
  104.                 }
  105.  
  106.                 //Переводим строки в другие типы где необходимо
  107.                 array_walk_recursive($array, function (&$item, $key) {
  108.                     if (preg_match('~[a-z]+Amount~', $key) && !is_array($item)) {
  109.                         $item = intval($item);
  110.                     } elseif (in_array($key, ['userLevel', 'lastPlayedMission'])) {
  111.                         $item = intval($item);
  112.                     } elseif (in_array($key, ['isLastMissionFailed'])) {
  113.                         $item = (boolean)$item;
  114.                     }
  115.                 });
  116.                 $i ++;
  117.  
  118.                 $eventData = json_encode($array);
  119.  
  120.                 print_r($array).PHP_EOL;
  121.                 print_r($eventData).PHP_EOL;
  122.                 //sendTrackCurl($eventData);
  123.                 //break;
  124.  
  125.                 // echo "<br>";
  126.                 //print_r($array);
  127.             }
  128.         }
  129.  
  130.     }
  131. }
  132.  
  133. echo $i.PHP_EOL;
  134.  
  135.  
  136.  
  137.  
  138. function sendTrackCurl(  $postFields = array())
  139. {
  140.     $method = 'post';
  141.     $url = '//collect4448bttns.deltadna.net/collect/api/39942684505533704996456211014308/';
  142.  
  143.     $customHeaders = array(
  144.         'Content-Type: application/json'
  145.     );
  146.  
  147.     $timeOut = 50000*3;
  148.  
  149.     try {
  150.  
  151.         if (strpos($url, 'http') === false) {
  152.             $url = 'http:' . $url;
  153.         }
  154.  
  155.         $curl = curl_init();
  156.         curl_setopt($curl, CURLOPT_HEADER, 0);
  157.         curl_setopt($curl, CURLOPT_URL, $url);
  158.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  159.         curl_setopt($curl, CURLOPT_NOSIGNAL, 1); //Unix|Linux SIGALRM hack (for timeouts less than 1 sec.)
  160.         curl_setopt($curl, CURLOPT_TIMEOUT_MS, $timeOut);
  161.         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, $timeOut);
  162.  
  163.         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  164.         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  165.  
  166.         if (sizeof($customHeaders) > 0) {
  167.             curl_setopt($curl, CURLOPT_HTTPHEADER, $customHeaders);
  168.         }
  169.  
  170.  
  171.  
  172.         curl_setopt($curl, CURLOPT_POST, true);
  173.  
  174.         if (sizeof($postFields) > 0) {
  175.             curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
  176.         }
  177.  
  178.         $result = curl_exec($curl);
  179.  
  180.         $curl_errno = curl_errno($curl);
  181.         $curl_error = curl_error($curl);
  182.  
  183.         curl_close($curl);
  184.  
  185.         if ($curl_errno > 0) {
  186.             throw new \Exception("cURL error ({$curl_errno}): {$curl_error}");
  187.         }
  188.         print_r('OK').PHP_EOL;
  189.  
  190.     } catch (\Exception $e) {
  191.  
  192.         /*$sLog = $e->getMessage() . "\n" . $method . ' ' . $url . "\n" .
  193.             "\n" . json_encode($postFields) . "\n" .
  194.             json_encode($customHeaders) . "\n\n";*/
  195.  
  196.         print_r($e->getMessage()).PHP_EOL;
  197.         print_r($postFields).PHP_EOL;
  198.         print_r($url).PHP_EOL;
  199.  
  200.  
  201.  
  202.     }
  203.  
  204.     return $result;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement