document.inc.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * EXTRA FUNCTIONS FOR DOCUMENTS TOOL
  5. * @package chamilo.document
  6. */
  7. /**
  8. * Builds the form thats enables the user to
  9. * select a directory to browse/upload in
  10. *
  11. * @param array An array containing the folders we want to be able to select
  12. * @param string The current folder (path inside of the "document" directory, including the prefix "/")
  13. * @param string Group directory, if empty, prevents documents to be uploaded (because group documents cannot be uploaded in root)
  14. * @param boolean Whether to change the renderer (this will add a template <span> to the QuickForm object displaying the form)
  15. * @todo this funcionality is really bad : jmontoya
  16. * @return string html form
  17. */
  18. function build_directory_selector($folders, $document_id, $group_dir = '', $change_renderer = false)
  19. {
  20. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  21. $course_id = api_get_course_int_id();
  22. $folder_titles = array();
  23. if (is_array($folders)) {
  24. $escaped_folders = array();
  25. foreach ($folders as $key => & $val) {
  26. $escaped_folders[$key] = Database::escape_string($val);
  27. }
  28. $folder_sql = implode("','", $escaped_folders);
  29. $sql = "SELECT * FROM $doc_table WHERE filetype = 'folder' AND c_id = $course_id AND path IN ('".$folder_sql."')";
  30. $res = Database::query($sql);
  31. $folder_titles = array();
  32. while ($obj = Database::fetch_object($res)) {
  33. $folder_titles[$obj->path] = $obj->title;
  34. }
  35. }
  36. $form = new FormValidator('selector', 'GET', api_get_self().'?'.api_get_cidreq());
  37. $form->addElement('hidden', 'cidReq', api_get_course_id());
  38. $parent_select = $form->addElement('select', 'id', get_lang('CurrentDirectory'), '', 'onchange="javascript: document.selector.submit();"');
  39. if ($change_renderer) {
  40. $renderer = $form->defaultRenderer();
  41. $renderer->setElementTemplate('<span>{label} : {element}</span> ', 'curdirpath');
  42. }
  43. // Group documents cannot be uploaded in the root
  44. if (empty($group_dir)) {
  45. $parent_select->addOption(get_lang('Documents'), '/');
  46. if (is_array($folders)) {
  47. foreach ($folders as $folder_id => & $folder) {
  48. $selected = ($document_id == $folder_id) ? ' selected="selected"' : '';
  49. $path_parts = explode('/', $folder);
  50. $folder_titles[$folder] = cut($folder_titles[$folder], 80);
  51. $counter = count($path_parts) - 2;
  52. if ($counter > 0) {
  53. $label = str_repeat('&nbsp;&nbsp;&nbsp;', $counter).' &mdash; '.$folder_titles[$folder];
  54. } else {
  55. $label = ' &mdash; '.$folder_titles[$folder];
  56. }
  57. $parent_select->addOption($label, $folder_id);
  58. if ($selected != '') {
  59. $parent_select->setSelected($folder_id);
  60. }
  61. }
  62. }
  63. } else {
  64. if (!empty($folders)) {
  65. foreach ($folders as $folder_id => & $folder) {
  66. $selected = ($document_id == $folder_id) ? ' selected="selected"' : '';
  67. $label = $folder_titles[$folder];
  68. if ($folder == $group_dir) {
  69. $label = get_lang('Documents');
  70. } else {
  71. $path_parts = explode('/', str_replace($group_dir, '', $folder));
  72. $label = cut($label, 80);
  73. $label = str_repeat('&nbsp;&nbsp;&nbsp;', count($path_parts) - 2).' &mdash; '.$label;
  74. }
  75. $parent_select->addOption($label, $folder_id);
  76. if ($selected != '') {
  77. $parent_select->setSelected($folder_id);
  78. }
  79. }
  80. }
  81. }
  82. $html = $form->toHtml();
  83. return $html;
  84. }
  85. /**
  86. * Create a html hyperlink depending on if it's a folder or a file
  87. *
  88. * @param string $www
  89. * @param string $title
  90. * @param string $path
  91. * @param string $filetype (file/folder)
  92. * @param int $visibility (1/0)
  93. * @param int $show_as_icon - if it is true, only a clickable icon will be shown
  94. * @return string url
  95. */
  96. function create_document_link($document_data, $show_as_icon = false, $counter = null, $visibility)
  97. {
  98. global $dbl_click_id;
  99. if (isset($_SESSION['_gid'])) {
  100. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  101. } else {
  102. $req_gid = '';
  103. }
  104. $course_info = api_get_course_info();
  105. $www = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document';
  106. // Get the title or the basename depending on what we're using
  107. if ($document_data['title'] != '') {
  108. $title = $document_data['title'];
  109. } else {
  110. $title = basename($document_data['path']);
  111. }
  112. $filetype = $document_data['filetype'];
  113. $size = $filetype == 'folder' ? get_total_folder_size($document_data['path'], api_is_allowed_to_edit(null, true)) : $document_data['size'];
  114. $path = $document_data['path'];
  115. $url_path = urlencode($document_data['path']);
  116. // Add class="invisible" on invisible files
  117. $visibility_class = ($visibility == false) ? ' class="muted"' : '';
  118. if (!$show_as_icon) {
  119. // Build download link (icon)
  120. $forcedownload_link = ($filetype == 'folder') ? api_get_self().'?'.api_get_cidreq().'&action=downloadfolder&id='.$document_data['id'] : api_get_self().'?'.api_get_cidreq().'&amp;action=download&amp;id='.$document_data['id'];
  121. // Folder download or file download?
  122. $forcedownload_icon = ($filetype == 'folder') ? 'save_pack.png' : 'save.png';
  123. // Prevent multiple clicks on zipped folder download
  124. $prevent_multiple_click = ($filetype == 'folder') ? " onclick=\"javascript: if(typeof clic_$dbl_click_id == 'undefined' || !clic_$dbl_click_id) { clic_$dbl_click_id=true; window.setTimeout('clic_".($dbl_click_id++)."=false;',10000); } else { return false; }\"" : '';
  125. }
  126. $target = '_self';
  127. $is_browser_viewable_file = false;
  128. if ($filetype == 'file') {
  129. // Check the extension
  130. $ext = explode('.', $path);
  131. $ext = strtolower($ext[sizeof($ext) - 1]);
  132. // HTML-files an some other types are shown in a frameset by default.
  133. $is_browser_viewable_file = is_browser_viewable($ext);
  134. if ($is_browser_viewable_file) {
  135. //$url = 'showinframes.php?'.api_get_cidreq().'&amp;file='.$url_path.$req_gid;
  136. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  137. } else {
  138. // url-encode for problematic characters (we may not call them dangerous characters...)
  139. $path = str_replace('%2F', '/', $url_path).'?'.api_get_cidreq();
  140. //$new_path = '?id='.$document_data['id'];
  141. $url = $www.$path;
  142. }
  143. //$path = str_replace('%2F', '/',$url_path).'?'.api_get_cidreq();
  144. $path = str_replace('%2F', '/', $url_path); //yox view hack otherwise the image can't be well read
  145. $url = $www.$path;
  146. // Disabled fragment of code, there is a special icon for opening in a new window.
  147. //// Files that we want opened in a new window
  148. //if ($ext == 'txt' || $ext == 'log' || $ext == 'css' || $ext == 'js') { // Add here
  149. // $target = '_blank';
  150. //}
  151. } else {
  152. //$url = api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$url_path.$req_gid;
  153. $url = api_get_self().'?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  154. }
  155. // The little download icon
  156. //$tooltip_title = str_replace('?cidReq='.$_GET['cidReq'], '', basename($path));
  157. $tooltip_title = explode('?', basename($path));
  158. $tooltip_title = $title;
  159. //Cut long titles
  160. //$title = cut($title, 120);
  161. $tooltip_title_alt = $tooltip_title;
  162. if ($path == '/shared_folder') {
  163. $tooltip_title_alt = get_lang('UserFolders');
  164. } elseif (strstr($path, 'shared_folder_session_')) {
  165. $tooltip_title_alt = get_lang('UserFolders').' ('.api_get_session_name(api_get_session_id()).')';
  166. } elseif (strstr($tooltip_title, 'sf_user_')) {
  167. $userinfo = Database::get_user_info_from_id(substr($tooltip_title, 8));
  168. $tooltip_title_alt = get_lang('UserFolder').' '.api_get_person_name($userinfo['firstname'], $userinfo['lastname']);
  169. } elseif ($path == '/chat_files') {
  170. $tooltip_title_alt = get_lang('ChatFiles');
  171. } elseif ($path == '/learning_path') {
  172. $tooltip_title_alt = get_lang('LearningPaths');
  173. } elseif ($path == '/video') {
  174. $tooltip_title_alt = get_lang('Video');
  175. } elseif ($path == '/audio') {
  176. $tooltip_title_alt = get_lang('Audio');
  177. } elseif ($path == '/flash') {
  178. $tooltip_title_alt = get_lang('Flash');
  179. } elseif ($path == '/images') {
  180. $tooltip_title_alt = get_lang('Images');
  181. } elseif ($path == '/images/gallery') {
  182. $tooltip_title_alt = get_lang('DefaultCourseImages');
  183. }
  184. $current_session_id = api_get_session_id();
  185. $copy_to_myfiles = $open_in_new_window_link = null;
  186. $curdirpath = isset($_GET['curdirpath']) ? Security::remove_XSS($_GET['curdirpath']) : null;
  187. if (!$show_as_icon) {
  188. if ($filetype == 'folder') {
  189. if (api_is_allowed_to_edit() || api_is_platform_admin() || api_get_setting('students_download_folders') == 'true') {
  190. //filter when I am into shared folder, I can show for donwload only my shared folder
  191. if (is_shared_folder($curdirpath, $current_session_id)) {
  192. if (preg_match('/shared_folder\/sf_user_'.api_get_user_id().'$/', urldecode($forcedownload_link)) || preg_match('/shared_folder_session_'.$current_session_id.'\/sf_user_'.api_get_user_id().'$/', urldecode($forcedownload_link)) || api_is_allowed_to_edit() || api_is_platform_admin()) {
  193. $force_download_html = ($size == 0) ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.Display::return_icon($forcedownload_icon, get_lang('Download'), array(), ICON_SIZE_SMALL).'</a>';
  194. }
  195. } elseif (!preg_match('/shared_folder/', urldecode($forcedownload_link)) || api_is_allowed_to_edit() || api_is_platform_admin()) {
  196. $force_download_html = ($size == 0) ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.Display::return_icon($forcedownload_icon, get_lang('Download'), array(), ICON_SIZE_SMALL).'</a>';
  197. }
  198. }
  199. } else {
  200. $force_download_html = ($size == 0) ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.Display::return_icon($forcedownload_icon, get_lang('Download'), array(), ICON_SIZE_SMALL).'</a>';
  201. }
  202. //Copy files to users myfiles
  203. if (api_get_setting('allow_social_tool') == 'true' && api_get_setting('users_copy_files') == 'true' && !api_is_anonymous()) {
  204. $copy_myfiles_link = ($filetype == 'file') ? api_get_self().'?'.api_get_cidreq().'&action=copytomyfiles&id='.$document_data['id'].$req_gid : api_get_self().'?'.api_get_cidreq();
  205. if ($filetype == 'file') {
  206. $copy_to_myfiles = '<a href="'.$copy_myfiles_link.'" style="float:right"'.$prevent_multiple_click.'>'.Display::return_icon('briefcase.png', get_lang('CopyToMyFiles'), array(), ICON_SIZE_SMALL).'&nbsp;&nbsp;</a>';
  207. }
  208. $send_to = '';
  209. if ($filetype == 'file') {
  210. $send_to = Portfolio::share('document', $document_data['id'], array('style' => 'float:right;'));
  211. }
  212. }
  213. $pdf_icon = '';
  214. $extension = pathinfo($path, PATHINFO_EXTENSION);
  215. if (!api_is_allowed_to_edit() && api_get_setting('students_export2pdf') == 'true' && $filetype == 'file' && in_array($extension, array('html', 'htm'))) {
  216. $pdf_icon = ' <a style="float:right".'.$prevent_multiple_click.' href="'.api_get_self().'?'.api_get_cidreq().'&action=export_to_pdf&id='.$document_data['id'].'">'.Display::return_icon('pdf.png', get_lang('Export2PDF'), array(), ICON_SIZE_SMALL).'</a> ';
  217. }
  218. if ($is_browser_viewable_file) {
  219. $open_in_new_window_link = '<a href="'.$www.str_replace('%2F', '/', $url_path).'?'.api_get_cidreq().'" style="float:right"'.$prevent_multiple_click.' target="_blank">'.Display::return_icon('open_in_new_window.png', get_lang('OpenInANewWindow'), array(), ICON_SIZE_SMALL).'&nbsp;&nbsp;</a>';
  220. }
  221. //target="'.$target.'"
  222. if ($filetype == 'file') {
  223. //Sound preview with jplayer
  224. if (preg_match('/mp3$/i', urldecode($url)) ||
  225. (preg_match('/wav$/i', urldecode($url)) && !preg_match('/_chnano_.wav$/i', urldecode($url))) ||
  226. preg_match('/ogg$/i', urldecode($url))) {
  227. return '<span style="float:left" '.$visibility_class.'>'.$title.'</span>'.$force_download_html.$send_to.$copy_to_myfiles.$open_in_new_window_link.$pdf_icon;
  228. } elseif (
  229. //Show preview
  230. //preg_match('/html$/i', urldecode($url)) ||
  231. //preg_match('/htm$/i', urldecode($url)) ||
  232. preg_match('/swf$/i', urldecode($url)) ||
  233. preg_match('/png$/i', urldecode($url)) ||
  234. preg_match('/gif$/i', urldecode($url)) ||
  235. preg_match('/jpg$/i', urldecode($url)) ||
  236. preg_match('/jpeg$/i', urldecode($url)) ||
  237. preg_match('/bmp$/i', urldecode($url)) ||
  238. preg_match('/svg$/i', urldecode($url)) ||
  239. (preg_match('/wav$/i', urldecode($url)) && preg_match('/_chnano_.wav$/i', urldecode($url)) && api_get_setting('enable_nanogong') == 'true')
  240. ) {
  241. //yox view
  242. //$url = 'showinframesmin.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  243. //Simpler version of showinframesmin.php with no headers
  244. $url = 'show_content.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid.'&width=700&height=500';
  245. $class = 'ajax';
  246. if ($visibility == false) {
  247. $class = "ajax invisible";
  248. }
  249. return '<a href="'.$url.'" class="'.$class.'" title="'.$tooltip_title_alt.'" style="float:left">'.$title.'</a>'.$force_download_html.$send_to.$copy_to_myfiles.$open_in_new_window_link.$pdf_icon;
  250. } else {
  251. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  252. //No plugin just the old and good showinframes.php page
  253. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" style="float:left" '.$visibility_class.' >'.$title.'</a>'.$force_download_html.$send_to.$copy_to_myfiles.$open_in_new_window_link.$pdf_icon;
  254. }
  255. } else {
  256. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.$title.'</a>'.$force_download_html.$send_to.$copy_to_myfiles.$open_in_new_window_link.$pdf_icon;
  257. }
  258. //end copy files to users myfiles
  259. } else {
  260. //Icon column
  261. if (preg_match('/shared_folder/', urldecode($url)) && preg_match('/shared_folder$/', urldecode($url)) == false && preg_match('/shared_folder_session_'.$current_session_id.'$/', urldecode($url)) == false) {
  262. if ($filetype == 'file') {
  263. //Sound preview with jplayer
  264. if (preg_match('/mp3$/i', urldecode($url)) ||
  265. (preg_match('/wav$/i', urldecode($url)) && !preg_match('/_chnano_.wav$/i', urldecode($url))) ||
  266. preg_match('/ogg$/i', urldecode($url))) {
  267. $sound_preview = DocumentManager::generate_media_preview($counter);
  268. return $sound_preview;
  269. } elseif (
  270. //Show preview
  271. //preg_match('/html$/i', urldecode($url)) ||
  272. //preg_match('/htm$/i', urldecode($url)) ||
  273. preg_match('/swf$/i', urldecode($url)) ||
  274. preg_match('/png$/i', urldecode($url)) ||
  275. preg_match('/gif$/i', urldecode($url)) ||
  276. preg_match('/jpg$/i', urldecode($url)) ||
  277. preg_match('/jpeg$/i', urldecode($url)) ||
  278. preg_match('/bmp$/i', urldecode($url)) ||
  279. preg_match('/svg$/i', urldecode($url)) ||
  280. (preg_match('/wav$/i', urldecode($url)) && preg_match('/_chnano_.wav$/i', urldecode($url)) && api_get_setting('enable_nanogong') == 'true')
  281. ) {
  282. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  283. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag($filetype, $path).Display::return_icon('shared.png', get_lang('ResourceShared'), array()).'</a>';
  284. } else {
  285. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag($filetype, $path).Display::return_icon('shared.png', get_lang('ResourceShared'), array()).'</a>';
  286. }
  287. } else {
  288. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.build_document_icon_tag($filetype, $path).Display::return_icon('shared.png', get_lang('ResourceShared'), array()).'</a>';
  289. }
  290. } else {
  291. if ($filetype == 'file') {
  292. //Sound preview with jplayer
  293. if (preg_match('/mp3$/i', urldecode($url)) ||
  294. (preg_match('/wav$/i', urldecode($url)) && !preg_match('/_chnano_.wav$/i', urldecode($url))) ||
  295. preg_match('/ogg$/i', urldecode($url))) {
  296. $sound_preview = DocumentManager::generate_media_preview($counter);
  297. return $sound_preview;
  298. } elseif (
  299. //Show preview
  300. preg_match('/html$/i', urldecode($url)) ||
  301. preg_match('/htm$/i', urldecode($url)) ||
  302. preg_match('/swf$/i', urldecode($url)) ||
  303. preg_match('/png$/i', urldecode($url)) ||
  304. preg_match('/gif$/i', urldecode($url)) ||
  305. preg_match('/jpg$/i', urldecode($url)) ||
  306. preg_match('/jpeg$/i', urldecode($url)) ||
  307. preg_match('/bmp$/i', urldecode($url)) ||
  308. preg_match('/svg$/i', urldecode($url)) ||
  309. (preg_match('/wav$/i', urldecode($url)) && preg_match('/_chnano_.wav$/i', urldecode($url)) && api_get_setting('enable_nanogong') == 'true')
  310. ) {
  311. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid; //without preview
  312. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag($filetype, $path).'</a>';
  313. } else {
  314. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag($filetype, $path).'</a>';
  315. }
  316. } else {
  317. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.build_document_icon_tag($filetype, $path).'</a>';
  318. }
  319. }
  320. }
  321. }
  322. /**
  323. * Builds an img html tag for the filetype
  324. *
  325. * @param string $type (file/folder)
  326. * @param string $path
  327. * @return string img html tag
  328. */
  329. function build_document_icon_tag($type, $path)
  330. {
  331. $basename = basename($path);
  332. $current_session_id = api_get_session_id();
  333. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  334. if ($type == 'file') {
  335. $icon = choose_image($basename);
  336. if (preg_match('/_chnano_.wav$/i', $basename)) {
  337. $icon = "jplayer_play.png";
  338. $basename = 'wav'.' '.'(Nanogong)';
  339. } else {
  340. $basename = substr(strrchr($basename, '.'), 1);
  341. }
  342. } else {
  343. if ($path == '/shared_folder') {
  344. $icon = 'folder_users.gif';
  345. if ($is_allowed_to_edit) {
  346. $basename = get_lang('HelpUsersFolder');
  347. } else {
  348. $basename = get_lang('UserFolders');
  349. }
  350. } elseif (strstr($basename, 'sf_user_')) {
  351. $userinfo = Database::get_user_info_from_id(substr($basename, 8));
  352. $image_path = UserManager::get_user_picture_path_by_id(substr($basename, 8), 'web', false, true);
  353. if ($image_path['file'] == 'unknown.jpg') {
  354. $icon = $image_path['file'];
  355. } else {
  356. $icon = '../upload/users/'.substr($basename, 8).'/'.$image_path['file'];
  357. }
  358. $basename = get_lang('UserFolder').' '.api_get_person_name($userinfo['firstname'], $userinfo['lastname']);
  359. } elseif (strstr($path, 'shared_folder_session_')) {
  360. if ($is_allowed_to_edit) {
  361. $basename = '***('.api_get_session_name($current_session_id).')*** '.get_lang('HelpUsersFolder');
  362. } else {
  363. $basename = get_lang('UserFolders').' ('.api_get_session_name($current_session_id).')';
  364. }
  365. $icon = 'folder_users.gif';
  366. } else {
  367. $icon = 'folder_document.gif';
  368. if ($path == '/audio') {
  369. $icon = 'folder_audio.gif';
  370. if (api_is_allowed_to_edit()) {
  371. $basename = get_lang('HelpDefaultDirDocuments');
  372. } else {
  373. $basename = get_lang('Audio');
  374. }
  375. } elseif ($path == '/flash') {
  376. $icon = 'folder_flash.gif';
  377. if (api_is_allowed_to_edit()) {
  378. $basename = get_lang('HelpDefaultDirDocuments');
  379. } else {
  380. $basename = get_lang('Flash');
  381. }
  382. } elseif ($path == '/images') {
  383. $icon = 'folder_images.gif';
  384. if (api_is_allowed_to_edit()) {
  385. $basename = get_lang('HelpDefaultDirDocuments');
  386. } else {
  387. $basename = get_lang('Images');
  388. }
  389. } elseif ($path == '/video') {
  390. $icon = 'folder_video.gif';
  391. if (api_is_allowed_to_edit()) {
  392. $basename = get_lang('HelpDefaultDirDocuments');
  393. } else {
  394. $basename = get_lang('Video');
  395. }
  396. } elseif ($path == '/images/gallery') {
  397. $icon = 'folder_gallery.gif';
  398. if (api_is_allowed_to_edit()) {
  399. $basename = get_lang('HelpDefaultDirDocuments');
  400. } else {
  401. $basename = get_lang('Gallery');
  402. }
  403. } elseif ($path == '/chat_files') {
  404. $icon = 'folder_chat.gif';
  405. if (api_is_allowed_to_edit()) {
  406. $basename = get_lang('HelpFolderChat');
  407. } else {
  408. $basename = get_lang('ChatFiles');
  409. }
  410. } elseif ($path == '/learning_path') {
  411. $icon = 'folder_learningpath.gif';
  412. if (api_is_allowed_to_edit()) {
  413. $basename = get_lang('HelpFolderLearningPaths');
  414. } else {
  415. $basename = get_lang('LearningPaths');
  416. }
  417. }
  418. }
  419. }
  420. return Display::return_icon($icon, $basename, array());
  421. }
  422. /**
  423. * Creates the row of edit icons for a file/folder
  424. *
  425. * @param string $curdirpath current path (cfr open folder)
  426. * @param string $type (file/folder)
  427. * @param string $path dbase path of file/folder
  428. * @param int $visibility (1/0)
  429. * @param int $id dbase id of the document
  430. * @return string html img tags with hyperlinks
  431. */
  432. function build_edit_icons($document_data, $id, $is_template, $is_read_only = 0, $visibility)
  433. {
  434. if (isset($_SESSION['_gid'])) {
  435. $req_gid = '&gidReq='.$_SESSION['_gid'];
  436. } else {
  437. $req_gid = '';
  438. }
  439. $document_id = $document_data['id'];
  440. $type = $document_data['filetype'];
  441. $is_read_only = $document_data['readonly'];
  442. $path = $document_data['path'];
  443. $parent_id = DocumentManager::get_document_id(api_get_course_info(), dirname($path));
  444. $curdirpath = dirname($document_data['path']);
  445. $is_certificate_mode = DocumentManager::is_certificate_mode($path);
  446. $curdirpath = urlencode($curdirpath);
  447. $extension = pathinfo($path, PATHINFO_EXTENSION);
  448. // Build URL-parameters for table-sorting
  449. $sort_params = array();
  450. if (isset($_GET['column'])) {
  451. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  452. }
  453. if (isset($_GET['page_nr'])) {
  454. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  455. }
  456. if (isset($_GET['per_page'])) {
  457. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  458. }
  459. if (isset($_GET['direction'])) {
  460. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  461. }
  462. $sort_params = implode('&amp;', $sort_params);
  463. $visibility_icon = ($visibility == 0) ? 'invisible' : 'visible';
  464. $visibility_command = ($visibility == 0) ? 'set_visible' : 'set_invisible';
  465. $modify_icons = '';
  466. // If document is read only *or* we're in a session and the document
  467. // is from a non-session context, hide the edition capabilities
  468. if ($is_read_only /* or ($session_id!=api_get_session_id()) */) {
  469. if (api_is_course_admin() || api_is_platform_admin()) {
  470. if ($extension == 'svg' && api_browser_support('svg') && api_get_setting('enabled_support_svg') == 'true') {
  471. $modify_icons = '<a href="edit_draw.php?'.api_get_cidreq().'&id='.$document_id.$req_gid.'">'.Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL).'</a>';
  472. } elseif ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg' || $extension == 'bmp' || $extension == 'gif' || $extension == 'pxd' && api_get_setting('enabled_support_pixlr') == 'true') {
  473. $modify_icons = '<a href="edit_paint.php?'.api_get_cidreq().'&id='.$document_id.$req_gid.'">'.Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL).'</a>';
  474. } else {
  475. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq().'&id='.$document_id.$req_gid.'">'.Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL).'</a>';
  476. }
  477. } else {
  478. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  479. }
  480. $modify_icons .= '&nbsp;'.Display::return_icon('move_na.png', get_lang('Move'), array(), ICON_SIZE_SMALL);
  481. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  482. $modify_icons .= '&nbsp;'.Display::return_icon($visibility_icon.'.png', get_lang('VisibilityCannotBeChanged'), '', ICON_SIZE_SMALL);
  483. }
  484. $modify_icons .= '&nbsp;'.Display::return_icon('delete_na.png', get_lang('Delete'), array(), ICON_SIZE_SMALL);
  485. } else {
  486. //Edit button
  487. if (in_array($path, DocumentManager::get_system_folders())) {
  488. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  489. } elseif ($is_certificate_mode) {
  490. // gradebook category doesn't seem to be taken into account
  491. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq().'&amp;id='.$document_id.$req_gid.'&curdirpath=/certificates">'.Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL).'</a>';
  492. } else {
  493. if (api_get_session_id()) {
  494. if ($document_data['session_id'] == api_get_session_id()) {
  495. if ($extension == 'svg' && api_browser_support('svg') && api_get_setting('enabled_support_svg') == 'true') {
  496. $modify_icons = '<a href="edit_draw.php?'.api_get_cidreq().'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL).'</a>';
  497. } elseif ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg' || $extension == 'bmp' || $extension == 'gif' || $extension == 'pxd' && api_get_setting('enabled_support_pixlr') == 'true') {
  498. $modify_icons = '<a href="edit_paint.php?'.api_get_cidreq().'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL).'</a>';
  499. } else {
  500. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq().'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL).'</a>';
  501. }
  502. } else {
  503. $modify_icons .= '&nbsp;'.Display::return_icon('edit_na.png', get_lang('Edit'), array(), ICON_SIZE_SMALL).'</a>';
  504. }
  505. } else {
  506. if ($extension == 'svg' && api_browser_support('svg') && api_get_setting('enabled_support_svg') == 'true') {
  507. $modify_icons = '<a href="edit_draw.php?'.api_get_cidreq().'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL).'</a>';
  508. } elseif ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg' || $extension == 'bmp' || $extension == 'gif' || $extension == 'pxd' && api_get_setting('enabled_support_pixlr') == 'true') {
  509. $modify_icons = '<a href="edit_paint.php?'.api_get_cidreq().'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL).'</a>';
  510. } else {
  511. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq().'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon('edit.png', get_lang('Modify'), '', ICON_SIZE_SMALL).'</a>';
  512. }
  513. }
  514. }
  515. //Move button
  516. if ($is_certificate_mode || in_array($path, DocumentManager::get_system_folders())) {
  517. $modify_icons .= '&nbsp;'.Display::return_icon('move_na.png', get_lang('Move'), array(), ICON_SIZE_SMALL).'</a>';
  518. } else {
  519. if (api_get_session_id()) {
  520. if ($document_data['session_id'] == api_get_session_id()) {
  521. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;id='.$parent_id.'&amp;move='.$document_id.$req_gid.'">'.Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL).'</a>';
  522. } else {
  523. $modify_icons .= '&nbsp;'.Display::return_icon('move_na.png', get_lang('Move'), array(), ICON_SIZE_SMALL).'</a>';
  524. }
  525. } else {
  526. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;id='.$parent_id.'&amp;move='.$document_id.$req_gid.'">'.Display::return_icon('move.png', get_lang('Move'), array(), ICON_SIZE_SMALL).'</a>';
  527. }
  528. }
  529. //Visibility button
  530. if ($is_certificate_mode) {
  531. $modify_icons .= '&nbsp;'.Display::return_icon($visibility_icon.'.png', get_lang('VisibilityCannotBeChanged'), array(), ICON_SIZE_SMALL).'</a>';
  532. } else {
  533. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  534. if ($visibility_icon == 'invisible') {
  535. $tip_visibility = get_lang('Show');
  536. } else {
  537. $tip_visibility = get_lang('Hide');
  538. }
  539. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;id='.$parent_id.'&amp;'.$visibility_command.'='.$id.$req_gid.'&amp;'.$sort_params.'">'.Display::return_icon($visibility_icon.'.png', $tip_visibility, '', ICON_SIZE_SMALL).'</a>';
  540. }
  541. }
  542. //Delete button
  543. if (in_array($path, DocumentManager::get_system_folders())) {
  544. $modify_icons .= '&nbsp;'.Display::return_icon('delete_na.png', get_lang('ThisFolderCannotBeDeleted'), array(), ICON_SIZE_SMALL);
  545. } else {
  546. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && DocumentManager::get_default_certificate_id(api_get_course_id()) == $id) {
  547. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode($path).$req_gid.'&amp;'.$sort_params.'delete_certificate_id='.$id.'" onclick="return confirmation(\''.basename($path).'\');">'.Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  548. } else {
  549. if ($is_certificate_mode) {
  550. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode($path).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename($path).'\');">'.Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  551. } else {
  552. if (api_get_session_id()) {
  553. if ($document_data['session_id'] == api_get_session_id()) {
  554. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode($path).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename($path).'\');">'.Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  555. } else {
  556. $modify_icons .= '&nbsp;'.Display::return_icon('delete_na.png', get_lang('ThisFolderCannotBeDeleted'), array(), ICON_SIZE_SMALL);
  557. }
  558. } else {
  559. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode($path).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename($path).'\');">'.Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  560. }
  561. }
  562. }
  563. }
  564. }
  565. if ($type == 'file' && ($extension == 'html' || $extension == 'htm')) {
  566. if ($is_template == 0) {
  567. if ((isset($_GET['curdirpath']) && $_GET['curdirpath'] != '/certificates') || !isset($_GET['curdirpath'])) {
  568. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$curdirpath.'&amp;add_as_template='.$id.$req_gid.'&amp;'.$sort_params.'">'.Display::return_icon('wizard.png', get_lang('AddAsTemplate'), array(), ICON_SIZE_SMALL).'</a>';
  569. }
  570. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates') {//allow attach certificate to course
  571. $visibility_icon_certificate = 'nocertificate';
  572. if (DocumentManager::get_default_certificate_id(api_get_course_id()) == $id) {
  573. $visibility_icon_certificate = 'certificate';
  574. $certificate = get_lang('DefaultCertificate');
  575. $preview = get_lang('PreviewCertificate');
  576. $is_preview = true;
  577. } else {
  578. $is_preview = false;
  579. $certificate = get_lang('NoDefaultCertificate');
  580. }
  581. if (isset($_GET['selectcat'])) {
  582. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$curdirpath.'&amp;selectcat='.Security::remove_XSS($_GET['selectcat']).'&amp;set_certificate='.$id.$req_gid.'&amp;'.$sort_params.'"><img src="../img/'.$visibility_icon_certificate.'.png" border="0" title="'.$certificate.'" alt="" /></a>';
  583. if ($is_preview) {
  584. $modify_icons .= '&nbsp;<a target="_blank" href="'.api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$curdirpath.'&amp;set_preview='.$id.$req_gid.'&amp;'.$sort_params.'" >'.
  585. Display::return_icon('preview_view.png', $preview, '', ICON_SIZE_SMALL).'</a>';
  586. }
  587. }
  588. }
  589. } else {
  590. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&curdirpath='.$curdirpath.'&amp;remove_as_template='.$id.$req_gid.'&amp;'.$sort_params.'">'.
  591. Display::return_icon('wizard_na.png', get_lang('RemoveAsTemplate'), '', ICON_SIZE_SMALL).'</a>';
  592. }
  593. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq().'&action=export_to_pdf&id='.$id.'">'.Display::return_icon('pdf.png', get_lang('Export2PDF'), array(), ICON_SIZE_SMALL).'</a>';
  594. }
  595. return $modify_icons;
  596. }
  597. function build_move_to_selector($folders, $curdirpath, $move_file, $group_dir = '')
  598. {
  599. $form = new FormValidator('move_to', 'post', api_get_self());
  600. // Form title
  601. $form->addElement('hidden', 'move_file', $move_file);
  602. $options = array();
  603. // Group documents cannot be uploaded in the root
  604. if ($group_dir == '') {
  605. if ($curdirpath != '/') {
  606. $options['/'] = get_lang('Documents');
  607. }
  608. if (is_array($folders)) {
  609. foreach ($folders as & $folder) {
  610. //Hide some folders
  611. if ($folder == '/HotPotatoes_files' || $folder == '/certificates' || basename($folder) == 'css') {
  612. continue;
  613. }
  614. //Admin setting for Hide/Show the folders of all users
  615. if (api_get_setting('show_users_folders') == 'false' && (strstr($folder, '/shared_folder') || strstr($folder, 'shared_folder_session_'))) {
  616. continue;
  617. }
  618. //Admin setting for Hide/Show Default folders to all users
  619. if (api_get_setting('show_default_folders') == 'false' && ($folder == '/images' || $folder == '/flash' || $folder == '/audio' || $folder == '/video' || strstr($folder, '/images/gallery') || $folder == '/video/flv')) {
  620. continue;
  621. }
  622. //Admin setting for Hide/Show chat history folder
  623. if (api_get_setting('show_chat_folder') == 'false' && $folder == '/chat_files') {
  624. continue;
  625. }
  626. // You cannot move a file to:
  627. // 1. current directory
  628. // 2. inside the folder you want to move
  629. // 3. inside a subfolder of the folder you want to move
  630. if (($curdirpath != $folder) && ($folder != $move_file) && (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')) {
  631. $path_displayed = $folder;
  632. // If document title is used, we have to display titles instead of real paths...
  633. $path_displayed = get_titles_of_path($folder);
  634. if (empty($path_displayed)) {
  635. $path_displayed = get_lang('Untitled');
  636. }
  637. $options[$folder] = $path_displayed;
  638. //$form .= '<option value="'.$folder.'">'.$path_displayed.'</option>';
  639. }
  640. }
  641. }
  642. } else {
  643. foreach ($folders as $folder) {
  644. if (($curdirpath != $folder) && ($folder != $move_file) && (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')) { // Cannot copy dir into his own subdir
  645. $path_displayed = get_titles_of_path($folder);
  646. $display_folder = substr($path_displayed, strlen($group_dir));
  647. $display_folder = ($display_folder == '') ? get_lang('Documents') : $display_folder;
  648. //$form .= '<option value="'.$folder.'">'.$display_folder.'</option>';
  649. $options[$folder] = $display_folder;
  650. }
  651. }
  652. }
  653. $form->addElement('select', 'move_to', get_lang('MoveTo'), $options);
  654. $form->addElement('button', 'move_file_submit', get_lang('MoveElement'));
  655. return $form->return_form();
  656. }
  657. /**
  658. * Gets the path translated with title of docs and folders
  659. * @param string the real path
  660. * @return the path which should be displayed
  661. */
  662. function get_titles_of_path($path)
  663. {
  664. global $tmp_folders_titles;
  665. $course_id = api_get_course_int_id();
  666. $nb_slashes = substr_count($path, '/');
  667. $tmp_path = '';
  668. $current_slash_pos = 0;
  669. $path_displayed = '';
  670. for ($i = 0; $i < $nb_slashes; $i++) {
  671. // For each folder of the path, retrieve title.
  672. $current_slash_pos = strpos($path, '/', $current_slash_pos + 1);
  673. $tmp_path = substr($path, strpos($path, '/', 0), $current_slash_pos);
  674. if (empty($tmp_path)) {
  675. // If empty, then we are in the final part of the path
  676. $tmp_path = $path;
  677. }
  678. if (!empty($tmp_folders_titles[$tmp_path])) {
  679. // If this path has soon been stored here we don't need a new query
  680. $path_displayed .= $tmp_folders_titles[$tmp_path];
  681. } else {
  682. $sql = 'SELECT title FROM '.Database::get_course_table(TABLE_DOCUMENT).'
  683. WHERE c_id = '.$course_id.' AND path LIKE BINARY "'.$tmp_path.'"';
  684. $rs = Database::query($sql);
  685. $tmp_title = '/'.Database::result($rs, 0, 0);
  686. $path_displayed .= $tmp_title;
  687. $tmp_folders_titles[$tmp_path] = $tmp_title;
  688. }
  689. }
  690. return $path_displayed;
  691. }
  692. /**
  693. * This function displays the name of the user and makes the link tothe user tool.
  694. *
  695. * @param $user_id
  696. * @param $name
  697. * @return a link to the userInfo.php
  698. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  699. * @version february 2006, dokeos 1.8
  700. */
  701. function display_user_link_document($user_id, $name)
  702. {
  703. if ($user_id != 0) {
  704. return '<a href="../user/userInfo.php?uInfo='.$user_id.'">'.$name.'</a>';
  705. } else {
  706. return get_lang('Anonymous');
  707. }
  708. }
  709. /**
  710. * Creates form that asks for the directory name.
  711. * @return string html-output text for the form
  712. */
  713. function create_dir_form($current_dir_id)
  714. {
  715. global $document_id;
  716. $form = new FormValidator('create_dir_form', 'post', '', '', null, false);
  717. $form->addElement('hidden', 'create_dir', 1);
  718. $form->addElement('hidden', 'dir_id', intval($document_id));
  719. $form->addElement('hidden', 'id', intval($current_dir_id));
  720. $form->addElement('header', '', get_lang('CreateDir'));
  721. $form->addElement('text', 'dirname', get_lang('NewDir'));
  722. $form->addElement('style_submit_button', 'submit', get_lang('CreateFolder'), 'class="add"');
  723. $new_folder_text = $form->return_form();
  724. return $new_folder_text;
  725. }
  726. /**
  727. * Checks whether the user is in shared folder
  728. * @return return bool Return true when user is into shared folder
  729. */
  730. function is_shared_folder($curdirpath, $current_session_id)
  731. {
  732. $clean_curdirpath = Security::remove_XSS($curdirpath);
  733. if ($clean_curdirpath == '/shared_folder') {
  734. return true;
  735. } elseif ($clean_curdirpath == '/shared_folder_session_'.$current_session_id) {
  736. return true;
  737. } else {
  738. return false;
  739. }
  740. }
  741. /**
  742. * Checks whether the user is into any user shared folder
  743. * @return return bool Return true when user is in any user shared folder
  744. */
  745. function is_any_user_shared_folder($path, $current_session_id)
  746. {
  747. $clean_path = Security::remove_XSS($path);
  748. if (strpos($clean_path, 'shared_folder/sf_user_')) {
  749. return true;
  750. } elseif (strpos($clean_path, 'shared_folder_session_'.$current_session_id.'/sf_user_')) {
  751. return true;
  752. } else {
  753. return false;
  754. }
  755. }
  756. /**
  757. * Checks whether the user is into his shared folder or into a subfolder
  758. * @return return bool Return true when user is in his user shared folder or into a subforder
  759. */
  760. function is_my_shared_folder($user_id, $path, $current_session_id)
  761. {
  762. $clean_path = Security::remove_XSS($path).'/';
  763. $main_user_shared_folder = '/shared_folder\/sf_user_'.$user_id.'\//'; //for security does not remove the last slash
  764. $main_user_shared_folder_session = '/shared_folder_session_'.$current_session_id.'\/sf_user_'.$user_id.'\//'; //for security does not remove the last slash
  765. if (preg_match($main_user_shared_folder, $clean_path)) {
  766. return true;
  767. } elseif (preg_match($main_user_shared_folder_session, $clean_path)) {
  768. return true;
  769. } else {
  770. return false;
  771. }
  772. }
  773. /**
  774. * Check if the file name or folder searched exist
  775. * @return return bool Return true when exist
  776. */
  777. function search_keyword($document_name, $keyword)
  778. {
  779. if (api_strripos($document_name, $keyword) !== false) {
  780. return true;
  781. } else {
  782. return false;
  783. }
  784. }
  785. /**
  786. * Checks whether a document can be previewed by using the browser.
  787. * @param string $file_extension The filename extension of the document (it must be in lower case).
  788. * @return bool Returns TRUE or FALSE.
  789. */
  790. function is_browser_viewable($file_extension)
  791. {
  792. static $allowed_extensions = array(
  793. 'htm', 'html', 'xhtml',
  794. 'gif', 'jpg', 'jpeg', 'png', 'tif', 'tiff',
  795. 'pdf', 'svg', 'swf',
  796. 'txt', 'log',
  797. 'mp4', 'ogg', 'ogv', 'ogx', 'mpg', 'mpeg', 'mov', 'avi', 'webm', 'wmv',
  798. 'mp3', 'oga', 'wav', 'au', 'wma', 'mid', 'kar'
  799. );
  800. /*
  801. //TODO: make a admin swich to strict mode
  802. 1. global default $allowed_extensions only: 'htm', 'html', 'xhtml', 'gif', 'jpg', 'jpeg', 'png', 'bmp', 'txt', 'log'
  803. if (in_array($file_extension, $allowed_extensions)) { // Assignment + a logical check.
  804. return true;
  805. }
  806. 2. check native support
  807. 3. check plugins: quicktime, mediaplayer, vlc, acrobat, flash, java
  808. */
  809. if (!($result = in_array($file_extension, $allowed_extensions))) { // Assignment + a logical check.
  810. return false;
  811. }
  812. //check native support (Explorer, Opera, Firefox, Chrome, Safari)
  813. if ($file_extension == "pdf") {
  814. return api_browser_support('pdf');
  815. } elseif ($file_extension == "mp3") {
  816. return api_browser_support('mp3');
  817. } elseif ($file_extension == "mp4") {
  818. return api_browser_support('mp4');
  819. } elseif ($file_extension == "ogg" || $file_extension == "ogx" || $file_extension == "ogv" || $file_extension == "oga") {
  820. return api_browser_support('ogg');
  821. } elseif ($file_extension == "svg") {
  822. return api_browser_support('svg');
  823. } elseif ($file_extension == "mpg" || $file_extension == "mpeg") {
  824. return api_browser_support('mpg');
  825. } elseif ($file_extension == "mov") {
  826. return api_browser_support('mov');
  827. } elseif ($file_extension == "wav") {
  828. return api_browser_support('wav');
  829. } elseif ($file_extension == "mid" || $file_extension == "kar") {
  830. return api_browser_support('mid');
  831. } elseif ($file_extension == "avi") {
  832. return api_browser_support('avi');
  833. } elseif ($file_extension == "wma") {
  834. return api_browser_support('wma');
  835. } elseif ($file_extension == "wmv") {
  836. return api_browser_support('wmv');
  837. } elseif ($file_extension == "tif" || $file_extension == "tiff") {
  838. return api_browser_support('tif');
  839. } elseif ($file_extension == "mov") {
  840. return api_browser_support('mov');
  841. } elseif ($file_extension == "au") {
  842. return api_browser_support('au');
  843. } elseif ($file_extension == "webm") {
  844. return api_browser_support('webm');
  845. }
  846. return $result;
  847. }
  848. function getlist($directory)
  849. {
  850. //global $delim, $win;
  851. if ($d = @opendir($directory)) {
  852. while (($filename = @readdir($d)) !== false) {
  853. $path = $directory.$filename;
  854. if ($filename != '.' && $filename != '..' && $filename != '.svn') {
  855. $file = array(
  856. "lastedit_date" => date("Y-m-d H:i:s", 9876), //date ("Y-m-d H:i:s", filemtime($path)),
  857. "visibility" => 1,
  858. "path" => $path,
  859. "title" => basename($path),
  860. "filetype" => filetype($path),
  861. "size" => filesize($path)
  862. );
  863. $files[] = $file;
  864. }
  865. }
  866. return $files;
  867. } else {
  868. return false;
  869. }
  870. }
  871. function check_and_create_resource_directory($repository_path, $resource_directory, $resource_directory_name)
  872. {
  873. global $permissions_for_new_directories;
  874. $resource_directory_full_path = substr($repository_path, 0, strlen($repository_path) - 1).$resource_directory.'/';
  875. if (!is_dir($resource_directory_full_path)) {
  876. if (@mkdir($resource_directory_full_path, $permissions_for_new_directories)) {
  877. // While we are in a course: Registering the newly created folder in the course's database.
  878. if (api_is_in_course()) {
  879. global $_course, $_user;
  880. global $group_properties, $to_group_id;
  881. $group_directory = !empty($group_properties['directory']) ? $group_properties['directory'] : '';
  882. $doc_id = add_document($_course, $group_directory.$resource_directory, 'folder', 0, $resource_directory_name);
  883. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $_user['user_id'], $to_group_id);
  884. }
  885. return true;
  886. }
  887. return false;
  888. }
  889. return true;
  890. }