PHP 8.3.27
Preview: class.dispatcher.php Size: 7.12 KB
/home7/doctorbruno/public_html/taeionline.com/support/include/class.dispatcher.php
<?php
/*********************************************************************
    class.dispatcher.php

    Dispatcher that will read files with URL lists in them and attempt to
    match the URL requested to a function that should be invoked to handle
    the request.

    Jared Hancock
    Copyright (c)  2006-2013 osTicket
    http://www.osticket.com

    Released under the GNU General Public License WITHOUT ANY WARRANTY.
    See LICENSE.TXT for details.

    vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/

/**
 * URL resolver and dispatcher. It's meant to be quite lightweight, so the
 * functions aren't separated
 */
class Dispatcher {
    function __construct($file=false) {
        $this->urls = array();
        $this->file = $file;
    }

    function resolve($url, $args=null) {
        if ($this->file) { $this->lazy_load(); }
        # Support HTTP method emulation with the _method GET argument
        if (isset($_GET['_method'])) {
            $_SERVER['REQUEST_METHOD'] = strtoupper($_GET['_method']);
            unset($_GET['_method']);
        }
        // Decode URL for accurate matching
        $url = urldecode($url);
        foreach ($this->urls as $matcher) {
            if ($matcher->matches($url)) {
                return $matcher->dispatch($url, $args);
            }
        }
        Http::response(400, __("URL not supported"));
    }
    /**
     * Returns the url for the given function and arguments (arguments
     * aren't declared, but will be handled
     */
    function reverse($func) { }
    /**
     * Add the url to the list of supported URLs
     */
    function append($url, $prefix=false) {
        if ($prefix) { $url->setPrefix($prefix); }
        array_push($this->urls, $url);
    }
    /**
     * Add the urls from another dispatcher onto this one
     */
    function extend($dispatcher) {
        foreach ($dispatcher->urls as $url) { $this->append($url); }
        /* allow inlining / chaining */ return $this;
    }

    /* static */ function include_urls($file, $absolute=false, $lazy=true) {
        if (!$absolute) {
            # Fetch the working path of the caller
            $bt = debug_backtrace();
            $file = dirname($bt[0]["file"]) . "/" . $file;
        }
        if ($lazy) return new Dispatcher($file);
        else return (include $file);
    }
    /**
     * The include_urls() method will create a new Dispatcher and set the
     * $this->file to where the file to be loaded is located. When this
     * dispatcher is first accessed, the file will be loaded.
     */
    function lazy_load() {
        $this->extend(include $this->file);
        $this->file=false;
    }
}

class UrlMatcher {
    function __construct($regex, $func, $args=false, $method=false) {
        # Add the slashes for the Perl syntax
        $this->regex = "@" . $regex . "@";
        $this->func = $func;
        $this->args = ($args) ? $args : array();
        $this->prefix = false;
        $this->method = $method;
    }

    function setPrefix($prefix) { $this->prefix = $prefix; }

    function matches($url) {
        if ($this->method && $_SERVER['REQUEST_METHOD'] != $this->method) {
            return false;
        }
        return preg_match($this->regex, $url, $this->matches) == 1;
    }

    function dispatch($url, $prev_args=null) {

        # Remove named values from the match array
        $f = array_filter(array_keys($this->matches), 'is_numeric');
        $this->matches = array_intersect_key($this->matches, array_flip($f));

        if (is_object($this->func) && (@get_class($this->func) == "Dispatcher")) {
            # Trim the leading match off the $url and call the
            # sub-dispatcher. This will be the case for lines in the URL
            # file like
            # url("^/blah", Dispatcher::include_urls("blah/urls.conf.php"))
            # Also, pass arguments matched so far (if any) to the receiving
            # resolve() method by merging the $prev_args into $this->matches
            # (excluding $this->matches[0], which is the matched URL at this
            # level)
            return $this->func->resolve(
                substr($url, strlen($this->matches[0])),
                array_merge(($prev_args) ? $prev_args : array(),
                    array_slice($this->matches, 1)));
        }

        # Drop the first item of the matches array (which is the whole
        # matched url). Then merge in any initial arguments.
        unset($this->matches[0]);

        # Prepend received arguments (from a parent Dispatcher). This is
        # different from the static args, which are postpended
        if (is_array($prev_args))
            $args = array_merge($prev_args, $this->matches);
        else $args = $this->matches;
        # Add in static args specified in the constructor
        $args = array_merge($args, $this->args);
        # Apply the $prefix given
        list($class, $func) = $this->apply_prefix();
        if ($class) {
            # Create instance of the class, which is the first item,
            # then call the method which is the second item
            $class = new $class;
            # Check access at the controller level
            if (!$class->access())
                 Http::response(403,  __('Access Denied!!!'));
            # Create callable function, class is the first item,
            # then call the method is the second item
            $func = array($class, $func);
        }

        if (!is_callable($func))
            Http::response(500, __('Dispatcher compile error. Function not callable'));

        return call_user_func_array($func, $args);
    }
    /**
     * For the $prefix recieved by the constuctor, prepend it to the
     * received $class, if any, then make an import if necessary. Lastly,
     * return the appropriate $class, and $func that should be invoked to
     * dispatch the URL.
     */
    function apply_prefix() {
        if (is_array($this->func)) { list($class, $func) = $this->func; }
        else { $func = $this->func; $class = ""; }
        if (is_object($class))
            return array(false, $this->func);
        if ($this->prefix)
            $class = $this->prefix . $class;

        if (strpos($class, ":")) {
            list($file, $class) = explode(":", $class, 2);
            include_once $file;
        }
        return array($class, $func);
    }
}

