Completed
Push — master ( 8239ac...55b92e )
by Michael
01:49
created

submit.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Xmf\Request;
4
5
require_once __DIR__ . '/header.php';
6
7
// Объявляем объекты
8
$insinstrHandler = xoops_getModuleHandler('instruction', 'instruction');
9
//$inscatHandler = xoops_getModuleHandler( 'category', 'instruction' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
10
$inspageHandler = xoops_getModuleHandler('page', 'instruction');
11
12
//
13
$uid  = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
14
$time = time();
15
16
// ID инструкции
17
$instrid = isset($_GET['instrid']) ? (int)$_GET['instrid'] : 0;
18
$instrid = isset($_POST['instrid']) ? (int)$_POST['instrid'] : $instrid;
19
// ID страницы
20
$pageid = isset($_GET['pageid']) ? (int)$_GET['pageid'] : 0;
21
$pageid = isset($_POST['pageid']) ? (int)$_POST['pageid'] : $pageid;
22
// ID категории
23
$cid = isset($_POST['cid']) ? (int)$_POST['cid'] : 0;
24
// Вес
25
$weight = isset($_POST['weight']) ? (int)$_POST['weight'] : 0;
26
//
27
$pid = isset($_POST['pid']) ? (int)$_POST['pid'] : 0;
28
29
// Права на добавление
30
$cat_submit = InstructionUtility::getItemIds($moduleDirName . '_submit');
31
// Права на редактирование
32
$cat_edit = InstructionUtility::getItemIds($moduleDirName . '_edit');
33
34
$op = isset($_GET['op']) ? $_GET['op'] : '';
35
$op = isset($_POST['op']) ? $_POST['op'] : $op;
36
37
switch ($op) {
38
39
    case 'editpage':
40
41
        // Задание тайтла
42
        $xoopsOption['xoops_pagetitle'] = '';
43
        // Шаблон
44
        $GLOBALS['xoopsOption']['template_main'] = $moduleDirName . '_editpage.tpl';
45
        // Заголовок
46
        include_once $GLOBALS['xoops']->path('header.php');
47
48
        // Если мы редактируем страницу
49
        if ($pageid) {
50
            // Получаем объект страницы
51
            $objInspage = $inspageHandler->get($pageid);
52
            // ID инструкции
53
            $instrid = $objInspage->getVar('instrid');
54
            // Объект инструкции
55
            $objInsinstr = $insinstrHandler->get($instrid);
56
            // Можно ли редактировать инструкцию в данной категории
57
            if (!in_array($objInsinstr->getVar('cid'), $cat_edit)) {
58
                redirect_header('index.php', 3, _MD_INSTRUCTION_NOPERM_EDITPAGE);
59
            }
60
            // Создание новой страницы
61
        } elseif ($instrid) {
62
63
            // Если нельзя добавлять не в одну категорию
64
            //if( ! count( $cat_submit ) ) redirect_header( 'index.php', 3, _MD_INSTRUCTION_NOPERM_SUBMIT_PAGE );
0 ignored issues
show
Unused Code Comprehensibility introduced by
52% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
            // Создаём объект страницы
66
            $objInspage = $inspageHandler->create();
67
            // Объект инструкции
68
            $objInsinstr = $insinstrHandler->get($instrid);
69
            // Можно ли добавлять инструкции в данной категории
70
            if (!in_array($objInsinstr->getVar('cid'), $cat_submit)) {
71
                redirect_header('index.php', 3, _MD_INSTRUCTION_NOPERM_SUBMITPAGE);
72
            }
73
        } else {
74
            redirect_header('index.php', 3, _MD_INSTRUCTION_BADREQUEST);
75
        }
76
77
        // Информация об инструкции
78
79
        // Массив данных об инструкции
80
        $instrs = [];
81
        // ID инструкции
82
        $instrs['instrid'] = $objInsinstr->getVar('instrid');
83
        // Название страницы
84
        $instrs['title'] = $objInsinstr->getVar('title');
85
        // Описание
86
        $instrs['description'] = $objInsinstr->getVar('description');
87
88
        // Выводим в шаблон
89
        $GLOBALS['xoopsTpl']->assign('insInstr', $instrs);
90
91
        //
92
93
        $form = $objInspage->getForm('submit.php', $instrid);
94
        // Форма
95
        $GLOBALS['xoopsTpl']->assign('insFormPage', $form->render());
96
97
        // Подвал
98
        include_once $GLOBALS['xoops']->path('footer.php');
99
100
        break;
101
    // Сохранение страницы
102
    case 'savepage':
103
104
        // Проверка
105 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
            redirect_header('index.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
107
        }
108
109
        $err         = false;
110
        $message_err = '';
111
112
        // Если мы редактируем
113
        if ($pageid) {
114
            $objInspage = $inspageHandler->get($pageid);
115
            // Объект инструкции
116
            $objInsinstr = $insinstrHandler->get($objInspage->getVar('instrid'));
117
            // Можно ли редактировать инструкцию в данной категории
118
            if (!in_array($objInsinstr->getVar('cid'), $cat_edit)) {
119
                redirect_header('index.php', 3, _MD_INSTRUCTION_NOPERM_EDITPAGE);
120
            }
121
        } elseif ($instrid) {
122
            $objInspage = $inspageHandler->create();
123
            // Объект инструкции
124
            $objInsinstr = $insinstrHandler->get($instrid);
125
            // Можно ли добавлять инструкции в данной категории
126
            if (!in_array($objInsinstr->getVar('cid'), $cat_submit)) {
127
                redirect_header('index.php', 3, _MD_INSTRUCTION_NOPERM_SUBMITPAGE);
128
            }
129
130
            // Если мы создаём страницу необходимо указать к какой инструкции
131
            $objInspage->setVar('instrid', $instrid);
132
            // Указываем дату создания
133
            $objInspage->setVar('datecreated', $time);
134
            // Указываем пользователя
135
            $objInspage->setVar('uid', $uid);
136
        } else {
137
            redirect_header('index.php', 3, _MD_INSTRUCTION_BADREQUEST);
138
        }
139
140
        // Родительская страница
141
        $objInspage->setVar('pid', $pid);
142
        // Дата обновления
143
        $objInspage->setVar('dateupdated', $time);
144
        //
145
        $objInspage->setVar('title', $_POST['title']);
146
        $objInspage->setVar('weight', $weight);
147
        $objInspage->setVar('hometext', $_POST['hometext']);
148
        // Сноска
149
        $objInspage->setVar('footnote', $_POST['footnote']);
150
        $objInspage->setVar('status', $_POST['status']);
151
        $objInspage->setVar('keywords', $_POST['keywords']);
152
        $objInspage->setVar('description', $_POST['description']);
153
154
        // Проверка категорий
155
        if (!$pageid && !$instrid) {
156
            $err         = true;
157
            $message_err .= _MD_INSTRUCTION_ERR_INSTR . '<br>';
158
        }
159
        // Проверка веса
160
        if (0 == $weight) {
161
            $err         = true;
162
            $message_err .= _MD_INSTRUCTION_ERR_WEIGHT . '<br>';
163
        }
164
        // Проверка родительской страницы
165
        if ($pageid && ($pageid == $pid)) {
166
            $err         = true;
167
            $message_err .= _MD_INSTRUCTION_ERR_PPAGE . '<br>';
168
        }
169
        // Если были ошибки
170
        if (true === $err) {
171
            // Задание тайтла
172
            $xoopsOption['xoops_pagetitle'] = '';
173
            // Шаблон
174
            $GLOBALS['xoopsOption']['template_main'] = $moduleDirName . '_savepage.tpl';
175
            // Заголовок
176
            include_once $GLOBALS['xoops']->path('header.php');
177
            // Сообщение об ошибке
178
            $message_err = '<div class="errorMsg" style="text-align: left;">' . $message_err . '</div>';
179
            // Выводим ошибки в шаблон
180
            $GLOBALS['xoopsTpl']->assign('insErrorMsg', $message_err);
181
            // Если небыло ошибок
182
        } else {
183
            // Вставляем данные в БД
184
            if ($inspageHandler->insert($objInspage)) {
185
                // Если мы редактируем
186
                if ($pageid) {
187
                    // Обновление даты
188
                    $sql = sprintf('UPDATE %s SET `dateupdated` = %u WHERE `instrid` = %u', $GLOBALS['xoopsDB']->prefix($moduleDirName . '_instr'), $time, $instrid);
189
                    $GLOBALS['xoopsDB']->query($sql);
190
                    // Запись в лог
191
                    xoops_loadLanguage('main', 'userslog');
192
                    //userslog_insert( $objInsinstr->getVar('title') . ': ' . $objInspage->getVar('title'), _MD_USERSLOG_MODIFY_PAGE );
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
193
                    //
194
                    redirect_header('index.php', 3, _MD_INSTRUCTION_PAGEMODIFY);
195
                    // Если мы добавляем
196
                } else {
197
                    // Инкримент комментов
198
                    $inspageHandler->updateposts($uid, $_POST['status'], 'add');
199
                    // Инкремент страниц и обновление даты
200
                    $sql = sprintf('UPDATE %s SET `pages` = `pages` + 1, `dateupdated` = %u WHERE `instrid` = %u', $GLOBALS['xoopsDB']->prefix($moduleDirName . '_instr'), $time, $instrid);
201
                    $GLOBALS['xoopsDB']->query($sql);
202
                    // Запись в лог
203
                    xoops_loadLanguage('main', 'userslog');
204
                    //userslog_insert( $objInsinstr->getVar('title') . ': ' . $objInspage->getVar('title'), _MD_USERSLOG_SUBMIT_PAGE );
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
205
                    //
206
                    redirect_header('index.php', 3, _MD_INSTRUCTION_PAGEADDED);
207
                }
208
            }
209
210
            // Задание тайтла
211
            $xoopsOption['xoops_pagetitle'] = '';
212
            // Шаблон
213
            $GLOBALS['xoopsOption']['template_main'] = $moduleDirName . '_savepage.tpl';
214
            // Заголовок
215
            include_once $GLOBALS['xoops']->path('header.php');
216
217
            // Выводим ошибки в шаблон
218
            $GLOBALS['xoopsTpl']->assign('insErrorMsg', $objInspage->getHtmlErrors());
219
        }
220
        // Получаем форму
221
        $form = $objInspage->getForm('submit.php', $instrid);
222
223
        // Форма
224
        $GLOBALS['xoopsTpl']->assign('insFormPage', $form->render());
225
226
        // Подвал
227
        include_once $GLOBALS['xoops']->path('footer.php');
228
229
        break;
230
}
231