profile.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /**
  3. * User profile view.
  4. *
  5. * Contains the view layout and ome code to present the data.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. * @category View
  11. * @property Profile_Page $page The page model.
  12. * @var Profile_Page $page The page model.
  13. */
  14. ?>
  15. <!DOCTYPE html>
  16. <html lang='en'>
  17. <head>
  18. <?=$page->generate_head()?>
  19. <script>
  20. /**
  21. * Redirects to the logout action.
  22. */
  23. function logout(){
  24. window.location = '/action/logout/';
  25. }
  26. /**
  27. * Shows an error window.
  28. *
  29. * @param msg Error message
  30. */
  31. function showError(msg){
  32. document.getElementById('error_window').style.display = 'block';
  33. document.getElementById('error_msg').innerHTML = msg;
  34. }
  35. /**
  36. * Closes the error window.
  37. */
  38. function closeError(){
  39. document.getElementById('error_window').style.display = 'none';
  40. document.getElementById('error_msg').innerHTML = ' ';
  41. }
  42. /**
  43. * Shows the mail change form.
  44. */
  45. function changeMail(){
  46. document.getElementById('static_mail').style.display = 'none';
  47. document.getElementById('change_mail').style.display = 'none';
  48. document.getElementById('edit_mail').style.display = 'block';
  49. document.getElementById('save_mail').style.display = 'block';
  50. }
  51. /**
  52. * Saves an email change.
  53. *
  54. * Validates the new email format and makes an AJAX request. If
  55. * succesfull, hides the email change form.
  56. */
  57. function saveMail(){
  58. var http = new XMLHttpRequest();
  59. var mail = document.getElementById('edit_mail').value;
  60. if (mail.length < 1){
  61. showError('Please enter a new email.');
  62. return;
  63. }
  64. if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail) == false){
  65. showError('Invalid email');
  66. return;
  67. }
  68. http.open('POST', '/action/edit_profile', true);
  69. http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  70. http.onreadystatechange = function(){
  71. if (this.readyState == 4){
  72. if (this.status == 200 || this.status == 204){
  73. document.getElementById('static_mail').innerHTML = mail;
  74. document.getElementById('static_mail').style.display = 'block';
  75. document.getElementById('change_mail').style.display = 'block';
  76. document.getElementById('edit_mail').style.display = 'none';
  77. document.getElementById('save_mail').style.display = 'none';
  78. }
  79. else{
  80. showError('An error ocurred: ' + this.responseText + ' (' + this.status + ').');
  81. }
  82. }
  83. }
  84. http.send('user=<?=get_context()->get_user()->get_id()?>&mail=' + mail);
  85. }
  86. /**
  87. * Shows the password change form.
  88. */
  89. function changePass(){
  90. document.getElementById('static_pass').style.display = 'none';
  91. document.getElementById('change_pass').style.display = 'none';
  92. document.getElementById('edit_pass_current').value = '';
  93. document.getElementById('edit_pass_current').value = '';
  94. document.getElementById('edit_pass_new').value = '';
  95. document.getElementById('edit_pass_repeat').style.display = 'block';
  96. document.getElementById('edit_pass_new').style.display = 'block';
  97. document.getElementById('edit_pass_repeat').style.display = 'block';
  98. document.getElementById('save_pass').style.display = 'block';
  99. }
  100. /**
  101. * Saves a password change.
  102. *
  103. * Validates the new password and makes an AJAX request. If
  104. * succesfull, hides the password change form.
  105. */
  106. function savePass(){
  107. var http = new XMLHttpRequest();
  108. var pass = document.getElementById('edit_pass_new').value;
  109. var currentPass = document.getElementById('edit_pass_current').value;
  110. var repeatPass = document.getElementById('edit_pass_repeat').value;
  111. if (pass.length < 1){
  112. showError('Please enter a new password.');
  113. return;
  114. }
  115. if (mail.length < 6){
  116. showError('Minimum password length is 6 characters.');
  117. return;
  118. }
  119. if (currentPass.length < 1){
  120. showError('Please enter your current password.');
  121. return;
  122. }
  123. if (pass != repeatPass){
  124. showError('Entered passwords do not match.');
  125. return;
  126. }
  127. http.open('POST', '/action/edit_profile', true);
  128. http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  129. http.onreadystatechange = function(){
  130. if (this.readyState == 4){
  131. if (this.status == 200 || this.status == 204){
  132. document.getElementById('static_pass').style.display = 'block';
  133. document.getElementById('change_pass').style.display = 'block';
  134. document.getElementById('edit_pass_current').style.display = 'none';
  135. document.getElementById('edit_pass_new').style.display = 'none';
  136. document.getElementById('edit_pass_repeat').style.display = 'none';
  137. document.getElementById('save_pass').style.display = 'none';
  138. }
  139. else{
  140. showError('An error ocurred: ' + this.responseText + ' (' + this.status + ').');
  141. }
  142. }
  143. }
  144. http.send('user=<?=get_context()->get_user()->get_id()?>&pass=' + pass + '&currentPass=' + currentPass);
  145. }
  146. /**
  147. * Generates a new random API key.
  148. *
  149. * Makes a request. On success, updates the API in the UI.
  150. */
  151. function regenerateApi(){
  152. var http = new XMLHttpRequest();
  153. http.open('POST', '/action/edit_profile', true);
  154. http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  155. http.onreadystatechange = function(){
  156. if (this.readyState == 4){
  157. if (this.status == 200 || this.status == 204){
  158. document.getElementById('api_key').innerHTML = this.responseText;
  159. }
  160. else{
  161. showError('An error ocurred: ' + this.responseText + ' (' + this.status + ').');
  162. }
  163. }
  164. }
  165. http.send('user=<?=get_context()->get_user()->get_id()?>&api=1');
  166. }
  167. </script>
  168. </head>
  169. <header>
  170. <?php
  171. include __DIR__ . "/inc/header_app.php";
  172. ?>
  173. </header>
  174. <aside>
  175. <h2>
  176. <?=get_context()->get_user()->get_name()?>
  177. </h2>
  178. <p>
  179. Edit your profile settings
  180. </p>
  181. </aside>
  182. <main>
  183. <section class='content'>
  184. <h2>
  185. <?=get_context()->get_user()->get_name()?>
  186. </h2>
  187. <article>
  188. <table id='profile'>
  189. <tr>
  190. <td class='title'>
  191. Username:
  192. </td>
  193. <td class='value'>
  194. <span>
  195. <?=get_context()->get_user()->get_name()?>
  196. </span>
  197. </td>
  198. <td class='action'>
  199. <input onclick='logout();' type='button' value='Logout'/>
  200. </td>
  201. </tr>
  202. <tr>
  203. <td class='title'>
  204. eMail:
  205. </td>
  206. <td class='value'>
  207. <span id='static_mail'>
  208. <?=get_context()->get_user()->get_mail()?>
  209. </span>
  210. <input id='edit_mail' type='text' value='<?=get_context()->get_user()->get_mail()?>'/>
  211. </td>
  212. <td class='action'>
  213. <input id='change_mail' onclick='changeMail();' type='button' value='Change'/>
  214. <input id='save_mail' onclick='saveMail();' type='button' value='Save'/>
  215. </td>
  216. </tr>
  217. <tr>
  218. <td class='title'>
  219. API key:
  220. </td>
  221. <td class='value'>
  222. <span id='api_key'>
  223. <?=get_context()->get_user()->get_api_key()?>
  224. </span>
  225. </td>
  226. <td class='action'>
  227. <input id='regenerate_api' onclick='regenerateApi();' type='button' value='Regenerate'/>
  228. </td>
  229. </tr>
  230. <tr>
  231. <td class='title'>
  232. Password:
  233. </td>
  234. <td class='value'>
  235. <span id='static_pass'>
  236. ********
  237. </span>
  238. <input id='edit_pass_current' type='password' placeholder='Currrent password'/>
  239. <input id='edit_pass_new' type='password' placeholder='New password'/>
  240. <input id='edit_pass_repeat' type='password' placeholder='Repeat password'/>
  241. </td>
  242. <td class='action'>
  243. <input id='change_pass' onclick='changePass();' type='button' value='Change'/>
  244. <input id='save_pass' onclick='savePass();' type='button' value='Save'/>
  245. </td>
  246. </tr>
  247. <tr>
  248. <td class='title'>
  249. Visibility:
  250. </td>
  251. <td id='visibility'>
  252. <input type='checkbox' name='visible' onChange='toggleVisibility(this.value);'/>
  253. <label for='filter_defeat'>Public profile</label>
  254. <div class='help_tooltip'>
  255. <p>
  256. When checked, other users can view you game data. This includes:
  257. <span>Units</span>
  258. <span>Teams</span>
  259. <span>Runes, artifacts and enchantments</span>
  260. <span>Your guild name</span>
  261. <span>Records</span>
  262. <span>Building levels</span>
  263. <span>Guild name</span>
  264. This data will never be shown to other users:
  265. <span>Personal data (email, password...)</span>
  266. <span>Run logs</span>
  267. <span>Statistics</span>
  268. <span>Guild member list</span>
  269. <span>Inventory</span>
  270. <span>Building levels</span>
  271. <span>Guild name</span>
  272. </p>
  273. </div>
  274. </td>
  275. </tr>
  276. </table>
  277. </article>
  278. </section>
  279. </main>
  280. <div id='error_window'>
  281. <section class='content'>
  282. <h2>
  283. <span>
  284. Error
  285. </span>
  286. </h2>
  287. <article>
  288. <p id='error_msg'>
  289. </p>
  290. <input type='button' value='Close' onclick='closeError();'/>
  291. </article>
  292. </section>
  293. </div>
  294. <?php
  295. include __DIR__ . "/inc/footer.php";
  296. ?>
  297. </body>
  298. </html>