Completed
Push — master ( e22ad1...12e8ae )
by Michael
02:11
created

wizard.php (24 issues)

Upgrade to new PHP Analysis Engine

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

1
<?php
2
/**
3
 * wizard.php - wiki page creation wizard
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 dirname(dirname(__DIR__)) . '/mainfile.php';
12
$xoopsOption['template_main'] = 'gwiki_wizard.tpl';
13
include XOOPS_ROOT_PATH . '/header.php';
14
include_once 'include/functions.php';
15
include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php';
16
global $wikiPage, $xoopsDB;
17
18
$token = 0;
19
20
/**
21
 * @param $params
22
 */
23
function redirect_to_edit($params)
24
{
25
    global $xoopsLogger, $wikiPage;
26
27
    $url = XOOPS_URL . '/modules/' . $wikiPage->getWikiDir() . '/edit.php#wikipage';
28
29
    $_SESSION['gwikiwizard'] = serialize($params);
30
31
    redirect_header($url, 1, _MD_GWIKI_WIZARD_FORWARDING);
32
    exit;
33
}
34
35
/**
36
 * @return bool
37
 */
38
function obtainPage()
39
{
40
    global $wikiPage, $xoopsTpl, $token;
41
42
    $wikiPage = new gwikiPage;
43
    $prefixes = $wikiPage->getUserNamespaces(true);
44
    if ($prefixes) {
45
        $options = array();
46
        foreach ($prefixes as $p) {
47
            $options[$p['prefix_id']] = $p['prefix'];
48
        }
49
    } else {
50
        $err_message = _MD_GWIKI_NO_PAGE_PERMISSION;
51
        redirect_header('index.php', 2, $err_message);
52
    }
53
54
    $page = '';
55
56
    $form = new XoopsThemeForm(_MD_GWIKI_WIZARD_NEWPAGE_PROMPT, 'gwizardform', 'wizard.php', 'POST', $token);
57
58
    $form_ns_select = new XoopsFormSelect(_MD_GWIKI_WIZARD_PICK_NAMESPACE, 'nsid'); //, [mixed $value = null], [int $size = 1], [bool $multiple = false]  )
59
    $form_ns_select->addOptionArray($options);
0 ignored issues
show
The variable $options 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...
60
    $form->addElement($form_ns_select);
61
62
    $form->addElement(new XoopsFormText(_MD_GWIKI_WIZARD_PAGE_NAME, 'page', 20, 120, $page));
63
64
    $btn_tray   = new XoopsFormElementTray('', ' ', 'gwizardformtray');
65
    $submit_btn = new XoopsFormButton('', 'wikiwizard_submit', _MD_GWIKI_WIZARD_CONTINUE, 'submit');
66
    //  $submit_btn->setExtra("onclick='prepForSubmit();'");
67
    $btn_tray->addElement($submit_btn);
68
69
    $cancel_btn = new XoopsFormButton('', 'wikiwizard_cancel', _MD_GWIKI_WIZARD_CANCEL, 'button');
70
    $cancel_btn->setExtra(' onclick="document.location.href=\'index.php\';"');
71
    $btn_tray->addElement($cancel_btn);
72
73
    $form->addElement($btn_tray);
74
75
    $form->assign($xoopsTpl);
76
77
    return true;
78
}
79
80
function obtainImportText()
81
{
82
    global $wikiPage, $xoopsTpl, $token;
83
84
    $form = new XoopsThemeForm(_MD_GWIKI_IMPORT_TEXT_TITLE, 'gwizardform', 'wizard.php', 'POST', $token);
85
    $form->setExtra(' enctype="multipart/form-data" ');
86
87
    $caption = _MD_GWIKI_IMPORT_TEXT_FILE;
88
    $form->addElement(new XoopsFormFile($caption, 'import_file', $wikiPage->getMaxUploadSize()), false);
89
    $form->addElement(new XoopsFormLabel('', _MD_GWIKI_IMPORT_TEXT_FORM_DESC, 'instructions'));
90
91
    $btn_tray   = new XoopsFormElementTray('', ' ', 'gwizardformtray');
92
    $submit_btn = new XoopsFormButton('', 'wikiwizard_submit', _MD_GWIKI_WIZARD_CONTINUE, 'submit');
93
    //  $submit_btn->setExtra("onclick='prepForSubmit();'");
94
    $btn_tray->addElement($submit_btn);
95
96
    $cancel_btn = new XoopsFormButton('', 'wikiwizard_cancel', _MD_GWIKI_WIZARD_CANCEL, 'button');
97
    $cancel_btn->setExtra(" onclick='history.back();'");
98
    $btn_tray->addElement($cancel_btn);
99
100
    $form->addElement($btn_tray);
101
    $form->addElement(new XoopsFormHidden('page', $wikiPage->keyword));
102
    $form->addElement(new XoopsFormHidden('op', 'doimporttext'));
103
104
    $form->assign($xoopsTpl);
105
}
106
107
/**
108
 * @param $page
109
 * @param $dir
110
 *
111
 * @return bool
112
 */
