Browse code

added CHANGELOG.txt appinfo/info.xml appinfo/signature.json js/sendfax.js lib/Controller/AuthorApiController.php lib/Controller/PaxfaxController.php

DoubleBastionAdmin authored on 22/03/2023 19:45:04
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,602 @@
1
+<?php
2
+/**
3
+ * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
4
+ *
5
+ * @author Double Bastion LLC
6
+ *
7
+ * @license GNU AGPL version 3 or any later version
8
+ *
9
+ * This program is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
+ * License as published by the Free Software Foundation; either
12
+ * version 3 of the License, or any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
+ *
19
+ * You should have received a copy of the GNU Affero General Public
20
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+declare(strict_types=1);
25
+
26
+namespace OCA\PaxFax\Controller;
27
+
28
+use OCP\IRequest;
29
+use OCP\AppFramework\Controller;
30
+use OCA\PaxFax\Service\PaxfaxService;
31
+use OCP\AppFramework\App;
32
+use OCP\Files\NotPermittedException;
33
+use OCP\Files\Folder;
34
+use OCP\IConfig;
35
+use OC\Files\Filesystem;
36
+use OC\Files\View;
37
+use \ReflectionClass;
38
+use \FilesystemIterator;
39
+
40
+use Phaxio;
41
+use Phaxio\OperationResult;
42
+use Phaxio\Error\AuthenticationException;
43
+use Phaxio\Error\NotFoundException;
44
+use Phaxio\Error\InvalidRequestException;
45
+use Phaxio\Error\RateLimitException;
46
+use Phaxio\Error\APIConnectionException;
47
+use Phaxio\Error\GeneralException;
48
+
49
+
50
+class PaxfaxController extends Controller {
51
+
52
+    private $service;
53
+    private $config;
54
+    private $userId;
55
+    private $folder;
56
+    private $filesystem;
57
+    private $view;
58
+
59
+    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view) {
60
+
61
+           parent::__construct($appName, $request);
62
+
63
+           $this->service = $service;
64
+           $this->config = $config;
65
+           $this->userId = $userId;
66
+           $this->folder = $folder;
67
+           $this->filesystem = $filesystem;
68
+           $this->view = $view;
69
+    }
70
+
71
+    /**
72
+     * @NoAdminRequired
73
+     */
74
+    public function cleantempdir($userId) {
75
+
76
+           // If the 'temp_files' folder doesn't exist create it
77
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
78
+               $this->folder->newFolder('Pax_Fax/temp_files');
79
+           }
80
+
81
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
82
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
83
+           $fileSystemIterator = new FilesystemIterator($targetdir);
84
+
85
+           $dirfiles = [];
86
+           foreach ($fileSystemIterator as $fileInfo){
87
+                    $dirfiles[] = $fileInfo->getFilename();
88
+           }
89
+
90
+           foreach ($dirfiles as $key => $indfile) {
91
+                    $thisuserroot = $this->view->getRoot();
92
+                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
93
+                    $removetmpfile = $this->filesystem->unlink($tempfile);
94
+           }
95
+    }
96
+
97
+
98
+    /**
99
+     * @NoAdminRequired
100
+     */
101
+    public function object_to_array($obj) {
102
+
103
+           if(is_object($obj)) $obj = (array)$this->dismount($obj);
104
+           if(is_array($obj)) {
105
+              $new = array();
106
+              foreach($obj as $key => $val) {
107
+                      $new[$key] = $this->object_to_array($val);
108
+              }
109
+           }
110
+           else $new = $obj;
111
+           return $new;
112
+    }
113
+
114
+
115
+    /**
116
+     * @NoAdminRequired
117
+     */
118
+    public function dismount($object) {
119
+           $reflectionClass = new ReflectionClass(get_class($object));
120
+           $array = array();
121
+           foreach ($reflectionClass->getProperties() as $property) {
122
+                    $property->setAccessible(true);
123
+                    $array[$property->getName()] = $property->getValue($object);
124
+                    $property->setAccessible(false);
125
+           }
126
+           return $array;
127
+    }
128
+
129
+
130
+    /**
131
+     * @NoAdminRequired
132
+     */
133
+    public function getbalance($userId) {
134
+
135
+           $apiMode = 'live';
136
+           $thisapicred = $this->service->getapicredentials($this->userId);
137
+
138
+           $apiKeys[$apiMode] = $thisapicred[0];
139
+           $apiSecrets[$apiMode] = $thisapicred[1];
140
+
141
+           $apiHost = 'https://api.phaxio.com/v2.1/';
142
+
143
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
144
+
145
+           try {
146
+                    // Get Phaxio account balance
147
+                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
148
+
149
+                    $balancetoarr = $this->object_to_array($phaxioresulttert);
150
+
151
+                    $phaxiobalance = $balancetoarr['data']['balance'];
152
+
153
+           } catch (InvalidRequestException $e) {
154
+                    $phaxiobalance = 'unknown';
155
+                    $phaxiosuccess = 'false';
156
+                    $errortype = 'invalid request error';
157
+           } catch (AuthenticationException $e) {
158
+                    $phaxiobalance = 'unknown';
159
+                    $phaxiosuccess = 'false';
160
+                    $errortype = 'authentication error';
161
+           } catch (APIConnectionException $e) {
162
+                    $phaxiobalance = 'unknown';
163
+                    $phaxiosuccess = 'false';
164
+                    $errortype = 'API connection error';
165
+           } catch (RateLimitException $e) {
166
+                    $phaxiobalance = 'unknown';
167
+                    $phaxiosuccess = 'false';
168
+                    $errortype = 'rate limit error';
169
+           } catch (NotFoundException $e) {
170
+                    $phaxiobalance = 'unknown';
171
+                    $phaxiosuccess = 'false';
172
+                    $errortype = 'not found error';
173
+           } catch (GeneralException $e) {
174
+                    $phaxiobalance = 'unknown';
175
+                    $phaxiosuccess = 'false';
176
+                    $errortype = 'undefined error';
177
+           }
178
+
179
+           return $phaxiobalance;
180
+
181
+    }
182
+
183
+
184
+    /**
185
+     * @NoAdminRequired
186
+     */
187
+    public function getfaxnumbers($userId) {
188
+
189
+           $apiMode = 'live';
190
+           $thisapicred = $this->service->getapicredentials($this->userId);
191
+
192
+           $apiKeys[$apiMode] = $thisapicred[0];
193
+           $apiSecrets[$apiMode] = $thisapicred[1];
194
+
195
+           $apiHost = 'https://api.phaxio.com/v2.1/';
196
+
197
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
198
+
199
+           try {
200
+                    // Get all Phaxio fax numbers
201
+                    $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
202
+
203
+                    $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
204
+
205
+                    $phaxioarr = [];
206
+
207
+                    foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
208
+                           if (is_array($phvalue)) {
209
+                                 foreach ($phvalue as $phkey2 => $phvalue2) {
210
+                                       if ($phkey2 == 'phone_number') {
211
+                                           $phaxioarr[] = $phvalue2;
212
+                                       }
213
+                                 }
214
+                           }
215
+                    }
216
+
217
+                    $phaxionmbrs = $phaxioarr;
218
+
219
+           } catch (InvalidRequestException $e) {
220
+                    $phaxiobalance = 'unknown';
221
+                    $phaxiosuccess = 'false';
222
+                    $errortype = 'invalid request error';
223
+           } catch (AuthenticationException $e) {
224
+                    $phaxiobalance = 'unknown';
225
+                    $phaxiosuccess = 'false';
226
+                    $errortype = 'authentication error';
227
+           } catch (APIConnectionException $e) {
228
+                    $phaxiobalance = 'unknown';
229
+                    $phaxiosuccess = 'false';
230
+                    $errortype = 'API connection error';
231
+           } catch (RateLimitException $e) {
232
+                    $phaxiobalance = 'unknown';
233
+                    $phaxiosuccess = 'false';
234
+                    $errortype = 'rate limit error';
235
+           } catch (NotFoundException $e) {
236
+                    $phaxiobalance = 'unknown';
237
+                    $phaxiosuccess = 'false';
238
+                    $errortype = 'not found error';
239
+           } catch (GeneralException $e) {
240
+                    $phaxiobalance = 'unknown';
241
+                    $phaxiosuccess = 'false';
242
+                    $errortype = 'undefined error';
243
+           }
244
+
245
+           return $phaxionmbrs;
246
+    }
247
+
248
+    /**
249
+     * @NoAdminRequired
250
+     */
251
+    protected function getFileID() {
252
+		if ($this->createdFile) {
253
+			return $this->createdFile;
254
+		}
255
+
256
+		$qb = $this->connection->getQueryBuilder();
257
+
258
+		// We create a new file entry and delete it after the test again
259
+		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
260
+		$qb->insert('filecache')
261
+			->values([
262
+				'path' => $qb->createNamedParameter($fileName),
263
+				'path_hash' => $qb->createNamedParameter(md5($fileName)),
264
+			])
265
+			->execute();
266
+		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
267
+		$qb->insert('filecache')
268
+			->values([
269
+				'path' => $qb->createNamedParameter($fileName),
270
+				'path_hash' => $qb->createNamedParameter(md5($fileName)),
271
+			])
272
+			->execute();
273
+
274
+		$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
275
+		return $this->createdFile;
276
+    }
277
+
278
+    /**
279
+     * @NoAdminRequired
280
+     */
281
+    public function uploadfile($uploadfileforfax) {
282
+
283
+           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
284
+           $fileName = $_FILES['uploadfileforfax']['name'];
285
+           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
286
+           $fileSize = $fileSizeinit / 1048576;
287
+
288
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
289
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
290
+           }
291
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
292
+               $this->folder->newFolder('Pax_Fax/faxes_received');
293
+           }
294
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
295
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
296
+           }
297
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
298
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
299
+           }
300
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
301
+               $this->folder->newFolder('Pax_Fax/temp_files');
302
+           }
303
+
304
+           $userroot = $this->view->getRoot();
305
+           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
306
+
307
+           $target = $this->folder->newFile($targetfile);
308
+           $target->putContent($fileContent);
309
+
310
+           // Get the cumulative files size of the uploaded files
311
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
312
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
313
+
314
+           $fileSystemIterator = new FilesystemIterator($targetdir);
315
+
316
+           $dirfiles = [];
317
+           foreach ($fileSystemIterator as $fileInfo){
318
+                    $dirfiles[] = $fileInfo->getFilename();
319
+           }
320
+
321
+           $totalflsizeinit = 0;
322
+           foreach ($dirfiles as $key => $indfile) {
323
+                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
324
+                    $mbSize = $fileSizeinit / 1048576;
325
+                    $totalflsizeinit += $mbSize;
326
+           }
327
+
328
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
329
+
330
+           return $totalflsize;
331
+    }
332
+
333
+    /**
334
+     * @NoAdminRequired
335
+     */
336
+    public function pickfile($path) {
337
+
338
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
339
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
340
+           }
341
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
342
+               $this->folder->newFolder('Pax_Fax/faxes_received');
343
+           }
344
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
345
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
346
+           }
347
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
348
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
349
+           }
350
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
351
+               $this->folder->newFolder('Pax_Fax/temp_files');
352
+           }
353
+
354
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
355
+
356
+           $fltgt = $datadir . $this->userId . "/files" . $path;
357
+
358
+           $fileContentpk = file_get_contents($fltgt);
359
+
360
+           $patharr = explode("/", $path);
361
+
362
+           $revarr = array_reverse($patharr);
363
+
364
+           $relflpath = "/Pax_Fax/temp_files/" . $revarr[0]; 
365
+
366
+           $target = $this->folder->newFile($relflpath);
367
+
368
+           $target->putContent($fileContentpk);
369
+
370
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
371
+
372
+           $fileSystemIterator = new FilesystemIterator($targetdir);
373
+
374
+           $dirfiles = [];
375
+           foreach ($fileSystemIterator as $fileInfo) {
376
+                    $dirfiles[] = $fileInfo->getFilename();
377
+           }
378
+
379
+           $totalflsizeinit = 0;
380
+
381
+           foreach ($dirfiles as $key => $indfile) {
382
+
383
+                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
384
+                    $mbSize = $fileSizeinit / 1048576;
385
+                    $totalflsizeinit += $mbSize;
386
+           }
387
+
388
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
389
+
390
+           return $totalflsize;
391
+    }
392
+
393
+    /**
394
+     * @NoAdminRequired
395
+     */
396
+    public function removeupfile($removedfilename) {
397
+
398
+           $tmpfl = "/" . $this->userId . "/files/Pax_Fax/temp_files/" . $removedfilename;
399
+
400
+           $removefile = $this->view->unlink($tmpfl);
401
+
402
+           // Get the cumulative files size of the uploaded files
403
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
404
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
405
+           $fileSystemIterator = new FilesystemIterator($targetdir);
406
+
407
+           $dirfiles = [];
408
+           foreach ($fileSystemIterator as $fileInfo) {
409
+                    $dirfiles[] = $fileInfo->getFilename();
410
+           }
411
+
412
+           $totalflsizeinit = 0;
413
+
414
+           foreach ($dirfiles as $key => $indfile) {
415
+                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
416
+                    $mbSize = $fileSizeinit / 1048576;
417
+                    $totalflsizeinit += $mbSize;
418
+           }
419
+
420
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
421
+
422
+           return $totalflsize;
423
+    }
424
+
425
+    /**
426
+     * @NoAdminRequired
427
+     */
428
+    public function getpickedfile($pickedfilename) {
429
+
430
+           $thisuserroot = $this->view->getRoot();
431
+
432
+           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
433
+           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
434
+
435
+           $namesplit = explode(".", $pickedfilename);
436
+           $extension = end($namesplit);
437
+
438
+           if ($extension == "txt" || $extension == "html") {
439
+               $getpickedfile = $getfilecontent;
440
+           } elseif ($extension == "jpg") {
441
+               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
442
+           } elseif ($extension == "png") {
443
+               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
444
+           } else { $getpickedfile = ""; }
445
+
446
+           return $getpickedfile;
447
+    }
448
+
449
+    /**
450
+     * @NoAdminRequired
451
+     */
452
+    public function sendfax($userId, $uploadedtofax, $selectedcid, $toNumber) {
453
+
454
+           $tonumbertr = str_replace("+", "", $toNumber[0]);
455
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
456
+
457
+           $fromnumberdigits = str_replace("+", "", $selectedcid);
458
+           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
459
+
460
+           $countfaxfiles = count($uploadedtofax);
461
+
462
+           $openedfiles = [];
463
+
464
+           foreach ($uploadedtofax as $key => $flname) {
465
+
466
+                    if ($key == 0) { $firstflname = $flname; }
467
+
468
+                    $fileNamesec = array_reverse(explode(".", $flname));
469
+                    $filenameext = $fileNamesec[0];
470
+                    array_shift($fileNamesec);
471
+
472
+                    $fileName = implode("", $fileNamesec);
473
+
474
+                    if ($countfaxfiles == 1) {
475
+                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
476
+                    } else {
477
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
478
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
479
+                         }
480
+
481
+                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
482
+                    }
483
+
484
+                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
485
+
486
+                    $fltgtsnd = $datadir . $this->userId . "/files/Pax_Fax/temp_files/" . $flname;
487
+                    $fltgtsndtmp = "/Pax_Fax/temp_files/" . $flname;
488
+
489
+                    $fileContentsnd = file_get_contents($fltgtsnd);
490
+
491
+                    $target = $this->folder->newFile($targetfile);
492
+                    $target->putContent($fileContentsnd);
493
+
494
+                    $removetmpfile = $this->filesystem->unlink($fltgtsndtmp);
495
+
496
+                    if ($countfaxfiles == 1) {
497
+                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
498
+                    } else {
499
+                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
500
+                    }
501
+           }
502
+
503
+           $params = array(
504
+                           'to' => $toNumber,
505
+                           'file' => $openedfiles,
506
+                           'caller_id' => $selectedcid
507
+                     );
508
+
509
+           $apiMode = 'live';
510
+
511
+           $thisapicred = $this->service->getapicredentials($this->userId);
512
+
513
+           $apiKeys[$apiMode] = $thisapicred[0];
514
+           $apiSecrets[$apiMode] = $thisapicred[1];
515
+
516
+           $apiHost = 'https://api.phaxio.com/v2.1/';
517
+
518
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
519
+
520
+           try {
521
+                    $phaxioresultinit = $phaxio->sendFax($params);
522
+
523
+                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
524
+
525
+                    $statustoarr = $this->object_to_array($phaxioresultsec);
526
+
527
+                    $phaxiosuccess = $statustoarr['success'];
528
+
529
+                    $errortype = 'there are no errors';
530
+
531
+           } catch (InvalidRequestException $e) {
532
+                    $phaxiosuccess = 'false';
533
+                    $errortype = 'invalid request error';
534
+           } catch (AuthenticationException $e) {
535
+                    $phaxiosuccess = 'false';
536
+                    $errortype = 'authentication error';
537
+           } catch (APIConnectionException $e) {
538
+                    $phaxiosuccess = 'false';
539
+                    $errortype = 'API connection error';
540
+           } catch (RateLimitException $e) {
541
+                    $phaxiosuccess = 'false';
542
+                    $errortype = 'rate limit error';
543
+           } catch (NotFoundException $e) {
544
+                    $phaxiosuccess = 'false';
545
+                    $errortype = 'not found error';
546
+           } catch (GeneralException $e) {
547
+                    $phaxiosuccess = 'false';
548
+                    $errortype = 'undefined error';
549
+           }
550
+
551
+           $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
552
+
553
+           if ($phaxiosuccess != 'true') {
554
+
555
+                    foreach ($uploadedtofax as $key => $flname) {
556
+
557
+                        $fileNamesec = array_reverse(explode(".", $flname));
558
+                        $filenameext = $fileNamesec[0];
559
+                        array_shift($fileNamesec);
560
+
561
+                        $fileName = implode("", $fileNamesec);
562
+
563
+                        if ($countfaxfiles == 1) {
564
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
565
+                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
566
+                        } else {
567
+
568
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
569
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
570
+                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
571
+                        }
572
+
573
+                        $fileContentfld = $this->filesystem->file_get_contents($failedfl);
574
+
575
+                        $targetact = $this->folder->newFile($newtargetfl);
576
+                        $targetact->putContent($fileContentfld);
577
+
578
+                        $removefailed = $this->filesystem->unlink($failedfl);
579
+                    }
580
+
581
+           }
582
+
583
+           return $phaxioresult;
584
+    }
585
+
586
+
587
+    /**
588
+     * @NoAdminRequired
589
+     */
590
+    public function getsettings($userId) {
591
+           return $this->service->getsettings($this->userId);
592
+    }
593
+
594
+
595
+    /**
596
+     * @NoAdminRequired
597
+     */
598
+    public function updatesettings($userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail) {
599
+           return $this->service->updatesettings($this->userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail);
600
+    }
601
+
602
+}
Browse code

removed CHANGELOG.txt appinfo/info.xml appinfo/signature.json js/sendfax.js lib/Controller/AuthorApiController.php lib/Controller/PaxfaxController.php

