aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/poche/pochePictures.php
diff options
context:
space:
mode:
authorNicolas Lœuillet <nicolas.loeuillet@gmail.com>2013-08-02 22:40:51 +0200
committerNicolas Lœuillet <nicolas.loeuillet@gmail.com>2013-08-02 22:40:51 +0200
commita4565e88edbc8e3bd092a475469769c86a4c350c (patch)
treea6a3c935b03a23ff87575c8c315cf8ba78fe68c2 /inc/poche/pochePictures.php
parentf6c9baab3efeec1d0efa151e276fc08d5b58f9e9 (diff)
downloadwallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.tar.gz
wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.tar.zst
wallabag-a4565e88edbc8e3bd092a475469769c86a4c350c.zip
add Twig & refactor poche
Diffstat (limited to 'inc/poche/pochePictures.php')
-rw-r--r--inc/poche/pochePictures.php114
1 files changed, 114 insertions, 0 deletions
diff --git a/inc/poche/pochePictures.php b/inc/poche/pochePictures.php
new file mode 100644
index 00000000..bfc80657
--- /dev/null
+++ b/inc/poche/pochePictures.php
@@ -0,0 +1,114 @@
1<?php
2/**
3 * poche, a read it later open source system
4 *
5 * @category poche
6 * @author Nicolas Lœuillet <support@inthepoche.com>
7 * @copyright 2013
8 * @license http://www.wtfpl.net/ see COPYING file
9 */
10
11/**
12 * On modifie les URLS des images dans le corps de l'article
13 */
14function filtre_picture($content, $url, $id)
15{
16 $matches = array();
17 preg_match_all('#<\s*(img)[^>]+src="([^"]*)"[^>]*>#Si', $content, $matches, PREG_SET_ORDER);
18 foreach($matches as $i => $link)
19 {
20 $link[1] = trim($link[1]);
21 if (!preg_match('#^(([a-z]+://)|(\#))#', $link[1]) )
22 {
23 $absolute_path = get_absolute_link($link[2],$url);
24 $filename = basename(parse_url($absolute_path, PHP_URL_PATH));
25 $directory = create_assets_directory($id);
26 $fullpath = $directory . '/' . $filename;
27 download_pictures($absolute_path, $fullpath);
28 $content = str_replace($matches[$i][2], $fullpath, $content);
29 }
30
31 }
32
33 return $content;
34}
35
36/**
37 * Retourne le lien absolu
38 */
39function get_absolute_link($relative_link, $url)
40{
41 /* return if already absolute URL */
42 if (parse_url($relative_link, PHP_URL_SCHEME) != '') return $relative_link;
43
44 /* queries and anchors */
45 if ($relative_link[0]=='#' || $relative_link[0]=='?') return $url . $relative_link;
46
47 /* parse base URL and convert to local variables:
48 $scheme, $host, $path */
49 extract(parse_url($url));
50
51 /* remove non-directory element from path */
52 $path = preg_replace('#/[^/]*$#', '', $path);
53
54 /* destroy path if relative url points to root */
55 if ($relative_link[0] == '/') $path = '';
56
57 /* dirty absolute URL */
58 $abs = $host . $path . '/' . $relative_link;
59
60 /* replace '//' or '/./' or '/foo/../' with '/' */
61 $re = array('#(/\.?/)#', '#/(?!\.\.)[^/]+/\.\./#');
62 for($n=1; $n>0; $abs=preg_replace($re, '/', $abs, -1, $n)) {}
63
64 /* absolute URL is ready! */
65 return $scheme.'://'.$abs;
66}
67
68/**
69 * Téléchargement des images
70 */
71
72function download_pictures($absolute_path, $fullpath)
73{
74 $rawdata = get_external_file($absolute_path);
75
76 if(file_exists($fullpath)) {
77 unlink($fullpath);
78 }
79 $fp = fopen($fullpath, 'x');
80 fwrite($fp, $rawdata);
81 fclose($fp);
82}
83
84/**
85 * Crée un répertoire de médias pour l'article
86 */
87function create_assets_directory($id)
88{
89 $assets_path = ABS_PATH;
90 if(!is_dir($assets_path)) {
91 mkdir($assets_path, 0705);
92 }
93
94 $article_directory = $assets_path . $id;
95 if(!is_dir($article_directory)) {
96 mkdir($article_directory, 0705);
97 }
98
99 return $article_directory;
100}
101
102/**
103 * Suppression du répertoire d'images
104 */
105function remove_directory($directory)
106{
107 if(is_dir($directory)) {
108 $files = array_diff(scandir($directory), array('.','..'));
109 foreach ($files as $file) {
110 (is_dir("$directory/$file")) ? remove_directory("$directory/$file") : unlink("$directory/$file");
111 }
112 return rmdir($directory);
113 }
114}