Merge pull request #3917 from annando/worker-again

All workers in /include now moved to /src/worker
This commit is contained in:
Hypolite Petovan 2017-11-19 14:51:12 -05:00 committed by GitHub
commit 51a29d381f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 1155 additions and 1159 deletions

View File

@ -34,7 +34,7 @@ function user_remove($uid) {
// The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php) // The user and related data will be deleted in "cron_expire_and_remove_users" (cronjobs.php)
q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid)); q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid));
Worker::add(PRIORITY_HIGH, "notifier", "removeme", $uid); Worker::add(PRIORITY_HIGH, "Notifier", "removeme", $uid);
// Send an update to the directory // Send an update to the directory
Worker::add(PRIORITY_LOW, "Directory", $r['url']); Worker::add(PRIORITY_LOW, "Directory", $r['url']);

View File

@ -3873,7 +3873,7 @@ function api_account_update_profile_image($type)
Worker::add(PRIORITY_LOW, "Directory", $url); Worker::add(PRIORITY_LOW, "Directory", $url);
} }
Worker::add(PRIORITY_LOW, 'profile_update', api_user()); Worker::add(PRIORITY_LOW, 'ProfileUpdate', api_user());
// output for client // output for client
if ($data) { if ($data) {

View File

@ -1,19 +0,0 @@
<?php
/**
* @file include/create_shadowentry.php
* @brief This script creates posts with UID = 0 for a given public post.
*
* This script is started from mod/item.php to save some time when doing a post.
*/
require_once("include/threads.php");
function create_shadowentry_run($argv, $argc) {
if ($argc != 2) {
return;
}
$message_id = intval($argv[1]);
add_shadow_entry($message_id);
}

View File

@ -1,76 +0,0 @@
<?php
use Friendica\Core\Config;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
function expire_run(&$argv, &$argc){
global $a;
require_once('include/datetime.php');
require_once('include/items.php');
require_once('include/Contact.php');
load_hooks();
if (($argc == 2) && ($argv[1] == 'delete')) {
logger('Delete expired items', LOGGER_DEBUG);
// physically remove anything that has been deleted for more than two months
$r = dba::p("SELECT `id` FROM `item` WHERE `deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY");
while ($row = dba::fetch($r)) {
dba::delete('item', array('id' => $row['id']));
}
dba::close($r);
logger('Delete expired items - done', LOGGER_DEBUG);
// make this optional as it could have a performance impact on large sites
if (intval(Config::get('system', 'optimize_items'))) {
q("OPTIMIZE TABLE `item`");
}
return;
} elseif (($argc == 2) && (intval($argv[1]) > 0)) {
$user = dba::select('user', array('uid', 'username', 'expire'), array('uid' => $argv[1]), array('limit' => 1));
if (DBM::is_result($user)) {
logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], LOGGER_DEBUG);
item_expire($user['uid'], $user['expire']);
logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', LOGGER_DEBUG);
}
return;
} elseif (($argc == 3) && ($argv[1] == 'hook') && is_array($a->hooks) && array_key_exists("expire", $a->hooks)) {
foreach ($a->hooks["expire"] as $hook) {
if ($hook[1] == $argv[2]) {
logger("Calling expire hook '" . $hook[1] . "'", LOGGER_DEBUG);
call_single_hook($a, $name, $hook, $data);
}
}
return;
}
logger('expire: start');
Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'expire', 'delete');
$r = dba::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
while ($row = dba::fetch($r)) {
logger('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', LOGGER_DEBUG);
Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'expire', (int)$row['uid']);
}
dba::close($r);
logger('expire: calling hooks');
if (is_array($a->hooks) && array_key_exists('expire', $a->hooks)) {
foreach ($a->hooks['expire'] as $hook) {
logger("Calling expire hook for '" . $hook[1] . "'", LOGGER_DEBUG);
Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'expire', 'hook', $hook[1]);
}
}
logger('expire: end');
return;
}

View File

@ -1,64 +0,0 @@
<?php
/**
* @file include/gprobe.php
*/
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Database\DBM;
use Friendica\Model\GlobalContact;
use Friendica\Network\Probe;
use Friendica\Protocol\PortableContact;
require_once 'include/datetime.php';
function gprobe_run(&$argv, &$argc)
{
if ($argc != 2) {
return;
}
$url = $argv[1];
$r = q(
"SELECT `id`, `url`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 1",
dbesc(normalise_link($url))
);
logger("gprobe start for ".normalise_link($url), LOGGER_DEBUG);
if (!DBM::is_result($r)) {
// Is it a DDoS attempt?
$urlparts = parse_url($url);
$result = Cache::get("gprobe:".$urlparts["host"]);
if (!is_null($result)) {
if (in_array($result["network"], array(NETWORK_FEED, NETWORK_PHANTOM))) {
logger("DDoS attempt detected for ".$urlparts["host"]." by ".$_SERVER["REMOTE_ADDR"].". server data: ".print_r($_SERVER, true), LOGGER_DEBUG);
return;
}
}
$arr = Probe::uri($url);
if (is_null($result)) {
Cache::set("gprobe:".$urlparts["host"], $arr);
}
if (!in_array($arr["network"], array(NETWORK_FEED, NETWORK_PHANTOM))) {
GlobalContact::update($arr);
}
$r = q(
"SELECT `id`, `url`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 1",
dbesc(normalise_link($url))
);
}
if (DBM::is_result($r)) {
// Check for accessibility and do a poco discovery
if (PortableContact::lastUpdated($r[0]['url'], true) && ($r[0]["network"] == NETWORK_DFRN)) {
PortableContact::loadWorker(0, 0, $r[0]['id'], str_replace('/profile/', '/poco/', $r[0]['url']));
}
}
logger("gprobe end for ".normalise_link($url), LOGGER_DEBUG);
return;
}

View File

@ -939,7 +939,7 @@ function zrl_init(App $a)
return; return;
} }
Worker::add(PRIORITY_LOW, 'gprobe', $tmp_str); Worker::add(PRIORITY_LOW, 'GProbe', $tmp_str);
$arr = array('zrl' => $tmp_str, 'url' => $a->cmd); $arr = array('zrl' => $tmp_str, 'url' => $a->cmd);
call_hooks('zrl_init', $arr); call_hooks('zrl_init', $arr);
} }

View File

