function wppa_encrypt_album($album)
{
    // Feature enabled?
    if (!wppa_switch('use_encrypted_links')) {
        return $album;
    }
    // Encrypted album enumeration must always be expanded
    $album = wppa_expand_enum($album);
    // Decompose possible album enumeration
    $album_ids = strpos($album, '.') === false ? array($album) : explode('.', $album);
    $album_crypts = array();
    $i = 0;
    // Process all tokens
    while ($i < count($album_ids)) {
        $id = $album_ids[$i];
        // Check for existance of album, otherwise return dummy
        if (wppa_is_int($id) && $id > '0' && !wppa_album_exists($id)) {
            $id = '999999';
        }
        switch ($id) {
            case '-3':
                $crypt = get_option('wppa_album_crypt_3', false);
                break;
            case '-2':
                $crypt = get_option('wppa_album_crypt_2', false);
                break;
            case '-1':
                $crypt = get_option('wppa_album_crypt_1', false);
                break;
            case '':
            case '0':
                $crypt = get_option('wppa_album_crypt_0', false);
                break;
            case '999999':
                $crypt = get_option('wppa_album_crypt_9', false);
                break;
            default:
                if (strlen($id) < 12) {
                    $crypt = wppa_get_album_item($id, 'crypt');
                } else {
                    $crypt = $id;
                    // Already encrypted
                }
        }
        $album_crypts[$i] = $crypt;
        $i++;
    }
    // Compose result
    $result = implode('.', $album_crypts);
    return $result;
}
 /** @see WP_Widget::widget */
 function widget($args, $instance)
 {
     global $wpdb;
     global $wppa_opt;
     require_once dirname(__FILE__) . '/wppa-links.php';
     require_once dirname(__FILE__) . '/wppa-styles.php';
     require_once dirname(__FILE__) . '/wppa-functions.php';
     require_once dirname(__FILE__) . '/wppa-thumbnails.php';
     require_once dirname(__FILE__) . '/wppa-boxes-html.php';
     require_once dirname(__FILE__) . '/wppa-slideshow.php';
     wppa_initialize_runtime();
     wppa('in_widget', 'lasten');
     wppa_bump_mocc();
     extract($args);
     $instance = wp_parse_args((array) $instance, array('title' => '', 'album' => '', 'albumenum' => '', 'timesince' => 'yes', 'display' => 'thumbs', 'includesubs' => 'no'));
     $widget_title = apply_filters('widget_title', $instance['title']);
     $page = in_array(wppa_opt('lasten_widget_linktype'), wppa('links_no_page')) ? '' : wppa_get_the_landing_page('lasten_widget_linkpage', __('Last Ten Uploaded Photos', 'wp-photo-album-plus'));
     $max = wppa_opt('lasten_count');
     $album = $instance['album'];
     $timesince = $instance['timesince'];
     $display = $instance['display'];
     $albumenum = $instance['albumenum'];
     $subs = $instance['includesubs'] == 'yes';
     switch ($album) {
         case '-99':
             // 'Multiple see below' is a list of id, seperated by comma's
             $album = str_replace(',', '.', $albumenum);
             if ($subs) {
                 $album = wppa_expand_enum(wppa_alb_to_enum_children($album));
             }
             $album = str_replace('.', ',', $album);
             break;
         case '0':
             // ---all---
             break;
         case '-2':
             // ---generic---
             $albs = $wpdb->get_results("SELECT `id` FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = '0'", ARRAY_A);
             $album = '';
             foreach ($albs as $alb) {
                 $album .= '.' . $alb['id'];
             }
             $album = ltrim($album, '.');
             if ($subs) {
                 $album = wppa_expand_enum(wppa_alb_to_enum_children($album));
             }
             $album = str_replace('.', ',', $album);
             break;
         default:
             if ($subs) {
                 $album = wppa_expand_enum(wppa_alb_to_enum_children($album));
                 $album = str_replace('.', ',', $album);
             }
             break;
     }
     $album = trim($album, ',');
     // Eiter look at timestamp or at date/time modified
     $order_by = wppa_switch('lasten_use_modified') ? 'modified' : 'timestamp';
     // If you want only 'New' photos in the selection, the period must be <> 0;
     if (wppa_switch('lasten_limit_new') && wppa_opt('max_photo_newtime')) {
         $newtime = " `" . $order_by . "` >= " . (time() - wppa_opt('max_photo_newtime'));
         if ($album) {
             $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE (" . $newtime . ") AND `album` IN ( " . $album . " ) AND ( `status` <> 'pending' AND `status` <> 'scheduled' ) ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
         } else {
             $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE (" . $newtime . ") AND `status` <> 'pending' AND `status` <> 'scheduled' ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
         }
     } else {
         if ($album) {
             $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` IN ( " . $album . " ) AND ( `status` <> 'pending' AND `status` <> 'scheduled' ) ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
         } else {
             $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `status` <> 'pending' AND `status` <> 'scheduled' ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
         }
     }
     $thumbs = $wpdb->get_results($q, ARRAY_A);
     $widget_content = "\n" . '<!-- WPPA+ LasTen Widget start -->';
     $maxw = wppa_opt('lasten_size');
     $maxh = $maxw;
     $lineheight = wppa_opt('fontsize_widget_thumb') * 1.5;
     $maxh += $lineheight;
     if ($timesince == 'yes') {
         $maxh += $lineheight;
     }
     $count = '0';
     if ($thumbs) {
         foreach ($thumbs as $image) {
             $thumb = $image;
             // Make the HTML for current picture
             if ($display == 'thumbs') {
                 $widget_content .= "\n" . '<div class="wppa-widget" style="width:' . $maxw . 'px; height:' . $maxh . 'px; margin:4px; display:inline; text-align:center; float:left;">';
             } else {
                 $widget_content .= "\n" . '<div class="wppa-widget" >';
             }
             if ($image) {
                 $no_album = !$album;
                 if ($no_album) {
                     $tit = __('View the most recent uploaded photos', 'wp-photo-album-plus');
                 } else {
                     $tit = esc_attr(__(stripslashes($image['description'])));
                 }
                 $link = wppa_get_imglnk_a('lasten', $image['id'], '', $tit, '', $no_album, str_replace(',', '.', $album));
                 $file = wppa_get_thumb_path($image['id']);
                 $imgstyle_a = wppa_get_imgstyle_a($image['id'], $file, $maxw, 'center', 'ltthumb');
                 $imgurl = wppa_get_thumb_url($image['id'], '', $imgstyle_a['width'], $imgstyle_a['height']);
                 $imgevents = wppa_get_imgevents('thumb', $image['id'], true);
                 $title = $link ? esc_attr(stripslashes($link['title'])) : '';
                 $widget_content .= wppa_get_the_widget_thumb('lasten', $image, $album, $display, $link, $title, $imgurl, $imgstyle_a, $imgevents);
                 $widget_content .= "\n\t" . '<div style="font-size:' . wppa_opt('fontsize_widget_thumb') . 'px; line-height:' . $lineheight . 'px;">';
                 if ($timesince == 'yes') {
                     $widget_content .= "\n\t" . '<div>' . wppa_get_time_since($image[$order_by]) . '</div>';
                 }
                 $widget_content .= '</div>';
             } else {
                 // No image
                 $widget_content .= __('Photo not found', 'wp-photo-album-plus');
             }
             $widget_content .= "\n" . '</div>';
             $count++;
             if ($count == wppa_opt('lasten_count')) {
                 break;
             }
         }
     } else {
         $widget_content .= __('There are no uploaded photos (yet)', 'wp-photo-album-plus');
     }
     $widget_content .= '<div style="clear:both"></div>';
     $widget_content .= "\n" . '<!-- WPPA+ LasTen Widget end -->';
     echo "\n" . $before_widget;
     if (!empty($widget_title)) {
         echo $before_title . $widget_title . $after_title;
     }
     echo $widget_content . $after_widget;
     wppa('in_widget', false);
 }
 /** @see WP_Widget::widget */
 function widget($args, $instance)
 {
     global $wpdb;
     require_once dirname(__FILE__) . '/wppa-links.php';
     require_once dirname(__FILE__) . '/wppa-styles.php';
     require_once dirname(__FILE__) . '/wppa-functions.php';
     require_once dirname(__FILE__) . '/wppa-thumbnails.php';
     require_once dirname(__FILE__) . '/wppa-boxes-html.php';
     require_once dirname(__FILE__) . '/wppa-slideshow.php';
     wppa_initialize_runtime();
     wppa('in_widget', 'topten');
     wppa_bump_mocc();
     extract($args);
     $instance = wp_parse_args((array) $instance, array('title' => '', 'sortby' => 'mean_rating', 'title' => '', 'album' => '', 'display' => 'thumbs', 'meanrat' => 'yes', 'ratcount' => 'yes', 'viewcount' => 'yes', 'includesubs' => 'yes', 'medalsonly' => 'no', 'showowner' => 'no', 'showalbum' => 'no'));
     $widget_title = apply_filters('widget_title', $instance['title']);
     $page = in_array(wppa_opt('topten_widget_linktype'), wppa('links_no_page')) ? '' : wppa_get_the_landing_page('topten_widget_linkpage', __('Top Ten Photos', 'wp-photo-album-plus'));
     $albumlinkpage = wppa_get_the_landing_page('topten_widget_album_linkpage', __('Top Ten Photo album', 'wp-photo-album-plus'));
     $max = wppa_opt('topten_count');
     $album = $instance['album'];
     switch ($instance['sortby']) {
         case 'mean_rating':
             $sortby = '`mean_rating` DESC, `rating_count` DESC, `views` DESC';
             break;
         case 'rating_count':
             $sortby = '`rating_count` DESC, `mean_rating` DESC, `views` DESC';
             break;
         case 'views':
             $sortby = '`views` DESC, `mean_rating` DESC, `rating_count` DESC';
             break;
     }
     $display = $instance['display'];
     $meanrat = $instance['meanrat'] == 'yes';
     $ratcount = $instance['ratcount'] == 'yes';
     $viewcount = $instance['viewcount'] == 'yes';
     $includesubs = $instance['includesubs'] == 'yes';
     $albenum = '';
     $medalsonly = $instance['medalsonly'] == 'yes';
     $showowner = $instance['showowner'] == 'yes';
     $showalbum = $instance['showalbum'] == 'yes';
     wppa('medals_only', $medalsonly);
     $likes = wppa_opt('rating_display_type') == 'likes';
     // When likes only, mean rating has no meaning, chan to (rating)(like)count
     if ($likes && $instance['sortby'] == 'mean_rating') {
         $instance['sortby'] = 'rating_count';
     }
     // Album specified?
     if ($album) {
         // All albums ?
         if ($album == '-2') {
             $album = '0';
         }
         // Albums of owner is current logged in user or public?
         if ($album == '-3') {
             $temp = $wpdb->get_results("SELECT `id` FROM `" . WPPA_ALBUMS . "` WHERE `owner` = '--- public ---' OR `owner` = '" . wppa_get_user() . "' ORDER BY `id`", ARRAY_A);
             $album = '';
             if ($temp) {
                 foreach ($temp as $t) {
                     $album .= '.' . $t['id'];
                 }
                 $album = ltrim($album, '.');
             }
         }
         // Including subalbums?
         if ($includesubs) {
             $albenum = wppa_alb_to_enum_children($album);
             $albenum = wppa_expand_enum($albenum);
             $album = str_replace('.', ',', $albenum);
         }
         // Doit
         if ($medalsonly) {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` IN (" . $album . ") " . "AND `status` IN ( 'gold', 'silver', 'bronze' ) " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         } else {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` IN (" . $album . ") " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         }
     } else {
         if ($medalsonly) {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `status` IN ( 'gold', 'silver', 'bronze' ) " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         } else {
             $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` " . "ORDER BY " . $sortby . " " . "LIMIT " . $max, ARRAY_A);
         }
     }
     $widget_content = "\n" . '<!-- WPPA+ TopTen Widget start -->';
     $maxw = wppa_opt('topten_size');
     $maxh = $maxw;
     $lineheight = wppa_opt('fontsize_widget_thumb') * 1.5;
     $maxh += $lineheight;
     if ($meanrat) {
         $maxh += $lineheight;
     }
     if ($ratcount) {
         $maxh += $lineheight;
     }
     if ($viewcount) {
         $maxh += $lineheight;
     }
     if ($showowner) {
         $maxh += $lineheight;
     }
     if ($showalbum) {
         $maxh += $lineheight;
     }
     if ($thumbs) {
         foreach ($thumbs as $image) {
             $thumb = $image;
             // Make the HTML for current picture
             if ($display == 'thumbs') {
                 $widget_content .= "\n" . '<div class="wppa-widget" style="width:' . $maxw . 'px; height:' . $maxh . 'px; margin:4px; display:inline; text-align:center; float:left;">';
             } else {
                 $widget_content .= "\n" . '<div class="wppa-widget" >';
             }
             if ($image) {
                 $no_album = !$album;
                 if ($no_album) {
                     $tit = __('View the top rated photos', 'wp-photo-album-plus');
                 } else {
                     $tit = esc_attr(__(stripslashes($image['description'])));
                 }
                 $compressed_albumenum = wppa_compress_enum($albenum);
                 $link = wppa_get_imglnk_a('topten', $image['id'], '', $tit, '', $no_album, $compressed_albumenum);
                 $file = wppa_get_thumb_path($image['id']);
                 $imgstyle_a = wppa_get_imgstyle_a($image['id'], $file, $maxw, 'center', 'ttthumb');
                 $imgurl = wppa_get_thumb_url($image['id'], '', $imgstyle_a['width'], $imgstyle_a['height']);
                 $imgevents = wppa_get_imgevents('thumb', $image['id'], true);
                 $title = $link ? esc_attr(stripslashes($link['title'])) : '';
                 $widget_content .= wppa_get_the_widget_thumb('topten', $image, $album, $display, $link, $title, $imgurl, $imgstyle_a, $imgevents);
                 $widget_content .= "\n\t" . '<div style="font-size:' . wppa_opt('fontsize_widget_thumb') . 'px; line-height:' . $lineheight . 'px;">';
                 // Display (owner) ?
                 if ($showowner) {
                     $widget_content .= '<div>(' . $image['owner'] . ')</div>';
                 }
                 // Display (album) ?
                 if ($showalbum) {
                     $href = wppa_convert_to_pretty(wppa_encrypt_url(wppa_get_album_url($image['album'], $albumlinkpage, 'content', '1')));
                     $widget_content .= '<div>(<a href="' . $href . '" >' . wppa_get_album_name($image['album']) . '</a>)</div>';
                 }
                 // Display the rating
                 if ($likes) {
                     $lt = wppa_get_like_title_a($image['id']);
                 }
                 switch ($instance['sortby']) {
                     case 'mean_rating':
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($ratcount == 'yes') {
                             $n = wppa_get_rating_count_by_id($image['id']);
                             $widget_content .= '<div>' . sprintf(_n('%d vote', '%d votes', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         if ($viewcount == 'yes') {
                             $n = $image['views'];
                             $widget_content .= '<div>' . sprintf(_n('%d view', '%d views', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         break;
                     case 'rating_count':
                         if ($ratcount == 'yes') {
                             $n = wppa_get_rating_count_by_id($image['id']);
                             $widget_content .= '<div>' . ($likes ? $lt['display'] : sprintf(_n('%d vote', '%d votes', $n, 'wp-photo-album-plus'), $n)) . '</div>';
                         }
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($viewcount == 'yes') {
                             $n = $image['views'];
                             $widget_content .= '<div>' . sprintf(_n('%d view', '%d views', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         break;
                     case 'views':
                         if ($viewcount == 'yes') {
                             $n = $image['views'];
                             $widget_content .= '<div>' . sprintf(_n('%d view', '%d views', $n, 'wp-photo-album-plus'), $n) . '</div>';
                         }
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($ratcount == 'yes') {
                             $n = wppa_get_rating_count_by_id($image['id']);
                             $widget_content .= '<div>' . ($likes ? $lt['display'] : sprintf(_n('%d vote', '%d votes', $n, 'wp-photo-album-plus'), $n)) . '</div>';
                         }
                         break;
                 }
                 $widget_content .= '</div>';
             } else {
                 // No image
                 $widget_content .= __('Photo not found', 'wp-photo-album-plus');
             }
             $widget_content .= "\n" . '</div>';
         }
     } else {
         $widget_content .= __('There are no rated photos (yet)', 'wp-photo-album-plus');
     }
     $widget_content .= '<div style="clear:both"></div>';
     $widget_content .= "\n" . '<!-- WPPA+ TopTen Widget end -->';
     echo "\n" . $before_widget;
     if (!empty($widget_title)) {
         echo $before_title . $widget_title . $after_title;
     }
     echo $widget_content . $after_widget;
     //wppa( 'in_widget', false );
     wppa_reset_occurrance();
 }
function wppa_get_thumbs()
{
    global $wpdb;
    global $thumbs;
    global $wppa_session;
    // Log we are in
    wppa_dbg_msg('Get_thumbs entered, mocc = ' . wppa('mocc') . ', Start_album=' . wppa('start_album') . ', Cover=' . wppa('is_cover'));
    // Done already this occ?
    if (is_array($thumbs)) {
        wppa_dbg_msg('Cached thumbs used');
        return $thumbs;
    }
    // A cover -> no thumbs
    if (wppa('is_cover')) {
        wppa_dbg_msg('Its cover, leave get_thumbs');
        return false;
    }
    // Albums only -> no thumbs
    if (wppa('albums_only')) {
        wppa_dbg_msg('Albums only, leave get_thumbs');
        return false;
    }
    // Init
    $count_first = true;
    // Start timer
    $time = -microtime(true);
    // Make Album clause
    $fullalb = wppa('start_album');
    // See if album is an enumeration or range
    if (strpos($fullalb, '.') == false) {
        $album_clause = ' `album` = ' . $fullalb;
    } else {
        $ids = wppa_series_to_array($fullalb);
        $album_clause = ' `album` IN ( ' . implode(',', $ids) . ' )';
    }
    // So far so good
    // Now make the query, dependant of type of selection
    // Init
    $query = '';
    // Single image slideshow?
    if (wppa('start_photo') && wppa('is_single')) {
        $query = $wpdb->prepare('SELECT * FROM `' . WPPA_PHOTOS . '` WHERE `id` = %s', wppa('start_photo'));
    } elseif (wppa('is_upldr')) {
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        if (wppa('start_album')) {
            $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $album_clause . " AND `owner` = %s AND ( ( " . $status . " ) OR `owner` = %s ) " . "ORDER BY `timestamp` DESC", wppa('is_upldr'), wppa_get_user());
        } else {
            $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `owner` = %s AND ( ( " . $status . " ) OR `owner` = %s ) " . "ORDER BY `timestamp` DESC", wppa('is_upldr'), wppa_get_user());
        }
    } elseif (wppa('is_topten')) {
        $max = wppa('topten_count');
        switch (wppa_opt('topten_sortby')) {
            case 'mean_rating':
                $sortby = '`mean_rating` DESC, `rating_count` DESC, `views` DESC';
                break;
            case 'rating_count':
                $sortby = '`rating_count` DESC, `mean_rating` DESC, `views` DESC';
                break;
            case 'views':
                $sortby = '`views` DESC, `mean_rating` DESC, `rating_count` DESC';
                break;
            default:
                wppa_error_message('Unimplemented sorting method');
                $sortby = '';
                break;
        }
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        if (wppa('start_album')) {
            $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $album_clause . " AND ( " . $status . " ) " . "ORDER BY " . $sortby . " LIMIT " . $max;
        } else {
            $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE ( " . $status . " ) " . "ORDER BY " . $sortby . " LIMIT " . $max;
        }
        $count_first = false;
    } elseif (wppa('is_featen')) {
        $max = wppa('featen_count');
        if (wppa('start_album')) {
            $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $album_clause . " AND `status` = 'featured' " . "ORDER BY RAND( " . wppa_get_randseed() . " ) DESC LIMIT " . $max;
        } else {
            $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `status` = 'featured' " . "ORDER BY RAND( " . wppa_get_randseed() . " ) DESC LIMIT " . $max;
        }
        $count_first = false;
    } elseif (wppa('is_lasten')) {
        $max = wppa('lasten_count');
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        $order_by = wppa_switch('lasten_use_modified') ? 'modified' : 'timestamp';
        // If you want only 'New' photos in the selection, the period must be <> 0;
        if (wppa_switch('wppa_lasten_limit_new') && wppa_opt('max_photo_newtime')) {
            $newtime = " `" . $order_by . "` >= " . (time() - wppa_opt('max_photo_newtime'));
            if (current_user_can('wppa_moderate')) {
                if (wppa('start_album')) {
                    $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE ( " . $album_clause . " ) AND ( " . $newtime . " ) " . "ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
                } else {
                    $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $newtime . " " . "ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
                }
            } else {
                if (wppa('start_album')) {
                    $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE ( " . $album_clause . " ) AND ( " . $status . " ) AND ( " . $newtime . " ) " . "ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
                } else {
                    $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE ( " . $status . " ) AND (" . $newtime . ") " . "ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
                }
            }
        } else {
            if (current_user_can('wppa_moderate')) {
                if (wppa('start_album')) {
                    $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $album_clause . " " . "ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
                } else {
                    $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
                }
            } else {
                if (wppa('start_album')) {
                    $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE ( " . $album_clause . " ) AND ( " . $status . " ) " . "ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
                } else {
                    $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $status . " " . "ORDER BY `" . $order_by . "` DESC LIMIT " . $max;
                }
            }
        }
        $count_first = false;
    } elseif (wppa('is_comten')) {
        $alb_ids = wppa('start_album');
        if (strpos($alb_ids, '.') !== false) {
            $alb_ids = wppa_series_to_array($alb_ids);
        }
        // Comments only visible if logged in or not required to log in
        if (!wppa_switch('comment_view_login') || is_user_logged_in()) {
            $photo_ids = wppa_get_comten_ids(wppa('comten_count'), (array) $alb_ids);
        } else {
            $photo_ids = false;
        }
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        // To keep the sequence ok ( in sequence of comments desc ), do the queries one by one
        $thumbs = array();
        if (is_array($photo_ids)) {
            foreach ($photo_ids as $id) {
                $temp = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $status . " AND `id` = %s", $id), ARRAY_A);
                if ($temp) {
                    $thumbs[] = $temp;
                }
            }
        }
        wppa('any', !empty($thumbs));
        wppa('thumb_count', empty($thumbs) ? '0' : count($thumbs));
        $time += microtime(true);
        wppa_dbg_msg('Get thumbs exit is_comten took ' . $time . ' seconds. ' . 'Found: ' . wppa('thumb_count') . ' items. ' . 'Mem used=' . ceil(memory_get_peak_usage(true) / (1024 * 1024)) . ' Mb.');
        return $thumbs;
    } elseif (wppa('is_tag')) {
        // Init
        $andor = 'AND';
        if (strpos(wppa('is_tag'), ';')) {
            $andor = 'OR';
        }
        // Compute status clause for query
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        // Define tags clause for query
        $seltags = explode(',', trim(wppa_sanitize_tags(wppa('is_tag')), ','));
        $tags_like = '';
        $first = true;
        foreach ($seltags as $tag) {
            if (!$first) {
                $tags_like .= " " . $andor;
            }
            $tags_like .= " `tags` LIKE '%," . $tag . ",%'";
            $first = false;
        }
        // Prepare the query
        if (current_user_can('wppa_moderate')) {
            $query = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE (" . $tags_like . " ) " . wppa_get_photo_order('0');
        } else {
            $query = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE (" . $tags_like . " ) AND " . $status . " " . wppa_get_photo_order('0');
        }
    } elseif (wppa('supersearch')) {
        $ss_data = explode(',', wppa('supersearch'));
        $data = $ss_data['3'];
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        switch ($ss_data['1']) {
            // Name
            case 'n':
                $is = '=';
                if (substr($data, -3) == '...') {
                    $data = substr($data, 0, strlen($data) - 3) . '%';
                    $is = 'LIKE';
                }
                if (current_user_can('wppa_moderate')) {
                    $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `name` " . $is . " %s " . wppa_get_photo_order('0'), $data);
                } else {
                    $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `name` " . $is . " %s AND " . $status . " " . wppa_get_photo_order('0'), $data);
                }
                break;
                // Owner
            // Owner
            case 'o':
                if (current_user_can('wppa_moderate')) {
                    $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `owner` = %s " . wppa_get_photo_order('0'), $data);
                } else {
                    $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `owner` = %s AND " . $status . " " . wppa_get_photo_order('0'), $data);
                }
                break;
                // Tag
            // Tag
            case 'g':
                $taglist = wppa_get_taglist();
                if (strpos($data, '.')) {
                    $qtags = explode('.', $data);
                    $tagids = $taglist[$qtags['0']]['ids'];
                    $i = '0';
                    while ($i < count($qtags)) {
                        $tagids = array_intersect($tagids, $taglist[$qtags[$i]]['ids']);
                        $i++;
                    }
                } else {
                    $tagids = $taglist[$data]['ids'];
                }
                if (count($tagids) > '0') {
                    $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $status . " AND `id` IN (" . implode(',', $tagids) . ")";
                }
                break;
                // Text
            // Text
            case 't':
                // To distinguish items with ellipses, we temporary replace them with ***
                $data = str_replace('...', '***', $data);
                if (strpos($data, '.')) {
                    $temp = explode('.', $data);
                    $is = '=';
                    if (wppa_opt('ss_text_max')) {
                        if (substr($temp['0'], -3) == '***') {
                            $temp['0'] = substr($temp['0'], 0, strlen($temp['0']) - 3) . '%';
                            $is = 'LIKE';
                        }
                    }
                    $query = $wpdb->prepare("SELECT * FROM `" . WPPA_INDEX . "` WHERE `slug` " . $is . " %s", $temp['0']);
                    $indexes = $wpdb->get_results($query, ARRAY_A);
                    $ids = array();
                    foreach ($indexes as $item) {
                        $ids = array_merge($ids, explode('.', wppa_expand_enum($item['photos'])));
                    }
                    $i = '1';
                    while ($i < count($temp)) {
                        $is = '=';
                        if (wppa_opt('ss_text_max')) {
                            if (substr($temp[$i], -3) == '***') {
                                $temp[$i] = substr($temp[$i], 0, strlen($temp[$i]) - 3) . '%';
                                $is = 'LIKE';
                            }
                        }
                        $query = $wpdb->prepare("SELECT * FROM `" . WPPA_INDEX . "` WHERE `slug` " . $is . " %s", $temp[$i]);
                        $indexes = $wpdb->get_results($query, ARRAY_A);
                        $deltaids = array();
                        foreach ($indexes as $item) {
                            $deltaids = array_merge($deltaids, explode('.', wppa_expand_enum($item['photos'])));
                        }
                        $ids = array_intersect($ids, $deltaids);
                        $i++;
                    }
                } else {
                    $is = '=';
                    if (wppa_opt('ss_text_max')) {
                        if (substr($data, -3) == '***') {
                            $data = substr($data, 0, strlen($data) - 3) . '%';
                            $is = 'LIKE';
                        }
                    }
                    $query = $wpdb->prepare("SELECT * FROM `" . WPPA_INDEX . "` WHERE `slug` " . $is . " %s", $data);
                    $indexes = $wpdb->get_results($query, ARRAY_A);
                    $ids = array();
                    foreach ($indexes as $item) {
                        $ids = array_merge($ids, explode('.', wppa_expand_enum($item['photos'])));
                    }
                }
                if (empty($ids)) {
                    $ids = array('0');
                    // Dummy
                }
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $status . " AND `id` IN (" . trim(implode(',', $ids), ',') . ")";
                break;
                // Iptc
            // Iptc
            case 'i':
                $itag = str_replace('H', '#', $ss_data['2']);
                $desc = $ss_data['3'];
                $query = $wpdb->prepare("SELECT * FROM `" . WPPA_IPTC . "` " . "WHERE `tag` = %s AND `description` = %s", $itag, $desc);
                $iptclines = $wpdb->get_results($query, ARRAY_A);
                $ids = '0';
                if (is_array($iptclines)) {
                    foreach ($iptclines as $item) {
                        $ids .= ',' . $item['photo'];
                    }
                }
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $status . " AND `id` IN (" . $ids . ")";
                break;
                // Exif
            // Exif
            case 'e':
                $etag = str_replace('H', '#', $ss_data['2']);
                $desc = $ss_data['3'];
                $query = $wpdb->prepare("SELECT * FROM `" . WPPA_EXIF . "` " . "WHERE `tag` = %s AND `description` = %s", $etag, $desc);
                $exiflines = $wpdb->get_results($query, ARRAY_A);
                $ids = '0';
                if (is_array($exiflines)) {
                    foreach ($exiflines as $item) {
                        $ids .= ',' . $item['photo'];
                    }
                }
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $status . " AND `id` IN (" . $ids . ")";
                break;
        }
    } elseif (wppa('src')) {
        // Searching
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        $searchstring = wppa('searchstring');
        if (!empty($wppa_session['use_searchstring'])) {
            $searchstring = $wppa_session['use_searchstring'];
        }
        $final_array = array();
        $chunks = explode(',', stripslashes(strtolower($searchstring)));
        // all chunks
        foreach ($chunks as $chunk) {
            if (strlen(trim($chunk))) {
                $words = wppa_index_raw_to_words($chunk);
                $photo_array = array();
                // all words in the searchstring
                foreach ($words as $word) {
                    $word = trim($word);
                    if (strlen($word) > 1) {
                        if (strlen($word) > 20) {
                            $word = substr($word, 0, 20);
                        }
                        if (wppa_switch('wild_front')) {
                            $pidxs = $wpdb->get_results("SELECT `slug`, `photos` FROM `" . WPPA_INDEX . "` WHERE `slug` LIKE '%" . $word . "%'", ARRAY_A);
                        } else {
                            $pidxs = $wpdb->get_results("SELECT `slug`, `photos` FROM `" . WPPA_INDEX . "` WHERE `slug` LIKE '" . $word . "%'", ARRAY_A);
                        }
                        wppa_dbg_q('Q-IX');
                        $photos = '';
                        if ($pidxs) {
                            foreach ($pidxs as $pi) {
                                $photos .= $pi['photos'] . ',';
                            }
                        }
                        $photo_array[] = wppa_index_string_to_array(trim($photos, ','));
                    }
                }
                // Must meet all words: intersect photo sets
                foreach (array_keys($photo_array) as $idx) {
                    if ($idx > '0') {
                        $photo_array['0'] = array_intersect($photo_array['0'], $photo_array[$idx]);
                    }
                }
                // Save partial result
                if (isset($photo_array['0'])) {
                    $final_array = array_merge($final_array, $photo_array['0']);
                }
                // Remove dups
                $final_array = array_unique($final_array);
            }
        }
        // Remove scheduled and pending when not can moderate
        if (!current_user_can('wppa_moderate')) {
            $needmod = $wpdb->get_results("SELECT `id` FROM `" . WPPA_PHOTOS . "` WHERE `status` = 'scheduled' OR `status` = 'pending'", ARRAY_A);
            if (is_array($needmod)) {
                $delta = array();
                foreach ($needmod as $item) {
                    $delta[] = $item['id'];
                }
            }
            $final_array = array_diff($final_array, $delta);
        }
        // Remove private when not logged in
        if (!is_user_logged_in()) {
            $needlogin = $wpdb->get_results("SELECT `id` FROM `" . WPPA_PHOTOS . "` WHERE `status` = 'private'", ARRAY_A);
            if (is_array($needlogin)) {
                $delta = array();
                foreach ($needlogin as $item) {
                    $delta[] = $item['id'];
                }
            }
            $final_array = array_diff($final_array, $delta);
        }
        // remove dups from $final_array
        $final_array = array_unique($final_array);
        // Make album clause
        $alb_clause = '';
        // If rootsearch, the album clause resticts to sub the root
        // else: maybe exclude separates
        // See for rootsearch
        if (wppa('is_rootsearch') && isset($wppa_session['search_root'])) {
            // Find all albums below root
            $root = $wppa_session['search_root'];
            $root_albs = wppa_expand_enum(wppa_alb_to_enum_children($root));
            $root_albs = str_replace('.', ',', $root_albs);
            $alb_clause = $root_albs ? ' AND `album` IN (' . $root_albs . ') ' : '';
        } elseif (!$alb_clause && wppa_switch('excl_sep')) {
            $sep_albs = '';
            $temp = $wpdb->get_results("SELECT * FROM `" . WPPA_ALBUMS . "` WHERE `a_parent` = '-1'", ARRAY_A);
            if (!empty($temp)) {
                $t = array();
                foreach ($temp as $item) {
                    $t[] = $item['id'];
                }
                $sep_albs = implode('.', $t);
                $sep_albs = wppa_expand_enum(wppa_alb_to_enum_children($sep_albs));
                $sep_albs = str_replace('.', ',', $sep_albs);
                $alb_clause = $sep_albs ? ' AND `album` NOT IN (' . $sep_albs . ') ' : '';
            }
        }
        // compose photo selection
        if (!empty($final_array)) {
            $selection = " `id` IN (";
            $selection .= implode(',', $final_array);
            $selection .= ") ";
        } else {
            $selection = " `id` = '0' ";
        }
        // Construct the query
        $query = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $selection . $alb_clause . wppa_get_photo_order('0');
    } elseif (wppa('calendar')) {
        if (wppa('start_album')) {
            $alb_clause = " AND `album` IN ( " . str_replace('.', ',', wppa_expand_enum(wppa('start_album'))) . " ) ";
        } else {
            $alb_clause = '';
        }
        switch (wppa('calendar')) {
            case 'exifdtm':
                $selection = "`exifdtm` LIKE '" . strip_tags(wppa('caldate')) . "%' AND `status` <> 'pending' AND `status` <> 'scheduled' ";
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $selection . $alb_clause . wppa_get_photo_order('0');
                break;
            case 'timestamp':
                $t1 = strval(intval(wppa('caldate') * 24 * 60 * 60));
                $t2 = $t1 + 24 * 60 * 60;
                $selection = "`timestamp` >= {$t1} AND `timestamp` < {$t2} AND `status` <> 'pending' AND `status` <> 'scheduled' ";
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $selection . $alb_clause . wppa_get_photo_order('0');
                break;
            case 'modified':
                $t1 = strval(intval(wppa('caldate') * 24 * 60 * 60));
                $t2 = $t1 + 24 * 60 * 60;
                $selection = "`modified` >= {$t1} AND `modified` < {$t2} AND `status` <> 'pending' AND `status` <> 'scheduled' ";
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $selection . $alb_clause . wppa_get_photo_order('0');
                break;
        }
    } else {
        // Status
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        // On which album( s )?
        if (strpos(wppa('start_album'), '.') !== false) {
            $allalb = wppa_series_to_array(wppa('start_album'));
        } else {
            $allalb = false;
        }
        wppa_dbg_msg('Startalbum = ' . wppa('start_album'));
        // All albums ?
        if (wppa('start_album') == -2) {
            if (current_user_can('wppa_moderate')) {
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . wppa_get_photo_order('0');
            } else {
                $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE ( ( " . $status . " ) OR `owner` = %s ) " . wppa_get_photo_order('0'), wppa_get_user());
            }
        } elseif (wppa_is_int(wppa('start_album'))) {
            if (current_user_can('wppa_moderate')) {
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` = " . wppa('start_album') . " " . wppa_get_photo_order(wppa('start_album'));
            } else {
                $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE ( ( " . $status . " ) OR `owner` = %s ) AND `album` = " . wppa('start_album') . " " . wppa_get_photo_order(wppa('start_album')), wppa_get_user());
            }
        } elseif (is_array($allalb)) {
            $wherealbum = ' `album` IN (' . implode(',', $allalb) . ') ';
            if (current_user_can('wppa_moderate')) {
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $wherealbum . " " . wppa_get_photo_order('0');
            } else {
                $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE ( ( " . $status . " ) OR `owner` = %s ) AND " . $wherealbum . " " . wppa_get_photo_order('0'), wppa_get_user());
            }
        }
    }
    // Anything to look for?
    if (!$query) {
        // Not implemented or impossable shortcode
        return false;
    }
    // Do query and return result after copy result to $thumbs!!
    $thumbs = wppa_do_get_thumbs_query($query);
    return $thumbs;
}
function wppa_grant_albums($xparent = false)
{
    global $wpdb;
    static $grant_parents;
    static $my_albs_parents;
    static $owner;
    static $user;
    // Feature enabled?
    if (!wppa_switch('grant_an_album')) {
        return false;
    }
    // Owners only?
    if (!wppa_switch('owner_only')) {
        return false;
    }
    // User logged in?
    if (!is_user_logged_in()) {
        return false;
    }
    // Can user upload?
    if (!current_user_can('wppa_upload') && !wppa_switch('user_upload_on')) {
        return false;
    }
    // Init
    $albums_created = array();
    // Get required data if not done already
    // First get the grant parent album(s)
    if (!is_array($grant_parents)) {
        switch (wppa_opt('grant_parent_sel_method')) {
            case 'selectionbox':
                // Album ids are and expanded enumeration sep by , in the setting
                $grant_parents = explode(',', wppa_opt('grant_parent'));
                if (!is_array($grant_parents)) {
                    $grant_parents = array('0');
                }
                break;
            case 'category':
                // The option hold a category
                $grant_parents = $wpdb->get_col("SELECT `id` " . "FROM `" . WPPA_ALBUMS . "` " . "WHERE `cats` LIKE '%," . wppa_opt('grant_parent') . ",%'");
                break;
            case 'indexsearch':
                $temp = $wpdb->get_var("SELECT `albums` " . "FROM `" . WPPA_INDEX . "` " . "WHERE `slug` = '" . wppa_opt('grant_parent') . "'");
                $grant_parents = explode('.', wppa_expand_enum($temp));
                break;
        }
    }
    if (!$owner) {
        $owner = wppa_get_user('login');
        // The current users login name
    }
    if (!is_array($my_albs_parents)) {
        $query = $wpdb->prepare("SELECT DISTINCT `a_parent` FROM `" . WPPA_ALBUMS . "` WHERE `owner` = %s", $owner);
        $my_albs_parents = $wpdb->get_col($query);
        if (!is_array($my_albs_parents)) {
            $my_albs_parents = array();
        }
    }
    if (!$user) {
        $user = wppa_get_user(wppa_opt('grant_name'));
        // The current users name as how the album should be named
    }
    // If a parent is given and it is not a grant parent, quit
    if ($xparent && !in_array($xparent, $grant_parents)) {
        return false;
    }
    // If a parent is given, it will now be a grant parent (see directly above), only create the granted album inside this parent.
    if ($xparent) {
        $parents = array($xparent);
    } else {
        $parents = $grant_parents;
    }
    // Parent independant album data
    $name = $user;
    $desc = __('Default photo album for', 'wp-photo-album-plus') . ' ' . $user;
    // May be multiple granted parents. Check for all parents.
    foreach ($parents as $parent) {
        // Create only grant album if: parent is either -1 or existing
        if ($parent == '-1' || wppa_album_exists($parent)) {
            if (!in_array($parent, $my_albs_parents, true)) {
                // make an album for this user
                $id = wppa_create_album_entry(array('name' => $name, 'description' => $desc, 'a_parent' => $parent));
                if ($id) {
                    wppa_log('Obs', 'Album ' . wppa_get_album_name($parent) . '(' . $parent . ')' . ' -> ' . $id . ' for ' . $user . ' created.');
                    $albums_created[] = $id;
                    // Add this parent to the array of my albums parents
                    $my_albs_parents[] = $parent;
                } else {
                    wppa_log('Err', 'Could not create subalbum of ' . $parent . ' for ' . $user);
                }
                wppa_flush_treecounts($parent);
                wppa_index_add('album', $id);
            }
        }
    }
    // Remake permalink redirects
    if (!empty($albums_created)) {
        wppa_create_pl_htaccess();
    }
    return $albums_created;
}
 /** @see WP_Widget::widget */
 function widget($args, $instance)
 {
     global $wpdb;
     require_once dirname(__FILE__) . '/wppa-links.php';
     require_once dirname(__FILE__) . '/wppa-styles.php';
     require_once dirname(__FILE__) . '/wppa-functions.php';
     require_once dirname(__FILE__) . '/wppa-thumbnails.php';
     require_once dirname(__FILE__) . '/wppa-boxes-html.php';
     require_once dirname(__FILE__) . '/wppa-slideshow.php';
     wppa_initialize_runtime();
     wppa('in_widget', 'topten');
     wppa_bump_mocc();
     extract($args);
     $instance = wp_parse_args((array) $instance, array('title' => '', 'sortby' => 'mean_rating', 'title' => '', 'album' => '', 'display' => 'thumbs', 'meanrat' => 'yes', 'ratcount' => 'yes', 'viewcount' => 'yes', 'includesubs' => 'yes', 'showowner' => 'no', 'showalbum' => 'no'));
     $widget_title = apply_filters('widget_title', $instance['title']);
     $page = in_array(wppa_opt('topten_widget_linktype'), wppa('links_no_page')) ? '' : wppa_get_the_landing_page('wppa_topten_widget_linkpage', __('Top Ten Photos', 'wp-photo-album-plus'));
     $albumlinkpage = wppa_get_the_landing_page('wppa_topten_widget_album_linkpage', __('Top Ten Photo album', 'wp-photo-album-plus'));
     $max = wppa_opt('topten_count');
     $album = $instance['album'];
     switch ($instance['sortby']) {
         case 'mean_rating':
             $sortby = '`mean_rating` DESC, `rating_count` DESC, `views` DESC';
             break;
         case 'rating_count':
             $sortby = '`rating_count` DESC, `mean_rating` DESC, `views` DESC';
             break;
         case 'views':
             $sortby = '`views` DESC, `mean_rating` DESC, `rating_count` DESC';
             break;
     }
     $display = $instance['display'];
     $meanrat = $instance['meanrat'] == 'yes';
     $ratcount = $instance['ratcount'] == 'yes';
     $viewcount = $instance['viewcount'] == 'yes';
     $includesubs = $instance['includesubs'] == 'yes';
     $albenum = '';
     $showowner = $instance['showowner'] == 'yes';
     $showalbum = $instance['showalbum'] == 'yes';
     if ($album) {
         if ($album == '-2') {
             $album = '0';
         }
         if ($includesubs) {
             $albenum = wppa_alb_to_enum_children($album);
             $albenum = wppa_expand_enum($albenum);
             $album = str_replace('.', ',', $albenum);
         }
         $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` IN (" . $album . ") ORDER BY " . $sortby . " LIMIT " . $max, ARRAY_A);
         //wppa_log('dbg', "SELECT * FROM `".WPPA_PHOTOS."` WHERE `album` IN (".$album.") ORDER BY " . $sortby . " LIMIT " . $max);
     } else {
         $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` ORDER BY " . $sortby . " LIMIT " . $max, ARRAY_A);
     }
     $widget_content = "\n" . '<!-- WPPA+ TopTen Widget start -->';
     $maxw = wppa_opt('topten_size');
     $maxh = $maxw;
     $lineheight = wppa_opt('fontsize_widget_thumb') * 1.5;
     $maxh += $lineheight;
     if ($meanrat) {
         $maxh += $lineheight;
     }
     if ($ratcount) {
         $maxh += $lineheight;
     }
     if ($viewcount) {
         $maxh += $lineheight;
     }
     if ($showowner) {
         $maxh += $lineheight;
     }
     if ($showalbum) {
         $maxh += $lineheight;
     }
     if ($thumbs) {
         foreach ($thumbs as $image) {
             $thumb = $image;
             // Make the HTML for current picture
             if ($display == 'thumbs') {
                 $widget_content .= "\n" . '<div class="wppa-widget" style="width:' . $maxw . 'px; height:' . $maxh . 'px; margin:4px; display:inline; text-align:center; float:left;">';
             } else {
                 $widget_content .= "\n" . '<div class="wppa-widget" >';
             }
             if ($image) {
                 $no_album = !$album;
                 if ($no_album) {
                     $tit = __('View the top rated photos', 'wp-photo-album-plus');
                 } else {
                     $tit = esc_attr(__(stripslashes($image['description'])));
                 }
                 $compressed_albumenum = wppa_compress_enum($albenum);
                 $link = wppa_get_imglnk_a('topten', $image['id'], '', $tit, '', $no_album, $compressed_albumenum);
                 $file = wppa_get_thumb_path($image['id']);
                 $imgstyle_a = wppa_get_imgstyle_a($image['id'], $file, $maxw, 'center', 'ttthumb');
                 $imgurl = wppa_get_thumb_url($image['id'], '', $imgstyle_a['width'], $imgstyle_a['height']);
                 $imgevents = wppa_get_imgevents('thumb', $image['id'], true);
                 $title = $link ? esc_attr(stripslashes($link['title'])) : '';
                 $widget_content .= wppa_get_the_widget_thumb('topten', $image, $album, $display, $link, $title, $imgurl, $imgstyle_a, $imgevents);
                 $widget_content .= "\n\t" . '<div style="font-size:' . wppa_opt('fontsize_widget_thumb') . 'px; line-height:' . $lineheight . 'px;">';
                 // Display (owner) ?
                 if ($showowner) {
                     $widget_content .= '<div>(' . $image['owner'] . ')</div>';
                 }
                 // Display (album) ?
                 if ($showalbum) {
                     $href = wppa_convert_to_pretty(wppa_get_album_url($image['album'], $albumlinkpage, 'content', '1'));
                     $widget_content .= '<div>(<a href="' . $href . '" >' . wppa_get_album_name($image['album']) . '</a>)</div>';
                 }
                 $rating = wppa_get_rating_by_id($image['id']);
                 switch ($instance['sortby']) {
                     case 'mean_rating':
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($ratcount == 'yes') {
                             $widget_content .= '<div>' . sprintf(__('%s Votes', 'wp-photo-album-plus'), wppa_get_rating_count_by_id($image['id'])) . '</div>';
                         }
                         if ($viewcount == 'yes' && $image['views']) {
                             $widget_content .= '<div>' . sprintf(__('Views: %s times', 'wp-photo-album-plus'), $image['views']) . '</div>';
                         }
                         break;
                     case 'rating_count':
                         if ($ratcount == 'yes') {
                             $widget_content .= '<div>' . sprintf(__('%s Votes', 'wp-photo-album-plus'), wppa_get_rating_count_by_id($image['id'])) . '</div>';
                         }
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($viewcount == 'yes' && $image['views']) {
                             $widget_content .= '<div>' . sprintf(__('Views: %s times', 'wp-photo-album-plus'), $image['views']) . '</div>';
                         }
                         break;
                     case 'views':
                         if ($viewcount == 'yes' && $image['views']) {
                             $widget_content .= '<div>' . sprintf(__('Views: %s times', 'wp-photo-album-plus'), $image['views']) . '</div>';
                         }
                         if ($meanrat == 'yes') {
                             $widget_content .= '<div>' . wppa_get_rating_by_id($image['id']) . '</div>';
                         }
                         if ($ratcount == 'yes') {
                             $widget_content .= '<div>' . sprintf(__('%s Votes', 'wp-photo-album-plus'), wppa_get_rating_count_by_id($image['id'])) . '</div>';
                         }
                         break;
                 }
                 $widget_content .= '</div>';
             } else {
                 // No image
                 $widget_content .= __('Photo not found.', 'wp-photo-album-plus');
             }
             $widget_content .= "\n" . '</div>';
         }
     } else {
         $widget_content .= 'There are no rated photos (yet).';
     }
     $widget_content .= '<div style="clear:both"></div>';
     $widget_content .= "\n" . '<!-- WPPA+ TopTen Widget end -->';
     echo "\n" . $before_widget;
     if (!empty($widget_title)) {
         echo $before_title . $widget_title . $after_title;
     }
     echo $widget_content . $after_widget;
     wppa('in_widget', false);
 }
