Skip to content

Commit 95edb0a

Browse files
committed
Post edition API
1 parent d934073 commit 95edb0a

File tree

9 files changed

+193
-71
lines changed

9 files changed

+193
-71
lines changed

featherbb/Controller/Api/Post.php

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace FeatherBB\Controller\Api;
1111

12-
use FeatherBB\Core\Error;
1312
use FeatherBB\Core\Utils;
1413

1514
class Post extends Api
@@ -29,17 +28,59 @@ public function display($req, $res, $args)
2928
public function delete($req, $res, $args)
3029
{
3130
// Fetch some information about the post, the topic and the forum
32-
$cur_post = \FeatherBB\Model\Post::get_info_delete($args['id']);
31+
$cur_post = $this->model->get_info_delete($args['id']);
32+
33+
if (!is_object($cur_post)) {
34+
return $cur_post;
35+
}
3336

3437
if (ForumSettings::get('o_censoring') == '1') {
3538
$cur_post['subject'] = Utils::censor($cur_post['subject']);
3639
}
3740

3841
// Sort out who the moderators are and if we are currently a moderator (or an admin)
39-
$is_topic_post = $this->model->getPermissions($cur_post, $args);
42+
$is_topic_post = $this->model->getDeletePermissions($cur_post, $args);
4043

4144
\FeatherBB\Model\Post::handle_deletion($is_topic_post, $args['id'], $cur_post);
4245

4346
return json_encode("Success", JSON_PRETTY_PRINT);
4447
}
48+
49+
public function update($req, $res, $args)
50+
{
51+
// Fetch some information about the post, the topic and the forum
52+
$cur_post = $this->model->get_info_edit($args['id']);
53+
54+
if (!is_object($cur_post)) {
55+
return $cur_post;
56+
}
57+
58+
$is_admmod = $this->model->getEditPermissions($cur_post);
59+
60+
$can_edit_subject = $args['id'] == $cur_post['first_post_id'];
61+
62+
if (ForumSettings::get('o_censoring') == '1') {
63+
$cur_post['subject'] = Utils::censor($cur_post['subject']);
64+
$cur_post['message'] = Utils::censor($cur_post['message']);
65+
}
66+
67+
// Start with a clean slate
68+
$errors = array();
69+
70+
// Let's see if everything went right
71+
$errors = \FeatherBB\Model\Post::check_errors_before_edit($can_edit_subject, $errors, $is_admmod);
72+
73+
// Setup some variables before post
74+
$post = \FeatherBB\Model\Post::setup_edit_variables($cur_post, $is_admmod, $can_edit_subject, $errors);
75+
76+
// Did everything go according to plan?
77+
if (empty($errors)) {
78+
// Edit the post
79+
$this->model->update($args, $can_edit_subject, $post, $cur_post, $is_admmod);
80+
81+
return json_encode($this->model->display($args['id']), JSON_PRETTY_PRINT);
82+
}
83+
84+
return json_encode($errors, JSON_PRETTY_PRINT);
85+
}
4586
}

featherbb/Controller/Api/Topic.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public function newTopic($req, $res, $args)
2828
// Fetch some info about the topic and/or the forum
2929
$cur_posting = $this->model->get_info_post(false, $args['id']);
3030

31+
if (!is_object($cur_posting)) {
32+
return $cur_posting;
33+
}
34+
3135
$is_admmod = $this->model->checkPermissions($cur_posting, null, $args['id']);
3236

