/**
  * @param InputInterface  $input
  * @param OutputInterface $output
  */
 private function cleanPaths(InputInterface $input, OutputInterface $output)
 {
     $output->writeln('');
     $output->writeln('<magenta>Clearing cache</magenta>');
     $output->writeln('');
     $user_config = USER_DIR . 'config/system.yaml';
     $anything = false;
     if ($input->getOption('all')) {
         $remove_paths = $this->all_remove;
     } elseif ($input->getOption('assets-only')) {
         $remove_paths = $this->assets_remove;
     } elseif ($input->getOption('images-only')) {
         $remove_paths = $this->images_remove;
     } elseif ($input->getOption('cache-only')) {
         $remove_paths = $this->cache_remove;
     } else {
         $remove_paths = $this->standard_remove;
     }
     foreach ($remove_paths as $path) {
         $files = glob(ROOT_DIR . $path . '*');
         foreach ($files as $file) {
             if (is_file($file)) {
                 if (@unlink($file)) {
                     $anything = true;
                 }
             } elseif (is_dir($file)) {
                 if (@Folder::delete($file)) {
                     $anything = true;
                 }
             }
         }
         if ($anything) {
             $output->writeln('<red>Cleared:  </red>' . $path . '*');
         }
     }
     if (file_exists($user_config)) {
         touch($user_config);
         $output->writeln('');
         $output->writeln('<red>Touched: </red>' . $user_config);
         $output->writeln('');
     }
     if (!$anything) {
         $output->writeln('<green>Nothing to clear...</green>');
         $output->writeln('');
     }
 }
 private function cleanPaths()
 {
     $this->output->writeln('');
     $this->output->writeln('<red>DELETING</red>');
     $anything = false;
     foreach ($this->paths_to_remove as $path) {
         $path = ROOT_DIR . $path;
         if (is_dir($path) && @Folder::delete($path)) {
             $anything = true;
             $this->output->writeln('<red>dir:  </red>' . $path);
         } elseif (is_file($path) && @unlink($path)) {
             $anything = true;
             $this->output->writeln('<red>file: </red>' . $path);
         }
     }
     if (!$anything) {
         $this->output->writeln('');
         $this->output->writeln('<green>Nothing to clean...</green>');
     }
 }
Beispiel #3
0
 public static function install($packages, $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;
     }
     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;
         }
         $local = static::download($package);
         Installer::install($local, $options['destination'], ['install_path' => $package->install_path]);
         Folder::delete(dirname($local));
         $errorCode = Installer::lastErrorCode();
         if (Installer::lastErrorCode() & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) {
             return false;
         }
     }
     return true;
 }
Beispiel #4
0
 /**
  * Uninstalls one or more given package
  *
  * @param  string $path     The slug of the package(s)
  * @param  array  $options     Options to use for uninstalling
  *
  * @return boolean True if everything went fine, False otherwise.
  */
 public static function uninstall($path, $options = [])
 {
     $options = array_merge(self::$options, $options);
     if (!self::isValidDestination($path, $options['exclude_checks'])) {
         return false;
     }
     return Folder::delete($path);
 }
Beispiel #5
0
 /**
  *
  */
 private function symlink()
 {
     $this->output->writeln('');
     $this->output->writeln('<comment>Resetting Symbolic Links</comment>');
     foreach ($this->mappings as $source => $target) {
         if ((int) $source == $source) {
             $source = $target;
         }
         $from = $this->source . $source;
         $to = $this->destination . $target;
         $this->output->writeln('    <cyan>' . $source . '</cyan> <comment>-></comment> ' . $to);
         if (is_dir($to)) {
             @Folder::delete($to);
         } else {
             @unlink($to);
         }
         symlink($from, $to);
     }
 }
