updates.php 14.7 KB
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
<?php

/**
 * @copyright  Copyright (c) 2010-2013 Nicholas K. Dionysopoulos / AkeebaBackup.com
 * @copyright  Copyright (c) 2009-2017 Ryan Demmer. All rights reserved
 * @license    GNU LGPLv3 or later <http://www.gnu.org/copyleft/lesser.html>
 *
 * Based on the LiveUpdateDownloadHelper class from Akeeba LiveUpdate
 */
defined('_JEXEC') or die();

/**
 * Check for and download updates from a remote server.
 */
class UpdatesHelper
{
    private static function applyCACert(&$ch)
    {
        $cacert = dirname(__FILE__).'/cacert.pem';

        if (file_exists($cacert)) {
            @curl_setopt($ch, CURLOPT_CAINFO, $cacert);

            return true;
        }

        return false;
    }

    /**
     * Fetches update information from the server using cURL or fopen.
     *
     * @param string $url  The URL to check
     * @param string $data Data to send via POST
     *
     * @return mixed Result string on success, false on failure
     */
    public function check($url, $data)
    {
        $result = false;

        if (self::hasCURL()) {
            $ch = curl_init($url);

            self::applyCACert($ch);

            curl_setopt($ch, CURLOPT_HEADER, 0);
            // Pretend we are Firefox, so that webservers play nice with us
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.14) Gecko/20110105 Firefox/3.6.14');
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // needed to display errors
            curl_setopt($ch, CURLOPT_FAILONERROR, true);
            // The @ sign allows the next line to fail if open_basedir is set or if safe mode is enabled
            @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            @curl_setopt($ch, CURLOPT_MAXREDIRS, 20);

            // add post data
            if ($data) {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            }

            $result = curl_exec($ch);

            // file download
            if ($result === false) {
                $error = curl_error($ch);
                $num = (int) curl_errno($ch);

                if (!$error && $num === 7) {
                    $error = 'Failed to connect() to host or proxy';
                }

                curl_close($ch);

                return array('error' => 'CURL ERROR : '.$num.' - '.$error);
            }

            curl_close($ch);
        } elseif (self::hasFOPEN()) {
            $options = array('http' => array('method' => 'POST', 'timeout' => 10, 'content' => $data));

            $context = stream_context_create($options);
            $result = @file_get_contents($url, false, $context);

            if ($result === false) {
                return array('error' => WFText::_('Update check failed : Invalid response from update server'));
            }
        }

