]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/functions.php
5 if(isset($_SERVER['HTTPS']))
6 if($_SERVER['HTTPS'] != "off")
9 return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
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);
19 $tpl = new raintpl(); //include Rain TPL
21 $tpl->assign( "url", $url);
22 $tpl->assign( "title", $title);
23 $tpl->assign( "content", $content);
25 $tpl->draw( "index"); // draw the template
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";
33 if (in_array ('curl', get_loaded_extensions())) {
34 // Fetch feed from URL
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);
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);
47 $data = curl_exec($curl);
49 $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE
);
51 $httpcodeOK = isset($httpcode) and ($httpcode == 200 or $httpcode == 301);
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
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);
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));
71 // if response is not empty and response is OK
72 if (isset($data) and isset($httpcodeOK) and $httpcodeOK ) {
74 // take charset of page and get it
75 preg_match('#<meta .*charset=.*>#Usi', $data, $meta);
77 // if meta tag is found
78 if (!empty($meta[0])) {
79 // retrieve encoding in $enc
80 preg_match('#charset="?(.*)"#si', $meta[0], $enc);
82 // if charset is found set it otherwise, set it to utf-8
83 $html_charset = (!empty($enc[1])) ? strtolower($enc[1]) : 'utf-8';
86 $html_charset = 'utf-8';
90 // replace charset of url to charset of page
91 $data = str_replace('charset='.$enc[1], 'charset='.$html_charset, $data);
100 function rel2abs($rel, $base)
102 /* return if already absolute URL */
103 if (parse_url($rel, PHP_URL_SCHEME
) != '') return $rel;
105 /* queries and anchors */
106 if ($rel[0]=='#' || $rel[0]=='?') return $base.$rel;
108 /* parse base URL and convert to local variables:
109 $scheme, $host, $path */
110 extract(parse_url($base));
112 /* remove non-directory element from path */
113 $path = preg_replace('#/[^/]*$#', '', $path);
115 /* destroy path if relative url points to root */
116 if ($rel[0] == '/') $path = '';
118 /* dirty absolute URL */
119 $abs = "$host$path/$rel";
121 /* replace '//' or '/./' or '/foo/../' with '/' */
122 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
123 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
125 /* absolute URL is ready! */
126 return $scheme.'://'.$abs;
129 // $str=preg_replace('#(href|src)="([^
:"]*)("|(?:(?:%
20|\s
|\+
)[^
"]*"))#','$1="http://wintermute.com.au/$2$3',$str);
131 function absolutes_links($data, $base) {
132 // cherche les balises 'a' qui contiennent un href
134 preg_match_all('#(href|src)="([^:"]*)("|(?:(?:%20|\s|\+)[^"]*"))#Si', $data, $matches, PREG_SET_ORDER
);
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]);
140 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1]) ) {
142 $absolutePath=rel2abs($link[2],$base);
144 $data = str_replace($matches[$i][2], $absolutePath, $data);