File: class.dc.modules.php

package info (click to toggle)
dotclear 2.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,420 kB
  • sloc: php: 54,270; sql: 1,290; sh: 213; xml: 173; makefile: 158
file content (611 lines) | stat: -rw-r--r-- 14,800 bytes parent folder | download
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------
if (!defined('DC_RC_PATH')) { return; }

/**
@ingroup DC_CORE
@brief Modules handler

Provides an object to handle modules (themes or plugins).
*/
class dcModules
{
	protected $path;
	protected $ns;
	protected $modules = array();
	protected $disabled = array();
	protected $errors = array();
	protected $modules_names = array();

	protected $id;
	protected $mroot;

	# Inclusion variables
	protected static $superglobals = array('GLOBALS','_SERVER','_GET','_POST','_COOKIE','_FILES','_ENV','_REQUEST','_SESSION');
	protected static $_k;
	protected static $_n;

	protected static $type = null;

	public $core;	///< <b>dcCore</b>	dcCore instance

	/**
	Object constructor.

	@param	core		<b>dcCore</b>	dcCore instance
	*/
	public function __construct($core)
	{
		$this->core =& $core;
	}

	/**
	Loads modules. <var>$path</var> could be a separated list of paths
	(path separator depends on your OS).

	<var>$ns</var> indicates if an additionnal file needs to be loaded on plugin
	load, value could be:
	- admin (loads module's _admin.php)
	- public (loads module's _public.php)
	- xmlrpc (loads module's _xmlrpc.php)

	<var>$lang</var> indicates if we need to load a lang file on plugin
	loading.
	*/
	public function loadModules($path,$ns=null,$lang=null)
	{
		$this->path = explode(PATH_SEPARATOR,$path);
		$this->ns = $ns;

		$disabled = isset($_SESSION['sess_safe_mode']) && $_SESSION['sess_safe_mode'];
		$disabled = $disabled && !get_parent_class($this) ? true : false;

		foreach ($this->path as $root)
		{
			if (!is_dir($root) || !is_readable($root)) {
				continue;
			}

			if (substr($root,-1) != '/') {
				$root .= '/';
			}

			if (($d = @dir($root)) === false) {
				continue;
			}

			while (($entry = $d->read()) !== false)
			{
				$full_entry = $root.$entry;

				if ($entry != '.' && $entry != '..' && is_dir($full_entry)
				&& file_exists($full_entry.'/_define.php'))
				{
					if (!file_exists($full_entry.'/_disabled') && !$disabled)
					{
						$this->id = $entry;
						$this->mroot = $full_entry;
						require $full_entry.'/_define.php';
						$this->id = null;
						$this->mroot = null;
					}
					else
					{
						$this->disabled[$entry] = array(
							'root' => $full_entry,
							'root_writable' => is_writable($full_entry)
						);
					}
				}
			}
			$d->close();
		}

		# Sort plugins
		uasort($this->modules,array($this,'sortModules'));

		# Load translation, _prepend and ns_file
		foreach ($this->modules as $id => $m)
		{
			if (file_exists($m['root'].'/_prepend.php'))
			{
				$r = $this->loadModuleFile($m['root'].'/_prepend.php');

				# If _prepend.php file returns null (ie. it has a void return statement)
				if (is_null($r)) {
					continue;
				}
				unset($r);
			}

			$this->loadModuleL10N($id,$lang,'main');
			if ($ns == 'admin') {
				$this->loadModuleL10Nresources($id,$lang);
			}
			$this->loadNsFile($id,$ns);
		}
	}

	public function requireDefine($dir,$id)
	{
		if (file_exists($dir.'/_define.php')) {
			$this->id = $id;
			require $dir.'/_define.php';
			$this->id = null;
		}
	}

	/**
	This method registers a module in modules list. You should use this to
	register a new module.

	<var>$permissions</var> is a comma separated list of permissions for your
	module. If <var>$permissions</var> is null, only super admin has access to
	this module.

	<var>$priority</var> is an integer. Modules are sorted by priority and name.
	Lowest priority comes first.

	@param	name			<b>string</b>		Module name
	@param	desc			<b>string</b>		Module description
	@param	author		<b>string</b>		Module author name
	@param	version		<b>string</b>		Module version
	@param	properties	<b>array</b>		extra properties
	(currently available keys : permissions, priority, type)
	*/
	public function registerModule($name,$desc,$author,$version, $properties = array())
	{
		# Fallback to legacy registerModule parameters
		if (!is_array($properties)) {
			$args = func_get_args();
			$properties = array();
			if (isset($args[4])) {
				$properties['permissions']=$args[4];
			}
			if (isset($args[5])) {
				$properties['priority']= (integer)$args[5];
			}
		}

		# Default module properties
		$properties = array_merge(
			array(
				'permissions' => null,
				'priority' => 1000,
				'standalone_config' => false,
				'type' => null
			), $properties
		);

		# Check module type
		if (self::$type !== null && $properties['type'] !== null && $properties['type'] != self::$type) {
			$this->errors[] = sprintf(
				__('Module "%s" has type "%s" that mismatch required module type "%s".'),
				'<strong>'.html::escapeHTML($name).'</strong>',
				'<em>'.html::escapeHTML($properties['type']).'</em>',
				'<em>'.html::escapeHTML(self::$type).'</em>'
			);
			return;
		}

		# Check module perms on admin side
		$permissions = $properties['permissions'];
		if ($this->ns == 'admin') {
			if ($permissions == '' && !$this->core->auth->isSuperAdmin()) {
				return;
			} elseif (!$this->core->auth->check($permissions,$this->core->blog->id)) {
				return;
			}
		}

		# Check module install on multiple path
		if ($this->id) {
			$module_exists = array_key_exists($name,$this->modules_names);
			$module_overwrite = $module_exists ? version_compare($this->modules_names[$name],$version,'<') : false;
			if (!$module_exists || ($module_exists && $module_overwrite)) {
				$this->modules_names[$name] = $version;
				$this->modules[$this->id] = array_merge(
					$properties,
					array(
						'root' => $this->mroot,
						'name' => $name,
						'desc' => $desc,
						'author' => $author,
						'version' => $version,
						'root_writable' => is_writable($this->mroot)
					)
				);
			}
			else {
				$path1 = path::real($this->moduleInfo($name,'root'));
				$path2 = path::real($this->mroot);
				$this->errors[] = sprintf(
					__('Module "%s" is installed twice in "%s" and "%s".'),
					'<strong>'.$name.'</strong>',
					'<em>'.$path1.'</em>',
					'<em>'.$path2.'</em>'
				);
			}
		}
	}

	public function resetModulesList()
	{
		$this->modules = array();
		$this->modules_names = array();
		$this->errors = array();
	}

	public static function installPackage($zip_file,dcModules &$modules)
	{
		$zip = new fileUnzip($zip_file);
		$zip->getList(false,'#(^|/)(__MACOSX|\.svn|\.hg|\.git|\.DS_Store|\.directory|Thumbs\.db)(/|$)#');

		$zip_root_dir = $zip->getRootDir();
		$define = '';
		if ($zip_root_dir != false) {
			$target = dirname($zip_file);
			$destination = $target.'/'.$zip_root_dir;
			$define = $zip_root_dir.'/_define.php';
			$has_define = $zip->hasFile($define);
		} else {
			$target = dirname($zip_file).'/'.preg_replace('/\.([^.]+)$/','',basename($zip_file));
			$destination = $target;
			$define = '_define.php';
			$has_define = $zip->hasFile($define);
		}

		if ($zip->isEmpty()) {
			$zip->close();
			unlink($zip_file);
			throw new Exception(__('Empty module zip file.'));
		}

		if (!$has_define) {
			$zip->close();
			unlink($zip_file);
			throw new Exception(__('The zip file does not appear to be a valid Dotclear module.'));
		}

		$ret_code = 1;

		if (!is_dir($destination))
		{
			try {
				files::makeDir($destination,true);

				$sandbox = clone $modules;
				$zip->unzip($define, $target.'/_define.php');

				$sandbox->resetModulesList();
				$sandbox->requireDefine($target,basename($destination));
				unlink($target.'/_define.php');

				$new_errors = $sandbox->getErrors();
				if (!empty($new_errors)) {
					$new_errors = is_array($new_errors) ? implode(" \n",$new_errors) : $new_errors;
					throw new Exception($new_errors);
				}

				files::deltree($destination);
			}
			catch(Exception $e)
			{
				$zip->close();
				unlink($zip_file);
				files::deltree($destination);
				throw new Exception($e->getMessage());
			}
		}
		else
		{
			# test for update
			$sandbox = clone $modules;
			$zip->unzip($define, $target.'/_define.php');

			$sandbox->resetModulesList();
			$sandbox->requireDefine($target,basename($destination));
			unlink($target.'/_define.php');
			$new_modules = $sandbox->getModules();

			if (!empty($new_modules))
			{
				$tmp = array_keys($new_modules);
				$id = $tmp[0];
				$cur_module = $modules->getModules($id);
				if (!empty($cur_module) && (defined('DC_DEV') && DC_DEV === true || dcUtils::versionsCompare($new_modules[$id]['version'], $cur_module['version'], '>', true)))
				{
					# delete old module
					if (!files::deltree($destination)) {
						throw new Exception(__('An error occurred during module deletion.'));
					}
					$ret_code = 2;
				}
				else
				{
					$zip->close();
					unlink($zip_file);
					throw new Exception(sprintf(__('Unable to upgrade "%s". (older or same version)'),basename($destination)));
				}
			}
			else
			{
				$zip->close();
				unlink($zip_file);
				throw new Exception(sprintf(__('Unable to read new _define.php file')));
			}
		}
		$zip->unzipAll($target);
		$zip->close();
		unlink($zip_file);
		return $ret_code;
	}

	/**
	This method installs all modules having a _install file.

	@see dcModules::installModule
	*/
	public function installModules()
	{
		$res = array('success'=>array(),'failure'=>array());
		foreach ($this->modules as $id => &$m)
		{
			$i = $this->installModule($id,$msg);
			if ($i === true) {
				$res['success'][$id] = true;
			} elseif ($i === false) {
				$res['failure'][$id] = $msg;
			}
		}

		return $res;
	}

	/**
	This method installs module with ID <var>$id</var> and having a _install
	file. This file should throw exception on failure or true if it installs
	successfully.

	<var>$msg</var> is an out parameter that handle installer message.

	@param	id		<b>string</b>		Module ID
	@param	msg		<b>string</b>		Module installer message
	@return	<b>boolean</b>
	*/
	public function installModule($id,&$msg)
	{
		try {
			$i = $this->loadModuleFile($this->modules[$id]['root'].'/_install.php');
			if ($i === true) {
				return true;
			}
		} catch (Exception $e) {
			$msg = $e->getMessage();
			return false;
		}

		return null;
	}

	public function deleteModule($id,$disabled=false)
	{
		if ($disabled) {
			$p =& $this->disabled;
		} else {
			$p =& $this->modules;
		}

		if (!isset($p[$id])) {
			throw new Exception(__('No such module.'));
		}

		if (!files::deltree($p[$id]['root'])) {
			throw new Exception(__('Cannot remove module files'));
		}
	}

	public function deactivateModule($id)
	{
		if (!isset($this->modules[$id])) {
			throw new Exception(__('No such module.'));
		}

		if (!$this->modules[$id]['root_writable']) {
			throw new Exception(__('Cannot deactivate plugin.'));
		}

		if (@file_put_contents($this->modules[$id]['root'].'/_disabled','')) {
			throw new Exception(__('Cannot deactivate plugin.'));
		}
	}

	public function activateModule($id)
	{
		if (!isset($this->disabled[$id])) {
			throw new Exception(__('No such module.'));
		}

		if (!$this->disabled[$id]['root_writable']) {
			throw new Exception(__('Cannot activate plugin.'));
		}

		if (@unlink($this->disabled[$id]['root'].'/_disabled') === false) {
			throw new Exception(__('Cannot activate plugin.'));
		}
	}