        return $result;
    }

    /**
     * Downloads from a URL and saves the result as a local file.
     *
     * @param string $url    The URL to fetch
     * @param string $target Where to save the file
     *
     * @return bool True on success
     */
    public static function download($url, $target)
    {
        // Import Joomla! libraries
        JLoader::import('joomla.filesystem.file');

        /* @var bool Did we try to force permissions? */
        $hackPermissions = false;

        // Make sure the target does not exist
        if (JFile::exists($target)) {
            if (!@unlink($target)) {
                JFile::delete($target);
            }
        }

        // Try to open the output file for writing
        $fp = @fopen($target, 'wb');

        if ($fp === false) {
            // The file can not be opened for writing. Let's try a hack.
            $empty = '';
            if (JFile::write($target, $empty)) {
                if (self::chmod($target, 511)) {
                    $fp = @fopen($target, 'wb');
                    $hackPermissions = true;
                }
            }
        }

        $result = false;

        if ($fp !== false) {
            // First try to download directly to file if $fp !== false
            $adapters = self::getAdapters();
            $result = false;

            while (!empty($adapters) && ($result === false)) {
                // Run the current download method
                $method = 'get'.strtoupper(array_shift($adapters));
                $result = self::$method($url, $fp);

                // Check if we have a download
                if ($result === true) {
                    // The download is complete, close the file pointer
                    @fclose($fp);

                    // If the filesize is not at least 1 byte, we consider it failed.
                    clearstatcache();
                    $filesize = @filesize($target);

                    if ($filesize <= 0) {
                        $result = false;
                        $fp = @fopen($target, 'wb');
                    }
                }
            }

            // If we have no download, close the file pointer
            if ($result === false) {
                @fclose($fp);
            }
        }

        if ($result === false) {
            // Delete the target file if it exists
            if (file_exists($target)) {
                if (!@unlink($target)) {
                    JFile::delete($target);
                }
            }
            // Download and write using JFile::write();
            $result = JFile::write($target, self::downloadAndReturn($url));
        }

        return $result;
    }

    /**
     * Downloads from a URL and returns the result as a string.
     *
     * @param string $url The URL to download from
     *
     * @return mixed Result string on success, false on failure
     */
    public static function downloadAndReturn($url)
    {
        $adapters = self::getAdapters();
        $result = false;

        while (!empty($adapters) && ($result === false)) {
            // Run the current download method
            $method = 'get'.strtoupper(array_shift($adapters));
            $result = self::$method($url, null);
        }

        return $result;
    }

    /**
     * Does the server support PHP's cURL extension?
     *
     * @return bool True if it is supported
     */
    public static function hasCURL()
    {
        static $result = null;

        if (is_null($result)) {
            $result = function_exists('curl_init');

            if ($result) {
                $cacert = dirname(__FILE__).'/cacert.pem';

                // check for SSL support
                $version = curl_version();
                $ssl_supported = ($version['features'] & CURL_VERSION_SSL);

                $result = (bool) $ssl_supported && file_exists($cacert);
            }
        }

        return $result;
    }

    /**
     * Downloads the contents of a URL and writes them to disk (if $fp is not null)
     * or returns them as a string (if $fp is null) using cURL.
     *
     * @param string   $url The URL to download from
     * @param resource $fp  The file pointer to download to. Omit to return the contents
     *
     * @return bool|string False on failure, true on success ($fp not null) or the URL contents (if $fp is null)
     */
    private static function &getCURL($url, $fp = null, $nofollow = false)
    {
        $result = false;

        $ch = curl_init($url);

        self::applyCACert($ch);

        if (!@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1) && !$nofollow) {
            // Safe Mode is enabled. We have to fetch the headers and
            // parse any redirections present in there.
            curl_setopt($ch, CURLOPT_AUTOREFERER, true);
            curl_setopt($ch, CURLOPT_FAILONERROR, true);
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);

            // Get the headers
            $data = curl_exec($ch);
            curl_close($ch);

            // Init
            $newURL = $url;

            // Parse the headers
            $lines = explode("\n", $data);

            foreach ($lines as $line) {
                if (substr($line, 0, 9) == 'Location:') {
                    $newURL = trim(substr($line, 9));
                }
            }

            // Download from the new URL
            if ($url != $newURL) {
                return self::getCURL($newURL, $fp);
            } else {
                return self::getCURL($newURL, $fp, true);
            }
        } else {
            @curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
        }

        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        // Pretend we are IE7, so that webservers play nice with us
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');

        if (is_resource($fp)) {
            curl_setopt($ch, CURLOPT_FILE, $fp);
        }

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }

    /**
     * Does the server support URL fopen() wrappers?
     *
     * @return bool
     */
    public static function hasFOPEN()
    {
        static $result = null;

        if (is_null($result)) {
            // If we are not allowed to use ini_get, we assume that URL fopen is
            // disabled.
            if (!function_exists('ini_get')) {
                $result = false;
            } else {
                // get wrappers
                $wrappers = stream_get_wrappers();

                $result = ini_get('allow_url_fopen') && in_array('https', $wrappers);
            }
        }

        return $result;
    }

    /**
     * Downloads the contents of a URL and writes them to disk (if $fp is not null)
     * or returns them as a string (if $fp is null) using fopen() URL wrappers.
     *
     * @param string   $url The URL to download from
     * @param resource $fp  The file pointer to download to. Omit to return the contents
     *
     * @return bool|string False on failure, true on success ($fp not null) or the URL contents (if $fp is null)
     */
    private static function &getFOPEN($url, $fp = null)
    {
        $result = false;

        // Track errors
        if (function_exists('ini_set')) {
            $track_errors = ini_set('track_errors', true);
        }

        // Open the URL for reading
        if (function_exists('stream_context_create')) {
            // PHP 5+ way (best)
            $httpopts = array(
                'user_agent' => 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)',
                'timeout' => 10.0,
            );
            $context = stream_context_create(array('http' => $httpopts));
            $ih = @fopen($url, 'r', false, $context);
        } else {
            // PHP 4 way (actually, it's just a fallback as we can't run this code in PHP4)
            if (function_exists('ini_set')) {
                ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
            }
            $ih = @fopen($url, 'r');
        }

        // If fopen() fails, abort
        if (!is_resource($ih)) {
            return $result;
        }

        // Try to download
        $bytes = 0;
        $result = true;
        $return = '';
        while (!feof($ih) && $result) {
            $contents = fread($ih, 4096);
            if ($contents === false) {
                @fclose($ih);
                $result = false;

                return $result;
            } else {
                $bytes += strlen($contents);
                if (is_resource($fp)) {
                    $result = @fwrite($fp, $contents);
                } else {
                    $return .= $contents;
                    unset($contents);
                }
            }
        }

        @fclose($ih);

        if (is_resource($fp)) {
            return $result;
        } elseif ($result === true) {
            return $return;
        } else {
            return $result;
        }
    }

    /**
     * Detect and return available download methods.
     *
     * @return array
     */
    private static function getAdapters()
    {
        // Detect available adapters
        $adapters = array();
        if (self::hasCURL()) {
            $adapters[] = 'curl';
        }
        if (self::hasFOPEN()) {
            $adapters[] = 'fopen';
        }

        return $adapters;
    }

    /**
     * Change the permissions of a file, optionally using FTP.
     *
     * @param string $file Absolute path to file
     * @param int    $mode Permissions, e.g. 0755
     *
     * @return bool Ture if successful
     */
    private static function chmod($path, $mode)
    {
        if (is_string($mode)) {
            $mode = octdec($mode);
            if (($mode < 0600) || ($mode > 0777)) {
                $mode = 0755;
            }
        }

        // Initialize variables
        JLoader::import('joomla.client.helper');
        $ftpOptions = JClientHelper::getCredentials('ftp');

        // Check to make sure the path valid and clean
        $path = JPath::clean($path);

        if ($ftpOptions['enabled'] == 1) {
            // Connect the FTP client
            JLoader::import('joomla.client.ftp');
            if (version_compare(JVERSION, '3.0', 'ge')) {
                $ftp = JClientFTP::getInstance(
                                $ftpOptions['host'], $ftpOptions['port'], array(), $ftpOptions['user'], $ftpOptions['pass']
                );
            } else {
                if (version_compare(JVERSION, '3.0', 'ge')) {
                    $ftp = JClientFTP::getInstance(
                                    $ftpOptions['host'], $ftpOptions['port'], array(), $ftpOptions['user'], $ftpOptions['pass']
                    );
                } else {
                    $ftp = JFTP::getInstance(
                                    $ftpOptions['host'], $ftpOptions['port'], array(), $ftpOptions['user'], $ftpOptions['pass']
                    );
                }
            }
        }

        if (@chmod($path, $mode)) {
            $ret = true;
        } elseif ($ftpOptions['enabled'] == 1) {
            // Translate path and delete
            JLoader::import('joomla.client.ftp');
            $path = JPath::clean(str_replace(JPATH_ROOT, $ftpOptions['root'], $path), '/');
            // FTP connector throws an error
            $ret = $ftp->chmod($path, $mode);
        } else {
            return false;
        }
    }
}