From 5910e13d56abbe47d2495ed875042975a76d6b65 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Mon, 9 Dec 2013 14:37:12 -0600
Subject: [PATCH] mysqli: Allow specification of a unix socket

This patch allows for the specification of a socket in the database hostname
field, such as localhost:/path/to/mysql.sock
---
 include/mysqli.php            | 18 ++++++++++++------
 setup/inc/class.installer.php |  4 +---
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/include/mysqli.php b/include/mysqli.php
index 4049050e6..2ec03daee 100644
--- a/include/mysqli.php
+++ b/include/mysqli.php
@@ -39,18 +39,24 @@ function db_connect($host, $user, $passwd, $options = array()) {
         return NULL;
 
     $port = ini_get("mysqli.default_port");
+    $socket = ini_get("mysqli.default_socket");
     if (strpos($host, ':') !== false) {
-        list($host, $port) = explode(':', $host);
+        list($host, $portspec) = 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 ($portspec && is_numeric($portspec)) {
+            if (!strcasecmp($host, 'localhost'))
+                // XXX: Looks like PHP gethostbyname() is IPv4 only
+                $host = gethostbyname($host);
+            $port = (int) $portspec;
+        }
+        elseif ($portspec) {
+            $socket = $portspec;
+        }
     }
 
     // Connect
     $start = microtime(true);
-    if (!@$__db->real_connect($host, $user, $passwd, null, $port)) # nolint
+    if (!@$__db->real_connect($host, $user, $passwd, null, $port, $socket)) # nolint
         return NULL;
 
     //Select the database, if any.
diff --git a/setup/inc/class.installer.php b/setup/inc/class.installer.php
index 22f06ee03..61741a5b7 100644
--- a/setup/inc/class.installer.php
+++ b/setup/inc/class.installer.php
@@ -81,9 +81,7 @@ class Installer extends SetupWizard {
 
         // 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))
+        if ($port && is_numeric($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!)
-- 
GitLab