summaryrefslogtreecommitdiffstats
path: root/admin/survey/minify/lib/Minify/Env.php
blob: 3bc78f7f3a1c9217fe48a46178833d5da208e5c6 (plain) (blame)
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
<?php

class Minify_Env
{

    /**
     * @return string
     */
    public function getDocRoot()
    {
        return $this->server['DOCUMENT_ROOT'];
    }

    /**
     * @return string
     */
    public function getRequestUri()
    {
        return $this->server['REQUEST_URI'];
    }

    public function __construct($options = array())
    {
        $options = array_merge(array(
            'server' => $_SERVER,
            'get' => $_GET,
            'post' => $_POST,
            'cookie' => $_COOKIE,
        ), $options);

        $this->server = $options['server'];
        if (empty($this->server['DOCUMENT_ROOT'])) {
            $this->server['DOCUMENT_ROOT'] = $this->computeDocRoot($options['server']);
        } else {
            $this->server['DOCUMENT_ROOT'] = rtrim($this->server['DOCUMENT_ROOT'], '/\\');
        }

        $this->server['DOCUMENT_ROOT'] = $this->normalizePath($this->server['DOCUMENT_ROOT']);
        $this->get = $options['get'];
        $this->post = $options['post'];
        $this->cookie = $options['cookie'];
    }

    public function server($key = null)
    {
        if (null === $key) {
            return $this->server;
        }

        return isset($this->server[$key]) ? $this->server[$key] : null;
    }

    public function cookie($key = null, $default = null)
    {
        if (null === $key) {
            return $this->cookie;
        }

        return isset($this->cookie[$key]) ? $this->cookie[$key] : $default;
    }

    public function get($key = null, $default = null)
    {
        if (null === $key) {
            return $this->get;
        }

        return isset($this->get[$key]) ? $this->get[$key] : $default;
    }

    public function post($key = null, $default = null)
    {
        if (null === $key) {
            return $this->post;
        }

        return isset($this->post[$key]) ? $this->post[$key] : $default;
    }

    /**
     * turn windows-style slashes into unix-style,
     * remove trailing slash
     * and lowercase drive letter
     *
     * @param string $path absolute path
     *
     * @return string
     */
    public function normalizePath($path)
    {
        $realpath = realpath($path);
        if ($realpath) {
            $path = $realpath;
        }

        $path = str_replace('\\', '/', $path);
        $path = rtrim($path, '/');
        if (substr($path, 1, 1) === ':') {
            $path = lcfirst($path);
        }

        return $path;
    }

    protected $server;
    protected $get;
    protected $post;
    protected $cookie;

    /**
     * Compute $_SERVER['DOCUMENT_ROOT'] for IIS using SCRIPT_FILENAME and SCRIPT_NAME.
     *
     * @param array $server
     * @return string
     */
    protected function computeDocRoot(array $server)
    {
        if (isset($server['SERVER_SOFTWARE']) && 0 !== strpos($server['SERVER_SOFTWARE'], 'Microsoft-IIS/')) {
            throw new InvalidArgumentException('DOCUMENT_ROOT is not provided and could not be computed');
        }

        $substrLength = strlen($server['SCRIPT_FILENAME']) - strlen($server['SCRIPT_NAME']);
        $docRoot = substr($server['SCRIPT_FILENAME'], 0, $substrLength);

        return rtrim($docRoot, '\\');
    }
}