summaryrefslogtreecommitdiffstats
path: root/inc/lib_plugin.php
blob: c846fe25603857cbcc2832c2b8dcf4d51394f768 (plain)
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
<?php

/**
* ownCloud
*
* @author Frank Karlitschek 
* @copyright 2010 Frank Karlitschek karlitschek@kde.org 
* 
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either 
* version 3 of the License, or any later version.
* 
* This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
*  
* You should have received a copy of the GNU Lesser General Public 
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

class OC_PLUGIN{
	static private $blacklist=array();
	
	/**
	* load the plugin with the given id
	* @param string id
	* @return bool
	*/
	static public function load($id){
		global $SERVERROOT;
		$data=self::getPluginData($id);
		if($data){
			if(isset($data['info']['require'])){
				$minVersion=explode('.',$data['info']['require']);
				$version=OC_UTIL::getVersion();
				$roundTo=count($minVersion);
				while(count($version)>$roundTo){
					if($version[count($version)-1]>=50){
						$version[count($version)-2]++;
					}
					unset($version[count($version)-1]);
				}
				for($i=0;$i<count($minVersion);$i++){
					if($version[$i]<$minVersion[$i]){
						return false;
					}
				}
			}
			foreach($data['runtime'] as $include){
				include($SERVERROOT.'/plugins/'.$id.'/'.$include);
			}
		}
		return false;
	}
	
	/**
	 * Load all plugins that aren't blacklisted
	 */
	public static function loadPlugins() {
		global $SERVERROOT;
		$plugins = array();
		$blacklist=self::loadBlacklist();
		$fd = opendir($SERVERROOT . '/plugins');
		while ( false !== ($filename = readdir($fd)) ) {
			if ( $filename<>'.' AND $filename<>'..' AND ('.' != substr($filename, 0, 1)) AND array_search($filename,$blacklist)===false) {
				self::load($filename);
			}
		}
		closedir($fd);
	}
	
	/**
	* load the blacklist from blacklist.txt
	* @return array
	*/
	private static function loadBlacklist(){
		global $SERVERROOT;
		if(count(self::$blacklist)>0){
			return self::$blacklist;
		}
		$blacklist=array();
		if(is_file($SERVERROOT.'/plugins/blacklist.txt')){
			$file=file_get_contents($SERVERROOT.'/plugins/blacklist.txt');
			$lines=explode("\n",$file);
			foreach($lines as $line){
				$id=trim($line);
				if($id!='' and is_dir($SERVERROOT.'/plugins/'.$id)){
					$blacklist[]=$id;
				}
			}
		}
		self::$blacklist=$blacklist;
		return $blacklist;
	}
	
	/**
	* save a blacklist to blacklist.txt
	* @param array blacklist
	*/
	private static function saveBlacklist($blacklist){
		global $SERVERROOT;
		$file='';
		foreach($blacklist as $item){
			$file.="$item\n";
		}
		self::$blacklist=$blacklist;
		file_put_contents($SERVERROOT.'/plugins/blacklist.txt',$file);
	}
	
	/**
	* add a plugin to the blacklist
	* @param string id
	*/
	public static function addToBlacklist($id){
		$blacklist=self::loadBlacklist();
		if(array_search($id,$blacklist)===false){
			$blacklist[]=$id;
			self::$blacklist=$blacklist;
			self::saveBlacklist($blacklist);
		}
	}
	
	/**
	* remove a plugin to the blacklist
	* @param string id
	*/
	public static function removeFromBlacklist($id){
		$blacklist=self::loadBlacklist();
		$index=array_search($id,$blacklist);
		if($index!==false){
			unset($blacklist[$index]);
			self::$blacklist=$blacklist;
			self::saveBlacklist($blacklist);
		}
	}
	
	/**
	( Load data from the plugin.xml of a plugin, either identified by the plugin or the path of the plugin.xml file
	* @param string id
	*( @return array
	*/
	public static function getPluginData($id){
		global $SERVERROOT;
		if(is_file($id)){
			$file=$id;
		}
		if(!is_dir($SERVERROOT.'/plugins/'.$id) or !is_file($SERVERROOT.'/plugins/'.$id.'/plugin.xml')){
			return false;
		}else{
			$file=$SERVERROOT.'/plugins/'.$id.'/plugin.xml';
		}
		$data=array();
		$plugin=new DOMDocument();
		$plugin->load($file);
		$data['version']=$plugin->documentElement->getAttribute('version');
		$info=$plugin->getElementsByTagName('info');
		if($info->length>0){
			$info=$info->item(0);
			$data['info']=array();
			foreach($info->childNodes as $child){
				if($child->nodeType==XML_ELEMENT_NODE){
					$data['info'][$child->tagName]=$child->textContent;
				}
			}
		}
		$runtime=$plugin->getElementsByTagName('runtime');
		if($runtime->length>0){
			$runtime=$runtime->item(0);
			$data['runtime']=array();
			foreach($runtime->childNodes as $child){
				if($child->nodeType==XML_ELEMENT_NODE and $child->tagName=='include'){
					$data['runtime'][]=$child->textContent;
				}
			}
		}
		$install=$plugin->getElementsByTagName('install');
		if($install->length>0){
			$install=$install->item(0);
			$data['install']=array();
			foreach($install->childNodes as $child){
				if($child->nodeType==XML_ELEMENT_NODE){
					$data['install']['include']=array();
					$data['install']['dialog']=array();
					$data['install']['database']=array();
					switch($child->tagName){
						case 'include':
							$data['install']['include'][]=$child->textContent;
							break;
						case 'dialog':
							$data['install']['dialog'][]=$child->textContent;
							break;
						case 'database':
							$data['install']['database'][]=$child->textContent;
							break;
					}
				}
			}
		}
		$uninstall=$plugin->getElementsByTagName('uninstall');
		if($uninstall->length>0){
			$uninstall=$uninstall->item(0);
			$data['uninstall']=array();
			foreach($uninstall->childNodes as $child){
				if($child->nodeType==XML_ELEMENT_NODE){
					$data['uninstall']['include']=array();
					$data['uninstall']['dialog']=array();
					switch($child->tagName){
						case 'include':
							$data['uninstall']['include'][]=$child->textContent;
							break;
						case 'dialog':
							$data['uninstall']['dialog'][]=$child->textContent;
							break;
					}
				}
			}
		}
		return $data;
	}
}

?>