PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/resourcelib.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 281 lines | 144 code | 42 blank | 95 comment | 27 complexity | 5f2f03c1964062eda9d13629843994af MD5 | raw 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. * Recourse module like helper functions
  18. *
  19. * @package core
  20. * @subpackage lib
  21. * @copyright 2009 Petr Skoda (http://skodak.org)
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. /** Try the best way */
  26. define('RESOURCELIB_DISPLAY_AUTO', 0);
  27. /** Display using object tag */
  28. define('RESOURCELIB_DISPLAY_EMBED', 1);
  29. /** Display inside frame */
  30. define('RESOURCELIB_DISPLAY_FRAME', 2);
  31. /** Display normal link in new window */
  32. define('RESOURCELIB_DISPLAY_NEW', 3);
  33. /** Force download of file instead of display */
  34. define('RESOURCELIB_DISPLAY_DOWNLOAD', 4);
  35. /** Open directly */
  36. define('RESOURCELIB_DISPLAY_OPEN', 5);
  37. /** Open in "emulated" pop-up without navigation */
  38. define('RESOURCELIB_DISPLAY_POPUP', 6);
  39. /** Legacy files not needed or new resource */
  40. define('RESOURCELIB_LEGACYFILES_NO', 0);
  41. /** Legacy files conversion marked as completed */
  42. define('RESOURCELIB_LEGACYFILES_DONE', 1);
  43. /** Legacy files conversion in progress*/
  44. define('RESOURCELIB_LEGACYFILES_ACTIVE', 2);
  45. /**
  46. * Try on demand migration of file from old course files
  47. * @param string $filepath old file path
  48. * @param int $cmid migrated course module if
  49. * @param int $courseid
  50. * @param string $component
  51. * @param string $filearea new file area
  52. * @param int $itemid migrated file item id
  53. * @return mixed, false if not found, stored_file instance if migrated to new area
  54. */
  55. function resourcelib_try_file_migration($filepath, $cmid, $courseid, $component, $filearea, $itemid) {
  56. $fs = get_file_storage();
  57. if (stripos($filepath, '/backupdata/') === 0 or stripos($filepath, '/moddata/') === 0) {
  58. // do not steal protected files!
  59. return false;
  60. }
  61. if (!$context = context_module::instance($cmid)) {
  62. return false;
  63. }
  64. if (!$coursecontext = context_course::instance($courseid)) {
  65. return false;
  66. }
  67. $fullpath = rtrim("/$coursecontext->id/course/legacy/0".$filepath, '/');
  68. do {
  69. if (!$file = $fs->get_file_by_hash(sha1($fullpath))) {
  70. if ($file = $fs->get_file_by_hash(sha1("$fullpath/.")) and $file->is_directory()) {
  71. if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.htm"))) {
  72. break;
  73. }
  74. if ($file = $fs->get_file_by_hash(sha1("$fullpath/index.html"))) {
  75. break;
  76. }
  77. if ($file = $fs->get_file_by_hash(sha1("$fullpath/Default.htm"))) {
  78. break;
  79. }
  80. }
  81. return false;
  82. }
  83. } while (false);
  84. // copy and keep the same path, name, etc.
  85. $file_record = array('contextid'=>$context->id, 'component'=>$component, 'filearea'=>$filearea, 'itemid'=>$itemid);
  86. try {
  87. return $fs->create_file_from_storedfile($file_record, $file);
  88. } catch (Exception $e) {
  89. // file may exist - highly unlikely, we do not want upgrades to stop here
  90. return false;
  91. }
  92. }
  93. /**
  94. * Returns list of available display options
  95. * @param array $enabled list of options enabled in module configuration
  96. * @param int $current current display options for existing instances
  97. * @return array of key=>name pairs
  98. */
  99. function resourcelib_get_displayoptions(array $enabled, $current=null) {
  100. if (is_number($current)) {
  101. $enabled[] = $current;
  102. }
  103. $options = array(RESOURCELIB_DISPLAY_AUTO => get_string('resourcedisplayauto'),
  104. RESOURCELIB_DISPLAY_EMBED => get_string('resourcedisplayembed'),
  105. RESOURCELIB_DISPLAY_FRAME => get_string('resourcedisplayframe'),
  106. RESOURCELIB_DISPLAY_NEW => get_string('resourcedisplaynew'),
  107. RESOURCELIB_DISPLAY_DOWNLOAD => get_string('resourcedisplaydownload'),
  108. RESOURCELIB_DISPLAY_OPEN => get_string('resourcedisplayopen'),
  109. RESOURCELIB_DISPLAY_POPUP => get_string('resourcedisplaypopup'));
  110. $result = array();
  111. foreach ($options as $key=>$value) {
  112. if (in_array($key, $enabled)) {
  113. $result[$key] = $value;
  114. }
  115. }
  116. if (empty($result)) {
  117. // there should be always something in case admin misconfigures module
  118. $result[RESOURCELIB_DISPLAY_OPEN] = $options[RESOURCELIB_DISPLAY_OPEN];
  119. }
  120. return $result;
  121. }
  122. /**
  123. * Tries to guess correct mimetype for arbitrary URL
  124. * @param string $fullurl
  125. * @return string mimetype
  126. */
  127. function resourcelib_guess_url_mimetype($fullurl) {
  128. global $CFG;
  129. require_once("$CFG->libdir/filelib.php");
  130. if ($fullurl instanceof moodle_url) {
  131. $fullurl = $fullurl->out(false);
  132. }
  133. $matches = null;
  134. if (preg_match("|^(.*)/[a-z]*file.php(\?file=)?(/[^&\?#]*)|", $fullurl, $matches)) {
  135. // remove the special moodle file serving hacks so that the *file.php is ignored
  136. $fullurl = $matches[1].$matches[3];
  137. }
  138. if (preg_match("|^(.*)#.*|", $fullurl, $matches)) {
  139. // ignore all anchors
  140. $fullurl = $matches[1];
  141. }
  142. if (strpos($fullurl, '.php')){
  143. // we do not really know what is in general php script
  144. return 'text/html';
  145. } else if (substr($fullurl, -1) === '/') {
  146. // directory index (http://example.com/smaples/)
  147. return 'text/html';
  148. } else if (strpos($fullurl, '//') !== false and substr_count($fullurl, '/') == 2) {
  149. // just a host name (http://example.com), solves Australian servers "audio" problem too
  150. return 'text/html';
  151. } else {
  152. // ok, this finally looks like a real file
  153. $parts = explode('?', $fullurl);
  154. $url = reset($parts);
  155. return mimeinfo('type', $url);
  156. }
  157. }
  158. /**
  159. * Looks for the extension.
  160. *
  161. * @param string $fullurl
  162. * @return string file extension
  163. */
  164. function resourcelib_get_extension($fullurl) {
  165. if ($fullurl instanceof moodle_url) {
  166. $fullurl = $fullurl->out(false);
  167. }
  168. $matches = null;
  169. if (preg_match("|^(.*)/[a-z]*file.php(\?file=)?(/.*)|", $fullurl, $matches)) {
  170. // remove the special moodle file serving hacks so that the *file.php is ignored
  171. $fullurl = $matches[1].$matches[3];
  172. }
  173. $matches = null;
  174. if (preg_match('/^[^#\?]+\.([a-z0-9]+)([#\?].*)?$/i', $fullurl, $matches)) {
  175. return strtolower($matches[1]);
  176. }
  177. return '';
  178. }
  179. /**
  180. * Returns image embedding html.
  181. * @param string $fullurl
  182. * @param string $title
  183. * @return string html
  184. */
  185. function resourcelib_embed_image($fullurl, $title) {
  186. $code = '';
  187. $code .= '<div class="resourcecontent resourceimg">';
  188. $code .= "<img title=\"".strip_tags(format_string($title))."\" class=\"resourceimage\" src=\"$fullurl\" alt=\"\" />";
  189. $code .= '</div>';
  190. return $code;
  191. }
  192. /**
  193. * Returns general link or pdf embedding html.
  194. * @param string $fullurl
  195. * @param string $title
  196. * @param string $clicktoopen
  197. * @return string html
  198. */
  199. function resourcelib_embed_pdf($fullurl, $title, $clicktoopen) {
  200. global $CFG, $PAGE;
  201. $code = <<<EOT
  202. <div class="resourcecontent resourcepdf">
  203. <object id="resourceobject" data="$fullurl" type="application/pdf" width="800" height="600">
  204. <param name="src" value="$fullurl" />
  205. $clicktoopen
  206. </object>
  207. </div>
  208. EOT;
  209. // the size is hardcoded in the boject obove intentionally because it is adjusted by the following function on-the-fly
  210. $PAGE->requires->js_init_call('M.util.init_maximised_embed', array('resourceobject'), true);
  211. return $code;
  212. }
  213. /**
  214. * Returns general link or file embedding html.
  215. * @param string $fullurl
  216. * @param string $title
  217. * @param string $clicktoopen
  218. * @param string $mimetype
  219. * @return string html
  220. */
  221. function resourcelib_embed_general($fullurl, $title, $clicktoopen, $mimetype) {
  222. global $CFG, $PAGE;
  223. if ($fullurl instanceof moodle_url) {
  224. $fullurl = $fullurl->out();
  225. }
  226. $param = '<param name="src" value="'.$fullurl.'" />';
  227. // Always use iframe embedding because object tag does not work much,
  228. // this is ok in HTML5.
  229. $code = <<<EOT
  230. <div class="resourcecontent resourcegeneral">
  231. <iframe id="resourceobject" src="$fullurl">
  232. $clicktoopen
  233. </iframe>
  234. </div>
  235. EOT;
  236. // the size is hardcoded in the boject obove intentionally because it is adjusted by the following function on-the-fly
  237. $PAGE->requires->js_init_call('M.util.init_maximised_embed', array('resourceobject'), true);
  238. return $code;
  239. }