DoubleBastionAdmin authored on 22/03/2023 19:26:29
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,599 +0,0 @@
1
-<?php
2
-/**
3
- * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
4
- *
5
- * @author Double Bastion LLC
6
- *
7
- * @license GNU AGPL version 3 or any later version
8
- *
9
- * This program is free software; you can redistribute it and/or
10
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
- * License as published by the Free Software Foundation; either
12
- * version 3 of the License, or any later version.
13
- *
14
- * This program is distributed in the hope that it will be useful,
15
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
- *
19
- * You should have received a copy of the GNU Affero General Public
20
- * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
- *
22
- */
23
-
24
-declare(strict_types=1);
25
-
26
-namespace OCA\PaxFax\Controller;
27
-
28
-use OCP\IRequest;
29
-use OCP\AppFramework\Controller;
30
-use OCA\PaxFax\Service\PaxfaxService;
31
-use OCP\AppFramework\App;
32
-use OCP\Files\NotPermittedException;
33
-use OCP\Files\Folder;
34
-use OCP\IConfig;
35
-use OC\Files\Filesystem;
36
-use OC\Files\View;
37
-use \ReflectionClass;
38
-use \FilesystemIterator;
39
-
40
-use Phaxio;
41
-use Phaxio\OperationResult;
42
-use Phaxio\Error\AuthenticationException;
43
-use Phaxio\Error\NotFoundException;
44
-use Phaxio\Error\InvalidRequestException;
45
-use Phaxio\Error\RateLimitException;
46
-use Phaxio\Error\APIConnectionException;
47
-use Phaxio\Error\GeneralException;
48
-
49
-
50
-class PaxfaxController extends Controller {
51
-
52
-    private $service;
53
-    private $config;
54
-    private $userId;
55
-    private $folder;
56
-    private $filesystem;
57
-    private $view;
58
-
59
-    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view) {
60
-
61
-           parent::__construct($appName, $request);
62
-
63
-           $this->service = $service;
64
-           $this->config = $config;
65
-           $this->userId = $userId;
66
-           $this->folder = $folder;
67
-           $this->filesystem = $filesystem;
68
-           $this->view = $view;
69
-    }
70
-
71
-    /**
72
-     * @NoAdminRequired
73
-     */
74
-    public function cleantempdir($userId) {
75
-
76
-           // If the 'temp_files' folder doesn't exist create it
77
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
78
-               $this->folder->newFolder('Pax_Fax/temp_files');
79
-           }
80
-
81
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
82
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
83
-           $fileSystemIterator = new FilesystemIterator($targetdir);
84
-
85
-           $dirfiles = [];
86
-           foreach ($fileSystemIterator as $fileInfo){
87
-                    $dirfiles[] = $fileInfo->getFilename();
88
-           }
89
-
90
-           foreach ($dirfiles as $key => $indfile) {
91
-                    $thisuserroot = $this->view->getRoot();
92
-                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
93
-                    $removetmpfile = $this->filesystem->unlink($tempfile);
94
-           }
95
-    }
96
-
97
-
98
-    /**
99
-     * @NoAdminRequired
100
-     */
101
-    public function object_to_array($obj) {
102
-
103
-           if(is_object($obj)) $obj = (array)$this->dismount($obj);
104
-           if(is_array($obj)) {
105
-              $new = array();
106
-              foreach($obj as $key => $val) {
107
-                      $new[$key] = $this->object_to_array($val);
108
-              }
109
-           }
110
-           else $new = $obj;
111
-           return $new;
112
-    }
113
-
114
-
115
-    /**
116
-     * @NoAdminRequired
117
-     */
118
-    public function dismount($object) {
119
-           $reflectionClass = new ReflectionClass(get_class($object));
120
-           $array = array();
121
-           foreach ($reflectionClass->getProperties() as $property) {
122
-                    $property->setAccessible(true);
123
-                    $array[$property->getName()] = $property->getValue($object);
124
-                    $property->setAccessible(false);
125
-           }
126
-           return $array;
127
-    }
128
-
129
-
130
-    /**
131
-     * @NoAdminRequired
132
-     */
133
-    public function getbalance($userId) {
134
-
135
-           $apiMode = 'live';
136
-           $thisapicred = $this->service->getapicredentials($this->userId);
137
-
138
-           $apiKeys[$apiMode] = $thisapicred[0];
139
-           $apiSecrets[$apiMode] = $thisapicred[1];
140
-
141
-           $apiHost = 'https://api.phaxio.com/v2.1/';
142
-
143
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
144
-
145
-           try {
146
-                    // Get Phaxio account balance
147
-                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
148
-
149
-                    $balancetoarr = $this->object_to_array($phaxioresulttert);
150
-
151
-                    $phaxiobalance = $balancetoarr['data']['balance'];
152
-
153
-           } catch (InvalidRequestException $e) {
154
-                    $phaxiobalance = 'unknown';
155
-                    $phaxiosuccess = 'false';
156
-                    $errortype = 'invalid request error';
157
-           } catch (AuthenticationException $e) {
158
-                    $phaxiobalance = 'unknown';
159
-                    $phaxiosuccess = 'false';
160
-                    $errortype = 'authentication error';
161
-           } catch (APIConnectionException $e) {
162
-                    $phaxiobalance = 'unknown';
163
-                    $phaxiosuccess = 'false';
164
-                    $errortype = 'API connection error';
165
-           } catch (RateLimitException $e) {
166
-                    $phaxiobalance = 'unknown';
167
-                    $phaxiosuccess = 'false';
168
-                    $errortype = 'rate limit error';
169
-           } catch (NotFoundException $e) {
170
-                    $phaxiobalance = 'unknown';
171
-                    $phaxiosuccess = 'false';
172
-                    $errortype = 'not found error';
173
-           } catch (GeneralException $e) {
174
-                    $phaxiobalance = 'unknown';
175
-                    $phaxiosuccess = 'false';
176
-                    $errortype = 'undefined error';
177
-           }
178
-
179
-           return $phaxiobalance;
180
-
181
-    }
182
-
183
-
184
-    /**
185
-     * @NoAdminRequired
186
-     */
187
-    public function getfaxnumbers($userId) {
188
-
189
-           $apiMode = 'live';
190
-           $thisapicred = $this->service->getapicredentials($this->userId);
191
-
192
-           $apiKeys[$apiMode] = $thisapicred[0];
193
-           $apiSecrets[$apiMode] = $thisapicred[1];
194
-
195
-           $apiHost = 'https://api.phaxio.com/v2.1/';
196
-
197
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
198
-
199
-           try {
200
-                    // Get all Phaxio fax numbers
201
-                    $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
202
-
203
-                    $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
204
-
205
-                    $phaxioarr = [];
206
-
207
-                    foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
208
-                           if (is_array($phvalue)) {
209
-                                 foreach ($phvalue as $phkey2 => $phvalue2) {
210
-                                       if ($phkey2 == 'phone_number') {
211
-                                           $phaxioarr[] = $phvalue2;
212
-                                       }
213
-                                 }
214
-                           }
215
-                    }
216
-
217
-                    $phaxionmbrs = $phaxioarr;
218
-
219
-           } catch (InvalidRequestException $e) {
220
-                    $phaxiobalance = 'unknown';
221
-                    $phaxiosuccess = 'false';
222
-                    $errortype = 'invalid request error';
223
-           } catch (AuthenticationException $e) {
224
-                    $phaxiobalance = 'unknown';
225
-                    $phaxiosuccess = 'false';
226
-                    $errortype = 'authentication error';
227
-           } catch (APIConnectionException $e) {
228
-                    $phaxiobalance = 'unknown';
229
-                    $phaxiosuccess = 'false';
230
-                    $errortype = 'API connection error';
231
-           } catch (RateLimitException $e) {
232
-                    $phaxiobalance = 'unknown';
233
-                    $phaxiosuccess = 'false';
234
-                    $errortype = 'rate limit error';
235
-           } catch (NotFoundException $e) {
236
-                    $phaxiobalance = 'unknown';
237
-                    $phaxiosuccess = 'false';
238
-                    $errortype = 'not found error';
239
-           } catch (GeneralException $e) {
240
-                    $phaxiobalance = 'unknown';
241
-                    $phaxiosuccess = 'false';
242
-                    $errortype = 'undefined error';
243
-           }
244
-
245
-           return $phaxionmbrs;
246
-    }
247
-
248
-    /**
249
-     * @NoAdminRequired
250
-     */
251
-    protected function getFileID() {
252
-		if ($this->createdFile) {
253
-			return $this->createdFile;
254
-		}
255
-
256
-		$qb = $this->connection->getQueryBuilder();
257
-
258
-		// We create a new file entry and delete it after the test again
259
-		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
260
-		$qb->insert('filecache')
261
-			->values([
262
-				'path' => $qb->createNamedParameter($fileName),
263
-				'path_hash' => $qb->createNamedParameter(md5($fileName)),
264
-			])
265
-			->execute();
266
-		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
267
-		$qb->insert('filecache')
268
-			->values([
269
-				'path' => $qb->createNamedParameter($fileName),
270
-				'path_hash' => $qb->createNamedParameter(md5($fileName)),
271
-			])
272
-			->execute();
273
-
274
-		$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
275
-		return $this->createdFile;
276
-    }
277
-
278
-    /**
279
-     * @NoAdminRequired
280
-     */
281
-    public function uploadfile($uploadfileforfax) {
282
-
283
-           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
284
-           $fileName = $_FILES['uploadfileforfax']['name'];
285
-           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
286
-           $fileSize = $fileSizeinit / 1048576;
287
-
288
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
289
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
290
-           }
291
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
292
-               $this->folder->newFolder('Pax_Fax/faxes_received');
293
-           }
294
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
295
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
296
-           }
297
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
298
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
299
-           }
300
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
301
-               $this->folder->newFolder('Pax_Fax/temp_files');
302
-           }
303
-
304
-           $userroot = $this->view->getRoot();
305
-           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
306
-
307
-           $target = $this->folder->newFile($targetfile);
308
-           $target->putContent($fileContent);
309
-
310
-           // Get the cumulative files size of the uploaded files
311
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
312
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
313
-
314
-           $fileSystemIterator = new FilesystemIterator($targetdir);
315
-
316
-           $dirfiles = [];
317
-           foreach ($fileSystemIterator as $fileInfo){
318
-                    $dirfiles[] = $fileInfo->getFilename();
319
-           }
320
-
321
-           $totalflsizeinit = 0;
322
-           foreach ($dirfiles as $key => $indfile) {
323
-                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
324
-                    $mbSize = $fileSizeinit / 1048576;
325
-                    $totalflsizeinit += $mbSize;
326
-           }
327
-
328
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
329
-
330
-           return $totalflsize;
331
-    }
332
-
333
-    /**
334
-     * @NoAdminRequired
335
-     */
336
-    public function pickfile($path) {
337
-
338
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
339
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
340
-           }
341
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
342
-               $this->folder->newFolder('Pax_Fax/faxes_received');
343
-           }
344
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
345
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
346
-           }
347
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
348
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
349
-           }
350
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
351
-               $this->folder->newFolder('Pax_Fax/temp_files');
352
-           }
353
-
354
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
355
-
356
-           $fltgt = $datadir . $this->userId . "/files" . $path;
357
-
358
-           $fileContent = file_get_contents($fltgt);
359
-
360
-           $patharr = explode("/", $path);
361
-
362
-           $revarr = array_reverse($patharr);
363
-
364
-           $relflpath = "/Pax_Fax/temp_files/" . $revarr[0]; 
365
-
366
-           $target = $this->folder->newFile($relflpath);
367
-
368
-           $target->putContent($fileContent);
369
-
370
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
371
-
372
-           $fileSystemIterator = new FilesystemIterator($targetdir);
373
-
374
-           $dirfiles = [];
375
-           foreach ($fileSystemIterator as $fileInfo) {
376
-                    $dirfiles[] = $fileInfo->getFilename();
377
-           }
378
-
379
-           $totalflsizeinit = 0;
380
-
381
-           foreach ($dirfiles as $key => $indfile) {
382
-
383
-                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
384
-                    $mbSize = $fileSizeinit / 1048576;
385
-                    $totalflsizeinit += $mbSize;
386
-           }
387
-
388
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
389
-
390
-           return $totalflsize;
391
-    }
392
-
393
-    /**
394
-     * @NoAdminRequired
395
-     */
396
-    public function removeupfile($removedfilename) {
397
-
398
-           $tmpfl = "/" . $this->userId . "/files/Pax_Fax/temp_files/" . $removedfilename;
399
-
400
-           $removefile = $this->view->unlink($tmpfl);
401
-
402
-           // Get the cumulative files size of the uploaded files
403
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
404
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
405
-           $fileSystemIterator = new FilesystemIterator($targetdir);
406
-
407
-           $dirfiles = [];
408
-           foreach ($fileSystemIterator as $fileInfo){
409
-                    $dirfiles[] = $fileInfo->getFilename();
410
-           }
411
-
412
-           $totalflsizeinit = 0;
413
-
414
-           foreach ($dirfiles as $key => $indfile) {
415
-                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
416
-                    $mbSize = $fileSizeinit / 1048576;
417
-                    $totalflsizeinit += $mbSize;
418
-           }
419
-
420
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
421
-
422
-           return $totalflsize;
423
-    }
424
-
425
-    /**
426
-     * @NoAdminRequired
427
-     */
428
-    public function getpickedfile($pickedfilename) {
429
-
430
-           $thisuserroot = $this->view->getRoot();
431
-
432
-           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
433
-           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
434
-
435
-           $namesplit = explode(".", $pickedfilename);
436
-           $extension = end($namesplit);
437
-
438
-           if ($extension == "txt" || $extension == "html") {
439
-               $getpickedfile = $getfilecontent;
440
-           } elseif ($extension == "jpg") {
441
-               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
442
-           } elseif ($extension == "png") {
443
-               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
444
-           } else { $getpickedfile = ""; }
445
-
446
-           return $getpickedfile;
447
-    }
448
-
449
-    /**
450
-     * @NoAdminRequired
451
-     */
452
-    public function sendfax($userId, $uploadedtofax, $selectedcid, $toNumber) {
453
-
454
-           $tonumbertr = str_replace("+", "", $toNumber[0]);
455
-           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
456
-
457
-           $fromnumberdigits = str_replace("+", "", $selectedcid);
458
-           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
459
-
460
-           $countfaxfiles = count($uploadedtofax);
461
-
462
-           $openedfiles = [];
463
-
464
-           foreach ($uploadedtofax as $key => $flname) {
465
-
466
-                    if ($key == 0) { $firstflname = $flname; }
467
-
468
-                    $fileNamesec = array_reverse(explode(".", $flname));
469
-                    $filenameext = $fileNamesec[0];
470
-                    array_shift($fileNamesec);
471
-
472
-                    $fileName = implode("", $fileNamesec);
473
-
474
-                    if ($countfaxfiles == 1) {
475
-                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
476
-                    } else {
477
-                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
478
-                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
479
-                         }
480
-
481
-                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
482
-                    }
483
-
484
-                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
485
-                    $fltgt = $datadir . $this->userId . "/files/Pax_Fax/temp_files/" . $flname;
486
-                    $fileContent = file_get_contents($fltgt);
487
-
488
-                    $target = $this->folder->newFile($targetfile);
489
-                    $target->putContent($fileContent);
490
-
491
-                    $removetmpfile = $this->filesystem->unlink($fltgt);
492
-
493
-                    if ($countfaxfiles == 1) {
494
-                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
495
-                    } else {
496
-                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
497
-                    }
498
-           }
499
-
500
-           $params = array(
501
-                           'to' => $toNumber,
502
-                           'file' => $openedfiles,
503
-                           'caller_id' => $selectedcid
504
-                     );
505
-
506
-           $apiMode = 'live';
507
-
508
-           $thisapicred = $this->service->getapicredentials($this->userId);
509
-
510
-           $apiKeys[$apiMode] = $thisapicred[0];
511
-           $apiSecrets[$apiMode] = $thisapicred[1];
512
-
513
-           $apiHost = 'https://api.phaxio.com/v2.1/';
514
-
515
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
516
-
517
-           try {
518
-                    $phaxioresultinit = $phaxio->sendFax($params);
519
-
520
-                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
521
-
522
-                    $statustoarr = $this->object_to_array($phaxioresultsec);
523
-
524
-                    $phaxiosuccess = $statustoarr['success'];
525
-
526
-                    $errortype = 'there are no errors';
527
-
528
-           } catch (InvalidRequestException $e) {
529
-                    $phaxiosuccess = 'false';
530
-                    $errortype = 'invalid request error';
531
-           } catch (AuthenticationException $e) {
532
-                    $phaxiosuccess = 'false';
533
-                    $errortype = 'authentication error';
534
-           } catch (APIConnectionException $e) {
535
-                    $phaxiosuccess = 'false';
536
-                    $errortype = 'API connection error';
537
-           } catch (RateLimitException $e) {
538
-                    $phaxiosuccess = 'false';
539
-                    $errortype = 'rate limit error';
540
-           } catch (NotFoundException $e) {
541
-                    $phaxiosuccess = 'false';
542
-                    $errortype = 'not found error';
543
-           } catch (GeneralException $e) {
544
-                    $phaxiosuccess = 'false';
545
-                    $errortype = 'undefined error';
546
-           }
547
-
548
-           $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
549
-
550
-           if ($phaxiosuccess != 'true') {
551
-
552
-                    foreach ($uploadedtofax as $key => $flname) {
553
-
554
-                        $fileNamesec = array_reverse(explode(".", $flname));
555
-                        $filenameext = $fileNamesec[0];
556
-                        array_shift($fileNamesec);
557
-
558
-                        $fileName = implode("", $fileNamesec);
559
-
560
-                        if ($countfaxfiles == 1) {
561
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
562
-                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
563
-                        } else {
564
-
565
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
566
-                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
567
-                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
568
-                        }
569
-
570
-                        $fileContent = $this->filesystem->file_get_contents($failedfl);
571
-
572
-                        $targetact = $this->folder->newFile($newtargetfl);
573
-                        $targetact->putContent($fileContent);
574
-
575
-                        $removefailed = $this->filesystem->unlink($failedfl);
576
-                    }
577
-
578
-           }
579
-
580
-           return $phaxioresult;
581
-    }
582
-
583
-
584
-    /**
585
-     * @NoAdminRequired
586
-     */
587
-    public function getsettings($userId) {
588
-           return $this->service->getsettings($this->userId);
589
-    }
590
-
591
-
592
-    /**
593
-     * @NoAdminRequired
594
-     */
595
-    public function updatesettings($userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail) {
596
-           return $this->service->updatesettings($this->userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail);
597
-    }
598
-
599
-}
Browse code

added README.md CHANGELOG.txt appinfo/info.xml appinfo/signature.json css/style.css js/sendfax.js templates/navigation/index.php lib/Controller/AuthorApiController.php lib/Controller/PaxfaxController.php

