00001 <?php 00002 // setup.php - Install/Upgrade and set up plugins 00003 // 00004 // SiT (Support Incident Tracker) - Support call tracking system 00005 // Copyright (C) 2010-2011 The Support Incident Tracker Project 00006 // Copyright (C) 2000-2009 Salford Software Ltd. and Contributors 00007 // 00008 // This software may be used and distributed according to the terms 00009 // of the GNU General Public License, incorporated herein by reference. 00010 // 00011 00012 // Author: Ivan Lucas <ivanlucas[at]users.sourceforge.net> 00013 00014 // Define path constants, we don't include core.php so we do this here 00015 define ('APPLICATION_FSPATH', realpath(dirname( __FILE__ ) . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR); 00016 define ('APPLICATION_LIBPATH', realpath(dirname( __FILE__ ).DIRECTORY_SEPARATOR . 'lib') . DIRECTORY_SEPARATOR); 00017 define ('APPLICATION_HELPPATH', realpath(dirname( __FILE__ ).DIRECTORY_SEPARATOR . 'help') . DIRECTORY_SEPARATOR); 00018 define ('APPLICATION_INCPATH', realpath(dirname( __FILE__ ).DIRECTORY_SEPARATOR . 'inc') . DIRECTORY_SEPARATOR); 00019 define ('APPLICATION_I18NPATH', realpath(dirname( __FILE__ ).DIRECTORY_SEPARATOR . 'i18n') . DIRECTORY_SEPARATOR); 00020 define ('APPLICATION_PORTALPATH', realpath(dirname( __FILE__ ).DIRECTORY_SEPARATOR . 'portal') . DIRECTORY_SEPARATOR); 00021 define ('APPLICATION_PLUGINPATH', realpath(dirname( __FILE__ ).DIRECTORY_SEPARATOR . 'plugins') . DIRECTORY_SEPARATOR); 00022 00023 00024 // Load config defaults 00025 @include (APPLICATION_LIBPATH . 'defaults.inc.php'); 00026 // Keep the defaults as a seperate array 00027 $DEFAULTS = $CONFIG; 00028 00029 // Load config file with customisations 00030 // @include ("config.inc-dist.php"); 00031 if (file_exists(APPLICATION_FSPATH . "config.inc.php") 00032 AND !include (APPLICATION_FSPATH . "config.inc.php")) 00033 { 00034 die('Could not read config file config.inc.php'); 00035 } 00036 // Server Configuration 00037 00038 // for legacy systems 00039 if (file_exists('/etc/webtrack.conf') 00040 AND !include ('/etc/webtrack.conf')) 00041 { 00042 die('Could not read config file webtrack.conf'); 00043 } 00044 00045 if (file_exists('/etc/sit.conf') 00046 AND !include ('/etc/sit.conf')) 00047 { 00048 die('Cound not read config file sit.conf'); 00049 } 00050 00051 // // Some actions require authentication 00052 // if ($_REQUEST['action'] == 'reconfigure') 00053 // { 00054 // $permission = 22; 00055 // $_REQUEST['config'] = 'advanced'; // set advanced mode 00056 // require (APPLICATION_LIBPATH . 'functions.inc.php'); 00057 // require (APPLICATION_LIBPATH . 'auth.inc.php'); 00058 // } 00059 00060 // These are the required variables we want to configure during installation 00061 $SETUP = array('db_hostname','db_database','db_username','db_password', 'db_tableprefix','application_webpath'); 00062 00063 require(APPLICATION_LIBPATH . 'configvars.inc.php'); 00064 00065 $upgradeok = FALSE; 00066 $config_filename = APPLICATION_FSPATH . 'config.inc.php'; 00067 00068 $configfiles = get_included_files(); 00069 00070 $systemhash = md5(date('Y-m-d') . $_SERVER['REMOTE_ADDR'] 00071 . $_SERVER['SCRIPT_FILENAME'] . $_SERVER['HTTP_USER_AGENT'] 00072 . $CONFIG['attachment_fspath'] . $_SERVER['SERVER_SIGNATURE'] ); 00073 00074 00082 function filterconfigfiles($var) 00083 { 00084 $poss_config_files = array('config.inc.php', 'sit.conf', 'webtrack.conf'); 00085 $recognised = FALSE; 00086 foreach ($poss_config_files AS $poss) 00087 { 00088 if (substr($var, strlen($var)-strlen($poss)) == $poss) $recognised = TRUE; 00089 } 00090 return $recognised; 00091 } 00092 00093 00094 $configfiles = array_filter($configfiles, 'filterconfigfiles'); 00095 $configfiles = array_values($configfiles); 00096 $numconfigfiles = count($configfiles); 00097 if ($numconfigfiles == 1) 00098 { 00099 $config_filename = $configfiles[0]; 00100 } 00101 elseif ($numconfigfiles < 1) 00102 { 00103 $configfiles[] = './config.inc.php'; 00104 } 00105 00106 $cfg_file_exists = FALSE; 00107 $cfg_file_writable = FALSE; 00108 foreach ($configfiles AS $conf_filename) 00109 { 00110 if (file_exists($conf_filename)) $cfg_file_exists = TRUE; 00111 if (is_writable($conf_filename)) $cfg_file_writable = TRUE; 00112 } 00113 00114 00115 // Detect whether an array is associative 00116 // From http://uk.php.net/manual/en/function.is-array.php#77744 00117 //function is_assoc($array) 00118 //{ 00119 // return is_array($array) && count($array) !== array_reduce(array_keys($array), 'is_assoc_callback', 0); 00120 //} 00121 00122 00123 //function is_assoc_callback($a, $b) 00124 //{ 00125 // return $a === $b ? $a + 1 : 0; 00126 //} 00127 00133 function setup_configure() 00134 { 00135 global $SETUP, $CFGVAR, $CONFIG, $configfiles, $config_filename, $cfg_file_exists; 00136 global $cfg_file_writable, $numconfigfiles; 00137 $html = ''; 00138 00139 if ($cfg_file_exists AND $_REQUEST['configfile'] != 'new') 00140 { 00141 if ($_SESSION['new']) 00142 { 00143 if ($numconfigfiles < 2) 00144 { 00145 $html .= "<h4>Found an existing config file <var>{$config_filename}</var></h4>"; 00146 } 00147 else 00148 { 00149 $html .= "<p class='error'>Found more than one existing config file</p>"; 00150 if ($cfg_file_writable) 00151 { 00152 $html .= "<ul>"; 00153 foreach ($configfiles AS $conf_filename) 00154 { 00155 $html .= "<li><var>{$conf_filename}</var></li>"; 00156 } 00157 $html .= "</ul>"; 00158 } 00159 } 00160 } 00161 //$html .= "<p>Since you already have a config file we assume you are upgrading or reconfiguring, if this is not the case please delete the existing config file.</p>"; 00162 if ($cfg_file_writable) 00163 { 00164 $html .= "<p class='error'>Important: The file permissions on the configuration file "; 00165 $html .= "allow it to be modified, we recommend you make this file read-only once SiT! is configured."; 00166 $html .= "</p>"; 00167 } 00168 else 00169 { 00170 $html .= "<p><a href='setup.php?action=reconfigure&configfile=new' >Create a new config file</a>.</p>"; 00171 } 00172 } 00173 else $html .= "<h2>New Configuration</h2><p>Please complete this form to create a new configuration file for SiT!</p>"; 00174 00175 if ($cfg_file_writable OR $_SESSION['new'] === 1 OR $cfg_file_exists == FALSE OR $_REQUEST['configfile'] == 'new') 00176 { 00177 $html .= "\n<form action='setup.php' method='post'>\n"; 00178 00179 if ($_REQUEST['config'] == 'advanced') 00180 { 00181 $html .= "<input type='hidden' name='config' value='advanced' />\n"; 00182 foreach ($CFGVAR AS $setupvar => $setupval) 00183 { 00184 $SETUP[] = $setupvar; 00185 } 00186 } 00187 00188 $c=1; 00189 foreach ($SETUP AS $setupvar) 00190 { 00191 $html .= "<div class='configvar{$c}'>"; 00192 if ($CFGVAR[$setupvar]['title']!='') $title = $CFGVAR[$setupvar]['title']; 00193 else $title = $setupvar; 00194 $html .= "<h4>{$title}</h4>"; 00195 if ($CFGVAR[$setupvar]['help']!='') $html .= "<p class='helptip'>{$CFGVAR[$setupvar]['help']}</p>\n"; 00196 00197 $html .= "<var>\$CONFIG['$setupvar']</var> = "; 00198 00199 $value = ''; 00200 if (!$cfg_file_exists OR ($cfg_file_exists AND $cfg_file_writable)) 00201 { 00202 $value = $CONFIG[$setupvar]; 00203 if (is_bool($value)) 00204 { 00205 if ($value==TRUE) $value='TRUE'; 00206 else $value='FALSE'; 00207 } 00208 elseif (is_array($value)) 00209 { 00210 if (is_assoc($value)) 00211 { 00212 $value = "array(".implode_assoc('=>',',',$value).")"; 00213 } 00214 else 00215 { 00216 $value="array(".implode(',',$value).")"; 00217 } 00218 } 00219 if ($setupvar=='db_password' AND $_REQUEST['action']!='reconfigure') $value=''; 00220 } 00221 00222 if (!$cfg_file_exists OR $_REQUEST['configfile'] == 'new') 00223 { 00224 // Dynamic defaults 00225 // application_fspath was removed, leaving this code just-in-case 00226 // DEPRECATED - remove for >= 3.50 00227 if ($setupvar == 'application_fspath') 00228 { 00229 $value = str_replace('htdocs' . DIRECTORY_SEPARATOR, '', dirname( __FILE__ ) . DIRECTORY_SEPARATOR); 00230 } 00231 00232 if ($setupvar == 'application_webpath') 00233 { 00234 $value = dirname( strip_tags( $_SERVER['PHP_SELF'] ) ); 00235 if ($value == '/' OR $value == '\\') $value = '/'; 00236 else $value = $value . '/'; 00237 } 00238 } 00239 00240 switch ($CFGVAR[$setupvar]['type']) 00241 { 00242 case 'select': 00243 $html .= "<select name='$setupvar'>"; 00244 if (empty($CFGVAR[$setupvar]['options'])) $CFGVAR[$setupvar]['options'] = "TRUE|FALSE"; 00245 $options = explode('|', $CFGVAR[$setupvar]['options']); 00246 foreach ($options AS $option) 00247 { 00248 $html .= "<option value=\"{$option}\""; 00249 if ($option == $value) $html .= " selected='selected'"; 00250 $html .= ">{$option}</option>\n"; 00251 } 00252 $html .= "</select>"; 00253 break; 00254 00255 case 'percent': 00256 $html .= "<select name='$setupvar'>"; 00257 for($i = 0; $i <= 100; $i++) 00258 { 00259 $html .= "<option value=\"{$i}\""; 00260 if ($i == $value) $html .= " selected='selected'"; 00261 $html .= ">{$i}</option>\n"; 00262 } 00263 $html .= "</select>"; 00264 break; 00265 00266 case 'text': 00267 default: 00268 if (strlen($CONFIG[$setupvar]) < 65) 00269 { 00270 $html .= "<input type='text' name='$setupvar' size='60' value=\"{$value}\" />"; 00271 } 00272 else 00273 { 00274 $html .= "<textarea name='$setupvar' cols='60' rows='10'>{$value}</textarea>"; 00275 } 00276 } 00277 if ($setupvar=='db_password' AND $_REQUEST['action']!='reconfigure' AND $value != '') $html .= "<p class='info'>The current password setting is not shown</p>"; 00278 $html .= "</div>"; 00279 $html .= "<br />\n"; 00280 if ($c==1) $c==2; else $c=1; 00281 } 00282 $html .= "<input type='hidden' name='action' value='save_config' />"; 00283 $html .= "<br /><input type='submit' name='submit' value='Save Configuration' />"; 00284 $html .= "</form>\n"; 00285 } 00286 return $html; 00287 } 00288 00289 00296 function setup_exec_sql($sqlquerylist) 00297 { 00298 global $CONFIG, $dbSystem, $installed_schema, $application_version; 00299 if (!empty($sqlquerylist)) 00300 { 00301 if (!is_array($sqlquerylist)) $sqlquerylist = array($sqlquerylist); 00302 00303 // echo "<pre>".print_r($sqlquerylist,true)."</pre>"; 00304 00305 // Loop around the queries 00306 foreach ($sqlquerylist AS $schemaversion => $queryelement) 00307 { 00308 if ($schemaversion != '0') $schemaversion = substr($schemaversion, 1); 00309 // echo "<p>Schema version $schemaversion, installed schema $installed_schema, query $queryelement</p>"; 00310 if ($schemaversion == 0 OR $installed_schema < $schemaversion) 00311 { 00312 $sqlqueries = explode( ';', $queryelement); 00313 // We don't need the last entry it's blank, as we end with a ; 00314 array_pop($sqlqueries); 00315 $errors = 0; 00316 foreach ($sqlqueries AS $sql) 00317 { 00318 if (!empty($sql)) 00319 { 00320 mysql_query($sql); 00321 if (mysql_error()) 00322 { 00323 $errno = mysql_errno(); 00324 $errstr = ''; 00325 // See http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html 00326 // For list of mysql error numbers 00327 switch ($errno) 00328 { 00329 case 1022: 00330 case 1050: 00331 case 1060: 00332 case 1061: 00333 case 1062: 00334 $severity = 'info'; 00335 $errstr = "This could be because this part of the database schema is already up to date."; 00336 break; 00337 00338 case 1058: 00339 $severity = 'error'; 00340 $errstr = "This looks suspiciously like a bug, if you think this is the case please report it."; 00341 break; 00342 00343 // case 1054: 00344 // if (preg_match("/ALTER TABLE/", $sql) >= 1) 00345 // { 00346 // $severity = 'info'; 00347 // $errstr = "This could be because this part of the database schema is already up to date."; 00348 // } 00349 // break; 00350 00351 case 1051: 00352 case 1091: 00353 if (preg_match("/DROP/", $sql) >= 1) 00354 { 00355 $severity = 'info'; 00356 $errstr = "We expected to find something in order to remove it but it doesn't exist. This could be because this part of the database schema is already up to date.."; 00357 } 00358 break; 00359 00360 00361 00362 case 1044: 00363 case 1045: 00364 case 1142: 00365 case 1143: 00366 case 1227: 00367 $severity = 'error'; 00368 $errstr = "This could be because the MySQL user '{$CONFIG['db_username']}' does not have appropriate permission to modify the database schema.<br />"; 00369 $errstr .= "<strong>Check your MySQL permissions allow the schema to be modified</strong>."; 00370 00371 default: 00372 $severity = 'error'; 00373 $errstr = "You may have found a bug, if you think this is the case please report it."; 00374 } 00375 $html .= "<p class='$severity'>"; 00376 if ($severity == 'info') 00377 { 00378 $html .= "<strong>Information:</strong>"; 00379 } 00380 else 00381 { 00382 $html .= "<strong>A MySQL error occurred:</strong>"; 00383 $errors ++; 00384 } 00385 $html .= " [".mysql_errno()."] ".mysql_error()."<br />"; 00386 if (!empty($errstr)) $html .= $errstr."<br />"; 00387 $html .= "Raw SQL: <code class='small'>".htmlspecialchars($sql)."</code>"; 00388 } 00389 } 00390 } 00391 } 00392 } 00393 } 00394 echo $html; 00395 return $errors; 00396 } 00397 00398 00405 function setup_createdb() 00406 { 00407 global $CONFIG; 00408 00409 $res = FALSE; 00410 $sql = "CREATE DATABASE `{$CONFIG['db_database']}` DEFAULT CHARSET utf8"; 00411 $db = @mysql_connect($CONFIG['db_hostname'], $CONFIG['db_username'], $CONFIG['db_password']); 00412 if (!@mysql_error()) 00413 { 00414 // See Mantis 506 for sql_mode discussion 00415 @mysql_query("SET SESSION sql_mode = '';"); 00416 00417 // Connected to database 00418 echo "<h2>Creating empty database...</h2>"; 00419 $result = mysql_query($sql); 00420 if ($result) 00421 { 00422 $res = TRUE; 00423 echo "<p><strong>OK</strong> Database '{$CONFIG['db_database']}' created.</p>"; 00424 echo setup_button('checkatttdir', 'Next'); 00425 } 00426 else $res = FALSE; 00427 } 00428 else 00429 { 00430 $res = FALSE; 00431 } 00432 00433 if ($res == FALSE) 00434 { 00435 echo "<p class='error'>"; 00436 if (mysql_error()) 00437 { 00438 echo mysql_error()."<br />"; 00439 } 00440 echo "The database could not be created automatically, "; 00441 echo "you can create it manually by executing the SQL statement <br /><code>{$sql};</code></p>"; 00442 echo setup_button('', 'Next'); 00443 } 00444 return $res; 00445 } 00446 00447 00454 function setup_check_adminuser() 00455 { 00456 global $dbUsers; 00457 $sql = "SELECT id FROM `{$dbUsers}` WHERE id=1 OR username='admin' OR roleid='1'"; 00458 $result = @mysql_query($sql); 00459 if (mysql_num_rows($result) >= 1) return TRUE; 00460 else FALSE; 00461 } 00462 00463 00473 function setup_button($action, $label, $extrahtml='') 00474 { 00475 $html = "\n<form action='{$_SERVER['PHP_SELF']}' method='post'>"; 00476 if (!empty($action)) 00477 { 00478 $html .= "<input type='hidden' name='action' value=\"{$action}\" />"; 00479 } 00480 $html .= "<input type='submit' value=\"{$label}\" />"; 00481 if (!empty($extrahtml)) $html .= $extrahtml; 00482 $html .= "</form>\n"; 00483 00484 return $html; 00485 } 00486 00487 session_name($CONFIG['session_name']); 00488 session_start(); 00489 00490 // Force logout 00491 $_SESSION['auth'] = FALSE; 00492 $_SESSION['portalauth'] = FALSE; 00493 00494 00495 echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"; 00496 echo " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"; 00497 echo "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n"; 00498 echo "<head>\n"; 00499 echo "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\" />\n"; 00500 echo "<style type=\"text/css\">\n"; 00501 echo "body { background-color: #FFF; font-family: Tahoma, Helvetica, sans-serif; font-size: 10pt;}\n"; 00502 echo "h1,h2,h3,h4,h5 { color: #203894; padding: 0.1em; border: 1px solid #4D485B; }\n"; 00503 if (file_exists('./images/sitlogo_270x100.png')) echo "body {background-image: url('images/sitlogo_270x100.png'); background-attachment:fixed; background-position: 98% 98%; background-repeat: no-repeat;}\n"; 00504 echo "h4 {background-color: transparent; color: #000; border: 0px; margin: 2px 0px 3px 0px; }\n"; 00505 echo "div.configvar1 {background-color: #F7FAFF; border: 1px solid #4D485B; margin-bottom: 10px; padding: 0px 5px 10px 5px; filter:alpha(opacity=75); opacity: 0.75; -moz-opacity:0.75; -moz-border-radius: 3px;} "; 00506 echo "div.configvar2 {background-color: green; border: 1px solid #4D485B; margin-bottom: 10px;} "; 00507 echo ".error {background-position: 3px 2px; 00508 background-repeat: no-repeat; 00509 padding: 3px 3px 3px 22px; 00510 min-height: 16px; 00511 -moz-border-radius: 5px; 00512 /* display: inline; */ 00513 border: 1px solid #000; 00514 margin-left: 2em; 00515 margin-right: 2em; 00516 width: auto; 00517 text-align: left; 00518 background-image: url('images/icons/sit/16x16/warning.png'); 00519 color: #5A3612; 00520 border: 1px solid #A26120; 00521 background-color: #FFECD7; 00522 } 00523 00524 .info { 00525 background-position: 3px 2px; 00526 background-repeat: no-repeat; 00527 padding: 3px 3px 3px 22px; 00528 min-height: 16px; 00529 -moz-border-radius: 5px; 00530 /* display: inline; */ 00531 border: 1px solid #000; 00532 margin-left: 2em; 00533 margin-right: 2em; 00534 width: auto; 00535 text-align: left; 00536 } 00537 p.info { 00538 background-image: url('images/icons/sit/16x16/info.png'); 00539 color: #17446B; 00540 border: 1px solid #17446B; 00541 background-color: #F4F6FF; 00542 } 00543 00544 a.button:link, a.button:visited 00545 { 00546 float: left; 00547 margin: 2px 5px 2px 5px; 00548 padding: 2px; 00549 width: 100px; 00550 border-top: 1px solid #ccc; 00551 border-bottom: 1px solid black; 00552 border-left: 1px solid #ccc; 00553 border-right: 1px solid black; 00554 background: #ccc; 00555 text-align: center; 00556 text-decoration: none; 00557 font: normal 10px Verdana; 00558 color: black; 00559 } 00560 00561 a.button:hover 00562 { 00563 background: #eee; 00564 } 00565 00566 a.button:active 00567 { 00568 border-bottom: 1px solid #eee; 00569 border-top: 1px solid black; 00570 border-right: 1px solid #eee; 00571 border-left: 1px solid black; 00572 } 00573 00574 var { font-family: Andale Mono, monospace; font-style: normal; } 00575 code.small { font-size: 75%; color: #555; } 00576 } 00577 00578 "; 00579 echo ".help {background: #F7FAFF; border: 1px solid #3165CD; color: #203894; padding: 2px;}\n"; 00580 echo ".helptip { color: #203894; }\n"; 00581 echo ".warning {background: #FFFFE6; border: 2px solid #FFFF31; color: red; padding: 2px;}\n"; 00582 echo "pre {background:#FFF; border:#999; padding: 1em;}\n"; 00583 echo "a.button { border: 1px outset #000; padding: 2px; background-color: #EFEFEF;} "; 00584 echo "a:link,a:visited { color: #000099; }\n"; 00585 echo "a:hover { background: #99CCFF; }\n"; 00586 echo "hr { background-color: #4D485B; margin-top: 3em; }\n"; 00587 echo "</style>\n"; 00588 echo "<title>Support Incident Tracker Setup</title>\n"; 00589 echo "</head>\n<body>\n"; 00590 00591 echo "<h1>Support Incident Tracker - Installation & Setup</h1>"; 00592 00593 // 00594 // Pre-flight Checks 00595 // 00596 00597 if (!empty($_REQUEST['msg'])) 00598 { 00599 $msg = strip_tags(base64_decode(urldecode($_REQUEST['msg']))); 00600 if ($cfg_file_exists === FALSE) 00601 { 00602 echo "<p class='info'><strong>It looks like you are setting up SiT! for the first time</strong> because we could not find a configuration file.<br />"; 00603 echo "Please proceed with creating a new configuration file.</p>"; 00604 } 00605 else 00606 { 00607 echo "<p class='error'><strong>Configuration Problem</strong>: {$msg}</p>"; 00608 } 00609 } 00610 00611 00612 00613 // Check that includes worked and that we have some config variables set, these two should always be set 00614 if ($CONFIG['application_name'] == '' AND $CONFIG['application_shortname'] == '') 00615 { 00616 echo "<p class='error'>SiT! Setup couldn't find configuration defaults (defaults.inc.php). Is your lib/ directory missing?</p>"; 00617 } 00618 00619 // Check we have the mysql extension 00620 if (!extension_loaded('mysql')) 00621 { 00622 echo "<p class='error'>Error: Could not find the mysql extension, SiT! requires MySQL to be able to run, you should install and enable the MySQL PHP Extension then run setup again.</p>"; 00623 } 00624 00625 if (version_compare(PHP_VERSION, "5.0.0", "<")) 00626 { 00627 echo "<p class='error'>You are running an older PHP version (< PHP 5), SiT v3.35 and later require PHP 5.0.0 or newer, some features may not work properly.</p>"; 00628 } 00629 00630 echo "\n\n<!-- A:".strip_tags($_REQUEST['action'])." -->\n\n"; 00631 00632 switch ($_REQUEST['action']) 00633 { 00634 case 'save_config': 00635 $newcfgfile = "<"; 00636 $newcfgfile .= "?php\n"; 00637 $newcfgfile .= "# config.inc.php - SiT! Config file generated automatically by setup.php on ".date('r')."\n\n"; 00638 00639 if ($_REQUEST['config'] == 'advanced') 00640 { 00641 foreach ($CFGVAR AS $setupvar => $setupval) 00642 { 00643 $SETUP[] = $setupvar; 00644 } 00645 } 00646 00647 // Keep the posted setup 00648 foreach ($SETUP AS $setupvar) 00649 { 00650 if ($_POST[$setupvar]==='TRUE') $_POST[$setupvar] = TRUE; 00651 if ($_POST[$setupvar]==='FALSE') $_POST[$setupvar] = FALSE; 00652 $CONFIG[$setupvar]=$_POST[$setupvar]; 00653 } 00654 00655 // Set up a hard to find attachment path 00656 if ($CONFIG['attachment_fspath'] == '') 00657 { 00658 // We generate a path based on some semi-static values so that it's hard to guess, 00659 // but will still probably be the same if setup is run again the same day 00660 $CONFIG['attachment_fspath'] = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . "attachments-" . $systemhash . DIRECTORY_SEPARATOR; 00661 } 00662 00663 // Extract the differences between the defaults and the newly configured items 00664 $CFGDIFF = array_diff_assoc($CONFIG, $DEFAULTS); 00665 00666 if (count($CFGDIFF) > 0) 00667 { 00668 foreach ($CFGDIFF AS $setupvar => $setupval) 00669 { 00670 if ($CFGVAR[$setupvar]['title'] != '') 00671 { 00672 $newcfgfile .= "# {$CFGVAR[$setupvar]['title']}\n"; 00673 } 00674 00675 if ($CFGVAR[$setupvar]['help']!='') 00676 { 00677 $newcfgfile .= "# {$CFGVAR[$setupvar]['help']}\n"; 00678 } 00679 00680 $newcfgfile .= "\$CONFIG['$setupvar'] = "; 00681 00682 if (is_numeric($setupval)) 00683 { 00684 $newcfgfile .= "{$setupval}"; 00685 } 00686 elseif (is_bool($setupval)) 00687 { 00688 $newcfgfile .= $setupval == TRUE ? "TRUE" : "FALSE"; 00689 } 00690 elseif (substr($setupval, 0, 6)=='array(') 00691 { 00692 $newcfgfile .= stripslashes("{$setupval}"); 00693 } 00694 else 00695 { 00696 $newcfgfile .= "'".addslashes($setupval)."'"; 00697 } 00698 $newcfgfile .= ";\n\n"; 00699 } 00700 } 00701 else 00702 { 00703 $newcfgfile .= "# Nothing configured. This will mean the defaults are used.\n\n"; 00704 } 00705 00706 // INL if we leave off the php closing tag it saves people having trouble 00707 // with whitespace 00708 //$newcfgfile .= "?"; 00709 //$newcfgfile .= ">"; 00710 00711 $fp = @fopen($config_filename, 'w'); 00712 if (!$fp) 00713 { 00714 echo "<p class='error'>Could not write {$config_filename}</p>"; 00715 echo "<p class='help'>Copy this text and paste it into a <var>config.inc.php</var> file in the SiT root directory (the folder than contains setup.php for example)<br />"; 00716 // or <var>sit.conf</var> in the <var>/etc</var> directory 00717 echo "Or change the permissions on the folder so that it is writable and <a href=\"javascript:location.reload(true)\">refresh</a> this page to try again (if you do this remember to make it "; 00718 echo "read-only again afterwards)</p>"; 00719 echo "<script type='text/javascript'>\n 00720 function selectText(divid) 00721 { 00722 if (document.selection) 00723 { 00724 var div = document.body.createTextRange(); 00725 div.moveToElementText(document.getElementById(divid)); 00726 div.select(); 00727 } 00728 else 00729 { 00730 var div = document.createRange(); 00731 div.setStartBefore(document.getElementById(divid)); 00732 div.setEndAfter(document.getElementById(divid)) ; 00733 window.getSelection().addRange(div); 00734 } 00735 00736 } 00737 </script>"; 00738 echo "<div id='configfile' onclick=\"selectText('configfile');\" style='margin-left: 5%; margin-right: 5%; background-color: #F7FAFF; padding: 1em; border: 1px dashed #ccc;filter:alpha(opacity=75); opacity: 0.75; -moz-opacity:0.75; -moz-border-radius: 3px; '>"; 00739 highlight_string($newcfgfile); 00740 echo "</div>"; 00741 echo "<p>After creating your config file click the 'Next' button below.</p>"; 00742 } 00743 else 00744 { 00745 echo "<p>Writing to {$config_filename}</p>"; 00746 fwrite($fp, $newcfgfile); 00747 fclose($fp); 00748 echo "<p>Config file modified</p>"; 00749 if (!@chmod($config_filename, 0640)) 00750 { 00751 echo "<p class='error'>Important: The file permissions on the file <var>{$config_filename}</var> "; 00752 echo "allow the file to be modified, we recommend you now make this file read-only."; 00753 if (DIRECTORY_SEPARATOR == '/') 00754 { 00755 $html .= "<br />You can run the command <code>chmod 444</code> to make it read-only."; 00756 } 00757 echo "</p>"; 00758 } 00759 } 00760 echo setup_button('checkdbstate', 'Next'); 00761 break; 00762 00763 case 'reconfigure': 00764 echo "<h2>Reconfigure</h2>"; 00765 echo "<p>Amend your existing SiT! configuration. Please take care or you may break your SiT! installation.</p>"; 00766 echo setup_configure(); 00767 break; 00768 00769 case 'checkdbstate': 00770 // Connect to Database server 00771 $db = @mysql_connect($CONFIG['db_hostname'], $CONFIG['db_username'], $CONFIG['db_password']); 00772 if (@mysql_error()) 00773 { 00774 echo "<p class='error'>Setup could not connect to the database server '{$CONFIG['db_hostname']}'. MySQL Said: ".mysql_error()."</p>"; 00775 echo setup_configure(); 00776 } 00777 else 00778 { 00779 // Connected to database 00780 // Select database 00781 mysql_select_db($CONFIG['db_database'], $db); 00782 if (mysql_error()) 00783 { 00784 if (!empty($CONFIG['db_username'])) 00785 { 00786 if ($cfg_file_exists) 00787 { 00788 echo "<p class='error'>".mysql_error()."<br />Could not select database"; 00789 if ($CONFIG['db_database']!='') 00790 { 00791 echo " '{$CONFIG['db_database']}', check the database name you have configured matches the database in MySQL"; 00792 } 00793 else 00794 { 00795 echo ", the database name was not configured, please set the <code>\$CONFIG['db_database'] config variable"; 00796 $CONFIG['db_database'] = 'sit'; 00797 } 00798 echo "</p>"; 00799 if ($_SESSION['new'] == 1) 00800 { 00801 echo "<p class='info'>If this is a new installation of SiT and you would like to use the database name '{$CONFIG['db_database']}', you should proceed and create a database</p>"; 00802 } 00803 echo setup_button('reconfigure', 'Reconfigure SiT!'); 00804 echo "<br />or<br /><br />"; 00805 } 00806 else 00807 { 00808 echo "<p class='info'>You can now go ahead and create a database called '{$CONFIG['db_database']}' for SiT! to use.</p>"; 00809 } 00810 echo setup_button('createdb', 'Create a database', "<br /><label><input type='checkbox' name='sampledata' value='yes' checked='checked' /> With sample data</label>"); 00811 //echo "<p><a href='{$_SERVER['PHP_SELF']}?action=reconfigure'>Reconfigure</a> SiT!</p>"; 00812 } 00813 else 00814 { 00815 // Username and Password are set, but the db could not be selected 00816 00817 } 00818 00819 // FIMXE INL temp removed 00820 // else 00821 // { 00822 // echo "<p class='help'>If this is the first time you have used SiT! you may need to create the database, "; 00823 // echo "if you have the necessary MySQL permissions you can create the database automatically.<br />"; 00824 // echo "Alternatively you can create it manually by executing the SQL statement <br /><code>{$sql};</code></p"; 00825 // echo "<p><a href='setup.php?action=createdatabase' class='button'>Create Database</a></p>"; 00826 // } 00827 // //echo "<p>After creating the database run <a href='setup.php' class='button'>setup</a> again to create the database schema</p>"; 00828 if (empty($CONFIG['db_database']) OR empty($CONFIG['db_username'])) 00829 { 00830 echo "<p>You need to configure SiT to be able access the MySQL database.</p>"; 00831 echo setup_configure(); 00832 } 00833 } 00834 else 00835 { 00836 echo "<p class='info'>Sucessfully connected to your database.</p>"; 00837 echo setup_button('checkatttdir', 'Next'); 00838 } 00839 } 00840 break; 00841 00842 00843 case 'createdb': 00844 if ($_REQUEST['sampledata'] == 'yes' ) $_SESSION['sampledata'] = TRUE; 00845 else $_SESSION['sampledata'] = FALSE; 00846 setup_createdb(); 00847 break; 00848 00849 case 'checkatttdiragain': 00850 $again = TRUE; 00851 case 'checkatttdir': 00852 if (file_exists($CONFIG['attachment_fspath']) == FALSE) 00853 { 00854 echo "<h2>Attachments Directory</h2>"; 00855 echo "<p>SiT! requires a directory to store attachments.</p>"; 00856 00857 echo setup_button('createattdir', "Create attachments directory"); 00858 echo "<br />"; 00859 if ($again) 00860 { 00861 echo setup_button('checkatttdiragain', 'Next'); 00862 echo "<p class='error'>The directory <code>{$CONFIG['attachment_fspath']}</code> must be created before setup can continue.</p>"; 00863 } 00864 } 00865 elseif (is_writable($CONFIG['attachment_fspath']) == FALSE) 00866 { 00867 echo "<h2>Attachments Directory</h2>"; 00868 echo "<p>SiT! requires that the attachments directory is writable by the web server.</p>"; 00869 if (DIRECTORY_SEPARATOR == '/') 00870 { 00871 echo "<br />You can run the following shell command to make it writable.<br /><br /><var>chmod ugo+wx {$CONFIG['attachment_fspath']}</var>"; 00872 } 00873 else 00874 { 00875 echo "<br />You may need to set windows file permissions to set the folder <var>{$CONFIG['attachment_fspath']}</var> writable."; 00876 } 00877 echo "</p>"; 00878 00879 echo setup_button('checkatttdiragain', 'Next'); 00880 } 00881 else 00882 { 00883 $sql = "SHOW TABLES LIKE '{$dbUsers}'"; 00884 $result = @mysql_query($sql); 00885 if (mysql_error() OR mysql_num_rows($result) < 1) 00886 { 00887 echo "<p>Next we will create a database schema</p>"; 00888 echo setup_button('', 'Next'); 00889 } 00890 else 00891 { 00892 echo "<p class='info'>You can now go ahead and run SiT!.</p>"; 00893 echo "<form action='index.php' method='get'>"; 00894 echo "<input type='submit' value=\"Run SiT!\" />"; 00895 echo "</form>\n"; 00896 } 00897 } 00898 break; 00899 00900 00901 case 'createattdir': 00902 // Note this creates a directory with 777 permissions! 00903 $dir = @mkdir($CONFIG['attachment_fspath'], '0777'); 00904 if ($dir) 00905 { 00906 echo setup_button('checkatttdir', 'Next'); 00907 } 00908 else 00909 { 00910 echo "<p class='error'>Sorry, the attachment directory could not be created for you.</p>"; // FIXME more help 00911 echo "<p>Please manually create a directory named <code>{$CONFIG['attachment_fspath']}</code></p>"; 00912 00913 if (substr($CONFIG['attachment_fspath'], 0, 14) == './attachments-') 00914 { 00915 echo "<p class='info'>Setup has chosen this random looking directory name on purpose, "; 00916 echo "please create the directory named exactly as shown above.</p>"; 00917 } 00918 echo setup_button('checkatttdiragain', 'Next'); 00919 } 00920 break; 00921 00922 default: 00923 require (APPLICATION_LIBPATH . 'tablenames.inc.php'); 00924 // Connect to Database server 00925 $db = @mysql_connect($CONFIG['db_hostname'], $CONFIG['db_username'], $CONFIG['db_password']); 00926 if (@mysql_error()) 00927 { 00928 echo setup_configure(); 00929 } 00930 else 00931 { 00932 // Connected to database 00933 // Select database 00934 mysql_select_db($CONFIG['db_database'], $db); 00935 if (mysql_error()) 00936 { 00937 if (!empty($CONFIG['db_username'])) 00938 { 00939 echo "<p class='error'>".mysql_error()."<br />Could not select database"; 00940 if ($CONFIG['db_database']!='') 00941 { 00942 echo " '{$CONFIG['db_database']}', check the database name you have configured matches the database in MySQL"; 00943 } 00944 else 00945 { 00946 echo ", the database name was not configured, please set the <code>\$CONFIG['db_database'] config variable"; 00947 $CONFIG['db_database'] = 'sit'; 00948 } 00949 echo "</p>"; 00950 echo setup_button('reconfigure', 'Reconfigure SiT!'); 00951 echo "<p>or</p>"; 00952 echo setup_button('createdb', 'Create a database', "<br /><label><input type='checkbox' name='sampledata' value='yes' checked='checked' /> With sample data</label>"); 00953 //echo "<p><a href='{$_SERVER['PHP_SELF']}?action=reconfigure'>Reconfigure</a> SiT!</p>"; 00954 } 00955 else 00956 { 00957 // Username and Password are set, but the db could not be selected 00958 00959 } 00960 00961 // FIMXE INL temp removed 00962 // else 00963 // { 00964 // echo "<p class='help'>If this is the first time you have used SiT! you may need to create the database, "; 00965 // echo "if you have the necessary MySQL permissions you can create the database automatically.<br />"; 00966 // echo "Alternatively you can create it manually by executing the SQL statement <br /><code>{$sql};</code></p"; 00967 // echo "<p><a href='setup.php?action=createdatabase' class='button'>Create Database</a></p>"; 00968 // } 00969 // //echo "<p>After creating the database run <a href='setup.php' class='button'>setup</a> again to create the database schema</p>"; 00970 if (empty($CONFIG['db_database']) OR empty($CONFIG['db_username'])) 00971 { 00972 echo "<p>You need to configure SiT to be able access the MySQL database.</p>"; 00973 echo setup_configure(); 00974 } 00975 } 00976 else 00977 { 00978 require (APPLICATION_LIBPATH . 'functions.inc.php'); 00979 00980 // Load the empty schema 00981 require ('setup-schema.php'); 00982 00983 // Connected to database and db selected 00984 echo "<p>Connected to database - ok</p>"; 00985 // Check to see if we're already installed 00986 $sql = "SHOW TABLES LIKE '{$dbUsers}'"; 00987 $result = mysql_query($sql); 00988 if (mysql_error()) 00989 { 00990 echo "<p class='error'>Could not find a users table, an error occurred ".mysql_error()."</p>"; 00991 exit; 00992 } 00993 if (mysql_num_rows($result) < 1) 00994 { 00995 echo "<h2>Creating new database schema...</h2>"; 00996 // No users table or empty users table, proceed to install 00997 // $installed_schema = 0; 00998 // $installed_schema = substr(end(array_keys($upgrade_schema[$application_version*100])),1); 00999 $errors = setup_exec_sql($schema); 01000 if ($_SESSION['sampledata'] == TRUE) 01001 { 01002 // Install sample data 01003 echo "<p>Installing sample data...</p>"; 01004 $errors = $errors + setup_exec_sql($sampledata_sql); 01005 } 01006 // Update the system version 01007 if ($errors < 1) 01008 { 01009 $vsql = "REPLACE INTO `{$dbSystem}` ( `id`, `version`) VALUES (0, $application_version)"; 01010 mysql_query($vsql); 01011 if (mysql_error()) 01012 { 01013 $html .= "<p class='error'>Could not store new schema version number '{$application_version}'. ".mysql_error()."</p>"; 01014 } 01015 else 01016 { 01017 $html .= "<p>Schema successfully created as version {$application_version}</p>"; 01018 } 01019 } 01020 else 01021 { 01022 $html .= "<p class='error'><strong>Summary</strong>: {$errors} Error(s) occurred while creating the schema, "; 01023 $html .= "please resolve the problems reported and then try running setup again.</p>"; 01024 } 01025 echo $html; 01026 /* // Set the system version number 01027 $sql = "REPLACE INTO `{$dbSystem}` ( id, version) VALUES (0, $application_version)"; 01028 mysql_query($sql); 01029 if (mysql_error()) trigger_error(mysql_error(),E_USER_ERROR);*/ 01030 $installed_version = $application_version; 01031 echo "<h2>Database schema created</h2>"; 01032 if ($errors > 0) 01033 { 01034 echo "<p>If these errors do not appear to be caused by your configuration or setup, "; 01035 echo "please log a bug <a href='http://sitracker.org/wiki/Bugs'>here</a>"; 01036 echo ", with the full error message.</p>"; 01037 } 01038 else 01039 { 01040 echo "<p>You can now proceed with the next step.</p>"; 01041 } 01042 echo setup_button('checkinstallcomplete', 'Next'); 01043 } 01044 else 01045 { 01046 // users table exists and has at least one record, must be already installed 01047 // Do upgrade 01048 01049 // Have a look what version is installed 01050 // First look to see if the system table exists 01051 $exists = mysql_query("SELECT 1 FROM `{$dbSystem}` LIMIT 0"); 01052 if (!$exists) 01053 { 01054 echo "<p class='error'>Could not find a 'system' table which probably means you have a version prior to v3.21</p>"; 01055 $installed_version = 3.00; 01056 } 01057 else 01058 { 01059 $sql = "SELECT `version` FROM `{$dbSystem}` WHERE id = 0"; 01060 $result = mysql_query($sql); 01061 if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING); 01062 list($installed_version) = mysql_fetch_row($result); 01063 01064 /* if ($installed_version >= 3.35) 01065 { 01066 $sql = "SELECT `schemaversion` FROM `{$dbSystem}` WHERE id = 0"; 01067 $result = mysql_query($sql); 01068 if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING); 01069 list($installed_schema) = mysql_fetch_row($result); 01070 } 01071 else 01072 { 01073 $installed_schema = 334; 01074 $sql = "SHOW COLUMNS FROM `{$dbSystem}` WHERE Field='schema'"; 01075 $result = mysql_query($sql); 01076 if (mysql_num_rows($result) < 1) 01077 { 01078 $sql = "ALTER TABLE `{$dbSystem}` ADD `schemaversion` BIGINT UNSIGNED NOT NULL COMMENT 'DateTime in YYYYMMDDHHMM format'"; 01079 $result = mysql_query($sql); 01080 if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING); 01081 } 01082 }*/ 01083 } 01084 01085 if (empty($installed_version)) 01086 { 01087 echo "<p class='error'>Fatal setup error - Could not determine version of installed software. Try wiping your installation and installing from clean. (sorry)</p>"; 01088 echo setup_button('', 'Restart setup'); 01089 exit; 01090 } 01091 01092 echo "<h2>Installed OK</h2>"; 01093 01094 if ($_REQUEST['action'] == 'upgrade') 01095 { 01096 /***************************** 01097 * Do pre-upgrade tasks here * 01098 *****************************/ 01099 01100 if ($installed_version < 3.35) 01101 { 01102 //Get anyone with var_notify_on_reassign on so we can add them a trigger later 01103 $sql = "SELECT * FROM `{$dbUsers}` WHERE var_notify_on_reassign='true'"; 01104 if ($result = @mysql_query($sql)) 01105 { 01106 while ($row = mysql_fetch_object($result)) 01107 { 01108 $assign_notify_users[] = $row->id; 01109 } 01110 } 01111 01112 //any kbarticles with private content, change whole type 01113 $sql = "SELECT docid, distribution FROM `{$dbKBContent} WHERE distribution!='public'"; 01114 if ($result = @mysql_query($sql)) 01115 { 01116 while ($row = @mysql_fetch_object($result)) 01117 { 01118 if ($row->distribution == 'private') 01119 { 01120 $kbprivate[] = $row->docid; 01121 } 01122 elseif (!in_array($row->docid, $kbprivate)) 01123 { 01124 $kbrestricted[] = $row->docid; 01125 } 01126 } 01127 } 01128 } 01129 01130 if ($installed_version < 3.45) 01131 { 01132 $sql = "SELECT i.id FROM `{$GLOBALS['dbIncidents']}` AS i, `{$GLOBALS['dbContacts']}` AS c, `{$dbServiceLevels}` AS sl "; 01133 $sql .= "WHERE c.id = i.contact "; 01134 $sql .= "AND sl.tag = i.servicelevel AND sl.priority = i.priority AND sl.timed = 'yes' "; 01135 $sql .= "AND i.status = 2 "; // Only want closed incidents, dont want awaiting closure as they could be reactivated 01136 01137 $result = mysql_query($sql); 01138 if (mysql_error()) trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING); 01139 if (mysql_num_rows($result) > 0) 01140 { 01141 while ($obj = mysql_fetch_object($result)) 01142 { 01143 $asql = "SELECT DISTINCT origcolref, linkcolref "; 01144 $asql .= "FROM `{$dbLinks}` AS l, `{$dbLinkTypes}` AS lt "; 01145 $asql .= "WHERE l.linktype = 6 "; 01146 $asql .= "AND linkcolref = {$obj->id} "; 01147 $asql .= "AND direction = 'left'"; 01148 $aresult = mysql_query($asql); 01149 if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING); 01150 01151 if (mysql_num_rows($aresult) == 0) 01152 { 01153 $billing_upgrade[] = $obj->id; 01154 } 01155 } 01156 } 01157 } 01158 01159 /***************************** 01160 * UPGRADE THE SCHEMA * 01161 *****************************/ 01162 for ($v=(($installed_version*100)+1); $v<=($application_version*100); $v++) 01163 { 01164 $html = ''; 01165 if (!empty($upgrade_schema[$v])) 01166 { 01167 $newversion = number_format(($v/100),2); 01168 echo "<p>Updating schema from {$installed_version} to v{$newversion}…</p>"; 01169 $errors = setup_exec_sql($upgrade_schema[$v]); 01170 // Update the system version 01171 if ($errors < 1) 01172 { 01173 $vsql = "REPLACE INTO `{$dbSystem}` ( `id`, `version`) VALUES (0, $newversion)"; 01174 mysql_query($vsql); 01175 if (mysql_error()) 01176 { 01177 $html .= "<p class='error'>Could not store new schema version number '{$newversion}'. ".mysql_error()."</p>"; 01178 } 01179 else 01180 { 01181 $html .= "<p>Schema successfully updated to version {$newversion}.</p>"; 01182 } 01183 $installed_version = $newversion; 01184 $upgradeok = TRUE; 01185 } 01186 else 01187 { 01188 $html .= "<p class='error'><strong>Summary</strong>: {$errors} Error(s) occurred while updating the schema, "; 01189 $html .= "please resolve the problems reported and then try running setup again.</p>"; 01190 } 01191 echo $html; 01192 } 01193 } 01194 01195 /****************************** 01196 * Do Post-upgrade tasks here * 01197 ******************************/ 01198 if ($installed_version < 3.21) 01199 { 01200 echo "<p>Upgrading incidents data from version prior to 3.21...</p>"; 01201 // Fill the new servicelevel field in the incidents table using information from the maintenance contract 01202 echo "<p>Upgrading incidents table to store service level...</p>"; 01203 $sql = "SELECT *,i.id AS incidentid FROM `{$dbIncidents}` AS i, `{$dbMaintenance}` AS m, {$dbServiceLevels}` AS sl WHERE i.maintenanceid=m.id AND "; 01204 $sql .= "m.servicelevelid = sl.id "; 01205 $result = mysql_query($sql); 01206 while ($row = mysql_fetch_object($result)) 01207 { 01208 $sql = "UPDATE `{$dbIncidents}` SET servicelevel='{$row->tag}' WHERE id='{$row->incidentid}' AND servicelevel IS NULL LIMIT 1"; 01209 mysql_query($sql); 01210 if (mysql_error()) 01211 { 01212 trigger_error(mysql_error(),E_USER_WARNING); 01213 echo "<p><strong>FAILED:</strong> $sql</p>"; 01214 $upgradeok = FALSE; 01215 } 01216 else echo "<p><strong>OK:</strong> $sql</p>"; 01217 } 01218 echo "<p>".mysql_num_rows($result)." incidents upgraded</p>"; 01219 } 01220 01221 if ($installed_version < 3.35) 01222 { 01223 if ($CONFIG['closure_delay'] > 0 AND $CONFIG['closure_delay'] != 554400) 01224 { 01225 echo "<p>Inserting value from deprecated config variable <var>closure_delay</var> into scheduler</p>"; 01226 $sql = "UPDATE `{$dbScheduler}` SET params = '{$CONFIG['closure_delay']}' WHERE action = 'CloseIncidents' LIMIT 1"; 01227 mysql_query($sql); 01228 if (mysql_error()) 01229 { 01230 trigger_error(mysql_error(),E_USER_WARNING); 01231 echo "<p><strong>FAILED:</strong> $sql</p>"; 01232 $upgradeok = FALSE; 01233 } 01234 else echo "<p><strong>OK:</strong> $sql</p>"; 01235 } 01236 01237 //add trigger to users, NOTE we do user 1(admin's) in the schema 01238 $sql = "SELECT id FROM `{$dbUsers}` WHERE id > 1"; 01239 if ($result = @mysql_query($sql)) 01240 { 01241 echo '<p>Adding default triggers to existing users.</p>'; 01242 while ($row = mysql_fetch_row($result)) 01243 { 01244 setup_user_triggers($row->id); 01245 } 01246 } 01247 01248 //add the triggers for var_notify users from above 01249 if (is_array($assign_notify_users)) 01250 { 01251 echo '<p>Replacing "Notify on reassign" option with triggers</p>'; 01252 foreach ($assign_notify_users as $assign_user) 01253 { 01254 $sql = "INSERT INTO `{$dbTriggers}`(triggerid, userid, action, template, checks) "; 01255 $sql .= "VALUES('TRIGGER_INCIDENT_ASSIGNED', '$assign_user', 'ACTION_EMAIL', 'EMAIL_INCIDENT_REASSIGNED_USER_NOTIFY', '{userstatus} == {$assign_user}')"; 01256 mysql_query($sql); 01257 if (mysql_error()) trigger_error(mysql_error(),E_USER_ERROR); 01258 } 01259 } 01260 01261 //fix the visibility for KB articles 01262 if (is_array($kbprivate)) 01263 { 01264 foreach ($kbprivate as $article) 01265 { 01266 $articleID = intval($article); 01267 $sql = "UPDATE `{$dbKBArticles}` SET visibility='private' WHERE id='{$articleID}'"; 01268 mysql_query($sql); 01269 if (mysql_error()) trigger_error(mysql_error(),E_USER_ERROR); 01270 } 01271 } 01272 01273 if (is_array($kbrestricted)) 01274 { 01275 foreach ($kbrestricted as $article) 01276 { 01277 $articleID = intval($article); 01278 $sql = "UPDATE `{$dbKBArticles}` SET visibility='restricted' WHERE id='{$articleID}'"; 01279 mysql_query($sql); 01280 if (mysql_error()) trigger_error(mysql_error(),E_USER_ERROR); 01281 } 01282 } 01283 01284 if (is_array($billing_upgrade)) 01285 { 01286 foreach ($billing_upgrade AS $incident) 01287 { 01288 $r = close_billable_incident($incident); 01289 if (!$r) 01290 { 01291 trigger_error("Error upgrading {$incident} to new billing format", E_USER_WARNING); 01292 } 01293 } 01294 } 01295 } 01296 01297 if ($installed_version < 3.40) 01298 { 01299 //remove any brackets from checks as per mantis 197 01300 $sql = "UPDATE `triggers` SET `checks` = REPLACE(`checks`, '(', ''); "; 01301 $sql .= "UPDATE `triggers` SET `checks` = REPLACE(`checks`, ')', '')"; 01302 mysql_query($sql); 01303 if (mysql_error()) trigger_error(mysql_error(),E_USER_ERROR); 01304 } 01305 01306 if ($installed_version == $application_version) 01307 { 01308 echo "<p>Everything is up to date</p>"; 01309 echo "<p>See the <code>doc/UPGRADE</code> file for further upgrade notes.<br />"; 01310 } 01311 else 01312 { 01313 $upgradeok = TRUE; 01314 echo "<p>See the <code>doc/UPGRADE</code> file for further upgrade instructions and help.<br />"; 01315 01316 } 01317 01318 if ($installed_version >= 3.24) 01319 { 01320 //upgrade dashboard components. 01321 01322 $sql = "SELECT * FROM `{$dbDashboard}` WHERE enabled = 'true'"; 01323 $result = mysql_query($sql); 01324 if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING); 01325 01326 //echo "<h2>Dashboard</h2>"; 01327 while ($dashboardnames = mysql_fetch_object($result)) 01328 { 01329 $version = 1; 01330 include (dirname( __FILE__ ).DIRECTORY_SEPARATOR."plugins/dashboard_{$dashboardnames->name}.php"); 01331 $func = "dashboard_{$dashboardnames->name}_get_version"; 01332 01333 if (function_exists($func)) 01334 { 01335 $version = $func(); 01336 } 01337 01338 if ($version > $dashboardnames->version) 01339 { 01340 echo "<p>Upgrading {$dashboardnames->name} dashlet to v$version...</p>"; 01341 // apply all upgrades since running version 01342 $upgrade_func = "dashboard_{$dashboardnames->name}_upgrade"; 01343 01344 if (function_exists($upgrade_func)) 01345 { 01346 $dashboard_schema = $upgrade_func(); 01347 for ($i = $dashboardnames->version; $i <= $version; $i++) 01348 { 01349 setup_exec_sql($dashboard_schema[$i]); 01350 } 01351 01352 $upgrade_sql = "UPDATE `{$dbDashboard}` SET version = '{$version}' WHERE id = {$dashboardnames->id}"; 01353 mysql_query($upgrade_sql); 01354 if (mysql_error()) trigger_error(mysql_error(),E_USER_ERROR); 01355 01356 echo "<p>{$dashboardnames->name} upgraded</p>"; 01357 } 01358 else 01359 { 01360 echo "<p>No upgrade function for {$dashboardnames->name}</p>"; 01361 } 01362 } 01363 } 01364 } 01365 01366 if ($upgradeok) 01367 { 01368 // Update the system version number 01369 $sql = "REPLACE INTO `{$dbSystem}` ( id, version) VALUES (0, $application_version)"; 01370 mysql_query($sql); 01371 if (mysql_error()) trigger_error(mysql_error(),E_USER_ERROR); 01372 $installed_version = $application_version; 01373 echo "<h2>Upgrade complete</h2>"; 01374 echo "<p>Upgraded to v{$application_version}</p>"; 01375 include (APPLICATION_LIBPATH . 'triggers.inc.php'); 01376 trigger("TRIGGER_SIT_UPGRADED", array('applicationversion' => $application_version)); 01377 } 01378 else 01379 { 01380 echo "<p class='error'>Upgrade failed. Maybe you could try a fresh installation?</p>"; 01381 } 01382 } 01383 else 01384 { 01385 // $latest_schema = substr(end(array_keys($upgrade_schema[$application_version*100])),1); 01386 echo "<p>Your database schema is v".number_format($installed_version,2); 01387 01388 if ($installed_version >= 3.99) 01389 { 01390 echo "<p class='error'>Your SiT! Schema is designed for SiT! v4 or above, downgrading or running v{$application_version} with this schema is not recommended, probably won't work very well and may cause data loss. You have been warned.</p>"; 01391 } 01392 01393 // . "-{$installed_schema}"; 01394 //if ($installed_schema < $latest_schema) 01395 // echo ", the latest available schema is v".number_format($installed_version,2) . "-{$latest_schema}"; 01396 if ($installed_version < $application_version) echo ", after making a backup you should upgrade your schema to v{$application_version}"; 01397 echo "</p>"; 01398 01399 // Display SQL schema changes for svn versions 01400 if (substr($application_revision, 0, 3) == 'svn' AND !empty($upgrade_schema[$installed_version*100])) 01401 { 01402 echo "<p>You are running an <a href='http://sitracker.org/wiki/Development/Unreleased_Versions'>SVN version</a>, you should check that you have all of these schema changes: (some may have been added recently)</p>"; 01403 echo "<div style='border: 1px solid red;padding:10px; background: #FFFFC0; font-family:monospace; font-size: 80%; height:200px; overflow:scroll;'>"; 01404 echo nl2br($upgrade_schema[$installed_version*100]); 01405 echo "</div>"; 01406 } 01407 01408 if (is_array($upgrade_schema[$installed_version*100])) 01409 { 01410 foreach ($upgrade_schema[$installed_version*100] AS $possible_schema_updates => $nothing) 01411 { 01412 $possible_schema_updates = substr($possible_schema_updates, 1); 01413 if ($possible_schema_updates > $installed_schema) $schemaupgradeneeded = TRUE; 01414 else $schemaupgradeneeded = FALSE; 01415 } 01416 } 01417 if ($installed_version < $application_version OR $schemaupgradeneeded == TRUE) 01418 { 01419 echo setup_button('upgrade', 'Upgrade Schema'); 01420 } 01421 } 01422 01423 if ($_REQUEST['action'] == 'createadminuser' AND setup_check_adminuser() == FALSE) 01424 { 01425 $password = mysql_real_escape_string($_POST['newpassword']); 01426 $passwordagain = mysql_real_escape_string($_POST['passwordagain']); 01427 if ($password == $passwordagain) 01428 { 01429 $password = md5($password); 01430 $email = mysql_real_escape_string($_POST['email']); 01431 $sql = "INSERT INTO `{$dbUsers}` (`id`, `username`, `password`, `realname`, `roleid`, `title`, `signature`, `email`, `status`, `var_style`, `lastseen`) "; 01432 $sql .= "VALUES (1, 'admin', '$password', 'Administrator', 1, 'Administrator', 'Regards,\r\n\r\nSiT Administrator', '$email', '1', '8', NOW());"; 01433 mysql_query($sql); 01434 if (mysql_error()) 01435 { 01436 trigger_error(mysql_error(),E_USER_WARNING); 01437 echo "<p><strong>FAILED:</strong> $sql</p>"; 01438 } 01439 } 01440 else 01441 { 01442 echo "<p class='error'>Admin account not created, the passwords you entered did not match.</p>"; 01443 } 01444 } 01445 // Check installation 01446 echo "<h2>Checking installation...</h2>"; 01447 if ($cfg_file_writable) 01448 { 01449 $checkadminuser = setup_check_adminuser(); 01450 echo "<p class='error'>Important: The file permissions on the configuration file <var>{$config_filename}</var> file "; 01451 echo "allow it to be modified, we recommend you make this file read-only."; 01452 if (DIRECTORY_SEPARATOR == '/') 01453 { 01454 echo "<br />You can run the following shell command to make it read-only.<br /><br /><var>chmod 444 {$config_filename}</var>"; 01455 } 01456 else 01457 { 01458 echo "<br />You can run the following command from windows command prompt to make it read-only.<br /><br /><var>attrib +r {$config_filename}</var>"; 01459 } 01460 echo "</p>"; 01461 if ($checkadminuser == FALSE) echo "<p>You must set your config file to be read-only before setup can continue.</p>"; 01462 echo setup_button('', 'Re-check installation'); 01463 if ($checkadminuser == TRUE) 01464 { 01465 echo "<br />or<br /><br />"; 01466 echo "<form action='index.php' method='get'>"; 01467 echo "<input type='submit' value=\"Run SiT!\" />"; 01468 echo "</form>\n"; 01469 } 01470 } 01471 elseif (!isset($_REQUEST)) 01472 { 01473 echo "<p class='error'>SiT! requires PHP 5.0.0 or later</p>"; 01474 } 01475 elseif (@ini_get('register_globals')==1 OR strtolower(@ini_get('register_globals'))=='on') 01476 { 01477 echo "<p class='error'>SiT! strongly recommends that you change your php.ini setting <code>register_globals</code> to OFF.</p>"; 01478 } 01479 elseif (setup_check_adminuser() == FALSE) 01480 { 01481 echo "<p><span style='color: red; font-weight: bolder;'>Important:</span> you <strong>must</strong> create an admin account before you can use SiT</p>"; 01482 echo "<form action='setup.php' method='post'>\n"; 01483 echo "<p>Username:<br /><input type='text' name='username' value='admin' disabled='disabled' /> (cannot be changed)</p>"; 01484 echo "<p><label>Password:<br /><input type='password' name='newpassword' size='30' maxlength='50' /></label></p>"; 01485 echo "<p><label>Confirm Password:<br /><input type='password' name='passwordagain' size='30' maxlength='50' /></label></p>"; 01486 echo "<p><label>Email:<br /><input type='text' name='email' size='30' maxlength='255' /></label></p>"; 01487 echo "<p><input type='submit' value='Create Admin User' />"; 01488 echo "<input type='hidden' name='action' value='createadminuser' />"; 01489 echo "</form>"; 01490 } 01491 else 01492 { 01493 if ($installed_version < $application_version) 01494 { 01495 echo "<p>SiT! v".number_format($application_version,2)." is installed "; 01496 echo "but the database schema, which is for v".number_format($installed_version,2).", is out of date."; 01497 } 01498 else 01499 { 01500 echo "<p>SiT! v".number_format($installed_version,2)." is installed "; 01501 echo "and ready."; 01502 } 01503 echo "</p>"; 01504 echo "<form action='index.php' method='get'>"; 01505 echo "<input type='submit' value=\"Run SiT!\" />"; 01506 echo "</form>\n"; 01507 01508 if ($_SESSION['userid'] == 1) 01509 { 01510 echo "<br /><p>As administrator you can <a href='config.php'>reconfigure</a> SiT!</p>"; 01511 } 01512 } 01513 } 01514 } 01515 } 01516 } 01517 echo "<div style='margin-top: 50px;'>"; 01518 echo "<hr style='width: 50%; margin-left: 0px;'/>"; 01519 echo "<p><a href='http://sitracker.org/'>{$CONFIG['application_name']}</a> Setup | <a href='http://sitracker.org/wiki/Installation'>Installation Help</a></p>"; 01520 echo "<p></p>"; 01521 echo "</div>"; 01522 echo "\n</body>\n</html>"; 01523 ?>
For more help developing with SiT! see http://sitracker.org/wiki/DevelopmentHowTo