Ignore:
Timestamp:
May 13, 2010, 12:06:29 PM (14 years ago)
Author:
patdenice
Message:

Upgrade work for 2.1.
Language keys have not been inserted yet.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/autoupdate/autoupdate.php

    r4711 r6167  
    22
    33load_language('plugin.lang', dirname(__FILE__).'/');
    4 
    5 if (isset($_GET['action']) and
    6   ($_GET['action'] == 'check_autoupdate' or $_GET['action'] == 'check_upgrade' ))
    7 {
    8   unset($_SESSION['need_update']);
    9   unset($_SESSION['plugins_need_update']);
    10 }
    11 
    12 if (isset($_GET['autoupdate']))
    13 {
    14   if ($_GET['autoupdate'] == 'success')
    15   {
    16     array_push($page['infos'], sprintf(l10n('autoupdate_success'), PHPWG_VERSION));
    17   }
    18   elseif ($file = $conf['local_data_dir'].'/autoupdate/'.$_GET['autoupdate'].'.zip' and file_exists($file))
    19   {
    20     include(dirname(__FILE__).'/class.inc.php');
    21     include_once(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
    22     $autoupdate = new autoupdate();
    23     $zip = new PclZip($file);
    24     if ($result = $zip->extract(PCLZIP_OPT_PATH, PHPWG_ROOT_PATH,
    25                                 PCLZIP_OPT_REMOVE_PATH, $_GET['autoupdate'],
    26                                 PCLZIP_OPT_SET_CHMOD, 0755,
    27                                 PCLZIP_OPT_REPLACE_NEWER))
    28     {
    29       //Check if all files were extracted
    30       $error = '';
    31       foreach($result as $extract)
    32       {
    33         if (!in_array($extract['status'], array('ok', 'filtered', 'already_a_directory')))
     4include(AUTOUPDATE_PATH.'include/functions.inc.php');
     5include(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
     6
     7/*
     8STEP:
     90 = check is needed. If version is latest or check fail, we stay on step 0
     101 = new version on same branch AND new branch are available => user may choose upgrade.
     112 = upgrade on same branch
     123 = upgrade on different branch
     13*/
     14$step = isset($_GET['step']) ? $_GET['step'] : 0;
     15
     16// +-----------------------------------------------------------------------+
     17// |                                Step 0                                 |
     18// +-----------------------------------------------------------------------+
     19if ($step == 0)
     20{
     21  if (preg_match('/(\d+\.\d+)\.(\d+)/', PHPWG_VERSION, $matches)
     22    and @fetchRemote(PHPWG_URL.'/download/all_versions.php', $result)
     23    and $all_versions = @explode("\n", $result)
     24    and is_array($all_versions))
     25  {
     26    $template->assign('CHECK_VERSION', true);
     27
     28    $last_version = trim($all_versions[0]);
     29
     30    if (version_compare(PHPWG_VERSION, $last_version, '<'))
     31    {
     32      $new_branch = preg_replace('/(\d+\.\d+)\.\d+/', '$1', $last_version);
     33      $actual_branch = $matches[1];
     34
     35      if ($new_branch == $actual_branch)
     36      {
     37        $step = 2;
     38      }
     39      else
     40      {
     41        $step = 3;
     42
     43        // Check if new version exists in same branch
     44        foreach ($all_versions as $version)
    3445        {
    35           // Try to change chmod and extract
    36           if (@chmod(PHPWG_ROOT_PATH.$extract['filename'], 0777)
    37             and ($res = $zip->extract(PCLZIP_OPT_BY_NAME, $_GET['autoupdate'].'/'.$extract['filename'],
    38                                       PCLZIP_OPT_PATH, PHPWG_ROOT_PATH,
    39                                       PCLZIP_OPT_REMOVE_PATH, $_GET['autoupdate'],
    40                                       PCLZIP_OPT_SET_CHMOD, 0755,
    41                                       PCLZIP_OPT_REPLACE_NEWER))
    42             and isset($res[0]['status'])
    43             and $res[0]['status'] == 'ok')
     46          $new_branch = preg_replace('/(\d+\.\d+)\.\d+/', '$1', $version);
     47
     48          if ($new_branch == $actual_branch)
    4449          {
    45             continue;
    46           }
    47           else
    48           {
    49             $error .= $extract['filename'].': '.$extract['status']."\n";
     50            if (version_compare(PHPWG_VERSION, $version, '<'))
     51            {
     52              $step = 1;
     53            }
     54            break;
    5055          }
    5156        }
    5257      }
    53 
    54       if (empty($error))
    55       {
    56         if (file_exists(PHPWG_ROOT_PATH.'obsolete.list')
    57           and $old_files = file(PHPWG_ROOT_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
    58           and !empty($old_files))
    59         {
    60           array_push($old_files, 'obsolete.list');
    61           foreach($old_files as $old_file)
    62           {
    63             $path = PHPWG_ROOT_PATH.$old_file;
    64             if (is_file($path))
    65             {
    66               @unlink($path);
    67             }
    68             elseif (is_dir($path))
    69             {
    70               $autoupdate->deltree($path);
    71             }
    72           }
    73         }
    74         if (preg_match('/\d+\.\d+\.\d+_to_(\d+\.\d+\.\d+)/', $_GET['autoupdate'], $matches))
    75         {
    76           unset($_SESSION['need_update']);
    77           $autoupdate->check_version($matches[1]);
    78         }
    79         $autoupdate->deltree($conf['local_data_dir'].'/autoupdate');
    80         invalidate_user_cache(true);
    81         $template->delete_compiled_templates();
    82         redirect('admin.php?autoupdate=success');
    83       }
    84       else
    85       {
    86         file_put_contents($conf['local_data_dir'].'/autoupdate/log_error.txt', $error);
    87         $relative_path = trim(str_replace(dirname(dirname(dirname(__FILE__))), '', $conf['local_data_dir']), '/\\');
    88         array_push($page['errors'], sprintf(l10n('autoupdate_extract_fail'), PHPWG_ROOT_PATH.$relative_path.'/autoupdate/log_error.txt'));
    89       }
    90     }
    91     else
    92     {
    93       $autoupdate->deltree($conf['local_data_dir'].'/autoupdate');
    94       array_push($page['errors'], l10n('autoupdate_fail'));
    9558    }
    9659  }
    9760  else
    9861  {
    99     @unlink($file);
    100     array_push($page['errors'], l10n('autoupdate_fail'));
    101   }
    102 }
    103 
    104 if (!isset($_SESSION['need_update']) or !isset($_SESSION['plugins_need_update'])
    105   or $_SESSION['need_update'] !== false or $_SESSION['plugins_need_update'] !== array())
    106 {
    107   $template->set_filename('autoupdate_head', realpath(dirname(__FILE__).'/head.tpl'));
    108   array_push($header_notes, $template->parse('autoupdate_head', true));
    109 }
     62    $template->assign('CHECK_VERSION', false);
     63  }
     64}
     65
     66// +-----------------------------------------------------------------------+
     67// |                                Step 1                                 |
     68// +-----------------------------------------------------------------------+
     69if ($step == 1)
     70{
     71  $new_versions = array($last_version, $version);
     72  $template->assign('new_versions', $new_versions);
     73}
     74
     75// +-----------------------------------------------------------------------+
     76// |                                Step 2                                 |
     77// +-----------------------------------------------------------------------+
     78if ($step == 2)
     79{
     80  if (isset($_POST['submit']) and isset($_POST['upgrade_to']))
     81  {
     82    upgrade_to($_POST['upgrade_to'], $step);
     83  }
     84
     85  $template->assign('UPGRADE_TO', isset($_GET['to']) ? $_GET['to'] : $last_version);
     86}
     87
     88// +-----------------------------------------------------------------------+
     89// |                                Step 3                                 |
     90// +-----------------------------------------------------------------------+
     91if ($step == 3)
     92{
     93  if (isset($_POST['saveTemplate']))
     94  {
     95    $path = $conf['local_data_dir'].'/autoupdate';
     96
     97    if (@mkgetdir($path)
     98      and ($zip = tempnam($path, 'zip'))
     99      and ($archive = new pclZip($zip))
     100      and ($v_list = $archive->add(PHPWG_ROOT_PATH.'template', PCLZIP_OPT_REMOVE_PATH, PHPWG_ROOT_PATH))
     101      and is_array($v_list)
     102      and !empty($v_list))
     103    {
     104      $http_headers = array(
     105        'Content-Length: '.@filesize($zip),
     106        'Content-Type: application/zip',
     107        'Content-Disposition: attachment; filename="template.zip";',
     108        'Content-Transfer-Encoding: binary',
     109        );
     110
     111      foreach ($http_headers as $header) {
     112        header($header);
     113      }
     114
     115      @readfile($zip);
     116      autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
     117      exit();
     118    }
     119
     120    array_push($page['errors'], l10n('Unable to send template directory.'));
     121  }
     122
     123  if (isset($_POST['dumpDatabase']))
     124  {
     125    if (version_compare(PHPWG_VERSION, '2.1', '<'))
     126    {
     127      global $cfgBase;
     128      $conf['db_base'] = $cfgBase;
     129    }
     130
     131    include(AUTOUPDATE_PATH.'include/mysqldump.php');
     132
     133    $path = $conf['local_data_dir'].'/autoupdate';
     134
     135    if (@mkgetdir($path)
     136      and ($backupFile = tempnam($path, 'sql'))
     137      and ($dumper = new MySQLDump($conf['db_base'],$backupFile,false,false)))
     138    {
     139      $tablesStructure = array(
     140        CATEGORIES_TABLE,
     141        COMMENTS_TABLE,
     142        CONFIG_TABLE,
     143        FAVORITES_TABLE,
     144        GROUP_ACCESS_TABLE,
     145        GROUPS_TABLE,
     146        HISTORY_TABLE,
     147        HISTORY_SUMMARY_TABLE,
     148        IMAGE_CATEGORY_TABLE,
     149        IMAGES_TABLE,
     150        SESSIONS_TABLE,
     151        SITES_TABLE,
     152        USER_ACCESS_TABLE,
     153        USER_GROUP_TABLE,
     154        USERS_TABLE,
     155        USER_INFOS_TABLE,
     156        USER_FEED_TABLE,
     157        WAITING_TABLE,
     158        RATE_TABLE,
     159        USER_CACHE_TABLE,
     160        USER_CACHE_CATEGORIES_TABLE,
     161        CADDIE_TABLE,
     162        UPGRADE_TABLE,
     163        SEARCH_TABLE,
     164        USER_MAIL_NOTIFICATION_TABLE,
     165        TAGS_TABLE,
     166        IMAGE_TAG_TABLE,
     167        PLUGINS_TABLE,
     168        OLD_PERMALINKS_TABLE,
     169        );
     170
     171      $tablesData = $tablesStructure;
     172
     173      if (!isset($_POST['includeHistory']))
     174      {
     175        unset($tablesData[6]);
     176      }
     177
     178      foreach ($tablesStructure as $table)
     179      {
     180        $dumper->getTableStructure($table);
     181      }
     182      foreach ($tablesData as $table)
     183      {
     184        $dumper->getTableData($table);
     185      }
     186    }
     187
     188    if (@filesize($backupFile))
     189    {
     190      $http_headers = array(
     191        'Content-Length: '.@filesize($backupFile),
     192        'Content-Type: text/x-sql',
     193        'Content-Disposition: attachment; filename="database.sql";',
     194        'Content-Transfer-Encoding: binary',
     195        );
     196
     197      foreach ($http_headers as $header) {
     198        header($header);
     199      }
     200
     201      @readfile($backupFile);
     202      autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
     203      exit();
     204    }
     205
     206    array_push($page['errors'], l10n('Unable to dump database.'));
     207  }
     208
     209  if (isset($_POST['submit']) and isset($_POST['upgrade_to']))
     210  {
     211    upgrade_to($_POST['upgrade_to'], $step);
     212  }
     213
     214  $template->assign('UPGRADE_TO', isset($_GET['to']) ? $_GET['to'] : $last_version);
     215}
     216
     217// +-----------------------------------------------------------------------+
     218// |                        Process template                               |
     219// +-----------------------------------------------------------------------+
     220
     221$template->assign(array(
     222  'STEP' => $step,
     223  'AU_URL' => get_admin_plugin_menu_link(AUTOUPDATE_PATH . '/autoupdate.php'),
     224  )
     225);
     226
     227$template->set_filename('plugin_admin_content', realpath(AUTOUPDATE_PATH.'template/autoupdate.tpl'));
     228$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
    110229
    111230?>
Note: See TracChangeset for help on using the changeset viewer.