DoubleBastionAdmin authored on 03/09/2022 21:02:20
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,599 @@
1
+<?php
2
+/**
3
+ * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
4
+ *
5
+ * @author Double Bastion LLC
6
+ *
7
+ * @license GNU AGPL version 3 or any later version
8
+ *
9
+ * This program is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
+ * License as published by the Free Software Foundation; either
12
+ * version 3 of the License, or any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
+ *
19
+ * You should have received a copy of the GNU Affero General Public
20
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+declare(strict_types=1);
25
+
26
+namespace OCA\PaxFax\Controller;
27
+
28
+use OCP\IRequest;
29
+use OCP\AppFramework\Controller;
30
+use OCA\PaxFax\Service\PaxfaxService;
31
+use OCP\AppFramework\App;
32
+use OCP\Files\NotPermittedException;
33
+use OCP\Files\Folder;
34
+use OCP\IConfig;
35
+use OC\Files\Filesystem;
36
+use OC\Files\View;
37
+use \ReflectionClass;
38
+use \FilesystemIterator;
39
+
40
+use Phaxio;
41
+use Phaxio\OperationResult;
42
+use Phaxio\Error\AuthenticationException;
43
+use Phaxio\Error\NotFoundException;
44
+use Phaxio\Error\InvalidRequestException;
45
+use Phaxio\Error\RateLimitException;
46
+use Phaxio\Error\APIConnectionException;
47
+use Phaxio\Error\GeneralException;
48
+
49
+
50
+class PaxfaxController extends Controller {
51
+
52
+    private $service;
53
+    private $config;
54
+    private $userId;
55
+    private $folder;
56
+    private $filesystem;
57
+    private $view;
58
+
59
+    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view) {
60
+
61
+           parent::__construct($appName, $request);
62
+
63
+           $this->service = $service;
64
+           $this->config = $config;
65
+           $this->userId = $userId;
66
+           $this->folder = $folder;
67
+           $this->filesystem = $filesystem;
68
+           $this->view = $view;
69
+    }
70
+
71
+    /**
72
+     * @NoAdminRequired
73
+     */
74
+    public function cleantempdir($userId) {
75
+
76
+           // If the 'temp_files' folder doesn't exist create it
77
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
78
+               $this->folder->newFolder('Pax_Fax/temp_files');
79
+           }
80
+
81
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
82
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
83
+           $fileSystemIterator = new FilesystemIterator($targetdir);
84
+
85
+           $dirfiles = [];
86
+           foreach ($fileSystemIterator as $fileInfo){
87
+                    $dirfiles[] = $fileInfo->getFilename();
88
+           }
89
+
90
+           foreach ($dirfiles as $key => $indfile) {
91
+                    $thisuserroot = $this->view->getRoot();
92
+                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
93
+                    $removetmpfile = $this->filesystem->unlink($tempfile);
94
+           }
95
+    }
96
+
97
+
98
+    /**
99
+     * @NoAdminRequired
100
+     */
101
+    public function object_to_array($obj) {
102
+
103
+           if(is_object($obj)) $obj = (array)$this->dismount($obj);
104
+           if(is_array($obj)) {
105
+              $new = array();
106
+              foreach($obj as $key => $val) {
107
+                      $new[$key] = $this->object_to_array($val);
108
+              }
109
+           }
110
+           else $new = $obj;
111
+           return $new;
112
+    }
113
+
114
+
115
+    /**
116
+     * @NoAdminRequired
117
+     */
118
+    public function dismount($object) {
119
+           $reflectionClass = new ReflectionClass(get_class($object));
120
+           $array = array();
121
+           foreach ($reflectionClass->getProperties() as $property) {
122
+                    $property->setAccessible(true);
123
+                    $array[$property->getName()] = $property->getValue($object);
124
+                    $property->setAccessible(false);
125
+           }
126
+           return $array;
127
+    }
128
+
129
+
130
+    /**
131
+     * @NoAdminRequired
132
+     */
133
+    public function getbalance($userId) {
134
+
135
+           $apiMode = 'live';
136
+           $thisapicred = $this->service->getapicredentials($this->userId);
137
+
138
+           $apiKeys[$apiMode] = $thisapicred[0];
139
+           $apiSecrets[$apiMode] = $thisapicred[1];
140
+
141
+           $apiHost = 'https://api.phaxio.com/v2.1/';
142
+
143
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
144
+
145
+           try {
146
+                    // Get Phaxio account balance
147
+                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
148
+
149
+                    $balancetoarr = $this->object_to_array($phaxioresulttert);
150
+
151
+                    $phaxiobalance = $balancetoarr['data']['balance'];
152
+
153
+           } catch (InvalidRequestException $e) {
154
+                    $phaxiobalance = 'unknown';
155
+                    $phaxiosuccess = 'false';
156
+                    $errortype = 'invalid request error';
157
+           } catch (AuthenticationException $e) {
158
+                    $phaxiobalance = 'unknown';
159
+                    $phaxiosuccess = 'false';
160
+                    $errortype = 'authentication error';
161
+           } catch (APIConnectionException $e) {
162
+                    $phaxiobalance = 'unknown';
163
+                    $phaxiosuccess = 'false';
164
+                    $errortype = 'API connection error';
165
+           } catch (RateLimitException $e) {
166
+                    $phaxiobalance = 'unknown';
167
+                    $phaxiosuccess = 'false';
168
+                    $errortype = 'rate limit error';
169
+           } catch (NotFoundException $e) {
170
+                    $phaxiobalance = 'unknown';
171
+                    $phaxiosuccess = 'false';
172
+                    $errortype = 'not found error';
173
+           } catch (GeneralException $e) {
174
+                    $phaxiobalance = 'unknown';
175
+                    $phaxiosuccess = 'false';
176
+                    $errortype = 'undefined error';
177
+           }
178
+
179
+           return $phaxiobalance;
180
+
181
+    }
182
+
183
+
184
+    /**
185
+     * @NoAdminRequired
186
+     */
187
+    public function getfaxnumbers($userId) {
188
+
189
+           $apiMode = 'live';
190
+           $thisapicred = $this->service->getapicredentials($this->userId);
191
+
192
+           $apiKeys[$apiMode] = $thisapicred[0];
193
+           $apiSecrets[$apiMode] = $thisapicred[1];
194
+
195
+           $apiHost = 'https://api.phaxio.com/v2.1/';
196
+
197
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
198
+
199
+           try {
200
+                    // Get all Phaxio fax numbers
201
+                    $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
202
+
203
+                    $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
204
+
205
+                    $phaxioarr = [];
206
+
207
+                    foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
208
+                           if (is_array($phvalue)) {
209
+                                 foreach ($phvalue as $phkey2 => $phvalue2) {
210
+                                       if ($phkey2 == 'phone_number') {
211
+                                           $phaxioarr[] = $phvalue2;
212
+                                       }
213
+                                 }
214
+                           }
215
+                    }
216
+
217
+                    $phaxionmbrs = $phaxioarr;
218
+
219
+           } catch (InvalidRequestException $e) {
220
+                    $phaxiobalance = 'unknown';
221
+                    $phaxiosuccess = 'false';
222
+                    $errortype = 'invalid request error';
223
+           } catch (AuthenticationException $e) {
224
+                    $phaxiobalance = 'unknown';
225
+                    $phaxiosuccess = 'false';
226
+                    $errortype = 'authentication error';
227
+           } catch (APIConnectionException $e) {
228
+                    $phaxiobalance = 'unknown';
229
+                    $phaxiosuccess = 'false';
230
+                    $errortype = 'API connection error';
231
+           } catch (RateLimitException $e) {
232
+                    $phaxiobalance = 'unknown';
233
+                    $phaxiosuccess = 'false';
234
+                    $errortype = 'rate limit error';
235
+           } catch (NotFoundException $e) {
236
+                    $phaxiobalance = 'unknown';
237
+                    $phaxiosuccess = 'false';
238
+                    $errortype = 'not found error';
239
+           } catch (GeneralException $e) {
240
+                    $phaxiobalance = 'unknown';
241
+                    $phaxiosuccess = 'false';
242
+                    $errortype = 'undefined error';
243
+           }
244
+
245
+           return $phaxionmbrs;
246
+    }
247
+
248
+    /**
249
+     * @NoAdminRequired
250
+     */
251
+    protected function getFileID() {
252
+		if ($this->createdFile) {
253
+			return $this->createdFile;
254
+		}
255
+
256
+		$qb = $this->connection->getQueryBuilder();
257
+
258
+		// We create a new file entry and delete it after the test again
259
+		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
260
+		$qb->insert('filecache')
261
+			->values([
262
+				'path' => $qb->createNamedParameter($fileName),
263
+				'path_hash' => $qb->createNamedParameter(md5($fileName)),
264
+			])
265
+			->execute();
266
+		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
267
+		$qb->insert('filecache')
268
+			->values([
269
+				'path' => $qb->createNamedParameter($fileName),
270
+				'path_hash' => $qb->createNamedParameter(md5($fileName)),
271
+			])
272
+			->execute();
273
+
274
+		$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
275
+		return $this->createdFile;
276
+    }
277
+
278
+    /**
279
+     * @NoAdminRequired
280
+     */
281
+    public function uploadfile($uploadfileforfax) {
282
+
283
+           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
284
+           $fileName = $_FILES['uploadfileforfax']['name'];
285
+           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
286
+           $fileSize = $fileSizeinit / 1048576;
287
+
288
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
289
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
290
+           }
291
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
292
+               $this->folder->newFolder('Pax_Fax/faxes_received');
293
+           }
294
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
295
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
296
+           }
297
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
298
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
299
+           }
300
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
301
+               $this->folder->newFolder('Pax_Fax/temp_files');
302
+           }
303
+
304
+           $userroot = $this->view->getRoot();
305
+           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
306
+
307
+           $target = $this->folder->newFile($targetfile);
308
+           $target->putContent($fileContent);
309
+
310
+           // Get the cumulative files size of the uploaded files
311
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
312
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
313
+
314
+           $fileSystemIterator = new FilesystemIterator($targetdir);
315
+
316
+           $dirfiles = [];
317
+           foreach ($fileSystemIterator as $fileInfo){
318
+                    $dirfiles[] = $fileInfo->getFilename();
319
+           }
320
+
321
+           $totalflsizeinit = 0;
322
+           foreach ($dirfiles as $key => $indfile) {
323
+                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
324
+                    $mbSize = $fileSizeinit / 1048576;
325
+                    $totalflsizeinit += $mbSize;
326
+           }
327
+
328
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
329
+
330
+           return $totalflsize;
331
+    }
332
+
333
+    /**
334
+     * @NoAdminRequired
335
+     */
336
+    public function pickfile($path) {
337
+
338
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
339
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
340
+           }
341
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
342
+               $this->folder->newFolder('Pax_Fax/faxes_received');
343
+           }
344
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
345
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
346
+           }
347
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
348
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
349
+           }
350
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
351
+               $this->folder->newFolder('Pax_Fax/temp_files');
352
+           }
353
+
354
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
355
+
356
+           $fltgt = $datadir . $this->userId . "/files" . $path;
357
+
358
+           $fileContent = file_get_contents($fltgt);
359
+
360
+           $patharr = explode("/", $path);
361
+
362
+           $revarr = array_reverse($patharr);
363
+
364
+           $relflpath = "/Pax_Fax/temp_files/" . $revarr[0]; 
365
+
366
+           $target = $this->folder->newFile($relflpath);
367
+
368
+           $target->putContent($fileContent);
369
+
370
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
371
+
372
+           $fileSystemIterator = new FilesystemIterator($targetdir);
373
+
374
+           $dirfiles = [];
375
+           foreach ($fileSystemIterator as $fileInfo) {
376
+                    $dirfiles[] = $fileInfo->getFilename();
377
+           }
378
+
379
+           $totalflsizeinit = 0;
380
+
381
+           foreach ($dirfiles as $key => $indfile) {
382
+
383
+                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
384
+                    $mbSize = $fileSizeinit / 1048576;
385
+                    $totalflsizeinit += $mbSize;
386
+           }
387
+
388
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
389
+
390
+           return $totalflsize;
391
+    }
392
+
393
+    /**
394
+     * @NoAdminRequired
395
+     */
396
+    public function removeupfile($removedfilename) {
397
+
398
+           $tmpfl = "/" . $this->userId . "/files/Pax_Fax/temp_files/" . $removedfilename;
399
+
400
+           $removefile = $this->view->unlink($tmpfl);
401
+
402
+           // Get the cumulative files size of the uploaded files
403
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
404
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
405
+           $fileSystemIterator = new FilesystemIterator($targetdir);
406
+
407
+           $dirfiles = [];
408
+           foreach ($fileSystemIterator as $fileInfo){
409
+                    $dirfiles[] = $fileInfo->getFilename();
410
+           }
411
+
412
+           $totalflsizeinit = 0;
413
+
414
+           foreach ($dirfiles as $key => $indfile) {
415
+                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
416
+                    $mbSize = $fileSizeinit / 1048576;
417
+                    $totalflsizeinit += $mbSize;
418
+           }
419
+
420
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
421
+
422
+           return $totalflsize;
423
+    }
424
+
425
+    /**
426
+     * @NoAdminRequired
427
+     */
428
+    public function getpickedfile($pickedfilename) {
429
+
430
+           $thisuserroot = $this->view->getRoot();
431
+
432
+           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
433
+           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
434
+
435
+           $namesplit = explode(".", $pickedfilename);
436
+           $extension = end($namesplit);
437
+
438
+           if ($extension == "txt" || $extension == "html") {
439
+               $getpickedfile = $getfilecontent;
440
+           } elseif ($extension == "jpg") {
441
+               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
442
+           } elseif ($extension == "png") {
443
+               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
444
+           } else { $getpickedfile = ""; }
445
+
446
+           return $getpickedfile;
447
+    }
448
+
449
+    /**
450
+     * @NoAdminRequired
451
+     */
452
+    public function sendfax($userId, $uploadedtofax, $selectedcid, $toNumber) {
453
+
454
+           $tonumbertr = str_replace("+", "", $toNumber[0]);
455
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
456
+
457
+           $fromnumberdigits = str_replace("+", "", $selectedcid);
458
+           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
459
+
460
+           $countfaxfiles = count($uploadedtofax);
461
+
462
+           $openedfiles = [];
463
+
464
+           foreach ($uploadedtofax as $key => $flname) {
465
+
466
+                    if ($key == 0) { $firstflname = $flname; }
467
+
468
+                    $fileNamesec = array_reverse(explode(".", $flname));
469
+                    $filenameext = $fileNamesec[0];
470
+                    array_shift($fileNamesec);
471
+
472
+                    $fileName = implode("", $fileNamesec);
473
+
474
+                    if ($countfaxfiles == 1) {
475
+                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
476
+                    } else {
477
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
478
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
479
+                         }
480
+
481
+                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
482
+                    }
483
+
484
+                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
485
+                    $fltgt = $datadir . $this->userId . "/files/Pax_Fax/temp_files/" . $flname;
486
+                    $fileContent = file_get_contents($fltgt);
487
+
488
+                    $target = $this->folder->newFile($targetfile);
489
+                    $target->putContent($fileContent);
490
+
491
+                    $removetmpfile = $this->filesystem->unlink($fltgt);
492
+
493
+                    if ($countfaxfiles == 1) {
494
+                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
495
+                    } else {
496
+                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
497
+                    }
498
+           }
499
+
500
+           $params = array(
501
+                           'to' => $toNumber,
502
+                           'file' => $openedfiles,
503
+                           'caller_id' => $selectedcid
504
+                     );
505
+
506
+           $apiMode = 'live';
507
+
508
+           $thisapicred = $this->service->getapicredentials($this->userId);
509
+
510
+           $apiKeys[$apiMode] = $thisapicred[0];
511
+           $apiSecrets[$apiMode] = $thisapicred[1];
512
+
513
+           $apiHost = 'https://api.phaxio.com/v2.1/';
514
+
515
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
516
+
517
+           try {
518
+                    $phaxioresultinit = $phaxio->sendFax($params);
519
+
520
+                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
521
+
522
+                    $statustoarr = $this->object_to_array($phaxioresultsec);
523
+
524
+                    $phaxiosuccess = $statustoarr['success'];
525
+
526
+                    $errortype = 'there are no errors';
527
+
528
+           } catch (InvalidRequestException $e) {
529
+                    $phaxiosuccess = 'false';
530
+                    $errortype = 'invalid request error';
531
+           } catch (AuthenticationException $e) {
532
+                    $phaxiosuccess = 'false';
533
+                    $errortype = 'authentication error';
534
+           } catch (APIConnectionException $e) {
535
+                    $phaxiosuccess = 'false';
536
+                    $errortype = 'API connection error';
537
+           } catch (RateLimitException $e) {
538
+                    $phaxiosuccess = 'false';
539
+                    $errortype = 'rate limit error';
540
+           } catch (NotFoundException $e) {
541
+                    $phaxiosuccess = 'false';
542
+                    $errortype = 'not found error';
543
+           } catch (GeneralException $e) {
544
+                    $phaxiosuccess = 'false';
545
+                    $errortype = 'undefined error';
546
+           }
547
+
548
+           $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
549
+
550
+           if ($phaxiosuccess != 'true') {
551
+
552
+                    foreach ($uploadedtofax as $key => $flname) {
553
+
554
+                        $fileNamesec = array_reverse(explode(".", $flname));
555
+                        $filenameext = $fileNamesec[0];
556
+                        array_shift($fileNamesec);
557
+
558
+                        $fileName = implode("", $fileNamesec);
559
+
560
+                        if ($countfaxfiles == 1) {
561
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
562
+                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
563
+                        } else {
564
+
565
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
566
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
567
+                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
568
+                        }
569
+
570
+                        $fileContent = $this->filesystem->file_get_contents($failedfl);
571
+
572
+                        $targetact = $this->folder->newFile($newtargetfl);
573
+                        $targetact->putContent($fileContent);
574
+
575
+                        $removefailed = $this->filesystem->unlink($failedfl);
576
+                    }
577
+
578
+           }
579
+
580
+           return $phaxioresult;
581
+    }
582
+
583
+
584
+    /**
585
+     * @NoAdminRequired
586
+     */
587
+    public function getsettings($userId) {
588
+           return $this->service->getsettings($this->userId);
589
+    }
590
+
591
+
592
+    /**
593
+     * @NoAdminRequired
594
+     */
595
+    public function updatesettings($userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail) {
596
+           return $this->service->updatesettings($this->userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail);
597
+    }
598
+
599
+}
Browse code

removed README.md CHANGELOG.txt appinfo/info.xml appinfo/signature.json css/style.css js/sendfax.js templates/navigation/index.php lib/Controller/AuthorApiController.php lib/Controller/PaxfaxController.php

