PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Mage/Connect/Validator.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 417 lines | 198 code | 50 blank | 169 comment | 30 complexity | 3ac1b21ec703073d5dd9b5dda3471fe8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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_Connect
  23. * @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Class to validate string resources
  28. *
  29. * @category Mage
  30. * @package Mage_Connect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Connect_Validator
  34. {
  35. protected static $_stability = array(0=>'devel',1=>'alpha',2=>'beta',3=>'stable');
  36. public static function getStabilities()
  37. {
  38. return self::$_stability;
  39. }
  40. /**
  41. * Compare stabilities. Returns:
  42. *
  43. * -1 if the first stability is lower than the second
  44. * 0 if they are equal
  45. * 1 if the second is lower.
  46. * @param $s1
  47. * @param $s2
  48. * @return int
  49. */
  50. public function compareStabilities($s1, $s2)
  51. {
  52. $list = $this->getStabilities();
  53. $tmp = array_combine(array_values($list),array_keys($list));
  54. if(!isset($tmp[$s1], $tmp[$s2])) {
  55. throw new Exception("Invalid stability in compareStabilities argument");
  56. }
  57. // 'stable' turns to 3
  58. // 'devel' turns to 0
  59. $s1 = $tmp[$s1];
  60. $s2 = $tmp[$s2];
  61. if($s1 === $s2) {
  62. return 0;
  63. } elseif($s1 > $s2) {
  64. return 1;
  65. } elseif($s1 < $s2) {
  66. return -1;
  67. }
  68. }
  69. /**
  70. * Constructor
  71. */
  72. public function __construct()
  73. {
  74. }
  75. /**
  76. * Validate max len of string
  77. * @param string $str
  78. * @param int $maxLen
  79. * @return bool
  80. */
  81. public function validateMaxLen($str, $maxLen)
  82. {
  83. return strlen((string) $str) <= (int) $maxLen;
  84. }
  85. /**
  86. * Validate channel name and url
  87. *
  88. * @param mixed $str
  89. * @return bool
  90. */
  91. public function validateChannelNameOrUri($str)
  92. {
  93. return ( $this->validateUrl($str) || $this->validatePackageName($str));
  94. }
  95. /**
  96. * Validate License url
  97. *
  98. * @param mixed $str
  99. * @return boolean
  100. */
  101. public function validateLicenseUrl($str)
  102. {
  103. if ($str) {
  104. return ( $this->validateUrl($str) || $this->validatePackageName($str));
  105. }
  106. return true;
  107. }
  108. /**
  109. * Validate compatible data
  110. * @param array $data
  111. * @return bool
  112. */
  113. public function validateCompatible(array $data)
  114. {
  115. if(!count($data)) {
  116. /**
  117. * Allow empty
  118. */
  119. return true;
  120. }
  121. $count = 0;
  122. foreach($data as $k=>$v) {
  123. foreach(array('name','channel','min','max') as $fld) {
  124. $$fld = trim($v[$fld]);
  125. }
  126. $count++;
  127. $res = $this->validateUrl($channel) && strlen($channel);
  128. if(!$res) {
  129. $this->addError("Invalid or empty channel in compat. #{$count}");
  130. }
  131. $res = $this->validatePackageName($name) && strlen($name);
  132. if(!$res) {
  133. $this->addError("Invalid or empty name in compat. #{$count}");
  134. }
  135. $res1 = $this->validateVersion($min);
  136. if(!$res1) {
  137. $this->addError("Invalid or empty minVersion in compat. #{$count}");
  138. }
  139. $res2 = $this->validateVersion($max);
  140. if(!$res2) {
  141. $this->addError("Invalid or empty maxVersion in compat. #{$count}");
  142. }
  143. if($res1 && $res2 && $this->versionLower($max, $min)) {
  144. $this->addError("Max version is lower than min in compat #{$count}");
  145. }
  146. }
  147. return ! $this->hasErrors();
  148. }
  149. /**
  150. * Validate authors of package
  151. * @param array $authors
  152. * @return bool
  153. */
  154. public function validateAuthors(array $authors)
  155. {
  156. if(!count($authors)) {
  157. $this->addError('Empty authors section');
  158. return false;
  159. }
  160. $count = 0;
  161. foreach($authors as $k=>$v) {
  162. $count++;
  163. array_map('trim', $v);
  164. $name = $v['name'];
  165. $login = $v['user'];
  166. $email = $v['email'];
  167. $res = $this->validateMaxLen($name, 256) && strlen($name);
  168. if(!$res) {
  169. $this->addError("Invalid or empty name for author #{$count}");
  170. }
  171. $res = $this->validatePackageName($login) && strlen($login);
  172. if(!$res) {
  173. $this->addError("Invalid or empty login for author #{$count}");
  174. }
  175. $res = $this->validateEmail($email);
  176. if(!$res) {
  177. $this->addError("Invalid or empty email for author #{$count}");
  178. }
  179. }
  180. return ! $this->hasErrors();
  181. }
  182. /**
  183. * Validator errors
  184. * @var array
  185. */
  186. private $_errors = array();
  187. /**
  188. * Add error
  189. * @param string $err
  190. * @return
  191. */
  192. private function addError($err)
  193. {
  194. $this->_errors[] = $err;
  195. }
  196. /**
  197. * Set validator errors
  198. * @param array $err
  199. * @return
  200. */
  201. private function setErrors(array $err)
  202. {
  203. $this->_errors = $err;
  204. }
  205. /**
  206. * Clear validator errors
  207. * @return
  208. */
  209. private function clearErrors()
  210. {
  211. $this->_errors = array();
  212. }
  213. /**
  214. * Check if there are validator errors set
  215. * @return unknown_type
  216. */
  217. public function hasErrors()
  218. {
  219. return count($this->_errors) != 0;
  220. }
  221. /**
  222. * Get errors
  223. * @param bool $clear if true after this call erros will be cleared
  224. * @return array
  225. */
  226. public function getErrors($clear = true)
  227. {
  228. $out = $this->_errors;
  229. if($clear) {
  230. $this->clearErrors();
  231. }
  232. return $out;
  233. }
  234. /**
  235. * Validate URL
  236. * @param string $str
  237. * @return bool
  238. */
  239. public function validateUrl($str)
  240. {
  241. $regex = "@([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}|"
  242. ."(((news|telnet|nttp|file|http|ftp|https)://)|(www|ftp)"
  243. ."[-A-Za-z0-9]*\\.)[-A-Za-z0-9\\.]+)(:[0-9]*)?@i";
  244. return preg_match($regex, $str);
  245. }
  246. /**
  247. * Validates package stability
  248. * @param string $str
  249. * @return bool
  250. */
  251. public function validateStability($str)
  252. {
  253. return in_array(strval($str), self::$_stability);
  254. }
  255. /**
  256. * Validate date format
  257. * @param $date
  258. * @return bool
  259. */
  260. public function validateDate($date)
  261. {
  262. $subs = null;
  263. $check1 = preg_match("/^([\d]{4})-([\d]{2})-([\d]{2})$/i", $date, $subs);
  264. if(!$check1) {
  265. return false;
  266. }
  267. return checkdate($subs[2], $subs[3], $subs[1]);
  268. }
  269. /**
  270. * Validate email
  271. * @param string $email
  272. * @return bool
  273. */
  274. public function validateEmail($email)
  275. {
  276. return preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email);
  277. }
  278. /**
  279. * Validate package name
  280. * @param $name
  281. * @return bool
  282. */
  283. public function validatePackageName($name)
  284. {
  285. return preg_match("/^[a-zA-Z0-9_-]+$/i", $name);
  286. }
  287. /**
  288. * Validate version number
  289. * @param string $version
  290. * @return bool
  291. */
  292. public function validateVersion($version)
  293. {
  294. return preg_match("/^[\d]+\.[\d]+\.[\d]+([[:alnum:]\.\-\_]+)?$/i", $version);
  295. }
  296. /**
  297. * Check versions are equal
  298. * @param string $v1
  299. * @param string $v2
  300. * @return bool
  301. */
  302. public function versionEqual($v1, $v2)
  303. {
  304. return version_compare($v1, $v2, "==");
  305. }
  306. /**
  307. * Check version $v1 <= $v2
  308. * @param string $v1
  309. * @param string $v2
  310. * @return bool
  311. */
  312. public function versionLowerEqual($v1, $v2)
  313. {
  314. return version_compare($v1, $v2, "le");
  315. }
  316. /**
  317. * Check if version $v1 lower than $v2
  318. * @param string $v1
  319. * @param string $v2
  320. * @return bool
  321. */
  322. public function versionLower($v1, $v2)
  323. {
  324. return version_compare($v1, $v2, "<");
  325. }
  326. /**
  327. * Check version $v1 >= $v2
  328. * @param string $v1
  329. * @param string $v2
  330. * @return bool
  331. */
  332. public function versionGreaterEqual($v1, $v2)
  333. {
  334. return version_compare($v1, $v2, "ge");
  335. }
  336. /**
  337. * Generic regex validation
  338. * @param string $str
  339. * @param string $regex
  340. * @return bool
  341. */
  342. public function validateRegex($str, $regex)
  343. {
  344. return preg_match($regex, $str);
  345. }
  346. /**
  347. * Check if PHP extension loaded
  348. * @param string $name Extension name
  349. * @return bool
  350. */
  351. public function validatePhpExtension($name)
  352. {
  353. return extension_loaded($name);
  354. }
  355. public function validatePHPVersion($min, $max, $ver = PHP_VERSION)
  356. {
  357. $minAccepted = true;
  358. if($min) {
  359. $minAccepted = version_compare($ver, $min, ">=");
  360. }
  361. $maxAccepted = true;
  362. if($max) {
  363. $maxAccepted = version_compate($ver, $max, "<=");
  364. }
  365. return (bool) $minAccepted && $maxAccepted;
  366. }
  367. }