Completed
Push — master ( a4e09c...825da9 )
by Michael
01:51
created

submit.php (1 issue)

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
use Xoopsmodules\instruction;
5
6
require_once __DIR__ . '/header.php';
7
8
// Объявляем объекты
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% 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...
9
//$instructionHandler = xoops_getModuleHandler('instruction', 'instruction');
10
//$categoryHandler = xoops_getModuleHandler( 'category', 'instruction' );
11
//$pageHandler  = xoops_getModuleHandler('page', 'instruction');
12
13
//
14
$uid  = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0;
15
$time = time();
16
17
// ID инструкции
18
$instrid = isset($_GET['instrid']) ? (int)$_GET['instrid'] : 0;
19
$instrid = isset($_POST['instrid']) ? (int)$_POST['instrid'] : $instrid;
20
// ID страницы
21
$pageid = isset($_GET['pageid']) ? (int)$_GET['pageid'] : 0;
22
$pageid = isset($_POST['pageid']) ? (int)$_POST['pageid'] : $pageid;
23
// ID категории
24
$cid = isset($_POST['cid']) ? (int)$_POST['cid'] : 0;
25
// Вес
26
$weight = isset($_POST['weight']) ? (int)$_POST['weight'] : 0;
27
//
28
$pid = isset($_POST['pid']) ? (int)$_POST['pid'] : 0;
29
30
// Права на добавление
31
$cat_submit = Xoopsmodules\instruction\Utility::getItemIds($moduleDirName . '_submit');
32
// Права на редактирование
33
$cat_edit = Xoopsmodules\instruction\Utility::getItemIds($moduleDirName . '_edit');
34
35
$op = isset($_GET['op']) ? $_GET['op'] : '';
36
$op = isset($_POST['op']) ? $_POST['op'] : $op;
37
38
switch ($op) {
39
40
    case 'editpage':
41
42
        // Задание тайтла
43
        $xoopsOption['xoops_pagetitle'] = '';
44
        // Шаблон
45
        $GLOBALS['xoopsOption']['template_main'] = $moduleDirName . '_editpage.tpl';
46
        // Заголовок
47
        include_once $GLOBALS['xoops']->path('header.php');
48
49
        // Если мы редактируем страницу
50
        if ($pageid) {
51
            // Получаем объект страницы
52
            $objInspage = $pageHandler->get($pageid);
53
            // ID инструкции
54
            $instrid = $objInspage->getVar('instrid');
55
            // Объект инструкции
56
            $objInsinstr = $instructionHandler->get($instrid);
57
            // Можно ли редактировать инструкцию в данной категории
58
            if (!in_array($objInsinstr->getVar('cid'), $cat_edit)) {
59
                redirect_header('index.php', 3, _MD_INSTRUCTION_NOPERM_EDITPAGE);
60
            }
61
            // Создание новой страницы
62
        } elseif ($instrid) {
63
64
            // Если нельзя добавлять не в одну категорию
65
            //if( ! count( $cat_submit ) ) redirect_header( 'index.php', 3, _MD_INSTRUCTION_NOPERM_SUBMIT_PAGE );
66
            // Создаём объект страницы
67
            $objInspage = $pageHandler->create();
68
            // Объект инструкции
69
            $objInsinstr = $instructionHandler->get($instrid);
70
            // Можно ли добавлять инструкции в данной категории
71
            if (!in_array($objInsinstr->getVar('cid'), $cat_submit)) {
72
                redirect_header('index.php', 3, _MD_INSTRUCTION_NOPERM_SUBMITPAGE);
73
            }
74
        } else {
75
            redirect_header('index.php', 3, _MD_INSTRUCTION_BADREQUEST);
76
        }
77
78
        // Информация об инструкции
79
80
        // Массив данных об инструкции
81
        $instrs = [];
82
        // ID инструкции
83
        $instrs['instrid'] = $objInsinstr->getVar('instrid');
84
        // Название страницы
85
        $instrs['title'] = $objInsinstr->getVar('title');
86
        // Описание
87
        $instrs['description'] = $objInsinstr->getVar('description');
88
89
        // Выводим в шаблон
90
        $GLOBALS['xoopsTpl']->assign('insInstr', $instrs);
91
92
        //
93
94
        $form = $objInspage->getForm('submit.php', $instrid);
95
        // Форма
96
        $GLOBALS['xoopsTpl']->assign('insFormPage', $form->render());
97
98
        // Подвал
99
        include_once $GLOBALS['xoops']->path('footer.php');
100
101
        break;
102
    // Сохранение страницы
103
    case 'savepage':
104
105
        // Проверка
106 View Code Duplication
        if (!$GLOBALS['xoopsSecurity']->check()) {
107
            redirect_header('index.php', 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors()));
108
        }
109
110
        $err         = false;
111
        $message_err = '';
112
113
        // Если мы редактируем
114
        if ($pageid) {
115
            $objInspage = $pageHandler->get($pageid);
116
            // Объект инструкции
117
            $objInsinstr = $instructionHandler->get($objInspage->getVar('instrid'));
118
            // Можно ли редактировать инструкцию в данной категории
119
            if (!in_array($objInsinstr->getVar('cid'), $cat_edit)) {
120
                redirect_header('index.php', 3, _MD_INSTRUCTION_NOPERM_EDITPAGE);
121
            }
122
        } elseif ($instrid) {
123
            $objInspage = $pageHandler->create();
124
            // Объект инструкции
125
            $objInsinstr = $instructionHandler->get($instrid);
126
            // Можно ли добавлять инструкции в данной категории
127
            if (!in_array($objInsinstr->getVar('cid'), $cat_submit)) {
128
                redirect_header('index.php', 3, _MD_INSTRUCTION_NOPERM_SUBMITPAGE);
129
            }
130
131
            // Если мы создаём страницу необходимо указать к какой инструкции
132
            $objInspage->setVar('instrid', $instrid);
133
            // Указываем дату создания
134
            $objInspage->setVar('datecreated', $time);
135
            // Указываем пользователя
136
            $objInspage->setVar('uid', $uid);
137
        } else {
138
            redirect_header('index.php', 3, _MD_INSTRUCTION_BADREQUEST);
139
        }
140
141
        // Родительская страница
142
        $objInspage->setVar('pid', $pid);
143
        // Дата обновления
144
        $objInspage->setVar('dateupdated', $time);
145
        //
146
        $objInspage->setVar('title', $_POST['title']);
147
        $objInspage->setVar('weight', $weight);
148
        $objInspage->setVar('hometext', $_POST['hometext']);
149
        // Сноска
150
        $objInspage->setVar('footnote', $_POST['footnote']);
151
        $objInspage->setVar('status', $_POST['status']);
152
        $objInspage->setVar('keywords', $_POST['keywords']);
153
        $objInspage->setVar('description', $_POST['description']);
154
155
        // Проверка категорий
156
        if (!$pageid && !$instrid) {
157
            $err         = true;
158
            $message_err .= _MD_INSTRUCTION_ERR_INSTR . '<br>';
159
        }
160
        // Проверка веса
161
        if (0 == $weight) {
162
            $err         = true;
163
            $message_err .= _MD_INSTRUCTION_ERR_WEIGHT . '<br>';
164
        }
165
        // Проверка родительской страницы
166
        if ($pageid && ($pageid == $pid)) {
167
            $err         = true;
168
            $message_err .= _MD_INSTRUCTION_ERR_PPAGE . '<br>';
169
        }
170
        // Если были ошибки
171
        if (true === $err) {
172
            // Задание тайтла
173
            $xoopsOption['xoops_pagetitle'] = '';
174
            // Шаблон
175
            $GLOBALS['xoopsOption']['template_main'] = $moduleDirName . '_savepage.tpl';
176
            // Заголовок
177
            include_once $GLOBALS['xoops']->path('header.php');
178
            // Сообщение об ошибке
179
            $message_err = '<div class="errorMsg" style="text-align: left;">' . $message_err . '</div>';
180
            // Выводим ошибки в шаблон
181
            $GLOBALS['xoopsTpl']->assign('insErrorMsg', $message_err);
182
            // Если небыло ошибок
183
        } else {
184
            // Вставляем данные в БД
185
            if ($pageHandler->insert($objInspage)) {
186
                // Если мы редактируем
187
                if ($pageid) {
188
                    // Обновление даты
189
                    $sql = sprintf('UPDATE %s SET `dateupdated` = %u WHERE `instrid` = %u', $GLOBALS['xoopsDB']->prefix($moduleDirName . '_instr'), $time, $instrid);
190
                    $GLOBALS['xoopsDB']->query($sql);
191
                    // Запись в лог
192
                    xoops_loadLanguage('main', 'userslog');
193
                    //userslog_insert( $objInsinstr->getVar('title') . ': ' . $objInspage->getVar('title'), _MD_USERSLOG_MODIFY_PAGE );
194
                    //
195
                    redirect_header('index.php', 3, _MD_INSTRUCTION_PAGEMODIFY);
196
                    // Если мы добавляем
197
                } else {
198
                    // Инкримент комментов
199
                    $pageHandler->updateposts($uid, $_POST['status'], 'add');
200
                    // Инкремент страниц и обновление даты
201
                    $sql = sprintf('UPDATE %s SET `pages` = `pages` + 1, `dateupdated` = %u WHERE `instrid` = %u', $GLOBALS['xoopsDB']->prefix($moduleDirName . '_instr'), $time, $instrid);
202
                    $GLOBALS['xoopsDB']->query($sql);
203
                    // Запись в лог
204
                    xoops_loadLanguage('main', 'userslog');
205
                    //userslog_insert( $objInsinstr->getVar('title') . ': ' . $objInspage->getVar('title'), _MD_USERSLOG_SUBMIT_PAGE );
206
                    //
207
                    redirect_header('index.php', 3, _MD_INSTRUCTION_PAGEADDED);
208
                }
209
            }
210
211
            // Задание тайтла
212
            $xoopsOption['xoops_pagetitle'] = '';
213
            // Шаблон
214
            $GLOBALS['xoopsOption']['template_main'] = $moduleDirName . '_savepage.tpl';
215
            // Заголовок
216
            include_once $GLOBALS['xoops']->path('header.php');
217
218
            // Выводим ошибки в шаблон
219
            $GLOBALS['xoopsTpl']->assign('insErrorMsg', $objInspage->getHtmlErrors());
220
        }
221
        // Получаем форму
222
        $form = $objInspage->getForm('submit.php', $instrid);
223
224
        // Форма
225
        $GLOBALS['xoopsTpl']->assign('insFormPage', $form->render());
226
227
        // Подвал
228
        include_once $GLOBALS['xoops']->path('footer.php');
229
230
        break;
231
}
232