File: Chora.php

package info (click to toggle)
chora2 2.1.1%2Bdebian0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,928 kB
  • ctags: 351
  • sloc: php: 1,479; xml: 384; makefile: 66
file content (642 lines) | stat: -rw-r--r-- 21,624 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
<?php
/**
 * Chora Base Class.
 *
 * $Horde: chora/lib/Chora.php,v 1.72.6.8 2008/10/09 17:40:05 jan Exp $
 *
 * @author  Anil Madhavapeddy <avsm@horde.org>
 * @package Chora
 */
class Chora {

    /**
     * Return a text description of how long its been since the file
     * has been last modified.
     *
     * @param integer $date  Number of seconds since epoch we wish to display.
     * @param boolean $long  If true, display a more verbose date.
     *
     * @return string  The human-readable date.
     */
    function readableTime($date, $long = false)
    {
        static $time, $desc, $breaks;

        /* Initialize popular variables. */
        if (is_null($time)) {
            $time = time();
            $desc = array(1 => array(_("second"), _("seconds")),
                          60 => array(_("minute"), _("minutes")),
                          3600 => array(_("hour"), _("hours")),
                          86400 => array(_("day"), _("days")),
                          604800 => array(_("week"), _("weeks")),
                          2628000 => array(_("month"), _("months")),
                          31536000 => array(_("year"), _("years")));
            $breaks = array_keys($desc);
        }

        $i = count($breaks);
        $secs = $time - $date;

        if ($secs < 2) {
            return _("very little time");
        }

        while (--$i && $i && $breaks[$i] * 2 > $secs);

        $break = $breaks[$i];

        $val = (int)($secs / $break);
        $retval = $val . ' ' . ($val > 1 ? $desc[$break][1] : $desc[$break][0]);
        if ($long && $i > 0) {
            $rest = $secs % $break;
            $break = $breaks[--$i];
            $rest = (int)($rest / $break);
            if ($rest > 0) {
                $resttime = $rest . ' ' . ($rest > 1 ? $desc[$break][1] : $desc[$break][0]);
                $retval .= ', ' . $resttime;
            }
        }

        return $retval;
    }