Beispiel #6
0
 /**
  * Helper method to clear all Grav caches
  *
  * @param string $remove    standard|all|assets-only|images-only|cache-only
  *
  * @return array
  */
 public static function clearCache($remove = 'standard')
 {
     $output = [];
     $user_config = USER_DIR . 'config/system.yaml';
     switch ($remove) {
         case 'all':
             $remove_paths = self::$all_remove;
             break;
         case 'assets-only':
             $remove_paths = self::$assets_remove;
             break;
         case 'images-only':
             $remove_paths = self::$images_remove;
             break;
         case 'cache-only':
             $remove_paths = self::$cache_remove;
             break;
         default:
             $remove_paths = self::$standard_remove;
     }
     foreach ($remove_paths as $path) {
         $anything = false;
         $files = glob(ROOT_DIR . $path . '*');
         if (is_array($files)) {
             foreach ($files as $file) {
                 if (is_file($file)) {
                     if (@unlink($file)) {
                         $anything = true;
                     }
                 } elseif (is_dir($file)) {
                     if (@Folder::delete($file)) {
                         $anything = true;
                     }
                 }
             }
         }
         if ($anything) {
             $output[] = '<red>Cleared:  </red>' . $path . '*';
         }
     }
     $output[] = '';
     if (($remove == 'all' || $remove == 'standard') && file_exists($user_config)) {
         touch($user_config);
         $output[] = '<red>Touched: </red>' . $user_config;
         $output[] = '';
     }
     return $output;
 }
 /**
  * @param $package
  *
  * @return bool
  */
 private function installPackage($package)
 {
     Installer::install($this->file, $this->destination, ['install_path' => $package->install_path]);
     $errorCode = Installer::lastErrorCode();
     Folder::delete($this->tmp);
     if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) {
         $this->output->write("\r");
         // extra white spaces to clear out the buffer properly
         $this->output->writeln("  |- Installing package...    <red>error</red>                             ");
         $this->output->writeln("  |  '- " . Installer::lastErrorMsg());
         return false;
     }
     $this->output->write("\r");
     // extra white spaces to clear out the buffer properly
     $this->output->writeln("  |- Installing package...    <green>ok</green>                             ");
     return true;
 }
Beispiel #8
0
 /**
  * Helper method to clear all Grav caches
  *
  * @param string $remove    standard|all|assets-only|images-only|cache-only
  *
  * @return array
  */
 public static function clearCache($remove = 'standard')
 {
     $locator = self::getGrav()['locator'];
     $output = [];
     $user_config = USER_DIR . 'config/system.yaml';
     switch ($remove) {
         case 'all':
             $remove_paths = self::$all_remove;
             break;
         case 'assets-only':
             $remove_paths = self::$assets_remove;
             break;
         case 'images-only':
             $remove_paths = self::$images_remove;
             break;
         case 'cache-only':
             $remove_paths = self::$cache_remove;
             break;
         default:
             $remove_paths = self::$standard_remove;
     }
     foreach ($remove_paths as $stream) {
         // Convert stream to a real path
         $path = $locator->findResource($stream, true, true);
         // Make sure path exists before proceeding, otherwise we would wipe ROOT_DIR
         if (!$path) {
             throw new \RuntimeException("Stream '{$stream}' not found", 500);
         }
         $anything = false;
         $files = glob($path . '/*');
         if (is_array($files)) {
             foreach ($files as $file) {
                 if (is_file($file)) {
                     if (@unlink($file)) {
                         $anything = true;
                     }
                 } elseif (is_dir($file)) {
                     if (@Folder::delete($file)) {
                         $anything = true;
                     }
                 }
             }
         }
         if ($anything) {
             $output[] = '<red>Cleared:  </red>' . $path . '/*';
         }
     }
     $output[] = '';
     if (($remove == 'all' || $remove == 'standard') && file_exists($user_config)) {
         touch($user_config);
         $output[] = '<red>Touched: </red>' . $user_config;
         $output[] = '';
     }
     return $output;
 }
 /**
  * Delete page.
  *
  * @return bool True if the action was performed.
  * @throws \RuntimeException
  */
 protected function taskDelete()
 {
     if (!$this->authoriseTask('delete page', ['admin.pages', 'admin.super'])) {
         return;
     }
     // Only applies to pages.
     if ($this->view != 'pages') {
         return false;
     }
     /** @var Uri $uri */
     $uri = $this->grav['uri'];
     try {
         $page = $this->admin->page();
         Folder::delete($page->path());
         $results = Cache::clearCache('standard');
         // Set redirect to either referrer or pages list.
         $redirect = $uri->referrer();
         if ($redirect == $uri->route()) {
             $redirect = 'pages';
         }
         $this->admin->setMessage('Successfully deleted', 'info');
         $this->setRedirect($redirect);
     } catch (\Exception $e) {
         throw new \RuntimeException('Deleting page failed on error: ' . $e->getMessage());
     }
     return true;
 }
