OSDN Git Service

BugTrack/2394 Add PHP4 Compatibility on constructor
[pukiwiki/pukiwiki.git] / plugin / counter.inc.php
1 <?php
2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: counter.inc.php,v 1.19 2007/02/04 11:14:44 henoheno Exp $
4 // Copyright (C)
5 //   2002-2005, 2007 PukiWiki Developers Team
6 //   2002 Y.MASUI GPL2 http://masui.net/pukiwiki/ masui@masui.net
7 // License: GPL2
8 //
9 // Counter plugin (per page)
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         // Open
73         $file = COUNTER_DIR . encode($page) . PLUGIN_COUNTER_SUFFIX;
74         pkwk_touch_file($file);
75         $fp = fopen($file, 'r+')
76                 or die('counter.inc.php: Cannot open COUNTER_DIR/' . basename($file));
77         set_file_buffer($fp, 0);
78         flock($fp, LOCK_EX);
79         rewind($fp);
80
81         // Read
82         foreach (array_keys($default) as $key) {
83                 // Update
84                 $counters[$page][$key] = rtrim(fgets($fp, 256));
85                 if (feof($fp)) break;
86         }
87
88         // Anothoer day?
89         if ($counters[$page]['date'] != $default['date']) {
90                 $modify = TRUE;
91                 $is_yesterday = ($counters[$page]['date'] == get_date('Y/m/d', UTIME - 24 * 60 * 60));
92                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
93                 $counters[$page]['date']      = $default['date'];
94                 $counters[$page]['yesterday'] = $is_yesterday ? $counters[$page]['today'] : 0;
95                 $counters[$page]['today']     = 1;
96                 $counters[$page]['total']++;
97         } else if ($counters[$page]['ip'] != $_SERVER['REMOTE_ADDR']) {
98                 // Not the same host
99                 $modify = TRUE;
100                 $counters[$page]['ip']        = $_SERVER['REMOTE_ADDR'];
101                 $counters[$page]['today']++;
102                 $counters[$page]['total']++;
103         }
104
105         // Modify
106         if ($modify && $vars['cmd'] == 'read') {
107                 rewind($fp);
108                 ftruncate($fp, 0);
109                 foreach (array_keys($default) as $key)
110                         fputs($fp, $counters[$page][$key] . "\n");
111         }
112
113         // Close
114         flock($fp, LOCK_UN);
115         fclose($fp);
116
117         return $counters[$page];
118 }