    /**
     * Initialize global variables and objects.
     */
    function initialize()
    {
        global $acts, $defaultActs, $conf, $where, $atdir,
            $fullname, $prefs, $sourceroot, $scriptName;

        $sourceroots = Chora::sourceroots();
        /**
         * Variables we wish to propagate across web pages
         *  sbt = Sort By Type (name, age, author, etc)
         *  ha  = Hide Attic Files
         *  ord = Sort order
         *
         * Obviously, defaults go into $defaultActs :)
         * TODO: defaults of 1 will not get propagated correctly - avsm
         * XXX: Rewrite this propagation code, since it sucks - avsm
         */
        $defaultActs = array('sbt' => constant($conf['options']['defaultsort']),
                             'sa'  => 0,
                             'ord' => VC_SORT_ASCENDING,
                             'ws'  => 1);

        /* Use the last sourceroot used as the default value if the user
         * has that preference. */
        $remember_last_file = $prefs->getValue('remember_last_file');
        if ($remember_last_file) {
            $last_file = $prefs->getValue('last_file') ? $prefs->getValue('last_file') : null;
            $last_sourceroot = $prefs->getValue('last_sourceroot') ? $prefs->getValue('last_sourceroot') : null;
        }

        if ($remember_last_file && !empty($last_sourceroot) &&
            is_array(@$sourceroots[$last_sourceroot])) {
            $defaultActs['rt'] = $last_sourceroot;
        } else {
            foreach ($sourceroots as $key => $val) {
                if (isset($val['default']) || !isset($defaultActs['rt'])) {
                    $defaultActs['rt'] = $key;
                }
            }
        }

        /* See if any have been passed as GET variables, and if so,
         * assign them into the acts array. */
        $acts = array();
        foreach ($defaultActs as $key => $default) {
            $acts[$key] = Util::getFormData($key, $default);
        }

        if (!isset($sourceroots[$acts['rt']])) {
            Chora::fatal(404, 'Malformed URL');
        }

        $sourcerootopts = $sourceroots[$acts['rt']];
        $sourceroot = $acts['rt'];

        $conf['paths']['temp'] = Horde::getTempDir();
        $GLOBALS['VC'] = &VC::factory($sourcerootopts['type'],
                                      array('sourceroot' => $sourcerootopts['location'],
                                            'paths' => $conf['paths'],
                                            'username' => isset($sourcerootopts['username']) ? $sourcerootopts['username'] : '',
                                            'password' => isset($sourcerootopts['password']) ? $sourcerootopts['password'] : ''));

        $conf['paths']['sourceroot'] = $sourcerootopts['location'];
        $conf['paths']['cvsusers'] = $sourcerootopts['location'] . '/' . (isset($sourcerootopts['cvsusers']) ? $sourcerootopts['cvsusers'] : '');
        $conf['paths']['introText'] = CHORA_BASE . '/config/' . (isset($sourcerootopts['intro']) ? $sourcerootopts['intro'] : '');
        $conf['options']['introTitle'] = isset($sourcerootopts['title']) ? $sourcerootopts['title'] : '';
        $conf['options']['sourceRootName'] = $sourcerootopts['name'];

        $where = Util::getFormData('f', '');
        if ($where == '') {
            $where = '/';
        }

        /* Location relative to the SOURCEROOT. */
        $where = preg_replace('|^/|', '', $where);
        $where = preg_replace('|\.\.|', '', $where);

        /* Location of this script (e.g. /chora/browse.php). */
        $scriptName = preg_replace('|^/?|', '/', $_SERVER['PHP_SELF']);
        $scriptName = preg_replace('|/$|', '', $scriptName);

        /* Store last file/repository viewed, and set 'where' to
         * last_file if necessary. */
        if ($remember_last_file) {
            if (!isset($_SESSION['chora']['login'])) {
                $_SESSION['chora']['login'] = 0;
            }

            /* We store last_sourceroot and last_file only when we have
             * already displayed at least one page. */
            if (!empty($_SESSION['chora']['login'])) {
                $prefs->setValue('last_sourceroot', $acts['rt']);
                $prefs->setValue('last_file', $where);
            } else {
                /* We are displaying the first page. */
                if ($last_file && !$where) {
                    $where = $last_file;
                }
                $_SESSION['chora']['login'] = 1;
            }
        }

        $fullname = $sourcerootopts['location'] . (substr($sourcerootopts['location'], -1) == '/' ? '' : '/') . $where;

        if ($sourcerootopts['type'] == 'cvs') {
            $fullname = preg_replace('|/$|', '', $fullname);
            $atdir = @is_dir($fullname);
        } else {
            $atdir = !$where || (substr($where, -1) == '/');
        }
        $where = preg_replace('|/$|', '', $where);

        if ($sourcerootopts['type'] == 'cvs' && !@is_dir($sourcerootopts['location'])) {
            Chora::fatal('500 Internal Server Error', _("SourceRoot not found! This could be a misconfiguration by the server administrator, or the server could be having temporary problems. Please try again later."));
        }

        if (Chora::isRestricted($where)) {
            Chora::fatal('403 Forbidden', "$where: Forbidden by server configuration");
        }
    }

    function whereMenu()
    {
        global $where, $atdir;

        $bar = '';
        $wherePath = '';

        $dirs = explode('/', $where);
        $last = count($dirs) - 1;
        $i = 0;
        foreach ($dirs as $dir) {
            if (!$atdir && $i++ == $last) {
                $wherePath .= "/$dir";
            } else {
                $wherePath .= "/$dir/";
            }
            $wherePath = str_replace('//', '/', $wherePath);
            if (!empty($dir) && ($dir != 'Attic')) {
                $bar .= '/ <a href="' . Chora::url('', $wherePath) . '">'. Text::htmlallspaces($dir) . '</a> ';
            }
        }
        return $bar;
    }

