Completed
Push — master ( f6ad35...221395 )
by Andrey
02:31
created

Site.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace humanity;
3
4
class Site {
5
6
    private static $js;
7
8
    public function __construct(){
9
10
        $config = (new Config)->get();
11
12
        if(!isset($_SERVER['HTTP_ACCEPT'])) $_SERVER['HTTP_ACCEPT'] = '*/*';
13
        $acceptSrc = explode(',',$_SERVER['HTTP_ACCEPT']);
14
        foreach($acceptSrc as $key=>$value){
15
            $value = explode('/',$value);
16
            $value[1] = explode(';',$value[1]);
17
            $name = array_shift($value[1]);
18
            $data = [];
19
            foreach($value[1] as $k=>$v){
20
                $v = explode('=',$v);
21
                $data[$v[0]] = $v[1];
22
            }
23
            $accept[$value[0]][$name] = $data;
24
        }
25
26
        unset($acceptSrc,$key,$value,$name,$data,$k,$v);
27
28
        $requestUri = parse_url($_SERVER['REQUEST_URI']);
29
        if($requestUri['path'] == '/javascript.js'){
30
            header('Content-Type: application/javascript');
31
32
            $tsstring = 'Sun, 03 Jan 2016 11:22:20 GMT';
33
            $etag = md5($tsstring);
34
            $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;
35
            $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? $_SERVER['HTTP_IF_NONE_MATCH'] : false;
36
            if ((($if_none_match && $if_none_match == $etag) || (!$if_none_match)) &&
0 ignored issues
show
Bug Best Practice introduced by
The expression $if_none_match of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
Bug Best Practice introduced by
The expression $if_none_match of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
37
                ($if_modified_since && $if_modified_since == $tsstring))
0 ignored issues
show
Bug Best Practice introduced by
The expression $if_modified_since of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
38
            {
39
                header('HTTP/1.1 304 Not Modified');
40
                exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method __construct() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
41
            }
42
            else
43
            {
44
                header("Last-Modified: $tsstring");
45
                header("ETag: $etag");
46
            }
47
            
48
            if(isset($requestUri['query'])){
49
                $query = explode(':',$requestUri['query']);
50
                (new Content)->js($query);
51
            }
52
            exit;
53
        }
54
55
        header('Access-Control-Allow-Origin: *');
56
        header('Access-Control-Max-Age: 31556926'); 
57
        header('Access-Control-Allow-Credentials: true'); 
58
        header('Access-Control-Allow-Methods: POST, GET, DELETE, PUT');
59
60
        if(isset($accept['application']['view'])){
61
            header('Content-Type: text/html');
62
            (new RestApi)->view();
63
        } else if(isset($accept['application']['widget'])){
64
            header('Content-Type: text/html');
65
            (new RestApi)->widget();
66
        } else if(isset($accept['application']['apps'])){
67
            header('Content-Type: application/json');
68
            $query = json_decode(file_get_contents('php://input'),1);
69
            $result = [];
70
            foreach($query as $method=>$params){
71
                $stableParams = [];
72
                $file = $config['core']['apps'].'/'.$method.'.php';
73
                $func = require_once($file);
74
                $reflection = new \ReflectionFunction($func);
75
                foreach($reflection->getParameters() as $key=>$value){
76
                    if(isset($params[$value->name])) $stableParams[$value->name] = $params[$value->name];
77
                    else $stableParams[$value->name] = null;
78
                }
79
                $name = explode('/',$method);
80
                $count = count($name)-1;
81
                $app = new Application;
82
                foreach($name as $key=>$var){
83
                    if($count == $key) $app = call_user_method_array($var,$app,$stableParams);
84
                    else $app = $app->{$var};
85
                }
86
                $result[$method] = $app;
87
            }
88
            die(json_encode($result));
89
        } else {
90
            (new Content)->page();
91
        }
92
    }
93
94
}
95
?>
96