/** * Sends email. * @param NMail * @return void */ public function send(NMail $mail) { $tmp = clone $mail; $tmp->setHeader('Subject', NULL); $tmp->setHeader('To', NULL); $parts = explode(NMail::EOL . NMail::EOL, $tmp->generateMessage(), 2); NDebugger::tryError(); $args = array( str_replace(NMail::EOL, PHP_EOL, $mail->getEncodedHeader('To')), str_replace(NMail::EOL, PHP_EOL, $mail->getEncodedHeader('Subject')), str_replace(NMail::EOL, PHP_EOL, $parts[1]), str_replace(NMail::EOL, PHP_EOL, $parts[0]), ); if ($this->commandArgs) { $args[] = (string) $this->commandArgs; } $res = call_user_func_array('mail', $args); if (NDebugger::catchError($e)) { throw new InvalidStateException('mail(): ' . $e->getMessage(), 0, $e); } elseif (!$res) { throw new InvalidStateException('Unable to send email.'); } }
/** * Reads configuration from INI file. * @param string file name * @return array * @throws InvalidStateException */ public function load($file) { NDebugger::tryError(); $ini = parse_ini_file($file, TRUE); if (NDebugger::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, "\xFF", $key); foreach (explode(self::KEY_SEPARATOR, $key) as $part) { $part = str_replace("\xFF", 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[NConfigHelpers::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 = NConfigHelpers::merge($secData, $cursor); } $cursor = $secData; } return $data; }
public function __construct($host = 'localhost', $port = 11211, $prefix = '', ICacheJournal $journal = NULL, $timeout = 1) { if (!self::isAvailable()) { throw new NotSupportedException("PHP extension 'memcache' is not loaded."); } $this->prefix = $prefix; $this->journal = $journal; $this->memcache = new Memcache; NDebugger::tryError(); $this->memcache->connect($host, $port, $timeout); if (NDebugger::catchError($e)) { throw new InvalidStateException('Memcache::connect(): ' . $e->getMessage(), 0, $e); } }
/** * Returns the JSON representation of a value. * @param mixed * @return string */ public static function encode($value) { NDebugger::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 (NDebugger::catchError($e)) { // needed to receive 'recursion detected' error throw new NJsonException($e->getMessage()); } return $json; }
/** @internal */ public static function catchPregError($pattern) { if (NDebugger::catchError($e)) { // compile error throw new NRegexpException($e->getMessage() . " in pattern: $pattern"); } elseif (preg_last_error()) { // run-time error static $messages = array( PREG_INTERNAL_ERROR => 'Internal error', PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted', PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted', PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data', 5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', // PREG_BAD_UTF8_OFFSET_ERROR ); $code = preg_last_error(); throw new NRegexpException((isset($messages[$code]) ? $messages[$code] : 'Unknown error') . " (pattern: $pattern)", $code); } }
/** * Starts and initializes session data. * @throws InvalidStateException * @return void */ public function start() { if (self::$started) { return; } $this->configure($this->options); NDebugger::tryError(); session_start(); if (NDebugger::catchError($e)) { @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) { $nfTime = $time; $this->regenerationNeeded = TRUE; } // browser closing detection $browserKey = $this->request->getCookie('nette-browser'); if (!$browserKey) { $browserKey = NStrings::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']) // whenBrowserIsClosed || Time || ($variable !== '' && is_object($nf['DATA'][$section][$variable]) && (isset($value['V']) ? $value['V'] : NULL) // Version !== NClassReflection::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->regenerationNeeded) { session_regenerate_id(TRUE); $this->regenerationNeeded = FALSE; } register_shutdown_function(array($this, 'clean')); }