source: extensions/autoupdate/trunk/include/functions.inc.php

Last change on this file was 9735, checked in by patdenice, 13 years ago

Remove useless function.
Update englich language key.

File size: 6.9 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5function autoupdate_deltree($path, $move_to_trash=false)
6{
7  if (is_dir($path))
8  {
9    $fh = opendir($path);
10    while ($file = readdir($fh))
11    {
12      if ($file != '.' and $file != '..')
13      {
14        $pathfile = $path . '/' . $file;
15        if (is_dir($pathfile))
16        {
17          autoupdate_deltree($pathfile, $move_to_trash);
18        }
19        else
20        {
21          @unlink($pathfile);
22        }
23      }
24    }
25    closedir($fh);
26    if (@rmdir($path))
27    {
28      return true;
29    }
30    elseif ($move_to_trash)
31    {
32      $trash = PHPWG_ROOT_PATH.'_trash';
33      if (!is_dir($trash))
34      {
35        @mkgetdir($trash);
36      }
37      return @rename($path, $trash . '/'.md5(uniqid(rand(), true)));
38    }
39    else
40    {
41      return false;
42    }
43  }
44}
45
46function autoupdate_add_index($path)
47{
48  if (is_dir($path))
49  {
50    $fh = opendir($path);
51    while ($file = readdir($fh))
52    {
53      if ($file != '.' and $file != '..')
54      {
55        $pathfile = $path . '/' . $file;
56        if (is_dir($pathfile))
57        {
58          autoupdate_add_index($pathfile);
59        }
60      }
61    }
62    $fp = @fopen($path.'/index.php', 'w');
63    @fwrite($fp, AU_DEFAULT_INDEX);
64    @fclose($fp);
65    closedir($fh);
66  }
67}
68
69function process_obsolete_list($file)
70{
71  if (file_exists(PHPWG_ROOT_PATH.$file)
72    and $old_files = file(PHPWG_ROOT_PATH.$file, FILE_IGNORE_NEW_LINES)
73    and !empty($old_files))
74  {
75    array_push($old_files, $file);
76    foreach($old_files as $old_file)
77    {
78      $path = PHPWG_ROOT_PATH.$old_file;
79      if (is_file($path))
80      {
81        @unlink($path);
82      }
83      elseif (is_dir($path))
84      {
85        autoupdate_deltree($path, true);
86      }
87    }
88  }
89}
90
91function autoupdate_dump_database()
92{
93  global $page, $conf, $cfgBase;
94
95  if (version_compare(PHPWG_VERSION, '2.1', '<'))
96  {
97    $conf['db_base'] = $cfgBase;
98  }
99
100  include(AUTOUPDATE_PATH.'include/mysqldump.php');
101
102  $path = $conf['local_data_dir'].'/autoupdate';
103
104  if (@mkgetdir($path)
105    and ($backupFile = tempnam($path, 'sql'))
106    and ($dumper = new MySQLDump($conf['db_base'],$backupFile,false,false)))
107  {
108    foreach (get_defined_constants() as $constant => $value)
109    {
110      if (preg_match('/_TABLE$/', $constant))
111      {
112        $dumper->getTableStructure($value);
113
114        if ($constant == 'HISTORY_TABLE' and !isset($_POST['includeHistory']))
115        {
116          continue;
117        }
118
119        $dumper->getTableData($value);
120      }
121    }
122  }
123
124  if (@filesize($backupFile))
125  {
126    $http_headers = array(
127      'Content-Length: '.@filesize($backupFile),
128      'Content-Type: text/x-sql',
129      'Content-Disposition: attachment; filename="database.sql";',
130      'Content-Transfer-Encoding: binary',
131      );
132
133    foreach ($http_headers as $header) {
134      header($header);
135    }
136
137    @readfile($backupFile);
138    autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
139    exit();
140  }
141  else
142  {
143    array_push($page['errors'], l10n('Unable to dump database.'));
144  }
145}
146
147function autoupdate_upgrade_to($upgrade_to, &$step)
148{
149  global $page, $conf, $template;
150
151  if (!version_compare($_POST['upgrade_to'], PHPWG_VERSION, '>'))
152  {
153    redirect(get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__)));
154  }
155
156  if ($step == 2)
157  {
158    preg_match('/(\d+\.\d+)\.(\d+)/', PHPWG_VERSION, $matches);
159    $code =  $matches[1].'.x_to_'.$_POST['upgrade_to'];
160    $dl_code = str_replace(array('.', '_'), '', $code);
161    $remove_path = $code;
162    $obsolete_list = 'obsolete.list';
163  }
164  else
165  {
166    $code = $_POST['upgrade_to'];
167    $dl_code = $code;
168    $remove_path = version_compare($code, '2.0.8', '>=') ? 'piwigo' : 'piwigo-'.$code;
169    $obsolete_list = PHPWG_ROOT_PATH.'install/obsolete.list';
170  }
171
172  if (empty($page['errors']))
173  {
174    $path = $conf['local_data_dir'].'/autoupdate';
175    $filename = $path.'/'.$code.'.zip';
176    @mkgetdir($path);
177
178    $chunk_num = 0;
179    $end = false;
180    $zip = @fopen($filename, 'w');
181    while (!$end)
182    {
183      $chunk_num++;
184      if (@fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.$dl_code.'&chunk_num='.$chunk_num, $result)
185        and $input = @unserialize($result))
186      {
187        if (0 == $input['remaining'])
188        {
189          $end = true;
190        }
191        @fwrite($zip, base64_decode($input['data']));
192      }
193      else
194      {
195        $end = true;
196      }
197    }
198    @fclose($zip);
199
200    if (@filesize($filename))
201    {
202      $zip = new PclZip($filename);
203      if ($result = $zip->extract(PCLZIP_OPT_PATH, PHPWG_ROOT_PATH,
204                                  PCLZIP_OPT_REMOVE_PATH, $remove_path,
205                                  PCLZIP_OPT_SET_CHMOD, 0755,
206                                  PCLZIP_OPT_REPLACE_NEWER))
207      {
208        //Check if all files were extracted
209        $error = '';
210        foreach($result as $extract)
211        {
212          if (!in_array($extract['status'], array('ok', 'filtered', 'already_a_directory')))
213          {
214            // Try to change chmod and extract
215            if (@chmod(PHPWG_ROOT_PATH.$extract['filename'], 0777)
216              and ($res = $zip->extract(PCLZIP_OPT_BY_NAME, $remove_path.'/'.$extract['filename'],
217                                        PCLZIP_OPT_PATH, PHPWG_ROOT_PATH,
218                                        PCLZIP_OPT_REMOVE_PATH, $remove_path,
219                                        PCLZIP_OPT_SET_CHMOD, 0755,
220                                        PCLZIP_OPT_REPLACE_NEWER))
221              and isset($res[0]['status'])
222              and $res[0]['status'] == 'ok')
223            {
224              continue;
225            }
226            else
227            {
228              $error .= $extract['filename'].': '.$extract['status']."\n";
229            }
230          }
231        }
232
233        if (empty($error))
234        {
235          process_obsolete_list($obsolete_list);
236          autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
237          invalidate_user_cache(true);
238          $template->delete_compiled_templates();
239          unset($_SESSION['need_update']);
240          if ($step == 2)
241          {
242            array_push($page['infos'], sprintf(l10n('autoupdate_success'), $upgrade_to));
243            $step = -1;
244          }
245          else
246          {
247            redirect(PHPWG_ROOT_PATH.'upgrade.php?now=');
248          }
249        }
250        else
251        {
252          file_put_contents($conf['local_data_dir'].'/autoupdate/log_error.txt', $error);
253          $relative_path = trim(str_replace(dirname(dirname(dirname(dirname(__FILE__)))), '', $conf['local_data_dir']), '/\\');
254          array_push($page['errors'], sprintf(l10n('autoupdate_extract_fail'), PHPWG_ROOT_PATH.$relative_path.'/autoupdate/log_error.txt'));
255        }
256      }
257      else
258      {
259        autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
260        array_push($page['errors'], l10n('autoupdate_fail'));
261      }
262    }
263    else
264    {
265      array_push($page['errors'], l10n('Piwigo cannot retrieve upgrade file from server'));
266    }
267  }
268}
269
270define('AU_DEFAULT_INDEX', file_get_contents(AUTOUPDATE_PATH.'index.php'));
271?>
Note: See TracBrowser for help on using the repository browser.