-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathParser.php
More file actions
120 lines (107 loc) · 5.63 KB
/
Parser.php
File metadata and controls
120 lines (107 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* Copyright (C) 2015-2019 FeatherBB
* based on code by (C) 2008-2015 FluxBB
* and Rickard Andersson (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
namespace FeatherBB\Controller\Admin;
use FeatherBB\Core\AdminUtils;
use FeatherBB\Core\Error;
use FeatherBB\Core\Interfaces\Cache as CacheInterface;
use FeatherBB\Core\Interfaces\Container;
use FeatherBB\Core\Interfaces\ForumEnv;
use FeatherBB\Core\Interfaces\ForumSettings;
use FeatherBB\Core\Interfaces\Hooks;
use FeatherBB\Core\Interfaces\Input;
use FeatherBB\Core\Interfaces\Lang;
use FeatherBB\Core\Interfaces\Router;
use FeatherBB\Core\Interfaces\User;
use FeatherBB\Core\Interfaces\View;
use FeatherBB\Core\Url;
use FeatherBB\Core\Utils;
use FeatherBB\Model\Cache;
class Parser
{
public function __construct()
{
$this->model = new \FeatherBB\Model\Admin\Parser();
Lang::load('admin/parser');
if (!User::isAdmin()) {
throw new Error(__('No permission'), '403');
}
if (!CacheInterface::isCached('smilies')) {
CacheInterface::store('smilies', Cache::getSmilies());
}
}
public function display($req, $res, $args)
{
Hooks::fire('controller.admin.parser.display');
$smilies = CacheInterface::retrieve('smilies');
if (Input::post('form_sent')) {
// Upload new smiley image to style/img/smilies
if (Input::post('upload') && isset($_FILES['new_smiley']) && isset($_FILES['new_smiley']['error'])) {
$f = $_FILES['new_smiley'];
switch ($f['error']) {
case 0: // 0: Successful upload.
$name = str_replace(' ', '_', $f['name']); // Convert spaces to underscore.
$name = preg_replace('/[^\w\-.]/S', '', $name); // Weed out all unsavory filename chars.
if (preg_match('/^[\w\-.]++$/', $name)) { // If we have a valid filename?
if (preg_match('%^image/%', $f['type'])) { // If we have an image file type?
if ($f['size'] > 0 && $f['size'] <= ForumSettings::get('o_avatars_size')) {
if (move_uploaded_file($f['tmp_name'], ForumEnv::get('FEATHER_ROOT') .'style/img/smilies/'. $name)) {
return Router::redirect(Router::pathFor('adminParser'), __('upload success'));
} else { // Error #1: 'Smiley upload failed. Unable to move to smiley folder.'.
throw new Error(__('upload_err_1'), 500);
}
} else { // Error #2: 'Smiley upload failed. File is too big.'
throw new Error(__('upload_err_2'), 400);
}
} else { // Error #3: 'Smiley upload failed. File type is not an image.'.
throw new Error(__('upload_err_3'), 400);
}
} else { // Error #4: 'Smiley upload failed. Bad filename.'
throw new Error(__('upload_err_4'), 400);
}
break;
case 1: // case 1 similar to case 2 so fall through...
case 2: throw new Error(__('upload_err_2'), 400); // File exceeds MAX_FILE_SIZE.
case 3: throw new Error(__('upload_err_5'), 400); // File only partially uploaded.
// case 4: break; // No error. Normal response when this form element left empty
case 4: throw new Error(__('upload_err_6'), 400); // No filename.
case 6: throw new Error(__('upload_err_7'), 500); // No temp folder.
case 7: throw new Error(__('upload_err_8'), 500); // Cannot write to disk.
default: throw new Error(__('upload_err_9'), 500); // Generic/unknown error
}
}
// Set new $smilies values:
if (Input::post('smiley_text') && is_array(Input::post('smiley_text')) &&
Input::post('smiley_file') && is_array(Input::post('smiley_file')) &&
count(Input::post('smiley_text')) === count(Input::post('smiley_file'))) {
$stext = Input::post('smiley_text');
$sfile = Input::post('smiley_file');
$len = count(Input::post('smiley_text'));
$smilies = [];
for ($i = 0; $i < $len; ++$i) { // Loop through all posted smileys.
if (isset($stext[$i]) && $stext[$i] != '' && $sfile !== 'select new file') {
echo $i.'<br>';
$smilies[$stext[$i]] = $sfile[$i];
}
}
CacheInterface::store('smilies', $smilies);
}
return Router::redirect(Router::pathFor('adminParser'), __('save_success'));
}
AdminUtils::generateAdminMenu('parser');
return View::setPageInfo([
'title' => [Utils::escape(ForumSettings::get('o_board_title')), __('Admin'), __('Parser')],
'active_page' => 'admin',
'admin_console' => true,
'smiley_files' => $this->model->getSmileyFiles(),
'smilies' => $smilies,
'file_uploads' => (ini_get('file_uploads') == 1),
'urlBase' => URL::base().'/style/img/smilies/',
]
)->addTemplate('@forum/admin/parser')->display();
}
}