File: //opt/proundcontra.mosmuc.de/pages/newQuestion.php
<?php
class PageNewQuestion extends Page
{
public function __construct($row)
{
global $sDB, $sRequest, $sQuery, $sUser;
parent::__construct($row);
$this->view = VIEW_NEW_QUESTION;
if ($sRequest->getInt("new_question")) {
if ($this->handleNewQuestion()) {
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 handleNewQuestion()
{
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 = nl2br(htmlspecialchars($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;
}
// Combine tags
$tags = [];
$tagsNoQuestion = $this->tagsByString($tagsRaw);
$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, $sNotify;
$url = url_sanitize($questionParsed);
// Ensure the URL is unique
$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;
}
// Additional data
$additionalData = new stdClass();
$additionalData->percPro = 0;
$additionalData->percCon = 0;
$additionalData->numCheckIns = 0;
$additionalData->tags = array_unique($tagsNoQuestion);
// Insert new question
$sDB->exec("
INSERT INTO `questions`
(`questionId`, `title`, `url`, `details`, `dateAdded`, `userId`,
`score`, `scoreTrending`, `scoreTop`, `additionalData`, `type`, `flags`)
VALUES
(NULL,
'".mysqli_real_escape_string($sDB->getLink(), $question)."',
'".mysqli_real_escape_string($sDB->getLink(), $url)."',
'".mysqli_real_escape_string($sDB->getLink(), $details)."',
'".time()."',
'".$sUser->getUserId()."',
'0',
'0',
'0',
'".serialize($additionalData)."',
'".i($type)."',
'".i($flags)."'
);
");
$questionId = mysqli_insert_id($sDB->getLink());
if (!$questionId) {
$this->setError($sTemplate->getString("ERROR_NEW_QUESTION_TRY_AGAIN"));
return false;
}
// Insert associated tags
foreach ($tags as $k => $v) {
$sDB->exec("
INSERT INTO `tags` (`tagId`, `questionId`, `tag`)
VALUES (NULL, '".i($questionId)."', '".mysqli_real_escape_string($sDB->getLink(), $v)."');
");
}
// For unlisted questions, prepend "unlisted/" to the URL path
if ($type == QUESTION_TYPE_UNLISTED) {
$url = "unlisted/".$url;
}
// Set redirect and follow
$this->redirectUrl = $sTemplate->getRoot().$url."/";
$sUser->follow($questionId);
return $questionId;
}
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_NEW_QUESTION");
}
public function getFormUrl()
{
global $sTemplate;
return $sTemplate->getRoot()."new-question/";
}
private $view;
private $redirectUrl;
}
?>