|
<?php /* SEO for CRE Loaded 6.1a Version: 3.0 Author: MRB (mrb@chainreactionweb.com) Release date: 11/30/2004 Legal notices: Standard GNU License
Installation instructions: see readme.txt Warning - do not use this on heavy loaded shops (more than 10000 visitors per day) if you are not on dedicated server.
*/
function callback($pagecontent) { // find all the href thatr are part of the <a> tag $pagecontent = preg_replace_callback("/(<a\s+[^>]*href=['\"]{1})([^'\">]+)([^>]*>)/", 'transform_uri', $pagecontent); return $pagecontent; }
function transform_uri($param) { global $languages_id, $seourlreads; // the url in the hrref should be passed here for reformatting // get the complete match and break it into pieces // need to allow for a partical href that uses relative addressing $uriparts = parse_url($param[2]); $scheme = isset( $uriparts['scheme'] ) ? $uriparts['scheme'] : ''; // no reformat on SSL addresses if ( $scheme == 'https' ) return $param[0]; if ( $scheme != '' ) $scheme .= '://'; $host = isset( $uriparts['host'] ) ? $uriparts['host'] : ''; $path = isset( $uriparts['path'] ) ? $uriparts['path'] : ''; $query = isset( $uriparts['query'] ) ? $uriparts['query'] : ''; $fragment = isset( $uriparts['fragment'] ) ? '#' . $uriparts['fragment'] : ''; // get the page name and page path $path_parts = pathinfo( $path ); $page_name = $path_parts['basename']; $page_path = $path_parts['dirname']; // allow for the pathinfo returning a '.' if there is no dirname if ( substr( $page_path, 0, 1 ) == '.' ) $page_path = ''; // the page path may need a trailing / // if ( $page_path != '' && substr( $page_path, -1 ) != '/' ) $page_path .= '/'; $page_path=str_replace("\","/",$page_path); // Find out what chacter to use if a space needs replacing if ( ! defined('CRE_SEO_SPACE_REPLACEMENT') ) { $space_replacement = '-'; // this is done for backward compatiablility, not really the best choice } else { $space_replacement = CRE_SEO_SPACE_REPLACEMENT; } // based on the page name, decide if reformating is required switch( $page_name ) { case 'index.php': case 'product_info.php': case 'articles.php': case 'article_info.php': case 'information.php': case 'pages.php': case 'product_reviews.php': // change the page name and reset the path to empty $page_name = substr( $page_name, 0, strlen($page_name) - 4 ) . '.html'; $path = ''; // process the query string if ( $query != '' ) { // repalce the & with & for backward compatiablility $query = str_replace('&', '&', $query); $query_parts = explode( '&', $query ); //reset the query and path strings $query = ''; // prcoess each piece found // Here is the odd part, normally a simple loop thru the parts found would do // however, because the rewrite rules require the parts to be processed in a certain order // The order of processing is set for backward comatiablility // find all the pieces to process $query_array = array(); foreach ( $query_parts as $q ) { list( $key, $val ) = split( '=', $q ); if ( ! empty( $key ) && ! empty( $val ) ) $query_array[$key] = $val; } if ( array_key_exists( 'cPath', $query_array ) ) { $val = $query_array['cPath']; // check to see if we already have this one if ( ! isset( $seourlreads['cPath'][$val] ) ) { $cat_arr = explode( '_', $val ); $count = false; foreach( $cat_arr as $value ){ $sql_query = tep_db_query("select c.categories_id, cd.categories_name from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = '" . $value . "' and c.categories_id = cd.categories_id and cd.language_id ='" . (int)$languages_id . "'"); $cat_name = tep_db_fetch_array( $sql_query ); if( !$count ){ $result .= encode_str($cat_name['categories_name']); $count = true; } else { $result .= '-' . encode_str($cat_name['categories_name']); } } $cat = str_replace(' ' , $space_replacement , $result); $seourlreads['cPath'][$val] = $cat; } else { $cat = $seourlreads['cPath'][$val]; } $path .= $cat . '/c' . $val . '/'; unset( $query_array['cPath'] ); } if ( array_key_exists( 'manufacturers_id', $query_array ) ) { $val = $query_array['manufacturers_id']; if ( ! isset( $seourlreads['manufacturers_id'][$val] ) ) { $sql_query = tep_db_query("select manufacturers_name from " . TABLE_MANUFACTURERS . " where manufacturers_id = '" . $val . "'"); $man = tep_db_fetch_array($sql_query ); $mname = str_replace( " ", $space_replacement , encode_str($man['manufacturers_name']) ); $seourlreads['manufacturers_id'][$val] = $mname; } else { $mname = $seourlreads['manufacturers_id'][$val]; } $path .= 'm' . $val . '/' . $mname . '/'; unset( $query_array['manufacturers_id'] ); } if ( array_key_exists( 'products_id', $query_array ) ) { $val = $query_array['products_id']; if ( ! isset( $seourlreads['products_id'][$val] ) ) { $sql_query = tep_db_query('select products_name from ' . TABLE_PRODUCTS_DESCRIPTION . ' where products_id = "' . (int)$val . '" and language_id ="' . (int)$languages_id . '"'); $t = tep_db_fetch_array($sql_query ); $pname = str_replace( ' ', $space_replacement , encode_str($t['products_name']) ); $seourlreads['products_id'][$val] = $pname; } else { $pname = $seourlreads['products_id'][$val]; } $path .= 'p' . (int)$val . '/' . $pname . '/'; unset( $query_array['products_id'] ); } if ( array_key_exists( 'info_id', $query_array ) ) { $val = $query_array['info_id']; if ( ! isset( $seourlreads['info_id'][$val] ) ) { $sql_query = tep_db_query('select info_title from ' . TABLE_INFORMATION . ' where information_id = "' . (int)$val . '" and languages_id="' . (int)$languages_id . '"'); $t = tep_db_fetch_array($sql_query ); $pname = str_replace( ' ', $space_replacement , encode_str($t['info_title']) ); $pname = str_replace( ' ', $space_replacement , encode_str($t['products_name']) ); $seourlreads['info_id'][$val] = $pname; } else { $pname = $seourlreads['info_id'][$val]; } $path .= 'i' . (int)$val . '/' . $pname . '/'; unset( $query_array['info_id'] ); } if ( array_key_exists( 'cID', $query_array ) ) { $val = $query_array['cID']; if ( ! isset( $seourlreads['cID'][$val] ) ) { $cat_arr = explode( '_', $val ); $count = false; foreach( $cat_arr as $value ){ $sql_query = tep_db_query("select ic.categories_id, icd.categories_name from " . TABLE_PAGES_CATEGORIES . " ic, " . TABLE_PAGES_CATEGORIES_DESCRIPTION . " icd where ic.categories_id = '" . $value . "' and ic.categories_id = icd.categories_id and icd.language_id='" . (int)$languages_id . "'"); $cat_name = tep_db_fetch_array( $sql_query ); if( !$count ){ $result .= encode_str($cat_name['categories_name']); $count = true; } else { $result .= '-' . encode_str($cat_name['categories_name']); } } $cat = str_replace(' ' , $space_replacement , $result); $seourlreads['cID'][$val] = $cat; } else { $cat = $seourlreads['cID'][$val]; } $path .= $cat . '/c' . $val . '/'; unset( $query_array['cID'] ); } if ( array_key_exists( 'pID', $query_array ) ) { $val = $query_array['pID']; if ( ! isset( $seourlreads['pID'][$val] ) ) { $sql_query = tep_db_query('select pages_title from ' . TABLE_PAGES_DESCRIPTION . ' where pages_id = "' . (int)$val . '" and language_id = "' . (int)$languages_id . '"'); $t = tep_db_fetch_array($sql_query ); $pname = str_replace( ' ', $space_replacement , encode_str($t['pages_title']) ); $seourlreads['pID'][$val] = $pname; } else { $pname = $seourlreads['pID'][$val]; } $path .= 'p' . (int)$val . '/' . $pname . '/'; unset( $query_array['pID'] ); } if ( array_key_exists( 'reviews_id', $query_array ) ) { $val = $query_array['reviews_id']; $path .= 'review' . $val . '/'; unset( $query_array['reviews_id'] ); } if ( array_key_exists( 'tPath', $query_array ) ) { $val = $query_array['tPath']; if ( ! isset( $seourlreads['tPath'][$val] ) ) { $cat_arr = explode( '_', $val ); $count = false; foreach( $cat_arr as $value ){ $sql_query = tep_db_query("select t.topics_id, td.topics_name from " . TABLE_TOPICS . " t, " . TABLE_TOPICS_DESCRIPTION . " td where t.topics_id = '" . $value . "' and t.topics_id = td.topics_id and td.language_id='" . (int)$languages_id . "'"); $cat_name = tep_db_fetch_array($sql_query ); if( !$count ){ $result .= encode_str($cat_name['topics_name']); $count = true; } else { $result .= '-' . encode_str($cat_name['topics_name']); } } $cat = str_replace(' ' , $space_replacement , $result); $seourlreads['tPath'][$val] = $cat; } else { $cat = $seourlreads['tPath'][$val]; } $path .= $cat . '/t' . $val . '/'; unset( $query_array['tPath'] ); } if ( array_key_exists( 'articles_id', $query_array ) ) { $val = $query_array['articles_id']; if ( ! isset( $seourlreads['articles_id'][$val] ) ) { $sql_query = tep_db_query('select articles_name from ' . TABLE_ARTICLES_DESCRIPTION . ' where articles_id = "' . (int)$val . '" and language_id="' . (int)$languages_id . '"'); $t = tep_db_fetch_array($sql_query ); $pname = str_replace( ' ', $space_replacement , encode_str($t['articles_name']) ); $seourlreads['articles_id'][$val] = $pname; } else { $pname = $seourlreads['articles_id'][$val]; } $path .= 'a' . (int)$val . '/' . $pname . '/'; unset( $query_array['articles_id'] ); } // any remain query keys goes back into the query string foreach ( $query_array as $key => $val ) { $query .= '&' . $key . '='.$val; } // remove leading & if needed if ( $query != '' ) { if ( substr( $query, 0, 5) == '&' ) $query = substr( $query, 5 ); $query = '?' . $query; } } return $param[1] . $scheme . $host . $page_path . $path . $page_name . $query . $fragment . $param[3]; break; default: return $param[0]; } }
function encode_str($str) { // replace all these special chanracter $str = str_replace( '"', '%22', $str ); $str = str_replace( '#', '%23', $str ); return $str; }
// the seourlreads array provides a limited form of caching to prevent // addittional queries from being done that have already been done $seourlreads = array();
ob_start("callback");
?>
|