PageRenderTime 27ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/handlers/ajaxHandler.php

http://cintient.googlecode.com/
PHP | 133 lines | 50 code | 25 blank | 58 comment | 11 complexity | 59761e5d63b1c2f59713cc8f048722d8 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. *
  4. * Cintient, Continuous Integration made simple.
  5. * Copyright (c) 2010, 2011, Pedro Mata-Mouros Fonseca
  6. *
  7. * This file is part of Cintient.
  8. *
  9. * Cintient is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * Cintient is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with Cintient. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /* +----------------------------------------------------------------+ *\
  24. |* | EARLY SANATIZATION | *|
  25. \* +----------------------------------------------------------------+ */
  26. //
  27. // Proxy layer compatibility hack, if there is one.
  28. //
  29. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  30. $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
  31. }
  32. /* +----------------------------------------------------------------+ *\
  33. |* | SETUP | *|
  34. \* +----------------------------------------------------------------+ */
  35. require dirname(__FILE__) . '/../config/cintient.conf.php';
  36. ini_set('display_errors', 0); // Don't echo errors on AJAX calls
  37. session_start(); // session_start *has* to come after the custom autoloading
  38. SystemEvent::setSeverityLevel(CINTIENT_LOG_SEVERITY);
  39. //
  40. // Global stuff
  41. //
  42. // Get to the part of the URL that matters
  43. $currentUrl = 'http://' . $_SERVER['HTTP_HOST'] . strtok($_SERVER['REQUEST_URI'], '?');
  44. $GLOBALS['uri'] = substr($currentUrl, strlen(CINTIENT_BASE_URL));
  45. SystemEvent::raise(SystemEvent::DEBUG, "Handling request. [URI={$GLOBALS['uri']}" . (empty($_SERVER['QUERY_STRING'])?'':'?'.html_entity_decode($_SERVER['QUERY_STRING'])) . "]", "AjaxHandler");
  46. $GLOBALS['ajaxMethod'] = null;
  47. $GLOBALS['section'] = null;
  48. $GLOBALS['subSection'] = null;
  49. $GLOBALS['user'] = (isset($_SESSION['userId']) ? User::getById($_SESSION['userId']) : null);
  50. $GLOBALS['project'] = (isset($_SESSION['projectId']) ? Project::getById($GLOBALS['user'], $_SESSION['projectId']) : null);
  51. /* +----------------------------------------------------------------+ *\
  52. |* | URL HANDLING | *|
  53. \* +----------------------------------------------------------------+ */
  54. //
  55. // Ajax related
  56. //
  57. if (preg_match('/^\/ajax\/([\w-]+)(?:\/([\w-]+))?\/$/', $GLOBALS['uri'], $matches)) {
  58. if (count($matches) <= 2) {
  59. $GLOBALS['section'] = 'default';
  60. $GLOBALS['subSection'] = $matches[1];
  61. } else {
  62. $GLOBALS['section'] = $matches[1];
  63. $GLOBALS['subSection'] = $matches[2];
  64. }
  65. }
  66. /* +----------------------------------------------------------------+ *\
  67. |* | AUTHENTICATION | *|
  68. \* +----------------------------------------------------------------+ */
  69. if (!isset($GLOBALS['user']) || !($GLOBALS['user'] instanceof User)) {
  70. SystemEvent::raise(SystemEvent::INFO, "Authentication is required on all ajax requests. [URI={$GLOBALS['uri']}]", "AjaxHandler");
  71. // TODO: send error here
  72. exit;
  73. }
  74. /* +----------------------------------------------------------------+ *\
  75. |* | ROUTING | *|
  76. \* +----------------------------------------------------------------+ */
  77. //
  78. // Ajax related
  79. //
  80. if (!empty($GLOBALS['section'])) {
  81. $GLOBALS['ajaxMethod'] = $GLOBALS['subSection'];
  82. if (strpos($GLOBALS['subSection'], '-') !== false) {
  83. $subSectionPieces = explode('-', $GLOBALS['subSection']);
  84. array_walk($subSectionPieces, function(&$value) {
  85. $value = ucfirst($value);
  86. });
  87. $GLOBALS['ajaxMethod'] = lcfirst(implode($subSectionPieces));
  88. }
  89. if ($GLOBALS['section'] != 'default') {
  90. $GLOBALS['ajaxMethod'] = $GLOBALS['section'] . '_' . $GLOBALS['ajaxMethod'];
  91. }
  92. if (method_exists('AjaxManager', $GLOBALS['ajaxMethod'])) {
  93. #if DEBUG
  94. SystemEvent::raise(SystemEvent::DEBUG, "Routing to known ajax function. [FUNCTION=AjaxManager::{$GLOBALS['ajaxMethod']}] [URI={$GLOBALS['uri']}]", "AjaxHandler");
  95. #endif
  96. AjaxManager::$GLOBALS['ajaxMethod']();
  97. exit;
  98. }
  99. #if DEBUG
  100. SystemEvent::raise(SystemEvent::DEBUG, "Unknown ajax function. [FUNCTION=AjaxManager::{$GLOBALS['ajaxMethod']}] [URI={$GLOBALS['uri']}]", "AjaxHandler");
  101. #endif
  102. }
  103. /* +----------------------------------------------------------------+ *\
  104. |* | RESTAURANT AT THE END OF THE UNIVERSE | *|
  105. \* +----------------------------------------------------------------+ */
  106. SystemEvent::raise(SystemEvent::INFO, "Not found. [URI={$GLOBALS['uri']}] [USER=" . (($GLOBALS['user'] instanceof User)? $GLOBALS['user']->getUsername() : 'N/A') . ']');
  107. // TODO: send error here
  108. exit;