1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Captcha
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Captcha image model
29: *
30: * @category Mage
31: * @package Mage_Captcha
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Captcha_Helper_Data extends Mage_Core_Helper_Abstract
35: {
36: /**
37: * Used for "name" attribute of captcha's input field
38: */
39: const INPUT_NAME_FIELD_VALUE = 'captcha';
40:
41: /**
42: * Always show captcha
43: */
44: const MODE_ALWAYS = 'always';
45:
46: /**
47: * Show captcha only after certain number of unsuccessful attempts
48: */
49: const MODE_AFTER_FAIL = 'after_fail';
50:
51: /**
52: * Captcha fonts path
53: */
54: const XML_PATH_CAPTCHA_FONTS = 'default/captcha/fonts';
55:
56: /**
57: * List uses Models of Captcha
58: * @var array
59: */
60: protected $_captcha = array();
61:
62: /**
63: * Get Captcha
64: *
65: * @param string $formId
66: * @return Mage_Captcha_Model_Interface
67: */
68: public function getCaptcha($formId)
69: {
70: if (!array_key_exists($formId, $this->_captcha)) {
71: $type = $this->getConfigNode('type');
72: $this->_captcha[$formId] = Mage::getModel('captcha/' . $type, array('formId' => $formId));
73: }
74: return $this->_captcha[$formId];
75: }
76:
77: /**
78: * Returns value of the node with respect to current area (frontend or backend)
79: *
80: * @param string $id The last part of XML_PATH_$area_CAPTCHA_ constant (case insensitive)
81: * @param Mage_Core_Model_Store $store
82: * @return Mage_Core_Model_Config_Element
83: */
84: public function getConfigNode($id, $store = null)
85: {
86: $areaCode = Mage::app()->getStore($store)->isAdmin() ? 'admin' : 'customer';
87: return Mage::getStoreConfig( $areaCode . '/captcha/' . $id, $store);
88: }
89:
90: /**
91: * Get list of available fonts
92: * Return format:
93: * [['arial'] => ['label' => 'Arial', 'path' => '/www/magento/fonts/arial.ttf']]
94: *
95: * @return array
96: */
97: public function getFonts()
98: {
99: $node = Mage::getConfig()->getNode(Mage_Captcha_Helper_Data::XML_PATH_CAPTCHA_FONTS);
100: $fonts = array();
101: if ($node) {
102: foreach ($node->children() as $fontName => $fontNode) {
103: $fonts[$fontName] = array(
104: 'label' => (string)$fontNode->label,
105: 'path' => Mage::getBaseDir('base') . DS . $fontNode->path
106: );
107: }
108: }
109: return $fonts;
110: }
111:
112: /**
113: * Get captcha image directory
114: *
115: * @param mixed $website
116: * @return string
117: */
118: public function getImgDir($website = null)
119: {
120: $websiteCode = Mage::app()->getWebsite($website)->getCode();
121: $captchaDir = Mage::getBaseDir('media') . DS . 'captcha' . DS . $websiteCode . DS;
122: $io = new Varien_Io_File();
123: $io->checkAndCreateFolder($captchaDir, 0755);
124: return $captchaDir;
125: }
126:
127: /**
128: * Get captcha image base URL
129: *
130: * @param mixed $website
131: * @return string
132: */
133: public function getImgUrl($website = null)
134: {
135: $websiteCode = Mage::app()->getWebsite($website)->getCode();
136: return Mage::getBaseUrl('media') . 'captcha' . '/' . $websiteCode . '/';
137: }
138: }
139: