/
classes.php
185 lines (156 loc) · 4.08 KB
/
classes.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
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
<?php if(!defined('IS_CMS')) die();
/***************************************************************
*
* Datenbank verwaltung fuer Plug-in Bootstrap
* by black-night - Daniel Neef
*
***************************************************************/
class bsCarouselItem {
public $Img;
public $Content;
public function __construct($Img,$Content) {
global $specialchars;
$this->Img = htmlspecialchars(trim($Img));
$this->Content = htmlspecialchars(trim($Content));
}
}
class bsCarousel {
public $ID;
public $Name;
public $Items = array();
public function __construct($Name,$ID="") {
global $specialchars;
if ($ID == "") {
$this->ID = $this->guid();
}else{
$this->ID = $ID;
}
$this->Name = htmlspecialchars(trim($Name));
}
protected function guid(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = chr(123)// "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12)
.chr(125);// "}"
return $uuid;
}
}
public static function compare($a,$b) {
return strcmp($a->Name,$b->Name);
}
}
class bsDatabase {
private static function lookFile($filename) {
if (!file_exists($filename))
touch($filename);
$fp = fopen($filename, "c+");
$retries = 0;
$max_retries = 100;
if (!$fp) {
return false;
}
// keep trying to get a lock as long as possible
do {
if ($retries > 0) {
usleep(rand(1, 10000));
}
$retries += 1;
} while (!flock($fp, LOCK_EX) and $retries <= $max_retries);
// couldn't get the lock, give up
if ($retries == $max_retries) {
return false;
}
return $fp;
}
private static function unlookFile($fileHandle) {
flock($fileHandle, LOCK_UN);
fclose($fileHandle);
return true;
}
public static function deleteEntry($id,$filename) {
$fp = self::lookFile($filename);
if (!$fp)
return false;
$newData = array();
if (filesize($filename) > 0) {
$newData = fread($fp,filesize($filename));
$newData = trim(str_replace("<?php die(); ?>","",$newData));
$newData = unserialize($newData);
}
for ($i = 0; $i < count($newData); $i++) {
$Carousel = $newData[$i];
if ($Carousel->ID == $id) {
unset($newData[$i]);
break;
}
}
$newData = array_values($newData);
rewind($fp);
ftruncate($fp,0);
fwrite($fp, "<?php die(); ?>\n".serialize($newData));
self::unlookFile($fp);
return true;
}
public static function loadArray($filename) {
if (!file_exists($filename))
touch($filename);
$data = file_get_contents($filename);
$data = trim(str_replace("<?php die(); ?>","",$data));
return unserialize($data);
}
public static function saveArray($filename, $data) {
$fp = self::lookFile($filename);
if (!$fp)
return false;
rewind($fp);
ftruncate($fp,0);
fwrite($fp, "<?php die(); ?>\n".serialize($data));
self::unlookFile($fp);
return true;
}
public static function appendArray($filename, $data) {
$fp = self::lookFile($filename);
if (!$fp)
return false;
$newData = array();
if (filesize($filename) > 0) {
$newData = fread($fp,filesize($filename));
$newData = trim(str_replace("<?php die(); ?>","",$newData));
$newData = unserialize($newData);
}
$newData[] = $data;
rewind($fp);
ftruncate($fp,0);
fwrite($fp, "<?php die(); ?>\n".serialize($newData));
self::unlookFile($fp);
return true;
}
public static function LoadCarousel($filename, $name) {
$data = self::loadArray($filename);
foreach ($data as $value) {
if ($value->Name == $name) {
return $value;
};
}
return false;
}
public static function LoadCarouselByID($filename, $id) {
$data = self::loadArray($filename);
foreach ($data as $value) {
if ($value->ID == $id) {
return $value;
};
}
return false;
}
}
?>