Advertisement
plas71k

2.php decoded - free version

Nov 23rd, 2012
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 30.41 KB | None | 0 0
  1. <?php
  2. function curent_date($var, $var2)
  3. {
  4.     date_default_timezone_set("Europe/Dublin");
  5.     return date($var, $var2);
  6. }
  7.  
  8. function w_log($name, $content)
  9. {
  10.     global $warr;
  11.     $worked_time = round(microtime(true) - $warr['start_time'], 3);
  12.     $ram         = memory_get_usage();
  13.     $warr['log'] .= "{$worked_time}:{$name} - {$content} | {$ram}";
  14. }
  15.  
  16. function is_boot()
  17. {
  18.     if (!preg_match("/bot|crawl|spider|robo/i", $_SERVER['HTTP_USER_AGENT']) && preg_match("/firefox|windows|explor|chrome|opera/i", $_SERVER['HTTP_USER_AGENT'])) {
  19.         return false;
  20.     }
  21.     return true;
  22. }
  23.  
  24. function skim($skim)
  25. {
  26.     $rn = floor(10000 / $skim);
  27.     if (mt_rand(1, $rn) <= 100 && 0 < $skim) {
  28.         return true;
  29.     }
  30.     return false;
  31. }
  32.  
  33. function title_to_len($title, $limit)
  34. {
  35.     if ($limit < strlen($title)) {
  36.         $title = substr($title, 0, strrpos(substr($title, 0, $limit), " ")) . "";
  37.     }
  38.     return $title;
  39. }
  40.  
  41. ini_set("expose_php", "off");
  42. ini_set("display_errors", "off");
  43. ini_set("display_startup_errors", "off");
  44. $config     = unserialize(file_get_contents("files/config"));
  45. $flgs       = unserialize(file_get_contents("files/flgs"));
  46. $st         = microtime(true);
  47. $time       = time();
  48. $cache_step = $config['cache_step'];
  49. if (empty($cache_step)) {
  50.     $cache_step = 1;
  51. }
  52. $curent_hour        = curent_date("G", time());
  53. $pstats_id          = md5($_SERVER['REQUEST_URI']);
  54. $host               = str_replace("www.", "", getenv("HTTP_HOST"));
  55. $warr               = array();
  56. $warr['start_time'] = microtime(true);
  57. $warr['host']       = $host;
  58. if (preg_match("/n=|t=|f=/i", $_SERVER['REQUEST_URI'])) {
  59.     preg_match("/([\\.\\?\\=0-9a-z\\_\\-\\/]{1,64})/i", $_SERVER['REQUEST_URI'], $matches);
  60. } else {
  61.     preg_match("/([0-9a-z\\_\\-\\/]{1,64})/i", $_SERVER['REQUEST_URI'], $matches);
  62. }
  63. $warr['uri_filtred'] = $matches['1'];
  64. $warr['query_url']   = str_replace("www.", "", "http://{$_SERVER['HTTP_HOST']}{$warr['uri_filtred']}");
  65. $warr['log']         = "";
  66. $warr['format']      = "m";
  67. $warr['param_str']   = "";
  68. $warr['query_mode']  = "base_search";
  69. w_log("warr_query_url", $warr['query_url']);
  70. if (empty($_SERVER['HTTP_USER_AGENT'])) {
  71.     $_SERVER['HTTP_USER_AGENT'] = "bot";
  72. }
  73. $warr['is_boot'] = is_boot();
  74. $link            = mysql_connect($config['host'], $config['user'], $config['pass']);
  75. if (mysql_select_db($config['dbname'], $link)) {
  76.     w_log("mysql_select_db", "ok");
  77. } else {
  78.     w_log("mysql_select_db", "error");
  79. }
  80. $cache_time = 240 * $cache_step;
  81. if (15 < $flgs['la']['0']) {
  82.     $cache_time = $cache_time * 10;
  83. }
  84. if (isset($_COOKIE['adminlog'])) {
  85.     $cache_time = "0";
  86. }
  87. if (!empty($_REQUEST['p'])) {
  88.     preg_match("/[0-9]{0,3}/", $_REQUEST['p'], $matches);
  89.     $warr['p'] = $matches['0'];
  90.     $warr['param_str'] .= $matches['0'];
  91. }
  92. if ($_REQUEST['n'] != __FILE__) {
  93.     preg_match("/[a-z0-9_]{1,32}/i", $_REQUEST['n'], $matches);
  94.     $_REQUEST['n']       = $matches['0'];
  95.     $warr['query_niche'] = $matches['0'];
  96.     $warr['param_str'] .= $matches['0'];
  97.     $warr['n'] = $matches['0'];
  98.     if (!empty($_COOKIE['tr_categories']) && $warr['is_boot'] == false) {
  99.         mysql_query("update `niches` set `clicks`=clicks+1 where `name`='{$warr['query_niche']}'");
  100.     }
  101. }
  102. if (!empty($_REQUEST['t'])) {
  103.     preg_match("/[a-z0-9]{1,32}/i", $_REQUEST['t'], $matches);
  104.     $_REQUEST['t'] = $matches['0'];
  105.     $warr['param_str'] .= $matches['0'];
  106.     $warr['t'] = $matches['0'];
  107. }
  108. if (!empty($warr['t']) || !empty($warr['p'])) {
  109.     $cache_time = 14400 * $cache_step;
  110. }
  111. if (!empty($_REQUEST['id'])) {
  112.     preg_match("/v([0-9]{1,8})/", $_REQUEST['id'], $matches);
  113.     @$id = $matches['1'];
  114.     @$warr['id'] = $matches['1'];
  115.     $warr['param_str'] .= $matches['1'];
  116. }
  117. if (!empty($_REQUEST['f']) && count($_REQUEST['f']) == "1" && $_REQUEST['f'] == "c") {
  118.     $warr['format'] = "c";
  119.     $warr['param_str'] .= "c";
  120.     $cache_time = $cache_time * 3;
  121.     setcookie("tr_categories", "1", time() + 86400, "/", ".{$host}");
  122.     mysql_query("update `niches` set `hits`=hits+1 where `name`='" . __FILE__ . "'");
  123. }
  124. $warr['cache_file'] = md5($warr['param_str']);
  125. w_log("cache_file", $warr['cache_file']);
  126. $r_max  = 20;
  127. $rp_max = 10;
  128. if ($flgs['num_rs'] <= 200) {
  129.     $r_max  = 3;
  130.     $rp_max = 2;
  131. }
  132. if (200 < $flgs['num_rs'] && $flgs['num_rs'] < 1000) {
  133.     $r_max  = 5;
  134.     $rp_max = 8;
  135. }
  136. if (mt_rand(1, $r_max) == "1") {
  137.     $rand = true;
  138. }
  139. if (empty($_COOKIE['tr_main']) && $warr['is_boot'] == false && $warr['format'] != "c") {
  140.     w_log("mysql_insert_pstats", "start");
  141.     if (mysql_query("UPDATE `pstats` set `i`=`i`+'1' where `id`='{$pstats_id}'")) {
  142.         w_log("mysql_insert_pstats", "ok");
  143.     } else {
  144.         w_log("mysql_insert_pstats", "error");
  145.     }
  146.     if (mysql_query("update `stats_h`                        set `hits`=hits+1                        where `hour`='{$curent_hour}'                        ")) {
  147.         w_log("mysql_update_hits", "ok");
  148.     } else {
  149.         w_log("mysql_update_hits", "error");
  150.     }
  151. } else {
  152.     w_log("mysql_update_hits", "noneed");
  153. }
  154. if (!empty($id)) {
  155.     w_log("as_id", $id);
  156.     if (!empty($_COOKIE['tr_main']) && $_COOKIE['tr_' . __FILE__] < 21 && empty($_COOKIE['tr_cjclicks']) && !preg_match("/bot|crawl|spider|robo/i", $_SERVER['HTTP_USER_AGENT']) && preg_match("/firefox|windows|explor|chrome|opera/i", $_SERVER['HTTP_USER_AGENT'])) {
  157.         mysql_query("update `w_content` set `l_clicks`=l_clicks+1,`clicks`=clicks+1 where `id` = '{$id}'");
  158.         mysql_query("update `stats_h` set `clicks`=clicks+1 where `hour` = '{$curent_hour}'");
  159.         w_log("as_id_update_stats", "ok");
  160.     }
  161.     if (mt_rand(1, 100) == "1" && empty($_COOKIE['tr_cjclicks']) && !empty($_COOKIE['tr_main'])) {
  162.         $domain = preg_replace("/^www./i", "", getenv("HTTP_HOST"));
  163.         if (!preg_match("/{$domain}/i", getenv("SCRIPT_FILENAME"))) {
  164.             $domain = mt_rand(999, 999999);
  165.         }
  166.         $str_to_key = "E45RtGD1Nb5 & +&7?r/Gt%4w8g5-^f1v#2@1xkjdpenDortmg)grF{$domain}T4G1fd-Qafuy6*D1cu&ld";
  167.         $key        = sha1($str_to_key);
  168.         $key        = "{$domain}_{$key}";
  169.         if ($config['lic_key'] != $key) {
  170.             header("location:http://www.tuberotator.com/src/in.php?d={$domain}&n={$_REQUEST['n']}&t={$_REQUEST['t']}");
  171.             mysql_close();
  172.             exit();
  173.         } else {
  174.             unset($key);
  175.             unset($str_to_key);
  176.         }
  177.     }
  178.     if ($config['mode'] == "out") {
  179.         $q = mysql_query("select url from `w_content` where `id`='{$id}'");
  180.         $r = mysql_fetch_array($q);
  181.         header("location:{$r['url']}");
  182.         exit();
  183.     } else {
  184.         $cache_time = 3600 * 6 * $cache_step;
  185.         if (isset($_COOKIE['adminlog'])) {
  186.             $cache_time = "0";
  187.         }
  188.         $rel = true;
  189.     }
  190. }
  191. if (empty($_COOKIE['tr_main'])) {
  192.     setcookie("tr_" . __FILE__, "1", time() + 86400, "/", ".{$host}");
  193.     w_log("cookie_new_tr_" . __FILE__, "ok");
  194. } else {
  195.     $c = intval($_COOKIE['tr_' . __FILE__]) + 1;
  196.     setcookie("tr_" . __FILE__, $c, time() + 86400, "/", ".{$host}");
  197.     w_log("cookie_update_tr_" . __FILE__, $c);
  198. }
  199. $cache_file = $warr['cache_file'];
  200. $cache_dir  = substr($cache_file, 0, 3);
  201. $cache_dir  = "files/tmp/cache/{$cache_dir}";
  202. $cache_file = "{$cache_dir}/{$cache_file}";
  203. w_log("cache_check_local_files", "start");
  204. if (file_exists("files/tmp/flgs_regen_url")) {
  205.     $flgs_regen_url = file_get_contents("files/tmp/flgs_regen_url");
  206. } else {
  207.     $flgs_regen_url = false;
  208. }
  209. if (file_exists($cache_file) && $warr['query_url'] != $flgs_regen_url && empty($_COOKIE['adminlog'])) {
  210.     w_log("cache_check_local_files", "end");
  211.     if (time() - $cache_time < filemtime($cache_file)) {
  212.         $flgs['cache'] = "on";
  213.         w_log("cache_read_cache_file", "start");
  214.         include($cache_file);
  215.         w_log("cache_read_cache_file", "end");
  216.         w_log("cache_time", $cache_time);
  217.         w_log("cache_get_from_file", $cache_file);
  218.     } else {
  219.         if (file_exists("files/tmp/cache/caches")) {
  220.             $caches = unserialize(file_get_contents("files/tmp/cache/caches"));
  221.             if (!is_array($caches)) {
  222.                 $caches = array();
  223.             }
  224.         } else {
  225.             $caches = array();
  226.         }
  227.         if (!in_array($warr['query_url'], $caches)) {
  228.             $caches[] = $warr['query_url'];
  229.         }
  230.         if (file_exists("files/tmp/cache/caches")) {
  231.             unlink("files/tmp/cache/caches");
  232.         }
  233.         file_put_contents("files/tmp/cache/caches", serialize($caches));
  234.         chmod("files/tmp/cache/caches", 438);
  235.         file_put_contents($cache_file, file_get_contents($cache_file));
  236.         include($cache_file);
  237.     }
  238. } else {
  239.     w_log("content", "gen new start");
  240.     file_put_contents("files/tmp/tmp_gen_page", "");
  241.     $flgs['cache'] = "off";
  242.     $q             = mysql_query("SELECT * FROM `templates`");
  243.     while ($r = mysql_fetch_array($q)) {
  244.         $arr['templates'][$r['name']] = $r['template'];
  245.     }
  246.     if (empty($_REQUEST['n']) && empty($_REQUEST['t'])) {
  247.         $_REQUEST['n'] = __FILE__;
  248.         w_log("content", "type " . __FILE__);
  249.     }
  250.     function get_all_ads($ads)
  251.     {
  252.         global $config;
  253.         if (empty($ads) && isset($config['a_block_flg']) && $config['a_block_flg'] == "on") {
  254.             $q   = mysql_query("SELECT * FROM `niches` where `name`='" . __FILE__ . "'");
  255.             $r   = mysql_fetch_assoc($q);
  256.             $ads = $r['ads'];
  257.         }
  258.         if (preg_match("/STARTADS/", $ads)) {
  259.             preg_match_all("/<--STARTADS([0-9]{1,3})-->(.+)<--END-->/Us", $ads, $matches, PREG_SET_ORDER);
  260.             foreach ($matches as $value) {
  261.                 $ads_arr[$value[1]] = $value[2];
  262.             }
  263.         } else {
  264.             $ads_arr['0'] = $ads;
  265.         }
  266.         return $ads_arr;
  267.     }
  268.     if (!empty($_REQUEST['n'])) {
  269.         $q = mysql_query("select * from `niches`");
  270.         while ($r = mysql_fetch_assoc($q)) {
  271.             $warr['all_niches_arr']      = $r;
  272.             $warr['all_niches_keys'][]   = $r['name'];
  273.             $warr['all_niches_thumbs'][] = $r['thumb'];
  274.         }
  275.         preg_match_all("/[a-z0-9_]{3,24}*?/i", $_REQUEST['n'], $search_query);
  276.         @$niche = @mysql_real_escape_string($search_query['0']['0']);
  277.         $q                     = mysql_query("SELECT * FROM `niches` where `name`='{$niche}'");
  278.         $r                     = mysql_fetch_assoc($q);
  279.         $niche                 = $r['name'];
  280.         $niche_num             = $r['num'];
  281.         $niche_keys            = $r['keys'];
  282.         $warr['niche_keys']    = $r['keys'];
  283.         $niche_title           = $r['title'];
  284.         $niche_desc            = $r['desc'];
  285.         $query                 = $niche_keys;
  286.         $warr['niche_ads_arr'] = get_all_ads($r['ads']);
  287.         if (preg_match("/[(|)]/", $niche_keys)) {
  288.             preg_match_all("/[a-z0-9()+s*-~]{3,128}*?/i", $niche_keys, $search_query);
  289.             $warr['query_mode'] = "full_search";
  290.         } else {
  291.             preg_match_all("/[a-z0-9]{3,18}*?/i", $niche_keys, $search_query);
  292.         }
  293.         w_log("content", "VARS:niche={$niche};niche_num = {$niche_num};niche_keys={$niche_keys};query={$query}");
  294.     } else if (!empty($_REQUEST['t'])) {
  295.         w_log("content", "type tag");
  296.         $q                     = mysql_query("SELECT * FROM `niches` where `name`='" . __FILE__ . "'");
  297.         $r                     = mysql_fetch_assoc($q);
  298.         $warr['niche_ads_arr'] = get_all_ads($r['ads']);
  299.         preg_match_all("/[a-z]{3,18}/i", $_REQUEST['t'], $search_query);
  300.         @$query = $search_query['0']['0'];
  301.         $q         = mysql_query("SELECT sum(num) FROM `tags_stats` WHERE `tag` like '{$query}%'");
  302.         $r         = mysql_fetch_row($q);
  303.         $niche_num = $r['0'];
  304.         w_log("content", "VARS:query={$query};niche_num={$niche_num}");
  305.     }
  306.     $search_query = array_unique($search_query['0']);
  307.     $search_str   = implode(" ", $search_query);
  308.     $num_words    = count($search_query);
  309.     if ($warr['format'] == "c") {
  310.         $main_tmpl     = $arr['templates']['categories'];
  311.         $warr['query'] = "";
  312.     } else {
  313.         $main_tmpl = $arr['templates'][__FILE__];
  314.         if (!empty($rel)) {
  315.             $main_tmpl = $arr['templates']['embed'];
  316.         }
  317.     }
  318.     preg_match_all("/<--THUMB-->/", $main_tmpl, $matches);
  319.     $limit = count($matches[0]);
  320.     w_log("content_num_thumbs", $limit);
  321.     @$warr['num_pages'] = ceil($niche_num / $limit);
  322.     $limit_start_max = $niche_num - $limit;
  323.     if ($limit_start_max < $limit) {
  324.         $limit_start_max = "0";
  325.     }
  326.     if (100000 < $limit_start_max) {
  327.         $limit_start_max = "100000";
  328.     }
  329.     $limit_start = "0";
  330.     if (!empty($warr['p'])) {
  331.         $limit_start = $warr['p'] * $limit - $limit;
  332.     }
  333.     if ($query != "*" && $warr['query_mode'] != "full_search") {
  334.         w_log("content_mode1_str_start", "ok");
  335.         function dbq($type)
  336.         {
  337.             global $search_query;
  338.             global $num_words;
  339.             global $limit_start;
  340.             global $limit_start_max;
  341.             global $limit;
  342.             global $niche_num;
  343.             global $rel;
  344.             global $config;
  345.             $q_str = "";
  346.             $i     = 0;
  347.             foreach ($search_query as $tag) {
  348.                 if (!empty($_REQUEST['n'])) {
  349.                     if (substr($tag, 0 - 1, 1) == "*") {
  350.                         $tag = str_replace("*", "", $tag);
  351.                         $q_str .= "`tag` like '{$tag}%'";
  352.                     } else {
  353.                         $q_str .= "`tag` ='{$tag}'";
  354.                     }
  355.                 }
  356.                 if (!empty($_REQUEST['t'])) {
  357.                     if (3 < strlen($tag)) {
  358.                         $tag = preg_replace("/^(.+)([m|t|b]{0,1}ings?|ous|ies|ed|s|y)$/isU", "$1", $tag);
  359.                     }
  360.                     $q_str .= "`tag` like '{$tag}%'";
  361.                 }
  362.                 ++$i;
  363.                 if ($i == $num_words) {
  364.                     break;
  365.                 }
  366.                 $q_str .= " or ";
  367.             }
  368.             $key       = implode(" ", $search_query);
  369.             $tq        = mysql_query("SELECT tid,num FROM `tags_stats` where {$q_str}");
  370.             $i         = 0;
  371.             $force     = "use index(`tid`,`ctr`)";
  372.             $q_str     = "";
  373.             $niche_num = "";
  374.             if (!empty($tq)) {
  375.                 $num_rows = mysql_num_rows($tq);
  376.                 while ($tqr = mysql_fetch_array($tq)) {
  377.                     $niche_num = $niche_num + $tqr['num'];
  378.                     $table_id  = substr($tqr['tid'], 0 - 2, 2);
  379.                     if (strlen($table_id) < 2) {
  380.                         $table_id = "0{$table_id}";
  381.                     }
  382.                     if ($type == "h") {
  383.                         $table_name = "`_tags{$table_id}`";
  384.                     } else {
  385.                         $table_name = "`tags{$table_id}`";
  386.                     }
  387.                     $q_str .= "SELECT id,ctr FROM {$table_name}  {$force} where `tid` = '{$tqr['tid']}'";
  388.                     ++$i;
  389.                     if ($i == $num_rows) {
  390.                         break;
  391.                     }
  392.                     $q_str .= " union ";
  393.                 }
  394.             }
  395.             $sort_order       = "order by `ctr` DESC";
  396.             $limit_start_to_q = $limit_start;
  397.             if (!empty($rand) || !empty($rel) && $config['rotation_mode'] != "max_perfomance") {
  398.                 $limit_max_to_q = $limit_start_max;
  399.                 if (4000 < $limit_max_to_q && !empty($rand)) {
  400.                     $limit_max_to_q = 4000;
  401.                 }
  402.                 $limit_start_to_q = mt_rand(0, $limit_max_to_q);
  403.             }
  404.             $q_str .= " {$sort_order} LIMIT {$limit_start_to_q},{$limit}";
  405.             return $q_str;
  406.         }
  407.         $q = mysql_query(dbq("h"));
  408.         @$row = @mysql_num_rows($q);
  409.         if (!$q || $row < 1) {
  410.             $q = mysql_query(dbq("m"));
  411.             @$row = @mysql_num_rows($q);
  412.             w_log("content_mode1_get_from", "disk_table");
  413.         } else {
  414.             w_log("content_mode1_get_from", "ram");
  415.         }
  416.         $q_str = "where `id` in (";
  417.         $i     = 0;
  418.         while ($r = @mysql_fetch_array($q)) {
  419.             $q_str .= "{$r['id']}";
  420.             ++$i;
  421.             if ($i == $row) {
  422.                 break;
  423.             }
  424.             $q_str .= ",";
  425.         }
  426.         $sort_order = "order by `ctr` DESC";
  427.         $q_str .= ") {$sort_order} ";
  428.         w_log("content_mode1_str_end", "str:{$q_str}");
  429.     }
  430.     function duration_time($sec)
  431.     {
  432.         $duration = curent_date("i:s", $sec);
  433.         if (3599 < $sec) {
  434.             $h        = floor($sec / 3600);
  435.             $ms       = curent_date("i:s", $sec);
  436.             $duration = "{$h}:{$ms}";
  437.         }
  438.         return $duration;
  439.     }
  440.     if ($query == "*" && $warr['query_mode'] != "full_search") {
  441.         w_log("content_mode2_str_start", "ok");
  442.         $sort_order = "order by `ctr` DESC";
  443.         $use_index  = "use index(`ctr_2`)";
  444.         $q_str      = "{$use_index} where `status`='1' {$sort_order} limit {$limit_start},{$limit}";
  445.         w_log("content_mode2_order", "{$sort_order}");
  446.         if (!empty($rand) || !empty($rel) && $config['rotation_mode'] != "max_perfomance") {
  447.             $limit_start_to_q = mt_rand(0, $limit_start_max);
  448.             $q                = mysql_query("select id from ids where i>='{$limit_start_to_q}' limit {$limit}");
  449.             while ($r = mysql_fetch_array($q)) {
  450.                 $arr_ids[] = $r['id'];
  451.             }
  452.             @$ids = @implode(",", $arr_ids);
  453.             $variant = "2";
  454.             if ($variant == "2" && !empty($arr_ids)) {
  455.                 $q_str = "where ";
  456.                 $i     = 0;
  457.                 foreach ($arr_ids as $iid) {
  458.                     $q_str .= "`id`='{$iid}'";
  459.                     ++$i;
  460.                     if (count($arr_ids) <= $i) {
  461.                         break;
  462.                     }
  463.                     $q_str .= " or ";
  464.                 }
  465.                 $q_str .= " limit 0,{$limit}";
  466.             } else {
  467.                 $q_str = "where `id` in({$ids}) limit 0,{$limit}";
  468.             }
  469.             w_log("content_mode2_order", "rand");
  470.         }
  471.         w_log("content_mode2_str_start", "end");
  472.     }
  473.     if ($warr['query_mode'] == "full_search") {
  474.         w_log("content_mode3_str_start", "ok");
  475.         function multi_search($query)
  476.         {
  477.             global $rel;
  478.             global $limit_start_max;
  479.             global $limit_start;
  480.             global $limit;
  481.             global $config;
  482.             global $warr;
  483.             if (!empty($rel) && $config['rotation_mode'] != "max_perfomance") {
  484.                 $limit_start = mt_rand(0, $limit_start_max);
  485.                 w_log("content_mode3_sort_order", "rand");
  486.             }
  487.             w_log("content_mode3_sort_order", "desc or other");
  488.             $q_str      = "";
  489.             $cache_time = 3600 * 6;
  490.             w_log("content_mode3_query_dump_cache_time", $cache_time);
  491.             $cache_file = md5($warr['niche_keys']);
  492.             $cache_dir  = substr($cache_file, 0, 3);
  493.             $cache_dir  = "files/tmp/cache_query/{$cache_dir}";
  494.             $cache_file = "{$cache_dir}/{$cache_file}";
  495.             w_log("content_mode3_query_dump_file_path", $cache_file);
  496.             if (time() - $cache_time < @filemtime($cache_file)) {
  497.                 $arr = unserialize(file_get_contents($cache_file));
  498.                 w_log("content_mode3_query_dump_is_cache?", "yes");
  499.             } else {
  500.                 if (preg_match("/([+|(])/", $query, $matches)) {
  501.                     if (preg_match("/^(([a-z0-9+]{3,255}))$/isU", $query, $matches)) {
  502.                         if (preg_match("/+/", $query)) {
  503.                             $query_arr = explode("+", $matches['1']);
  504.                         } else {
  505.                             $query_arr[] = $matches['1'];
  506.                         }
  507.                         $query_arr = array_unique($query_arr);
  508.                         $c         = count($query_arr);
  509.                         $i         = 0;
  510.                         foreach ($query_arr as $key) {
  511.                             $q_str .= "`tags` LIKE '%{$key}%'";
  512.                             ++$i;
  513.                             if ($i < $c) {
  514.                                 $q_str .= " and ";
  515.                             }
  516.                         }
  517.                     }
  518.                     unset($matches['0']);
  519.                     $cm = count($matches);
  520.                     $im = 0;
  521.                     foreach ($matches as $keys) {
  522.                         if (preg_match("/[a-z+|s]/i", $keys)) {
  523.                             $query_arr = explode("+", $keys);
  524.                             $q_str_sep = "and";
  525.                         }
  526.                         if (preg_match("/|/", $keys)) {
  527.                             $query_arr = explode("|", $keys);
  528.                             $q_str_sep = "or";
  529.                         }
  530.                         $c = count($query_arr);
  531.                         $i = 0;
  532.                         $q_str .= "(";
  533.                         foreach ($query_arr as $key) {
  534.                             $q_str .= "`tags` LIKE '%{$key}%'";
  535.                             ++$i;
  536.                             if ($i < $c) {
  537.                                 $q_str .= " {$q_str_sep} ";
  538.                             }
  539.                         }
  540.                         $q_str .= ")";
  541.                         ++$im;
  542.                         if ($im < $cm) {
  543.                             $q_str .= " and ";
  544.                         }
  545.                     }
  546.                     $q = mysql_query("select id,ctr from tags where {$q_str} order by ctr desc");
  547.                     while ($r = mysql_fetch_array($q)) {
  548.                         $arr[] = $r;
  549.                     }
  550.                     if (!file_exists("files/tmp/cache_query")) {
  551.                         mkdir("files/tmp/cache_query");
  552.                         chmod("files/tmp/cache_query", 511);
  553.                     }
  554.                     if (!file_exists($cache_dir)) {
  555.                         mkdir($cache_dir);
  556.                         chmod($cache_dir, 511);
  557.                     }
  558.                     if (!empty($arr)) {
  559.                         if (file_exists($cache_file)) {
  560.                             unlink($cache_file);
  561.                         }
  562.                         file_put_contents($cache_file, serialize($arr));
  563.                         $num = count($arr);
  564.                         mysql_query("update `niches` set `num`='{$num}' where `name`='{$warr['n']}'");
  565.                     }
  566.                 }
  567.                 w_log("content_mode3_query_dump_is_cache?", "no");
  568.             }
  569.             $content = array();
  570.             $i       = $limit_start;
  571.             while ($i < $limit_start + $limit) {
  572.                 if (empty($arr[$i])) {
  573.                     break;
  574.                 }
  575.                 $content[] = $arr[$i];
  576.                 ++$i;
  577.             }
  578.             $ids = array();
  579.             foreach ($content as $key) {
  580.                 $ids[] = $key['id'];
  581.             }
  582.             $ids        = implode(",", $ids);
  583.             $sort_order = "order by `ctr` DESC";
  584.             $q_str      = "where id in({$ids}) {$sort_order}";
  585.             return $q_str;
  586.         }
  587.         $q_str = multi_search($niche_keys);
  588.     }
  589.     w_log("content_qet", "start");
  590.     w_log("content_qet_query", $q_str);
  591.     $content = mysql_query("SELECT * FROM `w_content` {$q_str} ");
  592.     w_log("content_qet", "end");
  593.     @$content_num_rows = @mysql_num_rows($content);
  594.     if ($content_num_rows < 1 || !$content_num_rows && $warr['query_mode'] != "full_search") {
  595.         w_log("content_qet_status", "notfound");
  596.         $content          = mysql_query("SELECT * FROM `w_content` where `status`='1' limit {$limit_start},{$limit}");
  597.         $notfound         = true;
  598.         $content_num_rows = mysql_num_rows($content);
  599.     } else {
  600.         w_log("content_qet_status", "found-{$content_num_rows}");
  601.     }
  602.     function tag_title($title, $main_tmpl)
  603.     {
  604.         if (preg_match("/<--TITLE([0-9]{1,3})-->/", $main_tmpl, $matches)) {
  605.             $t         = title_to_len($title, $matches['1']);
  606.             $main_tmpl = preg_replace("/<--TITLE([0-9]{1,3})-->/", $t, $main_tmpl);
  607.         }
  608.         $pattern2 = "/<--TITLE([0-9]{1,3})([_|-]{0,1})(format)?-->/";
  609.         if (preg_match($pattern2, $main_tmpl, $matches)) {
  610.             if (isset($matches['3']) && $matches['3'] == "format") {
  611.                 $title = preg_replace("/[W|s|_|-]/i", " ", $title);
  612.                 preg_match_all("/[a-z]{3,24}/i", $title, $t_matches);
  613.                 $title_arr = $t_matches['0'];
  614.                 array_unique($title_arr);
  615.                 $title = implode(" ", $title_arr);
  616.                 if (strlen($title) < 3) {
  617.                     $title = "free video";
  618.                 }
  619.             }
  620.             $t         = title_to_len($title, $matches['1']);
  621.             $t         = strtolower(str_replace(" ", $matches['2'], $t));
  622.             $main_tmpl = preg_replace($pattern2, $t, $main_tmpl);
  623.         }
  624.         return $main_tmpl;
  625.     }
  626.     if (!empty($id)) {
  627.         w_log("embed_code_get", "start");
  628.         $qt = mysql_query("select * from `w_content` where `id`='{$id}'");
  629.         $rt = mysql_fetch_assoc($qt);
  630.         w_log("embed_code_get", "end");
  631.         w_log("embed_code_parse_tmpl", "start");
  632.         $embed_source = $rt['embed'];
  633.         $main_tmpl    = str_replace("<--EMBED_CODE-->", $embed_source, $main_tmpl);
  634.         $main_tmpl    = str_replace("<--DURATION-->", duration_time($rt['duration']), $main_tmpl);
  635.         $main_tmpl    = tag_title($rt['title'], $main_tmpl);
  636.         $main_tmpl    = str_replace("<--TITLE-->", $rt['title'], $main_tmpl);
  637.         $tags         = str_replace("_", "", $rt['tags']);
  638.         $tags         = explode(" ", $tags);
  639.         $num_tags     = count($tags);
  640.         if (0 < $num_tags) {
  641.             function gen_tags()
  642.             {
  643.                 global $main_tmpl;
  644.                 global $tags;
  645.                 $pattern = "#START_TAGS@(.*)@separator\\=\\'(.*)'\\,max_num\\=\\'([0-9]{0,1})\\'@?END#";
  646.                 preg_match_all("/{$pattern}/i", $main_tmpl, $matches);
  647.                 $n_of_matches = count($matches['0']);
  648.                 $ti           = 0;
  649.                 while ($ti <= $n_of_matches) {
  650.                     preg_match("/{$pattern}/i", $main_tmpl, $matches);
  651.                     if (!isset($tags_arr)) {
  652.                         $i = 0;
  653.                         foreach ($tags as $tag) {
  654.                             $qt = mysql_query("select * from `tags_stats` where `tag`='{$tag}'");
  655.                             $rt = mysql_fetch_assoc($qt);
  656.                             if (!(5 < $rt['num'])) {
  657.                                 continue;
  658.                             }
  659.                             $tags_arr[] = $tag;
  660.                             if (!(9 <= $i)) {
  661.                                 continue;
  662.                             }
  663.                             break;
  664.                             break;
  665.                         }
  666.                     }
  667.                     $tags = $tags_arr;
  668.                     if ($matches['3'] < count($tags)) {
  669.                         $keys = array_map("strlen", array_keys($tags));
  670.                         array_multisort($keys, SORT_DESC, $tags);
  671.                         $tags = array_slice($tags, 0, $matches['3']);
  672.                     }
  673.                     sort($tags);
  674.                     $i     = 0;
  675.                     $t_str = "";
  676.                     foreach ($tags as $tag) {
  677.                         ++$i;
  678.                         $t = str_replace("{tag}", "{$tag}", $matches[1]);
  679.                         $t = str_replace("{tag_path}", "t/{$tag}", $t);
  680.                         $t_str .= "{$t}";
  681.                         if (count($tags) <= $i) {
  682.                             break;
  683.                         }
  684.                         $t_str .= "{$matches['2']}";
  685.                     }
  686.                     $main_tmpl = preg_replace("/{$pattern}/i", $t_str, $main_tmpl, 1);
  687.                     ++$ti;
  688.                 }
  689.                 return $main_tmpl;
  690.             }
  691.             $main_tmpl = @gen_tags();
  692.         }
  693.         w_log("embed_code_parse_tmpl", "end");
  694.     }
  695.     while ($r = mysql_fetch_array($content)) {
  696.         $content_arr[] = $r;
  697.     }
  698.     if (!empty($warr['query_niche']) && empty($rel) && $limit_start == "0") {
  699.         function select_niche_thumb()
  700.         {
  701.             global $content_arr;
  702.             global $warr;
  703.             $ic = mt_rand(1, 3);
  704.             $i  = 0;
  705.             foreach ($content_arr as $arr) {
  706.                 ++$i;
  707.                 if ($i < $ic) {
  708.                     continue;
  709.                 }
  710.                 $thumbs    = unserialize($arr['thumbs']);
  711.                 $th_id     = "10";
  712.                 $arr['at'] = "10";
  713.                 if (3 < count($thumbs)) {
  714.                     $th_id     = "13";
  715.                     $arr['at'] = "13";
  716.                 } else if (1 < count($thumbs)) {
  717.                     $th_id     = "11";
  718.                     $arr['at'] = "11";
  719.                 }
  720.                 if (9 < $arr['at']) {
  721.                     $th_id = substr($r['at'], 0 - 1, 1);
  722.                 }
  723.                 $th_id = substr($arr['at'], 0 - 1, 1);
  724.                 $thumb = $thumbs[$th_id];
  725.                 if (in_array($thumb, $warr['all_niches_thumbs'])) {
  726.                     continue;
  727.                 }
  728.                 $warr['all_niches_thumbs'][] = $thumb;
  729.                 mysql_query("update `niches` set `thumb`='{$thumb}' where `name`='{$warr['query_niche']}'");
  730.                 break;
  731.                 break;
  732.             }
  733.         }
  734.         w_log("opt_select_niche_thumb", "start");
  735.         select_niche_thumb();
  736.         w_log("opt_select_niche_thumb", "end");
  737.     }
  738.     if (!empty($rand)) {
  739.         if (mt_rand(1, 3) == "1") {
  740.             shuffle($content_arr);
  741.         }
  742.     } else {
  743.         function cmp($a, $b)
  744.         {
  745.             if ($a['ctr'] == $b) {
  746.                 return 0;
  747.             }
  748.             return $b['ctr'] < $a['ctr'] ? 0 - 1 : 1;
  749.         }
  750.         @usort($content_arr, "cmp");
  751.         if (mt_rand(1, $rp_max) == "1" && !empty($content_arr)) {
  752.             shuffle($content_arr);
  753.         }
  754.         if ($config['rotation_mode'] == "max_perfomance" && !empty($rel)) {
  755.             shuffle($content_arr);
  756.         }
  757.     }
  758. //=========================================== PAYMENT ========================================//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement