Failed Conditions
Push — psr2 ( 64159a )
by Andreas
07:54 queued 04:15
created

html.php ➔ html_updateprofile()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 91
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 71
nc 48
nop 0
dl 0
loc 91
rs 5.574
c 0
b 0
f 0

How to fix   Long Method   

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
 * HTML output functions
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     Andreas Gohr <[email protected]>
7
 */
8
9
if (!defined('SEC_EDIT_PATTERN')) {
10
    define('SEC_EDIT_PATTERN', '#<!-- EDIT({.*?}) -->#');
11
}
12
13
14
/**
15
 * Convenience function to quickly build a wikilink
16
 *
17
 * @author Andreas Gohr <[email protected]>
18
 * @param string  $id      id of the target page
19
 * @param string  $name    the name of the link, i.e. the text that is displayed
20
 * @param string|array  $search  search string(s) that shall be highlighted in the target page
21
 * @return string the HTML code of the link
22
 */
23
function html_wikilink($id,$name=null,$search=''){
24
    /** @var Doku_Renderer_xhtml $xhtml_renderer */
25
    static $xhtml_renderer = null;
26
    if(is_null($xhtml_renderer)){
27
        $xhtml_renderer = p_get_renderer('xhtml');
28
    }
29
30
    return $xhtml_renderer->internallink($id,$name,$search,true,'navigation');
31
}
32
33
/**
34
 * The loginform
35
 *
36
 * @author   Andreas Gohr <[email protected]>
37
 *
38
 * @param bool $svg Whether to show svg icons in the register and resendpwd links or not
39
 */
40
function html_login($svg = false){
41
    global $lang;
42
    global $conf;
43
    global $ID;
44
    global $INPUT;
45
46
    print p_locale_xhtml('login');
47
    print '<div class="centeralign">'.NL;
48
    $form = new Doku_Form(array('id' => 'dw__login'));
49
    $form->startFieldset($lang['btn_login']);
50
    $form->addHidden('id', $ID);
51
    $form->addHidden('do', 'login');
52
    $form->addElement(form_makeTextField(
53
        'u',
54
        ((!$INPUT->bool('http_credentials')) ? $INPUT->str('u') : ''),
55
        $lang['user'],
56
        'focus__this',
57
        'block')
58
    );
59
    $form->addElement(form_makePasswordField('p', $lang['pass'], '', 'block'));
60
    if($conf['rememberme']) {
61
        $form->addElement(form_makeCheckboxField('r', '1', $lang['remember'], 'remember__me', 'simple'));
62
    }
63
    $form->addElement(form_makeButton('submit', '', $lang['btn_login']));
64
    $form->endFieldset();
65
66
    if(actionOK('register')){
67
        $registerLink = (new \dokuwiki\Menu\Item\Register())->asHtmlLink('', $svg);
68
        $form->addElement('<p>'.$lang['reghere'].': '. $registerLink .'</p>');
69
    }
70
71
    if (actionOK('resendpwd')) {
72
        $resendPwLink = (new \dokuwiki\Menu\Item\Resendpwd())->asHtmlLink('', $svg);
73
        $form->addElement('<p>'.$lang['pwdforget'].': '. $resendPwLink .'</p>');
74
    }
75
76
    html_form('login', $form);
77
    print '</div>'.NL;
78
}
79
80
81
/**
82
 * Denied page content
83
 *
84
 * @return string html
85
 */
86
function html_denied() {
87
    print p_locale_xhtml('denied');
88
89
    if(empty($_SERVER['REMOTE_USER'])){
90
        html_login();
91
    }
92
}
93
94
/**
95
 * inserts section edit buttons if wanted or removes the markers
96
 *
97
 * @author Andreas Gohr <[email protected]>
98
 *
99
 * @param string $text
100
 * @param bool   $show show section edit buttons?
101
 * @return string
102
 */
103
function html_secedit($text,$show=true){
104
    global $INFO;
105
106
    if(!$INFO['writable'] || !$show || $INFO['rev']){
107
        return preg_replace(SEC_EDIT_PATTERN,'',$text);
108
    }
109
110
    return preg_replace_callback(SEC_EDIT_PATTERN,
111
                'html_secedit_button', $text);
112
}
113
114
/**
115
 * prepares section edit button data for event triggering
116
 * used as a callback in html_secedit
117
 *
118
 * @author Andreas Gohr <[email protected]>
119
 *
120
 * @param array $matches matches with regexp
121
 * @return string
122
 * @triggers HTML_SECEDIT_BUTTON
123
 */
124
function html_secedit_button($matches){
125
    $json = htmlspecialchars_decode($matches[1], ENT_QUOTES);
126
    $data = json_decode($json, true);
127
    if ($data == NULL) {
128
        return;
129
    }
130
    $data ['target'] = strtolower($data['target']);
131
    $data ['hid'] = strtolower($data['hid']);
132
133
    return trigger_event('HTML_SECEDIT_BUTTON', $data,
134
                         'html_secedit_get_button');
135
}
136
137
/**
138
 * prints a section editing button
139
 * used as default action form HTML_SECEDIT_BUTTON
140
 *
141
 * @author Adrian Lang <[email protected]>
142
 *
143
 * @param array $data name, section id and target
144
 * @return string html
145
 */
146
function html_secedit_get_button($data) {
147
    global $ID;
148
    global $INFO;
149
150
    if (!isset($data['name']) || $data['name'] === '') return '';
151
152
    $name = $data['name'];
153
    unset($data['name']);
154
155
    $secid = $data['secid'];
156
    unset($data['secid']);
157
158
    return "<div class='secedit editbutton_" . $data['target'] .
159
                       " editbutton_" . $secid . "'>" .
160
           html_btn('secedit', $ID, '',
161
                    array_merge(array('do'  => 'edit',
162
                                      'rev' => $INFO['lastmod'],
163
                                      'summary' => '['.$name.'] '), $data),
164
                    'post', $name) . '</div>';
165
}
166
167
/**
168
 * Just the back to top button (in its own form)
169
 *
170
 * @author Andreas Gohr <[email protected]>
171
 *
172
 * @return string html
173
 */
174
function html_topbtn(){
175
    global $lang;
176
177
    $ret = '<a class="nolink" href="#dokuwiki__top">' .
178
        '<button class="button" onclick="window.scrollTo(0, 0)" title="' . $lang['btn_top'] . '">' .
179
        $lang['btn_top'] .
180
        '</button></a>';
181
182
    return $ret;
183
}
184
185
/**
186
 * Displays a button (using its own form)
187
 * If tooltip exists, the access key tooltip is replaced.
188
 *
189
 * @author Andreas Gohr <[email protected]>
190
 *
191
 * @param string         $name
192
 * @param string         $id
193
 * @param string         $akey   access key
194
 * @param string[] $params key-value pairs added as hidden inputs
195
 * @param string         $method
196
 * @param string         $tooltip
197
 * @param bool|string    $label  label text, false: lookup btn_$name in localization
198
 * @param string         $svg (optional) svg code, inserted into the button
199
 * @return string
200
 */
201
function html_btn($name, $id, $akey, $params, $method='get', $tooltip='', $label=false, $svg=null){
202
    global $conf;
203
    global $lang;
204
205
    if (!$label)
206
        $label = $lang['btn_'.$name];
207
208
    $ret = '';
209
210
    //filter id (without urlencoding)
211
    $id = idfilter($id,false);
212
213
    //make nice URLs even for buttons
214
    if($conf['userewrite'] == 2){
215
        $script = DOKU_BASE.DOKU_SCRIPT.'/'.$id;
216
    }elseif($conf['userewrite']){
217
        $script = DOKU_BASE.$id;
218
    }else{
219
        $script = DOKU_BASE.DOKU_SCRIPT;
220
        $params['id'] = $id;
221
    }
222
223
    $ret .= '<form class="button btn_'.$name.'" method="'.$method.'" action="'.$script.'"><div class="no">';
224
225
    if(is_array($params)){
226
        foreach($params as $key => $val) {
227
            $ret .= '<input type="hidden" name="'.$key.'" ';
228
            $ret .= 'value="'.hsc($val).'" />';
229
        }
230
    }
231
232
    if ($tooltip!='') {
233
        $tip = hsc($tooltip);
234
    }else{
235
        $tip = hsc($label);
236
    }
237
238
    $ret .= '<button type="submit" ';
239
    if($akey){
240
        $tip .= ' ['.strtoupper($akey).']';
241
        $ret .= 'accesskey="'.$akey.'" ';
242
    }
243
    $ret .= 'title="'.$tip.'">';
244
    if ($svg) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $svg of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
245
        $ret .= '<span>' . hsc($label) . '</span>';
246
        $ret .= inlineSVG($svg);
247
    } else {
248
        $ret .= hsc($label);
249
    }
250
    $ret .= '</button>';
251
    $ret .= '</div></form>';
252
253
    return $ret;
254
}
255
/**
256
 * show a revision warning
257
 *
258
 * @author Szymon Olewniczak <[email protected]>
259
 */
260
function html_showrev() {
261
    print p_locale_xhtml('showrev');
262
}
263
264
/**
265
 * Show a wiki page
266
 *
267
 * @author Andreas Gohr <[email protected]>
268
 *
269
 * @param null|string $txt wiki text or null for showing $ID
270
 */
271
function html_show($txt=null){
272
    global $ID;
273
    global $REV;
274
    global $HIGH;
275
    global $INFO;
276
    global $DATE_AT;
277
    //disable section editing for old revisions or in preview
278
    if($txt || $REV){
0 ignored issues
show
Bug Best Practice introduced by
The expression $txt of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
279
        $secedit = false;
280
    }else{
281
        $secedit = true;
282
    }
283
284
    if (!is_null($txt)){
285
        //PreviewHeader
286
        echo '<br id="scroll__here" />';
287
        echo p_locale_xhtml('preview');
288
        echo '<div class="preview"><div class="pad">';
289
        $html = html_secedit(p_render('xhtml',p_get_instructions($txt),$info),$secedit);
290
        if($INFO['prependTOC']) $html = tpl_toc(true).$html;
291
        echo $html;
292
        echo '<div class="clearer"></div>';
293
        echo '</div></div>';
294
295
    }else{
296
        if ($REV||$DATE_AT){
297
            $data = array('rev' => &$REV, 'date_at' => &$DATE_AT);
298
            trigger_event('HTML_SHOWREV_OUTPUT', $data, 'html_showrev');
299
        }
300
        $html = p_wiki_xhtml($ID,$REV,true,$DATE_AT);
301
        $html = html_secedit($html,$secedit);
0 ignored issues
show
Bug introduced by
It seems like $html can also be of type boolean or null; however, html_secedit() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
302
        if($INFO['prependTOC']) $html = tpl_toc(true).$html;
303
        $html = html_hilight($html,$HIGH);
304
        echo $html;
305
    }
306
}
307
308
/**
309
 * ask the user about how to handle an exisiting draft
310
 *
311
 * @author Andreas Gohr <[email protected]>
312
 */
313
function html_draft(){
314
    global $INFO;
315
    global $ID;
316
    global $lang;
317
    $draft = unserialize(io_readFile($INFO['draft'],false));
318
    $text  = cleanText(con($draft['prefix'],$draft['text'],$draft['suffix'],true));
319
320
    print p_locale_xhtml('draft');
321
    html_diff($text, false);
322
    $form = new Doku_Form(array('id' => 'dw__editform'));
323
    $form->addHidden('id', $ID);
324
    $form->addHidden('date', $draft['date']);
325
    $form->addElement(form_makeOpenTag('div', array('id'=>'draft__status')));
326
    $form->addElement($lang['draftdate'].' '. dformat(filemtime($INFO['draft'])));
327
    $form->addElement(form_makeCloseTag('div'));
328
    $form->addElement(form_makeButton('submit', 'recover', $lang['btn_recover'], array('tabindex'=>'1')));
329
    $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_draftdel'], array('tabindex'=>'2')));
330
    $form->addElement(form_makeButton('submit', 'show', $lang['btn_cancel'], array('tabindex'=>'3')));
331
    html_form('draft', $form);
332
}
333
334
/**
335
 * Highlights searchqueries in HTML code
336
 *
337
 * @author Andreas Gohr <[email protected]>
338
 * @author Harry Fuecks <[email protected]>
339
 *
340
 * @param string $html
341
 * @param array|string $phrases
342
 * @return string html
343
 */
344
function html_hilight($html,$phrases){
345
    $phrases = (array) $phrases;
346
    $phrases = array_map('preg_quote_cb', $phrases);
347
    $phrases = array_map('ft_snippet_re_preprocess', $phrases);
348
    $phrases = array_filter($phrases);
349
    $regex = join('|',$phrases);
350
351
    if ($regex === '') return $html;
352
    if (!utf8_check($regex)) return $html;
353
    $html = @preg_replace_callback("/((<[^>]*)|$regex)/ui",'html_hilight_callback',$html);
354
    return $html;
355
}
356
357
/**
358
 * Callback used by html_hilight()
359
 *
360
 * @author Harry Fuecks <[email protected]>
361
 *
362
 * @param array $m matches
363
 * @return string html
364
 */
365
function html_hilight_callback($m) {
366
    $hlight = unslash($m[0]);
367
    if ( !isset($m[2])) {
368
        $hlight = '<span class="search_hit">'.$hlight.'</span>';
369
    }
370
    return $hlight;
371
}
372
373
/**
374
 * Display error on locked pages
375
 *
376
 * @author Andreas Gohr <[email protected]>
377
 */
378
function html_locked(){
379
    global $ID;
380
    global $conf;
381
    global $lang;
382
    global $INFO;
383
384
    $locktime = filemtime(wikiLockFN($ID));
385
    $expire = dformat($locktime + $conf['locktime']);
386
    $min    = round(($conf['locktime'] - (time() - $locktime) )/60);
387
388
    print p_locale_xhtml('locked');
389
    print '<ul>';
390
    print '<li><div class="li"><strong>'.$lang['lockedby'].'</strong> '.editorinfo($INFO['locked']).'</div></li>';
391
    print '<li><div class="li"><strong>'.$lang['lockexpire'].'</strong> '.$expire.' ('.$min.' min)</div></li>';
392
    print '</ul>';
393
}
394
395
/**
396
 * list old revisions
397
 *
398
 * @author Andreas Gohr <[email protected]>
399
 * @author Ben Coburn <[email protected]>
400
 * @author Kate Arzamastseva <[email protected]>
401
 *
402
 * @param int $first skip the first n changelog lines
403
 * @param bool|string $media_id id of media, or false for current page
404
 */
