Completed
Push — master ( d10e6f...1f9c45 )
by Henry
08:18
created

Demo::routeHeader()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 8
nc 5
nop 0
1
<?php
2
namespace Redaxscript\Modules\Demo;
3
4
use Redaxscript\Auth;
5
use Redaxscript\Db;
6
use Redaxscript\Installer;
7
use Redaxscript\Messenger as Messenger;
8
9
/**
10
 * enable anonymous login
11
 *
12
 * @since 2.2.0
13
 *
14
 * @package Redaxscript
15
 * @category Modules
16
 * @author Henry Ruhs
17
 */
18
19
class Demo extends Config
20
{
21
	/**
22
	 * array of the module
23
	 *
24
	 * @var array
25
	 */
26
27
	protected static $_moduleArray =
28
	[
29
		'name' => 'Demo',
30
		'alias' => 'Demo',
31
		'author' => 'Redaxmedia',
32
		'description' => 'Enable demo login',
33
		'version' => '3.3.0'
34
	];
35
36
	/**
37
	 * routeHeader
38
	 *
39
	 * @since 3.3.0
40
	 */
41
42
	public function routeHeader()
43
	{
44
		if ($this->_registry->get('firstParameter') === 'demo')
45
		{
46
			/* handle login */
47
48
			if ($this->_registry->get('secondParameter') === 'login')
49
			{
50
				$this->_registry->set('useTitle', $this->_language->get('login'));
51
				$this->_registry->set('routerBreak', true);
52
			}
53
54
			/* handle reinstall */
55
56
			if ($this->_registry->get('secondParameter') === 'reinstall')
57
			{
58
				$this->_registry->set('renderBreak', true);
59
				$this->_reinstall();
60
			}
61
		}
62
	}
63
64
	/**
65
	 * routeContent
66
	 *
67
	 * @since 3.3.0
68
	 */
69
70
	public function routeContent()
71
	{
72
		if ($this->_registry->get('firstParameter') === 'demo' && $this->_registry->get('secondParameter') === 'login')
73
		{
74
			echo $this->process();
75
		}
76
	}
77
78
	/**
79
	 * adminPanelNotification
80
	 *
81
	 * @since 3.0.0
82
	 *
83
	 * @return array|bool
84
	 */
85
86
	public function adminPanelNotification()
87
	{
88
		$auth = new Auth($this->_request);
89
		$auth->init();
90
91
		/* demo user */
92
93
		if ($auth->getUser('user') === 'demo')
94
		{
95
			$this->setNotification('success', $this->_language->get('logged_in') . $this->_language->get('point'));
96
		}
97
		return $this->getNotification();
98
	}
99
100
	/**
101
	 * process
102
	 *
103
	 * @since 3.0.0
104
	 *
105
	 * @return string
106
	 */
107
108
	public function process()
109
	{
110
		$auth = new Auth($this->_request);
111
		$tableArray =
112
		[
113
			'categories',
114
			'articles',
115
			'extras',
116
			'comments',
117
			'groups',
118
			'users'
119
		];
120
121
		/* set the user */
122
123
		$auth->setUser('name', 'Demo');
124
		$auth->setUser('user', 'demo');
125
		$auth->setUser('email', 'demo@localhost');
126
127
		/* set the permission */
128
129
		foreach ($tableArray as $value)
130
		{
131
			$auth->setPermission($value,
132
			[
133
				1,
134
				2,
135
				3
136
			]);
137
		}
138
		$auth->setPermission('settings',
139
		[
140
			1
141
		]);
142
143
		/* save user and permission */
144
145
		$auth->save();
146
147
		/* handle success */
148
149
		if ($auth->getStatus())
150
		{
151
			return $this->_success();
152
		}
153
		return $this->_error();
154
	}
155
156
	/**
157
	 * success
158
	 *
159
	 * @since 3.0.0
160
	 *
161
	 * @return string
162
	 */
163
164
	protected function _success()
165
	{
166
		$messenger = new Messenger($this->_registry);
167
		return $messenger
168
			->setRoute($this->_language->get('continue'), 'admin')
169
			->doRedirect(0)
170
			->success($this->_language->get('logged_in'), $this->_language->get('welcome'));
171
	}
172
173
	/**
174
	 * error
175
	 *
176
	 * @since 3.0.0
177
	 *
178
	 * @return string
179
	 */
180
181
	protected function _error()
182
	{
183
		$messenger = new Messenger($this->_registry);
184
		return $messenger
185
			->setRoute($this->_language->get('back'), 'login')
186
			->doRedirect()
187
			->error($this->_language->get('something_wrong'), $this->_language->get('error_occurred'));
188
	}
189
190
	/**
191
	 * reinstall
192
	 *
193
	 * @since 2.4.0
194
	 */
195
196
	protected function _reinstall()
197
	{
198
		$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
199
		$installer->init();
200
		$installer->rawDrop();
201
		$installer->rawCreate();
202
		$installer->insertData(
203
		[
204
			'adminName' => 'Admin',
205
			'adminUser' => 'admin',
206
			'adminPassword' => 'admin',
207
			'adminEmail' => 'admin@localhost'
208
		]);
209
210
		/* process modules */
211
212
		foreach ($this->_configArray['modules'] as $key => $moduleClass)
213
		{
214
			if (is_dir('modules/' . $key))
215
			{
216
				$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config);
217
				$module->install();
218
			}
219
		}
220
221
		/* access and filter */
222
223
		Db::forTablePrefix('groups')
224
			->findMany()
225
			->set(
226
			[
227
				'modules' => null,
228
				'filter' => 1
229
			])
230
			->save();
231
	}
232
}
233