Example #1
0
function delete_resource_instance($R)
{
    extract($R);
    $resource_id = $R['resource_id'];
    $modified_by = $R['user_id'];
    $db = $R['db'];
    $resource_info = s3info('instance', $resource_id, $db);
    $resource_class_id = $resource_info['resource_class_id'];
    $project_id = $resource_info['project_id'];
    $old_rule_id = get_rule_id_by_entity_id($resource_class_id, $project_id, $db);
    #Find all statements on this resource_id
    if (!is_array($statements)) {
        $statements = CORElist(array('child' => 'statement', 'parent_ids' => array('instance_id' => $resource_id), 'user_id' => $user_id, 'db' => $db));
    }
    #Find all statements where this resource_id is object. First we have to figure out which rules point to the object where this instance might have been inserted.
    #$class_id = $resource_info['resource_class_id'];
    $s3ql = compact('user_id', 'db');
    $s3ql['select'] = '*';
    $s3ql['from'] = 'statements';
    $s3ql['where']['value'] = $resource_id;
    $s3ql['where']['object_id'] = $resource_class_id;
    #$borrowedStats = S3QLaction($s3ql); #COMING SOON
    if (is_array($statements)) {
        foreach ($statements as $i => $statement_info) {
            $s3ql = compact('user_id', 'db');
            $s3ql['delete'] = 'statement';
            $s3ql['where']['statement_id'] = $statement_info['statement_id'];
            $s3ql['where']['confirm'] = 'yes';
            $done = S3QLaction($s3ql);
            #$sql = "delete from s3db_statement where statement_id = '".$statement_info['statement_id']."'";
            #$db->query($sql, __LINE__, __FILE__);
            $S = array('statement_id' => $statement_info['statement_id'], 'old_rule_id' => $statement_info['rule_id'], 'old_resource_id' => $statement_info['resource_id'], 'old_project_id' => $statement_info['project_id'], 'old_value' => $statement_info['value'], 'old_notes' => $statement_info['notes'], 'created_by' => $statement_info['created_by'], 'created_on' => $statement_info['created_on'], 'modified_by' => $user_id, 'action' => 'delete', 'db' => $db);
            $logged = insert_statement_log($S);
        }
    }
    #Change the resources table
    #$sql = "delete from s3db_resource where resource_id = '".$resource_id."' and iid='1'";
    $sql = "update s3db_resource set status = 'I', modified_on = now() where resource_id = '" . $resource_id . "' and iid='1'";
    #echo '<pre>';print_r($statements);
    #echo $sql;exit;
    $db->query($sql, __LINE__, __FILE__);
    $dbdata = get_object_vars($db);
    if ($dbdata['Errno'] == 0) {
        $S = array('statement_id' => '0', 'old_rule_id' => $old_rule_id, 'old_resource_id' => $resource_id, 'old_project_id' => $resource_info['project_id'], 'old_value' => $resource_info['entity'], 'old_notes' => $resource_info['notes'], 'created_by' => $resource_info['created_by'], 'created_on' => $resource_info['created_on'], 'modified_by' => $modified_by, 'action' => 'delete', 'db' => $db);
        $logged = insert_statement_log($S);
        #Change in the statements table
        #$sql = "delete from s3db_statement where resource_id = '".$resource_id."'";
        #$db->query($sql, __LINE__, __FILE__);
        $dbdata = get_object_vars($db);
        ##This piece of code is meant to enable the regeneration of the list of resource instances in query result
        $queryresult = $_SESSION['query_result'];
        #echo '<pre>'; print_r($queryresult);
        if (is_array($queryresult)) {
            function resource_id_compare($a, $b)
            {
                if ($a == $b) {
                    return 0;
                }
            }
            foreach ($queryresult as $i => $value) {
                if ($queryresult[$i]['resource_id'] == $resource_id) {
                    unset($queryresult[$i]);
                }
            }
            #if (is_array($deleteme)) #Remove the small array from the big array
            #	$queryresult = array_diff_uassoc($queryresult,$deleteme, "resource_id_compare");
            #echo '<pre>'; print_r($queryresult);
        }
        $_SESSION['query_result'] = $queryresult;
        return True;
    } else {
        return False;
    }
}
Example #2
0
function update_resource_instance($R)
{
    extract($R);
    #echo '<pre>';print_r($R);exit;
    if (is_array($editing_resource)) {
        extract($editing_resource);
    }
    $modified_by = $user_id;
    #$sql ="update s3db_resource set notes='".$notes."', entity='".$entity."', modified_by='".$modified_by."', modified_on=now(), resource_class_id='".$resource_class_id."' where resource_id='".$resource_id."'";
    $sql = "update s3db_resource set notes='" . addslashes($inputs['notes']) . "', modified_by='" . $modified_by . "', modified_on=now() where resource_id='" . $info['resource_id'] . "'";
    #$echo $sql;exit;
    $db->query($sql, __LINE__, __FILE__);
    $dbdata = get_object_vars($db);
    $old_rule_id = get_rule_id_by_entity_id($info['class_id'], $project_id, $db);
    $instance_info = $info;
    $Z = compact('instance_info', 'user_id', 'db');
    #$L = array('statement_id'=>fastStatementId($Z), 'old_rule_id'=>$old_rule_id, 'old_resource_id'=>$info['resource_id'], 'old_project_id'=>$info['project_id'], 'old_value'=>$info['entity'], 'old_notes'=>$oldvalues['notes'], 'action'=>'modify', 'modified_by'=>$modified_by, 'created_on'=>$info['created_on'], 'created_by'=>$info['created_by'], 'db'=>$db);
    #echo '<pre>';print_r($L);exit;
    if ($dbdata['Errno'] == 0) {
        #echo '<pre>';print_r($olvalues);exit;
        $statement_info = $info;
        $statement_info['statement_id'] = fastStatementId($Z);
        $statement_info['rule_id'] = get_rule_id_by_entity_id($info['class_id'], $project_id, $db);
        $statement_info['value'] = $info['resource_id'];
        if ($input['notes'] != '') {
            $statement_info['notes'] = $info['notes'];
        }
        $action = 'edit';
        if ($inputs['notes'] != $oldvalues['notes']) {
            $logged = insert_statement_log(compact('oldvalues', 'inputs', 'action', 'statement_info', 'user_id', 'db'));
        }
        #nor for the rules. Which rule use this item as verb_id
        $sql = "select * from s3db_rule where verb_id = '" . $info['resource_id'] . "'";
        #echo $sql;
        $db->query($sql, __LINE__, __FILE__);
        while ($db->next_record()) {
            $rules[] = array('rule_id' => $db->f('rule_id'), 'project_id' => $db->f('project_id'), 'subject' => $db->f('subject'), 'verb' => $db->f('verb'), 'object' => $db->f('object'), 'subject_id' => $db->f('subject_id'), 'verb_id' => $db->f('verb_id'), 'object_id' => $db->f('object_id'), 'notes' => $db->f('notes'), 'created_on' => substr($db->f('created_on'), 0, 19), 'created_by' => $db->f('created_by'), 'modified_on' => $db->f('modified_on'), 'modified_by' => $db->f('modified_by'), 'permission' => $db->f('permission'));
        }
        $inputs = array('verb' => $inputs['notes']);
        if (is_array($rules)) {
            foreach ($rules as $rule_info) {
                $oldvalues['verb'] = $rule_info['verb'];
                $sql = "update s3db_rule set verb = '" . $inputs['verb'] . "' where rule_id = '" . $rule_info['rule_id'] . "'";
                #echo $sql.'<BR>';
                $db->query($sql, __LINE__, __FILE__);
                $action = 'edit';
                insert_rule_log(compact('oldvalues', 'inputs', 'rule_info', 'user_id', 'db', 'action'));
            }
        }
        #echo $sql.'<BR>';
        ##This piece of code is meant to enable the regeneration of the list of resource instances in query result
        $queryresult = $_SESSION['query_result'];
        if (is_array($queryresult)) {
            foreach ($queryresult as $i => $value) {
                if ($queryresult[$i]['resource_id'] == $editing_resource['resource_id']) {
                    $queryresult[$i]['notes'] = $editing_resource['notes'];
                }
            }
        }
        $_SESSION['query_result'] = $queryresult;
        return True;
    } else {
        return False;
    }
}
Example #3
0
function insert_s3db($D)
{
    #insert_s3db(compact('element', 'inputs', 'user_id', 'db'));
    #this is meant to be a general function for every insert, froum user to group. It create the entry, based on information on array $info and adds an entry on permissions
    #There will be 2 special cases: creating a class also creates the rule "has UID" and creating an instance also creates the statament where reosurce_id is instance_id and rule is "hasUID"
    extract($D);
    $table = $GLOBALS['s3tables'][$element];
    #echo '<pre>';print_r($D);
    $cols_for_entry = $GLOBALS['dbstruct'][$element];
    $letter = strtoupper(substr($element, 0, 1));
    #some special restrictions apply
    switch ($letter) {
        case 'U':
            $cols_for_entry = array_diff($cols_for_entry, array('addr1', 'addr2', 'city', 'state', 'postal_code', 'country'));
            array_push($cols_for_entry, 'account_pwd');
            $inputs['account_addr_id'] = insert_address($D);
            break;
        case 'G':
            $cols_for_entry = array_merge($cols_for_entry, array('account_pwd', 'account_group'));
            $inputs['account_type'] = 'g';
            $inputs['account_group'] = $inputs['account_type'];
            $inputs['account_uname'] = $inputs['account_lid'];
            break;
        case 'C':
            $inputs['iid'] = '0';
            break;
        case 'I':
            $inputs['iid'] = '1';
            $inputs['resource_class_id'] = $inputs['resource_class_id'] == '' ? $inputs['class_id'] : $inputs['resource_class_id'];
            $inputs['resource_id'] = $inputs['resource_id'] != '' ? $inputs['resource_id'] : $inputs['instance_id'];
            break;
        case 'F':
            $element = 'statement';
            $cols_for_entry = $GLOBALS['dbstruct']['statements'];
            $table = $GLOBALS['s3tables']['statements'];
            $inputs['statement_id'] = s3id();
            #now need to move file from tmp folder into final folder
            $moved = tmpfile2folder(array('inputs' => $inputs, 'db' => $db, 'user_id' => $user_id));
            if (!$moved[0]) {
                return $moved[1];
            } else {
                $inputs = $moved[1];
            }
            break;
    }
    #remove ''_id from cols for entry if that field is empty;
    if ($inputs[$GLOBALS['s3ids'][$element]] == '') {
        #never levae the primary key input empty
        #$inputs[$GLOBALS['s3ids'][$element]] = find_latest_UID($table, $db)+1;
        $inputs[$GLOBALS['s3ids'][$element]] = s3id();
    }
    $sql = buildInsertString($cols_for_entry, $inputs, $table);
    #echo $sql;exit;
    $db->query($sql, __LINE__, __FILE__);
    if ($db->Errno == 1) {
        $inputs[$GLOBALS['s3ids'][$element]] = s3id();
        $sql = buildInsertString($cols_for_entry, $inputs, $table);
        $db->query($sql, __LINE__, __FILE__);
    }
    $dbdata = get_object_vars($db);
    #$dbdata['Errno']='0';
    #echo '<pre>';print_r($dbdata);exit;
    if ($dbdata['Errno'] != '0') {
        if ($table == 'account') {
            $sql = "update s3db_" . $table . " set account_status = 'A' where account_id = '" . $inputs['account_id'] . "'";
            $db->query($sql, __LINE__, __FILE__);
            $dbdata = get_object_vars($db);
        }
        if ($dbdata['Errno'] != 0) {
            return array(False, $GLOBALS['error_codes']['something_went_wrong'] . '<message>' . str_replace('key', $GLOBALS['COREids'][$element], $dbdata['Error']) . '</message>', $GLOBALS['error_codes']['something_went_wrong'], $dbdata['Error']);
        }
    } else {
        #$element_id = $db->get_last_insert_id($table, $GLOBALS['s3ids'][$element]);
        #$element_id = find_latest_UID($table, $db);
        $element_id = $inputs[$GLOBALS['s3ids'][$element]];
        $info[$letter . $element_id] = $inputs;
        #special restrictions apply after create:
        switch ($letter) {
            case 'P':
                $project_id = $element_id;
                #if project_id is remote, need to change it's name a bit because / and # are not allowed in project_name;
                #$project_id = urlencode($project_id);
                #create the folder on the extras for the files of this project
                $folder_code_name = random_string(15) . '.project' . urlencode($project_id);
                $maindir = $GLOBALS['s3db_info']['server']['db']['uploads_folder'] . $GLOBALS['s3db_info']['server']['db']['uploads_file'];
                $destinationfolder = $maindir . '/' . $folder_code_name;
                #create the folder for the project
                if (mkdir($destinationfolder, 0777)) {
                    $indexfile = $destinationfolder . '/index.php';
                    if (file_exists($destinationfolder)) {
                        file_put_contents($indexfile, 'This folder cannot be accessed');
                        chmod($indexfile, 0777);
                    }
                    $sql = "update s3db_project set project_folder = '" . $folder_code_name . "' where project_id = '" . $project_id . "'";
                    $db->query($sql, __LINE__, __FILE__);
                } else {
                    echo "Could not create directory for this project. You might not be able to upload files to this project.";
                }
                break;
            case 'I':
                $class_id = $inputs['resource_class_id'];
                $statement_info = $inputs;
                $statement_info['rule_id'] = fastRuleID4class(compact('class_id', 'db', 'user_id'));
                $statement_info['value'] = $element_id;
                $statement_info['resource_id'] = $element_id;
                #$stat_inserted = insert_s3db(array('element'=>'statement', 'inputs'=>$statement_info, 'db'=>$db, 'user_id'=>$user_id));
                #echo '<pre>';print_r($statement_info);exit;
                $stat_inserted = insert_statement(compact('statement_info', 'db', 'user_id'));
                $action = 'create';
                insert_statement_log(compact('oldvalues', 'inputs', 'action', 'statement_info', 'user_id', 'db'));
                #echo '<pre>';print_r($stat_inserted);
                if ($stat_inserted[0]) {
                    ereg('<statement_id>([0-9]+)</statement_id>', $stat_inserted[1], $s3qlout);
                    $statement_info['statement_id'] = $stat_inserted[1];
                }
                $info['S' . $statement_info['statement_id']] = $statement_info;
                break;
            case 'C':
                $rule_info = $inputs;
                $rule_info['subject'] = $inputs['entity'];
                $rule_info['subject_id'] = $element_id;
                $rule_info['verb_id'] = '0';
                $rule_info['verb'] = 'has UID';
                $rule_info['object'] = 'UID';
                #echo '<pre>';print_r($inputs);
                #echo '<pre>';print_r($rule_info);exit;
                $rule_inserted = insert_rule(compact('rule_info', 'db', 'user_id'));
                break;
            case 'R':
                $rule_info = $inputs;
                $rule_info['rule_id'] = $element_id;
                #echo '<pre>';print_r($rule_info);exit;
                $action = 'create';
                $rule_inserted = insert_rule_log(compact('rule_info', 'action', 'db', 'user_id'));
                break;
            case 'S':
                $statement_info = $inputs;
                $action = 'create';
                insert_statement_log(compact('oldvalues', 'action', 'statement_info', 'user_id', 'db'));
            case 'F':
                $statement_info = $inputs;
                $action = 'create';
                insert_statement_log(compact('oldvalues', 'action', 'statement_info', 'user_id', 'db'));
        }
        #now add an entry that specifies user "creator' with permission level on 222 this entry (because someone has to have it)
        #some resources need to be mirrored, or swapped:
        if (ereg('^(U|G)$', $letter)) {
            #owner of groups is automatically created within it with PL 222
            if (ereg('^G$', $letter)) {
                $permission_info = array('uid' => 'U' . $user_id, 'shared_with' => strtoupper(substr($element, 0, 1)) . $element_id, 'permission_level' => '222');
                #echo '<pre>';print_r($permission_info);
                insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
            } elseif (ereg('^U$', $letter)) {
                ##also, for each user insertions, create an item_id for this user in the userManagement project. This will only create it if it does not yet exist
                include_once S3DB_SERVER_ROOT . '/s3dbcore/authentication.php';
                $user_proj = create_authentication_proj($db, $user_id);
                #now, create an item in the userManagement project for this user
                $user2add = $element_id;
                $c = compact('user2add', 'user_proj', 'user_id', 'db');
                $user_proj = insert_authentication_tuple($c);
                if ($inputs['permission_level'] != "") {
                    ##creator has specified that his own permissions can propagate
                    $permission_info = array('uid' => 'U' . $user_id, 'shared_with' => 'U' . $user2add, 'permission_level' => $inputs['permission_level']);
                    insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
                }
            }
            #and then insert them i deployment
            $permission_info = array('uid' => 'D' . $GLOBALS['Did'], 'shared_with' => strtoupper(substr($element, 0, 1)) . $element_id);
            $permission_info['permission_level'] = $inputs['permission_level'] != '' ? $inputs['permission_level'] : '200';
        } else {
            if (ereg('^P$', $letter)) {
                #project has a special treatment, creators of project get to have permission level 222 on it.
                $permission_info['shared_with'] = 'U' . $user_id;
                $permission_info['shared_with'] = 'U' . $user_id;
                $permission_info['uid'] = $letter . $element_id;
                $permission_info['permission_level'] = 'YYY';
                ##This assures that it will migrate to child resources
                insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
            } elseif (ereg('^R$', $letter)) {
                #Rule require permission to be inserted also for subject_id, verb_id and, if exists, object-id
                ##For SUBJECT
                $permission_info = array('uid' => 'R' . $rule_info['rule_id'], 'shared_with' => 'C' . $rule_info['subject_id'], 'permission_level' => '222', 'info' => $info);
                #insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
                ##For VERB
                $permission_info = array('uid' => 'R' . $rule_info['rule_id'], 'shared_with' => 'I' . $rule_info['verb_id'], 'permission_level' => '222', 'info' => $info);
                #insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
                #FOR OBJECT
                if ($rule_info['object_id']) {
                    $permission_info = array('uid' => 'R' . $rule_info['rule_id'], 'shared_with' => 'C' . $rule_info['object_id'], 'permission_level' => '222', 'info' => $info);
                    #insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
                }
                $permission_info['shared_with'] = 'P' . $inputs['project_id'];
            } elseif (ereg('^C$', $letter)) {
                $permission_info['shared_with'] = 'P' . $inputs['project_id'];
            } elseif (ereg('^I$', $letter)) {
                #insert for statement too
                $permission_info = array('uid' => 'S' . $statement_info['statement_id'], 'shared_with' => 'R' . $statement_info['rule_id'], 'permission_level' => '222', 'info' => $info);
                #insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
                #and then for instance
                $permission_info['shared_with'] = 'C' . $inputs['resource_class_id'];
            } elseif (ereg('^S|F$', $letter)) {
                if ($letter == 'F') {
                    $element_id = $statement_info['statement_id'];
                    $element = 'file';
                    $letter = 'S';
                }
                $permission_info = array('uid' => $letter . $statement_info['statement_id'], 'shared_with' => 'I' . $statement_info['resource_id'], 'permission_level' => '222', 'info' => $info);
                #insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
                ##If there is an object_id, insert one for that too
                if ($statement_info['object_id']) {
                    $permission_info = array('uid' => $letter . $statement_info['statement_id'], 'shared_with' => 'I' . $statement_info['object_id'], 'permission_level' => '222', 'info' => $info);
                    #insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
                }
                #And add one for the rule
                $permission_info['shared_with'] = 'R' . $inputs['rule_id'];
            }
            #and not these are global
            $permission_info['permission_level'] = $inputs['permission_level'] != '' ? $inputs['permission_level'] : '222';
            $permission_info['uid'] = $letter . $element_id;
            $info[$permission_info['uid']] = URI($permission_info['uid'], $user_id, $db);
        }
        #echo '<pre>';print_r($permission_info);
        #insert_permission(compact('permission_info', 'db', 'user_id', 'info'));
        return array(TRUE, $GLOBALS['error_codes']['success'] . "; " . $element . '_id' . ': <' . $element . '_id' . '>' . $element_id . '</' . $element . '_id' . '>' . '<a href =" ' . $query['url'] . '?key=' . $D['key'] . '&query=<S3QL><select>*</select><from>' . $GLOBALS['plurals'][$element] . '</from><where><' . $element . '_id>' . $element_id . '</' . $element . '_id></where></S3QL>">View ' . $element . '</a>', $element, $element . '_id' => $element_id, $GLOBALS['messages']['success'], strtoupper($element) . ' inserted');
    }
}