Guest User

Untitled

a guest
Jul 19th, 2015
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.40 KB | None | 0 0
  1. <?php
  2.  
  3. class miniCMS {
  4.  
  5.     var $host;
  6.     var $username;
  7.     var $password;
  8.     var $table;
  9.     var $i;
  10.  
  11.  
  12.     public function display_menu(){
  13.         $q = "SELECT * FROM PAGES ORDER BY created";
  14.         $r = mysql_query($q);
  15.         if ( $r !== false && mysql_num_rows($r) > 0 ) {
  16.             while ( $a = mysql_fetch_assoc($r) ) {
  17.                 $title = stripslashes($a['title']);
  18.                 $id = stripslashes($a['id']);
  19.                 if($_SESSION['admin'] == true){
  20.                     $entry_display .= <<<ADMIN_OPTION
  21.                     <div class="menu">
  22.                     <a href="?p={$id}">{$title}</a>
  23.                     </div>
  24. ADMIN_OPTION;
  25.                 }
  26.                 else{  
  27.                     $entry_display .= <<<ENTRY_DISPLAY     
  28.                     <div class="menu">
  29.                     <a href="?p={$id}">{$title}</a>
  30.                     </div>
  31. ENTRY_DISPLAY;
  32.                 }
  33.             }
  34.         }
  35.         return  $entry_display;
  36.     }
  37.  
  38.  
  39.     public function display_page() {
  40.         $q = "SELECT * FROM PAGES WHERE id = {$_GET['p']} ";
  41.         $r = mysql_query($q);
  42.         if ( $r !== false && mysql_num_rows($r) > 0 ) {
  43.             $i=1;
  44.             while ( $a = mysql_fetch_assoc($r) ) {
  45.                 $title = stripslashes($a['title']);
  46.                 $bodytext = stripslashes($a['bodytext']);
  47.                 if($_SESSION['admin'] == false){
  48.                     $entry_display .= <<<ENTRY_DISPLAY
  49.                     <div class="post">
  50.                     <p class="admin_link">{$a['created']}</p>
  51.                     <h2>$title</h2><p> $bodytext</p></div>
  52. ENTRY_DISPLAY;
  53.                 }
  54.                 if($_SESSION['admin'] == true){
  55.                     $entry_display .= <<<ENTRY_DISPLAY
  56.                     <div class="post">
  57.                     <p class="admin_link">{$a['created']}</p>
  58.                     <h2>$title </h2><p>$bodytext
  59.                     <p class="admin_link"><a href="{$_SERVER['PHP_SELF']}?delp={$a['id']}"><img src="x-ico.png"></a></p></p>
  60.                     </div>
  61. ENTRY_DISPLAY;
  62.                 }
  63.             }
  64.         }
  65.         return $entry_display;
  66.     }
  67.  
  68.     public function display_public() {
  69.         $q = "SELECT * FROM DB ORDER BY created DESC LIMIT 10";
  70.         $r = mysql_query($q);
  71.         if ( $r !== false && mysql_num_rows($r) > 0 ) {
  72.             $i=1;
  73.             while ( $a = mysql_fetch_assoc($r) ) {
  74.                 $title = stripslashes($a['title']);
  75.                 $bodytext = stripslashes($a['bodytext']);
  76.                 if($_SESSION['admin'] == true){
  77.                     $entry_display .= <<<ENTRY_DISPLAY
  78.                     <div class="post">
  79.                     <p class="admin_link">{$a['created']}</p>
  80.                     <h2>$title</h2><p>$bodytext
  81.                     <p class="admin_link"><a href="{$_SERVER['PHP_SELF']}?del={$a['id']}"><img src="x-ico.png"></a></p></p>    
  82.                     </div>
  83. ENTRY_DISPLAY;
  84.                 }
  85.          
  86.                 if($_SESSION['admin'] == false){
  87.                     $entry_display .= <<<ENTRY_DISPLAY
  88.                     <div class="post">
  89.                     <p class="admin_link">{$a['created']}</p>
  90.                     <h2>$title</h2><p>$bodytext</p>
  91.                     </div>
  92. ENTRY_DISPLAY;
  93.                 }
  94.                 $i++;
  95.             }
  96.         }
  97.         else{
  98.             $entry_display = <<<ENTRY_DISPLAY
  99.             <h2>Wpisy</h2><p>Brak wpisów</p>
  100. ENTRY_DISPLAY;
  101.         }
  102.         if($_SESSION['admin'] == true){
  103.             $entry_display .= <<<ADMIN_OPTION
  104.             <p class="admin_link">
  105.             <a href="{$_SERVER['PHP_SELF']}?addtweet"><img src="add.png"</a>
  106.             </p>
  107. ADMIN_OPTION;
  108.         }
  109.         return $entry_display;
  110.     }
  111.  
  112.     public function display_admin() {
  113.         if($_SESSION['admin'] == true) {
  114.             return  <<<ADMIN_FORM
  115.             <h1>NOWY WPIS</h1> 
  116.             <form action="{$_SERVER['PHP_SELF']}" method="post">
  117.             <label for="title">Nickname:</label><br />
  118.             <input name="title" id="title" type="text" maxlength="150" />
  119.             <div class="clear"></div>    
  120.             <label for="bodytext">Wpis:</label><br />
  121.             <textarea name="bodytext" id="bodytext"></textarea>
  122.             <div class="clear"></div>    
  123.             <input type="submit" name="add" value="Wrzuć" />    
  124.             </form>
  125.             <h1>NOWA PODSTRONA</h1>
  126.             <form action="{$_SERVER['PHP_SELF']}" method="post">
  127.             <label for="title">TITLE:</label><br />
  128.             <input name="title" id="title" type="text" maxlength="150" />
  129.             <div class="clear"></div>
  130.             <label for="bodytext">BODYTEXT:</label><br />
  131.             <textarea name="bodytext" id="bodytext"></textarea>
  132.             <div class="clear"></div>  
  133.             <input type="submit" name="addp" value="Wrzuć" />
  134.             </form><br/>
  135.             <a href="display.php">Powrót na główną</a>
  136.             <a href="display.php?logout">Wyloguj</a>
  137. ADMIN_FORM;
  138.         }
  139.         else{
  140.             return <<<ADMIN_FORM
  141.             <form action=""{$_SERVER['PHP_SELF']}" method="post">
  142.             <table>
  143.             <tr><th align="right">Login:</th><td align="left"><input type='text' id='login' name='login'></td></tr>
  144.             <tr><th align="right">Hasło:</th><td align="left"><input type='password' id='haslo' name='haslo'></td></tr>
  145.             <tr><td align="center" colspan="2"><input type='submit' value='Zaloguj' name='logowanie'></td></tr>
  146.             </table>
  147.             </form>  
  148. ADMIN_FORM;
  149.         }
  150.     }
  151.  
  152.     public function write() {
  153.         if ( $_POST['title'] )
  154.             $title = mysql_real_escape_string($_POST['title']);
  155.         if ( $_POST['bodytext'])
  156.             $bodytext = mysql_real_escape_string($_POST['bodytext']);
  157.         if ( $title && $bodytext ) {
  158.             $created = date('Y-m-d H:i:s');
  159.             $sql = "INSERT INTO DB VALUES('','$title','$bodytext','$created')";
  160.             return mysql_query($sql);
  161.         }
  162.         else{
  163.             return false;
  164.         }
  165.     }
  166.  
  167.     public function writepage() {
  168.         if ( $_POST['title'] )
  169.             $title = mysql_real_escape_string($_POST['title']);
  170.         if ( $_POST['bodytext'])
  171.             $bodytext = mysql_real_escape_string($_POST['bodytext']);
  172.         if ( $title && $bodytext ) {
  173.             $created = date('Y-m-d H:i:s');
  174.             $sql = "INSERT INTO PAGES VALUES('','$title','$bodytext','$created')";
  175.             return mysql_query($sql);
  176.         }
  177.         else{
  178.             return false;
  179.         }
  180.     }
  181.  
  182.     public function delete() {
  183.         $sql =  "DELETE  FROM DB where id = {$_GET['del']} ";
  184.         mysql_query($sql);
  185.         header( 'refresh: 0.1; url=display.php' );
  186.     }
  187.  
  188.     public function deletep() {
  189.         $sql =  "DELETE  FROM PAGES where id = {$_GET['delp']} ";
  190.         mysql_query($sql);
  191.         header( 'refresh: 0.1; url=display.php' );
  192.     }
  193.  
  194.     public function login() {
  195.         if(htmlspecialchars($_POST['login']) == 'admin' && htmlspecialchars($_POST['haslo']) == 'admin'){
  196.             $_SESSION['admin'] = true;
  197.         }
  198.     }
  199.  
  200.     public function logout() {
  201.         $_SESSION['admin'] = false;
  202.     }
  203.  
  204.     public function connect() {
  205.         mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
  206.         mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
  207.         return $this->buildDB();
  208.     }
  209.  
  210.     private function  buildDB() {
  211.         $sql = <<<MySQL_QUERY
  212.         CREATE TABLE IF NOT EXISTS DB (
  213.             id SERIAL,
  214.             title       VARCHAR(150),
  215.             bodytext    TEXT,
  216.             created     VARCHAR(100)
  217.         )
  218. MySQL_QUERY;
  219.  
  220.         $sql2 = <<<MySQL_QUERY
  221.         CREATE TABLE IF NOT EXISTS PAGES (
  222.             id SERIAL,
  223.             title       VARCHAR(150),
  224.             bodytext    TEXT,
  225.             created     VARCHAR(100)
  226.         )
  227. MySQL_QUERY;
  228.         mysql_query($sql);
  229.         mysql_query($sql2);
  230.     }
  231.  
  232. }
  233.  
  234. ?>
Advertisement
Add Comment
Please, Sign In to add comment