]> git.mxchange.org Git - friendica-addons.git/blob - privacy_image_cache/privacy_image_cache.php
Merge remote branch 'upstream/master'
[friendica-addons.git] / privacy_image_cache / privacy_image_cache.php
1 <?php
2
3 /**
4  * Name: Privacy Image Cache
5  * Version: 0.1
6  * Author: Tobias Hößl <https://github.com/CatoTH/>
7  */
8
9 define("PRIVACY_IMAGE_CACHE_DEFAULT_TIME", 86400); // 1 Day
10
11 require_once('include/security.php');
12
13 function privacy_image_cache_install() {
14     register_hook('bbcode',       'addon/privacy_image_cache/privacy_image_cache.php', 'privacy_image_cache_bbcode_hook');
15     register_hook('display_item', 'addon/privacy_image_cache/privacy_image_cache.php', 'privacy_image_cache_display_item_hook');
16     register_hook('ping_xmlize',  'addon/privacy_image_cache/privacy_image_cache.php', 'privacy_image_cache_ping_xmlize_hook');
17     register_hook('cron',         'addon/privacy_image_cache/privacy_image_cache.php', 'privacy_image_cache_cron');
18 }
19
20
21 function privacy_image_cache_uninstall() {
22     unregister_hook('bbcode',       'addon/privacy_image_cache/privacy_image_cache.php', 'privacy_image_cache_bbcode_hook');
23     unregister_hook('display_item', 'addon/privacy_image_cache/privacy_image_cache.php', 'privacy_image_cache_display_item_hook');
24     unregister_hook('ping_xmlize',  'addon/privacy_image_cache/privacy_image_cache.php', 'privacy_image_cache_ping_xmlize_hook');
25     unregister_hook('cron',         'addon/privacy_image_cache/privacy_image_cache.php', 'privacy_image_cache_cron');
26 }
27
28
29 function privacy_image_cache_module() {}
30
31
32 function privacy_image_cache_init() {
33     $urlhash = 'pic:' . sha1($_REQUEST['url']);
34     $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' LIMIT 1", $urlhash );
35     if (count($r)) {
36         $img_str = $r[0]['data'];
37     }
38     else {
39         require_once("Photo.php");
40
41         $img_str = fetch_url($_REQUEST['url'],true);
42         $img = new Photo($img_str);
43         if($img->is_valid()) {
44             $img->store(0, 0, $urlhash, $_REQUEST['url'], '', 100);
45             $img_str = $img->imageString();
46         }
47     }
48
49
50     header("Content-type: image/jpeg");
51     header("Expires: " . gmdate("D, d M Y H:i:s", time() + (3600*24)) . " GMT");
52     header("Cache-Control: max-age=" . (3600*24));
53
54     echo $img_str;
55
56     killme();
57 }
58
59 /**
60  * @param $url string
61  * @return boolean
62  */
63 function privacy_image_cache_is_local_image($url) {
64     if ($url[0] == '/') return true;
65         // links normalised - bug #431
66     $baseurl = normalise_link(get_app()->get_baseurl());
67         $url = normalise_link($url);
68     return (substr($url, 0, strlen($baseurl)) == $baseurl);
69 }
70
71 /**
72  * @param array $matches
73  * @return string
74  */
75 function privacy_image_cache_img_cb($matches) {
76         // following line changed per bug #431
77     if (privacy_image_cache_is_local_image($matches[2])) return $matches[1] . $matches[2] . $matches[3];
78     return $matches[1] . "/privacy_image_cache/?url=" . escape_tags(addslashes($matches[2])) . $matches[3];
79 }
80
81 /**
82  * @param App $a
83  * @param string $o
84  */
85 function privacy_image_cache_bbcode_hook(&$a, &$o) {
86     $o = preg_replace_callback("/(<img [^>]*src *= *[\"'])([^\"']+)([\"'][^>]*>)/siU", "privacy_image_cache_img_cb", $o);
87 }
88
89
90 /**
91  * @param App $a
92  * @param string $o
93  */
94 function privacy_image_cache_display_item_hook(&$a, &$o) {
95     if (isset($o["output"])) {
96         if (isset($o["output"]["thumb"]) && !privacy_image_cache_is_local_image($o["output"]["thumb"]))
97             $o["output"]["thumb"] = "/privacy_image_cache/?url=" . escape_tags(addslashes($o["output"]["thumb"]));
98         if (isset($o["output"]["author-avatar"]) && !privacy_image_cache_is_local_image($o["output"]["author-avatar"]))
99             $o["output"]["author-avatar"] = "/privacy_image_cache/?url=" . escape_tags(addslashes($o["output"]["author-avatar"]));
100     }
101 }
102
103
104 /**
105  * @param App $a
106  * @param string $o
107  */
108 function privacy_image_cache_ping_xmlize_hook(&$a, &$o) {
109     if ($o["photo"] != "" && !privacy_image_cache_is_local_image($o["photo"]))
110         $o["photo"] = "/privacy_image_cache/?url=" . escape_tags(addslashes($o["photo"]));
111 }
112
113
114 /**
115  * @param App $a
116  * @param null|object $b
117  */
118 function privacy_image_cache_cron(&$a = null, &$b = null) {
119     $cachetime = get_config('privacy_image_cache','cache_time');
120     if (!$cachetime) $cachetime = PRIVACY_IMAGE_CACHE_DEFAULT_TIME;
121
122     $last = get_config('pi_cache','last_delete');
123     $time = time();
124     if ($time < ($last + 3600)) return;
125
126     logger("Purging old Cache of the Privacy Image Cache", LOGGER_DEBUG);
127     q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%" AND `created` < NOW() - INTERVAL %d SECOND', $cachetime);
128     set_config('pi_cache', 'last_delete', $time);
129 }
130
131
132
133
134 /**
135  * @param App $a
136  * @param null|object $o
137  */
138 function privacy_image_cache_plugin_admin(&$a, &$o){
139
140
141     $o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token("picsave") . '">';
142
143     $cachetime = get_config('privacy_image_cache','cache_time');
144     if (!$cachetime) $cachetime = PRIVACY_IMAGE_CACHE_DEFAULT_TIME;
145     $cachetime_h = Ceil($cachetime / 3600);
146
147     $o .= '<label for="pic_cachetime">' . t('Lifetime of the cache (in hours)') . '</label>
148         <input id="pic_cachetime" name="cachetime" type="text" value="' . escape_tags($cachetime_h) . '"><br style="clear: both;">';
149
150     $o .= '<input type="submit" name="save" value="' . t('Save') . '">';
151
152     $o .= '<h4>' . t('Cache Statistics') . '</h4>';
153
154     $num = q('SELECT COUNT(*) num, SUM(LENGTH(data)) size FROM `photo` WHERE `uid`=0 AND `contact-id`=0 AND `resource-id` LIKE "pic:%%"');
155     $o .= '<label for="statictics_num">' . t('Number of items') . '</label><input style="color: gray;" id="statistics_num" disabled value="' . escape_tags($num[0]['num']) . '"><br style="clear: both;">';
156     $size = Ceil($num[0]['size'] / (1024 * 1024));
157     $o .= '<label for="statictics_size">' . t('Size of the cache') . '</label><input style="color: gray;" id="statistics_size" disabled value="' . $size . ' MB"><br style="clear: both;">';
158
159     $o .= '<input type="submit" name="delete_all" value="' . t('Delete the whole cache') . '">';
160 }
161
162
163 /**
164  * @param App $a
165  * @param null|object $o
166  */
167 function privacy_image_cache_plugin_admin_post(&$a = null, &$o = null){
168     check_form_security_token_redirectOnErr('/admin/plugins/privacy_image_cache', 'picsave');
169
170     if (isset($_REQUEST['save'])) {
171         $cachetime_h = IntVal($_REQUEST['cachetime']);
172         if ($cachetime_h < 1) $cachetime_h = 1;
173         set_config('privacy_image_cache','cache_time', $cachetime_h * 3600);
174     }
175     if (isset($_REQUEST['delete_all'])) {
176         q('DELETE FROM `photo` WHERE `uid` = 0 AND `resource-id` LIKE "pic:%%"');
177     }
178 }