1<?php
2/*
3 * Gitlab Access
4 * @author Algorys
5 */
6
7class DokuwikiGitlab {
8    public $client;
9    public $data;
10
11    function __construct($dw_data) {
12        $this->dw_data = $dw_data;
13        $this->client = curl_init();
14    }
15
16    function getAPIUrl() {
17        return $this->dw_data['server'] . '/api/v3/';
18    }
19
20    function gitlabRequest($url) {
21        curl_setopt($this->client, CURLOPT_URL, $url);
22        curl_setopt($this->client, CURLOPT_HTTPHEADER, array(
23            'Content-Type: application/json',
24            'Accept: application/json'
25        ));
26        curl_setopt($this->client, CURLOPT_SSL_VERIFYHOST, '1');
27        curl_setopt($this->client, CURLOPT_SSL_VERIFYPEER, '0');
28        curl_setopt($this->client, CURLOPT_RETURNTRANSFER, true);
29
30        $answer = curl_exec($this->client);
31        $answer_decoded = json_decode($answer, true);
32
33        return $answer_decoded;
34    }
35
36    function closeClient(){
37        curl_close($this->client);
38    }
39
40    function getProject() {
41        $url_request = $this->getAPIUrl().'projects/'.urlencode($this->dw_data['project']).'/?private_token='.$this->dw_data['token'];
42
43        $project = $this->gitlabRequest($url_request);
44
45        return $project;
46    }
47
48    function getProjectMembers($kind, $unwanted_members) {
49        // Define url requests for 'user' and 'group'
50        $user_url_request = $this->getAPIUrl().'projects/'.urlencode((string)$this->dw_data['project']).'/members/?private_token='.$this->dw_data['token'];
51
52        $namespace_array = explode('/', $this->dw_data['project']);
53        // Assign var Array after for PHP < 5.4
54        $namespace = $namespace_array[0];
55        $group_url_request = $this->getAPIUrl().'groups/'.urlencode($namespace).'/members/?private_token='.$this->dw_data['token'];
56
57        // Get members and merge them if needed
58        $user_members = $this->gitlabRequest($user_url_request);
59        $group_members = $this->gitlabRequest($group_url_request);
60
61        if (isset($group_members['message'])) {
62            $members = $user_members;
63        } else {
64            if (is_array($user_members)) {
65                $members = array_merge($user_members, $group_members);
66            }
67        }
68
69            // Remove unwanted members
70        $unwanted_members = explode(',', $unwanted_members);
71        foreach ($unwanted_members as $unwanted_key => $unwanted_member) {
72            if (is_array($members) && !empty($members)) {
73                foreach ($members as $key => $member) {
74                    if(isset($member['username'])) {
75                        if($member['username'] == trim($unwanted_member)) {
76                            unset($members[$key]);
77                        }
78                    }
79                }
80            }
81        }
82
83        return $members;
84    }
85
86    function getRoleName($role_nb) {
87        $roles = array(
88            10 => Guest,
89            20 => Reporter,
90            30 => Developer,
91            40 => Master,
92            50 => Owner
93        );
94        return $roles[$role_nb];
95    }
96
97}
98