DoubleBastionAdmin authored on 03/09/2022 20:58:28
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,599 +0,0 @@
1
-<?php
2
-/**
3
- * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
4
- *
5
- * @author Double Bastion LLC
6
- *
7
- * @license GNU AGPL version 3 or any later version
8
- *
9
- * This program is free software; you can redistribute it and/or
10
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
- * License as published by the Free Software Foundation; either
12
- * version 3 of the License, or any later version.
13
- *
14
- * This program is distributed in the hope that it will be useful,
15
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
- *
19
- * You should have received a copy of the GNU Affero General Public
20
- * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
- *
22
- */
23
-
24
-declare(strict_types=1);
25
-
26
-namespace OCA\PaxFax\Controller;
27
-
28
-use OCP\IRequest;
29
-use OCP\AppFramework\Controller;
30
-use OCA\PaxFax\Service\PaxfaxService;
31
-use OCP\AppFramework\App;
32
-use OCP\Files\NotPermittedException;
33
-use OCP\Files\Folder;
34
-use OCP\IConfig;
35
-use OC\Files\Filesystem;
36
-use OC\Files\View;
37
-use \ReflectionClass;
38
-use \FilesystemIterator;
39
-
40
-use Phaxio;
41
-use Phaxio\OperationResult;
42
-use Phaxio\Error\AuthenticationException;
43
-use Phaxio\Error\NotFoundException;
44
-use Phaxio\Error\InvalidRequestException;
45
-use Phaxio\Error\RateLimitException;
46
-use Phaxio\Error\APIConnectionException;
47
-use Phaxio\Error\GeneralException;
48
-
49
-
50
-class PaxfaxController extends Controller {
51
-
52
-    private $service;
53
-    private $config;
54
-    private $userId;
55
-    private $folder;
56
-    private $filesystem;
57
-    private $view;
58
-
59
-    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view) {
60
-
61
-           parent::__construct($appName, $request);
62
-
63
-           $this->service = $service;
64
-           $this->config = $config;
65
-           $this->userId = $userId;
66
-           $this->folder = $folder;
67
-           $this->filesystem = $filesystem;
68
-           $this->view = $view;
69
-    }
70
-
71
-    /**
72
-     * @NoAdminRequired
73
-     */
74
-    public function cleantempdir($userId) {
75
-
76
-           // If the 'temp_files' folder doesn't exist create it
77
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
78
-               $this->folder->newFolder('Pax_Fax/temp_files');
79
-           }
80
-
81
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
82
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
83
-           $fileSystemIterator = new FilesystemIterator($targetdir);
84
-
85
-           $dirfiles = [];
86
-           foreach ($fileSystemIterator as $fileInfo){
87
-                    $dirfiles[] = $fileInfo->getFilename();
88
-           }
89
-
90
-           foreach ($dirfiles as $key => $indfile) {
91
-                    $thisuserroot = $this->view->getRoot();
92
-                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
93
-                    $removetmpfile = $this->filesystem->unlink($tempfile);
94
-           }
95
-    }
96
-
97
-
98
-    /**
99
-     * @NoAdminRequired
100
-     */
101
-    public function object_to_array($obj) {
102
-
103
-           if(is_object($obj)) $obj = (array)$this->dismount($obj);
104
-           if(is_array($obj)) {
105
-              $new = array();
106
-              foreach($obj as $key => $val) {
107
-                      $new[$key] = $this->object_to_array($val);
108
-              }
109
-           }
110
-           else $new = $obj;
111
-           return $new;
112
-    }
113
-
114
-
115
-    /**
116
-     * @NoAdminRequired
117
-     */
118
-    public function dismount($object) {
119
-           $reflectionClass = new ReflectionClass(get_class($object));
120
-           $array = array();
121
-           foreach ($reflectionClass->getProperties() as $property) {
122
-                    $property->setAccessible(true);
123
-                    $array[$property->getName()] = $property->getValue($object);
124
-                    $property->setAccessible(false);
125
-           }
126
-           return $array;
127
-    }
128
-
129
-
130
-    /**
131
-     * @NoAdminRequired
132
-     */
133
-    public function getbalance($userId) {
134
-
135
-           $apiMode = 'live';
136
-           $thisapicred = $this->service->getapicredentials($this->userId);
137
-
138
-           $apiKeys[$apiMode] = $thisapicred[0];
139
-           $apiSecrets[$apiMode] = $thisapicred[1];
140
-
141
-           $apiHost = 'https://api.phaxio.com/v2.1/';
142
-
143
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
144
-
145
-           try {
146
-                    // Get Phaxio account balance
147
-                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
148
-
149
-                    $balancetoarr = $this->object_to_array($phaxioresulttert);
150
-
151
-                    $phaxiobalance = $balancetoarr['data']['balance'];
152
-
153
-           } catch (InvalidRequestException $e) {
154
-                    $phaxiobalance = 'unknown';
155
-                    $phaxiosuccess = 'false';
156
-                    $errortype = 'invalid request error';
157
-           } catch (AuthenticationException $e) {
158
-                    $phaxiobalance = 'unknown';
159
-                    $phaxiosuccess = 'false';
160
-                    $errortype = 'authentication error';
161
-           } catch (APIConnectionException $e) {
162
-                    $phaxiobalance = 'unknown';
163
-                    $phaxiosuccess = 'false';
164
-                    $errortype = 'API connection error';
165
-           } catch (RateLimitException $e) {
166
-                    $phaxiobalance = 'unknown';
167
-                    $phaxiosuccess = 'false';
168
-                    $errortype = 'rate limit error';
169
-           } catch (NotFoundException $e) {
170
-                    $phaxiobalance = 'unknown';
171
-                    $phaxiosuccess = 'false';
172
-                    $errortype = 'not found error';
173
-           } catch (GeneralException $e) {
174
-                    $phaxiobalance = 'unknown';
175
-                    $phaxiosuccess = 'false';
176
-                    $errortype = 'undefined error';
177
-           }
178
-
179
-           return $phaxiobalance;
180
-
181
-    }
182
-
183
-
184
-    /**
185
-     * @NoAdminRequired
186
-     */
187
-    public function getfaxnumbers($userId) {
188
-
189
-           $apiMode = 'live';
190
-           $thisapicred = $this->service->getapicredentials($this->userId);
191
-
192
-           $apiKeys[$apiMode] = $thisapicred[0];
193
-           $apiSecrets[$apiMode] = $thisapicred[1];
194
-
195
-           $apiHost = 'https://api.phaxio.com/v2.1/';
196
-
197
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
198
-
199
-           try {
200
-                    // Get all Phaxio fax numbers
201
-                    $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
202
-
203
-                    $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
204
-
205
-                    $phaxioarr = [];
206
-
207
-                    foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
208
-                           if (is_array($phvalue)) {
209
-                                 foreach ($phvalue as $phkey2 => $phvalue2) {
210
-                                       if ($phkey2 == 'phone_number') {
211
-                                           $phaxioarr[] = $phvalue2;
212
-                                       }
213
-                                 }
214
-                           }
215
-                    }
216
-
217
-                    $phaxionmbrs = $phaxioarr;
218
-
219
-           } catch (InvalidRequestException $e) {
220
-                    $phaxiobalance = 'unknown';
221
-                    $phaxiosuccess = 'false';
222
-                    $errortype = 'invalid request error';
223
-           } catch (AuthenticationException $e) {
224
-                    $phaxiobalance = 'unknown';
225
-                    $phaxiosuccess = 'false';
226
-                    $errortype = 'authentication error';
227
-           } catch (APIConnectionException $e) {
228
-                    $phaxiobalance = 'unknown';
229
-                    $phaxiosuccess = 'false';
230
-                    $errortype = 'API connection error';
231
-           } catch (RateLimitException $e) {
232
-                    $phaxiobalance = 'unknown';
233
-                    $phaxiosuccess = 'false';
234
-                    $errortype = 'rate limit error';
235
-           } catch (NotFoundException $e) {
236
-                    $phaxiobalance = 'unknown';
237
-                    $phaxiosuccess = 'false';
238
-                    $errortype = 'not found error';
239
-           } catch (GeneralException $e) {
240
-                    $phaxiobalance = 'unknown';
241
-                    $phaxiosuccess = 'false';
242
-                    $errortype = 'undefined error';
243
-           }
244
-
245
-           return $phaxionmbrs;
246
-    }
247
-
248
-    /**
249
-     * @NoAdminRequired
250
-     */
251
-    protected function getFileID() {
252
-		if ($this->createdFile) {
253
-			return $this->createdFile;
254
-		}
255
-
256
-		$qb = $this->connection->getQueryBuilder();
257
-
258
-		// We create a new file entry and delete it after the test again
259
-		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
260
-		$qb->insert('filecache')
261
-			->values([
262
-				'path' => $qb->createNamedParameter($fileName),
263
-				'path_hash' => $qb->createNamedParameter(md5($fileName)),
264
-			])
265
-			->execute();
266
-		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
267
-		$qb->insert('filecache')
268
-			->values([
269
-				'path' => $qb->createNamedParameter($fileName),
270
-				'path_hash' => $qb->createNamedParameter(md5($fileName)),
271
-			])
272
-			->execute();
273
-
274
-		$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
275
-		return $this->createdFile;
276
-    }
277
-
278
-    /**
279
-     * @NoAdminRequired
280
-     */
281
-    public function uploadfile($uploadfileforfax) {
282
-
283
-           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
284
-           $fileName = $_FILES['uploadfileforfax']['name'];
285
-           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
286
-           $fileSize = $fileSizeinit / 1048576;
287
-
288
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
289
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
290
-           }
291
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
292
-               $this->folder->newFolder('Pax_Fax/faxes_received');
293
-           }
294
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
295
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
296
-           }
297
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
298
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
299
-           }
300
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
301
-               $this->folder->newFolder('Pax_Fax/temp_files');
302
-           }
303
-
304
-           $userroot = $this->view->getRoot();
305
-           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
306
-
307
-           $target = $this->folder->newFile($targetfile);
308
-           $target->putContent($fileContent);
309
-
310
-           // Get the cumulative files size of the uploaded files
311
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
312
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
313
-
314
-           $fileSystemIterator = new FilesystemIterator($targetdir);
315
-
316
-           $dirfiles = [];
317
-           foreach ($fileSystemIterator as $fileInfo){
318
-                    $dirfiles[] = $fileInfo->getFilename();
319
-           }
320
-
321
-           $totalflsizeinit = 0;
322
-           foreach ($dirfiles as $key => $indfile) {
323
-                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
324
-                    $mbSize = $fileSizeinit / 1048576;
325
-                    $totalflsizeinit += $mbSize;
326
-           }
327
-
328
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
329
-
330
-           return $totalflsize;
331
-    }
332
-
333
-    /**
334
-     * @NoAdminRequired
335
-     */
336
-    public function pickfile($path) {
337
-
338
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
339
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
340
-           }
341
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
342
-               $this->folder->newFolder('Pax_Fax/faxes_received');
343
-           }
344
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
345
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
346
-           }
347
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
348
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
349
-           }
350
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
351
-               $this->folder->newFolder('Pax_Fax/temp_files');
352
-           }
353
-
354
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
355
-
356
-           $fltgt = $datadir . $this->userId . "/files" . $path;
357
-
358
-           $fileContent = file_get_contents($fltgt);
359
-
360
-           $patharr = explode("/", $path);
361
-
362
-           $revarr = array_reverse($patharr);
363
-
364
-           $relflpath = "/Pax_Fax/temp_files/" . $revarr[0]; 
365
-
366
-           $target = $this->folder->newFile($relflpath);
367
-
368
-           $target->putContent($fileContent);
369
-
370
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
371
-
372
-           $fileSystemIterator = new FilesystemIterator($targetdir);
373
-
374
-           $dirfiles = [];
375
-           foreach ($fileSystemIterator as $fileInfo) {
376
-                    $dirfiles[] = $fileInfo->getFilename();
377
-           }
378
-
379
-           $totalflsizeinit = 0;
380
-
381
-           foreach ($dirfiles as $key => $indfile) {
382
-
383
-                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
384
-                    $mbSize = $fileSizeinit / 1048576;
385
-                    $totalflsizeinit += $mbSize;
386
-           }
387
-
388
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
389
-
390
-           return $totalflsize;
391
-    }
392
-
393
-    /**
394
-     * @NoAdminRequired
395
-     */
396
-    public function removeupfile($removedfilename) {
397
-
398
-           $tmpfl = "/" . $this->userId . "/files/Pax_Fax/temp_files/" . $removedfilename;
399
-
400
-           $removefile = $this->view->unlink($tmpfl);
401
-
402
-           // Get the cumulative files size of the uploaded files
403
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
404
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
405
-           $fileSystemIterator = new FilesystemIterator($targetdir);
406
-
407
-           $dirfiles = [];
408
-           foreach ($fileSystemIterator as $fileInfo){
409
-                    $dirfiles[] = $fileInfo->getFilename();
410
-           }
411
-
412
-           $totalflsizeinit = 0;
413
-
414
-           foreach ($dirfiles as $key => $indfile) {
415
-                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
416
-                    $mbSize = $fileSizeinit / 1048576;
417
-                    $totalflsizeinit += $mbSize;
418
-           }
419
-
420
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
421
-
422
-           return $totalflsize;
423
-    }
424
-
425
-    /**
426
-     * @NoAdminRequired
427
-     */
428
-    public function getpickedfile($pickedfilename) {
429
-
430
-           $thisuserroot = $this->view->getRoot();
431
-
432
-           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
433
-           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
434
-
435
-           $namesplit = explode(".", $pickedfilename);
436
-           $extension = end($namesplit);
437
-
438
-           if ($extension == "txt" || $extension == "html") {
439
-               $getpickedfile = $getfilecontent;
440
-           } elseif ($extension == "jpg") {
441
-               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
442
-           } elseif ($extension == "png") {
443
-               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
444
-           } else { $getpickedfile = ""; }
445
-
446
-           return $getpickedfile;
447
-    }
448
-
449
-    /**
450
-     * @NoAdminRequired
451
-     */
452
-    public function sendfax($userId, $uploadedtofax, $selectedcid, $toNumber) {
453
-
454
-           $tonumbertr = str_replace("+", "", $toNumber[0]);
455
-           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
456
-
457
-           $fromnumberdigits = str_replace("+", "", $selectedcid);
458
-           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
459
-
460
-           $countfaxfiles = count($uploadedtofax);
461
-
462
-           $openedfiles = [];
463
-
464
-           foreach ($uploadedtofax as $key => $flname) {
465
-
466
-                    if ($key == 0) { $firstflname = $flname; }
467
-
468
-                    $fileNamesec = array_reverse(explode(".", $flname));
469
-                    $filenameext = $fileNamesec[0];
470
-                    array_shift($fileNamesec);
471
-
472
-                    $fileName = implode("", $fileNamesec);
473
-
474
-                    if ($countfaxfiles == 1) {
475
-                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
476
-                    } else {
477
-                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
478
-                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
479
-                         }
480
-
481
-                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
482
-                    }
483
-
484
-                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
485
-                    $fltgt = $datadir . $this->userId . "/files/Pax_Fax/temp_files/" . $flname;
486
-                    $fileContent = file_get_contents($fltgt);
487
-
488
-                    $target = $this->folder->newFile($targetfile);
489
-                    $target->putContent($fileContent);
490
-
491
-                    $removetmpfile = $this->filesystem->unlink($fltgt);
492
-
493
-                    if ($countfaxfiles == 1) {
494
-                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
495
-                    } else {
496
-                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
497
-                    }
498
-           }
499
-
500
-           $params = array(
501
-                           'to' => $toNumber,
502
-                           'file' => $openedfiles,
503
-                           'caller_id' => $selectedcid
504
-                     );
505
-
506
-           $apiMode = 'live';
507
-
508
-           $thisapicred = $this->service->getapicredentials($this->userId);
509
-
510
-           $apiKeys[$apiMode] = $thisapicred[0];
511
-           $apiSecrets[$apiMode] = $thisapicred[1];
512
-
513
-           $apiHost = 'https://api.phaxio.com/v2.1/';
514
-
515
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
516
-
517
-           try {
518
-                    $phaxioresultinit = $phaxio->sendFax($params);
519
-
520
-                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
521
-
522
-                    $statustoarr = $this->object_to_array($phaxioresultsec);
523
-
524
-                    $phaxiosuccess = $statustoarr['success'];
525
-
526
-                    $errortype = 'there are no errors';
527
-
528
-           } catch (InvalidRequestException $e) {
529
-                    $phaxiosuccess = 'false';
530
-                    $errortype = 'invalid request error';
531
-           } catch (AuthenticationException $e) {
532
-                    $phaxiosuccess = 'false';
533
-                    $errortype = 'authentication error';
534
-           } catch (APIConnectionException $e) {
535
-                    $phaxiosuccess = 'false';
536
-                    $errortype = 'API connection error';
537
-           } catch (RateLimitException $e) {
538
-                    $phaxiosuccess = 'false';
539
-                    $errortype = 'rate limit error';
540
-           } catch (NotFoundException $e) {
541
-                    $phaxiosuccess = 'false';
542
-                    $errortype = 'not found error';
543
-           } catch (GeneralException $e) {
544
-                    $phaxiosuccess = 'false';
545
-                    $errortype = 'undefined error';
546
-           }
547
-
548
-           $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
549
-
550
-           if ($phaxiosuccess != 'true') {
551
-
552
-                    foreach ($uploadedtofax as $key => $flname) {
553
-
554
-                        $fileNamesec = array_reverse(explode(".", $flname));
555
-                        $filenameext = $fileNamesec[0];
556
-                        array_shift($fileNamesec);
557
-
558
-                        $fileName = implode("", $fileNamesec);
559
-
560
-                        if ($countfaxfiles == 1) {
561
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
562
-                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
563
-                        } else {
564
-
565
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
566
-                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
567
-                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
568
-                        }
569
-
570
-                        $fileContent = $this->filesystem->file_get_contents($failedfl);
571
-
572
-                        $targetact = $this->folder->newFile($newtargetfl);
573
-                        $targetact->putContent($fileContent);
574
-
575
-                        $removefailed = $this->filesystem->unlink($failedfl);
576
-                    }
577
-
578
-           }
579
-
580
-           return $phaxioresult;
581
-    }
582
-
583
-
584
-    /**
585
-     * @NoAdminRequired
586
-     */
587
-    public function getsettings($userId) {
588
-           return $this->service->getsettings($this->userId);
589
-    }
590
-
591
-
592
-    /**
593
-     * @NoAdminRequired
594
-     */
595
-    public function updatesettings($userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail) {
596
-           return $this->service->updatesettings($this->userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail);
597
-    }
598
-
599
-}
Browse code

added CHANGELOG.txt Contributors.txt README.md templates/settings.php appinfo/info.xml appinfo/signature.json lib/Service/PaxfaxService.php lib/Controller/AuthorApiController.php lib/Controller/PaxfaxController.php

DoubleBastionAdmin authored on 10/05/2022 18:04:58
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,599 @@
1
+<?php
2
+/**
3
+ * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
4
+ *
5
+ * @author Double Bastion LLC
6
+ *
7
+ * @license GNU AGPL version 3 or any later version
8
+ *
9
+ * This program is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
+ * License as published by the Free Software Foundation; either
12
+ * version 3 of the License, or any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
+ *
19
+ * You should have received a copy of the GNU Affero General Public
20
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+declare(strict_types=1);
25
+
26
+namespace OCA\PaxFax\Controller;
27
+
28
+use OCP\IRequest;
29
+use OCP\AppFramework\Controller;
30
+use OCA\PaxFax\Service\PaxfaxService;
31
+use OCP\AppFramework\App;
32
+use OCP\Files\NotPermittedException;
33
+use OCP\Files\Folder;
34
+use OCP\IConfig;
35
+use OC\Files\Filesystem;
36
+use OC\Files\View;
37
+use \ReflectionClass;
38
+use \FilesystemIterator;
39
+
40
+use Phaxio;
41
+use Phaxio\OperationResult;
42
+use Phaxio\Error\AuthenticationException;
43
+use Phaxio\Error\NotFoundException;
44
+use Phaxio\Error\InvalidRequestException;
45
+use Phaxio\Error\RateLimitException;
46
+use Phaxio\Error\APIConnectionException;
47
+use Phaxio\Error\GeneralException;
48
+
49
+
50
+class PaxfaxController extends Controller {
51
+
52
+    private $service;
53
+    private $config;
54
+    private $userId;
55
+    private $folder;
56
+    private $filesystem;
57
+    private $view;
58
+
59
+    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view) {
60
+
61
+           parent::__construct($appName, $request);
62
+
63
+           $this->service = $service;
64
+           $this->config = $config;
65
+           $this->userId = $userId;
66
+           $this->folder = $folder;
67
+           $this->filesystem = $filesystem;
68
+           $this->view = $view;
69
+    }
70
+
71
+    /**
72
+     * @NoAdminRequired
73
+     */
74
+    public function cleantempdir($userId) {
75
+
76
+           // If the 'temp_files' folder doesn't exist create it
77
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
78
+               $this->folder->newFolder('Pax_Fax/temp_files');
79
+           }
80
+
81
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
82
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
83
+           $fileSystemIterator = new FilesystemIterator($targetdir);
84
+
85
+           $dirfiles = [];
86
+           foreach ($fileSystemIterator as $fileInfo){
87
+                    $dirfiles[] = $fileInfo->getFilename();
88
+           }
89
+
90
+           foreach ($dirfiles as $key => $indfile) {
91
+                    $thisuserroot = $this->view->getRoot();
92
+                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
93
+                    $removetmpfile = $this->filesystem->unlink($tempfile);
94
+           }
95
+    }
96
+
97
+
98
+    /**
99
+     * @NoAdminRequired
100
+     */
101
+    public function object_to_array($obj) {
102
+
103
+           if(is_object($obj)) $obj = (array)$this->dismount($obj);
104
+           if(is_array($obj)) {
105
+              $new = array();
106
+              foreach($obj as $key => $val) {
107
+                      $new[$key] = $this->object_to_array($val);
108
+              }
109
+           }
110
+           else $new = $obj;
111
+           return $new;
112
+    }
113
+
114
+
115
+    /**
116
+     * @NoAdminRequired
117
+     */
118
+    public function dismount($object) {
119
+           $reflectionClass = new ReflectionClass(get_class($object));
120
+           $array = array();
121
+           foreach ($reflectionClass->getProperties() as $property) {
122
+                    $property->setAccessible(true);
123
+                    $array[$property->getName()] = $property->getValue($object);
124
+                    $property->setAccessible(false);
125
+           }
126
+           return $array;
127
+    }
128
+
129
+
130
+    /**
131
+     * @NoAdminRequired
132
+     */
133
+    public function getbalance($userId) {
134
+
135
+           $apiMode = 'live';
136
+           $thisapicred = $this->service->getapicredentials($this->userId);
137
+
138
+           $apiKeys[$apiMode] = $thisapicred[0];
139
+           $apiSecrets[$apiMode] = $thisapicred[1];
140
+
141
+           $apiHost = 'https://api.phaxio.com/v2.1/';
142
+
143
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
144
+
145
+           try {
146
+                    // Get Phaxio account balance
147
+                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
148
+
149
+                    $balancetoarr = $this->object_to_array($phaxioresulttert);
150
+
151
+                    $phaxiobalance = $balancetoarr['data']['balance'];
152
+
153
+           } catch (InvalidRequestException $e) {
154
+                    $phaxiobalance = 'unknown';
155
+                    $phaxiosuccess = 'false';
156
+                    $errortype = 'invalid request error';
157
+           } catch (AuthenticationException $e) {
158
+                    $phaxiobalance = 'unknown';
159
+                    $phaxiosuccess = 'false';
160
+                    $errortype = 'authentication error';
161
+           } catch (APIConnectionException $e) {
162
+                    $phaxiobalance = 'unknown';
163
+                    $phaxiosuccess = 'false';
164
+                    $errortype = 'API connection error';
165
+           } catch (RateLimitException $e) {
166
+                    $phaxiobalance = 'unknown';
167
+                    $phaxiosuccess = 'false';
168
+                    $errortype = 'rate limit error';
169
+           } catch (NotFoundException $e) {
170
+                    $phaxiobalance = 'unknown';
171
+                    $phaxiosuccess = 'false';
172
+                    $errortype = 'not found error';
173
+           } catch (GeneralException $e) {
174
+                    $phaxiobalance = 'unknown';
175
+                    $phaxiosuccess = 'false';
176
+                    $errortype = 'undefined error';
177
+           }
178
+
179
+           return $phaxiobalance;
180
+
181
+    }
182
+
183
+
184
+    /**
185
+     * @NoAdminRequired
186
+     */
187
+    public function getfaxnumbers($userId) {
188
+
189
+           $apiMode = 'live';
190
+           $thisapicred = $this->service->getapicredentials($this->userId);
191
+
192
+           $apiKeys[$apiMode] = $thisapicred[0];
193
+           $apiSecrets[$apiMode] = $thisapicred[1];
194
+
195
+           $apiHost = 'https://api.phaxio.com/v2.1/';
196
+
197
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
198
+
199
+           try {
200
+                    // Get all Phaxio fax numbers
201
+                    $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
202
+
203
+                    $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
204
+
205
+                    $phaxioarr = [];
206
+
207
+                    foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
208
+                           if (is_array($phvalue)) {
209
+                                 foreach ($phvalue as $phkey2 => $phvalue2) {
210
+                                       if ($phkey2 == 'phone_number') {
211
+                                           $phaxioarr[] = $phvalue2;
212
+                                       }
213
+                                 }
214
+                           }
215
+                    }
216
+
217
+                    $phaxionmbrs = $phaxioarr;
218
+
219
+           } catch (InvalidRequestException $e) {
220
+                    $phaxiobalance = 'unknown';
221
+                    $phaxiosuccess = 'false';
222
+                    $errortype = 'invalid request error';
223
+           } catch (AuthenticationException $e) {
224
+                    $phaxiobalance = 'unknown';
225
+                    $phaxiosuccess = 'false';
226
+                    $errortype = 'authentication error';
227
+           } catch (APIConnectionException $e) {
228
+                    $phaxiobalance = 'unknown';
229
+                    $phaxiosuccess = 'false';
230
+                    $errortype = 'API connection error';
231
+           } catch (RateLimitException $e) {
232
+                    $phaxiobalance = 'unknown';
233
+                    $phaxiosuccess = 'false';
234
+                    $errortype = 'rate limit error';
235
+           } catch (NotFoundException $e) {
236
+                    $phaxiobalance = 'unknown';
237
+                    $phaxiosuccess = 'false';
238
+                    $errortype = 'not found error';
239
+           } catch (GeneralException $e) {
240
+                    $phaxiobalance = 'unknown';
241
+                    $phaxiosuccess = 'false';
242
+                    $errortype = 'undefined error';
243
+           }
244
+
245
+           return $phaxionmbrs;
246
+    }
247
+
248
+    /**
249
+     * @NoAdminRequired
250
+     */
251
+    protected function getFileID() {
252
+		if ($this->createdFile) {
253
+			return $this->createdFile;
254
+		}
255
+
256
+		$qb = $this->connection->getQueryBuilder();
257
+
258
+		// We create a new file entry and delete it after the test again
259
+		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
260
+		$qb->insert('filecache')
261
+			->values([
262
+				'path' => $qb->createNamedParameter($fileName),
263
+				'path_hash' => $qb->createNamedParameter(md5($fileName)),
264
+			])
265
+			->execute();
266
+		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
267
+		$qb->insert('filecache')
268
+			->values([
269
+				'path' => $qb->createNamedParameter($fileName),
270
+				'path_hash' => $qb->createNamedParameter(md5($fileName)),
271
+			])
272
+			->execute();
273
+
274
+		$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
275
+		return $this->createdFile;
276
+    }
277
+
278
+    /**
279
+     * @NoAdminRequired
280
+     */
281
+    public function uploadfile($uploadfileforfax) {
282
+
283
+           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
284
+           $fileName = $_FILES['uploadfileforfax']['name'];
285
+           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
286
+           $fileSize = $fileSizeinit / 1048576;
287
+
288
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
289
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
290
+           }
291
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
292
+               $this->folder->newFolder('Pax_Fax/faxes_received');
293
+           }
294
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
295
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
296
+           }
297
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
298
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
299
+           }
300
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
301
+               $this->folder->newFolder('Pax_Fax/temp_files');
302
+           }
303
+
304
+           $userroot = $this->view->getRoot();
305
+           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
306
+
307
+           $target = $this->folder->newFile($targetfile);
308
+           $target->putContent($fileContent);
309
+
310
+           // Get the cumulative files size of the uploaded files
311
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
312
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
313
+
314
+           $fileSystemIterator = new FilesystemIterator($targetdir);
315
+
316
+           $dirfiles = [];
317
+           foreach ($fileSystemIterator as $fileInfo){
318
+                    $dirfiles[] = $fileInfo->getFilename();
319
+           }
320
+
321
+           $totalflsizeinit = 0;
322
+           foreach ($dirfiles as $key => $indfile) {
323
+                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
324
+                    $mbSize = $fileSizeinit / 1048576;
325
+                    $totalflsizeinit += $mbSize;
326
+           }
327
+
328
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
329
+
330
+           return $totalflsize;
331
+    }
332
+
333
+    /**
334
+     * @NoAdminRequired
335
+     */
336
+    public function pickfile($path) {
337
+
338
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
339
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
340
+           }
341
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
342
+               $this->folder->newFolder('Pax_Fax/faxes_received');
343
+           }
344
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
345
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
346
+           }
347
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
348
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
349
+           }
350
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
351
+               $this->folder->newFolder('Pax_Fax/temp_files');
352
+           }
353
+
354
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
355
+
356
+           $fltgt = $datadir . $this->userId . "/files" . $path;
357
+
358
+           $fileContent = file_get_contents($fltgt);
359
+
360
+           $patharr = explode("/", $path);
361
+
362
+           $revarr = array_reverse($patharr);
363
+
364
+           $relflpath = "/Pax_Fax/temp_files/" . $revarr[0]; 
365
+
366
+           $target = $this->folder->newFile($relflpath);
367
+
368
+           $target->putContent($fileContent);
369
+
370
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
371
+
372
+           $fileSystemIterator = new FilesystemIterator($targetdir);
373
+
374
+           $dirfiles = [];
375
+           foreach ($fileSystemIterator as $fileInfo) {
376
+                    $dirfiles[] = $fileInfo->getFilename();
377
+           }
378
+
379
+           $totalflsizeinit = 0;
380
+
381
+           foreach ($dirfiles as $key => $indfile) {
382
+
383
+                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
384
+                    $mbSize = $fileSizeinit / 1048576;
385
+                    $totalflsizeinit += $mbSize;
386
+           }
387
+
388
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
389
+
390
+           return $totalflsize;
391
+    }
392
+
393
+    /**
394
+     * @NoAdminRequired
395
+     */
396
+    public function removeupfile($removedfilename) {
397
+
398
+           $tmpfl = "/" . $this->userId . "/files/Pax_Fax/temp_files/" . $removedfilename;
399
+
400
+           $removefile = $this->view->unlink($tmpfl);
401
+
402
+           // Get the cumulative files size of the uploaded files
403
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
404
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
405
+           $fileSystemIterator = new FilesystemIterator($targetdir);
406
+
407
+           $dirfiles = [];
408
+           foreach ($fileSystemIterator as $fileInfo){
409
+                    $dirfiles[] = $fileInfo->getFilename();
410
+           }
411
+
412
+           $totalflsizeinit = 0;
413
+
414
+           foreach ($dirfiles as $key => $indfile) {
415
+                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
416
+                    $mbSize = $fileSizeinit / 1048576;
417
+                    $totalflsizeinit += $mbSize;
418
+           }
419
+
420
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
421
+
422
+           return $totalflsize;
423
+    }
424
+
425
+    /**
426
+     * @NoAdminRequired
427
+     */
428
+    public function getpickedfile($pickedfilename) {
429
+
430
+           $thisuserroot = $this->view->getRoot();
431
+
432
+           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
433
+           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
434
+
435
+           $namesplit = explode(".", $pickedfilename);
436
+           $extension = end($namesplit);
437
+
438
+           if ($extension == "txt" || $extension == "html") {
439
+               $getpickedfile = $getfilecontent;
440
+           } elseif ($extension == "jpg") {
441
+               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
442
+           } elseif ($extension == "png") {
443
+               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
444
+           } else { $getpickedfile = ""; }
445
+
446
+           return $getpickedfile;
447
+    }
448
+
449
+    /**
450
+     * @NoAdminRequired
451
+     */
452
+    public function sendfax($userId, $uploadedtofax, $selectedcid, $toNumber) {
453
+
454
+           $tonumbertr = str_replace("+", "", $toNumber[0]);
455
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
456
+
457
+           $fromnumberdigits = str_replace("+", "", $selectedcid);
458
+           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
459
+
460
+           $countfaxfiles = count($uploadedtofax);
461
+
462
+           $openedfiles = [];
463
+
464
+           foreach ($uploadedtofax as $key => $flname) {
465
+
466
+                    if ($key == 0) { $firstflname = $flname; }
467
+
468
+                    $fileNamesec = array_reverse(explode(".", $flname));
469
+                    $filenameext = $fileNamesec[0];
470
+                    array_shift($fileNamesec);
471
+
472
+                    $fileName = implode("", $fileNamesec);
473
+
474
+                    if ($countfaxfiles == 1) {
475
+                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
476
+                    } else {
477
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
478
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
479
+                         }
480
+
481
+                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
482
+                    }
483
+
484
+                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
485
+                    $fltgt = $datadir . $this->userId . "/files/Pax_Fax/temp_files/" . $flname;
486
+                    $fileContent = file_get_contents($fltgt);
487
+
488
+                    $target = $this->folder->newFile($targetfile);
489
+                    $target->putContent($fileContent);
490
+
491
+                    $removetmpfile = $this->filesystem->unlink($fltgt);
492
+
493
+                    if ($countfaxfiles == 1) {
494
+                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
495
+                    } else {
496
+                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
497
+                    }
498
+           }
499
+
500
+           $params = array(
501
+                           'to' => $toNumber,
502
+                           'file' => $openedfiles,
503
+                           'caller_id' => $selectedcid
504
+                     );
505
+
506
+           $apiMode = 'live';
507
+
508
+           $thisapicred = $this->service->getapicredentials($this->userId);
509
+
510
+           $apiKeys[$apiMode] = $thisapicred[0];
511
+           $apiSecrets[$apiMode] = $thisapicred[1];
512
+
513
+           $apiHost = 'https://api.phaxio.com/v2.1/';
514
+
515
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
516
+
517
+           try {
518
+                    $phaxioresultinit = $phaxio->sendFax($params);
519
+
520
+                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
521
+
522
+                    $statustoarr = $this->object_to_array($phaxioresultsec);
523
+
524
+                    $phaxiosuccess = $statustoarr['success'];
525
+
526
+                    $errortype = 'there are no errors';
527
+
528
+           } catch (InvalidRequestException $e) {
529
+                    $phaxiosuccess = 'false';
530
+                    $errortype = 'invalid request error';
531
+           } catch (AuthenticationException $e) {
532
+                    $phaxiosuccess = 'false';
533
+                    $errortype = 'authentication error';
534
+           } catch (APIConnectionException $e) {
535
+                    $phaxiosuccess = 'false';
536
+                    $errortype = 'API connection error';
537
+           } catch (RateLimitException $e) {
538
+                    $phaxiosuccess = 'false';
539
+                    $errortype = 'rate limit error';
540
+           } catch (NotFoundException $e) {
541
+                    $phaxiosuccess = 'false';
542
+                    $errortype = 'not found error';
543
+           } catch (GeneralException $e) {
544
+                    $phaxiosuccess = 'false';
545
+                    $errortype = 'undefined error';
546
+           }
547
+
548
+           $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
549
+
550
+           if ($phaxiosuccess != 'true') {
551
+
552
+                    foreach ($uploadedtofax as $key => $flname) {
553
+
554
+                        $fileNamesec = array_reverse(explode(".", $flname));
555
+                        $filenameext = $fileNamesec[0];
556
+                        array_shift($fileNamesec);
557
+
558
+                        $fileName = implode("", $fileNamesec);
559
+
560
+                        if ($countfaxfiles == 1) {
561
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
562
+                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
563
+                        } else {
564
+
565
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
566
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
567
+                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
568
+                        }
569
+
570
+                        $fileContent = $this->filesystem->file_get_contents($failedfl);
571
+
572
+                        $targetact = $this->folder->newFile($newtargetfl);
573
+                        $targetact->putContent($fileContent);
574
+
575
+                        $removefailed = $this->filesystem->unlink($failedfl);
576
+                    }
577
+
578
+           }
579
+
580
+           return $phaxioresult;
581
+    }
582
+
583
+
584
+    /**
585
+     * @NoAdminRequired
586
+     */
587
+    public function getsettings($userId) {
588
+           return $this->service->getsettings($this->userId);
589
+    }
590
+
591
+
592
+    /**
593
+     * @NoAdminRequired
594
+     */
595
+    public function updatesettings($userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail) {
596
+           return $this->service->updatesettings($this->userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail);
597
+    }
598
+
599
+}
Browse code

