From 1f3c63f8926faabde825aa8b00fe47b6a205d3ef Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Fri, 14 Dec 2012 15:14:57 -0500
Subject: [PATCH] Make installation idiotproof - avoid database overwrite.

---
 setup/inc/class.installer.php  |  7 +++++++
 setup/inc/file-perm.inc.php    |  4 ++--
 setup/inc/file-unclean.inc.php |  2 +-
 setup/inc/install.inc.php      | 26 +++++++++++++-------------
 setup/install.php              |  7 +++++--
 5 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/setup/inc/class.installer.php b/setup/inc/class.installer.php
index 2d284eced..f355fbe04 100644
--- a/setup/inc/class.installer.php
+++ b/setup/inc/class.installer.php
@@ -89,6 +89,13 @@ class Installer extends SetupWizard {
                 $this->errors['db']='Unable to create the database.';
             } elseif(!db_select_database($vars['dbname'])) {
                 $this->errors['dbname']='Unable to select the database';
+            } else {
+                //Abort if we have another installation (or table) with same prefix.
+                $sql = 'SELECT * FROM `'.$vars['prefix'].'config` LIMIT 1';
+                if(mysql_query($sql)) {
+                    $this->errors['err'] = 'We have a problem - another installation with same table prefix exists!';
+                    $this->errors['prefix'] = 'Prefix already in-use';
+                }
             }
         }
 
diff --git a/setup/inc/file-perm.inc.php b/setup/inc/file-perm.inc.php
index 7f93a302c..6a158082d 100644
--- a/setup/inc/file-perm.inc.php
+++ b/setup/inc/file-perm.inc.php
@@ -9,9 +9,9 @@ if(!defined('SETUPINC')) die('Kwaheri!');
              </p>
             </div>
             <h3>Solution: <font color="red"><?php echo $errors['err']; ?></font></h3>
-            Please follow the instructions below to give read and write access to the web server.
+            Please follow the instructions below to give read and write access to the web server user.
             <ul>
-                <li><b>CLI</b>:<br><i>chmod 0777  include/ost-config.php</i></li>
+                <li><b>CLI</b>:<br><i>chmod 0666  include/ost-config.php</i></li>
                 <li><b>FTP</b>:<br>Using WS_FTP this would be right hand clicking on the fil, selecting chmod, and then giving all permissions to the file.</li>
                 <li><b>Cpanel</b>:<br>Click on the file, select change permission, and then giving all permissions to the file.</li>
             </ul>
diff --git a/setup/inc/file-unclean.inc.php b/setup/inc/file-unclean.inc.php
index 309fad10b..3c194b60a 100644
--- a/setup/inc/file-unclean.inc.php
+++ b/setup/inc/file-unclean.inc.php
@@ -4,7 +4,7 @@ if(!defined('SETUPINC')) die('Kwaheri!');
     <div id="main">
             <h1 style="color:#FF7700;">osTicket is already installed?</h1>
             <div id="intro">
-             <p>Configuration file already changed - which could mean osTicket is already installed or the config file is currupted. If you are trying to upgrade osTicket, then go to <a href="../scp/" >Admin Panel</a>.</p>
+             <p>Configuration file already changed - which could mean osTicket is already installed or the config file is currupted. If you are trying to upgrade osTicket, then go to <a href="../scp/admin.php" >Admin Panel</a>.</p>
 
              <p>If you believe this is in error, please try replacing the config file with a unchanged template copy and try again or get technical help.</p>
              <p>Refer to the <a target="_blank" href="http://osticket.com/wiki/Installation">Installation Guide</a> on the wiki for more information.</p>
