OSDN Git Service

PKWK_READONLY prohibits basic_auth
[pukiwiki/pukiwiki.git] / plugin / referer.inc.php
1 <?php
2 // $Id: referer.inc.php,v 1.10 2005/01/23 05:20:02 henoheno Exp $
3 /*
4  * PukiWiki Referer ¥×¥é¥°¥¤¥ó(¥ê¥ó¥¯¸µÉ½¼¨¥×¥é¥°¥¤¥ó)
5  * (C) 2003, Katsumi Saito <katsumi@jo1upk.ymt.prug.or.jp>
6  * License: GPL
7 */
8
9 define('CONFIG_REFERER', 'plugin/referer/config');
10
11 function plugin_referer_action()
12 {
13         global $vars, $referer;
14         global $_referer_msg;
15
16         // Setting: Off
17         if (! $referer) return array('msg'=>'','body'=>'');
18
19         if (isset($vars['page']) && is_page($vars['page'])) {
20                 $sort = (empty($vars['sort'])) ? '0d' : $vars['sort'];
21                 return array(
22                         'msg'  => $_referer_msg['msg_H0_Refer'],
23                         'body' => plugin_referer_body($vars['page'], $sort));
24         }
25         $pages = get_existpages(TRACKBACK_DIR, '.ref');
26
27         if (empty($pages)) {
28                 return array('msg'=>'', 'body'=>'');
29         } else {
30                 return array(
31                         'msg'  => 'referer list',
32                         'body' => page_list($pages, 'referer', FALSE));
33         }
34 }
35
36 // Referer ÌÀºÙ¹ÔÊÔ½¸
37 function plugin_referer_body($page, $sort)
38 {
39         global $script, $_referer_msg;
40
41         $data = tb_get(tb_get_filename($page, '.ref'));
42         if (empty($data)) return '<p>no data.</p>';
43
44         $bg = plugin_referer_set_color();
45
46         $arrow_last = $arrow_1st = $arrow_ctr = '';
47         $color_last = $color_1st = $color_ctr = $color_ref = $bg['etc'];
48         $sort_last = '0d';
49         $sort_1st  = '1d';
50         $sort_ctr  = '2d';
51
52         switch ($sort) {
53         case '0d': // 0d ºÇ½ª¹¹¿·Æü»þ(¿·Ãå½ç)
54                 usort($data, create_function('$a,$b', 'return $b[0] - $a[0];'));
55                 $color_last = $bg['cur'];
56                 $arrow_last = $_referer_msg['msg_Chr_darr'];
57                 $sort_last = '0a';
58                 break;
59         case '0a': // 0a ºÇ½ª¹¹¿·Æü»þ(ÆüÉÕ½ç)
60                 usort($data, create_function('$a,$b', 'return $a[0] - $b[0];'));
61                 $color_last = $bg['cur'];
62                 $arrow_last = $_referer_msg['msg_Chr_uarr'];
63 //              $sort_last = '0d';
64                 break;
65         case '1d': // 1d ½é²óÅÐÏ¿Æü»þ(¿·Ãå½ç)
66                 usort($data, create_function('$a,$b', 'return $b[1] - $a[1];'));
67                 $color_1st = $bg['cur'];
68                 $arrow_1st = $_referer_msg['msg_Chr_darr'];
69                 $sort_1st = '1a';
70                 break;
71         case '1a': // 1a ½é²óÅÐÏ¿Æü»þ(ÆüÉÕ½ç)
72                 usort($data, create_function('$a,$b', 'return $a[1] - $b[1];'));
73                 $color_1st = $bg['cur'];
74                 $arrow_1st = $_referer_msg['msg_Chr_uarr'];
75 //              $sort_1st = '1d';
76                 break;
77         case '2d': // 2d ¥«¥¦¥ó¥¿(Â礭¤¤½ç)
78                 usort($data, create_function('$a,$b', 'return $b[2] - $a[2];'));
79                 $color_ctr = $bg['cur'];
80                 $arrow_ctr = $_referer_msg['msg_Chr_darr'];
81                 $sort_ctr = '2a';
82                 break;
83         case '2a': // 2a ¥«¥¦¥ó¥¿(¾®¤µ¤¤½ç)
84                 usort($data, create_function('$a,$b', 'return $a[2] - $b[2];'));
85                 $color_ctr = $bg['cur'];
86                 $arrow_ctr = $_referer_msg['msg_Chr_uarr'];
87 //              $sort_ctr = '2d';
88                 break;
89         case '3': // 3 Referer
90                 usort($data, create_function('$a,$b',
91                         'return ($a[3] == $b[3]) ? 0 : (($a[3] > $b[3]) ? 1 : -1);'));
92                 $color_ref = $bg['cur'];
93                 break;
94         }
95
96         $body = '';
97         foreach ($data as $arr) {
98                 // 0:ºÇ½ª¹¹¿·Æü»þ, 1:½é²óÅÐÏ¿Æü»þ, 2:»²¾È¥«¥¦¥ó¥¿, 3:Referer ¥Ø¥Ã¥À, 4:ÍøÍѲÄÈݥե饰(1¤ÏÍ­¸ú)
99                 list($ltime, $stime, $count, $url, $enable) = $arr;
100
101                 // ÈóASCII¥­¥ã¥é¥¯¥¿(¤À¤±)¤òURL¥¨¥ó¥³¡¼¥É¤·¤Æ¤ª¤¯ BugTrack/440
102                 $e_url = htmlspecialchars(preg_replace('/([" \x80-\xff]+)/e', 'rawurlencode("$1")', $url));
103                 $s_url = htmlspecialchars(mb_convert_encoding(rawurldecode($url), SOURCE_ENCODING, 'auto'));
104
105                 $lpass = get_passage($ltime, FALSE); // ºÇ½ª¹¹¿·Æü»þ¤«¤é¤Î·Ð²á»þ´Ö
106                 $spass = get_passage($stime, FALSE); // ½é²óÅÐÏ¿Æü»þ¤«¤é¤Î·Ð²á»þ´Ö
107                 $ldate = get_date($_referer_msg['msg_Fmt_Date'], $ltime); // ºÇ½ª¹¹¿·Æü»þʸ»úÎó
108                 $sdate = get_date($_referer_msg['msg_Fmt_Date'], $stime); // ½é²óÅÐÏ¿Æü»þʸ»úÎó
109
110                 $body .=
111                         ' <tr>' . "\n" .
112                         '  <td>' . $ldate . '</td>' . "\n" .
113                         '  <td>' . $lpass . '</td>' . "\n";
114
115                 $body .= ($count == 1) ?
116                         '  <td colspan="2">N/A</td>' . "\n" :
117                         '  <td>' . $sdate . '</td>' . "\n" .
118                         '  <td>' . $spass . '</td>' . "\n";
119
120                 $body .= '  <td style="text-align:right;">' . $count . '</td>' . "\n";
121
122                 // Å¬ÍÑÉԲĥǡ¼¥¿¤Î¤È¤­¤Ï¥¢¥ó¥«¡¼¤ò¤Ä¤±¤Ê¤¤
123                 $body .= plugin_referer_ignore_check($url) ?
124                         '  <td>' . $s_url . '</td>' . "\n" :
125                         '  <td><a href="' . $e_url . '" rel="nofollow">' . $s_url . '</a></td>' . "\n";
126
127                 $body .= ' </tr>' . "\n";
128         }
129         $href = $script . '?plugin=referer&amp;page=' . rawurlencode($page);
130         return <<<EOD
131 <table border="1" cellspacing="1" summary="Referer">
132  <tr>
133   <td style="background-color:$color_last" colspan="2">
134    <a href="$href&amp;sort=$sort_last">{$_referer_msg['msg_Hed_LastUpdate']}$arrow_last</a>
135   </td>
136   <td style="background-color:$color_1st" colspan="2">
137    <a href="$href&amp;sort=$sort_1st">{$_referer_msg['msg_Hed_1stDate']}$arrow_1st</a>
138   </td>
139   <td style="background-color:$color_ctr;text-align:right">
140    <a href="$href&amp;sort=$sort_ctr">{$_referer_msg['msg_Hed_RefCounter']}$arrow_ctr</a>
141   </td>
142   <td style="background-color:$color_ref">
143    <a href="$href&amp;sort=3">{$_referer_msg['msg_Hed_Referer']}</a>
144    </td>
145  </tr>
146  $body
147 </table>
148 EOD;
149 }
150
151 function plugin_referer_set_color()
152 {
153         static $color;
154
155         if (! isset($color)) {
156                 // Default color
157                 $color = array('cur' => '#88ff88', 'etc' => '#cccccc');
158
159                 $config = new Config(CONFIG_REFERER);
160                 $config->read();
161                 $pconfig_color = $config->get('COLOR');
162                 unset($config);
163
164                 // BGCOLOR(#88ff88)
165                 $matches = array();
166                 foreach ($pconfig_color as $x)
167                         $color[$x[0]] = htmlspecialchars(
168                                 preg_match('/BGCOLOR\(([^)]+)\)/si', $x[1], $matches) ?
169                                         $matches[1] : $x[1]);
170         }
171         return $color;
172 }
173
174 function plugin_referer_ignore_check($url)
175 {
176         static $ignore_url;
177
178         // config.php
179         if (! isset($ignore_url)) {
180                 $config = new Config(CONFIG_REFERER);
181                 $config->read();
182                 $ignore_url = $config->get('IGNORE');
183                 unset($config);
184         }
185
186         foreach ($ignore_url as $x)
187                 if (strpos($url, $x) !== FALSE)
188                         return 1;
189         return 0;
190 }
191 ?>