-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmortgageContractController.php
More file actions
324 lines (253 loc) · 9.66 KB
/
mortgageContractController.php
File metadata and controls
324 lines (253 loc) · 9.66 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
ob_start();
include_once("PHPMailer-master\PHPMailer-master\PHPMailerAutoload.php");
include 'config.php';
$errors=array();
if($_SERVER['REQUEST_METHOD']=="POST"){
if(isset($_POST['action']) && $_POST['action']=="upload"){
if(empty($_FILES['upload']['name'])){
$errors['error']='Please select a file to upload';
echo json_encode($errors);
exit();
}
$loanid=json_decode($_POST["id"]);
$dir="user/".$loanid."/contract/";
$filename=$_FILES['upload']['name'];
$fnom = explode(".", $filename);
$fileExt = end($fnom);
$i=rand(100,100000);
$location = $dir.$loanid."_".$i.".".$fileExt;
$type=$_FILES['upload']['type'];
$size=$_FILES['upload']['size'];
$servername =DB_HOST;
$username =DB_USER;
$password =DB_PASSWORD;
$dbname =DB_DATABASE;
try {
$res="";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("SELECT * FROM contracts WHERE loanid=:lid");
$stmt->bindParam(':lid', $loanid);
$stmt->execute();
$res=$stmt->fetchAll(PDO::FETCH_ASSOC);
if($res){
$errors['error']="A contract already exists for that loan please delete and then add a new one";
echo json_encode($errors);
exit();
}
$stmt=null;
$stmt = $conn->prepare("INSERT INTO contracts(loanid,location,type,size)
VALUES (:lid, :loc, :ty, :sz)");
$stmt->bindParam(':lid', $loanid);
$stmt->bindParam(':loc', $location);
$stmt->bindParam(':ty', $type);
$stmt->bindParam(':sz', $size);
$stmt->execute();
if(!move_uploaded_file($_FILES['upload']['tmp_name'], $location)){
$errors['error']="Sorry file could not be uploaded";
$stmt = $conn->prepare("DELETE FROM contracts WHERE loanid=:o");
$stmt->bindParam(':o', $loanid);
$stmt->execute();
echo json_encode($errors);
exit();
}
$errors['success']=true;
//echo json_encode($errors);
}
catch(PDOException $e){
$errors['error']=$e;
}
$conn=null;
//$re=urlencode(base64_encode($loanid));
//echo $errors;
//header("Location:manageLoanTemplate.php?id=$re");
echo json_encode($errors);
//var_dump(debug_backtrace());
exit();
}
elseif(isset($_POST['action']) && $_POST['action']=="viewcontract"){
$loanid=json_decode($_POST['id']);
viewContract($loanid);
}
elseif(isset($_POST['action']) && ($_POST['action']=="sendcontract")){
$contractid=json_decode(base64_decode($_POST['contract']));
sendContract($contractid);
}
elseif(isset($_POST['action']) && $_POST['action']=="deletecontract"){
if(isset($_POST['contract'])){
$conid=json_decode(base64_decode($_POST['contract']));
deleteContract($conid);
exit();
}
}
}
function redirect($url){
$ch = curl_init();
curl_setopt_array(
$ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER =>false, // return web page
CURLOPT_COOKIESESSION=>true,
CURLOPT_FORBID_REUSE=>true,
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => false, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_POST => 0, // i am sending post data
// this are my post $s
CURLOPT_SSL_VERIFYHOST => 0, // don't verify ssl
CURLOPT_SSL_VERIFYPEER => false, //
// CURLOPT_VERBOSE => 0
));
// execute
$output = curl_exec($ch);
echo curl_error($ch);
// free
curl_close($ch);
}
function viewContract($id){
$errors=array();
$errors['success']=false;
$c=$id;
$servername=DB_HOST;
$dbname=DB_DATABASE;
$out="";
$res="";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", DB_USER, DB_PASSWORD);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt=$conn->prepare("SELECT * from contracts WHERE loanid=:b");
$stmt->bindParam(":b",$c);
$stmt->execute();
$res=$stmt->fetchAll(PDO::FETCH_ASSOC);
if($res){
$out="<h2>Your records</h2><table class='table table-hover table-bordered table-striped'><thead ><tr class='bg-primary'><th>Contract id</th><th>Loan id</th><th>File Location</th><th>File type</th><th>File size</th><th>Date added</th></tr></thead><tbody>";
foreach($res as $row){
$out.= "<tr data-loanid=".$row['loanid']." ><td>"
.$row['contractid']."</td><td>"
.$row['loanid']."</td><td>"
.$row['location']."</td><td>"
.$row['type']."</td><td>"
.$row['size']."</td><td>".
$row['date']."</td>";
$out.="<td><button class='btn btn-danger' onclick='deletecontract(".json_encode(base64_encode($row['contractid'])).")'>delete contract</button></td>";
$out.="<td><a href='".$row['location']."'><button>view</button></a></td>";
$out.="<td><button class='btn btn-success' type='button' onclick='mailcontract(".json_encode(base64_encode($row['contractid'])).")'>email contract</button></td>";
}
$out.="</tr></tbody></table>";
echo $out;
}
else{
echo $out="Sorry no records found.";
}
}
catch(PDOException $e){
$errors['error']=$e;
}
exit();
}
function deleteContract($id){
$errors=array();
$errors['success']=true;
$servername=DB_HOST;
$dbname=DB_DATABASE;
$out="";
try {
/*
if(is_file($loc)){
$fp=fopen($loc,"r");
//echo $files;
flock($fp, LOCK_EX);
fclose($fp);
}*/
$conn = new PDO("mysql:host=$servername;dbname=$dbname", DB_USER, DB_PASSWORD);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt=$conn->prepare("SELECT location FROM contracts WHERE contractid=:a LIMIT 1");
$stmt->bindParam(":a",$id);
$stmt->execute();
$res=$stmt->fetchAll(PDO::FETCH_ASSOC);
if($res){
foreach($res as $row){
unlink($row['location']);
$stmt=$conn->prepare("DELETE FROM contracts WHERE contractid=:b LIMIT 1");
$stmt->bindParam(":b",$id);
$stmt->execute();
}
$errors['success']="Contract deleted!";
}
//$r=base64_encode($id);
//header("Location:manageLoanTemplate.php?id=$r");
}
catch(PDOException $e){
$errors['error']=$e;
}
$conn=null;
echo json_encode($errors);
exit();
}
function sendContract($con){
$errors=array();
$errors['success']=false;
$servername=DB_HOST;
$dbname=DB_DATABASE;
$out="";
$row="";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", DB_USER, DB_PASSWORD);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt=$conn->prepare("SELECT register.loanid,register.firstname,register.email,contracts.location FROM register,contracts WHERE contracts.loanid=register.loanid AND contracts.contractid=:d LIMIT 1");
$stmt->bindParam(":d",$con);
$stmt->execute();
$row=$stmt->fetchAll(PDO::FETCH_ASSOC);
//var_dump($row);
if($row){
foreach($row as $res){
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.forshorelending.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'loans@forshorelending.com'; // SMTP username
$mail->Password = 'Abraham_123'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
//$mail->confirmReadingTo='loans@forshorelending.com';
//$mail->addReplyTo('loans@forshorelending.com','reply');
$mail->setFrom('loans@forshorelending.com', 'forshorelending');
$mail->addAddress($res['email'], $res['firstname']);
//$mail->addAddress($useremail, $name); // Add a recipient
//$mail->addAddress('ellen@example.com'); // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');
//$mail->addAttachment('a.jpg'); // Add attachments
$mail->addAttachment($res['location'], 'Your contract'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Your contract';
$mail->Body = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Forshorelending account creation</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.forshorelending.com">forshorelending</a>Forshorelending contract</div><div style="padding:24px; font-size:17px;">Hello '.$res['firstname'].',<br /><br />Please find attached your contract. Download sign and return via email to the address below:<br /><br /><br />Login after successful password change using your loanid and new password:<br />* Email: <b>loans@forshorelending.com</b></div></body></html>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
$mail->ErrorInfo;
$errors['error']=$mail;
$errors['success']=false;
} else {
$errors['success']=true;
}
}
}
}
catch(PDOException $e){
$errors['error']= $e;}
echo json_encode($errors);
exit();
}
?>