X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FDFRN.php;h=094c37717a25a100b96635ac6669dde40f79bb82;hb=771163e86056b92713577e55d0b5a3de7299cca4;hp=47aeaf05e57e14dd47595fd45a972447f79606a7;hpb=4032ad341832dc6ca86f1aa327e13aa087430e14;p=friendica.git diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 47aeaf05e5..094c37717a 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -9,28 +9,33 @@ namespace Friendica\Protocol; use Friendica\App; +use Friendica\Content\OEmbed; use Friendica\Core\Config; use Friendica\Core\System; use Friendica\Core\Worker; use Friendica\Database\DBM; -use Friendica\Model\GlobalContact; -use Friendica\Object\Contact; -use Friendica\Object\Profile; +use Friendica\Model\Contact; +use Friendica\Model\GContact; +use Friendica\Model\Group; +use Friendica\Model\Profile; +use Friendica\Model\Term; +use Friendica\Model\User; +use Friendica\Object\Image; use Friendica\Protocol\OStatus; use Friendica\Util\XML; use dba; use DOMDocument; -use DomXPath; +use DOMXPath; +require_once 'boot.php'; +require_once 'include/dba.php'; require_once "include/enotify.php"; require_once "include/threads.php"; require_once "include/items.php"; require_once "include/tags.php"; -require_once "include/files.php"; require_once "include/event.php"; require_once "include/text.php"; -require_once "include/oembed.php"; require_once "include/html2bbcode.php"; require_once "include/bbcode.php"; @@ -164,7 +169,7 @@ class DFRN $contact = $r[0]; include_once 'include/security.php'; - $groups = init_groups_visitor($contact['id']); + $groups = Group::getIdsByContactId($contact['id']); if (count($groups)) { for ($x = 0; $x < count($groups); $x ++) @@ -339,19 +344,15 @@ class DFRN $items = $r; $item = $r[0]; - $r = q( - "SELECT `contact`.*, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`, `user`.`account-type` - FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid` - WHERE `contact`.`self` AND `user`.`uid` = %d LIMIT 1", - intval($item['uid']) - ); - - if (!DBM::is_result($r)) { - killme(); + if ($item['uid'] != 0) { + $owner = User::getOwnerDataById($item['uid']); + if (!$owner) { + killme(); + } + } else { + $owner = ['uid' => 0, 'nick' => 'feed-item']; } - $owner = $r[0]; - $doc = new DOMDocument('1.0', 'utf-8'); $doc->formatOutput = true; $type = 'html'; @@ -464,7 +465,7 @@ class DFRN /* get site pubkey. this could be a new installation with no site keys*/ $pubkey = Config::get('system', 'site_pubkey'); if (! $pubkey) { - $res = new_keypair(1024); + $res = Crypto::newKeypair(1024); Config::set('system', 'site_prvkey', $res['prvkey']); Config::set('system', 'site_pubkey', $res['pubkey']); } @@ -475,7 +476,7 @@ class DFRN $uid ); $photos = array(); - $ext = Photo::supportedTypes(); + $ext = Image::supportedTypes(); foreach ($rp as $p) { $photos[$p['scale']] = System::baseUrl().'/photo/'.$p['resource-id'].'-'.$p['scale'].'.'.$ext[$p['type']]; @@ -1297,7 +1298,7 @@ class DFRN case 2: // RINO 2 based on php-encryption try { - $key = \Crypto::createNewRandomKey(); + $key = \Crypto::CreateNewRandomKey(); } catch (\CryptoTestFailedException $ex) { logger('Cannot safely create a key'); return -4; @@ -1306,7 +1307,7 @@ class DFRN return -5; } try { - $data = \Crypto::encrypt($postvars['data'], $key); + $data = \Crypto::Encrypt($postvars['data'], $key); } catch (\CryptoTestFailedException $ex) { logger('Cannot safely perform encryption'); return -6; @@ -1441,7 +1442,7 @@ class DFRN * @param bool $onlyfetch Should the data only be fetched or should it update the contact record as well * @param string $xml optional, default empty * - * @return Returns an array with relevant data of the author + * @return array Relevant data of the author * @todo Find good type-hints for all parameter */ private static function fetchauthor($xpath, $context, $importer, $element, $onlyfetch, $xml = "") @@ -1659,7 +1660,7 @@ class DFRN ); } - update_contact_avatar( + Contact::updateAvatar( $author["avatar"], $importer["uid"], $contact["id"], @@ -1677,9 +1678,9 @@ class DFRN $poco["photo"] = $author["avatar"]; $poco["hide"] = $hide; $poco["contact-type"] = $contact["contact-type"]; - $gcid = GlobalContact::update($poco); + $gcid = GContact::update($poco); - GlobalContact::link($gcid, $importer["uid"], $contact["id"]); + GContact::link($gcid, $importer["uid"], $contact["id"]); } return($author); @@ -1690,7 +1691,7 @@ class DFRN * * @param object $xpath XPath object * @param object $activity Activity object - * @param text $element element name + * @param string $element element name * * @return string XML string * @todo Find good type-hints for all parameter @@ -2034,7 +2035,7 @@ class DFRN dbesc(normalise_link($old["url"])) ); - update_contact_avatar($relocate["avatar"], $importer["importer_uid"], $importer["id"], true); + Contact::updateAvatar($relocate["avatar"], $importer["importer_uid"], $importer["id"], true); if ($x === false) { return false; @@ -2498,19 +2499,20 @@ class DFRN /// @todo Do we really need this check for HTML elements? (It was copied from the old function) if ((strpos($item['body'], '<') !== false) && (strpos($item['body'], '>') !== false)) { + $base_url = get_app()->get_baseurl(); $item['body'] = reltoabs($item['body'], $base_url); $item['body'] = html2bb_video($item['body']); - $item['body'] = oembed_html2bbcode($item['body']); + $item['body'] = OEmbed::HTML2BBCode($item['body']); - $config = HTMLPurifier_Config::createDefault(); + $config = \HTMLPurifier_Config::createDefault(); $config->set('Cache.DefinitionImpl', null); // we shouldn't need a whitelist, because the bbcode converter // will strip out any unsupported tags. - $purifier = new HTMLPurifier($config); + $purifier = new \HTMLPurifier($config); $item['body'] = $purifier->purify($item['body']); $item['body'] = @html2bbcode($item['body']); @@ -2911,7 +2913,7 @@ class DFRN intval($importer["uid"]) ); create_tags_from_itemuri($uri, $importer["uid"]); - create_files_from_itemuri($uri, $importer["uid"]); + Term::createFromItemURI($uri, $importer["uid"]); update_thread_uri($uri, $importer["uid"]); } else { $r = q( @@ -2924,7 +2926,7 @@ class DFRN intval($importer["uid"]) ); create_tags_from_itemuri($uri, $importer["uid"]); - create_files_from_itemuri($uri, $importer["uid"]); + Term::createFromItemURI($uri, $importer["uid"]); update_thread_uri($uri, $importer["importer_uid"]); if ($item["last-child"]) { // ensure that last-child is set in case the comment that had it just got wiped. @@ -2961,9 +2963,9 @@ class DFRN /** * @brief Imports a DFRN message * - * @param text $xml The DFRN message - * @param array $importer Record of the importer user mixed with contact of the content - * @param bool $sort_by_date Is used when feeds are polled + * @param string $xml The DFRN message + * @param array $importer Record of the importer user mixed with contact of the content + * @param bool $sort_by_date Is used when feeds are polled * @return integer Import status * @todo set proper type-hints */ @@ -2976,7 +2978,7 @@ class DFRN $doc = new DOMDocument(); @$doc->loadXML($xml); - $xpath = new DomXPath($doc); + $xpath = new DOMXPath($doc); $xpath->registerNamespace("atom", NAMESPACE_ATOM1); $xpath->registerNamespace("thr", NAMESPACE_THREAD); $xpath->registerNamespace("at", NAMESPACE_TOMB); @@ -3012,7 +3014,7 @@ class DFRN // The account type is new since 3.5.1 if ($xpath->query("/atom:feed/dfrn:account_type")->length > 0) { - $accounttype = intval($xpath->evaluate("/atom:feed/dfrn:account_type/text()", $context)->item(0)->nodeValue); + $accounttype = intval($xpath->evaluate("/atom:feed/dfrn:account_type/text()")->item(0)->nodeValue); if ($accounttype != $importer["contact-type"]) { dba::update('contact', array('contact-type' => $accounttype), array('id' => $importer["id"])); @@ -3021,7 +3023,7 @@ class DFRN // is it a public forum? Private forums aren't supported with this method // This is deprecated since 3.5.1 - $forum = intval($xpath->evaluate("/atom:feed/dfrn:community/text()", $context)->item(0)->nodeValue); + $forum = intval($xpath->evaluate("/atom:feed/dfrn:community/text()")->item(0)->nodeValue); if ($forum != $importer["forum"]) { $condition = array('`forum` != ? AND `id` = ?', $forum, $importer["id"]); @@ -3079,4 +3081,94 @@ class DFRN logger("Import done for user " . $importer["uid"] . " from contact " . $importer["id"], LOGGER_DEBUG); return 200; } + + /** + * @param App $a App + * @param string $contact_nick contact nickname + */ + public static function autoRedir(App $a, $contact_nick) + { + // prevent looping + if (x($_REQUEST, 'redir') && intval($_REQUEST['redir'])) { + return; + } + + if ((! $contact_nick) || ($contact_nick === $a->user['nickname'])) { + return; + } + + if (local_user()) { + // We need to find out if $contact_nick is a user on this hub, and if so, if I + // am a contact of that user. However, that user may have other contacts with the + // same nickname as me on other hubs or other networks. Exclude these by requiring + // that the contact have a local URL. I will be the only person with my nickname at + // this URL, so if a result is found, then I am a contact of the $contact_nick user. + // + // We also have to make sure that I'm a legitimate contact--I'm not blocked or pending. + + $baseurl = System::baseUrl(); + $domain_st = strpos($baseurl, "://"); + if ($domain_st === false) { + return; + } + $baseurl = substr($baseurl, $domain_st + 3); + $nurl = normalise_link($baseurl); + + /// @todo Why is there a query for "url" *and* "nurl"? Especially this normalising is strange. + $r = q("SELECT `id` FROM `contact` WHERE `uid` = (SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1) + AND `nick` = '%s' AND NOT `self` AND (`url` LIKE '%%%s%%' OR `nurl` LIKE '%%%s%%') AND NOT `blocked` AND NOT `pending` LIMIT 1", + dbesc($contact_nick), + dbesc($a->user['nickname']), + dbesc($baseurl), + dbesc($nurl) + ); + if ((! DBM::is_result($r)) || $r[0]['id'] == remote_user()) { + return; + } + + $r = q("SELECT * FROM contact WHERE nick = '%s' + AND network = '%s' AND uid = %d AND url LIKE '%%%s%%' LIMIT 1", + dbesc($contact_nick), + dbesc(NETWORK_DFRN), + intval(local_user()), + dbesc($baseurl) + ); + if (! DBM::is_result($r)) { + return; + } + + $cid = $r[0]['id']; + + $dfrn_id = (($r[0]['issued-id']) ? $r[0]['issued-id'] : $r[0]['dfrn-id']); + + if ($r[0]['duplex'] && $r[0]['issued-id']) { + $orig_id = $r[0]['issued-id']; + $dfrn_id = '1:' . $orig_id; + } + if ($r[0]['duplex'] && $r[0]['dfrn-id']) { + $orig_id = $r[0]['dfrn-id']; + $dfrn_id = '0:' . $orig_id; + } + + // ensure that we've got a valid ID. There may be some edge cases with forums and non-duplex mode + // that may have triggered some of the "went to {profile/intro} and got an RSS feed" issues + + if (strlen($dfrn_id) < 3) { + return; + } + + $sec = random_string(); + + dba::insert('profile_check', ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id, 'sec' => $sec, 'expire' => time() + 45]); + + $url = curPageURL(); + + logger('auto_redir: ' . $r[0]['name'] . ' ' . $sec, LOGGER_DEBUG); + $dest = (($url) ? '&destination_url=' . $url : ''); + goaway($r[0]['poll'] . '?dfrn_id=' . $dfrn_id + . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest); + } + + return; + } }