Subversion Repositories Sites.tela-botanica.org

Rev

Rev 609 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
420 florian 1
<?php
2
# ***** BEGIN LICENSE BLOCK *****
3
# This file is part of DotClear.
4
# Copyright (c) 2004 Olivier Meunier and contributors. All rights
5
# reserved.
6
#
7
# DotClear is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
11
#
12
# DotClear is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with DotClear; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
#
21
# ***** END LICENSE BLOCK *****
22
 
23
/*
24
Classe de gestion des plugins et des thèmes
25
*/
26
 
27
class plugins
28
{
29
	var $location;
30
	var $type;
31
	var $_xml;
32
	var $p_list = array();
33
 
34
	function plugins($location,$type='plugin')
35
	{
36
		if (is_dir($location)) {
37
			$this->location = $location.'/';
38
		} else {
39
			$this->location = NULL;
40
		}
41
 
42
		$this->type = $type;
43
	}
44
 
45
	function getPlugins($active_only=true)
46
	{
47
		if (($list_files = $this->_readDir()) !== false)
48
		{
49
			$this->p_list = array();
50
			foreach ($list_files as $entry => $pfile)
51
			{
52
				if (($info = $this->_getPluginInfo($pfile)) !== false) {
53
					if (($active_only && $info['active']) || !$active_only) {
54
						$this->p_list[$entry] = $info;
55
					}
56
				}
57
			}
58
			ksort($this->p_list);
59
			return true;
60
		}
61
		else
62
		{
63
			return false;
64
		}
65
	}
66
 
67
	function getPluginsList()
68
	{
69
		return $this->p_list;
70
	}
71
 
72
	function getFunctions($f='functions.php')
73
	{
74
		$res = array();
75
 
76
		if (($list_files = $this->_readDir()) !== false)
77
		{
78
			foreach ($list_files as $entry => $pfile)
79
			{
80
				if (file_exists(dirname($pfile).'/'.$f)) {
81
					$res[] = dirname($pfile).'/'.$f;
82
				}
83
			}
84
		}
85
		return $res;
86
	}
87
 
88
	function loadCallbacks()
89
	{
90
		$res['onPost'] = array();
91
 
92
		$ires = array_keys($res);
93
 
94
		foreach ($this->p_list as $k => $v)
95
		{
96
			# Chargement des fichiers events.php
97
 
98
			if (file_exists($this->location.$k.'/events.php'))
99
			{
100
				require_once $this->location.$k.'/events.php';
101
 
102
				foreach ($v['callbacks'] as $f)
103
				{
104
					if (in_array($f[0],$ires))
105
					{
106
						$pf = explode('::',$f[1]);
107
						if (count($pf) == 2 && is_callable($pf)) {
108
							$res[$f[0]][] = $pf;
109
						}
110
					}
111
				}
112
			}
113
		}
114
 
115
		return $res;
116
	}
117
 
118
	function loadl10n($p)
119
	{
120
		if (defined('DC_LANG')) {
121
			if (dc_encoding == 'UTF-8') {
122
				l10n::set($this->location.$p.'/l10n/'.DC_LANG.'-utf8/main');
123
			} else {
124
				l10n::set($this->location.$p.'/l10n/'.DC_LANG.'/main');
125
			}
126
		}
127
	}
128
 
129
	function switchStatus($p)
130
	{
131
		$xml_path = $this->location.$p.'/desc.xml';
132
		$p_info = $this->_getPluginInfo($xml_path);
133
		$xml = implode('',file($xml_path));
134
 
135
		$active = (integer) !$p_info['active'];
136
 
137
		$xml = preg_replace('|(<'.$this->type.'[^>]*?active=)"([^"]+)([^>]*>)|ms',
138
			'$1"'.$active.'$3',$xml);
139
 
140
		if (!files::putContent($xml_path,$xml)) {
141
			return false;
142
		}
143
		return true;
144
	}
145
 
146
 
147
	/* Installation d'un plugin */
148
	function install($url)
149
	{
150
		$dest = $this->location.'/'.basename($url);
151
	    if  ((!file_exists($dest)) && ($err = files::copyRemote($url,$dest) !== true)) {
152
             return $err;
153
        }
154
		else
155
		{
156
			if (($content = @implode('',@gzfile($dest))) === false) {
157
				return __('Cannot open file');
158
			} else {
159
				if (($list = unserialize($content)) === false)
160
				{
161
					return __('Plugin not valid');
162
				}
163
				else
164
				{
165
					if (is_dir($this->location.'/'.$list['name']))
166
					{
167
						/*if (files::deltree($this->location.'/'.$list['name']) === false)
168
						{
169
							return 'Impossible de supprimer le plugin existant';
170
						}*/
171
						unlink($dest);
172
						return __('This plugin still exists. Delete it before.');
173
					}
174
 
175
					foreach ($list['dirs'] as $d)
176
					{
177
						mkdir ($this->location.'/'.$d,fileperms($this->location));
178
						chmod($this->location.'/'.$d,fileperms($this->location));
179
					}
180
 
181
					foreach ($list['files'] as $f => $v)
182
					{
183
						$v = base64_decode($v);
184
						$fp = fopen($this->location.'/'.$f,'w');
185
						fwrite($fp,$v,strlen($v));
186
						fclose($fp);
187
						chmod($this->location.'/'.$f,fileperms($this->location) & ~0111);
188
					}
189
 
190
					unlink($dest);
191
				}
192
			}
193
		}
194
		return true;
195
	}
196
 
197
	/* Lecture d'un répertoire à la recherche des desc.xml */
198
	function _readDir()
199
	{
200
		if ($this->location === NULL) {
201
			return false;
202
		}
203
 
204
		$res = array();
205
 
206
		$d = dir($this->location);
207
 
208
		# Liste du répertoire des plugins
209
		while (($entry = $d->read()) !== false)
210
		{
211
			if ($entry != '.' && $entry != '..' &&
212
			is_dir($this->location.$entry) && file_exists($this->location.$entry.'/desc.xml'))
213
			{
214
				$res[$entry] = $this->location.$entry.'/desc.xml';
215
			}
216
		}
217
 
218
		return $res;
219
	}
220
 
221
	function _getPluginInfo($p)
222
	{
223
		if (file_exists($p))
224
		{
225
			$this->_current_tag_cdata = '';
226
			$this->_p_info = array('name'=>NULL,'version'=>NULL,
227
						'active'=>NULL,'author'=>NULL,'label'=>NULL,
228
						'desc'=>NULL,'callbacks'=>array());
229
 
230
			$this->_xml = xml_parser_create('ISO-8859-1');
231
			xml_parser_set_option($this->_xml, XML_OPTION_CASE_FOLDING, false);
232
			xml_set_object($this->_xml, $this);
233
			xml_set_element_handler($this->_xml,'_openTag','_closeTag');
234
			xml_set_character_data_handler($this->_xml, '_cdata');
235
 
236
			xml_parse($this->_xml,implode('',file($p)));
237
			xml_parser_free($this->_xml);
238
 
239
			if (!empty($this->_p_info['name'])) {
240
				return $this->_p_info;
241
			} else {
242
				return false;
243
			}
244
		}
245
	}
246
 
247
	function _openTag($p,$tag,$attr)
248
	{
249
		if ($tag == $this->type && !empty($attr['name']))
250
		{
251
			$this->_p_info['name'] = $attr['name'];
252
			$this->_p_info['version'] = (!empty($attr['version'])) ? $attr['version'] : NULL;
253
			$this->_p_info['active'] = (!empty($attr['active'])) ? (boolean) $attr['active'] : false;
254
		}
255
 
256
		if ($tag == 'callback') {
257
			$this->_p_info['callbacks'][] = array($attr['event'],$attr['function']);
258
		}
259
	}
260
 
261
	function _closeTag($p,$tag)
262
	{
263
		switch ($tag)
264
		{
265
			case 'author':
266
			case 'label':
267
			case 'desc':
268
				$this->_p_info[$tag] = $this->_current_tag_cdata;
269
				break;
270
		}
271
	}
272
 
273
	function _cdata($p,$cdata)
274
	{
275
		$this->_current_tag_cdata = $cdata;
276
	}
277
}
278
 
279
?>