+
+ /**
+ * @brief Returns a magic link to authenticate remote visitors
+ *
+ * @param string $contact_url The address of the target contact profile
+ * @param integer $url An url that we will be redirected to after the authentication
+ *
+ * @return string with "redir" link
+ */
+ public static function magicLink($contact_url, $url = '')
+ {
+ $cid = self::getIdForURL($contact_url, 0, true);
+ if (empty($cid)) {
+ return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
+ }
+
+ return self::magicLinkbyId($cid, $url);
+ }
+
+ /**
+ * @brief Returns a magic link to authenticate remote visitors
+ *
+ * @param integer $cid The contact id of the target contact profile
+ * @param integer $url An url that we will be redirected to after the authentication
+ *
+ * @return string with "redir" link
+ */
+ public static function magicLinkbyId($cid, $url = '')
+ {
+ $contact = DBA::selectFirst('contact', ['id', 'network', 'url', 'uid'], ['id' => $cid]);
+
+ return self::magicLinkbyContact($contact, $url);
+ }
+
+ /**
+ * @brief Returns a magic link to authenticate remote visitors
+ *
+ * @param array $contact The contact array with "uid", "network" and "url"
+ * @param integer $url An url that we will be redirected to after the authentication
+ *
+ * @return string with "redir" link
+ */
+ public static function magicLinkbyContact($contact, $url = '')
+ {
+ if ($contact['network'] != NETWORK_DFRN) {
+ return $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];
+ }
+
+ // Only redirections to the same host do make sense
+ if (($url != '') && (parse_url($url, PHP_URL_HOST) != parse_url($contact['url'], PHP_URL_HOST))) {
+ return $url;
+ }
+
+ if ($contact['uid'] != 0) {
+ return self::magicLink($contact['url'], $url);
+ }
+
+ $redirect = 'redir/' . $contact['id'];
+
+ if ($url != '') {
+ $redirect .= '?url=' . $url;
+ }
+
+ return $redirect;
+ }