/** * 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; }
/** * 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) { NDebugger::tryError(); if (is_object($replacement) || is_array($replacement)) { if ($replacement instanceof NCallback) { $replacement = $replacement->getNative(); } if (!is_callable($replacement, FALSE, $textual)) { NDebugger::catchError($foo); throw new InvalidStateException("Callback '$textual' is not callable."); } $res = preg_replace_callback($pattern, $replacement, $subject, $limit); if (NDebugger::catchError($e)) { // compile error $trace = $e->getTrace(); if (isset($trace[2]['class']) && $trace[2]['class'] === __CLASS__) { throw new NRegexpException($e->getMessage() . " in pattern: $pattern"); } } } 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; }
/** * 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')); }
/** * Returns array entries that match the pattern. * @param array * @param string * @param int * @return array */ public static function grep(array $arr, $pattern, $flags = 0) { NDebugger::tryError(); $res = preg_grep($pattern, $arr, $flags); NStrings::catchPregError($pattern); return $res; }