source: branches/1.4/install/install-upgrade.php @ 2000

Revision 2000, 29.6 KB checked in by shibuya246, 3 years ago (diff)

[Branch 1.4] change post_title column from navchar to text

Line 
1<?php
2/**
3 * Upgrade Hotaru CMS
4 *
5 * Steps through the set-up process, creating database tables and registering
6 * the Admin user. Note: You must delete this file after installation as it
7 * poses a serious security risk if left.
8 *
9 * PHP version 5
10 *
11 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation, either version 3 of
14 * the License, or (at your option) any later version.
15 *
16 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
22 *
23 * @category  Content Management System
24 * @package   HotaruCMS
25 * @author    Nick Ramsay <admin@hotarucms.org>
26 * @copyright Copyright (c) 2010, Hotaru CMS
27 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
28 * @link      http://www.hotarucms.org/
29 */
30
31
32$h = new Hotaru(); // must come before language inclusion
33$sql = "SELECT miscdata_value FROM " . TABLE_MISCDATA . " WHERE miscdata_key = %s";
34$old_version = $h->db->get_var($h->db->prepare($sql, "hotaru_version"));
35//require_once(INSTALL . 'install_language.php');    // language file for install
36
37// delete existing cache
38$h->deleteFiles(CACHE . 'db_cache');
39$h->deleteFiles(CACHE . 'css_js_cache');
40$h->deleteFiles(CACHE . 'rss_cache');
41$h->deleteFiles(CACHE . 'lang_cache');
42$h->deleteFiles(CACHE . 'html_cache');
43
44$step = $h->cage->get->getInt('step');        // Installation steps.
45
46switch ($step) {
47        case 0:
48                //upgrade_welcome();     // "Welcome to Hotaru CMS.
49                break;
50        case 1:
51                upgrade_check($h, $old_version);
52                break;
53        case 2:
54                do_upgrade($h, $old_version);
55                upgrade_complete($h);    // Delete "install" folder. Visit your site"
56                break;
57        case 3:
58                upgrade_plugins($h);
59                break;
60        default:               
61                upgrade_welcome();
62                break;
63}
64
65exit;
66
67
68
69/**
70 * Step 1 of upgrade - checks existing version available and confirms details
71 */
72function upgrade_check($h, $old_version) {
73        global $lang;
74       
75        echo html_header();
76
77        // Step title
78        echo "<h2>" . $lang['upgrade_step1'] . "</h2>\n";
79
80        // Current version
81        if ( isset($old_version) )
82            echo "<div class='install_content'>" . $lang['upgrade_step1_old_version'] . $old_version . "</div>\n";
83        else
84            echo "<div class='install_content'>" . $lang['upgrade_step1_old_no_version'] . "</div>\n";
85
86        if ($h->version > $old_version)
87            echo "<div class='install_content'>" . $lang['upgrade_step1_details'] . "</div>\n";
88        else
89            echo "<div class='install_content'>" . $lang['upgrade_step1_current_version'] . "</div>\n";
90
91        // Previous/Next buttons
92        echo "<div class='back button''><a href='index.php?step=0&action=upgrade'>" . $lang['install_back'] . "</a></div>\n";
93        echo "<div class='next button''><a href='?step=2&action=upgrade'>" . $lang['install_next'] . "</a></div>\n";
94
95        echo html_footer();
96}
97
98   
99/**
100 * Step 2 of upgrade - shows completion.
101 */
102function upgrade_complete($h)
103{
104        global $lang;
105        global $cage;
106        $delete = $cage->post->getAlpha('delete');        // delete install folder.
107        $folder_deleted = 0;
108
109        if ($delete) {
110            // try to delete the folder
111            //$folder_deleted = delTree('install');       
112            $folder_deleted = 2;
113            // if was deleted then redirect to baseurl
114            if ($folder_deleted == 1) header("Location: /index.php" );
115        }
116
117        echo html_header();
118       
119        // Step title
120        echo "<h2>" . $lang['upgrade_step2'] . "</h2>\n";
121
122        // Step content
123        if ($folder_deleted == 0) echo "<div class='install_content'>" . $lang['install_step4_installation_complete'] . "</div>\n";
124        echo "<div class='install_content'>" . $lang['install_step4_installation_delete'] . "</div>\n";
125
126        if ($folder_deleted == 0) {
127            // Confirm delete and continue install
128//          echo "<div class='install_content'>" . $lang['install_step4_installation_delete_folder'] . "</div>\n";
129//          echo "<form name='install_admin_reg_form' action='index.php?step=2&action=upgrade' method='post'>\n";
130//          echo "<input type='hidden' name='csrf' value='" . $h->csrfToken . "' />";
131//          echo "<input type='hidden' name='delete' value='folder' />";
132//          echo "<input type='hidden' name='step' value='2' />";
133//
134//          echo "<input class='update button' type='submit' value='" . $lang['install_step4_form_delete_folder'] . "' />";
135//          echo "</div></form>\n";
136        } else {
137            echo "<br/><img src='../content/admin_themes/admin_default/images/delete.png' style='float:left; margin-left:12px;'>";
138            echo "<div class='install_content'><span style='color: red;'>" . $lang['install_step1_warning'] . "</span>: " . $lang['install_step4_installation_delete_failed'] . "</div>\n";
139        }       
140
141        // Previous/Next buttons
142        echo "<div class='back button''><a href='index.php?step=1&action=upgrade'>" . $lang['install_back'] . "</a></div>\n";
143        echo "<div class='next button''><a href='index.php?step=3&action=upgrade'>" . $lang['install_next'] . "</a></div>\n";
144       
145        echo html_footer();   
146}
147
148
149
150/**
151 * Do Upgrade
152 */
153function do_upgrade($h, $old_version)
154{
155        // can't upgrade from pre-1.0 versions of Hotaru.
156       
157        // 1.0 to 1.0.1
158        if ($old_version == "1.0") {
159
160                // Change "positive" to 10
161                $sql = "UPDATE " . TABLE_POSTVOTES . " SET vote_rating = %d WHERE vote_rating = %s";
162                $h->db->query($h->db->prepare($sql, 10, 'positive'));
163               
164                // Change "negative" to -10
165                $sql = "UPDATE " . TABLE_POSTVOTES . " SET vote_rating = %d WHERE vote_rating = %s";
166                $h->db->query($h->db->prepare($sql, -10, 'negative'));
167               
168                // Change "alert" to -999
169                $sql = "UPDATE " . TABLE_POSTVOTES . " SET vote_rating = %d WHERE vote_rating = %s";
170                $h->db->query($h->db->prepare($sql, -999, 'alert'));
171               
172                // Alter the PostVotes table so the vote rating is an INT
173                $sql = "ALTER TABLE " . TABLE_POSTVOTES . " CHANGE vote_rating vote_rating smallint(11) NOT NULL DEFAULT %d";
174                $h->db->query($h->db->prepare($sql, 0));
175               
176                // check there are default permissions present and add if necessary
177                $sql = "SELECT miscdata_id FROM " . TABLE_MISCDATA . " WHERE miscdata_key = %s";
178                $result = $h->db->get_var($h->db->prepare($sql, 'permissions'));
179                if (!$result) {
180                        // Default permissions
181                        $perms['options']['can_access_admin'] = array('yes', 'no');
182                        $perms['can_access_admin']['admin'] = 'yes';
183                        $perms['can_access_admin']['supermod'] = 'yes';
184                        $perms['can_access_admin']['default'] = 'no';
185                        $perms = serialize($perms);
186                       
187                        $sql = "INSERT INTO " . TABLE_MISCDATA . " (miscdata_key, miscdata_value, miscdata_default) VALUES (%s, %s, %s)";
188                        $h->db->query($h->db->prepare($sql, 'permissions', $perms, $perms));
189                }
190               
191                // check there are default user_settings present and add if necessary
192                $sql = "SELECT miscdata_id FROM " . TABLE_MISCDATA . " WHERE miscdata_key = %s";
193                $result = $h->db->get_var($h->db->prepare($sql, 'user_settings'));
194                if (!$result) {
195                        // default settings
196                        $sql = "INSERT INTO " . TABLE_MISCDATA . " (miscdata_key, miscdata_value, miscdata_default) VALUES (%s, %s, %s)";
197                        $h->db->query($h->db->prepare($sql, 'user_settings', '', ''));
198                }
199               
200                // update "old version" for next set of upgrades
201                $old_version = "1.0.1";
202        }
203
204
205        // 1.0.1 to 1.0.2
206        if ($old_version == "1.0.1") {
207               
208                // Add new user_lastactivity field
209                $exists = $h->db->column_exists('users', 'user_lastvisit');
210                if (!$exists) {
211                        // Alter the Users table to include user_lastvisit
212                        $sql = "ALTER TABLE " . TABLE_USERS . " ADD user_lastvisit TIMESTAMP NULL AFTER user_lastlogin";
213                        $h->db->query($h->db->prepare($sql));
214                }
215               
216                // Add site announcement record
217                $sql = "SELECT miscdata_id FROM " . TABLE_MISCDATA . " WHERE miscdata_key = %s";
218                $result = $h->db->get_var($h->db->prepare($sql, 'site_announcement'));
219                if (!$result) {
220                        // site announcement
221                        $sql = "INSERT INTO " . DB_PREFIX . $table_name . " (miscdata_key, miscdata_value, miscdata_default) VALUES (%s, %s, %s)";
222                        $h->db->query($h->db->prepare($sql, 'site_announcement', '', ''));
223                }
224               
225                // update "old version" for next set of upgrades
226                $old_version = "1.0.2";
227        }
228
229
230        // 1.0.2 to 1.0.3
231        if ($old_version == "1.0.2") {
232                // nothing to do...
233               
234                // update "old version" for next set of upgrades
235                $old_version = "1.0.3";
236        }
237
238
239        // 1.0.3 to 1.0.4
240        if ($old_version == "1.0.3") {
241               
242                // remove language pack option from settings
243                $sql = "DELETE FROM " . TABLE_SETTINGS . " WHERE settings_name = %s";
244                $h->db->query($h->db->prepare($sql, 'LANGUAGE_PACK'));
245               
246                // Drop temporary cvotes_temp table if it already exists
247                $sql = 'DROP TABLE IF EXISTS `' . DB_PREFIX . 'cvotes_temp`;';
248                $h->db->query($sql);
249               
250                // create a temp table to store old comment votes
251                $sql = "CREATE TABLE `" . DB_PREFIX . "cvotes_temp` (
252                        `cvote_archived` enum('Y','N') NOT NULL DEFAULT 'N',
253                        `cvote_updatedts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
254                        `cvote_post_id` int(11) NOT NULL DEFAULT '0',
255                        `cvote_comment_id` int(11) NOT NULL DEFAULT '0',
256                        `cvote_user_id` int(11) NOT NULL DEFAULT '0',
257                        `cvote_user_ip` varchar(32) NOT NULL DEFAULT '0',
258                        `cvote_date` timestamp NOT NULL,
259                        `cvote_rating` smallint(11) NOT NULL DEFAULT '0',
260                        `cvote_reason` tinyint(3) NOT NULL DEFAULT 0,
261                        `cvote_updateby` int(20) NOT NULL DEFAULT 0
262                ) ENGINE=" . DB_ENGINE . " DEFAULT CHARSET=" . DB_CHARSET . " COLLATE=" . DB_COLLATE . " COMMENT='Comment Votes';";
263                $h->db->query($sql);
264
265                // get old cvote data
266                $sql = "SELECT * FROM " . TABLE_COMMENTVOTES;
267                $old_cvotes = $h->db->get_results($h->db->prepare($sql));
268                if ($old_cvotes) {
269                        $columns    = "cvote_post_id, cvote_comment_id, cvote_user_id, cvote_user_ip, cvote_date, cvote_rating, cvote_reason, cvote_updateby";
270                        foreach ($old_cvotes as $cvote) {
271                                if ($cvote->cvote_rating == 'negative') { $rating = -10; } else { $rating = 10; }
272                                $sql = "INSERT INTO " . DB_PREFIX . "cvotes_temp (" . $columns . ") VALUES(%d, %d, %d, %s, %s, %d, %d, %d)";
273                                $h->db->query($h->db->prepare($sql, $cvote->cvote_post_id, $cvote->cvote_comment_id, $cvote->cvote_user_id, $cvote->cvote_user_ip, $cvote->cvote_date, $rating, $cvote->cvote_reason, $cvote->cvote_updateby));
274                        }
275                }
276               
277                // drop old commentvotes table
278                $h->db->query("DROP TABLE " . TABLE_COMMENTVOTES);
279               
280                // rename new table
281                $h->db->query("RENAME TABLE " . DB_PREFIX . "cvotes_temp TO " . DB_PREFIX . "commentvotes");
282               
283                // add new comment_votes_down column to comments table
284                $exists = $h->db->column_exists('comments', 'comment_votes_down');
285                if (!$exists) {
286                        // Alter the Users table to include user_lastvisit
287                        $sql = "ALTER TABLE " . TABLE_COMMENTS . " ADD comment_votes_down smallint(11) NOT NULL DEFAULT '0' AFTER comment_votes";
288                        $h->db->query($h->db->prepare($sql));
289                }
290               
291                // rename comment_votes column to comments_votes_up
292                $exists = $h->db->column_exists('comments', 'comment_votes_up');
293                if (!$exists) {
294                        // Alter the Users table to include user_lastvisit
295                        $sql = "ALTER TABLE " . TABLE_COMMENTS . " CHANGE comment_votes comment_votes_up smallint(11) NOT NULL DEFAULT '0'";
296                        $h->db->query($h->db->prepare($sql));
297                }
298               
299                // move any negative comment vote counts to the down column
300                $sql = "SELECT comment_id, comment_votes_up FROM " . TABLE_COMMENTS . " WHERE comment_votes_up < %d";
301                $negatives = $h->db->get_results($h->db->prepare($sql, 0));
302                if ($negatives) {
303                        foreach ($negatives as $neg) {
304                                $sql = "UPDATE " . TABLE_COMMENTS . " SET comment_votes_up = %d, comment_votes_down = %d WHERE comment_id = %d";
305                                $h->db->query($h->db->prepare($sql, 0, abs($neg->comment_votes_up), $neg->comment_id));
306                        }
307                }
308
309                // update "old version" for next set of upgrades
310                $old_version = "1.0.4";
311        }
312
313        // 1.0.4 to 1.0.5
314        if ($old_version == "1.0.4") {
315
316                // remove true/false "Notes" from admin settings
317                $sql = "UPDATE " . TABLE_SETTINGS . " SET settings_note = %s WHERE settings_note = %s";
318                $h->db->query($h->db->prepare($sql, '', 'true/false'));
319               
320                // update "old version" for next set of upgrades
321                $old_version = "1.0.5";
322        }
323
324        // 1.0.5 to 1.1
325        if ($old_version == "1.0.5") { $old_version = "1.1"; } // update "old version" for next set of upgrades
326       
327        // 1.1 to 1.1.1
328        if ($old_version == "1.1") { $old_version = "1.1.1"; } // update "old version" for next set of upgrades
329       
330        // 1.1 to 1.1.2
331        if ($old_version == "1.1.1") {
332
333                // SMTP on
334                $sql = "SELECT settings_name FROM " . TABLE_SETTINGS . " WHERE settings_name = %s";
335                $result = $h->db->get_var($h->db->prepare($sql, 'SMTP_ON'));
336                if(!$result) {
337                        $sql = "INSERT INTO " . TABLE_SETTINGS . " (settings_name, settings_value, settings_default, settings_note) VALUES (%s, %s, %s, %s)";
338                        $h->db->query($h->db->prepare($sql, 'SMTP_ON', 'false', 'false', 'Email auth'));
339                }
340
341                // SMTP host
342                $sql = "SELECT settings_name FROM " . TABLE_SETTINGS . " WHERE settings_name = %s";
343                $result = $h->db->get_var($h->db->prepare($sql, 'SMTP_HOST'));
344                if(!$result) {
345                        $sql = "INSERT INTO " . TABLE_SETTINGS . " (settings_name, settings_value, settings_default, settings_note) VALUES (%s, %s, %s, %s)";
346                        $h->db->query($h->db->prepare($sql, 'SMTP_HOST', 'mail.example.com', 'mail.example.com', ''));
347                }
348
349                // SMTP port
350                $sql = "SELECT settings_name FROM " . TABLE_SETTINGS . " WHERE settings_name = %s";
351                $result = $h->db->get_var($h->db->prepare($sql, 'SMTP_PORT'));
352                if(!$result) {
353                        $sql = "INSERT INTO " . TABLE_SETTINGS . " (settings_name, settings_value, settings_default, settings_note) VALUES (%s, %s, %s, %s)";
354                        $h->db->query($h->db->prepare($sql, 'SMTP_PORT', '25', '25', ''));
355                }
356
357                // SMTP username
358                $sql = "SELECT settings_name FROM " . TABLE_SETTINGS . " WHERE settings_name = %s";
359                $result = $h->db->get_var($h->db->prepare($sql, 'SMTP_USERNAME'));
360                if(!$result) {
361                        $sql = "INSERT INTO " . TABLE_SETTINGS . " (settings_name, settings_value, settings_default, settings_note) VALUES (%s, %s, %s, %s)";
362                        $h->db->query($h->db->prepare($sql, 'SMTP_USERNAME', '', '', ''));
363                }
364
365                // SMTP password
366                $sql = "SELECT settings_name FROM " . TABLE_SETTINGS . " WHERE settings_name = %s";
367                $result = $h->db->get_var($h->db->prepare($sql, 'SMTP_PASSWORD'));
368                if(!$result) {
369                        $sql = "INSERT INTO " . TABLE_SETTINGS . " (settings_name, settings_value, settings_default, settings_note) VALUES (%s, %s, %s, %s)";
370                        $h->db->query($h->db->prepare($sql, 'SMTP_PASSWORD', '', '', ''));
371                }
372
373                // update "old version" for next set of upgrades
374                $old_version = "1.1.2";
375        }
376
377         // 1.1.2 to 1.1.3
378        if ($old_version == "1.1.2") {
379
380                // System Feedback
381                $sql = "SELECT settings_name FROM " . TABLE_SETTINGS . " WHERE settings_name = %s";
382                $result = $h->db->get_var($h->db->prepare($sql, 'SYS_FEEDBACK'));
383                if(!$result) {
384                        $sql = "INSERT INTO " . TABLE_SETTINGS . " (settings_name, settings_value, settings_default, settings_note) VALUES (%s, %s, %s, %s)";
385                        $h->db->query($h->db->prepare($sql, 'SYS_FEEDBACK', 'true', 'true', 'Send system report'));
386                }
387
388                // Remove ON from constant names
389                $sql = "UPDATE " . TABLE_SETTINGS . " SET settings_name = %s WHERE settings_name = %s";
390                $h->db->query($h->db->prepare($sql, 'DB_CACHE', 'DB_CACHE_ON'));
391               
392                $sql = "UPDATE " . TABLE_SETTINGS . " SET settings_name = %s WHERE settings_name = %s";
393                $h->db->query($h->db->prepare($sql, 'RSS_CACHE', 'RSS_CACHE_ON'));
394               
395                $sql = "UPDATE " . TABLE_SETTINGS . " SET settings_name = %s WHERE settings_name = %s";
396                $h->db->query($h->db->prepare($sql, 'CSS_JS_CACHE', 'CSS_JS_CACHE_ON'));
397               
398                $sql = "UPDATE " . TABLE_SETTINGS . " SET settings_name = %s WHERE settings_name = %s";
399                $h->db->query($h->db->prepare($sql, 'HTML_CACHE', 'HTML_CACHE_ON'));
400               
401                $sql = "UPDATE " . TABLE_SETTINGS . " SET settings_name = %s WHERE settings_name = %s";
402                $h->db->query($h->db->prepare($sql, 'SMTP', 'SMTP_ON'));
403               
404                // update "old version" for next set of upgrades
405                $old_version = "1.1.3";
406        }
407       
408         // 1.1.3 to 1.1.4
409        if ($old_version == "1.1.3") {
410       
411        // copy post_date column to post_pub_date
412                $exists = $h->db->column_exists('posts', 'post_pub_date');
413                if (!$exists) {
414                        // Create a post_pub_date column
415                        $sql = "ALTER TABLE " . TABLE_POSTS . " ADD post_pub_date timestamp NOT NULL AFTER post_date";
416                        $h->db->query($h->db->prepare($sql));
417                       
418                        // Copy post_date to post_pub_date
419                        $sql = "UPDATE " . TABLE_POSTS . " SET post_pub_date = post_date";
420                        $h->db->query($h->db->prepare($sql));
421                }
422               
423                // update "old version" for next set of upgrades
424                $old_version = "1.1.4";
425        }
426
427         // 1.1.4 to 1.2
428        if ($old_version == "1.1.4") {
429
430                // check whether table exists first
431                $exists = $h->db->table_exists('friends');
432                if (!$exists) {
433                        // create a Friends table
434                        $sql = "CREATE TABLE `" . DB_PREFIX . "friends` (
435                                `follower_user_id` int(20) NOT NULL default '0',
436                                `following_user_id` int(20) NOT NULL default '0',
437                                `friends_updatedts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
438                                PRIMARY KEY (follower_user_id, following_user_id)
439                        ) ENGINE=" . DB_ENGINE . " DEFAULT CHARSET=" . DB_CHARSET . " COLLATE=" . DB_COLLATE . " COMMENT='Friends';";
440                        $h->db->query($sql);
441                }
442               
443                // check whether table exists first
444                $exists = $h->db->table_exists('messaging');
445                if (!$exists) {
446                        // create a Messaging table
447                        $sql = "CREATE TABLE `" . DB_PREFIX . "messaging` (
448                                `message_id` int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
449                                `message_updatedts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
450                                `message_archived` enum('Y','N') NOT NULL DEFAULT 'N',
451                                `message_from` int(20) NOT NULL DEFAULT 0,
452                                `message_to` int(20) NOT NULL DEFAULT 0,
453                                `message_date` timestamp NOT NULL,
454                                `message_subject` varchar(255) NOT NULL DEFAULT '',
455                                `message_content` text NULL,
456                                `message_read` tinyint(1) NOT NULL DEFAULT '0',
457                                `message_inbox` tinyint(1) NOT NULL DEFAULT '1',
458                                `message_outbox` tinyint(1) NOT NULL DEFAULT '1',
459                                `message_updateby` int(20) NOT NULL DEFAULT 0,
460                                INDEX  (`message_archived`)
461                        ) ENGINE=" . DB_ENGINE . " DEFAULT CHARSET=" . DB_CHARSET . " COLLATE=" . DB_COLLATE . " COMMENT='Messaging';";
462                        $h->db->query($sql);
463                }
464
465                //Add indices to tables
466                $tables = array( 'comments' => array('comment_archived', 'comment_status'),
467                                 'posts' => array('post_archived', 'post_status', 'post_type'),
468                                 'tags' => array('tags_archived'),
469                                 'useractivity' => array('useract_userid'),
470                                 'messaging' => array('message_archived'),
471                                 'usermeta' => array('usermeta_key')
472                        );
473
474                foreach ($tables as $table => $indices) {
475                        if ($exists = $h->db->table_exists($table))
476                        {
477                                foreach ($indices as $index) {
478                                        $sql = "SHOW INDEX FROM `" . DB_PREFIX . $table . "` WHERE KEY_NAME = '" . $index . "'";                           
479                                        $result = $h->db->query($sql);                     
480                                        if (!$result) {
481                                                $sql = "ALTER TABLE `" . DB_PREFIX . $table . "` ADD INDEX (" . $index . ")";                           
482                                                $h->db->query($sql);
483                                        }
484                                }
485                        }
486                }               
487
488                // update "old version" for next set of upgrades
489                $old_version = "1.2.0";
490        }
491
492         // 1.2.0 to 1.3.0
493        if ($old_version == "1.2.0") {         
494               
495                $exists = $h->db->column_exists('plugins', 'plugin_latestversion');
496                if (!$exists) {
497                        // Create a plugin_version column
498                        $sql = "ALTER TABLE " . TABLE_PLUGINS . " ADD plugin_latestversion varchar(8) NOT NULL DEFAULT '0.0'";
499                        $h->db->query($h->db->prepare($sql));
500                }
501               
502                $exists = $h->db->column_exists('settings', 'settings_show');
503                if (!$exists) {
504                        // Create a plugin_version column
505                        $sql = "ALTER TABLE " . TABLE_SETTINGS . " ADD settings_show enum(%s, %s) NOT NULL DEFAULT %s AFTER settings_note";
506                        $h->db->query($h->db->prepare($sql, 'Y', 'N', 'Y'));
507                }
508               
509                // Hide Database duration. It's still technically used, but smartCache overrides the duration
510                $sql = "UPDATE " . TABLE_SETTINGS . " SET settings_show = %s WHERE settings_name = %s";
511                $h->db->query($h->db->prepare($sql, 'N', 'DB_CACHE_DURATION'));
512               
513                // Hide RSS duration. It's still technically used, but only by Admin "News" so not worth showing
514                $sql = "UPDATE " . TABLE_SETTINGS . " SET settings_show = %s WHERE settings_name = %s";
515                $h->db->query($h->db->prepare($sql, 'N', 'RSS_CACHE_DURATION'));
516               
517                // insert new lang_cache option in Admin Settings
518                $sql = "SELECT settings_id FROM " . TABLE_SETTINGS . " WHERE settings_name = %s";
519                $result = $h->db->get_var($h->db->prepare($sql, 'LANG_CACHE'));
520                if (!$result) {
521                        $sql = "INSERT INTO " . TABLE_SETTINGS . " (settings_name, settings_value, settings_default, settings_show) VALUES (%s, %s, %s, %s)";
522                        $h->db->query($h->db->prepare($sql, 'LANG_CACHE', 'true', 'true', 'Y'));
523                }
524
525                // Add index to comment_post_id to speed up countComments function
526                if ($exists = $h->db->table_exists('comments')) {
527                        $sql = "SHOW INDEX FROM `" . DB_PREFIX . "comments` WHERE KEY_NAME = %s";
528                        $result = $h->db->query($h->db->prepare($sql, 'comment_post_id'));
529                        if (!$result) {
530                                $sql = "ALTER TABLE `" . DB_PREFIX . "comments` ADD INDEX (comment_post_id)";
531                                $h->db->query($sql);
532                        }
533                }
534
535                // SITE TABLE - for multiple sites
536                if (!$exists = $h->db->table_exists('site')) {
537                        $sql = "CREATE TABLE `" . DB_PREFIX . "site` (
538                                `site_id` int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
539                                `site_adminuser_id` varchar(64) NULL,
540                                `site_url` varchar(128) NOT NULL DEFAULT '',
541                                `site_updatedts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
542                                `site_updateby` int(20) NOT NULL DEFAULT 0
543                        ) ENGINE=" . DB_ENGINE . " DEFAULT CHARSET=" . DB_CHARSET . " COLLATE=" . DB_COLLATE . " COMMENT='Site Table';";
544                        $h->db->query($sql);
545                }
546
547                //Add site_id column and index to each table
548                $tables = array(
549                        'blocked'=>'blocked','categories'=>'category',
550                        'comments'=>'comment', 'plugins'=>'plugin',
551                        'miscdata'=>'miscdata','pluginsettings'=>'pluginsetting',
552                        'posts'=>'post','settings'=>'settings',
553                        'tags'=>'tag', 'users'=>'user', 'widgets'=>'widget',
554                );
555
556                foreach ($tables as $table => $column) {
557
558                        if (!$exists = $h->db->column_exists($table, $column . '_siteid')) {
559                                // Create a column for index first
560                                $sql = "ALTER TABLE " . DB_PREFIX . $table . " ADD " . $column . "_siteid INT NOT NULL DEFAULT 1";
561                                $h->db->query($sql);
562                        } else {
563                                // fix to make sure siteid=0 columns are changed to siteid=1
564                                $sql = "ALTER TABLE " . DB_PREFIX . $table . " MODIFY " . $column . "_siteid INT NOT NULL DEFAULT 1";
565                                $h->db->query($sql);
566
567                                $sql = "UPDATE " . DB_PREFIX . $table . " SET " . $column . "_siteid=1 WHERE " . $column . "_siteid=0";
568                                $h->db->query($sql);
569                        }
570
571                       
572                        $sql = "SHOW INDEX FROM `" . DB_PREFIX . $table . "` WHERE KEY_NAME = '" . $column . "_siteid'";
573                        $result = $h->db->query($sql);
574                        if (!$result) {
575                                $sql = "ALTER TABLE `" . DB_PREFIX . $table . "` ADD INDEX (" . $column . "_siteid)";
576                                $h->db->query($sql);
577                        }
578
579                        // Unique Keys
580                        /*
581                         *
582                         */
583                        if ($exists = $h->db->column_exists('categories', 'category_siteid')) {
584                            $sql = "ALTER TABLE `" . TABLE_CATEGORIES . "` DROP KEY `key`";
585                            $h->db->query($sql);
586                            $sql = "ALTER TABLE `" . TABLE_CATEGORIES . "` ADD UNIQUE KEY `key` (`category_name`, `category_siteid`)";
587                            $h->db->query($sql);
588                        }
589                        if ($exists = $h->db->column_exists('plugins', 'plugin_siteid')) {
590                            $sql = "ALTER TABLE `" . TABLE_PLUGINS . "` DROP KEY `key`";
591                            $h->db->query($sql);
592                            $sql = "ALTER TABLE `" . TABLE_PLUGINS . "` ADD UNIQUE KEY `key` (`plugin_folder`, `plugin_siteid`)";
593                            $h->db->query($sql);
594                        }
595                        if ($exists = $h->db->column_exists('settings', 'settings_siteid')) {
596                            $sql = "ALTER TABLE `" . TABLE_SETTINGS . "` DROP KEY `key`";
597                            $h->db->query($sql);
598                            $sql = "ALTER TABLE `" . TABLE_SETTINGS . "` ADD UNIQUE KEY `key` (`settings_name`, `settings_siteid`)";
599                            $h->db->query($sql);
600                        }
601                        if ($exists = $h->db->column_exists('tags', 'tags_siteid')) {
602                            $sql = "ALTER TABLE `" . TABLE_TAGS . "` DROP KEY `key`";
603                            $h->db->query($sql);
604                            $sql = "ALTER TABLE `" . TABLE_TAGS . "` ADD UNIQUE KEY `key` (`tags_post_id`, `tags_word`, `tags_siteid`)";
605                            $h->db->query($sql);
606                        }
607                        if ($exists = $h->db->column_exists('users', 'user_siteid')) {
608                            $sql = "ALTER TABLE `" . TABLE_USERS . "` DROP KEY `key`";
609                            $h->db->query($sql);
610                            $sql = "ALTER TABLE `" . TABLE_USERS . "` ADD UNIQUE KEY `key` (`user_username`, `user_siteid`)";
611                            $h->db->query($sql);
612                        }
613                }
614               
615               
616                // reorder the admin settings
617                $desired_order = array('SITE_OPEN', 'SITE_NAME', 'THEME', 'ADMIN_THEME', 'DEBUG', 'FRIENDLY_URLS', 'DB_CACHE', 'DB_CACHE_DURATION', 'CSS_JS_CACHE', 'HTML_CACHE', 'LANG_CACHE', 'RSS_CACHE', 'RSS_CACHE_DURATION', 'SITE_EMAIL', 'SMTP', 'SMTP_HOST', 'SMTP_PORT', 'SMTP_USERNAME', 'SMTP_PASSWORD', 'SYS_FEEDBACK');
618                $sql = "SELECT * FROM " . TABLE_SETTINGS;
619                $results = $h->db->get_results($sql);
620                $h->db->query("TRUNCATE TABLE " . TABLE_SETTINGS);
621
622                if ($results) {
623                        $i = 0;
624                        while (!empty($desired_order)) {
625                                foreach ($results as $row) {
626                                        if (count($desired_order) == 0) { break; }
627                                        if ($row->settings_name == $desired_order[0]) {
628                                                $sql = "INSERT INTO " . TABLE_SETTINGS . " (settings_name, settings_value, settings_default, settings_note, settings_show, settings_siteid) VALUES(%s, %s, %s, %s, %s, %d)";
629                                                $h->db->query($h->db->prepare($sql, $row->settings_name, $row->settings_value, $row->settings_default, $row->settings_note, $row->settings_show, $row->settings_siteid));
630                                                array_shift($desired_order);
631                                        }
632                                        $i++;
633                                        if ($i > 10000) { break; } // got stuck in a loop
634                                }
635                        }
636                }
637
638                // update "old version" for next set of upgrades
639                $old_version = "1.3.0";
640        }
641
642         // 1.3.0 to 1.4.0
643        if ($old_version == "1.3.0") {
644
645            // Version Info Auto Update
646            $sql = "UPDATE  " . TABLE_SETTINGS . " SET settings_note = %s, settings_name = %s WHERE settings_name = %s";
647            $h->db->query($h->db->prepare($sql, 'Hotaru updates', 'SYS_UPDATES', 'SYS_FEEDBACK'));
648
649            // RELATES TABLE
650            if (!$exists = $h->db->table_exists('relates')) {
651                //echo "table doesn't exist. Stopping before creation."; exit;
652                $sql = "CREATE TABLE `" . DB_PREFIX .  "relates` (
653                        `relates_id` int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
654                        `relates_user_id` int(20) NOT NULL default '0',
655                        `relates_post_id` int(20) NOT NULL default '0',
656                        `relates_type` varchar(64) default '',
657                        `relates_updatedts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
658                        INDEX  (`relates_user_id`),
659                        INDEX  (`relates_post_id`),
660                        INDEX  (`relates_type`)
661                ) ENGINE=" . DB_ENGINE . " DEFAULT CHARSET=" . DB_CHARSET . " COLLATE=" . DB_COLLATE . " COMMENT='Relates';";           
662                $h->db->query($sql);
663            }
664
665            // Version Info Auto Update
666            $sql = "UPDATE  " . TABLE_SETTINGS . " SET settings_note = %s, settings_name = %s WHERE settings_name = %s";
667            $h->db->query($h->db->prepare($sql, 'Hotaru updates', 'SYS_UPDATES', 'SYS_FEEDBACK'));
668
669            // MULTI_SITE for SETTINGS table for siteid=1 only (admin)
670            $sql = "SELECT settings_siteid FROM " . TABLE_SETTINGS . " WHERE settings_name = %s AND settings_siteid = %d";
671            $result = $h->db->get_var($h->db->prepare($sql, 'MULTI_SITE', 1));
672
673            if (!$result) {
674                $sql = "INSERT INTO " . TABLE_SETTINGS . " (settings_name, settings_value, settings_default, settings_note) VALUES (%s, %s, %s, %s)";
675                $h->db->query($h->db->prepare($sql, 'MULTI_SITE', 'false', 'false', 'Multiple sites'));
676            }
677
678            // Add siteid to pluginhooks table
679            if (!$exists = $h->db->column_exists('pluginhooks', 'pluginhooks_siteid')) {
680                    // Create a column for index first
681                    $sql = "ALTER TABLE " . TABLE_PLUGINHOOKS . " ADD pluginhooks_siteid INT NOT NULL DEFAULT 1";
682                    $h->db->query($sql);
683            }
684
685            // Add index for siteid on pluginhooks table
686            $sql = "SHOW INDEX FROM `" . TABLE_PLUGINHOOKS . "` WHERE KEY_NAME = 'pluginhooks_siteid'";
687            $result = $h->db->query($sql);
688            if (!$result) {
689                    $sql = "ALTER TABLE `" . TABLE_PLUGINHOOKS . "` ADD INDEX (pluginhooks_siteid)";
690                    $h->db->query($sql);
691            }
692
693            // Change post_title column from `post_title` varchar(255) NULL, to `post_title` text NULL,
694            $exists = $h->db->column_exists('posts', 'post_title');
695            if (!$exists) {
696                    $sql = "ALTER TABLE " . TABLE_POSTS . " MODIFY post_title text NUL";
697                    $h->db->query($h->db->prepare($sql));
698            }
699
700        }
701
702
703        // Update Hotaru version number to the database (referred to when upgrading)
704        $sql = "UPDATE " . TABLE_MISCDATA . " SET miscdata_key = %s, miscdata_value = %s, miscdata_default = %s WHERE miscdata_key = %s";
705        $h->db->query($h->db->prepare($sql, 'hotaru_version', $h->version, $h->version, 'hotaru_version'));
706}
707
708
709
710
711//      // loop through all sites to insert this setting into each one's SETTING TABLE
712//      $sql = "SELECT site_id FROM " . TABLE_SITE;
713//      $sites = $h->db->get_results($h->db->prepare($sql));
714//
715//      $sql = "SELECT settings_siteid FROM " . TABLE_SETTINGS . " WHERE settings_name = %s";
716//      $result = $h->db->get_results($h->db->prepare($sql, 'MULTI_SITE'), ARRAY_N);
717//      if (!$result) { $result = array(); }
718//      //var_dump($result);
719//      if ($sites) {
720//          foreach ($sites as $site) {
721//              if (in_array($site->site_id, $result)) {
722//                  $sql = "UPDATE  " . TABLE_SETTINGS . " SET settings_value = %s WHERE settings_name = %s";
723//                  $h->db->query($h->db->prepare($sql, '', ''));
724//              } else {
725//                  $sql = "INSERT INTO " . TABLE_SETTINGS . " (settings_name, settings_value, settings_default, settings_note) VALUES (%s, %s, %s, %s)";
726//                  $h->db->query($h->db->prepare($sql, '', '', '', ''));
727//              }
728//          }
729//      }
730
731?>
Note: See TracBrowser for help on using the repository browser.