]> git.mxchange.org Git - friendica.git/blobdiff - include/items.php
Merge pull request #1683 from fabrixxm/issue-1655
[friendica.git] / include / items.php
index 7a30130bbcb019fe760150eb1d188869c2f6e8de..475980d2dc972584aaef409cbf64134566b53c1a 100644 (file)
@@ -15,6 +15,9 @@ require_once('include/plaintext.php');
 require_once('include/ostatus.php');
 require_once('mod/share.php');
 
+require_once('library/defuse/php-encryption-1.2.1/Crypto.php');
+
+
 function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0, $forpubsub = false) {
 
 
@@ -38,7 +41,7 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0,
 
        // default permissions - anonymous user
 
-       $sql_extra = " AND `allow_cid` = '' AND `allow_gid` = '' AND `deny_cid`  = '' AND `deny_gid`  = '' ";
+       $sql_extra = " AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' ";
 
        $r = q("SELECT `contact`.*, `user`.`uid` AS `user_uid`, `user`.`nickname`, `user`.`timezone`, `user`.`page-flags`
                FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
@@ -118,9 +121,10 @@ function get_feed_for(&$a, $dfrn_id, $owner_nick, $last_update, $direction = 0,
 
        // Include answers to status.net posts in pubsub feeds
        if($forpubsub) {
-               $sql_post_table = "INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent` ";
-               $visibility = sprintf("AND (`item`.`parent` = `item`.`id`) OR (`item`.`network` = '%s' AND `thread`.`network`='%s')",
-                                       dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS));
+               $sql_post_table = "INNER JOIN `thread` ON `thread`.`iid` = `item`.`parent`
+                               LEFT JOIN `item` AS `thritem` ON `thritem`.`uri`=`item`.`thr-parent` AND `thritem`.`uid`=`item`.`uid`";
+               $visibility = sprintf("AND (`item`.`parent` = `item`.`id`) OR (`item`.`network` = '%s' AND ((`thread`.`network`='%s') OR (`thritem`.`network` = '%s')))",
+                                       dbesc(NETWORK_DFRN), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_OSTATUS));
                $date_field = "`received`";
                $sql_order = "`item`.`received` DESC";
        } else {
@@ -1982,13 +1986,13 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
        if($contact['duplex'] && $contact['issued-id'])
                $idtosend = '1:' . $orig_id;
 
-       $rino = ((function_exists('mcrypt_encrypt')) ? 1 : 0);
-
-       $rino_enable = get_config('system','rino_encrypt');
+    
+       $rino = get_config('system','rino_encrypt');
+       $rino = intval($rino);
 
-       if(! $rino_enable)
-               $rino = 0;
+   
 
+       
        $ssl_val = intval(get_config('system','ssl_policy'));
        $ssl_policy = '';
 
@@ -2005,7 +2009,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
                        break;
        }
 
-       $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino=1' : '');
+       $url = $contact['notify'] . '&dfrn_id=' . $idtosend . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . (($rino) ? '&rino='.$rino : '');
 
        logger('dfrn_deliver: ' . $url);
 
@@ -2036,7 +2040,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
        $challenge    = hex2bin((string) $res->challenge);
        $perm         = (($res->perm) ? $res->perm : null);
        $dfrn_version = (float) (($res->dfrn_version) ? $res->dfrn_version : 2.0);
-       $rino_allowed = ((intval($res->rino) === 1) ? 1 : 0);
+       $rino_remote_version = intval($res->rino);
        $page         = (($owner['page-flags'] == PAGE_COMMUNITY) ? 1 : 0);
 
        if($owner['page-flags'] == PAGE_PRVGROUP)
@@ -2097,11 +2101,46 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
        if($page)
                $postvars['page'] = $page;
 
-       if($rino && $rino_allowed && (! $dissolve)) {
-               $key = substr(random_string(),0,16);
-               $data = bin2hex(aes_encrypt($postvars['data'],$key));
-               $postvars['data'] = $data;
-               logger('rino: sent key = ' . $key, LOGGER_DEBUG);
+
+       if($rino>0 && $rino_remote_version>0 && (! $dissolve)) {
+               logger('rino version: '. $rino_remote_version);
+
+               switch($rino_remote_version) {
+                       case 1:
+                               // Deprecated rino version!
+                               $key = substr(random_string(),0,16);
+                               $data = aes_encrypt($postvars['data'],$key);
+                               break;
+                       case 2:
+                               // RINO 2 based on php-encryption
+                               try {
+                                       $key = Crypto::createNewRandomKey();
+                               } catch (CryptoTestFailed $ex) {
+                                       logger('Cannot safely create a key');
+                                       return -1;
+                               } catch (CannotPerformOperation $ex) {
+                                       logger('Cannot safely create a key');
+                                       return -1; 
+                               }
+                               try {
+                                       $data = Crypto::encrypt($postvars['data'], $key);
+                               } catch (CryptoTestFailed $ex) {
+                                       logger('Cannot safely perform encryption');
+                                       return -1; 
+                               } catch (CannotPerformOperation $ex) {
+                                       logger('Cannot safely perform encryption');
+                                       return -1; 
+                               }
+                               break;
+                       default:
+                               logger("rino: invalid requested verision '$rino_remote_version'");
+                               return -1;
+               }
+               
+               $postvars['rino'] = $rino_remote_version;
+               $postvars['data'] = bin2hex($data);
+               
+               #logger('rino: sent key = ' . $key, LOGGER_DEBUG);
 
 
                if($dfrn_version >= 2.1) {
@@ -2128,6 +2167,7 @@ function dfrn_deliver($owner,$contact,$atom, $dissolve = false) {
 
                $postvars['key'] = bin2hex($postvars['key']);
        }
+       
 
        logger('dfrn_deliver: ' . "SENDING: " . print_r($postvars,true), LOGGER_DATA);