/** * @param integer $size */ public static function setMaxSize($size) { self::$_maxSize = intval($size); }
protected function _getUrlTitle($url) { $requestUrl = $url; $requestUrl = preg_replace('/#.*$/', '', $requestUrl); if (preg_match_all('/[^A-Za-z0-9._~:\\/?#\\[\\]@!$&\'()*+,;=%-]/', $requestUrl, $matches)) { foreach ($matches[0] as $match) { $requestUrl = str_replace($match[0], '%' . strtoupper(dechex(ord($match[0]))), $requestUrl); } } $requestUrl = preg_replace('/%(?![a-fA-F0-9]{2})/', '%25', $requestUrl); // ensure the URL is valid and isn't trying to load something locally $parts = @parse_url($requestUrl); if (!$parts || empty($parts['scheme']) || !preg_match('/^https?$/i', $parts['scheme']) || empty($parts['host']) || preg_match('#^(127\\.|192\\.168\\.|10\\.|172\\.(1[6789]|2|3[01])|169\\.254\\.|0\\.0\\.0\\.0)#', $parts['host']) || preg_match('#^localhost(\\.localdomain)?$#i', $parts['host'])) { return false; } $urlHash = md5($url); XenForo_HttpStream::register(); XenForo_HttpStream::setMaxSize(50 * 1024); // 50KB $streamUri = 'xf-http://' . $urlHash . '-' . uniqid(); $this->_streamFile = XenForo_HttpStream::getTempFile($streamUri); try { $response = XenForo_Helper_Http::getClient($requestUrl, array('output_stream' => $streamUri, 'timeout' => 3, 'httpversion' => Zend_Http_Client::HTTP_0))->setHeaders('Accept-encoding', 'identity')->setHeaders('Accept', 'text/html,*/*;q=0.8')->request('GET'); if (!$response->isSuccessful()) { return false; } $headers = $response->getHeaders(); if (!$headers) { return false; } $charset = false; if (isset($headers['Content-type'])) { $parts = explode(';', is_array($headers['Content-type']) ? end($headers['Content-type']) : $headers['Content-type'], 2); $type = trim($parts[0]); if ($type != 'text/html') { return false; } if (isset($parts[1]) && preg_match('/charset=([-a-z0-9_]+)/i', trim($parts[1]), $match)) { $charset = $match[1]; } } try { $body = $response->getBody(); } catch (Exception $e) { // some servers may ignore our request for no gzip and this can fail $body = ''; } $title = ''; if (preg_match('#<meta[^>]+property="(og:|twitter:)title"[^>]*content="([^">]+)"#siU', $body, $match)) { $title = isset($match[2]) ? $match[2] : ''; } if (!$title && preg_match('#<title[^>]*>(.*)</title>#siU', $body, $match)) { $title = $match[1]; } if (!$title) { return false; } if (!$charset) { preg_match('/charset=([^;"\\s]+|"[^;"]+")/i', $body, $contentTypeMatch); if (isset($contentTypeMatch[1])) { $charset = trim($contentTypeMatch[1], " \t\n\r\v\""); } if (!$charset) { $charset = 'windows-1252'; } } $title = XenForo_Input::cleanString($this->_toUtf8($title, $charset, true)); if (defined('ENT_HTML5')) { $title = html_entity_decode($title, ENT_QUOTES | ENT_HTML5, 'UTF-8'); } else { $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8'); } $title = utf8_unhtml($title); $title = str_replace("\n", ' ', trim($title)); if (!strlen($title)) { return false; } $formatter = $this->_getBbCodeFilterer(); $parser = $this->_getBbCodeParser(); $parser->render($title); if ($formatter->getSmilieTally() || $formatter->getTotalTagTally()) { $title = "[PLAIN]{$title}[/PLAIN]"; } return $title; } catch (Zend_Http_Client_Exception $e) { } catch (Zend_Uri_Exception $e) { } catch (Exception $e) { XenForo_Error::logException($e, false, "Error linking URL '{$url}': "); } return false; }