Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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