removed CHANGELOG.txt Contributors.txt README.md templates/settings.php appinfo/info.xml appinfo/signature.json lib/Service/PaxfaxService.php lib/Controller/AuthorApiController.php lib/Controller/PaxfaxController.php

DoubleBastionAdmin authored on 10/05/2022 18:00:53
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,600 +0,0 @@
1
-<?php
2
-/**
3
- * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
4
- *
5
- * @author Double Bastion LLC
6
- *
7
- * @license GNU AGPL version 3 or any later version
8
- *
9
- * This program is free software; you can redistribute it and/or
10
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
- * License as published by the Free Software Foundation; either
12
- * version 3 of the License, or any later version.
13
- *
14
- * This program is distributed in the hope that it will be useful,
15
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
- *
19
- * You should have received a copy of the GNU Affero General Public
20
- * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
- *
22
- */
23
-
24
-declare(strict_types=1);
25
-
26
-namespace OCA\PaxFax\Controller;
27
-
28
-use OCP\IRequest;
29
-use OCP\AppFramework\Controller;
30
-use OCA\PaxFax\Service\PaxfaxService;
31
-use OCP\AppFramework\App;
32
-use OCP\Files\NotPermittedException;
33
-use OCP\Files\Folder;
34
-use OCP\IConfig;
35
-use OC\Files\Filesystem;
36
-use OC\Files\View;
37
-use \ReflectionClass;
38
-use \FilesystemIterator;
39
-
40
-use Phaxio;
41
-use Phaxio\OperationResult;
42
-use Phaxio\Error\AuthenticationException;
43
-use Phaxio\Error\NotFoundException;
44
-use Phaxio\Error\InvalidRequestException;
45
-use Phaxio\Error\RateLimitException;
46
-use Phaxio\Error\APIConnectionException;
47
-use Phaxio\Error\GeneralException;
48
-
49
-
50
-class PaxfaxController extends Controller {
51
-
52
-    private $service;
53
-    private $config;
54
-    private $userId;
55
-    private $folder;
56
-    private $filesystem;
57
-    private $view;
58
-
59
-    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view) {
60
-
61
-           parent::__construct($appName, $request);
62
-
63
-           $this->service = $service;
64
-           $this->config = $config;
65
-           $this->userId = $userId;
66
-           $this->folder = $folder;
67
-           $this->filesystem = $filesystem;
68
-           $this->view = $view;
69
-    }
70
-
71
-    /**
72
-     * @NoAdminRequired
73
-     */
74
-    public function cleantempdir($userId) {
75
-
76
-           // If the 'temp_files' folder doesn't exist create it
77
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
78
-               $this->folder->newFolder('Pax_Fax/temp_files');
79
-           }
80
-
81
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
82
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
83
-           $fileSystemIterator = new FilesystemIterator($targetdir);
84
-
85
-           $dirfiles = [];
86
-           foreach ($fileSystemIterator as $fileInfo){
87
-                    $dirfiles[] = $fileInfo->getFilename();
88
-           }
89
-
90
-           foreach ($dirfiles as $key => $indfile) {
91
-                    $thisuserroot = $this->view->getRoot();
92
-                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
93
-                    $removetmpfile = $this->filesystem->unlink($tempfile);
94
-           }
95
-    }
96
-
97
-
98
-    /**
99
-     * @NoAdminRequired
100
-     */
101
-    public function object_to_array($obj) {
102
-
103
-           if(is_object($obj)) $obj = (array)$this->dismount($obj);
104
-           if(is_array($obj)) {
105
-              $new = array();
106
-              foreach($obj as $key => $val) {
107
-                      $new[$key] = $this->object_to_array($val);
108
-              }
109
-           }
110
-           else $new = $obj;
111
-           return $new;
112
-    }
113
-
114
-
115
-    /**
116
-     * @NoAdminRequired
117
-     */
118
-    public function dismount($object) {
119
-           $reflectionClass = new ReflectionClass(get_class($object));
120
-           $array = array();
121
-           foreach ($reflectionClass->getProperties() as $property) {
122
-                    $property->setAccessible(true);
123
-                    $array[$property->getName()] = $property->getValue($object);
124
-                    $property->setAccessible(false);
125
-           }
126
-           return $array;
127
-    }
128
-
129
-
130
-    /**
131
-     * @NoAdminRequired
132
-     */
133
-    public function getbalance($userId) {
134
-
135
-           $apiMode = 'live';
136
-           $thisapicred = $this->service->getapicredentials($this->userId);
137
-
138
-           $apiKeys[$apiMode] = $thisapicred[0];
139
-           $apiSecrets[$apiMode] = $thisapicred[1];
140
-
141
-           $apiHost = 'https://api.phaxio.com/v2.1/';
142
-
143
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
144
-
145
-           try {
146
-                    // Get Phaxio account balance
147
-                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
148
-
149
-                    $balancetoarr = $this->object_to_array($phaxioresulttert);
150
-
151
-                    $phaxiobalance = $balancetoarr['data']['balance'];
152
-
153
-           } catch (InvalidRequestException $e) {
154
-                    $phaxiobalance = 'unknown';
155
-                    $phaxiosuccess = 'false';
156
-                    $errortype = 'invalid request error';
157
-           } catch (AuthenticationException $e) {
158
-                    $phaxiobalance = 'unknown';
159
-                    $phaxiosuccess = 'false';
160
-                    $errortype = 'authentication error';
161
-           } catch (APIConnectionException $e) {
162
-                    $phaxiobalance = 'unknown';
163
-                    $phaxiosuccess = 'false';
164
-                    $errortype = 'API connection error';
165
-           } catch (RateLimitException $e) {
166
-                    $phaxiobalance = 'unknown';
167
-                    $phaxiosuccess = 'false';
168
-                    $errortype = 'rate limit error';
169
-           } catch (NotFoundException $e) {
170
-                    $phaxiobalance = 'unknown';
171
-                    $phaxiosuccess = 'false';
172
-                    $errortype = 'not found error';
173
-           } catch (GeneralException $e) {
174
-                    $phaxiobalance = 'unknown';
175
-                    $phaxiosuccess = 'false';
176
-                    $errortype = 'undefined error';
177
-           }
178
-
179
-           return $phaxiobalance;
180
-
181
-    }
182
-
183
-
184
-    /**
185
-     * @NoAdminRequired
186
-     */
187
-    public function getfaxnumbers($userId) {
188
-
189
-           $apiMode = 'live';
190
-           $thisapicred = $this->service->getapicredentials($this->userId);
191
-
192
-           $apiKeys[$apiMode] = $thisapicred[0];
193
-           $apiSecrets[$apiMode] = $thisapicred[1];
194
-
195
-           $apiHost = 'https://api.phaxio.com/v2.1/';
196
-
197
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
198
-
199
-           try {
200
-                    // Get all Phaxio fax numbers
201
-                    $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
202
-
203
-                    $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
204
-
205
-                    $phaxioarr = [];
206
-
207
-                    foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
208
-                           if (is_array($phvalue)) {
209
-                                 foreach ($phvalue as $phkey2 => $phvalue2) {
210
-                                       if ($phkey2 == 'phone_number') {
211
-                                           $phaxioarr[] = $phvalue2;
212
-                                       }
213
-                                 }
214
-                           }
215
-                    }
216
-
217
-                    $phaxionmbrs = $phaxioarr;
218
-
219
-           } catch (InvalidRequestException $e) {
220
-                    $phaxiobalance = 'unknown';
221
-                    $phaxiosuccess = 'false';
222
-                    $errortype = 'invalid request error';
223
-           } catch (AuthenticationException $e) {
224
-                    $phaxiobalance = 'unknown';
225
-                    $phaxiosuccess = 'false';
226
-                    $errortype = 'authentication error';
227
-           } catch (APIConnectionException $e) {
228
-                    $phaxiobalance = 'unknown';
229
-                    $phaxiosuccess = 'false';
230
-                    $errortype = 'API connection error';
231
-           } catch (RateLimitException $e) {
232
-                    $phaxiobalance = 'unknown';
233
-                    $phaxiosuccess = 'false';
234
-                    $errortype = 'rate limit error';
235
-           } catch (NotFoundException $e) {
236
-                    $phaxiobalance = 'unknown';
237
-                    $phaxiosuccess = 'false';
238
-                    $errortype = 'not found error';
239
-           } catch (GeneralException $e) {
240
-                    $phaxiobalance = 'unknown';
241
-                    $phaxiosuccess = 'false';
242
-                    $errortype = 'undefined error';
243
-           }
244
-
245
-           return $phaxionmbrs;
246
-    }
247
-
248
-    /**
249
-     * @NoAdminRequired
250
-     */
251
-    protected function getFileID() {
252
-		if ($this->createdFile) {
253
-			return $this->createdFile;
254
-		}
255
-
256
-		$qb = $this->connection->getQueryBuilder();
257
-
258
-		// We create a new file entry and delete it after the test again
259
-		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
260
-		$qb->insert('filecache')
261
-			->values([
262
-				'path' => $qb->createNamedParameter($fileName),
263
-				'path_hash' => $qb->createNamedParameter(md5($fileName)),
264
-			])
265
-			->execute();
266
-		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
267
-		$qb->insert('filecache')
268
-			->values([
269
-				'path' => $qb->createNamedParameter($fileName),
270
-				'path_hash' => $qb->createNamedParameter(md5($fileName)),
271
-			])
272
-			->execute();
273
-
274
-		$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
275
-		return $this->createdFile;
276
-    }
277
-
278
-    /**
279
-     * @NoAdminRequired
280
-     */
281
-    public function uploadfile($userId, $uploadfileforfax) {
282
-
283
-           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
284
-           $fileName = $_FILES['uploadfileforfax']['name'];
285
-           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
286
-           $fileSize = $fileSizeinit / 1048576;
287
-
288
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
289
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
290
-           }
291
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
292
-               $this->folder->newFolder('Pax_Fax/faxes_received');
293
-           }
294
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
295
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
296
-           }
297
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
298
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
299
-           }
300
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
301
-               $this->folder->newFolder('Pax_Fax/temp_files');
302
-           }
303
-
304
-           $userroot = $this->view->getRoot();
305
-           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
306
-
307
-           $target = $this->folder->newFile($targetfile);
308
-           $target->putContent($fileContent);
309
-
310
-           // Get the cumulative files size of the uploaded files
311
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
312
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
313
-
314
-           $fileSystemIterator = new FilesystemIterator($targetdir);
315
-
316
-           $dirfiles = [];
317
-           foreach ($fileSystemIterator as $fileInfo){
318
-                    $dirfiles[] = $fileInfo->getFilename();
319
-           }
320
-
321
-           $totalflsizeinit = 0;
322
-           foreach ($dirfiles as $key => $indfile) {
323
-                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
324
-                    $mbSize = $fileSizeinit / 1048576;
325
-                    $totalflsizeinit += $mbSize;
326
-           }
327
-
328
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
329
-
330
-           return $totalflsize;
331
-    }
332
-
333
-    /**
334
-     * @NoAdminRequired
335
-     */
336
-    public function pickfile($userId, $path) {
337
-
338
-           $userroot = $this->view->getRoot();
339
-           $fltgt = $userroot . $path;
340
-
341
-           $fileContent = $this->filesystem->file_get_contents($fltgt);
342
-
343
-           $fileNameinit = explode("/", $path);
344
-           $fileNamesec = array_reverse($fileNameinit);
345
-           $fileName = $fileNamesec[0];
346
-
347
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
348
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
349
-           }
350
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
351
-               $this->folder->newFolder('Pax_Fax/faxes_received');
352
-           }
353
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
354
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
355
-           }
356
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
357
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
358
-           }
359
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
360
-               $this->folder->newFolder('Pax_Fax/temp_files');
361
-           }
362
-
363
-           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
364
-
365
-           $target = $this->folder->newFile($targetfile);
366
-           $target->putContent($fileContent);
367
-
368
-           // Get the cumulative files size of the uploaded files
369
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
370
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
371
-           $fileSystemIterator = new FilesystemIterator($targetdir);
372
-
373
-           $dirfiles = [];
374
-           foreach ($fileSystemIterator as $fileInfo){
375
-                    $dirfiles[] = $fileInfo->getFilename();
376
-           }
377
-
378
-           $totalflsizeinit = 0;
379
-
380
-           foreach ($dirfiles as $key => $indfile) {
381
-
382
-                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
383
-                    $mbSize = $fileSizeinit / 1048576;
384
-                    $totalflsizeinit += $mbSize;
385
-           }
386
-
387
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
388
-
389
-           return $totalflsize;
390
-    }
391
-
392
-    /**
393
-     * @NoAdminRequired
394
-     */
395
-    public function removeupfile($userId, $removedfilename) {
396
-
397
-           $thisuserroot = $this->view->getRoot();
398
-
399
-           $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $removedfilename;
400
-           $removetmpfile = $this->filesystem->unlink($tempfile);
401
-
402
-           // Get the cumulative files size of the uploaded files
403
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
404
-           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
405
-           $fileSystemIterator = new FilesystemIterator($targetdir);
406
-
407
-           $dirfiles = [];
408
-           foreach ($fileSystemIterator as $fileInfo){
409
-                    $dirfiles[] = $fileInfo->getFilename();
410
-           }
411
-
412
-           $totalflsizeinit = 0;
413
-
414
-           foreach ($dirfiles as $key => $indfile) {
415
-
416
-                    $fileSizeinit = $this->filesystem->filesize($thisuserroot . "/Pax_Fax/temp_files/" . $indfile);
417
-                    $mbSize = $fileSizeinit / 1048576;
418
-                    $totalflsizeinit += $mbSize;
419
-           }
420
-
421
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
422
-
423
-           return $totalflsize;
424
-    }
425
-
426
-    /**
427
-     * @NoAdminRequired
428
-     */
429
-    public function getpickedfile($userId, $pickedfilename) {
430
-
431
-           $thisuserroot = $this->view->getRoot();
432
-
433
-           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
434
-           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
435
-
436
-           $namesplit = explode(".", $pickedfilename);
437
-           $extension = end($namesplit);
438
-
439
-           if ($extension == "txt" || $extension == "html") {
440
-               $getpickedfile = $getfilecontent;
441
-           } elseif ($extension == "jpg") {
442
-               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
443
-           } elseif ($extension == "png") {
444
-               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
445
-           } else { $getpickedfile = ""; }
446
-
447
-           return $getpickedfile;
448
-    }
449
-
450
-    /**
451
-     * @NoAdminRequired
452
-     */
453
-    public function sendfax($userId, $uploadedtofax, $selectedcid, $toNumber) {
454
-
455
-           $tonumbertr = str_replace("+", "", $toNumber[0]);
456
-           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
457
-
458
-           $fromnumberdigits = str_replace("+", "", $selectedcid);
459
-           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
460
-
461
-           $countfaxfiles = count($uploadedtofax);
462
-
463
-           $openedfiles = [];
464
-
465
-           foreach ($uploadedtofax as $key => $flname) {
466
-
467
-                    if ($key == 0) { $firstflname = $flname; }
468
-
469
-                    $fileNamesec = array_reverse(explode(".", $flname));
470
-                    $filenameext = $fileNamesec[0];
471
-                    array_shift($fileNamesec);
472
-
473
-                    $fileName = implode("", $fileNamesec);
474
-
475
-                    $userroot = $this->view->getRoot();
476
-
477
-                    if ($countfaxfiles == 1) {
478
-                        $targetfile = $userroot . "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
479
-                    } else {
480
-                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
481
-                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
482
-                         }
483
-                         $targetfile = $userroot . "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
484
-                    }
485
-
486
-                    $fltgt = $userroot . "/Pax_Fax/temp_files/" . $flname;
487
-                    $fileContent = $this->filesystem->file_get_contents($fltgt);
488
-
489
-                    $target = $this->folder->newFile($targetfile);
490
-                    $target->putContent($fileContent);
491
-
492
-                    $removetmpfile = $this->filesystem->unlink($fltgt);
493
-                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
494
-
495
-                    if ($countfaxfiles == 1) {
496
-                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
497
-                    } else {
498
-                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
499
-                    }
500
-           }
501
-
502
-           $params = array(
503
-                           'to' => $toNumber,
504
-                           'file' => $openedfiles,
505
-                           'caller_id' => $selectedcid
506
-                     );
507
-
508
-           $apiMode = 'live';
509
-
510
-           $thisapicred = $this->service->getapicredentials($this->userId);
511
-
512
-           $apiKeys[$apiMode] = $thisapicred[0];
513
-           $apiSecrets[$apiMode] = $thisapicred[1];
514
-
515
-           $apiHost = 'https://api.phaxio.com/v2.1/';
516
-
517
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
518
-
519
-           try {
520
-                    $phaxioresultinit = $phaxio->sendFax($params);
521
-
522
-                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
523
-
524
-                    $statustoarr = $this->object_to_array($phaxioresultsec);
525
-
526
-                    $phaxiosuccess = $statustoarr['success'];
527
-
528
-                    $errortype = 'there are no errors';
529
-
530
-           } catch (InvalidRequestException $e) {
531
-                    $phaxiosuccess = 'false';
532
-                    $errortype = 'invalid request error';
533
-           } catch (AuthenticationException $e) {
534
-                    $phaxiosuccess = 'false';
535
-                    $errortype = 'authentication error';
536
-           } catch (APIConnectionException $e) {
537
-                    $phaxiosuccess = 'false';
538
-                    $errortype = 'API connection error';
539
-           } catch (RateLimitException $e) {
540
-                    $phaxiosuccess = 'false';
541
-                    $errortype = 'rate limit error';
542
-           } catch (NotFoundException $e) {
543
-                    $phaxiosuccess = 'false';
544
-                    $errortype = 'not found error';
545
-           } catch (GeneralException $e) {
546
-                    $phaxiosuccess = 'false';
547
-                    $errortype = 'undefined error';
548
-           }
549
-
550
-           $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
551
-
552
-           if ($phaxiosuccess != 'true') {
553
-
554
-                    foreach ($uploadedtofax as $key => $flname) {
555
-
556
-                        $fileNamesec = array_reverse(explode(".", $flname));
557
-                        $filenameext = $fileNamesec[0];
558
-                        array_shift($fileNamesec);
559
-
560
-                        $fileName = implode("", $fileNamesec);
561
-
562
-                        if ($countfaxfiles == 1) {
563
-                            $failedfl = $userroot . "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
564
-                            $newtargetfl =  $userroot . "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
565
-                        } else {
566
-                            $failedfl = $userroot . "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
567
-                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
568
-                            $newtargetfl = $userroot . "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
569
-                        }
570
-
571
-                        $fileContent = $this->filesystem->file_get_contents($failedfl);
572
-
573
-                        $targetact = $this->folder->newFile($newtargetfl);
574
-                        $targetact->putContent($fileContent);
575
-
576
-                        $removefailed = $this->filesystem->unlink($failedfl);
577
-                    }
578
-
579
-           }
580
-
581
-           return $phaxioresult;
582
-    }
583
-
584
-
585
-    /**
586
-     * @NoAdminRequired
587
-     */
588
-    public function getsettings($userId) {
589
-           return $this->service->getsettings($this->userId);
590
-    }
591
-
592
-
593
-    /**
594
-     * @NoAdminRequired
595
-     */
596
-    public function updatesettings($userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail) {
597
-           return $this->service->updatesettings($this->userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail);
598
-    }
599
-
600
-}
Browse code

