]> git.immae.eu Git - github/wallabag/wallabag.git/blob - readityourself.php
Fix undefined index and constant problems
[github/wallabag/wallabag.git] / readityourself.php
1 <?php
2
3 define("VERSION", "0.0.3");
4
5 header('Content-type:text/html; charset=utf-8');
6 // Set locale to French
7 setlocale(LC_ALL, 'fr_FR');
8
9 // set timezone to Europe/Paris
10 date_default_timezone_set('Europe/Paris');
11
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');
14
15 // get readability library
16 require_once dirname(__FILE__).'/inc/Readability.php';
17
18 // get Encoding library.
19 require_once dirname(__FILE__).'/inc/Encoding.php';
20
21 // appel de la libraire RainTPL.
22 require_once dirname(__FILE__).'/inc/rain.tpl.class.php';
23
24 // FUNCTIONS BEGIN
25
26
27
28 function url(){
29 $protocol = "http";
30 if(isset($_SERVER['HTTPS']))
31 if($_SERVER['HTTPS'] != "off")
32 $protocol = "https";
33
34 return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
35 }
36
37 function generate_page($url,$title,$content) {
38 raintpl::$tpl_dir = './tpl/'; // template directory
39 raintpl::$cache_dir = "./cache/"; // cache directory
40 raintpl::$base_url = url(); // base URL of blog
41 raintpl::configure( 'path_replace', false );
42 raintpl::configure('debug', false);
43
44 $tpl = new raintpl(); //include Rain TPL
45
46 $tpl->assign( "url", $url);
47 $tpl->assign( "title", $title);
48 $tpl->assign( "content", $content);
49
50 $tpl->assign( "version", VERSION);
51
52 $tpl->draw( "index"); // draw the template
53 }
54
55 // function define to retrieve url content
56 function get_external_file($url, $timeout) {
57 // spoofing FireFox 18.0
58 $useragent="Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
59
60 if (in_array ('curl', get_loaded_extensions())) {
61 // Fetch feed from URL
62 $curl = curl_init();
63 curl_setopt($curl, CURLOPT_URL, $url);
64 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
65 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
66 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
67 curl_setopt($curl, CURLOPT_HEADER, false);
68
69 // FeedBurner requires a proper USER-AGENT...
70 curl_setopt($curl, CURL_HTTP_VERSION_1_1, true);
71 curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate");
72 curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
73
74 $data = curl_exec($curl);
75
76 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
77
78 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
79
80 curl_close($curl);
81 } else {
82
83 // create http context and add timeout and user-agent
84 $context = stream_context_create(array('http'=>array('timeout' => $timeout, // Timeout : time until we stop waiting for the response.
85 'header'=> "User-Agent: ".$useragent, // spoot Mozilla Firefox
86 'follow_location' => true
87 )));
88
89 // only download page lesser than 4MB
90 $data = @file_get_contents($url, false, $context, -1, 4000000); // We download at most 4 MB from source.
91 // echo "<pre>http_response_header : ".print_r($http_response_header);
92
93 if(isset($http_response_header) and isset($http_response_header[0])) {
94 $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));
95 }
96 }
97
98 // if response is not empty and response is OK
99 if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) {
100
101 // take charset of page and get it
102 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
103
104 // if meta tag is found
105 if (!empty($meta[0])) {
106 // retrieve encoding in $enc
107 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
108
109 // if charset is found set it otherwise, set it to utf-8
110 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
111
112 } else {
113 $html_charset = 'utf-8';
114 $enc[1] = '';
115 }
116
117 // replace charset of url to charset of page
118 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
119
120 return $data;
121 }
122 else {
123 return FALSE;
124 }
125 }
126
127 function rel2abs($rel, $base)
128 {
129 /* return if already absolute URL */
130 if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
131
132 /* queries and anchors */
133 if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
134
135 /* parse base URL and convert to local variables:
136 $scheme, $host, $path */
137 extract(parse_url($base));
138
139 /* remove non-directory element from path */
140 $path = preg_replace('#/[^/]*$#', '', $path);
141
142 /* destroy path if relative url points to root */
143 if ($rel[0] == '/') $path = '';
144
145 /* dirty absolute URL */
146 $abs = "$host$path/$rel";
147
148 /* replace '//' or '/./' or '/foo/../' with '/' */
149 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
150 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
151
152 /* absolute URL is ready! */
153 return $scheme.'://'.$abs;
154 }
155
156 // $str=preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#','$1="http://wintermute.com.au/$2$3',$str);
157
158 function absolutes_links($data, $base) {
159 // cherche les balises 'a' qui contiennent un href
160 $matches = array();
161 preg_match_all('#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#Si', $data, $matches, PREG_SET_ORDER);
162
163 // ne conserve que les liens ne commençant pas par un protocole « protocole:// » ni par une ancre « # »
164 foreach($matches as $i => $link) {
165 $link[1] = trim($link[1]);
166
167 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1]) ) {
168
169 $absolutePath=rel2abs($link[2],$base);
170
171 $data = str_replace($matches[$i][2], $absolutePath, $data);
172 }
173
174 }
175 return $data;
176 }
177
178
179 // FUNCTIONS END
180
181 // EXUCUTION CODE
182
183
184 if(isset($_GET['url']) && $_GET['url'] != null && trim($_GET['url']) != "") {
185 // get url link
186 if(strlen(trim($_GET['url'])) > 2048) {
187 echo "Error URL is too large !!";
188 } else {
189 $url = trim($_GET['url']);
190
191 // decode it
192 $url = html_entity_decode($url);
193
194 // if url use https protocol change it to http
195 if (!preg_match('!^https?://!i', $url)) $url = 'http://'.$url;
196
197 // convert page to utf-8
198 $html = Encoding::toUTF8(get_external_file($url,15));
199
200 if(isset($html) and strlen($html) > 0) {
201
202 // send result to readability library
203 $r = new Readability($html, $url);
204
205 if($r->init()) {
206 generate_page($url,$r->articleTitle->innerHTML,$r->articleContent->innerHTML);
207 } else {
208 // return data into an iframe
209 echo "<iframe id='readabilityframe'>".$html."</iframe>";
210 }
211 } else {
212 echo "Error unable to get link : ".$url;
213 }
214 }
215 }
216 ?>