Source for file SecurityUtil.class.php
Documentation is available at SecurityUtil.class.php
* Zikula Application Framework
* @copyright (c) 2001, Zikula Development Team
* @link http://www.zikula.org
* @version $Id: SecurityUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @subpackage SecurityUtil
* @param string $component
* @param string $instance
function checkPermission($component= null, $instance= null, $level= null, $user= null)
static $groupperms = array();
if (!is_numeric($level)) {
return pn_exit ("Invalid security level [ $level] received in SecurityUtil::checkPermission ");
$user = pnUserGetVar('uid');
if (!isset ($GLOBALS['authinfogathered'][$user]) || (int) $GLOBALS['authinfogathered'][$user] == 0) {
if (count($groupperms[$user]) == 0) {
return false; // No permissions
// if the check failed, we save the info so that LogUtil has it readily available
$PNRuntime['security']['last_failed_check']['component'] = $component;
$PNRuntime['security']['last_failed_check']['instance'] = $instance;
$PNRuntime['security']['last_failed_check']['level'] = $level;
$PNRuntime['security']['last_failed_check']['user'] = $user;
* register a permission schema
* @param string $component
// Regenerate static part of key
// Test works because timestamp is embedded in authkey and appended
// at the end of the authkey, so we can test validity of authid as
// well as the number of seconds elapsed since generation.
$timestamp = ($keyexpiry > 0 ? substr($authid, 40, strlen($authid)) : '');
// check build key against authid
// now test if time expired
$elapsedTime = (int) ((int) $timestamp > 0 ? time() - $timestamp: $keyexpiry- 1);
if ($elapsedTime < $keyexpiry) {
* @param string $modname module name
* @return string an encrypted key for use in authorisation of operations
// since we need sessions for authorisation keys we should check
// if a session exists and if not create one
$authid = sha1($key. $timestamp) . $timestamp;
* @param unknown_type $user
* @return array two element array of user and group permissions
// Table columns we use - pnModDBInfoLoad is done in pnInit
$groupmembershipcolumn = $pntable['group_membership_column'];
$grouppermcolumn = $pntable['group_perms_column'];
$vars['Active User'] = 'unregistered';
$vars['Active User'] = $user;
// Get all groups that user is in
if ($fldArray === false) {
static $usergroups = array();
$usergroups[] = 0; // Unregistered GID
$allgroups = array_merge ($usergroups, $fldArray);
$allgroups = implode(',', $allgroups);
// Get all group permissions
$orderBy = "ORDER BY $grouppermcolumn[sequence]";
foreach ($objArray as $obj) {
$component = SecurityUtil::_fixsecuritystring($obj['component']);
$instance = SecurityUtil::_fixsecuritystring($obj['instance']);
// Search/replace of special names
for($i= 0; $i< $size; $i++ ) {
$instance = preg_replace('/<([^>]+)>/', $vars[$res[1][$i]], $instance, 1);
$groupperms[] = array('component' => $component,
// we've now got the permissions info
$GLOBALS['authinfogathered'][$user] = 1;
* @param string $component
* @param string $instance
* @return int matching security level
// If we get a test component or instance purely consisting of ':' signs
// then it counts as blank
// Test for generic permission
if ((empty($component)) && (empty($instance))) {
// Looking for best permission
foreach ($perms as $perm) {
if ($perm['level'] > $level) {
// Test for generic instance
// additional fixes by BMW [larsneo]
// if the instance is empty, then we're looking for the per-module
// if $instance is empty, then there must be a component.
// Looking for best permission
foreach ($perms as $perm) {
if (!preg_match("=^$perm[component]$=", $component)) {
continue; // component doestn't match.
// check that the instance matches :: or '' (nothing)
continue; // instance does not match
// We have a match - set the level and quit
// Normal permissions check
// there *is* a $instance at this point.
foreach ($perms as $perm) {
// if there is a component, check that it matches
if ( ($component != '') &&
(!preg_match("=^$perm[component]$=", $component)) ) {
// component exists, and doestn't match.
// Confirm that instance matches
if (!preg_match("=^$perm[instance]$=", $instance)) {
// instance does not match
// We have a match - set the level and quit looking
function _fixsecuritystring($string)
if (strpos($string, ':') === 0) {
$string = '.*' . $string;
$string = $string . '.*';
* sign data object leaving data clearly visible
* @param unknown_type $data
* @return serialized string of signed data
$signature = sha1($unsignedData. $key);
$signedData = serialize(array($unsignedData, $signature));
* verify signed data object
* @param string of serialized $data
* @return mixed array or string of data if true or bool false if false
$signature = sha1($signedData[0]. $key);
if ($signature != $signedData[1]) {
|