mirror of
https://github.com/Sarjuuk/aowow.git
synced 2025-11-29 15:58:16 +08:00
Guides: initial implementation
* a guide is a wrapper around an article providing management tools. * administration is limited to the review process. Needs to be expanded. * articles on DB pages are seperate. Editor will be added in the future.
This commit is contained in:
@@ -5,9 +5,9 @@ if (!defined('AOWOW_REVISION'))
|
||||
|
||||
class AjaxAdmin extends AjaxHandler
|
||||
{
|
||||
protected $validParams = ['screenshots', 'siteconfig', 'weight-presets', 'spawn-override'];
|
||||
protected $validParams = ['screenshots', 'siteconfig', 'weight-presets', 'spawn-override', 'guide'];
|
||||
protected $_get = array(
|
||||
'action' => ['filter' => FILTER_UNSAFE_RAW, 'flags' => FILTER_FLAG_STRIP_AOWOW ],
|
||||
'action' => ['filter' => FILTER_UNSAFE_RAW, 'flags' => FILTER_FLAG_STRIP_AOWOW ],
|
||||
'id' => ['filter' => FILTER_CALLBACK, 'options' => 'AjaxHandler::checkIdListUnsigned'],
|
||||
'key' => ['filter' => FILTER_CALLBACK, 'options' => 'AjaxAdmin::checkKey' ],
|
||||
'all' => ['filter' => FILTER_CALLBACK, 'options' => 'AjaxHandler::checkFulltext' ],
|
||||
@@ -20,21 +20,22 @@ class AjaxAdmin extends AjaxHandler
|
||||
'floor' => ['filter' => FILTER_CALLBACK, 'options' => 'AjaxHandler::checkInt' ]
|
||||
);
|
||||
protected $_post = array(
|
||||
'alt' => ['filter' => FILTER_UNSAFE_RAW, 'flags' => FILTER_FLAG_STRIP_AOWOW ],
|
||||
'alt' => ['filter' => FILTER_UNSAFE_RAW, 'flags' => FILTER_FLAG_STRIP_AOWOW],
|
||||
'id' => ['filter' => FILTER_CALLBACK, 'options' => 'AjaxHandler::checkInt'],
|
||||
'scale' => ['filter' => FILTER_CALLBACK, 'options' => 'AjaxAdmin::checkScale'],
|
||||
'__icon' => ['filter' => FILTER_CALLBACK, 'options' => 'AjaxAdmin::checkKey' ]
|
||||
'__icon' => ['filter' => FILTER_CALLBACK, 'options' => 'AjaxAdmin::checkKey' ],
|
||||
'status' => ['filter' => FILTER_CALLBACK, 'options' => 'AjaxHandler::checkInt'],
|
||||
'msg' => ['filter' => FILTER_UNSAFE_RAW, 'flags' => FILTER_FLAG_STRIP_AOWOW]
|
||||
);
|
||||
|
||||
public function __construct(array $params)
|
||||
{
|
||||
parent::__construct($params);
|
||||
|
||||
// requires 'action' parameter in any case
|
||||
if (!$this->_get['action'] || !$this->params)
|
||||
if (!$this->params)
|
||||
return;
|
||||
|
||||
if ($this->params[0] == 'screenshots')
|
||||
if ($this->params[0] == 'screenshots' && $this->_get['action'])
|
||||
{
|
||||
if (!User::isInGroup(U_GROUP_ADMIN | U_GROUP_BUREAU | U_GROUP_SCREENSHOT))
|
||||
return;
|
||||
@@ -54,7 +55,7 @@ class AjaxAdmin extends AjaxHandler
|
||||
else if ($this->_get['action'] == 'relocate')
|
||||
$this->handler = 'ssRelocate';
|
||||
}
|
||||
else if ($this->params[0] == 'siteconfig')
|
||||
else if ($this->params[0] == 'siteconfig' && $this->_get['action'])
|
||||
{
|
||||
if (!User::isInGroup(U_GROUP_DEV | U_GROUP_ADMIN))
|
||||
return;
|
||||
@@ -66,7 +67,7 @@ class AjaxAdmin extends AjaxHandler
|
||||
else if ($this->_get['action'] == 'update')
|
||||
$this->handler = 'confUpdate';
|
||||
}
|
||||
else if ($this->params[0] == 'weight-presets')
|
||||
else if ($this->params[0] == 'weight-presets' && $this->_get['action'])
|
||||
{
|
||||
if (!User::isInGroup(U_GROUP_DEV | U_GROUP_ADMIN | U_GROUP_BUREAU))
|
||||
return;
|
||||
@@ -81,6 +82,13 @@ class AjaxAdmin extends AjaxHandler
|
||||
|
||||
$this->handler = 'spawnPosFix';
|
||||
}
|
||||
else if ($this->params[0] == 'guide')
|
||||
{
|
||||
if (!User::isInGroup(U_GROUP_STAFF))
|
||||
return;
|
||||
|
||||
$this->handler = 'guideManage';
|
||||
}
|
||||
}
|
||||
|
||||
// get all => null (optional)
|
||||
@@ -467,6 +475,57 @@ class AjaxAdmin extends AjaxHandler
|
||||
return '-1';
|
||||
}
|
||||
|
||||
protected function guideManage() : string
|
||||
{
|
||||
$update = function (int $id, int $status, ?string $msg = null) : bool
|
||||
{
|
||||
if (!DB::Aowow()->query('UPDATE ?_guides SET `status` = ?d WHERE `id` = ?d', $status, $id))
|
||||
return false;
|
||||
|
||||
// set display rev to latest
|
||||
if ($status == GUIDE_STATUS_APPROVED)
|
||||
DB::Aowow()->query('UPDATE ?_guides SET `rev` = (SELECT `rev` FROM ?_articles WHERE `type` = ?d AND `typeId` = ?d ORDER BY `rev` DESC LIMIT 1) WHERE `id` = ?d', Type::GUIDE, $id, $id);
|
||||
|
||||
DB::Aowow()->query('INSERT INTO ?_guides_changelog (`id`, `date`, `userId`, `status`) VALUES (?d, ?d, ?d, ?d)', $id, time(), User::$id, $status);
|
||||
if ($msg)
|
||||
DB::Aowow()->query('INSERT INTO ?_guides_changelog (`id`, `date`, `userId`, `msg`) VALUES (?d, ?d, ?d, ?)' , $id, time(), User::$id, $msg);
|
||||
return true;
|
||||
};
|
||||
|
||||
if (!$this->_post['id'])
|
||||
trigger_error('AjaxHander::guideManage - malformed request: id: '.$this->_post['id'].', status: '.$this->_post['status']);
|
||||
else
|
||||
{
|
||||
$guide = DB::Aowow()->selectRow('SELECT `userId`, `status` FROM ?_guides WHERE `id` = ?d', $this->_post['id']);
|
||||
if (!$guide)
|
||||
trigger_error('AjaxHander::guideManage - guide #'.$this->_post['id'].' not found');
|
||||
else
|
||||
{
|
||||
if ($this->_post['status'] == $guide['status'])
|
||||
trigger_error('AjaxHander::guideManage - guide #'.$this->_post['id'].' already has status #'.$this->_post['status']);
|
||||
else
|
||||
{
|
||||
if ($this->_post['status'] == GUIDE_STATUS_APPROVED)
|
||||
{
|
||||
if ($update($this->_post['id'], GUIDE_STATUS_APPROVED, $this->_post['msg']))
|
||||
{
|
||||
Util::gainSiteReputation($guide['userId'], SITEREP_ACTION_ARTICLE, ['id' => $this->_post['id']]);
|
||||
return '1';
|
||||
}
|
||||
else
|
||||
return '-2';
|
||||
}
|
||||
else if ($this->_post['status'] == GUIDE_STATUS_REJECTED)
|
||||
return $update($this->_post['id'], GUIDE_STATUS_REJECTED, $this->_post['msg']) ? '1' : '-2';
|
||||
else
|
||||
trigger_error('AjaxHander::guideManage - unhandled status change request');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '-1';
|
||||
}
|
||||
|
||||
|
||||
/***************************/
|
||||
/* additional input filter */
|
||||
@@ -499,6 +558,11 @@ class AjaxAdmin extends AjaxHandler
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
/**********/
|
||||
/* helper */
|
||||
/**********/
|
||||
|
||||
private static function confOnChange(string $key, string $val, string &$msg) : bool
|
||||
{
|
||||
$fn = $buildList = null;
|
||||
|
||||
@@ -103,7 +103,7 @@ class AjaxComment extends AjaxHandler
|
||||
Util::gainSiteReputation(User::$id, SITEREP_ACTION_COMMENT, ['id' => $postIdx]);
|
||||
|
||||
// every comment starts with a rating of +1 and i guess the simplest thing to do is create a db-entry with the system as owner
|
||||
DB::Aowow()->query('INSERT INTO ?_comments_rates (commentId, userId, value) VALUES (?d, 0, 1)', $postIdx);
|
||||
DB::Aowow()->query('INSERT INTO ?_user_ratings (`type`, `entry`, `userId`, `value`) VALUES (?d, ?d, 0, 1)', RATING_COMMENT, $postIdx);
|
||||
|
||||
// flag target with hasComment
|
||||
if ($tbl = Type::getClassAttrib($this->_get['type'], 'dataTable'))
|
||||
@@ -122,7 +122,13 @@ class AjaxComment extends AjaxHandler
|
||||
$_SESSION['error']['co'] = Lang::main('cannotComment');
|
||||
|
||||
$this->doRedirect = true;
|
||||
return '?'.Type::getFileString($this->_get['type']).'='.$this->_get['typeid'].'#comments';
|
||||
|
||||
$idOrUrl = $this->_get['typeid'];
|
||||
if ($this->_get['type'] == Type::GUIDE)
|
||||
if ($_ = DB::Aowow()->selectCell('SELECT `url` FROM ?_guides WHERE `id` = ?d', $this->_get['typeid']))
|
||||
$idOrUrl = $_;
|
||||
|
||||
return '?'.Type::getFileString($this->_get['type']).'='.$idOrUrl.'#comments';
|
||||
}
|
||||
|
||||
protected function handleCommentEdit() : void
|
||||
@@ -224,7 +230,7 @@ class AjaxComment extends AjaxHandler
|
||||
if (!$this->_get['id'])
|
||||
return Util::toJSON(['success' => 0]);
|
||||
|
||||
if ($votes = DB::Aowow()->selectRow('SELECT 1 AS success, SUM(IF(value > 0, value, 0)) AS up, SUM(IF(value < 0, -value, 0)) AS down FROM ?_comments_rates WHERE commentId = ?d and userId <> 0 GROUP BY commentId', $this->_get['id']))
|
||||
if ($votes = DB::Aowow()->selectRow('SELECT 1 AS success, SUM(IF(`value` > 0, `value`, 0)) AS up, SUM(IF(`value` < 0, -`value`, 0)) AS down FROM ?_user_ratings WHERE `type` = ?d AND `entry` = ?d AND userId <> 0 GROUP BY `entry`', RATING_COMMENT, $this->_get['id']))
|
||||
return Util::toJSON($votes);
|
||||
else
|
||||
return Util::toJSON(['success' => 1, 'up' => 0, 'down' => 0]);
|
||||
@@ -235,7 +241,7 @@ class AjaxComment extends AjaxHandler
|
||||
if (!User::$id || !$this->_get['id'] || !$this->_get['rating'])
|
||||
return Util::toJSON(['error' => 1, 'message' => Lang::main('genericError')]);
|
||||
|
||||
$target = DB::Aowow()->selectRow('SELECT c.userId AS owner, cr.value FROM ?_comments c LEFT JOIN ?_comments_rates cr ON cr.commentId = c.id AND cr.userId = ?d WHERE c.id = ?d', User::$id, $this->_get['id']);
|
||||
$target = DB::Aowow()->selectRow('SELECT c.`userId` AS owner, ur.`value` FROM ?_comments c LEFT JOIN ?_user_ratings ur ON ur.`type` = ?d AND ur.`entry` = c.id AND ur.`userId` = ?d WHERE c.id = ?d', RATING_COMMENT, User::$id, $this->_get['id']);
|
||||
$val = User::canSupervote() ? 2 : 1;
|
||||
if ($this->_get['rating'] < 0)
|
||||
$val *= -1;
|
||||
@@ -250,9 +256,9 @@ class AjaxComment extends AjaxHandler
|
||||
$ok = false;
|
||||
// old and new have same sign; undo vote (user may have gained/lost access to superVote in the meantime)
|
||||
if ($target['value'] && ($target['value'] < 0) == ($val < 0))
|
||||
$ok = DB::Aowow()->query('DELETE FROM ?_comments_rates WHERE commentId = ?d AND userId = ?d', $this->_get['id'], User::$id);
|
||||
$ok = DB::Aowow()->query('DELETE FROM ?_user_ratings WHERE `type` = ?d AND `entry` = ?d AND `userId` = ?d', RATING_COMMENT, $this->_get['id'], User::$id);
|
||||
else // replace, because we may be overwriting an old, opposing vote
|
||||
if ($ok = DB::Aowow()->query('REPLACE INTO ?_comments_rates (commentId, userId, value) VALUES (?d, ?d, ?d)', (int)$this->_get['id'], User::$id, $val))
|
||||
if ($ok = DB::Aowow()->query('REPLACE INTO ?_user_ratings (`type`, `entry`, `userId`, `value`) VALUES (?d, ?d, ?d, ?d)', RATING_COMMENT, (int)$this->_get['id'], User::$id, $val))
|
||||
User::decrementDailyVotes(); // do not refund retracted votes!
|
||||
|
||||
if (!$ok)
|
||||
@@ -385,7 +391,7 @@ class AjaxComment extends AjaxHandler
|
||||
}
|
||||
|
||||
if (DB::Aowow()->query('DELETE FROM ?_comments WHERE id = ?d{ AND userId = ?d}', $this->_post['id'][0], User::isInGroup(U_GROUP_MODERATOR) ? DBSIMPLE_SKIP : User::$id))
|
||||
DB::Aowow()->query('DELETE FROM ?_comments_rates WHERE commentId = ?d', $this->_post['id'][0]);
|
||||
DB::Aowow()->query('DELETE FROM ?_user_ratings WHERE `type` = ?d AND `entry` = ?d', RATING_COMMENT, $this->_post['id'][0]);
|
||||
else
|
||||
trigger_error('AjaxComment::handleReplyDelete - deleting comment #'.$this->_post['id'][0].' by user #'.User::$id.' from db failed', E_USER_ERROR);
|
||||
}
|
||||
@@ -417,7 +423,8 @@ class AjaxComment extends AjaxHandler
|
||||
}
|
||||
|
||||
$ok = DB::Aowow()->query(
|
||||
'INSERT INTO ?_comments_rates (commentId, userId, value) VALUES (?d, ?d, ?d)',
|
||||
'INSERT INTO ?_user_ratings (`type`, `entry`, `userId`, `value`) VALUES (?d, ?d, ?d, ?d)',
|
||||
RATING_COMMENT,
|
||||
$this->_post['id'][0],
|
||||
User::$id,
|
||||
User::canSupervote() ? 2 : 1
|
||||
@@ -448,7 +455,8 @@ class AjaxComment extends AjaxHandler
|
||||
}
|
||||
|
||||
$ok = DB::Aowow()->query(
|
||||
'INSERT INTO ?_comments_rates (commentId, userId, value) VALUES (?d, ?d, ?d)',
|
||||
'INSERT INTO ?_user_ratings (`type`, `entry`, `userId`, `value`) VALUES (?d, ?d, ?d, ?d)',
|
||||
RATING_COMMENT,
|
||||
$this->_post['id'][0],
|
||||
User::$id,
|
||||
User::canSupervote() ? -2 : -1
|
||||
|
||||
80
includes/ajaxHandler/edit.class.php
Normal file
80
includes/ajaxHandler/edit.class.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
if (!defined('AOWOW_REVISION'))
|
||||
die('illegal access');
|
||||
|
||||
class AjaxEdit extends AjaxHandler
|
||||
{
|
||||
protected $_get = array(
|
||||
'qqfile' => ['filter' => FILTER_UNSAFE_RAW, 'flags' => FILTER_FLAG_STRIP_AOWOW],
|
||||
'guide' => ['filter' => FILTER_SANITIZE_NUMBER_INT]
|
||||
);
|
||||
|
||||
public function __construct(array $params)
|
||||
{
|
||||
parent::__construct($params);
|
||||
|
||||
if (!$params)
|
||||
return;
|
||||
|
||||
if ($params[0] == 'image')
|
||||
$this->handler = 'handleUpload';
|
||||
else if ($params[0] == 'article') // has it's own editor page
|
||||
$this->handler = null;
|
||||
}
|
||||
|
||||
/*
|
||||
success: bool
|
||||
id: image enumerator
|
||||
type: 3 ? png : jpg
|
||||
name: old filename
|
||||
error: errString
|
||||
*/
|
||||
protected function handleUpload() : string
|
||||
{
|
||||
if (!User::$id || $this->_get['guide'] != 1)
|
||||
return Util::toJSON(['success' => false, 'error' => '']);
|
||||
|
||||
require_once('includes/libs/qqFileUploader.class.php');
|
||||
|
||||
$targetPath = 'static/uploads/guide/images/';
|
||||
$tmpPath = 'static/uploads/temp/';
|
||||
$tmpFile = User::$displayName.'-'.Type::GUIDE.'-0-'.Util::createHash(16);
|
||||
|
||||
$uploader = new qqFileUploader(['jpg', 'jpeg', 'png'], 10 * 1024 * 1024);
|
||||
$result = $uploader->handleUpload($tmpPath, $tmpFile, true);
|
||||
|
||||
if (isset($result['success']))
|
||||
{
|
||||
$finfo = new finfo(FILEINFO_MIME);
|
||||
$mime = $finfo->file($tmpPath.$result['newFilename']);
|
||||
if (preg_match('/^image\/(png|jpe?g)/i', $mime, $m))
|
||||
{
|
||||
$i = 1; // image index
|
||||
if ($files = scandir($targetPath, SCANDIR_SORT_DESCENDING))
|
||||
if (rsort($files, SORT_NATURAL) && $files[0] != '.' && $files[0] != '..')
|
||||
$i = explode('.', $files[0])[0] + 1;
|
||||
|
||||
$targetFile = $i . ($m[1] == 'png' ? '.png' : '.jpg');
|
||||
|
||||
// move to final location
|
||||
if (!rename($tmpPath.$result['newFilename'], $targetPath.$targetFile))
|
||||
return Util::toJSON(['error' => Lang::main('intError')]);
|
||||
|
||||
// send success
|
||||
return Util::toJSON(array(
|
||||
'success' => true,
|
||||
'id' => $i,
|
||||
'type' => $m[1] == 'png' ? 3 : 2,
|
||||
'name' => $this->_get['qqfile']
|
||||
));
|
||||
}
|
||||
|
||||
return Util::toJSON(['error' => Lang::screenshot('error', 'unkFormat')]);
|
||||
}
|
||||
|
||||
return Util::toJSON($result);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
35
includes/ajaxHandler/getdescription.class.php
Normal file
35
includes/ajaxHandler/getdescription.class.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
if (!defined('AOWOW_REVISION'))
|
||||
die('illegal access');
|
||||
|
||||
class AjaxGetdescription extends AjaxHandler
|
||||
{
|
||||
protected $_post = array(
|
||||
'description' => [FILTER_CALLBACK, ['options' => 'AjaxHandler::checkFulltext']]
|
||||
);
|
||||
|
||||
public function __construct(array $params)
|
||||
{
|
||||
parent::__construct($params);
|
||||
|
||||
if (!$params || $params[0]) // should be empty
|
||||
return;
|
||||
|
||||
$this->handler = 'handleDescription';
|
||||
}
|
||||
|
||||
protected function handleDescription() : string
|
||||
{
|
||||
$this->contentType = MIME_TYPE_TEXT;
|
||||
|
||||
if (!User::$id)
|
||||
return '';
|
||||
|
||||
$desc = (new Markup($this->_post['description']))->stripTags();
|
||||
|
||||
return Lang::trimTextClean($desc, 120);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
61
includes/ajaxHandler/guide.class.php
Normal file
61
includes/ajaxHandler/guide.class.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
if (!defined('AOWOW_REVISION'))
|
||||
die('illegal access');
|
||||
|
||||
class AjaxGuide extends AjaxHandler
|
||||
{
|
||||
protected $_post = array(
|
||||
'id' => [FILTER_SANITIZE_NUMBER_INT, null],
|
||||
'rating' => [FILTER_SANITIZE_NUMBER_INT, null]
|
||||
);
|
||||
|
||||
public function __construct(array $params)
|
||||
{
|
||||
parent::__construct($params);
|
||||
|
||||
if (!$this->params || count($this->params) != 1)
|
||||
return;
|
||||
|
||||
$this->contentType = MIME_TYPE_TEXT;
|
||||
|
||||
// select handler
|
||||
if ($this->params[0] == 'vote')
|
||||
$this->handler = 'voteGuide';
|
||||
}
|
||||
|
||||
protected function voteGuide() : string
|
||||
{
|
||||
if (!$this->_post['id'] || $this->_post['rating'] < 0 || $this->_post['rating'] > 5)
|
||||
{
|
||||
header('HTTP/1.0 404 Not Found', true, 404);
|
||||
return '';
|
||||
}
|
||||
else if (!User::canUpvote() || !User::canDownvote()) // same logic as comments?
|
||||
{
|
||||
header('HTTP/1.0 403 Forbidden', true, 403);
|
||||
return '';
|
||||
}
|
||||
// by id, not own, published
|
||||
if ($g = DB::Aowow()->selectRow('SELECT `userId`, `cuFlags` FROM ?_guides WHERE `id` = ?d AND (`status` = ?d OR `rev` > 0)', $this->_post['id'], GUIDE_STATUS_APPROVED))
|
||||
{
|
||||
if ($g['cuFlags'] & GUIDE_CU_NO_RATING || $g['userId'] == User::$id)
|
||||
{
|
||||
header('HTTP/1.0 403 Forbidden', true, 403);
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!$this->_post['rating'])
|
||||
DB::Aowow()->query('DELETE FROM ?_user_ratings WHERE `type` = ?d AND `entry` = ?d AND `userId` = ?d', RATING_GUIDE, $this->_post['id'], User::$id);
|
||||
else
|
||||
DB::Aowow()->query('REPLACE INTO ?_user_ratings VALUES (?d, ?d, ?d, ?d)', RATING_GUIDE, $this->_post['id'], User::$id, $this->_post['rating']);
|
||||
|
||||
$res = DB::Aowow()->selectRow('SELECT IFNULL(SUM(`value`), 0) AS `t`, IFNULL(COUNT(*), 0) AS `n` FROM ?_user_ratings WHERE `type` = ?d AND `entry` = ?d', RATING_GUIDE, $this->_post['id']);
|
||||
return Util::toJSON($res['n'] ? ['rating' => $res['t'] / $res['n'], 'nvotes' => $res['n']] : ['rating' => 0, 'nvotes' => 0]);
|
||||
}
|
||||
|
||||
return Util::toJSON(['rating' => 0, 'nvotes' => 0]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user