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

Last change on this file since 6181 was 6181, checked in by patdenice, 14 years ago

Update language keys.
Code cleaning.

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