Intraface_XMLRPC_Server0100::checkCredentials()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 35
Code Lines 22

Duplication

Lines 35
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 5
nop 1
dl 35
loc 35
rs 8.439
c 0
b 0
f 0
ccs 0
cts 22
cp 0
crap 30
1
<?php
2
/**
3
 * Main XMLRPC server class to extend all other Serves from
4
 *
5
 * This version 1.0.0 supports utf8 backend.
6
 *
7
 * Gives ability to encode and decode data correct.
8
 * @category XMLRPC_Server
9
 * @package  Intraface_XMLRPC
10
 * @author   Sune Jensen <[email protected]>
11
 * @author   Lars Olesen <[email protected]>
12
 * @version  @package-version@
13
 */
14
15
/**
16
 * Main XMLRPC server class to extend all other Serves from
17
 *
18
 * This version 1.0.0 supports utf8 backend.
19
 *
20
 * Gives ability to encode and decode data correct.
21
 * @category XMLRPC_Server
22
 * @package  Intraface_XMLRPC
23
 * @author   Sune Jensen <[email protected]>
24
 * @author   Lars Olesen <[email protected]>
25
 * @version  @package-version@
26
 */
27 View Code Duplication
class Intraface_XMLRPC_Server0100
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
{
29
    /**
30
     * @var struct $credentials
31
     */
32
    protected $credentials;
33
34
    /**
35
     * @var object $kernel intraface kernel
36
     */
37
    protected $kernel;
38
39
    /**
40
     * @var array with valid encodings
41
     */
42
    protected $valid_encodings = array('utf-8', 'iso-8859-1');
43
44
    /**
45
     * Constructor
46
     *
47
     * @param string  $encoding The encoding wich the server recieves and returns data in
48
     *
49
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
50
     */
51
    public function __construct($encoding = 'utf-8')
52
    {
53
        if (!in_array($encoding, $this->valid_encodings)) {
54
            throw new Exception('Invalid encoding: '.$encoding.'. Should either be utf-8 or iso-8859');
55
        }
56
        $this->encoding = $encoding;
0 ignored issues
show
Bug introduced by
The property encoding does not seem to exist. Did you mean valid_encodings?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
57
    }
58
59
    /**
60
     * Checks credentials
61
     *
62
     * @param struct $credentials Credentials to use the server
63
     *
64
     * @return array
65
     */
66
    protected function checkCredentials($credentials)
67
    {
68
        $this->credentials = $credentials;
69
70
        if (count($credentials) != 2) { // -4
71
            require_once 'XML/RPC2/Exception.php';
72
            throw new XML_RPC2_FaultException('wrong argument count in $credentials - got ' . count($credentials) . ' arguments - need 2', -4);
73
        }
74
        if (empty($credentials['private_key'])) { // -5
75
            require_once 'XML/RPC2/Exception.php';
76
            throw new XML_RPC2_FaultException('supply a private_key', -5);
77
        }
78
        if (empty($credentials['session_id'])) { // -5
79
            require_once 'XML/RPC2/Exception.php';
80
            throw new XML_RPC2_FaultException('supply a session_id', -5);
81
        }
82
83
        $auth_adapter = new Intraface_Auth_PrivateKeyLogin(MDB2::singleton(DB_DSN), $credentials['session_id'], $credentials['private_key']);
84
        $weblogin = $auth_adapter->auth();
85
86
        if (!$weblogin) {
87
            require_once 'XML/RPC2/Exception.php';
88
            throw new XML_RPC2_FaultException('access to intranet denied', -2);
89
        }
90
91
        $this->kernel = new Intraface_Kernel($credentials['session_id']);
92
        $this->kernel->weblogin = $weblogin;
0 ignored issues
show
Bug introduced by
The property weblogin does not seem to exist in Intraface_Kernel.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
93
        $this->kernel->intranet = new Intraface_Intranet($weblogin->getActiveIntranetId());
94
        $this->kernel->setting = new Intraface_Setting($this->kernel->intranet->get('id'));
95
96
        // makes intranet_id accessable in Doctrine
97
        Intraface_Doctrine_Intranet::singleton($this->kernel->intranet->getId());
98
99
        return true;
100
    }
101
102
    /**
103
     * Prepares response to be sent with the correct UTF-8 encoding.
104
     *
105
     * @param mixed $values Array or string to decode
106
     *
107
     * @return mixed UTF8 decoded request
108
     */
109
    protected function prepareResponseData($values)
110
    {
111
        $values = $this->recursiveMap(array($this, 'handleNull'), $values);
112
113
        if ($this->encoding == 'iso-8859-1') {
0 ignored issues
show
Bug introduced by
The property encoding does not seem to exist. Did you mean valid_encodings?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
114
            return $this->recursiveMap('utf8_decode', $values);
115
        }
116
117
        return $values;
118
    }
119
120
    /**
121
     * Process data from client, so that data is returned with the correct encoding.
122
     *
123
     * @param mixed $values Array or string to decode
124
     *
125
     * @return mixed correct encoded response
126
     */
127
    protected function processRequestData($values)
128
    {
129
        // @todo When sending from utf8-backend from outside to xmlrpcext
130
        //       the encoding will think it is iso-8859-1, but acutally it is utf8.
131
        //       So it seems that it is encoded twice.
132
        if ($this->encoding == 'iso-8859-1') {
0 ignored issues
show
Bug introduced by
The property encoding does not seem to exist. Did you mean valid_encodings?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
133
            return $this->recursiveMap('utf8_encode', $values);
134
        }
135
136
        return $values;
137
    }
138
139
    function handleNull($value)
140
    {
141
        if (is_null($value)) {
142
            return '';
143
        }
144
        return $value;
145
    }
146
147
    protected function recursiveMap($function, $values)
148
    {
149
        if (is_string($values)) {
150
            return call_user_func($function, $values);
151
        } elseif (is_null($values)) {
152
            return call_user_func($function, $values);
153
        } elseif (is_array($values)) {
154
            foreach ($values as $key => $value) {
155
                $values[$key] = $this->recursiveMap($function, $value);
156
            }
157
            return $values;
158
        } else {
159
            return $values;
160
        }
161
    }
162
}
163