added Contributors.txt CHANGELOG.txt appinfo/info.xml appinfo/signature.json js/sendfax.js lib/Controller/PaxfaxController.php

DoubleBastionAdmin authored on 10/04/2022 21:51:43
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,600 @@
1
+<?php
2
+/**
3
+ * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
4
+ *
5
+ * @author Double Bastion LLC
6
+ *
7
+ * @license GNU AGPL version 3 or any later version
8
+ *
9
+ * This program is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
+ * License as published by the Free Software Foundation; either
12
+ * version 3 of the License, or any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
+ *
19
+ * You should have received a copy of the GNU Affero General Public
20
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+declare(strict_types=1);
25
+
26
+namespace OCA\PaxFax\Controller;
27
+
28
+use OCP\IRequest;
29
+use OCP\AppFramework\Controller;
30
+use OCA\PaxFax\Service\PaxfaxService;
31
+use OCP\AppFramework\App;
32
+use OCP\Files\NotPermittedException;
33
+use OCP\Files\Folder;
34
+use OCP\IConfig;
35
+use OC\Files\Filesystem;
36
+use OC\Files\View;
37
+use \ReflectionClass;
38
+use \FilesystemIterator;
39
+
40
+use Phaxio;
41
+use Phaxio\OperationResult;
42
+use Phaxio\Error\AuthenticationException;
43
+use Phaxio\Error\NotFoundException;
44
+use Phaxio\Error\InvalidRequestException;
45
+use Phaxio\Error\RateLimitException;
46
+use Phaxio\Error\APIConnectionException;
47
+use Phaxio\Error\GeneralException;
48
+
49
+
50
+class PaxfaxController extends Controller {
51
+
52
+    private $service;
53
+    private $config;
54
+    private $userId;
55
+    private $folder;
56
+    private $filesystem;
57
+    private $view;
58
+
59
+    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view) {
60
+
61
+           parent::__construct($appName, $request);
62
+
63
+           $this->service = $service;
64
+           $this->config = $config;
65
+           $this->userId = $userId;
66
+           $this->folder = $folder;
67
+           $this->filesystem = $filesystem;
68
+           $this->view = $view;
69
+    }
70
+
71
+    /**
72
+     * @NoAdminRequired
73
+     */
74
+    public function cleantempdir($userId) {
75
+
76
+           // If the 'temp_files' folder doesn't exist create it
77
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
78
+               $this->folder->newFolder('Pax_Fax/temp_files');
79
+           }
80
+
81
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
82
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
83
+           $fileSystemIterator = new FilesystemIterator($targetdir);
84
+
85
+           $dirfiles = [];
86
+           foreach ($fileSystemIterator as $fileInfo){
87
+                    $dirfiles[] = $fileInfo->getFilename();
88
+           }
89
+
90
+           foreach ($dirfiles as $key => $indfile) {
91
+                    $thisuserroot = $this->view->getRoot();
92
+                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
93
+                    $removetmpfile = $this->filesystem->unlink($tempfile);
94
+           }
95
+    }
96
+
97
+
98
+    /**
99
+     * @NoAdminRequired
100
+     */
101
+    public function object_to_array($obj) {
102
+
103
+           if(is_object($obj)) $obj = (array)$this->dismount($obj);
104
+           if(is_array($obj)) {
105
+              $new = array();
106
+              foreach($obj as $key => $val) {
107
+                      $new[$key] = $this->object_to_array($val);
108
+              }
109
+           }
110
+           else $new = $obj;
111
+           return $new;
112
+    }
113
+
114
+
115
+    /**
116
+     * @NoAdminRequired
117
+     */
118
+    public function dismount($object) {
119
+           $reflectionClass = new ReflectionClass(get_class($object));
120
+           $array = array();
121
+           foreach ($reflectionClass->getProperties() as $property) {
122
+                    $property->setAccessible(true);
123
+                    $array[$property->getName()] = $property->getValue($object);
124
+                    $property->setAccessible(false);
125
+           }
126
+           return $array;
127
+    }
128
+
129
+
130
+    /**
131
+     * @NoAdminRequired
132
+     */
133
+    public function getbalance($userId) {
134
+
135
+           $apiMode = 'live';
136
+           $thisapicred = $this->service->getapicredentials($this->userId);
137
+
138
+           $apiKeys[$apiMode] = $thisapicred[0];
139
+           $apiSecrets[$apiMode] = $thisapicred[1];
140
+
141
+           $apiHost = 'https://api.phaxio.com/v2.1/';
142
+
143
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
144
+
145
+           try {
146
+                    // Get Phaxio account balance
147
+                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
148
+
149
+                    $balancetoarr = $this->object_to_array($phaxioresulttert);
150
+
151
+                    $phaxiobalance = $balancetoarr['data']['balance'];
152
+
153
+           } catch (InvalidRequestException $e) {
154
+                    $phaxiobalance = 'unknown';
155
+                    $phaxiosuccess = 'false';
156
+                    $errortype = 'invalid request error';
157
+           } catch (AuthenticationException $e) {
158
+                    $phaxiobalance = 'unknown';
159
+                    $phaxiosuccess = 'false';
160
+                    $errortype = 'authentication error';
161
+           } catch (APIConnectionException $e) {
162
+                    $phaxiobalance = 'unknown';
163
+                    $phaxiosuccess = 'false';
164
+                    $errortype = 'API connection error';
165
+           } catch (RateLimitException $e) {
166
+                    $phaxiobalance = 'unknown';
167
+                    $phaxiosuccess = 'false';
168
+                    $errortype = 'rate limit error';
169
+           } catch (NotFoundException $e) {
170
+                    $phaxiobalance = 'unknown';
171
+                    $phaxiosuccess = 'false';
172
+                    $errortype = 'not found error';
173
+           } catch (GeneralException $e) {
174
+                    $phaxiobalance = 'unknown';
175
+                    $phaxiosuccess = 'false';
176
+                    $errortype = 'undefined error';
177
+           }
178
+
179
+           return $phaxiobalance;
180
+
181
+    }
182
+
183
+
184
+    /**
185
+     * @NoAdminRequired
186
+     */
187
+    public function getfaxnumbers($userId) {
188
+
189
+           $apiMode = 'live';
190
+           $thisapicred = $this->service->getapicredentials($this->userId);
191
+
192
+           $apiKeys[$apiMode] = $thisapicred[0];
193
+           $apiSecrets[$apiMode] = $thisapicred[1];
194
+
195
+           $apiHost = 'https://api.phaxio.com/v2.1/';
196
+
197
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
198
+
199
+           try {
200
+                    // Get all Phaxio fax numbers
201
+                    $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
202
+
203
+                    $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
204
+
205
+                    $phaxioarr = [];
206
+
207
+                    foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
208
+                           if (is_array($phvalue)) {
209
+                                 foreach ($phvalue as $phkey2 => $phvalue2) {
210
+                                       if ($phkey2 == 'phone_number') {
211
+                                           $phaxioarr[] = $phvalue2;
212
+                                       }
213
+                                 }
214
+                           }
215
+                    }
216
+
217
+                    $phaxionmbrs = $phaxioarr;
218
+
219
+           } catch (InvalidRequestException $e) {
220
+                    $phaxiobalance = 'unknown';
221
+                    $phaxiosuccess = 'false';
222
+                    $errortype = 'invalid request error';
223
+           } catch (AuthenticationException $e) {
224
+                    $phaxiobalance = 'unknown';
225
+                    $phaxiosuccess = 'false';
226
+                    $errortype = 'authentication error';
227
+           } catch (APIConnectionException $e) {
228
+                    $phaxiobalance = 'unknown';
229
+                    $phaxiosuccess = 'false';
230
+                    $errortype = 'API connection error';
231
+           } catch (RateLimitException $e) {
232
+                    $phaxiobalance = 'unknown';
233
+                    $phaxiosuccess = 'false';
234
+                    $errortype = 'rate limit error';
235
+           } catch (NotFoundException $e) {
236
+                    $phaxiobalance = 'unknown';
237
+                    $phaxiosuccess = 'false';
238
+                    $errortype = 'not found error';
239
+           } catch (GeneralException $e) {
240
+                    $phaxiobalance = 'unknown';
241
+                    $phaxiosuccess = 'false';
242
+                    $errortype = 'undefined error';
243
+           }
244
+
245
+           return $phaxionmbrs;
246
+    }
247
+
248
+    /**
249
+     * @NoAdminRequired
250
+     */
251
+    protected function getFileID() {
252
+		if ($this->createdFile) {
253
+			return $this->createdFile;
254
+		}
255
+
256
+		$qb = $this->connection->getQueryBuilder();
257
+
258
+		// We create a new file entry and delete it after the test again
259
+		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
260
+		$qb->insert('filecache')
261
+			->values([
262
+				'path' => $qb->createNamedParameter($fileName),
263
+				'path_hash' => $qb->createNamedParameter(md5($fileName)),
264
+			])
265
+			->execute();
266
+		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
267
+		$qb->insert('filecache')
268
+			->values([
269
+				'path' => $qb->createNamedParameter($fileName),
270
+				'path_hash' => $qb->createNamedParameter(md5($fileName)),
271
+			])
272
+			->execute();
273
+
274
+		$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
275
+		return $this->createdFile;
276
+    }
277
+
278
+    /**
279
+     * @NoAdminRequired
280
+     */
281
+    public function uploadfile($userId, $uploadfileforfax) {
282
+
283
+           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
284
+           $fileName = $_FILES['uploadfileforfax']['name'];
285
+           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
286
+           $fileSize = $fileSizeinit / 1048576;
287
+
288
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
289
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
290
+           }
291
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
292
+               $this->folder->newFolder('Pax_Fax/faxes_received');
293
+           }
294
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
295
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
296
+           }
297
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
298
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
299
+           }
300
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
301
+               $this->folder->newFolder('Pax_Fax/temp_files');
302
+           }
303
+
304
+           $userroot = $this->view->getRoot();
305
+           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
306
+
307
+           $target = $this->folder->newFile($targetfile);
308
+           $target->putContent($fileContent);
309
+
310
+           // Get the cumulative files size of the uploaded files
311
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
312
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
313
+
314
+           $fileSystemIterator = new FilesystemIterator($targetdir);
315
+
316
+           $dirfiles = [];
317
+           foreach ($fileSystemIterator as $fileInfo){
318
+                    $dirfiles[] = $fileInfo->getFilename();
319
+           }
320
+
321
+           $totalflsizeinit = 0;
322
+           foreach ($dirfiles as $key => $indfile) {
323
+                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
324
+                    $mbSize = $fileSizeinit / 1048576;
325
+                    $totalflsizeinit += $mbSize;
326
+           }
327
+
328
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
329
+
330
+           return $totalflsize;
331
+    }
332
+
333
+    /**
334
+     * @NoAdminRequired
335
+     */
336
+    public function pickfile($userId, $path) {
337
+
338
+           $userroot = $this->view->getRoot();
339
+           $fltgt = $userroot . $path;
340
+
341
+           $fileContent = $this->filesystem->file_get_contents($fltgt);
342
+
343
+           $fileNameinit = explode("/", $path);
344
+           $fileNamesec = array_reverse($fileNameinit);
345
+           $fileName = $fileNamesec[0];
346
+
347
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
348
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
349
+           }
350
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
351
+               $this->folder->newFolder('Pax_Fax/faxes_received');
352
+           }
353
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
354
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
355
+           }
356
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
357
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
358
+           }
359
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
360
+               $this->folder->newFolder('Pax_Fax/temp_files');
361
+           }
362
+
363
+           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
364
+
365
+           $target = $this->folder->newFile($targetfile);
366
+           $target->putContent($fileContent);
367
+
368
+           // Get the cumulative files size of the uploaded files
369
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
370
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
371
+           $fileSystemIterator = new FilesystemIterator($targetdir);
372
+
373
+           $dirfiles = [];
374
+           foreach ($fileSystemIterator as $fileInfo){
375
+                    $dirfiles[] = $fileInfo->getFilename();
376
+           }
377
+
378
+           $totalflsizeinit = 0;
379
+
380
+           foreach ($dirfiles as $key => $indfile) {
381
+
382
+                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
383
+                    $mbSize = $fileSizeinit / 1048576;
384
+                    $totalflsizeinit += $mbSize;
385
+           }
386
+
387
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
388
+
389
+           return $totalflsize;
390
+    }
391
+
392
+    /**
393
+     * @NoAdminRequired
394
+     */
395
+    public function removeupfile($userId, $removedfilename) {
396
+
397
+           $thisuserroot = $this->view->getRoot();
398
+
399
+           $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $removedfilename;
400
+           $removetmpfile = $this->filesystem->unlink($tempfile);
401
+
402
+           // Get the cumulative files size of the uploaded files
403
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
404
+           $targetdir = $datadir . $this->userId . "/files/Pax_Fax/temp_files";
405
+           $fileSystemIterator = new FilesystemIterator($targetdir);
406
+
407
+           $dirfiles = [];
408
+           foreach ($fileSystemIterator as $fileInfo){
409
+                    $dirfiles[] = $fileInfo->getFilename();
410
+           }
411
+
412
+           $totalflsizeinit = 0;
413
+
414
+           foreach ($dirfiles as $key => $indfile) {
415
+
416
+                    $fileSizeinit = $this->filesystem->filesize($thisuserroot . "/Pax_Fax/temp_files/" . $indfile);
417
+                    $mbSize = $fileSizeinit / 1048576;
418
+                    $totalflsizeinit += $mbSize;
419
+           }
420
+
421
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
422
+
423
+           return $totalflsize;
424
+    }
425
+
426
+    /**
427
+     * @NoAdminRequired
428
+     */
429
+    public function getpickedfile($userId, $pickedfilename) {
430
+
431
+           $thisuserroot = $this->view->getRoot();
432
+
433
+           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
434
+           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
435
+
436
+           $namesplit = explode(".", $pickedfilename);
437
+           $extension = end($namesplit);
438
+
439
+           if ($extension == "txt" || $extension == "html") {
440
+               $getpickedfile = $getfilecontent;
441
+           } elseif ($extension == "jpg") {
442
+               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
443
+           } elseif ($extension == "png") {
444
+               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
445
+           } else { $getpickedfile = ""; }
446
+
447
+           return $getpickedfile;
448
+    }
449
+
450
+    /**
451
+     * @NoAdminRequired
452
+     */
453
+    public function sendfax($userId, $uploadedtofax, $selectedcid, $toNumber) {
454
+
455
+           $tonumbertr = str_replace("+", "", $toNumber[0]);
456
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
457
+
458
+           $fromnumberdigits = str_replace("+", "", $selectedcid);
459
+           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
460
+
461
+           $countfaxfiles = count($uploadedtofax);
462
+
463
+           $openedfiles = [];
464
+
465
+           foreach ($uploadedtofax as $key => $flname) {
466
+
467
+                    if ($key == 0) { $firstflname = $flname; }
468
+
469
+                    $fileNamesec = array_reverse(explode(".", $flname));
470
+                    $filenameext = $fileNamesec[0];
471
+                    array_shift($fileNamesec);
472
+
473
+                    $fileName = implode("", $fileNamesec);
474
+
475
+                    $userroot = $this->view->getRoot();
476
+
477
+                    if ($countfaxfiles == 1) {
478
+                        $targetfile = $userroot . "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
479
+                    } else {
480
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
481
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
482
+                         }
483
+                         $targetfile = $userroot . "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
484
+                    }
485
+
486
+                    $fltgt = $userroot . "/Pax_Fax/temp_files/" . $flname;
487
+                    $fileContent = $this->filesystem->file_get_contents($fltgt);
488
+
489
+                    $target = $this->folder->newFile($targetfile);
490
+                    $target->putContent($fileContent);
491
+
492
+                    $removetmpfile = $this->filesystem->unlink($fltgt);
493
+                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
494
+
495
+                    if ($countfaxfiles == 1) {
496
+                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
497
+                    } else {
498
+                        $openedfiles[] = fopen($datadir . $this->userId . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
499
+                    }
500
+           }
501
+
502
+           $params = array(
503
+                           'to' => $toNumber,
504
+                           'file' => $openedfiles,
505
+                           'caller_id' => $selectedcid
506
+                     );
507
+
508
+           $apiMode = 'live';
509
+
510
+           $thisapicred = $this->service->getapicredentials($this->userId);
511
+
512
+           $apiKeys[$apiMode] = $thisapicred[0];
513
+           $apiSecrets[$apiMode] = $thisapicred[1];
514
+
515
+           $apiHost = 'https://api.phaxio.com/v2.1/';
516
+
517
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
518
+
519
+           try {
520
+                    $phaxioresultinit = $phaxio->sendFax($params);
521
+
522
+                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
523
+
524
+                    $statustoarr = $this->object_to_array($phaxioresultsec);
525
+
526
+                    $phaxiosuccess = $statustoarr['success'];
527
+
528
+                    $errortype = 'there are no errors';
529
+
530
+           } catch (InvalidRequestException $e) {
531
+                    $phaxiosuccess = 'false';
532
+                    $errortype = 'invalid request error';
533
+           } catch (AuthenticationException $e) {
534
+                    $phaxiosuccess = 'false';
535
+                    $errortype = 'authentication error';
536
+           } catch (APIConnectionException $e) {
537
+                    $phaxiosuccess = 'false';
538
+                    $errortype = 'API connection error';
539
+           } catch (RateLimitException $e) {
540
+                    $phaxiosuccess = 'false';
541
+                    $errortype = 'rate limit error';
542
+           } catch (NotFoundException $e) {
543
+                    $phaxiosuccess = 'false';
544
+                    $errortype = 'not found error';
545
+           } catch (GeneralException $e) {
546
+                    $phaxiosuccess = 'false';
547
+                    $errortype = 'undefined error';
548
+           }
549
+
550
+           $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
551
+
552
+           if ($phaxiosuccess != 'true') {
553
+
554
+                    foreach ($uploadedtofax as $key => $flname) {
555
+
556
+                        $fileNamesec = array_reverse(explode(".", $flname));
557
+                        $filenameext = $fileNamesec[0];
558
+                        array_shift($fileNamesec);
559
+
560
+                        $fileName = implode("", $fileNamesec);
561
+
562
+                        if ($countfaxfiles == 1) {
563
+                            $failedfl = $userroot . "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
564
+                            $newtargetfl =  $userroot . "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
565
+                        } else {
566
+                            $failedfl = $userroot . "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
567
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
568
+                            $newtargetfl = $userroot . "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
569
+                        }
570
+
571
+                        $fileContent = $this->filesystem->file_get_contents($failedfl);
572
+
573
+                        $targetact = $this->folder->newFile($newtargetfl);
574
+                        $targetact->putContent($fileContent);
575
+
576
+                        $removefailed = $this->filesystem->unlink($failedfl);
577
+                    }
578
+
579
+           }
580
+
581
+           return $phaxioresult;
582
+    }
583
+
584
+
585
+    /**
586
+     * @NoAdminRequired
587
+     */
588
+    public function getsettings($userId) {
589
+           return $this->service->getsettings($this->userId);
590
+    }
591
+
592
+
593
+    /**
594
+     * @NoAdminRequired
595
+     */
596
+    public function updatesettings($userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail) {
597
+           return $this->service->updatesettings($this->userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail);
598
+    }
599
+
600
+}
Browse code

