OSDN Git Service

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