From 7ad056f1cf283a49c31fc4c35dafdd05f3eafba1 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 11 Oct 2013 15:06:26 +0000
Subject: [PATCH] Validate port during installation

Add help information about the non-standard port number to the popup tips in
the install screen.

Support MySQL 'localhost' connections using a non-standard port number.
Ordinarily, MySQL will ignore the port setting on *nix systems if the host
is specified as 'localhost'
---
 include/mysqli.php            | 9 ++++++---
 setup/inc/class.installer.php | 7 +++++++
 setup/inc/footer.inc.php      | 2 +-
 setup/inc/header.inc.php      | 2 +-
 setup/tips.html               | 9 ++++++++-
 5 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/include/mysqli.php b/include/mysqli.php
index 2079318e1..737fd3a06 100644
--- a/include/mysqli.php
+++ b/include/mysqli.php
@@ -42,9 +42,12 @@ function db_connect($host, $user, $passwd, $options = array()) {
     $start = microtime(true);
     $port = 3306;
     if (strpos($host, ':') !== false) {
-        $_host = explode(':', $host);
-        $host = $_host[0];
-        $port = (int) $_host[1];
+        list($host, $port) = explode(':', $host);
+        // PHP may not honor the port number if connecting to 'localhost'
+        if (!strcasecmp($host, 'localhost'))
+            // XXX: Looks like PHP gethostbyname() is IPv4 only
+            $host = gethostbyname($host);
+        $port = (int) $port;
     }
     if (!@$__db->real_connect($host, $user, $passwd, null, $port)) # nolint
         return NULL;
diff --git a/setup/inc/class.installer.php b/setup/inc/class.installer.php
index ad2ee14a3..aeec06d2e 100644
--- a/setup/inc/class.installer.php
+++ b/setup/inc/class.installer.php
@@ -79,6 +79,13 @@ class Installer extends SetupWizard {
         if(!$this->errors['username'] && in_array(strtolower($vars['username']),array('admin','admins','username','osticket')))
             $this->errors['username']='Bad username';
 
+        // Support port number specified in the hostname with a colon (:)
+        list($host, $port) = explode(':', $vars['dbhost']);
+        if ($port && (!is_numeric($port) || !((int)$port)))
+            $this->errors['db'] = 'Database port number must be a number';
+        elseif ($port && ($port < 1 || $port > 65535))
+            $this->errors['db'] = 'Invalid database port number';
+
         //MYSQL: Connect to the DB and check the version & database (create database if it doesn't exist!)
         if(!$this->errors) {
             if(!db_connect($vars['dbhost'],$vars['dbuser'],$vars['dbpass']))
diff --git a/setup/inc/footer.inc.php b/setup/inc/footer.inc.php
index 90dc3ddc7..852062efe 100644
--- a/setup/inc/footer.inc.php
+++ b/setup/inc/footer.inc.php
@@ -2,6 +2,6 @@
             <div class="clear"></div>
         </div> <!-- content -->
     </div> <!-- wizard -->
-    <div id="footer" class="centered">Copyright &copy; 2012 <a target="_blank" href="http://osticket.com">osTicket.com</a></div>
+    <div id="footer" class="centered">Copyright &copy; 2013 <a target="_blank" href="http://osticket.com">osTicket.com</a></div>
 </body>
 </html>
diff --git a/setup/inc/header.inc.php b/setup/inc/header.inc.php
index 28aa849a1..a1ee36a1d 100644
--- a/setup/inc/header.inc.php
+++ b/setup/inc/header.inc.php
@@ -5,7 +5,7 @@
     <title><?php echo $wizard['title']; ?></title>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <link rel="stylesheet" href="css/wizard.css">
-    <script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
+    <script type="text/javascript" src="../js/jquery-1.8.3.min.js"></script>
     <script type="text/javascript" src="js/tips.js"></script>
     <script type="text/javascript" src="js/setup.js"></script>
 </head>
diff --git a/setup/tips.html b/setup/tips.html
index 1cd8de936..f10896885 100644
--- a/setup/tips.html
+++ b/setup/tips.html
@@ -36,7 +36,14 @@
 </div>
 <div id="t10">
 <b>MySQL Hostname</b>
-<p>Most hosts use 'localhost' for local database hostname. Check with your host if localhost fails. Default port set in php.ini is assumed.</p>
+<p>
+Most hosts use 'localhost' for local database hostname. Check with your
+host if localhost fails.
+</p>
+<p>
+Default port set in php.ini is assumed. A non-standard port number can be
+specified as <code>hostname:port</code>
+</p>
 </div>
 <div id="t11">
 <b>MySQL Database</b>
-- 
GitLab