Example #1
0
 public function addServer($host = 'localhost', $port = 11211, $timeout = 1)
 {
     Debugger::tryError();
     $this->memcache->addServer($host, $port, TRUE, 1, $timeout);
     if (Debugger::catchError($e)) {
         throw new InvalidStateException('Memcache::addServer(): ' . $e->getMessage(), 0, $e);
     }
 }
Example #2
0
 /**
  * Reads configuration from INI file.
  * @param  string  file name
  * @return array
  * @throws InvalidStateException
  */
 public function load($file)
 {
     Debugger::tryError();
     $ini = parse_ini_file($file, TRUE);
     if (Debugger::catchError($e)) {
         throw new InvalidStateException('parse_ini_file(): ' . $e->getMessage(), 0, $e);
     }
     $data = array();
     foreach ($ini as $secName => $secData) {
         if (is_array($secData)) {
             // is section?
             if (substr($secName, -1) === self::RAW_SECTION) {
                 $secName = substr($secName, 0, -1);
             } else {
                 // process key nesting separator (key1.key2.key3)
                 $tmp = array();
                 foreach ($secData as $key => $val) {
                     $cursor =& $tmp;
                     $key = str_replace(self::ESCAPED_KEY_SEPARATOR, "ÿ", $key);
                     foreach (explode(self::KEY_SEPARATOR, $key) as $part) {
                         $part = str_replace("ÿ", self::KEY_SEPARATOR, $part);
                         if (!isset($cursor[$part]) || is_array($cursor[$part])) {
                             $cursor =& $cursor[$part];
                         } else {
                             throw new InvalidStateException("Invalid key '{$key}' in section [{$secName}] in file '{$file}'.");
                         }
                     }
                     $cursor = $val;
                 }
                 $secData = $tmp;
             }
             $parts = explode(self::INHERITING_SEPARATOR, $secName);
             if (count($parts) > 1) {
                 $secName = trim($parts[0]);
                 $secData[ConfigHelpers::EXTENDS_KEY] = trim($parts[1]);
             }
         }
         $cursor =& $data;
         // nesting separator in section name
         foreach (explode(self::KEY_SEPARATOR, $secName) as $part) {
             if (!isset($cursor[$part]) || is_array($cursor[$part])) {
                 $cursor =& $cursor[$part];
             } else {
                 throw new InvalidStateException("Invalid section [{$secName}] in file '{$file}'.");
             }
         }
         if (is_array($secData) && is_array($cursor)) {
             $secData = ConfigHelpers::merge($secData, $cursor);
         }
         $cursor = $secData;
     }
     return $data;
 }
Example #3
0
 /**
  * Returns the JSON representation of a value.
  * @param  mixed
  * @return string
  */
 public static function encode($value)
 {
     Debugger::tryError();
     if (function_exists('ini_set')) {
         $old = ini_set('display_errors', 0);
         // needed to receive 'Invalid UTF-8 sequence' error
         $json = json_encode($value);
         ini_set('display_errors', $old);
     } else {
         $json = json_encode($value);
     }
     if (Debugger::catchError($e)) {
         // needed to receive 'recursion detected' error
         throw new JsonException($e->getMessage());
     }
     return $json;
 }
Example #4
0
 /**
  * Sends email.
  * @return void
  */
 public function send(Mail $mail)
 {
     $tmp = clone $mail;
     $tmp->setHeader('Subject', NULL);
     $tmp->setHeader('To', NULL);
     $parts = explode(Mail::EOL . Mail::EOL, $tmp->generateMessage(), 2);
     Debugger::tryError();
     $args = array(str_replace(Mail::EOL, PHP_EOL, $mail->getEncodedHeader('To')), str_replace(Mail::EOL, PHP_EOL, $mail->getEncodedHeader('Subject')), str_replace(Mail::EOL, PHP_EOL, $parts[1]), str_replace(Mail::EOL, PHP_EOL, $parts[0]));
     if ($this->commandArgs) {
         $args[] = (string) $this->commandArgs;
     }
     $res = call_user_func_array('mail', $args);
     if (Debugger::catchError($e)) {
         throw new InvalidStateException('mail(): ' . $e->getMessage(), 0, $e);
     } elseif (!$res) {
         throw new InvalidStateException('Unable to send email.');
     }
 }
