+ /**
+ * Set the visitor cookies (see remote_user()) for the given handle
+ *
+ * @param string $handle Visitor handle
+ * @return array Visitor contact array
+ */
+ public static function addVisitorCookieForHandle($handle)
+ {
+ $a = \get_app();
+
+ // Try to find the public contact entry of the visitor.
+ $cid = Contact::getIdForURL($handle);
+ if (!$cid) {
+ Logger::log('unable to finger ' . $handle, Logger::DEBUG);
+ return [];
+ }
+
+ $visitor = DBA::selectFirst('contact', [], ['id' => $cid]);
+
+ // Authenticate the visitor.
+ $_SESSION['authenticated'] = 1;
+ $_SESSION['visitor_id'] = $visitor['id'];
+ $_SESSION['visitor_handle'] = $visitor['addr'];
+ $_SESSION['visitor_home'] = $visitor['url'];
+ $_SESSION['my_url'] = $visitor['url'];
+
+ /// @todo replace this and the query for this variable with some cleaner functionality
+ $_SESSION['remote'] = [];
+
+ $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => $visitor['nurl'], 'rel' => [Contact::FOLLOWER, Contact::FRIEND]]);
+ while ($contact = DBA::fetch($remote_contacts)) {
+ if (($contact['uid'] == 0) || Contact::isBlockedByUser($visitor['id'], $contact['uid'])) {
+ continue;
+ }
+
+ $_SESSION['remote'][] = ['cid' => $contact['id'], 'uid' => $contact['uid'], 'url' => $visitor['url']];
+ }
+
+ $a->contact = $visitor;
+
+ Logger::info('Authenticated visitor', ['url' => $visitor['url']]);
+
+ return $visitor;
+ }
+