]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/Delivery.php
Merge pull request #4206 from MrPetovan/task/fix-scrutinizer-issues
[friendica.git] / src / Worker / Delivery.php
index 14fe3027fc162c7d8ef800f321ed072674e0ad50..fc7f884f6ab6fa9b8bd36cb942704864511b4773 100644 (file)
@@ -9,16 +9,18 @@ use Friendica\App;
 use Friendica\Core\System;
 use Friendica\Core\Config;
 use Friendica\Database\DBM;
-use Friendica\Object\Contact;
+use Friendica\Model\Contact;
+use Friendica\Model\User;
 use Friendica\Protocol\Diaspora;
 use Friendica\Protocol\DFRN;
+use Friendica\Protocol\Email;
+use dba;
 
 require_once 'include/queue_fn.php';
 require_once 'include/html2plain.php';
 require_once 'include/datetime.php';
 require_once 'include/items.php';
 require_once 'include/bbcode.php';
-require_once 'include/email.php';
 
 /// @todo This is some ugly code that needs to be split into several methods
 
@@ -84,18 +86,17 @@ class Delivery {
                        $uid = $item_id;
                } else {
                        // find ancestors
-                       $r = q("SELECT * FROM `item` WHERE `id` = %d AND visible = 1 AND moderated = 0 LIMIT 1",
-                               intval($item_id)
-                       );
+                       $target_item = dba::fetch_first("SELECT `item`.*, `contact`.`uid` AS `cuid` FROM `item`
+                                                       INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
+                                                       WHERE `item`.`id` = ? AND `visible` AND NOT `moderated`", $item_id);
 
-                       if (!DBM::is_result($r) || !intval($r[0]['parent'])) {
+                       if (!DBM::is_result($target_item) || !intval($target_item['parent'])) {
                                return;
                        }
 
-                       $target_item = $r[0];
-                       $parent_id = intval($r[0]['parent']);
-                       $uid = $r[0]['uid'];
-                       $updated = $r[0]['edited'];
+                       $parent_id = intval($target_item['parent']);
+                       $uid = $target_item['cuid'];
+                       $updated = $target_item['edited'];
 
                        $items = q("SELECT `item`.*, `sign`.`signed_text`,`sign`.`signature`,`sign`.`signer`
                                FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d AND visible = 1 AND moderated = 0 ORDER BY `id` ASC",
@@ -140,27 +141,16 @@ class Delivery {
                        }
                }
 
-               $r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
-                       `user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
-                       `user`.`page-flags`, `user`.`account-type`, `user`.`prvnets`
-                       FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
-                       WHERE `contact`.`uid` = %d AND `contact`.`self` = 1 LIMIT 1",
-                       intval($uid)
-               );
-
-               if (!DBM::is_result($r)) {
+               $owner = User::getOwnerDataById($uid);
+               if (!$owner) {
                        return;
                }
 
-               $owner = $r[0];
-
                $walltowall = (($top_level && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
 
                $public_message = true;
 
-               if (!($mail || $fsuggest || $relocate)) {
-                       require_once 'include/group.php';
-
+               if (!$mail && !$fsuggest && !$relocate) {
                        $parent = $items[0];
 
                        // This is IMPORTANT!!!!
@@ -205,10 +195,10 @@ class Delivery {
                                $followup = true;
                        }
 
-                       if ((strlen($parent['allow_cid']))
-                               || (strlen($parent['allow_gid']))
-                               || (strlen($parent['deny_cid']))
-                               || (strlen($parent['deny_gid']))
+                       if (strlen($parent['allow_cid'])
+                               || strlen($parent['allow_gid'])
+                               || strlen($parent['deny_cid'])
+                               || strlen($parent['deny_gid'])
                                || $parent["private"]) {
                                $public_message = false; // private recipients, not public
                        }
@@ -227,9 +217,14 @@ class Delivery {
                }
                $deliver_status = 0;
 
+               // Transmit via Diaspora if not possible via Friendica
+               if (($item['uid'] == 0) && ($contact['network'] == NETWORK_DFRN)) {
+                       $contact['network'] = NETWORK_DIASPORA;
+               }
+
                logger("main delivery by delivery: followup=$followup mail=$mail fsuggest=$fsuggest relocate=$relocate - network ".$contact['network']);
 
-               switch($contact['network']) {
+               switch ($contact['network']) {
 
                        case NETWORK_DFRN:
                                logger('notifier: '.$target_item["guid"].' dfrndelivery: '.$contact['name']);
@@ -293,7 +288,6 @@ class Delivery {
                                // perform local delivery if we are on the same site
 
                                if (link_compare($basepath,System::baseUrl())) {
-
                                        $nickname = basename($contact['url']);
                                        if ($contact['issued-id']) {
                                                $sql_extra = sprintf(" AND `dfrn-id` = '%s' ", dbesc($contact['issued-id']));
@@ -376,7 +370,6 @@ class Delivery {
                                break;
 
                        case NETWORK_MAIL:
-                       case NETWORK_MAIL2:
 
                                if (Config::get('system','dfrn_only')) {
                                        break;
@@ -389,65 +382,67 @@ class Delivery {
                                }
 
                                if ($cmd === 'wall-new' || $cmd === 'comment-new') {
-
                                        $it = null;
                                        if ($cmd === 'wall-new') {
                                                $it = $items[0];
                                        } else {
-                                               $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-                                                       intval($item_id),
-                                                       intval($uid)
+                                               $r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1",
+                                                       intval($item_id)
                                                );
-                                               if (DBM::is_result($r))
+                                               if (DBM::is_result($r)) {
                                                        $it = $r[0];
+                                               }
                                        }
-                                       if (!$it)
+                                       if (!$it) {
                                                break;
-
+                                       }
 
                                        $local_user = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
                                                intval($uid)
                                        );
-                                       if (!count($local_user))
+                                       if (!count($local_user)) {
                                                break;
+                                       }
 
                                        $reply_to = '';
                                        $r1 = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
                                                intval($uid)
                                        );
-                                       if ($r1 && $r1[0]['reply_to'])
+                                       if ($r1 && $r1[0]['reply_to']) {
                                                $reply_to = $r1[0]['reply_to'];
+                                       }
 
-                                       $subject  = (($it['title']) ? email_header_encode($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
+                                       $subject  = (($it['title']) ? Email::encodeHeader($it['title'],'UTF-8') : t("\x28no subject\x29")) ;
 
                                        // only expose our real email address to true friends
 
                                        if (($contact['rel'] == CONTACT_IS_FRIEND) && !$contact['blocked']) {
                                                if ($reply_to) {
-                                                       $headers  = 'From: '.email_header_encode($local_user[0]['username'],'UTF-8').' <'.$reply_to.'>'."\n";
+                                                       $headers  = 'From: '.Email::encodeHeader($local_user[0]['username'],'UTF-8').' <'.$reply_to.'>'."\n";
                                                        $headers .= 'Sender: '.$local_user[0]['email']."\n";
                                                } else {
-                                                       $headers  = 'From: '.email_header_encode($local_user[0]['username'],'UTF-8').' <'.$local_user[0]['email'].'>'."\n";
+                                                       $headers  = 'From: '.Email::encodeHeader($local_user[0]['username'],'UTF-8').' <'.$local_user[0]['email'].'>'."\n";
                                                }
                                        } else {
-                                               $headers  = 'From: '. email_header_encode($local_user[0]['username'],'UTF-8') .' <'. t('noreply') .'@'.$a->get_hostname() .'>'. "\n";
+                                               $headers  = 'From: '. Email::encodeHeader($local_user[0]['username'],'UTF-8') .' <'. t('noreply') .'@'.$a->get_hostname() .'>'. "\n";
                                        }
 
                                        //if ($reply_to)
                                        //      $headers .= 'Reply-to: '.$reply_to . "\n";
 
-                                       $headers .= 'Message-Id: <'. iri2msgid($it['uri']).'>'. "\n";
+                                       $headers .= 'Message-Id: <'. Email::iri2msgid($it['uri']).'>'. "\n";
 
                                        //logger("Mail: uri: ".$it['uri']." parent-uri ".$it['parent-uri'], LOGGER_DEBUG);
                                        //logger("Mail: Data: ".print_r($it, true), LOGGER_DEBUG);
                                        //logger("Mail: Data: ".print_r($it, true), LOGGER_DATA);
 
                                        if ($it['uri'] !== $it['parent-uri']) {
-                                               $headers .= "References: <".iri2msgid($it["parent-uri"]).">";
+                                               $headers .= "References: <".Email::iri2msgid($it["parent-uri"]).">";
 
                                                // If Threading is enabled, write down the correct parent
-                                               if (($it["thr-parent"] != "") && ($it["thr-parent"] != $it["parent-uri"]))
-                                                       $headers .= " <".iri2msgid($it["thr-parent"]).">";
+                                               if (($it["thr-parent"] != "") && ($it["thr-parent"] != $it["parent-uri"])) {
+                                                       $headers .= " <".Email::iri2msgid($it["thr-parent"]).">";
+                                               }
                                                $headers .= "\n";
 
                                                if (!$it['title']) {
@@ -462,43 +457,46 @@ class Delivery {
                                                                        dbesc($it['parent-uri']),
                                                                        intval($uid));
 
-                                                               if (DBM::is_result($r) && ($r[0]['title'] != ''))
+                                                               if (DBM::is_result($r) && ($r[0]['title'] != '')) {
                                                                        $subject = $r[0]['title'];
+                                                               }
                                                        }
                                                }
-                                               if (strncasecmp($subject,'RE:',3))
+                                               if (strncasecmp($subject,'RE:',3)) {
                                                        $subject = 'Re: '.$subject;
+                                               }
                                        }
-                                       email_send($addr, $subject, $headers, $it);
+                                       Email::send($addr, $subject, $headers, $it);
                                }
                                break;
 
                        case NETWORK_DIASPORA:
-                               if ($public_message)
+                               if ($public_message) {
                                        $loc = 'public batch '.$contact['batch'];
-                               else
+                               } else {
                                        $loc = $contact['name'];
+                               }
 
                                logger('delivery: diaspora batch deliver: '.$loc);
 
-                               if (Config::get('system','dfrn_only') || !Config::get('system','diaspora_enabled'))
+                               if (Config::get('system','dfrn_only') || !Config::get('system','diaspora_enabled')) {
                                        break;
-
+                               }
                                if ($mail) {
-                                       Diaspora::send_mail($item,$owner,$contact);
+                                       Diaspora::sendMail($item,$owner,$contact);
                                        break;
                                }
 
-                               if (!$normal_mode)
+                               if (!$normal_mode) {
                                        break;
-
-                               if (!$contact['pubkey'] && !$public_message)
+                               }
+                               if (!$contact['pubkey'] && !$public_message) {
                                        break;
-
+                               }
                                if (($target_item['deleted']) && (($target_item['uri'] === $target_item['parent-uri']) || $followup)) {
                                        // top-level retraction
                                        logger('diaspora retract: '.$loc);
-                                       Diaspora::send_retraction($target_item,$owner,$contact,$public_message);
+                                       Diaspora::sendRetraction($target_item,$owner,$contact,$public_message);
                                        break;
                                } elseif ($relocate) {
                                        Diaspora::sendAccountMigration($owner, $contact, $uid);
@@ -506,17 +504,17 @@ class Delivery {
                                } elseif ($followup) {
                                        // send comments and likes to owner to relay
                                        logger('diaspora followup: '.$loc);
-                                       Diaspora::send_followup($target_item,$owner,$contact,$public_message);
+                                       Diaspora::sendFollowup($target_item,$owner,$contact,$public_message);
                                        break;
                                } elseif ($target_item['uri'] !== $target_item['parent-uri']) {
                                        // we are the relay - send comments, likes and relayable_retractions to our conversants
                                        logger('diaspora relay: '.$loc);
-                                       Diaspora::send_relay($target_item,$owner,$contact,$public_message);
+                                       Diaspora::sendRelay($target_item,$owner,$contact,$public_message);
                                        break;
                                } elseif ($top_level && !$walltowall) {
                                        // currently no workable solution for sending walltowall
                                        logger('diaspora status: '.$loc);
-                                       Diaspora::send_status($target_item,$owner,$contact,$public_message);
+                                       Diaspora::sendStatus($target_item,$owner,$contact,$public_message);
                                        break;
                                }