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