113
function doImportText($page, $dir)
114
{
115
    $import   = '';
116
    $pathname = XOOPS_ROOT_PATH . '/uploads/' . $dir . '/';
117 View Code Duplication
    if (isset($_POST['xoops_upload_file'][0])) {
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
118
        $filekey = $_POST['xoops_upload_file'][0];
119
        if (isset($_FILES[$filekey]) && !$_FILES[$filekey]['error']) {
120
            $zapus    = array(' ', '/', '\\');
0 ignored issues
show
$zapus is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
121
            $filename = tempnam($pathname, 'IMPORTTEXT_');
122
            if (move_uploaded_file($_FILES[$filekey]['tmp_name'], $filename)) {
123
                $import = file_get_contents($filename);
124
                unlink($filename);
125
            } else {
126
                return false;
127
            }
128
        }
129
    }
130
    if (empty($import)) {
131
        return false;
132
    }
133
134
    if (!empty($import)) {
135
        $params = array(
136
            'page' => $page,
137
            'op'   => 'preview',
138
            'body' => $import
139
        );
140
141
        redirect_to_edit($params);
142
        exit;
143
    }
144
145
    return false;
146
}
147
148
/**
149
 * @param string $import_html
150
 */
151
function obtainImportHTML($import_html = '')
152
{
153
    global $wikiPage, $xoopsTpl, $token;
154
155
    $form = new XoopsThemeForm(_MD_GWIKI_IMPORT_HTML_TITLE, 'gwizardform', 'wizard.php', 'POST', $token);
156
    $form->setExtra(' enctype="multipart/form-data" ');
157
158
    $caption = _MD_GWIKI_IMPORT_HTML_FILE;
159
    $form->addElement(new XoopsFormFile($caption, 'import_file', $wikiPage->getMaxUploadSize()), false);
160
    $form->addElement(new XoopsFormLabel('', _MD_GWIKI_IMPORT_HTML_FORM_DESC, 'instructions'));
161
162
    $form->addElement(new XoopsFormTextArea(_MD_GWIKI_IMPORT_HTML_TEXT, 'import_html', htmlspecialchars($import_html), 10, 40));
163
    $btn_tray   = new XoopsFormElementTray('', ' ', 'gwizardformtray');
164
    $submit_btn = new XoopsFormButton('', 'wikiwizard_submit', _MD_GWIKI_WIZARD_CONTINUE, 'submit');
165
    //  $submit_btn->setExtra("onclick='prepForSubmit();'");
166
    $btn_tray->addElement($submit_btn);
167
168
    $cancel_btn = new XoopsFormButton('', 'wikiwizard_cancel', _MD_GWIKI_WIZARD_CANCEL, 'button');
169
    $cancel_btn->setExtra(" onclick='history.back();'");
170
    $btn_tray->addElement($cancel_btn);
171
172
    $form->addElement($btn_tray);
173
    $form->addElement(new XoopsFormHidden('page', $wikiPage->keyword));
174
    $form->addElement(new XoopsFormHidden('op', 'doimporthtml'));
175
176
    $form->assign($xoopsTpl);
177
}
178
179
/**
180
 * @param         $out
181
 * @param DOMNode $domNode
182
 * @param         $nest
183
 * @param         $lt
184
 * @param         $ld
185
 * @param         $nop
186
 */
