summaryrefslogtreecommitdiffstats
path: root/lib/kolab_api_service.php
blob: 8630cbe2f0910d1d4fa0151e24cef87f431d343b (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
 +--------------------------------------------------------------------------+
 | This file is part of the Kolab Web Admin Panel                           |
 |                                                                          |
 | Copyright (C) 2011-2012, Kolab Systems AG                                |
 |                                                                          |
 | This program is free software: you can redistribute it and/or modify     |
 | it under the terms of the GNU Affero General Public License as published |
 | by the Free Software Foundation, either version 3 of the License, or     |
 | (at your option) any later version.                                      |
 |                                                                          |
 | This program is distributed in the hope that it will be useful,          |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             |
 | GNU Affero General Public License for more details.                      |
 |                                                                          |
 | You should have received a copy of the GNU Affero General Public License |
 | along with this program. If not, see <http://www.gnu.org/licenses/>      |
 +--------------------------------------------------------------------------+
 | Author: Aleksander Machniak <machniak@kolabsys.com>                      |
 | Author: Jeroen van Meeuwen <vanmeeuwen@kolabsys.com>                     |
 +--------------------------------------------------------------------------+
*/

/**
 * Interface class for Kolab Admin Services
 */
abstract class kolab_api_service
{
    protected $controller;
    protected $db;

    /**
     * Class constructor.
     *
     * @param kolab_api_controller Controller
     */
    public function __construct($ctrl)
    {
        $this->db         = SQL::get_instance();
        $this->controller = $ctrl;
    }

    /**
     * Advertise this service's capabilities
     */
    abstract public function capabilities($domain);

    /**
     * Returns attributes of specified user type.
     *
     * @param int  $type_id  User type identifier
     * @param bool $required Throws exception on empty ID
     *
     * @return array User type attributes
     */
    protected function user_type_attributes($type_id, $required = true)
    {
        if (empty($type_id)) {
            if ($required) {
                throw new Exception($this->controller->translate('user.notypeid'), 34);
            }

            return array();
        }

        $sql_result = $this->db->query("SELECT attributes FROM user_types WHERE id = ?", $type_id);
        $user_type  = $this->db->fetch_assoc($sql_result);

        if (empty($user_type)) {
            throw new Exception($this->controller->translate('user.invalidtypeid'), 35);
        }

        $uta = json_decode(unserialize($user_type['attributes']), true);

        return $uta;
    }

    /**
     * Returns attributes of specified group type.
     *
     * @param int  $type_id  Group type identifier
     * @param bool $required Throws exception on empty ID
     *
     * @return array Group type attributes
     */
    protected function group_type_attributes($type_id, $required = true)
    {
        if (empty($type_id)) {
            if ($required) {
                throw new Exception($this->controller->translate('group.notypeid'), 34);
            }

            return array();
        }

        $sql_result = $this->db->query("SELECT attributes FROM group_types WHERE id = ?", $type_id);
        $group_type = $this->db->fetch_assoc($sql_result);

        if (empty($group_type)) {
            throw new Exception($this->controller->translate('group.invalidtypeid'), 35);
        }

        $uta = json_decode(unserialize($group_type['attributes']), true);

        return $uta;
    }

    /**
     * Detects user type ID for specified objectClass attribute value
     *
     * @param array $object_class Value of objectClass attribute
     *
     * @return int User type identifier
     */
    protected function user_type_id($object_class)
    {
        if (empty($object_class)) {
            return null;
        }

        $object_class = array_map('strtolower', $object_class);                                                                                         
        $user_types   = $this->user_types();
        $type_score   = -1;
        $type_id      = null;

        console("Data objectClasses: " . implode(", ", $object_class));

        foreach ($user_types as $idx => $elem) {
            $ref_class = $elem['attributes']['fields']['objectclass'];

            console("Reference objectclasses for " . $elem['key'] . ": " . implode(", ", $ref_class));

            // Eliminate the duplicates between the $data_ocs and $ref_ocs
            $_object_class = array_diff($object_class, $ref_class);
            $_ref_class    = array_diff($ref_class, $object_class);

            $differences   = count($_object_class) + count($_ref_class);
            $commonalities = count($object_class) - $differences;
            $elem_score    = $differences > 0 ? ($commonalities / $differences) : $commonalities;

//            console("\$object_class not in \$ref_class (" . $elem['key'] . "): " . implode(", ", $_object_class));
//            console("\$ref_class not in \$object_class (" . $elem['key'] . "): " . implode(", ", $_ref_class));
            console("Score for user type " . $elem['name'] . ": " . $elem_score . "(" . $commonalities . "/" . $differences . ")");

            if ($elem_score > $type_score) {
                $type_id    = $idx;
                $type_score = $elem_score;
            }
        }

        return $type_id;
    }

    /**
     * Returns user types definitions.
     *
     * @return array User types.
     */
    protected function user_types()
    {
        $sql_result = $this->db->query("SELECT * FROM user_types");
        $user_types = array();

        while ($row = $this->db->fetch_assoc($sql_result)) {
            $user_types[$row['id']] = array();

            foreach ($row as $key => $value) {
                if ($key != "id") {
                    if ($key == "attributes") {
                        $user_types[$row['id']][$key] = json_decode(unserialize($value), true);
                    }
                    else {
                        $user_types[$row['id']][$key] = $value;
                    }
                }
            }
        }

        return $user_types;
    }
}