· 10 years ago · May 31, 2016, 07:36 PM
1<!DOCTYPE html>
2<html lang="en">
3
4<head>
5 <title>SimpleCMS</title>
6</head>
7
8<body>
9<?php
10
11include_once('_class/CMS.php');
12$obj = new CMS();
13$obj->host = 'mysql.cba.pl';
14$obj->username = 'S14091';
15$obj->password = 'Banan3214';
16$obj->table = 'mojprojektnawpr_c0_pl';
17$obj->connect();
18
19if ( $_POST )
20 $obj->write($_POST);
21
22echo ( $_GET['admin'] == 1 ) ? $obj->display_admin() : $obj->display_public();
23
24?>
25</body>
26
27</html>
28<?php
29
30/**
31 * Created by PhpStorm.
32 * User: Kuba
33 * Date: 2016-05-31
34 * Time: 20:59
35 */
36class CMS
37{
38 var $host;
39 var $surname;
40 var $password;
41 var $table;
42
43 public function display_pub(){
44 $q = "SELECT * FROM CMS ORDER BY created DESC LIMIT 3";
45 $r = mysql_query($q);
46
47 if ( $r !== false && mysql_num_rows($r) > 0 ) {
48 while ( $a = mysql_fetch_assoc($r) ) {
49 $title = stripslashes($a['title']);
50 $bodytext = stripslashes($a['bodytext']);
51
52 $entry_display .= <<<ENTRY_DISPLAY
53
54 <h2>$title</h2>
55 <p>
56 $bodytext
57 </p>
58
59ENTRY_DISPLAY;
60 }
61 } else {
62 $entry_display = <<<ENTRY_DISPLAY
63
64 <h2>This Page Is Under Construction</h2>
65 <p>
66 No entries have been made on this page.
67 Please check back soon, or click the
68 link below to add an entry!
69 </p>
70
71ENTRY_DISPLAY;
72 }
73 $entry_display .= <<<ADMIN_OPTION
74
75 <p class="admin_link">
76 <a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>
77 </p>
78
79ADMIN_OPTION;
80
81 return $entry_display;
82 }
83
84 public function display_admin(){
85 return <<<ADMIN_FORM
86
87 <form action="{$_SERVER['PHP_SELF']}" method="post">
88 <label for="title">Tytuł:</label>
89 <input name="title" id="title" type="text" maxlength="50" />
90 <label for="bodytext">Treść:</label>
91 <textarea name="bodytext" id="bodytext"></textarea>
92 <input type="submit" value="Stwórz!" />
93 </form>
94 ADMIN_FORM;
95 }
96 public function write($p){
97 if ( $p['title'] )
98 $title = mysql_real_escape_string($p['title']);
99 if ( $p['bodytext'])
100 $bodytext = mysql_real_escape_string($p['bodytext']);
101 if ( $title && $bodytext ) {
102 $created = time();
103 $sql = "INSERT INTO testDB VALUES('$title','$bodytext','$created')";
104 return mysql_query($sql);
105 } else {
106 return false;
107 }
108 }
109 public function connect(){
110 mysql_connect($this->host,$this->username,$this->password) or die("Failed to connect " . mysql_error());
111 mysql_select_db($this->table) or die("No database " . mysql_error());
112
113 return $this->buildDB();
114 }
115
116 private function buildDB(){
117 $sql = <<<MySQL_QUERY
118 CREATE TABLE IF NOT EXISTS CMS (
119 title VARCHAR(50),
120 text TEXT,
121 created VARCHAR(100)
122 )
123 MYSQL_QUERY;
124
125 return mysql_query($sql);
126
127 }
128
129
130}
131?>