/** * Stop the process of the cron server * @return null */ public function stop() { if (!$this->pid) { throw new ZiboException('The cron process is not running'); } System::execute('kill ' . $this->pid); }
public function testExecuteWithInvalidCommandThrowsException() { try { System::execute('unknownCommand'); } catch (ZiboException $e) { return; } $this->fail(); }
/** * Action to show and process the terminal form * @return null */ public function indexAction() { $action = $this->request->getBasePath(); $form = new TerminalForm($action); if ($form->isSubmitted()) { $command = $form->getCommand(); $output = ''; $isError = false; try { if ($this->path) { chdir($this->path); } $tokens = explode(' ', $command); if ($tokens[0] == 'cd') { // handle a change directory command try { if (array_key_exists(1, $tokens) && $tokens[1]) { chdir($tokens[1]); } else { chdir($this->defaultPath); } $this->path = getcwd(); } catch (ErrorException $exception) { $output = 'Error: No such file or directory'; $isError = true; } } else { // any other command $output = System::execute($command); $output = htmlentities($output); } } catch (ZiboException $exception) { $output = 'Error: ' . $exception->getMessage(); $isError = true; } if ($this->path) { chdir($this->defaultPath); } $result = array('path' => $this->path ? $this->path : $this->defaultPath, 'command' => $command, 'output' => $output, 'error' => $isError); $view = new JsonView($result); } else { $view = new TerminalView($form, $this->path ? $this->path : $this->defaultPath); } $this->response->setView($view); }
public function indexAction() { if (func_get_args()) { $this->setError404(); return; } $id = $this->getSession()->getId(); $fileSpider = new File(self::PATH_DATA, $id . self::SUFFIX_SPIDER); $baseUrl = $this->request->getBaseUrl(); $basePath = $this->request->getBasePath(); $formAction = $basePath; if ($basePath == $baseUrl) { $formAction .= '/'; } $form = new SpiderForm($formAction); if ($form->isSubmitted()) { if ($form->isCancelled()) { $fileCancel = new File(self::PATH_DATA, $id . self::SUFFIX_CANCEL); $fileCancel->write('1'); return; } try { $form->validate(); $url = $form->getUrl(); $delay = $form->getDelay(); $ignore = $form->getIgnore(); $ignore = explode("\n", $ignore); $spider = new Spider($url); foreach ($ignore as $ignoreRegex) { $ignoreRegex = trim($ignoreRegex); if (!$ignoreRegex) { continue; } $spider->addIgnoreRegex($ignoreRegex); } $spider->addBite(new AnchorSpiderBite()); $spider->addBite(new CssSpiderBite()); $spider->addBite(new CssImageSpiderBite()); $spider->addBite(new CssImportSpiderBite()); $spider->addBite(new ImageSpiderBite()); $spider->addBite(new JsSpiderBite()); $spider->addReport(new ErrorReport()); $spider->addReport(new RedirectReport()); $spider->addReport(new SuccessReport()); $spider->addReport(new ImageReport()); $spider->addReport(new CssReport()); $spider->addReport(new JsReport()); $spider->addReport(new ExternalReport()); $spider->addReport(new MailtoReport()); $spider->addReport(new IgnoredReport()); $parent = $fileSpider->getParent(); $parent->create(); $fileSpider->write(serialize($spider)); $php = Zibo::getInstance()->getConfigValue(self::CONFIG_PHP_COMMAND, self::DEFAULT_PHP_COMMAND); System::execute($php . ' ' . $_SERVER['SCRIPT_FILENAME'] . ' spider/crawl/' . $id . '/' . $delay . ' > /dev/null 2> /dev/null & echo $!'); return; } catch (ValidationException $exception) { $form->setValidationException($exception); } } if ($fileSpider->exists()) { $fileSpiderContent = $fileSpider->read(); $spider = unserialize($fileSpiderContent); $form->setUrl($spider->getBaseUrl()); $form->setIsDisabled(true, SpiderForm::FIELD_URL); $form->setIsDisabled(true, SpiderForm::BUTTON_SUBMIT); } $statusUrl = $basePath . '/status/' . $id; $reportUrl = $basePath . '/report/' . $id; $view = new SpiderView($form, $statusUrl, $reportUrl); $view->setTitle('spider.title', true); $this->response->setView($view); }
/** * @dataProvider providerExecuteWithInvalidCommandThrowsException * @expectedException zibo\ZiboException */ public function testExecuteWithInvalidCommandThrowsException($command) { System::execute($command); }
/** * Exports the database to a file * @param zibo\library\filesystem\File $file * @return null * @throws zibo\ZiboException when an error eoccured */ public function export(File $file) { $extension = $file->getExtension(); if ($extension != 'sql') { throw new MysqlException('Provided file needs to have an sql extension'); } $dsn = $this->getDsn(); $username = $dsn->getUsername(); $password = $dsn->getPassword(); $database = $dsn->getDatabase(); $command = 'mysqldump --user='******' --password='******' ' . $database; $command .= ' > ' . $file->getAbsolutePath(); System::execute($command); }