Beispiel #1
0
 /**
  * Converts value to DateTime object.
  * @param  string key
  * @param  string format
  * @return DateTime
  */
 public function asDateTime($key, $format = NULL)
 {
     $time = $this[$key];
     if ((int) $time === 0) {
         // '', NULL, FALSE, '0000-00-00', ...
         return NULL;
     }
     $dt = new DateTime53(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
     return $format === NULL ? $dt : $dt->format($format);
 }
Beispiel #2
0
 /**
  * Converts value to specified type and format.
  * @param  mixed  value
  * @param  int    type
  * @return mixed
  */
 protected function convert($value, $type)
 {
     if ($value === NULL || $value === FALSE) {
         return NULL;
     }
     switch ($type) {
         case dibi::TEXT:
             return (string) $value;
         case dibi::BINARY:
             return $this->getDriver()->unescape($value, $type);
         case dibi::INTEGER:
             return (int) $value;
         case dibi::FLOAT:
             return (double) $value;
         case dibi::DATE:
         case dibi::DATETIME:
             if ((int) $value === 0) {
                 // '', NULL, FALSE, '0000-00-00', ...
                 return NULL;
             } elseif ($this->dateFormat === '') {
                 // return DateTime object (default)
                 return new DateTime53(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
             } elseif ($this->dateFormat === 'U') {
                 // return timestamp
                 return is_numeric($value) ? (int) $value : strtotime($value);
             } elseif (is_numeric($value)) {
                 // formatted date
                 return date($this->dateFormat, $value);
             } else {
                 $value = new DateTime53($value);
                 return $value->format($this->dateFormat);
             }
         case dibi::BOOL:
             return (bool) $value && $value !== 'f' && $value !== 'F';
         default:
             return $value;
     }
 }
Beispiel #3
0
 private function completeDependencies($dp, $data)
 {
     if (is_object($data)) {
         $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkSerializationVersion'), get_class($data), ClassReflection::from($data)->getAnnotation('serializationVersion'));
     }
     // convert expire into relative amount of seconds
     if (isset($dp[Cache::EXPIRATION])) {
         $dp[Cache::EXPIRATION] = DateTime53::from($dp[Cache::EXPIRATION])->format('U') - time();
     }
     // convert FILES into CALLBACKS
     if (isset($dp[self::FILES])) {
         //clearstatcache();
         foreach (array_unique((array) $dp[self::FILES]) as $item) {
             $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkFile'), $item, @filemtime($item));
             // @ - stat may fail
         }
         unset($dp[self::FILES]);
     }
     // add namespaces to items
     if (isset($dp[self::ITEMS])) {
         $dp[self::ITEMS] = array_unique((array) $dp[self::ITEMS]);
         foreach ($dp[self::ITEMS] as $k => $item) {
             $dp[self::ITEMS][$k] = $this->generateKey($item);
         }
     }
     // convert CONSTS into CALLBACKS
     if (isset($dp[self::CONSTS])) {
         foreach (array_unique((array) $dp[self::CONSTS]) as $item) {
             $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkConst'), $item, constant($item));
         }
         unset($dp[self::CONSTS]);
     }
     if (!is_array($dp)) {
         $dp = array();
     }
     return $dp;
 }
Beispiel #4
0
 /**
  * Sends a cookie.
  * @param  string name of the cookie
  * @param  string value
  * @param  string|int|DateTime  expiration time, value 0 means "until the browser is closed"
  * @param  string
  * @param  string
  * @param  bool
  * @param  bool
  * @return HttpResponse  provides a fluent interface
  * @throws InvalidStateException  if HTTP headers have been sent
  */
 public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL, $httpOnly = NULL)
 {
     if (headers_sent($file, $line)) {
         throw new InvalidStateException("Cannot set cookie after HTTP headers have been sent" . ($file ? " (output started at {$file}:{$line})." : "."));
     }
     setcookie($name, $value, $time ? DateTime53::from($time)->format('U') : 0, $path === NULL ? $this->cookiePath : (string) $path, $domain === NULL ? $this->cookieDomain : (string) $domain, $secure === NULL ? $this->cookieSecure : (bool) $secure, $httpOnly === NULL ? $this->cookieHttpOnly : (bool) $httpOnly);
     if (ini_get('suhosin.cookie.encrypt')) {
         return $this;
     }
     $flatten = array();
     foreach (headers_list() as $header) {
         if (preg_match('#^Set-Cookie: .+?=#', $header, $m)) {
             $flatten[$m[0]] = $header;
             if (PHP_VERSION_ID < 50300) {
                 // multiple deleting due PHP bug #61605
                 header('Set-Cookie:');
             } else {
                 header_remove('Set-Cookie');
             }
         }
     }
     foreach (array_values($flatten) as $key => $header) {
         header($header, $key === 0);
     }
     return $this;
 }
Beispiel #5
0
 function __construct($val)
 {
     parent::__construct($val);
 }
Beispiel #6
0
    /**
     * Restricts the search by modified time.
     * @param  string  "[operator] [date]" example: >1978-01-23
     * @param  mixed
     * @return Finder  provides a fluent interface
     */
    public function date($operator, $date = NULL)
    {
        if (func_num_args() === 1) {
            // in $operator is predicate
            if (!preg_match('#^(?:([=<>!]=?|<>)\\s*)?(.+)$#i', $operator, $matches)) {
                throw new InvalidArgumentException('Invalid date predicate format.');
            }
            list(, $operator, $date) = $matches;
            $operator = $operator ? $operator : '=';
        }
        $date = DateTime53::from($date)->format('U');
        return $this->filter(create_function('$file', 'extract(NCFix::$vars[' . NCFix::uses(array('operator' => $operator, 'date' => $date)) . '], EXTR_REFS);
			return Finder::compare($file->getMTime(), $operator, $date);
		'));
    }
Beispiel #7
0
 /**
  * Enables log out after inactivity.
  * @param  string|int|DateTime Number of seconds or timestamp
  * @param  int Log out when the browser is closed | Clear the identity from persistent storage?
  * @return UserStorage Provides a fluent interface
  */
 public function setExpiration($time, $flags = 0)
 {
     $section = $this->getSessionSection(TRUE);
     if ($time) {
         $time = DateTime53::from($time)->format('U');
         $section->expireTime = $time;
         $section->expireDelta = $time - time();
     } else {
         unset($section->expireTime, $section->expireDelta);
     }
     $section->expireIdentity = (bool) ($flags & self::CLEAR_IDENTITY);
     $section->expireBrowser = (bool) ($flags & self::BROWSER_CLOSED);
     $section->browserCheck = TRUE;
     $section->setExpiration(0, 'browserCheck');
     $section->setExpiration($time, 'foo');
     // time check
     return $this;
 }
Beispiel #8
0
 /**
  * Sets the expiration of the section or specific variables.
  * @param  string|int|DateTime  time, value 0 means "until the browser is closed"
  * @param  mixed   optional list of variables / single variable to expire
  * @return SessionSection  provides a fluent interface
  */
 public function setExpiration($time, $variables = NULL)
 {
     $this->start();
     if (empty($time)) {
         $time = NULL;
         $whenBrowserIsClosed = TRUE;
     } else {
         $time = DateTime53::from($time)->format('U');
         $max = ini_get('session.gc_maxlifetime');
         if ($time - time() > $max + 3) {
             // bulgarian constant
             trigger_error("The expiration time is greater than the session expiration {$max} seconds", E_USER_NOTICE);
         }
         $whenBrowserIsClosed = FALSE;
     }
     if ($variables === NULL) {
         // to entire section
         $this->meta['']['T'] = $time;
         $this->meta['']['B'] = $whenBrowserIsClosed;
     } elseif (is_array($variables)) {
         // to variables
         foreach ($variables as $variable) {
             $this->meta[$variable]['T'] = $time;
             $this->meta[$variable]['B'] = $whenBrowserIsClosed;
         }
     } else {
         // to variable
         $this->meta[$variables]['T'] = $time;
         $this->meta[$variables]['B'] = $whenBrowserIsClosed;
     }
     return $this;
 }
Beispiel #9
0
 /**
  * Date/time formatting.
  * @param  string|int|DateTime
  * @param  string
  * @return string
  */
 public static function date($time, $format = NULL)
 {
     if ($time == NULL) {
         // intentionally ==
         return NULL;
     }
     if (!isset($format)) {
         $format = self::$dateFormat;
     }
     $time = DateTime53::from($time);
     return Strings::contains($format, '%') ? strftime($format, $time->format('U')) : $time->format($format);
     // formats using date()
 }
Beispiel #10
0
 /**
  * Sets the amount of time allowed between requests before the session will be terminated.
  * @param  string|int|DateTime  time, value 0 means "until the browser is closed"
  * @return Session  provides a fluent interface
  */
 public function setExpiration($time)
 {
     if (empty($time)) {
         return $this->setOptions(array('gc_maxlifetime' => self::DEFAULT_FILE_LIFETIME, 'cookie_lifetime' => 0));
     } else {
         $time = DateTime53::from($time)->format('U') - time();
         return $this->setOptions(array('gc_maxlifetime' => $time, 'cookie_lifetime' => $time));
     }
 }