Advertisement
postcd

seolinker

Aug 4th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.05 KB | None | 0 0
  1. <?php
  2.  
  3. class SeolinkClient {
  4.     var $sl_version           = '0.0.1';
  5.     var $sl_verbose           = false;
  6.     var $sl_charset           = 'DEFAULT';
  7.     var $sl_use_ssl           = false;
  8.     var $sl_server            = 'db.seolinker.cz';
  9.     var $sl_cache_lifetime    = 3600;
  10.     var $sl_cache_reloadtime  = 300;
  11.     var $sl_links_db_file     = '';
  12.     var $sl_links             = array();
  13.     var $sl_links_page        = array();
  14.     var $sl_links_delimiter   = '';
  15.     var $sl_error             = '';
  16.     var $sl_host              = '';
  17.     var $sl_request_uri       = '';
  18.     var $sl_fetch_remote_type = '';
  19.     var $sl_socket_timeout    = 6;
  20.     var $sl_force_show_code   = true;
  21.     var $sl_multi_site        = false;
  22.     var $sl_is_static         = false;
  23.     var $sl_ignore_tailslash  = false;
  24.  
  25.     function SeolinkClient($options = null) {
  26.         $host = '';
  27.  
  28.         if (is_array($options)) {
  29.             if (isset($options['host'])) {
  30.                 $host = $options['host'];
  31.             }
  32.         } elseif (strlen($options) != 0) {
  33.             $host = $options;
  34.             $options = array();
  35.         } else {
  36.             $options = array();
  37.         }
  38.  
  39.         if (strlen($host) != 0) {
  40.             $this->sl_host = $host;
  41.         } else {
  42.             $this->sl_host = $_SERVER['HTTP_HOST'];
  43.         }
  44.  
  45.         $this->sl_host = preg_replace('{^https?://}i', '', $this->sl_host);
  46.         $this->sl_host = preg_replace('{^www\.}i', '', $this->sl_host);
  47.         $this->sl_host = strtolower( $this->sl_host);
  48.  
  49.         if (isset($options['is_static']) && $options['is_static']) {
  50.             $this->sl_is_static = true;
  51.         }
  52.  
  53.         if (isset($options['ignore_tailslash']) && $options['ignore_tailslash']) {
  54.             $this->sl_ignore_tailslash = true;
  55.         }
  56.  
  57.         if (isset($options['request_uri']) && strlen($options['request_uri']) != 0) {
  58.             $this->sl_request_uri = $options['request_uri'];
  59.         } else {
  60.             if ($this->sl_is_static) {
  61.                 $this->sl_request_uri = preg_replace( '{\?.*$}', '', $_SERVER['REQUEST_URI']);
  62.                 $this->sl_request_uri = preg_replace( '{/+}', '/', $this->sl_request_uri);
  63.             } else {
  64.                 $this->sl_request_uri = $_SERVER['REQUEST_URI'];
  65.             }
  66.         }
  67.  
  68.         $this->sl_request_uri = rawurldecode($this->sl_request_uri);
  69.  
  70.         if (isset($options['multi_site']) && $options['multi_site'] == true) {
  71.             $this->sl_multi_site = true;
  72.         }
  73.  
  74.         if ((isset($options['verbose']) && $options['verbose']) ||
  75.             isset($this->sl_links['__seolink_debug__'])) {
  76.             $this->sl_verbose = true;
  77.         }
  78.  
  79.         if (isset($options['charset']) && strlen($options['charset']) != 0) {
  80.             $this->sl_charset = $options['charset'];
  81.         }
  82.  
  83.         if (isset($options['fetch_remote_type']) && strlen($options['fetch_remote_type']) != 0) {
  84.             $this->sl_fetch_remote_type = $options['fetch_remote_type'];
  85.         }
  86.  
  87.         if (isset($options['socket_timeout']) && is_numeric($options['socket_timeout']) && $options['socket_timeout'] > 0) {
  88.             $this->sl_socket_timeout = $options['socket_timeout'];
  89.         }
  90.  
  91.         if ((isset($options['force_show_code']) && $options['force_show_code']) ||
  92.             isset($this->sl_links['__seolink_debug__'])) {
  93.             $this->sl_force_show_code = true;
  94.         }
  95.  
  96.         if (!defined('SEOLINK_USER')) {
  97.             return $this->raise_error("Constant SEOLINK_USER is not defined.");
  98.         }
  99.  
  100.         $this->load_links();
  101.     }
  102.  
  103.     function load_links() {
  104.         if ($this->sl_multi_site) {
  105.             $this->sl_links_db_file = dirname(__FILE__) . '/seolink.' . $this->sl_host . '.links.db';
  106.         } else {
  107.             $this->sl_links_db_file = dirname(__FILE__) . '/seolink.links.db';
  108.         }
  109.  
  110.         if (!is_file($this->sl_links_db_file)) {
  111.             if (@touch($this->sl_links_db_file, time() - $this->sl_cache_lifetime)) {
  112.                 @chmod($this->sl_links_db_file, 0666);
  113.             } else {
  114.                 return $this->raise_error("There is no file " . $this->sl_links_db_file  . ". Fail to create. Set mode to 777 on the folder.");
  115.             }
  116.         }
  117.  
  118.         if (!is_writable($this->sl_links_db_file)) {
  119.             return $this->raise_error("There is no permissions to write: " . $this->sl_links_db_file . "! Set mode to 777 on the folder.");
  120.         }
  121.  
  122.         @clearstatcache();
  123.  
  124.         if (filemtime($this->sl_links_db_file) < (time()-$this->sl_cache_lifetime) ||
  125.            (filemtime($this->sl_links_db_file) < (time()-$this->sl_cache_reloadtime) && filesize($this->sl_links_db_file) == 0)) {
  126.  
  127.             @touch($this->sl_links_db_file, time());
  128.  
  129.             $path = '/' . SEOLINK_USER . '/' . strtolower( $this->sl_host ) . '/' . strtoupper( $this->sl_charset);
  130.  
  131.             if ($links = $this->fetch_remote_file($this->sl_server, $path)) {
  132.                 if (substr($links, 0, 12) == 'FATAL ERROR:') {
  133.                     $this->raise_error($links);
  134.                 } else if (@unserialize($links) !== false) {
  135.                     $this->sl_write($this->sl_links_db_file, $links);
  136.                 } else {
  137.                     $this->raise_error("Cann't unserialize received data.");
  138.                 }
  139.             }
  140.         }
  141.  
  142.         $links = $this->sl_read($this->sl_links_db_file);
  143.         $this->sl_file_change_date = gmstrftime ("%d.%m.%Y %H:%M:%S",filectime($this->sl_links_db_file));
  144.         $this->sl_file_size = strlen( $links);
  145.         if (!$links) {
  146.             $this->sl_links = array();
  147.             $this->raise_error("Empty file.");
  148.         } else if (!$this->sl_links = @unserialize($links)) {
  149.             $this->sl_links = array();
  150.             $this->raise_error("Cann't unserialize data from file.");
  151.         }
  152.  
  153.         if (isset($this->sl_links['__seolink_delimiter__'])) {
  154.             $this->sl_links_delimiter = $this->sl_links['__seolink_delimiter__'];
  155.         }
  156.  
  157.         $sl_links_temp=array();
  158.         foreach($this->sl_links as $key=>$value){
  159.           $sl_links_temp[rawurldecode($key)]=$value;
  160.         }
  161.         $this->sl_links=$sl_links_temp;
  162.         if ($this->sl_ignore_tailslash && $this->sl_request_uri[strlen($this->sl_request_uri)-1]=='/') $this->sl_request_uri=substr($this->sl_request_uri,0,-1);
  163.         $this->sl_links_page=array();
  164.         if (array_key_exists($this->sl_request_uri, $this->sl_links) && is_array($this->sl_links[$this->sl_request_uri])) {
  165.             $this->sl_links_page = array_merge($this->sl_links_page, $this->sl_links[$this->sl_request_uri]);
  166.         }
  167.         if ($this->sl_ignore_tailslash && array_key_exists($this->sl_request_uri.'/', $this->sl_links) && is_array($this->sl_links[$this->sl_request_uri.'/'])) {
  168.             $this->sl_links_page =array_merge($this->sl_links_page, $this->sl_links[$this->sl_request_uri.'/']);
  169.         }
  170.  
  171.         $this->sl_links_count = count($this->sl_links_page);
  172.     }
  173.  
  174.     function return_links($n = null) {
  175.         $result = '';
  176.         if (isset($this->sl_links['__seolink_start__']) && strlen($this->sl_links['__seolink_start__']) != 0 &&
  177.             (in_array($_SERVER['REMOTE_ADDR'], $this->sl_links['__seolink_robots__']) || $this->sl_force_show_code)
  178.         ) {
  179.             $result .= $this->sl_links['__seolink_start__'];
  180.         }
  181.  
  182.         if (isset($this->sl_links['__seolink_robots__']) && in_array($_SERVER['REMOTE_ADDR'], $this->sl_links['__seolink_robots__']) || $this->sl_verbose) {
  183.  
  184.             if ($this->sl_error != '') {
  185.                 $result .= $this->sl_error;
  186.             }
  187.  
  188.             $result .= '<!--REQUEST_URI=' . $_SERVER['REQUEST_URI'] . "-->\n";
  189.             $result .= "\n<!--\n";
  190.             $result .= 'L ' . $this->sl_version . "\n";
  191.             $result .= 'REMOTE_ADDR=' . $_SERVER['REMOTE_ADDR'] . "\n";
  192.             $result .= 'request_uri=' . $this->sl_request_uri . "\n";
  193.             $result .= 'charset=' . $this->sl_charset . "\n";
  194.             $result .= 'is_static=' . $this->sl_is_static . "\n";
  195.             $result .= 'multi_site=' . $this->sl_multi_site . "\n";
  196.             $result .= 'file change date=' . $this->sl_file_change_date . "\n";
  197.             $result .= 'sl_file_size=' . $this->sl_file_size . "\n";
  198.             $result .= 'sl_links_count=' . $this->sl_links_count . "\n";
  199.             $result .= 'left_links_count=' . count($this->sl_links_page) . "\n";
  200.             $result .= 'n=' . $n . "\n";
  201.             $result .= '-->';
  202.         }
  203.  
  204.         if (is_array($this->sl_links_page)) {
  205.             $total_page_links = count($this->sl_links_page);
  206.  
  207.             if (!is_numeric($n) || $n > $total_page_links) {
  208.                 $n = $total_page_links;
  209.             }
  210.  
  211.             $links = array();
  212.  
  213.             for ($i = 0; $i < $n; $i++) {
  214.                 $links[] = array_shift($this->sl_links_page);
  215.             }
  216.  
  217.             if ( count($links) > 0 && isset($this->sl_links['__seolink_before_text__']) ) {
  218.                $result .= $this->sl_links['__seolink_before_text__'];
  219.             }
  220.  
  221.             $result .= implode($this->sl_links_delimiter, $links);
  222.  
  223.             if ( count($links) > 0 && isset($this->sl_links['__seolink_after_text__']) ) {
  224.                $result .= $this->sl_links['__seolink_after_text__'];
  225.             }
  226.         }
  227.         if (isset($this->sl_links['__seolink_end__']) && strlen($this->sl_links['__seolink_end__']) != 0 &&
  228.             (in_array($_SERVER['REMOTE_ADDR'], $this->sl_links['__seolink_robots__']) || $this->sl_force_show_code)
  229.         ) {
  230.             $result .= $this->sl_links['__seolink_end__'];
  231.         }
  232.         return $result;
  233.     }
  234.  
  235.     function fetch_remote_file($host, $path) {
  236.         $user_agent = 'Seolink Client PHP ' . $this->sl_version;
  237.  
  238.         @ini_set('allow_url_fopen', 1);
  239.         @ini_set('default_socket_timeout', $this->sl_socket_timeout);
  240.         @ini_set('user_agent', $user_agent);
  241.  
  242.         if (
  243.             $this->sl_fetch_remote_type == 'file_get_contents' || (
  244.                 $this->sl_fetch_remote_type == '' && function_exists('file_get_contents') && ini_get('allow_url_fopen') == 1
  245.             )
  246.         ) {
  247.             if ($data = @file_get_contents('http://' . $host . $path)) {
  248.                 return $data;
  249.             }
  250.         } elseif (
  251.             $this->sl_fetch_remote_type == 'curl' || (
  252.                 $this->sl_fetch_remote_type == '' && function_exists('curl_init')
  253.             )
  254.         ) {
  255.             if ($ch = @curl_init()) {
  256.                 @curl_setopt($ch, CURLOPT_URL, 'http://' . $host . $path);
  257.                 @curl_setopt($ch, CURLOPT_HEADER, false);
  258.                 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  259.                 @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->sl_socket_timeout);
  260.                 @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  261.  
  262.                 if ($data = @curl_exec($ch)) {
  263.                     return $data;
  264.                 }
  265.  
  266.                 @curl_close($ch);
  267.             }
  268.         } else {
  269.             $buff = '';
  270.             $fp = @fsockopen($host, 80, $errno, $errstr, $this->sl_socket_timeout);
  271.             if ($fp) {
  272.                 @fputs($fp, "GET {$path} HTTP/1.0\r\nHost: {$host}\r\n");
  273.                 @fputs($fp, "User-Agent: {$user_agent}\r\n\r\n");
  274.                 while (!@feof($fp)) {
  275.                     $buff .= @fgets($fp, 128);
  276.                 }
  277.                 @fclose($fp);
  278.  
  279.                 $page = explode("\r\n\r\n", $buff);
  280.  
  281.                 return $page[1];
  282.             }
  283.         }
  284.  
  285.         return $this->raise_error("Cann't connect to server: " . $host . $path);
  286.     }
  287.  
  288.     function sl_read($filename) {
  289.         $fp = @fopen($filename, 'rb');
  290.         @flock($fp, LOCK_SH);
  291.         if ($fp) {
  292.             clearstatcache();
  293.             $length = @filesize($filename);
  294.             if(get_magic_quotes_gpc()){
  295.                 $mqr = get_magic_quotes_runtime();
  296.                 set_magic_quotes_runtime(0);
  297.             }
  298.             if ($length) {
  299.                 $data = @fread($fp, $length);
  300.             } else {
  301.                 $data = '';
  302.             }
  303.             if(isset($mqr)){
  304.                 set_magic_quotes_runtime($mqr);
  305.             }
  306.             @flock($fp, LOCK_UN);
  307.             @fclose($fp);
  308.  
  309.             return $data;
  310.         }
  311.  
  312.         return $this->raise_error("Cann't get data from the file: " . $filename);
  313.     }
  314.  
  315.     function sl_write($filename, $data) {
  316.         $fp = @fopen($filename, 'wb');
  317.         if ($fp) {
  318.             @flock($fp, LOCK_EX);
  319.             $length = strlen($data);
  320.             @fwrite($fp, $data, $length);
  321.             @flock($fp, LOCK_UN);
  322.             @fclose($fp);
  323.  
  324.             if (md5($this->sl_read($filename)) != md5($data)) {
  325.                 return $this->raise_error("Integrity was breaken while writing to file: " . $filename);
  326.             }
  327.  
  328.             return true;
  329.         }
  330.  
  331.         return $this->raise_error("Cann't write to file: " . $filename);
  332.     }
  333.  
  334.     function raise_error($e) {
  335.         $this->sl_error = '<!--ERROR: ' . $e . '-->';
  336.         return false;
  337.     }
  338.  
  339.    
  340. }
  341.  
  342. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement