gpm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace Grav\Plugin\Admin;
  3. use Grav\Common\Grav;
  4. use Grav\Common\GPM\GPM as GravGPM;
  5. use Grav\Common\GPM\Licenses;
  6. use Grav\Common\GPM\Installer;
  7. use Grav\Common\GPM\Response;
  8. use Grav\Common\GPM\Upgrader;
  9. use Grav\Common\Filesystem\Folder;
  10. use Grav\Common\GPM\Common\Package;
  11. use Grav\Plugin\Admin\Admin;
  12. /**
  13. * Class Gpm
  14. *
  15. * @package Grav\Plugin\Admin
  16. */
  17. class Gpm
  18. {
  19. // Probably should move this to Grav DI container?
  20. /** @var GravGPM */
  21. protected static $GPM;
  22. public static function GPM()
  23. {
  24. if (!static::$GPM) {
  25. static::$GPM = new GravGPM();
  26. if (method_exists('GravGPM', 'loadRemoteGrav')) {
  27. static::$GPM->loadRemoteGrav();
  28. }
  29. }
  30. return static::$GPM;
  31. }
  32. /**
  33. * Default options for the install
  34. *
  35. * @var array
  36. */
  37. protected static $options = [
  38. 'destination' => GRAV_ROOT,
  39. 'overwrite' => true,
  40. 'ignore_symlinks' => true,
  41. 'skip_invalid' => true,
  42. 'install_deps' => true,
  43. 'theme' => false
  44. ];
  45. /**
  46. * @param Package[]|string[]|string $packages
  47. * @param array $options
  48. *
  49. * @return bool
  50. */
  51. public static function install($packages, array $options)
  52. {
  53. $options = array_merge(self::$options, $options);
  54. if (!Installer::isGravInstance($options['destination']) || !Installer::isValidDestination($options['destination'],
  55. [Installer::EXISTS, Installer::IS_LINK])
  56. ) {
  57. return false;
  58. }
  59. $packages = is_array($packages) ? $packages : [$packages];
  60. $count = count($packages);
  61. $packages = array_filter(array_map(function ($p) {
  62. return !is_string($p) ? $p instanceof Package ? $p : false : self::GPM()->findPackage($p);
  63. }, $packages));
  64. if (!$options['skip_invalid'] && $count !== count($packages)) {
  65. return false;
  66. }
  67. $messages = '';
  68. foreach ($packages as $package) {
  69. if (isset($package->dependencies) && $options['install_deps']) {
  70. $result = static::install($package->dependencies, $options);
  71. if (!$result) {
  72. return false;
  73. }
  74. }
  75. // Check destination
  76. Installer::isValidDestination($options['destination'] . DS . $package->install_path);
  77. if (Installer::lastErrorCode() === Installer::EXISTS && !$options['overwrite']) {
  78. return false;
  79. }
  80. if (Installer::lastErrorCode() === Installer::IS_LINK && !$options['ignore_symlinks']) {
  81. return false;
  82. }
  83. $license = Licenses::get($package->slug);
  84. $local = static::download($package, $license);
  85. Installer::install($local, $options['destination'],
  86. ['install_path' => $package->install_path, 'theme' => $options['theme']]);
  87. Folder::delete(dirname($local));
  88. $errorCode = Installer::lastErrorCode();
  89. if ($errorCode) {
  90. $msg = Installer::lastErrorMsg();
  91. throw new \RuntimeException($msg);
  92. }
  93. if (count($packages) === 1) {
  94. $message = Installer::getMessage();
  95. if ($message) {
  96. return $message;
  97. }
  98. $messages .= $message;
  99. }
  100. }
  101. return $messages ?: true;
  102. }
  103. /**
  104. * @param Package[]|string[]|string $packages
  105. * @param array $options
  106. *
  107. * @return bool
  108. */
  109. public static function update($packages, array $options)
  110. {
  111. $options['overwrite'] = true;
  112. return static::install($packages, $options);
  113. }
  114. /**
  115. * @param Package[]|string[]|string $packages
  116. * @param array $options
  117. *
  118. * @return bool
  119. */
  120. public static function uninstall($packages, array $options)
  121. {
  122. $options = array_merge(self::$options, $options);
  123. $packages = is_array($packages) ? $packages : [$packages];
  124. $count = count($packages);
  125. $packages = array_filter(array_map(function ($p) {
  126. if (is_string($p)) {
  127. $p = strtolower($p);
  128. $plugin = static::GPM()->getInstalledPlugin($p);
  129. $p = $plugin ?: static::GPM()->getInstalledTheme($p);
  130. }
  131. return $p instanceof Package ? $p : false;
  132. }, $packages));
  133. if (!$options['skip_invalid'] && $count !== count($packages)) {
  134. return false;
  135. }
  136. foreach ($packages as $package) {
  137. $location = Grav::instance()['locator']->findResource($package->package_type . '://' . $package->slug);
  138. // Check destination
  139. Installer::isValidDestination($location);
  140. if (!$options['ignore_symlinks'] && Installer::lastErrorCode() === Installer::IS_LINK) {
  141. return false;
  142. }
  143. Installer::uninstall($location);
  144. $errorCode = Installer::lastErrorCode();
  145. if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) {
  146. $msg = Installer::lastErrorMsg();
  147. throw new \RuntimeException($msg);
  148. }
  149. if (count($packages) === 1) {
  150. $message = Installer::getMessage();
  151. if ($message) {
  152. return $message;
  153. }
  154. }
  155. }
  156. return true;
  157. }
  158. /**
  159. * Direct install a file
  160. *
  161. * @param $package_file
  162. *
  163. * @return bool
  164. */
  165. public static function directInstall($package_file)
  166. {
  167. if (!$package_file) {
  168. return Admin::translate('PLUGIN_ADMIN.NO_PACKAGE_NAME');
  169. }
  170. $tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true);
  171. $tmp_zip = $tmp_dir . '/Grav-' . uniqid();
  172. if (Response::isRemote($package_file)) {
  173. $zip = GravGPM::downloadPackage($package_file, $tmp_zip);
  174. } else {
  175. $zip = GravGPM::copyPackage($package_file, $tmp_zip);
  176. }
  177. if (file_exists($zip)) {
  178. $tmp_source = $tmp_dir . '/Grav-' . uniqid();
  179. $extracted = Installer::unZip($zip, $tmp_source);
  180. if (!$extracted) {
  181. Folder::delete($tmp_source);
  182. Folder::delete($tmp_zip);
  183. return Admin::translate('PLUGIN_ADMIN.PACKAGE_EXTRACTION_FAILED');
  184. }
  185. $type = GravGPM::getPackageType($extracted);
  186. if (!$type) {
  187. Folder::delete($tmp_source);
  188. Folder::delete($tmp_zip);
  189. return Admin::translate('PLUGIN_ADMIN.NOT_VALID_GRAV_PACKAGE');
  190. }
  191. if ($type === 'grav') {
  192. Installer::isValidDestination(GRAV_ROOT . '/system');
  193. if (Installer::IS_LINK === Installer::lastErrorCode()) {
  194. Folder::delete($tmp_source);
  195. Folder::delete($tmp_zip);
  196. return Admin::translate('PLUGIN_ADMIN.CANNOT_OVERWRITE_SYMLINKS');
  197. }
  198. Installer::install($zip, GRAV_ROOT,
  199. ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true, 'ignores' => ['tmp','user','vendor']], $extracted);
  200. } else {
  201. $name = GravGPM::getPackageName($extracted);
  202. if (!$name) {
  203. Folder::delete($tmp_source);
  204. Folder::delete($tmp_zip);
  205. return Admin::translate('PLUGIN_ADMIN.NAME_COULD_NOT_BE_DETERMINED');
  206. }
  207. $install_path = GravGPM::getInstallPath($type, $name);
  208. $is_update = file_exists($install_path);
  209. Installer::isValidDestination(GRAV_ROOT . DS . $install_path);
  210. if (Installer::lastErrorCode() === Installer::IS_LINK) {
  211. Folder::delete($tmp_source);
  212. Folder::delete($tmp_zip);
  213. return Admin::translate('PLUGIN_ADMIN.CANNOT_OVERWRITE_SYMLINKS');
  214. }
  215. Installer::install($zip, GRAV_ROOT,
  216. ['install_path' => $install_path, 'theme' => $type === 'theme', 'is_update' => $is_update],
  217. $extracted);
  218. }
  219. Folder::delete($tmp_source);
  220. if (Installer::lastErrorCode()) {
  221. return Installer::lastErrorMsg();
  222. }
  223. } else {
  224. return Admin::translate('PLUGIN_ADMIN.ZIP_PACKAGE_NOT_FOUND');
  225. }
  226. Folder::delete($tmp_zip);
  227. return true;
  228. }
  229. /**
  230. * @param Package $package
  231. *
  232. * @return string
  233. */
  234. private static function download(Package $package, $license = null)
  235. {
  236. $query = '';
  237. if ($package->premium) {
  238. $query = \json_encode(array_merge($package->premium, [
  239. 'slug' => $package->slug,
  240. 'filename' => $package->premium['filename'],
  241. 'license_key' => $license
  242. ]));
  243. $query = '?d=' . base64_encode($query);
  244. }
  245. try {
  246. $contents = Response::get($package->zipball_url . $query, []);
  247. } catch (\Exception $e) {
  248. throw new \RuntimeException($e->getMessage());
  249. }
  250. $tmp_dir = Admin::getTempDir() . '/Grav-' . uniqid();
  251. Folder::mkdir($tmp_dir);
  252. $bad_chars = array_merge(array_map('chr', range(0, 31)), ["<", ">", ":", '"', "/", "\\", "|", "?", "*"]);
  253. $filename = $package->slug . str_replace($bad_chars, "", basename($package->zipball_url));
  254. $filename = preg_replace('/[\\\\\/:"*?&<>|]+/mi', '-', $filename);
  255. file_put_contents($tmp_dir . DS . $filename . '.zip', $contents);
  256. return $tmp_dir . DS . $filename . '.zip';
  257. }
  258. /**
  259. * @param array $package
  260. * @param string $tmp
  261. *
  262. * @return string
  263. */
  264. private static function _downloadSelfupgrade(array $package, $tmp)
  265. {
  266. $output = Response::get($package['download'], []);
  267. Folder::mkdir($tmp);
  268. file_put_contents($tmp . DS . $package['name'], $output);
  269. return $tmp . DS . $package['name'];
  270. }
  271. /**
  272. * @return bool
  273. */
  274. public static function selfupgrade()
  275. {
  276. $upgrader = new Upgrader();
  277. if (!Installer::isGravInstance(GRAV_ROOT)) {
  278. return false;
  279. }
  280. if (is_link(GRAV_ROOT . DS . 'index.php')) {
  281. Installer::setError(Installer::IS_LINK);
  282. return false;
  283. }
  284. if (method_exists($upgrader, 'meetsRequirements') &&
  285. method_exists($upgrader, 'minPHPVersion') &&
  286. !$upgrader->meetsRequirements()) {
  287. $error = [];
  288. $error[] = '<p>Grav has increased the minimum PHP requirement.<br />';
  289. $error[] = 'You are currently running PHP <strong>' . phpversion() . '</strong>';
  290. $error[] = ', but PHP <strong>' . $upgrader->minPHPVersion() . '</strong> is required.</p>';
  291. $error[] = '<p><a href="http://getgrav.org/blog/changing-php-requirements-to-5.5" class="button button-small secondary">Additional information</a></p>';
  292. Installer::setError(implode("\n", $error));
  293. return false;
  294. }
  295. $update = $upgrader->getAssets()['grav-update'];
  296. $tmp = Admin::getTempDir() . '/Grav-' . uniqid();
  297. $file = self::_downloadSelfupgrade($update, $tmp);
  298. Installer::install($file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]);
  299. $errorCode = Installer::lastErrorCode();
  300. Folder::delete($tmp);
  301. return !($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR));
  302. }
  303. }