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