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

/silex_server/cgi/amf-core/app/Gateway.php

https://github.com/silexlabs/Silex-v1
PHP | 455 lines | 220 code | 49 blank | 186 comment | 18 complexity | 8cab152c3bece7cb3e3f6296c5be4245 MD5 | raw file
  1. <?php
  2. /**
  3. * The Gateway class is the main facade for the AMFPHP remoting service.
  4. *
  5. * The developer will instantiate a new gateway instance and will interface with
  6. * the gateway instance to control how the gateway processes request, securing the
  7. * gateway with instance names and turning on additional functionality for the gateway
  8. * instance.
  9. *
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. * @copyright (c) 2003 amfphp.org
  12. * @package flashservices
  13. * @subpackage app
  14. * @author Musicman original design
  15. * @author Justin Watkins Gateway architecture, class structure, datatype io additions
  16. * @author John Cowen Datatype io additions, class structure,
  17. * @author Klaasjan Tukker Modifications, check routines, and register-framework
  18. * @version $Id: Gateway.php,v 1.45 2005/07/22 10:58:09 pmineault Exp $
  19. */
  20. /**
  21. * AMFPHP_BASE is the location of the flashservices folder in the files system.
  22. * It is used as the absolute path to load all other required system classes.
  23. */
  24. define("AMFPHP_BASE", realpath(dirname(dirname(__FILE__))) . "/");
  25. /**
  26. * required classes for the application
  27. */
  28. require_once(AMFPHP_BASE . "app/Constants.php");
  29. require_once(AMFPHP_BASE . "app/Globals.php");
  30. require_once(AMFPHP_BASE . "util/AMFObject.php");
  31. require_once(AMFPHP_BASE . "util/CharsetHandler.php");
  32. require_once(AMFPHP_BASE . "util/NetDebug.php");
  33. require_once(AMFPHP_BASE . "util/Compat.php");
  34. require_once(AMFPHP_BASE . "util/Headers.php");
  35. require_once(AMFPHP_BASE . "app/Filters.php");
  36. require_once(AMFPHP_BASE . "app/Actions.php");
  37. require_once(AMFPHP_BASE . "exception/AMFException.php");
  38. class Gateway {
  39. var $error_List;
  40. var $_looseMode = false;
  41. var $_obLogging = false;
  42. var $_charsetMethod = "none";
  43. var $_charsetPhp = "";
  44. var $_charsetSql = "";
  45. var $exec;
  46. var $filters;
  47. var $actions;
  48. var $outgoingMessagesFolder = NULL;
  49. var $incomingMessagesFolder = NULL;
  50. var $useSslFirstMethod = true;
  51. /**
  52. * The Gateway constructor method.
  53. *
  54. * The constructor method initializes the executive object so any configurations
  55. * can immediately propogate to the instance.
  56. */
  57. function Gateway() {
  58. //Include right executive for php version
  59. //Try catch are not syntactically correct in PHP4, so we can't even include
  60. //them in PHP 4.
  61. if(AMFPHP_PHP5)
  62. {
  63. //Set gloriously nice error handling
  64. include_once(AMFPHP_BASE . "app/php5Executive.php");
  65. include_once(AMFPHP_BASE . "exception/php5Exception.php");
  66. }
  67. else
  68. {
  69. //Cry
  70. include_once(AMFPHP_BASE . "app/Executive.php");
  71. include_once(AMFPHP_BASE . "exception/php4Exception.php");
  72. }
  73. $this->exec = new Executive();
  74. $this->filters = array();
  75. $this->actions = array();
  76. $this->registerFilterChain();
  77. $this->registerActionChain();
  78. }
  79. /**
  80. * Create the chain of filters
  81. * Subclass gateway and overwrite to create a custom gateway
  82. */
  83. function registerFilterChain()
  84. {
  85. //filters
  86. $this->filters['deserial'] = 'deserializationFilter';
  87. $this->filters['auth'] = 'authenticationFilter';
  88. $this->filters['batch'] = 'batchProcessFilter';
  89. $this->filters['debug'] = 'debugFilter';
  90. $this->filters['serialize'] = 'serializationFilter';
  91. }
  92. /**
  93. * Create the chain of actions
  94. * Subclass gateway and overwrite to create a custom gateway
  95. */
  96. function registerActionChain()
  97. {
  98. $this->actions['adapter'] = 'adapterAction';
  99. $this->actions['class'] = 'classLoaderAction';
  100. $this->actions['meta'] = 'metaDataAction';
  101. $this->actions['security'] = 'securityAction';
  102. $this->actions['exec'] = 'executionAction';
  103. $this->actions['ws'] = 'webServiceAction';
  104. }
  105. /**
  106. * The service method runs the gateway application. It turns the gateway 'on'. You
  107. * have to call the service method as the last line of the gateway script after all of the
  108. * gateway configuration properties have been set.
  109. *
  110. * Right now the service method also includes a very primitive debugging mode that
  111. * just dumps the raw amf input and output to files. This may change in later versions.
  112. * The debugging implementation is NOT thread safe so be aware of file corruptions that
  113. * may occur in concurrent environments.
  114. */
  115. function service() {
  116. //Set the parameters for the charset handler
  117. CharsetHandler::setMethod($this->_charsetMethod);
  118. CharsetHandler::setPhpCharset($this->_charsetPhp);
  119. CharsetHandler::setSqlCharset($this->_charsetSql);
  120. //Attempt to call charset handler to catch any uninstalled extensions
  121. $ch = new CharsetHandler('flashtophp');
  122. $ch->transliterate('?');
  123. $ch2 = new CharsetHandler('sqltophp');
  124. $ch2->transliterate('?');
  125. $GLOBALS['amfphp']['actions'] = $this->actions;
  126. if (!isset($GLOBALS['HTTP_RAW_POST_DATA'])){
  127. $GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents('php://input');
  128. }
  129. if(isset($GLOBALS["HTTP_RAW_POST_DATA"]) && $GLOBALS["HTTP_RAW_POST_DATA"] != "")
  130. {
  131. //Start NetDebug
  132. NetDebug::initialize();
  133. error_reporting($GLOBALS['amfphp']['errorLevel']);
  134. //Enable loose mode if requested
  135. if($this->_looseMode)
  136. {
  137. ob_start();
  138. }
  139. $amf = new AMFObject($GLOBALS["HTTP_RAW_POST_DATA"]); // create the amf object
  140. if($this->incomingMessagesFolder != NULL)
  141. {
  142. $mt = microtime();
  143. $pieces = explode(' ', $mt);
  144. file_put_contents($this->incomingMessagesFolder .
  145. 'in.' . $pieces[1] . '.' . substr($pieces[0], 2) . ".amf",
  146. $GLOBALS["HTTP_RAW_POST_DATA"]);
  147. }
  148. foreach($this->filters as $key => $filter)
  149. {
  150. $filter($amf); // invoke the first filter in the chain
  151. }
  152. $output = $amf->outputStream; // grab the output stream
  153. //Clear the current output buffer if requested
  154. if($this->_looseMode)
  155. {
  156. if($this->_obLogging !== FALSE)
  157. {
  158. $this->_appendRawDataToFile($this->_obLogging, ob_get_clean());
  159. }
  160. else
  161. {
  162. ob_end_clean();
  163. }
  164. }
  165. //Send content length header
  166. //Thanks to Alec Horley for pointing out the necessity
  167. //of this for FlashComm support
  168. header(AMFPHP_CONTENT_TYPE); // define the proper header
  169. header("Content-length: " . strlen($output));
  170. //Send expire header, apparently helps for SSL
  171. //Thanks to Gary Rogers for that
  172. //And also to Lucas Filippi from openAMF list
  173. //And to Robert Reinhardt who appears to be the first who
  174. //documented the bug
  175. //Finally to Gary who appears to have find a solution which works even more reliably
  176. if($this->useSslFirstMethod)
  177. {
  178. $dateStr = date("D, j M Y ") . date("H:i:s", strtotime("-2 days"));
  179. header("Expires: $dateStr GMT");
  180. header("Pragma: no-store");
  181. header("Cache-Control: no-store");
  182. }
  183. //else don't send any special headers at all
  184. if($this->outgoingMessagesFolder != NULL)
  185. {
  186. $mt = microtime();
  187. $pieces = explode(' ', $mt);
  188. file_put_contents($this->outgoingMessagesFolder .
  189. 'out.' . $pieces[1] . '.' . substr($pieces[0], 2) . ".amf", $output);
  190. }
  191. print($output); // flush the binary data
  192. }
  193. else
  194. {
  195. echo("<p>amfphp and this gateway are installed correctly. You may now connect " .
  196. "to this gateway from Flash.</p><p>Note: If you're reading an " .
  197. "old tutorial, it will tell you that you should see a download ".
  198. "window instead of this message. This confused people so this is " .
  199. "the new behaviour starting from amfphp 1.2.</p><p>" .
  200. "<a href='http://www.amfphp.org/docs'>View the amfphp documentation</p>");
  201. }
  202. }
  203. /**
  204. * Setter for the debugging directory property
  205. *
  206. * @param string $dir The directory to store debugging files.
  207. */
  208. function setDebugDirectory($dir) {
  209. $this->debugdir = $dir;
  210. }
  211. /**
  212. * Setter for error handling
  213. *
  214. * @param the error handling level
  215. */
  216. function setErrorHandling($level)
  217. {
  218. $GLOBALS['amfphp']['errorLevel'] = $level;
  219. }
  220. /**
  221. * Set an instance name for this gateway instance
  222. * Setting an instance name is used for restricted access to a gateway
  223. * If a gateway has an instance name, only service methods that have a matching instance
  224. * name can be used with the gateway
  225. *
  226. * @param string $name The instance name to bind to the gateway instance, the default is <i>Instance1</i>
  227. */
  228. function setInstanceName($value = "Instance1") {
  229. $GLOBALS['amfphp']['instanceName'] = $value;
  230. }
  231. /**
  232. * Sets the base path for loading service methods.
  233. *
  234. * Call this method to define the directory to look for service classes in.
  235. * Relative or full paths are acceptable
  236. *
  237. * @param string $path The path the the service class directory
  238. */
  239. function setBaseClassPath($value) {
  240. $path = realpath($value . '/') . '/';
  241. $GLOBALS['amfphp']['classPath'] = $path;
  242. }
  243. /**
  244. * Sets the base path for loading service methods.
  245. *
  246. * Call this method to define the directory to look for service classes in.
  247. * Relative or full paths are acceptable
  248. *
  249. * @param string $path The path the the service class directory
  250. */
  251. function setBaseCustomMappingsPath($value) {
  252. $path = realpath($value . '/') . '/';
  253. $GLOBALS['amfphp']['customMappingsPath'] = $path;
  254. }
  255. /**
  256. * Add a class mapping for adapters
  257. */
  258. function addAdapterMapping($key, $value)
  259. {
  260. $GLOBALS['amfphp']['adapterMappings'][$key] = $value;
  261. }
  262. function setCustomIncomingClassMappings($value)
  263. {
  264. $GLOBALS['amfphp']['incomingClassMappings'] = $value;
  265. }
  266. function setCustomOutgoingClassMappings($value)
  267. {
  268. $GLOBALS['amfphp']['outgoingClassMappings'] = $value;
  269. }
  270. /**
  271. * Sets the loose mode. This will enable outbut buffering
  272. * And flushing and set error_reporting to 0. The point is if set to true, a few
  273. * of the usual NetConnection.BadVersion error should disappear
  274. * Like if you try to echo directly from your function, if you are issued a
  275. * warning and such. Errors should still be logged to the error log though.
  276. *
  277. * @example In gateway.php, before $gateway->service(), use $gateway->setLooseMode(true)
  278. * @param bool $mode Enable or disable loose mode
  279. */
  280. function setLooseMode($paramLoose = true) {
  281. $this->_looseMode = $paramLoose;
  282. }
  283. /**
  284. * Sets the charset handler.
  285. * The charset handler handles reencoding from and to a specific charset
  286. * for PHP and SQL resources.
  287. *
  288. * @param $method The method used for reencoding, either "none", "iconv" or "runtime"
  289. * @param $php The internal encoding that is assumed for PHP (typically ISO-8859-1)
  290. * @param $sql The internal encoding that is assumed for SQL resources
  291. */
  292. function setCharsetHandler($method = "none", $php, $sql) {
  293. $this->_charsetMethod = $method;
  294. $this->_charsetPhp = $php;
  295. $this->_charsetSql = $sql;
  296. }
  297. /**
  298. * Set output buffering logging. If set to a valid, writeable location, AND
  299. * loss mode is set to true, this will log all calls to echo, print, printf, any whitespace
  300. * in your class outside of < ? ? > etc. to a file. This gives you a very simple
  301. * way to debug your files. Note that this is not thread-safe and obLogging should
  302. * most likely be set to false in a production environment
  303. *
  304. * @example In gateway.php, before $gateway->service(), use $gateway->setObLogging("/tmp/oblog.txt")
  305. * @param string $path The path of the log file to use
  306. */
  307. function setObLogging($value = FALSE) {
  308. $this->_obLogging = $paramOb;
  309. }
  310. /**
  311. * setWebServiceHandler is a method to choose the SOAP package to use for
  312. * web service calls. Should be set to php5 (SoapClient), pear or nusoap
  313. *
  314. * @param string $handler Which service handler to use
  315. */
  316. function setWebServiceHandler($value = 'php5') {
  317. $GLOBALS['amfphp']['webServiceMethod'] = strtolower($value);
  318. }
  319. /**
  320. * disableStandalonePlayer will exit the script (die) if the standalone
  321. * player is sees in the User-Agent signature
  322. *
  323. * @param bool $bool Whether to disable the Standalone player
  324. */
  325. function disableStandalonePlayer($value = true) {
  326. if($value && $_SERVER['HTTP_USER_AGENT'] == "Shockwave Flash")
  327. {
  328. trigger_error("Standalone Flash player disabled", E_USER_ERROR);
  329. die();
  330. }
  331. }
  332. /**
  333. * disableServiceDescription will stop the gateway for sending service
  334. * descriptions to the IDE's service browser
  335. *
  336. * @param bool $bool Whether to disable service description
  337. */
  338. function disableServiceDescription($value = true) {
  339. $GLOBALS['amfphp']['disableDescribeService'] = $value;
  340. }
  341. /**
  342. * disableTrace will ignore any calls to NetDebug::trace
  343. *
  344. * @param bool $bool Whether to disable tracing
  345. */
  346. function disableTrace($value = true) {
  347. $GLOBALS['amfphp']['disableTrace'] = $value;
  348. }
  349. /**
  350. * disableDebug will stop the debug headers from being sent
  351. * (independant of trace)
  352. *
  353. * @param bool $bool Whether to disable debug headers
  354. */
  355. function disableDebug($value = true) {
  356. $GLOBALS['amfphp']['disableDebug'] = $value;
  357. }
  358. /**
  359. * Log incoming messages to the specified folder
  360. */
  361. function logIncomingMessages($folder = NULL)
  362. {
  363. $this->incomingMessagesFolder = realpath($folder) . '/';
  364. }
  365. /**
  366. * Log outgoing messages to the specified folder
  367. */
  368. function logOutgoingMessages($folder = NULL)
  369. {
  370. $this->outgoingMessagesFolder = realpath($folder) . '/';
  371. }
  372. /**
  373. * Dumps data to a file
  374. *
  375. * @param string $filepath The location of the dump file
  376. * @param string $data The data to insert into the dump file
  377. */
  378. function _saveRawDataToFile($filepath, $data) {
  379. if (!$handle = fopen($filepath, 'w')) {
  380. exit;
  381. }
  382. if (!fwrite($handle, $data)) {
  383. exit;
  384. }
  385. fclose($handle);
  386. }
  387. /**
  388. * Appends data to a file
  389. *
  390. * @param string $filepath The location of the dump file
  391. * @param string $data The data to append to the dump file
  392. */
  393. function _appendRawDataToFile($filepath, $data) {
  394. $handle = fopen($filepath, 'a');
  395. fwrite($handle, $data);
  396. fclose($handle);
  397. }
  398. /**
  399. * Sets the gateway to use the second ssl method
  400. */
  401. function useSslSecondMethod()
  402. {
  403. $this->useSslFirstMethod = false;
  404. }
  405. }
  406. ?>