namespace Friendica\Model;
use Friendica\BaseObject;
+use Friendica\Content\Text;
use Friendica\Core\Addon;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Database\DBM;
use Friendica\Model\Contact;
use Friendica\Model\Conversation;
-use Friendica\Model\GContact;
use Friendica\Model\Group;
use Friendica\Model\Term;
use Friendica\Object\Image;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\OStatus;
+use Friendica\Util\DateTimeFormat;
+use Friendica\Util\XML;
use dba;
use Text_LanguageDetect;
require_once 'boot.php';
-require_once 'include/tags.php';
-require_once 'include/threads.php';
require_once 'include/items.php';
require_once 'include/text.php';
// The condition needn't to be a simple array but could be a complex condition.
$items = dba::select('item', ['id', 'origin'], $condition);
while ($item = dba::fetch($items)) {
+ Term::insertFromTagFieldByItemId($item['id']);
+ Term::insertFromFileFieldByItemId($item['id']);
+ self::updateThread($item['id']);
+
// We only need to notfiy others when it is an original entry from us
- if (!$item['origin']) {
- continue;
+ if ($item['origin']) {
+ Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $item['id']);
}
-
- create_tags_from_item($item['id']);
- Term::createFromItem($item['id']);
- update_thread($item['id']);
-
- Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $item['id']);
}
return $rows;
}
+ /**
+ * @brief Delete an item and notify others about it - if it was ours
+ *
+ * @param array $condition The condition for finding the item entries
+ * @param integer $priority Priority for the notification
+ */
+ public static function delete($condition, $priority = PRIORITY_HIGH)
+ {
+ $items = dba::select('item', ['id'], $condition);
+ while ($item = dba::fetch($items)) {
+ self::deleteById($item['id'], $priority);
+ }
+ dba::close($items);
+ }
+
/**
* @brief Delete an item and notify others about it - if it was ours
*
* @param integer $item_id Item ID that should be delete
+ * @param integer $priority Priority for the notification
*
- * @return $boolean success
+ * @return boolean success
*/
- public static function delete($item_id, $priority = PRIORITY_HIGH)
+ public static function deleteById($item_id, $priority = PRIORITY_HIGH)
{
// locate item to be deleted
- $fields = ['id', 'uid', 'parent', 'parent-uri', 'origin', 'deleted', 'file', 'resource-id', 'event-id', 'attach'];
+ $fields = ['id', 'uid', 'parent', 'parent-uri', 'origin', 'deleted',
+ 'file', 'resource-id', 'event-id', 'attach',
+ 'verb', 'object-type', 'object', 'target', 'contact-id'];
$item = dba::selectFirst('item', $fields, ['id' => $item_id]);
if (!DBM::is_result($item)) {
return false;
dba::delete('photo', ['resource-id' => $item['resource-id'], 'uid' => $item['uid']]);
}
- // If item is a link to an event, nuke the event record.
+ // If item is a link to an event, delete the event.
if (intval($item['event-id'])) {
- dba::delete('event', ['id' => $item['event-id'], 'uid' => $item['uid']]);
+ Event::delete($item['event-id']);
}
// If item has attachments, drop them
dba::delete('attach', ['id' => $matches[1], 'uid' => $item['uid']]);
}
+ // Delete tags that had been attached to other items
+ self::deleteTagsFromItem($item);
+
// Set the item to "deleted"
dba::update('item', ['deleted' => true, 'title' => '', 'body' => '',
- 'edited' => datetime_convert(), 'changed' => datetime_convert()],
+ 'edited' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()],
['id' => $item['id']]);
- create_tags_from_item($item['id']);
- Term::createFromItem($item['id']);
- delete_thread($item['id'], $item['parent-uri']);
+ Term::insertFromTagFieldByItemId($item['id']);
+ Term::insertFromFileFieldByItemId($item['id']);
+ self::deleteThread($item['id'], $item['parent-uri']);
// If it's the parent of a comment thread, kill all the kids
if ($item['id'] == $item['parent']) {
- $items = dba::select('item', ['id'], ['parent' => $item['parent']]);
- while ($row = dba::fetch($items)) {
- self::delete($row['id'], $priority);
- }
+ self::delete(['parent' => $item['parent']], $priority);
}
// send the notification upstream/downstream
return true;
}
- public static function insert($arr, $force_parent = false, $notify = false, $dontcache = false)
+ private static function deleteTagsFromItem($item)
{
- $a = get_app();
+ if (($item["verb"] != ACTIVITY_TAG) || ($item["object-type"] != ACTIVITY_OBJ_TAGTERM)) {
+ return;
+ }
- // If it is a posting where users should get notifications, then define it as wall posting
- if ($notify) {
- $arr['wall'] = 1;
- $arr['type'] = 'wall';
- $arr['origin'] = 1;
- $arr['network'] = NETWORK_DFRN;
- $arr['protocol'] = PROTOCOL_DFRN;
+ $xo = XML::parseString($item["object"], false);
+ $xt = XML::parseString($item["target"], false);
+
+ if ($xt->type != ACTIVITY_OBJ_NOTE) {
+ return;
+ }
+
+ $i = dba::selectFirst('item', ['id', 'contact-id', 'tag'], ['uri' => $xt->id, 'uid' => $item['uid']]);
+ if (!DBM::is_result($i)) {
+ return;
+ }
+
+ // For tags, the owner cannot remove the tag on the author's copy of the post.
+ $owner_remove = ($item["contact-id"] == $i["contact-id"]);
+ $author_copy = $item["origin"];
+
+ if (($owner_remove && $author_copy) || !$owner_remove) {
+ return;
+ }
+
+ $tags = explode(',', $i["tag"]);
+ $newtags = [];
+ if (count($tags)) {
+ foreach ($tags as $tag) {
+ if (trim($tag) !== trim($xo->body)) {
+ $newtags[] = trim($tag);
+ }
+ }
+ }
+ self::update(['tag' => implode(',', $newtags)], ['id' => $i["id"]]);
+ }
+
+ private static function guid($item, $notify)
+ {
+ $guid = notags(trim($item['guid']));
+ if (!empty($guid)) {
+ return $guid;
+ }
+
+ if ($notify) {
// We have to avoid duplicates. So we create the GUID in form of a hash of the plink or uri.
- // In difference to the call to "self::guidFromUri" several lines below we add the hash of our own host.
- // This is done because our host is the original creator of the post.
- if (!isset($arr['guid'])) {
- if (isset($arr['plink'])) {
- $arr['guid'] = self::guidFromUri($arr['plink'], $a->get_hostname());
- } elseif (isset($arr['uri'])) {
- $arr['guid'] = self::guidFromUri($arr['uri'], $a->get_hostname());
+ // We add the hash of our own host because our host is the original creator of the post.
+ $prefix_host = get_app()->get_hostname();
+ } else {
+ $prefix_host = '';
+
+ // We are only storing the post so we create a GUID from the original hostname.
+ if (!empty($item['author-link'])) {
+ $parsed = parse_url($item['author-link']);
+ if (!empty($parsed['host'])) {
+ $prefix_host = $parsed['host'];
}
}
+
+ if (empty($prefix_host) && !empty($item['plink'])) {
+ $parsed = parse_url($item['plink']);
+ if (!empty($parsed['host'])) {
+ $prefix_host = $parsed['host'];
+ }
+ }
+
+ if (empty($prefix_host) && !empty($item['uri'])) {
+ $parsed = parse_url($item['uri']);
+ if (!empty($parsed['host'])) {
+ $prefix_host = $parsed['host'];
+ }
+ }
+
+ // Is it in the format data@host.tld? - Used for mail contacts
+ if (empty($prefix_host) && !empty($item['author-link']) && strstr($item['author-link'], '@')) {
+ $mailparts = explode('@', $item['author-link']);
+ $prefix_host = array_pop($mailparts);
+ }
+ }
+
+ if (!empty($item['plink'])) {
+ $guid = self::guidFromUri($item['plink'], $prefix_host);
+ } elseif (!empty($item['uri'])) {
+ $guid = self::guidFromUri($item['uri'], $prefix_host);
} else {
- $arr['network'] = trim(defaults($arr, 'network', NETWORK_PHANTOM));
+ $guid = get_guid(32, hash('crc32', $prefix_host));
+ }
+
+ return $guid;
+ }
+
+ private static function contactId($item)
+ {
+ $contact_id = (int)$item["contact-id"];
+
+ if (!empty($contact_id)) {
+ return $contact_id;
+ }
+ logger('Missing contact-id. Called by: '.System::callstack(), LOGGER_DEBUG);
+ /*
+ * First we are looking for a suitable contact that matches with the author of the post
+ * This is done only for comments
+ */
+ if ($item['parent-uri'] != $item['uri']) {
+ $contact_id = Contact::getIdForURL($item['author-link'], $item['uid']);
+ }
+
+ // If not present then maybe the owner was found
+ if ($contact_id == 0) {
+ $contact_id = Contact::getIdForURL($item['owner-link'], $item['uid']);
}
+ // Still missing? Then use the "self" contact of the current user
+ if ($contact_id == 0) {
+ $self = dba::selectFirst('contact', ['id'], ['self' => true, 'uid' => $item['uid']]);
+ if (DBM::is_result($self)) {
+ $contact_id = $self["id"];
+ }
+ }
+ logger("Contact-id was missing for post ".$item['guid']." from user id ".$item['uid']." - now set to ".$contact_id, LOGGER_DEBUG);
+
+ return $contact_id;
+ }
+
+ public static function insert($item, $force_parent = false, $notify = false, $dontcache = false)
+ {
+ $a = get_app();
+
+ // If it is a posting where users should get notifications, then define it as wall posting
if ($notify) {
- $guid_prefix = "";
- } elseif ((trim($arr['guid']) == "") && (trim($arr['plink']) != "")) {
- $arr['guid'] = self::guidFromUri($arr['plink']);
- } elseif ((trim($arr['guid']) == "") && (trim($arr['uri']) != "")) {
- $arr['guid'] = self::guidFromUri($arr['uri']);
+ $item['wall'] = 1;
+ $item['type'] = 'wall';
+ $item['origin'] = 1;
+ $item['network'] = NETWORK_DFRN;
+ $item['protocol'] = PROTOCOL_DFRN;
} else {
- $parsed = parse_url($arr["author-link"]);
- $guid_prefix = hash("crc32", $parsed["host"]);
+ $item['network'] = trim(defaults($item, 'network', NETWORK_PHANTOM));
}
- $arr['guid'] = notags(trim(defaults($arr, 'guid', get_guid(32, $guid_prefix))));
- $arr['uri'] = notags(trim(defaults($arr, 'uri', item_new_uri($a->get_hostname(), $uid, $arr['guid']))));
+ $item['guid'] = self::guid($item, $notify);
+ $item['uri'] = notags(trim(defaults($item, 'uri', item_new_uri($a->get_hostname(), $item['uid'], $item['guid']))));
// Store conversation data
- $arr = Conversation::insert($arr);
+ $item = Conversation::insert($item);
/*
* If a Diaspora signature structure was passed in, pull it out of the
*/
$dsprsig = null;
- if (x($arr, 'dsprsig')) {
- $encoded_signature = $arr['dsprsig'];
- $dsprsig = json_decode(base64_decode($arr['dsprsig']));
- unset($arr['dsprsig']);
+ if (x($item, 'dsprsig')) {
+ $encoded_signature = $item['dsprsig'];
+ $dsprsig = json_decode(base64_decode($item['dsprsig']));
+ unset($item['dsprsig']);
}
// Converting the plink
/// @TODO Check if this is really still needed
- if ($arr['network'] == NETWORK_OSTATUS) {
- if (isset($arr['plink'])) {
- $arr['plink'] = OStatus::convertHref($arr['plink']);
- } elseif (isset($arr['uri'])) {
- $arr['plink'] = OStatus::convertHref($arr['uri']);
+ if ($item['network'] == NETWORK_OSTATUS) {
+ if (isset($item['plink'])) {
+ $item['plink'] = OStatus::convertHref($item['plink']);
+ } elseif (isset($item['uri'])) {
+ $item['plink'] = OStatus::convertHref($item['uri']);
}
}
- if (x($arr, 'gravity')) {
- $arr['gravity'] = intval($arr['gravity']);
- } elseif ($arr['parent-uri'] === $arr['uri']) {
- $arr['gravity'] = 0;
- } elseif (activity_match($arr['verb'],ACTIVITY_POST)) {
- $arr['gravity'] = 6;
+ if (!empty($item['thr-parent'])) {
+ $item['parent-uri'] = $item['thr-parent'];
+ }
+
+ if (x($item, 'gravity')) {
+ $item['gravity'] = intval($item['gravity']);
+ } elseif ($item['parent-uri'] === $item['uri']) {
+ $item['gravity'] = 0;
+ } elseif (activity_match($item['verb'],ACTIVITY_POST)) {
+ $item['gravity'] = 6;
} else {
- $arr['gravity'] = 6; // extensible catchall
+ $item['gravity'] = 6; // extensible catchall
}
- $arr['type'] = defaults($arr, 'type', 'remote');
+ $item['type'] = defaults($item, 'type', 'remote');
- $uid = intval($arr['uid']);
+ $uid = intval($item['uid']);
// check for create date and expire time
$expire_interval = Config::get('system', 'dbclean-expire-days', 0);
$expire_interval = $user['expire'];
}
- if (($expire_interval > 0) && !empty($arr['created'])) {
+ if (($expire_interval > 0) && !empty($item['created'])) {
$expire_date = time() - ($expire_interval * 86400);
- $created_date = strtotime($arr['created']);
+ $created_date = strtotime($item['created']);
if ($created_date < $expire_date) {
- logger('item-store: item created ('.date('c', $created_date).') before expiration time ('.date('c', $expire_date).'). ignored. ' . print_r($arr,true), LOGGER_DEBUG);
+ logger('item-store: item created ('.date('c', $created_date).') before expiration time ('.date('c', $expire_date).'). ignored. ' . print_r($item,true), LOGGER_DEBUG);
return 0;
}
}
* We have to check several networks since Friendica posts could be repeated
* via OStatus (maybe Diasporsa as well)
*/
- if (in_array($arr['network'], [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 (DBM::is_result($r)) {
+ if (in_array($item['network'], [NETWORK_DIASPORA, NETWORK_DFRN, NETWORK_OSTATUS, ""])) {
+ $condition = ["`uri` = ? AND `uid` = ? AND `network` IN (?, ?, ?)",
+ trim($item['uri']), $item['uid'],
+ NETWORK_DIASPORA, NETWORK_DFRN, NETWORK_OSTATUS];
+ $existing = dba::selectFirst('item', ['id', 'network'], $condition);
+ if (DBM::is_result($existing)) {
// 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']);
+ logger("Item with uri ".$item['uri']." already existed for user ".$uid." with id ".$existing["id"]." target network ".$existing["network"]." - new network: ".$item['network']);
}
- return $r[0]["id"];
+ return $existing["id"];
}
}
- self::addLanguageInPostopts($arr);
-
- $arr['wall'] = intval(defaults($arr, 'wall', 0));
- $arr['extid'] = trim(defaults($arr, 'extid', ''));
- $arr['author-name'] = trim(defaults($arr, 'author-name', ''));
- $arr['author-link'] = trim(defaults($arr, 'author-link', ''));
- $arr['author-avatar'] = trim(defaults($arr, 'author-avatar', ''));
- $arr['owner-name'] = trim(defaults($arr, 'owner-name', ''));
- $arr['owner-link'] = trim(defaults($arr, 'owner-link', ''));
- $arr['owner-avatar'] = trim(defaults($arr, 'owner-avatar', ''));
- $arr['received'] = ((x($arr, 'received') !== false) ? datetime_convert('UTC','UTC', $arr['received']) : datetime_convert());
- $arr['created'] = ((x($arr, 'created') !== false) ? datetime_convert('UTC','UTC', $arr['created']) : $arr['received']);
- $arr['edited'] = ((x($arr, 'edited') !== false) ? datetime_convert('UTC','UTC', $arr['edited']) : $arr['created']);
- $arr['changed'] = ((x($arr, 'changed') !== false) ? datetime_convert('UTC','UTC', $arr['changed']) : $arr['created']);
- $arr['commented'] = ((x($arr, 'commented') !== false) ? datetime_convert('UTC','UTC', $arr['commented']) : $arr['created']);
- $arr['title'] = trim(defaults($arr, 'title', ''));
- $arr['location'] = trim(defaults($arr, 'location', ''));
- $arr['coord'] = trim(defaults($arr, 'coord', ''));
- $arr['visible'] = ((x($arr, 'visible') !== false) ? intval($arr['visible']) : 1);
- $arr['deleted'] = 0;
- $arr['parent-uri'] = trim(defaults($arr, 'parent-uri', $arr['uri']));
- $arr['verb'] = trim(defaults($arr, 'verb', ''));
- $arr['object-type'] = trim(defaults($arr, 'object-type', ''));
- $arr['object'] = trim(defaults($arr, 'object', ''));
- $arr['target-type'] = trim(defaults($arr, 'target-type', ''));
- $arr['target'] = trim(defaults($arr, 'target', ''));
- $arr['plink'] = trim(defaults($arr, 'plink', ''));
- $arr['allow_cid'] = trim(defaults($arr, 'allow_cid', ''));
- $arr['allow_gid'] = trim(defaults($arr, 'allow_gid', ''));
- $arr['deny_cid'] = trim(defaults($arr, 'deny_cid', ''));
- $arr['deny_gid'] = trim(defaults($arr, 'deny_gid', ''));
- $arr['private'] = intval(defaults($arr, 'private', 0));
- $arr['bookmark'] = intval(defaults($arr, 'bookmark', 0));
- $arr['body'] = trim(defaults($arr, 'body', ''));
- $arr['tag'] = trim(defaults($arr, 'tag', ''));
- $arr['attach'] = trim(defaults($arr, 'attach', ''));
- $arr['app'] = trim(defaults($arr, 'app', ''));
- $arr['origin'] = intval(defaults($arr, 'origin', 0));
- $arr['postopts'] = trim(defaults($arr, 'postopts', ''));
- $arr['resource-id'] = trim(defaults($arr, 'resource-id', ''));
- $arr['event-id'] = intval(defaults($arr, 'event-id', 0));
- $arr['inform'] = trim(defaults($arr, 'inform', ''));
- $arr['file'] = trim(defaults($arr, 'file', ''));
+ self::addLanguageInPostopts($item);
+
+ $item['wall'] = intval(defaults($item, 'wall', 0));
+ $item['extid'] = trim(defaults($item, 'extid', ''));
+ $item['author-name'] = trim(defaults($item, 'author-name', ''));
+ $item['author-link'] = trim(defaults($item, 'author-link', ''));
+ $item['author-avatar'] = trim(defaults($item, 'author-avatar', ''));
+ $item['owner-name'] = trim(defaults($item, 'owner-name', ''));
+ $item['owner-link'] = trim(defaults($item, 'owner-link', ''));
+ $item['owner-avatar'] = trim(defaults($item, 'owner-avatar', ''));
+ $item['received'] = ((x($item, 'received') !== false) ? DateTimeFormat::utc($item['received']) : DateTimeFormat::utcNow());
+ $item['created'] = ((x($item, 'created') !== false) ? DateTimeFormat::utc($item['created']) : $item['received']);
+ $item['edited'] = ((x($item, 'edited') !== false) ? DateTimeFormat::utc($item['edited']) : $item['created']);
+ $item['changed'] = ((x($item, 'changed') !== false) ? DateTimeFormat::utc($item['changed']) : $item['created']);
+ $item['commented'] = ((x($item, 'commented') !== false) ? DateTimeFormat::utc($item['commented']) : $item['created']);
+ $item['title'] = trim(defaults($item, 'title', ''));
+ $item['location'] = trim(defaults($item, 'location', ''));
+ $item['coord'] = trim(defaults($item, 'coord', ''));
+ $item['visible'] = ((x($item, 'visible') !== false) ? intval($item['visible']) : 1);
+ $item['deleted'] = 0;
+ $item['parent-uri'] = trim(defaults($item, 'parent-uri', $item['uri']));
+ $item['verb'] = trim(defaults($item, 'verb', ''));
+ $item['object-type'] = trim(defaults($item, 'object-type', ''));
+ $item['object'] = trim(defaults($item, 'object', ''));
+ $item['target-type'] = trim(defaults($item, 'target-type', ''));
+ $item['target'] = trim(defaults($item, 'target', ''));
+ $item['plink'] = trim(defaults($item, 'plink', ''));
+ $item['allow_cid'] = trim(defaults($item, 'allow_cid', ''));
+ $item['allow_gid'] = trim(defaults($item, 'allow_gid', ''));
+ $item['deny_cid'] = trim(defaults($item, 'deny_cid', ''));
+ $item['deny_gid'] = trim(defaults($item, 'deny_gid', ''));
+ $item['private'] = intval(defaults($item, 'private', 0));
+ $item['bookmark'] = intval(defaults($item, 'bookmark', 0));
+ $item['body'] = trim(defaults($item, 'body', ''));
+ $item['tag'] = trim(defaults($item, 'tag', ''));
+ $item['attach'] = trim(defaults($item, 'attach', ''));
+ $item['app'] = trim(defaults($item, 'app', ''));
+ $item['origin'] = intval(defaults($item, 'origin', 0));
+ $item['postopts'] = trim(defaults($item, 'postopts', ''));
+ $item['resource-id'] = trim(defaults($item, 'resource-id', ''));
+ $item['event-id'] = intval(defaults($item, 'event-id', 0));
+ $item['inform'] = trim(defaults($item, 'inform', ''));
+ $item['file'] = trim(defaults($item, 'file', ''));
// When there is no content then we don't post it
- if ($arr['body'].$arr['title'] == '') {
+ if ($item['body'].$item['title'] == '') {
return 0;
}
// Items cannot be stored before they happen ...
- if ($arr['created'] > datetime_convert()) {
- $arr['created'] = datetime_convert();
+ if ($item['created'] > DateTimeFormat::utcNow()) {
+ $item['created'] = DateTimeFormat::utcNow();
}
// We haven't invented time travel by now.
- if ($arr['edited'] > datetime_convert()) {
- $arr['edited'] = datetime_convert();
+ if ($item['edited'] > DateTimeFormat::utcNow()) {
+ $item['edited'] = DateTimeFormat::utcNow();
}
- if (($arr['author-link'] == "") && ($arr['owner-link'] == "")) {
+ if (($item['author-link'] == "") && ($item['owner-link'] == "")) {
logger("Both author-link and owner-link are empty. Called by: " . System::callstack(), LOGGER_DEBUG);
}
- if ($arr['plink'] == "") {
- $arr['plink'] = System::baseUrl() . '/display/' . urlencode($arr['guid']);
- }
-
- if ($arr['network'] == NETWORK_PHANTOM) {
- $r = q("SELECT `network` FROM `contact` WHERE `network` IN ('%s', '%s', '%s') AND `nurl` = '%s' AND `uid` = %d LIMIT 1",
- dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS),
- dbesc(normalise_link($arr['author-link'])),
- intval($arr['uid'])
- );
+ $item['plink'] = defaults($item, 'plink', System::baseUrl() . '/display/' . urlencode($item['guid']));
- if (!DBM::is_result($r)) {
- $r = q("SELECT `network` FROM `gcontact` WHERE `network` IN ('%s', '%s', '%s') AND `nurl` = '%s' LIMIT 1",
- dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA), dbesc(NETWORK_OSTATUS),
- dbesc(normalise_link($arr['author-link']))
- );
- }
-
- if (!DBM::is_result($r)) {
- $r = q("SELECT `network` FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
- intval($arr['contact-id']),
- intval($arr['uid'])
- );
- }
+ // The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes
+ $item["contact-id"] = self::contactId($item);
- if (DBM::is_result($r)) {
- $arr['network'] = $r[0]["network"];
- }
+ $item['author-id'] = defaults($item, 'author-id', Contact::getIdForURL($item["author-link"]));
- // Fallback to friendica (why is it empty in some cases?)
- if ($arr['network'] == "") {
- $arr['network'] = NETWORK_DFRN;
- }
-
- logger("Set network to " . $arr["network"] . " for " . $arr["uri"], LOGGER_DEBUG);
+ if (Contact::isBlocked($item["author-id"])) {
+ logger('Contact '.$item["author-id"].' is blocked, item '.$item["uri"].' will not be stored');
+ return 0;
}
- // The contact-id should be set before "self::insert" was called - but there seems to be some issues
- if ($arr["contact-id"] == 0) {
- /*
- * First we are looking for a suitable contact that matches with the author of the post
- * This is done only for comments (See below explanation at "gcontact-id")
- */
- if ($arr['parent-uri'] != $arr['uri']) {
- $arr["contact-id"] = Contact::getIdForURL($arr['author-link'], $uid);
- }
-
- // If not present then maybe the owner was found
- if ($arr["contact-id"] == 0) {
- $arr["contact-id"] = Contact::getIdForURL($arr['owner-link'], $uid);
- }
-
- // Still missing? Then use the "self" contact of the current user
- if ($arr["contact-id"] == 0) {
- $r = q("SELECT `id` FROM `contact` WHERE `self` AND `uid` = %d", intval($uid));
+ $item['owner-id'] = defaults($item, 'owner-id', Contact::getIdForURL($item["owner-link"]));
- if (DBM::is_result($r)) {
- $arr["contact-id"] = $r[0]["id"];
- }
- }
-
- logger("Contact-id was missing for post ".$arr["guid"]." from user id ".$uid." - now set to ".$arr["contact-id"], LOGGER_DEBUG);
+ if (Contact::isBlocked($item["owner-id"])) {
+ logger('Contact '.$item["owner-id"].' is blocked, item '.$item["uri"].' will not be stored');
+ return 0;
}
- if (!x($arr, "gcontact-id")) {
- /*
- * The gcontact should mostly behave like the contact. But is is supposed to be global for the system.
- * This means that wall posts, repeated posts, etc. should have the gcontact id of the owner.
- * On comments the author is the better choice.
- */
- if ($arr['parent-uri'] === $arr['uri']) {
- $arr["gcontact-id"] = GContact::getId(["url" => $arr['owner-link'], "network" => $arr['network'],
- "photo" => $arr['owner-avatar'], "name" => $arr['owner-name']]);
+ if ($item['network'] == NETWORK_PHANTOM) {
+ logger('Missing network. Called by: '.System::callstack(), LOGGER_DEBUG);
+
+ $contact = Contact::getDetailsByURL($item['author-link'], $item['uid']);
+ if (!empty($contact['network'])) {
+ $item['network'] = $contact["network"];
} else {
- $arr["gcontact-id"] = GContact::getId(["url" => $arr['author-link'], "network" => $arr['network'],
- "photo" => $arr['author-avatar'], "name" => $arr['author-name']]);
+ $item['network'] = NETWORK_DFRN;
}
+ logger("Set network to " . $item["network"] . " for " . $item["uri"], LOGGER_DEBUG);
}
- if ($arr["author-id"] == 0) {
- $arr["author-id"] = Contact::getIdForURL($arr["author-link"], 0);
- }
-
- if (Contact::isBlocked($arr["author-id"])) {
- logger('Contact '.$arr["author-id"].' is blocked, item '.$arr["uri"].' will not be stored');
- return 0;
- }
-
- if ($arr["owner-id"] == 0) {
- $arr["owner-id"] = Contact::getIdForURL($arr["owner-link"], 0);
- }
-
- if (Contact::isBlocked($arr["owner-id"])) {
- logger('Contact '.$arr["owner-id"].' is blocked, item '.$arr["uri"].' will not be stored');
+ // Checking if there is already an item with the same guid
+ logger('Checking for an item for user '.$item['uid'].' on network '.$item['network'].' with the guid '.$item['guid'], LOGGER_DEBUG);
+ $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']];
+ if (dba::exists('item', $condition)) {
+ logger('found item with guid '.$item['guid'].' for user '.$item['uid'].' on network '.$item['network'], LOGGER_DEBUG);
return 0;
}
- 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);
- $r = q("SELECT `guid` FROM `item` WHERE `guid` = '%s' AND `network` = '%s' AND `uid` = '%d' LIMIT 1",
- dbesc($arr['guid']), dbesc($arr['network']), intval($arr['uid']));
-
- if (DBM::is_result($r)) {
- logger('found item with guid '.$arr['guid'].' for user '.$arr['uid'].' on network '.$arr['network'], LOGGER_DEBUG);
- return 0;
- }
- }
-
// Check for hashtags in the body and repair or add hashtag links
- self::setHashtags($arr);
+ self::setHashtags($item);
+
+ $item['thr-parent'] = $item['parent-uri'];
- $arr['thr-parent'] = $arr['parent-uri'];
+ $notify_type = '';
+ $allow_cid = '';
+ $allow_gid = '';
+ $deny_cid = '';
+ $deny_gid = '';
- if ($arr['parent-uri'] === $arr['uri']) {
+ if ($item['parent-uri'] === $item['uri']) {
$parent_id = 0;
$parent_deleted = 0;
- $allow_cid = $arr['allow_cid'];
- $allow_gid = $arr['allow_gid'];
- $deny_cid = $arr['deny_cid'];
- $deny_gid = $arr['deny_gid'];
+ $allow_cid = $item['allow_cid'];
+ $allow_gid = $item['allow_gid'];
+ $deny_cid = $item['deny_cid'];
+ $deny_gid = $item['deny_gid'];
$notify_type = 'wall-new';
} else {
-
// find the parent and snarf the item id and ACLs
// and anything else we need to inherit
- $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d ORDER BY `id` ASC LIMIT 1",
- dbesc($arr['parent-uri']),
- intval($arr['uid'])
- );
-
- if (DBM::is_result($r)) {
+ $fields = ['uri', 'parent-uri', 'id', 'deleted',
+ 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
+ 'wall', 'private', 'forum_mode'];
+ $condition = ['uri' => $item['parent-uri'], 'uid' => $item['uid']];
+ $params = ['order' => ['id' => false]];
+ $parent = dba::selectFirst('item', $fields, $condition, $params);
+ if (DBM::is_result($parent)) {
// is the new message multi-level threaded?
// even though we don't support it now, preserve the info
// and re-attach to the conversation parent.
- if ($r[0]['uri'] != $r[0]['parent-uri']) {
- $arr['parent-uri'] = $r[0]['parent-uri'];
- $z = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `parent-uri` = '%s' AND `uid` = %d
- ORDER BY `id` ASC LIMIT 1",
- dbesc($r[0]['parent-uri']),
- dbesc($r[0]['parent-uri']),
- intval($arr['uid'])
- );
-
- if (DBM::is_result($z)) {
- $r = $z;
+ if ($parent['uri'] != $parent['parent-uri']) {
+ $item['parent-uri'] = $parent['parent-uri'];
+
+ $condition = ['uri' => $item['parent-uri'],
+ 'parent-uri' => $item['parent-uri'],
+ 'uid' => $item['uid']];
+ $params = ['order' => ['id' => false]];
+ $toplevel_parent = dba::selectFirst('item', $fields, $condition, $params);
+
+ if (DBM::is_result($toplevel_parent)) {
+ $parent = $toplevel_parent;
}
}
- $parent_id = $r[0]['id'];
- $parent_deleted = $r[0]['deleted'];
- $allow_cid = $r[0]['allow_cid'];
- $allow_gid = $r[0]['allow_gid'];
- $deny_cid = $r[0]['deny_cid'];
- $deny_gid = $r[0]['deny_gid'];
- $arr['wall'] = $r[0]['wall'];
+ $parent_id = $parent['id'];
+ $parent_deleted = $parent['deleted'];
+ $allow_cid = $parent['allow_cid'];
+ $allow_gid = $parent['allow_gid'];
+ $deny_cid = $parent['deny_cid'];
+ $deny_gid = $parent['deny_gid'];
+ $item['wall'] = $parent['wall'];
$notify_type = 'comment-new';
/*
* This differs from the above settings as it subtly allows comments from
* email correspondents to be private even if the overall thread is not.
*/
- if ($r[0]['private']) {
- $arr['private'] = $r[0]['private'];
+ if ($parent['private']) {
+ $item['private'] = $parent['private'];
}
/*
* The original author commented, but as this is a comment, the permissions
* weren't fixed up so it will still show the comment as private unless we fix it here.
*/
- if ((intval($r[0]['forum_mode']) == 1) && $r[0]['private']) {
- $arr['private'] = 0;
+ if ((intval($parent['forum_mode']) == 1) && $parent['private']) {
+ $item['private'] = 0;
}
// If its a post from myself then tag the thread as "mention"
- logger("Checking if parent ".$parent_id." has to be tagged as mention for user ".$arr['uid'], LOGGER_DEBUG);
- $u = q("SELECT `nickname` FROM `user` WHERE `uid` = %d", intval($arr['uid']));
- if (DBM::is_result($u)) {
- $self = normalise_link(System::baseUrl() . '/profile/' . $u[0]['nickname']);
- logger("'myself' is ".$self." for parent ".$parent_id." checking against ".$arr['author-link']." and ".$arr['owner-link'], LOGGER_DEBUG);
- if ((normalise_link($arr['author-link']) == $self) || (normalise_link($arr['owner-link']) == $self)) {
+ logger("Checking if parent ".$parent_id." has to be tagged as mention for user ".$item['uid'], LOGGER_DEBUG);
+ $user = dba::selectFirst('user', ['nickname'], ['uid' => $item['uid']]);
+ if (DBM::is_result($user)) {
+ $self = normalise_link(System::baseUrl() . '/profile/' . $user['nickname']);
+ logger("'myself' is ".$self." for parent ".$parent_id." checking against ".$item['author-link']." and ".$item['owner-link'], LOGGER_DEBUG);
+ if ((normalise_link($item['author-link']) == $self) || (normalise_link($item['owner-link']) == $self)) {
dba::update('thread', ['mention' => true], ['iid' => $parent_id]);
logger("tagged thread ".$parent_id." as mention for user ".$self, LOGGER_DEBUG);
}
if ($force_parent) {
logger('$force_parent=true, reply converted to top-level post.');
$parent_id = 0;
- $arr['parent-uri'] = $arr['uri'];
- $arr['gravity'] = 0;
+ $item['parent-uri'] = $item['uri'];
+ $item['gravity'] = 0;
} else {
- logger('item parent '.$arr['parent-uri'].' for '.$arr['uid'].' was not found - ignoring item');
+ logger('item parent '.$item['parent-uri'].' for '.$item['uid'].' was not found - ignoring item');
return 0;
}
}
}
- $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 (DBM::is_result($r)) {
- logger('duplicated item with the same uri found. '.print_r($arr,true));
+ $condition = ["`uri` = ? AND `network` IN (?, ?) AND `uid` = ?",
+ $item['uri'], $item['network'], NETWORK_DFRN, $item['uid']];
+ if (dba::exists('item', $condition)) {
+ logger('duplicated item with the same uri found. '.print_r($item,true));
return 0;
}
// On Friendica and Diaspora the GUID is unique
- if (in_array($arr['network'], [NETWORK_DFRN, NETWORK_DIASPORA])) {
- $r = q("SELECT `id` FROM `item` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1",
- dbesc($arr['guid']),
- intval($arr['uid'])
- );
- if (DBM::is_result($r)) {
- logger('duplicated item with the same guid found. '.print_r($arr,true));
+ if (in_array($item['network'], [NETWORK_DFRN, NETWORK_DIASPORA])) {
+ $condition = ['guid' => $item['guid'], 'uid' => $item['uid']];
+ if (dba::exists('item', $condition)) {
+ logger('duplicated item with the same guid found. '.print_r($item,true));
return 0;
}
} else {
// Check for an existing post with the same content. There seems to be a problem with OStatus.
- $r = q("SELECT `id` FROM `item` WHERE `body` = '%s' AND `network` = '%s' AND `created` = '%s' AND `contact-id` = %d AND `uid` = %d LIMIT 1",
- dbesc($arr['body']),
- dbesc($arr['network']),
- dbesc($arr['created']),
- intval($arr['contact-id']),
- intval($arr['uid'])
- );
- if (DBM::is_result($r)) {
- logger('duplicated item with the same body found. '.print_r($arr,true));
+ $condition = ["`body` = ? AND `network` = ? AND `created` = ? AND `contact-id` = ? AND `uid` = ?",
+ $item['body'], $item['network'], $item['created'], $item['contact-id'], $item['uid']];
+ if (dba::exists('item', $condition)) {
+ logger('duplicated item with the same body found. '.print_r($item,true));
return 0;
}
}
// Is this item available in the global items (with uid=0)?
- if ($arr["uid"] == 0) {
- $arr["global"] = true;
+ if ($item["uid"] == 0) {
+ $item["global"] = true;
// Set the global flag on all items if this was a global item entry
- dba::update('item', ['global' => true], ['uri' => $arr["uri"]]);
+ dba::update('item', ['global' => true], ['uri' => $item["uri"]]);
} else {
- $isglobal = q("SELECT `global` FROM `item` WHERE `uid` = 0 AND `uri` = '%s'", dbesc($arr["uri"]));
-
- $arr["global"] = (DBM::is_result($isglobal) && count($isglobal) > 0);
+ $item["global"] = dba::exists('item', ['uid' => 0, 'uri' => $item["uri"]]);
}
// ACL settings
if (strlen($allow_cid) || strlen($allow_gid) || strlen($deny_cid) || strlen($deny_gid)) {
$private = 1;
} else {
- $private = $arr['private'];
+ $private = $item['private'];
}
- $arr["allow_cid"] = $allow_cid;
- $arr["allow_gid"] = $allow_gid;
- $arr["deny_cid"] = $deny_cid;
- $arr["deny_gid"] = $deny_gid;
- $arr["private"] = $private;
- $arr["deleted"] = $parent_deleted;
+ $item["allow_cid"] = $allow_cid;
+ $item["allow_gid"] = $allow_gid;
+ $item["deny_cid"] = $deny_cid;
+ $item["deny_gid"] = $deny_gid;
+ $item["private"] = $private;
+ $item["deleted"] = $parent_deleted;
// Fill the cache field
- put_item_in_cache($arr);
+ put_item_in_cache($item);
if ($notify) {
- Addon::callHooks('post_local', $arr);
+ Addon::callHooks('post_local', $item);
} else {
- Addon::callHooks('post_remote', $arr);
+ Addon::callHooks('post_remote', $item);
}
// This array field is used to trigger some automatic reactions
// It is mainly used in the "post_local" hook.
- unset($arr['api_source']);
+ unset($item['api_source']);
- if (x($arr, 'cancel')) {
+ if (x($item, 'cancel')) {
logger('post cancelled by addon.');
return 0;
}
* There is a timing issue here that sometimes creates double postings.
* An unique index would help - but the limitations of MySQL (maximum size of index values) prevent this.
*/
- if ($arr["uid"] == 0) {
- $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' AND `uid` = 0 LIMIT 1", dbesc(trim($arr['uri'])));
- if (DBM::is_result($r)) {
- logger('Global item already stored. URI: '.$arr['uri'].' on network '.$arr['network'], LOGGER_DEBUG);
+ if ($item["uid"] == 0) {
+ if (dba::exists('item', ['uri' => trim($item['uri']), 'uid' => 0])) {
+ logger('Global item already stored. URI: '.$item['uri'].' on network '.$item['network'], LOGGER_DEBUG);
return 0;
}
}
- logger('' . print_r($arr,true), LOGGER_DATA);
+ logger('' . print_r($item,true), LOGGER_DATA);
dba::transaction();
- $r = dba::insert('item', $arr);
+ $ret = dba::insert('item', $item);
// When the item was successfully stored we fetch the ID of the item.
- if (DBM::is_result($r)) {
+ if (DBM::is_result($ret)) {
$current_post = dba::lastInsertId();
} else {
// This can happen - for example - if there are locking timeouts.
// At first we restore the Diaspora signature that we removed above.
if (isset($encoded_signature)) {
- $arr['dsprsig'] = $encoded_signature;
+ $item['dsprsig'] = $encoded_signature;
}
// Now we store the data in the spool directory
$spoolpath = get_spoolpath();
if ($spoolpath != "") {
$spool = $spoolpath.'/'.$file;
- file_put_contents($spool, json_encode($arr));
+ file_put_contents($spool, json_encode($item));
logger("Item wasn't stored - Item was spooled into file ".$file, LOGGER_DEBUG);
}
return 0;
// How much entries have we created?
// We wouldn't need this query when we could use an unique index - but MySQL has length problems with them.
- $r = q("SELECT COUNT(*) AS `entries` FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `network` = '%s'",
- dbesc($arr['uri']),
- intval($arr['uid']),
- dbesc($arr['network'])
- );
+ $entries = dba::count('item', ['uri' => $item['uri'], 'uid' => $item['uid'], 'network' => $item['network']]);
- if (!DBM::is_result($r)) {
- // This shouldn't happen, since COUNT always works when the database connection is there.
- logger("We couldn't count the stored entries. Very strange ...");
- dba::rollback();
- return 0;
- }
-
- if ($r[0]["entries"] > 1) {
+ if ($entries > 1) {
// There are duplicates. We delete our just created entry.
- logger('Duplicated post occurred. uri = ' . $arr['uri'] . ' uid = ' . $arr['uid']);
+ logger('Duplicated post occurred. uri = ' . $item['uri'] . ' uid = ' . $item['uid']);
// Yes, we could do a rollback here - but we are having many users with MyISAM.
dba::delete('item', ['id' => $current_post]);
dba::commit();
return 0;
- } elseif ($r[0]["entries"] == 0) {
+ } elseif ($entries == 0) {
// This really should never happen since we quit earlier if there were problems.
logger("Something is terribly wrong. We haven't found our created entry.");
dba::rollback();
}
logger('created item '.$current_post);
- self::updateContact($arr);
+ self::updateContact($item);
- if (!$parent_id || ($arr['parent-uri'] === $arr['uri'])) {
+ if (!$parent_id || ($item['parent-uri'] === $item['uri'])) {
$parent_id = $current_post;
}
// Set parent id
dba::update('item', ['parent' => $parent_id], ['id' => $current_post]);
- $arr['id'] = $current_post;
- $arr['parent'] = $parent_id;
+ $item['id'] = $current_post;
+ $item['parent'] = $parent_id;
// update the commented timestamp on the parent
// Only update "commented" if it is really a comment
- if (($arr['verb'] == ACTIVITY_POST) || !Config::get("system", "like_no_comment")) {
- dba::update('item', ['commented' => datetime_convert(), 'changed' => datetime_convert()], ['id' => $parent_id]);
+ if (($item['verb'] == ACTIVITY_POST) || !Config::get("system", "like_no_comment")) {
+ dba::update('item', ['commented' => DateTimeFormat::utcNow(), 'changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
} else {
- dba::update('item', ['changed' => datetime_convert()], ['id' => $parent_id]);
+ dba::update('item', ['changed' => DateTimeFormat::utcNow()], ['id' => $parent_id]);
}
if ($dsprsig) {
'signature' => $dsprsig->signature, 'signer' => $dsprsig->signer]);
}
- $deleted = self::tagDeliver($arr['uid'], $current_post);
+ $deleted = self::tagDeliver($item['uid'], $current_post);
/*
* current post can be deleted if is for a community page and no mention are
* in it.
*/
if (!$deleted && !$dontcache) {
- $r = q('SELECT * FROM `item` WHERE `id` = %d', intval($current_post));
- if (DBM::is_result($r) && (count($r) == 1)) {
+ $posted_item = dba::selectFirst('item', [], ['id' => $current_post]);
+ if (DBM::is_result($posted_item)) {
if ($notify) {
- Addon::callHooks('post_local_end', $r[0]);
+ Addon::callHooks('post_local_end', $posted_item);
} else {
- Addon::callHooks('post_remote_end', $r[0]);
+ Addon::callHooks('post_remote_end', $posted_item);
}
} else {
logger('new item not found in DB, id ' . $current_post);
}
}
- if ($arr['parent-uri'] === $arr['uri']) {
- add_thread($current_post);
+ if ($item['parent-uri'] === $item['uri']) {
+ self::addThread($current_post);
} else {
- update_thread($parent_id);
+ self::updateThread($parent_id);
}
dba::commit();
* Due to deadlock issues with the "term" table we are doing these steps after the commit.
* This is not perfect - but a workable solution until we found the reason for the problem.
*/
- create_tags_from_item($current_post);
- Term::createFromItem($current_post);
+ Term::insertFromTagFieldByItemId($current_post);
+ Term::insertFromFileFieldByItemId($current_post);
- if ($arr['parent-uri'] === $arr['uri']) {
+ if ($item['parent-uri'] === $item['uri']) {
self::addShadow($current_post);
} else {
self::addShadowPost($current_post);
public static function addShadow($itemid)
{
$fields = ['uid', 'wall', 'private', 'moderated', 'visible', 'contact-id', 'deleted', 'network', 'author-id', 'owner-id'];
- $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
+ $condition = ['id' => $itemid, 'parent' => [0, $itemid]];
$item = dba::selectFirst('item', $fields, $condition);
if (!DBM::is_result($item)) {
return;
}
- // Is the public contact configured as hidden?
- if (Contact::isHidden($item["owner-id"]) || Contact::isHidden($item["author-id"])) {
- return;
- }
-
- // Only do these checks if the post isn't a wall post
- if (!$item["wall"]) {
- // Check, if hide-friends is activated - then don't do a shadow entry
- if (dba::exists('profile', ['is-default' => true, 'uid' => $item['uid'], 'hide-friends' => true])) {
- return;
- }
-
- // Check if the contact is hidden or blocked
- if (!dba::exists('contact', ['hidden' => false, 'blocked' => false, 'id' => $item['contact-id']])) {
- return;
- }
- }
-
- // Only add a shadow, if the profile isn't hidden
- if (dba::exists('user', ['uid' => $item['uid'], 'hidewall' => true])) {
- return;
- }
-
$item = dba::selectFirst('item', [], ['id' => $itemid]);
- if (DBM::is_result($item) && ($item["allow_cid"] == '') && ($item["allow_gid"] == '') &&
+ if (DBM::is_result($item) && ($item["allow_cid"] == '') && ($item["allow_gid"] == '') &&
($item["deny_cid"] == '') && ($item["deny_gid"] == '')) {
if (!dba::exists('item', ['uri' => $item['uri'], 'uid' => 0])) {
$item['uid'] = 0;
$item['origin'] = 0;
$item['wall'] = 0;
- $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
+ if ($item['uri'] == $item['parent-uri']) {
+ $item['contact-id'] = Contact::getIdForURL($item['owner-link']);
+ } else {
+ $item['contact-id'] = Contact::getIdForURL($item['author-link']);
+ }
if (in_array($item['type'], ["net-comment", "wall-comment"])) {
$item['type'] = 'remote-comment';
$item['uid'] = 0;
$item['origin'] = 0;
$item['wall'] = 0;
- $item['contact-id'] = Contact::getIdForURL($item['author-link'], 0);
+ $item['contact-id'] = Contact::getIdForURL($item['author-link']);
if (in_array($item['type'], ["net-comment", "wall-comment"])) {
$item['type'] = 'remote-comment';
* if possible and not already present.
* Expects "body" element to exist in $arr.
*/
- private static function addLanguageInPostopts(&$arr)
+ private static function addLanguageInPostopts(&$item)
{
- if (x($arr, 'postopts')) {
- if (strstr($arr['postopts'], 'lang=')) {
+ if (!empty($item['postopts'])) {
+ if (strstr($item['postopts'], 'lang=')) {
// do not override
return;
}
- $postopts = $arr['postopts'];
+ $postopts = $item['postopts'];
} else {
$postopts = "";
}
- $naked_body = preg_replace('/\[(.+?)\]/','', $arr['body']);
- $l = new Text_LanguageDetect();
- $lng = $l->detect($naked_body, 3);
+ $naked_body = Text\BBCode::toPlaintext($item['body'], false);
+
+ $languages = (new Text_LanguageDetect())->detect($naked_body, 3);
- if (sizeof($lng) > 0) {
- if ($postopts != "") {
+ if (sizeof($languages) > 0) {
+ if ($postopts != '') {
$postopts .= '&'; // arbitrary separator, to be reviewed
}
$postopts .= 'lang=';
$sep = "";
- foreach ($lng as $language => $score) {
+ foreach ($languages as $language => $score) {
$postopts .= $sep . $language . ";" . $score;
$sep = ':';
}
- $arr['postopts'] = $postopts;
+ $item['postopts'] = $postopts;
}
}
* @brief Creates an unique guid out of a given uri
*
* @param string $uri uri of an item entry
- * @param string $host (Optional) hostname for the GUID prefix
+ * @param string $host hostname for the GUID prefix
* @return string unique guid
*/
- public static function guidFromUri($uri, $host = "")
+ public static function guidFromUri($uri, $host)
{
// Our regular guid routine is using this kind of prefix as well
// We have to avoid that different routines could accidentally create the same value
$parsed = parse_url($uri);
- // When the hostname isn't given, we take it from the uri
- if ($host == "") {
- // Is it in the format data@host.tld?
- if ((count($parsed) == 1) && strstr($uri, '@')) {
- $mailparts = explode('@', $uri);
- $host = array_pop($mailparts);
- } else {
- $host = $parsed["host"];
- }
- }
-
// We use a hash of the hostname as prefix for the guid
$guid_prefix = hash("crc32", $host);
private static function updateContact($arr)
{
// Unarchive the author
- $contact = dba::selectFirst('contact', [], ['id' => $arr["author-link"]]);
- if ($contact['term-date'] > NULL_DATE) {
- Contact::unmarkForArchival($contact);
+ $contact = dba::selectFirst('contact', [], ['id' => $arr["author-id"]]);
+ if (DBM::is_result($contact)) {
+ Contact::unmarkForArchival($contact);
}
- // Unarchive the contact if it is a toplevel posting
- if ($arr["parent-uri"] === $arr["uri"]) {
- $contact = dba::selectFirst('contact', [], ['id' => $arr["contact-id"]]);
- if ($contact['term-date'] > NULL_DATE) {
- Contact::unmarkForArchival($contact);
- }
+ // Unarchive the contact if it's not our own contact
+ $contact = dba::selectFirst('contact', [], ['id' => $arr["contact-id"], 'self' => false]);
+ if (DBM::is_result($contact)) {
+ Contact::unmarkForArchival($contact);
}
$update = (!$arr['private'] && (($arr["author-link"] === $arr["owner-link"]) || ($arr["parent-uri"] === $arr["uri"])));
// Is it a forum? Then we don't care about the rules from above
if (!$update && ($arr["network"] == NETWORK_DFRN) && ($arr["parent-uri"] === $arr["uri"])) {
- $isforum = q("SELECT `forum` FROM `contact` WHERE `id` = %d AND `forum`",
- intval($arr['contact-id']));
- if (DBM::is_result($isforum)) {
+ if (dba::exists('contact', ['id' => $arr['contact-id'], 'forum' => true])) {
$update = true;
}
}
public static function getGuidById($id)
{
- $r = q("SELECT `guid` FROM `item` WHERE `id` = %d LIMIT 1", intval($id));
- if (DBM::is_result($r)) {
- return $r[0]["guid"];
+ $item = dba::selectFirst('item', ['guid'], ['id' => $id]);
+ if (DBM::is_result($item)) {
+ return $item['guid'];
} else {
- return "";
+ return '';
}
}
// Does the given user have this item?
if ($uid) {
- $r = q("SELECT `item`.`id`, `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
- WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 AND `item`.`moderated` = 0
- AND `item`.`guid` = '%s' AND `item`.`uid` = %d", dbesc($guid), intval($uid));
- if (DBM::is_result($r)) {
- $id = $r[0]["id"];
- $nick = $r[0]["nickname"];
+ $item = dba::fetch_first("SELECT `item`.`id`, `user`.`nickname` FROM `item`
+ INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
+ WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
+ AND `item`.`guid` = ? AND `item`.`uid` = ?", $guid, $uid);
+ if (DBM::is_result($item)) {
+ $id = $item["id"];
+ $nick = $item["nickname"];
}
}
// Or is it anywhere on the server?
if ($nick == "") {
- $r = q("SELECT `item`.`id`, `user`.`nickname` FROM `item` INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
- WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 AND `item`.`moderated` = 0
+ $item = dba::fetch_first("SELECT `item`.`id`, `user`.`nickname` FROM `item`
+ INNER JOIN `user` ON `user`.`uid` = `item`.`uid`
+ WHERE `item`.`visible` AND NOT `item`.`deleted` AND NOT `item`.`moderated`
AND `item`.`allow_cid` = '' AND `item`.`allow_gid` = ''
AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
- AND `item`.`private` = 0 AND `item`.`wall` = 1
- AND `item`.`guid` = '%s'", dbesc($guid));
- if (DBM::is_result($r)) {
- $id = $r[0]["id"];
- $nick = $r[0]["nickname"];
+ AND NOT `item`.`private` AND `item`.`wall`
+ AND `item`.`guid` = ?", $guid);
+ if (DBM::is_result($item)) {
+ $id = $item["id"];
+ $nick = $item["nickname"];
}
}
return ["nick" => $nick, "id" => $id];
{
$mention = false;
- $u = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1",
- intval($uid)
- );
- if (!DBM::is_result($u)) {
+ $user = dba::selectFirst('user', [], ['uid' => $uid]);
+ if (!DBM::is_result($user)) {
return;
}
- $community_page = (($u[0]['page-flags'] == PAGE_COMMUNITY) ? true : false);
- $prvgroup = (($u[0]['page-flags'] == PAGE_PRVGROUP) ? true : false);
+ $community_page = (($user['page-flags'] == PAGE_COMMUNITY) ? true : false);
+ $prvgroup = (($user['page-flags'] == PAGE_PRVGROUP) ? true : false);
- $i = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
- intval($item_id),
- intval($uid)
- );
- if (!DBM::is_result($i)) {
+ $item = dba::selectFirst('item', [], ['id' => $item_id]);
+ if (!DBM::is_result($item)) {
return;
}
- $item = $i[0];
-
- $link = normalise_link(System::baseUrl() . '/profile/' . $u[0]['nickname']);
+ $link = normalise_link(System::baseUrl() . '/profile/' . $user['nickname']);
/*
* Diaspora uses their own hardwired link URL in @-tags
* instead of the one we supply with webfinger
*/
- $dlink = normalise_link(System::baseUrl() . '/u/' . $u[0]['nickname']);
+ $dlink = normalise_link(System::baseUrl() . '/u/' . $user['nickname']);
$cnt = preg_match_all('/[\@\!]\[url\=(.*?)\](.*?)\[\/url\]/ism', $item['body'], $matches, PREG_SET_ORDER);
if ($cnt) {
!$item['wall'] && !$item['origin'] && ($item['id'] == $item['parent'])) {
// mmh.. no mention.. community page or private group... no wall.. no origin.. top-post (not a comment)
// delete it!
- logger("no-mention top-level post to communuty or private group. delete.");
+ logger("no-mention top-level post to community or private group. delete.");
dba::delete('item', ['id' => $item_id]);
return true;
}
return;
}
- $arr = ['item' => $item, 'user' => $u[0], 'contact' => $r[0]];
+ $arr = ['item' => $item, 'user' => $user];
Addon::callHooks('tagged', $arr);
}
// now change this copy of the post to a forum head message and deliver to all the tgroup members
- $c = q("SELECT `name`, `url`, `thumb` FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
- intval($u[0]['uid'])
- );
- if (!DBM::is_result($c)) {
+ $self = dba::selectFirst('contact', ['id', 'name', 'url', 'thumb'], ['uid' => $uid, 'self' => true]);
+ if (!DBM::is_result($self)) {
return;
}
+ $owner_id = Contact::getIdForURL($self['url']);
+
// also reset all the privacy bits to the forum default permissions
- $private = ($u[0]['allow_cid'] || $u[0]['allow_gid'] || $u[0]['deny_cid'] || $u[0]['deny_gid']) ? 1 : 0;
-
- $forum_mode = (($prvgroup) ? 2 : 1);
-
- q("UPDATE `item` SET `wall` = 1, `origin` = 1, `forum_mode` = %d, `owner-name` = '%s', `owner-link` = '%s', `owner-avatar` = '%s',
- `private` = %d, `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' WHERE `id` = %d",
- intval($forum_mode),
- dbesc($c[0]['name']),
- dbesc($c[0]['url']),
- dbesc($c[0]['thumb']),
- intval($private),
- dbesc($u[0]['allow_cid']),
- dbesc($u[0]['allow_gid']),
- dbesc($u[0]['deny_cid']),
- dbesc($u[0]['deny_gid']),
- intval($item_id)
- );
- update_thread($item_id);
+ $private = ($user['allow_cid'] || $user['allow_gid'] || $user['deny_cid'] || $user['deny_gid']) ? 1 : 0;
- Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', 'tgroup', $item_id);
+ $forum_mode = ($prvgroup ? 2 : 1);
+
+ $fields = ['wall' => true, 'origin' => true, 'forum_mode' => $forum_mode, 'contact-id' => $self['id'],
+ 'owner-id' => $owner_id, 'owner-name' => $self['name'], 'owner-link' => $self['url'],
+ 'owner-avatar' => $self['thumb'], 'private' => $private, 'allow_cid' => $user['allow_cid'],
+ 'allow_gid' => $user['allow_gid'], 'deny_cid' => $user['deny_cid'], 'deny_gid' => $user['deny_gid']];
+ dba::update('item', $fields, ['id' => $item_id]);
+
+ self::updateThread($item_id);
+ Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', 'tgroup', $item_id);
}
public static function isRemoteSelf($contact, &$datarray)
$datarray2 = $datarray;
logger('remote-self start - Contact '.$contact['url'].' - '.$contact['remote_self'].' Item '.print_r($datarray, true), LOGGER_DEBUG);
if ($contact['remote_self'] == 2) {
- $r = q("SELECT `id`,`url`,`name`,`thumb` FROM `contact` WHERE `uid` = %d AND `self`",
- intval($contact['uid']));
- if (DBM::is_result($r)) {
- $datarray['contact-id'] = $r[0]["id"];
+ $self = dba::selectFirst('contact', ['id', 'name', 'url', 'thumb'],
+ ['uid' => $contact['uid'], 'self' => true]);
+ if (DBM::is_result($self)) {
+ $datarray['contact-id'] = $self["id"];
- $datarray['owner-name'] = $r[0]["name"];
- $datarray['owner-link'] = $r[0]["url"];
- $datarray['owner-avatar'] = $r[0]["thumb"];
+ $datarray['owner-name'] = $self["name"];
+ $datarray['owner-link'] = $self["url"];
+ $datarray['owner-avatar'] = $self["thumb"];
$datarray['author-name'] = $datarray['owner-name'];
$datarray['author-link'] = $datarray['owner-link'];
if ($x) {
$res = substr($i, $x + 1);
$i = substr($i, 0, $x);
- $r = q("SELECT * FROM `photo` WHERE `resource-id` = '%s' AND `scale` = %d AND `uid` = %d",
- dbesc($i),
- intval($res),
- intval($uid)
- );
- if (DBM::is_result($r)) {
+ $fields = ['data', 'type', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
+ $photo = dba::selectFirst('photo', $fields, ['resource-id' => $i, 'scale' => $res, 'uid' => $uid]);
+ if (DBM::is_result($photo)) {
/*
* Check to see if we should replace this photo link with an embedded image
* 1. No need to do so if the photo is public
* permissions, regardless of order but first check to see if they're an exact
* match to save some processing overhead.
*/
- if (self::hasPermissions($r[0])) {
+ if (self::hasPermissions($photo)) {
if ($cid) {
- $recips = self::enumeratePermissions($r[0]);
+ $recips = self::enumeratePermissions($photo);
if (in_array($cid, $recips)) {
$replace = true;
}
} elseif ($item) {
- if (self::samePermissions($item, $r[0])) {
+ if (self::samePermissions($item, $photo)) {
$replace = true;
}
}
}
if ($replace) {
- $data = $r[0]['data'];
- $type = $r[0]['type'];
+ $data = $photo['data'];
+ $type = $photo['type'];
// If a custom width and height were specified, apply before embedding
if (preg_match("/\[img\=([0-9]*)x([0-9]*)\]/is", substr($orig_body, $img_start, $img_st_close), $match)) {
continue;
}
- self::delete($item['id'], PRIORITY_LOW);
+ self::deleteById($item['id'], PRIORITY_LOW);
}
}
- /// @TODO: This query seems to be really slow
public static function firstPostDate($uid, $wall = false)
{
- $r = q("SELECT `id`, `created` FROM `item`
- WHERE `uid` = %d AND `wall` = %d AND `deleted` = 0 AND `visible` = 1 AND `moderated` = 0
- AND `id` = `parent`
- ORDER BY `created` ASC LIMIT 1",
- intval($uid),
- intval($wall ? 1 : 0)
- );
- if (DBM::is_result($r)) {
- return substr(datetime_convert('',date_default_timezone_get(), $r[0]['created']),0,10);
+ $condition = ['uid' => $uid, 'wall' => $wall, 'deleted' => false, 'visible' => true, 'moderated' => false];
+ $params = ['order' => ['created' => false]];
+ $thread = dba::selectFirst('thread', ['created'], $condition, $params);
+ if (DBM::is_result($thread)) {
+ return substr(DateTimeFormat::local($thread['created']), 0, 10);
}
return false;
}
$item_contact_id = $owner_self_contact['id'];
$item_contact = $owner_self_contact;
} else {
- $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid);
+ $item_contact_id = Contact::getIdForURL($author_contact['url'], $uid, true);
$item_contact = dba::selectFirst('contact', [], ['id' => $item_contact_id]);
if (!DBM::is_result($item_contact)) {
logger('like: unknown item contact ' . $item_contact_id);
$verbs = "'".dbesc($activity)."'";
}
+ /// @todo This query is expected to be a performance eater due to the "OR" - it has to be changed totally
$existing_like = q("SELECT `id`, `guid`, `verb` FROM `item`
WHERE `verb` IN ($verbs)
AND `deleted` = 0
$like_item = $existing_like[0];
// Already voted, undo it
- $fields = ['deleted' => true, 'unseen' => true, 'changed' => datetime_convert()];
+ $fields = ['deleted' => true, 'unseen' => true, 'changed' => DateTimeFormat::utcNow()];
dba::update('item', $fields, ['id' => $like_item['id']]);
// Clean up the Diaspora signatures for this like
'unseen' => 1,
];
- $new_item_id = Item::insert($new_item);
+ $new_item_id = self::insert($new_item);
- // @todo: Explain this block
- if (! $item['visible']) {
- q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d",
- intval($item['id'])
- );
+ // If the parent item isn't visible then set it to visible
+ if (!$item['visible']) {
+ self::update(['visible' => true], ['id' => $item['id']]);
}
// Save the author information for the like in case we need to relay to Diaspora
return true;
}
+
+ private static function addThread($itemid, $onlyshadow = false)
+ {
+ $fields = ['uid', 'created', 'edited', 'commented', 'received', 'changed', 'wall', 'private', 'pubmail',
+ 'moderated', 'visible', 'spam', 'starred', 'bookmark', 'contact-id',
+ 'deleted', 'origin', 'forum_mode', 'mention', 'network', 'author-id', 'owner-id'];
+ $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
+ $item = dba::selectFirst('item', $fields, $condition);
+
+ if (!DBM::is_result($item)) {
+ return;
+ }
+
+ $item['iid'] = $itemid;
+
+ if (!$onlyshadow) {
+ $result = dba::insert('thread', $item);
+
+ logger("Add thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
+ }
+ }
+
+ private static function updateThread($itemid, $setmention = false)
+ {
+ $fields = ['uid', 'guid', 'title', 'body', 'created', 'edited', 'commented', 'received', 'changed',
+ 'wall', 'private', 'pubmail', 'moderated', 'visible', 'spam', 'starred', 'bookmark', 'contact-id',
+ 'deleted', 'origin', 'forum_mode', 'network', 'rendered-html', 'rendered-hash'];
+ $condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
+
+ $item = dba::selectFirst('item', $fields, $condition);
+ if (!DBM::is_result($item)) {
+ return;
+ }
+
+ if ($setmention) {
+ $item["mention"] = 1;
+ }
+
+ $sql = "";
+
+ $fields = [];
+
+ foreach ($item as $field => $data) {
+ if (!in_array($field, ["guid", "title", "body", "rendered-html", "rendered-hash"])) {
+ $fields[$field] = $data;
+ }
+ }
+
+ $result = dba::update('thread', $fields, ['iid' => $itemid]);
+
+ logger("Update thread for item ".$itemid." - guid ".$item["guid"]." - ".(int)$result." ".print_r($item, true), LOGGER_DEBUG);
+
+ // Updating a shadow item entry
+ $items = dba::selectFirst('item', ['id'], ['guid' => $item['guid'], 'uid' => 0]);
+
+ if (!DBM::is_result($items)) {
+ return;
+ }
+
+ $fields = ['title' => $item['title'], 'body' => $item['body'],
+ 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']];
+ $result = dba::update('item', $fields, ['id' => $items['id']]);
+
+ logger("Updating public shadow for post ".$items["id"]." - guid ".$item["guid"]." Result: ".print_r($result, true), LOGGER_DEBUG);
+ }
+
+ private static function deleteThread($itemid, $itemuri = "")
+ {
+ $item = dba::selectFirst('thread', ['uid'], ['iid' => $itemid]);
+ if (!DBM::is_result($item)) {
+ logger('No thread found for id '.$itemid, LOGGER_DEBUG);
+ return;
+ }
+
+ // Using dba::delete at this time could delete the associated item entries
+ $result = dba::e("DELETE FROM `thread` WHERE `iid` = ?", $itemid);
+
+ logger("deleteThread: Deleted thread for item ".$itemid." - ".print_r($result, true), LOGGER_DEBUG);
+
+ if ($itemuri != "") {
+ $condition = ["`uri` = ? AND NOT `deleted` AND NOT (`uid` IN (?, 0))", $itemuri, $item["uid"]];
+ if (!dba::exists('item', $condition)) {
+ dba::delete('item', ['uri' => $itemuri, 'uid' => 0]);
+ logger("deleteThread: Deleted shadow for item ".$itemuri, LOGGER_DEBUG);
+ }
+ }
+ }
}