+ * @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
+ *
+ * @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
+ *
+ * @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
+ *
+ * @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
+ *
+ * @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
+ */
+ 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);
+ if ($photo === false) {
+ return self::createPhotoForSystemResource("images/nosign.jpg");
+ }
+ return $photo;
+ }
+
+ /**
+ * @brief Check if photo with given resource id exists
+ *
+ * @param string $resourceid Resource ID of the photo
+ *
+ * @return boolean
+ */
+ public static function exists($resourceid)
+ {
+ return DBA::count("photo", ["resource-id" => $resourceid]) > 0;
+ }
+
+ /**
+ * @brief Get Image object for given row id. null if row id does not exist
+ *
+ * @param integer $id Row id
+ *
+ * @return \Friendica\Object\Image
+ */
+ public static function getImageForPhoto($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
+ */
+ 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
+ */
+ public static function createPhotoForSystemResource($filename, $mimetype = "image/jpeg")
+ {
+ $fields = self::getFields();
+ $values = array_fill(0, count($fields), "");
+ $photo = array_combine($fields, $values);
+ $photo["backend-class"] = \Friendica\Model\Storage\SystemResource::class;
+ $photo["backend-ref"] = $filename;
+ $photo["type"] = $mimetype;
+ $photo["cacheable"] = false;
+ return $photo;
+ }
+
+
+ /**
+ * @brief store photo metadata in db and binary in default backend
+ *
+ * @param Image $Image Image object with data
+ * @param integer $uid User ID
+ * @param integer $cid Contact ID
+ * @param integer $rid Resource ID
+ * @param string $filename Filename
+ * @param string $album Album name
+ * @param integer $scale Scale
+ * @param integer $profile Is a profile image? optional, default = 0
+ * @param string $allow_cid Permissions, allowed contacts. optional, default = ""
+ * @param string $allow_gid Permissions, allowed groups. optional, default = ""
+ * @param string $deny_cid Permissions, denied contacts.optional, default = ""
+ * @param string $deny_gid Permissions, denied greoup.optional, default = ""
+ * @param string $desc Photo caption. optional, default = ""
+ *
+ * @return boolean True on success