From 660bd39efad52b1c69bd8dd0ca27141b81f84ca5 Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Wed, 9 Jul 2014 20:48:34 +0200
Subject: [PATCH] Let an item be accssible via the GUI (additionally to uid and
 item id)

---
 include/items.php | 13 +++++++++++
 mod/display.php   | 55 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/include/items.php b/include/items.php
index 6bec078bc3..d71c0e2031 100644
--- a/include/items.php
+++ b/include/items.php
@@ -1011,6 +1011,19 @@ function item_store($arr,$force_parent = false) {
 		}
 	}
 
+	// If there is no guid then take the same guid that was taken before for the same uri
+	if ((trim($arr['guid']) == "") AND (trim($arr['uri']) != "")) {
+		logger('item_store: checking for an existing guid for uri '.$arr['uri'], LOGGER_DEBUG);
+		$r = q("SELECT `guid` FROM `item` WHERE `uri` = '%s' AND `guid` != '' LIMIT 1",
+			dbesc(trim($arr['uri']))
+		);
+
+		if(count($r)) {
+			$arr['guid'] = $r[0]["guid"];
+			logger('item_store: found guid '.$arr['guid'].' for uri '.$arr['uri'], LOGGER_DEBUG);
+		}
+	}
+
 	// Shouldn't happen but we want to make absolutely sure it doesn't leak from a plugin.
 	// Deactivated, since the bbcode parser can handle with it - and it destroys posts with some smileys that contain "<"
 	//if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
diff --git a/mod/display.php b/mod/display.php
index 5c6277e34c..e0a7912a28 100644
--- a/mod/display.php
+++ b/mod/display.php
@@ -1,6 +1,5 @@
 <?php
 
-
 function display_init(&$a) {
 
 	if((get_config('system','block_public')) && (! local_user()) && (! remote_user())) {
@@ -8,6 +7,33 @@ function display_init(&$a) {
 	}
 
 	$nick = (($a->argc > 1) ? $a->argv[1] : '');
+
+	// If there is only one parameter, then check if this parameter could be a guid
+	if ($a->argc == 2) {
+		$nick = "";
+
+		// Does the local user have this item?
+		if (local_user()) {
+			$r = q("SELECT `id` FROM `item`
+				WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
+					AND `guid` = '%s' AND `uid` = %d", $a->argv[1], local_user());
+			if (count($r))
+				$nick = $a->user["nickname"];
+		}
+
+		// Or is it anywhere on the server?
+		if ($nick == "") {
+			$r = q("SELECT `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
+				WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
+					AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
+					AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
+					AND `item`.`private` = 0 AND `item`.`wall` = 1
+					AND `item`.`guid` = '%s'", $a->argv[1]);
+			if (count($r))
+				$nick = $r[0]["nickname"];
+		}
+	}
+
 	profile_load($a,$nick);
 
 }
@@ -44,6 +70,33 @@ function display_content(&$a, $update = 0) {
 	}
 	else {
 		$item_id = (($a->argc > 2) ? $a->argv[2] : 0);
+
+		if ($a->argc == 2) {
+			$nick = "";
+
+			if (local_user()) {
+				$r = q("SELECT `id` FROM `item`
+					WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
+						AND `guid` = '%s' AND `uid` = %d", $a->argv[1], local_user());
+				if (count($r)) {
+					$item_id = $r[0]["id"];
+					$nick = $a->user["nickname"];
+				}
+			}
+
+			if ($nick == "") {
+				$r = q("SELECT `user`.`nickname`, `item`.`id` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
+					WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0
+						AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = ''
+						AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = ''
+						AND `item`.`private` = 0 AND `item`.`wall` = 1
+						AND `item`.`guid` = '%s'", $a->argv[1]);
+				if (count($r)) {
+					$item_id = $r[0]["id"];
+					$nick = $r[0]["nickname"];
+				}
+			}
+		}
 	}
 
 	if(! $item_id) {