/**
  * Save/Load options and add a new hook for plugins
  * 
  * @return void
  */
 function processor()
 {
     global $ngg, $nggRewrite;
     $old_state = $ngg->options['usePermalinks'];
     $old_slug = $ngg->options['permalinkSlug'];
     if (isset($_POST['irDetect'])) {
         check_admin_referer('ngg_settings');
         $ngg->options['irURL'] = ngg_search_imagerotator();
         update_option('ngg_options', $ngg->options);
     }
     if (isset($_POST['updateoption'])) {
         check_admin_referer('ngg_settings');
         // get the hidden option fields, taken from WP core
         if ($_POST['page_options']) {
             $options = explode(',', stripslashes($_POST['page_options']));
         }
         if ($options) {
             foreach ($options as $option) {
                 $option = trim($option);
                 $value = isset($_POST[$option]) ? trim($_POST[$option]) : false;
                 //		$value = sanitize_option($option, $value); // This does stripslashes on those that need it
                 $ngg->options[$option] = $value;
             }
             // do not allow a empty string
             if (empty($ngg->options['permalinkSlug'])) {
                 $ngg->options['permalinkSlug'] = 'nggallery';
             }
             // the path should always end with a slash
             $ngg->options['gallerypath'] = trailingslashit($ngg->options['gallerypath']);
             $ngg->options['imageMagickDir'] = trailingslashit($ngg->options['imageMagickDir']);
             // the custom sortorder must be ascending
             $ngg->options['galSortDir'] = $ngg->options['galSort'] == 'sortorder' ? 'ASC' : $ngg->options['galSortDir'];
         }
         // Save options
         update_option('ngg_options', $ngg->options);
         // Flush Rewrite rules
         if ($old_state != $ngg->options['usePermalinks'] || $old_slug != $ngg->options['permalinkSlug']) {
             $nggRewrite->flush();
         }
         nggGallery::show_message(__('Update Successfully', 'nggallery'));
     }
     if (isset($_POST['clearcache'])) {
         check_admin_referer('ngg_settings');
         $path = WINABSPATH . $ngg->options['gallerypath'] . 'cache/';
         if (is_dir($path)) {
             if ($handle = opendir($path)) {
                 while (false !== ($file = readdir($handle))) {
                     if ($file != '.' && $file != '..') {
                         @unlink($path . '/' . $file);
                     }
                 }
                 closedir($handle);
             }
         }
         nggGallery::show_message(__('Cache cleared', 'nggallery'));
     }
     if (isset($_POST['createslugs'])) {
         check_admin_referer('ngg_settings');
         include_once dirname(__FILE__) . '/upgrade.php';
         ngg_rebuild_unique_slugs::start_rebuild();
     }
     do_action('ngg_update_options_page');
 }
/**
 * ngg_upgrade() - update routine for older version
 * 
 * @return Success message
 */
