]> git.mxchange.org Git - friendica.git/blob - include/plugin.php
Merge remote branch 'upstream/master'
[friendica.git] / include / plugin.php
1 <?php
2
3
4 // install and uninstall plugin
5 if (! function_exists('uninstall_plugin')){
6 function uninstall_plugin($plugin){
7         logger("Addons: uninstalling " . $plugin);
8         q("DELETE FROM `addon` WHERE `name` = '%s' ",
9                 dbesc($plugin)
10         );
11
12         @include_once('addon/' . $plugin . '/' . $plugin . '.php');
13         if(function_exists($plugin . '_uninstall')) {
14                 $func = $plugin . '_uninstall';
15                 $func();
16         }
17 }}
18
19 if (! function_exists('install_plugin')){
20 function install_plugin($plugin) {
21
22         // silently fail if plugin was removed
23
24         if(! file_exists('addon/' . $plugin . '/' . $plugin . '.php'))
25                 return false;
26         logger("Addons: installing " . $plugin);
27         $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
28         @include_once('addon/' . $plugin . '/' . $plugin . '.php');
29         if(function_exists($plugin . '_install')) {
30                 $func = $plugin . '_install';
31                 $func();
32                 
33                 $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0);
34                 
35                 $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
36                         dbesc($plugin),
37                         intval($t),
38                         $plugin_admin
39                 );
40
41                 // we can add the following with the previous SQL
42                 // once most site tables have been updated.
43                 // This way the system won't fall over dead during the update.
44
45                 if(file_exists('addon/' . $plugin . '/.hidden')) {
46                         q("update addon set hidden = 1 where name = '%s' limit 1",
47                                 dbesc($plugin)
48                         );
49                 }
50                 return true;
51         }
52         else {
53                 logger("Addons: FAILED installing " . $plugin);
54                 return false;
55         }
56
57 }}
58
59 // reload all updated plugins
60
61 if(! function_exists('reload_plugins')) {
62 function reload_plugins() {
63         $plugins = get_config('system','addon');
64         if(strlen($plugins)) {
65
66                 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
67                 if(count($r))
68                         $installed = $r;
69                 else
70                         $installed = array();
71
72                 $parr = explode(',',$plugins);
73                 if(count($parr)) {
74                         foreach($parr as $pl) {
75                                 $pl = trim($pl);
76
77                                 $fname = 'addon/' . $pl . '/' . $pl . '.php';
78                                 
79                                 if(file_exists($fname)) {
80                                         $t = @filemtime($fname);
81                                         foreach($installed as $i) {
82                                                 if(($i['name'] == $pl) && ($i['timestamp'] != $t)) {    
83                                                         logger('Reloading plugin: ' . $i['name']);
84                                                         @include_once($fname);
85
86                                                         if(function_exists($pl . '_uninstall')) {
87                                                                 $func = $pl . '_uninstall';
88                                                                 $func();
89                                                         }
90                                                         if(function_exists($pl . '_install')) {
91                                                                 $func = $pl . '_install';
92                                                                 $func();
93                                                         }
94                                                         q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d LIMIT 1",
95                                                                 intval($t),
96                                                                 intval($i['id'])
97                                                         );
98                                                 }
99                                         }
100                                 }
101                         }
102                 }
103         }
104 }}
105                                 
106
107
108
109
110 if(! function_exists('register_hook')) {
111 function register_hook($hook,$file,$function) {
112
113         $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
114                 dbesc($hook),
115                 dbesc($file),
116                 dbesc($function)
117         );
118         if(count($r))
119                 return true;
120
121         $r = q("INSERT INTO `hook` (`hook`, `file`, `function`) VALUES ( '%s', '%s', '%s' ) ",
122                 dbesc($hook),
123                 dbesc($file),
124                 dbesc($function)
125         );
126         return $r;
127 }}
128
129 if(! function_exists('unregister_hook')) {
130 function unregister_hook($hook,$file,$function) {
131
132         $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
133                 dbesc($hook),
134                 dbesc($file),
135                 dbesc($function)
136         );
137         return $r;
138 }}
139
140
141 if(! function_exists('load_hooks')) {
142 function load_hooks() {
143         $a = get_app();
144         $a->hooks = array();
145         $r = q("SELECT * FROM `hook` WHERE 1");
146         if(count($r)) {
147                 foreach($r as $rr) {
148                         $a->hooks[] = array($rr['hook'], $rr['file'], $rr['function']);
149                 }
150         }
151 }}
152
153
154 if(! function_exists('call_hooks')) {
155 function call_hooks($name, &$data = null) {
156         $a = get_app();
157
158         if(count($a->hooks)) {
159                 foreach($a->hooks as $hook) {
160                         if($hook[HOOK_HOOK] === $name) {
161                                 @include_once($hook[HOOK_FILE]);
162                                 if(function_exists($hook[HOOK_FUNCTION])) {
163                                         $func = $hook[HOOK_FUNCTION];
164                                         $func($a,$data);
165                                 }
166                         }
167                 }
168         }
169 }}
170
171
172 /*
173  * parse plugin comment in search of plugin infos.
174  * like
175  *      
176  *       * Name: Plugin
177  *   * Description: A plugin which plugs in
178  *       * Version: 1.2.3
179  *   * Author: John <profile url>
180  *   * Author: Jane <email>
181  *   *
182  */
183
184 if (! function_exists('get_plugin_info')){
185 function get_plugin_info($plugin){
186         $info=Array(
187                 'name' => $plugin,
188                 'description' => "",
189                 'author' => array(),
190                 'version' => ""
191         );
192         
193         if (!is_file("addon/$plugin/$plugin.php")) return $info;
194         
195         $f = file_get_contents("addon/$plugin/$plugin.php");
196         $r = preg_match("|/\*.*\*/|msU", $f, $m);
197         
198         if ($r){
199                 $ll = explode("\n", $m[0]);
200                 foreach( $ll as $l ) {
201                         $l = trim($l,"\t\n\r */");
202                         if ($l!=""){
203                                 list($k,$v) = array_map("trim", explode(":",$l,2));
204                                 $k= strtolower($k);
205                                 if ($k=="author"){
206                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
207                                         if ($r) {
208                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
209                                         } else {
210                                                 $info['author'][] = array('name'=>$v);
211                                         }
212                                 } else {
213                                         if (array_key_exists($k,$info)){
214                                                 $info[$k]=$v;
215                                         }
216                                 }
217                                 
218                         }
219                 }
220                 
221         }
222         return $info;
223 }}
224
225
226 /*
227  * parse theme comment in search of theme infos.
228  * like
229  *      
230  *       * Name: My Theme
231  *   * Description: My Cool Theme
232  *       * Version: 1.2.3
233  *   * Author: John <profile url>
234  *   * Maintainer: Jane <profile url>
235  *   *
236  */
237
238 if (! function_exists('get_theme_info')){
239 function get_theme_info($theme){
240         $info=Array(
241                 'name' => $theme,
242                 'description' => "",
243                 'author' => array(),
244                 'maintainer' => array(),
245                 'version' => "",
246                 'experimental' => false,
247                 'unsupported' => false
248         );
249
250         if(file_exists("view/theme/$theme/experimental"))
251                 $info['experimental'] = true;
252         if(file_exists("view/theme/$theme/unsupported"))
253                 $info['unsupported'] = true;
254
255         if (!is_file("view/theme/$theme/theme.php")) return $info;
256         
257         $f = file_get_contents("view/theme/$theme/theme.php");
258         $r = preg_match("|/\*.*\*/|msU", $f, $m);
259         
260         
261         if ($r){
262                 $ll = explode("\n", $m[0]);
263                 foreach( $ll as $l ) {
264                         $l = trim($l,"\t\n\r */");
265                         if ($l!=""){
266                                 list($k,$v) = array_map("trim", explode(":",$l,2));
267                                 $k= strtolower($k);
268                                 if ($k=="author"){
269
270                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
271                                         if ($r) {
272                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
273                                         } else {
274                                                 $info['author'][] = array('name'=>$v);
275                                         }
276                                 }
277                                 elseif ($k=="maintainer"){
278                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
279                                         if ($r) {
280                                                 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
281                                         } else {
282                                                 $info['maintainer'][] = array('name'=>$v);
283                                         }
284                                 } else {
285                                         if (array_key_exists($k,$info)){
286                                                 $info[$k]=$v;
287                                         }
288                                 }
289                                 
290                         }
291                 }
292                 
293         }
294         return $info;
295 }}
296
297
298 function get_theme_screenshot($theme) {
299         $a = get_app();
300         $exts = array('.png','.jpg');
301         foreach($exts as $ext) {
302                 if(file_exists('view/theme/' . $theme . '/screenshot' . $ext))
303                         return($a->get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
304         }
305         return($a->get_baseurl() . '/images/blank.png');
306 }