gyro/core/model/classes/formvalidations.facade.php
Go to the documentation of this file.00001 <?php 00002 /** 00003 * Stores form tokens 00004 * 00005 * @author Gerd Riesselmann 00006 * @ingroup Model 00007 */ 00008 class FormValidations { 00009 /** 00010 * Create a token for form of given name 00011 */ 00012 public static function create_token($name) { 00013 self::remove_expired(); 00014 00015 $token = Common::create_token(); 00016 $validations = self::create_token_instance($name, $token); 00017 $validations->expirationdate = time() + Config::get_value(Config::FORMVALIDATION_EXPIRATION_TIME) * GyroDate::ONE_MINUTE; // 00018 $validations->insert(); 00019 00020 return $token; 00021 } 00022 00023 /** 00024 * Create a token for form of given name or reuse if it was created for this request 00025 */ 00026 public static function create_or_reuse_token($name) { 00027 $token = RuntimeCache::get(array('reusedtokens', $name)); 00028 if (!$token) { 00029 $token = self::create_token($name); 00030 RuntimeCache::set(array('reusedtokens', $name), $token); 00031 } 00032 return $token; 00033 } 00034 00035 /** 00036 * Create a token for form of given name or reuse if it was created for this request 00037 */ 00038 public static function create_or_reuse_token_across_requests($name) { 00039 $ret = ''; 00040 00041 $validations = new DAOFormvalidations(); 00042 $validations->name = $name; 00043 $validations->sessionid = Session::get_session_id(); 00044 if ($validations->find(IDataObject::AUTOFETCH)) { 00045 if ($validations->is_valid_for_at_least(10)) { 00046 $ret = $validations->token; 00047 } 00048 } 00049 if ($ret) { 00050 return $ret; 00051 } else { 00052 return self::create_token($name); 00053 } 00054 } 00055 00056 /** 00057 * Validate a given token for form of given name 00058 * 00059 * @return Boolean 00060 */ 00061 public static function validate_token($name, $token) { 00062 $ret = false; 00063 00064 $validations = self::create_token_instance($name, $token); 00065 if ($validations->find(IDataObject::AUTOFETCH)) { 00066 $ret = ($validations->expirationdate > time()); 00067 $validations->delete(); 00068 } 00069 00070 return $ret; 00071 } 00072 00073 /** 00074 * @static 00075 * @param $name 00076 * @param $token 00077 * @return DAOFormvalidations 00078 */ 00079 private static function create_token_instance($name, $token) { 00080 $validations = new DAOFormvalidations(); 00081 00082 $validations->name = $name; 00083 $validations->token = $token; 00084 $validations->sessionid = Session::get_session_id(); 00085 00086 return $validations; 00087 } 00088 00089 00090 /** 00091 * Removes expired cache entries 00092 */ 00093 public static function remove_expired() { 00094 $dao = new DAOFormvalidations(); 00095 $dao->add_where('expirationdate', '<', DBFieldDateTime::NOW); 00096 $dao->delete(DAOFormvalidations::WHERE_ONLY); 00097 } 00098 }