/**
 * Returns  a randomly selected image from the album or its subalbums. (May be NULL if none exists)
 *
 * @param mixed $rootAlbum optional album object/folder from which to get the image.
 * @param bool $daily set to true to change picture only once a day.
 *
 * @return object
 */
function getRandomImagesAlbum($rootAlbum = NULL, $daily = false)
{
    global $_zp_current_album, $_zp_gallery, $_zp_current_search;
    if (empty($rootAlbum)) {
        $album = $_zp_current_album;
    } else {
        if (is_object($rootAlbum)) {
            $album = $rootAlbum;
        } else {
            $album = newAlbum($rootAlbum);
        }
    }
    if ($daily && ($potd = getOption('picture_of_the_day:' . $album->name))) {
        $potd = getSerializedArray($potd);
        if (date('Y-m-d', $potd['day']) == date('Y-m-d')) {
            $rndalbum = newAlbum($potd['folder']);
            $image = newImage($rndalbum, $potd['filename']);
            if ($image->exists) {
                return $image;
            }
        }
    }
    $image = NULL;
    if ($album->isDynamic()) {
        $images = $album->getImages(0);
        shuffle($images);
        while (count($images) > 0) {
            $result = array_pop($images);
            if (Gallery::validImage($result['filename'])) {
                $image = newImage(newAlbum($result['folder']), $result['filename']);
            }
        }
    } else {
        $albumfolder = $album->getFileName();
        if ($album->isMyItem(LIST_RIGHTS)) {
            $imageWhere = '';
            $albumInWhere = '';
        } else {
            $imageWhere = " AND " . prefix('images') . ".show=1";
            $albumInWhere = prefix('albums') . ".show=1";
        }
        $query = "SELECT id FROM " . prefix('albums') . " WHERE ";
        if ($albumInWhere) {
            $query .= $albumInWhere . ' AND ';
        }
        $query .= "folder LIKE " . db_quote(db_LIKE_escape($albumfolder) . '%');
        $result = query($query);
        if ($result) {
            $albumInWhere = prefix('albums') . ".id IN (";
            while ($row = db_fetch_assoc($result)) {
                $albumInWhere = $albumInWhere . $row['id'] . ", ";
            }
            db_free_result($result);
            $albumInWhere = ' AND ' . substr($albumInWhere, 0, -2) . ')';
            $sql = 'SELECT `folder`, `filename` ' . ' FROM ' . prefix('images') . ', ' . prefix('albums') . ' WHERE ' . prefix('albums') . '.folder!="" AND ' . prefix('images') . '.albumid = ' . prefix('albums') . '.id ' . $albumInWhere . $imageWhere . ' ORDER BY RAND()';
            $result = query($sql);
            $image = filterImageQuery($result, $album->name);
        }
    }
    if ($image) {
        if ($daily) {
            $potd = array('day' => time(), 'folder' => $image->getAlbumName(), 'filename' => $image->getFileName());
            setThemeOption('picture_of_the_day:' . $album->name, serialize($potd), NULL, $_zp_gallery->getCurrentTheme());
        }
    }
    return $image;
}
/**
 * Returns a randomly selected image from the gallery. (May be NULL if none exists)
 * @param bool $daily set to true and the picture changes only once a day.
 *
 * @return object
 */
function getRandomImages($daily = false)
{
    global $_zp_gallery;
    if ($daily && ($potd = getOption('picture_of_the_day'))) {
        $potd = getSerializedArray($potd);
        if (date('Y-m-d', $potd['day']) == date('Y-m-d')) {
            $album = newAlbum($potd['folder'], true, true);
            if ($album->exists) {
                $image = newImage($album, $potd['filename'], true);
                if ($image->exists) {
                    return $image;
                }
            }
        }
    }
    if (zp_loggedin()) {
        $imageWhere = '';
    } else {
        $imageWhere = " AND " . prefix('images') . ".show=1";
    }
    $result = query('SELECT `folder`, `filename` ' . ' FROM ' . prefix('images') . ', ' . prefix('albums') . ' WHERE ' . prefix('albums') . '.folder!="" AND ' . prefix('images') . '.albumid = ' . prefix('albums') . '.id ' . $imageWhere . ' ORDER BY RAND()');
    $image = filterImageQuery($result, NULL);
    if ($image) {
        if ($daily) {
            $potd = array('day' => time(), 'folder' => $image->getAlbumName(), 'filename' => $image->getFileName());
            setThemeOption('picture_of_the_day', serialize($potd), NULL, $_zp_gallery->getCurrentTheme());
        }
        return $image;
    }
    return NULL;
}