Let's say I have a directory called "admin". I want only Administrators (users with the rank "Administrator") to use the scripts pulled from the "admin" folder.
Well, let's give a little "example installer".
Run this script one time, and then delete it. (Be sure to create the database first ;)
Create a file named "install.php" or something, and run it, then delete it if it says "Everything Worked!". You may have to create a database. *This will only work 1 time.*
UPDATE: If you ran the query above from PhpMyAdmin, you don't need to make/run this install script.
PHP Code:
<?php
mysql_connect('localhost','root','XXXXX');
mysql_select_db('test');
/* Run the below script only one time! */
//Add Typical Ranks to DB
mysql_query('INSERT INTO `ranks` (`name`) VALUES("Administrator")') or die(mysql_error());
mysql_query('INSERT INTO `ranks` (`name`) VALUES("Moderator")') or die(mysql_error());
mysql_query('INSERT INTO `ranks` (`name`) VALUES("Registered")') or die(mysql_error());
mysql_query('INSERT INTO `ranks` (`name`) VALUES("Guest")') or die(mysql_error());
register('S-p-n','Secret',pull_rank('Administrator'));
//Assuming there's a rank named "Administrator" in the database, this will pull the id.
$rank = pull_rank('Administrator',false); //pull rank id from db where rank name is 'Administrator'
$dir = 'admin'; //The local directory name
add_module($dir); //create module row
$module = pull_module_id($dir); //Grab module id
assign_to_module($rank,$module,'rank'); //Assign rank to module
echo '<p>Everything Worked!</p>';
?>
If you installed the data with no issues, you just created the user 's-p-n' with the password 'Secret', and the rank 'Administrator'. Be aware of that and delete it when you're done testing, please.
You also created a few default ranks, "Guest", "Registered", "Moderator", and "Administrator".
On top of that, you've just added a module entry for the directory "admin".
Create these two files somewhere on your site/web server.
name_me_anything.php
PHP Code:
<?php
session_name('test');
session_start();
mysql_connect('localhost','root','XXXXX');
mysql_select_db('test');
if(isset($_GET['logout']))
{
session_destroy();
session_name('test');
session_start();
echo '<p>Logged Out.</p>';
}
require_once('module_settings.php');
if(isset($_POST['login_btn']))
{
login_user($_POST['user'],$_POST['pass']); //Run this to Log In
}
pull_module('admin/test.php');
if(!isset($_SESSION['users_user']))
{
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Username: <input type="text" name="user" />
<br />
Password: <input type="password" name="pass" />
<br />
<input type="submit" name="login_btn" value="Log In" />
</form>
<?php
}
?>
module_settings.php
PHP Code:
<?php
/*
You can use include, require, or either with the '_once' parameter. You can additionally use file_get_contents, which returns the contents of a file in a string, rather than including it in the script.
//$file=filename.
//$type = include{0/unknown}, require{1}, string{2}
//$once = use _once, 0/unknown=no, 1=yes // Not applicable with the string return type.
*/
function pull_module($file, $type=0,$once=0)
{
if(strpos('/',$file)!==false)
{
$directories = explode('/',$file);
$dir_clause = '';
$dir_root = '';
foreach($directories as $level)
{
$dir_clause.='`directory` = "'.$dir_root.$level.'" OR ';
$dir_root.=$level.'/';
}
$dir_clause = substr($dir_clause,0,strlen($dir_clause)-4);
} else {
$dir_clause = '`directory` = "'.$file.'"';
}
$query_modules = mysql_query('SELECT `directory` FROM `modules`
LEFT OUTER JOIN `module_ranks` ON(`module_ranks`.`module_id` = `modules`.`id`)
LEFT OUTER JOIN `module_users` ON(`module_users`.`module_id` = `modules`.`id`)
WHERE ('.$dir_clause.')
AND (
`rank_id` = "' . $_SESSION['users_rank'] . '"
OR `user_id` = "' . $_SESSION['users_id'] . '"
)') or die(mysql_error());
if(mysql_num_rows($query_modules)==0)
{
return false; //die('Permission Denied For: '.$dir_clause);
}
if($type==2)
{
return file_get_contents($file);
}
else if($type==1)
{
if($once==1)
{
return require_once($file);
} else
{
return require($file);
}
} else
{
if($once==1)
{
return include_once($file);
} else
{
return include($file);
}
}
}
//rank: name from id or id from name
function pull_rank($rank,$type=false)
{
//[type=true{id->name}]
//[type=false{name->id}]
if($type)
{
$query = mysql_query('SELECT `name` FROM `ranks` WHERE `id`="'.$rank.'"') or die(mysql_error());
} else {
$query = mysql_query('SELECT `id` FROM `ranks` WHERE `name`="'.$rank.'"') or die(mysql_error());
}
$row = mysql_fetch_row($query);
return $row[0];
}
//Register a User
function register($user,$pass,$rank=3)
{
return mysql_query('INSERT INTO `users` (`user`,`pass`,`rank`) VALUES("'.mysql_real_escape_string($user).'","'.md5($pass).'","'.$rank.'")') or die(mysql_error());
}
//Be sure to run the sign-in directly after register ;)
//A Function To Set a Session for a User If Their User+Pass is correct.
function login_user($user,$pass)
{
$query = mysql_query('SELECT * FROM `users` WHERE `user` = "'.mysql_real_escape_string($user).'" AND `pass` = "'.md5($pass).'"') or die(mysql_error());
if(mysql_num_rows($query)==1)
{
$row = mysql_fetch_assoc($query);
foreach($row as $key=>$val)
{
$_SESSION['users_'.$key] = $val;
}
return true;
}
return false;
}
//Database a Directory/Module
function add_module($file)
{
mysql_query('INSERT INTO `modules` (`directory`) VALUES("'.mysql_real_escape_string($file).'")') or die(mysql_error());
}
function pull_module_id($dir)
{
$query = mysql_query('SELECT `id` FROM `modules` WHERE `directory` = "'.mysql_real_escape_string($dir).'" LIMIT 1') or die(mysql_error());
$row = mysql_fetch_row($query);
return $row[0];
}
//Assign a Rank/User To a Module
function assign_to_module($data, $module,$type='rank')
{
//$data is either the rank id, or user id; decided by $type.
//$module is the module's id.
//$type must be a string, either 'rank' or 'user'.
if($type=='rank')
{
return mysql_query('INSERT INTO `module_ranks` (`module_id`,`rank_id`) VALUES("'.mysql_real_escape_string($module).'","'.mysql_real_escape_string($data).'")') or die(mysql_error());
} else if($type=='user')
{
return mysql_query('INSERT INTO `module_users` (`module_id`,`user_id`) VALUES("'.mysql_real_escape_string($module).'","'.mysql_real_escape_string($data).'")') or die(mysql_error());
} else return false;
}
?>
Now, in the same directory as the other files, create a folder named "admin".
Add these two files,
.htaccess
test.php
PHP Code:
<?php
echo '<p>Hello, '.$_SESSION['users_user'].' ('.pull_rank($_SESSION['users_rank'],true).')</p>';
echo '<a href="?logout">Log Out</a>?';
?>
Open the file, "name_me_anything.php" (or whatever you named it), and you should see a form to log in. You can log in as "S-p-n" with the password "Secret". Hopefully, viewing the PHP code and reviewing the comments help should help you to get a feel for how it works, and how you can utilize it to suit your needs. It gives you the ability to include scripts, with much less worry about security.