Guest User

Untitled

a guest
Jun 19th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. <?php
  2. /* By Sirupsen ... SlimplCMS */
  3. class CMS {
  4.  
  5. var $host;
  6. var $username;
  7. var $password;
  8. var $table;
  9. var $admin;
  10. var $error;
  11. var $BlogFileName;
  12. var $timefix;
  13. var $pageLimit;
  14.  
  15. public function __construct() { //Aka Config!
  16.  
  17. // Database stuff..
  18. $this->host = "localhost"; // Host
  19. $this->username = "sirupsen"; // Username to the databse
  20. $this->password = "safasdf"; // Password to the database
  21. $this->table = "sirupsen_dk_-_sirupsen"; // Database name
  22.  
  23. // Page name stuff
  24. $this->BlogFileName = "index.php"; //Name of the file where the blog is outputed
  25.  
  26. // Layout configuration
  27. $this->pageLimit = "10";
  28.  
  29. // Defaults
  30. $this->admin = "0";
  31. $this->timefix = "5550"; // Should usually be left "0", my server time is fucked so
  32. }
  33.  
  34. public function UnixStamp($fix){ // Converts the Unix Timestamp which came from the database
  35. // to a normal date, and plus a number if the GMT is different..
  36. return date("d-m-Y - G:i:s", $fix+$this->timefix);
  37. }
  38.  
  39. public function cTime($date) { // This makes the time more relevant like:
  40. // Posted.. 2 hours ago, or 2 minutes ago!
  41.  
  42. if(empty($date)) { // Checks if there's any date
  43. return "No date provided";
  44. }
  45.  
  46. $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
  47. $lengths = array("60","60","24","7","4.35","12","10");
  48.  
  49. $now = time();
  50.  
  51. // is it future date or past date
  52. if($now > $datee) {
  53. $difference = $now - $date;
  54. $tense = "ago";
  55.  
  56. } else {
  57. $difference = $date - $now;
  58. $tense = "from now";
  59. }
  60.  
  61. for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
  62. $difference /= $lengths[$j];
  63. }
  64.  
  65. $difference = round($difference);
  66.  
  67. if($difference != 1) {
  68. $periods[$j].= "s";
  69. }
  70.  
  71. return "$difference $periods[$j] {$tense}";
  72. }
  73.  
  74. public function display_public() { // Display the different events
  75. $q = "SELECT * FROM slimplCMS_posts ORDER BY created DESC LIMIT 0,$this->pageLimit";
  76. $r = mysql_query($q);
  77.  
  78. if ( $r !== false && mysql_num_rows($r) > 0 ) {
  79. while ( $a = mysql_fetch_assoc($r) ) {
  80. $title = stripslashes($a['title']);
  81. $bodytext = stripslashes($a['bodytext']);
  82. $created = stripcslashes($a['created']);
  83.  
  84. $timestamp = $this->cTime($created); // Returns the "cool" time
  85.  
  86. $entry_display .= <<<ENTRY_DISPLAY
  87.  
  88. <div class="post">
  89. <h2>
  90. $title
  91. </h2>
  92. <p>
  93. $bodytext
  94. </p>
  95. <i>
  96. $timestamp
  97. </i>
  98. </div>
  99.  
  100. ENTRY_DISPLAY;
  101. }
  102. } else {
  103. $entry_display = <<<ENTRY_DISPLAY
  104.  
  105. <h2> No entries </h2>
  106. <p>
  107. No entries yet in this system, check back later!
  108. </p>
  109.  
  110. ENTRY_DISPLAY;
  111. }
  112. $entry_display .= <<<ADMIN_OPTION
  113.  
  114. <p class="post_link">
  115. <a href="?post=1">New</a>
  116. </p>
  117.  
  118. ADMIN_OPTION;
  119.  
  120. return $entry_display;
  121. }
  122.  
  123. public function display_admin() { // Post new things
  124. return <<<ADMIN_FORM
  125.  
  126. <form action="{$_SERVER['PHP_SELF']}" method="post">
  127.  
  128. <label for="title">Title:</label><br />
  129. <input name="title" id="title" type="text" maxlength="150" />
  130. <div class="clear"></div>
  131.  
  132. <label for="bodytext">Body Text:</label><br />
  133. <textarea name="bodytext" id="bodytext"></textarea>
  134. <div class="clear"></div>
  135.  
  136. <input type="submit" value="Create" />
  137. </form>
  138.  
  139. <br />
  140.  
  141. <a href="$this->BlogFileName">Back to Home</a>
  142.  
  143. ADMIN_FORM;
  144. }
  145.  
  146. public function write($p) { // Write to the database from the form
  147. if ( $_POST['title'] )
  148. $title = mysql_real_escape_string($_POST['title']);
  149. if ( $_POST['bodytext'])
  150. $bodytext = mysql_real_escape_string($_POST['bodytext']);
  151. if ( $title && $bodytext ) {
  152. $created = time();
  153. $sql = "INSERT INTO slimplCMS_posts VALUES('$title','$bodytext','$created')";
  154. return mysql_query($sql);
  155. } else {
  156. $this->errorHandler("Error! You didn't fill out the fields. <a href='?post=1'>Return</a>");
  157. }
  158. }
  159.  
  160. public function errorHandler($error) { // Error handler
  161. echo "<p class='red'>$error</p>";
  162. }
  163.  
  164. public function connect() { // Connect to databse!
  165. mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
  166. mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
  167.  
  168. return $this->buildDB();
  169. }
  170.  
  171. private function buildDB() { // Build the database
  172. $sql = <<<MySQL_QUERY
  173. CREATE TABLE IF NOT EXISTS slimplCMS_posts (
  174. title VARCHAR(150),
  175. bodytext TEXT,
  176. created VARCHAR(100)
  177. )
  178. MySQL_QUERY;
  179.  
  180. return mysql_query($sql);
  181. }
  182.  
  183. }
  184.  
  185. ?>
Add Comment
Please, Sign In to add comment