-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoShare.php
124 lines (103 loc) · 4.64 KB
/
doShare.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
require_once("config.php");
session_start();
$debug= 0;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Verifico che la sessione sia valorizzata, altrimenti mando al login
if(!isset($_SESSION['name'])){
header('Location: login.php');
die();
}
//Il campo action definisce la tipologia di azione. Possibili valori sono
//share -> effettua la condivisione con l'insert
//delete -> elimina la condivisione rimuovendo dalla tabella share l'id in questione
if (isset($_GET['action'])){
$action = $_GET['action'];
} else {
die("Error: action not performed");
}
switch ($action){
case "share" :
//TODO: verificare che l'id della password che si vuole condividere appartenga al proprietario
//Recupero la password salvata dal db
$r = $database->get("password", "*", ["id" => $_GET['passwordId']]);
//Generazione del messaggio per la mail
$username = $r['username'];
$url = $r['url'];
$messaggio = "L'utente $username ha condiviso con te delle credenziali per il sito web " . $url . "\nAccedi alla piattaforma per i dettagli";
//Recupero credenziali dal db
$decrypted = null;
$k = openssl_private_decrypt($r['encPassword'], $decrypted, $_SESSION['privkey']);
//adesso recupero la chiave pubblica dell'utente con cui voglio condividere la password
$r = $database->get("user_login", ["pubkey", "notifyOnShare"], ["id" => $_GET['userId']]);
// var_dump($r);die();
$encriptedPassword = null;
openssl_public_encrypt($decrypted, $encriptedPassword, $r['pubkey']);
$database->insert("share",
[
"passwordId" => $_GET['passwordId'],
"userId" => $_GET['userId'],
"encPassword" => $encriptedPassword
] );
$db_id = $database->id();
//Verifico se l'utente vuole la notifica della password
$newUserToShare = $database->get("user_login", "*", ["id" => $_GET['userId']]);
if ($newUserToShare['notifyOnShare'] == 1){
//Ok l'utente vuole la notifica per mail, richiamo la funzione
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = $SMTPDebug; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = $configMailSmtp; // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->Username = $configMailFrom ; // SMTP username
//$mail->Password = 'secret'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
//$mail->Port = 587; // TCP port to connect to
$mail->SMTPSecure = false;
$mail->SMTPAutoTLS = false;
//Recipients
$mail->setFrom($configMailFrom, 'Gestore Password');
$mail->addAddress($newUserToShare['email'], $newUserToShare['full_name']);
// $mail->addAddress('joe@example.net', 'Joe User'); // Add a recipient
// $mail->addAddress('ellen@example.com'); // Name is optional
$mail->addReplyTo($configMailFrom, 'Gestore password');
// $mail->addCC('cc@example.com');
// $mail->addBCC('bcc@example.com');
//Attachments
// $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
// $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->setLanguage('it');
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Nuova password condivisa con te'; //Todo: migliorare il testo
$mail->Body = $messaggio;
$mail->AltBody = $messaggio;
$mail->send();
// echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
}
echo json_encode(["res" => 0, "msg" => "Condivisione creata correttamente"]);
break;
case "delete":
$data = $database->delete("share", ["id" => $_GET['id']]);
if ($data->rowCount() == 1){
echo json_encode(["res" => 0, "msg" => "Condivisione rimossa"]);
} else {
echo json_encode(["res" => 1, "msg" => "Errore nell'eliminazione"]);
}
break;
default:
echo json_encode(["res" => 1, "msg" => "Comando sconosciuto"]);
die();
break;
}
?>