Example #5
0
 /**
  * Returns array entries that match the pattern.
  * @param  array
  * @param  string
  * @param  int
  * @return array
  */
 public static function grep(array $arr, $pattern, $flags = 0)
 {
     Debugger::tryError();
     $res = preg_grep($pattern, $arr, $flags);
     if (Debugger::catchError($e) || preg_last_error()) {
         // compile error XOR run-time error
         throw new RegexpException($e ? $e->getMessage() : NULL, $e ? NULL : preg_last_error(), $pattern);
     }
     return $res;
 }
Example #6
0
 /**
  * Starts and initializes session data.
  * @throws InvalidStateException
  * @return void
  */
 public function start()
 {
     if (self::$started) {
         return;
     }
     $this->configure($this->options);
     Debugger::tryError();
     session_start();
     if (Debugger::catchError($e) && !session_id()) {
         @session_write_close();
         // this is needed
         throw new InvalidStateException('session_start(): ' . $e->getMessage(), 0, $e);
     }
     self::$started = TRUE;
     /* structure:
     			__NF: Counter, BrowserKey, Data, Meta, Time
     				DATA: section->variable = data
     				META: section->variable = Timestamp, Browser, Version
     		*/
     unset($_SESSION['__NT'], $_SESSION['__NS'], $_SESSION['__NM']);
     // old unused structures
     // initialize structures
     $nf =& $_SESSION['__NF'];
     if (empty($nf)) {
         // new session
         $nf = array('C' => 0);
     } else {
         $nf['C']++;
     }
     // session regenerate every 30 minutes
     $nfTime =& $nf['Time'];
     $time = time();
     if ($time - $nfTime > self::REGENERATE_INTERVAL) {
         $this->regenerated = $this->regenerated || isset($nfTime);
         $nfTime = $time;
     }
     // browser closing detection
     $browserKey = $this->request->getCookie('nette-browser');
     if (!$browserKey) {
         $browserKey = Strings::random();
     }
     $browserClosed = !isset($nf['B']) || $nf['B'] !== $browserKey;
     $nf['B'] = $browserKey;
     // resend cookie
     $this->sendCookie();
     // process meta metadata
     if (isset($nf['META'])) {
         $now = time();
         // expire section variables
         foreach ($nf['META'] as $section => $metadata) {
             if (is_array($metadata)) {
                 foreach ($metadata as $variable => $value) {
                     if (!empty($value['B']) && $browserClosed || !empty($value['T']) && $now > $value['T'] || isset($nf['DATA'][$section][$variable]) && is_object($nf['DATA'][$section][$variable]) && (isset($value['V']) ? $value['V'] : NULL) != ClassReflection::from($nf['DATA'][$section][$variable])->getAnnotation('serializationVersion')) {
                         if ($variable === '') {
                             // expire whole section
                             unset($nf['META'][$section], $nf['DATA'][$section]);
                             continue 2;
                         }
                         unset($nf['META'][$section][$variable], $nf['DATA'][$section][$variable]);
                     }
                 }
             }
         }
     }
     if ($this->regenerated) {
         $this->regenerated = FALSE;
         $this->regenerateId();
     }
     register_shutdown_function(array($this, 'clean'));
 }
Example #7
0
 /**
  * Perform a regular expression search and replace.
  * @param  string
  * @param  string|array
  * @param  string|callable
  * @param  int
  * @return string
  */
 public static function replace($subject, $pattern, $replacement = NULL, $limit = -1)
 {
     if (is_object($replacement) || is_array($replacement)) {
         if ($replacement instanceof Callback) {
             $replacement = $replacement->getNative();
         }
         if (!is_callable($replacement, FALSE, $textual)) {
             throw new InvalidStateException("Callback '{$textual}' is not callable.");
         }
         foreach ((array) $pattern as $tmp) {
             Debugger::tryError();
             preg_match($tmp, '');
             if (Debugger::catchError($e)) {
                 // compile error
                 throw new RegexpException($e->getMessage(), NULL, $tmp);
             }
         }
         $res = preg_replace_callback($pattern, $replacement, $subject, $limit);
         if ($res === NULL && preg_last_error()) {
             // run-time error
             throw new RegexpException(NULL, preg_last_error(), $pattern);
         }
         return $res;
     } elseif ($replacement === NULL && is_array($pattern)) {
         $replacement = array_values($pattern);
         $pattern = array_keys($pattern);
     }
     Debugger::tryError();
     $res = preg_replace($pattern, $replacement, $subject, $limit);
     if (Debugger::catchError($e) || preg_last_error()) {
         // compile error XOR run-time error
         throw new RegexpException($e ? $e->getMessage() : NULL, $e ? NULL : preg_last_error(), $pattern);
     }
     return $res;
 }