function album_no_newest_pictures($check_date, $cats, $exclude_cat_id = 0)
{
    global $db, $config, $user, $lang, $album_config;
    $user_last_visit = $user->data['user_lastvisit'];
    $pictotalrows = array();
    if (is_null($cats)) {
        return $pictotalrows;
    }
    // --------------------------------------------------------------------
    // NOTE : this function is weighted, meaning that days has higher
    // priority then months, and month higher priority then hours
    //
    // if $check_data = 12HMD, then we uses 12 days to calcuate
    // if $check_data = 12HM, then we uses 12 month calcuate...and so on
    // --------------------------------------------------------------------
    $check_date = strtoupper($check_date);
    // are we checking hours ?
    if (strstr($check_date, 'H') != false) {
        $multiplier = 60 * 60;
    }
    // are we checking months ?
    if (strstr($check_date, 'M') != false) {
        $multiplier = 30 * 24 * 60 * 60;
        // in my world a month is always 30 days ;)
    }
    // are we checking weeks ?
    if (strstr($check_date, 'W') != false) {
        $multiplier = 7 * 24 * 60 * 60;
        // in my world a month is always 30 days ;)
    }
    // are we checking days (default) ? - yes if multiplier is zero
    if (strstr($check_date, 'D') != false || $multiplier == 0) {
        $multiplier = 24 * 60 * 60;
    }
    // remove all the alpha characters from the string, since they aren't needed anymore
    $check_date = preg_replace('/[A-Z]+/', '', trim($check_date));
    // doa final test to see if it's a valid checkm further more
    // if intval should return 0 then we will not find any images
    // that are new, except those that only are a few second old
    // but we don't want to do a trip to the database just because of that
    // the minimum is 1 hour.
    if (intval($check_date) == 0) {
        return $pictotalrows;
    }
    // calculate the difference from today and the desired check date (beta code !)
    $curtime = time() - $multiplier * intval($check_date);
    //album_debug('date = %s',create_date($config['default_dateformat'], $curtime, $config['board_timezone']));
    if ($album_config['show_index_last_pic_lv'] == 1) {
        $sql_time = ' AND p.pic_time >= ' . $user_last_visit;
    } else {
        $sql_time = ' AND p.pic_time >= ' . $curtime;
    }
    $sql_exclude = $exclude_cat_id != 0 ? ' AND NOT IN (' . $exclude_cat_id . ')' : '';
    $sql_include = is_array($cats) ? implode(',', $cats) : $cats;
    $sql = 'SELECT c.cat_id, p.pic_id, COUNT(p.pic_id) AS pic_total
			FROM ' . ALBUM_TABLE . ' AS p, ' . ALBUM_CAT_TABLE . ' AS c
			WHERE c.cat_id IN (' . $sql_include . ')' . $sql_exclude . '
			AND p.pic_cat_id = c.cat_id ' . $sql_time . '
			GROUP BY c.cat_id';
    $result = $db->sql_query($sql);
    while ($row = $db->sql_fetchrow($result)) {
        $pictotalrows[$row['cat_id']] = $row['pic_total'];
    }
    $db->sql_freeresult($result);
    if (album_is_debug_enabled() == true) {
        album_debug('album_no_newest_pictures sql = %s', $sql);
        album_debug('$pictotalrows = %s', $pictotalrows);
    }
    return $pictotalrows;
}
function album_permissions($user_id, $cat_id, $permission_checks, $catdata = 0)
{
    global $db, $user, $lang, $album_config, $album_data;
    $moderator_check = 1;
    if (album_is_debug_enabled() == true) {
        if (!defined('ALBUM_AUTH_VIEW') || !defined('ALBUM_AUTH_UPLOAD') || !defined('ALBUM_AUTH_DELETE')) {
            album_debug("album_permissions : The defined authentication constants are NOT found !!!");
        }
    }
    $view_check = (int) checkFlag($permission_checks, ALBUM_AUTH_VIEW);
    $upload_check = (int) checkFlag($permission_checks, ALBUM_AUTH_UPLOAD);
    $rate_check = (int) checkFlag($permission_checks, ALBUM_AUTH_RATE);
    $comment_check = (int) checkFlag($permission_checks, ALBUM_AUTH_COMMENT);
    $edit_check = (int) checkFlag($permission_checks, ALBUM_AUTH_EDIT);
    $delete_check = (int) checkFlag($permission_checks, ALBUM_AUTH_DELETE);
    // ------------------------------------------------------------------------
    // if we are checkinfg the personal gallery category management permission
    // we need to do these also : view and upload
    // ------------------------------------------------------------------------
    if (checkFlag($permission_checks, ALBUM_AUTH_MANAGE_PERSONAL_CATEGORIES) == true) {
        $view_check = 1;
        $upload_check = 1;
    }
    // ------------------------------------------------------------------------
    // did we pass some category data or not ?
    // ------------------------------------------------------------------------
    if (!is_array($catdata)) {
        $sql = "SELECT *\n\t\t\t\tFROM " . ALBUM_CAT_TABLE . "\n\t\t\t\tWHERE cat_id = '{$cat_id}'";
        $result = $db->sql_query($sql);
        // ------------------------------------------------------------------------
        // did we find the category or not ?
        // ------------------------------------------------------------------------
        if ($db->sql_numrows($result) == 0) {
            // ------------------------------------------------------------------------
            // is it a personal gallery ?
            // ------------------------------------------------------------------------
            if ($user_id != ALBUM_PUBLIC_GALLERY) {
                $AH_thiscat = init_personal_gallery_cat($user_id);
                $album_permission = personal_gallery_access(1, 1);
                //$view_check, $upload_check);
            } else {
                message_die(GENERAL_ERROR, $lang['Category_not_exist'], '', __LINE__, __FILE__, $sql);
            }
        } else {
            $AH_thiscat = $db->sql_fetchrow($result);
        }
    } else {
        $AH_thiscat = $catdata;
        // ------------------------------------------------------------------------
        // it is the root category of a non existing personal gallery
        // ------------------------------------------------------------------------
        if ($AH_thiscat['cat_user_id'] != 0 && $AH_thiscat['cat_id'] == 0) {
            $album_permission = personal_gallery_access(1, 1);
            //$view_check, $upload_check);
        }
    }
    // ------------------------------------------------------------------------
    // if we set our $AH_thiscat and not our permission array then we must
    // authenticate it
    // ------------------------------------------------------------------------
    if (album_is_debug_enabled() == true) {
        album_debug('album_permissions : before album_user_access : %s(id=%d), $album_permission = %s', $AH_thiscat['cat_title'], $AH_thiscat['cat_id'], $album_permission);
    }
    if (!empty($AH_thiscat) && !is_array($album_permission)) {
        $album_permission = album_user_access($cat_id, $AH_thiscat, $view_check, $upload_check, $rate_check, $comment_check, $edit_check, $delete_check);
    }
    if (album_is_debug_enabled() == true) {
        album_debug('album_permissions : after album_user_access : %s(id=%d), $album_permission = %s', $AH_thiscat['cat_title'], $AH_thiscat['cat_id'], $album_permission);
    }
    // ------------------------------------------------------------------------
    // as default nobody can manage the galleries (personal galleries that is)
    // check is done later, but only for personal galleries, so its not possible
    // to manage the categories in the public galleries, only in the ACP
    // ------------------------------------------------------------------------
    $album_permission['manage'] = 0;
    // ------------------------------------------------------------------------
    // $album_permission should now hold our permission stuff for either a personal
    // gallery or a public gallery.
    // lets now do some more authentication for the personal galleries
    // ------------------------------------------------------------------------
    if ($AH_thiscat['cat_user_id'] != 0) {
        if (album_is_debug_enabled() == true) {
            album_Debug('$album_config[\'personal_gallery\'] = %d', $album_config['personal_gallery']);
        }
        switch ($album_config['personal_gallery']) {
            case ALBUM_USER:
                // ------------------------------------------------------------------------
                // are we checking a non existing personal gallery ?
                // ------------------------------------------------------------------------
                if (empty($AH_thiscat) || $AH_thiscat['cat_id'] == 0 || $cat_id == ALBUM_ROOT_CATEGORY) {
                    // ------------------------------------------------------------------------
                    // if the admin has set the creation of personal galleries to 'registered users'
                    // then filter out all other users then the current logged in user (and NON ADMIN)
                    // ------------------------------------------------------------------------
                    if ($user->data['user_id'] != $AH_thiscat['cat_user_id'] && $user->data['user_level'] != ADMIN) {
                        $album_permission['upload'] = 0;
                    }
                    // ------------------------------------------------------------------------
                    // set the other permissions to the same value of the upload
                    // for this non exsting personal gallery,
                    // ------------------------------------------------------------------------
                    $album_permission['rate'] = $album_permission['upload'];
                    $album_permission['edit'] = $album_permission['upload'];
                    $album_permission['delete'] = $album_permission['upload'];
                    $album_permission['comment'] = $album_permission['upload'];
                }
                break;
            case ALBUM_ADMIN:
                // ------------------------------------------------------------------------
                // Only admins can upload images to users personal gallery
                // ------------------------------------------------------------------------
                if ($user->data['user_level'] != ADMIN) {
                    $album_permission['upload'] = 0;
                }
                break;
            default:
                // NOTHING;
        }
        // ------------------------------------------------------------------------
        // we need to check the upload permission again to full fill all the
        // permission criterias
        // ------------------------------------------------------------------------
        switch ($AH_thiscat['cat_upload_level']) {
            case ALBUM_PRIVATE:
                // ------------------------------------------------------------------------
                // make sure the owner of the personal gallery can upload to his personal gallery
                // it the permission is set to private BUT only for existing personal galleries
                // if ($AH_thiscat['cat_id'] != 0 && ($user_id == $user->data['user_id']) )
                // ------------------------------------------------------------------------
                if ($AH_thiscat['cat_id'] != 0 && $AH_thiscat['cat_user_id'] == $user->data['user_id']) {
                    if ($album_config['personal_gallery'] == ALBUM_ADMIN && $user->data['user_level'] != ADMIN) {
                        $album_permission['upload'] = 0;
                    } else {
                        $album_permission['upload'] = 1;
                    }
                }
                break;
            default:
                // NOTHING;
        }
        // ------------------------------------------------------------------------
        // Check if we can moderate the personal gallery AND also check if we can
        // manage the personal gallery categories
        // ------------------------------------------------------------------------
        if ($user->data['user_level'] == ADMIN || $album_permission['upload'] == 1 && $album_config['personal_allow_gallery_mod'] == 1 && $AH_thiscat['cat_user_id'] == $user->data['user_id']) {
            $album_permission['moderator'] = 1;
        }
        if ($user->data['user_level'] == ADMIN || $album_config['personal_allow_sub_categories'] == 1 && $album_config['personal_sub_category_limit'] != 0 && $AH_thiscat['cat_user_id'] == $user->data['user_id'] && $album_permission['upload'] == 1) {
            $album_permission['manage'] = 1;
        }
        // ------------------------------------------------------------------------
        // If $moderator_check was called and this user is a MODERATOR the user
        // will be authorized for all accesses which were not set to ADMIN
        // except for the management of the categories in the personal gallery
        // ------------------------------------------------------------------------
        if ($album_permission['moderator'] == 1) {
            $album_permission_keys = array_keys($album_permission);
            for ($i = 0; $i < sizeof($album_permission); $i++) {
                if ($AH_thiscat['cat_' . $album_permission_keys[$i] . '_level'] != ALBUM_ADMIN && $album_permission_keys[$i] != 'manage') {
                    $album_permission[$album_permission_keys[$i]] = 1;
                }
            }
        }
    }
    if (album_is_debug_enabled() == true) {
        album_debug('final : $album_permission = %s', $album_permission);
    }
    return $album_permission;
}
function album_display_index($user_id, $cur_cat_id = ALBUM_ROOT_CATEGORY, $show_header = false, $show_public_footer = false, $force_display = false)
{
    global $lang, $config, $template, $images, $album_data, $album_config, $user;
    $keys = array();
    // for testing ONLY
    if (album_is_debug_enabled() == true) {
        if (strcmp($cur_cat_id, 'Root') == 0) {
            die('WRONG ROOT VALUE');
        }
    }
    $is_personal_gallery = $user_id != ALBUM_PUBLIC_GALLERY ? true : false;
    // if we are showing a personal gallery AND we are at the root of personal gallery
    // then ignore the root folder of the personal gallery, since it's 'hidden'
    if ($is_personal_gallery && $cur_cat_id == ALBUM_ROOT_CATEGORY) {
        $cur_cat_id = album_get_personal_root_id($user_id);
    }
    $template->set_filenames(array('album' => 'album_box.tpl'));
    $keys = album_get_auth_keys($cur_cat_id, ALBUM_AUTH_VIEW);
    $display = album_build_index($user_id, $keys, $cur_cat_id, ALBUM_ROOT_CATEGORY, ALBUM_ROOT_CATEGORY);
    if ($force_display && !$is_personal_gallery && sizeof($album_data) == 0) {
        $template->assign_block_vars('catmain', array());
        $template->assign_block_vars('catmain.catrow', array('CAT_TITLE' => $lang['No_Public_Galleries'], 'CAT_IMG' => $images['forum_nor_locked_read']));
        $display = true;
    }
    // Added so that, even if there are no public galleries, the member or user galleries can be reached
    if ($force_display && !$is_personal_gallery && !$display) {
        $display = true;
    }
    // lets do some debugging..
    if (album_is_debug_enabled() == true) {
        album_debug('$user_id = %d<br />$cur_cat_id = %d<br />$display = %d<br />album data = %s<br />authentication keys = %s', $user_id, $cur_cat_id, intval($display), $album_data, $keys);
    }
    if ($display || album_is_debug_enabled() == true) {
        if ($show_header) {
            // create header and send it to template
            $template->assign_block_vars('catheader', array('L_CATEGORY' => $lang['Category'], 'L_PUBLIC_CATS' => !$is_personal_gallery ? $lang['Public_Categories'] : sprintf($lang['Personal_Gallery_Of_User'], album_get_user_name($user_id)), 'U_YOUR_PERSONAL_GALLERY' => append_sid(album_append_uid('album.' . PHP_EXT . '?user_id=' . $user->data['user_id'])), 'L_YOUR_PERSONAL_GALLERY' => $lang['Your_Personal_Gallery'], 'U_USERS_PERSONAL_GALLERIES' => append_sid(album_append_uid('album_personal_index.' . PHP_EXT)), 'L_USERS_PERSONAL_GALLERIES' => $lang['Users_Personal_Galleries']));
            $cols_span = album_generate_index_columns($username);
            // but we need to specificly specify if we want to show the public gallery header
            if ($show_public_footer == true) {
                $template->assign_block_vars('catfooter.cat_public_footer', array('U_YOUR_PERSONAL_GALLERY' => append_sid(album_append_uid('album.' . PHP_EXT . '?user_id=' . $user->data['user_id'])), 'L_YOUR_PERSONAL_GALLERY' => $lang['Your_Personal_Gallery'], 'U_USERS_PERSONAL_GALLERIES' => append_sid(album_append_uid('album_personal_index.' . PHP_EXT)), 'L_USERS_PERSONAL_GALLERIES' => $lang['Users_Personal_Galleries'], 'FOOTER_COL_SPAN' => $cols_span));
                if ($album_config['show_otf_link'] == 1) {
                    $template->assign_block_vars('catfooter.cat_public_footer.show_otf_link', array());
                }
                if ($album_config['show_all_pics_link'] == 1) {
                    $template->assign_block_vars('catfooter.cat_public_footer.show_all_pics_link', array());
                }
                if ($album_config['show_personal_galleries_link'] == 1) {
                    $template->assign_block_vars('catfooter.cat_public_footer.show_personal_galleries_link', array());
                }
            }
        }
        $template->assign_var_from_handle('ALBUM_BOARD_INDEX', 'album');
    }
    return $display;
}