source: extensions/autoupdate/autoupdate.php @ 4007

Last change on this file since 4007 was 4007, checked in by patdenice, 15 years ago

[Plugin Autoupdate]
Try to change chmod if extraction fail.

File size: 5.2 KB
Line 
1<?php
2
3function autoupdate_deltree($path)
4{
5  if (is_dir($path))
6  {
7    $fh = opendir($path);
8    while ($file = readdir($fh))
9    {
10      if ($file != '.' and $file != '..')
11      {
12        $pathfile = $path . '/' . $file;
13        if (is_dir($pathfile))
14        {
15          autoupdate_deltree($pathfile);
16        }
17        else
18        {
19          @unlink($pathfile);
20        }
21      }
22    }
23    closedir($fh);
24    return @rmdir($path);
25  }
26}
27
28function check_version_for_autoupdate($version=PHPWG_VERSION)
29{
30  global $conf, $header_notes;
31
32  if (preg_match('/(\d+\.\d+)\.(\d+)/', $version, $matches) and @fetchRemote(PHPWG_URL.'/download/latest_version', $result))
33  {
34    $lines = @explode("\r\n", $result);
35    $new_version = trim($lines[1]);
36    $new_branch = preg_replace('/(\d+\.\d+)\.\d+/', '$1', $new_version);
37    $actual_branch = $matches[1];
38    $update_to =  $actual_branch . '.' . ($matches[2]+1);
39
40    if (version_compare($version, $new_version) < 0
41      and $actual_branch == $new_branch)
42    {
43      $path = $conf['local_data_dir'].'/autoupdate/';
44      $code = $version.'_to_'.$update_to;
45      $filename = $path.$version.'_to_'.$update_to.'.zip';
46      mkgetdir($path);
47
48      if (!file_exists($filename) or !filesize($filename))
49      {
50        $zip = @fopen($filename, 'w+');
51        @fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.str_replace(array('.', '_'), '', $code), $zip);
52        @fclose($zip);
53      }
54
55      if (file_exists($filename) and filesize($filename))
56      {
57        if (isset($_GET['autoupdate']) and preg_match('/\d+\.\d+\.\d+_to_'.preg_quote($version).'/', $_GET['autoupdate']))
58        {
59          redirect('admin.php?autoupdate='.$code);
60        }
61        else
62        {
63          array_push($header_notes,
64            '<p>'.l10n('A new version of Piwigo is available.').'<br>',
65            '<a href="admin.php?autoupdate='.$code.'" onClick="return confirm(\''.l10n('autoupdate_alert').'\');">'.l10n('Click here to upgrade automatically').'</a></p>'
66          );
67        }
68      }
69    }
70  }
71}
72
73load_language('plugin.lang', dirname(__FILE__).'/');
74
75if (isset($_GET['autoupdate']))
76{
77  if ($_GET['autoupdate'] == 'success')
78  {
79    array_push($page['infos'], sprintf(l10n('autoupdate_success'), PHPWG_VERSION));
80  }
81  elseif ($file = $conf['local_data_dir'].'/autoupdate/'.$_GET['autoupdate'].'.zip' and file_exists($file))
82  {
83    include_once(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php');
84    $zip = new PclZip($file);
85    if ($result = $zip->extract(PCLZIP_OPT_PATH, PHPWG_ROOT_PATH,
86                                PCLZIP_OPT_REMOVE_PATH, $_GET['autoupdate'],
87                                PCLZIP_OPT_SET_CHMOD, 0755,
88                                PCLZIP_OPT_REPLACE_NEWER))
89    {
90      //Check if all files were extracted
91      $error = '';
92      foreach($result as $extract)
93      {
94        if (!in_array($extract['status'], array('ok', 'filtered', 'already_a_directory')))
95        {
96          // Try to change chmod and extract
97          if (@chmod(PHPWG_ROOT_PATH.$extract['filename'], 0777)
98            and ($res = $zip->extract(PCLZIP_OPT_BY_NAME, $_GET['autoupdate'].'/'.$extract['filename'],
99                                      PCLZIP_OPT_PATH, PHPWG_ROOT_PATH,
100                                      PCLZIP_OPT_REMOVE_PATH, $_GET['autoupdate'],
101                                      PCLZIP_OPT_SET_CHMOD, 0755,
102                                      PCLZIP_OPT_REPLACE_NEWER))
103            and isset($res[0]['status'])
104            and $res[0]['status'] == 'ok')
105          {
106            continue;
107          }
108          else
109          {
110            $error .= $extract['filename'].': '.$extract['status']."\n";
111          }
112        }
113      }
114
115      if (empty($error))
116      {
117        if (file_exists(PHPWG_ROOT_PATH.'obsolete.list')
118          and $old_files = file(PHPWG_ROOT_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
119          and !empty($old_files))
120        {
121          array_push($old_files, 'obsolete.list');
122          foreach($old_files as $old_file)
123          {
124            $path = PHPWG_ROOT_PATH.$old_file;
125            if (is_file($path))
126            {
127              @unlink($path);
128            }
129            elseif (is_dir($path))
130            {
131              autoupdate_deltree($path);
132            }
133          }
134        }
135        if (preg_match('/\d+\.\d+\.\d+_to_(\d+\.\d+\.\d+)/', $_GET['autoupdate'], $matches))
136        {
137          check_version_for_autoupdate($matches[1]);
138        }
139        autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
140        invalidate_user_cache(true);
141        $template->delete_compiled_templates();
142        redirect('admin.php?autoupdate=success');
143      }
144      else
145      {
146        file_put_contents($conf['local_data_dir'].'/autoupdate/log_error.txt', $error);
147        $relative_path = trim(str_replace(dirname(dirname(dirname(__FILE__))), '', $conf['local_data_dir']), '/\\');
148        array_push($page['errors'], sprintf(l10n('autoupdate_extract_fail'), PHPWG_ROOT_PATH.$relative_path.'/autoupdate/log_error.txt'));
149      }
150    }
151    else
152    {
153      autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
154      array_push($page['errors'], l10n('autoupdate_fail'));
155    }
156  }
157  else
158  {
159    array_push($page['errors'], l10n('autoupdate_fail'));
160  }
161}
162else
163{
164  check_version_for_autoupdate();
165}
166
167?>
Note: See TracBrowser for help on using the repository browser.