示例#1
0
 /**
  * Construct the object by array of config properties. Config keys are set
  * in config/cache.php and this array will contain only block for the
  * requested cache driver. Yes, you can also build this manually, but that
  * is not recommended.
  * 
  * @param array $config
  * @throws \Koldy\Exception
  */
 public function __construct(array $config)
 {
     // because if cache is not enabled, then lets not do anything else
     if (!isset($config['path']) || $config['path'] === null) {
         $this->path = Application::getStoragePath('cache/');
     } else {
         $this->path = $config['path'];
     }
     if (substr($this->path, -1) != '/') {
         $this->path .= '/';
     }
     if (!is_dir($this->path) && !Directory::mkdir($this->path, 0777)) {
         throw new Exception("Cache directory '{$this->path}' doesn't exists and can't be created");
     }
     parent::__construct($config);
 }
示例#2
0
 /**
  * (non-PHPdoc)
  * @see SessionHandlerInterface::open()
  */
 public function open($save_path, $sessionid)
 {
     // we'll ignore $save_path because we have our own path from config
     if (isset($this->config['session_save_path'])) {
         $this->savePath = $this->config['session_save_path'];
     } else {
         $this->savePath = Application::getStoragePath() . 'session';
     }
     if (!is_dir($this->savePath)) {
         if (!Directory::mkdir($this->savePath, 0777)) {
             throw new Exception('Can not create directory for session storage');
         }
     }
     if (substr($this->savePath, -1) != DS) {
         $this->savePath .= DS;
     }
     return true;
 }
示例#3
0
 /**
  * Get (download) file from remote URL and save it on local path
  * 
  * @param string $remoteUrl
  * @param string $localPath
  * @return \Koldy\Http\Response
  * @example Request::getFile('http://remote.com/path/to.gif', '/var/www/local/my.gif');
  */
 public static function getFile($remoteUrl, $localPath)
 {
     Directory::mkdir(dirname($localPath), 0755);
     $fp = @fopen($localPath, 'wb');
     if (!$fp) {
         throw new Exception("Can not open file for writing: {$localPath}");
     }
     $ch = curl_init($remoteUrl);
     curl_setopt($ch, CURLOPT_FILE, $fp);
     curl_setopt($ch, CURLOPT_HEADER, false);
     curl_setopt($ch, CURLOPT_TIMEOUT, 21600);
     // 6 hours should be enough, orrr??
     curl_exec($ch);
     $info = curl_getinfo($ch);
     curl_close($ch);
     fclose($fp);
     return new Response($info, null);
 }
示例#4
0
 /**
  * Actually log message to file
  * 
  * @param string $level
  * @param string $message
  * @throws \Koldy\Exception
  */
 protected function logMessage($level, $message)
 {
     // If script is running for very long time (e.g. CRON), then date might change if time passes midnight.
     // In that case, log will continue to write messages to new file.
     $fpFile = $this->getFileName();
     if ($fpFile !== $this->fpFile) {
         //date has changed? or we need to init?
         if ($this->fp) {
             // close pointer to old file
             @fclose($this->fp);
         }
         if ($this->config['path'] === null) {
             $path = Application::getStoragePath('log' . DS . $fpFile);
         } else {
             $path = str_replace(DS . DS, DS, $this->config['path'] . DS . $fpFile);
         }
         $this->fpFile = $fpFile;
         if (!($this->fp = @fopen($path, 'a'))) {
             // file failed to open, maybe directory doesn't exists?
             $dir = dirname($path);
             if (!is_dir($dir)) {
                 if (!Directory::mkdir($dir, 0755)) {
                     throw new Exception('Can not create directory on location=' . $dir);
                 }
             }
             if (!($this->fp = @fopen($path, 'a'))) {
                 // now the directory should exists, so try to open file again
                 throw new Exception('Can not write to log file on path=' . $path);
             }
         }
     }
     if (!$this->fp || $this->fp === null) {
         throw new Exception('Can not write to log file');
     }
     if (is_object($message)) {
         $message = $message->__toString();
     }
     $logMessage = $this->getMessage($level, $message);
     if ($logMessage !== false) {
         if (in_array($level, $this->config['log'])) {
             if (!@fwrite($this->fp, $logMessage)) {
                 // actually write it in file
                 throw new Exception('Unable to write to log file');
             } else {
                 // so, writing was ok, but what if showdown was already called?
                 // then we'll close the file, but additional email alerts won't
                 // be sent any more - sorry
                 if ($this->closed) {
                     @fclose($this->fp);
                     $this->fp = null;
                     $this->fpFile = null;
                 }
             }
         }
         $this->appendMessage($logMessage);
         $this->detectEmailAlert($level);
     }
 }