Completed
Pull Request — develop (#530)
by
unknown
05:43
created

ajax.php ➔ getDocumentTv()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 3 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
define('MODX_API_MODE', true);
4
define('IN_MANAGER_MODE', true);
5
6
include_once("./../../../../index.php");
7
8
$modx->db->connect();
9
10
if (empty ($modx->config)) {
11
    $modx->getSettings();
12
}
13
14
if (!isset($_SESSION['mgrValidated']) || !isset($_SERVER['HTTP_X_REQUESTED_WITH']) || (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') || ($_SERVER['REQUEST_METHOD'] != 'POST')) {
15
    $modx->sendErrorPage();
16
}
17
18
$modx->sid = session_id();
19
$modx->loadExtension("ManagerAPI");
20
extract($modx->config, EXTR_OVERWRITE); // For correct function of native processors
21
22
$_lang = array();
23
include_once MODX_MANAGER_PATH . '/includes/lang/english.inc.php';
24
if ($modx->config['manager_language'] != 'english') {
25
    include_once MODX_MANAGER_PATH . '/includes/lang/' . $modx->config['manager_language'] . '.inc.php';
26
}
27
include_once MODX_MANAGER_PATH . '/media/style/' . $modx->config['manager_theme'] . '/style.php';
28
29
$action = isset($_REQUEST['a']) ? $_REQUEST['a'] : '';
30
$frame = isset($_REQUEST['f']) ? $_REQUEST['f'] : '';
31
$role = isset($_SESSION['mgrRole']) && $_SESSION['mgrRole'] == 1 ? 1 : 0;
32
$docGroups = isset($_SESSION['mgrDocgroups']) && is_array($_SESSION['mgrDocgroups']) ? implode(',', $_SESSION['mgrDocgroups']) : '';
33
34
// set limit sql query
35
$limit = !empty($modx->config['number_of_results']) ? (int) $modx->config['number_of_results'] : 100;
36
37
$json = array();
38
39
if (isset($action)) {
40
    switch ($action) {
41
42
        case '1': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
43
44
            switch ($frame) {
45
                case 'nodes':
46
                    include_once MODX_MANAGER_PATH . '/frames/nodes.php';
47
48
                    break;
49
            }
50
51
            break;
52
        }
53
54
        case '76': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
55
56
            $elements = isset($_REQUEST['elements']) && is_scalar($_REQUEST['elements']) ? htmlentities($_REQUEST['elements']) : '';
57
58
            if ($elements) {
59
                $output = '';
60
                $items = '';
61
                $sql = '';
62
                $a = '';
63
                $filter = !empty($_REQUEST['filter']) && is_scalar($_REQUEST['filter']) ? addcslashes(trim($_REQUEST['filter']), '%*_') : '';
64
                $sqlLike = $filter ? 'WHERE t1.name LIKE "' . $modx->db->escape($filter) . '%"' : '';
65
                $sqlLimit = $sqlLike ? '' : 'LIMIT ' . $limit;
66
67
                switch ($elements) {
68
                    case 'element_templates':
69
                        $a = 16;
70
                        $sqlLike = $filter ? 'WHERE t1.templatename LIKE "' . $modx->db->escape($filter) . '%"' : '';
71
                        $sql = $modx->db->query('SELECT t1.*, t1.templatename AS name
72
                        FROM ' . $modx->getFullTableName('site_templates') . ' AS t1
73
                        ' . $sqlLike . '
74
                        ORDER BY t1.templatename ASC
75
                        ' . $sqlLimit);
76
77
                        if ($modx->hasPermission('new_template')) {
78
                            $output .= '<li><a id="a_19" href="index.php?a=19" target="main"><i class="fa fa-plus"></i>' . $_lang['new_template'] . '</a></li>';
79
                        }
80
81
                        break;
82
83
                    case 'element_tplvars':
84
                        $a = 301;
85
                        $sql = $modx->db->query('SELECT t1.*, IF(t2.templateid,0,1) AS disabled
86
                        FROM ' . $modx->getFullTableName('site_tmplvars') . ' AS t1
87
                        LEFT JOIN ' . $modx->getFullTableName('site_tmplvar_templates') . ' AS t2 ON t1.id=t2.tmplvarid
88
                        ' . $sqlLike . '
89
                        GROUP BY t1.id
90
                        ORDER BY t1.name ASC
91
                        ' . $sqlLimit);
92
93
                        if ($modx->hasPermission('edit_template') && $modx->hasPermission('edit_snippet') && $modx->hasPermission('edit_chunk') && $modx->hasPermission('edit_plugin')) {
94
                            $output .= '<li><a id="a_300" href="index.php?a=300" target="main"><i class="fa fa-plus"></i>' . $_lang['new_tmplvars'] . '</a></li>';
95
                        }
96
97
                        break;
98
99 View Code Duplication
                    case 'element_htmlsnippets':
100
                        $a = 78;
101
                        $sql = $modx->db->query('SELECT t1.*
102
                        FROM ' . $modx->getFullTableName('site_htmlsnippets') . ' AS t1
103
                        ' . $sqlLike . '
104
                        ORDER BY t1.name ASC
105
                        ' . $sqlLimit);
106
107
                        if ($modx->hasPermission('new_chunk')) {
108
                            $output .= '<li><a id="a_77" href="index.php?a=77" target="main"><i class="fa fa-plus"></i>' . $_lang['new_htmlsnippet'] . '</a></li>';
109
                        }
110
111
                        break;
112
113 View Code Duplication
                    case 'element_snippets':
114
                        $a = 22;
115
                        $sql = $modx->db->query('SELECT t1.*
116
                        FROM ' . $modx->getFullTableName('site_snippets') . ' AS t1
117
                        ' . $sqlLike . '
118
                        ORDER BY t1.name ASC
119
                        ' . $sqlLimit);
120
121
                        if ($modx->hasPermission('new_snippet')) {
122
                            $output .= '<li><a id="a_23" href="index.php?a=23" target="main"><i class="fa fa-plus"></i>' . $_lang['new_snippet'] . '</a></li>';
123
                        }
124
125
                        break;
126
127 View Code Duplication
                    case 'element_plugins':
128
                        $a = 102;
129
                        $sql = $modx->db->query('SELECT t1.*
130
                        FROM ' . $modx->getFullTableName('site_plugins') . ' AS t1
131
                        ' . $sqlLike . '
132
                        ORDER BY t1.name ASC
133
                        ' . $sqlLimit);
134
135
                        if ($modx->hasPermission('new_plugin')) {
136
                            $output .= '<li><a id="a_101" href="index.php?a=101" target="main"><i class="fa fa-plus"></i>' . $_lang['new_plugin'] . '</a></li>';
137
                        }
138
139
                        break;
140
                }
141
142
                if ($count = $modx->db->getRecordCount($sql)) {
143
                    if ($count == $limit) {
144
                        $output .= '<li class="item-input"><input type="text" name="filter" class="dropdown-item form-control form-control-sm" autocomplete="off" /></li>';
145
                    }
146
                    while ($row = $modx->db->getRow($sql)) {
147
                        if (($row['disabled'] || $row['locked']) && $role != 1) {
148
                            continue;
149
                        }
150
151
                        $items .= '<li class="item ' . ($row['disabled'] ? 'disabled' : '') . ($row['locked'] ? ' locked' : '') . '"><a id="a_' . $a . '__id_' . $row['id'] . '" href="index.php?a=' . $a . '&id=' . $row['id'] . '" target="main" data-parent-id="a_76__elements_' . $elements . '">' . $row['name'] . ' <small>(' . $row['id'] . ')</small></a></li>' . "\n";
152
                    }
153
                }
154
155
                if (isset($_REQUEST['filter'])) {
156
                    $output = $items;
157
                } else {
158
                    $output .= $items;
159
                }
160
161
                echo $output;
162
            }
163
164
            break;
165
        }
166
167 View Code Duplication
        case '75': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
168
            $a = 12;
169
            $output = '';
170
            $items = '';
171
            $filter = !empty($_REQUEST['filter']) && is_scalar($_REQUEST['filter']) ? addcslashes(trim($_REQUEST['filter']), '\%*_') : '';
172
            $sqlLike = $filter ? 'WHERE t1.username LIKE "' . $modx->db->escape($filter) . '%"' : '';
173
            $sqlLimit = $sqlLike ? '' : 'LIMIT ' . $limit;
174
175
            $sql = $modx->db->query('SELECT t1.*, t1.username AS name, t2.blocked
176
				FROM ' . $modx->getFullTableName('manager_users') . ' AS t1
177
				LEFT JOIN ' . $modx->getFullTableName('user_attributes') . ' AS t2 ON t1.id=t2.internalKey
178
				' . $sqlLike . '
179
				ORDER BY t1.username ASC
180
				' . $sqlLimit);
181
182
            if ($modx->hasPermission('new_user')) {
183
                $output .= '<li><a id="a_11" href="index.php?a=11" target="main"><i class="fa fa-plus"></i>' . $_lang['new_user'] . '</a></li>';
184
            }
185
186
            if ($count = $modx->db->getRecordCount($sql)) {
187
                if ($count == $limit) {
188
                    $output .= '<li class="item-input"><input type="text" name="filter" class="dropdown-item form-control form-control-sm" autocomplete="off" /></li>';
189
                }
190
                while ($row = $modx->db->getRow($sql)) {
191
                    $items .= '<li class="item ' . ($row['blocked'] ? 'disabled' : '') . '"><a id="a_' . $a . '__id_' . $row['id'] . '" href="index.php?a=' . $a . '&id=' . $row['id'] . '" target="main">' . $row['name'] . ' <small>(' . $row['id'] . ')</small></a></li>';
192
                }
193
            }
194
195
            if (isset($_REQUEST['filter'])) {
196
                $output = $items;
197
            } else {
198
                $output .= $items;
199
            }
200
201
            echo $output;
202
203
            break;
204
        }
205
206 View Code Duplication
        case '99': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
207
            $a = 88;
208
            $output = '';
209
            $items = '';
210
            $filter = !empty($_REQUEST['filter']) && is_scalar($_REQUEST['filter']) ? addcslashes(trim($_REQUEST['filter']), '\%*_') : '';
211
            $sqlLike = $filter ? 'WHERE t1.username LIKE "' . $modx->db->escape($filter) . '%"' : '';
212
            $sqlLimit = $sqlLike ? '' : 'LIMIT ' . $limit;
213
214
            $sql = $modx->db->query('SELECT t1.*, t1.username AS name, t2.blocked
215
				FROM ' . $modx->getFullTableName('web_users') . ' AS t1
216
				LEFT JOIN ' . $modx->getFullTableName('web_user_attributes') . ' AS t2 ON t1.id=t2.internalKey
217
				' . $sqlLike . '
218
				ORDER BY t1.username ASC
219
				' . $sqlLimit);
220
221
            if ($modx->hasPermission('new_web_user')) {
222
                $output .= '<li><a id="a_87" href="index.php?a=87" target="main"><i class="fa fa-plus"></i>' . $_lang['new_web_user'] . '</a></li>';
223
            }
224
225
            if ($count = $modx->db->getRecordCount($sql)) {
226
                if ($count == $limit) {
227
                    $output .= '<li class="item-input"><input type="text" name="filter" class="dropdown-item form-control form-control-sm" autocomplete="off" /></li>';
228
                }
229
                while ($row = $modx->db->getRow($sql)) {
230
                    $items .= '<li class="item ' . ($row['blocked'] ? 'disabled' : '') . '"><a id="a_' . $a . '__id_' . $row['id'] . '" href="index.php?a=' . $a . '&id=' . $row['id'] . '" target="main">' . $row['name'] . ' <small>(' . $row['id'] . ')</small></a></li>';
231
                }
232
            }
233
234
            if (isset($_REQUEST['filter'])) {
235
                $output = $items;
236
            } else {
237
                $output .= $items;
238
            }
239
240
            echo $output;
241
242
            break;
243
        }
244
245
        case 'modxTagHelper': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
246
            $name = isset($_REQUEST['name']) && is_scalar($_REQUEST['name']) ? $modx->db->escape($_REQUEST['name']) : false;
247
            $type = isset($_REQUEST['type']) && is_scalar($_REQUEST['type']) ? $modx->db->escape($_REQUEST['type']) : false;
248
            $contextmenu = '';
249
250
            if ($role && $name && $type) {
251
                switch ($type) {
252
                    case 'Snippet':
253 View Code Duplication
                    case 'SnippetNoCache': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
254
255
                        $sql = $modx->db->query('SELECT *
256
						FROM ' . $modx->getFullTableName('site_snippets') . '
257
						WHERE name="' . $name . '"
258
						LIMIT 1');
259
260
                        if ($modx->db->getRecordCount($sql)) {
261
                            $row = $modx->db->getRow($sql);
262
                            $contextmenu = array(
263
                                'header' => array(
264
                                    'innerHTML' => '<i class="fa fa-code"></i> ' . $row['name']
265
                                ),
266
                                'item' => array(
267
                                    'innerHTML' => '<i class="fa fa-pencil-square-o"></i> ' . $_lang['edit'],
268
                                    'url' => "index.php?a=22&id=" . $row['id']
269
                                )
270
                            );
271
                            if (!empty($row['description'])) {
272
                                $contextmenu['seperator'] = '';
273
                                $contextmenu['description'] = array(
274
                                    'innerHTML' => '<i class="fa fa-info"></i> ' . $row['description']
275
                                );
276
                            }
277
                        } else {
278
                            $contextmenu = array(
279
                                'header' => array(
280
                                    'innerHTML' => '<i class="fa fa-code"></i> ' . $name
281
                                ),
282
                                'item' => array(
283
                                    'innerHTML' => '<i class="fa fa-plus"></i> ' . $_lang['new_snippet'],
284
                                    'url' => "index.php?a=23&itemname=" . $name
285
                                )
286
                            );
287
                        }
288
289
                        break;
290
                    }
291 View Code Duplication
                    case 'Chunk' : {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
292
293
                        $sql = $modx->db->query('SELECT *
294
						FROM ' . $modx->getFullTableName('site_htmlsnippets') . '
295
						WHERE name="' . $name . '"
296
						LIMIT 1');
297
298
                        if ($modx->db->getRecordCount($sql)) {
299
                            $row = $modx->db->getRow($sql);
300
                            $contextmenu = array(
301
                                'header' => array(
302
                                    'innerHTML' => '<i class="fa fa-th-large"></i> ' . $row['name']
303
                                ),
304
                                'item' => array(
305
                                    'innerHTML' => '<i class="fa fa-pencil-square-o"></i> ' . $_lang['edit'],
306
                                    'url' => "index.php?a=78&id=" . $row['id']
307
                                )
308
                            );
309
                            if (!empty($row['description'])) {
310
                                $contextmenu['seperator'] = '';
311
                                $contextmenu['description'] = array(
312
                                    'innerHTML' => '<i class="fa fa-info"></i> ' . $row['description']
313
                                );
314
                            }
315
                        } else {
316
                            $contextmenu = array(
317
                                'header' => array(
318
                                    'innerHTML' => '<i class="fa fa-th-large"></i> ' . $name
319
                                ),
320
                                'item' => array(
321
                                    'innerHTML' => '<i class="fa fa-plus"></i> ' . $_lang['new_htmlsnippet'],
322
                                    'url' => "index.php?a=77&itemname=" . $name
323
                                )
324
                            );
325
                        }
326
327
                        break;
328
                    }
329
                    case 'AttributeValue': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
330
                        $sql = $modx->db->query('SELECT *
331
						FROM ' . $modx->getFullTableName('site_htmlsnippets') . '
332
						WHERE name="' . $name . '"
333
						LIMIT 1');
334
335
                        if ($modx->db->getRecordCount($sql)) {
336
                            $row = $modx->db->getRow($sql);
337
                            $contextmenu = array(
338
                                'header' => array(
339
                                    'innerText' => $row['name']
340
                                ),
341
                                'item' => array(
342
                                    'innerHTML' => '<i class="fa fa-pencil-square-o"></i> ' . $_lang['edit'],
343
                                    'url' => "index.php?a=78&id=" . $row['id']
344
                                )
345
                            );
346
                            if (!empty($row['description'])) {
347
                                $contextmenu['seperator'] = '';
348
                                $contextmenu['description'] = array(
349
                                    'innerHTML' => '<i class="fa fa-info"></i> ' . $row['description']
350
                                );
351
                            }
352
                        } else {
353
354
                            $sql = $modx->db->query('SELECT *
355
							FROM ' . $modx->getFullTableName('site_snippets') . '
356
							WHERE name="' . $name . '"
357
							LIMIT 1');
358
359
                            if ($modx->db->getRecordCount($sql)) {
360
                                $row = $modx->db->getRow($sql);
361
                                $contextmenu = array(
362
                                    'header' => array(
363
                                        'innerHTML' => '<i class="fa fa-code"></i> ' . $row['name']
364
                                    ),
365
                                    'item' => array(
366
                                        'innerHTML' => '<i class="fa fa-pencil-square-o"></i> ' . $_lang['edit'],
367
                                        'url' => "index.php?a=22&id=" . $row['id']
368
                                    )
369
                                );
370
                                if (!empty($row['description'])) {
371
                                    $contextmenu['seperator'] = '';
372
                                    $contextmenu['description'] = array(
373
                                        'innerHTML' => '<i class="fa fa-info"></i> ' . $row['description']
374
                                    );
375
                                }
376
                            } else {
377
                                $contextmenu = array(
378
                                    'header' => array(
379
                                        'innerHTML' => '<i class="fa fa-code"></i> ' . $name
380
                                    ),
381
                                    'item' => array(
382
                                        'innerHTML' => '<i class="fa fa-plus"></i> ' . $_lang['new_htmlsnippet'],
383
                                        'url' => "index.php?a=77&itemname=" . $name
384
                                    ),
385
                                    'item2' => array(
386
                                        'innerHTML' => '<i class="fa fa-plus"></i> ' . $_lang['new_snippet'],
387
                                        'url' => "index.php?a=23&itemname=" . $name
388
                                    )
389
                                );
390
                            }
391
                        }
392
393
                        break;
394
                    }
395
                    case 'Placeholder' :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
396
                    case 'Tv' : {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
397
                        $default_field = array(
398
                            'id',
399
                            'type',
400
                            'contentType',
401
                            'pagetitle',
402
                            'longtitle',
403
                            'description',
404
                            'alias',
405
                            'link_attributes',
406
                            'published',
407
                            'pub_date',
408
                            'unpub_date',
409
                            'parent',
410
                            'isfolder',
411
                            'introtext',
412
                            'content',
413
                            'richtext',
414
                            'template',
415
                            'menuindex',
416
                            'searchable',
417
                            'cacheable',
418
                            'createdon',
419
                            'createdby',
420
                            'editedon',
421
                            'editedby',
422
                            'deleted',
423
                            'deletedon',
424
                            'deletedby',
425
                            'publishedon',
426
                            'publishedby',
427
                            'menutitle',
428
                            'donthit',
429
                            'haskeywords',
430
                            'hasmetatags',
431
                            'privateweb',
432
                            'privatemgr',
433
                            'content_dispo',
434
                            'hidemenu',
435
                            'alias_visible'
436
                        );
437
438
                        if (in_array($name, $default_field)) {
439
                            return;
440
                        }
441
442
                        $sql = $modx->db->query('SELECT *
443
						FROM ' . $modx->getFullTableName('site_tmplvars') . '
444
						WHERE name="' . $name . '"
445
						LIMIT 1');
446
447
                        if ($modx->db->getRecordCount($sql)) {
448
                            $row = $modx->db->getRow($sql);
449
                            $contextmenu = array(
450
                                'header' => array(
451
                                    'innerHTML' => '<i class="fa fa-list-alt"></i> ' . $row['name']
452
                                ),
453
                                'item' => array(
454
                                    'innerHTML' => '<i class="fa fa-pencil-square-o"></i> ' . $_lang['edit'],
455
                                    'url' => "index.php?a=301&id=" . $row['id']
456
                                )
457
                            );
458
                            if (!empty($row['description'])) {
459
                                $contextmenu['seperator'] = '';
460
                                $contextmenu['description'] = array(
461
                                    'innerHTML' => '<i class="fa fa-info"></i> ' . $row['description']
462
                                );
463
                            }
464
                        } else {
465
                            $contextmenu = array(
466
                                'header' => array(
467
                                    'innerHTML' => '<i class="fa fa-list-alt"></i> ' . $name
468
                                ),
469
                                'item' => array(
470
                                    'innerHTML' => '<i class="fa fa-plus"></i> ' . $_lang['new_tmplvars'],
471
                                    'url' => "index.php?a=300&itemname=" . $name
472
                                )
473
                            );
474
                        }
475
476
                        break;
477
                    }
478
                }
479
                echo json_encode($contextmenu, JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE);
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
480
                break;
481
            }
482
483
            break;
484
        }
485
486
        case 'movedocument' : {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
487
            if ($modx->hasPermission('new_document') && $modx->hasPermission('edit_document') && $modx->hasPermission('save_document')) {
488
                $id = !empty($_REQUEST['id']) ? (int)$_REQUEST['id'] : '';
489
                $parent = isset($_REQUEST['parent']) ? (int)$_REQUEST['parent'] : 0;
490
                $menuindex = isset($_REQUEST['menuindex']) && is_scalar($_REQUEST['menuindex']) ? $_REQUEST['menuindex'] : 0;
491
492
                // set parent
493
                if ($id && $parent >= 0) {
494
495
                    // find older parent
496
                    $parentOld = $modx->db->getValue($modx->db->select('parent', $modx->getFullTableName('site_content'), 'id=' . $id));
497
498
                    $eventOut = $modx->invokeEvent('onBeforeMoveDocument', [
499
                        'id_document' => $id,
500
                        'old_parent'  => $parentOld,
501
                        'new_parent'  => $parent,
502
                    ]);
503
504
                    if (is_array($eventOut) && count($eventOut) > 0) {
505
                        $eventParent = array_pop($eventOut);
506
507
                        if ($eventParent == $parentOld) {
508
                            $json['errors'] = $_lang['error_movedocument2'];
509
                        } else {
510
                            $parent = $eventParent;
511
                        }
512
                    }
513
514
                    if (empty($json['errors'])) {
515
                        // check privileges user for move docs
516
                        if (!empty($modx->config['tree_show_protected']) && $role != 1) {
517
                            $sql = $modx->db->select('*', $modx->getFullTableName('document_groups'), 'document IN(' . $id . ',' . $parent . ',' . $parentOld . ')');
518
                            if ($modx->db->getRecordCount($sql)) {
519
                                $document_groups = array();
520
                                while ($row = $modx->db->getRow($sql)) {
521
                                    $document_groups[$row['document']]['groups'][] = $row['document_group'];
522
                                }
523
                                foreach ($document_groups as $key => $value) {
524
                                    if (($key == $parent || $key == $parentOld || $key == $id) && !in_array($role, $value['groups'])) {
525
                                        $json['errors'] = $_lang["error_no_privileges"];
526
                                    }
527
                                }
528
                                if ($json['errors']) {
529
                                    header('content-type: application/json');
530
                                    echo json_encode($json, JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE);
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
531
                                    break;
532
                                }
533
                            }
534
                        }
535
536
                        if ($parent == 0 && $parent != $parentOld && !$modx->config['udperms_allowroot'] && $role != 1) {
537
                            $json['errors'] = $_lang["error_no_privileges"];
538
                        } else {
539
                            // set new parent
540
                            $modx->db->update(array(
541
                                'parent' => $parent
542
                            ), $modx->getFullTableName('site_content'), 'id=' . $id);
543
                            // set parent isfolder = 1
544
                            $modx->db->update(array(
545
                                'isfolder' => 1
546
                            ), $modx->getFullTableName('site_content'), 'id=' . $parent);
547
548
                            if ($parent != $parentOld) {
549
                                // check children docs and set parent isfolder
550
                                if ($modx->db->getRecordCount($modx->db->select('id', $modx->getFullTableName('site_content'), 'parent=' . $parentOld))) {
551
                                    $modx->db->update(array(
552
                                        'isfolder' => 1
553
                                    ), $modx->getFullTableName('site_content'), 'id=' . $parentOld);
554
                                } else {
555
                                    $modx->db->update(array(
556
                                        'isfolder' => 0
557
                                    ), $modx->getFullTableName('site_content'), 'id=' . $parentOld);
558
                                }
559
                            }
560
561
                            // set menuindex
562
                            if (!empty($menuindex)) {
563
                                $menuindex = explode(',', $menuindex);
564
                                foreach ($menuindex as $key => $value) {
565
                                    $modx->db->query('UPDATE ' . $modx->getFullTableName('site_content') . ' SET menuindex=' . $key . ' WHERE id=' . $value);
566
                                }
567
                            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
568
                                // TODO: max(*) menuindex
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
569
                            }
570
571
                            if (!$json['errors']) {
572
                                $json['success'] = $_lang["actioncomplete"];
573
574
                                $modx->invokeEvent('onAfterMoveDocument', [
575
                                    'id_document' => $id,
576
                                    'old_parent'  => $parentOld,
577
                                    'new_parent'  => $parent,
578
                                ]);
579
                            }
580
                        }
581
                    }
582
                }
583
            } else {
584
                $json['errors'] = $_lang["error_no_privileges"];
585
            }
586
587
            $modx->jsonResponse($json);
588
589
            break;
590
        }
591
592
        case 'getLockedElements': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
593
            $type = isset($_REQUEST['type']) ? (int)$_REQUEST['type'] : 0;
594
            $id = isset($_REQUEST['id']) ? (int)$_REQUEST['id'] : 0;
595
596
            $output = !!$modx->elementIsLocked($type, $id, true);
597
598
            if (!$output) {
599
                $docgrp = (isset($_SESSION['mgrDocgroups']) && is_array($_SESSION['mgrDocgroups'])) ? implode(',', $_SESSION['mgrDocgroups']) : '';
600
                $docgrp_cond = $docgrp ? ' OR dg.document_group IN (' . $docgrp . ')' : '';
601
                $sql = '
602
                    SELECT MAX(IF(1=' . $role . ' OR sc.privatemgr=0' . $docgrp_cond . ', 0, 1)) AS locked
603
                    FROM ' . $modx->getFullTableName('site_content') . ' AS sc 
604
                    LEFT JOIN ' . $modx->getFullTableName('document_groups') . ' dg ON dg.document=sc.id
605
                    WHERE sc.id=' . $id . ' GROUP BY sc.id';
606
                $sql = $modx->db->query($sql);
607
                if ($modx->db->getRecordCount($sql)) {
608
                    $row = $modx->db->getRow($sql);
609
                    $output = !!$row['locked'];
610
                }
611
            }
612
            
613
            echo $output;
614
615
            break;
616
        }
617
        
618
        /* Ajax-Button Actions */
619
	    case 'publish.res': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
620
		    $published = getDocumentTv('published');
621
		    $state = $published == 0 ? 1 : 0;
622
		    if($state) {
623
			    require_once('../../../processors/publish_content.processor.php');
624
		    } else {
625
			    require_once('../../../processors/unpublish_content.processor.php');
626
		    }
627
		    $json['result'] = $state == 1 ? 'publish' : 'unpublish';
628
			break;
629
		}
630
	    case 'delete.res': {
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
631
		    $deleted = getDocumentTv('deleted');
632
		    $state = $deleted == 0 ? 1 : 0;
633
		    if($state) {
634
			    require_once('../../../processors/delete_content.processor.php');
635
		    } else {
636
			    require_once('../../../processors/undelete_content.processor.php');
637
		    }
638
		    $json['result'] = $state == 1 ? 'delete' : 'undelete';
639
			break;
640
		}
641
    }
642
    if(!empty($json)) $modx->jsonResponse($json);
643
}
644
645
function getDocumentTv($tv) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $tv. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style introduced by
getDocumentTv uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
646
	global $modx, $_lang;
1 ignored issue
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
647
	$modx->setAjaxMode(true);
648
	$id = isset($_REQUEST['id'])? intval($_REQUEST['id']) : 0;
649
	if($id==0) $modx->webAlertAndQuit($_lang["error_no_id"]);
650
	return $modx->db->getValue($modx->db->select($tv, $modx->getFullTableName('site_content'), 'id=' . $id));
651
}