OSDN Git Service

初回コミット(v2.6.17.1)
[magic3/magic3.git] / widgets / wiki_main / include / plugin / referer.inc.php
1 <?php
2 /**
3  * refererプラグイン
4  *
5  * PHP versions 5
6  *
7  * LICENSE: This source file is licensed under the terms of the GNU General Public License.
8  *
9  * @package    Magic3 Framework
10  * @author     平田直毅(Naoki Hirata) <naoki@aplo.co.jp>
11  * @copyright  Copyright 2006-2008 Magic3 Project.
12  * @license    http://www.gnu.org/copyleft/gpl.html  GPL License
13  * @version    SVN: $Id: referer.inc.php 1141 2008-10-27 06:10:58Z fishbone $
14  * @link       http://www.magic3.org
15  */
16 /*
17  * PukiWiki Referer プラグイン(リンク元表示プラグイン)
18  * (C) 2003, Katsumi Saito <katsumi@jo1upk.ymt.prug.or.jp>
19  * License: GPL
20 */
21
22 define('CONFIG_REFERER', 'plugin/referer/config');
23
24 function plugin_referer_action()
25 {
26         //global $vars, $referer;
27         global $referer;
28         global $_referer_msg;
29
30         // Setting: Off
31         if (! $referer) return array('msg'=>'','body'=>'');
32
33         $page = WikiParam::getPage();
34         //if (isset($vars['page']) && is_page($vars['page'])) {
35         if (is_page($page)){
36                 //$sort = (empty($vars['sort'])) ? '0d' : $vars['sort'];
37                 $sort = (WikiParam::getVar('sort') == '') ? '0d' : WikiParam::getVar('sort');
38                 return array(
39                         'msg'  => $_referer_msg['msg_H0_Refer'],
40                         'body' => plugin_referer_body($page, $sort));
41                         //'body' => plugin_referer_body($vars['page'], $sort));
42         }
43         //$pages = get_existpages(TRACKBACK_DIR, '.ref');
44         $pages = WikiPage::getTrackbackPages();
45
46         if (empty($pages)) {
47                 return array('msg'=>'', 'body'=>'');
48         } else {
49                 return array(
50                         'msg'  => 'referer list',
51                         'body' => page_list($pages, 'referer', FALSE));
52         }
53 }
54
55 // Referer 明細行編集
56 function plugin_referer_body($page, $sort)
57 {
58         global $script, $_referer_msg;
59
60         $data = tb_get(tb_get_filename($page, '.ref'));
61         if (empty($data)) return '<p>no data.</p>';
62
63         $bg = plugin_referer_set_color();
64
65         $arrow_last = $arrow_1st = $arrow_ctr = '';
66         $color_last = $color_1st = $color_ctr = $color_ref = $bg['etc'];
67         $sort_last = '0d';
68         $sort_1st  = '1d';
69         $sort_ctr  = '2d';
70
71         switch ($sort) {
72         case '0d': // 0d 最終更新日時(新着順)
73                 usort($data, create_function('$a,$b', 'return $b[0] - $a[0];'));
74                 $color_last = $bg['cur'];
75                 $arrow_last = $_referer_msg['msg_Chr_darr'];
76                 $sort_last = '0a';
77                 break;
78         case '0a': // 0a 最終更新日時(日付順)
79                 usort($data, create_function('$a,$b', 'return $a[0] - $b[0];'));
80                 $color_last = $bg['cur'];
81                 $arrow_last = $_referer_msg['msg_Chr_uarr'];
82 //              $sort_last = '0d';
83                 break;
84         case '1d': // 1d 初回登録日時(新着順)
85                 usort($data, create_function('$a,$b', 'return $b[1] - $a[1];'));
86                 $color_1st = $bg['cur'];
87                 $arrow_1st = $_referer_msg['msg_Chr_darr'];
88                 $sort_1st = '1a';
89                 break;
90         case '1a': // 1a 初回登録日時(日付順)
91                 usort($data, create_function('$a,$b', 'return $a[1] - $b[1];'));
92                 $color_1st = $bg['cur'];
93                 $arrow_1st = $_referer_msg['msg_Chr_uarr'];
94 //              $sort_1st = '1d';
95                 break;
96         case '2d': // 2d カウンタ(大きい順)
97                 usort($data, create_function('$a,$b', 'return $b[2] - $a[2];'));
98                 $color_ctr = $bg['cur'];
99                 $arrow_ctr = $_referer_msg['msg_Chr_darr'];
100                 $sort_ctr = '2a';
101                 break;
102         case '2a': // 2a カウンタ(小さい順)
103                 usort($data, create_function('$a,$b', 'return $a[2] - $b[2];'));
104                 $color_ctr = $bg['cur'];
105                 $arrow_ctr = $_referer_msg['msg_Chr_uarr'];
106 //              $sort_ctr = '2d';
107                 break;
108         case '3': // 3 Referer
109                 usort($data, create_function('$a,$b',
110                         'return ($a[3] == $b[3]) ? 0 : (($a[3] > $b[3]) ? 1 : -1);'));
111                 $color_ref = $bg['cur'];
112                 break;
113         }
114
115         $body = '';
116         foreach ($data as $arr) {
117                 // 0:最終更新日時, 1:初回登録日時, 2:参照カウンタ, 3:Referer ヘッダ, 4:利用可否フラグ(1は有効)
118                 list($ltime, $stime, $count, $url, $enable) = $arr;
119
120                 // 非ASCIIキャラクタ(だけ)をURLエンコードしておく BugTrack/440
121                 $e_url = htmlspecialchars(preg_replace('/([" \x80-\xff]+)/e', 'rawurlencode("$1")', $url));
122                 $s_url = htmlspecialchars(mb_convert_encoding(rawurldecode($url), SOURCE_ENCODING, 'auto'));
123
124                 $lpass = get_passage($ltime, FALSE); // 最終更新日時からの経過時間
125                 $spass = get_passage($stime, FALSE); // 初回登録日時からの経過時間
126                 $ldate = get_date($_referer_msg['msg_Fmt_Date'], $ltime); // 最終更新日時文字列
127                 $sdate = get_date($_referer_msg['msg_Fmt_Date'], $stime); // 初回登録日時文字列
128
129                 $body .=
130                         ' <tr>' . "\n" .
131                         '  <td>' . $ldate . '</td>' . "\n" .
132                         '  <td>' . $lpass . '</td>' . "\n";
133
134                 $body .= ($count == 1) ?
135                         '  <td colspan="2">N/A</td>' . "\n" :
136                         '  <td>' . $sdate . '</td>' . "\n" .
137                         '  <td>' . $spass . '</td>' . "\n";
138
139                 $body .= '  <td style="text-align:right;">' . $count . '</td>' . "\n";
140
141                 // 適用不可データのときはアンカーをつけない
142                 $body .= plugin_referer_ignore_check($url) ?
143                         '  <td>' . $s_url . '</td>' . "\n" :
144                         '  <td><a href="' . $e_url . '" rel="nofollow">' . $s_url . '</a></td>' . "\n";
145
146                 $body .= ' </tr>' . "\n";
147         }
148         //$href = $script . '?plugin=referer&amp;page=' . rawurlencode($page);
149         $href = $script . WikiParam::convQuery('?plugin=referer&amp;page=' . rawurlencode($page));
150         return <<<EOD
151 <table border="1" cellspacing="1" summary="Referer">
152  <tr>
153   <td style="background-color:$color_last" colspan="2">
154    <a href="$href&amp;sort=$sort_last">{$_referer_msg['msg_Hed_LastUpdate']}$arrow_last</a>
155   </td>
156   <td style="background-color:$color_1st" colspan="2">
157    <a href="$href&amp;sort=$sort_1st">{$_referer_msg['msg_Hed_1stDate']}$arrow_1st</a>
158   </td>
159   <td style="background-color:$color_ctr;text-align:right">
160    <a href="$href&amp;sort=$sort_ctr">{$_referer_msg['msg_Hed_RefCounter']}$arrow_ctr</a>
161   </td>
162   <td style="background-color:$color_ref">
163    <a href="$href&amp;sort=3">{$_referer_msg['msg_Hed_Referer']}</a>
164    </td>
165  </tr>
166  $body
167 </table>
168 EOD;
169 }
170
171 function plugin_referer_set_color()
172 {
173         static $color;
174
175         if (! isset($color)) {
176                 // Default color
177                 $color = array('cur' => '#88ff88', 'etc' => '#cccccc');
178
179                 $config = new Config(CONFIG_REFERER);
180                 $config->read();
181                 $pconfig_color = $config->get('COLOR');
182                 unset($config);
183
184                 // BGCOLOR(#88ff88)
185                 $matches = array();
186                 foreach ($pconfig_color as $x)
187                         $color[$x[0]] = htmlspecialchars(
188                                 preg_match('/BGCOLOR\(([^)]+)\)/si', $x[1], $matches) ?
189                                         $matches[1] : $x[1]);
190         }
191         return $color;
192 }
193
194 function plugin_referer_ignore_check($url)
195 {
196         static $ignore_url;
197
198         // config.php
199         if (! isset($ignore_url)) {
200                 $config = new Config(CONFIG_REFERER);
201                 $config->read();
202                 $ignore_url = $config->get('IGNORE');
203                 unset($config);
204         }
205
206         foreach ($ignore_url as $x)
207                 if (strpos($url, $x) !== FALSE)
208                         return 1;
209         return 0;
210 }
211 ?>