Beispiel #10
0
 /**
  * Helper method to clear all Grav caches
  *
  * @param string $remove standard|all|assets-only|images-only|cache-only
  *
  * @return array
  */
 public static function clearCache($remove = 'standard')
 {
     $locator = Grav::instance()['locator'];
     $output = [];
     $user_config = USER_DIR . 'config/system.yaml';
     switch ($remove) {
         case 'all':
             $remove_paths = self::$all_remove;
             break;
         case 'assets-only':
             $remove_paths = self::$assets_remove;
             break;
         case 'images-only':
             $remove_paths = self::$images_remove;
             break;
         case 'cache-only':
             $remove_paths = self::$cache_remove;
             break;
         case 'tmp-only':
             $remove_paths = self::$tmp_remove;
             break;
         default:
             $remove_paths = self::$standard_remove;
     }
     foreach ($remove_paths as $stream) {
         // Convert stream to a real path
         try {
             $path = $locator->findResource($stream, true, true);
         } catch (\Exception $e) {
             // stream not found..
             continue;
         }
         $anything = false;
         $files = glob($path . '/*');
         if (is_array($files)) {
             foreach ($files as $file) {
                 if (is_file($file)) {
                     if (@unlink($file)) {
                         $anything = true;
                     }
                 } elseif (is_dir($file)) {
                     if (Folder::delete($file)) {
                         $anything = true;
                     }
                 }
             }
         }
         if ($anything) {
             $output[] = '<red>Cleared:  </red>' . $path . '/*';
         }
     }
     $output[] = '';
     if (($remove == 'all' || $remove == 'standard') && file_exists($user_config)) {
         touch($user_config);
         $output[] = '<red>Touched: </red>' . $user_config;
         $output[] = '';
     }
     return $output;
 }
