1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33:
34: class Mage_Core_Controller_Front_Action extends Mage_Core_Controller_Varien_Action
35: {
36: 37: 38:
39: const SESSION_NAMESPACE = 'frontend';
40:
41: 42: 43: 44: 45:
46: protected $_currentArea = 'frontend';
47:
48: 49: 50: 51: 52:
53: protected $_sessionNamespace = self::SESSION_NAMESPACE;
54:
55: 56: 57: 58: 59:
60: public function preDispatch()
61: {
62: $this->getLayout()->setArea($this->_currentArea);
63:
64: parent::preDispatch();
65: return $this;
66: }
67:
68: 69: 70: 71: 72:
73: public function postDispatch()
74: {
75: parent::postDispatch();
76: if (!$this->getFlag('', self::FLAG_NO_START_SESSION )) {
77: Mage::getSingleton('core/session')->setLastUrl(Mage::getUrl('*/*/*', array('_current'=>true)));
78: }
79: return $this;
80: }
81:
82: 83: 84: 85: 86:
87: public function __()
88: {
89: $args = func_get_args();
90: $expr = new Mage_Core_Model_Translate_Expr(array_shift($args), $this->_getRealModuleName());
91: array_unshift($args, $expr);
92: return Mage::app()->getTranslator()->translate($args);
93: }
94:
95: 96: 97: 98: 99: 100: 101: 102: 103: 104:
105: protected function _prepareDownloadResponse($fileName, $content, $contentType = 'application/octet-stream',
106: $contentLength = null
107: ) {
108: $session = Mage::getSingleton('admin/session');
109: if ($session->isFirstPageAfterLogin()) {
110: $this->_redirect($session->getUser()->getStartupPageUrl());
111: return $this;
112: }
113:
114: $isFile = false;
115: $file = null;
116: if (is_array($content)) {
117: if (!isset($content['type']) || !isset($content['value'])) {
118: return $this;
119: }
120: if ($content['type'] == 'filename') {
121: $isFile = true;
122: $file = $content['value'];
123: $contentLength = filesize($file);
124: }
125: }
126:
127: $this->getResponse()
128: ->setHttpResponseCode(200)
129: ->setHeader('Pragma', 'public', true)
130: ->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
131: ->setHeader('Content-type', $contentType, true)
132: ->setHeader('Content-Length', is_null($contentLength) ? strlen($content) : $contentLength)
133: ->setHeader('Content-Disposition', 'attachment; filename="'.$fileName.'"')
134: ->setHeader('Last-Modified', date('r'));
135:
136: if (!is_null($content)) {
137: if ($isFile) {
138: $this->getResponse()->clearBody();
139: $this->getResponse()->sendHeaders();
140:
141: $ioAdapter = new Varien_Io_File();
142: if (!$ioAdapter->fileExists($file)) {
143: Mage::throwException(Mage::helper('core')->__('File not found'));
144: }
145: $ioAdapter->open(array('path' => $ioAdapter->dirname($file)));
146: $ioAdapter->streamOpen($file, 'r');
147: while ($buffer = $ioAdapter->streamRead()) {
148: print $buffer;
149: }
150: $ioAdapter->streamClose();
151: if (!empty($content['rm'])) {
152: $ioAdapter->rm($file);
153: }
154:
155: exit(0);
156: } else {
157: $this->getResponse()->setBody($content);
158: }
159: }
160: return $this;
161: }
162: }
163: