orangehrm /installer/applicationSetup.php

Language PHP Lines 447
MD5 Hash 7c117fd7a92b45e4e148d186f6f9fee0 Estimated Cost $8,381 (why?)
Repository https://bitbucket.org/wildanm/orangehrm View Raw File View Project SPDX
  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
<?php
/**
 * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
 * all the essential functionalities required for any enterprise.
 * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
 *
 * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA
 *
 */

require_once ROOT_PATH.'/lib/common/UniqueIDGenerator.php';

// Installing
function createDB() {

	if ($_SESSION['dbCreateMethod'] == 'existing') { // If the user wants to use an existing empty database

		$dbName = $_SESSION['dbInfo']['dbName'];
		$dbHost = $_SESSION['dbInfo']['dbHostName'];
		$dbPort = $_SESSION['dbInfo']['dbHostPort'];
		$dbUser = $_SESSION['dbInfo']['dbUserName'];
		$dbPassword = $_SESSION['dbInfo']['dbPassword'];

		if (mysql_connect($dbHost.':'.$dbPort, $dbUser, $dbPassword)) {

			if (mysql_select_db($dbName)) {

				$result = mysql_query("SHOW TABLES");

				if (mysql_num_rows($result) > 0) {
					$_SESSION['error'] = 'Given database is not empty.';
				}

			} else {
			    $_SESSION['error'] = 'Cannot connect to the database. '.mysql_error();
			}

		} else {
		    $_SESSION['error'] = 'Cannot make a database connection using given details. '.mysql_error();
		}

	} elseif ($_SESSION['dbCreateMethod'] == 'new') { // If the user wants OrangeHRM to create the database for him

		connectDB();
		$dbName = '`'.$_SESSION['dbInfo']['dbName'].'`';
		mysql_query("CREATE DATABASE " . $dbName);

		if(!@mysql_select_db($_SESSION['dbInfo']['dbName'])) {
			$mysqlErrNo = mysql_errno();

			$errorMsg = mysql_error();
			if(!isset($errorMsg) || $errorMsg == '') {
				$errorMsg = 'Unable to create Database.';
			}

			if (isset($mysqlErrNo)) {
				if ($mysqlErrNo == '1102') {
					$errorMsg .= '. Please use valid name for database.';
				}
			}

			$_SESSION['error'] = $errorMsg.' '.mysql_error();

			return;
		}

	}

}

function connectDB() {

	if(!@mysql_connect($_SESSION['dbInfo']['dbHostName'].':'.$_SESSION['dbInfo']['dbHostPort'], 		$_SESSION['dbInfo']['dbUserName'], $_SESSION['dbInfo']['dbPassword'])) {
		$_SESSION['error'] =  'Database Connection Error!';
		return;
	}

}

/**
 * Initialize unique ID's
 */
function initUniqueIDs() {
	connectDB();

	if(!mysql_select_db($_SESSION['dbInfo']['dbName'])) {
		$_SESSION['error'] = 'Unable to connect to Database!';
		error_log (date("r")." Initializing unique id's. Error - Unable to connect to Database\n",3, "installer/log.txt");
		return false;
	}

	/* Initialize the hs_hr_unique_id table */
	try {
		UniqueIDGenerator::getInstance()->initTable();
	} catch (IDGeneratorException $e) {
		$errMsg = $e->getMessage() . ". Trace = " . $e->getTraceAsString();
		$_SESSION['error'] = $errMsg;
		error_log (date("r")." Initializing hs_hr_unique_id table failed with: $errMsg\n",3, "log.txt");
		return false;
	}
	return true;
}

