Advertisement
Guest User

Untitled

a guest
Jan 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. ini_set('display_error', 0);
  3. error_reporting(0);
  4. function detect_encoding($string) {
  5.   static $list = array('utf-8', 'windows-1251');
  6.  
  7.   foreach ($list as $item) {
  8.     $sample = iconv($item, $item, $string);
  9.     if (md5($sample) == md5($string))
  10.       return $item;
  11.   }
  12.   return null;
  13. }
  14. class url_helper{
  15.     static $modo_promiscuo = true;
  16.     static $instance;
  17.     static function getInstance(){
  18.         if( !isset(self::$instance) ){
  19.             self::$instance = new self();
  20.         }
  21.         return self::$instance;
  22.     }
  23.     public function clear_url_key( $url_key, $broken_utf=false ){
  24.         $acentos = array(
  25.             '�'=>'a', '�'=>'e', '�'=>'i', '�'=>'o', '�'=>'u', '�'=>'A', '�'=>'E', '�'=>'I', '�'=>'O', '�'=>'U',
  26.             '�'=>'n', '�'=>'N',
  27.             '�'=>'a', '�'=>'e', '�'=>'i', '�'=>'o', '�'=>'u', '�'=>'A', '�'=>'E', '�'=>'I', '�'=>'O', '�'=>'U',
  28.             '�'=>'a', '�'=>'e', '�'=>'i', '�'=>'o', '�'=>'u', '�'=>'A', '�'=>'E', '�'=>'I', '�'=>'O', '�'=>'U',
  29.             '�'=>'a', '�'=>'e', '�'=>'i', '�'=>'o', '�'=>'u', '�'=>'A', '�'=>'E', '�'=>'I', '�'=>'O', '�'=>'U',
  30.         );
  31.         $replace_pairs = array();
  32. //      if($broken_utf)
  33. //          $url_key = utf8_decode($url_key);
  34.         $url_key = strtolower($url_key);
  35.         if(!$broken_utf || true){  
  36.             foreach( $acentos as $acento=>$reemplazo ){
  37.                 $replace_pairs[ utf8_encode($acento) ] = $reemplazo;
  38.             }
  39.         }
  40.         else{
  41.             //$url_key = utf8_encode($url_key);
  42.             foreach( $acentos as $acento=>$reemplazo ){
  43.                 $replace_pairs[ utf8_encode($acento) ] = $reemplazo;
  44.             }
  45.             foreach( $acentos as $acento=>$reemplazo ){
  46.                 $replace_pairs[ utf8_decode($acento) ] = $reemplazo;
  47.             }
  48.         }
  49.         foreach( $acentos as $acento=>$reemplazo ){
  50.             $replace_pairs[ $acento ] = $reemplazo;
  51.         }
  52.         global $contador;
  53.         if($broken_utf){
  54.             if(!isset($contador)){
  55.                 header('content-type:text/plain');
  56.                 $contador = 0;
  57.             }
  58.             $new_url_key = strtr( $url_key,  $replace_pairs );
  59.             //EUC-JP
  60.             if($contador++==3){
  61.                 $list = mb_list_encodings();
  62.                 $list = array_diff($list, array('UTF-8'));
  63.                 //var_dump($list);
  64. //              var_dump(array(
  65. //              mb_detect_encoding('�', $list),
  66. //              mb_detect_encoding(utf8_encode('�'), $list, true),
  67. //              mb_detect_encoding(substr($url_key, 4, 2), $list, true),
  68. //              ));
  69. //              $caracter_raro = substr($url_key, 4, 2);
  70. //              $str = mb_convert_encoding($url_key, "UTF-8", "EUC-JP");
  71. //              var_dump($str, mb_convert_encoding(utf8_encode('diet�tica'), "EUC-JP", "UTF-8"));
  72.                 echo $url_key."\n\n";
  73. //              foreach($list as $charset){
  74. //                  echo $charset . ' = ' . mb_convert_encoding(utf8_decode('diet�tica'), $charset, "UTF-8") . "\n";
  75. //              }
  76. //              $chars = /*'�'.utf8_encode('�').*/'�' . utf8_encode('�') . substr($url_key, 4, 2) ;
  77. //              var_dump(ord($caracter_raro[0])& ~0x2);
  78. //              echo "\n";
  79. //              var_dump($chars);
  80. //              for($i=0;$i<strlen($chars);$i++){
  81. //                  var_dump($chars[$i], dechex(ord($chars[$i])));
  82. //              }
  83. //              echo "\n";
  84. //              var_dump('�',utf8_encode('�'), strtolower(utf8_encode('�')));
  85. //              var_dump($url_key, $new_url_key, utf8_decode($url_key));
  86.                 die(__FILE__.__LINE__);
  87.             }
  88.         }
  89.  
  90.        
  91.         $url_key = strtr( $url_key,  $replace_pairs );
  92.         $url_key = preg_replace('/-?[^A-Za-z0-9-]+/', '-', $url_key );
  93.         $url_key = preg_replace('/-+/', '-', $url_key);
  94.         return preg_replace('/^-|-$/', '', $url_key);
  95.     }
  96.     public function check_generate( $url_key, $id = null, $table = 'product' ){
  97.         $sql = 'select count(*) as cantidad from '.$table.' where url_key = \''.mysql_real_escape_string($url_key).'\' ' . (isset($id)?' and id!='.intval($id):'');
  98.         if( $res = mysql_query($sql) ){
  99.             if( $fila = mysql_fetch_object($res) ){
  100.                 if( $fila->cantidad>0 ){
  101.                     $sql = 'select url_key from '.$table.' where url_key regexp \'^' . $url_key . '-[0-9]+\$\' ' . (isset($id)?' and id!='.intval($id):'');
  102.                     $res2 = mysql_query($sql);
  103.                     $taken = array();
  104.                     while( $fila = mysql_fetch_object($res2) ){
  105. //                      if( strpos($url_key, 'gerimax')===0)
  106. //                          var_dump($fila->url_key);
  107.                         if( preg_match('/^'.$url_key.'-(?P<taken>[0-9]+)\$/', $fila->url_key, $matches) ){
  108.                             $taken[ $matches['taken'] ] = true;
  109.                         }
  110.                     }
  111.                     mysql_free_result($res2);
  112.                     for($i=1; $i<1000; $i++){
  113.                         if( !isset($taken[$i]) ){
  114.                             $url_key = $url_key . '-' . $i;
  115.                             break;
  116.                         }
  117.                     }
  118.                 }
  119.                 return $url_key;
  120.             }
  121.             mysql_free_result($res);
  122.         }
  123.         return false;
  124.     }
  125.     public function auto_gen( $table='product', $broken_utf=false ){
  126.         $sql = 'select id,url_key from '.$table.'';
  127.         //var_dump($sql);
  128.         $res2 = mysql_query($sql);
  129.         $taken = array();
  130.         while( $fila = mysql_fetch_object($res2) ){
  131.             $url_key = $this->clear_url_key( $fila->url_key, $broken_utf );
  132.             $new_url_key = $this->check_generate( $url_key, $fila->id, $table );
  133. //          echo '<br />';
  134. //          var_dump($new_url_key , $url_key);
  135.             $sql = 'update ' . $table . ' set url_key = \'' . $new_url_key . '\' where id=\''.$fila->id.'\'';
  136.             mysql_query($sql);
  137.         }
  138.         mysql_free_result($res2);
  139.     }
  140.     public function auto_gen_all(){
  141.         $each = array(
  142.             'section'=>array('field'=>'title','broken_utf'=>false),
  143.             'product'=>array('field'=>'title','broken_utf'=>false),
  144.             'categories'=>array('field'=>'name','broken_utf'=>false),
  145.         );
  146.         foreach( $each as $table=>$data ){
  147.             mysql_query('update `'.$table.'` set `url_key` = `'.$data['field'].'`;');
  148.             $this->auto_gen( $table, strtolower($data['broken_utf']) );
  149.         }
  150. //      $bt = debug_backtrace();
  151. //      foreach($bt as $bti){
  152. //          echo $bti['file'].$bti['line']."\n";
  153. //      }
  154.         header('location: ./index.php');
  155.         die();
  156.     }
  157.     public function regenerar_cache_urls(){
  158.         $variables = array();
  159.         $hashed = array();
  160.         foreach( array('product'=>'title', 'categories'=>'name', 'section'=>'title') as $table=>$field ){
  161.             $this->regenerar_cache_urls_tabla( $table, $variables, $hashed );
  162.         }
  163.         header('content-type:text/plain');
  164.         //var_dump($variables);
  165.         $cache = var_export( $variables, true);
  166.         file_put_contents( dirname(__FILE__).'/cache_urls.php', '<?php global $cache_urls; $cache_urls = '.$cache.'; ?>' );
  167.         $hashed = var_export( $hashed, true);
  168.         file_put_contents( dirname(__FILE__).'/hashed_cache_urls.php', '<?php global $hashed_cache_urls; $hashed_cache_urls = '.$hashed.'; ?>' );
  169.         header('location: ./index.php');
  170.         die();
  171.     }
  172.     public function regenerar_cache_urls_tabla( $table, &$variables, &$hashed ){
  173.         $sql = 'select id,url_key from '.$table;
  174.         $res2 = mysql_query($sql);
  175.         $taken = array();
  176.         while( $fila = mysql_fetch_object($res2) ){
  177.             $variables[$table][$fila->url_key] = $fila->id . '-' . $fila->url_key;
  178.             $hashed[$table][$fila->id] = $fila->url_key;
  179.         }
  180.         mysql_free_result($res2);
  181.     }
  182.     public function loadCacheUrls(){
  183.         include_once(dirname(__FILE__).'/cache_urls.php');
  184.     }
  185.     public function translateFrontendUrl(){
  186.         //var_dump($_SERVER["REQUEST_URI"]);
  187.         //header('content-type:text/plain');
  188.         if(isset($_ENV["REQUEST_URI"]))
  189.             $url = $_ENV["REQUEST_URI"];
  190.         else
  191.             $url = $_SERVER["REQUEST_URI"];
  192.         //var_dump($url);
  193.         global $cache_urls;
  194.         // Quito el .html del final de la url para procesarla, salvo si es secci�n "comprar".
  195.         if((strpos($url,'comprar')) === FALSE){
  196.             if((strpos($url,'.html')) !== FALSE){
  197.                 $url = substr($url,0,-5);
  198.             }
  199.         }
  200.         if(preg_match('/^(?P<start_url>.*?)\\/(?P<identifier>(product)|(category)?)\\/(?P<section_key>.+?)(\\/(?P<category_key>.+?))?(\\/(?P<product_key>.+?))?\\/?$/', $url, $matches)){
  201. //          echo "si machea\n";
  202. //          var_dump($matches['section_key'], $matches['category_key'], $matches['product_key']);
  203. //var_dump($matches['identifier']);
  204. //die();
  205.             if(in_array($matches['identifier'], array('product','category'))){
  206.                
  207.                 $this->loadCacheUrls();
  208.                 $change = false;
  209.                 $section = $matches['section_key'];
  210.                 $category = $matches['category_key'];
  211.                 $product = $matches['product_key'];
  212.                 //var_dump($section, $category, $product);
  213.                 if(isset($matches['section_key'])){
  214.                     if(isset($cache_urls['section'][$section])){
  215.                         $section = $cache_urls['section'][$section];
  216.                     }
  217.                 }
  218.                 if(isset($matches['category_key'])){
  219.                     if(isset($cache_urls['categories'][$category])){
  220.                         $category = $cache_urls['categories'][$category];
  221.                     }
  222.                 }
  223.                 if(isset($matches['product_key'])){
  224.                     if(isset($cache_urls['product'][$product])){
  225.                         $product = $cache_urls['product'][$product];
  226.                     }
  227.                 }
  228.                 //var_dump($section, $category, $product);
  229.                 $new_url = $matches['start_url'] . '/'.$matches['identifier'].'/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
  230.                 //var_dump($new_url, $url);
  231.                 $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $new_url;
  232.             }
  233.         }
  234.         elseif( self::$modo_promiscuo && preg_match('/^(?P<start_url>.*?)\\/(?P<web_part>web\\/)?(?P<section_key>.+?)(\\/(?P<category_key>.+?))?(\\/(?P<product_key>.+?))?\\/?$/', $url, $matches)){
  235.             //header('content-type:text/plain');
  236.             $this->loadCacheUrls();
  237.             $change = false;
  238.             $section = $matches['section_key'];
  239.             $category = $matches['category_key'];
  240.             $product = $matches['product_key'];
  241.             $error = false;
  242.             //var_dump($section, $category, $product);
  243.             if(isset($matches['section_key'])){
  244.                 if(isset($cache_urls['section'][$section])){
  245.                     $section = $cache_urls['section'][$section];
  246.                 }
  247.                 else{
  248.                     $error = true;
  249.                 }
  250.             }
  251.             if(isset($matches['category_key'])){
  252.                 if(isset($cache_urls['categories'][$category])){
  253.                     $category = $cache_urls['categories'][$category];
  254.                 }
  255.                 else{
  256.                     $error = true;
  257.                 }
  258.             }
  259.             if(isset($matches['product_key'])){
  260.                 if(isset($cache_urls['product'][$product])){
  261.                     $product = $cache_urls['product'][$product];
  262.                 }
  263.                 else{
  264.                     $error = true;
  265.                 }
  266.             }
  267.             if( !$error ){
  268.     //          var_dump($error);
  269.                 //var_dump($section, $category, $product);
  270.                 $identifier = $category || $product ? 'product' : 'category';
  271.                 $new_url = $matches['start_url'] . '/'.$matches['web_part'].$identifier.'/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
  272.                 //var_dump($new_url, $url);
  273.                 $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $new_url;
  274.    
  275.     //          var_dump($matches);
  276.     //          var_dump($new_url);
  277.     //          die();
  278.             }
  279.             else{
  280.                 $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $url;
  281.             }
  282.         }
  283.         else{
  284.             $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $url;
  285.         }
  286.        
  287.         //die(__FILE__.__LINE__);
  288.        
  289.         //$_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = '/farmacia_virtual/web/product/6-medicina-natural/46-homeopat�a/96-sdfsdf';
  290.     }
  291.     public function translateFrontendUrlByParam($url){
  292.         global $cache_urls;
  293.         // Quito el .html del final de la url para procesarla, salvo si es secci�n "comprar".
  294.         if((strpos($url,'comprar')) === FALSE){
  295.             if((strpos($url,'.html')) !== FALSE){
  296.                 $url = substr($url,0,-5);
  297.             }
  298.         }
  299.         if(preg_match('/^(?P<start_url>.*?)\\/(?P<identifier>(product)|(category)?)\\/(?P<section_key>.+?)(\\/(?P<category_key>.+?))?(\\/(?P<product_key>.+?))?\\/?$/', $url, $matches)){
  300. //          echo "si machea\n";
  301. //          var_dump($matches['section_key'], $matches['category_key'], $matches['product_key']);
  302. //var_dump($matches['identifier']);
  303. //die();
  304.             if(in_array($matches['identifier'], array('product','category'))){
  305.  
  306.                 $this->loadCacheUrls();
  307.                 $change = false;
  308.                 $section = $matches['section_key'];
  309.                 $category = $matches['category_key'];
  310.                 $product = $matches['product_key'];
  311.                 //var_dump($section, $category, $product);
  312.                 if(isset($matches['section_key'])){
  313.                     if(isset($cache_urls['section'][$section])){
  314.                         $section = $cache_urls['section'][$section];
  315.                     }
  316.                 }
  317.                 if(isset($matches['category_key'])){
  318.                     if(isset($cache_urls['categories'][$category])){
  319.                         $category = $cache_urls['categories'][$category];
  320.                     }
  321.                 }
  322.                 if(isset($matches['product_key'])){
  323.                     if(isset($cache_urls['product'][$product])){
  324.                         $product = $cache_urls['product'][$product];
  325.                     }
  326.                 }
  327.                 //var_dump($section, $category, $product);
  328.                 $new_url = $matches['start_url'] . '/'.$matches['identifier'].'/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
  329.                 var_dump($new_url, $url);die('llega');
  330.  
  331.             }
  332.         }
  333.         elseif( self::$modo_promiscuo && preg_match('/^(?P<start_url>.*?)\\/(?P<web_part>web\\/)?(?P<section_key>.+?)(\\/(?P<category_key>.+?))?(\\/(?P<product_key>.+?))?\\/?$/', $url, $matches)){
  334.             //header('content-type:text/plain');
  335.             $this->loadCacheUrls();
  336.             $change = false;
  337.             $section = $matches['section_key'];
  338.             $category = $matches['category_key'];
  339.             $product = $matches['product_key'];
  340.             $error = false;
  341.             //var_dump($section, $category, $product);
  342.             if(isset($matches['section_key'])){
  343.                 if(isset($cache_urls['section'][$section])){
  344.                     $section = $cache_urls['section'][$section];
  345.                 }
  346.                 else{
  347.                     $error = true;
  348.                 }
  349.             }
  350.             if(isset($matches['category_key'])){
  351.                 if(isset($cache_urls['categories'][$category])){
  352.                     $category = $cache_urls['categories'][$category];
  353.                 }
  354.                 else{
  355.                     $error = true;
  356.                 }
  357.             }
  358.             if(isset($matches['product_key'])){
  359.                 if(isset($cache_urls['product'][$product])){
  360.                     $product = $cache_urls['product'][$product];
  361.                 }
  362.                 else{
  363.                     $error = true;
  364.                 }
  365.             }
  366.             if( !$error ){
  367.                 //          var_dump($error);
  368.                 //var_dump($section, $category, $product);
  369.                 $identifier = $category || $product ? 'product' : 'category';
  370.                 $new_url = $matches['start_url'] . '/'.$matches['web_part'].$identifier.'/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
  371.                 //var_dump($new_url, $url);
  372.                 $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $new_url;
  373.  
  374.                 //          var_dump($matches);
  375.                             var_dump($new_url);
  376.                             die('llega1');
  377.             }
  378.             else{
  379.                 var_dump($url);die('llega2');
  380.                 $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $url;
  381.             }
  382.         }
  383.         else{
  384.             var_dump($url);die('llega3');
  385.             $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $url;
  386.         }
  387.  
  388.         //die(__FILE__.__LINE__);
  389.  
  390.         //$_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = '/farmacia_virtual/web/product/6-medicina-natural/46-homeopat�a/96-sdfsdf';
  391.     }
  392.  
  393.     public function loadHashedCacheUrls(){
  394.         include_once(dirname(__FILE__).'/hashed_cache_urls.php');
  395.     }
  396.     public function hookFrontendUrlGen( $url ){
  397.         //var_dump($_SERVER["REQUEST_URI"]);
  398.         //header('content-type:text/plain');
  399.         //var_dump($url);
  400.         global $hashed_cache_urls;
  401.         if(preg_match('/^(?P<start_url>.*?)\\/web\\/(?P<sub_url>.*?\\/)?(?P<identifier>(product)|(category)?)\\/(?P<section_key>.+?)(\\/(?P<category_key>.+?))?(\\/(?P<product_key>.+?))?\\/?$/', $url, $matches)){
  402. //          echo "si machea\n";
  403. //          var_dump($matches['section_key'], $matches['category_key'], $matches['product_key']);
  404. //var_dump($matches);
  405. //die();
  406.             if(in_array($matches['identifier'], array('product','category'))){
  407.                
  408.                 $this->loadHashedCacheUrls();
  409.                 $change = false;
  410.                 $section = strpos($matches['section_key'], '-')!==false?array_shift(explode('-', $matches['section_key'])):null;
  411.                 $category = strpos($matches['category_key'], '-')!==false?array_shift(explode('-', $matches['category_key'])):null;
  412.                 $product = strpos($matches['product_key'], '-')!==false?array_shift(explode('-', $matches['product_key'])):null;
  413.                 //var_dump($section, $category, $product);
  414.                 if(isset($matches['section_key'])){
  415.                     if(isset($hashed_cache_urls['section'][$section])){
  416.                         $section = $hashed_cache_urls['section'][$section];
  417.                     }
  418.                 }
  419.                 if(isset($matches['category_key'])){
  420.                     if(isset($hashed_cache_urls['categories'][$category])){
  421.                         $category = $hashed_cache_urls['categories'][$category];
  422.                     }
  423.                 }
  424.                 if(isset($matches['product_key'])){
  425.                     if(isset($hashed_cache_urls['product'][$product])){
  426.                         $product = $hashed_cache_urls['product'][$product];
  427.                     }
  428.                 }
  429.                 //var_dump($section, $category, $product);
  430.                 if( self::$modo_promiscuo ){
  431.                     $new_url = $matches['start_url'] . @$matches['sub_url'] . '/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
  432.                 }
  433.                 else {
  434.                 $new_url = $matches['start_url'] . @$matches['sub_url'] . '/'.$matches['identifier'].'/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
  435.                 }
  436.                 $url = $new_url;
  437. //              var_dump($new_url, $url);
  438. //              $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $new_url;
  439.             }
  440. //          die(__FILE__.__LINE__);
  441.         }
  442. //      die(__FILE__.__LINE__);
  443.        
  444.         //$_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = '/farmacia_virtual/web/product/6-medicina-natural/46-homeopat�a/96-sdfsdf';
  445.         // Agrego .html a la url, excepto a la secci�n "comprar".
  446.         if((strpos($url,'tufarmaciavirtual.com/comprar')) !== FALSE){
  447.             return $url;
  448.         }else if((strpos($url,'tufarmaciavirtual.com/resultados')) !== FALSE){
  449.             return $url;
  450.         }else if((strpos($url,'tufarmaciavirtual.com/web/')) !== FALSE){
  451.             return $url;
  452.         }else if($url == '/web/'){
  453.             return substr($url,0,-4);;
  454.         }else{
  455.             return $url.'.html';
  456.         }
  457.     }
  458. }
  459. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement