diff --git a/boot.php b/boot.php index 78ef7fea..4dd781e1 100755 --- a/boot.php +++ b/boot.php @@ -8,7 +8,7 @@ define('EOL', "
\r\n"); define('REGISTER_CLOSED', 0); define('REGISTER_APPROVE', 1); -define('REGISTER_OPEN', 2); +define('REGISTER_OPEN', 2); define('DIRECTION_NONE', 0); define('DIRECTION_IN', 1); @@ -17,22 +17,20 @@ define('DIRECTION_BOTH', 3); define('NOTIFY_INTRO', 0x0001); define('NOTIFY_CONFIRM', 0x0002); -define('NOTIFY_WALL', 0x0004); +define('NOTIFY_WALL', 0x0004); define('NOTIFY_COMMENT', 0x0008); -define('NOTIFY_MAIL', 0x0010); +define('NOTIFY_MAIL', 0x0010); define('NAMESPACE_DFRN', 'http://purl.org/macgirvin/dfrn/1.0'); /** * log levels */ - -define('LOGGER_NORMAL', 0); -define('LOGGER_TRACE', 1); -define('LOGGER_DEBUG', 2); -define('LOGGER_DATA', 3); -define('LOGGER_ALL', 4); - +define('LOGGER_NORMAL', 0); +define('LOGGER_TRACE', 1); +define('LOGGER_DEBUG', 2); +define('LOGGER_DATA', 3); +define('LOGGER_ALL', 4); class App { @@ -64,15 +62,13 @@ class App { $this->config = array(); $this->page = array(); - $this->pager= array(); + $this->pager = array(); - $this->scheme = ((isset($_SERVER['HTTPS']) - && ($_SERVER['HTTPS'])) ? 'https' : 'http'); - $this->hostname = str_replace('www.', '', - $_SERVER['SERVER_NAME']); + $this->scheme = ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'])) ? 'https' : 'http'); + $this->hostname = str_replace('www.', '', $_SERVER['SERVER_NAME']); set_include_path("include/$this->hostname" - . PATH_SEPARATOR . 'include' - . PATH_SEPARATOR . '.'); + . PATH_SEPARATOR . 'include' + . PATH_SEPARATOR . '.'); if (substr($_SERVER['QUERY_STRING'], 0, 2) == "q=") { $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'], 2); @@ -104,8 +100,7 @@ class App } $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname - . ((isset($this->path) && strlen($this->path)) - ? '/' . $this->path : ''); + . ((isset($this->path) && strlen($this->path)) ? '/' . $this->path : ''); return $this->baseurl; } @@ -152,13 +147,12 @@ class App $s = file_get_contents("view/head.tpl"); } $this->page['htmlhead'] = replace_macros($s, array( - '$baseurl' => $this->get_baseurl() - )); + '$baseurl' => $this->get_baseurl() + )); } } - -if (! function_exists('x')) { +if (!function_exists('x')) { function x($s, $k = null) { if ($k != null) { @@ -181,7 +175,7 @@ if (! function_exists('x')) { } } -if (! function_exists('system_unavailable')) { +if (!function_exists('system_unavailable')) { function system_unavailable() { include('system_unavailable.php'); @@ -189,14 +183,14 @@ if (! function_exists('system_unavailable')) { } } -if (! function_exists('logger')) { +if (!function_exists('logger')) { function logger($msg, $level = 0) { $debugging = 1; - $loglevel = LOGGER_ALL; - $logfile = 'logfile.out'; + $loglevel = LOGGER_ALL; + $logfile = 'logfile.out'; - if ((! $debugging) || (! $logfile) || ($level > $loglevel)) { + if ((!$debugging) || (!$logfile) || ($level > $loglevel)) { return; } require_once('include/datetime.php'); @@ -207,7 +201,7 @@ if (! function_exists('logger')) { } -if (! function_exists('replace_macros')) { +if (!function_exists('replace_macros')) { function replace_macros($s, $r) { $search = array(); @@ -215,7 +209,7 @@ if (! function_exists('replace_macros')) { if (is_array($r) && count($r)) { foreach ($r as $k => $v) { - $search[] = $k; + $search[] = $k; $replace[] = $v; } } @@ -224,14 +218,14 @@ if (! function_exists('replace_macros')) { } -if (! function_exists('load_translation_table')) { +if (!function_exists('load_translation_table')) { function load_translation_table($lang) { global $a; } } -if (! function_exists('t')) { +if (!function_exists('t')) { function t($s) { global $a; @@ -243,14 +237,11 @@ if (! function_exists('t')) { } } - - - -if (! function_exists('fetch_url')) { - function fetch_url($url, $binary = false, $timeout=20) +if (!function_exists('fetch_url')) { + function fetch_url($url, $binary = false, $timeout = 20) { $ch = curl_init($url); - if (! $ch) { + if (!$ch) { return false; } @@ -269,12 +260,11 @@ if (! function_exists('fetch_url')) { } } - -if (! function_exists('post_url')) { +if (!function_exists('post_url')) { function post_url($url, $params) { $ch = curl_init($url); - if (! $ch) { + if (!$ch) { return false; } @@ -290,30 +280,29 @@ if (! function_exists('post_url')) { } } - -if (! function_exists('random_string')) { +if (!function_exists('random_string')) { function random_string() { return(hash('sha256', uniqid(rand(), true))); } } -if (! function_exists('notags')) { +if (!function_exists('notags')) { function notags($string) { // protect against :<> with high-bit set - return(str_replace(array("<", ">", "\xBA", "\xBC", "\xBE"), array('[', ']', '', '', ''), $string)); + return(str_replace(array("<", ">", "\xBA", "\xBC", "\xBE"), array('[', ']', '', '', ''), $string)); } } -if (! function_exists('escape_tags')) { +if (!function_exists('escape_tags')) { function escape_tags($string) { return(htmlspecialchars($string)); } } -if (! function_exists('login')) { +if (!function_exists('login')) { function login($register = false) { $o = ""; @@ -325,14 +314,13 @@ if (! function_exists('login')) { } else { $o = file_get_contents("view/login.tpl"); - $o = replace_macros($o, array('$register_html' => $register_html )); + $o = replace_macros($o, array('$register_html' => $register_html)); } return $o; } } - -if (! function_exists('killme')) { +if (!function_exists('killme')) { function killme() { session_write_close(); @@ -341,7 +329,7 @@ if (! function_exists('killme')) { } } -if (! function_exists('goaway')) { +if (!function_exists('goaway')) { function goaway($s) { header("Location: $s"); @@ -349,7 +337,7 @@ if (! function_exists('goaway')) { } } -if (! function_exists('local_user')) { +if (!function_exists('local_user')) { function local_user() { if ((x($_SESSION, 'authenticated')) && (x($_SESSION, 'uid'))) { @@ -359,7 +347,7 @@ if (! function_exists('local_user')) { } } -if (! function_exists('notice')) { +if (!function_exists('notice')) { function notice($s) { if (!isset($_SESSION['sysmsg'])) { @@ -369,18 +357,14 @@ if (! function_exists('notice')) { } } -if (! function_exists('hex2bin')) { +if (!function_exists('hex2bin')) { function hex2bin($s) { return(pack("H*", $s)); } } - - - - -if (! function_exists('paginate')) { +if (!function_exists('paginate')) { function paginate(&$a) { $o = ''; @@ -388,16 +372,15 @@ if (! function_exists('paginate')) { $stripped = str_replace('q=', '', $stripped); $stripped = trim($stripped, '/'); $pagenum = $a->pager['page']; - $url = $a->get_baseurl() . '/' . $stripped ; - + $url = $a->get_baseurl() . '/' . $stripped; if ($a->pager['total'] > $a->pager['itemspage']) { $o .= '
'; if ($a->pager['page'] != 1) { - $o .= ''."pager['page'] - 1).'">' . t('prev') . ' '; + $o .= '' . "' . t('prev') . ' '; } - $o .= "" . t('first') . " "; + $o .= "" . t('first') . " "; $numpages = $a->pager['total'] / $a->pager['itemspage']; @@ -411,35 +394,34 @@ if (! function_exists('paginate')) { for ($i = $numstart; $i <= $numstop; $i++) { if ($i == $a->pager['page']) { - $o .= ''.(($i < 10) ? ' '.$i : $i); + $o .= '' . (($i < 10) ? ' ' . $i : $i); } else { - $o .= "".(($i < 10) ? ' '.$i : $i).""; + $o .= "" . (($i < 10) ? ' ' . $i : $i) . ""; } $o .= ' '; } if (($a->pager['total'] % $a->pager['itemspage']) != 0) { if ($i == $a->pager['page']) { - $o .= ''.(($i < 10) ? ' '.$i : $i); + $o .= '' . (($i < 10) ? ' ' . $i : $i); } else { - $o .= "".(($i < 10) ? ' '.$i : $i).""; + $o .= "" . (($i < 10) ? ' ' . $i : $i) . ""; } $o .= ' '; } - $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages); - $o .= "" . t('last') . " "; + $lastpage = (($numpages > intval($numpages)) ? intval($numpages) + 1 : $numpages); + $o .= "" . t('last') . " "; if (($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0) { - $o .= ''."pager['page'] + 1).'">' . t('next') . ''; + $o .= '' . "' . t('next') . ''; } - $o .= '
'."\r\n"; + $o .= '' . "\r\n"; } return $o; } } - function get_my_url() { if (x($_SESSION, 'my_url')) { @@ -450,10 +432,10 @@ function get_my_url() function zrl($s, $force = false) { - if (! strlen($s)) { + if (!strlen($s)) { return $s; } - if ((! strpos($s, '/profile/')) && (! $force)) { + if ((!strpos($s, '/profile/')) && (!$force)) { return $s; } $achar = strpos($s, '?') ? '&' : '?'; @@ -464,7 +446,7 @@ function zrl($s, $force = false) return $s; } -if (! function_exists('link_compare')) { +if (!function_exists('link_compare')) { function link_compare($a, $b) { if (strcasecmp(normalise_link($a), normalise_link($b)) === 0) { @@ -474,7 +456,7 @@ if (! function_exists('link_compare')) { } } -if (! function_exists('normalise_link')) { +if (!function_exists('normalise_link')) { function normalise_link($url) { $ret = str_replace(array('https:', '//www.'), array('http:', '//'), $url); diff --git a/include/cron_maintain.php b/include/cron_maintain.php index e71a7949..40174024 100644 --- a/include/cron_maintain.php +++ b/include/cron_maintain.php @@ -1,114 +1,128 @@ config['maintenance']['min_scrape_delay'])), + intval($a->config['maintenance']['max_scrapes']) +); + +//Nothing to do. +if (!$res || !count($res)) { + exit; +} + +//Close DB here. Threads need their private connection. +$db->getdb()->close(); + +//We need the scraper. +require_once 'include/submit.php'; + +//POSIX threads only. +if (!function_exists('pcntl_fork')) { + logger('Error: no pcntl_fork support. Are you running a different OS? Report an issue please.'); + die('Error: no pcntl_fork support. Are you running a different OS? Report an issue please.'); +} + +//Create the threads we need. +$items = count($res); +$threadc = min($a->config['maintenance']['threads'], $items); //Don't need more threads than items. +$threads = array(); + +//Debug... +if ($verbose) { + echo "Creating $threadc maintainer threads for $items profiles." . PHP_EOL; +} +logger("Creating $threadc maintainer threads for $items profiles."); + +for ($i = 0; $i < $threadc; $i++) { + + $pid = pcntl_fork(); + if ($pid === -1) { + if ($verbose) { + echo('Error: something went wrong with the fork. ' . pcntl_strerror()); + } + logger('Error: something went wrong with the fork. ' . pcntl_strerror()); + die('Error: something went wrong with the fork. ' . pcntl_strerror()); + } + + //You're a child, go do some labor! + if ($pid === 0) { + break; + } + + //Store the list of PID's. + if ($pid > 0) { + $threads[] = $pid; + } +} + +//The work for child processes. +if ($pid === 0) { + + //Lets be nice, we're only doing maintenance here... + pcntl_setpriority(5); + + //Get personal DBA's. $db = new dba($db_host, $db_user, $db_pass, $db_data, $install); - - //Get our set of items. Youngest items first, after the threshold. - //This may be counter-intuitive, but is to prevent items that fail to update from blocking the rest. - $res = q( - "SELECT `id`, `homepage`, `censored` FROM `profile` WHERE `updated` < '%s' ORDER BY `updated` DESC LIMIT %u", - dbesc(date('Y-m-d H:i:s', time()-$a->config['maintenance']['min_scrape_delay'])), - intval($a->config['maintenance']['max_scrapes']) - ); - - //Nothing to do. - if(!$res || !count($res)){ - exit; + + //Get our (round-robin) workload from the DB results. + $myIndex = $i + 1; + $workload = array(); + while (isset($res[$i])) { + $entry = $res[$i]; + $workload[] = $entry; + $ids[] = $entry['id']; + $i += $threadc; } - - //Close DB here. Threads need their private connection. - $db->getdb()->close(); - - //We need the scraper. - require_once('include/submit.php'); - - //POSIX threads only. - if(!function_exists('pcntl_fork')){ - logger('Error: no pcntl_fork support. Are you running a different OS? Report an issue please.'); - die('Error: no pcntl_fork support. Are you running a different OS? Report an issue please.'); - } - - //Create the threads we need. - $items = count($res); - $threadc = min($a->config['maintenance']['threads'], $items); //Don't need more threads than items. - $threads = array(); - - //Debug... - if($verbose) echo("Creating $threadc maintainer threads for $items profiles.".PHP_EOL); - logger("Creating $threadc maintainer threads for $items profiles."); - - for($i = 0; $i < $threadc; $i++){ - - $pid = pcntl_fork(); - if($pid === -1){ - if($verbose) echo('Error: something went wrong with the fork. '.pcntl_strerror()); - logger('Error: something went wrong with the fork. '.pcntl_strerror()); - die('Error: something went wrong with the fork. '.pcntl_strerror()); + + while (count($workload)) { + $entry = array_pop($workload); + set_time_limit(20); //This should work for 1 submit. + if ($verbose) { + echo "Submitting " . $entry['homepage'] . PHP_EOL; } - - //You're a child, go do some labor! - if($pid === 0) break; - - //Store the list of PID's. - if($pid > 0) $threads[] = $pid; - + run_submit($entry['homepage']); } - - //The work for child processes. - if($pid === 0){ - - //Lets be nice, we're only doing maintenance here... - pcntl_setpriority(5); - - //Get personal DBA's. - $db = new dba($db_host, $db_user, $db_pass, $db_data, $install); - - //Get our (round-robin) workload from the DB results. - $myIndex = $i+1; - $workload = array(); - while(isset($res[$i])){ - $entry = $res[$i]; - $workload[] = $entry; - $ids[] = $entry['id']; - $i+=$threadc; - } - - while(count($workload)){ - $entry = array_pop($workload); - set_time_limit(20); //This should work for 1 submit. - if($verbose) echo "Submitting ".$entry['homepage'].PHP_EOL; - run_submit($entry['homepage']); - } - - exit; - - } - + + exit; +} else { //The main process. - else{ - foreach($threads as $pid){ - pcntl_waitpid($pid, $status); - if($status !== 0){ - if($verbose) echo "Bad process return value $pid:$status".PHP_EOL; - logger("Bad process return value $pid:$status"); + foreach ($threads as $pid) { + pcntl_waitpid($pid, $status); + if ($status !== 0) { + if ($verbose) { + echo "Bad process return value $pid:$status" . PHP_EOL; } + logger("Bad process return value $pid:$status"); } - $time = time() - $start_maintain; - if($verbose) echo("Maintenance completed. Took $time seconds.".PHP_EOL); - logger("Maintenance completed. Took $time seconds."); } + $time = time() - $start_maintain; + if ($verbose) { + echo("Maintenance completed. Took $time seconds." . PHP_EOL); + } + logger("Maintenance completed. Took $time seconds."); +} diff --git a/include/cron_sync.php b/include/cron_sync.php index b4f6ec55..750e56dd 100644 --- a/include/cron_sync.php +++ b/include/cron_sync.php @@ -1,76 +1,81 @@ getdb()->close(); -if(count($pull_batch)) +if (count($pull_batch)) { run_pulling_job($a, $pull_batch, $db_host, $db_user, $db_pass, $db_data, $install); +} //Do our multi-fork job, if we have a batch and targets. -if(count($push_targets) && count($push_batch)) +if (count($push_targets) && count($push_batch)) { run_pushing_job($push_targets, $push_batch, $db_host, $db_user, $db_pass, $db_data, $install); +} //Log the time it took. $time = time() - $start_syncing; diff --git a/include/directory.php b/include/directory.php index 1966bd36..23abc861 100644 --- a/include/directory.php +++ b/include/directory.php @@ -1,27 +1,31 @@ set_baseurl(get_config('system','url')); - - $dir = get_config('system','directory_submit_url'); - - if(! strlen($dir)) - exit; - - fetch_url($dir . '?url=' . bin2hex($argv[1])); +unset($db_host, $db_user, $db_pass, $db_data); +if ($argc != 2) { exit; +} + +load_config('system'); + +$a->set_baseurl(get_config('system', 'url')); + +$dir = get_config('system', 'directory_submit_url'); + +if (!strlen($dir)) { + exit; +} + +fetch_url($dir . '?url=' . bin2hex($argv[1])); + +exit; diff --git a/include/notifier.php b/include/notifier.php index bdc773a0..e87d9e68 100644 --- a/include/notifier.php +++ b/include/notifier.php @@ -1,311 +1,291 @@ set_baseurl(get_config('system','url')); +require_once 'session.php'; +require_once 'datetime.php'; - $cmd = $argv[1]; +if ($argc < 3) { + exit; +} - switch($cmd) { - case 'mail': - default: - $item_id = intval($argv[2]); - if(! $item_id) - killme(); - break; - } +$a->set_baseurl(get_config('system', 'url')); +$cmd = $argv[1]; - $recipients = array(); - - if($cmd == 'mail') { - - $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1", - intval($item_id) - ); - if(! count($message)) +switch ($cmd) { + case 'mail': + default: + $item_id = intval($argv[2]); + if (!$item_id) { killme(); - $recipients[] = $message[0]['contact-id']; - $item = $message[0]; + } + break; +} - } - else { - // find ancestors +$recipients = array(); - $r = q("SELECT `parent`, `edited` FROM `item` WHERE `id` = %d LIMIT 1", - intval($item_id) - ); - if(! count($r)) - killme(); +if ($cmd == 'mail') { - $parent = $r[0]['parent']; - $updated = $r[0]['edited']; - - $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC", - intval($parent) - ); - - if(! count($items)) - killme(); - } - - $r = q("SELECT * FROM `contact` WHERE `self` = 1 LIMIT 1"); - - if(count($r)) - $owner = $r[0]; - else + $message = q("SELECT * FROM `mail` WHERE `id` = %d LIMIT 1", intval($item_id)); + if (!count($message)) { killme(); + } + $recipients[] = $message[0]['contact-id']; + $item = $message[0]; +} else { + // find ancestors - if($cmd != 'mail') { - - require_once('include/group.php'); - - $parent = $items[0]; - - if($parent['type'] == 'remote') { - // local followup to remote post - $followup = true; - $conversant_str = dbesc($parent['contact-id']); - } - else { - $followup = false; - - $allow_people = expand_acl($parent['allow_cid']); - $allow_groups = expand_groups(expand_acl($parent['allow_gid'])); - $deny_people = expand_acl($parent['deny_cid']); - $deny_groups = expand_groups(expand_acl($parent['deny_gid'])); - - $conversants = array(); - - foreach($items as $item) { - $recipients[] = $item['contact-id']; - $conversants[] = $item['contact-id']; - } - - $conversants = array_unique($conversants,SORT_NUMERIC); - - - $recipients = array_unique(array_merge($recipients,$allow_people,$allow_groups),SORT_NUMERIC); - $deny = array_unique(array_merge($deny_people,$deny_groups),SORT_NUMERIC); - $recipients = array_diff($recipients,$deny); - - $conversant_str = dbesc(implode(', ',$conversants)); - } - - $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0"); - - if( ! count($r)) - killme(); - - $contacts = $r; - - $tomb_template = file_get_contents('view/atom_tomb.tpl'); - $item_template = file_get_contents('view/atom_item.tpl'); - $cmnt_template = file_get_contents('view/atom_cmnt.tpl'); + $r = q("SELECT `parent`, `edited` FROM `item` WHERE `id` = %d LIMIT 1", intval($item_id)); + if (!count($r)) { + killme(); } - $feed_template = file_get_contents('view/atom_feed.tpl'); - $mail_template = file_get_contents('view/atom_mail.tpl'); + $parent = $r[0]['parent']; + $updated = $r[0]['edited']; - $atom = ''; + $items = q("SELECT * FROM `item` WHERE `parent` = %d ORDER BY `id` ASC", intval($parent)); + if (!count($items)) { + killme(); + } +} - $atom .= replace_macros($feed_template, array( - '$feed_id' => xmlify($a->get_baseurl()), - '$feed_title' => xmlify($owner['name']), - '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', - $updated . '+00:00' , 'Y-m-d\TH:i:s\Z')) , - '$name' => xmlify($owner['name']), - '$profile_page' => xmlify($owner['url']), - '$photo' => xmlify($owner['photo']), - '$thumb' => xmlify($owner['thumb']), - '$picdate' => xmlify(datetime_convert('UTC','UTC',$owner['avatar-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z')) , - '$uridate' => xmlify(datetime_convert('UTC','UTC',$owner['uri-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z')) , - '$namdate' => xmlify(datetime_convert('UTC','UTC',$owner['name-date'] . '+00:00' , 'Y-m-d\TH:i:s\Z')) +$r = q("SELECT * FROM `contact` WHERE `self` = 1 LIMIT 1"); + +if (count($r)) { + $owner = $r[0]; +} else { + killme(); +} + +if ($cmd != 'mail') { + + require_once 'include/group.php'; + + $parent = $items[0]; + + if ($parent['type'] == 'remote') { + // local followup to remote post + $followup = true; + $conversant_str = dbesc($parent['contact-id']); + } else { + $followup = false; + + $allow_people = expand_acl($parent['allow_cid']); + $allow_groups = expand_groups(expand_acl($parent['allow_gid'])); + $deny_people = expand_acl($parent['deny_cid']); + $deny_groups = expand_groups(expand_acl($parent['deny_gid'])); + + $conversants = array(); + + foreach ($items as $item) { + $recipients[] = $item['contact-id']; + $conversants[] = $item['contact-id']; + } + + $conversants = array_unique($conversants, SORT_NUMERIC); + + $recipients = array_unique(array_merge($recipients, $allow_people, $allow_groups), SORT_NUMERIC); + $deny = array_unique(array_merge($deny_people, $deny_groups), SORT_NUMERIC); + $recipients = array_diff($recipients, $deny); + + $conversant_str = dbesc(implode(', ', $conversants)); + } + + $r = q("SELECT * FROM `contact` WHERE `id` IN ( $conversant_str ) AND `blocked` = 0 AND `pending` = 0"); + + if (!count($r)) { + killme(); + } + + $contacts = $r; + + $tomb_template = file_get_contents('view/atom_tomb.tpl'); + $item_template = file_get_contents('view/atom_item.tpl'); + $cmnt_template = file_get_contents('view/atom_cmnt.tpl'); +} + +$feed_template = file_get_contents('view/atom_feed.tpl'); +$mail_template = file_get_contents('view/atom_mail.tpl'); + +$atom = ''; + +$atom .= replace_macros($feed_template, array( + '$feed_id' => xmlify($a->get_baseurl()), + '$feed_title' => xmlify($owner['name']), + '$feed_updated' => xmlify(datetime_convert('UTC', 'UTC', $updated . '+00:00', 'Y-m-d\TH:i:s\Z')), + '$name' => xmlify($owner['name']), + '$profile_page' => xmlify($owner['url']), + '$photo' => xmlify($owner['photo']), + '$thumb' => xmlify($owner['thumb']), + '$picdate' => xmlify(datetime_convert('UTC', 'UTC', $owner['avatar-date'] . '+00:00', 'Y-m-d\TH:i:s\Z')), + '$uridate' => xmlify(datetime_convert('UTC', 'UTC', $owner['uri-date'] . '+00:00', 'Y-m-d\TH:i:s\Z')), + '$namdate' => xmlify(datetime_convert('UTC', 'UTC', $owner['name-date'] . '+00:00', 'Y-m-d\TH:i:s\Z')) +)); + +if ($cmd == 'mail') { + $atom .= replace_macros($mail_template, array( + '$name' => xmlify($owner['name']), + '$profile_page' => xmlify($owner['url']), + '$thumb' => xmlify($owner['thumb']), + '$item_id' => xmlify($item['uri']), + '$subject' => xmlify($item['title']), + '$created' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00', 'Y-m-d\TH:i:s\Z')), + '$content' => xmlify($item['body']), + '$parent_id' => xmlify($item['parent-uri']) )); - - if($cmd == 'mail') { - $atom .= replace_macros($mail_template, array( - '$name' => xmlify($owner['name']), - '$profile_page' => xmlify($owner['url']), - '$thumb' => xmlify($owner['thumb']), - '$item_id' => xmlify($item['uri']), - '$subject' => xmlify($item['title']), - '$created' => xmlify(datetime_convert('UTC', 'UTC', - $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')), - '$content' =>xmlify($item['body']), - '$parent_id' => xmlify($item['parent-uri']) - - )); - } - else { - - if($followup) { - foreach($items as $item) { - if($item['id'] == $item_id) { - $atom .= replace_macros($cmnt_template, array( - '$name' => xmlify($owner['name']), - '$profile_page' => xmlify($owner['url']), - '$thumb' => xmlify($owner['thumb']), - '$item_id' => xmlify($item['uri']), - '$title' => xmlify($item['title']), - '$published' => xmlify(datetime_convert('UTC', 'UTC', - $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')), - '$updated' => xmlify(datetime_convert('UTC', 'UTC', - $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')), - '$content' =>xmlify($item['body']), - '$parent_id' => xmlify($item['parent-uri']), - '$comment_allow' => 0 - )); - } +} else { + if ($followup) { + foreach ($items as $item) { + if ($item['id'] == $item_id) { + $atom .= replace_macros($cmnt_template, array( + '$name' => xmlify($owner['name']), + '$profile_page' => xmlify($owner['url']), + '$thumb' => xmlify($owner['thumb']), + '$item_id' => xmlify($item['uri']), + '$title' => xmlify($item['title']), + '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00', 'Y-m-d\TH:i:s\Z')), + '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00', 'Y-m-d\TH:i:s\Z')), + '$content' => xmlify($item['body']), + '$parent_id' => xmlify($item['parent-uri']), + '$comment_allow' => 0 + )); } } - else { - foreach($items as $item) { - if($item['deleted']) { - $atom .= replace_macros($tomb_template, array( - '$id' => xmlify($item['uri']), - '$updated' => xmlify(datetime_convert('UTC', 'UTC', - $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')) - )); - } - else { - foreach($contacts as $contact) { - if($item['contact-id'] == $contact['id']) { - if($item['parent'] == $item['id']) { - $atom .= replace_macros($item_template, array( - '$name' => xmlify($contact['name']), - '$profile_page' => xmlify($contact['url']), - '$thumb' => xmlify($contact['thumb']), - '$owner_name' => xmlify($item['owner-name']), - '$owner_profile_page' => xmlify($item['owner-link']), - '$owner_thumb' => xmlify($item['owner-avatar']), - '$item_id' => xmlify($item['uri']), - '$title' => xmlify($item['title']), - '$published' => xmlify(datetime_convert('UTC', 'UTC', - $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')), - '$updated' => xmlify(datetime_convert('UTC', 'UTC', - $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')), - '$content' =>xmlify($item['body']), - '$comment_allow' => (($item['last-child'] && strlen($contact['dfrn-id'])) ? 1 : 0) - )); - } - else { - $atom .= replace_macros($cmnt_template, array( - '$name' => xmlify($contact['name']), - '$profile_page' => xmlify($contact['url']), - '$thumb' => xmlify($contact['thumb']), - '$item_id' => xmlify($item['uri']), - '$title' => xmlify($item['title']), - '$published' => xmlify(datetime_convert('UTC', 'UTC', - $item['created'] . '+00:00' , 'Y-m-d\TH:i:s\Z')), - '$updated' => xmlify(datetime_convert('UTC', 'UTC', - $item['edited'] . '+00:00' , 'Y-m-d\TH:i:s\Z')), - '$content' =>xmlify($item['body']), - '$parent_id' => xmlify($item['parent-uri']), - '$comment_allow' => (($item['last-child']) ? 1 : 0) - )); - } + } else { + foreach ($items as $item) { + if ($item['deleted']) { + $atom .= replace_macros($tomb_template, array( + '$id' => xmlify($item['uri']), + '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00', 'Y-m-d\TH:i:s\Z')) + )); + } else { + foreach ($contacts as $contact) { + if ($item['contact-id'] == $contact['id']) { + if ($item['parent'] == $item['id']) { + $atom .= replace_macros($item_template, array( + '$name' => xmlify($contact['name']), + '$profile_page' => xmlify($contact['url']), + '$thumb' => xmlify($contact['thumb']), + '$owner_name' => xmlify($item['owner-name']), + '$owner_profile_page' => xmlify($item['owner-link']), + '$owner_thumb' => xmlify($item['owner-avatar']), + '$item_id' => xmlify($item['uri']), + '$title' => xmlify($item['title']), + '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00', 'Y-m-d\TH:i:s\Z')), + '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00', 'Y-m-d\TH:i:s\Z')), + '$content' => xmlify($item['body']), + '$comment_allow' => (($item['last-child'] && strlen($contact['dfrn-id'])) ? 1 : 0) + )); + } else { + $atom .= replace_macros($cmnt_template, array( + '$name' => xmlify($contact['name']), + '$profile_page' => xmlify($contact['url']), + '$thumb' => xmlify($contact['thumb']), + '$item_id' => xmlify($item['uri']), + '$title' => xmlify($item['title']), + '$published' => xmlify(datetime_convert('UTC', 'UTC', $item['created'] . '+00:00', 'Y-m-d\TH:i:s\Z')), + '$updated' => xmlify(datetime_convert('UTC', 'UTC', $item['edited'] . '+00:00', 'Y-m-d\TH:i:s\Z')), + '$content' => xmlify($item['body']), + '$parent_id' => xmlify($item['parent-uri']), + '$comment_allow' => (($item['last-child']) ? 1 : 0) + )); } } } } } } - $atom .= "\r\n"; +} +$atom .= "\r\n"; - // create a clone of this feed but with comments disabled to send to those who can't respond. +// create a clone of this feed but with comments disabled to send to those who can't respond. - $atom_nowrite = str_replace('1','0',$atom); +$atom_nowrite = str_replace('1', '0', $atom); - if($followup) - $recip_str = $parent['contact-id']; - else - $recip_str = implode(', ', $recipients); +if ($followup) { + $recip_str = $parent['contact-id']; +} else { + $recip_str = implode(', ', $recipients); +} - $r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ", - dbesc($recip_str) - ); - if(! count($r)) - killme(); +$r = q("SELECT * FROM `contact` WHERE `id` IN ( %s ) ", dbesc($recip_str)); +if (!count($r)) { + killme(); +} - // delivery loop +// delivery loop - foreach($r as $rr) { - if($rr['self']) - continue; - - if(! strlen($rr['dfrn-id'])) - continue; - - - $url = $rr['notify'] . '?dfrn_id=' . $rr['dfrn-id']; - - $xml = fetch_url($url); - - if(! $xml) - continue; - - $res = simplexml_load_string($xml); - - if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id))) - continue; - - $postvars = array(); - $sent_dfrn_id = hex2bin($res->dfrn_id); - - $final_dfrn_id = ''; - openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$rr['pubkey']); - $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.')); - if($final_dfrn_id != $rr['dfrn-id']) { - // did not decode properly - cannot trust this site - continue; - } - - $postvars['dfrn_id'] = $rr['dfrn-id']; - - $challenge = hex2bin($res->challenge); - - openssl_public_decrypt($challenge,$postvars['challenge'],$rr['pubkey']); - - if($cmd == 'mail') { - $postvars['data'] = $atom; - } - elseif(strlen($rr['dfrn-id']) && (! ($rr['blocked']) || ($rr['readonly']))) { - $postvars['data'] = $atom; - } - else { - $postvars['data'] = $atom_nowrite; - } - - $xml = post_url($rr['notify'],$postvars); - - $res = simplexml_load_string($xml); - - // Currently there is no retry attempt for failed mail delivery. - // We need to handle this in the UI, report the non-deliverables and try again - - if(($cmd == 'mail') && (intval($res->status) == 0)) { - - $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1", - intval($item_id) - ); - } +foreach ($r as $rr) { + if ($rr['self']) { + continue; } - killme(); + if (!strlen($rr['dfrn-id'])) { + continue; + } + + $url = $rr['notify'] . '?dfrn_id=' . $rr['dfrn-id']; + + $xml = fetch_url($url); + + if (!$xml) { + continue; + } + + $res = simplexml_load_string($xml); + + if ((intval($res->status) != 0) || (!strlen($res->challenge)) || (!strlen($res->dfrn_id))) { + continue; + } + + $postvars = array(); + $sent_dfrn_id = hex2bin($res->dfrn_id); + + $final_dfrn_id = ''; + openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $rr['pubkey']); + $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.')); + if ($final_dfrn_id != $rr['dfrn-id']) { + // did not decode properly - cannot trust this site + continue; + } + + $postvars['dfrn_id'] = $rr['dfrn-id']; + + $challenge = hex2bin($res->challenge); + + openssl_public_decrypt($challenge, $postvars['challenge'], $rr['pubkey']); + + if ($cmd == 'mail') { + $postvars['data'] = $atom; + } elseif (strlen($rr['dfrn-id']) && (!($rr['blocked']) || ($rr['readonly']))) { + $postvars['data'] = $atom; + } else { + $postvars['data'] = $atom_nowrite; + } + + $xml = post_url($rr['notify'], $postvars); + + $res = simplexml_load_string($xml); + + // Currently there is no retry attempt for failed mail delivery. + // We need to handle this in the UI, report the non-deliverables and try again + + if (($cmd == 'mail') && (intval($res->status) == 0)) { + $r = q("UPDATE `mail` SET `delivered` = 1 WHERE `id` = %d LIMIT 1", intval($item_id)); + } +} + +killme(); diff --git a/include/poller.php b/include/poller.php index 8f580b1d..d692bb30 100644 --- a/include/poller.php +++ b/include/poller.php @@ -1,270 +1,266 @@ set_baseurl(get_config('system','url')); +unset($db_host, $db_user, $db_pass, $db_data); - $contacts = q("SELECT * FROM `contact` - WHERE `dfrn-id` != '' AND `self` = 0 AND `blocked` = 0 +require_once 'session.php'; +require_once 'datetime.php'; +require_once 'simplepie/simplepie.inc'; +require_once 'include/items.php'; + +$a->set_baseurl(get_config('system', 'url')); + +$contacts = q("SELECT * FROM `contact` + WHERE `dfrn-id` != '' AND `self` = 0 AND `blocked` = 0 AND `readonly` = 0 ORDER BY RAND()"); - if(! count($contacts)) - killme(); +if (!count($contacts)) { + killme(); +} - foreach($contacts as $contact) { +foreach ($contacts as $contact) { + if ($contact['priority']) { + $update = false; + $t = $contact['last-update']; - if($contact['priority']) { - - $update = false; - $t = $contact['last-update']; - - switch ($contact['priority']) { - case 5: - if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 month")) - $update = true; - break; - case 4: - if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 week")) - $update = true; - break; - case 3: - if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 day")) - $update = true; - break; - case 2: - if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 12 hour")) - $update = true; - break; - case 1: - default: - if(datetime_convert('UTC','UTC', 'now') > datetime_convert('UTC','UTC', t . " + 1 hour")) - $update = true; - break; - } - if(! $update) - continue; + switch ($contact['priority']) { + case 5: + if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', t . " + 1 month")) + $update = true; + break; + case 4: + if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', t . " + 1 week")) + $update = true; + break; + case 3: + if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', t . " + 1 day")) + $update = true; + break; + case 2: + if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', t . " + 12 hour")) + $update = true; + break; + case 1: + default: + if (datetime_convert('UTC', 'UTC', 'now') > datetime_convert('UTC', 'UTC', t . " + 1 hour")) + $update = true; + break; } - - - $r = q("SELECT * FROM `contact` WHERE `self` = 1 LIMIT 1"); - if(! count($r)) + if (!$update) { continue; + } + } - $importer = $r[0]; + $r = q("SELECT * FROM `contact` WHERE `self` = 1 LIMIT 1"); + if (!count($r)) { + continue; + } - $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') - ? datetime_convert('UTC','UTC','now - 30 days','Y-m-d\TH:i:s\Z') - : datetime_convert('UTC','UTC',$contact['last-update'],'Y-m-d\TH:i:s\Z')); + $importer = $r[0]; - $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update ; + $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') ? datetime_convert('UTC', 'UTC', 'now - 30 days', 'Y-m-d\TH:i:s\Z') : datetime_convert('UTC', 'UTC', $contact['last-update'], 'Y-m-d\TH:i:s\Z')); - $xml = fetch_url($url); - if(! $xml) - continue; + $url = $contact['poll'] . '?dfrn_id=' . $contact['dfrn-id'] . '&type=data&last_update=' . $last_update; - $res = simplexml_load_string($xml); + $xml = fetch_url($url); + if (!$xml) { + continue; + } - if((intval($res->status) != 0) || (! strlen($res->challenge)) || (! strlen($res->dfrn_id))) - continue; + $res = simplexml_load_string($xml); - $postvars = array(); + if ((intval($res->status) != 0) || (!strlen($res->challenge)) || (!strlen($res->dfrn_id))) { + continue; + } - $sent_dfrn_id = hex2bin($res->dfrn_id); + $postvars = array(); - $final_dfrn_id = ''; - openssl_public_decrypt($sent_dfrn_id,$final_dfrn_id,$contact['pubkey']); - $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.')); - if($final_dfrn_id != $contact['dfrn-id']) { - // did not decode properly - cannot trust this site - continue; - } + $sent_dfrn_id = hex2bin($res->dfrn_id); - $postvars['dfrn_id'] = $contact['dfrn-id']; - $challenge = hex2bin($res->challenge); + $final_dfrn_id = ''; + openssl_public_decrypt($sent_dfrn_id, $final_dfrn_id, $contact['pubkey']); + $final_dfrn_id = substr($final_dfrn_id, 0, strpos($final_dfrn_id, '.')); + if ($final_dfrn_id != $contact['dfrn-id']) { + // did not decode properly - cannot trust this site + continue; + } - openssl_public_decrypt($challenge,$postvars['challenge'],$contact['pubkey']); + $postvars['dfrn_id'] = $contact['dfrn-id']; + $challenge = hex2bin($res->challenge); - $xml = post_url($contact['poll'],$postvars); + openssl_public_decrypt($challenge, $postvars['challenge'], $contact['pubkey']); - if(! strlen($xml)) { - // an empty response may mean there's nothing new - record the fact that we checked - $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1", - dbesc(datetime_convert()), - intval($contact['id']) - ); - continue; - } + $xml = post_url($contact['poll'], $postvars); - $feed = new SimplePie(); - $feed->set_raw_data($xml); - $feed->enable_order_by_date(false); - $feed->init(); - - $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN,'icon-updated'); - if($photo_rawupdate) { - $photo_timestamp = datetime_convert('UTC','UTC',$photo_rawupdate[0]['data']); - $photo_url = $feed->get_image_url(); - if(strlen($photo_url) && $photo_timestamp > $contact['avatar-date']) { - - require_once("Photo.php"); - - $photo_failure = false; - - $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d LIMIT 1", - intval($contact['id']) - ); - if(count($r)) { - $resource_id = $r[0]['resource-id']; - $img_str = fetch_url($photo_url,true); - $img = new Photo($img_str); - if($img) { - q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d ", - dbesc($resource_id), - intval($contact['id']) - ); - - $img->scaleImageSquare(175); - - $hash = $resource_id; - - $r = $img->store($contact['id'], $hash, basename($photo_url), t('Contact Photos') , 4); - - $img->scaleImage(80); - $r = $img->store($contact['id'], $hash, basename($photo_url), t('Contact Photos') , 5); - if($r) - q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `id` = %d LIMIT 1", - dbesc(datetime_convert()), - intval($contact['id']) - ); - } - } - } - } - - - foreach($feed->get_items() as $item) { - - $deleted = false; - - $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry'); - if(isset($rawdelete[0]['attribs']['']['ref'])) { - $uri = $rawthread[0]['attribs']['']['ref']; - $deleted = true; - if(isset($rawdelete[0]['attribs']['']['when'])) { - $when = $rawthread[0]['attribs']['']['when']; - $when = datetime_convert('UTC','UTC', $when, 'Y-m-d H:i:s'); - } - else - $when = datetime_convert('UTC','UTC','now','Y-m-d H:i:s'); - } - if($deleted) { - $r = q("SELECT * FROM `item` WHERE `uri` = '%s' LIMIT 1", - dbesc($uri) - ); - if(count($r)) { - if($r[0]['uri'] == $r[0]['parent-uri']) { - $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', - `body` = '', `title` = '' - WHERE `parent-uri` = '%s'", - dbesc($when), - dbesc($r[0]['uri']) - ); - } - else { - $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', - `body` = '', `title` = '' - WHERE `uri` = '%s' LIMIT 1", - dbesc($when), - dbesc($uri) - ); - } - } - continue; - } - - - $is_reply = false; - $item_id = $item->get_id(); - $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0",'in-reply-to'); - if(isset($rawthread[0]['attribs']['']['ref'])) { - $is_reply = true; - $parent_uri = $rawthread[0]['attribs']['']['ref']; - } - - - if($is_reply) { - - // Have we seen it? If not, import it. - - $item_id = $item->get_id(); - - $r = q("SELECT `last-child`, `edited` FROM `item` WHERE `uri` = '%s' LIMIT 1", - dbesc($item_id) - ); - // FIXME update content if 'updated' changes - if(count($r)) { - $allow = $item->get_item_tags( NAMESPACE_DFRN , 'comment-allow'); - if($allow && $allow[0]['data'] != $r[0]['last-child']) { - $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' LIMIT 1", - intval($allow[0]['data']), - dbesc($item_id) - ); - } - continue; - } - $datarray = get_atom_elements($item); - $datarray['parent-uri'] = $parent_uri; - $datarray['contact-id'] = $contact['id']; - $r = post_remote($a,$datarray); - continue; - } - - else { - // Head post of a conversation. Have we seen it? If not, import it. - - $item_id = $item->get_id(); - $r = q("SELECT `last-child`, `edited` FROM `item` WHERE `uri` = '%s' LIMIT 1", - dbesc($item_id) - ); - if(count($r)) { - $allow = $item->get_item_tags( NAMESPACE_DFRN ,'comment-allow'); - if($allow && $allow[0]['data'] != $r[0]['last-child']) { - $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' LIMIT 1", - intval($allow[0]['data']), - dbesc($item_id) - ); - } - continue; - } - - $datarray = get_atom_elements($item); - $datarray['parent-uri'] = $item_id; - $datarray['contact-id'] = $contact['id']; - $r = post_remote($a,$datarray); - continue; - - } - - } - - $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1", + if (!strlen($xml)) { + // an empty response may mean there's nothing new - record the fact that we checked + $r = q( + "UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1", dbesc(datetime_convert()), intval($contact['id']) ); - + continue; } - killme(); + $feed = new SimplePie(); + $feed->set_raw_data($xml); + $feed->enable_order_by_date(false); + $feed->init(); + + $photo_rawupdate = $feed->get_feed_tags(NAMESPACE_DFRN, 'icon-updated'); + + if ($photo_rawupdate) { + $photo_timestamp = datetime_convert('UTC', 'UTC', $photo_rawupdate[0]['data']); + $photo_url = $feed->get_image_url(); + + if (strlen($photo_url) && $photo_timestamp > $contact['avatar-date']) { + require_once 'Photo.php'; + + $photo_failure = false; + + $r = q("SELECT `resource-id` FROM `photo` WHERE `contact-id` = %d LIMIT 1", intval($contact['id'])); + + if (count($r)) { + $resource_id = $r[0]['resource-id']; + $img_str = fetch_url($photo_url, true); + $img = new Photo($img_str); + + if ($img) { + q("DELETE FROM `photo` WHERE `resource-id` = '%s' AND contact-id` = %d ", + dbesc($resource_id), + intval($contact['id']) + ); + + $img->scaleImageSquare(175); + + $hash = $resource_id; + + $r = $img->store($contact['id'], $hash, basename($photo_url), t('Contact Photos'), 4); + + $img->scaleImage(80); + $r = $img->store($contact['id'], $hash, basename($photo_url), t('Contact Photos'), 5); + if ($r) { + q("UPDATE `contact` SET `avatar-date` = '%s' WHERE `id` = %d LIMIT 1", + dbesc(datetime_convert()), + intval($contact['id']) + ); + } + } + } + } + } + + foreach ($feed->get_items() as $item) { + $deleted = false; + + $rawdelete = $item->get_item_tags("http://purl.org/atompub/tombstones/1.0", 'deleted-entry'); + + if (isset($rawdelete[0]['attribs']['']['ref'])) { + $uri = $rawthread[0]['attribs']['']['ref']; + $deleted = true; + + if (isset($rawdelete[0]['attribs']['']['when'])) { + $when = $rawthread[0]['attribs']['']['when']; + $when = datetime_convert('UTC', 'UTC', $when, 'Y-m-d H:i:s'); + } else { + $when = datetime_convert('UTC', 'UTC', 'now', 'Y-m-d H:i:s'); + } + } + + if ($deleted) { + $r = q("SELECT * FROM `item` WHERE `uri` = '%s' LIMIT 1", dbesc($uri)); + if (count($r)) { + if ($r[0]['uri'] == $r[0]['parent-uri']) { + $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', + `body` = '', `title` = '' + WHERE `parent-uri` = '%s'", + dbesc($when), + dbesc($r[0]['uri']) + ); + } else { + $r = q("UPDATE `item` SET `deleted` = 1, `edited` = '%s', + `body` = '', `title` = '' + WHERE `uri` = '%s' LIMIT 1", + dbesc($when), + dbesc($uri) + ); + } + } + continue; + } + + $is_reply = false; + $item_id = $item->get_id(); + $rawthread = $item->get_item_tags("http://purl.org/syndication/thread/1.0", 'in-reply-to'); + + if (isset($rawthread[0]['attribs']['']['ref'])) { + $is_reply = true; + $parent_uri = $rawthread[0]['attribs']['']['ref']; + } + + if ($is_reply) { + // Have we seen it? If not, import it. + $item_id = $item->get_id(); + + $r = q("SELECT `last-child`, `edited` FROM `item` WHERE `uri` = '%s' LIMIT 1", dbesc($item_id)); + + // FIXME update content if 'updated' changes + if (count($r)) { + $allow = $item->get_item_tags(NAMESPACE_DFRN, 'comment-allow'); + if ($allow && $allow[0]['data'] != $r[0]['last-child']) { + $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' LIMIT 1", + intval($allow[0]['data']), + dbesc($item_id) + ); + } + continue; + } + + $datarray = get_atom_elements($item); + $datarray['parent-uri'] = $parent_uri; + $datarray['contact-id'] = $contact['id']; + $r = post_remote($a, $datarray); + + continue; + } else { + // Head post of a conversation. Have we seen it? If not, import it. + + $item_id = $item->get_id(); + $r = q("SELECT `last-child`, `edited` FROM `item` WHERE `uri` = '%s' LIMIT 1", dbesc($item_id)); + + if (count($r)) { + $allow = $item->get_item_tags(NAMESPACE_DFRN, 'comment-allow'); + if ($allow && $allow[0]['data'] != $r[0]['last-child']) { + $r = q("UPDATE `item` SET `last-child` = %d WHERE `uri` = '%s' LIMIT 1", + intval($allow[0]['data']), + dbesc($item_id) + ); + } + continue; + } + + $datarray = get_atom_elements($item); + $datarray['parent-uri'] = $item_id; + $datarray['contact-id'] = $contact['id']; + $r = post_remote($a, $datarray); + + continue; + } + } + + $r = q("UPDATE `contact` SET `last-update` = '%s' WHERE `id` = %d LIMIT 1", + dbesc(datetime_convert()), + intval($contact['id']) + ); +} + +killme(); diff --git a/include/rockstar.php b/include/rockstar.php index 58de4073..427fec68 100644 --- a/include/rockstar.php +++ b/include/rockstar.php @@ -1,165 +1,165 @@ set_baseurl(get_config('system','url')); +unset($db_host, $db_user, $db_pass, $db_data); - $u = q("SELECT * FROM `user` WHERE 1 LIMIT 1"); - if(! count($u)) - killme(); +require_once 'session.php'; +require_once 'datetime.php'; - $uid = $u[0]['uid']; - $nickname = $u[0]['nickname']; +$a->set_baseurl(get_config('system', 'url')); - $intros = q("SELECT `intro`.*, `intro`.`id` AS `intro_id`, `contact`.* +$u = q("SELECT * FROM `user` WHERE 1 LIMIT 1"); + +if (!count($u)) { + killme(); +} + +$uid = $u[0]['uid']; +$nickname = $u[0]['nickname']; + +$intros = q("SELECT `intro`.*, `intro`.`id` AS `intro_id`, `contact`.* FROM `intro` LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id` WHERE `intro`.`blocked` = 0 AND `intro`.`ignore` = 0"); - if(! count($intros)) - return; +if (!count($intros)) { + return; +} +foreach ($intros as $intro) { + $intro_id = intval($intro['intro_id']); - foreach($intros as $intro) { - - $intro_id = intval($intro['intro_id']); + $dfrn_id = $intro['issued-id']; + $contact_id = $intro['contact-id']; + $relation = $intro['rel']; + $site_pubkey = $intro['site-pubkey']; + $dfrn_confirm = $intro['confirm']; + $aes_allow = $intro['aes_allow']; - $dfrn_id = $intro['issued-id']; - $contact_id = $intro['contact-id']; - $relation = $intro['rel']; - $site_pubkey = $intro['site-pubkey']; - $dfrn_confirm = $intro['confirm']; - $aes_allow = $intro['aes_allow']; + $res = openssl_pkey_new(array( + 'digest_alg' => 'whirlpool', + 'private_key_bits' => 4096, + 'encrypt_key' => false)); - $res=openssl_pkey_new(array( - 'digest_alg' => 'whirlpool', - 'private_key_bits' => 4096, - 'encrypt_key' => false )); + $private_key = ''; - $private_key = ''; + openssl_pkey_export($res, $private_key); - openssl_pkey_export($res, $private_key); + $pubkey = openssl_pkey_get_details($res); + $public_key = $pubkey["key"]; - $pubkey = openssl_pkey_get_details($res); - $public_key = $pubkey["key"]; + $r = q("UPDATE `contact` SET `issued-pubkey` = '%s', `prvkey` = '%s' WHERE `id` = %d LIMIT 1", + dbesc($public_key), + dbesc($private_key), + intval($contact_id) + ); - $r = q("UPDATE `contact` SET `issued-pubkey` = '%s', `prvkey` = '%s' WHERE `id` = %d LIMIT 1", - dbesc($public_key), - dbesc($private_key), - intval($contact_id) - ); + $params = array(); - $params = array(); + $src_aes_key = random_string(); + $result = ""; - $src_aes_key = random_string(); - $result = ""; + openssl_private_encrypt($dfrn_id, $result, $u[0]['prvkey']); - openssl_private_encrypt($dfrn_id,$result,$u[0]['prvkey']); + $params['dfrn_id'] = $result; + $params['public_key'] = $public_key; - $params['dfrn_id'] = $result; - $params['public_key'] = $public_key; + $my_url = $a->get_baseurl() . '/profile/' . $nickname; - $my_url = $a->get_baseurl() . '/profile/' . $nickname ; - - openssl_public_encrypt($my_url, $params['source_url'], $site_pubkey); - - if($aes_allow && function_exists('openssl_encrypt')) { - openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey); - $params['public_key'] = openssl_encrypt($public_key,'AES-256-CBC',$src_aes_key); - } - - $res = post_url($dfrn_confirm,$params); - - $xml = simplexml_load_string($res); - $status = (int) $xml->status; - switch($status) { - case 0: - break; - case 1: - // birthday paradox - generate new dfrn-id and fall through. - - $new_dfrn_id = random_string(); - $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1", - dbesc($new_dfrn_id), - intval($contact_id) - ); - case 2: - break; - - case 3: - default: - break; - } - - if(($status == 0 || $status == 3) && ($intro_id)) { - - // delete the notification - - $r = q("DELETE FROM `intro` WHERE `id` = %d LIMIT 1", - intval($intro_id) - ); - } - if($status != 0) - killme(); - - require_once("Photo.php"); - - $photo_failure = false; - - - $filename = basename($intro['photo']); - $img_str = fetch_url($intro['photo'],true); - $img = new Photo($img_str); - if($img) { - - $img->scaleImageSquare(175); - $hash = hash('md5',uniqid(mt_rand(),true)); - - $r = $img->store($contact_id, $hash, $filename, t('Contact Photos'), 4 ); - - if($r === false) - $photo_failure = true; - $img->scaleImage(80); - - $r = $img->store($contact_id, $hash, $filename, t('Contact Photos'), 5 ); - - if($r === false) - $photo_failure = true; - - $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg'; - $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg'; - } - else - $photo_failure = true; - - if($photo_failure) { - $photo = $a->get_baseurl() . '/images/default-profile.jpg'; - $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg'; - } - - $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `rel` = %d, - `name-date` = '%s', `uri-date` = '%s', `avatar-date` = '%s', - `readonly` = %d, `profile-id` = %d, `blocked` = 0, `pending` = 0, - `network` = 'dfrn' WHERE `id` = %d LIMIT 1", - dbesc($photo), - dbesc($thumb), - intval(($relation == DIRECTION_OUT) ? DIRECTION_BOTH : DIRECTION_IN), - dbesc(datetime_convert()), - dbesc(datetime_convert()), - dbesc(datetime_convert()), - intval((x($a->config,'rockstar-readonly')) ? $a->config['rockstar-readonly'] : 0), - intval((x($a->config,'rockstar-profile')) ? $a->config['rockstar-profile'] : 0), - intval($contact_id) - ); + openssl_public_encrypt($my_url, $params['source_url'], $site_pubkey); + if ($aes_allow && function_exists('openssl_encrypt')) { + openssl_public_encrypt($src_aes_key, $params['aes_key'], $site_pubkey); + $params['public_key'] = openssl_encrypt($public_key, 'AES-256-CBC', $src_aes_key); } - killme(); + + $res = post_url($dfrn_confirm, $params); + + $xml = simplexml_load_string($res); + $status = (int) $xml->status; + switch ($status) { + case 0: + break; + case 1: + // birthday paradox - generate new dfrn-id and fall through. + $new_dfrn_id = random_string(); + $r = q("UPDATE contact SET `issued-id` = '%s' WHERE `id` = %d LIMIT 1", + dbesc($new_dfrn_id), + intval($contact_id) + ); + case 2: + break; + + case 3: + default: + break; + } + + if (($status == 0 || $status == 3) && ($intro_id)) { + // delete the notification + $r = q("DELETE FROM `intro` WHERE `id` = %d LIMIT 1", intval($intro_id)); + } + + if ($status != 0) { + killme(); + } + + require_once 'Photo.php'; + + $photo_failure = false; + + $filename = basename($intro['photo']); + $img_str = fetch_url($intro['photo'], true); + $img = new Photo($img_str); + + if ($img) { + $img->scaleImageSquare(175); + $hash = hash('md5', uniqid(mt_rand(), true)); + + $r = $img->store($contact_id, $hash, $filename, t('Contact Photos'), 4); + + if ($r === false) { + $photo_failure = true; + } + $img->scaleImage(80); + + $r = $img->store($contact_id, $hash, $filename, t('Contact Photos'), 5); + + if ($r === false) { + $photo_failure = true; + } + + $photo = $a->get_baseurl() . '/photo/' . $hash . '-4.jpg'; + $thumb = $a->get_baseurl() . '/photo/' . $hash . '-5.jpg'; + } else { + $photo_failure = true; + } + + if ($photo_failure) { + $photo = $a->get_baseurl() . '/images/default-profile.jpg'; + $thumb = $a->get_baseurl() . '/images/default-profile-sm.jpg'; + } + + $r = q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `rel` = %d, + `name-date` = '%s', `uri-date` = '%s', `avatar-date` = '%s', + `readonly` = %d, `profile-id` = %d, `blocked` = 0, `pending` = 0, + `network` = 'dfrn' WHERE `id` = %d LIMIT 1", + dbesc($photo), + dbesc($thumb), + intval(($relation == DIRECTION_OUT) ? DIRECTION_BOTH : DIRECTION_IN), + dbesc(datetime_convert()), + dbesc(datetime_convert()), + dbesc(datetime_convert()), + intval((x($a->config, 'rockstar-readonly')) ? $a->config['rockstar-readonly'] : 0), + intval((x($a->config, 'rockstar-profile')) ? $a->config['rockstar-profile'] : 0), + intval($contact_id) + ); +} +killme(); diff --git a/include/sync.php b/include/sync.php index ac0e8785..fd9c0840 100644 --- a/include/sync.php +++ b/include/sync.php @@ -7,14 +7,12 @@ */ function sync_pull($url) { - - global $a; - - //If we support it that is. - if($a->config['syncing']['enable_pulling']){ - q("INSERT INTO `sync-pull-queue` (`url`) VALUES ('%s')", dbesc($url)); - } - + global $a; + + //If we support it that is. + if ($a->config['syncing']['enable_pulling']) { + q("INSERT INTO `sync-pull-queue` (`url`) VALUES ('%s')", dbesc($url)); + } } /** @@ -24,16 +22,14 @@ function sync_pull($url) */ function sync_push($url) { - - global $a; - - //If we support it that is. - if($a->config['syncing']['enable_pushing']){ - q("INSERT INTO `sync-push-queue` (`url`) VALUES ('%s')", dbesc($url)); - } - - sync_mark($url); - + global $a; + + //If we support it that is. + if ($a->config['syncing']['enable_pushing']) { + q("INSERT INTO `sync-push-queue` (`url`) VALUES ('%s')", dbesc($url)); + } + + sync_mark($url); } /** @@ -44,21 +40,20 @@ function sync_push($url) */ function sync_mark($url) { - - global $a; - - //If we support it that is. - if(!$a->config['syncing']['enable_pulling']){ - return; - } - - $exists = count(q("SELECT * FROM `sync-timestamps` WHERE `url`='%s'", dbesc($url))); - - if(!$exists) - q("INSERT INTO `sync-timestamps` (`url`, `modified`) VALUES ('%s', NOW())", dbesc($url)); - else - q("UPDATE `sync-timestamps` SET `modified`=NOW() WHERE `url`='%s'", dbesc($url)); - + global $a; + + //If we support it that is. + if (!$a->config['syncing']['enable_pulling']) { + return; + } + + $exists = count(q("SELECT * FROM `sync-timestamps` WHERE `url`='%s'", dbesc($url))); + + if (!$exists) { + q("INSERT INTO `sync-timestamps` (`url`, `modified`) VALUES ('%s', NOW())", dbesc($url)); + } else { + q("UPDATE `sync-timestamps` SET `modified`=NOW() WHERE `url`='%s'", dbesc($url)); + } } /** @@ -70,27 +65,26 @@ function sync_mark($url) */ function push_worker($target, $batch) { - - //Lets be nice, we're only doing a background job here... - pcntl_setpriority(5); - - //Find our target's submit URL. - $submit = $target['base_url'].'/submit'; - - foreach($batch as $item){ - set_time_limit(30); //This should work for 1 submit. - msg("Submitting {$item['url']} to $submit"); - fetch_url($submit.'?url='.bin2hex($item['url'])); - } - + //Lets be nice, we're only doing a background job here... + pcntl_setpriority(5); + + //Find our target's submit URL. + $submit = $target['base_url'] . '/submit'; + + foreach ($batch as $item) { + set_time_limit(30); //This should work for 1 submit. + msg("Submitting {$item['url']} to $submit"); + fetch_url($submit . '?url=' . bin2hex($item['url'])); + } } /** * Gets an array of push targets. * @return array Push targets. */ -function get_push_targets(){ - return q("SELECT * FROM `sync-targets` WHERE `push`=b'1'"); +function get_push_targets() +{ + return q("SELECT * FROM `sync-targets` WHERE `push`=b'1'"); } /** @@ -98,8 +92,9 @@ function get_push_targets(){ * @param object $a The App instance. * @return array Batch of URL's. */ -function get_push_batch($a){ - return q("SELECT * FROM `sync-push-queue` LIMIT %u", intval($a->config['syncing']['max_push_items'])); +function get_push_batch($a) +{ + return q("SELECT * FROM `sync-push-queue` LIMIT %u", intval($a->config['syncing']['max_push_items'])); } /** @@ -109,35 +104,31 @@ function get_push_batch($a){ */ function get_pushing_job($a) { - - //When pushing is requested... - if(!!$a->config['syncing']['enable_pushing']){ - - //Find our targets. - $targets = get_push_targets(); - - //No targets? - if(!count($targets)){ - msg('Pushing enabled, but no push targets.'); - $batch = array(); - } - - //If we have targets, get our batch. - else{ - $batch = get_push_batch($a); - if(!count($batch)) msg('Empty pushing queue.'); //No batch, means no work. - } - - } + //When pushing is requested... + if (!!$a->config['syncing']['enable_pushing']) { - //No pushing if it's disabled. - else{ - $targets = array(); - $batch = array(); - } - - return array($targets, $batch); - + //Find our targets. + $targets = get_push_targets(); + + //No targets? + if (!count($targets)) { + msg('Pushing enabled, but no push targets.'); + $batch = array(); + } + + //If we have targets, get our batch. + else { + $batch = get_push_batch($a); + if (!count($batch)) + msg('Empty pushing queue.'); //No batch, means no work. + } + } else { + //No pushing if it's disabled. + $targets = array(); + $batch = array(); + } + + return array($targets, $batch); } /** @@ -153,74 +144,76 @@ function get_pushing_job($a) */ function run_pushing_job($targets, $batch, $db_host, $db_user, $db_pass, $db_data, $install) { - - //Create a thread for each target we want to serve push messages to. - //Not good creating more, because it would stress their server too much. - $threadc = count($targets); - $threads = array(); - - //Do we only have 1 target? No need for threads. - if($threadc === 1){ - msg('No threads needed. Only one pushing target.'); - push_worker($targets[0], $batch); - } - - //When we need threads. - elseif($threadc > 1){ - - //POSIX threads only. - if(!function_exists('pcntl_fork')){ - msg('Error: no pcntl_fork support. Are you running a different OS? Report an issue please.', true); - } - - //Debug... - $items = count($batch); - msg("Creating $threadc push threads for $items items."); - - //Loop while we need more threads. - for($i = 0; $i < $threadc; $i++){ - - $pid = pcntl_fork(); - if($pid === -1) msg('Error: something went wrong with the fork. '.pcntl_strerror(), true); - - //You're a child, go do some labor! - if($pid === 0){push_worker($targets[$i], $batch); exit;} - - //Store the list of PID's. - if($pid > 0) $threads[] = $pid; - - } - - } - - //Wait for all child processes. - $theading_problems = false; - foreach($threads as $pid){ - pcntl_waitpid($pid, $status); - if($status !== 0){ - $theading_problems = true; - msg("Bad process return value $pid:$status"); - } - } - - //If we did not have any "threading" problems. - if(!$theading_problems){ - - //Reconnect - global $db; - $db = new dba($db_host, $db_user, $db_pass, $db_data, $install); - - //Create a query for deleting this queue. - $where = array(); - foreach($batch as $item) $where[] = dbesc($item['url']); - $where = "WHERE `url` IN ('".implode("', '", $where)."')"; - - //Remove the items from queue. - q("DELETE FROM `sync-push-queue` $where LIMIT %u", count($batch)); - msg('Removed items from push queue.'); - - } - + //Create a thread for each target we want to serve push messages to. + //Not good creating more, because it would stress their server too much. + $threadc = count($targets); + $threads = array(); + + //Do we only have 1 target? No need for threads. + if ($threadc === 1) { + msg('No threads needed. Only one pushing target.'); + push_worker($targets[0], $batch); + } elseif ($threadc > 1) { + //When we need threads. + + //POSIX threads only. + if (!function_exists('pcntl_fork')) { + msg('Error: no pcntl_fork support. Are you running a different OS? Report an issue please.', true); + } + + //Debug... + $items = count($batch); + msg("Creating $threadc push threads for $items items."); + + //Loop while we need more threads. + for ($i = 0; $i < $threadc; $i++) { + $pid = pcntl_fork(); + if ($pid === -1) + msg('Error: something went wrong with the fork. ' . pcntl_strerror(), true); + + //You're a child, go do some labor! + if ($pid === 0) { + push_worker($targets[$i], $batch); + exit; + } + + //Store the list of PID's. + if ($pid > 0) { + $threads[] = $pid; + } + } + } + + //Wait for all child processes. + $theading_problems = false; + + foreach ($threads as $pid) { + pcntl_waitpid($pid, $status); + + if ($status !== 0) { + $theading_problems = true; + msg("Bad process return value $pid:$status"); + } + } + + //If we did not have any "threading" problems. + if (!$theading_problems) { + //Reconnect + global $db; + + $db = new dba($db_host, $db_user, $db_pass, $db_data, $install); + + //Create a query for deleting this queue. + $where = array(); + foreach ($batch as $item) { + $where[] = dbesc($item['url']); + } + $where = "WHERE `url` IN ('" . implode("', '", $where) . "')"; + + //Remove the items from queue. + q("DELETE FROM `sync-push-queue` $where LIMIT %u", count($batch)); + msg('Removed items from push queue.'); + } } /** @@ -228,19 +221,21 @@ function run_pushing_job($targets, $batch, $db_host, $db_user, $db_pass, $db_dat * @param object $a The App instance. * @return array Batch of URL's. */ -function get_queued_pull_batch($a){ - //Randomize this, to prevent scraping the same servers too much or dead URL's. - $batch = q("SELECT * FROM `sync-pull-queue` ORDER BY RAND() LIMIT %u", intval($a->config['syncing']['max_pull_items'])); - msg(sprintf('Pulling %u items from queue.', count($batch))); - return $batch; +function get_queued_pull_batch($a) +{ + //Randomize this, to prevent scraping the same servers too much or dead URL's. + $batch = q("SELECT * FROM `sync-pull-queue` ORDER BY RAND() LIMIT %u", intval($a->config['syncing']['max_pull_items'])); + msg(sprintf('Pulling %u items from queue.', count($batch))); + return $batch; } /** * Gets an array of pull targets. * @return array Pull targets. */ -function get_pull_targets(){ - return q("SELECT * FROM `sync-targets` WHERE `pull`=b'1'"); +function get_pull_targets() +{ + return q("SELECT * FROM `sync-targets` WHERE `pull`=b'1'"); } /** @@ -250,57 +245,59 @@ function get_pull_targets(){ */ function get_remote_pull_batch($a) { - - //Find our targets. - $targets = get_pull_targets(); - - msg(sprintf('Pulling from %u remote targets.', count($targets))); - - //No targets, means no batch. - if(!count($targets)) - return array(); - - //Pull a list of URL's from each target. - $urls = array(); - foreach($targets as $i => $target){ - - //First pull, or an update? - if(!$target['dt_last_pull']) - $url = $target['base_url'].'/sync/pull/all'; - else - $url = $target['base_url'].'/sync/pull/since/'.intval($target['dt_last_pull']); - - //Go for it :D - $targets[$i]['pull_data'] = json_decode(fetch_url($url), true); - - //If we didn't get any JSON. - if(!$targets[$i]['pull_data']){ - msg(sprintf('Failed to pull from "%s".', $url)); - continue; - } - - //Add all entries as keys, to remove duplicates. - foreach($targets[$i]['pull_data']['results'] as $url) - $urls[$url]=true; - - } - - //Now that we have our URL's. Store them in the queue. - foreach($urls as $url=>$bool){ - if($url) sync_pull($url); - } - - //Since this all worked out, mark each source with the timestamp of pulling. - foreach($targets as $target){ - if($target['pull_data'] && $target['pull_data']['now']){ - msg('New pull timestamp '.$target['pull_data']['now'].' for '.$target['base_url']); - q("UPDATE `sync-targets` SET `dt_last_pull`=%u WHERE `base_url`='%s'", $target['pull_data']['now'], dbesc($target['base_url'])); - } - } - - //Finally, return a batch of this. - return get_queued_pull_batch($a); - + //Find our targets. + $targets = get_pull_targets(); + + msg(sprintf('Pulling from %u remote targets.', count($targets))); + + //No targets, means no batch. + if (!count($targets)) { + return array(); + } + + //Pull a list of URL's from each target. + $urls = array(); + + foreach ($targets as $i => $target) { + //First pull, or an update? + if (!$target['dt_last_pull']) { + $url = $target['base_url'] . '/sync/pull/all'; + } else { + $url = $target['base_url'] . '/sync/pull/since/' . intval($target['dt_last_pull']); + } + + //Go for it :D + $targets[$i]['pull_data'] = json_decode(fetch_url($url), true); + + //If we didn't get any JSON. + if (!$targets[$i]['pull_data']) { + msg(sprintf('Failed to pull from "%s".', $url)); + continue; + } + + //Add all entries as keys, to remove duplicates. + foreach ($targets[$i]['pull_data']['results'] as $url) { + $urls[$url] = true; + } + } + + //Now that we have our URL's. Store them in the queue. + foreach ($urls as $url => $bool) { + if ($url) { + sync_pull($url); + } + } + + //Since this all worked out, mark each source with the timestamp of pulling. + foreach ($targets as $target) { + if ($target['pull_data'] && $target['pull_data']['now']) { + msg('New pull timestamp ' . $target['pull_data']['now'] . ' for ' . $target['base_url']); + q("UPDATE `sync-targets` SET `dt_last_pull`=%u WHERE `base_url`='%s'", $target['pull_data']['now'], dbesc($target['base_url'])); + } + } + + //Finally, return a batch of this. + return get_queued_pull_batch($a); } /** @@ -310,19 +307,22 @@ function get_remote_pull_batch($a) */ function get_pulling_job($a) { - - //No pulling today... - if(!$a->config['syncing']['enable_pulling']) - return array(); - - //Firstly, finish the items from our queue. - $batch = get_queued_pull_batch($a); - if(count($batch)) return $batch; - - //If that is empty, fill the queue with remote items and return a batch of that. - $batch = get_remote_pull_batch($a); - if(count($batch)) return $batch; - + //No pulling today... + if (!$a->config['syncing']['enable_pulling']) { + return array(); + } + + //Firstly, finish the items from our queue. + $batch = get_queued_pull_batch($a); + if (count($batch)) { + return $batch; + } + + //If that is empty, fill the queue with remote items and return a batch of that. + $batch = get_remote_pull_batch($a); + if (count($batch)) { + return $batch; + } } /** @@ -340,30 +340,28 @@ function get_pulling_job($a) */ function pull_worker($i, $threadc, $pull_batch, $db_host, $db_user, $db_pass, $db_data, $install) { - - //Lets be nice, we're only doing maintenance here... - pcntl_setpriority(5); - - //Get personal DBA's. - global $db; - $db = new dba($db_host, $db_user, $db_pass, $db_data, $install); - - //Get our (round-robin) workload from the batch. - $workload = array(); - while(isset($pull_batch[$i])){ - $entry = $pull_batch[$i]; - $workload[] = $entry; - $i+=$threadc; - } - - //While we've got work to do. - while(count($workload)){ - $entry = array_pop($workload); - set_time_limit(20); //This should work for 1 submit. - msg("Submitting ".$entry['url']); - run_submit($entry['url']); - } - + //Lets be nice, we're only doing maintenance here... + pcntl_setpriority(5); + + //Get personal DBA's. + global $db; + $db = new dba($db_host, $db_user, $db_pass, $db_data, $install); + + //Get our (round-robin) workload from the batch. + $workload = array(); + while (isset($pull_batch[$i])) { + $entry = $pull_batch[$i]; + $workload[] = $entry; + $i += $threadc; + } + + //While we've got work to do. + while (count($workload)) { + $entry = array_pop($workload); + set_time_limit(20); //This should work for 1 submit. + msg("Submitting " . $entry['url']); + run_submit($entry['url']); + } } /** @@ -379,62 +377,66 @@ function pull_worker($i, $threadc, $pull_batch, $db_host, $db_user, $db_pass, $d */ function run_pulling_job($a, $pull_batch, $db_host, $db_user, $db_pass, $db_data, $install) { - - //We need the scraper. - require_once('include/submit.php'); - - //POSIX threads only. - if(!function_exists('pcntl_fork')){ - msg('Error: no pcntl_fork support. Are you running a different OS? Report an issue please.', true); - } - - //Create the threads we need. - $items = count($pull_batch); - $threadc = min($a->config['syncing']['pulling_threads'], $items); //Don't need more threads than items. - $threads = array(); - - msg("Creating $threadc pulling threads for $items profiles."); - - //Build the threads. - for($i = 0; $i < $threadc; $i++){ - - $pid = pcntl_fork(); - if($pid === -1) msg('Error: something went wrong with the fork. '.pcntl_strerror(), true); - - //You're a child, go do some labor! - if($pid === 0){pull_worker($i, $threadc, $pull_batch, $db_host, $db_user, $db_pass, $db_data, $install); exit;} - - //Store the list of PID's. - if($pid > 0) $threads[] = $pid; - - } - - //Wait for all child processes. - $theading_problems = false; - foreach($threads as $pid){ - pcntl_waitpid($pid, $status); - if($status !== 0){ - $theading_problems = true; - msg("Bad process return value $pid:$status"); - } - } - - //If we did not have any "threading" problems. - if(!$theading_problems){ - - //Reconnect - global $db; - $db = new dba($db_host, $db_user, $db_pass, $db_data, $install); - - //Create a query for deleting this queue. - $where = array(); - foreach($pull_batch as $item) $where[] = dbesc($item['url']); - $where = "WHERE `url` IN ('".implode("', '", $where)."')"; - - //Remove the items from queue. - q("DELETE FROM `sync-pull-queue` $where LIMIT %u", count($pull_batch)); - msg('Removed items from pull queue.'); - - } - + //We need the scraper. + require_once 'include/submit.php'; + + //POSIX threads only. + if (!function_exists('pcntl_fork')) { + msg('Error: no pcntl_fork support. Are you running a different OS? Report an issue please.', true); + } + + //Create the threads we need. + $items = count($pull_batch); + $threadc = min($a->config['syncing']['pulling_threads'], $items); //Don't need more threads than items. + $threads = array(); + + msg("Creating $threadc pulling threads for $items profiles."); + + //Build the threads. + for ($i = 0; $i < $threadc; $i++) { + $pid = pcntl_fork(); + if ($pid === -1) { + msg('Error: something went wrong with the fork. ' . pcntl_strerror(), true); + } + + //You're a child, go do some labor! + if ($pid === 0) { + pull_worker($i, $threadc, $pull_batch, $db_host, $db_user, $db_pass, $db_data, $install); + exit; + } + + //Store the list of PID's. + if ($pid > 0) { + $threads[] = $pid; + } + } + + //Wait for all child processes. + $theading_problems = false; + foreach ($threads as $pid) { + pcntl_waitpid($pid, $status); + if ($status !== 0) { + $theading_problems = true; + msg("Bad process return value $pid:$status"); + } + } + + //If we did not have any "threading" problems. + if (!$theading_problems) { + //Reconnect + global $db; + + $db = new dba($db_host, $db_user, $db_pass, $db_data, $install); + + //Create a query for deleting this queue. + $where = array(); + foreach ($pull_batch as $item) { + $where[] = dbesc($item['url']); + } + $where = "WHERE `url` IN ('" . implode("', '", $where) . "')"; + + //Remove the items from queue. + q("DELETE FROM `sync-pull-queue` $where LIMIT %u", count($pull_batch)); + msg('Removed items from pull queue.'); + } } diff --git a/index.php b/index.php index 2c6086dd..e3568349 100644 --- a/index.php +++ b/index.php @@ -5,8 +5,8 @@ require_once 'boot.php'; $a = new App; -error_reporting(E_ERROR | E_WARNING | E_PARSE ); -error_reporting(E_ALL ); +error_reporting(E_ERROR | E_WARNING | E_PARSE); +error_reporting(E_ALL); ini_set('error_log', 'php.out'); ini_set('log_errors', '1'); ini_set('display_errors', '0'); @@ -18,7 +18,7 @@ require_once '.htconfig.php'; require_once 'dba.php'; $db = new dba($db_host, $db_user, $db_pass, $db_data); - unset($db_host, $db_user, $db_pass, $db_data); +unset($db_host, $db_user, $db_pass, $db_data); $a->init_pagehead(); $a->page['aside'] = '
Your friends. Your web.
'; @@ -43,35 +43,35 @@ if (strlen($a->module)) { include("mod/{$a->module}.php"); $a->module_loaded = true; } - if (! $a->module_loaded) { + + if (!$a->module_loaded) { if ((x($_SERVER, 'QUERY_STRING')) && ($_SERVER['QUERY_STRING'] === 'q=internal_error.html') && isset($dreamhost_error_hack)) { goaway($a->get_baseurl() . $_SERVER['REQUEST_URI']); } header($_SERVER['SERVER_PROTOCOL'] . ' 404 ' . t('Not Found')); - notice(t('Page not found' ) . EOL); + notice(t('Page not found') . EOL); } } if ($a->module_loaded) { $a->page['page_title'] = $a->module; + if (function_exists($a->module . '_init')) { $func = $a->module . '_init'; $func($a); } - if (($_SERVER['REQUEST_METHOD'] == 'POST') && (! $a->error) - && (function_exists($a->module . '_post')) - && (! x($_POST, 'auth-params'))) { + if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!$a->error) && (function_exists($a->module . '_post')) && (!x($_POST, 'auth-params'))) { $func = $a->module . '_post'; $func($a); } - if ((! $a->error) && (function_exists($a->module . '_afterpost'))) { + if ((!$a->error) && (function_exists($a->module . '_afterpost'))) { $func = $a->module . '_afterpost'; $func($a); } - if ((! $a->error) && (function_exists($a->module . '_content'))) { + if ((!$a->error) && (function_exists($a->module . '_content'))) { $func = $a->module . '_content'; $a->page['content'] = $func($a); } @@ -83,9 +83,11 @@ if (x($_SESSION, 'sysmsg')) { if (stristr($_SESSION['sysmsg'], t('Permission denied'))) { header($_SERVER['SERVER_PROTOCOL'] . ' 403 ' . t('Permission denied.')); } + if (!isset($a->page['content'])) { $a->page['content'] = ''; } + $a->page['content'] = '
' . $_SESSION['sysmsg'] . '
' . PHP_EOL . $a->page['content']; unset($_SESSION['sysmsg']); @@ -97,17 +99,21 @@ $a->page['htmlhead'] = replace_macros($a->page['htmlhead'], array( '$stylesheet' => $a->get_baseurl() . '/view/theme/' . ((x($_SESSION, 'theme')) ? $_SESSION['theme'] : 'default') . '/style.css' -)); + )); -$page = $a->page; +$page = $a->page; $profile = $a->profile; header('Content-type: text/html; charset=utf-8'); + $template = 'view/' . ((x($a->page, 'template')) ? $a->page['template'] : 'default' ) . '.php'; require_once $template; + session_write_close(); + closedb(); + exit; diff --git a/mod/photo.php b/mod/photo.php index a347ac09..f0654040 100644 --- a/mod/photo.php +++ b/mod/photo.php @@ -2,7 +2,8 @@ require_once 'datetime.php'; -function photo_init(App $a) { +function photo_init(App $a) +{ switch ($a->argc) { case 2: $photo = $a->argv[1]; @@ -14,13 +15,13 @@ function photo_init(App $a) { $profile_id = str_replace('.jpg', '', $photo); - $r = q('SELECT * FROM `photo` WHERE `profile-id` = %d LIMIT 1', - intval($profile_id) - ); + $r = q('SELECT * FROM `photo` WHERE `profile-id` = %d LIMIT 1', intval($profile_id)); + if (count($r)) { $data = $r[0]['data']; } - if (x($data) === false || (! strlen($data))) { + + if (x($data) === false || (!strlen($data))) { $data = file_get_contents('images/default-profile-sm.jpg'); } @@ -29,9 +30,10 @@ function photo_init(App $a) { //Try and cache our result. $etag = md5($data); - header('Etag: '.$etag); + header('Etag: ' . $etag); header('Expires: ' . datetime_convert('UTC', 'UTC', 'now + 1 week', 'D, d M Y H:i:s' . ' GMT')); - header('Cache-Control: max-age=' . intval(7*24*3600)); + header('Cache-Control: max-age=' . intval(7 * 24 * 3600)); + if (function_exists('header_remove')) { header_remove('Pragma'); header_remove('pragma'); @@ -42,7 +44,7 @@ function photo_init(App $a) { exit; } - header('Content-type: image/jpeg'); + header('Content-type: image/jpeg'); echo $data; exit; } diff --git a/mod/search.php b/mod/search.php index f8db4a2b..e964d9d0 100644 --- a/mod/search.php +++ b/mod/search.php @@ -4,90 +4,93 @@ use Friendica\Directory\Rendering\View; use Friendica\Directory\Helper\Search as SearchHelper; use Friendica\Directory\Helper\Profile as ProfileHelper; -function search_content(&$a) { - +function search_content(App $a) +{ //Filters $community = null; $filter = null; - if($a->argc >= 2){ - + + if ($a->argc >= 2) { $filter = $a->argv[1]; - switch($filter){ - + switch ($filter) { + case 'forums': $community = 1; break; - + case 'people': $community = 0; break; - + default: $community = null; $filter = null; break; - } - } $alpha = false; - if(x($_GET, 'alpha') == 1) + if (x($_GET, 'alpha') == 1) $alpha = true; //Query - $search = ((x($_GET,'query')) ? notags(trim($_GET['query'])) : ''); - - if(empty($search)){ + $search = ((x($_GET, 'query')) ? notags(trim($_GET['query'])) : ''); + + if (empty($search)) { goaway('/home'); } - - if($search) + + if ($search) { $alpha = true; - + } + //Run our query. - if($search) + if ($search) { $search = dbesc($search . '*'); - $sql_extra = ((strlen($search)) ? " AND MATCH (`name`, `pdesc`, `homepage`, `locality`, `region`, `country-name`, `tags` ) + } + + $sql_extra = ((strlen($search)) ? " AND MATCH (`name`, `pdesc`, `homepage`, `locality`, `region`, `country-name`, `tags` ) AGAINST ('$search' IN BOOLEAN MODE) " : ""); - - if(!is_null($community)) - $sql_extra .= " and comm=".intval($community)." "; - - $sql_extra = str_replace('%','%%',$sql_extra); - + + if (!is_null($community)) { + $sql_extra .= " and comm=" . intval($community) . " "; + } + + $sql_extra = str_replace('%', '%%', $sql_extra); + $total = 0; $r = q("SELECT COUNT(*) AS `total` FROM `profile` WHERE `censored` = 0 $sql_extra "); - if(count($r)){ + if (count($r)) { $total = $r[0]['total']; $a->set_pager_total($r[0]['total']); } - if($alpha) + if ($alpha) { $order = " order by name asc "; - else + } else { $order = " order by updated desc, id desc "; - + } $r = q("SELECT * FROM `profile` WHERE `censored` = 0 $sql_extra $order LIMIT %d , %d ", intval($a->pager['start']), intval($a->pager['itemspage']) ); - + //Show results. - $view = new View('search'); - - $view->addHelper('paginate', function()use($a){return paginate($a);}); - $view->addHelper('photoUrl', ProfileHelper::get('photoUrl')); - $view->addHelper('filterAllUrl', SearchHelper::get('filterAllUrl')); - $view->addHelper('filterPeopleUrl', SearchHelper::get('filterPeopleUrl')); - $view->addHelper('filterForumsUrl', SearchHelper::get('filterForumsUrl')); - - $view->output(array( - 'total' => number_format($total), - 'results' => $r, - 'filter' => $filter, - 'query' => x($_GET,'query') ? $_GET['query'] : '' - )); - -} \ No newline at end of file + $view = new View('search'); + + $view->addHelper('paginate', function() use ($a) { + return paginate($a); + }); + $view->addHelper('photoUrl', ProfileHelper::get('photoUrl')); + $view->addHelper('filterAllUrl', SearchHelper::get('filterAllUrl')); + $view->addHelper('filterPeopleUrl', SearchHelper::get('filterPeopleUrl')); + $view->addHelper('filterForumsUrl', SearchHelper::get('filterForumsUrl')); + + $view->output(array( + 'total' => number_format($total), + 'results' => $r, + 'filter' => $filter, + 'query' => x($_GET, 'query') ? $_GET['query'] : '' + )); +} diff --git a/mod/servers.php b/mod/servers.php index f2ebda74..bdd300d1 100644 --- a/mod/servers.php +++ b/mod/servers.php @@ -2,7 +2,7 @@ use Friendica\Directory\Rendering\View; -require_once('include/site-health.php'); +require_once 'include/site-health.php'; function servers_content(&$a) { diff --git a/src/Helper/BaseHelper.php b/src/Helper/BaseHelper.php index a6661f2b..35fa0c47 100644 --- a/src/Helper/BaseHelper.php +++ b/src/Helper/BaseHelper.php @@ -1,4 +1,6 @@ -{$name}; - } - - /** - * A reference to the global App. - * @var \App - */ - protected $app; - - public function __construct() - { - global $a; - $this->app = $a; - } - - //Provides access to a wrapper for your helper functions. - public function __get($name) - { - - if(!method_exists($this, $name)){ - throw new OutOfBoundsException("Helper method '$name' does not exist on ".get_class($this)); - } - - $helper = $this; - $method = new ReflectionMethod($this, $name); - return function()use($method, $helper){ - $arguments = func_get_args(); - return $method->invokeArgs($helper, $arguments); - }; - - } - -} \ No newline at end of file + /** + * A reference to the global App. + * @var \App + */ + protected $app; + + public static function get($name) + { + $helper = new static(); + return $helper->{$name}; + } + + public function __construct() + { + global $a; + $this->app = $a; + } + + //Provides access to a wrapper for your helper functions. + public function __get($name) + { + if (!method_exists($this, $name)) { + throw new OutOfBoundsException("Helper method '$name' does not exist on " . get_class($this)); + } + + $helper = $this; + $method = new ReflectionMethod($this, $name); + return function()use($method, $helper) { + $arguments = func_get_args(); + return $method->invokeArgs($helper, $arguments); + }; + } +}