+ * @brief Select rows from the photo table
+ *
+ * @param array $fields Array of selected fields, empty for all
+ * @param array $conditions Array of fields for conditions
+ * @param array $params Array of several parameters
+ *
+ * @return boolean|array
+ *
+ * @throws \Exception
+ * @see \Friendica\Database\DBA::select
+ */
+ public static function select(array $fields = [], array $conditions = [], array $params = [])
+ {
+ if (empty($fields)) {
+ $selected = self::getFields();
+ }
+
+ $r = DBA::select("photo", $fields, $conditions, $params);
+ return DBA::toArray($r);
+ }
+
+ /**
+ * @brief Retrieve a single record from the photo table
+ *
+ * @param array $fields Array of selected fields, empty for all
+ * @param array $conditions Array of fields for conditions
+ * @param array $params Array of several parameters
+ *
+ * @return bool|array
+ *
+ * @throws \Exception
+ * @see \Friendica\Database\DBA::select
+ */
+ public static function selectFirst(array $fields = [], array $conditions = [], array $params = [])
+ {
+ if (empty($fields)) {
+ $fields = self::getFields();
+ }
+
+ return DBA::selectFirst("photo", $fields, $conditions, $params);
+ }
+
+ /**
+ * @brief Get photos for user id
+ *
+ * @param integer $uid User id
+ * @param string $resourceid Rescource ID of the photo
+ * @param array $conditions Array of fields for conditions
+ * @param array $params Array of several parameters
+ *
+ * @return bool|array
+ *
+ * @throws \Exception
+ * @see \Friendica\Database\DBA::select
+ */
+ public static function getPhotosForUser($uid, $resourceid, array $conditions = [], array $params = [])
+ {
+ $conditions["resource-id"] = $resourceid;
+ $conditions["uid"] = $uid;
+
+ return self::select([], $conditions, $params);
+ }
+
+ /**
+ * @brief Get a photo for user id
+ *
+ * @param integer $uid User id
+ * @param string $resourceid Rescource ID of the photo
+ * @param integer $scale Scale of the photo. Defaults to 0
+ * @param array $conditions Array of fields for conditions
+ * @param array $params Array of several parameters
+ *
+ * @return bool|array
+ *
+ * @throws \Exception
+ * @see \Friendica\Database\DBA::select
+ */
+ public static function getPhotoForUser($uid, $resourceid, $scale = 0, array $conditions = [], array $params = [])
+ {
+ $conditions["resource-id"] = $resourceid;
+ $conditions["uid"] = $uid;
+ $conditions["scale"] = $scale;
+
+ return self::selectFirst([], $conditions, $params);
+ }
+
+ /**
+ * @brief Get a single photo given resource id and scale
+ *
+ * This method checks for permissions. Returns associative array
+ * on success, "no sign" image info, if user has no permission,
+ * false if photo does not exists
+ *
+ * @param string $resourceid Rescource ID of the photo
+ * @param integer $scale Scale of the photo. Defaults to 0
+ *
+ * @return boolean|array
+ * @throws \Exception
+ */
+ public static function getPhoto($resourceid, $scale = 0)
+ {
+ $r = self::selectFirst(["uid"], ["resource-id" => $resourceid]);
+ if ($r === false) {
+ return false;
+ }
+
+ $sql_acl = Security::getPermissionsSQLByUserId($r["uid"]);
+
+ $conditions = [
+ "`resource-id` = ? AND `scale` <= ? " . $sql_acl,
+ $resourceid, $scale
+ ];
+
+ $params = ["order" => ["scale" => true]];
+
+ $photo = self::selectFirst([], $conditions, $params);
+
+ return $photo;
+ }
+
+ /**
+ * @brief Check if photo with given conditions exists
+ *
+ * @param array $conditions Array of extra conditions
+ *
+ * @return boolean
+ * @throws \Exception
+ */
+ public static function exists(array $conditions)
+ {
+ return DBA::exists("photo", $conditions);
+ }
+
+
+ /**
+ * @brief Get Image object for given row id. null if row id does not exist
+ *
+ * @param array $photo Photo data. Needs at least 'id', 'type', 'backend-class', 'backend-ref'
+ *
+ * @return \Friendica\Object\Image
+ * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+ * @throws \ImagickException
+ */
+ public static function getImageForPhoto(array $photo)
+ {
+ $data = "";
+ if ($photo["backend-class"] == "") {
+ // legacy data storage in "data" column
+ $i = self::selectFirst(["data"], ["id" => $photo["id"]]);
+ if ($i === false) {
+ return null;
+ }
+ $data = $i["data"];
+ } else {
+ $backendClass = $photo["backend-class"];
+ $backendRef = $photo["backend-ref"];
+ $data = $backendClass::get($backendRef);
+ }
+
+ if ($data === "") {
+ return null;
+ }
+ return new Image($data, $photo["type"]);
+ }
+
+ /**
+ * @brief Return a list of fields that are associated with the photo table
+ *
+ * @return array field list
+ * @throws \Exception
+ */
+ private static function getFields()
+ {
+ $allfields = DBStructure::definition(false);
+ $fields = array_keys($allfields["photo"]["fields"]);
+ array_splice($fields, array_search("data", $fields), 1);
+ return $fields;
+ }
+
+ /**
+ * @brief Construct a photo array for a system resource image
+ *
+ * @param string $filename Image file name relative to code root
+ * @param string $mimetype Image mime type. Defaults to "image/jpeg"
+ *
+ * @return array
+ * @throws \Exception