示例#1
0
 public function __construct($filename, $fields)
 {
     $this->fields = $fields;
     $f = new File($filename);
     $f->setFlags(File::DROP_NEW_LINE | File::READ_AHEAD | File::SKIP_EMPTY);
     parent::__construct($f);
 }
示例#2
0
 /**
  * Validate the given token with the one in the token-file
  *
  * @param   string  $value      The token to validate
  * @param   null    $context    The form context (ignored)
  *
  * @return  bool
  */
 public function isValid($value, $context = null)
 {
     try {
         $file = new File($this->tokenPath);
         $expectedToken = trim($file->fgets());
     } catch (Exception $e) {
         $msg = $e->getMessage();
         $this->_error('TOKEN_FILE_ERROR', substr($msg, strpos($msg, ']: ') + 3));
         return false;
     }
     if (empty($expectedToken)) {
         $this->_error('TOKEN_FILE_EMPTY');
         return false;
     } elseif ($value !== $expectedToken) {
         $this->_error('TOKEN_INVALID');
         return false;
     }
     return true;
 }
示例#3
0
 /**
  * Creates the assigned key to the resource
  *
  * @param ResourceConfigForm $form
  *
  * @return bool
  */
 public static function beforeAdd(ResourceConfigForm $form)
 {
     $configDir = Icinga::app()->getConfigDir();
     $user = $form->getElement('user')->getValue();
     $filePath = $configDir . '/ssh/' . $user;
     if (!file_exists($filePath)) {
         $file = File::create($filePath, 0600);
     } else {
         $form->error(sprintf($form->translate('The private key for the user "%s" is already exists.'), $user));
         return false;
     }
     $file->fwrite($form->getElement('private_key')->getValue());
     $form->getElement('private_key')->setValue($configDir . '/ssh/' . $user);
     return true;
 }
示例#4
0
 /**
  * Write the preferences
  *
  * @throws  NotWritableError    In case the INI file cannot be written
  */
 public function write()
 {
     if ($this->writer === null) {
         if (!file_exists($this->preferencesFile)) {
             if (!is_writable($this->getStoreConfig()->location)) {
                 throw new NotWritableError(sprintf('Path to the preferences INI files %s is not writable', $this->getStoreConfig()->location));
             }
             File::create($this->preferencesFile);
         }
         if (!is_writable($this->preferencesFile)) {
             throw new NotWritableError('Preferences INI file ' . $this->preferencesFile . ' for user ' . $this->getUser()->getUsername() . ' is not writable');
         }
         $this->writer = new PreservingIniWriter(array('config' => new Zend_Config($this->preferences), 'filename' => $this->preferencesFile));
     }
     $this->writer->write();
 }
示例#5
0
 /**
  * Save configuration to the given INI file
  *
  * @param   string|null     $filePath   The path to the INI file or null in case this config's path should be used
  * @param   int             $fileMode   The file mode to store the file with
  *
  * @throws  LogicException              In case this config has no path and none is passed in either
  * @throws  NotWritableError            In case the INI file cannot be written
  *
  * @todo    create basepath and throw NotWritableError in case its not possible
  */
 public function saveIni($filePath = null, $fileMode = 0660)
 {
     if ($filePath === null && $this->configFile) {
         $filePath = $this->configFile;
     } elseif ($filePath === null) {
         throw new LogicException('You need to pass $filePath or set a path using Config::setConfigFile()');
     }
     if (!file_exists($filePath)) {
         File::create($filePath, $fileMode);
     }
     $this->getIniWriter($filePath, $fileMode)->write();
 }
示例#6
0
 /**
  * Write the command to the local Icinga command file
  *
  * @param   IcingaCommand   $command
  * @param   int|null        $now
  *
  * @throws  ConfigurationError
  * @throws  CommandTransportException
  */
 public function send(IcingaCommand $command, $now = null)
 {
     if (!isset($this->path)) {
         throw new ConfigurationError('Can\'t send external Icinga Command. Path to the local command file is missing');
     }
     $commandString = $this->renderer->render($command, $now);
     Logger::debug('Sending external Icinga command "%s" to the local command file "%s"', $commandString, $this->path);
     try {
         $file = new File($this->path, $this->openMode);
         $file->fwrite($commandString . "\n");
     } catch (Exception $e) {
         $message = $e->getMessage();
         if ($e instanceof RuntimeException && ($pos = strrpos($message, ':')) !== false) {
             // Assume RuntimeException thrown by SplFileObject in the format: __METHOD__ . "({$filename}): Message"
             $message = substr($message, $pos + 1);
         }
         throw new CommandTransportException('Can\'t send external Icinga command to the local command file "%s": %s', $this->path, $message);
     }
 }
示例#7
0
 /**
  * Write a message to the log
  *
  * @param string $message
  */
 protected function write($message)
 {
     $file = new File($this->file, 'a');
     $file->fwrite($message);
     $file->fflush();
 }
示例#8
0
 /**
  * @expectedException \Icinga\Exception\NotWritableError
  */
 public function testWhetherTruncatingNonWritableFilesThrowsAnException()
 {
     $file = new File('/dev/null');
     $file->ftruncate(0);
 }
 /**
  * Recursively scan the given directory for translatable source files
  *
  * @param   string      $directory      The directory where to search for sources
  * @param   File        $file           The file where to write the results
  * @param   array       $blacklist      A list of directories to omit
  *
  * @throws  Exception                   In case the given directory is not readable
  */
 private function getSourceFileNames($directory, File $file)
 {
     $directoryHandle = opendir($directory);
     if (!$directoryHandle) {
         throw new IcingaException('Unable to read files from %s', $directory);
     }
     $subdirs = array();
     while (($filename = readdir($directoryHandle)) !== false) {
         $filepath = $directory . DIRECTORY_SEPARATOR . $filename;
         if (preg_match('@^[^\\.].+\\.(' . implode('|', $this->sourceExtensions) . ')$@', $filename)) {
             $file->fwrite($filepath . PHP_EOL);
         } elseif (is_dir($filepath) && !preg_match('@^(\\.|\\.\\.)$@', $filename)) {
             $subdirs[] = $filepath;
         }
     }
     closedir($directoryHandle);
     foreach ($subdirs as $subdir) {
         $this->getSourceFileNames($subdir, $file);
     }
 }