PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/_Includes/Sitengine/Proto/Binaries/Couldies/Uploads/Controller.php

https://github.com/sitengine/sitengine
PHP | 359 lines | 268 code | 62 blank | 29 comment | 28 complexity | ba6858f4b383ce5ff78dcb0e439cd917 MD5 | raw file
  1. <?php
  2. /**
  3. * Sitengine - An example serving as a pattern
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. *
  10. * @category Sitengine
  11. * @package Sitengine_Proto
  12. * @copyright Copyright (c) 2007, Christian Hoegl, Switzerland (http://sitengine.org)
  13. * @license http://sitengine.org/license/new-bsd New BSD License
  14. */
  15. require_once 'Sitengine/Controller/Action.php';
  16. abstract class Sitengine_Proto_Binaries_Couldies_Uploads_Controller extends Sitengine_Controller_Action
  17. {
  18. const ACTION_BIN = '_bin';
  19. protected $_started = false;
  20. protected $_config = null;
  21. protected $_env = null;
  22. protected $_logger = null;
  23. protected $_database = null;
  24. protected $_status = null;
  25. protected $_preferences = null;
  26. protected $_locale = null;
  27. protected $_permiso = null;
  28. protected $_namespace = null;
  29. public function getEnv() { return $this->_env; }
  30. public function getLogger() { return $this->_logger; }
  31. public function getDatabase() { return $this->_database; }
  32. public function getStatus() { return $this->_status; }
  33. public function getPreferences() { return $this->_preferences; }
  34. public function getLocale() { return $this->_locale; }
  35. public function getPermiso() { return $this->_permiso; }
  36. public function getNamespace() { return $this->_namespace; }
  37. public function getTranslate() { return $this->_translate; }
  38. public function __construct(
  39. Zend_Controller_Request_Abstract $request,
  40. Zend_Controller_Response_Abstract $response,
  41. array $invokeArgs = array()
  42. )
  43. {
  44. try {
  45. parent::__construct($request, $response, $invokeArgs);
  46. $this->_mapInvokeArgs($invokeArgs);
  47. $this->_mapConfig($this->_config);
  48. $this->_logger = $this->getEnv()->getLoggerInstance(
  49. $this->getEnv()->getMyLogsDir(),
  50. gmdate('Ymd').'-sitengine.log',
  51. $this->getEnv()->getLogFilterPriority(),
  52. get_class($this)
  53. );
  54. $this->_database = $this->getEnv()->getDatabaseInstance(
  55. 'Pdo_Mysql',
  56. $this->getEnv()->getDatabaseConfig('default'),
  57. $this->getEnv()->getDebugControl()
  58. );
  59. $this->getEnv()->startSession($this->getDatabase());
  60. require_once 'Sitengine/Status.php';
  61. $this->_status = Sitengine_Status::getInstance();
  62. require_once 'Sitengine/Env/Preferences.php';
  63. $this->_preferences = Sitengine_Env_Preferences::getInstance();
  64. $this->_locale = $this->getEnv()->getLocaleInstance();
  65. $this->_permiso = $this->getFrontController()->getPermiso()->start($this->getDatabase());
  66. require_once 'Zend/Session/Namespace.php';
  67. $this->_namespace = new Zend_Session_Namespace(get_class($this));
  68. }
  69. catch (Exception $exception) {
  70. require_once 'Sitengine/Proto/Binaries/Couldies/Uploads/Exception.php';
  71. throw new Sitengine_Proto_Binaries_Couldies_Uploads_Exception('object instantiation error', $exception);
  72. }
  73. }
  74. protected function _mapInvokeArgs(array $invokeArgs)
  75. {
  76. if(
  77. array_key_exists('env', $invokeArgs) &&
  78. $invokeArgs['env'] instanceof Sitengine_Env_Default &&
  79. array_key_exists('frontController', $invokeArgs) &&
  80. $invokeArgs['frontController'] instanceof Sitengine_Proto_Binaries_Front &&
  81. array_key_exists('config', $invokeArgs) &&
  82. $invokeArgs['config'] instanceof Zend_Config
  83. )
  84. {
  85. $this->_env = $invokeArgs['env'];
  86. $this->_frontController = $invokeArgs['frontController'];
  87. $this->_config = $invokeArgs['config'];
  88. }
  89. else {
  90. require_once 'Sitengine/Proto/Binaries/Couldies/Uploads/Exception.php';
  91. throw new Sitengine_Proto_Binaries_Couldies_Uploads_Exception('invalid invoke args');
  92. }
  93. }
  94. protected function _mapConfig(Zend_Config $config)
  95. {
  96. /*
  97. $config = $config->protoFilesCouldiesUploadsController;
  98. if(
  99. isset($config->authorizedGroups)
  100. )
  101. {
  102. $this->_authorizedGroups = $config->authorizedGroups->toArray();
  103. }
  104. else {
  105. require_once 'Sitengine/Proto/Binaries/Couldies/Uploads/Exception.php';
  106. throw new Sitengine_Proto_Binaries_Couldies_Uploads_Exception('action controller config error');
  107. }
  108. */
  109. }
  110. protected function _start()
  111. {
  112. try {
  113. if(!$this->_started)
  114. {
  115. $this->_started = true;
  116. #$this->getDatabase()->query('SET NAMES "utf8"');
  117. if($this->getRequest()->get(Sitengine_Env::PARAM_LOGOUT))
  118. {
  119. $this->getPermiso()->getAuth()->clearIdentity();
  120. }
  121. if($this->getPermiso()->getAuth()->hasIdentity())
  122. {
  123. $this->getPermiso()->getAuth()->extendValidity();
  124. }
  125. $this->getPreferences()->establishLanguage(
  126. $this->getRequest(),
  127. Sitengine_Env::PARAM_LANGUAGE
  128. );
  129. $this->getPreferences()->establishTranscript(
  130. $this->getRequest(),
  131. Sitengine_Env::PARAM_TRANSCRIPT
  132. );
  133. $this->getPreferences()->establishItemsPerPage(
  134. $this->getRequest(),
  135. Sitengine_Env::PARAM_IPP
  136. );
  137. $this->getPreferences()->establishTimezone(
  138. $this->getRequest(),
  139. Sitengine_Env::PARAM_TIMEZONE
  140. );
  141. $this->getPreferences()->establishDebugMode(
  142. $this->getRequest(),
  143. Sitengine_Env::PARAM_DBG
  144. );
  145. if($this->getEnv()->getDebugControl()) {
  146. require_once 'Sitengine/Debug.php';
  147. Sitengine_Debug::action($this->getPreferences()->getDebugMode());
  148. }
  149. #$this->getLocale()->setLocale($this->getPreferences()->getLanguage());
  150. }
  151. }
  152. catch (Exception $exception) {
  153. require_once 'Sitengine/Proto/Binaries/Couldies/Uploads/Exception.php';
  154. throw new Sitengine_Proto_Binaries_Couldies_Uploads_Exception('init error', $exception);
  155. }
  156. }
  157. protected function _goToAction($action)
  158. {
  159. $handler = $action.'Action';
  160. if(is_callable(array($this, $handler))) {
  161. $this->getRequest()->setActionName($action);
  162. call_user_func(array($this, $handler));
  163. }
  164. else {
  165. require_once 'Sitengine/Proto/Binaries/Couldies/Uploads/Exception.php';
  166. throw new Sitengine_Proto_Binaries_Couldies_Uploads_Exception('trying to forward to a non-existing action handler');
  167. }
  168. }
  169. public function __call($a, $b)
  170. {
  171. $routeName = $this->getFrontController()->getRouter()->getCurrentRouteName();
  172. $method = $this->getRequest()->getIntendedMethod();
  173. $action = null;
  174. switch($method) {
  175. case Sitengine_Env::METHOD_GET: $action = self::ACTION_BIN; break;
  176. }
  177. if($action === null) {
  178. require_once 'Sitengine/Proto/Binaries/Couldies/Uploads/Exception.php';
  179. $exception = new Sitengine_Proto_Binaries_Couldies_Uploads_Exception(
  180. "'$method' not supported on route '$route'",
  181. Sitengine_Env::ERROR_NOT_IMPLEMENTED
  182. );
  183. throw $this->_prepareErrorHandler($exception);
  184. }
  185. return $this->_goToAction($action);
  186. }
  187. protected function _prepareErrorHandler(Exception $exception)
  188. {
  189. if($this->getPreferences() !== null) {
  190. $this->getRequest()->setParam(
  191. Sitengine_Env::PARAM_LANGUAGE,
  192. $this->getPreferences()->getLanguage()
  193. );
  194. }
  195. require_once 'Sitengine/Error/Controller.php';
  196. switch($exception->getCode())
  197. {
  198. case Sitengine_Env::ERROR_NOT_FOUND:
  199. $handler = Sitengine_Error_Controller::ACTION_NOT_FOUND;
  200. break;
  201. case Sitengine_Env::ERROR_BAD_REQUEST:
  202. $handler = Sitengine_Error_Controller::ACTION_BAD_REQUEST;
  203. break;
  204. case Sitengine_Env::ERROR_FORBIDDEN:
  205. $handler = Sitengine_Error_Controller::ACTION_FORBIDDEN;
  206. break;
  207. case Sitengine_Env::ERROR_NOT_IMPLEMENTED:
  208. $handler = Sitengine_Error_Controller::ACTION_NOT_IMPLEMENTED;
  209. break;
  210. default:
  211. $handler = Sitengine_Error_Controller::ACTION_INTERNAL_SERVER_ERROR;
  212. }
  213. $pluginClass = 'Zend_Controller_Plugin_ErrorHandler';
  214. if($this->getFrontController()->hasPlugin($pluginClass))
  215. {
  216. $this->getFrontController()->getPlugin($pluginClass)->setErrorHandlerAction($handler);
  217. }
  218. return $exception;
  219. }
  220. public function getTempDir()
  221. {
  222. $this->_start();
  223. if(!$this->getFrontController()->getProtoPackage()->tempDirPerUser()) { return $this->getFrontController()->getProtoPackage()->getCouldyTempDir(); }
  224. else {
  225. $userTempDir = $this->getFrontController()->getProtoPackage()->getCouldyTempDir().'/'.$this->getPermiso()->getAuth()->getId();
  226. if(!is_dir($userTempDir)) { mkdir($userTempDir, 0777); }
  227. return $userTempDir;
  228. }
  229. }
  230. protected function _binAction()
  231. {
  232. try {
  233. $this->_start();
  234. if(!$this->getPermiso()->getAcl()->privateAccessGranted($this->getFrontController()->getProtoPackage()->getAuthorizedGroups())) {
  235. require_once 'Sitengine/Proto/Binaries/Couldies/Uploads/Exception.php';
  236. throw new Sitengine_Proto_Binaries_Couldies_Uploads_Exception(
  237. 'unauthorized',
  238. Sitengine_Env::ERROR_FORBIDDEN
  239. );
  240. }
  241. $filename = $this->getRequest()->get(Sitengine_Env::PARAM_FILE);
  242. $path = $this->getTempDir().'/'.$filename;
  243. if(!is_readable($path)) {
  244. require_once 'Sitengine/Proto/Binaries/Couldies/Uploads/Exception.php';
  245. throw new Sitengine_Proto_Binaries_Couldies_Uploads_Exception('file can not be downloaded', Sitengine_Env::ERROR_NOT_FOUND);
  246. }
  247. if(preg_match('/\.(jpg|jpeg|pjpg)$/', $filename)) {
  248. header('Content-type: image/jpeg');
  249. header('Content-Disposition inline; filename="'.$filename.'"');
  250. }
  251. else if(preg_match('/\.(gif)$/', $filename)) {
  252. header('Content-type: image/gif');
  253. header('Content-Disposition inline; filename="'.$filename.'"');
  254. }
  255. else if(preg_match('/\.(png)$/', $filename)) {
  256. header('Content-type: image/png');
  257. header('Content-Disposition inline; filename="'.$filename.'"');
  258. }
  259. else {
  260. header('Content-type: application/octet-stream');
  261. header('Content-Disposition download; filename="'.$filename.'"');
  262. }
  263. $escapedProjectDir = preg_replace('/\//', '\/', $this->getEnv()->getMyProjectDir());
  264. $escapedProjectDir = preg_replace('/\./', '\.', $escapedProjectDir);
  265. $xsendPath = preg_replace('/'.$escapedProjectDir.'\//', '', $path);
  266. header("X-Sendfile: $xsendPath");
  267. }
  268. catch (Exception $exception) {
  269. throw $this->_prepareErrorHandler($exception);
  270. }
  271. }
  272. protected function _getDebugDump(array $data)
  273. {
  274. $dump = '';
  275. if($this->getEnv()->getDebugControl())
  276. {
  277. ob_start();
  278. $debugMode = $this->getPreferences()->getDebugMode();
  279. if($debugMode == 'queries') {
  280. require_once 'Sitengine/Db/Debug.php';
  281. Sitengine_Db_Debug::profiler($this->getDatabase());
  282. }
  283. else if($debugMode=='templateData') {
  284. print '<hr /><h1>TEMPLATE DATA</h1><hr />';
  285. Sitengine_Debug::print_r($data);
  286. }
  287. else {
  288. print Sitengine_Debug::info($debugMode);
  289. }
  290. $dump = ob_get_contents();
  291. ob_end_clean();
  292. }
  293. return $dump;
  294. }
  295. }
  296. ?>