187
function showDOMNode(&$out, DOMNode $domNode, $nest, $lt, $ld, $nop)
188
{
189
    foreach ($domNode->childNodes as $node) {
190
        switch ($node->nodeName) {
191
            case 'a':
192
                $h = $node->getAttribute('href');
193
                $h = str_replace(array("\n", "\r"), '', $h);
194
                if (!empty($h)) {
195
                    $out .= '[[' . $h . '|';
196
                    if ($node->hasChildNodes()) {
197
                        showDOMNode($out, $node, $nest + 1, $lt, $ld, 1);
198
                    }
199
                    $out .= ' ]]';
200
                }
201
                break;
202
            case 'img':
203
                $out .= '{{' . $node->getAttribute('src');
204
                $alt = trim($node->getAttribute('alt'));
205
                if (!empty($alt)) {
206
                    $out .= '|' . $alt;
207
                }
208
                if ($node->hasChildNodes()) {
209
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
210
                }
211
                $out .= '}}';
212
                break;
213 View Code Duplication
            case 'p':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
214
                if ($ld < 1) {
215
                    $out .= "\n\n";
216
                }
217
                if ($node->hasChildNodes()) {
218
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
219
                }
220
                break;
221 View Code Duplication
            case 'div':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
222
                $out .= "\n\n";
223
                if ($node->hasChildNodes()) {
224
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
225
                }
226
                $out .= "\n\n";
227
                break;
228 View Code Duplication
            case 'blockquote':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
229
                $out .= "\n> ";
230
                if ($node->hasChildNodes()) {
231
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
232
                }
233
                break;
234 View Code Duplication
            case 'pre':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
235
                $out .= "\n{{{\n";
236
                if ($node->hasChildNodes()) {
237
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, 0);
238
                }
239
                $out .= "\n}}}\n";
240
                break;
241 View Code Duplication
            case 'ul':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
242
                $out .= "\n";
243
                if ($node->hasChildNodes()) {
244
                    showDOMNode($out, $node, $nest + 1, '*', $ld + 1, $nop);
245
                }
246
                $out .= "\n";
247
                break;
248 View Code Duplication
            case 'ol':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
249
                $out .= "\n";
250
                if ($node->hasChildNodes()) {
251
                    showDOMNode($out, $node, $nest + 1, '#', $ld + 1, $nop);
252
                }
253
                $out .= "\n";
254
                break;
255
            case 'li':
256
                $out .= "\n";
257
                if ($ld === 0) {
258
                    $ld = 1;
259
                }
260
                if ($lt === '#') {
261
                    for ($i = 1; $i <= $ld; ++$i) {
262
                        $out .= '#';
263
                    }
264
                } else {
265
                    for ($i = 1; $i <= $ld; ++$i) {
266
                        $out .= '*';
267
                    }
268
                }
269
                $out .= ' ';
270
                if ($node->hasChildNodes()) {
271
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, 1);
272
                }
273
                break;
274 View Code Duplication
            case 'h1':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
275
                $out .= "\n= " . $node->getAttribute('href');
276
                if ($node->hasChildNodes()) {
277
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, 1);
278
                }
279
                $out .= "\n";
280
                break;
281 View Code Duplication
            case 'h2':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
282
                $out .= "\n== " . $node->getAttribute('href');
283
                if ($node->hasChildNodes()) {
284
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, 1);
285
                }
