3) return false; // Initialize $dest is_resource($dest) or $dest = ''; // Try curl to read remote file if (function_exists('curl_init')) { $ch = @curl_init(); @curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); @curl_setopt($ch, CURLOPT_URL, $src); @curl_setopt($ch, CURLOPT_HEADER, 1); @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $content = @curl_exec($ch); $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE); $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE); @curl_close($content); if ($content !== false and $status >= 200 and $status < 400) { if (preg_match('/Location:\s+?(.+)/', substr($content, 0, $header_length), $m)) { return autoupdate_fetchRemote($m[1], $dest, $user_agent, $step+1); } $content = substr($content, $header_length); is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; return true; } } // Try file_get_contents to read remote file if (ini_get('allow_url_fopen')) { $opts['http'] = array('timeout' => $timeout); $ctx = stream_context_create($opts); $content = @file_get_contents($src, 0, $ctx); if ($content !== false) { is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; return true; } } return false; } function autoupdate_deltree($path) { if (is_dir($path)) { $fh = opendir($path); while ($file = readdir($fh)) { if ($file != '.' and $file != '..') { $pathfile = $path . '/' . $file; if (is_dir($pathfile)) { autoupdate_deltree($pathfile); } else { @unlink($pathfile); } } } closedir($fh); return @rmdir($path); } } function check_version_for_autoupdate($version=PHPWG_VERSION) { global $conf, $header_notes; if (preg_match('/(\d+\.\d+)\.(\d+)/', $version, $matches) and @autoupdate_fetchRemote(PHPWG_URL.'/download/latest_version', $result, 1)) { $lines = @explode("\r\n", $result); $new_version = trim($lines[1]); $new_branch = preg_replace('/(\d+\.\d+)\.\d+/', '$1', $new_version); $actual_branch = $matches[1]; $update_to = $actual_branch . '.' . ($matches[2]+1); if (version_compare($version, $new_version) < 0 and $actual_branch == $new_branch) { $path = $conf['local_data_dir'].'/autoupdate/'; $code = $version.'_to_'.$update_to; $filename = $path.$version.'_to_'.$update_to.'.zip'; mkgetdir($path); if (!file_exists($filename) or !filesize($filename)) { $zip = @fopen($filename, 'w+'); @autoupdate_fetchRemote(PHPWG_URL.'/download/dlcounter.php?code='.str_replace(array('.', '_'), '', $code), $zip); @fclose($zip); } if (file_exists($filename) and filesize($filename)) { if (isset($_GET['autoupdate']) and preg_match('/\d+\.\d+\.\d+_to_'.preg_quote($version).'/', $_GET['autoupdate'])) { redirect('admin.php?autoupdate='.$code); } else { array_push($header_notes, '

'.l10n('A new version of Piwigo is available.').'
', ''.l10n('Click here to upgrade automatically').'

' ); } } } } } load_language('plugin.lang', dirname(__FILE__).'/'); if (isset($_GET['autoupdate'])) { if ($_GET['autoupdate'] == 'success') { array_push($page['infos'], sprintf(l10n('autoupdate_success'), PHPWG_VERSION)); } elseif ($file = $conf['local_data_dir'].'/autoupdate/'.$_GET['autoupdate'].'.zip' and file_exists($file)) { include_once(PHPWG_ROOT_PATH.'admin/include/pclzip.lib.php'); $zip = new PclZip($file); if ($result = $zip->extract(PCLZIP_OPT_PATH, PHPWG_ROOT_PATH, PCLZIP_OPT_REMOVE_PATH, $_GET['autoupdate'], PCLZIP_OPT_SET_CHMOD, 0755, PCLZIP_OPT_REPLACE_NEWER)) { //Check if all files were extracted $error = ''; foreach($result as $extract) { if (!in_array($extract['status'], array('ok', 'filtered', 'already_a_directory'))) { // Try to change chmod and extract if (@chmod(PHPWG_ROOT_PATH.$extract['filename'], 0777) and ($res = $zip->extract(PCLZIP_OPT_BY_NAME, $_GET['autoupdate'].'/'.$extract['filename'], PCLZIP_OPT_PATH, PHPWG_ROOT_PATH, PCLZIP_OPT_REMOVE_PATH, $_GET['autoupdate'], PCLZIP_OPT_SET_CHMOD, 0755, PCLZIP_OPT_REPLACE_NEWER)) and isset($res[0]['status']) and $res[0]['status'] == 'ok') { continue; } else { $error .= $extract['filename'].': '.$extract['status']."\n"; } } } if (empty($error)) { if (file_exists(PHPWG_ROOT_PATH.'obsolete.list') and $old_files = file(PHPWG_ROOT_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES) and !empty($old_files)) { array_push($old_files, 'obsolete.list'); foreach($old_files as $old_file) { $path = PHPWG_ROOT_PATH.$old_file; if (is_file($path)) { @unlink($path); } elseif (is_dir($path)) { autoupdate_deltree($path); } } } if (preg_match('/\d+\.\d+\.\d+_to_(\d+\.\d+\.\d+)/', $_GET['autoupdate'], $matches)) { check_version_for_autoupdate($matches[1]); } autoupdate_deltree($conf['local_data_dir'].'/autoupdate'); invalidate_user_cache(true); $template->delete_compiled_templates(); redirect('admin.php?autoupdate=success'); } else { file_put_contents($conf['local_data_dir'].'/autoupdate/log_error.txt', $error); $relative_path = trim(str_replace(dirname(dirname(dirname(__FILE__))), '', $conf['local_data_dir']), '/\\'); array_push($page['errors'], sprintf(l10n('autoupdate_extract_fail'), PHPWG_ROOT_PATH.$relative_path.'/autoupdate/log_error.txt')); } } else { autoupdate_deltree($conf['local_data_dir'].'/autoupdate'); array_push($page['errors'], l10n('autoupdate_fail')); } } else { array_push($page['errors'], l10n('autoupdate_fail')); } } else { check_version_for_autoupdate(); } ?>