profile.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * User profile view.
  4. *
  5. * Contains the view layout and ome code to present the data.
  6. *
  7. * @category View
  8. * @property_read Profile_Page $page The page model.
  9. * @property_read int $UID Player ID.
  10. */
  11. ?>
  12. <!DOCTYPE html>
  13. <html lang='en'>
  14. <head>
  15. <meta content='text/html; charset=utf-8' http-equiv='content-type'/>
  16. <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1'/>
  17. <title><?=$page->title?></title>
  18. <link rel='shortcut icon' href='<?=$page->favicon?>'/>
  19. <!-- CSS files -->
  20. <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>ui.css'/>
  21. <link rel='stylesheet' type='text/css' href='<?=URL::CSS?>profile.css'/>
  22. <!-- Meta tags -->
  23. <link rel='canonical' href='<?=$page->canonical?>'/>
  24. <link rel='author' href='<?=$page->author?>'/>
  25. <link rel='publisher' href='<?=$page->author?>'/>
  26. <meta name='description' content='<?=$page->description?>'/>
  27. <meta property='og:title' content='<?=$page->title?>'/>
  28. <meta property='og:url' content='<?=$page->canonical?>'/>
  29. <meta property='og:description' content='<?=$page->description?>'/>
  30. <meta property='og:image' content='<?=$page->icon?>'/>
  31. <meta property='og:site_name' content='<?=$page->name?>'/>
  32. <meta property='og:type' content='website'/>
  33. <meta property='og:locale' content='en'/>
  34. <meta name='twitter:card' content='summary'/>
  35. <meta name='twitter:title' content='<?=$page->title?>'/>
  36. <meta name='twitter:description' content='<?=$page->description?>'/>
  37. <meta name='twitter:image' content='<?=$page->icon?>'/>
  38. <meta name='twitter:url' content='<?=$page->canonical?>'/>
  39. <meta name='robots' content='index follow'/>
  40. <script>
  41. /**
  42. * Redirects to the logout action.
  43. */
  44. function logout(){
  45. window.location = '/action/logout/';
  46. }
  47. /**
  48. * Shows an error window.
  49. *
  50. * @param msg Error message
  51. */
  52. function showError(msg){
  53. document.getElementById('error_window').style.display = 'block';
  54. document.getElementById('error_msg').innerHTML = msg;
  55. }
  56. /**
  57. * Closes the error window.
  58. */
  59. function closeError(){
  60. document.getElementById('error_window').style.display = 'none';
  61. document.getElementById('error_msg').innerHTML = ' ';
  62. }
  63. /**
  64. * Shows the mail change form.
  65. */
  66. function changeMail(){
  67. document.getElementById('static_mail').style.display = 'none';
  68. document.getElementById('change_mail').style.display = 'none';
  69. document.getElementById('edit_mail').style.display = 'block';
  70. document.getElementById('save_mail').style.display = 'block';
  71. }
  72. /**
  73. * Saves an email change.
  74. *
  75. * Validates the new email format and makes an AJAX request. If
  76. * succesfull, hides the email change form.
  77. */
  78. function saveMail(){
  79. var http = new XMLHttpRequest();
  80. var mail = document.getElementById('edit_mail').value;
  81. if (mail.length < 1){
  82. showError('Please enter a new email.');
  83. return;
  84. }
  85. if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail) == false){
  86. showError('Invalid email');
  87. return;
  88. }
  89. http.open('POST', '/action/edit_profile', true);
  90. http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  91. http.onreadystatechange = function(){
  92. if (this.readyState == 4){
  93. if (this.status == 200 || this.status == 204){
  94. document.getElementById('static_mail').innerHTML = mail;
  95. document.getElementById('static_mail').style.display = 'block';
  96. document.getElementById('change_mail').style.display = 'block';
  97. document.getElementById('edit_mail').style.display = 'none';
  98. document.getElementById('save_mail').style.display = 'none';
  99. }
  100. else{
  101. showError('An error ocurred: ' + this.responseText + ' (' + this.status + ').');
  102. }
  103. }
  104. }
  105. http.send('uid=<?=$UID?>&mail=' + mail);
  106. }
  107. /**
  108. * Shows the password change form.
  109. */
  110. function changePass(){
  111. document.getElementById('static_pass').style.display = 'none';
  112. document.getElementById('change_pass').style.display = 'none';
  113. document.getElementById('edit_pass_current').value = '';
  114. document.getElementById('edit_pass_current').value = '';
  115. document.getElementById('edit_pass_new').value = '';
  116. document.getElementById('edit_pass_repeat').style.display = 'block';
  117. document.getElementById('edit_pass_new').style.display = 'block';
  118. document.getElementById('edit_pass_repeat').style.display = 'block';
  119. document.getElementById('save_pass').style.display = 'block';
  120. }
  121. /**
  122. * Saves a password change.
  123. *
  124. * Validates the new password and makes an AJAX request. If
  125. * succesfull, hides the password change form.
  126. */
  127. function savePass(){
  128. var http = new XMLHttpRequest();
  129. var pass = document.getElementById('edit_pass_new').value;
  130. var currentPass = document.getElementById('edit_pass_current').value;
  131. var repeatPass = document.getElementById('edit_pass_repeat').value;
  132. if (pass.length < 1){
  133. showError('Please enter a new password.');
  134. return;
  135. }
  136. if (mail.length < 6){
  137. showError('Minimum password length is 6 characters.');
  138. return;
  139. }
  140. if (currentPass.length < 1){
  141. showError('Please enter your current password.');
  142. return;
  143. }
  144. if (pass != repeatPass){
  145. showError('Entered passwords do not match.');
  146. return;
  147. }
  148. http.open('POST', '/action/edit_profile', true);
  149. http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  150. http.onreadystatechange = function(){
  151. if (this.readyState == 4){
  152. if (this.status == 200 || this.status == 204){
  153. document.getElementById('static_pass').style.display = 'block';
  154. document.getElementById('change_pass').style.display = 'block';
  155. document.getElementById('edit_pass_current').style.display = 'none';
  156. document.getElementById('edit_pass_new').style.display = 'none';
  157. document.getElementById('edit_pass_repeat').style.display = 'none';
  158. document.getElementById('save_pass').style.display = 'none';
  159. }
  160. else{
  161. showError('An error ocurred: ' + this.responseText + ' (' + this.status + ').');
  162. }
  163. }
  164. }
  165. http.send('uid=<?=$UID?>&pass=' + pass + '&currentPass=' + currentPass);
  166. }
  167. /**
  168. * Generates a new random API key.
  169. *
  170. * Makes a request. On success, updates the API in the UI.
  171. */
  172. function regenerateApi(){
  173. var http = new XMLHttpRequest();
  174. http.open('POST', '/action/edit_profile', true);
  175. http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  176. http.onreadystatechange = function(){
  177. if (this.readyState == 4){
  178. if (this.status == 200 || this.status == 204){
  179. document.getElementById('api_key').innerHTML = this.responseText;
  180. }
  181. else{
  182. showError('An error ocurred: ' + this.responseText + ' (' + this.status + ').');
  183. }
  184. }
  185. }
  186. http.send('uid=<?=$UID?>&api=1');
  187. }
  188. </script>
  189. </head>
  190. <body>
  191. <?php
  192. include __DIR__ . "/inc/header.php";
  193. ?>
  194. <section id='monster' class='content'>
  195. <h2>
  196. <span>
  197. <?=$page->name?>
  198. </span>
  199. </h2>
  200. <article>
  201. <table id='profile'>
  202. <tr>
  203. <td class='title'>
  204. Username:
  205. </td>
  206. <td class='value'>
  207. <span>
  208. <?=$page->name?>
  209. </span>
  210. </td>
  211. <td class='action'>
  212. <input onclick='logout();' type='button' value='Logout'/>
  213. </td>
  214. </tr>
  215. <tr>
  216. <td class='title'>
  217. eMail:
  218. </td>
  219. <td class='value'>
  220. <span id='static_mail'>
  221. <?=$page->mail?>
  222. </span>
  223. <input id='edit_mail' type='text' value='<?=$page->mail?>'/>
  224. </td>
  225. <td class='action'>
  226. <input id='change_mail' onclick='changeMail();' type='button' value='Change'/>
  227. <input id='save_mail' onclick='saveMail();' type='button' value='Save'/>
  228. </td>
  229. </tr>
  230. <tr>
  231. <td class='title'>
  232. API key:
  233. </td>
  234. <td class='value'>
  235. <span id='api_key'>
  236. <?=$page->api_key?>
  237. </span>
  238. </td>
  239. <td class='action'>
  240. <input id='regenerate_api' onclick='regenerateApi();' type='button' value='Regenerate'/>
  241. </td>
  242. </tr>
  243. <tr>
  244. <td class='title'>
  245. Password:
  246. </td>
  247. <td class='value'>
  248. <span id='static_pass'>
  249. ********
  250. </span>
  251. <input id='edit_pass_current' type='password' placeholder='Currrent password'/>
  252. <input id='edit_pass_new' type='password' placeholder='New password'/>
  253. <input id='edit_pass_repeat' type='password' placeholder='Repeat password'/>
  254. </td>
  255. <td class='action'>
  256. <input id='change_pass' onclick='changePass();' type='button' value='Change'/>
  257. <input id='save_pass' onclick='savePass();' type='button' value='Save'/>
  258. </td>
  259. </tr>
  260. </table>
  261. </article>
  262. </section> <!-- runes -->
  263. <div id='error_window'>
  264. <section class='content'>
  265. <h2>
  266. <span>
  267. Error
  268. </span>
  269. </h2>
  270. <article>
  271. <p id='error_msg'>
  272. </p>
  273. <input type='button' value='Close' onclick='closeError();'/>
  274. </article>
  275. </section>
  276. </div>
  277. <?php
  278. include __DIR__ . "/inc/footer.php";
  279. ?>
  280. </body>
  281. </html>