X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=mod%2Fitem.php;h=0deade41811fb4e0811f0063736c0df47c664403;hb=13150c09a56de662677ee17a1614a2ee55931890;hp=60e6931583426f08bc91f2827972fbecd40be94d;hpb=28f97845caecd1c51cf0d005a9dd1d3cbe9c299c;p=friendica.git diff --git a/mod/item.php b/mod/item.php index 60e6931583..0deade4181 100644 --- a/mod/item.php +++ b/mod/item.php @@ -25,6 +25,7 @@ require_once('include/text.php'); require_once('include/items.php'); require_once('include/Scrape.php'); require_once('include/diaspora.php'); +require_once('include/Contact.php'); function item_post(&$a) { @@ -114,7 +115,7 @@ function item_post(&$a) { if(($r === false) || (! count($r))) { notice( t('Unable to locate original post.') . EOL); if(x($_REQUEST,'return')) - goaway($a->get_baseurl() . "/" . $return_path ); + goaway($return_path); killme(); } $parent_item = $r[0]; @@ -129,7 +130,7 @@ function item_post(&$a) { intval($parent_item['contact-id']), intval($uid) ); - if(count($r)) + if (dbm::is_result($r)) $parent_contact = $r[0]; // If the contact id doesn't fit with the contact, then set the contact to null @@ -174,6 +175,19 @@ function item_post(&$a) { $app = ((x($_REQUEST,'source')) ? strip_tags($_REQUEST['source']) : ''); $extid = ((x($_REQUEST,'extid')) ? strip_tags($_REQUEST['extid']) : ''); + // Check for multiple posts with the same message id (when the post was created via API) + if (($message_id != '') AND ($profile_uid != 0)) { + $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", + dbesc($message_id), + intval($profile_uid) + ); + + if (dbm::is_result($r)) { + logger("Message with URI ".$message_id." already exists for user ".$profile_uid, LOGGER_DEBUG); + return; + } + } + $allow_moderated = false; // here is where we are going to check for permission to post a moderated comment. @@ -183,7 +197,7 @@ function item_post(&$a) { if((x($_REQUEST,'commenter')) && ((! $parent) || (! $parent_item['wall']))) { notice( t('Permission denied.') . EOL) ; if(x($_REQUEST,'return')) - goaway($a->get_baseurl() . "/" . $return_path ); + goaway($return_path); killme(); } @@ -195,7 +209,7 @@ function item_post(&$a) { if((! can_write_wall($a,$profile_uid)) && (! $allow_moderated)) { notice( t('Permission denied.') . EOL) ; if(x($_REQUEST,'return')) - goaway($a->get_baseurl() . "/" . $return_path ); + goaway($return_path); killme(); } @@ -219,7 +233,7 @@ function item_post(&$a) { $r = q("SELECT * FROM `user` WHERE `uid` = %d LIMIT 1", intval($profile_uid) ); - if(count($r)) + if (dbm::is_result($r)) $user = $r[0]; if($orig_post) { @@ -291,7 +305,6 @@ function item_post(&$a) { // If this is a comment, set the permissions from the parent. if($parent_item) { - $private = 0; // for non native networks use the network of the original post as network of the item if (($parent_item['network'] != NETWORK_DIASPORA) @@ -299,19 +312,13 @@ function item_post(&$a) { AND ($network == "")) $network = $parent_item['network']; - if(($parent_item['private']) - || strlen($parent_item['allow_cid']) - || strlen($parent_item['allow_gid']) - || strlen($parent_item['deny_cid']) - || strlen($parent_item['deny_gid'])) { - $private = (($parent_item['private']) ? $parent_item['private'] : 1); - } - $str_contact_allow = $parent_item['allow_cid']; $str_group_allow = $parent_item['allow_gid']; $str_contact_deny = $parent_item['deny_cid']; $str_group_deny = $parent_item['deny_gid']; + $private = $parent_item['private']; } + $pubmail_enable = ((x($_REQUEST,'pubmail_enable') && intval($_REQUEST['pubmail_enable']) && (! $private)) ? 1 : 0); // if using the API, we won't see pubmail_enable - figure out if it should be set @@ -332,7 +339,7 @@ function item_post(&$a) { killme(); info( t('Empty post discarded.') . EOL ); if(x($_REQUEST,'return')) - goaway($a->get_baseurl() . "/" . $return_path ); + goaway($return_path); killme(); } } @@ -391,7 +398,7 @@ function item_post(&$a) { } } - if(count($r)) { + if (dbm::is_result($r)) { $author = $r[0]; $contact_id = $author['id']; } @@ -405,7 +412,7 @@ function item_post(&$a) { $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1", intval($profile_uid) ); - if(count($r)) + if (dbm::is_result($r)) $contact_record = $r[0]; } @@ -459,7 +466,6 @@ function item_post(&$a) { if(! count($r)) continue; - $r = q("UPDATE `photo` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' WHERE `resource-id` = '%s' AND `uid` = %d AND `album` = '%s' ", dbesc($str_contact_allow), @@ -470,7 +476,6 @@ function item_post(&$a) { intval($profile_uid), dbesc( t('Wall Photos')) ); - } } } @@ -490,7 +495,7 @@ function item_post(&$a) { intval($profile_uid), intval($attach) ); - if(count($r)) { + if (dbm::is_result($r)) { $r = q("UPDATE `attach` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s' WHERE `uid` = %d AND `id` = %d", dbesc($str_contact_allow), @@ -505,10 +510,11 @@ function item_post(&$a) { } } - // embedded bookmark in post? set bookmark flag + // embedded bookmark or attachment in post? set bookmark flag $bookmark = 0; - if(preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism",$body,$match,PREG_SET_ORDER)) { + $data = get_attachment_data($body); + if (preg_match_all("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/ism", $body, $match, PREG_SET_ORDER) OR isset($data["type"])) { $objecttype = ACTIVITY_OBJ_BOOKMARK; $bookmark = 1; } @@ -630,7 +636,7 @@ function item_post(&$a) { intval($profile_uid), intval($mtch) ); - if(count($r)) { + if (dbm::is_result($r)) { if(strlen($attachments)) $attachments .= ','; $attachments .= '[attach]href="' . $a->get_baseurl() . '/attach/' . $r[0]['id'] . '" length="' . $r[0]['filesize'] . '" type="' . $r[0]['filetype'] . '" title="' . (($r[0]['filename']) ? $r[0]['filename'] : '') . '"[/attach]'; @@ -675,9 +681,11 @@ function item_post(&$a) { $datarray['owner-name'] = $contact_record['name']; $datarray['owner-link'] = $contact_record['url']; $datarray['owner-avatar'] = $contact_record['thumb']; + $datarray["owner-id"] = get_contact($datarray["owner-link"], 0); $datarray['author-name'] = $author['name']; $datarray['author-link'] = $author['url']; $datarray['author-avatar'] = $author['thumb']; + $datarray["author-id"] = get_contact($datarray["author-link"], 0); $datarray['created'] = datetime_convert(); $datarray['edited'] = datetime_convert(); $datarray['commented'] = datetime_convert(); @@ -710,6 +718,7 @@ function item_post(&$a) { $datarray['moderated'] = $allow_moderated; $datarray['gcontact-id'] = get_gcontact_id(array("url" => $datarray['author-link'], "network" => $datarray['network'], "photo" => $datarray['author-avatar'], "name" => $datarray['author-name'])); + /** * These fields are for the convenience of plugins... * 'self' if true indicates the owner is posting on their own wall @@ -720,6 +729,11 @@ function item_post(&$a) { $datarray['self'] = $self; // $datarray['prvnets'] = $user['prvnets']; + $datarray['parent-uri'] = ($parent == 0) ? $uri : $parent_item['uri']; + $datarray['plink'] = $a->get_baseurl().'/display/'.urlencode($datarray['guid']); + $datarray['last-child'] = 1; + $datarray['visible'] = 1; + if($orig_post) $datarray['edit'] = true; @@ -730,6 +744,9 @@ function item_post(&$a) { if($preview) { require_once('include/conversation.php'); + // We set the datarray ID to -1 because in preview mode the dataray + // doesn't have an ID. + $datarray["id"] = -1; $o = conversation($a,array(array_merge($contact_record,$datarray)),'search', false, true); logger('preview: ' . $o); echo json_encode(array('preview' => $o)); @@ -742,7 +759,7 @@ function item_post(&$a) { if(x($datarray,'cancel')) { logger('mod_item: post cancelled by plugin.'); if($return_path) { - goaway($a->get_baseurl() . "/" . $return_path); + goaway($return_path); } $json = array('cancel' => 1); @@ -778,21 +795,38 @@ function item_post(&$a) { // update filetags in pconfig file_tag_update_pconfig($uid,$categories_old,$categories_new,'category'); - proc_run('php', "include/notifier.php", 'edit_post', "$post_id"); + proc_run(PRIORITY_HIGH, "include/notifier.php", 'edit_post', $post_id); if((x($_REQUEST,'return')) && strlen($return_path)) { logger('return: ' . $return_path); - goaway($a->get_baseurl() . "/" . $return_path ); + goaway($return_path); } killme(); - } - else + } else $post_id = 0; - - $r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`,`owner-name`,`owner-link`,`owner-avatar`, `author-name`, `author-link`, `author-avatar`, - `created`, `edited`, `commented`, `received`, `changed`, `uri`, `thr-parent`, `title`, `body`, `app`, `location`, `coord`, `tag`, `inform`, `verb`, `object-type`, `postopts`, - `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private`, `pubmail`, `attach`, `bookmark`,`origin`, `moderated`, `file`, `rendered-html`, `rendered-hash`) - VALUES( '%s', '%s', %d, '%s', %d, %d, '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, %d, %d, '%s', '%s', '%s')", + q("COMMIT"); + q("START TRANSACTION;"); + + $r = q("INSERT INTO `item` (`guid`, `extid`, `uid`,`type`,`wall`,`gravity`, `network`, `contact-id`, + `owner-name`,`owner-link`,`owner-avatar`, `owner-id`, + `author-name`, `author-link`, `author-avatar`, `author-id`, + `created`, `edited`, `commented`, `received`, `changed`, + `uri`, `thr-parent`, `title`, `body`, `app`, `location`, `coord`, + `tag`, `inform`, `verb`, `object-type`, `postopts`, + `allow_cid`, `allow_gid`, `deny_cid`, `deny_gid`, `private`, + `pubmail`, `attach`, `bookmark`,`origin`, `moderated`, `file`, + `rendered-html`, `rendered-hash`, + `parent`, `parent-uri`, `plink`, `last-child`, `visible`) + VALUES('%s', '%s', %d, '%s', %d, %d, '%s', %d, + '%s', '%s', '%s', %d, + '%s', '%s', '%s', %d, + '%s', '%s', '%s', '%s', '%s', + '%s', '%s', '%s', '%s', '%s', '%s', '%s', + '%s', '%s', '%s', '%s', '%s', + '%s', '%s', '%s', '%s', %d, + %d, '%s', %d, %d, %d, '%s', + '%s', '%s', + %d, '%s', '%s', %d, %d)", dbesc($datarray['guid']), dbesc($datarray['extid']), intval($datarray['uid']), @@ -804,9 +838,11 @@ function item_post(&$a) { dbesc($datarray['owner-name']), dbesc($datarray['owner-link']), dbesc($datarray['owner-avatar']), + intval($datarray['owner-id']), dbesc($datarray['author-name']), dbesc($datarray['author-link']), dbesc($datarray['author-avatar']), + intval($datarray['author-id']), dbesc($datarray['created']), dbesc($datarray['edited']), dbesc($datarray['commented']), @@ -836,23 +872,39 @@ function item_post(&$a) { intval($datarray['moderated']), dbesc($datarray['file']), dbesc($datarray['rendered-html']), - dbesc($datarray['rendered-hash']) + dbesc($datarray['rendered-hash']), + intval($datarray['parent']), + dbesc($datarray['parent-uri']), + dbesc($datarray['plink']), + intval($datarray['last-child']), + intval($datarray['visible']) ); - $r = q("SELECT `id` FROM `item` WHERE `uri` = '%s' LIMIT 1", - dbesc($datarray['uri'])); - if(!count($r)) { + if (dbm::is_result($r)) { + $r = q("SELECT LAST_INSERT_ID() AS `item-id`"); + if (dbm::is_result($r)) { + $post_id = $r[0]['item-id']; + } else { + $post_id = 0; + } + } else { + logger('mod_item: unable to create post.'); + $post_id = 0; + } + + if ($post_id == 0) { + q("COMMIT"); logger('mod_item: unable to retrieve post that was just stored.'); - notice( t('System error. Post not saved.') . EOL); - goaway($a->get_baseurl() . "/" . $return_path ); + notice(t('System error. Post not saved.') . EOL); + goaway($return_path); // NOTREACHED } - $post_id = $r[0]['id']; logger('mod_item: saved item ' . $post_id); $datarray["id"] = $post_id; - $datarray["plink"] = $a->get_baseurl().'/display/'.urlencode($datarray["guid"]); + + item_set_last_item($datarray); // update filetags in pconfig file_tag_update_pconfig($uid,$categories_old,$categories_new,'category'); @@ -860,22 +912,17 @@ function item_post(&$a) { if($parent) { // This item is the last leaf and gets the comment box, clear any ancestors - $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d ", + $r = q("UPDATE `item` SET `last-child` = 0, `changed` = '%s' WHERE `parent` = %d AND `last-child` AND `id` != %d", dbesc(datetime_convert()), - intval($parent) + intval($parent), + intval($post_id) ); - update_thread($parent, true); - - // Inherit ACLs from the parent item. - $r = q("UPDATE `item` SET `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `private` = %d - WHERE `id` = %d", - dbesc($parent_item['allow_cid']), - dbesc($parent_item['allow_gid']), - dbesc($parent_item['deny_cid']), - dbesc($parent_item['deny_gid']), - intval($parent_item['private']), - intval($post_id) + // update the commented timestamp on the parent + q("UPDATE `item` SET `visible` = 1, `commented` = '%s', `changed` = '%s' WHERE `id` = %d", + dbesc(datetime_convert()), + dbesc(datetime_convert()), + intval($parent) ); if($contact_record != $author) { @@ -906,6 +953,10 @@ function item_post(&$a) { } else { $parent = $post_id; + $r = q("UPDATE `item` SET `parent` = %d WHERE `id` = %d", + intval($parent), + intval($post_id)); + if($contact_record != $author) { notification(array( 'type' => NOTIFY_WALL, @@ -925,41 +976,6 @@ function item_post(&$a) { } } - // fallback so that parent always gets set to non-zero. - - if(! $parent) - $parent = $post_id; - - $r = q("UPDATE `item` SET `parent` = %d, `parent-uri` = '%s', `plink` = '%s', `changed` = '%s', `last-child` = 1, `visible` = 1 - WHERE `id` = %d", - intval($parent), - dbesc(($parent == $post_id) ? $uri : $parent_item['uri']), - dbesc($a->get_baseurl().'/display/'.urlencode($datarray['guid'])), - dbesc(datetime_convert()), - intval($post_id) - ); - - // photo comments turn the corresponding item visible to the profile wall - // This way we don't see every picture in your new photo album posted to your wall at once. - // They will show up as people comment on them. - - if(! $parent_item['visible']) { - $r = q("UPDATE `item` SET `visible` = 1 WHERE `id` = %d", - intval($parent_item['id']) - ); - update_thread($parent_item['id']); - } - - // update the commented timestamp on the parent - - q("UPDATE `item` set `commented` = '%s', `changed` = '%s' WHERE `id` = %d", - dbesc(datetime_convert()), - dbesc(datetime_convert()), - intval($parent) - ); - if ($post_id != $parent) - update_thread($parent); - call_hooks('post_local_end', $datarray); if(strlen($emailcc) && $profile_uid == local_user()) { @@ -996,22 +1012,25 @@ function item_post(&$a) { } } + if ($post_id == $parent) { + add_thread($post_id); + } else { + update_thread($parent, true); + } + + q("COMMIT"); + create_tags_from_item($post_id); create_files_from_item($post_id); - if ($post_id == $parent) - add_thread($post_id); - - // This is a real juggling act on shared hosting services which kill your processes - // e.g. dreamhost. We used to start delivery to our native delivery agents in the background - // and then run our plugin delivery from the foreground. We're now doing plugin delivery first, - // because as soon as you start loading up a bunch of remote delivey processes, *this* page is - // likely to get killed off. If you end up looking at an /item URL and a blank page, - // it's very likely the delivery got killed before all your friends could be notified. - // Currently the only realistic fixes are to use a reliable server - which precludes shared hosting, - // or cut back on plugins which do remote deliveries. + // Insert an item entry for UID=0 for global entries. + // We now do it in the background to save some time. + // This is important in interactive environments like the frontend or the API. + // We don't fork a new process since this is done anyway with the following command + proc_run(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "include/create_shadowentry.php", $post_id); - proc_run('php', "include/notifier.php", $notify_type, "$post_id"); + // Call the background process that is delivering the item to the receivers + proc_run(PRIORITY_HIGH, "include/notifier.php", $notify_type, $post_id); logger('post_complete'); @@ -1026,7 +1045,7 @@ function item_post_return($baseurl, $api_source, $return_path) { return; if($return_path) { - goaway($baseurl . "/" . $return_path); + goaway($return_path); } $json = array('success' => 1); @@ -1094,7 +1113,17 @@ function handle_tag($a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $netwo // Checking for the alias that is used for OStatus $pattern = "/@\[url\=(.*?)\](.*?)\[\/url\]/ism"; if (preg_match($pattern, $tag, $matches)) { - $data = probe_url($matches[1]); + + $r = q("SELECT `alias`, `name` FROM `contact` WHERE `nurl` = '%s' AND `alias` != '' AND `uid` = 0", + normalise_link($matches[1])); + if (!$r) + $r = q("SELECT `alias`, `name` FROM `gcontact` WHERE `nurl` = '%s' AND `alias` != ''", + normalise_link($matches[1])); + if ($r) + $data = $r[0]; + else + $data = probe_url($matches[1]); + if ($data["alias"] != "") { $newtag = '@[url='.$data["alias"].']'.$data["name"].'[/url]'; if(!stristr($str_tags,$newtag)) { @@ -1121,29 +1150,43 @@ function handle_tag($a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $netwo // Is it in format @user@domain.tld or @http://domain.tld/...? // First check the contact table for the address - $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network`, `notify` FROM `contact` WHERE `addr` = '%s' AND `uid` = %d LIMIT 1", + $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network`, `notify` FROM `contact` + WHERE `addr` = '%s' AND `uid` = %d AND + (`network` != '%s' OR (`notify` != '' AND `alias` != '')) + LIMIT 1", dbesc($name), - intval($profile_uid) + intval($profile_uid), + dbesc(NETWORK_OSTATUS) ); // Then check in the contact table for the url if (!$r) - $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `notify`, `network` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1", + $r = q("SELECT `id`, `url`, `nick`, `name`, `alias`, `network`, `notify` FROM `contact` + WHERE `nurl` = '%s' AND `uid` = %d AND + (`network` != '%s' OR (`notify` != '' AND `alias` != '')) + LIMIT 1", dbesc(normalise_link($name)), - intval($profile_uid) + intval($profile_uid), + dbesc(NETWORK_OSTATUS) ); // Then check in the global contacts for the address if (!$r) - $r = q("SELECT `url`, `name`, `nick`, `network`, `alias`, `notify` FROM `gcontact` WHERE `addr` = '%s' LIMIT 1", dbesc($name)); + $r = q("SELECT `url`, `nick`, `name`, `alias`, `network`, `notify` FROM `gcontact` + WHERE `addr` = '%s' AND (`network` != '%s' OR (`notify` != '' AND `alias` != '')) + LIMIT 1", + dbesc($name), + dbesc(NETWORK_OSTATUS) + ); // Then check in the global contacts for the url if (!$r) - $r = q("SELECT `url`, `name`, `nick`, `network`, `alias`, `notify` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1", dbesc(normalise_link($name))); - - // If the data isn't complete then refetch the data - if ($r AND ($r[0]["network"] == NETWORK_OSTATUS) AND (($r[0]["notify"] == "") OR ($r[0]["alias"] == ""))) - $r = false; + $r = q("SELECT `url`, `nick`, `name`, `alias`, `network`, `notify` FROM `gcontact` + WHERE `nurl` = '%s' AND (`network` != '%s' OR (`notify` != '' AND `alias` != '')) + LIMIT 1", + dbesc(normalise_link($name)), + dbesc(NETWORK_OSTATUS) + ); if (!$r) { $probed = probe_url($name);