streams/include/onepoll.php

140 lines
3.2 KiB
PHP
Raw Normal View History

2013-02-26 01:09:40 +00:00
<?php /** @file */
2012-12-31 00:14:29 +00:00
require_once('boot.php');
require_once('include/cli_startup.php');
2012-12-31 07:51:39 +00:00
require_once('include/zot.php');
require_once('include/socgraph.php');
2013-01-25 23:36:18 +00:00
require_once('include/Contact.php');
2012-12-31 00:14:29 +00:00
function onepoll_run($argv, $argc){
2012-12-31 00:14:29 +00:00
cli_startup();
$a = get_app();
logger('onepoll: start');
$manual_id = 0;
$generation = 0;
2012-08-31 01:47:07 +00:00
$force = false;
$restart = false;
if(($argc > 1) && (intval($argv[1])))
$contact_id = intval($argv[1]);
if(! $contact_id) {
logger('onepoll: no contact');
return;
}
$d = datetime_convert();
2012-12-31 06:41:53 +00:00
$contacts = q("SELECT abook.*, xchan.*, account.*
2012-12-31 06:56:52 +00:00
FROM abook LEFT JOIN account on abook_account = account_id left join xchan on xchan_hash = abook_xchan
2012-12-31 06:41:53 +00:00
where abook_id = %d
2012-12-31 07:51:39 +00:00
AND (( abook_flags = %d ) OR ( abook_flags = %d ))
AND (( account_flags = %d ) OR ( account_flags = %d )) limit 1",
2012-12-31 00:14:29 +00:00
intval($contact_id),
2012-12-31 07:51:39 +00:00
intval(ABOOK_FLAG_HIDDEN),
intval(0),
intval(ACCOUNT_OK),
intval(ACCOUNT_UNVERIFIED)
);
2012-12-31 00:14:29 +00:00
if(! $contacts) {
logger('onepoll: abook_id not found: ' . $contact_id);
return;
}
$contact = $contacts[0];
2012-12-31 07:55:57 +00:00
$t = $contact['abook_updated'];
2012-12-31 06:41:53 +00:00
$importer_uid = $contact['abook_channel'];
2012-12-31 06:41:53 +00:00
$r = q("SELECT * from channel left join xchan on channel_hash = xchan_hash where channel_id = %d limit 1",
intval($importer_uid)
);
2012-12-31 07:51:39 +00:00
2012-12-31 06:41:53 +00:00
if(! $r)
return;
$importer = $r[0];
2012-12-31 06:41:53 +00:00
logger("onepoll: poll: ({$contact['id']}) IMPORTER: {$importer['xchan_name']}, CONTACT: {$contact['xchan_name']}");
2013-01-25 23:36:18 +00:00
$last_update = (($contact['abook_updated'] === '0000-00-00 00:00:00')
2012-12-31 06:41:53 +00:00
? datetime_convert('UTC','UTC','now - 7 days')
: datetime_convert('UTC','UTC',$contact['abook_updated'])
);
2012-12-31 06:41:53 +00:00
// update permissions
2012-06-14 23:56:46 +00:00
2012-12-31 06:41:53 +00:00
$x = zot_refresh($contact,$importer);
2013-01-25 23:36:18 +00:00
$responded = false;
2013-01-30 03:28:19 +00:00
$updated = datetime_convert();
2012-12-31 06:41:53 +00:00
if(! $x) {
2013-01-30 03:28:19 +00:00
// mark for death by not updating abook_connected, this is caught in include/poller.php
q("update abook set abook_updated = '%s' where abook_id = %d limit 1",
dbesc($updated),
intval($contact['abook_id'])
);
}
2012-12-31 06:41:53 +00:00
else {
2013-01-25 23:36:18 +00:00
q("update abook set abook_updated = '%s', abook_connected = '%s' where abook_id = %d limit 1",
2013-01-30 03:28:19 +00:00
dbesc($updated),
dbesc($updated),
2012-12-31 07:55:57 +00:00
intval($contact['abook_id'])
);
2013-01-25 23:36:18 +00:00
$responded = true;
}
2013-01-25 23:36:18 +00:00
if(! $responded)
return;
if($contact['xchan_connurl']) {
$feedurl = str_replace('/poco/','/zotfeed/',$channel['xchan_connurl']);
$x = z_fetch_url($feedurl . '?f=$mindate=' . $last_update);
if($x['success']) {
$total = 0;
2013-01-04 03:35:42 +00:00
$j = json_decode($x['body'],true);
if($j['success'] && $j['messages']) {
foreach($j['messages'] as $message) {
$results = process_delivery(array('hash' => $contact['xchan_hash']),$message,
array(array('hash' => $importer['xchan_hash'])), false);
$total ++;
}
logger("onepoll: $total messages processed");
}
}
}
2012-12-31 06:41:53 +00:00
// fetch some items
// set last updated timestamp
2013-01-02 07:56:27 +00:00
if($contact['xchan_connurl']) {
$r = q("SELECT xlink_id from xlink
where xlink_xchan = '%s' and xlink_updated > UTC_TIMESTAMP() - INTERVAL 1 DAY limit 1",
2013-01-02 07:56:27 +00:00
intval($contact['xchan_hash'])
);
if(! $r) {
2013-01-23 04:48:03 +00:00
poco_load($contact['xchan_hash'],$contact['xchan_connurl']);
}
}
return;
}
if (array_search(__file__,get_included_files())===0){
onepoll_run($argv,$argc);
killme();
}