forked from CarlosMC/OsTicket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.php
259 lines (237 loc) · 13.2 KB
/
install.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
<?php
/*********************************************************************
install.php
osTicket Installer.
Peter Rotich <[email protected]>
Copyright (c) 2006-2010 osTicket
http:https://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
$Id: $
**********************************************************************/
#inits
error_reporting(E_ALL ^ E_NOTICE); //turn on errors
ini_set('magic_quotes_gpc', 0);
ini_set('session.use_trans_sid', 0);
ini_set('session.cache_limiter', 'nocache');
ini_set('display_errors',1); //We want the user to see errors during install process.
ini_set('display_startup_errors',1);
#start session
session_start();
require('setup.inc.php');
$errors=array();
$fp=null;
$_SESSION['abort']=false;
define('VERSION','1.6 ST'); //Current version number
define('VERSION_VERBOSE','1.6 Stable'); //What the user sees during installation process.
define('CONFIGFILE','../include/ost-config.php'); //osTicket config file full path.
// TODO (issue 6)insert here code to select language to install
// how use a constant, need redefine the constant if
//language change on first instaltion screen
define('SCHEMAFILE','./inc/osticket-v1.6.sql'); //osTicket SQL schema.
define('URL',rtrim('http'.(($_SERVER['HTTPS']=='on')?'s':'').':https://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']),'setup'));
$install='<strong>Need help?</strong> <a href="http:https://www.osticket.com/support/" target="_blank">Professional Installation Available</a>';
$support='<strong>Get a peace of mind</strong> <a href="http:https://www.osticket.com/support/" target="_blank">Commercial Support Available</a>';
//Basic checks
$inc='install.inc.php';
$info=$install;
if(file_exists('../ostconfig.php') || file_exists('../include/settings.php')) { //old installation? try upgrading it buddy.
header('Location: upgrade.php');
die('Old installation exists....try upgrade instead');
}elseif((double)phpversion()<4.3){ //Old PHP installation
$errors['err']='PHP installation seriously out of date';
$inc='php.inc.php';
}elseif(!ini_get('short_open_tag')) {
$errors['err']='Short open tag disabled! - osTicket requires it turned on.';
$inc='shortopentag.inc.php';
}elseif(!file_exists(CONFIGFILE)) {
$errors['err']=sprintf('Configuration file (%s) missing!',basename(CONFIGFILE));
$inc='missing.inc.php';
}elseif(($cFile=file_get_contents(CONFIGFILE)) && preg_match("/define\('OSTINSTALLED',TRUE\)\;/i",$cFile)){
$errors['err']='Configuration file already modified!';
$inc='unclean.inc.php';
}elseif(!file_exists(CONFIGFILE) || !is_writable(CONFIGFILE)) { //writable config file??
clearstatcache();
$errors['err']='Configuration file is not writable';
$inc='chmod.inc.php';
}else {
$configfile=file_get_contents(CONFIGFILE); //Get the goodies...peek and tell.
//Make SURE this is a new installation.
if(preg_match("/define\('OSTINSTALLED',TRUE\)\;/i",$configfile) || !strpos($configfile,'%CONFIG-DBHOST')){
$errors['err']='Configuration file already modified!';
$inc='unclean.inc.php';
}elseif($_POST){
$f=array();
$f['ostlang'] = array('type'=>'string', 'required'=>1, 'error'=>'OsTicket Language is Required');
$f['title'] = array('type'=>'string', 'required'=>1, 'error'=>'Title required');
$f['sysemail'] = array('type'=>'email', 'required'=>1, 'error'=>'Valid email required');
$f['username'] = array('type'=>'username', 'required'=>1, 'error'=>'Username required');
$f['password'] = array('type'=>'password', 'required'=>1, 'error'=>'Password required');
$f['password2'] = array('type'=>'password', 'required'=>1, 'error'=>'Confirm password');
$f['email'] = array('type'=>'email', 'required'=>1, 'error'=>'Valid email required');
$f['dbhost'] = array('type'=>'string', 'required'=>1, 'error'=>'Hostname required');
$f['dbname'] = array('type'=>'string', 'required'=>1, 'error'=>'Database name required');
$f['dbuser'] = array('type'=>'string', 'required'=>1, 'error'=>'Username required');
$f['dbpass'] = array('type'=>'string', 'required'=>1, 'error'=>'password required');
$f['prefix'] = array('type'=>'string', 'required'=>1, 'error'=>'Table prefix required');
$validate = new Validator($f);
if(!$validate->validate($_POST)){
$errors=array_merge($errors,$validate->errors());
}
if($_POST['sysemail'] && $_POST['email'] && !strcasecmp($_POST['sysemail'],$_POST['email']))
$errors['email']='Conflicts with system email above';
if(!$errors && strcasecmp($_POST['password'],$_POST['password2']))
$errors['password2']='passwords to not match!';
//Check table prefix underscore required at the end!
if($_POST['prefix'] && substr($_POST['prefix'], -1)!='_')
$errors['prefix']='Bad prefix. Must have underscore (_) at the end. e.g \'ost_\'';
//Make sure admin username is not very predictable.
if(!$errors['username'] && in_array(strtolower($_POST['username']),array('admin','admins','username','osticket')))
$errors['username']='Bad username';
//Connect to the DB
if(!$errors && !db_connect($_POST['dbhost'],$_POST['dbuser'],$_POST['dbpass']))
$errors['mysql']='Unable to connect to MySQL server. Possibly invalid login info. <br>';
//check mysql version
if(!$errors && (db_version()<'4.1.1'))
$errors['mysql']='osTicket requires MySQL 4.1.1 or better! Please upgrade';
//Select the DB
if(!$errors && !db_select_database($_POST['dbname'])) {
//Try creating the missing DB
if(!mysql_query('CREATE DATABASE '.$_POST['dbname'].' DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci')) {
$errors['dbname']='Database doesn\'t exist';
$errors['mysql']='Unable to create the database due to permission';
}elseif(!db_select_database($_POST['dbname'])) {
$errors['dbname']='Unable to select the database';
}
}
//Get database schema
// TODO (issue 6)
if(!$errors && !file_exists(SCHEMAFILE)) {
$errors['err']='Internal error. Please make sure your download is the latest';
$errors['mysql']='Missing SQL schema file';
}
//Open the file for writing..
if(!$errors && !($fp = @fopen(CONFIGFILE,'r+'))){
$errors['err']='Unable to open config file for writing. Permission denied!';
}
//IF no errors..Do the install. Let the fun start...
if(!$errors && $fp) {
define('ADMIN_EMAIL',$_POST['email']); //Needed to report SQL errors during install.
define('PREFIX',$_POST['prefix']); //Table prefix
$debug=false; //Change it to true to show failed query
// TODO (issue 6)
if(!load_sql_schema(SCHEMAFILE,$errors,$debug) && !$errors['err'])
$errors['err']='Error parsing SQL schema! Get help from developers';
if(!$errors) {
$info=$support;
//Rewrite the config file.
$configfile= str_replace('%CONFIG-OSTLANG',$_POST['ostlang'],$configfile);
$configfile= str_replace("define('OSTINSTALLED',FALSE);","define('OSTINSTALLED',TRUE);",$configfile);
$configfile= str_replace('%ADMIN-EMAIL',$_POST['email'],$configfile);
$configfile= str_replace('%CONFIG-DBHOST',$_POST['dbhost'],$configfile);
$configfile= str_replace('%CONFIG-DBNAME',$_POST['dbname'],$configfile);
$configfile= str_replace('%CONFIG-DBUSER',$_POST['dbuser'],$configfile);
$configfile= str_replace('%CONFIG-DBPASS',$_POST['dbpass'],$configfile);
$configfile= str_replace('%CONFIG-PREFIX',$_POST['prefix'],$configfile);
$configfile= str_replace('%CONFIG-SIRI',Misc::randcode(32),$configfile);
if(ftruncate($fp,0) && fwrite($fp,$configfile)){
//Some more configurations.
$tzoffset= date("Z")/3600; //Server's offset.
//Create admin user. Dummy last name.
$sql='INSERT INTO '.PREFIX.'staff SET created=NOW(), isadmin=1,change_passwd=0,group_id=1,dept_id=1 '.
',email='.db_input($_POST['email']).',firstname='.db_input('Admin').',lastname='.db_input('Admin').
',username='.db_input($_POST['username']).',passwd='.db_input(MD5($_POST['password'])).
',timezone_offset='.db_input($tzoffset);
mysql_query($sql);
//Add emails - hopefully the domain is actually valid
list($uname,$domain)=explode('@',$_POST['sysemail']);
//1 - main support email
$sql='INSERT INTO '.PREFIX.'email SET created=NOW(),updated=NOW(),priority_id=2,dept_id=1'.
',name='.db_input('Support').',email='.db_input($_POST['sysemail']);
mysql_query($sql);
//2 - alert email
$sql='INSERT INTO '.PREFIX.'email SET created=NOW(),updated=NOW(),priority_id=1,dept_id=1'.
',name='.db_input('osTicket Alerts').',email='.db_input('alerts@'.$domain);
mysql_query($sql);
//3 - noreply email
$sql='INSERT INTO '.PREFIX.'email SET created=NOW(),updated=NOW(),priority_id=1,dept_id=1'.
',name='.db_input('').',email='.db_input('noreply@'.$domain);
mysql_query($sql);
//config info
$sql='INSERT INTO '.PREFIX.'config SET updated=NOW() '.
',isonline=0,default_email_id=1,alert_email_id=2,default_dept_id=1,default_template_id=1'.
',timezone_offset='.db_input($tzoffset).
',ostversion='.db_input(VERSION).
',admin_email='.db_input($_POST['email']).
',helpdesk_url='.db_input(URL).
',helpdesk_title='.db_input($_POST['title']);
mysql_query($sql);
//Create a ticket to make the system warm and happy.
$sql='INSERT INTO '.PREFIX.'ticket SET created=NOW(),ticketID='.db_input(Misc::randNumber(6)).
",priority_id=2,dept_id=1,email='[email protected]',name='osTicket Support' ".
",subject='osTicket Installed!',helptopic='Commercial support',status='open',source='Web'";
if(db_query($sql) && ($id=db_insert_id())){
db_query('INSERT INTO '.PREFIX."ticket_message VALUES (1,$id,NULL,".db_input(OSTICKET_INSTALLED).",NULL,'Web','',NOW(),NULL)");
}
//Log a message.
$sql='INSERT INTO '.PREFIX.'syslog SET created=NOW(),updated=NOW() '.
',title="osTicket installed!",log_type="Debug" '.
',log='.db_input("Congratulations osTicket basic installation completed!\n\nThank you for choosing osTicket!").
',ip_address='.db_input($_SERVER['REMOTE_ADDR']);
mysql_query($sql);
$msg='Congratulations osTicket basic installation completed!';
$inc='done.inc.php';
}else{
$errors['err']='Unable to write to config file!';
}
}
@fclose($fp);
}else{
$errors['err']=$errors['err']?$errors['err']:'Error(s) occured. Please correct them and try again';
}
}
}
$title=sprintf('osTicket version %s - Basic installation',VERSION_VERBOSE);
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>osTicket Installer</title>
<link rel="stylesheet" href="style.css" media="screen">
</head>
<body>
<div id="container">
<div id="header">
<a id="logo" href="#" title="osTicket"><img src="images/ostlogo.jpg" width="188" height="72" alt="osTicket Installer"></a>
<p id="info"><?=$info?></p>
</div>
<div id="nav">
<ul id="sub_nav">
<li><?=$title?></li>
</ul>
</div>
<div class="clear"></div>
<div id="content" width="100%" height="100%">
<div>
<?if($errors['err']) {?>
<p align="center" id="errormessage"><?=$errors['err']?></p>
<?}elseif($msg) {?>
<p align="center" id="infomessage"><?=$msg?></p>
<?}elseif($warn) {?>
<p align="center" id="warnmessage"><?=$warn?></p>
<?}?>
</div>
<div style="padding:0 3px 5px 3px;">
<?php
if(file_exists("./inc/$inc"))
require("./inc/$inc");
else
echo '<span class="error">Invalid path - get technical support</span>';
?>
</div>
</div>
<div id="footer">Copyright © <?=date('Y')?> osTicket-reloaded.com. All Rights Reserved.</div>
</div>
</body>
</html>