]> git.mxchange.org Git - friendica.git/blobdiff - include/notifier.php
Merge pull request #3909 from zeroadam/BaseObject-#3878
[friendica.git] / include / notifier.php
index 370611b4eb438aaf69509aee01622e80d15436a9..55f2986d21c7ca4804b17b3382b4ad7cf074ba5b 100644 (file)
@@ -1,13 +1,16 @@
 <?php
 
-use \Friendica\Core\Config;
-
-require_once('include/queue_fn.php');
-require_once('include/html2plain.php');
-require_once('include/Scrape.php');
-require_once('include/diaspora.php');
-require_once('include/ostatus.php');
-require_once('include/salmon.php');
+use Friendica\App;
+use Friendica\Core\Config;
+use Friendica\Core\Worker;
+use Friendica\Database\DBM;
+use Friendica\Network\Probe;
+use Friendica\Protocol\Diaspora;
+use Friendica\Protocol\OStatus;
+
+require_once 'include/queue_fn.php';
+require_once 'include/html2plain.php';
+require_once 'include/salmon.php';
 
 /*
  * This file was at one time responsible for doing all deliveries, but this caused
@@ -18,7 +21,7 @@ require_once('include/salmon.php');
 /*
  * The notifier is typically called with:
  *
- *             proc_run(PRIORITY_HIGH, "include/notifier.php", COMMAND, ITEM_ID);
+ *             Worker::add(PRIORITY_HIGH, "notifier", COMMAND, ITEM_ID);
  *
  * where COMMAND is one of the following:
  *
@@ -45,10 +48,10 @@ require_once('include/salmon.php');
 function notifier_run(&$argv, &$argc){
        global $a;
 
-       require_once('include/datetime.php');
-       require_once('include/items.php');
-       require_once('include/bbcode.php');
-       require_once('include/email.php');
+       require_once 'include/datetime.php';
+       require_once 'include/items.php';
+       require_once 'include/bbcode.php';
+       require_once 'include/email.php';
 
        if ($argc < 3) {
                return;
@@ -138,7 +141,7 @@ function notifier_run(&$argv, &$argc){
                if (!$r) {
                        return;
                }
-               require_once('include/Contact.php');
+               require_once 'include/Contact.php';
                foreach ($r as $contact) {
                        terminate_friendship($user, $self, $contact);
                }
@@ -148,14 +151,15 @@ function notifier_run(&$argv, &$argc){
                $relocate = true;
                $uid = $item_id;
 
-               $recipients_relocate = q("SELECT * FROM contact WHERE uid = %d  AND self = 0 AND network = '%s'" , intval($uid), NETWORK_DFRN);
+               $recipients_relocate = q("SELECT * FROM `contact` WHERE `uid` = %d AND NOT `self` AND `network` IN ('%s', '%s')",
+                                       intval($uid), NETWORK_DFRN, NETWORK_DIASPORA);
        } else {
                // find ancestors
-               $r = q("SELECT * FROM `item` WHERE `id` = %d and visible = 1 and moderated = 0 LIMIT 1",
+               $r = q("SELECT * FROM `item` WHERE `id` = %d AND visible = 1 AND moderated = 0 LIMIT 1",
                        intval($item_id)
                );
 
-               if ((! dbm::is_result($r)) || (! intval($r[0]['parent']))) {
+               if ((! DBM::is_result($r)) || (! intval($r[0]['parent']))) {
                        return;
                }
 
@@ -165,7 +169,7 @@ function notifier_run(&$argv, &$argc){
                $updated = $r[0]['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",
+                       FROM `item` LEFT JOIN `sign` ON `sign`.`iid` = `item`.`id` WHERE `parent` = %d AND visible = 1 AND moderated = 0 ORDER BY `id` ASC",
                        intval($parent_id)
                );
 
@@ -196,7 +200,7 @@ function notifier_run(&$argv, &$argc){
                intval($uid)
        );
 
-       if (! dbm::is_result($r)) {
+       if (! DBM::is_result($r)) {
                return;
        }
 
@@ -204,8 +208,6 @@ function notifier_run(&$argv, &$argc){
 
        $walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
 
-       $hub = get_config('system','huburl');
-
        // Should the post be transmitted to Diaspora?
        $diaspora_delivery = true;
 
@@ -215,14 +217,17 @@ function notifier_run(&$argv, &$argc){
        // Do a PuSH
        $push_notify = false;
 
+       // Deliver directly to a forum, don't PuSH
+       $direct_forum_delivery = false;
+
        // fill this in with a single salmon slap if applicable
        $slap = '';
 
        if (! ($mail || $fsuggest || $relocate)) {
 
-               $slap = ostatus::salmon($target_item,$owner);
+               $slap = OStatus::salmon($target_item, $owner);
 
-               require_once('include/group.php');
+               require_once 'include/group.php';
 
                $parent = $items[0];
 
@@ -285,6 +290,26 @@ function notifier_run(&$argv, &$argc){
                if ($parent['origin']) {
                        $relay_to_owner = false;
                }
+
+               // Special treatment for forum posts
+               if (($target_item['author-link'] != $target_item['owner-link']) &&
+                       ($owner['id'] != $target_item['contact-id']) &&
+                       ($target_item['uri'] === $target_item['parent-uri'])) {
+
+                       $fields = array('forum', 'prv');
+                       $condition = array('id' => $target_item['contact-id']);
+                       $contact = dba::select('contact', $fields, $condition, array('limit' => 1));
+                       if (!DBM::is_result($contact)) {
+                               // Should never happen
+                               return false;
+                       }
+
+                       // Is the post from a forum?
+                       if ($contact['forum'] || $contact['prv']) {
+                               $relay_to_owner = true;
+                               $direct_forum_delivery = true;
+                       }
+               }
                if ($relay_to_owner) {
                        logger('notifier: followup '.$target_item["guid"], LOGGER_DEBUG);
                        // local followup to remote post
@@ -294,13 +319,13 @@ function notifier_run(&$argv, &$argc){
                        $recipients = array($parent['contact-id']);
                        $recipients_followup  = array($parent['contact-id']);
 
-                       //if (!$target_item['private'] AND $target_item['wall'] AND
-                       if (!$target_item['private'] AND
+                       //if (!$target_item['private'] && $target_item['wall'] &&
+                       if (!$target_item['private'] &&
                                (strlen($target_item['allow_cid'].$target_item['allow_gid'].
                                        $target_item['deny_cid'].$target_item['deny_gid']) == 0))
                                $push_notify = true;
 
-                       if (($thr_parent AND ($thr_parent[0]['network'] == NETWORK_OSTATUS)) OR ($parent['network'] == NETWORK_OSTATUS)) {
+                       if (($thr_parent && ($thr_parent[0]['network'] == NETWORK_OSTATUS)) || ($parent['network'] == NETWORK_OSTATUS)) {
 
                                $push_notify = true;
 
@@ -311,13 +336,18 @@ function notifier_run(&$argv, &$argc){
                                                intval($uid),
                                                dbesc(NETWORK_DFRN)
                                        );
-                                       if (dbm::is_result($r)) {
+                                       if (DBM::is_result($r)) {
                                                foreach ($r as $rr) {
                                                        $recipients_followup[] = $rr['id'];
                                                }
                                        }
                                }
                        }
+
+                       if ($direct_forum_delivery) {
+                               $push_notify = false;
+                       }
+
                        logger("Notify ".$target_item["guid"]." via PuSH: ".($push_notify?"Yes":"No"), LOGGER_DEBUG);
                } else {
                        $followup = false;
@@ -347,7 +377,7 @@ function notifier_run(&$argv, &$argc){
                        // a delivery fork. private groups (forum_mode == 2) do not uplink
 
                        if ((intval($parent['forum_mode']) == 1) && (! $top_level) && ($cmd !== 'uplink')) {
-                               proc_run(PRIORITY_HIGH,'include/notifier.php','uplink',$item_id);
+                               Worker::add($a->queue['priority'], 'notifier', 'uplink', $item_id);
                        }
 
                        $conversants = array();
@@ -384,7 +414,7 @@ function notifier_run(&$argv, &$argc){
 
                // If the thread parent is OStatus then do some magic to distribute the messages.
                // We have not only to look at the parent, since it could be a Friendica thread.
-               if (($thr_parent AND ($thr_parent[0]['network'] == NETWORK_OSTATUS)) OR ($parent['network'] == NETWORK_OSTATUS)) {
+               if (($thr_parent && ($thr_parent[0]['network'] == NETWORK_OSTATUS)) || ($parent['network'] == NETWORK_OSTATUS)) {
 
                        $diaspora_delivery = false;
 
@@ -394,10 +424,10 @@ function notifier_run(&$argv, &$argc){
                        $r = q("SELECT `url`, `notify` FROM `contact` WHERE `nurl`='%s' AND `uid` IN (0, %d) AND `notify` != ''",
                                dbesc(normalise_link($thr_parent[0]['author-link'])),
                                intval($uid));
-                       if (dbm::is_result($r)) {
+                       if (DBM::is_result($r)) {
                                $probed_contact = $r[0];
                        } else {
-                               $probed_contact = probe_url($thr_parent[0]['author-link']);
+                               $probed_contact = Probe::uri($thr_parent[0]['author-link']);
                        }
 
                        if ($probed_contact["notify"] != "") {
@@ -409,10 +439,10 @@ function notifier_run(&$argv, &$argc){
                        $r = q("SELECT `url`, `notify` FROM `contact` WHERE `nurl`='%s' AND `uid` IN (0, %d) AND `notify` != ''",
                                dbesc(normalise_link($thr_parent[0]['owner-link'])),
                                intval($uid));
-                       if (dbm::is_result($r)) {
+                       if (DBM::is_result($r)) {
                                $probed_contact = $r[0];
                        } else {
-                               $probed_contact = probe_url($thr_parent[0]['owner-link']);
+                               $probed_contact = Probe::uri($thr_parent[0]['owner-link']);
                        }
 
                        if ($probed_contact["notify"] != "") {
@@ -426,7 +456,7 @@ function notifier_run(&$argv, &$argc){
                                //logger('Checking tag '.$x, LOGGER_DEBUG);
                                $matches = null;
                                if (preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
-                                               $probed_contact = probe_url($matches[1]);
+                                               $probed_contact = Probe::uri($matches[1]);
                                        if ($probed_contact["notify"] != "") {
                                                logger('Notify mentioned user '.$probed_contact["url"].': '.$probed_contact["notify"]);
                                                $url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
@@ -445,7 +475,7 @@ function notifier_run(&$argv, &$argc){
 
        // If this is a public message and pubmail is set on the parent, include all your email contacts
 
-       $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
+       $mail_disabled = ((function_exists('imap_open') && (!Config::get('system','imap_disabled'))) ? 0 : 1);
 
        if (! $mail_disabled) {
                if ((! strlen($target_item['allow_cid'])) && (! strlen($target_item['allow_gid']))
@@ -455,7 +485,7 @@ function notifier_run(&$argv, &$argc){
                                intval($uid),
                                dbesc(NETWORK_MAIL)
                        );
-                       if (dbm::is_result($r)) {
+                       if (DBM::is_result($r)) {
                                foreach ($r as $rr) {
                                        $recipients[] = $rr['id'];
                                }
@@ -479,14 +509,15 @@ function notifier_run(&$argv, &$argc){
 
        // delivery loop
 
-       if (dbm::is_result($r)) {
+       if (DBM::is_result($r)) {
                foreach ($r as $contact) {
                        if ($contact['self']) {
                                continue;
                        }
                        logger("Deliver ".$target_item["guid"]." to ".$contact['url']." via network ".$contact['network'], LOGGER_DEBUG);
 
-                       proc_run(PRIORITY_HIGH,'include/delivery.php', $cmd, $item_id, $contact['id']);
+                       Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
+                                       'delivery', $cmd, $item_id, (int)$contact['id']);
                }
        }
 
@@ -494,7 +525,7 @@ function notifier_run(&$argv, &$argc){
        // They are especially used for notifications to OStatus users that don't follow us.
 
        if ($slap && count($url_recipients) && ($public_message || $push_notify) && $normal_mode) {
-               if (!get_config('system','dfrn_only')) {
+               if (!Config::get('system','dfrn_only')) {
                        foreach ($url_recipients as $url) {
                                if ($url) {
                                        logger('notifier: urldelivery: ' . $url);
@@ -517,8 +548,8 @@ function notifier_run(&$argv, &$argc){
                        }
 
                        $r1 = q("SELECT `batch`, ANY_VALUE(`id`) AS `id`, ANY_VALUE(`name`) AS `name`, ANY_VALUE(`network`) AS `network`
-                               FROM `contact` WHERE `network` = '%s'
-                               AND `uid` = %d AND `rel` != %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` GROUP BY `batch` ORDER BY rand()",
+                               FROM `contact` WHERE `network` = '%s' AND `batch` != ''
+                               AND `uid` = %d AND `rel` != %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` GROUP BY `batch`",
                                dbesc(NETWORK_DIASPORA),
                                intval($owner['uid']),
                                intval(CONTACT_IS_SHARING)
@@ -526,8 +557,7 @@ function notifier_run(&$argv, &$argc){
                }
 
                $r2 = q("SELECT `id`, `name`,`network` FROM `contact`
-                       WHERE `network` in ( '%s', '%s')  AND `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `archive`
-                       AND `rel` != %d order by rand() ",
+                       WHERE `network` in ('%s', '%s') AND `uid` = %d AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND `rel` != %d",
                        dbesc(NETWORK_DFRN),
                        dbesc(NETWORK_MAIL2),
                        intval($owner['uid']),
@@ -536,7 +566,7 @@ function notifier_run(&$argv, &$argc){
 
                $r = array_merge($r2,$r1,$r0);
 
-               if (dbm::is_result($r)) {
+               if (DBM::is_result($r)) {
                        logger('pubdeliver '.$target_item["guid"].': '.print_r($r,true), LOGGER_DEBUG);
 
                        foreach ($r as $rr) {
@@ -551,7 +581,8 @@ function notifier_run(&$argv, &$argc){
 
                                if ((! $mail) && (! $fsuggest) && (! $followup)) {
                                        logger('notifier: delivery agent: '.$rr['name'].' '.$rr['id'].' '.$rr['network'].' '.$target_item["guid"]);
-                                       proc_run(PRIORITY_HIGH,'include/delivery.php',$cmd,$item_id,$rr['id']);
+                                       Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
+                                                       'delivery', $cmd, $item_id, (int)$rr['id']);
                                }
                        }
                }
@@ -561,37 +592,17 @@ function notifier_run(&$argv, &$argc){
        }
 
        // Notify PuSH subscribers (Used for OStatus distribution of regular posts)
-       if ($push_notify AND strlen($hub)) {
-               $hubs = explode(',', $hub);
-               if (count($hubs)) {
-                       foreach ($hubs as $h) {
-                               $h = trim($h);
-                               if (! strlen($h)) {
-                                       continue;
-                               }
-
-                               if ($h === '[internal]') {
-                                       // Set push flag for PuSH subscribers to this topic,
-                                       // they will be notified in queue.php
-                                       q("UPDATE `push_subscriber` SET `push` = 1 ".
-                                         "WHERE `nickname` = '%s' AND `push` = 0", dbesc($owner['nickname']));
-
-                                       logger('Activating internal PuSH for item '.$item_id, LOGGER_DEBUG);
+       if ($push_notify) {
+               // Set push flag for PuSH subscribers to this topic,
+               // they will be notified in queue.php
+               q("UPDATE `push_subscriber` SET `push` = 1 ".
+                 "WHERE `nickname` = '%s' AND `push` = 0", dbesc($owner['nickname']));
 
-                               } else {
-
-                                       $params = 'hub.mode=publish&hub.url=' . urlencode( App::get_baseurl() . '/dfrn_poll/' . $owner['nickname'] );
-                                       post_url($h,$params);
-                                       logger('publish for item '.$item_id.' ' . $h . ' ' . $params . ' returned ' . $a->get_curl_code());
-                               }
-                               if (count($hubs) > 1) {
-                                       sleep(7);                               // try and avoid multiple hubs responding at precisely the same time
-                               }
-                       }
-               }
+               logger('Activating internal PuSH for item '.$item_id, LOGGER_DEBUG);
 
                // Handling the pubsubhubbub requests
-               proc_run(PRIORITY_HIGH,'include/pubsubpublish.php');
+               Worker::add(array('priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true),
+                               'pubsubpublish');
        }
 
        logger('notifier: calling hooks', LOGGER_DEBUG);