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

/lib/filelib.php

https://github.com/nfreear/moodle
PHP | 3096 lines | 1979 code | 274 blank | 843 comment | 450 complexity | 2f1d16160006e497f69909fd98b6c9c5 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, BSD-3-Clause

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

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