405
function html_revisions($first=0, $media_id = false){
406
    global $ID;
407
    global $INFO;
408
    global $conf;
409
    global $lang;
410
    $id = $ID;
411
    if ($media_id) {
412
        $id = $media_id;
413
        $changelog = new MediaChangeLog($id);
414
    } else {
415
        $changelog = new PageChangeLog($id);
416
    }
417
418
    /* we need to get one additional log entry to be able to
419
     * decide if this is the last page or is there another one.
420
     * see html_recent()
421
     */
422
423
    $revisions = $changelog->getRevisions($first, $conf['recent']+1);
424
425
    if(count($revisions)==0 && $first!=0){
426
        $first=0;
427
        $revisions = $changelog->getRevisions($first, $conf['recent']+1);
428
    }
429
    $hasNext = false;
430
    if (count($revisions)>$conf['recent']) {
431
        $hasNext = true;
432
        array_pop($revisions); // remove extra log entry
433
    }
434
435
    if (!$media_id) print p_locale_xhtml('revisions');
436
437
    $params = array('id' => 'page__revisions', 'class' => 'changes');
438
    if($media_id) {
439
        $params['action'] = media_managerURL(array('image' => $media_id), '&');
440
    }
441
442
    if(!$media_id) {
443
        $exists = $INFO['exists'];
444
        $display_name = useHeading('navigation') ? hsc(p_get_first_heading($id)) : $id;
445
        if(!$display_name) {
446
            $display_name = $id;
447
        }
448
    } else {
449
        $exists = file_exists(mediaFN($id));
450
        $display_name = $id;
451
    }
452
453
    $form = new Doku_Form($params);
454
    $form->addElement(form_makeOpenTag('ul'));
455
456
    if($exists && $first == 0) {
457
        $minor = false;
458
        if($media_id) {
459
            $date = dformat(@filemtime(mediaFN($id)));
460
            $href = media_managerURL(array('image' => $id, 'tab_details' => 'view'), '&');
461
462
            $changelog->setChunkSize(1024);
463
            $revinfo = $changelog->getRevisionInfo(@filemtime(fullpath(mediaFN($id))));
464
465
            $summary = $revinfo['sum'];
466
            if($revinfo['user']) {
467
                $editor = $revinfo['user'];
468
            } else {
469
                $editor = $revinfo['ip'];
470
            }
471
            $sizechange = $revinfo['sizechange'];
472
        } else {
473
            $date = dformat($INFO['lastmod']);
474
            if(isset($INFO['meta']) && isset($INFO['meta']['last_change'])) {
475
                if($INFO['meta']['last_change']['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) {
476
                    $minor = true;
477
                }
478
                if(isset($INFO['meta']['last_change']['sizechange'])) {
479
                    $sizechange = $INFO['meta']['last_change']['sizechange'];
480
                } else {
481
                    $sizechange = null;
482
                }
483
            }
484
            $pagelog = new PageChangeLog($ID);
485
            $latestrev = $pagelog->getRevisions(-1, 1);
486
            $latestrev = array_pop($latestrev);
487
            $href = wl($id,"rev=$latestrev",false,'&');
488
            $summary = $INFO['sum'];
489
            $editor = $INFO['editor'];
490
        }
491
492
        $form->addElement(form_makeOpenTag('li', array('class' => ($minor ? 'minor' : ''))));
493
        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
494
        $form->addElement(form_makeTag('input', array(
495
                        'type' => 'checkbox',
496
                        'name' => 'rev2[]',
497
                        'value' => 'current')));
498
499
        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
500
        $form->addElement($date);
501
        $form->addElement(form_makeCloseTag('span'));
502
503
        $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
504
505
        $form->addElement(form_makeOpenTag('a', array(
506
                        'class' => 'wikilink1',
507
                        'href'  => $href)));
508
        $form->addElement($display_name);
509
        $form->addElement(form_makeCloseTag('a'));
510
511
        if ($media_id) $form->addElement(form_makeOpenTag('div'));
512
513
        if($summary) {
514
            $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
515
            if(!$media_id) $form->addElement(' – ');
516
            $form->addElement('<bdi>' . hsc($summary) . '</bdi>');
517
            $form->addElement(form_makeCloseTag('span'));
518
        }
519
520
        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
521
        $form->addElement((empty($editor))?('('.$lang['external_edit'].')'):'<bdi>'.editorinfo($editor).'</bdi>');
522
        $form->addElement(form_makeCloseTag('span'));
523
524
        html_sizechange($sizechange, $form);
0 ignored issues
show
Bug introduced by
The variable $sizechange 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...
525
526
        $form->addElement('('.$lang['current'].')');
527
528
        if ($media_id) $form->addElement(form_makeCloseTag('div'));
529
530
        $form->addElement(form_makeCloseTag('div'));
531
        $form->addElement(form_makeCloseTag('li'));
532
    }
533
534
    foreach($revisions as $rev) {
535
        $date = dformat($rev);
536
        $info = $changelog->getRevisionInfo($rev);
537
        if($media_id) {
538
            $exists = file_exists(mediaFN($id, $rev));
539
        } else {
540
            $exists = page_exists($id, $rev);
541
        }
542
543
        $class = '';
544
        if($info['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) {
545
            $class = 'minor';
546
        }
547
        $form->addElement(form_makeOpenTag('li', array('class' => $class)));
548
        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
549
        if($exists){
550
            $form->addElement(form_makeTag('input', array(
551
                            'type' => 'checkbox',
552
                            'name' => 'rev2[]',
553
                            'value' => $rev)));
554
        }else{
555
            $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
556
        }
557
558
        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
559
        $form->addElement($date);
560
        $form->addElement(form_makeCloseTag('span'));
561
562
        if($exists){
563
            if (!$media_id) {
564
                $href = wl($id,"rev=$rev,do=diff", false, '&');
565
            } else {
566
                $href = media_managerURL(array('image' => $id, 'rev' => $rev, 'mediado' => 'diff'), '&');
567
            }
568
            $form->addElement(form_makeOpenTag('a', array(
569
                            'class' => 'diff_link',
570
                            'href' => $href)));
571
            $form->addElement(form_makeTag('img', array(
572
                            'src'    => DOKU_BASE.'lib/images/diff.png',
573
                            'width'  => 15,
574
                            'height' => 11,
575
                            'title'  => $lang['diff'],
576
                            'alt'    => $lang['diff'])));
577
            $form->addElement(form_makeCloseTag('a'));
578
579
            if (!$media_id) {
580
                $href = wl($id,"rev=$rev",false,'&');
581
            } else {
582
                $href = media_managerURL(array('image' => $id, 'tab_details' => 'view', 'rev' => $rev), '&');
583
            }
584
            $form->addElement(form_makeOpenTag('a', array(
585
                            'class' => 'wikilink1',
586
                            'href' => $href)));
587
            $form->addElement($display_name);
588
            $form->addElement(form_makeCloseTag('a'));
589
        }else{
590
            $form->addElement('<img src="'.DOKU_BASE.'lib/images/blank.gif" width="15" height="11" alt="" />');
591
            $form->addElement($display_name);
592
        }
593
594
        if ($media_id) $form->addElement(form_makeOpenTag('div'));
595
596
        if ($info['sum']) {
597
            $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
598
            if(!$media_id) $form->addElement(' – ');
599
            $form->addElement('<bdi>'.hsc($info['sum']).'</bdi>');
600
            $form->addElement(form_makeCloseTag('span'));
601
        }
602
603
        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
604
        if($info['user']){
605
            $form->addElement('<bdi>'.editorinfo($info['user']).'</bdi>');
606
            if(auth_ismanager()){
607
                $form->addElement(' <bdo dir="ltr">('.$info['ip'].')</bdo>');
608
            }
609
        }else{
610
            $form->addElement('<bdo dir="ltr">'.$info['ip'].'</bdo>');
611
        }
612
        $form->addElement(form_makeCloseTag('span'));
613
614
        html_sizechange($info['sizechange'], $form);
615
616
        if ($media_id) $form->addElement(form_makeCloseTag('div'));
617
618
        $form->addElement(form_makeCloseTag('div'));
619
        $form->addElement(form_makeCloseTag('li'));
620
    }
621
    $form->addElement(form_makeCloseTag('ul'));
622
    if (!$media_id) {
623
        $form->addElement(form_makeButton('submit', 'diff', $lang['diff2']));
624
    } else {
625
        $form->addHidden('mediado', 'diff');
626
        $form->addElement(form_makeButton('submit', '', $lang['diff2']));
627
    }
628
    html_form('revisions', $form);
629
630
    print '<div class="pagenav">';
631
    $last = $first + $conf['recent'];
632
    if ($first > 0) {
633
        $first -= $conf['recent'];
634
        if ($first < 0) $first = 0;
635
        print '<div class="pagenav-prev">';
636
        if ($media_id) {
637
            print html_btn('newer',$media_id,"p",media_managerURL(array('first' => $first), '&amp;', false, true));
638
        } else {
639
            print html_btn('newer',$id,"p",array('do' => 'revisions', 'first' => $first));
640
        }
641
        print '</div>';
642
    }
643
    if ($hasNext) {
644
        print '<div class="pagenav-next">';
645
        if ($media_id) {
646
            print html_btn('older',$media_id,"n",media_managerURL(array('first' => $last), '&amp;', false, true));
647
        } else {
648
            print html_btn('older',$id,"n",array('do' => 'revisions', 'first' => $last));
649
        }
650
        print '</div>';
651
    }
652
    print '</div>';
653
654
}
655
656
/**
657
 * display recent changes
658
 *
659
 * @author Andreas Gohr <[email protected]>
660
 * @author Matthias Grimm <[email protected]>
661
 * @author Ben Coburn <[email protected]>
662
 * @author Kate Arzamastseva <[email protected]>
663
 *
664
 * @param int $first
665
 * @param string $show_changes
666
 */
667
function html_recent($first = 0, $show_changes = 'both') {
668
    global $conf;
669
    global $lang;
670
    global $ID;
671
    /* we need to get one additionally log entry to be able to
672
     * decide if this is the last page or is there another one.
673
     * This is the cheapest solution to get this information.
674
     */
675
    $flags = 0;
676
    if($show_changes == 'mediafiles' && $conf['mediarevisions']) {
677
        $flags = RECENTS_MEDIA_CHANGES;
678
    } elseif($show_changes == 'pages') {
679
        $flags = 0;
680
    } elseif($conf['mediarevisions']) {
681
        $show_changes = 'both';
682
        $flags = RECENTS_MEDIA_PAGES_MIXED;
683
    }
684
685
    $recents = getRecents($first, $conf['recent'] + 1, getNS($ID), $flags);
0 ignored issues
show
Security Bug introduced by
It seems like getNS($ID) targeting getNS() can also be of type false; however, getRecents() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
686
    if(count($recents) == 0 && $first != 0) {
687
        $first = 0;
688
        $recents = getRecents($first, $conf['recent'] + 1, getNS($ID), $flags);
0 ignored issues
show
Security Bug introduced by
It seems like getNS($ID) targeting getNS() can also be of type false; however, getRecents() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
689
    }
690
    $hasNext = false;
691
    if(count($recents) > $conf['recent']) {
692
        $hasNext = true;
693
        array_pop($recents); // remove extra log entry
694
    }
695
696
    print p_locale_xhtml('recent');
697
698
    if(getNS($ID) != '') {
699
        print '<div class="level1"><p>' .
700
            sprintf($lang['recent_global'], getNS($ID), wl('', 'do=recent')) .
701
            '</p></div>';
702
    }
703
704
    $form = new Doku_Form(array('id' => 'dw__recent', 'method' => 'GET', 'class' => 'changes'));
705
    $form->addHidden('sectok', null);
706
    $form->addHidden('do', 'recent');
707
    $form->addHidden('id', $ID);
708
709
    if($conf['mediarevisions']) {
710
        $form->addElement('<div class="changeType">');
711
        $form->addElement(form_makeListboxField(
712
                    'show_changes',
713
                    array(
714
                        'pages'      => $lang['pages_changes'],
715
                        'mediafiles' => $lang['media_changes'],
716
                        'both'       => $lang['both_changes']
717
                    ),
718
                    $show_changes,
719
                    $lang['changes_type'],
720
                    '', '',
721
                    array('class' => 'quickselect')));
722
723
        $form->addElement(form_makeButton('submit', 'recent', $lang['btn_apply']));
724
        $form->addElement('</div>');
725
    }
726
727
    $form->addElement(form_makeOpenTag('ul'));
728
729
    foreach($recents as $recent) {
730
        $date = dformat($recent['date']);
731
732
        $class = '';
733
        if($recent['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT) {
734
            $class = 'minor';
735
        }
736
        $form->addElement(form_makeOpenTag('li', array('class' => $class)));
737
        $form->addElement(form_makeOpenTag('div', array('class' => 'li')));
738
739
        if(!empty($recent['media'])) {
740
            $form->addElement(media_printicon($recent['id']));
741
        } else {
742
            $icon = DOKU_BASE . 'lib/images/fileicons/file.png';
743
            $form->addElement('<img src="' . $icon . '" alt="' . $recent['id'] . '" class="icon" />');
744
        }
745
746
        $form->addElement(form_makeOpenTag('span', array('class' => 'date')));
747
        $form->addElement($date);
748
        $form->addElement(form_makeCloseTag('span'));
749
750
        $diff = false;
751
        $href = '';
752
753
        if(!empty($recent['media'])) {
754
            $changelog = new MediaChangeLog($recent['id']);
755
            $revs = $changelog->getRevisions(0, 1);
756
            $diff = (count($revs) && file_exists(mediaFN($recent['id'])));
757
            if($diff) {
758
                $href = media_managerURL(array(
759
                                            'tab_details' => 'history',
760
                                            'mediado' => 'diff',
761
                                            'image' => $recent['id'],
762
                                            'ns' => getNS($recent['id'])
763
                                        ), '&');
764
            }
765
        } else {
766
            $href = wl($recent['id'], "do=diff", false, '&');
767
        }
768
769
        if(!empty($recent['media']) && !$diff) {
770
            $form->addElement('<img src="' . DOKU_BASE . 'lib/images/blank.gif" width="15" height="11" alt="" />');
771
        } else {
772
            $form->addElement(form_makeOpenTag('a', array('class' => 'diff_link', 'href' => $href)));
773
            $form->addElement(form_makeTag('img', array(
774
                            'src'    => DOKU_BASE . 'lib/images/diff.png',
775
                            'width'  => 15,
776
                            'height' => 11,
777
                            'title'  => $lang['diff'],
778
                            'alt'    => $lang['diff']
779
                        )));
780
            $form->addElement(form_makeCloseTag('a'));
781
        }
782
783
        if(!empty($recent['media'])) {
784
            $href = media_managerURL(
785
                array(
786
                    'tab_details' => 'history',
787
                    'image' => $recent['id'],
788
                    'ns' => getNS($recent['id'])
789
                ),
790
                '&'
791
            );
792
        } else {
793
            $href = wl($recent['id'], "do=revisions", false, '&');
794
        }
795
        $form->addElement(form_makeOpenTag('a', array(
796
                        'class' => 'revisions_link',
797
                        'href'  => $href)));
798
        $form->addElement(form_makeTag('img', array(
799
                        'src'    => DOKU_BASE . 'lib/images/history.png',
800
                        'width'  => 12,
801
                        'height' => 14,
802
                        'title'  => $lang['btn_revs'],
803
                        'alt'    => $lang['btn_revs']
804
                    )));
805
        $form->addElement(form_makeCloseTag('a'));
806
807
        if(!empty($recent['media'])) {
808
            $href = media_managerURL(
809
                array(
810
                    'tab_details' => 'view',
811
                    'image' => $recent['id'],
812
                    'ns' => getNS($recent['id'])
813
                ),
814
                '&'
815
            );
816
            $class = file_exists(mediaFN($recent['id'])) ? 'wikilink1' : 'wikilink2';
817
            $form->addElement(form_makeOpenTag('a', array(
818
                        'class' => $class,
819
                        'href'  => $href)));
820
            $form->addElement($recent['id']);
821
            $form->addElement(form_makeCloseTag('a'));
822
        } else {
823
            $form->addElement(html_wikilink(':' . $recent['id'], useHeading('navigation') ? null : $recent['id']));
824
        }
825
        $form->addElement(form_makeOpenTag('span', array('class' => 'sum')));
826
        $form->addElement(' – ' . hsc($recent['sum']));
827
        $form->addElement(form_makeCloseTag('span'));
828
829
        $form->addElement(form_makeOpenTag('span', array('class' => 'user')));
830
        if($recent['user']) {
831
            $form->addElement('<bdi>' . editorinfo($recent['user']) . '</bdi>');
832
            if(auth_ismanager()) {
833
                $form->addElement(' <bdo dir="ltr">(' . $recent['ip'] . ')</bdo>');
834
            }
835
        } else {
836
            $form->addElement('<bdo dir="ltr">' . $recent['ip'] . '</bdo>');
837
        }
838
        $form->addElement(form_makeCloseTag('span'));
839
840
        html_sizechange($recent['sizechange'], $form);
841
842
        $form->addElement(form_makeCloseTag('div'));
843
        $form->addElement(form_makeCloseTag('li'));
844
    }
845
    $form->addElement(form_makeCloseTag('ul'));
846
847
    $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav')));
848
    $last = $first + $conf['recent'];
849
    if($first > 0) {
850
        $first -= $conf['recent'];
851
        if($first < 0) $first = 0;
852
        $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav-prev')));
853
        $form->addElement(form_makeOpenTag('button', array(
854
                        'type'      => 'submit',
855
                        'name'      => 'first[' . $first . ']',
856
                        'accesskey' => 'n',
857
                        'title'     => $lang['btn_newer'] . ' [N]',
858
                        'class'     => 'button show'
859
                    )));
860
        $form->addElement($lang['btn_newer']);
861
        $form->addElement(form_makeCloseTag('button'));
862
        $form->addElement(form_makeCloseTag('div'));
863
    }
864
    if($hasNext) {
865
        $form->addElement(form_makeOpenTag('div', array('class' => 'pagenav-next')));
866
        $form->addElement(form_makeOpenTag('button', array(
867
                        'type'      => 'submit',
868
                        'name'      => 'first[' . $last . ']',
869
                        'accesskey' => 'p',
870
                        'title'     => $lang['btn_older'] . ' [P]',
871
                        'class'     => 'button show'
872
                    )));
873
        $form->addElement($lang['btn_older']);
874
        $form->addElement(form_makeCloseTag('button'));
875
        $form->addElement(form_makeCloseTag('div'));
876
    }
877
    $form->addElement(form_makeCloseTag('div'));
878
    html_form('recent', $form);
879
}
880
881
/**
882
 * Display page index
883
 *
884
 * @author Andreas Gohr <[email protected]>
885
 *
886
 * @param string $ns
887
 */
888
function html_index($ns){
889
    global $conf;
890
    global $ID;
891
    $ns  = cleanID($ns);
892
    if(empty($ns)){
893
        $ns = getNS($ID);
894
        if($ns === false) $ns ='';
895
    }
896
    $ns  = utf8_encodeFN(str_replace(':','/',$ns));
897
898
    echo p_locale_xhtml('index');
899
    echo '<div id="index__tree" class="index__tree">';
900
901
    $data = array();
902
    search($data,$conf['datadir'],'search_index',array('ns' => $ns));
903
    echo html_buildlist($data,'idx','html_list_index','html_li_index');
904
905
    echo '</div>';
906
}
907
908
/**
909
 * Index item formatter
910
 *
911
 * User function for html_buildlist()
912
 *
913
 * @author Andreas Gohr <[email protected]>
914
 *
915
 * @param array $item
916
 * @return string
917
 */
918
function html_list_index($item){
919
    global $ID, $conf;
920
921
    // prevent searchbots needlessly following links
922
    $nofollow = ($ID != $conf['start'] || $conf['sitemap']) ? 'rel="nofollow"' : '';
923
924
    $ret = '';
925
    $base = ':'.$item['id'];
926
    $base = substr($base,strrpos($base,':')+1);
927
    if($item['type']=='d'){
928
        // FS#2766, no need for search bots to follow namespace links in the index
929
        $link = wl($ID, 'idx=' . rawurlencode($item['id']));
930
        $ret .= '<a href="' . $link . '" title="' . $item['id'] . '" class="idx_dir" ' . $nofollow . '><strong>';
931
        $ret .= $base;
932
        $ret .= '</strong></a>';
933
    }else{
934
        // default is noNSorNS($id), but we want noNS($id) when useheading is off FS#2605
935
        $ret .= html_wikilink(':'.$item['id'], useHeading('navigation') ? null : noNS($item['id']));
936
    }
937
    return $ret;
938
}
939
940
/**
941
 * Index List item
942
 *
943
 * This user function is used in html_buildlist to build the
944
 * <li> tags for namespaces when displaying the page index
945
 * it gives different classes to opened or closed "folders"
946
 *
947
 * @author Andreas Gohr <[email protected]>
948
 *
949
 * @param array $item
950
 * @return string html
951
 */
952
function html_li_index($item){
953
    global $INFO;
954
    global $ACT;
955
956
    $class = '';
957
    $id = '';
958
959
    if($item['type'] == "f"){
960
        // scroll to the current item
961
        if($item['id'] == $INFO['id'] && $ACT == 'index') {
962
            $id = ' id="scroll__here"';
963
            $class = ' bounce';
964
        }
965
        return '<li class="level'.$item['level'].$class.'" '.$id.'>';
966
    }elseif($item['open']){
967
        return '<li class="open">';
968
    }else{
969
        return '<li class="closed">';
970
    }
971
}
972
973
/**
974
 * Default List item
975
 *
976
 * @author Andreas Gohr <[email protected]>
977
 *
978
 * @param array $item
979
 * @return string html
980
 */
981
function html_li_default($item){
982
    return '<li class="level'.$item['level'].'">';
983
}
984
985
/**
986
 * Build an unordered list
987
 *
988
 * Build an unordered list from the given $data array
989
 * Each item in the array has to have a 'level' property
990
 * the item itself gets printed by the given $func user
991
 * function. The second and optional function is used to
992
 * print the <li> tag. Both user function need to accept
993
 * a single item.
994
 *
995
 * Both user functions can be given as array to point to
996
 * a member of an object.
997
 *
998
 * @author Andreas Gohr <[email protected]>
999
 *
1000
 * @param array    $data  array with item arrays
1001
 * @param string   $class class of ul wrapper
1002
 * @param callable $func  callback to print an list item
1003
 * @param callable $lifunc callback to the opening li tag
1004
 * @param bool     $forcewrapper Trigger building a wrapper ul if the first level is
1005
 *                               0 (we have a root object) or 1 (just the root content)
1006
 * @return string html of an unordered list
1007
 */
1008
function html_buildlist($data,$class,$func,$lifunc='html_li_default',$forcewrapper=false){
1009
    if (count($data) === 0) {
1010
        return '';
1011
    }
1012
1013
    $firstElement = reset($data);
1014
    $start_level = $firstElement['level'];
1015
    $level = $start_level;
1016
    $ret   = '';
1017
    $open  = 0;
1018
1019
    foreach ($data as $item){
1020
1021
        if( $item['level'] > $level ){
1022
            //open new list
1023
            for($i=0; $i<($item['level'] - $level); $i++){
1024
                if ($i) $ret .= "<li class=\"clear\">";
1025
                $ret .= "\n<ul class=\"$class\">\n";
1026
                $open++;
1027
            }
1028
            $level = $item['level'];
1029
1030
        }elseif( $item['level'] < $level ){
1031
            //close last item
1032
            $ret .= "</li>\n";
1033
            while( $level > $item['level'] && $open > 0 ){
1034
                //close higher lists
1035
                $ret .= "</ul>\n</li>\n";
1036
                $level--;
1037
                $open--;
1038
            }
1039
        } elseif ($ret !== '') {
1040
            //close previous item
1041
            $ret .= "</li>\n";
1042
        }
1043
1044
        //print item
1045
        $ret .= call_user_func($lifunc,$item);
1046
        $ret .= '<div class="li">';
1047
1048
        $ret .= call_user_func($func,$item);
1049
        $ret .= '</div>';
1050
    }
1051
1052
    //close remaining items and lists
1053
    $ret .= "</li>\n";
1054
    while($open-- > 0) {
1055
        $ret .= "</ul></li>\n";
1056
    }
1057
1058
    if ($forcewrapper || $start_level < 2) {
1059
        // Trigger building a wrapper ul if the first level is
1060
        // 0 (we have a root object) or 1 (just the root content)
1061
        $ret = "\n<ul class=\"$class\">\n".$ret."</ul>\n";
1062
    }
1063
1064
    return $ret;
1065
}
1066
1067
/**
1068
 * display backlinks
1069
 *
1070
 * @author Andreas Gohr <[email protected]>
1071
 * @author Michael Klier <[email protected]>
1072
 */
1073
function html_backlinks(){
1074
    global $ID;
1075
    global $lang;
1076
1077
    print p_locale_xhtml('backlinks');
1078
1079
    $data = ft_backlinks($ID);
1080
1081
    if(!empty($data)) {
1082
        print '<ul class="idx">';
1083
        foreach($data as $blink){
1084
            print '<li><div class="li">';
1085
            print html_wikilink(':'.$blink,useHeading('navigation')?null:$blink);
1086
            print '</div></li>';
1087
        }
1088
        print '</ul>';
1089
    } else {
1090
        print '<div class="level1"><p>' . $lang['nothingfound'] . '</p></div>';
1091
    }
1092
}
1093
1094
/**
1095
 * Get header of diff HTML
1096
 *
1097
 * @param string $l_rev   Left revisions
1098
 * @param string $r_rev   Right revision
1099
 * @param string $id      Page id, if null $ID is used
1100
 * @param bool   $media   If it is for media files
1101
 * @param bool   $inline  Return the header on a single line
1102
 * @return string[] HTML snippets for diff header
1103
 */
1104
function html_diff_head($l_rev, $r_rev, $id = null, $media = false, $inline = false) {
1105
    global $lang;
1106
    if ($id === null) {
1107
        global $ID;
1108
        $id = $ID;
1109
    }
1110
    $head_separator = $inline ? ' ' : '<br />';
1111
    $media_or_wikiFN = $media ? 'mediaFN' : 'wikiFN';
1112
    $ml_or_wl = $media ? 'ml' : 'wl';
1113
    $l_minor = $r_minor = '';
1114
1115
    if($media) {
1116
        $changelog = new MediaChangeLog($id);
1117
    } else {
1118
        $changelog = new PageChangeLog($id);
1119
    }
1120
    if(!$l_rev){
1121
        $l_head = '&mdash;';
1122
    }else{
1123
        $l_info   = $changelog->getRevisionInfo($l_rev);
1124
        if($l_info['user']){
1125
            $l_user = '<bdi>'.editorinfo($l_info['user']).'</bdi>';
1126
            if(auth_ismanager()) $l_user .= ' <bdo dir="ltr">('.$l_info['ip'].')</bdo>';
1127
        } else {
1128
            $l_user = '<bdo dir="ltr">'.$l_info['ip'].'</bdo>';
1129
        }
1130
        $l_user  = '<span class="user">'.$l_user.'</span>';
1131
        $l_sum   = ($l_info['sum']) ? '<span class="sum"><bdi>'.hsc($l_info['sum']).'</bdi></span>' : '';
1132
        if ($l_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $l_minor = 'class="minor"';
1133
1134
        $l_head_title = ($media) ? dformat($l_rev) : $id.' ['.dformat($l_rev).']';
1135
        $l_head = '<bdi><a class="wikilink1" href="'.$ml_or_wl($id,"rev=$l_rev").'">'.
1136
        $l_head_title.'</a></bdi>'.
1137
        $head_separator.$l_user.' '.$l_sum;
1138
    }
1139
1140
    if($r_rev){
1141
        $r_info   = $changelog->getRevisionInfo($r_rev);
1142
        if($r_info['user']){
1143
            $r_user = '<bdi>'.editorinfo($r_info['user']).'</bdi>';
1144
            if(auth_ismanager()) $r_user .= ' <bdo dir="ltr">('.$r_info['ip'].')</bdo>';
1145
        } else {
1146
            $r_user = '<bdo dir="ltr">'.$r_info['ip'].'</bdo>';
1147
        }
1148
        $r_user = '<span class="user">'.$r_user.'</span>';
1149
        $r_sum  = ($r_info['sum']) ? '<span class="sum"><bdi>'.hsc($r_info['sum']).'</bdi></span>' : '';
1150
        if ($r_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"';
1151
1152
        $r_head_title = ($media) ? dformat($r_rev) : $id.' ['.dformat($r_rev).']';
1153
        $r_head = '<bdi><a class="wikilink1" href="'.$ml_or_wl($id,"rev=$r_rev").'">'.
1154
        $r_head_title.'</a></bdi>'.
1155
        $head_separator.$r_user.' '.$r_sum;
1156
    }elseif($_rev = @filemtime($media_or_wikiFN($id))){
1157
        $_info   = $changelog->getRevisionInfo($_rev);
1158
        if($_info['user']){
1159
            $_user = '<bdi>'.editorinfo($_info['user']).'</bdi>';
1160
            if(auth_ismanager()) $_user .= ' <bdo dir="ltr">('.$_info['ip'].')</bdo>';
1161
        } else {
1162
            $_user = '<bdo dir="ltr">'.$_info['ip'].'</bdo>';
1163
        }
1164
        $_user = '<span class="user">'.$_user.'</span>';
1165
        $_sum  = ($_info['sum']) ? '<span class="sum"><bdi>'.hsc($_info['sum']).'</span></bdi>' : '';
1166
        if ($_info['type']===DOKU_CHANGE_TYPE_MINOR_EDIT) $r_minor = 'class="minor"';
1167
1168
        $r_head_title = ($media) ? dformat($_rev) : $id.' ['.dformat($_rev).']';
1169
        $r_head  = '<bdi><a class="wikilink1" href="'.$ml_or_wl($id).'">'.
1170
        $r_head_title.'</a></bdi> '.
1171
        '('.$lang['current'].')'.
1172
        $head_separator.$_user.' '.$_sum;
1173
    }else{
1174
        $r_head = '&mdash; ('.$lang['current'].')';
1175
    }
1176
1177
    return array($l_head, $r_head, $l_minor, $r_minor);
1178
}
1179
1180
/**
1181
 * Show diff
1182
 * between current page version and provided $text
1183
 * or between the revisions provided via GET or POST
1184
 *
1185
 * @author Andreas Gohr <[email protected]>
1186
 * @param  string $text  when non-empty: compare with this text with most current version
1187
 * @param  bool   $intro display the intro text
1188
 * @param  string $type  type of the diff (inline or sidebyside)
1189
 */
1190
function html_diff($text = '', $intro = true, $type = null) {
1191
    global $ID;
1192
    global $REV;
1193
    global $lang;
1194
    global $INPUT;
1195
    global $INFO;
1196
    $pagelog = new PageChangeLog($ID);
1197
1198
    /*
1199
     * Determine diff type
1200
     */
1201
    if(!$type) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $type of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
1202
        $type = $INPUT->str('difftype');
1203
        if(empty($type)) {
1204
            $type = get_doku_pref('difftype', $type);
1205
            if(empty($type) && $INFO['ismobile']) {
1206
                $type = 'inline';
1207
            }
1208
        }
1209
    }
1210
    if($type != 'inline') $type = 'sidebyside';
1211
1212
    /*
1213
     * Determine requested revision(s)
1214
     */
1215
    // we're trying to be clever here, revisions to compare can be either
1216
    // given as rev and rev2 parameters, with rev2 being optional. Or in an
1217
    // array in rev2.
1218
    $rev1 = $REV;
1219
1220
    $rev2 = $INPUT->ref('rev2');
1221
    if(is_array($rev2)) {
1222
        $rev1 = (int) $rev2[0];
1223
        $rev2 = (int) $rev2[1];
1224
1225
        if(!$rev1) {
1226
            $rev1 = $rev2;
1227
            unset($rev2);
1228
        }
1229
    } else {
1230
        $rev2 = $INPUT->int('rev2');
1231
    }
1232
1233
    /*
1234
     * Determine left and right revision, its texts and the header
1235
     */
1236
    $r_minor = '';
1237
    $l_minor = '';
1238
1239
    if($text) { // compare text to the most current revision
1240
        $l_rev = '';
1241
        $l_text = rawWiki($ID, '');
1242
        $l_head = '<a class="wikilink1" href="' . wl($ID) . '">' .
1243
            $ID . ' ' . dformat((int) @filemtime(wikiFN($ID))) . '</a> ' .
1244
            $lang['current'];
1245
1246
        $r_rev = '';
1247
        $r_text = cleanText($text);
1248
        $r_head = $lang['yours'];
1249
    } else {
1250
        if($rev1 && isset($rev2) && $rev2) { // two specific revisions wanted
1251
            // make sure order is correct (older on the left)
1252
            if($rev1 < $rev2) {
1253
                $l_rev = $rev1;
1254
                $r_rev = $rev2;
1255
            } else {
1256
                $l_rev = $rev2;
1257
                $r_rev = $rev1;
1258
            }
1259
        } elseif($rev1) { // single revision given, compare to current
1260
            $r_rev = '';
1261
            $l_rev = $rev1;
1262
        } else { // no revision was given, compare previous to current
1263
            $r_rev = '';
1264
            $revs = $pagelog->getRevisions(0, 1);
1265
            $l_rev = $revs[0];
1266
            $REV = $l_rev; // store revision back in $REV
1267
        }
1268
1269
        // when both revisions are empty then the page was created just now
1270
        if(!$l_rev && !$r_rev) {
1271
            $l_text = '';
1272
        } else {
1273
            $l_text = rawWiki($ID, $l_rev);
1274
        }
1275
        $r_text = rawWiki($ID, $r_rev);
1276
1277
        list($l_head, $r_head, $l_minor, $r_minor) = html_diff_head($l_rev, $r_rev, null, false, $type == 'inline');
1278
    }
1279
1280
    /*
1281
     * Build navigation
1282
     */
1283
    $l_nav = '';
1284
    $r_nav = '';
1285
    if(!$text) {
1286
        list($l_nav, $r_nav) = html_diff_navigation($pagelog, $type, $l_rev, $r_rev);
1287
    }
1288
    /*
1289
     * Create diff object and the formatter
1290
     */
1291
    $diff = new Diff(explode("\n", $l_text), explode("\n", $r_text));
1292
1293
    if($type == 'inline') {
1294
        $diffformatter = new InlineDiffFormatter();
1295
    } else {
1296
        $diffformatter = new TableDiffFormatter();
1297
    }
1298
    /*
1299
     * Display intro
1300
     */
1301
    if($intro) print p_locale_xhtml('diff');
1302
1303
    /*
1304
     * Display type and exact reference
1305
     */
1306
    if(!$text) {
1307
        ptln('<div class="diffoptions group">');
1308
1309
1310
        $form = new Doku_Form(array('action' => wl()));
1311
        $form->addHidden('id', $ID);
1312
        $form->addHidden('rev2[0]', $l_rev);
1313
        $form->addHidden('rev2[1]', $r_rev);
1314
        $form->addHidden('do', 'diff');
1315
        $form->addElement(
1316
             form_makeListboxField(
1317
                 'difftype',
1318
                 array(
1319
                     'sidebyside' => $lang['diff_side'],
1320
                     'inline' => $lang['diff_inline']
1321
                 ),
1322
                 $type,
1323
                 $lang['diff_type'],
1324
                 '', '',
1325
                 array('class' => 'quickselect')
1326
             )
1327
        );
1328
        $form->addElement(form_makeButton('submit', 'diff', 'Go'));
1329
        $form->printForm();
1330
1331
        ptln('<p>');
1332
        // link to exactly this view FS#2835
1333
        echo html_diff_navigationlink($type, 'difflink', $l_rev, $r_rev ? $r_rev : $INFO['currentrev']);
1334
        ptln('</p>');
1335
1336
        ptln('</div>'); // .diffoptions
1337
    }
1338
1339
    /*
1340
     * Display diff view table
1341
     */
1342
    ?>
1343
    <div class="table">
1344
    <table class="diff diff_<?php echo $type ?>">
1345
1346
        <?php
1347
        //navigation and header
1348
        if($type == 'inline') {
1349
            if(!$text) { ?>
1350
                <tr>
1351
                    <td class="diff-lineheader">-</td>
1352
                    <td class="diffnav"><?php echo $l_nav ?></td>
1353
                </tr>
1354
                <tr>
1355
                    <th class="diff-lineheader">-</th>
1356
                    <th <?php echo $l_minor ?>>
1357
                        <?php echo $l_head ?>
1358
                    </th>
1359
                </tr>
1360
            <?php } ?>
1361
            <tr>
1362
                <td class="diff-lineheader">+</td>
1363
                <td class="diffnav"><?php echo $r_nav ?></td>
1364
            </tr>
1365
            <tr>
1366
                <th class="diff-lineheader">+</th>
1367
                <th <?php echo $r_minor ?>>
1368
                    <?php echo $r_head ?>
1369
                </th>
1370
            </tr>
1371
        <?php } else {
1372
            if(!$text) { ?>
1373
                <tr>
1374
                    <td colspan="2" class="diffnav"><?php echo $l_nav ?></td>
1375
                    <td colspan="2" class="diffnav"><?php echo $r_nav ?></td>
1376
                </tr>
1377
            <?php } ?>
1378
            <tr>
1379
                <th colspan="2" <?php echo $l_minor ?>>
1380
                    <?php echo $l_head ?>
1381
                </th>
1382
                <th colspan="2" <?php echo $r_minor ?>>
1383
                    <?php echo $r_head ?>
1384
                </th>
1385
            </tr>
1386
        <?php }
1387
1388
        //diff view
1389
        echo html_insert_softbreaks($diffformatter->format($diff)); ?>
1390
1391
    </table>
1392
    </div>
1393
<?php
1394
}
1395
1396
/**
1397
 * Create html for revision navigation
1398
 *
1399
 * @param PageChangeLog $pagelog changelog object of current page
1400
 * @param string        $type    inline vs sidebyside
1401
 * @param int           $l_rev   left revision timestamp
1402
 * @param int           $r_rev   right revision timestamp
1403
 * @return string[] html of left and right navigation elements
1404
 */
1405
function html_diff_navigation($pagelog, $type, $l_rev, $r_rev) {
1406
    global $INFO, $ID;
1407
1408
    // last timestamp is not in changelog, retrieve timestamp from metadata
1409
    // note: when page is removed, the metadata timestamp is zero
1410
    if(!$r_rev) {
1411
        if(isset($INFO['meta']['last_change']['date'])) {
1412
            $r_rev = $INFO['meta']['last_change']['date'];
1413
        } else {
1414
            $r_rev = 0;
1415
        }
1416
    }
1417
1418
    //retrieve revisions with additional info
1419
    list($l_revs, $r_revs) = $pagelog->getRevisionsAround($l_rev, $r_rev);
1420
    $l_revisions = array();
1421
    if(!$l_rev) {
1422
        $l_revisions[0] = array(0, "", false); //no left revision given, add dummy
1423
    }
1424
    foreach($l_revs as $rev) {
1425
        $info = $pagelog->getRevisionInfo($rev);
1426
        $l_revisions[$rev] = array(
1427
            $rev,
1428
            dformat($info['date']) . ' ' . editorinfo($info['user'], true) . ' ' . $info['sum'],
1429
            $r_rev ? $rev >= $r_rev : false //disable?
1430
        );
1431
    }
1432
    $r_revisions = array();
1433
    if(!$r_rev) {
1434
        $r_revisions[0] = array(0, "", false); //no right revision given, add dummy
1435
    }
1436
    foreach($r_revs as $rev) {
1437
        $info = $pagelog->getRevisionInfo($rev);
1438
        $r_revisions[$rev] = array(
1439
            $rev,
1440
            dformat($info['date']) . ' ' . editorinfo($info['user'], true) . ' ' . $info['sum'],
1441
            $rev <= $l_rev //disable?
1442
        );
1443
    }
1444
1445
    //determine previous/next revisions
1446
    $l_index = array_search($l_rev, $l_revs);
1447
    $l_prev = $l_revs[$l_index + 1];
1448
    $l_next = $l_revs[$l_index - 1];
1449
    if($r_rev) {
1450
        $r_index = array_search($r_rev, $r_revs);
1451
        $r_prev = $r_revs[$r_index + 1];
1452
        $r_next = $r_revs[$r_index - 1];
1453
    } else {
1454
        //removed page
1455
        if($l_next) {
1456
            $r_prev = $r_revs[0];
1457
        } else {
1458
            $r_prev = null;
1459
        }
1460
        $r_next = null;
1461
    }
1462
1463
    /*
1464
     * Left side:
1465
     */
1466
    $l_nav = '';
1467
    //move back
1468
    if($l_prev) {
1469
        $l_nav .= html_diff_navigationlink($type, 'diffbothprevrev', $l_prev, $r_prev);
1470
        $l_nav .= html_diff_navigationlink($type, 'diffprevrev', $l_prev, $r_rev);
1471
    }
1472
    //dropdown
1473
    $form = new Doku_Form(array('action' => wl()));
1474
    $form->addHidden('id', $ID);
1475
    $form->addHidden('difftype', $type);
1476
    $form->addHidden('rev2[1]', $r_rev);
1477
    $form->addHidden('do', 'diff');
1478
    $form->addElement(
1479
         form_makeListboxField(
1480
             'rev2[0]',
1481
             $l_revisions,
1482
             $l_rev,
1483
             '', '', '',
1484
             array('class' => 'quickselect')
1485
         )
1486
    );
1487
    $form->addElement(form_makeButton('submit', 'diff', 'Go'));
1488
    $l_nav .= $form->getForm();
1489
    //move forward
1490
    if($l_next && ($l_next < $r_rev || !$r_rev)) {
1491
        $l_nav .= html_diff_navigationlink($type, 'diffnextrev', $l_next, $r_rev);
1492
    }
1493
1494
    /*
1495
     * Right side:
1496
     */
1497
    $r_nav = '';
1498
    //move back
1499
    if($l_rev < $r_prev) {
1500
        $r_nav .= html_diff_navigationlink($type, 'diffprevrev', $l_rev, $r_prev);
1501
    }
1502
    //dropdown
1503
    $form = new Doku_Form(array('action' => wl()));
1504
    $form->addHidden('id', $ID);
1505
    $form->addHidden('rev2[0]', $l_rev);
1506
    $form->addHidden('difftype', $type);
1507
    $form->addHidden('do', 'diff');
1508
    $form->addElement(
1509
         form_makeListboxField(
1510
             'rev2[1]',
1511
             $r_revisions,
1512
             $r_rev,
1513
             '', '', '',
1514
             array('class' => 'quickselect')
1515
         )
1516
    );
1517
    $form->addElement(form_makeButton('submit', 'diff', 'Go'));
1518
    $r_nav .= $form->getForm();
1519
    //move forward
1520
    if($r_next) {
1521
        if($pagelog->isCurrentRevision($r_next)) {
1522
            $r_nav .= html_diff_navigationlink($type, 'difflastrev', $l_rev); //last revision is diff with current page
1523
        } else {
1524
            $r_nav .= html_diff_navigationlink($type, 'diffnextrev', $l_rev, $r_next);
1525
        }
1526
        $r_nav .= html_diff_navigationlink($type, 'diffbothnextrev', $l_next, $r_next);
1527
    }
1528
    return array($l_nav, $r_nav);
1529
}
1530
1531
/**
1532
 * Create html link to a diff defined by two revisions
1533
 *
1534
 * @param string $difftype display type
1535
 * @param string $linktype
1536
 * @param int $lrev oldest revision
1537
 * @param int $rrev newest revision or null for diff with current revision
1538
 * @return string html of link to a diff
1539
 */
1540
function html_diff_navigationlink($difftype, $linktype, $lrev, $rrev = null) {
1541
    global $ID, $lang;
1542
    if(!$rrev) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rrev of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
1543
        $urlparam = array(
1544
            'do' => 'diff',
1545
            'rev' => $lrev,
1546
            'difftype' => $difftype,
1547
        );
1548
    } else {
1549
        $urlparam = array(
1550
            'do' => 'diff',
1551
            'rev2[0]' => $lrev,
1552
            'rev2[1]' => $rrev,
1553
            'difftype' => $difftype,
1554
        );
1555
    }
1556
    return  '<a class="' . $linktype . '" href="' . wl($ID, $urlparam) . '" title="' . $lang[$linktype] . '">' .
1557
                '<span>' . $lang[$linktype] . '</span>' .
1558
            '</a>' . "\n";
1559
}
1560
1561
/**
1562
 * Insert soft breaks in diff html
1563
 *
1564
 * @param string $diffhtml
1565
 * @return string
1566
 */
1567
function html_insert_softbreaks($diffhtml) {
1568
    // search the diff html string for both:
1569
    // - html tags, so these can be ignored
1570
    // - long strings of characters without breaking characters
1571
    return preg_replace_callback('/<[^>]*>|[^<> ]{12,}/','html_softbreak_callback',$diffhtml);
1572
}
1573
1574
/**
1575
 * callback which adds softbreaks
1576
 *
1577
 * @param array $match array with first the complete match
1578
 * @return string the replacement
1579
 */
1580
function html_softbreak_callback($match){
1581
    // if match is an html tag, return it intact
1582
    if ($match[0]{0} == '<') return $match[0];
1583
1584
    // its a long string without a breaking character,
1585
    // make certain characters into breaking characters by inserting a
1586
    // breaking character (zero length space, U+200B / #8203) in front them.
1587
    $regex = <<< REGEX
1588
(?(?=              # start a conditional expression with a positive look ahead ...
1589
&\#?\\w{1,6};)     # ... for html entities - we don't want to split them (ok to catch some invalid combinations)
1590
&\#?\\w{1,6};      # yes pattern - a quicker match for the html entity, since we know we have one
1591
|
1592
[?/,&\#;:]         # no pattern - any other group of 'special' characters to insert a breaking character after
1593
)+                 # end conditional expression
1594
REGEX;
1595
1596
    return preg_replace('<'.$regex.'>xu','\0&#8203;',$match[0]);
1597
}
1598
1599
/**
1600
 * show warning on conflict detection
1601
 *
1602
 * @author Andreas Gohr <[email protected]>
1603
 *
1604
 * @param string $text
1605
 * @param string $summary
1606
 */
1607
function html_conflict($text,$summary){
1608
    global $ID;
1609
    global $lang;
1610
1611
    print p_locale_xhtml('conflict');
1612
    $form = new Doku_Form(array('id' => 'dw__editform'));
1613
    $form->addHidden('id', $ID);
1614
    $form->addHidden('wikitext', $text);
1615
    $form->addHidden('summary', $summary);
1616
    $form->addElement(form_makeButton('submit', 'save', $lang['btn_save'], array('accesskey'=>'s')));
1617
    $form->addElement(form_makeButton('submit', 'cancel', $lang['btn_cancel']));
1618
    html_form('conflict', $form);
1619
    print '<br /><br /><br /><br />'.NL;
1620
}
1621
1622
/**
1623
 * Prints the global message array
1624
 *
1625
 * @author Andreas Gohr <[email protected]>
1626
 */
1627
function html_msgarea(){
1628
    global $MSG, $MSG_shown;
1629
    /** @var array $MSG */
1630
    // store if the global $MSG has already been shown and thus HTML output has been started
1631
    $MSG_shown = true;
1632
1633
    if(!isset($MSG)) return;
1634
1635
    $shown = array();
1636
    foreach($MSG as $msg){
1637
        $hash = md5($msg['msg']);
1638
        if(isset($shown[$hash])) continue; // skip double messages
1639
        if(info_msg_allowed($msg)){
1640
            print '<div class="'.$msg['lvl'].'">';
1641
            print $msg['msg'];
1642
            print '</div>';
1643
        }
1644
        $shown[$hash] = 1;
1645
    }
1646
1647
    unset($GLOBALS['MSG']);
1648
}
1649
1650
/**
1651
 * Prints the registration form
1652
 *
1653
 * @author Andreas Gohr <[email protected]>
1654
 */
1655
function html_register(){
1656
    global $lang;
1657
    global $conf;
1658
    global $INPUT;
1659
1660
    $base_attrs = array('size'=>50,'required'=>'required');
1661
    $email_attrs = $base_attrs + array('type'=>'email','class'=>'edit');
1662
1663
    print p_locale_xhtml('register');
1664
    print '<div class="centeralign">'.NL;
1665
    $form = new Doku_Form(array('id' => 'dw__register'));
1666
    $form->startFieldset($lang['btn_register']);
1667
    $form->addHidden('do', 'register');
1668
    $form->addHidden('save', '1');
1669
    $form->addElement(
1670
        form_makeTextField(
1671
            'login',
1672
            $INPUT->post->str('login'),
1673
            $lang['user'],
1674
            '',
1675
            'block',
1676
            $base_attrs
1677
        )
1678
    );
1679
    if (!$conf['autopasswd']) {
1680
        $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', $base_attrs));
1681
        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', $base_attrs));
1682
    }
1683
    $form->addElement(
1684
        form_makeTextField(
1685
            'fullname',
1686
            $INPUT->post->str('fullname'),
1687
            $lang['fullname'],
1688
            '',
1689
            'block',
1690
            $base_attrs
1691
        )
1692
    );
1693
    $form->addElement(
1694
        form_makeField(
1695
            'email',
1696
            'email',
1697
            $INPUT->post->str('email'),
1698
            $lang['email'],
1699
            '',
1700
            'block',
1701
            $email_attrs
1702
        )
1703
    );
1704
    $form->addElement(form_makeButton('submit', '', $lang['btn_register']));
1705
    $form->endFieldset();
1706
    html_form('register', $form);
1707
1708
    print '</div>'.NL;
1709
}
1710
1711
/**
1712
 * Print the update profile form
1713
 *
1714
 * @author Christopher Smith <[email protected]>
1715
 * @author Andreas Gohr <[email protected]>
1716
 */
1717
function html_updateprofile(){
1718
    global $lang;
1719
    global $conf;
1720
    global $INPUT;
1721
    global $INFO;
1722
    /** @var DokuWiki_Auth_Plugin $auth */
1723
    global $auth;
1724
1725
    print p_locale_xhtml('updateprofile');
1726
    print '<div class="centeralign">'.NL;
1727
1728
    $fullname = $INPUT->post->str('fullname', $INFO['userinfo']['name'], true);
1729
    $email = $INPUT->post->str('email', $INFO['userinfo']['mail'], true);
1730
    $form = new Doku_Form(array('id' => 'dw__register'));
1731
    $form->startFieldset($lang['profile']);
1732
    $form->addHidden('do', 'profile');
1733
    $form->addHidden('save', '1');
1734
    $form->addElement(
1735
        form_makeTextField(
1736
            'login',
1737
            $_SERVER['REMOTE_USER'],
1738
            $lang['user'],
1739
            '',
1740
            'block',
1741
            array('size' => '50', 'disabled' => 'disabled')
1742
        )
1743
    );
1744
    $attr = array('size'=>'50');
1745
    if (!$auth->canDo('modName')) $attr['disabled'] = 'disabled';
1746
    $form->addElement(form_makeTextField('fullname', $fullname, $lang['fullname'], '', 'block', $attr));
1747
    $attr = array('size'=>'50', 'class'=>'edit');
1748
    if (!$auth->canDo('modMail')) $attr['disabled'] = 'disabled';
1749
    $form->addElement(form_makeField('email','email', $email, $lang['email'], '', 'block', $attr));
1750
    $form->addElement(form_makeTag('br'));
1751
    if ($auth->canDo('modPass')) {
1752
        $form->addElement(form_makePasswordField('newpass', $lang['newpass'], '', 'block', array('size'=>'50')));
1753
        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
1754
    }
1755
    if ($conf['profileconfirm']) {
1756
        $form->addElement(form_makeTag('br'));
1757
        $form->addElement(
1758
            form_makePasswordField(
1759
                'oldpass',
1760
                $lang['oldpass'],
1761
                '',
1762
                'block',
1763
                array('size' => '50', 'required' => 'required')
1764
            )
1765
        );
1766
    }
1767
    $form->addElement(form_makeButton('submit', '', $lang['btn_save']));
1768
    $form->addElement(form_makeButton('reset', '', $lang['btn_reset']));
1769
1770
    $form->endFieldset();
1771
    html_form('updateprofile', $form);
1772
1773
    if ($auth->canDo('delUser') && actionOK('profile_delete')) {
1774
        $form_profiledelete = new Doku_Form(array('id' => 'dw__profiledelete'));
1775
        $form_profiledelete->startFieldset($lang['profdeleteuser']);
1776
        $form_profiledelete->addHidden('do', 'profile_delete');
1777
        $form_profiledelete->addHidden('delete', '1');
1778
        $form_profiledelete->addElement(
1779
            form_makeCheckboxField(
1780
                'confirm_delete',
1781
                '1',
1782
                $lang['profconfdelete'],
1783
                'dw__confirmdelete',
1784
                '',
1785
                array('required' => 'required')
1786
            )
1787
        );
1788
        if ($conf['profileconfirm']) {
1789
            $form_profiledelete->addElement(form_makeTag('br'));
1790
            $form_profiledelete->addElement(
1791
                form_makePasswordField(
1792
                    'oldpass',
1793
                    $lang['oldpass'],
1794
                    '',
1795
                    'block',
1796
                    array('size' => '50', 'required' => 'required')
1797
                )
1798
            );
1799
        }
1800
        $form_profiledelete->addElement(form_makeButton('submit', '', $lang['btn_deleteuser']));
1801
        $form_profiledelete->endFieldset();
1802
1803
        html_form('profiledelete', $form_profiledelete);
1804
    }
1805
1806
    print '</div>'.NL;
1807
}
1808
1809
/**
1810
 * Preprocess edit form data
1811
 *
1812
 * @author   Andreas Gohr <[email protected]>
1813
 *
1814
 * @triggers HTML_EDITFORM_OUTPUT
1815
 */
1816
function html_edit(){
1817
    global $INPUT;
1818
    global $ID;
1819
    global $REV;
1820
    global $DATE;
1821
    global $PRE;
1822
    global $SUF;
1823
    global $INFO;
1824
    global $SUM;
1825
    global $lang;
1826
    global $conf;
1827
    global $TEXT;
1828
1829
    if ($INPUT->has('changecheck')) {
1830
        $check = $INPUT->str('changecheck');
1831
    } elseif(!$INFO['exists']){
1832
        // $TEXT has been loaded from page template
1833
        $check = md5('');
1834
    } else {
1835
        $check = md5($TEXT);
1836
    }
1837
    $mod = md5($TEXT) !== $check;
1838
1839
    $wr = $INFO['writable'] && !$INFO['locked'];
1840
    $include = 'edit';
1841
    if($wr){
1842
        if ($REV) $include = 'editrev';
1843
    }else{
1844
        // check pseudo action 'source'
1845
        if(!actionOK('source')){
1846
            msg('Command disabled: source',-1);
1847
            return;
1848
        }
1849
        $include = 'read';
1850
    }
1851
1852
    global $license;
1853
1854
    $form = new Doku_Form(array('id' => 'dw__editform'));
1855
    $form->addHidden('id', $ID);
1856
    $form->addHidden('rev', $REV);
1857
    $form->addHidden('date', $DATE);
1858
    $form->addHidden('prefix', $PRE . '.');
1859
    $form->addHidden('suffix', $SUF);
1860
    $form->addHidden('changecheck', $check);
1861
1862
    $data = array('form' => $form,
1863
                  'wr'   => $wr,
1864
                  'media_manager' => true,
1865
                  'target' => ($INPUT->has('target') && $wr) ? $INPUT->str('target') : 'section',
1866
                  'intro_locale' => $include);
1867
1868
    if ($data['target'] !== 'section') {
1869
        // Only emit event if page is writable, section edit data is valid and
1870
        // edit target is not section.
1871
        trigger_event('HTML_EDIT_FORMSELECTION', $data, 'html_edit_form', true);
1872
    } else {
1873
        html_edit_form($data);
1874
    }
1875
    if (isset($data['intro_locale'])) {
1876
        echo p_locale_xhtml($data['intro_locale']);
1877
    }
1878
1879
    $form->addHidden('target', $data['target']);
1880
    if ($INPUT->has('hid')) {
1881
        $form->addHidden('hid', $INPUT->str('hid'));
1882
    }
1883
    if ($INPUT->has('codeblockOffset')) {
1884
        $form->addHidden('codeblockOffset', $INPUT->str('codeblockOffset'));
1885
    }
1886
    $form->addElement(form_makeOpenTag('div', array('id'=>'wiki__editbar', 'class'=>'editBar')));
1887
    $form->addElement(form_makeOpenTag('div', array('id'=>'size__ctl')));
1888
    $form->addElement(form_makeCloseTag('div'));
1889
    if ($wr) {
1890
        $form->addElement(form_makeOpenTag('div', array('class'=>'editButtons')));
1891
        $form->addElement(
1892
            form_makeButton(
1893
                'submit',
1894
                'save',
1895
                $lang['btn_save'],
1896
                array('id' => 'edbtn__save', 'accesskey' => 's', 'tabindex' => '4')
1897
            )
1898
        );
1899
        $form->addElement(
1900
            form_makeButton(
1901
                'submit',
1902
                'preview',
1903
                $lang['btn_preview'],
1904
                array('id' => 'edbtn__preview', 'accesskey' => 'p', 'tabindex' => '5')
1905
            )
1906
        );
1907
        $form->addElement(form_makeButton('submit', 'draftdel', $lang['btn_cancel'], array('tabindex'=>'6')));
1908
        $form->addElement(form_makeCloseTag('div'));
1909
        $form->addElement(form_makeOpenTag('div', array('class'=>'summary')));
1910
        $form->addElement(
1911
            form_makeTextField(
1912
                'summary',
1913
                $SUM,
1914
                $lang['summary'],
1915
                'edit__summary',
1916
                'nowrap',
1917
                array('size' => '50', 'tabindex' => '2')
1918
            )
1919
        );
1920
        $elem = html_minoredit();
1921
        if ($elem) $form->addElement($elem);
1922
        $form->addElement(form_makeCloseTag('div'));
1923
    }
1924
    $form->addElement(form_makeCloseTag('div'));
1925
    if($wr && $conf['license']){
1926
        $form->addElement(form_makeOpenTag('div', array('class'=>'license')));
1927
        $out  = $lang['licenseok'];
1928
        $out .= ' <a href="'.$license[$conf['license']]['url'].'" rel="license" class="urlextern"';
1929
        if($conf['target']['extern']) $out .= ' target="'.$conf['target']['extern'].'"';
1930
        $out .= '>'.$license[$conf['license']]['name'].'</a>';
1931
        $form->addElement($out);
1932
        $form->addElement(form_makeCloseTag('div'));
1933
    }
1934
1935
    if ($wr) {
1936
        // sets changed to true when previewed
1937
        echo '<script type="text/javascript">/*<![CDATA[*/'. NL;
1938
        echo 'textChanged = ' . ($mod ? 'true' : 'false');
1939
        echo '/*!]]>*/</script>' . NL;
1940
    } ?>
1941
    <div class="editBox" role="application">
1942
1943
    <div class="toolbar group">
1944
        <div id="draft__status" class="draft__status"><?php
1945
            if(!empty($INFO['draft'])) echo $lang['draftdate'].' '.dformat();
1946
            ?></div>
1947
        <div id="tool__bar" class="tool__bar"><?php
1948
            if ($wr && $data['media_manager']){
1949
                ?><a href="<?php echo DOKU_BASE?>lib/exe/mediamanager.php?ns=<?php echo $INFO['namespace']?>"
1950
                target="_blank"><?php echo $lang['mediaselect'] ?></a><?php
1951
            }?></div>
1952
    </div>
1953
    <?php
1954
1955
    html_form('edit', $form);
1956
    print '</div>'.NL;
1957
}
1958
1959
/**
1960
 * Display the default edit form
1961
 *
1962
 * Is the default action for HTML_EDIT_FORMSELECTION.
1963
 *
1964
 * @param mixed[] $param
1965
 */
1966
function html_edit_form($param) {
1967
    global $TEXT;
1968
1969
    if ($param['target'] !== 'section') {
1970
        msg('No editor for edit target ' . hsc($param['target']) . ' found.', -1);
1971
    }
1972
1973
    $attr = array('tabindex'=>'1');
1974
    if (!$param['wr']) $attr['readonly'] = 'readonly';
1975
1976
    $param['form']->addElement(form_makeWikiText($TEXT, $attr));
1977
}
1978
1979
/**
1980
 * Adds a checkbox for minor edits for logged in users
1981
 *
1982
 * @author Andreas Gohr <[email protected]>
1983
 *
1984
 * @return array|bool
1985
 */
1986
function html_minoredit(){
1987
    global $conf;
1988
    global $lang;
1989
    global $INPUT;
1990
    // minor edits are for logged in users only
1991
    if(!$conf['useacl'] || !$_SERVER['REMOTE_USER']){
1992
        return false;
1993
    }
1994
1995
    $p = array();
1996
    $p['tabindex'] = 3;
1997
    if($INPUT->bool('minor')) $p['checked']='checked';
1998
    return form_makeCheckboxField('minor', '1', $lang['minoredit'], 'minoredit', 'nowrap', $p);
1999
}
2000
2001
/**
2002
 * prints some debug info
2003
 *
2004
 * @author Andreas Gohr <[email protected]>
2005
 */
2006
function html_debug(){
2007
    global $conf;
2008
    global $lang;
2009
    /** @var DokuWiki_Auth_Plugin $auth */
2010
    global $auth;
2011
    global $INFO;
2012
2013
    //remove sensitive data
2014
    $cnf = $conf;
2015
    debug_guard($cnf);
2016
    $nfo = $INFO;
2017
    debug_guard($nfo);
2018
    $ses = $_SESSION;
2019
    debug_guard($ses);
2020
2021
    print '<html><body>';
2022
2023
    print '<p>When reporting bugs please send all the following ';
2024
    print 'output as a mail to [email protected] ';
2025
    print 'The best way to do this is to save this page in your browser</p>';
2026
2027
    print '<b>$INFO:</b><pre>';
2028
    print_r($nfo);
2029
    print '</pre>';
2030
2031
    print '<b>$_SERVER:</b><pre>';
2032
    print_r($_SERVER);
2033
    print '</pre>';
2034
2035
    print '<b>$conf:</b><pre>';
2036
    print_r($cnf);
2037
    print '</pre>';
2038
2039
    print '<b>DOKU_BASE:</b><pre>';
2040
    print DOKU_BASE;
2041
    print '</pre>';
2042
2043
    print '<b>abs DOKU_BASE:</b><pre>';
2044
    print DOKU_URL;
2045
    print '</pre>';
2046
2047
    print '<b>rel DOKU_BASE:</b><pre>';
2048
    print dirname($_SERVER['PHP_SELF']).'/';
2049
    print '</pre>';
2050
2051
    print '<b>PHP Version:</b><pre>';
2052
    print phpversion();
2053
    print '</pre>';
2054
2055
    print '<b>locale:</b><pre>';
2056
    print setlocale(LC_ALL,0);
2057
    print '</pre>';
2058
2059
    print '<b>encoding:</b><pre>';
2060
    print $lang['encoding'];
2061
    print '</pre>';
2062
2063
    if($auth){
2064
        print '<b>Auth backend capabilities:</b><pre>';
2065
        foreach ($auth->getCapabilities() as $cando){
2066
            print '   '.str_pad($cando,16) . ' => ' . (int)$auth->canDo($cando) . NL;
2067
        }
2068
        print '</pre>';
2069
    }
2070
2071
    print '<b>$_SESSION:</b><pre>';
2072
    print_r($ses);
2073
    print '</pre>';
2074
2075
    print '<b>Environment:</b><pre>';
2076
    print_r($_ENV);
2077
    print '</pre>';
2078
2079
    print '<b>PHP settings:</b><pre>';
2080
    $inis = ini_get_all();
2081
    print_r($inis);
2082
    print '</pre>';
2083
2084
    if (function_exists('apache_get_version')) {
2085
        $apache = array();
2086
        $apache['version'] = apache_get_version();
2087
2088
        if (function_exists('apache_get_modules')) {
2089
            $apache['modules'] = apache_get_modules();
2090
        }
2091
        print '<b>Apache</b><pre>';
2092
        print_r($apache);
2093
        print '</pre>';
2094
    }
2095
2096
    print '</body></html>';
2097
}
2098
2099
/**
2100
 * Form to request a new password for an existing account
2101
 *
2102
 * @author Benoit Chesneau <[email protected]>
2103
 * @author Andreas Gohr <[email protected]>
2104
 */
2105
function html_resendpwd() {
2106
    global $lang;
2107
    global $conf;
2108
    global $INPUT;
2109
2110
    $token = preg_replace('/[^a-f0-9]+/','',$INPUT->str('pwauth'));
2111
2112
    if(!$conf['autopasswd'] && $token){
2113
        print p_locale_xhtml('resetpwd');
2114
        print '<div class="centeralign">'.NL;
2115
        $form = new Doku_Form(array('id' => 'dw__resendpwd'));
2116
        $form->startFieldset($lang['btn_resendpwd']);
2117
        $form->addHidden('token', $token);
2118
        $form->addHidden('do', 'resendpwd');
2119
2120
        $form->addElement(form_makePasswordField('pass', $lang['pass'], '', 'block', array('size'=>'50')));
2121
        $form->addElement(form_makePasswordField('passchk', $lang['passchk'], '', 'block', array('size'=>'50')));
2122
2123
        $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
2124
        $form->endFieldset();
2125
        html_form('resendpwd', $form);
2126
        print '</div>'.NL;
2127
    }else{
2128
        print p_locale_xhtml('resendpwd');
2129
        print '<div class="centeralign">'.NL;
2130
        $form = new Doku_Form(array('id' => 'dw__resendpwd'));
2131
        $form->startFieldset($lang['resendpwd']);
2132
        $form->addHidden('do', 'resendpwd');
2133
        $form->addHidden('save', '1');
2134
        $form->addElement(form_makeTag('br'));
2135
        $form->addElement(form_makeTextField('login', $INPUT->post->str('login'), $lang['user'], '', 'block'));
2136
        $form->addElement(form_makeTag('br'));
2137
        $form->addElement(form_makeTag('br'));
2138
        $form->addElement(form_makeButton('submit', '', $lang['btn_resendpwd']));
2139
        $form->endFieldset();
2140
        html_form('resendpwd', $form);
2141
        print '</div>'.NL;
2142
    }
2143
}
2144
2145
/**
2146
 * Return the TOC rendered to XHTML
2147
 *
2148
 * @author Andreas Gohr <[email protected]>
2149
 *
2150
 * @param array $toc
2151
 * @return string html
2152
 */
2153
function html_TOC($toc){
2154
    if(!count($toc)) return '';
2155
    global $lang;
2156
    $out  = '<!-- TOC START -->'.DOKU_LF;
2157
    $out .= '<div id="dw__toc" class="dw__toc">'.DOKU_LF;
2158
    $out .= '<h3 class="toggle">';
2159
    $out .= $lang['toc'];
2160
    $out .= '</h3>'.DOKU_LF;
2161
    $out .= '<div>'.DOKU_LF;
2162
    $out .= html_buildlist($toc,'toc','html_list_toc','html_li_default',true);
2163
    $out .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
2164
    $out .= '<!-- TOC END -->'.DOKU_LF;
2165
    return $out;
2166
}
2167
2168
/**
2169
 * Callback for html_buildlist
2170
 *
2171
 * @param array $item
2172
 * @return string html
2173
 */
2174
function html_list_toc($item){
2175
    if(isset($item['hid'])){
2176
        $link = '#'.$item['hid'];
2177
    }else{
2178
        $link = $item['link'];
2179
    }
2180
2181
    return '<a href="'.$link.'">'.hsc($item['title']).'</a>';
2182
}
2183
2184
/**
2185
 * Helper function to build TOC items
2186
 *
2187
 * Returns an array ready to be added to a TOC array
2188
 *
2189
 * @param string $link  - where to link (if $hash set to '#' it's a local anchor)
2190
 * @param string $text  - what to display in the TOC
2191
 * @param int    $level - nesting level
2192
 * @param string $hash  - is prepended to the given $link, set blank if you want full links
2193
 * @return array the toc item
2194
 */
2195
function html_mktocitem($link, $text, $level, $hash='#'){
2196
    return  array( 'link'  => $hash.$link,
2197
            'title' => $text,
2198
            'type'  => 'ul',
2199
            'level' => $level);
2200
}
2201
2202
/**
2203
 * Output a Doku_Form object.
2204
 * Triggers an event with the form name: HTML_{$name}FORM_OUTPUT
2205
 *
2206
 * @author Tom N Harris <[email protected]>
2207
 *
2208
 * @param string     $name The name of the form
2209
 * @param Doku_Form  $form The form
2210
 */
2211
function html_form($name, &$form) {
2212
    // Safety check in case the caller forgets.
2213
    $form->endFieldset();
2214
    trigger_event('HTML_'.strtoupper($name).'FORM_OUTPUT', $form, 'html_form_output', false);
2215
}
2216
2217
/**
2218
 * Form print function.
2219
 * Just calls printForm() on the data object.
2220
 *
2221
 * @param Doku_Form $data The form
2222
 */
2223
function html_form_output($data) {
2224
    $data->printForm();
2225
}
2226
2227
/**
2228
 * Embed a flash object in HTML
2229
 *
2230
 * This will create the needed HTML to embed a flash movie in a cross browser
2231
 * compatble way using valid XHTML
2232
 *
2233
 * The parameters $params, $flashvars and $atts need to be associative arrays.
2234
 * No escaping needs to be done for them. The alternative content *has* to be
2235
 * escaped because it is used as is. If no alternative content is given
2236
 * $lang['noflash'] is used.
2237
 *
2238
 * @author Andreas Gohr <[email protected]>
2239
 * @link   http://latrine.dgx.cz/how-to-correctly-insert-a-flash-into-xhtml
2240
 *
2241
 * @param string $swf      - the SWF movie to embed
2242
 * @param int $width       - width of the flash movie in pixels
2243
 * @param int $height      - height of the flash movie in pixels
2244
 * @param array $params    - additional parameters (<param>)
2245
 * @param array $flashvars - parameters to be passed in the flashvar parameter
2246
 * @param array $atts      - additional attributes for the <object> tag
2247
 * @param string $alt      - alternative content (is NOT automatically escaped!)
2248
 * @return string         - the XHTML markup
2249
 */
2250
function html_flashobject($swf,$width,$height,$params=null,$flashvars=null,$atts=null,$alt=''){
2251
    global $lang;
2252
2253
    $out = '';
2254
2255
    // prepare the object attributes
2256
    if(is_null($atts)) $atts = array();
2257
    $atts['width']  = (int) $width;
2258
    $atts['height'] = (int) $height;
2259
    if(!$atts['width'])  $atts['width']  = 425;
2260
    if(!$atts['height']) $atts['height'] = 350;
2261
2262
    // add object attributes for standard compliant browsers
2263
    $std = $atts;
2264
    $std['type'] = 'application/x-shockwave-flash';
2265
    $std['data'] = $swf;
2266
2267
    // add object attributes for IE
2268
    $ie  = $atts;
2269
    $ie['classid'] = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
2270
2271
    // open object (with conditional comments)
2272
    $out .= '<!--[if !IE]> -->'.NL;
2273
    $out .= '<object '.buildAttributes($std).'>'.NL;
2274
    $out .= '<!-- <![endif]-->'.NL;
2275
    $out .= '<!--[if IE]>'.NL;
2276
    $out .= '<object '.buildAttributes($ie).'>'.NL;
2277
    $out .= '    <param name="movie" value="'.hsc($swf).'" />'.NL;
2278
    $out .= '<!--><!-- -->'.NL;
2279
2280
    // print params
2281
    if(is_array($params)) foreach($params as $key => $val){
2282
        $out .= '  <param name="'.hsc($key).'" value="'.hsc($val).'" />'.NL;
2283
    }
2284
2285
    // add flashvars
2286
    if(is_array($flashvars)){
2287
        $out .= '  <param name="FlashVars" value="'.buildURLparams($flashvars).'" />'.NL;
2288
    }
2289
2290
    // alternative content
2291
    if($alt){
2292
        $out .= $alt.NL;
2293
    }else{
2294
        $out .= $lang['noflash'].NL;
2295
    }
2296
2297
    // finish
2298
    $out .= '</object>'.NL;
2299
    $out .= '<!-- <![endif]-->'.NL;
2300
2301
    return $out;
2302
}
2303
2304
/**
2305
 * Prints HTML code for the given tab structure
2306
 *
2307
 * @param array  $tabs        tab structure
2308
 * @param string $current_tab the current tab id
2309
 */
2310
function html_tabs($tabs, $current_tab = null) {
2311
    echo '<ul class="tabs">'.NL;
2312
2313
    foreach($tabs as $id => $tab) {
2314
        html_tab($tab['href'], $tab['caption'], $id === $current_tab);
2315
    }
2316
2317
    echo '</ul>'.NL;
2318
}
2319
2320
/**
2321
 * Prints a single tab
2322
 *
2323
 * @author Kate Arzamastseva <[email protected]>
2324
 * @author Adrian Lang <[email protected]>
2325
 *
2326
 * @param string $href - tab href
2327
 * @param string $caption - tab caption
2328
 * @param boolean $selected - is tab selected
2329
 */
2330
2331
function html_tab($href, $caption, $selected=false) {
2332
    $tab = '<li>';
2333
    if ($selected) {
2334
        $tab .= '<strong>';
2335
    } else {
2336
        $tab .= '<a href="' . hsc($href) . '">';
2337
    }
2338
    $tab .= hsc($caption)
2339
         .  '</' . ($selected ? 'strong' : 'a') . '>'
2340
         .  '</li>'.NL;
2341
    echo $tab;
2342
}
2343
2344
/**
2345
 * Display size change
2346
 *
2347
 * @param int $sizechange - size of change in Bytes
2348
 * @param Doku_Form $form - form to add elements to
2349
 */
2350
2351
function html_sizechange($sizechange, Doku_Form $form) {
2352
    if(isset($sizechange)) {
2353
        $class = 'sizechange';
2354
        $value = filesize_h(abs($sizechange));
2355
        if($sizechange > 0) {
2356
            $class .= ' positive';
2357
            $value = '+' . $value;
2358
        } elseif($sizechange < 0) {
2359
            $class .= ' negative';
2360
            $value = '-' . $value;
2361
        } else {
2362
            $value = '±' . $value;
2363
        }
2364
        $form->addElement(form_makeOpenTag('span', array('class' => $class)));
2365
        $form->addElement($value);
2366
        $form->addElement(form_makeCloseTag('span'));
2367
    }
2368
}
2369