1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
4// +----------------------------------------------------------------------+
5// | Eventum - Issue Tracking System                                      |
6// +----------------------------------------------------------------------+
7// | Copyright (c) 2008 Elan Ruusamäe                                     |
8// | Copyright (c) 2011 - 2015 Eventum Team.                              |
9// |                                                                      |
10// | This program is free software; you can redistribute it and/or modify |
11// | it under the terms of the GNU General Public License as published by |
12// | the Free Software Foundation; either version 2 of the License, or    |
13// | (at your option) any later version.                                  |
14// |                                                                      |
15// | This program is distributed in the hope that it will be useful,      |
16// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
17// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
18// | GNU General Public License for more details.                         |
19// |                                                                      |
20// | You should have received a copy of the GNU General Public License    |
21// | along with this program; if not, write to:                           |
22// |                                                                      |
23// | Free Software Foundation, Inc.                                       |
24// | 51 Franklin Street, Suite 330                                        |
25// | Boston, MA 02110-1301, USA.                                          |
26// +----------------------------------------------------------------------+
27// | Authors: Elan Ruusamäe <glen@delfi.ee>                               |
28// +----------------------------------------------------------------------+
29
30class Eventum_RPC_Exception extends Exception
31{
32}
33
34class Eventum_RPC
35{
36    /**
37     * The URL of Eventum installation to send requests to
38     *
39     * @var string
40     */
41    private $url;
42
43    /**
44     * @var XML_RPC_Client
45     */
46    private $client;
47
48    public function __construct($url)
49    {
50        $this->url = $url;
51        $this->client = $this->getClient();
52
53        // not sure why this is ever off,
54        // because data that can't be encoded to xml can't be submitted at all
55        $this->client->setAutoBase64(true);
56    }
57
58    /**
59     * Change the current debug mode
60     *
61     * @param int $debug  where 1 = on, 0 = off
62     */
63    public function setDebug($debug)
64    {
65        $this->client->setDebug($debug);
66    }
67
68    /**
69     * Set username and password properties for connecting to the RPC server
70     *
71     * @param string $username the user name
72     * @param string $password the password
73     * @see XML_RPC_Client::$username, XML_RPC_Client::$password
74     */
75    public function setCredentials($username, $password)
76    {
77        $this->client->setCredentials($username, $password);
78    }
79
80    /**
81     * Implementation independent method to encode value as binary
82     *
83     * @param mixed $value
84     * @return XML_RPC_Value
85     */
86    public function encodeBinary($value)
87    {
88        return new XML_RPC_Value($value, 'base64');
89    }
90
91    private function getClient()
92    {
93        $data = parse_url($this->url);
94        if (!isset($data['port'])) {
95            $data['port'] = $data['scheme'] == 'https' ? 443 : 80;
96        }
97        if (!isset($data['path'])) {
98            $data['path'] = '';
99        }
100
101        return new XML_RPC_Client($data['path'], $data['host'], $data['port']);
102    }
103
104    public function __call($method, $args)
105    {
106        $params = array();
107        foreach ($args as $arg) {
108            // argument already encoded as XML_RPC_Value
109            if ($arg instanceof XML_RPC_Value) {
110                $params[] = $arg;
111                continue;
112            }
113
114            // serialize all objects first
115            if (is_object($arg)) {
116                $arg = serialize($arg);
117            }
118
119            $params[] = XML_RPC_encode($arg);
120        }
121        $msg = new XML_RPC_Message($method, $params);
122
123        $result = $this->client->send($msg);
124
125        if ($result === 0) {
126            throw new Eventum_RPC_Exception($this->client->errstr);
127        }
128        if (is_object($result) && $result->faultCode()) {
129            throw new Eventum_RPC_Exception($result->faultString());
130        }
131
132        $value = XML_RPC_decode($result->value());
133
134        return $value;
135    }
136}
137