root/branches/plogger-with-tags/_install.php

Revision 402, 18.2 KB (checked in by stefan, 3 years ago)

merging in changes from trunk that have been made since the branch was created.

Line 
1<?php
2error_reporting(E_ERROR);
3$TABLE_PREFIX = "plogger_";
4$config_table = $TABLE_PREFIX.'config';
5
6if (file_exists("plog-config.php")) {
7    require_once("plog-config.php");
8    if (    @mysql_connect(PLOGGER_DB_HOST,PLOGGER_DB_USER,PLOGGER_DB_PW) &&
9        @mysql_select_db(PLOGGER_DB_NAME)) {
10        die("Plogger has already been installed!");
11    };
12};
13
14
15
16require_once(dirname(__FILE__) . '/lib/plogger/install_functions.php');
17$errors = check_requirements();
18
19if (sizeof($errors) > 0) {
20    print '<ul>';
21    foreach($errors as $error) {
22        print '<li>' . $error . '</li>';
23    };
24    print '</ul>';
25    exit;
26
27};
28
29foreach($_POST as $key => $val) $_POST[$key] = stripslashes($val);
30
31$config_keys = array("db_user","db_pass","db_name","gallery_name","username","password");
32$config_vars = array();
33foreach($config_keys as $config_key) {
34    $config_val = !empty($_POST[$config_key]) ? $_POST[$config_key] : "";
35    $config_vars[$config_key] = $config_val;
36};
37
38
39if (!empty($_POST))
40{
41    $cfg_file = "<?php\n";
42    $cfg_file .= '// this is the file used to connect to your database.'."\n";
43    $cfg_file .= '// you must change these values in order to run the gallery.'."\n";
44
45    $cfg_file .= 'define("PLOGGER_DB_HOST","'.$_POST["db_host"].'");'."\n";
46    $cfg_file .= 'define("PLOGGER_DB_USER","'.$_POST["db_user"].'");'."\n";
47    $cfg_file .= 'define("PLOGGER_DB_PW","'.$_POST["db_pass"].'");'."\n";
48    $cfg_file .= 'define("PLOGGER_DB_NAME","'.$_POST["db_name"].'");'."\n";
49
50    $cfg_file .= "?>\n";
51
52};
53
54$errors = $config_errors = array();
55$output = '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
56
57if (isset($_POST["action"]) && $_POST["action"] == "install"){
58    // First, check for missing data.
59    if (strlen(trim($_POST["db_host"])) == 0){
60        $errors[] = 'Please enter the name of your MySQL host.';
61    }
62    if (strlen(trim($_POST["db_user"])) == 0){
63        $errors[] = 'Please enter the MySQL username.';
64    }
65    if (strlen(trim($_POST["db_name"])) == 0){
66        $errors[] = 'Please enter the MySQL database name.';
67    }
68    if (strlen(trim($_POST["db_pass"])) == 0){
69        $errors[] = 'Please enter the MySQL password.';
70    }
71    if (strlen(trim($_POST["gallery_name"])) == 0){
72        $errors[] = 'Please enter a gallery name.';
73    }
74    if (strlen(trim($_POST["username"])) == 0){
75        $errors[] = 'Please enter a username.';
76    }
77    if (strlen(trim($_POST["password"])) == 0){
78        $errors[] = 'Please enter a password.';
79    }
80    if (trim($_POST["password"]) != trim($_POST["confirm_password"])){
81        $errors[] = 'Passwords do not match.';
82    }
83   
84    $files_to_read = array("./","./admin","./css","./images","./lib","./thumbs","./uploads");
85   
86    foreach($files_to_read as $file){
87        if (!is_readable(realpath($file))){
88            $errors[] = "The path ".realpath($file)." (".$file.") is not readable.";
89        }
90    }
91   
92    $files_to_write = array("./","./thumbs","./images", "./uploads");
93   
94    foreach($files_to_write as $file){
95        if (!is_writable(realpath($file))){
96            $errors[] = 'The path '.realpath($file).' is not writable by the Web server.';
97        }
98    }
99   
100    // Check for the correct database information.   
101    if (count($errors) == 0){
102        if(!@mysql_connect($_POST["db_host"],$_POST["db_user"],$_POST["db_pass"])){
103            $errors[] = 'Plogger could not connect to the MySQL host with the information you provided. (' . mysql_error() . ')';
104        }
105        elseif(!mysql_select_db($_POST["db_name"])){
106            $errors[] = 'Plogger connected to the MySQL host, but could not find the database '.$_POST["db_name"].'.';
107        }
108    }
109   
110    // Try to write the plog-config.php file.
111    if (count($errors) == 0){
112        // Open the file for writing.
113
114        $handle = fopen(realpath("./plog-config.php"),"w");
115       
116        if ($handle){
117            fwrite($handle, $cfg_file);
118            fclose($handle);
119        }
120        else{
121            // Try chmodding the file.
122            @chmod(realpath("./plog-config.php"), 0666);
123           
124            $handle = fopen(realpath("./plog-config.php"),"w");
125           
126            if ($handle){
127                fwrite($handle, $cfg_file);
128                fclose($handle);
129            }
130            else{
131                $config_errors[] = "Plogger could not write the plog-config.php file.  Either make this file writable by the Web server and click 'Try Again', or replace the contents of the current plog-config.php file with the following code:<br /><pre>" . htmlentities($cfg_file) . "</pre>If you choose to overwrite the file manually, do so, and don't forget to change the permissions back after you overwrite it.  Proceed to your <a href=\"admin\">Administrative Tools</a> when you have fixed the connection file.";
132            }
133        }
134
135        // since 4.1 MySQL has support for specifying character encoding for tables
136        // and I really want to  use it if avaiable. So we need figure out what version
137        // we are running on and to the right hting
138        $mysql_version = mysql_get_server_info();
139        $mysql_charset_support = "4.1";
140        $default_charset = "";
141
142        if (1 == version_compare($mysql_version,$mysql_charset_support))
143        {
144            $default_charset = "DEFAULT CHARACTER SET UTF8";
145        };
146
147        // first lets check if the tables have already been installed
148       
149        $query = "CREATE TABLE `".$TABLE_PREFIX."collections` (
150          `name` varchar(128) NOT NULL default '',
151          `description` varchar(255) NOT NULL default '',
152          `path` varchar(255) NOT NULL default '',
153          `id` int(11) NOT NULL auto_increment,
154          `thumbnail_id` int(11) NOT NULL DEFAULT '0',
155            PRIMARY KEY  (id)
156        ) Type=MyISAM $default_charset";
157        mysql_query($query) or die(mysql_error().'<br /><br />'. $query);
158       
159        $query = "CREATE TABLE `".$TABLE_PREFIX."albums` (
160          `name` varchar(128) NOT NULL default '',
161          `id` int(11) NOT NULL auto_increment,
162          `description` varchar(255) NOT NULL default '',
163          `path` varchar(255) NOT NULL default '',
164          `parent_id` int(11) NOT NULL default '0',
165          `thumbnail_id` int(11) NOT NULL default '0',
166          PRIMARY KEY  (id),
167            INDEX pid_idx (parent_id)
168            ) Type=MyISAM $default_charset";
169        mysql_query($query) or die(mysql_error().'<br /><br />'. $query);
170
171        $query = "CREATE TABLE `".$TABLE_PREFIX."config` (
172            `max_thumbnail_size` int(11) NOT NULL default '0',
173            `max_display_size` int(11) NOT NULL default '0',
174            `thumb_num` int(11) NOT NULL default '0',
175            `admin_username` varchar(64) NOT NULL default '',
176            `admin_password` varchar(64) NOT NULL default '',
177            `admin_email` varchar(50) NOT NULL default '',
178            `date_format` varchar(64) NOT NULL default '',
179            `compression` int(11) NOT NULL default '75',
180            `default_sortby` varchar(20) NOT NULL default '',
181            `default_sortdir` varchar(5) NOT NULL default '',
182            `album_sortby` varchar(20) NOT NULL default '',
183            `album_sortdir` varchar(5) NOT NULL default '',
184            `collection_sortby` varchar(20) NOT NULL default '',
185            `collection_sortdir` varchar(5) NOT NULL default '',
186            `gallery_name` varchar(255) NOT NULL default '',
187            `allow_dl` smallint(1) NOT NULL default '0',
188            `allow_comments` smallint(1) NOT NULL default '1',
189            `allow_print` smallint(1) NOT NULL default '1',
190            `truncate` int(11) NOT NULL default '12',
191            `square_thumbs` tinyint default 1,
192            `feed_num_entries` int(15) NOT NULL default '15',
193            `rss_thumbsize` int(11) NOT NULL default '400',
194            `feed_title` text NOT NULL,
195            `use_mod_rewrite` tinyint NOT NULL default '0',
196            `gallery_url` varchar(255) NOT NULL default '',
197            `comments_notify` tinyint NOT NULL default '1',
198            `comments_moderate` tinyint NOT NULL default '0',
199            `feed_language` varchar(255) NOT NULL default 'en-us',
200            `theme_dir` varchar(128) NOT NULL default '',
201            `thumb_nav_range` int(11) NOT NULL default '0',
202            `enable_thumb_nav` tinyint default '0',
203            `allow_fullpic` tinyint default '1'
204        ) Type=MyISAM $default_charset";
205        mysql_query($query) or die(mysql_error().'<br /><br />'. $query);
206        $query = "CREATE TABLE `".$TABLE_PREFIX."pictures` (
207          `path` varchar(255) NOT NULL default '',
208          `parent_album` int(11) NOT NULL default '0',
209          `parent_collection` int(11) NOT NULL default '0',
210          `caption` mediumtext NOT NULL,
211          `description` text NOT NULL,
212          `id` int(11) NOT NULL auto_increment,
213          `date_modified` timestamp(14) NOT NULL,
214          `date_submitted` timestamp(14) NOT NULL,
215          `EXIF_date_taken` varchar(64) NOT NULL default '',
216          `EXIF_camera` varchar(64) NOT NULL default '',
217          `EXIF_shutterspeed` varchar(64) NOT NULL default '',
218          `EXIF_focallength` varchar(64) NOT NULL default '',
219          `EXIF_flash` varchar(64) NOT NULL default '',
220          `EXIF_aperture` varchar(64) NOT NULL default '',
221          `allow_comments` int(11) NOT NULL default '1',
222          PRIMARY KEY  (id),
223            INDEX pa_idx (parent_album),
224            INDEX pc_idx (parent_collection)
225        ) Type=MyISAM $default_charset";
226        mysql_query($query) or die(mysql_error().'<br /><br />'. $query);
227       
228                $query = "CREATE TABLE `".$TABLE_PREFIX."comments` (
229          `id` int(11) NOT NULL auto_increment,
230            `parent_id` int(11) NOT NULL default '0',
231          `author` varchar(64) NOT NULL default '',
232          `email` varchar(64) NOT NULL default '',
233          `url` varchar(64) NOT NULL default '',
234          `date` datetime NOT NULL,
235          `comment` longtext NOT NULL,
236          `ip` char(64),
237          `approved` tinyint default '1',
238            PRIMARY KEY  (id),
239            INDEX pid_idx (parent_id),
240            INDEX approved_idx (approved)
241        ) Type=MyISAM $default_charset";
242        mysql_query($query) or die(mysql_error().'<br /><br />'. $query);
243       
244                $query = "CREATE TABLE `".$TABLE_PREFIX."tag2picture` (
245          `tag_id` bigint(20) unsigned NOT NULL default '0',
246          `picture_id` bigint(20) unsigned NOT NULL default '0',
247          `tagdate` datetime default NULL,
248          KEY `tag_id` (`tag_id`),
249          KEY `picture_id` (`picture_id`)
250        ) Type=MyISAM $default_charset";
251        mysql_query($query) or die(mysql_error().'<br /><br />'. $query);
252
253                $query = "CREATE TABLE `".$TABLE_PREFIX."tags` (
254          `id` bigint(20) unsigned NOT NULL auto_increment,
255          `tag` char(50) NOT NULL default '',
256          `tagdate` datetime NOT NULL default '0000-00-00 00:00:00',
257          `urlified` char(50) NOT NULL default '',
258          PRIMARY KEY  (`id`),
259          UNIQUE `tag` (`tag`),
260          UNIQUE `urlified` (`urlified`)
261        ) Type=MyISAM $default_charset";
262        mysql_query($query) or die(mysql_error().'<br /><br />'. $query);
263
264        $default_theme_dir = dirname(__FILE__)."/". 'themes/default/';
265       
266        $query = "INSERT INTO `".$TABLE_PREFIX."config`
267            (`theme_dir`,
268            `compression`,
269            `max_thumbnail_size`,
270              `max_display_size`,
271              `thumb_num`,
272              `admin_username`,
273              `admin_password`,
274              `date_format`,
275            `feed_title`,
276           `gallery_name`)
277           VALUES
278           ('".mysql_escape_string($default_theme_dir)."',
279            75,
280            100,
281            500,
282            20,
283            '".mysql_escape_string($_POST["username"])."',
284            MD5('".mysql_escape_string($_POST["password"])."'),
285            'n.j.Y',
286            'Plogger Photo Feed',
287            '".mysql_escape_string($_POST["gallery_name"])."')";
288        mysql_query($query) or die(mysql_error().'<br /><br />'. $query);
289       
290        maybe_add_table($TABLE_PREFIX."thumbnail_config","
291            `id` int(10) unsigned NOT NULL auto_increment,
292            `update_timestamp` int(10) unsigned default NULL,
293            `max_size` int(10) unsigned default NULL,
294            `disabled` tinyint default 0,
295            PRIMARY KEY  (`id`)
296        ");
297       
298        // use a random timestamp from the past to keep the existing thumbnails
299        $tambov_constant = 1096396500;
300       
301        if (!isset($config['max_thumbnail_size'])) {
302            $config['max_thumbnail_size'] = 100;
303        };
304       
305        $sql = "INSERT INTO `".$TABLE_PREFIX."thumbnail_config` (id,update_timestamp,max_size)
306            VALUES('".THUMB_SMALL."','".$tambov_constant."','".$config['max_thumbnail_size']."')";
307        mysql_query($sql);
308       
309        if (!isset($config['max_display_size'])) {
310            $config['max_display_size'] = 500;
311        };
312        $sql = "INSERT INTO `".$TABLE_PREFIX."thumbnail_config` (id,update_timestamp,max_size)
313            VALUES('".THUMB_LARGE."','".$tambov_constant."','".$config['max_display_size']."')";
314        mysql_query($sql);
315       
316        if (!isset($config['rss_thumbsize'])) {
317            $config['rss_thumbsize'] = 400;
318        };
319       
320        $sql = "INSERT INTO `".$TABLE_PREFIX."thumbnail_config` (id,update_timestamp,max_size)
321            VALUES('".THUMB_RSS."','".$tambov_constant."','".$config['rss_thumbsize']."')";
322        mysql_query($sql);
323       
324        if (!isset($config['nav_thumbsize'])) {
325            $config['nav_thumbsize'] = 60;
326        };
327
328        $sql = "INSERT INTO `".$TABLE_PREFIX."thumbnail_config` (id,update_timestamp,max_size)
329            VALUES('".THUMB_NAV."','".$tambov_constant."','".$config['nav_thumbsize']."')";
330        mysql_query($sql);
331       
332        $config["baseurl"] = "http://".$_SERVER["SERVER_NAME"]. substr($_SERVER["PHP_SELF"],0,strrpos($_SERVER["PHP_SELF"],"/")) . "/";
333       
334        $sql = "UPDATE $config_table SET gallery_url = '" . $config["baseurl"] . "'";
335        mysql_query($sql);
336       
337        if (count($config_errors) == 0){
338            header("Location: ./admin/index.php");
339            exit;
340        }
341        else{
342            $set_config_error = true;
343           
344            // Display the config error information
345           
346            $output .= '
347                <input type="hidden" name="action" value="try_again" />
348                <input type="hidden" name="db_host" value="'.$_POST["db_host"].'" />
349                <input type="hidden" name="db_user" value="'.$_POST["db_user"].'" />
350                <input type="hidden" name="db_pass" value="'.$_POST["db_pass"].'" />
351                <input type="hidden" name="db_name" value="'.$_POST["db_name"].'" />
352                <input type="hidden" name="username" value="'.$_POST["username"].'" />
353                <input type="hidden" name="password" value="'.$_POST["password"].'" />
354                <input type="hidden" name="gallery_name" value="'.$_POST["gallery_name"].'" />
355                <p>The following errors occurred:</p><ul class="error">';
356           
357            foreach($config_errors as $error){
358                $output .= '<li>'.$error.'</li>';
359            }
360           
361            $output .= '</ul>
362                        <table>
363                            <tr>
364                                <td class="submitButtonRow" colspan="2"><input type="submit" name="submit" value="Try Again" /></td>
365                            </tr>
366                        </table>';
367        }
368    }
369}
370
371if(isset($_POST["action"]) && $_POST["action"] == "try_again"){
372    // The user has done the database setup, but the plog-config.php file has not been written.
373    $handle = @fopen(realpath("./plog-config.php"),"w");
374    if ($handle){
375        fwrite($handle, $cfg_file);
376        fclose($handle);
377    }
378    else{
379        $config_errors[] = "Plogger could not write the plog-config.php file.  Either make this file writable by the Web server and click 'Try Again', or replace the contents of the current plog-config.php file with the following code:<br /><pre>" . htmlentities($cfg_file) . "</pre>If you choose to overwrite the file manually, do so, and don't forget to change the permissions back after you overwrite it.  Proceed to your <a href=\"admin\">Administrative Tools</a> when you have fixed the connection file.";
380    }
381   
382    if (count($config_errors) == 0){
383        header("Location: ./admin/index.php");
384        exit;
385    }
386    else{
387        $output .= '
388                <input type="hidden" name="action" value="try_again" />
389                <input type="hidden" name="db_host" value="'.stripslashes($_POST["db_host"]).'" />
390                <input type="hidden" name="db_user" value="'.stripslashes($_POST["db_user"]).'" />
391                <input type="hidden" name="db_pass" value="'.stripslashes($_POST["db_pass"]).'" />
392                <input type="hidden" name="db_name" value="'.stripslashes($_POST["db_name"]).'" />
393                <input type="hidden" name="username" value="'.stripslashes($_POST["username"]).'" />
394                <input type="hidden" name="password" value="'.stripslashes($_POST["password"]).'" />
395                <input type="hidden" name="gallery_name" value="'.stripslashes($_POST["gallery_name"]).'" />
396                <p>The following errors occurred:</p><ul class="error">';
397       
398        foreach($config_errors as $error){
399            $output .= '<li>'.$error.'</li>';
400        }
401       
402        $output .= '</ul>
403                    <table>
404                        <tr>
405                            <td class="submitButtonRow" colspan="2"><input type="submit" name="submit" value="Try Again" /></td>
406                        </tr>
407                    </table>';
408    }
409}
410elseif(empty($set_config_error)){
411    $db_host = (isset($_POST["action"])) ? stripslashes($_POST["db_host"]) : 'localhost';
412       
413    if (count($errors) > 0){
414        $output .= '<p>The following errors occurred:</p><ul class="error">';
415       
416        foreach($errors as $error){
417            $output .= '<li>'.$error.'</li>';
418        }
419       
420        $output .= '</ul>';
421    }
422   
423    $output .= '    <input type="hidden" name="action" value="install" />
424        <table>
425            <tr>
426                <td colspan="2">
427                    <div id="navcontainer">
428                        <h1>Database Setup</h1>
429                       
430                    </div>
431                </td>
432            </tr>
433            <tr>
434                <td class="form_label"><label for="db_host">MySQL host:</label></td>
435                <td class="form_input"><input type="text" name="db_host" id="db_host" value="'.$db_host.'" /></td>
436            </tr>
437            <tr>
438                <td class="form_label"><label for="db_user">MySQL Username:</label></td>
439                <td class="form_input"><input type="text" name="db_user" id="db_user" value="'.$config_vars["db_user"].'" /></td>
440            </tr>
441            <tr>
442                <td class="form_label"><label for="db_password">MySQL Password:</label></td>
443                <td class="form_input"><input type="password" name="db_pass" id="db_pass" value="'.$config_vars["db_pass"].'" /></td>
444            </tr>
445            <tr>
446                <td class="form_label"><label for="db_name">MySQL Database:</label></td>
447                <td class="form_input"><input type="text" name="db_name" id="db_name" value="'.$config_vars["db_name"].'" /></td>
448            </tr>
449            <tr>
450                <td colspan="2">
451                    <div id="navcontainer">
452                        <h1>Administrative Setup</h1>
453                    </div>
454                </td>
455            </tr>
456            <tr>
457                <td class="form_label"><label for="gallery">Gallery Name:</label></td>
458                <td class="form_input"><input type="text" name="gallery_name" id="gallery" value="'.$config_vars["gallery_name"].'" /></td>
459            </tr>
460            <tr>
461                <td class="form_label"><label for="username">Username:</label></td>
462                <td class="form_input"><input type="text" name="username" id="username" value="'.$config_vars["username"].'" /></td>
463            </tr>
464            <tr>
465                <td class="form_label"><label for="password">Password:</label></td>
466                <td class="form_input"><input type="password" name="password" id="password" value="'.$config_vars["password"].'" /></td>
467            </tr>
468            <tr>
469                <td class="form_label"><label for="confirm_password">Confirm Password:</label></td>
470                <td class="form_input"><input type="password" name="confirm_password" id="confirm_password" value="'.$config_vars["confirm_password"].'" /></td>
471            </tr>
472            <tr>
473                <td class="submitButtonRow" colspan="2"><input type="submit" name="submit" id="submit" value="Install" /></td>
474            </tr>
475        </table>';
476}
477
478$output .= '</form>';
479
480echo '
481    <html>
482        <head>
483            <title>Install Plogger</title>
484            <link rel="stylesheet" type="text/css" href="admin/../css/admin.css">
485        </head>
486        <body>
487            <img src="graphics/plogger.gif" alt="Plogger">
488            <h1>Install Plogger</h1>
489            <p>Welcome to the Plogger installation page.  To install, simply fill out the following form.  If there are any errors, such as unexecutable files or incorrect data, you will be notified and ask to fix them before the installation will continue.  After the installation has finished, you will be redirected to your Plogger home page.</p>
490            '.$output.'
491        </body>
492    </html>';
493
494?>
Note: See TracBrowser for help on using the browser.