286
                $out .= "\n";
287
                break;
288 View Code Duplication
            case 'h3':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
289
                $out .= "\n=== " . $node->getAttribute('href');
290
                if ($node->hasChildNodes()) {
291
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
292
                }
293
                $out .= "\n";
294
                break;
295 View Code Duplication
            case 'h4':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
296
                $out .= "\n=== " . $node->getAttribute('href');
297
                if ($node->hasChildNodes()) {
298
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
299
                }
300
                $out .= "\n";
301
                break;
302 View Code Duplication
            case 'h5':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
303
                $out .= "\n===== " . $node->getAttribute('href');
304
                if ($node->hasChildNodes()) {
305
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
306
                }
307
                $out .= "\n";
308
                break;
309
            case 'b':
310 View Code Duplication
            case 'strong':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
311
                $out .= '**';
312
                if ($node->hasChildNodes()) {
313
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
314
                }
315
                $out .= '**';
316
                break;
317
            case 'i':
318 View Code Duplication
            case 'em':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
319
                $out .= '//';
320
                if ($node->hasChildNodes()) {
321
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
322
                }
323
                $out .= '//';
324
                break;
325 View Code Duplication
            case 'u':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
326
                $out .= '__';
327
                if ($node->hasChildNodes()) {
328
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
329
                }
330
                $out .= '__';
331
                break;
332
            case 'br':
333
                $out .= '\\\\';
334
                break;
335
            case 'hr':
336
                $out .= "\n----\n";
337
                break;
338 View Code Duplication
            case 'tr':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
339
                if ($node->hasChildNodes()) {
340
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
341
                }
342
                $out .= "|\n";
343
                break;
344 View Code Duplication
            case 'td':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
345
                $out .= '|';
346
                if ($node->hasChildNodes()) {
347
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, 1);
348
                }
349
                break;
350 View Code Duplication
            case 'th':
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
351
                $out .= '|=';
352
                if ($node->hasChildNodes()) {
353
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, 1);
354
                }
355
                break;
356
            case '#text':
357
                if ($nop) {
358
                    $out .= str_replace(array("\n", "\r", '  '), ' ', $node->nodeValue);
359
                } else {
360
                    $out .= $node->nodeValue;
361
                }
362
                break;
363
            default:
364
                if ($node->hasChildNodes()) {
365
                    showDOMNode($out, $node, $nest + 1, $lt, $ld, $nop);
366
                }
367
                break;
368
        }
369
    }
370
}
371
372
/**
373
 * @param $page
374
 * @param $import_html
375
 * @param $dir
376
 *
377
 * @return bool
378
 */
379
function doImportHTML($page, $import_html, $dir)
380
{
381
    $import   = '';
382
    $pathname = XOOPS_ROOT_PATH . '/uploads/' . $dir . '/';
383 View Code Duplication
    if (isset($_POST['xoops_upload_file'][0])) {
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
384
        $filekey = $_POST['xoops_upload_file'][0];
385
        if (isset($_FILES[$filekey]) && !$_FILES[$filekey]['error']) {
386
            $zapus    = array(' ', '/', '\\');
0 ignored issues
show
$zapus is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
387
            $filename = tempnam($pathname, 'IMPORTHTML_');
388
            if (move_uploaded_file($_FILES[$filekey]['tmp_name'], $filename)) {
389
                $import = file_get_contents($filename);
390
                unlink($filename);
391
            } else {
392
                return false;
393
            }
394
        }
395
    }
396
    if (empty($import) && !empty($import_html)) {
397
        $import = $import_html;
398
    }
399
400
    if (!empty($import)) {
401
        // the "--" mark is common in text, but gets interpreted as strike
402
        //$search  = "#(?<=\s)(-{2})(?=\s)#";
403
        //$replace = "~\\1";
404
        //$import=preg_replace($search, $replace, $import);
405
406
        $doc = new DOMDocument();
407
        $doc->loadHTML($import);
408
        $domlist = $doc->getElementsByTagName('body');
409
        $out     = '';
410
        foreach ($domlist as $node) {
411
            showDOMNode($out, $node, 0, '', 0, 1);
412
        }
413
414
        $params = array(
415
            'page' => $page,
416
            'op'   => 'preview',
417
            'body' => $out
418
        );
419
420
        redirect_to_edit($params);
421
        exit;
422
    }
423
424
    return false;
425
}
426
427
/**
428
 * @param $page
429
 * @param $templatename
430
 *
431
 * @return bool
432
 */
