Completed
Push — master ( ad6325...4bb5f3 )
by Michael
02:50
created

pages.php ➔ showPages()   F

Complexity

Conditions 16
Paths 6400

Size

Total Lines 87
Code Lines 61

Duplication

Lines 5
Ratio 5.75 %

Importance

Changes 7
Bugs 1 Features 1
Metric Value
cc 16
eloc 61
c 7
b 1
f 1
nc 6400
nop 1
dl 5
loc 87
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * admin/pages.php - manage wiki page revision
4
 *
5
 * @copyright  Copyright © 2013 geekwright, LLC. All rights reserved.
6
 * @license    gwiki/docs/license.txt  GNU General Public License (GPL)
7
 * @since      1.0
8
 * @author     Richard Griffith <[email protected]>
9
 * @package    gwiki
10
 */
11
include __DIR__ . '/header.php';
12
13
include_once dirname(__DIR__) . '/include/functions.php';
14
include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
15
16
echo $moduleAdmin->addNavigation(basename(__FILE__));
17
18
/**
19
 * @param $url
20
 * @param $params
21
 */
22
function post_clean_request($url, $params)
23
{
24
    foreach ($params as $key => &$val) {
25
        if (is_array($val)) {
26
            $val = implode(',', $val);
27
        }
28
        $post_params[] = $key . '=' . urlencode($val);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$post_params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $post_params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
29
    }
30
    $post_string = implode('&', $post_params);
0 ignored issues
show
Bug introduced by
The variable $post_params does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
31
32
    $parts = parse_url($url);
33
34
    $fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30);
35
36
    $out = 'POST ' . $parts['path'] . " HTTP/1.1\r\n";
37
    $out .= 'Host: ' . $parts['host'] . "\r\n";
38
    $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
39
    $out .= 'Content-Length: ' . strlen($post_string) . "\r\n";
40
    $out .= "Connection: Close\r\n\r\n";
41
    if (isset($post_string)) {
42
        $out .= $post_string;
43
    }
44
45
    fwrite($fp, $out);
46
    fclose($fp);
47
}
48
49
/**
50
 * @param null $message
51
 */