    /**
     * Output an error page.
     *
     * @param string $errcode  The HTTP error number and text.
     * @param string $errmsg   The verbose error message to be displayed.
     */
    function fatal($errcode, $errmsg)
    {
        if (defined('CHORA_ERROR_HANDLER') && constant('CHORA_ERROR_HANDLER')) {
            return;
        }

        global $registry, $conf, $notification, $browser, $prefs;

        /* Don't store the bad file in the user's preferences. */
        $prefs->setValue('last_file', '');

        $notification->push($errcode . ': ' . $errmsg, 'horde.error');
        require CHORA_TEMPLATES . '/common-header.inc';
        require CHORA_TEMPLATES . '/menu.inc';
        require $registry->get('templates', 'horde') . '/common-footer.inc';
        exit;
    }

    /**
     * Given a return object from a VC:: call, make sure
     * that it's not a PEAR_Error object.
     *
     * @param mixed $e  Return object from a VC:: call.
     */
    function checkError($e)
    {
        if (is_a($e, 'PEAR_Error')) {
            Chora::fatal($e->getCode(), $e->getMessage());
        }
    }

    /**
     * Convert a commit-name into whatever the user wants.
     *
     * @param string $name  Account name.
     *
     * @return string  The transformed name.
     */
    function showAuthorName($name, $fullname = false)
    {
        static $users = null;
        if ($users === null) {
            $users = $GLOBALS['VC']->getUsers($GLOBALS['conf']['paths']['cvsusers']);
        }

        if (is_array($users) && isset($users[$name])) {
            return '<a href="mailto:' . htmlspecialchars($users[$name]['mail']) . '">' .
                htmlspecialchars($fullname ? $users[$name]['name'] : $name) .
                '</a>' . ($fullname ? ' <em>' . htmlspecialchars($name) . '</em>' : '');
        }
        return htmlspecialchars($name);
    }

    /**
     * Generate a URL that links into Chora.
     *
     * @param string $script  Name of the Chora script to link into
     * @param string $uri     The path being browsed.
     * @param array  $args    Key/value pair of any GET parameters to append
     * @param string $anchor  Anchor entity name
     *
     * @return string  The URL, with session information if necessary.
     */
    function url($script = '', $uri = '', $args = array(), $anchor = '')
    {
        global $conf, $acts, $defaultActs;

        $differing = array();
        foreach ($acts as $key => $val) {
            if ($val != $defaultActs[$key]) {
                $differing[$key] = $val;
            }
        }

        $arglist = array_merge($differing, $args);
        $script = $script ? $script . '.php' : 'browse.php';

        if ($conf['options']['urls'] == 'rewrite') {
            if ($script == 'browse.php') {
                $script = $uri;
                if (substr($script, 0, 1) == '/') {
                    $script = substr($script, 1);
                }
            } else {
                $script .= '/' . $uri;
            }
        } else {
            $arglist['f'] = $uri;
        }

        $url = Util::addParameter(Horde::applicationUrl($script), $arglist);
        if (!empty($anchor)) {
            $url .= "#$anchor";
        }

        return $url;
    }

    /**
     * Generates hidden form fields with all required parameters.
     *
     * @param array  $args    Key/value pair of any POST parameters to append
     *
     * @return string  The form fields, with session information if necessary.
     */
    function formInputs($args = array())
    {
        global $conf, $acts, $defaultActs;

        $differing = array();
        foreach ($acts as $key => $val) {
            if ($val != $defaultActs[$key]) {
                $differing[$key] = $val;
            }
        }

        $arglist = array_merge($differing, $args);

        $fields = Util::formInput();
        foreach ($arglist as $key => $val) {
            $fields .= '<input type="hidden" name="' . htmlspecialchars($key)
                . '" value="' . htmlspecialchars($val) . '" />';
        }

        return $fields;
    }

    /**
     * Returns the entries of $sourceroots that the current user has access to.
     *
     * @return array  The sourceroots that the current user has access to.
     */
    function sourceroots()
    {
        global $perms, $sourceroot, $sourceroots;

        $arr = array();
        foreach ($sourceroots as $key => $val) {
            if (!$perms->exists('chora:sourceroots:' . $key) ||
                 $perms->hasPermission('chora:sourceroots:' . $key,
                                       Auth::getAuth(),
                                       PERMS_READ | PERMS_SHOW)) {
                $arr[$key] = $val;
            }
        }

        return $arr;
    }

