pm-class
database class:
Code (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
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
<?php
class database{
var $dbh;
function connect(){
$hostname = "";
$database = "";
$username = "";
$password = "";
try {
$this->dbh = new PDO("mysql:host=".$hostname.";dbname=".$database.";",$username, $password);
}
catch(PDOException $e){
echo $e->getMessage();
}
}
function dbselect($sql){
$result = $this->dbh->query($sql);
if($result->rowCount() <= 1)
{
$result = $result->fetch(PDO::FETCH_ASSOC);
}
elseif($result->rowCount() > 1)
{
$result = $result->fetchall(PDO::FETCH_ASSOC);
}
return $result;
}
function dbquery($sql){
$result = $this->dbh->exec($sql);
}
}
?>
class database{
var $dbh;
function connect(){
$hostname = "";
$database = "";
$username = "";
$password = "";
try {
$this->dbh = new PDO("mysql:host=".$hostname.";dbname=".$database.";",$username, $password);
}
catch(PDOException $e){
echo $e->getMessage();
}
}
function dbselect($sql){
$result = $this->dbh->query($sql);
if($result->rowCount() <= 1)
{
$result = $result->fetch(PDO::FETCH_ASSOC);
}
elseif($result->rowCount() > 1)
{
$result = $result->fetchall(PDO::FETCH_ASSOC);
}
return $result;
}
function dbquery($sql){
$result = $this->dbh->exec($sql);
}
}
?>
class.php:
Code (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
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
<?php
class messaging{
var $smilies = array(
':)' => "smile.gif",
':(' => "sad.gif",
':D' => "biggrin.gif",
':d' => "biggrin.gif",
':p' => "tongue.gif",
':P' => "tongue.gif",
':-)' => "unsure.gif",
'(A)' => "angel.gif",
'(a)' => "angel.gif",
':s' => "blink.gif",
':S' => "blink.gif",
':$' => "blush.gif",
'(h)' => "cool.gif",
'(H)' => "cool.gif",
':\'(' => "cry.gif",
'--' => "dry.gif",
'-_-' => "dry.gif",
'^^' => "happy.gif",
'^_^' => "happy.gif",
':|' => "huh.gif",
';d' => "laugh.gif",
';D' => "laugh.gif",
':@' => "mad.gif",
':o' => "ohmy.gif",
':O' => "ohmy.gif",
':0' => "ohmy.gif",
'8S' => "woot.gif",
'8s' => "woot.gif",
'8)' => "wacko.gif",
';)' => "wink.gif",
);
function bb_code($check) { // maakt ubb codes
foreach($this->smilies AS $find => $replace)
{
$check = str_replace(htmlentities($find), "<img src='images/smileys/".$replace."' alt='smileys' />", $check); // voegt smileys toe
}
$check = preg_replace("#\[img\](http(s)?://)([a-zA-Z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\[/img\]#", "<img src='\\1\\3' />", $check); // maakt plaatje
$check = preg_replace("#\[color=(\#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]#si", "<font color='\\1'>\\2</font>", $check); // maakt gekleurde letters
return $check;
}
function check_message($check){ // bericht beveiligen
$check = mysql_real_escape_string($check);
$check = bb_code($check);
$check = nl2br($check);
return $check;
}
function check_subject($check){ // titel beveiligen
$check = mysql_real_escape_string($check);
return $check;
}
function send($from,$to,$subject,$message){ // bericht verzenden
$message = $this->check_message($message);
$subject = $this->check_subject($subject);
$db->dbquery("INSERT INTO pm (msg_from,msg_to,subject,time,message) VALUES (".$from.",".$to.",'".$subject."',NOW(),'".$message."')");
}
}
?>
class messaging{
var $smilies = array(
':)' => "smile.gif",
':(' => "sad.gif",
':D' => "biggrin.gif",
':d' => "biggrin.gif",
':p' => "tongue.gif",
':P' => "tongue.gif",
':-)' => "unsure.gif",
'(A)' => "angel.gif",
'(a)' => "angel.gif",
':s' => "blink.gif",
':S' => "blink.gif",
':$' => "blush.gif",
'(h)' => "cool.gif",
'(H)' => "cool.gif",
':\'(' => "cry.gif",
'--' => "dry.gif",
'-_-' => "dry.gif",
'^^' => "happy.gif",
'^_^' => "happy.gif",
':|' => "huh.gif",
';d' => "laugh.gif",
';D' => "laugh.gif",
':@' => "mad.gif",
':o' => "ohmy.gif",
':O' => "ohmy.gif",
':0' => "ohmy.gif",
'8S' => "woot.gif",
'8s' => "woot.gif",
'8)' => "wacko.gif",
';)' => "wink.gif",
);
function bb_code($check) { // maakt ubb codes
foreach($this->smilies AS $find => $replace)
{
$check = str_replace(htmlentities($find), "<img src='images/smileys/".$replace."' alt='smileys' />", $check); // voegt smileys toe
}
$check = preg_replace("#\[img\](http(s)?://)([a-zA-Z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\[/img\]#", "<img src='\\1\\3' />", $check); // maakt plaatje
$check = preg_replace("#\[color=(\#[0-9A-F]{6}|[a-z\-]+)\](.*?)\[/color\]#si", "<font color='\\1'>\\2</font>", $check); // maakt gekleurde letters
return $check;
}
function check_message($check){ // bericht beveiligen
$check = mysql_real_escape_string($check);
$check = bb_code($check);
$check = nl2br($check);
return $check;
}
function check_subject($check){ // titel beveiligen
$check = mysql_real_escape_string($check);
return $check;
}
function send($from,$to,$subject,$message){ // bericht verzenden
$message = $this->check_message($message);
$subject = $this->check_subject($subject);
$db->dbquery("INSERT INTO pm (msg_from,msg_to,subject,time,message) VALUES (".$from.",".$to.",'".$subject."',NOW(),'".$message."')");
}
}
?>
#####
#SQL#
#####
CREATE TABLE `pm` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`msg_from` int(11) NOT NULL,
`msg_to` int(11) NOT NULL,
`subject` text NOT NULL,
`message` text NOT NULL,
`time` datetime NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Members tabel moet minimaal een `id` en een `username` bevatten.
Ook moet er een $_SESSION['username'] aanwezig zijn.
CREATE TABLE `members` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` text NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
#################
##Voorbeeld scriptjes:##
#################
Code (php)
1
2
3
4
5
6
2
3
4
5
6
<?php
# In onderstaande scriptjes gebruik ik de database class.
# Deze kun je op de volgende mabier aanroepen:
# $db = database;
# $db->connect();
?>
# In onderstaande scriptjes gebruik ik de database class.
# Deze kun je op de volgende mabier aanroepen:
# $db = database;
# $db->connect();
?>
Bericht verzenden:
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
<?php
$pm_class = new messaging; #class aanroepen
$from = 1; #verzender
$to = 10; #ontvanger
$subject = "Mijn onderwerp"; #onderwerp
$message = "Mijn bericht"; #bericht
$pm_class->send($from,$to,$subject,$message);#bericht verzenden
?>
$pm_class = new messaging; #class aanroepen
$from = 1; #verzender
$to = 10; #ontvanger
$subject = "Mijn onderwerp"; #onderwerp
$message = "Mijn bericht"; #bericht
$pm_class->send($from,$to,$subject,$message);#bericht verzenden
?>
inbox:
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
<?php
$pm_class = new messaging; #class aanroepen
$bericht = $db->dbselect("SELECT * FROM pm AS p INNER JOIN members AS m WHERE m.username = '".$_SESSION['username']."' AND p.msg_to = m.id"); #inbox
foreach($bericht AS $bericht)
{
echo "Van:".$bericht->msg_from."<br/>";
echo "Onderwerp:".htmlspecialchars($bericht->subject)."<br/>";
}
?>
$pm_class = new messaging; #class aanroepen
$bericht = $db->dbselect("SELECT * FROM pm AS p INNER JOIN members AS m WHERE m.username = '".$_SESSION['username']."' AND p.msg_to = m.id"); #inbox
foreach($bericht AS $bericht)
{
echo "Van:".$bericht->msg_from."<br/>";
echo "Onderwerp:".htmlspecialchars($bericht->subject)."<br/>";
}
?>
verzonden berichten:
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
<?php
$pm_class = new messaging; #class aanroepen
$bericht = $db->dbselect("SELECT * FROM pm AS p INNER JOIN members AS m WHERE m.username = '".$_SESSION['username']."' AND p.msg_from = m.id"); #inbox
foreach($bericht AS $bericht)
{
echo "Van:".$bericht->msg_from."<br/>";
echo "Onderwerp:".htmlspecialchars($bericht->subject)."<br/>";
}
?>
$pm_class = new messaging; #class aanroepen
$bericht = $db->dbselect("SELECT * FROM pm AS p INNER JOIN members AS m WHERE m.username = '".$_SESSION['username']."' AND p.msg_from = m.id"); #inbox
foreach($bericht AS $bericht)
{
echo "Van:".$bericht->msg_from."<br/>";
echo "Onderwerp:".htmlspecialchars($bericht->subject)."<br/>";
}
?>
bericht lezen:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$pm_class = new messaging; #class aanroepen
$id = 6; # bericht id
$bericht = $db->dbselect("SELECT * FROM pm AS p INNER JOIN members AS m WHERE m.username = '".$_SESSION['username']."' AND p.msg_to = m.id AND p.id = ".$id);; #bericht ophalen
foreach($bericht AS $bericht)
{
echo "Van:".$bericht->msg_from."<br/>";
echo "Aan:".$bericht->msg_to."<br/>";
echo "Onderwerp:".$bericht->subject."<br/>";
echo "Bericht:".$bericht->message."<br/><hr/>";
}
?>
$pm_class = new messaging; #class aanroepen
$id = 6; # bericht id
$bericht = $db->dbselect("SELECT * FROM pm AS p INNER JOIN members AS m WHERE m.username = '".$_SESSION['username']."' AND p.msg_to = m.id AND p.id = ".$id);; #bericht ophalen
foreach($bericht AS $bericht)
{
echo "Van:".$bericht->msg_from."<br/>";
echo "Aan:".$bericht->msg_to."<br/>";
echo "Onderwerp:".$bericht->subject."<br/>";
echo "Bericht:".$bericht->message."<br/><hr/>";
}
?>