HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/proundcontra.mosmuc.de/pages/editQuestion.php
<?php
class PageEditQuestion extends Page
{
    public function __construct($row)
    {
        global $sDB, $sRequest, $sQuery, $sUser, $sTemplate, $sSession;
        parent::__construct($row);

        $this->view     = VIEW_NEW_QUESTION;
        $questionTitle  = $sRequest->getString("title");
        $this->question = false;

        $res = $sDB->exec("
            SELECT *
            FROM `questions`
            WHERE `url` = '".mysqli_real_escape_string($sDB->getLink(), $questionTitle)."'
            LIMIT 1;
        ");
        while($rowQ = mysqli_fetch_object($res))
        {
            $this->question = new Question($rowQ->questionId, $rowQ);
        }

        if(!$this->question || $this->question->authorId() != $sUser->getUserId())
        {
            $sTemplate->error($sTemplate->getString("ERROR_INVALID_QUESTION"));
        }

        if(!$this->question->canEdit($sUser))
        {
            $sSession->setVal('notification', $sTemplate->getString("QUESTION_EDIT_EXCEEDED"));
            $sSession->serialize();
            header("Location: ".$this->question->url());
            exit;
        }

        // Handle form submission
        if($sRequest->getInt("edit_question"))
        {
            if($this->handleEditQuestion())
            {
                header("Location: ".$this->redirectUrl);
                exit;
            }
        }
    }

    public function getView()
    {
        return $this->view;
    }

    public function canView()
    {
        global $sUser, $sTemplate, $sPermissions;

        if(!$sUser->isLoggedIn())
        {
            $this->setError($sTemplate->getString("ERROR_NOT_LOGGED_IN"));
            return false;
        }

        if($sPermissions->getPermission($sUser, ACTION_NEW_QUESTION) == PERMISSION_DISALLOWED)
        {
            $this->setError($sTemplate->getString("ERROR_NO_PERMISSION"));
            return false;
        }

        return true;
    }

    public function handleEditQuestion()
    {
        global $sRequest, $sTemplate, $sUser, $sPermissions;

        if(!$sUser->isLoggedIn() ||
           $sPermissions->getPermission($sUser, ACTION_NEW_QUESTION) == PERMISSION_DISALLOWED)
        {
            return false;
        }

        $question       = substr($sRequest->getStringPlain("new_question_title"), 0, MAX_QUESTION_CHR_LENGTH);
        $tagsRaw        = substr($sRequest->getStringPlain("new_question_tags"), 0, MAX_TAGS_CHR_LENGTH);
        $details        = $sRequest->getStringPlain("new_question_details");
        $type           = $sRequest->getInt("new_question_type");
        $flags          = $sRequest->getInt("new_question_flags");

        validateQuestionType($type);
        validateQuestionFlags($flags);

        if($type == QUESTION_TYPE_LISTED)
        {
            $flags = 0;
        }

        $questionParsed = preg_replace("/[^0-9a-zÄÖÜäöüáàâéèêíìîóòôúùû\[\]\{\} -]/i", "", $question);

        if($question == "" || $questionParsed == "")
        {
            $this->setError($sTemplate->getString("ERROR_NEW_QUESTION_INVALID_QUESTION"));
            return false;
        }

        $tags           = [];
        $tagsNoQuestion = $this->tagsByString($tagsRaw);

        // Combine tags from 'new_question_tags' and from the question text
        $tags = array_merge($tags, $tagsNoQuestion);
        $tags = array_merge($tags, $this->tagsByString(str_replace(" ", ",", $question)));
        $tags = $this->filterTags($tags);

        return $this->store($question, $questionParsed, $tags, $details, $tagsNoQuestion, $type, $flags);
    }

    private function store($question, $questionParsed, $tags, $details, $tagsNoQuestion, $type, $flags)
    {
        global $sDB, $sUser, $sTemplate, $sStatistics;

        $url = url_sanitize($questionParsed);

        // Only update URL if the title has changed
        if($question != $this->question()->titlePlain())
        {
            $i = 0;
            while(true)
            {
                $cur = $url.($i > 0 ? '-'.$i : '');
                $res = $sDB->exec("
                    SELECT `url`
                    FROM `questions`
                    WHERE `url` = '".mysqli_real_escape_string($sDB->getLink(), $cur)."'
                    LIMIT 1;
                ");

                if(mysqli_num_rows($res))
                {
                    $i++;
                    continue;
                }
                break;
            }

            if($i > 0)
            {
                $url .= '-'.$i;
            }
        }
        else
        {
            $url = $this->question()->urlPart();
        }

        $additionalData = new stdClass();
        $additionalData->percPro     = 0;
        $additionalData->percCon     = 0;
        $additionalData->numCheckIns = 0;
        $additionalData->tags        = array_unique($tagsNoQuestion);

        $sDB->exec("
            UPDATE `questions`
               SET `title`         = '".mysqli_real_escape_string($sDB->getLink(), $question)."',
                   `url`           = '".mysqli_real_escape_string($sDB->getLink(), $url)."',
                   `details`       = '".mysqli_real_escape_string($sDB->getLink(), $details)."',
                   `additionalData`= '".serialize($additionalData)."',
                   `type`          = '".i($type)."',
                   `flags`         = '".i($flags)."',
                   `score`         = 0,
                   `scoreTop`      = 0
             WHERE `questionId`    = '".i($this->question()->questionId())."'
             LIMIT 1;
        ");

        // Remove existing tags for this question
        $sDB->exec("
            DELETE FROM `tags`
            WHERE `questionId` = '".i($this->question()->questionId())."';
        ");

        $sStatistics->resetQuestionVotes($this->question());

        // Insert updated tags
        foreach($tags as $k => $v)
        {
            $sDB->exec("
                INSERT INTO `tags` (`tagId`, `questionId`, `tag`)
                VALUES(NULL, '".i($this->question()->questionId())."', '".mysqli_real_escape_string($sDB->getLink(), $v)."');
            ");
        }

        // If question is unlisted, add "unlisted" to the URL path
        if($type == QUESTION_TYPE_UNLISTED)
        {
            $url = "unlisted/".$url;
        }
        $this->redirectUrl = $sTemplate->getRoot().$url."/";

        return true;
    }

    private function filterTags($tags)
    {
        return array_unique($tags);
    }

    private function tagsByString($string)
    {
        $tags = [];

        $tagsRaw = str_replace(" ", "-", $string);
        $tagsRaw = str_replace([",", "\n", "\r", "\t"], " ", $tagsRaw);
        $tagsRaw = explode(" ", $tagsRaw);

        foreach($tagsRaw as $k => $v)
        {
            $v = preg_replace('/[^a-z0-9ÄÖÜöäüáàâéèêíìîóòôúùû\[\]\{\}_-]/i', '', $v);
            $v = trim($v, "-");

            if($v != "")
            {
                $tags[] = $v;
            }
        }

        return $tags;
    }

    public function title()
    {
        global $sTemplate;
        return $sTemplate->getString("HTML_META_TITLE_EDIT_QUESTION");
    }

    public function getFormUrl()
    {
        return $this->question()->url()."edit/";
    }

    public function question()
    {
        return $this->question;
    }

    private $view;
    private $redirectUrl;
    private $question;
}
?>