PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/oc-includes/osclass/helpers/hValidate.php

https://code.google.com/
PHP | 308 lines | 166 code | 18 blank | 124 comment | 91 complexity | 15171cfb700e3605530b0d2474d0450c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. * OSCLass software for creating and publishing online classified
  4. * advertising platforms
  5. *
  6. * Copyright (C) 2010 OSCLASS
  7. *
  8. * This program is free software: you can redistribute it and/or
  9. * modify it under the terms of the GNU Affero General Public License
  10. * as published by the Free Software Foundation, either version 3 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * Helper Validation
  23. * @package OSClass
  24. * @subpackage Helpers
  25. * @author OSClass
  26. */
  27. /**
  28. * Validate the text with a minimum of non-punctuation characters (international)
  29. *
  30. * @param string $value
  31. * @param integer $count
  32. * @param boolean $required
  33. * @return boolean
  34. */
  35. function osc_validate_text ($value = '', $count = 1, $required = true) {
  36. if ($required || $value) {
  37. if ( !preg_match("/([\p{L}\p{N}]){".$count."}/iu", strip_tags($value)) ) {
  38. return false;
  39. }
  40. }
  41. return true;
  42. }
  43. /**
  44. * Validate one or more numbers (no periods)
  45. *
  46. * @param string $value
  47. * @return boolean
  48. */
  49. function osc_validate_int ($value) {
  50. if ( preg_match("/^[0-9]+$/", $value) ) {
  51. return true;
  52. }
  53. return false;
  54. }
  55. /**
  56. * Validate one or more numbers (no periods), must be more than 0.
  57. *
  58. * @param string $value
  59. * @return boolean
  60. */
  61. function osc_validate_nozero ($value) {
  62. if ( preg_match("/^[0-9]+$/", $value) && $value>0 ) {
  63. return true;
  64. }
  65. return false;
  66. }
  67. /**
  68. * Validate $value is a number or a numeric string
  69. *
  70. * @param string $value
  71. * @param boolean $required
  72. * @return boolean
  73. */
  74. function osc_validate_number ($value = null, $required = false) {
  75. if ($required || strlen($value) > 0) {
  76. if ( !is_numeric($value) ) {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. /**
  83. * Validate $value is a number phone,
  84. * with $count lenght
  85. *
  86. * @param string $value
  87. * @param int $count
  88. * @param boolean $required
  89. * @return boolean
  90. */
  91. function osc_validate_phone ($value = null, $count = 10, $required = false) {
  92. if ($required || strlen($value) > 0) {
  93. if ( !preg_match("/([\p{Nd}][^\p{Nd}]*){".$count."}/i", strip_tags($value)) ) {
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. /**
  100. * Validate if $value is more than $min
  101. *
  102. * @param string $value
  103. * @param int $min
  104. * @return boolean
  105. */
  106. function osc_validate_min ($value = null, $min = 6) {
  107. if ( strlen($value) < $min ) {
  108. return false;
  109. }
  110. return true;
  111. }
  112. /**
  113. * Validate if $value is less than $max
  114. * @param string $value
  115. * @param int $max
  116. * @return boolean
  117. */
  118. function osc_validate_max ($value = null, $max = 255) {
  119. if ( strlen($value) > $max ) {
  120. return false;
  121. }
  122. return true;
  123. }
  124. /**
  125. * Validate if $value belongs at range between min to max
  126. * @param string $value
  127. * @param int $min
  128. * @param int $max
  129. * @return boolean
  130. */
  131. function osc_validate_range ($value, $min = 6, $max = 255) {
  132. if ( strlen($value)>=$min && strlen($value)<=$max ) {
  133. return true;
  134. }
  135. return false;
  136. }
  137. /**
  138. * Validate if exist $city, $region, $country in db
  139. *
  140. * @param string $city
  141. * @param string $region
  142. * @param string $country
  143. * @return boolean
  144. */
  145. function osc_validate_location ($city,$sCity,$region,$sRegion,$country,$sCountry) {
  146. if ( osc_validate_nozero($city) && osc_validate_nozero($region) && osc_validate_text($country,2) ) {
  147. $data = Country::newInstance()->findByCode($country);
  148. $countryId = $data['pk_c_code'];
  149. if ( $countryId ) {
  150. $data = Region::newInstance()->findByPrimaryKey($region);
  151. $regionId = $data['pk_i_id'];
  152. if ( $data['b_active'] == 1 ) {
  153. $data = City::newInstance()->findByPrimaryKey($city);
  154. if ($data['b_active'] == 1 && $data['fk_i_region_id'] == $regionId && strtolower($data['fk_c_country_code']) == strtolower($countryId)) {
  155. return true;
  156. }
  157. }
  158. }
  159. } else if(osc_validate_nozero($region) && osc_validate_text($country,2) && $sCity != "" ) {
  160. return true;
  161. } else if($sRegion != "" && osc_validate_text($country,2) && $sCity != "" ) {
  162. return true;
  163. } else if($sRegion != "" && $sCountry != "" && $sCity != "" ){
  164. return true;
  165. }
  166. return false;
  167. }
  168. /**
  169. * Validate if exist category $value and is enabled in db
  170. *
  171. * @param string $value
  172. * @return boolean
  173. */
  174. function osc_validate_category ($value) {
  175. if ( osc_validate_nozero($value) ) {
  176. $data = Category::newInstance()->findByPrimaryKey($value);
  177. if (isset($data['b_enabled']) && $data['b_enabled'] == 1) {
  178. if(osc_selectable_parent_categories()){
  179. return true;
  180. } else {
  181. if($data['fk_i_parent_id']!=null) {
  182. return true;
  183. }
  184. }
  185. }
  186. }
  187. return false;
  188. }
  189. /**
  190. * Validate if $value url is a valid url.
  191. * Check header response to validate.
  192. *
  193. * @param string $value
  194. * @param boolean $required
  195. * @return boolean
  196. */
  197. function osc_validate_url ($value, $required = false) {
  198. if ($required || strlen($value) > 0) {
  199. $value = osc_sanitize_url($value);
  200. if(!function_exists('filter_var')) {
  201. $success = preg_match('|^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$|', $value, $m);
  202. } else {
  203. $success = filter_var($value, FILTER_VALIDATE_URL);
  204. }
  205. if ($success) {
  206. @$headers = get_headers($value);
  207. if (!preg_match('/^HTTP\/\d\.\d\s+(200|301|302)/', $headers[0])) {
  208. return false;
  209. }
  210. } else {
  211. return false;
  212. }
  213. }
  214. return true;
  215. }
  216. /**
  217. * Validate time between two items added/comments
  218. *
  219. * @param string $type
  220. * @return boolean
  221. */
  222. function osc_validate_spam_delay($type = 'item') {
  223. if ($type == 'item') {
  224. $delay = osc_item_spam_delay();
  225. $saved_as = 'last_submit_item';
  226. } else {
  227. $delay = osc_comment_spam_delay();
  228. $saved_as = 'last_submit_comment';
  229. }
  230. // check $_SESSION
  231. if ((Session::newInstance()->_get($saved_as)+$delay) > time() ||
  232. (Cookie::newInstance()->get_value($saved_as)+$delay) > time()) {
  233. return false;
  234. }
  235. return true;
  236. }
  237. /**
  238. * Validate an email address
  239. * Source: http://www.linuxjournal.com/article/9585?page=0,3
  240. *
  241. * @param string $email
  242. * @param boolean $required
  243. * @return boolean
  244. */
  245. function osc_validate_email ($email, $required = true) {
  246. if ($required || strlen($email) > 0) {
  247. $atIndex = strrpos($email, "@");
  248. if (is_bool($atIndex) && !$atIndex) {
  249. return false;
  250. } else {
  251. $domain = substr($email, $atIndex+1);
  252. $local = substr($email, 0, $atIndex);
  253. $localLen = strlen($local);
  254. $domainLen = strlen($domain);
  255. if ($localLen < 1 || $localLen > 64) {
  256. return false;
  257. } else if ($domainLen < 1 || $domainLen > 255) {
  258. return false;
  259. } else if ($local[0] == '.' || $local[$localLen-1] == '.') {
  260. return false;
  261. } else if (preg_match('/\\.\\./', $local)) {
  262. return false;
  263. } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
  264. return false;
  265. } else if (preg_match('/\\.\\./', $domain)) {
  266. return false;
  267. } else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&amp;`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
  268. if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) {
  269. return false;
  270. }
  271. }
  272. return true;
  273. }
  274. }
  275. return true;
  276. }
  277. /**
  278. * validate username, accept letters plus underline, without separators
  279. *
  280. * @param $value
  281. * @param $min
  282. */
  283. function osc_validate_username( $value, $min = 1 ) {
  284. if(strlen($value) >= $min && preg_match('/^[A-Za-z0-9_]+$/',$value) ){
  285. return true;
  286. } else {
  287. return false;
  288. }
  289. }
  290. ?>