PageRenderTime 55ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/filelib.php

https://github.com/mylescarrick/moodle
PHP | 3074 lines | 1968 code | 272 blank | 834 comment | 449 complexity | d017a9dcaccc3f0d8603b9011ade1082 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 $CFG, $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. }
  644. $wwwroot = $CFG->wwwroot;
  645. if ($forcehttps) {
  646. $wwwroot = str_replace('http://', 'https://', $wwwroot);
  647. }
  648. // relink embedded files if text submitted - no absolute links allowed in database!
  649. $text = str_ireplace("$wwwroot/draftfile.php/$usercontext->id/user/draft/$draftitemid/", '@@PLUGINFILE@@/', $text);
  650. if (strpos($text, 'draftfile.php?file=') !== false) {
  651. $matches = array();
  652. preg_match_all("!$wwwroot/draftfile.php\?file=%2F{$usercontext->id}%2Fuser%2Fdraft%2F{$draftitemid}%2F[^'\",&<>|`\s:\\\\]+!iu", $text, $matches);
  653. if ($matches) {
  654. foreach ($matches[0] as $match) {
  655. $replace = str_ireplace('%2F', '/', $match);
  656. $text = str_replace($match, $replace, $text);
  657. }
  658. }
  659. $text = str_ireplace("$wwwroot/draftfile.php?file=/$usercontext->id/user/draft/$draftitemid/", '@@PLUGINFILE@@/', $text);
  660. }
  661. return $text;
  662. }
  663. /**
  664. * Set file sort order
  665. * @global object $DB
  666. * @param integer $contextid the context id
  667. * @param string $component
  668. * @param string $filearea file area.
  669. * @param integer $itemid itemid.
  670. * @param string $filepath file path.
  671. * @param string $filename file name.
  672. * @param integer $sortorer the sort order of file.
  673. * @return boolean
  674. */
  675. function file_set_sortorder($contextid, $component, $filearea, $itemid, $filepath, $filename, $sortorder) {
  676. global $DB;
  677. $conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid, 'filepath'=>$filepath, 'filename'=>$filename);
  678. if ($file_record = $DB->get_record('files', $conditions)) {
  679. $sortorder = (int)$sortorder;
  680. $file_record->sortorder = $sortorder;
  681. $DB->update_record('files', $file_record);
  682. return true;
  683. }
  684. return false;
  685. }
  686. /**
  687. * reset file sort order number to 0
  688. * @global object $DB
  689. * @param integer $contextid the context id
  690. * @param string $component
  691. * @param string $filearea file area.
  692. * @param integer $itemid itemid.
  693. * @return boolean
  694. */
  695. function file_reset_sortorder($contextid, $component, $filearea, $itemid=false) {
  696. global $DB;
  697. $conditions = array('contextid'=>$contextid, 'component'=>$component, 'filearea'=>$filearea);
  698. if ($itemid !== false) {
  699. $conditions['itemid'] = $itemid;
  700. }
  701. $file_records = $DB->get_records('files', $conditions);
  702. foreach ($file_records as $file_record) {
  703. $file_record->sortorder = 0;
  704. $DB->update_record('files', $file_record);
  705. }
  706. return true;
  707. }
  708. /**
  709. * Returns description of upload error
  710. *
  711. * @param int $errorcode found in $_FILES['filename.ext']['error']
  712. * @return string error description string, '' if ok
  713. */
  714. function file_get_upload_error($errorcode) {
  715. switch ($errorcode) {
  716. case 0: // UPLOAD_ERR_OK - no error
  717. $errmessage = '';
  718. break;
  719. case 1: // UPLOAD_ERR_INI_SIZE
  720. $errmessage = get_string('uploadserverlimit');
  721. break;
  722. case 2: // UPLOAD_ERR_FORM_SIZE
  723. $errmessage = get_string('uploadformlimit');
  724. break;
  725. case 3: // UPLOAD_ERR_PARTIAL
  726. $errmessage = get_string('uploadpartialfile');
  727. break;
  728. case 4: // UPLOAD_ERR_NO_FILE
  729. $errmessage = get_string('uploadnofilefound');
  730. break;
  731. // Note: there is no error with a value of 5
  732. case 6: // UPLOAD_ERR_NO_TMP_DIR
  733. $errmessage = get_string('uploadnotempdir');
  734. break;
  735. case 7: // UPLOAD_ERR_CANT_WRITE
  736. $errmessage = get_string('uploadcantwrite');
  737. break;
  738. case 8: // UPLOAD_ERR_EXTENSION
  739. $errmessage = get_string('uploadextension');
  740. break;
  741. default:
  742. $errmessage = get_string('uploadproblem');
  743. }
  744. return $errmessage;
  745. }
  746. /**
  747. * Recursive function formating an array in POST parameter
  748. * @param array $arraydata - the array that we are going to format and add into &$data array
  749. * @param string $currentdata - a row of the final postdata array at instant T
  750. * when finish, it's assign to $data under this format: name[keyname][][]...[]='value'
  751. * @param array $data - the final data array containing all POST parameters : 1 row = 1 parameter
  752. */
  753. function format_array_postdata_for_curlcall($arraydata, $currentdata, &$data) {
  754. foreach ($arraydata as $k=>$v) {
  755. $newcurrentdata = $currentdata;
  756. if (is_array($v)) { //the value is an array, call the function recursively
  757. $newcurrentdata = $newcurrentdata.'['.urlencode($k).']';
  758. format_array_postdata_for_curlcall($v, $newcurrentdata, $data);
  759. } else { //add the POST parameter to the $data array
  760. $data[] = $newcurrentdata.'['.urlencode($k).']='.urlencode($v);
  761. }
  762. }
  763. }
  764. /**
  765. * Transform a PHP array into POST parameter
  766. * (see the recursive function format_array_postdata_for_curlcall)
  767. * @param array $postdata
  768. * @return array containing all POST parameters (1 row = 1 POST parameter)
  769. */
  770. function format_postdata_for_curlcall($postdata) {
  771. $data = array();
  772. foreach ($postdata as $k=>$v) {
  773. if (is_array($v)) {
  774. $currentdata = urlencode($k);
  775. format_array_postdata_for_curlcall($v, $currentdata, $data);
  776. } else {
  777. $data[] = urlencode($k).'='.urlencode($v);
  778. }
  779. }
  780. $convertedpostdata = implode('&', $data);
  781. return $convertedpostdata;
  782. }
  783. /**
  784. * Fetches content of file from Internet (using proxy if defined). Uses cURL extension if present.
  785. * Due to security concerns only downloads from http(s) sources are supported.
  786. *
  787. * @global object
  788. * @param string $url file url starting with http(s)://
  789. * @param array $headers http headers, null if none. If set, should be an
  790. * associative array of header name => value pairs.
  791. * @param array $postdata array means use POST request with given parameters
  792. * @param bool $fullresponse return headers, responses, etc in a similar way snoopy does
  793. * (if false, just returns content)
  794. * @param int $timeout timeout for complete download process including all file transfer
  795. * (default 5 minutes)
  796. * @param int $connecttimeout timeout for connection to server; this is the timeout that
  797. * usually happens if the remote server is completely down (default 20 seconds);
  798. * may not work when using proxy
  799. * @param bool $skipcertverify If true, the peer's SSL certificate will not be checked. Only use this when already in a trusted location.
  800. * @param string $tofile store the downloaded content to file instead of returning it
  801. * @return mixed false if request failed or content of the file as string if ok. true if file downloaded into $tofile successfully.
  802. */
  803. function download_file_content($url, $headers=null, $postdata=null, $fullresponse=false, $timeout=300, $connecttimeout=20, $skipcertverify=false, $tofile=NULL) {
  804. global $CFG;
  805. // some extra security
  806. $newlines = array("\r", "\n");
  807. if (is_array($headers) ) {
  808. foreach ($headers as $key => $value) {
  809. $headers[$key] = str_replace($newlines, '', $value);
  810. }
  811. }
  812. $url = str_replace($newlines, '', $url);
  813. if (!preg_match('|^https?://|i', $url)) {
  814. if ($fullresponse) {
  815. $response = new stdClass();
  816. $response->status = 0;
  817. $response->headers = array();
  818. $response->response_code = 'Invalid protocol specified in url';
  819. $response->results = '';
  820. $response->error = 'Invalid protocol specified in url';
  821. return $response;
  822. } else {
  823. return false;
  824. }
  825. }
  826. // check if proxy (if used) should be bypassed for this url
  827. $proxybypass = is_proxybypass($url);
  828. if (!$ch = curl_init($url)) {
  829. debugging('Can not init curl.');
  830. return false;
  831. }
  832. // set extra headers
  833. if (is_array($headers) ) {
  834. $headers2 = array();
  835. foreach ($headers as $key => $value) {
  836. $headers2[] = "$key: $value";
  837. }
  838. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers2);
  839. }
  840. if ($skipcertverify) {
  841. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  842. }
  843. // use POST if requested
  844. if (is_array($postdata)) {
  845. $postdata = format_postdata_for_curlcall($postdata);
  846. curl_setopt($ch, CURLOPT_POST, true);
  847. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  848. }
  849. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  850. curl_setopt($ch, CURLOPT_HEADER, false);
  851. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connecttimeout);
  852. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  853. if (!ini_get('open_basedir') and !ini_get('safe_mode')) {
  854. // TODO: add version test for '7.10.5'
  855. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  856. curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
  857. }
  858. if (!empty($CFG->proxyhost) and !$proxybypass) {
  859. // SOCKS supported in PHP5 only
  860. if (!empty($CFG->proxytype) and ($CFG->proxytype == 'SOCKS5')) {
  861. if (defined('CURLPROXY_SOCKS5')) {
  862. curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
  863. } else {
  864. curl_close($ch);
  865. if ($fullresponse) {
  866. $response = new stdClass();
  867. $response->status = '0';
  868. $response->headers = array();
  869. $response->response_code = 'SOCKS5 proxy is not supported in PHP4';
  870. $response->results = '';
  871. $response->error = 'SOCKS5 proxy is not supported in PHP4';
  872. return $response;
  873. } else {
  874. debugging("SOCKS5 proxy is not supported in PHP4.", DEBUG_ALL);
  875. return false;
  876. }
  877. }
  878. }
  879. curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, false);
  880. if (empty($CFG->proxyport)) {
  881. curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost);
  882. } else {
  883. curl_setopt($ch, CURLOPT_PROXY, $CFG->proxyhost.':'.$CFG->proxyport);
  884. }
  885. if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
  886. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
  887. if (defined('CURLOPT_PROXYAUTH')) {
  888. // any proxy authentication if PHP 5.1
  889. curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
  890. }
  891. }
  892. }
  893. // set up header and content handlers
  894. $received = new stdClass();
  895. $received->headers = array(); // received headers array
  896. $received->tofile = $tofile;
  897. $received->fh = null;
  898. curl_setopt($ch, CURLOPT_HEADERFUNCTION, partial('download_file_content_header_handler', $received));
  899. if ($tofile) {
  900. curl_setopt($ch, CURLOPT_WRITEFUNCTION, partial('download_file_content_write_handler', $received));
  901. }
  902. $result = curl_exec($ch);
  903. // try to detect encoding problems
  904. if ((curl_errno($ch) == 23 or curl_errno($ch) == 61) and defined('CURLOPT_ENCODING')) {
  905. curl_setopt($ch, CURLOPT_ENCODING, 'none');
  906. $result = curl_exec($ch);
  907. }
  908. if ($received->fh) {
  909. fclose($received->fh);
  910. }
  911. if (curl_errno($ch)) {
  912. $error = curl_error($ch);
  913. $error_no = curl_errno($ch);
  914. curl_close($ch);
  915. if ($fullresponse) {
  916. $response = new stdClass();
  917. if ($error_no == 28) {
  918. $response->status = '-100'; // mimic snoopy
  919. } else {
  920. $response->status = '0';
  921. }
  922. $response->headers = array();
  923. $response->response_code = $error;
  924. $response->results = false;
  925. $response->error = $error;
  926. return $response;
  927. } else {
  928. debugging("cURL request for \"$url\" failed with: $error ($error_no)", DEBUG_ALL);
  929. return false;
  930. }
  931. } else {
  932. $info = curl_getinfo($ch);
  933. curl_close($ch);
  934. if (empty($info['http_code'])) {
  935. // for security reasons we support only true http connections (Location: file:// exploit prevention)
  936. $response = new stdClass();
  937. $response->status = '0';
  938. $response->headers = array();
  939. $response->response_code = 'Unknown cURL error';
  940. $response->results = false; // do NOT change this, we really want to ignore the result!
  941. $response->error = 'Unknown cURL error';
  942. } else {
  943. $response = new stdClass();;
  944. $response->status = (string)$info['http_code'];
  945. $response->headers = $received->headers;
  946. $response->response_code = $received->headers[0];
  947. $response->results = $result;
  948. $response->error = '';
  949. }
  950. if ($fullresponse) {
  951. return $response;
  952. } else if ($info['http_code'] != 200) {
  953. debugging("cURL request for \"$url\" failed, HTTP response code: ".$response->response_code, DEBUG_ALL);
  954. return false;
  955. } else {
  956. return $response->results;
  957. }
  958. }
  959. }
  960. /**
  961. * internal implementation
  962. */
  963. function download_file_content_header_handler($received, $ch, $header) {
  964. $received->headers[] = $header;
  965. return strlen($header);
  966. }
  967. /**
  968. * internal implementation
  969. */
  970. function download_file_content_write_handler($received, $ch, $data) {
  971. if (!$received->fh) {
  972. $received->fh = fopen($received->tofile, 'w');
  973. if ($received->fh === false) {
  974. // bad luck, file creation or overriding failed
  975. return 0;
  976. }
  977. }
  978. if (fwrite($received->fh, $data) === false) {
  979. // bad luck, write failed, let's abort completely
  980. return 0;
  981. }
  982. return strlen($data);
  983. }
  984. /**
  985. * @return array List of information about file types based on extensions.
  986. * Associative array of extension (lower-case) to associative array
  987. * from 'element name' to data. Current element names are 'type' and 'icon'.
  988. * Unknown types should use the 'xxx' entry which includes defaults.
  989. */
  990. function get_mimetypes_array() {
  991. static $mimearray = array (
  992. 'xxx' => array ('type'=>'document/unknown', 'icon'=>'unknown'),
  993. '3gp' => array ('type'=>'video/quicktime', 'icon'=>'video'),
  994. 'ai' => array ('type'=>'application/postscript', 'icon'=>'image'),
  995. 'aif' => array ('type'=>'audio/x-aiff', 'icon'=>'audio'),
  996. 'aiff' => array ('type'=>'audio/x-aiff', 'icon'=>'audio'),
  997. 'aifc' => array ('type'=>'audio/x-aiff', 'icon'=>'audio'),
  998. 'applescript' => array ('type'=>'text/plain', 'icon'=>'text'),
  999. 'asc' => array ('type'=>'text/plain', 'icon'=>'text'),
  1000. 'asm' => array ('type'=>'text/plain', 'icon'=>'text'),
  1001. 'au' => array ('type'=>'audio/au', 'icon'=>'audio'),
  1002. 'avi' => array ('type'=>'video/x-ms-wm', 'icon'=>'avi'),
  1003. 'bmp' => array ('type'=>'image/bmp', 'icon'=>'image'),
  1004. 'c' => array ('type'=>'text/plain', 'icon'=>'text'),
  1005. 'cct' => array ('type'=>'shockwave/director', 'icon'=>'flash'),
  1006. 'cpp' => array ('type'=>'text/plain', 'icon'=>'text'),
  1007. 'cs' => array ('type'=>'application/x-csh', 'icon'=>'text'),
  1008. 'css' => array ('type'=>'text/css', 'icon'=>'text'),
  1009. 'csv' => array ('type'=>'text/csv', 'icon'=>'excel'),
  1010. 'dv' => array ('type'=>'video/x-dv', 'icon'=>'video'),
  1011. 'dmg' => array ('type'=>'application/octet-stream', 'icon'=>'dmg'),
  1012. 'doc' => array ('type'=>'application/msword', 'icon'=>'word'),
  1013. 'docx' => array ('type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'icon'=>'docx'),
  1014. 'docm' => array ('type'=>'application/vnd.ms-word.document.macroEnabled.12', 'icon'=>'docm'),
  1015. 'dotx' => array ('type'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'icon'=>'dotx'),
  1016. 'dotm' => array ('type'=>'application/vnd.ms-word.template.macroEnabled.12', 'icon'=>'dotm'),
  1017. 'dcr' => array ('type'=>'application/x-director', 'icon'=>'flash'),
  1018. 'dif' => array ('type'=>'video/x-dv', 'icon'=>'video'),
  1019. 'dir' => array ('type'=>'application/x-director', 'icon'=>'flash'),
  1020. 'dxr' => array ('type'=>'application/x-director', 'icon'=>'flash'),
  1021. 'eps' => array ('type'=>'application/postscript', 'icon'=>'pdf'),
  1022. 'fdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
  1023. 'flv' => array ('type'=>'video/x-flv', 'icon'=>'video'),
  1024. 'gif' => array ('type'=>'image/gif', 'icon'=>'image'),
  1025. 'gtar' => array ('type'=>'application/x-gtar', 'icon'=>'zip'),
  1026. 'tgz' => array ('type'=>'application/g-zip', 'icon'=>'zip'),
  1027. 'gz' => array ('type'=>'application/g-zip', 'icon'=>'zip'),
  1028. 'gzip' => array ('type'=>'application/g-zip', 'icon'=>'zip'),
  1029. 'h' => array ('type'=>'text/plain', 'icon'=>'text'),
  1030. 'hpp' => array ('type'=>'text/plain', 'icon'=>'text'),
  1031. 'hqx' => array ('type'=>'application/mac-binhex40', 'icon'=>'zip'),
  1032. 'htc' => array ('type'=>'text/x-component', 'icon'=>'text'),
  1033. 'html' => array ('type'=>'text/html', 'icon'=>'html'),
  1034. 'xhtml'=> array ('type'=>'application/xhtml+xml', 'icon'=>'html'),
  1035. 'htm' => array ('type'=>'text/html', 'icon'=>'html'),
  1036. 'ico' => array ('type'=>'image/vnd.microsoft.icon', 'icon'=>'image'),
  1037. 'ics' => array ('type'=>'text/calendar', 'icon'=>'text'),
  1038. 'isf' => array ('type'=>'application/inspiration', 'icon'=>'isf'),
  1039. 'ist' => array ('type'=>'application/inspiration.template', 'icon'=>'isf'),
  1040. 'java' => array ('type'=>'text/plain', 'icon'=>'text'),
  1041. 'jcb' => array ('type'=>'text/xml', 'icon'=>'jcb'),
  1042. 'jcl' => array ('type'=>'text/xml', 'icon'=>'jcl'),
  1043. 'jcw' => array ('type'=>'text/xml', 'icon'=>'jcw'),
  1044. 'jmt' => array ('type'=>'text/xml', 'icon'=>'jmt'),
  1045. 'jmx' => array ('type'=>'text/xml', 'icon'=>'jmx'),
  1046. 'jpe' => array ('type'=>'image/jpeg', 'icon'=>'image'),
  1047. 'jpeg' => array ('type'=>'image/jpeg', 'icon'=>'image'),
  1048. 'jpg' => array ('type'=>'image/jpeg', 'icon'=>'image'),
  1049. 'jqz' => array ('type'=>'text/xml', 'icon'=>'jqz'),
  1050. 'js' => array ('type'=>'application/x-javascript', 'icon'=>'text'),
  1051. 'latex'=> array ('type'=>'application/x-latex', 'icon'=>'text'),
  1052. 'm' => array ('type'=>'text/plain', 'icon'=>'text'),
  1053. 'mbz' => array ('type'=>'application/vnd.moodle.backup', 'icon'=>'moodle'),
  1054. 'mov' => array ('type'=>'video/quicktime', 'icon'=>'video'),
  1055. 'movie'=> array ('type'=>'video/x-sgi-movie', 'icon'=>'video'),
  1056. 'm3u' => array ('type'=>'audio/x-mpegurl', 'icon'=>'audio'),
  1057. 'mp3' => array ('type'=>'audio/mp3', 'icon'=>'audio'),
  1058. 'mp4' => array ('type'=>'video/mp4', 'icon'=>'video'),
  1059. 'm4v' => array ('type'=>'video/mp4', 'icon'=>'video'),
  1060. 'm4a' => array ('type'=>'audio/mp4', 'icon'=>'audio'),
  1061. 'mpeg' => array ('type'=>'video/mpeg', 'icon'=>'video'),
  1062. 'mpe' => array ('type'=>'video/mpeg', 'icon'=>'video'),
  1063. 'mpg' => array ('type'=>'video/mpeg', 'icon'=>'video'),
  1064. 'odt' => array ('type'=>'application/vnd.oasis.opendocument.text', 'icon'=>'odt'),
  1065. 'ott' => array ('type'=>'application/vnd.oasis.opendocument.text-template', 'icon'=>'odt'),
  1066. 'oth' => array ('type'=>'application/vnd.oasis.opendocument.text-web', 'icon'=>'odt'),
  1067. 'odm' => array ('type'=>'application/vnd.oasis.opendocument.text-master', 'icon'=>'odm'),
  1068. 'odg' => array ('type'=>'application/vnd.oasis.opendocument.graphics', 'icon'=>'odg'),
  1069. 'otg' => array ('type'=>'application/vnd.oasis.opendocument.graphics-template', 'icon'=>'odg'),
  1070. 'odp' => array ('type'=>'application/vnd.oasis.opendocument.presentation', 'icon'=>'odp'),
  1071. 'otp' => array ('type'=>'application/vnd.oasis.opendocument.presentation-template', 'icon'=>'odp'),
  1072. 'ods' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet', 'icon'=>'ods'),
  1073. 'ots' => array ('type'=>'application/vnd.oasis.opendocument.spreadsheet-template', 'icon'=>'ods'),
  1074. 'odc' => array ('type'=>'application/vnd.oasis.opendocument.chart', 'icon'=>'odc'),
  1075. 'odf' => array ('type'=>'application/vnd.oasis.opendocument.formula', 'icon'=>'odf'),
  1076. 'odb' => array ('type'=>'application/vnd.oasis.opendocument.database', 'icon'=>'odb'),
  1077. 'odi' => array ('type'=>'application/vnd.oasis.opendocument.image', 'icon'=>'odi'),
  1078. 'ogg' => array ('type'=>'audio/ogg', 'icon'=>'audio'),
  1079. 'ogv' => array ('type'=>'video/ogg', 'icon'=>'video'),
  1080. 'pct' => array ('type'=>'image/pict', 'icon'=>'image'),
  1081. 'pdf' => array ('type'=>'application/pdf', 'icon'=>'pdf'),
  1082. 'php' => array ('type'=>'text/plain', 'icon'=>'text'),
  1083. 'pic' => array ('type'=>'image/pict', 'icon'=>'image'),
  1084. 'pict' => array ('type'=>'image/pict', 'icon'=>'image'),
  1085. 'png' => array ('type'=>'image/png', 'icon'=>'image'),
  1086. 'pps' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint'),
  1087. 'ppt' => array ('type'=>'application/vnd.ms-powerpoint', 'icon'=>'powerpoint'),
  1088. 'pptx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'icon'=>'pptx'),
  1089. 'pptm' => array ('type'=>'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'icon'=>'pptm'),
  1090. 'potx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.template', 'icon'=>'potx'),
  1091. 'potm' => array ('type'=>'application/vnd.ms-powerpoint.template.macroEnabled.12', 'icon'=>'potm'),
  1092. 'ppam' => array ('type'=>'application/vnd.ms-powerpoint.addin.macroEnabled.12', 'icon'=>'ppam'),
  1093. 'ppsx' => array ('type'=>'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'icon'=>'ppsx'),
  1094. 'ppsm' => array ('type'=>'application/vnd.ms-powerpoint.slideshow.macroEnabled.12', 'icon'=>'ppsm'),
  1095. 'ps' => array ('type'=>'application/postscript', 'icon'=>'pdf'),
  1096. 'qt' => array ('type'=>'video/quicktime', 'icon'=>'video'),
  1097. 'ra' => array ('type'=>'audio/x-realaudio-plugin', 'icon'=>'audio'),
  1098. 'ram' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio'),
  1099. 'rhb' => array ('type'=>'text/xml', 'icon'=>'xml'),
  1100. 'rm' => array ('type'=>'audio/x-pn-realaudio-plugin', 'icon'=>'audio'),
  1101. 'rtf' => array ('type'=>'text/rtf', 'icon'=>'text'),
  1102. 'rtx' => array ('type'=>'text/richtext', 'icon'=>'te…

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