diff options
author | nicosomb <nicolas@loeuillet.org> | 2013-04-04 21:09:34 +0200 |
---|---|---|
committer | nicosomb <nicolas@loeuillet.org> | 2013-04-04 21:09:34 +0200 |
commit | 246195341cd40fd46b8cfe023b9874f94d6630ce (patch) | |
tree | c6be43d023aeaeba1357576028fee3bfae4ae29f /readityourself.php | |
parent | a590ea55c2422cd0b11fb60e64275ad7e4f6bd26 (diff) | |
download | wallabag-246195341cd40fd46b8cfe023b9874f94d6630ce.tar.gz wallabag-246195341cd40fd46b8cfe023b9874f94d6630ce.tar.zst wallabag-246195341cd40fd46b8cfe023b9874f94d6630ce.zip |
récupération du titre de la page pochée et rangement de fonctions dans un nouveau fichier
Diffstat (limited to 'readityourself.php')
-rwxr-xr-x | readityourself.php | 167 |
1 files changed, 6 insertions, 161 deletions
diff --git a/readityourself.php b/readityourself.php index c59bb9d1..451b5878 100755 --- a/readityourself.php +++ b/readityourself.php | |||
@@ -21,162 +21,7 @@ require_once dirname(__FILE__).'/inc/Encoding.php'; | |||
21 | // appel de la libraire RainTPL. | 21 | // appel de la libraire RainTPL. |
22 | require_once dirname(__FILE__).'/inc/rain.tpl.class.php'; | 22 | require_once dirname(__FILE__).'/inc/rain.tpl.class.php'; |
23 | 23 | ||
24 | // FUNCTIONS BEGIN | 24 | include dirname(__FILE__).'/inc/functions.php'; |
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 | 25 | ||
181 | // EXUCUTION CODE | 26 | // EXUCUTION CODE |
182 | 27 | ||
@@ -190,15 +35,15 @@ if(isset($_GET['url']) && $_GET['url'] != null && trim($_GET['url']) != "") { | |||
190 | 35 | ||
191 | // decode it | 36 | // decode it |
192 | $url = html_entity_decode($url); | 37 | $url = html_entity_decode($url); |
193 | 38 | ||
194 | // if url use https protocol change it to http | 39 | // if url use https protocol change it to http |
195 | if (!preg_match('!^https?://!i', $url)) $url = 'http://'.$url; | 40 | if (!preg_match('!^https?://!i', $url)) $url = 'http://'.$url; |
196 | 41 | ||
197 | // convert page to utf-8 | 42 | // convert page to utf-8 |
198 | $html = Encoding::toUTF8(get_external_file($url,15)); | 43 | $html = Encoding::toUTF8(get_external_file($url,15)); |
199 | 44 | ||
200 | if(isset($html) and strlen($html) > 0) { | 45 | if(isset($html) and strlen($html) > 0) { |
201 | 46 | ||
202 | // send result to readability library | 47 | // send result to readability library |
203 | $r = new Readability($html, $url); | 48 | $r = new Readability($html, $url); |
204 | 49 | ||
@@ -213,4 +58,4 @@ if(isset($_GET['url']) && $_GET['url'] != null && trim($_GET['url']) != "") { | |||
213 | } | 58 | } |
214 | } | 59 | } |
215 | } | 60 | } |
216 | ?> | 61 | ?> \ No newline at end of file |