diff --git a/private/plugins/filemgmt/classes/Category.class.php b/private/plugins/filemgmt/classes/Category.class.php
index 92702a8a1..5ed6cbcbd 100644
--- a/private/plugins/filemgmt/classes/Category.class.php
+++ b/private/plugins/filemgmt/classes/Category.class.php
@@ -23,6 +23,9 @@
*/
class Category
{
+ const ACCESS_READ = 1;
+ const ACCESS_WRITE = 2;
+
/** Category ID.
* @var integer */
private $cid = 0;
@@ -60,7 +63,6 @@ public function __construct($id=0)
global $_USER, $_VARS;
$this->isNew = true;
-
if (is_array($id)) {
$this->setVars($id, true);
} elseif ($id > 0) {
@@ -102,7 +104,7 @@ public function setVars($row, $fromDB=false)
* @param integer $id Optional ID. Current ID is used if zero.
* @return boolean True if a record was read, False on failure
*/
- public function Read($id = 0)
+ public function Read(int $id = 0) : bool
{
global $_TABLES;
@@ -110,19 +112,26 @@ public function Read($id = 0)
if ($id == 0) $id = $this->cid;
if ($id == 0) {
$this->error = 'Invalid ID in Read()';
- return;
+ return false;
}
- $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 = ?",
+ 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;
}
}
@@ -134,7 +143,7 @@ public function Read($id = 0)
* @param integer $cid Category ID
* @return object Category object
*/
- public static function getInstance($cid)
+ public static function getInstance(int $cid)
{
static $cats = array();
if (!isset($cats[$cid])) {
@@ -710,24 +719,45 @@ 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 ";
+ $qb = Database::getInstance()->conn->createQueryBuilder();
+ $qb->select('*')
+ ->from($_TABLES['filemgmt_cat'])
+ ->where('pid = :pid')
+ ->setParameter('pid', $pid, Database::INTEGER)
+ ->orderBy('cid', 'ASC');
+ $values = array($pid);
+ $types = array(Database::INTEGER);
if ($checkAccess) {
- if (count($_GROUPS) == 1) {
- $sql .= " AND grp_access = '" . current($_GROUPS) ."' ";
+ $values[] = array_values($_GROUPS);
+ if ($checkAccess == self::ACCESS_WRITE) {
+ $qb->andWhere('grp_writeaccess IN (:groups)');
} else {
- $sql .= " AND grp_access IN (" . implode(',',array_values($_GROUPS)) .") ";
+ $qb->andWhere('grp_access IN (:groups)');
}
+ $qb->setParameter('groups', array_values($_GROUPS), Database::PARAM_INT_ARRAY);
+ }
+ try {
+ $stmt = $qb->execute();
+ } catch (\Throwable $e) {
+ Log::write('system', Log::ERROR, __METHOD__ . ': ' . $e->getMessage());
+ $stmt = false;
}
- $sql .= "ORDER BY cid";
- $result = DB_query($sql);
- while ($A = DB_fetchArray($result, false)) {
- $retval[$A['cid']] = new self($A);
+ if ($stmt) {
+ while ($A = $stmt->fetchAssociative()) {
+ $retval[$A['cid']] = new self($A);
+ }
}
return $retval;
}
@@ -785,7 +815,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 +823,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 .= '