    /**
     * Generate a list of repositories available from this
     * installation of Chora.
     *
     * @return string  XHTML code representing links to the repositories.
     */
    function repositories()
    {
        $sourceroots = Chora::sourceroots();
        $num_repositories = count($sourceroots);
        if ($num_repositories == 1) {
            return '';
        }

        $arr = array();
        foreach ($sourceroots as $key => $val) {
            if ($GLOBALS['sourceroot'] != $key) {
                $arr[] = '<option value="' .
                    Chora::url('', '', array('rt' => $key)) .
                    '">' . $val['name'] . '</option>';
            }
        }

        return
            '<form action="#" id="repository-picker">'
            . '<select onchange="location.href=this[this.selectedIndex].value">'
            . '<option value="">' . _("Change repositories:") . '</option>'
            . implode(' , ', $arr) . '</select></form>';
    }

    /**
     * Pretty-print the checked out copy, using the
     * Horde::Mime::Viewer package.
     *
     * @param string $mime_type File extension of the checked out file
     * @param resource fp File pointer to the head of the checked out copy
     * @return object The MIME_Viewer object which can be rendered or
     *                false on failure
     */
    function &pretty($mime_type, $fp)
    {
        $lns = '';
        while ($ln = fread($fp, 8192)) {
            $lns .= $ln;
        }

        $mime = &new MIME_Part($mime_type, $lns);
        $viewer = &MIME_Viewer::factory($mime);
        return $viewer;
    }

