From 0e0f6dcb1435ce83b41b78dca5f20850d06bf26e Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Tue, 28 May 2013 17:26:18 -0400
Subject: [PATCH] Various bug-fixes for the upgrader

- timezone_offset was dropped from the config table at 1.7-dpr1
- upgrader: only apply five patches in one request
- upgrader: fix readPatchInfo to work correctly
- session: support migrating from 1.6 (again)
- config: support migrating from 1.6 (fallback)
- config: no default for 'isonline' setting
- config: fix SQL whitespace issue for schema signature fallbacks
- config: hash 1.6 versions in the schema signature lookup
- upgrader: fix logging bug in attachment migration
---
 include/class.config.php                      | 33 +++++++++++--------
 include/class.migrater.php                    |  2 +-
 include/class.osticket.php                    |  2 +-
 include/class.ostsession.php                  |  2 +-
 include/class.upgrader.php                    | 10 +++---
 ....patch.sql => 32de1766-852ca89e.patch.sql} |  7 ++--
 main.inc.php                                  |  2 +-
 setup/inc/sql/osTicket-mysql.sql              |  1 -
 setup/inc/sql/osTicket-mysql.sql.md5          |  2 +-
 9 files changed, 33 insertions(+), 28 deletions(-)
 rename include/upgrader/sql/{32de1766-75fce769.patch.sql => 32de1766-852ca89e.patch.sql} (98%)

