| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2017-01-20 13:55:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | namespace Grav\Plugin\Admin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-04 15:42:43 -07:00
										 |  |  | use Grav\Common\Grav; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | use Grav\Common\GPM\GPM as GravGPM; | 
					
						
							| 
									
										
										
										
											2016-10-13 10:13:18 -07:00
										 |  |  | use Grav\Common\GPM\Licenses; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | use Grav\Common\GPM\Installer; | 
					
						
							|  |  |  | use Grav\Common\GPM\Response; | 
					
						
							| 
									
										
										
										
											2015-08-06 09:34:55 +02:00
										 |  |  | use Grav\Common\GPM\Upgrader; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | use Grav\Common\Filesystem\Folder; | 
					
						
							|  |  |  | use Grav\Common\GPM\Common\Package; | 
					
						
							| 
									
										
										
										
											2016-11-24 15:13:47 +01:00
										 |  |  | use Grav\Plugin\Admin\Admin; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-01 19:50:12 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Class Gpm | 
					
						
							| 
									
										
										
										
											2017-01-20 13:55:03 -08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-03-01 19:50:12 +01:00
										 |  |  |  * @package Grav\Plugin\Admin | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | class Gpm | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // Probably should move this to Grav DI container?
 | 
					
						
							| 
									
										
										
										
											2016-03-01 19:50:12 +01:00
										 |  |  |     /** @var GravGPM */ | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |     protected static $GPM; | 
					
						
							| 
									
										
										
										
											2016-03-01 19:50:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |     public static function GPM() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (!static::$GPM) { | 
					
						
							|  |  |  |             static::$GPM = new GravGPM(); | 
					
						
							| 
									
										
										
										
											2017-11-28 05:14:31 +01:00
										 |  |  |             if (method_exists('GravGPM', 'loadRemoteGrav')) { | 
					
						
							|  |  |  |                 static::$GPM->loadRemoteGrav(); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         return static::$GPM; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Default options for the install | 
					
						
							| 
									
										
										
										
											2017-01-20 13:55:03 -08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected static $options = [ | 
					
						
							|  |  |  |         'destination'     => GRAV_ROOT, | 
					
						
							|  |  |  |         'overwrite'       => true, | 
					
						
							|  |  |  |         'ignore_symlinks' => true, | 
					
						
							|  |  |  |         'skip_invalid'    => true, | 
					
						
							| 
									
										
										
										
											2015-09-15 16:07:05 -06:00
										 |  |  |         'install_deps'    => true, | 
					
						
							|  |  |  |         'theme'           => false | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Package[]|string[]|string $packages | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * @param array                     $options | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function install($packages, array $options) | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $options = array_merge(self::$options, $options); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |         if (!Installer::isGravInstance($options['destination']) || !Installer::isValidDestination($options['destination'], | 
					
						
							|  |  |  |                 [Installer::EXISTS, Installer::IS_LINK]) | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         ) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         $packages = is_array($packages) ? $packages : [$packages]; | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $count    = count($packages); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-20 18:55:58 +02:00
										 |  |  |         $messages = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         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 (Installer::lastErrorCode() === Installer::EXISTS && !$options['overwrite']) { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (Installer::lastErrorCode() === Installer::IS_LINK && !$options['ignore_symlinks']) { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 10:13:18 -07:00
										 |  |  |             $license = Licenses::get($package->slug); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |             $local   = static::download($package, $license); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |             Installer::install($local, $options['destination'], | 
					
						
							|  |  |  |                 ['install_path' => $package->install_path, 'theme' => $options['theme']]); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |             Folder::delete(dirname($local)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-20 18:55:58 +02:00
										 |  |  |             $errorCode = Installer::lastErrorCode(); | 
					
						
							|  |  |  |             if ($errorCode) { | 
					
						
							|  |  |  |                 $msg = Installer::lastErrorMsg(); | 
					
						
							|  |  |  |                 throw new \RuntimeException($msg); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |             if (count($packages) === 1) { | 
					
						
							| 
									
										
										
										
											2016-04-20 18:55:58 +02:00
										 |  |  |                 $message = Installer::getMessage(); | 
					
						
							|  |  |  |                 if ($message) { | 
					
						
							|  |  |  |                     return $message; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $messages .= $message; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-20 18:55:58 +02:00
										 |  |  |         return $messages ?: true; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Package[]|string[]|string $packages | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * @param array                     $options | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function update($packages, array $options) | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $options['overwrite'] = true; | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |         return static::install($packages, $options); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Package[]|string[]|string $packages | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * @param array                     $options | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function uninstall($packages, array $options) | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $options = array_merge(self::$options, $options); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |         $packages = is_array($packages) ? $packages : [$packages]; | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $count    = count($packages); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $packages = array_filter(array_map(function ($p) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (is_string($p)) { | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |                 $p      = strtolower($p); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |                 $plugin = static::GPM()->getInstalledPlugin($p); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |                 $p      = $plugin ?: static::GPM()->getInstalledTheme($p); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $p instanceof Package ? $p : false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         }, $packages)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!$options['skip_invalid'] && $count !== count($packages)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         foreach ($packages as $package) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-04 20:59:08 +01:00
										 |  |  |             $location = Grav::instance()['locator']->findResource($package->package_type . '://' . $package->slug); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Check destination
 | 
					
						
							|  |  |  |             Installer::isValidDestination($location); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |             if (!$options['ignore_symlinks'] && Installer::lastErrorCode() === Installer::IS_LINK) { | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Installer::uninstall($location); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $errorCode = Installer::lastErrorCode(); | 
					
						
							|  |  |  |             if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) { | 
					
						
							| 
									
										
										
										
											2016-04-20 18:55:58 +02:00
										 |  |  |                 $msg = Installer::lastErrorMsg(); | 
					
						
							|  |  |  |                 throw new \RuntimeException($msg); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |             if (count($packages) === 1) { | 
					
						
							| 
									
										
										
										
											2016-04-20 18:55:58 +02:00
										 |  |  |                 $message = Installer::getMessage(); | 
					
						
							|  |  |  |                 if ($message) { | 
					
						
							|  |  |  |                     return $message; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Direct install a file | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param $package_file | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function directInstall($package_file) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |         if (!$package_file) { | 
					
						
							|  |  |  |             return Admin::translate('PLUGIN_ADMIN.NO_PACKAGE_NAME'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |         $tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true); | 
					
						
							|  |  |  |         $tmp_zip = $tmp_dir . '/Grav-' . uniqid(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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(); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |             $extracted  = Installer::unZip($zip, $tmp_source); | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (!$extracted) { | 
					
						
							| 
									
										
										
										
											2017-04-02 20:46:19 -04:00
										 |  |  |                 Folder::delete($tmp_source); | 
					
						
							|  |  |  |                 Folder::delete($tmp_zip); | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                 return Admin::translate('PLUGIN_ADMIN.PACKAGE_EXTRACTION_FAILED'); | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $type = GravGPM::getPackageType($extracted); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!$type) { | 
					
						
							| 
									
										
										
										
											2017-04-02 20:46:19 -04:00
										 |  |  |                 Folder::delete($tmp_source); | 
					
						
							|  |  |  |                 Folder::delete($tmp_zip); | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                 return Admin::translate('PLUGIN_ADMIN.NOT_VALID_GRAV_PACKAGE'); | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |             if ($type === 'grav') { | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |                 Installer::isValidDestination(GRAV_ROOT . '/system'); | 
					
						
							|  |  |  |                 if (Installer::IS_LINK === Installer::lastErrorCode()) { | 
					
						
							| 
									
										
										
										
											2017-04-02 20:46:19 -04:00
										 |  |  |                     Folder::delete($tmp_source); | 
					
						
							|  |  |  |                     Folder::delete($tmp_zip); | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                     return Admin::translate('PLUGIN_ADMIN.CANNOT_OVERWRITE_SYMLINKS'); | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                 Installer::install($zip, GRAV_ROOT, | 
					
						
							| 
									
										
										
										
											2017-04-24 18:03:40 -06:00
										 |  |  |                     ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true, 'ignores' => ['tmp','user','vendor']], $extracted); | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 $name = GravGPM::getPackageName($extracted); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (!$name) { | 
					
						
							| 
									
										
										
										
											2017-04-02 20:46:19 -04:00
										 |  |  |                     Folder::delete($tmp_source); | 
					
						
							|  |  |  |                     Folder::delete($tmp_zip); | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                     return Admin::translate('PLUGIN_ADMIN.NAME_COULD_NOT_BE_DETERMINED'); | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 $install_path = GravGPM::getInstallPath($type, $name); | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |                 $is_update    = file_exists($install_path); | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 Installer::isValidDestination(GRAV_ROOT . DS . $install_path); | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |                 if (Installer::lastErrorCode() === Installer::IS_LINK) { | 
					
						
							| 
									
										
										
										
											2017-04-02 20:46:19 -04:00
										 |  |  |                     Folder::delete($tmp_source); | 
					
						
							|  |  |  |                     Folder::delete($tmp_zip); | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                     return Admin::translate('PLUGIN_ADMIN.CANNOT_OVERWRITE_SYMLINKS'); | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                 Installer::install($zip, GRAV_ROOT, | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |                     ['install_path' => $install_path, 'theme' => $type === 'theme', 'is_update' => $is_update], | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |                     $extracted); | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Folder::delete($tmp_source); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |             if (Installer::lastErrorCode()) { | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |                 return Installer::lastErrorMsg(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |             return Admin::translate('PLUGIN_ADMIN.ZIP_PACKAGE_NOT_FOUND'); | 
					
						
							| 
									
										
										
										
											2017-02-21 08:23:09 -07:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Folder::delete($tmp_zip); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @param Package $package | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-10-13 10:13:18 -07:00
										 |  |  |     private static function download(Package $package, $license = null) | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-10-13 10:13:18 -07:00
										 |  |  |         $query = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($package->premium) { | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |             $query = \json_encode(array_merge($package->premium, [ | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |                 'slug'        => $package->slug, | 
					
						
							|  |  |  |                 'filename'    => $package->premium['filename'], | 
					
						
							|  |  |  |                 'license_key' => $license | 
					
						
							|  |  |  |             ])); | 
					
						
							| 
									
										
										
										
											2016-10-13 10:13:18 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |             $query = '?d=' . base64_encode($query); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $contents = Response::get($package->zipball_url . $query, []); | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             throw new \RuntimeException($e->getMessage()); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 22:00:57 -06:00
										 |  |  |         $tmp_dir = Admin::getTempDir() . '/Grav-' . uniqid(); | 
					
						
							| 
									
										
										
										
											2016-08-23 16:35:41 -07:00
										 |  |  |         Folder::mkdir($tmp_dir); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |         $bad_chars = array_merge(array_map('chr', range(0, 31)), ["<", ">", ":", '"', "/", "\\", "|", "?", "*"]); | 
					
						
							| 
									
										
										
										
											2017-01-19 12:56:58 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $filename = $package->slug . str_replace($bad_chars, "", basename($package->zipball_url)); | 
					
						
							| 
									
										
										
										
											2017-03-20 13:32:03 -06:00
										 |  |  |         $filename = preg_replace('/[\\\\\/:"*?&<>|]+/mi', '-', $filename); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:35:41 -07:00
										 |  |  |         file_put_contents($tmp_dir . DS . $filename . '.zip', $contents); | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-23 16:35:41 -07:00
										 |  |  |         return $tmp_dir . DS . $filename . '.zip'; | 
					
						
							| 
									
										
										
										
											2015-04-13 21:37:12 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-06 09:34:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * @param array  $package | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |      * @param string $tmp | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |      * @return string | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     private static function _downloadSelfupgrade(array $package, $tmp) | 
					
						
							| 
									
										
										
										
											2015-08-06 09:34:55 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         $output = Response::get($package['download'], []); | 
					
						
							|  |  |  |         Folder::mkdir($tmp); | 
					
						
							|  |  |  |         file_put_contents($tmp . DS . $package['name'], $output); | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 09:34:55 +02:00
										 |  |  |         return $tmp . DS . $package['name']; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-21 09:46:38 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-01-10 17:17:04 +01:00
										 |  |  |     public static function selfupgrade() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2015-08-06 09:34:55 +02:00
										 |  |  |         $upgrader = new Upgrader(); | 
					
						
							| 
									
										
										
										
											2015-08-07 13:31:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!Installer::isGravInstance(GRAV_ROOT)) { | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (is_link(GRAV_ROOT . DS . 'index.php')) { | 
					
						
							|  |  |  |             Installer::setError(Installer::IS_LINK); | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-07 13:31:15 -07:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-18 14:13:09 -07:00
										 |  |  |         if (method_exists($upgrader, 'meetsRequirements') && | 
					
						
							|  |  |  |             method_exists($upgrader, 'minPHPVersion') && | 
					
						
							|  |  |  |             !$upgrader->meetsRequirements()) { | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |             $error   = []; | 
					
						
							| 
									
										
										
										
											2015-12-18 13:42:57 -08:00
										 |  |  |             $error[] = '<p>Grav has increased the minimum PHP requirement.<br />'; | 
					
						
							| 
									
										
										
										
											2018-02-18 14:13:09 -07:00
										 |  |  |             $error[] = 'You are currently running PHP <strong>' . phpversion() . '</strong>'; | 
					
						
							|  |  |  |             $error[] = ', but PHP <strong>' . $upgrader->minPHPVersion() . '</strong> is required.</p>'; | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |             $error[] = '<p><a href="http://getgrav.org/blog/changing-php-requirements-to-5.5" class="button button-small secondary">Additional information</a></p>'; | 
					
						
							| 
									
										
										
										
											2015-12-18 13:42:57 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             Installer::setError(implode("\n", $error)); | 
					
						
							| 
									
										
										
										
											2016-07-07 18:55:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-18 13:42:57 -08:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 09:34:55 +02:00
										 |  |  |         $update = $upgrader->getAssets()['grav-update']; | 
					
						
							| 
									
										
										
										
											2017-02-26 19:36:01 +01:00
										 |  |  |         $tmp    = Admin::getTempDir() . '/Grav-' . uniqid(); | 
					
						
							|  |  |  |         $file   = self::_downloadSelfupgrade($update, $tmp); | 
					
						
							| 
									
										
										
										
											2015-08-06 09:34:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 21:34:21 +01:00
										 |  |  |         Installer::install($file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]); | 
					
						
							| 
									
										
										
										
											2015-08-06 09:34:55 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $errorCode = Installer::lastErrorCode(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Folder::delete($tmp); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-09 12:24:01 +03:00
										 |  |  |         return !($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)); | 
					
						
							| 
									
										
										
										
											2015-08-06 09:34:55 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-06 16:35:45 +02:00
										 |  |  | } |