File: Chora.php

package info (click to toggle)
chora2 2.0.1-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,208 kB
  • ctags: 302
  • sloc: php: 1,298; xml: 406; makefile: 66
file content (490 lines) | stat: -rw-r--r-- 16,849 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
<?php
/**
 * Chora Base Class.
 *
 * $Horde: chora/lib/Chora.php,v 1.72.6.3 2005/10/11 21:13:14 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 init()
    {
        global $acts, $defaultActs, $conf, $where, $atdir,
            $fullname, $prefs, $sourceroots, $sourceroot, $scriptName;

        /**
         * 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']));

        $conf['paths']['sourceroot'] = $sourcerootopts['location'];
        $conf['paths']['cvsusers'] = $sourcerootopts['location'] . '/' . @$sourcerootopts['cvsusers'];
        $conf['paths']['introText'] = CHORA_BASE . '/config/' . @$sourcerootopts['intro'];
        $conf['options']['introTitle'] = @$sourcerootopts['title'];
        $conf['options']['sourceRootName'] = $sourcerootopts['name'];

        $where = Util::getFormData('f', '');

        /* Override $where with PATH_INFO if appropriate. */
        if (($conf['options']['urls'] == 'path_info' || $conf['options']['urls'] == 'rewrite') &&
            isset($_SERVER['PATH_INFO'])) {
            $where = $_SERVER['PATH_INFO'];
        }

        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(_("SourceRoot not found! This could be a misconfiguration by the server administrator, or the server could be having temporary problems. Please try again later."));
        }
    }

    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 $msg  The verbose error message to be displayed.
     */
    function fatal($msg)
    {
        global $registry, $conf, $notification, $browser, $prefs;

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

        $notification->push($msg, '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->getMessage());
        }
    }

    /**
     * Return an array with the names of any of the variables we need
     * to keep, that are different from the defaults.
     *
     * @return array  Names/vals of differing variables.
     */
    function differingVars()
    {
        global $acts, $defaultActs;
        reset($acts);
        $ret = array();
        foreach ($acts as $key => $val) {
            if ($val != $defaultActs[$key]) {
                $ret[$key] = $val;
            }
        }
        return $ret;
    }

    /**
     * Generate a series of hidden input variables based on the GET
     * parameters which are different from the defaults.
     *
     * @param array $except  Array of exceptions to never output.
     *
     * @return string  A set of input tags with the different variables.
     */
    function generateHiddens($except = array())
    {
        global $acts;
        $toOut = Chora::differingVars();
        $ret = Util::formInput() . "\n";
        while (list($key, $val) = each($toOut)) {
            if (is_array($except) && !in_array($key, $except)) {
                $ret .= "<input type=\"hidden\" name=\"$key\" value=\"$val\" />\n";
            }
        }
        return $ret;
    }

    /**
     * Convert a commit-name into whatever the user wants.
     *
     * @param string $name  Account name.
     *
     * @return string  The transformed name.
     */
    function showAuthorName($name, $fullname = false)
    {
        global $VC;

        $users = $VC->getUsers($GLOBALS['conf']['paths']['cvsusers']);
        if (is_array($users) && isset($users[$name])) {
            return '<a href="mailto:' . $users[$name]['mail'] . '">' .
                ($fullname ? $users[$name]['name'] : $name) .
                '</a>' . ($fullname ? " <i>($name)</i>" : '');
        } else {
            return $name;
        }
    }

    /**
     * Generate a URL that links into Chora.
     *
     * @param string $script  Name of the Chora script to link into
     * @param string $uri     Any PATH_INFO portion that should be included
     * @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;

        $arglist = array_merge(Chora::differingVars(), $args);
        $script = $script ? $script . '.php' : '';

        if ($conf['options']['urls'] != 'get') {
            if ($conf['options']['urls'] == 'path_info' && empty($script)) {
                $script = 'browse.php';
            }

            if (substr($uri, 0, 1) != '/' && $script) {
                $script .= '/';
            }
            $script .= $uri;
            if (substr($script, 0, 1) == '/') {
                $script = substr($script, 1);
            }
        } else {
            $arglist['f'] = $uri;
            if (empty($script)) {
                $script = 'browse.php';
            }
        }

        $url = Horde::applicationUrl($script);
        $url = Util::addParameter($url, $arglist);

        if (!empty($anchor)) {
            $url .= "#$anchor";
        }

        return $url;
    }

    /**
     * Generate a list of repositories available from this
     * installation of Chora.
     *
     * @return string  XHTML code representing links to the repositories.
     */
    function repositories()
    {
        global $sourceroot, $sourceroots, $defaultActs;

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

        if (count($arr)) {
            return _("Other Repositories") . ': ' . implode(' , ', $arr);
        } else {
            return '';
        }
    }

    /**
     * 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, $sourceroots, $sourceroot;
        static $restricted;

        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 = $_SERVER['PHP_SELF'];
        if (!empty($_SERVER['PATH_INFO'])) {
            $current = str_replace($_SERVER['PATH_INFO'], '', $current);
        }
        $current = str_replace('.php', '', basename($current));

        $views[] = $current == 'browse' ? '<i class="widget">' . _("Logs") . '</i>' : 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' ? '<i class="widget">' . _("Patchsets") . '</i>' : Horde::widget(Chora::url('patchsets', $where), _("Patchsets"), 'widget', '', '', _("_Patchsets"));
        }
        if (!is_a($GLOBALS['VC'], 'VC_svn')) {
            $views[] = $current == 'history' ? '<i class="widget">' . _("Branches") . '</i>' : Horde::widget(Chora::url('history', $where), _("Branches"), 'widget', '', '', _("_Branches"));
        }
        if (!empty($GLOBALS['conf']['paths']['cvsgraph']) && !is_a($GLOBALS['VC'], 'VC_svn')) {
            $views[] = $current == 'cvsgraph' ? '<i class="widget">' . _("Graph") . '</i>' : Horde::widget(Chora::url('cvsgraph', $where), _("Graph"), 'widget', '', '', _("_Graph"));
        }
        $views[] = $current == 'stats' ? '<i class="widget">' . _("Statistics") . '</i>' : 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;
    }

}