b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (CVS HEAD) [ class tree: main ] [ index: main ] [ all elements ]

Source for file _maintenance.funcs.php

Documentation is available at _maintenance.funcs.php

  1. <?php
  2.  
  3. if!defined('EVO_MAIN_INIT') ) die'Please, do not access this page directly.' );
  4.  
  5.  
  6. /**
  7.  * Check version
  8.  * @param new version dir name
  9.  * @return string message or NULL
  10.  */
  11. function check_version$new_version_dir )
  12. {
  13.     global $install_subdir$install_path$upgrade_path;
  14.     // Upgrade database using regular upgrader script
  15.     require_once$install_path.'/_version.php' );
  16.     require_once$install_path.'/_version.php' );
  17.  
  18.     $new_version_file $upgrade_path.$new_version_dir.'/'.$install_subdir.'_version.php';
  19.     $current_version_file $install_path.'/_version.php';
  20.  
  21.     if!file_exists$current_version_file ) )
  22.     {
  23.         return T_'Installed version doesn\'t support upgrade!' );
  24.     }
  25.  
  26.     require$new_version_file );
  27.     $new_version $current_version;
  28.  
  29.     unset$current_version );
  30.  
  31.     require$current_version_file );
  32.     $current_version $current_version;
  33.  
  34.     if$new_version == $current_version )
  35.     {
  36.         return T_'This package is already installed!' );
  37.     }
  38.     elseif$new_version $current_version )
  39.     {
  40.         return T_'This is an old version!' );
  41.     }
  42.  
  43.     return NULL;
  44. }
  45.  
  46.  
  47. /**
  48.  * Set max execution time
  49.  * @param integer seconds
  50.  */
  51. function set_max_execution_time$seconds )
  52. {
  53.     iffunction_exists'set_time_limit' ) )
  54.     {
  55.         set_time_limit$seconds );
  56.     }
  57.     @ini_set'max_execution_time'$seconds );
  58. }
  59.  
  60.  
  61. /**
  62.  * Enable/disable maintenance mode
  63.  *
  64.  * @param boolean true if maintenance mode need to be enabled
  65.  * @param string maintenance mode message
  66.  */
  67. function switch_maintenance_mode$enable$msg '' )
  68. {
  69.     global $conf_path;
  70.  
  71.     $maintenance_mode_file 'maintenance.txt';
  72.  
  73.     if$enable )
  74.     {    // Create maintenance file
  75.         echo '<p>'.T_('Switching to maintenance mode...').'</p>';
  76.         flush();
  77.  
  78.         $f @fopen$conf_path.$maintenance_mode_file 'w+' );
  79.         if$f == false )
  80.         {    // Maintenance file has not been created
  81.             echo '<p style="color:red">'.sprintfT_'Unable to switch maintenance mode. Maintenance file can\'t be created: &laquo;%s&raquo;' )$maintenance_mode_file ).'</p>';
  82.             flush();
  83.  
  84.             return false;
  85.         }
  86.         else
  87.         {    // Write content
  88.             fwrite$f$msg );
  89.             fclose($f);
  90.         }
  91.     }
  92.     else
  93.     {    // Delete maintenance file
  94.         echo '<p>'.T_('Switching out of maintenance mode...').'</p>';
  95.         unlink$conf_path.$maintenance_mode_file );
  96.     }
  97.  
  98.     return true;
  99. }
  100.  
  101.  
  102. /**
  103.  * Prepare maintenance directory
  104.  *
  105.  * @param string directory path
  106.  * @param boolean create .htaccess file with 'deny from all' text
  107.  * @return boolean 
  108.  */
  109. function prepare_maintenance_dir$dir_name$deny_access false )
  110. {
  111.     if!file_exists$dir_name ) )
  112.     {    // We can create directory
  113.         if mkdir_r$dir_name ) )
  114.         {
  115.             echo '<p style="color:red">'.sprintfT_'Unable to create &laquo;%s&raquo; directory.' )$dir_name ).'</p>';
  116.             flush();
  117.  
  118.             return false;
  119.         }
  120.     }
  121.  
  122.     if$deny_access )
  123.     {    // Create .htaccess file
  124.         $htaccess_name $dir_name.'.htaccess';
  125.  
  126.         if!file_exists$htaccess_name ) )
  127.         {    // We can create .htaccess file
  128.             $f @fopen$htaccess_name 'w+' );
  129.             if$f == false )
  130.             {
  131.                 echo '<p style="color:red">'.sprintfT_'Unable to create &laquo;%s&raquo; file in directory.' )$htaccess_name ).'</p>';
  132.                 flush();
  133.  
  134.                 return false;
  135.             }
  136.             else
  137.             {    // Write content
  138.                 fwrite$f'deny from all' );
  139.                 fclose($f);
  140.             }
  141.         }
  142.     }
  143.  
  144.     return true;
  145. }
  146.  
  147.  
  148. /**
  149.  * Unpack ZIP archive to destination directory
  150.  *
  151.  * @param string source file path
  152.  * @param string destination directory path
  153.  * @param boolean true if create destination directory
  154.  * @return boolean results
  155.  */
  156. function unpack_archive$src_file$dest_dir$mk_dest_dir false )
  157. {
  158.     global $inc_path;
  159.  
  160.     if!file_exists$dest_dir ) )
  161.     {    // We can create directory
  162.         if !mkdir_r$dest_dir ) )
  163.         {
  164.             echo '<p style="color:red">'.sprintfT_'Unable to create &laquo;%s&raquo; directory.' )$dest_dir ).'</p>';
  165.             flush();
  166.  
  167.             return false;
  168.         }
  169.     }
  170.  
  171.     iffunction_exists('gzopen') )
  172.     {    // Unpack using 'zlib' extension and PclZip wrapper
  173.  
  174.         // Load PclZip class (PHP4):
  175.         load_class'_ext/pclzip/pclzip.lib.php''PclZip' );
  176.  
  177.         $PclZip new PclZip$src_file );
  178.         if$PclZip->extractPCLZIP_OPT_PATH$dest_dir == )
  179.         {
  180.             echo '<p style="color:red">'.sprintfT_'Unable to unpack &laquo;%s&raquo; ZIP archive.' )$src_file ).'</p>';
  181.             flush();
  182.  
  183.             return false;
  184.         }
  185.     }
  186.     else
  187.     {
  188.         debug_die'There is no \'zip\' or \'zlib\' extension installed!' );
  189.     }
  190.  
  191.     return true;
  192. }
  193.  
  194.  
  195. /**
  196.  * Verify that destination files can be overwritten
  197.  *
  198.  * @param string source directory
  199.  * @param string destination directory
  200.  * @param string action name
  201.  * @param boolean overwrite
  202.  * @param array read only file list
  203.  */
  204. function verify_overwrite$src$dest$action ''$overwrite true&$read_only_list )
  205. {
  206.     $dir opendir$src );
  207.  
  208.     $dir_list array();
  209.     $file_list array();
  210.     whilefalse !== $file readdir$dir ) ) )
  211.     {
  212.         if ( ( $file != '.' && $file != '..' ) )
  213.         {
  214.             $srcfile $src.'/'.$file;
  215.             $destfile $dest.'/'.$file;
  216.  
  217.             ifisset$read_only_list && file_exists$destfile && !is_writable$destfile ) )
  218.             {    // Folder or file is not writable
  219.                 $read_only_list[$destfile;
  220.             }
  221.  
  222.             if is_dir$srcfile ) )
  223.             {
  224.                 $dir_list[$srcfile$destfile;
  225.             }
  226.             elseif$overwrite )
  227.             {    // Add to overwrite
  228.                 $file_list[$srcfile$destfile;
  229.             }
  230.         }
  231.     }
  232.  
  233.     foreach$dir_list as $src_dir => $dest_dir )
  234.     {
  235.         if!empty$action ) )
  236.         {
  237.             // progressive display of what backup is doing
  238.             echo $action.' &laquo;<strong>'.$dest_dir.'</strong>&raquo;...<br />';
  239.             flush();
  240.         }
  241.  
  242.         if$overwrite && !file_exists$dest_dir ) )
  243.         {
  244.             // Create destination directory
  245.             @mkdir$dest_dir );
  246.         }
  247.  
  248.         verify_overwrite$src_dir$dest_dir''$overwrite$read_only_list );
  249.     }
  250.  
  251.     foreach$file_list as $src_file => $dest_file )
  252.     {    // Overwrite destination file
  253.         copy$src_file$dest_file );
  254.     }
  255.  
  256.     closedir$dir );
  257. }
  258.  
  259.  
  260. /**
  261.  * Get upgrade action
  262.  * @param string download url
  263.  * @return upgrade action
  264.  */
  265. function get_upgrade_action$download_url )
  266. {
  267.     global $upgrade_path$servertimenow$debug;
  268.  
  269.     // Construct version name from download URL
  270.     $slash_pos strrpos$download_url'/' );
  271.     $point_pos strrpos$download_url'.' );
  272.  
  273.     if$slash_pos $point_pos )
  274.     {
  275.         $version_name substr$download_url$slash_pos 1$point_pos $slash_pos );
  276.     }
  277.  
  278.     ifempty$version_name ) )
  279.     {
  280.         return false;
  281.     }
  282.  
  283.     iffile_exists$upgrade_path ) )
  284.     {
  285.         // Search if there is unpacked version in '_upgrade' directory
  286.         foreachget_filenames$upgrade_pathfalsetruetruefalsetrue as $dir_name )
  287.         {
  288.             ifstrpos$dir_name$version_name === )
  289.             {
  290.                 $action_props array();
  291.                 $new_version_status check_version$dir_name );
  292.                 if!empty$new_version_status ) )
  293.                 {
  294.                     $action_props['action''none';
  295.                     $action_props['status'$new_version_status;
  296.                 }
  297.  
  298.                 if$debug || empty$new_version_status ) )
  299.                 {
  300.                     $action_props['action''install';
  301.                     $action_props['name'$dir_name;
  302.                 }
  303.  
  304.                 return $action_props;
  305.             }
  306.         }
  307.  
  308.         // Search if there is packed version in '_upgrade' directory
  309.         foreachget_filenames$upgrade_pathtruefalsetruefalsetrue as $file_name )
  310.         {
  311.             ifstrpos$file_name$version_name === )
  312.             {
  313.                 return array'action' => 'unzip''name' => substr$file_name0strrpos$file_name'.' ) ) );
  314.             }
  315.         }
  316.     }
  317.  
  318.     // There is no any version in '_upgrade' directory. So, we need download package before.
  319.     return array'action' => 'download''name' => $version_name.'-'.date'Y-m-d'$servertimenow ) );
  320. }
  321.  
  322.  
  323. /**
  324.  * Convert aliases to real table names as table backup works with real table names
  325.  * @param mixed aliases
  326.  * @return mixed 
  327.  */
  328. function aliases_to_tables$aliases )
  329. {
  330.     global $DB;
  331.  
  332.     ifis_array$aliases ) )
  333.     {
  334.         $tables array();
  335.         foreach$aliases as $alias )
  336.         {
  337.             $tables[preg_replace$DB->dbaliases$DB->dbreplaces$alias );
  338.         }
  339.         return $tables;
  340.     }
  341.     elseif$aliases == '*' )
  342.     {
  343.         return $aliases;
  344.     }
  345.     else
  346.     {
  347.         return preg_replace$DB->dbaliases$DB->dbreplaces$aliases );
  348.     }
  349. }
  350.  
  351.  
  352. /*
  353.  * $Log: _maintenance.funcs.php,v $
  354.  * Revision 1.8  2010/02/04 19:29:53  blueyed
  355.  * wording
  356.  *
  357.  * Revision 1.7  2010/01/17 16:15:24  sam2kb
  358.  * Localization clean-up
  359.  *
  360.  * Revision 1.6  2009/11/22 19:15:34  efy-maxim
  361.  * load class
  362.  *
  363.  * Revision 1.5  2009/11/19 12:10:51  efy-maxim
  364.  * Force 'upgrade' for debug mode
  365.  *
  366.  * Revision 1.4  2009/11/18 21:54:25  efy-maxim
  367.  * compatibility fix for PHP4
  368.  *
  369.  * Revision 1.3  2009/10/21 14:27:39  efy-maxim
  370.  * upgrade
  371.  *
  372.  * Revision 1.2  2009/10/20 14:38:54  efy-maxim
  373.  * maintenance modulde: downloading - unpacking - verifying destination files - backing up - copying new files - upgrade database using regular script (Warning: it is very unstable version! Please, don't use maintenance modulde, because it can affect your data )
  374.  *
  375.  * Revision 1.1  2009/10/18 20:15:51  efy-maxim
  376.  * 1. backup, upgrade have been moved to maintenance module
  377.  * 2. maintenance module permissions
  378.  *
  379.  */
  380. ?>

Documentation generated on Sat, 06 Mar 2010 04:18:45 +0100 by phpDocumentor 1.4.2. This site is hosted and maintained by Daniel HAHLER (Contact).