diff --git a/include/class.config.php b/include/class.config.php
index 7123205dd..d01d1be1b 100644
--- a/include/class.config.php
+++ b/include/class.config.php
@@ -39,13 +39,9 @@ class Config {
         $sql='SELECT id, `key`, value FROM '.$this->table
             .' WHERE `'.$this->section_column.'` = '.db_input($this->section);
 
-        if(!($res=db_query($sql)) || !db_num_rows($res))
-            return false;
-
-        while ($row = db_fetch_array($res))
-            $this->config[$row['key']] = $row;
-
-        return true;
+        if(($res=db_query($sql)) && db_num_rows($res))
+            while ($row = db_fetch_array($res))
+                $this->config[$row['key']] = $row;
     }
 
     function getNamespace() {
@@ -59,6 +55,7 @@ class Config {
             return $this->session[$key];
         elseif ($default !== null)
             return $this->set($key, $default);
+        return null;
     }
 
     function exists($key) {
@@ -75,11 +72,11 @@ class Config {
     }
 
     function create($key, $value) {
-        $res = db_query('INSERT INTO '.$this->table
+        $sql = 'INSERT INTO '.$this->table
             .' SET `'.$this->section_column.'`='.db_input($this->section)
             .', `key`='.db_input($key)
-            .', value='.db_input($value));
-        if (!db_query($res) || !($id=db_insert_id()))
+            .', value='.db_input($value);
+        if (!db_query($sql) || !($id=db_insert_id()))
             return false;
 
         $this->config[$key] = array('key'=>$key, 'value'=>$value, 'id'=>$id);
@@ -123,6 +120,14 @@ class OsticketConfig extends Config {
     function OsticketConfig($section=null) {
         parent::Config($section);
 
+        if (count($this->config) == 0) {
+            // Fallback for osticket < 1.7@852ca89e
+            $sql='SELECT * FROM '.$this->table.' WHERE id = 1';
+            if (($res=db_query($sql)) && db_num_rows($res))
+                foreach (db_fetch_array($res) as $key=>$value)
+                    $this->config[$key] = array('value'=>$value);
+        }
+
         //Get the default time zone
         // We can't JOIN timezone table above due to upgrade support.
         if ($this->get('default_timezone_id')) {
@@ -146,7 +151,7 @@ class OsticketConfig extends Config {
     }
 
     function isOnline() {
-        return ($this->get('isonline', false));
+        return ($this->get('isonline'));
     }
 
     function isKnowledgebaseEnabled() {
@@ -166,19 +171,19 @@ class OsticketConfig extends Config {
         // 1.7 after namespaced configuration, other namespace
         if ($section) {
             $sql='SELECT value FROM '.$this->table
-                .'WHERE `key` = "schema_signature" and namespace='.db_input($section);
+                .' WHERE `key` = "schema_signature" and namespace='.db_input($section);
             if (($res=db_query($sql, false)) && db_num_rows($res))
                 return db_result($res);
         }
 
         // 1.7 before namespaced configuration
         $sql='SELECT `schema_signature` FROM '.$this->table
-            .'WHERE id=1';
+            .' WHERE id=1';
         if (($res=db_query($sql, false)) && db_num_rows($res))
             return db_result($res);
 
         // old version 1.6
-        return self::getDBVersion();
+        return md5(self::getDBVersion());
     }
 
     function getDBTZoffset() {
diff --git a/include/class.migrater.php b/include/class.migrater.php
index 93854d797..ba26d091e 100644
--- a/include/class.migrater.php
+++ b/include/class.migrater.php
@@ -212,7 +212,7 @@ class AttachmentMigrater {
         if(!($res=db_query($sql)))
             return $this->error('Unable to query DB for attached files to migrate!');
 
-        $ost->logDebug('Found '.db_num_rows($res).' attachments to migrate');
+        $ost->logDebug("Attachment migration", 'Found '.db_num_rows($res).' attachments to migrate');
         if(!db_num_rows($res))
             return 0;  //Nothing else to do!!
 
diff --git a/include/class.osticket.php b/include/class.osticket.php
index cd130e1ff..1a1945218 100644
--- a/include/class.osticket.php
+++ b/include/class.osticket.php
@@ -229,7 +229,7 @@ class osTicket {
         if($email) {
             $email->sendAlert($to, $subject, $message);
         } else {//no luck - try the system mail.
-            Email::sendmail($to, $subject, $message, sprintf('"osTicket Alerts"<%s>',$to));
+            Mailer::sendmail($to, $subject, $message, sprintf('"osTicket Alerts"<%s>',$to));
         }
 
         //log the alert? Watch out for loops here.
diff --git a/include/class.ostsession.php b/include/class.ostsession.php
index bad26c1e7..f2be6b18b 100644
--- a/include/class.ostsession.php
+++ b/include/class.ostsession.php
@@ -26,7 +26,7 @@ class osTicketSession {
         if(!$this->ttl)
             $this->ttl=SESSION_TTL;
 
-        if ($this->read(session_id()) !== false) {
+        if (!OsticketConfig::getDBVersion()) {
             //Set handlers.
             session_set_save_handler(
                 array(&$this, 'open'),
diff --git a/include/class.upgrader.php b/include/class.upgrader.php
index 8b86b9e57..8f6fea223 100644
--- a/include/class.upgrader.php
+++ b/include/class.upgrader.php
@@ -148,10 +148,11 @@ class Upgrader extends SetupWizard {
 
     function readPatchInfo($patch) {
         $info = array();
-        if (preg_match('/\*(.*)\*/', file_get_contents($patch), $matches)) {
-            if (preg_match('/@([\w\d_-]+)\s+(.*)$/', $matches[0], $matches2))
+        if (preg_match(':/\*\*(.*)\*/:s', file_get_contents($patch), $matches)) {
+            if (preg_match_all('/@([\w\d_-]+)\s+(.*)$/m', $matches[0],
+                        $matches2, PREG_SET_ORDER))
                 foreach ($matches2 as $match)
-                    $info[$match[0]] = $match[1];
+                    $info[$match[1]] = $match[2];
         }
         if (!isset($info['version']))
             $info['version'] = substr(basename($patch), 9, 8);
@@ -249,7 +250,8 @@ class Upgrader extends SetupWizard {
         if(!($max_time = ini_get('max_execution_time')))
             $max_time = 300; //Apache/IIS defaults.
 
-        foreach ($patches as $patch) {
+        // Apply up to five patches at a time
+        foreach (array_slice($patches, 0, 5) as $patch) {
             //TODO: check time used vs. max execution - break if need be
             if (!$this->load_sql_file($patch, $this->getTablePrefix()))
                 return false;
diff --git a/include/upgrader/sql/32de1766-75fce769.patch.sql b/include/upgrader/sql/32de1766-852ca89e.patch.sql
similarity index 98%
rename from include/upgrader/sql/32de1766-75fce769.patch.sql
rename to include/upgrader/sql/32de1766-852ca89e.patch.sql
index f4bd4b323..ba1768fa3 100644
--- a/include/upgrader/sql/32de1766-75fce769.patch.sql
+++ b/include/upgrader/sql/32de1766-852ca89e.patch.sql
@@ -1,6 +1,6 @@
 /**
  * @version v1.7.1
- * @signature 75fce76967f1549ffd296a44139dea57
+ * @signature 852ca89e1440e736d763b3b87f039bd7
  *
  *  - Changes config table to be key/value based and allows for
  *    configuration key clobbering by defining a namespace for the keys. The
@@ -18,9 +18,8 @@ CREATE TABLE `%TABLE_PREFIX%_config` (
   UNIQUE KEY (`namespace`, `key`)
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 
-INSERT INTO `%TABLE_PREFIX%_config` (`key`, `value`, `namespace`) VALUES 
+INSERT INTO `%TABLE_PREFIX%_config` (`key`, `value`, `namespace`) VALUES
   ('isonline', (SELECT `isonline` FROM `%TABLE_PREFIX%config` WHERE `id` = 1), 'core')
-, ('timezone_offset', (SELECT `timezone_offset` FROM `%TABLE_PREFIX%config` WHERE `id` = 1), 'core')
 , ('enable_daylight_saving', (SELECT `enable_daylight_saving` FROM `%TABLE_PREFIX%config` WHERE `id` = 1), 'core')
 , ('staff_ip_binding', (SELECT `staff_ip_binding` FROM `%TABLE_PREFIX%config` WHERE `id` = 1), 'core')
 , ('staff_max_logins', (SELECT `staff_max_logins` FROM `%TABLE_PREFIX%config` WHERE `id` = 1), 'core')
@@ -120,5 +119,5 @@ ALTER TABLE `%TABLE_PREFIX%_config` RENAME TO `%TABLE_PREFIX%config`;
 
 -- Finished with patch
 UPDATE `%TABLE_PREFIX%config`
-    SET `value` = '75fce76967f1549ffd296a44139dea57'
+    SET `value` = '852ca89e1440e736d763b3b87f039bd7'
 	WHERE `key` = 'schema_signature' AND `namespace` = 'core';
diff --git a/main.inc.php b/main.inc.php
index 7c6a9c2f7..69ec71bde 100644
--- a/main.inc.php
+++ b/main.inc.php
@@ -76,7 +76,7 @@
 
     #Current version && schema signature (Changes from version to version)
     define('THIS_VERSION','1.7.0+'); //Shown on admin panel
-    define('SCHEMA_SIGNATURE', '75fce76967f1549ffd296a44139dea57'); //MD5 signature of the db schema. (used to trigger upgrades)
+    define('SCHEMA_SIGNATURE', '852ca89e1440e736d763b3b87f039bd7'); //MD5 signature of the db schema. (used to trigger upgrades)
     #load config info
     $configfile='';
     if(file_exists(ROOT_DIR.'ostconfig.php')) //Old installs prior to v 1.6 RC5
diff --git a/setup/inc/sql/osTicket-mysql.sql b/setup/inc/sql/osTicket-mysql.sql
index 66f081d4a..6ad1a1810 100644
--- a/setup/inc/sql/osTicket-mysql.sql
+++ b/setup/inc/sql/osTicket-mysql.sql
@@ -91,7 +91,6 @@ CREATE TABLE `%TABLE_PREFIX%config` (
 
 INSERT INTO `%TABLE_PREFIX%config` (`namespace`, `key`, `value`) VALUES
   ('core', 'isonline', '0'),
-  ('core', 'timezone_offset', '0.0'),
   ('core', 'enable_daylight_saving', '0'),
   ('core', 'staff_ip_binding', '1'),
   ('core', 'staff_max_logins', '4'),
diff --git a/setup/inc/sql/osTicket-mysql.sql.md5 b/setup/inc/sql/osTicket-mysql.sql.md5
index 772bbd109..d55c64b4a 100644
--- a/setup/inc/sql/osTicket-mysql.sql.md5
+++ b/setup/inc/sql/osTicket-mysql.sql.md5
@@ -1 +1 @@
-75fce76967f1549ffd296a44139dea57
+852ca89e1440e736d763b3b87f039bd7
-- 
GitLab