8 * @author Cake <cake_67@users.sourceforge.jp>
9 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
10 * @link http://trpgtools-onweb.sourceforge.jp/
13 app::import('Sanitize');
14 class AppModel extends Model {
16 'Cakeplus.ValidationErrorI18n',
17 'Cakeplus.AddValidationRule',
23 var $public_flag = array(
31 * var $fields = array(
32 * 'field_name' => array( // field名
33 * 'auto' => false, // true:自動設定値 ユーザ設定不可(id, created, modified...)
34 * 'add' => true, // true: 追加画面でフォームを表示する項目
35 * 'edit' => false, // 編集画面でフォーム表示を表示する項目
36 * 'escape' => array( // escape処理
37 * 'html' => true, // true: Sanitize::html()を行なう
38 * 'all' => true, // true: Sanitize::stripAll()を行なう
39 * 'whitespace' => true, // true: Sanitize::stripWhitespace()を行なう
40 * 'images' => true, // true: Sanitize::stripImages()を行なう
41 * 'sctipts' => true, // true: Sanitize::stripScripts()を行なう
44 * $fields設定は個々のモデルで行なう
46 var $fields = array();
48 function __construct($id = false, $table = null, $ds = null) {
49 parent::__construct($id, $table, $ds);
51 // MySQLが非UTF-8設定のサーバ対策
52 mysql_set_charset('utf8');
56 function beforeValidate($options = array())
58 // i18n Error Messages
59 // Working on the CakePlus
60 $error_messages = array(
61 'alphaNumeric' => __('Please input only alphanumeric characters.', true),
62 'between' => __('Between %2$d and %3$d characters.', true),
63 'betweenWrapper' => __('Too short or Too long.', true),
64 'boolean' => __('Incorrect value.', true),
65 'compare2fields' => __('Input same as avobe.', true),
66 'comparison' => __('Please input number %2$s %3$d.', true),
67 'isPcMail' => __('Invalid PC-mail address.', true),
68 'isUnique' => __('It was already registed.', true),
69 'isUniquePcMail' => __('It was already registed.', true),
70 'isUniqueRegistMail' => __('It is registering-mail.', true),
71 'isUniqueKeyname4profile' => __('It was already registed.', true),
72 'isUniqueKeyname4system' => __('It was already registed.', true),
73 'keyName' => __('Please input number, small letter and underline only.', true),
74 'minLength' => __('More than %2$d characters', true),
75 'maxLength' => __('Less than %2$d characters', true),
76 'notEmpty' => __('Please be sure to input.', true),
77 'numeric' => __('Please input only number.', true),
78 'profileType' => __('Invalid Type.', true),
79 'publicFlag' => __('Incorrect value.', true),
80 'url' => __('Please input URL.', true),
81 'validProfileId' => __('Invalid Profile.', true),
82 'validProfileSelectId' => __('Invalid ProfileSelect.', true),
83 'validProfiletableId' => __('Invalid ProfileTable.', true),
84 'validSystemId' => __('Invalid System.', true),
85 'validStatus' => __('Invalid Status.', true),
88 'resource' => __('The file is invalid.', true),
89 'access' => __('The file cannot be processed.', true),
90 'location' => __('The file cannot be transferred from or to location.', true),
91 'permission' => __('Executable files cannot be uploaded.', true),
92 'size' => __('The file must be less than %2$s bytes.', true),
93 'pixels' => __('The file must be within %2$s pixel.', true),
94 'extension' => __('The file has the wrong extension.', true),
95 'mimeType' => __('The file has the wrong MIME type.', true),
99 $error_messages['minLengthJP'] = $error_messages['minLength'];
100 $error_messages['maxLengthJP'] = $error_messages['maxLength'];
102 $this->setErrorMessageI18n($error_messages, false);
104 $this->replaceValidationErrorMessagesI18n();
106 return parent::beforeValidate($options);
109 function beforeSave($options = array())
111 // $fieldsの設定でSanitize
112 if (isset($this->data[$this->name])) {
113 foreach($this->data[$this->name] as $field => $value) {
114 $this->data[$this->name][$field] = $this->escapeByFields($field, $value);
116 // Sanitize(不正コード、SQL Injection)
117 $this->data = Sanitize::clean(
125 return parent::beforeSave($options);
130 /** It Overwrites the original alphaNumeric() **/
131 function alphaNumeric($data)
133 $check = is_array($data) ? array_shift($data) : $data;
134 if (preg_match('/^[0-9a-z]+$/i',$check)) {
141 /* Wrapper to the origibal between() */
142 function betweenWrapper($check, $min, $max)
144 return Validation::between($check, $min, $max);
147 /* check PublicFlag Setting */
148 function publicFlag($data)
150 if (isset($data["public_flag"]) && in_array($data["public_flag"], $this->public_flag)) {
158 function validUserId($data, $is_active = true)
160 if (!isset($data['user_id']) || !$data['user_id']) {
164 if (!isset($this->User)) {
165 $this->User = CorePlus::set_model('User');
167 $user = $this->User->read(null, $data['user_id']);
168 if (!$user['User']) {
173 if (!isset($this->Group)) {
174 $this->Group = CorePlus::set_model('Group');
176 if (!in_array($user['User']['group_id'], $this->Group->is_active)) {
184 /* valid System Id */
185 function validSystemId($data, $is_public = true)
187 $this->System = CorePlus::set_model('System');
191 'fields' => 'System.id',
192 'conditions' => array(
193 'System.id' => $data["system_id"],
197 if ($is_public === true || $is_public == 'public') {
198 $params['conditions']['System.public_flag'] = 'public';
200 if (isset($params['conditions']['System.public_flag'])) {
201 unset($params['conditions']['System.public_flag']);
204 $system_id = $this->System->find('list', $params);
206 return (bool)$system_id;
209 /* keyName (number, small letter and underline) */
210 function keyName($data)
212 $check = is_array($data) ? array_shift($data) : $data;
213 return (bool)preg_match('/^[_0-9a-z]+$/', $check);
217 function isUniqueRegistMail($data)
220 $registId = $this->getRegistId4address($data);
222 return (bool)$registId;
224 function getRegistId4address($data)
226 if (isset($data['pcmail'])) {
227 $mail = $data['pcmail'];
228 } elseif (isset($data['mail'])) {
229 $mail = $data['mail'];
239 $encrypted_address = $this->getEncryptedAddress($mail);
241 $this->RegistMail = CorePlus::set_model('RegistMail');
243 'RegistMail.mail' => $encrypted_address,
248 $user = $this->RegistMail->find('first', array(
249 'conditions' => $conditions,
254 if (isset($user['RegistMail']['id'])) {
255 return $user['RegistMail']['id'];
261 function getRegistData4code($code)
267 $this->RegistMail = CorePlus::set_model('RegistMail');
269 'RegistMail.key_code' => $code,
271 return $this->RegistMail->find('first', array(
272 'conditions' => $conditions,
276 function deleteRegistData4mail($mail)
282 $this->RegistMail = CorePlus::set_model('RegistMail');
284 'RegistMail.mail' => $mail,
286 return $this->RegistMail->deleteAll($conditions);
289 function isUniquePcMail($data)
292 $userId = $this->getUserId4address($data);
294 return !(bool)$userId;
296 function getUserId4address($data)
298 if (isset($data['pcmail'])) {
299 $mail = $data['pcmail'];
300 } elseif (isset($data['mail'])) {
301 $mail = $data['mail'];
311 $encrypted_address = $this->getEncryptedAddress($mail);
313 $this->User = CorePlus::set_model('User');
315 'User.pcmail' => $encrypted_address,
320 $user = $this->User->find('first', array(
321 'conditions' => $conditions,
326 if (isset($user['User']['id'])) {
327 return $user['User']['id'];
333 function getEncryptedAddress($mail)
335 if (!isset($this->Crypt)) {
336 App::import('Component', 'Crypt');
337 $this->Crypt = new CryptComponent;
339 return $this->Crypt->crypt($mail);
342 function isPcMail($data)
344 if (isset($data['pcmail'])) {
345 $mail = $data['pcmail'];
346 } elseif (isset($data['mail'])) {
347 $mail = $data['mail'];
356 if (!$this->isAddress($mail)) {
360 return !$this->checkMobileDomain($mail);
362 function checkMobileDomain($data)
367 if (!Configure::read('mobile.domain')) {
371 $pieces = explode('@', $data);
372 $domain = array_pop($pieces);
374 return in_array($domain, Configure::read('mobile.domain'));
376 function isAddress($data)
382 if (preg_match('/^[^:;@,\s\x80-\xFF]+@\w[\w\-.]*\.[a-zA-Z]+$/', $data)) {
389 function find($type = null, $queryData = array())
391 $args = array($type, $queryData);
393 if ($this->Behaviors->attached('Cache')) {
394 if ($this->cacheEnabled()) {
395 return $this->cacheMethod(Configure::read('Cache.expire'), __FUNCTION__, $args);
398 // $parent = get_parent_class($this);
399 // return call_user_func_array(array($parent, __FUNCTION__), $args);
400 return parent::find($type, $queryData);
406 function deleteCacheHome()
408 $this->deleteCacheLastCharacters();
409 $this->deleteCacheAnonymousHome();
410 $this->deleteCacheMyData();
414 function deleteCacheLastCharacters()
416 @unlink(CACHE.'views'.DS.'element__character_picture_table');
420 function deleteCacheAnonymousHome()
422 @unlink(CACHE.'views'.DS.'element__home_right');
426 function deleteCacheMyData()
428 $Session = CorePlus::set_behavoir('Session');
430 if (!$Session->id()) {
434 @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_character_picture_table');
435 @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_home_left');
437 @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_character_index');
441 function deleteCacheCharacter($character_id = null, $deleteViewCache = true)
443 if ($deleteViewCache) {
444 @unlink(CACHE.'views'.DS.'chara_shee_characters.php');
447 if (!empty($character_id)) {
448 $this->deleteCacheCharacterView($character_id);
450 @unlink(CACHE.'views'.DS.'element_'.$character_id.'_character_picture_table');
453 function deleteCacheCharacterView($character_id = null, $deleteModelCache = false)
455 if (!empty($character_id)) {
456 @unlink(CACHE.'views'.DS.'element_'.$character_id.'_character_view');
458 $Session = CorePlus::set_behavoir('Session');
459 @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_'.$character_id.'_character_view');
462 if ($deleteModelCache) {
463 $this->Character->cacheDelete();
468 function deleteCache4CharacterPicture($character_id)
470 if (!empty($character_id)) {
471 @unlink(CACHE.'views'.DS.'element_'.$character_id.'_character_picture_index');
473 $Session = CorePlus::set_behavoir('Session');
474 @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_'.$character_id.'_character_picture_index');
479 function deleteCacheUser($user_id)
481 @unlink(CACHE.'views'.DS.'chara_shee_users.php');
482 if (!empty($user_id)) {
483 @unlink(CACHE.'views'.DS.'chara_shee_users_view_'.$user_id.'.php');
485 @unlink(CACHE.'views'.DS.'element_'.$user_id.'_character_picture_table');
486 @unlink(CACHE.'views'.DS.'element_'.$user_id.'_character_view');
491 function deleteCacheSystem($system_id, $deleteModelCache = false)
493 @unlink(CACHE.'views'.DS.'chara_shee_systems.php');
494 if (!empty($system_id)) {
495 @unlink(CACHE.'views'.DS.'chara_shee_systems_view_'.$system_id.'.php');
498 if ($deleteModelCache) {
499 if (!isset($this->System)) {
500 $this->System = CorePlus::set_model('System');
503 $this->System->cacheDelete();
508 function deleteCache4ProfileChilds()
510 if (!isset($this->Profile)) {
511 $this->Profile = CorePlus::set_model('Profile');
513 $this->Profile->cacheDelete();
515 if (!isset($this->System)) {
516 $this->System = CorePlus::set_model('System');
518 $this->System->cacheDelete();
522 function deleteCache4ProfileArchivesIndex($character_id)
524 if (empty($character_id)) {
528 $Session = CorePlus::set_behavoir('Session');
529 @unlink(CACHE.'views'.DS.'element_'.$character_id.'_character_profile_archive_index');
530 @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_'.$character_id.'_character_profile_archive_index');
535 function deleteCacheAll($deleteCore = true)
538 $this->deleteAllFiles(CACHE);
541 $this->deleteAllFiles(CACHE.'views');
545 $this->deleteAllFiles(CACHE.'models');
546 $this->deleteAllFiles(CACHE.'persistent');
550 function deleteAllFiles($dir)
552 if ($handle = opendir($dir)) {
553 while (false !== ($file = readdir($handle))) {
554 if (is_file($dir. DS. $file)
555 && $file != "." && $file != ".." && $file != "empty"
557 @unlink($dir. DS. $file);