]> git.mxchange.org Git - friendica.git/blob - include/plugin.php
add remove_user hook (it looks like dreamhost changed all my file permissions, this...
[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         logger("Addons: installing " . $plugin);
22         $t = @filemtime('addon/' . $plugin . '/' . $plugin . '.php');
23         @include_once('addon/' . $plugin . '/' . $plugin . '.php');
24         if(function_exists($plugin . '_install')) {
25                 $func = $plugin . '_install';
26                 $func();
27                 
28                 $plugin_admin = (function_exists($plugin."_plugin_admin")?1:0);
29                 
30                 $r = q("INSERT INTO `addon` (`name`, `installed`, `timestamp`, `plugin_admin`) VALUES ( '%s', 1, %d , %d ) ",
31                         dbesc($plugin),
32                         intval($t),
33                         $plugin_admin
34                 );
35         }
36         else {
37                 logger("Addons: FAILED installing " . $plugin);
38         }
39
40 }}
41
42 // reload all updated plugins
43
44 if(! function_exists('reload_plugins')) {
45 function reload_plugins() {
46         $plugins = get_config('system','addon');
47         if(strlen($plugins)) {
48
49                 $r = q("SELECT * FROM `addon` WHERE `installed` = 1");
50                 if(count($r))
51                         $installed = $r;
52                 else
53                         $installed = array();
54
55                 $parr = explode(',',$plugins);
56                 if(count($parr)) {
57                         foreach($parr as $pl) {
58                                 $pl = trim($pl);
59                                 
60                                 $t = filemtime('addon/' . $pl . '/' . $pl . '.php');
61                                 foreach($installed as $i) {
62                                         if(($i['name'] == $pl) && ($i['timestamp'] != $t)) {    
63                                                 logger('Reloading plugin: ' . $i['name']);
64                                                 @include_once('addon/' . $pl . '/' . $pl . '.php');
65
66                                                 if(function_exists($pl . '_uninstall')) {
67                                                         $func = $pl . '_uninstall';
68                                                         $func();
69                                                 }
70                                                 if(function_exists($pl . '_install')) {
71                                                         $func = $pl . '_install';
72                                                         $func();
73                                                 }
74                                                 q("UPDATE `addon` SET `timestamp` = %d WHERE `id` = %d LIMIT 1",
75                                                         intval($t),
76                                                         intval($i['id'])
77                                                 );
78                                         }
79                                 }
80                         }
81                 }
82         }
83 }}
84                                 
85
86
87
88
89 if(! function_exists('register_hook')) {
90 function register_hook($hook,$file,$function) {
91
92         $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
93                 dbesc($hook),
94                 dbesc($file),
95                 dbesc($function)
96         );
97         if(count($r))
98                 return true;
99
100         $r = q("INSERT INTO `hook` (`hook`, `file`, `function`) VALUES ( '%s', '%s', '%s' ) ",
101                 dbesc($hook),
102                 dbesc($file),
103                 dbesc($function)
104         );
105         return $r;
106 }}
107
108 if(! function_exists('unregister_hook')) {
109 function unregister_hook($hook,$file,$function) {
110
111         $r = q("DELETE FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' LIMIT 1",
112                 dbesc($hook),
113                 dbesc($file),
114                 dbesc($function)
115         );
116         return $r;
117 }}
118
119
120 if(! function_exists('load_hooks')) {
121 function load_hooks() {
122         $a = get_app();
123         $a->hooks = array();
124         $r = q("SELECT * FROM `hook` WHERE 1");
125         if(count($r)) {
126                 foreach($r as $rr) {
127                         $a->hooks[] = array($rr['hook'], $rr['file'], $rr['function']);
128                 }
129         }
130 }}
131
132
133 if(! function_exists('call_hooks')) {
134 function call_hooks($name, &$data = null) {
135         $a = get_app();
136
137         if(count($a->hooks)) {
138                 foreach($a->hooks as $hook) {
139                         if($hook[HOOK_HOOK] === $name) {
140                                 @include_once($hook[HOOK_FILE]);
141                                 if(function_exists($hook[HOOK_FUNCTION])) {
142                                         $func = $hook[HOOK_FUNCTION];
143                                         $func($a,$data);
144                                 }
145                         }
146                 }
147         }
148 }}
149
150
151 /*
152  * parse plugin comment in search of plugin infos.
153  * like
154  *      
155  *       * Name: Plugin
156  *   * Description: A plugin which plugs in
157  *       * Version: 1.2.3
158  *   * Author: John <profile url>
159  *   * Author: Jane <email>
160  *   *
161  */
162
163 if (! function_exists('get_plugin_info')){
164 function get_plugin_info($plugin){
165         if (!is_file("addon/$plugin/$plugin.php")) return false;
166         
167         $f = file_get_contents("addon/$plugin/$plugin.php");
168         $r = preg_match("|/\*.*\*/|msU", $f, $m);
169         
170         $info=Array(
171                 'name' => $plugin,
172                 'description' => "",
173                 'author' => array(),
174                 'version' => ""
175         );
176         
177         if ($r){
178                 $ll = explode("\n", $m[0]);
179                 foreach( $ll as $l ) {
180                         $l = trim($l,"\t\n\r */");
181                         if ($l!=""){
182                                 list($k,$v) = array_map("trim", explode(":",$l,2));
183                                 $k= strtolower($k);
184                                 if ($k=="author"){
185                                         $r=preg_match("|([^<]+)<([^>]+)>|", $v, $m);
186                                         if ($r) {
187                                                 $info['author'][] = array('name'=>$m[1], 'link'=>$m[2]);
188                                         } else {
189                                                 $info['author'][] = array('name'=>$v);
190                                         }
191                                 } else {
192                                         if (array_key_exists($k,$info)){
193                                                 $info[$k]=$v;
194                                         }
195                                 }
196                                 
197                         }
198                 }
199                 
200         }
201         return $info;
202 }}
203