File size: 3,908 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php

/**
 * HttpProxyController
 *
 * @author Erik Amaru Ortiz <erik@colosa.com, aortiz.erik@gmail.com>
 * @package gulliver.system
 * @access private
 */
class HttpProxyController
{

    /**
     *
     * @var array - private array to store proxy data
     */
    private $__data__ = array ();

    /**
     *
     * @var object - private object to store the http request data
     */
    private $__request__;

    public $jsonResponse = true;

    private $sendResponse = true;

    public function __construct() {
        $this->__request__ = new stdclass();
    }

    /**
     * Magic setter method
     *
     * @param string $name
     * @param string $value
     */
    public function __set ($name, $value)
    {
        //echo "Setting '$name' to '$value'\n";
        $this->__data__[$name] = $value;
    }

    /**
     * Magic getter method
     *
     * @param string $name
     * @return string or NULL if the internal var doesn't exist
     */
    public function __get ($name)
    {
        //echo "Getting '$name'\n";
        if (array_key_exists( $name, $this->__data__ )) {
            return $this->__data__[$name];
        }

        /*$trace = debug_backtrace();
        trigger_error(
            'Undefined property via __get(): ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;*/
    }

    /**
     * Magic isset method
     *
     * @param string $name
     */
    public function __isset ($name)
    {
        //echo "Is '$name' set?\n";
        return isset( $this->__data__[$name] );
    }

    /**
     * Magic unset method
     *
     * @param string $name
     */
    public function __unset ($name)
    {
        //echo "Unsetting '$name'\n";
        unset( $this->__data__[$name] );
    }

    /**
     * call to execute a internal proxy method and handle its exceptions
     *
     * @param string $name
     */
    public function call ($name)
    {
        $result = new stdClass();
        try {
            $result = $this->$name( $this->__request__ );

            if (! $this->jsonResponse) {
                return null;
            }

            if (! $result) {
                $result = $this->__data__;
            }

        } catch (Exception $e) {
            $result->success = false;
            $result->message = $result->msg = $e->getMessage();
            switch (get_class( $e )) {
                case 'Exception':
                    $error = "SYSTEM ERROR";
                    break;
                case 'PMException':
                    $error = "PROCESSMAKER ERROR";
                    break;
                case 'PropelException':
                    $error = "DATABASE ERROR";
                    break;
                case 'UserException':
                    $error = "USER ERROR";
                    break;
            }
            $result->error = $e->getMessage();
            $result->exception = new stdClass();
            $result->exception->class = get_class( $e );
            $result->exception->code = $e->getCode();
            $result->exception->trace = $e->getTraceAsString();
        }

        if ($this->sendResponse) {
            print G::json_encode( $result );
        }
    }

    /**
     * Set the http request data
     *
     * @param array $data
     */
    public function setHttpRequestData ($data)
    {
        if (is_array( $data )) {
            while ($var = each( $data )) {
                $this->__request__->$var['key'] = $var['value'];
            }
        } else {
            $this->__request__ = $data;
        }
    }

    public function setJsonResponse ($bool)
    {
        $this->jsonResponse = $bool;
    }

    /**
     * Send response to client
     *
     * @param boolean $val
     */
    public function setSendResponse ($val)
    {
        $this->sendResponse = $val;
    }
}