From 73bcaace5335bbe885824dcb25c70855393730af Mon Sep 17 00:00:00 2001 From: Lee Garner Date: Mon, 22 Aug 2022 08:57:05 -0700 Subject: [PATCH 1/2] Fix #555, limit to uploadable categories on file upload form --- .../filemgmt/classes/Category.class.php | 95 +++++++++++++------ .../filemgmt/classes/Download.class.php | 2 +- 2 files changed, 66 insertions(+), 31 deletions(-) diff --git a/private/plugins/filemgmt/classes/Category.class.php b/private/plugins/filemgmt/classes/Category.class.php index 92702a8a1..e35698818 100644 --- a/private/plugins/filemgmt/classes/Category.class.php +++ b/private/plugins/filemgmt/classes/Category.class.php @@ -113,16 +113,23 @@ public function Read($id = 0) return; } - $sql = "SELECT * FROM {$_TABLES['filemgmt_cat']} - WHERE cid = '$id'"; - $result = DB_query($sql); - if (!$result || DB_numRows($result) != 1) { - return false; - } else { - $row = DB_fetchArray($result, false); + try { + $row = Database::getInstance()->conn->executeQuery( + "SELECT * FROM {$_TABLES['filemgmt_cat']} + WHERE cid = '$id'", + array($id), + array(Database::INTEGER) + )->fetchAssociative(); + } catch (\Throwable $e) { + Log::write('system', Log::ERROR, __METHOD__ . ': ' . $e->getMessage()); + $row = false; + } + if (is_array($row)) { $this->setVars($row, true); $this->isNew = false; return true; + } else { + return false; } } @@ -710,24 +717,42 @@ public static function getAll() } - public static function getChildren($pid=0, $checkAccess=true) + /** + * Get all the child categories from a given root. + * + * @param integer $pid Root category ID + * @param integer $checkAccess 1 to check view, 2 to check write + * @return array Array of categories + */ + public static function getChildren(int $pid=0, int $checkAccess=1) : array { global $_TABLES, $_GROUPS; $retval = array(); $pid = (int)$pid; - $sql = "SELECT * FROM {$_TABLES['filemgmt_cat']} WHERE pid = $pid "; + $sql = "SELECT * FROM {$_TABLES['filemgmt_cat']} WHERE pid = ? "; + $values = array($pid); + $types = array(Database::INTEGER); if ($checkAccess) { - if (count($_GROUPS) == 1) { - $sql .= " AND grp_access = '" . current($_GROUPS) ."' "; + $values[] = array_values($_GROUPS); + $types[] = Database::PARAM_INT_ARRAY; + if ($checkAccess == 2) { + $sql .= " AND grp_writeaccess IN (?) "; } else { - $sql .= " AND grp_access IN (" . implode(',',array_values($_GROUPS)) .") "; + $sql .= " AND grp_access IN (?) "; } } $sql .= "ORDER BY cid"; - $result = DB_query($sql); - while ($A = DB_fetchArray($result, false)) { - $retval[$A['cid']] = new self($A); + try { + $stmt = Database::getInstance()->conn->executeQuery($sql, $values, $types); + } catch (\Throwable $e) { + Log::write('system', Log::ERROR, __METHOD__ . ': ' . $e->getMessage()); + $stmt = false; + } + if ($stmt) { + while ($A = $stmt->fetchAssociative()) { + $retval[$A['cid']] = new self($A); + } } return $retval; } @@ -785,7 +810,7 @@ public function getDscp() * @param integer $current_cat Current category ID, to set "selected" * @return string HTML for selection options */ - public static function getChildOptions($pid, $indent, $current_cat) + public static function getChildOptions(int $pid, string $indent, int $current_cat) : string { global $_TABLES; @@ -793,21 +818,31 @@ public static function getChildOptions($pid, $indent, $current_cat) $retval = ''; $spaces = ($indent+1) * 2; - $sql = "SELECT * FROM {$_TABLES['filemgmt_cat']} - WHERE pid = $pid - ORDER BY title ASC"; - $result = DB_query($sql); - while (($C = DB_fetchArray($result)) != NULL) { - $retval .= ''; - $retval .= self::getChildOptions($C['cid'], $indent+1, $current_cat); } return $retval; } diff --git a/private/plugins/filemgmt/classes/Download.class.php b/private/plugins/filemgmt/classes/Download.class.php index 406c73796..2def1ae5f 100644 --- a/private/plugins/filemgmt/classes/Download.class.php +++ b/private/plugins/filemgmt/classes/Download.class.php @@ -1018,7 +1018,7 @@ public function edit($post=array()) $pathstring .= "lid}\">{$hdr_title}"; $categorySelectHTML = ''; - $rootCats = Category::getChildren(0, true); + $rootCats = Category::getChildren(0, 2); foreach ($rootCats as $cid=>$Cat) { $categorySelectHTML .= '