public static function loadCached($url, $expires, $debug = false) { if (!function_exists('curl_version')) { ErrorHelper::logWarning('Could not fetch data, cURL unavailable'); return null; } if ($debug) { ErrorHelper::logDebug('cURL for: ' . $url); } $filename = LoadHelper::getFilename($url); if (!LoadHelper::hasCache($url, $expires)) { if ($debug) { ErrorHelper::logDebug('No cache or expired, making new request'); } $rawData = LoadHelper::load($url); if (!$rawData) { if ($debug) { ErrorHelper::logDebug('cURL request failed: ' . LoadHelper::$error); } if (LoadHelper::hasCache($url, -1)) { if ($debug) { ErrorHelper::logDebug('Using expired cache'); } $cache = file_get_contents($filename); return $cache; } else { if ($debug) { ErrorHelper::logDebug('Request failed, no cache'); } return false; } } if ($debug) { ErrorHelper::logDebug('Data returned, saving it to cache'); } $folder = LoadHelper::getCacheDir(); if (!file_exists($folder)) { mkdir($folder); } $cache = @fopen($filename, 'wb'); $write = $cache ? fwrite($cache, $rawData) : null; if (!$write) { ErrorHelper::logWarning('Writing to cache failed. Make sure the folder ' . $folder . ' exists and is writeable (chmod 777)'); } if ($cache) { fclose($cache); } return $rawData; } if ($debug) { ErrorHelper::logDebug('Cache hit, using that'); } return file_get_contents($filename); }
private static function install($debug = false) { if (!extension_loaded('zip')) { ErrorHelper::logWarning('zip extensions unavailable on server, can\'t extract updates without it'); return; } LoadHelper::loadCached(UpdateHelper::$new->zipball_url, 1000, $debug); $zip = new ZipArchive(); if ($zip->open(LoadHelper::getFilename(UpdateHelper::$new->zipball_url)) === TRUE) { // make sure temp directory is empty UpdateHelper::removeTemp(); $success = $zip->extractTo(LoadHelper::getCacheDir() . UpdateHelper::tmp_path); $zip->close(); if ($debug) { ErrorHelper::logDebug('Update archive ' . ($success ? 'extracted successfully' : 'failed to extract')); } } else { if ($debug) { ErrorHelper::logWarning('Failed extracting update archive'); } return; } // updates from github are packed in a folder within the zip, we need the name of this folder $rootFolder = ''; $iterator = UpdateHelper::getUpdateIterator(false); foreach ($iterator as $item) { // the first folder we hit when iterating will be the root folder, save that if ($item->isDir() && $rootFolder == '') { $rootFolder = $iterator->getSubPathName(); if ($debug) { ErrorHelper::logDebug('Update root folder is ' . $rootFolder); } // we won't ever need to create the root folder, so we skip to the next item continue; } // figure out destination path $destination = realpath(UpdateHelper::destination . UpdateHelper::removePrefix($iterator->getSubPathName(), $rootFolder)); if ($item->isDir()) { if (file_exists($destination)) { continue; } if ($debug) { ErrorHelper::logDebug('Create directory ' . $destination); } mkdir($destination); } else { $success = copy($item, $destination); if ($debug) { ErrorHelper::logDebug('Copy file from ' . $item . ' to ' . $destination . ($success ? ' OK' : ' Failed')); } } } // remove any temporary files extracted in the process UpdateHelper::removeTemp(); // remove the update zip file, we won't need it anymore unlink(LoadHelper::getFilename(UpdateHelper::$new->zipball_url)); return true; }