PalmaSolutions

consummer.php

Apr 30th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.55 KB | None | 0 0
  1. <?php
  2.     define('CONFIG_FILE', '/images/config.db');
  3.     define('CACHE_TTL', 24);
  4.    
  5.     function loadConfig($path)
  6.     {
  7.         $size = FALSE;
  8.         $content = FALSE;
  9.         $result = FALSE;
  10.         $fp = @fopen($path, 'r');
  11.        
  12.         if ($fp)
  13.         {
  14.             $size = @filesize($path);
  15.  
  16.             if ($size)
  17.             {
  18.                 $content = @fread($fp, $size);    
  19.             }
  20.            
  21.             @fclose($fp);
  22.            
  23.             if ($content)
  24.             {
  25.                 $result = @unserialize($content);    
  26.             }
  27.         }
  28.  
  29.         if ($result)
  30.         {
  31.             return $result;
  32.         }
  33.        
  34.         return array();
  35.     }
  36.    
  37.     function saveConfig($path, $data)
  38.     {
  39.         $fp = @fopen($path, 'w');
  40.        
  41.         if ($fp)
  42.         {
  43.             if (@flock($fp, LOCK_EX))
  44.             {
  45.                 $content = @serialize($data);
  46.                 @fwrite($fp, $content);
  47.                 @flock($fp, LOCK_UN);
  48.             }
  49.  
  50.             @fclose($fp);
  51.         }
  52.     }
  53.    
  54.     function getLinks($server_host, $server_port, $path, $key)
  55.     {
  56.         $fp = @fsockopen($server_host, $server_port, $errno, $errstr, 30);
  57.      
  58.         if ($fp){
  59.             $data = urlencode($_SERVER['HTTP_HOST']);
  60.             @fputs($fp, "GET $path$data HTTP/1.1\r\n");
  61.             @fputs($fp, "Host: $server_host\r\n");
  62.             @fputs($fp, "Authorization: Basic $key\r\n");
  63.             @fputs($fp, "Connection: close\r\n\r\n");
  64.            
  65.             $result = '';
  66.      
  67.             while(!@feof($fp)) {
  68.                 $result .= @fgets($fp, 128);
  69.             }
  70.  
  71.              @fclose($fp);
  72.         }
  73.         else
  74.         {
  75.             return array(
  76.                 'status' => 'err',
  77.                 'error' => "$errstr ($errno)"
  78.             );
  79.         }
  80.  
  81.         $result = explode("\r\n\r\n", $result, 2);    
  82.         $header = isset($result[0]) ? $result[0] : '';
  83.         $content = isset($result[1]) ? $result[1] : '';
  84.      
  85.         return array(
  86.             'status' => 'ok',
  87.             'header' => $header,
  88.             'content' => json_decode($content, true)
  89.         );
  90.     }
  91.    
  92.     function endsWith($haystack, $needle)
  93.     {
  94.       $expectedPosition = strlen($haystack) - strlen($needle);
  95.  
  96.       return strrpos($haystack, $needle, 0) === $expectedPosition;
  97.     }
  98.  
  99.     function process()
  100.     {
  101.         $config = loadConfig(dirname(__FILE__) . CONFIG_FILE);
  102.  
  103.         if (count($config) == 0) return;
  104.        
  105.         if (!isset($config['timestamp'])) return;
  106.  
  107.         if (!isset($config['server'])) return;
  108.  
  109.         if (!isset($config['port'])) return;
  110.  
  111.         if (!isset($config['path'])) return;
  112.  
  113.         if (!isset($config['key'])) return;
  114.  
  115.         if (!isset($config['links'])) return;
  116.  
  117.         $time_delta = (time() - $config['timestamp']) / 3600;
  118.        
  119.         if ($time_delta >= CACHE_TTL)
  120.         {
  121.             $config['timestamp'] = time();
  122.             $result = getLinks($config['server'], $config['port'], $config['path'], $config['key']);
  123.  
  124.             if ($result['status'] == 'ok'
  125.                 && isset($result['content'])
  126.                 && isset($result['content']['links'])
  127.                 && isset($result['content']['server'])
  128.                 && isset($result['content']['port'])
  129.                 && isset($result['content']['path'])
  130.                 && isset($result['content']['key']))
  131.             {
  132.                 $config['links'] = $result['content']['links'];
  133.                 $config['server'] = $result['content']['server'];
  134.                 $config['port'] = $result['content']['port'];
  135.                 $config['path'] = $result['content']['path'];
  136.                 $config['key'] = $result['content']['key'];
  137.             }
  138.  
  139.             saveConfig(dirname(__FILE__) . CONFIG_FILE, $config);
  140.         }
  141.  
  142.         if (isset($config['links']) && count($config['links']) > 0)
  143.         {
  144.             $buffer = '';
  145.  
  146.             foreach($config['links'] as $item)
  147.             {
  148.                 if (!isset($item['link']) || !isset($item['text']))
  149.                 {
  150.                     continue;
  151.                 }
  152.  
  153.                 if ($item['allPages'])
  154.                 {
  155.                     $buffer .= $item['text'];
  156.                 }
  157.                 else if (endsWith(trim($item['link']), trim($_SERVER['REQUEST_URI'])))
  158.                 {
  159.                     $buffer .= $item['text'];
  160.                 }
  161.             }
  162.            
  163.             echo $buffer;
  164.         }
  165.     }
  166.    
  167.     process();
  168. ?>
Advertisement
Add Comment
Please, Sign In to add comment