+ foreach ($tags as $tag=>$link) {
+
+ if (substr(trim($tag), 0, 1) == "#") {
+ // try to ignore #039 or #1 or anything like that
+ if(ctype_digit(substr(trim($tag),1)))
+ continue;
+ // try to ignore html hex escapes, e.g. #x2317
+ if((substr(trim($tag),1,1) == 'x' || substr(trim($tag),1,1) == 'X') && ctype_digit(substr(trim($tag),2)))
+ continue;
+ $type = TERM_HASHTAG;
+ $term = substr($tag, 1);
+ } elseif (substr(trim($tag), 0, 1) == "@") {
+ $type = TERM_MENTION;
+ $term = substr($tag, 1);
+ } else { // This shouldn't happen
+ $type = TERM_HASHTAG;
+ $term = $tag;
+ }
+
+ if ($message["uid"] == 0) {
+ $global = true;
+
+ q("UPDATE `term` SET `global` = 1 WHERE `otype` = %d AND `guid` = '%s'",
+ intval(TERM_OBJ_POST), dbesc($message["guid"]));
+ } else {
+ $isglobal = q("SELECT `global` FROM `term` WHERE `uid` = 0 AND `otype` = %d AND `guid` = '%s'",
+ intval(TERM_OBJ_POST), dbesc($message["guid"]));
+
+ $global = (count($isglobal) > 0);
+ }
+
+ $r = q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`, `url`, `guid`, `created`, `received`, `global`)
+ VALUES (%d, %d, %d, %d, '%s', '%s', '%s', '%s', '%s', %d)",
+ intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval($type), dbesc($term),
+ dbesc($link), dbesc($message["guid"]), dbesc($message["created"]), dbesc($message["received"]), intval($global));
+
+ // Search for mentions
+ if ((substr($tag, 0, 1) == '@') AND (strpos($link, $profile_base_friendica) OR strpos($link, $profile_base_diaspora))) {
+ $users = q("SELECT `uid` FROM `contact` WHERE self AND (`url` = '%s' OR `nurl` = '%s')", $link, $link);
+ foreach ($users AS $user) {
+ if ($user["uid"] == $message["uid"]) {
+ q("UPDATE `item` SET `mention` = 1 WHERE `id` = %d", intval($itemid));
+
+ q("UPDATE `thread` SET `mention` = 1 WHERE `iid` = %d", intval($message["parent"]));
+ }
+ }
+ }
+ }