OSDN Git Service

更新時モデルキャッシュ削除追加
[trpgtools-onweb/cake-frame.git] / app / models / app_model.php
1 <?php
2 /**
3  * PHP version 5
4  *
5  * @category Model
6  * @package  Chara-Shee!
7  * @version  beta
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/
11  */
12
13 app::import('Sanitize');
14 class AppModel extends Model {
15         var $actsAs = array(
16                 'Cakeplus.ValidationErrorI18n',
17                 'Cakeplus.AddValidationRule',
18                 'SanitizePlus',
19                 'Containable',
20                 'Cache',
21         );
22
23         var $public_flag = array(
24                 'public',
25                 'private',
26         );
27
28         /* 
29          * DBフィールドごとの基本処理設定
30          * 例
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()を行なう
42          *              ),
43          *      ),
44          * $fields設定は個々のモデルで行なう
45         */
46         var $fields = array();
47
48         function __construct($id = false, $table = null, $ds = null) {
49                 parent::__construct($id, $table, $ds);
50
51                 // MySQLが非UTF-8設定のサーバ対策
52                 mysql_set_charset('utf8');
53         }
54
55         /* コールバックメソッド */
56         function beforeValidate($options = array())
57         {
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),
86
87                         // mediaプラグイン
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),
96
97                 );
98
99                 $error_messages['minLengthJP'] = $error_messages['minLength'];
100                 $error_messages['maxLengthJP'] = $error_messages['maxLength'];
101
102                 $this->setErrorMessageI18n($error_messages, false);
103
104                 $this->replaceValidationErrorMessagesI18n();
105
106                 return parent::beforeValidate($options);
107         }
108
109         function beforeSave($options = array())
110         {
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);
115                         }
116                         // Sanitize(不正コード、SQL Injection)
117                         $this->data = Sanitize::clean(
118                                 $this->data,
119                                 array(
120                                         'encode' => false,
121                                 )
122                         );
123                 }
124
125                 return parent::beforeSave($options);
126         }
127
128         /* Validation */
129         /* alphaNumeric() */
130         /** It Overwrites the original alphaNumeric() **/
131         function alphaNumeric($data)
132         {
133                 $check = is_array($data) ? array_shift($data) : $data;
134                 if (preg_match('/^[0-9a-z]+$/i',$check)) {
135                         return true;
136                 } else {
137                         return false;
138                 }
139         }
140
141         /* Wrapper to the origibal between() */
142         function betweenWrapper($check, $min, $max)
143         {
144                 return Validation::between($check, $min, $max);
145         }
146
147         /* check PublicFlag Setting */
148         function publicFlag($data)
149         {
150                 if (isset($data["public_flag"]) && in_array($data["public_flag"], $this->public_flag)) {
151                         return true;
152                 } else {
153                         return false;
154                 }
155         }
156
157         /* valid User Id */
158         function validUserId($data, $is_active = true)
159         {
160                 if (!isset($data['user_id']) || !$data['user_id']) {
161                         return false;
162                 }
163
164                 if (!isset($this->User)) {
165                         $this->User = CorePlus::set_model('User');
166                 }
167                 $user = $this->User->read(null, $data['user_id']);
168                 if (!$user['User']) {
169                         return false;
170                 }
171
172                 if ($is_active) {
173                         if (!isset($this->Group)) {
174                                 $this->Group = CorePlus::set_model('Group');
175                         }
176                         if (!in_array($user['User']['group_id'], $this->Group->is_active)) {
177                                 return false;
178                         }
179                 }
180
181                 return true;
182         }
183
184         /* valid System Id */
185         function validSystemId($data, $is_public = true)
186         {
187                 $this->System = CorePlus::set_model('System');
188
189                 $params =  array(
190                         'recursive' => -1,
191                         'fields' => 'System.id',
192                         'conditions' => array(
193                                 'System.id' => $data["system_id"],
194                         ),
195                 );
196                 
197                 if ($is_public === true || $is_public == 'public') {
198                         $params['conditions']['System.public_flag'] = 'public';
199                 } else {
200                         if (isset($params['conditions']['System.public_flag'])) {
201                                 unset($params['conditions']['System.public_flag']);
202                         }
203                 }
204                 $system_id = $this->System->find('list', $params);
205
206                 return (bool)$system_id;
207         }
208
209         /* keyName (number, small letter and underline) */
210         function keyName($data) 
211         {
212                 $check = is_array($data) ? array_shift($data) : $data;
213                 return (bool)preg_match('/^[_0-9a-z]+$/', $check);
214         }
215
216         /* アドレスチェック */
217         function isUniqueRegistMail($data)
218         {
219                 // regist_mailチェック
220                 $registId = $this->getRegistId4address($data);
221
222                 return (bool)$registId;
223         }
224         function getRegistId4address($data)
225         {
226                 if (isset($data['pcmail'])) {
227                         $mail = $data['pcmail'];
228                 } elseif (isset($data['mail'])) {
229                         $mail = $data['mail'];
230                 } else {
231                         $mail = null;
232                 }
233
234                 if (!$mail) {
235                         return null;
236                 }
237
238                 // アドレス暗号化
239                 $encrypted_address = $this->getEncryptedAddress($mail);
240
241                 $this->RegistMail = CorePlus::set_model('RegistMail');
242                 $conditions = array(
243                         'RegistMail.mail' => $encrypted_address,
244                 );
245                 $fields = array(
246                         'RegistMail.id',
247                 );
248                 $user = $this->RegistMail->find('first', array(
249                         'conditions' => $conditions,
250                         'fields' => $fields,
251                         'recursive' => -1,
252                 ));
253
254                 if (isset($user['RegistMail']['id'])) {
255                         return $user['RegistMail']['id'];
256                 } else {
257                         return null;
258                 }
259         }
260
261         function getRegistData4code($code)
262         {
263                 if (!$code) {
264                         return array();
265                 }
266
267                 $this->RegistMail = CorePlus::set_model('RegistMail');
268                 $conditions = array(
269                         'RegistMail.key_code' => $code,
270                 );
271                 return  $this->RegistMail->find('first', array(
272                         'conditions' => $conditions,
273                         'recursive' => -1,
274                 ));
275         }
276         function deleteRegistData4mail($mail)
277         {
278                 if (!$mail) {
279                         return null;
280                 }
281
282                 $this->RegistMail = CorePlus::set_model('RegistMail');
283                 $conditions = array(
284                         'RegistMail.mail' => $mail,
285                 );
286                 return $this->RegistMail->deleteAll($conditions);
287         }
288
289         function isUniquePcMail($data)
290         {
291                 // usersチェック
292                 $userId = $this->getUserId4address($data);
293
294                 return !(bool)$userId;
295         }
296         function getUserId4address($data)
297         {
298                 if (isset($data['pcmail'])) {
299                         $mail = $data['pcmail'];
300                 } elseif (isset($data['mail'])) {
301                         $mail = $data['mail'];
302                 } else {
303                         $mail = null;
304                 }
305
306                 if (!$mail) {
307                         return null;
308                 }
309
310                 // アドレス暗号化
311                 $encrypted_address = $this->getEncryptedAddress($mail);
312
313                 $this->User = CorePlus::set_model('User');
314                 $conditions = array(
315                         'User.pcmail' => $encrypted_address,
316                 );
317                 $fields = array(
318                         'User.id',
319                 );
320                 $user = $this->User->find('first', array(
321                         'conditions' => $conditions,
322                         'fields' => $fields,
323                         'recursive' => -1,
324                 ));
325
326                 if (isset($user['User']['id'])) {
327                         return $user['User']['id'];
328                 } else {
329                         return null;
330                 }
331         }
332
333         function getEncryptedAddress($mail)
334         {
335                 if (!isset($this->Crypt)) {
336                         App::import('Component', 'Crypt');
337                         $this->Crypt = new CryptComponent;
338                 }
339                 return $this->Crypt->crypt($mail);
340         }
341
342         function isPcMail($data)
343         {
344                 if (isset($data['pcmail'])) {
345                         $mail = $data['pcmail'];
346                 } elseif (isset($data['mail'])) {
347                         $mail = $data['mail'];
348                 } else {
349                         $mail = null;
350                 }
351
352                 if (!$mail) {
353                         return true;
354                 }
355
356                 if (!$this->isAddress($mail)) {
357                         return false;
358                 }
359
360                 return !$this->checkMobileDomain($mail);
361         }
362         function checkMobileDomain($data)
363         {
364                 if (empty($data)) {
365                         return true;
366                 }
367                 if (!Configure::read('mobile.domain')) {
368                         return false;
369                 }
370
371                 $pieces = explode('@', $data);
372                 $domain = array_pop($pieces);
373
374                 return in_array($domain, Configure::read('mobile.domain'));
375         }
376         function isAddress($data)
377         {
378                 if (empty($data)) {
379                         return true;
380                 }
381
382                 if (preg_match('/^[^:;@,\s\x80-\xFF]+@\w[\w\-.]*\.[a-zA-Z]+$/', $data)) {
383                         return true;
384                 } else {
385                         return false;
386                 }
387         }
388
389         function find($type = null, $queryData = array())
390         {
391                 $args = array($type, $queryData);
392
393                 if ($this->Behaviors->attached('Cache')) {
394                         if ($this->cacheEnabled()) {
395                                 return $this->cacheMethod(Configure::read('Cache.expire'), __FUNCTION__, $args);
396                         }
397                 }
398 //              $parent = get_parent_class($this);
399 //              return call_user_func_array(array($parent, __FUNCTION__), $args);
400                 return parent::find($type, $queryData);
401         }
402
403
404         /* キャッシュ削除 */
405         // ホーム関連
406         function deleteCacheHome()
407         {
408                 $this->deleteCacheLastCharacters();
409                 $this->deleteCacheAnonymousHome();
410                 $this->deleteCacheMyData();
411         }
412
413         // 最近更新されたキャラクター
414         function deleteCacheLastCharacters()
415         {
416                 @unlink(CACHE.'views'.DS.'element__character_picture_table');
417         }
418
419         // 未ログインホーム
420         function deleteCacheAnonymousHome()
421         {
422                 @unlink(CACHE.'views'.DS.'element__home_right');
423         }
424
425         // 自セッション関連
426         function deleteCacheMyData()
427         {
428                 $Session = CorePlus::set_behavoir('Session');
429
430                 if (!$Session->id()) {
431                         return false;
432                 }
433
434                 @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_character_picture_table');
435                 @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_home_left');
436
437                 @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_character_index');
438         }
439
440         // キャラクター
441         function deleteCacheCharacter($character_id = null, $deleteViewCache = true)
442         {
443                 if ($deleteViewCache) {
444                         @unlink(CACHE.'views'.DS.'chara_shee_characters.php');
445                 }
446
447                 if (!empty($character_id)) {
448                         $this->deleteCacheCharacterView($character_id);
449
450                         @unlink(CACHE.'views'.DS.'element_'.$character_id.'_character_picture_table');
451                 }
452         }
453         function deleteCacheCharacterView($character_id = null, $deleteModelCache = false)
454         {
455                 if (!empty($character_id)) {
456                         @unlink(CACHE.'views'.DS.'element_'.$character_id.'_character_view');
457
458                         $Session = CorePlus::set_behavoir('Session');
459                         @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_'.$character_id.'_character_view');
460                 }
461
462                 if ($deleteModelCache) {
463                         $this->Character->cacheDelete();
464                 }
465         }
466
467         // キャラ画像
468         function deleteCache4CharacterPicture($character_id)
469         {
470                 if (!empty($character_id)) {
471                         @unlink(CACHE.'views'.DS.'element_'.$character_id.'_character_picture_index');
472
473                         $Session = CorePlus::set_behavoir('Session');
474                         @unlink(CACHE.'views'.DS.'element_'.$Session->id().'_'.$character_id.'_character_picture_index');
475                 }
476         }
477
478         // ユーザ
479         function deleteCacheUser($user_id)
480         {
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');
484
485                         @unlink(CACHE.'views'.DS.'element_'.$user_id.'_character_picture_table');
486                         @unlink(CACHE.'views'.DS.'element_'.$user_id.'_character_view');
487                 }
488         }
489
490         // システム
491         function deleteCacheSystem($system_id, $deleteModelCache = false)
492         {
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');
496                 }
497
498                 if ($deleteModelCache) {
499                         if (!isset($this->System)) {
500                                 $this->System = CorePlus::set_model('System');
501                         }
502
503                         $this->System->cacheDelete();
504                 }
505         }
506
507         // プロフィール関連
508         function deleteCache4ProfileChilds()
509         {
510                 if (!isset($this->Profile)) {
511                         $this->Profile = CorePlus::set_model('Profile');
512                 }
513                 $this->Profile->cacheDelete();
514
515                 if (!isset($this->System)) {
516                         $this->System = CorePlus::set_model('System');
517                 }
518                 $this->System->cacheDelete();
519         }
520
521         // Archive
522         function deleteCache4ProfileArchivesIndex($character_id)
523         {
524                 if (empty($character_id)) {
525                         return null;
526                 }
527
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');
531         }
532
533
534         // 全てのキャッシュ
535         function deleteCacheAll($deleteCore = true)
536         {
537                 // モデルキャッシュ
538                 $this->deleteAllFiles(CACHE);
539
540                 // ビューキャッシュ
541                 $this->deleteAllFiles(CACHE.'views');
542
543                 // cake core
544                 if ($deleteCore) {
545                         $this->deleteAllFiles(CACHE.'models');
546                         $this->deleteAllFiles(CACHE.'persistent');
547                 }
548         }
549
550         function deleteAllFiles($dir)
551         {
552                 if ($handle = opendir($dir)) {
553                         while (false !== ($file = readdir($handle))) {
554                                 if (is_file($dir. DS. $file) 
555                                          && $file != "." && $file != ".." && $file != "empty"
556                                 ) {
557                                         @unlink($dir. DS. $file);
558                                 }
559                         }
560                 }
561                 closedir($handle);
562         }
563
564 }
565