Nucleus CMS日本語版用プラグインのうち、日本語版開発者がサポートしているもの
Revision | 9963839c4355ed49a964ac8edf0773ffe7e77e50 (tree) |
---|---|
Time | 2008-07-15 23:18:35 |
Author | kadota <kadota@1ca2...> |
Commiter | kadota |
first upload (v0.21)
git-svn-id: https://svn.sourceforge.jp/svnroot/nucleus-jp/plugin@665 1ca29b6e-896d-4ea0-84a5-967f57386b96
@@ -0,0 +1,925 @@ | ||
1 | +<?php | |
2 | +/* | |
3 | + NP_SimpleURL : Improve url handling with a simple format. | |
4 | + | |
5 | + License | |
6 | + ------- | |
7 | + This program is free software; you can redistribute it and/or | |
8 | + modify it under the terms of the GNU General Public License | |
9 | + as published by the Free Software Foundation; either version 2 | |
10 | + of the License, or (at your option) any later version. | |
11 | + (see nucleus/documentation/index.html#license for more info) | |
12 | + | |
13 | + Usage | |
14 | + ----- | |
15 | + Install this plugin and turn "FancyURLs mode" on. | |
16 | + | |
17 | + In index.php : | |
18 | + Set absolute url (http://...) to $CONF['Self']. | |
19 | + Set blog's shortname to $CONF['NP_SimpleURL']['selectBlog'] before including config.php. (optional) | |
20 | + Set selectBlog('shortname') before selector(). (optional) | |
21 | + | |
22 | + In fancyurls.config.php or index.php : | |
23 | + You can use some features (flags). | |
24 | + | |
25 | + $CONF['NP_SimpleURL']['UseAlias'] = 1; //convert id number to alias (shortname). don't turn "DropKeys" on if "UseAlias" is off. | |
26 | + $CONF['NP_SimpleURL']['DropKeys'] = 1; //drop keys for blog, category and item | |
27 | + $CONF['NP_SimpleURL']['ShowBlogAlways'] = 0; //show blog always | |
28 | + $CONF['NP_SimpleURL']['ShowCategoryAlways'] = 1; //show category always (in permalink) | |
29 | + | |
30 | + 誤動作にハマらないために注意すべき事柄: | |
31 | + 1.実在するディレクトリ名と同じ名前を、URL修飾キーやエイリアスに使わない。 | |
32 | + 2.スキンタイプ、URL修飾キーなどで予約済みの名前をエイリアスに使わない。 | |
33 | + 3.ブログ間で同名のカテゴリーエイリアスを使う場合は index.phpに $CONF['NP_SimpleURL']['selectBlog']をセットする。 | |
34 | + ※2の制約から解放されたい場合は DropKeys を無効にすること。 | |
35 | + ※3の制約は ShowBlogAlways を有効にしてURLにブログ情報を必ず含めることでも解消できる。 | |
36 | + | |
37 | + History | |
38 | + ------- | |
39 | + 2008-07-12 v0.21: Improve function for category alias. (yu) | |
40 | + 2008-06-08 v0.2 : Add some new features. Support alias names. (yu) | |
41 | + 2008-05-02 v0.1 : First release. (yu http://nucleus.datoka.jp/) | |
42 | +*/ | |
43 | + | |
44 | + | |
45 | +/* ----------------- additional global functions ----------------- */ | |
46 | + | |
47 | +/* addLinkParams()をグローバル関数として切り出せるように下記も一応グローバルで宣言しておく */ | |
48 | + | |
49 | +if(!function_exists('getBlogURLFromID')) { | |
50 | + function getBlogURLFromID($blogid) | |
51 | + { | |
52 | + static $buff; | |
53 | + | |
54 | + $blogid = (int)$blogid; | |
55 | + if (!$buff[$blogid]) { | |
56 | + $res = sql_query('SELECT bnumber, burl FROM ' . sql_table('blog')); | |
57 | + if (mysql_num_rows($res)) { | |
58 | + while ($data = mysql_fetch_assoc($res)) { | |
59 | + $buff[ $data['bnumber'] ] = $data['burl']; | |
60 | + } | |
61 | + } | |
62 | + } | |
63 | + return $buff[$blogid]; | |
64 | + } | |
65 | +} | |
66 | + | |
67 | +if(!function_exists('getBlogAliasFromID')) { | |
68 | + function getBlogAliasFromID($id) | |
69 | + { | |
70 | + static $buff; | |
71 | + | |
72 | + $id = (int)$id; | |
73 | + if (!$buff[$id]) { | |
74 | + $buff[$id] = quickQuery('SELECT bshortname as result FROM ' . sql_table('blog') . ' WHERE bnumber=' . $id); | |
75 | + } | |
76 | + return ($buff[$id]) ? $buff[$id] : $id; | |
77 | + } | |
78 | +} | |
79 | + | |
80 | +if(!function_exists('getMemberAliasFromID')) { | |
81 | + function getMemberAliasFromID($id) | |
82 | + { | |
83 | + static $buff; | |
84 | + | |
85 | + $id = (int)$id; | |
86 | + if (!$buff[$id]) { | |
87 | + $buff[$id] = quickQuery('SELECT mname as result FROM ' . sql_table('member') . ' WHERE mnumber=' . $id); | |
88 | + $buff[$id] = str_replace(' ', '_', $buff[$id]); | |
89 | + } | |
90 | + return ($buff[$id]) ? $buff[$id] : $id; | |
91 | + } | |
92 | +} | |
93 | + | |
94 | +if(!function_exists('getCatAliasFromID')) { | |
95 | + function getCatAliasFromID($id, $blogid) | |
96 | + { | |
97 | + global $manager; | |
98 | + static $buff; | |
99 | + | |
100 | + $id = (int)$id; | |
101 | + if (! isset($buff)) { | |
102 | + $buff = getCatAliasesFromDB(); | |
103 | + } | |
104 | + return ($buff[$blogid][$id]) ? $buff[$blogid][$id] : $id; | |
105 | + } | |
106 | +} | |
107 | + | |
108 | +if(!function_exists('getCatAliasesFromDB')) { | |
109 | + function getCatAliasesFromDB() | |
110 | + { | |
111 | + $arr = array(); | |
112 | + $res = sql_query('SELECT catid, blogid, cshortname FROM ' . sql_table('plug_simpleurl') .' ORDER BY catid'); | |
113 | + while($data = mysql_fetch_assoc($res)) { | |
114 | + $arr[ $data['blogid'] ][$data['catid']] = $data['cshortname']; | |
115 | + } | |
116 | + return $arr; | |
117 | + } | |
118 | +} | |
119 | + | |
120 | +if(!function_exists('getItemAliasFromID')) { | |
121 | + function getItemAliasFromID($id) | |
122 | + { | |
123 | + global $manager; | |
124 | + | |
125 | + if ($manager->pluginInstalled('NP_SimpleURL')) { | |
126 | + $obj =& $manager->getPlugin('NP_SimpleURL'); | |
127 | + $t = strip_tags($obj->_getDataFromItemID($id, $type='ititle')); | |
128 | + $t = mb_substr(str_replace(array(' ','/','?','&','%'), '_', $t), 0, 20) .'-'. $id; | |
129 | + return $t; | |
130 | + } | |
131 | + } | |
132 | +} | |
133 | + | |
134 | + | |
135 | +class NP_SimpleURL extends NucleusPlugin | |
136 | +{ | |
137 | + function getName() { return 'Simple URL'; } | |
138 | + function getAuthor() { return 'yu'; } | |
139 | + function getURL() { return 'http://nucleus.datoka.jp/'; } | |
140 | + function getVersion() { return '0.21'; } | |
141 | + function getMinNucleusVersion() { return 322; } | |
142 | + function supportsFeature($what) { return (int)($what == 'SqlTablePrefix'); } | |
143 | + function hasAdminArea() { return 1; } | |
144 | + function getDescription() | |
145 | + { | |
146 | + return 'Improve url handling with a simple format.'; | |
147 | + } | |
148 | + function getEventList() | |
149 | + { | |
150 | + return array( | |
151 | + 'PostAuthentication', | |
152 | + 'ParseURL', | |
153 | + 'GenerateURL', | |
154 | + 'PostAddCategory', | |
155 | + 'PostDeleteCategory', | |
156 | + 'PostMoveCategory', | |
157 | + 'QuickMenu' | |
158 | + ); | |
159 | + } | |
160 | + | |
161 | + var $flg_fancy; | |
162 | + var $arr_item; | |
163 | + | |
164 | + function install() | |
165 | + { | |
166 | + $this->createOption('flg_qmenu', 'Show on quick menu', 'yesno', 'yes'); | |
167 | + $this->createOption('flg_erase', 'Erase data on uninstallation', 'yesno', 'no'); | |
168 | + | |
169 | + //テーブルが存在するかチェックする | |
170 | + $chktable = sql_query("SHOW TABLES LIKE '". sql_table('plug_simpleurl') ."'"); | |
171 | + if (mysql_num_rows($chktable)) return; | |
172 | + | |
173 | + //存在しなければテーブル生成&データ代入 | |
174 | + sql_query("CREATE TABLE IF NOT EXISTS ". sql_table('plug_simpleurl') ." ( | |
175 | + catid INT UNSIGNED NOT NULL DEFAULT 0 PRIMARY KEY, | |
176 | + blogid INT UNSIGNED NOT NULL DEFAULT 0, | |
177 | + cshortname VARCHAR(50) NOT NULL DEFAULT '')"); | |
178 | + $res = sql_query("SELECT catid, cblog FROM ". sql_table('category')); | |
179 | + while ($data = mysql_fetch_assoc($res)) { | |
180 | + sql_query("INSERT ". sql_table('plug_simpleurl') | |
181 | + ." SET catid=". $data['catid'] .", blogid=". $data['cblog'] .", cshortname='c". $data['catid'] ."'"); | |
182 | + } | |
183 | + } | |
184 | + | |
185 | + function uninstall() | |
186 | + { | |
187 | + if ($this->getOption('flg_erase') == 'yes') { | |
188 | + sql_query("DROP TABLE ". sql_table('plug_simpleurl')); | |
189 | + } | |
190 | + } | |
191 | + | |
192 | + function init() | |
193 | + { | |
194 | + global $CONF; | |
195 | + | |
196 | + $this->flg_fancy = ($CONF['URLMode'] == 'pathinfo'); | |
197 | + | |
198 | + if ($this->flg_fancy) { | |
199 | + //URLパラメータの並び順(あらかじめキーを作っておく) | |
200 | + //外部プラグインは各々init()で$CONF['HogeKey']登録と、NP_SimpleURL::setURLOrder('hoge',N)する。 | |
201 | + $CONF['NP_SimpleURL']['URLOrder'] = array( | |
202 | + $CONF['BlogKey'] => 0, //1番目 | |
203 | + $CONF['CategoryKey'] => 1, //2番目 | |
204 | + $CONF['SpecialskinKey'] => 2, //3番目 | |
205 | + $CONF['ArchiveKey'] => 10, //以下同順 | |
206 | + $CONF['ArchivesKey'] => 10, | |
207 | + $CONF['ItemKey'] => 10, | |
208 | + $CONF['MemberKey'] => 10, | |
209 | + ); | |
210 | + } | |
211 | + } | |
212 | + | |
213 | + /* ----------------- event methods ----------------- */ | |
214 | + | |
215 | + function event_PostAuthentication(&$data) | |
216 | + { | |
217 | + //wake up! (nothing to do) | |
218 | + //外部からNP_SimpleURLのメソッドを静的呼び出しするときの便宜を考えて。 | |
219 | + //NP_SimpleURL::addLinkParams()など | |
220 | + } | |
221 | + | |
222 | + function event_QuickMenu(&$data) | |
223 | + { | |
224 | + global $member; | |
225 | + | |
226 | + if ($this->getOption('flg_qmenu') != 'yes') return; | |
227 | + if (!$this->_canEdit()) return; | |
228 | + | |
229 | + array_push( | |
230 | + $data['options'], | |
231 | + array( | |
232 | + 'title' => 'Simple URL', | |
233 | + 'url' => $this->getAdminURL(), | |
234 | + 'tooltip' => 'Setting of category alias' | |
235 | + ) | |
236 | + ); | |
237 | + } | |
238 | + | |
239 | + function event_PostMoveCategory(&$data) | |
240 | + { | |
241 | + sql_query("UPDATE ". sql_table('plug_simpleurl') | |
242 | + ." SET blogid=". $data['destblog']->getID() ." WHERE catid=". $data['catid']); | |
243 | + } | |
244 | + | |
245 | + function event_PostAddCategory(&$data) | |
246 | + { | |
247 | + sql_query("INSERT ". sql_table('plug_simpleurl') | |
248 | + ." SET catid=". $data['catid'] .", blogid=". $data['blog']->getID() .", cshortname='c". $data['catid'] ."'"); | |
249 | + } | |
250 | + | |
251 | + function event_PostDeleteCategory($data) | |
252 | + { | |
253 | + sql_query("DELETE ". sql_table('plug_simpleurl') ." WHERE catid=". $data['catid']); | |
254 | + } | |
255 | + | |
256 | + //URLパース | |
257 | + function event_ParseURL(&$pdata) | |
258 | + { | |
259 | + // nothing to do if another plugin already parsed the URL | |
260 | + if ($pdata['complete']) return; | |
261 | + | |
262 | + global $CONF, $blogid, $catid, $itemid, $memberid, $archive, $archivelist; | |
263 | + | |
264 | + $complete =& $pdata['complete']; | |
265 | + $virtualpath =& $pdata['info']; | |
266 | + $virtualpath = preg_replace('/\.html$/', '', $virtualpath); | |
267 | + | |
268 | + //selectBlog()の先取り。URLパースのタイミング上、selectBlog()では遅いため。 | |
269 | + if (!$blogid and $CONF['SimpleURL']['selectBlog']) { | |
270 | + selectBlog( $CONF['SimpleURL']['selectBlog'] ); | |
271 | + } | |
272 | + | |
273 | + $data = explode("/", $virtualpath ); | |
274 | + for ($i = 0; $i < sizeof($data); $i++) { | |
275 | + switch ($data[$i]) { | |
276 | + case $CONF['ItemKey']: // item/1 (itemid) or item/name (urlencoded ititle) | |
277 | + $i++; | |
278 | + if ($i < sizeof($data) ) { | |
279 | + if (is_numeric($data[$i])) $itemid = intval($data[$i]); | |
280 | + else $itemid = $this->_getItemIDFromAlias($data[$i]); | |
281 | + } | |
282 | + break; | |
283 | + | |
284 | + case $CONF['ArchivesKey']: // archives | |
285 | + if ($blogid) { | |
286 | + $archivelist = $blogid; | |
287 | + } | |
288 | + else { | |
289 | + $archivelist = $CONF['DefaultBlog']; | |
290 | + } | |
291 | + break; | |
292 | + | |
293 | + case $CONF['ArchiveKey']: // archive/yyyy-mm | |
294 | + $i++; | |
295 | + if ($i < sizeof($data) ) { | |
296 | + $archive = $data[$i]; | |
297 | + } | |
298 | + break; | |
299 | + | |
300 | + case $CONF['BlogKey']: // blog/1 or blog/bshortname | |
301 | + case 'blogid': | |
302 | + $i++; | |
303 | + if ($i < sizeof($data) ) { | |
304 | + if (is_numeric($data[$i])) $blogid = intval($data[$i]); | |
305 | + else $blogid = $this->_getBlogIDFromAlias($data[$i]); | |
306 | + } | |
307 | + break; | |
308 | + | |
309 | + case $CONF['CategoryKey']: // category/1 (catid) or category/aliasname | |
310 | + case 'catid': | |
311 | + $i++; | |
312 | + if ($i < sizeof($data) ) { | |
313 | + if (is_numeric($data[$i])) $catid = intval($data[$i]); | |
314 | + else $catid = $this->_getCatIDFromAlias($data[$i]); | |
315 | + } | |
316 | + break; | |
317 | + | |
318 | + case $CONF['MemberKey']: // member/1 (memberid) or member/mname | |
319 | + $i++; | |
320 | + if ($i < sizeof($data) ) { | |
321 | + if (is_numeric($data[$i])) $memberid = intval($data[$i]); | |
322 | + else $memberid = $this->_getMemberIDFromAlias($data[$i]); | |
323 | + } | |
324 | + break; | |
325 | + | |
326 | + case $CONF['SpecialskinKey']: //extra/specialname | |
327 | + $i++; | |
328 | + if ($i < sizeof($data) ) { | |
329 | + $_REQUEST['special'] = $data[$i]; | |
330 | + } | |
331 | + break; | |
332 | + | |
333 | + default: | |
334 | + //無効なキーは捨てる | |
335 | + if ($data[$i] == '') break; //skip | |
336 | + | |
337 | + $flg_num = (is_numeric($data[$i])); | |
338 | + | |
339 | + //拡張キーと合致するかスキャンする | |
340 | + $exkey = ucfirst($data[$i]) . 'Key'; | |
341 | + if (isset($CONF[$exkey])) { | |
342 | + $exstore =& $_REQUEST; //代入先はリクエスト変数 | |
343 | + | |
344 | + if (!$exstore[ $CONF[$exkey] ]) { //対象が未定義なら代入を許可する | |
345 | + $i++; | |
346 | + $exstore[ $CONF[$exkey] ] = $data[$i]; //そのまま代入してるので、利用時は値のチェックを忘れずに。 | |
347 | + } | |
348 | + break; | |
349 | + } | |
350 | + | |
351 | + //debuglog('check non-key val: '.$data[$i]); | |
352 | + //「キー無し」の可能性をチェック。ここでは数値のみの値を避ける | |
353 | + $tempid = 0; | |
354 | + if ($CONF['NP_SimpleURL']['DropKeys'] and !$flg_num and preg_match('/^[0-9a-z_-]+$/', $data[$i])) { | |
355 | + if (!$blog_done and $i == 0 and $CONF['NP_SimpleURL']['ShowBlogAlways']) { //ブログのキー無しは ShowBlogAlways が有効なときのみ | |
356 | + $tempid = $this->_getBlogIDFromAlias($data[$i]); | |
357 | + if ($tempid) { | |
358 | + //debuglog('hit: blog'.$tempid. ' ' .$data[$i]); | |
359 | + $blogid = $tempid; | |
360 | + $blog_done = true; | |
361 | + break; | |
362 | + } | |
363 | + //debuglog('through: blog '.$i); | |
364 | + } | |
365 | + if (!$cat_done and $i < 2) { //カテゴリーのキー無しの可能性をチェック | |
366 | + $tempid = $this->_getCatIDFromAlias($data[$i]); | |
367 | + if ($tempid) { | |
368 | + //debuglog('hit: cat'.$tempid. ' ' .$data[$i]); | |
369 | + $catid = $tempid; | |
370 | + $cat_done = true; | |
371 | + break; | |
372 | + } | |
373 | + //debuglog('through: cat '.$i); | |
374 | + } | |
375 | + } | |
376 | + //アイテムのキー無しの可能性をチェック。数値のみも受け付ける(キー無し数値はアイテムとみなされる) | |
377 | + if ($CONF['NP_SimpleURL']['DropKeys'] and !$item_done and | |
378 | + ( | |
379 | + ($i >= 0 and !$CONF['NP_SimpleURL']['ShowBlogAlways']) or | |
380 | + ($i >= 1 and $CONF['NP_SimpleURL']['ShowBlogAlways'] ^ $CONF['NP_SimpleURL']['ShowCategoryAlways']) or | |
381 | + ($i >= 2 and $CONF['NP_SimpleURL']['ShowBlogAlways'] & $CONF['NP_SimpleURL']['ShowCategoryAlways']) | |
382 | + ) | |
383 | + ) { | |
384 | + if ($flg_num) $tempid = (int)$data[$i]; | |
385 | + else $tempid = $this->_getItemIDFromAlias($data[$i]); | |
386 | + | |
387 | + if ($tempid) { | |
388 | + //debuglog('hit: item'.$tempid. ' ' .$data[$i]); | |
389 | + $itemid = $tempid; | |
390 | + $item_done = true; | |
391 | + break; | |
392 | + } | |
393 | + //debuglog('through: item '.$i); | |
394 | + } | |
395 | + //debuglog('through: last '.$i); | |
396 | + } | |
397 | + } | |
398 | + //debuglog(array('blogid'=>$blogid, 'catid'=>$catid, 'vpath'=>$virtualpath)); | |
399 | + $complete = true; | |
400 | + } | |
401 | + | |
402 | + //URL生成 | |
403 | + //ここでは$baseurlの決定と、$set(=extra)の用意だけをする。最終的な組み立てはaddLinkParams()に委ねる。 | |
404 | + //ノーマルURLでも動くと思われるが、きちんと検証してない。 | |
405 | + function event_GenerateURL(&$data) | |
406 | + { | |
407 | + // if another plugin already generated the URL | |
408 | + if ($data['completed']) return; | |
409 | + | |
410 | + global $manager, $CONF, $blog, $blogid; | |
411 | + static $defBlogURL, $oldItemID, $oldBlogID, $oldBaseurl; | |
412 | + | |
413 | + //フィード配信時の例外。必ずDB側のburlを取得する。 | |
414 | + $flg_feed = (strpos($CONF['Self'], 'xml') === 0 or strpos($CONF['Self'], 'atom') === 0); | |
415 | + | |
416 | + if ($defBlogURL == null) { | |
417 | + $defBlogURL = getBlogURLFromID($CONF['DefaultBlog']); | |
418 | + } | |
419 | + | |
420 | + $params =& $data['params']; | |
421 | + $set =& $params['extra']; //addLinkParams()に送る追加パラメータ | |
422 | + | |
423 | + //追加パラメータの例外処理 | |
424 | + if ($this->flg_fancy and $set['archivelist']) { | |
425 | + $set['archivelist'] = '[[NONE]]'; //キーのみ、値無し(dummy) | |
426 | + } | |
427 | + | |
428 | + //blogidはこの時点で必ず決まっているのでそれと$params['hogeid']から割り出したblogidとを比較 | |
429 | + //呼び出しブログのときはCONF['Self'](絶対URL指定)、それ以外のときは必ずburlを参照してベースurlとする | |
430 | + switch ($data['type']) { | |
431 | + case 'item': | |
432 | + if ($params['itemid'] == $oldItemID) { | |
433 | + $params['blogid'] = $oldBlogID; | |
434 | + $baseurl = $oldBaseurl; | |
435 | + } | |
436 | + else { | |
437 | + $params['blogid'] = $this->_getBlogIDFromItemID($params['itemid']); | |
438 | + $baseurl = ($blogid == $params['blogid'] and !$flg_feed) ? $CONF['ItemURL'] : getBlogURLFromID($params['blogid']); | |
439 | + $oldItemID = $params['itemid']; | |
440 | + $oldBlogID = $params['blogid']; | |
441 | + $oldBaseurl = $baseurl; | |
442 | + } | |
443 | + | |
444 | + $set['blogid'] = $params['blogid']; | |
445 | + $set['itemid'] = $params['itemid']; | |
446 | + if ($CONF['NP_SimpleURL']['ShowCategoryAlways']) { | |
447 | + $set['catid'] = ($params['catid']) ? $params['catid'] : $this->_getCatIDFromItemID($params['itemid']); | |
448 | + } | |
449 | + break; | |
450 | + | |
451 | + case 'member': | |
452 | + $params['blogid'] = $blogid; | |
453 | + $baseurl = $CONF['MemberURL']; | |
454 | + | |
455 | + $set['blogid'] = $params['blogid']; | |
456 | + $set['memberid'] = $params['memberid']; | |
457 | + if (isset($set['catid'])) unset($set['catid']); //例外 | |
458 | + break; | |
459 | + | |
460 | + case 'category': | |
461 | + $params['blogid'] = $this->_getBlogIDFromCatID($params['catid']); | |
462 | + $baseurl = ($blogid == $params['blogid'] and !$flg_feed) ? $CONF['CategoryURL'] : getBlogURLFromID($params['blogid']); | |
463 | + | |
464 | + $set['blogid'] = $params['blogid']; | |
465 | + $set['catid'] = $params['catid']; | |
466 | + break; | |
467 | + | |
468 | + case 'archivelist': | |
469 | + if (!$params['blogid']) $params['blogid'] = $CONF['DefaultBlog']; | |
470 | + $baseurl = ($blogid == $params['blogid'] and !$flg_feed) ? $CONF['ArchiveListURL'] : getBlogURLFromID($params['blogid']); | |
471 | + | |
472 | + $set['blogid'] = $params['blogid']; | |
473 | + if ($this->flg_fancy) { | |
474 | + $set['archivelist'] = '[[NONE]]'; //キーのみ、値無し(dummy) | |
475 | + } | |
476 | + else { | |
477 | + $set['archivelist'] = $params['blogid']; | |
478 | + } | |
479 | + break; | |
480 | + | |
481 | + case 'archive': | |
482 | + $baseurl = ($blogid == $params['blogid'] and !$flg_feed) ? $CONF['ArchiveURL'] : getBlogURLFromID($params['blogid']); | |
483 | + | |
484 | + $set['blogid'] = $params['blogid']; | |
485 | + $set['archive'] = $params['archive']; | |
486 | + break; | |
487 | + | |
488 | + case 'blog': | |
489 | + $baseurl = ($blogid == $params['blogid'] and !$flg_feed) ? $CONF['BlogURL'] : getBlogURLFromID($params['blogid']); | |
490 | + | |
491 | + $set['blogid'] = $params['blogid']; | |
492 | + break; | |
493 | + } | |
494 | + | |
495 | + //addLinkParams()の変更はコアファイルを書き換えるか、NP_SimpleURL::addLinkParams()を呼ぶかのいずれか | |
496 | + $data['url'] = $this->addLinkParams($baseurl, $set, '.html', true); | |
497 | + $data['completed'] = true; | |
498 | + } | |
499 | + | |
500 | + /** | |
501 | + * add link params | |
502 | + * | |
503 | + * 単独でglobalfunctions.phpの addLinkParams() にコピペ・上書きしても動くはず。 | |
504 | + * | |
505 | + * @static | |
506 | + * @access public | |
507 | + * @param $link base link | |
508 | + * @param $params parameters (assoc array) | |
509 | + * @param $postfix postfix to add extension like ".html" | |
510 | + * @param $flg_check flag for event_GenerateURL | |
511 | + */ | |
512 | + function addLinkParams($link, $params, $postfix='', $flg_check=false) | |
513 | + { | |
514 | + global $manager, $CONF, $blog, $blogid; | |
515 | + static $defBlogURL, $orderbase; | |
516 | + | |
517 | + if (! is_array($params) ) return $link; | |
518 | + | |
519 | + $blogid_copy = $params['blogid']; //後の処理で消える場合があるのでコピーをとっておく | |
520 | + | |
521 | + if ($flg_check) { //GenerateURLからの処理の続き | |
522 | + if (!$CONF['NP_SimpleURL']['ShowBlogAlways']) { | |
523 | + if ($defBlogURL == null) $defBlogURL = getBlogURLFromID($CONF['DefaultBlog']); | |
524 | + | |
525 | + if ($params['archivelist']) { //archivelistの例外 | |
526 | + unset($params['blogid']); | |
527 | + } | |
528 | + else if ($CONF['URLMode'] == 'pathinfo' and | |
529 | + ($params['blogid'] == $CONF['DefaultBlog'] or //既定のブログ→blogid不要 | |
530 | + $link != $defBlogURL) //既定以外の呼び出しURLはselectBlog()前提→blogid不要 | |
531 | + ) { | |
532 | + unset($params['blogid']); | |
533 | + } | |
534 | + } | |
535 | + } | |
536 | + | |
537 | + if ($CONF['URLMode'] == 'pathinfo') { | |
538 | + //パラメータの並び順を取得する | |
539 | + $order = array(); | |
540 | + if (!$orderbase) { | |
541 | + $orderbase = $CONF['NP_SimpleURL']['URLOrder']; | |
542 | + asort($orderbase); //優先順位でソート | |
543 | + } | |
544 | + foreach (array_keys($orderbase) as $key) $order[$key] = ''; //先にキーをセット | |
545 | + | |
546 | + //$paramsに対応するキーのマッピング | |
547 | + //ここに割り込む必要がないように、外部からのURL拡張は キーを書き換えずに使える名前にすること。 | |
548 | + $keymap = array( | |
549 | + 'blogid' => $CONF['BlogKey'], | |
550 | + 'catid' => $CONF['CategoryKey'], | |
551 | + 'special' => $CONF['SpecialskinKey'], | |
552 | + 'itemid' => $CONF['ItemKey'], | |
553 | + 'archivelist' => $CONF['ArchivesKey'], | |
554 | + 'archive' => $CONF['ArchiveKey'], | |
555 | + 'memberid' => $CONF['MemberKey'], | |
556 | + ); | |
557 | + | |
558 | + //キーや値の書き換え、並び順への追加 | |
559 | + foreach ($params as $key => $value) { | |
560 | + //blogid, catid, memberid, itemidの書き換え(数値→文字列へ) | |
561 | + if ($CONF['NP_SimpleURL']['UseAlias']) { | |
562 | + switch ($key) { | |
563 | + case 'blogid': | |
564 | + $value = getBlogAliasFromID($value); | |
565 | + break; | |
566 | + case 'catid': | |
567 | + $value = getCatAliasFromID($value, $blogid_copy); | |
568 | + break; | |
569 | + case 'itemid': | |
570 | + $value = getItemAliasFromID($value); | |
571 | + break; | |
572 | + case 'memberid': | |
573 | + $value = getMemberAliasFromID($value); | |
574 | + break; | |
575 | + } | |
576 | + } | |
577 | + | |
578 | + if ($keymap[$key]) $key = $keymap[$key]; //該当キーを$CONF['hogeKey']に書き換え | |
579 | + $order[$key] = $value; //並び順指定があればそのキーに値を格納。なければ$orderの最後尾に新規追加されていく | |
580 | + } | |
581 | + | |
582 | + //実際にURLを組み立てる | |
583 | + foreach ($order as $key => $value) { | |
584 | + if ($value == '') continue; | |
585 | + | |
586 | + if ($CONF['NP_SimpleURL']['DropKeys'] and ($key == $CONF['BlogKey'] or $key == $CONF['CategoryKey'])) { | |
587 | + $link .= ((substr($link, -1) == '/') ? '' : '/') . $value . '/'; //ディレクトリ構造を模して最後を閉じる | |
588 | + } | |
589 | + else if ($CONF['NP_SimpleURL']['DropKeys'] and $key == $CONF['ItemKey']) { | |
590 | + $link .= ((substr($link, -1) == '/') ? '' : '/') . rawurlencode($value); | |
591 | + } | |
592 | + else if ($value == '[[NONE]]') { //キーのみ、値無し | |
593 | + $link .= ((substr($link, -1) == '/') ? '' : '/') . $key; | |
594 | + } | |
595 | + else { | |
596 | + $link .= ((substr($link, -1) == '/') ? '' : '/') . $key . '/' . rawurlencode($value); | |
597 | + } | |
598 | + } | |
599 | + | |
600 | + $link .= (substr($link, -1) == '/') ? '' : $postfix; | |
601 | + } | |
602 | + else { //normal url | |
603 | + foreach ($params as $param => $value) { | |
604 | + $link .= ((strpos($link, '?') === false) ? '?' : '&') . $param . '=' . urlencode($value); | |
605 | + } | |
606 | + } | |
607 | + return $link; | |
608 | + } | |
609 | + | |
610 | + /** | |
611 | + * setter of url order | |
612 | + * | |
613 | + * @static | |
614 | + * @access public | |
615 | + * @param $key key of the parameter | |
616 | + * @param $rank rank for order (integer; the smaller number is the higher rank) | |
617 | + */ | |
618 | + function setURLOrder($key, $rank) | |
619 | + { | |
620 | + global $CONF; | |
621 | + $CONF['NP_SimpleURL']['URLOrder'][$key] = $rank; | |
622 | + } | |
623 | + | |
624 | + | |
625 | + | |
626 | + /* ----------------- helper methods ----------------- */ | |
627 | + | |
628 | + function _quoteSmart($val) | |
629 | + { | |
630 | + if (get_magic_quotes_gpc()) $val = stripslashes($val); | |
631 | + if (preg_match('/^[0-9]+$/', $val)) { | |
632 | + $qmark = ''; | |
633 | + } | |
634 | + else { | |
635 | + $qmark = '"'; | |
636 | + $val = mysql_real_escape_string($val); | |
637 | + } | |
638 | + return $qmark . $val . $qmark; | |
639 | + } | |
640 | + | |
641 | + function _getBlogIDFromAlias($name) | |
642 | + { | |
643 | + static $buff; | |
644 | + | |
645 | + if (! isset($buff)) { | |
646 | + $buff[$name] = quickQuery('SELECT bnumber as result FROM ' . sql_table('blog') . | |
647 | + ' WHERE bshortname="' . mysql_real_escape_string($name) . '"'); | |
648 | + } | |
649 | + return ($buff[$name]) ? $buff[$name] : 0; | |
650 | + } | |
651 | + | |
652 | + function _getMemberIDFromAlias($name) | |
653 | + { | |
654 | + static $buff; | |
655 | + | |
656 | + $name = str_replace('_', ' ', $name); | |
657 | + if (!$buff[$name]) { | |
658 | + $buff[$name] = quickQuery('SELECT mnumber as result FROM ' . sql_table('member') . | |
659 | + ' WHERE mname="' . mysql_real_escape_string($name) .'"'); | |
660 | + } | |
661 | + return ($buff[$name]) ? $buff[$name] : 0; | |
662 | + } | |
663 | + | |
664 | + function _getCatIDFromAlias($name) | |
665 | + { | |
666 | + global $blogid; | |
667 | + static $buff; | |
668 | + | |
669 | + if (! isset($buff)) { | |
670 | + //$buff = array_flip($this->getAllCategoryOptions('cshortname')); | |
671 | + $query = 'SELECT catid, cshortname FROM ' . sql_table('plug_simpleurl'); | |
672 | + if ($blogid) $query .=' WHERE blogid='. (int)$blogid; | |
673 | + $res = sql_query($query); | |
674 | + | |
675 | + while($data = mysql_fetch_assoc($res)) { | |
676 | + $buff[ $data['cshortname'] ] = $data['catid']; | |
677 | + } | |
678 | + } | |
679 | + | |
680 | + return ($buff[$name]) ? $buff[$name] : 0; | |
681 | + } | |
682 | + | |
683 | + function _getItemIDFromAlias($name) | |
684 | + { | |
685 | + /*static $buff; | |
686 | + | |
687 | + $name = urldecode($name); | |
688 | + if (!$buff[$name]) { | |
689 | + $buff[$name] = quickQuery('SELECT inumber as result FROM ' . sql_table('item') . | |
690 | + ' WHERE ititle="' . mysql_real_escape_string($name) . '"'); | |
691 | + } | |
692 | + return ($buff[$name]) ? $buff[$name] : 0; | |
693 | + */ | |
694 | + if (preg_match('/-([0-9]+?)$/', $name, $m)) { | |
695 | + return (int)$m[1]; | |
696 | + } | |
697 | + else { | |
698 | + return 0; | |
699 | + } | |
700 | + } | |
701 | + | |
702 | + function _getDataFromItemID($itemid, $type='') | |
703 | + { | |
704 | + $itemid = (int)$itemid; | |
705 | + | |
706 | + if (!$this->arr_item[$itemid]) { | |
707 | + $res = sql_query('SELECT iblog, icat, ititle FROM ' . sql_table('item') . ' WHERE inumber=' . $itemid); | |
708 | + if (mysql_num_rows($res)) { | |
709 | + $this->arr_item[$itemid] = mysql_fetch_assoc($res); | |
710 | + } | |
711 | + } | |
712 | + if ($type) return $this->arr_item[$itemid][$type]; | |
713 | + else return $this->arr_item[$itemid]; | |
714 | + } | |
715 | + | |
716 | + function _getBlogIDFromItemID($itemid) | |
717 | + { | |
718 | + return $this->_getDataFromItemID($itemid, 'iblog'); | |
719 | + } | |
720 | + | |
721 | + function _getCatIDFromItemID($itemid) | |
722 | + { | |
723 | + return $this->_getDataFromItemID($itemid, 'icat'); | |
724 | + } | |
725 | + | |
726 | + function _getBlogIDFromCatID($catid) | |
727 | + { | |
728 | + static $buff; | |
729 | + | |
730 | + $catid = (int)$catid; | |
731 | + if (! $buff[$catid]) { | |
732 | + $res = sql_query('SELECT catid, cblog FROM ' . sql_table('category')); | |
733 | + if (mysql_num_rows($res)) { | |
734 | + while ($data = mysql_fetch_assoc($res)) { | |
735 | + $buff[ $data['catid'] ] = $data['cblog']; | |
736 | + } | |
737 | + } | |
738 | + } | |
739 | + return $buff[$catid]; | |
740 | + } | |
741 | + | |
742 | + function _getCatNameFromID($id) | |
743 | + { | |
744 | + return quickQuery('SELECT cname as result FROM '. sql_table('category') .' WHERE catid='. (int)$id); | |
745 | + } | |
746 | + | |
747 | + function _canEdit($type='admin') | |
748 | + { | |
749 | + global $member; | |
750 | + | |
751 | + $query = 'SELECT * FROM '.sql_table('team').' WHERE tmember='. $member->getID(); | |
752 | + if ($type == 'admin') $query .= ' and tadmin=1'; | |
753 | + return (mysql_num_rows(sql_query($query)) != 0); | |
754 | + } | |
755 | + | |
756 | + | |
757 | + /* ----------------- methods for admin area ----------------- */ | |
758 | + | |
759 | + function _makeAdminArea(&$oPluginAdmin) | |
760 | + { | |
761 | + global $manager, $member; | |
762 | + | |
763 | + $adminurl = $this->getAdminURL(); | |
764 | + | |
765 | + if (!$member->isLoggedIn() or !$this->_canEdit()) { | |
766 | + $oPluginAdmin->start(); | |
767 | + echo '<p>' . _ERROR_DISALLOWED . '</p>'; | |
768 | + $oPluginAdmin->end(); | |
769 | + exit; | |
770 | + } | |
771 | + | |
772 | +//<script type='text/javascript' src='../../javascript/numbercheck.js'></script> | |
773 | + $oPluginAdmin->start(" | |
774 | +<script type='text/javascript'> | |
775 | +<!-- | |
776 | + | |
777 | +function confirm_check(message) { | |
778 | + if( confirm(message) ){ | |
779 | + sent = true; | |
780 | + return true; | |
781 | + } | |
782 | + else { | |
783 | + return false; | |
784 | + } | |
785 | +} | |
786 | + | |
787 | +// --> | |
788 | +</script> | |
789 | +<style type='text/css'> | |
790 | +<!-- | |
791 | +p.message { | |
792 | + font-weight: bold; | |
793 | + color: #c00; | |
794 | +} | |
795 | +form.button { | |
796 | + display: inline; | |
797 | +} | |
798 | +table.group { | |
799 | + margin: 5px 0; | |
800 | +} | |
801 | +table.group td { | |
802 | + background-color: #ddd; | |
803 | +} | |
804 | +table.link { | |
805 | + margin: 0; | |
806 | +} | |
807 | +table.link th { | |
808 | + /*background-color: #ddd;*/ | |
809 | +} | |
810 | +table.link td.stripe { | |
811 | + background-color: #eee; | |
812 | +} | |
813 | +span.idlabel { | |
814 | + border: 1px solid #666; | |
815 | + padding: 1px 2px; | |
816 | + background-color: #999; | |
817 | + color: white; | |
818 | +} | |
819 | +--> | |
820 | +</style> | |
821 | +"); | |
822 | + | |
823 | + echo "<h2>SimpleURL</h2>"; | |
824 | + | |
825 | + $action = requestVar('action'); | |
826 | + $actions = array ( | |
827 | + 'index', | |
828 | + 'update', | |
829 | + ); | |
830 | + | |
831 | + if ($action == '') { | |
832 | + $this->_makeCatAliasForm(); | |
833 | + } | |
834 | + else if (in_array($action, $actions)) { | |
835 | + if (!$manager->checkTicket()) { | |
836 | + echo '<p class="error">Error: ' . _ERROR_BADTICKET . '</p>'; | |
837 | + } | |
838 | + else { | |
839 | + switch ($action) { | |
840 | + case 'index': | |
841 | + $this->_makeCatAliasForm(); | |
842 | + break; | |
843 | + case 'update': | |
844 | + $isok = $this->_updateCatAlias(); | |
845 | + if ($isok) echo '<p class="message">Updated successfully.</p>'; | |
846 | + else echo '<p class="error">Error: Update failed (in some records).</p>'; | |
847 | + | |
848 | + $this->_makeCatAliasForm(); | |
849 | + break; | |
850 | + } | |
851 | + } | |
852 | + } | |
853 | + else { | |
854 | + echo '<p class="error">Error: Invalid action type.</p>'; | |
855 | + } | |
856 | + $oPluginAdmin->end(); | |
857 | + } | |
858 | + | |
859 | + function _makeCatAliasForm() | |
860 | + { | |
861 | + global $manager; | |
862 | + | |
863 | + $adminurl = $this->getAdminURL(); | |
864 | + $action = 'update'; | |
865 | + | |
866 | + echo <<<OUT | |
867 | +<h3>Setting: Category Alias</h3> | |
868 | +<table> | |
869 | +<form method="post" action="{$adminurl}"> | |
870 | +<input type="hidden" name="action" value="{$action}" /> | |
871 | +<input type="hidden" name="type" value="categoryalias" /> | |
872 | +OUT; | |
873 | + $manager->addTicketHidden(); | |
874 | + echo <<<OUT | |
875 | +<tr> | |
876 | + <th>Blog</th><th>Category</th><th>Alias Name [0-9a-z_-]</th> | |
877 | +</tr> | |
878 | +OUT; | |
879 | + $data = getCatAliasesFromDB(); | |
880 | + foreach ($data as $blogid => $bdata) { | |
881 | + $blogname = htmlspecialchars(getBlogNameFromID($blogid)); | |
882 | + foreach ($bdata as $catid => $cshortname) { | |
883 | + $catname = htmlspecialchars($this->_getCatNameFromID($catid)); | |
884 | + echo <<<OUT | |
885 | +<tr> | |
886 | + <td><span class="idlabel">ID:{$blogid}</span> {$blogname}</td><td><span class="idlabel">ID:{$catid}</span> {$catname}</td><td><input type="text" name="catids[{$catid}]" value="{$cshortname}" size="20" /></td> | |
887 | +</tr> | |
888 | +OUT; | |
889 | + } | |
890 | + } | |
891 | + echo <<<OUT | |
892 | +<tr> | |
893 | + <th colspan="3" style="text-align:center"><input type="submit" value="Submit"></th> | |
894 | +</tr> | |
895 | +</form> | |
896 | +</table> | |
897 | + | |
898 | +OUT; | |
899 | + } | |
900 | + | |
901 | + function _updateCatAlias() | |
902 | + { | |
903 | + if (!is_array($_POST['catids'])) return false; | |
904 | + | |
905 | + $err = 0; | |
906 | + foreach ($_POST['catids'] as $catid => $cshortname) { | |
907 | + $cshortname = strtolower($cshortname); | |
908 | + $cshortname = preg_replace('/[^0-9a-z_-]/', '', $cshortname); | |
909 | + if ($cshortname == '') { | |
910 | + $err++; | |
911 | + continue; | |
912 | + } | |
913 | + $query = sprintf("UPDATE %s SET cshortname=%s WHERE catid=%d", | |
914 | + sql_table("plug_simpleurl"), | |
915 | + $this->_quoteSmart($cshortname), | |
916 | + $this->_quoteSmart($catid) | |
917 | + ); | |
918 | + $res = sql_query($query); | |
919 | + if (!$res) $err++; | |
920 | + } | |
921 | + return ($err == 0); | |
922 | + } | |
923 | + | |
924 | +} | |
925 | +?> |
@@ -0,0 +1,13 @@ | ||
1 | +<?php | |
2 | +/* | |
3 | + NP_SimpleURL admin area | |
4 | +*/ | |
5 | + | |
6 | +$strRel = '../../../'; | |
7 | +include($strRel . 'config.php'); | |
8 | +include($DIR_LIBS . 'PLUGINADMIN.php'); | |
9 | + | |
10 | +$oPluginAdmin = new PluginAdmin('SimpleURL'); | |
11 | +$oPluginAdmin->plugin->_makeAdminArea(&$oPluginAdmin); | |
12 | + | |
13 | +?> |