OSDN Git Service

showrss: Added about PLUGIN_SHOWRSS_USAGE (by Nibun-no-ni)
[pukiwiki/pukiwiki.git] / plugin / counter.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: counter.inc.php,v 1.18 2006/05/27 13:31:11 henoheno Exp $
4 // Copyright (C)
5 //   2002-2005 PukiWiki Developers Team
6 //   2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
7 // License: GPL2
8 //
9 // Counter plugin
10
11 // Counter file's suffix
12 define('PLUGIN_COUNTER_SUFFIX', '.count');
13
14 // Report one
15 function plugin_counter_inline()
16 {
17         global $vars;
18
19         // BugTrack2/106: Only variables can be passed by reference from PHP 5.0.5
20         $args = func_get_args(); // with array_shift()
21
22         $arg = strtolower(array_shift($args));
23         switch ($arg) {
24         case ''     : $arg = 'total'; /*FALLTHROUGH*/
25         case 'total': /*FALLTHROUGH*/
26         case 'today': /*FALLTHROUGH*/
27         case 'yesterday':
28                 $counter = plugin_counter_get_count($vars['page']);
29                 return $counter[$arg];
30         default:
31                 return '&counter([total|today|yesterday]);';
32         }
33 }
34
35 // Report all
36 function plugin_counter_convert()
37 {
38         global $vars;
39
40         $counter = plugin_counter_get_count($vars['page']);
41         return <<<EOD
42 <div class="counter">
43 Counter:   {$counter['total']},
44 today:     {$counter['today']},
45 yesterday: {$counter['yesterday']}
46 </div>
47 EOD;
48 }
49
50 // Return a summary
51 function plugin_counter_get_count($page)
52 {
53         global $vars;
54         static $counters = array();
55         static $default;
56
57         if (! isset($default))
58                 $default = array(
59                         'total'     => 0,
60                         'date'      => get_date('Y/m/d'),
61                         'today'     => 0,
62                         'yesterday' => 0,
63                         'ip'        => '');
64
65         if (! is_page($page)) return $default;
66         if (isset($counters[$page])) return $counters[$page];
67
68         // Set default
69         $counters[$page] = $default;
70         $modify = FALSE;
71
72         $file = COUNTER_DIR . encode($page) . PLUGIN_COUNTER_SUFFIX;
73         $fp = fopen($file, file_exists($file) ? 'r+' : 'w+')
74                 or die('counter.inc.php: Cannot open COUTER_DIR/' . basename($file));
75         set_file_buffer($fp, 0);
76         flock($fp, LOCK_EX);
77         rewind($fp);
78         foreach ($default as $key=>$val) {
79                 // Update
80                 $counters[$page][$key] = rtrim(fgets($fp, 256));
81                 if (feof($fp)) break;
82         }
83         if ($counters[$page]['date'] != $default['date']) {
84                 // New day
85                 $modify = TRUE;
86                 $is_yesterday = ($counters[$page]['date'] == get_date('Y/m/d', strtotime('yesterday', UTIME)));
87                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
88                 $counters[$page]['date']      = $default['date'];
89                 $counters[$page]['yesterday'] = $is_yesterday ? $counters[$page]['today'] : 0;
90                 $counters[$page]['today']     = 1;
91                 $counters[$page]['total']++;
92
93         } else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR']) {
94                 // Not the same host
95                 $modify = TRUE;
96                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
97                 $counters[$page]['today']++;
98                 $counters[$page]['total']++;
99         }
100         // Modify
101         if ($modify && $vars['cmd'] == 'read') {
102                 rewind($fp);
103                 ftruncate($fp, 0);
104                 foreach (array_keys($default) as $key)
105                         fputs($fp, $counters[$page][$key] . "\n");
106         }
107         flock($fp, LOCK_UN);
108         fclose($fp);
109
110         return $counters[$page];
111 }
112 ?>