00001 <?php
00002
00007 class oxSeoDecoder extends oxSuperCfg
00008 {
00017 public function parseStdUrl($sUrl)
00018 {
00019 $oStr = getStr();
00020 $aRet = array();
00021 $sUrl = $oStr->html_entity_decode( $sUrl );
00022
00023 if ( ( $iPos = strpos( $sUrl, '?' ) ) !== false ) {
00024 parse_str( $oStr->substr( $sUrl, $iPos+1 ), $aRet );
00025 }
00026
00027 return $aRet;
00028 }
00029
00038 protected function _getIdent( $sSeoUrl, $blIgnore = false )
00039 {
00040 return md5( strtolower( $sSeoUrl ) );
00041 }
00042
00052 public function decodeUrl( $sSeoUrl )
00053 {
00054 $oStr = getStr();
00055 $sBaseUrl = $this->getConfig()->getShopURL();
00056 if ( $oStr->strpos( $sSeoUrl, $sBaseUrl ) === 0 ) {
00057 $sSeoUrl = $oStr->substr( $sSeoUrl, $oStr->strlen( $sBaseUrl ) );
00058 }
00059 $sSeoUrl = rawurldecode( $sSeoUrl );
00060 $iShopId = $this->getConfig()->getShopId();
00061
00062 $sKey = $this->_getIdent( $sSeoUrl );
00063 $aRet = false;
00064
00065 $oRs = oxDb::getDb(true)->Execute( "select oxstdurl, oxlang from oxseo where oxident='$sKey' and oxshopid='$iShopId' limit 1");
00066 if ( !$oRs->EOF ) {
00067
00068 $aRet = $this->parseStdUrl( $oRs->fields['oxstdurl'] );
00069 $aRet['lang'] = $oRs->fields['oxlang'];;
00070 }
00071 return $aRet;
00072 }
00073
00083 protected function _decodeOldUrl( $sSeoUrl )
00084 {
00085 $oStr = getStr();
00086 $oDb = oxDb::getDb(true);
00087 $sBaseUrl = $this->getConfig()->getShopURL();
00088 if ( $oStr->strpos( $sSeoUrl, $sBaseUrl ) === 0 ) {
00089 $sSeoUrl = $oStr->substr( $sSeoUrl, $oStr->strlen( $sBaseUrl ) );
00090 }
00091 $iShopId = $this->getConfig()->getShopId();
00092 $sSeoUrl = rawurldecode($sSeoUrl);
00093
00094 $sKey = $this->_getIdent( $sSeoUrl, true );
00095
00096 $sUrl = false;
00097 $oRs = $oDb->execute( "select oxobjectid, oxlang from oxseohistory where oxident = '{$sKey}' and oxshopid = '{$iShopId}' limit 1");
00098 if ( !$oRs->EOF ) {
00099
00100 $oDb->execute( "update oxseohistory set oxhits = oxhits + 1 where oxident = '{$sKey}' and oxshopid = '{$iShopId}' limit 1" );
00101
00102
00103 $sUrl = $oDb->getOne( "select oxseourl from oxseo where oxobjectid = '{$oRs->fields['oxobjectid']}' and oxlang = '{$oRs->fields['oxlang']}' and oxshopid = '{$iShopId}' order by oxparams " );
00104 }
00105
00106 return $sUrl;
00107 }
00108
00118 public function processSeoCall( $sRequest = null, $sPath = null )
00119 {
00120
00121 if ( !$sRequest ) {
00122 if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
00123 $sRequest = $_SERVER['REQUEST_URI'];
00124 } else {
00125 $sRequest = $_SERVER['SCRIPT_URI'];
00126 }
00127 }
00128
00129 $sPath = $sPath ? $sPath : str_replace( 'oxseo.php', '', $_SERVER['SCRIPT_NAME'] );
00130 if ( ( $sParams = $this->_getParams( $sRequest, $sPath ) ) ) {
00131
00132
00133 if ( is_array( $aGet = $this->decodeUrl( $sParams ) ) ) {
00134 $_GET = array_merge( $aGet, $_GET );
00135 oxLang::getInstance()->resetBaseLanguage();
00136 } elseif ( ( $sRedirectUrl = $this->_decodeOldUrl( $sParams ) ) ) {
00137
00138 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00139 } elseif ( ( $sRedirectUrl = $this->_decodeSimpleUrl( $sParams ) ) ) {
00140
00141 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00142 } else {
00143 error_404_handler( $sParams );
00144 }
00145 }
00146 }
00147
00156 protected function _decodeSimpleUrl( $sParams )
00157 {
00158 $oStr = getStr();
00159 $sLastParam = rtrim( $sParams, '/' );
00160 $sLastParam = $oStr->substr( $sLastParam, ( ( int ) strrpos( $sLastParam, '/' ) ) - ( $oStr->strlen( $sLastParam ) ) );
00161 $sLastParam = trim( $sParams, '/' );
00162
00163
00164 $sUrl = null;
00165
00166 if ( $sLastParam ) {
00167
00168 $sLastParam = oxDb::getDb()->quote( $sLastParam );
00169 $iLanguage = oxLang::getInstance()->getBaseLanguage();
00170
00171
00172 if ( strpos( $sLastParam, '.htm' ) !== false ) {
00173 $sUrl = $this->_getObjectUrl( $sLastParam, 'oxarticles', $iLanguage, 'oxarticle' );
00174 } else {
00175
00176
00177 if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxcategories', $iLanguage, 'oxcategory' ) ) ) {
00178
00179 if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxmanufacturers', $iLanguage, 'oxmanufacturer' ) ) ) {
00180
00181 $sUrl = $this->_getObjectUrl( $sLastParam, 'oxvendor', $iLanguage, 'oxvendor' );
00182 }
00183 }
00184 }
00185 }
00186
00187 return $sUrl;
00188 }
00189
00200 protected function _getObjectUrl( $sSeoId, $sTable, $iLanguage, $sType )
00201 {
00202 $oDb = oxDb::getDb();
00203 $sTable = getViewName( $sTable );
00204 $sField = "oxseoid".oxLang::getInstance()->getLanguageTag( $iLanguage );
00205 $sSeoUrl = null;
00206
00207 try {
00208 if ( $sObjectId = $oDb->getOne( "select oxid from $sTable where $sField = $sSeoId" ) ) {
00209 $sSeoUrl = $oDb->getOne( "select oxseourl from oxseo where oxtype = '$sType' and oxobjectid = '$sObjectId' and oxlang = '$iLanguage' " );
00210 }
00211 } catch ( Exception $oEx ) {
00212
00213 }
00214
00215 return $sSeoUrl;
00216 }
00217
00226 protected function _getParams( $sRequest, $sPath )
00227 {
00228 $sParams = preg_replace( '/\?.*/', '', $sRequest );
00229 $sPath = preg_quote($sPath, '/');
00230 $sParams = preg_replace( "/^$sPath/", '', $sParams );
00231
00232
00233 if ( $sParams && !preg_match( '/\.html$/', $sParams ) && !preg_match( '/\/$/', $sParams ) ) {
00234 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL() . $sParams . '/', false );
00235 }
00236
00237 return $sParams;
00238 }
00239
00240 }