public function __construct($host = 'localhost', $port = 11211, $prefix = '', Nette\Context $context = NULL) { if (!self::isAvailable()) { throw new \NotSupportedException("PHP extension 'memcache' is not loaded."); } $this->prefix = $prefix; $this->context = $context; $this->memcache = new \Memcache(); Nette\Debug::tryError(); $this->memcache->connect($host, $port); if (Nette\Debug::catchError($msg)) { throw new \InvalidStateException($msg); } }
/** * 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); Nette\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 (Nette\Debug::catchError($msg)) { throw new \InvalidStateException($msg); } elseif (!$res) { throw new \InvalidStateException('Unable to send email.'); } }
public function __construct($host = 'localhost', $port = 11211, $prefix = '', ICacheJournal $journal = NULL) { if (!self::isAvailable()) { throw new \NotSupportedException("PHP extension 'memcache' is not loaded."); } $this->prefix = $prefix; $this->journal = $journal; $this->memcache = new \Memcache(); Nette\Debug::tryError(); $this->memcache->connect($host, $port); if (Nette\Debug::catchError($e)) { throw new \InvalidStateException($e->getMessage()); } }
/** * 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($e)) { // needed to receive 'recursion detected' error throw new JsonException($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) { 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); if (Debug::catchError($e)) { // compile error $trace = $e->getTrace(); if (isset($trace[2]['class']) && $trace[2]['class'] === __CLASS__) { throw new RegexpException($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; }
/** * 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; }
/** * 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."); } Nette\Debug::tryError(); $ini = parse_ini_file($file, TRUE); if (Nette\Debug::catchError($e)) { throw $e; } $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 = Nette\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 = Nette\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]; } }
/** * 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 (!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; }
/** * Starts and initializes session data. * @throws \InvalidStateException * @return void */ public function start() { if (self::$started) { return; } elseif (self::$started === NULL && defined('SID')) { throw new \InvalidStateException('A session had already been started by session.auto-start or session_start().'); } $this->configure($this->options); Nette\Debug::tryError(); session_start(); if (Nette\Debug::catchError($e)) { @session_write_close(); // this is needed throw new \InvalidStateException($e->getMessage()); } 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) !== Nette\Reflection\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')); }