Ignore:
Timestamp:
May 8, 2012, 4:18:51 PM (12 years ago)
Author:
Eric
Message:

Fist 1.0.0 release

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/Prune_History/include/functions.inc.php

    r14838 r14854  
    11<?php
    2 /* Function called from main.inc.php to get the plugin version and name */
     2/**
     3 * Plugin administration menu
     4 */
     5function 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 */
    330function PHInfos($dir)
    431{
     
    4673
    4774
    48 /* *************************************** */
    49 /* Update plugin version in conf table     */
    50 /* Used everytime a new version is updated */
    51 /* even if no database upgrade is needed   */
    52 /* *************************************** */
     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 */
    5380function PH_version_update()
    5481{
     
    190217
    191218/**
    192  * Automatic prune function
    193  *
    194  * @param : Start and stop range to prune
    195  *
    196  * @return : boolean
     219 * Automatic prune function called from 'login_success' trigger
     220 * in main.inc.php file
     221 *
    197222 */
    198223function history_autoprune()
     
    236261}
    237262
     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 */
     271function PH_dump($download)
     272{
     273  global $conf;
     274
     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
     361/**
     362 * PH_Restore_backup_file
     363 * Restore backup history table
     364 *
     365 * @returns : Boolean
     366 */
     367function 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 */
     397function 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}
    238429
    239430
Note: See TracChangeset for help on using the changeset viewer.