PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/downloader/lib/Mage/Connect/Validator.php

https://bitbucket.org/jokusafet/magento2
PHP | 481 lines | 219 code | 36 blank | 226 comment | 32 complexity | d1bd9f06448f65b9c11b46ee6e4e9a47 MD5 | raw file
  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) 2012 X.commerce, Inc. (http://www.magentocommerce.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. /**
  36. * Array of stability variants
  37. *
  38. * @var array
  39. */
  40. protected static $_stability = array(0=>'devel',1=>'alpha',2=>'beta',3=>'stable');
  41. /**
  42. * Get array of Stability variants
  43. *
  44. * @static
  45. * @return array
  46. */
  47. public static function getStabilities()
  48. {
  49. return self::$_stability;
  50. }
  51. /**
  52. * Compare stabilities. Returns:
  53. *
  54. * -1 if the first stability is lower than the second
  55. * 0 if they are equal
  56. * 1 if the second is lower.
  57. *
  58. * @param int|string $s1
  59. * @param int|string $s2
  60. * @return int|null
  61. */
  62. public function compareStabilities($s1, $s2)
  63. {
  64. $list = $this->getStabilities();
  65. $tmp = array_combine(array_values($list),array_keys($list));
  66. if (!isset($tmp[$s1], $tmp[$s2])) {
  67. throw new Exception("Invalid stability in compareStabilities argument");
  68. }
  69. $s1 = $tmp[$s1];
  70. $s2 = $tmp[$s2];
  71. if ($s1 === $s2) {
  72. return 0;
  73. } elseif ($s1 > $s2) {
  74. return 1;
  75. } elseif ($s1 < $s2) {
  76. return -1;
  77. }
  78. return null;
  79. }
  80. /**
  81. * Constructor
  82. */
  83. public function __construct()
  84. {
  85. }
  86. /**
  87. * Validate max len of string
  88. *
  89. * @param string $str
  90. * @param int $maxLen
  91. * @return bool
  92. */
  93. public function validateMaxLen($str, $maxLen)
  94. {
  95. return strlen((string) $str) <= (int) $maxLen;
  96. }
  97. /**
  98. * Validate channel name and url
  99. *
  100. * @param mixed $str
  101. * @return bool
  102. */
  103. public function validateChannelNameOrUri($str)
  104. {
  105. return ( $this->validateUrl($str) || $this->validatePackageName($str));
  106. }
  107. /**
  108. * Validate License url
  109. *
  110. * @param mixed $str
  111. * @return boolean
  112. */
  113. public function validateLicenseUrl($str)
  114. {
  115. if ($str) {
  116. return ( $this->validateUrl($str) || $this->validatePackageName($str));
  117. }
  118. return true;
  119. }
  120. /**
  121. * Validate compatible data
  122. *
  123. * @param array $data
  124. * @return bool
  125. */
  126. public function validateCompatible(array $data)
  127. {
  128. if (!count($data)) {
  129. /**
  130. * Allow empty
  131. */
  132. return true;
  133. }
  134. $count = 0;
  135. foreach ($data as $v) {
  136. /**
  137. * Converts an array to variables
  138. * @var $channel string Channel Name
  139. * @var $name string Package Name
  140. * @var $max string Maximum version number
  141. * @var $min string Minimum version number
  142. */
  143. foreach (array('name','channel','min','max') as $fld) {
  144. $$fld = trim($v[$fld]);
  145. }
  146. $count++;
  147. $res = $this->validateUrl($channel) && strlen($channel);
  148. if (!$res) {
  149. $this->addError("Invalid or empty channel in compatibility #{$count}");
  150. }
  151. $res = $this->validatePackageName($name) && strlen($name);
  152. if (!$res) {
  153. $this->addError("Invalid or empty name in compatibility #{$count}");
  154. }
  155. $res1 = $this->validateVersion($min);
  156. if (!$res1) {
  157. $this->addError("Invalid or empty minVersion in compatibility #{$count}");
  158. }
  159. $res2 = $this->validateVersion($max);
  160. if (!$res2) {
  161. $this->addError("Invalid or empty maxVersion in compatibility #{$count}");
  162. }
  163. if ($res1 && $res2 && $this->versionLower($max, $min)) {
  164. $this->addError("Max version is lower than min in compatibility #{$count}");
  165. }
  166. }
  167. return !$this->hasErrors();
  168. }
  169. /**
  170. * Validate authors of package
  171. *
  172. * @param array $authors
  173. * @return bool
  174. */
  175. public function validateAuthors(array $authors)
  176. {
  177. if (!count($authors)) {
  178. $this->addError('Empty authors section');
  179. return false;
  180. }
  181. $count = 0;
  182. foreach ($authors as $v) {
  183. $count++;
  184. array_map('trim', $v);
  185. $name = $v['name'];
  186. $login = $v['user'];
  187. $email = $v['email'];
  188. $res = $this->validateMaxLen($name, 256) && strlen($name);
  189. if (!$res) {
  190. $this->addError("Invalid or empty name for author #{$count}");
  191. }
  192. $res = $this->validateAuthorName($login) && strlen($login);
  193. if (!$res) {
  194. $this->addError("Invalid or empty login for author #{$count}");
  195. }
  196. $res = $this->validateEmail($email);
  197. if (!$res) {
  198. $this->addError("Invalid or empty email for author #{$count}");
  199. }
  200. }
  201. return !$this->hasErrors();
  202. }
  203. /**
  204. * Validator errors
  205. *
  206. * @var array
  207. */
  208. private $_errors = array();
  209. /**
  210. * Add error
  211. *
  212. * @param string $err
  213. * @return void
  214. */
  215. private function addError($err)
  216. {
  217. $this->_errors[] = $err;
  218. }
  219. /**
  220. * Set validator errors
  221. *
  222. * @param array $err
  223. * @return void
  224. */
  225. private function setErrors(array $err)
  226. {
  227. $this->_errors = $err;
  228. }
  229. /**
  230. * Clear validator errors
  231. *
  232. * @return void
  233. */
  234. private function clearErrors()
  235. {
  236. $this->_errors = array();
  237. }
  238. /**
  239. * Check if there are validator errors set
  240. *
  241. * @return int
  242. */
  243. public function hasErrors()
  244. {
  245. return count($this->_errors) != 0;
  246. }
  247. /**
  248. * Get errors
  249. *
  250. * @param bool $clear if true after this call erros will be cleared
  251. * @return array
  252. */
  253. public function getErrors($clear = true)
  254. {
  255. $out = $this->_errors;
  256. if ($clear) {
  257. $this->clearErrors();
  258. }
  259. return $out;
  260. }
  261. /**
  262. * Validate URL
  263. *
  264. * @param string $str
  265. * @return bool
  266. */
  267. public function validateUrl($str)
  268. {
  269. $regex = "@([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}|"
  270. ."(((news|telnet|nttp|file|http|ftp|https)://)|(www|ftp)"
  271. ."[-A-Za-z0-9]*\\.)[-A-Za-z0-9\\.]+)(:[0-9]*)?@i";
  272. return preg_match($regex, $str);
  273. }
  274. /**
  275. * Validates package stability
  276. *
  277. * @param string $str
  278. * @return bool
  279. */
  280. public function validateStability($str)
  281. {
  282. return in_array(strval($str), self::$_stability);
  283. }
  284. /**
  285. * Validate date format
  286. *
  287. * @param $date
  288. * @return bool
  289. */
  290. public function validateDate($date)
  291. {
  292. $subs = null;
  293. $check1 = preg_match("/^([\d]{4})-([\d]{2})-([\d]{2})$/i", $date, $subs);
  294. if (!$check1) {
  295. return false;
  296. }
  297. return checkdate($subs[2], $subs[3], $subs[1]);
  298. }
  299. /**
  300. * Validate email
  301. * @param string $email
  302. * @return bool
  303. */
  304. public function validateEmail($email)
  305. {
  306. return preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email);
  307. }
  308. /**
  309. * Validate package name
  310. * @param $name
  311. * @return bool
  312. */
  313. public function validatePackageName($name)
  314. {
  315. return preg_match("/^[a-zA-Z0-9_]+$/i", $name);
  316. }
  317. /**
  318. * Validate author name
  319. *
  320. * @param string $name
  321. * @return bool
  322. */
  323. public function validateAuthorName($name)
  324. {
  325. return preg_match("/^[a-zA-Z0-9_-]+$/i", $name);
  326. }
  327. /**
  328. * Validate version number
  329. *
  330. * @param string $version
  331. * @return bool
  332. */
  333. public function validateVersion($version)
  334. {
  335. return preg_match("/^[\d]+\.[\d]+\.[\d]+([[:alnum:]\.\-\_]+)?$/i", $version);
  336. }
  337. /**
  338. * Check versions are equal
  339. *
  340. * @param string $v1
  341. * @param string $v2
  342. * @return bool
  343. */
  344. public function versionEqual($v1, $v2)
  345. {
  346. return version_compare($v1, $v2, "==");
  347. }
  348. /**
  349. * Check version $v1 <= $v2
  350. *
  351. * @param string $v1
  352. * @param string $v2
  353. * @return bool
  354. */
  355. public function versionLowerEqual($v1, $v2)
  356. {
  357. return version_compare($v1, $v2, "le");
  358. }
  359. /**
  360. * Check if version $v1 lower than $v2
  361. *
  362. * @param string $v1
  363. * @param string $v2
  364. * @return bool
  365. */
  366. public function versionLower($v1, $v2)
  367. {
  368. return version_compare($v1, $v2, "<");
  369. }
  370. /**
  371. * Check version $v1 >= $v2
  372. *
  373. * @param string $v1
  374. * @param string $v2
  375. * @return bool
  376. */
  377. public function versionGreaterEqual($v1, $v2)
  378. {
  379. return version_compare($v1, $v2, "ge");
  380. }
  381. /**
  382. * Generic regex validation
  383. *
  384. * @param string $str
  385. * @param string $regex
  386. * @return bool
  387. */
  388. public function validateRegex($str, $regex)
  389. {
  390. return preg_match($regex, $str);
  391. }
  392. /**
  393. * Check if PHP extension loaded
  394. *
  395. * @param string $name Extension name
  396. * @return bool
  397. */
  398. public function validatePhpExtension($name)
  399. {
  400. return extension_loaded($name);
  401. }
  402. /**
  403. * Validate PHP version
  404. *
  405. * @param string $min
  406. * @param string $max
  407. * @param string $ver
  408. * @return bool
  409. */
  410. public function validatePHPVersion($min, $max, $ver = PHP_VERSION)
  411. {
  412. $minAccepted = true;
  413. if ($min) {
  414. $minAccepted = version_compare($ver, $min, ">=");
  415. }
  416. $maxAccepted = true;
  417. if ($max) {
  418. $maxAccepted = version_compare($ver, $max, "<=");
  419. }
  420. return (bool) $minAccepted && $maxAccepted;
  421. }
  422. /**
  423. * Validate contents of package
  424. *
  425. * @param array $contents
  426. * @param Mage_Connect_Config $config
  427. * @return bool
  428. */
  429. public function validateContents(array $contents, $config)
  430. {
  431. if (!count($contents)) {
  432. $this->addError('Empty package contents section');
  433. return false;
  434. }
  435. $targetPath = rtrim($config->magento_root, "\\/");
  436. foreach ($contents as $file) {
  437. $dest = $targetPath . DS . $file;
  438. if (file_exists($dest)) {
  439. $this->addError("'{$file}' already exists");
  440. return false;
  441. }
  442. }
  443. return true;
  444. }
  445. }