function checkAlbumParentid($albumname, $id) { global $gallery; $album = new Album($gallery, $albumname); $oldid = $album->get('parentid'); if ($oldid !== $id) { $album->set('parentid', $id); $album->save(); if (is_null($oldid)) { $oldid = '<em>NULL</em>'; } if (is_null($id)) { $id = '<em>NULL</em>'; } printf('Fixed album <strong>%1$s</strong>: parentid was %2$s should have been %3$s<br />', $albumname, $oldid, $id); } $id = $album->id; $albums = $album->getSubalbums(); foreach ($albums as $albumname) { checkAlbumParentid($albumname, $id); } }
/** * * Checks for bad parentIDs from old move/copy bug * @param unknown_type $albumname * @param unknown_type $id */ function checkAlbumParentid($albumname, $id, $recorder) { $album = newAlbum($albumname); $oldid = $album->getParentID(); if ($oldid != $id) { $album->set('parentid', $id); $album->save(); if (is_null($oldid)) { $oldid = '<em>NULL</em>'; } if (is_null($id)) { $id = '<em>NULL</em>'; } $msg = sprintf('Fixed album <strong>%1$s</strong>: parentid was %2$s should have been %3$s<br />', $albumname, $oldid, $id); $recorder($msg, true); } $id = $album->getID(); if (!$album->isDynamic()) { $albums = $album->getAlbums(); foreach ($albums as $albumname) { checkAlbumParentid($albumname, $id, $recorder); } } }
} $sql = 'SELECT `id`, `codeblock` FROM ' . prefix('pages') . ' WHERE `codeblock` NOT REGEXP "^a:[0-9]+:{"'; $result = query_full_array($sql, false); if (is_array($result)) { foreach ($result as $row) { $codeblock = base64_decode($row['codeblock']); $sql = 'UPDATE ' . prefix('pages') . ' SET `codeblock`=' . db_quote($codeblock) . ' WHERE `id`=' . $row['id']; query($sql); } } } if ($debug == 'albumids') { // fixes 1.2 move/copy albums with wrong ids $albums = $_zp_gallery->getAlbums(); foreach ($albums as $album) { checkAlbumParentid($album, NULL, 'setuplog'); } } } if ($createTables) { if ($_zp_loggedin == ADMIN_RIGHTS) { $filelist = safe_glob(SERVERPATH . "/" . BACKUPFOLDER . '/*.zdb'); if (count($filelist) > 0) { $link = sprintf(gettext('You may <a href="%1$s">set your admin user and password</a> or <a href="%2$s">run backup-restore</a>'), WEBPATH . '/' . ZENFOLDER . '/admin-users.php?page=users', WEBPATH . '/' . ZENFOLDER . '/' . UTILITIES_FOLDER . '/backup_restore.php'); $autorun = false; } else { $link = sprintf(gettext('You need to <a href="%1$s">set your admin user and password</a>'), WEBPATH . '/' . ZENFOLDER . '/admin-users.php?page=users'); if ($autorun == 'admin' || $autorun == 'gallery') { $autorun = WEBPATH . '/' . ZENFOLDER . '/admin-users.php?page=users'; } }
/** For every album in the gallery, look for its file. Delete from the database * if the file does not exist. Do the same for images. Clean up comments that have * been left orphaned. * * Returns true if the operation was interrupted because it was taking too long * * @param bool $cascade garbage collect every image and album in the gallery. * @param bool $complete garbage collect every image and album in the *database* - completely cleans the database. * @param int $restart Image ID to restart scan from * @return bool */ function garbageCollect($cascade = true, $complete = false, $restart = '') { global $_zp_gallery, $_zp_authority; if (empty($restart)) { setOption('last_garbage_collect', time()); /* purge old search cache items */ $sql = 'DELETE FROM ' . prefix('search_cache'); if (!$complete) { $sql .= ' WHERE `date`<' . db_quote(date('Y-m-d H:m:s', time() - SEARCH_CACHE_DURATION * 60)); } $result = query($sql); /* clean the comments table */ $this->commentClean('images'); $this->commentClean('albums'); $this->commentClean('news'); $this->commentClean('pages'); // clean up obj_to_tag $dead = array(); $result = query("SELECT * FROM " . prefix('obj_to_tag')); if ($result) { while ($row = db_fetch_assoc($result)) { $tbl = $row['type']; $dbtag = query_single_row("SELECT `id` FROM " . prefix('tags') . " WHERE `id`='" . $row['tagid'] . "'", false); if (!$dbtag) { $dead[] = $row['id']; } $dbtag = query_single_row("SELECT `id` FROM " . prefix($tbl) . " WHERE `id`='" . $row['objectid'] . "'", false); if (!$dbtag) { $dead[] = $row['id']; } } db_free_result($result); } if (!empty($dead)) { $dead = array_unique($dead); query('DELETE FROM ' . prefix('obj_to_tag') . ' WHERE `id`=' . implode(' OR `id`=', $dead)); } // clean up admin_to_object $dead = array(); $result = query("SELECT * FROM " . prefix('admin_to_object')); if ($result) { while ($row = db_fetch_assoc($result)) { if (!$_zp_authority->validID($row['adminid'])) { $dead[] = $row['id']; } $tbl = $row['type']; $dbtag = query_single_row("SELECT `id` FROM " . prefix($tbl) . " WHERE `id`='" . $row['objectid'] . "'", false); if (!$dbtag) { $dead[] = $row['id']; } } db_free_result($result); } if (!empty($dead)) { $dead = array_unique($dead); query('DELETE FROM ' . prefix('admin_to_object') . ' WHERE `id`=' . implode(' OR `id`=', $dead)); } // clean up news2cat $dead = array(); $result = query("SELECT * FROM " . prefix('news2cat')); if ($result) { while ($row = db_fetch_assoc($result)) { $dbtag = query_single_row("SELECT `id` FROM " . prefix('news') . " WHERE `id`='" . $row['news_id'] . "'", false); if (!$dbtag) { $dead[] = $row['id']; } $dbtag = query_single_row("SELECT `id` FROM " . prefix('news_categories') . " WHERE `id`='" . $row['cat_id'] . "'", false); if (!$dbtag) { $dead[] = $row['id']; } } db_free_result($result); } if (!empty($dead)) { $dead = array_unique($dead); query('DELETE FROM ' . prefix('news2cat') . ' WHERE `id`=' . implode(' OR `id`=', $dead)); } // Check for the existence albums $dead = array(); $live = array(''); // purge the root album if it exists $deadalbumthemes = array(); // Load the albums from disk $result = query("SELECT * FROM " . prefix('albums')); while ($row = db_fetch_assoc($result)) { $albumpath = internalToFilesystem($row['folder']); $albumpath_valid = preg_replace('~/\\.*/~', '/', $albumpath); $albumpath_valid = ltrim(trim($albumpath_valid, '/'), './'); $illegal = $albumpath != $albumpath_valid; $valid = file_exists(ALBUM_FOLDER_SERVERPATH . $albumpath_valid) && (hasDynamicAlbumSuffix($albumpath_valid) || is_dir(ALBUM_FOLDER_SERVERPATH . $albumpath_valid)); if ($valid && $illegal) { // maybe there is only one record so we can fix it. $valid = query('UPDATE ' . prefix('albums') . ' SET `folder`=' . db_quote($albumpath_valid) . ' WHERE `id`=' . $row['id'], false); debugLog(sprintf(gettext('Invalid album folder: %1$s %2$s'), $albumpath, $valid ? gettext('fixed') : gettext('discarded'))); } if (!$valid || in_array($row['folder'], $live)) { $dead[] = $row['id']; if ($row['album_theme'] !== '') { // orphaned album theme options table $deadalbumthemes[$row['id']] = $row['folder']; } } else { $live[] = $row['folder']; } } db_free_result($result); if (count($dead) > 0) { /* delete the dead albums from the DB */ asort($dead); $criteria = '(' . implode(',', $dead) . ')'; $first = array_pop($dead); $sql1 = "DELETE FROM " . prefix('albums') . " WHERE `id` IN {$criteria}"; $n = query($sql1); if (!$complete && $n && $cascade) { $sql2 = "DELETE FROM " . prefix('images') . " WHERE `albumid` IN {$criteria}"; query($sql2); $sql3 = "DELETE FROM " . prefix('comments') . " WHERE `type`='albums' AND `ownerid` IN {$criteria}"; query($sql3); $sql4 = "DELETE FROM " . prefix('obj_to_tag') . " WHERE `type`='albums' AND `objectid` IN {$criteria}"; query($sql4); } } if (count($deadalbumthemes) > 0) { // delete the album theme options tables for dead albums foreach ($deadalbumthemes as $id => $deadtable) { $sql = 'DELETE FROM ' . prefix('options') . ' WHERE `ownerid`=' . $id; query($sql, false); } } } if ($complete) { if (empty($restart)) { /* check album parent linkage */ $albums = $_zp_gallery->getAlbums(); foreach ($albums as $album) { checkAlbumParentid($album, NULL, 'debuglog'); } /* refresh 'metadata' albums */ $albumids = query("SELECT `id`, `mtime`, `folder`, `dynamic` FROM " . prefix('albums')); if ($albumids) { while ($analbum = db_fetch_assoc($albumids)) { if (($mtime = filemtime(ALBUM_FOLDER_SERVERPATH . internalToFilesystem($analbum['folder']))) > $analbum['mtime']) { // refresh $album = newAlbum($analbum['folder']); $album->set('mtime', $mtime); if ($this->getAlbumUseImagedate()) { $album->setDateTime(NULL); } if ($album->isDynamic()) { $data = file_get_contents($album->localpath); $thumb = getOption('AlbumThumbSelect'); $words = $fields = ''; while (!empty($data)) { $data1 = trim(substr($data, 0, $i = strpos($data, "\n"))); if ($i === false) { $data1 = $data; $data = ''; } else { $data = substr($data, $i + 1); } if (strpos($data1, 'WORDS=') !== false) { $words = "words=" . urlencode(substr($data1, 6)); } if (strpos($data1, 'THUMB=') !== false) { $thumb = trim(substr($data1, 6)); } if (strpos($data1, 'FIELDS=') !== false) { $fields = "&searchfields=" . trim(substr($data1, 7)); } } if (!empty($words)) { if (empty($fields)) { $fields = '&searchfields=tags'; } } $album->set('search_params', $words . $fields); $album->set('thumb', $thumb); } $album->save(); zp_apply_filter('album_refresh', $album); } } db_free_result($albumids); } /* Delete all image entries that don't belong to an album at all. */ $albumids = query("SELECT `id` FROM " . prefix('albums')); /* all the album IDs */ $idsofalbums = array(); if ($albumids) { while ($row = db_fetch_assoc($albumids)) { $idsofalbums[] = $row['id']; } db_free_result($albumids); } $imageAlbums = query("SELECT DISTINCT `albumid` FROM " . prefix('images')); /* albumids of all the images */ $albumidsofimages = array(); if ($imageAlbums) { while ($row = db_fetch_assoc($imageAlbums)) { $albumidsofimages[] = $row['albumid']; } db_free_result($imageAlbums); } $orphans = array_diff($albumidsofimages, $idsofalbums); /* albumids of images with no album */ if (count($orphans) > 0) { /* delete dead images from the DB */ $sql = "DELETE FROM " . prefix('images') . " WHERE "; foreach ($orphans as $id) { if (is_null($id)) { $sql .= "`albumid` is NULL OR "; } else { $sql .= " `albumid`='" . $id . "' OR "; } } $sql = substr($sql, 0, -4); query($sql); // Then go into existing albums recursively to clean them... very invasive. foreach ($this->getAlbums(0) as $folder) { $album = newAlbum($folder); if (!$album->isDynamic()) { if (is_null($album->getDateTime())) { // see if we can get one from an image $images = $album->getImages(0, 0); if (count($images) > 0) { $image = newImage($album, array_shift($images)); $album->setDateTime($image->getDateTime()); $album->save(); } } $album->garbageCollect(true); } zp_apply_filter('album_refresh', $album); } } } /* Look for image records where the file no longer exists. While at it, check for images with IPTC data to update the DB */ $start = array_sum(explode(" ", microtime())); // protect against too much processing. if (!empty($restart)) { $restartwhere = ' WHERE `id`>' . $restart . ' AND `mtime`=0'; } else { $restartwhere = ' WHERE `mtime`=0'; } define('RECORD_LIMIT', 5); $sql = 'SELECT * FROM ' . prefix('images') . $restartwhere . ' ORDER BY `id` LIMIT ' . (RECORD_LIMIT + 2); $images = query($sql); if ($images) { $c = 0; while ($image = db_fetch_assoc($images)) { $albumobj = getItemByID('albums', $image['albumid']); if ($albumobj->exists && file_exists($imageName = internalToFilesystem(ALBUM_FOLDER_SERVERPATH . $albumobj->name . '/' . $image['filename']))) { if ($image['mtime'] != ($mtime = filemtime($imageName))) { // file has changed since we last saw it $imageobj = newImage($albumobj, $image['filename']); $imageobj->set('mtime', $mtime); $imageobj->updateMetaData(); // prime the EXIF/IPTC fields $imageobj->updateDimensions(); // update the width/height & account for rotation $imageobj->save(); zp_apply_filter('image_refresh', $imageobj); } } else { $sql = 'DELETE FROM ' . prefix('images') . ' WHERE `id`="' . $image['id'] . '";'; $result = query($sql); $sql = 'DELETE FROM ' . prefix('comments') . ' WHERE `type` IN (' . zp_image_types('"') . ') AND `ownerid` ="' . $image['id'] . '";'; $result = query($sql); } if (++$c >= RECORD_LIMIT) { return $image['id']; // avoide excessive processing } } db_free_result($images); } // cleanup the tables $resource = db_show('tables'); if ($resource) { while ($row = db_fetch_assoc($resource)) { $tbl = array_shift($row); query('OPTIMIZE TABLE `' . $tbl . '`'); } db_free_result($resource); } } return false; }
echo gettext('Done with table create!'); } else { echo gettext('Done with table create with errors!'); } } else { if ($createTables) { echo gettext('Done with table update'); } else { echo gettext('Done with table update with errors'); } } echo "</h3>"; // fixes 1.2 move/copy albums with wrong ids $albums = $_zp_gallery->getAlbums(); foreach ($albums as $album) { checkAlbumParentid($album, NULL); } } if ($createTables) { if (isset($_GET['delete_files'])) { $curdir = getcwd(); chdir(dirname(__FILE__) . '/setup'); $list = setup_glob('*.*'); chdir($curdir); $rslt = true; foreach ($list as $component) { if ($component != '..' && $component != '.') { $rslt = $rslt && setupDeleteComponent(@unlink(SERVERPATH . '/' . ZENFOLDER . '/setup/' . $component), $component); } } $rslt = $rslt && setupDeleteComponent(@unlink(SERVERPATH . '/' . ZENFOLDER . '/setup.php'), 'setup.php');