]> git.mxchange.org Git - friendica.git/blob - include/plugin.php
missing $submit
[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
74                 if(count($parr)) {
75                         foreach($parr as $pl) {
76
77                                 $pl = trim($pl);
78
79                                 $fname = 'addon/' . $pl . '/' . $pl . '.php';
80                                 
81                                 if(file_exists($fname)) {
82                                         $t = @filemtime($fname);
83                                         foreach($installed as $i) {
84                                                 if(($i['name'] == $pl) && ($i['timestamp'] != $t)) {    
85                                                         logger('Reloading plugin: ' . $i['name']);
86                                                         @include_once($fname);
87
88                                                         if(function_exists($pl . '_uninstall')) {
89                                                                 $func = $pl . '_uninstall';
90                                                                 $func();
91                                                         }
92                                                         if(function_exists($pl . '_install')) {
93                                                                 $func = $pl . '_install';
94                                                                 $func();
95                                                         }
96                                                         q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d LIMIT 1",
97                                                                 intval($t),
98                                                                 intval($i['id'])
99                                                         );
100                                                 }
101                                         }
102                                 }
103                         }
104                 }
105         }
106
107 }}
108                                 
109
110
111
112
113 if(! function_exists('register_hook')) {
114 function register_hook($hook,$file,$function) {
115
116         $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
117                 dbesc($hook),
118                 dbesc($file),
119                 dbesc($function)
120         );
121         if(count($r))
122                 return true;
123
124         $r = q("INSERT INTO `hook` (`hook`, `file`, `function`) VALUES ( '%s', '%s', '%s' ) ",
125                 dbesc($hook),
126                 dbesc($file),
127                 dbesc($function)
128         );
129         return $r;
130 }}
131
132 if(! function_exists('unregister_hook')) {
133 function unregister_hook($hook,$file,$function) {
134
135         $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
136                 dbesc($hook),
137                 dbesc($file),
138                 dbesc($function)
139         );
140         return $r;
141 }}
142
143
144 if(! function_exists('load_hooks')) {
145 function load_hooks() {
146         $a = get_app();
147         $a->hooks = array();
148         $r = q("SELECT * FROM `hook` WHERE 1");
149         if(count($r)) {
150                 foreach($r as $rr) {
151                         $a->hooks[] = array($rr['hook'], $rr['file'], $rr['function']);
152                 }
153         }
154 }}
155
156
157 if(! function_exists('call_hooks')) {
158 function call_hooks($name, &$data = null) {
159         $a = get_app();
160
161         if(count($a->hooks)) {
162                 foreach($a->hooks as $hook) {
163                         if($hook[HOOK_HOOK] === $name) {
164                                 @include_once($hook[HOOK_FILE]);
165                                 if(function_exists($hook[HOOK_FUNCTION])) {
166                                         $func = $hook[HOOK_FUNCTION];
167                                         $func($a,$data);
168                                 }
169                                 else {
170                                         // remove orphan hooks
171                                         q("delete from hook where hook = '%s' and file = '$s' and function = '%s' limit 1",
172                                                 dbesc($hook[HOOK_HOOK]),
173                                                 dbesc($hook[HOOK_FILE]),
174                                                 dbesc($hook[HOOK_FUNCTION])
175                                         );
176                                 }
177                         }
178                 }
179         }
180 }}
181
182
183 /*
184  * parse plugin comment in search of plugin infos.
185  * like
186  *      
187  *       * Name: Plugin
188  *   * Description: A plugin which plugs in
189  *       * Version: 1.2.3
190  *   * Author: John <profile url>
191  *   * Author: Jane <email>
192  *   *
193  */
194
195 if (! function_exists('get_plugin_info')){
196 function get_plugin_info($plugin){
197         $info=Array(
198                 'name' => $plugin,
199                 'description' => "",
200                 'author' => array(),
201                 'version' => ""
202         );
203         
204         if (!is_file("addon/$plugin/$plugin.php")) return $info;
205         
206         $f = file_get_contents("addon/$plugin/$plugin.php");
207         $r = preg_match("|/\*.*\*/|msU", $f, $m);
208         
209         if ($r){
210                 $ll = explode("\n", $m[0]);
211                 foreach( $ll as $l ) {
212                         $l = trim($l,"\t\n\r */");
213                         if ($l!=""){
214                                 list($k,$v) = array_map("trim", explode(":",$l,2));
215                                 $k= strtolower($k);
216                                 if ($k=="author"){
217                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
218                                         if ($r) {
219                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
220                                         } else {
221                                                 $info['author'][] = array('name'=>$v);
222                                         }
223                                 } else {
224                                         if (array_key_exists($k,$info)){
225                                                 $info[$k]=$v;
226                                         }
227                                 }
228                                 
229                         }
230                 }
231                 
232         }
233         return $info;
234 }}
235
236
237 /*
238  * parse theme comment in search of theme infos.
239  * like
240  *      
241  *       * Name: My Theme
242  *   * Description: My Cool Theme
243  *       * Version: 1.2.3
244  *   * Author: John <profile url>
245  *   * Maintainer: Jane <profile url>
246  *   *
247  */
248
249 if (! function_exists('get_theme_info')){
250 function get_theme_info($theme){
251         $info=Array(
252                 'name' => $theme,
253                 'description' => "",
254                 'author' => array(),
255                 'maintainer' => array(),
256                 'version' => "",
257                 'experimental' => false,
258                 'unsupported' => false
259         );
260
261         if(file_exists("view/theme/$theme/experimental"))
262                 $info['experimental'] = true;
263         if(file_exists("view/theme/$theme/unsupported"))
264                 $info['unsupported'] = true;
265
266         if (!is_file("view/theme/$theme/theme.php")) return $info;
267         
268         $f = file_get_contents("view/theme/$theme/theme.php");
269         $r = preg_match("|/\*.*\*/|msU", $f, $m);
270         
271         
272         if ($r){
273                 $ll = explode("\n", $m[0]);
274                 foreach( $ll as $l ) {
275                         $l = trim($l,"\t\n\r */");
276                         if ($l!=""){
277                                 list($k,$v) = array_map("trim", explode(":",$l,2));
278                                 $k= strtolower($k);
279                                 if ($k=="author"){
280
281                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
282                                         if ($r) {
283                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
284                                         } else {
285                                                 $info['author'][] = array('name'=>$v);
286                                         }
287                                 }
288                                 elseif ($k=="maintainer"){
289                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
290                                         if ($r) {
291                                                 $info['maintainer'][] = array('name'=>$m[1], 'link'=>$m[2]);
292                                         } else {
293                                                 $info['maintainer'][] = array('name'=>$v);
294                                         }
295                                 } else {
296                                         if (array_key_exists($k,$info)){
297                                                 $info[$k]=$v;
298                                         }
299                                 }
300                                 
301                         }
302                 }
303                 
304         }
305         return $info;
306 }}
307
308
309 function get_theme_screenshot($theme) {
310         $a = get_app();
311         $exts = array('.png','.jpg');
312         foreach($exts as $ext) {
313                 if(file_exists('view/theme/' . $theme . '/screenshot' . $ext))
314                         return($a->get_baseurl() . '/view/theme/' . $theme . '/screenshot' . $ext);
315         }
316         return($a->get_baseurl() . '/images/blank.png');
317 }