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_Downloadable
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: * Downloadable links validator
29: *
30: * @category Mage
31: * @package Mage_Downloadable
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Downloadable_Model_Link_Api_Validator //extends Mage_Api_Model_Resource_Abstract
35: {
36: /**
37: * Acceptable resourceTypes array
38: * @var array
39: */
40: protected $_types = array('link', 'sample');
41:
42: /**
43: * Acceptable upload types array
44: * @var array
45: */
46: protected $_uploadTypes = array('file', 'url');
47:
48: /**
49: * List of all attributes and names endings of validation functions
50: *
51: * @var array
52: */
53: protected $_defaultAttributes = array(
54: 'link' => array(
55: 'title' => 'Title', // $1
56: 'price' => 'Price', // $2
57: 'number_of_downloads' => 'NumOfDownloads', // if no set is_unlimited to 1 $3
58: 'is_unlimited' => 'Unlimited', // 1|0 $4
59: 'is_shareable' => 'Shareable', // 1|0|2 (2) $5
60: 'type' => 'UploadType', // file|url (file) $6
61: 'file' => 'File', // array(name, base64_content) $7
62: 'link_url' => 'Url', // URL $8
63: 'sort_order' => 'Order', // int (0) $9
64: 'sample' => array(
65: 'type' => 'UploadType', // file|url (file) $6
66: 'file' => 'File', // array(name, base64_content) $7
67: 'url' => 'Url' // URL $8
68: )
69: ),
70: 'sample' => array(
71: 'title' => 'Title', // $1
72: 'type' => 'UploadType', // file|url (file) $6
73: 'file' => 'File', // array(name, base64_content) $7
74: 'sample_url' => 'Url', // URL $8
75: 'sort_order' => 'Order' // int (0) $9
76: )
77: );
78:
79: /**
80: * Get resource types
81: *
82: * @return array
83: */
84: public function getResourceTypes()
85: {
86: return $this->_types;
87: }
88:
89: /**
90: * Validate resourceType, it should be one of (links|samples|link_samples)
91: *
92: * @param string $type
93: * @return boolean
94: */
95: public function validateType($type)
96: {
97: if (!in_array($type, $this->getResourceTypes())) {
98: throw new Exception('unknown_resource_type');
99: }
100: return true;
101: }
102:
103: /**
104: * Validate all parameters and loads default values for omitted parameters.
105: *
106: * @param array $resource
107: * @param string $resourceType
108: */
109: public function validateAttributes(&$resource, $resourceType)
110: {
111: $fields = $this->_defaultAttributes[$resourceType];
112: $this->_dispatch($resource, $fields);
113:
114: $this->completeCheck($resource, $resourceType);
115: }
116:
117: /**
118: * Final check
119: *
120: * @param array $resource
121: * @param string $resourceType
122: */
123: public function completeCheck(&$resource, $resourceType)
124: {
125: if ($resourceType == 'link') {
126: if ($resource['type'] == 'file') {
127: $this->validateFileDetails($resource['file']);
128: }
129: if ($resource['type'] == 'url' && empty($resource['link_url'])) {
130: throw new Exception('empty_url');
131: }
132: // sample
133: if ($resource['sample']['type'] == 'file') {
134: $this->validateFileDetails($resource['sample']['file']);
135: }
136: if ($resource['sample']['type'] == 'url' && empty($resource['sample']['url'])) {
137: throw new Exception('empty_url');
138: }
139: }
140: if ($resourceType == 'sample') {
141: if ($resource['type'] == 'file') {
142: $this->validateFileDetails($resource['file']);
143: }
144: if ($resource['type'] == 'url' && empty($resource['sample_url'])) {
145: throw new Exception('empty_url');
146: }
147: }
148: }
149:
150: /**
151: * Validate variable, in case of fault throw exception
152: *
153: * @param mixed $var
154: */
155: public function validateFileDetails(&$var)
156: {
157: if (!isset ($var['name']) || !is_string($var['name']) || strlen($var['name']) === 0) {
158: throw new Exception('no_filename');
159: }
160: if (!isset ($var['base64_content'])
161: || !is_string($var['base64_content'])
162: || strlen($var['base64_content']) === 0
163: ) {
164: throw new Exception('no_file_base64_content');
165: }
166: }
167:
168: /**
169: * Runs all checks.
170: *
171: * @param array $resource
172: * @param array $fields
173: */
174: protected function _dispatch(&$resource, $fields)
175: {
176: foreach ($fields as $name => $validator) {
177: if (is_string($validator) && strlen($validator) > 0 && array_key_exists($name, $resource)) {
178: $call = 'validate' . $validator;
179: $this->$call($resource[$name]);
180: }
181: if (is_array($validator)) {
182: $this->_dispatch($resource[$name], $validator);
183: }
184: }
185: }
186:
187: /**
188: * Validate variable, in case of fault loads default entity.
189: *
190: * @param string $var
191: */
192: public function validateTitle(&$var)
193: {
194: if (!is_string($var) || strlen($var) === 0) {
195: throw new Exception('no_title');
196: }
197: }
198:
199: /**
200: * Validate variable, in case of fault loads default entity.
201: *
202: * @param float $var
203: */
204: public function validatePrice(&$var)
205: {
206: $var = is_numeric($var)? floatval($var) : floatval(0);
207: }
208:
209: /**
210: * Validate variable, in case of fault loads default entity.
211: *
212: * @param int $var
213: */
214: public function validateNumOfDownloads(&$var)
215: {
216: $var = is_numeric($var)? intval($var) : 0;
217: }
218:
219: /**
220: * Validate variable, in case of fault loads default entity.
221: *
222: * @param int|boolean $var
223: */
224: public function validateUnlimited(&$var)
225: {
226: $var = ((is_numeric($var) && $var >= 0 && $var <= 1) || (is_bool($var)))? intval($var) : 0;
227: }
228:
229: /**
230: * Validate variable, in case of fault loads default entity.
231: *
232: * @param int $var
233: */
234: public function validateShareable(&$var)
235: {
236: $var = (is_numeric($var) && $var >= 0 && $var <= 2)? intval($var) : 2;
237: }
238:
239: /**
240: * Validate variable, in case of fault loads default entity.
241: *
242: * @param array $var
243: */
244: public function validateFile(&$var)
245: {
246: $var = is_array($var)? $var : null;
247: }
248:
249: /**
250: * Validate variable, in case of fault loads default entity.
251: *
252: * @param string $var
253: */
254: public function validateUrl(&$var)
255: {
256:
257: if (is_string($var) && strlen($var) > 0) {
258: $urlregex = "/^(https?|ftp)\:\/\/([a-z0-9+\!\*\(\)\,\;\?\&\=\$\_\.\-]+(\:[a-z0-9+\!\*\(\)\,\;\?\&\=\$\_\.\-]+)?@)?[a-z0-9\+\$\_\-]+(\.[a-z0-9+\$\_\-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$\_\-]\.?)+)*\/?(\?[a-z\+\&\$\_\.\-][a-z0-9\;\:\@\/\&\%\=\+\$\_\.\-]*)?(#[a-z\_\.\-][a-z0-9\+\$\_\.\-]*)?$/i";
259: if (!preg_match($urlregex, $var)) {
260: throw new Exception('url_not_valid');
261: }
262: } else {
263: $var = '';
264: }
265: }
266:
267: /**
268: * Validate variable, in case of fault loads default entity.
269: *
270: * @param int $var
271: */
272: public function validateOrder(&$var)
273: {
274: $var = is_numeric($var)? intval($var) : 0;
275: }
276:
277: /**
278: * Validate variable, in case of fault loads default entity.
279: *
280: * @param string $var
281: */
282: public function validateUploadType(&$var)
283: {
284: $var = in_array($var, $this->_uploadTypes)? $var : 'file';
285: }
286: }
287: