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

Commit 48fe821

Browse files
committed
Add README.md
1 parent 9b76091 commit 48fe821

File tree

6 files changed

+84
-126
lines changed

6 files changed

+84
-126
lines changed

INSTALLATION.md

Lines changed: 0 additions & 118 deletions
This file was deleted.

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# TempFiles Backend
2+
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/Carlgo11/Tempfiles-backend/Test%20PHPUnit?style=for-the-badge)](https://github.com/Carlgo11/Tempfiles-backend/actions)
3+
4+
## API calls :mega:
5+
A list of available API calls can be found over at [Postman](https://documenter.getpostman.com/view/1675224/SW7ezkZn).
6+
7+
## Local installation :desktop-computer:
8+
9+
1. Install PHP, Nginx, Git, MySQL
10+
```BASH
11+
sudo apt update
12+
sudo apt upgrade
13+
sudo apt install nginx php php-fpm php-mysql git mysql
14+
```
15+
16+
2. MySQL will ask you to generate a new password. Remember this password for later.
17+
![MySQL Password input](https://cloud.githubusercontent.com/assets/3535780/25774895/c03b5a3c-3298-11e7-94ac-e10cc4d92b39.png)
18+
19+
3. Download the source code
20+
```BASH
21+
git clone https://github.com/Carlgo11/Tempfiles-backend.git
22+
cd Tempfiles-backend/
23+
```
24+
25+
4. Install the MySQL database.
26+
```BASH
27+
mysql -u root -p < ./resources/install_mysql.sql
28+
```
29+
30+
5. Sign in to MySQL and create a new user
31+
```mysql
32+
mysql - u root -p
33+
CREATE USER 'tempfiles'@'localhost' IDENTIFIED BY '<password>';
34+
grant all privileges on tempfiles.files to `tempfiles`@`localhost`;
35+
flush privileges;
36+
exit;
37+
```
38+
Optionally, if you want to set stricter permissions, The MySQL user only needs _SELECT_, _INSERT_, _UPDATE_, _DELETE_ permissions to the `files` table.
39+
40+
6. Copy the Nginx configurations to the sites-available directory.
41+
```BASH
42+
cp ./ressouces/nginx/*.conf > /etc/nginx/sites-available/
43+
```
44+
45+
7. Set the mysql password and username in the Nginx configurations.
46+
```BASH
47+
nano /etc/nginx/sites-available/*.conf
48+
```
49+
Change the fastcgi_param variable values. Each variable has a comment suffix that describes it's usage.
50+
```
51+
# Env vars
52+
fastcgi_param ag44jc7aqs2rsup2bb6cx7utc 'localhost'; # hostname
53+
fastcgi_param hp7wz20wu4qfbfcmqywfai1j4 'tempfiles'; # username
54+
fastcgi_param mom8c5hrbn8c1r5lro1imfyax 'password'; # password
55+
fastcgi_param qb1yi60nrz3tjjjqqb7l2yqra 'tempfiles'; # database
56+
fastcgi_param rb421p9wniz81ttj7bdgrg0ub 'files'; # table
57+
```
58+
59+
8. Generate certificates.
60+
For HTTPS to work you'll need a certificate. Due to the many different certificate companies and their different ways of generating certificates I won't go into that in this text.
61+
When you have a certificate, change the following lines in both nginx configs:
62+
```
63+
ssl_certificate {path_to_cert}/cert.pem; #Change path
64+
ssl_certificate_key {path_to_key}/privkey.pem; #Change path
65+
```
66+
67+
9. Restart Nginx
68+
```BASH
69+
sudo systemctl restart nginx
70+
```

api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
} catch (Exception $ex) {
3838
// error_log($ex); //Spews out the error to log. Maybe not so good for production env?
3939
$api = new API();
40+
$api->addMessage('success', false);
4041
$api->addMessage('error', $ex->getMessage());
4142
$api->outputJSON(500);
4243
}

src/com/carlgo11/tempfiles/DataStorage.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ public static function getFile(string $id, string $password) {
104104
$query->close();
105105

106106
$file = new File(NULL, $id);
107-
$iv = explode(",", base64_decode($iv_encoded));
107+
$iv = explode(" ", base64_decode($iv_encoded));
108108
$file->setIV($iv);
109109

110110
if ($enc_content !== NULL) {
111111
$metadata_string = Encryption::decrypt($enc_filedata, $password, $iv[2], $iv[3]);
112-
if($metadata_string === FALSE) throw new Exception("Unable to decrypt file metadata.");
112+
if ($metadata_string === FALSE) return FALSE;
113113

114114
/** @var array $metadata_array
115115
* Array containing the following: [name, size, type, deletionPassword, views_array[ 0 => currentViews, 1 => maxViews]]
@@ -143,22 +143,23 @@ public static function getFile(string $id, string $password) {
143143
*/
144144
public static function uploadFile(File $file, string $password) {
145145
global $conf;
146-
global $mysql_connection;
146+
global /** @var \mysqli_driver $mysql_connection */
147+
$mysql_connection;
147148
$fileContent = Encryption::encryptFileContent($file->getContent(), $password);
148149
$fileMetadata = Encryption::encryptFileDetails($file->getMetaData(), $file->getDeletionPassword(), 0, $file->getMaxViews(), $password);
149150
$iv = [$fileContent['iv'], $fileContent['tag'], $fileMetadata['iv'], $fileMetadata['tag']];
150-
$enc_iv = base64_encode(implode(',', $iv));
151+
$enc_iv = base64_encode(implode(' ', $iv));
151152
$null = NULL;
152153

153154
try {
155+
/** @var \mysqli_stmt $query */
154156
$query = $mysql_connection->prepare("INSERT INTO `" . $conf['mysql-table'] . "` (id, iv, metadata, content) VALUES (?, ?, ?, ?)");
155157
if (!$query)
156158
throw new Exception('prepare() failed: ' . htmlspecialchars($mysql_connection->error));
157159

158160
$id = $file->getID();
159161

160-
$bp = $query->bind_param("sssb", $id, $enc_iv, $fileMetadata['data'], $null);
161-
if (!$bp)
162+
if (!$query->bind_param("sssb", $id, $enc_iv, $fileMetadata['data'], $null))
162163
throw new Exception('bind_param() failed: ' . htmlspecialchars($query->error));
163164

164165
// Replace $null with content blob
@@ -167,7 +168,6 @@ public static function uploadFile(File $file, string $password) {
167168

168169
if (!$query->execute())
169170
throw new Exception('execute() failed: ' . htmlspecialchars($query->error));
170-
171171
$query->close();
172172
return TRUE;
173173
} catch (Exception $e) {

src/com/carlgo11/tempfiles/Encryption.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ public static function encryptFileDetails(array $file, string $deletionpass, int
5757
$data_string = implode(" ", $data_array);
5858

5959
$data_enc = base64_encode(openssl_encrypt($data_string, $cipher, $password, OPENSSL_RAW_DATA, $iv, $tag));
60+
if(Encryption::decrypt($data_enc, $password, $iv, $tag) !== FALSE)
6061
return ['data' => $data_enc, 'iv' => $iv, 'tag' => $tag];
62+
else {
63+
error_log("Decryption returned false");
64+
return self::encryptFileDetails($file, $deletionpass, $currentViews, $maxViews, $password);
65+
}
6166
}
6267

6368
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function __construct(string $method) {
1212
if ($method !== 'PURGE') throw new Exception("Bad HTTP method. Use PURGE.");
1313

1414
$status = filter_var(DataStorage::deleteOldFiles(), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
15-
parent::addMessage('status', $status);
15+
parent::addMessage('success', $status);
1616
parent::outputJSON(202);
1717
}
1818
}

0 commit comments

Comments
 (0)