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

Piwik.php « modules - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0931d2d6ae61499a3f4d287b654e48b19cac38cf (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
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
<?php
/**
 * 
 * @package Piwik
 */
require_once "Config.php";
require_once "Zend/Db.php";
require_once "Zend/Db/Table.php";
require_once "Log.php";
require_once "PluginsManager.php";

class Piwik
{
	const CLASSES_PREFIX = "Piwik_";
	
	static $idPeriods =  array(
			'day'	=>1,
			'week'	=>2,
			'month'	=>3,
			'year'	=>4,
		);
		
	static public function log($message = '')
	{
		Zend_Registry::get('logger_message')->log($message);
		Zend_Registry::get('logger_message')->log( "<br>" . PHP_EOL);
	}
	
	static function displayZendProfiler()
	{
		$profiler = Zend_Registry::get('db')->getProfiler();
	
		$totalTime    = $profiler->getTotalElapsedSecs();
		$queryCount   = $profiler->getTotalNumQueries();
		$longestTime  = 0;
		$longestQuery = null;
		
		foreach ($profiler->getQueryProfiles() as $query) {
		    if ($query->getElapsedSecs() > $longestTime) {
		        $longestTime  = $query->getElapsedSecs();
		        $longestQuery = $query->getQuery();
		    }
		}
		
		echo '<br>Executed ' . $queryCount . ' queries in ' . $totalTime . ' seconds' . "\n";
		echo '<br>Average query length: ' . $totalTime / $queryCount . ' seconds' . "\n";
		echo '<br>Queries per second: ' . $queryCount / $totalTime . "\n";
		echo '<br>Longest query length: ' . $longestTime . "\n";
		echo '<br>Longest query: <br>' . $longestQuery . "\n";
	}
	
	static public function error($message = '')
	{
		trigger_error($message, E_USER_ERROR);
	}
	
	//TODO TEST secureDiv
	static public function secureDiv( $i1, $i2 )
	{
	    if ( is_numeric($i1) && is_numeric($i2) && floatval($i2) != 0)
		{ 
			return $i1 / $i2;
		}   
		return 0;
	}
	
	static public function printQueryCount()
	{
		$profiler = Zend_Registry::get('db')->getProfiler();
		$totalTime    = $profiler->getTotalElapsedSecs();
		$queryCount   = $profiler->getTotalNumQueries();
		Piwik::log("Total queries = $queryCount (total sql time = ".round($totalTime,2)."s)");
	}
	
	static public function printLogStatsSQLProfiling()
	{
		function maxSumMsFirst($a,$b)
		{
			return $a['sum_time_ms'] < $b['sum_time_ms'];
		}
		
		$db = Zend_Registry::get('db');
		$all = $db->fetchAll('	SELECT *, sum_time_ms / count as avg_time_ms 
								FROM '.Piwik::prefixTable('log_profiling') 
						);
		usort($all, 'maxSumMsFirst');
		
		$str='<br><br>Query Profiling<br>----------------------<br>';
		foreach($all as $infoQuery)
		{
			$query = $infoQuery['query'];
			$count = $infoQuery['count'];
			$sum_time_ms = $infoQuery['sum_time_ms'];
			$avg_time_ms = round($infoQuery['avg_time_ms'],1);
			$query = str_replace("\t", "", $query);
			
			$str .= "	$query <br>
						$count times, <b>$sum_time_ms ms total</b><br>
						$avg_time_ms ms average<br>
						<br>";
		}		
		
		print($str);
	}

	static public function printMemoryUsage( $prefixString = null )
	{
		if(function_exists('xdebug_memory_usage'))
		{
			$memory = xdebug_memory_usage();
		}
		else
		{
			$memory = memory_get_usage();
		}
		
		$usage = round( $memory / 1024 / 1024, 2);
		if(!is_null($prefixString))
		{
			Piwik::log($prefixString);
		}
		Piwik::log("Memory usage = $usage Mb");
		Piwik::log();
	}
	
	static public function isNumeric($value)
	{
		return !is_array($value) && ereg('^([-]{0,1}[0-9]{1,}[.]{0,1}[0-9]*)$', $value);
	}
	
	static public function loadPlugins()
	{
		Piwik_PluginsManager::getInstance()->setLanguageToLoad(  Piwik_Translate::getInstance()->getLanguageToLoad() );
		Piwik_PluginsManager::getInstance()->setPluginsToLoad( Zend_Registry::get('config')->Plugins->enabled );
	}
	
	static public function getTableCreateSql( $tableName )
	{
		$tables = Piwik::getTablesCreateSql();
		
		if(!isset($tables[$tableName]))
		{
			throw new Exception("The table '$tableName' SQL creation code couldn't be found.");
		}
		
		return $tables[$tableName];
	}
	
	static public function getTablesCreateSql()
	{
		$config = Zend_Registry::get('config');
		$prefixTables = $config->database->tables_prefix;
		$tables = array(
			'user' => "CREATE TABLE {$prefixTables}user (
						  login VARCHAR(20) NOT NULL,
						  password CHAR(32) NOT NULL,
						  alias VARCHAR(45) NOT NULL,
						  email VARCHAR(100) NOT NULL,
						  token_auth CHAR(32) NOT NULL,
						  date_registered TIMESTAMP NOT NULL,
						  PRIMARY KEY(login),
						  UNIQUE INDEX uniq_keytoken(token_auth)
						)
			",
			
			'access' => "CREATE TABLE {$prefixTables}access (
						  login VARCHAR(20) NOT NULL,
						  idsite INTEGER UNSIGNED NOT NULL,
						  access VARCHAR(10) NULL,
						  PRIMARY KEY(login, idsite)
						)
			",
			
			'site' => "CREATE TABLE {$prefixTables}site (
						  idsite INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
						  name VARCHAR(90) NOT NULL,
						  main_url VARCHAR(255) NOT NULL,
						  PRIMARY KEY(idsite)
						)
			",
			
			'site_url' => "CREATE TABLE {$prefixTables}site_url (
							  idsite INTEGER(10) UNSIGNED NOT NULL,
							  url VARCHAR(255) NOT NULL,
							  PRIMARY KEY(idsite, url)
						)
			",
			
			
			'logger_message' => "CREATE TABLE {$prefixTables}logger_message (
									  idlogger_message INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
									  timestamp TIMESTAMP NULL,
									  message TINYTEXT NULL,
									  PRIMARY KEY(idlogger_message)
									)
			",
			
			'logger_api_call' => "CREATE TABLE {$prefixTables}logger_api_call (
									  idlogger_api_call INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
									  class_name VARCHAR(255) NULL,
									  method_name VARCHAR(255) NULL,
									  parameter_names_default_values TINYTEXT NULL,
									  parameter_values TINYTEXT NULL,
									  execution_time FLOAT NULL,
									  caller_ip BIGINT NULL,
									  timestamp TIMESTAMP NULL,
									  returned_value TINYTEXT NULL,
									  PRIMARY KEY(idlogger_api_call)
									) 
			",
			
			'logger_error' => "CREATE TABLE {$prefixTables}logger_error (
									  idlogger_error INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
									  timestamp TIMESTAMP NULL,
									  message TINYTEXT NULL,
									  errno INTEGER UNSIGNED NULL,
									  errline INTEGER UNSIGNED NULL,
									  errfile VARCHAR(255) NULL,
									  backtrace TEXT NULL,
									  PRIMARY KEY(idlogger_error)
									)
			",
			
			'logger_exception' => "CREATE TABLE {$prefixTables}logger_exception (
									  idlogger_exception INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
									  timestamp TIMESTAMP NULL,
									  message TINYTEXT NULL,
									  errno INTEGER UNSIGNED NULL,
									  errline INTEGER UNSIGNED NULL,
									  errfile VARCHAR(255) NULL,
									  backtrace TEXT NULL,
									  PRIMARY KEY(idlogger_exception)
									)
			",
			
			
			'log_action' => "CREATE TABLE {$prefixTables}log_action (
									  idaction INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
									  name VARCHAR(255) NOT NULL,
  									  type TINYINT UNSIGNED NULL,
									  PRIMARY KEY(idaction)
						)
			",
			
			'log_visit' => "CREATE TABLE {$prefixTables}log_visit (
  idvisit INTEGER(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  idsite INTEGER(10) UNSIGNED NOT NULL,
  visitor_localtime TIME NOT NULL,
  visitor_idcookie CHAR(32) NOT NULL,
  visitor_returning TINYINT(1) NOT NULL,
  visit_first_action_time DATETIME NOT NULL,
  visit_last_action_time DATETIME NOT NULL,
  visit_server_date DATE NOT NULL,
  visit_exit_idaction INTEGER(11) NOT NULL,
  visit_entry_idaction INTEGER(11) NOT NULL,
  visit_total_actions SMALLINT(5) UNSIGNED NOT NULL,
  visit_total_time SMALLINT(5) UNSIGNED NOT NULL,
  referer_type INTEGER UNSIGNED NULL,
  referer_name VARCHAR(70) NULL,
  referer_url TEXT NOT NULL,
  referer_keyword VARCHAR(255) NULL,
  config_md5config CHAR(32) NOT NULL,
  config_os CHAR(3) NOT NULL,
  config_browser_name VARCHAR(10) NOT NULL,
  config_browser_version VARCHAR(20) NOT NULL,
  config_resolution VARCHAR(9) NOT NULL,
  config_color_depth TINYINT(2) UNSIGNED NOT NULL,
  config_pdf TINYINT(1) NOT NULL,
  config_flash TINYINT(1) NOT NULL,
  config_java TINYINT(1) NOT NULL,
  config_director TINYINT(1) NOT NULL,
  config_quicktime TINYINT(1) NOT NULL,
  config_realplayer TINYINT(1) NOT NULL,
  config_windowsmedia TINYINT(1) NOT NULL,
  config_cookie TINYINT(1) NOT NULL,
  location_ip BIGINT(11) NOT NULL,
  location_browser_lang VARCHAR(20) NOT NULL,
  location_country CHAR(3) NOT NULL,
  location_continent CHAR(3) NOT NULL,
  PRIMARY KEY(idvisit)
)
			",
			
			'log_link_visit_action' => "CREATE TABLE {$prefixTables}log_link_visit_action (
											  idlink_va INTEGER(11) NOT NULL AUTO_INCREMENT,
											  idvisit INTEGER(10) UNSIGNED NOT NULL,
											  idaction INTEGER(10) UNSIGNED NOT NULL,
											  idaction_ref INTEGER(11) UNSIGNED NOT NULL,
											  time_spent_ref_action INTEGER(10) UNSIGNED NOT NULL,
											  PRIMARY KEY(idlink_va)
											)
			",
			
			'log_profiling' => "CREATE TABLE {$prefixTables}log_profiling (

  query TEXT NOT NULL,
  count INTEGER UNSIGNED NULL,
  sum_time_ms FLOAT NULL,
  UNIQUE INDEX query(query(700))
)
			",
			
			'archive_numeric'	=> "CREATE TABLE {$prefixTables}archive_numeric (
										idarchive INTEGER UNSIGNED NOT NULL,
										name VARCHAR(255) NOT NULL,
										  idsite INTEGER UNSIGNED NULL,
										  date1 DATE NULL,
									  date2 DATE NULL,
										  period TINYINT UNSIGNED NULL,
									  ts_archived TIME NULL,
									  value FLOAT NULL,
									  PRIMARY KEY(idarchive, name)
									)
			",
			'archive_blob'	=> "CREATE TABLE {$prefixTables}archive_blob (
  idarchive INTEGER UNSIGNED NOT NULL,
  name VARCHAR(255) NOT NULL,
  idsite INTEGER UNSIGNED NULL,
  date1 DATE NULL,
  date2 DATE NULL,
  period TINYINT UNSIGNED NULL,
  ts_archived DATETIME NULL,
  value BLOB NULL,
  PRIMARY KEY(idarchive, name)
)
			",
		);
		return $tables;
	}
	
	static public function getCurrentUserLogin()
	{
		return Zend_Registry::get('access')->getIdentity();
	}
	
	// Accessible either to the user itself
	static public function checkUserIsSuperUserOrTheUser( $theUser )
	{
		try{
			if( Piwik::getCurrentUserLogin() !== $theUser)
			{
				// or to the super user
				Piwik::checkUserIsSuperUser();
			}
		} catch( Exception $e){
			throw new Exception("The user has to be either the Super User or the user '$theUser' itself.");
		}
	}
	
	static public function checkUserIsSuperUser()
	{
		Zend_Registry::get('access')->checkUserIsSuperUser();
	}
	
	static public function checkUserHasAdminAccess( $idSites )
	{
		Zend_Registry::get('access')->checkUserHasAdminAccess( $idSites );
	}
	static public function checkUserHasSomeAdminAccess()
	{
		Zend_Registry::get('access')->checkUserHasSomeAdminAccess();
	}
	
	static public function checkUserHasViewAccess( $idSites )
	{
		Zend_Registry::get('access')->checkUserHasViewAccess( $idSites );
	}
	
	static public function prefixClass( $class )
	{
		if(substr_count($class, Piwik::CLASSES_PREFIX) > 0)
		{
			return $class;
		}
		return Piwik::CLASSES_PREFIX.$class;
	}
	
	static public function createHtAccess( $path )
	{
		file_put_contents($path . ".htaccess", "Deny from all");
	}
	
	static public function mkdir( $path, $mode = 0755, $denyAccess = true )
	{
		$path = PIWIK_INCLUDE_PATH . '/' . $path;
		if(!is_dir($path))
		{
			mkdir($path, $mode, true);
		}
		
		if($denyAccess)
		{
			Piwik::createHtAccess($path);
		}
	}
	
	static public function prefixTable( $table )
	{
		$config = Zend_Registry::get('config');
		$prefixTables = $config->database->tables_prefix;
		return $prefixTables . $table;
	}
	
	static public function getTablesNames()
	{
		$aTables = array_keys(self::getTablesCreateSql());
		$config = Zend_Registry::get('config');
		$prefixTables = $config->database->tables_prefix;
		$return = array();
		foreach($aTables as $table)
		{
			$return[] = $prefixTables.$table;
		}
		return $return;
	}
	
	static public function getTablesInstalled()
	{
		$allMyTables = self::getTablesNames();

		$db = Zend_Registry::get('db');
		$config = Zend_Registry::get('config');
		$prefixTables = $config->database->tables_prefix;
		
		$allTables = $db->fetchCol("SHOW TABLES LIKE '$prefixTables%'");
				
		return $allTables;		
	}
	
	static public function createDatabase()
	{
		$db = Zend_Registry::get('db');
		$dbName = Zend_Registry::get('config')->database->dbname;
		$db->query("CREATE DATABASE IF NOT EXISTS ".$dbName);
	}
	
	static public function dropDatabase()
	{
		$db = Zend_Registry::get('db');
		$dbName = Zend_Registry::get('config')->database->dbname;
		$db->query("DROP DATABASE IF EXISTS ".$dbName);
	}
	
	
	static public function createDatabaseObject()
	{
		$config = Zend_Registry::get('config');
		$dbOptions = $config->database->toArray();
		$db = Zend_Db::factory($config->database->adapter, $dbOptions);
		Zend_Db_Table::setDefaultAdapter($db);
		Zend_Registry::set('db', $db);
	}

	static public function createLogObject()
	{
		require_once PIWIK_INCLUDE_PATH . "/modules/Log/APICall.php";
		require_once PIWIK_INCLUDE_PATH . "/modules/Log/Exception.php";
		require_once PIWIK_INCLUDE_PATH . "/modules/Log/Error.php";
		require_once PIWIK_INCLUDE_PATH . "/modules/Log/Message.php";
		
		$configAPI = Zend_Registry::get('config')->log;
		
		$aLoggers = array(
//				'logger_query_profile' => new Piwik_Log_QueryProfile, // TODO Piwik_Log_QueryProfile
				'logger_api_call' => new Piwik_Log_APICall,
				'logger_exception' => new Piwik_Log_Exception,
				'logger_error' => new Piwik_Log_Error,
				'logger_message' => new Piwik_Log_Message,
			);			
			
		foreach($configAPI as $loggerType => $aRecordTo)
		{
			if(isset($aLoggers[$loggerType]))
			{
				$logger = $aLoggers[$loggerType];
				
				foreach($aRecordTo as $recordTo)
				{
					switch($recordTo)
					{
						case 'screen':
							$logger->addWriteToScreen();
						break;
						
						case 'database':
							$logger->addWriteToDatabase();
						break;
						
						case 'file':
							$logger->addWriteToFile();		
						break;
						
						default:
							throw new Exception("TODO");
						break;
					}
				}
			}
		}
		
		foreach($aLoggers as $loggerType =>$logger)
		{
			if($logger->getWritersCount() == 0)
			{
				$logger->addWriteToNull();
			}
			Zend_Registry::set($loggerType, $logger);
		}
	}
	
	static public function createConfigObject( $pathConfigFile = null )
	{
		$config = new Piwik_Config($pathConfigFile);
		
		assert(count($config) != 0);
	}

	static public function dropTables( $doNotDelete)
	{
		$tablesAlreadyInstalled = self::getTablesInstalled();
		$db = Zend_Registry::get('db');
		
		foreach($tablesAlreadyInstalled as $tableName)
		{
			$doNotDeletePattern = "(".implode("|",$doNotDelete).")";
			if(!in_array($tableName,$doNotDelete)
				&& !ereg($doNotDeletePattern,$tableName)
				)
			{
				print("drop $tableName ");
				$db->query("DROP TABLE $tableName");
			}
		}			
	}
	
	static public function createTables()
	{
		$db = Zend_Registry::get('db');
		
		$config = Zend_Registry::get('config');
		$prefixTables = $config->database->tables_prefix;
		
		//Piwik::log("Creating ". implode(", ", self::getTablesNames()));
		
		$tablesToCreate = self::getTablesCreateSql();
		
		unset($tablesToCreate['archive_blob']);
		unset($tablesToCreate['archive_numeric']);
		
		$tablesAlreadyInstalled = self::getTablesInstalled();
		
		foreach($tablesToCreate as $tableName => $tableSql)
		{
			$tableName = $prefixTables . $tableName;

			// if the table doesn't exist already
			if(!in_array($tableName, $tablesAlreadyInstalled))
			{
				$db->query( $tableSql );
			}
		}
	}
	
	static public function uninstall()
	{
		// delete tables
				//create tables
		$db = Zend_Registry::get('db');
		
		Piwik::log("Droping ". implode(", ", self::getTablesNames()));
		
		$db->query( "DROP TABLE IF EXISTS ". implode(", ", self::getTablesNames()) );
	}
}