3337
if (!is_bool($is_admmod)) {
@@ -74,6 +78,10 @@ public function newReply($req, $res, $args)
7478
// Fetch some info about the topic and/or the forum
7579
$cur_posting = $this->model->get_info_post($args['id'], false);
7680

81+
if (!is_object($cur_posting)) {
82+
return $cur_posting;
83+
}
84+
7785
$is_subscribed = $args['id'] && $cur_posting['is_subscribed'];
7886

7987
$is_admmod = $this->model->checkPermissions($cur_posting, null, $args['id']);

featherbb/Controller/Forum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct()
2626
public function display($req, $res, $args)
2727
{
2828
Container::get('hooks')->fire('controller.forum.display');
29-
// Fetch some informations about the forum
29+
// Fetch some information about the forum
3030
$cur_forum = $this->model->get_forum_info($args['id']);
3131

3232
// Is this a redirect forum? In that case, redirect!

featherbb/Controller/Post.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function delete($req, $res, $args)
199199
{
200200
Container::get('hooks')->fire('controller.post.delete');
201201

202-
// Fetch some informations about the post, the topic and the forum
202+
// Fetch some information about the post, the topic and the forum
203203
$cur_post = $this->model->get_info_delete($args['id']);
204204

205205
if (ForumSettings::get('o_censoring') == '1') {
@@ -244,7 +244,7 @@ public function editpost($req, $res, $args)
244244
{
245245
Container::get('hooks')->fire('controller.post.edit');
246246

247-
// Fetch some informations about the post, the topic and the forum
247+
// Fetch some information about the post, the topic and the forum
248248
$cur_post = $this->model->get_info_edit($args['id']);
249249

250250
// Sort out who the moderators are and if we are currently a moderator (or an admin)
@@ -274,7 +274,7 @@ public function editpost($req, $res, $args)
274274
Container::get('hooks')->fire('controller.post.edit.submit', $args['id']);
275275

276276
// Let's see if everything went right
277-
$errors = $this->model->check_errors_before_edit($can_edit_subject, $errors);
277+
$errors = $this->model->check_errors_before_edit($can_edit_subject, $errors, $is_admmod);
278278

279279
// Setup some variables before post
280280
$post = $this->model->setup_edit_variables($cur_post, $is_admmod, $can_edit_subject, $errors);

featherbb/Controller/Topic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function display($req, $res, $args)
4444
$lang_antispam_questions = require ForumEnv::get('FEATHER_ROOT').'featherbb/lang/'.User::get()->language.'/antispam.php';
4545
$index_questions = rand(0, count($lang_antispam_questions)-1);
4646

47-
// Fetch some informations about the topic
47+
// Fetch some information about the topic
4848
$cur_topic = $this->model->get_info_topic($args['id']);
4949

5050
// Sort out who the moderators are and if we are currently a moderator (or an admin)

featherbb/Core/Search.php

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,11 @@
1616

1717
class Search
1818
{
19-
public function __construct()
20-
{
21-
22-
23-
// Make a regex that will match CJK or Hangul characters
24-
define('FEATHER_CJK_HANGUL_REGEX', '[' .
25-
'\x{1100}-\x{11FF}' . // Hangul Jamo 1100-11FF (http://www.fileformat.info/info/unicode/block/hangul_jamo/index.htm)
26-
'\x{3130}-\x{318F}' . // Hangul Compatibility Jamo 3130-318F (http://www.fileformat.info/info/unicode/block/hangul_compatibility_jamo/index.htm)
27-
'\x{AC00}-\x{D7AF}' . // Hangul Syllables AC00-D7AF (http://www.fileformat.info/info/unicode/block/hangul_syllables/index.htm)
28-
29-
// Hiragana
30-
'\x{3040}-\x{309F}' . // Hiragana 3040-309F (http://www.fileformat.info/info/unicode/block/hiragana/index.htm)
31-
32-
// Katakana
33-
'\x{30A0}-\x{30FF}' . // Katakana 30A0-30FF (http://www.fileformat.info/info/unicode/block/katakana/index.htm)
34-
'\x{31F0}-\x{31FF}' . // Katakana Phonetic Extensions 31F0-31FF (http://www.fileformat.info/info/unicode/block/katakana_phonetic_extensions/index.htm)
35-
36-
// CJK Unified Ideographs (http://en.wikipedia.org/wiki/CJK_Unified_Ideographs)
37-
'\x{2E80}-\x{2EFF}' . // CJK Radicals Supplement 2E80-2EFF (http://www.fileformat.info/info/unicode/block/cjk_radicals_supplement/index.htm)
38-
'\x{2F00}-\x{2FDF}' . // Kangxi Radicals 2F00-2FDF (http://www.fileformat.info/info/unicode/block/kangxi_radicals/index.htm)
39-
'\x{2FF0}-\x{2FFF}' . // Ideographic Description Characters 2FF0-2FFF (http://www.fileformat.info/info/unicode/block/ideographic_description_characters/index.htm)
40-
'\x{3000}-\x{303F}' . // CJK Symbols and Punctuation 3000-303F (http://www.fileformat.info/info/unicode/block/cjk_symbols_and_punctuation/index.htm)
41-
'\x{31C0}-\x{31EF}' . // CJK Strokes 31C0-31EF (http://www.fileformat.info/info/unicode/block/cjk_strokes/index.htm)
42-
'\x{3200}-\x{32FF}' . // Enclosed CJK Letters and Months 3200-32FF (http://www.fileformat.info/info/unicode/block/enclosed_cjk_letters_and_months/index.htm)
43-
'\x{3400}-\x{4DBF}' . // CJK Unified Ideographs Extension A 3400-4DBF (http://www.fileformat.info/info/unicode/block/cjk_unified_ideographs_extension_a/index.htm)
44-
'\x{4E00}-\x{9FFF}' . // CJK Unified Ideographs 4E00-9FFF (http://www.fileformat.info/info/unicode/block/cjk_unified_ideographs/index.htm)
45-
'\x{20000}-\x{2A6DF}' . // CJK Unified Ideographs Extension B 20000-2A6DF (http://www.fileformat.info/info/unicode/block/cjk_unified_ideographs_extension_b/index.htm)
46-
']');
47-
}
48-
49-
5019
//
5120
// "Cleans up" a text string and returns an array of unique words
5221
// This function depends on the current locale setting
5322
//
54-
public function split_words($text, $idx)
23+
public static function split_words($text, $idx)
5524
{
5625
// Remove BBCode
5726
$text = preg_replace('%\[/?(b|u|s|ins|del|em|i|h|colou?r|quote|code|img|url|email|list|topic|post|forum|user)(?:\=[^\]]*)?\]%', ' ', $text);
@@ -71,7 +40,7 @@ public function split_words($text, $idx)
7140
// Remove any words that should not be indexed
7241
foreach ($words as $key => $value) {
7342
// If the word shouldn't be indexed, remove it
74-
if (!$this->validate_search_word($value, $idx)) {
43+
if (!self::validate_search_word($value, $idx)) {
7544
unset($words[$key]);
7645
}
7746
}
@@ -83,12 +52,12 @@ public function split_words($text, $idx)
8352
//
8453
// Checks if a word is a valid searchable word
8554
//
86-
public function validate_search_word($word, $idx)
55+
public static function validate_search_word($word, $idx)
8756
{
8857
static $stopwords;
8958

9059
// If the word is a keyword we don't want to index it, but we do want to be allowed to search it
91-
if ($this->is_keyword($word)) {
60+
if (self::is_keyword($word)) {
9261
return !$idx;
9362
}
9463

@@ -105,7 +74,7 @@ public function validate_search_word($word, $idx)
10574
}
10675

10776
// If the word is CJK we don't want to index it, but we do want to be allowed to search it
108-
if ($this->is_cjk($word)) {
77+
if (self::is_cjk($word)) {
10978
return !$idx;
11079
}
11180

@@ -121,7 +90,7 @@ public function validate_search_word($word, $idx)
12190
//
12291
// Check a given word is a search keyword.
12392
//
124-
public function is_keyword($word)
93+
public static function is_keyword($word)
12594
{
12695
return $word == 'and' || $word == 'or' || $word == 'not';
12796
}
@@ -130,16 +99,39 @@ public function is_keyword($word)
13099
//
131100
// Check if a given word is CJK or Hangul.
132101
//
133-
public function is_cjk($word)
102+
public static function is_cjk($word)
134103
{
135-
return preg_match('%^' . FEATHER_CJK_HANGUL_REGEX . '+$%u', $word) ? true : false;
104+
// Make a regex that will match CJK or Hangul characters
105+
return preg_match('%^' . '[' .
106+
'\x{1100}-\x{11FF}' . // Hangul Jamo 1100-11FF (http://www.fileformat.info/info/unicode/block/hangul_jamo/index.htm)
107+
'\x{3130}-\x{318F}' . // Hangul Compatibility Jamo 3130-318F (http://www.fileformat.info/info/unicode/block/hangul_compatibility_jamo/index.htm)
108+
'\x{AC00}-\x{D7AF}' . // Hangul Syllables AC00-D7AF (http://www.fileformat.info/info/unicode/block/hangul_syllables/index.htm)
109+
110+
// Hiragana
111+
'\x{3040}-\x{309F}' . // Hiragana 3040-309F (http://www.fileformat.info/info/unicode/block/hiragana/index.htm)
112+
113+
// Katakana
114+
'\x{30A0}-\x{30FF}' . // Katakana 30A0-30FF (http://www.fileformat.info/info/unicode/block/katakana/index.htm)
115+
'\x{31F0}-\x{31FF}' . // Katakana Phonetic Extensions 31F0-31FF (http://www.fileformat.info/info/unicode/block/katakana_phonetic_extensions/index.htm)
116+
117+
// CJK Unified Ideographs (http://en.wikipedia.org/wiki/CJK_Unified_Ideographs)
118+
'\x{2E80}-\x{2EFF}' . // CJK Radicals Supplement 2E80-2EFF (http://www.fileformat.info/info/unicode/block/cjk_radicals_supplement/index.htm)
119+
'\x{2F00}-\x{2FDF}' . // Kangxi Radicals 2F00-2FDF (http://www.fileformat.info/info/unicode/block/kangxi_radicals/index.htm)
120+
'\x{2FF0}-\x{2FFF}' . // Ideographic Description Characters 2FF0-2FFF (http://www.fileformat.info/info/unicode/block/ideographic_description_characters/index.htm)
121+
'\x{3000}-\x{303F}' . // CJK Symbols and Punctuation 3000-303F (http://www.fileformat.info/info/unicode/block/cjk_symbols_and_punctuation/index.htm)
122+
'\x{31C0}-\x{31EF}' . // CJK Strokes 31C0-31EF (http://www.fileformat.info/info/unicode/block/cjk_strokes/index.htm)
123+
'\x{3200}-\x{32FF}' . // Enclosed CJK Letters and Months 3200-32FF (http://www.fileformat.info/info/unicode/block/enclosed_cjk_letters_and_months/index.htm)
124+
'\x{3400}-\x{4DBF}' . // CJK Unified Ideographs Extension A 3400-4DBF (http://www.fileformat.info/info/unicode/block/cjk_unified_ideographs_extension_a/index.htm)
125+
'\x{4E00}-\x{9FFF}' . // CJK Unified Ideographs 4E00-9FFF (http://www.fileformat.info/info/unicode/block/cjk_unified_ideographs/index.htm)
126+
'\x{20000}-\x{2A6DF}' . // CJK Unified Ideographs Extension B 20000-2A6DF (http://www.fileformat.info/info/unicode/block/cjk_unified_ideographs_extension_b/index.htm)
127+
']' . '+$%u', $word) ? true : false;
136128
}
137129

138130

139131
//
140132
// Strip [img] [url] and [email] out of the message so we don't index their contents
141133
//
142-
public function strip_bbcode($text)
134+
public static function strip_bbcode($text)
143135
{
144136
static $patterns;
145137

@@ -159,17 +151,17 @@ public function strip_bbcode($text)
159151
//
160152
// Updates the search index with the contents of $post_id (and $subject)
161153
//
162-
public function update_search_index($mode, $post_id, $message, $subject = null)
154+
public static function update_search_index($mode, $post_id, $message, $subject = null)
163155
{
164156
$message = utf8_strtolower($message);
165157
$subject = utf8_strtolower($subject);
166158

167159
// Remove any bbcode that we shouldn't index
168-
$message = $this->strip_bbcode($message);
160+
$message = self::strip_bbcode($message);
169161

170162
// Split old and new post/subject to obtain array of 'words'
171-
$words_message = $this->split_words($message, true);
172-
$words_subject = ($subject) ? $this->split_words($subject, true) : array();
163+
$words_message = self::split_words($message, true);
164+
$words_subject = ($subject) ? self::split_words($subject, true) : array();
173165

174166
if ($mode == 'edit') {
175167
$select_update_search_index = array('w.id', 'w.word', 'm.subject_match');

0 commit comments

Comments
 (0)