bugfix: trustedproxy is not a delimited pcre
[dokuwiki.git] / lib / plugins / popularity / helper.php
blobdedd28193421491e688b67896accc303552ba97f
1 <?php
3 use dokuwiki\Extension\AuthPlugin;
4 use dokuwiki\HTTP\DokuHTTPClient;
5 use dokuwiki\Extension\Event;
7 /**
8 * Popularity Feedback Plugin
10 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
12 class helper_plugin_popularity extends Dokuwiki_Plugin
14 /**
15 * The url where the data should be sent
17 public $submitUrl = 'https://update.dokuwiki.org/popularity.php';
19 /**
20 * Name of the file which determine if the the autosubmit is enabled,
21 * and when it was submited for the last time
23 public $autosubmitFile;
25 /**
26 * File where the last error which happened when we tried to autosubmit, will be log
28 public $autosubmitErrorFile;
30 /**
31 * Name of the file which determine when the popularity data was manually
32 * submitted for the last time
33 * (If this file doesn't exist, the data has never been sent)
35 public $popularityLastSubmitFile;
37 /**
38 * helper_plugin_popularity constructor.
40 public function __construct()
42 global $conf;
43 $this->autosubmitFile = $conf['cachedir'] . '/autosubmit.txt';
44 $this->autosubmitErrorFile = $conf['cachedir'] . '/autosubmitError.txt';
45 $this->popularityLastSubmitFile = $conf['cachedir'] . '/lastSubmitTime.txt';
48 /**
49 * Check if autosubmit is enabled
51 * @return boolean TRUE if we should send data once a month, FALSE otherwise
53 public function isAutoSubmitEnabled()
55 return file_exists($this->autosubmitFile);
58 /**
59 * Send the data, to the submit url
61 * @param string $data The popularity data
62 * @return string An empty string if everything worked fine, a string describing the error otherwise
64 public function sendData($data)
66 $error = '';
67 $httpClient = new DokuHTTPClient();
68 $status = $httpClient->sendRequest($this->submitUrl, ['data' => $data], 'POST');
69 if (! $status) {
70 $error = $httpClient->error;
72 return $error;
75 /**
76 * Compute the last time the data was sent. If it has never been sent, we return 0.
78 * @return int
80 public function lastSentTime()
82 $manualSubmission = @filemtime($this->popularityLastSubmitFile);
83 $autoSubmission = @filemtime($this->autosubmitFile);
85 return max((int) $manualSubmission, (int) $autoSubmission);
88 /**
89 * Gather all information
91 * @return string The popularity data as a string
93 public function gatherAsString()
95 $data = $this->gather();
96 $string = '';
97 foreach ($data as $key => $val) {
98 if (is_array($val)) foreach ($val as $v) {
99 $string .= hsc($key) . "\t" . hsc($v) . "\n";
100 } else {
101 $string .= hsc($key) . "\t" . hsc($val) . "\n";
104 return $string;
108 * Initialize an empty list to be used in file traversing
110 * @return array
111 * @see searchCountCallback
113 protected function initEmptySearchList()
115 return $list = array_fill_keys([
116 'file_count',
117 'file_size',
118 'file_max',
119 'file_min',
120 'dir_count',
121 'dir_nest',
122 'file_oldest'
123 ], 0);
127 * Gather all information
129 * @return array The popularity data as an array
131 protected function gather()
133 global $conf;
134 /** @var $auth DokuWiki_Auth_Plugin */
135 global $auth;
136 $data = [];
137 $phptime = ini_get('max_execution_time');
138 @set_time_limit(0);
139 $pluginInfo = $this->getInfo();
141 // version
142 $data['anon_id'] = md5(auth_cookiesalt());
143 $data['version'] = getVersion();
144 $data['popversion'] = $pluginInfo['date'];
145 $data['language'] = $conf['lang'];
146 $data['now'] = time();
147 $data['popauto'] = (int) $this->isAutoSubmitEnabled();
149 // some config values
150 $data['conf_useacl'] = $conf['useacl'];
151 $data['conf_authtype'] = $conf['authtype'];
152 $data['conf_template'] = $conf['template'];
154 // number and size of pages
155 $list = $this->initEmptySearchList();
156 search($list, $conf['datadir'], [$this, 'searchCountCallback'], ['all' => false], '');
157 $data['page_count'] = $list['file_count'];
158 $data['page_size'] = $list['file_size'];
159 $data['page_biggest'] = $list['file_max'];
160 $data['page_smallest'] = $list['file_min'];
161 $data['page_nscount'] = $list['dir_count'];
162 $data['page_nsnest'] = $list['dir_nest'];
163 if ($list['file_count']) $data['page_avg'] = $list['file_size'] / $list['file_count'];
164 $data['page_oldest'] = $list['file_oldest'];
165 unset($list);
167 // number and size of media
168 $list = $this->initEmptySearchList();
169 search($list, $conf['mediadir'], [$this, 'searchCountCallback'], ['all' => true]);
170 $data['media_count'] = $list['file_count'];
171 $data['media_size'] = $list['file_size'];
172 $data['media_biggest'] = $list['file_max'];
173 $data['media_smallest'] = $list['file_min'];
174 $data['media_nscount'] = $list['dir_count'];
175 $data['media_nsnest'] = $list['dir_nest'];
176 if ($list['file_count']) $data['media_avg'] = $list['file_size'] / $list['file_count'];
177 unset($list);
179 // number and size of cache
180 $list = $this->initEmptySearchList();
181 search($list, $conf['cachedir'], [$this, 'searchCountCallback'], ['all' => true]);
182 $data['cache_count'] = $list['file_count'];
183 $data['cache_size'] = $list['file_size'];
184 $data['cache_biggest'] = $list['file_max'];
185 $data['cache_smallest'] = $list['file_min'];
186 if ($list['file_count']) $data['cache_avg'] = $list['file_size'] / $list['file_count'];
187 unset($list);
189 // number and size of index
190 $list = $this->initEmptySearchList();
191 search($list, $conf['indexdir'], [$this, 'searchCountCallback'], ['all' => true]);
192 $data['index_count'] = $list['file_count'];
193 $data['index_size'] = $list['file_size'];
194 $data['index_biggest'] = $list['file_max'];
195 $data['index_smallest'] = $list['file_min'];
196 if ($list['file_count']) $data['index_avg'] = $list['file_size'] / $list['file_count'];
197 unset($list);
199 // number and size of meta
200 $list = $this->initEmptySearchList();
201 search($list, $conf['metadir'], [$this, 'searchCountCallback'], ['all' => true]);
202 $data['meta_count'] = $list['file_count'];
203 $data['meta_size'] = $list['file_size'];
204 $data['meta_biggest'] = $list['file_max'];
205 $data['meta_smallest'] = $list['file_min'];
206 if ($list['file_count']) $data['meta_avg'] = $list['file_size'] / $list['file_count'];
207 unset($list);
209 // number and size of attic
210 $list = $this->initEmptySearchList();
211 search($list, $conf['olddir'], [$this, 'searchCountCallback'], ['all' => true]);
212 $data['attic_count'] = $list['file_count'];
213 $data['attic_size'] = $list['file_size'];
214 $data['attic_biggest'] = $list['file_max'];
215 $data['attic_smallest'] = $list['file_min'];
216 if ($list['file_count']) $data['attic_avg'] = $list['file_size'] / $list['file_count'];
217 $data['attic_oldest'] = $list['file_oldest'];
218 unset($list);
220 // user count
221 if ($auth instanceof AuthPlugin && $auth->canDo('getUserCount')) {
222 $data['user_count'] = $auth->getUserCount();
225 // calculate edits per day
226 $list = (array) @file($conf['metadir'] . '/_dokuwiki.changes');
227 $count = count($list);
228 if ($count > 2) {
229 $first = (int) substr(array_shift($list), 0, 10);
230 $last = (int) substr(array_pop($list), 0, 10);
231 $dur = ($last - $first) / (60 * 60 * 24); // number of days in the changelog
232 $data['edits_per_day'] = $count / $dur;
234 unset($list);
236 // plugins
237 $data['plugin'] = plugin_list();
239 // pcre info
240 if (defined('PCRE_VERSION')) $data['pcre_version'] = PCRE_VERSION;
241 $data['pcre_backtrack'] = ini_get('pcre.backtrack_limit');
242 $data['pcre_recursion'] = ini_get('pcre.recursion_limit');
244 // php info
245 $data['os'] = PHP_OS;
246 $data['webserver'] = $_SERVER['SERVER_SOFTWARE'];
247 $data['php_version'] = phpversion();
248 $data['php_sapi'] = PHP_SAPI;
249 $data['php_memory'] = php_to_byte(ini_get('memory_limit'));
250 $data['php_exectime'] = $phptime;
251 $data['php_extension'] = get_loaded_extensions();
253 // plugin usage data
254 $this->addPluginUsageData($data);
256 return $data;
260 * Triggers event to let plugins add their own data
262 * @param $data
264 protected function addPluginUsageData(&$data)
266 $pluginsData = [];
267 Event::createAndTrigger('PLUGIN_POPULARITY_DATA_SETUP', $pluginsData);
268 foreach ($pluginsData as $plugin => $d) {
269 if (is_array($d)) {
270 foreach ($d as $key => $value) {
271 $data['plugin_' . $plugin . '_' . $key] = $value;
273 } else {
274 $data['plugin_' . $plugin] = $d;
280 * Callback to search and count the content of directories in DokuWiki
282 * @param array &$data Reference to the result data structure
283 * @param string $base Base usually $conf['datadir']
284 * @param string $file current file or directory relative to $base
285 * @param string $type Type either 'd' for directory or 'f' for file
286 * @param int $lvl Current recursion depht
287 * @param array $opts option array as given to search()
288 * @return bool
290 public function searchCountCallback(&$data, $base, $file, $type, $lvl, $opts)
292 // traverse
293 if ($type == 'd') {
294 if ($data['dir_nest'] < $lvl) $data['dir_nest'] = $lvl;
295 $data['dir_count']++;
296 return true;
299 //only search txt files if 'all' option not set
300 if ($opts['all'] || str_ends_with($file, '.txt')) {
301 $size = filesize($base . '/' . $file);
302 $date = filemtime($base . '/' . $file);
303 $data['file_count']++;
304 $data['file_size'] += $size;
305 if (!isset($data['file_min']) || $data['file_min'] > $size) $data['file_min'] = $size;
306 if ($data['file_max'] < $size) $data['file_max'] = $size;
307 if (!isset($data['file_oldest']) || $data['file_oldest'] > $date) $data['file_oldest'] = $date;
310 return false;