| 1 | <?php |
|---|
| 2 | // wbb imports |
|---|
| 3 | require_once(WBB_DIR.'lib/data/board/Board.class.php'); |
|---|
| 4 | |
|---|
| 5 | // does wbb seo util exist ? |
|---|
| 6 | if(file_exists(WBB_DIR.'lib/data/page/seo/WBBSEORewriter.class.php')) { |
|---|
| 7 | require_once(WBB_DIR.'lib/data/page/seo/WBBSEORewriter.class.php'); |
|---|
| 8 | } |
|---|
| 9 | // otherwise create a new class |
|---|
| 10 | else { |
|---|
| 11 | class WBBSEORewriter { |
|---|
| 12 | protected $encodeHTML, $cachedThreads=array(), $cachedBoardTitles=array(); |
|---|
| 13 | } |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | // does wcf seo util exist ? |
|---|
| 17 | if(file_exists(WCF_DIR.'lib/data/page/seo/SEOUtil.class.php')) { |
|---|
| 18 | require_once(WCF_DIR.'lib/data/page/seo/SEOUtil.class.php'); |
|---|
| 19 | } |
|---|
| 20 | // otherwise create a new class |
|---|
| 21 | else { |
|---|
| 22 | class SEOUtil { |
|---|
| 23 | public static function formatString($string) { |
|---|
| 24 | return $string; |
|---|
| 25 | } |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | * Rewrites links to normal or seo urls |
|---|
| 31 | * |
|---|
| 32 | * @author Torben Brodt |
|---|
| 33 | * @package de.easy-coding.wcf.data.page.publicseorewriter |
|---|
| 34 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-3.0.html> |
|---|
| 35 | */ |
|---|
| 36 | class PublicSEORewriter extends WBBSEORewriter { |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * Caches threads. |
|---|
| 40 | * |
|---|
| 41 | * @param int $threadID |
|---|
| 42 | * @param array $row |
|---|
| 43 | */ |
|---|
| 44 | public function publicCacheThreads($threadID, $row) { |
|---|
| 45 | $row['topic'] = SEOUtil::formatString($row['topic']); |
|---|
| 46 | if ($this->encodeHTML) $row['topic'] = StringUtil::encodeHTML($row['topic']); |
|---|
| 47 | $this->cachedThreads[$row['threadID']] = array( |
|---|
| 48 | 'topic' => $row['topic'], |
|---|
| 49 | 'boardID' => $row['boardID'] |
|---|
| 50 | ); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Caches boards. |
|---|
| 55 | * |
|---|
| 56 | * @param int $boardID |
|---|
| 57 | * @param array $row |
|---|
| 58 | */ |
|---|
| 59 | public function publicCacheBoards($boardID, $row) { |
|---|
| 60 | $this->cachedBoardTitles[$boardID] = SEOUtil::formatString(WCF::getLanguage()->get($row['title'])); |
|---|
| 61 | if ($this->encodeHTML && !preg_match('/^[a-z0-9_\-]+(?:\.[a-z0-9_\-]+)+$/i', $row['title'])) { |
|---|
| 62 | $this->cachedBoardTitles[$boardID] = StringUtil::encodeHTML($this->cachedBoardTitles[$boardID]); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * clears the cache |
|---|
| 68 | */ |
|---|
| 69 | public function clearCache() { |
|---|
| 70 | $this->cachedThreads = array(); |
|---|
| 71 | $this->cachedBoardTitles = array(); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Parses index urls. |
|---|
| 77 | * |
|---|
| 78 | * @param string $queryString |
|---|
| 79 | * @param string $string |
|---|
| 80 | * |
|---|
| 81 | * @return string |
|---|
| 82 | */ |
|---|
| 83 | public function publicParseIndexURLs($queryString, $string = null) { |
|---|
| 84 | if(defined('SEO_ENABLE') && SEO_ENABLE && defined('SEO_REWRITE_INDEX') && SEO_REWRITE_INDEX) { |
|---|
| 85 | if($string === null) $string = SEO_REWRITE_INDEX_FORMAT; |
|---|
| 86 | return $this->parseIndexURLs($queryString); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | * Parses multiple board urls. |
|---|
| 92 | * |
|---|
| 93 | * @param integer $boardID |
|---|
| 94 | * @param integer $pageNo |
|---|
| 95 | * @param string $queryString |
|---|
| 96 | * @param string $string |
|---|
| 97 | * |
|---|
| 98 | * @return string |
|---|
| 99 | */ |
|---|
| 100 | public function publicParseMultipleBoardURLs($boardID, $pageNo, $queryString, $string = null) { |
|---|
| 101 | if(defined('SEO_ENABLE') && SEO_ENABLE && defined('SEO_REWRITE_BOARD') && SEO_REWRITE_BOARD) { |
|---|
| 102 | if($string === null) $string = SEO_REWRITE_BOARD_MULTIPLE_FORMAT; |
|---|
| 103 | return $this->parseMultipleBoardURLs($boardID, $pageNo, $queryString); |
|---|
| 104 | } else { |
|---|
| 105 | return sprintf('index.php?page=Board&boardID=%d&pageNo=%d', $boardID, $pageNo); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Parses board urls. |
|---|
| 111 | * |
|---|
| 112 | * @param integer $boardID |
|---|
| 113 | * @param string $queryString |
|---|
| 114 | * @param string $string |
|---|
| 115 | * |
|---|
| 116 | * @return string |
|---|
| 117 | */ |
|---|
| 118 | public function publicParseBoardURLs($boardID, $queryString, $string = null) { |
|---|
| 119 | if(defined('SEO_ENABLE') && SEO_ENABLE && defined('SEO_REWRITE_BOARD') && SEO_REWRITE_BOARD) { |
|---|
| 120 | if($string === null) $string = SEO_REWRITE_BOARD_FORMAT; |
|---|
| 121 | return $this->parseBoardURLs($boardID, $queryString); |
|---|
| 122 | } else { |
|---|
| 123 | return sprintf('index.php?page=Board&boardID=%d', $boardID); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Parses multiple thread urls. |
|---|
| 129 | * |
|---|
| 130 | * @param integer $threadID |
|---|
| 131 | * @param integer $pageNo |
|---|
| 132 | * @param string $queryString |
|---|
| 133 | * @param string $string |
|---|
| 134 | * |
|---|
| 135 | * @return string |
|---|
| 136 | */ |
|---|
| 137 | public function publicParseMultipleThreadURLs($threadID, $pageNo, $queryString, $string = null) { |
|---|
| 138 | if(defined('SEO_ENABLE') && SEO_ENABLE && defined('SEO_REWRITE_THREAD') && SEO_REWRITE_THREAD) { |
|---|
| 139 | if($string === null) $string = SEO_REWRITE_THREAD_MULTIPLE_FORMAT; |
|---|
| 140 | return $this->parseMultipleThreadURLs($threadID, $pageNo, $queryString); |
|---|
| 141 | } else { |
|---|
| 142 | return sprintf('index.php?page=Thread&threadID=%d&pageNo=%d', $threadID, $pageNo); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * Parses thread urls. |
|---|
| 148 | * |
|---|
| 149 | * @param integer $threadID |
|---|
| 150 | * @param string $queryString |
|---|
| 151 | * @param string $string |
|---|
| 152 | * |
|---|
| 153 | * @return string |
|---|
| 154 | */ |
|---|
| 155 | public function publicParseThreadURLs($threadID, $queryString, $string = null) { |
|---|
| 156 | if(defined('SEO_ENABLE') && SEO_ENABLE && defined('SEO_REWRITE_THREAD') && SEO_REWRITE_THREAD) { |
|---|
| 157 | if($string === null) $string = SEO_REWRITE_THREAD_FORMAT; |
|---|
| 158 | return $this->parseThreadURLs($threadID, $queryString); |
|---|
| 159 | } else { |
|---|
| 160 | return sprintf('index.php?page=Thread&threadID=%d', $threadID); |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | /** |
|---|
| 165 | * Parses post urls. |
|---|
| 166 | * |
|---|
| 167 | * @param integer $postID |
|---|
| 168 | * @param string $queryString |
|---|
| 169 | * @param string $string |
|---|
| 170 | * |
|---|
| 171 | * @return string |
|---|
| 172 | */ |
|---|
| 173 | public function publicParsePostURLs($postID, $queryString, $string = null) { |
|---|
| 174 | if(defined('SEO_ENABLE') && SEO_ENABLE && defined('SEO_REWRITE_THREAD') && SEO_REWRITE_THREAD) { |
|---|
| 175 | if($string === null) $string = SEO_REWRITE_THREAD_POST_FORMAT; |
|---|
| 176 | return $this->parsePostURLs($postID, $queryString); |
|---|
| 177 | } else { |
|---|
| 178 | return sprintf('index.php?page=Thread&postID=%d', $postID); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | * resolves a thread url to its threadID |
|---|
| 184 | * @param string $url |
|---|
| 185 | * |
|---|
| 186 | * @return integer |
|---|
| 187 | */ |
|---|
| 188 | public function thread2threadID($url) { |
|---|
| 189 | $url = parse_url($url); |
|---|
| 190 | |
|---|
| 191 | if(defined('SEO_ENABLE') && SEO_ENABLE && defined('SEO_REWRITE_THREAD') && SEO_REWRITE_THREAD) { |
|---|
| 192 | $seorewrite = SEO_REWRITE_THREAD_FORMAT; |
|---|
| 193 | $seorewrite = preg_replace("/\{[^}]+_ID\}/", "([0-9]+)", $seorewrite); |
|---|
| 194 | $seorewrite = preg_replace("/\{[^}]+\}/", ".+", $seorewrite); |
|---|
| 195 | $seorewrite = "/".str_replace('/','\/',$seorewrite)."/"; |
|---|
| 196 | |
|---|
| 197 | if(preg_match($seorewrite, $url['path'].(isset($url['query'])?$url['query']:''), $match)) { |
|---|
| 198 | return intval($match[1]); |
|---|
| 199 | } |
|---|
| 200 | } else { |
|---|
| 201 | if(isset($url['query'])) { |
|---|
| 202 | parse_str($url['query'], $output); |
|---|
| 203 | return intval($output['threadID']); |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | return null; |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | ?> |
|---|