    /**
     * Check if the given item is restricted from being shown.
     * @return boolean whether or not the item is allowed to be displayed
     **/
    function isRestricted($item)
    {
        global $conf, $perms, $sourceroots, $sourceroot;
        static $restricted;

        // First check if the current user has access to this repository.
        if ($perms->exists('chora:sourceroots:' . $sourceroot) &&
            !$perms->hasPermission('chora:sourceroots:' . $sourceroot,
                                   Auth::getAuth(),
                                   PERMS_READ | PERMS_SHOW)) {
            return true;
        }

        if (!isset($restricted)) {
            $restricted = array();
            if (isset($conf['restrictions']) && is_array($conf['restrictions'])) {
                $restricted = $conf['restrictions'];
            }

            foreach ($sourceroots as $key => $val) {
                if ($sourceroot == $key) {
                    if (isset($val['restrictions']) && is_array($val['restrictions'])) {
                        $restricted = array_merge($restricted, $val['restrictions']);
                        break;
                    }
                }
            }
        }

        if (!empty($restricted) && is_array($restricted) && count($restricted)) {
            for ($i = 0; $i < count($restricted); $i++) {
                if (preg_match('|' . str_replace('|', '\|', $restricted[$i]) . '|', $item)) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * Build Chora's list of menu items.
     */
    function getMenu($returnType = 'object')
    {
        require_once 'Horde/Menu.php';

        $menu = &new Menu();
        $menu->add(Chora::url(), _("_Browse"), 'chora.png');

        if ($returnType == 'object') {
            return $menu;
        } else {
            return $menu->render();
        }
    }

    /**
     */
    function getFileViews()
    {
        global $where;

        $views = array();
        $current = str_replace('.php', '', basename($_SERVER['PHP_SELF']));

        $views[] = $current == 'browse'
            ? '<em class="widget">' . _("Logs") . '</em>'
            : Horde::widget(Chora::url('', $where), _("Logs"), 'widget', '',
                            '', _("_Logs"));
        // Subversion supports patchsets natively.
        if (!empty($GLOBALS['conf']['paths']['cvsps']) ||
            is_a($GLOBALS['VC'], 'VC_svn')) {
            $views[] = $current == 'patchsets'
                ? '<em class="widget">' . _("Patchsets") . '</em>'
                : Horde::widget(Chora::url('patchsets', $where), _("Patchsets"),
                                'widget', '', '', _("_Patchsets"));
        }
        if (!is_a($GLOBALS['VC'], 'VC_svn')) {
            if (empty($GLOBALS['conf']['paths']['cvsgraph'])) {
                $views[] = $current == 'history'
                    ? '<em class="widget">' . _("Branches") . '</em>'
                    : Horde::widget(Chora::url('history', $where), _("Branches"),
                                    'widget', '', '', _("_Branches"));
            } else {
                $views[] = $current == 'cvsgraph'
                    ? '<em class="widget">' . _("Branches") . '</em>'
                    : Horde::widget(Chora::url('cvsgraph', $where), _("Branches"),
                                    'widget', '', '', _("_Branches"));
            }
        }
        $views[] = $current == 'stats'
            ? '<em class="widget">' . _("Statistics") . '</em>'
            : Horde::widget(Chora::url('stats', $where), _("Statistics"),
                            'widget', '', '', _("_Statistics"));

        return _("View:") . ' ' . implode(' | ', $views);
    }

    /**
     */
    function formatLogMessage($log)
    {
        global $conf;

        require_once 'Horde/Text/Filter.php';

        $log = Text_Filter::filter($log, 'text2html', array('parselevel' => TEXT_HTML_MICRO, 'charset' => NLS::getCharset(), 'class' => ''));

        if (!empty($conf['tickets']['regexp']) && !empty($conf['tickets']['replacement'])) {
            $log = preg_replace($conf['tickets']['regexp'], $conf['tickets']['replacement'], $log);
        }

        return $log;
    }

    /**
     * Return a list of tags for a given log entry.
     *
     * @since Chora 2.1
     *
     * @param VC_Log_* $lg   The VC_Log object.
     * @param string $where  The filename.
     *
     * @return array  An array of linked tags.
     */
    function getTags($lg, $where)
    {
        $tags = array();
        foreach ($lg->querySymbolicBranches() as $symb => $bra) {
            $tags[] = '<a href="' . Chora::url('', $where, array('onb' => $bra)) . '">'. htmlspecialchars($symb) . '</a>';
        }
        if ($lg->tags) {
            foreach ($lg->tags as $tag) {
            $tags[] = htmlspecialchars($tag);
            }
        }
        return $tags;
    }

    /**
     * Return branch information for a given revision.
     *
     * @since Chora 2.1
     *
     * @param VC_File $fl  The VC_File object.
     * @param string $rev  The filename.
     *
     * @return array  An 2-member array - branch name and branch revision.
     */
    function getBranch($fl, $rev)
    {
        $branchName = '';
        $branchRev = VC_Revision::strip($rev, 1);
        if (isset($fl->branches[$rev])) {
            $branchName = $fl->branches[$rev];
        } elseif (isset($fl->branches[$branchRev])) {
            $branchName = $fl->branches[$branchRev];
        }
        return array($branchName, $branchRev);
    }

    /**
     * Create a range of revisions between two revision numbers.
     *
     * @since Chora 2.1
     *
     * @param string $r1  The initial revision.
     * @param string $r2  The ending revision.
     *
     * @return array  The revision range, or empty if there is no straight
     *                line path between the revisions.
     */
    function getRevisionRange($r1, $r2)
    {
        if (VC_Revision::cmp($r1, $r2) == 1) {
            $curr = VC_Revision::prev($r1);
            $stop = VC_Revision::prev($r2);
            $flip = true;
        } else {
            $curr = $r2;
            $stop = $r1;
            $flip = false;
        }

        $ret_array = array();

        do {
            $ret_array[] = $curr;
            $curr = VC_Revision::prev($curr);
            if ($curr == $stop) {
                return ($flip) ? array_reverse($ret_array) : $ret_array;
            }
        } while (VC_Revision::cmp($curr, $stop) != -1);

        return array();
    }

    /**
     * Return formatted date information.
     *
     * @since Chora 2.1
     *
     * @param integer $date  Number of seconds since epoch we wish to display.
     *
     * @return string  The date formatted pursuant to Horde prefs.
     */
    function formatDate($date)
    {
        static $format;

        if (!isset($format)) {
            $format = $GLOBALS['prefs']->getValue('date_format') .
                ($GLOBALS['prefs']->getValue('twenty_four')
                 ? ' %H:%M'
                 : ' %I:%M %p');
        }

        return strftime($format, $date);
    }

}