aboutsummaryrefslogtreecommitdiffstats
path: root/toj/php/event.inc.php
blob: 9d2a23c0cd1a9bda1257eea2a2979eac0edb63a4 (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
<?php
class event
{
    private static $white_list;

    private static function init()
    {
        event::$white_list = array('10.8.0.2');
    }

    // execute function $name in file $fname
    // if want to call a function of a class, have 2 choice:
    // 1. pass an array as $name, put every level of class in it, like:
    //   A::B::C() should be passed like array("A", "B", "C").
    // 2. pass a string with :: directly, it will be automatically 
    //   translated.
    // return value will be FALSE on failure, return value of target
    // function otherwise. NULL when no return value.
    public static function exec_func($fname, $name, $arg)
    {
        if(!file_exists($fname))
        {
            return false;
        }
        require_once($fname);
        if(is_string($name) && strpos($name, ":") !== FALSE)
        {
            $name = preg_split("/::/", $name);
        }
        if(is_string($name) && !is_callable($name))
        {
            return false;
        }
        if(is_array($name) && !is_callable(join($name, "::")))
        {
            return false;
        }
        if($arg == NULL)
        {
            $res = call_user_func($name);
        }
        else
        {
            $res = call_user_func_array($name, $arg);
        }
        if($res === false)
        {
            return false;
        }
        return $res;
    }

    public static function validate_ip()
    {
        event::init();
        if (!empty($_SERVER['HTTP_CLIENT_IP']))
        {
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
        {
            $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
            $ip=$_SERVER['REMOTE_ADDR'];
        }
        return in_array($ip, event::$white_list);
    }
}
?>