/** @param string[] $data */ public function SetData($data) { if (isset($data['baseIngredientID'])) { if ($data['baseIngredientID']) { $baseIngredient = $this->Session->IngredientByID($data['baseIngredientID']); if (!$baseIngredient) { APIResponse(RESPONSE_400, "Update Ingredient with bad base ingredient ID."); } else { $this->BaseIngredient = $baseIngredient; } } else { $this->BaseIngredient = false; } } if (isset($data['id'])) { $this->ID = (int) $data['id']; } if (isset($data['title'])) { $this->Title = $data['title']; } if (isset($data['type'])) { $this->Type = $data['type']; } if (isset($data['description'])) { $this->Description = $data['description']; } }
/** * @param string $type * @param string $message */ function APIResponse($type = RESPONSE_404, $message = null) { switch ($type) { case RESPONSE_200: case RESPONSE_404: case RESPONSE_401: case RESPONSE_400: case RESPONSE_500: header("Status: {$type}"); header('Cache-Control: max-age=30'); if (!is_null($message)) { if (!is_array($message)) { $message = array('message' => $message); } buildJSONResponse($message); } break; default: APIResponse(RESPONSE_500); break; } exit; }
default: break; } } $dbCredentials = 'DevDBCredentials'; if ($unitTest) { include 'application/UnitTest/Setup.php'; } session_name(SESSION_NAME); session_start(); if (!isset($_SESSION['session'])) { $_SESSION['session'] = new Session(new MySQLDatabase($dbCredentials)); } $Session =& $_SESSION['session']; $Session->Process($_SERVER, $Path, $PerfLog); session_write_close(); break; default: APIResponse(RESPONSE_400); break; } break; default: APIResponse(RESPONSE_400); break; } break; default: APIResponse(RESPONSE_400); break; }
/** * @param string[] $server * @param string $path * @param string[] $headers */ public function Process($server, $path, $headers) { $method = isset($server['REQUEST_METHOD']) ? $server['REQUEST_METHOD'] : false; if (empty($path)) { switch ($method) { case 'POST': break; case 'GET': APIResponse(RESPONSE_200, $this->ToArray()); break; case 'PUT': if (isset($_POST['type'])) { $this->Type = $_POST['type']; } if (isset($_POST['title'])) { $this->Title = $_POST['title']; } if (isset($_POST['description'])) { $this->Description = $_POST['description']; } $this->UpdateDatabase(); APIResponse(RESPONSE_200, $this->ToArray()); break; case 'DELETE': $query = $this->DB->Query("\n UPDATE tblBars\n SET active = 0\n WHERE userID = " . (int) $this->Session->ID . "\n AND id = " . (int) $this->ID . " LIMIT 1;\n "); if ($query) { $this->Session->RefreshBars(); APIResponse(RESPONSE_200); } else { APIResponse(RESPONSE_500); } break; default: break; } } }
/** @return bool */ public function LoggedIn() { if ($this->auth) { $headers = getallheaders(); if ($headers && isset($headers['Authorization']) && $headers['Authorization'] == $this->auth) { return (bool) $this->auth; } else { $this->auth = false; APIResponse(RESPONSE_401); exit; } } else { return false; } }
/* * All this crap switches the environment to run from the testing database and session pool. * Pass in /reset, and the database and sessions will be cleared, and a default user will be created. * * Magic! */ $dbCredentials = 'UnitTestDBCredentials'; if (!file_exists(UNITTESTSESSIONPATH)) { mkdir(UNITTESTSESSIONPATH); } session_save_path(UNITTESTSESSIONPATH); if (isset($Path[0]) && $Path[0] == 'reset') { $rm = "rm " . UNITTESTSESSIONPATH . "*"; $rm = `{$rm}`; $db = new MySQLDatabase('UnitTestDBCredentials'); $tables = $db->Query("\n\t\tSELECT table_name AS `table`\n\t\tFROM information_schema.tables\n\t\tWHERE table_schema = " . $db->Quote($UnitTestDBCredentials['name']) . ";\n\t"); singleLog($tables); if ($tables) { $db->Query("SET FOREIGN_KEY_CHECKS = 0;"); while ($table = $tables->Fetch()) { singleLog($db->Query("DROP TABLE IF EXISTS " . $table['table'] . ";")); } $db->Query("SET FOREIGN_KEY_CHECKS = 1;"); } $dump = "mysqldump -u " . $DevDBCredentials['user'] . " -p" . $DevDBCredentials['pass'] . " -d " . $DevDBCredentials['name'] . " | mysql -u " . $UnitTestDBCredentials['user'] . " -p" . $UnitTestDBCredentials['pass'] . " -D" . $UnitTestDBCredentials['name'] . ""; $dump = `{$dump}`; $makeUser = $db->Query("INSERT INTO tblUsers (username, password, accountType, displayName, email) VALUES ('joe', 'nohomohug', 'Standard', 'Joe Testmoore', '*****@*****.**');"); APIResponse(RESPONSE_200, "Cleared the database and sessions."); exit; }