Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $commands = array("DELETE FROM users WHERE (user_id = 5)" ,
- "UPDATE users SET (age = 10) WHERE (user_id = 1)",
- "DELETE FROM users WHERE (id = 5)" ,
- "UDATE users SET (age = 10) WHERE (user_id = 1)" );
- //$commands = $_GET['commands'];
- $usersDatabase = array();
- $db = "";
- $lastId = 0;
- $errors = 0;
- $insert = "/^(.*?)\s*INTO\s*(.*?)\s*\((.*?)\)\s*VALUES\s*\((.*?)\)$/";
- $update = "/^(\w+)\s*(.*?)\s*SET\s*\((.*?)\s*=\s*(.*?)\)\s*WHERE\s*\((.*?)\s*=\s*(.*?)\)$/";
- $delete = "/^(.*?)\s*FROM\s*(.*?)\s*WHERE\s*\((.*?)\s*=\s*(.*?)\)$/";
- //model of a user
- $user = array(
- "user_id",
- "login",
- "gender",
- "age"
- );
- function insert($insertionData) {
- $keys = preg_split("/,\s*/", $insertionData[3]);
- $values = preg_split("/,\s*/", $insertionData[4]);
- $user = array(
- "user_id" => $GLOBALS['lastId'],
- "login" => "",
- "gender" => "undefined",
- "age" => "undefined"
- );
- for($i = 0; $i < count($keys) ;$i++) {
- if($keys[$i] == "user_id") {
- if($GLOBALS['lastId'] < $values[$i]) {
- $GLOBALS['lastId'] = $values[$i];
- } else {
- $values[$i] = $GLOBALS['lastId'];
- }
- }
- $user[$keys[$i]] = $values[$i];
- }
- $GLOBALS['usersDatabase'][] = $user;
- $GLOBALS['lastId']++;
- }
- function update($updates) {
- $valueToUpdate = $updates[3];
- $value = $updates[4];
- $conditionId = $updates[5];
- $conditionValue = $updates[6];
- $found = false;
- foreach ($GLOBALS['usersDatabase'] as &$user) {
- if($user[$conditionId] == $conditionValue) {
- $user[$valueToUpdate] = $value;
- $found = true;
- }
- }
- if(!$found) {
- $GLOBALS['errors']++;
- }
- }
- function delete($deletions) {
- $key = $deletions[3];
- $value = $deletions[4];
- foreach ($GLOBALS['usersDatabase'] as $k => $v) {
- if($GLOBALS['usersDatabase'][$k][$key] == $value) {
- unset($GLOBALS['usersDatabase'][$k]);
- }
- }
- }
- //game loop
- for($i = 0; $i < count($commands) ;$i++) {
- $currCommand = $commands[$i];
- preg_match($insert, $currCommand, $insertions);
- preg_match($update, $currCommand, $updates);
- preg_match($delete, $currCommand, $deletions);
- if($insertions) {
- $keys = preg_split("/,\s*/", $insertions[3]);
- $diff = array_diff($keys, $user);
- $values = preg_split("/,\s*/", $insertions[4]);
- if($insertions[1] != "INSERT" || count($keys) != count($values) ||
- !in_array("login", preg_split("/,\s*/", $insertions[3])) ||
- count($diff) != 0) {
- $errors++;
- continue;
- } else {
- if($insertions[2] != "") {
- if($db == "") {
- $db = $insertions[2];
- }
- if($insertions[2] == $db) {
- insert($insertions);
- }
- }
- }
- } elseif($updates) {
- if( !in_array($updates[3], $user) ||
- !in_array($updates[5], $user) ||
- $updates[1] != "UPDATE" ||
- $updates[6] == "" ||
- $updates[3] == 'user_id' ||
- $db != $updates[2] ||
- $db == "") {
- $errors++;
- continue;
- } else {
- //perform update
- update($updates);
- }
- } elseif($deletions) {
- if($deletions[1] != "DELETE" || $deletions[3] == "login" || !in_array($deletions[3], $user)) {
- $errors++;
- continue;
- } else {
- if(count($usersDatabase) == 0 || $deletions[2] != $db) {
- continue;
- }
- //perform deletion
- delete($deletions);
- }
- } else {
- $errors++;
- }
- }
- if(count($usersDatabase) == 0) {
- echo "You have " . $errors . " error/s";
- } else {
- $table = "<table><thead><tr><th>user_id</th><th>login</th><th>gender</th><th>age</th></tr></thead><tbody>";
- foreach ($usersDatabase as $user) {
- $table .= "<tr><td>" . htmlspecialchars($user['user_id']) . "</td><td>" .
- htmlspecialchars($user['login']) . "</td><td>" .
- htmlspecialchars($user['gender']) . "</td><td>" .
- htmlspecialchars($user['age']) . "</td></tr>";
- }
- $table .= '</tbody><tfoot><tr><td colspan="4">Errors=' . $errors .'</td></tr></tfoot></table>';
- echo $table;
- }
- //var_dump($usersDatabase);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement