Browse code

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

DoubleBastionAdmin authored on 09/09/2025 20:26:03
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,763 @@
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 OCA\PaxFax\Service\PaxfaxService;
29
+use OCP\IRequest;
30
+use OCP\AppFramework\Controller;
31
+use OCP\Files\File;
32
+use OCP\Files\Folder;
33
+use OCP\Files\IRootFolder;
34
+use OCP\AppFramework\App;
35
+use OCP\Files\NotPermittedException;
36
+use OCP\IConfig;
37
+use OCP\IUserSession;
38
+use OC\Files\Filesystem;
39
+use OC\Files\View;
40
+use OC\URLGenerator;
41
+use \ReflectionClass;
42
+use \FilesystemIterator;
43
+use Psr\Log\LoggerInterface;
44
+
45
+use Phaxio;
46
+use Phaxio\OperationResult;
47
+use Phaxio\Error\AuthenticationException;
48
+use Phaxio\Error\NotFoundException;
49
+use Phaxio\Error\InvalidRequestException;
50
+use Phaxio\Error\RateLimitException;
51
+use Phaxio\Error\APIConnectionException;
52
+use Phaxio\Error\GeneralException;
53
+
54
+
55
+class PaxfaxController extends Controller {
56
+
57
+    private $service;
58
+    private $config;
59
+    private $userId;
60
+    private $folder;
61
+    private $filesystem;
62
+    private $view;
63
+    private $userSession;
64
+    private $urlGenerator;
65
+    private $logger;
66
+    private $rootFolder;
67
+
68
+    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view, IUserSession $userSession, URLGenerator $urlGenerator, LoggerInterface $logger, IRootFolder $rootFolder) {
69
+
70
+           parent::__construct($appName, $request);
71
+
72
+           $this->service = $service;
73
+           $this->config = $config;
74
+           $this->userId = $userId;
75
+           $this->folder = $folder;
76
+           $this->filesystem = $filesystem;
77
+           $this->view = $view;
78
+           $this->userSession = $userSession;
79
+           $this->urlGenerator = $urlGenerator;
80
+           $this->logger = $logger;
81
+           $this->rootFolder = $rootFolder;
82
+    }
83
+
84
+    /**
85
+     * @NoAdminRequired
86
+     */
87
+    public function cleantempdir() {
88
+
89
+           // If the 'temp_files' folder doesn't exist create it
90
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
91
+               $this->folder->newFolder('Pax_Fax/temp_files');
92
+           }
93
+
94
+           $gettempdir = $this->folder->get("/Pax_Fax/temp_files");
95
+           $fileSystemIterator = $gettempdir->getDirectoryListing();
96
+
97
+           $dirfiles = [];
98
+           foreach ($fileSystemIterator as $fileInfo){
99
+                    $dirfiles[] = $fileInfo->getName();
100
+           }
101
+
102
+           foreach ($dirfiles as $key => $indfile) {
103
+                    $tempfile = "/Pax_Fax/temp_files/" . $indfile;
104
+                    $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
105
+                    $removetmpfile = $userFolder->get($tempfile)->delete();
106
+           }
107
+    }
108
+
109
+
110
+    /**
111
+     * @NoAdminRequired
112
+     */
113
+    public function object_to_array($obj) {
114
+
115
+           if(is_object($obj)) $obj = (array)$this->dismount($obj);
116
+           if(is_array($obj)) {
117
+              $new = array();
118
+              foreach($obj as $key => $val) {
119
+                      $new[$key] = $this->object_to_array($val);
120
+              }
121
+           }
122
+           else $new = $obj;
123
+           return $new;
124
+    }
125
+
126
+
127
+    /**
128
+     * @NoAdminRequired
129
+     */
130
+    public function dismount($object) {
131
+           $reflectionClass = new ReflectionClass(get_class($object));
132
+           $array = array();
133
+           foreach ($reflectionClass->getProperties() as $property) {
134
+                    $property->setAccessible(true);
135
+                    $array[$property->getName()] = $property->getValue($object);
136
+                    $property->setAccessible(false);
137
+           }
138
+           return $array;
139
+    }
140
+
141
+
142
+    /**
143
+     * @NoAdminRequired
144
+     */
145
+    public function getbalance() {
146
+
147
+           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
148
+
149
+           $sinchKeyId = $thisapicred[5];
150
+           $sinchKeySecret = $thisapicred[6];
151
+
152
+           if ($sinchKeyId && $sinchKeySecret) {
153
+
154
+               // Use Sinch
155
+               return "n/a";
156
+
157
+           } else {
158
+
159
+               // Use Phaxio
160
+               $apiMode = 'live';
161
+               $apiKeys[$apiMode] = $thisapicred[0];
162
+               $apiSecrets[$apiMode] = $thisapicred[1];
163
+
164
+               $apiHost = 'https://api.phaxio.com/v2.1/';
165
+
166
+               $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
167
+
168
+               try {
169
+                    // Get Phaxio account balance
170
+                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
171
+
172
+                    $balancetoarr = $this->object_to_array($phaxioresulttert);
173
+
174
+                    $phaxiobalance = $balancetoarr['data']['balance'];
175
+
176
+               } catch (InvalidRequestException $e) {
177
+                    $phaxiobalance = 'unknown';
178
+                    $phaxiosuccess = 'false';
179
+                    $errortype = 'invalid request error';
180
+               } catch (AuthenticationException $e) {
181
+                    $phaxiobalance = 'unknown';
182
+                    $phaxiosuccess = 'false';
183
+                    $errortype = 'authentication error';
184
+               } catch (APIConnectionException $e) {
185
+                    $phaxiobalance = 'unknown';
186
+                    $phaxiosuccess = 'false';
187
+                    $errortype = 'API connection error';
188
+               } catch (RateLimitException $e) {
189
+                    $phaxiobalance = 'unknown';
190
+                    $phaxiosuccess = 'false';
191
+                    $errortype = 'rate limit error';
192
+               } catch (NotFoundException $e) {
193
+                    $phaxiobalance = 'unknown';
194
+                    $phaxiosuccess = 'false';
195
+                    $errortype = 'not found error';
196
+               } catch (GeneralException $e) {
197
+                    $phaxiobalance = 'unknown';
198
+                    $phaxiosuccess = 'false';
199
+                    $errortype = 'undefined error';
200
+               }
201
+
202
+               return $phaxiobalance;
203
+           }
204
+    }
205
+
206
+
207
+    /**
208
+     * @NoAdminRequired
209
+     */
210
+    public function getfaxnumbers() {
211
+
212
+           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
213
+
214
+           $sinchKeyId = $thisapicred[5];
215
+           $sinchKeySecret = $thisapicred[6];
216
+
217
+           if ($sinchKeyId && $sinchKeySecret) {
218
+
219
+               // Use Sinch
220
+	       $sinchProjectId = $thisapicred[4];
221
+	       $sinchServiceId = $thisapicred[7];
222
+
223
+	       $nbquery = [ "pageSize" => "100", "page" => "string" ];
224
+
225
+	       $curl = curl_init();
226
+
227
+	       curl_setopt_array($curl, [
228
+		    CURLOPT_HTTPHEADER => [ "Authorization: Basic " . base64_encode("" . $sinchKeyId . ":" . $sinchKeySecret ."") ],
229
+                    CURLOPT_PORT => "",
230
+		    CURLOPT_URL => "https://fax.api.sinch.com/v3/projects/" . $sinchProjectId . "/services/" . $sinchServiceId . "/numbers?" . http_build_query($nbquery),
231
+		    CURLOPT_RETURNTRANSFER => true,
232
+		    CURLOPT_CUSTOMREQUEST => "GET",
233
+		]);
234
+
235
+		$responsesnb = curl_exec($curl);
236
+		$errorsnb = curl_error($curl);
237
+
238
+		curl_close($curl);
239
+
240
+		if ($errorsnb) { $this->logger->error('cURL Error #: '. $errorsnb . '.'); }
241
+
242
+                $sincharr = [];
243
+                $sinchnmbrs = json_decode($responsesnb, true);
244
+
245
+	        foreach ($sinchnmbrs['numbers'] as $snkey => $snvalue) {
246
+	                 if (is_array($snvalue)) {
247
+	                        foreach ($snvalue as $snkeysec => $snvaluesec) {
248
+	                               if ($snkeysec == 'phoneNumber') {
249
+	                                   $sincharr[] = $snvaluesec;
250
+	                               }
251
+	                        }
252
+	                 }
253
+	        }
254
+
255
+                $sinchnbdata = ["nblist" => $sincharr, "provider" => "sinch"];
256
+
257
+		return $sinchnbdata;
258
+
259
+           } else {
260
+
261
+                // Use Phaxio
262
+		$apiMode = 'live';
263
+		$apiKeys[$apiMode] = $thisapicred[0];
264
+		$apiSecrets[$apiMode] = $thisapicred[1];
265
+
266
+		$apiHost = 'https://api.phaxio.com/v2.1/';
267
+
268
+		$phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
269
+
270
+		try {
271
+	            // Get all Phaxio fax numbers
272
+	            $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
273
+
274
+	            $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
275
+
276
+	            $phaxioarr = [];
277
+
278
+	            foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
279
+	                   if (is_array($phvalue)) {
280
+	                         foreach ($phvalue as $phkeysec => $phvaluesec) {
281
+	                               if ($phkeysec == 'phone_number') {
282
+	                                   $phaxioarr[] = $phvaluesec;
283
+	                               }
284
+	                         }
285
+	                   }
286
+	            }
287
+
288
+		} catch (InvalidRequestException $e) {
289
+	            $phaxiobalance = 'unknown';
290
+	            $phaxiosuccess = 'false';
291
+	            $errortype = 'invalid request error';
292
+		} catch (AuthenticationException $e) {
293
+	            $phaxiobalance = 'unknown';
294
+	            $phaxiosuccess = 'false';
295
+	            $errortype = 'authentication error';
296
+		} catch (APIConnectionException $e) {
297
+	            $phaxiobalance = 'unknown';
298
+	            $phaxiosuccess = 'false';
299
+	            $errortype = 'API connection error';
300
+		} catch (RateLimitException $e) {
301
+	            $phaxiobalance = 'unknown';
302
+	            $phaxiosuccess = 'false';
303
+	            $errortype = 'rate limit error';
304
+		} catch (NotFoundException $e) {
305
+	            $phaxiobalance = 'unknown';
306
+	            $phaxiosuccess = 'false';
307
+	            $errortype = 'not found error';
308
+		} catch (GeneralException $e) {
309
+	            $phaxiobalance = 'unknown';
310
+	            $phaxiosuccess = 'false';
311
+	            $errortype = 'undefined error';
312
+		}
313
+
314
+                $phaxionbdata = ["nblist" => $phaxioarr, "provider" => "phaxio"];
315
+
316
+		return $phaxionbdata;
317
+           }
318
+    }
319
+
320
+
321
+    /**
322
+     * @NoAdminRequired
323
+     */
324
+    public function uploadfile($uploadfileforfax) {
325
+
326
+           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
327
+           $fileName = $_FILES['uploadfileforfax']['name'];
328
+           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
329
+           $fileSize = $fileSizeinit / 1048576;
330
+
331
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
332
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
333
+           }
334
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
335
+               $this->folder->newFolder('Pax_Fax/faxes_received');
336
+           }
337
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
338
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
339
+           }
340
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
341
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
342
+           }
343
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
344
+               $this->folder->newFolder('Pax_Fax/temp_files');
345
+           }
346
+
347
+           $targetfile = "/Pax_Fax/temp_files/" . $fileName;
348
+
349
+           $target = $this->folder->newFile($targetfile);
350
+           $target->putContent($fileContent);
351
+
352
+           // Get the cumulative files size of the uploaded files
353
+           $gettmpdir = $this->folder->get("/Pax_Fax/temp_files");
354
+           $fliterator = $gettmpdir->getDirectoryListing();
355
+
356
+
357
+           $dirfiles = [];
358
+           foreach ($fliterator as $fileInfo){
359
+                    $dirfiles[] = $fileInfo->getName();
360
+           }
361
+
362
+           $totalflsizeinit = 0;
363
+           foreach ($dirfiles as $key => $indfile) {
364
+                    $flsize = $this->folder->get("/Pax_Fax/temp_files/" . $indfile);
365
+                    $fileSizeinit = $flsize->getSize();
366
+                    $mbSize = $fileSizeinit / 1048576;
367
+                    $totalflsizeinit += $mbSize;
368
+           }
369
+
370
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
371
+
372
+           return $totalflsize;
373
+
374
+    }
375
+
376
+    /**
377
+     * @NoAdminRequired
378
+     */
379
+    public function pickfile($path) {
380
+
381
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
382
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
383
+           }
384
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
385
+               $this->folder->newFolder('Pax_Fax/faxes_received');
386
+           }
387
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
388
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
389
+           }
390
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
391
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
392
+           }
393
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
394
+               $this->folder->newFolder('Pax_Fax/temp_files');
395
+           }
396
+
397
+           $getfl = $this->folder->get($path);
398
+
399
+           $fileContentpk = $getfl->getContent();
400
+
401
+           $patharr = explode("/", $path);
402
+
403
+           $revarr = array_reverse($patharr);
404
+
405
+           $relflpath = "/Pax_Fax/temp_files/" . $revarr[0]; 
406
+
407
+           $target = $this->folder->newFile($relflpath);
408
+
409
+           $target->putContent($fileContentpk);
410
+
411
+           $gettmpdir = $this->folder->get("/Pax_Fax/temp_files");
412
+
413
+           $fileitrt = $gettmpdir->getDirectoryListing();
414
+
415
+           $dirfls = [];
416
+           foreach ($fileitrt as $fileInfo) {
417
+                    $dirfls[] = $fileInfo->getName();
418
+           }
419
+
420
+           $totalflsizeinitp = 0;
421
+
422
+           foreach ($dirfls as $keyp => $indfilep) {
423
+
424
+                    $flsizep = $this->folder->get("/Pax_Fax/temp_files/" . $indfilep);
425
+                    $fileSizeinitp = $flsizep->getSize();
426
+                    $mbSizep = $fileSizeinitp / 1048576;
427
+                    $totalflsizeinitp += $mbSizep;
428
+           }
429
+
430
+           $totalflsizep = number_format($totalflsizeinitp, 2) . ' MB';
431
+
432
+           return $totalflsizep;
433
+    }
434
+
435
+    /**
436
+     * @NoAdminRequired
437
+     */
438
+    public function removeupfile($removedfilename) {
439
+
440
+           $upfile = "/Pax_Fax/temp_files/" . $removedfilename;
441
+           $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
442
+           $removetmpfile = $userFolder->get($upfile)->delete();
443
+
444
+           $gettmpdir = $this->folder->get("/Pax_Fax/temp_files");
445
+           $fliterator = $gettmpdir->getDirectoryListing();
446
+
447
+           $dirfiles = [];
448
+           foreach ($fliterator as $fileInfo){
449
+                    $dirfiles[] = $fileInfo->getName();
450
+           }
451
+
452
+           $totalflsizeinit = 0;
453
+           foreach ($dirfiles as $key => $indfile) {
454
+                    $flsize = $this->folder->get("/Pax_Fax/temp_files/" . $indfile);
455
+                    $fileSizeinit = $flsize->getSize();
456
+                    $mbSize = $fileSizeinit / 1048576;
457
+                    $totalflsizeinit += $mbSize;
458
+           }
459
+
460
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
461
+
462
+           return $totalflsize;
463
+    }
464
+
465
+    /**
466
+     * @NoAdminRequired
467
+     */
468
+    public function prevpickedfile($pickedfilename) {
469
+
470
+           $getfl = $this->folder->get("/Pax_Fax/temp_files/" . $pickedfilename);
471
+           $getfilecontent = $getfl->getContent();
472
+
473
+           $namesplit = explode(".", $pickedfilename);
474
+           $extension = end($namesplit);
475
+
476
+           if ($extension == "txt" || $extension == "html") {
477
+               $getpickedfile = $getfilecontent;
478
+           } elseif ($extension == "jpg") {
479
+               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
480
+           } elseif ($extension == "png") {
481
+               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
482
+           } else { $getpickedfile = ""; }
483
+
484
+           return $getpickedfile;
485
+    }
486
+
487
+    /**
488
+     * @NoAdminRequired
489
+     */
490
+    public function sendfax($uploadedtofax, $selectedcid, $toNumber) {
491
+
492
+           $tonumbertr = str_replace("+", "", $toNumber[0]);
493
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
494
+           $fromnumberdigits = str_replace("+", "", $selectedcid);
495
+
496
+           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $selectedcid; } else { $fromnumber = ''; }
497
+
498
+           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
499
+
500
+           $sinchKeyId = $thisapicred[5];
501
+           $sinchKeySecret = $thisapicred[6];
502
+
503
+           if ($sinchKeyId && $sinchKeySecret) {
504
+
505
+               // Use Sinch
506
+	       $sinchProjectId = $thisapicred[4];
507
+
508
+               $countfaxfiles = count($uploadedtofax);
509
+
510
+               $openedfiles = [];
511
+               $crtuser = $this->userSession->getUser()->getUID();
512
+               $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
513
+
514
+               foreach ($uploadedtofax as $key => $flname) {
515
+
516
+                    if ($key == 0) { $firstflname = $flname; }
517
+
518
+                    $fileNamesec = array_reverse(explode(".", $flname));
519
+                    $filenameext = $fileNamesec[0];
520
+
521
+                    array_shift($fileNamesec);
522
+
523
+                    $fileName = implode("", $fileNamesec);
524
+
525
+                    if ($countfaxfiles == 1) {
526
+
527
+                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
528
+
529
+                    } else {
530
+
531
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
532
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
533
+                         }
534
+
535
+                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
536
+                    }
537
+
538
+		    $getfl = $this->folder->get("/Pax_Fax/temp_files/" . $flname);
539
+		    $fileContentsnd = $getfl->getContent();
540
+
541
+		    $target = $this->folder->newFile($targetfile);
542
+		    $target->putContent($fileContentsnd);
543
+
544
+		    $upfilefx = "/Pax_Fax/temp_files/" . $flname;
545
+		    $removetmpfile = $userFolder->get($upfilefx)->delete();
546
+
547
+                    if ($countfaxfiles == 1) {
548
+                        $gtflfx = $this->folder->get("/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
549
+                        $openedfiles[] = [ "file" => base64_encode($gtflfx->getContent()), "fileType" => $filenameext ];
550
+                    } else {
551
+                        $gtflfx = $this->folder->get("/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
552
+                        $openedfiles[] = [ "file" => base64_encode($gtflfx->getContent()), "fileType" => $filenameext ];
553
+                    }
554
+               }
555
+
556
+               $toNumbersnfin = implode(",", $toNumber);
557
+
558
+	       $datatosendsn = ["to" => $toNumbersnfin, "files" => $openedfiles];
559
+
560
+	       $postedparamsn = json_encode($datatosendsn);
561
+
562
+	       $chsn = curl_init();
563
+               curl_setopt($chsn, CURLOPT_URL, "https://" . $sinchKeyId . ":" . $sinchKeySecret . "@fax.api.sinch.com/v3/projects/" . $sinchProjectId . "/faxes");
564
+	       curl_setopt($chsn, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
565
+	       curl_setopt($chsn, CURLOPT_TIMEOUT, 300);
566
+	       curl_setopt($chsn, CURLOPT_RETURNTRANSFER, 1);
567
+	       curl_setopt($chsn, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
568
+	       curl_setopt($chsn, CURLOPT_POST, 1);
569
+	       curl_setopt($chsn, CURLOPT_POSTFIELDS, $postedparamsn);
570
+	       $resultsn = curl_exec($chsn);
571
+	       $statusCodesn = curl_getinfo($chsn, CURLINFO_HTTP_CODE);
572
+	       curl_close($chsn);
573
+
574
+	       $decresultsn = json_decode($resultsn, true);
575
+
576
+	       if (in_array($statusCodesn, [400, 401, 402, 404, 422, 429, 500, 800])) {
577
+                   $sinchsuccess = false;
578
+                   $sincherrortype = "Error: " . $decresultsn['code'] . " . Message: " . $decresultsn['message'];
579
+               } else { $sinchsuccess = true; $sincherrortype = ''; }
580
+
581
+               $sendingresult = [ 'success' => $sinchsuccess, 'errortype' => $sincherrortype ];
582
+
583
+               if ($sinchsuccess != true) {
584
+
585
+                    foreach ($uploadedtofax as $key => $flname) {
586
+
587
+                        $fileNamesec = array_reverse(explode(".", $flname));
588
+                        $filenameext = $fileNamesec[0];
589
+                        array_shift($fileNamesec);
590
+
591
+                        $fileName = implode("", $fileNamesec);
592
+
593
+                        if ($countfaxfiles == 1) {
594
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
595
+                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
596
+                        } else {
597
+
598
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
599
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
600
+                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
601
+                        }
602
+
603
+		        $getflsc = $this->folder->get($failedfl);
604
+		        $fileContentfld = $getflsc->getContent();
605
+
606
+		        $targetact = $this->folder->newFile($newtargetfl);
607
+		        $targetact->putContent($fileContentfld);
608
+
609
+		        $removetmpfile = $userFolder->get($failedfl)->delete();
610
+                    }
611
+
612
+                }
613
+
614
+                return $sendingresult;
615
+
616
+           } else {
617
+
618
+               // Use Phaxio
619
+               if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
620
+
621
+               $countfaxfiles = count($uploadedtofax);
622
+
623
+               $openedfiles = [];
624
+               $crtuser = $this->userSession->getUser()->getUID();
625
+               $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
626
+
627
+               foreach ($uploadedtofax as $key => $flname) {
628
+
629
+                    if ($key == 0) { $firstflname = $flname; }
630
+
631
+                    $fileNamesec = array_reverse(explode(".", $flname));
632
+                    $filenameext = $fileNamesec[0];
633
+                    array_shift($fileNamesec);
634
+
635
+                    $fileName = implode("", $fileNamesec);
636
+
637
+                    if ($countfaxfiles == 1) {
638
+                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
639
+                    } else {
640
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
641
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
642
+                         }
643
+
644
+                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
645
+                    }
646
+
647
+		    $getflsc = $this->folder->get("/Pax_Fax/temp_files/" . $flname);
648
+		    $fileContentsnd = $getflsc->getContent();
649
+
650
+                    $target = $this->folder->newFile($targetfile);
651
+                    $target->putContent($fileContentsnd);
652
+
653
+                    $fltodel = "/Pax_Fax/temp_files/" . $flname;
654
+		    $removetmpfile = $userFolder->get($fltodel)->delete();
655
+
656
+                    if ($countfaxfiles == 1) {
657
+                        $getflopen = $userFolder->get("/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
658
+                        $openedfiles[] = $getflopen->fopen('r');
659
+                    } else {
660
+                        $getflopen = $userFolder->get("/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
661
+                        $openedfiles[] = $getflopen->fopen('r');
662
+                    }
663
+               }
664
+
665
+               $params = array(
666
+                           'to' => $toNumber,
667
+                           'file' => $openedfiles,
668
+                           'caller_id' => $selectedcid
669
+                         );
670
+
671
+               $apiMode = 'live';
672
+
673
+               $apiKeys[$apiMode] = $thisapicred[0];
674
+               $apiSecrets[$apiMode] = $thisapicred[1];
675
+
676
+               $apiHost = 'https://api.phaxio.com/v2.1/';
677
+
678
+               $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
679
+
680
+               try {
681
+                    $phaxioresultinit = $phaxio->sendFax($params);
682
+
683
+                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
684
+
685
+                    $statustoarr = $this->object_to_array($phaxioresultsec);
686
+
687
+                    $phaxiosuccess = $statustoarr['success'];
688
+
689
+                    $errortype = 'there are no errors';
690
+
691
+               } catch (InvalidRequestException $e) {
692
+                    $phaxiosuccess = 'false';
693
+                    $errortype = 'invalid request error';
694
+               } catch (AuthenticationException $e) {
695
+                    $phaxiosuccess = 'false';
696
+                    $errortype = 'authentication error';
697
+               } catch (APIConnectionException $e) {
698
+                    $phaxiosuccess = 'false';
699
+                    $errortype = 'API connection error';
700
+               } catch (RateLimitException $e) {
701
+                    $phaxiosuccess = 'false';
702
+                    $errortype = 'rate limit error';
703
+               } catch (NotFoundException $e) {
704
+                    $phaxiosuccess = 'false';
705
+                    $errortype = 'not found error';
706
+               } catch (GeneralException $e) {
707
+                    $phaxiosuccess = 'false';
708
+                    $errortype = 'undefined error';
709
+               }
710
+
711
+               $sendingresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
712
+
713
+               if ($phaxiosuccess != 'true') {
714
+
715
+                    foreach ($uploadedtofax as $key => $flname) {
716
+
717
+                        $fileNamesec = array_reverse(explode(".", $flname));
718
+                        $filenameext = $fileNamesec[0];
719
+                        array_shift($fileNamesec);
720
+
721
+                        $fileName = implode("", $fileNamesec);
722
+
723
+                        if ($countfaxfiles == 1) {
724
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
725
+                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
726
+                        } else {
727
+
728
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
729
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
730
+                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
731
+                        }
732
+
733
+		        $getflthrd = $this->folder->get($failedfl);
734
+		        $fileContentfld = $getflthrd->getContent();
735
+
736
+                        $targetact = $this->folder->newFile($targetfile);
737
+                        $targetact->putContent($fileContentfld);
738
+
739
+		        $removefailed = $userFolder->get($failedfl)->delete();
740
+                    }
741
+               }
742
+
743
+               return $sendingresult;
744
+           }
745
+    }
746
+
747
+
748
+    /**
749
+     * @NoAdminRequired
750
+     */
751
+    public function getsettings() {
752
+           return $this->service->getsettings($this->userSession->getUser()->getUID());
753
+    }
754
+
755
+
756
+    /**
757
+     * @NoAdminRequired
758
+     */
759
+    public function updatesettings($apiKey, $apiSecret, $webhookToken, $receiveUrl, $sinchProjectId, $sinchKeyId, $sinchKeySecret, $sinchServiceId, $sinchCallbackUrl, $getNotification, $notificationEmail) {
760
+           return $this->service->updatesettings($this->userSession->getUser()->getUID(), $apiKey, $apiSecret, $webhookToken, $receiveUrl, $sinchProjectId, $sinchKeyId, $sinchKeySecret, $sinchServiceId, $sinchCallbackUrl, $getNotification, $notificationEmail);
761
+    }
762
+
763
+}
Browse code

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

DoubleBastionAdmin authored on 09/09/2025 20:23:20
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,765 +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 OCA\PaxFax\Service\PaxfaxService;
29
-use OCP\IRequest;
30
-use OCP\AppFramework\Controller;
31
-use OCP\Files\File;
32
-use OCP\Files\Folder;
33
-use OCP\Files\IRootFolder;
34
-use OCP\AppFramework\App;
35
-use OCP\Files\NotPermittedException;
36
-use OCP\IConfig;
37
-use OCP\IUserSession;
38
-use OC\Files\Filesystem;
39
-use OC\Files\View;
40
-use OC\URLGenerator;
41
-use \ReflectionClass;
42
-use \FilesystemIterator;
43
-use Psr\Log\LoggerInterface;
44
-
45
-use Phaxio;
46
-use Phaxio\OperationResult;
47
-use Phaxio\Error\AuthenticationException;
48
-use Phaxio\Error\NotFoundException;
49
-use Phaxio\Error\InvalidRequestException;
50
-use Phaxio\Error\RateLimitException;
51
-use Phaxio\Error\APIConnectionException;
52
-use Phaxio\Error\GeneralException;
53
-
54
-
55
-class PaxfaxController extends Controller {
56
-
57
-    private $service;
58
-    private $config;
59
-    private $userId;
60
-    private $folder;
61
-    private $filesystem;
62
-    private $view;
63
-    private $userSession;
64
-    private $urlGenerator;
65
-    private $logger;
66
-    private $rootFolder;
67
-
68
-    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view, IUserSession $userSession, URLGenerator $urlGenerator, LoggerInterface $logger, IRootFolder $rootFolder) {
69
-
70
-           parent::__construct($appName, $request);
71
-
72
-           $this->service = $service;
73
-           $this->config = $config;
74
-           $this->userId = $userId;
75
-           $this->folder = $folder;
76
-           $this->filesystem = $filesystem;
77
-           $this->view = $view;
78
-           $this->userSession = $userSession;
79
-           $this->urlGenerator = $urlGenerator;
80
-           $this->logger = $logger;
81
-           $this->rootFolder = $rootFolder;
82
-    }
83
-
84
-    /**
85
-     * @NoAdminRequired
86
-     */
87
-    public function cleantempdir() {
88
-
89
-           // If the 'temp_files' folder doesn't exist create it
90
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
91
-               $this->folder->newFolder('Pax_Fax/temp_files');
92
-           }
93
-
94
-           $gettempdir = $this->folder->get("/Pax_Fax/temp_files");
95
-           $fileSystemIterator = $gettempdir->getDirectoryListing();
96
-
97
-           $dirfiles = [];
98
-           foreach ($fileSystemIterator as $fileInfo){
99
-                    $dirfiles[] = $fileInfo->getName();
100
-           }
101
-
102
-           foreach ($dirfiles as $key => $indfile) {
103
-                    $tempfile = "/Pax_Fax/temp_files/" . $indfile;
104
-                    $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
105
-                    $removetmpfile = $userFolder->get($tempfile)->delete();
106
-           }
107
-    }
108
-
109
-
110
-    /**
111
-     * @NoAdminRequired
112
-     */
113
-    public function object_to_array($obj) {
114
-
115
-           if(is_object($obj)) $obj = (array)$this->dismount($obj);
116
-           if(is_array($obj)) {
117
-              $new = array();
118
-              foreach($obj as $key => $val) {
119
-                      $new[$key] = $this->object_to_array($val);
120
-              }
121
-           }
122
-           else $new = $obj;
123
-           return $new;
124
-    }
125
-
126
-
127
-    /**
128
-     * @NoAdminRequired
129
-     */
130
-    public function dismount($object) {
131
-           $reflectionClass = new ReflectionClass(get_class($object));
132
-           $array = array();
133
-           foreach ($reflectionClass->getProperties() as $property) {
134
-                    $property->setAccessible(true);
135
-                    $array[$property->getName()] = $property->getValue($object);
136
-                    $property->setAccessible(false);
137
-           }
138
-           return $array;
139
-    }
140
-
141
-
142
-    /**
143
-     * @NoAdminRequired
144
-     */
145
-    public function getbalance() {
146
-
147
-           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
148
-
149
-           $sinchKeyId = $thisapicred[5];
150
-           $sinchKeySecret = $thisapicred[6];
151
-
152
-           if ($sinchKeyId && $sinchKeySecret) {
153
-
154
-               // Use Sinch
155
-               return "n/a";
156
-
157
-           } else {
158
-
159
-               // Use Phaxio
160
-               $apiMode = 'live';
161
-               $apiKeys[$apiMode] = $thisapicred[0];
162
-               $apiSecrets[$apiMode] = $thisapicred[1];
163
-
164
-               $apiHost = 'https://api.phaxio.com/v2.1/';
165
-
166
-               $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
167
-
168
-               try {
169
-                    // Get Phaxio account balance
170
-                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
171
-
172
-                    $balancetoarr = $this->object_to_array($phaxioresulttert);
173
-
174
-                    $phaxiobalance = $balancetoarr['data']['balance'];
175
-
176
-               } catch (InvalidRequestException $e) {
177
-                    $phaxiobalance = 'unknown';
178
-                    $phaxiosuccess = 'false';
179
-                    $errortype = 'invalid request error';
180
-               } catch (AuthenticationException $e) {
181
-                    $phaxiobalance = 'unknown';
182
-                    $phaxiosuccess = 'false';
183
-                    $errortype = 'authentication error';
184
-               } catch (APIConnectionException $e) {
185
-                    $phaxiobalance = 'unknown';
186
-                    $phaxiosuccess = 'false';
187
-                    $errortype = 'API connection error';
188
-               } catch (RateLimitException $e) {
189
-                    $phaxiobalance = 'unknown';
190
-                    $phaxiosuccess = 'false';
191
-                    $errortype = 'rate limit error';
192
-               } catch (NotFoundException $e) {
193
-                    $phaxiobalance = 'unknown';
194
-                    $phaxiosuccess = 'false';
195
-                    $errortype = 'not found error';
196
-               } catch (GeneralException $e) {
197
-                    $phaxiobalance = 'unknown';
198
-                    $phaxiosuccess = 'false';
199
-                    $errortype = 'undefined error';
200
-               }
201
-
202
-               return $phaxiobalance;
203
-           }
204
-    }
205
-
206
-
207
-    /**
208
-     * @NoAdminRequired
209
-     */
210
-    public function getfaxnumbers() {
211
-
212
-           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
213
-
214
-           $sinchKeyId = $thisapicred[5];
215
-           $sinchKeySecret = $thisapicred[6];
216
-
217
-           if ($sinchKeyId && $sinchKeySecret) {
218
-
219
-               // Use Sinch
220
-	       $sinchProjectId = $thisapicred[4];
221
-	       $sinchServiceId = $thisapicred[7];
222
-
223
-	       $nbquery = [ "pageSize" => "100", "page" => "string" ];
224
-
225
-	       $curl = curl_init();
226
-
227
-	       curl_setopt_array($curl, [
228
-		    CURLOPT_HTTPHEADER => [ "Authorization: Basic " . base64_encode("" . $sinchKeyId . ":" . $sinchKeySecret ."") ],
229
-                    CURLOPT_PORT => "",
230
-		    CURLOPT_URL => "https://fax.api.sinch.com/v3/projects/" . $sinchProjectId . "/services/" . $sinchServiceId . "/numbers?" . http_build_query($nbquery),
231
-		    CURLOPT_RETURNTRANSFER => true,
232
-		    CURLOPT_CUSTOMREQUEST => "GET",
233
-		]);
234
-
235
-		$responsesnb = curl_exec($curl);
236
-		$errorsnb = curl_error($curl);
237
-
238
-		curl_close($curl);
239
-
240
-		if ($errorsnb) { $this->logger->error('cURL Error #: '. $errorsnb . '.'); }
241
-
242
-                $sincharr = [];
243
-                $sinchnmbrs = json_decode($responsesnb, true);
244
-
245
-	        foreach ($sinchnmbrs['numbers'] as $snkey => $snvalue) {
246
-	                 if (is_array($snvalue)) {
247
-	                        foreach ($snvalue as $snkeysec => $snvaluesec) {
248
-	                               if ($snkeysec == 'phoneNumber') {
249
-	                                   $sincharr[] = $snvaluesec;
250
-	                               }
251
-	                        }
252
-	                 }
253
-	        }
254
-
255
-                $sinchnbdata = ["nblist" => $sincharr, "provider" => "sinch"];
256
-
257
-		return $sinchnbdata;
258
-
259
-           } else {
260
-
261
-                // Use Phaxio
262
-		$apiMode = 'live';
263
-		$apiKeys[$apiMode] = $thisapicred[0];
264
-		$apiSecrets[$apiMode] = $thisapicred[1];
265
-
266
-		$apiHost = 'https://api.phaxio.com/v2.1/';
267
-
268
-		$phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
269
-
270
-		try {
271
-	            // Get all Phaxio fax numbers
272
-	            $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
273
-
274
-	            $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
275
-
276
-	            $phaxioarr = [];
277
-
278
-	            foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
279
-	                   if (is_array($phvalue)) {
280
-	                         foreach ($phvalue as $phkeysec => $phvaluesec) {
281
-	                               if ($phkeysec == 'phone_number') {
282
-	                                   $phaxioarr[] = $phvaluesec;
283
-	                               }
284
-	                         }
285
-	                   }
286
-	            }
287
-
288
-		} catch (InvalidRequestException $e) {
289
-	            $phaxiobalance = 'unknown';
290
-	            $phaxiosuccess = 'false';
291
-	            $errortype = 'invalid request error';
292
-		} catch (AuthenticationException $e) {
293
-	            $phaxiobalance = 'unknown';
294
-	            $phaxiosuccess = 'false';
295
-	            $errortype = 'authentication error';
296
-		} catch (APIConnectionException $e) {
297
-	            $phaxiobalance = 'unknown';
298
-	            $phaxiosuccess = 'false';
299
-	            $errortype = 'API connection error';
300
-		} catch (RateLimitException $e) {
301
-	            $phaxiobalance = 'unknown';
302
-	            $phaxiosuccess = 'false';
303
-	            $errortype = 'rate limit error';
304
-		} catch (NotFoundException $e) {
305
-	            $phaxiobalance = 'unknown';
306
-	            $phaxiosuccess = 'false';
307
-	            $errortype = 'not found error';
308
-		} catch (GeneralException $e) {
309
-	            $phaxiobalance = 'unknown';
310
-	            $phaxiosuccess = 'false';
311
-	            $errortype = 'undefined error';
312
-		}
313
-
314
-                $phaxionbdata = ["nblist" => $phaxioarr, "provider" => "phaxio"];
315
-
316
-		return $phaxionbdata;
317
-           }
318
-    }
319
-
320
-
321
-    /**
322
-     * @NoAdminRequired
323
-     */
324
-    public function uploadfile($uploadfileforfax) {
325
-
326
-           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
327
-           $fileName = $_FILES['uploadfileforfax']['name'];
328
-           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
329
-           $fileSize = $fileSizeinit / 1048576;
330
-
331
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
332
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
333
-           }
334
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
335
-               $this->folder->newFolder('Pax_Fax/faxes_received');
336
-           }
337
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
338
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
339
-           }
340
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
341
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
342
-           }
343
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
344
-               $this->folder->newFolder('Pax_Fax/temp_files');
345
-           }
346
-
347
-           $targetfile = "/Pax_Fax/temp_files/" . $fileName;
348
-
349
-           $target = $this->folder->newFile($targetfile);
350
-           $target->putContent($fileContent);
351
-
352
-           // Get the cumulative files size of the uploaded files
353
-           $gettmpdir = $this->folder->get("/Pax_Fax/temp_files");
354
-           $fliterator = $gettmpdir->getDirectoryListing();
355
-
356
-
357
-           $dirfiles = [];
358
-           foreach ($fliterator as $fileInfo){
359
-                    $dirfiles[] = $fileInfo->getName();
360
-           }
361
-
362
-           $totalflsizeinit = 0;
363
-           foreach ($dirfiles as $key => $indfile) {
364
-                    $flsize = $this->folder->get("/Pax_Fax/temp_files/" . $indfile);
365
-                    $fileSizeinit = $flsize->getSize();
366
-                    $mbSize = $fileSizeinit / 1048576;
367
-                    $totalflsizeinit += $mbSize;
368
-           }
369
-
370
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
371
-
372
-           return $totalflsize;
373
-
374
-    }
375
-
376
-    /**
377
-     * @NoAdminRequired
378
-     */
379
-    public function pickfile($path) {
380
-
381
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
382
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
383
-           }
384
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
385
-               $this->folder->newFolder('Pax_Fax/faxes_received');
386
-           }
387
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
388
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
389
-           }
390
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
391
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
392
-           }
393
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
394
-               $this->folder->newFolder('Pax_Fax/temp_files');
395
-           }
396
-
397
-           $getfl = $this->folder->get($path);
398
-
399
-           $fileContentpk = $getfl->getContent();
400
-
401
-           $patharr = explode("/", $path);
402
-
403
-           $revarr = array_reverse($patharr);
404
-
405
-           $relflpath = "/Pax_Fax/temp_files/" . $revarr[0]; 
406
-
407
-           $target = $this->folder->newFile($relflpath);
408
-
409
-           $target->putContent($fileContentpk);
410
-
411
-           $gettmpdir = $this->folder->get("/Pax_Fax/temp_files");
412
-
413
-           $fileitrt = $gettmpdir->getDirectoryListing();
414
-
415
-           $dirfls = [];
416
-           foreach ($fileitrt as $fileInfo) {
417
-                    $dirfls[] = $fileInfo->getName();
418
-           }
419
-
420
-           $totalflsizeinitp = 0;
421
-
422
-           foreach ($dirfls as $keyp => $indfilep) {
423
-
424
-                    $flsizep = $this->folder->get("/Pax_Fax/temp_files/" . $indfilep);
425
-                    $fileSizeinitp = $flsizep->getSize();
426
-                    $mbSizep = $fileSizeinitp / 1048576;
427
-                    $totalflsizeinitp += $mbSizep;
428
-           }
429
-
430
-           $totalflsizep = number_format($totalflsizeinitp, 2) . ' MB';
431
-
432
-           return $totalflsizep;
433
-    }
434
-
435
-    /**
436
-     * @NoAdminRequired
437
-     */
438
-    public function removeupfile($removedfilename) {
439
-
440
-           $upfile = "/Pax_Fax/temp_files/" . $removedfilename;
441
-           $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
442
-           $removetmpfile = $userFolder->get($upfile)->delete();
443
-
444
-           $gettmpdir = $this->folder->get("/Pax_Fax/temp_files");
445
-           $fliterator = $gettmpdir->getDirectoryListing();
446
-
447
-           $dirfiles = [];
448
-           foreach ($fliterator as $fileInfo){
449
-                    $dirfiles[] = $fileInfo->getName();
450
-           }
451
-
452
-           $totalflsizeinit = 0;
453
-           foreach ($dirfiles as $key => $indfile) {
454
-                    $flsize = $this->folder->get("/Pax_Fax/temp_files/" . $indfile);
455
-                    $fileSizeinit = $flsize->getSize();
456
-                    $mbSize = $fileSizeinit / 1048576;
457
-                    $totalflsizeinit += $mbSize;
458
-           }
459
-
460
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
461
-
462
-           return $totalflsize;
463
-    }
464
-
465
-    /**
466
-     * @NoAdminRequired
467
-     */
468
-    public function prevpickedfile($pickedfilename) {
469
-
470
-           $getfl = $this->folder->get("/Pax_Fax/temp_files/" . $pickedfilename);
471
-           $getfilecontent = $getfl->getContent();
472
-
473
-           $namesplit = explode(".", $pickedfilename);
474
-           $extension = end($namesplit);
475
-
476
-           if ($extension == "txt" || $extension == "html") {
477
-               $getpickedfile = $getfilecontent;
478
-           } elseif ($extension == "jpg") {
479
-               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
480
-           } elseif ($extension == "png") {
481
-               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
482
-           } else { $getpickedfile = ""; }
483
-
484
-           return $getpickedfile;
485
-    }
486
-
487
-    /**
488
-     * @NoAdminRequired
489
-     */
490
-    public function sendfax($uploadedtofax, $selectedcid, $toNumber) {
491
-
492
-           $tonumbertr = str_replace("+", "", $toNumber[0]);
493
-           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
494
-           $fromnumberdigits = str_replace("+", "", $selectedcid);
495
-
496
-           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $selectedcid; } else { $fromnumber = ''; }
497
-
498
-           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
499
-
500
-           $sinchKeyId = $thisapicred[5];
501
-           $sinchKeySecret = $thisapicred[6];
502
-
503
-           if ($sinchKeyId && $sinchKeySecret) {
504
-
505
-               // Use Sinch
506
-	       $sinchProjectId = $thisapicred[4];
507
-
508
-               $countfaxfiles = count($uploadedtofax);
509
-
510
-               $openedfiles = [];
511
-               $crtuser = $this->userSession->getUser()->getUID();
512
-               $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
513
-
514
-               foreach ($uploadedtofax as $key => $flname) {
515
-
516
-                    if ($key == 0) { $firstflname = $flname; }
517
-
518
-                    $fileNamesec = array_reverse(explode(".", $flname));
519
-                    $filenameext = $fileNamesec[0];
520
-
521
-                    array_shift($fileNamesec);
522
-
523
-                    $fileName = implode("", $fileNamesec);
524
-
525
-                    if ($countfaxfiles == 1) {
526
-
527
-                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
528
-
529
-                    } else {
530
-
531
-                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
532
-                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
533
-                         }
534
-
535
-                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
536
-                    }
537
-
538
-		    $getfl = $this->folder->get("/Pax_Fax/temp_files/" . $flname);
539
-		    $fileContentsnd = $getfl->getContent();
540
-
541
-		    $target = $this->folder->newFile($targetfile);
542
-		    $target->putContent($fileContentsnd);
543
-
544
-		    $upfilefx = "/Pax_Fax/temp_files/" . $flname;
545
-		    $removetmpfile = $userFolder->get($upfilefx)->delete();
546
-
547
-                    if ($countfaxfiles == 1) {
548
-                        $gtflfx = $this->folder->get("/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
549
-                        $openedfiles[] = [ "file" => base64_encode($gtflfx->getContent()), "fileType" => $filenameext ];
550
-                    } else {
551
-                        $gtflfx = $this->folder->get("/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
552
-                        $openedfiles[] = [ "file" => base64_encode($gtflfx->getContent()), "fileType" => $filenameext ];
553
-                    }
554
-               }
555
-
556
-               $toNumbersn = [];
557
-               foreach ($toNumber as $nbkey => $nbvalue) { $toNumbersn[] = "+" . $nbvalue; }
558
-               $toNumbersnfin = implode(";", $toNumbersn);
559
-
560
-	       $datatosendsn = ["to" => $toNumbersnfin, "files" => $openedfiles];
561
-
562
-	       $postedparamsn = json_encode($datatosendsn);
563
-
564
-	       $chsn = curl_init();
565
-               curl_setopt($chsn, CURLOPT_URL, "https://" . $sinchKeyId . ":" . $sinchKeySecret . "@fax.api.sinch.com/v3/projects/" . $sinchProjectId . "/faxes");
566
-	       curl_setopt($chsn, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
567
-	       curl_setopt($chsn, CURLOPT_TIMEOUT, 300);
568
-	       curl_setopt($chsn, CURLOPT_RETURNTRANSFER, 1);
569
-	       curl_setopt($chsn, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
570
-	       curl_setopt($chsn, CURLOPT_POST, 1);
571
-	       curl_setopt($chsn, CURLOPT_POSTFIELDS, $postedparamsn);
572
-	       $resultsn = curl_exec($chsn);
573
-	       $statusCodesn = curl_getinfo($chsn, CURLINFO_HTTP_CODE);
574
-	       curl_close($chsn);
575
-
576
-	       $decresultsn = json_decode($resultsn, true);
577
-
578
-	       if (in_array($statusCodesn, [400, 401, 402, 404, 422, 429, 500, 800])) {
579
-                   $sinchsuccess = false;
580
-                   $sincherrortype = "Error: " . $decresultsn['code'] . " . Message: " . $decresultsn['message'];
581
-               } else { $sinchsuccess = true; $sincherrortype = ''; }
582
-
583
-               $sinchresult = [ 'success' => $sinchsuccess, 'errortype' => $sincherrortype ];
584
-
585
-               if ($sinchsuccess != true) {
586
-
587
-                    foreach ($uploadedtofax as $key => $flname) {
588
-
589
-                        $fileNamesec = array_reverse(explode(".", $flname));
590
-                        $filenameext = $fileNamesec[0];
591
-                        array_shift($fileNamesec);
592
-
593
-                        $fileName = implode("", $fileNamesec);
594
-
595
-                        if ($countfaxfiles == 1) {
596
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
597
-                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
598
-                        } else {
599
-
600
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
601
-                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
602
-                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
603
-                        }
604
-
605
-		        $getflsc = $this->folder->get($failedfl);
606
-		        $fileContentfld = $getflsc->getContent();
607
-
608
-		        $targetact = $this->folder->newFile($newtargetfl);
609
-		        $targetact->putContent($fileContentfld);
610
-
611
-		        $removetmpfile = $userFolder->get($failedfl)->delete();
612
-                    }
613
-
614
-                }
615
-
616
-                return $sinchresult;
617
-
618
-           } else {
619
-
620
-               // Use Phaxio
621
-               if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
622
-
623
-               $countfaxfiles = count($uploadedtofax);
624
-
625
-               $openedfiles = [];
626
-               $crtuser = $this->userSession->getUser()->getUID();
627
-               $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
628
-
629
-               foreach ($uploadedtofax as $key => $flname) {
630
-
631
-                    if ($key == 0) { $firstflname = $flname; }
632
-
633
-                    $fileNamesec = array_reverse(explode(".", $flname));
634
-                    $filenameext = $fileNamesec[0];
635
-                    array_shift($fileNamesec);
636
-
637
-                    $fileName = implode("", $fileNamesec);
638
-
639
-                    if ($countfaxfiles == 1) {
640
-                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
641
-                    } else {
642
-                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
643
-                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
644
-                         }
645
-
646
-                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
647
-                    }
648
-
649
-		    $getflsc = $this->folder->get("/Pax_Fax/temp_files/" . $flname);
650
-		    $fileContentsnd = $getflsc->getContent();
651
-
652
-                    $target = $this->folder->newFile($targetfile);
653
-                    $target->putContent($fileContentsnd);
654
-
655
-                    $fltodel = "/Pax_Fax/temp_files/" . $flname;
656
-		    $removetmpfile = $userFolder->get($fltodel)->delete();
657
-
658
-                    if ($countfaxfiles == 1) {
659
-                        $getflopen = $userFolder->get("/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
660
-                        $openedfiles[] = $getflopen->fopen('r');
661
-                    } else {
662
-                        $getflopen = $userFolder->get("/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
663
-                        $openedfiles[] = $getflopen->fopen('r');
664
-                    }
665
-               }
666
-
667
-               $params = array(
668
-                           'to' => $toNumber,
669
-                           'file' => $openedfiles,
670
-                           'caller_id' => $selectedcid
671
-                         );
672
-
673
-               $apiMode = 'live';
674
-
675
-               $apiKeys[$apiMode] = $thisapicred[0];
676
-               $apiSecrets[$apiMode] = $thisapicred[1];
677
-
678
-               $apiHost = 'https://api.phaxio.com/v2.1/';
679
-
680
-               $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
681
-
682
-               try {
683
-                    $phaxioresultinit = $phaxio->sendFax($params);
684
-
685
-                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
686
-
687
-                    $statustoarr = $this->object_to_array($phaxioresultsec);
688
-
689
-                    $phaxiosuccess = $statustoarr['success'];
690
-
691
-                    $errortype = 'there are no errors';
692
-
693
-               } catch (InvalidRequestException $e) {
694
-                    $phaxiosuccess = 'false';
695
-                    $errortype = 'invalid request error';
696
-               } catch (AuthenticationException $e) {
697
-                    $phaxiosuccess = 'false';
698
-                    $errortype = 'authentication error';
699
-               } catch (APIConnectionException $e) {
700
-                    $phaxiosuccess = 'false';
701
-                    $errortype = 'API connection error';
702
-               } catch (RateLimitException $e) {
703
-                    $phaxiosuccess = 'false';
704
-                    $errortype = 'rate limit error';
705
-               } catch (NotFoundException $e) {
706
-                    $phaxiosuccess = 'false';
707
-                    $errortype = 'not found error';
708
-               } catch (GeneralException $e) {
709
-                    $phaxiosuccess = 'false';
710
-                    $errortype = 'undefined error';
711
-               }
712
-
713
-               $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
714
-
715
-               if ($phaxiosuccess != 'true') {
716
-
717
-                    foreach ($uploadedtofax as $key => $flname) {
718
-
719
-                        $fileNamesec = array_reverse(explode(".", $flname));
720
-                        $filenameext = $fileNamesec[0];
721
-                        array_shift($fileNamesec);
722
-
723
-                        $fileName = implode("", $fileNamesec);
724
-
725
-                        if ($countfaxfiles == 1) {
726
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
727
-                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
728
-                        } else {
729
-
730
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
731
-                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
732
-                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
733
-                        }
734
-
735
-		        $getflthrd = $this->folder->get($failedfl);
736
-		        $fileContentfld = $getflthrd->getContent();
737
-
738
-                        $targetact = $this->folder->newFile($targetfile);
739
-                        $targetact->putContent($fileContentfld);
740
-
741
-		        $removefailed = $userFolder->get($failedfl)->delete();
742
-                    }
743
-               }
744
-
745
-               return $phaxioresult;
746
-           }
747
-    }
748
-
749
-
750
-    /**
751
-     * @NoAdminRequired
752
-     */
753
-    public function getsettings() {
754
-           return $this->service->getsettings($this->userSession->getUser()->getUID());
755
-    }
756
-
757
-
758
-    /**
759
-     * @NoAdminRequired
760
-     */
761
-    public function updatesettings($apiKey, $apiSecret, $webhookToken, $receiveUrl, $sinchProjectId, $sinchKeyId, $sinchKeySecret, $sinchServiceId, $sinchCallbackUrl, $getNotification, $notificationEmail) {
762
-           return $this->service->updatesettings($this->userSession->getUser()->getUID(), $apiKey, $apiSecret, $webhookToken, $receiveUrl, $sinchProjectId, $sinchKeyId, $sinchKeySecret, $sinchServiceId, $sinchCallbackUrl, $getNotification, $notificationEmail);
763
-    }
764
-
765
-}
Browse code

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

DoubleBastionAdmin authored on 02/06/2025 20:11:59
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,765 @@
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 OCA\PaxFax\Service\PaxfaxService;
29
+use OCP\IRequest;
30
+use OCP\AppFramework\Controller;
31
+use OCP\Files\File;
32
+use OCP\Files\Folder;
33
+use OCP\Files\IRootFolder;
34
+use OCP\AppFramework\App;
35
+use OCP\Files\NotPermittedException;
36
+use OCP\IConfig;
37
+use OCP\IUserSession;
38
+use OC\Files\Filesystem;
39
+use OC\Files\View;
40
+use OC\URLGenerator;
41
+use \ReflectionClass;
42
+use \FilesystemIterator;
43
+use Psr\Log\LoggerInterface;
44
+
45
+use Phaxio;
46
+use Phaxio\OperationResult;
47
+use Phaxio\Error\AuthenticationException;
48
+use Phaxio\Error\NotFoundException;
49
+use Phaxio\Error\InvalidRequestException;
50
+use Phaxio\Error\RateLimitException;
51
+use Phaxio\Error\APIConnectionException;
52
+use Phaxio\Error\GeneralException;
53
+
54
+
55
+class PaxfaxController extends Controller {
56
+
57
+    private $service;
58
+    private $config;
59
+    private $userId;
60
+    private $folder;
61
+    private $filesystem;
62
+    private $view;
63
+    private $userSession;
64
+    private $urlGenerator;
65
+    private $logger;
66
+    private $rootFolder;
67
+
68
+    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view, IUserSession $userSession, URLGenerator $urlGenerator, LoggerInterface $logger, IRootFolder $rootFolder) {
69
+
70
+           parent::__construct($appName, $request);
71
+
72
+           $this->service = $service;
73
+           $this->config = $config;
74
+           $this->userId = $userId;
75
+           $this->folder = $folder;
76
+           $this->filesystem = $filesystem;
77
+           $this->view = $view;
78
+           $this->userSession = $userSession;
79
+           $this->urlGenerator = $urlGenerator;
80
+           $this->logger = $logger;
81
+           $this->rootFolder = $rootFolder;
82
+    }
83
+
84
+    /**
85
+     * @NoAdminRequired
86
+     */
87
+    public function cleantempdir() {
88
+
89
+           // If the 'temp_files' folder doesn't exist create it
90
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
91
+               $this->folder->newFolder('Pax_Fax/temp_files');
92
+           }
93
+
94
+           $gettempdir = $this->folder->get("/Pax_Fax/temp_files");
95
+           $fileSystemIterator = $gettempdir->getDirectoryListing();
96
+
97
+           $dirfiles = [];
98
+           foreach ($fileSystemIterator as $fileInfo){
99
+                    $dirfiles[] = $fileInfo->getName();
100
+           }
101
+
102
+           foreach ($dirfiles as $key => $indfile) {
103
+                    $tempfile = "/Pax_Fax/temp_files/" . $indfile;
104
+                    $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
105
+                    $removetmpfile = $userFolder->get($tempfile)->delete();
106
+           }
107
+    }
108
+
109
+
110
+    /**
111
+     * @NoAdminRequired
112
+     */
113
+    public function object_to_array($obj) {
114
+
115
+           if(is_object($obj)) $obj = (array)$this->dismount($obj);
116
+           if(is_array($obj)) {
117
+              $new = array();
118
+              foreach($obj as $key => $val) {
119
+                      $new[$key] = $this->object_to_array($val);
120
+              }
121
+           }
122
+           else $new = $obj;
123
+           return $new;
124
+    }
125
+
126
+
127
+    /**
128
+     * @NoAdminRequired
129
+     */
130
+    public function dismount($object) {
131
+           $reflectionClass = new ReflectionClass(get_class($object));
132
+           $array = array();
133
+           foreach ($reflectionClass->getProperties() as $property) {
134
+                    $property->setAccessible(true);
135
+                    $array[$property->getName()] = $property->getValue($object);
136
+                    $property->setAccessible(false);
137
+           }
138
+           return $array;
139
+    }
140
+
141
+
142
+    /**
143
+     * @NoAdminRequired
144
+     */
145
+    public function getbalance() {
146
+
147
+           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
148
+
149
+           $sinchKeyId = $thisapicred[5];
150
+           $sinchKeySecret = $thisapicred[6];
151
+
152
+           if ($sinchKeyId && $sinchKeySecret) {
153
+
154
+               // Use Sinch
155
+               return "n/a";
156
+
157
+           } else {
158
+
159
+               // Use Phaxio
160
+               $apiMode = 'live';
161
+               $apiKeys[$apiMode] = $thisapicred[0];
162
+               $apiSecrets[$apiMode] = $thisapicred[1];
163
+
164
+               $apiHost = 'https://api.phaxio.com/v2.1/';
165
+
166
+               $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
167
+
168
+               try {
169
+                    // Get Phaxio account balance
170
+                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
171
+
172
+                    $balancetoarr = $this->object_to_array($phaxioresulttert);
173
+
174
+                    $phaxiobalance = $balancetoarr['data']['balance'];
175
+
176
+               } catch (InvalidRequestException $e) {
177
+                    $phaxiobalance = 'unknown';
178
+                    $phaxiosuccess = 'false';
179
+                    $errortype = 'invalid request error';
180
+               } catch (AuthenticationException $e) {
181
+                    $phaxiobalance = 'unknown';
182
+                    $phaxiosuccess = 'false';
183
+                    $errortype = 'authentication error';
184
+               } catch (APIConnectionException $e) {
185
+                    $phaxiobalance = 'unknown';
186
+                    $phaxiosuccess = 'false';
187
+                    $errortype = 'API connection error';
188
+               } catch (RateLimitException $e) {
189
+                    $phaxiobalance = 'unknown';
190
+                    $phaxiosuccess = 'false';
191
+                    $errortype = 'rate limit error';
192
+               } catch (NotFoundException $e) {
193
+                    $phaxiobalance = 'unknown';
194
+                    $phaxiosuccess = 'false';
195
+                    $errortype = 'not found error';
196
+               } catch (GeneralException $e) {
197
+                    $phaxiobalance = 'unknown';
198
+                    $phaxiosuccess = 'false';
199
+                    $errortype = 'undefined error';
200
+               }
201
+
202
+               return $phaxiobalance;
203
+           }
204
+    }
205
+
206
+
207
+    /**
208
+     * @NoAdminRequired
209
+     */
210
+    public function getfaxnumbers() {
211
+
212
+           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
213
+
214
+           $sinchKeyId = $thisapicred[5];
215
+           $sinchKeySecret = $thisapicred[6];
216
+
217
+           if ($sinchKeyId && $sinchKeySecret) {
218
+
219
+               // Use Sinch
220
+	       $sinchProjectId = $thisapicred[4];
221
+	       $sinchServiceId = $thisapicred[7];
222
+
223
+	       $nbquery = [ "pageSize" => "100", "page" => "string" ];
224
+
225
+	       $curl = curl_init();
226
+
227
+	       curl_setopt_array($curl, [
228
+		    CURLOPT_HTTPHEADER => [ "Authorization: Basic " . base64_encode("" . $sinchKeyId . ":" . $sinchKeySecret ."") ],
229
+                    CURLOPT_PORT => "",
230
+		    CURLOPT_URL => "https://fax.api.sinch.com/v3/projects/" . $sinchProjectId . "/services/" . $sinchServiceId . "/numbers?" . http_build_query($nbquery),
231
+		    CURLOPT_RETURNTRANSFER => true,
232
+		    CURLOPT_CUSTOMREQUEST => "GET",
233
+		]);
234
+
235
+		$responsesnb = curl_exec($curl);
236
+		$errorsnb = curl_error($curl);
237
+
238
+		curl_close($curl);
239
+
240
+		if ($errorsnb) { $this->logger->error('cURL Error #: '. $errorsnb . '.'); }
241
+
242
+                $sincharr = [];
243
+                $sinchnmbrs = json_decode($responsesnb, true);
244
+
245
+	        foreach ($sinchnmbrs['numbers'] as $snkey => $snvalue) {
246
+	                 if (is_array($snvalue)) {
247
+	                        foreach ($snvalue as $snkeysec => $snvaluesec) {
248
+	                               if ($snkeysec == 'phoneNumber') {
249
+	                                   $sincharr[] = $snvaluesec;
250
+	                               }
251
+	                        }
252
+	                 }
253
+	        }
254
+
255
+                $sinchnbdata = ["nblist" => $sincharr, "provider" => "sinch"];
256
+
257
+		return $sinchnbdata;
258
+
259
+           } else {
260
+
261
+                // Use Phaxio
262
+		$apiMode = 'live';
263
+		$apiKeys[$apiMode] = $thisapicred[0];
264
+		$apiSecrets[$apiMode] = $thisapicred[1];
265
+
266
+		$apiHost = 'https://api.phaxio.com/v2.1/';
267
+
268
+		$phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
269
+
270
+		try {
271
+	            // Get all Phaxio fax numbers
272
+	            $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
273
+
274
+	            $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
275
+
276
+	            $phaxioarr = [];
277
+
278
+	            foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
279
+	                   if (is_array($phvalue)) {
280
+	                         foreach ($phvalue as $phkeysec => $phvaluesec) {
281
+	                               if ($phkeysec == 'phone_number') {
282
+	                                   $phaxioarr[] = $phvaluesec;
283
+	                               }
284
+	                         }
285
+	                   }
286
+	            }
287
+
288
+		} catch (InvalidRequestException $e) {
289
+	            $phaxiobalance = 'unknown';
290
+	            $phaxiosuccess = 'false';
291
+	            $errortype = 'invalid request error';
292
+		} catch (AuthenticationException $e) {
293
+	            $phaxiobalance = 'unknown';
294
+	            $phaxiosuccess = 'false';
295
+	            $errortype = 'authentication error';
296
+		} catch (APIConnectionException $e) {
297
+	            $phaxiobalance = 'unknown';
298
+	            $phaxiosuccess = 'false';
299
+	            $errortype = 'API connection error';
300
+		} catch (RateLimitException $e) {
301
+	            $phaxiobalance = 'unknown';
302
+	            $phaxiosuccess = 'false';
303
+	            $errortype = 'rate limit error';
304
+		} catch (NotFoundException $e) {
305
+	            $phaxiobalance = 'unknown';
306
+	            $phaxiosuccess = 'false';
307
+	            $errortype = 'not found error';
308
+		} catch (GeneralException $e) {
309
+	            $phaxiobalance = 'unknown';
310
+	            $phaxiosuccess = 'false';
311
+	            $errortype = 'undefined error';
312
+		}
313
+
314
+                $phaxionbdata = ["nblist" => $phaxioarr, "provider" => "phaxio"];
315
+
316
+		return $phaxionbdata;
317
+           }
318
+    }
319
+
320
+
321
+    /**
322
+     * @NoAdminRequired
323
+     */
324
+    public function uploadfile($uploadfileforfax) {
325
+
326
+           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
327
+           $fileName = $_FILES['uploadfileforfax']['name'];
328
+           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
329
+           $fileSize = $fileSizeinit / 1048576;
330
+
331
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
332
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
333
+           }
334
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
335
+               $this->folder->newFolder('Pax_Fax/faxes_received');
336
+           }
337
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
338
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
339
+           }
340
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
341
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
342
+           }
343
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
344
+               $this->folder->newFolder('Pax_Fax/temp_files');
345
+           }
346
+
347
+           $targetfile = "/Pax_Fax/temp_files/" . $fileName;
348
+
349
+           $target = $this->folder->newFile($targetfile);
350
+           $target->putContent($fileContent);
351
+
352
+           // Get the cumulative files size of the uploaded files
353
+           $gettmpdir = $this->folder->get("/Pax_Fax/temp_files");
354
+           $fliterator = $gettmpdir->getDirectoryListing();
355
+
356
+
357
+           $dirfiles = [];
358
+           foreach ($fliterator as $fileInfo){
359
+                    $dirfiles[] = $fileInfo->getName();
360
+           }
361
+
362
+           $totalflsizeinit = 0;
363
+           foreach ($dirfiles as $key => $indfile) {
364
+                    $flsize = $this->folder->get("/Pax_Fax/temp_files/" . $indfile);
365
+                    $fileSizeinit = $flsize->getSize();
366
+                    $mbSize = $fileSizeinit / 1048576;
367
+                    $totalflsizeinit += $mbSize;
368
+           }
369
+
370
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
371
+
372
+           return $totalflsize;
373
+
374
+    }
375
+
376
+    /**
377
+     * @NoAdminRequired
378
+     */
379
+    public function pickfile($path) {
380
+
381
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
382
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
383
+           }
384
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
385
+               $this->folder->newFolder('Pax_Fax/faxes_received');
386
+           }
387
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
388
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
389
+           }
390
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
391
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
392
+           }
393
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
394
+               $this->folder->newFolder('Pax_Fax/temp_files');
395
+           }
396
+
397
+           $getfl = $this->folder->get($path);
398
+
399
+           $fileContentpk = $getfl->getContent();
400
+
401
+           $patharr = explode("/", $path);
402
+
403
+           $revarr = array_reverse($patharr);
404
+
405
+           $relflpath = "/Pax_Fax/temp_files/" . $revarr[0]; 
406
+
407
+           $target = $this->folder->newFile($relflpath);
408
+
409
+           $target->putContent($fileContentpk);
410
+
411
+           $gettmpdir = $this->folder->get("/Pax_Fax/temp_files");
412
+
413
+           $fileitrt = $gettmpdir->getDirectoryListing();
414
+
415
+           $dirfls = [];
416
+           foreach ($fileitrt as $fileInfo) {
417
+                    $dirfls[] = $fileInfo->getName();
418
+           }
419
+
420
+           $totalflsizeinitp = 0;
421
+
422
+           foreach ($dirfls as $keyp => $indfilep) {
423
+
424
+                    $flsizep = $this->folder->get("/Pax_Fax/temp_files/" . $indfilep);
425
+                    $fileSizeinitp = $flsizep->getSize();
426
+                    $mbSizep = $fileSizeinitp / 1048576;
427
+                    $totalflsizeinitp += $mbSizep;
428
+           }
429
+
430
+           $totalflsizep = number_format($totalflsizeinitp, 2) . ' MB';
431
+
432
+           return $totalflsizep;
433
+    }
434
+
435
+    /**
436
+     * @NoAdminRequired
437
+     */
438
+    public function removeupfile($removedfilename) {
439
+
440
+           $upfile = "/Pax_Fax/temp_files/" . $removedfilename;
441
+           $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
442
+           $removetmpfile = $userFolder->get($upfile)->delete();
443
+
444
+           $gettmpdir = $this->folder->get("/Pax_Fax/temp_files");
445
+           $fliterator = $gettmpdir->getDirectoryListing();
446
+
447
+           $dirfiles = [];
448
+           foreach ($fliterator as $fileInfo){
449
+                    $dirfiles[] = $fileInfo->getName();
450
+           }
451
+
452
+           $totalflsizeinit = 0;
453
+           foreach ($dirfiles as $key => $indfile) {
454
+                    $flsize = $this->folder->get("/Pax_Fax/temp_files/" . $indfile);
455
+                    $fileSizeinit = $flsize->getSize();
456
+                    $mbSize = $fileSizeinit / 1048576;
457
+                    $totalflsizeinit += $mbSize;
458
+           }
459
+
460
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
461
+
462
+           return $totalflsize;
463
+    }
464
+
465
+    /**
466
+     * @NoAdminRequired
467
+     */
468
+    public function prevpickedfile($pickedfilename) {
469
+
470
+           $getfl = $this->folder->get("/Pax_Fax/temp_files/" . $pickedfilename);
471
+           $getfilecontent = $getfl->getContent();
472
+
473
+           $namesplit = explode(".", $pickedfilename);
474
+           $extension = end($namesplit);
475
+
476
+           if ($extension == "txt" || $extension == "html") {
477
+               $getpickedfile = $getfilecontent;
478
+           } elseif ($extension == "jpg") {
479
+               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
480
+           } elseif ($extension == "png") {
481
+               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
482
+           } else { $getpickedfile = ""; }
483
+
484
+           return $getpickedfile;
485
+    }
486
+
487
+    /**
488
+     * @NoAdminRequired
489
+     */
490
+    public function sendfax($uploadedtofax, $selectedcid, $toNumber) {
491
+
492
+           $tonumbertr = str_replace("+", "", $toNumber[0]);
493
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
494
+           $fromnumberdigits = str_replace("+", "", $selectedcid);
495
+
496
+           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $selectedcid; } else { $fromnumber = ''; }
497
+
498
+           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
499
+
500
+           $sinchKeyId = $thisapicred[5];
501
+           $sinchKeySecret = $thisapicred[6];
502
+
503
+           if ($sinchKeyId && $sinchKeySecret) {
504
+
505
+               // Use Sinch
506
+	       $sinchProjectId = $thisapicred[4];
507
+
508
+               $countfaxfiles = count($uploadedtofax);
509
+
510
+               $openedfiles = [];
511
+               $crtuser = $this->userSession->getUser()->getUID();
512
+               $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
513
+
514
+               foreach ($uploadedtofax as $key => $flname) {
515
+
516
+                    if ($key == 0) { $firstflname = $flname; }
517
+
518
+                    $fileNamesec = array_reverse(explode(".", $flname));
519
+                    $filenameext = $fileNamesec[0];
520
+
521
+                    array_shift($fileNamesec);
522
+
523
+                    $fileName = implode("", $fileNamesec);
524
+
525
+                    if ($countfaxfiles == 1) {
526
+
527
+                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
528
+
529
+                    } else {
530
+
531
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
532
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
533
+                         }
534
+
535
+                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
536
+                    }
537
+
538
+		    $getfl = $this->folder->get("/Pax_Fax/temp_files/" . $flname);
539
+		    $fileContentsnd = $getfl->getContent();
540
+
541
+		    $target = $this->folder->newFile($targetfile);
542
+		    $target->putContent($fileContentsnd);
543
+
544
+		    $upfilefx = "/Pax_Fax/temp_files/" . $flname;
545
+		    $removetmpfile = $userFolder->get($upfilefx)->delete();
546
+
547
+                    if ($countfaxfiles == 1) {
548
+                        $gtflfx = $this->folder->get("/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
549
+                        $openedfiles[] = [ "file" => base64_encode($gtflfx->getContent()), "fileType" => $filenameext ];
550
+                    } else {
551
+                        $gtflfx = $this->folder->get("/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
552
+                        $openedfiles[] = [ "file" => base64_encode($gtflfx->getContent()), "fileType" => $filenameext ];
553
+                    }
554
+               }
555
+
556
+               $toNumbersn = [];
557
+               foreach ($toNumber as $nbkey => $nbvalue) { $toNumbersn[] = "+" . $nbvalue; }
558
+               $toNumbersnfin = implode(";", $toNumbersn);
559
+
560
+	       $datatosendsn = ["to" => $toNumbersnfin, "files" => $openedfiles];
561
+
562
+	       $postedparamsn = json_encode($datatosendsn);
563
+
564
+	       $chsn = curl_init();
565
+               curl_setopt($chsn, CURLOPT_URL, "https://" . $sinchKeyId . ":" . $sinchKeySecret . "@fax.api.sinch.com/v3/projects/" . $sinchProjectId . "/faxes");
566
+	       curl_setopt($chsn, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
567
+	       curl_setopt($chsn, CURLOPT_TIMEOUT, 300);
568
+	       curl_setopt($chsn, CURLOPT_RETURNTRANSFER, 1);
569
+	       curl_setopt($chsn, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
570
+	       curl_setopt($chsn, CURLOPT_POST, 1);
571
+	       curl_setopt($chsn, CURLOPT_POSTFIELDS, $postedparamsn);
572
+	       $resultsn = curl_exec($chsn);
573
+	       $statusCodesn = curl_getinfo($chsn, CURLINFO_HTTP_CODE);
574
+	       curl_close($chsn);
575
+
576
+	       $decresultsn = json_decode($resultsn, true);
577
+
578
+	       if (in_array($statusCodesn, [400, 401, 402, 404, 422, 429, 500, 800])) {
579
+                   $sinchsuccess = false;
580
+                   $sincherrortype = "Error: " . $decresultsn['code'] . " . Message: " . $decresultsn['message'];
581
+               } else { $sinchsuccess = true; $sincherrortype = ''; }
582
+
583
+               $sinchresult = [ 'success' => $sinchsuccess, 'errortype' => $sincherrortype ];
584
+
585
+               if ($sinchsuccess != true) {
586
+
587
+                    foreach ($uploadedtofax as $key => $flname) {
588
+
589
+                        $fileNamesec = array_reverse(explode(".", $flname));
590
+                        $filenameext = $fileNamesec[0];
591
+                        array_shift($fileNamesec);
592
+
593
+                        $fileName = implode("", $fileNamesec);
594
+
595
+                        if ($countfaxfiles == 1) {
596
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
597
+                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
598
+                        } else {
599
+
600
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
601
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
602
+                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
603
+                        }
604
+
605
+		        $getflsc = $this->folder->get($failedfl);
606
+		        $fileContentfld = $getflsc->getContent();
607
+
608
+		        $targetact = $this->folder->newFile($newtargetfl);
609
+		        $targetact->putContent($fileContentfld);
610
+
611
+		        $removetmpfile = $userFolder->get($failedfl)->delete();
612
+                    }
613
+
614
+                }
615
+
616
+                return $sinchresult;
617
+
618
+           } else {
619
+
620
+               // Use Phaxio
621
+               if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
622
+
623
+               $countfaxfiles = count($uploadedtofax);
624
+
625
+               $openedfiles = [];
626
+               $crtuser = $this->userSession->getUser()->getUID();
627
+               $userFolder = $this->rootFolder->getUserFolder($this->userSession->getUser()->getUID());
628
+
629
+               foreach ($uploadedtofax as $key => $flname) {
630
+
631
+                    if ($key == 0) { $firstflname = $flname; }
632
+
633
+                    $fileNamesec = array_reverse(explode(".", $flname));
634
+                    $filenameext = $fileNamesec[0];
635
+                    array_shift($fileNamesec);
636
+
637
+                    $fileName = implode("", $fileNamesec);
638
+
639
+                    if ($countfaxfiles == 1) {
640
+                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
641
+                    } else {
642
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
643
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
644
+                         }
645
+
646
+                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
647
+                    }
648
+
649
+		    $getflsc = $this->folder->get("/Pax_Fax/temp_files/" . $flname);
650
+		    $fileContentsnd = $getflsc->getContent();
651
+
652
+                    $target = $this->folder->newFile($targetfile);
653
+                    $target->putContent($fileContentsnd);
654
+
655
+                    $fltodel = "/Pax_Fax/temp_files/" . $flname;
656
+		    $removetmpfile = $userFolder->get($fltodel)->delete();
657
+
658
+                    if ($countfaxfiles == 1) {
659
+                        $getflopen = $userFolder->get("/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
660
+                        $openedfiles[] = $getflopen->fopen('r');
661
+                    } else {
662
+                        $getflopen = $userFolder->get("/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext);
663
+                        $openedfiles[] = $getflopen->fopen('r');
664
+                    }
665
+               }
666
+
667
+               $params = array(
668
+                           'to' => $toNumber,
669
+                           'file' => $openedfiles,
670
+                           'caller_id' => $selectedcid
671
+                         );
672
+
673
+               $apiMode = 'live';
674
+
675
+               $apiKeys[$apiMode] = $thisapicred[0];
676
+               $apiSecrets[$apiMode] = $thisapicred[1];
677
+
678
+               $apiHost = 'https://api.phaxio.com/v2.1/';
679
+
680
+               $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
681
+
682
+               try {
683
+                    $phaxioresultinit = $phaxio->sendFax($params);
684
+
685
+                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
686
+
687
+                    $statustoarr = $this->object_to_array($phaxioresultsec);
688
+
689
+                    $phaxiosuccess = $statustoarr['success'];
690
+
691
+                    $errortype = 'there are no errors';
692
+
693
+               } catch (InvalidRequestException $e) {
694
+                    $phaxiosuccess = 'false';
695
+                    $errortype = 'invalid request error';
696
+               } catch (AuthenticationException $e) {
697
+                    $phaxiosuccess = 'false';
698
+                    $errortype = 'authentication error';
699
+               } catch (APIConnectionException $e) {
700
+                    $phaxiosuccess = 'false';
701
+                    $errortype = 'API connection error';
702
+               } catch (RateLimitException $e) {
703
+                    $phaxiosuccess = 'false';
704
+                    $errortype = 'rate limit error';
705
+               } catch (NotFoundException $e) {
706
+                    $phaxiosuccess = 'false';
707
+                    $errortype = 'not found error';
708
+               } catch (GeneralException $e) {
709
+                    $phaxiosuccess = 'false';
710
+                    $errortype = 'undefined error';
711
+               }
712
+
713
+               $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
714
+
715
+               if ($phaxiosuccess != 'true') {
716
+
717
+                    foreach ($uploadedtofax as $key => $flname) {
718
+
719
+                        $fileNamesec = array_reverse(explode(".", $flname));
720
+                        $filenameext = $fileNamesec[0];
721
+                        array_shift($fileNamesec);
722
+
723
+                        $fileName = implode("", $fileNamesec);
724
+
725
+                        if ($countfaxfiles == 1) {
726
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
727
+                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
728
+                        } else {
729
+
730
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
731
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
732
+                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
733
+                        }
734
+
735
+		        $getflthrd = $this->folder->get($failedfl);
736
+		        $fileContentfld = $getflthrd->getContent();
737
+
738
+                        $targetact = $this->folder->newFile($targetfile);
739
+                        $targetact->putContent($fileContentfld);
740
+
741
+		        $removefailed = $userFolder->get($failedfl)->delete();
742
+                    }
743
+               }
744
+
745
+               return $phaxioresult;
746
+           }
747
+    }
748
+
749
+
750
+    /**
751
+     * @NoAdminRequired
752
+     */
753
+    public function getsettings() {
754
+           return $this->service->getsettings($this->userSession->getUser()->getUID());
755
+    }
756
+
757
+
758
+    /**
759
+     * @NoAdminRequired
760
+     */
761
+    public function updatesettings($apiKey, $apiSecret, $webhookToken, $receiveUrl, $sinchProjectId, $sinchKeyId, $sinchKeySecret, $sinchServiceId, $sinchCallbackUrl, $getNotification, $notificationEmail) {
762
+           return $this->service->updatesettings($this->userSession->getUser()->getUID(), $apiKey, $apiSecret, $webhookToken, $receiveUrl, $sinchProjectId, $sinchKeyId, $sinchKeySecret, $sinchServiceId, $sinchCallbackUrl, $getNotification, $notificationEmail);
763
+    }
764
+
765
+}
Browse code

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

DoubleBastionAdmin authored on 02/06/2025 20:06:46
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,768 +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 OCP\IUserSession;
38
-use OC\URLGenerator;
39
-use \ReflectionClass;
40
-use \FilesystemIterator;
41
-use Psr\Log\LoggerInterface;
42
-
43
-use Phaxio;
44
-use Phaxio\OperationResult;
45
-use Phaxio\Error\AuthenticationException;
46
-use Phaxio\Error\NotFoundException;
47
-use Phaxio\Error\InvalidRequestException;
48
-use Phaxio\Error\RateLimitException;
49
-use Phaxio\Error\APIConnectionException;
50
-use Phaxio\Error\GeneralException;
51
-
52
-
53
-class PaxfaxController extends Controller {
54
-
55
-    private $service;
56
-    private $config;
57
-    private $userId;
58
-    private $folder;
59
-    private $filesystem;
60
-    private $view;
61
-    private $userSession;
62
-    private $urlGenerator;
63
-    private $logger;
64
-
65
-    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view, IUserSession $userSession, URLGenerator $urlGenerator, LoggerInterface $logger) {
66
-
67
-           parent::__construct($appName, $request);
68
-
69
-           $this->service = $service;
70
-           $this->config = $config;
71
-           $this->userId = $userId;
72
-           $this->folder = $folder;
73
-           $this->filesystem = $filesystem;
74
-           $this->view = $view;
75
-           $this->userSession = $userSession;
76
-           $this->urlGenerator = $urlGenerator;
77
-           $this->logger = $logger;
78
-    }
79
-
80
-
81
-    /**
82
-     * @NoAdminRequired
83
-     */
84
-    public function cleantempdir() {
85
-
86
-           // If the 'temp_files' folder doesn't exist create it
87
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
88
-               $this->folder->newFolder('Pax_Fax/temp_files');
89
-           }
90
-
91
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
92
-           $targetdir = $datadir . $this->userSession->getUser()->getUID() . "/files/Pax_Fax/temp_files";
93
-           $fileSystemIterator = new FilesystemIterator($targetdir);
94
-
95
-           $dirfiles = [];
96
-           foreach ($fileSystemIterator as $fileInfo){
97
-                    $dirfiles[] = $fileInfo->getFilename();
98
-           }
99
-
100
-           foreach ($dirfiles as $key => $indfile) {
101
-                    $thisuserroot = $this->view->getRoot();
102
-                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
103
-                    $removetmpfile = $this->filesystem->unlink($tempfile);
104
-           }
105
-    }
106
-
107
-
108
-    /**
109
-     * @NoAdminRequired
110
-     */
111
-    public function object_to_array($obj) {
112
-
113
-           if(is_object($obj)) $obj = (array)$this->dismount($obj);
114
-           if(is_array($obj)) {
115
-              $new = array();
116
-              foreach($obj as $key => $val) {
117
-                      $new[$key] = $this->object_to_array($val);
118
-              }
119
-           }
120
-           else $new = $obj;
121
-           return $new;
122
-    }
123
-
124
-
125
-    /**
126
-     * @NoAdminRequired
127
-     */
128
-    public function dismount($object) {
129
-           $reflectionClass = new ReflectionClass(get_class($object));
130
-           $array = array();
131
-           foreach ($reflectionClass->getProperties() as $property) {
132
-                    $property->setAccessible(true);
133
-                    $array[$property->getName()] = $property->getValue($object);
134
-                    $property->setAccessible(false);
135
-           }
136
-           return $array;
137
-    }
138
-
139
-
140
-    /**
141
-     * @NoAdminRequired
142
-     */
143
-    public function getbalance() {
144
-
145
-           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
146
-
147
-           $sinchKeyId = $thisapicred[5];
148
-           $sinchKeySecret = $thisapicred[6];
149
-
150
-           if ($sinchKeyId && $sinchKeySecret) {
151
-
152
-               // Use Sinch
153
-               return "n/a";
154
-
155
-           } else {
156
-
157
-               // Use Phaxio
158
-               $apiMode = 'live';
159
-               $apiKeys[$apiMode] = $thisapicred[0];
160
-               $apiSecrets[$apiMode] = $thisapicred[1];
161
-
162
-               $apiHost = 'https://api.phaxio.com/v2.1/';
163
-
164
-               $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
165
-
166
-               try {
167
-                    // Get Phaxio account balance
168
-                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
169
-
170
-                    $balancetoarr = $this->object_to_array($phaxioresulttert);
171
-
172
-                    $phaxiobalance = $balancetoarr['data']['balance'];
173
-
174
-               } catch (InvalidRequestException $e) {
175
-                    $phaxiobalance = 'unknown';
176
-                    $phaxiosuccess = 'false';
177
-                    $errortype = 'invalid request error';
178
-               } catch (AuthenticationException $e) {
179
-                    $phaxiobalance = 'unknown';
180
-                    $phaxiosuccess = 'false';
181
-                    $errortype = 'authentication error';
182
-               } catch (APIConnectionException $e) {
183
-                    $phaxiobalance = 'unknown';
184
-                    $phaxiosuccess = 'false';
185
-                    $errortype = 'API connection error';
186
-               } catch (RateLimitException $e) {
187
-                    $phaxiobalance = 'unknown';
188
-                    $phaxiosuccess = 'false';
189
-                    $errortype = 'rate limit error';
190
-               } catch (NotFoundException $e) {
191
-                    $phaxiobalance = 'unknown';
192
-                    $phaxiosuccess = 'false';
193
-                    $errortype = 'not found error';
194
-               } catch (GeneralException $e) {
195
-                    $phaxiobalance = 'unknown';
196
-                    $phaxiosuccess = 'false';
197
-                    $errortype = 'undefined error';
198
-               }
199
-
200
-               return $phaxiobalance;
201
-           }
202
-    }
203
-
204
-
205
-    /**
206
-     * @NoAdminRequired
207
-     */
208
-    public function getfaxnumbers() {
209
-
210
-           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
211
-
212
-           $sinchKeyId = $thisapicred[5];
213
-           $sinchKeySecret = $thisapicred[6];
214
-
215
-           if ($sinchKeyId && $sinchKeySecret) {
216
-
217
-               // Use Sinch
218
-	       $sinchProjectId = $thisapicred[4];
219
-	       $sinchServiceId = $thisapicred[7];
220
-
221
-	       $nbquery = [ "pageSize" => "100", "page" => "string" ];
222
-
223
-	       $curl = curl_init();
224
-
225
-	       curl_setopt_array($curl, [
226
-		    CURLOPT_HTTPHEADER => [ "Authorization: Basic " . base64_encode("" . $sinchKeyId . ":" . $sinchKeySecret ."") ],
227
-                    CURLOPT_PORT => "",
228
-		    CURLOPT_URL => "https://fax.api.sinch.com/v3/projects/" . $sinchProjectId . "/services/" . $sinchServiceId . "/numbers?" . http_build_query($nbquery),
229
-		    CURLOPT_RETURNTRANSFER => true,
230
-		    CURLOPT_CUSTOMREQUEST => "GET",
231
-		]);
232
-
233
-		$responsesnb = curl_exec($curl);
234
-		$errorsnb = curl_error($curl);
235
-
236
-		curl_close($curl);
237
-
238
-		if ($errorsnb) { $this->logger->error('cURL Error #: '. $errorsnb . '.'); }
239
-
240
-                $sincharr = [];
241
-                $sinchnmbrs = json_decode($responsesnb, true);
242
-
243
-	        foreach ($sinchnmbrs['numbers'] as $snkey => $snvalue) {
244
-	                 if (is_array($snvalue)) {
245
-	                        foreach ($snvalue as $snkeysec => $snvaluesec) {
246
-	                               if ($snkeysec == 'phoneNumber') {
247
-	                                   $sincharr[] = $snvaluesec;
248
-	                               }
249
-	                        }
250
-	                 }
251
-	        }
252
-
253
-                $sinchnbdata = ["nblist" => $sincharr, "provider" => "sinch"];
254
-
255
-		return $sinchnbdata;
256
-
257
-           } else {
258
-
259
-                // Use Phaxio
260
-		$apiMode = 'live';
261
-		$apiKeys[$apiMode] = $thisapicred[0];
262
-		$apiSecrets[$apiMode] = $thisapicred[1];
263
-
264
-		$apiHost = 'https://api.phaxio.com/v2.1/';
265
-
266
-		$phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
267
-
268
-		try {
269
-	            // Get all Phaxio fax numbers
270
-	            $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
271
-
272
-	            $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
273
-
274
-	            $phaxioarr = [];
275
-
276
-	            foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
277
-	                   if (is_array($phvalue)) {
278
-	                         foreach ($phvalue as $phkeysec => $phvaluesec) {
279
-	                               if ($phkeysec == 'phone_number') {
280
-	                                   $phaxioarr[] = $phvaluesec;
281
-	                               }
282
-	                         }
283
-	                   }
284
-	            }
285
-
286
-		} catch (InvalidRequestException $e) {
287
-	            $phaxiobalance = 'unknown';
288
-	            $phaxiosuccess = 'false';
289
-	            $errortype = 'invalid request error';
290
-		} catch (AuthenticationException $e) {
291
-	            $phaxiobalance = 'unknown';
292
-	            $phaxiosuccess = 'false';
293
-	            $errortype = 'authentication error';
294
-		} catch (APIConnectionException $e) {
295
-	            $phaxiobalance = 'unknown';
296
-	            $phaxiosuccess = 'false';
297
-	            $errortype = 'API connection error';
298
-		} catch (RateLimitException $e) {
299
-	            $phaxiobalance = 'unknown';
300
-	            $phaxiosuccess = 'false';
301
-	            $errortype = 'rate limit error';
302
-		} catch (NotFoundException $e) {
303
-	            $phaxiobalance = 'unknown';
304
-	            $phaxiosuccess = 'false';
305
-	            $errortype = 'not found error';
306
-		} catch (GeneralException $e) {
307
-	            $phaxiobalance = 'unknown';
308
-	            $phaxiosuccess = 'false';
309
-	            $errortype = 'undefined error';
310
-		}
311
-
312
-                $phaxionbdata = ["nblist" => $phaxioarr, "provider" => "phaxio"];
313
-
314
-		return $phaxionbdata;
315
-           }
316
-        
317
-    }
318
-
319
-
320
-    /**
321
-     * @NoAdminRequired
322
-     */
323
-    public function uploadfile($uploadfileforfax) {
324
-
325
-           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
326
-           $fileName = $_FILES['uploadfileforfax']['name'];
327
-           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
328
-           $fileSize = $fileSizeinit / 1048576;
329
-
330
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
331
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
332
-           }
333
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
334
-               $this->folder->newFolder('Pax_Fax/faxes_received');
335
-           }
336
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
337
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
338
-           }
339
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
340
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
341
-           }
342
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
343
-               $this->folder->newFolder('Pax_Fax/temp_files');
344
-           }
345
-
346
-           $userroot = $this->view->getRoot();
347
-           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
348
-
349
-           $target = $this->folder->newFile($targetfile);
350
-           $target->putContent($fileContent);
351
-
352
-           // Get the cumulative files size of the uploaded files
353
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
354
-           $targetdir = $datadir . $this->userSession->getUser()->getUID() . "/files/Pax_Fax/temp_files";
355
-
356
-           $fileSystemIterator = new FilesystemIterator($targetdir);
357
-
358
-           $dirfiles = [];
359
-           foreach ($fileSystemIterator as $fileInfo){
360
-                    $dirfiles[] = $fileInfo->getFilename();
361
-           }
362
-
363
-           $totalflsizeinit = 0;
364
-           foreach ($dirfiles as $key => $indfile) {
365
-                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
366
-                    $mbSize = $fileSizeinit / 1048576;
367
-                    $totalflsizeinit += $mbSize;
368
-           }
369
-
370
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
371
-
372
-           return $totalflsize;
373
-    }
374
-
375
-    /**
376
-     * @NoAdminRequired
377
-     */
378
-    public function pickfile($path) {
379
-
380
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
381
-               $this->folder->newFolder('Pax_Fax/faxes_sent');
382
-           }
383
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
384
-               $this->folder->newFolder('Pax_Fax/faxes_received');
385
-           }
386
-           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
387
-               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
388
-           }
389
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
390
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
391
-           }
392
-           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
393
-               $this->folder->newFolder('Pax_Fax/temp_files');
394
-           }
395
-
396
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
397
-
398
-           $fltgt = $datadir . $this->userSession->getUser()->getUID() . "/files" . $path;
399
-
400
-           $fileContentpk = file_get_contents($fltgt);
401
-
402
-           $patharr = explode("/", $path);
403
-
404
-           $revarr = array_reverse($patharr);
405
-
406
-           $relflpath = "/Pax_Fax/temp_files/" . $revarr[0]; 
407
-
408
-           $target = $this->folder->newFile($relflpath);
409
-
410
-           $target->putContent($fileContentpk);
411
-
412
-           $targetdir = $datadir . $this->userSession->getUser()->getUID() . "/files/Pax_Fax/temp_files";
413
-
414
-           $fileSystemIterator = new FilesystemIterator($targetdir);
415
-
416
-           $dirfiles = [];
417
-           foreach ($fileSystemIterator as $fileInfo) {
418
-                    $dirfiles[] = $fileInfo->getFilename();
419
-           }
420
-
421
-           $totalflsizeinit = 0;
422
-
423
-           foreach ($dirfiles as $key => $indfile) {
424
-
425
-                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
426
-                    $mbSize = $fileSizeinit / 1048576;
427
-                    $totalflsizeinit += $mbSize;
428
-           }
429
-
430
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
431
-
432
-           return $totalflsize;
433
-    }
434
-
435
-    /**
436
-     * @NoAdminRequired
437
-     */
438
-    public function removeupfile($removedfilename) {
439
-
440
-           $tmpfl = "/" . $this->userSession->getUser()->getUID() . "/files/Pax_Fax/temp_files/" . $removedfilename;
441
-
442
-           $removefile = $this->view->unlink($tmpfl);
443
-
444
-           // Get the cumulative files size of the uploaded files
445
-           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
446
-           $targetdir = $datadir . $this->userSession->getUser()->getUID() . "/files/Pax_Fax/temp_files";
447
-           $fileSystemIterator = new FilesystemIterator($targetdir);
448
-
449
-           $dirfiles = [];
450
-           foreach ($fileSystemIterator as $fileInfo) {
451
-                    $dirfiles[] = $fileInfo->getFilename();
452
-           }
453
-
454
-           $totalflsizeinit = 0;
455
-
456
-           foreach ($dirfiles as $key => $indfile) {
457
-                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
458
-                    $mbSize = $fileSizeinit / 1048576;
459
-                    $totalflsizeinit += $mbSize;
460
-           }
461
-
462
-           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
463
-
464
-           return $totalflsize;
465
-    }
466
-
467
-    /**
468
-     * @NoAdminRequired
469
-     */
470
-    public function getpickedfile($pickedfilename) {
471
-
472
-           $thisuserroot = $this->view->getRoot();
473
-
474
-           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
475
-           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
476
-
477
-           $namesplit = explode(".", $pickedfilename);
478
-           $extension = end($namesplit);
479
-
480
-           if ($extension == "txt" || $extension == "html") {
481
-               $getpickedfile = $getfilecontent;
482
-           } elseif ($extension == "jpg") {
483
-               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
484
-           } elseif ($extension == "png") {
485
-               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
486
-           } else { $getpickedfile = ""; }
487
-
488
-           return $getpickedfile;
489
-    }
490
-
491
-    /**
492
-     * @NoAdminRequired
493
-     */
494
-    public function sendfax($uploadedtofax, $selectedcid, $toNumber) {
495
-
496
-           $tonumbertr = str_replace("+", "", $toNumber[0]);
497
-           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
498
-           $fromnumberdigits = str_replace("+", "", $selectedcid);
499
-
500
-           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $selectedcid; } else { $fromnumber = ''; }
501
-
502
-           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
503
-
504
-           $sinchKeyId = $thisapicred[5];
505
-           $sinchKeySecret = $thisapicred[6];
506
-
507
-           if ($sinchKeyId && $sinchKeySecret) {
508
-
509
-               // Use Sinch
510
-	       $sinchProjectId = $thisapicred[4];
511
-
512
-               $countfaxfiles = count($uploadedtofax);
513
-
514
-               $openedfiles = [];
515
-               $crtuser = $this->userSession->getUser()->getUID();
516
-
517
-               foreach ($uploadedtofax as $key => $flname) {
518
-
519
-                    if ($key == 0) { $firstflname = $flname; }
520
-
521
-                    $fileNamesec = array_reverse(explode(".", $flname));
522
-                    $filenameext = $fileNamesec[0];
523
-
524
-                    array_shift($fileNamesec);
525
-
526
-                    $fileName = implode("", $fileNamesec);
527
-
528
-                    if ($countfaxfiles == 1) {
529
-
530
-                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
531
-
532
-                    } else {
533
-
534
-                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
535
-                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
536
-                         }
537
-
538
-                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
539
-                    }
540
-
541
-                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
542
-
543
-                    $fltgtsnd = $datadir . $crtuser . "/files/Pax_Fax/temp_files/" . $flname;
544
-                    $fltgtsndtmp = "/Pax_Fax/temp_files/" . $flname;
545
-
546
-                    $fileContentsnd = file_get_contents($fltgtsnd);
547
-
548
-                    $target = $this->folder->newFile($targetfile);
549
-                    $target->putContent($fileContentsnd);
550
-
551
-                    $removetmpfile = $this->filesystem->unlink($fltgtsndtmp);
552
-
553
-                    if ($countfaxfiles == 1) {
554
-                        $openedfiles[] = [ "file" => base64_encode(file_get_contents($datadir . $crtuser . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext)), "fileType" => $filenameext ];
555
-                    } else {
556
-                        $openedfiles[] = [ "file" => base64_encode(file_get_contents($datadir . $crtuser . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext)), "fileType" => $filenameext ];
557
-                    }
558
-               }
559
-
560
-               $toNumbersn = [];
561
-               foreach ($toNumber as $nbkey => $nbvalue) { $toNumbersn[] = "+" . $nbvalue; }
562
-               $toNumbersnfin = implode(";", $toNumbersn);
563
-
564
-	       $datatosendsn = ["to" => $toNumbersnfin, "files" => $openedfiles];
565
-
566
-	       $postedparamsn = json_encode($datatosendsn);
567
-
568
-	       $chsn = curl_init();
569
-               curl_setopt($chsn, CURLOPT_URL, "https://" . $sinchKeyId . ":" . $sinchKeySecret . "@fax.api.sinch.com/v3/projects/" . $sinchProjectId . "/faxes");
570
-	       curl_setopt($chsn, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
571
-	       curl_setopt($chsn, CURLOPT_TIMEOUT, 300);
572
-	       curl_setopt($chsn, CURLOPT_RETURNTRANSFER, 1);
573
-	       curl_setopt($chsn, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
574
-	       curl_setopt($chsn, CURLOPT_POST, 1);
575
-	       curl_setopt($chsn, CURLOPT_POSTFIELDS, $postedparamsn);
576
-	       $resultsn = curl_exec($chsn);
577
-	       $statusCodesn = curl_getinfo($chsn, CURLINFO_HTTP_CODE);
578
-	       curl_close($chsn);
579
-
580
-	       $decresultsn = json_decode($resultsn, true);
581
-
582
-	       if (in_array($statusCodesn, [400, 401, 402, 404, 422, 429, 500, 800])) {
583
-                   $sinchsuccess = false;
584
-                   $sincherrortype = "Error: " . $decresultsn['code'] . " . Message: " . $decresultsn['message'];
585
-               } else { $sinchsuccess = true; $sincherrortype = ''; }
586
-
587
-               $sinchresult = [ 'success' => $sinchsuccess, 'errortype' => $sincherrortype ];
588
-
589
-               if ($sinchsuccess != true) {
590
-
591
-                    foreach ($uploadedtofax as $key => $flname) {
592
-
593
-                        $fileNamesec = array_reverse(explode(".", $flname));
594
-                        $filenameext = $fileNamesec[0];
595
-                        array_shift($fileNamesec);
596
-
597
-                        $fileName = implode("", $fileNamesec);
598
-
599
-                        if ($countfaxfiles == 1) {
600
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
601
-                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
602
-                        } else {
603
-
604
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
605
-                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
606
-                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
607
-                        }
608
-
609
-                        $fileContentfld = $this->filesystem->file_get_contents($failedfl);
610
-
611
-                        $targetact = $this->folder->newFile($newtargetfl);
612
-                        $targetact->putContent($fileContentfld);
613
-
614
-                        $removefailed = $this->filesystem->unlink($failedfl);
615
-                    }
616
-
617
-                }
618
-
619
-                return $sinchresult;
620
-
621
-           } else {
622
-
623
-               // Use Phaxio
624
-               if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
625
-
626
-               $countfaxfiles = count($uploadedtofax);
627
-
628
-               $openedfiles = [];
629
-               $crtuser = $this->userSession->getUser()->getUID();
630
-
631
-               foreach ($uploadedtofax as $key => $flname) {
632
-
633
-                    if ($key == 0) { $firstflname = $flname; }
634
-
635
-                    $fileNamesec = array_reverse(explode(".", $flname));
636
-                    $filenameext = $fileNamesec[0];
637
-                    array_shift($fileNamesec);
638
-
639
-                    $fileName = implode("", $fileNamesec);
640
-
641
-                    if ($countfaxfiles == 1) {
642
-                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
643
-                    } else {
644
-                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
645
-                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
646
-                         }
647
-
648
-                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
649
-                    }
650
-
651
-                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
652
-
653
-                    $fltgtsnd = $datadir . $crtuser . "/files/Pax_Fax/temp_files/" . $flname;
654
-                    $fltgtsndtmp = "/Pax_Fax/temp_files/" . $flname;
655
-
656
-                    $fileContentsnd = file_get_contents($fltgtsnd);
657
-
658
-                    $target = $this->folder->newFile($targetfile);
659
-                    $target->putContent($fileContentsnd);
660
-
661
-                    $removetmpfile = $this->filesystem->unlink($fltgtsndtmp);
662
-
663
-                    if ($countfaxfiles == 1) {
664
-                        $openedfiles[] = fopen($datadir . $crtuser . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
665
-                    } else {
666
-                        $openedfiles[] = fopen($datadir . $crtuser . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
667
-                    }
668
-               }
669
-
670
-               $params = array(
671
-                           'to' => $toNumber,
672
-                           'file' => $openedfiles,
673
-                           'caller_id' => $selectedcid
674
-                         );
675
-
676
-               $apiMode = 'live';
677
-
678
-               $apiKeys[$apiMode] = $thisapicred[0];
679
-               $apiSecrets[$apiMode] = $thisapicred[1];
680
-
681
-               $apiHost = 'https://api.phaxio.com/v2.1/';
682
-
683
-               $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
684
-
685
-               try {
686
-                    $phaxioresultinit = $phaxio->sendFax($params);
687
-
688
-                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
689
-
690
-                    $statustoarr = $this->object_to_array($phaxioresultsec);
691
-
692
-                    $phaxiosuccess = $statustoarr['success'];
693
-
694
-                    $errortype = 'there are no errors';
695
-
696
-               } catch (InvalidRequestException $e) {
697
-                    $phaxiosuccess = 'false';
698
-                    $errortype = 'invalid request error';
699
-               } catch (AuthenticationException $e) {
700
-                    $phaxiosuccess = 'false';
701
-                    $errortype = 'authentication error';
702
-               } catch (APIConnectionException $e) {
703
-                    $phaxiosuccess = 'false';
704
-                    $errortype = 'API connection error';
705
-               } catch (RateLimitException $e) {
706
-                    $phaxiosuccess = 'false';
707
-                    $errortype = 'rate limit error';
708
-               } catch (NotFoundException $e) {
709
-                    $phaxiosuccess = 'false';
710
-                    $errortype = 'not found error';
711
-               } catch (GeneralException $e) {
712
-                    $phaxiosuccess = 'false';
713
-                    $errortype = 'undefined error';
714
-               }
715
-
716
-               $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
717
-
718
-               if ($phaxiosuccess != 'true') {
719
-
720
-                    foreach ($uploadedtofax as $key => $flname) {
721
-
722
-                        $fileNamesec = array_reverse(explode(".", $flname));
723
-                        $filenameext = $fileNamesec[0];
724
-                        array_shift($fileNamesec);
725
-
726
-                        $fileName = implode("", $fileNamesec);
727
-
728
-                        if ($countfaxfiles == 1) {
729
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
730
-                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
731
-                        } else {
732
-
733
-                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
734
-                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
735
-                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
736
-                        }
737
-
738
-                        $fileContentfld = $this->filesystem->file_get_contents($failedfl);
739
-
740
-                        $targetact = $this->folder->newFile($newtargetfl);
741
-                        $targetact->putContent($fileContentfld);
742
-
743
-                        $removefailed = $this->filesystem->unlink($failedfl);
744
-                    }
745
-
746
-               }
747
-
748
-               return $phaxioresult;
749
-           }
750
-    }
751
-
752
-
753
-    /**
754
-     * @NoAdminRequired
755
-     */
756
-    public function getsettings() {
757
-           return $this->service->getsettings($this->userSession->getUser()->getUID());
758
-    }
759
-
760
-
761
-    /**
762
-     * @NoAdminRequired
763
-     */
764
-    public function updatesettings($apiKey, $apiSecret, $webhookToken, $receiveUrl, $sinchProjectId, $sinchKeyId, $sinchKeySecret, $sinchServiceId, $sinchCallbackUrl, $getNotification, $notificationEmail) {
765
-           return $this->service->updatesettings($this->userSession->getUser()->getUID(), $apiKey, $apiSecret, $webhookToken, $receiveUrl, $sinchProjectId, $sinchKeyId, $sinchKeySecret, $sinchServiceId, $sinchCallbackUrl, $getNotification, $notificationEmail);
766
-    }
767
-
768
-}
Browse code

