/src/system/application/libraries/Wsvalidate.php

https://github.com/dstockto/joind.in · PHP · 288 lines · 118 code · 29 blank · 141 comment · 10 complexity · c99fc2226d5d4ea536a3defd53b599b6 MD5 · raw file

  1. <?php
  2. /**
  3. * Joindin validation class
  4. *
  5. * PHP version 5
  6. *
  7. * @category Joind.in
  8. * @package Configuration
  9. * @copyright 2009 - 2012 Joind.in
  10. * @license http://github.com/joindin/joind.in/blob/master/doc/LICENSE JoindIn
  11. */
  12. if (!defined('BASEPATH')) {
  13. exit('No direct script access allowed');
  14. }
  15. /**
  16. * Joindin validation class
  17. *
  18. * PHP version 5
  19. *
  20. * @category Joind.in
  21. * @package Configuration
  22. * @copyright 2009 - 2012 Joind.in
  23. * @license http://github.com/joindin/joind.in/blob/master/doc/LICENSE JoindIn
  24. */
  25. class Wsvalidate
  26. {
  27. // If the check is valid - i.e. the email address is good, return true
  28. public $CI = null;
  29. public $default_err = 'Missing value: %s';
  30. public $errs = array();
  31. public $seed = 'th1st0k3n';
  32. /**
  33. * Instantiates the validation class
  34. */
  35. public function __construct()
  36. {
  37. $this->CI = & get_instance();
  38. }
  39. /**
  40. * Validates an object based on some rules
  41. *
  42. * @param array $rules Rules to validate with
  43. * @param object $obj Object to validate
  44. *
  45. * @return array|bool
  46. */
  47. public function validate($rules, $obj)
  48. {
  49. $fail = array();
  50. //print_r($rules); print_r($obj);
  51. foreach ($rules as $k => $v) {
  52. $m = explode('|', $v); //print_r($m);
  53. //echo $v.' '.$k.' '.$obj->$k."\n";
  54. foreach ($m as $mk => $mv) {
  55. //$str=(string)$obj->$k;
  56. //chek to see if we're anything more complex
  57. if (preg_match('/\[(.*?)\]/', $mv, $matches)) {
  58. //print_r($matches);
  59. $par = array_merge(array($k, $obj), explode(',', $matches[1]));
  60. $func = str_replace($matches[0], '', $mv);
  61. $ret = call_user_func_array(
  62. array(&$this, 'validate_' . $func), $par
  63. );
  64. } else {
  65. $ret = $this->{'validate_' . $mv}($k, $obj);
  66. }
  67. if (!$ret) {
  68. if ($msg = $this->getCustErr($k)) {
  69. $fail[] = $msg;
  70. } else {
  71. $fail[] = sprintf($this->default_err, $k);
  72. }
  73. }
  74. }
  75. }
  76. return (count($fail) > 0) ? $fail : false;
  77. }
  78. /**
  79. * Sets an error message
  80. *
  81. * @param string $k Key for error message
  82. * @param string $msg Error message
  83. *
  84. * @return void
  85. */
  86. public function setCustErr($k, $msg)
  87. {
  88. $this->errs[$k] = $msg;
  89. }
  90. /**
  91. * Retrieves the custom error messages. Returns false if the key doesn't exist
  92. *
  93. * @param string $k Key to retrieve
  94. *
  95. * @return string|boolean
  96. */
  97. public function getCustErr($k)
  98. {
  99. return (isset($this->errs[$k])) ? $this->errs[$k] : false;
  100. }
  101. /**
  102. * Gets a request token from the config, echos it and ends the script
  103. *
  104. * @return string
  105. *
  106. * @todo Fix or remove
  107. */
  108. public function generateReqKey()
  109. {
  110. //print_r($_SERVER);
  111. $t = $this->config->item('token');
  112. echo $t;
  113. die();
  114. $str = $_SERVER['SCRIPT_FILENAME'];
  115. $str .= $this->seed;
  116. $str .= $_SERVER['REQUEST_TIME'];
  117. return md5($str);
  118. }
  119. /**
  120. * Validates that a required field is set
  121. *
  122. * @param string $k Key to check
  123. * @param object $obj Object to check
  124. *
  125. * @return bool
  126. */
  127. public function validate_required($k, $obj)
  128. {
  129. $str = $str = (string)$obj->$k;
  130. //return (!empty($str)) ? true : false; <-- used this but "false" tripped it
  131. return (strlen($str) > 0) ? true : false;
  132. }
  133. /**
  134. * Validates that a field is an email address
  135. *
  136. * @param string $k Key to check
  137. * @param object $obj Object to check
  138. *
  139. * @return bool
  140. */
  141. public function validate_email($k, $obj)
  142. {
  143. $str = $str = (string)$obj->$k;
  144. return (filter_var($str, FILTER_VALIDATE_EMAIL)) ? true : false;
  145. }
  146. /**
  147. * Validates that a provided date is in the future
  148. *
  149. * @param string $k Key that contains date
  150. * @param object $obj Object to check
  151. *
  152. * @return bool
  153. */
  154. public function validate_date_future($k, $obj)
  155. {
  156. $str = $str = (string)$obj->$k;
  157. return ($str >= time()) ? true : false;
  158. }
  159. /**
  160. * Does nothing. Literally it is empty
  161. *
  162. * @param string $k Key to check
  163. * @param object $obj Object to check
  164. *
  165. * @return void
  166. */
  167. public function validate_int($k, $obj)
  168. {
  169. }
  170. /**
  171. * Validates that a value is within a specified range
  172. *
  173. * @param string $k Key to check
  174. * @param object $obj Object to check
  175. * @param integer $min Minimum value
  176. * @param integer $max Maximum value
  177. *
  178. * @return bool
  179. */
  180. public function validate_range($k, $obj, $min, $max)
  181. {
  182. $this->setCustErr($k, $k . ': Number out of range!');
  183. $num = (float)$obj->$k;
  184. if (ctype_digit((string)$num)) {
  185. return ($num >= $min && $num <= $max) ? true : false;
  186. } else {
  187. return false;
  188. }
  189. }
  190. /**
  191. * Does nothing but return true.
  192. *
  193. * @param string $k Key to check
  194. * @param object $obj Object to check
  195. *
  196. * @return bool
  197. */
  198. public function validate_reqkey($k, $obj)
  199. {
  200. return true;
  201. }
  202. /**
  203. * Validates that a model is unique
  204. *
  205. * @param string $tbl Table to check
  206. * @param object $obj Model object to check
  207. *
  208. * @return boolean
  209. */
  210. public function validate_unique($tbl, $obj)
  211. {
  212. $arr = (array)$obj;
  213. //if there's attributes, unset them
  214. if (isset($arr['@attributes'])) {
  215. unset($arr['@attributes']);
  216. }
  217. $mod = $tbl . '_model';
  218. $this->CI->load->model($mod);
  219. return $this->CI->$mod->isUnique($arr);
  220. }
  221. /**
  222. * Returns true if a user is logged in
  223. *
  224. * @return bool
  225. */
  226. public function validate_loggedin()
  227. {
  228. return ($this->CI->user_model->isAuth()) ? true : false;
  229. }
  230. /**
  231. * Validates that an object represents an event
  232. *
  233. * @param string $k Key to check
  234. * @param object $obj Object to check
  235. *
  236. * @return bool
  237. */
  238. public function validate_isevent($k, $obj)
  239. {
  240. $eid = ($obj->eid) ? $obj->eid : $obj->event_id;
  241. $this->CI->load->model('event_model');
  242. $ret = $this->CI->event_model->getEventDetail($eid);
  243. return (!empty($ret)) ? true : false;
  244. }
  245. /**
  246. * Validates that an object represents a talk
  247. *
  248. * @param string $k Key to check
  249. * @param object $obj Object to check
  250. *
  251. * @return bool
  252. */
  253. public function validate_istalk($k, $obj)
  254. {
  255. $this->CI->load->model('talks_model');
  256. $ret = $this->CI->talks_model->getTalks($obj->talk_id);
  257. return (!empty($ret)) ? true : false;
  258. }
  259. }