Tuesday, March 6, 2012

Why dont you create your own CMS???--Part X

Let add some more functions on our 'functions.php'.
We will be looking at each functions in detail.
/***************************************
ESCAPING STRING
*************************************/

function escape($str) {
 global $db;
    $str = get_magic_quotes_gpc()?stripslashes($str):$str;
    $str = mysql_real_escape_string($str, $db->connection);
    return $str;
  }
 
/**************************************
showing a message
EG:-  show_msg('TITLE OF YOUR MESSAGE','CONTENT');
******************************************/

function show_msg($title,$msg)
{
echo '<script src="jquery.js" type="text/javascript">
</script>
 <script src="jquery.freeow.js" type="text/javascript">
</script>';
echo '<script type="text/javascript">
  (function ($) {

  $(document).ready(function() {
 
   var title, message, opts, container;
   message="'.$msg.'";
   title="'.$title.'";      
   $("#freeow-tr").freeow(title,message);
   
  });

}(jQuery));
</script>
';
}

/***************************
 To get the username of the
 logged in person

****************************/
 
function get_username()
{
global $user;
return $user-&gt;get_property('username');
}
/***************************
To get user id of the logged in person

****************************/
function get_user_id()
{
global $user;
return $user-&gt;get_property('u_id');
}
/***************************
Check whether the logged in user is admin or not
The admin has a user id 1

****************************/
function is_admin()
{
global $user;
$id=get_user_id();
if($id==1)
 return true;
else 
 return false;
}
/***************************
Get the various access level of the user

****************************/
function get_user_level()
{
global $user;
return $user-&gt;get_property('level');
}
/***************************
Checks whether user is logged in or not

****************************/
function is_logged_in()
{
global $user;
return $user-&gt;is_logged_in();
}
/***********************************
viewing area to show message
*************************************/

function message_area()
{
echo ('<div class="freeow freeow-top-right" id="freeow-tr"></div>');
}

/*************************************
draw login box
*****************************************/
function draw_login_form()
{
 echo '<div id="login_box"><h1>Login</h1><form action="index.php?action=login" method="post"></form>username: <input name="uname" />

password: <input name="pwd" type="password" />

Remember me? <input name="remember" type="checkbox" value="1" />

<input type="submit" value="login" />
 </div>';
}

/*****************************************
draw register form
*******************************************/
function draw_register_form()
{ 
    echo '<div id="register_box"><h1>Register</h1><form action="index.php?action=register" method="post"></form>username: <input name="username" />

password: <input name="pwd" type="password" />

email: <input name="email" />

<input name="register" type="submit" value="Register user" />
   </div>';
}
 
The function escape is to mysql real escape the string.
Then comes our important function 'show_msg()' function.
function show_msg($title,$msg)
{
echo '<script type="text/javascript" src="jquery.js"></script>
 <script type="text/javascript" src="jquery.freeow.js"></script>';
echo '<script type="text/javascript">
  (function ($) {
  $(document).ready(function() {
   var title, message, opts, container;
   message="'.$msg.'";
   title="'.$title.'";
   $("#freeow-tr").freeow(title,message);
  });
}(jQuery));
</script>';
We are using the free script 'freeow' for showing short messages to the user.The function will accept two arguments-the title and the message.Then we included two javascripts for our function to work.

You can download the scripts from here and copy it in 'stupid' folder.

To show the message you need a message area.Message area can easily be defined by using the message_area() function.Here I have put this below our header.

To obtain the username of the user get_username() can be used.
All other functions are self explanatory.

Now you have to add some css to work with messages.
Please download the modified theme from here

Now we have to include our newly created class files and instantiate in our 'engine.php'.
I am providing the full source code upto our development of stupid cms here..

Download stupid cms

The first registered  user will be the admin.After registering ,login and logout to see all are correct.

5 comments: