+ return System::createGUID(32, false);
+ }
+
+ /**
+ * Changes photo permissions that had been embedded in a post
+ *
+ * @todo This function currently does have some flaws:
+ * - Sharing a post with a forum will create a photo that only the forum can see.
+ * - Sharing a photo again that been shared non public before doesn't alter the permissions.
+ *
+ * @return string
+ * @throws \Exception
+ */
+ public static function setPermissionFromBody($body, $uid, $original_contact_id, $str_contact_allow, $str_group_allow, $str_contact_deny, $str_group_deny)
+ {
+ // Simplify image codes
+ $img_body = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism", '[img]$3[/img]', $body);
+ $img_body = preg_replace("/\[img\=(.*?)\](.*?)\[\/img\]/ism", '[img]$1[/img]', $img_body);
+
+ // Search for images
+ if (!preg_match_all("/\[img\](.*?)\[\/img\]/", $img_body, $match)) {
+ return false;
+ }
+ $images = $match[1];
+ if (empty($images)) {
+ return false;
+ }
+
+ foreach ($images as $image) {
+ if (!stristr($image, System::baseUrl() . '/photo/')) {
+ continue;
+ }
+ $image_uri = substr($image,strrpos($image,'/') + 1);
+ $image_uri = substr($image_uri,0, strpos($image_uri,'-'));
+ if (!strlen($image_uri)) {
+ continue;
+ }
+
+ // Ensure to only modify photos that you own
+ $srch = '<' . intval($original_contact_id) . '>';
+
+ $condition = [
+ 'allow_cid' => $srch, 'allow_gid' => '', 'deny_cid' => '', 'deny_gid' => '',
+ 'resource-id' => $image_uri, 'uid' => $uid
+ ];
+ if (!Photo::exists($condition)) {
+ continue;
+ }
+
+ /// @todo Check if $str_contact_allow does contain a public forum. Then set the permissions to public.
+
+ $fields = ['allow_cid' => $str_contact_allow, 'allow_gid' => $str_group_allow,
+ 'deny_cid' => $str_contact_deny, 'deny_gid' => $str_group_deny];
+ $condition = ['resource-id' => $image_uri, 'uid' => $uid];
+ Logger::info('Set permissions', ['condition' => $condition, 'permissions' => $fields]);
+ Photo::update($fields, $condition);
+ }
+
+ return true;
+ }
+
+ /**
+ * Strips known picture extensions from picture links
+ *
+ * @param string $name Picture link
+ * @return string stripped picture link
+ * @throws \Exception
+ */
+ public static function stripExtension($name)
+ {
+ $name = str_replace([".jpg", ".png", ".gif"], ["", "", ""], $name);
+ foreach (Image::supportedTypes() as $m => $e) {
+ $name = str_replace("." . $e, "", $name);
+ }
+ return $name;
+ }
+
+ /**
+ * Returns the GUID from picture links
+ *
+ * @param string $name Picture link
+ * @return string GUID
+ * @throws \Exception
+ */
+ public static function getGUID($name)
+ {
+ $a = \get_app();
+ $base = $a->getBaseURL();
+
+ $guid = str_replace([Strings::normaliseLink($base), '/photo/'], '', Strings::normaliseLink($name));
+
+ $guid = self::stripExtension($guid);
+ if (substr($guid, -2, 1) != "-") {
+ return '';
+ }
+
+ $scale = intval(substr($guid, -1, 1));
+ if (empty($scale)) {
+ return '';
+ }
+
+ $guid = substr($guid, 0, -2);
+ return $guid;
+ }
+
+ /**
+ * Tests if the picture link points to a locally stored picture
+ *
+ * @param string $name Picture link
+ * @return boolean
+ * @throws \Exception
+ */
+ public static function isLocal($name)
+ {
+ $guid = self::getGUID($name);
+
+ if (empty($guid)) {
+ return false;
+ }
+
+ return DBA::exists('photo', ['resource-id' => $guid]);