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

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

Clean code.
Rename files.

File size: 10.8 KB
RevLine 
[6167]1<?php
2
[6196]3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5function autoupdate_deltree($path, $move_to_trash=false)
[6167]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        {
[6196]17          autoupdate_deltree($pathfile, $move_to_trash);
[6167]18        }
19        else
20        {
21          @unlink($pathfile);
22        }
23      }
24    }
25    closedir($fh);
[6196]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    }
[6167]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      {
[6196]85        autoupdate_deltree($path, true);
[6167]86      }
87    }
88  }
89}
90
91function move_local_files($dir)
92{
93  global $page, $cfgBase, $cfgUser, $cfgPassword, $cfgHote, $prefixeTable;
94
95  if ((!is_dir($dir) and !mkdir($dir, 0777))
96    or (!is_dir($dir.'/config') and !mkdir($dir.'/config'))
97    or (!is_dir($dir.'/css') and !mkdir($dir.'/css'))
98    or (!is_dir($dir.'/language') and !mkdir($dir.'/language')))
99  {
100    autoupdate_deltree($dir);
101    array_push($page['errors'], l10n('Unable to write new local directory.'));
102    return;
103  }
104
105  // Add index.php
106  autoupdate_add_index($dir);
107
108  // mysql.inc.php
109  $file = PHPWG_ROOT_PATH.'include/mysql.inc.php';
110  if (is_readable($file))
111  {
112    $file_content = '<?php
113$conf[\'dblayer\'] = \'mysql\';
114$conf[\'db_base\'] = \''.$cfgBase.'\';
115$conf[\'db_user\'] = \''.$cfgUser.'\';
116$conf[\'db_password\'] = \''.$cfgPassword.'\';
117$conf[\'db_host\'] = \''.$cfgHote.'\';
118
119$prefixeTable = \''.$prefixeTable.'\';
120
121define(\'PHPWG_INSTALLED\', true);';
122    if (defined('PWG_CHARSET'))
123    {
124      $file_content.= '
125define(\'PWG_CHARSET\', \'utf-8\');
126define(\'DB_CHARSET\', \'utf8\');
127define(\'DB_COLLATE\', \'\');';
128    }
129    $file_content.= '
130?>';
131    $new_config_file = $dir.'/config/database.inc.php';
132
133    if (!($fp = @fopen($new_config_file, 'w'))
134      or !@fwrite($fp, $file_content)
135      or !@fclose($fp))
136    {
137      array_push($page['errors'], l10n('Unable to write new local directory.'));
138      return;
139    }
140    @chmod($new_config_file, 0755);
141  }
142
143  // config_local.inc.php
144  $file = PHPWG_ROOT_PATH.'include/config_local.inc.php';
145  if (is_readable($file))
146  {
147    copy($file, $dir.'/config/config.inc.php');
148    @chmod($file, 0755);
149  }
150
151  // languages
152  $language_dir = opendir(PHPWG_ROOT_PATH.'language');
153  while ($file = readdir($language_dir))
154  {
155    $path = PHPWG_ROOT_PATH.'language/'.$file;
156    if (!is_link($path) and is_dir($path) and is_readable($path.'/local.lang.php'))
157    {
158      $content = file_get_contents($path.'/local.lang.php');
159      $langdef = explode('.',$file);
160      if (count($langdef)>1)
161      {
162        $content = utf8_encode($content);
163      }
164      $filename = $dir.'/language/'.$langdef[0].'.lang.php';
165      $fp = @fopen($filename, 'w');
166      @fwrite($fp, $content);
167      @fclose($fp);
168      @chmod($filename, 0755);
169    }
170  }
171  closedir($language_dir);
172
173  // template-common/local-layout.css
174  $file = PHPWG_ROOT_PATH.'template-common/local-layout.css';
175  if (is_readable($file))
176  {
177    copy($file, $dir.'/css/rules.css');
178    @chmod($file, 0755);
179  }
180
181  // template/xxx/local-layout.css
182  $known_templates = array(
183    'yoga'     => 'default',
184    'floPure'  => 'Pure_default',
185    'floOs'    => 'OS_default',
186    'gally'    => 'gally-default',
187    'simple'   => 'simple',
188  );
189
190  foreach ($known_templates as $old_tpl => $new_tpl)
191  {
192    $file = PHPWG_ROOT_PATH.'template/'.$old_tpl.'/local-layout.css';
193    if (is_readable($file))
194    {
195      copy($file, $dir.'/css/'.$new_tpl.'-rules.css');
196      @chmod($file, 0755);
197    }
198  }
199}
200
[6181]201function autoupdate_save_template_dir()
[6167]202{
[6181]203  global $page, $conf;
204
205  $path = $conf['local_data_dir'].'/autoupdate';
206
207  if (@mkgetdir($path)
208    and ($zip = tempnam($path, 'zip'))
209    and ($archive = new pclZip($zip))
210    and ($v_list = $archive->add(PHPWG_ROOT_PATH.'template', PCLZIP_OPT_REMOVE_PATH, PHPWG_ROOT_PATH))
211    and is_array($v_list)
212    and !empty($v_list))
213  {
214    $http_headers = array(
215      'Content-Length: '.@filesize($zip),
216      'Content-Type: application/zip',
217      'Content-Disposition: attachment; filename="template.zip";',
218      'Content-Transfer-Encoding: binary',
219      );
220
221    foreach ($http_headers as $header) {
222      header($header);
223    }
224
225    @readfile($zip);
226    autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
227    exit();
228  }
229  else
230  {
231    array_push($page['errors'], l10n('Unable to send template directory.'));
232  }
233}
234
235function autoupdate_dump_database()
236{
237  global $page, $conf, $cfgBase;
238
239  if (version_compare(PHPWG_VERSION, '2.1', '<'))
240  {
241    $conf['db_base'] = $cfgBase;
242  }
243
244  include(AUTOUPDATE_PATH.'include/mysqldump.php');
245
246  $path = $conf['local_data_dir'].'/autoupdate';
247
248  if (@mkgetdir($path)
249    and ($backupFile = tempnam($path, 'sql'))
250    and ($dumper = new MySQLDump($conf['db_base'],$backupFile,false,false)))
251  {
252    foreach (get_defined_constants() as $constant => $value)
253    {
254      if (preg_match('/_TABLE$/', $constant))
255      {
256        $dumper->getTableStructure($value);
257
258        if ($constant == 'HISTORY_TABLE' and !isset($_POST['includeHistory']))
259        {
260          continue;
261        }
262
263        $dumper->getTableData($value);
264      }
265    }
266  }
267
268  if (@filesize($backupFile))
269  {
270    $http_headers = array(
271      'Content-Length: '.@filesize($backupFile),
272      'Content-Type: text/x-sql',
273      'Content-Disposition: attachment; filename="database.sql";',
274      'Content-Transfer-Encoding: binary',
275      );
276
277    foreach ($http_headers as $header) {
278      header($header);
279    }
280
281    @readfile($backupFile);
282    autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
283    exit();
284  }
285  else
286  {
287    array_push($page['errors'], l10n('Unable to dump database.'));
288  }
289}
290
291function autoupdate_upgrade_to($upgrade_to, &$step)
292{
[6167]293  global $page, $conf, $template;
294
295  if (!version_compare($_POST['upgrade_to'], PHPWG_VERSION, '>'))
296  {
[9712]297    redirect(get_root_url().'admin.php?page=plugin-'.basename(dirname(__FILE__)));
[6167]298  }
299
300  if ($step == 2)
301  {
302    preg_match('/(\d+\.\d+)\.(\d+)/', PHPWG_VERSION, $matches);
303    $code =  $matches[1].'.x_to_'.$_POST['upgrade_to'];
304    $dl_code = str_replace(array('.', '_'), '', $code);
305    $remove_path = $code;
306    $obsolete_list = 'obsolete.list';
307  }
308  else
309  {
310    $code = $_POST['upgrade_to'];
311    $dl_code = $code;
312    $remove_path = version_compare($code, '2.0.8', '>=') ? 'piwigo' : 'piwigo-'.$code;
313    $obsolete_list = PHPWG_ROOT_PATH.'install/obsolete.list';
314
315    if (version_compare(PHPWG_VERSION, '2.1', '<'))
316    {
317      move_local_files(PHPWG_ROOT_PATH.'local');
318    }
319  }
320
321  if (empty($page['errors']))
322  {
323    $path = $conf['local_data_dir'].'/autoupdate';
324    $filename = $path.'/'.$code.'.zip';
325    @mkgetdir($path);
326
327    $chunk_num = 0;
328    $end = false;
329    $zip = @fopen($filename, 'w');
330    while (!$end)
331    {
332      $chunk_num++;
333      if (@fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.$dl_code.'&chunk_num='.$chunk_num, $result)
334        and $input = @unserialize($result))
335      {
336        if (0 == $input['remaining'])
337        {
338          $end = true;
339        }
340        @fwrite($zip, base64_decode($input['data']));
341      }
342      else
343      {
344        $end = true;
345      }
346    }
347    @fclose($zip);
348
349    if (@filesize($filename))
350    {
351      $zip = new PclZip($filename);
352      if ($result = $zip->extract(PCLZIP_OPT_PATH, PHPWG_ROOT_PATH,
353                                  PCLZIP_OPT_REMOVE_PATH, $remove_path,
354                                  PCLZIP_OPT_SET_CHMOD, 0755,
355                                  PCLZIP_OPT_REPLACE_NEWER))
356      {
357        //Check if all files were extracted
358        $error = '';
359        foreach($result as $extract)
360        {
361          if (!in_array($extract['status'], array('ok', 'filtered', 'already_a_directory')))
362          {
363            // Try to change chmod and extract
364            if (@chmod(PHPWG_ROOT_PATH.$extract['filename'], 0777)
365              and ($res = $zip->extract(PCLZIP_OPT_BY_NAME, $remove_path.'/'.$extract['filename'],
366                                        PCLZIP_OPT_PATH, PHPWG_ROOT_PATH,
367                                        PCLZIP_OPT_REMOVE_PATH, $remove_path,
368                                        PCLZIP_OPT_SET_CHMOD, 0755,
369                                        PCLZIP_OPT_REPLACE_NEWER))
370              and isset($res[0]['status'])
371              and $res[0]['status'] == 'ok')
372            {
373              continue;
374            }
375            else
376            {
377              $error .= $extract['filename'].': '.$extract['status']."\n";
378            }
379          }
380        }
381
382        if (empty($error))
383        {
384          process_obsolete_list($obsolete_list);
385          autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
386          invalidate_user_cache(true);
387          $template->delete_compiled_templates();
[6351]388          unset($_SESSION['need_update']);
[6167]389          if ($step == 2)
390          {
391            array_push($page['infos'], sprintf(l10n('autoupdate_success'), $upgrade_to));
[6207]392            $step = -1;
[6167]393          }
394          else
395          {
396            redirect(PHPWG_ROOT_PATH.'upgrade.php?now=');
397          }
398        }
399        else
400        {
401          file_put_contents($conf['local_data_dir'].'/autoupdate/log_error.txt', $error);
402          $relative_path = trim(str_replace(dirname(dirname(dirname(dirname(__FILE__)))), '', $conf['local_data_dir']), '/\\');
403          array_push($page['errors'], sprintf(l10n('autoupdate_extract_fail'), PHPWG_ROOT_PATH.$relative_path.'/autoupdate/log_error.txt'));
404        }
405      }
406      else
407      {
408        autoupdate_deltree($conf['local_data_dir'].'/autoupdate');
409        array_push($page['errors'], l10n('autoupdate_fail'));
410      }
411    }
412    else
413    {
414      array_push($page['errors'], l10n('Piwigo cannot retrieve upgrade file from server'));
415    }
416  }
417}
418
419define('AU_DEFAULT_INDEX', file_get_contents(AUTOUPDATE_PATH.'index.php'));
420?>
Note: See TracBrowser for help on using the repository browser.