/
System.php
646 lines (592 loc) · 19 KB
/
System.php
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<?php
/**
* DotBoost Technologies Inc.
* DotKernel Application Framework
*
* @category DotKernel
* @copyright Copyright (c) 2009-2016 DotBoost Technologies Inc. (http://www.dotboost.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @version $Id$
*/
/**
* System Model
* Here are all the actions related to the system settings
* @category DotKernel
* @package Admin
* @author DotKernel Team <team@dotkernel.com>
*/
class System extends Dot_Model
{
/**
* Ini Values
* @var array
*/
private $_ini;
/**
* Constructor
* @access public
*/
public function __construct()
{
parent::__construct();
}
/**
* Get MySQL Version
* @access public
* @return string
*/
public function getMysqlVersion()
{
$select = $this->db->select()
->from('', array('ve' => new Zend_Db_Expr('version()')));
return $this->db->fetchOne($select);
}
/**
* Get settings that are by default editable
* @access public
* @param int $isEditable [optional]
* @return array
*/
public function getSettings($isEditable='1')
{
$select = $this->db->select()
->from('setting')
->where('isEditable = ? ', $isEditable);
return $this->db->fetchAll($select);
}
/**
* Update settings
* @access public
* @param array $data
* @return void
*/
public function updateSettings($data)
{
foreach ($data as $k => $v)
{
$this->db->update('setting', array('value' => $v), $this->db->quoteIdentifier('key').' = '.$this->db->quote($k));
}
}
/**
* Get GeoIp Version release
* Return an array with keys "country" & "city"
* @access public
* @return array
*/
public function getGeoIpVersion()
{
$return = array('country' => '-', 'city' => '-', 'local' => '-');
// let's see the version of local .dat file
$geoipPath = $this->config->resources->geoip->path;
$geoipVersion = explode(" ", Dot_Geoip_Country::geoipDatabaseInfo($geoipPath));
$return['local'] = $geoipVersion[0] . ' ' . Dot_Kernel::TimeFormat($geoipVersion[1]);
// do we have geoIP server-wide ?
if(function_exists('geoip_database_info'))
{
if(geoip_db_avail(GEOIP_COUNTRY_EDITION))
{
$info = explode(" ",geoip_database_info(GEOIP_COUNTRY_EDITION));
$return['country'] = $info[0].' '.Dot_Kernel::TimeFormat($info[1]);
}
}
return $return;
}
/**
* Get Hostname
* Return a string
* @access public
* @return string
*/
public static function getSystemHostname()
{
if(version_compare(PHP_VERSION, '5.3.0', '>='))
{
$hostName = gethostname();
}
else
{
$hostName = php_uname('n');
}
return $hostName;
}
/**
* Get information about APC
* Returns an array with the following elements:
* name: string with the name of the extension: either old APC or new APCu
* version: a string with the version of APC that is currently installed or an empty string if it's not installed
* enabled: boolean
* @access public
* @return array
*/
public function getAPCInfo()
{
$result = array();
// check APC . First for APCu, then if is not present, check for old APC
$apcu= phpversion('apcu');
if($apcu)
{
$apcVersion = $apcu;
$result["name"] = 'APCu';
}
else
{
$apcVersion = phpversion('apc');
$result["name"] = 'APC';
}
$result["version"] = ($apcVersion===false) ? "" : $apcVersion;
$result["enabled"] = (function_exists('apc_cache_info') && (@apc_cache_info() !== false));
return $result;
}
/**
* Checks session-related configuration
* @access private
* @return array - empty if no errors were found
*/
private function _getSessionNotifications()
{
$errors = $warnings = $infos= array();
$messages = array();
$config = Zend_Registry::get('configuration');
$namespacePrefix = 'default';
if(isset($config->session->namespace_prefix) && is_string($config->session->namespace_prefix))
{
$namespacePrefix = $config->session->namespace_prefix;
}
$messageKey = 'session namespace prefix';
if($namespacePrefix == 'default')
{
$messages[$messageKey][] = 'Session prefix is not defined in application.ini ';
$messages[$messageKey][] = 'This might lead to undesired results when working with multiple DotKernel instances on the same server';
$messages[$messageKey][] = 'Please add <b>resources.session.namespace_prefix = "your_session_prefix"</b> in the application.ini file';
$messages[$messageKey]['namespacePrefix'] = 'Current namespace prefix is: ' . $namespacePrefix;
}
if($namespacePrefix == 'dotkernel')
{
$messages[$messageKey][] = 'Session prefix was not changed from its default ';
$messages[$messageKey][] = 'This might lead to undesired results when working with multiple DotKernel instances on the same server';
$messages[$messageKey][] = 'Please change <b>resources.session.namespace_prefix = "dot_kernel"</b> to <b>resources.session.namespace_prefix = "your_session_prefix"</b> in the application.ini file';
$messages[$messageKey]['namespacePrefix'] = 'Current namespace prefix is: ' . $namespacePrefix;
}
if(!isset($config->resources->session->save_path))
{
$warnings['session save_path'][] = 'Session prefix was not defined or changed from its default';
$warnings['session save_path'][] = 'This might lead to undesired results when working with multiple DotKernel instances on the same server';
$errors = $messages;
}
return array('error'=>$errors,'warning'=>$warnings,'info'=>$infos);
}
/**
* Get Cache Notifications
* @access private
* @return array
*/
private function _getCacheNotifications()
{
$errors = $warnings = $infos= array();
$cacheSimpleTest = Dot_Cache::testCache();
$cacheTagsTest = Dot_Cache::testTags();
$cacheInfo = Dot_Cache::getCacheInfo();
if($cacheInfo['config']['namespace'] == 'dotkernel' || $cacheInfo['config']['namespace'] == 'default')
{
$errors['Cache Namespace Warning'][] = 'Please change the cache namespace';
$errors['Cache Namespace Warning'][] = 'Cache might not work correctly if the namespace is dotkernel';
$errors['Cache Namespace Warning'][] = 'You can change it from the cache.namespace setting in application.ini';
}
if($cacheSimpleTest == true)
{
foreach($cacheInfo['config'] as $key => $value)
{
$infos['Cache Info'][] = $key . ' : ' . $value;
}
if($cacheTagsTest == true )
{
$infos['Cache Info'][] = 'tags are supported';
}
else
{
$warnings['Cache Test Failed'][] = 'Cache does not support tags';
$warnings['Cache Test Failed'][] = 'Check cache provider in application.ini';
$warnings['Cache Test Failed'][] = ''.
'More info: <a href="http://framework.zend.com/manual/1.12/en/zend.cache.backends.html" target="_blank"> ZF Cache Backends </a>';
}
}
else
{
$errors['Cache Test Failed'][] = 'Cache is not working or disabled';
$errors['Cache Test Failed'][] = 'Check cache settings or if cache module is supported';
$errors['Cache Test Failed'][] = ''.
'More info: <a href="http://www.dotkernel.com/dotkernel/caching-in-dotkernel-using-zend-framework/" target="_blank"> Caching in DotKernel</a>';
}
return array('error'=>$errors,'warning'=>$warnings,'info'=>$infos);
}
/**
* Get File-related Notifications
*
* Checks file permissions and files that should be deleted
* This will return "Make Writable", "Make Unwritable" & "Delete Files" warnings
*
* @access private
* @return array
*/
private function _getFileNotifications()
{
$errors = $warnings = $infos= array();
$errors['Make Writable'] = array();
$errors['Make Unwritable'] = array();
//ignore permission warning if OS is Windows
if(strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')
{
// warning if application.ini is writable
if(is_writable(APPLICATION_PATH . "/configs/application.ini"))
{
$errors["Make Unwritable"][] = 'configs/application.ini';
}
// only the folders set in application.ini (folders.permission[]) should be writable
$folderException = $this->config->folders->permission->toArray();
// go through all folders in the tree
$folders = $this->_listDirectory(APPLICATION_PATH);
foreach($folders as $path)
{
// exceptions are configured in application.ini. they should be writable
$isException = false;
foreach($folderException as $exception)
{
if(strpos($path, $exception) !== false)
{
$isException = true;
break;
}
}
if($isException)
{
if(! is_writable($path) && $path === $exception)
{
$errors["Make Writable"][] = $path;
}
}
else
{
if(is_writable($path))
{
$errors["Make Unwritable"][] = $path;
}
}
}
// info about how to add exception
if(count($errors["Make Unwritable"]))
{
$errors["Make Unwritable"][] = '** <em>It is possible to add your writable folders to the exclude list by adding it
as folders.permission[] exception in application.ini</em>';
}
}
// check for files that should be deleted
$filesToDelete = array("dot_kernel.sql", "readme.txt", "dk.php");
foreach($filesToDelete as $file)
{
if(file_exists(APPLICATION_PATH . "/" . $file))
{
$errors['Delete Files'][] = $file;
}
}
return array('error'=>$errors,'warning'=>$warnings,'info'=>$infos);
}
private function _getPluginNotifications()
{
$errors = $warnings = $infos= array();
// plugin check
$pluginHandler = Plugin_Loader::getInstance();
$pluginData = $pluginHandler->getAllPlugins();
foreach($pluginData as $plugin)
{
// check if the class is missing
if( ! $pluginHandler->pluginExists($plugin['vendor'], $plugin['pluginName']))
{
$errors['Plugin Check'][] = 'Plugin '. $plugin['pluginName'] . ' (by ' .$plugin['vendor']. ') is missing';
}
// check if the plugin is enabled
elseif( ! $plugin['enabled'])
{
$warnings['Plugin Check'][] = 'Plugin '. $plugin['pluginName'] . ' (by ' .$plugin['vendor']. ') is not enabled';
}
}
return array('error'=>$errors,'warning'=>$warnings,'info'=>$infos);
}
/**
* Checks the admin credentials and if his email or devEmails contain team@dotkernel.com
*
* Checks if the devEmails setting and admin account contain team@dotkernel.com
* Also checks if the password for "admin" is "dot"
*
* @access private
* @return array
*/
private function _getAdminNotifications()
{
$errors = $warnings = $infos= array();
// check that the default admin user isn't enabled
$dotAuth = Dot_Auth::getInstance();
$defaultAdminValid = $dotAuth->process('admin', array("username"=>"admin", "password"=>"dot"), $storeInSession = false);
if ($defaultAdminValid)
{
$errors["Security Warning"][] = "Please change the password of the oldest admin user or deactivate him";
}
// if the oldest admin have the same email team@dotkernel.com
$select = $this->db->select()->from('admin', 'email')->where('isActive = ?', '1')->order('dateCreated asc')->limit(1);
$emailAdmin = $this->db->fetchOne($select);
if('team@dotkernel.com' == $emailAdmin)
{
$errors["Debug Email"][] = "Please change the email of the default admin user or deactivate him.";
}
//if the devEmails is the default one : team@dotkernel.com
// why query db when we have it in the Dot_Model
if(stripos($this->settings->devEmails, 'team@dotkernel.com') !== false)
{
$errors["Debug Email"][] = "Update the setting.devEmails value to reflect your debug email.";
}
return array('error'=>$errors,'warning'=>$warnings,'info'=>$infos);
}
/**
* Get any messages to display in the dashboard
* Each array element returned is an array with two strings: type and description
* @access public
* @return array
*/
public function getNotifications()
{
$messages = array();
$adminMessages = $this->_getAdminNotifications();
$fileMessages = $this->_getFileNotifications();
$cacheMessages = $this->_getCacheNotifications();
$sessionMessages = $this->_getSessionNotifications();
$pluginMessages = $this->_getPluginNotifications();
// @todo: get proper data for collations
$collationMessages = $this->getCollationMessages();
$messages = array_merge_recursive($messages, $adminMessages, $fileMessages, $cacheMessages, $sessionMessages, $pluginMessages, $collationMessages );
return $messages;
}
/**
* Get collation for all text tables and columns
* @access public
* @param $databaseName [optional] database name, if not given, the main db
* @return array $collations - array with collations and charset for each table & column
*/
public function getTableColumnCollations($databaseName = null)
{
if($databaseName == null)
{
$databaseName = $this->config->database->params->dbname;
}
$properties = array(
'database' =>'TABLE_SCHEMA' ,
'table' =>'TABLE_NAME' ,
'column' =>'COLUMN_NAME' ,
'charset' =>'CHARACTER_SET_NAME',
'collation' =>'COLLATION_NAME'
);
$select = $this->db->select()
->from('INFORMATION_SCHEMA.COLUMNS', $properties)
->where('TABLE_SCHEMA =?', $databaseName)
// ignore numeric values they have no collation
->where('COLLATION_NAME IS NOT NULL');
return $this->db->fetchAll($select);
}
/**
* Get all used collations
* @access public
* @param $databaseName [optional] database name, if not given, the main db
* @return array $collations - array with collations and charset for each table & column
*/
public function getCollations($databaseName = null)
{
if($databaseName == null)
{
$databaseName = $this->config->database->params->dbname;
}
$properties = array(
'database' =>'TABLE_SCHEMA' ,
'charset' =>'CHARACTER_SET_NAME',
'collation' =>'COLLATION_NAME'
);
$select = $this->db->select()
->from('INFORMATION_SCHEMA.COLUMNS', $properties)
->where('TABLE_SCHEMA =?', $databaseName)
// ignore numeric values they have no collation
->where('COLLATION_NAME IS NOT NULL')
->group('COLLATION_NAME');
return $this->db->fetchAll($select);
}
/**
* Organize collations in $v[db][table][column] structure
*
* @param array $collationList
* @return array $organizedCollations
*/
public function organizeCollations($collationList)
{
foreach($collationList as $c)
{
// used $c instead of $colation for the sake of code understandability and look
$organizedCollations[$c['database']][$c['table']][$c['column']] = $c;
unset(
$organizedCollations[$c['database']][$c['table']][$c['column']]['database'],
$organizedCollations[$c['database']][$c['table']][$c['column']]['table'],
$organizedCollations[$c['database']][$c['table']][$c['column']]['column']
);
}
return $organizedCollations;
}
/**
* Check Database Collation
*
* @access private
* @return bool $isCollationValid
*/
private function _isDbCollationValid()
{
$charset = $this->config->database->params->charset;
$databaseName = $this->config->database->params->dbname;
$collations = $this->getCollations();
if(count($collations) == 1)
{
if(stripos($collations[0]['charset'], $charset) !== FALSE && strtolower($collations[0]['charset']) == strtolower($charset))
{
// everything looks ok
return true;
}
}
return false;
}
/**
* Get Collation Related Messages
* @access public
* @return array
*/
public function getCollationMessages()
{
$errors = $warnings = $infos= array();
$collationList = $this->getCollations();
if($this->_isDbCollationValid())
{
$infos['Connection Charset OK'][] = 'Detected charset: ' . $collationList[0]['charset'];
$infos['Connection Charset OK'][] = 'Detected collation: '. $collationList[0]['collation'];
}
else
{
$charset = $this->config->database->params->charset;
$presentCollations = $this->getCollations();
$errors['DB Charset Conflict'][] = 'Connection charset: '. $charset;
$errors['DB Charset Conflict'][] = 'Detected charsets: ' ;
foreach($collationList as $collation)
{
$errors['DB Charset Conflict'][] = $collation['charset'];
}
$errors['DB Charset Conflict'][] = 'Using multiple charsets for the same DB connection are not recommended' ;
}
return array('error'=>$errors,'warning'=>$warnings,'info'=>$infos);
}
/**
* Get a list of files in a directory
* @access public
* @param string $directory
* @return array
*/
private function _listDirectory($directory)
{
$result = array();
if ($handle = opendir($directory))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && $file != ".svn")
{
$dir = $directory.'/'.$file;
if(is_dir($dir))
{
$result[] = $dir;
$list = $this->_listDirectory($dir);
$result = array_merge($result, $list);
}
}
}
closedir($handle);
}
return $result;
}
/**
* Check if the developer e-mail is team@dotkernel.com (or the one provided)
* @access private
* @param $invalidEmailList [optional] / "team@dotkernel.com"
* @return bool
*/
private function _checkDevEmails($invalidDevEmails = 'team@dotkernel.com')
{
if(is_string($invalidDevEmails))
{
// @todo: check if an array
// @todo: write in the flight log
}
return ($this->settings->devEmails == $invalidDevEmails);
}
/**
* Get current php ini values
*
* This function is an alias to ini_get_all()
*
* @param $environment [optional] - global by default
* @return boolean|array()
*/
private function _getPhpIniValues($scope = 'global')
{
// global means the values from the php.ini file
// local means locally declared values (the ones set with ini_set($key,$val) )
$this->_ini = ini_get_all();
$newArray = array();
switch($scope)
{
default:
return false;
// If called from within a function, the return() statement immediately ends execution of the current function
// so no breaks -- only returns
case 'local':
case 'global':
foreach($this->_ini as $key => $value)
{
$newArray[$key] = $value[$scope.'_value'];
}
return $newArray;
}
}
/**
* Get the Ini files with recommended correction
*
* The default scope is local because DotKernel changes some of the
* ini vars, see /configs/application.ini
*
* @param string $scope
* @return multitype:|Ambigous array
*/
public function getIniValuesWithCorrection($scope = 'local')
{
$iniValues = array();
// Recommended Values - multi-env
$goodIniValues = $this->option->recommendedPhpIniValues->toArray();
$recommendedIniValues = array_replace($goodIniValues['production'], $goodIniValues[APPLICATION_ENV]);
// Current Values
$allIniValues = $this->_getPhpIniValues($scope);
// removing correct values from current ini values
$currentIniValues = array_intersect_key($allIniValues, $recommendedIniValues);
// making sure we only have the needed values, not all of them
$recommendedIniValues = array_intersect_key($recommendedIniValues, $currentIniValues);
foreach($currentIniValues as $key => $value)
{
// $value <=> $currentIniValues[$key]
if($recommendedIniValues[$key] != $currentIniValues[$key] )
{
$iniValues[$key]['recommended'] = $recommendedIniValues[$key];
$iniValues[$key]['current'] = $value ;
$iniValues[$key]['access'] = $this->_ini[$key]['access'];
}
}
return $iniValues;
}
}