<commit_before><?php
/**
 * @copyright Copyright (C) 2010-2022, 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/>.
 *
 */

namespace Friendica\Module\Post;

use Friendica\App;
use Friendica\BaseModule;
use Friendica\Content\Feature;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Model\Contact;
use Friendica\Model\Post;
use Friendica\Model\User;
use Friendica\Module\Response;
use Friendica\Navigation\SystemMessages;
use Friendica\Network\HTTPException;
use Friendica\Util\Crypto;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;

/**
 * Controller to edit a post
 */
class Edit extends BaseModule
{
	/** @var IHandleUserSessions */
	protected $session;
	/** @var SystemMessages */
	protected $sysMessages;
	/** @var App\Page */
	protected $page;
	/** @var App\Mode */
	protected $mode;
	/** @var App */
	protected $app;
	/** @var bool */
	protected $isModal = false;

	public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, SystemMessages $sysMessages, App\Page $page, App\Mode $mode, App $app, array $server, array $parameters = [])
	{
		parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);

		$this->session     = $session;
		$this->sysMessages = $sysMessages;
		$this->page        = $page;
		$this->mode        = $mode;
		$this->app         = $app;
	}


	protected function content(array $request = []): string
	{
		$this->isModal = $request['mode'] ?? '' === 'none';

		if (!$this->session->getLocalUserId()) {
			$this->errorExit($this->t('Permission denied.'), HTTPException\UnauthorizedException::class);
		}

		$postId = $this->parameters['post_id'] ?? 0;

		if (empty($postId)) {
			$this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
		}

		$fields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
				   'body', 'title', 'uri-id', 'wall', 'post-type', 'guid'];

		$item = Post::selectFirstForUser($this->session->getLocalUserId(), $fields, [
			'id'  => $postId,
			'uid' => $this->session->getLocalUserId(),
		]);

		if (empty($item)) {
			$this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
		}

		$user   = User::getById($this->session->getLocalUserId());

		$output = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
			'$title' => $this->t('Edit post'),
		]);

		$this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot-header.tpl'), [
			'$ispublic'  => '&nbsp;', // $this->t('Visible to <strong>everybody</strong>'),
			'$nickname'  => $this->app->getLoggedInUserNickname(),
			'$is_mobile' => $this->mode->isMobile(),
		]);

		if (strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
			$lockstate = 'lock';
		} else {
			$lockstate = 'unlock';
		}

		$jotplugins = '';

		Hook::callAll('jot_tool', $jotplugins);

		$output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot.tpl'), [
			'$is_edit'             => true,
			'$return_path'         => '/display/' . $item['guid'],
			'$action'              => 'item',
			'$share'               => $this->t('Save'),
			'$loading'             => $this->t('Loading...'),
			'$upload'              => $this->t('Upload photo'),
			'$shortupload'         => $this->t('upload photo'),
			'$attach'              => $this->t('Attach file'),
			'$shortattach'         => $this->t('attach file'),
			'$weblink'             => $this->t('Insert web link'),
			'$shortweblink'        => $this->t('web link'),
			'$video'               => $this->t('Insert video link'),
			'$shortvideo'          => $this->t('video link'),
			'$audio'               => $this->t('Insert audio link'),
			'$shortaudio'          => $this->t('audio link'),
			'$setloc'              => $this->t('Set your location'),
			'$shortsetloc'         => $this->t('set location'),
			'$noloc'               => $this->t('Clear browser location'),
			'$shortnoloc'          => $this->t('clear location'),
			'$wait'                => $this->t('Please wait'),
			'$permset'             => $this->t('Permission settings'),
			'$wall'                => $item['wall'],
			'$posttype'            => $item['post-type'],
			'$content'             => $this->undoPostTagging($item['body']),
			'$post_id'             => $postId,
			'$defloc'              => $user['default-location'],
			'$visitor'             => 'none',
			'$pvisit'              => 'none',
			'$emailcc'             => $this->t('CC: email addresses'),
			'$public'              => $this->t('Public post'),
			'$title'               => $item['title'],
			'$placeholdertitle'    => $this->t('Set title'),
			'$category'            => Post\Category::getCSVByURIId($item['uri-id'], $this->session->getLocalUserId(), Post\Category::CATEGORY),
			'$placeholdercategory' => (Feature::isEnabled($this->session->getLocalUserId(), 'categories') ? $this->t("Categories \x28comma-separated list\x29") : ''),
			'$emtitle'             => $this->t('Example: <EMAIL>, <EMAIL>'),
			'$lockstate'           => $lockstate,
			'$acl'                 => '', // populate_acl((($group) ? $group_acl : $a->user)),
			'$bang'                => ($lockstate === 'lock' ? '!' : ''),
			'$profile_uid'         => $this->session->getLocalUserId(),
			'$preview'             => $this->t('Preview'),
			'$jotplugins'          => $jotplugins,<commit_msg>Remove legacy comments<commit_after><?php
/**
 * @copyright Copyright (C) 2010-2022, 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/>.
 *
 */

namespace Friendica\Module\Post;

