]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - readityourself.php
3 define("VERSION", "0.0.3");
5 header('Content-type:text/html; charset=utf-8');
6 // Set locale to French
7 setlocale(LC_ALL
, 'fr_FR');
9 // set timezone to Europe/Paris
10 date_default_timezone_set('Europe/Paris');
12 // set charset to utf-8 important since all pages will be transform to utf-8
13 header('Content-Type: text/html;charset=utf-8');
15 // get readability library
16 require_once dirname(__FILE__
).'/inc/Readability.php';
18 // get Encoding library.
19 require_once dirname(__FILE__
).'/inc/Encoding.php';
21 // appel de la libraire RainTPL.
22 require_once dirname(__FILE__
).'/inc/rain.tpl.class.php';
29 $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
30 return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
33 function generate_page($url,$title,$content) {
34 raintpl
::$tpl_dir = './tpl/'; // template directory
35 raintpl
::$cache_dir = "./cache/"; // cache directory
36 raintpl
::$base_url = url(); // base URL of blog
37 raintpl
::configure( 'path_replace', false );
38 raintpl
::configure('debug', false);
40 $tpl = new raintpl(); //include Rain TPL
42 $tpl->assign( "url", $url);
43 $tpl->assign( "title", $title);
44 $tpl->assign( "content", $content);
46 $tpl->assign( "version", VERSION
);
48 $tpl->draw( "index"); // draw the template
51 // function define to retrieve url content
52 function get_external_file($url, $timeout) {
53 // spoofing FireFox 18.0
54 $useragent="Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
56 if (in_array ('curl', get_loaded_extensions())) {
57 // Fetch feed from URL
59 curl_setopt($curl, CURLOPT_URL
, $url);
60 curl_setopt($curl, CURLOPT_TIMEOUT
, $timeout);
61 curl_setopt($curl, CURLOPT_FOLLOWLOCATION
, true);
62 curl_setopt($curl, CURLOPT_RETURNTRANSFER
, true);
63 curl_setopt($curl, CURLOPT_HEADER
, false);
65 // FeedBurner requires a proper USER-AGENT...
66 curl_setopt($curl, CURL_HTTP_VERSION_1_1
, true);
67 curl_setopt($curl, CURLOPT_ENCODING
, "gzip, deflate");
68 curl_setopt($curl, CURLOPT_USERAGENT
, $useragent);
70 $data = curl_exec($curl);
72 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
74 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
79 // create http context and add timeout and user-agent
80 $context = stream_context_create(array('http'=>array('timeout' => $timeout, // Timeout : time until we stop waiting for the response.
81 'header'=> "User-Agent: ".$useragent, // spoot Mozilla Firefox
82 'follow_location' => true
85 // only download page lesser than 4MB
86 $data = @file_get_contents($url, false, $context, -1, 4000000); // We download at most 4 MB from source.
87 // echo "<pre>http_response_header : ".print_r($http_response_header);
89 if(isset($http_response_header) and isset($http_response_header[0])) {
90 $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));
94 // if response is not empty and response is OK
95 if (isset($data) and isset($httpcodeOK) and httpcodeOK
) {
97 // take charset of page and get it
98 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
100 // if meta tag is found
101 if (!empty($meta[0])) {
102 // retrieve encoding in $enc
103 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
105 // if charset is found set it otherwise, set it to utf-8
106 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
109 $html_charset = 'utf-8';
113 // replace charset of url to charset of page
114 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
123 function rel2abs($rel, $base)
125 /* return if already absolute URL */
126 if (parse_url($rel, PHP_URL_SCHEME
) != '') return $rel;
128 /* queries and anchors */
129 if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
131 /* parse base URL and convert to local variables:
132 $scheme, $host, $path */
133 extract(parse_url($base));
135 /* remove non-directory element from path */
136 $path = preg_replace('#/[^/]*$#', '', $path);
138 /* destroy path if relative url points to root */
139 if ($rel[0] == '/') $path = '';
141 /* dirty absolute URL */
142 $abs = "$host$path/$rel";
144 /* replace '//' or '/./' or '/foo/../' with '/' */
145 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
146 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
148 /* absolute URL is ready! */
149 return $scheme.'://'.$abs;
152 // $str=preg_replace('#(href|src)="([^
:"]*)("|(?:(?:%
20|\s
|\+
)[^
"]*"))#','$1="http://wintermute.com.au/$2$3',$str);
154 function absolutes_links($data, $base) {
155 // cherche les balises 'a' qui contiennent un href
157 preg_match_all('#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#Si', $data, $matches, PREG_SET_ORDER
);
159 // ne conserve que les liens ne commençant pas par un protocole « protocole:// » ni par une ancre « # »
160 foreach($matches as $i => $link) {
161 $link[1] = trim($link[1]);
163 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1]) ) {
165 $absolutePath=rel2abs($link[2],$base);
167 $data = str_replace($matches[$i][2], $absolutePath, $data);
180 if(isset($_GET['url']) && $_GET['url'] != null && trim($_GET['url']) != "") {
182 if(strlen(trim($_GET['url'])) > 2048) {
183 echo "Error URL is too large !!";
185 $url = trim($_GET['url']);
188 $url = html_entity_decode($url);
190 // if url use https protocol change it to http
191 if (!preg_match('!^https?://!i', $url)) $url = 'http://'.$url;
193 // convert page to utf-8
194 $html = Encoding
::toUTF8(get_external_file($url,15));
196 if(isset($html) and strlen($html) > 0) {
198 // send result to readability library
199 $r = new Readability($html, $url);
202 generate_page($url,$r->articleTitle
->innerHTML
,$r->articleContent
->innerHTML
);
204 // return data into an iframe
205 echo "<iframe id='readabilityframe'>".$html."</iframe>";
208 echo "Error unable to get link : ".$url;