433
function doTemplate($page, $templatename)
434
{
435
    global $wikiPage, $xoopsDB;
436
437
    $p = $wikiPage->getPage($templatename);
438
    if ($p) {
439
        $params = array(
440
            'page' => $page,
441
            'op'   => 'preview',
442
            'body' => $p['body']
443
        );
444
445
        redirect_to_edit($params);
446
    }
447
    redirect_header(XOOPS_URL . "/modules/{$wikiPage->getWikiDir()}/wizard.php?page={$page}", 2, _MD_GWIKI_PAGENOTFOUND);
448
449
    return false;
450
}
451
452
function doGallery()
453
{
454
    global $wikiPage, $xoopsDB;
455
456
    $page = $wikiPage->keyword;
457
458
    $params = array(
459
        'page' => $page,
460
        'op'   => 'preview',
461
        'body' => '{gallery}'
462
    );
463
464
    redirect_to_edit($params);
465
}
466
467
/**
468
 * @param $page
469
 * @param $templatename
470
 *
471
 * @return bool
472
 */
473
function doCopy($page, $templatename)
474
{
475
    global $wikiPage, $xoopsDB;
476
477
    $p = $wikiPage->getPage($templatename);
478
    if ($p) {
479
        $params = array(
480
            'page'             => $page,
481
            'op'               => 'preview',
482
            'body'             => $p['body'],
483
            'title'            => $p['title'],
484
            'display_keyword'  => $page,
485
            'parent_page'      => $p['parent_page'],
486
            'page_set_home'    => $p['page_set_home'],
487
            'page_set_order'   => '',
488
            'meta_description' => $p['meta_description'],
489
            'meta_keywords'    => $p['meta_keywords'],
490
            'show_in_index'    => '1',
491
            'leave_inactive'   => '0'
492
        );
493
494
        redirect_to_edit($params);
495
    }
496
    redirect_header(XOOPS_URL . "/modules/{$wikiPage->getWikiDir()}/wizard.php?page={$page}", 2, _MD_GWIKI_PAGENOTFOUND);
497
498
    return false;
499
}
500
501
/**
502
 * @param $keyword_like
503
 *
504
 * @return array|bool
505
 */
506
function getPagesLike($keyword_like)
507
{
508
    global $wikiPage, $xoopsDB;
509
510
    $pages = false;
511
512
    if (!empty($keyword_like)) {
513
        $q_keyword = $wikiPage->escapeForDB($keyword_like . '%');
514
515
        $sql = 'SELECT keyword, display_keyword FROM ' . $xoopsDB->prefix('gwiki_pages');
516
        $sql .= " WHERE keyword like '{$q_keyword}'";
517
        $sql .= ' AND active = 1';
518
        $sql .= ' ORDER BY display_keyword ';
519
        $pages  = array();
520
        $result = $xoopsDB->query($sql);
521
        while ($myrow = $xoopsDB->fetchArray($result)) {
522
            $pages[$myrow['keyword']] = $myrow['display_keyword'];
523
        }
524
    }
525
526
    return $pages;
527
}
528
529
/**
530
 * @return bool
531
 */