Example #7
0
function wppa_get_calendar_html()
{
    global $wpdb;
    // Init
    $result = '';
    $secinday = 24 * 60 * 60;
    $calendar_type = wppa('calendar');
    $autoall = wppa('calendarall');
    $albums = wppa('start_album') ? wppa_expand_enum(wppa_alb_to_enum_children(wppa('start_album'))) : '';
    $alb_clause = $albums ? ' AND `album` IN ( ' . str_replace('.', ',', $albums) . ' ) ' : '';
    $alb_arg = wppa('start_album') ? 'wppa-album=' . wppa_alb_to_enum_children(wppa('start_album')) . '&' : '';
    $reverse = wppa('reverse') ? ' DESC ' : '';
    // Get todays daynumber and range
    $today = floor(time() / $secinday);
    switch ($calendar_type) {
        case 'exifdtm':
            $photos = $wpdb->get_results("SELECT `id`, `exifdtm` " . "FROM `" . WPPA_PHOTOS . "` " . "WHERE `exifdtm` <> '' " . "AND `status` <> 'pending' " . "AND `status` <> 'scheduled' " . $alb_clause . "ORDER BY `exifdtm`" . $reverse, ARRAY_A);
            $dates = array();
            foreach ($photos as $photo) {
                $date = substr($photo['exifdtm'], 0, 10);
                if (wppa_is_exif_date($date)) {
                    if (isset($dates[$date])) {
                        $dates[$date]++;
                    } else {
                        $dates[$date] = '1';
                    }
                }
            }
            $from = 0;
            $to = count($dates);
            break;
        case 'timestamp':
        case 'modified':
            $photos = $wpdb->get_results("SELECT `id`, `" . $calendar_type . "` " . "FROM `" . WPPA_PHOTOS . "` " . "WHERE `" . $calendar_type . "` > 0 " . "AND `status` <> 'pending' " . "AND `status` <> 'scheduled' " . $alb_clause . "ORDER BY `" . $calendar_type . "`" . $reverse, ARRAY_A);
            $dates = array();
            foreach ($photos as $photo) {
                $date = floor($photo[$calendar_type] / $secinday);
                if (isset($dates[$date])) {
                    $dates[$date]++;
                } else {
                    $dates[$date] = '1';
                }
            }
            $from = 0;
            $to = count($dates);
            break;
    }
    // Display minicovers
    $result .= '<div' . ' style="' . 'width:' . 33 * ($to - $from) . 'px;' . 'position:relative;' . '"' . ' >';
    $result .= '<style type="text/css" scoped >' . '.wppa-minicover-current div {' . 'color:blue;' . '}' . '.wppa-minicover {' . 'box-sizing:content-box;' . '}' . '</style>';
    $result .= '<script type="text/javascript" >' . 'wppaWaitForCounter = 0;' . '</script>';
    switch ($calendar_type) {
        case 'exifdtm':
            $keys = array_keys($dates);
            for ($day = $from; $day < $to; $day++) {
                $date = date_create_from_format('Y:m:d', $keys[$day]);
                if (is_object($date)) {
                    $ajaxurl = wppa_get_ajaxlink('', '1') . 'wppa-calendar=exifdtm&' . 'wppa-caldate=' . $keys[$day] . '&' . $alb_arg . 'wppa-occur=1';
                    if ($autoall) {
                        $onclick = '';
                    } else {
                        $onclick = 'jQuery( \'.wppa-minicover-' . wppa('mocc') . '\' ).removeClass( \'wppa-minicover-current\' );' . 'jQuery( this ).addClass( \'wppa-minicover-current\' );' . 'wppaDoAjaxRender( ' . (wppa('mocc') + '1') . ', \'' . $ajaxurl . '\', \'\' );';
                    }
                    $result .= '<a' . ($autoall ? ' href="#wppa-' . $day . '"' : '') . ' class="wppa-minicover-' . wppa('mocc') . '"' . ' onclick="' . $onclick . '"' . ' >' . '<div' . ' id="wppa-minicover-' . $day . '"' . ' class="wppa-minicover"' . ' style="' . 'border:1px solid gray;' . 'margin-right:1px;' . 'float:left;' . 'text-align:center;' . 'width:30px;"' . ' >' . $date->format('M') . '<br />' . $date->format('d') . '<br />' . $date->format('D') . '<br />' . $date->format('Y') . '<br />' . '(' . $dates[$keys[$day]] . ')' . '</div>' . '</a>';
                    if ($autoall) {
                        $addlabel = '<a id=\\"wppa-' . $day . '\\" ></a>';
                        $result .= '<script type="text/javascript" >' . 'wppaDoAjaxRender( ' . (wppa('mocc') + '1') . ', \'' . str_replace('&amp;', '&', $ajaxurl) . '\', \'\', \'' . $addlabel . '\', ' . ($day + '1') . ' );' . '</script>';
                    }
                }
            }
            break;
        case 'timestamp':
        case 'modified':
            $keys = array_keys($dates);
            for ($day = $from; $day < $to; $day++) {
                $date = $keys[$day];
                $ajaxurl = wppa_get_ajaxlink('', '1') . 'wppa-calendar=' . $calendar_type . '&' . 'wppa-caldate=' . $keys[$day] . '&' . $alb_arg . 'wppa-occur=1';
                if ($autoall) {
                    $onclick = '';
                } else {
                    $onclick = 'jQuery( \'.wppa-minicover-' . wppa('mocc') . '\' ).removeClass( \'wppa-minicover-current\' );' . 'jQuery( this ).addClass( \'wppa-minicover-current\' );' . 'wppaDoAjaxRender( ' . (wppa('mocc') + '1') . ', \'' . $ajaxurl . '\', \'\' );';
                }
                $result .= '<a' . ' class="wppa-minicover-' . wppa('mocc') . '"' . ' onclick="' . $onclick . '"' . ' >' . '<div' . ' id="wppa-minicover-' . $day . '"' . ' class="wppa-minicover"' . ' style="' . 'border:1px solid gray;' . 'margin-right:1px;' . 'float:left;' . 'text-align:center;' . 'width:30px;"' . ' >' . date('M', $date * $secinday) . '<br />' . date('d', $date * $secinday) . '<br />' . date('D', $date * $secinday) . '<br />' . date('Y', $date * $secinday) . '<br />' . '(' . $dates[$keys[$day]] . ')' . '</div>' . '</a>';
                if ($autoall) {
                    $addlabel = '<a id=\\"wppa-' . $day . '\\" ></a>';
                    $result .= '<script type="text/javascript" >' . 'wppaDoAjaxRender( ' . (wppa('mocc') + '1') . ', \'' . str_replace('&amp;', '&', $ajaxurl) . '\', \'\', \'' . $addlabel . '\', ' . ($day + '1') . ' );' . '</script>';
                }
            }
            break;
    }
    $result .= '<script type="text/javascript" >' . 'jQuery(document).ready(function(){ wppaWaitForCounter = 1; });' . '</script>';
    $result .= '</div>';
    return $result;
}
function wppa_get_widgetphotos($alb, $option = '')
{
    global $wpdb;
    if (!$alb) {
        return false;
    }
    $photos = false;
    $query = '';
    // Compile status clause
    switch (wppa_opt('potd_status_filter')) {
        case 'publish':
            $statusclause = " `status` = 'publish' ";
            break;
        case 'featured':
            $statusclause = " `status` = 'featured' ";
            break;
        case 'gold':
            $statusclause = " `status` = 'gold' ";
            break;
        case 'silver':
            $statusclause = " `status` = 'silver' ";
            break;
        case 'bronze':
            $statusclause = " `status` = 'bronze' ";
            break;
        case 'anymedal':
            $statusclause = " `status` IN ( 'gold', 'silver', 'bronze' ) ";
            break;
        default:
            $statusclause = " `status` <> 'scheduled' ";
            if (!is_user_logged_in()) {
                $statusclause .= " AND `status` <> 'private' ";
            }
    }
    // If physical album(s) and include subalbums is active, make it an enumeration(with ',' as seperator)
    if (wppa_opt('potd_album_type') == 'physical' && wppa_switch('potd_include_subs')) {
        $alb = str_replace(',', '.', $alb);
        $alb = wppa_expand_enum(wppa_alb_to_enum_children($alb));
        $alb = str_replace('.', ',', $alb);
    }
    // If physical albums and inverse selection is active, invert selection
    if (wppa_opt('potd_album_type') == 'physical' && wppa_switch('potd_inverse')) {
        $albs = explode(',', $alb);
        $all = $wpdb->get_col("SELECT `id` FROM `" . WPPA_ALBUMS . "` ");
        $alb = implode(',', array_diff($all, $albs));
    }
    /* Now find out the final query */
    /* Physical albums */
    // Is it a single album?
    if (wppa_is_int($alb)) {
        $query = $wpdb->prepare("SELECT `id`, `p_order` " . "FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` = %s " . "AND " . $statusclause . $option, $alb);
    } elseif (strchr($alb, ',')) {
        $alb = trim($alb, ',');
        $query = "SELECT `id`, `p_order` " . "FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` IN ( " . $alb . " ) " . "AND " . $statusclause . $option;
    } elseif ($alb == 'all') {
        $query = "SELECT `id`, `p_order` " . "FROM `" . WPPA_PHOTOS . "` " . "WHERE " . $statusclause . $option;
    } elseif ($alb == 'sep') {
        $albs = $wpdb->get_results("SELECT `id`, `a_parent` FROM `" . WPPA_ALBUMS . "`", ARRAY_A);
        $query = "SELECT `id`, `p_order` FROM `" . WPPA_PHOTOS . "` WHERE ( `album` = '0' ";
        $first = true;
        foreach ($albs as $a) {
            if ($a['a_parent'] == '-1') {
                $query .= "OR `album` = '" . $a['id'] . "' ";
            }
        }
        $query .= ") AND " . $statusclause . $option;
    } elseif ($alb == 'all-sep') {
        $albs = $wpdb->get_results("SELECT `id`, `a_parent` FROM `" . WPPA_ALBUMS . "`", ARRAY_A);
        $query = "SELECT `id`, `p_order` FROM `" . WPPA_PHOTOS . "` WHERE ( `album` IN ('0'";
        foreach ($albs as $a) {
            if ($a['a_parent'] != '-1') {
                $query .= ",'" . $a['id'] . "'";
            }
        }
        $query .= ") ) AND " . $statusclause . $option;
    } elseif ($alb == 'topten') {
        // Find the 'top' policy
        switch (wppa_opt('topten_sortby')) {
            case 'mean_rating':
                $sortby = '`mean_rating` DESC, `rating_count` DESC, `views` DESC';
                break;
            case 'rating_count':
                $sortby = '`rating_count` DESC, `mean_rating` DESC, `views` DESC';
                break;
            case 'views':
                $sortby = '`views` DESC, `mean_rating` DESC, `rating_count` DESC';
                break;
            default:
                wppa_error_message('Unimplemented sorting method');
                $sortby = '';
                break;
        }
        // It is assumed that status is ok for top rated photos
        $query = "SELECT `id`, `p_order` FROM `" . WPPA_PHOTOS . "` ORDER BY " . $sortby . " LIMIT " . wppa_opt('topten_count');
        $query .= $option;
    }
    // Do the query
    if ($query) {
        $photos = $wpdb->get_results($query, ARRAY_A);
        wppa_dbg_msg('Potd query: ' . $query);
    } else {
        $photos = array();
    }
    // Ready
    return $photos;
}
Example #9
0
    /** @see WP_Widget::form */
    function form($instance)
    {
        global $wpdb;
        //Defaults
        $instance = wp_parse_args((array) $instance, array('title' => __('User Photos', 'wp-photo-album-plus'), 'sortby' => 'name', 'ignore' => 'admin', 'parent' => ''));
        $widget_title = apply_filters('widget_title', $instance['title']);
        ?>
		<p><label for="<?php 
        echo $this->get_field_id('title');
        ?>
"><?php 
        _e('Title:', 'wp-photo-album-plus');
        ?>
</label>
			<input class="widefat" id="<?php 
        echo $this->get_field_id('title');
        ?>
" name="<?php 
        echo $this->get_field_name('title');
        ?>
" type="text" value="<?php 
        echo $widget_title;
        ?>
" />
		</p>

		<p><label for="<?php 
        echo $this->get_field_id('sortby');
        ?>
"><?php 
        _e('Sort by:', 'wp-photo-album-plus');
        ?>
</label>
			<select class="widefat" id="<?php 
        echo $this->get_field_id('sortby');
        ?>
" name="<?php 
        echo $this->get_field_name('sortby');
        ?>
" >
				<option value="name" <?php 
        if ($instance['sortby'] == 'name') {
            echo 'selected="selected"';
        }
        ?>
><?php 
        _e('Display name', 'wp-photo-album-plus');
        ?>
</option>
				<option value="count" <?php 
        if ($instance['sortby'] == 'count') {
            echo 'selected="selected"';
        }
        ?>
><?php 
        _e('Number of photos', 'wp-photo-album-plus');
        ?>
</option>
				<option value="date" <?php 
        if ($instance['sortby'] == 'date') {
            echo 'selected="selected"';
        }
        ?>
><?php 
        _e('Most recent photo', 'wp-photo-album-plus');
        ?>
</option>
			</select>
		</p>

		<p><label for="<?php 
        echo $this->get_field_id('ignore');
        ?>
"><?php 
        _e('Ignore:', 'wp-photo-album-plus');
        ?>
</label>
			<input class="widefat" id=<?php 
        echo $this->get_field_id('ignore');
        ?>
" name="<?php 
        echo $this->get_field_name('ignore');
        ?>
" value="<?php 
        echo $instance['ignore'];
        ?>
" />
			<small><?php 
        _e('Enter loginnames seperated by commas', 'wp-photo-album-plus');
        ?>
</small>
		</p>

		<p><label for="<?php 
        echo $this->get_field_id('parent');
        ?>
"><?php 
        _e('Look only in albums (including sub-albums):', 'wp-photo-album-plus');
        ?>
</label>
			<input type="hidden" id="<?php 
        echo $this->get_field_id('parent');
        ?>
" name="<?php 
        echo $this->get_field_name('parent');
        ?>
" value="<?php 
        echo $instance['parent'];
        ?>
" />
			<?php 
        if ($instance['parent']) {
            echo '<br/><small>( ' . $instance['parent'] . ' )</small>';
        }
        ?>
			<select class="widefat" multiple="multiple" onchange="wppaGetSelEnumToId( 'parentalbums-<?php 
        echo $this->get_widget_id();
        ?>
', '<?php 
        echo $this->get_field_id('parent');
        ?>
' )" id="<?php 
        echo $this->get_field_id('parent-list');
        ?>
" name="<?php 
        echo $this->get_field_name('parent-list');
        ?>
" >
			<?php 
        // Prepare albuminfo
        $albums = $wpdb->get_results("SELECT `id`, `name` FROM `" . WPPA_ALBUMS . "`", ARRAY_A);
        if (wppa_switch('hier_albsel')) {
            $albums = wppa_add_paths($albums);
        } else {
            foreach (array_keys($albums) as $index) {
                $albums[$index]['name'] = __(stripslashes($albums[$index]['name']), 'wp-photo-album-plus');
            }
        }
        $albums = wppa_array_sort($albums, 'name');
        // Please select
        $sel = $instance['parent'] ? '' : 'selected="selected" ';
        echo '<option class="parentalbums-' . $this->get_widget_id() . '" value="" ' . $sel . '>-- ' . __('All albums', 'wp-photo-album-plus') . ' --</option>';
        // Find the albums currently selected
        $selalbs = explode('.', wppa_expand_enum($instance['parent']));
        // All standard albums
        foreach ($albums as $album) {
            $s = in_array($album['id'], $selalbs);
            $sel = $s ? 'selected="selected" ' : '';
            echo '<option class="parentalbums-' . $this->get_widget_id() . '" value="' . $album['id'] . '" ' . $sel . '>' . stripslashes(__($album['name'], 'wp-photo-album-plus')) . ' (' . $album['id'] . ')</option>';
        }
        ?>
			</select>
		</p>

<?php 
    }