use Friendica\App;
use Friendica\BaseModule;
use Friendica\Content\Feature;
use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Model\Contact;
use Friendica\Model\Post;
use Friendica\Model\User;
use Friendica\Module\Response;
use Friendica\Navigation\SystemMessages;
use Friendica\Network\HTTPException;
use Friendica\Util\Crypto;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;

/**
 * Controller to edit a post
 */
class Edit extends BaseModule
{
	/** @var IHandleUserSessions */
	protected $session;
	/** @var SystemMessages */
	protected $sysMessages;
	/** @var App\Page */
	protected $page;
	/** @var App\Mode */
	protected $mode;
	/** @var App */
	protected $app;
	/** @var bool */
	protected $isModal = false;

	public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, SystemMessages $sysMessages, App\Page $page, App\Mode $mode, App $app, array $server, array $parameters = [])
	{
		parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);

		$this->session     = $session;
		$this->sysMessages = $sysMessages;
		$this->page        = $page;
		$this->mode        = $mode;
		$this->app         = $app;
	}


	protected function content(array $request = []): string
	{
		$this->isModal = $request['mode'] ?? '' === 'none';

		if (!$this->session->getLocalUserId()) {
			$this->errorExit($this->t('Permission denied.'), HTTPException\UnauthorizedException::class);
		}

		$postId = $this->parameters['post_id'] ?? 0;

		if (empty($postId)) {
			$this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
		}

		$fields = ['allow_cid', 'allow_gid', 'deny_cid', 'deny_gid',
				   'body', 'title', 'uri-id', 'wall', 'post-type', 'guid'];

		$item = Post::selectFirstForUser($this->session->getLocalUserId(), $fields, [
			'id'  => $postId,
			'uid' => $this->session->getLocalUserId(),
		]);

		if (empty($item)) {
			$this->errorExit($this->t('Post not found.'), HTTPException\BadRequestException::class);
		}

		$user = User::getById($this->session->getLocalUserId());

		$output = Renderer::replaceMacros(Renderer::getMarkupTemplate('section_title.tpl'), [
			'$title' => $this->t('Edit post'),
		]);

		$this->page['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot-header.tpl'), [
			'$ispublic'  => '&nbsp;',
			'$nickname'  => $this->app->getLoggedInUserNickname(),
			'$is_mobile' => $this->mode->isMobile(),
		]);

		if (strlen($item['allow_cid']) || strlen($item['allow_gid']) || strlen($item['deny_cid']) || strlen($item['deny_gid'])) {
			$lockstate = 'lock';
		} else {
			$lockstate = 'unlock';
		}

		$jotplugins = '';

		Hook::callAll('jot_tool', $jotplugins);

		$output .= Renderer::replaceMacros(Renderer::getMarkupTemplate('jot.tpl'), [
			'$is_edit'             => true,
			'$return_path'         => '/display/' . $item['guid'],
			'$action'              => 'item',
			'$share'               => $this->t('Save'),
			'$loading'             => $this->t('Loading...'),
			'$upload'              => $this->t('Upload photo'),
			'$shortupload'         => $this->t('upload photo'),
			'$attach'              => $this->t('Attach file'),
			'$shortattach'         => $this->t('attach file'),
			'$weblink'             => $this->t('Insert web link'),
			'$shortweblink'        => $this->t('web link'),
			'$video'               => $this->t('Insert video link'),
			'$shortvideo'          => $this->t('video link'),
			'$audio'               => $this->t('Insert audio link'),
			'$shortaudio'          => $this->t('audio link'),
			'$setloc'              => $this->t('Set your location'),
			'$shortsetloc'         => $this->t('set location'),
			'$noloc'               => $this->t('Clear browser location'),
			'$shortnoloc'          => $this->t('clear location'),
			'$wait'                => $this->t('Please wait'),
			'$permset'             => $this->t('Permission settings'),
			'$wall'                => $item['wall'],
			'$posttype'            => $item['post-type'],
			'$content'             => $this->undoPostTagging($item['body']),
			'$post_id'             => $postId,
			'$defloc'              => $user['default-location'],
			'$visitor'             => 'none',
			'$pvisit'              => 'none',
			'$emailcc'             => $this->t('CC: email addresses'),
			'$public'              => $this->t('Public post'),
			'$title'               => $item['title'],
			'$placeholdertitle'    => $this->t('Set title'),
			'$category'            => Post\Category::getCSVByURIId($item['uri-id'], $this->session->getLocalUserId(), Post\Category::CATEGORY),
			'$placeholdercategory' => (Feature::isEnabled($this->session->getLocalUserId(), 'categories') ? $this->t("Categories \x28comma-separated list\x29") : ''),
			'$emtitle'             => $this->t('Example: <EMAIL>, <EMAIL>'),
			'$lockstate'           => $lockstate,
			'$acl'                 => '',
			'$bang'                => ($lockstate === 'lock' ? '!' : ''),
			'$profile_uid'         => $this->session->getLocalUserId(),
			'$preview'             => $this->t('Preview'),
			'$jotplugins'          => $jotplugins,
{'fraction_non_alphanumeric': 0.1903655017633857, 'fraction_numerical': 0.0036870791920487335, 'mean_word_length': 3.1645527369826434, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 223, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7625976', 'n_tokens_mistral': 4189, 'n_tokens_neox': 3867, 'n_words': 857}