/** * Returns encoded message. * @return string */ public function generateMessage() { $output = ''; $boundary = '--------' . Strings::random(); foreach ($this->headers as $name => $value) { $output .= $name . ': ' . $this->getEncodedHeader($name); if ($this->parts && $name === 'Content-Type') { $output .= ';' . self::EOL . "\tboundary=\"{$boundary}\""; } $output .= self::EOL; } $output .= self::EOL; $body = (string) $this->body; if ($body !== '') { switch ($this->getEncoding()) { case self::ENCODING_QUOTED_PRINTABLE: $output .= function_exists('quoted_printable_encode') ? quoted_printable_encode($body) : self::encodeQuotedPrintable($body); break; case self::ENCODING_BASE64: $output .= rtrim(chunk_split(base64_encode($body), self::LINE_LENGTH, self::EOL)); break; case self::ENCODING_7BIT: $body = preg_replace('#[\\x80-\\xFF]+#', '', $body); // break intentionally omitted // break intentionally omitted case self::ENCODING_8BIT: $body = str_replace(array("", "\r"), '', $body); $body = str_replace("\n", self::EOL, $body); $output .= $body; break; default: throw new InvalidStateException('Unknown encoding.'); } } if ($this->parts) { if (substr($output, -strlen(self::EOL)) !== self::EOL) { $output .= self::EOL; } foreach ($this->parts as $part) { $output .= '--' . $boundary . self::EOL . $part->generateMessage() . self::EOL; } $output .= '--' . $boundary . '--'; } return $output; }
/** * New node is found. * @return bool */ public function nodeOpened(MacroNode $node) { $this->used = TRUE; $node->isEmpty = FALSE; $node->openingCode = PhpWriter::using($node)->write('<?php if (CacheMacro::createCache($netteCacheStorage, %var, $_g->caches, %node.array?)) { ?>', Strings::random()); }
/** * Cross-Site Request Forgery (CSRF) form protection. * @param string * @param int * @return void */ public function addProtection($message = NULL, $timeout = NULL) { $session = $this->getSession()->getSection('Nette.Forms.Form/CSRF'); $key = "key{$timeout}"; if (isset($session->{$key})) { $token = $session->{$key}; } else { $session->{$key} = $token = Strings::random(); } $session->setExpiration($timeout, $key); $this[self::PROTECTOR_ID] = new HiddenField($token); $this[self::PROTECTOR_ID]->addRule(self::PROTECTION, $message, $token); }
/** * @return void */ public function __destruct() { if (self::$fixIE && isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE ') !== FALSE && in_array($this->code, array(400, 403, 404, 405, 406, 408, 409, 410, 500, 501, 505), TRUE) && $this->getHeader('Content-Type', 'text/html') === 'text/html') { echo Strings::random(2000.0, " \t\r\n"); // sends invisible garbage for IE self::$fixIE = FALSE; } }
/** * Returns session namespace provided to pass temporary data between redirects. * @return SessionSection */ public function getFlashSession() { if (empty($this->params[self::FLASH_KEY])) { $this->params[self::FLASH_KEY] = Strings::random(4); } return $this->getSession('Nette.Application.Flash/' . $this->params[self::FLASH_KEY]); }
/** * Generates code for macro <tag n:attr> to the output. * @param string * @return void */ public function writeAttrsMacro($code, HtmlNode $htmlNode) { $attrs = $htmlNode->macroAttrs; $left = $right = array(); $attrCode = ''; foreach ($this->macros as $name => $foo) { $attrName = MacroNode::PREFIX_INNER . "-{$name}"; if (isset($attrs[$attrName])) { if ($htmlNode->closing) { $left[] = array("/{$name}", '', MacroNode::PREFIX_INNER); } else { array_unshift($right, array($name, $attrs[$attrName], MacroNode::PREFIX_INNER)); } unset($attrs[$attrName]); } } foreach (array_reverse($this->macros) as $name => $foo) { $attrName = MacroNode::PREFIX_TAG . "-{$name}"; if (isset($attrs[$attrName])) { $left[] = array($name, $attrs[$attrName], MacroNode::PREFIX_TAG); array_unshift($right, array("/{$name}", '', MacroNode::PREFIX_TAG)); unset($attrs[$attrName]); } } foreach ($this->macros as $name => $foo) { if (isset($attrs[$name])) { if ($htmlNode->closing) { $right[] = array("/{$name}", '', NULL); } else { array_unshift($left, array($name, $attrs[$name], NULL)); } unset($attrs[$name]); } } if ($attrs) { throw new CompileException("Unknown macro-attribute " . Parser::N_PREFIX . implode(' and ' . Parser::N_PREFIX, array_keys($attrs))); } if (!$htmlNode->closing) { $htmlNode->attrCode =& $this->attrCodes[$uniq = ' n:' . Strings::random()]; $code = substr_replace($code, $uniq, ($tmp = strrpos($code, '/>')) ? $tmp : strrpos($code, '>'), 0); } foreach ($left as $item) { $node = $this->writeMacro($item[0], $item[1], NULL, NULL, $htmlNode, $item[2]); if ($node->closing || $node->isEmpty) { $htmlNode->attrCode .= $node->attrCode; if ($node->isEmpty) { unset($htmlNode->macroAttrs[$node->name]); } } } $this->output .= $code; foreach ($right as $item) { $node = $this->writeMacro($item[0], $item[1], NULL, NULL, $htmlNode); if ($node->closing) { $htmlNode->attrCode .= $node->attrCode; } } if ($right && substr($this->output, -2) === '?>') { $this->output .= "\n"; } }
/** @return string */ private function getRandomId() { return '<' . Strings::random() . '@' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost')) . '>'; }
/** * 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')); }