Proyectos de Subversion LeadersLinked - Backend

Rev

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