Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ini_set('display_error', 0);
- error_reporting(0);
- function detect_encoding($string) {
- static $list = array('utf-8', 'windows-1251');
- foreach ($list as $item) {
- $sample = iconv($item, $item, $string);
- if (md5($sample) == md5($string))
- return $item;
- }
- return null;
- }
- class url_helper{
- static $modo_promiscuo = true;
- static $instance;
- static function getInstance(){
- if( !isset(self::$instance) ){
- self::$instance = new self();
- }
- return self::$instance;
- }
- public function clear_url_key( $url_key, $broken_utf=false ){
- $acentos = array(
- '�'=>'a', '�'=>'e', '�'=>'i', '�'=>'o', '�'=>'u', '�'=>'A', '�'=>'E', '�'=>'I', '�'=>'O', '�'=>'U',
- '�'=>'n', '�'=>'N',
- '�'=>'a', '�'=>'e', '�'=>'i', '�'=>'o', '�'=>'u', '�'=>'A', '�'=>'E', '�'=>'I', '�'=>'O', '�'=>'U',
- '�'=>'a', '�'=>'e', '�'=>'i', '�'=>'o', '�'=>'u', '�'=>'A', '�'=>'E', '�'=>'I', '�'=>'O', '�'=>'U',
- '�'=>'a', '�'=>'e', '�'=>'i', '�'=>'o', '�'=>'u', '�'=>'A', '�'=>'E', '�'=>'I', '�'=>'O', '�'=>'U',
- );
- $replace_pairs = array();
- // if($broken_utf)
- // $url_key = utf8_decode($url_key);
- $url_key = strtolower($url_key);
- if(!$broken_utf || true){
- foreach( $acentos as $acento=>$reemplazo ){
- $replace_pairs[ utf8_encode($acento) ] = $reemplazo;
- }
- }
- else{
- //$url_key = utf8_encode($url_key);
- foreach( $acentos as $acento=>$reemplazo ){
- $replace_pairs[ utf8_encode($acento) ] = $reemplazo;
- }
- foreach( $acentos as $acento=>$reemplazo ){
- $replace_pairs[ utf8_decode($acento) ] = $reemplazo;
- }
- }
- foreach( $acentos as $acento=>$reemplazo ){
- $replace_pairs[ $acento ] = $reemplazo;
- }
- global $contador;
- if($broken_utf){
- if(!isset($contador)){
- header('content-type:text/plain');
- $contador = 0;
- }
- $new_url_key = strtr( $url_key, $replace_pairs );
- //EUC-JP
- if($contador++==3){
- $list = mb_list_encodings();
- $list = array_diff($list, array('UTF-8'));
- //var_dump($list);
- // var_dump(array(
- // mb_detect_encoding('�', $list),
- // mb_detect_encoding(utf8_encode('�'), $list, true),
- // mb_detect_encoding(substr($url_key, 4, 2), $list, true),
- // ));
- // $caracter_raro = substr($url_key, 4, 2);
- // $str = mb_convert_encoding($url_key, "UTF-8", "EUC-JP");
- // var_dump($str, mb_convert_encoding(utf8_encode('diet�tica'), "EUC-JP", "UTF-8"));
- echo $url_key."\n\n";
- // foreach($list as $charset){
- // echo $charset . ' = ' . mb_convert_encoding(utf8_decode('diet�tica'), $charset, "UTF-8") . "\n";
- // }
- // $chars = /*'�'.utf8_encode('�').*/'�' . utf8_encode('�') . substr($url_key, 4, 2) ;
- // var_dump(ord($caracter_raro[0])& ~0x2);
- // echo "\n";
- // var_dump($chars);
- // for($i=0;$i<strlen($chars);$i++){
- // var_dump($chars[$i], dechex(ord($chars[$i])));
- // }
- // echo "\n";
- // var_dump('�',utf8_encode('�'), strtolower(utf8_encode('�')));
- // var_dump($url_key, $new_url_key, utf8_decode($url_key));
- die(__FILE__.__LINE__);
- }
- }
- $url_key = strtr( $url_key, $replace_pairs );
- $url_key = preg_replace('/-?[^A-Za-z0-9-]+/', '-', $url_key );
- $url_key = preg_replace('/-+/', '-', $url_key);
- return preg_replace('/^-|-$/', '', $url_key);
- }
- public function check_generate( $url_key, $id = null, $table = 'product' ){
- $sql = 'select count(*) as cantidad from '.$table.' where url_key = \''.mysql_real_escape_string($url_key).'\' ' . (isset($id)?' and id!='.intval($id):'');
- if( $res = mysql_query($sql) ){
- if( $fila = mysql_fetch_object($res) ){
- if( $fila->cantidad>0 ){
- $sql = 'select url_key from '.$table.' where url_key regexp \'^' . $url_key . '-[0-9]+\$\' ' . (isset($id)?' and id!='.intval($id):'');
- $res2 = mysql_query($sql);
- $taken = array();
- while( $fila = mysql_fetch_object($res2) ){
- // if( strpos($url_key, 'gerimax')===0)
- // var_dump($fila->url_key);
- if( preg_match('/^'.$url_key.'-(?P<taken>[0-9]+)\$/', $fila->url_key, $matches) ){
- $taken[ $matches['taken'] ] = true;
- }
- }
- mysql_free_result($res2);
- for($i=1; $i<1000; $i++){
- if( !isset($taken[$i]) ){
- $url_key = $url_key . '-' . $i;
- break;
- }
- }
- }
- return $url_key;
- }
- mysql_free_result($res);
- }
- return false;
- }
- public function auto_gen( $table='product', $broken_utf=false ){
- $sql = 'select id,url_key from '.$table.'';
- //var_dump($sql);
- $res2 = mysql_query($sql);
- $taken = array();
- while( $fila = mysql_fetch_object($res2) ){
- $url_key = $this->clear_url_key( $fila->url_key, $broken_utf );
- $new_url_key = $this->check_generate( $url_key, $fila->id, $table );
- // echo '<br />';
- // var_dump($new_url_key , $url_key);
- $sql = 'update ' . $table . ' set url_key = \'' . $new_url_key . '\' where id=\''.$fila->id.'\'';
- mysql_query($sql);
- }
- mysql_free_result($res2);
- }
- public function auto_gen_all(){
- $each = array(
- 'section'=>array('field'=>'title','broken_utf'=>false),
- 'product'=>array('field'=>'title','broken_utf'=>false),
- 'categories'=>array('field'=>'name','broken_utf'=>false),
- );
- foreach( $each as $table=>$data ){
- mysql_query('update `'.$table.'` set `url_key` = `'.$data['field'].'`;');
- $this->auto_gen( $table, strtolower($data['broken_utf']) );
- }
- // $bt = debug_backtrace();
- // foreach($bt as $bti){
- // echo $bti['file'].$bti['line']."\n";
- // }
- header('location: ./index.php');
- die();
- }
- public function regenerar_cache_urls(){
- $variables = array();
- $hashed = array();
- foreach( array('product'=>'title', 'categories'=>'name', 'section'=>'title') as $table=>$field ){
- $this->regenerar_cache_urls_tabla( $table, $variables, $hashed );
- }
- header('content-type:text/plain');
- //var_dump($variables);
- $cache = var_export( $variables, true);
- file_put_contents( dirname(__FILE__).'/cache_urls.php', '<?php global $cache_urls; $cache_urls = '.$cache.'; ?>' );
- $hashed = var_export( $hashed, true);
- file_put_contents( dirname(__FILE__).'/hashed_cache_urls.php', '<?php global $hashed_cache_urls; $hashed_cache_urls = '.$hashed.'; ?>' );
- header('location: ./index.php');
- die();
- }
- public function regenerar_cache_urls_tabla( $table, &$variables, &$hashed ){
- $sql = 'select id,url_key from '.$table;
- $res2 = mysql_query($sql);
- $taken = array();
- while( $fila = mysql_fetch_object($res2) ){
- $variables[$table][$fila->url_key] = $fila->id . '-' . $fila->url_key;
- $hashed[$table][$fila->id] = $fila->url_key;
- }
- mysql_free_result($res2);
- }
- public function loadCacheUrls(){
- include_once(dirname(__FILE__).'/cache_urls.php');
- }
- public function translateFrontendUrl(){
- //var_dump($_SERVER["REQUEST_URI"]);
- //header('content-type:text/plain');
- if(isset($_ENV["REQUEST_URI"]))
- $url = $_ENV["REQUEST_URI"];
- else
- $url = $_SERVER["REQUEST_URI"];
- //var_dump($url);
- global $cache_urls;
- // Quito el .html del final de la url para procesarla, salvo si es secci�n "comprar".
- if((strpos($url,'comprar')) === FALSE){
- if((strpos($url,'.html')) !== FALSE){
- $url = substr($url,0,-5);
- }
- }
- if(preg_match('/^(?P<start_url>.*?)\\/(?P<identifier>(product)|(category)?)\\/(?P<section_key>.+?)(\\/(?P<category_key>.+?))?(\\/(?P<product_key>.+?))?\\/?$/', $url, $matches)){
- // echo "si machea\n";
- // var_dump($matches['section_key'], $matches['category_key'], $matches['product_key']);
- //var_dump($matches['identifier']);
- //die();
- if(in_array($matches['identifier'], array('product','category'))){
- $this->loadCacheUrls();
- $change = false;
- $section = $matches['section_key'];
- $category = $matches['category_key'];
- $product = $matches['product_key'];
- //var_dump($section, $category, $product);
- if(isset($matches['section_key'])){
- if(isset($cache_urls['section'][$section])){
- $section = $cache_urls['section'][$section];
- }
- }
- if(isset($matches['category_key'])){
- if(isset($cache_urls['categories'][$category])){
- $category = $cache_urls['categories'][$category];
- }
- }
- if(isset($matches['product_key'])){
- if(isset($cache_urls['product'][$product])){
- $product = $cache_urls['product'][$product];
- }
- }
- //var_dump($section, $category, $product);
- $new_url = $matches['start_url'] . '/'.$matches['identifier'].'/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
- //var_dump($new_url, $url);
- $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $new_url;
- }
- }
- elseif( self::$modo_promiscuo && preg_match('/^(?P<start_url>.*?)\\/(?P<web_part>web\\/)?(?P<section_key>.+?)(\\/(?P<category_key>.+?))?(\\/(?P<product_key>.+?))?\\/?$/', $url, $matches)){
- //header('content-type:text/plain');
- $this->loadCacheUrls();
- $change = false;
- $section = $matches['section_key'];
- $category = $matches['category_key'];
- $product = $matches['product_key'];
- $error = false;
- //var_dump($section, $category, $product);
- if(isset($matches['section_key'])){
- if(isset($cache_urls['section'][$section])){
- $section = $cache_urls['section'][$section];
- }
- else{
- $error = true;
- }
- }
- if(isset($matches['category_key'])){
- if(isset($cache_urls['categories'][$category])){
- $category = $cache_urls['categories'][$category];
- }
- else{
- $error = true;
- }
- }
- if(isset($matches['product_key'])){
- if(isset($cache_urls['product'][$product])){
- $product = $cache_urls['product'][$product];
- }
- else{
- $error = true;
- }
- }
- if( !$error ){
- // var_dump($error);
- //var_dump($section, $category, $product);
- $identifier = $category || $product ? 'product' : 'category';
- $new_url = $matches['start_url'] . '/'.$matches['web_part'].$identifier.'/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
- //var_dump($new_url, $url);
- $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $new_url;
- // var_dump($matches);
- // var_dump($new_url);
- // die();
- }
- else{
- $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $url;
- }
- }
- else{
- $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $url;
- }
- //die(__FILE__.__LINE__);
- //$_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = '/farmacia_virtual/web/product/6-medicina-natural/46-homeopat�a/96-sdfsdf';
- }
- public function translateFrontendUrlByParam($url){
- global $cache_urls;
- // Quito el .html del final de la url para procesarla, salvo si es secci�n "comprar".
- if((strpos($url,'comprar')) === FALSE){
- if((strpos($url,'.html')) !== FALSE){
- $url = substr($url,0,-5);
- }
- }
- if(preg_match('/^(?P<start_url>.*?)\\/(?P<identifier>(product)|(category)?)\\/(?P<section_key>.+?)(\\/(?P<category_key>.+?))?(\\/(?P<product_key>.+?))?\\/?$/', $url, $matches)){
- // echo "si machea\n";
- // var_dump($matches['section_key'], $matches['category_key'], $matches['product_key']);
- //var_dump($matches['identifier']);
- //die();
- if(in_array($matches['identifier'], array('product','category'))){
- $this->loadCacheUrls();
- $change = false;
- $section = $matches['section_key'];
- $category = $matches['category_key'];
- $product = $matches['product_key'];
- //var_dump($section, $category, $product);
- if(isset($matches['section_key'])){
- if(isset($cache_urls['section'][$section])){
- $section = $cache_urls['section'][$section];
- }
- }
- if(isset($matches['category_key'])){
- if(isset($cache_urls['categories'][$category])){
- $category = $cache_urls['categories'][$category];
- }
- }
- if(isset($matches['product_key'])){
- if(isset($cache_urls['product'][$product])){
- $product = $cache_urls['product'][$product];
- }
- }
- //var_dump($section, $category, $product);
- $new_url = $matches['start_url'] . '/'.$matches['identifier'].'/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
- var_dump($new_url, $url);die('llega');
- }
- }
- elseif( self::$modo_promiscuo && preg_match('/^(?P<start_url>.*?)\\/(?P<web_part>web\\/)?(?P<section_key>.+?)(\\/(?P<category_key>.+?))?(\\/(?P<product_key>.+?))?\\/?$/', $url, $matches)){
- //header('content-type:text/plain');
- $this->loadCacheUrls();
- $change = false;
- $section = $matches['section_key'];
- $category = $matches['category_key'];
- $product = $matches['product_key'];
- $error = false;
- //var_dump($section, $category, $product);
- if(isset($matches['section_key'])){
- if(isset($cache_urls['section'][$section])){
- $section = $cache_urls['section'][$section];
- }
- else{
- $error = true;
- }
- }
- if(isset($matches['category_key'])){
- if(isset($cache_urls['categories'][$category])){
- $category = $cache_urls['categories'][$category];
- }
- else{
- $error = true;
- }
- }
- if(isset($matches['product_key'])){
- if(isset($cache_urls['product'][$product])){
- $product = $cache_urls['product'][$product];
- }
- else{
- $error = true;
- }
- }
- if( !$error ){
- // var_dump($error);
- //var_dump($section, $category, $product);
- $identifier = $category || $product ? 'product' : 'category';
- $new_url = $matches['start_url'] . '/'.$matches['web_part'].$identifier.'/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
- //var_dump($new_url, $url);
- $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $new_url;
- // var_dump($matches);
- var_dump($new_url);
- die('llega1');
- }
- else{
- var_dump($url);die('llega2');
- $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $url;
- }
- }
- else{
- var_dump($url);die('llega3');
- $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $url;
- }
- //die(__FILE__.__LINE__);
- //$_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = '/farmacia_virtual/web/product/6-medicina-natural/46-homeopat�a/96-sdfsdf';
- }
- public function loadHashedCacheUrls(){
- include_once(dirname(__FILE__).'/hashed_cache_urls.php');
- }
- public function hookFrontendUrlGen( $url ){
- //var_dump($_SERVER["REQUEST_URI"]);
- //header('content-type:text/plain');
- //var_dump($url);
- global $hashed_cache_urls;
- 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)){
- // echo "si machea\n";
- // var_dump($matches['section_key'], $matches['category_key'], $matches['product_key']);
- //var_dump($matches);
- //die();
- if(in_array($matches['identifier'], array('product','category'))){
- $this->loadHashedCacheUrls();
- $change = false;
- $section = strpos($matches['section_key'], '-')!==false?array_shift(explode('-', $matches['section_key'])):null;
- $category = strpos($matches['category_key'], '-')!==false?array_shift(explode('-', $matches['category_key'])):null;
- $product = strpos($matches['product_key'], '-')!==false?array_shift(explode('-', $matches['product_key'])):null;
- //var_dump($section, $category, $product);
- if(isset($matches['section_key'])){
- if(isset($hashed_cache_urls['section'][$section])){
- $section = $hashed_cache_urls['section'][$section];
- }
- }
- if(isset($matches['category_key'])){
- if(isset($hashed_cache_urls['categories'][$category])){
- $category = $hashed_cache_urls['categories'][$category];
- }
- }
- if(isset($matches['product_key'])){
- if(isset($hashed_cache_urls['product'][$product])){
- $product = $hashed_cache_urls['product'][$product];
- }
- }
- //var_dump($section, $category, $product);
- if( self::$modo_promiscuo ){
- $new_url = $matches['start_url'] . @$matches['sub_url'] . '/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
- }
- else {
- $new_url = $matches['start_url'] . @$matches['sub_url'] . '/'.$matches['identifier'].'/' . $section . ( $category?'/' . $category . ($product?'/' . $product:''):'');
- }
- $url = $new_url;
- // var_dump($new_url, $url);
- // $_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = $new_url;
- }
- // die(__FILE__.__LINE__);
- }
- // die(__FILE__.__LINE__);
- //$_SERVER["REDIRECT_URL"] = $_SERVER["REQUEST_URI"] = '/farmacia_virtual/web/product/6-medicina-natural/46-homeopat�a/96-sdfsdf';
- // Agrego .html a la url, excepto a la secci�n "comprar".
- if((strpos($url,'tufarmaciavirtual.com/comprar')) !== FALSE){
- return $url;
- }else if((strpos($url,'tufarmaciavirtual.com/resultados')) !== FALSE){
- return $url;
- }else if((strpos($url,'tufarmaciavirtual.com/web/')) !== FALSE){
- return $url;
- }else if($url == '/web/'){
- return substr($url,0,-4);;
- }else{
- return $url.'.html';
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement