PageRenderTime 86ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

/moodle/lib/filelib.php

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

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