X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fitems.php;h=549027671d5e11c827c81ca43454a23acd0ee95f;hb=a0f3fe6fde55a61291ce96f657262c6b084b5894;hp=65c49bf26373d5262136aba2e964c0c713f26828;hpb=009b3364e51bd12b201af6cae1f595adf88746f5;p=friendica.git diff --git a/include/items.php b/include/items.php index 65c49bf263..549027671d 100644 --- a/include/items.php +++ b/include/items.php @@ -1097,10 +1097,13 @@ function add_guid($item) { dbesc($item["uri"]), dbesc($item["network"])); } -// Adds a "lang" specification in a "postopts" element of given $arr, -// if possible and not already present. -// Expects "body" element to exist in $arr. -// TODO: add a parameter to request forcing override +/** + * Adds a "lang" specification in a "postopts" element of given $arr, + * if possible and not already present. + * Expects "body" element to exist in $arr. + * + * @todo Add a parameter to request forcing override + */ function item_add_language_opt(&$arr) { if (version_compare(PHP_VERSION, '5.3.0', '<')) return; // LanguageDetect.php not available ? @@ -1110,7 +1113,7 @@ function item_add_language_opt(&$arr) { if ( strstr($arr['postopts'], 'lang=') ) { // do not override - // TODO: add parameter to request overriding + /// @TODO Add parameter to request overriding return; } $postopts = $arr['postopts']; @@ -1196,6 +1199,24 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa } } + // Do we already have this item? + // We have to check several networks since Friendica posts could be repeated via OStatus (maybe Diasporsa as well) + if (in_array(trim($arr['network']), array(NETWORK_DIASPORA, NETWORK_DFRN, NETWORK_OSTATUS, ""))) { + $r = q("SELECT `id`, `network` FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `network` IN ('%s', '%s', '%s') LIMIT 1", + dbesc(trim($arr['uri'])), + intval($uid), + dbesc(NETWORK_DIASPORA), + dbesc(NETWORK_DFRN), + dbesc(NETWORK_OSTATUS) + ); + if ($r) { + // We only log the entries with a different user id than 0. Otherwise we would have too many false positives + if ($uid != 0) + logger("Item with uri ".$arr['uri']." already existed for user ".$uid." with id ".$r[0]["id"]." target network ".$r[0]["network"]." - new network: ".$arr['network']); + return($r[0]["id"]); + } + } + // If there is no guid then take the same guid that was taken before for the same uri if ((trim($arr['guid']) == "") AND (trim($arr['uri']) != "") AND (trim($arr['network']) != "")) { logger('item_store: checking for an existing guid for uri '.$arr['uri'], LOGGER_DEBUG); @@ -1232,8 +1253,10 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa if ($notify) $guid_prefix = ""; - else - $guid_prefix = $arr['network']; + else { + $parsed = parse_url($arr["author-link"]); + $guid_prefix = hash("crc32", $parsed["host"]); + } $arr['wall'] = ((x($arr,'wall')) ? intval($arr['wall']) : 0); $arr['guid'] = ((x($arr,'guid')) ? notags(trim($arr['guid'])) : get_guid(32, $guid_prefix)); @@ -1315,6 +1338,10 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa logger("item_store: Set network to ".$arr["network"]." for ".$arr["uri"], LOGGER_DEBUG); } + if ($arr["gcontact-id"] == 0) + $arr["gcontact-id"] = get_gcontact_id(array("url" => $arr['author-link'], "network" => $arr['network'], + "photo" => $arr['author-avatar'], "name" => $arr['author-name'])); + if ($arr['guid'] != "") { // Checking if there is already an item with the same guid logger('checking for an item for user '.$arr['uid'].' on network '.$arr['network'].' with the guid '.$arr['guid'], LOGGER_DEBUG); @@ -1425,9 +1452,10 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa } } - $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `network` = '%s' AND `uid` = %d LIMIT 1", + $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `network` IN ('%s', '%s') AND `uid` = %d LIMIT 1", dbesc($arr['uri']), dbesc($arr['network']), + dbesc(NETWORK_DFRN), intval($arr['uid']) ); if($r && count($r)) { @@ -1488,14 +1516,24 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa // And restore it $arr = $unescaped; - // find the item we just created - $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `network` = '%s' ORDER BY `id` ASC ", + // find the item that we just created + $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `network` = '%s' ORDER BY `id` ASC", dbesc($arr['uri']), intval($arr['uid']), dbesc($arr['network']) ); - if(count($r)) { + if(count($r) > 1) { + // There are duplicates. Keep the oldest one, delete the others + logger('item_store: duplicated post occurred. Removing newer duplicates. uri = '.$arr['uri'].' uid = '.$arr['uid']); + q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `network` = '%s' AND `id` > %d", + dbesc($arr['uri']), + intval($arr['uid']), + dbesc($arr['network']), + intval($r[0]["id"]) + ); + return 0; + } elseif(count($r)) { // Store the guid and other relevant data add_guid($arr); @@ -1528,14 +1566,6 @@ function item_store($arr,$force_parent = false, $notify = false, $dontcache = fa logger('item_store: could not locate created item'); return 0; } - if(count($r) > 1) { - logger('item_store: duplicated post occurred. Removing duplicates. uri = '.$arr['uri'].' uid = '.$arr['uid']); - q("DELETE FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `id` != %d ", - dbesc($arr['uri']), - intval($arr['uid']), - intval($current_post) - ); - } if((! $parent_id) || ($arr['parent-uri'] === $arr['uri'])) $parent_id = $current_post; @@ -2299,6 +2329,9 @@ function edited_timestamp_is_newer($existing, $update) { function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) { if ($contact['network'] === NETWORK_OSTATUS) { if ($pass < 2) { + // Test - remove before flight + //$tempfile = tempnam(get_temppath(), "ostatus2"); + //file_put_contents($tempfile, $xml); logger("Consume OStatus messages ", LOGGER_DEBUG); ostatus_import($xml,$importer,$contact, $hub); } @@ -2384,7 +2417,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) $photos = import_profile_photo($photo_url,$contact['uid'],$contact['id']); q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s' - WHERE `uid` = %d AND `id` = %d", + WHERE `uid` = %d AND `id` = %d AND NOT `self`", dbesc(datetime_convert()), dbesc($photos[0]), dbesc($photos[1]), @@ -2403,7 +2436,7 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) intval($contact['id']) ); - $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d AND `name` != '%s'", + $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d AND `name` != '%s' AND NOT `self`", dbesc(notags(trim($new_name))), dbesc(datetime_convert()), intval($contact['uid']), @@ -2739,20 +2772,20 @@ function consume_feed($xml,$importer,&$contact, &$hub, $datedir = 0, $pass = 0) $datarray['gravity'] = GRAVITY_LIKE; // only one like or dislike per person // splitted into two queries for performance issues - $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s') limit 1", + $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `parent-uri` = '%s' AND NOT `deleted` LIMIT 1", intval($datarray['uid']), - intval($datarray['contact-id']), + dbesc($datarray['author-link']), dbesc($datarray['verb']), - dbesc($parent_uri) + dbesc($datarray['parent-uri']) ); if($r && count($r)) continue; - $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`thr-parent` = '%s') limit 1", + $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1", intval($datarray['uid']), - intval($datarray['contact-id']), + dbesc($datarray['author-link']), dbesc($datarray['verb']), - dbesc($parent_uri) + dbesc($datarray['parent-uri']) ); if($r && count($r)) continue; @@ -3083,7 +3116,7 @@ function local_delivery($importer,$data) { $photos = import_profile_photo($photo_url,$importer['importer_uid'],$importer['id']); q("UPDATE `contact` SET `avatar-date` = '%s', `photo` = '%s', `thumb` = '%s', `micro` = '%s' - WHERE `uid` = %d AND `id` = %d", + WHERE `uid` = %d AND `id` = %d AND NOT `self`", dbesc(datetime_convert()), dbesc($photos[0]), dbesc($photos[1]), @@ -3102,7 +3135,7 @@ function local_delivery($importer,$data) { intval($importer['id']) ); - $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d AND `name` != '%s'", + $x = q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `id` = %d AND `name` != '%s' AND NOT `self`", dbesc(notags(trim($new_name))), dbesc(datetime_convert()), intval($importer['importer_uid']), @@ -3202,11 +3235,11 @@ function local_delivery($importer,$data) { return 1; } - // TODO - // merge with current record, current contents have priority - // update record, set url-updated - // update profile photos - // schedule a scan? + /// @TODO + /// merge with current record, current contents have priority + /// update record, set url-updated + /// update profile photos + /// schedule a scan? return 0; } @@ -3688,7 +3721,7 @@ function local_delivery($importer,$data) { $datarray['owner-avatar'] = $own[0]['thumb']; $datarray['contact-id'] = $importer['id']; - if(($datarray['verb'] === ACTIVITY_LIKE) + if(($datarray['verb'] === ACTIVITY_LIKE) || ($datarray['verb'] === ACTIVITY_DISLIKE) || ($datarray['verb'] === ACTIVITY_ATTEND) || ($datarray['verb'] === ACTIVITY_ATTENDNO) @@ -3699,19 +3732,18 @@ function local_delivery($importer,$data) { $datarray['last-child'] = 0; // only one like or dislike per person // splitted into two queries for performance issues - $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`parent-uri` = '%s') and deleted = 0 limit 1", + $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `parent-uri` = '%s' AND NOT `deleted` LIMIT 1", intval($datarray['uid']), - intval($datarray['contact-id']), + dbesc($datarray['author-link']), dbesc($datarray['verb']), dbesc($datarray['parent-uri']) - ); if($r && count($r)) continue; - $r = q("select id from item where uid = %d and `contact-id` = %d and verb = '%s' and (`thr-parent` = '%s') and deleted = 0 limit 1", + $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1", intval($datarray['uid']), - intval($datarray['contact-id']), + dbesc($datarray['author-link']), dbesc($datarray['verb']), dbesc($datarray['parent-uri']) @@ -3881,7 +3913,7 @@ function local_delivery($importer,$data) { $datarray['parent-uri'] = $parent_uri; $datarray['uid'] = $importer['importer_uid']; $datarray['contact-id'] = $importer['id']; - if(($datarray['verb'] === ACTIVITY_LIKE) + if(($datarray['verb'] === ACTIVITY_LIKE) || ($datarray['verb'] === ACTIVITY_DISLIKE) || ($datarray['verb'] === ACTIVITY_ATTEND) || ($datarray['verb'] === ACTIVITY_ATTENDNO) @@ -3890,20 +3922,20 @@ function local_delivery($importer,$data) { $datarray['gravity'] = GRAVITY_LIKE; // only one like or dislike per person // splitted into two queries for performance issues - $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`parent-uri` = '%s') limit 1", + $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `parent-uri` = '%s' AND NOT `deleted` LIMIT 1", intval($datarray['uid']), - intval($datarray['contact-id']), + dbesc($datarray['author-link']), dbesc($datarray['verb']), - dbesc($parent_uri) + dbesc($datarray['parent-uri']) ); if($r && count($r)) continue; - $r = q("select id from item where uid = %d and `contact-id` = %d and verb ='%s' and deleted = 0 and (`thr-parent` = '%s') limit 1", + $r = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `author-link` = '%s' AND `verb` = '%s' AND `thr-parent` = '%s' AND NOT `deleted` LIMIT 1", intval($datarray['uid']), - intval($datarray['contact-id']), + dbesc($datarray['author-link']), dbesc($datarray['verb']), - dbesc($parent_uri) + dbesc($datarray['parent-uri']) ); if($r && count($r)) continue; @@ -4171,14 +4203,13 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) { ); } // send email notification to owner? - } - else { + } else { // create contact record - $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`, - `blocked`, `readonly`, `pending`, `writable` ) - VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1 ) ", + $r = q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`, + `blocked`, `readonly`, `pending`, `writable`) + VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1)", intval($importer['uid']), dbesc(datetime_convert()), dbesc($url), @@ -4193,27 +4224,38 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) { intval($importer['uid']), dbesc($url) ); - if(count($r)) + if(count($r)) { $contact_record = $r[0]; - // create notification - $hash = random_string(); + $photos = import_profile_photo($photo,$importer["uid"],$contact_record["id"]); - if(is_array($contact_record)) { - $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`) - VALUES ( %d, %d, 0, 0, '%s', '%s' )", - intval($importer['uid']), - intval($contact_record['id']), - dbesc($hash), - dbesc(datetime_convert()) - ); + q("UPDATE `contact` SET `photo` = '%s', `thumb` = '%s', `micro` = '%s' WHERE `id` = %d", + dbesc($photos[0]), + dbesc($photos[1]), + dbesc($photos[2]), + intval($contact_record["id"]) + ); } + $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($importer['uid']) ); $a = get_app(); - if(count($r)) { + if(count($r) AND !in_array($r[0]['page-flags'], array(PAGE_SOAPBOX, PAGE_FREELOVE))) { + + // create notification + $hash = random_string(); + + if(is_array($contact_record)) { + $ret = q("INSERT INTO `intro` ( `uid`, `contact-id`, `blocked`, `knowyou`, `hash`, `datetime`) + VALUES ( %d, %d, 0, 0, '%s', '%s' )", + intval($importer['uid']), + intval($contact_record['id']), + dbesc($hash), + dbesc(datetime_convert()) + ); + } if(intval($r[0]['def_gid'])) { require_once('include/group.php'); @@ -4221,7 +4263,7 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) { } if(($r[0]['notify-flags'] & NOTIFY_INTRO) && - in_array($r[0]['page-flags'], array(PAGE_NORMAL, PAGE_SOAPBOX, PAGE_FREELOVE))) { + in_array($r[0]['page-flags'], array(PAGE_NORMAL))) { notification(array( 'type' => NOTIFY_INTRO, @@ -4239,7 +4281,13 @@ function new_follower($importer,$contact,$datarray,$item,$sharing = false) { )); } + } elseif (count($r) AND in_array($r[0]['page-flags'], array(PAGE_SOAPBOX, PAGE_FREELOVE))) { + $r = q("UPDATE `contact` SET `pending` = 0 WHERE `uid` = %d AND `url` = '%s' AND `pending` LIMIT 1", + intval($importer['uid']), + dbesc($url) + ); } + } } @@ -4360,7 +4408,7 @@ function atom_author($tag,$name,$uri,$h,$w,$photo,$geo) { $o .= "\t".xmlify($r[0]["nick"])."\r\n"; $o .= "\t".xmlify($r[0]["name"])."\r\n"; - $o .= "\t".xmlify($r[0]["about"])."\r\n"; + $o .= "\t".xmlify(bbcode($r[0]["about"]))."\r\n"; $o .= "\t\r\n"; $o .= "\t\t".xmlify($location)."\r\n"; $o .= "\t\r\n"; @@ -4473,13 +4521,14 @@ function atom_entry($item,$type,$author,$owner,$comment = false,$cid = 0) { $o .= '' . "\r\n"; } - // To-Do: - // To support these elements, the API needs to be enhanced - //$o .= ''."\r\n"; - //$o .= "\t".''."\r\n"; - //$o .= "\t".''."\r\n"; + /// @TODO + /// To support these elements, the API needs to be enhanced + /// $o .= ''."\r\n"; + /// $o .= "\t".''."\r\n"; + /// $o .= "\t".''."\r\n"; - $o .= item_get_attachment($item); + // Deactivated since it was meant only for OStatus + //$o .= item_get_attachment($item); $o .= item_getfeedattach($item); @@ -4644,7 +4693,7 @@ function item_getfeedtags($item) { if($cnt) { for($x = 0; $x < $cnt; $x ++) { if($matches[1][$x]) - $ret[] = array('#',$matches[1][$x], $matches[2][$x]); + $ret[$matches[2][$x]] = array('#',$matches[1][$x], $matches[2][$x]); } } $matches = false;