52
function showPages($message = null)
53
{
54
    global $xoopsDB;
55
    echo <<<EOT
56
<style>
57
div.pagination.default {display:inline;}
58
form {display:inline;}
59
</style>
60
EOT;
61
    $total = 0;
62
    $limit = 10;
63
    $start = 0;
64
    $like  = '';
65
    if (!empty($_GET['start'])) {
66
        $start = (int)$_GET['start'];
67
    }
68
    if (!empty($_GET['like'])) {
69
        $like = cleaner($_GET['like']);
70
    }
71
72
    $sql = 'SELECT COUNT(*) FROM ' . $xoopsDB->prefix('gwiki_pageids');
73
    if (!empty($like)) {
74
        $sql .= " WHERE keyword LIKE '{$like}%' ";
75
    }
76
    $result = $xoopsDB->query($sql);
77
    if ($result) {
78
        $myrow = $xoopsDB->fetchRow($result);
79
        $total = $myrow[0];
80
    }
81
82
    echo '<form method="get"><b>' . _AD_GWIKI_KEYWORD_FILTER . '</b><input type="text" name="like"><input type="submit"></form><br>';
83
    adminTableStart(_AD_GWIKI_ADMINTITLE, 4);
84
    if (!empty($message)) {
85
        echo '<tr><td colspan="4" align="center"><br><b>' . $message . '</b><br><br></td></tr>';
86
    }
87
    echo '<tr><th width="15%">' . _AD_GWIKI_KEYWORD . '</th><th>' . _MD_GWIKI_TITLE . '</th><th width="5%">' . _AD_GWIKI_REVISIONS . '</th><th width="30%">' . _AD_GWIKI_ACTION . '</th></tr>';
88
    $sqlwhere = '';
89
    if (!empty($like)) {
90
        $sqlwhere = " WHERE t1.keyword LIKE '{$like}%' ";
91
    }
92
    $sql    = 'SELECT t1.keyword, COUNT(*), t2.title, t2.admin_lock, t2.active FROM ' . $xoopsDB->prefix('gwiki_pages') . ' t1 ' . ' LEFT JOIN ' . $xoopsDB->prefix('gwiki_pages')
93
              . ' t2 on t1.keyword = t2.keyword and t2.active = 1 ' . $sqlwhere . ' GROUP BY keyword ';
94
    $result = $xoopsDB->query($sql, $limit, $start);
95
96
    for ($i = 0, $iMax = $xoopsDB->getRowsNum($result); $i < $iMax; ++$i) {
97
        list($page, $revs, $title, $lock, $active) = $xoopsDB->fetchRow($result);
98
        if (empty($active)) {
99
            $title = _AD_GWIKI_NO_ACTIVE_PAGE;
100
        }
101
        //if(empty($title)) $title=_AD_GWIKI_NO_ACTIVE_PAGE;
102
        if ($lock) {
103
            $lockaction = ' | <a href="pages.php?page=' . $page . '&op=unlock">' . _AD_GWIKI_UNLOCK . '</a>';
104
        } else {
105
            $lockaction = ' | <a href="pages.php?page=' . $page . '&op=lock">' . _AD_GWIKI_LOCK . '</a>';
106
        }
107
        echo '<tr class="' . (($i % 2) ? 'even' : 'odd') . '"><td><a href="pages.php?page=' . $page . '&op=history">' . $page . '</a></td>' . '<td>' . htmlspecialchars($title, ENT_QUOTES) . '</td>'
108
             . '<td>' . $revs . '</td>' . '<td><a href="pages.php?page=' . $page . '&op=display">' . _AD_GWIKI_VIEW . '</a> | <a href="pages.php?page=' . $page . '&op=history">' . _AD_GWIKI_HISTORY
109
             . '</a>' . $lockaction . ' | <a href="pages.php?page=' . $page . '&op=delete">' . _DELETE . '</a></td></tr>';
110
    }
111
    if ($i === 0) {
112
        echo '<tr class="odd"><td colspan="3">' . _AD_GWIKI_EMPTYWIKI . '</td></tr>';
113
    }
114
115
    $endarray[_AD_GWIKI_CLEANUPDB] = 'pages.php?op=clean';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$endarray was never initialized. Although not strictly required by PHP, it is generally a good practice to add $endarray = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
116
    $endarray[_AD_GWIKI_PARTITION] = 'pages.php?op=partition';
117
    $endarray[_AD_GWIKI_ADD_HELP]  = 'pages.php?op=addhelp';
118
    // set up pagenav
119
    $pager = '';
120
    if ($total > $limit) {
121
        include_once XOOPS_ROOT_PATH . '/class/pagenav.php';
122
        $likenav = '';
123
        if (!empty($like)) {
124
            $likenav = "like={$like}";
125
        }
126
        $nav = new xoopsPageNav($total, $limit, $start, 'start', $likenav);
127 View Code Duplication
        if ((int)($total / $limit) < 5) {
0 ignored issues
show
Duplication introduced by
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...
128
            $pager = $nav->renderNav();
129
        } else {
130
            $pager = _AD_GWIKI_PAGENAV . $nav->renderSelect(false);
131
        }
132
    }
133
    if (!empty($pager)) {
134
        $endarray['!PREFORMATTED!'] = $pager;
135
    }
136
137
    adminTableEnd($endarray);
138
}
139
140
/**
141
 * @param $page
142
 */
143
function showHistory($page)
144
{
145
    global $xoopsDB, $xoopsModuleConfig, $wikiPage;
146
147
    allowRestoration($page);
148
149
    adminTableStart(_AD_GWIKI_ADMINTITLE . ' : ' . $page, 4);
150
    echo '<tr><th>' . _MD_GWIKI_TITLE . '</th><th width="20%">' . _AD_GWIKI_MODIFIED . '</th><th width="10%">' . _AD_GWIKI_AUTHOR . '</th><th width="30%">' . _AD_GWIKI_ACTION . '</th></tr>';
151
152
    $sql    = 'SELECT gwiki_id, title, body, lastmodified, uid, active, FROM_UNIXTIME(lastmodified) FROM ' . $xoopsDB->prefix('gwiki_pages')
153
              . " WHERE keyword='{$page}' ORDER BY active DESC, lastmodified DESC";
154
    $result = $xoopsDB->query($sql);
155
156
    for ($i = 0, $iMax = $xoopsDB->getRowsNum($result); $i < $iMax; ++$i) {
157
        list($id, $title, $body, $lastmodified, $uid, $active, $modified) = $xoopsDB->fetchRow($result);
0 ignored issues
show
Unused Code introduced by
The assignment to $body is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $lastmodified is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
158
159
        echo '<tr class="' . (($i % 2) ? 'even' : 'odd') . '"><td><a href="pages.php?page=' . $page . '&op=display&id=' . $id . '">' . htmlspecialchars($title, ENT_QUOTES) . '</a></td>';
160
        echo '<td>' . $modified . ($active ? '*' : '') . '</td>';
161
        echo '<td>' . $wikiPage->getUserName($uid) . '</td>';
162
        echo '<td><a href="pages.php?page=' . $page . '&op=display&id=' . $id . '">' . _AD_GWIKI_VIEW . '</a> | <a href="javascript:restoreRevision(\'' . $id . '\');">' . _AD_GWIKI_RESTORE . '</a> ';
163
        echo ' | <a href="pages.php?page=' . $page . '&op=fix&id=' . $id . '">' . _AD_GWIKI_FIX . '</a> | <a href="pages.php?page=' . $page . '&op=tool&id=' . $id . '">' . _AD_GWIKI_PAGETOOLS
164
             . '</a>';
165
        echo ' | <a href="../edit.php?page=' . $page . '&id=' . $id . '">' . _EDIT . '</a> </td></tr>';
166
    }
167
    if ($i === 0) {
168
        echo '<tr class="odd"><td colspan="4">' . _MD_GWIKI_PAGENOTFOUND . '</td></tr>';
169
    }
170
171
    adminTableEnd(array(_BACK => 'pages.php?op=manage'));
172
}
173
174
/**
175
 * @param $page
176
 * @param $id
177
 */
178
function showPage($page, $id)
179
{
180
    global $xoopsDB, $xoopsModuleConfig, $wikiPage, $xoTheme;
181
182
    $dir = basename(dirname(__DIR__));
183
    if (is_object($xoTheme)) {
184
        $xoTheme->addStylesheet(XOOPS_URL . '/modules/' . $dir . '/assets/css/module.css');
185
    }
186
187
    //    xoops_cp_header();
188
    allowRestoration($page);
189
190
    $wikiPage->setWikiLinkURL('pages.php?page=%s&op=history');
191
    $wikiPage->getPage($page, $id);
192
    if (empty($id)) {
193
        $id = $wikiPage->gwiki_id;
194
    }
195
196
    adminTableStart(_AD_GWIKI_SHOWPAGE, 1);
197
    echo '<tr><td width="100%" >';
198
    echo '<div style="width: 94%; margin: 2em;">';
199
    echo '<p style="padding-bottom: 2px; border-bottom: 1px solid #000000;">' . _MD_GWIKI_PAGE . ": <strong>{$page}</strong> - " . _MD_GWIKI_LASTMODIFIED . ' <i>'
200
         . date($xoopsModuleConfig['date_format'], $wikiPage->lastmodified) . '</i> ' . _MD_GWIKI_BY . ' <i>' . $wikiPage->getUserName($wikiPage->uid) . '</i></p>';
201
202
    echo '<div id="wikipage"><h1 class="wikititle" id="toc0">' . htmlspecialchars($wikiPage->title) . '</h1>';
203
    echo $wikiPage->renderPage();
204
    echo '</div>';
205
206
    echo '</div>';
207
    echo '</td></tr>';
208
    adminTableEnd(array(
209
                      _BACK               => "pages.php?page={$page}&op=history",
210
                      _AD_GWIKI_RESTORE   => "javascript:restoreRevision('{$id}');",
211
                      _AD_GWIKI_PAGETOOLS => "pages.php?page={$page}&op=tool&id={$id}",
212
                      _AD_GWIKI_FIX       => "pages.php?page={$page}&op=fix&id={$id}"
213
                  ));
214
}
215
216
/**
217
 * @param $page
218
 * @param $id
219
 */
