1+ <?php
2+ require_once "pdo.php " ;
3+ require_once "head.php " ;
4+ date_default_timezone_set ('Asia/Taipei ' );
5+
6+ if (!isset ($ _SESSION ["email " ])) {
7+ echo "<p align='center'>PLEASE LOGIN</p> " ;
8+ echo "<br /> " ;
9+ echo "<p align='center'>Redirecting in 3 seconds</p> " ;
10+ header ("refresh:3;url=login.php " );
11+ die ();
12+ }
13+ if ($ _SESSION ['email ' ] == 'guest@guest.com ' ) {
14+ echo "<p align='center'>LOGGED IN AS GUEST ACCOUNT</p> " ;
15+ echo "<p align='center'>EDIT ACCOUNT DETAILS NOT ALLOWED</p> " ;
16+ echo "<br /> " ;
17+ echo "<p align='center'>Redirecting in 3 seconds</p> " ;
18+ header ("refresh:3;url=index.php " );
19+ die ();
20+ }
21+
22+ if (isset ($ _SESSION ["email " ])) {
23+ $ statement = $ pdo ->prepare ("SELECT * FROM account where user_Id = :usr " );
24+ $ statement ->execute (array (':usr ' => $ _SESSION ['user_id ' ]));
25+ $ response = $ statement ->fetch ();
26+ $ pfpsrc_default = './img/default-pfp.png ' ;
27+
28+ if ($ response ['pfp ' ] != null ) {
29+ $ userpfp = $ response ['pfp ' ];
30+ } else {
31+ $ userpfp = $ pfpsrc_default ;
32+ }
33+ }
34+
35+ if (isset ($ _POST ["submit " ])) {
36+ if (!file_exists ($ _FILES ['fileToUpload ' ]['tmp_name ' ]) || !is_uploaded_file ($ _FILES ['fileToUpload ' ]['tmp_name ' ])) {
37+ $ stmta = $ pdo ->prepare ("SELECT pfp FROM account WHERE name=? " );
38+ $ stmta ->execute ([$ _SESSION ['name ' ]]);
39+ $ pfptemp = $ stmta ->fetchAll (PDO ::FETCH_ASSOC );
40+
41+ foreach ($ pfptemp as $ test ) {
42+ if ($ test ['pfp ' ] != null ) {
43+ $ base64 = $ test ['pfp ' ];
44+ }
45+ }
46+ } else {
47+ $ target_dir = "uploads/ " ;
48+ $ target_file = $ target_dir . basename ($ _FILES ["fileToUpload " ]["name " ]);
49+ $ uploadOk = 1 ;
50+ $ imageFileType = strtolower (pathinfo ($ target_file , PATHINFO_EXTENSION ));
51+ $ check = getimagesize ($ _FILES ["fileToUpload " ]["tmp_name " ]);
52+ $ uploadOk = 1 ;
53+ $ path = $ _FILES ["fileToUpload " ]["tmp_name " ];
54+ $ type = pathinfo ($ path , PATHINFO_EXTENSION );
55+ $ data = file_get_contents ($ path );
56+ $ base64 = 'data:image/ ' . $ type . ';base64, ' . base64_encode ($ data );
57+ }
58+ if ($ check !== false ) {
59+ $ statement = $ pdo ->prepare ("SELECT user_id FROM account where email = :em " );
60+ $ statement ->execute (array (':em ' => $ _POST ['email ' ]));
61+ $ checkEmail = $ statement ->fetch ();
62+
63+ if ($ checkEmail ['user_id ' ] == $ _SESSION ['user_id ' ] || $ checkEmail ['user_id ' ] == "" ) {
64+ $ emailCheck = true ;
65+ } else {
66+ $ emailCheck = false ;
67+ }
68+
69+ if ($ emailCheck != false ) {
70+ if (isset ($ _POST ['password ' ])) {
71+ $ salt = getenv ('SALT ' );
72+ $ newPassword = $ _POST ['password ' ];
73+ $ hash = hash ("md5 " , $ salt . $ newPassword );
74+ }
75+ if ($ _POST ["show_email " ] == "on " ) {
76+ $ show_email = "True " ;
77+ } else {
78+ $ show_email = "False " ;
79+ }
80+
81+ $ sql = "UPDATE account SET pfp = :pfp,
82+ name = :newName,
83+ email = :email,
84+ password = :password,
85+ about = :about,
86+ show_email = :showEmail
87+ WHERE user_id = :usrid " ;
88+ $ stmt = $ pdo ->prepare ($ sql );
89+ $ stmt ->execute (array (
90+ ':pfp ' => $ base64 ,
91+ ':usrid ' => $ _SESSION ['user_id ' ],
92+ ':newName ' => str_replace ('< ' , ' ¯\_(ツ)_/¯ ' , $ _POST ['name ' ]),
93+ ':email ' => str_replace ('< ' , ' ¯\_(ツ)_/¯ ' , $ _POST ['email ' ]),
94+ ':password ' => $ hash ,
95+ ':about ' => str_replace ('< ' , ' ¯\_(ツ)_/¯ ' , $ _POST ['about ' ]),
96+ ':showEmail ' => $ show_email
97+ ));
98+ $ _SESSION ['success ' ] = 'Account details updated. ' ;
99+ } else {
100+ $ _SESSION ['error ' ] = 'Email taken ' ;
101+ }
102+ } else {
103+ $ _SESSION ['error ' ] = "File is not an image. " ;
104+ $ uploadOk = 0 ;
105+ }
106+ header ("Location: ./index.php " );
107+ }
108+ ?>
109+ <!DOCTYPE html>
110+ <html>
111+
112+ <head>
113+ <title>Account Settings</title>
114+ <style>
115+ html,
116+ body {
117+ height: 100%;
118+ }
119+
120+ body {
121+ display: -ms-flexbox;
122+ display: -webkit-box;
123+ display: flex;
124+ -ms-flex-align: center;
125+ -ms-flex-pack: center;
126+ -webkit-box-align: center;
127+ align-items: center;
128+ -webkit-box-pack: center;
129+ justify-content: center;
130+ padding-top: 40px;
131+ padding-bottom: 40px;
132+ background-color: #f5f5f5;
133+ }
134+
135+ .form-signin {
136+ width: 100%;
137+ max-width: 330px;
138+ padding: 15px;
139+ margin: 0 auto;
140+ }
141+
142+ .form-signin .checkbox {
143+ font-weight: 400;
144+ }
145+
146+ .form-signin .form-control {
147+ position: relative;
148+ box-sizing: border-box;
149+ height: auto;
150+ padding: 10px;
151+ font-size: 16px;
152+ }
153+
154+ .form-signin .form-control:focus {
155+ z-index: 2;
156+ }
157+
158+ .form-signin input[type="email"] {
159+ margin-bottom: -1px;
160+ border-bottom-right-radius: 0;
161+ border-bottom-left-radius: 0;
162+ }
163+
164+ .form-signin input[type="password"] {
165+ margin-bottom: 10px;
166+ border-top-left-radius: 0;
167+ border-top-right-radius: 0;
168+ }
169+ </style>
170+ </head>
171+
172+ <body>
173+ <form class="form-signin" action="account-settings.php" method="post" enctype="multipart/form-data" autocomplete="off">
174+ <h1 class="h3 mb-3 font-weight-normal">Account Settings</h1>
175+ Select image to upload for <?= $ _SESSION ['name ' ] ?>
176+ <input type="file" name="fileToUpload" id="fileToUpload">
177+ <label for="name" class="sr-only">Name</label>
178+ <input type="text" name="name" class="form-control" placeholder="" required="" autofocus="" value="<?= $ response ['name ' ] ?> ">
179+ <label for="email" class="sr-only">Email</label>
180+ <input type="email" name="email" class="form-control" placeholder="" required="" value="<?= $ response ['email ' ] ?> ">
181+ <label for="about" class="sr-only">About</label>
182+ <input type="text" name="about" class="form-control" placeholder="" required="" value="<?= $ response ['about ' ] ?> ">
183+ <label for="password" class="sr-only">New Password</label>
184+ <input type="password" name="password" class="form-control" placeholder="Password" required="">
185+ <div class="checkbox mb-3">
186+ <label>
187+ <input type="checkbox" name="show_email" <?php echo ($ response ['show_email ' ] == 'True ' ) ? 'checked ' : '' ?> > Show Email
188+ </label>
189+ </div>
190+ <input class="btn btn-lg btn-primary btn-block" type="submit" name="submit" value="Save Changes">
191+ <br />
192+ <a href="./index.php">Cancel</a> | <a href="./delete-account.php">Delete Account</a>
193+ </form>
194+ </div>
195+ </body>
196+
197+ </html>
0 commit comments