-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUploadFileValidateBehavior.php
More file actions
132 lines (120 loc) · 3.79 KB
/
UploadFileValidateBehavior.php
File metadata and controls
132 lines (120 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* UploadFileValidateBehavior
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('SiteSettingUtil', 'SiteManager.Utility');
/**
* Class UploadFileValidateBehavior
*/
class UploadFileValidateBehavior extends ModelBehavior {
/**
* NetCommons3のセキュリティ設定で許可されている拡張子かチェックする
*
* @param Model $model Model
* @param string $extension 拡張子
* @return bool
*/
public function isAllowUploadFileExtension(Model $model, $extension) {
$allowExtension = $this->getAllowExtension($model);
return in_array(strtolower($extension), $allowExtension, true);
}
/**
* NetCommons3のセキュリティ設定で許可されている拡張子のリストを返す
*
* @param Model $model Model
* @return array
*/
public function getAllowExtension(Model $model) {
$uploadAllowExtension = explode(',', SiteSettingUtil::read('Upload.allow_extension'));
$uploadAllowExtension = array_map('trim', $uploadAllowExtension);
return $uploadAllowExtension;
}
/**
* ルームのファイルサイズ合計を返す
* 履歴データは含まない。
*
* @param Model $model Model
* @param int $roomId ルームID
* @return int 合計ファイルサイズ(Byte)
*/
public function getTotalSizeByRoomId(Model $model, $roomId) {
// 単純sumじゃだめ。重複は排除しないといけないのでSQL直書き
$query = <<< EOF
SELECT sum(size) AS total_size FROM
(
SELECT DISTINCT `UploadFile`.`id`, `UploadFile`.`size`
FROM `%s` AS `UploadFilesContent`
LEFT JOIN `%s` AS `UploadFile`
ON (`UploadFilesContent`.`upload_file_id` = `UploadFile`.`id`)
WHERE (
(`UploadFilesContent`.`content_is_active` IN (1, NULL))
OR
(`UploadFilesContent`.`content_is_latest` IN (1, NULL))
) AND `UploadFile`.`room_id` = ?
GROUP BY `UploadFile`.`id`
) AS UploadFileSize;
EOF;
$query = sprintf($query,
$model->tablePrefix . 'upload_files_contents',
$model->tablePrefix . 'upload_files');
$result = $model->query($query, [$roomId]);
$total = $result[0][0]['total_size'];
$total = (is_null($total)) ? 0 : $total;
return $total;
}
/**
* NetCommons3のシステム管理→一般設定で許可されているルーム容量内かをチェックするバリデータ
*
* @param Model $model Model
* @param array $check バリデートする値
* @return bool|string 容量内: true, 容量オーバー: string エラーメッセージ
*/
public function validateRoomFileSizeLimit(Model $model, $check) {
$field = $this->_getField($check);
$roomId = Current::read('Room.id');
$maxRoomDiskSize = Current::read('Space.room_disk_size');
if ($maxRoomDiskSize === null) {
return true;
}
$size = $check[$field]['size'];
$roomTotalSize = $this->getTotalSizeByRoomId($model, $roomId);
if (($roomTotalSize + $size) < $maxRoomDiskSize) {
return true;
} else {
$roomsLanguage = ClassRegistry::init('Room.RoomsLanguage');
$data = $roomsLanguage->find(
'first',
[
'conditions' => [
'room_id' => $roomId,
'language_id' => Current::read('Language.id'),
]
]
);
$roomName = $data['RoomsLanguage']['name'];
// ファイルサイズをMBとかkb表示に
$message = __d(
'files',
'Total file size uploaded to the %s, exceeded the limit. The limit is %s(%s left).',
$roomName,
CakeNumber::toReadableSize($maxRoomDiskSize),
CakeNumber::toReadableSize($maxRoomDiskSize - $roomTotalSize)
);
return $message;
}
}
/**
* Returns the field to check
*
* @param array $check array of validation data
* @return string
*/
protected function _getField($check) {
$fieldKeys = array_keys($check);
return array_pop($fieldKeys);
}
}