Esempio n. 1
0
 /**
  * Sends e-mail.
  * @param  Mail
  * @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);
     Debug::tryError();
     $res = mail(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 (Debug::catchError($msg)) {
         throw new InvalidStateException($msg);
     } elseif (!$res) {
         throw new InvalidStateException('Unable to send email.');
     }
 }
Esempio n. 2
0
 /**
  * Returns the JSON representation of a value.
  * @param  mixed
  * @return string
  */
 public static function encode($value)
 {
     Debug::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 (Debug::catchError($message)) {
         // needed to receive 'recursion detected' error
         throw new JsonException($message);
     }
     return $json;
 }
Esempio n. 3
0
 /**
  * Starts and initializes session data.
  * @throws InvalidStateException
  * @return void
  */
 public function start()
 {
     if (self::$started) {
         throw new InvalidStateException('Session has already been started.');
     } elseif (self::$started === NULL && defined('SID')) {
         throw new InvalidStateException('A session had already been started by session.auto-start or session_start().');
     }
     // start session
     try {
         $this->configure($this->options);
     } catch (NotSupportedException $e) {
         // ignore?
     }
     Debug::tryError();
     session_start();
     if (Debug::catchError($msg)) {
         @session_write_close();
         // this is needed
         throw new InvalidStateException($msg);
     }
     self::$started = TRUE;
     if ($this->regenerationNeeded) {
         session_regenerate_id(TRUE);
         $this->regenerationNeeded = FALSE;
     }
     /* structure:
     			__NF: Counter, BrowserKey, Data, Meta
     				DATA: namespace->variable = data
     				META: namespace->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']++;
     }
     // browser closing detection
     $browserKey = $this->getHttpRequest()->getCookie('nette-browser');
     if (!$browserKey) {
         $browserKey = (string) lcg_value();
     }
     $browserClosed = !isset($nf['B']) || $nf['B'] !== $browserKey;
     $nf['B'] = $browserKey;
     // resend cookie
     $this->sendCookie();
     // process meta metadata
     if (isset($nf['META'])) {
         $now = time();
         // expire namespace variables
         foreach ($nf['META'] as $namespace => $metadata) {
             if (is_array($metadata)) {
                 foreach ($metadata as $variable => $value) {
                     if (!empty($value['B']) && $browserClosed || !empty($value['T']) && $now > $value['T'] || $variable !== '' && is_object($nf['DATA'][$namespace][$variable]) && (isset($value['V']) ? $value['V'] : NULL) !== ClassReflection::from($nf['DATA'][$namespace][$variable])->getAnnotation('serializationVersion')) {
                         if ($variable === '') {
                             // expire whole namespace
                             unset($nf['META'][$namespace], $nf['DATA'][$namespace]);
                             continue 2;
                         }
                         unset($nf['META'][$namespace][$variable], $nf['DATA'][$namespace][$variable]);
                     }
                 }
             }
         }
     }
     register_shutdown_function(array($this, 'clean'));
 }
Esempio n. 4
0
 /**
  * Return array entries that match the pattern.
  * @param  array
  * @param  string
  * @param  int
  * @return array
  */
 public static function grep(array $arr, $pattern, $flags = 0)
 {
     Debug::tryError();
     $res = preg_grep($pattern, $arr, $flags);
     String::catchPregError($pattern);
     return $res;
 }
Esempio n. 5
0
 /**
  * Perform a regular expression search and replace.
  * @param  string
  * @param  string|array
  * @param  string|callback
  * @param  int
  * @return string
  */
 public static function replace($subject, $pattern, $replacement = NULL, $limit = -1)
 {
     Debug::tryError();
     if (is_object($replacement) || is_array($replacement)) {
         if ($replacement instanceof Callback) {
             $replacement = $replacement->getNative();
         }
         if (!is_callable($replacement, FALSE, $textual)) {
             Debug::catchError($foo);
             throw new InvalidStateException("Callback '{$textual}' is not callable.");
         }
         $res = preg_replace_callback($pattern, $replacement, $subject, $limit);
     } elseif (is_array($pattern)) {
         $res = preg_replace(array_keys($pattern), array_values($pattern), $subject, $limit);
     } else {
         $res = preg_replace($pattern, $replacement, $subject, $limit);
     }
     self::catchPregError($pattern);
     return $res;
 }
Esempio n. 6
0
 /**
  * Reads configuration from INI file.
  * @param  string  file name
  * @param  string  section to load
  * @return array
  * @throws InvalidStateException
  */
 public static function load($file, $section = NULL)
 {
     if (!is_file($file) || !is_readable($file)) {
         throw new FileNotFoundException("File '{$file}' is missing or is not readable.");
     }
     Debug::tryError();
     $ini = parse_ini_file($file, TRUE);
     if (Debug::catchError($msg)) {
         throw new Exception($msg);
     }
     $separator = trim(self::$sectionSeparator);
     $data = array();
     foreach ($ini as $secName => $secData) {
         // is section?
         if (is_array($secData)) {
             if (substr($secName, -1) === self::$rawSection) {
                 $secName = substr($secName, 0, -1);
             } elseif (self::$keySeparator) {
                 // process key separators (key1> key2> key3)
                 $tmp = array();
                 foreach ($secData as $key => $val) {
                     $cursor =& $tmp;
                     foreach (explode(self::$keySeparator, $key) as $part) {
                         if (!isset($cursor[$part]) || is_array($cursor[$part])) {
                             $cursor =& $cursor[$part];
                         } else {
                             throw new InvalidStateException("Invalid key '{$key}' in section [{$secName}] in '{$file}'.");
                         }
                     }
                     $cursor = $val;
                 }
                 $secData = $tmp;
             }
             // process extends sections like [staging < production] (with special support for separator ':')
             $parts = $separator ? explode($separator, strtr($secName, ':', $separator)) : array($secName);
             if (count($parts) > 1) {
                 $parent = trim($parts[1]);
                 $cursor =& $data;
                 foreach (self::$keySeparator ? explode(self::$keySeparator, $parent) : array($parent) as $part) {
                     if (isset($cursor[$part]) && is_array($cursor[$part])) {
                         $cursor =& $cursor[$part];
                     } else {
                         throw new InvalidStateException("Missing parent section [{$parent}] in '{$file}'.");
                     }
                 }
                 $secData = ArrayTools::mergeTree($secData, $cursor);
             }
             $secName = trim($parts[0]);
             if ($secName === '') {
                 throw new InvalidStateException("Invalid empty section name in '{$file}'.");
             }
         }
         if (self::$keySeparator) {
             $cursor =& $data;
             foreach (explode(self::$keySeparator, $secName) as $part) {
                 if (!isset($cursor[$part]) || is_array($cursor[$part])) {
                     $cursor =& $cursor[$part];
                 } else {
                     throw new InvalidStateException("Invalid section [{$secName}] in '{$file}'.");
                 }
             }
         } else {
             $cursor =& $data[$secName];
         }
         if (is_array($secData) && is_array($cursor)) {
             $secData = ArrayTools::mergeTree($secData, $cursor);
         }
         $cursor = $secData;
     }
     if ($section === NULL) {
         return $data;
     } elseif (!isset($data[$section]) || !is_array($data[$section])) {
         throw new InvalidStateException("There is not section [{$section}] in '{$file}'.");
     } else {
         return $data[$section];
     }
 }