532
function galleryForm()
533
{
534
    global $wikiPage, $xoopsTpl, $xoopsModuleConfig;
535
536
    $page   = $wikiPage->keyword;
537
    $title  = _MD_GWIKI_WIZARD_GALLERY_SELECT;
538
    $body   = array();
539
    $body[] = '<div class="wikiimagedetail">';
540
    $body[] = '<form id="wikieditimg_form" action="ajaximgedit.php" method="POST" enctype="multipart/form-data">';
541
    $body[] = '<input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="' . $wikiPage->getMaxUploadSize() . '" />';
542
    $body[] = '<input type="hidden" id="page" name="page" value="' . $page . '" />';
543
    $body[] = '<div id="wikieditimg_dd">';
544
    //  $body[] = '<img name="wikieditimg_img" id="wikieditimg_img" class="wikieditimg" src="assets/images/blank.png" /><br />';
545
    $body[] = '<span id="wikieditimg_dd_msg">' . _MD_GWIKI_IMAGES_DROPHERE . '</span>';
546
    $body[] = '<div id="gwikiimgform_nofiledrag">' . _MD_GWIKI_IMAGES_PICKFILE . '<input type="file" id="wikieditimg_fileselect" name="fileselect[]"  multiple="multiple"/></div>';
547
    $body[] = '<div id="wikieditimg_progress"></div>';
548
    $body[] = '</div>';
549
    $body[] = '</form>';
550
    $body[] = '</div>';
551
    $body[] = '<form id="gwizardform" name="gwizardform" action="wizard.php" method="POST">';
552
    $body[] = '<table class="wikiwizard_table">';
553
    $body[] = '<tr><td></td><td><hr /></td></tr>';
554
    $body[] = '<tr><td> </td><td>';
555
    $body[] = '<input type="hidden" name="page" value="' . $page . '">';
556
    $body[] = '<input type="hidden" name="op" value="addgallery">';
557
    $body[] = '<input type="submit" class="formButton" name="wikiwizard_submit" id="wikiwizard_submit" value="' . _MD_GWIKI_WIZARD_CONTINUE . '" />';
558
    $body[] = '<input type="button" class="formButton" name="wikiwizard_cancel" id="wikiwizard_cancel" value="' . _MD_GWIKI_WIZARD_CANCEL . '" onclick="document.location.href=\'wizard.php\';" />';
559
    $body[] = '</td></tr>';
560
    $body[] = '</table>';
561
    $body[] = '</form>';
562
563
    $xoopsTpl->assign('body', implode("\n", $body));
564
    $xoopsTpl->assign('title', $title);
565
566
    return true;
567
}
568
569
/**
570
 * @return bool
571
 */
