$config = array(); if (!@(include_once dirname(__FILE__) . '/include/loadutils.php')) { $err = 'Problem loading utility script'; } if (!$err && !load_utils('auth', 'service')) { $err = 'Problem loading utility scripts'; } if (!$err) { // pull in configuration so we can log other errors $config = config_get(); $err = config_error($config); $log_responses = $config['log_response']; } // autheticate the user (will exit if not possible) if (!$err && !auth_ok()) { auth_challenge($config); } if (!$err) { // pull in other configuration and check for required input if (!($php_input = file_get_contents('php://input'))) { $err = 'JSON payload required'; } else { if (!($request = @json_decode($php_input, TRUE))) { $err = 'Could not parse JSON payload'; } } } if (!$err) { if ($config['log_request']) { log_file(print_r($request, 1), $config); }
function h_delete_homework() { global $uri, $user; if (!$user) { auth_challenge(); } $p = HOMEWORK_DIR; $fn = homework_get_uri_fn($uri); if ($hw = homework_load($fn)) { if (!(isset($hw->author) && $hw->author == $user->un)) { header('HTTP/1.1 403 Forbidden'); header('Content-Type: text/plain'); print "You don't have permission to delete that.\n"; return; } if (req_header('if-match')) { if (req_header('if-match') !== etag_file($p)) { header('HTTP/1.1 412 Precondition Failed'); header('Content-Type: text/plain'); print "Entity tag mismatch.\n"; return; } } $del = @unlink($p . $fn); if (!$del) { header('HTTP/1.1 500 Internal Server Error'); header('Content-Type: text/plain'); print "Could not delete.\n"; } header('HTTP/1.1 200 OK'); header('Content-Type: text/plain'); print "Deleted.\n"; } else { header('HTTP/1.1 404 Not Found'); header('Content-Type: text/plain'); print "File not found.\n"; } }