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: class Mage_Cms_Helper_Wysiwyg_Images extends Mage_Core_Helper_Abstract
31: {
32:
33: 34: 35: 36:
37: protected $_currentPath;
38:
39: 40: 41: 42:
43: protected $_currentUrl;
44:
45: 46: 47: 48: 49:
50: protected $_storeId = null;
51:
52:
53: 54: 55: 56: 57:
58: public function setStoreId($store)
59: {
60: $this->_storeId = $store;
61: return $this;
62: }
63:
64: 65: 66: 67: 68:
69: public function getStorageRoot()
70: {
71: return Mage::getConfig()->getOptions()->getMediaDir() . DS . Mage_Cms_Model_Wysiwyg_Config::IMAGE_DIRECTORY
72: . DS;
73: }
74:
75: 76: 77: 78: 79:
80: public function getBaseUrl()
81: {
82: return Mage::getBaseUrl('media') . '/';
83: }
84:
85: 86: 87: 88: 89:
90: public function getTreeNodeName()
91: {
92: return 'node';
93: }
94:
95: 96: 97: 98: 99: 100:
101: public function convertPathToId($path)
102: {
103: $path = str_replace($this->getStorageRoot(), '', $path);
104: return $this->idEncode($path);
105: }
106:
107: 108: 109: 110: 111: 112:
113: public function convertIdToPath($id)
114: {
115: $path = $this->idDecode($id);
116: if (!strstr($path, $this->getStorageRoot())) {
117: $path = $this->getStorageRoot() . $path;
118: }
119: return $path;
120: }
121:
122: 123: 124: 125: 126: 127: 128:
129: public function correctPath($path, $trim = true)
130: {
131: $path = strtr($path, "\\\/", DS . DS);
132: if ($trim) {
133: $path = trim($path, DS);
134: }
135: return $path;
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function convertPathToUrl($path)
145: {
146: return str_replace(DS, '/', $path);
147: }
148:
149: 150: 151: 152: 153:
154: public function isUsingStaticUrlsAllowed()
155: {
156: $checkResult = new StdClass;
157: $checkResult->isAllowed = false;
158: Mage::dispatchEvent('cms_wysiwyg_images_static_urls_allowed', array(
159: 'result' => $checkResult,
160: 'store_id' => $this->_storeId
161: ));
162: return $checkResult->isAllowed;
163: }
164:
165: 166: 167: 168: 169: 170: 171:
172: public function getImageHtmlDeclaration($filename, $renderAsTag = false)
173: {
174: $fileurl = $this->getCurrentUrl() . $filename;
175: $mediaPath = str_replace(Mage::getBaseUrl('media'), '', $fileurl);
176: $directive = sprintf('{{media url="%s"}}', $mediaPath);
177: if ($renderAsTag) {
178: $html = sprintf('<img src="%s" alt="" />', $this->isUsingStaticUrlsAllowed() ? $fileurl : $directive);
179: } else {
180: if ($this->isUsingStaticUrlsAllowed()) {
181: $html = $fileurl;
182: } else {
183: $directive = Mage::helper('core')->urlEncode($directive);
184: $html = Mage::helper('adminhtml')->getUrl('*/cms_wysiwyg/directive', array('___directive' => $directive));
185: }
186: }
187: return $html;
188: }
189:
190: 191: 192: 193: 194: 195: 196:
197: public function getCurrentPath()
198: {
199: if (!$this->_currentPath) {
200: $currentPath = $this->getStorageRoot();
201: $path = $this->_getRequest()->getParam($this->getTreeNodeName());
202: if ($path) {
203: $path = $this->convertIdToPath($path);
204: if (is_dir($path)) {
205: $currentPath = $path;
206: }
207: }
208: $io = new Varien_Io_File();
209: if (!$io->isWriteable($currentPath) && !$io->mkdir($currentPath)) {
210: $message = Mage::helper('cms')->__('The directory %s is not writable by server.',$currentPath);
211: Mage::throwException($message);
212: }
213: $this->_currentPath = $currentPath;
214: }
215: return $this->_currentPath;
216: }
217:
218: 219: 220: 221: 222:
223: public function getCurrentUrl()
224: {
225: if (!$this->_currentUrl) {
226: $path = str_replace(Mage::getConfig()->getOptions()->getMediaDir(), '', $this->getCurrentPath());
227: $path = trim($path, DS);
228: $this->_currentUrl = Mage::app()->getStore($this->_storeId)->getBaseUrl('media') .
229: $this->convertPathToUrl($path) . '/';
230: }
231: return $this->_currentUrl;
232: }
233:
234: 235: 236: 237: 238:
239: public function getStorage()
240: {
241: return Mage::getSingleton('cms/wysiwyg_images_storage');
242: }
243:
244: 245: 246: 247: 248: 249:
250: public function idEncode($string)
251: {
252: return strtr(base64_encode($string), '+/=', ':_-');
253: }
254:
255: 256: 257: 258: 259: 260:
261: public function idDecode($string)
262: {
263: $string = strtr($string, ':_-', '+/=');
264: return base64_decode($string);
265: }
266:
267: 268: 269: 270: 271: 272: 273:
274: public function getShortFilename($filename, $maxLength = 20)
275: {
276: if (strlen($filename) <= $maxLength) {
277: return $filename;
278: }
279: return substr($filename, 0, $maxLength) . '...';
280: }
281: }
282: