Completed
Push — master ( da58d4...61a0f7 )
by Henry
06:34
created

includes/Installer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript;
3
4
use function file_get_contents;
5
use function method_exists;
6
use function str_replace;
7
use function ucfirst;
8
9
/**
10
 * parent class to install the database
11
 *
12
 * @since 2.4.0
13
 *
14
 * @category Installer
15
 * @package Redaxscript
16
 * @author Henry Ruhs
17
 */
18
19
class Installer
20
{
21
	/**
22
	 * instance of the registry class
23
	 *
24
	 * @var Registry
25
	 */
26
27
	protected $_registry;
28
29
	/**
30
	 * instance of the request class
31
	 *
32
	 * @var Request
33
	 */
34
35
	protected $_request;
36
37
	/**
38
	 * instance of the language class
39
	 *
40
	 * @var Language
41
	 */
42
43
	protected $_language;
44
45
	/**
46
	 * instance of the config class
47
	 *
48
	 * @var Config
49
	 */
50
51
	protected $_config;
52
53
	/**
54
	 * name of the directory
55
	 *
56
	 * @var string
57
	 */
58
59
	protected $_directory;
60
61
	/**
62
	 * placeholder for the prefix
63
	 *
64
	 * @var string
65
	 */
66
67
	protected $_prefixPlaceholder = '/* %PREFIX% */';
68
69
	/**
70
	 * constructor of the class
71
	 *
72
	 * @since 3.0.0
73
	 *
74
	 * @param Registry $registry instance of the registry class
75
	 * @param Request $request instance of the request class
76
	 * @param Language $language instance of the language class
77
	 * @param Config $config instance of the config class
78
	 */
79
80 3
	public function __construct(Registry $registry, Request $request, Language $language, Config $config)
81
	{
82 3
		$this->_registry = $registry;
83 3
		$this->_request = $request;
84 3
		$this->_language = $language;
85 3
		$this->_config = $config;
86 3
	}
87
88
	/**
89
	 * init the class
90
	 *
91
	 * @since 2.6.0
92
	 *
93
	 * @param string $directory name of the directory
94
	 */
95
96 3
	public function init(string $directory = 'database') : void
97
	{
98 3
		$this->_directory = $directory;
99 3
	}
100
101
	/**
102
	 * create from sql
103
	 *
104
	 * @since 2.4.0
105
	 */
106
107 1
	public function rawCreate() : void
108
	{
109 1
		$this->_rawExecute('create', $this->_config->get('dbType'));
110 1
	}
111
112
	/**
113
	 * drop from sql
114
	 *
115
	 * @since 2.4.0
116
	 */
117
118 1
	public function rawDrop() : void
119
	{
120 1
		$this->_rawExecute('drop', $this->_config->get('dbType'));
121 1
	}
122
123
	/**
124
	 * insert the data
125
	 *
126
	 * @since 3.1.0
127
	 *
128
	 * @param array $optionArray options of the installation
129
	 */
130
131 1
	public function insertData(array $optionArray = []) : void
132
	{
133 1
		$this->insertCategories($optionArray);
134 1
		$this->insertArticles($optionArray);
135 1
		$this->insertExtras($optionArray);
136 1
		$this->insertComments($optionArray);
137 1
		$this->insertGroups();
138 1
		$this->insertUsers($optionArray);
139 1
		$this->insertModules();
140 1
		$this->insertSettings($optionArray);
141 1
	}
142
143
	/**
144
	 * insert the categories
145
	 *
146
	 * @since 3.1.0
147
	 *
148
	 * @param array $optionArray options of the installation
149
	 */
150
151 1
	public function insertCategories(array $optionArray = []) : void
152
	{
153 1
		$now = $this->_registry->get('now');
154 1
		Db::forTablePrefix('categories')
155 1
			->create()
156 1
			->set(
157
			[
158 1
				'title' => 'Home',
159 1
				'alias' => 'home',
160 1
				'author' => $optionArray['adminUser'],
161 1
				'rank' => 1,
162 1
				'date' => $now
163
			])
164 1
			->save();
165 1
	}
166
167
	/**
168
	 * insert the articles
169
	 *
170
	 * @since 3.1.0
171
	 *
172
	 * @param array $optionArray options of the installation
173
	 */
174
175 1
	public function insertArticles(array $optionArray = []) : void
176
	{
177 1
		$now = $this->_registry->get('now');
178 1
		Db::forTablePrefix('articles')
179 1
			->create()
180 1
			->set(
181
			[
182 1
				'title' => 'Welcome',
183 1
				'alias' => 'welcome',
184 1
				'author' => $optionArray['adminUser'],
185 1
				'text' => file_get_contents('database' . DIRECTORY_SEPARATOR . 'html' . DIRECTORY_SEPARATOR . 'articles' . DIRECTORY_SEPARATOR . 'welcome.phtml'),
186 1
				'category' => 1,
187 1
				'comments' => 1,
188 1
				'rank' => 1,
189 1
				'date' => $now
190
			])
191 1
			->save();
192 1
	}
193
194
	/**
195
	 * insert the extras
196
	 *
197
	 * @since 3.1.0
198
	 *
199
	 * @param array $optionArray options of the installation
200
	 */
201
202 1
	public function insertExtras(array $optionArray = []) : void
203
	{
204 1
		$now = $this->_registry->get('now');
205
		$extrasArray =
206
		[
207 1
			'categories' =>
208
			[
209
				'category' => null,
210
				'headline' => 1,
211
				'status' => 1
212
			],
213
			'articles' =>
214
			[
215
				'category' => null,
216
				'headline' => 1,
217
				'status' => 1
218
			],
219
			'comments' =>
220
			[
221
				'category' => null,
222
				'headline' => 1,
223
				'status' => 1
224
			],
225
			'languages' =>
226
			[
227
				'category' => null,
228
				'headline' => 1,
229
				'status' => 0
230
			],
231
			'templates' =>
232
			[
233
				'category' => null,
234
				'headline' => 1,
235
				'status' => 0
236
			],
237
			'teaser' =>
238
			[
239
				'category' => 1,
240
				'headline' => 0,
241
				'status' => 0
242
			]
243
		];
244 1
		$extrasRank = 0;
245
246
		/* process extras */
247
248 1
		foreach ($extrasArray as $key => $value)
249
		{
250 1
			Db::forTablePrefix('extras')
251 1
				->create()
252 1
				->set(
253
				[
254 1
					'title' => ucfirst($key),
255 1
					'alias' => $key,
256 1
					'author' => $optionArray['adminUser'],
257 1
					'text' => file_get_contents('database' . DIRECTORY_SEPARATOR . 'html' . DIRECTORY_SEPARATOR . 'extras' . DIRECTORY_SEPARATOR . $key . '.phtml'),
258 1
					'category' => $value['category'],
259 1
					'headline' => $value['headline'],
260 1
					'status' => $value['status'],
261 1
					'rank' => ++$extrasRank,
262 1
					'date' => $now
263
				])
264 1
				->save();
265
		}
266 1
	}
267
268
	/**
269
	 * insert the comments
270
	 *
271
	 * @since 3.1.0
272
	 *
273
	 * @param array $optionArray options of the installation
274
	 */
275
276 1
	public function insertComments(array $optionArray = []) : void
277
	{
278 1
		$now = $this->_registry->get('now');
279 1
		Db::forTablePrefix('comments')
280 1
			->create()
281 1
			->set(
282
			[
283 1
				'author' => $optionArray['adminUser'],
284 1
				'email' => $optionArray['adminEmail'],
285 1
				'text' => file_get_contents('database' . DIRECTORY_SEPARATOR . 'html' . DIRECTORY_SEPARATOR . 'comments' . DIRECTORY_SEPARATOR . 'hello.phtml'),
286 1
				'article' => 1,
287 1
				'rank' => 1,
288 1
				'date' => $now
289
			])
290 1
			->save();
291 1
	}
292
293
	/**
294
	 * insert the groups
295
	 *
296
	 * @since 3.1.0
297
	 */
298
299 1
	public function insertGroups() : void
300
	{
301 1
		Db::forTablePrefix('groups')
302 1
			->create()
303 1
			->set(
304
			[
305 1
				'name' => 'Administrators',
306
				'alias' => 'administrators',
307
				'description' => 'Unlimited access',
308
				'categories' => '[1, 2, 3]',
309
				'articles' => '[1, 2, 3]',
310
				'extras' => '[1, 2, 3]',
311
				'comments' => '[1, 2, 3]',
312
				'groups' => '[1, 2, 3]',
313
				'users' => '[1, 2, 3]',
314
				'modules' => '[1, 2, 3]',
315
				'settings' => 1,
316
				'filter' => 0
317
			])
318 1
			->save();
319 1
		Db::forTablePrefix('groups')
320 1
			->create()
321 1
			->set(
322
			[
323 1
				'name' => 'Members',
324
				'alias' => 'members',
325
				'description' => 'Default members group'
326
			])
327 1
			->save();
328 1
	}
329
330
	/**
331
	 * insert the users
332
	 *
333
	 * @since 3.1.0
334
	 *
335
	 * @param array $optionArray options of the installation
336
	 */
337
338 1
	public function insertUsers(array $optionArray = []) : void
339
	{
340 1
		$passwordHash = new Hash();
341 1
		$passwordHash->init($optionArray['adminPassword']);
342 1
		Db::forTablePrefix('users')
343 1
			->create()
344 1
			->set(
345
			[
346 1
				'name' => $optionArray['adminName'],
347 1
				'user' => $optionArray['adminUser'],
348 1
				'password' => $passwordHash->getHash(),
349 1
				'email' => $optionArray['adminEmail'],
350 1
				'description' => 'God admin',
351 1
				'groups' => '[1]'
352
			])
353 1
			->save();
354 1
	}
355
356
	/**
357
	 * insert the modules
358
	 *
359
	 * @since 3.1.0
360
	 */
361
362 1
	public function insertModules() : void
363
	{
364
		$moduleArray =
365
		[
366 1
			'AliasGenerator',
367
			'CallHome',
368
			'Dialog',
369
			'FormValidator',
370
			'TextareaResizer',
371
			'TableSorter'
372
		];
373
374
		/* process modules */
375
376 1
		foreach ($moduleArray as $alias)
377
		{
378 1
			$moduleClass = 'Redaxscript\Modules\\' . $alias . '\\' . $alias;
379
380
			/* install */
381
382 1
			if (method_exists($moduleClass, 'install'))
383
			{
384 1
				$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config);
385 1
				$module->install();
386
			}
387
		}
388 1
	}
389
390
	/**
391
	 * insert the settings
392
	 *
393
	 * @since 3.1.0
394
	 *
395
	 * @param array $optionArray options of the installation
396
	 */
397
398 1
	public function insertSettings(array $optionArray = []) : void
399
	{
400
		$settingArray =
401
		[
402 1
			'language' => null,
403
			'template' => null,
404 1
			'title' => $this->_language->get('_package')['name'],
405 1
			'author' => $optionArray['adminName'],
406
			'copyright' => null,
407 1
			'description' => $this->_language->get('_package')['description'],
408
			'keywords' => null,
409 1
			'robots' => 1,
410 1
			'email' => $optionArray['adminEmail'],
411 1
			'subject' => $this->_language->get('_package')['name'],
412 1
			'notification' => 0,
413 1
			'charset' => 'utf-8',
414 1
			'divider' => ' - ',
415 1
			'zone' => 'Europe/Berlin',
416 1
			'time' => 'H:i',
417 1
			'date' => 'd.m.Y',
418 1
			'homepage' => 0,
419 1
			'limit' => 10,
420 1
			'order' => 'asc',
421 1
			'pagination' => 1,
422 1
			'registration' => 0,
423 1
			'verification' => 0,
424 1
			'recovery' => 1,
425 1
			'moderation' => 0,
426 1
			'captcha' => 0,
427 1
			'version' => $this->_language->get('_package')['version']
428
		];
429
430
		/* process settings */
431
432 1
		foreach ($settingArray as $name => $value)
433
		{
434 1
			Db::forTablePrefix('settings')
435 1
				->create()
436 1
				->set(
437
				[
438 1
					'name' => $name,
439 1
					'value' => $value
440
				])
441 1
				->save();
442
		}
443 1
	}
444
445
	/**
446
	 * execute from sql
447
	 *
448
	 * @since 2.4.0
449
	 *
450
	 * @param string $action action to process
451
	 * @param string $type type of the database
452
	 */
453
454 2
	protected function _rawExecute(string $action = null, string $type = 'mysql') : void
455
	{
456 2
		$actionFilesystem = new Filesystem\File();
457 2
		$actionFilesystem->init($this->_directory . DIRECTORY_SEPARATOR . $type . DIRECTORY_SEPARATOR . $action);
458 2
		$actionFilesystemArray = $actionFilesystem->getSortArray();
459
460
		/* process filesystem */
461
462 2
		foreach ($actionFilesystemArray as $file)
463
		{
464 2
			$query = $actionFilesystem->readFile($file);
465 2
			if ($query)
466
			{
467 2
				if ($this->_config->get('dbPrefix'))
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_config->get('dbPrefix') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
468
				{
469 2
					$query = str_replace($this->_prefixPlaceholder, $this->_config->get('dbPrefix'), $query);
470
				}
471 2
				Db::rawExecute($query);
472
			}
473
		}
474 2
	}
475
}
476