Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Controller.php « Installation « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fdd6f7c395cba2ee4763d7ba83788863437cee12 (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
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
<?php
/**
 * Piwik - Open source web analytics
 * 
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
 * @version $Id$
 * 
 * @package Piwik_Installation
 */

require_once "View.php";
require_once "Installation/View.php";

/**
 * 
 * @package Piwik_Installation
 */
class Piwik_Installation_Controller extends Piwik_Controller
{
	// public so plugins can modify it
	public $steps = array(
			'welcome',
			'systemCheck',
			'databaseSetup',
			'tablesCreation',
			'generalSetup',
			'firstWebsiteSetup',
			'displayJavascriptCode',
			'finished'
		);
		
	protected $pathView = 'Installation/templates/';
	
	public function __construct()
	{
		if(!isset($_SESSION['currentStepDone'])) 
		{
			$_SESSION['currentStepDone'] = '';
		}
		
		Piwik_PostEvent('InstallationController.construct', $this);
	}
	
	public function getInstallationSteps()
	{
		return $this->steps;
	}
	
	function getDefaultAction()
	{
		return $this->steps[0];
	}
	
	function welcome()
	{
		$view = new Piwik_Install_View(
						$this->pathView . 'welcome.tpl', 
						$this->getInstallationSteps(),
						__FUNCTION__
					);
		$this->skipThisStep( __FUNCTION__ );
		$view->showNextStep = true;
		
		$_SESSION['currentStepDone'] = __FUNCTION__;		
		echo $view->render();
	}
	
	function systemCheck()
	{
		$view = new Piwik_Install_View(
						$this->pathView . 'systemCheck.tpl', 
						$this->getInstallationSteps(),
						__FUNCTION__
					);
		$this->checkPreviousStepIsValid( __FUNCTION__ );
		$this->skipThisStep( __FUNCTION__ );
		
		$view->infos = $this->getSystemInformation();
		$view->problemWithSomeDirectories = (false !== array_search(false, $view->infos['directories']));
		
		$pathToMainPiwik = Piwik::getPathToPiwikRoot();
		$view->basePath = $pathToMainPiwik; 
		
		$view->showNextStep = !$view->problemWithSomeDirectories 
							&& $view->infos['phpVersion_ok']
							&& $view->infos['pdo_ok']
							&& $view->infos['pdo_mysql_ok']

						;
		$_SESSION['currentStepDone'] = __FUNCTION__;		

		echo $view->render();
	}
	
	
	function databaseSetup()
	{
		// case the user hits the back button
		$_SESSION['skipThisStep']['firstWebsiteSetup'] = false;
		$_SESSION['skipThisStep']['displayJavascriptCode'] = false;
		
		$view = new Piwik_Install_View(
						$this->pathView . 'databaseSetup.tpl', 
						$this->getInstallationSteps(),
						__FUNCTION__
					);
		$this->checkPreviousStepIsValid( __FUNCTION__ );
		$this->skipThisStep( __FUNCTION__ );
					
		$view->showNextStep = false;
		require_once "FormDatabaseSetup.php";
		$form = new Piwik_Installation_FormDatabaseSetup;
		
		if($form->validate())
		{
			$dbInfos = array(
				'host' 			=> $form->getSubmitValue('host'),
				'username' 		=> $form->getSubmitValue('username'),
				'password' 		=> $form->getSubmitValue('password'),
				'dbname' 		=> $form->getSubmitValue('dbname'),
				'tables_prefix' => $form->getSubmitValue('tables_prefix'),
				'adapter' 		=> Zend_Registry::get('config')->database->adapter,
				'profiler' 		=> 'false',
			);
			
			// we test the DB connection with these settings
			try{ 
//				var_dump($dbInfos);exit;
				$dbInfos['password'] = '"'.htmlspecialchars($form->getSubmitValue('password')).'"';
				
				Piwik::createDatabaseObject($dbInfos);
				
				$_SESSION['db_infos'] = $dbInfos;
				
				$this->redirectToNextStep( __FUNCTION__ );
			} catch(Exception $e) {
				$view->errorMessage = $e->getMessage();
			}
		}
		$view->addForm($form);
		
		$view->infos = $this->getSystemInformation();
			
		echo $view->render();
	}
	
	protected function skipThisStep( $step )
	{
		if(isset($_SESSION['skipThisStep'][$step])
			&& $_SESSION['skipThisStep'][$step])
		{
			$this->redirectToNextStep($step);
		}
	}
	
	function tablesCreation()
	{
		$view = new Piwik_Install_View(
						$this->pathView . 'tablesCreation.tpl', 
						$this->getInstallationSteps(),
						__FUNCTION__
					);
		$this->checkPreviousStepIsValid( __FUNCTION__ );
		$this->skipThisStep( __FUNCTION__ );
		$this->createDbFromSessionInformation();
		
		if(Piwik_Common::getRequestVar('deleteTables', 0, 'int') == 1)
		{
			Piwik::dropTables();
			$view->existingTablesDeleted = true;
			
			// when the user decides to drop the tables then we dont skip the next steps anymore
			$_SESSION['skipThisStep']['firstWebsiteSetup'] = false;
			$_SESSION['skipThisStep']['displayJavascriptCode'] = false;
		}
		
		$tablesInstalled = Piwik::getTablesInstalled();
		$tablesToInstall = Piwik::getTablesNames();
		
		if(count($tablesInstalled) > 0)
		{
			$view->someTablesInstalled = true;
			$view->tablesInstalled = implode(", ", $tablesInstalled);
			
			// when the user reuses the same tables we skip the website creation step
			$_SESSION['skipThisStep']['firstWebsiteSetup'] = true;
			$_SESSION['skipThisStep']['displayJavascriptCode'] = true;
		}
		else
		{
			Piwik::createTables();
			Piwik::createAnonymousUser();
			Piwik::createTablesIndex();
			
			$view->tablesCreated = true;
			$view->showNextStep = true;
		}
		
		$_SESSION['currentStepDone'] = __FUNCTION__;
		echo $view->render();
	}
	
	function generalSetup()
	{		
		$view = new Piwik_Install_View(
						$this->pathView . 'generalSetup.tpl', 
						$this->getInstallationSteps(),
						__FUNCTION__
					);
		$this->checkPreviousStepIsValid( __FUNCTION__ );
		$this->skipThisStep( __FUNCTION__ );
		
		require_once "FormGeneralSetup.php";
		$form = new Piwik_Installation_FormGeneralSetup;
		
		if($form->validate())
		{			
			$superUserInfos = array(
				'login' 		=> $form->getSubmitValue('login'),
				'password' 		=> md5( $form->getSubmitValue('password') ),
				'email' 		=> $form->getSubmitValue('email'),
			);
			
			$_SESSION['superuser_infos'] = $superUserInfos;
			$this->redirectToNextStep( __FUNCTION__ );
		}
		$view->addForm($form);
			
		echo $view->render();
	}
	
	public function firstWebsiteSetup()
	{
				
		$view = new Piwik_Install_View(
						$this->pathView . 'firstWebsiteSetup.tpl', 
						$this->getInstallationSteps(),
						__FUNCTION__
					);
		$this->checkPreviousStepIsValid( __FUNCTION__ );
		$this->skipThisStep( __FUNCTION__ );
		
		require_once "FormFirstWebsiteSetup.php";
		$form = new Piwik_Installation_FormFirstWebsiteSetup;
		
		if( !isset($_SESSION['generalSetupSuccessMessage']))
		{
			$view->displayGeneralSetupSuccess = true;
			$_SESSION['generalSetupSuccessMessage'] = true;
		}
		
		if($form->validate())
		{
			// we setup the superuser login & password in the config that will be checked by the
			// API authentication process
			Zend_Registry::get('config')->superuser = $_SESSION['superuser_infos'];
			
			$name = urlencode($form->getSubmitValue('siteName'));
			$url = urlencode($form->getSubmitValue('url'));
			
			$this->initObjectsToCallAPI();
						
			require_once "API/Request.php";
			$request = new Piwik_API_Request("
							method=SitesManager.addSite
							&siteName=$name
							&urls=$url
							&format=original
						");
						
			try {
				$result = $request->process();
				$_SESSION['site_idSite'] = $result;
				$_SESSION['site_name'] = $name;
				$_SESSION['site_url'] = $url;
				
				$this->redirectToNextStep( __FUNCTION__ );
			} catch(Exception $e) {
				$view->errorMessage = $e->getMessage();
			}

		}
		$view->addForm($form);
		
		echo $view->render();
	}
	public function displayJavascriptCode()
	{
		$view = new Piwik_Install_View(
						$this->pathView . 'displayJavascriptCode.tpl', 
						$this->getInstallationSteps(),
						__FUNCTION__
					);
		$this->checkPreviousStepIsValid( __FUNCTION__ );
		$this->skipThisStep( __FUNCTION__ );
		
		if( !isset($_SESSION['firstWebsiteSetupSuccessMessage']))
		{
			$view->displayfirstWebsiteSetupSuccess = true;
			$_SESSION['firstWebsiteSetupSuccessMessage'] = true;
		}
		
		
		$view->websiteName = urldecode($_SESSION['site_name']);
		
		$jsTag = Piwik::getJavascriptCode($_SESSION['site_idSite'], Piwik_Url::getCurrentUrlWithoutFileName());
		
		$view->javascriptTag = $jsTag;
		$view->showNextStep = true;
		
		$_SESSION['currentStepDone'] = __FUNCTION__;
		echo $view->render();
	}
	
	public function finished()
	{
		$view = new Piwik_Install_View(
						$this->pathView . 'finished.tpl', 
						$this->getInstallationSteps(),
						__FUNCTION__
					);
		$this->writeConfigFileFromSession();
		$this->checkPreviousStepIsValid( __FUNCTION__ );
		$this->skipThisStep( __FUNCTION__ );
		
		
		$_SESSION['currentStepDone'] = __FUNCTION__;		
		$view->showNextStep = false;
		
	    setcookie(session_name(), session_id(), 1, '/');
		@session_destroy();	
		echo $view->render();
		
		// cron tab help
		// javascript reminder
		// giving good names to pages
	}
	
	
	
	
	protected function initObjectsToCallAPI()
	{
		// connect to the database using the DB infos currently in the session
		$this->createDbFromSessionInformation();

		// create the fake access to grant super user privilege
		Zend_Registry::set('access', new Piwik_FakeAccess_SetSuperUser);
		
		// we need to create the logs otherwise the API request throws an exception
		Piwik::createLogObject();
	}
	
	protected function writeConfigFileFromSession()
	{
		$configFile = "; <?php exit; ?> DO NOT REMOVE THIS LINE\n";
		$configFile .= "; file automatically generated during the piwik installation process\n";
		
		// super user information
		$configFile .= "[superuser]\n";
		foreach( $_SESSION['superuser_infos'] as $key => $value)
		{
			$configFile .= "$key = $value\n";
		}
		$configFile .= "\n";
		
		// database information
		$configFile .= "[database]\n";
		foreach($_SESSION['db_infos'] as  $key => $value)
		{
			$configFile .= "$key = $value\n";
		}
		
		file_put_contents(Piwik_Config::getDefaultUserConfigPath(), $configFile);
	}
	/**
	 * The previous step is valid if it is either 
	 * - any step before (OK to go back)
	 * - the current step (case when validating a form)
	 */
	function checkPreviousStepIsValid( $currentStep )
	{
		// the currentStep
		$currentStepId = array_search($currentStep, $this->steps);
		
		// the step before
		$previousStepId = array_search($_SESSION['currentStepDone'], $this->steps);
		
		// not OK if currentStepId > previous+1
		if( $currentStepId > $previousStepId + 1 )
		{
			$message = "Error: it seems you try to skip a step of the Installation process, #
						or your cookies are disabled. 
						<br><b>Make sure your cookies are enabled</b> and go back 
						<a href='".Piwik_Url::getCurrentUrlWithoutFileName()."'>
						to the first page of the installation</a>.";
			Piwik::exitWithErrorMessage( $message );
		}		
	}

	protected function redirectToNextStep($currentStep)
	{
		$_SESSION['currentStepDone'] = $currentStep;
		$nextStep = $this->steps[1 + array_search($currentStep, $this->steps)];
		Piwik::redirectToModule('Installation' , $nextStep);
	}
	
	protected function createDbFromSessionInformation()
	{
		$dbInfos = $_SESSION['db_infos'];
		
		Zend_Registry::get('config')->database = $dbInfos;
		Piwik::createDatabaseObject($dbInfos);
	}
	
	protected function getSystemInformation()
	{
		$minimumPhpVersion = Zend_Registry::get('config')->General->minimum_php_version;
		$minimumMemoryLimit = Zend_Registry::get('config')->General->minimum_memory_limit;
		
		$infos = array();
	
		// directory to write
		$infos['directories'] = Piwik::checkDirectoriesWritable();
		
		// php version
		$infos['phpVersion_minimum'] = $minimumPhpVersion;
		$infos['phpVersion'] = phpversion();
		$infos['phpVersion_ok'] = version_compare( $minimumPhpVersion, $infos['phpVersion']) === -1;
		
		$extensions = @get_loaded_extensions();
		
		$infos['pdo_ok'] = false;
		// Mysql + version
		if (in_array('PDO', $extensions))  
		{
		    $infos['pdo_ok'] = true;
		}
				
		$infos['pdo_mysql_ok'] = false;
		// Mysql + version
		if (in_array('pdo_mysql', $extensions))  
		{
		    $infos['pdo_mysql_ok'] = true;
		}
		
		$infos['gd_ok'] = false;
		// Gd version
		if (in_array('gd', $extensions)) 
		{
		    $gdInfo = gd_info();
		
			$infos['gd_version'] = $gdInfo['GD Version'];
			
		    ereg ("([0-9]{1})", $gdInfo['GD Version'], $gdVersion);
		    if($gdVersion[0] >= 2) 
		    {
				$infos['gd_ok'] = true;
		    }
		}
			
		// server version
		$infos['serverVersion'] = addslashes($_SERVER['SERVER_SOFTWARE']);
	
		// server os (linux)
		$infos['serverOs'] = @php_uname();
		
		// server time
		$infos['serverTime'] = date('H:i:s');
				
		if(function_exists( 'set_time_limit'))
		{
			$infos['setTimeLimit_ok'] = true;
		}
	
//		$infos['phpXml_ok'] = false;
//		if(function_exists( 'utf8_encode') 
//			&& function_exists( 'utf8_decode'))
//		{
//			$infos['phpXml_ok'] = true;
//		}
		
		$infos['mail_ok'] = false;
		if(function_exists('mail'))
		{
			$infos['mail_ok'] = true;
		}
		
		//Registre global
		$infos['registerGlobals_ok'] = ini_get('register_globals') == 0;
		
		$infos['memoryMinimum'] = $minimumMemoryLimit;
		
		// we set true by default, in case we can't read the memory_limit value
		// we dont want to display a warning (instead we should figure out why
		// we can't read this value...)
		$infos['memory_ok'] = true;
		
		// on windows the ini_get is not working?
		$infos['memoryCurrent'] = '?M';
		
		
		$raised = Piwik::raiseMemoryLimitIfNecessary();
		if(	$memoryValue = Piwik::getMemoryLimitValue() )
		{
			$infos['memoryCurrent'] = $memoryValue."M";
			$infos['memory_ok'] = $memoryValue >= $minimumMemoryLimit;
		}
				/*
		// server uptime from mysql uptime
		$res = query('SHOW STATUS');
		if($res)
		{
			while ($row = mysql_fetch_array($res)) 
			{
			   $serverStatus[$row[0]] = $row[1];
			}
	
			$infos['server_uptime'] = date("r",time() - $serverStatus['Uptime']); 		
		}*/
		
		return $infos;
	}
}


/**
 * 
 * @package Piwik_Installation
 */
class Piwik_FakeAccess_SetSuperUser {
	function checkUserIsSuperUser()
	{
		return true;
	}
	function loadAccess() {}
}