XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
file.model.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
7 class fileModel extends file
8 {
13  function init()
14  {
15  }
16 
25  function getFileList()
26  {
27  $oModuleModel = getModel('module');
28 
29  $mid = Context::get('mid');
30  $editor_sequence = Context::get('editor_sequence');
31  $upload_target_srl = Context::get('upload_target_srl');
32  if(!$upload_target_srl) $upload_target_srl = $_SESSION['upload_info'][$editor_sequence]->upload_target_srl;
33 
34  if($upload_target_srl)
35  {
36  $oDocumentModel = getModel('document');
37  $oCommentModel = getModel('comment');
38  $logged_info = Context::get('logged_info');
39 
40  $oDocument = $oDocumentModel->getDocument($upload_target_srl);
41 
42  // comment 권한 확인
43  if(!$oDocument->isExists())
44  {
45  $oComment = $oCommentModel->getComment($upload_target_srl);
46  if($oComment->isExists() && $oComment->isSecret() && !$oComment->isGranted())
47  {
48  return new BaseObject(-1, 'msg_not_permitted');
49  }
50 
51  $oDocument = $oDocumentModel->getDocument($oComment->get('document_srl'));
52  }
53 
54  // document 권한 확인
55  if($oDocument->isExists() && $oDocument->isSecret() && !$oDocument->isGranted())
56  {
57  return new BaseObject(-1, 'msg_not_permitted');
58  }
59 
60  // 모듈 권한 확인
61  if($oDocument->isExists())
62  {
63  $grant = $oModuleModel->getGrant($oModuleModel->getModuleInfoByModuleSrl($oDocument->get('module_srl')), $logged_info);
64  if(!$grant->access)
65  {
66  return new BaseObject(-1, 'msg_not_permitted');
67  }
68  }
69 
70  $tmp_files = $this->getFiles($upload_target_srl);
71  if(!$tmp_files) $tmp_files = array();
72 
73  foreach($tmp_files as $file_info)
74  {
75  if(!$file_info->file_srl) continue;
76 
77  $obj = new stdClass;
78  $obj->file_srl = $file_info->file_srl;
79  $obj->source_filename = $file_info->source_filename;
80  $obj->file_size = $file_info->file_size;
81  $obj->disp_file_size = FileHandler::filesize($file_info->file_size);
82  if($file_info->direct_download=='N') $obj->download_url = $this->getDownloadUrl($file_info->file_srl, $file_info->sid, $file_info->module_srl);
83  else $obj->download_url = str_replace('./', '', $file_info->uploaded_filename);
84  $obj->direct_download = $file_info->direct_download;
85  $obj->cover_image = ($file_info->cover_image === 'Y') ? true : false;
86  $files[] = $obj;
87  $attached_size += $file_info->file_size;
88  }
89  }
90  else
91  {
92  $upload_target_srl = 0;
93  $attached_size = 0;
94  $files = array();
95  }
96  // Display upload status
97  $upload_status = $this->getUploadStatus($attached_size);
98  // Check remained file size until upload complete
99  //$config = $oModuleModel->getModuleInfoByMid($mid); //perhaps config varialbles not used
100 
101  $file_config = $this->getUploadConfig();
102  $left_size = $file_config->allowed_attach_size*1024*1024 - $attached_size;
103  // Settings of required information
104  $attached_size = FileHandler::filesize($attached_size);
105  $allowed_attach_size = FileHandler::filesize($file_config->allowed_attach_size*1024*1024);
106  $allowed_filesize = FileHandler::filesize($file_config->allowed_filesize*1024*1024);
107  $allowed_filetypes = $file_config->allowed_filetypes;
108  $this->add("files",$files);
109  $this->add("editor_sequence",$editor_sequence);
110  $this->add("upload_target_srl",$upload_target_srl);
111  $this->add("upload_status",$upload_status);
112  $this->add("left_size",$left_size);
113  $this->add('attached_size', $attached_size);
114  $this->add('allowed_attach_size', $allowed_attach_size);
115  $this->add('allowed_filesize', $allowed_filesize);
116  $this->add('allowed_filetypes', $allowed_filetypes);
117  }
118 
125  function getFilesCount($upload_target_srl)
126  {
127  $args = new stdClass();
128  $args->upload_target_srl = $upload_target_srl;
129  $output = executeQuery('file.getFilesCount', $args);
130  return (int)$output->data->count;
131  }
132 
140  function getDownloadUrl($file_srl, $sid, $module_srl="")
141  {
142  return sprintf('?module=%s&amp;act=%s&amp;file_srl=%s&amp;sid=%s&amp;module_srl=%s', 'file', 'procFileDownload', $file_srl, $sid, $module_srl);
143  }
144 
151  function getFileConfig($module_srl = null)
152  {
153  // Get configurations (using module model object)
154  $oModuleModel = getModel('module');
155 
156  $file_module_config = $oModuleModel->getModuleConfig('file');
157 
158  if($module_srl) $file_config = $oModuleModel->getModulePartConfig('file',$module_srl);
159  if(!$file_config) $file_config = $file_module_config;
160 
161  $config = new stdClass();
162 
163  if($file_config)
164  {
165  $config->allowed_filesize = $file_config->allowed_filesize;
166  $config->allowed_attach_size = $file_config->allowed_attach_size;
167  $config->allowed_filetypes = $file_config->allowed_filetypes;
168  $config->download_grant = $file_config->download_grant;
169  $config->allow_outlink = $file_config->allow_outlink;
170  $config->allow_outlink_site = $file_config->allow_outlink_site;
171  $config->allow_outlink_format = $file_config->allow_outlink_format;
172  }
173  // Property for all files comes first than each property
174  if(!$config->allowed_filesize) $config->allowed_filesize = $file_module_config->allowed_filesize;
175  if(!$config->allowed_attach_size) $config->allowed_attach_size = $file_module_config->allowed_attach_size;
176  if(!$config->allowed_filetypes) $config->allowed_filetypes = $file_module_config->allowed_filetypes;
177  if(!$config->allow_outlink) $config->allow_outlink = $file_module_config->allow_outlink;
178  if(!$config->allow_outlink_site) $config->allow_outlink_site = $file_module_config->allow_outlink_site;
179  if(!$config->allow_outlink_format) $config->allow_outlink_format = $file_module_config->allow_outlink_format;
180  if(!$config->download_grant) $config->download_grant = $file_module_config->download_grant;
181  // Default setting if not exists
182  if(!$config->allowed_filesize) $config->allowed_filesize = '2';
183  if(!$config->allowed_attach_size) $config->allowed_attach_size = '3';
184  if(!$config->allowed_filetypes) $config->allowed_filetypes = '*.*';
185  if(!$config->allow_outlink) $config->allow_outlink = 'Y';
186  if(!$config->download_grant) $config->download_grant = array();
187 
188  $size = ini_get('upload_max_filesize');
189  $unit = strtolower($size[strlen($size) - 1]);
190  $size = (float)$size;
191  if($unit == 'g') $size *= 1024;
192  if($unit == 'k') $size /= 1024;
193 
194  if($config->allowed_filesize > $size)
195  {
196  $config->allowed_filesize = $size;
197  }
198  if($config->allowed_attach_size > $size)
199  {
200  $config->allowed_attach_size = $size;
201  }
202 
203  return $config;
204  }
205 
213  function getFile($file_srl, $columnList = array())
214  {
215  $args = new stdClass();
216  $args->file_srl = $file_srl;
217  $output = executeQueryArray('file.getFile', $args, $columnList);
218  if(!$output->toBool()) return $output;
219 
220  // old version compatibility
221  if(count($output->data) == 1)
222  {
223  $file = $output->data[0];
224  $file->download_url = $this->getDownloadUrl($file->file_srl, $file->sid, $file->module_srl);
225 
226  return $file;
227  }
228  else
229  {
230  $fileList = array();
231 
232  if(is_array($output->data))
233  {
234  foreach($output->data as $key=>$value)
235  {
236  $file = $value;
237  $file->download_url = $this->getDownloadUrl($file->file_srl, $file->sid, $file->module_srl);
238  $fileList[] = $file;
239  }
240  }
241  return $fileList;
242  }
243  }
244 
253  function getFiles($upload_target_srl, $columnList = array(), $sortIndex = 'file_srl', $ckValid = false)
254  {
255  $args = new stdClass();
256  $args->upload_target_srl = $upload_target_srl;
257  $args->sort_index = $sortIndex;
258  if($ckValid) $args->isvalid = 'Y';
259  $output = executeQueryArray('file.getFiles', $args, $columnList);
260  if(!$output->data) return;
261 
262  $file_list = $output->data;
263 
264  if($file_list && !is_array($file_list)) $file_list = array($file_list);
265 
266  foreach ($file_list as &$file)
267  {
268  $file->source_filename = stripslashes($file->source_filename);
269  $file->source_filename = htmlspecialchars($file->source_filename);
270  $file->download_url = $this->getDownloadUrl($file->file_srl, $file->sid, $file->module_srl);
271  }
272 
273  return $file_list;
274  }
275 
281  function getUploadConfig()
282  {
283  $logged_info = Context::get('logged_info');
284 
285  $module_srl = Context::get('module_srl');
286  // Get the current module if module_srl doesn't exist
287  if(!$module_srl)
288  {
289  $current_module_info = Context::get('current_module_info');
290  $module_srl = $current_module_info->module_srl;
291  }
292  $file_config = $this->getFileConfig($module_srl);
293 
294  if($logged_info->is_admin == 'Y')
295  {
296  $iniPostMaxSize = FileHandler::returnbytes(ini_get('post_max_size'));
297  $iniUploadMaxSize = FileHandler::returnbytes(ini_get('upload_max_filesize'));
298  $size = min($iniPostMaxSize, $iniUploadMaxSize) / 1048576;
299  $file_config->allowed_attach_size = $size;
300  $file_config->allowed_filesize = $size;
301  $file_config->allowed_filetypes = '*.*';
302  }
303  return $file_config;
304  }
305 
312  function getUploadStatus($attached_size = 0)
313  {
314  $file_config = $this->getUploadConfig();
315  // Display upload status
316  $upload_status = sprintf(
317  '%s : %s/ %s<br /> %s : %s (%s : %s)',
318  Context::getLang('allowed_attach_size'),
319  FileHandler::filesize($attached_size),
320  FileHandler::filesize($file_config->allowed_attach_size*1024*1024),
321  Context::getLang('allowed_filesize'),
322  FileHandler::filesize($file_config->allowed_filesize*1024*1024),
323  Context::getLang('allowed_filetypes'),
324  $file_config->allowed_filetypes
325  );
326  return $upload_status;
327  }
328 
336  {
337  return $this->getFileConfig($module_srl);
338  }
339 
347  function getFileGrant($file_info, $member_info)
348  {
349  if(!$file_info) return null;
350 
351  if($_SESSION['__XE_UPLOADING_FILES_INFO__'][$file_info->file_srl])
352  {
353  $file_grant->is_deletable = true;
354  return $file_grant;
355  }
356 
357  $oModuleModel = getModel('module');
358  $grant = $oModuleModel->getGrant($oModuleModel->getModuleInfoByModuleSrl($file_info->module_srl), $member_info);
359 
360  $oDocumentModel = getModel('document');
361  $oDocument = $oDocumentModel->getDocument($file_info->upload_target_srl);
362  if($oDocument->isExists()) $document_grant = $oDocument->isGranted();
363 
364  $file_grant->is_deletable = ($document_grant || $member_info->is_admin == 'Y' || $member_info->member_srl == $file_info->member_srl || $grant->manager);
365 
366  return $file_grant;
367  }
368 }
369 /* End of file file.model.php */
370 /* Location: ./modules/file/file.model.php */
$oModuleModel
Definition: ko.install.php:236
$obj
Definition: ko.install.php:262
$output
Definition: ko.install.php:193
add($key, $val)
getUploadStatus($attached_size=0)
Definition: file.model.php:312
getFileGrant($file_info, $member_info)
Definition: file.model.php:347
$module_srl
integer value to represent a run-time instance of Module (XE Module)
$args
Definition: ko.install.php:185
getFileConfig($module_srl=null)
Definition: file.model.php:151
getDownloadUrl($file_srl, $sid, $module_srl="")
Definition: file.model.php:140
$mid
string to represent run-time instance of Module (XE Module)
getFiles($upload_target_srl, $columnList=array(), $sortIndex= 'file_srl', $ckValid=false)
Definition: file.model.php:253
$oDocumentModel
Definition: ko.install.php:259
getFileModuleConfig($module_srl)
Definition: file.model.php:335
getLang($code)
getFile($file_srl, $columnList=array())
Definition: file.model.php:213
getUploadConfig()
Definition: file.model.php:281
getModel($module_name)
Definition: func.inc.php:145
getFilesCount($upload_target_srl)
Definition: file.model.php:125
executeQueryArray($query_id, $args=NULL, $arg_columns=NULL)
Definition: func.inc.php:219
executeQuery($query_id, $args=NULL, $arg_columns=NULL)
Definition: func.inc.php:203