function patterns($prefix) {
    $disp = new Dispatcher();
    for ($i=1, $k=func_num_args(); $i<$k; $i++) {
        # NOTE: that $prefix is added to each url rather than to the
        #       dispatcher as a whole so that urls can be copied from one
        #       dispatcher to another (via the ->extend() method) and
        #       completely maintain their integrity
        $disp->append(func_get_arg($i), $prefix);
    }
    return $disp;
}

function url($regex, $func, $args=false, $method=false) {
    return new UrlMatcher($regex, $func, $args, $method);
}

function url_post($regex, $func, $args=false) {
    return url($regex, $func, $args, "POST");
}

function url_get($regex, $func, $args=false) {
    return url($regex, $func, $args, "GET");
}

function url_delete($regex, $func, $args=false) {
    return url($regex, $func, $args, "DELETE");
}
?>

Directory Contents

Dirs: 11 × Files: 132
Name Size Perms Modified Actions
cli DIR
- drwxr-xr-x 2026-10-02 10:08:28
Edit Download
client DIR
- drwxr-xr-x 2026-10-02 10:08:28
Edit Download
config DIR
- drwxr-xr-x 2017-09-15 04:26:55
Edit Download
fpdf DIR
- drwxr-xr-x 2026-10-02 10:08:28
Edit Download
i18n DIR
- drwxr-xr-x 2026-10-02 10:08:28
Edit Download
- drwxr-xr-x 2026-10-02 10:08:28
Edit Download
mpdf DIR
- drwxr-xr-x 2026-10-02 10:08:28
Edit Download
pear DIR
- drwxr-xr-x 2026-10-02 10:08:29
Edit Download
plugins DIR
- drwxr-xr-x 2026-10-02 10:08:29
Edit Download
staff DIR
- drwxr-xr-x 2026-10-02 10:08:29
Edit Download
upgrader DIR
- drwxr-xr-x 2026-10-02 10:08:29
Edit Download
14 B lrw-r--r-- 2025-02-04 09:58:56
Edit Download
194.97 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
7.29 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
4.61 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
9.76 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
13.03 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.33 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1006 B lrw-r--r-- 2025-02-04 09:58:56
Edit Download
874 B lrw-r--r-- 2025-02-04 09:58:56
Edit Download
13.84 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
5.10 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.93 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.05 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
11.73 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.15 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
4.29 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
12.72 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
3.20 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
11.62 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
33.87 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
8.81 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
79.70 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.66 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.24 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
18.33 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.06 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
10.36 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
7.76 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.45 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
15.31 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.49 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
6.95 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
50.73 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
6.44 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.58 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
4.07 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
7.08 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
9.05 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.73 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
11.14 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
3.41 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
9.48 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
15.39 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
5.65 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.65 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
60.36 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.15 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
3.60 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
18.92 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.38 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
33.21 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
7.12 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
6.23 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
64.98 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
49.00 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.69 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
32.92 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
15.05 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
34.59 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
31.90 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
23.67 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
44.30 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
195.58 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
7.16 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
24.28 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
6.90 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.71 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
5.80 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
42.00 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
4.05 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.55 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
35.76 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
25.75 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
9.72 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
30.99 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
6.42 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
5.20 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
7.50 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.30 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
14.15 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
2.39 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
4.04 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
22.45 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
120.90 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
20.64 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
21.86 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
10.59 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
5.53 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.21 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
3.83 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
36.13 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.81 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
103.51 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
11.76 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
11.27 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
46.28 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
61.66 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
7.27 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
19.25 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
3.55 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
4.16 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
9.11 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
60.21 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
56.21 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
12.31 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
23.45 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
108.96 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
17.08 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
167.39 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
21.94 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
20.06 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
34.79 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
13.76 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
43.40 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
7.55 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
10.07 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
12.94 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
11.93 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
3.23 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
1.15 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
33.71 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
53.53 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
37 B lrw-r--r-- 2025-02-04 09:58:56
Edit Download
44 B lrw-r--r-- 2026-10-02 10:08:28
Edit Download
33.13 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
9.55 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
5.63 KB lr--r--r-- 2017-09-16 04:37:08
Edit Download
6.24 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
6.92 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
31.74 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
19.82 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
8.61 KB lrw-r--r-- 2025-02-04 09:58:56
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).