function wppa_session_start()
{
    global $wpdb;
    global $wppa_session;
    if (is_array($wppa_session)) {
        return true;
    }
    // To prevent dup opens
    // If the session table does not yet exist on activation
    if (!wppa_table_exists(WPPA_SESSION)) {
        $wppa_session['id'] = '0';
        return false;
    }
    // Cleanup first
    $lifetime = 3600;
    // Sessions expire after one hour
    $savetime = 3600;
    // Save session data for 1 hour // 30 days
    $expire = time() - $lifetime;
    $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_SESSION . "` SET `status` = 'expired' WHERE `timestamp` < %s", $expire));
    $purge = time() - $savetime;
    $wpdb->query($wpdb->prepare("DELETE FROM `" . WPPA_SESSION . "` WHERE `timestamp` < %s", $purge));
    // Is session already started?
    $session = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPPA_SESSION . "` WHERE `session` = %s AND `status` = 'valid' LIMIT 1", wppa_get_session_id()), ARRAY_A);
    $data = isset($session['data']) ? $session['data'] : false;
    if ($data === false) {
        $iret = false;
        $tries = '0';
        while (!$iret && $tries < '10') {
            $iret = wppa_create_session_entry(array());
            if (!$iret) {
                sleep(1);
                $tries++;
            }
        }
        if ($tries > '3' && $iret) {
            wppa_log('Debug', 'It took ' . $tries . ' retries to start session ' . $iret);
        }
        if (!$iret) {
            wppa_log('Error', 'Unable to create session.');
            return false;
        }
        $wppa_session['page'] = '1';
        $wppa_session['ajax'] = '0';
        $wppa_session['id'] = $wpdb->get_var($wpdb->prepare("SELECT `id` FROM `" . WPPA_SESSION . "` WHERE `session` = %s LIMIT 1", wppa_get_session_id()));
    } else {
        // Update counter
        $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_SESSION . "` SET `count` = %s WHERE `id` = %s", $session['count'] + '1', $session['id']));
        $wppa_session = unserialize($data);
        $wppa_session['page'] = isset($wppa_session['page']) ? $wppa_session['page'] + '1' : '1';
        $wppa_session['id'] = $session['id'];
    }
    return true;
}
function wppa_setup($force = false)
{
    global $silent;
    global $wpdb;
    global $wppa_revno;
    global $current_user;
    global $wppa_error;
    $old_rev = get_option('wppa_revision', '100');
    if ($old_rev == $wppa_revno && !$force) {
        return;
    }
    // Nothing to do here
    wppa_clear_cache(true);
    // Clear cache
    $wppa_error = false;
    // Init no error
    $create_albums = "CREATE TABLE " . WPPA_ALBUMS . " (\r\n\t\t\t\t\tid bigint(20) NOT NULL,\r\n\t\t\t\t\tname text NOT NULL,\r\n\t\t\t\t\tdescription text NOT NULL,\r\n\t\t\t\t\ta_order smallint(5) NOT NULL,\r\n\t\t\t\t\tmain_photo bigint(20) NOT NULL,\r\n\t\t\t\t\ta_parent bigint(20) NOT NULL,\r\n\t\t\t\t\tp_order_by smallint(5) NOT NULL,\r\n\t\t\t\t\tcover_linktype tinytext NOT NULL,\r\n\t\t\t\t\tcover_linkpage bigint(20) NOT NULL,\r\n\t\t\t\t\towner text NOT NULL,\r\n\t\t\t\t\ttimestamp tinytext NOT NULL,\r\n\t\t\t\t\tmodified tinytext NOT NULL,\r\n\t\t\t\t\tupload_limit tinytext NOT NULL,\r\n\t\t\t\t\talt_thumbsize tinytext NOT NULL,\r\n\t\t\t\t\tdefault_tags tinytext NOT NULL,\r\n\t\t\t\t\tcover_type tinytext NOT NULL,\r\n\t\t\t\t\tsuba_order_by tinytext NOT NULL,\r\n\t\t\t\t\tviews bigint(20) NOT NULL default '0',\r\n\t\t\t\t\tcats tinytext NOT NULL,\r\n\t\t\t\t\tscheduledtm tinytext NOT NULL,\r\n\t\t\t\t\tPRIMARY KEY  (id)\r\n\t\t\t\t\t) DEFAULT CHARACTER SET utf8;";
    $create_photos = "CREATE TABLE " . WPPA_PHOTOS . " (\r\n\t\t\t\t\tid bigint(20) NOT NULL,\r\n\t\t\t\t\talbum bigint(20) NOT NULL,\r\n\t\t\t\t\text tinytext NOT NULL,\r\n\t\t\t\t\tname text NOT NULL,\r\n\t\t\t\t\tdescription longtext NOT NULL,\r\n\t\t\t\t\tp_order smallint(5) NOT NULL,\r\n\t\t\t\t\tmean_rating tinytext NOT NULL,\r\n\t\t\t\t\tlinkurl text NOT NULL,\r\n\t\t\t\t\tlinktitle text NOT NULL,\r\n\t\t\t\t\tlinktarget tinytext NOT NULL,\r\n\t\t\t\t\towner text NOT NULL,\r\n\t\t\t\t\ttimestamp tinytext NOT NULL,\r\n\t\t\t\t\tstatus tinytext NOT NULL,\r\n\t\t\t\t\trating_count bigint(20) NOT NULL default '0',\r\n\t\t\t\t\ttags tinytext NOT NULL,\r\n\t\t\t\t\talt tinytext NOT NULL,\r\n\t\t\t\t\tfilename tinytext NOT NULL,\r\n\t\t\t\t\tmodified tinytext NOT NULL,\r\n\t\t\t\t\tlocation tinytext NOT NULL,\r\n\t\t\t\t\tviews bigint(20) NOT NULL default '0',\r\n\t\t\t\t\tpage_id bigint(20) NOT NULL default '0',\r\n\t\t\t\t\texifdtm tinytext NOT NULL,\r\n\t\t\t\t\tvideox smallint(5) NOT NULL default '0',\r\n\t\t\t\t\tvideoy smallint(5) NOT NULL default '0',\r\n\t\t\t\t\tthumbx smallint(5) NOT NULL default '0',\r\n\t\t\t\t\tthumby smallint(5) NOT NULL default '0',\r\n\t\t\t\t\tphotox smallint(5) NOT NULL default '0',\r\n\t\t\t\t\tphotoy smallint(5) NOT NULL default '0',\r\n\t\t\t\t\tscheduledtm tinytext NOT NULL,\r\n\t\t\t\t\tcustom longtext NOT NULL,\r\n\t\t\t\t\tstereo smallint NOT NULL default '0',\r\n\t\t\t\t\tPRIMARY KEY  (id),\r\n\t\t\t\t\tKEY albumkey (album)\r\n\t\t\t\t\t) DEFAULT CHARACTER SET utf8;";
    $create_rating = "CREATE TABLE " . WPPA_RATING . " (\r\n\t\t\t\t\tid bigint(20) NOT NULL,\r\n\t\t\t\t\ttimestamp tinytext NOT NULL,\r\n\t\t\t\t\tphoto bigint(20) NOT NULL,\r\n\t\t\t\t\tvalue smallint(5) NOT NULL,\r\n\t\t\t\t\tuser text NOT NULL,\r\n\t\t\t\t\tstatus tinytext NOT NULL,\r\n\t\t\t\t\tPRIMARY KEY  (id),\r\n\t\t\t\t\tKEY photokey (photo)\r\n\t\t\t\t\t) DEFAULT CHARACTER SET utf8;";
    $create_comments = "CREATE TABLE " . WPPA_COMMENTS . " (\r\n\t\t\t\t\tid bigint(20) NOT NULL,\r\n\t\t\t\t\ttimestamp tinytext NOT NULL,\r\n\t\t\t\t\tphoto bigint(20) NOT NULL,\r\n\t\t\t\t\tuser text NOT NULL,\r\n\t\t\t\t\tip tinytext NOT NULL,\r\n\t\t\t\t\temail text NOT NULL,\r\n\t\t\t\t\tcomment text NOT NULL,\r\n\t\t\t\t\tstatus tinytext NOT NULL,\r\n\t\t\t\t\tPRIMARY KEY  (id),\r\n\t\t\t\t\tKEY photokey (photo)\r\n\t\t\t\t\t) DEFAULT CHARACTER SET utf8;";
    $create_iptc = "CREATE TABLE " . WPPA_IPTC . " (\r\n\t\t\t\t\tid bigint(20) NOT NULL,\r\n\t\t\t\t\tphoto bigint(20) NOT NULL,\r\n\t\t\t\t\ttag tinytext NOT NULL,\r\n\t\t\t\t\tdescription text NOT NULL,\r\n\t\t\t\t\tstatus tinytext NOT NULL,\r\n\t\t\t\t\tPRIMARY KEY  (id),\r\n\t\t\t\t\tKEY photokey (photo)\r\n\t\t\t\t\t) DEFAULT CHARACTER SET utf8;";
    $create_exif = "CREATE TABLE " . WPPA_EXIF . " (\r\n\t\t\t\t\tid bigint(20) NOT NULL,\r\n\t\t\t\t\tphoto bigint(20) NOT NULL,\r\n\t\t\t\t\ttag tinytext NOT NULL,\r\n\t\t\t\t\tdescription text NOT NULL,\r\n\t\t\t\t\tstatus tinytext NOT NULL,\r\n\t\t\t\t\tPRIMARY KEY  (id),\r\n\t\t\t\t\tKEY photokey (photo)\r\n\t\t\t\t\t) DEFAULT CHARACTER SET utf8;";
    $create_index = "CREATE TABLE " . WPPA_INDEX . " (\r\n\t\t\t\t\tid bigint(20) NOT NULL,\r\n\t\t\t\t\tslug tinytext NOT NULL,\r\n\t\t\t\t\talbums text NOT NULL,\r\n\t\t\t\t\tphotos text NOT NULL,\r\n\t\t\t\t\tPRIMARY KEY  (id),\r\n\t\t\t\t\tKEY slugkey (slug(20))\r\n\t\t\t\t\t) DEFAULT CHARACTER SET utf8;";
    $create_session = "CREATE TABLE " . WPPA_SESSION . " (\r\n\t\t\t\t\tid bigint(20) NOT NULL,\r\n\t\t\t\t\tsession tinytext NOT NULL,\r\n\t\t\t\t\ttimestamp tinytext NOT NULL,\r\n\t\t\t\t\tuser tinytext NOT NULL,\r\n\t\t\t\t\tip tinytext NOT NULL,\r\n\t\t\t\t\tstatus tinytext NOT NULL,\r\n\t\t\t\t\tdata text NOT NULL,\r\n\t\t\t\t\tcount bigint(20) NOT NULL default '0',\r\n\t\t\t\t\tPRIMARY KEY  (id),\r\n\t\t\t\t\tKEY sessionkey (session(20))\r\n\t\t\t\t\t) DEFAULT CHARACTER SET utf8;";
    require_once WPPA_ABSPATH . 'wp-admin/includes/upgrade.php';
    // Create or update db tables
    $tn = array(WPPA_ALBUMS, WPPA_PHOTOS, WPPA_RATING, WPPA_COMMENTS, WPPA_IPTC, WPPA_EXIF, WPPA_INDEX, WPPA_SESSION);
    $tc = array($create_albums, $create_photos, $create_rating, $create_comments, $create_iptc, $create_exif, $create_index, $create_session);
    $idx = 0;
    while ($idx < 8) {
        $a0 = wppa_table_exists($tn[$idx]);
        dbDelta($tc[$idx]);
        $a1 = wppa_table_exists($tn[$idx]);
        if (WPPA_DEBUG) {
            if (!$a0) {
                if ($a1) {
                    wppa_ok_message('Database table ' . $tn[$idx] . ' created.');
                } else {
                    wppa_error_message('Could not create database table ' . $tn[$idx]);
                }
            } else {
                wppa_ok_message('Database table ' . $tn[$idx] . ' updated.');
            }
        }
        $idx++;
    }
    // Although we do not rely om auto increment, it may help avoiding concurrency conflicts
    $wpdb->query("ALTER TABLE `" . WPPA_SESSION . "` MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT");
    // Clear Session
    $wpdb->query("TRUNCATE TABLE `" . WPPA_SESSION . "`");
    wppa_session_start();
    // Convert any changed and remove obsolete setting options
    if ($old_rev > '100') {
        // On update only
        if ($old_rev <= '402') {
            wppa_convert_setting('wppa_coverphoto_left', 'no', 'wppa_coverphoto_pos', 'right');
            wppa_convert_setting('wppa_coverphoto_left', 'yes', 'wppa_coverphoto_pos', 'left');
        }
        if ($old_rev <= '440') {
            wppa_convert_setting('wppa_fadein_after_fadeout', 'yes', 'wppa_animation_type', 'fadeafter');
            wppa_convert_setting('wppa_fadein_after_fadeout', 'no', 'wppa_animation_type', 'fadeover');
        }
        if ($old_rev <= '450') {
            wppa_remove_setting('wppa_fadein_after_fadeout');
            wppa_copy_setting('wppa_show_bbb', 'wppa_show_bbb_widget');
            wppa_convert_setting('wppa_comment_use_gravatar', 'yes', 'wppa_comment_gravatar', 'mm');
            wppa_convert_setting('wppa_comment_use_gravatar', 'no', 'wppa_comment_gravatar', 'none');
            wppa_remove_setting('wppa_comment_use_gravatar');
            wppa_revalue_setting('wppa_start_slide', 'yes', 'run');
            wppa_revalue_setting('wppa_start_slide', 'no', 'still');
            wppa_rename_setting('wppa_accesslevel', 'wppa_accesslevel_admin');
            wppa_remove_setting('wppa_charset');
            wppa_remove_setting('wppa_chmod');
            wppa_remove_setting('wppa_coverphoto_left');
            wppa_remove_setting('wppa_2col_treshold');
            wppa_remove_setting('wppa_album_admin_autosave');
            wppa_remove_setting('wppa_doublethevotes');
            wppa_remove_setting('wppa_halvethevotes');
            wppa_remove_setting('wppa_lightbox_overlaycolor');
            wppa_remove_setting('wppa_lightbox_overlayopacity');
            wppa_remove_setting('wppa_multisite');
            wppa_remove_setting('wppa_set_access_by');
            wppa_remove_setting('wppa_accesslevel_admin');
            wppa_remove_setting('wppa_accesslevel_upload');
            wppa_remove_setting('wppa_accesslevel_sidebar');
        }
        if ($old_rev <= '452') {
            wppa_copy_setting('wppa_fontfamily_numbar', 'wppa_fontfamily_numbar_active');
            wppa_copy_setting('wppa_fontsize_numbar', 'wppa_fontsize_numbar_active');
            wppa_copy_setting('wppa_fontcolor_numbar', 'wppa_fontcolor_numbar_active');
            wppa_copy_setting('wppa_fontweight_numbar', 'wppa_fontweight_numbar_active');
        }
        if ($old_rev <= '455') {
            // rating_count added to WPPA_PHOTOS
            $phs = $wpdb->get_results('SELECT `id` FROM `' . WPPA_PHOTOS . '`', ARRAY_A);
            if ($phs) {
                foreach ($phs as $ph) {
                    $cnt = $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM `' . WPPA_RATING . '` WHERE `photo` = %s', $ph['id']));
                    $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_PHOTOS . '` SET `rating_count` = %s WHERE `id` = %s', $cnt, $ph['id']));
                }
            }
        }
        if ($old_rev < '470') {
            // single photo re-impl. has its own links, clone from slideshow
            wppa_copy_setting('wppa_slideshow_linktype', 'wppa_sphoto_linktype');
            wppa_copy_setting('wppa_slideshow_blank', 'wppa_sphoto_blank');
            wppa_copy_setting('wppa_slideshow_overrule', 'wppa_sphoto_overrule');
        }
        if ($old_rev <= '474') {
            // Convert album and photo descriptions to contain html in stead of htmlspecialchars. Allowing html is assumed, if not permitted, wppa_html will convert to specialcars.
            // html
            $at = 0;
            $ah = 0;
            $pt = 0;
            $ph = 0;
            $albs = $wpdb->get_results('SELECT `id`, `description` FROM ' . WPPA_ALBUMS, ARRAY_A);
            if ($albs) {
                foreach ($albs as $alb) {
                    $at++;
                    if (html_entity_decode($alb['description']) != $alb['description']) {
                        $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_ALBUMS . '` SET `description` = %s WHERE `id` = %s', html_entity_decode($alb['description']), $alb['id']));
                        $ah++;
                    }
                }
            }
            $phots = $wpdb->get_results('SELECT `id`, `description` FROM ' . WPPA_PHOTOS, ARRAY_A);
            if ($phots) {
                foreach ($phots as $phot) {
                    $pt++;
                    if (html_entity_decode($phot['description']) != $phot['description']) {
                        $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_PHOTOS . '` SET `description` = %s WHERE `id` = %s', html_entity_decode($phot['description']), $phot['id']));
                        $ph++;
                    }
                }
            }
            if (WPPA_DEBUG) {
                if ($ah || $ph) {
                    wppa_ok_message($ah . ' out of ' . $at . ' albums and ' . $ph . ' out of ' . $pt . ' photos html converted');
                }
            }
        }
        if ($old_rev <= '482') {
            // Share box added
            $so = get_option('wppa_slide_order', '0,1,2,3,4,5,6,7,8,9');
            if (strlen($so) == '19') {
                wppa_update_option('wppa_slide_order', $so . ',10');
            }
            $so = get_option('wppa_slide_order_split', '0,1,2,3,4,5,6,7,8,9,10');
            if (strlen($so) == '22') {
                wppa_update_option('wppa_slide_order_split', $so . ',11');
            }
            wppa_remove_setting('wppa_sharetype');
            wppa_copy_setting('wppa_bgcolor_namedesc', 'wppa_bgcolor_share');
            wppa_copy_setting('wppa_bcolor_namedesc', 'wppa_bcolor_share');
        }
        if ($old_rev <= '4811') {
            wppa_rename_setting('wppa_comment_count', 'wppa_comten_count');
            wppa_rename_setting('wppa_comment_size', 'wppa_comten_size');
        }
        if ($old_rev <= '4910') {
            wppa_copy_setting('wppa_show_bread', 'wppa_show_bread_posts');
            wppa_copy_setting('wppa_show_bread', 'wppa_show_bread_pages');
            wppa_remove_setting('wppa_show_bread');
        }
        if ($old_rev <= '5000') {
            wppa_remove_setting('wppa_autoclean');
        }
        if ($old_rev <= '5010') {
            wppa_copy_setting('wppa_apply_newphoto_desc', 'wppa_apply_newphoto_desc_user');
        }
        if ($old_rev <= '5107') {
            delete_option('wppa_taglist');
            // Forces recreation
        }
        if ($old_rev <= '5205') {
            if (get_option('wppa_list_albums_desc', 'nil') == 'yes') {
                $value = get_option('wppa_list_albums_by', '0') * '-1';
                wppa_update_option('wppa_list_albums_by', $value);
                wppa_remove_setting('wppa_list_albums_desc');
            }
            if (get_option('wppa_list_photos_desc', 'nil') == 'yes') {
                $value = get_option('wppa_list_photos_by', '0') * '-1';
                wppa_update_option('wppa_list_photos_by', $value);
                wppa_remove_setting('wppa_list_photos_desc');
            }
        }
        if ($old_rev <= '5207') {
            if (get_option('wppa_strip_file_ext', 'nil') == 'yes') {
                wppa_update_option('wppa_newphoto_name_method', 'noext');
                delete_option('wppa_strip_file_ext');
            }
        }
        if ($old_rev <= '5307') {
            $wpdb->query("TRUNCATE TABLE `" . WPPA_SESSION . "`");
        }
        if ($old_rev <= '5308') {
            wppa_flush_treecounts();
        }
        if ($old_rev <= '5410') {
            wppa_copy_setting('wppa_widget_width', 'wppa_potd_widget_width');
            wppa_flush_upldr_cache('all');
            // New format
        }
        if ($old_rev == '5421' || $old_rev == '5420.99') {
            // The rev where the bug was
            if ($wppa_revno >= '5422') {
                // The rev where we fix it
                if (get_option('wppa_rating_on', 'no') == 'yes') {
                    // Only if rating used
                    if (get_option('wppa_ajax_non_admin', 'yes') == 'no') {
                        // Only if backend ajax
                        update_option('wppa_rerate_status', __('Required', 'wp-photo-album-plus'));
                        // Make sure they see the message
                    }
                }
            }
        }
        if ($old_rev <= '5500') {
            wppa_create_pl_htaccess(get_option('wppa_pl_dirname', 'wppa-pl'));
            // Remake due to fix in wppa_sanitize_file_name()
        }
        if ($old_rev <= '6103') {
            wppa_copy_setting('wppa_owner_only', 'wppa_upload_owner_only');
        }
        if ($old_rev <= '6305') {
            if (get_option('wppa_comment_captcha') == 'no') {
                update_option('wppa_comment_captcha', 'none');
            }
            if (get_option('wppa_comment_captcha') == 'yes') {
                update_option('wppa_comment_captcha', 'all');
            }
        }
        if ($old_rev <= '6310') {
            $wpdb->query("UPDATE `" . WPPA_PHOTOS . "` SET `timestamp` = '0' WHERE `timestamp` = ''");
            $wpdb->query("UPDATE `" . WPPA_PHOTOS . "` SET `modified` = `timestamp` WHERE `modified` = '' OR `modified` = '0'");
        }
        if ($old_rev <= '6312') {
            $wpdb->query("UPDATE `" . WPPA_ALBUMS . "` SET `timestamp` = '0' WHERE `timestamp` = ''");
            $wpdb->query("UPDATE `" . WPPA_ALBUMS . "` SET `modified` = `timestamp` WHERE `modified` = '' OR `modified` = '0'");
            wppa_copy_setting('wppa_wppa_set_shortcodes', 'wppa_set_shortcodes');
            wppa_remove_setting('wppa_wppa_set_shortcodes');
            wppa_copy_setting('wppa_max_album_newtime', 'wppa_max_album_modtime');
            wppa_copy_setting('wppa_max_photo_newtime', 'wppa_max_photo_modtime');
        }
        if ($old_rev <= '6316') {
            wppa_remove_setting('wppa_start_symbol_url');
            wppa_remove_setting('wppa_pause_symbol_url');
            wppa_remove_setting('wppa_stop_symbol_url');
        }
    }
    // Set Defaults
    wppa_set_defaults();
    // Check required directories
    if (!wppa_check_dirs()) {
        $wppa_error = true;
    }
    // Create .htaccess file in .../wp-content/uploads/wppa
    wppa_create_wppa_htaccess();
    // Copy factory supplied watermarks
    $frompath = WPPA_PATH . '/watermarks';
    $watermarks = glob($frompath . '/*.png');
    if (is_array($watermarks)) {
        foreach ($watermarks as $fromfile) {
            $tofile = WPPA_UPLOAD_PATH . '/watermarks/' . basename($fromfile);
            @copy($fromfile, $tofile);
        }
    }
    // Copy factory supplied watermark fonts
    $frompath = WPPA_PATH . '/fonts';
    $fonts = glob($frompath . '/*');
    if (is_array($fonts)) {
        foreach ($fonts as $fromfile) {
            if (is_file($fromfile)) {
                $tofile = WPPA_UPLOAD_PATH . '/fonts/' . basename($fromfile);
                @copy($fromfile, $tofile);
            }
        }
    }
    // Copy audiostub.jpg, the default audiostub
    $fromfile = WPPA_PATH . '/images/audiostub.jpg';
    $tofile = WPPA_UPLOAD_PATH . '/audiostub';
    if (!is_file($tofile . '.jpg') && !is_file($tofile . '.gif') && !is_file($tofile . '.png')) {
        @copy($fromfile, $tofile . '.jpg');
        wppa_update_option('wppa_audiostub', 'audiostub.jpg');
    }
    // Check if this update comes with a new wppa-theme.php and/or a new wppa-style.css
    // If so, produce message
    $key = '0';
    if ($old_rev < '5400') {
        // theme changed since...
        $usertheme = get_theme_root() . '/' . get_option('template') . '/wppa-theme.php';
        if (is_file($usertheme)) {
            $key += '2';
        }
    }
    if ($old_rev < '5211') {
        // css changed since...
        $userstyle = get_theme_root() . '/' . get_option('stylesheet') . '/wppa-style.css';
        if (is_file($userstyle)) {
            $key += '1';
        } else {
            $userstyle = get_theme_root() . '/' . get_option('template') . '/wppa-style.css';
            if (is_file($userstyle)) {
                $key += '1';
            }
        }
    }
    if ($key) {
        $msg = '<center>' . __('IMPORTANT UPGRADE NOTICE', 'wp-photo-album-plus') . '</center><br/>';
        if ($key == '1' || $key == '3') {
            $msg .= '<br/>' . __('Please CHECK your customized WPPA-STYLE.CSS file against the newly supplied one. You may wish to add or modify some attributes. Be aware of the fact that most settings can now be set in the admin settings page.', 'wp-photo-album-plus');
        }
        if ($key == '2' || $key == '3') {
            $msg .= '<br/>' . __('Please REPLACE your customized WPPA-THEME.PHP file by the newly supplied one, or just remove it from your theme directory. You may modify it later if you wish. Your current customized version is NOT compatible with this version of the plugin software.', 'wp-photo-album-plus');
        }
        wppa_ok_message($msg);
    }
    // Check if db is ok
    if (!wppa_check_database()) {
        $wppa_error = true;
    }
    // Remove dynamic files
    $files = glob(WPPA_PATH . '/wppa-init.*.js');
    if ($files) {
        foreach ($files as $file) {
            @unlink($file);
            // Will be auto re-created
        }
    }
    @unlink(WPPA_PATH . '/wppa-dynamic.css');
    // Will be auto re-created
    // Done!
    if (!$wppa_error) {
        $old_rev = round($old_rev);
        // might be 0.01 off
        if ($old_rev < $wppa_revno) {
            // was a real upgrade,
            wppa_update_option('wppa_prevrev', $old_rev);
            // Remember prev rev. For support purposes. They say they stay up to rev, but they come from stoneage...
        }
        wppa_update_option('wppa_revision', $wppa_revno);
        if (WPPA_DEBUG) {
            if (is_multisite()) {
                wppa_ok_message(sprintf(__('WPPA+ successfully updated in multi site mode to db version %s.', 'wp-photo-album-plus'), $wppa_revno));
            } else {
                wppa_ok_message(sprintf(__('WPPA+ successfully updated in single site mode to db version %s.', 'wp-photo-album-plus'), $wppa_revno));
            }
        }
    } else {
        if (WPPA_DEBUG) {
            wppa_error_message(__('An error occurred during update', 'wp-photo-album-plus'));
        }
    }
}
function wppa_check_database($verbose = false)
{
    global $wpdb;
    $any_error = false;
    // Check db tables
    // This is to test if dbdelta did his job in adding tables and columns
    $tn = array(WPPA_ALBUMS, WPPA_PHOTOS, WPPA_RATING, WPPA_COMMENTS, WPPA_IPTC, WPPA_EXIF, WPPA_INDEX);
    $flds = array(WPPA_ALBUMS => array('id' => 'bigint( 20 ) NOT NULL', 'name' => 'text NOT NULL', 'description' => 'text NOT NULL', 'a_order' => 'smallint( 5 ) unsigned NOT NULL', 'main_photo' => 'bigint( 20 ) NOT NULL', 'a_parent' => 'bigint( 20 ) NOT NULL', 'p_order_by' => 'int unsigned NOT NULL', 'cover_linktype' => 'tinytext NOT NULL', 'cover_linkpage' => 'bigint( 20 ) NOT NULL', 'owner' => 'text NOT NULL', 'timestamp' => 'tinytext NOT NULL', 'upload_limit' => 'tinytext NOT NULL', 'alt_thumbsize' => 'tinytext NOT NULL', 'default_tags' => 'tinytext NOT NULL', 'cover_type' => 'tinytext NOT NULL', 'suba_order_by' => 'tinytext NOT NULL'), WPPA_PHOTOS => array('id' => 'bigint( 20 ) NOT NULL', 'album' => 'bigint( 20 ) NOT NULL', 'ext' => 'tinytext NOT NULL', 'name' => 'text NOT NULL', 'description' => 'longtext NOT NULL', 'p_order' => 'smallint( 5 ) unsigned NOT NULL', 'mean_rating' => 'tinytext NOT NULL', 'linkurl' => 'text NOT NULL', 'linktitle' => 'text NOT NULL', 'linktarget' => 'tinytext NOT NULL', 'owner' => 'text NOT NULL', 'timestamp' => 'tinytext NOT NULL', 'status' => 'tinytext NOT NULL', 'rating_count' => "bigint( 20 ) default '0'", 'tags' => 'tinytext NOT NULL', 'alt' => 'tinytext NOT NULL', 'filename' => 'tinytext NOT NULL', 'modified' => 'tinytext NOT NULL', 'location' => 'tinytext NOT NULL'), WPPA_RATING => array('id' => 'bigint( 20 ) NOT NULL', 'photo' => 'bigint( 20 ) NOT NULL', 'value' => 'smallint( 5 ) NOT NULL', 'user' => 'text NOT NULL'), WPPA_COMMENTS => array('id' => 'bigint( 20 ) NOT NULL', 'timestamp' => 'tinytext NOT NULL', 'photo' => 'bigint( 20 ) NOT NULL', 'user' => 'text NOT NULL', 'ip' => 'tinytext NOT NULL', 'email' => 'text NOT NULL', 'comment' => 'text NOT NULL', 'status' => 'tinytext NOT NULL'), WPPA_IPTC => array('id' => 'bigint( 20 ) NOT NULL', 'photo' => 'bigint( 20 ) NOT NULL', 'tag' => 'tinytext NOT NULL', 'description' => 'text NOT NULL', 'status' => 'tinytext NOT NULL'), WPPA_EXIF => array('id' => 'bigint( 20 ) NOT NULL', 'photo' => 'bigint( 20 ) NOT NULL', 'tag' => 'tinytext NOT NULL', 'description' => 'text NOT NULL', 'status' => 'tinytext NOT NULL'), WPPA_INDEX => array('id' => 'bigint( 20 ) NOT NULL', 'slug' => 'tinytext NOT NULL', 'albums' => 'text NOT NULL', 'photos' => 'text NOT NULL'));
    $errtxt = '';
    $idx = 0;
    while ($idx < 7) {
        // Test existence of table
        $ext = wppa_table_exists($tn[$idx]);
        if (!$ext) {
            if ($verbose) {
                wppa_error_message(__('Unexpected error:', 'wp-photo-album-plus') . ' ' . __('Missing database table:', 'wp-photo-album-plus') . ' ' . $tn[$idx], 'red', 'force');
            }
            $any_error = true;
        } else {
            $tablefields = $wpdb->get_results("DESCRIBE {$tn[$idx]};", "ARRAY_A");
            // unset flags for found fields
            foreach ($tablefields as $field) {
                if (isset($flds[$tn[$idx]][$field['Field']])) {
                    unset($flds[$tn[$idx]][$field['Field']]);
                }
            }
            // Fields left?
            if (is_array($flds[$tn[$idx]])) {
                foreach (array_keys($flds[$tn[$idx]]) as $field) {
                    $errtxt .= '<tr><td>' . $tn[$idx] . '</td><td>' . $field . '</td><td>' . $flds[$tn[$idx]][$field] . '</td></tr>';
                }
            }
        }
        $idx++;
    }
    if ($errtxt) {
        $fulltxt = 'The latest update failed to update the database tables required for wppa+ to function properly<br /><br />';
        $fulltxt .= 'Make sure you have the rights to issue SQL commands like <i>"ALTER TABLE tablename ADD COLUMN columname datatype"</i> and run the action on <i>Table VIII-A1</i> on the Photo Albums -> Settings admin page.<br /><br />';
        $fulltxt .= 'The following table lists the missing columns:';
        $fulltxt .= '<br /><table id="wppa-err-table"><thead style="font-weight:bold;"><tr><td>Table name</td><td>Column name</td><td>Data type</td></thead>';
        $fulltxt .= $errtxt;
        $fulltxt .= '</table><b>';
        if ($verbose) {
            wppa_error_message($fulltxt, 'red', 'force');
        }
        $any_error = true;
    }
    // Check directories
    $dn = array(dirname(WPPA_DEPOT_PATH), WPPA_UPLOAD_PATH, WPPA_UPLOAD_PATH . '/thumbs', WPPA_UPLOAD_PATH . '/temp', WPPA_UPLOAD_PATH . '/fonts', WPPA_DEPOT_PATH);
    $idx = 0;
    while ($idx < 6) {
        if (!file_exists($dn[$idx])) {
            // First try to repair
            @wppa_mktree($dn[$idx]);
        } else {
            @chmod($dn[$idx], 0755);
            // there are always people who destruct things
        }
        if (!file_exists($dn[$idx])) {
            // Test again
            if ($verbose) {
                wppa_error_message(__('Unexpected error:', 'wp-photo-album-plus') . ' ' . __('Missing directory:', 'wp-photo-album-plus') . ' ' . $dn[$idx], 'red', 'force');
            }
            $any_error = true;
        } elseif (!is_writable($dn[$idx])) {
            if ($verbose) {
                wppa_error_message(__('Unexpected error:', 'wp-photo-album-plus') . ' ' . __('Directory is not writable:', 'wp-photo-album-plus') . ' ' . $dn[$idx], 'red', 'force');
            }
            $any_error = true;
        } elseif (!is_readable($dn[$idx])) {
            if ($verbose) {
                wppa_error_message(__('Unexpected error:', 'wp-photo-album-plus') . ' ' . __('Directory is not readable:', 'wp-photo-album-plus') . ' ' . $dn[$idx], 'red', 'force');
            }
            $any_error = true;
        }
        $idx++;
    }
    if ($any_error) {
        if ($verbose) {
            wppa_error_message(__('Please de-activate and re-activate the plugin. If this problem persists, ask your administrator.', 'wp-photo-album-plus'), 'red', 'force');
        }
    }
    return !$any_error;
    // True = no error
}
function wppa_session_start()
{
    global $wpdb;
    global $wppa_session;
    // If the session table does not yet exist on activation
    if (!wppa_table_exists(WPPA_SESSION)) {
        $wppa_session['id'] = '0';
        return false;
    }
    $lifetime = 3600;
    // Sessions expire after one hour
    $expire = time() - $lifetime;
    // Is session already started?
    $session = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPPA_SESSION . "` WHERE `session` = %s AND `status` = 'valid' LIMIT 1", wppa_get_session_id()), ARRAY_A);
    // Started but expired?
    if ($session) {
        if ($session['timestamp'] < $expire) {
            $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_SESSION . "` SET `status` = 'expired' WHERE `id` = %s", $session['id']));
            $session = false;
        }
    }
    // Get data if valid session exists
    $data = $session ? $session['data'] : false;
    // No valid session exists, start new
    if ($data === false) {
        $iret = wppa_create_session_entry(array());
        if (!$iret) {
            // Failed, retry after 1 sec.
            sleep(1);
            $iret = wppa_create_session_entry(array());
            if (!$iret) {
                wppa_log('Err', 'Unable to create session for user ' . wppa_get_user());
                // Give up
                return false;
            } else {
                wppa_log('Obs', 'Session ' . $iret . ' created after 1 retry for user ' . wppa_get_user());
            }
        }
        $wppa_session = array();
        $wppa_session['page'] = '0';
        $wppa_session['ajax'] = '0';
        $wppa_session['id'] = $iret;
        $wppa_session['user'] = wppa_get_user();
    } else {
        $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_SESSION . "` SET `count` = %s WHERE `id` = %s", $session['count'] + '1', $session['id']));
        $data_arr = unserialize($data);
        if (is_array($data_arr)) {
            $wppa_session = $data_arr;
        } else {
            $wppa_session = array();
        }
    }
    // Get info for root and sub search
    if (isset($_REQUEST['wppa-search-submit'])) {
        $wppa_session['rootbox'] = wppa_get_get('rootsearch') || wppa_get_post('rootsearch');
        $wppa_session['subbox'] = wppa_get_get('subsearch') || wppa_get_post('subsearch');
        if ($wppa_session['subbox']) {
            if (isset($wppa_session['use_searchstring'])) {
                $t = explode(',', $wppa_session['use_searchstring']);
                foreach (array_keys($t) as $idx) {
                    $t[$idx] .= ' ' . wppa_test_for_search('at_session_start');
                    $t[$idx] = trim($t[$idx]);
                    $v = explode(' ', $t[$idx]);
                    $t[$idx] = implode(' ', array_unique($v));
                }
                $wppa_session['use_searchstring'] = ' ' . implode(',', array_unique($t));
            } else {
                $wppa_session['use_searchstring'] = wppa_test_for_search('at_session_start');
            }
        } else {
            $wppa_session['use_searchstring'] = wppa_test_for_search('at_session_start');
        }
        if (isset($wppa_session['use_searchstring'])) {
            $wppa_session['use_searchstring'] = trim($wppa_session['use_searchstring'], ' ,');
            $wppa_session['display_searchstring'] = str_replace(',', ' &#8746 ', str_replace(' ', ' &#8745 ', $wppa_session['use_searchstring']));
        }
    }
    // Add missing defaults
    $defaults = array('has_searchbox' => false, 'rootbox' => false, 'search_root' => '', 'subbox' => false, 'use_searchstring' => '', 'display_searchstring' => '', 'supersearch' => '', 'superview' => 'thumbs', 'superalbum' => '0', 'page' => '0', 'ajax' => '0', 'user' => '', 'id' => '0', 'uris' => array(), 'isrobot' => false);
    $wppa_session = wp_parse_args($wppa_session, $defaults);
    ksort($wppa_session);
    $wppa_session['page']++;
    if (isset($_SERVER['REQUEST_URI'])) {
        $wppa_session['uris'][] = date_i18n("g:i") . ' ' . $_SERVER['REQUEST_URI'];
        if (stripos($_SERVER['REQUEST_URI'], '/robots.txt') !== false) {
            $wppa_session['isrobot'] = true;
        }
    }
    wppa_save_session();
    return true;
}
function wppa_session_start()
{
    global $wpdb;
    global $wppa_session;
    // If the session table does not yet exist on activation
    if (!wppa_table_exists(WPPA_SESSION)) {
        $wppa_session['id'] = '0';
        return false;
    }
    // Cleanup first
    $lifetime = 3600;
    // Sessions expire after one hour
    $savetime = 3600;
    // Save session data for 1 hour
    $expire = time() - $lifetime;
    $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_SESSION . "` SET `status` = 'expired' WHERE `timestamp` < %s", $expire));
    $purge = time() - $savetime;
    $wpdb->query($wpdb->prepare("DELETE FROM `" . WPPA_SESSION . "` WHERE `timestamp` < %s", $purge));
    // Is session already started?
    $session = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPPA_SESSION . "` WHERE `session` = %s AND `status` = 'valid' LIMIT 1", wppa_get_session_id()), ARRAY_A);
    $data = $session ? $session['data'] : false;
    // Not started yet, setup session
    if ($data === false) {
        $iret = false;
        $tries = '0';
        while (!$iret && $tries < '10') {
            $iret = wppa_create_session_entry(array());
            if (!$iret) {
                sleep(1);
                $tries++;
            }
        }
        if ($tries > '3' && $iret) {
            wppa_log('Debug', 'It took ' . $tries . ' retries to start session ' . $iret);
        }
        if (!$iret) {
            wppa_log('Error', 'Unable to create session.');
            return false;
        }
        $wppa_session = array();
        $wppa_session['page'] = '0';
        $wppa_session['ajax'] = '0';
        $wppa_session['id'] = $wpdb->get_var($wpdb->prepare("SELECT `id` FROM `" . WPPA_SESSION . "` WHERE `session` = %s LIMIT 1", wppa_get_session_id()));
        $wppa_session['user'] = wppa_get_user();
    } else {
        $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_SESSION . "` SET `count` = %s WHERE `id` = %s", $session['count'] + '1', $session['id']));
        $data_arr = unserialize($data);
        if (is_array($data_arr)) {
            $wppa_session = $data_arr;
        } else {
            $wppa_session = array();
        }
    }
    // Get info for root and sub search
    if (isset($_REQUEST['wppa-search-submit'])) {
        $wppa_session['rootbox'] = wppa_get_get('rootsearch') || wppa_get_post('rootsearch');
        $wppa_session['subbox'] = wppa_get_get('subsearch') || wppa_get_post('subsearch');
        if ($wppa_session['subbox']) {
            if (isset($wppa_session['use_searchstring'])) {
                $t = explode(',', $wppa_session['use_searchstring']);
                foreach (array_keys($t) as $idx) {
                    $t[$idx] .= ' ' . wppa_test_for_search('at_session_start');
                    $t[$idx] = trim($t[$idx]);
                    $v = explode(' ', $t[$idx]);
                    $t[$idx] = implode(' ', array_unique($v));
                }
                $wppa_session['use_searchstring'] = ' ' . implode(',', array_unique($t));
            } else {
                $wppa_session['use_searchstring'] = wppa_test_for_search('at_session_start');
            }
        } else {
            $wppa_session['use_searchstring'] = wppa_test_for_search('at_session_start');
        }
        if (isset($wppa_session['use_searchstring'])) {
            $wppa_session['use_searchstring'] = trim($wppa_session['use_searchstring'], ' ,');
            $wppa_session['display_searchstring'] = str_replace(',', ' &#8746 ', str_replace(' ', ' &#8745 ', $wppa_session['use_searchstring']));
        }
    }
    // Add missing defaults
    $defaults = array('has_searchbox' => false, 'rootbox' => false, 'search_root' => '', 'subbox' => false, 'use_searchstring' => '', 'display_searchstring' => '', 'supersearch' => '', 'superview' => 'thumbs', 'superalbum' => '0', 'page' => '0', 'ajax' => '0', 'user' => '', 'id' => $wpdb->get_var($wpdb->prepare("SELECT `id` FROM `" . WPPA_SESSION . "` WHERE `session` = %s LIMIT 1", wppa_get_session_id())));
    $wppa_session = wp_parse_args($wppa_session, $defaults);
    ksort($wppa_session);
    $wppa_session['page']++;
    wppa_save_session();
    return true;
}