summaryrefslogtreecommitdiffstats
path: root/phpa.php
blob: 48a1df92711387921a0d85c6161b24221a682b53 (plain)
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
#!/usr/bin/php -qCn
<?
/*
    $Id: phpa.php 2008/04/28 $

    David Phillips <david@acz.org>
*/

    __phpa__setup();
    __phpa__print_info();

    for (;;)
    {
        readline_completion_function("__phpa__rl_complete");
        $__phpa__line = readline(">>> ");
        if ($__phpa__line === false)
        {
            echo "\n";
            break;
        }
        if (strlen($__phpa__line) == 0)
            continue;
        if ((!isset($__phpa__hist)) || (($__phpa__line != $__phpa__hist)))
        {
            readline_add_history($__phpa__line);
            $__phpa__hist = $__phpa__line;
        }

        if (__phpa__is_immediate($__phpa__line))
            $__phpa__line = "return ($__phpa__line)";

        ob_start();
        $ret = eval("unset(\$__phpa__line); $__phpa__line;");
        if (ob_get_length() == 0)
        {
            if (is_bool($ret))
                echo ($ret ? "true" : "false");
            else if (is_string($ret))
                echo "'" . addcslashes($ret, "\0..\37\177..\377")  . "'";
            else if (!is_null($ret))
                print_r($ret);
        }
        unset($ret);
        $out = ob_get_contents();
        ob_end_clean();
        if ((strlen($out) > 0) && (substr($out, -1) != "\n"))
            $out .= "\n";
        echo $out;
        unset($out);
    }

    function __phpa__rl_complete($line, $pos, $cursor)
    {
        $const = array_keys(get_defined_constants());
        $var = array_keys($GLOBALS);

        $func = get_defined_functions();
        $s = "__phpa__";
        foreach ($func["user"] as $i)
            if (substr($i, 0, strlen($s)) != $s)
                $func["internal"][] = $i;
        $func = $func["internal"];

        return array_merge($const, $var, $func);
    }

    function __phpa__is_immediate($line)
    {
        $skip = array("class", "declare", "die", "echo", "exit", "for",
                      "foreach", "function", "global", "if", "include",
                      "include_once", "print", "require", "require_once",
                      "return", "static", "switch", "unset", "while");
        $okeq = array("===", "!==", "==", "!=", "<=", ">=");
        $code = "";
        $sq = false;
        $dq = false;
        for ($i = 0; $i < strlen($line); $i++)
        {
            $c = $line{$i};
            if ($c == "'")
                $sq = !$sq;
            else if ($c == '"')
                $dq = !$dq;
            else if (($sq) || ($dq))
            {
                if ($c == "\\")
                    $i++;
            }
            else
                $code .= $c;
        }
        $code = str_replace($okeq, "", $code);
        if (strcspn($code, ";{=") != strlen($code))
            return false;
        $kw = split("[^A-Za-z0-9_]", $code);
        foreach ($kw as $i)
            if (in_array($i, $skip))
                return false;
        return true;
    }

    function __phpa__print_info()
    {
        $ver = phpversion();
        $sapi = php_sapi_name();
        $date = __phpa__build_date();
        $os = PHP_OS;
        echo "PHP $ver ($sapi) ($date) [$os]\n";
    }

    function __phpa__build_date()
    {
        ob_start();
        phpinfo(INFO_GENERAL);
        $x = ob_get_contents();
        ob_end_clean();
        $x = strip_tags($x);
        $x = explode("\n", $x);
        $s = array("Build Date => ", "Build Date ");
        foreach ($x as $i)
            foreach ($s as $j)
                if (substr($i, 0, strlen($j)) == $j)
                    return trim(substr($i, strlen($j)));
        return "???";
    }

    function __phpa__setup()
    {
        if (version_compare(phpversion(), "4.3.0", "<"))
        {
            echo "PHP 4.3.0 or above is required.\n";
            exit(111);
        }
        if (!extension_loaded("readline"))
        {
            $prefix = (PHP_SHLIB_SUFFIX == "dll") ? "php_" : "";
            if (!@dl($prefix . "readline." . PHP_SHLIB_SUFFIX))
            {
                echo "The 'readline' module is required.\n";
                exit(111);
            }
        }
        error_reporting(E_ALL | E_STRICT);
        ini_set("error_log", NULL);
        ini_set("log_errors", 1);
        ini_set("html_errors", 0);
        ini_set("display_errors", 0);
        while (ob_get_level())
            ob_end_clean();
        ob_implicit_flush(true);
    }
?>