/** * For every image in the album, look for its file. Delete from the database * if the file does not exist. Same for each sub-directory/album. * * @param bool $deep set to true for a thorough cleansing */ function garbageCollect($deep = false) { if (is_null($this->images)) { $this->getImages(); } $result = query("SELECT * FROM " . prefix('images') . " WHERE `albumid` = '" . $this->id . "'"); $dead = array(); $live = array(); $files = $this->loadFileNames(); // Does the filename from the db row match any in the files on disk? while ($row = db_fetch_assoc($result)) { if (!in_array($row['filename'], $files)) { // In the database but not on disk. Kill it. $dead[] = $row['id']; } else { if (in_array($row['filename'], $live)) { // Duplicate in the database. Kill it. $dead[] = $row['id']; // Do something else here? Compare titles/descriptions/metadata/update dates to see which is the latest? } else { $live[] = $row['filename']; } } } db_free_result($result); if (count($dead) > 0) { $sql = "DELETE FROM " . prefix('images') . " WHERE `id` = '" . array_pop($dead) . "'"; $sql2 = "DELETE FROM " . prefix('comments') . " WHERE `type`='albums' AND `ownerid` = '" . array_pop($dead) . "'"; foreach ($dead as $id) { $sql .= " OR `id` = '{$id}'"; $sql2 .= " OR `ownerid` = '{$id}'"; } query($sql); query($sql2); } // Get all sub-albums and make sure they exist. $result = query("SELECT * FROM " . prefix('albums') . " WHERE `folder` LIKE " . db_quote(db_LIKE_escape($this->name) . '%')); $dead = array(); $live = array(); // Does the dirname from the db row exist on disk? while ($row = db_fetch_assoc($result)) { if (!is_dir(ALBUM_FOLDER_SERVERPATH . internalToFilesystem($row['folder'])) || in_array($row['folder'], $live) || substr($row['folder'], -1) == '/' || substr($row['folder'], 0, 1) == '/') { $dead[] = $row['id']; } else { $live[] = $row['folder']; } } db_free_result($result); if (count($dead) > 0) { $sql = "DELETE FROM " . prefix('albums') . " WHERE `id` = '" . array_pop($dead) . "'"; $sql2 = "DELETE FROM " . prefix('comments') . " WHERE `type`='albums' AND `ownerid` = '" . array_pop($dead) . "'"; foreach ($dead as $albumid) { $sql .= " OR `id` = '{$albumid}'"; $sql2 .= " OR `ownerid` = '{$albumid}'"; } query($sql); query($sql2); } if ($deep) { foreach ($this->getAlbums(0) as $dir) { $subalbum = newAlbum($dir); // Could have been deleted if it didn't exist above... if ($subalbum->exists) { $subalbum->garbageCollect($deep); } } } }
/** * Returns an array of album ids whose parent is the folder * @param string $albumfolder folder name if you want a album different >>from the current album * @return array */ function getAllSubAlbumIDs($albumfolder = '') { global $_zp_current_album; if (empty($albumfolder)) { if (isset($_zp_current_album)) { $albumfolder = $_zp_current_album->getFileName(); } else { return null; } } $query = "SELECT `id`,`folder`, `show` FROM " . prefix('albums') . " WHERE `folder` LIKE " . db_quote(db_LIKE_escape($albumfolder) . '%'); $subIDs = query_full_array($query); return $subIDs; }
/** * returns the non-empty value of $field from the album or one of its parents * * @param string $folder the album name * @param string $field the desired field name * @param int $id will be set to the album `id` of the album which has the non-empty field * @return string */ function getAlbumInherited($folder, $field, &$id) { $folders = explode('/', filesystemToInternal($folder)); $album = array_shift($folders); $like = ' LIKE ' . db_quote(db_LIKE_escape($album)); while (count($folders) > 0) { $album .= '/' . array_shift($folders); $like .= ' OR `folder` LIKE ' . db_quote(db_LIKE_escape($album)); } $sql = 'SELECT `id`, `' . $field . '` FROM ' . prefix('albums') . ' WHERE `folder`' . $like; $result = query_full_array($sql); if (!is_array($result)) { return ''; } while (count($result) > 0) { $try = array_pop($result); if (!empty($try[$field])) { $id = $try['id']; return $try[$field]; } } return ''; }
/** * 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; }
function db_show($what, $aux = '') { global $_zp_DB_details; switch ($what) { case 'tables': $sql = "SHOW TABLES FROM `" . $_zp_DB_details['mysql_database'] . "` LIKE '" . db_LIKE_escape($_zp_DB_details['mysql_prefix']) . "%'"; return query($sql, false); case 'columns': $sql = 'SHOW FULL COLUMNS FROM `' . $_zp_DB_details['mysql_prefix'] . $aux . '`'; return query($sql, true); case 'variables': $sql = "SHOW VARIABLES LIKE '{$aux}'"; return query_full_array($sql); case 'index': $sql = "SHOW INDEX FROM `" . $_zp_DB_details['mysql_database'] . '`.' . $aux; return query_full_array($sql); } }
/** * Prints a tag cloud list of the tags in one album and optionally its subalbums. Returns FALSE if no value. * * @param string $albumname folder name of the album to get the tags from ($subalbums = true this is the base albums)- This value is mandatory. * @param bool $subalbums TRUE if the tags of subalbum should be. FALSE is default * @param string $mode "images" for image tags, "albums" for album tags."images" is default. * @return array */ function getAllTagsFromAlbum($albumname, $subalbums = false, $mode = 'images') { global $_zp_gallery; $passwordcheck = ''; $imageWhere = ''; $tagWhere = ""; if (empty($albumname)) { return FALSE; } $albumobj = newAlbum($albumname); if (!$albumobj->exists) { return FALSE; } if (zp_loggedin()) { $albumWhere = "WHERE `dynamic`=0"; } else { $albumscheck = query_full_array("SELECT * FROM " . prefix('albums') . " ORDER BY title"); foreach ($albumscheck as $albumcheck) { if (!checkAlbumPassword($albumcheck['folder'])) { $albumpasswordcheck = " AND id != " . $albumcheck['id']; $passwordcheck = $passwordcheck . $albumpasswordcheck; } } $albumWhere = "WHERE `dynamic`=0 AND `show`=1" . $passwordcheck; } if ($subalbums) { $albumWhere .= " AND `folder` LIKE " . db_quote(db_LIKE_escape($albumname) . "%"); } else { $albumWhere .= " AND `folder` = " . db_quote($albumname); } $albumids = query_full_array("SELECT id, folder FROM " . prefix('albums') . $albumWhere); switch ($mode) { case "images": if (count($albumids) == 0) { return FALSE; } else { $imageWhere = " WHERE "; $count = ""; foreach ($albumids as $albumid) { $count++; $imageWhere .= 'albumid=' . $albumid['id']; if ($count != count($albumids)) { $imageWhere .= " OR "; } } } $imageids = query_full_array("SELECT id, albumid FROM " . prefix('images') . $imageWhere); // if the album has no direct images and $subalbums is set to false if (count($imageids) == 0) { return FALSE; } else { $count = ""; $tagWhere = " WHERE "; foreach ($imageids as $imageid) { $count++; $tagWhere .= '(o.objectid =' . $imageid['id'] . " AND o.tagid = t.id AND o.type = 'images')"; if ($count != count($imageids)) { $tagWhere .= " OR "; } } } if (empty($tagWhere)) { return FALSE; } else { $tags = query_full_array("SELECT DISTINCT t.name, t.id, (SELECT DISTINCT COUNT(*) FROM " . prefix('obj_to_tag') . " WHERE tagid = t.id AND type = 'images') AS count FROM " . prefix('obj_to_tag') . " AS o," . prefix('tags') . " AS t" . $tagWhere . " ORDER BY t.name"); } break; case "albums": $count = ""; if (count($albumids) == 0) { return FALSE; } else { $tagWhere = " WHERE "; foreach ($albumids as $albumid) { $count++; $tagWhere .= '(o.objectid =' . $albumid['id'] . " AND o.tagid = t.id AND o.type = 'albums')"; if ($count != count($albumids)) { $tagWhere .= " OR "; } } } if (empty($tagWhere)) { return FALSE; } else { $tags = query_full_array("SELECT DISTINCT t.name, t.id, (SELECT DISTINCT COUNT(*) FROM " . prefix('obj_to_tag') . " WHERE tagid = t.id AND o.type = 'albums') AS count FROM " . prefix('obj_to_tag') . " AS o," . prefix('tags') . " AS t" . $tagWhere . " ORDER BY t.name"); } break; } return $tags; }
/** * Retrieves the tags for an object * Returns them in an array * * @param int $id the record id of the album/image * @param string $tbl 'albums' or 'images', etc. * @return array */ function readTags($id, $tbl, $language) { global $_zp_current_locale; if (is_null($language)) { switch (getOption('languageTagSearch')) { case 1: $language = substr($_zp_current_locale, 0, 2); break; case 2: $language = $_zp_current_locale; break; default: $langage = ''; break; } } $tags = array(); $sql = 'SELECT * FROM ' . prefix('tags') . ' AS tags, ' . prefix('obj_to_tag') . ' AS objects WHERE `type`="' . $tbl . '" AND `objectid`="' . $id . '" AND tagid=tags.id'; if ($language) { $sql .= ' AND (tags.language="" OR tags.language LIKE ' . db_quote(db_LIKE_escape($language) . '%') . ')'; } $result = query($sql); if ($result) { while ($row = db_fetch_assoc($result)) { $tags[mb_strtolower($row['name'])] = $row['name']; } db_free_result($result); } natcasesort($tags); return $tags; }
/** * Searches the table for tags * Returns an array of database records. * * @param array $searchstring * @param string $tbl set DB table name to be searched * @param string $sorttype what to sort on * @param string $sortdirection what direction * @return array */ protected function searchFieldsAndTags($searchstring, $tbl, $sorttype, $sortdirection) { global $_zp_gallery; $weights = $idlist = array(); $sql = $allIDs = NULL; $tagPattern = $this->tagPattern; // create an array of [tag, objectid] pairs for tags $tag_objects = array(); $fields = $this->fieldList; if (count($fields) == 0) { // then use the default ones $fields = $this->allowedSearchFields(); } foreach ($fields as $key => $field) { switch ($field) { case 'news_categories': if ($tbl != 'news') { break; } unset($fields[$key]); query('SET @serachfield="news_categories"'); $tagsql = 'SELECT @serachfield AS field, t.`title` AS name, o.`news_id` AS `objectid` FROM ' . prefix('news_categories') . ' AS t, ' . prefix('news2cat') . ' AS o WHERE t.`id`=o.`cat_id` AND ('; foreach ($searchstring as $singlesearchstring) { switch ($singlesearchstring) { case '&': case '!': case '|': case '(': case ')': break; case '*': $targetfound = true; $tagsql .= "COALESCE(title, '') != '' OR "; break; default: $targetfound = true; $tagsql .= '`title` = ' . db_quote($singlesearchstring) . ' OR '; } } $tagsql = substr($tagsql, 0, strlen($tagsql) - 4) . ') ORDER BY t.`id`'; $objects = query_full_array($tagsql, false); if (is_array($objects)) { $tag_objects = $objects; } break; case 'tags_exact': $tagPattern = array('type' => '=', 'open' => '', 'close' => ''); case 'tags': unset($fields[$key]); query('SET @serachfield="tags"'); $tagsql = 'SELECT @serachfield AS field, t.`name`, o.`objectid` FROM ' . prefix('tags') . ' AS t, ' . prefix('obj_to_tag') . ' AS o WHERE t.`id`=o.`tagid` AND o.`type`="' . $tbl . '" AND ('; foreach ($searchstring as $singlesearchstring) { switch ($singlesearchstring) { case '&': case '!': case '|': case '(': case ')': break; case '*': query('SET @emptyfield="*"'); $tagsql = str_replace('t.`name`', '@emptyfield as name', $tagsql); $tagsql .= "t.`name` IS NOT NULL OR "; break; default: $targetfound = true; if ($tagPattern['type'] == 'like') { $target = db_LIKE_escape($singlesearchstring); } else { $target = $singlesearchstring; } $tagsql .= 't.`name` ' . strtoupper($tagPattern['type']) . ' ' . db_quote($tagPattern['open'] . $target . $tagPattern['close']) . ' OR '; } } $tagsql = substr($tagsql, 0, strlen($tagsql) - 4) . ') ORDER BY t.`id`'; $objects = query_full_array($tagsql, false); if (is_array($objects)) { $tag_objects = array_merge($tag_objects, $objects); } break; default: break; } } // create an array of [name, objectid] pairs for the search fields. $field_objects = array(); if (count($fields) > 0) { $columns = array(); $dbfields = db_list_fields($tbl); if (is_array($dbfields)) { foreach ($dbfields as $row) { $columns[] = strtolower($row['Field']); } } foreach ($searchstring as $singlesearchstring) { switch ($singlesearchstring) { case '!': case '&': case '|': case '(': case ')': break; default: $targetfound = true; query('SET @serachtarget=' . db_quote($singlesearchstring)); foreach ($fields as $fieldname) { if ($tbl == 'albums' && strtolower($fieldname) == 'filename') { $fieldname = 'folder'; } else { $fieldname = strtolower($fieldname); } if ($fieldname && in_array($fieldname, $columns)) { query('SET @serachfield=' . db_quote($fieldname)); switch ($singlesearchstring) { case '*': $sql = 'SELECT @serachtarget AS name, @serachfield AS field, `id` AS `objectid` FROM ' . prefix($tbl) . ' WHERE (' . "COALESCE(`{$fieldname}`, '') != ''" . ') ORDER BY `id`'; break; default: if ($this->pattern['type'] == 'like') { $target = db_LIKE_escape($singlesearchstring); } else { $target = $singlesearchstring; } $fieldsql = ' `' . $fieldname . '` ' . strtoupper($this->pattern['type']) . ' ' . db_quote($this->pattern['open'] . $target . $this->pattern['close']); $sql = 'SELECT @serachtarget AS name, @serachfield AS field, `id` AS `objectid` FROM ' . prefix($tbl) . ' WHERE (' . $fieldsql . ') ORDER BY `id`'; } $objects = query_full_array($sql, false); if (is_array($objects)) { $field_objects = array_merge($field_objects, $objects); } } } } } } // now do the boolean logic of the search string $exact = $tagPattern['type'] == '='; $objects = array_merge($tag_objects, $field_objects); if (count($objects) != 0) { $tagid = ''; $taglist = array(); foreach ($objects as $object) { $tagid = strtolower($object['name']); if (!isset($taglist[$tagid]) || !is_array($taglist[$tagid])) { $taglist[$tagid] = array(); } $taglist[$tagid][] = $object['objectid']; } $op = ''; $idstack = array(); $opstack = array(); while (count($searchstring) > 0) { $singlesearchstring = array_shift($searchstring); switch ($singlesearchstring) { case '&': case '!': case '|': $op = $op . $singlesearchstring; break; case '(': array_push($idstack, $idlist); array_push($opstack, $op); $idlist = array(); $op = ''; break; case ')': $objectid = $idlist; $idlist = array_pop($idstack); $op = array_pop($opstack); switch ($op) { case '&': if (is_array($objectid)) { $idlist = array_intersect($idlist, $objectid); } else { $idlist = array(); } break; case '!': break; // Paren followed by NOT is nonsensical? // Paren followed by NOT is nonsensical? case '&!': if (is_array($objectid)) { $idlist = array_diff($idlist, $objectid); } break; case '': case '|': if (is_array($objectid)) { $idlist = array_merge($idlist, $objectid); } break; } $op = ''; break; default: $lookfor = strtolower($singlesearchstring); $objectid = NULL; foreach ($taglist as $key => $objlist) { if ($exact && $lookfor == $key || !$exact && preg_match('|' . preg_quote($lookfor) . '|', $key)) { if (is_array($objectid)) { $objectid = array_merge($objectid, $objlist); } else { $objectid = $objlist; } } } switch ($op) { case '&': if (is_array($objectid)) { $idlist = array_intersect($idlist, $objectid); } else { $idlist = array(); } break; case '!': if (is_null($allIDs)) { $allIDs = array(); $result = query("SELECT `id` FROM " . prefix($tbl)); if ($result) { while ($row = db_fetch_assoc($result)) { $allIDs[] = $row['id']; } db_free_result($result); } } if (is_array($objectid)) { $idlist = array_merge($idlist, array_diff($allIDs, $objectid)); } break; case '&!': if (is_array($objectid)) { $idlist = array_diff($idlist, $objectid); } break; case '': case '|': if (is_array($objectid)) { $idlist = array_merge($idlist, $objectid); } break; } $op = ''; break; } } } // we now have an id list of the items that were found and will create the SQL Search to retrieve their records if (count($idlist) > 0) { $weights = array_count_values($idlist); arsort($weights, SORT_NUMERIC); $sql = 'SELECT DISTINCT `id`,`show`,'; switch ($tbl) { case 'news': if ($this->search_unpublished || zp_loggedin(MANAGE_ALL_NEWS_RIGHTS)) { $show = ''; } else { $show = "`show` = 1 AND "; } $sql .= '`titlelink` '; if (is_array($this->category_list)) { $news_list = $this->subsetNewsCategories(); $idlist = array_intersect($news_list, $idlist); if (count($idlist) == 0) { return array(false, array()); } } if (empty($sorttype)) { $key = '`date` DESC'; } else { $key = trim($sorttype . $sortdirection); } if ($show) { $show .= '`date`<=' . db_quote(date('Y-m-d H:i:s')) . ' AND '; } break; case 'pages': if (zp_loggedin(MANAGE_ALL_PAGES_RIGHTS)) { $show = ''; } else { $show = "`show` = 1 AND "; } $sql .= '`titlelink` '; if ($show) { $show .= '`date`<=' . db_quote(date('Y-m-d H:i:s')) . ' AND '; } $key = '`sort_order`'; break; case 'albums': if ($this->search_unpublished || zp_loggedin()) { $show = ''; } else { $show = "`show` = 1 AND "; } $sql .= "`folder`, `title` "; if (is_null($sorttype)) { if (empty($this->album)) { list($key, $sortdirection) = $this->sortKey($_zp_gallery->getSortType(), $sortdirection, 'title', 'albums'); if ($_zp_gallery->getSortDirection()) { $key .= " DESC"; } } else { $key = $this->album->getAlbumSortKey(); if ($key != '`sort_order`' && $key != 'RAND()') { if ($this->album->getSortDirection('album')) { $key .= " DESC"; } } } } else { list($key, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'albums'); $key = trim($key . ' ' . $sortdirection); } break; default: // images if ($this->search_unpublished || zp_loggedin()) { $show = ''; } else { $show = "`show` = 1 AND "; } $sql .= "`albumid`, `filename`, `title` "; if (is_null($sorttype)) { if (empty($this->album)) { list($key, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'images'); if ($sortdirection) { $key .= " DESC"; } } else { $key = $this->album->getImageSortKey(); if ($key != '`sort_order`') { if ($this->album->getSortDirection('image')) { $key .= " DESC"; } } } } else { list($key, $sortdirection) = $this->sortKey($sorttype, $sortdirection, 'title', 'images'); $key = trim($key . ' ' . $sortdirection); } break; } $sql .= "FROM " . prefix($tbl) . " WHERE " . $show; $sql .= '(' . self::compressedIDList($idlist) . ')'; $sql .= " ORDER BY " . $key; return array($sql, $weights); } return array(false, array()); }