function fillData($phase=1, $source='/dbscript/dbscript-') {
	$source .= $phase.'.sql';
	connectDB();

	error_log (date("r")." Fill Data Phase $phase - Connected to the DB Server\n",3, "installer/log.txt");

	if(!mysql_select_db($_SESSION['dbInfo']['dbName'])) {
		$_SESSION['error'] = 'Cannot select the given database! '.mysql_error();
		error_log (date("r")." Fill Data Phase $phase - Error - Cannot select the given database\n",3, "installer/log.txt");
		return;
	}

	error_log (date("r")." Fill Data Phase $phase - Selected the DB\n",3, "installer/log.txt");
	error_log (date("r")." Fill Data Phase $phase - Reading DB Script\n",3, "installer/log.txt");

	$queryFile = ROOT_PATH . $source;
	$fp    = fopen($queryFile, 'r');

	error_log (date("r")." Fill Data Phase $phase - Opened DB Script\n",3, "installer/log.txt");

	$query = fread($fp, filesize($queryFile));
	fclose($fp);

	error_log (date("r")." Fill Data Phase $phase - Read DB script\n",3, "installer/log.txt");

	$dbScriptStatements = explode(";", $query);

	error_log (date("r")." Fill Data Phase $phase - There are ".count($dbScriptStatements)." Statements in the DB script\n",3, "installer/log.txt");

	for($c=0;(count($dbScriptStatements)-1)>$c;$c++)
		if(!@mysql_query($dbScriptStatements[$c])) {
			$_SESSION['error'] = mysql_error();
			$error = mysql_error();
			error_log (date("r")." Fill Data Phase $phase - Error Statement # $c \n",3, "installer/log.txt");
			error_log (date("r")." ".$dbScriptStatements[$c]."\n",3, "installer/log.txt");
			return;
		}

	if(isset($error))
		return;
}

function createDBUser() {

if ($_SESSION['dbCreateMethod'] == 'new') {

	connectDB();

	if(isset($_SESSION['dbInfo']['dbOHRMUserName'])) {

		$dbName = $_SESSION['dbInfo']['dbName'];
		$dbOHRMUser = $_SESSION['dbInfo']['dbOHRMUserName'];
		$dbOHRMPassword = $_SESSION['dbInfo']['dbOHRMPassword'];

		$querryIdentifiedBy = (isset($dbOHRMPassword) && ($dbOHRMPassword !== ''))? "IDENTIFIED BY '$dbOHRMPassword'": '';


      	$query = <<< USRSQL
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, INDEX
ON `$dbName`.*
TO "$dbOHRMUser"@"localhost"
$querryIdentifiedBy;
USRSQL;

      	if(!@mysql_query($query)) {
         	$_SESSION['error'] = mysql_error() or die();
         	return;
      	}

      	$query = <<< USRSQL
set password for "$dbOHRMUser"@"localhost"
 = old_password('$dbOHRMPassword');
USRSQL;

		if (isset($dbOHRMPassword) && ($dbOHRMPassword !== '')) {
      		if (!@mysql_query($query)) {
        		$_SESSION['error'] = mysql_error() or die();
         		return;
      		}
		}

	  	$dbName = $_SESSION['dbInfo']['dbName'];
	  	$dbOHRMUser = $_SESSION['dbInfo']['dbOHRMUserName'];
		$dbOHRMPassword = $_SESSION['dbInfo']['dbOHRMPassword'];

      	$query = <<< USRSQL
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, INDEX
ON `$dbName`.*
TO "$dbOHRMUser"@"%"
$querryIdentifiedBy;
USRSQL;

      	if(!@mysql_query($query)) {
         	$_SESSION['error'] = mysql_error() or die();
         	return;
      	}

      	$query = <<< USRSQL
set password for "$dbOHRMUser"@"%"
 = old_password('$dbOHRMPassword');
USRSQL;

		if (isset($dbOHRMPassword) && ($dbOHRMPassword !== '')) {
      		if(!@mysql_query($query)) {
         		$_SESSION['error'] = mysql_error() or die();
         		return;
      		}
		}
	}

}

}

function createUser() {

	connectDB();

	if(!@mysql_select_db($_SESSION['dbInfo']['dbName'])) {
		$_SESSION['error'] = 'Unable to access OrangeHRM Database!';
		return;
	}

	$query = "INSERT INTO `hs_hr_users` (`id`, `user_name`, `user_password`, `first_name`, `last_name`, `emp_number`, `user_hash`, `is_admin`, `receive_notification`, `description`, `modified_user_id`, `created_by`, `title`, `department`, `phone_home`, `phone_mobile`,   `phone_work`, `phone_other`, `phone_fax`, `email1`, `email2`, `status`, `address_street`, `address_city`, `address_state`,  `address_country`, `address_postalcode`, `user_preferences`, `deleted`, `employee_status`, `userg_id`) VALUES ('USR001','" .$_SESSION['defUser']['AdminUserName']. "','".md5($_SESSION['defUser']['AdminPassword'])."','Admin','',null,'','Yes','1','',null,null,'','','','','','','','','','Enabled','','','','','','',0,'','USG001')";

	if(!mysql_query($query)) {
		$_SESSION['error'] = 'Unable to Create OrangeHRM Admin User Account';
		return;
	}

}

