/** * Reads content from system crontab of the current user. * @throws AccessDeniedException if access to crontab is denied. * @return string */ public function read() { $result = $this->shell->execute($this->getCronCommand() . ' -l'); if ($this->shell->isFailed()) { throw new AccessDeniedException($result, $this->shell->getErrorCode()); } return $this->shell->getOutput(); }
/** * Writes the given content to the system crontab of the current user. * * @param array $lines * @throws AccessDeniedException if access to crontab is denied. * @throws WriteException if the temp path is not writable. */ public function write(array $lines) { // Reindex content array to ensure numeric indexes. $lines = array_values($lines); if (($lineCount = count($lines)) > 0) { // Add a new line to last line to prevent the error: // "new crontab file is missing newline before EOF, can't install.". $lines[$lineCount - 1] = rtrim($lines[$lineCount - 1]) . PHP_EOL; $tmpFile = $this->config['tmpPath'] . DIRECTORY_SEPARATOR . uniqid() . '.cron.import'; if (!is_writable($this->config['tmpPath'])) { throw new WriteException('Temp path "' . $this->config['tmpPath'] . '" is not writable.'); } // Write lines to file. file_put_contents($tmpFile, implode(PHP_EOL, (array) $lines)); $result = $this->shell->execute($this->getCronCommand() . ' ' . $tmpFile); unlink($tmpFile); } else { // If no lines should be added to crontab, clear crontab for current user. $result = $this->shell->execute($this->getCronCommand() . ' -r'); } if ($this->shell->isFailed()) { throw new AccessDeniedException($result, $this->shell->getErrorCode()); } }