PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/code/ajax.php

https://github.com/ahassan/opentape
PHP | 123 lines | 83 code | 34 blank | 6 comment | 32 complexity | 3ce831e6a7cd1648ed0bae21c8d208f7 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. require_once ('opentape_common.php');
  3. $json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  4. $command = $_POST['command'];
  5. $args = $json->decode(stripslashes($_POST['args']));
  6. header("Content-type: application/json; charset=UTF-8");
  7. // create_password is the exception, since you can't be logged in
  8. if (!is_logged_in() && strcmp($command, "create_password") ) {
  9. echo '{"status":false,"command":"' . $command . '","debug":"You must authenticate."}';
  10. exit;
  11. }
  12. //error_log ("$command - " . print_r($args,1));
  13. if (isset($args['password1']) && !strcmp($args['password1'], $args['password2']) && !strcmp($command,"create_password")) {
  14. // don't allow people to set password using this method once the file exists
  15. if(is_password_set()) { echo '{"status":false,"command":"' . $command . '","debug":"The password is already configured, login to change it."}'; }
  16. if (set_password($args['password1'])) {
  17. // proceed to next step, nothing here really...
  18. } else {
  19. echo '{"status":false,"command":"' . $command . '","debug":""}';
  20. exit;
  21. }
  22. if (create_session()) {
  23. echo '{"status":true,"command":"create_password","debug":""}';
  24. } else {
  25. echo '{"status":false,"command":"' . $command . '","debug":""}';
  26. }
  27. } elseif (isset($args['password1']) && !strcmp($args['password1'], $args['password2']) && !strcmp($command,"change_password")) {
  28. if (set_password($args['password1'])) {
  29. echo '{"status":true,"command":"' . $command . '","debug":""}';
  30. } else {
  31. echo '{"status":false,"command":"' . $command . '","debug":""}';
  32. }
  33. } elseif (!strcmp($command, "rename")) {
  34. if(get_magic_quotes_gpc()) {
  35. $_POST['artist'] = stripslashes($_POST['artist']);
  36. $_POST['title'] = stripslashes($_POST['title']);
  37. }
  38. if (rename_song($args['song_key'], $_POST['artist'], $_POST['title'])) {
  39. echo '{"status":true,"command":"' . $command . '","debug":"","args":{"song_key":"' . $args['song_key'] . '","artist":"' . escape_for_json($_POST['artist']) . '","title":"' . escape_for_json($_POST['title']) .'"}}';
  40. } else {
  41. echo '{"status":false,"command":"' . $command . '","debug":""}';
  42. }
  43. } elseif (!strcmp($command, "reorder")) {
  44. if (reorder_songs($args)) {
  45. echo '{"status":true,"command":"' . $command . '","debug":""}';
  46. } else {
  47. echo '{"status":false,"command":"' . $command . '","debug":""}';
  48. }
  49. } elseif (!strcmp($command, "delete")) {
  50. if (delete_song($_POST['args'])) {
  51. echo '{"status":true,"command":"' . $command . '","debug":"","args":"' . $_POST['args'] . '"}';
  52. } else {
  53. echo '{"status":false,"command":"' . $command . '","debug":""}';
  54. }
  55. } elseif (!strcmp($command, "bannercaptioncolor")) {
  56. $prefs_struct = get_opentape_prefs();
  57. if(get_magic_quotes_gpc()) {
  58. $_POST['banner'] = stripslashes($_POST['banner']);
  59. $_POST['caption'] = stripslashes($_POST['caption']);
  60. $_POST['color'] = stripslashes($_POST['color']);
  61. }
  62. $prefs_struct['banner'] = $_POST['banner'];
  63. $prefs_struct['caption'] = $_POST['caption'];
  64. $prefs_struct['color'] = $_POST['color'];
  65. if (write_opentape_prefs($prefs_struct)) {
  66. echo '{"status":true,"command":"' . $command . '","debug":""}';
  67. } else {
  68. echo '{"status":false,"command":"' . $command . '","debug":""}';
  69. }
  70. } elseif (!strcmp($command, "set_option")) {
  71. $prefs_struct = get_opentape_prefs();
  72. // maybe should check if the key is a valid data item type, to prevent
  73. // some kind of sizing attack... though checking for login does well.
  74. foreach ($args as $key => $data) {
  75. if (!strcmp($data,"on") || !strcmp($data,"true") || $data===true || $data==1 ) {
  76. $prefs_struct[$key] = 1;
  77. } else {
  78. $prefs_struct[$key] = 0;
  79. }
  80. }
  81. if (write_opentape_prefs($prefs_struct)) {
  82. echo '{"status":true,"command":"' . $command . '","debug":""}';
  83. } else {
  84. echo '{"status":false,"command":"' . $command . '","debug":""}';
  85. }
  86. } else {
  87. echo '{"status":false,"command":"' . $command . '","debug":""}';
  88. }
  89. ?>