220
function showPageTool($page, $id)
221
{
222
    global $xoopsDB, $xoopsModuleConfig, $wikiPage, $xoTheme;
223
224
    $dir = basename(dirname(__DIR__));
225
    if (is_object($xoTheme)) {
226
        $xoTheme->addStylesheet(XOOPS_URL . '/modules/' . $dir . '/assets/css/module.css');
227
    }
228
229
    //    xoops_cp_header();
230
    allowRestoration($page);
231
232
    $wikiPage->setWikiLinkURL("javascript:alert('%s');");
233
    $wikiPage->getPage($page, $id);
234
235
    $form = new XoopsThemeForm(_AD_GWIKI_PAGETOOLS . ": {$page}", 'gwikiform', "pages.php?page={$page}");
236
    $form->addElement(new XoopsFormSelectUser('user', 'uid', true, $wikiPage->uid));
237
    $form->addElement(new XoopsFormDateTime(_MD_GWIKI_LASTMODIFIED, 'lastmodified', $size = 15, $wikiPage->lastmodified));
238
    $form->addElement(new XoopsFormHidden('op', 'toolupdate'));
239
    $form->addElement(new XoopsFormHidden('page', $page));
240
    $form->addElement(new XoopsFormHidden('id', $id));
241
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
242
    //$form->addElement(new XoopsFormText(_MD_GWIKI_TITLE, "title", 40, 250, $title));
243
    //$form->addElement(new XoopsFormTextArea(_MD_GWIKI_BODY, 'body', $body, 20, 80));
244
    //$var_name = strtotime($var_name['date']) + $var_name['time'];
245
246
    adminTableStart(_AD_GWIKI_PAGETOOLS, 1);
247
    echo '<tr><td width="100%" >';
248
    echo '<div style="width: 94%; margin: 2em;">';
249
    echo '<p style="padding-bottom: 2px; border-bottom: 1px solid #000000;">' . _MD_GWIKI_PAGE . ": <strong>{$page}</strong> - " . _MD_GWIKI_LASTMODIFIED . ' <i>'
250
         . date($xoopsModuleConfig['date_format'], $wikiPage->lastmodified) . '</i> ' . _MD_GWIKI_BY . ' <i>' . $wikiPage->getUserName($wikiPage->uid) . '</i></p>';
251
    echo $form->render();
252
    echo '<br><div id="wikipage" style="height: 120px; overflow: auto;" ><h1 class="wikititle" id="toc0">' . htmlspecialchars($wikiPage->title) . '</h1>';
253
    echo $wikiPage->renderPage();
254
    echo '</div>';
255
256
    echo '</div>';
257
    echo '</td></tr>';
258
    adminTableEnd(array(
259
                      _BACK             => "pages.php?page={$page}&op=history",
260
                      _AD_GWIKI_RESTORE => "javascript:restoreRevision('{$id}');",
261
                      _AD_GWIKI_FIX     => "pages.php?page={$page}&op=fix&id={$id}"
262
                  ));
263
}
264
265
/**
266
 * @param $page
267
 * @param $id
268
 *
269
 * @return mixed
270
 */
271
function pageToolUpdate($page, $id)
272
{
273
    global $xoopsDB;
274
275
    if (isset($_POST['uid'])) {
276
        $uid = (int)$_POST['uid'];
277
    }
278
    if (isset($_POST['lastmodified'])) {
279
        $modified = $_POST['lastmodified'];
280
    }
281
    if (empty($uid) || empty($modified)) {
282
        return false;
283
    }
284
    $lastmodified = strtotime($modified['date']) + $modified['time'];
285
    //print_r($modified);
286
    $sql    = 'UPDATE ' . $xoopsDB->prefix('gwiki_pages') . " SET uid = {$uid}, lastmodified = {$lastmodified}  WHERE keyword='{$page}' AND gwiki_id='{$id}'";
287
    $result = $xoopsDB->query($sql);
288
289
    return $result;
290
}
291
292
/**
293
 * @param        $action
294
 * @param string $keyword
295
 * @param        $id
296
 */
