123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- <?php
- namespace Grav\Plugin\Admin;
- use Grav\Common\Cache;
- use Grav\Common\Grav;
- use Grav\Common\GPM\GPM as GravGPM;
- use Grav\Common\GPM\Licenses;
- use Grav\Common\GPM\Installer;
- use Grav\Common\GPM\Upgrader;
- use Grav\Common\HTTP\Response;
- use Grav\Common\Filesystem\Folder;
- use Grav\Common\GPM\Common\Package;
- /**
- * Class Gpm
- *
- * @package Grav\Plugin\Admin
- */
- class Gpm
- {
- // Probably should move this to Grav DI container?
- /** @var GravGPM */
- protected static $GPM;
- public static function GPM()
- {
- if (!static::$GPM) {
- static::$GPM = new GravGPM();
- }
- return static::$GPM;
- }
- /**
- * Default options for the install
- *
- * @var array
- */
- protected static $options = [
- 'destination' => GRAV_ROOT,
- 'overwrite' => true,
- 'ignore_symlinks' => true,
- 'skip_invalid' => true,
- 'install_deps' => true,
- 'theme' => false
- ];
- /**
- * @param Package[]|string[]|string $packages
- * @param array $options
- *
- * @return string|bool
- */
- public static function install($packages, array $options)
- {
- $options = array_merge(self::$options, $options);
- if (!Installer::isGravInstance($options['destination']) || !Installer::isValidDestination($options['destination'],
- [Installer::EXISTS, Installer::IS_LINK])
- ) {
- return false;
- }
- $packages = is_array($packages) ? $packages : [$packages];
- $count = count($packages);
- $packages = array_filter(array_map(function ($p) {
- return !is_string($p) ? $p instanceof Package ? $p : false : self::GPM()->findPackage($p);
- }, $packages));
- if (!$options['skip_invalid'] && $count !== count($packages)) {
- return false;
- }
- $messages = '';
- foreach ($packages as $package) {
- if (isset($package->dependencies) && $options['install_deps']) {
- $result = static::install($package->dependencies, $options);
- if (!$result) {
- return false;
- }
- }
- // Check destination
- Installer::isValidDestination($options['destination'] . DS . $package->install_path);
- if (!$options['overwrite'] && Installer::lastErrorCode() === Installer::EXISTS) {
- return false;
- }
- if (!$options['ignore_symlinks'] && Installer::lastErrorCode() === Installer::IS_LINK) {
- return false;
- }
- $license = Licenses::get($package->slug);
- $local = static::download($package, $license);
- Installer::install($local, $options['destination'],
- ['install_path' => $package->install_path, 'theme' => $options['theme']]);
- Folder::delete(dirname($local));
- $errorCode = Installer::lastErrorCode();
- if ($errorCode) {
- $msg = Installer::lastErrorMsg();
- throw new \RuntimeException($msg);
- }
- if (count($packages) === 1) {
- $message = Installer::getMessage();
- if ($message) {
- return $message;
- }
- $messages .= $message;
- }
- }
- Cache::clearCache();
- return $messages ?: true;
- }
- /**
- * @param Package[]|string[]|string $packages
- * @param array $options
- *
- * @return string|bool
- */
- public static function update($packages, array $options)
- {
- $options['overwrite'] = true;
- return static::install($packages, $options);
- }
- /**
- * @param Package[]|string[]|string $packages
- * @param array $options
- *
- * @return string|bool
- */
- public static function uninstall($packages, array $options)
- {
- $options = array_merge(self::$options, $options);
- $packages = (array)$packages;
- $count = count($packages);
- $packages = array_filter(array_map(function ($p) {
- if (is_string($p)) {
- $p = strtolower($p);
- $plugin = static::GPM()->getInstalledPlugin($p);
- $p = $plugin ?: static::GPM()->getInstalledTheme($p);
- }
- return $p instanceof Package ? $p : false;
- }, $packages));
- if (!$options['skip_invalid'] && $count !== count($packages)) {
- return false;
- }
- foreach ($packages as $package) {
- $location = Grav::instance()['locator']->findResource($package->package_type . '://' . $package->slug);
- // Check destination
- Installer::isValidDestination($location);
- if (!$options['ignore_symlinks'] && Installer::lastErrorCode() === Installer::IS_LINK) {
- return false;
- }
- Installer::uninstall($location);
- $errorCode = Installer::lastErrorCode();
- if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) {
- $msg = Installer::lastErrorMsg();
- throw new \RuntimeException($msg);
- }
- if (count($packages) === 1) {
- $message = Installer::getMessage();
- if ($message) {
- return $message;
- }
- }
- }
- Cache::clearCache();
- return true;
- }
- /**
- * Direct install a file
- *
- * @param string $package_file
- *
- * @return string|bool
- */
- public static function directInstall($package_file)
- {
- if (!$package_file) {
- return Admin::translate('PLUGIN_ADMIN.NO_PACKAGE_NAME');
- }
- $tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true);
- $tmp_zip = $tmp_dir . '/Grav-' . uniqid('', false);
- if (Response::isRemote($package_file)) {
- $zip = GravGPM::downloadPackage($package_file, $tmp_zip);
- } else {
- $zip = GravGPM::copyPackage($package_file, $tmp_zip);
- }
- if (file_exists($zip)) {
- $tmp_source = $tmp_dir . '/Grav-' . uniqid('', false);
- $extracted = Installer::unZip($zip, $tmp_source);
- if (!$extracted) {
- Folder::delete($tmp_source);
- Folder::delete($tmp_zip);
- return Admin::translate('PLUGIN_ADMIN.PACKAGE_EXTRACTION_FAILED');
- }
- $type = GravGPM::getPackageType($extracted);
- if (!$type) {
- Folder::delete($tmp_source);
- Folder::delete($tmp_zip);
- return Admin::translate('PLUGIN_ADMIN.NOT_VALID_GRAV_PACKAGE');
- }
- if ($type === 'grav') {
- Installer::isValidDestination(GRAV_ROOT . '/system');
- if (Installer::IS_LINK === Installer::lastErrorCode()) {
- Folder::delete($tmp_source);
- Folder::delete($tmp_zip);
- return Admin::translate('PLUGIN_ADMIN.CANNOT_OVERWRITE_SYMLINKS');
- }
- static::upgradeGrav($zip, $extracted);
- } else {
- $name = GravGPM::getPackageName($extracted);
- if (!$name) {
- Folder::delete($tmp_source);
- Folder::delete($tmp_zip);
- return Admin::translate('PLUGIN_ADMIN.NAME_COULD_NOT_BE_DETERMINED');
- }
- $install_path = GravGPM::getInstallPath($type, $name);
- $is_update = file_exists($install_path);
- Installer::isValidDestination(GRAV_ROOT . DS . $install_path);
- if (Installer::lastErrorCode() === Installer::IS_LINK) {
- Folder::delete($tmp_source);
- Folder::delete($tmp_zip);
- return Admin::translate('PLUGIN_ADMIN.CANNOT_OVERWRITE_SYMLINKS');
- }
- Installer::install($zip, GRAV_ROOT,
- ['install_path' => $install_path, 'theme' => $type === 'theme', 'is_update' => $is_update],
- $extracted);
- }
- Folder::delete($tmp_source);
- if (Installer::lastErrorCode()) {
- return Installer::lastErrorMsg();
- }
- } else {
- return Admin::translate('PLUGIN_ADMIN.ZIP_PACKAGE_NOT_FOUND');
- }
- Folder::delete($tmp_zip);
- Cache::clearCache();
- return true;
- }
- /**
- * @param Package $package
- *
- * @return string
- */
- private static function download(Package $package, $license = null)
- {
- $query = '';
- if ($package->premium) {
- $query = \json_encode(array_merge($package->premium, [
- 'slug' => $package->slug,
- 'license_key' => $license,
- 'sid' => md5(GRAV_ROOT)
- ]));
- $query = '?d=' . base64_encode($query);
- }
- try {
- $contents = Response::get($package->zipball_url . $query, []);
- } catch (\Exception $e) {
- throw new \RuntimeException($e->getMessage());
- }
- $tmp_dir = Admin::getTempDir() . '/Grav-' . uniqid('', false);
- Folder::mkdir($tmp_dir);
- $bad_chars = array_merge(array_map('chr', range(0, 31)), ['<', '>', ':', '"', '/', '\\', '|', '?', '*']);
- $filename = $package->slug . str_replace($bad_chars, '', basename($package->zipball_url));
- $filename = preg_replace('/[\\\\\/:"*?&<>|]+/m', '-', $filename);
- file_put_contents($tmp_dir . DS . $filename . '.zip', $contents);
- return $tmp_dir . DS . $filename . '.zip';
- }
- /**
- * @param array $package
- * @param string $tmp
- *
- * @return string
- */
- private static function _downloadSelfupgrade(array $package, $tmp)
- {
- $output = Response::get($package['download'], []);
- Folder::mkdir($tmp);
- file_put_contents($tmp . DS . $package['name'], $output);
- return $tmp . DS . $package['name'];
- }
- /**
- * @return bool
- */
- public static function selfupgrade()
- {
- $upgrader = new Upgrader();
- if (!Installer::isGravInstance(GRAV_ROOT)) {
- return false;
- }
- if (is_link(GRAV_ROOT . DS . 'index.php')) {
- Installer::setError(Installer::IS_LINK);
- return false;
- }
- if (method_exists($upgrader, 'meetsRequirements') &&
- method_exists($upgrader, 'minPHPVersion') &&
- !$upgrader->meetsRequirements()) {
- $error = [];
- $error[] = '<p>Grav has increased the minimum PHP requirement.<br />';
- $error[] = 'You are currently running PHP <strong>' . phpversion() . '</strong>';
- $error[] = ', but PHP <strong>' . $upgrader->minPHPVersion() . '</strong> is required.</p>';
- $error[] = '<p><a href="http://getgrav.org/blog/changing-php-requirements-to-5.5" class="button button-small secondary">Additional information</a></p>';
- Installer::setError(implode("\n", $error));
- return false;
- }
- $update = $upgrader->getAssets()['grav-update'];
- $tmp = Admin::getTempDir() . '/Grav-' . uniqid('', false);
- if ($tmp) {
- $file = self::_downloadSelfupgrade($update, $tmp);
- $folder = Installer::unZip($file, $tmp . '/zip');
- $keepFolder = false;
- } else {
- // If you make $tmp empty, you can install your local copy of Grav (for testing purposes only).
- $file = 'grav.zip';
- $folder = '~/phpstorm/grav-clones/grav';
- //$folder = '/home/matias/phpstorm/rockettheme/grav-devtools/grav-clones/grav';
- $keepFolder = true;
- }
- static::upgradeGrav($file, $folder, $keepFolder);
- $errorCode = Installer::lastErrorCode();
- if ($tmp) {
- Folder::delete($tmp);
- }
- return !(is_string($errorCode) || ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)));
- }
- private static function upgradeGrav($zip, $folder, $keepFolder = false)
- {
- static $ignores = [
- 'backup',
- 'cache',
- 'images',
- 'logs',
- 'tmp',
- 'user',
- '.htaccess',
- 'robots.txt'
- ];
- if (!is_dir($folder)) {
- Installer::setError('Invalid source folder');
- }
- try {
- $script = $folder . '/system/install.php';
- /** Install $installer */
- if ((file_exists($script) && $install = include $script) && is_callable($install)) {
- $install($zip);
- } else {
- Installer::install(
- $zip,
- GRAV_ROOT,
- ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true, 'ignores' => $ignores],
- $folder,
- $keepFolder
- );
- Cache::clearCache();
- }
- } catch (\Exception $e) {
- Installer::setError($e->getMessage());
- }
- }
- }
|