Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 61cf981

Browse files
committed
Add views check
1 parent 229f337 commit 61cf981

File tree

9 files changed

+111
-61
lines changed

9 files changed

+111
-61
lines changed

Download.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ function return404() {
1414
$url = explode('/', strtoupper($_SERVER['REQUEST_URI']));
1515
$id = filter_var($url[1]);
1616
$password = filter_input(INPUT_GET, "p");
17-
18-
$file = DataStorage::getFile($id, $password);
19-
17+
if (is_null($password)) die("No password specified.");
18+
try {
19+
$file = DataStorage::getFile($id, $password);
20+
} catch (Exception $ex) {
21+
return404();
22+
}
2023
$metadata = $file->getMetaData();
2124
$content = base64_encode($file->getContent());
2225

2326
if ($file->getMaxViews()) { // max views > 0
2427
if ($file->getMaxViews() <= $file->getCurrentViews() + 1) DataStorage::deleteFile($id);
25-
else $file->setCurrentViews($file->getCurrentViews() + 1);
28+
else DataStorage::updateViews($id, $file->getCurrentViews() + 1);
2629
}
2730

2831
// Set headers

src/com/carlgo11/tempfiles/EncryptedFile.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class EncryptedFile {
1414
protected $_metadata;
1515
protected string $_id;
1616

17-
public function __toString() {
17+
public function __toString(): string {
1818
return $this->_id;
1919
}
2020

@@ -43,7 +43,7 @@ public function setFileContent(string $blob, string $password) {
4343
* @throws Exception
4444
*/
4545
public function setFileMetaData(array $metadata, File $file, string $password) {
46-
$data = Encryption::encryptFileDetails($metadata, $file->getDeletionPassword(), (int)$file->getCurrentViews(), (int)$file->getMaxViews(), $password);
46+
$data = Encryption::encryptFileDetails($metadata, $file->getDeletionPassword(), $password);
4747
$this->_metadata = $data['data'];
4848
$this->_iv[1] = $data['iv'];
4949
$this->_tag[1] = $data['tag'];

src/com/carlgo11/tempfiles/Encryption.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ public static function decrypt(string $input, string $password, string $iv, stri
8080
*
8181
* @param array $metadata the $_FILES[] array to use.
8282
* @param string $deletionPassword Deletion password to encrypt along with the metadata.
83-
* @param int $currentViews Current views of the file.
84-
* @param int $maxViews Max allowable views of the file before deletion.
8583
* @param string $password Password used to encrypt the data.
8684
* @return array|false
8785
* @throws Exception
@@ -90,19 +88,16 @@ public static function decrypt(string $input, string $password, string $iv, stri
9088
* @since 2.3 Added support for AEAD cipher modes.
9189
* @global array $conf Configuration variables.
9290
*/
93-
public static function encryptFileDetails(array $metadata, string $deletionPassword, int $currentViews, int $maxViews, string $password) {
91+
public static function encryptFileDetails(array $metadata, string $deletionPassword, string $password) {
9492
global $conf;
9593
$cipher = $conf['Encryption-Method'];
9694
$iv = self::getIV($cipher);
9795

98-
99-
$views_string = implode(' ', [$currentViews, $maxViews]);
10096
$data_array = [
10197
base64_encode($metadata['name']),
10298
base64_encode($metadata['size']),
10399
base64_encode($metadata['type']),
104100
base64_encode($deletionPassword),
105-
base64_encode($views_string)
106101
];
107102

108103
$data_string = implode(" ", $data_array);

src/com/carlgo11/tempfiles/Exceptions/BadMethod.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class BadMethod extends Exception {
88

9-
public function __construct($message = "", $code = 0, Throwable $previous = NULL) {
9+
public function __construct($message = "", $code = 400, Throwable $previous = NULL) {
1010
parent::__construct($message, $code, $previous);
1111
}
1212
}

src/com/carlgo11/tempfiles/Exceptions/MissingEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class MissingEntry extends Exception {
1010

11-
public function __construct($message = "", $code = 0, Throwable $previous = NULL) {
11+
public function __construct($message = "No file found with matching ID and Password.", $code = 400, Throwable $previous = NULL) {
1212
parent::__construct($message, $code, $previous);
1313
}
1414
}

src/com/carlgo11/tempfiles/File.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616
class File {
1717

18-
protected $_id;
19-
protected $_content;
20-
protected $_currentViews;
21-
protected $_maxViews = 0;
22-
protected $_deletionPassword;
23-
protected $_metaData;
18+
protected string $_id;
19+
protected string $_content;
20+
protected int $_currentViews = 0;
21+
protected int $_maxViews = 0;
22+
protected string $_deletionPassword;
23+
protected array $_metaData;
2424
protected $_iv;
2525
protected $_time;
2626

@@ -45,7 +45,7 @@ public function __construct($file = NULL, string $id = NULL) {
4545
* @return boolean Returns TRUE if the action was successfully executed, otherwise FALSE.
4646
* @since 2.2
4747
*/
48-
private function generateID() {
48+
private function generateID(): bool {
4949
return is_string($this->_id = strtoupper(uniqid("d")));
5050
}
5151

@@ -56,7 +56,7 @@ private function generateID() {
5656
* @return boolean Returns TRUE if the action was successfully executed, otherwise FALSE.
5757
* @since 2.2
5858
*/
59-
private function setID(string $id) {
59+
private function setID(string $id): bool {
6060
return ($this->_id = $id) === $id;
6161
}
6262

@@ -66,7 +66,7 @@ private function setID(string $id) {
6666
* @return string Returns the ID of the file.
6767
* @since 2.2
6868
*/
69-
public function __toString() {
69+
public function __toString(): string {
7070
return $this->_id;
7171
}
7272

@@ -76,7 +76,7 @@ public function __toString() {
7676
* @return string Returns the ID of the file.
7777
* @since 2.2
7878
*/
79-
public function getID() {
79+
public function getID(): string {
8080
return $this->_id;
8181
}
8282

@@ -86,7 +86,7 @@ public function getID() {
8686
* @return string Returns file content in clear text.
8787
* @since 2.2
8888
*/
89-
public function getContent() {
89+
public function getContent(): string {
9090
return $this->_content;
9191
}
9292

@@ -97,7 +97,7 @@ public function getContent() {
9797
* @return boolean Returns TRUE if the action was successfully executed, otherwise FALSE.
9898
* @since 2.2
9999
*/
100-
public function setContent(string $content) {
100+
public function setContent(string $content): bool {
101101
return ($this->_content = $content) === $content;
102102
}
103103

@@ -107,7 +107,7 @@ public function setContent(string $content) {
107107
* @return int Returns current views/downloads of the file if supplied, otherwise NULL.
108108
* @since 2.2
109109
*/
110-
public function getCurrentViews() {
110+
public function getCurrentViews(): int {
111111
return (int)$this->_currentViews;
112112
}
113113

@@ -128,7 +128,7 @@ public function setCurrentViews(int $views) {
128128
* @return int|null Returns max views of the file if supplied, otherwise NULL.
129129
* @since 2.2
130130
*/
131-
public function getMaxViews() {
131+
public function getMaxViews(): ?int {
132132
return $this->_maxViews;
133133
}
134134

@@ -139,7 +139,7 @@ public function getMaxViews() {
139139
* @return boolean Returns TRUE if the action was successfully executed, otherwise FALSE.
140140
* @since 2.2
141141
*/
142-
public function setMaxViews(int $views) {
142+
public function setMaxViews(int $views): bool {
143143
return ($this->_maxViews = $views) === $views;
144144
}
145145

@@ -149,7 +149,7 @@ public function setMaxViews(int $views) {
149149
* @return string Returns deletion password if supplied, otherwise NULL.
150150
* @since 2.2
151151
*/
152-
public function getDeletionPassword() {
152+
public function getDeletionPassword(): string {
153153
return $this->_deletionPassword;
154154
}
155155

@@ -160,7 +160,7 @@ public function getDeletionPassword() {
160160
* @return boolean Returns TRUE if the action was successfully executed, otherwise FALSE.
161161
* @since 2.2
162162
*/
163-
public function setDeletionPassword(string $deletionPassword) {
163+
public function setDeletionPassword(string $deletionPassword): bool {
164164
return ($this->_deletionPassword = $deletionPassword) === $deletionPassword;
165165
}
166166

@@ -184,7 +184,7 @@ public function getMetaData(string $type = NULL) {
184184
* @throws Exception Throws exception if size isn't a number.
185185
* @since 2.2
186186
*/
187-
public function setMetaData(array $metaData) {
187+
public function setMetaData(array $metaData): bool {
188188
if (!filter_var($metaData['size'], FILTER_VALIDATE_INT, ['min_range' => 0]))
189189
throw new Exception("File size " . $metaData['size'] . " isn't a number.");
190190
else $newMetaData['size'] = $metaData['size'];
@@ -200,7 +200,7 @@ public function getIV() {
200200
return $this->_iv;
201201
}
202202

203-
public function setIV(array $iv) {
203+
public function setIV(array $iv): bool {
204204
return ($this->_iv = $iv) === $iv;
205205
}
206206

@@ -211,7 +211,7 @@ public function setIV(array $iv) {
211211
* @return boolean Returns TRUE if the action was successful, otherwise FALSE.
212212
* @since 2.4
213213
*/
214-
public function setDateTime(DateTime $time) {
214+
public function setDateTime(DateTime $time): bool {
215215
return ($this->_time = $time) === $time && !NULL;
216216
}
217217

@@ -221,7 +221,7 @@ public function setDateTime(DateTime $time) {
221221
* @return DateTime|null Returns the time of which the file will be removed if one is set.
222222
* @since 2.4
223223
*/
224-
public function getDateTime() {
224+
public function getDateTime(): ?DateTime {
225225
return $this->_time;
226226
}
227227
}

src/com/carlgo11/tempfiles/api/Upload.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ function __construct(string $method) {
2525
$file = new File($fileArray);
2626
$output = [];
2727

28-
if (Misc::getVar('maxviews') !== NULL) {
29-
$file->setMaxViews(Misc::getVar('maxviews'));
28+
if (!is_null(Misc::getVar('maxviews'))) {
29+
$file->setMaxViews(Misc::getVar('maxviews') + 1);
3030
$output['maxviews'] = (int)$file->getMaxViews();
3131
}
3232

src/com/carlgo11/tempfiles/datastorage/DataStorage.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use com\carlgo11\tempfiles\EncryptedFile;
66
use com\carlgo11\tempfiles\Encryption;
7+
use com\carlgo11\tempfiles\exception\MissingEntry;
78
use com\carlgo11\tempfiles\File;
89
use DateTime;
910
use Exception;
@@ -25,21 +26,36 @@ class DataStorage {
2526
* @throws Exception Throws exception upon file-fetching failure.
2627
* @since 2.5
2728
*/
28-
public static function getFile(string $id, string $password) {
29-
$storage = DataStorage::getStorage();
30-
$storedContent = $storage->getEntryContent($id);
31-
$storedMetaData = $storage->getEntryMetaData($id);
32-
$storedEncryptionData = $storage->getFileEncryptionData($id);
29+
public static function getFile(string $id, string $password): File {
30+
try {
31+
$storage = DataStorage::getStorage();
32+
$storedContent = $storage->getEntryContent($id);
33+
$storedMetaData = $storage->getEntryMetaData($id);
34+
$storedEncryptionData = $storage->getFileEncryptionData($id);
35+
$storedViews = $storage->getEntryViews($id);
36+
} catch (MissingEntry $ex) {
37+
throw new MissingEntry();
38+
}
3339

3440
$content = Encryption::decrypt(base64_decode($storedContent), $password, $storedEncryptionData['iv'][0], $storedEncryptionData['tag'][0]);
35-
$metadata = Encryption::decrypt($storedMetaData, $password, $storedEncryptionData['iv'][1], $storedEncryptionData['tag'][1]);
36-
$metadata = explode(' ', $metadata);
41+
$metadata = explode(' ', Encryption::decrypt($storedMetaData, $password, $storedEncryptionData['iv'][1], $storedEncryptionData['tag'][1]));
42+
$metadata = ['name' => $metadata[0],
43+
'size' => $metadata[1],
44+
'type' => $metadata[2],
45+
'delpass' => $metadata[3],
46+
];
47+
3748
$file = new File(NULL, $id);
3849
$file->setContent($content);
39-
$file->setDeletionPassword(base64_decode($metadata[3]));
50+
$file->setDeletionPassword(base64_decode($metadata['delpass']));
51+
52+
if ($storedViews !== NULL && sizeof($storedViews) === 2) {
53+
$file->setCurrentViews($storedViews[0] + 1);
54+
$file->setMaxViews($storedViews[1]);
55+
}
4056

4157
// List keys are lost during storage.
42-
$file->setMetaData(['size' => base64_decode($metadata[1]), 'name' => base64_decode($metadata[0]), 'type' => base64_decode($metadata[2])]);
58+
$file->setMetaData(['size' => base64_decode($metadata['size']), 'name' => base64_decode($metadata['name']), 'type' => base64_decode($metadata['type'])]);
4359
return $file;
4460
}
4561

@@ -71,7 +87,7 @@ public static function getStorage() {
7187
* @throws Exception
7288
* @since 2.5
7389
*/
74-
public static function saveFile(File $file, string $password) {
90+
public static function saveFile(File $file, string $password): bool {
7591
$storage = DataStorage::getStorage();
7692

7793
include_once __DIR__ . '/../EncryptedFile.php';
@@ -80,7 +96,7 @@ public static function saveFile(File $file, string $password) {
8096
$encryptedFile->setFileMetaData($file->getMetaData(), $file, $password);
8197
$encryptedFile->setID($file->getID());
8298

83-
return $storage->saveEntry($encryptedFile, $password);
99+
return $storage->saveEntry($encryptedFile, $password, [$file->getCurrentViews(), $file->getMaxViews()]);
84100
}
85101

86102
/**
@@ -104,8 +120,19 @@ public static function deleteOldFiles() {
104120
* @return bool Returns TRUE on success & FALSE on failure.
105121
* @throws Exception Throws any exceptions from the storage classes.
106122
*/
107-
public static function deleteFile(string $id) {
123+
public static function deleteFile(string $id): bool {
108124
$storage = DataStorage::getStorage();
109125
return $storage->deleteEntry($id);
110126
}
127+
128+
/**
129+
* @param string $id
130+
* @param int $currentViews
131+
* @return bool
132+
* @throws Exception
133+
*/
134+
public static function updateViews(string $id, int $currentViews): bool {
135+
$storage = DataStorage::getStorage();
136+
return $storage->updateEntryViews($id, $currentViews);
137+
}
111138
}

0 commit comments

Comments
 (0)