removed CHANGELOG.txt appinfo/info.xml appinfo/signature.json js/sendfax.js lib/Controller/PaxfaxController.php

DoubleBastionAdmin authored on 10/04/2022 21:48:49
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,592 +0,0 @@
1
-<?php
2
-/**
3
- * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
4
- *
5
- * @author Double Bastion LLC
6
- *
7
- * @license GNU AGPL version 3 or any later version
8
- *
9
- * This program is free software; you can redistribute it and/or
10
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
- * License as published by the Free Software Foundation; either
12
- * version 3 of the License, or any later version.
13
- *
14
- * This program is distributed in the hope that it will be useful,
15
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
- *
19
- * You should have received a copy of the GNU Affero General Public
20
- * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
- *
22
- */
23
-
24
-declare(strict_types=1);
25
-
26
-namespace OCA\PaxFax\Controller;
27
-
28
-use OCP\IRequest;
29
-use OCP\AppFramework\Controller;
30
-use OCA\PaxFax\Service\PaxfaxService;
31
-use OCP\AppFramework\App;
32
-use OCP\Files\NotPermittedException;
33
-use OCP\Files\Folder;
34
-use OC\Files\Filesystem;
35
-use OC\Files\View;
36
-use \ReflectionClass;
37
-use \FilesystemIterator;
38
-
39
-use Phaxio;
40
-use Phaxio\OperationResult;
41
-use Phaxio\Error\AuthenticationException;
42
-use Phaxio\Error\NotFoundException;
43
-use Phaxio\Error\InvalidRequestException;
44
-use Phaxio\Error\RateLimitException;
45
-use Phaxio\Error\APIConnectionException;
46
-use Phaxio\Error\GeneralException;
47
-
48
-
49
-class PaxfaxController extends Controller {
50
-
51
-    private $service;
52
-    private $userId;
53
-    private $folder;
54
-    private $filesystem;
55
-    private $view;
56
-
57
-    public function __construct($appName, IRequest $request, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId, View $view) {
58
-
59
-           parent::__construct($appName, $request);
60
-
61
-           $this->service = $service;
62
-           $this->userId = $userId;
63
-           $this->folder = $folder;
64
-           $this->filesystem = $filesystem;
65
-           $this->view = $view;
66
-    }
67
-
68
-    /**
69
-     * @NoAdminRequired
70
-     */
71
-    public function cleantempdir($userId) {
72
-
73
-           // If the 'temp_files' folder doesn't exist create it
74
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
75
-               $this->folder->newFolder('Pax_Fax/temp_files');
76
-           }
77
-
78
-           $targetdir = "data/" . $this->userId . "/files/Pax_Fax/temp_files";
79
-           $fileSystemIterator = new FilesystemIterator($targetdir);
80
-
81
-           $dirfiles = [];
82
-           foreach ($fileSystemIterator as $fileInfo){
83
-                    $dirfiles[] = $fileInfo->getFilename();
84
-           }
85
-
86
-           foreach ($dirfiles as $key => $indfile) {
87
-                    $thisuserroot = $this->view->getRoot();
88
-                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
89
-                    $removetmpfile = $this->filesystem->unlink($tempfile);
90
-           }
91
-    }
92
-
93
-
94
-    /**
95
-     * @NoAdminRequired
96
-     */
97
-    public function object_to_array($obj) {
98
-
99
-           if(is_object($obj)) $obj = (array)$this->dismount($obj);
100
-           if(is_array($obj)) {
101
-              $new = array();
102
-              foreach($obj as $key => $val) {
103
-                      $new[$key] = $this->object_to_array($val);
104
-              }
105
-           }
106
-           else $new = $obj;
107
-           return $new;
108
-    }
109
-
110
-
111
-    /**
112
-     * @NoAdminRequired
113
-     */
114
-    public function dismount($object) {
115
-           $reflectionClass = new ReflectionClass(get_class($object));
116
-           $array = array();
117
-           foreach ($reflectionClass->getProperties() as $property) {
118
-                    $property->setAccessible(true);
119
-                    $array[$property->getName()] = $property->getValue($object);
120
-                    $property->setAccessible(false);
121
-           }
122
-           return $array;
123
-    }
124
-
125
-
126
-    /**
127
-     * @NoAdminRequired
128
-     */
129
-    public function getbalance($userId) {
130
-
131
-           $apiMode = 'live';
132
-           $thisapicred = $this->service->getapicredentials($this->userId);
133
-
134
-           $apiKeys[$apiMode] = $thisapicred[0];
135
-           $apiSecrets[$apiMode] = $thisapicred[1];
136
-
137
-           $apiHost = 'https://api.phaxio.com/v2.1/';
138
-
139
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
140
-
141
-           try {
142
-                    // Get Phaxio account balance
143
-                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
144
-
145
-                    $balancetoarr = $this->object_to_array($phaxioresulttert);
146
-
147
-                    $phaxiobalance = $balancetoarr['data']['balance'];
148
-
149
-           } catch (InvalidRequestException $e) {
150
-                    $phaxiobalance = 'unknown';
151
-                    $phaxiosuccess = 'false';
152
-                    $errortype = 'invalid request error';
153
-           } catch (AuthenticationException $e) {
154
-                    $phaxiobalance = 'unknown';
155
-                    $phaxiosuccess = 'false';
156
-                    $errortype = 'authentication error';
157
-           } catch (APIConnectionException $e) {
158
-                    $phaxiobalance = 'unknown';
159
-                    $phaxiosuccess = 'false';
160
-                    $errortype = 'API connection error';
161
-           } catch (RateLimitException $e) {
162
-                    $phaxiobalance = 'unknown';
163
-                    $phaxiosuccess = 'false';
164
-                    $errortype = 'rate limit error';
165
-           } catch (NotFoundException $e) {
166
-                    $phaxiobalance = 'unknown';
167
-                    $phaxiosuccess = 'false';
168
-                    $errortype = 'not found error';
169
-           } catch (GeneralException $e) {
170
-                    $phaxiobalance = 'unknown';
171
-                    $phaxiosuccess = 'false';
172
-                    $errortype = 'undefined error';
173
-           }
174
-
175
-           return $phaxiobalance;
176
-
177
-    }
178
-
179
-
180
-    /**
181
-     * @NoAdminRequired
182
-     */
183
-    public function getfaxnumbers($userId) {
184
-
185
-           $apiMode = 'live';
186
-           $thisapicred = $this->service->getapicredentials($this->userId);
187
-
188
-           $apiKeys[$apiMode] = $thisapicred[0];
189
-           $apiSecrets[$apiMode] = $thisapicred[1];
190
-
191
-           $apiHost = 'https://api.phaxio.com/v2.1/';
192
-
193
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
194
-
195
-           try {
196
-                    // Get all Phaxio fax numbers
197
-                    $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
198
-
199
-                    $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
200
-
201
-                    $phaxioarr = [];
202
-
203
-                    foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
204
-                           if (is_array($phvalue)) {
205
-                                 foreach ($phvalue as $phkey2 => $phvalue2) {
206
-                                       if ($phkey2 == 'phone_number') {
207
-                                           $phaxioarr[] = $phvalue2;
208
-                                       }
209
-                                 }
210
-                           }
211
-                    }
212
-
213
-                    $phaxionmbrs = $phaxioarr;
214
-
215
-           } catch (InvalidRequestException $e) {
216
-                    $phaxiobalance = 'unknown';
217
-                    $phaxiosuccess = 'false';
218
-                    $errortype = 'invalid request error';
219
-           } catch (AuthenticationException $e) {
220
-                    $phaxiobalance = 'unknown';
221
-                    $phaxiosuccess = 'false';
222
-                    $errortype = 'authentication error';
223
-           } catch (APIConnectionException $e) {
224
-                    $phaxiobalance = 'unknown';
225
-                    $phaxiosuccess = 'false';
226
-                    $errortype = 'API connection error';
227
-           } catch (RateLimitException $e) {
228
-                    $phaxiobalance = 'unknown';
229
-                    $phaxiosuccess = 'false';
230
-                    $errortype = 'rate limit error';
231
-           } catch (NotFoundException $e) {
232
-                    $phaxiobalance = 'unknown';
233
-                    $phaxiosuccess = 'false';
234
-                    $errortype = 'not found error';
235
-           } catch (GeneralException $e) {
236
-                    $phaxiobalance = 'unknown';
237
-                    $phaxiosuccess = 'false';
238
-                    $errortype = 'undefined error';
239
-           }
240
-
241
-           return $phaxionmbrs;
242
-    }
243
-
244
-    /**
245
-     * @NoAdminRequired
246
-     */
247
-    protected function getFileID() {
248
-		if ($this->createdFile) {
249
-			return $this->createdFile;
250
-		}
251
-
252
-		$qb = $this->connection->getQueryBuilder();
253
-
254
-		// We create a new file entry and delete it after the test again
255
-		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
256
-		$qb->insert('filecache')
257
-			->values([
258
-				'path' => $qb->createNamedParameter($fileName),
259
-				'path_hash' => $qb->createNamedParameter(md5($fileName)),
260
-			])
261
-			->execute();
262
-		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
263
-		$qb->insert('filecache')
264
-			->values([
265
-				'path' => $qb->createNamedParameter($fileName),
266
-				'path_hash' => $qb->createNamedParameter(md5($fileName)),
267
-			])
268
-			->execute();
269
-
270
-		$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
271
-		return $this->createdFile;
272
-    }
273
-
274
-    /**
275
-     * @NoAdminRequired
276
-     */
277
-    public function uploadfile($userId, $uploadfileforfax) {
278
-
279
-           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
280
-           $fileName = $_FILES['uploadfileforfax']['name'];
281
-           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
282
-           $fileSize = $fileSizeinit / 1048576;
283
-
284
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
285
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
286
-           }
287
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
288
-               $this->folder->newFolder('Pax_Fax/faxes_received');
289
-           }
290
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
291
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
292
-           }
293
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
294
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
295
-           }
296
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
297
-               $this->folder->newFolder('Pax_Fax/temp_files');
298
-           }
299
-
300
-           $userroot = $this->view->getRoot();
301
-           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
302
-
303
-           $target = $this->folder->newFile($targetfile);
304
-           $target->putContent($fileContent);
305
-
306
-           // Get the cumulative files size of the uploaded files
307
-           $targetdir = "data/" . $this->userId . "/files/Pax_Fax/temp_files";
308
-
309
-           $fileSystemIterator = new FilesystemIterator($targetdir);
310
-
311
-           $dirfiles = [];
312
-           foreach ($fileSystemIterator as $fileInfo){
313
-                    $dirfiles[] = $fileInfo->getFilename();
314
-           }
315
-
316
-           $totalflsizeinit = 0;
317
-           foreach ($dirfiles as $key => $indfile) {
318
-                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
319
-                    $mbSize = $fileSizeinit / 1048576;
320
-                    $totalflsizeinit += $mbSize;
321
-           }
322
-
323
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
324
-
325
-           return $totalflsize;
326
-    }
327
-
328
-    /**
329
-     * @NoAdminRequired
330
-     */
331
-    public function pickfile($userId, $path) {
332
-
333
-           $userroot = $this->view->getRoot();
334
-           $fltgt = $userroot . $path;
335
-
336
-           $fileContent = $this->filesystem->file_get_contents($fltgt);
337
-
338
-           $fileNameinit = explode("/", $path);
339
-           $fileNamesec = array_reverse($fileNameinit);
340
-           $fileName = $fileNamesec[0];
341
-
342
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
343
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
344
-           }
345
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
346
-               $this->folder->newFolder('Pax_Fax/faxes_received');
347
-           }
348
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
349
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
350
-           }
351
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
352
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
353
-           }
354
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
355
-               $this->folder->newFolder('Pax_Fax/temp_files');
356
-           }
357
-
358
-           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
359
-
360
-           $target = $this->folder->newFile($targetfile);
361
-           $target->putContent($fileContent);
362
-
363
-           // Get the cumulative files size of the uploaded files
364
-           $targetdir = "data/" . $this->userId . "/files/Pax_Fax/temp_files";
365
-           $fileSystemIterator = new FilesystemIterator($targetdir);
366
-
367
-           $dirfiles = [];
368
-           foreach ($fileSystemIterator as $fileInfo){
369
-                    $dirfiles[] = $fileInfo->getFilename();
370
-           }
371
-
372
-           $totalflsizeinit = 0;
373
-
374
-           foreach ($dirfiles as $key => $indfile) {
375
-
376
-                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
377
-                    $mbSize = $fileSizeinit / 1048576;
378
-                    $totalflsizeinit += $mbSize;
379
-           }
380
-
381
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
382
-
383
-           return $totalflsize;
384
-    }
385
-
386
-    /**
387
-     * @NoAdminRequired
388
-     */
389
-    public function removeupfile($userId, $removedfilename) {
390
-
391
-           $thisuserroot = $this->view->getRoot();
392
-
393
-           $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $removedfilename;
394
-           $removetmpfile = $this->filesystem->unlink($tempfile);
395
-
396
-           // Get the cumulative files size of the uploaded files
397
-           $targetdir = "data/" . $this->userId . "/files/Pax_Fax/temp_files";
398
-           $fileSystemIterator = new FilesystemIterator($targetdir);
399
-
400
-           $dirfiles = [];
401
-           foreach ($fileSystemIterator as $fileInfo){
402
-                    $dirfiles[] = $fileInfo->getFilename();
403
-           }
404
-
405
-           $totalflsizeinit = 0;
406
-
407
-           foreach ($dirfiles as $key => $indfile) {
408
-
409
-                    $fileSizeinit = $this->filesystem->filesize($thisuserroot . "/Pax_Fax/temp_files/" . $indfile);
410
-                    $mbSize = $fileSizeinit / 1048576;
411
-                    $totalflsizeinit += $mbSize;
412
-           }
413
-
414
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
415
-
416
-           return $totalflsize;
417
-    }
418
-
419
-    /**
420
-     * @NoAdminRequired
421
-     */
422
-    public function getpickedfile($userId, $pickedfilename) {
423
-
424
-           $thisuserroot = $this->view->getRoot();
425
-
426
-           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
427
-           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
428
-
429
-           $namesplit = explode(".", $pickedfilename);
430
-           $extension = end($namesplit);
431
-
432
-           if ($extension == "txt" || $extension == "html") {
433
-               $getpickedfile = $getfilecontent;
434
-           } elseif ($extension == "jpg") {
435
-               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
436
-           } elseif ($extension == "png") {
437
-               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
438
-           } else { $getpickedfile = ""; }
439
-
440
-           return $getpickedfile;
441
-    }
442
-
443
-    /**
444
-     * @NoAdminRequired
445
-     */
446
-    public function sendfax($userId, $uploadedtofax, $selectedcid, $toNumber) {
447
-
448
-           $tonumbertr = str_replace("+", "", $toNumber[0]);
449
-           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
450
-
451
-           $fromnumberdigits = str_replace("+", "", $selectedcid);
452
-           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
453
-
454
-           $countfaxfiles = count($uploadedtofax);
455
-
456
-           $openedfiles = [];
457
-
458
-           foreach ($uploadedtofax as $key => $flname) {
459
-
460
-                    if ($key == 0) { $firstflname = $flname; }
461
-
462
-                    $fileNamesec = array_reverse(explode(".", $flname));
463
-                    $filenameext = $fileNamesec[0];
464
-                    array_shift($fileNamesec);
465
-
466
-                    $fileName = implode("", $fileNamesec);
467
-
468
-                    $userroot = $this->view->getRoot();
469
-
470
-                    if ($countfaxfiles == 1) {
471
-                        $targetfile = $userroot . "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
472
-                    } else {
473
-                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
474
-                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
475
-                         }
476
-                         $targetfile = $userroot . "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
477
-                    }
478
-
479
-                    $fltgt = $userroot . "/Pax_Fax/temp_files/" . $flname;
480
-                    $fileContent = $this->filesystem->file_get_contents($fltgt);
481
-
482
-                    $target = $this->folder->newFile($targetfile);
483
-                    $target->putContent($fileContent);
484
-
485
-                    $removetmpfile = $this->filesystem->unlink($fltgt);
486
-
487
-                    if ($countfaxfiles == 1) {
488
-                        $openedfiles[] = fopen("data/" . $this->userId . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
489
-                    } else {
490
-                        $openedfiles[] = fopen("data/" . $this->userId . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
491
-                    }
492
-           }
493
-
494
-           $params = array(
495
-                           'to' => $toNumber,
496
-                           'file' => $openedfiles,
497
-                           'caller_id' => $selectedcid
498
-                     );
499
-
500
-           $apiMode = 'live';
501
-
502
-           $thisapicred = $this->service->getapicredentials($this->userId);
503
-
504
-           $apiKeys[$apiMode] = $thisapicred[0];
505
-           $apiSecrets[$apiMode] = $thisapicred[1];
506
-
507
-           $apiHost = 'https://api.phaxio.com/v2.1/';
508
-
509
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
510
-
511
-           try {
512
-                    $phaxioresultinit = $phaxio->sendFax($params);
513
-
514
-                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
515
-
516
-                    $statustoarr = $this->object_to_array($phaxioresultsec);
517
-
518
-                    $phaxiosuccess = $statustoarr['success'];
519
-
520
-                    $errortype = 'there are no errors';
521
-
522
-           } catch (InvalidRequestException $e) {
523
-                    $phaxiosuccess = 'false';
524
-                    $errortype = 'invalid request error';
525
-           } catch (AuthenticationException $e) {
526
-                    $phaxiosuccess = 'false';
527
-                    $errortype = 'authentication error';
528
-           } catch (APIConnectionException $e) {
529
-                    $phaxiosuccess = 'false';
530
-                    $errortype = 'API connection error';
531
-           } catch (RateLimitException $e) {
532
-                    $phaxiosuccess = 'false';
533
-                    $errortype = 'rate limit error';
534
-           } catch (NotFoundException $e) {
535
-                    $phaxiosuccess = 'false';
536
-                    $errortype = 'not found error';
537
-           } catch (GeneralException $e) {
538
-                    $phaxiosuccess = 'false';
539
-                    $errortype = 'undefined error';
540
-           }
541
-
542
-           $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
543
-
544
-           if ($phaxiosuccess != 'true') {
545
-
546
-                    foreach ($uploadedtofax as $key => $flname) {
547
-
548
-                        $fileNamesec = array_reverse(explode(".", $flname));
549
-                        $filenameext = $fileNamesec[0];
550
-                        array_shift($fileNamesec);
551
-
552
-                        $fileName = implode("", $fileNamesec);
553
-
554
-                        if ($countfaxfiles == 1) {
555
-                            $failedfl = $userroot . "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
556
-                            $newtargetfl =  $userroot . "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
557
-                        } else {
558
-                            $failedfl = $userroot . "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
559
-                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
560
-                            $newtargetfl = $userroot . "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
561
-                        }
562
-
563
-                        $fileContent = $this->filesystem->file_get_contents($failedfl);
564
-
565
-                        $targetact = $this->folder->newFile($newtargetfl);
566
-                        $targetact->putContent($fileContent);
567
-
568
-                        $removefailed = $this->filesystem->unlink($failedfl);
569
-                    }
570
-
571
-           }
572
-
573
-           return $phaxioresult;
574
-    }
575
-
576
-
577
-    /**
578
-     * @NoAdminRequired
579
-     */
580
-    public function getsettings($userId) {
581
-           return $this->service->getsettings($this->userId);
582
-    }
583
-
584
-
585
-    /**
586
-     * @NoAdminRequired
587
-     */
588
-    public function updatesettings($userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail) {
589
-           return $this->service->updatesettings($this->userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail);
590
-    }
591
-
592
-}
Browse code