function wppa_get_thumbs()
{
    global $wpdb;
    global $wppa;
    global $thumbs;
    global $wppa_session;
    if ($wppa['is_owner'] && !$wppa['start_album']) {
        return false;
    }
    // No owner album( s ) -> no photos
    wppa_dbg_msg('get_thumbs entered: ' . $wppa['mocc'] . ' Start_album=' . $wppa['start_album'] . ', Cover=' . $wppa['is_cover']);
    if ($wppa['is_cover']) {
        wppa_dbg_msg('its cover, leave get_thumbs');
        return;
    }
    if ($wppa['albums_only']) {
        return false;
    }
    if (is_array($thumbs)) {
        // Done already?
        wppa_dbg_msg('cached thumbs used');
        return $thumbs;
    }
    $time = -microtime(true);
    // See if album is an enumeration or range
    $fullalb = $wppa['start_album'];
    // Assume not
    if (strpos($fullalb, '.') !== false) {
        $ids = wppa_series_to_array($fullalb);
        $fullalb = implode(' OR `album` = ', $ids);
    }
    // Single image slideshow?
    if ($wppa['start_photo'] && $wppa['is_single']) {
        $thumbs = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . WPPA_PHOTOS . '` WHERE `id` = %s', $wppa['start_photo']), ARRAY_A);
        wppa_dbg_q('Q-SIS');
    } elseif ($wppa['is_upldr']) {
        $max = '1000000';
        $alb = $fullalb;
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        if ($alb) {
            $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE ( `album` = " . $alb . " ) AND `owner` = %s AND ( ( " . $status . " ) OR `owner` = %s ) ORDER BY `timestamp` DESC LIMIT %d", $wppa['is_upldr'], wppa_get_user(), $max);
            //, ARRAY_A );
        } else {
            $query = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `owner` = %s AND ( ( " . $status . " ) OR `owner` = %s ) ORDER BY `timestamp` DESC LIMIT %d", $wppa['is_upldr'], wppa_get_user(), $max);
            //, ARRAY_A );
        }
        $thumbs = $wpdb->get_results($query, ARRAY_A);
        wppa_dbg_q('Q-UPL');
    } elseif ($wppa['is_topten']) {
        $max = $wppa['topten_count'];
        $alb = $fullalb;
        switch (wppa_opt('topten_sortby')) {
            case 'mean_rating':
                $sortby = '`mean_rating` DESC, `rating_count` DESC, `views` DESC';
                break;
            case 'rating_count':
                $sortby = '`rating_count` DESC, `mean_rating` DESC, `views` DESC';
                break;
            case 'views':
                $sortby = '`views` DESC, `mean_rating` DESC, `rating_count` DESC';
                break;
            default:
                wppa_error_message('Unimplemented sorting method');
                $sortby = '';
                break;
        }
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        if ($alb) {
            $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE ( `album` = " . $alb . " AND " . $status . " ) ORDER BY " . $sortby . " LIMIT " . $max, ARRAY_A);
        } else {
            $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE ( " . $status . " ) ORDER BY " . $sortby . " LIMIT " . $max, ARRAY_A);
        }
        wppa_dbg_q('Q-TT');
    } elseif ($wppa['is_featen']) {
        $max = $wppa['featen_count'];
        $alb = $fullalb;
        if ($alb) {
            $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `status` = 'featured' AND ( `album` = " . $alb . " ) ORDER BY RAND( " . wppa_get_randseed() . " ) DESC LIMIT " . $max, ARRAY_A);
        } else {
            $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `status` = 'featured' ORDER BY RAND( " . wppa_get_randseed() . " ) DESC LIMIT " . $max, ARRAY_A);
        }
        wppa_dbg_q('Q-FT');
    } elseif ($wppa['is_lasten']) {
        $max = $wppa['lasten_count'];
        $alb = $fullalb;
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        // If you want only 'New' photos in the selection, the period must be <> 0;
        if (wppa_switch('wppa_lasten_limit_new') && wppa_opt('max_photo_newtime')) {
            $newtime = " `timestamp` >= " . (time() - wppa_opt('max_photo_newtime'));
            if (current_user_can('wppa_moderate')) {
                if ($alb) {
                    $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE ( `album` = " . $alb . " ) AND (" . $newtime . ") ORDER BY `timestamp` DESC LIMIT " . $max;
                } else {
                    $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $newtime . " ORDER BY `timestamp` DESC LIMIT " . $max;
                }
            } else {
                if ($alb) {
                    $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE ( `album` = " . $alb . " ) AND ( " . $status . " ) AND ( " . $newtime . " ) ORDER BY `timestamp` DESC LIMIT " . $max;
                } else {
                    $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE ( " . $status . " ) AND (" . $newtime . ") ORDER BY `timestamp` DESC LIMIT " . $max;
                }
            }
        } else {
            if (current_user_can('wppa_moderate')) {
                if ($alb) {
                    $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = " . $alb . " ORDER BY `timestamp` DESC LIMIT " . $max;
                } else {
                    $q = "SELECT * FROM `" . WPPA_PHOTOS . "` ORDER BY `timestamp` DESC LIMIT " . $max;
                }
            } else {
                if ($alb) {
                    $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE ( `album` = " . $alb . " ) AND ( " . $status . " ) ORDER BY `timestamp` DESC LIMIT " . $max;
                } else {
                    $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $status . " ORDER BY `timestamp` DESC LIMIT " . $max;
                }
            }
        }
        // echo $q;
        $thumbs = $wpdb->get_results($q, ARRAY_A);
        wppa_dbg_q('Q-LT');
    } elseif ($wppa['is_comten']) {
        $alb_ids = $wppa['start_album'];
        if (strpos($alb_ids, '.') !== false) {
            $alb_ids = wppa_series_to_array($alb_ids);
        }
        $photo_ids = wppa_get_comten_ids($wppa['comten_count'], (array) $alb_ids);
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        $thumbs = array();
        if (is_array($photo_ids)) {
            foreach ($photo_ids as $id) {
                $temp = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $status . " AND `id` = %s", $id), ARRAY_A);
                if ($temp) {
                    $thumbs[] = $temp;
                }
            }
        }
    } elseif ($wppa['is_tag']) {
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        if (current_user_can('wppa_moderate')) {
            $temp = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `tags` <> '' " . wppa_get_photo_order('0'), ARRAY_A);
        } else {
            $temp = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $status . " AND `tags` <> '' " . wppa_get_photo_order('0'), ARRAY_A);
        }
        wppa_dbg_q('Q-TG');
        $tags = wppa_get_taglist();
        $thumbs = false;
        $andor = 'and';
        if (strpos($wppa['is_tag'], ';')) {
            $andor = 'or';
        }
        foreach (array_keys($temp) as $index) {
            if ($andor == 'and') {
                // and
                $seltags = explode(',', $wppa['is_tag']);
                $in = true;
                if ($seltags) {
                    foreach ($seltags as $seltag) {
                        if ($seltag && !@in_array($temp[$index]['id'], $tags[$seltag]['ids'])) {
                            $in = false;
                        }
                    }
                }
            } else {
                // or
                $seltags = explode(';', $wppa['is_tag']);
                $in = false;
                if ($seltags) {
                    foreach ($seltags as $seltag) {
                        if ($seltag && @in_array($temp[$index]['id'], $tags[$seltag]['ids'])) {
                            $in = true;
                        }
                    }
                }
            }
            if ($in) {
                if ($wppa['is_related'] != 'tags' || count($thumbs) < $wppa['related_count']) {
                    $thumbs[] = $temp[$index];
                }
            }
        }
        wppa_dbg_msg('Found:' . count($thumbs) . ' thumbs');
    } elseif ($wppa['supersearch']) {
        $ss_data = explode(',', $wppa['supersearch']);
        $data = $ss_data['3'];
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        switch ($ss_data['1']) {
            // Name
            case 'n':
                $is = '=';
                if (substr($data, -3) == '...') {
                    $data = substr($data, 0, strlen($data) - 3) . '%';
                    $is = 'LIKE';
                }
                if (current_user_can('wppa_moderate')) {
                    $thumbs = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `name` " . $is . " %s " . wppa_get_photo_order('0'), $data), ARRAY_A);
                } else {
                    $thumbs = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `name` " . $is . " %s AND " . $status . " " . wppa_get_photo_order('0'), $data), ARRAY_A);
                }
                wppa_dbg_q('Q-SS');
                break;
                // Owner
            // Owner
            case 'o':
                if (current_user_can('wppa_moderate')) {
                    $thumbs = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `owner` = %s " . wppa_get_photo_order('0'), $data), ARRAY_A);
                } else {
                    $thumbs = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `owner` = %s AND " . $status . " " . wppa_get_photo_order('0'), $data), ARRAY_A);
                }
                wppa_dbg_q('Q-SS');
                break;
                // Tag
            // Tag
            case 'g':
                $taglist = wppa_get_taglist();
                if (strpos($data, '.')) {
                    $qtags = explode('.', $data);
                    $tagids = $taglist[$qtags['0']]['ids'];
                    $i = '0';
                    while ($i < count($qtags)) {
                        $tagids = array_intersect($tagids, $taglist[$qtags[$i]]['ids']);
                        $i++;
                    }
                } else {
                    $tagids = $taglist[$data]['ids'];
                }
                if (count($tagids) > '0') {
                    $query = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $status . " AND `id` IN (" . implode(',', $tagids) . ")";
                    $thumbs = $wpdb->get_results($query, ARRAY_A);
                    wppa_dbg_q('Q-SS');
                }
                break;
                // Text
            // Text
            case 't':
                // To distinguish items with ellipses, we temporary replace them with ***
                $data = str_replace('...', '***', $data);
                if (strpos($data, '.')) {
                    $temp = explode('.', $data);
                    $is = '=';
                    if (wppa_opt('ss_text_max')) {
                        if (substr($temp['0'], -3) == '***') {
                            $temp['0'] = substr($temp['0'], 0, strlen($temp['0']) - 3) . '%';
                            $is = 'LIKE';
                        }
                    }
                    $query = $wpdb->prepare("SELECT * FROM `" . WPPA_INDEX . "` WHERE `slug` " . $is . " %s", $temp['0']);
                    $indexes = $wpdb->get_results($query, ARRAY_A);
                    $ids = array();
                    foreach ($indexes as $item) {
                        $ids = array_merge($ids, explode('.', wppa_expand_enum($item['photos'])));
                    }
                    $i = '1';
                    while ($i < count($temp)) {
                        $is = '=';
                        if (wppa_opt('ss_text_max')) {
                            if (substr($temp[$i], -3) == '***') {
                                $temp[$i] = substr($temp[$i], 0, strlen($temp[$i]) - 3) . '%';
                                $is = 'LIKE';
                            }
                        }
                        $query = $wpdb->prepare("SELECT * FROM `" . WPPA_INDEX . "` WHERE `slug` " . $is . " %s", $temp[$i]);
                        $indexes = $wpdb->get_results($query, ARRAY_A);
                        $deltaids = array();
                        foreach ($indexes as $item) {
                            $deltaids = array_merge($deltaids, explode('.', wppa_expand_enum($item['photos'])));
                        }
                        $ids = array_intersect($ids, $deltaids);
                        $i++;
                    }
                } else {
                    $is = '=';
                    if (wppa_opt('ss_text_max')) {
                        if (substr($data, -3) == '***') {
                            $data = substr($data, 0, strlen($data) - 3) . '%';
                            $is = 'LIKE';
                        }
                    }
                    $query = $wpdb->prepare("SELECT * FROM `" . WPPA_INDEX . "` WHERE `slug` " . $is . " %s", $data);
                    $indexes = $wpdb->get_results($query, ARRAY_A);
                    $ids = array();
                    foreach ($indexes as $item) {
                        $ids = array_merge($ids, explode('.', wppa_expand_enum($item['photos'])));
                    }
                }
                if (empty($ids)) {
                    $ids = array('0');
                    // Dummy
                }
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $status . " AND `id` IN (" . implode(',', $ids) . ")";
                $thumbs = $wpdb->get_results($query, ARRAY_A);
                wppa_dbg_q('Q_SS');
                break;
                // Iptc
            // Iptc
            case 'i':
                $itag = str_replace('H', '#', $ss_data['2']);
                $desc = $ss_data['3'];
                $query = $wpdb->prepare("SELECT * FROM `" . WPPA_IPTC . "` WHERE `tag` = %s AND `description` = %s", $itag, $desc);
                $iptclines = $wpdb->get_results($query, ARRAY_A);
                $ids = '0';
                if (is_array($iptclines)) {
                    foreach ($iptclines as $item) {
                        $ids .= ',' . $item['photo'];
                    }
                }
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $status . " AND `id` IN (" . $ids . ")";
                $thumbs = $wpdb->get_results($query, ARRAY_A);
                wppa_dbg_q('Q_SS');
                break;
                // Exif
            // Exif
            case 'e':
                $etag = str_replace('H', '#', $ss_data['2']);
                $desc = $ss_data['3'];
                $query = $wpdb->prepare("SELECT * FROM `" . WPPA_EXIF . "` WHERE `tag` = %s AND `description` = %s", $etag, $desc);
                $exiflines = $wpdb->get_results($query, ARRAY_A);
                $ids = '0';
                if (is_array($exiflines)) {
                    foreach ($exiflines as $item) {
                        $ids .= ',' . $item['photo'];
                    }
                }
                $query = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $status . " AND `id` IN (" . $ids . ")";
                $thumbs = $wpdb->get_results($query, ARRAY_A);
                wppa_dbg_q('Q_SS');
                break;
        }
    } elseif ($wppa['src']) {
        // Searching
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        $searchstring = $wppa['searchstring'];
        if (isset($wppa_session['use_searchstring']) && !empty($wppa_session['use_searchstring'])) {
            $searchstring = $wppa_session['use_searchstring'];
        }
        // Indexed search??
        //		if ( wppa_switch( 'indexed_search' ) ) {
        $final_array = array();
        $chunks = explode(',', stripslashes(strtolower($searchstring)));
        // all chunks
        foreach ($chunks as $chunk) {
            if (strlen(trim($chunk))) {
                $words = wppa_index_raw_to_words($chunk);
                $photo_array = array();
                // all words in the searchstring
                foreach ($words as $word) {
                    $word = trim($word);
                    if (strlen($word) > 1) {
                        if (strlen($word) > 20) {
                            $word = substr($word, 0, 20);
                        }
                        if (wppa_switch('wild_front')) {
                            $pidxs = $wpdb->get_results("SELECT `slug`, `photos` FROM `" . WPPA_INDEX . "` WHERE `slug` LIKE '%" . $word . "%'", ARRAY_A);
                        } else {
                            $pidxs = $wpdb->get_results("SELECT `slug`, `photos` FROM `" . WPPA_INDEX . "` WHERE `slug` LIKE '" . $word . "%'", ARRAY_A);
                        }
                        wppa_dbg_q('Q-IX');
                        $photos = '';
                        if ($pidxs) {
                            foreach ($pidxs as $pi) {
                                $photos .= $pi['photos'] . ',';
                            }
                        }
                        $photo_array[] = wppa_index_string_to_array(trim($photos, ','));
                    }
                }
                // Must meet all words: intersect photo sets
                foreach (array_keys($photo_array) as $idx) {
                    if ($idx > '0') {
                        $photo_array['0'] = array_intersect($photo_array['0'], $photo_array[$idx]);
                    }
                }
                // Save partial result
                if (isset($photo_array['0'])) {
                    $final_array = array_merge($final_array, $photo_array['0']);
                }
            }
        }
        // Compose WHERE clause
        $selection = " `id` = '0' ";
        $count = '0';
        foreach (array_keys($final_array) as $p) {
            if (wppa_opt('max_search_photos')) {
                $selection .= "OR `id` = '" . $final_array[$p] . "' ";
                $count++;
            }
        }
        // Get them, depending of 'pending' criteria
        if (current_user_can('wppa_moderate')) {
            $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $selection . wppa_get_photo_order('0'), ARRAY_A);
        } else {
            $thumbs = $wpdb->get_results("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $status . " AND ( " . $selection . " ) " . wppa_get_photo_order('0'), ARRAY_A);
        }
        wppa_dbg_q('Q-SR');
        // Check on seperate albums?
        if (wppa_switch('excl_sep')) {
            $broken = false;
            foreach (array_keys($thumbs) as $idx) {
                $alb = $thumbs[$idx]['album'];
                if (wppa_is_separate($alb)) {
                    unset($thumbs[$idx]);
                    $broken = true;
                }
            }
            // Sequence broken?, create new indexes for thumbs array
            if ($broken) {
                $temp = $thumbs;
                $thumbs = array();
                foreach ($temp as $item) {
                    $thumbs[] = $item;
                }
            }
        }
        //		}
        /*
        		else { // Conventional search
        
        			if ( current_user_can( 'wppa_moderate' ) ) {
        				$q = "SELECT * FROM `" . WPPA_PHOTOS . "` " . wppa_get_photo_order( '0' );
        			}
        			else {
        				$q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `status` <> 'pending' AND `status` <> 'scheduled' " . wppa_get_photo_order( '0' );
        			}
        			$tmbs = $wpdb->get_results( $q, ARRAY_A );
        			wppa_dbg_q( 'Q-SR' );
        			
        			$thumbs = array();
        			foreach ( $tmbs as $thumb ) {
        				if ( ! wppa_switch( 'excl_sep' ) || ! wppa_is_separate( $thumb['album'] ) ) {	// Not exclude sepreate or not seperate
        					$haystack = __( $thumb['name'] ).' '.wppa_filter_exif( wppa_filter_iptc( __( stripslashes( $thumb['description'] ) ),$thumb['id'] ),$thumb['id'] );
        					if ( wppa_switch( 'search_tags' ) ) {
        						$haystack .= ' '.str_replace( ',', ' ', $thumb['tags'] );
        					}
        					if ( wppa_switch( 'search_comments' ) ) {
        						$comms = $wpdb->get_results( $wpdb->prepare( " SELECT * FROM `".WPPA_COMMENTS."` WHERE `photo` = %s", $thumb['id'] ), ARRAY_A );
        						wppa_dbg_q( 'Q-CM' );
        						if ( $comms ) foreach ( $comms as $comm ) {
        							$haystack .= $comm['comment'];
        						}
        					}
        					if ( wppa_deep_stristr( strtolower( $haystack ), $searchstring ) ) {
        						$thumbs[] = $thumb;
        					}
        				}
        			}
        		} // end conventional search
        */
        // Rootsearch?
        if ($wppa['is_rootsearch'] && isset($wppa_session['search_root'])) {
            // Find all albums below root
            $root = $wppa_session['search_root'];
            $albs = array($root);
            $albs = array_merge($albs, wppa_get_all_children($root));
            // Now remove the thumbs that are not in any of these albums
            $c1 = count($thumbs);
            $broken = false;
            foreach (array_keys($thumbs) as $idx) {
                if (!in_array($thumbs[$idx]['album'], $albs)) {
                    unset($thumbs[$idx]);
                    $broken = true;
                }
            }
            $c2 = count($thumbs);
            wppa_dbg_msg('Rootsearch thumbs:' . $c1 . ' -> ' . $c2);
            // Sequence broken?, create new indexes for thumbs array. required for filmstrip to be able to use the cached thumbs. It relies on uninterrupted sequence
            if ($broken) {
                $temp = $thumbs;
                $thumbs = array();
                foreach ($temp as $item) {
                    $thumbs[] = $item;
                }
            }
        }
        // Check maximum
        if (is_array($thumbs) && count($thumbs) > wppa_opt('max_search_photos') && wppa_opt('max_search_photos') != '0') {
            $alert_text = sprintf(__a('There are %s photos found. Only the first %s will be shown. Please refine your search criteria.'), count($thumbs), wppa_opt('max_search_photos'));
            wppa_alert($alert_text);
            foreach (array_keys($thumbs) as $idx) {
                if ($idx >= wppa_opt('max_search_photos')) {
                    unset($thumbs[$idx]);
                }
            }
        }
        $wppa['any'] = !empty($thumbs);
    } else {
        // Not search, normal
        // Init $thumbs
        $thumbs = array();
        // Status
        $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
        if (!is_user_logged_in()) {
            $status .= " AND `status` <> 'private'";
        }
        // On which album( s )?
        if (strpos($wppa['start_album'], '.') !== false) {
            $allalb = wppa_series_to_array($wppa['start_album']);
        } else {
            $allalb = false;
        }
        wppa_dbg_msg('Startalbum = ' . $wppa['start_album'], 'red');
        /* Nieuwe versie */
        // All albums ?
        if ($wppa['start_album'] == -2) {
            if (current_user_can('wppa_moderate')) {
                $q = "SELECT * FROM `" . WPPA_PHOTOS . "` " . wppa_get_photo_order('0');
            } else {
                $q = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE ( ( " . $status . " ) OR `owner` = %s ) " . wppa_get_photo_order('0'), wppa_get_user());
            }
            wppa_dbg_msg('Q-PH1 ' . $q);
            wppa_dbg_q('Q-PH1');
            $thumbs = $wpdb->get_results($q, ARRAY_A);
        } elseif (wppa_is_int($wppa['start_album'])) {
            if (current_user_can('wppa_moderate')) {
                $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = " . $wppa['start_album'] . " " . wppa_get_photo_order($wppa['start_album']);
            } else {
                $q = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE ( ( " . $status . " ) OR `owner` = %s ) AND `album` = " . $wppa['start_album'] . " " . wppa_get_photo_order($wppa['start_album']), wppa_get_user());
            }
            wppa_dbg_msg('Q-PH2 ' . $q);
            wppa_dbg_q('Q-PH2');
            $thumbs = $wpdb->get_results($q, ARRAY_A);
        } elseif (is_array($allalb)) {
            $wherealbum = ' `album` IN (' . implode(',', $allalb) . ') ';
            if (current_user_can('wppa_moderate')) {
                $q = "SELECT * FROM `" . WPPA_PHOTOS . "` WHERE " . $wherealbum . " " . wppa_get_photo_order('0');
            } else {
                $q = $wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE ( ( " . $status . " ) OR `owner` = %s ) AND " . $wherealbum . " " . wppa_get_photo_order('0'), wppa_get_user());
            }
            wppa_dbg_msg('Q-PH3 ' . $q);
            wppa_dbg_q('Q-PH3');
            $thumbs = $wpdb->get_results($q, ARRAY_A);
        } else {
            $thumbs = array();
        }
    }
    $wppa['thumb_count'] = empty($thumbs) ? '0' : count($thumbs);
    $time += microtime(true);
    wppa_dbg_msg('Get thumbs took ' . $time . ' seconds, found: ' . $wppa['thumb_count'] . ' items.');
    wppa_cache_photo('add', $thumbs);
    return $thumbs;
}
function wppa_ajax_callback()
{
    global $wpdb;
    global $wppa_session;
    global $wppa_log_file;
    wppa('ajax', true);
    wppa('error', '0');
    wppa('out', '');
    $wppa_session['page']--;
    $wppa_session['ajax']++;
    wppa_save_session();
    // ALTHOUGH IF WE ARE HERE AS FRONT END VISITOR, is_admin() is true.
    // So, $wppa_opt switches are 'yes' or 'no' and not true or false.
    // So, always use the function wppa_switch( $slug ) to test on a bool setting
    // Globally check query args to prevent php injection
    $wppa_args = array('album', 'photo', 'slide', 'cover', 'occur', 'woccur', 'searchstring', 'topten', 'lasten', 'comten', 'featen', 'single', 'photos-only', 'debug', 'relcount', 'upldr', 'owner', 'rootsearch');
    foreach ($_REQUEST as $arg) {
        if (in_array(str_replace('wppa-', '', $arg), $wppa_args)) {
            if (strpos($arg, '<?') !== false) {
                die('Security check failure #91');
            }
            if (strpos($arg, '?>') !== false) {
                die('Security check failure #92');
            }
        }
    }
    wppa_vfy_arg('wppa-action', true);
    wppa_vfy_arg('photo-id');
    wppa_vfy_arg('comment-id');
    wppa_vfy_arg('moccur');
    wppa_vfy_arg('comemail', true);
    wppa_vfy_arg('comname', true);
    wppa_vfy_arg('tag', true);
    $wppa_action = $_REQUEST['wppa-action'];
    switch ($wppa_action) {
        case 'getssiptclist':
            $tag = str_replace('H', '#', $_REQUEST['tag']);
            $mocc = $_REQUEST['moccur'];
            $oldvalue = '';
            if (strpos($wppa_session['supersearch'], ',') !== false) {
                $ss_data = explode(',', $wppa_session['supersearch']);
                if (count($ss_data) == '4') {
                    if ($ss_data['0'] == 'p') {
                        if ($ss_data['1'] == 'i') {
                            if ($ss_data['2'] == $_REQUEST['tag']) {
                                $oldvalue = $ss_data['3'];
                            }
                        }
                    }
                }
            }
            $iptcdata = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_IPTC . "` WHERE `photo` > '0' AND `tag` = %s ORDER BY `description`", $tag), ARRAY_A);
            $last = '';
            $any = false;
            if (is_array($iptcdata)) {
                foreach ($iptcdata as $item) {
                    $desc = sanitize_text_field($item['description']);
                    $desc = str_replace(array(chr(0), chr(1), chr(2), chr(3), chr(4), chr(5), chr(6), chr(7)), '', $desc);
                    if ($desc != $last) {
                        $sel = $oldvalue && $oldvalue == $desc ? 'selected="selected"' : '';
                        if ($sel) {
                            echo 'selected:' . $oldvalue;
                        }
                        $ddesc = strlen($desc) > '32' ? substr($desc, 0, 30) . '...' : $desc;
                        echo '<option' . ' value="' . esc_attr($desc) . '"' . ' class="wppa-iptclist-' . $mocc . '"' . ' ' . $sel . ' >' . $ddesc . '</option>';
                        $last = $desc;
                        $any = true;
                    }
                }
            }
            if (!$any) {
                $query = $wpdb->prepare("DELETE FROM `" . WPPA_IPTC . "` WHERE `photo` = '0' AND `tag` = %s", $tag);
                $wpdb->query($query);
                //				wppa_log( 'dbg', $query );
            }
            wppa_exit();
            break;
        case 'getssexiflist':
            $tag = str_replace('H', '#', $_REQUEST['tag']);
            $mocc = $_REQUEST['moccur'];
            $oldvalue = '';
            if (strpos($wppa_session['supersearch'], ',') !== false) {
                $ss_data = explode(',', $wppa_session['supersearch']);
                if (count($ss_data) == '4') {
                    if ($ss_data['0'] == 'p') {
                        if ($ss_data['1'] == 'e') {
                            if ($ss_data['2'] == $_REQUEST['tag']) {
                                $oldvalue = $ss_data['3'];
                            }
                        }
                    }
                }
            }
            $exifdata = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_EXIF . "` WHERE `photo` > '0' AND `tag` = %s ORDER BY `description`", $tag), ARRAY_A);
            $last = '';
            $any = false;
            if (is_array($exifdata)) {
                foreach ($exifdata as $item) {
                    $desc = sanitize_text_field($item['description']);
                    $desc = str_replace(array(chr(0), chr(1), chr(2), chr(3), chr(4), chr(5), chr(6), chr(7)), '', $desc);
                    if ($desc != $last) {
                        $sel = $oldvalue && $oldvalue == $desc ? 'selected="selected"' : '';
                        $ddesc = strlen($desc) > '32' ? substr($desc, 0, 30) . '...' : $desc;
                        echo '<option' . ' value="' . esc_attr($desc) . '"' . ' class="wppa-exiflist-' . $mocc . '"' . ' ' . $sel . ' >' . $ddesc . '</option>';
                        $last = $desc;
                        $any = true;
                    }
                }
            }
            if (!$any) {
                $query = $wpdb->prepare("DELETE FROM `" . WPPA_EXIF . "` WHERE `photo` = '0' AND `tag` = %s", $tag);
                $wpdb->query($query);
                //				wppa_log( 'dbg', $query );
            }
            wppa_exit();
            break;
        case 'front-edit':
            // Fetch the html for edit dialog
            // Is the call valid?
            if (!isset($_REQUEST['photo-id'])) {
                die('Missing required argument');
            }
            if (strlen($_REQUEST['photo-id']) == 12) {
                $photo = wppa_decrypt_photo($_REQUEST['photo-id']);
            } else {
                $photo = $_REQUEST['photo-id'];
            }
            // Is this user aloowed to edit thisphoto?
            $ok = wppa_may_user_fe_edit($photo);
            // No rights, die
            if (!$ok) {
                die('You do not have sufficient rights to do this');
            }
            // Do it
            require_once 'wppa-photo-admin-autosave.php';
            // New style?
            if (wppa_opt('upload_edit') == 'new') {
                wppa_fe_edit_new_style($photo);
            }
            // Old style
            if (wppa_opt('upload_edit') == 'classic') {
                wppa('front_edit', true);
                echo '	<div style="padding-bottom:4px;height:24px;" >
							<span style="color:#777;" >
								<i>' . __('All modifications are instantly updated on the server.<br />The <b style="color:#070" >Remark</b> field keeps you informed on the actions taken at the background.', 'wp-photo-album-plus') . '</i>
							</span>
							<input id="wppa-fe-exit" type="button" style="float:right;color:red;font-weight:bold;" onclick="window.opener.location.reload( true );window.close();" value="' . __('Exit & Refresh', 'wp-photo-album-plus') . '" />
							<div id="wppa-fe-count" style="float:right;" ></div>
						</div><div style="clear:both;"></div>';
                wppa_album_photos('', $photo);
            }
            // Done
            wppa_exit();
            break;
        case 'update-photo-new':
            // Do the actual edit update
            // Get photo id
            if (strlen($_REQUEST['photo-id']) == 12) {
                $photo = wppa_decrypt_photo($_REQUEST['photo-id']);
            } else {
                $photo = $_REQUEST['photo-id'];
            }
            // Is the call valid?
            $nonce = $_REQUEST['wppa-nonce'];
            if (!wp_verify_nonce($nonce, 'wppa-nonce-' . $photo)) {
                die('Security check falure');
            }
            if (!isset($_REQUEST['photo-id'])) {
                die('Missing required argument');
            }
            // Name
            if (isset($_POST['name'])) {
                $name = strip_tags($_POST['name']);
                wppa_update_photo(array('id' => $photo, 'name' => $name));
            }
            // Description
            if (isset($_POST['description'])) {
                $desc = str_replace(array('<br/>', '<br>'), '<br />', $_POST['description']);
                $desc = balanceTags($desc, true);
                wppa_update_photo(array('id' => $photo, 'description' => $_POST['description']));
            }
            // Tags
            if (isset($_POST['tags'])) {
                $tags = wppa_sanitize_tags($_POST['tags']);
                wppa_update_photo(array('id' => $photo, 'tags' => $_POST['tags']));
            }
            // Custom fields
            $custom = wppa_get_photo_item($photo, 'custom');
            if ($custom) {
                $custom_data = unserialize($custom);
            } else {
                $custom_data = array('', '', '', '', '', '', '', '', '', '');
            }
            for ($i = 0; $i < 10; $i++) {
                if (isset($_POST['custom_' . $i]) && wppa_opt('custom_caption_' . $i) && wppa_switch('custom_edit_' . $i)) {
                    $custom_data[$i] = wppa_sanitize_custom_field($_POST['custom_' . $i]);
                }
            }
            $custom = serialize($custom_data);
            wppa_update_photo(array('id' => $photo, 'custom' => $custom, 'modified' => time()));
            // Housekeeping
            wppa_index_update('photo', $photo);
            wppa_exit();
            break;
        case 'do-comment':
            // Security check
            $mocc = $_REQUEST['moccur'];
            $nonce = $_REQUEST['wppa-nonce'];
            if (!wp_verify_nonce($nonce, 'wppa-nonce-' . $mocc)) {
                _e('Security check failure', 'wp-photo-album-plus');
                wppa_exit();
            }
            // Correct the fact that this is a non-admin operation, if it is only
            if (is_admin()) {
                require_once 'wppa-non-admin.php';
            }
            wppa('mocc', $_REQUEST['moccur']);
            wppa('comment_photo', isset($_REQUEST['photo-id']) ? $_REQUEST['photo-id'] : '0');
            wppa('comment_id', isset($_REQUEST['comment-edit']) ? $_REQUEST['comment-edit'] : '0');
            $comment_allowed = !wppa_switch('comment_login') || is_user_logged_in();
            if (wppa_switch('show_comments') && $comment_allowed) {
                //				if ( wppa_switch( 'search_comments' ) ) wppa_index_remove( 'photo', $_REQUEST['photo-id'] );
                wppa_do_comment($_REQUEST['photo-id']);
                // Process the comment
                if (wppa_switch('search_comments')) {
                    wppa_index_update('photo', $_REQUEST['photo-id']);
                }
            }
            wppa('no_esc', true);
            echo wppa_comment_html($_REQUEST['photo-id'], $comment_allowed);
            // Retrieve the new commentbox content
            wppa_exit();
            break;
        case 'import':
            require_once 'wppa-import.php';
            _wppa_page_import();
            wppa_exit();
            break;
        case 'approve':
            $iret = '0';
            if (!current_user_can('wppa_moderate') && !current_user_can('wppa_comments')) {
                _e('You do not have the rights to moderate photos this way', 'wp-photo-album-plus');
                wppa_exit();
            }
            if (isset($_REQUEST['photo-id']) && current_user_can('wppa_moderate')) {
                $iret = $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_PHOTOS . "` SET `status` = 'publish' WHERE `id` = %s", $_REQUEST['photo-id']));
                wppa_flush_upldr_cache('photoid', $_REQUEST['photo-id']);
                $alb = $wpdb->get_var($wpdb->prepare("SELECT `album` FROM `" . WPPA_PHOTOS . "` WHERE `id` = %s", $_REQUEST['photo-id']));
                wppa_clear_taglist();
                wppa_flush_treecounts($alb);
            }
            if (isset($_REQUEST['comment-id'])) {
                $iret = $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_COMMENTS . "` SET `status` = 'approved' WHERE `id` = %s", $_REQUEST['comment-id']));
                if ($iret) {
                    wppa_send_comment_approved_email($_REQUEST['comment-id']);
                    wppa_add_credit_points(wppa_opt('cp_points_comment_appr'), __('Photo comment approved', 'wp-photo-album-plus'), $_REQUEST['photo-id'], '', wppa_get_photo_item($_REQUEST['photo-id'], 'owner'));
                }
            }
            if ($iret) {
                echo 'OK';
            } else {
                if (isset($_REQUEST['photo-id'])) {
                    if (current_user_can('wppa_moderate')) {
                        echo sprintf(__('Failed to update stutus of photo %s', 'wp-photo-album-plus'), $_REQUEST['photo-id']) . "\n" . __('Please refresh the page', 'wp-photo-album-plus');
                    } else {
                        _e('Security check failure', 'wp-photo-album-plus');
                    }
                }
                if (isset($_REQUEST['comment-id'])) {
                    echo sprintf(__('Failed to update stutus of comment %s', 'wp-photo-album-plus'), $_REQUEST['comment-id']) . "\n" . __('Please refresh the page', 'wp-photo-album-plus');
                }
            }
            wppa_exit();
        case 'remove':
            if (isset($_REQUEST['photo-id'])) {
                // Remove photo
                if (strlen($_REQUEST['photo-id']) == 12) {
                    $photo = wppa_decrypt_photo($_REQUEST['photo-id']);
                } else {
                    $photo = $_REQUEST['photo-id'];
                }
                if (wppa_may_user_fe_edit($photo)) {
                    // Frontend edit may also delete
                    wppa_delete_photo($photo);
                    echo 'OK||' . __('Photo removed', 'wp-photo-album-plus');
                    wppa_exit();
                }
            }
            if (!current_user_can('wppa_moderate') && !current_user_can('wppa_comments')) {
                _e('You do not have the rights to moderate photos this way', 'wp-photo-album-plus');
                wppa_exit();
            }
            if (isset($_REQUEST['photo-id'])) {
                // Remove photo
                if (strlen($_REQUEST['photo-id']) == 12) {
                    $photo = wppa_decrypt_photo($_REQUEST['photo-id']);
                } else {
                    $photo = $_REQUEST['photo-id'];
                }
                if (!current_user_can('wppa_moderate')) {
                    _e('Security check failure', 'wp-photo-album-plus');
                    wppa_exit();
                }
                wppa_delete_photo($photo);
                echo 'OK||' . __('Photo removed', 'wp-photo-album-plus');
                wppa_exit();
            }
            if (isset($_REQUEST['comment-id'])) {
                // Remove comment
                $iret = $wpdb->query($wpdb->prepare("DELETE FROM `" . WPPA_COMMENTS . "` WHERE `id`= %s", $_REQUEST['comment-id']));
                if ($iret) {
                    echo 'OK||' . __('Comment removed', 'wp-photo-album-plus');
                } else {
                    _e('Could not remove comment', 'wp-photo-album-plus');
                }
                wppa_exit();
            }
            _e('Unexpected error', 'wp-photo-album-plus');
            wppa_exit();
        case 'downloadalbum':
            // Feature enabled?
            if (!wppa_switch('allow_download_album')) {
                echo '||ER||' . __('This feature is not enabled on this website', 'wp-photo-album-plus');
                wppa_exit();
            }
            // Validate args
            $alb = wppa_decrypt_album($_REQUEST['album-id']);
            $status = "`status` <> 'pending' AND `status` <> 'scheduled'";
            if (!is_user_logged_in()) {
                $status .= " AND `status` <> 'private'";
            }
            $photos = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = %s AND ( ( " . $status . " ) OR owner = %s ) " . wppa_get_photo_order($alb), $alb, wppa_get_user()), ARRAY_A);
            if (!$photos) {
                echo '||ER||' . __('The album is empty', 'wp-photo-album-plus');
                wppa_exit();
            }
            // Remove obsolete files
            wppa_delete_obsolete_tempfiles();
            // Open zipfile
            if (!class_exists('ZipArchive')) {
                echo '||ER||' . __('Unable to create zip archive', 'wp-photo-album-plus');
                wppa_exit();
            }
            $zipfilename = wppa_get_album_name($alb);
            $zipfilename = wppa_sanitize_file_name($zipfilename . '.zip');
            // Remove illegal chars
            $zipfilepath = WPPA_UPLOAD_PATH . '/temp/' . $zipfilename;
            if (is_file($zipfilepath)) {
                //		unlink( $zipfilepath );	// Debug
            }
            $wppa_zip = new ZipArchive();
            $iret = $wppa_zip->open($zipfilepath, 1);
            if ($iret !== true) {
                echo '||ER||' . sprintf(__('Unable to create zip archive. code = %s', 'wp-photo-album-plus'), $iret);
                wppa_exit();
            }
            // Add photos to zip
            $stop = false;
            foreach ($photos as $p) {
                if (wppa_is_time_up()) {
                    wppa_log('obs', 'Time up during album to zip creation');
                    $stop = true;
                } else {
                    $id = $p['id'];
                    if (!wppa_is_multi($id)) {
                        $source = wppa_switch('download_album_source') && is_file(wppa_get_source_path($id)) ? wppa_get_source_path($id) : wppa_get_photo_path($id);
                        if (is_file($source)) {
                            $dest = $p['filename'] ? wppa_sanitize_file_name($p['filename']) : wppa_sanitize_file_name(wppa_strip_ext($p['name']) . '.' . $p['ext']);
                            $dest = wppa_fix_poster_ext($dest, $id);
                            $iret = $wppa_zip->addFile($source, $dest);
                            // To prevent too may files open, and to have at least a file when there are too many photos, close and re-open
                            $wppa_zip->close();
                            $wppa_zip->open($zipfilepath);
                            // wppa_log( 'dbg', 'Added ' . basename($source) . ' to ' . basename($zipfilepath));
                        }
                    }
                }
                if ($stop) {
                    break;
                }
            }
            // Close zip and return
            $zipcount = $wppa_zip->numFiles;
            $wppa_zip->close();
            // A zip is created
            $desturl = WPPA_UPLOAD_URL . '/temp/' . $zipfilename;
            echo $desturl . '||OK||';
            if ($zipcount != count($photos)) {
                echo sprintf(__('Only %s out of %s photos could be added to the zipfile', 'wp-photo-album-plus'), $zipcount, count($photos));
            }
            wppa_exit();
            break;
        case 'getalbumzipurl':
            $alb = $_REQUEST['album-id'];
            $zipfilename = wppa_get_album_name($alb);
            $zipfilename = wppa_sanitize_file_name($zipfilename . '.zip');
            // Remove illegal chars
            $zipfilepath = WPPA_UPLOAD_PATH . '/temp/' . $zipfilename;
            $zipfileurl = WPPA_UPLOAD_URL . '/temp/' . $zipfilename;
            if (is_file($zipfilepath)) {
                echo $zipfileurl;
            } else {
                echo 'ER';
            }
            wppa_exit();
            break;
        case 'addtozip':
            // Check if the user is allowed to do this
            $photo = wppa_decrypt_photo($_REQUEST['photo-id']);
            if (!wppa_user_is('administrator')) {
                echo 'ER||Security check failure';
                wppa_exit();
            }
            // Do we have ziparchive on board?
            if (!class_exists('ZipArchive')) {
                echo 'ER||' . __('Unable to create zip archive', 'wp-photo-album-plus');
                wppa_exit();
            }
            // Verify existance of zips dir
            $zipsdir = WPPA_UPLOAD_PATH . '/zips/';
            if (!is_dir($zipsdir)) {
                wppa_mkdir($zipsdir);
            }
            if (!is_dir($zipsdir)) {
                echo 'ER||' . __('Unable to create zipsdir', 'wp-photo-album-plus');
                wppa_exit();
            }
            // Compose the users zip filename
            $zipfile = $zipsdir . wppa_get_user() . '.zip';
            // Find the photo data
            $data = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `id` = %s", $photo), ARRAY_A);
            // Find the photo file
            if (is_file(wppa_get_source_path($photo))) {
                $source = wppa_get_source_path($photo);
            } else {
                $source = wppa_get_photo_path($photo);
            }
            $source = wppa_fix_poster_ext($source, $photo);
            // Add photo to zip
            $wppa_zip = new ZipArchive();
            $wppa_zip->open($zipfile, 1);
            $wppa_zip->addFile($source, wppa_fix_poster_ext($data['filename'], $photo));
            $wppa_zip->close();
            echo 'OK||' . __('Selected', 'wp-photo-album-plus');
            wppa_exit();
            break;
        case 'delmyzip':
            // Verify existance of zips dir
            $zipsdir = WPPA_UPLOAD_PATH . '/zips/';
            if (is_dir($zipsdir)) {
                // Compose the users zip filename
                $zipfile = $zipsdir . wppa_get_user() . '.zip';
                // Check file existance and remove
                if (is_file($zipfile)) {
                    @unlink($zipfile);
                }
            }
            wppa_exit();
            break;
        case 'makeorigname':
            $photo = wppa_decrypt_photo($_REQUEST['photo-id']);
            $from = $_REQUEST['from'];
            if ($from == 'fsname') {
                $type = wppa_opt('art_monkey_link');
            } elseif ($from == 'popup') {
                $type = wppa_opt('art_monkey_popup_link');
            } else {
                echo '||7||' . __('Unknown source of request', 'wp-photo-album-plus');
                wppa_exit();
            }
            $data = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `id` = %s", $photo), ARRAY_A);
            if ($data) {
                // The photo is supposed to exist
                // Make the name
                if ($data['filename']) {
                    $name = $data['filename'];
                } else {
                    $name = __($data['name'], 'wp-photo-album-plus');
                }
                $name = wppa_sanitize_file_name($name);
                // Remove illegal chars
                $name = preg_replace('/\\.[^.]*$/', '', $name);
                // Remove file extension
                if (strlen($name) == '0') {
                    echo '||1||' . __('Empty filename', 'wp-photo-album-plus');
                    wppa_exit();
                }
                // Make the file
                if (wppa_switch('artmonkey_use_source')) {
                    if (is_file(wppa_get_source_path($photo))) {
                        $source = wppa_get_source_path($photo);
                    } else {
                        $source = wppa_get_photo_path($photo);
                    }
                } else {
                    $source = wppa_get_photo_path($photo);
                }
                $source = wppa_fix_poster_ext($source, $photo);
                // Fix the extension for mm items.
                if ($data['ext'] == 'xxx') {
                    $data['ext'] = wppa_get_ext($source);
                }
                $dest = WPPA_UPLOAD_PATH . '/temp/' . $name . '.' . $data['ext'];
                $zipfile = WPPA_UPLOAD_PATH . '/temp/' . $name . '.zip';
                $tempdir = WPPA_UPLOAD_PATH . '/temp';
                if (!is_dir($tempdir)) {
                    wppa_mkdir($tempdir);
                }
                if (!is_dir($tempdir)) {
                    echo '||2||' . __('Unable to create tempdir', 'wp-photo-album-plus');
                    wppa_exit();
                }
                // Remove obsolete files
                wppa_delete_obsolete_tempfiles();
                // Make the files
                if ($type == 'file') {
                    copy($source, $dest);
                    $ext = $data['ext'];
                } elseif ($type == 'zip') {
                    if (!class_exists('ZipArchive')) {
                        echo '||8||' . __('Unable to create zip archive', 'wp-photo-album-plus');
                        wppa_exit();
                    }
                    $ext = 'zip';
                    $wppa_zip = new ZipArchive();
                    $wppa_zip->open($zipfile, 1);
                    $wppa_zip->addFile($source, basename($dest));
                    $wppa_zip->close();
                } else {
                    echo '||6||' . __('Unknown type', 'wp-photo-album-plus');
                    wppa_exit();
                }
                $desturl = WPPA_UPLOAD_URL . '/temp/' . $name . '.' . $ext;
                echo '||0||' . $desturl;
                // No error: return url
                wppa_exit();
            } else {
                echo '||9||' . __('The photo does no longer exist', 'wp-photo-album-plus');
                wppa_exit();
            }
            wppa_exit();
            break;
        case 'tinymcedialog':
            $result = wppa_make_tinymce_dialog();
            echo $result;
            wppa_exit();
            break;
        case 'bumpviewcount':
            $nonce = $_REQUEST['wppa-nonce'];
            if (wp_verify_nonce($nonce, 'wppa-check')) {
                wppa_bump_viewcount('photo', $_REQUEST['wppa-photo']);
            } else {
                _e('Security check failure', 'wp-photo-album-plus');
            }
            wppa_exit();
            break;
        case 'bumpclickcount':
            $nonce = $_REQUEST['wppa-nonce'];
            $photo = isset($_REQUEST['wppa-photo']) ? $_REQUEST['wppa-photo'] : 0;
            if (strlen($photo) == 12) {
                $photo = wppa_decrypt_photo($photo);
            }
            if ($photo && wp_verify_nonce($nonce, 'wppa-check')) {
                wppa_bump_clickcount($photo);
            } else {
                _e('Security check failure', 'wp-photo-album-plus');
            }
            wppa_exit();
            break;
        case 'rate':
            // Correct the fact that this is a non-admin operation, if it is only
            if (is_admin()) {
                require_once 'wppa-non-admin.php';
            }
            // Get commandline args
            $photo = wppa_decrypt_photo($_REQUEST['wppa-rating-id']);
            $rating = $_REQUEST['wppa-rating'];
            $occur = $_REQUEST['wppa-occur'];
            $index = $_REQUEST['wppa-index'];
            $nonce = $_REQUEST['wppa-nonce'];
            // Make errortext
            $errtxt = __('An error occurred while processing you rating request.', 'wp-photo-album-plus');
            $errtxt .= "\n" . __('Maybe you opened the page too long ago to recognize you.', 'wp-photo-album-plus');
            $errtxt .= "\n" . __('You may refresh the page and try again.', 'wp-photo-album-plus');
            $wartxt = __('Althoug an error occurred while processing your rating, your vote has been registered.', 'wp-photo-album-plus');
            $wartxt .= "\n" . __('However, this may not be reflected in the current pageview', 'wp-photo-album-plus');
            // Check on validity
            if (!wp_verify_nonce($nonce, 'wppa-check')) {
                echo '0||100||' . $errtxt;
                wppa_exit();
                // Nonce check failed
            }
            if (wppa_opt('rating_max') == '1' && $rating != '1') {
                echo '0||106||' . $errtxt . ':' . $rating;
                wppa_exit();
                // Value out of range
            } elseif (wppa_opt('rating_max') == '5' && !in_array($rating, array('-1', '1', '2', '3', '4', '5'))) {
                echo '0||106||' . $errtxt . ':' . $rating;
                wppa_exit();
                // Value out of range
            } elseif (wppa_opt('rating_max') == '10' && !in_array($rating, array('-1', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'))) {
                echo '0||106||' . $errtxt . ':' . $rating;
                wppa_exit();
                // Value out of range
            }
            // Check for one rating per period
            $wait_text = wppa_get_rating_wait_text($photo, wppa_get_user());
            if ($wait_text) {
                echo '0||900||' . $wait_text;
                // 900 is recoverable error
                wppa_exit();
            }
            // Get other data
            if (!wppa_photo_exists($photo)) {
                echo '0||999||' . __('Photo has been removed.', 'wp-photo-album-plus');
                wppa_exit();
            }
            $mylast = wppa_get_my_last_vote($photo);
            $myavgrat = '0';
            // Init
            $user = wppa_get_user();
            // Rate own photo?
            if (wppa_get_photo_item($photo, 'owner') == $user && !wppa_switch('allow_owner_votes')) {
                echo '0||900||' . __('Sorry, you can not rate your own photos', 'wp-photo-album-plus');
                wppa_exit();
            }
            // Already a pending one?
            $pending = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_RATING . "` WHERE `photo` = %s AND `user` = %s AND `status` = %s", $photo, $user, 'pending'));
            // Has user motivated his vote?
            $hascommented = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_COMMENTS . "` WHERE `photo` = %s AND `user` = %s", $photo, wppa_get_user('display')));
            if ($pending) {
                if (!$hascommented) {
                    echo '0||900||' . __('Please enter a comment.', 'wp-photo-album-plus');
                    wppa_exit();
                } else {
                    $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_RATING . "` SET `status` = 'publish' WHERE `photo` = %s AND `user` = %s", $photo, $user));
                }
            }
            if (wppa_switch('vote_needs_comment')) {
                $ratingstatus = $hascommented ? 'publish' : 'pending';
            } else {
                $ratingstatus = 'publish';
            }
            // When done, we have to echo $occur.'||'.$photo.'||'.$index.'||'.$myavgrat.'||'.$allavgrat.'||'.$discount.'||'.$hascommented.'||'.$message;
            // So we have to do: process rating and find new $myavgrat, $allavgrat and $discount ( $occur, $photo and $index are known )
            // Error message format: '0||<errcode>||<errtext>
            // errcode = 900: user error, other codes: real errors
            // Case -1: Likes only
            if (wppa_opt('rating_display_type') == 'likes') {
                // If i liked this, i do no longer like this
                if ($mylast) {
                    // Remove my like
                    $wpdb->query($wpdb->prepare("DELETE FROM `" . WPPA_RATING . "` WHERE `photo` = %s AND `user` = %s", $photo, $user));
                    $myavgrat = '0';
                } else {
                    // Add my like
                    wppa_create_rating_entry(array('photo' => $photo, 'value' => '1', 'user' => $user));
                    $myavgrat = '1';
                }
                // Update photo data
                wppa_rate_photo($photo);
                // Get callback data
                $lt = wppa_get_like_title_a($photo);
                $allavgratcombi = $lt['title'] . '|' . $lt['display'];
                // Output and quit
                echo $occur . '||' . $photo . '||' . $index . '||' . $myavgrat . '||' . esc_attr($allavgratcombi) . '||||||likes';
                wppa_exit();
            } elseif ($mylast) {
                // I did vote already
                // Can vote only once
                if (!wppa_switch('rating_change') && !wppa_switch('rating_multi')) {
                    echo '0||900||' . __('You can not change your vote', 'wp-photo-album-plus');
                    wppa_exit();
                }
                // I did a dislike, can not modify
                if ($mylast < '0') {
                    echo '0||900||' . __('You can not change a dislike', 'wp-photo-album-plus');
                    wppa_exit();
                }
                // I did a rating, can not change into dislike
                if ($mylast > '0' && $rating == '-1') {
                    echo '0||900||' . __('You can not change your vote into a dislike', 'wp-photo-album-plus');
                    wppa_exit();
                }
            }
            // Case 1: value = -1 this is a legal dislike vote
            if ($rating == '-1') {
                // Add my dislike
                $iret = wppa_create_rating_entry(array('photo' => $photo, 'value' => $rating, 'user' => $user, 'status' => $ratingstatus));
                if (!$iret) {
                    echo '0||101||' . $errtxt;
                    wppa_exit();
                    // Fail on storing vote
                }
                // Add points
                wppa_add_credit_points(wppa_opt('cp_points_rating'), __('Photo rated', 'wp-photo-album-plus'), $photo, $rating);
                // Check for email to be sent every .. dislikes
                wppa_dislike_check($photo);
                // Photo is removed?
                if (!is_file(wppa_get_thumb_path($photo))) {
                    echo $occur . '||' . $photo . '||' . $index . '||-1||-1|0||' . wppa_opt('dislike_delete');
                    wppa_exit();
                }
            } elseif (!$mylast) {
                // Add my vote
                $iret = wppa_create_rating_entry(array('photo' => $photo, 'value' => $rating, 'user' => $user, 'status' => $ratingstatus));
                if (!$iret) {
                    echo '0||102||' . $errtxt;
                    wppa_exit();
                    // Fail on storing vote
                }
                // Add points
                wppa_add_credit_points(wppa_opt('cp_points_rating'), __('Photo rated', 'wp-photo-album-plus'), $photo, $rating);
            } elseif (wppa_switch('rating_change')) {
                // Votechanging is allowed
                $iret = $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_RATING . '` SET `value` = %s WHERE `photo` = %s AND `user` = %s LIMIT 1', $rating, $photo, $user));
                if ($iret === false) {
                    echo '0||103||' . $errtxt;
                    wppa_exit();
                    // Fail on update
                }
            } elseif (wppa_switch('rating_multi')) {
                // Rating multi is allowed
                $iret = wppa_create_rating_entry(array('photo' => $photo, 'value' => $rating, 'user' => $user, 'status' => $ratingstatus));
                if (!$iret) {
                    echo '0||104||' . $errtxt;
                    wppa_exit();
                    // Fail on storing vote
                }
            } else {
                // Should never get here....
                echo '0||110||' . __('Unexpected error', 'wp-photo-album-plus');
                wppa_exit();
            }
            // Compute my avg rating
            $myrats = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . WPPA_RATING . '`  WHERE `photo` = %s AND `user` = %s AND `status` = %s ', $photo, $user, 'publish'), ARRAY_A);
            if ($myrats) {
                $sum = 0;
                $cnt = 0;
                foreach ($myrats as $rat) {
                    if ($rat['value'] == '-1') {
                        $sum += wppa_opt('dislike_value');
                    } else {
                        $sum += $rat['value'];
                    }
                    $cnt++;
                }
                $myavgrat = $sum / $cnt;
                $i = wppa_opt('rating_prec');
                $j = $i + '1';
                $myavgrat = sprintf('%' . $j . '.' . $i . 'f', $myavgrat);
            } else {
                $myavgrat = '0';
            }
            // Compute new allavgrat
            $ratings = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . WPPA_RATING . ' WHERE `photo` = %s AND `status` = %s', $photo, 'publish'), ARRAY_A);
            if ($ratings) {
                $sum = 0;
                $cnt = 0;
                foreach ($ratings as $rat) {
                    if ($rat['value'] == '-1') {
                        $sum += wppa_opt('dislike_value');
                    } else {
                        $sum += $rat['value'];
                    }
                    $cnt++;
                }
                $allavgrat = $sum / $cnt;
                if ($allavgrat == '10') {
                    $allavgrat = '9.99999999';
                }
                // For sort order reasons text field
            } else {
                $allavgrat = '0';
            }
            // Store it in the photo info
            $iret = $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_PHOTOS . '` SET `mean_rating` = %s WHERE `id` = %s', $allavgrat, $photo));
            if ($iret === false) {
                echo '0||106||' . $wartxt;
                wppa_exit();
                // Fail on save
            }
            // Compute rating_count and store in the photo info
            $ratcount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_RATING . "` WHERE `photo` = %s", $photo));
            if ($ratcount !== false) {
                $iret = $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_PHOTOS . "` SET `rating_count` = %s WHERE `id` = %s", $ratcount, $photo));
                if ($iret === false) {
                    echo '0||107||' . $wartxt;
                    wppa_exit();
                    // Fail on save
                }
            }
            // Format $allavgrat for output
            $allavgratcombi = $allavgrat . '|' . $ratcount;
            // Compute dsilike count
            $discount = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM `" . WPPA_RATING . "` WHERE `photo` = %s AND `value` = -1 AND `status` = %s", $photo, 'publish'));
            if ($discount === false) {
                echo '0||108||' . $wartxt;
                wppa_exit();
                // Fail on save
            }
            $distext = wppa_get_distext($discount, $rating);
            if (!$distext) {
                $distext = '0';
            }
            // Test for possible medal
            wppa_test_for_medal($photo);
            // Success!
            wppa_clear_cache();
            if (wppa_switch('vote_needs_comment') && !$hascommented) {
                $message = __("Please explain your vote in a comment.\nYour vote will be discarded if you don't.\n\nAfter completing your comment,\nyou can refresh the page to see\nyour vote became effective.", 'wp-photo-album-plus');
            } else {
                $message = '';
            }
            echo $occur . '||' . $photo . '||' . $index . '||' . $myavgrat . '||' . $allavgratcombi . '||' . $distext . '||' . $hascommented . '||' . $message;
            break;
        case 'render':
            $tim_1 = microtime(true);
            $nq_1 = get_num_queries();
            // Correct the fact that this is a non-admin operation, if it is
            if (is_admin()) {
                require_once 'wppa-non-admin.php';
            }
            wppa_load_theme();
            // Register geo shortcode if google-maps-gpx-vieuwer is on board. GPX does it in wp_head(), what is not done in an ajax call
            //			if ( function_exists( 'gmapv3' ) ) add_shortcode( 'map', 'gmapv3' );
            // Get the post we are working for
            if (isset($_REQUEST['wppa-fromp'])) {
                $p = $_REQUEST['wppa-fromp'];
                if (wppa_is_int($p)) {
                    $GLOBALS['post'] = get_post($p);
                }
            }
            // Render
            $result = wppa_albums();
            $result = wppa_make_relative($result);
            echo $result;
            $tim_2 = microtime(true);
            $nq_2 = get_num_queries();
            $mem = memory_get_peak_usage(true) / 1024 / 1024;
            $msg = sprintf('WPPA Ajax render: db queries: WP:%d, WPPA+: %d in %4.2f seconds, using %4.2f MB memory max', $nq_1, $nq_2 - $nq_1, $tim_2 - $tim_1, $mem);
            echo '<script type="text/javascript">wppaConsoleLog( \'' . $msg . '\', \'force\' )</script>';
            break;
        case 'delete-photo':
            $photo = $_REQUEST['photo-id'];
            $nonce = $_REQUEST['wppa-nonce'];
            // Check validity
            if (!wp_verify_nonce($nonce, 'wppa_nonce_' . $photo)) {
                echo '||0||' . __('You do not have the rights to delete a photo', 'wp-photo-album-plus');
                wppa_exit();
                // Nonce check failed
            }
            if (!is_numeric($photo)) {
                echo '||0||' . __('Security check failure', 'wp-photo-album-plus');
                wppa_exit();
                // Nonce check failed
            }
            $album = $wpdb->get_var($wpdb->prepare('SELECT `album` FROM `' . WPPA_PHOTOS . '` WHERE `id` = %s', $photo));
            wppa_delete_photo($photo);
            wppa_clear_cache();
            echo '||1||<span style="color:red" >' . sprintf(__('Photo %s has been deleted', 'wp-photo-album-plus'), $photo) . '</span>';
            echo '||';
            $a = wppa_allow_uploads($album);
            if (!$a) {
                echo 'full';
            } else {
                echo 'notfull||' . $a;
            }
            break;
        case 'update-album':
            $album = $_REQUEST['album-id'];
            $nonce = $_REQUEST['wppa-nonce'];
            $item = $_REQUEST['item'];
            $value = $_REQUEST['value'];
            $value = wppa_decode($value);
            // Check validity
            if (!wp_verify_nonce($nonce, 'wppa_nonce_' . $album)) {
                echo '||0||' . __('You do not have the rights to update album information', 'wp-photo-album-plus') . $nonce;
                wppa_exit();
                // Nonce check failed
            }
            switch ($item) {
                case 'clear_ratings':
                    $photos = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . WPPA_PHOTOS . '` WHERE `album` = %s', $album), ARRAY_A);
                    if ($photos) {
                        foreach ($photos as $photo) {
                            $iret1 = $wpdb->query($wpdb->prepare('DELETE FROM `' . WPPA_RATING . '` WHERE `photo` = %s', $photo['id']));
                            $iret2 = $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_PHOTOS . '` SET `mean_rating` = %s WHERE `id` = %s', '', $photo['id']));
                        }
                    }
                    if ($photos && $iret1 !== false && $iret2 !== false) {
                        echo '||0||' . __('Ratings cleared', 'wp-photo-album-plus') . '||' . __('No ratings for this photo.', 'wp-photo-album-plus');
                    } elseif ($photos) {
                        echo '||1||' . __('An error occurred while clearing ratings', 'wp-photo-album-plus');
                    } else {
                        echo '||0||' . __('No photos in this album', 'wp-photo-album-plus') . '||' . __('No ratings for this photo.', 'wp-photo-album-plus');
                    }
                    wppa_exit();
                    break;
                case 'set_deftags':
                    // to be changed for large albums
                    $photos = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . WPPA_PHOTOS . '` WHERE `album` = %s', $album), ARRAY_A);
                    $deftag = $wpdb->get_var($wpdb->prepare('SELECT `default_tags` FROM `' . WPPA_ALBUMS . '` WHERE `id` = %s', $album));
                    if (is_array($photos)) {
                        foreach ($photos as $photo) {
                            $tags = wppa_sanitize_tags(wppa_filter_iptc(wppa_filter_exif($deftag, $photo['id']), $photo['id']));
                            $iret = $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_PHOTOS . '` SET `tags` = %s WHERE `id` = %s', $tags, $photo['id']));
                            wppa_index_update('photo', $photo['id']);
                        }
                    }
                    if ($photos && $iret !== false) {
                        echo '||0||' . __('Tags set to defaults', 'wp-photo-album-plus');
                    } elseif ($photos) {
                        echo '||1||' . __('An error occurred while setting tags', 'wp-photo-album-plus');
                    } else {
                        echo '||0||' . __('No photos in this album', 'wp-photo-album-plus');
                    }
                    wppa_clear_taglist();
                    wppa_exit();
                    break;
                case 'add_deftags':
                    $photos = $wpdb->get_results($wpdb->prepare('SELECT * FROM `' . WPPA_PHOTOS . '` WHERE `album` = %s', $album), ARRAY_A);
                    $deftag = $wpdb->get_var($wpdb->prepare('SELECT `default_tags` FROM `' . WPPA_ALBUMS . '` WHERE `id` = %s', $album));
                    if (is_array($photos)) {
                        foreach ($photos as $photo) {
                            $tags = wppa_sanitize_tags(wppa_filter_iptc(wppa_filter_exif($photo['tags'] . ',' . $deftag, $photo['id']), $photo['id']));
                            $iret = $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_PHOTOS . '` SET `tags` = %s WHERE `id` = %s', $tags, $photo['id']));
                            wppa_index_update('photo', $photo['id']);
                        }
                    }
                    if ($photos && $iret !== false) {
                        echo '||0||' . __('Tags added with defaults', 'wp-photo-album-plus');
                    } elseif ($photos) {
                        echo '||1||' . __('An error occurred while adding tags', 'wp-photo-album-plus');
                    } else {
                        echo '||0||' . __('No photos in this album', 'wp-photo-album-plus');
                    }
                    wppa_clear_taglist();
                    wppa_exit();
                    break;
                case 'inherit_cats':
                case 'inhadd_cats':
                    $albids = wppa_expand_enum(wppa_alb_to_enum_children($album));
                    $albarr = explode('.', $albids);
                    $cats = wppa_get_album_item($album, 'cats');
                    if ($cats || $item == 'inherit_cats') {
                        if (count($albarr) > 1) {
                            foreach ($albarr as $alb) {
                                if ($album != $alb) {
                                    if ($item == 'inherit_cats') {
                                        wppa_update_album(array('id' => $alb, 'cats' => $cats));
                                    } else {
                                        // 'inhadd_cats'
                                        $mycats = wppa_get_album_item($alb, 'cats');
                                        wppa_update_album(array('id' => $alb, 'cats' => $mycats . $cats));
                                    }
                                }
                            }
                        } else {
                            echo '||0||' . __('No subalbums found to process', 'wp-photo-album-plus');
                            wppa_exit();
                        }
                    } else {
                        echo '||0||' . __('No categories found to process', 'wp-photo-album-plus');
                        wppa_exit();
                    }
                    $n = count($albarr) - 1;
                    echo '||0||' . sprintf(_n('%d album updated', '%d albums updated', $n, 'wp-photo-album-plus'), $n);
                    wppa_exit();
                    break;
                case 'name':
                    $value = trim(strip_tags($value));
                    if (!wppa_sanitize_file_name($value)) {
                        // Empty album name is not allowed
                        $value = 'Album-#' . $album;
                        echo '||5||' . sprintf(__('Album name may not be empty.<br />Reset to %s', 'wp-photo-album-plus'), $value);
                    }
                    $itemname = __('Name', 'wp-photo-album-plus');
                    break;
                case 'description':
                    $itemname = __('Description', 'wp-photo-album-plus');
                    if (wppa_switch('check_balance')) {
                        $value = str_replace(array('<br/>', '<br>'), '<br />', $value);
                        if (balanceTags($value, true) != $value) {
                            echo '||3||' . __('Unbalanced tags in album description!', 'wp-photo-album-plus');
                            wppa_exit();
                        }
                    }
                    $value = trim($value);
                    break;
                case 'a_order':
                    $itemname = __('Album order #', 'wp-photo-album-plus');
                    break;
                case 'main_photo':
                    $itemname = __('Cover photo', 'wp-photo-album-plus');
                    break;
                case 'a_parent':
                    $itemname = __('Parent album', 'wp-photo-album-plus');
                    wppa_flush_treecounts($album);
                    // Myself and my parents
                    wppa_flush_treecounts($value);
                    // My new parent
                    break;
                case 'p_order_by':
                    $itemname = __('Photo order', 'wp-photo-album-plus');
                    break;
                case 'alt_thumbsize':
                    $itemname = __('Use Alt thumbsize', 'wp-photo-album-plus');
                    break;
                case 'cover_type':
                    $itemname = __('Cover Type', 'wp-photo-album-plus');
                    break;
                case 'cover_linktype':
                    $itemname = __('Link type', 'wp-photo-album-plus');
                    break;
                case 'cover_linkpage':
                    $itemname = __('Link to', 'wp-photo-album-plus');
                    break;
                case 'owner':
                    $itemname = __('Owner', 'wp-photo-album-plus');
                    if ($value != '--- public ---' && !get_user_by('login', $value)) {
                        echo '||4||' . sprintf(__('User %s does not exist', 'wp-photo-album-plus'), $value);
                        wppa_exit();
                    }
                    break;
                case 'upload_limit_count':
                    wppa_ajax_check_range($value, false, '0', false, __('Upload limit count', 'wp-photo-album-plus'));
                    if (wppa('error')) {
                        wppa_exit();
                    }
                    $oldval = $wpdb->get_var($wpdb->prepare('SELECT `upload_limit` FROM ' . WPPA_ALBUMS . ' WHERE `id` = %s', $album));
                    $temp = explode('/', $oldval);
                    $value = $value . '/' . $temp[1];
                    $item = 'upload_limit';
                    $itemname = __('Upload limit count', 'wp-photo-album-plus');
                    break;
                case 'upload_limit_time':
                    $oldval = $wpdb->get_var($wpdb->prepare('SELECT `upload_limit` FROM ' . WPPA_ALBUMS . ' WHERE `id` = %s', $album));
                    $temp = explode('/', $oldval);
                    $value = $temp[0] . '/' . $value;
                    $item = 'upload_limit';
                    $itemname = __('Upload limit time', 'wp-photo-album-plus');
                    break;
                case 'default_tags':
                    $value = wppa_sanitize_tags($value, false, true);
                    $itemname = __('Default tags', 'wp-photo-album-plus');
                    break;
                case 'cats':
                    $value = wppa_sanitize_cats($value);
                    wppa_clear_catlist();
                    $itemname = __('Categories', 'wp-photo-album-plus');
                    break;
                case 'suba_order_by':
                    $itemname = __('Sub albums sort order', 'wp-photo-album-plus');
                    break;
                case 'year':
                case 'month':
                case 'day':
                case 'hour':
                case 'min':
                    $itemname = __('Schedule date/time', 'wp-photo-album-plus');
                    $scheduledtm = $wpdb->get_var($wpdb->prepare("SELECT `scheduledtm` FROM`" . WPPA_ALBUMS . "` WHERE `id` = %s", $album));
                    if (!$scheduledtm) {
                        $scheduledtm = wppa_get_default_scheduledtm();
                    }
                    $temp = explode(',', $scheduledtm);
                    if ($item == 'year') {
                        $temp[0] = $value;
                    }
                    if ($item == 'month') {
                        $temp[1] = $value;
                    }
                    if ($item == 'day') {
                        $temp[2] = $value;
                    }
                    if ($item == 'hour') {
                        $temp[3] = $value;
                    }
                    if ($item == 'min') {
                        $temp[4] = $value;
                    }
                    $scheduledtm = implode(',', $temp);
                    wppa_update_album(array('id' => $album, 'scheduledtm' => $scheduledtm));
                    echo '||0||' . sprintf(__('%s of album %s updated', 'wp-photo-album-plus'), $itemname, $album);
                    wppa_exit();
                    break;
                case 'setallscheduled':
                    $scheduledtm = $wpdb->get_var($wpdb->prepare("SELECT `scheduledtm` FROM `" . WPPA_ALBUMS . "` WHERE `id` = %s", $album));
                    if ($scheduledtm) {
                        $iret = $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_PHOTOS . "` SET `status` = 'scheduled', `scheduledtm` = %s WHERE `album` = %s", $scheduledtm, $album));
                        echo '||0||' . __('All photos set to scheduled per date', 'wp-photo-album-plus') . ' ' . wppa_format_scheduledtm($scheduledtm);
                    }
                    wppa_exit();
                    break;
                case 'album_custom_0':
                case 'album_custom_1':
                case 'album_custom_2':
                case 'album_custom_3':
                case 'album_custom_4':
                case 'album_custom_5':
                case 'album_custom_6':
                case 'album_custom_7':
                case 'album_custom_8':
                case 'album_custom_9':
                    $index = substr($item, -1);
                    $custom = wppa_get_album_item($album, 'custom');
                    if ($custom) {
                        $custom_data = unserialize($custom);
                    } else {
                        $custom_data = array('', '', '', '', '', '', '', '', '', '');
                    }
                    $custom_data[$index] = wppa_sanitize_custom_field($value);
                    $custom = serialize($custom_data);
                    wppa_update_album(array('id' => $album, 'custom' => $custom, 'modified' => time()));
                    wppa_index_update('album', $album);
                    echo '||0||' . sprintf(__('Custom field %s updated', 'wp-photo-album-plus'), wppa_opt('album_custom_caption_' . $index));
                    wppa_exit();
                    break;
                default:
                    $itemname = $item;
            }
            $query = $wpdb->prepare('UPDATE ' . WPPA_ALBUMS . ' SET `' . $item . '` = %s WHERE `id` = %s', $value, $album);
            $iret = $wpdb->query($query);
            if ($iret !== false) {
                if ($item == 'name' || $item == 'description' || $item == 'cats') {
                    wppa_index_update('album', $album);
                }
                if ($item == 'name') {
                    wppa_create_pl_htaccess();
                }
                echo '||0||' . sprintf(__('%s of album %s updated', 'wp-photo-album-plus'), $itemname, $album);
                if ($item == 'upload_limit') {
                    echo '||';
                    $a = wppa_allow_uploads($album);
                    if (!$a) {
                        echo 'full';
                    } else {
                        echo 'notfull||' . $a;
                    }
                }
            } else {
                echo '||2||' . sprintf(__('An error occurred while trying to update %s of album %s', 'wp-photo-album-plus'), $itemname, $album);
                echo '<br>' . __('Press CTRL+F5 and try again.', 'wp-photo-album-plus');
            }
            wppa_clear_cache();
            wppa_exit();
            break;
        case 'update-comment-status':
            $photo = $_REQUEST['wppa-photo-id'];
            $nonce = $_REQUEST['wppa-nonce'];
            $comid = $_REQUEST['wppa-comment-id'];
            $comstat = $_REQUEST['wppa-comment-status'];
            // Check validity
            if (!wp_verify_nonce($nonce, 'wppa_nonce_' . $photo)) {
                echo '||0||' . __('You do not have the rights to update comment status', 'wp-photo-album-plus') . $nonce;
                wppa_exit();
                // Nonce check failed
            }
            $iret = $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_COMMENTS . '` SET `status` = %s WHERE `id` = %s', $comstat, $comid));
            if (wppa_switch('search_comments')) {
                wppa_index_update('photo', $photo);
            }
            if ($iret !== false) {
                if ($comstat == 'approved') {
                    wppa_send_comment_approved_email($comid);
                    wppa_add_credit_points(wppa_opt('cp_points_comment_appr'), __('Photo comment approved', 'wp-photo-album-plus'), $photo, '', wppa_get_photo_item($photo, 'owner'));
                }
                echo '||0||' . sprintf(__('Status of comment #%s updated', 'wp-photo-album-plus'), $comid);
            } else {
                echo '||1||' . sprintf(__('Error updating status comment #%s', 'wp-photo-album-plus'), $comid);
            }
            wppa_exit();
            break;
        case 'watermark-photo':
            $photo = $_REQUEST['photo-id'];
            $nonce = $_REQUEST['wppa-nonce'];
            // Check validity
            if (!wp_verify_nonce($nonce, 'wppa_nonce_' . $photo)) {
                echo '||1||' . __('You do not have the rights to change photos', 'wp-photo-album-plus');
                wppa_exit();
                // Nonce check failed
            }
            wppa_cache_thumb($photo);
            if (wppa_add_watermark($photo)) {
                if (wppa_switch('watermark_thumbs')) {
                    wppa_create_thumbnail($photo);
                    // create new thumb
                }
                echo '||0||' . __('Watermark applied', 'wp-photo-album-plus');
                wppa_exit();
            } else {
                echo '||1||' . __('An error occured while trying to apply a watermark', 'wp-photo-album-plus');
                wppa_exit();
            }
        case 'update-photo':
            if (strlen($_REQUEST['photo-id']) == 12) {
                $photo = wppa_decrypt_photo($_REQUEST['photo-id']);
            } else {
                $photo = $_REQUEST['photo-id'];
            }
            //			$photo = $_REQUEST['photo-id'];
            $nonce = $_REQUEST['wppa-nonce'];
            $item = $_REQUEST['item'];
            $value = isset($_REQUEST['value']) ? $_REQUEST['value'] : '';
            $value = wppa_decode($value);
            // Check validity
            if (!wp_verify_nonce($nonce, 'wppa_nonce_' . $photo)) {
                echo '||0||' . __('You do not have the rights to update photo information', 'wp-photo-album-plus');
                wppa_exit();
                // Nonce check failed
            }
            if (substr($item, 0, 20) == 'wppa_watermark_file_' || substr($item, 0, 19) == 'wppa_watermark_pos_') {
                wppa_update_option($item, $value);
                echo '||0||' . sprintf(__('%s updated to %s.', 'wp-photo-album-plus'), $item, $value);
                wppa_exit();
            }
            switch ($item) {
                case 'exifdtm':
                    $format = '0000:00:00 00:00:00';
                    $err = '0';
                    // Length ok?
                    if (strlen($value) != 19) {
                        $err = '1';
                    }
                    // Check on digits, colons and space
                    for ($i = 0; $i < 19; $i++) {
                        $d = substr($value, $i, 1);
                        $f = substr($format, $i, 1);
                        switch ($f) {
                            case '0':
                                if (!in_array($d, array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'))) {
                                    $err = '2';
                                }
                                break;
                            case ':':
                            case ' ':
                                if ($d != $f) {
                                    $err = '3';
                                }
                                break;
                        }
                    }
                    // Check on values if format correct, report first error only
                    if (!$err) {
                        $temp = explode(':', str_replace(' ', ':', $value));
                        if ($temp['0'] < '1970') {
                            $err = '11';
                        }
                        // Before UNIX epoch
                        if (!$err && $temp['0'] > date('Y')) {
                            $err = '12';
                        }
                        // Future
                        if (!$err && $temp['1'] < '1') {
                            $err = '13';
                        }
                        // Before january
                        if (!$err && $temp['1'] > '12') {
                            $err = '14';
                        }
                        // After december
                        if (!$err && $temp['2'] < '1') {
                            $err = '15';
                        }
                        // Before first of month
                        if (!$err && $temp['2'] > '31') {
                            $err = '17';
                        }
                        // After 31st ( forget about feb and months with 30 days )
                        if (!$err && $temp['3'] < '1') {
                            $err = '18';
                        }
                        // Before first hour
                        if (!$err && $temp['3'] > '24') {
                            $err = '19';
                        }
                        // Hour > 24
                        if (!$err && $temp['4'] < '1') {
                            $err = '20';
                        }
                        // Min < 1
                        if (!$err && $temp['4'] > '59') {
                            $err = '21';
                        }
                        // Min > 59
                        if (!$err && $temp['5'] < '1') {
                            $err = '22';
                        }
                        // Sec < 1
                        if (!$err && $temp['5'] > '59') {
                            $err = '23';
                        }
                        // Sec > 59
                    }
                    if ($err) {
                        echo '||1||' . sprintf(__('Format error %s. Must be yyyy:mm:dd hh:mm:ss', 'wp-photo-album-plus'), $err);
                    } else {
                        wppa_update_photo(array('id' => $photo, 'exifdtm' => $value));
                        echo '||0||' . __('Exif date/time updated', 'wp-photo-album-plus');
                    }
                    wppa_exit();
                    break;
                case 'lat':
                    if (!is_numeric($value) || $value < '-90.0' || $value > '90.0') {
                        echo '||1||' . __('Enter a value > -90 and < 90', 'wp-photo-album-plus');
                        wppa_exit();
                    }
                    $photodata = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . WPPA_PHOTOS . ' WHERE `id` = %s', $photo), ARRAY_A);
                    $geo = $photodata['location'] ? $photodata['location'] : '///';
                    $geo = explode('/', $geo);
                    $geo = wppa_format_geo($value, $geo['3']);
                    $iret = $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_PHOTOS . '` SET `location` = %s WHERE `id` = %s', $geo, $photo));
                    if ($iret) {
                        echo '||0||' . __('Lattitude updated', 'wp-photo-album-plus');
                    } else {
                        echo '||1||' . __('Could not update lattitude', 'wp-photo-album-plus');
                    }
                    wppa_exit();
                    break;
                case 'lon':
                    if (!is_numeric($value) || $value < '-180.0' || $value > '180.0') {
                        echo '||1||' . __('Enter a value > -180 and < 180', 'wp-photo-album-plus');
                        wppa_exit();
                    }
                    $photodata = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . WPPA_PHOTOS . ' WHERE `id` = %s', $photo), ARRAY_A);
                    $geo = $photodata['location'] ? $photodata['location'] : '///';
                    $geo = explode('/', $geo);
                    $geo = wppa_format_geo($geo['2'], $value);
                    $iret = $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_PHOTOS . '` SET `location` = %s WHERE `id` = %s', $geo, $photo));
                    if ($iret) {
                        echo '||0||' . __('Longitude updated', 'wp-photo-album-plus');
                    } else {
                        echo '||1||' . __('Could not update longitude', 'wp-photo-album-plus');
                    }
                    wppa_exit();
                    break;
                case 'remake':
                    if (wppa_remake_files('', $photo)) {
                        wppa_bump_photo_rev();
                        wppa_bump_thumb_rev();
                        echo '||0||' . __('Photo files remade', 'wp-photo-album-plus');
                    } else {
                        echo '||2||' . __('Could not remake files', 'wp-photo-album-plus');
                    }
                    wppa_exit();
                    break;
                case 'remakethumb':
                    if (wppa_create_thumbnail($photo)) {
                        echo '||0||' . __('Thumbnail remade', 'wp-photo-album-plus');
                    } else {
                        echo '||0||' . __('Could not remake thumbnail', 'wp-photo-album-plus');
                    }
                    wppa_exit();
                    break;
                case 'rotright':
                case 'rot180':
                case 'rotleft':
                case 'flip':
                    switch ($item) {
                        case 'rotleft':
                            $angle = '90';
                            $dir = __('left', 'wp-photo-album-plus');
                            break;
                        case 'rot180':
                            $angle = '180';
                            $dir = __('180&deg;', 'wp-photo-album-plus');
                            break;
                        case 'rotright':
                            $angle = '270';
                            $dir = __('right', 'wp-photo-album-plus');
                            break;
                        case 'flip':
                            $angle = '360';
                            $dir = '';
                            break;
                    }
                    wppa('error', wppa_rotate($photo, $angle));
                    if (!wppa('error')) {
                        wppa_update_modified($photo);
                        wppa_bump_photo_rev();
                        wppa_bump_thumb_rev();
                        if ($angle == '360') {
                            echo '||0||' . sprintf(__('Photo flipped', 'wp-photo-album-plus'), $photo);
                        } else {
                            echo '||0||' . sprintf(__('Photo %s rotated %s', 'wp-photo-album-plus'), $photo, $dir);
                        }
                    } else {
                        echo '||' . wppa('error') . '||' . sprintf(__('An error occurred while trying to rotate or flip photo %s', 'wp-photo-album-plus'), $photo);
                    }
                    wppa_exit();
                    break;
                case 'moveto':
                    $photodata = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . WPPA_PHOTOS . ' WHERE `id` = %s', $photo), ARRAY_A);
                    if (wppa_switch('void_dups')) {
                        // Check for already exists
                        $exists = wppa_file_is_in_album($photodata['filename'], $value);
                        if ($exists) {
                            // Already exists
                            echo '||3||' . sprintf(__('A photo with filename %s already exists in album %s.', 'wp-photo-album-plus'), $photodata['filename'], $value);
                            wppa_exit();
                            break;
                        }
                    }
                    if (!wppa_album_exists($value)) {
                        echo '||4||' . sprintf(__('Album %s does not exist', 'wp-photo-album-plus'), $value);
                        wppa_exit();
                        break;
                    }
                    wppa_flush_treecounts($photodata['album']);
                    // Current album
                    wppa_flush_treecounts($value);
                    // New album
                    $iret = $wpdb->query($wpdb->prepare('UPDATE ' . WPPA_PHOTOS . ' SET `album` = %s WHERE `id` = %s', $value, $photo));
                    if ($iret !== false) {
                        wppa_move_source($photodata['filename'], $photodata['album'], $value);
                        echo '||99||' . sprintf(__('Photo %s has been moved to album %s (%s)', 'wp-photo-album-plus'), $photo, wppa_get_album_name($value), $value);
                    } else {
                        echo '||3||' . sprintf(__('An error occurred while trying to move photo %s', 'wp-photo-album-plus'), $photo);
                    }
                    wppa_exit();
                    break;
                case 'copyto':
                    $photodata = $wpdb->get_row($wpdb->prepare('SELECT * FROM ' . WPPA_PHOTOS . ' WHERE `id` = %s', $photo), ARRAY_A);
                    if (wppa_switch('void_dups')) {
                        // Check for already exists
                        $exists = wppa_file_is_in_album($photodata['filename'], $value);
                        if ($exists) {
                            // Already exists
                            echo '||4||' . sprintf(__('A photo with filename %s already exists in album %s.', 'wp-photo-album-plus'), $photodata['filename'], $value);
                            wppa_exit();
                            break;
                        }
                    }
                    if (!wppa_album_exists($value)) {
                        echo '||4||' . sprintf(__('Album %s does not exist', 'wp-photo-album-plus'), $value);
                        wppa_exit();
                        break;
                    }
                    wppa('error', wppa_copy_photo($photo, $value));
                    wppa_flush_treecounts($value);
                    // New album
                    if (!wppa('error')) {
                        echo '||0||' . sprintf(__('Photo %s copied to album %s (%s)', 'wp-photo-album-plus'), $photo, wppa_get_album_name($value), $value);
                    } else {
                        echo '||4||' . sprintf(__('An error occurred while trying to copy photo %s', 'wp-photo-album-plus'), $photo) . ' wppa_copy_photo() returned ' . wppa('error');
                        echo '<br>' . __('Press CTRL+F5 and try again.', 'wp-photo-album-plus');
                    }
                    wppa_exit();
                    break;
                case 'status':
                    if (!current_user_can('wppa_moderate') && !current_user_can('wppa_admin')) {
                        die('Security check failure #78');
                    }
                    wppa_flush_treecounts(wppa_get_photo_item($photo, 'album'));
                    // $wpdb->get_var( $wpdb->prepare( "SELECT `album` FROM `".WPPA_PHOTOS."` WHERE `id` = %s", $photo ) ) );
                // $wpdb->get_var( $wpdb->prepare( "SELECT `album` FROM `".WPPA_PHOTOS."` WHERE `id` = %s", $photo ) ) );
                case 'owner':
                case 'name':
                case 'description':
                case 'p_order':
                case 'linkurl':
                case 'linktitle':
                case 'linktarget':
                case 'tags':
                case 'alt':
                case 'videox':
                case 'videoy':
                    switch ($item) {
                        case 'name':
                            $value = strip_tags($value);
                            $itemname = __('Name', 'wp-photo-album-plus');
                            break;
                        case 'description':
                            $itemname = __('Description', 'wp-photo-album-plus');
                            if (wppa_switch('check_balance')) {
                                $value = str_replace(array('<br/>', '<br>'), '<br />', $value);
                                if (balanceTags($value, true) != $value) {
                                    echo '||3||' . __('Unbalanced tags in photo description!', 'wp-photo-album-plus');
                                    wppa_exit();
                                }
                            }
                            break;
                        case 'p_order':
                            $itemname = __('Photo order #', 'wp-photo-album-plus');
                            break;
                        case 'owner':
                            $usr = get_user_by('login', $value);
                            if (!$usr) {
                                echo '||4||' . sprintf(__('User %s does not exists', 'wp-photo-album-plus'), $value);
                                wppa_exit();
                            }
                            $value = $usr->user_login;
                            // Correct possible case mismatch
                            wppa_flush_upldr_cache('photoid', $photo);
                            // Current owner
                            wppa_flush_upldr_cache('username', $value);
                            // New owner
                            $itemname = __('Owner', 'wp-photo-album-plus');
                            break;
                        case 'linkurl':
                            $itemname = __('Link url', 'wp-photo-album-plus');
                            break;
                        case 'linktitle':
                            $itemname = __('Link title', 'wp-photo-album-plus');
                            break;
                        case 'linktarget':
                            $itemname = __('Link target', 'wp-photo-album-plus');
                            break;
                        case 'tags':
                            $value = wppa_sanitize_tags($value, false, true);
                            $value = wppa_sanitize_tags(wppa_filter_iptc(wppa_filter_exif($value, $photo), $photo));
                            wppa_clear_taglist();
                            $itemname = __('Photo Tags', 'wp-photo-album-plus');
                            break;
                        case 'status':
                            wppa_clear_taglist();
                            wppa_flush_upldr_cache('photoid', $photo);
                            $itemname = __('Status', 'wp-photo-album-plus');
                            break;
                        case 'alt':
                            $itemname = __('HTML Alt', 'wp-photo-album-plus');
                            $value = strip_tags(stripslashes($value));
                            break;
                        case 'videox':
                            $itemname = __('Video width', 'wp-photo-album-plus');
                            if (!wppa_is_int($value) || $value < '0') {
                                echo '||3||' . __('Please enter an integer value >= 0', 'wp-photo-album-plus');
                                wppa_exit();
                            }
                            break;
                        case 'videoy':
                            $itemname = __('Video height', 'wp-photo-album-plus');
                            if (!wppa_is_int($value) || $value < '0') {
                                echo '||3||' . __('Please enter an integer value >= 0', 'wp-photo-album-plus');
                                wppa_exit();
                            }
                            break;
                        default:
                            $itemname = $item;
                    }
                    //				if ( $item == 'name' || $item == 'description' || $item == 'tags' ) wppa_index_quick_remove( 'photo', $photo );
                    $iret = $wpdb->query($wpdb->prepare('UPDATE ' . WPPA_PHOTOS . ' SET `' . $item . '` = %s WHERE `id` = %s', $value, $photo));
                    if ($item == 'name' || $item == 'description' || $item == 'tags') {
                        wppa_index_update('photo', $photo);
                    }
                    if ($item == 'status' && $value != 'scheduled') {
                        wppa_update_photo(array('id' => $photo, 'scheduledtm' => ''));
                    }
                    if ($item == 'status') {
                        wppa_flush_treecounts(wppa_get_photo_item($photo, 'album'));
                    }
                    if ($iret !== false) {
                        wppa_update_modified($photo);
                        if (wppa_is_video($photo)) {
                            echo '||0||' . sprintf(__('%s of video %s updated', 'wp-photo-album-plus'), $itemname, $photo);
                        } else {
                            echo '||0||' . sprintf(__('%s of photo %s updated', 'wp-photo-album-plus'), $itemname, $photo);
                        }
                    } else {
                        echo '||2||' . sprintf(__('An error occurred while trying to update %s of photo %s', 'wp-photo-album-plus'), $itemname, $photo);
                        wppa_exit();
                    }
                    break;
                case 'year':
                case 'month':
                case 'day':
                case 'hour':
                case 'min':
                    $itemname = __('Schedule date/time', 'wp-photo-album-plus');
                    $scheduledtm = $wpdb->get_var($wpdb->prepare("SELECT `scheduledtm` FROM`" . WPPA_PHOTOS . "` WHERE `id` = %s", $photo));
                    if (!$scheduledtm) {
                        $scheduledtm = wppa_get_default_scheduledtm();
                    }
                    $temp = explode(',', $scheduledtm);
                    if ($item == 'year') {
                        $temp[0] = $value;
                    }
                    if ($item == 'month') {
                        $temp[1] = $value;
                    }
                    if ($item == 'day') {
                        $temp[2] = $value;
                    }
                    if ($item == 'hour') {
                        $temp[3] = $value;
                    }
                    if ($item == 'min') {
                        $temp[4] = $value;
                    }
                    $scheduledtm = implode(',', $temp);
                    wppa_update_photo(array('id' => $photo, 'scheduledtm' => $scheduledtm, 'status' => 'scheduled'));
                    wppa_flush_treecounts($wpdb->get_var($wpdb->prepare("SELECT `album` FROM `" . WPPA_PHOTOS . "` WHERE `id` = %s", $photo)));
                    wppa_flush_upldr_cache('photoid', $photo);
                    if (wppa_is_video($photo)) {
                        echo '||0||' . sprintf(__('%s of video %s updated', 'wp-photo-album-plus'), $itemname, $photo);
                    } else {
                        echo '||0||' . sprintf(__('%s of photo %s updated', 'wp-photo-album-plus'), $itemname, $photo);
                    }
                    break;
                case 'custom_0':
                case 'custom_1':
                case 'custom_2':
                case 'custom_3':
                case 'custom_4':
                case 'custom_5':
                case 'custom_6':
                case 'custom_7':
                case 'custom_8':
                case 'custom_9':
                    $index = substr($item, -1);
                    $custom = wppa_get_photo_item($photo, 'custom');
                    if ($custom) {
                        $custom_data = unserialize($custom);
                    } else {
                        $custom_data = array('', '', '', '', '', '', '', '', '', '');
                    }
                    $custom_data[$index] = wppa_sanitize_custom_field($value);
                    $custom = serialize($custom_data);
                    wppa_update_photo(array('id' => $photo, 'custom' => $custom, 'modified' => time()));
                    wppa_index_update('photo', $photo);
                    echo '||0||' . sprintf(__('Custom field %s of photo %s updated', 'wp-photo-album-plus'), wppa_opt('custom_caption_' . $index), $photo);
                    break;
                case 'file':
                    // Check on upload error
                    if ($_FILES['photo']['error']) {
                        echo '||' . $_FILES['photo']['error'] . '||' . __('Error during upload.', 'wp-photo-album-plus');
                        wppa_exit();
                    }
                    // Save new source
                    $filename = wppa_get_photo_item($photo, 'filename');
                    // If very old, no filename, take new name
                    if (!$filename) {
                        $filename = $_FILES['photo']['name'];
                        wppa_update_photo(array('id' => $photo, 'filename' => $filename));
                    }
                    wppa_save_source($_FILES['photo']['tmp_name'], $filename, wppa_get_photo_item($photo, 'album'));
                    // Make proper oriented source
                    wppa_make_o1_source($photo);
                    // Make the files
                    $bret = wppa_make_the_photo_files($_FILES['photo']['tmp_name'], $photo, strtolower(wppa_get_ext($_FILES['photo']['name'])));
                    if ($bret) {
                        // Update timestamps and sizes
                        $alb = wppa_get_photo_item($photo, 'album');
                        wppa_update_album(array('id' => $alb, 'modified' => time()));
                        wppa_update_photo(array('id' => $photo, 'modified' => time(), 'thumbx' => '0', 'thumby' => '0', 'photox' => '0', 'photoy' => '0'));
                        // Report success
                        echo '||0||' . __('Photo files updated.', 'wp-photo-album-plus');
                    } else {
                        // Report fail
                        echo '||1||' . __('Could not update files.', 'wp-photo-album-plus');
                    }
                    wppa_exit();
                    break;
                case 'stereo':
                    $t = microtime(true);
                    wppa_update_photo(array('id' => $photo, 'stereo' => $value));
                    wppa_create_stereo_images($photo);
                    wppa_create_thumbnail($photo);
                    $t = microtime(true) - $t;
                    echo '||0||' . sprintf(__('Stereo mode updated in %d milliseconds', 'wp-photo-album-plus'), floor($t * 1000));
                    wppa_exit();
                    break;
                default:
                    echo '||98||This update action is not implemented yet( ' . $item . ' )';
                    wppa_exit();
            }
            wppa_clear_cache();
            break;
            // The wppa-settings page calls ajax with $wppa_action == 'update-option';
        // The wppa-settings page calls ajax with $wppa_action == 'update-option';
        case 'update-option':
            // Verify that we are legally here
            $nonce = $_REQUEST['wppa-nonce'];
            if (!wp_verify_nonce($nonce, 'wppa-nonce')) {
                echo '||1||' . __('You do not have the rights to update settings', 'wp-photo-album-plus');
                wppa_exit();
                // Nonce check failed
            }
            // Initialize
            $old_minisize = wppa_get_minisize();
            // Remember for later, maybe we do something that requires regen
            $option = 'wppa_' . $_REQUEST['wppa-option'];
            // The option to be processed
            $value = isset($_REQUEST['value']) ? wppa_decode($_REQUEST['value']) : '';
            // The new value, may also contain & # and +
            $value = stripslashes($value);
            $value = trim($value);
            // Remaove surrounding spaces
            $alert = '';
            // Init the return string data
            wppa('error', '0');
            //
            $title = '';
            //
            // Check for potd settings
            $potdarr = array('wppa_potd_title', 'wppa_potd_widget_width', 'wppa_potd_align', 'wppa_potd_linkurl', 'wppa_potd_linktitle', 'wppa_potd_subtitle', 'wppa_potd_counter', 'wppa_potd_counter_link', 'wppa_potd_album_type', 'wppa_potd_album', 'wppa_potd_include_subs', 'wppa_potd_status_filter', 'wppa_potd_inverse', 'wppa_potd_method', 'wppa_potd_period', 'wppa_potd_offset', 'wppa_potd_photo');
            if (in_array($option, $potdarr)) {
                if (!current_user_can('wppa_potd')) {
                    echo '||1||' . __('You do not have the rights to update photo of the day settings', 'wp-photo-album-plus');
                    wppa_exit();
                }
            } else {
                if (!current_user_can('wppa_settings')) {
                    echo '||1||' . __('You do not have the rights to update settings', 'wp-photo-album-plus');
                    wppa_exit();
                }
            }
            // If it is a font family, change all double quotes into single quotes as this destroys much more than you would like
            if (strpos($option, 'wppa_fontfamily_') !== false) {
                $value = str_replace('"', "'", $value);
            }
            $option = wppa_decode($option);
            // Dispatch on option
            if (substr($option, 0, 16) == 'wppa_iptc_label_') {
                $tag = substr($option, 16);
                $q = $wpdb->prepare("UPDATE `" . WPPA_IPTC . "` SET `description`=%s WHERE `tag`=%s AND `photo`='0'", $value, $tag);
                $bret = $wpdb->query($q);
                // Produce the response text
                if ($bret) {
                    $output = '||0||' . $tag . ' updated to ' . $value . '||';
                } else {
                    $output = '||1||Failed to update ' . $tag . '||';
                }
                echo $output;
                wppa_exit();
            } elseif (substr($option, 0, 17) == 'wppa_iptc_status_') {
                $tag = substr($option, 17);
                $q = $wpdb->prepare("UPDATE `" . WPPA_IPTC . "` SET `status`=%s WHERE `tag`=%s AND `photo`='0'", $value, $tag);
                $bret = $wpdb->query($q);
                // Produce the response text
                if ($bret) {
                    $output = '||0||' . $tag . ' updated to ' . $value . '||';
                } else {
                    $output = '||1||Failed to update ' . $tag . '||';
                }
                echo $output;
                wppa_exit();
            } elseif (substr($option, 0, 16) == 'wppa_exif_label_') {
                $tag = substr($option, 16);
                $q = $wpdb->prepare("UPDATE `" . WPPA_EXIF . "` SET `description`=%s WHERE `tag`=%s AND `photo`='0'", $value, $tag);
                $bret = $wpdb->query($q);
                // Produce the response text
                if ($bret) {
                    $output = '||0||' . $tag . ' updated to ' . $value . '||';
                } else {
                    $output = '||1||Failed to update ' . $tag . '||';
                }
                echo $output;
                wppa_exit();
            } elseif (substr($option, 0, 17) == 'wppa_exif_status_') {
                $tag = substr($option, 17);
                $q = $wpdb->prepare("UPDATE `" . WPPA_EXIF . "` SET `status`=%s WHERE `tag`=%s AND `photo`='0'", $value, $tag);
                $bret = $wpdb->query($q);
                // Produce the response text
                if ($bret) {
                    $output = '||0||' . $tag . ' updated to ' . $value . '||';
                } else {
                    $output = '||1||Failed to update ' . $tag . '||';
                }
                echo $output;
                wppa_exit();
            } elseif (substr($option, 0, 10) == 'wppa_caps-') {
                // Is capability setting
                global $wp_roles;
                //$R = new WP_Roles;
                $setting = explode('-', $option);
                if ($value == 'yes') {
                    $wp_roles->add_cap($setting[2], $setting[1]);
                    echo '||0||' . __('Capability granted', 'wp-photo-album-plus') . '||';
                    wppa_exit();
                } elseif ($value == 'no') {
                    $wp_roles->remove_cap($setting[2], $setting[1]);
                    echo '||0||' . __('Capability withdrawn', 'wp-photo-album-plus') . '||';
                    wppa_exit();
                } else {
                    echo '||1||Invalid value: ' . $value . '||';
                    wppa_exit();
                }
            } else {
                switch ($option) {
                    //wppa_log('obs', 'option '.$option.' attempt to set to '.$value);
                    // Changing potd_album_type ( physical / virtual ) also clears potd_album
                    case 'wppa_potd_album_type':
                        if (!in_array($value, array('physical', 'virtual'))) {
                            echo '||1||Invalid value: ' . $value . '||';
                            wppa_exit();
                        }
                        if ($value == 'physical') {
                            wppa_update_option('wppa_potd_album', '');
                        } else {
                            wppa_update_option('wppa_potd_album', 'all');
                        }
                        break;
                    case 'wppa_potd_album':
                        if (wppa_opt('potd_album_type') == 'physical') {
                            $value = str_replace('.', ',', wppa_expand_enum(str_replace(',', '.', $value)));
                        }
                        break;
                    case 'wppa_colwidth':
                        //	 ??	  fixed   low	high	title
                        wppa_ajax_check_range($value, 'auto', '100', false, __('Column width.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_initial_colwidth':
                        wppa_ajax_check_range($value, false, '100', false, __('Initial width.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_fullsize':
                        wppa_ajax_check_range($value, false, '100', false, __('Full size.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_maxheight':
                        wppa_ajax_check_range($value, false, '100', false, __('Max height.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_film_thumbsize':
                    case 'wppa_thumbsize':
                    case 'wppa_thumbsize_alt':
                        wppa_ajax_check_range($value, false, '50', false, __('Thumbnail size.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_tf_width':
                    case 'wppa_tf_width_alt':
                        wppa_ajax_check_range($value, false, '50', false, __('Thumbnail frame width', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_tf_height':
                    case 'wppa_tf_height_alt':
                        wppa_ajax_check_range($value, false, '50', false, __('Thumbnail frame height', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_tn_margin':
                        wppa_ajax_check_range($value, false, '0', false, __('Thumbnail Spacing', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_min_thumbs':
                        wppa_ajax_check_range($value, false, '0', false, __('Photocount treshold.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_thumb_page_size':
                        wppa_ajax_check_range($value, false, '0', false, __('Thumb page size.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_smallsize':
                        wppa_ajax_check_range($value, false, '50', false, __('Cover photo size.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_album_page_size':
                        wppa_ajax_check_range($value, false, '0', false, __('Album page size.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_topten_count':
                        wppa_ajax_check_range($value, false, '2', false, __('Number of TopTen photos', 'wp-photo-album-plus'), '40');
                        break;
                    case 'wppa_topten_size':
                        wppa_ajax_check_range($value, false, '32', false, __('Widget image thumbnail size', 'wp-photo-album-plus'), wppa_get_minisize());
                        break;
                    case 'wppa_max_cover_width':
                        wppa_ajax_check_range($value, false, '150', false, __('Max Cover width', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_text_frame_height':
                        wppa_ajax_check_range($value, false, '0', false, __('Minimal description height', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_cover_minheight':
                        wppa_ajax_check_range($value, false, '0', false, __('Minimal cover height', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_head_and_text_frame_height':
                        wppa_ajax_check_range($value, false, '0', false, __('Minimal text frame height', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_bwidth':
                        wppa_ajax_check_range($value, '', '0', false, __('Border width', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_bradius':
                        wppa_ajax_check_range($value, '', '0', false, __('Border radius', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_box_spacing':
                        wppa_ajax_check_range($value, '', '-20', '100', __('Box spacing', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_popupsize':
                        $floor = wppa_opt('thumbsize');
                        $temp = wppa_opt('smallsize');
                        if ($temp > $floor) {
                            $floor = $temp;
                        }
                        wppa_ajax_check_range($value, false, $floor, wppa_opt('fullsize'), __('Popup size', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_fullimage_border_width':
                        wppa_ajax_check_range($value, '', '0', false, __('Fullsize border width', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_lightbox_bordersize':
                        wppa_ajax_check_range($value, false, '0', false, __('Lightbox Bordersize', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_ovl_border_width':
                        wppa_ajax_check_range($value, false, '0', '16', __('Lightbox Borderwidth', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_ovl_border_radius':
                        wppa_ajax_check_range($value, false, '0', '16', __('Lightbox Borderradius', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_comment_count':
                        wppa_ajax_check_range($value, false, '2', '40', __('Number of Comment widget entries', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_comment_size':
                        wppa_ajax_check_range($value, false, '32', wppa_get_minisize(), __('Comment Widget image thumbnail size', 'wp-photo-album-plus'), wppa_get_minisize());
                        break;
                    case 'wppa_thumb_opacity':
                        wppa_ajax_check_range($value, false, '0', '100', __('Opacity.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_cover_opacity':
                        wppa_ajax_check_range($value, false, '0', '100', __('Opacity.', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_star_opacity':
                        wppa_ajax_check_range($value, false, '0', '50', __('Opacity.', 'wp-photo-album-plus'));
                        break;
                        //				case 'wppa_filter_priority':
                        //					wppa_ajax_check_range( $value, false, wppa_opt( 'shortcode_priority' ), false, __( 'Filter priority' ,'wp-photo-album-plus' ) );
                        //					break;
                        //				case 'wppa_shortcode_priority':
                        //					wppa_ajax_check_range( $value, false, '0', wppa_opt( 'filter_priority' ) - '1', __( 'Shortcode_priority', 'wp-photo-album-plus' ) );
                        //					break;
                    //				case 'wppa_filter_priority':
                    //					wppa_ajax_check_range( $value, false, wppa_opt( 'shortcode_priority' ), false, __( 'Filter priority' ,'wp-photo-album-plus' ) );
                    //					break;
                    //				case 'wppa_shortcode_priority':
                    //					wppa_ajax_check_range( $value, false, '0', wppa_opt( 'filter_priority' ) - '1', __( 'Shortcode_priority', 'wp-photo-album-plus' ) );
                    //					break;
                    case 'wppa_gravatar_size':
                        wppa_ajax_check_range($value, false, '10', '256', __('Avatar size', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_watermark_opacity':
                        wppa_ajax_check_range($value, false, '0', '100', __('Watermark opacity', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_watermark_opacity_text':
                        wppa_ajax_check_range($value, false, '0', '100', __('Watermark opacity', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_ovl_txt_lines':
                        wppa_ajax_check_range($value, 'auto', '0', '24', __('Number of text lines', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_ovl_opacity':
                        wppa_ajax_check_range($value, false, '0', '100', __('Overlay opacity', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_upload_limit_count':
                        wppa_ajax_check_range($value, false, '0', false, __('Upload limit', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_dislike_mail_every':
                        wppa_ajax_check_range($value, false, '0', false, __('Notify inappropriate', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_dislike_set_pending':
                        wppa_ajax_check_range($value, false, '0', false, __('Dislike pending', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_dislike_delete':
                        wppa_ajax_check_range($value, false, '0', false, __('Dislike delete', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_max_execution_time':
                        wppa_ajax_check_range($value, false, '0', '900', __('Max execution time', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_cp_points_comment':
                    case 'wppa_cp_points_comment_appr':
                    case 'wppa_cp_points_rating':
                    case 'wppa_cp_points_upload':
                        wppa_ajax_check_range($value, false, '0', false, __('myCRED / Cube Points', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_jpeg_quality':
                        wppa_ajax_check_range($value, false, '20', '100', __('JPG Image quality', 'wp-photo-album-plus'));
                        if (wppa_cdn('admin') == 'cloudinary' && !wppa('out')) {
                            wppa_delete_derived_from_cloudinary();
                        }
                        break;
                    case 'wppa_imgfact_count':
                        wppa_ajax_check_range($value, false, '1', '24', __('Number of coverphotos', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_dislike_value':
                        wppa_ajax_check_range($value, false, '-10', '0', __('Dislike value', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_slideshow_pagesize':
                        wppa_ajax_check_range($value, false, '0', false, __('Slideshow pagesize', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_slideonly_max':
                        wppa_ajax_check_range($value, false, '0', false, __('Slideonly max', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_pagelinks_max':
                        wppa_ajax_check_range($value, false, '0', false, __('Max Pagelinks', 'wp-photo-album-plus'));
                        break;
                        /*
                        				case 'wppa_start_pause_symbol_size':
                        					wppa_ajax_check_range( $value, false, '0', false, __('Start/pause symbol size', 'wp-photo-album-plus') );
                        					break;
                        				case 'wppa_start_pause_symbol_bradius':
                        					wppa_ajax_check_range( $value, false, '0', false, __('Start/pause symbol border radius', 'wp-photo-album-plus') );
                        					break;
                        				case 'wppa_stop_symbol_size':
                        					wppa_ajax_check_range( $value, false, '0', false, __('Stop symbol size', 'wp-photo-album-plus') );
                        					break;
                        				case 'wppa_stop_symbol_bradius':
                        					wppa_ajax_check_range( $value, false, '0', false, __('Stop symbol border radius', 'wp-photo-album-plus') );
                        					break;
                        */
                    /*
                    				case 'wppa_start_pause_symbol_size':
                    					wppa_ajax_check_range( $value, false, '0', false, __('Start/pause symbol size', 'wp-photo-album-plus') );
                    					break;
                    				case 'wppa_start_pause_symbol_bradius':
                    					wppa_ajax_check_range( $value, false, '0', false, __('Start/pause symbol border radius', 'wp-photo-album-plus') );
                    					break;
                    				case 'wppa_stop_symbol_size':
                    					wppa_ajax_check_range( $value, false, '0', false, __('Stop symbol size', 'wp-photo-album-plus') );
                    					break;
                    				case 'wppa_stop_symbol_bradius':
                    					wppa_ajax_check_range( $value, false, '0', false, __('Stop symbol border radius', 'wp-photo-album-plus') );
                    					break;
                    */
                    case 'wppa_sticky_header_size':
                        wppa_ajax_check_range($value, false, '0', '200', __('Sticky header size', 'wp-photo-album-plus'));
                        break;
                    case 'wppa_rating_clear':
                        $iret1 = $wpdb->query('TRUNCATE TABLE ' . WPPA_RATING);
                        $iret2 = $wpdb->query('UPDATE ' . WPPA_PHOTOS . ' SET mean_rating="0", rating_count="0" WHERE id > -1');
                        if ($iret1 !== false && $iret2 !== false) {
                            delete_option('wppa_' . WPPA_RATING . '_lastkey');
                            $title = __('Ratings cleared', 'wp-photo-album-plus');
                        } else {
                            $title = __('Could not clear ratings', 'wp-photo-album-plus');
                            $alert = $title;
                            wppa('error', '1');
                        }
                        break;
                    case 'wppa_viewcount_clear':
                        $iret = $wpdb->query("UPDATE `" . WPPA_PHOTOS . "` SET `views` = '0'") && $wpdb->query("UPDATE `" . WPPA_ALBUMS . "` SET `views` = '0'");
                        if ($iret !== false) {
                            $title = __('Viewcounts cleared', 'wp-photo-album-plus');
                        } else {
                            $title = __('Could not clear viewcounts', 'wp-photo-album-plus');
                            $alert = $title;
                            wppa('error', '1');
                        }
                        break;
                    case 'wppa_iptc_clear':
                        $iret = $wpdb->query('TRUNCATE TABLE ' . WPPA_IPTC);
                        if ($iret !== false) {
                            delete_option('wppa_' . WPPA_IPTC . '_lastkey');
                            $title = __('IPTC data cleared', 'wp-photo-album-plus');
                            $alert = __('Refresh this page to clear table X', 'wp-photo-album-plus');
                            update_option('wppa_index_need_remake', 'yes');
                        } else {
                            $title = __('Could not clear IPTC data', 'wp-photo-album-plus');
                            $alert = $title;
                            wppa('error', '1');
                        }
                        break;
                    case 'wppa_exif_clear':
                        $iret = $wpdb->query('TRUNCATE TABLE ' . WPPA_EXIF);
                        if ($iret !== false) {
                            delete_option('wppa_' . WPPA_EXIF . '_lastkey');
                            $title = __('EXIF data cleared', 'wp-photo-album-plus');
                            $alert = __('Refresh this page to clear table XI', 'wp-photo-album-plus');
                            update_option('wppa_index_need_remake', 'yes');
                        } else {
                            $title = __('Could not clear EXIF data', 'wp-photo-album-plus');
                            $alert = $title;
                            wppa('error', '1');
                        }
                        break;
                    case 'wppa_recup':
                        $result = wppa_recuperate_iptc_exif();
                        echo '||0||' . __('Recuperation performed', 'wp-photo-album-plus') . '||' . $result;
                        wppa_exit();
                        break;
                    case 'wppa_bgcolor_thumbnail':
                        $value = trim(strtolower($value));
                        if (strlen($value) != '7' || substr($value, 0, 1) != '#') {
                            wppa('error', '1');
                        } else {
                            for ($i = 1; $i < 7; $i++) {
                                if (!in_array(substr($value, $i, 1), array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'))) {
                                    wppa('error', '1');
                                }
                            }
                        }
                        if (!wppa('error')) {
                            $old_minisize--;
                        } else {
                            $alert = __('Illegal format. Please enter a 6 digit hexadecimal color value. Example: #77bbff', 'wp-photo-album-plus');
                        }
                        break;
                    case 'wppa_thumb_aspect':
                        $old_minisize--;
                        // Trigger regen message
                        break;
                    case 'wppa_rating_max':
                        if ($value == '5' && wppa_opt('rating_max') == '10') {
                            $rats = $wpdb->get_results('SELECT `id`, `value` FROM `' . WPPA_RATING . '`', ARRAY_A);
                            if ($rats) {
                                foreach ($rats as $rat) {
                                    $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_RATING . '` SET `value` = %s WHERE `id` = %s', $rat['value'] / 2, $rat['id']));
                                }
                            }
                        }
                        if ($value == '10' && wppa_opt('rating_max') == '5') {
                            $rats = $wpdb->get_results('SELECT `id`, `value` FROM `' . WPPA_RATING . '`', ARRAY_A);
                            if ($rats) {
                                foreach ($rats as $rat) {
                                    $wpdb->query($wpdb->prepare('UPDATE `' . WPPA_RATING . '` SET `value` = %s WHERE `id` = %s', $rat['value'] * 2, $rat['id']));
                                }
                            }
                        }
                        update_option('wppa_rerate_status', 'Required');
                        $alert .= __('You just changed a setting that requires the recalculation of ratings.', 'wp-photo-album-plus');
                        $alert .= ' ' . __('Please run the appropriate action in Table VIII.', 'wp-photo-album-plus');
                        wppa_update_option($option, $value);
                        wppa('error', '0');
                        break;
                    case 'wppa_newphoto_description':
                        if (wppa_switch('check_balance') && balanceTags($value, true) != $value) {
                            $alert = __('Unbalanced tags in photo description!', 'wp-photo-album-plus');
                            wppa('error', '1');
                        } else {
                            wppa_update_option($option, $value);
                            wppa('error', '0');
                            $alert = '';
                            wppa_index_compute_skips();
                        }
                        break;
                    case 'wppa_keep_source':
                        $dir = wppa_opt('source_dir');
                        if (!is_dir($dir)) {
                            wppa_mkdir($dir);
                        }
                        if (!is_dir($dir) || !is_writable($dir)) {
                            wppa('error', '1');
                            $alert = sprintf(__('Unable to create or write to %s', 'wp-photo-album-plus'), $dir);
                        }
                        break;
                    case 'wppa_source_dir':
                        $olddir = wppa_opt('source_dir');
                        $value = rtrim($value, '/');
                        if (strpos($value . '/', WPPA_UPLOAD_PATH . '/') !== false) {
                            wppa('error', '1');
                            $alert = sprintf(__('Source can not be inside the wppa folder.', 'wp-photo-album-plus'));
                        } else {
                            $dir = $value;
                            if (!is_dir($dir)) {
                                wppa_mkdir($dir);
                            }
                            if (!is_dir($dir) || !is_writable($dir)) {
                                wppa('error', '1');
                                $alert = sprintf(__('Unable to create or write to %s', 'wp-photo-album-plus'), $dir);
                            } else {
                                @rmdir($olddir);
                                // try to remove when empty
                            }
                        }
                        break;
                    case 'wppa_newpag_content':
                        if (strpos($value, 'w#album') === false) {
                            $alert = __('The content must contain w#album', 'wp-photo-album-plus');
                            wppa('error', '1');
                        }
                        break;
                    case 'wppa_gpx_shortcode':
                        if (strpos($value, 'w#lat') === false || strpos($value, 'w#lon') === false) {
                            $alert = __('The content must contain w#lat and w#lon', 'wp-photo-album-plus');
                            wppa('error', '1');
                        }
                        break;
                    case 'wppa_i_responsive':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_colwidth', 'auto');
                            wppa_update_option('wppa_cover_type', 'default-mcr');
                        }
                        if ($value == 'no') {
                            wppa_update_option('wppa_colwidth', '640');
                            wppa_update_option('wppa_cover_type', 'default');
                        }
                        break;
                    case 'wppa_i_downsize':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_resize_on_upload', 'yes');
                            if (wppa_opt('resize_to') == '0') {
                                wppa_update_option('wppa_resize_to', '1024x768');
                            }
                        }
                        if ($value == 'no') {
                            wppa_update_option('wppa_resize_on_upload', 'no');
                        }
                        break;
                    case 'wppa_i_source':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_keep_source_admin', 'yes');
                            wppa_update_option('wppa_keep_source_frontend', 'yes');
                        }
                        if ($value == 'no') {
                            wppa_update_option('wppa_keep_source_admin', 'no');
                            wppa_update_option('wppa_keep_source_frontend', 'no');
                        }
                        break;
                    case 'wppa_i_userupload':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_user_upload_on', 'yes');
                            wppa_update_option('wppa_user_upload_login', 'yes');
                            wppa_update_option('wppa_owner_only', 'yes');
                            wppa_update_option('wppa_upload_moderate', 'yes');
                            wppa_update_option('wppa_upload_edit', 'new');
                            wppa_update_option('wppa_upload_notify', 'yes');
                            wppa_update_option('wppa_grant_an_album', 'yes');
                            $grantparent = wppa_opt('grant_parent');
                            if (!wppa_album_exists($grantparent)) {
                                $id = wppa_create_album_entry(array('name' => __('Members', 'wp-photo-album-plus'), 'description' => __('Parent of the member albums', 'wp-photo-album-plus'), 'upload_limit' => '0/0'));
                                if ($id) {
                                    wppa_index_add('album', $id);
                                    wppa_update_option('wppa_grant_parent', $id);
                                }
                                $my_post = array('post_title' => __('Members', 'wp-photo-album-plus'), 'post_content' => '[wppa type="content" album="' . $id . '"][/wppa]', 'post_status' => 'publish', 'post_type' => 'page');
                                $pagid = wp_insert_post($my_post);
                            }
                            wppa_update_option('wppa_alt_is_restricted', 'yes');
                            wppa_update_option('wppa_link_is_restricted', 'yes');
                            wppa_update_option('wppa_covertype_is_restricted', 'yes');
                            wppa_update_option('wppa_porder_restricted', 'yes');
                        }
                        if ($value == 'no') {
                            wppa_update_option('wppa_user_upload_on', 'no');
                        }
                        break;
                    case 'wppa_i_rating':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_rating_on', 'yes');
                        }
                        if ($value == 'no') {
                            wppa_update_option('wppa_rating_on', 'no');
                        }
                        break;
                    case 'wppa_i_comment':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_show_comments', 'yes');
                            wppa_update_option('wppa_comment_moderation', 'all');
                            wppa_update_option('wppa_comment_notify', 'admin');
                        }
                        if ($value == 'no') {
                            wppa_update_option('wppa_show_comments', 'no');
                        }
                        break;
                    case 'wppa_i_share':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_share_on', 'yes');
                        }
                        if ($value == 'no') {
                            wppa_update_option('wppa_share_on', 'no');
                        }
                        break;
                    case 'wppa_i_iptc':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_show_iptc', 'yes');
                            wppa_update_option('wppa_save_iptc', 'yes');
                        }
                        if ($value == 'no') {
                            wppa_update_option('wppa_show_iptc', 'no');
                            wppa_update_option('wppa_save_iptc', 'no');
                        }
                        break;
                    case 'wppa_i_exif':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_show_exif', 'yes');
                            wppa_update_option('wppa_save_exif', 'yes');
                        }
                        if ($value == 'no') {
                            wppa_update_option('wppa_show_exif', 'no');
                            wppa_update_option('wppa_save_exif', 'no');
                        }
                        break;
                    case 'wppa_i_gpx':
                        if ($value == 'yes') {
                            $custom_content = wppa_opt('custom_content');
                            if (strpos($custom_content, 'w#location') === false) {
                                $custom_content = $custom_content . ' w#location';
                                wppa_update_option('wppa_custom_content', $custom_content);
                            }
                            if (!wppa_switch('custom_on')) {
                                wppa_update_option('wppa_custom_on', 'yes');
                            }
                            if (wppa_opt('gpx_implementation') == 'none') {
                                wppa_update_option('wppa_gpx_implementation', 'wppa-plus-embedded');
                            }
                        }
                        break;
                    case 'wppa_i_fotomoto':
                        if ($value == 'yes') {
                            $custom_content = wppa_opt('custom_content');
                            if (strpos($custom_content, 'w#fotomoto') === false) {
                                $custom_content = 'w#fotomoto ' . $custom_content;
                                wppa_update_option('wppa_custom_content', $custom_content);
                            }
                            if (!wppa_switch('custom_on')) {
                                wppa_update_option('wppa_custom_on', 'yes');
                            }
                            wppa_update_option('wppa_fotomoto_on', 'yes');
                            wppa_update_option('wppa_custom_on', 'yes');
                        }
                        break;
                    case 'wppa_i_video':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_enable_video', 'yes');
                        } else {
                            wppa_update_option('wppa_enable_video', 'no');
                        }
                        break;
                    case 'wppa_i_audio':
                        if ($value == 'yes') {
                            wppa_update_option('wppa_enable_audio', 'yes');
                        } else {
                            wppa_update_option('wppa_enable_audio', 'no');
                        }
                        break;
                    case 'wppa_i_done':
                        $value = 'done';
                        break;
                    case 'wppa_search_tags':
                    case 'wppa_search_cats':
                    case 'wppa_search_comments':
                        update_option('wppa_index_need_remake', 'yes');
                        break;
                    case 'wppa_blacklist_user':
                        // Does user exist?
                        $value = trim($value);
                        $user = get_user_by('login', $value);
                        // seems to be case insensitive
                        if ($user && $user->user_login === $value) {
                            $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_PHOTOS . "` SET `status` = 'pending' WHERE `owner` = %s", $value));
                            $black_listed_users = get_option('wppa_black_listed_users', array());
                            if (!in_array($value, $black_listed_users)) {
                                $black_listed_users[] = $value;
                                update_option('wppa_black_listed_users', $black_listed_users);
                            }
                            $alert = esc_js(sprintf(__('User %s has been blacklisted.', 'wp-photo-album-plus'), $value));
                        } else {
                            $alert = esc_js(sprintf(__('User %s does not exist.', 'wp-photo-album-plus'), $value));
                        }
                        $value = '';
                        break;
                    case 'wppa_un_blacklist_user':
                        $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_PHOTOS . "` SET `status` = 'publish' WHERE `owner` = %s", $value));
                        $black_listed_users = get_option('wppa_black_listed_users', array());
                        if (in_array($value, $black_listed_users)) {
                            foreach (array_keys($black_listed_users) as $usr) {
                                if ($black_listed_users[$usr] == $value) {
                                    unset($black_listed_users[$usr]);
                                }
                            }
                            update_option('wppa_black_listed_users', $black_listed_users);
                        }
                        $value = '0';
                        break;
                    case 'wppa_superuser_user':
                        // Does user exist?
                        $value = trim($value);
                        $user = get_user_by('login', $value);
                        // seems to be case insensitive
                        if ($user && $user->user_login === $value) {
                            $super_users = get_option('wppa_super_users', array());
                            if (!in_array($value, $super_users)) {
                                $super_users[] = $value;
                                update_option('wppa_super_users', $super_users);
                            }
                            $alert = esc_js(sprintf(__('User %s is now superuser.', 'wp-photo-album-plus'), $value));
                        } else {
                            $alert = esc_js(sprintf(__('User %s does not exist.', 'wp-photo-album-plus'), $value));
                        }
                        $value = '';
                        break;
                    case 'wppa_un_superuser_user':
                        $super_users = get_option('wppa_super_users', array());
                        if (in_array($value, $super_users)) {
                            foreach (array_keys($super_users) as $usr) {
                                if ($super_users[$usr] == $value) {
                                    unset($super_users[$usr]);
                                }
                            }
                            update_option('wppa_super_users', $super_users);
                        }
                        $value = '0';
                        break;
                    case 'wppa_fotomoto_on':
                        if ($value == 'yes') {
                            $custom_content = wppa_opt('custom_content');
                            if (strpos($custom_content, 'w#fotomoto') === false) {
                                $custom_content = 'w#fotomoto ' . $custom_content;
                                wppa_update_option('wppa_custom_content', $custom_content);
                                $alert = __('The content of the Custom box has been changed to display the Fotomoto toolbar.', 'wp-photo-album-plus') . ' ';
                            }
                            if (!wppa_switch('custom_on')) {
                                wppa_update_option('wppa_custom_on', 'yes');
                                $alert .= __('The display of the custom box has been enabled', 'wp-photo-album-plus');
                            }
                        }
                        break;
                    case 'wppa_gpx_implementation':
                        if ($value != 'none') {
                            $custom_content = wppa_opt('custom_content');
                            if (strpos($custom_content, 'w#location') === false) {
                                $custom_content = $custom_content . ' w#location';
                                wppa_update_option('wppa_custom_content', $custom_content);
                                $alert = __('The content of the Custom box has been changed to display maps.', 'wp-photo-album-plus') . ' ';
                            }
                            if (!wppa_switch('custom_on')) {
                                wppa_update_option('wppa_custom_on', 'yes');
                                $alert .= __('The display of the custom box has been enabled', 'wp-photo-album-plus');
                            }
                        }
                        break;
                    case 'wppa_regen_thumbs_skip_one':
                        $last = get_option('wppa_regen_thumbs_last', '0');
                        $skip = $last + '1';
                        update_option('wppa_regen_thumbs_last', $skip);
                        break;
                    case 'wppa_remake_skip_one':
                        $last = get_option('wppa_remake_last', '0');
                        $skip = $last + '1';
                        update_option('wppa_remake_last', $skip);
                        break;
                    case 'wppa_create_o1_files_skip_one':
                        $last = get_option('wppa_create_o1_files_last', '0');
                        $skip = $last + '1';
                        update_option('wppa_create_o1_files_last', $skip);
                        break;
                    case 'wppa_errorlog_purge':
                        if (is_file($wppa_log_file)) {
                            unlink($wppa_log_file);
                        }
                        break;
                    case 'wppa_pl_dirname':
                        $value = wppa_sanitize_file_name($value);
                        $value = trim($value, ' /');
                        // Remove old file if it exists
                        $oldfile = WPPA_CONTENT_PATH . '/' . get_option('wppa_pl_dirname') . '/.htaccess';
                        if (is_file($oldfile)) {
                            if (!unlink($oldfile)) {
                                wppa_log('obs', 'Could not remove: ' . $oldfile);
                            }
                        } else {
                            wppa_log('obs', 'Does not exist: ' . $oldfile);
                        }
                        if ($value) {
                            wppa_create_pl_htaccess($value);
                        }
                        break;
                    case 'wppa_new_tag_value':
                        $value = wppa_sanitize_tags($value, false, true);
                        break;
                    case 'wppa_up_tagselbox_content_1':
                    case 'wppa_up_tagselbox_content_2':
                    case 'wppa_up_tagselbox_content_3':
                        $value = wppa_sanitize_tags($value);
                        break;
                    case 'wppa_wppa_set_shortcodes':
                        $value = str_replace(' ', '', $value);
                        break;
                    case 'wppa_use_encrypted_links':
                        if ($value == 'yes') {
                            $ca = $wpdb->get_var("SELECT COUNT(*) FROM `" . WPPA_ALBUMS . "` WHERE `crypt` = ''");
                            $cp = $wpdb->get_var("SELECT COUNT(*) FROM `" . WPPA_PHOTOS . "` WHERE `crypt` = ''");
                            if ($ca + $cp) {
                                if ($ca) {
                                    update_option('wppa_crypt_albums_status', 'Required');
                                }
                                if ($cp) {
                                    update_option('wppa_crypt_photos_status', 'Required');
                                }
                                wppa('error', '4711');
                                $alert .= __('You must run Table VIII-A13 and VIII-A14 first before you can switch to encrypted urls.', 'wp-photo-album-plus');
                            }
                            if (wppa_switch('use_photo_names_in_urls')) {
                                $alert .= ' ' . __('Table IV-A3 will be switched off.', 'wp-photo-album-plus');
                                wppa_update_option('wppa_use_photo_names_in_urls', 'no');
                            }
                            if (wppa_switch('use_album_names_in_urls')) {
                                $alert .= ' ' . __('Table IV-A4 will be switched off.', 'wp-photo-album-plus');
                                wppa_update_option('wppa_use_album_names_in_urls', 'no');
                            }
                        }
                        break;
                    case 'wppa_use_photo_names_in_urls':
                    case 'wppa_use_album_names_in_urls':
                        if (wppa_switch('use_encrypted_links')) {
                            wppa('error', '4711');
                            $alert .= __('Not allowed when cryptic links is active', 'wp-photo-album-plus');
                        }
                    case 'wppa_enable_video':
                        // if off: set all statusses of videos to pending
                        break;
                    case 'wppa_twitter_account':
                        $value = sanitize_text_field($value);
                        $value = str_replace(' ', '', $value);
                        if (substr($value, 0, 1) != '@') {
                            wppa('error', '4712');
                            $alert .= __('A Twitter account name must start with an at sign: @', 'wp-photo-album-plus');
                        }
                        break;
                    case 'wppa_rating_display_type':
                        if ($value == 'likes') {
                            wppa_update_option('wppa_rating_multi', 'yes');
                            wppa_update_option('wppa_rating_dayly', '0');
                            wppa_update_option('wppa_vote_needs_comment', 'no');
                        }
                        break;
                    default:
                        wppa('error', '0');
                        $alert = '';
                }
            }
            if (wppa('error')) {
                if (!$title) {
                    $title = sprintf(__('Failed to set %s to %s', 'wp-photo-album-plus'), $option, $value);
                }
                if (!$alert) {
                    $alert .= wppa('out');
                }
            } else {
                wppa_update_option($option, $value);
                if (!$title) {
                    $title = sprintf(__('Setting %s updated to %s', 'wp-photo-album-plus'), $option, $value);
                }
            }
            // Save possible error
            $error = wppa('error');
            // Something to do after changing the setting?
            wppa_initialize_runtime(true);
            // force reload new values
            // .htaccess
            wppa_create_wppa_htaccess();
            // Thumbsize
            $new_minisize = wppa_get_minisize();
            if ($old_minisize != $new_minisize) {
                update_option('wppa_regen_thumbs_status', 'Required');
                $alert .= __('You just changed a setting that requires the regeneration of thumbnails.', 'wp-photo-album-plus');
                $alert .= ' ' . __('Please run the appropriate action in Table VIII.', 'wp-photo-album-plus');
            }
            // Produce the response text
            $output = '||' . $error . '||' . esc_attr($title) . '||' . esc_js($alert);
            echo $output;
            wppa_clear_cache();
            wppa_exit();
            break;
            // End update-option
        // End update-option
        case 'maintenance':
            // Get args
            $slug = $_POST['slug'];
            $nonce = $_REQUEST['wppa-nonce'];
            $cron = isset($_REQUEST['wppa-cron']);
            // Security check
            if (!wp_verify_nonce($nonce, 'wppa-nonce')) {
                echo 'Security check failure||' . $slug . '||Error||0';
                wppa_exit();
            }
            // If cron request, schedule
            if ($cron) {
                echo wppa_schedule_maintenance_proc($slug, 'first');
                // Remove in case this is a re-start of a crashed cron job
                delete_option($slug . '_lasttimestamp');
            } else {
                echo wppa_do_maintenance_proc($slug);
            }
            wppa_exit();
            break;
        case 'maintenancepopup':
            $slug = $_POST['slug'];
            $nonce = $_REQUEST['wppa-nonce'];
            if (!wp_verify_nonce($nonce, 'wppa-nonce')) {
                echo 'Security check failure||' . $slug . '||Error||0';
                wppa_exit();
            }
            echo wppa_do_maintenance_popup($slug);
            wppa_exit();
            break;
        case 'do-fe-upload':
            if (is_admin()) {
                require_once 'wppa-non-admin.php';
            }
            wppa_user_upload();
            echo wppa('out');
            wppa_exit();
            break;
        case 'sanitizetags':
            $tags = isset($_GET['tags']) ? $_GET['tags'] : '';
            $album = isset($_GET['album']) ? $_GET['album'] : '0';
            $deftags = wppa_is_int($album) && $album > '0' ? wppa_get_album_item($album, 'default_tags') : '';
            $tags = $deftags ? $tags . ',' . $deftags : $tags;
            echo wppa_sanitize_tags($tags, false, true);
            wppa_exit();
            break;
        case 'destroyalbum':
            $album = isset($_GET['album']) ? $_GET['album'] : '0';
            if (!$album) {
                _e('Missing album id', 'wp-photo-album-plus');
                wppa_exit();
            }
            $nonce = isset($_GET['nonce']) ? $_GET['nonce'] : '';
            if (!$nonce || !wp_verify_nonce($nonce, 'wppa_nonce_' . $album)) {
                echo 'Security check failure #798';
                wppa_exit();
            }
            // May I?
            $imay = true;
            if (!wppa_switch('user_destroy_on')) {
                $may = false;
            }
            if (wppa_switch('user_create_login')) {
                if (!is_user_logged_in()) {
                    $may = false;
                }
                // Must login
            }
            if (!wppa_have_access($album)) {
                $may = false;
                // No album access
            }
            if (wppa_is_user_blacklisted()) {
                $may = false;
            }
            if (!$imay) {
                _e('You do not have the rights to delete this album', 'wp-photo-album-plus');
                wppa_exit();
            }
            // I may
            require_once 'wppa-album-admin-autosave.php';
            wppa_del_album($album, '');
            wppa_exit();
            break;
        case 'export-table':
            if (!wppa_user_is('administrator')) {
                echo '||1||' . __('Security check failure', 'wp-photo-album-plus');
                wppa_exit();
            }
            $table = $_REQUEST['table'];
            $bret = wppa_export_table($table);
            if ($bret) {
                echo '||0||' . WPPA_UPLOAD_URL . '/temp/' . $table . '.csv';
            } else {
                echo '||2||' . __('An error has occurred', 'wp-photo-album-plus');
            }
            wppa_exit();
            break;
        default:
            // Unimplemented $wppa-action
            die('-1');
    }
    wppa_exit();
}
function wppa_get_coverphoto_ids($alb, $count)
{
    global $wpdb;
    if (!$alb) {
        return false;
    }
    // no album, no coverphoto
    // Find cover photo id
    $id = wppa_get_album_item($alb, 'main_photo');
    // main_photo is a positive integer ( photo id )?
    if ($id > '0') {
        // 1 coverphoto explicitly given
        $photo = wppa_cache_photo($id);
        if (!$photo) {
            // Photo gone, set id to 0
            $id = '0';
        } elseif ($photo['album'] != $alb) {
            // Photo moved to other album, set id to 0
            $id = '0';
        } else {
            $temp['0'] = $photo;
            // Found!
        }
    }
    // main_photo is 0? Random
    if ('0' == $id) {
        if (current_user_can('wppa_moderate')) {
            $temp = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = %s ORDER BY RAND( " . wppa_get_randseed('page') . " ) LIMIT %d", $alb, $count), ARRAY_A);
        } else {
            $temp = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = %s AND ( ( `status` <> 'pending' AND `status` <> 'scheduled' ) OR `owner` = %s ) ORDER BY RAND( " . wppa_get_randseed('page') . " ) LIMIT %d", $alb, wppa_get_user(), $count), ARRAY_A);
        }
    }
    // main_photo is -2? Last upload
    if ('-2' == $id) {
        if (current_user_can('wppa_moderate')) {
            $temp = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = %s ORDER BY `timestamp` DESC LIMIT %d", $alb, $count), ARRAY_A);
        } else {
            $temp = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = %s AND ( ( `status` <> 'pending' AND `status` <> 'scheduled' ) OR `owner` = %s ) ORDER BY `timestamp` DESC LIMIT %d", $alb, wppa_get_user(), $count), ARRAY_A);
        }
    }
    // main_phtot is -1? Random featured
    if ('-1' == $id) {
        $temp = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `album` = %s AND `status` = 'featured' ORDER BY RAND( " . wppa_get_randseed('page') . " ) LIMIT %d", $alb, $count), ARRAY_A);
    }
    // Random from children
    if ('-3' == $id) {
        $allalb = wppa_expand_enum(wppa_alb_to_enum_children($alb));
        $temp = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` IN ( " . str_replace('.', ',', $allalb) . " ) " . "AND ( ( `status` <> 'pending' AND `status` <> 'scheduled' ) OR `owner` = %s ) " . "ORDER BY RAND( " . wppa_get_randseed('page') . " ) LIMIT %d", wppa_get_user(), $count), ARRAY_A);
    }
    // Most recent from children
    if ('-4' == $id) {
        $allalb = wppa_expand_enum(wppa_alb_to_enum_children($alb));
        $temp = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` " . "WHERE `album` IN ( " . str_replace('.', ',', $allalb) . " ) " . "AND ( ( `status` <> 'pending' AND `status` <> 'scheduled' ) OR `owner` = %s ) " . "ORDER BY `timestamp` DESC LIMIT %d", wppa_get_user(), $count), ARRAY_A);
    }
    // Report query
    wppa_dbg_q('Q-gcovp');
    // Add to 2nd level cache
    wppa_cache_photo('add', $temp);
    // Extract the ids only
    $ids = array();
    if (is_array($temp)) {
        foreach ($temp as $item) {
            $ids[] = $item['id'];
        }
    }
    return $ids;
}