[TikiWiki-commits] [Git][tikiwiki/tiki][master] [FIX] File Galleries: Ensure gallery object handles "not found" cases gracefully
Benoit Grégoire (@benoitg) via TikiWiki-cvs <[email protected]>
| Newsgroups | gmane.comp.cms.tiki.cvs |
|---|---|
| Message-ID | <696912a18bc8c_2b181dfc200f@gitlab-sidekiq-low-urgency-cpu-bound-v2-f5cc6c9bf-qcnvn.mail> |
Benoit Grégoire pushed to branch master at Tiki Wiki CMS Groupware / Tiki
Commits:
a5afd27c by MAGENE Sem Joel at 2026-01-15T16:06:40+00:00
[FIX] File Galleries: Ensure gallery object handles "not found" cases gracefully
---
* [FIX][REF] File Galleries: Ensure gallery object is complete and handle "not found" cases gracefully.
See merge request tikiwiki/tiki!8467
- - - - -
2 changed files:
- lib/Filegals/FileGalLib.php
- lib/wiki-plugins/wikiplugin_files.php
Changes:
=====================================
lib/Filegals/FileGalLib.php
=====================================
@@ -808,7 +808,7 @@ class FileGalLib extends TikiLib
}
$file = $return[0];
-
+ $this->verifyFileGalleryIntegrity($file);
if ($use_draft && $prefs['feature_file_galleries_save_draft'] == 'y') {
$draft = $this->table('tiki_file_drafts')->fetchRow(
['filename', 'filesize', 'filetype', 'data', 'user', 'path', 'hash', 'lastModif', 'lockedby'],
@@ -2420,9 +2420,15 @@ class FileGalLib extends TikiLib
$where = 'tf.`galleryId`=? order by ' . $this->convertSortMode('random') . ' limit 1 ';
$bindvars[] = (int)$randomGalleryId;
}
- $query = "select tf.*, tfg.`backlinkPerms` from `tiki_files` tf left join `tiki_file_galleries` tfg on (tfg.`galleryId`=tf.`galleryId`) where $where";
+ $query = "select tf.*, tfg.`backlinkPerms`, tfg.galleryId as validGalleryId
+ from `tiki_files` tf
+ left join `tiki_file_galleries` tfg on (tfg.`galleryId`=tf.`galleryId`)
+ where $where";
+
$result = $this->query($query, $bindvars);
- return $result ? $result->fetchRow() : [];
+ $file = $result ? $result->fetchRow() : [];
+ $this->verifyFileGalleryIntegrity($file);
+ return $file;
}
/**
@@ -2460,10 +2466,13 @@ class FileGalLib extends TikiLib
} else {
$name = substr($name, 0, $max);
}
- $query = "select `fileId`,`path`,`galleryId`,`filename`,`filetype`,`data`,`filesize`,`name`,`description`,
- `created`, `lastModif` from `tiki_files` where `galleryId`=? AND `$column`=? ORDER BY created DESC LIMIT 1";
+ $query = "select tf.*, tfg.galleryId as validGalleryId
+ from `tiki_files` tf
+ left join `tiki_file_galleries` tfg on (tf.`galleryId`=tfg.`galleryId`)
+ where tf.`galleryId`=? AND tf.`$column`=? ORDER BY tf.created DESC LIMIT 1";
$result = $this->query($query, [(int) $galleryId, $name]);
$res = $result->fetchRow();
+ $this->verifyFileGalleryIntegrity($res);
return $res;
}
@@ -2474,10 +2483,14 @@ class FileGalLib extends TikiLib
} else {
$filename = substr($filename, 0, 80);
}
- $query = "select `fileId`,`path`,`galleryId`,`filename`,`filetype`,`data`,`filesize`,`name`,`description`,
- `created` from `tiki_files` where `filename`=? ORDER BY created DESC LIMIT 1";
+ $query = "select tf.*, tfg.galleryId as validGalleryId
+ from `tiki_files` tf
+ left join `tiki_file_galleries` tfg on (tf.`galleryId`=tfg.`galleryId`)
+ where tf.`filename`=? ORDER BY tf.created DESC LIMIT 1";
$result = $this->query($query, [$filename]);
- return $result->fetchRow();
+ $file = $result->fetchRow();
+ $this->verifyFileGalleryIntegrity($file);
+ return $file;
}
public function get_file_gallery_by_name($parentId, $name)
@@ -4335,4 +4348,29 @@ class FileGalLib extends TikiLib
return $listfgals;
}
+
+ /**
+ * Checks if the gallery referenced by a file exists.
+ * Throws an exception if the record is orphaned to prevent silent failures.
+ *
+ * @param array|bool|null $file The file row from the database
+ * @return void
+ * @throws Exception
+ */
+ private function verifyFileGalleryIntegrity(&$file): void
+ {
+ // Guard clause: If file wasn't found we can't check it.
+ if (! is_array($file) || empty($file['galleryId'])) {
+ return;
+ }
+
+ if (array_key_exists('validGalleryId', $file)) {
+ $invalid = $file['validGalleryId'] === null;
+ unset($file['validGalleryId']);
+
+ if ($invalid) {
+ throw new Exception(tr('Orphaned file record detected: File %0 points to non-existent gallery %1', $file['fileId'] ?? 'unknown', $file['galleryId']));
+ }
+ }
+ }
}
=====================================
lib/wiki-plugins/wikiplugin_files.php
=====================================
@@ -537,7 +537,7 @@ function wikiplugin_files($data, $params)
}
$gal_info = $filegallib->get_file_gallery($galId);
if (empty($gal_info)) {
- $gal_info = [];
+ return '~np~' . tr('File gallery "%0" not found', $galId) . '~/np~';
}
$gal_info['name'] = $filegallib->get_user_gallery_name($gal_info, $user);
@@ -610,6 +610,9 @@ function wikiplugin_files($data, $params)
// get the files of the gallery
foreach ($objects['data'] as $og) {
$gal_info = $filegallib->get_file_gallery($og['itemId']);
+ if (empty($gal_info)) {
+ continue; // Skip this gallery if it doesn't exist
+ }
$fs = $filegallib->get_files(0, $max, $sort, '', $og['itemId'], false, $withsubgals == 'y', false, true, false, $show_parentName == 'y', true, $recursive, '', false, false, false, $filter);
if ($fs['count']) {
for ($i = 0, $count_fs_data = count($fs['data']); $i < $count_fs_data; ++$i) {
@@ -729,7 +732,7 @@ function wikiplugin_files_check_perm_file($fileId)
Feedback::error(tr('File %0 does not exist', $fileId));
return $info;
}
- $gal_info = $filegallib->get_file_gallery($info['galleryId']);
+ $gal_info = $filegallib->get_file_gallery($info['galleryId']);
if ($tiki_p_admin != 'y' && $tiki_p_admin_files_galleries != 'y' && $gal_info['user'] != $user) {
$info['p_view_file_gallery'] = $tikilib->user_has_perm_on_object($user, $info['fileId'], 'file', 'tiki_p_view_file_gallery') ? 'y' : 'n';
if ($info['p_view_file_gallery'] != 'y') {
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/a5afd27cba6b76b5edd35588591d7ff05b2fbc55
--
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/a5afd27cba6b76b5edd35588591d7ff05b2fbc55
You're receiving this email because of your account on gitlab.com.
_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs