<?php
require_once('functions.php');
session_start();
// ログインしているユーザーから以外のアクセスは拒否
if (!isset($_SESSION['user_id'])){
echo '不正なリクエスト';
exit;
}
$db_path = GetDbPath();
$db = new PDO("sqlite:{$db_path}");
$prof_text = GetProfileText($db, $_SESSION['user_id']);
$prof_image_path = GetProfileImagePath($db, $_SESSION['user_id']);
$user_name = GetUserNameFromUserID($db, $_SESSION['user_id']);
// [プロフィール画像をクリアする] ボタンがクリックされた
if (isset($_POST['image-clear'])){
UpdateProfile($db, $user_name, $_POST['text'], 'clear');
header("Location: ./?user=".$_SESSION['user_id']);
$db = null;
exit;
}
// [プロフィールを変更する] ボタンがクリックされた
if (isset($_POST['text']) && isset($_POST['user-name'])){
// 元ファイル名の先頭にアップロード日時を加える
// 事前にimagesディレクトリを作成しておかないとエラーになるので注意
$image_path = './images/'. date("Y-md-His")."-".$_FILES['file_upload']['name'];
$prof_image_path = '';
if(move_uploaded_file($_FILES['file_upload']['tmp_name'], $image_path)) {
// PHPエラーを非表示
error_reporting(0);
if (imagecreatefromstring(file_get_contents($image_path)) !== false)
$prof_image_path = $image_path; // アップロードされたファイルが画像であるならプロフィール画像を変更
else
unlink($image_path); // 画像でないなら削除
}
UpdateProfile($db, $_POST['user-name'], $_POST['text'], $prof_image_path);
header("Location: ./?user=".$_SESSION['user_id']);
$db = null;
exit;
}
$db = null;
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>プロフィールの編集|ぴよったー</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="./style.css" type="text/css" media="all" />
</head>
<body>
<?php ShowHeader(); ?>
<div id = "main">
<div>プロフィールの編集</div>
<form action="" method="post" enctype="multipart/form-data" style = "margin-bottom: 10px">
<input type="text" name = "user-name" required value="<?php echo $user_name; ?>">
<div id = "textarea"><textarea name="text" cols="50" rows="5" maxlength ="250" required><?php echo $prof_text; ?></textarea></div>
<input type="submit" value="プロフィールを変更する">
<span class = "ms-3"><input name="file_upload" type="file" id = "file-name"></span>
</form>
<form action="" method="post" style = "margin-bottom: 20px">
<input type="text" name="image-clear" style = "display:none">
<input type="submit" value="プロフィール画像をクリアする">
</form>
<p><img src="" id="file-image" /></p>
<p id="image-check"></p>
<?php if($prof_image_path != '') ?>
<p><img src="<?php echo $prof_image_path; ?>" id="file-image" /></p>
</div>
<script src = "./app.js"></script>
</body>
</html>