1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------
if (!defined('DC_RC_PATH')) { return; }
class dcUpdate
{
const ERR_FILES_CHANGED = 101;
const ERR_FILES_UNREADABLE = 102;
const ERR_FILES_UNWRITALBE = 103;
protected $url;
protected $subject;
protected $version;
protected $cache_file;
protected $version_info = array(
'version' => null,
'href' => null,
'checksum' => null,
'info' => null,
'notify' => true
);
protected $cache_ttl = '-6 hours';
protected $forced_files = array();
/**
* Constructor
*
* @param url string Versions file URL
* @param subject string Subject to check
* @param version string Version type
* @param cache_dir string Directory cache path
*/
public function __construct($url,$subject,$version,$cache_dir)
{
$this->url = $url;
$this->subject = $subject;
$this->version = $version;
$this->cache_file = $cache_dir.'/'.$subject.'-'.$version;
}
/**
* Checks for Dotclear updates.
* Returns latest version if available or false.
*
* @param version string Current version to compare
* @param nocache boolean Force checking
* @return string Latest version if available
*/
public function check($version, $nocache=false)
{
$this->getVersionInfo($nocache);
$v = $this->getVersion();
if ($v && version_compare($version,$v,'<')) {
return $v;
}
return false;
}
public function getVersionInfo($nocache=false)
{
# Check cached file
if (is_readable($this->cache_file) && filemtime($this->cache_file) > strtotime($this->cache_ttl) && !$nocache)
{
$c = @file_get_contents($this->cache_file);
$c = @unserialize($c);
if (is_array($c)) {
$this->version_info = $c;
return;
}
}
$cache_dir = dirname($this->cache_file);
$can_write = (!is_dir($cache_dir) && is_writable(dirname($cache_dir)))
|| (!file_exists($this->cache_file) && is_writable($cache_dir))
|| is_writable($this->cache_file);
# If we can't write file, don't bug host with queries
if (!$can_write) {
return;
}
if (!is_dir($cache_dir)) {
try {
files::makeDir($cache_dir);
} catch (Exception $e) {
return;
}
}
# Try to get latest version number
try
{
$path = '';
$client = netHttp::initClient($this->url,$path);
if ($client !== false) {
$client->setTimeout(4);
$client->setUserAgent($_SERVER['HTTP_USER_AGENT']);
$client->get($path);
$this->readVersion($client->getContent());
}
}
catch (Exception $e) {}
# Create cache
file_put_contents($this->cache_file,serialize($this->version_info));
}
public function getVersion()
{
return $this->version_info['version'];
}
public function getFileURL()
{
return $this->version_info['href'];
}
public function getInfoURL()
{
return $this->version_info['info'];
}
public function getChecksum()
{
return $this->version_info['checksum'];
}
public function getNotify()
{
return $this->version_info['notify'];
}
public function getForcedFiles()
{
return $this->forced_files;
}
public function setForcedFiles()
{
$this->forced_files = func_get_args();
}
/**
* Sets notification flag.
*/
public function setNotify($n)
{
if (!is_writable($this->cache_file)) {
return;
}
$this->version_info['notify'] = (boolean) $n;
file_put_contents($this->cache_file,serialize($this->version_info));
}
public function checkIntegrity($digests_file,$root)
{
if (!$digests_file) {
throw new Exception(__('Digests file not found.'));
}
$changes = $this->md5sum($root,$digests_file);
if (!empty($changes)) {
$e = new Exception('Some files have changed.',self::ERR_FILES_CHANGED);
$e->bad_files = $changes;
throw $e;
}
return true;
}
/**
* Downloads new version to destination $dest.
*/
public function download($dest)
{
$url = $this->getFileURL();
if (!$url) {
throw new Exception(__('No file to download'));
}
if (!is_writable(dirname($dest))) {
throw new Exception(__('Root directory is not writable.'));
}
try
{
$client = netHttp::initClient($url,$path);
$client->setTimeout(4);
$client->setUserAgent($_SERVER['HTTP_USER_AGENT']);
$client->useGzip(false);
$client->setPersistReferers(false);
$client->setOutput($dest);
$client->get($path);
if ($client->getStatus() != 200) {
@unlink($dest);
throw new Exception();
}
}
catch (Exception $e)
{
throw new Exception(__('An error occurred while downloading archive.'));
}
}
/**
* Checks if archive was successfully downloaded.
*/
public function checkDownload($zip)
{
$cs = $this->getChecksum();
return $cs && is_readable($zip) && md5_file($zip) == $cs;
}
/**
* Backups changed files before an update.
*/
public function backup($zip_file,$zip_digests,$root,$root_digests,$dest)
{
if (!is_readable($zip_file)) {
throw new Exception(__('Archive not found.'));
}
if (!is_readable($root_digests)) {
@unlink($zip_file);
throw new Exception(__('Unable to read current digests file.'));
}
# Stop everything if a backup already exists and can not be overrided
if (!is_writable(dirname($dest)) && !file_exists($dest)) {
throw new Exception(__('Root directory is not writable.'));
}
if (file_exists($dest) && !is_writable($dest)) {
return false;
}
$b_fp = @fopen($dest,'wb');
if ($b_fp === false) {
return false;
}
$zip = new fileUnzip($zip_file);
$b_zip = new fileZip($b_fp);
if (!$zip->hasFile($zip_digests))
{
@unlink($zip_file);
throw new Exception(__('Downloaded file does not seem to be a valid archive.'));
}
$opts = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES;
$cur_digests = file($root_digests,$opts);
$new_digests = explode("\n",$zip->unzip($zip_digests));
$new_files = $this->getNewFiles($cur_digests,$new_digests);
$zip->close();
unset($opts,$cur_digests,$new_digests,$zip);
$not_readable = array();
if (!empty($this->forced_files)) {
$new_files = array_merge($new_files,$this->forced_files);
}
foreach ($new_files as $file)
{
if (!$file || !file_exists($root.'/'.$file)) {
continue;
}
try {
$b_zip->addFile($root.'/'.$file,$file);
} catch (Exception $e) {
$not_readable[] = $file;
}
}
# If only one file is not readable, stop everything now
if (!empty($not_readable)) {
$e = new Exception('Some files are not readable.',self::ERR_FILES_UNREADABLE);
$e->bad_files = $not_readable;
throw $e;
}
$b_zip->write();
fclose($b_fp);
$b_zip->close();
return true;
}
/**
* Upgrade process.
*/
public function performUpgrade($zip_file,$zip_digests,$zip_root,$root,$root_digests)
{
if (!is_readable($zip_file)) {
throw new Exception(__('Archive not found.'));
}
if (!is_readable($root_digests)) {
@unlink($zip_file);
throw new Exception(__('Unable to read current digests file.'));
}
$zip = new fileUnzip($zip_file);
if (!$zip->hasFile($zip_digests))
{
@unlink($zip_file);
throw new Exception(__('Downloaded file does not seem to be a valid archive.'));
}
$opts = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES;
$cur_digests = file($root_digests,$opts);
$new_digests = explode("\n",$zip->unzip($zip_digests));
$new_files = self::getNewFiles($cur_digests,$new_digests);
if (!empty($this->forced_files)) {
$new_files = array_merge($new_files,$this->forced_files);
}
$zip_files = array();
$not_writable = array();
foreach ($new_files as $file)
{
if (!$file) {
continue;
}
if (!$zip->hasFile($zip_root.'/'.$file)) {
@unlink($zip_file);
throw new Exception(__('Incomplete archive.'));
}
$dest = $dest_dir = $root.'/'.$file;
while (!is_dir($dest_dir = dirname($dest_dir)));
if ((file_exists($dest) && !is_writable($dest)) ||
(!file_exists($dest) && !is_writable($dest_dir))) {
$not_writable[] = $file;
continue;
}
$zip_files[] = $file;
}
# If only one file is not writable, stop everything now
if (!empty($not_writable)) {
$e = new Exception('Some files are not writable',self::ERR_FILES_UNWRITALBE);
$e->bad_files = $not_writable;
throw $e;
}
# Everything's fine, we can write files, then do it now
$can_touch = function_exists('touch');
foreach ($zip_files as $file) {
$zip->unzip($zip_root.'/'.$file, $root.'/'.$file);
if ($can_touch) {
@touch($root.'/'.$file);
}
}
@unlink($zip_file);
}
protected function getNewFiles($cur_digests,$new_digests)
{
$cur_md5 = $cur_path = $cur_digests;
$new_md5 = $new_path = $new_digests;
array_walk($cur_md5, array($this,'parseLine'),1);
array_walk($cur_path,array($this,'parseLine'),2);
array_walk($new_md5, array($this,'parseLine'),1);
array_walk($new_path,array($this,'parseLine'),2);
$cur = array_combine($cur_md5,$cur_path);
$new = array_combine($new_md5,$new_path);
return array_values(array_diff_key($new,$cur));
}
protected function readVersion($str)
{
try
{
$xml = new SimpleXMLElement($str,LIBXML_NOERROR);
$r = $xml->xpath("/versions/subject[@name='".$this->subject."']/release[@name='".$this->version."']");
if (!empty($r) && is_array($r))
{
$r = $r[0];
$this->version_info['version'] = isset($r['version']) ? (string) $r['version'] : null;
$this->version_info['href'] = isset($r['href']) ? (string) $r['href'] : null;
$this->version_info['checksum'] = isset($r['checksum']) ? (string) $r['checksum'] : null;
$this->version_info['info'] = isset($r['info']) ? (string) $r['info'] : null;
}
}
catch (Exception $e)
{
throw $e;
}
}
protected function md5sum($root,$digests_file)
{
if (!is_readable($digests_file)) {
throw new Exception(__('Unable to read digests file.'));
}
$opts = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES;
$contents = file($digests_file,$opts);
$changes = array();
foreach ($contents as $digest)
{
if (!preg_match('#^([\da-f]{32})\s+(.+?)$#',$digest,$m)) {
continue;
}
$md5 = $m[1];
$filename = $root.'/'.$m[2];
# Invalid checksum
if (!is_readable($filename) || !self::md5_check($filename, $md5)) {
$changes[] = substr($m[2],2);
}
}
# No checksum found in digests file
if (empty($md5)) {
throw new Exception(__('Invalid digests file.'));
}
return $changes;
}
protected function parseLine(&$v,$k,$n)
{
if (!preg_match('#^([\da-f]{32})\s+(.+?)$#',$v,$m)) {
return;
}
$v = $n == 1 ? md5($m[2].$m[1]) : substr($m[2],2);
}
protected static function md5_check($filename,$md5)
{
if (md5_file($filename) == $md5) {
return true;
} else {
$filecontent = file_get_contents($filename);
$filecontent = str_replace ("\r\n","\n",$filecontent);
$filecontent = str_replace ("\r","\n",$filecontent);
if (md5($filecontent) == $md5) return true;
}
return false;
}
}
|