friendica/mod/item.php

454 lines
14 KiB
PHP
Raw Normal View History

2010-07-02 01:48:07 +02:00
<?php
2017-12-02 00:13:39 +01:00
/**
2023-01-01 15:36:24 +01:00
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
2011-02-04 22:37:04 +01:00
* This is the POST destination for most all locally posted
* text stuff. This function handles status, wall-to-wall status,
* local comments, and remote coments that are posted on this site
2011-02-04 22:37:04 +01:00
* (as opposed to being delivered in a feed).
* Also processed here are posts and comments coming through the
* statusnet/twitter API.
*
* All of these become an "item" which is our basic unit of
2011-02-04 22:37:04 +01:00
* information.
*/
use Friendica\App;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Hook;
2018-10-29 22:20:46 +01:00
use Friendica\Core\Logger;
use Friendica\Core\Protocol;
use Friendica\Core\System;
2022-10-17 07:49:55 +02:00
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
2017-12-07 15:04:24 +01:00
use Friendica\Model\Contact;
use Friendica\Model\Item;
use Friendica\Model\ItemURI;
use Friendica\Model\Photo;
use Friendica\Model\Post;
use Friendica\Network\HTTPException;
use Friendica\Protocol\Activity;
use Friendica\Util\DateTimeFormat;
function item_post(App $a) {
2023-01-04 06:44:52 +01:00
$uid = DI::userSession()->getLocalUserId();
if (!DI::userSession()->isAuthenticated() || !$uid) {
throw new HTTPException\ForbiddenException();
}
2010-07-02 01:48:07 +02:00
if (!empty($_REQUEST['dropitems'])) {
$arr_drop = explode(',', $_REQUEST['dropitems']);
foreach ($arr_drop as $item) {
Item::deleteForUser(['id' => $item], $uid);
}
$json = ['success' => 1];
System::jsonExit($json);
2011-06-16 05:43:39 +02:00
}
Hook::callAll('post_local_start', $_REQUEST);
$return_path = $_REQUEST['return'] ?? '';
2023-01-04 06:44:52 +01:00
$preview = intval($_REQUEST['preview'] ?? 0);
2011-08-01 05:01:00 +02:00
/*
* Check for doubly-submitted posts, and reject duplicates
* Note that we have to ignore previews, otherwise nothing will post
* after it's been previewed
*/
if (!$preview && !empty($_REQUEST['post_id_random'])) {
if (!empty($_SESSION['post-random']) && $_SESSION['post-random'] == $_REQUEST['post_id_random']) {
2022-08-30 21:45:30 +02:00
Logger::warning('duplicate post');
item_post_return(DI::baseUrl(), $return_path);
} else {
$_SESSION['post-random'] = $_REQUEST['post_id_random'];
}
}
2023-01-04 06:44:52 +01:00
$post_id = intval($_REQUEST['post_id'] ?? 0);
2023-01-04 06:44:52 +01:00
// is this an edited post?
if ($post_id > 0) {
$orig_post = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $post_id]);
} else {
$orig_post = null;
}
$emailcc = trim($_REQUEST['emailcc'] ?? '');
$post = ['uid' => $uid];
$post = DI::contentItem()->initializePost($post);
2023-01-04 07:52:40 +01:00
$post['edit'] = $orig_post;
$post['self'] = true;
$post['api_source'] = false;
$post['file'] = '';
$post['attach'] = '';
$post['inform'] = '';
$post['postopts'] = '';
$post['wall'] = $_REQUEST['wall'] ?? true;
$post['post-type'] = $_REQUEST['post_type'] ?? '';
$post['title'] = trim($_REQUEST['title'] ?? '');
$post['body'] = $_REQUEST['body'] ?? '';
$post['location'] = trim($_REQUEST['location'] ?? '');
$post['coord'] = trim($_REQUEST['coord'] ?? '');
$post['parent'] = intval($_REQUEST['parent'] ?? 0);
$post['pubmail'] = $_REQUEST['pubmail_enable'] ?? false;
$post['created'] = $_REQUEST['created_at'] ?? DateTimeFormat::utcNow();
$post['edited'] = $post['changed'] = $post['commented'] = $post['created'];
$post['app'] = '';
2023-01-04 06:44:52 +01:00
if ($post['parent']) {
if ($post['parent']) {
$parent_item = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $post['parent']]);
}
// if this isn't the top-level parent of the conversation, find it
if (DBA::isResult($parent_item)) {
// The URI and the contact is taken from the direct parent which needn't to be the top parent
2023-01-04 06:44:52 +01:00
$post['thr-parent'] = $parent_item['uri'];
$toplevel_item = $parent_item;
if ($parent_item['gravity'] != Item::GRAVITY_PARENT) {
$toplevel_item = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $toplevel_item['parent']]);
2011-08-01 05:01:00 +02:00
}
}
if (!DBA::isResult($toplevel_item)) {
2022-10-17 13:27:32 +02:00
DI::sysmsg()->addNotice(DI::l10n()->t('Unable to locate original post.'));
2020-01-30 04:34:32 +01:00
if ($return_path) {
DI::baseUrl()->redirect($return_path);
}
throw new HTTPException\NotFoundException(DI::l10n()->t('Unable to locate original post.'));
}
// When commenting on a public post then store the post for the current user
// This enables interaction like starring and saving into folders
if ($toplevel_item['uid'] == 0) {
2023-01-04 06:44:52 +01:00
$stored = Item::storeForUserByUriId($toplevel_item['uri-id'], $post['uid'], ['post-reason' => Item::PR_ACTIVITY]);
Logger::info('Public item stored for user', ['uri-id' => $toplevel_item['uri-id'], 'uid' => $post['uid'], 'stored' => $stored]);
if ($stored) {
$toplevel_item = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $stored]);
}
}
2023-01-04 06:44:52 +01:00
$post['parent'] = $toplevel_item['id'];
$post['gravity'] = Item::GRAVITY_COMMENT;
$post['wall'] = $toplevel_item['wall'];
} else {
2023-01-04 06:44:52 +01:00
$parent_item = [];
$post['parent'] = 0;
$post['gravity'] = Item::GRAVITY_PARENT;
$post['thr-parent'] = $post['uri'];
2011-03-18 13:06:16 +01:00
}
2023-01-04 06:44:52 +01:00
$post = DI::contentItem()->getACL($post, $parent_item, $_REQUEST);
2023-01-04 06:44:52 +01:00
$post['pubmail'] = $post['pubmail'] && !$post['private'];
2019-10-13 17:52:33 +02:00
if (!empty($orig_post)) {
2023-01-04 06:44:52 +01:00
$post['file'] = Post\Category::getTextByURIId($orig_post['uri-id'], $orig_post['uid']);
}
2023-01-04 06:44:52 +01:00
$post = DI::contentItem()->addCategories($post, $_REQUEST['category'] ?? '');
2010-07-18 04:26:00 +02:00
2023-01-04 06:44:52 +01:00
if (!$preview) {
if (Photo::setPermissionFromBody($post['body'], $post['uid'], $post['contact-id'], $post['allow_cid'], $post['allow_gid'], $post['deny_cid'], $post['deny_gid'])) {
$post['object-type'] = Activity\ObjectType::IMAGE;
2011-05-25 11:08:15 +02:00
}
2011-09-05 04:58:03 +02:00
2023-01-04 06:44:52 +01:00
$post = DI::contentItem()->moveAttachmentsFromBodyToAttach($post);
2011-09-05 04:58:03 +02:00
}
2023-01-04 06:44:52 +01:00
// Add the attachment to the body.
if (!empty($_REQUEST['has_attachment'])) {
$post['body'] .= DI::contentItem()->storeAttachmentFromRequest($_REQUEST);
}
2012-02-25 23:22:51 +01:00
2023-01-04 06:44:52 +01:00
$post = DI::contentItem()->finalizePost($post);
2023-01-04 06:44:52 +01:00
if (!strlen($post['body'])) {
if ($preview) {
System::jsonExit(['preview' => '']);
}
2023-01-04 06:44:52 +01:00
DI::sysmsg()->addNotice(DI::l10n()->t('Empty post discarded.'));
if ($return_path) {
DI::baseUrl()->redirect($return_path);
2011-05-25 11:08:15 +02:00
}
2023-01-04 06:44:52 +01:00
throw new HTTPException\BadRequestException(DI::l10n()->t('Empty post discarded.'));
}
2012-01-06 00:02:44 +01:00
// preview mode - prepare the body for display and send it via json
if ($preview) {
2023-01-04 07:52:40 +01:00
// We have to preset some fields, so that the conversation can be displayed
$post['id'] = -1;
$post['uri-id'] = -1;
2023-01-04 06:44:52 +01:00
$post['author-network'] = Protocol::DFRN;
$post['author-updated'] = '';
2023-01-04 07:52:40 +01:00
$post['author-gsid'] = 0;
$post['author-uri-id'] = ItemURI::getIdByURI($post['author-link']);
$post['owner-updated'] = '';
$post['has-media'] = false;
$post['quote-uri-id'] = Item::getQuoteUriId($post['body'], $post['uid']);
$post['body'] = BBCode::removeSharedData(Item::setHashtags($post['body']));
$post['writable'] = true;
2023-01-04 06:44:52 +01:00
$o = DI::conversation()->create([$post], 'search', false, true);
System::jsonExit(['preview' => $o]);
}
2023-01-04 06:44:52 +01:00
Hook::callAll('post_local',$post);
2023-01-04 07:52:40 +01:00
unset($post['edit']);
unset($post['self']);
unset($post['api_source']);
if (!empty($_REQUEST['scheduled_at'])) {
$scheduled_at = DateTimeFormat::convert($_REQUEST['scheduled_at'], 'UTC', $a->getTimeZone());
if ($scheduled_at > DateTimeFormat::utcNow()) {
2023-01-04 06:44:52 +01:00
unset($post['created']);
unset($post['edited']);
unset($post['commented']);
unset($post['received']);
unset($post['changed']);
Post\Delayed::add($post['uri'], $post, Worker::PRIORITY_HIGH, Post\Delayed::PREPARED_NO_HOOK, $scheduled_at);
item_post_return(DI::baseUrl(), $return_path);
}
2012-01-06 00:02:44 +01:00
}
2023-01-04 06:44:52 +01:00
if (!empty($post['cancel'])) {
Logger::info('mod_item: post cancelled by addon.');
if ($return_path) {
DI::baseUrl()->redirect($return_path);
}
$json = ['cancel' => 1];
if (!empty($_REQUEST['jsreload'])) {
$json['reload'] = DI::baseUrl() . '/' . $_REQUEST['jsreload'];
}
System::jsonExit($json);
}
if ($orig_post) {
$fields = [
2023-01-04 07:52:40 +01:00
'title' => $post['title'],
'body' => $post['body'],
'attach' => $post['attach'],
'file' => $post['file'],
'edited' => DateTimeFormat::utcNow(),
'changed' => DateTimeFormat::utcNow()
2022-05-05 14:23:44 +02:00
];
2023-01-04 07:52:40 +01:00
$fields['body'] = Item::setHashtags($fields['body']);
$quote_uri_id = Item::getQuoteUriId($fields['body'], $post['uid']);
if (!empty($quote_uri_id)) {
$fields['quote-uri-id'] = $quote_uri_id;
$fields['body'] = BBCode::removeSharedData($post['body']);
}
Item::update($fields, ['id' => $post_id]);
2023-01-04 07:52:40 +01:00
Item::updateDisplayCache($orig_post['uri-id']);
2020-01-30 04:34:32 +01:00
if ($return_path) {
DI::baseUrl()->redirect($return_path);
2011-03-18 13:06:16 +01:00
}
throw new HTTPException\OKException(DI::l10n()->t('Post updated.'));
}
2011-03-18 13:06:16 +01:00
2023-01-04 06:44:52 +01:00
$post_id = Item::insert($post);
if (!$post_id) {
2022-10-17 13:27:32 +02:00
DI::sysmsg()->addNotice(DI::l10n()->t('Item wasn\'t stored.'));
2020-01-30 04:34:32 +01:00
if ($return_path) {
DI::baseUrl()->redirect($return_path);
}
throw new HTTPException\InternalServerErrorException(DI::l10n()->t('Item wasn\'t stored.'));
}
2023-01-04 06:44:52 +01:00
$post = Post::selectFirst(Item::ITEM_FIELDLIST, ['id' => $post_id]);
if (!DBA::isResult($post)) {
Logger::error('Item couldn\'t be fetched.', ['post_id' => $post_id]);
2020-01-30 04:34:32 +01:00
if ($return_path) {
DI::baseUrl()->redirect($return_path);
}
throw new HTTPException\InternalServerErrorException(DI::l10n()->t('Item couldn\'t be fetched.'));
}
2023-01-04 06:44:52 +01:00
$recipients = explode(',', $emailcc);
2023-01-04 07:52:40 +01:00
DI::contentItem()->postProcessPost($post, $recipients);
2011-02-01 03:18:28 +01:00
2022-08-30 21:45:30 +02:00
Logger::debug('post_complete');
item_post_return(DI::baseUrl(), $return_path);
// NOTREACHED
}
2018-01-20 18:34:53 +01:00
function item_post_return($baseurl, $return_path)
{
if ($return_path) {
DI::baseUrl()->redirect($return_path);
2011-02-17 06:17:49 +01:00
}
2011-09-12 06:52:50 +02:00
$json = ['success' => 1];
if (!empty($_REQUEST['jsreload'])) {
$json['reload'] = $baseurl . '/' . $_REQUEST['jsreload'];
}
2011-02-17 06:17:49 +01:00
2022-08-30 21:45:30 +02:00
Logger::debug('post_json', ['json' => $json]);
2011-02-17 06:17:49 +01:00
System::jsonExit($json);
2010-07-27 02:01:37 +02:00
}
function item_content(App $a)
{
if (!DI::userSession()->isAuthenticated()) {
throw new HTTPException\UnauthorizedException();
}
$args = DI::args();
if (!$args->has(3)) {
throw new HTTPException\BadRequestException();
}
2010-07-27 02:01:37 +02:00
$o = '';
switch ($args->get(1)) {
case 'drop':
if (DI::mode()->isAjax()) {
Item::deleteForUser(['id' => $args->get(2)], DI::userSession()->getLocalUserId());
// ajax return: [<item id>, 0 (no perm) | <owner id>]
System::jsonExit([intval($args->get(2)), DI::userSession()->getLocalUserId()]);
} else {
if (!empty($args->get(3))) {
$o = drop_item($args->get(2), $args->get(3));
} else {
$o = drop_item($args->get(2));
}
}
break;
case 'block':
$item = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), ['guid', 'author-id', 'parent', 'gravity'], ['id' => $args->get(2)]);
if (empty($item['author-id'])) {
throw new HTTPException\NotFoundException('Item not found');
}
Contact\User::setBlocked($item['author-id'], DI::userSession()->getLocalUserId(), true);
if (DI::mode()->isAjax()) {
// ajax return: [<item id>, 0 (no perm) | <owner id>]
System::jsonExit([intval($args->get(2)), DI::userSession()->getLocalUserId()]);
} else {
item_redirect_after_action($item, $args->get(3));
2018-10-26 14:05:59 +02:00
}
break;
}
return $o;
}
/**
* @param int $id
* @param string $return
* @return string
* @throws HTTPException\InternalServerErrorException
*/
function drop_item(int $id, string $return = ''): string
{
// Locate item to be deleted
$item = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), ['id', 'uid', 'guid', 'contact-id', 'deleted', 'gravity', 'parent'], ['id' => $id]);
if (!DBA::isResult($item)) {
2022-10-17 13:27:32 +02:00
DI::sysmsg()->addNotice(DI::l10n()->t('Item not found.'));
DI::baseUrl()->redirect('network');
//NOTREACHED
}
if ($item['deleted']) {
return '';
}
$contact_id = 0;
// check if logged in user is either the author or owner of this item
if (DI::userSession()->getRemoteContactID($item['uid']) == $item['contact-id']) {
$contact_id = $item['contact-id'];
}
if ((DI::userSession()->getLocalUserId() == $item['uid']) || $contact_id) {
// delete the item
Item::deleteForUser(['id' => $item['id']], DI::userSession()->getLocalUserId());
item_redirect_after_action($item, $return);
//NOTREACHED
} else {
Logger::warning('Permission denied.', ['local' => DI::userSession()->getLocalUserId(), 'uid' => $item['uid'], 'cid' => $contact_id]);
2022-10-17 13:27:32 +02:00
DI::sysmsg()->addNotice(DI::l10n()->t('Permission denied.'));
DI::baseUrl()->redirect('display/' . $item['guid']);
//NOTREACHED
2010-07-27 02:01:37 +02:00
}
return '';
2011-05-22 06:40:16 +02:00
}
function item_redirect_after_action(array $item, string $returnUrlHex)
{
$return_url = hex2bin($returnUrlHex);
// removes update_* from return_url to ignore Ajax refresh
$return_url = str_replace('update_', '', $return_url);
// Check if delete a comment
if ($item['gravity'] == Item::GRAVITY_COMMENT) {
if (!empty($item['parent'])) {
$parentitem = Post::selectFirstForUser(DI::userSession()->getLocalUserId(), ['guid'], ['id' => $item['parent']]);
}
// Return to parent guid
if (!empty($parentitem)) {
DI::baseUrl()->redirect('display/' . $parentitem['guid']);
//NOTREACHED
} // In case something goes wrong
else {
DI::baseUrl()->redirect('network');
//NOTREACHED
}
} else {
// if unknown location or deleting top level post called from display
if (empty($return_url) || strpos($return_url, 'display') !== false) {
DI::baseUrl()->redirect('network');
//NOTREACHED
} else {
DI::baseUrl()->redirect($return_url);
//NOTREACHED
}
}
}