· 6 years ago · Nov 25, 2019, 09:48 AM
1<?php
2function gen($n){
3 $b = array( 'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit',
4 'a', 'ac', 'accumsan', 'ad', 'aenean', 'aliquam', 'aliquet', 'ante',
5 'aptent', 'arcu', 'at', 'auctor', 'augue', 'bibendum', 'blandit',
6 'class', 'commodo', 'condimentum', 'congue', 'consequat', 'conubia',
7 'convallis', 'cras', 'cubilia', 'curabitur', 'curae', 'cursus',
8 'dapibus', 'diam', 'dictum', 'dictumst', 'dignissim', 'dis', 'donec',
9 'dui', 'duis', 'efficitur', 'egestas', 'eget', 'eleifend', 'elementum',
10 'enim', 'erat', 'eros', 'est', 'et', 'etiam', 'eu', 'euismod', 'ex',
11 'facilisi', 'facilisis', 'fames', 'faucibus', 'felis', 'fermentum',
12 'feugiat', 'finibus', 'fringilla', 'fusce', 'gravida', 'habitant',
13 'habitasse', 'hac', 'hendrerit', 'himenaeos', 'iaculis', 'id',
14 'imperdiet', 'in', 'inceptos', 'integer', 'interdum', 'justo',
15 'lacinia', 'lacus', 'laoreet', 'lectus', 'leo', 'libero', 'ligula',
16 'litora', 'lobortis', 'luctus', 'maecenas', 'magna', 'magnis',
17 'malesuada', 'massa', 'mattis', 'mauris', 'maximus', 'metus', 'mi',
18 'molestie', 'mollis', 'montes', 'morbi', 'mus', 'nam', 'nascetur',
19 'natoque', 'nec', 'neque', 'netus', 'nibh', 'nisi', 'nisl', 'non',
20 'nostra', 'nulla', 'nullam', 'nunc', 'odio', 'orci', 'ornare',
21 'parturient', 'pellentesque', 'penatibus', 'per', 'pharetra',
22 'phasellus', 'placerat', 'platea', 'porta', 'porttitor', 'posuere',
23 'potenti', 'praesent', 'pretium', 'primis', 'proin', 'pulvinar',
24 'purus', 'quam', 'quis', 'quisque', 'rhoncus', 'ridiculus', 'risus',
25 'rutrum', 'sagittis', 'sapien', 'scelerisque', 'sed', 'sem', 'semper',
26 'senectus', 'sociosqu', 'sodales', 'sollicitudin', 'suscipit',
27 'suspendisse', 'taciti', 'tellus', 'tempor', 'tempus', 'tincidunt',
28 'torquent', 'tortor', 'tristique', 'turpis', 'ullamcorper', 'ultrices',
29 'ultricies', 'urna', 'ut', 'varius', 'vehicula', 'vel', 'velit',
30 'venenatis', 'vestibulum', 'vitae', 'vivamus', 'viverra', 'volutpat',
31 'vulputate',
32 );
33 $rand = array();
34 shuffle($b);
35 for ($i = 0; $i < $n; $i++) {
36 $rand[$i] = $b[$i];
37 }
38 $rand = implode(" ", $rand);
39 return $rand;
40}
41
42function db_get_connection() {
43 require "config.php";
44 static $db;
45 try {
46 if (!isset($db)) {
47 $db = new PDO("mysql:host=localhost;dbname=$dbname", $username, $password);
48 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
49 }
50 } catch(PDOException $ex) {
51 echo "Connection failed! " . $ex->getMessage();
52 }
53 return $db;
54}
55
56function add_tables($dbname, $username, $password, $dummyno) {
57 try {
58 $conn1 = new PDO("mysql:host=$host", $username, $password);
59 $conn1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
60 $sql = "DROP DATABASE IF EXISTS " . $dbname . ";";
61 $conn1->exec($sql);
62 $db = "CREATE DATABASE " . $dbname;
63 $usedb = "USE " . $dbname;
64 $conn1->exec($db);
65 $conn1->exec($usedb);
66 $table1 = "CREATE TABLE blog (
67 bid INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
68 title TEXT NOT NULL,
69 content TEXT NOT NULL,
70 date DATE NOT NULL)";
71 $conn1->exec($table1);
72 $table2 = "CREATE TABLE tag (
73 tid INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
74 tags varchar(100) NOT NULL UNIQUE)";
75 $conn1->exec($table2);
76 $table3 = "CREATE TABLE category (
77 cid INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
78 categories varchar(100) NOT NULL UNIQUE)";
79 $conn1->exec($table3);
80 $table4 = "CREATE TABLE relation (
81 blogid INT(11) NOT NULL,
82 tagid INT(11) NOT NULL,
83 categoryid INT(11) NOT NULL,
84 FOREIGN KEY (blogid) REFERENCES blog (bid),
85 FOREIGN KEY (tagid) REFERENCES tag (tid),
86 FOREIGN KEY (categoryid) REFERENCES category (cid))";
87 $conn1->exec($table4);
88 if (isset($_POST['dummyno'])) {
89 for ($k = 0; $k < number_format($dummyno); $k++) {
90 $dummycurrentdate = date("Y-m-d H:i:s");
91 $dummyt = gen(4);
92 $dummyb = gen(100);
93 $dummyta = gen(2);
94 $dummyc = gen(2);
95 $words = explode(" ", $dummyta);
96 $s = sizeof($words);
97 for($i = 0; $i < $s; $i++) {
98 $words[$i] = trim($words[$i]);
99 }
100 $words2 = explode(" ", $dummyc);
101 $s2 = sizeof($words2);
102 for($i = 0; $i < $s2; $i++) {
103 $words2[$i] = trim($words2[$i]);
104 }
105 $sql = "INSERT INTO blog (title, content, date) VALUES ('$dummyt', '$dummyb', '$dummycurrentdate')";
106 $conn1->exec($sql);
107 for ($i = 0; $i < $s; $i++) {
108 $resl = $conn1->query("SELECT tags FROM tag WHERE tags = '$words[$i]'");
109 $count = $resl->rowCount();
110 if ($count == 0) {
111 $tagsq="INSERT INTO tag (tags) VALUES('$words[$i]')";
112 $conn1->exec($tagsq);
113 }
114 $resl2 = $conn1->query("SELECT categories FROM category WHERE categories = '$words2[$i]'");
115 $count2 = $resl2->rowCount();
116 if ($count2 == 0) {
117 $tagsq2="INSERT INTO category (categories) VALUES('$words2[$i]')";
118 $conn1->exec($tagsq2);
119 }
120 $relquery = "INSERT INTO relation (blogid, tagid, categoryid)
121 SELECT bd.bid, tt.tid, cc.cid FROM blog bd
122 JOIN tag tt ON bd.title = '$dummyt' AND tt.tags = '$words[$i]'
123 JOIN category cc ON bd.title = '$dummyt' AND cc.categories = '$words2[$i]'";
124 $conn1->exec($relquery);
125 }
126 }
127 }
128 } catch(PDOException $e) {
129 echo "Connection failed: " . $e->getMessage();
130 }
131}
132
133function add_post($conn, $title, $blogg, $tag, $date) {
134 $tag = str_replace(" ", '',$tag);
135 $tag = rtrim($tag, ",");
136 $tag = strtolower($tag);
137 $words = explode(",", $tag);
138 $s = sizeof($words);
139 for ($i = 0; $i < $s; $i++) {
140 $words[$i] = trim($words[$i]);
141 }
142 try {
143 $blogg = addslashes($blogg);
144 $sql = "INSERT INTO blog (title, content, date) VALUES ('$title', '$blogg', '$date')";
145 $conn->exec($sql);
146 for ($i =0; $i < $s; $i++) {
147 $resl = $conn->query("SELECT tags FROM tag WHERE tags = '$words[$i]'");
148 $count = $resl->rowCount();
149 if ($count == 0) {
150 $tagsq = "INSERT INTO tag (tags) VALUES ('$words[$i]')";
151 $conn->exec($tagsq);
152 }
153 $relquery = "INSERT INTO relation (blogid, tagid, categoryid)
154 SELECT bd.bid, tt.tid, cc.cid
155 FROM blog bd JOIN tag tt
156 ON bd.title = '$title' AND tt.tags = '$words[$i]'
157 JOIN category cc ON bd.title = '$title' AND cc.cid = '$words2[$i]'";
158 $conn->exec($relquery);
159
160 }
161 return TRUE;
162 } catch(PDOException $e) {
163 echo "Connection failed: " . $e->getMessage();
164 }
165}
166
167function update_post($conn, $title, $blogg, $tag, $idval) {
168 $tag = str_replace(" ", '',$tag);
169 $tag = rtrim($tag, ",");
170 $tag = strtolower($tag);
171 $words = explode(",", $tag);
172 $s = sizeof($words);
173 for ($i = 0; $i < $s; $i++) {
174 $words[$i] = trim($words[$i]);
175 }
176 try {
177 $blogg = addslashes($blogg);
178 $sql = "UPDATE blog SET title = '$title', content = '$blogg' WHERE bid = $idval";
179 $conn->exec($sql);
180 $del = "DELETE FROM relation where blogid = $idval";
181 $conn->exec($del);
182 for ($i = 0; $i < $s; $i++) {
183 $resl = $conn->query("SELECT tags FROM tag WHERE tags = '$words[$i]'");
184 $count = $resl->rowCount();
185 if ($count == 0) {
186 $tagsq = "INSERT INTO tag (tags) VALUES ('$words[$i]')";
187 $conn->exec($tagsq);
188 }
189 $relquery = "INSERT INTO relation (blogid, tagid)
190 SELECT bd.bid, tt.tid
191 FROM blog bd JOIN tag tt
192 ON bd.bid = $idval AND tt.tags = '$words[$i]'";
193 $conn->exec($relquery);
194 }
195 return TRUE;
196 } catch(PDOException $e) {
197 echo "Connection failed: " . $e->getMessage();
198 }
199}
200
201function post_displayer($conn, $id) {
202$stmt = $conn->prepare("SELECT title , content, date FROM blog WHERE bid=?");
203$stmt->execute([$id]);
204return $stmt->fetch();
205}
206
207function all_post_displayer($conn, $offset, $n, $sort) {
208 $stmt = $conn->prepare("SELECT bid, title, content, date FROM blog
209 ORDER BY bid $sort LIMIT $offset, $n");
210 $stmt->execute();
211 $data = $stmt->fetchAll();
212 return $data;
213}
214
215function related_post_displayer($conn, $offset, $n, $sort, $tagidvalue2) {
216 $sql = "SELECT bid, title, content, date FROM blog, relation
217 WHERE blog.bid = relation.blogid and $tagidvalue2 = relation.tagid
218 ORDER BY bid $sort LIMIT $offset, $n";
219 if (filter_var($tagidvalue2, FILTER_VALIDATE_INT)) {
220 $stmt = $conn->prepare($sql);
221 $stmt->execute();
222 $data = $stmt->fetchAll();
223 }
224 return $data;
225}
226
227function tag_displayer($idval, $conn) {
228 $sql1 = "SELECT tag.tags, tag.tid FROM relation, tag
229 WHERE relation.blogid = ? AND tag.tid = relation.tagid";
230 $stmt2 = $conn->prepare($sql1);
231 $stmt2->execute([$idval]);
232 return $stmt2->fetchAll();
233}
234
235function category_displayer($idval, $conn) {
236 $sql1 = "SELECT category.cid, category.categories FROM relation, category
237 WHERE relation.blogid = ? AND category.cid = relation.categoryid";
238 $stmt2 = $conn->prepare($sql1);
239 $stmt2->execute([$idval]);
240 return $stmt2->fetchAll();
241}
242
243function page_counter($n, $conn) {
244 $total_pages_sql = "SELECT bid FROM blog";
245 $q1 = $conn->query($total_pages_sql);
246 $total_rows = $q1->rowCount();
247 $total_pages = ceil($total_rows / $n);
248 return $total_pages;
249}
250
251function page_counter_relpost($n, $conn, $tagidvalue2) {
252 $total_pages_sql = "SELECT bid, title, content, date FROM blog, relation
253 WHERE blog.bid = relation.blogid and $tagidvalue2 = relation.tagid";
254 $q1 = $conn->query($total_pages_sql);
255 $total_rows = $q1->rowCount();
256 $total_pages = ceil($total_rows / $n);
257 return $total_pages;
258}
259
260function page_counter_catpost($n, $conn, $catidvalue2) {
261 $total_pages_sql = "SELECT bid, title, content, date FROM blog, relation
262 WHERE blog.bid = relation.blogid and $catidvalue2 = relation.categoryid";
263 $q1 = $conn->query($total_pages_sql);
264 $total_rows = $q1->rowCount();
265 $total_pages = ceil($total_rows / $n);
266 return $total_pages;
267}
268
269function content_trimmer($str) {
270 $words = explode(" ", $str);
271 $cont = implode(" ", array_splice($words, 0, 200));
272 if (str_word_count($cont) > 199) {
273 $cont= $cont."...";
274 }
275 return $cont;
276}
277
278function delete_relation($idval, $conn) {
279 $del="DELETE from relation where blogid = $idval";
280 $conn->exec($del);
281}
282
283function delete_blog($idval, $conn) {
284 $del = "DELETE FROM blog WHERE bid = $idval";
285 $conn->exec($del);
286
287
288}
289?>