-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.php
107 lines (84 loc) · 3.52 KB
/
database.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
<?php
// @license MIT (see license file)
$status = '';
/** connect to database */
function dbConnect($dsn, $dbUsername, $dbPassword) : PDO {
global $status;
try {
$db = new PDO($dsn, $dbUsername, $dbPassword);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$status = 'You are now connected to the database.';
} catch ( PDOException $e ) {
echo '###exception caught!! ' . $e->getMessage();
}
return $db;
}
/**
* retrieves the user IP address
*/
function getIpAddress() {
$ip = '';
//$_SERVER['REMOTE_ADDR']
// if(isset($_SERVER['HTTPS_CLIENT_IP'])){
// //ip from share internet
// $ip = $_SERVER['HTTPS_CLIENT_IP'];
// } else if (!empty($_SERVER['HTTPS_X_FORWARDED_FOR'])) {
// //ip pass from proxy
// $ip = $_SERVER['HTTPS_X_FORWARDED_FOR'];
// } else {
// $ip = $_SERVER['REMOTE_ADDR'];
// }
$ip = $_SERVER['REMOTE_ADDR'];
return $ip;
}
function getPostsFromTable(PDO $dbConn) : array {
$query = 'SELECT postID, textContent, ipAddress, fileName, authorName, postDate, altText FROM postsTable ORDER BY postID DESC LIMIT 10;';
$statement = $dbConn->prepare($query);
$statement->execute();
$elements = $statement->fetchAll();
$statement->closeCursor();
$last = count($elements) - 1;
$posts = array();
foreach ($elements as $i => $row) {
$isFirst = ($i == 0);
$isLast = ($i == $last);
// use of appended empty strings to conform to parameters that require strongly typed strings
$post = new Post( $row['postID'] , $row['textContent'], $row['ipAddress'],
$row['fileName'].'', $row['authorName'], $row['postDate'], $row['altText'].'');
array_push($posts, $post);
}
return $posts;
}
/**
* inserts a new post into the database
* @param PDO $dbConn database PDO connection
* @param Post $post post instance
*/
function dbInsert(PDO $dbConn, Post $post) : void {
global $debug, $status;
$queryString = 'INSERT INTO postsTable (postID, textContent, ipAddress, fileName, authorName, postDate, altText)
VALUES (:postID, :textContent,:ipAddress, :fileName,:authorName,:postDate, :altText)';
try {
//setup INSERT query
$statement = $dbConn->prepare($queryString);
$statement->bindValue(':postID', NULL);
$statement->bindValue(':textContent', $post->getTextContent());
$statement->bindValue(':ipAddress', $post->getIpAddress());
$statement->bindValue(':fileName', $post->getFileName());
$statement->bindValue(':authorName', $post->getAuthor());
$statement->bindValue(':postDate', $post->getPostDate());
$statement->bindValue(':altText', $post->getAltText());
//execute the query to mysql
$statement->execute();
$statement->closeCursor();
} catch (PDOException $e) {
echo $e->getMessage();
} catch (Exception $e) {
echo $e->getMessage();
}
if ($debug) {
$status .= ' Inserted records.';
}
// do not return anything (void)
}
?>