source: trunk/plog-admin/includes/install-functions.php @ 590

Revision 590, 40.5 KB checked in by sidtheduck, 19 months ago (diff)

Large commit based on work with Kim Parsell and myself
Should be ready for a release candidate after this commit.
Items worked on:

  • Large overhaul for code cleanup and syntax standardization
  • Security fixes for folder permissions on all server environments (now all directories should be set to 0755 and all files set to 0644)
  • Works compeletely with safe_mode restrictions using FTP commands
  • Beginnings of plugin usage (no architecture yet, but start of code standardization and addon code)
  • Fixing comments and adding security
  • More error messages
  • Minor fixes to upgrade and install process
  • Should fix tickets #188, #206, #194, #195, #197, #201, #203, #204, #89, #174, #200
  • Many other minor edits that I can't remember now (hopefully future commits will be much smaller and deal with individual issues, enhancements, or bugs)
Line 
1<?php
2if (basename($_SERVER['PHP_SELF']) == basename( __FILE__ )) {
3        // ignorance is bliss
4        exit();
5}
6
7require_once(PLOGGER_DIR.'plog-admin/plog-admin-functions.php');
8
9/**** Common Functions ****/
10
11function maybe_add_column($table, $column, $add_sql) {
12        $sql = "DESCRIBE $table";
13        $res = mysql_query($sql);
14        $found = false;
15        while($row = mysql_fetch_array($res, MYSQL_NUM)) {
16                if ($row[0] == $column) $found = true;
17        }
18        if (!$found) {
19                mysql_query("ALTER TABLE $table ADD `$column` ".$add_sql);
20                return plog_tr('Added new field to database').': '.$column;
21        } else {
22                if (defined('PLOGGER_DEBUG')) {
23//                      return plog_tr('Field').' <strong>'.$column.'</strong> .'plog_tr('already exists, ignoring.').'';
24                        return 'Field <strong>'.$column.'</strong> already exists, ignoring.';
25                }
26        }
27}
28
29function maybe_drop_column($table, $column) {
30        $sql = "DESCRIBE $table";
31        $res = mysql_query($sql);
32        $found = false;
33        while($row = mysql_fetch_array($res, MYSQL_NUM)) {
34                if ($row[0] == $column) $found = true;
35        }
36        if ($found) {
37                $sql = "ALTER TABLE $table DROP `$column`";
38                mysql_query($sql);
39                return plog_tr('Dropped field').': '.$column;
40        } else {
41                if (defined('PLOGGER_DEBUG')) {
42//                      return $column.' '.plog_tr('does not exist').'';
43                        return $column.' does not exist';
44                }
45        }
46}
47
48function maybe_add_table($table, $add_sql, $options = '') {
49        $sql = "DESCRIBE $table";
50        $res = mysql_query($sql);
51        if (!$res) {
52                $q = "CREATE table `$table` ($add_sql) $options";
53                mysql_query($q);
54                if (mysql_error()) {
55                        var_dump(mysql_error());
56                } else {
57                        return true;
58                }
59        } else {
60                if (defined('PLOGGER_DEBUG')) {
61//                      return plog_tr('Table').' <strong>'.$table.'</strong> .'plog_tr('already exists, ignoring.').'';
62                        return 'Table <strong>'.$table.'</strong> already exists, ignoring.';
63                }
64        }
65}
66
67function gd_missing() {
68        require_once(PLOGGER_DIR.'/plog-includes/lib/phpthumb/phpthumb.functions.php');
69        // this is copied over from phpthumb
70        return phpthumb_functions::gd_version() < 1;
71}
72
73function check_requirements() {
74        $errors = array();
75
76        // check that the session variable can be read
77        if (!isset($_SESSION['plogger_session'])) {
78                $save_path = ini_get('session.save_path');
79                // check that session.save_path is set (not set by default on PHP5)
80                if (empty($save_path)) {
81                        if (!defined('SESSION_SAVE_PATH')) {
82                                $sample_text = ' ('.sprintf(plog_tr('see %s if your %s does not contain this variable'), 'plog-config-sample.php', 'plog-config.php').')';
83                        } else {
84                                $sample_text = '';
85                        }
86                        $errors[] = sprintf( plog_tr('The PHP %s variable is not set in your php.ini file.'), '<strong>session.save_path</strong>').' '.sprintf(plog_tr('You can attempt to set this by adding a writable directory path to the %s variable in %s or contact your webhost on how to set this system variable.'), '<strong>SESSION_SAVE_PATH</strong>', 'plog-config.php'.$sample_text);
87                } else {
88                        $errors[] = sprintf(plog_tr('PHP session cookies are not being set. Please check that session cookies are enabled on your browser or verify that your %s variable is set up correctly.'), '<strong>session.save_path</strong>').' '.sprintf(plog_tr('You can attempt to set this by adding a writable directory path to the %s variable in %s or contact your webhost on how to set this system variable.'), '<strong>SESSION_SAVE_PATH</strong>', 'plog-config.php'.$sample_text);
89                }
90        }
91
92        // check that the GD library is available
93        if (gd_missing()) {
94                $errors[] = plog_tr('PHP GD module was not detected.');
95        }
96
97        // check that MySQL functions are available
98        if (!function_exists('mysql_connect')) {
99                $errors[] = plog_tr('PHP MySQL module was not detected.');
100        }
101
102        // make sure we have permission to read these folders/files
103        $files_to_read = array('./plog-admin', './plog-admin/css', './plog-admin/images', './plog-content/images', './plog-content/thumbs', './plog-content/uploads', './plog-includes', './plog-includes/lib');
104        foreach($files_to_read as $file) {
105                if (!is_readable(PLOGGER_DIR.$file)) {
106                        $errors[] = sprintf(plog_tr('The path %s is not readable by the web server.'), '<strong>'.realpath(PLOGGER_DIR.$file).'</strong>');
107                }
108        }
109
110        // workaround for upgrading from beta1 since there are conflicting function in plog-functions.php and beta1 plog-connect.php
111        if (function_exists('is_safe_mode')) {
112                // if safe mode enabled, we will use the FTP workarounds to deal with folder permissions
113                if (!is_safe_mode()) {
114                        // make sure we have permission to write to these folders
115                        $files_to_write = array('./plog-content/images', './plog-content/thumbs');
116                        $i = 0;
117                        foreach($files_to_write as $file) {
118                                if (!is_writable(PLOGGER_DIR.$file)) {
119                                        $errors[] = sprintf(plog_tr('The path %s is not writable by the web server.'), '<strong>'.realpath(PLOGGER_DIR.$file).'</strong>');
120                                } else if (is_open_perms(realpath(PLOGGER_DIR.$file))) {
121                                        $_SESSION['plogger_close_perms'][basename($file)] = realpath(PLOGGER_DIR.$file);
122                                }
123                        }
124                        if (isset($_SESSION['plogger_close_perms'])) {
125                                if (!is_writable(PLOGGER_DIR.'plog-content/')) {
126                                        $errors[] = sprintf(plog_tr('Please temporarily CHMOD the %s directory to 0777 to allow Plogger to create initial directories for increased security. You will be prompted to CHMOD the directory back to 0755 after installation is complete.'), '<strong>plog-content/</strong>');
127                                }
128                        }
129                }
130        }
131
132        return $errors;
133}
134
135function check_mysql_form($form) {
136        $errors = array();
137
138        if (empty($form['db_host'])) {
139                $errors[] = plog_tr('Please enter the name of your MySQL host.');
140        }
141
142        if (empty($form['db_user'])) {
143                $errors[] = plog_tr('Please enter the MySQL username.');
144        }
145
146        if (empty($form['db_name'])) {
147                $errors[] = plog_tr('Please enter the MySQL database name.');
148        }
149
150        return $errors;
151}
152
153function check_ftp_form($form) {
154        $errors = array();
155
156        if (empty($form['ftp_host'])) {
157                $errors[] = plog_tr('Please enter the name of your FTP host.');
158        }
159
160        if (empty($form['ftp_user'])) {
161                $errors[] = plog_tr('Please enter the FTP username.');
162        }
163
164        if (empty($form['ftp_pass'])) {
165                $errors[] = plog_tr('Please enter the FTP password.');
166        }
167
168        if (!empty($form['ftp_path'])) {
169                if (substr($form['ftp_path'], 0, 1) != '/'){
170                        $form['ftp_path'] = '/'.$form['ftp_path'];
171                }
172                if (substr($form['ftp_path'], -1) != '/'){
173                        $form['ftp_path'] = $form['ftp_path'].'/';
174                }
175        }
176
177        return array('errors' => $errors, 'form' => $form);
178}
179
180function check_ftp($host, $user, $pass, $path) {
181        $errors = array();
182
183        $connection = @ftp_connect($host);
184        if (!$connection) {
185                $errors[] = sprintf(plog_tr('Cannot connect to FTP host %s. Please check your FTP Host:'), '<strong>'.$host.'</strong>');
186        } else {
187                $login = @ftp_login($connection, $user, $pass);
188                if (!$login) {
189                        $errors[] = sprintf( plog_tr('Cannot login to FTP host %s with username %s and password %s. Please check your FTP Username: and FTP Password:'), '<strong>'.$host.'</strong>', '<strong>'.$user.'</strong>', '<strong>'.$pass.'</strong>');
190                } else {
191                        $checkdir = @ftp_chdir($connection, $path.'plog-content/images/'); // check to see if the plog-content/images/ folder is accessible
192                        if (!$checkdir) {
193                                $errors[] = sprintf(plog_tr('Cannot find the Plogger %s directory along the path %s. Please check your FTP path to Plogger base folder (from FTP login):'), '<strong>plog-content/images/</strong>', '<strong>'.$path.'</strong>');
194                        }
195                }
196        }
197        @ftp_close($connection);
198        return $errors;
199}
200
201/**** Install Functions ****/
202
203function do_install($form) {
204        $form = array_map('stripslashes', $form);
205        $form = array_map('trim', $form);
206
207        // first check the requirements
208        $errors = check_requirements();
209        if (sizeof($errors) > 0) {
210                echo "\t" . '<p class="errors">'.plog_tr('Plogger cannot be installed until the following problems are resolved').':</p>';
211                echo "\n\n\t\t" . '<ul class="info">';
212                foreach($errors as $error) {
213                        echo "\n\t\t\t" . '<li class="margin-5">'.$error.'</li>';
214                }
215                echo "\n\t\t" . '</ul>';
216                echo "\n\n\t" . '<form method="get" action="'.$_SERVER['REQUEST_URI'].'">
217                <p><input class="submit" type="submit" value="'.plog_tr('Try again').'" /></p>
218        </form>' . "\n";
219                return false;
220        }
221
222        $ok = false;
223        $errors = array();
224
225        // if we've already defined the database information, pass the values and skip them on the form
226        if (defined('PLOGGER_DB_HOST')) {
227                $mysql = check_mysql(PLOGGER_DB_HOST, PLOGGER_DB_USER, PLOGGER_DB_PW, PLOGGER_DB_NAME);
228                if (!empty($mysql)) {
229                        $mysql_fail = true;
230                } else {
231                        unset($_SESSION['plogger_config']);
232                }
233                // set the form values equal to config values if already set
234                if (empty($form['db_host'])) {
235                        $form['db_host'] = PLOGGER_DB_HOST;
236                }
237                if (empty($form['db_user'])) {
238                        $form['db_user'] = PLOGGER_DB_USER;
239                }
240                if (empty($form['db_pass'])) {
241                        $form['db_pass'] = PLOGGER_DB_PW;
242                }
243                if (empty($form['db_name'])) {
244                        $form['db_name'] = PLOGGER_DB_NAME;
245                }
246        }
247
248        if (isset($form['action']) && $form['action'] == 'install') {
249                if (!defined('PLOGGER_DB_HOST') || isset($mysql_fail)) {
250                        $mysql_form_check = check_mysql_form($form);
251                        if (!empty($mysql_form_check)) {
252                                $errors = array_merge($errors, $mysql_form_check);
253                        }
254                }
255
256                if (empty($form['gallery_name'])) {
257                        $errors[] = plog_tr('Please enter the name for your gallery.');
258                }
259
260                if (empty($form['admin_email'])) {
261                        $errors[] = plog_tr('Please enter your email address.');
262                }
263
264                if (empty($form['admin_username'])) {
265                        $errors[] = plog_tr('Please enter a username.');
266                }
267
268                if (empty($form['admin_password'])) {
269                        $errors[] = plog_tr('Please enter a password.');
270                }
271
272                if (is_safe_mode()) {
273                        //if safe_mode enabled, check the FTP information form inputs
274                        $ftp_form_check = check_ftp_form($form);
275                        $form = $ftp_form_check['form'];
276                        if (!empty($ftp_form_check['form']['errors'])) {
277                                $errors = array_merge($errors, $ftp_form_check['form']['errors']);
278                        }
279                }
280
281                if (empty($errors)) {
282                        $mysql_errors = check_mysql($form['db_host'], $form['db_user'], $form['db_pass'], $form['db_name']);
283                        if (is_safe_mode()) {
284                                $ftp_errors = check_ftp($form['ftp_host'], $form['ftp_user'], $form['ftp_pass'], $form['ftp_path']);
285                        } else {
286                                $ftp_errors = array();
287                        }
288                        $errors = array_merge($mysql_errors, $ftp_errors);
289                        $ok = empty($errors);
290                }
291
292                if (!$ok) {
293                        echo '<ul class="errors" style="background-image: none;">' . "\n\t" . '<li class="margin-5">';
294                        echo join("</li>\n\t<li class=\"margin-5\">", $errors);
295                        echo "</li>\n</ul>\n\n";
296                } else {
297                        $_SESSION['install_values'] = array(
298                                'gallery_name' => $form['gallery_name'],
299                                'admin_email' => $form['admin_email'],
300                                'admin_password' => $form['admin_password'],
301                                'admin_username' => $form['admin_username']
302                        );
303                        if (is_safe_mode()) {
304                                $_SESSION['ftp_values'] = array(
305                                        'ftp_host' => $form['ftp_host'],
306                                        'ftp_user' => $form['ftp_user'],
307                                        'ftp_pass' => $form['ftp_pass'],
308                                        'ftp_path' => $form['ftp_path']
309                                );
310                        }
311
312                        if (!defined('PLOGGER_DB_HOST') || isset($mysql_fail)) {
313                                // serve the config file and ask user to upload it to webhost
314                                $_SESSION['plogger_config'] = create_config_file($form['db_host'], $form['db_user'], $form['db_pass'], $form['db_name']);
315                        }
316                        return true;
317                }
318        }
319
320        include(PLOGGER_DIR.'plog-admin/includes/install-form-setup.php');
321        return false;
322}
323
324function create_tables() {
325        // since 4.1 MySQL has support for specifying character encoding for tables
326        // and I really want to  use it if available. So we need figure out what version
327        // we are running on and to the right thing
328        $mysql_version = mysql_get_server_info();
329        $mysql_charset_support = '4.1';
330        $default_charset = '';
331
332        if (1 == version_compare($mysql_version, $mysql_charset_support)) {
333                $default_charset = 'DEFAULT CHARACTER SET UTF8';
334        }
335
336        maybe_add_table(
337        PLOGGER_TABLE_PREFIX.'collections'
338        ,"`name` varchar(128) NOT NULL default '',
339        `description` varchar(255) NOT NULL default '',
340        `path` varchar(255) NOT NULL default '',
341        `id` int(11) NOT NULL auto_increment,
342        `thumbnail_id` int(11) NOT NULL DEFAULT '0',
343        PRIMARY KEY (id)"
344        ,"Type=MyISAM $default_charset");
345
346        maybe_add_table(
347        PLOGGER_TABLE_PREFIX.'albums'
348        ," `name` varchar(128) NOT NULL default '',
349        `id` int(11) NOT NULL auto_increment,
350        `description` varchar(255) NOT NULL default '',
351        `path` varchar(255) NOT NULL default '',
352        `parent_id` int(11) NOT NULL default '0',
353        `thumbnail_id` int(11) NOT NULL default '0',
354        PRIMARY KEY (id),
355        INDEX pid_idx (parent_id)"
356        ," Type=MyISAM $default_charset");
357
358        maybe_add_table(
359        PLOGGER_TABLE_PREFIX.'pictures'
360        ,"`path` varchar(255) NOT NULL default '',
361        `parent_album` int(11) NOT NULL default '0',
362        `parent_collection` int(11) NOT NULL default '0',
363        `caption` mediumtext NOT NULL,
364        `description` text NOT NULL,
365        `id` int(11) NOT NULL auto_increment,
366        `date_modified` timestamp(14) NOT NULL,
367        `date_submitted` timestamp(14) NOT NULL,
368        `EXIF_date_taken` varchar(64) NOT NULL default '',
369        `EXIF_camera` varchar(64) NOT NULL default '',
370        `EXIF_shutterspeed` varchar(64) NOT NULL default '',
371        `EXIF_focallength` varchar(64) NOT NULL default '',
372        `EXIF_flash` varchar(64) NOT NULL default '',
373        `EXIF_aperture` varchar(64) NOT NULL default '',
374        `EXIF_iso` varchar(64) NOT NULL default '',
375        `allow_comments` int(11) NOT NULL default '1',
376        PRIMARY KEY (id),
377        INDEX pa_idx (parent_album),
378        INDEX pc_idx (parent_collection)"
379        ,"Type=MyISAM $default_charset");
380
381        maybe_add_table(
382        PLOGGER_TABLE_PREFIX.'comments'
383        ,"`id` int(11) NOT NULL auto_increment,
384        `parent_id` int(11) NOT NULL default '0',
385        `author` varchar(64) NOT NULL default '',
386        `email` varchar(64) NOT NULL default '',
387        `url` varchar(64) NOT NULL default '',
388        `date` datetime NOT NULL,
389        `comment` longtext NOT NULL,
390        `ip` char(64),
391        `approved` tinyint default '1',
392        PRIMARY KEY (id),
393        INDEX pid_idx (parent_id),
394        INDEX approved_idx (approved)"
395        ,"Type=MyISAM $default_charset");
396
397        maybe_add_table(
398        PLOGGER_TABLE_PREFIX.'config'
399        ,"`max_thumbnail_size` int(11) NOT NULL default '0',
400        `max_display_size` int(11) NOT NULL default '0',
401        `thumb_num` int(11) NOT NULL default '0',
402        `admin_username` varchar(64) NOT NULL default '',
403        `admin_password` varchar(64) NOT NULL default '',
404        `activation_key` varchar(64) NOT NULL default '',
405        `admin_email` varchar(50) NOT NULL default '',
406        `date_format` varchar(64) NOT NULL default '',
407        `compression` int(11) NOT NULL default '75',
408        `default_sortby` varchar(20) NOT NULL default '',
409        `default_sortdir` varchar(5) NOT NULL default '',
410        `album_sortby` varchar(20) NOT NULL default '',
411        `album_sortdir` varchar(5) NOT NULL default '',
412        `collection_sortby` varchar(20) NOT NULL default '',
413        `collection_sortdir` varchar(5) NOT NULL default '',
414        `gallery_name` varchar(255) NOT NULL default '',
415        `allow_dl` smallint(1) NOT NULL default '0',
416        `allow_comments` smallint(1) NOT NULL default '1',
417        `allow_print` smallint(1) NOT NULL default '1',
418        `truncate` int(11) NOT NULL default '12',
419        `square_thumbs` tinyint default 1,
420        `feed_num_entries` int(15) NOT NULL default '15',
421        `rss_thumbsize` int(11) NOT NULL default '400',
422        `feed_title` text NOT NULL,
423        `use_mod_rewrite` tinyint NOT NULL default '0',
424        `gallery_url` varchar(255) NOT NULL default '',
425        `comments_notify` tinyint NOT NULL default '1',
426        `comments_moderate` tinyint NOT NULL default '0',
427        `feed_language` varchar(255) NOT NULL default 'en-us',
428        `theme_dir` varchar(128) NOT NULL default '',
429        `thumb_nav_range` int(11) NOT NULL default '0',
430        `enable_thumb_nav` tinyint default '0',
431        `allow_fullpic` tinyint default '1',
432        PRIMARY KEY (`thumb_num`)"
433        ,"Type=MyISAM $default_charset");
434
435        maybe_add_table(
436        PLOGGER_TABLE_PREFIX.'thumbnail_config'
437        ,"`id` int(10) unsigned NOT NULL auto_increment,
438        `update_timestamp` int(10) unsigned default NULL,
439        `max_size` int(10) unsigned default NULL,
440        `disabled` tinyint default 0,
441        PRIMARY KEY (`id`)"
442        ,"Type=MyISAM $default_charset");
443
444        /*maybe_add_table(
445        PLOGGER_TABLE_PREFIX.'tag2picture'
446        ,"`tag_id` bigint(20) unsigned NOT NULL default '0',
447        `picture_id` bigint(20) unsigned NOT NULL default '0',
448        `tagdate` datetime default NULL,
449        KEY `tag_id` (`tag_id`),
450        KEY `picture_id` (`picture_id`)"
451        ,"Type=MyISAM $default_charset");
452
453        maybe_add_table(
454        PLOGGER_TABLE_PREFIX.'tags'
455        ,"`id` bigint(20) unsigned NOT NULL auto_increment,
456        `tag` char(50) NOT NULL default '',
457        `tagdate` datetime NOT NULL default '0000-00-00 00:00:00',
458        `urlified` char(50) NOT NULL default '',
459        PRIMARY KEY  (`id`),
460        UNIQUE `tag` (`tag`),
461        UNIQUE `urlified` (`urlified`)"
462        ,"Type=MyISAM $default_charset");*/
463
464}
465
466function configure_plogger($form) {
467        // use a random timestamp from the past to keep the existing thumbnails
468        $long_ago = 1096396500;
469
470        $thumbnail_sizes = array(
471                THUMB_SMALL => 100,
472                THUMB_LARGE => 500,
473                THUMB_RSS => 400,
474                THUMB_NAV => 60
475        );
476
477        foreach($thumbnail_sizes as $key => $size) {
478                $sql = "INSERT INTO `".PLOGGER_TABLE_PREFIX."thumbnail_config` (id, update_timestamp, max_size)
479                VALUES('$key', '$long_ago', '$size')";
480                mysql_query($sql);
481        }
482
483        $config['gallery_url'] = 'http://'.$_SERVER['SERVER_NAME'].dirname(dirname($_SERVER['PHP_SELF']));
484        // remove plog-admin/ from the end, if present .. is there a better way to determine the full url?
485        if (strpos($config['gallery_url'], 'plog-admin/')) {
486                $config['gallery_url'] = substr($config['gallery_url'], 0, strpos($config['gallery_url'], 'plog-admin/'));
487        }
488        // verify that gallery URL contains a trailing slash. if not, add one.
489        if ($config['gallery_url']{strlen($config['gallery_url'])-1} != '/') {
490                $config['gallery_url'] .= '/';
491        }
492        // verify that the gallery URL begins with 'http://' for mod_rewrite 301 redirects
493        if (strpos($config['gallery_url'], 'http://') === false) {
494                $config['gallery_url'] = 'http://'.$config['gallery_url'];
495        }
496        $config['admin_username'] = $form['admin_username'];
497        $config['admin_password'] = $form['admin_password'];
498        $config['admin_email'] = $form['admin_email'];
499        $config['gallery_name'] = $form['gallery_name'];
500
501        $config = array_map('mysql_real_escape_string', $config);
502
503        $row_exist = mysql_query("SELECT * FROM `".PLOGGER_TABLE_PREFIX."config`");
504        $row_exist_num = mysql_num_rows($row_exist);
505
506        if ($row_exist_num == 0) {
507                $query = "INSERT INTO `".PLOGGER_TABLE_PREFIX."config`
508                        (`theme_dir`,
509                        `compression`,
510                        `max_thumbnail_size`,
511                        `max_display_size`,
512                        `thumb_num`,
513                        `admin_username`,
514                        `admin_email`,
515                        `admin_password`,
516                        `date_format`,
517                        `feed_title`,
518                        `gallery_name`,
519                        `gallery_url`)
520                        VALUES
521                        ('default',
522                        75,
523                        100,
524                        500,
525                        20,
526                        '${config['admin_username']}',
527                        '${config['admin_email']}',
528                        MD5('${config['admin_password']}'),
529                        'n.j.Y',
530                        'Plogger Photo Feed',
531                        '${config['gallery_name']}',
532                        '${config['gallery_url']}')";
533        } else {
534                $query = "UPDATE `".PLOGGER_TABLE_PREFIX."config` SET
535                        `theme_dir` = 'default',
536                        `compression` = 75,
537                        `max_thumbnail_size` = 100,
538                        `max_display_size` = 500,
539                        `thumb_num` = 20,
540                        `admin_username` = '${config['admin_username']}',
541                        `admin_email` = '${config['admin_email']}',
542                        `admin_password` = MD5('${config['admin_password']}'),
543                        `date_format` = 'n.j.Y',
544                        `feed_title` = 'Plogger Photo Feed',
545                        `gallery_name` = '${config['gallery_name']}',
546                        `gallery_url` = '${config['gallery_url']}'";
547        }
548        mysql_query($query);
549
550        // create the FTP columns in the config table if safe_mode enabled/
551        if (is_safe_mode() && isset($_SESSION['ftp_values'])) {
552                configure_ftp($_SESSION['ftp_values']);
553        }
554
555        // send an email with the username and password
556        $from = str_replace('www.', '', $_SERVER['HTTP_HOST']);
557        ini_set('sendmail_from', 'noreply@'.$from); // set for windows machines
558        @mail(
559                $config['admin_email'],
560                plog_tr('[Plogger] Your new gallery'),
561                plog_tr('You have successfully installed your new Plogger gallery.') . "\n\n" .sprintf(plog_tr('You can log in and manage it at %s'), $config['gallery_url'].'plog-admin/') . "\n\n" .plog_tr('Username').': '.$config['admin_username']. "\n" .plog_tr('Password').': '.$config['admin_password'],
562                'From: Plogger <noreply@'.$from.'>'
563        );
564}
565
566function configure_ftp($form) {
567        maybe_add_column(PLOGGER_TABLE_PREFIX.'config', 'ftp_host', "varchar(64) NOT NULL default ''");
568        maybe_add_column(PLOGGER_TABLE_PREFIX.'config', 'ftp_user', "varchar(64) NOT NULL default ''");
569        maybe_add_column(PLOGGER_TABLE_PREFIX.'config', 'ftp_pass', "varchar(64) NOT NULL default ''");
570        maybe_add_column(PLOGGER_TABLE_PREFIX.'config', 'ftp_path', "varchar(255) NOT NULL default ''");
571        $query = "UPDATE `".PLOGGER_TABLE_PREFIX."config` SET
572                `ftp_host` = '".$form['ftp_host']."',
573                `ftp_user` = '".$form['ftp_user']."',
574                `ftp_pass` = '".$form['ftp_pass']."',
575                `ftp_path` = '".$form['ftp_path']."'";
576        mysql_query($query);
577}
578
579function fix_open_perms($dirs, $action = 'rename') {
580        if (!empty($dirs)) {
581                foreach ($dirs as $key => $dir) {
582                        if ($action == 'delete') {
583                                kill_dir(PLOGGER_DIR.'plog-content/'.$key);
584                        } else {
585                                @rename(PLOGGER_DIR.'plog-content/'.$key, PLOGGER_DIR.'plog-content/'.$key.'-old');
586                        }
587                        makeDirs(PLOGGER_DIR.'plog-content/'.$key);
588                }
589        }
590}
591
592function create_config_file($db_host, $db_user, $db_pass, $db_name) {
593        $cfg_file = "<?php\n";
594        $cfg_file .= "/* You can manually modify this file before installing (renaming this file as plog-config.php before\n";
595        $cfg_file .= " * installation) or you can let Plogger generate the file automatically by running the installation\n";
596        $cfg_file .= " * script (run plog-admin/_install.php in your browser).\n\n";
597        $cfg_file .= " * If you want to change the database connection information, you may also edit this file manually\n";
598        $cfg_file .= " * after Plogger has been installed. */\n\n";
599        $cfg_file .= "/* MySQL hostname */\n";
600        $cfg_file .= "define('PLOGGER_DB_HOST', '".$db_host."');\n\n";
601        $cfg_file .= "/* MySQL database username */\n";
602        $cfg_file .= "define('PLOGGER_DB_USER', '".$db_user."');\n\n";
603        $cfg_file .= "/* MySQL database password */\n";
604        $cfg_file .= "define('PLOGGER_DB_PW', '".addcslashes($db_pass, "\\'")."');\n\n"; // escape certain password characters stored in single quotes (\) (')
605        $cfg_file .= "/* The name of the database for Plogger */\n";
606        $cfg_file .= "define('PLOGGER_DB_NAME', '".$db_name."');\n\n";
607        $cfg_file .= "/* Define the Plogger Database Table prefix. You can have multiple installations in one database\n";
608        $cfg_file .= " * if you give each a unique prefix. Only numbers, letters, and underscores are permitted (i.e., plogger_). */\n";
609        $cfg_file .= "define('PLOGGER_TABLE_PREFIX', 'plogger_');\n\n";
610        $cfg_file .= "/* Define the Plogger directory permissions. Change permissions if you are having issues with images or sub-directories\n";
611        $cfg_file .= " * being saved, moved, or deleted from the Plogger-created directories (i.e. Collections or Albums) */\n";
612        $cfg_file .= "define('PLOGGER_CHMOD_DIR', 0755);\n\n";
613        $cfg_file .= "/* Define the Plogger file permissions. Change permissions if you are having issues with\n";
614        $cfg_file .= " * viewing, deleting, or moving images within Plogger (i.e. Pictures) */\n";
615        $cfg_file .= "define('PLOGGER_CHMOD_FILE', 0644);\n\n";
616        $cfg_file .= "/* Is Plogger embedded in another program, like WordPress?\n";
617        $cfg_file .= " * 1/0 (True/False) if set will overrule automatic check */\n";
618        $cfg_file .= "define('PLOGGER_EMBEDDED', '');\n\n";
619        $cfg_file .= "/* Define a directory path to save session variables if you are having trouble\n";
620        $cfg_file .= " * logging in or Plogger is telling you that you have session.save_path issues\n";
621        $cfg_file .= " * and/or if your server php.ini setup has a blank session.save_path php.ini variable */\n";
622        $cfg_file .= "define('PLOGGER_SESSION_SAVE_PATH', '');\n\n";
623        $cfg_file .= "/* Plogger localized language, defaults to English. Change this to localize Plogger.\n";
624        $cfg_file .= " * A corresponding MO file for the chosen language must be installed in /plog-content/translations/.\n";
625        $cfg_file .= " * For example, upload de.mo to /plog-content/translations/ and set PLOGGER_LOCALE to 'de' to\n";
626        $cfg_file .= " * enable German language support.\n";
627        $cfg_file .= " * Example language codes: da, de, et, fr, pl, ro, tr, en-CA (for Canadian English) */\n";
628        $cfg_file .= "define('PLOGGER_LOCALE', '');\n\n";
629        $cfg_file .= "?>";
630        return $cfg_file;
631}
632
633/**** Upgrade Functions ****/
634
635function upgrade_database() {
636        global $config, $thumbnail_config;
637        $output = array();
638
639/** plogger_thumbnail_config **/
640        $thumb_table = maybe_add_table(
641                PLOGGER_TABLE_PREFIX.'thumbnail_config'
642                ,"`id` int(10) unsigned NOT NULL auto_increment,
643                `update_timestamp` int(10) unsigned default NULL,
644                `max_size` int(10) unsigned default NULL,
645                `disabled` tinyint default 0,
646                PRIMARY KEY (`id`)"
647                );
648
649        if ($thumb_table === true) {
650                $output[] = plog_tr('Added new table').': '.PLOGGER_TABLE_PREFIX.'thumbnail_config';
651                // use a random timestamp from the past to keep the existing thumbnails
652                $long_ago = 1096396500;
653
654                if (!isset($config['max_thumbnail_size'])) {
655                        $config['max_thumbnail_size'] = 100;
656                }
657                if (!isset($thumbnail_config[THUMB_SMALL]) || empty($thumbnail_config[THUMB_SMALL]['size'])) {
658                        $sql = "INSERT INTO `".PLOGGER_TABLE_PREFIX."thumbnail_config` (id, update_timestamp, max_size)
659                                VALUES('".THUMB_SMALL."', '".$long_ago."', '".$config['max_thumbnail_size']."')";
660                        mysql_query($sql);
661                }
662
663                if (!isset($config['max_display_size'])) {
664                        $config['max_display_size'] = 500;
665                }
666                if (!isset($thumbnail_config[THUMB_LARGE]) || empty($thumbnail_config[THUMB_LARGE]['size'])) {
667                        $sql = "INSERT INTO `".PLOGGER_TABLE_PREFIX."thumbnail_config` (id, update_timestamp, max_size)
668                                VALUES('".THUMB_LARGE."', '".$long_ago."', '".$config['max_display_size']."')";
669                        mysql_query($sql);
670                }
671
672                if (!isset($config['rss_thumbsize'])) {
673                        $config['rss_thumbsize'] = 400;
674                }
675                if (!isset($thumbnail_config[THUMB_RSS]) || empty($thumbnail_config[THUMB_RSS]['size'])) {
676                        $sql = "INSERT INTO `".PLOGGER_TABLE_PREFIX."thumbnail_config` (id, update_timestamp, max_size)
677                                VALUES('".THUMB_RSS."', '".$long_ago."', '".$config['rss_thumbsize']."')";
678                        mysql_query($sql);
679                }
680
681                if (!isset($config['nav_thumbsize'])) {
682                        $config['nav_thumbsize'] = 60;
683                }
684                if (!isset($thumbnail_config[THUMB_NAV]) || empty($thumbnail_config[THUMB_NAV]['size'])) {
685                        $sql = "INSERT INTO `".PLOGGER_TABLE_PREFIX."thumbnail_config` (id, update_timestamp, max_size)
686                                VALUES('".THUMB_NAV."', '".$long_ago."', '".$config['nav_thumbsize']."')";
687                        mysql_query($sql);
688                }
689        }
690
691        $thumbnail_add_list = array(
692                'disabled' => "tinyint default 0"
693        );
694        foreach ($thumbnail_add_list as $key => $value) {
695                $result = maybe_add_column(PLOGGER_TABLE_PREFIX.'thumbnail_config', $key, $value);
696                if (!empty($result)) {
697                        $output[] = $result;
698                }
699        }
700
701/** plogger_config **/
702        $config_drop_list = array(
703                'max_thumbnail_size',
704                'max_display_size',
705                'rss_thumbsize'
706        );
707        foreach ($config_drop_list as $value) {
708                $result = maybe_drop_column(PLOGGER_TABLE_PREFIX.'config', $value);
709                if (!empty($result)) {
710                        $output[] = $result;
711                }
712        }
713
714        $config_add_list = array(
715                'gallery_url' => "varchar(255) NOT NULL",
716                // RSS config
717                'feed_num_entries' => "int(15) NOT NULL default '15'",
718                'feed_title' => "varchar(255) NOT NULL default 'Plogger Photo Feed'",
719                'feed_language' => "varchar(20) NOT NULL default 'en-us'",
720                // cruft-free URLs
721                'use_mod_rewrite' => "smallint NOT NULL default '0'",
722                // default sort order
723                'default_sortdir' => "varchar(5) NOT NULL",
724                'default_sortby' => "varchar(20) NOT NULL",
725                // add field for admin email
726                'admin_email' => "varchar(50) NOT NULL",
727                // disable link to full size pic
728                'allow_fullpic' => "tinyint NOT NULL default '1'",
729                // comment notify
730                'comments_notify' => "tinyint NOT NULL",
731                // comment moderation
732                'comments_moderate' => "tinyint NOT NULL default 0",
733                // square thumbs
734                'square_thumbs' => "tinyint default 1",
735                // user definable theme directory
736                'theme_dir' => "varchar(128) NOT NULL",
737                // add support for user defined sort order for albums and collections
738                'album_sortby' => "varchar(20) NOT NULL default 'id'",
739                'album_sortdir' => "varchar(5) NOT NULL default 'DESC'",
740                'collection_sortby' => "varchar(20) NOT NULL default 'id'",
741                'collection_sortdir' => "varchar(5) NOT NULL default 'DESC'",
742                // add support for thumbnail configuration
743                'enable_thumb_nav' => "tinyint default 0",
744                'thumb_nav_range' => "int(11) NOT NULL default 0",
745                // add reset password activation key
746                'activation_key' => "varchar(64) NOT NULL default ''"
747        );
748        foreach ($config_add_list as $key => $value) {
749                $result = maybe_add_column(PLOGGER_TABLE_PREFIX.'config', $key, $value);
750                if (!empty($result)) {
751                        $output[] = $result;
752                }
753        }
754
755        // insert the gallery_url if not already set
756        if (!isset($config['gallery_url']) || empty($config['gallery_url'])) {
757                $config['baseurl'] = 'http://'.$_SERVER['HTTP_HOST'].dirname(dirname($_SERVER['PHP_SELF'])).'/';
758                $output[] = plog_tr('Setting gallery url to ').$config['baseurl'];
759                $sql = "UPDATE `".PLOGGER_TABLE_PREFIX."config` SET gallery_url = '".$config['baseurl']."'";
760                mysql_query($sql);
761        }
762
763        // insert default theme directory if not already set
764        if (!isset($config['theme_dir']) || empty($config['theme_dir'])) {
765                $output[] = plog_tr('Setting default theme directory to \'default\'');
766                $sql = "UPDATE ".PLOGGER_TABLE_PREFIX."config SET `theme_dir` = 'default' WHERE 1";
767                mysql_query($sql);
768        }
769
770/** plogger_collections **/
771        $collections_add_list = array(
772                // selectable thumbnails
773                'thumbnail_id' => "int(11) NOT NULL default 0",
774                // add the path column
775                'path' => "varchar(255) NOT NULL"
776        );
777        foreach ($collections_add_list as $key => $value) {
778                $result = maybe_add_column(PLOGGER_TABLE_PREFIX.'collections', $key, $value);
779                if (!empty($result)) {
780                        $output[] = $result;
781                }
782        }
783
784/** plogger_albums **/
785        $albums_add_list = array(
786                // selectable thumbnails
787                'thumbnail_id' => "int(11) NOT NULL default 0",
788                // add the path column
789                'path' => "varchar(255) NOT NULL"
790        );
791        foreach ($albums_add_list as $key => $value) {
792                $result = maybe_add_column(PLOGGER_TABLE_PREFIX.'albums', $key, $value);
793                if (!empty($result)) {
794                        $output[] = $result;
795                }
796        }
797
798/** plogger_pictures **/
799        $pictures_add_list = array(
800                // add description
801                'description' => "text",
802                'EXIF_iso' => "varchar(64) NOT NULL default ''"
803        );
804        foreach ($pictures_add_list as $key => $value) {
805                $result = maybe_add_column(PLOGGER_TABLE_PREFIX.'pictures', $key, $value);
806                if (!empty($result)) {
807                        $output[] = $result;
808                }
809        }
810
811/** plogger_comments **/
812        $comments_add_list = array(
813                // add ip and approved fields to comments table
814                'ip' => "char(64)",
815                'approved' => "tinyint default 1"
816        );
817        foreach ($comments_add_list as $key => $value) {
818                $result = maybe_add_column(PLOGGER_TABLE_PREFIX.'comments', $key, $value);
819                if (!empty($result)) {
820                        $output[] = $result;
821                }
822        }
823
824                /*$output[] = maybe_add_table(PLOGGER_TABLE_PREFIX.'tag2picture',"
825                `tag_id` bigint(20) unsigned NOT NULL default '0',
826                `picture_id` bigint(20) unsigned NOT NULL default '0',
827                `tagdate` datetime default NULL,
828                KEY `tag_id` (`tag_id`),
829                KEY `picture_id` (`picture_id`)
830        ");
831
832        $output[] = maybe_add_table(PLOGGER_TABLE_PREFIX.'tags',"
833                `id` bigint(20) unsigned NOT NULL auto_increment,
834                `tag` char(50) NOT NULL default '',
835                `tagdate` datetime NOT NULL default '0000-00-00 00:00:00',
836                `urlified` char(50) NOT NULL default '',
837                PRIMARY KEY  (`id`),
838                UNIQUE `tag` (`tag`),
839                UNIQUE `urlified` (`urlified`)
840        ");*/
841
842        $sql = 'ALTER TABLE '.PLOGGER_TABLE_PREFIX.'comments ADD INDEX approved_idx (`approved`)';
843        mysql_query($sql);
844
845        // add ip and approved fields to comments table
846        $sql = 'ALTER TABLE '.PLOGGER_TABLE_PREFIX.'comments CHANGE `date` `date` datetime';
847        mysql_query($sql);
848
849        // convert charsets
850        // since 4.1 MySQL has support for specifying character encoding for tables
851        // and I really want to use it if available. So we need figure out what version
852        // we are running on and to the right hting
853        $mysql_version = mysql_get_server_info();
854        $mysql_charset_support = '4.1';
855        $default_charset = '';
856
857        if (1 == version_compare($mysql_version,$mysql_charset_support)) {
858                $charset = 'utf8';
859                $tables = array('collections', 'albums', 'pictures', 'comments', 'config', 'thumbnail_config');
860                foreach($tables as $table) {
861                        $tablename = PLOGGER_TABLE_PREFIX.$table;
862                        $sql = "ALTER TABLE $tablename DEFAULT CHARACTER SET $charset";
863                        if (!mysql_query($sql)) {
864                                $output[] = "failed to convert $tablename to $charset<br />".mysql_error();
865                        }
866                }
867        }
868
869        return $output;
870}
871
872function upgrade_image_list() {
873        $list = array();
874        $total = 0;
875
876        // strip 'images/' prefix from pictures table
877        $sql = "UPDATE ".PLOGGER_TABLE_PREFIX."pictures SET path = SUBSTRING(path,8) WHERE SUBSTRING(path,1,7) = 'images/'";
878        mysql_query($sql);
879
880        // update 'path' for collections table
881        $sql = "SELECT id,name FROM ".PLOGGER_TABLE_PREFIX."collections";
882        $result = mysql_query($sql);
883        while($row = mysql_fetch_assoc($result)) {
884                $sql = "UPDATE ".PLOGGER_TABLE_PREFIX."collections SET path = '".strtolower(sanitize_filename($row['name']))."' WHERE id = ".$row['id'];
885                mysql_query($sql);
886                if (!file_exists(PLOGGER_DIR.'plog-content/images/'.strtolower(sanitize_filename($row['name'])))) {
887                        $list[$total] = array('container' => 1, 'new_path' => 'plog-content/images/'.strtolower(sanitize_filename($row['name'])));
888                        $total++;
889                }
890        }
891
892        // update 'path' for albums table
893        $sql = "SELECT a.id AS id, a.name AS name, c.path AS collection_path
894                                        FROM ".PLOGGER_TABLE_PREFIX."albums a, ".PLOGGER_TABLE_PREFIX."collections c
895                                        WHERE a.parent_id = c.id";
896        $result = mysql_query($sql);
897        while($row = mysql_fetch_assoc($result)) {
898                $sql = "UPDATE ".PLOGGER_TABLE_PREFIX."albums SET path = '".strtolower(sanitize_filename($row['name']))."' WHERE id = ".$row['id'];
899                mysql_query($sql);
900                if (!file_exists(PLOGGER_DIR.'plog-content/images/'.$row['collection_path'].'/'.strtolower(sanitize_filename($row['name'])))) {
901                        $list[$total] = array('container' => 1, 'new_path' => 'plog-content/images/'.$row['collection_path'].'/'.strtolower(sanitize_filename($row['name'])));
902                        $total++;
903                }
904        }
905
906        // loop through each image from the pictures table, get its parent album name and parent collection
907        $sql = "SELECT p.path AS path, p.id AS pid,c.path AS collection_path, a.path AS album_path
908                        FROM ".PLOGGER_TABLE_PREFIX."albums a, ".PLOGGER_TABLE_PREFIX."pictures p, ".PLOGGER_TABLE_PREFIX."collections c
909                        WHERE p.parent_album = a.id AND p.parent_collection = c.id";
910        $result = mysql_query($sql);
911
912        while($row = mysql_fetch_assoc($result)) {
913                $filename = sanitize_filename(basename($row['path']));
914                $c_directory = $row['collection_path'].'/';
915                $a_directory = $row['collection_path'].'/'.$row['album_path'].'/';
916                $new_path = $row['collection_path'].'/'.$row['album_path'].'/'.$filename;
917                // if the file exists, grab the information and add to the total
918                if (!file_exists(PLOGGER_DIR.'plog-content/images/'.$new_path)) {
919                        // first see if it's in the old directory structure
920                        if (file_exists(PLOGGER_DIR.'images/'.$row['path'])) {
921                                $path = 'images/';
922                        // next check the temporary folder location for closing folder permissions
923                        } else if (file_exists(PLOGGER_DIR.'plog-content/images-old/'.$row['path'])) {
924                                $path = 'plog-content/images-old/';
925                        // otherwise check if it's in the new structure, but set up without new sanitized paths
926                        } else if (file_exists(PLOGGER_DIR.'plog-content/images/'.$row['path'])) {
927                                $path = 'plog-content/images/';
928                        } else {
929                                // have no idea where the old image is
930                                $path = '';
931                        }
932                        $list[$total] = array('id' => $row['pid'], 'old_path' => $path.$row['path'], 'new_path' => $new_path);
933                        $total++;
934                }
935        }
936
937        // add any photos from the uploads directory
938        if (file_exists(PLOGGER_DIR.'uploads/')) {
939                $old_uploads = get_files(PLOGGER_DIR.'uploads/', false, false, dirname(dirname(dirname(__FILE__))).'/uploads/');
940                $new_uploads = get_files(PLOGGER_DIR.'plog-content/uploads/', false, false, dirname(dirname(dirname(__FILE__))).'/plog-content/uploads/');
941
942                // compare the two paths for differences
943                $compare_uploads = array_diff($old_uploads, $new_uploads);
944                foreach ($compare_uploads as $uploads) {
945                        $list[$total] = array('uploads' => 1, 'old_path' => 'uploads/'.$uploads, 'new_path' => 'plog-content/uploads/'.$uploads);
946                        $total++;
947                }
948        }
949
950        $list['total'] = $total;
951        return $list;
952}
953
954function upgrade_images($num, $list) {
955        $output = array();
956        $errors = array();
957        $count = 0;
958
959        $list = array_slice($list, 0, $num);
960
961        foreach ($list as $image) {
962                if (!empty($image['id'])) {
963                        // work on the images - move physical file, create directory if necessary and update path in database
964                        if (!makeDirs(PLOGGER_DIR.'plog-content/images/'.dirname($image['new_path'].'/'))) {
965                                $errors[] = plog_tr('Could not create directory').': '.PLOGGER_DIR.'plog-content/images/'.$image['new_path'];
966                        } else {
967                                if (!move_this(PLOGGER_DIR.$image['old_path'], PLOGGER_DIR.'plog-content/images/'.$image['new_path'])) {
968                                        $errors[] = plog_tr('Could not move file').': '.PLOGGER_DIR.$image['old_path'];
969                                } else {
970                                        @chmod(PLOGGER_DIR.$new_path, PLOGGER_CHMOD_DIR);
971                                        $output[] = sprintf(plog_tr('Moved file %s -> %s'), '<strong>'.$image['old_path'].'</strong>', '<strong>'.'plog-content/images/'.$image['new_path'].'</strong>');
972                                        // generate a new small thumbnail
973                                        $thumbpath = generate_thumb(PLOGGER_DIR.'plog-content/images/'.$image['new_path'], $image['id'], THUMB_SMALL);
974                                        // update database
975                                        $sql = "UPDATE ".PLOGGER_TABLE_PREFIX."pictures SET path = '".mysql_real_escape_string($image['new_path'])."' WHERE id = '".$image['id']."'";
976                                        run_query($sql);
977                                        $count++;
978                                }
979                        }
980                } else if (!empty($image['uploads'])) {
981                        // work on the uploads - move physical file and create directory in the uploads folder if necessary and update path in database
982                        if (!makeDirs(PLOGGER_DIR.dirname($image['new_path'].'/'))) {
983                                $errors[] = plog_tr('Could not create directory').': '.PLOGGER_DIR.$image['new_path'];
984                        } else {
985                                if (!move_this(PLOGGER_DIR.$image['old_path'], PLOGGER_DIR.$image['new_path'])) {
986                                        $errors[] = plog_tr('Could not move file').': '.PLOGGER_DIR.$image['old_path'];
987                                } else {
988                                        @chmod(PLOGGER_DIR.$new_path, PLOGGER_CHMOD_DIR);
989                                        $output[] = sprintf(plog_tr('Moved file %s -> %s'), '<strong>'.$image['old_path'].'</strong>', '<strong>'.$image['new_path'].'</strong>');
990                                        $count++;
991                                }
992                        }
993                } else if (!empty($image['container'])) {
994                        // create the collection and album directory structure
995                        if (!makeDirs(PLOGGER_DIR.$image['new_path'].'/')) {
996                                $errors[] = plog_tr('Could not create directory').': '.PLOGGER_DIR.$image['new_path'];
997                        } else {
998                                $output[] = sprintf(plog_tr('Created directory %s'), '<strong>'.$image['new_path'].'</strong>');
999                                $count++;
1000                        }
1001                }
1002        }
1003
1004        return array('errors' => $errors, 'output' => $output, 'count' => $count);
1005}
1006
1007function check_list() {
1008        $themes = array();
1009        $translations = array();
1010
1011        // see if there are any old themes
1012        if (file_exists(PLOGGER_DIR.'themes/')) {
1013                $themes_old = get_files(PLOGGER_DIR.'themes/', true, false, dirname(dirname(dirname(__FILE__))).'/themes/');
1014                if (!empty($themes_old)) {
1015                        foreach ($themes_old as $theme) {
1016                                if (!empty($theme) && $theme != 'index.php') {
1017                                        $theme_parts = explode('/', $theme);
1018                                        $themes[] = $theme_parts[0].'/';
1019                                }
1020                        }
1021                        $themes = array_unique($themes);
1022                }
1023        }
1024
1025        // see if there are any old translations
1026        if (file_exists(PLOGGER_DIR.'plog-translations/')) {
1027                $translations_old = get_files(PLOGGER_DIR.'plog-translations/', true, false, dirname(dirname(dirname(__FILE__))).'/plog-translations/');
1028                if (!empty($translations_old)) {
1029                        foreach ($translations_old as $trans) {
1030                                if (!empty($trans)) {
1031                                        $translations[] = $trans;
1032                                }
1033                        }
1034                        $translations = array_unique($translations);
1035                }
1036        }
1037
1038        return array('themes' => $themes, 'translations' => $translations);
1039}
1040
1041function cleanup_list() {
1042        $files = array();
1043        $folders = array();
1044
1045        $file_list = array(
1046                '_install.php',
1047                '_upgrade.php',
1048                'plog-captcha.php',
1049                'plog-connect.php',
1050                'plog-functions.php',
1051                'plog-load_config.php',
1052                'plog-tag-functions.php',
1053                'set_session_var.php',
1054                'dynamics.js',
1055                'slideshow.js',
1056                'captcha.ttf',
1057                'plog-includes/plog-comment.php',
1058                'plog-includes/plog-tag-functions.php'
1059        );
1060        foreach ($file_list as $file) {
1061                if (file_exists(PLOGGER_DIR.$file)) {
1062                        $files[] = PLOGGER_DIR.$file;
1063                }
1064        }
1065
1066        $folder_list = array(
1067                'admin/',
1068                'css/',
1069                'graphics/',
1070                'images/',
1071                'lib/',
1072                'plog-translations/',
1073                'themes/',
1074                'thumbs/',
1075                'uploads/',
1076                'summary/',
1077                'plog-content/images-old/',
1078                'plog-content/thumbs-old/'
1079        );
1080        foreach ($folder_list as $folder) {
1081                if (file_exists(PLOGGER_DIR.$folder)) {
1082                        $folders[] = PLOGGER_DIR.$folder;
1083                }
1084        }
1085
1086        return array('files' => $files, 'folders' => $folders);
1087}
1088
1089function cleanup_files($files, $folders) {
1090        global $config;
1091        $output = array();
1092        $errors = array();
1093
1094        // delete the files first
1095        foreach ($files as $file) {
1096                if (file_exists($file)) {
1097                        if (kill_file($file)) {
1098                                $output[] = plog_tr('Plogger found and deleted the file').': '.$file;
1099                        } else {
1100                                $errors[] = plog_tr('Plogger could not delete the file').': '.$file;
1101                        }
1102                }
1103        }
1104
1105        // remove the folders since there should be no files in them
1106        foreach ($folders as $folder) {
1107                if (file_exists($folder)) {
1108                        if (kill_dir($folder)) {
1109                                $output[] = plog_tr('Plogger found and deleted the folder').': '.$folder;
1110                        } else {
1111                                $errors[] = plog_tr('Plogger could not delete the folder').': '.$folder;
1112                        }
1113                }
1114        }
1115
1116        return array('errors' => $errors, 'output' => $output);
1117}
1118
1119?>
Note: See TracBrowser for help on using the repository browser.