Browse code

added CHANGELOG.txt appinfo/info.xml appinfo/signature.json js/sendfax.js l10n/en_GB.js l10n/en_GB.json lib/Controller/AuthorApiController.php lib/AppInfo/Application.php lib/Notification/Notifier.php l10n/en_US.js l10n/en_US.json

DoubleBastionAdmin authored on 02/11/2023 18:51:43
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,300 @@
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\AppFramework\ApiController;
29
+use OCP\IRequest;
30
+use OCP\AppFramework\Http\DataResponse;
31
+use OCP\AppFramework\OCSController;
32
+use OCP\IUserSession;
33
+use OCP\AppFramework\Controller;
34
+use OCP\Files\IAppData;
35
+use OCA\PaxFax\Service\PaxfaxService;
36
+use OCP\AppFramework\App;
37
+use OCP\Files\NotPermittedException;
38
+use OCP\Files\Folder;
39
+use OC\Files\Filesystem;
40
+use \ReflectionClass;
41
+use OCP\Notification;
42
+use OCP\Notification\INotification;
43
+use OCP\Notification\IManager;
44
+use OCP\Notification\IAction;
45
+use \DateTime;
46
+
47
+use Phaxio;
48
+use Phaxio\OperationResult;
49
+use Phaxio\Error\AuthenticationException;
50
+use Phaxio\Error\NotFoundException;
51
+use Phaxio\Error\InvalidRequestException;
52
+use Phaxio\Error\RateLimitException;
53
+use Phaxio\Error\APIConnectionException;
54
+use Phaxio\Error\GeneralException;
55
+
56
+
57
+class AuthorApiController extends ApiController {
58
+
59
+    private $service;
60
+    private $userId;
61
+    private $folder;
62
+    private $filesystem;
63
+    private $userSession;
64
+    private $manager;
65
+
66
+    public function __construct($appName, IRequest $request, IUserSession $userSession, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId, IManager $manager) {
67
+        parent::__construct(
68
+                $appName,
69
+                $request,
70
+                'PUT, POST, GET, DELETE, PATCH',
71
+                'Authorization, Content-Type, Accept',
72
+                1728000);
73
+
74
+        $this->service = $service;
75
+        $this->userId = $userId;
76
+        $this->folder = $folder;
77
+        $this->filesystem = $filesystem;
78
+        $this->userSession = $userSession;
79
+	$this->manager = $manager;
80
+    }
81
+
82
+    /**
83
+     * @NoAdminRequired
84
+     */
85
+    public function object_to_array($obj) {
86
+        if(is_object($obj)) $obj = (array)$this->dismount($obj);
87
+        if(is_array($obj)) {
88
+           $new = array();
89
+           foreach($obj as $key => $val) {
90
+               $new[$key] = $this->object_to_array($val);
91
+           }
92
+        }
93
+        else $new = $obj;
94
+        return $new;
95
+    }
96
+
97
+    /**
98
+     * @NoAdminRequired
99
+     */
100
+    public function dismount($object) {
101
+        $reflectionClass = new ReflectionClass(get_class($object));
102
+        $array = array();
103
+        foreach ($reflectionClass->getProperties() as $property) {
104
+           $property->setAccessible(true);
105
+           $array[$property->getName()] = $property->getValue($object);
106
+           $property->setAccessible(false);
107
+        }
108
+        return $array;
109
+    }
110
+
111
+    /**
112
+     * @NoAdminRequired
113
+     */
114
+    public function isValidCallbackRequest($token, $url = null, $postParameters = null, $uploadedFiles = null, $signature = null) {
115
+
116
+	if (!$url) {
117
+	    $url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
118
+	}
119
+
120
+	if (!$postParameters) {
121
+	    $postParameters = $_REQUEST;
122
+	}
123
+
124
+	if (!$uploadedFiles) {
125
+	    $uploadedFiles = $_FILES;
126
+	}
127
+
128
+	if (!$signature) {
129
+	    $signature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
130
+	}
131
+
132
+	// Sort the array by keys
133
+	ksort($postParameters);
134
+
135
+	// Append the data array to the url string, with no delimiters
136
+	foreach ($postParameters as $key => $value) {
137
+		 $url .= $key . $value;
138
+	}
139
+
140
+	foreach ($uploadedFiles as $key => $value) {
141
+		 $url .= $key . sha1_file($value['tmp_name']);
142
+	}
143
+
144
+	$hmac = hash_hmac("sha1", $url, $token);
145
+	return $signature == $hmac;
146
+    }
147
+
148
+    /**
149
+     * @NoAdminRequired
150
+     * @NoCSRFRequired
151
+     */
152
+    public function receivefaxphaxio() {
153
+
154
+       $phaxiosignature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
155
+
156
+       $thisapicred = $this->service->getapicredentials($this->userId);
157
+       $phaxiowhtoken = $thisapicred[2];
158
+       $phaxiorecURL = $thisapicred[3];
159
+
160
+       // Verify Phaxio's signature
161
+       $signverify = $this->isValidCallbackRequest($phaxiowhtoken, $phaxiorecURL, null, null, $phaxiosignature);
162
+
163
+       if ($signverify) {
164
+
165
+           $fileContent = file_get_contents($_FILES['file']['tmp_name']);
166
+           $fileNameinit = $_FILES['file']['name'];
167
+
168
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
169
+
170
+           $gmtind = "UTC " . date('P');
171
+           $flmsdateinit = date("Y-m-d  H:i:s");
172
+           $flmsdate = $flmsdateinit . " " . $gmtind;
173
+
174
+           $fileNamesec = array_reverse(explode(".", $fileNameinit));
175
+           $filenameext = $fileNamesec[0];
176
+           array_shift($fileNamesec);
177
+
178
+           $fileName = implode("", $fileNamesec);
179
+
180
+           $faxid = str_replace("Fax-", "", $fileName);
181
+
182
+           $apiMode = 'live';
183
+
184
+           $apiKeys[$apiMode] = $thisapicred[0];
185
+           $apiSecrets[$apiMode] = $thisapicred[1];
186
+
187
+           $apiHost = 'https://api.phaxio.com/v2.1/';
188
+
189
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
190
+
191
+
192
+           try {
193
+                      $phaxioresultreq = $phaxio->doRequest("GET", 'faxes/' . $faxid);
194
+
195
+                      $fromnmbrtoarr = $this->object_to_array($phaxioresultreq);
196
+
197
+                      $phaxiofromraw = $fromnmbrtoarr['data']['from_number'];
198
+                      $phaxiotoraw  = $fromnmbrtoarr['data']['to_number'];
199
+                      $phaxiofromnumber = str_replace("+", "", $phaxiofromraw);
200
+                      $phaxiotonumber = str_replace("+", "", $phaxiotoraw);
201
+                      $errortype = 'there are no errors';
202
+
203
+           } catch (InvalidRequestException $e) {
204
+                      $phaxiofromnumber = 'null';
205
+                      $errortype = 'invalid request error';
206
+           } catch (AuthenticationException $e) {
207
+                      $phaxiofromnumber = 'null';
208
+                      $errortype = 'authentication error';
209
+           } catch (APIConnectionException $e) {
210
+                      $phaxiofromnumber = 'null';
211
+                      $errortype = 'API connection error';
212
+           } catch (RateLimitException $e) {
213
+                      $phaxiofromnumber = 'null';
214
+                      $errortype = 'rate limit error';
215
+           } catch (NotFoundException $e) {
216
+                      $phaxiofromnumber = 'null';
217
+                      $errortype = 'not found error';
218
+           } catch (GeneralException $e) {
219
+                      $phaxiofromnumber = 'null';
220
+                      $errortype = 'undefined error';
221
+           }
222
+
223
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
224
+               $this->folder->newFolder('Pax_Fax/faxes_received');
225
+           }
226
+
227
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
228
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
229
+           }
230
+
231
+           $targetfile = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber  . "_" . $fldate . "." . $filenameext;
232
+
233
+           $target = $this->folder->newFile($targetfile);
234
+
235
+           $target->putContent($fileContent);
236
+
237
+           // Move failed received faxes to the 'faxes_received_failed' folder
238
+           if ($filenameext == '') {
239
+
240
+               $newtargetfl =  "/Pax_Fax/faxes_received_failed/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
241
+               $targetact = $this->folder->newFile($newtargetfl);
242
+
243
+               $targetact->putContent($fileContent);
244
+
245
+               $failedfl = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
246
+               $removefailed = $this->filesystem->unlink($failedfl);
247
+           }
248
+
249
+           // Send notifications
250
+           $nameofhost = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'];
251
+           $getnextnotify = $thisapicred[4];
252
+           $useremailaddr = $thisapicred[5];
253
+
254
+           if ($filenameext != '') {
255
+               $targetflname = $targetfile;
256
+               $targettrim = "/Pax_Fax/faxes_received/";
257
+               $validfaxparam = "fax";
258
+               $setsubject = "New fax received";
259
+           } else {
260
+               $targetflname = $newtargetfl;
261
+               $targettrim = "/Pax_Fax/faxes_received_failed/";
262
+               $validfaxparam = "failed fax";
263
+               $setsubject = "New failed fax received";
264
+           }
265
+
266
+           if ($useremailaddr != '') {
267
+
268
+               $subject = $setsubject;
269
+               $message = "Hello! <br><br> You have received a new ".$validfaxparam." on ".$flmsdate." .<br> The new ".$validfaxparam." file is '".$targetflname."' . <br><br>____________<br><br> Yours, <br> Pax Fax <br> A fax application for Nextcloud <br> Host: '".$nameofhost."' <br>";
270
+
271
+               $messagefin = chunk_split(base64_encode($message));
272
+
273
+               $headers = "MIME-Version: 1.0" . "\r\n";
274
+               $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
275
+               $headers .= "Content-Transfer-Encoding: base64" . "\r\n";
276
+
277
+               // Set the email sender
278
+               $headers .= "From: " . $useremailaddr . "\r\n";
279
+               $headers .= "Reply-To: " . $useremailaddr . "\r\n";
280
+
281
+               mail($useremailaddr, $subject, $messagefin, $headers);
282
+           }
283
+
284
+           if ($getnextnotify != 0 ) {
285
+
286
+	       $notificationph = $this->manager->createNotification();
287
+	       $notificationph->setApp('pax_fax')
288
+			      ->setUser($this->userId)
289
+			      ->setDateTime(new \DateTime())
290
+			      ->setObject('pax_fax', '12')
291
+			      ->setSubject('New fax');
292
+	       $this->manager->notify($notificationph);
293
+          }
294
+
295
+       } else { return "access denied"; }
296
+
297
+    }
298
+
299
+}
300
+
Browse code

CHANGELOG.txt appinfo/info.xml appinfo/signature.json js/sendfax.js l10n/en_GB.js l10n/en_GB.json lib/Controller/AuthorApiController.php lib/AppInfo/Application.php

DoubleBastionAdmin authored on 02/11/2023 18:44:01
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,292 +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\AppFramework\ApiController;
29
-use OCP\IRequest;
30
-use OCP\AppFramework\Http\DataResponse;
31
-use OCP\AppFramework\OCSController;
32
-use OCP\IUserSession;
33
-use OCP\AppFramework\Controller;
34
-use OCP\Files\IAppData;
35
-use OCA\PaxFax\Service\PaxfaxService;
36
-use OCP\AppFramework\App;
37
-use OCP\Files\NotPermittedException;
38
-use OCP\Files\Folder;
39
-use OC\Files\Filesystem;
40
-use \ReflectionClass;
41
-use OCP\Notification;
42
-use OCP\Notification\INotification;
43
-use OCP\Notification\IManager;
44
-use OCP\Notification\IAction;
45
-use \DateTime;
46
-
47
-use Phaxio;
48
-use Phaxio\OperationResult;
49
-use Phaxio\Error\AuthenticationException;
50
-use Phaxio\Error\NotFoundException;
51
-use Phaxio\Error\InvalidRequestException;
52
-use Phaxio\Error\RateLimitException;
53
-use Phaxio\Error\APIConnectionException;
54
-use Phaxio\Error\GeneralException;
55
-
56
-
57
-class AuthorApiController extends ApiController {
58
-
59
-    private $service;
60
-    private $userId;
61
-    private $folder;
62
-    private $filesystem;
63
-    private $userSession;
64
-
65
-    public function __construct($appName, IRequest $request, IUserSession $userSession, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId) {
66
-        parent::__construct(
67
-                $appName,
68
-                $request,
69
-                'PUT, POST, GET, DELETE, PATCH',
70
-                'Authorization, Content-Type, Accept',
71
-                1728000);
72
-
73
-        $this->service = $service;
74
-        $this->userId = $userId;
75
-        $this->folder = $folder;
76
-        $this->filesystem = $filesystem;
77
-        $this->userSession = $userSession;
78
-    }
79
-
80
-    /**
81
-     * @NoAdminRequired
82
-     */
83
-    public function object_to_array($obj) {
84
-        if(is_object($obj)) $obj = (array)$this->dismount($obj);
85
-        if(is_array($obj)) {
86
-           $new = array();
87
-           foreach($obj as $key => $val) {
88
-               $new[$key] = $this->object_to_array($val);
89
-           }
90
-        }
91
-        else $new = $obj;
92
-        return $new;
93
-    }
94
-
95
-    /**
96
-     * @NoAdminRequired
97
-     */
98
-    public function dismount($object) {
99
-        $reflectionClass = new ReflectionClass(get_class($object));
100
-        $array = array();
101
-        foreach ($reflectionClass->getProperties() as $property) {
102
-           $property->setAccessible(true);
103
-           $array[$property->getName()] = $property->getValue($object);
104
-           $property->setAccessible(false);
105
-        }
106
-        return $array;
107
-    }
108
-
109
-    /**
110
-     * @NoAdminRequired
111
-     */
112
-    public function isValidCallbackRequest($token, $url = null, $postParameters = null, $uploadedFiles = null, $signature = null) {
113
-
114
-	if (!$url) {
115
-	    $url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
116
-	}
117
-
118
-	if (!$postParameters) {
119
-	    $postParameters = $_REQUEST;
120
-	}
121
-
122
-	if (!$uploadedFiles) {
123
-	    $uploadedFiles = $_FILES;
124
-	}
125
-
126
-	if (!$signature) {
127
-	    $signature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
128
-	}
129
-
130
-	// Sort the array by keys
131
-	ksort($postParameters);
132
-
133
-	// Append the data array to the url string, with no delimiters
134
-	foreach ($postParameters as $key => $value) {
135
-		 $url .= $key . $value;
136
-	}
137
-
138
-	foreach ($uploadedFiles as $key => $value) {
139
-		 $url .= $key . sha1_file($value['tmp_name']);
140
-	}
141
-
142
-	$hmac = hash_hmac("sha1", $url, $token);
143
-	return $signature == $hmac;
144
-    }
145
-
146
-    /**
147
-     * @NoAdminRequired
148
-     * @NoCSRFRequired
149
-     */
150
-    public function receivefaxphaxio() {
151
-
152
-       $phaxiosignature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
153
-
154
-       $thisapicred = $this->service->getapicredentials($this->userId);
155
-       $phaxiowhtoken = $thisapicred[2];
156
-       $phaxiorecURL = $thisapicred[3];
157
-
158
-       // Verify Phaxio's signature
159
-       $signverify = $this->isValidCallbackRequest($phaxiowhtoken, $phaxiorecURL, null, null, $phaxiosignature);
160
-
161
-       if ($signverify) {
162
-
163
-           $fileContent = file_get_contents($_FILES['file']['tmp_name']);
164
-           $fileNameinit = $_FILES['file']['name'];
165
-
166
-           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
167
-
168
-           $gmtind = "UTC " . date('P');
169
-           $flmsdateinit = date("Y-m-d  H:i:s");
170
-           $flmsdate = $flmsdateinit . " " . $gmtind;
171
-
172
-           $fileNamesec = array_reverse(explode(".", $fileNameinit));
173
-           $filenameext = $fileNamesec[0];
174
-           array_shift($fileNamesec);
175
-
176
-           $fileName = implode("", $fileNamesec);
177
-
178
-           $faxid = str_replace("Fax-", "", $fileName);
179
-
180
-           $apiMode = 'live';
181
-
182
-           $apiKeys[$apiMode] = $thisapicred[0];
183
-           $apiSecrets[$apiMode] = $thisapicred[1];
184
-
185
-           $apiHost = 'https://api.phaxio.com/v2.1/';
186
-
187
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
188
-
189
-
190
-           try {
191
-                      $phaxioresultreq = $phaxio->doRequest("GET", 'faxes/' . $faxid);
192
-
193
-                      $fromnmbrtoarr = $this->object_to_array($phaxioresultreq);
194
-
195
-                      $phaxiofromraw = $fromnmbrtoarr['data']['from_number'];
196
-                      $phaxiotoraw  = $fromnmbrtoarr['data']['to_number'];
197
-                      $phaxiofromnumber = str_replace("+", "", $phaxiofromraw);
198
-                      $phaxiotonumber = str_replace("+", "", $phaxiotoraw);
199
-                      $errortype = 'there are no errors';
200
-
201
-           } catch (InvalidRequestException $e) {
202
-                      $phaxiofromnumber = 'null';
203
-                      $errortype = 'invalid request error';
204
-           } catch (AuthenticationException $e) {
205
-                      $phaxiofromnumber = 'null';
206
-                      $errortype = 'authentication error';
207
-           } catch (APIConnectionException $e) {
208
-                      $phaxiofromnumber = 'null';
209
-                      $errortype = 'API connection error';
210
-           } catch (RateLimitException $e) {
211
-                      $phaxiofromnumber = 'null';
212
-                      $errortype = 'rate limit error';
213
-           } catch (NotFoundException $e) {
214
-                      $phaxiofromnumber = 'null';
215
-                      $errortype = 'not found error';
216
-           } catch (GeneralException $e) {
217
-                      $phaxiofromnumber = 'null';
218
-                      $errortype = 'undefined error';
219
-           }
220
-
221
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
222
-               $this->folder->newFolder('Pax_Fax/faxes_received');
223
-           }
224
-
225
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
226
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
227
-           }
228
-
229
-           $targetfile = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber  . "_" . $fldate . "." . $filenameext;
230
-
231
-           $target = $this->folder->newFile($targetfile);
232
-
233
-           $target->putContent($fileContent);
234
-
235
-           // Move failed received faxes to the 'faxes_received_failed' folder
236
-           if ($filenameext == '') {
237
-
238
-               $newtargetfl =  "/Pax_Fax/faxes_received_failed/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
239
-               $targetact = $this->folder->newFile($newtargetfl);
240
-
241
-               $targetact->putContent($fileContent);
242
-
243
-               $failedfl = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
244
-               $removefailed = $this->filesystem->unlink($failedfl);
245
-           }
246
-
247
-           // Send notifications
248
-           $nameofhost = exec("hostname");
249
-           $getnextnotify = $thisapicred[4];
250
-           $useremailaddr = $thisapicred[5];
251
-
252
-           if ($filenameext != '') {
253
-               $targetflname = $targetfile;
254
-               $targettrim = "/Pax_Fax/faxes_received/";
255
-               $validfaxparam = "fax";
256
-               $setsubject = "New fax received";
257
-           } else {
258
-               $targetflname = $newtargetfl;
259
-               $targettrim = "/Pax_Fax/faxes_received_failed/";
260
-               $validfaxparam = "failed fax";
261
-               $setsubject = "New failed fax received";
262
-           }
263
-
264
-           if ($useremailaddr != '') {
265
-
266
-               $subject = $setsubject;
267
-               $message = "Hello! <br><br> You have received a new ".$validfaxparam." on ".$flmsdate." .<br> The new ".$validfaxparam." file is '".$targetflname."' . <br><br>____________<br><br> Yours, <br> Pax Fax <br> A fax application for Nextcloud <br> Host: '".$nameofhost."' <br>";
268
-
269
-               $messagefin = chunk_split(base64_encode($message));
270
-
271
-               $headers = "MIME-Version: 1.0" . "\r\n";
272
-               $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
273
-               $headers .= "Content-Transfer-Encoding: base64" . "\r\n";
274
-
275
-               // Set the email sender
276
-               $headers .= "From: " . $useremailaddr . "\r\n";
277
-               $headers .= "Reply-To: " . $useremailaddr . "\r\n";
278
-
279
-               mail($useremailaddr, $subject, $messagefin, $headers);
280
-           }
281
-
282
-           if ($getnextnotify != 0 ) {
283
-
284
-               $notify = exec("php ./occ notification:generate ".$this->userId." 'Pax Fax has received a new fax !' -l 'The new fax can be found in the \"".$targettrim."\" folder.'");
285
-          }
286
-
287
-       } else { return "access denied"; }
288
-
289
-    }
290
-
291
-}
292
-
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,292 @@
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\AppFramework\ApiController;
29
+use OCP\IRequest;
30
+use OCP\AppFramework\Http\DataResponse;
31
+use OCP\AppFramework\OCSController;
32
+use OCP\IUserSession;
33
+use OCP\AppFramework\Controller;
34
+use OCP\Files\IAppData;
35
+use OCA\PaxFax\Service\PaxfaxService;
36
+use OCP\AppFramework\App;
37
+use OCP\Files\NotPermittedException;
38
+use OCP\Files\Folder;
39
+use OC\Files\Filesystem;
40
+use \ReflectionClass;
41
+use OCP\Notification;
42
+use OCP\Notification\INotification;
43
+use OCP\Notification\IManager;
44
+use OCP\Notification\IAction;
45
+use \DateTime;
46
+
47
+use Phaxio;
48
+use Phaxio\OperationResult;
49
+use Phaxio\Error\AuthenticationException;
50
+use Phaxio\Error\NotFoundException;
51
+use Phaxio\Error\InvalidRequestException;
52
+use Phaxio\Error\RateLimitException;
53
+use Phaxio\Error\APIConnectionException;
54
+use Phaxio\Error\GeneralException;
55
+
56
+
57
+class AuthorApiController extends ApiController {
58
+
59
+    private $service;
60
+    private $userId;
61
+    private $folder;
62
+    private $filesystem;
63
+    private $userSession;
64
+
65
+    public function __construct($appName, IRequest $request, IUserSession $userSession, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId) {
66
+        parent::__construct(
67
+                $appName,
68
+                $request,
69
+                'PUT, POST, GET, DELETE, PATCH',
70
+                'Authorization, Content-Type, Accept',
71
+                1728000);
72
+
73
+        $this->service = $service;
74
+        $this->userId = $userId;
75
+        $this->folder = $folder;
76
+        $this->filesystem = $filesystem;
77
+        $this->userSession = $userSession;
78
+    }
79
+
80
+    /**
81
+     * @NoAdminRequired
82
+     */
83
+    public function object_to_array($obj) {
84
+        if(is_object($obj)) $obj = (array)$this->dismount($obj);
85
+        if(is_array($obj)) {
86
+           $new = array();
87
+           foreach($obj as $key => $val) {
88
+               $new[$key] = $this->object_to_array($val);
89
+           }
90
+        }
91
+        else $new = $obj;
92
+        return $new;
93
+    }
94
+
95
+    /**
96
+     * @NoAdminRequired
97
+     */
98
+    public function dismount($object) {
99
+        $reflectionClass = new ReflectionClass(get_class($object));
100
+        $array = array();
101
+        foreach ($reflectionClass->getProperties() as $property) {
102
+           $property->setAccessible(true);
103
+           $array[$property->getName()] = $property->getValue($object);
104
+           $property->setAccessible(false);
105
+        }
106
+        return $array;
107
+    }
108
+
109
+    /**
110
+     * @NoAdminRequired
111
+     */
112
+    public function isValidCallbackRequest($token, $url = null, $postParameters = null, $uploadedFiles = null, $signature = null) {
113
+
114
+	if (!$url) {
115
+	    $url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
116
+	}
117
+
118
+	if (!$postParameters) {
119
+	    $postParameters = $_REQUEST;
120
+	}
121
+
122
+	if (!$uploadedFiles) {
123
+	    $uploadedFiles = $_FILES;
124
+	}
125
+
126
+	if (!$signature) {
127
+	    $signature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
128
+	}
129
+
130
+	// Sort the array by keys
131
+	ksort($postParameters);
132
+
133
+	// Append the data array to the url string, with no delimiters
134
+	foreach ($postParameters as $key => $value) {
135
+		 $url .= $key . $value;
136
+	}
137
+
138
+	foreach ($uploadedFiles as $key => $value) {
139
+		 $url .= $key . sha1_file($value['tmp_name']);
140
+	}
141
+
142
+	$hmac = hash_hmac("sha1", $url, $token);
143
+	return $signature == $hmac;
144
+    }
145
+
146
+    /**
147
+     * @NoAdminRequired
148
+     * @NoCSRFRequired
149
+     */
150
+    public function receivefaxphaxio() {
151
+
152
+       $phaxiosignature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
153
+
154
+       $thisapicred = $this->service->getapicredentials($this->userId);
155
+       $phaxiowhtoken = $thisapicred[2];
156
+       $phaxiorecURL = $thisapicred[3];
157
+
158
+       // Verify Phaxio's signature
159
+       $signverify = $this->isValidCallbackRequest($phaxiowhtoken, $phaxiorecURL, null, null, $phaxiosignature);
160
+
161
+       if ($signverify) {
162
+
163
+           $fileContent = file_get_contents($_FILES['file']['tmp_name']);
164
+           $fileNameinit = $_FILES['file']['name'];
165
+
166
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
167
+
168
+           $gmtind = "UTC " . date('P');
169
+           $flmsdateinit = date("Y-m-d  H:i:s");
170
+           $flmsdate = $flmsdateinit . " " . $gmtind;
171
+
172
+           $fileNamesec = array_reverse(explode(".", $fileNameinit));
173
+           $filenameext = $fileNamesec[0];
174
+           array_shift($fileNamesec);
175
+
176
+           $fileName = implode("", $fileNamesec);
177
+
178
+           $faxid = str_replace("Fax-", "", $fileName);
179
+
180
+           $apiMode = 'live';
181
+
182
+           $apiKeys[$apiMode] = $thisapicred[0];
183
+           $apiSecrets[$apiMode] = $thisapicred[1];
184
+
185
+           $apiHost = 'https://api.phaxio.com/v2.1/';
186
+
187
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
188
+
189
+
190
+           try {
191
+                      $phaxioresultreq = $phaxio->doRequest("GET", 'faxes/' . $faxid);
192
+
193
+                      $fromnmbrtoarr = $this->object_to_array($phaxioresultreq);
194
+
195
+                      $phaxiofromraw = $fromnmbrtoarr['data']['from_number'];
196
+                      $phaxiotoraw  = $fromnmbrtoarr['data']['to_number'];
197
+                      $phaxiofromnumber = str_replace("+", "", $phaxiofromraw);
198
+                      $phaxiotonumber = str_replace("+", "", $phaxiotoraw);
199
+                      $errortype = 'there are no errors';
200
+
201
+           } catch (InvalidRequestException $e) {
202
+                      $phaxiofromnumber = 'null';
203
+                      $errortype = 'invalid request error';
204
+           } catch (AuthenticationException $e) {
205
+                      $phaxiofromnumber = 'null';
206
+                      $errortype = 'authentication error';
207
+           } catch (APIConnectionException $e) {
208
+                      $phaxiofromnumber = 'null';
209
+                      $errortype = 'API connection error';
210
+           } catch (RateLimitException $e) {
211
+                      $phaxiofromnumber = 'null';
212
+                      $errortype = 'rate limit error';
213
+           } catch (NotFoundException $e) {
214
+                      $phaxiofromnumber = 'null';
215
+                      $errortype = 'not found error';
216
+           } catch (GeneralException $e) {
217
+                      $phaxiofromnumber = 'null';
218
+                      $errortype = 'undefined error';
219
+           }
220
+
221
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
222
+               $this->folder->newFolder('Pax_Fax/faxes_received');
223
+           }
224
+
225
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
226
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
227
+           }
228
+
229
+           $targetfile = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber  . "_" . $fldate . "." . $filenameext;
230
+
231
+           $target = $this->folder->newFile($targetfile);
232
+
233
+           $target->putContent($fileContent);
234
+
235
+           // Move failed received faxes to the 'faxes_received_failed' folder
236
+           if ($filenameext == '') {
237
+
238
+               $newtargetfl =  "/Pax_Fax/faxes_received_failed/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
239
+               $targetact = $this->folder->newFile($newtargetfl);
240
+
241
+               $targetact->putContent($fileContent);
242
+
243
+               $failedfl = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
244
+               $removefailed = $this->filesystem->unlink($failedfl);
245
+           }
246
+
247
+           // Send notifications
248
+           $nameofhost = exec("hostname");
249
+           $getnextnotify = $thisapicred[4];
250
+           $useremailaddr = $thisapicred[5];
251
+
252
+           if ($filenameext != '') {
253
+               $targetflname = $targetfile;
254
+               $targettrim = "/Pax_Fax/faxes_received/";
255
+               $validfaxparam = "fax";
256
+               $setsubject = "New fax received";
257
+           } else {
258
+               $targetflname = $newtargetfl;
259
+               $targettrim = "/Pax_Fax/faxes_received_failed/";
260
+               $validfaxparam = "failed fax";
261
+               $setsubject = "New failed fax received";
262
+           }
263
+
264
+           if ($useremailaddr != '') {
265
+
266
+               $subject = $setsubject;
267
+               $message = "Hello! <br><br> You have received a new ".$validfaxparam." on ".$flmsdate." .<br> The new ".$validfaxparam." file is '".$targetflname."' . <br><br>____________<br><br> Yours, <br> Pax Fax <br> A fax application for Nextcloud <br> Host: '".$nameofhost."' <br>";
268
+
269
+               $messagefin = chunk_split(base64_encode($message));
270
+
271
+               $headers = "MIME-Version: 1.0" . "\r\n";
272
+               $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
273
+               $headers .= "Content-Transfer-Encoding: base64" . "\r\n";
274
+
275
+               // Set the email sender
276
+               $headers .= "From: " . $useremailaddr . "\r\n";
277
+               $headers .= "Reply-To: " . $useremailaddr . "\r\n";
278
+
279
+               mail($useremailaddr, $subject, $messagefin, $headers);
280
+           }
281
+
282
+           if ($getnextnotify != 0 ) {
283
+
284
+               $notify = exec("php ./occ notification:generate ".$this->userId." 'Pax Fax has received a new fax !' -l 'The new fax can be found in the \"".$targettrim."\" folder.'");
285
+          }
286
+
287
+       } else { return "access denied"; }
288
+
289
+    }
290
+
291
+}
292
+
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,295 +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\AppFramework\ApiController;
29
-use OCP\IRequest;
30
-use OCP\AppFramework\Http\DataResponse;
31
-use OCP\AppFramework\OCSController;
32
-use OCP\IUserSession;
33
-use OCP\AppFramework\Controller;
34
-use OCP\Files\IAppData;
35
-use OCA\PaxFax\Service\PaxfaxService;
36
-use OCP\AppFramework\App;
37
-use OCP\Files\NotPermittedException;
38
-use OCP\Files\Folder;
39
-use OC\Files\Filesystem;
40
-use OC\Files\View;
41
-use \ReflectionClass;
42
-use OCP\Notification;
43
-use OCP\Notification\INotification;
44
-use OCP\Notification\IManager;
45
-use OCP\Notification\IAction;
46
-use \DateTime;
47
-
48
-use Phaxio;
49
-use Phaxio\OperationResult;
50
-use Phaxio\Error\AuthenticationException;
51
-use Phaxio\Error\NotFoundException;
52
-use Phaxio\Error\InvalidRequestException;
53
-use Phaxio\Error\RateLimitException;
54
-use Phaxio\Error\APIConnectionException;
55
-use Phaxio\Error\GeneralException;
56
-
57
-
58
-class AuthorApiController extends ApiController {
59
-
60
-    private $service;
61
-    private $userId;
62
-    private $folder;
63
-    private $filesystem;
64
-    private $view;
65
-    private $userSession;
66
-
67
-    public function __construct($appName, IRequest $request, IUserSession $userSession, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId, View $view) {
68
-        parent::__construct(
69
-                $appName,
70
-                $request,
71
-                'PUT, POST, GET, DELETE, PATCH',
72
-                'Authorization, Content-Type, Accept',
73
-                1728000);
74
-
75
-        $this->service = $service;
76
-        $this->userId = $userId;
77
-        $this->folder = $folder;
78
-        $this->filesystem = $filesystem;
79
-        $this->view = $view;
80
-        $this->userSession = $userSession;
81
-    }
82
-
83
-    /**
84
-     * @NoAdminRequired
85
-     */
86
-    public function object_to_array($obj) {
87
-        if(is_object($obj)) $obj = (array)$this->dismount($obj);
88
-        if(is_array($obj)) {
89
-           $new = array();
90
-           foreach($obj as $key => $val) {
91
-               $new[$key] = $this->object_to_array($val);
92
-           }
93
-        }
94
-        else $new = $obj;
95
-        return $new;
96
-    }
97
-
98
-    /**
99
-     * @NoAdminRequired
100
-     */
101
-    public function dismount($object) {
102
-        $reflectionClass = new ReflectionClass(get_class($object));
103
-        $array = array();
104
-        foreach ($reflectionClass->getProperties() as $property) {
105
-           $property->setAccessible(true);
106
-           $array[$property->getName()] = $property->getValue($object);
107
-           $property->setAccessible(false);
108
-        }
109
-        return $array;
110
-    }
111
-
112
-    /**
113
-     * @NoAdminRequired
114
-     */
115
-    public function isValidCallbackRequest($token, $url = null, $postParameters = null, $uploadedFiles = null, $signature = null) {
116
-
117
-	if (!$url) {
118
-	    $url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
119
-	}
120
-
121
-	if (!$postParameters) {
122
-	    $postParameters = $_REQUEST;
123
-	}
124
-
125
-	if (!$uploadedFiles) {
126
-	    $uploadedFiles = $_FILES;
127
-	}
128
-
129
-	if (!$signature) {
130
-	    $signature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
131
-	}
132
-
133
-	// Sort the array by keys
134
-	ksort($postParameters);
135
-
136
-	// Append the data array to the url string, with no delimiters
137
-	foreach ($postParameters as $key => $value) {
138
-		 $url .= $key . $value;
139
-	}
140
-
141
-	foreach ($uploadedFiles as $key => $value) {
142
-		 $url .= $key . sha1_file($value['tmp_name']);
143
-	}
144
-
145
-	$hmac = hash_hmac("sha1", $url, $token);
146
-	return $signature == $hmac;
147
-    }
148
-
149
-    /**
150
-     * @NoAdminRequired
151
-     * @NoCSRFRequired
152
-     */
153
-    public function receivefaxphaxio() {
154
-
155
-       $phaxiosignature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
156
-
157
-       $thisapicred = $this->service->getapicredentials($this->userId);
158
-       $phaxiowhtoken = $thisapicred[2];
159
-       $phaxiorecURL = $thisapicred[3];
160
-
161
-       // Verify Phaxio's signature
162
-       $signverify = $this->isValidCallbackRequest($phaxiowhtoken, $phaxiorecURL, null, null, $phaxiosignature);
163
-
164
-       if ($signverify) {
165
-
166
-           $fileContent = file_get_contents($_FILES['file']['tmp_name']);
167
-           $fileNameinit = $_FILES['file']['name'];
168
-
169
-           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
170
-
171
-           $gmtind = "UTC " . date('P');
172
-           $flmsdateinit = date("Y-m-d  H:i:s");
173
-           $flmsdate = $flmsdateinit . " " . $gmtind;
174
-
175
-           $fileNamesec = array_reverse(explode(".", $fileNameinit));
176
-           $filenameext = $fileNamesec[0];
177
-           array_shift($fileNamesec);
178
-
179
-           $fileName = implode("", $fileNamesec);
180
-
181
-           $faxid = str_replace("Fax-", "", $fileName);
182
-
183
-           $apiMode = 'live';
184
-
185
-           $apiKeys[$apiMode] = $thisapicred[0];
186
-           $apiSecrets[$apiMode] = $thisapicred[1];
187
-
188
-           $apiHost = 'https://api.phaxio.com/v2.1/';
189
-
190
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
191
-
192
-
193
-           try {
194
-                      $phaxioresultreq = $phaxio->doRequest("GET", 'faxes/' . $faxid);
195
-
196
-                      $fromnmbrtoarr = $this->object_to_array($phaxioresultreq);
197
-
198
-                      $phaxiofromraw = $fromnmbrtoarr['data']['from_number'];
199
-                      $phaxiotoraw  = $fromnmbrtoarr['data']['to_number'];
200
-                      $phaxiofromnumber = str_replace("+", "", $phaxiofromraw);
201
-                      $phaxiotonumber = str_replace("+", "", $phaxiotoraw);
202
-                      $errortype = 'there are no errors';
203
-
204
-           } catch (InvalidRequestException $e) {
205
-                      $phaxiofromnumber = 'null';
206
-                      $errortype = 'invalid request error';
207
-           } catch (AuthenticationException $e) {
208
-                      $phaxiofromnumber = 'null';
209
-                      $errortype = 'authentication error';
210
-           } catch (APIConnectionException $e) {
211
-                      $phaxiofromnumber = 'null';
212
-                      $errortype = 'API connection error';
213
-           } catch (RateLimitException $e) {
214
-                      $phaxiofromnumber = 'null';
215
-                      $errortype = 'rate limit error';
216
-           } catch (NotFoundException $e) {
217
-                      $phaxiofromnumber = 'null';
218
-                      $errortype = 'not found error';
219
-           } catch (GeneralException $e) {
220
-                      $phaxiofromnumber = 'null';
221
-                      $errortype = 'undefined error';
222
-           }
223
-
224
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
225
-               $this->folder->newFolder('Pax_Fax/faxes_received');
226
-           }
227
-
228
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
229
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
230
-           }
231
-
232
-           $targetfile = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber  . "_" . $fldate . "." . $filenameext;
233
-
234
-           $target = $this->folder->newFile($targetfile);
235
-
236
-           $target->putContent($fileContent);
237
-
238
-           // Move failed received faxes to the 'faxes_received_failed' folder
239
-           if ($filenameext == '') {
240
-
241
-               $newtargetfl =  "/Pax_Fax/faxes_received_failed/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
242
-               $targetact = $this->folder->newFile($newtargetfl);
243
-
244
-               $targetact->putContent($fileContent);
245
-
246
-               $failedfl = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
247
-               $removefailed = $this->filesystem->unlink($failedfl);
248
-           }
249
-
250
-           // Send notifications
251
-           $nameofhost = exec("hostname");
252
-           $getnextnotify = $thisapicred[4];
253
-           $useremailaddr = $thisapicred[5];
254
-
255
-           if ($filenameext != '') {
256
-               $targetflname = $targetfile;
257
-               $targettrim = "/Pax_Fax/faxes_received/";
258
-               $validfaxparam = "fax";
259
-               $setsubject = "New fax received";
260
-           } else {
261
-               $targetflname = $newtargetfl;
262
-               $targettrim = "/Pax_Fax/faxes_received_failed/";
263
-               $validfaxparam = "failed fax";
264
-               $setsubject = "New failed fax received";
265
-           }
266
-
267
-           if ($useremailaddr != '') {
268
-
269
-               $subject = $setsubject;
270
-               $message = "Hello! <br><br> You have received a new ".$validfaxparam." on ".$flmsdate." .<br> The new ".$validfaxparam." file is '".$targetflname."' . <br><br>____________<br><br> Yours, <br> Pax Fax <br> A fax application for Nextcloud <br> Host: '".$nameofhost."' <br>";
271
-
272
-               $messagefin = chunk_split(base64_encode($message));
273
-
274
-               $headers = "MIME-Version: 1.0" . "\r\n";
275
-               $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
276
-               $headers .= "Content-Transfer-Encoding: base64" . "\r\n";
277
-
278
-               // Set the email sender
279
-               $headers .= "From: " . $useremailaddr . "\r\n";
280
-               $headers .= "Reply-To: " . $useremailaddr . "\r\n";
281
-
282
-               mail($useremailaddr, $subject, $messagefin, $headers);
283
-           }
284
-
285
-           if ($getnextnotify != 0 ) {
286
-
287
-               $notify = exec("php ./occ notification:generate ".$this->userId." 'Pax Fax has received a new fax !' -l 'The new fax can be found in the \"".$targettrim."\" folder.'");
288
-          }
289
-
290
-       } else { return "access denied"; }
291
-
292
-    }
293
-
294
-}
295
-
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,295 @@
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\AppFramework\ApiController;
29
+use OCP\IRequest;
30
+use OCP\AppFramework\Http\DataResponse;
31
+use OCP\AppFramework\OCSController;
32
+use OCP\IUserSession;
33
+use OCP\AppFramework\Controller;
34
+use OCP\Files\IAppData;
35
+use OCA\PaxFax\Service\PaxfaxService;
36
+use OCP\AppFramework\App;
37
+use OCP\Files\NotPermittedException;
38
+use OCP\Files\Folder;
39
+use OC\Files\Filesystem;
40
+use OC\Files\View;
41
+use \ReflectionClass;
42
+use OCP\Notification;
43
+use OCP\Notification\INotification;
44
+use OCP\Notification\IManager;
45
+use OCP\Notification\IAction;
46
+use \DateTime;
47
+
48
+use Phaxio;
49
+use Phaxio\OperationResult;
50
+use Phaxio\Error\AuthenticationException;
51
+use Phaxio\Error\NotFoundException;
52
+use Phaxio\Error\InvalidRequestException;
53
+use Phaxio\Error\RateLimitException;
54
+use Phaxio\Error\APIConnectionException;
55
+use Phaxio\Error\GeneralException;
56
+
57
+
58
+class AuthorApiController extends ApiController {
59
+
60
+    private $service;
61
+    private $userId;
62
+    private $folder;
63
+    private $filesystem;
64
+    private $view;
65
+    private $userSession;
66
+
67
+    public function __construct($appName, IRequest $request, IUserSession $userSession, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId, View $view) {
68
+        parent::__construct(
69
+                $appName,
70
+                $request,
71
+                'PUT, POST, GET, DELETE, PATCH',
72
+                'Authorization, Content-Type, Accept',
73
+                1728000);
74
+
75
+        $this->service = $service;
76
+        $this->userId = $userId;
77
+        $this->folder = $folder;
78
+        $this->filesystem = $filesystem;
79
+        $this->view = $view;
80
+        $this->userSession = $userSession;
81
+    }
82
+
83
+    /**
84
+     * @NoAdminRequired
85
+     */
86
+    public function object_to_array($obj) {
87
+        if(is_object($obj)) $obj = (array)$this->dismount($obj);
88
+        if(is_array($obj)) {
89
+           $new = array();
90
+           foreach($obj as $key => $val) {
91
+               $new[$key] = $this->object_to_array($val);
92
+           }
93
+        }
94
+        else $new = $obj;
95
+        return $new;
96
+    }
97
+
98
+    /**
99
+     * @NoAdminRequired
100
+     */
101
+    public function dismount($object) {
102
+        $reflectionClass = new ReflectionClass(get_class($object));
103
+        $array = array();
104
+        foreach ($reflectionClass->getProperties() as $property) {
105
+           $property->setAccessible(true);
106
+           $array[$property->getName()] = $property->getValue($object);
107
+           $property->setAccessible(false);
108
+        }
109
+        return $array;
110
+    }
111
+
112
+    /**
113
+     * @NoAdminRequired
114
+     */
115
+    public function isValidCallbackRequest($token, $url = null, $postParameters = null, $uploadedFiles = null, $signature = null) {
116
+
117
+	if (!$url) {
118
+	    $url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
119
+	}
120
+
121
+	if (!$postParameters) {
122
+	    $postParameters = $_REQUEST;
123
+	}
124
+
125
+	if (!$uploadedFiles) {
126
+	    $uploadedFiles = $_FILES;
127
+	}
128
+
129
+	if (!$signature) {
130
+	    $signature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
131
+	}
132
+
133
+	// Sort the array by keys
134
+	ksort($postParameters);
135
+
136
+	// Append the data array to the url string, with no delimiters
137
+	foreach ($postParameters as $key => $value) {
138
+		 $url .= $key . $value;
139
+	}
140
+
141
+	foreach ($uploadedFiles as $key => $value) {
142
+		 $url .= $key . sha1_file($value['tmp_name']);
143
+	}
144
+
145
+	$hmac = hash_hmac("sha1", $url, $token);
146
+	return $signature == $hmac;
147
+    }
148
+
149
+    /**
150
+     * @NoAdminRequired
151
+     * @NoCSRFRequired
152
+     */
153
+    public function receivefaxphaxio() {
154
+
155
+       $phaxiosignature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
156
+
157
+       $thisapicred = $this->service->getapicredentials($this->userId);
158
+       $phaxiowhtoken = $thisapicred[2];
159
+       $phaxiorecURL = $thisapicred[3];
160
+
161
+       // Verify Phaxio's signature
162
+       $signverify = $this->isValidCallbackRequest($phaxiowhtoken, $phaxiorecURL, null, null, $phaxiosignature);
163
+
164
+       if ($signverify) {
165
+
166
+           $fileContent = file_get_contents($_FILES['file']['tmp_name']);
167
+           $fileNameinit = $_FILES['file']['name'];
168
+
169
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
170
+
171
+           $gmtind = "UTC " . date('P');
172
+           $flmsdateinit = date("Y-m-d  H:i:s");
173
+           $flmsdate = $flmsdateinit . " " . $gmtind;
174
+
175
+           $fileNamesec = array_reverse(explode(".", $fileNameinit));
176
+           $filenameext = $fileNamesec[0];
177
+           array_shift($fileNamesec);
178
+
179
+           $fileName = implode("", $fileNamesec);
180
+
181
+           $faxid = str_replace("Fax-", "", $fileName);
182
+
183
+           $apiMode = 'live';
184
+
185
+           $apiKeys[$apiMode] = $thisapicred[0];
186
+           $apiSecrets[$apiMode] = $thisapicred[1];
187
+
188
+           $apiHost = 'https://api.phaxio.com/v2.1/';
189
+
190
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
191
+
192
+
193
+           try {
194
+                      $phaxioresultreq = $phaxio->doRequest("GET", 'faxes/' . $faxid);
195
+
196
+                      $fromnmbrtoarr = $this->object_to_array($phaxioresultreq);
197
+
198
+                      $phaxiofromraw = $fromnmbrtoarr['data']['from_number'];
199
+                      $phaxiotoraw  = $fromnmbrtoarr['data']['to_number'];
200
+                      $phaxiofromnumber = str_replace("+", "", $phaxiofromraw);
201
+                      $phaxiotonumber = str_replace("+", "", $phaxiotoraw);
202
+                      $errortype = 'there are no errors';
203
+
204
+           } catch (InvalidRequestException $e) {
205
+                      $phaxiofromnumber = 'null';
206
+                      $errortype = 'invalid request error';
207
+           } catch (AuthenticationException $e) {
208
+                      $phaxiofromnumber = 'null';
209
+                      $errortype = 'authentication error';
210
+           } catch (APIConnectionException $e) {
211
+                      $phaxiofromnumber = 'null';
212
+                      $errortype = 'API connection error';
213
+           } catch (RateLimitException $e) {
214
+                      $phaxiofromnumber = 'null';
215
+                      $errortype = 'rate limit error';
216
+           } catch (NotFoundException $e) {
217
+                      $phaxiofromnumber = 'null';
218
+                      $errortype = 'not found error';
219
+           } catch (GeneralException $e) {
220
+                      $phaxiofromnumber = 'null';
221
+                      $errortype = 'undefined error';
222
+           }
223
+
224
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
225
+               $this->folder->newFolder('Pax_Fax/faxes_received');
226
+           }
227
+
228
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
229
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
230
+           }
231
+
232
+           $targetfile = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber  . "_" . $fldate . "." . $filenameext;
233
+
234
+           $target = $this->folder->newFile($targetfile);
235
+
236
+           $target->putContent($fileContent);
237
+
238
+           // Move failed received faxes to the 'faxes_received_failed' folder
239
+           if ($filenameext == '') {
240
+
241
+               $newtargetfl =  "/Pax_Fax/faxes_received_failed/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
242
+               $targetact = $this->folder->newFile($newtargetfl);
243
+
244
+               $targetact->putContent($fileContent);
245
+
246
+               $failedfl = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
247
+               $removefailed = $this->filesystem->unlink($failedfl);
248
+           }
249
+
250
+           // Send notifications
251
+           $nameofhost = exec("hostname");
252
+           $getnextnotify = $thisapicred[4];
253
+           $useremailaddr = $thisapicred[5];
254
+
255
+           if ($filenameext != '') {
256
+               $targetflname = $targetfile;
257
+               $targettrim = "/Pax_Fax/faxes_received/";
258
+               $validfaxparam = "fax";
259
+               $setsubject = "New fax received";
260
+           } else {
261
+               $targetflname = $newtargetfl;
262
+               $targettrim = "/Pax_Fax/faxes_received_failed/";
263
+               $validfaxparam = "failed fax";
264
+               $setsubject = "New failed fax received";
265
+           }
266
+
267
+           if ($useremailaddr != '') {
268
+
269
+               $subject = $setsubject;
270
+               $message = "Hello! <br><br> You have received a new ".$validfaxparam." on ".$flmsdate." .<br> The new ".$validfaxparam." file is '".$targetflname."' . <br><br>____________<br><br> Yours, <br> Pax Fax <br> A fax application for Nextcloud <br> Host: '".$nameofhost."' <br>";
271
+
272
+               $messagefin = chunk_split(base64_encode($message));
273
+
274
+               $headers = "MIME-Version: 1.0" . "\r\n";
275
+               $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
276
+               $headers .= "Content-Transfer-Encoding: base64" . "\r\n";
277
+
278
+               // Set the email sender
279
+               $headers .= "From: " . $useremailaddr . "\r\n";
280
+               $headers .= "Reply-To: " . $useremailaddr . "\r\n";
281
+
282
+               mail($useremailaddr, $subject, $messagefin, $headers);
283
+           }
284
+
285
+           if ($getnextnotify != 0 ) {
286
+
287
+               $notify = exec("php ./occ notification:generate ".$this->userId." 'Pax Fax has received a new fax !' -l 'The new fax can be found in the \"".$targettrim."\" folder.'");
288
+          }
289
+
290
+       } else { return "access denied"; }
291
+
292
+    }
293
+
294
+}
295
+
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,293 +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\AppFramework\ApiController;
29
-use OCP\IRequest;
30
-use OCP\AppFramework\Http\DataResponse;
31
-use OCP\AppFramework\OCSController;
32
-use OCP\IUserSession;
33
-use OCP\AppFramework\Controller;
34
-use OCP\Files\IAppData;
35
-use OCA\PaxFax\Service\PaxfaxService;
36
-use OCP\AppFramework\App;
37
-use OCP\Files\NotPermittedException;
38
-use OCP\Files\Folder;
39
-use OC\Files\Filesystem;
40
-use OC\Files\View;
41
-use \ReflectionClass;
42
-use OCP\Notification;
43
-use OCP\Notification\INotification;
44
-use OCP\Notification\IManager;
45
-use OCP\Notification\IAction;
46
-use \DateTime;
47
-
48
-use Phaxio;
49
-use Phaxio\OperationResult;
50
-use Phaxio\Error\AuthenticationException;
51
-use Phaxio\Error\NotFoundException;
52
-use Phaxio\Error\InvalidRequestException;
53
-use Phaxio\Error\RateLimitException;
54
-use Phaxio\Error\APIConnectionException;
55
-use Phaxio\Error\GeneralException;
56
-
57
-
58
-class AuthorApiController extends ApiController {
59
-
60
-    private $service;
61
-    private $userId;
62
-    private $folder;
63
-    private $filesystem;
64
-    private $view;
65
-    private $userSession;
66
-
67
-    public function __construct($appName, IRequest $request, IUserSession $userSession, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId, View $view) {
68
-        parent::__construct(
69
-                $appName,
70
-                $request,
71
-                'PUT, POST, GET, DELETE, PATCH',
72
-                'Authorization, Content-Type, Accept',
73
-                1728000);
74
-
75
-        $this->service = $service;
76
-        $this->userId = $userId;
77
-        $this->folder = $folder;
78
-        $this->filesystem = $filesystem;
79
-        $this->view = $view;
80
-        $this->userSession = $userSession;
81
-    }
82
-
83
-    /**
84
-     * @NoAdminRequired
85
-     */
86
-    public function object_to_array($obj) {
87
-        if(is_object($obj)) $obj = (array)$this->dismount($obj);
88
-        if(is_array($obj)) {
89
-           $new = array();
90
-           foreach($obj as $key => $val) {
91
-               $new[$key] = $this->object_to_array($val);
92
-           }
93
-        }
94
-        else $new = $obj;
95
-        return $new;
96
-    }
97
-
98
-    /**
99
-     * @NoAdminRequired
100
-     */
101
-    public function dismount($object) {
102
-        $reflectionClass = new ReflectionClass(get_class($object));
103
-        $array = array();
104
-        foreach ($reflectionClass->getProperties() as $property) {
105
-           $property->setAccessible(true);
106
-           $array[$property->getName()] = $property->getValue($object);
107
-           $property->setAccessible(false);
108
-        }
109
-        return $array;
110
-    }
111
-
112
-    /**
113
-     * @NoAdminRequired
114
-     */
115
-    public function isValidCallbackRequest($token, $url = null, $postParameters = null, $uploadedFiles = null, $signature = null) {
116
-
117
-	if (!$url) {
118
-	    $url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
119
-	}
120
-
121
-	if (!$postParameters) {
122
-	    $postParameters = $_REQUEST;
123
-	}
124
-
125
-	if (!$uploadedFiles) {
126
-	    $uploadedFiles = $_FILES;
127
-	}
128
-
129
-	if (!$signature) {
130
-	    $signature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
131
-	}
132
-
133
-	// Sort the array by keys
134
-	ksort($postParameters);
135
-
136
-	// Append the data array to the url string, with no delimiters
137
-	foreach ($postParameters as $key => $value) {
138
-		 $url .= $key . $value;
139
-	}
140
-
141
-	foreach ($uploadedFiles as $key => $value) {
142
-		 $url .= $key . sha1_file($value['tmp_name']);
143
-	}
144
-
145
-	$hmac = hash_hmac("sha1", $url, $token);
146
-	return $signature == $hmac;
147
-    }
148
-
149
-    /**
150
-     * @NoAdminRequired
151
-     * @NoCSRFRequired
152
-     */
153
-    public function receivefaxphaxio() {
154
-
155
-       $phaxiosignature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
156
-
157
-       $thisapicred = $this->service->getapicredentials($this->userId);
158
-       $phaxiowhtoken = $thisapicred[2];
159
-       $phaxiorecURL = $thisapicred[3];
160
-
161
-       // Verify Phaxio's signature
162
-       $signverify = $this->isValidCallbackRequest($phaxiowhtoken, $phaxiorecURL, null, null, $phaxiosignature);
163
-
164
-       if ($signverify) {
165
-
166
-           $fileContent = file_get_contents($_FILES['file']['tmp_name']);
167
-           $fileNameinit = $_FILES['file']['name'];
168
-
169
-           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
170
-
171
-           $gmtind = "UTC " . date('P');
172
-           $flmsdateinit = date("Y-m-d  H:i:s");
173
-           $flmsdate = $flmsdateinit . " " . $gmtind;
174
-
175
-           $fileNamesec = array_reverse(explode(".", $fileNameinit));
176
-           $filenameext = $fileNamesec[0];
177
-           array_shift($fileNamesec);
178
-
179
-           $fileName = implode("", $fileNamesec);
180
-
181
-           $faxid = str_replace("Fax-", "", $fileName);
182
-
183
-           $apiMode = 'live';
184
-
185
-           $apiKeys[$apiMode] = $thisapicred[0];
186
-           $apiSecrets[$apiMode] = $thisapicred[1];
187
-
188
-           $apiHost = 'https://api.phaxio.com/v2.1/';
189
-
190
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
191
-
192
-
193
-           try {
194
-                      $phaxioresultreq = $phaxio->doRequest("GET", 'faxes/' . $faxid);
195
-
196
-                      $fromnmbrtoarr = $this->object_to_array($phaxioresultreq);
197
-
198
-                      $phaxiofromraw = $fromnmbrtoarr['data']['from_number'];
199
-                      $phaxiotoraw  = $fromnmbrtoarr['data']['to_number'];
200
-                      $phaxiofromnumber = str_replace("+", "", $phaxiofromraw);
201
-                      $phaxiotonumber = str_replace("+", "", $phaxiotoraw);
202
-                      $errortype = 'there are no errors';
203
-
204
-           } catch (InvalidRequestException $e) {
205
-                      $phaxiofromnumber = 'null';
206
-                      $errortype = 'invalid request error';
207
-           } catch (AuthenticationException $e) {
208
-                      $phaxiofromnumber = 'null';
209
-                      $errortype = 'authentication error';
210
-           } catch (APIConnectionException $e) {
211
-                      $phaxiofromnumber = 'null';
212
-                      $errortype = 'API connection error';
213
-           } catch (RateLimitException $e) {
214
-                      $phaxiofromnumber = 'null';
215
-                      $errortype = 'rate limit error';
216
-           } catch (NotFoundException $e) {
217
-                      $phaxiofromnumber = 'null';
218
-                      $errortype = 'not found error';
219
-           } catch (GeneralException $e) {
220
-                      $phaxiofromnumber = 'null';
221
-                      $errortype = 'undefined error';
222
-           }
223
-
224
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
225
-               $this->folder->newFolder('Pax_Fax/faxes_received');
226
-           }
227
-
228
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
229
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
230
-           }
231
-
232
-           $targetfile = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber  . "_" . $fldate . "." . $filenameext;
233
-
234
-           $target = $this->folder->newFile($targetfile);
235
-
236
-           $target->putContent($fileContent);
237
-
238
-           // Move failed received faxes to the 'faxes_received_failed' folder
239
-           if ($filenameext == '') {
240
-
241
-               $newtargetfl =  "/Pax_Fax/faxes_received_failed/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
242
-               $targetact = $this->folder->newFile($newtargetfl);
243
-
244
-               $targetact->putContent($fileContent);
245
-
246
-               $failedfl = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
247
-               $removefailed = $this->filesystem->unlink($failedfl);
248
-           }
249
-
250
-           // Send notifications
251
-           $nameofhost = exec("hostname");
252
-           $getnextnotify = $thisapicred[4];
253
-           $useremailaddr = $thisapicred[5];
254
-
255
-           if ($filenameext != '') {
256
-               $targetflname = $targetfile;
257
-               $targettrim = "/Pax_Fax/faxes_received/";
258
-               $validfaxparam = "fax";
259
-               $setsubject = "New fax received";
260
-           } else {
261
-               $targetflname = $newtargetfl;
262
-               $targettrim = "/Pax_Fax/faxes_received_failed/";
263
-               $validfaxparam = "failed fax";
264
-               $setsubject = "New failed fax received";
265
-           }
266
-
267
-           if ($useremailaddr != '') {
268
-
269
-               $subject = $setsubject;
270
-               $message = "Hello! <br><br> You have received a new ".$validfaxparam." on ".$flmsdate." .<br> The new ".$validfaxparam." file is '".$targetflname."' . <br><br>____________<br><br> Yours, <br> Pax Fax <br> A fax application for Nextcloud <br> Host: '".$nameofhost."' <br>";
271
-
272
-               // Mention the content-type, because we send an HTML email
273
-               $headers = "MIME-Version: 1.0" . "\r\n";
274
-               $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
275
-
276
-               // Set the email sender
277
-               $headers .= "From: " . $useremailaddr . "\r\n";
278
-               $headers .= "Reply-To: " . $useremailaddr . "\r\n";
279
-
280
-               mail($useremailaddr, $subject, $message, $headers);
281
-           }
282
-
283
-           if ($getnextnotify != 0 ) {
284
-
285
-               $notify = exec("php ./occ notification:generate ".$this->userId." 'Pax Fax has received a new fax !' -l 'The new fax can be found in the \"".$targettrim."\" folder.'");
286
-          }
287
-
288
-       } else { return "access denied"; }
289
-
290
-    }
291
-
292
-}
293
-
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,293 @@
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\AppFramework\ApiController;
29
+use OCP\IRequest;
30
+use OCP\AppFramework\Http\DataResponse;
31
+use OCP\AppFramework\OCSController;
32
+use OCP\IUserSession;
33
+use OCP\AppFramework\Controller;
34
+use OCP\Files\IAppData;
35
+use OCA\PaxFax\Service\PaxfaxService;
36
+use OCP\AppFramework\App;
37
+use OCP\Files\NotPermittedException;
38
+use OCP\Files\Folder;
39
+use OC\Files\Filesystem;
40
+use OC\Files\View;
41
+use \ReflectionClass;
42
+use OCP\Notification;
43
+use OCP\Notification\INotification;
44
+use OCP\Notification\IManager;
45
+use OCP\Notification\IAction;
46
+use \DateTime;
47
+
48
+use Phaxio;
49
+use Phaxio\OperationResult;
50
+use Phaxio\Error\AuthenticationException;
51
+use Phaxio\Error\NotFoundException;
52
+use Phaxio\Error\InvalidRequestException;
53
+use Phaxio\Error\RateLimitException;
54
+use Phaxio\Error\APIConnectionException;
55
+use Phaxio\Error\GeneralException;
56
+
57
+
58
+class AuthorApiController extends ApiController {
59
+
60
+    private $service;
61
+    private $userId;
62
+    private $folder;
63
+    private $filesystem;
64
+    private $view;
65
+    private $userSession;
66
+
67
+    public function __construct($appName, IRequest $request, IUserSession $userSession, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId, View $view) {
68
+        parent::__construct(
69
+                $appName,
70
+                $request,
71
+                'PUT, POST, GET, DELETE, PATCH',
72
+                'Authorization, Content-Type, Accept',
73
+                1728000);
74
+
75
+        $this->service = $service;
76
+        $this->userId = $userId;
77
+        $this->folder = $folder;
78
+        $this->filesystem = $filesystem;
79
+        $this->view = $view;
80
+        $this->userSession = $userSession;
81
+    }
82
+
83
+    /**
84
+     * @NoAdminRequired
85
+     */
86
+    public function object_to_array($obj) {
87
+        if(is_object($obj)) $obj = (array)$this->dismount($obj);
88
+        if(is_array($obj)) {
89
+           $new = array();
90
+           foreach($obj as $key => $val) {
91
+               $new[$key] = $this->object_to_array($val);
92
+           }
93
+        }
94
+        else $new = $obj;
95
+        return $new;
96
+    }
97
+
98
+    /**
99
+     * @NoAdminRequired
100
+     */
101
+    public function dismount($object) {
102
+        $reflectionClass = new ReflectionClass(get_class($object));
103
+        $array = array();
104
+        foreach ($reflectionClass->getProperties() as $property) {
105
+           $property->setAccessible(true);
106
+           $array[$property->getName()] = $property->getValue($object);
107
+           $property->setAccessible(false);
108
+        }
109
+        return $array;
110
+    }
111
+
112
+    /**
113
+     * @NoAdminRequired
114
+     */
115
+    public function isValidCallbackRequest($token, $url = null, $postParameters = null, $uploadedFiles = null, $signature = null) {
116
+
117
+	if (!$url) {
118
+	    $url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
119
+	}
120
+
121
+	if (!$postParameters) {
122
+	    $postParameters = $_REQUEST;
123
+	}
124
+
125
+	if (!$uploadedFiles) {
126
+	    $uploadedFiles = $_FILES;
127
+	}
128
+
129
+	if (!$signature) {
130
+	    $signature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
131
+	}
132
+
133
+	// Sort the array by keys
134
+	ksort($postParameters);
135
+
136
+	// Append the data array to the url string, with no delimiters
137
+	foreach ($postParameters as $key => $value) {
138
+		 $url .= $key . $value;
139
+	}
140
+
141
+	foreach ($uploadedFiles as $key => $value) {
142
+		 $url .= $key . sha1_file($value['tmp_name']);
143
+	}
144
+
145
+	$hmac = hash_hmac("sha1", $url, $token);
146
+	return $signature == $hmac;
147
+    }
148
+
149
+    /**
150
+     * @NoAdminRequired
151
+     * @NoCSRFRequired
152
+     */
153
+    public function receivefaxphaxio() {
154
+
155
+       $phaxiosignature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
156
+
157
+       $thisapicred = $this->service->getapicredentials($this->userId);
158
+       $phaxiowhtoken = $thisapicred[2];
159
+       $phaxiorecURL = $thisapicred[3];
160
+
161
+       // Verify Phaxio's signature
162
+       $signverify = $this->isValidCallbackRequest($phaxiowhtoken, $phaxiorecURL, null, null, $phaxiosignature);
163
+
164
+       if ($signverify) {
165
+
166
+           $fileContent = file_get_contents($_FILES['file']['tmp_name']);
167
+           $fileNameinit = $_FILES['file']['name'];
168
+
169
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
170
+
171
+           $gmtind = "UTC " . date('P');
172
+           $flmsdateinit = date("Y-m-d  H:i:s");
173
+           $flmsdate = $flmsdateinit . " " . $gmtind;
174
+
175
+           $fileNamesec = array_reverse(explode(".", $fileNameinit));
176
+           $filenameext = $fileNamesec[0];
177
+           array_shift($fileNamesec);
178
+
179
+           $fileName = implode("", $fileNamesec);
180
+
181
+           $faxid = str_replace("Fax-", "", $fileName);
182
+
183
+           $apiMode = 'live';
184
+
185
+           $apiKeys[$apiMode] = $thisapicred[0];
186
+           $apiSecrets[$apiMode] = $thisapicred[1];
187
+
188
+           $apiHost = 'https://api.phaxio.com/v2.1/';
189
+
190
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
191
+
192
+
193
+           try {
194
+                      $phaxioresultreq = $phaxio->doRequest("GET", 'faxes/' . $faxid);
195
+
196
+                      $fromnmbrtoarr = $this->object_to_array($phaxioresultreq);
197
+
198
+                      $phaxiofromraw = $fromnmbrtoarr['data']['from_number'];
199
+                      $phaxiotoraw  = $fromnmbrtoarr['data']['to_number'];
200
+                      $phaxiofromnumber = str_replace("+", "", $phaxiofromraw);
201
+                      $phaxiotonumber = str_replace("+", "", $phaxiotoraw);
202
+                      $errortype = 'there are no errors';
203
+
204
+           } catch (InvalidRequestException $e) {
205
+                      $phaxiofromnumber = 'null';
206
+                      $errortype = 'invalid request error';
207
+           } catch (AuthenticationException $e) {
208
+                      $phaxiofromnumber = 'null';
209
+                      $errortype = 'authentication error';
210
+           } catch (APIConnectionException $e) {
211
+                      $phaxiofromnumber = 'null';
212
+                      $errortype = 'API connection error';
213
+           } catch (RateLimitException $e) {
214
+                      $phaxiofromnumber = 'null';
215
+                      $errortype = 'rate limit error';
216
+           } catch (NotFoundException $e) {
217
+                      $phaxiofromnumber = 'null';
218
+                      $errortype = 'not found error';
219
+           } catch (GeneralException $e) {
220
+                      $phaxiofromnumber = 'null';
221
+                      $errortype = 'undefined error';
222
+           }
223
+
224
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
225
+               $this->folder->newFolder('Pax_Fax/faxes_received');
226
+           }
227
+
228
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
229
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
230
+           }
231
+
232
+           $targetfile = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber  . "_" . $fldate . "." . $filenameext;
233
+
234
+           $target = $this->folder->newFile($targetfile);
235
+
236
+           $target->putContent($fileContent);
237
+
238
+           // Move failed received faxes to the 'faxes_received_failed' folder
239
+           if ($filenameext == '') {
240
+
241
+               $newtargetfl =  "/Pax_Fax/faxes_received_failed/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
242
+               $targetact = $this->folder->newFile($newtargetfl);
243
+
244
+               $targetact->putContent($fileContent);
245
+
246
+               $failedfl = "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
247
+               $removefailed = $this->filesystem->unlink($failedfl);
248
+           }
249
+
250
+           // Send notifications
251
+           $nameofhost = exec("hostname");
252
+           $getnextnotify = $thisapicred[4];
253
+           $useremailaddr = $thisapicred[5];
254
+
255
+           if ($filenameext != '') {
256
+               $targetflname = $targetfile;
257
+               $targettrim = "/Pax_Fax/faxes_received/";
258
+               $validfaxparam = "fax";
259
+               $setsubject = "New fax received";
260
+           } else {
261
+               $targetflname = $newtargetfl;
262
+               $targettrim = "/Pax_Fax/faxes_received_failed/";
263
+               $validfaxparam = "failed fax";
264
+               $setsubject = "New failed fax received";
265
+           }
266
+
267
+           if ($useremailaddr != '') {
268
+
269
+               $subject = $setsubject;
270
+               $message = "Hello! <br><br> You have received a new ".$validfaxparam." on ".$flmsdate." .<br> The new ".$validfaxparam." file is '".$targetflname."' . <br><br>____________<br><br> Yours, <br> Pax Fax <br> A fax application for Nextcloud <br> Host: '".$nameofhost."' <br>";
271
+
272
+               // Mention the content-type, because we send an HTML email
273
+               $headers = "MIME-Version: 1.0" . "\r\n";
274
+               $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
275
+
276
+               // Set the email sender
277
+               $headers .= "From: " . $useremailaddr . "\r\n";
278
+               $headers .= "Reply-To: " . $useremailaddr . "\r\n";
279
+
280
+               mail($useremailaddr, $subject, $message, $headers);
281
+           }
282
+
283
+           if ($getnextnotify != 0 ) {
284
+
285
+               $notify = exec("php ./occ notification:generate ".$this->userId." 'Pax Fax has received a new fax !' -l 'The new fax can be found in the \"".$targettrim."\" folder.'");
286
+          }
287
+
288
+       } else { return "access denied"; }
289
+
290
+    }
291
+
292
+}
293
+
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,299 +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\AppFramework\ApiController;
29
-use OCP\IRequest;
30
-
31
-use OCP\AppFramework\Http\DataResponse;
32
-use OCP\AppFramework\OCSController;
33
-use OCP\IUserSession;
34
-
35
-use OCP\AppFramework\Controller;
36
-use OCP\Files\IAppData;
37
-use OCA\PaxFax\Service\PaxfaxService;
38
-use OCP\AppFramework\App;
39
-use OCP\Files\NotPermittedException;
40
-use OCP\Files\Folder;
41
-use OC\Files\Filesystem;
42
-use OC\Files\View;
43
-use \ReflectionClass;
44
-
45
-use OCP\Notification;
46
-use OCP\Notification\INotification;
47
-use OCP\Notification\IManager;
48
-use OCP\Notification\IAction;
49
-use \DateTime;
50
-
51
-use Phaxio;
52
-use Phaxio\OperationResult;
53
-use Phaxio\Error\AuthenticationException;
54
-use Phaxio\Error\NotFoundException;
55
-use Phaxio\Error\InvalidRequestException;
56
-use Phaxio\Error\RateLimitException;
57
-use Phaxio\Error\APIConnectionException;
58
-use Phaxio\Error\GeneralException;
59
-
60
-
61
-class AuthorApiController extends ApiController {
62
-
63
-    private $service;
64
-    private $userId;
65
-    private $folder;
66
-    private $filesystem;
67
-    private $view;
68
-    private $userSession;
69
-
70
-    public function __construct($appName, IRequest $request, IUserSession $userSession, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId, View $view) {
71
-        parent::__construct(
72
-                $appName,
73
-                $request,
74
-                'PUT, POST, GET, DELETE, PATCH',
75
-                'Authorization, Content-Type, Accept',
76
-                1728000);
77
-
78
-        $this->service = $service;
79
-        $this->userId = $userId;
80
-        $this->folder = $folder;
81
-        $this->filesystem = $filesystem;
82
-        $this->view = $view;
83
-        $this->userSession = $userSession;
84
-    }
85
-
86
-    /**
87
-     * @NoAdminRequired
88
-     */
89
-    public function object_to_array($obj) {
90
-        if(is_object($obj)) $obj = (array)$this->dismount($obj);
91
-        if(is_array($obj)) {
92
-           $new = array();
93
-           foreach($obj as $key => $val) {
94
-               $new[$key] = $this->object_to_array($val);
95
-           }
96
-        }
97
-        else $new = $obj;
98
-        return $new;
99
-    }
100
-
101
-    /**
102
-     * @NoAdminRequired
103
-     */
104
-    public function dismount($object) {
105
-        $reflectionClass = new ReflectionClass(get_class($object));
106
-        $array = array();
107
-        foreach ($reflectionClass->getProperties() as $property) {
108
-           $property->setAccessible(true);
109
-           $array[$property->getName()] = $property->getValue($object);
110
-           $property->setAccessible(false);
111
-        }
112
-        return $array;
113
-    }
114
-
115
-    /**
116
-     * @NoAdminRequired
117
-     */
118
-    public function isValidCallbackRequest($token, $url = null, $postParameters = null, $uploadedFiles = null, $signature = null) {
119
-
120
-	if (!$url) {
121
-	    $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
122
-	}
123
-
124
-	if (!$postParameters) {
125
-	    $postParameters = $_REQUEST;
126
-	}
127
-
128
-	if (!$uploadedFiles) {
129
-	    $uploadedFiles = $_FILES;
130
-	}
131
-
132
-	if (!$signature) {
133
-	    $signature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
134
-	}
135
-
136
-	// Sort the array by keys
137
-	ksort($postParameters);
138
-
139
-	// Append the data array to the url string, with no delimiters
140
-	foreach ($postParameters as $key => $value) {
141
-		 $url .= $key . $value;
142
-	}
143
-
144
-	foreach ($uploadedFiles as $key => $value) {
145
-		 $url .= $key . sha1_file($value['tmp_name']);
146
-	}
147
-
148
-	$hmac = hash_hmac("sha1", $url, $token);
149
-	return $signature == $hmac;
150
-    }
151
-
152
-    /**
153
-     * @NoAdminRequired
154
-     * @NoCSRFRequired
155
-     */
156
-    public function receivefaxphaxio() {
157
-
158
-       $phaxiosignature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
159
-
160
-       $thisapicred = $this->service->getapicredentials($this->userId);
161
-       $phaxiowhtoken = $thisapicred[2];
162
-       $phaxiorecURL = $thisapicred[3];
163
-
164
-       // Verify Phaxio's signature
165
-       $signverify = $this->isValidCallbackRequest($phaxiowhtoken, $phaxiorecURL, null, null, $phaxiosignature);
166
-
167
-       if ($signverify) {
168
-
169
-           $fileContent = file_get_contents($_FILES['file']['tmp_name']);
170
-           $fileNameinit = $_FILES['file']['name'];
171
-
172
-           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
173
-
174
-           $gmtind = "UTC " . date('P');
175
-           $flmsdateinit = date("Y-m-d  H:i:s");
176
-           $flmsdate = $flmsdateinit . " " . $gmtind;
177
-
178
-
179
-           $fileNamesec = array_reverse(explode(".", $fileNameinit));
180
-           $filenameext = $fileNamesec[0];
181
-           array_shift($fileNamesec);
182
-
183
-           $fileName = implode("", $fileNamesec);
184
-
185
-           $faxid = str_replace("Fax-", "", $fileName);
186
-
187
-           $apiMode = 'live';
188
-
189
-           $apiKeys[$apiMode] = $thisapicred[0];
190
-           $apiSecrets[$apiMode] = $thisapicred[1];
191
-
192
-           $apiHost = 'https://api.phaxio.com/v2.1/';
193
-
194
-           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
195
-
196
-
197
-           try {
198
-                      $phaxioresultreq = $phaxio->doRequest("GET", 'faxes/' . $faxid);
199
-
200
-                      $fromnmbrtoarr = $this->object_to_array($phaxioresultreq);
201
-
202
-                      $phaxiofromraw = $fromnmbrtoarr['data']['from_number'];
203
-                      $phaxiotoraw  = $fromnmbrtoarr['data']['to_number'];
204
-                      $phaxiofromnumber = str_replace("+", "", $phaxiofromraw);
205
-                      $phaxiotonumber = str_replace("+", "", $phaxiotoraw);
206
-                      $errortype = 'there are no errors';
207
-
208
-           } catch (InvalidRequestException $e) {
209
-                      $phaxiofromnumber = 'null';
210
-                      $errortype = 'invalid request error';
211
-           } catch (AuthenticationException $e) {
212
-                      $phaxiofromnumber = 'null';
213
-                      $errortype = 'authentication error';
214
-           } catch (APIConnectionException $e) {
215
-                      $phaxiofromnumber = 'null';
216
-                      $errortype = 'API connection error';
217
-           } catch (RateLimitException $e) {
218
-                      $phaxiofromnumber = 'null';
219
-                      $errortype = 'rate limit error';
220
-           } catch (NotFoundException $e) {
221
-                      $phaxiofromnumber = 'null';
222
-                      $errortype = 'not found error';
223
-           } catch (GeneralException $e) {
224
-                      $phaxiofromnumber = 'null';
225
-                      $errortype = 'undefined error';
226
-           }
227
-
228
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
229
-               $this->folder->newFolder('Pax_Fax/faxes_received');
230
-           }
231
-
232
-           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
233
-               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
234
-           }
235
-
236
-           $userroot = $this->view->getRoot();
237
-
238
-           $targetfile = $userroot . "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber  . "_" . $fldate . "." . $filenameext;
239
-
240
-           $target = $this->folder->newFile($targetfile);
241
-
242
-           $target->putContent($fileContent);
243
-
244
-           // Move failed received faxes to the 'faxes_received_failed' folder
245
-           if ($filenameext == '') {
246
-
247
-               $newtargetfl =  $userroot . "/Pax_Fax/faxes_received_failed/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
248
-               $targetact = $this->folder->newFile($newtargetfl);
249
-
250
-               $targetact->putContent($fileContent);
251
-
252
-               $failedfl = $userroot . "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
253
-               $removefailed = $this->filesystem->unlink($failedfl);
254
-           }
255
-
256
-           // Send notifications
257
-           $nameofhost = exec("hostname");
258
-           $getnextnotify = $thisapicred[4];
259
-           $useremailaddr = $thisapicred[5];
260
-
261
-           if ($filenameext != '') {
262
-               $targetflname = $targetfile;
263
-               $targettrim = $userroot . "/Pax_Fax/faxes_received/";
264
-               $validfaxparam = "fax";
265
-               $setsubject = "New fax received";
266
-           } else {
267
-               $targetflname = $newtargetfl;
268
-               $targettrim = $userroot . "/Pax_Fax/faxes_received_failed/";
269
-               $validfaxparam = "failed fax";
270
-               $setsubject = "New failed fax received";
271
-           }
272
-
273
-           if ($useremailaddr != '') {
274
-
275
-               $subject = $setsubject;
276
-               $message = "Hello! <br><br> You have received a new ".$validfaxparam." on ".$flmsdate." .<br> The new ".$validfaxparam." file is '".$targetflname."' . <br><br>____________<br><br> Yours, <br> Pax Fax <br> A fax application for Nextcloud <br> Host: '".$nameofhost."' <br>";
277
-
278
-               // Mention the content-type, because we send an HTML email
279
-               $headers = "MIME-Version: 1.0" . "\r\n";
280
-               $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
281
-
282
-               // Set the email sender
283
-               $headers .= "From: " . $useremailaddr . "\r\n";
284
-               $headers .= "Reply-To: " . $useremailaddr . "\r\n";
285
-
286
-               mail($useremailaddr, $subject, $message, $headers);
287
-           }
288
-
289
-           if ($getnextnotify != 0 ) {
290
-
291
-               $notify = exec("php ./occ notification:generate ".$this->userId." 'Pax Fax has received a new fax !' -l 'The new fax can be found in the \"".$targettrim."\" folder.'");
292
-          }
293
-
294
-       } else { return "access denied"; }
295
-
296
-    }
297
-
298
-}
299
-
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,299 @@
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\AppFramework\ApiController;
29
+use OCP\IRequest;
30
+
31
+use OCP\AppFramework\Http\DataResponse;
32
+use OCP\AppFramework\OCSController;
33
+use OCP\IUserSession;
34
+
35
+use OCP\AppFramework\Controller;
36
+use OCP\Files\IAppData;
37
+use OCA\PaxFax\Service\PaxfaxService;
38
+use OCP\AppFramework\App;
39
+use OCP\Files\NotPermittedException;
40
+use OCP\Files\Folder;
41
+use OC\Files\Filesystem;
42
+use OC\Files\View;
43
+use \ReflectionClass;
44
+
45
+use OCP\Notification;
46
+use OCP\Notification\INotification;
47
+use OCP\Notification\IManager;
48
+use OCP\Notification\IAction;
49
+use \DateTime;
50
+
51
+use Phaxio;
52
+use Phaxio\OperationResult;
53
+use Phaxio\Error\AuthenticationException;
54
+use Phaxio\Error\NotFoundException;
55
+use Phaxio\Error\InvalidRequestException;
56
+use Phaxio\Error\RateLimitException;
57
+use Phaxio\Error\APIConnectionException;
58
+use Phaxio\Error\GeneralException;
59
+
60
+
61
+class AuthorApiController extends ApiController {
62
+
63
+    private $service;
64
+    private $userId;
65
+    private $folder;
66
+    private $filesystem;
67
+    private $view;
68
+    private $userSession;
69
+
70
+    public function __construct($appName, IRequest $request, IUserSession $userSession, PaxfaxService $service, Folder $folder, Filesystem $filesystem, $userId, View $view) {
71
+        parent::__construct(
72
+                $appName,
73
+                $request,
74
+                'PUT, POST, GET, DELETE, PATCH',
75
+                'Authorization, Content-Type, Accept',
76
+                1728000);
77
+
78
+        $this->service = $service;
79
+        $this->userId = $userId;
80
+        $this->folder = $folder;
81
+        $this->filesystem = $filesystem;
82
+        $this->view = $view;
83
+        $this->userSession = $userSession;
84
+    }
85
+
86
+    /**
87
+     * @NoAdminRequired
88
+     */
89
+    public function object_to_array($obj) {
90
+        if(is_object($obj)) $obj = (array)$this->dismount($obj);
91
+        if(is_array($obj)) {
92
+           $new = array();
93
+           foreach($obj as $key => $val) {
94
+               $new[$key] = $this->object_to_array($val);
95
+           }
96
+        }
97
+        else $new = $obj;
98
+        return $new;
99
+    }
100
+
101
+    /**
102
+     * @NoAdminRequired
103
+     */
104
+    public function dismount($object) {
105
+        $reflectionClass = new ReflectionClass(get_class($object));
106
+        $array = array();
107
+        foreach ($reflectionClass->getProperties() as $property) {
108
+           $property->setAccessible(true);
109
+           $array[$property->getName()] = $property->getValue($object);
110
+           $property->setAccessible(false);
111
+        }
112
+        return $array;
113
+    }
114
+
115
+    /**
116
+     * @NoAdminRequired
117
+     */
118
+    public function isValidCallbackRequest($token, $url = null, $postParameters = null, $uploadedFiles = null, $signature = null) {
119
+
120
+	if (!$url) {
121
+	    $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
122
+	}
123
+
124
+	if (!$postParameters) {
125
+	    $postParameters = $_REQUEST;
126
+	}
127
+
128
+	if (!$uploadedFiles) {
129
+	    $uploadedFiles = $_FILES;
130
+	}
131
+
132
+	if (!$signature) {
133
+	    $signature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
134
+	}
135
+
136
+	// Sort the array by keys
137
+	ksort($postParameters);
138
+
139
+	// Append the data array to the url string, with no delimiters
140
+	foreach ($postParameters as $key => $value) {
141
+		 $url .= $key . $value;
142
+	}
143
+
144
+	foreach ($uploadedFiles as $key => $value) {
145
+		 $url .= $key . sha1_file($value['tmp_name']);
146
+	}
147
+
148
+	$hmac = hash_hmac("sha1", $url, $token);
149
+	return $signature == $hmac;
150
+    }
151
+
152
+    /**
153
+     * @NoAdminRequired
154
+     * @NoCSRFRequired
155
+     */
156
+    public function receivefaxphaxio() {
157
+
158
+       $phaxiosignature = $_SERVER['HTTP_X_PHAXIO_SIGNATURE'];
159
+
160
+       $thisapicred = $this->service->getapicredentials($this->userId);
161
+       $phaxiowhtoken = $thisapicred[2];
162
+       $phaxiorecURL = $thisapicred[3];
163
+
164
+       // Verify Phaxio's signature
165
+       $signverify = $this->isValidCallbackRequest($phaxiowhtoken, $phaxiorecURL, null, null, $phaxiosignature);
166
+
167
+       if ($signverify) {
168
+
169
+           $fileContent = file_get_contents($_FILES['file']['tmp_name']);
170
+           $fileNameinit = $_FILES['file']['name'];
171
+
172
+           $fldate = date("Y-m-d_H-i-s_").gettimeofday()["usec"];
173
+
174
+           $gmtind = "UTC " . date('P');
175
+           $flmsdateinit = date("Y-m-d  H:i:s");
176
+           $flmsdate = $flmsdateinit . " " . $gmtind;
177
+
178
+
179
+           $fileNamesec = array_reverse(explode(".", $fileNameinit));
180
+           $filenameext = $fileNamesec[0];
181
+           array_shift($fileNamesec);
182
+
183
+           $fileName = implode("", $fileNamesec);
184
+
185
+           $faxid = str_replace("Fax-", "", $fileName);
186
+
187
+           $apiMode = 'live';
188
+
189
+           $apiKeys[$apiMode] = $thisapicred[0];
190
+           $apiSecrets[$apiMode] = $thisapicred[1];
191
+
192
+           $apiHost = 'https://api.phaxio.com/v2.1/';
193
+
194
+           $phaxio = new Phaxio($apiKeys[$apiMode], $apiSecrets[$apiMode], $apiHost);
195
+
196
+
197
+           try {
198
+                      $phaxioresultreq = $phaxio->doRequest("GET", 'faxes/' . $faxid);
199
+
200
+                      $fromnmbrtoarr = $this->object_to_array($phaxioresultreq);
201
+
202
+                      $phaxiofromraw = $fromnmbrtoarr['data']['from_number'];
203
+                      $phaxiotoraw  = $fromnmbrtoarr['data']['to_number'];
204
+                      $phaxiofromnumber = str_replace("+", "", $phaxiofromraw);
205
+                      $phaxiotonumber = str_replace("+", "", $phaxiotoraw);
206
+                      $errortype = 'there are no errors';
207
+
208
+           } catch (InvalidRequestException $e) {
209
+                      $phaxiofromnumber = 'null';
210
+                      $errortype = 'invalid request error';
211
+           } catch (AuthenticationException $e) {
212
+                      $phaxiofromnumber = 'null';
213
+                      $errortype = 'authentication error';
214
+           } catch (APIConnectionException $e) {
215
+                      $phaxiofromnumber = 'null';
216
+                      $errortype = 'API connection error';
217
+           } catch (RateLimitException $e) {
218
+                      $phaxiofromnumber = 'null';
219
+                      $errortype = 'rate limit error';
220
+           } catch (NotFoundException $e) {
221
+                      $phaxiofromnumber = 'null';
222
+                      $errortype = 'not found error';
223
+           } catch (GeneralException $e) {
224
+                      $phaxiofromnumber = 'null';
225
+                      $errortype = 'undefined error';
226
+           }
227
+
228
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received') == false) {
229
+               $this->folder->newFolder('Pax_Fax/faxes_received');
230
+           }
231
+
232
+           if ($this->folder->nodeExists('Pax_Fax/faxes_received_failed') == false) {
233
+               $this->folder->newFolder('Pax_Fax/faxes_received_failed');
234
+           }
235
+
236
+           $userroot = $this->view->getRoot();
237
+
238
+           $targetfile = $userroot . "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber  . "_" . $fldate . "." . $filenameext;
239
+
240
+           $target = $this->folder->newFile($targetfile);
241
+
242
+           $target->putContent($fileContent);
243
+
244
+           // Move failed received faxes to the 'faxes_received_failed' folder
245
+           if ($filenameext == '') {
246
+
247
+               $newtargetfl =  $userroot . "/Pax_Fax/faxes_received_failed/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
248
+               $targetact = $this->folder->newFile($newtargetfl);
249
+
250
+               $targetact->putContent($fileContent);
251
+
252
+               $failedfl = $userroot . "/Pax_Fax/faxes_received/" . $fileName . "_" . $phaxiofromnumber . "_" . $phaxiotonumber . "_" . $fldate . "." . $filenameext;
253
+               $removefailed = $this->filesystem->unlink($failedfl);
254
+           }
255
+
256
+           // Send notifications
257
+           $nameofhost = exec("hostname");
258
+           $getnextnotify = $thisapicred[4];
259
+           $useremailaddr = $thisapicred[5];
260
+
261
+           if ($filenameext != '') {
262
+               $targetflname = $targetfile;
263
+               $targettrim = $userroot . "/Pax_Fax/faxes_received/";
264
+               $validfaxparam = "fax";
265
+               $setsubject = "New fax received";
266
+           } else {
267
+               $targetflname = $newtargetfl;
268
+               $targettrim = $userroot . "/Pax_Fax/faxes_received_failed/";
269
+               $validfaxparam = "failed fax";
270
+               $setsubject = "New failed fax received";
271
+           }
272
+
273
+           if ($useremailaddr != '') {
274
+
275
+               $subject = $setsubject;
276
+               $message = "Hello! <br><br> You have received a new ".$validfaxparam." on ".$flmsdate." .<br> The new ".$validfaxparam." file is '".$targetflname."' . <br><br>____________<br><br> Yours, <br> Pax Fax <br> A fax application for Nextcloud <br> Host: '".$nameofhost."' <br>";
277
+
278
+               // Mention the content-type, because we send an HTML email
279
+               $headers = "MIME-Version: 1.0" . "\r\n";
280
+               $headers .= "Content-type: text/html; charset=UTF-8" . "\r\n";
281
+
282
+               // Set the email sender
283
+               $headers .= "From: " . $useremailaddr . "\r\n";
284
+               $headers .= "Reply-To: " . $useremailaddr . "\r\n";
285
+
286
+               mail($useremailaddr, $subject, $message, $headers);
287
+           }
288
+
289
+           if ($getnextnotify != 0 ) {
290
+
291
+               $notify = exec("php ./occ notification:generate ".$this->userId." 'Pax Fax has received a new fax !' -l 'The new fax can be found in the \"".$targettrim."\" folder.'");
292
+          }
293
+
294
+       } else { return "access denied"; }
295
+
296
+    }
297
+
298
+}
299
+