diff --git a/setup/inc/install.inc.php b/setup/inc/install.inc.php
index 7f050baef..cb5f2d82b 100644
--- a/setup/inc/install.inc.php
+++ b/setup/inc/install.inc.php
@@ -16,13 +16,13 @@ $info=($_POST && $errors)?Format::htmlchars($_POST):array('prefix'=>'ost_','dbho
                 </div>
                 <div class="row">
                     <label>Helpdesk Name:</label>
-                    <input type="text" name="name" size="30" tabindex="1" value="<?php echo $info['name']; ?>">
+                    <input type="text" name="name" size="45" tabindex="1" value="<?php echo $info['name']; ?>">
                     <a class="tip" href="#t1">?</a>
                     <font class="error"><?php echo $errors['name']; ?></font>
                 </div>
                 <div class="row">
                     <label>Default Email:</label>
-                    <input type="text" name="email" size="30" tabindex="2" value="<?php echo $info['email']; ?>">
+                    <input type="text" name="email" size="45" tabindex="2" value="<?php echo $info['email']; ?>">
                     <a class="tip" href="#t2">?</a>
                     <font class="error"><?php echo $errors['email']; ?></font>
                 </div>
@@ -31,37 +31,37 @@ $info=($_POST && $errors)?Format::htmlchars($_POST):array('prefix'=>'ost_','dbho
                 <span class="subhead">Your primary administrator account - you can add more users later.</span>
                 <div class="row">
                     <label>First Name:</label>
-                    <input type="text" name="fname" size="30" tabindex="3" value="<?php echo $info['fname']; ?>">
+                    <input type="text" name="fname" size="45" tabindex="3" value="<?php echo $info['fname']; ?>">
                     <a class="tip" href="#t3">?</a>
                     <font class="error"><?php echo $errors['fname']; ?></font>
                 </div>
                 <div class="row">
                     <label>Last Name:</label>
-                    <input type="text" name="lname" size="30" tabindex="4" value="<?php echo $info['lname']; ?>">
+                    <input type="text" name="lname" size="45" tabindex="4" value="<?php echo $info['lname']; ?>">
                     <a class="tip" href="#t4">?</a>
                     <font class="error"><?php echo $errors['lname']; ?></font>
                 </div>
                 <div class="row">
                     <label>Email Address:</label>
-                    <input type="text" name="admin_email" size="30" tabindex="5" value="<?php echo $info['admin_email']; ?>">
+                    <input type="text" name="admin_email" size="45" tabindex="5" value="<?php echo $info['admin_email']; ?>">
                     <a class="tip" href="#t5">?</a>
                     <font class="error"><?php echo $errors['admin_email']; ?></font>
                 </div>
                 <div class="row">
                     <label>Username:</label>
-                    <input type="text" name="username" size="30" tabindex="6" value="<?php echo $info['username']; ?>" autocomplete="off">
+                    <input type="text" name="username" size="45" tabindex="6" value="<?php echo $info['username']; ?>" autocomplete="off">
                     <a class="tip" href="#t6">?</a>
                     <font class="error"><?php echo $errors['username']; ?></font>
                 </div>
                 <div class="row">
                     <label> Password:</label>
-                    <input type="password" name="passwd" size="30" tabindex="7" value="<?php echo $info['passwd']; ?>" autocomplete="off">
+                    <input type="password" name="passwd" size="45" tabindex="7" value="<?php echo $info['passwd']; ?>" autocomplete="off">
                     <a class="tip" href="#t7">?</a>
                     <font class="error"><?php echo $errors['passwd']; ?></font>
                 </div>
                 <div class="row">
                     <label>Retype Password:</label>
-                    <input type="password" name="passwd2" size="30" tabindex="8" value="<?php echo $info['passwd2']; ?>">
+                    <input type="password" name="passwd2" size="45" tabindex="8" value="<?php echo $info['passwd2']; ?>">
                     <a class="tip" href="#t8">?</a>
                     <font class="error"><?php echo $errors['passwd2']; ?></font>
                 </div>
@@ -70,31 +70,31 @@ $info=($_POST && $errors)?Format::htmlchars($_POST):array('prefix'=>'ost_','dbho
                 <span class="subhead">Database connection information <font class="error"><?php echo $errors['db']; ?></font></span>
                 <div class="row">
                     <label>MySQL Table Prefix:</label>
-                    <input type="text" name="prefix" size="30" tabindex="9" value="<?php echo $info['prefix']; ?>">
+                    <input type="text" name="prefix" size="45" tabindex="9" value="<?php echo $info['prefix']; ?>">
                     <a class="tip" href="#t9">?</a>
                     <font class="error"><?php echo $errors['prefix']; ?></font>
                 </div>
                 <div class="row">
                     <label>MySQL Hostname:</label>
-                    <input type="text" name="dbhost" size="30" tabindex="10" value="<?php echo $info['dbhost']; ?>">
+                    <input type="text" name="dbhost" size="45" tabindex="10" value="<?php echo $info['dbhost']; ?>">
                     <a class="tip" href="#t10">?</a>
                     <font class="error"><?php echo $errors['dbhost']; ?></font>
                 </div>
                 <div class="row">
                     <label>MySQL Database:</label>
-                    <input type="text" name="dbname" size="30" tabindex="11" value="<?php echo $info['dbname']; ?>">
+                    <input type="text" name="dbname" size="45" tabindex="11" value="<?php echo $info['dbname']; ?>">
                     <a class="tip" href="#t11">?</a>
                     <font class="error"><?php echo $errors['dbname']; ?></font>
                 </div>
                 <div class="row">
                     <label>MySQL Username:</label>
-                    <input type="text" name="dbuser" size="30" tabindex="12" value="<?php echo $info['dbuser']; ?>">
+                    <input type="text" name="dbuser" size="45" tabindex="12" value="<?php echo $info['dbuser']; ?>">
                     <a class="tip" href="#t12">?</a>
                     <font class="error"><?php echo $errors['dbuser']; ?></font>
                 </div>
                 <div class="row">
                     <label>MySQL Password:</label>
-                    <input type="password" name="dbpass" size="30" tabindex="13" value="<?php echo $info['dbpass']; ?>">
+                    <input type="password" name="dbpass" size="45" tabindex="13" value="<?php echo $info['dbpass']; ?>">
                     <a class="tip" href="#t13">?</a>
                     <font class="error"><?php echo $errors['dbpass']; ?></font>
                 </div>
diff --git a/setup/install.php b/setup/install.php
index d019598e5..5f590da1c 100644
--- a/setup/install.php
+++ b/setup/install.php
@@ -80,7 +80,6 @@ if($_POST && $_POST['s']) {
     $_SESSION['ost_installer']['s']='done';
 }
 
-
 switch(strtolower($_SESSION['ost_installer']['s'])) {
     case 'config':
     case 'install':
@@ -105,7 +104,11 @@ switch(strtolower($_SESSION['ost_installer']['s'])) {
             $inc='install-prereq.inc.php';
         break;
     default:
-         $inc='install-prereq.inc.php';
+        //Fail IF any of the old config files exists.
+        if(file_exists(INCLUDE_DIR.'settings.php') || file_exists(ROOT_DIR.'ostconfig.php'))
+            $inc='file-unclean.inc.php';
+        else
+            $inc='install-prereq.inc.php';
 }
 
 require(INC_DIR.'header.inc.php');
-- 
GitLab