function ngg_upgrade()
{
    global $wpdb, $user_ID, $nggRewrite;
    // get the current user ID
    get_currentuserinfo();
    // in multisite environment the pointer $wpdb->nggpictures need to be set again
    $wpdb->nggpictures = $wpdb->prefix . 'ngg_pictures';
    $wpdb->nggallery = $wpdb->prefix . 'ngg_gallery';
    $wpdb->nggalbum = $wpdb->prefix . 'ngg_album';
    // Be sure that the tables exist, avoid case sensitive : http://dev.mysql.com/doc/refman/5.1/en/identifier-case-sensitivity.html
    if ($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->nggpictures}'")) {
        echo __('Upgrade database structure...', 'nggallery');
        $wpdb->show_errors();
        $installed_ver = get_option('ngg_db_version');
        // 0.9.7 is smaller that 0.97, my fault :-)
        if ($installed_ver == '0.9.7') {
            $installed_ver = '0.97';
        }
        // v0.33 -> v.071
        if (version_compare($installed_ver, '0.71', '<')) {
            $wpdb->query("ALTER TABLE {$wpdb->nggpictures} CHANGE pid pid BIGINT(20) NOT NULL AUTO_INCREMENT ");
            $wpdb->query("ALTER TABLE {$wpdb->nggpictures} CHANGE galleryid galleryid BIGINT(20) NOT NULL ");
            $wpdb->query("ALTER TABLE {$wpdb->nggallery} CHANGE gid gid BIGINT(20) NOT NULL AUTO_INCREMENT ");
            $wpdb->query("ALTER TABLE {$wpdb->nggallery} CHANGE pageid pageid BIGINT(20) NULL DEFAULT '0'");
            $wpdb->query("ALTER TABLE {$wpdb->nggallery} CHANGE previewpic previewpic BIGINT(20) NULL DEFAULT '0'");
            $wpdb->query("ALTER TABLE {$wpdb->nggallery} CHANGE gid gid BIGINT(20) NOT NULL AUTO_INCREMENT ");
            $wpdb->query("ALTER TABLE {$wpdb->nggallery} CHANGE description galdesc MEDIUMTEXT NULL");
        }
        // v0.71 -> v0.84
        if (version_compare($installed_ver, '0.84', '<')) {
            ngg_maybe_add_column($wpdb->nggpictures, 'sortorder', "BIGINT(20) DEFAULT '0' NOT NULL AFTER exclude");
        }
        // v0.84 -> v0.95
        if (version_compare($installed_ver, '0.95', '<')) {
            // first add the author field and set it to the current administrator
            ngg_maybe_add_column($wpdb->nggallery, 'author', "BIGINT(20) NOT NULL DEFAULT '{$user_ID}' AFTER previewpic");
            // switch back to zero
            $wpdb->query("ALTER TABLE {$wpdb->nggallery} CHANGE author author BIGINT(20) NOT NULL DEFAULT '0'");
        }
        // v0.95 -> v0.97
        if (version_compare($installed_ver, '0.96', '<')) {
            // Convert into WordPress Core taxonomy scheme
            ngg_convert_tags();
            // Drop tables, we don't need them anymore
            $wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . "ngg_tags");
            $wpdb->query("DROP TABLE IF EXISTS " . $wpdb->prefix . "ngg_pic2tags");
            // New capability for administrator role
            $role = get_role('administrator');
            $role->add_cap('NextGEN Manage tags');
            // Add new option
            $ngg_options = get_option('ngg_options');
            $ngg_options['graphicLibrary'] = 'gd';
            update_option('ngg_options', $ngg_options);
        }
        // v0.97 -> v1.00
        if (version_compare($installed_ver, '0.97', '<')) {
            ngg_maybe_add_column($wpdb->nggpictures, 'imagedate', "DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' AFTER alttext");
        }
        // v0.97 -> v1.3.0
        if (version_compare($installed_ver, '1.3.0', '<')) {
            ngg_maybe_add_column($wpdb->nggpictures, 'post_id', "BIGINT(20) DEFAULT '0' NOT NULL AFTER pid");
            ngg_maybe_add_column($wpdb->nggpictures, 'meta_data', "LONGTEXT AFTER sortorder");
            $wpdb->query("ALTER TABLE " . $wpdb->nggpictures . " ADD INDEX post_id ( post_id )");
        }
        // v1.3.0 -> v1.3.1
        if (version_compare($installed_ver, '1.3.1', '<')) {
            // add description and previewpic for the album itself
            ngg_maybe_add_column($wpdb->nggalbum, 'previewpic', "BIGINT(20) DEFAULT '0' NOT NULL AFTER name");
            ngg_maybe_add_column($wpdb->nggalbum, 'albumdesc', "MEDIUMTEXT NULL AFTER previewpic");
        }
        // v1.3.5 -> v1.4.0
        if (version_compare($installed_ver, '1.4.0', '<')) {
            // add link from album to a page
            ngg_maybe_add_column($wpdb->nggalbum, 'pageid', "BIGINT(20) DEFAULT '0' NOT NULL AFTER sortorder");
        }
        // v1.4.0 -> v1.7.0
        if (version_compare($installed_ver, '1.7.0', '<')) {
            // add slug fields
            ngg_maybe_add_column($wpdb->nggpictures, 'image_slug', "VARCHAR(255) NOT NULL AFTER pid");
            ngg_maybe_add_column($wpdb->nggalbum, 'slug', "VARCHAR(255) NOT NULL AFTER name");
            ngg_maybe_add_column($wpdb->nggallery, 'slug', "VARCHAR(255) NOT NULL AFTER name");
        }
        // update now the database
        update_option("ngg_db_version", NGG_DBVERSION);
        echo __('finished', 'nggallery') . "<br />\n";
        $wpdb->hide_errors();
        // *** From here we start file operation which could failed sometimes,
        // *** ensure that the DB changes are not performed two times...
        // Change all thumbnail folders to "thumbs"
        if (version_compare($installed_ver, '0.96', '<')) {
            echo __('Update file structure...', 'nggallery');
            ngg_convert_filestructure();
            echo __('finished', 'nggallery') . "<br />\n";
        }
        // On some reason the import / date sometimes failed, due to the memory limit
        if (version_compare($installed_ver, '0.97', '<')) {
            echo __('Import date and time information...', 'nggallery');
            ngg_import_date_time();
            echo __('finished', 'nggallery') . "<br />\n";
        }
        // Move imagerotator outside the plugin folder
        if (version_compare($installed_ver, '1.1.0', '<')) {
            $ngg_options = get_option('ngg_options');
            echo __('Move imagerotator to new location...', 'nggallery');
            $ngg_options['irURL'] = ngg_move_imagerotator();
            $ngg_options['galPagedGalleries'] = 0;
            $ngg_options['galColumns'] = 0;
            update_option('ngg_options', $ngg_options);
            echo __('finished', 'nggallery') . "<br />\n";
        }
        // Remove thumbcrop setting, thumbfix and quare size do the same
        if (version_compare($installed_ver, '1.4.0', '<')) {
            $ngg_options = get_option('ngg_options');
            echo __('Update settings...', 'nggallery');
            if ($ngg_options['thumpcrop']) {
                $ngg_options['thumbfix'] = true;
                $ngg_options['thumbheight'] = $ngg_options['thumbwidth'];
                $ngg_options['galAjaxNav'] = true;
            }
            $ngg_options['galHiddenImg'] = false;
            update_option('ngg_options', $ngg_options);
            echo __('finished', 'nggallery') . "<br />\n";
        }
        // Remove the old widget options
        if (version_compare($installed_ver, '1.4.4', '<')) {
            delete_option('ngg_widget');
            echo __('Updated widget structure. If you used NextGEN Widgets, you need to setup them again...', 'nggallery');
        }
        if (version_compare($installed_ver, '1.6.0', '<')) {
            $ngg_options = get_option('ngg_options');
            $ngg_options['enableIR'] = '1';
            $ngg_options['slideFx'] = 'fade';
            update_option('ngg_options', $ngg_options);
            echo __('Updated options.', 'nggallery');
        }
        if (version_compare($installed_ver, '1.7.0', '<')) {
            // Network blogs need to call this manually
            if (!is_multisite()) {
                ?>
               <h2><?php 
                _e('Create unique slug', 'nggallery');
                ?>
</h2>
        	   <p><?php 
                _e('One of the upcomming features are a reworked permalinks structure.', 'nggallery');
                ?>
        	   <?php 
                _e('Therefore it\'s needed to have a unique identifier for each image, gallery and album.', 'nggallery');
                ?>
<br />
               <?php 
                _e('Depend on the amount of database entries this will take a while, don\'t reload this page.', 'nggallery');
                ?>
</p>
               <?php 
                ngg_rebuild_unique_slugs::start_rebuild();
            }
        }
        if (version_compare($installed_ver, '1.8.0', '<')) {
            $ngg_options = get_option('ngg_options');
            // new permalink structure
            $ngg_options['permalinkSlug'] = 'nggallery';
            update_option('ngg_options', $ngg_options);
            echo __('Updated options.', 'nggallery');
        }
        // better to flush rewrite rules after upgrades
        $nggRewrite->flush();
        return;
    }
    echo __('Could not find NextGEN Gallery database tables, upgrade failed !', 'nggallery');
    return;
}