PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/amfphp/core/shared/app/BasicActions.php

https://gitlab.com/endomorphosis/suschu
PHP | 183 lines | 147 code | 19 blank | 17 comment | 18 complexity | 51a730bf5370e970e177fe5adcbd9988 MD5 | raw file
  1. <?php
  2. /**
  3. * This file contains actions which are used by all gateways
  4. */
  5. include_once(AMFPHP_BASE . 'shared/util/Authenticate.php');
  6. include_once(AMFPHP_BASE . 'shared/util/NetDebug.php');
  7. include_once(AMFPHP_BASE . 'shared/util/Headers.php');
  8. include_once(AMFPHP_BASE . 'shared/util/CharsetHandler.php');
  9. /**
  10. * Class loader action loads the class from which we will get the remote method
  11. */
  12. function classLoaderAction (&$amfbody) {
  13. if(!$amfbody->noExec)
  14. {
  15. // change to the gateway.php script directory
  16. // now change to the directory of the classpath. Possible relative to gateway.php
  17. $dirname = dirname($amfbody->classPath);
  18. if(is_dir($dirname))
  19. {
  20. chdir($dirname);
  21. }
  22. else
  23. {
  24. $ex = new MessageException(E_USER_ERROR, "The classpath folder {" . $amfbody->classPath . "} does not exist. You probably misplaced your service." , __FILE__, __LINE__, "AMFPHP_CLASSPATH_NOT_FOUND");
  25. MessageException::throwException($amfbody, $ex);
  26. return false;
  27. }
  28. $fileExists = @file_exists(basename($amfbody->classPath)); // see if the file exists
  29. if(!$fileExists)
  30. {
  31. $ex = new MessageException(E_USER_ERROR, "The class {" . $amfbody->className . "} could not be found under the class path {" . $amfbody->classPath . "}" , __FILE__, __LINE__, "AMFPHP_FILE_NOT_FOUND");
  32. MessageException::throwException($amfbody, $ex);
  33. return false;
  34. }
  35. global $amfphp;
  36. $time = microtime_float();
  37. $fileIncluded = Executive::includeClass($amfbody, "./" . basename($amfbody->classPath));
  38. $amfphp['includeTime'] += microtime_float() - $time;
  39. if (!$fileIncluded)
  40. {
  41. $ex = new MessageException(E_USER_ERROR, "The class file {" . $amfbody->className . "} exists but could not be included. The file may have syntax errors, or includes at the top of the file cannot be resolved.", __FILE__, __LINE__, "AMFPHP_FILE_NOT_INCLUDED");
  42. MessageException::throwException($amfbody, $ex);
  43. return false;
  44. }
  45. if (!class_exists($amfbody->className))
  46. { // Just make sure the class name is the same as the file name
  47. $ex = new MessageException(E_USER_ERROR, "The file {" . $amfbody->className . ".php} exists and was included correctly but a class by that name could not be found in that file. Perhaps the class is misnamed.", __FILE__, __LINE__, "AMFPHP_CLASS_NOT_FOUND");
  48. MessageException::throwException($amfbody, $ex);
  49. return false;
  50. }
  51. //Let executive handle building the class
  52. //The executive can handle making exceptions and all that, that's why
  53. $classConstruct = Executive::buildClass($amfbody, $amfbody->className);
  54. if($classConstruct !== '__amfphp_error')
  55. {
  56. $amfbody->setClassConstruct($classConstruct);
  57. }
  58. else
  59. {
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. /**
  66. * MetaDataAction loads the required info from the methodTable
  67. */
  68. function securityAction (&$amfbody) {
  69. if(!$amfbody->noExec)
  70. {
  71. $classConstruct = &$amfbody->getClassConstruct();
  72. $methodName = $amfbody->methodName;
  73. $className = $amfbody->className;
  74. //Check if method exists
  75. if (!method_exists($classConstruct, $methodName)) { // check to see if the method exists
  76. $ex = new MessageException(E_USER_ERROR, "The method {" . $methodName . "} does not exist in class {" . $className . "}.", __FILE__, __LINE__, "AMFPHP_INEXISTANT_METHOD");
  77. MessageException::throwException($amfbody, $ex);
  78. return false;
  79. }
  80. //Check if method is private (PHP4)
  81. if (strpos($methodName, '_') === 0) { // check to see if the method exists
  82. $ex = new MessageException(E_USER_ERROR, "The method {" . $methodName . "} starts with an underscore and is therefore considered private, so it cannot be remotely called.", __FILE__, __LINE__, "AMFPHP_PRIVATE_METHOD");
  83. MessageException::throwException($amfbody, $ex);
  84. return false;
  85. }
  86. //Check to see if method is private or protected (PHP5)
  87. if(class_exists('ReflectionMethod'))
  88. {
  89. $method = new ReflectionMethod($className, $methodName);
  90. if(!$method->isPublic())
  91. {
  92. $ex = new MessageException(E_USER_ERROR, "The method {" . $methodName . "} in {" . $className . "} is not public and therefore cannot be called.", __FILE__, __LINE__, "AMFPHP_PRIVATE_METHOD");
  93. MessageException::throwException($amfbody, $ex);
  94. return false;
  95. }
  96. }
  97. $classConstruct = &$amfbody->getClassConstruct();
  98. $methodName = $amfbody->methodName;
  99. $className = $amfbody->className;
  100. if (method_exists($classConstruct, "beforeFilter")) {
  101. //Pass throught the executive
  102. $allow = Executive::doMethodCall($amfbody,
  103. $classConstruct,
  104. 'beforeFilter',
  105. array($methodName));
  106. if ($allow === '__amfphp_error' || $allow === false) {
  107. $ex = new MessageException(E_USER_ERROR, "Method access blocked by beforeFilter in " . $className . " class", __FILE__, __LINE__, "AMFPHP_AUTHENTICATE_ERROR");
  108. MessageException::throwException($amfbody, $ex);
  109. return false;
  110. }
  111. }
  112. }
  113. return true;
  114. }
  115. function adapterMap(&$results)
  116. {
  117. if(is_array($results))
  118. {
  119. array_walk($results, 'adapterMap');
  120. }
  121. elseif(is_object($results))
  122. {
  123. $className = strtolower(get_class($results));
  124. if(array_key_exists($className, $GLOBALS['amfphp']['adapterMappings']))
  125. {
  126. $type = $GLOBALS['amfphp']['adapterMappings'][$className];
  127. $results = mapRecordSet($results, $type);
  128. }
  129. else
  130. {
  131. $vars = get_object_vars($results);
  132. array_walk($vars, 'adapterMap');
  133. foreach($vars as $key => $value)
  134. {
  135. $results->$key = $value;
  136. }
  137. }
  138. }
  139. elseif(is_resource($results))
  140. {
  141. $type = get_resource_type($results);
  142. $str = explode(' ', $type);
  143. if(in_array($str[1], array("result", 'resultset', "recordset", "statement")))
  144. {
  145. $results = mapRecordSet($results, $str[0]);
  146. }
  147. else
  148. {
  149. $results = false;
  150. }
  151. }
  152. return $results;
  153. }
  154. function mapRecordSet($result, $type)
  155. {
  156. $classname = $type . "Adapter"; // full class name
  157. $includeFile = include_once(AMFPHP_BASE . "shared/adapters/" . $classname . ".php"); // try to load the recordset library from the sql folder
  158. if (!$includeFile) {
  159. trigger_error("The recordset filter class " . $classname . " was not found");
  160. }
  161. $recordSet = new $classname($result); // returns formatted recordset
  162. return array("columns" => $recordSet->columns, "rows" => $recordSet->rows);
  163. }
  164. ?>