function writeConfFile() {

	$dbHost = $_SESSION['dbInfo']['dbHostName'];
	$dbHostPort = $_SESSION['dbInfo']['dbHostPort'];
	$dbName = $_SESSION['dbInfo']['dbName'];

	if(isset($_SESSION['dbInfo']['dbOHRMUserName'])) {
		$dbOHRMUser = $_SESSION['dbInfo']['dbOHRMUserName'];
		$dbOHRMPassword = $_SESSION['dbInfo']['dbOHRMPassword'];
	} else {
		$dbOHRMUser = $_SESSION['dbInfo']['dbUserName'];
		$dbOHRMPassword = $_SESSION['dbInfo']['dbPassword'];
	}

    $confContent = <<< CONFCONT
<?php
class Conf {

	var \$smtphost;
	var \$dbhost;
	var \$dbport;
	var \$dbname;
	var \$dbuser;
	var \$version;

	function Conf() {

		\$this->dbhost	= '$dbHost';
		\$this->dbport 	= '$dbHostPort';
		if(defined('ENVIRNOMENT') && ENVIRNOMENT == 'test'){
		\$this->dbname    = 'test_$dbName';		
		}else {
		\$this->dbname    = '$dbName';
		}
		\$this->dbuser    = '$dbOHRMUser';
		\$this->dbpass	= '$dbOHRMPassword';
		\$this->version = '2.6-beta.5';

		\$this->emailConfiguration = dirname(__FILE__).'/mailConf.php';
		\$this->errorLog =  realpath(dirname(__FILE__).'/../logs/').'/';
	}
}
?>
CONFCONT;

	$filename = ROOT_PATH . '/lib/confs/Conf.php';
	$handle = fopen($filename, 'w');
	fwrite($handle, $confContent);

    fclose($handle);

}

function writeSymfonyDbConfigFile() {

	$dbHost = $_SESSION['dbInfo']['dbHostName'];
	$dbHostPort = $_SESSION['dbInfo']['dbHostPort'];
	$dbName = $_SESSION['dbInfo']['dbName'];

	if(isset($_SESSION['dbInfo']['dbOHRMUserName'])) {
		$dbOHRMUser = $_SESSION['dbInfo']['dbOHRMUserName'];
		$dbOHRMPassword = $_SESSION['dbInfo']['dbOHRMPassword'];
	} else {
		$dbOHRMUser = $_SESSION['dbInfo']['dbUserName'];
		$dbOHRMPassword = $_SESSION['dbInfo']['dbPassword'];
	}
	
    $confContent = <<< CONFCONT
all:
  doctrine:
    class: sfDoctrineDatabase
    param:
      dsn: 'mysql:host=$dbHost;dbname=$dbName'
      username: $dbOHRMUser
      password: $dbOHRMPassword
      port: $dbHostPort
      attributes: { export: tables }
test:
  doctrine:
    class: sfDoctrineDatabase
    param:
      dsn: 'mysql:host=$dbHost;dbname=test_$dbName'
      username: $dbOHRMUser
      password: $dbOHRMPassword
      port: $dbHostPort
CONFCONT;

	$filename = ROOT_PATH . '/symfony/config/databases.yml';
	$handle = fopen($filename, 'w');
	fwrite($handle, $confContent);

    fclose($handle);

}