Beispiel #11
0
 /**
  * Delete page.
  *
  * @return bool True if the action was performed.
  * @throws \RuntimeException
  */
 protected function taskDelete()
 {
     if (!$this->authorizeTask('delete page', ['admin.pages', 'admin.super'])) {
         return;
     }
     // Only applies to pages.
     if ($this->view != 'pages') {
         return false;
     }
     /** @var Uri $uri */
     $uri = $this->grav['uri'];
     try {
         $page = $this->admin->page();
         if (count($page->translatedLanguages()) > 1) {
             $page->file()->delete();
         } else {
             Folder::delete($page->path());
         }
         $results = Cache::clearCache('standard');
         // Set redirect to either referrer or pages list.
         $redirect = 'pages';
         $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.SUCCESSFULLY_DELETED'), 'info');
         $this->setRedirect($redirect);
     } catch (\Exception $e) {
         throw new \RuntimeException('Deleting page failed on error: ' . $e->getMessage());
     }
     return true;
 }
 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;
     }
     $update = $upgrader->getAssets()['grav-update'];
     $tmp = CACHE_DIR . 'tmp/Grav-' . uniqid();
     $file = self::_downloadSelfupgrade($update, $tmp);
     Installer::install($file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]);
     $errorCode = Installer::lastErrorCode();
     Folder::delete($tmp);
     if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) {
         return false;
     }
     return true;
 }
 /**
  * Install a package
  *
  * @param Package $package
  * @param bool    $is_update True if it's an update. False if it's an install
  *
  * @return bool
  */
 private function installPackage($package, $is_update = false)
 {
     $type = $package->package_type;
     Installer::install($this->file, $this->destination, ['install_path' => $package->install_path, 'theme' => $type == 'themes', 'is_update' => $is_update]);
     $error_code = Installer::lastErrorCode();
     Folder::delete($this->tmp);
     if ($error_code) {
         $this->output->write("\r");
         // extra white spaces to clear out the buffer properly
         $this->output->writeln("  |- Installing package...    <red>error</red>                             ");
         $this->output->writeln("  |  '- " . Installer::lastErrorMsg());
         return false;
     }
     $message = Installer::getMessage();
     if ($message) {
         $this->output->write("\r");
         // extra white spaces to clear out the buffer properly
         $this->output->writeln("  |- " . $message);
     }
     $this->output->write("\r");
     // extra white spaces to clear out the buffer properly
     $this->output->writeln("  |- Installing package...    <green>ok</green>                             ");
     return true;
 }
 /**
  * @return int|null|void
  */
 protected function serve()
 {
     $package_file = $this->input->getArgument('package-file');
     $helper = $this->getHelper('question');
     $question = new ConfirmationQuestion('Are you sure you want to direct-install <cyan>' . $package_file . '</cyan> [y|N] ', false);
     $answer = $helper->ask($this->input, $this->output, $question);
     if (!$answer) {
         $this->output->writeln("exiting...");
         $this->output->writeln('');
         exit;
     }
     $tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true);
     $tmp_zip = $tmp_dir . '/Grav-' . uniqid();
     $this->output->writeln("");
     $this->output->writeln("Preparing to install <cyan>" . $package_file . "</cyan>");
     if ($this->isRemote($package_file)) {
         $zip = $this->downloadPackage($package_file, $tmp_zip);
     } else {
         $zip = $this->copyPackage($package_file, $tmp_zip);
     }
     if (file_exists($zip)) {
         $tmp_source = $tmp_dir . '/Grav-' . uniqid();
         $this->output->write("  |- Extracting package...    ");
         $extracted = Installer::unZip($zip, $tmp_source);
         if (!$extracted) {
             $this->output->write("\r");
             $this->output->writeln("  |- Extracting package...    <red>failed</red>");
             exit;
         }
         $this->output->write("\r");
         $this->output->writeln("  |- Extracting package...    <green>ok</green>");
         $type = $this->getPackageType($extracted);
         if (!$type) {
             $this->output->writeln("  '- <red>ERROR: Not a valid Grav package</red>");
             $this->output->writeln('');
             exit;
         }
         $blueprint = $this->getBlueprints($extracted);
         if ($blueprint) {
             if (isset($blueprint['dependencies'])) {
                 $depencencies = [];
                 foreach ($blueprint['dependencies'] as $dependency) {
                     if (is_array($dependency) && isset($dependency['name'])) {
                         $depencencies[] = $dependency['name'];
                     } else {
                         $depencencies[] = $dependency;
                     }
                 }
                 $this->output->writeln("  |- Dependencies found...    <cyan>[" . implode(',', $depencencies) . "]</cyan>");
                 $question = new ConfirmationQuestion("  |  '- Dependencies will not be satisfied. Continue ? [y|N] ", false);
                 $answer = $helper->ask($this->input, $this->output, $question);
                 if (!$answer) {
                     $this->output->writeln("exiting...");
                     $this->output->writeln('');
                     exit;
                 }
             }
         }
         if ($type == 'grav') {
             $this->output->write("  |- Checking destination...  ");
             Installer::isValidDestination(GRAV_ROOT . '/system');
             if (Installer::IS_LINK === Installer::lastErrorCode()) {
                 $this->output->write("\r");
                 $this->output->writeln("  |- Checking destination...  <yellow>symbolic link</yellow>");
                 $this->output->writeln("  '- <red>ERROR: symlinks found...</red> <yellow>" . GRAV_ROOT . "</yellow>");
                 $this->output->writeln('');
                 exit;
             }
             $this->output->write("\r");
             $this->output->writeln("  |- Checking destination...  <green>ok</green>");
             $this->output->write("  |- Installing package...  ");
             Installer::install($zip, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true], $extracted);
         } else {
             $name = $this->getPackageName($extracted);
             if (!$name) {
                 $this->output->writeln("<red>ERROR: Name could not be determined.</red> Please specify with --name|-n");
                 $this->output->writeln('');
                 exit;
             }
             $install_path = $this->getInstallPath($type, $name);
             $is_update = file_exists($install_path);
             $this->output->write("  |- Checking destination...  ");
             Installer::isValidDestination(GRAV_ROOT . DS . $install_path);
             if (Installer::lastErrorCode() == Installer::IS_LINK) {
                 $this->output->write("\r");
                 $this->output->writeln("  |- Checking destination...  <yellow>symbolic link</yellow>");
                 $this->output->writeln("  '- <red>ERROR: symlink found...</red>  <yellow>" . GRAV_ROOT . DS . $install_path . '</yellow>');
                 $this->output->writeln('');
                 exit;
             } else {
                 $this->output->write("\r");
                 $this->output->writeln("  |- Checking destination...  <green>ok</green>");
             }
             $this->output->write("  |- Installing package...  ");
             Installer::install($zip, GRAV_ROOT, ['install_path' => $install_path, 'theme' => $type == 'theme', 'is_update' => $is_update], $extracted);
         }
         Folder::delete($tmp_source);
         $this->output->write("\r");
         if (Installer::lastErrorCode()) {
             $this->output->writeln("  '- <red>Installation failed or aborted.</red>");
             $this->output->writeln('');
         } else {
             $this->output->writeln("  |- Installing package...    <green>ok</green>");
             $this->output->writeln("  '- <green>Success!</green>  ");
             $this->output->writeln('');
         }
     } else {
         $this->output->writeln("  '- <red>ERROR: ZIP package could not be found</red>");
     }
     Folder::delete($tmp_zip);
     // clear cache after successful upgrade
     $this->clearCache();
     return true;
 }
