1 | <?php |
---|
2 | /** |
---|
3 | * Plugin administration menu |
---|
4 | */ |
---|
5 | function PH_admin_menu($menu) |
---|
6 | { |
---|
7 | // Retreive plugin name |
---|
8 | $plugin = PHInfos(PH_PATH); |
---|
9 | $name = $plugin['name']; |
---|
10 | |
---|
11 | array_push($menu, |
---|
12 | array( |
---|
13 | 'NAME' => $name, |
---|
14 | 'URL' => get_root_url().'admin.php?page=plugin-'.basename(PH_PATH) |
---|
15 | ) |
---|
16 | ); |
---|
17 | |
---|
18 | return $menu; |
---|
19 | } |
---|
20 | |
---|
21 | |
---|
22 | /** |
---|
23 | * Function to retreive some plugin information like version and name |
---|
24 | * stored in main.inc.php file |
---|
25 | * |
---|
26 | * @param : Path to plugin |
---|
27 | * |
---|
28 | * @return : Array of retreived information |
---|
29 | */ |
---|
30 | function PHInfos($dir) |
---|
31 | { |
---|
32 | $path = $dir; |
---|
33 | |
---|
34 | $plg_data = implode( '', file($path.'main.inc.php') ); |
---|
35 | if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) ) |
---|
36 | { |
---|
37 | $plugin['name'] = trim( $val[1] ); |
---|
38 | } |
---|
39 | if (preg_match("|Version: (.*)|", $plg_data, $val)) |
---|
40 | { |
---|
41 | $plugin['version'] = trim($val[1]); |
---|
42 | } |
---|
43 | if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) ) |
---|
44 | { |
---|
45 | $plugin['uri'] = trim($val[1]); |
---|
46 | } |
---|
47 | if ($desc = load_language('description.txt', $path.'/', array('return' => true))) |
---|
48 | { |
---|
49 | $plugin['description'] = trim($desc); |
---|
50 | } |
---|
51 | elseif ( preg_match("|Description: (.*)|", $plg_data, $val) ) |
---|
52 | { |
---|
53 | $plugin['description'] = trim($val[1]); |
---|
54 | } |
---|
55 | if ( preg_match("|Author: (.*)|", $plg_data, $val) ) |
---|
56 | { |
---|
57 | $plugin['author'] = trim($val[1]); |
---|
58 | } |
---|
59 | if ( preg_match("|Author URI: (.*)|", $plg_data, $val) ) |
---|
60 | { |
---|
61 | $plugin['author uri'] = trim($val[1]); |
---|
62 | } |
---|
63 | if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid=')) |
---|
64 | { |
---|
65 | list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']); |
---|
66 | if (is_numeric($extension)) $plugin['extension'] = $extension; |
---|
67 | } |
---|
68 | // IMPORTANT SECURITY ! |
---|
69 | $plugin = array_map('htmlspecialchars', $plugin); |
---|
70 | |
---|
71 | return $plugin ; |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | /** |
---|
76 | * Function to update plugin version number in config table |
---|
77 | * Used everytime a new version is updated even if no database |
---|
78 | * upgrade is needed |
---|
79 | */ |
---|
80 | function PH_version_update() |
---|
81 | { |
---|
82 | global $conf; |
---|
83 | |
---|
84 | load_language('plugin.lang', PH_PATH); |
---|
85 | |
---|
86 | // Get current plugin version |
---|
87 | // -------------------------- |
---|
88 | $plugin = PHInfos(PH_PATH); |
---|
89 | $version = $plugin['version']; |
---|
90 | |
---|
91 | // Upgrading options |
---|
92 | // ----------------- |
---|
93 | $query = ' |
---|
94 | SELECT value |
---|
95 | FROM '.CONFIG_TABLE.' |
---|
96 | WHERE param = "PruneHistory" |
---|
97 | ;'; |
---|
98 | |
---|
99 | $result = pwg_query($query); |
---|
100 | $conf_PH = pwg_db_fetch_assoc($result); |
---|
101 | |
---|
102 | $Newconf_PH = unserialize($conf_PH['value']); |
---|
103 | |
---|
104 | $Newconf_PH['PHVersion'] = $version; |
---|
105 | |
---|
106 | $update_conf = serialize($Newconf_PH); |
---|
107 | |
---|
108 | conf_update_param('PruneHistory', pwg_db_real_escape_string($update_conf)); |
---|
109 | |
---|
110 | |
---|
111 | // Check #_plugin table consistency |
---|
112 | // Only useful if a previous version upgrade has not worked correctly (rare case) |
---|
113 | // ------------------------------------------------------------------------------ |
---|
114 | $query = ' |
---|
115 | SELECT version |
---|
116 | FROM '.PLUGINS_TABLE.' |
---|
117 | WHERE id = "PruneHistory" |
---|
118 | ;'; |
---|
119 | |
---|
120 | $data = pwg_db_fetch_assoc(pwg_query($query)); |
---|
121 | |
---|
122 | if (empty($data['version']) or $data['version'] <> $version) |
---|
123 | { |
---|
124 | $query = ' |
---|
125 | UPDATE '.PLUGINS_TABLE.' |
---|
126 | SET version="'.$version.'" |
---|
127 | WHERE id = "PruneHistory" |
---|
128 | LIMIT 1 |
---|
129 | ;'; |
---|
130 | |
---|
131 | pwg_query($query); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /** |
---|
137 | * Delete obsolete files on plugin upgrade |
---|
138 | * Obsolete files are listed in file obsolete.list |
---|
139 | * |
---|
140 | */ |
---|
141 | function PH_Obsolete_Files() |
---|
142 | { |
---|
143 | if (file_exists(PH_PATH.'obsolete.list') |
---|
144 | and $old_files = file(PH_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES) |
---|
145 | and !empty($old_files)) |
---|
146 | { |
---|
147 | array_push($old_files, 'obsolete.list'); |
---|
148 | foreach($old_files as $old_file) |
---|
149 | { |
---|
150 | $path = PH_PATH.$old_file; |
---|
151 | if (is_file($path)) |
---|
152 | { |
---|
153 | @unlink($path); |
---|
154 | } |
---|
155 | elseif (is_dir($path)) |
---|
156 | { |
---|
157 | @rmdir($path); |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | /** |
---|
165 | * Manual prune function |
---|
166 | * |
---|
167 | * @param : Start and stop range to prune |
---|
168 | * |
---|
169 | * @return : boolean |
---|
170 | */ |
---|
171 | function history_prune($startdate,$stopdate) |
---|
172 | { |
---|
173 | list($year, $month, $day) = explode('-', $startdate); // explode date of $startdate |
---|
174 | $starttimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp |
---|
175 | |
---|
176 | list($year, $month, $day) = explode('-', $stopdate); // explode date of $stopdate |
---|
177 | $stoptimestamp = mktime(0, 0, 0, $month, $day, $year);// Generate UNIX timestamp |
---|
178 | |
---|
179 | if ($starttimestamp < $stoptimestamp) |
---|
180 | { |
---|
181 | $query = ' |
---|
182 | DELETE |
---|
183 | FROM '.HISTORY_TABLE.' |
---|
184 | WHERE date < "'.$stopdate.'" |
---|
185 | AND date > "'.$startdate.'" |
---|
186 | AND summarized = "true";'; |
---|
187 | |
---|
188 | $r = pwg_query($query); |
---|
189 | |
---|
190 | if ($r) |
---|
191 | { |
---|
192 | $query = 'OPTIMIZE TABLE '.HISTORY_TABLE.';'; |
---|
193 | pwg_query($query); |
---|
194 | } |
---|
195 | return true; |
---|
196 | } |
---|
197 | else if ($starttimestamp == $stoptimestamp) |
---|
198 | { |
---|
199 | $query = ' |
---|
200 | DELETE FROM '.HISTORY_TABLE.' |
---|
201 | WHERE date = "'.$stopdate.'" |
---|
202 | AND summarized = "true";'; |
---|
203 | |
---|
204 | $r = pwg_query($query); |
---|
205 | |
---|
206 | if ($r) |
---|
207 | { |
---|
208 | $query = 'OPTIMIZE TABLE '.HISTORY_TABLE.';'; |
---|
209 | pwg_query($query); |
---|
210 | } |
---|
211 | return true; |
---|
212 | } |
---|
213 | |
---|
214 | return false; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | /** |
---|
219 | * Automatic prune function called from 'login_success' trigger |
---|
220 | * in main.inc.php file |
---|
221 | * |
---|
222 | */ |
---|
223 | function history_autoprune() |
---|
224 | { |
---|
225 | global $conf; |
---|
226 | |
---|
227 | $conf_PH = unserialize($conf['PruneHistory']); |
---|
228 | |
---|
229 | // Definition of the range to keep |
---|
230 | if (isset($conf_PH['AUTOPRUNE']) and $conf_PH['AUTOPRUNE'] == 'true' and isset($conf_PH['RANGEVALUE']) and $conf_PH['RANGEVALUE'] <> '0' and isset($conf_PH['RANGE']) and $conf_PH['RANGE'] <> '0') |
---|
231 | { |
---|
232 | if ($conf_PH['RANGE'] == '1') // Ranged for days |
---|
233 | { |
---|
234 | $limit = mktime(0, 0, 0, date("m") , date("d")-$conf_PH['RANGEVALUE'], date("Y")); |
---|
235 | $limit = date('Y-m-d', $limit); |
---|
236 | } |
---|
237 | else if ($conf_PH['RANGE'] == '2') // Ranged for months |
---|
238 | { |
---|
239 | $limit = mktime(0, 0, 0, date("m")-$conf_PH['RANGEVALUE'] , date("d"), date("Y")); |
---|
240 | $limit = date('Y-m-d', $limit); |
---|
241 | } |
---|
242 | else if ($conf_PH['RANGE'] == '3') // Ranged for years |
---|
243 | { |
---|
244 | $limit = mktime(0, 0, 0, date("m") , date("d"), date("Y")-$conf_PH['RANGEVALUE']); |
---|
245 | $limit = date('Y-m-d', $limit); |
---|
246 | } |
---|
247 | |
---|
248 | $query = ' |
---|
249 | DELETE FROM '.HISTORY_TABLE.' |
---|
250 | WHERE date < "'.$limit.'" |
---|
251 | AND summarized = "true";'; |
---|
252 | |
---|
253 | $r = pwg_query($query); |
---|
254 | |
---|
255 | if($r) |
---|
256 | { |
---|
257 | $query = 'OPTIMIZE TABLE '.HISTORY_TABLE.';'; |
---|
258 | pwg_query($query); |
---|
259 | } |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | /** |
---|
265 | * PH specific database dump |
---|
266 | * Creates an SQL dump of history table for safety before manual prune |
---|
267 | * |
---|
268 | * @returns : Boolean to manage appropriate message display |
---|
269 | * |
---|
270 | */ |
---|
271 | function PH_dump($download) |
---|
272 | { |
---|
273 | global $conf; |
---|
274 | |
---|
275 | $plugin = PHInfos(PH_PATH); |
---|
276 | $version = $plugin['version']; |
---|
277 | |
---|
278 | // Initial backup folder creation and file initialisation |
---|
279 | // ------------------------------------------------------ |
---|
280 | if (!is_dir(PH_PATH.'/include/backup')) |
---|
281 | mkdir(PH_PATH.'/include/backup'); |
---|
282 | |
---|
283 | $Backup_File = PH_PATH.'/include/backup/PH_Historybackup.sql'; |
---|
284 | |
---|
285 | $fp = fopen($Backup_File, 'w'); |
---|
286 | |
---|
287 | // Writing plugin version |
---|
288 | $insertions = "-- ".$version." --\n\n"; |
---|
289 | fwrite($fp, $insertions); |
---|
290 | |
---|
291 | // Saving History table |
---|
292 | // -------------------- |
---|
293 | $ListTables = array(HISTORY_TABLE); |
---|
294 | $j=0; |
---|
295 | |
---|
296 | while($j < count($ListTables)) |
---|
297 | { |
---|
298 | $sql = 'SHOW CREATE TABLE '.$ListTables[$j]; |
---|
299 | $res = pwg_query($sql); |
---|
300 | |
---|
301 | if ($res) |
---|
302 | { |
---|
303 | $insertions = "-- -------------------------------------------------------\n"; |
---|
304 | $insertions .= "-- Create ".$ListTables[$j]." table\n"; |
---|
305 | $insertions .= "-- ------------------------------------------------------\n\n"; |
---|
306 | |
---|
307 | $insertions .= "DROP TABLE IF EXISTS ".$ListTables[$j].";\n\n"; |
---|
308 | |
---|
309 | $array = pwg_db_fetch_row($res); |
---|
310 | $array[1] .= ";\n\n"; |
---|
311 | $insertions .= $array[1]; |
---|
312 | |
---|
313 | $req_table = pwg_query('DESCRIBE '.$ListTables[$j].';') or die(my_error()); |
---|
314 | $nb_fields = pwg_db_num_rows($req_table); |
---|
315 | $req_table2 = pwg_query('SELECT * FROM '.$ListTables[$j]) or die(my_error()); |
---|
316 | |
---|
317 | while ($line = pwg_db_fetch_row($req_table2)) |
---|
318 | { |
---|
319 | $insertions .= 'INSERT INTO '.$ListTables[$j].' VALUES ('; |
---|
320 | for ($i=0; $i<$nb_fields; $i++) |
---|
321 | { |
---|
322 | $insertions .= '\'' . pwg_db_real_escape_string($line[$i]) . '\', '; |
---|
323 | } |
---|
324 | $insertions = substr($insertions, 0, -2); |
---|
325 | $insertions .= ");\n"; |
---|
326 | } |
---|
327 | $insertions .= "\n\n"; |
---|
328 | } |
---|
329 | |
---|
330 | fwrite($fp, $insertions); |
---|
331 | $j++; |
---|
332 | } |
---|
333 | |
---|
334 | fclose($fp); |
---|
335 | |
---|
336 | // Download generated dump file |
---|
337 | // ---------------------------- |
---|
338 | if ($download == 'true') |
---|
339 | { |
---|
340 | if (@filesize($Backup_File)) |
---|
341 | { |
---|
342 | $http_headers = array( |
---|
343 | 'Content-Length: '.@filesize($Backup_File), |
---|
344 | 'Content-Type: text/x-sql', |
---|
345 | 'Content-Disposition: attachment; filename="PH_Historybackup.sql";', |
---|
346 | 'Content-Transfer-Encoding: binary', |
---|
347 | ); |
---|
348 | |
---|
349 | foreach ($http_headers as $header) |
---|
350 | { |
---|
351 | header($header); |
---|
352 | } |
---|
353 | |
---|
354 | @readfile($Backup_File); |
---|
355 | exit(); |
---|
356 | } |
---|
357 | } |
---|
358 | |
---|
359 | return true; |
---|
360 | } |
---|
361 | |
---|
362 | |
---|
363 | /** |
---|
364 | * PH_Restore_backup_file |
---|
365 | * Restore backup history table |
---|
366 | * |
---|
367 | * @returns : Boolean |
---|
368 | */ |
---|
369 | function PH_Restore_backup_file() |
---|
370 | { |
---|
371 | global $prefixeTable, $dblayer, $conf; |
---|
372 | |
---|
373 | define('DEFAULT_PREFIX_TABLE', 'piwigo_'); |
---|
374 | |
---|
375 | $Backup_File = PH_PATH.'/include/backup/PH_Historybackup.sql'; |
---|
376 | |
---|
377 | // Restore sql backup file - DROP TABLE queries are executed |
---|
378 | // --------------------------------------------------------- |
---|
379 | PH_execute_sqlfile( |
---|
380 | $Backup_File, |
---|
381 | DEFAULT_PREFIX_TABLE, |
---|
382 | $prefixeTable, |
---|
383 | $dblayer |
---|
384 | ); |
---|
385 | } |
---|
386 | |
---|
387 | |
---|
388 | /** |
---|
389 | * loads an sql file and executes all queries / Based on Piwigo's original install file |
---|
390 | * |
---|
391 | * Before executing a query, $replaced is... replaced by $replacing. This is |
---|
392 | * useful when the SQL file contains generic words. |
---|
393 | * |
---|
394 | * @param string filepath |
---|
395 | * @param string replaced |
---|
396 | * @param string replacing |
---|
397 | * @return void |
---|
398 | */ |
---|
399 | function PH_execute_sqlfile($filepath, $replaced, $replacing, $dblayer) |
---|
400 | { |
---|
401 | $sql_lines = file($filepath); |
---|
402 | $query = ''; |
---|
403 | foreach ($sql_lines as $sql_line) |
---|
404 | { |
---|
405 | $sql_line = trim($sql_line); |
---|
406 | if (preg_match('/(^--|^$)/', $sql_line)) |
---|
407 | { |
---|
408 | continue; |
---|
409 | } |
---|
410 | |
---|
411 | $query.= ' '.$sql_line; |
---|
412 | |
---|
413 | // if we reached the end of query, we execute it and reinitialize the |
---|
414 | // variable "query" |
---|
415 | if (preg_match('/;$/', $sql_line)) |
---|
416 | { |
---|
417 | $query = trim($query); |
---|
418 | $query = str_replace($replaced, $replacing, $query); |
---|
419 | if ('mysql' == $dblayer) |
---|
420 | { |
---|
421 | if (preg_match('/^(CREATE TABLE .*)[\s]*;[\s]*/im', $query, $matches)) |
---|
422 | { |
---|
423 | $query = $matches[1].' DEFAULT CHARACTER SET utf8'.';'; |
---|
424 | } |
---|
425 | } |
---|
426 | pwg_query($query); |
---|
427 | $query = ''; |
---|
428 | } |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | |
---|
433 | /** |
---|
434 | * Useful for debugging - 4 vars can be set |
---|
435 | * Output result to log.txt file |
---|
436 | * |
---|
437 | */ |
---|
438 | function PHLog($var1, $var2, $var3, $var4) |
---|
439 | { |
---|
440 | $fo=fopen (PH_PATH.'log.txt','a') ; |
---|
441 | fwrite($fo,"======================\n") ; |
---|
442 | fwrite($fo,'le ' . date('D, d M Y H:i:s') . "\r\n"); |
---|
443 | fwrite($fo,$var1 ."\r\n") ; |
---|
444 | fwrite($fo,$var2 ."\r\n") ; |
---|
445 | fwrite($fo,$var3 ."\r\n") ; |
---|
446 | fwrite($fo,$var4 ."\r\n") ; |
---|
447 | fclose($fo) ; |
---|
448 | } |
---|
449 | ?> |
---|