Ejemplo n.º 1
0
 /**
  * Main shurl creation. shurl is first created by encoding the next
  * id in the shurl table, using an 18 characters subset of the alphabet
  * plus a few digits. These have been selected to avoid risks of
  * characters confusion.
  * 
  * There is no check a
  * 
  */
 private function _buildPageId()
 {
     $shURL = '';
     $nextId = $this->_getNextDBId('#__sh404sef_pageids');
     if ($nextId !== false) {
         $nextId = base_convert(18 + $nextId, 10, 18);
         for ($c = 0; $c < strlen($nextId); ++$c) {
             $char = base_convert($nextId[$c], 18, 10);
             $shURL .= self::$_regular[$char];
         }
     }
     // now check if this shurl is not an existing
     // SEF or alias. If so, use the alternate char set
     // to create a new shurl, and try again.
     // using alternate char set (instead of simply increasing nextId)
     // makes sure that next time we try to create a shurl (for next URL)
     // we won't try something we've already used, making the number of attempts
     // for each shurl creation grows each time there is a collision
     try {
         $attempts = 0;
         $maxAttempts = 8;
         // don't need to check for collisions with existing shurls
         // as we use the next insert id, and code that using a unique char set
         //however, if we need to modify the shurl because it collides with
         // an existing SEF url or an alias, we will do so using the alternate
         // character set, so the new shurl don't risk collision with a regular
         // shurl but it may then collide with another, previously modified shurl
         // and so we need to check for shurl collisions when this happens
         $doneShurl = true;
         // however, need to check for collisions with regular sef urls and aliases
         $doneSef = false;
         $doneAlias = false;
         // and for bad language
         $doneClean = false;
         // prepare user set bad language/exclusion list
         $sefConfig =& shRouter::shGetConfig();
         $sefConfig->shurlBlackList = JString::trim($sefConfig->shurlBlackList);
         if (empty($sefConfig->shurlBlackList)) {
             $blackList = array();
         } else {
             if (strpos($sefConfig->shurlBlackList, '|') !== false) {
                 $blackList = explode('|', $sefConfig->shurlBlackList);
             } else {
                 $blackList = array($sefConfig->shurlBlackList);
             }
         }
         $doneBlackList = false;
         do {
             // clean word check: this is an internal bad words list
             if (!$doneClean) {
                 if (in_array($shURL, self::$_badWords)) {
                     // bad language
                     $attempts++;
                     // build a new shurl, by changing a character
                     // with one from the alternate set
                     $shURL = $this->_getModifiedShurl($shURL);
                     // invalidate shurl and alias check flag, to check again with this new shurl
                     $doneShurl = false;
                     $doneAlias = false;
                     $doneSef = false;
                     $doneBlackList = false;
                 } else {
                     $doneClean = true;
                 }
             }
             // user word black list
             if (!$doneBlackList) {
                 if (in_array($shURL, $blackList)) {
                     // bad language
                     $attempts++;
                     // build a new shurl, by changing a character
                     // with one from the alternate set
                     $shURL = $this->_getModifiedShurl($shURL);
                     // invalidate shurl and alias check flag, to check again with this new shurl
                     $doneShurl = false;
                     $doneAlias = false;
                     $doneSef = false;
                     $doneClean = false;
                 } else {
                     $doneBlackList = true;
                 }
             }
             // regular SEF url collision check
             if (!$doneSef) {
                 $isSEF = (int) Sh404sefHelperDb::count('#__redirection', '*', $this->_db->nameQuote('oldurl') . ' = ? and ' . $this->_db->nameQuote('newurl') . ' <> ?', array($shURL, ''));
                 if (!empty($isSEF)) {
                     // there is already a SEF url like that
                     $attempts++;
                     // build a new shurl, by changing a character
                     // with one from the alternate set
                     $shURL = $this->_getModifiedShurl($shURL);
                     // invalidate shurl and alias check flag, to check again with this new shurl
                     $doneShurl = false;
                     $doneAlias = false;
                     $doneClean = false;
                     $doneBlackList = false;
                 } else {
                     $doneSef = true;
                 }
             }
             // previous shurl check
             if (!$doneShurl) {
                 $isShurl = (int) Sh404sefHelperDb::count('#__sh404sef_pageids', '*', array('pageid' => $shURL, 'type' => Sh404sefHelperGeneral::COM_SH404SEF_URLTYPE_PAGEID));
                 if (!empty($isShurl)) {
                     // there is already a shurl like that
                     $attempts++;
                     // build a new shurl, by changing a character
                     // with one from the alternate set
                     $shURL = $this->_getModifiedShurl($shURL);
                     // invalidate regular sef and alias check flag, to check again with this new shurl
                     $doneSef = false;
                     $doneAlias = false;
                     $doneClean = false;
                     $doneBlackList = false;
                 } else {
                     $doneShurl = true;
                 }
             }
             // alias collision check
             if (!$doneAlias) {
                 $isAlias = (int) Sh404sefHelperDb::count('#__sh404sef_aliases', '*', array('alias' => $shURL, 'type' => Sh404sefHelperGeneral::COM_SH404SEF_URLTYPE_ALIAS));
                 if (!empty($isAlias)) {
                     // there is already an alias like that
                     $attempts++;
                     // build a new shurl, by changing a character
                     // with one from the alternate set
                     $shURL = $this->_getModifiedShurl($shURL);
                     // invalidate regular sef and shurl check flag, to check again with this new shurl
                     $doneSef = false;
                     $doneShurl = false;
                     $doneClean = false;
                     $doneBlackList = false;
                 } else {
                     $doneAlias = true;
                 }
             }
         } while ((!$doneSef || !$doneAlias || !$doneShurl || !$doneClean || !$doneBlackList) && $attempts < $maxAttempts);
     } catch (Sh404sefExceptionDefault $e) {
     }
     return $shURL;
 }
Ejemplo n.º 2
0
 public function getAliasesCount($which = 'auto')
 {
     switch (strtolower($which)) {
         // we want to read all automatic urls (include duplicates)
         case 'auto':
             try {
                 $numberOfUrls = Sh404sefHelperDb::count($this->_getTableName(), '*', array('type' => Sh404sefHelperGeneral::COM_SH404SEF_URLTYPE_ALIAS));
             } catch (Sh404sefExceptionDefault $e) {
                 $numberofUrls = 0;
             }
             break;
             // we want to read urls as per current selection input fields
             // ie : component, language, custom, ...
         // we want to read urls as per current selection input fields
         // ie : component, language, custom, ...
         case 'selected':
             $numberOfUrls = $this->getTotal();
             break;
         default:
             $numberOfUrls = 0;
             break;
     }
     return intval($numberOfUrls);
 }