<?php
/***
* Class Permissions
*
* The Permissions class enables the easy management
* of access rights an user data. Reading and editting
* access rights to secured areas and the creation
* of such secured areas is one part of this class.
* On the other hand this class accounts for the
* creation and editting of user data.
* ZodiacXP from tutorials.de optimized the performance
* of the class. Thank you for your help.
*
* @package Permissions
* @version 0.5
* @author Andreas Wilhelm <Andreas2209@web.de>
* @copyright Andreas Wilhelm
**/
class Permissions
{
// private class variables
private $db;
private $userTab = "";
private $modTab = "";
/**
* Constructor - Is called when the class is instanced
*
* @access public
* @param Obj $db
* @param Str $userTab
* @param Str $modTab
* @return NONE
*/
public function __construct(mysqli $db, $userTab, $modTab)
{
// save parameters to class variables
$this->db = $db;
$this->modTab = trim($modTab);
$this->userTab = trim($userTab);
}
/**
* addMod() - Adds a new protected section
*
* @access public
* @param Str $name
* @return Boolean
*/
public function addMod($name)
{
// add new section
$sql = "INSERT INTO " . $this->modTab . "
(`name`)
VALUES
('" . $name . "')";
// send sql-query
$this->db->query($sql);
// check result
if( $this->db->errno )
{
throw new Exception("Cannot create section " . $name);
}
return true;
}
/**
* delMod() - Removes section from table
*
* @access public
* @param Mix $ident
* @return Boolean
*/
public function delMod($ident)
{
// get id of the section
$id = $this->getId($ident, $this->modTab);
// delete section
$sql = "DELETE
FROM
" . $this->modTab . "
WHERE
id = " . $id;
// send sql-query
$this->db->query($sql);
// check result
if( $this->db->errno )
{
throw new Exception("Cannot delete Mod " . $ident);
}
return true;
}
/**
* getMods() - Returns all section-ids
*
* @access public
* @return Array
*/
public function getMods()
{
// get section-ids
$sql = "SELECT
*
FROM
" . $this->modTab;
// send sql-query
$result = $this->db->query($sql);
// check result
if(!$result)
{
throw new Exception("Cannot get sections.");
}
// save perms to array
$mods = array();
while( $row = $result->fetch_assoc() )
{
$mods[] = $row;
}
return $mods;
}
/**
* addUser() - Updates the permissions of an user
*
* @access public
* @param Arr $fields
* @param Arr $values
* @return NONE
*/
public function addUser($fields = array(), $values = array())
{
// save size of arrays
$i = count($fields);
$j = count($values);
// check count of fields and values
if( $i === $j)
{
// create sql-query
$sql = "INSERT INTO " . $this->userTab . " (";
if ($i)
{
// insert field names
$sql .= "`" . implode("`, `", $fields) . "`";
}
$sql .= ") VALUES (";
if ($j)
{
// insert values
$sql .= "'" . implode("', '", $values) . "'";
}
$sql .= ")";
}
else
{
throw new Exception("Invalid user data.");
}
// create an new user
$this->db->query($sql);
// check result
if( $this->db->errno )
{
throw new Exception("Cannot create user.");
}
}
/**
* delUser() - Removes user from table
*
* @access public
* @param Mix $ident
* @return Boolean
*/
public function delUser($ident)
{
// get id of the section
$id = $this->getId($ident, $this->userTab);
// delete section
$sql = "DELETE
FROM
" . $this->userTab . "
WHERE
id = " . $id;
// send sql-query
$this->db->query($sql);
// check result
if( $this->db->errno )
{
throw new Exception("Cannot delete user " . $ident);
}
return true;
}
/**
* editUser() - Changes the data of an user
*
* @access public
* @param Str $user
* @param Arr $fields
* @param Arr $values
* @return NONE
*/
public function editUser($user, $fields = array(), $values = array())
{
// get id of the user
$id = $this->getId($user, $this->userTab);
// get number of entries
$i = count($fields);
$j = count($values);
// check count of fields and values
if( $i === $j )
{
// create sql-query
$sql = "UPDATE " . $this->userTab;
$sql .= " SET ";
// update values
for($i = $i-1; $i >= 0; $i--)
{
$sql .= $fields[$i] . " = '" . $values[$i] . "'";
}
$sql .= " WHERE id = '" . $id . "'";
}
else
{
throw new Exception("Invalid user data.");
}
// edit user data
$this->db->query($sql);
// check result
if( $this->db->errno )
{
throw new Exception("Cannot change user data.");
}
}
/**
* getUser() - Returns data of the given user
*
* @access public
* @param Mix $ident
* @return Array
*/
public function getUser($ident)
{
// get id of the user
$id = $this->getId($ident, $this->userTab);
// get user data
$sql = "SELECT
*
FROM
" . $this->userTab . "
WHERE
id = '" . $id . "'
LIMIT 1";
// send sql-query
$result = $this->db->query($sql);
// check result
if(!$result)
{
throw new Exception("Cannot get " . $ident);
}
// save user data to array
$user = array();
while( $row = $result->fetch_assoc() )
{
$user[] = $row;
}
return $user;
}
/**
* getPerms() - Returns all rights of an user
*
* @access public
* @param Mix $ident
* @return Array
*/
public function getPerms($ident)
{
// get id of the user
$id = $this->getId($ident, $this->userTab);
// get permissions of the user
$sql = "SELECT
`perm`
FROM
" . $this->userTab . "
WHERE
id = " . $id . "
LIMIT 1";
// send sql-query
if ( $result = $this->db->query($sql) )
{
// save result into an array
$array = $result->fetch_row();
// get perm from array
$perm = $array[0];
}
else
{
throw new Exception("Cannot get permissions of user " . $ident);
}
//start counter
$i = 0;
// initiate array of ids
$mods = array();
// extrahate specific permissions
$i = 1;
do
{
if( ($perm & $i) == $i )
{
$mods[] = $i;
}
$i <<= 1;
}
while( $perm >= $i );
return $mods;
}
/**
* calcPerms() - Calculates permission integer of an user
*
* @access public
* @param Arr $mods
* @return Integer
*/
public function calcPerms($mods)
{
// create permission string
$perms = 0;
// create binary permission data
foreach($mods as $id)
{
// create mod perms
$perms += 1 << $id;
}
return $perms;
}
/**
* check() - Checks access rights
*
* @access public
* @param Mix $user
* @param Mix $mod
* @return Boolean
*/
public function check($user, $mod)
{
// get id of the user
$userId = $this->getId($user, $this->userTab);
// get permissions of the user
$sql = "SELECT
`perm`
FROM
" . $this->userTab . "
WHERE
id = " . $userId . "
LIMIT 1";
// send sql-query
if ( $result = $this->db->query($sql) )
{
// save result into an array
$array = $result->fetch_row();
// get perm from array
$perms = $array[0];
}
else
{
throw new Exception("Cannot get permissions of " . $user);
}
// get needed perms
$perms >>= $this->getId($mod, $this->modTab);
return $perms & 1;
}
/**
* getId() - Returns the id to a user or secured area
*
* @access private
* @param Mix $ident
* @param Str $table
* @return Integer
*/
private function getId($ident, $table)
{
// check if identifier is an integer
if( is_numeric($ident) )
{
return $ident;
}
else
{
// get id
$sql = "SELECT
`id`
FROM
" . $table . "
WHERE
name = '" . $ident . "'
LIMIT 1";
// send sql-query
if ( $result = $this->db->query($sql) )
{
// save result into an array
$array = $result->fetch_row();
// get id from array
return $array[0];
}
else
{
return false;
}
}
}
}
?>