示例#1
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;
 }
示例#2
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);
 }
示例#3
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);
     }
 }
示例#4
0
 /**
  * (non-PHPdoc)
  * @see \Koldy\Cache\Driver\AbstractDriver::deleteOld()
  */
 public function deleteOld($olderThenSeconds = null)
 {
     if ($olderThenSeconds === null) {
         $olderThenSeconds = $this->defaultDuration;
     }
     clearstatcache();
     /**
      * This is probably not good since lifetime is written in file
      * But going into every file and read might be even worse idea
      * XXX: Think about this
      */
     foreach (Directory::read($this->path) as $fullPath => $fileName) {
         $timeCreated = @filemtime($fullPath);
         if ($timeCreated !== false) {
             // successfully red the file modification time
             if (time() - $olderThenSeconds > $timeCreated) {
                 // it is old enough to be removed
                 if (!@unlink($fullPath)) {
                     Log::warning("Can not delete cached file on path {$fullPath}");
                 }
             }
         }
     }
 }