Source for file PathInfoRequestHandler.class.php

Documentation is available at PathInfoRequestHandler.class.php

  1. <?php
  2. /**
  3. * @since 1/8/07
  4. * @package harmoni.architecture.request
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: PathInfoRequestHandler.class.php,v 1.2 2007/09/04 20:25:31 adamfranco Exp $
  10. */
  11.  
  12. require_once(HARMONI."architecture/request/RequestHandler.interface.php");
  13. require_once(HARMONI."architecture/request/URLWriter.abstract.php");
  14.  
  15. /**
  16. * <##>
  17. *
  18. * @since 1/8/07
  19. * @package harmoni.architecture.request
  20. *
  21. * @copyright Copyright &copy; 2005, Middlebury College
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  23. *
  24. * @version $Id: PathInfoRequestHandler.class.php,v 1.2 2007/09/04 20:25:31 adamfranco Exp $
  25. */
  26. class PathInfoRequestHandler
  27. extends RequestHandler
  28. {
  29. /**
  30. * Returns an associative array of key=value pairs corresponding to the request
  31. * data from the browser. This could just be the data from $_REQUEST, in the
  32. * simplest case.
  33. *
  34. * @return array
  35. * @access public
  36. */
  37. function getRequestVariables() {
  38. $this->_loadRequest();
  39. return $this->_request;
  40. }
  41. /**
  42. * Returns an associative array of file upload data. This will usually come from
  43. * the $_FILES superglobal.
  44. *
  45. * @return array
  46. * @access public
  47. */
  48. function getFileVariables() {
  49. return $_FILES;
  50. }
  51. /**
  52. * Returns a new {@link URLWriter} object corresponding to this RequestHandler.
  53. *
  54. * @return ref object URLWriter
  55. * @access public
  56. */
  57. function createURLWriter() {
  58. $writer = new PathInfoURLWriter();
  59. return $writer;
  60. }
  61. /**
  62. * Returns a dotted-pair string representing the module and action requested
  63. * by the end user ("module.action" format).
  64. *
  65. * @return string
  66. * @access public
  67. */
  68. function getRequestedModuleAction() {
  69. $this->_loadRequest();
  70. if (isset($this->_request["module"]))
  71. $mod = preg_replace('/[^a-zA-Z0-9_\-]/i', '', $this->_request["module"]);
  72. else
  73. $mod = NULL;
  74. if (isset($this->_request["action"]))
  75. $act = preg_replace('/[^a-zA-Z0-9_\-]/i', '', $this->_request["action"]);
  76. else
  77. $act = NULL;
  78. return $mod .".". $act;
  79. }
  80. /**
  81. * build an array of all request parameters including those from the path info
  82. *
  83. * @return void
  84. * @access public
  85. * @since 1/8/07
  86. */
  87. function _loadRequest () {
  88. if (!isset($this->_request)) {
  89. // Add on other vars that may be pass in get or post
  90. $this->_request = $_REQUEST;
  91. if (isset($_SERVER['PATH_INFO'])) {
  92. $pathInfo = trim($_SERVER['PATH_INFO'], "/");
  93. if ($pathInfo) {
  94. $pathInfoParts = explode('/', $pathInfo);
  95. // printpre($pathInfoParts);
  96. // exit;
  97. // Set the module and Action to the first two pathinfo parts
  98. $this->_request['module'] = $pathInfoParts[0];
  99. $this->_request['action'] = $pathInfoParts[1];
  100. // Add the rest of the path as name => value pairs
  101. for ($i = 2; $i < count ($pathInfoParts); $i = $i + 2) {
  102. $this->_request[$pathInfoParts[$i]] = $pathInfoParts[$i+1];
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109.  
  110.  
  111. /**
  112. * The purpose of a URLWriter is to generate URLs from contextual data. This
  113. * data would be the current/target module and action, any contextual name=value
  114. * pairs specified by the code, and any additional query data.
  115. *
  116. * @package harmoni.architecture.request
  117. *
  118. * @copyright Copyright &copy; 2005, Middlebury College
  119. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  120. *
  121. * @version $Id: PathInfoRequestHandler.class.php,v 1.2 2007/09/04 20:25:31 adamfranco Exp $
  122. */
  123.  
  124. class PathInfoURLWriter
  125. extends URLWriter
  126. {
  127. /**
  128. * The following function has many forms, and due to PHP's lack of
  129. * method overloading they are all contained within the same class
  130. * method.
  131. *
  132. * write()
  133. * write(array $vars)
  134. * write(string $key1, string $value1, [string $key2, string $value2, [...]])
  135. *
  136. * @access public
  137. * @return string The URL.
  138. */
  139. function write(/* variable-length argument list*/) {
  140. if (!defined("MYURL")) {
  141. throwError( new Error("PathInfoURLWriter requires that 'MYURL' is defined and set to the full URL of the main index PHP script of this Harmoni program!", "PathInfoURLWriter", true));
  142. }
  143. $num = func_num_args();
  144. $args = func_get_args();
  145. if ($num > 1 && $num % 2 == 0) {
  146. for($i = 0; $i < $num; $i+=2) {
  147. $this->_vars[RequestContext::name($args[$i])] = $args[$i+1];
  148. }
  149. } else if ($num == 1 && is_array($args[0])) {
  150. $this->setValues($args[0]);
  151. }
  152. $url = MYURL;
  153. $pairs = array();
  154. $harmoni = Harmoni::instance();
  155. if (!$harmoni->config->get("sessionUseOnlyCookies") && defined("SID") && SID)
  156. $pairs[] = strip_tags(SID);
  157. $pairs[] = $this->_module;
  158. $pairs[] = $this->_action;
  159. foreach ($this->_vars as $key=>$val) {
  160. if (is_object($val)) {
  161. throwError( new Error("Expecting string for key '$key', got '$val'.", "GETMethodRequestHandler", true));
  162. }
  163. $pairs[] = $key . "/" . urlencode($val);
  164. }
  165. $url .= "/" . implode("/", $pairs);
  166. return $url;
  167. }
  168. }
  169.  
  170. ?>

Documentation generated on Wed, 19 Sep 2007 10:25:28 -0400 by phpDocumentor 1.3.0RC3