function writeLog() {
	$Content = "Client Info\n\n";

	$Content .= "User Agent : ".$_SERVER['HTTP_USER_AGENT']."\n";
	$Content .= "Remote Address : ".$_SERVER['REMOTE_ADDR']."\n\n";

	$Content .= "Server Info\n\n";
	$Content .= "Host : ".$_SERVER['HTTP_HOST']."\n";
	$Content .= "PHP Version : ".constant('PHP_VERSION')."\n";
	$Content .= "Server : ".$_SERVER['SERVER_SOFTWARE']."\n";
	$Content .= "Admin : ".$_SERVER['SERVER_ADMIN']."\n\n";

	$Content .= "Document Root : ".$_SERVER['DOCUMENT_ROOT']."\n";
	$Content .= "ROOT_PATH : ".ROOT_PATH."\n\n";

	$Content .= "OrangeHRM Installation Log\n\n";

	$filename = 'installer/log.txt';
	$handle = fopen($filename, 'w');
	fwrite($handle, $Content);
	fclose($handle);
}

   if (isset($_SESSION['INSTALLING'])) {
	switch ($_SESSION['INSTALLING']) {
		case 0	:	writeLog();
					error_log (date("r")." DB Creation - Starting\n",3, "installer/log.txt");
					createDB();
					error_log (date("r")." DB Creation - Done\n",3, "installer/log.txt");
					if (!isset($error) || !isset($_SESSION['error'])) {
						$_SESSION['INSTALLING'] = 1;
						error_log (date("r")." DB Creation - No Errors\n",3, "installer/log.txt");
					} else {
						error_log (date("r")." DB Creation - Errors\n",3, "installer/log.txt");
						error_log (date("r")." ".(isset($error)? $error: $_SESSION['error'])."\n",3, "installer/log.txt");
					}

					break;

		case 1	:	error_log (date("r")." Fill Data Phase 1 - Starting\n",3, "installer/log.txt");
					fillData();
					error_log (date("r")." Fill Data Phase 1 - Done\n",3, "installer/log.txt");
					if (!isset($error) || !isset($_SESSION['error'])) {
						$_SESSION['INSTALLING'] = 2;
						error_log (date("r")." Fill Data Phase 1 - No Errors\n",3, "installer/log.txt");
					} else {
						error_log (date("r")." Fill Data Phase 1 - Errors\n",3, "installer/log.txt");
						error_log (date("r")." ".(isset($error)? $error: $_SESSION['error'])."\n",3, "installer/log.txt");
					}
					break;

		case 2	:	error_log (date("r")." Fill Data Phase 2 - Starting\n",3, "installer/log.txt");
					fillData(2);
					error_log (date("r")." Fill Data Phase 2 - Done\n",3, "installer/log.txt");
					if (!isset($error) || !isset($_SESSION['error'])) {
						$res = initUniqueIDs();
						if ($res) {
							$_SESSION['INSTALLING'] = 3;
							error_log (date("r")." Fill Data Phase 2 - No Errors\n",3, "installer/log.txt");
						}
					} else {
						error_log (date("r")." Fill Data Phase 2 - Errors\n",3, "installer/log.txt");
						error_log (date("r")." ".(isset($error)? $error: $_SESSION['error'])."\n",3, "installer/log.txt");
					}
					break;

		case 3	:	error_log (date("r")." Create DB user - Starting\n",3, "installer/log.txt");
					createDBUser();
					error_log (date("r")." Create DB user - Done\n",3, "installer/log.txt");
					if (!isset($error) || !isset($_SESSION['error'])) {
						$_SESSION['INSTALLING'] = 4;
						error_log (date("r")." Create DB user - No Errors\n",3, "installer/log.txt");
					} else {
						error_log (date("r")." Create DB user - Errors\n",3, "installer/log.txt");
						error_log (date("r")." ".(isset($error)? $error: $_SESSION['error'])."\n",3, "installer/log.txt");
					}
					break;

		case 4	:	error_log (date("r")." Create OrangeHRM user - Starting\n",3, "installer/log.txt");
					createUser();
					error_log (date("r")." Create OrangeHRM user - Done\n",3, "installer/log.txt");
					if (!isset($error) || !isset($_SESSION['error'])) {
						$_SESSION['INSTALLING'] = 5;
						error_log (date("r")." Create OrangeHRM user - No Errors\n",3, "installer/log.txt");
					} else {
						error_log (date("r")." Create OrangeHRM user - Errors\n",3, "installer/log.txt");
						error_log (date("r")." ".(isset($error)? $error: $_SESSION['error'])."\n",3, "installer/log.txt");
					}
					break;

		case 5 :	error_log (date("r")." Write Conf - Starting\n",3, "installer/log.txt");
					writeConfFile();
					writeSymfonyDbConfigFile();
					error_log (date("r")." Write Conf - Done\n",3, "installer/log.txt");
					if (!isset($error) || !isset($_SESSION['error'])) {
						$_SESSION['INSTALLING'] = 6;
						error_log (date("r")." Write Conf - No Errors\n",3, "installer/log.txt");
					} else {
						error_log (date("r")." Write Conf - Errors\n",3, "installer/log.txt");
						error_log (date("r")." ".(isset($error)? $error: $_SESSION['error'])."\n",3, "installer/log.txt");
					}
					break;

	}
  }
?>
Back to Top