aboutsummaryrefslogtreecommitdiffstats
path: root/include/diaspora.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/diaspora.php')
-rwxr-xr-xinclude/diaspora.php145
1 files changed, 116 insertions, 29 deletions
diff --git a/include/diaspora.php b/include/diaspora.php
index 0ca9163a8..7551ea9b3 100755
--- a/include/diaspora.php
+++ b/include/diaspora.php
@@ -5,6 +5,7 @@ require_once('include/items.php');
require_once('include/bb2diaspora.php');
require_once('include/contact_selectors.php');
require_once('include/queue_fn.php');
+require_once('include/lock.php');
function diaspora_dispatch_public($msg) {
@@ -113,27 +114,93 @@ function diaspora_get_contact_by_handle($uid,$handle) {
}
function find_diaspora_person_by_handle($handle) {
+
+ $person = false;
$update = false;
- $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
- dbesc(NETWORK_DIASPORA),
- dbesc($handle)
- );
- if(count($r)) {
- logger('find_diaspora_person_by handle: in cache ' . print_r($r,true), LOGGER_DEBUG);
- // update record occasionally so it doesn't get stale
- $d = strtotime($r[0]['updated'] . ' +00:00');
- if($d > strtotime('now - 14 days'))
- return $r[0];
- $update = true;
- }
- logger('find_diaspora_person_by_handle: refresh',LOGGER_DEBUG);
- require_once('include/Scrape.php');
- $r = probe_url($handle, PROBE_DIASPORA);
- if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
- add_fcontact($r,$update);
- return ($r);
- }
- return false;
+ $got_lock = false;
+
+ $endlessloop = 0;
+ $maxloops = 10;
+
+ do {
+ $r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
+ dbesc(NETWORK_DIASPORA),
+ dbesc($handle)
+ );
+ if(count($r)) {
+ $person = $r[0];
+ logger('find_diaspora_person_by handle: in cache ' . print_r($r,true), LOGGER_DEBUG);
+
+ // update record occasionally so it doesn't get stale
+ $d = strtotime($person['updated'] . ' +00:00');
+ if($d < strtotime('now - 14 days'))
+ $update = true;
+ }
+
+
+ // FETCHING PERSON INFORMATION FROM REMOTE SERVER
+ //
+ // If the person isn't in our 'fcontact' table, or if he/she is but
+ // his/her information hasn't been updated for more than 14 days, then
+ // we want to fetch the person's information from the remote server.
+ //
+ // Note that $person isn't changed by this block of code unless the
+ // person's information has been successfully fetched from the remote
+ // server. So if $person was 'false' to begin with (because he/she wasn't
+ // in the local cache), it'll stay false, and if $person held the local
+ // cache information to begin with, it'll keep that information. That way
+ // if there's a problem with the remote fetch, we can at least use our
+ // cached information--it's better than nothing.
+
+ if((! $person) || ($update)) {
+ // Lock the function to prevent race conditions if multiple items
+ // come in at the same time from a person who doesn't exist in
+ // fcontact
+ //
+ // Don't loop forever. On the last loop, try to create the contact
+ // whether the function is locked or not. Maybe the locking thread
+ // has died or something. At any rate, a duplicate in 'fcontact'
+ // is a much smaller problem than a deadlocked thread
+ $got_lock = lock_function('find_diaspora_person_by_handle', false);
+ if(($endlessloop + 1) >= $maxloops)
+ $got_lock = true;
+
+ if($got_lock) {
+ logger('find_diaspora_person_by_handle: create or refresh', LOGGER_DEBUG);
+ require_once('include/Scrape.php');
+ $r = probe_url($handle, PROBE_DIASPORA);
+
+ // Note that Friendica contacts can return a "Diaspora person"
+ // if Diaspora connectivity is enabled on their server
+ if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
+ add_fcontact($r,$update);
+ $person = ($r);
+ }
+
+ unlock_function('find_diaspora_person_by_handle');
+ }
+ else {
+ logger('find_diaspora_person_by_handle: couldn\'t lock function', LOGGER_DEBUG);
+ if(! $person)
+ block_on_function_lock('find_diaspora_person_by_handle');
+ }
+ }
+ } while((! $person) && (! $got_lock) && (++$endlessloop < $maxloops));
+ // We need to try again if the person wasn't in 'fcontact' but the function was locked.
+ // The fact that the function was locked may mean that another process was creating the
+ // person's record. It could also mean another process was creating or updating an unrelated
+ // person.
+ //
+ // At any rate, we need to keep trying until we've either got the person or had a chance to
+ // try to fetch his/her remote information. But we don't want to block on locking the
+ // function, because if the other process is creating the record, then when we acquire the lock
+ // we'll dive right into creating another, duplicate record. We DO want to at least wait
+ // until the lock is released, so we don't flood the database with requests.
+ //
+ // If the person was in the 'fcontact' table, don't try again. It's not worth the time, since
+ // we do have some information for the person
+
+ return $person;
}
@@ -844,7 +911,7 @@ function diaspora_reshare($importer,$xml) {
else
$details = $orig_author;
- $prefix = '&#x2672; ' . $details . "\n";
+ $prefix = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8') . $details . "\n";
// allocate a guid on our system - we aren't fixing any collisions.
@@ -1560,7 +1627,8 @@ function diaspora_photo($importer,$xml,$msg) {
$link_text = '[img]' . $remote_photo_path . $remote_photo_name . '[/img]' . "\n";
- $link_text = scale_external_images($link_text);
+ $link_text = scale_external_images($link_text, true,
+ array($remote_photo_name, 'scaled_full_' . $remote_photo_name));
if(strpos($parent_item['body'],$link_text) === false) {
$r = q("update item set `body` = '%s', `visible` = 1 where `id` = %d and `uid` = %d limit 1",
@@ -2005,6 +2073,7 @@ function diaspora_share($me,$contact) {
));
$slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
+ //$slap = 'xml=' . urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey']));
return(diaspora_transmit($owner,$contact,$slap, false));
}
@@ -2022,6 +2091,7 @@ function diaspora_unshare($me,$contact) {
));
$slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey'])));
+ //$slap = 'xml=' . urlencode(diaspora_msg_build($msg,$me,$contact,$me['prvkey'],$contact['pubkey']));
return(diaspora_transmit($owner,$contact,$slap, false));
@@ -2060,13 +2130,21 @@ function diaspora_send_status($item,$owner,$contact,$public_batch = false) {
$images[] = $detail;
$body = str_replace($detail['str'],$mtch[1],$body);
}
- }
+ }
*/
+ // Removal of tags
+ $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
+
+ //if(strlen($title))
+ // $body = "[b]".html_entity_decode($title)."[/b]\n\n".$body;
+
+ // convert to markdown
$body = xmlify(html_entity_decode(bb2diaspora($body)));
+ //$body = bb2diaspora($body);
+ // Adding the title
if(strlen($title))
- $body = xmlify('**' . html_entity_decode($title) . '**' . "\n") . $body;
-
+ $body = "## ".html_entity_decode($title)."\n\n".$body;
if($item['attach']) {
$cnt = preg_match_all('/href=\"(.*?)\"(.*?)title=\"(.*?)\"/ism',$item['attach'],$matches,PREG_SET_ORDER);
@@ -2096,6 +2174,7 @@ function diaspora_send_status($item,$owner,$contact,$public_batch = false) {
logger('diaspora_send_status: ' . $owner['username'] . ' -> ' . $contact['name'] . ' base message: ' . $msg, LOGGER_DATA);
$slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
+ //$slap = 'xml=' . urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch));
$return_code = diaspora_transmit($owner,$contact,$slap,$public_batch);
@@ -2140,6 +2219,7 @@ function diaspora_send_images($item,$owner,$contact,$images,$public_batch = fals
logger('diaspora_send_photo: base message: ' . $msg, LOGGER_DATA);
$slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
+ //$slap = 'xml=' . urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch));
diaspora_transmit($owner,$contact,$slap,$public_batch);
}
@@ -2203,6 +2283,7 @@ function diaspora_send_followup($item,$owner,$contact,$public_batch = false) {
logger('diaspora_followup: base message: ' . $msg, LOGGER_DATA);
$slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
+ //$slap = 'xml=' . urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch));
return(diaspora_transmit($owner,$contact,$slap,$public_batch));
}
@@ -2238,7 +2319,6 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
$relay_retract = true;
$target_type = ( ($item['verb'] === ACTIVITY_LIKE) ? 'Like' : 'Comment');
- $sender_signed_text = $item['guid'] . ';' . $target_type ;
$sql_sign_id = 'retract_iid';
$tpl = get_markup_template('diaspora_relayable_retraction.tpl');
@@ -2249,13 +2329,10 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
$target_type = 'Post';
// $positive = (($item['deleted']) ? 'false' : 'true');
$positive = 'true';
- $sender_signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $myaddr;
$tpl = get_markup_template('diaspora_like_relay.tpl');
}
else { // item is a comment
- $sender_signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $myaddr;
-
$tpl = get_markup_template('diaspora_comment_relay.tpl');
}
@@ -2281,6 +2358,13 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
return;
}
+ if($relay_retract)
+ $sender_signed_text = $item['guid'] . ';' . $target_type;
+ elseif($like)
+ $sender_signed_text = $item['guid'] . ';' . $target_type . ';' . $parent_guid . ';' . $positive . ';' . $handle;
+ else
+ $sender_signed_text = $item['guid'] . ';' . $parent_guid . ';' . $text . ';' . $handle;
+
// Sign the relayable with the top-level owner's signature
//
// We'll use the $sender_signed_text that we just created, instead of the $signed_text
@@ -2309,6 +2393,7 @@ function diaspora_send_relay($item,$owner,$contact,$public_batch = false) {
$slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
+ //$slap = 'xml=' . urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch));
return(diaspora_transmit($owner,$contact,$slap,$public_batch));
@@ -2343,6 +2428,7 @@ function diaspora_send_retraction($item,$owner,$contact,$public_batch = false) {
));
$slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch)));
+ //$slap = 'xml=' . urlencode(diaspora_msg_build($msg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],$public_batch));
return(diaspora_transmit($owner,$contact,$slap,$public_batch));
}
@@ -2403,6 +2489,7 @@ function diaspora_send_mail($item,$owner,$contact) {
logger('diaspora_conversation: ' . print_r($xmsg,true), LOGGER_DATA);
$slap = 'xml=' . urlencode(urlencode(diaspora_msg_build($xmsg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],false)));
+ //$slap = 'xml=' . urlencode(diaspora_msg_build($xmsg,$owner,$contact,$owner['uprvkey'],$contact['pubkey'],false));
return(diaspora_transmit($owner,$contact,$slap,false));