297
function confirmAction($action, $keyword = '', $id = -1)
298
{
299
    adminTableStart(_AD_GWIKI_CONFIRM, 1);
300
    echo '<tr><td width="100%" >';
301
    echo '<div class="confirmMsg">';
302
    echo '<form method="post" action="pages.php">';
303
304
    switch ($action) {
305
        case 'clean':
306
            echo '<input type="hidden" name="op" value="cleanit" />';
307
            $confMsg = _AD_GWIKI_CONFIRM_CLEAN;
308
            break;
309
        case 'delete':
310
            echo '<input type="hidden" name="page" value="' . $keyword . '" />';
311
            echo '<input type="hidden" id="op" name="op" value="deleteit" />';
312
            $confMsg = sprintf(_AD_GWIKI_CONFIRM_DEL, $keyword);
313
            break;
314
        case 'fix':
315
            echo '<input type="hidden" name="page" value="' . $keyword . '" />';
316
            echo '<input type="hidden" id="id" name="id" value="' . $id . '" />
317
                <input type="hidden" id="op" name="op" value="fixit" />';
318
            $confMsg = sprintf(_AD_GWIKI_CONFIRM_FIX, $keyword);
319
            break;
320
        case 'lock':
321
            echo '<input type="hidden" name="page" value="' . $keyword . '" />';
322
            echo '<input type="hidden" id="op" name="op" value="lockit" />';
323
            $confMsg = sprintf(_AD_GWIKI_CONFIRM_LOCK, $keyword);
324
            break;
325
        case 'unlock':
326
            echo '<input type="hidden" name="page" value="' . $keyword . '" />';
327
            echo '<input type="hidden" id="op" name="op" value="unlockit" />';
328
            $confMsg = sprintf(_AD_GWIKI_CONFIRM_UNLOCK, $keyword);
329
            break;
330
        case 'partition':
331
            //          echo '<input type="hidden" name="page" value="'.$keyword.'" />';
332
            echo '<input type="hidden" id="op" name="op" value="partitionit" />';
333
            $confMsg = _AD_GWIKI_CONFIRM_PARTITION;
334
            break;
335
        case 'addhelp':
336
            //          echo '<input type="hidden" name="page" value="'.$keyword.'" />';
337
            echo '<input type="hidden" id="op" name="op" value="addhelpit" />';
338
            $confMsg = _AD_GWIKI_CONFIRM_ADD_HELP;
339
            break;
340
    }
341
342
    echo '<p align="center">' . $confMsg . '<br><br>
0 ignored issues
show
Bug introduced by
The variable $confMsg does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
343
        <input type="submit" value="' . _YES . '">
344
        <input type="button" onclick="history.back();" value="' . _NO . '"></p></form></div>';
345
    echo '</td></tr>';
346
    adminTableEnd(array(_BACK => 'pages.php?op=manage'));
347
}
348
349
/**
350
 * @param $page
351
 * @param $id
352
 *
353
 * @return mixed
354
 */
355
function getRevision($page, $id)
356
{
357
    global $xoopsDB;
358
359
    $sql    = 'SELECT title, body, lastmodified, uid FROM ' . $xoopsDB->prefix('gwiki_pages') . " WHERE gwiki_id='{$id}' AND keyword='{$page}'";
360
    $result = $xoopsDB->query($sql);
361
362
    return $xoopsDB->fetchRow($result);
363
}
364
365
/**
366
 * @param $page
367
 * @param $id
368
 *
369
 * @return mixed
370
 */
371
function fixRevision($page, $id)
372
{
373
    global $xoopsDB, $wikiPage;
374
375
    $result = $wikiPage->setRevision($page, $id);
376
    if ($result) {
377
        $sql    = 'DELETE FROM ' . $xoopsDB->prefix('gwiki_pages') . " WHERE keyword='{$page}' AND active=0 ";
378
        $result = $xoopsDB->query($sql);
379
    }
380
381
    return $result;
382
}
383
384
/**
385
 * @return bool
386
 */
387
function checkForPartitions()
388
{
389
    global $xoopsDB;
390
391
    $sql        = 'SELECT PARTITION_NAME FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_SCHEMA = \'' . XOOPS_DB_NAME . '\' AND TABLE_NAME  = \'' . $xoopsDB->prefix('gwiki_pages') . '\'';
392
    $result     = $xoopsDB->query($sql);
393
    $partitions = $xoopsDB->getRowsNum($result);
394
395
    return $partitions > 1;
396
}
397
398
/**
399
 * @return string
400
 */
401
function createPartitions()
402
{
403
    global $xoopsDB;
404
405
    if (checkForPartitions()) {
406
        $message = _AD_GWIKI_PARTITION_ALREADY;
407
    } else {
408
        $tablename = $xoopsDB->prefix('gwiki_pages');
409
        $sql       = 'ALTER TABLE ' . $tablename . ' PARTITION BY LIST (active) ';
410
        $sql .= '(PARTITION ' . $tablename . '_inactive VALUES IN (0), ';
411
        $sql .= ' PARTITION ' . $tablename . '_active VALUES IN (1) )';
412
        $result  = $xoopsDB->query($sql);
413
        $message = _AD_GWIKI_PARTITION_FAILED;
414
        if ($result) {
415
            $message = _AD_GWIKI_PARTITION_OK;
416
        }
417
    }
418
419
    return $message;
420
}
421
422
/**
423
 * @return string
424
 */
425
function createHelpPages()
426
{
427
    global $xoopsDB;
428
429
    $result  = $xoopsDB->queryFromFile(__DIR__ . '/helppages.sql');
430
    $message = _AD_GWIKI_ADD_HELP_FAILED;
431
    if ($result) {
432
        $message = _AD_GWIKI_ADD_HELP_OK;
433
    }
434
435
    return $message;
436
}
437
438
/**
439
 * @param $page
440
 */
441
function allowRestoration($page)
442
{
443
    echo '<script type="text/javascript">
444
    <!--
445
        function restoreRevision(id)
446
        {
447
            document.restore.id.value = id;
448
            document.restore.submit();
449
        }
450
    // -->
451
    </script>
452
    <form id="restore" name="restore" action="pages.php" method="post">
453
    <input type="hidden" id="op" name="op" value="restore" />
454
    <input type="hidden" id="page" name="page" value="' . $page . '" />
455
    <input type="hidden" id="id" name="id" value="" />
456
    </form>';
457
}
458
459
// page, op, id
460
$page = isset($_GET['page']) ? cleaner($_GET['page']) : '';
461
//$page = makeKeyWord((isset($_GET['page']))?cleaner($_GET['page']):"");
462
$op = isset($_GET['op']) ? cleaner($_GET['op']) : '';
463
464
// $_POST variables we use
465
if (isset($_POST['op'])) {
466
    $op = cleaner($_POST['op']);
467
}
468
if (isset($_POST['page'])) {
469
    $page = cleaner($_POST['page']);
470
}
471
if (isset($_POST['id'])) {
472
    $id = (int)$_POST['id'];
473
}
474
//if(isset($_POST[''])) $ = (int)($_POST['']);
475
476
switch ($op) {
477
    case 'history':
478
        showHistory($page);
479
        break;
480
481
    case 'display':
482
        $id = null;
483
        if (!empty($_GET['id'])) {
484
            $id = (int)$_GET['id'];
485
        }
486
        showPage($page, $id);
487
        break;
488
489 View Code Duplication
    case 'restore':
0 ignored issues
show
Duplication introduced by
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...
490
        $success = $wikiPage->setRevision($page, $id);
491
        redirect_header('pages.php?page=' . $page . '&op=history', 2, $success ? _MD_GWIKI_DBUPDATED : _MD_GWIKI_ERRORINSERT);
492
        break;
493
494
    case 'fix':
495
        confirmAction('fix', $page, (int)$_GET['id']);
496
        break;
497
498 View Code Duplication
    case 'fixit':
0 ignored issues
show
Duplication introduced by
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...
499
        $success = fixRevision($page, $id);
500
        redirect_header('pages.php?page=' . $page . '&op=history', 2, $success ? _MD_GWIKI_DBUPDATED : _MD_GWIKI_ERRORINSERT);
501
        break;
502
503
    case 'tool':
504
        showPageTool($page, (int)$_GET['id']);
505
        break;
506
507
    case 'toolupdate':
508
        $success = pageToolUpdate($page, $id);
509
        $message = $success ? _MD_GWIKI_DBUPDATED : _MD_GWIKI_ERRORINSERT;
510
        $op      = '';
511
        showPages($message);
512
        break;
513
514
    case 'delete':
515
        confirmAction('delete', $page);
516
        break;
517
518 View Code Duplication
    case 'deleteit':
0 ignored issues
show
Duplication introduced by
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...
519
        //  mark all versions inactive -- these will disappear as they age and the database is cleaned
520
        $sql = 'UPDATE ' . $xoopsDB->prefix('gwiki_pages') . " SET active = 0 WHERE keyword='{$page}' ";
521
522
        $success = $xoopsDB->query($sql);
523
        redirect_header('pages.php?op=manage', 2, $success ? _MD_GWIKI_DBUPDATED : _MD_GWIKI_ERRORINSERT);
524
        break;
525
526
    case 'clean':
527
        confirmAction('clean');
528
        break;
529
530
    case 'cleanit':
531
        // delete inactive pages older than config option retain_days
532
        $retaindays = (int)$xoopsModuleConfig['retain_days'];
533
        if ($retaindays > 0) {
534
            $dir    = basename(dirname(__DIR__));
535
            $url    = XOOPS_URL . '/modules/' . $dir . '/cleanit.php';
536
            $params = array('check' => $retaindays);
537
            post_clean_request($url, $params);
538
            $message = _MD_GWIKI_CLEAN_STARTED;
539
        } else {
540
            $message = _MD_GWIKI_CLEAN_DISABLED;
541
        }
542
        $op = '';
543
        showPages($message);
544
        break;
545
546
    case 'lock':
547
        confirmAction('lock', $page);
548
        break;
549
550 View Code Duplication
    case 'lockit':
0 ignored issues
show
Duplication introduced by
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...
551
        $sql = 'UPDATE ' . $xoopsDB->prefix('gwiki_pages') . " SET admin_lock = 1 WHERE keyword='{$page}' ";
552
553
        $success = $xoopsDB->query($sql);
554
        redirect_header('pages.php?op=manage', 2, $success ? _MD_GWIKI_DBUPDATED : _MD_GWIKI_ERRORINSERT);
555
        break;
556
557
    case 'unlock':
558
        confirmAction('unlock', $page);
559
        break;
560
561 View Code Duplication
    case 'unlockit':
0 ignored issues
show
Duplication introduced by
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...
562
        $sql = 'UPDATE ' . $xoopsDB->prefix('gwiki_pages') . " SET admin_lock = 0 WHERE keyword='{$page}' ";
563
564
        $success = $xoopsDB->query($sql);
565
        redirect_header('pages.php?op=manage', 2, $success ? _MD_GWIKI_DBUPDATED : _MD_GWIKI_ERRORINSERT);
566
        break;
567
568
    case 'partition':
569
        if (checkForPartitions()) {
570
            showPages(_AD_GWIKI_PARTITION_ALREADY);
571
        } else {
572
            confirmAction('partition', '');
573
        }
574
        break;
575
576
    case 'partitionit':
577
        $message = createPartitions();
578
        showPages($message);
579
        break;
580
581
    case 'addhelp':
582
        confirmAction('addhelp', '');
583
        break;
584
585
    case 'addhelpit':
586
        $message = createHelpPages();
587
        showPages($message);
588
        break;
589
590
    case 'manage':
591
    default:
592
        showPages();
593
        break;
594
595
}
596
597
include __DIR__ . '/footer.php';
598