	/**
	This method will search for file <var>$file</var> in language
	<var>$lang</var> for module <var>$id</var>.

	<var>$file</var> should not have any extension.

	@param	id		<b>string</b>		Module ID
	@param	lang		<b>string</b>		Language code
	@param	file		<b>string</b>		File name (without extension)
	*/
	public function loadModuleL10N($id,$lang,$file)
	{
		if (!$lang || !isset($this->modules[$id])) {
			return;
		}

		$lfile = $this->modules[$id]['root'].'/locales/%s/%s';
		if (l10n::set(sprintf($lfile,$lang,$file)) === false && $lang != 'en') {
			l10n::set(sprintf($lfile,'en',$file));
		}
	}

	public function loadModuleL10Nresources($id,$lang)
	{
		if (!$lang || !isset($this->modules[$id])) {
			return;
		}

		$f = l10n::getFilePath($this->modules[$id]['root'].'/locales','resources.php',$lang);
		if ($f) {
			$this->loadModuleFile($f);
		}
	}

	/**
	Returns all modules associative array or only one module if <var>$id</var>
	is present.

	@param	id		<b>string</b>		Optionnal module ID
	@return	<b>array</b>
	*/
	public function getModules($id=null)
	{
		if ($id && isset($this->modules[$id])) {
			return $this->modules[$id];
		}
		return $this->modules;
	}

	/**
	Returns true if the module with ID <var>$id</var> exists.

	@param	id		<b>string</b>		Module ID
	@return	<b>boolean</b>
	*/
	public function moduleExists($id)
	{
		return isset($this->modules[$id]);
	}

	/**
	Returns all disabled modules in an array

	@return	<b>array</b>
	*/
	public function getDisabledModules()
	{
		return $this->disabled;
	}

	/**
	Returns root path for module with ID <var>$id</var>.

	@param	id		<b>string</b>		Module ID
	@return	<b>string</b>
	*/
	public function moduleRoot($id)
	{
		return $this->moduleInfo($id,'root');
	}

	/**
	Returns a module information that could be:
	- root
	- name
	- desc
	- author
	- version
	- permissions
	- priority

	@param	id		<b>string</b>		Module ID
	@param	info		<b>string</b>		Information to retrieve
	@return	<b>string</b>
	*/
	public function moduleInfo($id,$info)
	{
		return isset($this->modules[$id][$info]) ? $this->modules[$id][$info] : null;
	}

	/**
	Loads namespace <var>$ns</var> specific files for all modules.

	@param	ns		<b>string</b>		Namespace name
	*/
	public function loadNsFiles($ns=null)
	{
		foreach ($this->modules as $k => $v) {
			$this->loadNsFile($k,$ns);
		}
	}

	/**
	Loads namespace <var>$ns</var> specific file for module with ID
	<var>$id</var>

	@param	id		<b>string</b>		Module ID
	@param	ns		<b>string</b>		Namespace name
	*/
	public function loadNsFile($id,$ns=null)
	{
		switch ($ns) {
			case 'admin':
				$this->loadModuleFile($this->modules[$id]['root'].'/_admin.php');
				break;
			case 'public':
				$this->loadModuleFile($this->modules[$id]['root'].'/_public.php');
				break;
			case 'xmlrpc':
				$this->loadModuleFile($this->modules[$id]['root'].'/_xmlrpc.php');
				break;
		}
	}

	public function getErrors()
	{
		return $this->errors;
	}

	protected function loadModuleFile($________)
	{
		if (!file_exists($________)) {
			return;
		}

		self::$_k = array_keys($GLOBALS);

		foreach (self::$_k as self::$_n) {
			if (!in_array(self::$_n,self::$superglobals)) {
				global ${self::$_n};
			}
		}

		return require $________;
	}

	private function sortModules($a,$b)
	{
		if ($a['priority'] == $b['priority']) {
			return strcasecmp($a['name'],$b['name']);
		}

		return ($a['priority'] < $b['priority']) ? -1 : 1;
	}
}