$aErrors = array();
$aAlerts = array();
global $DB;
if (isset($_POST['add_capab'])) {
    foreach ($_POST as $k => $v) {
        $_POST[$k] = get_magic_quotes_gpc() ? trim($v) : trim(addslashes($v));
    }
    $Check = new cCheckForm();
    $namePat = '/^[a-zA-Z0-9_-]{1,50}$/';
    if ($Check->check('name', 'preg_match("' . $namePat . '",$test)', 'The NAME must be without whitespaces and diacritical marks and max. 50 symbols!')) {
        $Check->check('name', '$test==false', 'This capability already exists!', admin_capabExists($_POST['name']));
    }
    $Check->check('description', 'strlen($test) > 0 && strlen($test) < 266', 'The description of capability is required with max length 255 symbols!');
    $Logs->addLog($Check->isValid(), 'add new one valid');
    if (!$Check->isValid()) {
        foreach ($Check->getErrors() as $k => $error) {
            $aErrors[] = admin_getErrorToPrint($k, $error);
        }
    } else {
        try {
            $aVals = array();
            $aVals[] = array('name', $_POST['name']);
            $aVals[] = array('description', $_POST['description']);
            /// insert values ///
            if (!$DB->insert('core_capabilities', $aVals)) {
                throw new cException("Some error during insert operation!");
            }
            $aAlerts[] = "New capability waw added.";
        } catch (cException $e) {
            $msg = $e->getDbMessageError(__METHOD__ . '(line:' . __LINE__ . ')', $query);
            $aErrors[] = $msg;
예제 #2
0
function _updateCapabilities()
{
    global $DB;
    global $_aErrors;
    global $_aAlerts;
    $Check = new cCheckForm();
    $colsToDb = array(array('capability', false), array('role', false));
    $dataToDb = array();
    foreach ($_POST as $k => $v) {
        $_POST[$k] = get_magic_quotes_gpc() ? trim($v) : trim(addslashes($v));
        $aDat = explode('_', $k, 3);
        /// post data with values for core_role_capability ///
        if ($aDat[0] == "caprole" && count($aDat) == 3) {
            $Check->check('cap', 'is_numeric($test)', 'The id of capability is in wrong type', $aDat[1]);
            $Check->check('role', 'is_numeric($test)', 'The id of role is in wrong type', $aDat[2]);
            $dataToDb[] = array($aDat[1], $aDat[2]);
        }
    }
    //$Logs->addLog($Check->isValid(), 'valid');
    try {
        if (!$Check->isValid()) {
            foreach ($Check->getErrors() as $k => $error) {
                $_aErrors[] = implode("(<strong>{$k}</strong>)<br />", $error['msg']) . "(<strong>{$k}</strong>)";
            }
            throw new cException("Form is not valid!");
        }
        /// make backup of original table ///
        if (!$DB->createCopyOfTable('core_role_capability', 'core_role_capability_back')) {
            throw new cException("Some error during backup operation of old data!");
        }
        /// empty original table ///
        if (!$DB->truncateTable('core_role_capability')) {
            /// drop backup table ///
            $DB->dropTable('core_role_capability_back');
            throw new cException("Some error during insert operation!");
        }
        /// insert new values to original table ///
        if (!$DB->insertMore('core_role_capability', $colsToDb, $dataToDb)) {
            /// copy data from backup to original table ///
            $DB->createCopyOfTable('core_role_capability_bak', 'core_role_capability');
            throw new cException("Some error during insert operation!");
        }
        /// empty backup table ///
        $DB->dropTable('core_role_capability_back');
        $_aAlerts[] = "Capabilities were updated.";
    } catch (cException $e) {
        $msg = $e->getDbMessageError(__METHOD__ . '(line:' . __LINE__ . ')', $query);
        $_aErrors[] = $msg;
        cLogsDb::addFileLog($msg);
    }
}