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 if (($iPos = strpos($sUrl, '?')) !== false) {
00023 $aParams = explode('&', $oStr->substr($sUrl, $iPos+1));
00024 foreach ($aParams as $sParam) {
00025 $aP = explode('=', $sParam);
00026 if (count($aP) == 2) {
00027 if (($sName = trim($aP[0])) && ($sValue = trim($aP[1]))) {
00028 $aRet[$sName] = rawurldecode($sValue);
00029 }
00030 }
00031 }
00032 }
00033 return $aRet;
00034 }
00035
00044 protected function _getIdent( $sSeoUrl, $blIgnore = false )
00045 {
00046 return md5( strtolower( $sSeoUrl ) );
00047 }
00048
00058 public function decodeUrl( $sSeoUrl )
00059 {
00060 $oStr = getStr();
00061 $sBaseUrl = $this->getConfig()->getShopURL();
00062 if ( $oStr->strpos( $sSeoUrl, $sBaseUrl ) === 0 ) {
00063 $sSeoUrl = $oStr->substr( $sSeoUrl, $oStr->strlen( $sBaseUrl ) );
00064 }
00065 $sSeoUrl = rawurldecode( $sSeoUrl );
00066 $iShopId = $this->getConfig()->getShopId();
00067
00068 $sKey = $this->_getIdent( $sSeoUrl );
00069 $aRet = false;
00070
00071 $oRs = oxDb::getDb(true)->Execute( "select oxstdurl, oxlang from oxseo where oxident='$sKey' and oxshopid='$iShopId' limit 1");
00072 if ( !$oRs->EOF ) {
00073
00074 $aRet = $this->parseStdUrl( $oRs->fields['oxstdurl'] );
00075 $aRet['lang'] = $oRs->fields['oxlang'];;
00076 }
00077 return $aRet;
00078 }
00079
00089 protected function _decodeOldUrl( $sSeoUrl )
00090 {
00091 $oStr = getStr();
00092 $oDb = oxDb::getDb(true);
00093 $sBaseUrl = $this->getConfig()->getShopURL();
00094 if ( $oStr->strpos( $sSeoUrl, $sBaseUrl ) === 0 ) {
00095 $sSeoUrl = $oStr->substr( $sSeoUrl, $oStr->strlen( $sBaseUrl ) );
00096 }
00097 $iShopId = $this->getConfig()->getShopId();
00098 $sSeoUrl = rawurldecode($sSeoUrl);
00099
00100 $sKey = $this->_getIdent( $sSeoUrl, true );
00101
00102 $sUrl = false;
00103 $oRs = $oDb->execute( "select oxobjectid, oxlang from oxseohistory where oxident = '{$sKey}' and oxshopid = '{$iShopId}' limit 1");
00104 if ( !$oRs->EOF ) {
00105
00106 $oDb->execute( "update oxseohistory set oxhits = oxhits + 1 where oxident = '{$sKey}' and oxshopid = '{$iShopId}' limit 1" );
00107
00108
00109 $sUrl = $oDb->getOne( "select oxseourl from oxseo where oxobjectid = '{$oRs->fields['oxobjectid']}' and oxlang = '{$oRs->fields['oxlang']}' and oxshopid = '{$iShopId}' " );
00110 }
00111
00112 return $sUrl;
00113 }
00114
00124 public function processSeoCall( $sRequest = null, $sPath = null )
00125 {
00126
00127 if ( !$sRequest ) {
00128 if ( isset( $_SERVER['REQUEST_URI'] ) && $_SERVER['REQUEST_URI'] ) {
00129 $sRequest = $_SERVER['REQUEST_URI'];
00130 } else {
00131 $sRequest = $_SERVER['SCRIPT_URI'];
00132 }
00133 }
00134
00135 $sPath = $sPath ? $sPath : str_replace( 'oxseo.php', '', $_SERVER['SCRIPT_NAME'] );
00136 if ( ( $sParams = $this->_getParams( $sRequest, $sPath ) ) ) {
00137
00138
00139 if ( is_array( $aGet = $this->decodeUrl( $sParams ) ) ) {
00140 $_GET = array_merge( $aGet, $_GET );
00141 oxLang::getInstance()->resetBaseLanguage();
00142 } elseif ( ( $sRedirectUrl = $this->_decodeOldUrl( $sParams ) ) ) {
00143
00144 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00145 } elseif ( ( $sRedirectUrl = $this->_decodeSimpleUrl( $sParams ) ) ) {
00146
00147 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL().$sRedirectUrl, false );
00148 } else {
00149 error_404_handler( $sParams );
00150 }
00151 }
00152 }
00153
00162 protected function _decodeSimpleUrl( $sParams )
00163 {
00164 $oStr = getStr();
00165 $sLastParam = rtrim( $sParams, '/' );
00166 $sLastParam = $oStr->substr( $sLastParam, ( ( int ) strrpos( $sLastParam, '/' ) ) - ( $oStr->strlen( $sLastParam ) ) );
00167 $sLastParam = trim( $sParams, '/' );
00168
00169
00170 $sUrl = null;
00171
00172 if ( $sLastParam ) {
00173
00174 $sLastParam = oxDb::getDb()->quote( $sLastParam );
00175 $iLanguage = oxLang::getInstance()->getBaseLanguage();
00176
00177
00178 if ( strpos( $sLastParam, '.htm' ) !== false ) {
00179 $sUrl = $this->_getObjectUrl( $sLastParam, 'oxarticles', $iLanguage, 'oxarticle' );
00180 } else {
00181
00182
00183 if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxcategories', $iLanguage, 'oxcategory' ) ) ) {
00184
00185 if ( !( $sUrl = $this->_getObjectUrl( $sLastParam, 'oxmanufacturers', $iLanguage, 'oxmanufacturer' ) ) ) {
00186
00187 $sUrl = $this->_getObjectUrl( $sLastParam, 'oxvendor', $iLanguage, 'oxvendor' );
00188 }
00189 }
00190 }
00191 }
00192
00193 return $sUrl;
00194 }
00195
00206 protected function _getObjectUrl( $sSeoId, $sTable, $iLanguage, $sType )
00207 {
00208 $oDb = oxDb::getDb();
00209 $sTable = getViewName( $sTable );
00210 $sField = "oxseoid".oxLang::getInstance()->getLanguageTag( $iLanguage );
00211 $sSeoUrl = null;
00212
00213 try {
00214 if ( $sObjectId = $oDb->getOne( "select oxid from $sTable where $sField = $sSeoId" ) ) {
00215 $sSeoUrl = $oDb->getOne( "select oxseourl from oxseo where oxtype = '$sType' and oxobjectid = '$sObjectId' and oxlang = '$iLanguage' " );
00216 }
00217 } catch ( Exception $oEx ) {
00218
00219 }
00220
00221 return $sSeoUrl;
00222 }
00223
00232 protected function _getParams( $sRequest, $sPath )
00233 {
00234 $sParams = preg_replace( '/\?.*/', '', $sRequest );
00235 $sPath = preg_quote($sPath, '/');
00236 $sParams = preg_replace( "/^$sPath/", '', $sParams );
00237
00238
00239 if ( $sParams && !ereg( '\.html$', $sParams ) && !ereg( '\/$', $sParams ) ) {
00240 oxUtils::getInstance()->redirect( $this->getConfig()->getShopURL() . $sParams . '/', false );
00241 }
00242
00243 return $sParams;
00244 }
00245
00246 }