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