+ /**
+ * @brief Processes the suggestion elements
+ *
+ * @param object $xpath XPath object
+ * @param object $suggestion suggestion elements
+ * @param array $importer Record of the importer user mixed with contact of the content
+ */
+ private function process_suggestion($xpath, $suggestion, $importer) {
+
+ logger("Processing suggestions");
+
+ $suggest = array();
+ $suggest["uid"] = $importer["importer_uid"];
+ $suggest["cid"] = $importer["id"];
+ $suggest["url"] = $xpath->query("dfrn:url/text()", $suggestion)->item(0)->nodeValue;
+ $suggest["name"] = $xpath->query("dfrn:name/text()", $suggestion)->item(0)->nodeValue;
+ $suggest["photo"] = $xpath->query("dfrn:photo/text()", $suggestion)->item(0)->nodeValue;
+ $suggest["request"] = $xpath->query("dfrn:request/text()", $suggestion)->item(0)->nodeValue;
+ $suggest["body"] = $xpath->query("dfrn:note/text()", $suggestion)->item(0)->nodeValue;
+
+ // Does our member already have a friend matching this description?
+
+ $r = q("SELECT `id` FROM `contact` WHERE `name` = '%s' AND `nurl` = '%s' AND `uid` = %d LIMIT 1",
+ dbesc($suggest["name"]),
+ dbesc(normalise_link($suggest["url"])),
+ intval($suggest["uid"])
+ );
+ if(count($r))
+ return false;
+
+ // Do we already have an fcontact record for this person?
+
+ $fid = 0;
+ $r = q("SELECT `id` FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
+ dbesc($suggest["url"]),
+ dbesc($suggest["name"]),
+ dbesc($suggest["request"])
+ );
+ if(count($r)) {
+ $fid = $r[0]["id"];
+
+ // OK, we do. Do we already have an introduction for this person ?
+ $r = q("SELECT `id` FROM `intro` WHERE `uid` = %d AND `fid` = %d LIMIT 1",
+ intval($suggest["uid"]),
+ intval($fid)
+ );
+ if(count($r))
+ return false;
+ }
+ if(!$fid)
+ $r = q("INSERT INTO `fcontact` (`name`,`url`,`photo`,`request`) VALUES ('%s', '%s', '%s', '%s')",
+ dbesc($suggest["name"]),
+ dbesc($suggest["url"]),
+ dbesc($suggest["photo"]),
+ dbesc($suggest["request"])
+ );
+ $r = q("SELECT `id` FROM `fcontact` WHERE `url` = '%s' AND `name` = '%s' AND `request` = '%s' LIMIT 1",
+ dbesc($suggest["url"]),
+ dbesc($suggest["name"]),
+ dbesc($suggest["request"])
+ );
+ if(count($r))
+ $fid = $r[0]["id"];
+ else
+ // database record did not get created. Quietly give up.
+ return false;
+
+
+ $hash = random_string();
+
+ $r = q("INSERT INTO `intro` (`uid`, `fid`, `contact-id`, `note`, `hash`, `datetime`, `blocked`)
+ VALUES(%d, %d, %d, '%s', '%s', '%s', %d)",
+ intval($suggest["uid"]),
+ intval($fid),
+ intval($suggest["cid"]),
+ dbesc($suggest["body"]),
+ dbesc($hash),
+ dbesc(datetime_convert()),
+ intval(0)
+ );
+
+ notification(array(
+ "type" => NOTIFY_SUGGEST,
+ "notify_flags" => $importer["notify-flags"],
+ "language" => $importer["language"],
+ "to_name" => $importer["username"],
+ "to_email" => $importer["email"],
+ "uid" => $importer["importer_uid"],
+ "item" => $suggest,
+ "link" => App::get_baseurl()."/notifications/intros",
+ "source_name" => $importer["name"],
+ "source_link" => $importer["url"],
+ "source_photo" => $importer["photo"],
+ "verb" => ACTIVITY_REQ_FRIEND,
+ "otype" => "intro"
+ ));
+
+ return true;
+
+ }
+
+ /**
+ * @brief Processes the relocation elements
+ *
+ * @param object $xpath XPath object
+ * @param object $relocation relocation elements
+ * @param array $importer Record of the importer user mixed with contact of the content
+ */
+ private function process_relocation($xpath, $relocation, $importer) {
+
+ logger("Processing relocations");
+
+ $relocate = array();
+ $relocate["uid"] = $importer["importer_uid"];
+ $relocate["cid"] = $importer["id"];
+ $relocate["url"] = $xpath->query("dfrn:url/text()", $relocation)->item(0)->nodeValue;
+ $relocate["name"] = $xpath->query("dfrn:name/text()", $relocation)->item(0)->nodeValue;
+ $relocate["photo"] = $xpath->query("dfrn:photo/text()", $relocation)->item(0)->nodeValue;
+ $relocate["thumb"] = $xpath->query("dfrn:thumb/text()", $relocation)->item(0)->nodeValue;
+ $relocate["micro"] = $xpath->query("dfrn:micro/text()", $relocation)->item(0)->nodeValue;
+ $relocate["request"] = $xpath->query("dfrn:request/text()", $relocation)->item(0)->nodeValue;
+ $relocate["confirm"] = $xpath->query("dfrn:confirm/text()", $relocation)->item(0)->nodeValue;
+ $relocate["notify"] = $xpath->query("dfrn:notify/text()", $relocation)->item(0)->nodeValue;
+ $relocate["poll"] = $xpath->query("dfrn:poll/text()", $relocation)->item(0)->nodeValue;
+ $relocate["sitepubkey"] = $xpath->query("dfrn:sitepubkey/text()", $relocation)->item(0)->nodeValue;
+
+ // update contact
+ $r = q("SELECT `photo`, `url` FROM `contact` WHERE `id` = %d AND `uid` = %d;",
+ intval($importer["id"]),
+ intval($importer["importer_uid"]));
+ if (!$r)
+ return false;
+
+ $old = $r[0];
+
+ $x = q("UPDATE `contact` SET
+ `name` = '%s',
+ `photo` = '%s',
+ `thumb` = '%s',
+ `micro` = '%s',
+ `url` = '%s',
+ `nurl` = '%s',
+ `request` = '%s',
+ `confirm` = '%s',
+ `notify` = '%s',
+ `poll` = '%s',
+ `site-pubkey` = '%s'
+ WHERE `id` = %d AND `uid` = %d;",
+ dbesc($relocate["name"]),
+ dbesc($relocate["photo"]),
+ dbesc($relocate["thumb"]),
+ dbesc($relocate["micro"]),
+ dbesc($relocate["url"]),
+ dbesc(normalise_link($relocate["url"])),
+ dbesc($relocate["request"]),
+ dbesc($relocate["confirm"]),
+ dbesc($relocate["notify"]),
+ dbesc($relocate["poll"]),
+ dbesc($relocate["sitepubkey"]),
+ intval($importer["id"]),
+ intval($importer["importer_uid"]));
+
+ if ($x === false)
+ return false;
+
+ // update items
+ $fields = array(
+ 'owner-link' => array($old["url"], $relocate["url"]),
+ 'author-link' => array($old["url"], $relocate["url"]),
+ 'owner-avatar' => array($old["photo"], $relocate["photo"]),
+ 'author-avatar' => array($old["photo"], $relocate["photo"]),
+ );
+ foreach ($fields as $n=>$f){
+ $x = q("UPDATE `item` SET `%s` = '%s' WHERE `%s` = '%s' AND `uid` = %d",
+ $n, dbesc($f[1]),
+ $n, dbesc($f[0]),
+ intval($importer["importer_uid"]));
+ if ($x === false)
+ return false;
+ }