aboutsummaryrefslogtreecommitdiffhomepage
path: root/readityourself.php
diff options
context:
space:
mode:
Diffstat (limited to 'readityourself.php')
-rwxr-xr-xreadityourself.php212
1 files changed, 212 insertions, 0 deletions
diff --git a/readityourself.php b/readityourself.php
new file mode 100755
index 00000000..4a4661c2
--- /dev/null
+++ b/readityourself.php
@@ -0,0 +1,212 @@
1<?php
2
3define("VERSION", "0.0.3");
4
5header('Content-type:text/html; charset=utf-8');
6// Set locale to French
7setlocale(LC_ALL, 'fr_FR');
8
9// set timezone to Europe/Paris
10date_default_timezone_set('Europe/Paris');
11
12// set charset to utf-8 important since all pages will be transform to utf-8
13header('Content-Type: text/html;charset=utf-8');
14
15// get readability library
16require_once dirname(__FILE__).'/inc/Readability.php';
17
18// get Encoding library.
19require_once dirname(__FILE__).'/inc/Encoding.php';
20
21// appel de la libraire RainTPL.
22require_once dirname(__FILE__).'/inc/rain.tpl.class.php';
23
24// FUNCTIONS BEGIN
25
26
27
28function url(){
29 $protocol = ($_SERVER['HTTPS'] && $_SERVER['HTTPS'] != "off") ? "https" : "http";
30 return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
31}
32
33function 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);
39
40 $tpl = new raintpl(); //include Rain TPL
41
42 $tpl->assign( "url", $url);
43 $tpl->assign( "title", $title);
44 $tpl->assign( "content", $content);
45
46 $tpl->assign( "version", VERSION);
47
48 $tpl->draw( "index"); // draw the template
49}
50
51// function define to retrieve url content
52function 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";
55
56 if (in_array ('curl', get_loaded_extensions())) {
57 // Fetch feed from URL
58 $curl = curl_init();
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);
64
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);
69
70 $data = curl_exec($curl);
71
72 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
73
74 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
75
76 curl_close($curl);
77 } else {
78
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
83 )));
84
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);
88
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));
91 }
92 }
93
94 // if response is not empty and response is OK
95 if (isset($data) and isset($httpcodeOK) and httpcodeOK ) {
96
97 // take charset of page and get it
98 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
99
100 // if meta tag is found
101 if (!empty($meta[0])) {
102 // retrieve encoding in $enc
103 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
104
105 // if charset is found set it otherwise, set it to utf-8
106 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
107
108 } else {
109 $html_charset = 'utf-8';
110 $enc[1] = '';
111 }
112
113 // replace charset of url to charset of page
114 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
115
116 return $data;
117 }
118 else {
119 return FALSE;
120 }
121}
122
123function rel2abs($rel, $base)
124{
125 /* return if already absolute URL */
126 if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
127
128 /* queries and anchors */
129 if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
130
131 /* parse base URL and convert to local variables:
132 $scheme, $host, $path */
133 extract(parse_url($base));
134
135 /* remove non-directory element from path */
136 $path = preg_replace('#/[^/]*$#', '', $path);
137
138 /* destroy path if relative url points to root */
139 if ($rel[0] == '/') $path = '';
140
141 /* dirty absolute URL */
142 $abs = "$host$path/$rel";
143
144 /* replace '//' or '/./' or '/foo/../' with '/' */
145 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
146 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
147
148 /* absolute URL is ready! */
149 return $scheme.'://'.$abs;
150}
151
152// $str=preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#','$1="http://wintermute.com.au/$2$3',$str);
153
154function absolutes_links($data, $base) {
155 // cherche les balises 'a' qui contiennent un href
156 $matches = array();
157 preg_match_all('#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#Si', $data, $matches, PREG_SET_ORDER);
158
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]);
162
163 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1]) ) {
164
165 $absolutePath=rel2abs($link[2],$base);
166
167 $data = str_replace($matches[$i][2], $absolutePath, $data);
168 }
169
170 }
171 return $data;
172}
173
174
175// FUNCTIONS END
176
177// EXUCUTION CODE
178
179
180if(isset($_GET['url']) && $_GET['url'] != null && trim($_GET['url']) != "") {
181 // get url link
182 if(strlen(trim($_GET['url'])) > 2048) {
183 echo "Error URL is too large !!";
184 } else {
185 $url = trim($_GET['url']);
186
187 // decode it
188 $url = html_entity_decode($url);
189
190 // if url use https protocol change it to http
191 if (!preg_match('!^https?://!i', $url)) $url = 'http://'.$url;
192
193 // convert page to utf-8
194 $html = Encoding::toUTF8(get_external_file($url,15));
195
196 if(isset($html) and strlen($html) > 0) {
197
198 // send result to readability library
199 $r = new Readability($html, $url);
200
201 if($r->init()) {
202 generate_page($url,$r->articleTitle->innerHTML,$r->articleContent->innerHTML);
203 } else {
204 // return data into an iframe
205 echo "<iframe id='readabilityframe'>".$html."</iframe>";
206 }
207 } else {
208 echo "Error unable to get link : ".$url;
209 }
210 }
211}
212?>