Beispiel #15
0
 /**
  * @return bool
  */
 private function upgrade()
 {
     Installer::install($this->file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]);
     $errorCode = Installer::lastErrorCode();
     Folder::delete($this->tmp);
     if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) {
         $this->output->write("\r");
         // extra white spaces to clear out the buffer properly
         $this->output->writeln("  |- Installing upgrade...    <red>error</red>                             ");
         $this->output->writeln("  |  '- " . Installer::lastErrorMsg());
         return false;
     }
     $this->output->write("\r");
     // extra white spaces to clear out the buffer properly
     $this->output->writeln("  |- Installing upgrade...    <green>ok</green>                             ");
     return true;
 }
Beispiel #16
0
 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') && !$upgrader->meetsRequirements()) {
         $error = [];
         $error[] = '<p>Grav has increased the minimum PHP requirement.<br />';
         $error[] = 'You are currently running PHP <strong>' . PHP_VERSION . '</strong>';
         $error[] = ', but PHP <strong>' . GRAV_PHP_MIN . '</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 = CACHE_DIR . 'tmp/Grav-' . uniqid();
     $file = self::_downloadSelfupgrade($update, $tmp);
     Installer::install($file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]);
     $errorCode = Installer::lastErrorCode();
     Folder::delete($tmp);
     if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) {
         return false;
     }
     return true;
 }
Beispiel #17
0
 /**
  * Uninstalls one or more given package
  *
  * @param  string $path    The slug of the package(s)
  * @param  array  $options Options to use for uninstalling
  *
  * @return boolean True if everything went fine, False otherwise.
  */
 public static function uninstall($path, $options = [])
 {
     $options = array_merge(self::$options, $options);
     if (!self::isValidDestination($path, $options['exclude_checks'])) {
         return false;
     }
     $installer_file_folder = $path;
     $is_install = false;
     $installer = self::loadInstaller($installer_file_folder, $is_install);
     if ($installer && method_exists($installer, 'preUninstall')) {
         $method_result = $installer::preUninstall();
         if ($method_result !== true) {
             self::$error = 'An error occurred';
             if (is_string($method_result)) {
                 self::$error = $method_result;
             }
             return false;
         }
     }
     $result = Folder::delete($path);
     self::$message = '';
     if ($result && $installer && method_exists($installer, 'postUninstall')) {
         self::$message = $installer::postUninstall();
     }
     return $result;
 }