PageRenderTime 35ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/form.class.php

http://freewms.googlecode.com/
PHP | 407 lines | 248 code | 29 blank | 130 comment | 68 complexity | b6b9e03da8e83e3304ef517d4cd51e49 MD5 | raw file
  1. <?php if(!defined('BASEPATH')) die('Access Denied');
  2. /*-------------------------------------------------
  3. * FreeWMS - A Free Website Management System
  4. * Ver:0.1.0 Update: 2010-05-16
  5. * Home: http://code.google.com/p/freewms
  6. * Copyright 2010, FreeWMS Team, SOVO, Neusoft
  7. * Released under the New BSD Licenses
  8. *-------------------------------------------------*/
  9. class Form {
  10. private static $post = NULL;
  11. private static $fields = NULL;
  12. private static $errors = NULL;
  13. public function __construct(&$postarray) {
  14. Lang::load('form');
  15. if(is_array($postarray) && !empty($postarray)) {
  16. self::$post =& $postarray;
  17. }
  18. }
  19. public function __destruct() {
  20. self::$errors = NULL;
  21. self::$fields = NULL;
  22. }
  23. /**
  24. * ????????
  25. * @param string $field ???
  26. * @param string $lable ????
  27. * @param string $rules ????
  28. * @param string $filters ????
  29. */
  30. public function set_field($field, $lable = NULL, $rules = NULL, $filters = NULL) {
  31. if(is_array($field)) {
  32. foreach($field as $key => $value) {
  33. $this->set_field($key, $value['lable'], $value['rules'], $value['filters']);
  34. }
  35. }else{
  36. $lable = empty($lable) ? $field : $lable;
  37. $rules = explode('|', $rules);
  38. $filters = explode('|', $filters);
  39. self::$fields[$field] = array(
  40. 'field' => $field,
  41. 'label' => $lable,
  42. 'rules' => $rules,
  43. 'filters' => $filters,
  44. 'value' => self::$post[$field]
  45. );
  46. }
  47. }
  48. /**
  49. * ?????????POST??
  50. * @return bool
  51. */
  52. public function is_post() {
  53. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  54. return TRUE;
  55. }else{
  56. return FALSE;
  57. }
  58. }
  59. /**
  60. * ??????
  61. * @return bool
  62. */
  63. public function run() {
  64. if(count(self::$post) == 0 || $_SERVER['REQUEST_METHOD'] != 'POST') {
  65. return FALSE;
  66. }
  67. if(count(self::$fields) == 0) {
  68. return FALSE;
  69. }
  70. self::$post = NULL;
  71. foreach(self::$fields as $row) {
  72. //??????
  73. if(empty($row['rules'])) {
  74. continue;
  75. }
  76. foreach($row['rules'] as $rule) {
  77. $param = FALSE;
  78. if(preg_match("/(.*?)\\[(.*)\\]/", $rule, $match)) {
  79. $rule = $match[1];
  80. $param = $match[2];
  81. }
  82. $is_method = FALSE;
  83. if(!function_exists($rule)) {
  84. if(method_exists($this, $rule)) {
  85. $is_method = TRUE;
  86. }else{
  87. continue;
  88. }
  89. }
  90. if($is_method == TRUE) {
  91. $rst = $this->$rule($row['value'], $param);
  92. }else{
  93. $rst = $rule($row['value'], $param);
  94. }
  95. if($rst != FALSE) {
  96. $rst = sprintf($rst, $row['label']);
  97. self::$errors[$row['field']] = $rst;
  98. break;
  99. }
  100. }
  101. //??????
  102. if(empty($row['filters'])) {
  103. continue;
  104. }
  105. $value = $row['value'];
  106. foreach($row['filters'] as $filter) {
  107. if(function_exists($filter)) {
  108. $value = $filter($value);
  109. }
  110. }
  111. self::$post[$row['field']] = $value;
  112. }
  113. if(count(self::$errors) > 0) {
  114. return FALSE;
  115. }
  116. return TRUE;
  117. }
  118. /**
  119. * ????????
  120. * @return mixed
  121. */
  122. public static function get_all_errors($type = NULL) {
  123. if(count(self::$errors) > 0) {
  124. if($type == NULL) {
  125. return array_values(self::$errors);
  126. }else{
  127. return implode($type, array_values(self::$errors));
  128. }
  129. }
  130. return FALSE;
  131. }
  132. /**
  133. * ?????????????
  134. * @param string $field
  135. * @return mixed
  136. */
  137. public static function get_error($field, $prefix = NULL, $surfix = NULL) {
  138. if(!empty(self::$errors[$field])) {
  139. return $prefix.self::$errors[$field].$surfix;
  140. }else{
  141. return NULL;
  142. }
  143. }
  144. /**
  145. * ????????
  146. * @param string $field
  147. * @param string $default
  148. * @return string
  149. */
  150. public static function set_value($field, $default = NULL) {
  151. if(is_null(self::$post[$field])) {
  152. return $default;
  153. }else{
  154. return self::$post[$field];
  155. }
  156. }
  157. //----------------------------------------------------------------
  158. // ???????? ???????????
  159. //----------------------------------------------------------------
  160. /**
  161. * ??????-??????
  162. * @param string/array $str ????
  163. * @return mixed
  164. */
  165. private function required($str) {
  166. $msg = Lang::_('form_required');
  167. if (!is_array($str)) {
  168. return (trim($str) == '') ? $msg : FALSE;
  169. }else{
  170. return (empty($str)) ? $msg : FALSE;
  171. }
  172. }
  173. /**
  174. * ?????????
  175. * @param string $str ????
  176. * @param string $field ????
  177. * @return bool
  178. */
  179. private function matches($str, $field) {
  180. $fieldname = empty(self::$fields[$field]['label']) ? $field : self::$fields[$field]['label'];
  181. $msg = str_replace('$1', $fieldname, Lang::_('form_matches'));
  182. $field = self::$fields[$field]['value'];
  183. return ($str !== $field) ? $msg : FALSE;
  184. }
  185. /**
  186. * ?????????
  187. * @param string $str ????
  188. * @param string $val ????
  189. * @return bool
  190. */
  191. private function min_length($str, $val) {
  192. $msg = str_replace('$1', $val, Lang::_('form_min_length'));
  193. if(function_exists('mb_strlen')) {
  194. return (mb_strlen($str) < $val) ? $msg : FALSE;
  195. }
  196. return (strlen($str) < $val) ? $msg : FALSE;
  197. }
  198. /**
  199. * ?????????
  200. * @param string $str ????
  201. * @param string $val ????
  202. * @return bool
  203. */
  204. private function max_length($str, $val) {
  205. $msg = str_replace('$1', $val, Lang::_('form_max_length'));
  206. if(function_exists('mb_strlen')) {
  207. return (mb_strlen($str) > $val) ? $msg : FALSE;
  208. }
  209. return (strlen($str) > $val) ? $msg : FALSE;
  210. }
  211. /**
  212. * ??????????
  213. * @param string $str ????
  214. * @param string $val ???
  215. * @return bool
  216. */
  217. private function max_num($str, $val) {
  218. $msg = str_replace('$1', $val, Lang::_('form_max_num'));
  219. if($str > $val) {
  220. return $msg;
  221. }else{
  222. return FALSE;
  223. }
  224. }
  225. /**
  226. * ??????????
  227. * @param string $str ????
  228. * @param string $val ???
  229. * @return bool
  230. */
  231. private function min_num($str, $val) {
  232. $msg = str_replace('$1', $val, Lang::_('form_min_num'));
  233. if($str < $val) {
  234. return $msg;
  235. }else{
  236. return FALSE;
  237. }
  238. }
  239. /**
  240. * ?????????????
  241. * @param string $str ??
  242. * @param ini $val ????
  243. * @return bool
  244. */
  245. private function exact_length($str, $val) {
  246. $msg = str_replace('$1', $val, Lang::_('form_exact_length'));
  247. if (preg_match("/[^0-9]/", $val)) {
  248. return $msg;
  249. }
  250. if (function_exists('mb_strlen')) {
  251. return (mb_strlen($str) != $val) ? $msg : FALSE;
  252. }
  253. return (strlen($str) != $val) ? $msg : FALSE;
  254. }
  255. /**
  256. * ??Email??????
  257. * @param string $str ?????
  258. * @return bool
  259. */
  260. private function valid_email($str) {
  261. $msg = Lang::_('form_valid_email');
  262. if($str == '') return FALSE;
  263. return (!preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? $msg : FALSE;
  264. }
  265. /**
  266. * ??email?
  267. * @param string $str ??
  268. * @return bool
  269. */
  270. private function valid_emails($str) {
  271. $msg = Lang::_('form_valid_emails');
  272. if($str == '') return FALSE;
  273. if(strpos($str, ',') === FALSE) {
  274. return $this->valid_email(trim($str));
  275. }
  276. foreach(explode(',', $str) as $email) {
  277. if(trim($email) != '' && $this->valid_email(trim($email)) !== FALSE) {
  278. return $msg;
  279. }
  280. }
  281. return FALSE;
  282. }
  283. /**
  284. * ????????IP??
  285. * @param string $ip
  286. * @return bool
  287. */
  288. private function valid_ip($ip) {
  289. $msg = Lang::_('form_valid_ip');
  290. if($str == '') return FALSE;
  291. $ip_segments = explode('.', $ip);
  292. if (count($ip_segments) != 4) {
  293. return $msg;
  294. }
  295. if ($ip_segments[0][0] == '0') {
  296. return $msg;
  297. }
  298. foreach ($ip_segments as $segment) {
  299. if ($segment == '' || preg_match("/[^0-9]/", $segment) || $segment > 255 || strlen($segment) > 3) {
  300. return $msg;
  301. }
  302. }
  303. return FALSE;
  304. }
  305. /**
  306. * ?????????, ?????????
  307. * @param string $str ??
  308. * @return bool
  309. */
  310. private function dir_name($str) {
  311. $msg = Lang::_('form_dir_name');
  312. if(empty($str)) return FALSE;
  313. return (!preg_match("/^([a-z0-9_])+$/i", $str)) ? $msg : FALSE;
  314. }
  315. /**
  316. * ????????????
  317. * @param string $str ??
  318. * @return bool
  319. */
  320. private function user_name($str) {
  321. $msg = Lang::_('form_user_name');
  322. if(empty($str)) return FALSE;
  323. return (!preg_match('/^[\x{4e00}-\x{9fa5}a-z_A-Z0-9]+$/u', $str)) ? $msg : FALSE;
  324. }
  325. /**
  326. * ?????????
  327. * @param string $str ??
  328. * @return bool
  329. */
  330. private function numeric($str) {
  331. $msg = Lang::_('form_numeric');
  332. if($str == '') return FALSE;
  333. return preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str) ? FALSE : $msg;
  334. }
  335. /**
  336. * ????????
  337. * @param string $str ??
  338. * @return bool
  339. */
  340. private function integer($str) {
  341. $msg = Lang::_('form_integer');
  342. if($str == '') return FALSE;
  343. return preg_match( '/^[\-+]?[0-9]+$/', $str) ? FALSE : $msg;
  344. }
  345. /**
  346. * ????????
  347. * @param string $str
  348. * @return bool
  349. */
  350. private function natural($str) {
  351. $msg = Lang::_('form_is_natural');
  352. if($str == '') return FALSE;
  353. return (bool)preg_match('/^[0-9]+$/', $str) ? FALSE : $msg;
  354. }
  355. /**
  356. * ?????????????
  357. */
  358. private function regex($str, $pattern) {
  359. $msg = Lang::_('form_regex');
  360. return (bool)preg_match('/'.$pattern.'/i', $str) ? FALSE : $msg;
  361. }
  362. /**
  363. * ?????????
  364. */
  365. private function valid_code($str) {
  366. $msg = Lang::_('form_valid_code');
  367. $code = Session::flash('valid_code');
  368. if(empty($code)) {
  369. return $msg;
  370. }else{
  371. if(strtolower($code) != strtolower($str)) {
  372. return $msg;
  373. }
  374. }
  375. return FALSE;
  376. }
  377. }
  378. /* End of this file */