Example #1
0
 /**
  * Implements integrate_buffer
  * This is the core of the mod.  Rewrites the output buffer to create SEF
  * urls.  It will only rewrite urls for the site at hand, not other urls
  *
  * @global string $scripturl
  * @global array $smcFunc
  * @global string $boardurl
  * @global array $txt
  * @global array $modSettings
  * @global array $context
  * @param string $buffer The output buffer after SMF has output the templates
  * @return string Returns the altered buffer (or unaltered if the mod is disabled)
  */
 public static function ob_simplesef($buffer)
 {
     global $scripturl, $boardurl, $txt, $modSettings, $context;
     if (empty($modSettings['simplesef_enable'])) {
         return $buffer;
     }
     self::benchmark('buffer');
     // Bump up our memory limit a bit
     if (@ini_get('memory_limit') < 128) {
         @ini_set('memory_limit', '128M');
     }
     // Grab the topics...
     $matches = array();
     preg_match_all('~\\b' . preg_quote($scripturl) . '.*?topic=([0-9]+)~', $buffer, $matches);
     if (!empty($matches[1])) {
         //self::$debug_info .= 'ob_simplesef > call loadtopicnames<br>';
         self::loadTopicNames(array_unique($matches[1]));
     }
     // We need to find urls that include a user id, so we can grab them all and fetch them ahead of time
     $matches = array();
     preg_match_all('~\\b' . preg_quote($scripturl) . '.*?u=([0-9]+)~', $buffer, $matches);
     if (!empty($matches[1])) {
         self::loadUserNames(array_unique($matches[1]));
     }
     // Grab all URLs and fix them
     $matches = array();
     $count = 0;
     preg_match_all('~\\b(' . preg_quote($scripturl) . '[-a-zA-Z0-9+&@#/%?=\\~_|!:,.;\\[\\]]*[-a-zA-Z0-9+&@#/%=\\~_|\\[\\]]?)([^-a-zA-Z0-9+&@#/%=\\~_|])~', $buffer, $matches);
     if (!empty($matches[0])) {
         $replacements = array();
         foreach (array_unique($matches[1]) as $i => $url) {
             $replacement = self::create_sef_url($url);
             if ($url != $replacement) {
                 $replacements[$matches[0][$i]] = $replacement . $matches[2][$i];
             }
         }
         $buffer = str_replace(array_keys($replacements), array_values($replacements), $buffer);
         $count = count($replacements);
     }
     // Gotta fix up some javascript laying around in the templates
     $extra_replacements = array('/$d\',' => '_%1$d/\',', '/rand,' => '/rand=', '%1.html$d\',' => '%1$d.html\',', $boardurl . '/topic/' => $scripturl . '?topic=', '%1_%1$d/\',' => '%1$d/\',', 'var smf_scripturl = "' . $boardurl . '/' => 'var smf_scripturl = "' . $scripturl);
     $buffer = str_replace(array_keys($extra_replacements), array_values($extra_replacements), $buffer);
     // Check to see if we need to update the actions lists
     $changeArray = array();
     $possibleChanges = array('actions', 'useractions');
     foreach ($possibleChanges as $change) {
         if (empty($modSettings['simplesef_' . $change]) || substr_count($modSettings['simplesef_' . $change], ',') + 1 != count(self::${$change})) {
             $changeArray['simplesef_' . $change] = implode(',', self::${$change});
         }
     }
     if (!empty($changeArray)) {
         updateSettings($changeArray);
         self::$queryCount++;
     }
     self::benchmark('buffer');
     /*
             if (!empty($context['show_load_time']))
                 $buffer = preg_replace('~(.*[s]\sCPU,\s.*queries\.)~', '$1' . sprintf('SimpleSEF: %d replacements', $count) . ' ' . round(self::$benchMark['total'], 3) . $txt['seconds_with'] . self::$queryCount . $txt['queries'], $buffer);
     */
     self::$_count = $count;
     // I think we're done
     return $buffer;
 }