Created repository.

DoubleBastionAdmin authored on 01/03/2022 23:31:10
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,592 @@
1
+<?php
2
+/**
3
+ * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
4
+ *
5
+ * @author Double Bastion LLC
6
+ *
7
+ * @license GNU AGPL version 3 or any later version
8
+ *
9
+ * This program is free software; you can redistribute it and/or
10
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
+ * License as published by the Free Software Foundation; either
12
+ * version 3 of the License, or any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
+ *
19
+ * You should have received a copy of the GNU Affero General Public
20
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+declare(strict_types=1);
25
+
26
+namespace OCA\PaxFax\Controller;
27
+
28
+use OCP\IRequest;
29
+use OCP\AppFramework\Controller;
30
+use OCA\PaxFax\Service\PaxfaxService;
31
+use OCP\AppFramework\App;
32
+use OCP\Files\NotPermittedException;
33
+use OCP\Files\Folder;
34
+use OC\Files\Filesystem;
35
+use OC\Files\View;
36
+use \ReflectionClass;
37
+use \FilesystemIterator;
38
+
39
+use Phaxio;
40
+use Phaxio\OperationResult;
41
+use Phaxio\Error\AuthenticationException;
42
+use Phaxio\Error\NotFoundException;
43
+use Phaxio\Error\InvalidRequestException;
44
+use Phaxio\Error\RateLimitException;
45
+use Phaxio\Error\APIConnectionException;
46
+use Phaxio\Error\GeneralException;
47
+
48
+
49
+class PaxfaxController extends Controller {
50
+
51
+    private $service;
52
+    private $userId;
53
+    private $folder;
54
+    private $filesystem;
55
+    private $view;
56
+
57
+    public function __construct($appName, IRequest $request, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId, View $view) {
58
+
59
+           parent::__construct($appName, $request);
60
+
61
+           $this->service = $service;
62
+           $this->userId = $userId;
63
+           $this->folder = $folder;
64
+           $this->filesystem = $filesystem;
65
+           $this->view = $view;
66
+    }
67
+
68
+    /**
69
+     * @NoAdminRequired
70
+     */
71
+    public function cleantempdir($userId) {
72
+
73
+           // If the 'temp_files' folder doesn't exist create it
74
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
75
+               $this->folder->newFolder('Pax_Fax/temp_files');
76
+           }
77
+
78
+           $targetdir = "data/" . $this->userId . "/files/Pax_Fax/temp_files";
79
+           $fileSystemIterator = new FilesystemIterator($targetdir);
80
+
81
+           $dirfiles = [];
82
+           foreach ($fileSystemIterator as $fileInfo){
83
+                    $dirfiles[] = $fileInfo->getFilename();
84
+           }
85
+
86
+           foreach ($dirfiles as $key => $indfile) {
87
+                    $thisuserroot = $this->view->getRoot();
88
+                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
89
+                    $removetmpfile = $this->filesystem->unlink($tempfile);
90
+           }
91
+    }
92
+
93
+
94
+    /**
95
+     * @NoAdminRequired
96
+     */
97
+    public function object_to_array($obj) {
98
+
99
+           if(is_object($obj)) $obj = (array)$this->dismount($obj);
100
+           if(is_array($obj)) {
101
+              $new = array();
102
+              foreach($obj as $key => $val) {
103
+                      $new[$key] = $this->object_to_array($val);
104
+              }
105
+           }
106
+           else $new = $obj;
107
+           return $new;
108
+    }
109
+
110
+
111
+    /**
112
+     * @NoAdminRequired
113
+     */
114
+    public function dismount($object) {
115
+           $reflectionClass = new ReflectionClass(get_class($object));
116
+           $array = array();
117
+           foreach ($reflectionClass->getProperties() as $property) {
118
+                    $property->setAccessible(true);
119
+                    $array[$property->getName()] = $property->getValue($object);
120
+                    $property->setAccessible(false);
121
+           }
122
+           return $array;
123
+    }
124
+
125
+
126
+    /**
127
+     * @NoAdminRequired
128
+     */
129
+    public function getbalance($userId) {
130
+
131
+           $apiMode = 'live';
132
+           $thisapicred = $this->service->getapicredentials($this->userId);
133
+
134
+           $apiKeys[$apiMode] = $thisapicred[0];
135
+           $apiSecrets[$apiMode] = $thisapicred[1];
136
+
137
+           $apiHost = 'https://api.phaxio.com/v2.1/';
138
+
139
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
140
+
141
+           try {
142
+                    // Get Phaxio account balance
143
+                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
144
+
145
+                    $balancetoarr = $this->object_to_array($phaxioresulttert);
146
+
147
+                    $phaxiobalance = $balancetoarr['data']['balance'];
148
+
149
+           } catch (InvalidRequestException $e) {
150
+                    $phaxiobalance = 'unknown';
151
+                    $phaxiosuccess = 'false';
152
+                    $errortype = 'invalid request error';
153
+           } catch (AuthenticationException $e) {
154
+                    $phaxiobalance = 'unknown';
155
+                    $phaxiosuccess = 'false';
156
+                    $errortype = 'authentication error';
157
+           } catch (APIConnectionException $e) {
158
+                    $phaxiobalance = 'unknown';
159
+                    $phaxiosuccess = 'false';
160
+                    $errortype = 'API connection error';
161
+           } catch (RateLimitException $e) {
162
+                    $phaxiobalance = 'unknown';
163
+                    $phaxiosuccess = 'false';
164
+                    $errortype = 'rate limit error';
165
+           } catch (NotFoundException $e) {
166
+                    $phaxiobalance = 'unknown';
167
+                    $phaxiosuccess = 'false';
168
+                    $errortype = 'not found error';
169
+           } catch (GeneralException $e) {
170
+                    $phaxiobalance = 'unknown';
171
+                    $phaxiosuccess = 'false';
172
+                    $errortype = 'undefined error';
173
+           }
174
+
175
+           return $phaxiobalance;
176
+
177
+    }
178
+
179
+
180
+    /**
181
+     * @NoAdminRequired
182
+     */
183
+    public function getfaxnumbers($userId) {
184
+
185
+           $apiMode = 'live';
186
+           $thisapicred = $this->service->getapicredentials($this->userId);
187
+
188
+           $apiKeys[$apiMode] = $thisapicred[0];
189
+           $apiSecrets[$apiMode] = $thisapicred[1];
190
+
191
+           $apiHost = 'https://api.phaxio.com/v2.1/';
192
+
193
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
194
+
195
+           try {
196
+                    // Get all Phaxio fax numbers
197
+                    $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
198
+
199
+                    $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
200
+
201
+                    $phaxioarr = [];
202
+
203
+                    foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
204
+                           if (is_array($phvalue)) {
205
+                                 foreach ($phvalue as $phkey2 => $phvalue2) {
206
+                                       if ($phkey2 == 'phone_number') {
207
+                                           $phaxioarr[] = $phvalue2;
208
+                                       }
209
+                                 }
210
+                           }
211
+                    }
212
+
213
+                    $phaxionmbrs = $phaxioarr;
214
+
215
+           } catch (InvalidRequestException $e) {
216
+                    $phaxiobalance = 'unknown';
217
+                    $phaxiosuccess = 'false';
218
+                    $errortype = 'invalid request error';
219
+           } catch (AuthenticationException $e) {
220
+                    $phaxiobalance = 'unknown';
221
+                    $phaxiosuccess = 'false';
222
+                    $errortype = 'authentication error';
223
+           } catch (APIConnectionException $e) {
224
+                    $phaxiobalance = 'unknown';
225
+                    $phaxiosuccess = 'false';
226
+                    $errortype = 'API connection error';
227
+           } catch (RateLimitException $e) {
228
+                    $phaxiobalance = 'unknown';
229
+                    $phaxiosuccess = 'false';
230
+                    $errortype = 'rate limit error';
231
+           } catch (NotFoundException $e) {
232
+                    $phaxiobalance = 'unknown';
233
+                    $phaxiosuccess = 'false';
234
+                    $errortype = 'not found error';
235
+           } catch (GeneralException $e) {
236
+                    $phaxiobalance = 'unknown';
237
+                    $phaxiosuccess = 'false';
238
+                    $errortype = 'undefined error';
239
+           }
240
+
241
+           return $phaxionmbrs;
242
+    }
243
+
244
+    /**
245
+     * @NoAdminRequired
246
+     */
247
+    protected function getFileID() {
248
+		if ($this->createdFile) {
249
+			return $this->createdFile;
250
+		}
251
+
252
+		$qb = $this->connection->getQueryBuilder();
253
+
254
+		// We create a new file entry and delete it after the test again
255
+		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
256
+		$qb->insert('filecache')
257
+			->values([
258
+				'path' => $qb->createNamedParameter($fileName),
259
+				'path_hash' => $qb->createNamedParameter(md5($fileName)),
260
+			])
261
+			->execute();
262
+		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
263
+		$qb->insert('filecache')
264
+			->values([
265
+				'path' => $qb->createNamedParameter($fileName),
266
+				'path_hash' => $qb->createNamedParameter(md5($fileName)),
267
+			])
268
+			->execute();
269
+
270
+		$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
271
+		return $this->createdFile;
272
+    }
273
+
274
+    /**
275
+     * @NoAdminRequired
276
+     */
277
+    public function uploadfile($userId, $uploadfileforfax) {
278
+
279
+           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
280
+           $fileName = $_FILES['uploadfileforfax']['name'];
281
+           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
282
+           $fileSize = $fileSizeinit / 1048576;
283
+
284
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
285
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
286
+           }
287
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
288
+               $this->folder->newFolder('Pax_Fax/faxes_received');
289
+           }
290
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
291
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
292
+           }
293
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
294
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
295
+           }
296
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
297
+               $this->folder->newFolder('Pax_Fax/temp_files');
298
+           }
299
+
300
+           $userroot = $this->view->getRoot();
301
+           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
302
+
303
+           $target = $this->folder->newFile($targetfile);
304
+           $target->putContent($fileContent);
305
+
306
+           // Get the cumulative files size of the uploaded files
307
+           $targetdir = "data/" . $this->userId . "/files/Pax_Fax/temp_files";
308
+
309
+           $fileSystemIterator = new FilesystemIterator($targetdir);
310
+
311
+           $dirfiles = [];
312
+           foreach ($fileSystemIterator as $fileInfo){
313
+                    $dirfiles[] = $fileInfo->getFilename();
314
+           }
315
+
316
+           $totalflsizeinit = 0;
317
+           foreach ($dirfiles as $key => $indfile) {
318
+                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
319
+                    $mbSize = $fileSizeinit / 1048576;
320
+                    $totalflsizeinit += $mbSize;
321
+           }
322
+
323
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
324
+
325
+           return $totalflsize;
326
+    }
327
+
328
+    /**
329
+     * @NoAdminRequired
330
+     */
331
+    public function pickfile($userId, $path) {
332
+
333
+           $userroot = $this->view->getRoot();
334
+           $fltgt = $userroot . $path;
335
+
336
+           $fileContent = $this->filesystem->file_get_contents($fltgt);
337
+
338
+           $fileNameinit = explode("/", $path);
339
+           $fileNamesec = array_reverse($fileNameinit);
340
+           $fileName = $fileNamesec[0];
341
+
342
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
343
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
344
+           }
345
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
346
+               $this->folder->newFolder('Pax_Fax/faxes_received');
347
+           }
348
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
349
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
350
+           }
351
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
352
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
353
+           }
354
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
355
+               $this->folder->newFolder('Pax_Fax/temp_files');
356
+           }
357
+
358
+           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
359
+
360
+           $target = $this->folder->newFile($targetfile);
361
+           $target->putContent($fileContent);
362
+
363
+           // Get the cumulative files size of the uploaded files
364
+           $targetdir = "data/" . $this->userId . "/files/Pax_Fax/temp_files";
365
+           $fileSystemIterator = new FilesystemIterator($targetdir);
366
+
367
+           $dirfiles = [];
368
+           foreach ($fileSystemIterator as $fileInfo){
369
+                    $dirfiles[] = $fileInfo->getFilename();
370
+           }
371
+
372
+           $totalflsizeinit = 0;
373
+
374
+           foreach ($dirfiles as $key => $indfile) {
375
+
376
+                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
377
+                    $mbSize = $fileSizeinit / 1048576;
378
+                    $totalflsizeinit += $mbSize;
379
+           }
380
+
381
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
382
+
383
+           return $totalflsize;
384
+    }
385
+
386
+    /**
387
+     * @NoAdminRequired
388
+     */
389
+    public function removeupfile($userId, $removedfilename) {
390
+
391
+           $thisuserroot = $this->view->getRoot();
392
+
393
+           $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $removedfilename;
394
+           $removetmpfile = $this->filesystem->unlink($tempfile);
395
+
396
+           // Get the cumulative files size of the uploaded files
397
+           $targetdir = "data/" . $this->userId . "/files/Pax_Fax/temp_files";
398
+           $fileSystemIterator = new FilesystemIterator($targetdir);
399
+
400
+           $dirfiles = [];
401
+           foreach ($fileSystemIterator as $fileInfo){
402
+                    $dirfiles[] = $fileInfo->getFilename();
403
+           }
404
+
405
+           $totalflsizeinit = 0;
406
+
407
+           foreach ($dirfiles as $key => $indfile) {
408
+
409
+                    $fileSizeinit = $this->filesystem->filesize($thisuserroot . "/Pax_Fax/temp_files/" . $indfile);
410
+                    $mbSize = $fileSizeinit / 1048576;
411
+                    $totalflsizeinit += $mbSize;
412
+           }
413
+
414
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
415
+
416
+           return $totalflsize;
417
+    }
418
+
419
+    /**
420
+     * @NoAdminRequired
421
+     */
422
+    public function getpickedfile($userId, $pickedfilename) {
423
+
424
+           $thisuserroot = $this->view->getRoot();
425
+
426
+           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
427
+           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
428
+
429
+           $namesplit = explode(".", $pickedfilename);
430
+           $extension = end($namesplit);
431
+
432
+           if ($extension == "txt" || $extension == "html") {
433
+               $getpickedfile = $getfilecontent;
434
+           } elseif ($extension == "jpg") {
435
+               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
436
+           } elseif ($extension == "png") {
437
+               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
438
+           } else { $getpickedfile = ""; }
439
+
440
+           return $getpickedfile;
441
+    }
442
+
443
+    /**
444
+     * @NoAdminRequired
445
+     */
446
+    public function sendfax($userId, $uploadedtofax, $selectedcid, $toNumber) {
447
+
448
+           $tonumbertr = str_replace("+", "", $toNumber[0]);
449
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
450
+
451
+           $fromnumberdigits = str_replace("+", "", $selectedcid);
452
+           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
453
+
454
+           $countfaxfiles = count($uploadedtofax);
455
+
456
+           $openedfiles = [];
457
+
458
+           foreach ($uploadedtofax as $key => $flname) {
459
+
460
+                    if ($key == 0) { $firstflname = $flname; }
461
+
462
+                    $fileNamesec = array_reverse(explode(".", $flname));
463
+                    $filenameext = $fileNamesec[0];
464
+                    array_shift($fileNamesec);
465
+
466
+                    $fileName = implode("", $fileNamesec);
467
+
468
+                    $userroot = $this->view->getRoot();
469
+
470
+                    if ($countfaxfiles == 1) {
471
+                        $targetfile = $userroot . "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
472
+                    } else {
473
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
474
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
475
+                         }
476
+                         $targetfile = $userroot . "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
477
+                    }
478
+
479
+                    $fltgt = $userroot . "/Pax_Fax/temp_files/" . $flname;
480
+                    $fileContent = $this->filesystem->file_get_contents($fltgt);
481
+
482
+                    $target = $this->folder->newFile($targetfile);
483
+                    $target->putContent($fileContent);
484
+
485
+                    $removetmpfile = $this->filesystem->unlink($fltgt);
486
+
487
+                    if ($countfaxfiles == 1) {
488
+                        $openedfiles[] = fopen("data/" . $this->userId . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
489
+                    } else {
490
+                        $openedfiles[] = fopen("data/" . $this->userId . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
491
+                    }
492
+           }
493
+
494
+           $params = array(
495
+                           'to' => $toNumber,
496
+                           'file' => $openedfiles,
497
+                           'caller_id' => $selectedcid
498
+                     );
499
+
500
+           $apiMode = 'live';
501
+
502
+           $thisapicred = $this->service->getapicredentials($this->userId);
503
+
504
+           $apiKeys[$apiMode] = $thisapicred[0];
505
+           $apiSecrets[$apiMode] = $thisapicred[1];
506
+
507
+           $apiHost = 'https://api.phaxio.com/v2.1/';
508
+
509
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
510
+
511
+           try {
512
+                    $phaxioresultinit = $phaxio->sendFax($params);
513
+
514
+                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
515
+
516
+                    $statustoarr = $this->object_to_array($phaxioresultsec);
517
+
518
+                    $phaxiosuccess = $statustoarr['success'];
519
+
520
+                    $errortype = 'there are no errors';
521
+
522
+           } catch (InvalidRequestException $e) {
523
+                    $phaxiosuccess = 'false';
524
+                    $errortype = 'invalid request error';
525
+           } catch (AuthenticationException $e) {
526
+                    $phaxiosuccess = 'false';
527
+                    $errortype = 'authentication error';
528
+           } catch (APIConnectionException $e) {
529
+                    $phaxiosuccess = 'false';
530
+                    $errortype = 'API connection error';
531
+           } catch (RateLimitException $e) {
532
+                    $phaxiosuccess = 'false';
533
+                    $errortype = 'rate limit error';
534
+           } catch (NotFoundException $e) {
535
+                    $phaxiosuccess = 'false';
536
+                    $errortype = 'not found error';
537
+           } catch (GeneralException $e) {
538
+                    $phaxiosuccess = 'false';
539
+                    $errortype = 'undefined error';
540
+           }
541
+
542
+           $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
543
+
544
+           if ($phaxiosuccess != 'true') {
545
+
546
+                    foreach ($uploadedtofax as $key => $flname) {
547
+
548
+                        $fileNamesec = array_reverse(explode(".", $flname));
549
+                        $filenameext = $fileNamesec[0];
550
+                        array_shift($fileNamesec);
551
+
552
+                        $fileName = implode("", $fileNamesec);
553
+
554
+                        if ($countfaxfiles == 1) {
555
+                            $failedfl = $userroot . "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
556
+                            $newtargetfl =  $userroot . "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
557
+                        } else {
558
+                            $failedfl = $userroot . "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
559
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
560
+                            $newtargetfl = $userroot . "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
561
+                        }
562
+
563
+                        $fileContent = $this->filesystem->file_get_contents($failedfl);
564
+
565
+                        $targetact = $this->folder->newFile($newtargetfl);
566
+                        $targetact->putContent($fileContent);
567
+
568
+                        $removefailed = $this->filesystem->unlink($failedfl);
569
+                    }
570
+
571
+           }
572
+
573
+           return $phaxioresult;
574
+    }
575
+
576
+
577
+    /**
578
+     * @NoAdminRequired
579
+     */
580
+    public function getsettings($userId) {
581
+           return $this->service->getsettings($this->userId);
582
+    }
583
+
584
+
585
+    /**
586
+     * @NoAdminRequired
587
+     */
588
+    public function updatesettings($userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail) {
589
+           return $this->service->updatesettings($this->userId, $apiKey, $apiSecret, $webhookToken, $receiveUrl, $getNotification, $notificationEmail);
590
+    }
591
+
592
+}