forked from RmK9/Wanderblog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeletePictures.php
98 lines (82 loc) · 2.87 KB
/
deletePictures.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
<?php
/**
* Created by PhpStorm.
* User: Sean
* Date: 12/15/2015
* Time: 10:09 PM
*/
require_once 'functions.php'; //Grabs any extra functions
if(isset($_POST['data'])){
deletePictures($_POST['data']);
} else if(isset($_GET['postid'])){
deleteAllPictures($_GET['postid']);
}
function deletePictures($data){
$data = json_decode($_POST['data']);
if($data){
try{
$loggedIn = loggedIn();
$oConn = loginToDB();
if ($loggedIn['user_group'] > 1) {
//Gets all the pictures of the logged in author
$query = $oConn->prepare("SELECT * FROM (SELECT p.*, a.Username FROM Adventures a, Pictures p WHERE a.PostID = p.PostID AND a.Username = :username) AS AdventureAuthorUsername;");
$query->bindValue(':username', $loggedIn['username'], PDO::PARAM_STR);
$query->execute();
$pictures = $query->fetchAll(PDO::FETCH_ASSOC);
}
foreach ($data as $d) {
if ($loggedIn['user_group'] === 3){
if(in_array($d, $pictures)){
//Code to remove pic or pics
}
}
//If file exists on server
if (file_exists($d)) {
//remove the file
unlink($d);
echo "Picture deleted from server...";
//query to remove from database
$query = $oConn->prepare("DELETE FROM Pictures WHERE Pictures.Path = :path");
$query->bindValue(':path', $d, PDO::PARAM_STR);
if ($query->execute()) {
echo "Picture deleted from database...";
}
} else {
echo "File not found";
}
}
}catch(PDOException $e){
echo 'ERROR: ' . $e->getMessage();
}finally{
$oConn = null;
}
}else{
echo "No data";
}
}
function deleteAllPictures($postId){
$loggedIn = loggedIn();
$oConn = loginToDB();
try{
if($loggedIn['user_group'] === 3){
$query = $oConn->prepare("SELECT Path FROM Pictures WHERE PostID = :postId");
$query->bindValue(':postId', $postId);
$query->execute();
$picturePaths = $query->fetchAll(PDO::FETCH_ASSOC);
foreach($picturePaths as $key => $p){
unlink($picturePaths[$key]['Path']);
}
$query = $oConn->prepare("DELETE FROM Pictures WHERE PostID = :postId");
$query->bindValue(':postId', $postId);
$query->execute();
echo json_encode(array('success'));
}
}
catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
echo json_encode(array('fail'));
}
finally{
$oConn = null;
}
}