]> git.mxchange.org Git - friendica.git/blobdiff - mod/dfrn_confirm.php
Merge remote-tracking branch 'upstream/develop' into develop
[friendica.git] / mod / dfrn_confirm.php
index 45243900c2dff96fcbe1a62f8054326f14a8865b..0e99b26c32cd9d81a7650f1d8a4c14445eb9a85c 100644 (file)
  *    https://github.com/friendica/friendica/blob/master/spec/dfrn2_contact_confirmation.png
  */
 
-require_once('include/enotify.php');
-require_once('include/group.php');
-require_once('include/Probe.php');
+use Friendica\App;
+use Friendica\Core\System;
+use Friendica\Network\Probe;
 
-function dfrn_confirm_post(&$a,$handsfree = null) {
+require_once 'include/enotify.php';
+require_once 'include/group.php';
+
+function dfrn_confirm_post(App $a, $handsfree = null) {
 
        if(is_array($handsfree)) {
 
@@ -121,7 +124,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                        intval($uid)
                );
 
-               if(! count($r)) {
+               if (! dbm::is_result($r)) {
                        logger('Contact not found in DB.');
                        notice( t('Contact not found.') . EOL );
                        notice( t('This may occasionally happen if contact was requested by both persons and it has already been approved.') . EOL );
@@ -152,7 +155,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                         * worried about key leakage than anybody cracking it.
                         *
                         */
-                       require_once('include/crypto.php');
+                       require_once 'include/crypto.php';
 
                        $res = new_keypair(4096);
 
@@ -185,16 +188,16 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                         *
                         */
 
-                       $src_aes_key = random_string();
+                       $src_aes_key = openssl_random_pseudo_bytes(64);
 
                        $result = '';
-                       openssl_private_encrypt($dfrn_id,$result,$user[0]['prvkey']);
+                       openssl_private_encrypt($dfrn_id, $result, $user[0]['prvkey']);
 
                        $params['dfrn_id'] = bin2hex($result);
                        $params['public_key'] = $public_key;
 
 
-                       $my_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
+                       $my_url = System::baseUrl() . '/profile/' . $user[0]['nickname'];
 
                        openssl_public_encrypt($my_url, $params['source_url'], $site_pubkey);
                        $params['source_url'] = bin2hex($params['source_url']);
@@ -224,9 +227,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                         *
                         */
 
-                       $a->config['system']['curl_timeout'] = 120;
-
-                       $res = post_url($dfrn_confirm,$params);
+                       $res = post_url($dfrn_confirm, $params, null, $redirects, 120);
 
                        logger(' Confirm: received data: ' . $res, LOGGER_DATA);
 
@@ -319,7 +320,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                 *
                 */
 
-               require_once('include/Photo.php');
+               require_once 'include/Photo.php';
 
                update_contact_avatar($contact['photo'],$uid,$contact_id);
 
@@ -351,8 +352,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                                dbesc(NETWORK_DFRN),
                                intval($contact_id)
                        );
-               }
-               else {
+               } else {
 
                        // $network !== NETWORK_DFRN
 
@@ -360,18 +360,16 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                        $notify = (($contact['notify']) ? $contact['notify'] : '');
                        $poll   = (($contact['poll']) ? $contact['poll'] : '');
 
-                       if((! $contact['notify']) || (! $contact['poll'])) {
-                               $arr = Probe::lrdd($contact['url']);
-                               if(count($arr)) {
-                                       foreach($arr as $link) {
-                                               if($link['@attributes']['rel'] === 'salmon')
-                                                       $notify = $link['@attributes']['href'];
-                                               if($link['@attributes']['rel'] === NAMESPACE_FEED)
-                                                       $poll = $link['@attributes']['href'];
-                                       }
-                               }
+                       $arr = Probe::uri($contact['url']);
+                       if (empty($contact['notify'])) {
+                               $notify = $arr['notify'];
+                       }
+                       if (empty($contact['poll'])) {
+                               $poll = $arr['poll'];
                        }
 
+                       $addr = $arr['addr'];
+
                        $new_relation = $contact['rel'];
                        $writable = $contact['writable'];
 
@@ -393,6 +391,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
 
                        $r = q("UPDATE `contact` SET `name-date` = '%s',
                                `uri-date` = '%s',
+                               `addr` = '%s',
                                `notify` = '%s',
                                `poll` = '%s',
                                `blocked` = 0,
@@ -405,6 +404,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                        ",
                                dbesc(datetime_convert()),
                                dbesc(datetime_convert()),
+                               dbesc($addr),
                                dbesc($notify),
                                dbesc($poll),
                                dbesc($network),
@@ -415,24 +415,27 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                        );
                }
 
-               if($r === false)
-                               notice( t('Unable to set contact photo.') . EOL);
+               /// @TODO is dbm::is_result() working here?
+               if ($r === false) {
+                       notice( t('Unable to set contact photo.') . EOL);
+               }
 
                // reload contact info
 
                $r = q("SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
                        intval($contact_id)
                );
-               if(count($r))
+               if (dbm::is_result($r)) {
                        $contact = $r[0];
-               else
+               } else {
                        $contact = null;
+               }
 
 
-               if((isset($new_relation) && $new_relation == CONTACT_IS_FRIEND)) {
+               if ((isset($new_relation) && $new_relation == CONTACT_IS_FRIEND)) {
 
-                       if(($contact) && ($contact['network'] === NETWORK_DIASPORA)) {
-                               require_once('include/diaspora.php');
+                       if (($contact) && ($contact['network'] === NETWORK_DIASPORA)) {
+                               require_once 'include/diaspora.php';
                                $ret = Diaspora::send_share($user[0],$r[0]);
                                logger('share returns: ' . $ret);
                        }
@@ -443,9 +446,9 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                                intval($uid)
                        );
 
-                       if((count($r)) && ($r[0]['hide-friends'] == 0) && ($activity) && (! $hidden)) {
+                       if((dbm::is_result($r)) && ($r[0]['hide-friends'] == 0) && ($activity) && (! $hidden)) {
 
-                               require_once('include/items.php');
+                               require_once 'include/items.php';
 
                                $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
                                        intval($uid)
@@ -503,10 +506,11 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                // Let's send our user to the contact editor in case they want to
                // do anything special with this new friend.
 
-               if($handsfree === null)
-                       goaway($a->get_baseurl() . '/contacts/' . intval($contact_id));
-               else
+               if ($handsfree === null) {
+                       goaway(System::baseUrl() . '/contacts/' . intval($contact_id));
+               } else {
                        return;
+               }
                //NOTREACHED
        }
 
@@ -522,7 +526,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
         *
         */
 
-       if(x($_POST,'source_url')) {
+       if (x($_POST,'source_url')) {
 
                // We are processing an external confirmation to an introduction created by our user.
 
@@ -543,7 +547,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
 
                // If $aes_key is set, both of these items require unpacking from the hex transport encoding.
 
-               if(x($aes_key)) {
+               if (x($aes_key)) {
                        $aes_key = hex2bin($aes_key);
                        $public_key = hex2bin($public_key);
                }
@@ -553,7 +557,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                $r = q("SELECT * FROM `user` WHERE `nickname` = '%s' LIMIT 1",
                        dbesc($node));
 
-               if(! count($r)) {
+               if (! dbm::is_result($r)) {
                        $message = sprintf(t('No user record found for \'%s\' '), $node);
                        xml_status(3,$message); // failure
                        // NOTREACHED
@@ -584,17 +588,18 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                        dbesc($decrypted_source_url),
                        intval($local_uid)
                );
-               if(! count($ret)) {
-                       if(strstr($decrypted_source_url,'http:'))
+               if (!dbm::is_result($ret)) {
+                       if (strstr($decrypted_source_url,'http:')) {
                                $newurl = str_replace('http:','https:',$decrypted_source_url);
-                       else
+                       } else {
                                $newurl = str_replace('https:','http:',$decrypted_source_url);
+                       }
 
                        $ret = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
                                dbesc($newurl),
                                intval($local_uid)
                        );
-                       if(! count($ret)) {
+                       if (!dbm::is_result($ret)) {
                                // this is either a bogus confirmation (?) or we deleted the original introduction.
                                $message = t('Contact record was not found for you on our site.');
                                xml_status(3,$message);
@@ -609,7 +614,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                $foreign_pubkey = $ret[0]['site-pubkey'];
                $dfrn_record    = $ret[0]['id'];
 
-               if(! $foreign_pubkey) {
+               if (! $foreign_pubkey) {
                        $message = sprintf( t('Site public key not available in contact record for URL %s.'), $newurl);
                        xml_status(3,$message);
                }
@@ -617,7 +622,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                $decrypted_dfrn_id = "";
                openssl_public_decrypt($dfrn_id,$decrypted_dfrn_id,$foreign_pubkey);
 
-               if(strlen($aes_key)) {
+               if (strlen($aes_key)) {
                        $decrypted_aes_key = "";
                        openssl_private_decrypt($aes_key,$decrypted_aes_key,$my_prvkey);
                        $dfrn_pubkey = openssl_decrypt($public_key,'AES-256-CBC',$decrypted_aes_key);
@@ -629,7 +634,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                $r = q("SELECT * FROM `contact` WHERE `dfrn-id` = '%s' LIMIT 1",
                        dbesc($decrypted_dfrn_id)
                );
-               if(count($r)) {
+               if (dbm::is_result($r)) {
                        $message = t('The ID provided by your system is a duplicate on our system. It should work if you try again.');
                        xml_status(1,$message); // Birthday paradox - duplicate dfrn-id
                        // NOTREACHED
@@ -640,7 +645,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                        dbesc($dfrn_pubkey),
                        intval($dfrn_record)
                );
-               if(! count($r)) {
+               if (! dbm::is_result($r)) {
                        $message = t('Unable to set your contact credentials on our system.');
                        xml_status(3,$message);
                }
@@ -661,23 +666,26 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                $r = q("SELECT `photo` FROM `contact` WHERE `id` = %d LIMIT 1",
                        intval($dfrn_record));
 
-               if(count($r))
+               if (dbm::is_result($r)) {
                        $photo = $r[0]['photo'];
-               else
-                       $photo = $a->get_baseurl() . '/images/person-175.jpg';
+               } else {
+                       $photo = System::baseUrl() . '/images/person-175.jpg';
+               }
 
-               require_once("include/Photo.php");
+               require_once 'include/Photo.php';
 
                update_contact_avatar($photo,$local_uid,$dfrn_record);
 
                logger('dfrn_confirm: request - photos imported');
 
                $new_relation = CONTACT_IS_SHARING;
-               if(($relation == CONTACT_IS_FOLLOWER) || ($duplex))
+               if (($relation == CONTACT_IS_FOLLOWER) || ($duplex)) {
                        $new_relation = CONTACT_IS_FRIEND;
+               }
 
-               if(($relation == CONTACT_IS_FOLLOWER) && ($duplex))
+               if (($relation == CONTACT_IS_FOLLOWER) && ($duplex)) {
                        $duplex = 0;
+               }
 
                $r = q("UPDATE `contact` SET
                        `rel` = %d,
@@ -699,7 +707,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                        dbesc(NETWORK_DFRN),
                        intval($dfrn_record)
                );
-               if($r === false) {    // indicates schema is messed up or total db failure
+               if ($r === false) {    // indicates schema is messed up or total db failure
                        $message = t('Unable to update your contact profile details on our system');
                        xml_status(3,$message);
                }
@@ -714,10 +722,10 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                        intval($dfrn_record)
                );
 
-               if(count($r))
+               if (dbm::is_result($r))
                        $combined = $r[0];
 
-               if((count($r)) && ($r[0]['notify-flags'] & NOTIFY_CONFIRM)) {
+               if((dbm::is_result($r)) && ($r[0]['notify-flags'] & NOTIFY_CONFIRM)) {
                        $mutual = ($new_relation == CONTACT_IS_FRIEND);
                        notification(array(
                                'type'         => NOTIFY_CONFIRM,
@@ -726,7 +734,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                                'to_name'      => $r[0]['username'],
                                'to_email'     => $r[0]['email'],
                                'uid'          => $r[0]['uid'],
-                               'link'             => $a->get_baseurl() . '/contacts/' . $dfrn_record,
+                               'link'             => System::baseUrl() . '/contacts/' . $dfrn_record,
                                'source_name'  => ((strlen(stripslashes($r[0]['name']))) ? stripslashes($r[0]['name']) : t('[Name Withheld]')),
                                'source_link'  => $r[0]['url'],
                                'source_photo' => $r[0]['photo'],
@@ -742,9 +750,9 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
                                intval($local_uid)
                        );
 
-                       if((count($r)) && ($r[0]['hide-friends'] == 0)) {
+                       if((dbm::is_result($r)) && ($r[0]['hide-friends'] == 0)) {
 
-                               require_once('include/items.php');
+                               require_once 'include/items.php';
 
                                $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
                                        intval($local_uid)
@@ -801,7 +809,7 @@ function dfrn_confirm_post(&$a,$handsfree = null) {
 
        // somebody arrived here by mistake or they are fishing. Send them to the homepage.
 
-       goaway(z_root());
+       goaway(System::baseUrl());
        // NOTREACHED
 
 }