profile.php.txt 12 KB

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