PageRenderTime 69ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/moodle/lib/filelib.php

https://bitbucket.org/sebastianberm/moodle20
PHP | 3142 lines | 2008 code | 279 blank | 855 comment | 463 complexity | 4bf9a1069fc8e0bf5d270b245fdba038 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Functions for file handling.
  18. *
  19. * @package core
  20. * @subpackage file
  21. * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /** @var string unique string constant. */
  26. define('BYTESERVING_BOUNDARY', 's1k2o3d4a5k6s7');
  27. require_once("$CFG->libdir/filestorage/file_exceptions.php");
  28. require_once("$CFG->libdir/filestorage/file_storage.php");
  29. require_once("$CFG->libdir/filestorage/zip_packer.php");
  30. require_once("$CFG->libdir/filebrowser/file_browser.php");
  31. /**
  32. * Encodes file serving url
  33. *
  34. * @deprecated use moodle_url factory methods instead
  35. *
  36. * @global object
  37. * @param string $urlbase
  38. * @param string $path /filearea/itemid/dir/dir/file.exe
  39. * @param bool $forcedownload
  40. * @param bool $https https url required
  41. * @return string encoded file url
  42. */
  43. function file_encode_url($urlbase, $path, $forcedownload=false, $https=false) {
  44. global $CFG;
  45. //TODO: deprecate this
  46. if ($CFG->slasharguments) {
  47. $parts = explode('/', $path);
  48. $parts = array_map('rawurlencode', $parts);
  49. $path = implode('/', $parts);
  50. $return = $urlbase.$path;
  51. if ($forcedownload) {
  52. $return .= '?forcedownload=1';
  53. }
  54. } else {
  55. $path = rawurlencode($path);
  56. $return = $urlbase.'?file='.$path;
  57. if ($forcedownload) {
  58. $return .= '&amp;forcedownload=1';
  59. }
  60. }
  61. if ($https) {
  62. $return = str_replace('http://', 'https://', $return);
  63. }
  64. return $return;
  65. }
  66. /**
  67. * Prepares 'editor' formslib element from data in database
  68. *
  69. * The passed $data record must contain field foobar, foobarformat and optionally foobartrust. This
  70. * function then copies the embedded files into draft area (assigning itemids automatically),
  71. * creates the form element foobar_editor and rewrites the URLs so the embedded images can be
  72. * displayed.
  73. * In your mform definition, you must have an 'editor' element called foobar_editor. Then you call
  74. * your mform's set_data() supplying the object returned by this function.
  75. *
  76. * @param object $data database field that holds the html text with embedded media
  77. * @param string $field the name of the database field that holds the html text with embedded media
  78. * @param array $options editor options (like maxifiles, maxbytes etc.)
  79. * @param object $context context of the editor
  80. * @param string $component
  81. * @param string $filearea file area name
  82. * @param int $itemid item id, required if item exists
  83. * @return object modified data object
  84. */
  85. function file_prepare_standard_editor($data, $field, array $options, $context=null, $component=null, $filearea=null, $itemid=null) {
  86. $options = (array)$options;
  87. if (!isset($options['trusttext'])) {
  88. $options['trusttext'] = false;
  89. }
  90. if (!isset($options['forcehttps'])) {
  91. $options['forcehttps'] = false;
  92. }
  93. if (!isset($options['subdirs'])) {
  94. $options['subdirs'] = false;
  95. }
  96. if (!isset($options['maxfiles'])) {
  97. $options['maxfiles'] = 0; // no files by default
  98. }
  99. if (!isset($options['noclean'])) {
  100. $options['noclean'] = false;
  101. }
  102. //sanity check for passed context. This function doesn't expect $option['context'] to be set
  103. //But this function is called before creating editor hence, this is one of the best places to check
  104. //if context is used properly. This check notify developer that they missed passing context to editor.
  105. if (isset($context) && !isset($options['context'])) {
  106. //if $context is not null then make sure $option['context'] is also set.
  107. debugging('Context for editor is not set in editoroptions. Hence editor will not respect editor filters', DEBUG_DEVELOPER);
  108. } else if (isset($options['context']) && isset($context)) {
  109. //If both are passed then they should be equal.
  110. if ($options['context']->id != $context->id) {
  111. $exceptionmsg = 'Editor context ['.$options['context']->id.'] is not equal to passed context ['.$context->id.']';
  112. throw new coding_exception($exceptionmsg);
  113. }
  114. }
  115. if (is_null($itemid) or is_null($context)) {
  116. $contextid = null;
  117. $itemid = null;
  118. if (!isset($data->{$field})) {
  119. $data->{$field} = '';
  120. }
  121. if (!isset($data->{$field.'format'})) {
  122. $data->{$field.'format'} = editors_get_preferred_format();
  123. }
  124. if (!$options['noclean']) {
  125. $data->{$field} = clean_text($data->{$field}, $data->{$field.'format'});
  126. }
  127. } else {
  128. if ($options['trusttext']) {
  129. // noclean ignored if trusttext enabled
  130. if (!isset($data->{$field.'trust'})) {
  131. $data->{$field.'trust'} = 0;
  132. }
  133. $data = trusttext_pre_edit($data, $field, $context);
  134. } else {
  135. if (!$options['noclean']) {
  136. $data->{$field} = clean_text($data->{$field}, $data->{$field.'format'});
  137. }
  138. }
  139. $contextid = $context->id;
  140. }
  141. if ($options['maxfiles'] != 0) {
  142. $draftid_editor = file_get_submitted_draft_itemid($field);
  143. $currenttext = file_prepare_draft_area($draftid_editor, $contextid, $component, $filearea, $itemid, $options, $data->{$field});
  144. $data->{$field.'_editor'} = array('text'=>$currenttext, 'format'=>$data->{$field.'format'}, 'itemid'=>$draftid_editor);
  145. } else {
  146. $data->{$field.'_editor'} = array('text'=>$data->{$field}, 'format'=>$data->{$field.'format'}, 'itemid'=>0);
  147. }
  148. return $data;
  149. }
  150. /**
  151. * Prepares the content of the 'editor' form element with embedded media files to be saved in database
  152. *
  153. * This function moves files from draft area to the destination area and
  154. * encodes URLs to the draft files so they can be safely saved into DB. The
  155. * form has to contain the 'editor' element named foobar_editor, where 'foobar'
  156. * is the name of the database field to hold the wysiwyg editor content. The
  157. * editor data comes as an array with text, format and itemid properties. This
  158. * function automatically adds $data properties foobar, foobarformat and
  159. * foobartrust, where foobar has URL to embedded files encoded.
  160. *
  161. * @param object $data raw data submitted by the form
  162. * @param string $field name of the database field containing the html with embedded media files
  163. * @param array $options editor options (trusttext, subdirs, maxfiles, maxbytes etc.)
  164. * @param object $context context, required for existing data
  165. * @param string component
  166. * @param string $filearea file area name
  167. * @param int $itemid item id, required if item exists
  168. * @return object modified data object
  169. */
  170. function file_postupdate_standard_editor($data, $field, array $options, $context, $component=null, $filearea=null, $itemid=null) {
  171. $options = (array)$options;
  172. if (!isset($options['trusttext'])) {
  173. $options['trusttext'] = false;
  174. }
  175. if (!isset($options['forcehttps'])) {
  176. $options['forcehttps'] = false;
  177. }
  178. if (!isset($options['subdirs'])) {
  179. $options['subdirs'] = false;
  180. }
  181. if (!isset($options['maxfiles'])) {
  182. $options['maxfiles'] = 0; // no files by default
  183. }
  184. if (!isset($options['maxbytes'])) {
  185. $options['maxbytes'] = 0; // unlimited
  186. }
  187. if ($options['trusttext']) {
  188. $data->{$field.'trust'} = trusttext_trusted($context);
  189. } else {
  190. $data->{$field.'trust'} = 0;
  191. }
  192. $editor = $data->{$field.'_editor'};
  193. if ($options['maxfiles'] == 0 or is_null($filearea) or is_null($itemid) or empty($editor['itemid'])) {
  194. $data->{$field} = $editor['text'];
  195. } else {
  196. $data->{$field} = file_save_draft_area_files($editor['itemid'], $context->id, $component, $filearea, $itemid, $options, $editor['text'], $options['forcehttps']);
  197. }
  198. $data->{$field.'format'} = $editor['format'];
  199. return $data;
  200. }
  201. /**
  202. * Saves text and files modified by Editor formslib element
  203. *
  204. * @param object $data $database entry field
  205. * @param string $field name of data field
  206. * @param array $options various options
  207. * @param object $context context - must already exist
  208. * @param string $component
  209. * @param string $filearea file area name
  210. * @param int $itemid must already exist, usually means data is in db
  211. * @return object modified data obejct
  212. */
  213. function file_prepare_standard_filemanager($data, $field, array $options, $context=null, $component=null, $filearea=null, $itemid=null) {
  214. $options = (array)$options;
  215. if (!isset($options['subdirs'])) {
  216. $options['subdirs'] = false;
  217. }
  218. if (is_null($itemid) or is_null($context)) {
  219. $itemid = null;
  220. $contextid = null;
  221. } else {
  222. $contextid = $context->id;
  223. }
  224. $draftid_editor = file_get_submitted_draft_itemid($field.'_filemanager');
  225. file_prepare_draft_area($draftid_editor, $contextid, $component, $filearea, $itemid, $options);
  226. $data->{$field.'_filemanager'} = $draftid_editor;
  227. return $data;
  228. }
  229. /**
  230. * Saves files modified by File manager formslib element
  231. *
  232. * @param object $data $database entry field
  233. * @param string $field name of data field
  234. * @param array $options various options
  235. * @param object $context context - must already exist
  236. * @param string $component
  237. * @param string $filearea file area name
  238. * @param int $itemid must already exist, usually means data is in db
  239. * @return object modified data obejct
  240. */
  241. function file_postupdate_standard_filemanager($data, $field, array $options, $context, $component, $filearea, $itemid) {
  242. $options = (array)$options;
  243. if (!isset($options['subdirs'])) {
  244. $options['subdirs'] = false;
  245. }
  246. if (!isset($options['maxfiles'])) {
  247. $options['maxfiles'] = -1; // unlimited
  248. }
  249. if (!isset($options['maxbytes'])) {
  250. $options['maxbytes'] = 0; // unlimited
  251. }
  252. if (empty($data->{$field.'_filemanager'})) {
  253. $data->$field = '';
  254. } else {
  255. file_save_draft_area_files($data->{$field.'_filemanager'}, $context->id, $component, $filearea, $itemid, $options);
  256. $fs = get_file_storage();
  257. if ($fs->get_area_files($context->id, $component, $filearea, $itemid)) {
  258. $data->$field = '1'; // TODO: this is an ugly hack (skodak)
  259. } else {
  260. $data->$field = '';
  261. }
  262. }
  263. return $data;
  264. }
  265. /**
  266. *
  267. * @global object
  268. * @global object
  269. * @return int a random but available draft itemid that can be used to create a new draft
  270. * file area.
  271. */
  272. function file_get_unused_draft_itemid() {
  273. global $DB, $USER;
  274. if (isguestuser() or !isloggedin()) {
  275. // guests and not-logged-in users can not be allowed to upload anything!!!!!!
  276. print_error('noguest');
  277. }
  278. $contextid = get_context_instance(CONTEXT_USER, $USER->id)->id;
  279. $fs = get_file_storage();
  280. $draftitemid = rand(1, 999999999);
  281. while ($files = $fs->get_area_files($contextid, 'user', 'draft', $draftitemid)) {
  282. $draftitemid = rand(1, 999999999);
  283. }
  284. return $draftitemid;
  285. }
  286. /**
  287. * Initialise a draft file area from a real one by copying the files. A draft
  288. * area will be created if one does not already exist. Normally you should
  289. * get $draftitemid by calling file_get_submitted_draft_itemid('elementname');
  290. *
  291. * @global object
  292. * @global object
  293. * @param int &$draftitemid the id of the draft area to use, or 0 to create a new one, in which case this parameter is updated.
  294. * @param integer $contextid This parameter and the next two identify the file area to copy files from.
  295. * @param string $component
  296. * @param string $filearea helps indentify the file area.
  297. * @param integer $itemid helps identify the file area. Can be null if there are no files yet.
  298. * @param array $options text and file options ('subdirs'=>false, 'forcehttps'=>false)
  299. * @param string $text some html content that needs to have embedded links rewritten to point to the draft area.
  300. * @return string if $text was passed in, the rewritten $text is returned. Otherwise NULL.
  301. */
  302. function file_prepare_draft_area(&$draftitemid, $contextid, $component, $filearea, $itemid, array $options=null, $text=null) {
  303. global $CFG, $USER, $CFG;
  304. $options = (array)$options;
  305. if (!isset($options['subdirs'])) {
  306. $options['subdirs'] = false;
  307. }
  308. if (!isset($options['forcehttps'])) {
  309. $options['forcehttps'] = false;
  310. }
  311. $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
  312. $fs = get_file_storage();
  313. if (empty($draftitemid)) {
  314. // create a new area and copy existing files into
  315. $draftitemid = file_get_unused_draft_itemid();
  316. $file_record = array('contextid'=>$usercontext->id, 'component'=>'user', 'filearea'=>'draft', 'itemid'=>$draftitemid);
  317. if (!is_null($itemid) and $files = $fs->get_area_files($contextid, $component, $filearea, $itemid)) {
  318. foreach ($files as $file) {
  319. if ($file->is_directory() and $file->get_filepath() === '/') {
  320. // we need a way to mark the age of each draft area,
  321. // by not copying the root dir we force it to be created automatically with current timestamp
  322. continue;
  323. }
  324. if (!$options['subdirs'] and ($file->is_directory() or $file->get_filepath() !== '/')) {
  325. continue;
  326. }
  327. $fs->create_file_from_storedfile($file_record, $file);
  328. }
  329. }
  330. if (!is_null($text)) {
  331. // at this point there should not be any draftfile links yet,
  332. // because this is a new text from database that should still contain the @@pluginfile@@ links
  333. // this happens when developers forget to post process the text
  334. $text = str_replace("\"$CFG->httpswwwroot/draftfile.php", "\"$CFG->httpswwwroot/brokenfile.php#", $text);
  335. }
  336. } else {
  337. // nothing to do
  338. }
  339. if (is_null($text)) {
  340. return null;
  341. }
  342. // relink embedded files - editor can not handle @@PLUGINFILE@@ !
  343. return file_rewrite_pluginfile_urls($text, 'draftfile.php', $usercontext->id, 'user', 'draft', $draftitemid, $options);
  344. }
  345. /**
  346. * Convert encoded URLs in $text from the @@PLUGINFILE@@/... form to an actual URL.
  347. *
  348. * @global object
  349. * @param string $text The content that may contain ULRs in need of rewriting.
  350. * @param string $file The script that should be used to serve these files. pluginfile.php, draftfile.php, etc.
  351. * @param integer $contextid This parameter and the next two identify the file area to use.
  352. * @param string $component
  353. * @param string $filearea helps identify the file area.
  354. * @param integer $itemid helps identify the file area.
  355. * @param array $options text and file options ('forcehttps'=>false)
  356. * @return string the processed text.
  357. */
  358. function file_rewrite_pluginfile_urls($text, $file, $contextid, $component, $filearea, $itemid, array $options=null) {
  359. global $CFG;
  360. $options = (array)$options;
  361. if (!isset($options['forcehttps'])) {
  362. $options['forcehttps'] = false;
  363. }
  364. if (!$CFG->slasharguments) {
  365. $file = $file . '?file=';
  366. }
  367. $baseurl = "$CFG->wwwroot/$file/$contextid/$component/$filearea/";
  368. if ($itemid !== null) {
  369. $baseurl .= "$itemid/";
  370. }
  371. if ($options['forcehttps']) {
  372. $baseurl = str_replace('http://', 'https://', $baseurl);
  373. }
  374. return str_replace('@@PLUGINFILE@@/', $baseurl, $text);
  375. }
  376. /**
  377. * Returns information about files in a draft area.
  378. *
  379. * @global object
  380. * @global object
  381. * @param integer $draftitemid the draft area item id.
  382. * @return array with the following entries:
  383. * 'filecount' => number of files in the draft area.
  384. * (more information will be added as needed).
  385. */
  386. function file_get_draft_area_info($draftitemid) {
  387. global $CFG, $USER;
  388. $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
  389. $fs = get_file_storage();
  390. $results = array();
  391. // The number of files
  392. $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id', false);
  393. $results['filecount'] = count($draftfiles);
  394. $results['filesize'] = 0;
  395. foreach ($draftfiles as $file) {
  396. $results['filesize'] += $file->get_filesize();
  397. }
  398. return $results;
  399. }
  400. /**
  401. * Get used space of files
  402. * @return int total bytes
  403. */
  404. function file_get_user_used_space() {
  405. global $DB, $USER;
  406. $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
  407. $sql = "SELECT SUM(files1.filesize) AS totalbytes FROM {files} files1
  408. JOIN (SELECT contenthash, filename, MAX(id) AS id
  409. FROM {files}
  410. WHERE contextid = ? AND component = ? AND filearea != ?
  411. GROUP BY contenthash, filename) files2 ON files1.id = files2.id";
  412. $params = array('contextid'=>$usercontext->id, 'component'=>'user', 'filearea'=>'draft');
  413. $record = $DB->get_record_sql($sql, $params);
  414. return (int)$record->totalbytes;
  415. }
  416. /**
  417. * Convert any string to a valid filepath
  418. * @param string $str
  419. * @return string path
  420. */
  421. function file_correct_filepath($str) { //TODO: what is this? (skodak)
  422. if ($str == '/' or empty($str)) {
  423. return '/';
  424. } else {
  425. return '/'.trim($str, './@#$ ').'/';
  426. }
  427. }
  428. /**
  429. * Generate a folder tree of draft area of current USER recursively
  430. * @param int $itemid
  431. * @param string $filepath
  432. * @param mixed $data //TODO: use normal return value instead, this does not fit the rest of api here (skodak)
  433. */
  434. function file_get_drafarea_folders($draftitemid, $filepath, &$data) {
  435. global $USER, $OUTPUT, $CFG;
  436. $data->children = array();
  437. $context = get_context_instance(CONTEXT_USER, $USER->id);
  438. $fs = get_file_storage();
  439. if ($files = $fs->get_directory_files($context->id, 'user', 'draft', $draftitemid, $filepath, false)) {
  440. foreach ($files as $file) {
  441. if ($file->is_directory()) {
  442. $item = new stdClass();
  443. $item->sortorder = $file->get_sortorder();
  444. $item->filepath = $file->get_filepath();
  445. $foldername = explode('/', trim($item->filepath, '/'));
  446. $item->fullname = trim(array_pop($foldername), '/');
  447. $item->id = uniqid();
  448. file_get_drafarea_folders($draftitemid, $item->filepath, $item);
  449. $data->children[] = $item;
  450. } else {
  451. continue;
  452. }
  453. }
  454. }
  455. }
  456. /**
  457. * Listing all files (including folders) in current path (draft area)
  458. * used by file manager
  459. * @param int $draftitemid
  460. * @param string $filepath
  461. * @return mixed
  462. */
  463. function file_get_drafarea_files($draftitemid, $filepath = '/') {
  464. global $USER, $OUTPUT, $CFG;
  465. $context = get_context_instance(CONTEXT_USER, $USER->id);
  466. $fs = get_file_storage();
  467. $data = new stdClass();
  468. $data->path = array();
  469. $data->path[] = array('name'=>get_string('files'), 'path'=>'/');
  470. // will be used to build breadcrumb
  471. $trail = '';
  472. if ($filepath !== '/') {
  473. $filepath = file_correct_filepath($filepath);
  474. $parts = explode('/', $filepath);
  475. foreach ($parts as $part) {
  476. if ($part != '' && $part != null) {
  477. $trail .= ('/'.$part.'/');
  478. $data->path[] = array('name'=>$part, 'path'=>$trail);
  479. }
  480. }
  481. }
  482. $list = array();
  483. $maxlength = 12;
  484. if ($files = $fs->get_directory_files($context->id, 'user', 'draft', $draftitemid, $filepath, false)) {
  485. foreach ($files as $file) {
  486. $item = new stdClass();
  487. $item->filename = $file->get_filename();
  488. $item->filepath = $file->get_filepath();
  489. $item->fullname = trim($item->filename, '/');
  490. $filesize = $file->get_filesize();
  491. $item->filesize = $filesize ? display_size($filesize) : '';
  492. $icon = mimeinfo_from_type('icon', $file->get_mimetype());
  493. $item->icon = $OUTPUT->pix_url('f/' . $icon)->out();
  494. $item->sortorder = $file->get_sortorder();
  495. if ($icon == 'zip') {
  496. $item->type = 'zip';
  497. } else {
  498. $item->type = 'file';
  499. }
  500. if ($file->is_directory()) {
  501. $item->filesize = 0;
  502. $item->icon = $OUTPUT->pix_url('f/folder')->out();
  503. $item->type = 'folder';
  504. $foldername = explode('/', trim($item->filepath, '/'));
  505. $item->fullname = trim(array_pop($foldername), '/');
  506. } else {
  507. // do NOT use file browser here!
  508. $item->url = moodle_url::make_draftfile_url($draftitemid, $item->filepath, $item->filename)->out();
  509. }
  510. $list[] = $item;
  511. }
  512. }
  513. $data->itemid = $draftitemid;
  514. $data->list = $list;
  515. return $data;
  516. }
  517. /**
  518. * Returns draft area itemid for a given element.
  519. *
  520. * @param string $elname name of formlib editor element, or a hidden form field that stores the draft area item id, etc.
  521. * @return integer the itemid, or 0 if there is not one yet.
  522. */
  523. function file_get_submitted_draft_itemid($elname) {
  524. $param = optional_param($elname, 0, PARAM_INT);
  525. if ($param) {
  526. require_sesskey();
  527. }
  528. if (is_array($param)) {
  529. if (!empty($param['itemid'])) {
  530. $param = $param['itemid'];
  531. } else {
  532. debugging('Missing itemid, maybe caused by unset maxfiles option', DEBUG_DEVELOPER);
  533. return false;
  534. }
  535. }
  536. return $param;
  537. }
  538. /**
  539. * Saves files from a draft file area to a real one (merging the list of files).
  540. * Can rewrite URLs in some content at the same time if desired.
  541. *
  542. * @global object
  543. * @global object
  544. * @param integer $draftitemid the id of the draft area to use. Normally obtained
  545. * from file_get_submitted_draft_itemid('elementname') or similar.
  546. * @param integer $contextid This parameter and the next two identify the file area to save to.
  547. * @param string $component
  548. * @param string $filearea indentifies the file area.
  549. * @param integer $itemid helps identifies the file area.
  550. * @param array $options area options (subdirs=>false, maxfiles=-1, maxbytes=0)
  551. * @param string $text some html content that needs to have embedded links rewritten
  552. * to the @@PLUGINFILE@@ form for saving in the database.
  553. * @param boolean $forcehttps force https urls.
  554. * @return string if $text was passed in, the rewritten $text is returned. Otherwise NULL.
  555. */
  556. function file_save_draft_area_files($draftitemid, $contextid, $component, $filearea, $itemid, array $options=null, $text=null, $forcehttps=false) {
  557. global $USER;
  558. $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
  559. $fs = get_file_storage();
  560. $options = (array)$options;
  561. if (!isset($options['subdirs'])) {
  562. $options['subdirs'] = false;
  563. }
  564. if (!isset($options['maxfiles'])) {
  565. $options['maxfiles'] = -1; // unlimited
  566. }
  567. if (!isset($options['maxbytes'])) {
  568. $options['maxbytes'] = 0; // unlimited
  569. }
  570. $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid, 'id');
  571. $oldfiles = $fs->get_area_files($contextid, $component, $filearea, $itemid, 'id');
  572. if (count($draftfiles) < 2) {
  573. // means there are no files - one file means root dir only ;-)
  574. $fs->delete_area_files($contextid, $component, $filearea, $itemid);
  575. } else if (count($oldfiles) < 2) {
  576. $filecount = 0;
  577. // there were no files before - one file means root dir only ;-)
  578. $file_record = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid);
  579. foreach ($draftfiles as $file) {
  580. if (!$options['subdirs']) {
  581. if ($file->get_filepath() !== '/' or $file->is_directory()) {
  582. continue;
  583. }
  584. }
  585. if ($options['maxbytes'] and $options['maxbytes'] < $file->get_filesize()) {
  586. // oversized file - should not get here at all
  587. continue;
  588. }
  589. if ($options['maxfiles'] != -1 and $options['maxfiles'] <= $filecount) {
  590. // more files - should not get here at all
  591. break;
  592. }
  593. if (!$file->is_directory()) {
  594. $filecount++;
  595. }
  596. $fs->create_file_from_storedfile($file_record, $file);
  597. }
  598. } else {
  599. // we have to merge old and new files - we want to keep file ids for files that were not changed
  600. // we change time modified for all new and changed files, we keep time created as is
  601. $file_record = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'timemodified'=>time());
  602. $newhashes = array();
  603. foreach ($draftfiles as $file) {
  604. $newhash = $fs->get_pathname_hash($contextid, $component, $filearea, $itemid, $file->get_filepath(), $file->get_filename());
  605. $newhashes[$newhash] = $file;
  606. }
  607. $filecount = 0;
  608. foreach ($oldfiles as $oldfile) {
  609. $oldhash = $oldfile->get_pathnamehash();
  610. if (!isset($newhashes[$oldhash])) {
  611. // delete files not needed any more - deleted by user
  612. $oldfile->delete();
  613. continue;
  614. }
  615. $newfile = $newhashes[$oldhash];
  616. if ($oldfile->get_contenthash() != $newfile->get_contenthash() or $oldfile->get_sortorder() != $newfile->get_sortorder()
  617. or $oldfile->get_status() != $newfile->get_status() or $oldfile->get_license() != $newfile->get_license()
  618. or $oldfile->get_author() != $newfile->get_author() or $oldfile->get_source() != $newfile->get_source()) {
  619. // file was changed, use updated with new timemodified data
  620. $oldfile->delete();
  621. continue;
  622. }
  623. // unchanged file or directory - we keep it as is
  624. unset($newhashes[$oldhash]);
  625. if (!$oldfile->is_directory()) {
  626. $filecount++;
  627. }
  628. }
  629. // now add new/changed files
  630. // the size and subdirectory tests are extra safety only, the UI should prevent it
  631. foreach ($newhashes as $file) {
  632. if (!$options['subdirs']) {
  633. if ($file->get_filepath() !== '/' or $file->is_directory()) {
  634. continue;
  635. }
  636. }
  637. if ($options['maxbytes'] and $options['maxbytes'] < $file->get_filesize()) {
  638. // oversized file - should not get here at all
  639. continue;
  640. }
  641. if ($options['maxfiles'] != -1 and $options['maxfiles'] <= $filecount) {
  642. // more files - should not get here at all
  643. break;
  644. }
  645. if (!$file->is_directory()) {
  646. $filecount++;
  647. }
  648. $fs->create_file_from_storedfile($file_record, $file);
  649. }
  650. }
  651. // note: do not purge the draft area - we clean up areas later in cron,
  652. // the reason is that user might press submit twice and they would loose the files,
  653. // also sometimes we might want to use hacks that save files into two different areas
  654. if (is_null($text)) {
  655. return null;
  656. } else {
  657. return file_rewrite_urls_to_pluginfile($text, $draftitemid, $forcehttps);
  658. }
  659. }
  660. /**
  661. * Convert the draft file area URLs in some content to @@PLUGINFILE@@ tokens
  662. * ready to be saved in the database. Normally, this is done automatically by
  663. * {@link file_save_draft_area_files()}.
  664. * @param string $text the content to process.
  665. * @param int $draftitemid the draft file area the content was using.
  666. * @param bool $forcehttps whether the content contains https URLs. Default false.
  667. * @return string the processed content.
  668. */
  669. function file_rewrite_urls_to_pluginfile($text, $draftitemid, $forcehttps = false) {
  670. global $CFG, $USER;
  671. $usercontext = get_context_instance(CONTEXT_USER, $USER->id);
  672. $wwwroot = $CFG->wwwroot;
  673. if ($forcehttps) {
  674. $wwwroot = str_replace('http://', 'https://', $wwwroot);
  675. }
  676. // relink embedded files if text submitted - no absolute links allowed in database!
  677. $text = str_ireplace("$wwwroot/draftfile.php/$usercontext->id/user/draft/$draftitemid/", '@@PLUGINFILE@@/', $text);
  678. if (strpos($text, 'draftfile.php?file=') !== false) {
  679. $matches = array();
  680. preg_match_all("!$wwwroot/draftfile.php\?file=%2F{$usercontext->id}%2Fuser%2Fdraft%2F{$draftitemid}%2F[^'\",&<>|`\s:\\\\]+!iu", $text, $matches);
  681. if ($matches) {
  682. foreach ($matches[0] as $match) {
  683. $replace = str_ireplace('%2F', '/', $match);
  684. $text = str_replace($match, $replace, $text);
  685. }
  686. }
  687. $text = str_ireplace("$wwwroot/draftfile.php?file=/$usercontext->id/user/draft/$draftitemid/", '@@PLUGINFILE@@/', $text);
  688. }
  689. return $text;
  690. }
  691. /**
  692. * Set file sort order
  693. * @global object $DB
  694. * @param integer $contextid the context id
  695. * @param string $component
  696. * @param string $filearea file area.
  697. * @param integer $itemid itemid.
  698. * @param string $filepath file path.
  699. * @param string $filename file name.
  700. * @param integer $sortorer the sort order of file.
  701. * @return boolean
  702. */
  703. function file_set_sortorder($contextid, $component, $filearea, $itemid, $filepath, $filename, $sortorder) {
  704. global $DB;
  705. $conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename);
  706. if ($file_record = $DB->get_record('files', $conditions)) {
  707. $sortorder = (int)$sortorder;
  708. $file_record->sortorder = $sortorder;
  709. $DB->update_record('files', $file_record);
  710. return true;
  711. }
  712. return false;
  713. }
  714. /**
  715. * reset file sort order number to 0
  716. * @global object $DB
  717. * @param integer $contextid the context id
  718. * @param string $component
  719. * @param string $filearea file area.
  720. * @param integer $itemid itemid.
  721. * @return boolean
  722. */
  723. function file_reset_sortorder($contextid, $component, $filearea, $itemid=false) {
  724. global $DB;
  725. $conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea);
  726. if ($itemid !== false) {
  727. $conditions['itemid'] = $itemid;
  728. }
  729. $file_records = $DB->get_records('files', $conditions);
  730. foreach ($file_records as $file_record) {
  731. $file_record->sortorder = 0;
  732. $DB->update_record('files', $file_record);
  733. }
  734. return true;
  735. }
  736. /**
  737. * Returns description of upload error
  738. *
  739. * @param int $errorcode found in $_FILES['filename.ext']['error']
  740. * @return string error description string, '' if ok
  741. */
  742. function file_get_upload_error($errorcode) {
  743. switch ($errorcode) {
  744. case 0: // UPLOAD_ERR_OK - no error
  745. $errmessage = '';
  746. break;
  747. case 1: // UPLOAD_ERR_INI_SIZE
  748. $errmessage = get_string('uploadserverlimit');
  749. break;
  750. case 2: // UPLOAD_ERR_FORM_SIZE
  751. $errmessage = get_string('uploadformlimit');
  752. break;
  753. case 3: // UPLOAD_ERR_PARTIAL
  754. $errmessage = get_string('uploadpartialfile');
  755. break;
  756. case 4: // UPLOAD_ERR_NO_FILE
  757. $errmessage = get_string('uploadnofilefound');
  758. break;
  759. // Note: there is no error with a value of 5
  760. case 6: // UPLOAD_ERR_NO_TMP_DIR
  761. $errmessage = get_string('uploadnotempdir');
  762. break;
  763. case 7: // UPLOAD_ERR_CANT_WRITE
  764. $errmessage = get_string('uploadcantwrite');
  765. break;
  766. case 8: // UPLOAD_ERR_EXTENSION
  767. $errmessage = get_string('uploadextension');
  768. break;
  769. default:
  770. $errmessage = get_string('uploadproblem');
  771. }
  772. return $errmessage;
  773. }
  774. /**
  775. * Recursive function formating an array in POST parameter
  776. * @param array $arraydata - the array that we are going to format and add into &$data array
  777. * @param string $currentdata - a row of the final postdata array at instant T
  778. * when finish, it's assign to $data under this format: name[keyname][][]...[]='value'
  779. * @param array $data - the final data array containing all POST parameters : 1 row = 1 parameter
  780. */
  781. function format_array_postdata_for_curlcall($arraydata, $currentdata, &$data) {
  782. foreach ($arraydata as $k=>$v) {
  783. $newcurrentdata = $currentdata;
  784. if (is_array($v)) { //the value is an array, call the function recursively
  785. $newcurrentdata = $newcurrentdata.'['.urlencode($k).']';
  786. format_array_postdata_for_curlcall($v, $newcurrentdata, $data);
  787. } else { //add the POST parameter to the $data array
  788. $data[] = $newcurrentdata.'['.urlencode($k).']='.urlencode($v);
  789. }
  790. }
  791. }
  792. /**
  793. * Transform a PHP array into POST parameter
  794. * (see the recursive function format_array_postdata_for_curlcall)
  795. * @param array $postdata
  796. * @return array containing all POST parameters (1 row = 1 POST parameter)
  797. */
  798. function format_postdata_for_curlcall($postdata) {
  799. $data = array();
  800. foreach ($postdata as $k=>$v) {
  801. if (is_array($v)) {
  802. $currentdata = urlencode($k);
  803. format_array_postdata_for_curlcall($v, $currentdata, $data);
  804. } else {
  805. $data[] = urlencode($k).'='.urlencode($v);
  806. }
  807. }
  808. $convertedpostdata = implode('&', $data);
  809. return $convertedpostdata;
  810. }
  811. /**
  812. * Fetches content of file from Internet (using proxy if defined). Uses cURL extension if present.
  813. * Due to security concerns only downloads from http(s) sources are supported.
  814. *
  815. * @param string $url file url starting with http(s)://
  816. * @param array $headers http headers, null if none. If set, should be an
  817. * associative array of header name => value pairs.
  818. * @param array $postdata array means use POST request with given parameters
  819. * @param bool $fullresponse return headers, responses, etc in a similar way snoopy does
  820. * (if false, just returns content)
  821. * @param int $timeout timeout for complete download process including all file transfer
  822. * (default 5 minutes)
  823. * @param int $connecttimeout timeout for connection to server; this is the timeout that
  824. * usually happens if the remote server is completely down (default 20 seconds);
  825. * may not work when using proxy
  826. * @param bool $skipcertverify If true, the peer's SSL certificate will not be checked.
  827. * Only use this when already in a trusted location.
  828. * @param string $tofile store the downloaded content to file instead of returning it.
  829. * @param bool $calctimeout false by default, true enables an extra head request to try and determine
  830. * filesize and appropriately larger timeout based on $CFG->curltimeoutkbitrate
  831. * @return mixed false if request failed or content of the file as string if ok. True if file downloaded into $tofile successfully.
  832. */
  833. function download_file_content($url, $headers=null, $postdata=null, $fullresponse=false, $timeout=300, $connecttimeout=20, $skipcertverify=false, $tofile=NULL, $calctimeout=false) {
  834. global $CFG;
  835. // some extra security
  836. $newlines = array("\r", "\n");
  837. if (is_array($headers) ) {
  838. foreach ($headers as $key => $value) {
  839. $headers[$key] = str_replace($newlines, '', $value);
  840. }
  841. }
  842. $url = str_replace($newlines, '', $url);
  843. if (!preg_match('|^https?://|i', $url)) {
  844. if ($fullresponse) {
  845. $response = new stdClass();
  846. $response->status = 0;
  847. $response->headers = array();
  848. $response->response_code = 'Invalid protocol specified in url';
  849. $response->results = '';
  850. $response->error = 'Invalid protocol specified in url';
  851. return $response;
  852. } else {
  853. return false;
  854. }
  855. }
  856. // check if proxy (if used) should be bypassed for this url
  857. $proxybypass = is_proxybypass($url);
  858. if (!$ch = curl_init($url)) {
  859. debugging('Can not init curl.');
  860. return false;
  861. }
  862. // set extra headers
  863. if (is_array($headers) ) {
  864. $headers2 = array();
  865. foreach ($headers as $key => $value) {
  866. $headers2[] = "$key: $value";
  867. }
  868. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers2);
  869. }
  870. if ($skipcertverify) {
  871. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  872. }
  873. // use POST if requested
  874. if (is_array($postdata)) {
  875. $postdata = format_postdata_for_curlcall($postdata);
  876. curl_setopt($ch, CURLOPT_POST, true);
  877. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  878. }
  879. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  880. curl_setopt($ch, CURLOPT_HEADER, false);
  881. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
  882. if (!ini_get('open_basedir') and !ini_get('safe_mode')) {
  883. // TODO: add version test for '7.10.5'
  884. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  885. curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
  886. }
  887. if (!empty($CFG->proxyhost) and !$proxybypass) {
  888. // SOCKS supported in PHP5 only
  889. if (!empty($CFG->proxytype) and ($CFG->proxytype == 'SOCKS5')) {
  890. if (defined('CURLPROXY_SOCKS5')) {
  891. curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  892. } else {
  893. curl_close($ch);
  894. if ($fullresponse) {
  895. $response = new stdClass();
  896. $response->status = '0';
  897. $response->headers = array();
  898. $response->response_code = 'SOCKS5 proxy is not supported in PHP4';
  899. $response->results = '';
  900. $response->error = 'SOCKS5 proxy is not supported in PHP4';
  901. return $response;
  902. } else {
  903. debugging("SOCKS5 proxy is not supported in PHP4.", DEBUG_ALL);
  904. return false;
  905. }
  906. }
  907. }
  908. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
  909. if (empty($CFG->proxyport)) {
  910. curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost);
  911. } else {
  912. curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost.':'.$CFG->proxyport);
  913. }
  914. if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
  915. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
  916. if (defined('CURLOPT_PROXYAUTH')) {
  917. // any proxy authentication if PHP 5.1
  918. curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
  919. }
  920. }
  921. }
  922. // set up header and content handlers
  923. $received = new stdClass();
  924. $received->headers = array(); // received headers array
  925. $received->tofile = $tofile;
  926. $received->fh = null;
  927. curl_setopt($ch, CURLOPT_HEADERFUNCTION, partial('download_file_content_header_handler', $received));
  928. if ($tofile) {
  929. curl_setopt($ch, CURLOPT_WRITEFUNCTION, partial('download_file_content_write_handler', $received));
  930. }
  931. if (!isset($CFG->curltimeoutkbitrate)) {
  932. //use very slow rate of 56kbps as a timeout speed when not set
  933. $bitrate = 56;
  934. } else {
  935. $bitrate = $CFG->curltimeoutkbitrate;
  936. }
  937. // try to calculate the proper amount for timeout from remote file size.
  938. // if disabled or zero, we won't do any checks nor head requests.
  939. if ($calctimeout && $bitrate > 0) {
  940. //setup header request only options
  941. curl_setopt_array ($ch, array(
  942. CURLOPT_RETURNTRANSFER => false,
  943. CURLOPT_NOBODY => true)
  944. );
  945. curl_exec($ch);
  946. $info = curl_getinfo($ch);
  947. $err = curl_error($ch);
  948. if ($err === '' && $info['download_content_length'] > 0) { //no curl errors
  949. $timeout = max($timeout, ceil($info['download_content_length'] * 8 / ($bitrate * 1024))); //adjust for large files only - take max timeout.
  950. }
  951. //reinstate affected curl options
  952. curl_setopt_array ($ch, array(
  953. CURLOPT_RETURNTRANSFER => true,
  954. CURLOPT_NOBODY => false)
  955. );
  956. }
  957. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  958. $result = curl_exec($ch);
  959. // try to detect encoding problems
  960. if ((curl_errno($ch) == 23 or curl_errno($ch) == 61) and defined('CURLOPT_ENCODING')) {
  961. curl_setopt($ch, CURLOPT_ENCODING, 'none');
  962. $result = curl_exec($ch);
  963. }
  964. if ($received->fh) {
  965. fclose($received->fh);
  966. }
  967. if (curl_errno($ch)) {
  968. $error = curl_error($ch);
  969. $error_no = curl_errno($ch);
  970. curl_close($ch);
  971. if ($fullresponse) {
  972. $response = new stdClass();
  973. if ($error_no == 28) {
  974. $response->status = '-100'; // mimic snoopy
  975. } else {
  976. $response->status = '0';
  977. }
  978. $response->headers = array();
  979. $response->response_code = $error;
  980. $response->results = false;
  981. $response->error = $error;
  982. return $response;
  983. } else {
  984. debugging("cURL request for \"$url\" failed with: $error ($error_no)", DEBUG_ALL);
  985. return false;
  986. }
  987. } else {
  988. $info = curl_getinfo($ch);
  989. curl_close($ch);
  990. if (empty($info['http_code'])) {
  991. // for security reasons we support only true http connections (Location: file:// exploit prevention)
  992. $response = new stdClass();
  993. $response->status = '0';
  994. $response->headers = array();
  995. $response->response_code = 'Unknown cURL error';
  996. $response->results = false; // do NOT change this, we really want to ignore the result!
  997. $response->error = 'Unknown cURL error';
  998. } else {
  999. $response = new stdClass();;
  1000. $response->status = (string)$info['http_code'];
  1001. $response->headers = $received->headers;
  1002. $response->response_code = $received->headers[0];
  1003. $response->results = $result;
  1004. $response->error = '';
  1005. }
  1006. if ($fullresponse) {
  1007. return $response;
  1008. } else if ($info['http_code'] != 200) {
  1009. debugging("cURL request for \"$url\" failed, HTTP response code: ".$response->response_code, DEBUG_ALL);
  1010. return false;
  1011. } else {
  1012. return $response->results;
  1013. }
  1014. }
  1015. }
  1016. /**
  1017. * internal implementation
  1018. */
  1019. function download_file_content_header_handler($received, $ch, $header) {
  1020. $received->headers[] = $header;
  1021. return strlen($header);
  1022. }
  1023. /**
  1024. * internal implementation
  1025. */
  1026. function download_file_content_write_handler($received, $ch, $data) {
  1027. if (!$received->fh) {
  1028. $received->fh = fopen($received->tofile, 'w');
  1029. if ($received->fh === false) {
  1030. // bad luck, file creation or overriding failed
  1031. return 0;
  1032. }
  1033. }
  1034. if (fwrite($received->fh, $data) === false) {
  1035. // bad luck, write failed, let's abort completely
  1036. return 0;
  1037. }
  1038. return strlen($data);
  1039. }
  1040. /**
  1041. * @return array List of information about file types based on extensions.
  1042. * Associative array of extension (lower-case) to associative array
  1043. * from 'element name' to data. Current element names are 'type' and 'icon'.
  1044. * Unknown types should use the 'xxx' entry which includes defaults.
  1045. */
  1046. function get_mimetypes_array() {
  1047. static $mimearray = array (
  1048. 'xxx' => array ('type'=>'document/unknown', 'icon'=>'unknown'),
  1049. '3gp' => array ('type'=>'video/quicktime', 'icon'=>'video'),
  1050. 'aac' => array ('type'=>'audio/aac', 'icon'=>'audio'),
  1051. 'ai' => array ('type'=>'application/postscript', 'icon'=>'image'),
  1052. 'aif' => array ('type'=>'audio/x-aiff', 'icon'=>'audio'),
  1053. 'aiff' => array ('type'=>'audio/x-aiff', 'icon'=>'audio'),
  1054. 'aifc' => array ('type'=>'audio/x-aiff', 'icon'=>'audio'),
  1055. 'applescript' => array ('type'=>'text/plain', 'icon'=>'text'),
  1056. 'asc' => array ('type'=>'text/plain', 'icon'=>'text'),
  1057. 'asm' => array ('type'=>'text/plain', 'icon'=>'text'),
  1058. 'au' => array ('type'=>'audio/au', 'icon'=>'audio'),
  1059. 'avi' => array ('type'=>'video/x-ms-wm', 'icon'=>'avi'),
  1060. 'bmp' => array ('type'=>'image/bmp', 'icon'=>'image'),
  1061. 'c' => array ('type'=>'text/plain', 'icon'=>'text'),
  1062. 'cct' => array ('type'=>'shockwave/director', 'icon'=>'flash'),
  1063. 'cpp' => array ('type'=>'text/plain', 'icon'=>'text'),
  1064. 'cs' => array ('type'=>'application/x-csh', 'icon'=>'text'),
  1065. 'css' => array ('type'=>'text/css', 'icon'=>'text'),
  1066. 'csv' => array ('type'=>'text/csv', 'icon'=>'excel'),
  1067. 'dv' => array ('type'=>'video/x-dv', 'icon'=>'video'),
  1068. 'dmg' => array ('type'=>'application/octet-stream', 'icon'=>'dmg'),
  1069. 'doc' => array ('type'=>'application/msword', 'icon'=>'word'),
  1070. 'docx' => array ('type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'icon'=>'docx'),
  1071. 'docm' => array ('type'=>'application/vnd.ms-word.document.macroEnabled.12', 'icon'=>'docm'),
  1072. 'dotx' => array ('type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'icon'=>'dotx'),
  1073. 'dotm' => array ('type'=>'application/vnd.ms-word.template.macroEnabled.12', 'icon'=>'dotm'),
  1074. 'dcr' => array ('type'=>'application/x-director', 'icon'=>'flash'),
  1075. 'dif' => array ('type'=>'video/x-dv', 'icon'=>'video'),
  1076. 'dir' => array ('type'=>'application/x-director', 'icon'=>'flash'),
  1077. 'dxr' => array ('type'=>'application/x-director', 'icon'=>'flash'),
  1078. 'eps' => array ('type'=>'application/postscript', 'icon'=>'pdf'),
  1079. 'fdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
  1080. 'flv' => array ('type'=>'video/x-flv', 'icon'=>'video'),
  1081. 'f4v' => array ('type'=>'video/mp4', 'icon'=>'video'),
  1082. 'gif' => array ('type'=>'image/gif', 'icon'=>'image'),
  1083. 'gtar' => array ('type'=>'application/x-gtar', 'icon'=>'zip'),
  1084. 'tgz' => array ('type'=>'application/g-zip', 'icon'=>'zip'),
  1085. 'gz' => array ('type'=>'application/g-zip', 'icon'=>'zip'),
  1086. 'gzip' => array ('type'=>'application/g-zip', 'icon'=>'zip'),
  1087. 'h' => array ('type'=>'text/plain', 'icon'=>'text'),
  1088. 'hpp' => array ('type'=>'text/plain', 'icon'=>'text'),
  1089. 'hqx' => array ('type'=>'application/mac-binhex40', 'icon'=>'zip'),
  1090. 'htc' => array ('type'=>'text/x-component', 'icon'=>'text'),
  1091. 'html' => array ('type'=>'text/html', 'icon'=>'html'),
  1092. 'xhtml'=> array ('type'=>'application/xhtml+xml', 'icon'=>'html'),
  1093. 'htm' => array ('type'=>'text/html', 'icon'=>'html'),
  1094. 'ico' => array ('type'=>'image/vnd.microsoft.icon', 'icon'=>'image'),
  1095. 'ics' => array ('type'=>'text/calendar', 'icon'=>'text'),
  1096. 'isf' => array ('type'=>'application/inspiration', 'icon'=>'isf'),
  1097. 'ist' => array ('type'=>'application/inspiration.template', 'icon'=>'isf'),
  1098. 'java' => array ('type'=>'text/plain', 'icon'=>'text'),
  1099. 'jcb' => array ('type'=>'text/xml', 'icon'=>'jcb'),
  1100. 'jcl' => array ('type'=>'text/xml', 'icon'=>'jcl'),
  1101. 'jcw' => array ('type'=>'text/xml', 'icon'=>'jcw'),
  1102. 'jmt' => array ('type'=>'text/xml', 'icon'=>'jmt'),
  1103. 'jmx' => array ('type'=>'text/xml', 'icon'=>'jmx'),
  1104. 'jpe' => array ('type'=>'image/jpeg', 'icon'=>'image'),
  1105. 'jpeg' => array ('type'=>'image/jpeg', 'icon'=>'image'),
  1106. 'jpg' => array ('type'=>'image/jpeg', 'icon'=>'image'),
  1107. 'jqz' => array ('type'=>'text/xml', 'icon'=>'jqz'),
  1108. 'js' => array ('type'=>'application/x-javascript', 'icon'=>'text'),
  1109. 'latex'=> array ('type'=>'application/x-latex', 'icon'=>'text'),
  1110. 'm' => array ('type'=>'text/plain', 'icon'=>'text'),
  1111. 'mbz' => array ('type'=>'application/vnd.moodle.backup', 'icon'=>'moodle'),
  1112. 'mov' => array ('type'=>'video/quicktime', 'icon'=>'video'),
  1113. 'movie'=> array ('type'=>'video/x-sgi-movie', 'icon'=>'video'),
  1114. 'm3u' => array ('type'=>'audio/x-mpegurl', 'icon'=>'audio'),
  1115. 'mp3' => array ('type'=>'audio/mp3', 'icon'=>'audio'),
  1116. 'mp4' => array ('type'=>'video/mp4', 'icon'=>'video'),
  1117. 'm4v' => array ('type'=>'video/mp4', 'icon'=>'video'),
  1118. 'm4a' => array ('type'=>'audio/mp4', 'icon'=>'audio'),
  1119. 'mpeg' => array ('type'=>'video/mpeg', 'icon'=>'video'),
  1120. 'mpe' => array ('type'=>'video/mpeg', 'icon'=>'video'),
  1121. 'mpg' => array ('type'=>'video/mpeg', 'icon'=>'video'),
  1122. 'odt' => array ('type'=>'application/vnd.oasis.opendocument.text', 'icon'=>'odt'),
  1123. 'ott' => array ('type'=>'application/vnd.oasis.opendocument.text-template', 'icon'=>'odt'),
  1124. 'oth' => array ('type'=>'application/vnd.oasis.opendocument.text-web', 'icon'=>'odt'),
  1125. 'odm' => array ('type'=>'application/vnd.oasis.opendocument.text-master', 'i…

Large files files are truncated, but you can click here to view the full file