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

Commit b826765

Browse files
committed
Add more tests
1 parent ea4f873 commit b826765

File tree

11 files changed

+143
-28
lines changed

11 files changed

+143
-28
lines changed

src/com/carlgo11/tempfiles/Encryption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static function encrypt(string $input, string $password): array {
2929
foreach ($input as $part) {
3030
$iv = self::createIV($cipher);
3131
$encrypted = openssl_encrypt($part, $cipher, $password, OPENSSL_RAW_DATA, $iv, $tag);
32-
$output[] = implode("&", [base64_encode($encrypted), base64_encode($iv), base64_encode($tag)]);
32+
$output[] = implode('&', [base64_encode($encrypted), base64_encode($iv), base64_encode($tag)]);
3333
}
3434
return $output;
3535
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Cleanup extends API {
1717
*/
1818
public function __construct($method) {
1919
try {
20-
if ($method !== 'DELETE') throw new BadMethod("Bad method. Use DELETE.");
20+
if ($method !== 'DELETE') throw new BadMethod('Bad method. Use DELETE.');
2121
DataStorage::deleteOldFiles();
2222
http_response_code(204);
2323
} catch (Exception $e) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ class Delete extends API {
1717
*/
1818
public function __construct($method) {
1919
try {
20-
if ($method !== 'DELETE') throw new BadMethod("Bad method. Use DELETE.");
21-
$id = filter_var(Misc::getVar('id'), FILTER_VALIDATE_REGEXP, ["options" => ['regexp' => '/^D([0-9]|[A-z]){13}/']]);
20+
if ($method !== 'DELETE') throw new BadMethod('Bad method. Use DELETE.');
21+
$id = filter_var(Misc::getVar('id'), FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^D([0-9]|[A-z]){13}/']]);
2222

2323
if (password_verify(Misc::getVar('delete'), DataStorage::getDeletionPassword($id)))
2424
if (DataStorage::deleteFile($id)) http_response_code(204);
25-
else throw new Exception("Unable to delete file");
26-
else throw new MissingEntry("Bad ID or Password");
25+
else throw new Exception('Unable to delete file');
26+
else throw new MissingEntry('Bad ID or Password');
2727
} catch (Exception $e) {
2828
parent::outputJSON(['error' => $e->getMessage()], $e->getCode() ?: 400);
2929
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ class Download extends API {
1717
*/
1818
public function __construct($method) {
1919
try {
20-
if ($method !== 'GET') throw new BadMethod("Bad method. Use GET.");
20+
if ($method !== 'GET') throw new BadMethod('Bad method. Use GET.');
2121

22-
$id = filter_var(Misc::getVar('id'), FILTER_VALIDATE_REGEXP, ["options" => ['regexp' => '/^D([0-9]|[A-z]){13}/']]);
22+
$id = filter_var(Misc::getVar('id'), FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => '/^D([0-9]|[A-z]){13}/']]);
2323
$p = Misc::getVar('p');
2424
$file = DataStorage::getFile($id, $p);
2525

2626
if (isset($file)) {
2727
$metadata = $file->getMetaData();
2828
$content = base64_encode($file->getContent());
2929
parent::outputJSON([
30-
"type" => $metadata['type'],
31-
"filename" => $metadata['name'],
32-
"length" => $metadata['size'],
33-
"data" => $content
30+
'type' => $metadata['type'],
31+
'filename' => $metadata['name'],
32+
'length' => $metadata['size'],
33+
'data' => $content
3434
], 200);
3535

3636
if ($file->getMaxViews()) { // max views > 0
3737
if ($file->getMaxViews() <= $file->getCurrentViews() + 1) DataStorage::deleteFile($id);
3838
else $file->setCurrentViews($file->getCurrentViews() + 1);
3939
}
40-
} else throw new MissingEntry("File not found");
40+
} else throw new MissingEntry('File not found');
4141
} catch (Exception $e) {
4242
parent::outputJSON(['error' => $e->getMessage()], $e->getCode() ?: 400);
4343
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class Upload extends API {
1818
function __construct($method) {
1919
global $conf;
2020
try {
21-
if ($method !== 'POST') throw new BadMethod("Bad method. Use POST.");
22-
if (!isset($_FILES['file']) || $_FILES['file'] === NULL) throw new Exception("No file uploaded.");
21+
if ($method !== 'POST') throw new BadMethod('Bad method. Use POST.');
22+
if (!isset($_FILES['file']) || $_FILES['file'] === NULL) throw new Exception('No file uploaded.');
2323

2424
$fileArray = $_FILES['file'];
2525
$file = new File($fileArray);
@@ -47,14 +47,14 @@ function __construct($method) {
4747
'type' => $fileArray['type']
4848
]);
4949

50-
if (!DataStorage::saveFile($file, $password)) throw new Exception("File-storing failed.");
50+
if (!DataStorage::saveFile($file, $password)) throw new Exception('File-storing failed.');
5151

5252
$output = array_merge($output, [
5353
'url' => sprintf($conf['download-url'], $file->getID(), $password),
5454
'id' => $file->getID(),
5555
'deletepassword' => $deletionPassword]);
5656

57-
syslog(LOG_INFO, $output['id'] . " created.");
57+
syslog(LOG_INFO, $output['id'] . ' created.');
5858
return parent::outputJSON($output, 201);
5959
} catch (Exception $e) {
6060
parent::outputJSON(['error' => $e->getMessage()], $e->getCode() ?: 400);

src/com/carlgo11/tempfiles/autoload.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function createDirectory($dir) {
3030
checkFile(__DIR__ . '/File.php');
3131
checkFile(__DIR__ . '/api/API.php');
3232
checkFile(__DIR__ . '/datastorage/DataStorage.php');
33-
checkFile(__DIR__ . '/exceptions/BadMethod.php');
34-
checkFile(__DIR__ . '/exceptions/MissingEntry.php');
33+
checkFile(__DIR__ . '/exception/BadMethod.php');
34+
checkFile(__DIR__ . '/exception/MissingEntry.php');
3535

3636
createDirectory($conf['file-path']);
3737

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ public function getEntryMetaData(string $id): ?array;
3232
* Save an uploaded entry.
3333
*
3434
* @param array $file {@see EncryptedFile} object to store
35-
* @param string $password Encryption key
3635
* @param string $deletionPassword Deletion password hash.
3736
* @param array|null $views Views array containing current views and max views.
3837
* @return bool Returns true if file was successfully saved.
3938
* @since 2.5
4039
*/
41-
public function saveEntry(array $file, string $password, string $deletionPassword, array $views = NULL): bool;
40+
public function saveEntry(array $file, string $deletionPassword, array $views = NULL): bool;
4241

4342
/**
4443
* See if an entry with the provided ID exists.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static function saveFile(File $file, string $password): bool {
8383
'metadata' => Encryption::encrypt(implode(";", $file->getMetaData()), $password),
8484
];
8585

86-
return DataStorage::getStorage()->saveEntry($data, $password, $file->getDeletionPassword(), [$file->getCurrentViews(), $file->getMaxViews()]);
86+
return DataStorage::getStorage()->saveEntry($data, $file->getDeletionPassword(), [$file->getCurrentViews(), $file->getMaxViews()]);
8787
}
8888

8989
/**

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,12 @@ public function getEntryViews(string $id): ?array {
8282
* Save an uploaded entry.
8383
*
8484
* @param array $file object to store
85-
* @param string $password Encryption key
8685
* @param string $deletionPassword Deletion password hash.
8786
* @param array|null $views Views array containing current views and max views.
8887
* @return bool Returns true if file was successfully saved.
8988
* @since 2.5
9089
*/
91-
public function saveEntry(array $file, string $password, string $deletionPassword, array $views = NULL): bool {
90+
public function saveEntry(array $file, string $deletionPassword, array $views = NULL): bool {
9291
global $conf;
9392
$newFile = fopen($conf['file-path'] . $file['id'], "w");
9493

@@ -160,7 +159,7 @@ public function listEntries() {
160159
public function updateEntryViews(string $id, int $currentViews): bool {
161160
global $conf;
162161
$file = file_get_contents($conf['file-path'] . $id);
163-
$newFile = fopen($conf['file-path'] . $id, "w");
162+
$newFile = fopen($conf['file-path'] . $id, 'w');
164163
$data = json_decode($file, TRUE);
165164
$views = explode('/', $data['views']);
166165
$data['views'] = "$currentViews/$views[1]";

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class MySQLStorage implements DataInterface {
1212

1313
public function __construct() {
1414
global $conf;
15-
$this->_mysql = new mysqli($conf['MYSQL_HOST'], $conf['MYSQL_USER'], $conf['MYSQL_PASSWORD'], $conf['MYSQL_DATABASE'], $conf['MYSQL_PORT']) or die("error: " + mysqli_error($this->_mysql));
15+
$this->_mysql = new mysqli($conf['MYSQL_HOST'], $conf['MYSQL_USER'], $conf['MYSQL_PASSWORD'], $conf['MYSQL_DATABASE'], $conf['MYSQL_PORT']) or die('error: ' + mysqli_error($this->_mysql));
1616
}
1717

1818
public function __destruct() {
@@ -48,14 +48,14 @@ public function getEntryMetaData(string $id): ?array {
4848
* Save an uploaded entry.
4949
*
5050
* @param array $file {@see EncryptedFile} object to store.
51-
* @param string $password Encryption key.
51+
* @param string $key Encryption key.
5252
* @param string $deletionPassword Deletion password hash.
5353
* @param array|null $views Views array containing current views and max views.
5454
* @return bool Returns true if file was successfully saved.
5555
* @since 2.5
5656
* @since 3.0 Split into 3 tables
5757
*/
58-
public function saveEntry(array $file, string $password, string $deletionPassword, array $views = NULL): bool {
58+
public function saveEntry(array $file, string $key, string $deletionPassword, array $views = NULL): bool {
5959

6060
// INSERT INTO MAIN TABLE
6161
$expiry = (new DateTime('+1 day'))->getTimestamp();

0 commit comments

Comments
 (0)