'error', 'message' => $msg])); } $allowedTypes = [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF]; if ($_POST['image_upload_file_submit'] == 'submit') { header('Content-Type: application/json'); if (!crsf_check()) { throwError('expired'); } if (!isset($_SESSION['phrase']) || !PhraseBuilder::comparePhrases($_SESSION['phrase'], $_POST['captcha'])) { throwError("bad_captcha"); } if ($_FILES["image_upload_file"]["error"] > 0) { throwError($_FILES["image_upload_file"]["error"]); } if ($_FILES["image_upload_file"]["size"] > 5242880) { // 5 MB throwError('too_large'); } if ($used + $_FILES["image_upload_file"]["size"] > $limit) { throwError('storage_limit_exceeded'); } $size = getimagesize($_FILES['image_upload_file']['tmp_name']); if (!$size || !in_array($size[2], $allowedTypes)) { throwError('not_a_image'); } list($width, $height, $type) = $size; $hash = hash_file("sha256", $_FILES['image_upload_file']['tmp_name']); $watermark_text = UOJConfig::$data['profile']['oj-name-short']; if (isSuperUser($myUser) && $_POST['watermark'] == 'no_watermark') { $watermark_text = ""; $hash .= "__no_watermark"; } elseif ($_POST['watermark'] == 'site_shortname_and_username') { $watermark_text .= ' @'.Auth::id(); $hash .= "__id_".Auth::id(); } $existing_image = DB::selectFirst("SELECT * FROM users_images WHERE `hash` = '$hash'"); if ($existing_image) { die(json_encode(['status' => 'success', 'path' => $existing_image['path']])); } $img = imagecreatefromstring(file_get_contents($_FILES["image_upload_file"]["tmp_name"])); $white = imagecolorallocatealpha($img, 255, 255, 255, 30); $black = imagecolorallocatealpha($img, 50, 50, 50, 70); $scale = ceil($width / 750.0); imagettftext($img, strval($scale * 16), 0, ($scale * 16) + $scale, max(0, $height - ($scale * 16) + 5) + $scale, $black, UOJContext::documentRoot().'/fonts/roboto-mono/RobotoMono-Bold.ttf', $watermark_text); imagefilter($img, IMG_FILTER_GAUSSIAN_BLUR); imagettftext($img, strval($scale * 16), 0, ($scale * 16), max(0, $height - ($scale * 16) + 5), $white, UOJContext::documentRoot().'/fonts/roboto-mono/RobotoMono-Bold.ttf', $watermark_text); imagepng($img, $_FILES["image_upload_file"]["tmp_name"]); imagedestroy($img); if (filesize($_FILES["image_upload_file"]["tmp_name"]) > 5242880) { // 5 MB throwError('too_large'); } $filename = uojRandAvaiableFileName('/image_hosting/', 10, '.png'); if (!move_uploaded_file($_FILES["image_upload_file"]["tmp_name"], UOJContext::storagePath().$filename)) { throwError('unknown error'); } DB::insert("INSERT INTO users_images (`path`, uploader, width, height, upload_time, size, `hash`) VALUES ('$filename', '{$myUser['username']}', $width, $height, now(), {$_FILES["image_upload_file"]["size"]}, '$hash')"); die(json_encode(['status' => 'success', 'path' => $filename])); } elseif ($_POST['image_delete_submit'] == 'submit') { crsf_defend(); $id = $_POST['image_delete_id']; if (!validateUInt($id)) { becomeMsgPage('ID 不合法。返回'); } else { $result = DB::selectFirst("SELECT * from users_images WHERE id = $id"); if (!$result) { becomeMsgPage('图片不存在。返回'); } else { unlink(UOJContext::storagePath().$result['path']); DB::delete("DELETE FROM users_images WHERE id = $id"); header("Location: ". UOJContext::requestURI()); die(); } } } ?>