1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
/*
Copyright (c) Ampache.org
All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License v2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
class Plugin {
/* Base Variables */
public $name;
/* constructed objects */
public $_plugin;
/**
* Constructor
* This constructor loads the Plugin config file which defines how to
* install/uninstall the plugin from Ampache's database
*/
public function __construct($name) {
/* Load the plugin */
if (!$this->_get_info($name)) {
return false;
}
return true;
} // Constructor
/**
* _get_info
* This actually loads the config file for the plugin the name of the
* class contained within the config file must be Plugin[NAME OF FILE]
*/
public function _get_info($name) {
/* Require the file we want */
require_once Config::get('prefix') . '/modules/plugins/' . $name . '.plugin.php';
$plugin_name = "Ampache$name";
$this->_plugin = new $plugin_name();
if (!$this->is_valid()) {
return false;
}
return true;
} // _get_info
/**
* get_plugins
* This returns an array of plugin names
*/
public static function get_plugins() {
$results = array();
// Open up the plugin dir
$handle = opendir(Config::get('prefix') . '/modules/plugins');
if (!is_resource($handle)) {
debug_event('Plugins','Unable to read plugins directory','1');
}
// Recurse the directory
while ($file = readdir($handle)) {
// Ignore non-plugin files
if (substr($file,-10,10) != 'plugin.php') { continue; }
if (is_dir($file)) { continue; }
// It's a plugin record it
$plugin_name = basename($file,'.plugin.php');
$results[$plugin_name] = $plugin_name;
} // end while
// Little stupid but hey
ksort($results);
return $results;
} // get_plugins
/**
* is_valid
* This checks to make sure the plugin has the required functions and
* settings, Ampache requires Name/Description/Version (Int) and a
* install & uninstall method and Ampache must be within the min/max
* version specifications
*/
function is_valid() {
/* Check the plugin to make sure it's got the needed vars */
if (!strlen($this->_plugin->name)) {
return false;
}
if (!strlen($this->_plugin->description)) {
return false;
}
if (!strlen($this->_plugin->version)) {
return false;
}
/* Make sure we've got the required methods */
if (!method_exists($this->_plugin,'install')) {
return false;
}
if (!method_exists($this->_plugin,'uninstall')) {
return false;
}
/* Make sure it's within the version confines */
$db_version = $this->get_ampache_db_version();
if ($db_version < $this->_plugin->min_ampache) {
return false;
}
if ($db_version > $this->_plugin->max_ampache) {
return false;
}
/* We've passed all of the tests its good */
return true;
} // is_valid
/**
* is_installed
* This checks to see if the specified plugin is currently installed in the
* database, it doesn't check the files for integrity
*/
public static function is_installed($plugin_name) {
/* All we do is check the version */
return self::get_plugin_version($plugin_name);
} // is_installed
/**
* install
* This runs the install function of the plugin (must be called install)
* at the end it inserts a row into the update_info table to indicate
* That it's installed
*/
public function install() {
$installed = $this->_plugin->install();
$version = $this->set_plugin_version($this->_plugin->version);
if (!$installed OR !$version) { return false; }
return true;
} // install
/**
* uninstall
* This runs the uninstall function of the plugin (must be called uninstall)
* at the end it removes the row from the update_info table to indicate
* that it isn't installed
*/
public function uninstall() {
$this->_plugin->uninstall();
$this->remove_plugin_version();
} // uninstall
/**
* get_plugin_version
* This returns the version of the specified plugin
*/
public static function get_plugin_version($plugin_name) {
$name = Dba::escape('Plugin_' . $plugin_name);
$sql = "SELECT * FROM `update_info` WHERE `key`='$name'";
$db_results = Dba::read($sql);
$results = Dba::fetch_assoc($db_results);
return $results['value'];
} // get_plugin_version
/**
* get_ampache_db_version
* This function returns the Ampache database version
*/
function get_ampache_db_version() {
$sql = "SELECT * FROM `update_info` WHERE `key`='db_version'";
$db_results = Dba::read($sql);
$results = Dba::fetch_assoc($db_results);
return $results['value'];
} // get_ampache_db_version
/**
* set_plugin_version
* This sets the plugin version in the update_info table
*/
public function set_plugin_version($version) {
$name = Dba::escape('Plugin_' . $this->_plugin->name);
$version = Dba::escape($version);
$sql = "INSERT INTO `update_info` SET `key`='$name', `value`='$version'";
$db_results = Dba::read($sql);
return true;
} // set_plugin_version
/**
* remove_plugin_version
* This removes the version row from the db done on uninstall
*/
public function remove_plugin_version() {
$name = Dba::escape('Plugin_' . $this->_plugin->name);
$sql = "DELETE FROM `update_info` WHERE `key`='$name'";
$db_results = Dba::read($sql);
return true;
} // remove_plugin_version
} //end plugin class
?>
|