summaryrefslogtreecommitdiff
path: root/lib/api/kolab_api_service_form_value.php
blob: 0f0ad74a2340206132b6f59bcf407b83835ef010 (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
<?php

/**
 *
 */
class kolab_api_service_form_value extends kolab_api_service
{

    public function capabilities($domain)
    {
        return array(
            'generate_cn' => 'w',
            'generate_displayname' => 'w',
            'generate_mail' => 'w',
            'generate_password' => 'r',
            'generate_uid' => 'w',
            'generate_userpassword' => 'r',
//            'info' => 'r',
        );
    }

    public function generate_cn($getdata, $postdata)
    {
        $uta = $this->user_type_attributes($postdata['user_type_id']);

        if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['cn'])) {
            // Use Data Please
            foreach ($uta['auto_form_fields']['cn']['data'] as $key) {
                if (!isset($postdata[$key])) {
                    throw new Exception("Key not set: " . $key, 12356);
                }
            }

            $cn = trim($postdata['givenname'] . " " . $postdata['sn']);

            return array("cn" => $cn);
        }
    }

    public function generate_displayname($getdata, $postdata)
    {
        $uta = $this->user_type_attributes($postdata['user_type_id']);

        if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['displayname'])) {
            // Use Data Please
            foreach ($uta['auto_form_fields']['displayname']['data'] as $key) {
                if (!isset($postdata[$key])) {
                    throw new Exception("Key not set: " . $key, 12356);
                }
            }

            $displayname = $postdata['givenname'];
            if ($postdata['sn']) {
                $displayname = $postdata['sn'] . ", " . $displayname;
            }

            return array("displayname" => $displayname);
        }
    }

    public function generate_mail($getdata, $postdata)
    {
        $uta = $this->user_type_attributes($postdata['user_type_id']);

        if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['mail'])) {
            // Use Data Please
            foreach ($uta['auto_form_fields']['mail']['data'] as $key) {
                if (!isset($postdata[$key])) {
                    throw new Exception("Key not set: " . $key, 12356);
                }
            }

            $givenname = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['givenname']);
            $sn        = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);

            $givenname = strtolower($givenname);
            $sn        = strtolower($sn);

            $givenname = preg_replace('/[^a-z-_]/i', '', $givenname);
            $sn        = preg_replace('/[^a-z-_]/i', '', $sn);

            $mail = $givenname . "." . $sn . "@" . $_SESSION['user']->get_domain();

            $orig_mail = $mail;

            $auth = Auth::get_instance($_SESSION['user']->get_domain());

            $x = 2;
            while ($auth->user_find_by_attribute(array('mail' => $mail))) {
                list($mail_local, $mail_domain) = explode('@', $orig_mail);
                $mail = $mail_local . $x . '@' . $mail_domain;
                $x++;
            }

            return array('mail' => $mail);
        }
    }

    public function generate_password($getdata, $postdata)
    {
        exec("head -c 200 /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c15", $userpassword_plain);
        $userpassword_plain = $userpassword_plain[0];
        return array('password' => $userpassword_plain);
    }

    public function generate_uid($getdata, $postdata)
    {
        $uta = $this->user_type_attributes($postdata['user_type_id']);

        if (isset($uta['auto_form_fields']) && isset($uta['auto_form_fields']['uid'])) {
            // Use Data Please
            foreach ($uta['auto_form_fields']['uid']['data'] as $key) {
                if (!isset($postdata[$key])) {
                    throw new Exception("Key not set: " . $key, 12356);
                }
            }

            $uid = iconv('UTF-8', 'ASCII//TRANSLIT', $postdata['sn']);
            $uid = strtolower($uid);
            $uid = preg_replace('/[^a-z-_]/i', '', $uid);

            $orig_uid = $uid;

            $auth = Auth::get_instance($_SESSION['user']->get_domain());

            $x = 2;
            while ($auth->user_find_by_attribute(array('uid' => $uid))) {
                $uid = $orig_uid . $x;
                $x++;
            }

            return array('uid' => $uid);
        }
    }

    public function generate_userpassword($getdata, $postdata)
    {
        $password = $this->generate_password($getdata, $postdata);
        return array('userpassword' => $password['password']);
    }

}