Volkova

SimpleCMS

Jun 15th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.04 KB | None | 0 0
  1. <?php
  2. /*
  3. *    +--------------------------------------------------------------+
  4. *    | Volkova PHP & MySQL CMS                                      |
  5. *    +--------------------------------------------------------------+
  6. *    |                                                              |
  7. *    | Author: Ryan                                                 |
  8. *    | Version: 0.01                                                |
  9. *    |                                                              |
  10. *    |                                                              |
  11. *    |                                                              |
  12. *    +--------------------------------------------------------------+
  13. */
  14.  
  15. class simpleCMS {
  16.  
  17.     var $host;
  18.     var $username;
  19.     var $password;
  20.     var $table;
  21.  
  22.     public function display_id($id) {
  23.         $id = mysql_real_escape_string(htmlentities($id));
  24.        
  25.         $q = "SELECT * FROM testDB WHERE created = '$id'";
  26.         $r = mysql_query($q);
  27.  
  28.         if ( $r !== false && mysql_num_rows($r) > 0 ) {
  29.             while ( $a = mysql_fetch_assoc($r) ) {
  30.                 $title = stripslashes($a['title']);
  31.                 $bodytext = stripslashes($a['bodytext']);
  32.                 $id = stripslashes($a['created']);
  33.                
  34.                 $date = date('H:i:s d-m-Y', $id);
  35.                
  36.                 $entry_display = <<<ENTRY_DISPLAY
  37.  
  38.                 <div class="post">
  39.                     <h2>
  40.                         $title
  41.                     </h2>
  42.                    
  43.                     <h4>
  44.                         $date
  45.                     </h4>
  46.                    
  47.                     <div class="post-body">
  48.                     <p>
  49.                         $bodytext
  50.                     </p>
  51.                     </div>
  52.                 </div>
  53.  
  54. ENTRY_DISPLAY;
  55.             }
  56.         } else {
  57.             $entry_display = <<<ENTRY_DISPLAY
  58.  
  59.             <h2> Article Not Found </h2>
  60.                 <p>
  61.                     This article has not been found.
  62.                     It may have been moved or deleted..
  63.                 </p>
  64.  
  65. ENTRY_DISPLAY;
  66.             }
  67.         return $entry_display;
  68.     }
  69.  
  70.     public function display_public() {
  71.         $amount_page = 3;
  72.  
  73.         $pages = "<div id=\"pages\">";
  74.  
  75.         $SQL = "SELECT COUNT(created) from testDB";
  76.         $result = mysql_query($SQL);
  77.         $row = mysql_fetch_row($result);
  78.         $total_records = $row[0];
  79.         $total_pages = ceil($total_records / $amount_page);
  80.  
  81.         if (isset($_GET["page"])) { $currentpage = $_GET["page"]; } else { $currentpage = 1; };
  82.         $currentpage = mysql_real_escape_string(htmlentities($currentpage));
  83.  
  84.         $pages .= "<span class=\"pages\">";
  85.  
  86.         if ($total_pages > 0 && $currentpage > 1) {
  87.             $pages .= "<a href=\"?page=" . (intval($currentpage) - 1) . "\">Prev &laquo; </a>";
  88.         }
  89.         if ($total_pages > 1) {
  90.             for ( $i= 1; $i <= $total_pages; $i++) {
  91.                 $pages .= "<a href=\"?page=$i\"> $i </a>  ";
  92.                 if ($i != $total_pages) {
  93.                     $pages .=  " - ";
  94.                 }
  95.             }
  96.         }
  97.  
  98.         if ($total_pages > 0 && $currentpage != $total_pages && $total_pages > 1) {
  99.             $pages .= "<a href=\"?page=" . (intval($currentpage) + 1) . "\"> &raquo; Next </a>";
  100.         }
  101.  
  102.         $pages .= "</span>\n</div>";
  103.  
  104.         if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page = 1; };
  105.  
  106.         $start_from = ($page-1) * $amount_page;
  107.  
  108.         $q = "SELECT * FROM testDB ORDER BY created DESC LIMIT $start_from, $amount_page";
  109.         $r = mysql_query($q);
  110.  
  111.         if ( $r !== false && mysql_num_rows($r) > 0 ) {
  112.             while ( $a = mysql_fetch_assoc($r) ) {
  113.                 $title = stripslashes($a['title']);
  114.                 $bodytext = stripslashes($a['bodytext']);
  115.                 $id = stripslashes($a['created']);
  116.                
  117.                 $date = date('H:i d-m-Y', $id);
  118.                
  119.                 $entry_display .= <<<ENTRY_DISPLAY
  120.  
  121.                 <div class="post">
  122.                     <h2>
  123.                         $title
  124.                     </h2>
  125.                    
  126.                     <h4>
  127.                         $date
  128.                     </h4>
  129.                    
  130.                     <div class="post-body">
  131.                     <p>
  132.                         $bodytext
  133.                     </p>
  134.                     </div>
  135.                    
  136.                     <h4>
  137.                     <a href="{$_SERVER['PHP_SELF']}?id=$id">Read more... </a>
  138.                     </h4>
  139.                 </div>
  140.  
  141. ENTRY_DISPLAY;
  142.             }
  143.         } else {
  144.             $entry_display = <<<ENTRY_DISPLAY
  145.  
  146.             <h2> This Page Is Under Construction </h2>
  147.                 <p>
  148.                     No entries have been made on this page.
  149.                     Please check back soon, or click the
  150.                     link below to add an entry!
  151.                 </p>
  152.  
  153. ENTRY_DISPLAY;
  154.             }
  155.         $entry_display .= <<<ADMIN_OPTION
  156.  
  157.         <p class="admin_link">
  158.             <a href="{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>
  159.         </p>
  160.  
  161. ADMIN_OPTION;
  162.         $entry_display .= $pages;
  163.         return $entry_display;
  164.    
  165.     }
  166.  
  167.     public function display_register() {
  168.         $frfff = "INSERT INTO users VALUES (NULL, '$user','202cb962ac59075b964b07152d234b70\',\'user\',\'none\', 1307894307, 1307894307);";
  169.         return <<<REGISTER_FORM
  170.         <form action="{$_SERVER['PHP_SELF']}" method="post">
  171.  
  172.             <label for="username">Username:</label><br />
  173.             <input name="username" id="username" type="text" maxlength="150" />
  174.             <div class="clear"></div>
  175.             <label for="password">Password:</label><br />
  176.             <input name="password" id="password" type="text" maxlength="150" />
  177.             <div class="clear"></div>
  178.             <label for="repassword">Re-enter Password:</label><br />
  179.             <input name="repassword" id="repassword" type="text" maxlength="150" />
  180.             <div class="clear"></div>
  181.  
  182.             <input type="submit" value="Login!" />
  183.         </form>
  184.  
  185.         <br />
  186.  
  187.         <a href="index.php">Back to Home</a> | <a href="{$_SERVER['PHP_SELF']}?admin=1">Login</a>
  188.  
  189.  
  190. REGISTER_FORM;
  191.     }
  192.  
  193.  
  194.     public function display_admin($loggedin) {
  195.         if ( $_COOKIE['username'] && $loggedin == 0) {
  196.             $username = mysql_real_escape_string(htmlentities($_COOKIE['username']));
  197.             $md5pass = mysql_real_escape_string(htmlentities($_COOKIE['md5pass']));
  198.             return $this->login($username,$md5pass);
  199.         }
  200.         if ($loggedin && $_GET['larticles'] != 1) {
  201.             return <<<ADMIN_FORM
  202.  
  203.                         <div id="adminmenu">
  204.             <ul class="ulmenu">
  205.                 <li><a href="{$_SERVER['PHP_SELF']}?admin=1&larticles=1" title="List Articles">List Articles</a></li>
  206.                 <li><a href="/" title="Stuff">Stuff</a></li>
  207.                 <li><a href="/" title="Other stuff">Other Stuff</a></li>
  208.             </ul>
  209.             </div>
  210.            
  211.            
  212.         <form action="{$_SERVER['PHP_SELF']}" method="post">
  213.  
  214.             <label for="title">Title:</label><br />
  215.             <input name="title" id="title" type="text" maxlength="150" />
  216.             <div class="clear"></div>
  217.  
  218.             <label for="bodytext">Body Text:</label><br />
  219.             <textarea name="bodytext" id="bodytext"></textarea>
  220.             <div class="clear"></div>
  221.  
  222.             <input type="submit" value="Create This Entry!" />
  223.         </form>
  224.  
  225.         <br />
  226.  
  227.         <a href="index.php">Back to Home</a> | <a href="{$_SERVER['PHP_SELF']}?logout=1">Logout</a>
  228.  
  229. ADMIN_FORM;
  230.         } elseif ($loggedin && $_GET['larticles'] == 1) {
  231.             if ($_GET['action'] == 'delete') {
  232.                 $created = mysql_real_escape_string(htmlentities($_GET['id']));
  233.                 $q = "DELETE FROM testDB WHERE created = '$created'";
  234.                 $r = mysql_query($q);
  235.             }
  236.             if ($_GET['action'] == 'edit') {
  237.                 $created = mysql_real_escape_string(htmlentities($_GET['id']));
  238.                 $q = "SELECT * FROM testDB WHERE created = '$created' ";
  239.                 $r = mysql_query($q);
  240.                
  241.                 if ( $r !== false && mysql_num_rows($r) > 0 ) {
  242.                     while ( $a = mysql_fetch_assoc($r) ) {
  243.                         $title = stripslashes($a['title']);
  244.                         $bodytext = stripslashes($a['bodytext']);
  245.                         $created= stripslashes($a['created']);
  246.                     }
  247.                 }
  248.                 return <<<EDIT_FORM
  249.                
  250.                 <form action="{$_SERVER['PHP_SELF']}" method="post">
  251.  
  252.             <label for="created">ID:</label><br />
  253.             <input name="created" id="created" type="text" maxlength="150" value="$created"/>
  254.             <div class="clear"></div>
  255.            
  256.             <label for="title">Title:</label><br />
  257.             <input name="edtitle" id="title" type="text" maxlength="150" value="$title"/>
  258.             <div class="clear"></div>
  259.            
  260.  
  261.             <label for="bodytext">Body Text:</label><br />
  262.             <textarea name="edbodytext" id="bodytext">$bodytext</textarea>
  263.             <div class="clear"></div>
  264.  
  265.             <input type="submit" value="Edit This Entry!" />
  266.         </form>
  267.  
  268.         <br />
  269. EDIT_FORM;
  270.             } else {
  271.                 $q = "SELECT * FROM testDB ORDER BY created DESC LIMIT 6";
  272.                 $r = mysql_query($q);
  273.  
  274.                 $articles = "";
  275.                
  276.                 if ( $r !== false && mysql_num_rows($r) > 0 ) {
  277.                     while ( $a = mysql_fetch_assoc($r) ) {
  278.                         $title = stripslashes($a['title']);
  279.                         $bodytext = stripslashes($a['bodytext']);
  280.                         $created = stripslashes($a['created']);
  281.                         $articles .= $title . " - Created: " . date('H:m d-m-y', $created) . "<br>  ";
  282.                         $articles .= "<div class='buttons'>\n";
  283.                         $articles .= "<input type=\"button\" value=\"Delete\" class='inputdelete' onClick=\"delete_article('$created')\" >\n";
  284.                         $articles .= "<input type=\"button\" value=\"Edit\" class='inputedit' onClick=\"parent.location='?admin=1&larticles=1&action=edit&id=$created';\" > <br>\n";
  285.                         $articles .= "</div>\n";
  286.                     }
  287.                 }
  288.                 return <<<ADMIN_FORM
  289.  
  290.                 <div id="adminmenu">
  291.                 <ul class="ulmenu">
  292.                     <li><a href="{$_SERVER['PHP_SELF']}?admin=1" title="New Article">New Article</a></li>
  293.                     <li><a href="{$_SERVER['PHP_SELF']}?admin=1&larticles=1" title="List Articles">List Articles</a></li>
  294.                     <li><a href="/" title="Stuff">Stuff</a></li>
  295.                     <li><a href="/" title="Other stuff">Other Stuff</a></li>
  296.                 </ul>
  297.                 </div>
  298.                
  299.  
  300.                 $articles
  301.  
  302.             <br />
  303.  
  304.             <a href="index.php">Back to Home</a> | <a href="{$_SERVER['PHP_SELF']}?logout=1">Logout</a>
  305.  
  306. ADMIN_FORM;
  307.         }
  308.         } else {
  309.             return <<<LOGIN_FORM
  310.             You are not logged in<br />
  311.         <form action="{$_SERVER['PHP_SELF']}" method="post">
  312.  
  313.             <label for="username">Username:</label><br />
  314.             <input name="username" id="username" type="text" maxlength="150" />
  315.             <div class="clear"></div>
  316.             <label for="password">Password:</label><br />
  317.             <input name="password" id="password" type="text" maxlength="150" />
  318.             <div class="clear"></div>
  319.  
  320.             <input type="submit" value="Login!" />
  321.         </form>
  322.  
  323.         <br />
  324.  
  325.         <a href="index.php">Back to Home</a> | <a href="{$_SERVER['PHP_SELF']}?register=1">Register</a>
  326.  
  327. LOGIN_FORM;
  328.         }
  329.     }
  330.  
  331.     public function update($p) {
  332.         if ( $_POST['edtitle'] )
  333.             $title = mysql_real_escape_string(htmlentities($_POST['edtitle']));
  334.         if ( $_POST['edbodytext'])
  335.             $bodytext = mysql_real_escape_string(htmlentities($_POST['edbodytext']));
  336.         if ( $_POST['created'])
  337.             $created = mysql_real_escape_string(htmlentities($_POST['created']));
  338.         if ( $title && $bodytext ) {
  339.         $SQL = "UPDATE testDB SET title = '$title' WHERE created = '$created'";
  340.         mysql_query($SQL);
  341.         $SQL = "UPDATE testDB SET bodytext = '$bodytext' WHERE created = '$created'";
  342.         mysql_query($SQL);
  343.        
  344.         }
  345.     }
  346.  
  347.     public function write($p) {
  348.         if ( $_POST['title'] )
  349.             $title = mysql_real_escape_string(htmlentities($_POST['title']));
  350.         if ( $_POST['bodytext'])
  351.             $bodytext = mysql_real_escape_string(htmlentities($_POST['bodytext']));
  352.         if ( $title && $bodytext ) {
  353.             $created = time();
  354.             $sql = "INSERT INTO testDB VALUES('$title','$bodytext','$created')";
  355.             return mysql_query($sql);
  356.         } else {
  357.             return false;
  358.         }
  359.     }
  360.  
  361.     public function logout() {
  362.         print "<script>SetCookie('username','$username',-7);</script>";
  363.         print "<script>SetCookie('md5pass','$md5pass',-7);</script>";
  364.         return $this->display_public();
  365.     }
  366.  
  367.     public function login($username, $md5pass) {
  368.         if ( $_POST['username'] )
  369.             $username = mysql_real_escape_string(htmlentities($_POST['username']));
  370.         if ( $_POST['password'])
  371.             $md5pass = md5(mysql_real_escape_string(htmlentities($_POST['password'])));
  372.         $SQL = "SELECT user_id FROM users WHERE username='$username' and password='$md5pass'";
  373.         $result=mysql_query($SQL);
  374.         $loggedin=mysql_num_rows($result);
  375.         while ($db_field = mysql_fetch_assoc($result)) {
  376.             $id = $db_field['user_id'];
  377.         }
  378.         if($loggedin==1) {
  379.             print "<script>SetCookie('username','$username',7);</script>";
  380.             print "<script>SetCookie('md5pass','$md5pass',7);</script>";
  381.             //print "You are logged in";
  382.         } else {
  383.             return false;
  384.         }
  385.         return $this->display_admin($loggedin);
  386.     }
  387.  
  388.     public function upload($p) {
  389.         if ( $_POST['title'] )
  390.             $title = mysql_real_escape_string($_POST['title']);
  391.         else
  392.             return false;
  393.        
  394.        
  395.        
  396.     }
  397.  
  398.     public function connect() {
  399.             mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
  400.             mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
  401.  
  402.     return $this->buildDB();
  403.     }
  404.  
  405.     private function buildDB() {
  406.         $sql = <<<MySQL_QUERY
  407. CREATE TABLE IF NOT EXISTS testDB (
  408. title       VARCHAR(150),
  409. bodytext    TEXT,
  410. created     VARCHAR(100)
  411. )
  412. MySQL_QUERY;
  413.  
  414.         return mysql_query($sql);
  415.     }
  416.  
  417. }
  418.  
  419. ?>
Advertisement
Add Comment
Please, Sign In to add comment