Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 16766 | Rev 16769 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
16298 anderson 1
<?php
2
declare(strict_types=1);
3
 
4
 
5
namespace LeadersLinked\Library;
6
 
7
 
8
use \Firebase\JWT\JWT;
9
use GuzzleHttp\Client;
16768 efrain 10
 
16298 anderson 11
use Laminas\Db\Adapter\Adapter;
12
use LeadersLinked\Model\ZoomMeeting;
16768 efrain 13
use LeadersLinked\Cache\CacheInterface;
14
use LeadersLinked\Cache\CacheImpl;
16298 anderson 15
 
16
 
17
class Zoom
18
{
19
    const CACHE_OAUTH_TOOKEN = 'zoom_oauth_tooken';
20
 
21
    /**
22
     *
23
     * @var Adapter
24
     */
25
    private $adapter;
26
 
27
 
28
    /**
29
     *
30
     * @var array
31
     */
32
    private $config;
33
 
34
    /**
16768 efrain 35
     *
36
     * @var CacheInterface
16298 anderson 37
     */
38
    private $cache;
39
 
40
 
41
 
16768 efrain 42
 
16298 anderson 43
    /**
44
     *
45
     * @param Adapter $adapter
46
     * @param array config
47
     */
16768 efrain 48
    public function __construct($adapter, $config)
16298 anderson 49
    {
50
        $this->adapter = $adapter;
51
        $this->config = $config;
16768 efrain 52
        $this->cache = CacheImpl::getInstance($config);
16298 anderson 53
    }
54
 
55
 
56
 
57
    public function addMeeting($access_token, $topic, $agenda, $type, $start_time, $duration, $timezone, $password)
58
    {
59
 
60
        $base_url = $this->config['leaderslinked.zoom.base_url'];
61
 
62
 
63
 
64
        $client = new Client([
65
            'base_uri' => $base_url,
66
        ]);
67
 
68
 
69
 
70
        if($type == ZoomMeeting::TYPE_SCHEDULED) {
71
            $json = [
72
                'topic' => $topic,
73
                'agenda' => $agenda,
74
                'type' => 2,
75
                'start_time' => $start_time,
76
                'duration' => $duration,
77
                'password' => $password,
78
                'timezone' => $timezone,
79
                'settings' => [
80
                    'participant_video'=> 'true',
81
                    'auto_recording' => 'true',
82
                ]
83
            ];
84
        } else {
85
            $json = [
86
                'topic' => $topic,
87
                'agenda' => $agenda,
88
                'type' => 1,
89
                'duration' => $duration,
90
                'password' => $password,
91
                'timezone' => $timezone,
92
                'settings' => [
93
                    'participant_video'=> 'true',
94
                    'auto_recording' => 'none',
95
                ]
96
            ];
97
        }
98
 
99
 
100
        $request = [
101
            'headers' => [
102
                'Authorization' => 'Bearer ' . $access_token
103
            ],
104
            'json' => $json,
105
        ];
106
 
107
 
108
        $response = $client->request('POST', '/v2/users/me/meetings', $request );
109
        $statusCode = $response->getStatusCode() ;
110
 
111
       // error_log('$statusCode = ' . $statusCode);
112
 
113
 
114
 
115
        if($statusCode >= 200 && $statusCode <= 300) {
116
            $data = json_decode($response->getBody()->getContents());
117
 
118
 
119
            return [
120
                'success' => true,
121
                'data' => [
122
                    'id' => $data->id,
123
                    'uuid' => $data->uuid,
124
                    'join_url' => $data->join_url,
125
                    'password' => $data->password,
126
                ]
127
 
128
            ];
129
 
130
 
131
        } else {
132
            return [
133
                'success' => false,
134
                'data' => $response->getStatusCode()
135
            ];
136
        }
137
 
138
 
139
 
140
    }
141
 
142
    public function getMeetings($access_token, $next_page_token)
143
    {
144
        $base_url = $this->config['leaderslinked.zoom.base_url'];
145
        $client = new Client([
146
 
147
            'base_uri' => $base_url,
148
        ]);
149
 
150
        $request = [
151
            'headers' => [
152
                'Authorization' => 'Bearer '. $access_token
153
            ],
154
            'type' => 'scheduled'
155
        ];
156
 
157
        if($next_page_token) {
158
            $request['query'] = ['next_page_token' => $next_page_token];
159
        }
160
 
161
 
162
        $response = $client->request('GET', '/v2/users/me/meetings', $request);
163
        $statusCode = $response->getStatusCode() ;
164
 
165
        if($statusCode >= 200 && $statusCode <= 300) {
166
            $meetings = json_decode($response->getBody()->getContents());
167
 
168
            return [
169
                'success' =>  true,
170
                'data' => $meetings
171
            ];
172
 
173
        } else {
174
            return [
175
                'success' => false,
176
                'data' => $response->getStatusCode()
177
            ];
178
        }
179
    }
180
 
181
 
182
 
183
 
184
 
185
    public  function getOAuthAccessToken()
186
    {
187
 
188
 
189
        if($this->cache->hasItem(self::CACHE_OAUTH_TOOKEN)) {
190
            $data = $this->cache->getItem(self::CACHE_OAUTH_TOOKEN);
191
 
192
            return [
193
                'success' => true,
194
                'data' => $data->accesstoken
195
 
196
            ];
197
        }
198
 
199
 
200
 
201
        $account_id     = $this->config['leaderslinked.zoom.account_id'];
202
        $client_id      = $this->config['leaderslinked.zoom.client_id'];
203
        $client_secret  = $this->config['leaderslinked.zoom.client_secret'];
204
 
205
        $client_token_base64_encode =  base64_encode($client_id . ':' . $client_secret);
206
 
207
 
208
        $timecalled = time();
209
 
210
        $client = new Client([
211
            'base_uri' => 'https://zoom.us',
212
        ]);
213
 
214
        $request = [
215
            'headers' => [
216
                'Authorization' => 'Basic ' . $client_token_base64_encode,
217
                'Accept' =>  'application/json'
218
            ],
219
            'form_params' => [
220
                'grant_type' => 'account_credentials',
221
                'account_id' => $account_id
222
            ],
223
        ];
224
 
225
 
226
        $response = $client->request('POST', '/oauth/token', $request );
227
        $statusCode = $response->getStatusCode() ;
228
 
229
 
230
        if($statusCode >= 200 && $statusCode <= 300) {
231
            $response = json_decode($response->getBody()->getContents());
232
 
233
            $requiredscopes = [
234
                'meeting:read:admin',
235
                'meeting:write:admin',
236
                'user:read:admin',
237
            ];
238
            $scopes = explode(' ', $response->scope);
239
            $missingscopes = array_diff($requiredscopes, $scopes);
240
 
241
            if (!empty($missingscopes)) {
242
                $missingscopes = implode(', ', $missingscopes);
243
 
244
 
245
                return [
246
                    'success' => false,
247
                    'data' => 'ERROR_OAUTH_MISSING_SCOPES ' . $missingscopes
248
                ];
249
 
250
 
251
 
252
 
253
            }
254
            /*
255
            echo '<pre>';
256
            print_r($response);
257
            echo '</pre>';
258
            */
259
 
260
 
261
 
262
            if (isset($response->expires_in)) {
263
                $expires = $response->expires_in + $timecalled;
264
            } else {
265
                $expires = 3599 + $timecalled;
266
            }
267
 
268
            $obj = new \stdClass();
269
            $obj->accesstoken   = $response->access_token;
270
            $obj->expires       = $expires;
271
            $obj->scopes        = $scopes;
272
 
273
            $this->cache->setItem(self::CACHE_OAUTH_TOOKEN, $obj);
274
 
275
            return [
276
                'success' => true,
277
                'data' => $response->access_token
278
            ];
279
 
280
 
281
        } else {
282
            return [
283
                'success' => false,
284
                'data' => $response->getStatusCode()
285
            ];
286
 
287
 
288
            return $obj;
289
 
290
        }
291
    }
292
 
293
 
294
}