added appinfo/info.xml appinfo/signature.json appinfo/routes.php CHANGELOG.txt templates/navigation/index.php templates/settings.php css/style.css js/settings.js js/sendfax.js lib/Controller/AuthorApiController.php lib/Controller/PaxfaxController.php lib/Service/PaxfaxService.php img/pax_fax_received_fax.png img/pax_fax_send_uploaded_file_as_fax.png l10n/en_GB.js l10n/en_US.js l10n/en_GB.json l10n/en_US.json lib/Migration/Version119Date20250425082914.php

DoubleBastionAdmin authored on 25/04/2025 06:06:16
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,768 @@
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 OCP\IUserSession;
38
+use OC\URLGenerator;
39
+use \ReflectionClass;
40
+use \FilesystemIterator;
41
+use Psr\Log\LoggerInterface;
42
+
43
+use Phaxio;
44
+use Phaxio\OperationResult;
45
+use Phaxio\Error\AuthenticationException;
46
+use Phaxio\Error\NotFoundException;
47
+use Phaxio\Error\InvalidRequestException;
48
+use Phaxio\Error\RateLimitException;
49
+use Phaxio\Error\APIConnectionException;
50
+use Phaxio\Error\GeneralException;
51
+
52
+
53
+class PaxfaxController extends Controller {
54
+
55
+    private $service;
56
+    private $config;
57
+    private $userId;
58
+    private $folder;
59
+    private $filesystem;
60
+    private $view;
61
+    private $userSession;
62
+    private $urlGenerator;
63
+    private $logger;
64
+
65
+    public function __construct($appName, IRequest $request, PaxfaxService $service, IConfig $config, $userId, Folder $folder, Filesystem $filesystem, View $view, IUserSession $userSession, URLGenerator $urlGenerator, LoggerInterface $logger) {
66
+
67
+           parent::__construct($appName, $request);
68
+
69
+           $this->service = $service;
70
+           $this->config = $config;
71
+           $this->userId = $userId;
72
+           $this->folder = $folder;
73
+           $this->filesystem = $filesystem;
74
+           $this->view = $view;
75
+           $this->userSession = $userSession;
76
+           $this->urlGenerator = $urlGenerator;
77
+           $this->logger = $logger;
78
+    }
79
+
80
+
81
+    /**
82
+     * @NoAdminRequired
83
+     */
84
+    public function cleantempdir() {
85
+
86
+           // If the 'temp_files' folder doesn't exist create it
87
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
88
+               $this->folder->newFolder('Pax_Fax/temp_files');
89
+           }
90
+
91
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
92
+           $targetdir = $datadir . $this->userSession->getUser()->getUID() . "/files/Pax_Fax/temp_files";
93
+           $fileSystemIterator = new FilesystemIterator($targetdir);
94
+
95
+           $dirfiles = [];
96
+           foreach ($fileSystemIterator as $fileInfo){
97
+                    $dirfiles[] = $fileInfo->getFilename();
98
+           }
99
+
100
+           foreach ($dirfiles as $key => $indfile) {
101
+                    $thisuserroot = $this->view->getRoot();
102
+                    $tempfile = $thisuserroot . "/Pax_Fax/temp_files/" . $indfile;
103
+                    $removetmpfile = $this->filesystem->unlink($tempfile);
104
+           }
105
+    }
106
+
107
+
108
+    /**
109
+     * @NoAdminRequired
110
+     */
111
+    public function object_to_array($obj) {
112
+
113
+           if(is_object($obj)) $obj = (array)$this->dismount($obj);
114
+           if(is_array($obj)) {
115
+              $new = array();
116
+              foreach($obj as $key => $val) {
117
+                      $new[$key] = $this->object_to_array($val);
118
+              }
119
+           }
120
+           else $new = $obj;
121
+           return $new;
122
+    }
123
+
124
+
125
+    /**
126
+     * @NoAdminRequired
127
+     */
128
+    public function dismount($object) {
129
+           $reflectionClass = new ReflectionClass(get_class($object));
130
+           $array = array();
131
+           foreach ($reflectionClass->getProperties() as $property) {
132
+                    $property->setAccessible(true);
133
+                    $array[$property->getName()] = $property->getValue($object);
134
+                    $property->setAccessible(false);
135
+           }
136
+           return $array;
137
+    }
138
+
139
+
140
+    /**
141
+     * @NoAdminRequired
142
+     */
143
+    public function getbalance() {
144
+
145
+           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
146
+
147
+           $sinchKeyId = $thisapicred[5];
148
+           $sinchKeySecret = $thisapicred[6];
149
+
150
+           if ($sinchKeyId && $sinchKeySecret) {
151
+
152
+               // Use Sinch
153
+               return "n/a";
154
+
155
+           } else {
156
+
157
+               // Use Phaxio
158
+               $apiMode = 'live';
159
+               $apiKeys[$apiMode] = $thisapicred[0];
160
+               $apiSecrets[$apiMode] = $thisapicred[1];
161
+
162
+               $apiHost = 'https://api.phaxio.com/v2.1/';
163
+
164
+               $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
165
+
166
+               try {
167
+                    // Get Phaxio account balance
168
+                    $phaxioresulttert = $phaxio->doRequest("GET", 'account/status');
169
+
170
+                    $balancetoarr = $this->object_to_array($phaxioresulttert);
171
+
172
+                    $phaxiobalance = $balancetoarr['data']['balance'];
173
+
174
+               } catch (InvalidRequestException $e) {
175
+                    $phaxiobalance = 'unknown';
176
+                    $phaxiosuccess = 'false';
177
+                    $errortype = 'invalid request error';
178
+               } catch (AuthenticationException $e) {
179
+                    $phaxiobalance = 'unknown';
180
+                    $phaxiosuccess = 'false';
181
+                    $errortype = 'authentication error';
182
+               } catch (APIConnectionException $e) {
183
+                    $phaxiobalance = 'unknown';
184
+                    $phaxiosuccess = 'false';
185
+                    $errortype = 'API connection error';
186
+               } catch (RateLimitException $e) {
187
+                    $phaxiobalance = 'unknown';
188
+                    $phaxiosuccess = 'false';
189
+                    $errortype = 'rate limit error';
190
+               } catch (NotFoundException $e) {
191
+                    $phaxiobalance = 'unknown';
192
+                    $phaxiosuccess = 'false';
193
+                    $errortype = 'not found error';
194
+               } catch (GeneralException $e) {
195
+                    $phaxiobalance = 'unknown';
196
+                    $phaxiosuccess = 'false';
197
+                    $errortype = 'undefined error';
198
+               }
199
+
200
+               return $phaxiobalance;
201
+           }
202
+    }
203
+
204
+
205
+    /**
206
+     * @NoAdminRequired
207
+     */
208
+    public function getfaxnumbers() {
209
+
210
+           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
211
+
212
+           $sinchKeyId = $thisapicred[5];
213
+           $sinchKeySecret = $thisapicred[6];
214
+
215
+           if ($sinchKeyId && $sinchKeySecret) {
216
+
217
+               // Use Sinch
218
+	       $sinchProjectId = $thisapicred[4];
219
+	       $sinchServiceId = $thisapicred[7];
220
+
221
+	       $nbquery = [ "pageSize" => "100", "page" => "string" ];
222
+
223
+	       $curl = curl_init();
224
+
225
+	       curl_setopt_array($curl, [
226
+		    CURLOPT_HTTPHEADER => [ "Authorization: Basic " . base64_encode("" . $sinchKeyId . ":" . $sinchKeySecret ."") ],
227
+                    CURLOPT_PORT => "",
228
+		    CURLOPT_URL => "https://fax.api.sinch.com/v3/projects/" . $sinchProjectId . "/services/" . $sinchServiceId . "/numbers?" . http_build_query($nbquery),
229
+		    CURLOPT_RETURNTRANSFER => true,
230
+		    CURLOPT_CUSTOMREQUEST => "GET",
231
+		]);
232
+
233
+		$responsesnb = curl_exec($curl);
234
+		$errorsnb = curl_error($curl);
235
+
236
+		curl_close($curl);
237
+
238
+		if ($errorsnb) { $this->logger->error('cURL Error #: '. $errorsnb . '.'); }
239
+
240
+                $sincharr = [];
241
+                $sinchnmbrs = json_decode($responsesnb, true);
242
+
243
+	        foreach ($sinchnmbrs['numbers'] as $snkey => $snvalue) {
244
+	                 if (is_array($snvalue)) {
245
+	                        foreach ($snvalue as $snkeysec => $snvaluesec) {
246
+	                               if ($snkeysec == 'phoneNumber') {
247
+	                                   $sincharr[] = $snvaluesec;
248
+	                               }
249
+	                        }
250
+	                 }
251
+	        }
252
+
253
+                $sinchnbdata = ["nblist" => $sincharr, "provider" => "sinch"];
254
+
255
+		return $sinchnbdata;
256
+
257
+           } else {
258
+
259
+                // Use Phaxio
260
+		$apiMode = 'live';
261
+		$apiKeys[$apiMode] = $thisapicred[0];
262
+		$apiSecrets[$apiMode] = $thisapicred[1];
263
+
264
+		$apiHost = 'https://api.phaxio.com/v2.1/';
265
+
266
+		$phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
267
+
268
+		try {
269
+	            // Get all Phaxio fax numbers
270
+	            $phaxioresultfour = $phaxio->doRequest("GET", 'phone_numbers');
271
+
272
+	            $phaxionbstoarr = $this->object_to_array($phaxioresultfour);
273
+
274
+	            $phaxioarr = [];
275
+
276
+	            foreach ($phaxionbstoarr['data'] as $phkey => $phvalue) {
277
+	                   if (is_array($phvalue)) {
278
+	                         foreach ($phvalue as $phkeysec => $phvaluesec) {
279
+	                               if ($phkeysec == 'phone_number') {
280
+	                                   $phaxioarr[] = $phvaluesec;
281
+	                               }
282
+	                         }
283
+	                   }
284
+	            }
285
+
286
+		} catch (InvalidRequestException $e) {
287
+	            $phaxiobalance = 'unknown';
288
+	            $phaxiosuccess = 'false';
289
+	            $errortype = 'invalid request error';
290
+		} catch (AuthenticationException $e) {
291
+	            $phaxiobalance = 'unknown';
292
+	            $phaxiosuccess = 'false';
293
+	            $errortype = 'authentication error';
294
+		} catch (APIConnectionException $e) {
295
+	            $phaxiobalance = 'unknown';
296
+	            $phaxiosuccess = 'false';
297
+	            $errortype = 'API connection error';
298
+		} catch (RateLimitException $e) {
299
+	            $phaxiobalance = 'unknown';
300
+	            $phaxiosuccess = 'false';
301
+	            $errortype = 'rate limit error';
302
+		} catch (NotFoundException $e) {
303
+	            $phaxiobalance = 'unknown';
304
+	            $phaxiosuccess = 'false';
305
+	            $errortype = 'not found error';
306
+		} catch (GeneralException $e) {
307
+	            $phaxiobalance = 'unknown';
308
+	            $phaxiosuccess = 'false';
309
+	            $errortype = 'undefined error';
310
+		}
311
+
312
+                $phaxionbdata = ["nblist" => $phaxioarr, "provider" => "phaxio"];
313
+
314
+		return $phaxionbdata;
315
+           }
316
+        
317
+    }
318
+
319
+
320
+    /**
321
+     * @NoAdminRequired
322
+     */
323
+    public function uploadfile($uploadfileforfax) {
324
+
325
+           $fileContent = file_get_contents($_FILES['uploadfileforfax']['tmp_name']);
326
+           $fileName = $_FILES['uploadfileforfax']['name'];
327
+           $fileSizeinit = $_FILES['uploadfileforfax']['size'];
328
+           $fileSize = $fileSizeinit / 1048576;
329
+
330
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
331
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
332
+           }
333
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
334
+               $this->folder->newFolder('Pax_Fax/faxes_received');
335
+           }
336
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
337
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
338
+           }
339
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
340
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
341
+           }
342
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
343
+               $this->folder->newFolder('Pax_Fax/temp_files');
344
+           }
345
+
346
+           $userroot = $this->view->getRoot();
347
+           $targetfile = $userroot . "/Pax_Fax/temp_files/" . $fileName;
348
+
349
+           $target = $this->folder->newFile($targetfile);
350
+           $target->putContent($fileContent);
351
+
352
+           // Get the cumulative files size of the uploaded files
353
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
354
+           $targetdir = $datadir . $this->userSession->getUser()->getUID() . "/files/Pax_Fax/temp_files";
355
+
356
+           $fileSystemIterator = new FilesystemIterator($targetdir);
357
+
358
+           $dirfiles = [];
359
+           foreach ($fileSystemIterator as $fileInfo){
360
+                    $dirfiles[] = $fileInfo->getFilename();
361
+           }
362
+
363
+           $totalflsizeinit = 0;
364
+           foreach ($dirfiles as $key => $indfile) {
365
+                    $fileSizeinit = $this->filesystem->filesize($userroot . "/Pax_Fax/temp_files/" . $indfile);
366
+                    $mbSize = $fileSizeinit / 1048576;
367
+                    $totalflsizeinit += $mbSize;
368
+           }
369
+
370
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
371
+
372
+           return $totalflsize;
373
+    }
374
+
375
+    /**
376
+     * @NoAdminRequired
377
+     */
378
+    public function pickfile($path) {
379
+
380
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent') == false) {
381
+               $this->folder->newFolder('Pax_Fax/faxes_sent');
382
+           }
383
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
384
+               $this->folder->newFolder('Pax_Fax/faxes_received');
385
+           }
386
+           if ($this->folder->nodeExists('Pax_Fax/faxes_sent_failed') == false) {
387
+               $this->folder->newFolder('Pax_Fax/faxes_sent_failed');
388
+           }
389
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
390
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
391
+           }
392
+           if ($this->folder->nodeExists('Pax_Fax/temp_files') == false) {
393
+               $this->folder->newFolder('Pax_Fax/temp_files');
394
+           }
395
+
396
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
397
+
398
+           $fltgt = $datadir . $this->userSession->getUser()->getUID() . "/files" . $path;
399
+
400
+           $fileContentpk = file_get_contents($fltgt);
401
+
402
+           $patharr = explode("/", $path);
403
+
404
+           $revarr = array_reverse($patharr);
405
+
406
+           $relflpath = "/Pax_Fax/temp_files/" . $revarr[0]; 
407
+
408
+           $target = $this->folder->newFile($relflpath);
409
+
410
+           $target->putContent($fileContentpk);
411
+
412
+           $targetdir = $datadir . $this->userSession->getUser()->getUID() . "/files/Pax_Fax/temp_files";
413
+
414
+           $fileSystemIterator = new FilesystemIterator($targetdir);
415
+
416
+           $dirfiles = [];
417
+           foreach ($fileSystemIterator as $fileInfo) {
418
+                    $dirfiles[] = $fileInfo->getFilename();
419
+           }
420
+
421
+           $totalflsizeinit = 0;
422
+
423
+           foreach ($dirfiles as $key => $indfile) {
424
+
425
+                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
426
+                    $mbSize = $fileSizeinit / 1048576;
427
+                    $totalflsizeinit += $mbSize;
428
+           }
429
+
430
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
431
+
432
+           return $totalflsize;
433
+    }
434
+
435
+    /**
436
+     * @NoAdminRequired
437
+     */
438
+    public function removeupfile($removedfilename) {
439
+
440
+           $tmpfl = "/" . $this->userSession->getUser()->getUID() . "/files/Pax_Fax/temp_files/" . $removedfilename;
441
+
442
+           $removefile = $this->view->unlink($tmpfl);
443
+
444
+           // Get the cumulative files size of the uploaded files
445
+           $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
446
+           $targetdir = $datadir . $this->userSession->getUser()->getUID() . "/files/Pax_Fax/temp_files";
447
+           $fileSystemIterator = new FilesystemIterator($targetdir);
448
+
449
+           $dirfiles = [];
450
+           foreach ($fileSystemIterator as $fileInfo) {
451
+                    $dirfiles[] = $fileInfo->getFilename();
452
+           }
453
+
454
+           $totalflsizeinit = 0;
455
+
456
+           foreach ($dirfiles as $key => $indfile) {
457
+                    $fileSizeinit = $this->filesystem->filesize("/Pax_Fax/temp_files/" . $indfile);
458
+                    $mbSize = $fileSizeinit / 1048576;
459
+                    $totalflsizeinit += $mbSize;
460
+           }
461
+
462
+           $totalflsize = number_format($totalflsizeinit, 2) . ' MB';
463
+
464
+           return $totalflsize;
465
+    }
466
+
467
+    /**
468
+     * @NoAdminRequired
469
+     */
470
+    public function getpickedfile($pickedfilename) {
471
+
472
+           $thisuserroot = $this->view->getRoot();
473
+
474
+           $temppickedfile = $thisuserroot . "/Pax_Fax/temp_files/" . $pickedfilename;
475
+           $getfilecontent = $this->filesystem->file_get_contents($temppickedfile);
476
+
477
+           $namesplit = explode(".", $pickedfilename);
478
+           $extension = end($namesplit);
479
+
480
+           if ($extension == "txt" || $extension == "html") {
481
+               $getpickedfile = $getfilecontent;
482
+           } elseif ($extension == "jpg") {
483
+               $getpickedfile = 'data:image/jpeg;base64,' . base64_encode($getfilecontent);
484
+           } elseif ($extension == "png") {
485
+               $getpickedfile = 'data:image/png;base64,' . base64_encode($getfilecontent);
486
+           } else { $getpickedfile = ""; }
487
+
488
+           return $getpickedfile;
489
+    }
490
+
491
+    /**
492
+     * @NoAdminRequired
493
+     */
494
+    public function sendfax($uploadedtofax, $selectedcid, $toNumber) {
495
+
496
+           $tonumbertr = str_replace("+", "", $toNumber[0]);
497
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
498
+           $fromnumberdigits = str_replace("+", "", $selectedcid);
499
+
500
+           if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $selectedcid; } else { $fromnumber = ''; }
501
+
502
+           $thisapicred = $this->service->getapicredentials($this->userSession->getUser()->getUID());
503
+
504
+           $sinchKeyId = $thisapicred[5];
505
+           $sinchKeySecret = $thisapicred[6];
506
+
507
+           if ($sinchKeyId && $sinchKeySecret) {
508
+
509
+               // Use Sinch
510
+	       $sinchProjectId = $thisapicred[4];
511
+
512
+               $countfaxfiles = count($uploadedtofax);
513
+
514
+               $openedfiles = [];
515
+               $crtuser = $this->userSession->getUser()->getUID();
516
+
517
+               foreach ($uploadedtofax as $key => $flname) {
518
+
519
+                    if ($key == 0) { $firstflname = $flname; }
520
+
521
+                    $fileNamesec = array_reverse(explode(".", $flname));
522
+                    $filenameext = $fileNamesec[0];
523
+
524
+                    array_shift($fileNamesec);
525
+
526
+                    $fileName = implode("", $fileNamesec);
527
+
528
+                    if ($countfaxfiles == 1) {
529
+
530
+                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
531
+
532
+                    } else {
533
+
534
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
535
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
536
+                         }
537
+
538
+                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
539
+                    }
540
+
541
+                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
542
+
543
+                    $fltgtsnd = $datadir . $crtuser . "/files/Pax_Fax/temp_files/" . $flname;
544
+                    $fltgtsndtmp = "/Pax_Fax/temp_files/" . $flname;
545
+
546
+                    $fileContentsnd = file_get_contents($fltgtsnd);
547
+
548
+                    $target = $this->folder->newFile($targetfile);
549
+                    $target->putContent($fileContentsnd);
550
+
551
+                    $removetmpfile = $this->filesystem->unlink($fltgtsndtmp);
552
+
553
+                    if ($countfaxfiles == 1) {
554
+                        $openedfiles[] = [ "file" => base64_encode(file_get_contents($datadir . $crtuser . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext)), "fileType" => $filenameext ];
555
+                    } else {
556
+                        $openedfiles[] = [ "file" => base64_encode(file_get_contents($datadir . $crtuser . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext)), "fileType" => $filenameext ];
557
+                    }
558
+               }
559
+
560
+               $toNumbersn = [];
561
+               foreach ($toNumber as $nbkey => $nbvalue) { $toNumbersn[] = "+" . $nbvalue; }
562
+               $toNumbersnfin = implode(";", $toNumbersn);
563
+
564
+	       $datatosendsn = ["to" => $toNumbersnfin, "files" => $openedfiles];
565
+
566
+	       $postedparamsn = json_encode($datatosendsn);
567
+
568
+	       $chsn = curl_init();
569
+               curl_setopt($chsn, CURLOPT_URL, "https://" . $sinchKeyId . ":" . $sinchKeySecret . "@fax.api.sinch.com/v3/projects/" . $sinchProjectId . "/faxes");
570
+	       curl_setopt($chsn, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
571
+	       curl_setopt($chsn, CURLOPT_TIMEOUT, 300);
572
+	       curl_setopt($chsn, CURLOPT_RETURNTRANSFER, 1);
573
+	       curl_setopt($chsn, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
574
+	       curl_setopt($chsn, CURLOPT_POST, 1);
575
+	       curl_setopt($chsn, CURLOPT_POSTFIELDS, $postedparamsn);
576
+	       $resultsn = curl_exec($chsn);
577
+	       $statusCodesn = curl_getinfo($chsn, CURLINFO_HTTP_CODE);
578
+	       curl_close($chsn);
579
+
580
+	       $decresultsn = json_decode($resultsn, true);
581
+
582
+	       if (in_array($statusCodesn, [400, 401, 402, 404, 422, 429, 500, 800])) {
583
+                   $sinchsuccess = false;
584
+                   $sincherrortype = "Error: " . $decresultsn['code'] . " . Message: " . $decresultsn['message'];
585
+               } else { $sinchsuccess = true; $sincherrortype = ''; }
586
+
587
+               $sinchresult = [ 'success' => $sinchsuccess, 'errortype' => $sincherrortype ];
588
+
589
+               if ($sinchsuccess != true) {
590
+
591
+                    foreach ($uploadedtofax as $key => $flname) {
592
+
593
+                        $fileNamesec = array_reverse(explode(".", $flname));
594
+                        $filenameext = $fileNamesec[0];
595
+                        array_shift($fileNamesec);
596
+
597
+                        $fileName = implode("", $fileNamesec);
598
+
599
+                        if ($countfaxfiles == 1) {
600
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
601
+                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
602
+                        } else {
603
+
604
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
605
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
606
+                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
607
+                        }
608
+
609
+                        $fileContentfld = $this->filesystem->file_get_contents($failedfl);
610
+
611
+                        $targetact = $this->folder->newFile($newtargetfl);
612
+                        $targetact->putContent($fileContentfld);
613
+
614
+                        $removefailed = $this->filesystem->unlink($failedfl);
615
+                    }
616
+
617
+                }
618
+
619
+                return $sinchresult;
620
+
621
+           } else {
622
+
623
+               // Use Phaxio
624
+               if ($selectedcid != '' && $selectedcid != 'click refresh button') { $fromnumber = $fromnumberdigits; } else { $fromnumber = 'nocallerid'; }
625
+
626
+               $countfaxfiles = count($uploadedtofax);
627
+
628
+               $openedfiles = [];
629
+               $crtuser = $this->userSession->getUser()->getUID();
630
+
631
+               foreach ($uploadedtofax as $key => $flname) {
632
+
633
+                    if ($key == 0) { $firstflname = $flname; }
634
+
635
+                    $fileNamesec = array_reverse(explode(".", $flname));
636
+                    $filenameext = $fileNamesec[0];
637
+                    array_shift($fileNamesec);
638
+
639
+                    $fileName = implode("", $fileNamesec);
640
+
641
+                    if ($countfaxfiles == 1) {
642
+                        $targetfile = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_"  . $tonumbertr . "_" . $fldate . "." . $filenameext;
643
+                    } else {
644
+                         if ($this->folder->nodeExists("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate) == false) {
645
+                             $this->folder->newFolder("Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
646
+                         }
647
+
648
+                         $targetfile = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
649
+                    }
650
+
651
+                    $datadir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/';
652
+
653
+                    $fltgtsnd = $datadir . $crtuser . "/files/Pax_Fax/temp_files/" . $flname;
654
+                    $fltgtsndtmp = "/Pax_Fax/temp_files/" . $flname;
655
+
656
+                    $fileContentsnd = file_get_contents($fltgtsnd);
657
+
658
+                    $target = $this->folder->newFile($targetfile);
659
+                    $target->putContent($fileContentsnd);
660
+
661
+                    $removetmpfile = $this->filesystem->unlink($fltgtsndtmp);
662
+
663
+                    if ($countfaxfiles == 1) {
664
+                        $openedfiles[] = fopen($datadir . $crtuser . "/files/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
665
+                    } else {
666
+                        $openedfiles[] = fopen($datadir . $crtuser . "/files/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext, "r");
667
+                    }
668
+               }
669
+
670
+               $params = array(
671
+                           'to' => $toNumber,
672
+                           'file' => $openedfiles,
673
+                           'caller_id' => $selectedcid
674
+                         );
675
+
676
+               $apiMode = 'live';
677
+
678
+               $apiKeys[$apiMode] = $thisapicred[0];
679
+               $apiSecrets[$apiMode] = $thisapicred[1];
680
+
681
+               $apiHost = 'https://api.phaxio.com/v2.1/';
682
+
683
+               $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
684
+
685
+               try {
686
+                    $phaxioresultinit = $phaxio->sendFax($params);
687
+
688
+                    $phaxioresultsec = $phaxio->doRequest("GET", 'faxes/' . urlencode((string)$phaxioresultinit['id']));
689
+
690
+                    $statustoarr = $this->object_to_array($phaxioresultsec);
691
+
692
+                    $phaxiosuccess = $statustoarr['success'];
693
+
694
+                    $errortype = 'there are no errors';
695
+
696
+               } catch (InvalidRequestException $e) {
697
+                    $phaxiosuccess = 'false';
698
+                    $errortype = 'invalid request error';
699
+               } catch (AuthenticationException $e) {
700
+                    $phaxiosuccess = 'false';
701
+                    $errortype = 'authentication error';
702
+               } catch (APIConnectionException $e) {
703
+                    $phaxiosuccess = 'false';
704
+                    $errortype = 'API connection error';
705
+               } catch (RateLimitException $e) {
706
+                    $phaxiosuccess = 'false';
707
+                    $errortype = 'rate limit error';
708
+               } catch (NotFoundException $e) {
709
+                    $phaxiosuccess = 'false';
710
+                    $errortype = 'not found error';
711
+               } catch (GeneralException $e) {
712
+                    $phaxiosuccess = 'false';
713
+                    $errortype = 'undefined error';
714
+               }
715
+
716
+               $phaxioresult = ['success' => $phaxiosuccess, 'errortype' => $errortype];
717
+
718
+               if ($phaxiosuccess != 'true') {
719
+
720
+                    foreach ($uploadedtofax as $key => $flname) {
721
+
722
+                        $fileNamesec = array_reverse(explode(".", $flname));
723
+                        $filenameext = $fileNamesec[0];
724
+                        array_shift($fileNamesec);
725
+
726
+                        $fileName = implode("", $fileNamesec);
727
+
728
+                        if ($countfaxfiles == 1) {
729
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
730
+                            $newtargetfl =  "/Pax_Fax/faxes_sent_failed/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
731
+                        } else {
732
+
733
+                            $failedfl = "/Pax_Fax/faxes_sent/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
734
+                            $this->folder->newFolder("Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate);
735
+                            $newtargetfl = "/Pax_Fax/faxes_sent_failed/" . $firstflname . "_et_al_" . $fromnumber . "_" . $tonumbertr . "_" . $fldate . "/" . $fileName . "_" . $tonumbertr . "_" . $fldate . "." . $filenameext;
736
+                        }
737
+
738
+                        $fileContentfld = $this->filesystem->file_get_contents($failedfl);
739
+
740
+                        $targetact = $this->folder->newFile($newtargetfl);
741
+                        $targetact->putContent($fileContentfld);
742
+
743
+                        $removefailed = $this->filesystem->unlink($failedfl);
744
+                    }
745
+
746
+               }
747
+
748
+               return $phaxioresult;
749
+           }
750
+    }
751
+
752
+
753
+    /**
754
+     * @NoAdminRequired
755
+     */
756
+    public function getsettings() {
757
+           return $this->service->getsettings($this->userSession->getUser()->getUID());
758
+    }
759
+
760
+
761
+    /**
762
+     * @NoAdminRequired
763
+     */
764
+    public function updatesettings($apiKey, $apiSecret, $webhookToken, $receiveUrl, $sinchProjectId, $sinchKeyId, $sinchKeySecret, $sinchServiceId, $sinchCallbackUrl, $getNotification, $notificationEmail) {
765
+           return $this->service->updatesettings($this->userSession->getUser()->getUID(), $apiKey, $apiSecret, $webhookToken, $receiveUrl, $sinchProjectId, $sinchKeyId, $sinchKeySecret, $sinchServiceId, $sinchCallbackUrl, $getNotification, $notificationEmail);
766
+    }
767
+
768
+}
Browse code

removed appinfo/info.xml appinfo/signature.json appinfo/routes.php CHANGELOG.txt templates/navigation/index.php templates/settings.php css/style.css js/settings.js js/sendfax.js lib/Controller/AuthorApiController.php lib/Controller/PaxfaxController.php lib/Service/PaxfaxService.php img/pax_fax_received_fax.png img/pax_fax_send_uploaded_file_as_fax.png l10n/en_GB.js l10n/en_US.js l10n/en_GB.json l10n/en_US.json

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

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

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

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

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

added appinfo/info.xml appinfo/signature.json appinfo/routes.php CHANGELOG.txt js/sendfax.js lib/Controller/PaxfaxController.php templates/settings.php l10n/en_GB.js l10n/en_GB.json l10n/en_US.js l10n/en_US.json lib/Migration/Version116Date20240527235146.php

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

removed appinfo/info.xml appinfo/signature.json appinfo/routes.php CHANGELOG.txt js/sendfax.js lib/Controller/PaxfaxController.php templates/settings.php l10n/en_GB.js l10n/en_GB.json l10n/en_US.js l10n/en_US.json

DoubleBastionAdmin authored on 27/05/2024 20:46:57
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,602 +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
-           $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

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
+}