Session::getLinkpdo()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
nc 8
nop 0
dl 0
loc 20
c 0
b 0
f 0
cc 4
rs 9.9666
1
<?php
2
/**
3
 Copyright (C) 2018-2020 KANOUN Salim
4
 This program is free software; you can redistribute it and/or modify
5
 it under the terms of the Affero GNU General Public v.3 License as published by
6
 the Free Software Foundation;
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
 Affero GNU General Public Public for more details.
11
 You should have received a copy of the Affero GNU General Public Public along
12
 with this program; if not, write to the Free Software Foundation, Inc.,
13
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14
 */
15
16
/**
17
 * Open Sessions, load constants, instanciate dabase connexion, write text logs for all scripts
18
 */
19
20
use Monolog\Logger;
0 ignored issues
show
Bug introduced by
The type Monolog\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use Monolog\Handler\RotatingFileHandler;
0 ignored issues
show
Bug introduced by
The type Monolog\Handler\RotatingFileHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Monolog\Processor\WebProcessor;
0 ignored issues
show
Bug introduced by
The type Monolog\Processor\WebProcessor was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
/**
25
 * Methods that are call by all scripts
26
 */
27
Class Session {
28
    
29
	public static function checkSession(bool $log=true, bool $writeSession=false) {
30
        
31
		if (session_status() == PHP_SESSION_NONE) {
32
				session_start();
33
		}
34
        
35
		//Write logs
36
		if ($log) {
37
			isset($_POST['id_visit']) ? $logIdVisit=$_POST['id_visit'] : $logIdVisit='N/A';
38
			isset($_POST['patient_num']) ? $logPatientNum=$_POST['patient_num'] : $logPatientNum='N/A';
39
			@self::logInfo('Username : '.$_SESSION['username'].
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::logInfo('Username ...um: ' . $logPatientNum) targeting Session::logInfo() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for logInfo(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

39
			/** @scrutinizer ignore-unhandled */ @self::logInfo('Username : '.$_SESSION['username'].

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
40
				' Role: '.$_SESSION ['role'].' Study: '.$_SESSION['study'].' Visit ID: '.$logIdVisit.' Patient Num: '.$logPatientNum);
41
            
42
		}
43
44
		//Check session availability
45
		if (isset($_SESSION['LAST_ACTIVITY']) && (time()-$_SESSION['LAST_ACTIVITY'] > 1200)) {
46
			// last request was more than 30 minutes ago or unexisting
47
			session_unset(); // unset $_SESSION variable for the run-time
48
			session_destroy(); // destroy session data in storage
49
			self::redirectAndEndScript();
50
		}else if (empty($_SESSION)) {
51
			//if session already empty
52
			self::redirectAndEndScript();
53
		}else {
54
			$_SESSION['LAST_ACTIVITY']=time(); // update last activity time stamp
55
		}
56
        
57
		//If script dosen't need to write on session data, close write to free async ajax request
58
		if (!$writeSession) {
59
			session_write_close();
60
		}
61
        
62
	}
63
    
64
	/**
65
	 * Redirect to index and end script execution
66
	 */
67
	private static function redirectAndEndScript() {
68
		echo '<meta http-equiv="Refresh" content="0;/index.php">';
69
		exit("Session Lost");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
70
	}
71
    
72
	/**
73
	 * Instanciate a new PDO object for database connexion
74
	 * And Fill Php constant parameter
75
	 * @return PDO
76
	 */
77
	public static function getLinkpdo() {
78
        
79
		//Load the config file defining constants
80
		if (!defined('DATABASE_HOST')) {
81
			require_once($_SERVER["DOCUMENT_ROOT"].'/data/_config/config.inc.php');
82
		}
83
        
84
		//Instanciate PDO connexion with SSL or not
85
		if (DATABASE_SSL) {
0 ignored issues
show
Bug introduced by
The constant DATABASE_SSL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
86
			$linkpdo=new PDO('mysql:host='.DATABASE_HOST.';dbname='.DATABASE_NAME.';charset=UTF8', ''.DATABASE_USERNAME.'', ''.DATABASE_PASSWORD.'', self::getSSLPDOArrayOptions());    
0 ignored issues
show
Bug introduced by
The constant DATABASE_PASSWORD was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant DATABASE_NAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant DATABASE_USERNAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant DATABASE_HOST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
87
		}else {
88
			$linkpdo=new PDO('mysql:host='.DATABASE_HOST.';dbname='.DATABASE_NAME.';charset=UTF8', ''.DATABASE_USERNAME.'', ''.DATABASE_PASSWORD.'');   
89
		}
90
91
		$linkpdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
92
        
93
		//Load preferences from the database
94
		if (!defined('GAELO_PATIENT_CODE_LENGHT')) Session::loadPreferencesInConstants($linkpdo);
95
        
96
		return $linkpdo;
97
	}
98
    
99
	/**
100
	 * Options to use SSL connexion
101
	 * @return array
102
	 */
103
	public static function getSSLPDOArrayOptions() {
104
		$sslOptions=array(
105
			PDO::MYSQL_ATTR_SSL_CA => '',
106
			PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
107
		);
108
        
109
		return $sslOptions;
110
	}
111
    
112
	/**
113
	 * Write log in a daily log file in log folder
114
	 * @param string $stringInfo
115
	 */
116
	public static function logInfo(string $stringInfo) {
117
        
118
		if (is_writable($_SERVER["DOCUMENT_ROOT"].'/data/logs/')) {
119
			// create a log channel
120
			$log=new Logger('OpenTrialProcessor');
121
			$log->pushHandler(new RotatingFileHandler($_SERVER["DOCUMENT_ROOT"].'/data/logs/gaelO.log', Logger::INFO));
122
			$log->pushProcessor(new WebProcessor());
123
			$log->info($stringInfo);
124
		}else {
125
			error_log("Can't write logs folder");
126
		}
127
	}
128
    
129
	/**
130
	 * Store preference from the database in PHP constants
131
	 * @param PDO $linkpdo
132
	 */
133
	public static function loadPreferencesInConstants(PDO $linkpdo) {
134
        
135
		$connecter=$linkpdo->prepare('SELECT * FROM preferences');
136
		$connecter->execute();
137
        
138
		$result=$connecter->fetch(PDO::FETCH_ASSOC);
139
        
140
		define('GAELO_PATIENT_CODE_LENGHT', $result['patient_code_length']);
141
		define('GAELO_PLATEFORM_NAME', $result['name']);
142
		define('GAELO_ADMIN_EMAIL', $result['admin_email']);
143
		define('GAELO_REPLY_TO', $result['email_reply_to']);
144
		define('GAELO_CORPORATION', $result['corporation']);
145
		define('GAELO_WEB_ADDRESS', $result['address']);
146
		define('GAELO_DATE_FORMAT', $result['parse_date_import']);
147
		define('GAELO_COUNTRY_LANGUAGE', $result['parse_country_name']);
148
		
149
		define('TUS_SERVER', 'http://tus:1080');
150
151
		define('GAELO_ORTHANC_EXPOSED_INTERNAL_ADDRESS', $result['orthanc_exposed_internal_address']);
152
		define('GAELO_ORTHANC_EXPOSED_INTERNAL_PORT', $result['orthanc_exposed_internal_port']);
153
		define('GAELO_ORTHANC_EXPOSED_EXTERNAL_ADDRESS', $result['orthanc_exposed_external_address']);
154
		define('GAELO_ORTHANC_EXPOSED_EXTERNAL_PORT', $result['orthanc_exposed_external_port']);
155
		define('GAELO_ORTHANC_EXPOSED_INTERNAL_LOGIN', $result['orthanc_exposed_internal_login']);
156
		define('GAELO_ORTHANC_EXPOSED_INTERNAL_PASSWORD', $result['orthanc_exposed_internal_password']);
157
		define('GAELO_ORTHANC_EXPOSED_EXTERNAL_LOGIN', $result['orthanc_exposed_external_login']);
158
		define('GAELO_ORTHANC_EXPOSED_EXTERNAL_PASSWORD', $result['orthanc_exposed_external_password']);
159
        
160
		define('GAELO_ORTHANC_PACS_ADDRESS', $result['orthanc_pacs_address']);
161
		define('GAELO_ORTHANC_PACS_PORT', $result['orthanc_pacs_port']);
162
		define('GAELO_ORTHANC_PACS_LOGIN', $result['orthanc_pacs_login']);
163
		define('GAELO_ORTHANC_PACS_PASSWORD', $result['orthanc_pacs_password']);
164
        
165
		define('GAELO_USE_SMTP', $result['use_smtp']);
166
		define('GAELO_SMTP_HOST', $result['smtp_host']);
167
		define('GAELO_SMTP_PORT', $result['smtp_port']);
168
		define('GAELO_SMTP_USER', $result['smtp_user']);
169
		define('GAELO_SMTP_PASSWORD', $result['smtp_password']);
170
		define('GAELO_SMTP_SECURE', $result['smtp_secure']);
171
        
172
	}
173
174
}