aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornicosomb <nicolas@loeuillet.org>2013-04-04 21:09:34 +0200
committernicosomb <nicolas@loeuillet.org>2013-04-04 21:09:34 +0200
commit246195341cd40fd46b8cfe023b9874f94d6630ce (patch)
treec6be43d023aeaeba1357576028fee3bfae4ae29f
parenta590ea55c2422cd0b11fb60e64275ad7e4f6bd26 (diff)
downloadwallabag-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
-rw-r--r--[-rwxr-xr-x]cache/.gitignore (renamed from inc/index.html)0
-rwxr-xr-xdb/poche.sqlitebin163840 -> 163840 bytes
-rwxr-xr-xinc/functions.php153
-rwxr-xr-xindex.php34
-rwxr-xr-xreadityourself.php167
5 files changed, 180 insertions, 174 deletions
diff --git a/inc/index.html b/cache/.gitignore
index e69de29b..e69de29b 100755..100644
--- a/inc/index.html
+++ b/cache/.gitignore
diff --git a/db/poche.sqlite b/db/poche.sqlite
index 9ae63ec0..6847e957 100755
--- a/db/poche.sqlite
+++ b/db/poche.sqlite
Binary files differ
diff --git a/inc/functions.php b/inc/functions.php
new file mode 100755
index 00000000..9d321c67
--- /dev/null
+++ b/inc/functions.php
@@ -0,0 +1,153 @@
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?> \ No newline at end of file
diff --git a/index.php b/index.php
index a27f1993..4fdc5736 100755
--- a/index.php
+++ b/index.php
@@ -22,6 +22,9 @@
22 * afficher liens mis en favoris et archivés 22 * afficher liens mis en favoris et archivés
23 * tri des liens 23 * tri des liens
24 */ 24 */
25require_once dirname(__FILE__).'/inc/Readability.php';
26require_once dirname(__FILE__).'/inc/Encoding.php';
27include dirname(__FILE__).'/inc/functions.php';
25 28
26try 29try
27{ 30{
@@ -39,8 +42,21 @@ $id = (isset ($_GET['id'])) ? htmlspecialchars($_GET['id']) : '';
39 42
40switch ($action) { 43switch ($action) {
41 case 'add': 44 case 'add':
42 $url = (isset ($_GET['url'])) ? htmlspecialchars($_GET['url']) : ''; 45 $url = (isset ($_GET['url'])) ? $_GET['url'] : '';
46 $url = html_entity_decode(trim($url));
43 $title = $url; 47 $title = $url;
48 // if url use https protocol change it to http
49 if (!preg_match('!^https?://!i', $url)) $url = 'http://'.$url;
50 // convert page to utf-8
51 $html = Encoding::toUTF8(get_external_file($url,15));
52 if(isset($html) and strlen($html) > 0) {
53 // send result to readability library
54 $r = new Readability($html, $url);
55 if($r->init()) {
56 $title = $r->articleTitle->innerHTML;
57 }
58 }
59
44 $query = $db_handle->prepare('INSERT INTO entries ( url, title ) VALUES (?, ?)'); 60 $query = $db_handle->prepare('INSERT INTO entries ( url, title ) VALUES (?, ?)');
45 $query->execute(array($url, $title)); 61 $query->execute(array($url, $title));
46 break; 62 break;
@@ -53,6 +69,8 @@ switch ($action) {
53 $params_action = array($id); 69 $params_action = array($id);
54 break; 70 break;
55 case 'delete': 71 case 'delete':
72 $sql_action = "DELETE FROM entries WHERE id=?";
73 $params_action = array($id);
56 break; 74 break;
57 default: 75 default:
58 break; 76 break;
@@ -98,16 +116,6 @@ catch (Exception $e)
98 die('query error : '.$e->getMessage()); 116 die('query error : '.$e->getMessage());
99} 117}
100 118
101function url() {
102 $protocol = "http";
103 if(isset($_SERVER['HTTPS'])) {
104 if($_SERVER['HTTPS'] != "off") {
105 $protocol = "https";
106 }
107 }
108
109 return $protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
110}
111?> 119?>
112<!DOCTYPE html> 120<!DOCTYPE html>
113<!--[if lte IE 6]> <html class="no-js ie6 ie67 ie678" lang="en"> <![endif]--> 121<!--[if lte IE 6]> <html class="no-js ie6 ie67 ie678" lang="en"> <![endif]-->
@@ -137,13 +145,13 @@ function url() {
137 <ul id="entries"> 145 <ul id="entries">
138 <?php 146 <?php
139 foreach ($entries as $entry) { 147 foreach ($entries as $entry) {
140 echo '<li><a href="readityourself.php?url='.urlencode($entry['url']).'">' . $entry['title'] . '</a> <a href="?action=toggle_archive&id='.$entry['id'].'" title="toggle mark as read" class="tool">&#10003;</a> <a href="?action=toggle_fav&id='.$entry['id'].'" title="toggle favorite" class="tool">'.(($entry['is_fav'] == 0) ? '&#9734;' : '&#9733;' ).'</a> <a href="#" title="toggle delete" class="tool">&#10799;</a></li>'; 148 echo '<li><a href="readityourself.php?url='.urlencode($entry['url']).'">' . $entry['title'] . '</a> <a href="?action=toggle_archive&id='.$entry['id'].'" title="toggle mark as read" class="tool">&#10003;</a> <a href="?action=toggle_fav&id='.$entry['id'].'" title="toggle favorite" class="tool">'.(($entry['is_fav'] == 0) ? '&#9734;' : '&#9733;' ).'</a> <a href="?action=delete&id='.$entry['id'].'" title="toggle delete" class="tool">&#10799;</a></li>';
141 } 149 }
142 ?> 150 ?>
143 </ul> 151 </ul>
144 </div> 152 </div>
145 <footer class="mr2 mt3"> 153 <footer class="mr2 mt3">
146 <p class="smaller"><a href="http://github.com/nicosomb/poche">poche</a> is a read it later open source system, based on <a href="http://www.memiks.fr/readityourself/">ReadItYourself</a>. poche is developed by <a href="http://nicolas.loeuillet.org">Nicolas LÅ“uillet</a> under the <a href="http://www.wtfpl.net/">Do What the Fuck You Want to Public License</a></p> 154 <p class="smaller"><a href="http://github.com/nicosomb/poche">poche</a> is a read it later open source system, based on <a href="http://www.memiks.fr/readityourself/">ReadItYourself</a>. poche is developed by <a href="http://nicolas.loeuillet.org">Nicolas LÅ“uillet</a> under the <a href="http://www.wtfpl.net/">WTFPL</a>.</p>
147 </footer> 155 </footer>
148 </body> 156 </body>
149</html> \ No newline at end of file 157</html> \ No newline at end of file
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.
22require_once dirname(__FILE__).'/inc/rain.tpl.class.php'; 22require_once dirname(__FILE__).'/inc/rain.tpl.class.php';
23 23
24// FUNCTIONS BEGIN 24include dirname(__FILE__).'/inc/functions.php';
25
26
27
28function 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
37function 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
56function 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
127function 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
158function 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