PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/file/file.model.php

http://xe-core.googlecode.com/
PHP | 313 lines | 195 code | 32 blank | 86 comment | 41 complexity | 9430bc8b9d8d2272091aae47239ba978 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Model class of the file module
  4. * @author NHN (developers@xpressengine.com)
  5. */
  6. class fileModel extends file
  7. {
  8. /**
  9. * Initialization
  10. * @return void
  11. */
  12. function init()
  13. {
  14. }
  15. /**
  16. * Return a file list attached in the document
  17. *
  18. * It is used when a file list of the upload_target_srl is requested for creating/updating a document.
  19. * Attempt to replace with sever-side session if upload_target_srl is not yet determined
  20. *
  21. * @return void
  22. */
  23. function getFileList()
  24. {
  25. $oModuleModel = &getModel('module');
  26. $mid = Context::get('mid');
  27. $editor_sequence = Context::get('editor_sequence');
  28. $upload_target_srl = Context::get('upload_target_srl');
  29. if(!$upload_target_srl) $upload_target_srl = $_SESSION['upload_info'][$editor_sequence]->upload_target_srl;
  30. if($upload_target_srl)
  31. {
  32. $tmp_files = $this->getFiles($upload_target_srl);
  33. $file_count = count($tmp_files);
  34. for($i=0;$i<$file_count;$i++)
  35. {
  36. $file_info = $tmp_files[$i];
  37. if(!$file_info->file_srl) continue;
  38. $obj = null;
  39. $obj->file_srl = $file_info->file_srl;
  40. $obj->source_filename = $file_info->source_filename;
  41. $obj->file_size = $file_info->file_size;
  42. $obj->disp_file_size = FileHandler::filesize($file_info->file_size);
  43. if($file_info->direct_download=='N') $obj->download_url = $this->getDownloadUrl($file_info->file_srl, $file_info->sid);
  44. else $obj->download_url = str_replace('./', '', $file_info->uploaded_filename);
  45. $obj->direct_download = $file_info->direct_download;
  46. $files[] = $obj;
  47. $attached_size += $file_info->file_size;
  48. }
  49. }
  50. else
  51. {
  52. $upload_target_srl = 0;
  53. $attached_size = 0;
  54. $files = array();
  55. }
  56. // Display upload status
  57. $upload_status = $this->getUploadStatus($attached_size);
  58. // Check remained file size until upload complete
  59. //$config = $oModuleModel->getModuleInfoByMid($mid); //perhaps config varialbles not used
  60. $file_config = $this->getUploadConfig();
  61. $left_size = $file_config->allowed_attach_size*1024*1024 - $attached_size;
  62. // Settings of required information
  63. $this->add("files",$files);
  64. $this->add("editor_sequence",$editor_sequence);
  65. $this->add("upload_target_srl",$upload_target_srl);
  66. $this->add("upload_status",$upload_status);
  67. $this->add("left_size",$left_size);
  68. }
  69. /**
  70. * Return number of attachments which belongs to a specific document
  71. *
  72. * @param int $upload_target_srl The sequence to get a number of files
  73. * @return int Returns a number of files
  74. */
  75. function getFilesCount($upload_target_srl)
  76. {
  77. $args = new stdClass();
  78. $args->upload_target_srl = $upload_target_srl;
  79. $output = executeQuery('file.getFilesCount', $args);
  80. return (int)$output->data->count;
  81. }
  82. /**
  83. * Get a download path
  84. *
  85. * @param int $file_srl The sequence of file to get url
  86. * @param string $sid
  87. * @return string Returns a url
  88. */
  89. function getDownloadUrl($file_srl, $sid)
  90. {
  91. return sprintf('?module=%s&amp;act=%s&amp;file_srl=%s&amp;sid=%s', 'file', 'procFileDownload', $file_srl, $sid);
  92. }
  93. /**
  94. * Get file configurations
  95. *
  96. * @param int $module_srl If set this, returns specific module's configuration. Otherwise returns global configuration.
  97. * @return object Returns configuration.
  98. */
  99. function getFileConfig($module_srl = null)
  100. {
  101. // Get configurations (using module model object)
  102. $oModuleModel = &getModel('module');
  103. $file_module_config = $oModuleModel->getModuleConfig('file');
  104. if($module_srl) $file_config = $oModuleModel->getModulePartConfig('file',$module_srl);
  105. if(!$file_config) $file_config = $file_module_config;
  106. $config = new stdClass();
  107. if($file_config)
  108. {
  109. $config->allowed_filesize = $file_config->allowed_filesize;
  110. $config->allowed_attach_size = $file_config->allowed_attach_size;
  111. $config->allowed_filetypes = $file_config->allowed_filetypes;
  112. $config->download_grant = $file_config->download_grant;
  113. $config->allow_outlink = $file_config->allow_outlink;
  114. $config->allow_outlink_site = $file_config->allow_outlink_site;
  115. $config->allow_outlink_format = $file_config->allow_outlink_format;
  116. }
  117. // Property for all files comes first than each property
  118. if(!$config->allowed_filesize) $config->allowed_filesize = $file_module_config->allowed_filesize;
  119. if(!$config->allowed_attach_size) $config->allowed_attach_size = $file_module_config->allowed_attach_size;
  120. if(!$config->allowed_filetypes) $config->allowed_filetypes = $file_module_config->allowed_filetypes;
  121. if(!$config->allow_outlink) $config->allow_outlink = $file_module_config->allow_outlink;
  122. if(!$config->allow_outlink_site) $config->allow_outlink_site = $file_module_config->allow_outlink_site;
  123. if(!$config->allow_outlink_format) $config->allow_outlink_format = $file_module_config->allow_outlink_format;
  124. if(!$config->download_grant) $config->download_grant = $file_module_config->download_grant;
  125. // Default setting if not exists
  126. if(!$config->allowed_filesize) $config->allowed_filesize = '2';
  127. if(!$config->allowed_attach_size) $config->allowed_attach_size = '3';
  128. if(!$config->allowed_filetypes) $config->allowed_filetypes = '*.*';
  129. if(!$config->allow_outlink) $config->allow_outlink = 'Y';
  130. if(!$config->download_grant) $config->download_grant = array();
  131. return $config;
  132. }
  133. /**
  134. * Get file information
  135. *
  136. * @param int $file_srl The sequence of file to get information
  137. * @param array $columnList The list of columns to get from DB
  138. * @return Object|object|array If error returns an instance of Object. If result set is one returns a object that contins file information. If result set is more than one returns array of object.
  139. */
  140. function getFile($file_srl, $columnList = array())
  141. {
  142. $args = new stdClass();
  143. $args->file_srl = $file_srl;
  144. $output = executeQueryArray('file.getFile', $args, $columnList);
  145. if(!$output->toBool()) return $output;
  146. // old version compatibility
  147. if(count($output->data) == 1)
  148. {
  149. $file = $output->data[0];
  150. $file->download_url = $this->getDownloadUrl($file->file_srl, $file->sid);
  151. return $file;
  152. }
  153. else
  154. {
  155. $fileList = array();
  156. if(is_array($output->data))
  157. {
  158. foreach($output->data as $key=>$value)
  159. {
  160. $file = $value;
  161. $file->download_url = $this->getDownloadUrl($file->file_srl, $file->sid);
  162. array_push($fileList, $file);
  163. }
  164. }
  165. return $fileList;
  166. }
  167. }
  168. /**
  169. * Return all files which belong to a specific document
  170. *
  171. * @param int $upload_target_srl The sequence of target to get file list
  172. * @param array $columnList The list of columns to get from DB
  173. * @param string $sortIndex The column that used as sort index
  174. * @return array Returns array of object that contains file information. If no result returns null.
  175. */
  176. function getFiles($upload_target_srl, $columnList = array(), $sortIndex = 'file_srl', $ckValid = false)
  177. {
  178. $args = new stdClass();
  179. $args->upload_target_srl = $upload_target_srl;
  180. $args->sort_index = $sortIndex;
  181. if($ckValid) $args->isvalid = 'Y';
  182. $output = executeQuery('file.getFiles', $args, $columnList);
  183. if(!$output->data) return;
  184. $file_list = $output->data;
  185. if($file_list && !is_array($file_list)) $file_list = array($file_list);
  186. $file_count = count($file_list);
  187. for($i=0;$i<$file_count;$i++)
  188. {
  189. $file = $file_list[$i];
  190. $file->source_filename = stripslashes($file->source_filename);
  191. $file->download_url = $this->getDownloadUrl($file->file_srl, $file->sid);
  192. $file_list[$i] = $file;
  193. }
  194. return $file_list;
  195. }
  196. /**
  197. * Return configurations of the attachement (it automatically checks if an administrator is)
  198. *
  199. * @return object Returns a file configuration of current module. If user is admin, returns PHP's max file size and allow all file types.
  200. */
  201. function getUploadConfig()
  202. {
  203. $logged_info = Context::get('logged_info');
  204. $file_config = new stdClass();
  205. if($logged_info->is_admin == 'Y')
  206. {
  207. $file_config->allowed_filesize = preg_replace("/[a-z]/is","",ini_get('upload_max_filesize'));
  208. $file_config->allowed_attach_size = preg_replace("/[a-z]/is","",ini_get('upload_max_filesize'));
  209. $file_config->allowed_filetypes = '*.*';
  210. }
  211. else
  212. {
  213. $module_srl = Context::get('module_srl');
  214. // Get the current module if module_srl doesn't exist
  215. if(!$module_srl)
  216. {
  217. $current_module_info = Context::get('current_module_info');
  218. $module_srl = $current_module_info->module_srl;
  219. }
  220. $file_config = $this->getFileConfig($module_srl);
  221. }
  222. return $file_config;
  223. }
  224. /**
  225. * Return messages for file upload and it depends whether an admin is or not
  226. *
  227. * @param int $attached_size
  228. * @return string
  229. */
  230. function getUploadStatus($attached_size = 0)
  231. {
  232. $file_config = $this->getUploadConfig();
  233. // Display upload status
  234. $upload_status = sprintf(
  235. '%s : %s/ %s<br /> %s : %s (%s : %s)',
  236. Context::getLang('allowed_attach_size'),
  237. FileHandler::filesize($attached_size),
  238. FileHandler::filesize($file_config->allowed_attach_size*1024*1024),
  239. Context::getLang('allowed_filesize'),
  240. FileHandler::filesize($file_config->allowed_filesize*1024*1024),
  241. Context::getLang('allowed_filetypes'),
  242. $file_config->allowed_filetypes
  243. );
  244. return $upload_status;
  245. }
  246. /**
  247. * Return file configuration of the module
  248. *
  249. * @param int $module_srl The sequence of module to get configuration
  250. * @return object
  251. */
  252. function getFileModuleConfig($module_srl)
  253. {
  254. return $this->getFileConfig($module_srl);
  255. }
  256. /**
  257. * Returns a grant of file
  258. *
  259. * @param object $file_info The file information to get grant
  260. * @param object $member_info The member information to get grant
  261. * @return object Returns a grant of file
  262. */
  263. function getFileGrant($file_info, $member_info)
  264. {
  265. if(!$file_info) return null;
  266. if($_SESSION['__XE_UPLOADING_FILES_INFO__'][$file_info->file_srl])
  267. {
  268. $file_grant->is_deletable = true;
  269. return $file_grant;
  270. }
  271. $oModuleModel = &getModel('module');
  272. $grant = $oModuleModel->getGrant($oModuleModel->getModuleInfoByModuleSrl($file_info->module_srl), $member_info);
  273. $oDocumentModel = &getModel('document');
  274. $oDocument = $oDocumentModel->getDocument($file_info->upload_target_srl);
  275. if($oDocument->isExists()) $document_grant = $oDocument->isGranted();
  276. $file_grant->is_deletable = ($document_grant || $member_info->is_admin == 'Y' || $member_info->member_srl == $file_info->member_srl || $grant->manager);
  277. return $file_grant;
  278. }
  279. }
  280. /* End of file file.model.php */
  281. /* Location: ./modules/file/file.model.php */