· 10 years ago · Jul 30, 2016, 12:06 AM
1<?php
2
3class simpleCMS {
4 var $host;
5 var $username;
6 var $password;
7 var $table;
8
9 public function display_public() {
10 $q = "SELECT * FROM pages ORDER BY created DESC LIMIT 3";
11 $r = mysql_query($q);
12
13 if ( $r !== false && mysql_num_rows($r) > 0 ) {
14 while ( $a = mysql_fetch_assoc($r) ) {
15 $title = stripslashes($a['title']);
16 $bodytext = stripslashes($a['bodytext']);
17
18 $entry_display .= <<<ENTRY_DISPLAY
19
20 <h2>$title</h2>
21 <p>
22 $bodytext
23 </p>
24
25ENTRY_DISPLAY;
26 }
27 } else {
28 $entry_display = <<<ENTRY_DISPLAY
29
30 <h2>This Page Is Under Construction</h2>
31 <p>
32 No entries have been made on this page.
33 Please check back soon, or click the
34 link below to add an entry!
35 </p>
36
37ENTRY_DISPLAY;
38 }
39 $entry_display .= <<<ADMIN_OPTION
40
41 <p class="admin_link">
42 <a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>
43 </p>
44
45ADMIN_OPTION;
46
47 return $entry_display;
48 }
49
50 public function display_admin() {
51 return <<<ADMIN_FORM
52
53 <form action="{$_SERVER['PHP_SELF']}" method="POST">
54 <label for="title">Title:</label>
55 <input name="title" id="title" type="text" maxlength="150" />
56 <label for="bodytext">Body Text:</label>
57 <textarea name="bodytext" id="bodytext"></textarea>
58 <input type="submit" value="Create This Entry!" />
59 </form>
60ADMIN_FORM;
61 }
62
63 public function write($p) {
64 if ( $p['title'] ){
65 $title = mysql_real_escape_string($t);
66 }
67 if ( $p['bodytext'] ){
68 $bodytext = mysql_real_escape_string($b);
69 }
70 if ( $title && $bodytext ) {
71 $created = time();
72 $sql = "INSERT INTO testDB VALUES('$title','$bodytext','$created')";
73 return mysql_query($sql);
74 } else {
75 return false;
76 }
77 }
78
79 public function connect() {
80 mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
81 mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
82
83 return $this->buildDB();
84 }
85
86 private function buildDB() {
87 $sql = <<<MySQL_QUERY
88 CREATE TABLE IF NOT EXISTS testDB (
89 title VARCHAR(150),
90 bodytext TEXT,
91 time VARCHAR(100)
92 )
93MySQL_QUERY;
94
95 return mysql_query($sql);
96 }
97}
98?>
99
100<?php
101 include_once('cms.php');
102 $obj = new simpleCMS();
103 $obj->host = 'localhost';
104 $obj->username = 'root';
105 $obj->password = '';
106 $obj->table = 'content_management_system';
107 $obj->connect();
108
109 if ( $_POST ){
110 $obj->write($_POST);
111 }
112 echo ( $_GET['admin'] == 1 ) ? $obj->display_admin() : $obj->display_public();
113
114 ?>