@ -1138,7 +1138,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f
check_item_notification($current_post, $uid); check_item_notification($current_post, $uid);
if ($notify) { if ($notify) {
Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "notifier", $notify_type, $current_post); Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "Notifier", $notify_type, $current_post);
} }
return $current_post; return $current_post;
@ -1421,7 +1421,7 @@ function tag_deliver($uid, $item_id) {
); );
update_thread($item_id); update_thread($item_id);
Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), 'notifier', 'tgroup', $item_id); Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), 'Notifier', 'tgroup', $item_id);
} }
@ -2055,7 +2055,7 @@ function item_expire($uid, $days, $network = "", $force = false) {
drop_item($item['id'], false); drop_item($item['id'], false);
} }
Worker::add(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "notifier", "expire", $uid); Worker::add(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "Notifier", "expire", $uid);
} }
/// @TODO type-hint is array /// @TODO type-hint is array
@ -2077,7 +2077,7 @@ function drop_items($items) {
// multiple threads may have been deleted, send an expire notification // multiple threads may have been deleted, send an expire notification
if ($uid) { if ($uid) {
Worker::add(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "notifier", "expire", $uid); Worker::add(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "Notifier", "expire", $uid);
} }
} }
@ -2269,7 +2269,7 @@ function drop_item($id, $interactive = true) {
$drop_id = intval($item['id']); $drop_id = intval($item['id']);
$priority = ($interactive ? PRIORITY_HIGH : PRIORITY_LOW); $priority = ($interactive ? PRIORITY_HIGH : PRIORITY_LOW);
Worker::add(array('priority' => $priority, 'dont_fork' => true), "notifier", "drop", $drop_id); Worker::add(array('priority' => $priority, 'dont_fork' => true), "Notifier", "drop", $drop_id);
if (! $interactive) { if (! $interactive) {
return $owner; return $owner;

View File

@ -167,7 +167,7 @@ function do_like($item_id, $verb) {
); );
$like_item_id = $like_item['id']; $like_item_id = $like_item['id'];
Worker::add(PRIORITY_HIGH, "notifier", "like", $like_item_id); Worker::add(PRIORITY_HIGH, "Notifier", "like", $like_item_id);
if (!$event_verb_flag || $like_item['verb'] == $activity) { if (!$event_verb_flag || $like_item['verb'] == $activity) {
return true; return true;
@ -254,7 +254,7 @@ EOT;
call_hooks('post_local_end', $new_item); call_hooks('post_local_end', $new_item);
Worker::add(PRIORITY_HIGH, "notifier", "like", $new_item_id); Worker::add(PRIORITY_HIGH, "Notifier", "like", $new_item_id);
return true; return true;
} }

View File

@ -145,7 +145,7 @@ function send_message($recipient=0, $body='', $subject='', $replyto=''){
} }
if ($post_id) { if ($post_id) {
Worker::add(PRIORITY_HIGH, "notifier", "mail", $post_id); Worker::add(PRIORITY_HIGH, "Notifier", "mail", $post_id);
return intval($post_id); return intval($post_id);
} else { } else {
return -3; return -3;

View File

@ -1,617 +0,0 @@
<?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
* big problems when the process was killed or stalled during the delivery process.
* It now invokes separate queues that are delivering via delivery.php and pubsubpublish.php.
*/
/*
* The notifier is typically called with:
*
* Worker::add(PRIORITY_HIGH, "notifier", COMMAND, ITEM_ID);
*
* where COMMAND is one of the following:
*
* activity (in diaspora.php, dfrn_confirm.php, profiles.php)
* comment-import (in diaspora.php, items.php)
* comment-new (in item.php)
* drop (in diaspora.php, items.php, photos.php)
* edit_post (in item.php)
* event (in events.php)
* expire (in items.php)
* like (in like.php, poke.php)
* mail (in message.php)
* suggest (in fsuggest.php)
* tag (in photos.php, poke.php, tagger.php)
* tgroup (in items.php)
* wall-new (in photos.php, item.php)
* removeme (in Contact.php)
* relocate (in uimport.php)
*
* and ITEM_ID is the id of the item in the database that needs to be sent to others.
*/
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';
if ($argc < 3) {
return;
}
logger('notifier: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
$cmd = $argv[1];
switch($cmd) {
case 'mail':
default:
$item_id = intval($argv[2]);
if (! $item_id) {
return;
}
break;
}
$expire = false;
$mail = false;
$fsuggest = false;
$relocate = false;
$top_level = false;
$recipients = array();
$url_recipients = array();
$normal_mode = true;
if ($cmd === 'mail') {
$normal_mode = false;
$mail = true;
$message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
intval($item_id)
);
if (! count($message)) {
return;
}
$uid = $message[0]['uid'];
$recipients[] = $message[0]['contact-id'];
$item = $message[0];
} elseif ($cmd === 'expire') {
$normal_mode = false;
$expire = true;
$items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE",
intval($item_id)
);
$uid = $item_id;
$item_id = 0;
if (! count($items)) {
return;
}
} elseif ($cmd === 'suggest') {
$normal_mode = false;
$fsuggest = true;
$suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
intval($item_id)
);
if (! count($suggest)) {
return;
}
$uid = $suggest[0]['uid'];
$recipients[] = $suggest[0]['cid'];
$item = $suggest[0];
} elseif ($cmd === 'removeme') {
$r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
`user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
`user`.`page-flags`, `user`.`prvnets`, `user`.`account-type`, `user`.`guid`
FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
WHERE `contact`.`uid` = %d AND `contact`.`self` LIMIT 1",
intval($item_id));
if (!$r)
return;
$user = $r[0];
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", intval($item_id));
if (!$r)
return;
$self = $r[0];
$r = q("SELECT * FROM `contact` WHERE NOT `self` AND `uid` = %d", intval($item_id));
if (!$r) {
return;
}
require_once 'include/Contact.php';
foreach ($r as $contact) {
terminate_friendship($user, $self, $contact);
}
return;
} elseif ($cmd === 'relocate') {
$normal_mode = false;
$relocate = true;
$uid = $item_id;
$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",
intval($item_id)
);
if ((! DBM::is_result($r)) || (! intval($r[0]['parent']))) {
return;
}
$target_item = $r[0];
$parent_id = intval($r[0]['parent']);
$uid = $r[0]['uid'];
$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",
intval($parent_id)
);
if (! count($items)) {
return;
}
// avoid race condition with deleting entries
if ($items[0]['deleted']) {
foreach ($items as $item) {
$item['deleted'] = 1;
}
}
if ((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
logger('notifier: top level post');
$top_level = true;
}
}
$r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
`user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
`user`.`page-flags`, `user`.`prvnets`, `user`.`account-type`
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)) {
return;
}
$owner = $r[0];
$walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
// Should the post be transmitted to Diaspora?
$diaspora_delivery = true;
// If this is a public conversation, notify the feed hub
$public_message = true;
// 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);
require_once 'include/group.php';
$parent = $items[0];
$thr_parent = q("SELECT `network`, `author-link`, `owner-link` FROM `item` WHERE `uri` = '%s' AND `uid` = %d",
dbesc($target_item["thr-parent"]), intval($target_item["uid"]));
logger('GUID: '.$target_item["guid"].': Parent is '.$parent['network'].'. Thread parent is '.$thr_parent[0]['network'], LOGGER_DEBUG);
// This is IMPORTANT!!!!
// We will only send a "notify owner to relay" or followup message if the referenced post
// originated on our system by virtue of having our hostname somewhere
// in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
// if $parent['wall'] == 1 we will already have the parent message in our array
// and we will relay the whole lot.
// expire sends an entire group of expire messages and cannot be forwarded.
// However the conversation owner will be a part of the conversation and will
// be notified during this run.
// Other DFRN conversation members will be alerted during polled updates.
// Diaspora members currently are not notified of expirations, and other networks have
// either limited or no ability to process deletions. We should at least fix Diaspora
// by stringing togther an array of retractions and sending them onward.
$localhost = str_replace('www.','',$a->get_hostname());
if (strpos($localhost,':')) {
$localhost = substr($localhost,0,strpos($localhost,':'));
}
/**
*
* Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes
* have been known to cause runaway conditions which affected several servers, along with
* permissions issues.
*
*/
$relay_to_owner = false;
if (!$top_level && ($parent['wall'] == 0) && !$expire && (stristr($target_item['uri'],$localhost))) {
$relay_to_owner = true;
}
if (($cmd === 'uplink') && (intval($parent['forum_mode']) == 1) && !$top_level) {
$relay_to_owner = true;
}
// until the 'origin' flag has been in use for several months
// we will just use it as a fallback test
// later we will be able to use it as the primary test of whether or not to relay.
if (! $target_item['origin']) {
$relay_to_owner = false;
}
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
$followup = true;
$public_message = false; // not public
$conversant_str = dbesc($parent['contact-id']);
$recipients = array($parent['contact-id']);
$recipients_followup = array($parent['contact-id']);
//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 && ($thr_parent[0]['network'] == NETWORK_OSTATUS)) || ($parent['network'] == NETWORK_OSTATUS)) {
$push_notify = true;
if ($parent["network"] == NETWORK_OSTATUS) {
// Distribute the message to the DFRN contacts as if this wasn't a followup since OStatus can't relay comments
// Currently it is work at progress
$r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `network` = '%s' AND NOT `blocked` AND NOT `pending` AND NOT `archive`",
intval($uid),
dbesc(NETWORK_DFRN)
);
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;
logger('Distributing directly '.$target_item["guid"], LOGGER_DEBUG);
// don't send deletions onward for other people's stuff
if ($target_item['deleted'] && (! intval($target_item['wall']))) {
logger('notifier: ignoring delete notification for non-wall item');
return;
}
if ((strlen($parent['allow_cid']))
|| (strlen($parent['allow_gid']))
|| (strlen($parent['deny_cid']))
|| (strlen($parent['deny_gid']))) {
$public_message = false; // private recipients, not public
}
$allow_people = expand_acl($parent['allow_cid']);
$allow_groups = expand_groups(expand_acl($parent['allow_gid']),true);
$deny_people = expand_acl($parent['deny_cid']);
$deny_groups = expand_groups(expand_acl($parent['deny_gid']));
// if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
// a delivery fork. private groups (forum_mode == 2) do not uplink
if ((intval($parent['forum_mode']) == 1) && (! $top_level) && ($cmd !== 'uplink')) {
Worker::add($a->queue['priority'], 'notifier', 'uplink', $item_id);
}
$conversants = array();
foreach ($items as $item) {
$recipients[] = $item['contact-id'];
$conversants[] = $item['contact-id'];
// pull out additional tagged people to notify (if public message)
if ($public_message && strlen($item['inform'])) {
$people = explode(',',$item['inform']);
foreach ($people as $person) {
if (substr($person,0,4) === 'cid:') {
$recipients[] = intval(substr($person,4));
$conversants[] = intval(substr($person,4));
} else {
$url_recipients[] = substr($person,4);
}
}
}
}
if (count($url_recipients))
logger('notifier: '.$target_item["guid"].' url_recipients ' . print_r($url_recipients,true));
$conversants = array_unique($conversants);
$recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
$deny = array_unique(array_merge($deny_people,$deny_groups));
$recipients = array_diff($recipients,$deny);
$conversant_str = dbesc(implode(', ',$conversants));
}
// 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 && ($thr_parent[0]['network'] == NETWORK_OSTATUS)) || ($parent['network'] == NETWORK_OSTATUS)) {
$diaspora_delivery = false;
logger('Some parent is OStatus for '.$target_item["guid"]." - Author: ".$thr_parent[0]['author-link']." - Owner: ".$thr_parent[0]['owner-link'], LOGGER_DEBUG);
// Send a salmon to the parent author
$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)) {
$probed_contact = $r[0];
} else {
$probed_contact = Probe::uri($thr_parent[0]['author-link']);
}
if ($probed_contact["notify"] != "") {
logger('Notify parent author '.$probed_contact["url"].': '.$probed_contact["notify"]);
$url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
}
// Send a salmon to the parent owner
$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)) {
$probed_contact = $r[0];
} else {
$probed_contact = Probe::uri($thr_parent[0]['owner-link']);
}
if ($probed_contact["notify"] != "") {
logger('Notify parent owner '.$probed_contact["url"].': '.$probed_contact["notify"]);
$url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
}
// Send a salmon notification to every person we mentioned in the post
$arr = explode(',',$target_item['tag']);
foreach ($arr as $x) {
//logger('Checking tag '.$x, LOGGER_DEBUG);
$matches = null;
if (preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
$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"];
}
}
}
// It only makes sense to distribute answers to OStatus messages to Friendica and OStatus - but not Diaspora
$sql_extra = " AND `network` IN ('".NETWORK_OSTATUS."', '".NETWORK_DFRN."')";
} else {
$sql_extra = " AND `network` IN ('".NETWORK_OSTATUS."', '".NETWORK_DFRN."', '".NETWORK_DIASPORA."', '".NETWORK_MAIL."', '".NETWORK_MAIL2."')";
}
} else {
$public_message = false;
}
// If this is a public message and pubmail is set on the parent, include all your email contacts
$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']))
&& (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid']))
&& (intval($target_item['pubmail']))) {
$r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
intval($uid),
dbesc(NETWORK_MAIL)
);
if (DBM::is_result($r)) {
foreach ($r as $rr) {
$recipients[] = $rr['id'];
}
}
}
}
if ($followup) {
$recip_str = implode(', ', $recipients_followup);
} else {
$recip_str = implode(', ', $recipients);
}
if ($relocate) {
$r = $recipients_relocate;
} else {
$r = q("SELECT `id`, `url`, `network`, `self` FROM `contact`
WHERE `id` IN (%s) AND NOT `blocked` AND NOT `pending` AND NOT `archive`".$sql_extra,
dbesc($recip_str)
);
}
// delivery loop
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);
Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'delivery', $cmd, $item_id, (int)$contact['id']);
}
}
// send salmon slaps to mentioned remote tags (@foo@example.com) in OStatus posts
// 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 (!Config::get('system','dfrn_only')) {
foreach ($url_recipients as $url) {
if ($url) {
logger('notifier: urldelivery: ' . $url);
$deliver_status = slapper($owner,$url,$slap);
/// @TODO Redeliver/queue these items on failure, though there is no contact record
}
}
}
}
if ($public_message) {
$r0 = array();
$r1 = array();
if ($diaspora_delivery) {
if (!$followup) {
$r0 = Diaspora::relay_list();
}
$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 `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)
);
}
$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",
dbesc(NETWORK_DFRN),
dbesc(NETWORK_MAIL2),
intval($owner['uid']),
intval(CONTACT_IS_SHARING)
);
$r = array_merge($r2,$r1,$r0);
if (DBM::is_result($r)) {
logger('pubdeliver '.$target_item["guid"].': '.print_r($r,true), LOGGER_DEBUG);
foreach ($r as $rr) {
// except for Diaspora batch jobs
// Don't deliver to folks who have already been delivered to
if (($rr['network'] !== NETWORK_DIASPORA) && (in_array($rr['id'],$conversants))) {
logger('notifier: already delivered id=' . $rr['id']);
continue;
}
if ((! $mail) && (! $fsuggest) && (! $followup)) {
logger('notifier: delivery agent: '.$rr['name'].' '.$rr['id'].' '.$rr['network'].' '.$target_item["guid"]);
Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'delivery', $cmd, $item_id, (int)$rr['id']);
}
}
}
$push_notify = true;
}
// Notify PuSH subscribers (Used for OStatus distribution of regular posts)
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']));
logger('Activating internal PuSH for item '.$item_id, LOGGER_DEBUG);
// Handling the pubsubhubbub requests
Worker::add(array('priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true),
'pubsubpublish');
}
logger('notifier: calling hooks', LOGGER_DEBUG);
if ($normal_mode) {
call_hooks('notifier_normal',$target_item);
}
call_hooks('notifier_end',$target_item);
return;
}

View File

@ -1,12 +0,0 @@
<?php
use Friendica\Protocol\Diaspora;
function profile_update_run(&$argv, &$argc) {
if ($argc != 2) {
return;
}
$uid = intval($argv[1]);
Diaspora::send_profile($uid);
}

View File

@ -1,94 +0,0 @@
<?php
/**
* @file include/pubsubpublish.php
*/
use Friendica\App;
use Friendica\Core\System;
use Friendica\Core\Config;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Protocol\OStatus;
require_once 'include/items.php';
function pubsubpublish_run(&$argv, &$argc)
{
global $a;
if ($argc > 1) {
$pubsubpublish_id = intval($argv[1]);
} else {
// We'll push to each subscriber that has push > 0,
// i.e. there has been an update (set in notifier.php).
$r = q("SELECT `id`, `callback_url` FROM `push_subscriber` WHERE `push` > 0 ORDER BY `last_update` DESC");
foreach ($r as $rr) {
logger("Publish feed to ".$rr["callback_url"], LOGGER_DEBUG);
Worker::add(array('priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true),
'pubsubpublish', (int)$rr["id"]);
}
}
handle_pubsubhubbub($pubsubpublish_id);
return;
}
function handle_pubsubhubbub($id) {
global $a;
$r = q("SELECT * FROM `push_subscriber` WHERE `id` = %d", intval($id));
if (!DBM::is_result($r)) {
return;
}
$rr = $r[0];
/// @todo Check server status with PortableContact::checkServer()
// Before this can be done we need a way to safely detect the server url.
logger("Generate feed of user ".$rr['nickname']." to ".$rr['callback_url']." - last updated ".$rr['last_update'], LOGGER_DEBUG);
$last_update = $rr['last_update'];
$params = OStatus::feed($a, $rr['nickname'], $last_update);
if (!$params) {
return;
}
$hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
$headers = array("Content-type: application/atom+xml",
sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
System::baseUrl().'/pubsubhubbub/'.$rr['nickname'],
$rr['topic']),
"X-Hub-Signature: sha1=".$hmac_sig);
logger('POST '.print_r($headers, true)."\n".$params, LOGGER_DEBUG);
post_url($rr['callback_url'], $params, $headers);
$ret = $a->get_curl_code();
if ($ret >= 200 && $ret <= 299) {
logger('successfully pushed to '.$rr['callback_url']);
// set last_update to the "created" date of the last item, and reset push=0
q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' WHERE id = %d",
dbesc($last_update),
intval($rr['id']));
} else {
logger('error when pushing to '.$rr['callback_url'].' HTTP: '.$ret);
// we use the push variable also as a counter, if we failed we
// increment this until some upper limit where we give up
$new_push = intval($rr['push']) + 1;
if ($new_push > 30) // OK, let's give up
$new_push = 0;
q("UPDATE `push_subscriber` SET `push` = %d WHERE id = %d",
$new_push,
intval($rr['id']));
}
}

View File

@ -1,191 +0,0 @@
<?php
/**
* @file include/queue.php
*/
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\DFRN;
use Friendica\Protocol\PortableContact;
require_once 'include/queue_fn.php';
require_once 'include/datetime.php';
require_once 'include/items.php';
require_once 'include/bbcode.php';
function queue_run(&$argv, &$argc)
{
global $a;
if ($argc > 1) {
$queue_id = intval($argv[1]);
} else {
$queue_id = 0;
}
$cachekey_deadguy = 'queue_run:deadguy:';
$cachekey_server = 'queue_run:server:';
if (!$queue_id) {
logger('queue: start');
// Handling the pubsubhubbub requests
Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), 'pubsubpublish');
$r = q(
"SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY"
);
if (DBM::is_result($r)) {
foreach ($r as $rr) {
logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
logger('Expired queue data: ' . $rr['content'], LOGGER_DATA);
}
q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
}
/*
* For the first 12 hours we'll try to deliver every 15 minutes
* After that, we'll only attempt delivery once per hour.
*/
$r = q("SELECT `id` FROM `queue` WHERE ((`created` > UTC_TIMESTAMP() - INTERVAL 12 HOUR AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE) OR (`last` < UTC_TIMESTAMP() - INTERVAL 1 HOUR)) ORDER BY `cid`, `created`");
call_hooks('queue_predeliver', $a, $r);
if (DBM::is_result($r)) {
foreach ($r as $q_item) {
logger('Call queue for id '.$q_item['id']);
Worker::add(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "queue", (int)$q_item['id']);
}
}
return;
}
// delivering
require_once 'include/salmon.php';
$r = q(
"SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
intval($queue_id)
);
if (!DBM::is_result($r)) {
return;
}
$q_item = $r[0];
$c = q(
"SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
intval($q_item['cid'])
);
if (!DBM::is_result($c)) {
remove_queue_item($q_item['id']);
return;
}
$dead = Cache::get($cachekey_deadguy.$c[0]['notify']);
if (!is_null($dead) && $dead) {
logger('queue: skipping known dead url: '.$c[0]['notify']);
update_queue_time($q_item['id']);
return;
}
$server = PortableContact::detectServer($c[0]['url']);
if ($server != "") {
$vital = Cache::get($cachekey_server.$server);
if (is_null($vital)) {
logger("Check server ".$server." (".$c[0]["network"].")");
$vital = PortableContact::checkServer($server, $c[0]["network"], true);
Cache::set($cachekey_server.$server, $vital, CACHE_QUARTER_HOUR);
}
if (!is_null($vital) && !$vital) {
logger('queue: skipping dead server: '.$server);
update_queue_time($q_item['id']);
return;
}
}
$u = q(
"SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
FROM `user` WHERE `uid` = %d LIMIT 1",
intval($c[0]['uid'])
);
if (!DBM::is_result($u)) {
remove_queue_item($q_item['id']);
return;
}
$data = $q_item['content'];
$public = $q_item['batch'];
$contact = $c[0];
$owner = $u[0];
$deliver_status = 0;
switch ($contact['network']) {
case NETWORK_DFRN:
logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = DFRN::deliver($owner, $contact, $data);
if ($deliver_status == (-1)) {
update_queue_time($q_item['id']);
Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
} else {
remove_queue_item($q_item['id']);
}
break;
case NETWORK_OSTATUS:
if ($contact['notify']) {
logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = slapper($owner, $contact['notify'], $data);
if ($deliver_status == (-1)) {
update_queue_time($q_item['id']);
Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
} else {
remove_queue_item($q_item['id']);
}
}
break;
case NETWORK_DIASPORA:
if ($contact['notify']) {
logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = Diaspora::transmit($owner, $contact, $data, $public, true);
if ($deliver_status == (-1)) {
update_queue_time($q_item['id']);
Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
} else {
remove_queue_item($q_item['id']);
}
}
break;
default:
$params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
call_hooks('queue_deliver', $a, $params);
if ($params['result']) {
remove_queue_item($q_item['id']);
} else {
update_queue_time($q_item['id']);
}
break;
}
logger('Deliver status '.(int)$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
return;
}

View File

@ -286,7 +286,7 @@ function import_account(App $a, $file) {
} }
// send relocate messages // send relocate messages
Worker::add(PRIORITY_HIGH, 'notifier', 'relocate', $newuid); Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
info(t("Done. You can now login with your username and password")); info(t("Done. You can now login with your username and password"));
goaway(System::baseUrl() . "/login"); goaway(System::baseUrl() . "/login");

View File

@ -778,7 +778,7 @@ function admin_page_site_post(App $a) {
$users = q("SELECT `uid` FROM `user` WHERE `account_removed` = 0 AND `account_expired` = 0"); $users = q("SELECT `uid` FROM `user` WHERE `account_removed` = 0 AND `account_expired` = 0");
foreach ($users as $user) { foreach ($users as $user) {
Worker::add(PRIORITY_HIGH, 'notifier', 'relocate', $user['uid']); Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $user['uid']);
} }
info("Relocation started. Could take a while to complete."); info("Relocation started. Could take a while to complete.");

View File

@ -498,7 +498,7 @@ function dfrn_confirm_post(App $a, $handsfree = null) {
$i = item_store($arr); $i = item_store($arr);
if($i) if($i)
Worker::add(PRIORITY_HIGH, "notifier", "activity", $i); Worker::add(PRIORITY_HIGH, "Notifier", "activity", $i);
} }
} }
} }
@ -800,7 +800,7 @@ function dfrn_confirm_post(App $a, $handsfree = null) {
$i = item_store($arr); $i = item_store($arr);
if($i) if($i)
Worker::add(PRIORITY_HIGH, "notifier", "activity", $i); Worker::add(PRIORITY_HIGH, "Notifier", "activity", $i);
} }
} }

View File

@ -180,7 +180,7 @@ function events_post(App $a) {
$item_id = event_store($datarray); $item_id = event_store($datarray);
if (! $cid) { if (! $cid) {
Worker::add(PRIORITY_HIGH, "notifier", "event", $item_id); Worker::add(PRIORITY_HIGH, "Notifier", "event", $item_id);
} }
goaway($_SESSION['return_url']); goaway($_SESSION['return_url']);

View File

@ -61,7 +61,7 @@ function fsuggest_post(App $a) {
intval($fsuggest_id), intval($fsuggest_id),
intval(local_user()) intval(local_user())
); );
Worker::add(PRIORITY_HIGH, 'notifier', 'suggest', $fsuggest_id); Worker::add(PRIORITY_HIGH, 'Notifier', 'suggest', $fsuggest_id);
} }
info( t('Friend suggestion sent.') . EOL); info( t('Friend suggestion sent.') . EOL);

View File

@ -834,7 +834,7 @@ function item_post(App $a) {
// update filetags in pconfig // update filetags in pconfig
file_tag_update_pconfig($uid,$categories_old,$categories_new,'category'); file_tag_update_pconfig($uid,$categories_old,$categories_new,'category');
Worker::add(PRIORITY_HIGH, "notifier", 'edit_post', $post_id); Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $post_id);
if ((x($_REQUEST, 'return')) && strlen($return_path)) { if ((x($_REQUEST, 'return')) && strlen($return_path)) {
logger('return: ' . $return_path); logger('return: ' . $return_path);
goaway($return_path); goaway($return_path);
@ -1064,10 +1064,10 @@ function item_post(App $a) {
// We now do it in the background to save some time. // We now do it in the background to save some time.
// This is important in interactive environments like the frontend or the API. // This is important in interactive environments like the frontend or the API.
// We don't fork a new process since this is done anyway with the following command // We don't fork a new process since this is done anyway with the following command
Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "create_shadowentry", $post_id); Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "CreateShadowEntry", $post_id);
// Call the background process that is delivering the item to the receivers // Call the background process that is delivering the item to the receivers
Worker::add(PRIORITY_HIGH, "notifier", $notify_type, $post_id); Worker::add(PRIORITY_HIGH, "Notifier", $notify_type, $post_id);
logger('post_complete'); logger('post_complete');

View File

@ -100,13 +100,13 @@ function mood_init(App $a) {
intval($uid), intval($uid),
intval($item_id) intval($item_id)
); );
Worker::add(PRIORITY_HIGH, "notifier", "tag", $item_id); Worker::add(PRIORITY_HIGH, "Notifier", "tag", $item_id);
} }
call_hooks('post_local_end', $arr); call_hooks('post_local_end', $arr);
Worker::add(PRIORITY_HIGH, "notifier", "like", $post_id); Worker::add(PRIORITY_HIGH, "Notifier", "like", $post_id);
return; return;
} }

View File

@ -306,7 +306,7 @@ function photos_post(App $a) {
// send the notification upstream/downstream as the case may be // send the notification upstream/downstream as the case may be
if ($rr['visible']) { if ($rr['visible']) {
Worker::add(PRIORITY_HIGH, "notifier", "drop", $drop_id); Worker::add(PRIORITY_HIGH, "Notifier", "drop", $drop_id);
} }
} }
} }
@ -383,7 +383,7 @@ function photos_post(App $a) {
photo_albums($page_owner_uid, true); photo_albums($page_owner_uid, true);
if ($i[0]['visible']) { if ($i[0]['visible']) {
Worker::add(PRIORITY_HIGH, "notifier", "drop", $drop_id); Worker::add(PRIORITY_HIGH, "Notifier", "drop", $drop_id);
} }
} }
} }
@ -731,7 +731,7 @@ function photos_post(App $a) {
$item_id = item_store($arr); $item_id = item_store($arr);
if ($item_id) { if ($item_id) {
Worker::add(PRIORITY_HIGH, "notifier", "tag", $item_id); Worker::add(PRIORITY_HIGH, "Notifier", "tag", $item_id);
} }
} }
} }
@ -936,7 +936,7 @@ function photos_post(App $a) {
photo_albums($page_owner_uid, true); photo_albums($page_owner_uid, true);
if ($visible) { if ($visible) {
Worker::add(PRIORITY_HIGH, "notifier", 'wall-new', $item_id); Worker::add(PRIORITY_HIGH, "Notifier", 'wall-new', $item_id);
} }
call_hooks('photo_post_end',intval($item_id)); call_hooks('photo_post_end',intval($item_id));

View File

@ -139,13 +139,13 @@ function poke_init(App $a) {
// intval($uid), // intval($uid),
// intval($item_id) // intval($item_id)
//); //);
Worker::add(PRIORITY_HIGH, "notifier", "tag", $item_id); Worker::add(PRIORITY_HIGH, "Notifier", "tag", $item_id);
} }
call_hooks('post_local_end', $arr); call_hooks('post_local_end', $arr);
Worker::add(PRIORITY_HIGH, "notifier", "like", $post_id); Worker::add(PRIORITY_HIGH, "Notifier", "like", $post_id);
return; return;
} }

View File

@ -135,7 +135,7 @@ function profile_photo_post(App $a) {
Worker::add(PRIORITY_LOW, "Directory", $url); Worker::add(PRIORITY_LOW, "Directory", $url);
} }
Worker::add(PRIORITY_LOW, 'profile_update', local_user()); Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
} else { } else {
notice( t('Unable to process image') . EOL); notice( t('Unable to process image') . EOL);
} }

View File

@ -506,7 +506,7 @@ function profiles_post(App $a) {
Worker::add(PRIORITY_LOW, "Directory", $url); Worker::add(PRIORITY_LOW, "Directory", $url);
} }
Worker::add(PRIORITY_LOW, 'profile_update', local_user()); Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
// Update the global contact for the user // Update the global contact for the user
GlobalContact::updateForUser(local_user()); GlobalContact::updateForUser(local_user());
@ -600,7 +600,7 @@ function profile_activity($changed, $value) {
$i = item_store($arr); $i = item_store($arr);
if ($i) { if ($i) {
Worker::add(PRIORITY_HIGH, "notifier", "activity", $i); Worker::add(PRIORITY_HIGH, "Notifier", "activity", $i);
} }
} }

View File

@ -360,7 +360,7 @@ function settings_post(App $a) {
check_form_security_token_redirectOnErr('/settings', 'settings'); check_form_security_token_redirectOnErr('/settings', 'settings');
if (x($_POST,'resend_relocate')) { if (x($_POST,'resend_relocate')) {
Worker::add(PRIORITY_HIGH, 'notifier', 'relocate', local_user()); Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', local_user());
info(t("Relocate message has been send to your contacts")); info(t("Relocate message has been send to your contacts"));
goaway('settings'); goaway('settings');
} }
@ -651,7 +651,7 @@ function settings_post(App $a) {
} }
} }
Worker::add(PRIORITY_LOW, 'profile_update', local_user()); Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
// Update the global contact for the user // Update the global contact for the user
GlobalContact::updateForUser(local_user()); GlobalContact::updateForUser(local_user());

View File

@ -216,7 +216,7 @@ EOT;
call_hooks('post_local_end', $arr); call_hooks('post_local_end', $arr);
Worker::add(PRIORITY_HIGH, "notifier", "tag", $post_id); Worker::add(PRIORITY_HIGH, "Notifier", "tag", $post_id);
killme(); killme();

View File

@ -173,7 +173,7 @@ function videos_post(App $a) {
$drop_id = intval($i[0]['id']); $drop_id = intval($i[0]['id']);
if ($i[0]['visible']) { if ($i[0]['visible']) {
Worker::add(PRIORITY_HIGH, "notifier", "drop", $drop_id); Worker::add(PRIORITY_HIGH, "Notifier", "drop", $drop_id);
} }
} }
} }

View File

@ -922,8 +922,8 @@ class Worker {
* @param (integer|array) priority or parameter array, strings are deprecated and are ignored * @param (integer|array) priority or parameter array, strings are deprecated and are ignored
* *
* next args are passed as $cmd command line * next args are passed as $cmd command line
* or: Worker::add(PRIORITY_HIGH, "notifier", "drop", $drop_id); * or: Worker::add(PRIORITY_HIGH, "Notifier", "drop", $drop_id);
* or: Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "create_shadowentry", $post_id); * or: Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "CreateShadowEntry", $post_id);
* *
* @note $cmd and string args are surrounded with "" * @note $cmd and string args are surrounded with ""
* *

View File

@ -688,7 +688,7 @@ class GlobalContact
if ($doprobing) { if ($doprobing) {
logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG); logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG);
Worker::add(PRIORITY_LOW, 'gprobe', $contact["url"]); Worker::add(PRIORITY_LOW, 'GProbe', $contact["url"]);
} }
return $gcontact_id; return $gcontact_id;

View File

@ -2106,7 +2106,7 @@ class DFRN
$changed = true; $changed = true;
if ($entrytype == DFRN_REPLY_RC) { if ($entrytype == DFRN_REPLY_RC) {
Worker::add(PRIORITY_HIGH, "notifier", "comment-import", $current["id"]); Worker::add(PRIORITY_HIGH, "Notifier", "comment-import", $current["id"]);
} }
} }
@ -2752,7 +2752,7 @@ class DFRN
if ($posted_id && $parent && ($entrytype == DFRN_REPLY_RC)) { if ($posted_id && $parent && ($entrytype == DFRN_REPLY_RC)) {
logger("Notifying followers about comment ".$posted_id, LOGGER_DEBUG); logger("Notifying followers about comment ".$posted_id, LOGGER_DEBUG);
Worker::add(PRIORITY_HIGH, "notifier", "comment-import", $posted_id); Worker::add(PRIORITY_HIGH, "Notifier", "comment-import", $posted_id);
} }
return true; return true;
@ -2940,7 +2940,7 @@ class DFRN
if ($entrytype == DFRN_REPLY_RC) { if ($entrytype == DFRN_REPLY_RC) {
logger("Notifying followers about deletion of post " . $item["id"], LOGGER_DEBUG); logger("Notifying followers about deletion of post " . $item["id"], LOGGER_DEBUG);
Worker::add(PRIORITY_HIGH, "notifier", "drop", $item["id"]); Worker::add(PRIORITY_HIGH, "Notifier", "drop", $item["id"]);
} }
} }
} }

View File

@ -1696,7 +1696,7 @@ class Diaspora
dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($data))); dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($data)));
// notify others // notify others
Worker::add(PRIORITY_HIGH, "notifier", "comment-import", $message_id); Worker::add(PRIORITY_HIGH, "Notifier", "comment-import", $message_id);
} }
return true; return true;
@ -2024,7 +2024,7 @@ class Diaspora
dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($data))); dba::insert('sign', array('iid' => $message_id, 'signed_text' => json_encode($data)));
// notify others // notify others
Worker::add(PRIORITY_HIGH, "notifier", "comment-import", $message_id); Worker::add(PRIORITY_HIGH, "Notifier", "comment-import", $message_id);
} }
return true; return true;
@ -2320,7 +2320,7 @@ class Diaspora
$i = item_store($arr); $i = item_store($arr);
if ($i) { if ($i) {
Worker::add(PRIORITY_HIGH, "notifier", "activity", $i); Worker::add(PRIORITY_HIGH, "Notifier", "activity", $i);
} }
} }
} }
@ -2768,7 +2768,7 @@ class Diaspora
// Now check if the retraction needs to be relayed by us // Now check if the retraction needs to be relayed by us
if ($parent["origin"]) { if ($parent["origin"]) {
// notify others // notify others
Worker::add(PRIORITY_HIGH, "notifier", "drop", $item["id"]); Worker::add(PRIORITY_HIGH, "Notifier", "drop", $item["id"]);
} }
} }

View File

@ -0,0 +1,21 @@
<?php
/**
* @file src/Worker/CreateShadowEntry.php
* @brief This script creates posts with UID = 0 for a given public post.
*
* This script is started from mod/item.php to save some time when doing a post.
*/
namespace Friendica\Worker;
require_once("include/threads.php");
class CreateShadowEntry {
public static function execute($message_id = 0) {
if (empty($message_id)) {
return;
}
add_shadow_entry($message_id);
}
}

View File

@ -39,7 +39,7 @@ Class Cron {
logger('cron: start'); logger('cron: start');
// run queue delivery process in the background // run queue delivery process in the background
Worker::add(PRIORITY_NEGLIGIBLE, "queue"); Worker::add(PRIORITY_NEGLIGIBLE, "Queue");
// run the process to discover global contacts in the background // run the process to discover global contacts in the background
Worker::add(PRIORITY_LOW, "DiscoverPoCo"); Worker::add(PRIORITY_LOW, "DiscoverPoCo");
@ -79,7 +79,7 @@ Class Cron {
Config::set('system', 'last_expire_day', $d2); Config::set('system', 'last_expire_day', $d2);
Worker::add(PRIORITY_LOW, 'expire'); Worker::add(PRIORITY_LOW, 'Expire');
Worker::add(PRIORITY_MEDIUM, 'DBClean'); Worker::add(PRIORITY_MEDIUM, 'DBClean');

View File

@ -1,7 +1,10 @@
<?php <?php
/** /**
* @file include/delivery.php * @file src/Worker/Delivery.php
*/ */
namespace Friendica\Worker;
use Friendica\App; use Friendica\App;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Core\Config; use Friendica\Core\Config;
@ -11,31 +14,18 @@ use Friendica\Protocol\DFRN;
require_once 'include/queue_fn.php'; require_once 'include/queue_fn.php';
require_once 'include/html2plain.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';
function delivery_run(&$argv, &$argc){ /// @todo This is some ugly code that needs to be split into several methods
global $a;
require_once 'include/datetime.php'; class Delivery {
require_once 'include/items.php'; public static function execute($cmd, $item_id, $contact_id) {
require_once 'include/bbcode.php'; global $a;
require_once 'include/email.php';
if ($argc < 3) { logger('delivery: invoked: '.$cmd.': '.$item_id.' to '.$contact_id, LOGGER_DEBUG);
return;
}
logger('delivery: invoked: '. print_r($argv,true), LOGGER_DEBUG);
$cmd = $argv[1];
$item_id = intval($argv[2]);
for ($x = 3; $x < $argc; $x ++) {
$contact_id = intval($argv[$x]);
if (!$item_id || !$contact_id) {
continue;
}
$expire = false; $expire = false;
$mail = false; $mail = false;
@ -72,7 +62,7 @@ function delivery_run(&$argv, &$argc){
$uid = $item_id; $uid = $item_id;
$item_id = 0; $item_id = 0;
if (!count($items)) { if (!count($items)) {
continue; return;
} }
} elseif ($cmd === 'suggest') { } elseif ($cmd === 'suggest') {
$normal_mode = false; $normal_mode = false;
@ -97,8 +87,8 @@ function delivery_run(&$argv, &$argc){
intval($item_id) intval($item_id)
); );
if ((!DBM::is_result($r)) || (!intval($r[0]['parent']))) { if (!DBM::is_result($r) || !intval($r[0]['parent'])) {
continue; return;
} }
$target_item = $r[0]; $target_item = $r[0];
@ -112,7 +102,7 @@ function delivery_run(&$argv, &$argc){
); );
if (!count($items)) { if (!count($items)) {
continue; return;
} }
$icontacts = null; $icontacts = null;
@ -129,7 +119,7 @@ function delivery_run(&$argv, &$argc){
); );
} }
if ( !($icontacts && count($icontacts))) { if ( !($icontacts && count($icontacts))) {
continue; return;
} }
// avoid race condition with deleting entries // avoid race condition with deleting entries
@ -158,12 +148,12 @@ function delivery_run(&$argv, &$argc){
); );
if (!DBM::is_result($r)) { if (!DBM::is_result($r)) {
continue; return;
} }
$owner = $r[0]; $owner = $r[0];
$walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false); $walltowall = (($top_level && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
$public_message = true; $public_message = true;
@ -232,7 +222,7 @@ function delivery_run(&$argv, &$argc){
$contact = $r[0]; $contact = $r[0];
} }
if ($contact['self']) { if ($contact['self']) {
continue; return;
} }
$deliver_status = 0; $deliver_status = 0;
@ -255,7 +245,7 @@ function delivery_run(&$argv, &$argc){
$msgitems = array(); $msgitems = array();
foreach ($items as $item) { // there is only one item foreach ($items as $item) { // there is only one item
if (!$item['parent']) { if (!$item['parent']) {
continue; return;
} }
if ($item['id'] == $item_id) { if ($item['id'] == $item_id) {
logger('followup: item: '. print_r($item,true), LOGGER_DATA); logger('followup: item: '. print_r($item,true), LOGGER_DATA);
@ -267,17 +257,17 @@ function delivery_run(&$argv, &$argc){
$msgitems = array(); $msgitems = array();
foreach ($items as $item) { foreach ($items as $item) {
if (!$item['parent']) { if (!$item['parent']) {
continue; return;
} }
// private emails may be in included in public conversations. Filter them. // private emails may be in included in public conversations. Filter them.
if ($public_message && $item['private']) { if ($public_message && $item['private']) {
continue; return;
} }
$item_contact = get_item_contact($item,$icontacts); $item_contact = get_item_contact($item,$icontacts);
if (!$item_contact) { if (!$item_contact) {
continue; return;
} }
if ($normal_mode) { if ($normal_mode) {
@ -404,7 +394,7 @@ function delivery_run(&$argv, &$argc){
$it = $items[0]; $it = $items[0];
} else { } else {
$r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($argv[2]), intval($item_id),
intval($uid) intval($uid)
); );
if (DBM::is_result($r)) if (DBM::is_result($r))
@ -490,7 +480,7 @@ function delivery_run(&$argv, &$argc){
logger('delivery: diaspora batch deliver: '.$loc); 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; break;
if ($mail) { if ($mail) {
@ -536,7 +526,7 @@ function delivery_run(&$argv, &$argc){
default: default:
break; break;
} }
}
return; return;
}
} }

85
src/Worker/Expire.php Normal file
View File

@ -0,0 +1,85 @@
<?php
/**
* @file src/Worker/Expire.php
* @brief Expires old item entries
*/
namespace Friendica\Worker;
use Friendica\Core\Config;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use dba;
class Expire {
public static function execute($param = '', $hook_name = '') {
global $a;
require_once('include/datetime.php');
require_once('include/items.php');
require_once('include/Contact.php');
load_hooks();
if ($param == 'delete') {
logger('Delete expired items', LOGGER_DEBUG);
// physically remove anything that has been deleted for more than two months
$r = dba::p("SELECT `id` FROM `item` WHERE `deleted` AND `changed` < UTC_TIMESTAMP() - INTERVAL 60 DAY");
while ($row = dba::fetch($r)) {
dba::delete('item', array('id' => $row['id']));
}
dba::close($r);
logger('Delete expired items - done', LOGGER_DEBUG);
// make this optional as it could have a performance impact on large sites
if (intval(Config::get('system', 'optimize_items'))) {
dba::e("OPTIMIZE TABLE `item`");
}
return;
} elseif (intval($param) > 0) {
$user = dba::select('user', array('uid', 'username', 'expire'), array('uid' => $param), array('limit' => 1));
if (DBM::is_result($user)) {
logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - interval: '.$user['expire'], LOGGER_DEBUG);
item_expire($user['uid'], $user['expire']);
logger('Expire items for user '.$user['uid'].' ('.$user['username'].') - done ', LOGGER_DEBUG);
}
return;
} elseif (!empty($hook_name) && ($param == 'hook') && is_array($a->hooks) && array_key_exists("expire", $a->hooks)) {
foreach ($a->hooks["expire"] as $hook) {
if ($hook[1] == $hook_name) {
logger("Calling expire hook '" . $hook[1] . "'", LOGGER_DEBUG);
call_single_hook($a, $name, $hook, $data);
}
}
return;
}
logger('expire: start');
Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'Expire', 'delete');
$r = dba::p("SELECT `uid`, `username` FROM `user` WHERE `expire` != 0");
while ($row = dba::fetch($r)) {
logger('Calling expiry for user '.$row['uid'].' ('.$row['username'].')', LOGGER_DEBUG);
Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'Expire', (int)$row['uid']);
}
dba::close($r);
logger('expire: calling hooks');
if (is_array($a->hooks) && array_key_exists('expire', $a->hooks)) {
foreach ($a->hooks['expire'] as $hook) {
logger("Calling expire hook for '" . $hook[1] . "'", LOGGER_DEBUG);
Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'Expire', 'hook', $hook[1]);
}
}
logger('expire: end');
return;
}
}

65
src/Worker/GProbe.php Normal file
View File

@ -0,0 +1,65 @@
<?php
/**
* @file include/gprobe.php
*/
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Database\DBM;
use Friendica\Model\GlobalContact;
use Friendica\Network\Probe;
use Friendica\Protocol\PortableContact;
require_once 'include/datetime.php';
class GProbe {
function execute($url = '')
{
if (empty($url)) {
return;
}
$r = q(
"SELECT `id`, `url`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 1",
dbesc(normalise_link($url))
);
logger("gprobe start for ".normalise_link($url), LOGGER_DEBUG);
if (!DBM::is_result($r)) {
// Is it a DDoS attempt?
$urlparts = parse_url($url);
$result = Cache::get("gprobe:".$urlparts["host"]);
if (!is_null($result)) {
if (in_array($result["network"], array(NETWORK_FEED, NETWORK_PHANTOM))) {
logger("DDoS attempt detected for ".$urlparts["host"]." by ".$_SERVER["REMOTE_ADDR"].". server data: ".print_r($_SERVER, true), LOGGER_DEBUG);
return;
}
}
$arr = Probe::uri($url);
if (is_null($result)) {
Cache::set("gprobe:".$urlparts["host"], $arr);
}
if (!in_array($arr["network"], array(NETWORK_FEED, NETWORK_PHANTOM))) {
GlobalContact::update($arr);
}
$r = q(
"SELECT `id`, `url`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 1",
dbesc(normalise_link($url))
);
}
if (DBM::is_result($r)) {
// Check for accessibility and do a poco discovery
if (PortableContact::lastUpdated($r[0]['url'], true) && ($r[0]["network"] == NETWORK_DFRN)) {
PortableContact::loadWorker(0, 0, $r[0]['id'], str_replace('/profile/', '/poco/', $r[0]['url']));
}
}
logger("gprobe end for ".normalise_link($url), LOGGER_DEBUG);
return;
}
}

606
src/Worker/Notifier.php Normal file
View File

@ -0,0 +1,606 @@
<?php
/**
* @file src/Worker/Notifier.php
*/
namespace Friendica\Worker;
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;
use dba;
require_once 'include/queue_fn.php';
require_once 'include/html2plain.php';
require_once 'include/salmon.php';
require_once 'include/datetime.php';
require_once 'include/items.php';
require_once 'include/bbcode.php';
require_once 'include/email.php';
/*
* This file was at one time responsible for doing all deliveries, but this caused
* big problems when the process was killed or stalled during the delivery process.
* It now invokes separate queues that are delivering via delivery.php and pubsubpublish.php.
*/
/*
* The notifier is typically called with:
*
* Worker::add(PRIORITY_HIGH, "Notifier", COMMAND, ITEM_ID);
*
* where COMMAND is one of the following:
*
* activity (in diaspora.php, dfrn_confirm.php, profiles.php)
* comment-import (in diaspora.php, items.php)
* comment-new (in item.php)
* drop (in diaspora.php, items.php, photos.php)
* edit_post (in item.php)
* event (in events.php)
* expire (in items.php)
* like (in like.php, poke.php)
* mail (in message.php)
* suggest (in fsuggest.php)
* tag (in photos.php, poke.php, tagger.php)
* tgroup (in items.php)
* wall-new (in photos.php, item.php)
* removeme (in Contact.php)
* relocate (in uimport.php)
*
* and ITEM_ID is the id of the item in the database that needs to be sent to others.
*/
class Notifier {
public static function execute($cmd, $item_id) {
global $a;
logger('notifier: invoked: '.$cmd.': '.$item_id, LOGGER_DEBUG);
$expire = false;
$mail = false;
$fsuggest = false;
$relocate = false;
$top_level = false;
$recipients = array();
$url_recipients = array();
$normal_mode = true;
if ($cmd === 'mail') {
$normal_mode = false;
$mail = true;
$message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1",
intval($item_id)
);
if (! count($message)) {
return;
}
$uid = $message[0]['uid'];
$recipients[] = $message[0]['contact-id'];
$item = $message[0];
} elseif ($cmd === 'expire') {
$normal_mode = false;
$expire = true;
$items = q("SELECT * FROM `item` WHERE `uid` = %d AND `wall` = 1
AND `deleted` = 1 AND `changed` > UTC_TIMESTAMP() - INTERVAL 10 MINUTE",
intval($item_id)
);
$uid = $item_id;
$item_id = 0;
if (! count($items)) {
return;
}
} elseif ($cmd === 'suggest') {
$normal_mode = false;
$fsuggest = true;
$suggest = q("SELECT * FROM `fsuggest` WHERE `id` = %d LIMIT 1",
intval($item_id)
);
if (! count($suggest)) {
return;
}
$uid = $suggest[0]['uid'];
$recipients[] = $suggest[0]['cid'];
$item = $suggest[0];
} elseif ($cmd === 'removeme') {
$r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
`user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
`user`.`page-flags`, `user`.`prvnets`, `user`.`account-type`, `user`.`guid`
FROM `contact` INNER JOIN `user` ON `user`.`uid` = `contact`.`uid`
WHERE `contact`.`uid` = %d AND `contact`.`self` LIMIT 1",
intval($item_id));
if (!$r)
return;
$user = $r[0];
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1", intval($item_id));
if (!$r)
return;
$self = $r[0];
$r = q("SELECT * FROM `contact` WHERE NOT `self` AND `uid` = %d", intval($item_id));
if (!$r) {
return;
}
require_once 'include/Contact.php';
foreach ($r as $contact) {
terminate_friendship($user, $self, $contact);
}
return;
} elseif ($cmd === 'relocate') {
$normal_mode = false;
$relocate = true;
$uid = $item_id;
$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",
intval($item_id)
);
if ((! DBM::is_result($r)) || (! intval($r[0]['parent']))) {
return;
}
$target_item = $r[0];
$parent_id = intval($r[0]['parent']);
$uid = $r[0]['uid'];
$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",
intval($parent_id)
);
if (! count($items)) {
return;
}
// avoid race condition with deleting entries
if ($items[0]['deleted']) {
foreach ($items as $item) {
$item['deleted'] = 1;
}
}
if ((count($items) == 1) && ($items[0]['id'] === $target_item['id']) && ($items[0]['uri'] === $items[0]['parent-uri'])) {
logger('notifier: top level post');
$top_level = true;
}
}
$r = q("SELECT `contact`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`,
`user`.`timezone`, `user`.`nickname`, `user`.`sprvkey`, `user`.`spubkey`,
`user`.`page-flags`, `user`.`prvnets`, `user`.`account-type`
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)) {
return;
}
$owner = $r[0];
$walltowall = ((($top_level) && ($owner['id'] != $items[0]['contact-id'])) ? true : false);
// Should the post be transmitted to Diaspora?
$diaspora_delivery = true;
// If this is a public conversation, notify the feed hub
$public_message = true;
// 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);
require_once 'include/group.php';
$parent = $items[0];
$thr_parent = q("SELECT `network`, `author-link`, `owner-link` FROM `item` WHERE `uri` = '%s' AND `uid` = %d",
dbesc($target_item["thr-parent"]), intval($target_item["uid"]));
logger('GUID: '.$target_item["guid"].': Parent is '.$parent['network'].'. Thread parent is '.$thr_parent[0]['network'], LOGGER_DEBUG);
// This is IMPORTANT!!!!
// We will only send a "notify owner to relay" or followup message if the referenced post
// originated on our system by virtue of having our hostname somewhere
// in the URI, AND it was a comment (not top_level) AND the parent originated elsewhere.
// if $parent['wall'] == 1 we will already have the parent message in our array
// and we will relay the whole lot.
// expire sends an entire group of expire messages and cannot be forwarded.
// However the conversation owner will be a part of the conversation and will
// be notified during this run.
// Other DFRN conversation members will be alerted during polled updates.
// Diaspora members currently are not notified of expirations, and other networks have
// either limited or no ability to process deletions. We should at least fix Diaspora
// by stringing togther an array of retractions and sending them onward.
$localhost = str_replace('www.','',$a->get_hostname());
if (strpos($localhost,':')) {
$localhost = substr($localhost,0,strpos($localhost,':'));
}
/**
*
* Be VERY CAREFUL if you make any changes to the following several lines. Seemingly innocuous changes
* have been known to cause runaway conditions which affected several servers, along with
* permissions issues.
*
*/
$relay_to_owner = false;
if (!$top_level && ($parent['wall'] == 0) && !$expire && (stristr($target_item['uri'],$localhost))) {
$relay_to_owner = true;
}
if (($cmd === 'uplink') && (intval($parent['forum_mode']) == 1) && !$top_level) {
$relay_to_owner = true;
}
// until the 'origin' flag has been in use for several months
// we will just use it as a fallback test
// later we will be able to use it as the primary test of whether or not to relay.
if (! $target_item['origin']) {
$relay_to_owner = false;
}
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
$followup = true;
$public_message = false; // not public
$conversant_str = dbesc($parent['contact-id']);
$recipients = array($parent['contact-id']);
$recipients_followup = array($parent['contact-id']);
//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 && ($thr_parent[0]['network'] == NETWORK_OSTATUS)) || ($parent['network'] == NETWORK_OSTATUS)) {
$push_notify = true;
if ($parent["network"] == NETWORK_OSTATUS) {
// Distribute the message to the DFRN contacts as if this wasn't a followup since OStatus can't relay comments
// Currently it is work at progress
$r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `network` = '%s' AND NOT `blocked` AND NOT `pending` AND NOT `archive`",
intval($uid),
dbesc(NETWORK_DFRN)
);
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;
logger('Distributing directly '.$target_item["guid"], LOGGER_DEBUG);
// don't send deletions onward for other people's stuff
if ($target_item['deleted'] && (! intval($target_item['wall']))) {
logger('notifier: ignoring delete notification for non-wall item');
return;
}
if ((strlen($parent['allow_cid']))
|| (strlen($parent['allow_gid']))
|| (strlen($parent['deny_cid']))
|| (strlen($parent['deny_gid']))) {
$public_message = false; // private recipients, not public
}
$allow_people = expand_acl($parent['allow_cid']);
$allow_groups = expand_groups(expand_acl($parent['allow_gid']),true);
$deny_people = expand_acl($parent['deny_cid']);
$deny_groups = expand_groups(expand_acl($parent['deny_gid']));
// if our parent is a public forum (forum_mode == 1), uplink to the origional author causing
// a delivery fork. private groups (forum_mode == 2) do not uplink
if ((intval($parent['forum_mode']) == 1) && (! $top_level) && ($cmd !== 'uplink')) {
Worker::add($a->queue['priority'], 'Notifier', 'uplink', $item_id);
}
$conversants = array();
foreach ($items as $item) {
$recipients[] = $item['contact-id'];
$conversants[] = $item['contact-id'];
// pull out additional tagged people to notify (if public message)
if ($public_message && strlen($item['inform'])) {
$people = explode(',',$item['inform']);
foreach ($people as $person) {
if (substr($person,0,4) === 'cid:') {
$recipients[] = intval(substr($person,4));
$conversants[] = intval(substr($person,4));
} else {
$url_recipients[] = substr($person,4);
}
}
}
}
if (count($url_recipients))
logger('notifier: '.$target_item["guid"].' url_recipients ' . print_r($url_recipients,true));
$conversants = array_unique($conversants);
$recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups));
$deny = array_unique(array_merge($deny_people,$deny_groups));
$recipients = array_diff($recipients,$deny);
$conversant_str = dbesc(implode(', ',$conversants));
}
// 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 && ($thr_parent[0]['network'] == NETWORK_OSTATUS)) || ($parent['network'] == NETWORK_OSTATUS)) {
$diaspora_delivery = false;
logger('Some parent is OStatus for '.$target_item["guid"]." - Author: ".$thr_parent[0]['author-link']." - Owner: ".$thr_parent[0]['owner-link'], LOGGER_DEBUG);
// Send a salmon to the parent author
$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)) {
$probed_contact = $r[0];
} else {
$probed_contact = Probe::uri($thr_parent[0]['author-link']);
}
if ($probed_contact["notify"] != "") {
logger('Notify parent author '.$probed_contact["url"].': '.$probed_contact["notify"]);
$url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
}
// Send a salmon to the parent owner
$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)) {
$probed_contact = $r[0];
} else {
$probed_contact = Probe::uri($thr_parent[0]['owner-link']);
}
if ($probed_contact["notify"] != "") {
logger('Notify parent owner '.$probed_contact["url"].': '.$probed_contact["notify"]);
$url_recipients[$probed_contact["notify"]] = $probed_contact["notify"];
}
// Send a salmon notification to every person we mentioned in the post
$arr = explode(',',$target_item['tag']);
foreach ($arr as $x) {
//logger('Checking tag '.$x, LOGGER_DEBUG);
$matches = null;
if (preg_match('/@\[url=([^\]]*)\]/',$x,$matches)) {
$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"];
}
}
}
// It only makes sense to distribute answers to OStatus messages to Friendica and OStatus - but not Diaspora
$sql_extra = " AND `network` IN ('".NETWORK_OSTATUS."', '".NETWORK_DFRN."')";
} else {
$sql_extra = " AND `network` IN ('".NETWORK_OSTATUS."', '".NETWORK_DFRN."', '".NETWORK_DIASPORA."', '".NETWORK_MAIL."', '".NETWORK_MAIL2."')";
}
} else {
$public_message = false;
}
// If this is a public message and pubmail is set on the parent, include all your email contacts
$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']))
&& (! strlen($target_item['deny_cid'])) && (! strlen($target_item['deny_gid']))
&& (intval($target_item['pubmail']))) {
$r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `network` = '%s'",
intval($uid),
dbesc(NETWORK_MAIL)
);
if (DBM::is_result($r)) {
foreach ($r as $rr) {
$recipients[] = $rr['id'];
}
}
}
}
if ($followup) {
$recip_str = implode(', ', $recipients_followup);
} else {
$recip_str = implode(', ', $recipients);
}
if ($relocate) {
$r = $recipients_relocate;
} else {
$r = q("SELECT `id`, `url`, `network`, `self` FROM `contact`
WHERE `id` IN (%s) AND NOT `blocked` AND NOT `pending` AND NOT `archive`".$sql_extra,
dbesc($recip_str)
);
}
// delivery loop
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);
Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'Delivery', $cmd, $item_id, (int)$contact['id']);
}
}
// send salmon slaps to mentioned remote tags (@foo@example.com) in OStatus posts
// 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 (!Config::get('system','dfrn_only')) {
foreach ($url_recipients as $url) {
if ($url) {
logger('notifier: urldelivery: ' . $url);
$deliver_status = slapper($owner,$url,$slap);
/// @TODO Redeliver/queue these items on failure, though there is no contact record
}
}
}
}
if ($public_message) {
$r0 = array();
$r1 = array();
if ($diaspora_delivery) {
if (!$followup) {
$r0 = Diaspora::relay_list();
}
$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 `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)
);
}
$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",
dbesc(NETWORK_DFRN),
dbesc(NETWORK_MAIL2),
intval($owner['uid']),
intval(CONTACT_IS_SHARING)
);
$r = array_merge($r2,$r1,$r0);
if (DBM::is_result($r)) {
logger('pubdeliver '.$target_item["guid"].': '.print_r($r,true), LOGGER_DEBUG);
foreach ($r as $rr) {
// except for Diaspora batch jobs
// Don't deliver to folks who have already been delivered to
if (($rr['network'] !== NETWORK_DIASPORA) && (in_array($rr['id'],$conversants))) {
logger('notifier: already delivered id=' . $rr['id']);
continue;
}
if ((! $mail) && (! $fsuggest) && (! $followup)) {
logger('notifier: delivery agent: '.$rr['name'].' '.$rr['id'].' '.$rr['network'].' '.$target_item["guid"]);
Worker::add(array('priority' => $a->queue['priority'], 'created' => $a->queue['created'], 'dont_fork' => true),
'Delivery', $cmd, $item_id, (int)$rr['id']);
}
}
}
$push_notify = true;
}
// Notify PuSH subscribers (Used for OStatus distribution of regular posts)
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']));
logger('Activating internal PuSH for item '.$item_id, LOGGER_DEBUG);
// Handling the pubsubhubbub requests
Worker::add(array('priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true),
'PubSubPublish');
}
logger('notifier: calling hooks', LOGGER_DEBUG);
if ($normal_mode) {
call_hooks('notifier_normal',$target_item);
}
call_hooks('notifier_end',$target_item);
return;
}
}

View File

@ -0,0 +1,19 @@
<?php
/**
* @file src/Worker/ProfileUpdate.php
* @brief Send updated profile data to Diaspora
*/
namespace Friendica\Worker;
use Friendica\Protocol\Diaspora;
class ProfileUpdate {
public static function execute($uid = 0) {
if (empty($uid)) {
return;
}
Diaspora::send_profile($uid);
}
}

View File

@ -0,0 +1,97 @@
<?php
/**
* @file src/Worker/PubSubPublish.php
*/
namespace Friendica\Worker;
use Friendica\App;
use Friendica\Core\System;
use Friendica\Core\Config;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Protocol\OStatus;
require_once 'include/items.php';
class PubSubPublish {
public static function execute($pubsubpublish_id = 0)
{
global $a;
if ($pubsubpublish_id == 0) {
// We'll push to each subscriber that has push > 0,
// i.e. there has been an update (set in notifier.php).
$r = q("SELECT `id`, `callback_url` FROM `push_subscriber` WHERE `push` > 0 ORDER BY `last_update` DESC");
foreach ($r as $rr) {
logger("Publish feed to ".$rr["callback_url"], LOGGER_DEBUG);
Worker::add(array('priority' => PRIORITY_HIGH, 'created' => $a->queue['created'], 'dont_fork' => true),
'PubSubPublish', (int)$rr["id"]);
}
}
self::publish($pubsubpublish_id);
return;
}
private static function publish($id) {
global $a;
$r = q("SELECT * FROM `push_subscriber` WHERE `id` = %d", intval($id));
if (!DBM::is_result($r)) {
return;
}
$rr = $r[0];
/// @todo Check server status with PortableContact::checkServer()
// Before this can be done we need a way to safely detect the server url.
logger("Generate feed of user ".$rr['nickname']." to ".$rr['callback_url']." - last updated ".$rr['last_update'], LOGGER_DEBUG);
$last_update = $rr['last_update'];
$params = OStatus::feed($a, $rr['nickname'], $last_update);
if (!$params) {
return;
}
$hmac_sig = hash_hmac("sha1", $params, $rr['secret']);
$headers = array("Content-type: application/atom+xml",
sprintf("Link: <%s>;rel=hub,<%s>;rel=self",
System::baseUrl().'/pubsubhubbub/'.$rr['nickname'],
$rr['topic']),
"X-Hub-Signature: sha1=".$hmac_sig);
logger('POST '.print_r($headers, true)."\n".$params, LOGGER_DEBUG);
post_url($rr['callback_url'], $params, $headers);
$ret = $a->get_curl_code();
if ($ret >= 200 && $ret <= 299) {
logger('successfully pushed to '.$rr['callback_url']);
// set last_update to the "created" date of the last item, and reset push=0
q("UPDATE `push_subscriber` SET `push` = 0, last_update = '%s' WHERE id = %d",
dbesc($last_update),
intval($rr['id']));
} else {
logger('error when pushing to '.$rr['callback_url'].' HTTP: '.$ret);
// we use the push variable also as a counter, if we failed we
// increment this until some upper limit where we give up
$new_push = intval($rr['push']) + 1;
if ($new_push > 30) // OK, let's give up
$new_push = 0;
q("UPDATE `push_subscriber` SET `push` = %d WHERE id = %d",
$new_push,
intval($rr['id']));
}
}
}

186
src/Worker/Queue.php Normal file
View File

@ -0,0 +1,186 @@
<?php
/**
* @file include/queue.php
*/
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\DFRN;
use Friendica\Protocol\PortableContact;
require_once 'include/queue_fn.php';
require_once 'include/datetime.php';
require_once 'include/items.php';
require_once 'include/bbcode.php';
require_once 'include/salmon.php';
class Queue {
public static function execute($queue_id = 0)
{
global $a;
$cachekey_deadguy = 'queue_run:deadguy:';
$cachekey_server = 'queue_run:server:';
if (!$queue_id) {
logger('queue: start');
// Handling the pubsubhubbub requests
Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), 'PubSubPublish');
$r = q(
"SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue`
INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id`
WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY"
);
if (DBM::is_result($r)) {
foreach ($r as $rr) {
logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']);
logger('Expired queue data: ' . $rr['content'], LOGGER_DATA);
}
q("DELETE FROM `queue` WHERE `created` < UTC_TIMESTAMP() - INTERVAL 3 DAY");
}
/*
* For the first 12 hours we'll try to deliver every 15 minutes
* After that, we'll only attempt delivery once per hour.
*/
$r = q("SELECT `id` FROM `queue` WHERE ((`created` > UTC_TIMESTAMP() - INTERVAL 12 HOUR AND `last` < UTC_TIMESTAMP() - INTERVAL 15 MINUTE) OR (`last` < UTC_TIMESTAMP() - INTERVAL 1 HOUR)) ORDER BY `cid`, `created`");
call_hooks('queue_predeliver', $a, $r);
if (DBM::is_result($r)) {
foreach ($r as $q_item) {
logger('Call queue for id '.$q_item['id']);
Worker::add(array('priority' => PRIORITY_LOW, 'dont_fork' => true), "Queue", (int)$q_item['id']);
}
}
return;
}
// delivering
$r = q(
"SELECT * FROM `queue` WHERE `id` = %d LIMIT 1",
intval($queue_id)
);
if (!DBM::is_result($r)) {
return;
}
$q_item = $r[0];
$c = q(
"SELECT * FROM `contact` WHERE `id` = %d LIMIT 1",
intval($q_item['cid'])
);
if (!DBM::is_result($c)) {
remove_queue_item($q_item['id']);
return;
}
$dead = Cache::get($cachekey_deadguy.$c[0]['notify']);
if (!is_null($dead) && $dead) {
logger('queue: skipping known dead url: '.$c[0]['notify']);
update_queue_time($q_item['id']);
return;
}
$server = PortableContact::detectServer($c[0]['url']);
if ($server != "") {
$vital = Cache::get($cachekey_server.$server);
if (is_null($vital)) {
logger("Check server ".$server." (".$c[0]["network"].")");
$vital = PortableContact::checkServer($server, $c[0]["network"], true);
Cache::set($cachekey_server.$server, $vital, CACHE_QUARTER_HOUR);
}
if (!is_null($vital) && !$vital) {
logger('queue: skipping dead server: '.$server);
update_queue_time($q_item['id']);
return;
}
}
$u = q(
"SELECT `user`.*, `user`.`pubkey` AS `upubkey`, `user`.`prvkey` AS `uprvkey`
FROM `user` WHERE `uid` = %d LIMIT 1",
intval($c[0]['uid'])
);
if (!DBM::is_result($u)) {
remove_queue_item($q_item['id']);
return;
}
$data = $q_item['content'];
$public = $q_item['batch'];
$contact = $c[0];
$owner = $u[0];
$deliver_status = 0;
switch ($contact['network']) {
case NETWORK_DFRN:
logger('queue: dfrndelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = DFRN::deliver($owner, $contact, $data);
if ($deliver_status == (-1)) {
update_queue_time($q_item['id']);
Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
} else {
remove_queue_item($q_item['id']);
}
break;
case NETWORK_OSTATUS:
if ($contact['notify']) {
logger('queue: slapdelivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = slapper($owner, $contact['notify'], $data);
if ($deliver_status == (-1)) {
update_queue_time($q_item['id']);
Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
} else {
remove_queue_item($q_item['id']);
}
}
break;
case NETWORK_DIASPORA:
if ($contact['notify']) {
logger('queue: diaspora_delivery: item '.$q_item['id'].' for '.$contact['name'].' <'.$contact['url'].'>');
$deliver_status = Diaspora::transmit($owner, $contact, $data, $public, true);
if ($deliver_status == (-1)) {
update_queue_time($q_item['id']);
Cache::set($cachekey_deadguy.$contact['notify'], true, CACHE_QUARTER_HOUR);
} else {
remove_queue_item($q_item['id']);
}
}
break;
default:
$params = array('owner' => $owner, 'contact' => $contact, 'queue' => $q_item, 'result' => false);
call_hooks('queue_deliver', $a, $params);
if ($params['result']) {
remove_queue_item($q_item['id']);
} else {
update_queue_time($q_item['id']);
}
break;
}
logger('Deliver status '.(int)$deliver_status.' for item '.$q_item['id'].' to '.$contact['name'].' <'.$contact['url'].'>');
return;
}
}