From b9ec99e25b5972bb3d0702919519382ebc37dacc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 27 Jan 2015 13:08:02 +0100 Subject: replace legacy calls with new one --- .../CoreBundle/Controller/EntryController.php | 4 +- src/Wallabag/CoreBundle/Helper/Tools.php | 141 +++++++++++++++++++++ src/Wallabag/CoreBundle/Helper/Url.php | 25 ++++ src/Wallabag/CoreBundle/Service/Extractor.php | 2 +- 4 files changed, 169 insertions(+), 3 deletions(-) create mode 100755 src/Wallabag/CoreBundle/Helper/Tools.php create mode 100644 src/Wallabag/CoreBundle/Helper/Url.php (limited to 'src') diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 48d19307..c6c94462 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -8,8 +8,8 @@ use Symfony\Component\HttpFoundation\Request; use Wallabag\CoreBundle\Repository; use Wallabag\CoreBundle\Entity\Entries; use Wallabag\CoreBundle\Service\Extractor; -use Wallabag\Wallabag\Tools; -use Wallabag\Wallabag\Url; +use Wallabag\CoreBundle\Helper\Tools; +use Wallabag\CoreBundle\Helper\Url; class EntryController extends Controller { diff --git a/src/Wallabag/CoreBundle/Helper/Tools.php b/src/Wallabag/CoreBundle/Helper/Tools.php new file mode 100755 index 00000000..c773af37 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/Tools.php @@ -0,0 +1,141 @@ + array( + 'timeout' => $timeout, + 'header' => "User-Agent: " . $useragent, + 'follow_location' => true + ), + 'ssl' => array( + 'verify_peer' => false, + 'allow_self_signed' => true + ) + ) + ); + + # only download page lesser than 4MB + $data = @file_get_contents($url, false, $context, -1, 4000000); + + if (isset($http_response_header) and isset($http_response_header[0])) { + $httpcodeOK = isset($http_response_header) and isset($http_response_header[0]) and ((strpos($http_response_header[0], '200 OK') !== FALSE) or (strpos($http_response_header[0], '301 Moved Permanently') !== FALSE)); + } + } + + # if response is not empty and response is OK + if (isset($data) and isset($httpcodeOK) and $httpcodeOK) { + + # take charset of page and get it + preg_match('##Usi', $data, $meta); + + # if meta tag is found + if (!empty($meta[0])) { + preg_match('#charset="?(.*)"#si', $meta[0], $encoding); + # if charset is found set it otherwise, set it to utf-8 + $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8'; + if (empty($encoding[1])) $encoding[1] = 'utf-8'; + } else { + $html_charset = 'utf-8'; + $encoding[1] = ''; + } + + # replace charset of url to charset of page + $data = str_replace('charset=' . $encoding[1], 'charset=' . $html_charset, $data); + + return $data; + } + else { + return FALSE; + } + } + + /** + * Encode a URL by using a salt + * + * @param $string + * @return string + */ + public static function encodeString($string) + { + return sha1($string . SALT); + } + + /** + * Returns the correct header for a status code + * + * @param $status_code + */ + private static function _status($status_code) + { + if (strpos(php_sapi_name(), 'apache') !== false) { + + header('HTTP/1.0 '.$status_code); + } + else { + + header('Status: '.$status_code); + } + } + + public static function generateToken() + { + if (ini_get('open_basedir') === '') { + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { + // alternative to /dev/urandom for Windows + $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); + } else { + $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); + } + } + else { + $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); + } + + return str_replace('+', '', $token); + } + +} diff --git a/src/Wallabag/CoreBundle/Helper/Url.php b/src/Wallabag/CoreBundle/Helper/Url.php new file mode 100644 index 00000000..b076e7c7 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/Url.php @@ -0,0 +1,25 @@ +url = base64_decode($url); + } + + public function getUrl() { + return $this->url; + } + + public function setUrl($url) { + $this->url = $url; + } + + public function isCorrect() { + return filter_var($this->url, FILTER_VALIDATE_URL) !== FALSE; + } +} \ No newline at end of file diff --git a/src/Wallabag/CoreBundle/Service/Extractor.php b/src/Wallabag/CoreBundle/Service/Extractor.php index 790386d4..1c6ff0ae 100644 --- a/src/Wallabag/CoreBundle/Service/Extractor.php +++ b/src/Wallabag/CoreBundle/Service/Extractor.php @@ -3,7 +3,7 @@ namespace Wallabag\CoreBundle\Service; use Wallabag\CoreBundle\Helper\Content; -use Wallabag\Wallabag\Url; +use Wallabag\CoreBundle\Helper\Url; final class Extractor { -- cgit v1.2.3