aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfriendica <info@friendica.com>2015-02-02 20:13:07 -0800
committerfriendica <info@friendica.com>2015-02-02 20:13:07 -0800
commit1807db6cb0cc297aff32750b371a4e2e6539ad31 (patch)
treed8045e14bfa0ddf0760a8ac90a284fdd9eb4ca10
parentfa710106e5f97744e43a61a7e9ef5a1b993261ee (diff)
downloadvolse-hubzilla-1807db6cb0cc297aff32750b371a4e2e6539ad31.tar.gz
volse-hubzilla-1807db6cb0cc297aff32750b371a4e2e6539ad31.tar.bz2
volse-hubzilla-1807db6cb0cc297aff32750b371a4e2e6539ad31.zip
send rating information to directories
-rw-r--r--include/notifier.php14
-rw-r--r--include/ratenotif.php124
-rw-r--r--include/zot.php39
-rw-r--r--mod/connedit.php6
-rw-r--r--mod/prate.php2
5 files changed, 157 insertions, 28 deletions
diff --git a/include/notifier.php b/include/notifier.php
index edb2f1946..fe6ac33c0 100644
--- a/include/notifier.php
+++ b/include/notifier.php
@@ -297,15 +297,6 @@ function notifier_run($argv, $argc){
$private = false;
$packet_type = 'purge';
}
- elseif($cmd === 'rating') {
- $r = q("select * from xlink where xlink_id = %d and xlink_static = 1 limit 1",
- intval($item_id)
- );
- if($r) {
- logger('rating message: ' . print_r($r[0],true));
- return;
- }
- }
else {
// Normal items
@@ -483,11 +474,6 @@ function notifier_run($argv, $argc){
// Now we have collected recipients (except for external mentions, FIXME)
// Let's reduce this to a set of hubs.
-
- // for public posts always include our own hub
-// this shouldn't be needed any more. collect_recipients should take care of it.
-// $sql_extra = (($private) ? "" : " or hubloc_url = '" . dbesc(z_root()) . "' ");
-
logger('notifier: hub choice: ' . intval($relay_to_owner) . ' ' . intval($private) . ' ' . $cmd, LOGGER_DEBUG);
if($relay_to_owner && (! $private) && ($cmd !== 'relay')) {
diff --git a/include/ratenotif.php b/include/ratenotif.php
new file mode 100644
index 000000000..4fa0077a6
--- /dev/null
+++ b/include/ratenotif.php
@@ -0,0 +1,124 @@
+<?php
+
+require_once('include/cli_startup.php');
+require_once('include/zot.php');
+require_once('include/queue_fn.php');
+
+
+function ratenotif_run($argv, $argc){
+
+ cli_startup();
+
+ $a = get_app();
+
+ require_once("session.php");
+ require_once("datetime.php");
+ require_once('include/items.php');
+ require_once('include/Contact.php');
+
+ if($argc < 3)
+ return;
+
+
+ logger('ratenotif: invoked: ' . print_r($argv,true), LOGGER_DEBUG);
+
+ $cmd = $argv[1];
+
+ $item_id = $argv[2];
+
+
+ if($cmd === 'rating') {
+ $r = q("select * from xlink where xlink_id = %d and xlink_static = 1 limit 1",
+ intval($item_id)
+ );
+ if(! $r) {
+ logger('rating not found');
+ return;
+ }
+
+ $encoded_item = array(
+ 'type' => 'rating',
+ 'encoding' => 'zot',
+ 'target' => $r[0]['xlink_link'],
+ 'rating' => intval($r[0]['xlink_rating']),
+ 'rating_text' => $r[0]['xlink_rating_text'],
+ 'signature' => $r[0]['xlink_sig'],
+ 'edited' => $r[0]['xlink_updated']
+ );
+ }
+
+ $channel = channelx_by_hash($r[0]['xlink_xchan']);
+ if(! $channel) {
+ logger('no channel');
+ return;
+ }
+
+
+ $primary = get_directory_primary();
+
+ if(! $primary)
+ return;
+
+
+ $interval = ((get_config('system','delivery_interval') !== false)
+ ? intval(get_config('system','delivery_interval')) : 2 );
+
+ $deliveries_per_process = intval(get_config('system','delivery_batch_count'));
+
+ if($deliveries_per_process <= 0)
+ $deliveries_per_process = 1;
+
+ $deliver = array();
+
+ $x = z_fetch_url($primary . '/regdir');
+ if($x['success']) {
+ $j = json_decode($x['body'],true);
+ if($j && $j['success'] && is_array($j['directories'])) {
+
+ foreach($j['directories'] as $h) {
+// if($h == z_root())
+// continue;
+
+ $hash = random_string();
+ $n = zot_build_packet($channel,'notify',null,null,$hash);
+
+ q("insert into outq ( outq_hash, outq_account, outq_channel, outq_driver, outq_posturl, outq_async, outq_created, outq_updated, outq_notify, outq_msg ) values ( '%s', %d, %d, '%s', '%s', %d, '%s', '%s', '%s', '%s' )",
+ dbesc($hash),
+ intval($channel['channel_account_id']),
+ intval($channel['channel_id']),
+ dbesc('zot'),
+ dbesc($h . '/post'),
+ intval(1),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc($n),
+ dbesc(json_encode($encoded_item))
+ );
+ }
+ $deliver[] = $hash;
+
+ if(count($deliver) >= $deliveries_per_process) {
+ proc_run('php','include/deliver.php',$deliver);
+ $deliver = array();
+ if($interval)
+ @time_sleep_until(microtime(true) + (float) $interval);
+ }
+
+
+ // catch any stragglers
+
+ if(count($deliver)) {
+ proc_run('php','include/deliver.php',$deliver);
+ }
+ }
+ }
+
+ logger('ratenotif: complete.');
+ return;
+
+}
+
+if (array_search(__file__,get_included_files())===0){
+ ratenotif_run($argv,$argc);
+ killme();
+}
diff --git a/include/zot.php b/include/zot.php
index 04b6806c2..1e9577dc6 100644
--- a/include/zot.php
+++ b/include/zot.php
@@ -1108,7 +1108,8 @@ function zot_import($arr, $sender_url) {
if(array_key_exists('message',$i) && array_key_exists('type',$i['message']) && $i['message']['type'] === 'rating') {
// rating messages are processed only by directory servers
logger('Rating received: ' . print_r($arr,true), LOGGER_DATA);
- $result = process_rating_delivery($i['notify']['sender'],$arr);
+ $result = process_rating_delivery($i['notify']['sender'],$i['message']);
+ continue;
}
if(array_key_exists('recipients',$i['notify']) && count($i['notify']['recipients'])) {
@@ -1813,34 +1814,52 @@ function process_mail_delivery($sender,$arr,$deliveries) {
function process_rating_delivery($sender,$arr) {
- $dirmode = intval(get_config('system','directory_mode'));
- if($dirmode == DIRECTORY_MODE_NORMAL)
- return;
+ logger('process_rating_delivery: ' . print_r($arr,true));
if(! $arr['target'])
return;
- $r = q("select * from xlink where xlink_xchan = '%s' and xlink_target = '%s' limit 1",
+ $z = q("select xchan_pubkey from xchan where xchan_hash = '%s' limit 1",
+ dbesc($sender['hash'])
+ );
+
+
+ if((! $z) || (! rsa_verify($arr['target'] . '.' . $arr['rating'] . '.' . $arr['rating_text'], base64url_decode($arr['signature']),$z[0]['xchan_pubkey']))) {
+ logger('failed to verify rating');
+ return;
+ }
+
+ $r = q("select * from xlink where xlink_xchan = '%s' and xlink_link = '%s' and xlink_static = 1 limit 1",
dbesc($sender['hash']),
dbesc($arr['target'])
- );
+ );
+
if($r) {
- $x = q("update xlink set xlink_rating = %d, xlink_rating_text = '%s', xlink_updated = '%s' where xlink_id = %d",
+ if($r[0]['xlink_updated'] >= $arr['edited']) {
+ logger('rating message duplicate');
+ return;
+ }
+
+ $x = q("update xlink set xlink_rating = %d, xlink_rating_text = '%s', xlink_sig = '%s', xlink_updated = '%s' where xlink_id = %d",
intval($arr['rating']),
- intval($arr['rating_text']),
+ dbesc($arr['rating_text']),
+ dbesc($arr['signature']),
dbesc(datetime_convert()),
intval($r[0]['xlink_id'])
);
+ logger('rating updated');
}
else {
- $x = q("insert into xlink ( xlink_xchan, xlink_link, xlink_rating, xlink_rating_text, xlink_updated, xlink_static )
+ $x = q("insert into xlink ( xlink_xchan, xlink_link, xlink_rating, xlink_rating_text, xlink_sig, xlink_updated, xlink_static )
values( '%s', '%s', %d, '%s', '%s', 1 ) ",
dbesc($sender['hash']),
dbesc($arr['target']),
intval($arr['rating']),
- intval($arr['rating_text']),
+ dbesc($arr['rating_text']),
+ dbesc($arr['signature']),
dbesc(datetime_convert())
);
+ logger('rating created');
}
return;
}
diff --git a/mod/connedit.php b/mod/connedit.php
index 93cf1b09a..79c7e6a4f 100644
--- a/mod/connedit.php
+++ b/mod/connedit.php
@@ -117,7 +117,7 @@ function connedit_post(&$a) {
if($rating > 10)
$rating = 10;
- $rating_text = escape_tags($_REQUEST['rating_text']);
+ $rating_text = trim(escape_tags($_REQUEST['rating_text']));
$abook_my_perms = 0;
@@ -132,7 +132,7 @@ function connedit_post(&$a) {
if(! $is_self) {
- $signed = $target . '.' . $rating . '.' . $rating_text;
+ $signed = $orig_record[0]['abook_xchan'] . '.' . $rating . '.' . $rating_text;
$sig = base64url_encode(rsa_sign($signed,$channel['channel_prvkey']));
@@ -170,7 +170,7 @@ function connedit_post(&$a) {
$record = $z[0]['xlink_id'];
}
if($record) {
- proc_run('php','include/notifier.php','rating',$record);
+ proc_run('php','include/ratenotif.php','rating',$record);
}
}
diff --git a/mod/prate.php b/mod/prate.php
index 00de8b6fc..30de97927 100644
--- a/mod/prate.php
+++ b/mod/prate.php
@@ -60,7 +60,7 @@ function prate_post(&$a) {
$record = $z[0]['xlink_id'];
}
if($record) {
- proc_run('php','include/notifier.php','rating',$record);
+ proc_run('php','include/ratenotif.php','rating',$record);
}
$x = q("select abook_id from abook where abook_xchan = '%s' and abook_channel = %d limit 1",