PageRenderTime 70ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/cache/frontend/prod/config/config_core_compile.yml.php

https://bitbucket.org/vasyabigi/underconstruction
PHP | 4063 lines | 3994 code | 67 blank | 2 comment | 443 complexity | c16621464255fd6c3c6c28760344d955 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, AGPL-3.0
  1. <?php
  2. // auto-generated by sfCompileConfigHandler
  3. // date: 2009/11/25 13:31:30
  4. class sfAutoload
  5. {
  6. static protected
  7. $freshCache = false,
  8. $instance = null;
  9. protected
  10. $overriden = array(),
  11. $classes = array();
  12. protected function __construct()
  13. {
  14. }
  15. static public function getInstance()
  16. {
  17. if (!isset(self::$instance))
  18. {
  19. self::$instance = new sfAutoload();
  20. }
  21. return self::$instance;
  22. }
  23. static public function register()
  24. {
  25. ini_set('unserialize_callback_func', 'spl_autoload_call');
  26. if (false === spl_autoload_register(array(self::getInstance(), 'autoload')))
  27. {
  28. throw new sfException(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
  29. }
  30. }
  31. static public function unregister()
  32. {
  33. spl_autoload_unregister(array(self::getInstance(), 'autoload'));
  34. }
  35. public function setClassPath($class, $path)
  36. {
  37. $this->overriden[$class] = $path;
  38. $this->classes[$class] = $path;
  39. }
  40. public function getClassPath($class)
  41. {
  42. return isset($this->classes[$class]) ? $this->classes[$class] : null;
  43. }
  44. public function reloadClasses($force = false)
  45. {
  46. if (self::$freshCache)
  47. {
  48. return;
  49. }
  50. $configuration = sfProjectConfiguration::getActive();
  51. if (!$configuration || !$configuration instanceof sfApplicationConfiguration)
  52. {
  53. return;
  54. }
  55. self::$freshCache = true;
  56. if (file_exists($configuration->getConfigCache()->getCacheName('config/autoload.yml')))
  57. {
  58. self::$freshCache = false;
  59. if ($force)
  60. {
  61. unlink($configuration->getConfigCache()->getCacheName('config/autoload.yml'));
  62. }
  63. }
  64. $file = $configuration->getConfigCache()->checkConfig('config/autoload.yml');
  65. $this->classes = include($file);
  66. foreach ($this->overriden as $class => $path)
  67. {
  68. $this->classes[$class] = $path;
  69. }
  70. }
  71. public function autoload($class)
  72. {
  73. if (!$this->classes)
  74. {
  75. self::reloadClasses();
  76. }
  77. return self::loadClass($class);
  78. }
  79. public function autoloadAgain($class)
  80. {
  81. self::reloadClasses(true);
  82. return self::loadClass($class);
  83. }
  84. public function loadClass($class)
  85. {
  86. if (class_exists($class, false) || interface_exists($class, false))
  87. {
  88. return true;
  89. }
  90. if (isset($this->classes[$class]))
  91. {
  92. require($this->classes[$class]);
  93. return true;
  94. }
  95. if (sfContext::hasInstance() && ($module = sfContext::getInstance()->getModuleName()) && isset($this->classes[$module.'/'.$class]))
  96. {
  97. require($this->classes[$module.'/'.$class]);
  98. return true;
  99. }
  100. return false;
  101. }
  102. }
  103. abstract class sfComponent
  104. {
  105. protected
  106. $moduleName = '',
  107. $actionName = '',
  108. $context = null,
  109. $dispatcher = null,
  110. $request = null,
  111. $response = null,
  112. $varHolder = null,
  113. $requestParameterHolder = null;
  114. public function __construct($context, $moduleName, $actionName)
  115. {
  116. $this->initialize($context, $moduleName, $actionName);
  117. }
  118. public function initialize($context, $moduleName, $actionName)
  119. {
  120. $this->moduleName = $moduleName;
  121. $this->actionName = $actionName;
  122. $this->context = $context;
  123. $this->dispatcher = $context->getEventDispatcher();
  124. $this->varHolder = new sfParameterHolder();
  125. $this->request = $context->getRequest();
  126. $this->response = $context->getResponse();
  127. $this->requestParameterHolder = $this->request->getParameterHolder();
  128. }
  129. abstract function execute($request);
  130. public function getModuleName()
  131. {
  132. return $this->moduleName;
  133. }
  134. public function getActionName()
  135. {
  136. return $this->actionName;
  137. }
  138. public final function getContext()
  139. {
  140. return $this->context;
  141. }
  142. public final function getLogger()
  143. {
  144. return $this->context->getLogger();
  145. }
  146. public function logMessage($message, $priority = 'info')
  147. {
  148. if (sfConfig::get('sf_logging_enabled'))
  149. {
  150. $this->dispatcher->notify(new sfEvent($this, 'application.log', array($message, 'priority' => constant('sfLogger::'.strtoupper($priority)))));
  151. }
  152. }
  153. public function debugMessage($message)
  154. {
  155. if (sfConfig::get('sf_web_debug') && sfConfig::get('sf_logging_enabled'))
  156. {
  157. $this->dispatcher->notify(new sfEvent(null, 'application.log', array('This function is deprecated in favor of the logMessage function.', 'priority' => sfLogger::ERR)));
  158. }
  159. }
  160. public function getRequestParameter($name, $default = null)
  161. {
  162. return $this->requestParameterHolder->get($name, $default);
  163. }
  164. public function hasRequestParameter($name)
  165. {
  166. return $this->requestParameterHolder->has($name);
  167. }
  168. public function getRequest()
  169. {
  170. return $this->request;
  171. }
  172. public function getResponse()
  173. {
  174. return $this->response;
  175. }
  176. public function getController()
  177. {
  178. return $this->context->getController();
  179. }
  180. public function generateUrl($route, $params = array(), $absolute = false)
  181. {
  182. return $this->context->getRouting()->generate($route, $params, $absolute);
  183. }
  184. public function getUser()
  185. {
  186. return $this->context->getUser();
  187. }
  188. public function setVar($name, $value, $safe = false)
  189. {
  190. $this->varHolder->set($name, $safe ? new sfOutputEscaperSafe($value) : $value);
  191. }
  192. public function getVar($name)
  193. {
  194. return $this->varHolder->get($name);
  195. }
  196. public function getVarHolder()
  197. {
  198. return $this->varHolder;
  199. }
  200. public function __set($key, $value)
  201. {
  202. return $this->varHolder->setByRef($key, $value);
  203. }
  204. public function & __get($key)
  205. {
  206. return $this->varHolder->get($key);
  207. }
  208. public function __isset($name)
  209. {
  210. return $this->varHolder->has($name);
  211. }
  212. public function __unset($name)
  213. {
  214. $this->varHolder->remove($name);
  215. }
  216. public function __call($method, $arguments)
  217. {
  218. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'component.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  219. if (!$event->isProcessed())
  220. {
  221. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  222. }
  223. return $event->getReturnValue();
  224. }
  225. }
  226. abstract class sfAction extends sfComponent
  227. {
  228. protected
  229. $security = array();
  230. public function initialize($context, $moduleName, $actionName)
  231. {
  232. parent::initialize($context, $moduleName, $actionName);
  233. if ($file = $context->getConfigCache()->checkConfig('modules/'.$this->getModuleName().'/config/security.yml', true))
  234. {
  235. require($file);
  236. }
  237. }
  238. public function preExecute()
  239. {
  240. }
  241. public function postExecute()
  242. {
  243. }
  244. public function forward404($message = null)
  245. {
  246. throw new sfError404Exception($this->get404Message($message));
  247. }
  248. public function forward404Unless($condition, $message = null)
  249. {
  250. if (!$condition)
  251. {
  252. throw new sfError404Exception($this->get404Message($message));
  253. }
  254. }
  255. public function forward404If($condition, $message = null)
  256. {
  257. if ($condition)
  258. {
  259. throw new sfError404Exception($this->get404Message($message));
  260. }
  261. }
  262. public function redirect404()
  263. {
  264. return $this->redirect('/'.sfConfig::get('sf_error_404_module').'/'.sfConfig::get('sf_error_404_action'));
  265. }
  266. public function forward($module, $action)
  267. {
  268. if (sfConfig::get('sf_logging_enabled'))
  269. {
  270. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Forward to action "%s/%s"', $module, $action))));
  271. }
  272. $this->getController()->forward($module, $action);
  273. throw new sfStopException();
  274. }
  275. public function forwardIf($condition, $module, $action)
  276. {
  277. if ($condition)
  278. {
  279. $this->forward($module, $action);
  280. }
  281. }
  282. public function forwardUnless($condition, $module, $action)
  283. {
  284. if (!$condition)
  285. {
  286. $this->forward($module, $action);
  287. }
  288. }
  289. public function redirect($url, $statusCode = 302)
  290. {
  291. $this->getController()->redirect($url, 0, $statusCode);
  292. throw new sfStopException();
  293. }
  294. public function redirectIf($condition, $url, $statusCode = 302)
  295. {
  296. if ($condition)
  297. {
  298. $this->redirect($url, $statusCode);
  299. }
  300. }
  301. public function redirectUnless($condition, $url, $statusCode = 302)
  302. {
  303. if (!$condition)
  304. {
  305. $this->redirect($url, $statusCode);
  306. }
  307. }
  308. public function renderText($text)
  309. {
  310. $this->getResponse()->setContent($this->getResponse()->getContent().$text);
  311. return sfView::NONE;
  312. }
  313. public function getPartial($templateName, $vars = null)
  314. {
  315. $this->getContext()->getConfiguration()->loadHelpers('Partial');
  316. $vars = !is_null($vars) ? $vars : $this->varHolder->getAll();
  317. return get_partial($templateName, $vars);
  318. }
  319. public function renderPartial($templateName, $vars = null)
  320. {
  321. return $this->renderText($this->getPartial($templateName, $vars));
  322. }
  323. public function getComponent($moduleName, $componentName, $vars = null)
  324. {
  325. $this->getContext()->getConfiguration()->loadHelpers('Partial');
  326. $vars = !is_null($vars) ? $vars : $this->varHolder->getAll();
  327. return get_component($moduleName, $componentName, $vars);
  328. }
  329. public function renderComponent($moduleName, $componentName, $vars = null)
  330. {
  331. return $this->renderText($this->getComponent($moduleName, $componentName, $vars));
  332. }
  333. public function getDefaultView()
  334. {
  335. if (!sfConfig::get('sf_compat_10'))
  336. {
  337. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  338. }
  339. return sfView::INPUT;
  340. }
  341. public function handleError()
  342. {
  343. if (!sfConfig::get('sf_compat_10'))
  344. {
  345. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  346. }
  347. return sfView::ERROR;
  348. }
  349. public function validate()
  350. {
  351. if (!sfConfig::get('sf_compat_10'))
  352. {
  353. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  354. }
  355. return true;
  356. }
  357. public function getSecurityConfiguration()
  358. {
  359. return $this->security;
  360. }
  361. public function setSecurityConfiguration($security)
  362. {
  363. $this->security = $security;
  364. }
  365. public function isSecure()
  366. {
  367. $actionName = strtolower($this->getActionName());
  368. if (isset($this->security[$actionName]['is_secure']))
  369. {
  370. return $this->security[$actionName]['is_secure'];
  371. }
  372. if (isset($this->security['all']['is_secure']))
  373. {
  374. return $this->security['all']['is_secure'];
  375. }
  376. return false;
  377. }
  378. public function getCredential()
  379. {
  380. $actionName = strtolower($this->getActionName());
  381. if (isset($this->security[$actionName]['credentials']))
  382. {
  383. $credentials = $this->security[$actionName]['credentials'];
  384. }
  385. else if (isset($this->security['all']['credentials']))
  386. {
  387. $credentials = $this->security['all']['credentials'];
  388. }
  389. else
  390. {
  391. $credentials = null;
  392. }
  393. return $credentials;
  394. }
  395. public function setTemplate($name, $module = null)
  396. {
  397. if (sfConfig::get('sf_logging_enabled'))
  398. {
  399. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Change template to "%s/%s"', is_null($module) ? 'CURRENT' : $module, $name))));
  400. }
  401. if (!is_null($module))
  402. {
  403. $name = sfConfig::get('sf_app_dir').'/modules/'.$module.'/templates/'.$name;
  404. }
  405. sfConfig::set('symfony.view.'.$this->getModuleName().'_'.$this->getActionName().'_template', $name);
  406. }
  407. public function getTemplate()
  408. {
  409. return sfConfig::get('symfony.view.'.$this->getModuleName().'_'.$this->getActionName().'_template');
  410. }
  411. public function setLayout($name)
  412. {
  413. if (sfConfig::get('sf_logging_enabled'))
  414. {
  415. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Change layout to "%s"', $name))));
  416. }
  417. sfConfig::set('symfony.view.'.$this->getModuleName().'_'.$this->getActionName().'_layout', $name);
  418. }
  419. public function getLayout()
  420. {
  421. return sfConfig::get('symfony.view.'.$this->getModuleName().'_'.$this->getActionName().'_layout');
  422. }
  423. public function setViewClass($class)
  424. {
  425. sfConfig::set('mod_'.strtolower($this->getModuleName()).'_view_class', $class);
  426. }
  427. public function getRoute()
  428. {
  429. return $this->getRequest()->getAttribute('sf_route');
  430. }
  431. protected function get404Message($message = null)
  432. {
  433. return is_null($message) ? sprintf('This request has been forwarded to a 404 error page by the action "%s/%s".', $this->getModuleName(), $this->getActionName()) : $message;
  434. }
  435. }
  436. abstract class sfActions extends sfAction
  437. {
  438. public function execute($request)
  439. {
  440. $actionToRun = 'execute'.ucfirst($this->getActionName());
  441. if ($actionToRun === 'execute')
  442. {
  443. throw new sfInitializationException(sprintf('sfAction initialization failed for module "%s". There was no action given.', $this->getModuleName()));
  444. }
  445. if (!is_callable(array($this, $actionToRun)))
  446. {
  447. throw new sfInitializationException(sprintf('sfAction initialization failed for module "%s", action "%s". You must create a "%s" method.', $this->getModuleName(), $this->getActionName(), $actionToRun));
  448. }
  449. if (sfConfig::get('sf_logging_enabled'))
  450. {
  451. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Call "%s->%s()"', get_class($this), $actionToRun))));
  452. }
  453. return $this->$actionToRun($request);
  454. }
  455. }
  456. class sfActionStack
  457. {
  458. protected
  459. $stack = array();
  460. public function addEntry($moduleName, $actionName, $actionInstance)
  461. {
  462. $actionEntry = new sfActionStackEntry($moduleName, $actionName, $actionInstance);
  463. $this->stack[] = $actionEntry;
  464. return $actionEntry;
  465. }
  466. public function getEntry($index)
  467. {
  468. $retval = null;
  469. if ($index > -1 && $index < count($this->stack))
  470. {
  471. $retval = $this->stack[$index];
  472. }
  473. return $retval;
  474. }
  475. public function popEntry()
  476. {
  477. return array_pop($this->stack);
  478. }
  479. public function getFirstEntry()
  480. {
  481. $retval = null;
  482. if (isset($this->stack[0]))
  483. {
  484. $retval = $this->stack[0];
  485. }
  486. return $retval;
  487. }
  488. public function getLastEntry()
  489. {
  490. $count = count($this->stack);
  491. $retval = null;
  492. if (isset($this->stack[0]))
  493. {
  494. $retval = $this->stack[$count - 1];
  495. }
  496. return $retval;
  497. }
  498. public function getSize()
  499. {
  500. return count($this->stack);
  501. }
  502. }
  503. class sfActionStackEntry
  504. {
  505. protected
  506. $actionInstance = null,
  507. $actionName = null,
  508. $moduleName = null,
  509. $presentation = null;
  510. public function __construct($moduleName, $actionName, $actionInstance)
  511. {
  512. $this->actionName = $actionName;
  513. $this->actionInstance = $actionInstance;
  514. $this->moduleName = $moduleName;
  515. }
  516. public function getActionName()
  517. {
  518. return $this->actionName;
  519. }
  520. public function getActionInstance()
  521. {
  522. return $this->actionInstance;
  523. }
  524. public function getModuleName()
  525. {
  526. return $this->moduleName;
  527. }
  528. public function & getPresentation()
  529. {
  530. return $this->presentation;
  531. }
  532. public function setPresentation(&$presentation)
  533. {
  534. $this->presentation =& $presentation;
  535. }
  536. }
  537. class sfLoader
  538. {
  539. static public function getHelperDirs($moduleName = '')
  540. {
  541. $configuration = sfProjectConfiguration::getActive();
  542. $configuration->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array('The sfLoader::getHelperDirs() method is deprecated. Please use the same method from sfApplicationConfiguration.', 'priority' => sfLogger::ERR)));
  543. return $configuration->getHelperDirs($moduleName);
  544. }
  545. static public function loadHelpers($helpers, $moduleName = '')
  546. {
  547. $configuration = sfProjectConfiguration::getActive();
  548. $configuration->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array('The sfLoader::loadHelpers() method is deprecated. Please use the same method from sfApplicationConfiguration.', 'priority' => sfLogger::ERR)));
  549. return $configuration->loadHelpers($helpers, $moduleName);
  550. }
  551. }
  552. abstract class sfController
  553. {
  554. protected
  555. $context = null,
  556. $dispatcher = null,
  557. $controllerClasses = array(),
  558. $maxForwards = 5,
  559. $renderMode = sfView::RENDER_CLIENT;
  560. public function __construct($context)
  561. {
  562. $this->initialize($context);
  563. }
  564. public function initialize($context)
  565. {
  566. $this->context = $context;
  567. $this->dispatcher = $context->getEventDispatcher();
  568. $this->maxForwards = sfConfig::get('sf_max_forwards', $this->maxForwards);
  569. }
  570. public function componentExists($moduleName, $componentName)
  571. {
  572. return $this->controllerExists($moduleName, $componentName, 'component', false);
  573. }
  574. public function actionExists($moduleName, $actionName)
  575. {
  576. return $this->controllerExists($moduleName, $actionName, 'action', false);
  577. }
  578. protected function controllerExists($moduleName, $controllerName, $extension, $throwExceptions)
  579. {
  580. $dirs = $this->context->getConfiguration()->getControllerDirs($moduleName);
  581. foreach ($dirs as $dir => $checkEnabled)
  582. {
  583. if ($checkEnabled && !in_array($moduleName, sfConfig::get('sf_enabled_modules')) && is_readable($dir))
  584. {
  585. throw new sfConfigurationException(sprintf('The module "%s" is not enabled.', $moduleName));
  586. }
  587. $classFile = strtolower($extension);
  588. $classSuffix = ucfirst(strtolower($extension));
  589. $file = $dir.'/'.$controllerName.$classSuffix.'.class.php';
  590. if (is_readable($file))
  591. {
  592. require_once($file);
  593. $this->controllerClasses[$moduleName.'_'.$controllerName.'_'.$classSuffix] = $controllerName.$classSuffix;
  594. return true;
  595. }
  596. $module_file = $dir.'/'.$classFile.'s.class.php';
  597. if (is_readable($module_file))
  598. {
  599. require_once($module_file);
  600. if (!class_exists($moduleName.$classSuffix.'s', false))
  601. {
  602. if ($throwExceptions)
  603. {
  604. throw new sfControllerException(sprintf('There is no "%s" class in your action file "%s".', $moduleName.$classSuffix.'s', $module_file));
  605. }
  606. return false;
  607. }
  608. if (!in_array('execute'.ucfirst($controllerName), get_class_methods($moduleName.$classSuffix.'s')))
  609. {
  610. if ($throwExceptions)
  611. {
  612. throw new sfControllerException(sprintf('There is no "%s" method in your action class "%s".', 'execute'.ucfirst($controllerName), $moduleName.$classSuffix.'s'));
  613. }
  614. return false;
  615. }
  616. $this->controllerClasses[$moduleName.'_'.$controllerName.'_'.$classSuffix] = $moduleName.$classSuffix.'s';
  617. return true;
  618. }
  619. }
  620. if ($throwExceptions && sfConfig::get('sf_debug'))
  621. {
  622. $dirs = array_keys($dirs);
  623. foreach ($dirs as &$dir)
  624. {
  625. $dir = str_replace(sfConfig::get('sf_root_dir'), '%SF_ROOT_DIR%', $dir);
  626. }
  627. throw new sfControllerException(sprintf('Controller "%s/%s" does not exist in: %s.', $moduleName, $controllerName, implode(', ', $dirs)));
  628. }
  629. return false;
  630. }
  631. public function forward($moduleName, $actionName)
  632. {
  633. $moduleName = preg_replace('/[^a-z0-9_]+/i', '', $moduleName);
  634. $actionName = preg_replace('/[^a-z0-9_]+/i', '', $actionName);
  635. if ($this->getActionStack()->getSize() >= $this->maxForwards)
  636. {
  637. throw new sfForwardException(sprintf('Too many forwards have been detected for this request (> %d).', $this->maxForwards));
  638. }
  639. $this->context->getConfigCache()->import('modules/'.$moduleName.'/config/generator.yml', false, true);
  640. if (!$this->actionExists($moduleName, $actionName))
  641. {
  642. if (sfConfig::get('sf_logging_enabled'))
  643. {
  644. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Action "%s/%s" does not exist', $moduleName, $actionName))));
  645. }
  646. throw new sfError404Exception(sprintf('Action "%s/%s" does not exist.', $moduleName, $actionName));
  647. }
  648. $actionInstance = $this->getAction($moduleName, $actionName);
  649. $this->getActionStack()->addEntry($moduleName, $actionName, $actionInstance);
  650. require($this->context->getConfigCache()->checkConfig('modules/'.$moduleName.'/config/module.yml'));
  651. if ($this->getActionStack()->getSize() == 1 && sfConfig::get('mod_'.strtolower($moduleName).'_is_internal') && !sfConfig::get('sf_test'))
  652. {
  653. throw new sfConfigurationException(sprintf('Action "%s" from module "%s" cannot be called directly.', $actionName, $moduleName));
  654. }
  655. if (sfConfig::get('mod_'.strtolower($moduleName).'_enabled'))
  656. {
  657. $moduleConfig = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/config/config.php';
  658. if (is_readable($moduleConfig))
  659. {
  660. require_once($moduleConfig);
  661. }
  662. $filterChain = new sfFilterChain();
  663. $filterChain->loadConfiguration($actionInstance);
  664. $this->context->getEventDispatcher()->notify(new sfEvent($this, 'controller.change_action', array('module' => $moduleName, 'action' => $actionName)));
  665. if ($moduleName == sfConfig::get('sf_error_404_module') && $actionName == sfConfig::get('sf_error_404_action'))
  666. {
  667. $this->context->getResponse()->setStatusCode(404);
  668. $this->context->getResponse()->setHttpHeader('Status', '404 Not Found');
  669. $this->dispatcher->notify(new sfEvent($this, 'controller.page_not_found', array('module' => $moduleName, 'action' => $actionName)));
  670. }
  671. $filterChain->execute();
  672. }
  673. else
  674. {
  675. $moduleName = sfConfig::get('sf_module_disabled_module');
  676. $actionName = sfConfig::get('sf_module_disabled_action');
  677. if (!$this->actionExists($moduleName, $actionName))
  678. {
  679. throw new sfConfigurationException(sprintf('Invalid configuration settings: [sf_module_disabled_module] "%s", [sf_module_disabled_action] "%s".', $moduleName, $actionName));
  680. }
  681. $this->forward($moduleName, $actionName);
  682. }
  683. }
  684. public function getAction($moduleName, $actionName)
  685. {
  686. return $this->getController($moduleName, $actionName, 'action');
  687. }
  688. public function getComponent($moduleName, $componentName)
  689. {
  690. return $this->getController($moduleName, $componentName, 'component');
  691. }
  692. protected function getController($moduleName, $controllerName, $extension)
  693. {
  694. $classSuffix = ucfirst(strtolower($extension));
  695. if (!isset($this->controllerClasses[$moduleName.'_'.$controllerName.'_'.$classSuffix]))
  696. {
  697. $this->controllerExists($moduleName, $controllerName, $extension, true);
  698. }
  699. $class = $this->controllerClasses[$moduleName.'_'.$controllerName.'_'.$classSuffix];
  700. $moduleClass = $moduleName.'_'.$class;
  701. if (class_exists($moduleClass, false))
  702. {
  703. $class = $moduleClass;
  704. }
  705. return new $class($this->context, $moduleName, $controllerName);
  706. }
  707. public function getActionStack()
  708. {
  709. return $this->context->getActionStack();
  710. }
  711. public function getRenderMode()
  712. {
  713. return $this->renderMode;
  714. }
  715. public function getView($moduleName, $actionName, $viewName)
  716. {
  717. $file = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/view/'.$actionName.$viewName.'View.class.php';
  718. if (is_readable($file))
  719. {
  720. require_once($file);
  721. $class = $actionName.$viewName.'View';
  722. $moduleClass = $moduleName.'_'.$class;
  723. if (class_exists($moduleClass, false))
  724. {
  725. $class = $moduleClass;
  726. }
  727. }
  728. else
  729. {
  730. $class = sfConfig::get('mod_'.strtolower($moduleName).'_view_class', 'sfPHP').'View';
  731. }
  732. return new $class($this->context, $moduleName, $actionName, $viewName);
  733. }
  734. public function sendEmail($module, $action)
  735. {
  736. if (sfConfig::get('sf_logging_enabled'))
  737. {
  738. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('sendEmail method is deprecated', 'priority' => sfLogger::ERR)));
  739. }
  740. return $this->getPresentationFor($module, $action, 'sfMail');
  741. }
  742. public function getPresentationFor($module, $action, $viewName = null)
  743. {
  744. if (sfConfig::get('sf_logging_enabled'))
  745. {
  746. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Get presentation for action "%s/%s" (view class: "%s")', $module, $action, $viewName))));
  747. }
  748. $renderMode = $this->getRenderMode();
  749. $this->setRenderMode(sfView::RENDER_VAR);
  750. $actionStack = $this->getActionStack();
  751. $index = $actionStack->getSize();
  752. if ($viewName)
  753. {
  754. $currentViewName = sfConfig::get('mod_'.strtolower($module).'_view_class');
  755. sfConfig::set('mod_'.strtolower($module).'_view_class', $viewName);
  756. }
  757. try
  758. {
  759. $this->forward($module, $action);
  760. }
  761. catch (Exception $e)
  762. {
  763. $this->setRenderMode($renderMode);
  764. if ($viewName)
  765. {
  766. sfConfig::set('mod_'.strtolower($module).'_view_class', $currentViewName);
  767. }
  768. throw $e;
  769. }
  770. $actionEntry = $actionStack->getEntry($index);
  771. $presentation =& $actionEntry->getPresentation();
  772. $this->setRenderMode($renderMode);
  773. $nb = $actionStack->getSize() - $index;
  774. while ($nb-- > 0)
  775. {
  776. $actionEntry = $actionStack->popEntry();
  777. if ($actionEntry->getModuleName() == sfConfig::get('sf_login_module') && $actionEntry->getActionName() == sfConfig::get('sf_login_action'))
  778. {
  779. throw new sfException('Your action is secured, but the user is not authenticated.');
  780. }
  781. else if ($actionEntry->getModuleName() == sfConfig::get('sf_secure_module') && $actionEntry->getActionName() == sfConfig::get('sf_secure_action'))
  782. {
  783. throw new sfException('Your action is secured, but the user does not have access.');
  784. }
  785. }
  786. if ($viewName)
  787. {
  788. sfConfig::set('mod_'.strtolower($module).'_view_class', $currentViewName);
  789. }
  790. return $presentation;
  791. }
  792. public function setRenderMode($mode)
  793. {
  794. if ($mode == sfView::RENDER_CLIENT || $mode == sfView::RENDER_VAR || $mode == sfView::RENDER_NONE)
  795. {
  796. $this->renderMode = $mode;
  797. return;
  798. }
  799. throw new sfRenderException(sprintf('Invalid rendering mode: %s.', $mode));
  800. }
  801. public function inCLI()
  802. {
  803. return 0 == strncasecmp(PHP_SAPI, 'cli', 3);
  804. }
  805. public function __call($method, $arguments)
  806. {
  807. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'controller.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  808. if (!$event->isProcessed())
  809. {
  810. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  811. }
  812. return $event->getReturnValue();
  813. }
  814. }
  815. class sfDatabaseManager
  816. {
  817. protected
  818. $configuration = null,
  819. $databases = array();
  820. public function __construct(sfProjectConfiguration $configuration, $options = array())
  821. {
  822. $this->initialize($configuration);
  823. if (!isset($options['auto_shutdown']) || $options['auto_shutdown'])
  824. {
  825. register_shutdown_function(array($this, 'shutdown'));
  826. }
  827. }
  828. public function initialize(sfProjectConfiguration $configuration)
  829. {
  830. $this->configuration = $configuration;
  831. $this->loadConfiguration();
  832. }
  833. public function loadConfiguration()
  834. {
  835. if ($this->configuration instanceof sfApplicationConfiguration)
  836. {
  837. $databases = include($this->configuration->getConfigCache()->checkConfig('config/databases.yml'));
  838. }
  839. else
  840. {
  841. $configHandler = new sfDatabaseConfigHandler();
  842. $databases = $configHandler->evaluate(array($this->configuration->getRootDir().'/config/databases.yml'));
  843. }
  844. foreach ($databases as $name => $database)
  845. {
  846. $this->setDatabase($name, $database);
  847. }
  848. }
  849. public function setDatabase($name, sfDatabase $database)
  850. {
  851. $this->databases[$name] = $database;
  852. }
  853. public function getDatabase($name = 'default')
  854. {
  855. if (isset($this->databases[$name]))
  856. {
  857. return $this->databases[$name];
  858. }
  859. throw new sfDatabaseException(sprintf('Database "%s" does not exist.', $name));
  860. }
  861. public function getNames()
  862. {
  863. return array_keys($this->databases);
  864. }
  865. public function shutdown()
  866. {
  867. foreach ($this->databases as $database)
  868. {
  869. $database->shutdown();
  870. }
  871. }
  872. }
  873. class sfEvent implements ArrayAccess
  874. {
  875. protected
  876. $value = null,
  877. $processed = false,
  878. $subject = null,
  879. $name = '',
  880. $parameters = null;
  881. public function __construct($subject, $name, $parameters = array())
  882. {
  883. $this->subject = $subject;
  884. $this->name = $name;
  885. $this->parameters = $parameters;
  886. }
  887. public function getSubject()
  888. {
  889. return $this->subject;
  890. }
  891. public function getName()
  892. {
  893. return $this->name;
  894. }
  895. public function setReturnValue($value)
  896. {
  897. $this->value = $value;
  898. }
  899. public function getReturnValue()
  900. {
  901. return $this->value;
  902. }
  903. public function setProcessed($processed)
  904. {
  905. $this->processed = (boolean) $processed;
  906. }
  907. public function isProcessed()
  908. {
  909. return $this->processed;
  910. }
  911. public function getParameters()
  912. {
  913. return $this->parameters;
  914. }
  915. public function offsetExists($name)
  916. {
  917. return array_key_exists($name, $this->parameters);
  918. }
  919. public function offsetGet($name)
  920. {
  921. if (!array_key_exists($name, $this->parameters))
  922. {
  923. throw new InvalidArgumentException(sprintf('The event "%s" has no "%s" parameter.', $this->name, $name));
  924. }
  925. return $this->parameters[$name];
  926. }
  927. public function offsetSet($name, $value)
  928. {
  929. $this->parameters[$name] = $value;
  930. }
  931. public function offsetUnset($name)
  932. {
  933. unset($this->parameters[$name]);
  934. }
  935. }
  936. abstract class sfFilter
  937. {
  938. protected
  939. $parameterHolder = null,
  940. $context = null;
  941. public static
  942. $filterCalled = array();
  943. public function __construct($context, $parameters = array())
  944. {
  945. $this->initialize($context, $parameters);
  946. }
  947. public function initialize($context, $parameters = array())
  948. {
  949. $this->context = $context;
  950. $this->parameterHolder = new sfParameterHolder();
  951. $this->parameterHolder->add($parameters);
  952. return true;
  953. }
  954. protected function isFirstCall()
  955. {
  956. $class = get_class($this);
  957. if (isset(self::$filterCalled[$class]))
  958. {
  959. return false;
  960. }
  961. else
  962. {
  963. self::$filterCalled[$class] = true;
  964. return true;
  965. }
  966. }
  967. public final function getContext()
  968. {
  969. return $this->context;
  970. }
  971. public function getParameterHolder()
  972. {
  973. return $this->parameterHolder;
  974. }
  975. public function getParameter($name, $default = null)
  976. {
  977. return $this->parameterHolder->get($name, $default);
  978. }
  979. public function hasParameter($name)
  980. {
  981. return $this->parameterHolder->has($name);
  982. }
  983. public function setParameter($name, $value)
  984. {
  985. return $this->parameterHolder->set($name, $value);
  986. }
  987. }
  988. class sfCommonFilter extends sfFilter
  989. {
  990. public function execute($filterChain)
  991. {
  992. $filterChain->execute();
  993. $response = $this->context->getResponse();
  994. $content = $response->getContent();
  995. if (false !== ($pos = strpos($content, '</head>')))
  996. {
  997. $this->context->getConfiguration()->loadHelpers(array('Tag', 'Asset'));
  998. $html = '';
  999. if (!sfConfig::get('symfony.asset.javascripts_included', false))
  1000. {
  1001. $html .= get_javascripts($response);
  1002. }
  1003. if (!sfConfig::get('symfony.asset.stylesheets_included', false))
  1004. {
  1005. $html .= get_stylesheets($response);
  1006. }
  1007. if ($html)
  1008. {
  1009. $response->setContent(substr($content, 0, $pos).$html.substr($content, $pos));
  1010. }
  1011. }
  1012. sfConfig::set('symfony.asset.javascripts_included', false);
  1013. sfConfig::set('symfony.asset.stylesheets_included', false);
  1014. }
  1015. }
  1016. class sfExecutionFilter extends sfFilter
  1017. {
  1018. public function execute($filterChain)
  1019. {
  1020. $actionInstance = $this->context->getController()->getActionStack()->getLastEntry()->getActionInstance();
  1021. if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
  1022. {
  1023. $timer = sfTimerManager::getTimer(sprintf('Action "%s/%s"', $actionInstance->getModuleName(), $actionInstance->getActionName()));
  1024. $viewName = $this->handleAction($filterChain, $actionInstance);
  1025. $timer->addTime();
  1026. $timer = sfTimerManager::getTimer(sprintf('View "%s" for "%s/%s"', $viewName, $actionInstance->getModuleName(), $actionInstance->getActionName()));
  1027. $this->handleView($filterChain, $actionInstance, $viewName);
  1028. $timer->addTime();
  1029. }
  1030. else
  1031. {
  1032. $viewName = $this->handleAction($filterChain, $actionInstance);
  1033. $this->handleView($filterChain, $actionInstance, $viewName);
  1034. }
  1035. }
  1036. protected function handleAction($filterChain, $actionInstance)
  1037. {
  1038. $uri = $this->context->getRouting()->getCurrentInternalUri();
  1039. if (sfConfig::get('sf_cache') && !is_null($uri) && $this->context->getViewCacheManager()->hasActionCache($uri))
  1040. {
  1041. return sfView::SUCCESS;
  1042. }
  1043. return $this->executeAction($actionInstance);
  1044. }
  1045. protected function executeAction($actionInstance)
  1046. {
  1047. $actionInstance->preExecute();
  1048. $viewName = $actionInstance->execute($this->context->getRequest());
  1049. $actionInstance->postExecute();
  1050. return is_null($viewName) ? sfView::SUCCESS : $viewName;
  1051. }
  1052. protected function handleView($filterChain, $actionInstance, $viewName)
  1053. {
  1054. switch ($viewName)
  1055. {
  1056. case sfView::HEADER_ONLY:
  1057. $this->context->getResponse()->setHeaderOnly(true);
  1058. return;
  1059. case sfView::NONE:
  1060. return;
  1061. }
  1062. $this->executeView($actionInstance->getModuleName(), $actionInstance->getActionName(), $viewName, $actionInstance->getVarHolder()->getAll());
  1063. }
  1064. protected function executeView($moduleName, $actionName, $viewName, $viewAttributes)
  1065. {
  1066. $controller = $this->context->getController();
  1067. $view = $controller->getView($moduleName, $actionName, $viewName);
  1068. $view->execute();
  1069. $view->getAttributeHolder()->add($viewAttributes);
  1070. switch ($controller->getRenderMode())
  1071. {
  1072. case sfView::RENDER_NONE:
  1073. break;
  1074. case sfView::RENDER_CLIENT:
  1075. $viewData = $view->render();
  1076. $this->context->getResponse()->setContent($viewData);
  1077. break;
  1078. case sfView::RENDER_VAR:
  1079. $viewData = $view->render();
  1080. $controller->getActionStack()->getLastEntry()->setPresentation($viewData);
  1081. break;
  1082. }
  1083. }
  1084. }
  1085. class sfRenderingFilter extends sfFilter
  1086. {
  1087. public function execute($filterChain)
  1088. {
  1089. $filterChain->execute();
  1090. $response = $this->context->getResponse();
  1091. if (sfForm::hasToStringException())
  1092. {
  1093. throw sfForm::getToStringException();
  1094. }
  1095. else if (sfFormField::hasToStringException())
  1096. {
  1097. throw sfFormField::getToStringException();
  1098. }
  1099. $response->send();
  1100. }
  1101. }
  1102. class sfFilterChain
  1103. {
  1104. protected
  1105. $chain = array(),
  1106. $index = -1;
  1107. public function loadConfiguration($actionInstance)
  1108. {
  1109. require(sfContext::getInstance()->getConfigCache()->checkConfig('modules/'.$actionInstance->getModuleName().'/config/filters.yml'));
  1110. }
  1111. public function execute()
  1112. {
  1113. ++$this->index;
  1114. if ($this->index < count($this->chain))
  1115. {
  1116. if (sfConfig::get('sf_logging_enabled'))
  1117. {
  1118. sfContext::getInstance()->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array(sprintf('Executing filter "%s"', get_class($this->chain[$this->index])))));
  1119. }
  1120. $this->chain[$this->index]->execute($this);
  1121. }
  1122. }
  1123. public function hasFilter($class)
  1124. {
  1125. foreach ($this->chain as $filter)
  1126. {
  1127. if ($filter instanceof $class)
  1128. {
  1129. return true;
  1130. }
  1131. }
  1132. return false;
  1133. }
  1134. public function register($filter)
  1135. {
  1136. $this->chain[] = $filter;
  1137. }
  1138. }
  1139. abstract class sfLogger
  1140. {
  1141. const EMERG = 0; const ALERT = 1; const CRIT = 2; const ERR = 3; const WARNING = 4; const NOTICE = 5; const INFO = 6; const DEBUG = 7;
  1142. protected
  1143. $level = self::INFO;
  1144. public function __construct(sfEventDispatcher $dispatcher, $options = array())
  1145. {
  1146. $this->initialize($dispatcher, $options);
  1147. if (!isset($options['auto_shutdown']) || $options['auto_shutdown'])
  1148. {
  1149. register_shutdown_function(array($this, 'shutdown'));
  1150. }
  1151. }
  1152. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  1153. {
  1154. if (isset($options['level']))
  1155. {
  1156. $this->setLogLevel($options['level']);
  1157. }
  1158. $dispatcher->connect('application.log', array($this, 'listenToLogEvent'));
  1159. }
  1160. public function getLogLevel()
  1161. {
  1162. return $this->level;
  1163. }
  1164. public function setLogLevel($level)
  1165. {
  1166. if (!is_int($level))
  1167. {
  1168. $level = constant('sfLogger::'.strtoupper($level));
  1169. }
  1170. $this->level = $level;
  1171. }
  1172. public function log($message, $priority = self::INFO)
  1173. {
  1174. if ($this->getLogLevel() < $priority)
  1175. {
  1176. return false;
  1177. }
  1178. return $this->doLog($message, $priority);
  1179. }
  1180. abstract protected function doLog($message, $priority);
  1181. public function emerg($message)
  1182. {
  1183. $this->log($message, self::EMERG);
  1184. }
  1185. public function alert($message)
  1186. {
  1187. $this->log($message, self::ALERT);
  1188. }
  1189. public function crit($message)
  1190. {
  1191. $this->log($message, self::CRIT);
  1192. }
  1193. public function err($message)
  1194. {
  1195. $this->log($message, self::ERR);
  1196. }
  1197. public function warning($message)
  1198. {
  1199. $this->log($message, self::WARNING);
  1200. }
  1201. public function notice($message)
  1202. {
  1203. $this->log($message, self::NOTICE);
  1204. }
  1205. public function info($message)
  1206. {
  1207. $this->log($message, self::INFO);
  1208. }
  1209. public function debug($message)
  1210. {
  1211. $this->log($message, self::DEBUG);
  1212. }
  1213. public function listenToLogEvent(sfEvent $event)
  1214. {
  1215. $priority = isset($event['priority']) ? $event['priority'] : self::INFO;
  1216. $subject = $event->getSubject();
  1217. $subject = is_object($subject) ? get_class($subject) : (is_string($subject) ? $subject : 'main');
  1218. foreach ($event->getParameters() as $key => $message)
  1219. {
  1220. if ('priority' === $key)
  1221. {
  1222. continue;
  1223. }
  1224. $this->log(sprintf('{%s} %s', $subject, $message), $priority);
  1225. }
  1226. }
  1227. public function shutdown()
  1228. {
  1229. }
  1230. static public function getPriorityName($priority)
  1231. {
  1232. static $levels = array(
  1233. self::EMERG => 'emerg',
  1234. self::ALERT => 'alert',
  1235. self::CRIT => 'crit',
  1236. self::ERR => 'err',
  1237. self::WARNING => 'warning',
  1238. self::NOTICE => 'notice',
  1239. self::INFO => 'info',
  1240. self::DEBUG => 'debug',
  1241. );
  1242. if (!isset($levels[$priority]))
  1243. {
  1244. throw new sfException(sprintf('The priority level "%s" does not exist.', $priority));
  1245. }
  1246. return $levels[$priority];
  1247. }
  1248. }
  1249. class sfNoLogger extends sfLogger
  1250. {
  1251. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  1252. {
  1253. }
  1254. protected function doLog($message, $priority)
  1255. {
  1256. }
  1257. }
  1258. abstract class sfRequest
  1259. {
  1260. const GET = 'GET';
  1261. const POST = 'POST';
  1262. const PUT = 'PUT';
  1263. const DELETE = 'DELETE';
  1264. const HEAD = 'HEAD';
  1265. protected
  1266. $dispatcher = null,
  1267. $method = null,
  1268. $options = array(),
  1269. $parameterHolder = null,
  1270. $attributeHolder = null;
  1271. public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
  1272. {
  1273. $this->initialize($dispatcher, $parameters, $attributes, $options);
  1274. }
  1275. public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
  1276. {
  1277. $this->dispatcher = $dispatcher;
  1278. $this->options = $options;
  1279. if (!isset($this->options['logging']))
  1280. {
  1281. $this->options['logging'] = false;
  1282. }
  1283. $this->parameterHolder = new sfParameterHolder();
  1284. $this->attributeHolder = new sfParameterHolder();
  1285. $this->parameterHolder->add($parameters);
  1286. $this->attributeHolder->add($attributes);
  1287. }
  1288. public function extractParameters($names)
  1289. {
  1290. $array = array();
  1291. $parameters = $this->parameterHolder->getAll();
  1292. foreach ($parameters as $key => $value)
  1293. {
  1294. if (in_array($key, $names))
  1295. {
  1296. $array[$key] = $value;
  1297. }
  1298. }
  1299. return $array;
  1300. }
  1301. public function getMethod()
  1302. {
  1303. return $this->method;
  1304. }
  1305. public function setMethod($method)
  1306. {
  1307. if (!in_array(strtoupper($method), array(self::GET, self::POST, self::PUT, self::DELETE, self::HEAD)))
  1308. {
  1309. throw new sfException(sprintf('Invalid request method: %s.', $method));
  1310. }
  1311. $this->method = strtoupper($method);
  1312. }
  1313. public function getParameterHolder()
  1314. {
  1315. return $this->parameterHolder;
  1316. }
  1317. public function getAttributeHolder()
  1318. {
  1319. return $this->attributeHolder;
  1320. }
  1321. public function getAttribute($name, $default = null)
  1322. {
  1323. return $this->attributeHolder->get($name, $default);
  1324. }
  1325. public function hasAttribute($name)
  1326. {
  1327. return $this->attributeHolder->has($name);
  1328. }
  1329. public function setAttribute($name, $value)
  1330. {
  1331. $this->attributeHolder->set($name, $value);
  1332. }
  1333. public function getParameter($name, $default = null)
  1334. {
  1335. return $this->parameterHolder->get($name, $default);
  1336. }
  1337. public function hasParameter($name)
  1338. {
  1339. return $this->parameterHolder->has($name);
  1340. }
  1341. public function setParameter($name, $value)
  1342. {
  1343. $this->parameterHolder->set($name, $value);
  1344. }
  1345. public function __call($method, $arguments)
  1346. {
  1347. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'request.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  1348. if (!$event->isProcessed())
  1349. {
  1350. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  1351. }
  1352. return $event->getReturnValue();
  1353. }
  1354. public function __clone()
  1355. {
  1356. $this->parameterHolder = clone $this->parameterHolder;
  1357. $this->attributeHolder = clone $this->attributeHolder;
  1358. }
  1359. }
  1360. abstract class sfResponse implements Serializable
  1361. {
  1362. protected
  1363. $options = array(),
  1364. $dispatcher = null,
  1365. $content = '';
  1366. public function __construct(sfEventDispatcher $dispatcher, $options = array())
  1367. {
  1368. $this->initialize($dispatcher, $options);
  1369. }
  1370. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  1371. {
  1372. $this->dispatcher = $dispatcher;
  1373. $this->options = $options;
  1374. if (!isset($this->options['logging']))
  1375. {
  1376. $this->options['logging'] = false;
  1377. }
  1378. }
  1379. public function setEventDispatcher(sfEventDispatcher $dispatcher)
  1380. {
  1381. $this->dispatcher = $dispatcher;
  1382. }
  1383. public function setContent($content)
  1384. {
  1385. $this->content = $content;
  1386. }
  1387. public function getContent()
  1388. {
  1389. return $this->content;
  1390. }
  1391. public function sendContent()
  1392. {
  1393. $event = $this->dispatcher->filter(new sfEvent($this, 'response.filter_content'), $this->getContent());
  1394. $content = $event->getReturnValue();
  1395. if ($this->options['logging'])
  1396. {
  1397. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send content (%s o)', strlen($content)))));
  1398. }
  1399. echo $content;
  1400. }
  1401. public function send()
  1402. {
  1403. $this->sendContent();
  1404. }
  1405. public function getOptions()
  1406. {
  1407. return $this->options;
  1408. }
  1409. public function __call($method, $arguments)
  1410. {
  1411. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'response.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  1412. if (!$event->isProcessed())
  1413. {
  1414. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  1415. }
  1416. return $event->getReturnValue();
  1417. }
  1418. public function serialize()
  1419. {
  1420. return serialize($this->content);
  1421. }
  1422. public function unserialize($serialized)
  1423. {
  1424. $this->content = unserialize($serialized);
  1425. }
  1426. }
  1427. abstract class sfRouting
  1428. {
  1429. protected
  1430. $dispatcher = null,
  1431. $cache = null,
  1432. $defaultParameters = array(),
  1433. $options = array();
  1434. public function __construct(sfEventDispatcher $dispatcher, sfCache $cache = null, $options = array())
  1435. {
  1436. $this->initialize($dispatcher, $cache, $options);
  1437. if (!isset($this->options['auto_shutdown']) || $this->options['auto_shutdown'])
  1438. {
  1439. register_shutdown_function(array($this, 'shutdown'));
  1440. }
  1441. }
  1442. public function getCache()
  1443. {
  1444. return $this->cache;
  1445. }
  1446. public function initialize(sfEventDispatcher $dispatcher, sfCache $cache = null, $options = array())
  1447. {
  1448. $this->dispatcher = $dispatcher;
  1449. $options['debug'] = isset($options['debug']) ? (boolean) $options['debug'] : false;
  1450. $this->cache = $options['debug'] ? null : $cache;
  1451. $this->setDefaultParameter('module', isset($options['default_module']) ? $options['default_module'] : 'default');
  1452. $this->setDefaultParameter('action', isset($options['default_action']) ? $options['default_action'] : 'index');
  1453. if (!isset($options['logging']))
  1454. {
  1455. $options['logging'] = false;
  1456. }
  1457. if (!isset($options['context']))
  1458. {
  1459. $options['context'] = array();
  1460. }
  1461. $this->options = $options;
  1462. $this->dispatcher->connect('user.change_culture', array($this, 'listenToChangeCultureEvent'));
  1463. $this->dispatcher->connect('request.filter_parameters', array($this, 'filterParametersEvent'));
  1464. $this->loadConfiguration();
  1465. }
  1466. public function getOptions()
  1467. {
  1468. return $this->options;
  1469. }
  1470. public function loadConfiguration()
  1471. {
  1472. $this->dispatcher->notify(new sfEvent($this, 'routing.load_configuration'));
  1473. }
  1474. abstract public function getCurrentInternalUri($with_route_name = false);
  1475. abstract public function getRoutes();
  1476. abstract public function setRoutes($routes);
  1477. abstract public function hasRoutes();
  1478. abstract public function clearRoutes();
  1479. abstract public function generate($name, $params = array(), $absolute = false);
  1480. abstract public function parse($url);
  1481. public function getDefaultParameters()
  1482. {
  1483. return $this->defaultParameters;
  1484. }
  1485. public function getDefaultParameter($key)
  1486. {
  1487. return isset($this->defaultParameters[$key]) ? $this->defaultParameters[$key] : null;
  1488. }
  1489. public function setDefaultParameter($key, $value)
  1490. {
  1491. $this->defaultParameters[$key] = $value;
  1492. }
  1493. public function setDefaultParameters($parameters)
  1494. {
  1495. $this->defaultParameters = $parameters;
  1496. }
  1497. protected function mergeArrays($arr1, $arr2)
  1498. {
  1499. foreach ($arr2 as $key => $value)
  1500. {
  1501. $arr1[$key] = $value;
  1502. }
  1503. return $arr1;
  1504. }
  1505. public function listenToChangeCultureEvent(sfEvent $event)
  1506. {
  1507. $this->setDefaultParameter('sf_culture', $event['culture']);
  1508. }
  1509. public function filterParametersEvent(sfEvent $event, $parameters)
  1510. {
  1511. $context = $event->getParameters();
  1512. $this->options['context'] = $context;
  1513. if (false === $params = $this->parse($event['path_info']))
  1514. {
  1515. return $parameters;
  1516. }
  1517. return array_merge($parameters, $params);
  1518. }
  1519. protected function fixGeneratedUrl($url, $absolute = false)
  1520. {
  1521. if (isset($this->options['context']['prefix']))
  1522. {
  1523. if (0 === strpos($url, 'http'))
  1524. {
  1525. $url = preg_replace('#https?\://[^/]+#', '$0'.$this->options['context']['prefix'], $url);
  1526. }
  1527. else
  1528. {
  1529. $url = $this->options['context']['prefix'].$url;
  1530. }
  1531. }
  1532. if ($absolute && isset($this->options['context']['host']) && 0 !== strpos($url, 'http'))
  1533. {
  1534. $url = 'http'.(isset($this->options['context']['is_secure']) && $this->options['context']['is_secure'] ? 's' : '').'://'.$this->options['context']['host'].$url;
  1535. }
  1536. return $url;
  1537. }
  1538. public function shutdown()
  1539. {
  1540. }
  1541. }
  1542. abstract class sfStorage
  1543. {
  1544. protected
  1545. $options = array();
  1546. public function __construct($options = array())
  1547. {
  1548. $this->initialize($options);
  1549. if ($this->options['auto_shutdown'])
  1550. {
  1551. register_shutdown_function(array($this, 'shutdown'));
  1552. }
  1553. }
  1554. public function initialize($options = array())
  1555. {
  1556. $this->options = array_merge(array(
  1557. 'auto_shutdown' => true,
  1558. ), $options);
  1559. }
  1560. public function getOptions()
  1561. {
  1562. return $this->options;
  1563. }
  1564. abstract public function read($key);
  1565. abstract public function regenerate($destroy = false);
  1566. abstract public function remove($key);
  1567. abstract public function shutdown();
  1568. abstract public function write($key, $data);
  1569. }
  1570. class sfUser
  1571. {
  1572. const ATTRIBUTE_NAMESPACE = 'symfony/user/sfUser/attributes';
  1573. const CULTURE_NAMESPACE = 'symfony/user/sfUser/culture';
  1574. protected
  1575. $options = array(),
  1576. $attributeHolder = null,
  1577. $culture = null,
  1578. $storage = null,
  1579. $dispatcher = null;
  1580. public function __construct(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
  1581. {
  1582. $this->initialize($dispatcher, $storage, $options);
  1583. if ($this->options['auto_shutdown'])
  1584. {
  1585. register_shutdown_function(array($this, 'shutdown'));
  1586. }
  1587. }
  1588. public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
  1589. {
  1590. $this->dispatcher = $dispatcher;
  1591. $this->storage = $storage;
  1592. $this->options = array_merge(array(
  1593. 'auto_shutdown' => true,
  1594. 'culture' => null,
  1595. 'default_culture' => 'en',
  1596. 'use_flash' => false,
  1597. 'logging' => false,
  1598. ), $options);
  1599. $this->attributeHolder = new sfNamespacedParameterHolder(self::ATTRIBUTE_NAMESPACE);
  1600. $attributes = $storage->read(self::ATTRIBUTE_NAMESPACE);
  1601. if (is_array($attributes))
  1602. {
  1603. foreach ($attributes as $namespace => $values)
  1604. {
  1605. $this->attributeHolder->add($values, $namespace);
  1606. }
  1607. }
  1608. $currentCulture = $storage->read(self::CULTURE_NAMESPACE);
  1609. $this->setCulture(!is_null($this->options['culture']) ? $this->options['culture'] : (!is_null($currentCulture) ? $currentCulture : $this->options['default_culture']));
  1610. if ($this->options['use_flash'] && $names = $this->attributeHolder->getNames('symfony/user/sfUser/flash'))
  1611. {
  1612. if ($this->options['logging'])
  1613. {
  1614. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Flag old flash messages ("%s")', implode('", "', $names)))));
  1615. }
  1616. foreach ($names as $name)
  1617. {
  1618. $this->attributeHolder->set($name, true, 'symfony/user/sfUser/flash/remove');
  1619. }
  1620. }
  1621. }
  1622. public function getOptions()
  1623. {
  1624. return $this->options;
  1625. }
  1626. public function setCulture($culture)
  1627. {
  1628. if ($this->culture != $culture)
  1629. {
  1630. $this->culture = $culture;
  1631. $this->dispatcher->notify(new sfEvent($this, 'user.change_culture', array('culture' => $culture)));
  1632. }
  1633. }
  1634. public function setFlash($name, $value, $persist = true)
  1635. {
  1636. if (!$this->options['use_flash'])
  1637. {
  1638. return;
  1639. }
  1640. $this->setAttribute($name, $value, 'symfony/user/sfUser/flash');
  1641. if ($persist)
  1642. {
  1643. $this->attributeHolder->remove($name, null, 'symfony/user/sfUser/flash/remove');
  1644. }
  1645. else
  1646. {
  1647. $this->setAttribute($name, true, 'symfony/user/sfUser/flash/remove');
  1648. }
  1649. }
  1650. public function getFlash($name, $default = null)
  1651. {
  1652. if (!$this->options['use_flash'])
  1653. {
  1654. return $default;
  1655. }
  1656. return $this->getAttribute($name, $default, 'symfony/user/sfUser/flash');
  1657. }
  1658. public function hasFlash($name)
  1659. {
  1660. if (!$this->options['use_flash'])
  1661. {
  1662. return false;
  1663. }
  1664. return $this->hasAttribute($name, 'symfony/user/sfUser/flash');
  1665. }
  1666. public function getCulture()
  1667. {
  1668. return $this->culture;
  1669. }
  1670. public function getAttributeHolder()
  1671. {
  1672. return $this->attributeHolder;
  1673. }
  1674. public function getAttribute($name, $default = null, $ns = null)
  1675. {
  1676. return $this->attributeHolder->get($name, $default, $ns);
  1677. }
  1678. public function hasAttribute($name, $ns = null)
  1679. {
  1680. return $this->attributeHolder->has($name, $ns);
  1681. }
  1682. public function setAttribute($name, $value, $ns = null)
  1683. {
  1684. return $this->attributeHolder->set($name, $value, $ns);
  1685. }
  1686. public function shutdown()
  1687. {
  1688. if ($this->options['use_flash'] && $names = $this->attributeHolder->getNames('symfony/user/sfUser/flash/remove'))
  1689. {
  1690. if ($this->options['logging'])
  1691. {
  1692. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Remove old flash messages ("%s")', implode('", "', $names)))));
  1693. }
  1694. foreach ($names as $name)
  1695. {
  1696. $this->attributeHolder->remove($name, null, 'symfony/user/sfUser/flash');
  1697. $this->attributeHolder->remove($name, null, 'symfony/user/sfUser/flash/remove');
  1698. }
  1699. }
  1700. $attributes = array();
  1701. foreach ($this->attributeHolder->getNamespaces() as $namespace)
  1702. {
  1703. $attributes[$namespace] = $this->attributeHolder->getAll($namespace);
  1704. }
  1705. $this->storage->write(self::ATTRIBUTE_NAMESPACE, $attributes);
  1706. $this->storage->write(self::CULTURE_NAMESPACE, $this->culture);
  1707. }
  1708. public function __call($method, $arguments)
  1709. {
  1710. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'user.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  1711. if (!$event->isProcessed())
  1712. {
  1713. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  1714. }
  1715. return $event->getReturnValue();
  1716. }
  1717. }
  1718. class sfNamespacedParameterHolder extends sfParameterHolder
  1719. {
  1720. protected $default_namespace = null;
  1721. protected $parameters = array();
  1722. public function __construct($namespace = 'symfony/default')
  1723. {
  1724. $this->default_namespace = $namespace;
  1725. }
  1726. public function setDefaultNamespace($namespace, $move = true)
  1727. {
  1728. if ($move)
  1729. {
  1730. $values = $this->removeNamespace();
  1731. $this->addByRef($values, $namespace);
  1732. }
  1733. $this->default_namespace = $namespace;
  1734. }
  1735. public function getDefaultNamespace()
  1736. {
  1737. return $this->default_namespace;
  1738. }
  1739. public function clear()
  1740. {
  1741. $this->parameters = null;
  1742. $this->parameters = array();
  1743. }
  1744. public function & get($name, $default = null, $ns = null)
  1745. {
  1746. if (!$ns)
  1747. {
  1748. $ns = $this->default_namespace;
  1749. }
  1750. if (isset($this->parameters[$ns][$name]))
  1751. {
  1752. $value = & $this->parameters[$ns][$name];
  1753. }
  1754. else if (isset($this->parameters[$ns]))
  1755. {
  1756. $value = sfToolkit::getArrayValueForPath($this->parameters[$ns], $name, $default);
  1757. }
  1758. else
  1759. {
  1760. $value = $default;
  1761. }
  1762. return $value;
  1763. }
  1764. public function getNames($ns = null)
  1765. {
  1766. if (!$ns)
  1767. {
  1768. $ns = $this->default_namespace;
  1769. }
  1770. if (isset($this->parameters[$ns]))
  1771. {
  1772. return array_keys($this->parameters[$ns]);
  1773. }
  1774. return array();
  1775. }
  1776. public function getNamespaces()
  1777. {
  1778. return array_keys($this->parameters);
  1779. }
  1780. public function & getAll($ns = null)
  1781. {
  1782. if (!$ns)
  1783. {
  1784. $ns = $this->default_namespace;
  1785. }
  1786. $parameters = array();
  1787. if (isset($this->parameters[$ns]))
  1788. {
  1789. $parameters = $this->parameters[$ns];
  1790. }
  1791. return $parameters;
  1792. }
  1793. public function has($name, $ns = null)
  1794. {
  1795. if (!$ns)
  1796. {
  1797. $ns = $this->default_namespace;
  1798. }
  1799. if (isset($this->parameters[$ns][$name]))
  1800. {
  1801. return true;
  1802. }
  1803. else if (isset($this->parameters[$ns]))
  1804. {
  1805. return sfToolkit::hasArrayValueForPath($this->parameters[$ns], $name);
  1806. }
  1807. return false;
  1808. }
  1809. public function hasNamespace($ns)
  1810. {
  1811. return isset($this->parameters[$ns]);
  1812. }
  1813. public function remove($name, $default = null, $ns = null)
  1814. {
  1815. if (!$ns)
  1816. {
  1817. $ns = $this->default_namespace;
  1818. }
  1819. $retval = $default;
  1820. if (isset($this->parameters[$ns]) && array_key_exists($name, $this->parameters[$ns]))
  1821. {
  1822. $retval = $this->parameters[$ns][$name];
  1823. unset($this->parameters[$ns][$name]);
  1824. }
  1825. else
  1826. {
  1827. $retval = sfToolkit::removeArrayValueForPath($this->parameters[$ns], $name, $default);
  1828. }
  1829. return $retval;
  1830. }
  1831. public function & removeNamespace($ns = null)
  1832. {
  1833. if (!$ns)
  1834. {
  1835. $ns = $this->default_namespace;
  1836. }
  1837. $retval = null;
  1838. if (isset($this->parameters[$ns]))
  1839. {
  1840. $retval =& $this->parameters[$ns];
  1841. unset($this->parameters[$ns]);
  1842. }
  1843. return $retval;
  1844. }
  1845. public function set($name, $value, $ns = null)
  1846. {
  1847. if (!$ns)
  1848. {
  1849. $ns = $this->default_namespace;
  1850. }
  1851. if (!isset($this->parameters[$ns]))
  1852. {
  1853. $this->parameters[$ns] = array();
  1854. }
  1855. $this->parameters[$ns][$name] = $value;
  1856. }
  1857. public function setByRef($name, & $value, $ns = null)
  1858. {
  1859. if (!$ns)
  1860. {
  1861. $ns = $this->default_namespace;
  1862. }
  1863. if (!isset($this->parameters[$ns]))
  1864. {
  1865. $this->parameters[$ns] = array();
  1866. }
  1867. $this->parameters[$ns][$name] =& $value;
  1868. }
  1869. public function add($parameters, $ns = null)
  1870. {
  1871. if ($parameters === null) return;
  1872. if (!$ns)
  1873. {
  1874. $ns = $this->default_namespace;
  1875. }
  1876. if (!isset($this->parameters[$ns]))
  1877. {
  1878. $this->parameters[$ns] = array();
  1879. }
  1880. foreach ($parameters as $key => $value)
  1881. {
  1882. $this->parameters[$ns][$key] = $value;
  1883. }
  1884. }
  1885. public function addByRef(& $parameters, $ns = null)
  1886. {
  1887. if (!$ns)
  1888. {
  1889. $ns = $this->default_namespace;
  1890. }
  1891. if (!isset($this->parameters[$ns]))
  1892. {
  1893. $this->parameters[$ns] = array();
  1894. }
  1895. foreach ($parameters as $key => &$value)
  1896. {
  1897. $this->parameters[$ns][$key] =& $value;
  1898. }
  1899. }
  1900. public function serialize()
  1901. {
  1902. return serialize(array($this->default_namespace, $this->parameters));
  1903. }
  1904. public function unserialize($serialized)
  1905. {
  1906. $data = unserialize($serialized);
  1907. $this->default_namespace = $data[0];
  1908. $this->parameters = $data[1];
  1909. }
  1910. }
  1911. abstract class sfView
  1912. {
  1913. const ALERT = 'Alert';
  1914. const ERROR = 'Error';
  1915. const INPUT = 'Input';
  1916. const NONE = 'None';
  1917. const SUCCESS = 'Success';
  1918. const RENDER_NONE = 1;
  1919. const RENDER_CLIENT = 2;
  1920. const RENDER_VAR = 4;
  1921. const HEADER_ONLY = 8;
  1922. protected
  1923. $context = null,
  1924. $dispatcher = null,
  1925. $decorator = false,
  1926. $decoratorDirectory = null,
  1927. $decoratorTemplate = null,
  1928. $directory = null,
  1929. $componentSlots = array(),
  1930. $template = null,
  1931. $attributeHolder = null,
  1932. $parameterHolder = null,
  1933. $moduleName = '',
  1934. $actionName = '',
  1935. $viewName = '',
  1936. $extension = '.php';
  1937. public function __construct($context, $moduleName, $actionName, $viewName)
  1938. {
  1939. $this->initialize($context, $moduleName, $actionName, $viewName);
  1940. }
  1941. public function initialize($context, $moduleName, $actionName, $viewName)
  1942. {
  1943. $this->moduleName = $moduleName;
  1944. $this->actionName = $actionName;
  1945. $this->viewName = $viewName;
  1946. $this->context = $context;
  1947. $this->dispatcher = $context->getEventDispatcher();
  1948. sfOutputEscaper::markClassesAsSafe(array('sfForm', 'sfModelGeneratorHelper'));
  1949. $this->attributeHolder = $this->initializeAttributeHolder();
  1950. $this->parameterHolder = new sfParameterHolder();
  1951. $this->parameterHolder->add(sfConfig::get('mod_'.strtolower($moduleName).'_view_param', array()));
  1952. $request = $context->getRequest();
  1953. if (!is_null($format = $request->getRequestFormat()))
  1954. {
  1955. if ('html' != $format)
  1956. {
  1957. $this->setExtension('.'.$format.$this->getExtension());
  1958. }
  1959. if ($mimeType = $request->getMimeType($format))
  1960. {
  1961. $this->context->getResponse()->setContentType($mimeType);
  1962. if ('html' != $format)
  1963. {
  1964. $this->setDecorator(false);
  1965. }
  1966. }
  1967. $this->dispatcher->notify(new sfEvent($this, 'view.configure_format', array('format' => $format, 'response' => $context->getResponse(), 'request' => $context->getRequest())));
  1968. }
  1969. $this->configure();
  1970. return true;
  1971. }
  1972. protected function initializeAttributeHolder($attributes = array())
  1973. {
  1974. if ('both' === sfConfig::get('sf_escaping_strategy'))
  1975. {
  1976. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Escaping strategy "both" is deprecated, please use "on".', 'priority' => sfLogger::ERR)));
  1977. sfConfig::set('sf_escaping_strategy', 'on');
  1978. }
  1979. else if ('bc' === sfConfig::get('sf_escaping_strategy'))
  1980. {
  1981. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Escaping strategy "bc" is deprecated, please use "off".', 'priority' => sfLogger::ERR)));
  1982. sfConfig::set('sf_escaping_strategy', 'off');
  1983. }
  1984. $attributeHolder = new sfViewParameterHolder($this->dispatcher, $attributes, array(
  1985. 'escaping_method' => sfConfig::get('sf_escaping_method'),
  1986. 'escaping_strategy' => sfConfig::get('sf_escaping_strategy'),
  1987. ));
  1988. return $attributeHolder;
  1989. }
  1990. abstract function execute();
  1991. abstract function configure();
  1992. public function getDecoratorDirectory()
  1993. {
  1994. return $this->decoratorDirectory;
  1995. }
  1996. public function getDecoratorTemplate()
  1997. {
  1998. return $this->decoratorTemplate;
  1999. }
  2000. public function getDirectory()
  2001. {
  2002. return $this->directory;
  2003. }
  2004. abstract function getEngine();
  2005. public function getTemplate()
  2006. {
  2007. return $this->template;
  2008. }
  2009. public function getAttributeHolder()
  2010. {
  2011. return $this->attributeHolder;
  2012. }
  2013. public function getAttribute($name, $default = null)
  2014. {
  2015. return $this->attributeHolder->get($name, $default);
  2016. }
  2017. public function hasAttribute($name)
  2018. {
  2019. return $this->attributeHolder->has($name);
  2020. }
  2021. public function setAttribute($name, $value)
  2022. {
  2023. $this->attributeHolder->set($name, $value);
  2024. }
  2025. public function getParameterHolder()
  2026. {
  2027. return $this->parameterHolder;
  2028. }
  2029. public function getParameter($name, $default = null)
  2030. {
  2031. return $this->parameterHolder->get($name, $default);
  2032. }
  2033. public function hasParameter($name)
  2034. {
  2035. return $this->parameterHolder->has($name);
  2036. }
  2037. public function setParameter($name, $value)
  2038. {
  2039. $this->parameterHolder->set($name, $value);
  2040. }
  2041. public function isDecorator()
  2042. {
  2043. return $this->decorator;
  2044. }
  2045. public function setDecorator($boolean)
  2046. {
  2047. $this->decorator = (boolean) $boolean;
  2048. if (false === $boolean)
  2049. {
  2050. $this->decoratorTemplate = false;
  2051. }
  2052. }
  2053. protected function preRenderCheck()
  2054. {
  2055. if (is_null($this->template))
  2056. {
  2057. throw new sfRenderException('A template has not been set.');
  2058. }
  2059. if (!is_readable($this->directory.'/'.$this->template))
  2060. {
  2061. if ('404' == $this->context->getResponse()->getStatusCode())
  2062. {
  2063. $this->template = sfException::getTemplatePathForError($this->context->getRequest()->getRequestFormat(), false);
  2064. $this->directory = dirname($this->template);
  2065. $this->template = basename($this->template);
  2066. $this->setAttribute('code', '404');
  2067. $this->setAttribute('text', 'Not Found');
  2068. }
  2069. else
  2070. {
  2071. throw new sfRenderException(sprintf('The template "%s" does not exist or is unreadable in "%s".', $this->template, $this->directory));
  2072. }
  2073. }
  2074. if ($this->decorator && !is_readable($this->decoratorDirectory.'/'.$this->decoratorTemplate))
  2075. {
  2076. throw new sfRenderException(sprintf('The decorator template "%s" does not exist or is unreadable in "%s".', $this->decoratorTemplate, $this->decoratorDirectory));
  2077. }
  2078. }
  2079. abstract function render();
  2080. public function setDecoratorDirectory($directory)
  2081. {
  2082. $this->decoratorDirectory = $directory;
  2083. }
  2084. public function setDecoratorTemplate($template)
  2085. {
  2086. if (false === $template)
  2087. {
  2088. $this->setDecorator(false);
  2089. return;
  2090. }
  2091. else if (is_null($template))
  2092. {
  2093. return;
  2094. }
  2095. if (!strpos($template, '.'))
  2096. {
  2097. $template .= $this->getExtension();
  2098. }
  2099. if (sfToolkit::isPathAbsolute($template))
  2100. {
  2101. $this->decoratorDirectory = dirname($template);
  2102. $this->decoratorTemplate = basename($template);
  2103. }
  2104. else
  2105. {
  2106. $this->decoratorDirectory = $this->context->getConfiguration()->getDecoratorDir($template);
  2107. $this->decoratorTemplate = $template;
  2108. }
  2109. $this->decorator = true;
  2110. }
  2111. public function setDirectory($directory)
  2112. {
  2113. $this->directory = $directory;
  2114. }
  2115. public function setComponentSlot($attributeName, $moduleName, $componentName)
  2116. {
  2117. $this->componentSlots[$attributeName] = array();
  2118. $this->componentSlots[$attributeName]['module_name'] = $moduleName;
  2119. $this->componentSlots[$attributeName]['component_name'] = $componentName;
  2120. }
  2121. public function hasComponentSlot($name)
  2122. {
  2123. return isset($this->componentSlots[$name]);
  2124. }
  2125. public function getComponentSlot($name)
  2126. {
  2127. if (isset($this->componentSlots[$name]) && $this->componentSlots[$name]['module_name'] && $this->componentSlots[$name]['component_name'])
  2128. {
  2129. return array($this->componentSlots[$name]['module_name'], $this->componentSlots[$name]['component_name']);
  2130. }
  2131. return null;
  2132. }
  2133. public function setTemplate($template)
  2134. {
  2135. if (sfToolkit::isPathAbsolute($template))
  2136. {
  2137. $this->directory = dirname($template);
  2138. $this->template = basename($template);
  2139. }
  2140. else
  2141. {
  2142. $this->directory = $this->context->getConfiguration()->getTemplateDir($this->moduleName, $template);
  2143. $this->template = $template;
  2144. }
  2145. }
  2146. public function getExtension()
  2147. {
  2148. return $this->extension;
  2149. }
  2150. public function setExtension($extension)
  2151. {
  2152. $this->extension = $extension;
  2153. }
  2154. public function getModuleName()
  2155. {
  2156. return $this->moduleName;
  2157. }
  2158. public function getActionName()
  2159. {
  2160. return $this->actionName;
  2161. }
  2162. public function getViewName()
  2163. {
  2164. return $this->viewName;
  2165. }
  2166. public function __call($method, $arguments)
  2167. {
  2168. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'view.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  2169. if (!$event->isProcessed())
  2170. {
  2171. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  2172. }
  2173. return $event->getReturnValue();
  2174. }
  2175. }
  2176. class sfViewParameterHolder extends sfParameterHolder
  2177. {
  2178. protected
  2179. $dispatcher = null,
  2180. $escaping = null,
  2181. $escapingMethod = null;
  2182. public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $options = array())
  2183. {
  2184. $this->initialize($dispatcher, $parameters, $options);
  2185. }
  2186. public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $options = array())
  2187. {
  2188. $this->dispatcher = $dispatcher;
  2189. $this->add($parameters);
  2190. $this->setEscaping(isset($options['escaping_strategy']) ? $options['escaping_strategy'] : false);
  2191. $this->setEscapingMethod(isset($options['escaping_method']) ? $options['escaping_method'] : 'ESC_SPECIALCHARS');
  2192. }
  2193. public function isEscaped()
  2194. {
  2195. return in_array($this->getEscaping(), array('on', true), true);
  2196. }
  2197. public function toArray()
  2198. {
  2199. $event = $this->dispatcher->filter(new sfEvent($this, 'template.filter_parameters'), $this->getAll());
  2200. $parameters = $event->getReturnValue();
  2201. $attributes = array();
  2202. if ($this->isEscaped())
  2203. {
  2204. $attributes['sf_data'] = sfOutputEscaper::escape($this->getEscapingMethod(), $parameters);
  2205. foreach ($attributes['sf_data'] as $key => $value)
  2206. {
  2207. $attributes[$key] = $value;
  2208. }
  2209. }
  2210. else if (in_array($this->getEscaping(), array('off', false), true))
  2211. {
  2212. $attributes = $parameters;
  2213. $attributes['sf_data'] = sfOutputEscaper::escape(ESC_RAW, $parameters);
  2214. }
  2215. else
  2216. {
  2217. throw new InvalidArgumentException(sprintf('Unknown strategy "%s".', $this->getEscaping()));
  2218. }
  2219. return $attributes;
  2220. }
  2221. public function getEscaping()
  2222. {
  2223. return $this->escaping;
  2224. }
  2225. public function setEscaping($escaping)
  2226. {
  2227. $this->escaping = $escaping;
  2228. }
  2229. public function getEscapingMethod()
  2230. {
  2231. if (empty($this->escapingMethod))
  2232. {
  2233. return $this->escapingMethod;
  2234. }
  2235. if (!defined($this->escapingMethod))
  2236. {
  2237. throw new InvalidArgumentException(sprintf('The escaping method "%s" is not available.', $this->escapingMethod));
  2238. }
  2239. return constant($this->escapingMethod);
  2240. }
  2241. public function setEscapingMethod($method)
  2242. {
  2243. $this->escapingMethod = $method;
  2244. }
  2245. public function serialize()
  2246. {
  2247. return serialize(array($this->getAll(), $this->escapingMethod, $this->escaping));
  2248. }
  2249. public function unserialize($serialized)
  2250. {
  2251. list($this->parameters, $escapingMethod, $escaping) = unserialize($serialized);
  2252. $this->initialize(sfContext::hasInstance() ? sfContext::getInstance()->getEventDispatcher() : new sfEventDispatcher());
  2253. $this->setEscapingMethod($escapingMethod);
  2254. $this->setEscaping($escaping);
  2255. }
  2256. }
  2257. abstract class sfWebController extends sfController
  2258. {
  2259. public function genUrl($parameters = array(), $absolute = false)
  2260. {
  2261. if (is_string($parameters) && preg_match('#^[a-z][a-z0-9\+.\-]*\://#i', $parameters))
  2262. {
  2263. return $parameters;
  2264. }
  2265. if (is_string($parameters) && 0 === strpos($parameters, '/'))
  2266. {
  2267. return $parameters;
  2268. }
  2269. if (is_string($parameters) && $parameters == '#')
  2270. {
  2271. return $parameters;
  2272. }
  2273. $route = '';
  2274. $fragment = '';
  2275. if (is_string($parameters))
  2276. {
  2277. if (false !== ($pos = strpos($parameters, '#')))
  2278. {
  2279. $fragment = substr($parameters, $pos + 1);
  2280. $parameters = substr($parameters, 0, $pos);
  2281. }
  2282. list($route, $parameters) = $this->convertUrlStringToParameters($parameters);
  2283. }
  2284. else if (is_array($parameters))
  2285. {
  2286. if (isset($parameters['sf_route']))
  2287. {
  2288. $route = $parameters['sf_route'];
  2289. unset($parameters['sf_route']);
  2290. }
  2291. }
  2292. $url = $this->context->getRouting()->generate($route, $parameters, $absolute);
  2293. if ($fragment)
  2294. {
  2295. $url .= '#'.$fragment;
  2296. }
  2297. return $url;
  2298. }
  2299. public function convertUrlStringToParameters($url)
  2300. {
  2301. $givenUrl = $url;
  2302. $params = array();
  2303. $queryString = '';
  2304. $route = '';
  2305. if (!$url)
  2306. {
  2307. $url = '/';
  2308. }
  2309. if ($pos = strpos($url, '?'))
  2310. {
  2311. $queryString = substr($url, $pos + 1);
  2312. $url = substr($url, 0, $pos);
  2313. }
  2314. if ($url[0] == '/')
  2315. {
  2316. $url = substr($url, 1);
  2317. }
  2318. if ($url[0] == '@')
  2319. {
  2320. $route = substr($url, 1);
  2321. }
  2322. else if (false !== strpos($url, '/'))
  2323. {
  2324. list($params['module'], $params['action']) = explode('/', $url);
  2325. }
  2326. else if (!$queryString)
  2327. {
  2328. $route = $givenUrl;
  2329. }
  2330. else
  2331. {
  2332. throw new InvalidArgumentException(sprintf('An internal URI must contain a module and an action (module/action) ("%s" given).', $givenUrl));
  2333. }
  2334. if ($queryString)
  2335. {
  2336. $matched = preg_match_all('/
  2337. ([^&=]+) # key
  2338. = # =
  2339. (.*?) # value
  2340. (?:
  2341. (?=&[^&=]+=) | $ # followed by another key= or the end of the string
  2342. )
  2343. /x', $queryString, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
  2344. foreach ($matches as $match)
  2345. {
  2346. $params[urldecode($match[1][0])] = urldecode($match[2][0]);
  2347. }
  2348. if (!$matched)
  2349. {
  2350. throw new sfParseException(sprintf('Unable to parse query string "%s".', $queryString));
  2351. }
  2352. }
  2353. return array($route, $params);
  2354. }
  2355. public function redirect($url, $delay = 0, $statusCode = 302)
  2356. {
  2357. $url = $this->genUrl($url, true);
  2358. if (sfConfig::get('sf_logging_enabled'))
  2359. {
  2360. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Redirect to "%s"', $url))));
  2361. }
  2362. $response = $this->context->getResponse();
  2363. $response->clearHttpHeaders();
  2364. $response->setStatusCode($statusCode);
  2365. $response->setHttpHeader('Location', $url);
  2366. $response->setContent(sprintf('<html><head><meta http-equiv="refresh" content="%d;url=%s"/></head></html>', $delay, htmlspecialchars($url, ENT_QUOTES, sfConfig::get('sf_charset'))));
  2367. $response->send();
  2368. }
  2369. }
  2370. class sfFrontWebController extends sfWebController
  2371. {
  2372. public function dispatch()
  2373. {
  2374. try
  2375. {
  2376. sfFilter::$filterCalled = array();
  2377. $request = $this->context->getRequest();
  2378. $moduleName = $request->getParameter('module');
  2379. $actionName = $request->getParameter('action');
  2380. if (empty($moduleName) || empty($actionName))
  2381. {
  2382. throw new sfError404Exception(sprintf('Empty module and/or action after parsing the URL "%s" (%s/%s).', $request->getPathInfo(), $moduleName, $actionName));
  2383. }
  2384. $this->forward($moduleName, $actionName);
  2385. }
  2386. catch (sfException $e)
  2387. {
  2388. $e->printStackTrace();
  2389. }
  2390. catch (Exception $e)
  2391. {
  2392. sfException::createFromException($e)->printStackTrace();
  2393. }
  2394. }
  2395. }
  2396. class sfWebRequest extends sfRequest
  2397. {
  2398. protected
  2399. $languages = null,
  2400. $charsets = null,
  2401. $acceptableContentTypes = null,
  2402. $pathInfoArray = null,
  2403. $relativeUrlRoot = null,
  2404. $getParameters = null,
  2405. $postParameters = null,
  2406. $requestParameters = null,
  2407. $formats = array(),
  2408. $format = null,
  2409. $fixedFileArray = false;
  2410. public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
  2411. {
  2412. parent::initialize($dispatcher, $parameters, $attributes, $options);
  2413. $this->getParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_GET) : $_GET;
  2414. $this->parameterHolder->add($this->getParameters);
  2415. $this->postParameters = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_POST) : $_POST;
  2416. $this->parameterHolder->add($this->postParameters);
  2417. if (isset($_SERVER['REQUEST_METHOD']))
  2418. {
  2419. switch ($_SERVER['REQUEST_METHOD'])
  2420. {
  2421. case 'GET':
  2422. $this->setMethod(self::GET);
  2423. break;
  2424. case 'POST':
  2425. $this->setMethod(strtoupper($this->getParameter('sf_method', 'POST')));
  2426. $this->parameterHolder->remove('sf_method');
  2427. break;
  2428. case 'PUT':
  2429. $this->setMethod(self::PUT);
  2430. break;
  2431. case 'DELETE':
  2432. $this->setMethod(self::DELETE);
  2433. break;
  2434. case 'HEAD':
  2435. $this->setMethod(self::HEAD);
  2436. break;
  2437. default:
  2438. $this->setMethod(self::GET);
  2439. }
  2440. }
  2441. else
  2442. {
  2443. $this->setMethod(self::GET);
  2444. }
  2445. if (isset($this->options['formats']))
  2446. {
  2447. foreach ($this->options['formats'] as $format => $mimeTypes)
  2448. {
  2449. $this->setFormat($format, $mimeTypes);
  2450. }
  2451. }
  2452. if (!isset($this->options['path_info_key']))
  2453. {
  2454. $this->options['path_info_key'] = 'PATH_INFO';
  2455. }
  2456. if (!isset($this->options['path_info_array']))
  2457. {
  2458. $this->options['path_info_array'] = 'SERVER';
  2459. }
  2460. $this->requestParameters = $this->parseRequestParameters();
  2461. $this->parameterHolder->add($this->requestParameters);
  2462. $this->fixParameters();
  2463. }
  2464. public function getUri()
  2465. {
  2466. $pathArray = $this->getPathInfoArray();
  2467. if ('HTTP_X_REWRITE_URL' == $this->options['path_info_key'])
  2468. {
  2469. $uri = isset($pathArray['HTTP_X_REWRITE_URL']) ? $pathArray['HTTP_X_REWRITE_URL'] : '';
  2470. }
  2471. else
  2472. {
  2473. $uri = isset($pathArray['REQUEST_URI']) ? $pathArray['REQUEST_URI'] : '';
  2474. }
  2475. return $this->isAbsUri() ? $uri : $this->getUriPrefix().$uri;
  2476. }
  2477. public function isAbsUri()
  2478. {
  2479. $pathArray = $this->getPathInfoArray();
  2480. return isset($pathArray['REQUEST_URI']) ? preg_match('/^http/', $pathArray['REQUEST_URI']) : false;
  2481. }
  2482. public function getUriPrefix()
  2483. {
  2484. $pathArray = $this->getPathInfoArray();
  2485. if ($this->isSecure())
  2486. {
  2487. $standardPort = '443';
  2488. $protocol = 'https';
  2489. }
  2490. else
  2491. {
  2492. $standardPort = '80';
  2493. $protocol = 'http';
  2494. }
  2495. $host = explode(":", $this->getHost());
  2496. if (count($host) == 1)
  2497. {
  2498. $host[] = isset($pathArray['SERVER_PORT']) ? $pathArray['SERVER_PORT'] : '';
  2499. }
  2500. if ($host[1] == $standardPort || empty($host[1]))
  2501. {
  2502. unset($host[1]);
  2503. }
  2504. return $protocol.'://'.implode(':', $host);;
  2505. }
  2506. public function getPathInfo()
  2507. {
  2508. $pathInfo = '';
  2509. $pathArray = $this->getPathInfoArray();
  2510. $sf_path_info_key = $this->options['path_info_key'];
  2511. if (!isset($pathArray[$sf_path_info_key]) || !$pathArray[$sf_path_info_key])
  2512. {
  2513. if (isset($pathArray['REQUEST_URI']))
  2514. {
  2515. $script_name = $this->getScriptName();
  2516. $uri_prefix = $this->isAbsUri() ? $this->getUriPrefix() : '';
  2517. $pathInfo = preg_replace('/^'.preg_quote($uri_prefix, '/').'/','',$pathArray['REQUEST_URI']);
  2518. $pathInfo = preg_replace('/^'.preg_quote($script_name, '/').'/', '', $pathInfo);
  2519. $prefix_name = preg_replace('#/[^/]+$#', '', $script_name);
  2520. $pathInfo = preg_replace('/^'.preg_quote($prefix_name, '/').'/', '', $pathInfo);
  2521. $pathInfo = preg_replace('/\??'.preg_quote($pathArray['QUERY_STRING'], '/').'$/', '', $pathInfo);
  2522. }
  2523. }
  2524. else
  2525. {
  2526. $pathInfo = $pathArray[$sf_path_info_key];
  2527. if ($relativeUrlRoot = $this->getRelativeUrlRoot())
  2528. {
  2529. $pathInfo = preg_replace('/^'.str_replace('/', '\\/', $relativeUrlRoot).'\//', '', $pathInfo);
  2530. }
  2531. }
  2532. if (isset($_SERVER['SERVER_SOFTWARE']) && false !== stripos($_SERVER['SERVER_SOFTWARE'], 'iis') && $pos = stripos($pathInfo, '.php'))
  2533. {
  2534. $pathInfo = substr($pathInfo, $pos + 4);
  2535. }
  2536. if (!$pathInfo)
  2537. {
  2538. $pathInfo = '/';
  2539. }
  2540. return $pathInfo;
  2541. }
  2542. public function getPathInfoPrefix()
  2543. {
  2544. $prefix = $this->getRelativeUrlRoot();
  2545. if (!isset($this->options['no_script_name']) || !$this->options['no_script_name'])
  2546. {
  2547. $scriptName = $this->getScriptName();
  2548. $prefix = is_null($prefix) ? $scriptName : $prefix.'/'.basename($scriptName);
  2549. }
  2550. return $prefix;
  2551. }
  2552. public function getGetParameters()
  2553. {
  2554. return $this->getParameters;
  2555. }
  2556. public function getPostParameters()
  2557. {
  2558. return $this->postParameters;
  2559. }
  2560. public function getRequestParameters()
  2561. {
  2562. return $this->requestParameters;
  2563. }
  2564. public function addRequestParameters($parameters)
  2565. {
  2566. $this->requestParameters = array_merge($this->requestParameters, $parameters);
  2567. $this->getParameterHolder()->add($parameters);
  2568. $this->fixParameters();
  2569. }
  2570. public function getReferer()
  2571. {
  2572. $pathArray = $this->getPathInfoArray();
  2573. return isset($pathArray['HTTP_REFERER']) ? $pathArray['HTTP_REFERER'] : '';
  2574. }
  2575. public function getHost()
  2576. {
  2577. $pathArray = $this->getPathInfoArray();
  2578. return isset($pathArray['HTTP_X_FORWARDED_HOST']) ? $pathArray['HTTP_X_FORWARDED_HOST'] : (isset($pathArray['HTTP_HOST']) ? $pathArray['HTTP_HOST'] : '');
  2579. }
  2580. public function getScriptName()
  2581. {
  2582. $pathArray = $this->getPathInfoArray();
  2583. return isset($pathArray['SCRIPT_NAME']) ? $pathArray['SCRIPT_NAME'] : (isset($pathArray['ORIG_SCRIPT_NAME']) ? $pathArray['ORIG_SCRIPT_NAME'] : '');
  2584. }
  2585. public function isMethod($method)
  2586. {
  2587. return strtoupper($method) == $this->getMethod();
  2588. }
  2589. public function getMethodName()
  2590. {
  2591. if ($this->options['logging'])
  2592. {
  2593. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('The "sfWebRequest::getMethodName()" method is deprecated, please use "getMethod()" instead.', 'priority' => sfLogger::WARNING)));
  2594. }
  2595. return $this->getMethod();
  2596. }
  2597. public function getPreferredCulture(array $cultures = null)
  2598. {
  2599. $preferredCultures = $this->getLanguages();
  2600. if (is_null($cultures))
  2601. {
  2602. return isset($preferredCultures[0]) ? $preferredCultures[0] : null;
  2603. }
  2604. if (!$preferredCultures)
  2605. {
  2606. return $cultures[0];
  2607. }
  2608. $preferredCultures = array_values(array_intersect($preferredCultures, $cultures));
  2609. return isset($preferredCultures[0]) ? $preferredCultures[0] : $cultures[0];
  2610. }
  2611. public function getLanguages()
  2612. {
  2613. if ($this->languages)
  2614. {
  2615. return $this->languages;
  2616. }
  2617. if (!isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  2618. {
  2619. return array();
  2620. }
  2621. $languages = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  2622. foreach ($languages as $lang)
  2623. {
  2624. if (strstr($lang, '-'))
  2625. {
  2626. $codes = explode('-', $lang);
  2627. if ($codes[0] == 'i')
  2628. {
  2629. if (count($codes) > 1)
  2630. {
  2631. $lang = $codes[1];
  2632. }
  2633. }
  2634. else
  2635. {
  2636. for ($i = 0, $max = count($codes); $i < $max; $i++)
  2637. {
  2638. if ($i == 0)
  2639. {
  2640. $lang = strtolower($codes[0]);
  2641. }
  2642. else
  2643. {
  2644. $lang .= '_'.strtoupper($codes[$i]);
  2645. }
  2646. }
  2647. }
  2648. }
  2649. $this->languages[] = $lang;
  2650. }
  2651. return $this->languages;
  2652. }
  2653. public function getCharsets()
  2654. {
  2655. if ($this->charsets)
  2656. {
  2657. return $this->charsets;
  2658. }
  2659. if (!isset($_SERVER['HTTP_ACCEPT_CHARSET']))
  2660. {
  2661. return array();
  2662. }
  2663. $this->charsets = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT_CHARSET']);
  2664. return $this->charsets;
  2665. }
  2666. public function getAcceptableContentTypes()
  2667. {
  2668. if ($this->acceptableContentTypes)
  2669. {
  2670. return $this->acceptableContentTypes;
  2671. }
  2672. if (!isset($_SERVER['HTTP_ACCEPT']))
  2673. {
  2674. return array();
  2675. }
  2676. $this->acceptableContentTypes = $this->splitHttpAcceptHeader($_SERVER['HTTP_ACCEPT']);
  2677. return $this->acceptableContentTypes;
  2678. }
  2679. public function isXmlHttpRequest()
  2680. {
  2681. return ($this->getHttpHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
  2682. }
  2683. public function getHttpHeader($name, $prefix = 'http')
  2684. {
  2685. if ($prefix)
  2686. {
  2687. $prefix = strtoupper($prefix).'_';
  2688. }
  2689. $name = $prefix.strtoupper(strtr($name, '-', '_'));
  2690. $pathArray = $this->getPathInfoArray();
  2691. return isset($pathArray[$name]) ? sfToolkit::stripslashesDeep($pathArray[$name]) : null;
  2692. }
  2693. public function getCookie($name, $defaultValue = null)
  2694. {
  2695. $retval = $defaultValue;
  2696. if (isset($_COOKIE[$name]))
  2697. {
  2698. $retval = get_magic_quotes_gpc() ? sfToolkit::stripslashesDeep($_COOKIE[$name]) : $_COOKIE[$name];
  2699. }
  2700. return $retval;
  2701. }
  2702. public function isSecure()
  2703. {
  2704. $pathArray = $this->getPathInfoArray();
  2705. return (
  2706. (isset($pathArray['HTTPS']) && (strtolower($pathArray['HTTPS']) == 'on' || $pathArray['HTTPS'] == 1))
  2707. ||
  2708. (isset($pathArray['HTTP_SSL_HTTPS']) && (strtolower($pathArray['HTTP_SSL_HTTPS']) == 'on' || $pathArray['HTTP_SSL_HTTPS'] == 1))
  2709. ||
  2710. (isset($pathArray['HTTP_X_FORWARDED_PROTO']) && strtolower($pathArray['HTTP_X_FORWARDED_PROTO']) == 'https')
  2711. );
  2712. }
  2713. public function getRelativeUrlRoot()
  2714. {
  2715. if (is_null($this->relativeUrlRoot))
  2716. {
  2717. if (!isset($this->options['relative_url_root']))
  2718. {
  2719. $this->relativeUrlRoot = preg_replace('#/[^/]+\.php5?$#', '', $this->getScriptName());
  2720. }
  2721. else
  2722. {
  2723. $this->relativeUrlRoot = $this->options['relative_url_root'];
  2724. }
  2725. }
  2726. return $this->relativeUrlRoot;
  2727. }
  2728. public function setRelativeUrlRoot($value)
  2729. {
  2730. $this->relativeUrlRoot = $value;
  2731. }
  2732. public function splitHttpAcceptHeader($header)
  2733. {
  2734. $values = array();
  2735. foreach (array_filter(explode(',', $header)) as $value)
  2736. {
  2737. if ($pos = strpos($value, ';'))
  2738. {
  2739. $q = (float) trim(substr($value, $pos + 3));
  2740. $value = trim(substr($value, 0, $pos));
  2741. }
  2742. else
  2743. {
  2744. $q = 1;
  2745. }
  2746. $values[$value] = $q;
  2747. }
  2748. arsort($values);
  2749. return array_keys($values);
  2750. }
  2751. public function getPathInfoArray()
  2752. {
  2753. if (!$this->pathInfoArray)
  2754. {
  2755. switch ($this->options['path_info_array'])
  2756. {
  2757. case 'SERVER':
  2758. $this->pathInfoArray =& $_SERVER;
  2759. break;
  2760. case 'ENV':
  2761. default:
  2762. $this->pathInfoArray =& $_ENV;
  2763. }
  2764. }
  2765. return $this->pathInfoArray;
  2766. }
  2767. public function getMimeType($format)
  2768. {
  2769. return isset($this->formats[$format]) ? $this->formats[$format][0] : null;
  2770. }
  2771. public function getFormat($mimeType)
  2772. {
  2773. foreach ($this->formats as $format => $mimeTypes)
  2774. {
  2775. if (in_array($mimeType, $mimeTypes))
  2776. {
  2777. return $format;
  2778. }
  2779. }
  2780. return null;
  2781. }
  2782. public function setFormat($format, $mimeTypes)
  2783. {
  2784. $this->formats[$format] = is_array($mimeTypes) ? $mimeTypes : array($mimeTypes);
  2785. }
  2786. public function setRequestFormat($format)
  2787. {
  2788. $this->format = $format;
  2789. }
  2790. public function getRequestFormat()
  2791. {
  2792. if (is_null($this->format))
  2793. {
  2794. $this->setRequestFormat($this->getParameter('sf_format'));
  2795. }
  2796. return $this->format;
  2797. }
  2798. public function getFiles($key = null)
  2799. {
  2800. if (false === $this->fixedFileArray)
  2801. {
  2802. $this->fixedFileArray = self::convertFileInformation($_FILES);
  2803. }
  2804. return is_null($key) ? $this->fixedFileArray : (isset($this->fixedFileArray[$key]) ? $this->fixedFileArray[$key] : array());
  2805. }
  2806. static public function convertFileInformation(array $taintedFiles)
  2807. {
  2808. $files = array();
  2809. foreach ($taintedFiles as $key => $data)
  2810. {
  2811. $files[$key] = self::fixPhpFilesArray($data);
  2812. }
  2813. return $files;
  2814. }
  2815. static protected function fixPhpFilesArray($data)
  2816. {
  2817. $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
  2818. $keys = array_keys($data);
  2819. sort($keys);
  2820. if ($fileKeys != $keys || !isset($data['name']) || !is_array($data['name']))
  2821. {
  2822. return $data;
  2823. }
  2824. $files = $data;
  2825. foreach ($fileKeys as $k)
  2826. {
  2827. unset($files[$k]);
  2828. }
  2829. foreach (array_keys($data['name']) as $key)
  2830. {
  2831. $files[$key] = self::fixPhpFilesArray(array(
  2832. 'error' => $data['error'][$key],
  2833. 'name' => $data['name'][$key],
  2834. 'type' => $data['type'][$key],
  2835. 'tmp_name' => $data['tmp_name'][$key],
  2836. 'size' => $data['size'][$key],
  2837. ));
  2838. }
  2839. return $files;
  2840. }
  2841. public function getGetParameter($name, $default = null)
  2842. {
  2843. if (isset($this->getParameters[$name]))
  2844. {
  2845. return $this->getParameters[$name];
  2846. }
  2847. else
  2848. {
  2849. return sfToolkit::getArrayValueForPath($this->getParameters, $name, $default);
  2850. }
  2851. }
  2852. public function getPostParameter($name, $default = null)
  2853. {
  2854. if (isset($this->postParameters[$name]))
  2855. {
  2856. return $this->postParameters[$name];
  2857. }
  2858. else
  2859. {
  2860. return sfToolkit::getArrayValueForPath($this->postParameters, $name, $default);
  2861. }
  2862. }
  2863. public function getUrlParameter($name, $default = null)
  2864. {
  2865. if (isset($this->requestParameters[$name]))
  2866. {
  2867. return $this->requestParameters[$name];
  2868. }
  2869. else
  2870. {
  2871. return sfToolkit::getArrayValueForPath($this->requestParameters, $name, $default);
  2872. }
  2873. }
  2874. public function getRemoteAddress()
  2875. {
  2876. $pathInfo = $this->getPathInfoArray();
  2877. return $pathInfo['REMOTE_ADDR'];
  2878. }
  2879. public function getForwardedFor()
  2880. {
  2881. $pathInfo = $this->getPathInfoArray();
  2882. if (empty($pathInfo['HTTP_X_FORWARDED_FOR']))
  2883. {
  2884. return null;
  2885. }
  2886. return explode(', ', $pathInfo['HTTP_X_FORWARDED_FOR']);
  2887. }
  2888. public function checkCSRFProtection()
  2889. {
  2890. $form = new sfForm();
  2891. $form->bind($form->isCSRFProtected() ? array($form->getCSRFFieldName() => $this->getParameter($form->getCSRFFieldName())) : array());
  2892. if (!$form->isValid())
  2893. {
  2894. throw $form->getErrorSchema();
  2895. }
  2896. }
  2897. protected function parseRequestParameters()
  2898. {
  2899. return $this->dispatcher->filter(new sfEvent($this, 'request.filter_parameters', $this->getRequestContext()), array())->getReturnValue();
  2900. }
  2901. public function getRequestContext()
  2902. {
  2903. return array(
  2904. 'path_info' => $this->getPathInfo(),
  2905. 'prefix' => $this->getPathInfoPrefix(),
  2906. 'method' => $this->getMethod(),
  2907. 'format' => $this->getRequestFormat(),
  2908. 'host' => $this->getHost(),
  2909. 'is_secure' => $this->isSecure(),
  2910. 'request_uri' => $this->getUri(),
  2911. );
  2912. }
  2913. protected function fixParameters()
  2914. {
  2915. foreach ($this->parameterHolder->getAll() as $key => $value)
  2916. {
  2917. if (0 === stripos($key, '_sf_'))
  2918. {
  2919. $this->parameterHolder->remove($key);
  2920. $this->setAttribute(substr($key, 1), $value);
  2921. }
  2922. }
  2923. }
  2924. }
  2925. class sfPatternRouting extends sfRouting
  2926. {
  2927. protected
  2928. $currentRouteName = null,
  2929. $currentInternalUri = array(),
  2930. $routes = array(),
  2931. $cacheData = array(),
  2932. $cacheChanged = false,
  2933. $routesFullyLoaded = true;
  2934. public function initialize(sfEventDispatcher $dispatcher, sfCache $cache = null, $options = array())
  2935. {
  2936. $options = array_merge(array(
  2937. 'variable_prefixes' => array(':'),
  2938. 'segment_separators' => array('/', '.'),
  2939. 'variable_regex' => '[\w\d_]+',
  2940. 'load_configuration' => false,
  2941. 'suffix' => '',
  2942. 'generate_shortest_url' => true,
  2943. 'extra_parameters_as_query_string' => true,
  2944. 'lazy_routes_deserialize' => false,
  2945. 'lookup_cache_dedicated_keys' => false,
  2946. ), $options);
  2947. if ('.' == $options['suffix'])
  2948. {
  2949. $options['suffix'] = '';
  2950. }
  2951. parent::initialize($dispatcher, $cache, $options);
  2952. if (!is_null($this->cache) && !$options['lookup_cache_dedicated_keys'] && $cacheData = $this->cache->get('symfony.routing.data'))
  2953. {
  2954. $this->cacheData = unserialize($cacheData);
  2955. }
  2956. }
  2957. public function loadConfiguration()
  2958. {
  2959. if (!is_null($this->cache) && $routes = $this->cache->get('symfony.routing.configuration'))
  2960. {
  2961. $this->routes = unserialize($routes);
  2962. $this->routesFullyLoaded = false;
  2963. }
  2964. else
  2965. {
  2966. if ($this->options['load_configuration'] && $config = sfContext::getInstance()->getConfigCache()->checkConfig('config/routing.yml', true))
  2967. {
  2968. $this->setRoutes(include($config));
  2969. }
  2970. parent::loadConfiguration();
  2971. if (!is_null($this->cache))
  2972. {
  2973. if (!$this->options['lazy_routes_deserialize'])
  2974. {
  2975. $this->cache->set('symfony.routing.configuration', serialize($this->routes));
  2976. }
  2977. else
  2978. {
  2979. $lazyMap = array();
  2980. foreach ($this->routes as $name => $route)
  2981. {
  2982. if (is_string($route))
  2983. {
  2984. $route = $this->loadRoute($name);
  2985. }
  2986. $lazyMap[$name] = serialize($route);
  2987. }
  2988. $this->cache->set('symfony.routing.configuration', serialize($lazyMap));
  2989. }
  2990. }
  2991. }
  2992. }
  2993. protected function loadRoute($name)
  2994. {
  2995. if (is_string($route = $this->routes[$name]))
  2996. {
  2997. $this->routes[$name] = unserialize($route);
  2998. $this->routes[$name]->setDefaultParameters($this->defaultParameters);
  2999. return $this->routes[$name];
  3000. }
  3001. else
  3002. {
  3003. return $route;
  3004. }
  3005. }
  3006. protected function loadRoutes()
  3007. {
  3008. if ($this->routesFullyLoaded)
  3009. {
  3010. return;
  3011. }
  3012. foreach ($this->routes as $name => $route)
  3013. {
  3014. if (is_string($route))
  3015. {
  3016. $this->loadRoute($name);
  3017. }
  3018. }
  3019. $this->routesFullyLoaded = true;
  3020. }
  3021. public function getCurrentInternalUri($withRouteName = false)
  3022. {
  3023. return is_null($this->currentRouteName) ? null : $this->currentInternalUri[$withRouteName ? 0 : 1];
  3024. }
  3025. public function getCurrentRouteName()
  3026. {
  3027. return $this->currentRouteName;
  3028. }
  3029. public function getRoutes()
  3030. {
  3031. if (!$this->routesFullyLoaded)
  3032. {
  3033. $this->loadRoutes();
  3034. }
  3035. return $this->routes;
  3036. }
  3037. public function setRoutes($routes)
  3038. {
  3039. foreach ($routes as $name => $route)
  3040. {
  3041. $this->connect($name, $route);
  3042. }
  3043. if (!$this->routesFullyLoaded)
  3044. {
  3045. $this->loadRoutes();
  3046. }
  3047. return $this->routes;
  3048. }
  3049. public function hasRoutes()
  3050. {
  3051. return count($this->routes) ? true : false;
  3052. }
  3053. public function clearRoutes()
  3054. {
  3055. if ($this->options['logging'])
  3056. {
  3057. $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Clear all current routes')));
  3058. }
  3059. $this->routes = array();
  3060. }
  3061. public function hasRouteName($name)
  3062. {
  3063. return isset($this->routes[$name]) ? true : false;
  3064. }
  3065. public function prependRoute($name, $route)
  3066. {
  3067. $routes = $this->routes;
  3068. $this->routes = array();
  3069. $newroutes = $this->connect($name, $route);
  3070. $this->routes = array_merge($newroutes, $routes);
  3071. if (!$this->routesFullyLoaded)
  3072. {
  3073. $this->loadRoutes();
  3074. }
  3075. return $this->routes;
  3076. }
  3077. public function appendRoute($name, $route)
  3078. {
  3079. return $this->connect($name, $route);
  3080. }
  3081. public function insertRouteBefore($pivot, $name, $route)
  3082. {
  3083. if (!isset($this->routes[$pivot]))
  3084. {
  3085. throw new sfConfigurationException(sprintf('Unable to insert route "%s" before inexistent route "%s".', $name, $pivot));
  3086. }
  3087. $routes = $this->routes;
  3088. $this->routes = array();
  3089. $newroutes = array();
  3090. foreach ($routes as $key => $value)
  3091. {
  3092. if ($key == $pivot)
  3093. {
  3094. $newroutes = array_merge($newroutes, $this->connect($name, $route));
  3095. }
  3096. $newroutes[$key] = $value;
  3097. }
  3098. $this->routes = $newroutes;
  3099. if (!$this->routesFullyLoaded)
  3100. {
  3101. $this->loadRoutes();
  3102. }
  3103. return $this->routes;
  3104. }
  3105. public function connect($name, $route)
  3106. {
  3107. $routes = $route instanceof sfRouteCollection ? $route : array($name => $route);
  3108. foreach (self::flattenRoutes($routes) as $name => $route)
  3109. {
  3110. $this->routes[$name] = $route;
  3111. $this->configureRoute($route);
  3112. if ($this->options['logging'])
  3113. {
  3114. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Connect %s "%s" (%s)', get_class($route), $name, $route->getPattern()))));
  3115. }
  3116. }
  3117. if (!$this->routesFullyLoaded)
  3118. {
  3119. $this->loadRoutes();
  3120. }
  3121. return $this->routes;
  3122. }
  3123. public function configureRoute(sfRoute $route)
  3124. {
  3125. $route->setDefaultParameters($this->defaultParameters);
  3126. $route->setDefaultOptions($this->options);
  3127. }
  3128. public function generate($name, $params = array(), $absolute = false)
  3129. {
  3130. if (!is_null($this->cache))
  3131. {
  3132. $cacheKey = 'generate_'.$name.'_'.md5(serialize(array_merge($this->defaultParameters, $params))).'_'.md5(serialize($this->options['context']));
  3133. if ($this->options['lookup_cache_dedicated_keys'] && $url = $this->cache->get('symfony.routing.data.'.$cacheKey))
  3134. {
  3135. return $this->fixGeneratedUrl($url, $absolute);
  3136. }
  3137. elseif (isset($this->cacheData[$cacheKey]))
  3138. {
  3139. return $this->fixGeneratedUrl($this->cacheData[$cacheKey], $absolute);
  3140. }
  3141. }
  3142. if ($name)
  3143. {
  3144. if (!isset($this->routes[$name]))
  3145. {
  3146. throw new sfConfigurationException(sprintf('The route "%s" does not exist.', $name));
  3147. }
  3148. $route = $this->routes[$name];
  3149. if (is_string($route))
  3150. {
  3151. $route = $this->loadRoute($name);
  3152. }
  3153. $route->setDefaultParameters($this->defaultParameters);
  3154. }
  3155. else
  3156. {
  3157. if (false === $route = $this->getRouteThatMatchesParameters($params, $this->options['context']))
  3158. {
  3159. throw new sfConfigurationException(sprintf('Unable to find a matching route to generate url for params "%s".', is_object($params) ? 'Object('.get_class($params).')' : str_replace("\n", '', var_export($params, true))));
  3160. }
  3161. }
  3162. $url = $route->generate($params, $this->options['context'], $absolute);
  3163. if (!is_null($this->cache))
  3164. {
  3165. if ($this->options['lookup_cache_dedicated_keys'])
  3166. {
  3167. $this->cache->set('symfony.routing.data.'.$cacheKey, $url);
  3168. }
  3169. else
  3170. {
  3171. $this->cacheChanged = true;
  3172. $this->cacheData[$cacheKey] = $url;
  3173. }
  3174. }
  3175. return $this->fixGeneratedUrl($url, $absolute);
  3176. }
  3177. public function parse($url)
  3178. {
  3179. if (false === $info = $this->findRoute($url))
  3180. {
  3181. $this->currentRouteName = null;
  3182. $this->currentInternalUri = array();
  3183. return false;
  3184. }
  3185. if ($this->options['logging'])
  3186. {
  3187. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Match route "%s" (%s) for %s with parameters %s', $info['name'], $info['pattern'], $url, str_replace("\n", '', var_export($info['parameters'], true))))));
  3188. }
  3189. $this->updateCurrentInternalUri($info['name'], $info['parameters']);
  3190. $route = $this->routes[$info['name']];
  3191. if (is_string($route))
  3192. {
  3193. $route = $this->loadRoute($info['name']);
  3194. }
  3195. $route->setDefaultParameters($this->defaultParameters);
  3196. $route->bind($this->options['context'], $info['parameters']);
  3197. $info['parameters']['_sf_route'] = $route;
  3198. return $info['parameters'];
  3199. }
  3200. protected function updateCurrentInternalUri($name, array $parameters)
  3201. {
  3202. $this->currentRouteName = $name;
  3203. $internalUri = array('@'.$this->currentRouteName, $parameters['module'].'/'.$parameters['action']);
  3204. unset($parameters['module'], $parameters['action']);
  3205. $params = array();
  3206. foreach ($parameters as $key => $value)
  3207. {
  3208. $params[] = $key.'='.$value;
  3209. }
  3210. sort($params);
  3211. $params = $params ? '?'.implode('&', $params) : '';
  3212. $this->currentInternalUri = array($internalUri[0].$params, $internalUri[1].$params);
  3213. }
  3214. public function findRoute($url)
  3215. {
  3216. $url = $this->normalizeUrl($url);
  3217. if (!is_null($this->cache))
  3218. {
  3219. $cacheKey = 'parse_'.$url.'_'.md5(serialize($this->options['context']));
  3220. if ($this->options['lookup_cache_dedicated_keys'] && $info = $this->cache->get('symfony.routing.data.'.$cacheKey))
  3221. {
  3222. return unserialize($info);
  3223. }
  3224. elseif (isset($this->cacheData[$cacheKey]))
  3225. {
  3226. return $this->cacheData[$cacheKey];
  3227. }
  3228. }
  3229. $info = $this->getRouteThatMatchesUrl($url);
  3230. if (!is_null($this->cache))
  3231. {
  3232. if ($this->options['lookup_cache_dedicated_keys'])
  3233. {
  3234. $this->cache->set('symfony.routing.data.'.$cacheKey, serialize($info));
  3235. }
  3236. else
  3237. {
  3238. $this->cacheChanged = true;
  3239. $this->cacheData[$cacheKey] = $info;
  3240. }
  3241. }
  3242. return $info;
  3243. }
  3244. static public function flattenRoutes($routes)
  3245. {
  3246. $flattenRoutes = array();
  3247. foreach ($routes as $name => $route)
  3248. {
  3249. if ($route instanceof sfRouteCollection)
  3250. {
  3251. $flattenRoutes = array_merge($flattenRoutes, self::flattenRoutes($route));
  3252. }
  3253. else
  3254. {
  3255. $flattenRoutes[$name] = $route;
  3256. }
  3257. }
  3258. return $flattenRoutes;
  3259. }
  3260. protected function getRouteThatMatchesUrl($url)
  3261. {
  3262. foreach ($this->routes as $name => $route)
  3263. {
  3264. if (is_string($route))
  3265. {
  3266. $route = $this->loadRoute($name);
  3267. }
  3268. $route->setDefaultParameters($this->defaultParameters);
  3269. if (false === $parameters = $route->matchesUrl($url, $this->options['context']))
  3270. {
  3271. continue;
  3272. }
  3273. return array('name' => $name, 'pattern' => $route->getPattern(), 'parameters' => $parameters);
  3274. }
  3275. return false;
  3276. }
  3277. protected function getRouteThatMatchesParameters($parameters)
  3278. {
  3279. foreach ($this->routes as $name => $route)
  3280. {
  3281. if (is_string($route))
  3282. {
  3283. $route = $this->loadRoute($name);
  3284. }
  3285. $route->setDefaultParameters($this->defaultParameters);
  3286. if ($route->matchesParameters($parameters, $this->options['context']))
  3287. {
  3288. return $route;
  3289. }
  3290. }
  3291. return false;
  3292. }
  3293. protected function normalizeUrl($url)
  3294. {
  3295. if ('/' != substr($url, 0, 1))
  3296. {
  3297. $url = '/'.$url;
  3298. }
  3299. if (false !== $pos = strpos($url, '?'))
  3300. {
  3301. $url = substr($url, 0, $pos);
  3302. }
  3303. $url = preg_replace('#/+#', '/', $url);
  3304. return $url;
  3305. }
  3306. public function shutdown()
  3307. {
  3308. if (!is_null($this->cache) && $this->cacheChanged)
  3309. {
  3310. $this->cacheChanged = false;
  3311. $this->cache->set('symfony.routing.data', serialize($this->cacheData));
  3312. }
  3313. }
  3314. }
  3315. class sfWebResponse extends sfResponse
  3316. {
  3317. const
  3318. FIRST = 'first',
  3319. MIDDLE = '',
  3320. LAST = 'last',
  3321. ALL = 'ALL',
  3322. RAW = 'RAW';
  3323. protected
  3324. $cookies = array(),
  3325. $statusCode = 200,
  3326. $statusText = 'OK',
  3327. $headerOnly = false,
  3328. $headers = array(),
  3329. $metas = array(),
  3330. $httpMetas = array(),
  3331. $positions = array('first', '', 'last'),
  3332. $stylesheets = array(),
  3333. $javascripts = array(),
  3334. $slots = array();
  3335. static protected $statusTexts = array(
  3336. '100' => 'Continue',
  3337. '101' => 'Switching Protocols',
  3338. '200' => 'OK',
  3339. '201' => 'Created',
  3340. '202' => 'Accepted',
  3341. '203' => 'Non-Authoritative Information',
  3342. '204' => 'No Content',
  3343. '205' => 'Reset Content',
  3344. '206' => 'Partial Content',
  3345. '300' => 'Multiple Choices',
  3346. '301' => 'Moved Permanently',
  3347. '302' => 'Found',
  3348. '303' => 'See Other',
  3349. '304' => 'Not Modified',
  3350. '305' => 'Use Proxy',
  3351. '306' => '(Unused)',
  3352. '307' => 'Temporary Redirect',
  3353. '400' => 'Bad Request',
  3354. '401' => 'Unauthorized',
  3355. '402' => 'Payment Required',
  3356. '403' => 'Forbidden',
  3357. '404' => 'Not Found',
  3358. '405' => 'Method Not Allowed',
  3359. '406' => 'Not Acceptable',
  3360. '407' => 'Proxy Authentication Required',
  3361. '408' => 'Request Timeout',
  3362. '409' => 'Conflict',
  3363. '410' => 'Gone',
  3364. '411' => 'Length Required',
  3365. '412' => 'Precondition Failed',
  3366. '413' => 'Request Entity Too Large',
  3367. '414' => 'Request-URI Too Long',
  3368. '415' => 'Unsupported Media Type',
  3369. '416' => 'Requested Range Not Satisfiable',
  3370. '417' => 'Expectation Failed',
  3371. '500' => 'Internal Server Error',
  3372. '501' => 'Not Implemented',
  3373. '502' => 'Bad Gateway',
  3374. '503' => 'Service Unavailable',
  3375. '504' => 'Gateway Timeout',
  3376. '505' => 'HTTP Version Not Supported',
  3377. );
  3378. public function initialize(sfEventDispatcher $dispatcher, $options = array())
  3379. {
  3380. parent::initialize($dispatcher, $options);
  3381. $this->javascripts = array_combine($this->positions, array_fill(0, count($this->positions), array()));
  3382. $this->stylesheets = array_combine($this->positions, array_fill(0, count($this->positions), array()));
  3383. if (!isset($this->options['charset']))
  3384. {
  3385. $this->options['charset'] = 'utf-8';
  3386. }
  3387. if (!isset($this->options['send_http_headers']))
  3388. {
  3389. $this->options['send_http_headers'] = true;
  3390. }
  3391. if (!isset($this->options['http_protocol']))
  3392. {
  3393. $this->options['http_protocol'] = 'HTTP/1.0';
  3394. }
  3395. $this->options['content_type'] = $this->fixContentType(isset($this->options['content_type']) ? $this->options['content_type'] : 'text/html');
  3396. }
  3397. public function setHeaderOnly($value = true)
  3398. {
  3399. $this->headerOnly = (boolean) $value;
  3400. }
  3401. public function isHeaderOnly()
  3402. {
  3403. return $this->headerOnly;
  3404. }
  3405. public function setCookie($name, $value, $expire = null, $path = '/', $domain = '', $secure = false, $httpOnly = false)
  3406. {
  3407. if ($expire !== null)
  3408. {
  3409. if (is_numeric($expire))
  3410. {
  3411. $expire = (int) $expire;
  3412. }
  3413. else
  3414. {
  3415. $expire = strtotime($expire);
  3416. if ($expire === false || $expire == -1)
  3417. {
  3418. throw new sfException('Your expire parameter is not valid.');
  3419. }
  3420. }
  3421. }
  3422. $this->cookies[$name] = array(
  3423. 'name' => $name,
  3424. 'value' => $value,
  3425. 'expire' => $expire,
  3426. 'path' => $path,
  3427. 'domain' => $domain,
  3428. 'secure' => $secure ? true : false,
  3429. 'httpOnly' => $httpOnly,
  3430. );
  3431. }
  3432. public function setStatusCode($code, $name = null)
  3433. {
  3434. $this->statusCode = $code;
  3435. $this->statusText = null !== $name ? $name : self::$statusTexts[$code];
  3436. }
  3437. public function getStatusText()
  3438. {
  3439. return $this->statusText;
  3440. }
  3441. public function getStatusCode()
  3442. {
  3443. return $this->statusCode;
  3444. }
  3445. public function setHttpHeader($name, $value, $replace = true)
  3446. {
  3447. $name = $this->normalizeHeaderName($name);
  3448. if (is_null($value))
  3449. {
  3450. unset($this->headers[$name]);
  3451. return;
  3452. }
  3453. if ('Content-Type' == $name)
  3454. {
  3455. if ($replace || !$this->getHttpHeader('Content-Type', null))
  3456. {
  3457. $this->setContentType($value);
  3458. }
  3459. return;
  3460. }
  3461. if (!$replace)
  3462. {
  3463. $current = isset($this->headers[$name]) ? $this->headers[$name] : '';
  3464. $value = ($current ? $current.', ' : '').$value;
  3465. }
  3466. $this->headers[$name] = $value;
  3467. }
  3468. public function getHttpHeader($name, $default = null)
  3469. {
  3470. $name = $this->normalizeHeaderName($name);
  3471. return isset($this->headers[$name]) ? $this->headers[$name] : $default;
  3472. }
  3473. public function hasHttpHeader($name)
  3474. {
  3475. return array_key_exists($this->normalizeHeaderName($name), $this->headers);
  3476. }
  3477. public function setContentType($value)
  3478. {
  3479. $this->headers['Content-Type'] = $this->fixContentType($value);
  3480. }
  3481. public function getCharset()
  3482. {
  3483. return $this->options['charset'];
  3484. }
  3485. public function getContentType()
  3486. {
  3487. return $this->getHttpHeader('Content-Type', $this->options['content_type']);
  3488. }
  3489. public function sendHttpHeaders()
  3490. {
  3491. if (!$this->options['send_http_headers'])
  3492. {
  3493. return;
  3494. }
  3495. $status = $this->options['http_protocol'].' '.$this->statusCode.' '.$this->statusText;
  3496. header($status);
  3497. if ($this->options['logging'])
  3498. {
  3499. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send status "%s"', $status))));
  3500. }
  3501. if (!$this->getHttpHeader('Content-Type'))
  3502. {
  3503. $this->setContentType($this->options['content_type']);
  3504. }
  3505. foreach ($this->headers as $name => $value)
  3506. {
  3507. header($name.': '.$value);
  3508. if ($value != '' && $this->options['logging'])
  3509. {
  3510. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send header "%s: %s"', $name, $value))));
  3511. }
  3512. }
  3513. foreach ($this->cookies as $cookie)
  3514. {
  3515. setrawcookie($cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httpOnly']);
  3516. if ($this->options['logging'])
  3517. {
  3518. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Send cookie "%s": "%s"', $cookie['name'], $cookie['value']))));
  3519. }
  3520. }
  3521. }
  3522. public function sendContent()
  3523. {
  3524. if (!$this->headerOnly)
  3525. {
  3526. parent::sendContent();
  3527. }
  3528. }
  3529. public function send()
  3530. {
  3531. $this->sendHttpHeaders();
  3532. $this->sendContent();
  3533. }
  3534. protected function normalizeHeaderName($name)
  3535. {
  3536. return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", strtr(ucfirst(strtolower($name)), '_', '-'));
  3537. }
  3538. static public function getDate($timestamp, $type = 'rfc1123')
  3539. {
  3540. $type = strtolower($type);
  3541. if ($type == 'rfc1123')
  3542. {
  3543. return substr(gmdate('r', $timestamp), 0, -5).'GMT';
  3544. }
  3545. else if ($type == 'rfc1036')
  3546. {
  3547. return gmdate('l, d-M-y H:i:s ', $timestamp).'GMT';
  3548. }
  3549. else if ($type == 'asctime')
  3550. {
  3551. return gmdate('D M j H:i:s', $timestamp);
  3552. }
  3553. else
  3554. {
  3555. throw new InvalidArgumentException('The second getDate() method parameter must be one of: rfc1123, rfc1036 or asctime.');
  3556. }
  3557. }
  3558. public function addVaryHttpHeader($header)
  3559. {
  3560. $vary = $this->getHttpHeader('Vary');
  3561. $currentHeaders = array();
  3562. if ($vary)
  3563. {
  3564. $currentHeaders = preg_split('/\s*,\s*/', $vary);
  3565. }
  3566. $header = $this->normalizeHeaderName($header);
  3567. if (!in_array($header, $currentHeaders))
  3568. {
  3569. $currentHeaders[] = $header;
  3570. $this->setHttpHeader('Vary', implode(', ', $currentHeaders));
  3571. }
  3572. }
  3573. public function addCacheControlHttpHeader($name, $value = null)
  3574. {
  3575. $cacheControl = $this->getHttpHeader('Cache-Control');
  3576. $currentHeaders = array();
  3577. if ($cacheControl)
  3578. {
  3579. foreach (preg_split('/\s*,\s*/', $cacheControl) as $tmp)
  3580. {
  3581. $tmp = explode('=', $tmp);
  3582. $currentHeaders[$tmp[0]] = isset($tmp[1]) ? $tmp[1] : null;
  3583. }
  3584. }
  3585. $currentHeaders[strtr(strtolower($name), '_', '-')] = $value;
  3586. $headers = array();
  3587. foreach ($currentHeaders as $key => $value)
  3588. {
  3589. $headers[] = $key.(null !== $value ? '='.$value : '');
  3590. }
  3591. $this->setHttpHeader('Cache-Control', implode(', ', $headers));
  3592. }
  3593. public function getHttpMetas()
  3594. {
  3595. return $this->httpMetas;
  3596. }
  3597. public function addHttpMeta($key, $value, $replace = true)
  3598. {
  3599. $key = $this->normalizeHeaderName($key);
  3600. $this->setHttpHeader($key, $value, $replace);
  3601. if (is_null($value))
  3602. {
  3603. unset($this->httpMetas[$key]);
  3604. return;
  3605. }
  3606. if ('Content-Type' == $key)
  3607. {
  3608. $value = $this->getContentType();
  3609. }
  3610. elseif (!$replace)
  3611. {
  3612. $current = isset($this->httpMetas[$key]) ? $this->httpMetas[$key] : '';
  3613. $value = ($current ? $current.', ' : '').$value;
  3614. }
  3615. $this->httpMetas[$key] = $value;
  3616. }
  3617. public function getMetas()
  3618. {
  3619. return $this->metas;
  3620. }
  3621. public function addMeta($key, $value, $replace = true, $escape = true)
  3622. {
  3623. $key = strtolower($key);
  3624. if (is_null($value))
  3625. {
  3626. unset($this->metas[$key]);
  3627. return;
  3628. }
  3629. if ($escape)
  3630. {
  3631. $value = htmlspecialchars($value, ENT_QUOTES, $this->options['charset']);
  3632. }
  3633. $current = isset($this->metas[$key]) ? $this->metas[$key] : null;
  3634. if ($replace || !$current)
  3635. {
  3636. $this->metas[$key] = $value;
  3637. }
  3638. }
  3639. public function getTitle()
  3640. {
  3641. return isset($this->metas['title']) ? $this->metas['title'] : '';
  3642. }
  3643. public function setTitle($title, $escape = true)
  3644. {
  3645. $this->addMeta('title', $title, true, $escape);
  3646. }
  3647. public function getPositions()
  3648. {
  3649. return $this->positions;
  3650. }
  3651. public function getStylesheets($position = self::ALL)
  3652. {
  3653. if (self::ALL === $position)
  3654. {
  3655. $stylesheets = array();
  3656. foreach ($this->getPositions() as $position)
  3657. {
  3658. foreach ($this->stylesheets[$position] as $file => $options)
  3659. {
  3660. $stylesheets[$file] = $options;
  3661. }
  3662. }
  3663. return $stylesheets;
  3664. }
  3665. else if (self::RAW === $position)
  3666. {
  3667. return $this->stylesheets;
  3668. }
  3669. $this->validatePosition($position);
  3670. return $this->stylesheets[$position];
  3671. }
  3672. public function addStylesheet($file, $position = '', $options = array())
  3673. {
  3674. $this->validatePosition($position);
  3675. $this->stylesheets[$position][$file] = $options;
  3676. }
  3677. public function removeStylesheet($file)
  3678. {
  3679. foreach ($this->getPositions() as $position)
  3680. {
  3681. unset($this->stylesheets[$position][$file]);
  3682. }
  3683. }
  3684. public function getJavascripts($position = self::ALL)
  3685. {
  3686. if (self::ALL === $position)
  3687. {
  3688. $javascripts = array();
  3689. foreach ($this->getPositions() as $position)
  3690. {
  3691. foreach ($this->javascripts[$position] as $file => $options)
  3692. {
  3693. $javascripts[$file] = $options;
  3694. }
  3695. }
  3696. return $javascripts;
  3697. }
  3698. else if (self::RAW === $position)
  3699. {
  3700. return $this->javascripts;
  3701. }
  3702. $this->validatePosition($position);
  3703. return $this->javascripts[$position];
  3704. }
  3705. public function addJavascript($file, $position = '', $options = array())
  3706. {
  3707. $this->validatePosition($position);
  3708. $this->javascripts[$position][$file] = $options;
  3709. }
  3710. public function removeJavascript($file)
  3711. {
  3712. foreach ($this->getPositions() as $position)
  3713. {
  3714. unset($this->javascripts[$position][$file]);
  3715. }
  3716. }
  3717. public function getSlots()
  3718. {
  3719. return $this->slots;
  3720. }
  3721. public function setSlot($name, $content)
  3722. {
  3723. $this->slots[$name] = $content;
  3724. }
  3725. public function getCookies()
  3726. {
  3727. return $this->cookies;
  3728. }
  3729. public function getHttpHeaders()
  3730. {
  3731. return $this->headers;
  3732. }
  3733. public function clearHttpHeaders()
  3734. {
  3735. $this->headers = array();
  3736. }
  3737. public function copyProperties(sfWebResponse $response)
  3738. {
  3739. $this->options = $response->getOptions();
  3740. $this->headers = $response->getHttpHeaders();
  3741. $this->metas = $response->getMetas();
  3742. $this->httpMetas = $response->getHttpMetas();
  3743. $this->stylesheets = $response->getStylesheets(self::RAW);
  3744. $this->javascripts = $response->getJavascripts(self::RAW);
  3745. $this->slots = $response->getSlots();
  3746. }
  3747. public function merge(sfWebResponse $response)
  3748. {
  3749. foreach ($this->getPositions() as $position)
  3750. {
  3751. $this->javascripts[$position] = array_merge($this->getJavascripts($position), $response->getJavascripts($position));
  3752. $this->stylesheets[$position] = array_merge($this->getStylesheets($position), $response->getStylesheets($position));
  3753. }
  3754. $this->slots = array_merge($this->getSlots(), $response->getSlots());
  3755. }
  3756. public function serialize()
  3757. {
  3758. return serialize(array($this->content, $this->statusCode, $this->statusText, $this->options, $this->cookies, $this->headerOnly, $this->headers, $this->metas, $this->httpMetas, $this->stylesheets, $this->javascripts, $this->slots));
  3759. }
  3760. public function unserialize($serialized)
  3761. {
  3762. list($this->content, $this->statusCode, $this->statusText, $this->options, $this->cookies, $this->headerOnly, $this->headers, $this->metas, $this->httpMetas, $this->stylesheets, $this->javascripts, $this->slots) = unserialize($serialized);
  3763. }
  3764. protected function validatePosition($position)
  3765. {
  3766. if (!in_array($position, $this->positions, true))
  3767. {
  3768. throw new InvalidArgumentException(sprintf('The position "%s" does not exist (available positions: %s).', $position, implode(', ', $this->positions)));
  3769. }
  3770. }
  3771. protected function fixContentType($contentType)
  3772. {
  3773. if (false === stripos($contentType, 'charset') && (0 === stripos($contentType, 'text/') || strlen($contentType) - 3 === strripos($contentType, 'xml')))
  3774. {
  3775. $contentType .= '; charset='.$this->options['charset'];
  3776. }
  3777. if (preg_match('/charset\s*=\s*(.+)\s*$/', $contentType, $match))
  3778. {
  3779. $this->options['charset'] = $match[1];
  3780. }
  3781. return $contentType;
  3782. }
  3783. }
  3784. class sfSessionStorage extends sfStorage
  3785. {
  3786. static protected
  3787. $sessionIdRegenerated = false,
  3788. $sessionStarted = false;
  3789. public function initialize($options = null)
  3790. {
  3791. $cookieDefaults = session_get_cookie_params();
  3792. $options = array_merge(array(
  3793. 'session_name' => 'symfony',
  3794. 'session_id' => null,
  3795. 'auto_start' => true,
  3796. 'session_cookie_lifetime' => $cookieDefaults['lifetime'],
  3797. 'session_cookie_path' => $cookieDefaults['path'],
  3798. 'session_cookie_domain' => $cookieDefaults['domain'],
  3799. 'session_cookie_secure' => $cookieDefaults['secure'],
  3800. 'session_cookie_httponly' => isset($cookieDefaults['httponly']) ? $cookieDefaults['httponly'] : false,
  3801. 'session_cache_limiter' => 'none',
  3802. ), $options);
  3803. parent::initialize($options);
  3804. $sessionName = $this->options['session_name'];
  3805. session_name($sessionName);
  3806. if (!(boolean) ini_get('session.use_cookies') && $sessionId = $this->options['session_id'])
  3807. {
  3808. session_id($sessionId);
  3809. }
  3810. $lifetime = $this->options['session_cookie_lifetime'];
  3811. $path = $this->options['session_cookie_path'];
  3812. $domain = $this->options['session_cookie_domain'];
  3813. $secure = $this->options['session_cookie_secure'];
  3814. $httpOnly = $this->options['session_cookie_httponly'];
  3815. session_set_cookie_params($lifetime, $path, $domain, $secure, $httpOnly);
  3816. if (!is_null($this->options['session_cache_limiter']))
  3817. {
  3818. session_cache_limiter($this->options['session_cache_limiter']);
  3819. }
  3820. if ($this->options['auto_start'] && !self::$sessionStarted)
  3821. {
  3822. session_start();
  3823. self::$sessionStarted = true;
  3824. }
  3825. }
  3826. public function read($key)
  3827. {
  3828. $retval = null;
  3829. if (isset($_SESSION[$key]))
  3830. {
  3831. $retval = $_SESSION[$key];
  3832. }
  3833. return $retval;
  3834. }
  3835. public function remove($key)
  3836. {
  3837. $retval = null;
  3838. if (isset($_SESSION[$key]))
  3839. {
  3840. $retval = $_SESSION[$key];
  3841. unset($_SESSION[$key]);
  3842. }
  3843. return $retval;
  3844. }
  3845. public function write($key, $data)
  3846. {
  3847. $_SESSION[$key] = $data;
  3848. }
  3849. public function regenerate($destroy = false)
  3850. {
  3851. if (self::$sessionIdRegenerated)
  3852. {
  3853. return;
  3854. }
  3855. session_regenerate_id($destroy);
  3856. self::$sessionIdRegenerated = true;
  3857. }
  3858. public function shutdown()
  3859. {
  3860. session_write_close();
  3861. }
  3862. }
  3863. class sfPHPView extends sfView
  3864. {
  3865. public function execute()
  3866. {
  3867. }
  3868. protected function loadCoreAndStandardHelpers()
  3869. {
  3870. static $coreHelpersLoaded = 0;
  3871. if ($coreHelpersLoaded)
  3872. {
  3873. return;
  3874. }
  3875. $coreHelpersLoaded = 1;
  3876. $helpers = array_unique(array_merge(array('Helper', 'Url', 'Asset', 'Tag', 'Escaping'), sfConfig::get('sf_standard_helpers')));
  3877. if (!sfConfig::get('sf_compat_10') && false !== $i = array_search('Form', $helpers))
  3878. {
  3879. unset($helpers[$i]);
  3880. }
  3881. $this->context->getConfiguration()->loadHelpers($helpers);
  3882. }
  3883. protected function renderFile($_sfFile)
  3884. {
  3885. if (sfConfig::get('sf_logging_enabled'))
  3886. {
  3887. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Render "%s"', $_sfFile))));
  3888. }
  3889. $this->loadCoreAndStandardHelpers();
  3890. $vars = $this->attributeHolder->toArray();
  3891. extract($vars);
  3892. ob_start();
  3893. ob_implicit_flush(0);
  3894. require($_sfFile);
  3895. return ob_get_clean();
  3896. }
  3897. public function getEngine()
  3898. {
  3899. return null;
  3900. }
  3901. public function configure()
  3902. {
  3903. $this->context->set('view_instance', $this);
  3904. require($this->context->getConfigCache()->checkConfig('modules/'.$this->moduleName.'/config/view.yml'));
  3905. if (!$this->directory)
  3906. {
  3907. $this->setDirectory($this->context->getConfiguration()->getTemplateDir($this->moduleName, $this->getTemplate()));
  3908. }
  3909. }
  3910. protected function decorate($content)
  3911. {
  3912. if (sfConfig::get('sf_logging_enabled'))
  3913. {
  3914. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Decorate content with "%s/%s"', $this->getDecoratorDirectory(), $this->getDecoratorTemplate()))));
  3915. }
  3916. $attributeHolder = $this->attributeHolder;
  3917. $this->attributeHolder = $this->initializeAttributeHolder(array('sf_content' => new sfOutputEscaperSafe($content)));
  3918. $this->attributeHolder->set('sf_type', 'layout');
  3919. $ret = $this->renderFile($this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate());
  3920. $this->attributeHolder = $attributeHolder;
  3921. return $ret;
  3922. }
  3923. public function render()
  3924. {
  3925. $content = null;
  3926. if (sfConfig::get('sf_cache'))
  3927. {
  3928. $viewCache = $this->context->getViewCacheManager();
  3929. $uri = $this->context->getRouting()->getCurrentInternalUri();
  3930. if (!is_null($uri))
  3931. {
  3932. list($content, $decoratorTemplate) = $viewCache->getActionCache($uri);
  3933. if (!is_null($content))
  3934. {
  3935. $this->setDecoratorTemplate($decoratorTemplate);
  3936. }
  3937. }
  3938. }
  3939. if (is_null($content))
  3940. {
  3941. $this->preRenderCheck();
  3942. $this->attributeHolder->set('sf_type', 'action');
  3943. $content = $this->renderFile($this->getDirectory().'/'.$this->getTemplate());
  3944. if (sfConfig::get('sf_cache') && !is_null($uri))
  3945. {
  3946. $content = $viewCache->setActionCache($uri, $content, $this->isDecorator() ? $this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate() : false);
  3947. }
  3948. }
  3949. if ($this->isDecorator())
  3950. {
  3951. $content = $this->decorate($content);
  3952. }
  3953. return $content;
  3954. }
  3955. }
  3956. class sfOutputEscaperSafe extends ArrayIterator
  3957. {
  3958. protected
  3959. $value = null;
  3960. public function __construct($value)
  3961. {
  3962. $this->value = $value;
  3963. if (is_array($value) || is_object($value))
  3964. {
  3965. parent::__construct($value);
  3966. }
  3967. }
  3968. public function __toString()
  3969. {
  3970. return (string) $this->value;
  3971. }
  3972. public function __get($key)
  3973. {
  3974. return $this->value->$key;
  3975. }
  3976. public function __set($key, $value)
  3977. {
  3978. $this->value->$key = $value;
  3979. }
  3980. public function __call($method, $arguments)
  3981. {
  3982. return call_user_func_array(array($this->value, $method), $arguments);
  3983. }
  3984. public function __isset($key)
  3985. {
  3986. return isset($this->value->$key);
  3987. }
  3988. public function __unset($key)
  3989. {
  3990. unset($this->value->$key);
  3991. }
  3992. public function getValue()
  3993. {
  3994. return $this->value;
  3995. }
  3996. }