572
function chooseWizard()
573
{
574
    global $wikiPage, $xoopsTpl, $xoopsModuleConfig;
575
576
    $wizopts = array();
577
578
    $template_namespace = $xoopsModuleConfig['template_namespace'];
579
    if (!empty($template_namespace)) {
580
        $templates = getPagesLike($template_namespace);
581
        if ($templates) {
582
            $wizopts[] = array(
583
                'name'        => 'template',
584
                'title'       => _MD_GWIKI_WIZARD_TEMPLATE_TITLE,
585
                'description' => _MD_GWIKI_WIZARD_TEMPLATE_DESC,
586
                'options'     => array(
587
                    array('type' => 'select', 'prompt' => '', 'name' => 'templatename', 'values' => $templates)
588
                )
589
            );
590
        }
591
    }
592
593
    $wizopts[] = array(
594
        'name'        => 'copy',
595
        'title'       => _MD_GWIKI_WIZARD_COPY_TITLE,
596
        'description' => _MD_GWIKI_WIZARD_COPY_DESC,
597
        'options'     => array(
598
            array('type' => 'text', 'prompt' => _MD_GWIKI_WIZARD_COPY_PAGE, 'name' => 'copykeyword', 'values' => '')
599
        )
600
    );
601
602
    $wizopts[] = array(
603
        'name'        => 'importhtml',
604
        'title'       => _MD_GWIKI_WIZARD_HTML_TITLE,
605
        'description' => _MD_GWIKI_WIZARD_HTML_DESC,
606
        'options'     => null
607
    );
608
609
    $wizopts[] = array(
610
        'name'        => 'importtext',
611
        'title'       => _MD_GWIKI_WIZARD_TEXT_TITLE,
612
        'description' => _MD_GWIKI_WIZARD_TEXT_DESC,
613
        'options'     => null
614
    );
615
616
    $wizopts[] = array(
617
        'name'        => 'gallery',
618
        'title'       => _MD_GWIKI_WIZARD_GALLERY_TITLE,
619
        'description' => _MD_GWIKI_WIZARD_GALLERY_DESC,
620
        'options'     => null
621
    );
622
623
    $page   = $wikiPage->keyword;
624
    $title  = _MD_GWIKI_WIZARD_OPTIONS_TITLE;
625
    $body   = array();
626
    $body[] = '<form id="gwizardform" name="gwizardform" action="wizard.php" method="POST">';
627
    $body[] = '<table class="wikiwizard_table">';
628
    foreach ($wizopts as $i => $opt) {
629
        $rid    = 'radio_id_' . $opt['name'];
630
        $body[] = '<tr><td> </td><td><span class="wikiwizard_formcaption">' . $opt['title'] . '</span></td></tr>';
631
        $body[] = '<tr><td> <input type="radio" name="op" id="' . $rid . '" value="' . $opt['name'] . '"></td><td>' . $opt['description'] . '</td></tr>';
632
        if (!empty($opt['options'])) {
633
            foreach ($opt['options'] as $value) {
634
                switch ($value['type']) {
635
                    case 'select':
636
                        $body[] = '<tr><td>' . $value['prompt'] . '</td><td><select name="' . $value['name'] . '" id="' . $value['name'] . '" onchange="setRadioButton(\'' . $rid . '\');">';
637
                        foreach ($value['values'] as $n => $v) {
0 ignored issues
show
The expression $value['values'] of type string is not traversable.
Loading history...
638
                            $body[] = '<option value="' . $n . '">' . $v . '</option>';
639
                        }
640
                        $body[] = '</select></td></tr>';
641
                        break;
642
                    case 'text':
643
                        $body[] = '<tr><td> </td><td>' . $value['prompt'] . ' <input name="' . $value['name'] . '" id="' . $value['name'] . '" value="' . $value['values'] . '" onchange="setRadioButton(\'' . $rid . '\');"></td></tr>';
644
                        break;
645
                    default:
646
                        break;
647
                }
648
            }
649
        }
650
        $body[] = '<tr><td></td><td><hr /></td></tr>';
651
    }
652
    $body[] = '<tr><td> </td><td>';
653
    $body[] = '<input type="hidden" name="page" value="' . $page . '">';
654
    $body[] = '<input type="submit" class="formButton" name="wikiwizard_submit" id="wikiwizard_submit" value="' . _MD_GWIKI_WIZARD_CONTINUE . '" />';
655
    $body[] = '<input type="button" class="formButton" name="wikiwizard_cancel" id="wikiwizard_cancel" value="' . _MD_GWIKI_WIZARD_CANCEL . '" onclick="document.location.href=\'wizard.php\';" />';
656
    $body[] = '</td></tr>';
657
    $body[] = '</table>';
658
    $body[] = '</form>';
659
660
    $xoopsTpl->assign('body', implode("\n", $body));
661
    $xoopsTpl->assign('title', $title);
662
663
    return true;
664
}
665
666
$page = '';
667
if (isset($_GET['page'])) {
668
    $page = cleaner($_GET['page']);
669
}
670
if (isset($_POST['page'])) {
671
    $page = cleaner($_POST['page']);
672
}
673
// namespace id (prefix_id) is set by newpage block, turn it into a full page name
674
if (isset($_REQUEST['nsid'])) {
675
    $page = $wikiPage->makeKeywordFromPrefix((int)$_REQUEST['nsid'], $page);
676
}
677
678
$op = '';
679
if (isset($_POST['op'])) {
680
    $op = cleaner($_POST['op']);
681
}
682
$import_html = '';
683
if (isset($_POST['import_html'])) {
684
    $import_html = cleaner($_POST['import_html']);
685
}
686
$templatename = '';
687
if (isset($_POST['templatename'])) {
688
    $templatename = cleaner($_POST['templatename']);
689
}
690
$copykeyword = '';
691
if (isset($_POST['copykeyword'])) {
692
    $copykeyword = cleaner($_POST['copykeyword']);
693
}
694
if (empty($page)) {
695
    $pageX   = false;
696
    $op      = 'page';
697
    $mayEdit = false;
698
} else {
699
    $pageX   = $wikiPage->getPage($page);
700
    $mayEdit = $wikiPage->checkEdit();
701
    if (!$mayEdit) {
702
        $err_message = _MD_GWIKI_NO_PAGE_PERMISSION;
703
        redirect_header("index.php?page=$page", 2, $err_message);
704
    }
705
}
706
707
if ($pageX) {
708
    $pageX['author']       = $wikiPage->getUserName($wikiPage->uid);
709
    $pageX['revisiontime'] = date($wikiPage->dateFormat, $pageX['lastmodified']);
710
    $pageX['mayEdit']      = $mayEdit;
711
    $pageX['pageFound']    = true;
712 View Code Duplication
} else {
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
713
    $pageX                 = array();
714
    $uid                   = $xoopsUser ? $xoopsUser->getVar('uid') : 0;
715
    $pageX['uid']          = $uid;
716
    $pageX['author']       = $wikiPage->getUserName($uid);
717
    $pageX['revisiontime'] = date($wikiPage->dateFormat);
718
    $pageX['mayEdit']      = $mayEdit;
719
    $pageX['keyword']      = $page;
720
    $pageX['pageFound']    = false;
721
}
722
723
$dir               = basename(__DIR__);
724
$pageX['moddir']   = $dir;
725
$pageX['modpath']  = XOOPS_ROOT_PATH . '/modules/' . $dir;
726
$pageX['modurl']   = XOOPS_URL . '/modules/' . $dir;
727
$pageX['ineditor'] = false;
728
729
switch ($op) {
730
    case 'page':
731
        obtainPage();
732
        break;
733
    case 'importtext':
734
        obtainImportText();
735
        break;
736
    case 'doimporttext':
737
        doImportText($page, $dir);
738
        obtainImportText(); // if we come back, we failed so try again
739
        break;
740
    case 'importhtml':
741
        obtainImportHTML($import_html);
742
        break;
743
    case 'doimporthtml':
744
        doImportHTML($page, $import_html, $dir);
745
        obtainImportHTML($import_html); // if we come back, we failed so try again
746
        break;
747
    case 'template':
748
        doTemplate($page, $templatename);
749
        chooseWizard();
750
        break;
751
    case 'copy':
752
        doCopy($page, $copykeyword);
753
        chooseWizard();
754
        break;
755
    case 'gallery':
756
        galleryForm();
757
        break;
758
    case 'addgallery':
759
        doGallery();
760
        break;
761
    default:
762
        chooseWizard();
763
        break;
764
}
765
766
$title = _MD_GWIKI_WIZARD;
767
$xoopsTpl->assign('xoops_pagetitle', $title);
768
$xoopsTpl->assign('icms_pagetitle', $title);
769
770
$xoopsTpl->assign('gwiki', $pageX);
771
772
if (!empty($err_message)) {
773
    $xoopsTpl->assign('err_message', $err_message);
774
}
775
if (!empty($message)) {
776
    $xoopsTpl->assign('message', $message);
777
}
778
779
$xoTheme->addStylesheet(XOOPS_URL . '/modules/gwiki/assets/css/module.css');
780
781
include XOOPS_ROOT_PATH . '/footer.php';
782