[14838] | 1 | <?php |
---|
[14854] | 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 | */ |
---|
[14838] | 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 | |
---|
[14854] | 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 | */ |
---|
[14838] | 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[0] = $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 | /** |
---|
[14854] | 219 | * Automatic prune function called from 'login_success' trigger |
---|
| 220 | * in main.inc.php file |
---|
[14838] | 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[1]) and $conf_PH[1] == 'true' and isset($conf_PH[2]) and $conf_PH[2] <> '0' and isset($conf_PH[3]) and $conf_PH[3] <> '0') |
---|
| 231 | { |
---|
| 232 | if ($conf_PH[3] == '1') // Ranged for days |
---|
| 233 | { |
---|
| 234 | $limit = mktime(0, 0, 0, date("m") , date("d")-$conf_PH[2], date("Y")); |
---|
| 235 | $limit = date('Y-m-d', $limit); |
---|
| 236 | } |
---|
| 237 | else if ($conf_PH[3] == '2') // Ranged for months |
---|
| 238 | { |
---|
| 239 | $limit = mktime(0, 0, 0, date("m")-$conf_PH[2] , date("d"), date("Y")); |
---|
| 240 | $limit = date('Y-m-d', $limit); |
---|
| 241 | } |
---|
| 242 | else if ($conf_PH[3] == '3') // Ranged for years |
---|
| 243 | { |
---|
| 244 | $limit = mktime(0, 0, 0, date("m") , date("d"), date("Y")-$conf_PH[2]); |
---|
| 245 | $limit = date('Y-m-d', $limit); |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | $query = ' |
---|
| 250 | DELETE FROM '.HISTORY_TABLE.' |
---|
| 251 | WHERE date < "'.$limit.'" |
---|
| 252 | AND summarized = "true";'; |
---|
| 253 | |
---|
| 254 | $r = pwg_query($query); |
---|
| 255 | |
---|
| 256 | if($r) |
---|
| 257 | { |
---|
| 258 | $query = 'OPTIMIZE TABLE '.HISTORY_TABLE.';'; |
---|
| 259 | pwg_query($query); |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | |
---|
[14854] | 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; |
---|
[14838] | 274 | |
---|
[14854] | 275 | $plugin = PluginInfos(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 = mysql_fetch_array($res); |
---|
| 310 | $array[1] .= ";\n\n"; |
---|
| 311 | $insertions .= $array[1]; |
---|
| 312 | |
---|
| 313 | $req_table = pwg_query('SELECT * FROM '.$ListTables[$j]) or die(mysql_error()); |
---|
| 314 | $nb_fields = mysql_num_fields($req_table); |
---|
| 315 | while ($line = mysql_fetch_array($req_table)) |
---|
| 316 | { |
---|
| 317 | $insertions .= 'INSERT INTO '.$ListTables[$j].' VALUES ('; |
---|
| 318 | for ($i=0; $i<$nb_fields; $i++) |
---|
| 319 | { |
---|
| 320 | $insertions .= '\'' . pwg_db_real_escape_string($line[$i]) . '\', '; |
---|
| 321 | } |
---|
| 322 | $insertions = substr($insertions, 0, -2); |
---|
| 323 | $insertions .= ");\n"; |
---|
| 324 | } |
---|
| 325 | $insertions .= "\n\n"; |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | fwrite($fp, $insertions); |
---|
| 329 | $j++; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | fclose($fp); |
---|
| 333 | |
---|
| 334 | // Download generated dump file |
---|
| 335 | // ---------------------------- |
---|
| 336 | if ($download == 'true') |
---|
| 337 | { |
---|
| 338 | if (@filesize($Backup_File)) |
---|
| 339 | { |
---|
| 340 | $http_headers = array( |
---|
| 341 | 'Content-Length: '.@filesize($Backup_File), |
---|
| 342 | 'Content-Type: text/x-sql', |
---|
| 343 | 'Content-Disposition: attachment; filename="PH_Historybackup.sql";', |
---|
| 344 | 'Content-Transfer-Encoding: binary', |
---|
| 345 | ); |
---|
| 346 | |
---|
| 347 | foreach ($http_headers as $header) |
---|
| 348 | { |
---|
| 349 | header($header); |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | @readfile($Backup_File); |
---|
| 353 | exit(); |
---|
| 354 | } |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | return true; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | |
---|
[14838] | 361 | /** |
---|
[14854] | 362 | * PH_Restore_backup_file |
---|
| 363 | * Restore backup history table |
---|
| 364 | * |
---|
| 365 | * @returns : Boolean |
---|
| 366 | */ |
---|
| 367 | function PH_Restore_backup_file() |
---|
| 368 | { |
---|
| 369 | global $prefixeTable, $dblayer, $conf; |
---|
| 370 | |
---|
| 371 | define('DEFAULT_PREFIX_TABLE', 'piwigo_'); |
---|
| 372 | |
---|
| 373 | $Backup_File = PH_PATH.'/include/backup/PH_Historybackup.sql'; |
---|
| 374 | |
---|
| 375 | // Restore sql backup file - DROP TABLE queries are executed |
---|
| 376 | // --------------------------------------------------------- |
---|
| 377 | UAM_execute_sqlfile( |
---|
| 378 | $Backup_File, |
---|
| 379 | DEFAULT_PREFIX_TABLE, |
---|
| 380 | $prefixeTable, |
---|
| 381 | $dblayer |
---|
| 382 | ); |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | |
---|
| 386 | /** |
---|
| 387 | * loads an sql file and executes all queries / Based on Piwigo's original install file |
---|
| 388 | * |
---|
| 389 | * Before executing a query, $replaced is... replaced by $replacing. This is |
---|
| 390 | * useful when the SQL file contains generic words. |
---|
| 391 | * |
---|
| 392 | * @param string filepath |
---|
| 393 | * @param string replaced |
---|
| 394 | * @param string replacing |
---|
| 395 | * @return void |
---|
| 396 | */ |
---|
| 397 | function PH_execute_sqlfile($filepath, $replaced, $replacing, $dblayer) |
---|
| 398 | { |
---|
| 399 | $sql_lines = file($filepath); |
---|
| 400 | $query = ''; |
---|
| 401 | foreach ($sql_lines as $sql_line) |
---|
| 402 | { |
---|
| 403 | $sql_line = trim($sql_line); |
---|
| 404 | if (preg_match('/(^--|^$)/', $sql_line)) |
---|
| 405 | { |
---|
| 406 | continue; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | $query.= ' '.$sql_line; |
---|
| 410 | |
---|
| 411 | // if we reached the end of query, we execute it and reinitialize the |
---|
| 412 | // variable "query" |
---|
| 413 | if (preg_match('/;$/', $sql_line)) |
---|
| 414 | { |
---|
| 415 | $query = trim($query); |
---|
| 416 | $query = str_replace($replaced, $replacing, $query); |
---|
| 417 | if ('mysql' == $dblayer) |
---|
| 418 | { |
---|
| 419 | if (preg_match('/^(CREATE TABLE .*)[\s]*;[\s]*/im', $query, $matches)) |
---|
| 420 | { |
---|
| 421 | $query = $matches[1].' DEFAULT CHARACTER SET utf8'.';'; |
---|
| 422 | } |
---|
| 423 | } |
---|
| 424 | pwg_query($query); |
---|
| 425 | $query = ''; |
---|
| 426 | } |
---|
| 427 | } |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | |
---|
| 431 | /** |
---|
[14838] | 432 | * Useful for debugging - 4 vars can be set |
---|
| 433 | * Output result to log.txt file |
---|
| 434 | * |
---|
| 435 | */ |
---|
| 436 | function PHLog($var1, $var2, $var3, $var4) |
---|
| 437 | { |
---|
| 438 | $fo=fopen (PH_PATH.'log.txt','a') ; |
---|
| 439 | fwrite($fo,"======================\n") ; |
---|
| 440 | fwrite($fo,'le ' . date('D, d M Y H:i:s') . "\r\n"); |
---|
| 441 | fwrite($fo,$var1 ."\r\n") ; |
---|
| 442 | fwrite($fo,$var2 ."\r\n") ; |
---|
| 443 | fwrite($fo,$var3 ."\r\n") ; |
---|
| 444 | fwrite($fo,$var4 ."\r\n") ; |
---|
| 445 | fclose($fo) ; |
---|
| 446 | } |
---|
| 447 | ?> |
---|