File: rest.php

package info (click to toggle)
horde3 3.1.3-4etch7
  • links: PTS
  • area: main
  • in suites: etch
  • size: 22,876 kB
  • ctags: 18,071
  • sloc: php: 75,151; xml: 2,979; sql: 1,069; makefile: 79; sh: 64
file content (126 lines) | stat: -rw-r--r-- 3,851 bytes parent folder | download | duplicates (2)
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
<?php
/**
 * The Horde_RPC_rest class provides a REST implementation of the Horde RPC
 * system.
 *
 * $Horde: framework/RPC/RPC/rest.php,v 1.6.2.2 2006/05/04 12:24:14 jan Exp $
 *
 * Copyright Rafael Varela Pet <rafael.varela.pet@usc.es>
 *
 * See the enclosed file COPYING for license information (LGPL). If you did
 * not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Rafael Varela <rafael.varela.pet@usc.es>
 * @since   Horde 3.1
 * @package Horde_RPC
 */
class Horde_RPC_rest extends Horde_RPC {

    /**
     * @var string
     */
    var $_contentType = 'text/html';

    /**
     * Check authentication. Different backends may handle
     * authentication in different ways. The base class implementation
     * checks for HTTP Authentication against the Horde auth setup.
     *
     * @return boolean  Returns true if authentication is successful.
     *                  Should send appropriate "not authorized" headers
     *                  or other response codes/body if auth fails,
     *                  and take care of exiting.
     */
    function authorize()
    {
        if (!$this->_authorize) {
            return true;
        }

        if (Auth::getAuth()) {
            return true;
        }

        return parent::authorize();
    }

    /**
     * Get. all the GET input data. The 'call' param is extracted from the
     * array and treated separately.
     *
     * @return array  (method name, array of parameters)
     *
     * TODO: deserializing of complex data types.
     */
    function getInput()
    {
        $this->_contentType = Util::getGet('restContentType', 'text/html');

    	$getData = Util::dispelMagicQuotes($_GET);
    	unset($getData['restCall']);
    	unset($getData['restContentType']);

    	return array('method' => Util::getGet('restCall'),
    	             'params' => $getData);
    }

    /**
     * Sends an RPC request to the server and returns the result.
     *
     * @param string  The raw request string.
     *
     * @return mixed  The response from the server or an PEAR error object on
     *                failure.
     */
    function getResponse($request)
    {
        global $registry;

        $method = str_replace('.', '/', $request['method']);

        if (!$registry->hasMethod($method)) {
            return PEAR::raiseError(sprintf(_("Method not defined. Called method: %s"), $method));
        }

        /* Look at the method signature so that parameters are assigned by
         * name, instead of relying on the order of GET parameters to match
         * the order defined in the API method. */
        $signature = $registry->getSignature($method);
        $params = array();
        foreach (array_keys($signature[0]) as $param) {
            $params[$param] = isset($request['params'][$param]) ? $request['params'][$param] : null;
        }

        $result = $registry->call($method, $params);
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }

        /* The result is returned depending on the requested content type. */
        if ($this->_contentType == 'text/html') {
            return '<html><body>' . print_r($result, true) . '</body></html>';
        } elseif ($this->_contentType == 'text/plain') {
            return print_r($result, true);
        } elseif ($this->_contentType == 'application/x-httpd-php') {
            return serialize($result);
        } else {
            return $result;
        }
    }

    /**
     * Get the Content-Type of the response.
     *
     * @return string  The MIME Content-Type of the RPC response.
     */
    function getResponseContentType()
    {
        switch ($this->_contentType) {
        case 'application/x-httpd-php':
            return 'text/plain';
        default:
            return $this->_contentType;
        }
    }

}