]> git.immae.eu Git - github/wallabag/wallabag.git/blame - inc/functions.php
récupération du titre de la page pochée et rangement de fonctions dans un nouveau...
[github/wallabag/wallabag.git] / inc / functions.php
CommitLineData
24619534 1<?php
2
3function url(){
4 $protocol = "http";
5 if(isset($_SERVER['HTTPS']))
6 if($_SERVER['HTTPS'] != "off")
7 $protocol = "https";
8
9 return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
10}
11
12function generate_page($url,$title,$content) {
13 raintpl::$tpl_dir = './tpl/'; // template directory
14 raintpl::$cache_dir = "./cache/"; // cache directory
15 raintpl::$base_url = url(); // base URL of blog
16 raintpl::configure( 'path_replace', false );
17 raintpl::configure('debug', false);
18
19 $tpl = new raintpl(); //include Rain TPL
20
21 $tpl->assign( "url", $url);
22 $tpl->assign( "title", $title);
23 $tpl->assign( "content", $content);
24
25 $tpl->assign( "version", VERSION);
26
27 $tpl->draw( "index"); // draw the template
28}
29
30// function define to retrieve url content
31function get_external_file($url, $timeout) {
32 // spoofing FireFox 18.0
33 $useragent="Mozilla/5.0 (Windows NT 5.1; rv:18.0) Gecko/20100101 Firefox/18.0";
34
35 if (in_array ('curl', get_loaded_extensions())) {
36 // Fetch feed from URL
37 $curl = curl_init();
38 curl_setopt($curl, CURLOPT_URL, $url);
39 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
40 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
41 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
42 curl_setopt($curl, CURLOPT_HEADER, false);
43
44 // FeedBurner requires a proper USER-AGENT...
45 curl_setopt($curl, CURL_HTTP_VERSION_1_1, true);
46 curl_setopt($curl, CURLOPT_ENCODING, "gzip, deflate");
47 curl_setopt($curl, CURLOPT_USERAGENT, $useragent);
48
49 $data = curl_exec($curl);
50
51 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
52
53 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
54
55 curl_close($curl);
56 } else {
57
58 // create http context and add timeout and user-agent
59 $context = stream_context_create(array('http'=>array('timeout' => $timeout, // Timeout : time until we stop waiting for the response.
60 'header'=> "User-Agent: ".$useragent, // spoot Mozilla Firefox
61 'follow_location' => true
62 )));
63
64 // only download page lesser than 4MB
65 $data = @file_get_contents($url, false, $context, -1, 4000000); // We download at most 4 MB from source.
66 // echo "<pre>http_response_header : ".print_r($http_response_header);
67
68 if(isset($http_response_header) and isset($http_response_header[0])) {
69 $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));
70 }
71 }
72
73 // if response is not empty and response is OK
74 if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) {
75
76 // take charset of page and get it
77 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
78
79 // if meta tag is found
80 if (!empty($meta[0])) {
81 // retrieve encoding in $enc
82 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
83
84 // if charset is found set it otherwise, set it to utf-8
85 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
86
87 } else {
88 $html_charset = 'utf-8';
89 $enc[1] = '';
90 }
91
92 // replace charset of url to charset of page
93 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
94
95 return $data;
96 }
97 else {
98 return FALSE;
99 }
100}
101
102function rel2abs($rel, $base)
103{
104 /* return if already absolute URL */
105 if (parse_url($rel, PHP_URL_SCHEME) != '') return $rel;
106
107 /* queries and anchors */
108 if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
109
110 /* parse base URL and convert to local variables:
111 $scheme, $host, $path */
112 extract(parse_url($base));
113
114 /* remove non-directory element from path */
115 $path = preg_replace('#/[^/]*$#', '', $path);
116
117 /* destroy path if relative url points to root */
118 if ($rel[0] == '/') $path = '';
119
120 /* dirty absolute URL */
121 $abs = "$host$path/$rel";
122
123 /* replace '//' or '/./' or '/foo/../' with '/' */
124 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
125 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
126
127 /* absolute URL is ready! */
128 return $scheme.'://'.$abs;
129}
130
131// $str=preg_replace('#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#','$1="http://wintermute.com.au/$2$3',$str);
132
133function absolutes_links($data, $base) {
134 // cherche les balises 'a' qui contiennent un href
135 $matches = array();
136 preg_match_all('#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#Si', $data, $matches, PREG_SET_ORDER);
137
138 // ne conserve que les liens ne commençant pas par un protocole « protocole:// » ni par une ancre « # »
139 foreach($matches as $i => $link) {
140 $link[1] = trim($link[1]);
141
142 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1]) ) {
143
144 $absolutePath=rel2abs($link[2],$base);
145
146 $data = str_replace($matches[$i][2], $absolutePath, $data);
147 }
148
149 }
150 return $data;
151}
152
153?>