document.inc.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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(
  39. 'select',
  40. 'id',
  41. get_lang('CurrentDirectory'),
  42. '',
  43. 'onchange="javascript: document.selector.submit();"'
  44. );
  45. if ($change_renderer) {
  46. $renderer = $form->defaultRenderer();
  47. $renderer->setElementTemplate('<span>{label} : {element}</span> ', 'curdirpath');
  48. }
  49. // Group documents cannot be uploaded in the root
  50. if (empty($group_dir)) {
  51. $parent_select->addOption(get_lang('Documents'), '/');
  52. if (is_array($folders)) {
  53. foreach ($folders as $folder_id => & $folder) {
  54. $selected = ($document_id == $folder_id) ? ' selected="selected"' : '';
  55. $path_parts = explode('/', $folder);
  56. $folder_titles[$folder] = Text::cut($folder_titles[$folder], 80);
  57. $counter = count($path_parts) - 2;
  58. if ($counter > 0) {
  59. $label = str_repeat('&nbsp;&nbsp;&nbsp;', $counter).' &mdash; '.$folder_titles[$folder];
  60. } else {
  61. $label = ' &mdash; '.$folder_titles[$folder];
  62. }
  63. $parent_select->addOption($label, $folder_id);
  64. if ($selected != '') {
  65. $parent_select->setSelected($folder_id);
  66. }
  67. }
  68. }
  69. } else {
  70. if (!empty($folders)) {
  71. foreach ($folders as $folder_id => & $folder) {
  72. $selected = ($document_id == $folder_id) ? ' selected="selected"' : '';
  73. $label = $folder_titles[$folder];
  74. if ($folder == $group_dir) {
  75. $label = get_lang('Documents');
  76. } else {
  77. $path_parts = explode('/', str_replace($group_dir, '', $folder));
  78. $label = Text::cut($label, 80);
  79. $label = str_repeat('&nbsp;&nbsp;&nbsp;', count($path_parts) - 2).' &mdash; '.$label;
  80. }
  81. $parent_select->addOption($label, $folder_id);
  82. if ($selected != '') {
  83. $parent_select->setSelected($folder_id);
  84. }
  85. }
  86. }
  87. }
  88. $html = $form->toHtml();
  89. return $html;
  90. }
  91. /**
  92. * Create a html hyperlink depending on if it's a folder or a file
  93. *
  94. * @param string $www
  95. * @param string $title
  96. * @param string $path
  97. * @param string $filetype (file/folder)
  98. * @param int $visibility (1/0)
  99. * @param int $show_as_icon - if it is true, only a clickable icon will be shown
  100. * @return string url
  101. */
  102. function create_document_link($document_data, $show_as_icon = false, $counter = null, $visibility)
  103. {
  104. global $dbl_click_id;
  105. if (isset($_SESSION['_gid'])) {
  106. $req_gid = '&amp;gidReq='.$_SESSION['_gid'];
  107. } else {
  108. $req_gid = '';
  109. }
  110. $course_info = api_get_course_info();
  111. $www = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document';
  112. // Get the title or the basename depending on what we're using
  113. if ($document_data['title'] != '') {
  114. $title = $document_data['title'];
  115. } else {
  116. $title = basename($document_data['path']);
  117. }
  118. $filetype = $document_data['filetype'];
  119. $size = $filetype == 'folder' ? FileManager::get_total_folder_size(
  120. $document_data['path'],
  121. api_is_allowed_to_edit(null, true)
  122. ) : $document_data['size'];
  123. $path = $document_data['path'];
  124. $url_path = urlencode($document_data['path']);
  125. // Add class="invisible" on invisible files
  126. $visibility_class = ($visibility == false) ? ' class="muted"' : '';
  127. if (!$show_as_icon) {
  128. // Build download link (icon)
  129. $forcedownload_link = ($filetype == 'folder') ? api_get_self().'?'.api_get_cidreq(
  130. ).'&action=downloadfolder&id='.$document_data['id'] : api_get_self().'?'.api_get_cidreq(
  131. ).'&amp;action=download&amp;id='.$document_data['id'];
  132. // Folder download or file download?
  133. $forcedownload_icon = ($filetype == 'folder') ? 'save_pack.png' : 'save.png';
  134. // Prevent multiple clicks on zipped folder download
  135. $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; }\"" : '';
  136. }
  137. $target = '_self';
  138. $is_browser_viewable_file = false;
  139. if ($filetype == 'file') {
  140. // Check the extension
  141. $ext = explode('.', $path);
  142. $ext = strtolower($ext[sizeof($ext) - 1]);
  143. // HTML-files an some other types are shown in a frameset by default.
  144. $is_browser_viewable_file = is_browser_viewable($ext);
  145. if ($is_browser_viewable_file) {
  146. //$url = 'showinframes.php?'.api_get_cidreq().'&amp;file='.$url_path.$req_gid;
  147. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  148. } else {
  149. // url-encode for problematic characters (we may not call them dangerous characters...)
  150. $path = str_replace('%2F', '/', $url_path).'?'.api_get_cidreq();
  151. //$new_path = '?id='.$document_data['id'];
  152. $url = $www.$path;
  153. }
  154. //$path = str_replace('%2F', '/',$url_path).'?'.api_get_cidreq();
  155. $path = str_replace('%2F', '/', $url_path); //yox view hack otherwise the image can't be well read
  156. $url = $www.$path;
  157. // Disabled fragment of code, there is a special icon for opening in a new window.
  158. //// Files that we want opened in a new window
  159. //if ($ext == 'txt' || $ext == 'log' || $ext == 'css' || $ext == 'js') { // Add here
  160. // $target = '_blank';
  161. //}
  162. } else {
  163. //$url = api_get_self().'?'.api_get_cidreq().'&amp;curdirpath='.$url_path.$req_gid;
  164. $url = api_get_self().'?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  165. }
  166. // The little download icon
  167. //$tooltip_title = str_replace('?cidReq='.$_GET['cidReq'], '', basename($path));
  168. $tooltip_title = explode('?', basename($path));
  169. $tooltip_title = $title;
  170. $tooltip_title_alt = $tooltip_title;
  171. if ($path == '/shared_folder') {
  172. $tooltip_title_alt = get_lang('UserFolders');
  173. } elseif (strstr($path, 'shared_folder_session_')) {
  174. $tooltip_title_alt = get_lang('UserFolders').' ('.api_get_session_name(api_get_session_id()).')';
  175. } elseif (strstr($tooltip_title, 'sf_user_')) {
  176. $userinfo = api_get_user_info(substr($tooltip_title, 8));
  177. $tooltip_title_alt = get_lang('UserFolder').' '.$userinfo['complete_name'];
  178. } elseif ($path == '/chat_files') {
  179. $tooltip_title_alt = get_lang('ChatFiles');
  180. } elseif ($path == '/learning_path') {
  181. $tooltip_title_alt = get_lang('LearningPaths');
  182. } elseif ($path == '/video') {
  183. $tooltip_title_alt = get_lang('Video');
  184. } elseif ($path == '/audio') {
  185. $tooltip_title_alt = get_lang('Audio');
  186. } elseif ($path == '/flash') {
  187. $tooltip_title_alt = get_lang('Flash');
  188. } elseif ($path == '/images') {
  189. $tooltip_title_alt = get_lang('Images');
  190. } elseif ($path == '/images/gallery') {
  191. $tooltip_title_alt = get_lang('DefaultCourseImages');
  192. }
  193. $current_session_id = api_get_session_id();
  194. $copy_to_myfiles = $open_in_new_window_link = null;
  195. $curdirpath = isset($_GET['curdirpath']) ? Security::remove_XSS($_GET['curdirpath']) : null;
  196. if (!$show_as_icon) {
  197. if ($filetype == 'folder') {
  198. if (api_is_allowed_to_edit() || api_is_platform_admin() || api_get_setting(
  199. 'students_download_folders'
  200. ) == 'true'
  201. ) {
  202. //filter when I am into shared folder, I can show for donwload only my shared folder
  203. if (is_shared_folder($curdirpath, $current_session_id)) {
  204. if (preg_match(
  205. '/shared_folder\/sf_user_'.api_get_user_id().'$/',
  206. urldecode($forcedownload_link)
  207. ) || preg_match(
  208. '/shared_folder_session_'.$current_session_id.'\/sf_user_'.api_get_user_id().'$/',
  209. urldecode($forcedownload_link)
  210. ) || api_is_allowed_to_edit() || api_is_platform_admin()
  211. ) {
  212. $force_download_html = ($size == 0) ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.Display::return_icon(
  213. $forcedownload_icon,
  214. get_lang('Download'),
  215. array(),
  216. ICON_SIZE_SMALL
  217. ).'</a>';
  218. }
  219. } elseif (!preg_match('/shared_folder/', urldecode($forcedownload_link)) || api_is_allowed_to_edit(
  220. ) || api_is_platform_admin()
  221. ) {
  222. $force_download_html = ($size == 0) ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.Display::return_icon(
  223. $forcedownload_icon,
  224. get_lang('Download'),
  225. array(),
  226. ICON_SIZE_SMALL
  227. ).'</a>';
  228. }
  229. }
  230. } else {
  231. $force_download_html = ($size == 0) ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.Display::return_icon(
  232. $forcedownload_icon,
  233. get_lang('Download'),
  234. array(),
  235. ICON_SIZE_SMALL
  236. ).'</a>';
  237. }
  238. $send_to = null;
  239. //Copy files to users myfiles
  240. if (api_get_setting('allow_social_tool') == 'true' && api_get_setting(
  241. 'users_copy_files'
  242. ) == 'true' && !api_is_anonymous()
  243. ) {
  244. $copy_myfiles_link = ($filetype == 'file') ? api_get_self().'?'.api_get_cidreq(
  245. ).'&action=copytomyfiles&id='.$document_data['id'].$req_gid : api_get_self().'?'.api_get_cidreq();
  246. if ($filetype == 'file') {
  247. $copy_to_myfiles = '<a href="'.$copy_myfiles_link.'" style="float:right"'.$prevent_multiple_click.'>'.Display::return_icon(
  248. 'briefcase.png',
  249. get_lang('CopyToMyFiles'),
  250. array(),
  251. ICON_SIZE_SMALL
  252. ).'&nbsp;&nbsp;</a>';
  253. }
  254. if ($filetype == 'file') {
  255. $send_to = Portfolio::share('document', $document_data['id'], array('style' => 'float:right;'));
  256. }
  257. }
  258. $pdf_icon = '';
  259. $extension = pathinfo($path, PATHINFO_EXTENSION);
  260. if (!api_is_allowed_to_edit() && api_get_setting(
  261. 'students_export2pdf'
  262. ) == 'true' && $filetype == 'file' && in_array($extension, array('html', 'htm'))
  263. ) {
  264. $pdf_icon = ' <a style="float:right".'.$prevent_multiple_click.' href="'.api_get_self().'?'.api_get_cidreq(
  265. ).'&action=export_to_pdf&id='.$document_data['id'].'">'.Display::return_icon(
  266. 'pdf.png',
  267. get_lang('Export2PDF'),
  268. array(),
  269. ICON_SIZE_SMALL
  270. ).'</a> ';
  271. }
  272. if ($is_browser_viewable_file) {
  273. $open_in_new_window_link = '<a href="'.$www.str_replace('%2F', '/', $url_path).'?'.api_get_cidreq(
  274. ).'" style="float:right"'.$prevent_multiple_click.' target="_blank">'.Display::return_icon(
  275. 'open_in_new_window.png',
  276. get_lang('OpenInANewWindow'),
  277. array(),
  278. ICON_SIZE_SMALL
  279. ).'&nbsp;&nbsp;</a>';
  280. }
  281. //target="'.$target.'"
  282. if ($filetype == 'file') {
  283. //Sound preview with jplayer
  284. if (preg_match('/mp3$/i', urldecode($url)) ||
  285. (preg_match('/wav$/i', urldecode($url)) && !preg_match('/_chnano_.wav$/i', urldecode($url))) ||
  286. preg_match('/ogg$/i', urldecode($url))
  287. ) {
  288. return '<span style="float:left" '.$visibility_class.'>'.$title.'</span>'.$force_download_html.$send_to.$copy_to_myfiles.$open_in_new_window_link.$pdf_icon;
  289. } elseif (
  290. //Show preview
  291. //preg_match('/html$/i', urldecode($url)) ||
  292. //preg_match('/htm$/i', urldecode($url)) ||
  293. preg_match('/swf$/i', urldecode($url)) ||
  294. preg_match('/png$/i', urldecode($url)) ||
  295. preg_match('/gif$/i', urldecode($url)) ||
  296. preg_match('/jpg$/i', urldecode($url)) ||
  297. preg_match('/jpeg$/i', urldecode($url)) ||
  298. preg_match('/bmp$/i', urldecode($url)) ||
  299. preg_match('/svg$/i', urldecode($url)) ||
  300. (preg_match('/wav$/i', urldecode($url)) && preg_match(
  301. '/_chnano_.wav$/i',
  302. urldecode($url)
  303. ) && api_get_setting('enable_nanogong') == 'true')
  304. ) {
  305. //yox view
  306. //$url = 'showinframesmin.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  307. //Simpler version of showinframesmin.php with no headers
  308. $url = 'show_content.php?id='.$document_data['id'].$req_gid.'&'.api_get_cidreq().'&width=700&height=500';
  309. $class = 'ajax';
  310. if ($visibility == false) {
  311. $class = "ajax invisible";
  312. }
  313. 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;
  314. } else {
  315. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  316. //No plugin just the old and good showinframes.php page
  317. 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;
  318. }
  319. } else {
  320. 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;
  321. }
  322. //end copy files to users myfiles
  323. } else {
  324. //Icon column
  325. if (preg_match('/shared_folder/', urldecode($url)) && preg_match(
  326. '/shared_folder$/',
  327. urldecode($url)
  328. ) == false && preg_match('/shared_folder_session_'.$current_session_id.'$/', urldecode($url)) == false
  329. ) {
  330. if ($filetype == 'file') {
  331. //Sound preview with jplayer
  332. if (preg_match('/mp3$/i', urldecode($url)) ||
  333. (preg_match('/wav$/i', urldecode($url)) && !preg_match('/_chnano_.wav$/i', urldecode($url))) ||
  334. preg_match('/ogg$/i', urldecode($url))
  335. ) {
  336. $sound_preview = DocumentManager::generate_media_preview($counter);
  337. return $sound_preview;
  338. } elseif (
  339. //Show preview
  340. //preg_match('/html$/i', urldecode($url)) ||
  341. //preg_match('/htm$/i', urldecode($url)) ||
  342. preg_match('/swf$/i', urldecode($url)) ||
  343. preg_match('/png$/i', urldecode($url)) ||
  344. preg_match('/gif$/i', urldecode($url)) ||
  345. preg_match('/jpg$/i', urldecode($url)) ||
  346. preg_match('/jpeg$/i', urldecode($url)) ||
  347. preg_match('/bmp$/i', urldecode($url)) ||
  348. preg_match('/svg$/i', urldecode($url)) ||
  349. (preg_match('/wav$/i', urldecode($url)) && preg_match(
  350. '/_chnano_.wav$/i',
  351. urldecode($url)
  352. ) && api_get_setting('enable_nanogong') == 'true')
  353. ) {
  354. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  355. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag(
  356. $filetype,
  357. $path
  358. ).Display::return_icon('shared.png', get_lang('ResourceShared'), array()).'</a>';
  359. } else {
  360. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag(
  361. $filetype,
  362. $path
  363. ).Display::return_icon('shared.png', get_lang('ResourceShared'), array()).'</a>';
  364. }
  365. } else {
  366. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.build_document_icon_tag(
  367. $filetype,
  368. $path
  369. ).Display::return_icon('shared.png', get_lang('ResourceShared'), array()).'</a>';
  370. }
  371. } else {
  372. if ($filetype == 'file') {
  373. //Sound preview with jplayer
  374. if (preg_match('/mp3$/i', urldecode($url)) ||
  375. (preg_match('/wav$/i', urldecode($url)) && !preg_match('/_chnano_.wav$/i', urldecode($url))) ||
  376. preg_match('/ogg$/i', urldecode($url))
  377. ) {
  378. $sound_preview = DocumentManager::generate_media_preview($counter);
  379. return $sound_preview;
  380. } elseif (
  381. //Show preview
  382. preg_match('/html$/i', urldecode($url)) ||
  383. preg_match('/htm$/i', urldecode($url)) ||
  384. preg_match('/swf$/i', urldecode($url)) ||
  385. preg_match('/png$/i', urldecode($url)) ||
  386. preg_match('/gif$/i', urldecode($url)) ||
  387. preg_match('/jpg$/i', urldecode($url)) ||
  388. preg_match('/jpeg$/i', urldecode($url)) ||
  389. preg_match('/bmp$/i', urldecode($url)) ||
  390. preg_match('/svg$/i', urldecode($url)) ||
  391. (preg_match('/wav$/i', urldecode($url)) && preg_match(
  392. '/_chnano_.wav$/i',
  393. urldecode($url)
  394. ) && api_get_setting('enable_nanogong') == 'true')
  395. ) {
  396. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid; //without preview
  397. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag(
  398. $filetype,
  399. $path
  400. ).'</a>';
  401. } else {
  402. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag(
  403. $filetype,
  404. $path
  405. ).'</a>';
  406. }
  407. } else {
  408. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.build_document_icon_tag(
  409. $filetype,
  410. $path
  411. ).'</a>';
  412. }
  413. }
  414. }
  415. }
  416. /**
  417. * Builds an img html tag for the filetype
  418. *
  419. * @param string $type (file/folder)
  420. * @param string $path
  421. * @return string img html tag
  422. */
  423. function build_document_icon_tag($type, $path)
  424. {
  425. $basename = basename($path);
  426. $current_session_id = api_get_session_id();
  427. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  428. if ($type == 'file') {
  429. $icon = FileManager::choose_image($basename);
  430. if (preg_match('/_chnano_.wav$/i', $basename)) {
  431. $icon = "jplayer_play.png";
  432. $basename = 'wav'.' '.'(Nanogong)';
  433. } else {
  434. $basename = substr(strrchr($basename, '.'), 1);
  435. }
  436. } else {
  437. if ($path == '/shared_folder') {
  438. $icon = 'folder_users.gif';
  439. if ($is_allowed_to_edit) {
  440. $basename = get_lang('HelpUsersFolder');
  441. } else {
  442. $basename = get_lang('UserFolders');
  443. }
  444. } elseif (strstr($basename, 'sf_user_')) {
  445. $userinfo = api_get_user_info(substr($basename, 8));
  446. $image_path = UserManager::get_user_picture_path_by_id(substr($basename, 8), 'web', false, true);
  447. if ($image_path['file'] == 'unknown.jpg') {
  448. $icon = $image_path['file'];
  449. } else {
  450. $icon = '../upload/users/'.substr($basename, 8).'/'.$image_path['file'];
  451. }
  452. $basename = get_lang('UserFolder').' '.$userinfo['complete_name'];
  453. } elseif (strstr($path, 'shared_folder_session_')) {
  454. if ($is_allowed_to_edit) {
  455. $basename = '***('.api_get_session_name($current_session_id).')*** '.get_lang('HelpUsersFolder');
  456. } else {
  457. $basename = get_lang('UserFolders').' ('.api_get_session_name($current_session_id).')';
  458. }
  459. $icon = 'folder_users.gif';
  460. } else {
  461. $icon = 'folder_document.gif';
  462. if ($path == '/audio') {
  463. $icon = 'folder_audio.gif';
  464. if (api_is_allowed_to_edit()) {
  465. $basename = get_lang('HelpDefaultDirDocuments');
  466. } else {
  467. $basename = get_lang('Audio');
  468. }
  469. } elseif ($path == '/flash') {
  470. $icon = 'folder_flash.gif';
  471. if (api_is_allowed_to_edit()) {
  472. $basename = get_lang('HelpDefaultDirDocuments');
  473. } else {
  474. $basename = get_lang('Flash');
  475. }
  476. } elseif ($path == '/images') {
  477. $icon = 'folder_images.gif';
  478. if (api_is_allowed_to_edit()) {
  479. $basename = get_lang('HelpDefaultDirDocuments');
  480. } else {
  481. $basename = get_lang('Images');
  482. }
  483. } elseif ($path == '/video') {
  484. $icon = 'folder_video.gif';
  485. if (api_is_allowed_to_edit()) {
  486. $basename = get_lang('HelpDefaultDirDocuments');
  487. } else {
  488. $basename = get_lang('Video');
  489. }
  490. } elseif ($path == '/images/gallery') {
  491. $icon = 'folder_gallery.gif';
  492. if (api_is_allowed_to_edit()) {
  493. $basename = get_lang('HelpDefaultDirDocuments');
  494. } else {
  495. $basename = get_lang('Gallery');
  496. }
  497. } elseif ($path == '/chat_files') {
  498. $icon = 'folder_chat.gif';
  499. if (api_is_allowed_to_edit()) {
  500. $basename = get_lang('HelpFolderChat');
  501. } else {
  502. $basename = get_lang('ChatFiles');
  503. }
  504. } elseif ($path == '/learning_path') {
  505. $icon = 'folder_learningpath.gif';
  506. if (api_is_allowed_to_edit()) {
  507. $basename = get_lang('HelpFolderLearningPaths');
  508. } else {
  509. $basename = get_lang('LearningPaths');
  510. }
  511. }
  512. }
  513. }
  514. return Display::return_icon($icon, $basename, array());
  515. }
  516. /**
  517. * Creates the row of edit icons for a file/folder
  518. *
  519. * @param string $curdirpath current path (cfr open folder)
  520. * @param string $type (file/folder)
  521. * @param string $path dbase path of file/folder
  522. * @param int $visibility (1/0)
  523. * @param int $id dbase id of the document
  524. * @return string html img tags with hyperlinks
  525. */
  526. function build_edit_icons($document_data, $id, $is_template, $is_read_only = 0, $visibility)
  527. {
  528. if (isset($_SESSION['_gid'])) {
  529. $req_gid = '&gidReq='.$_SESSION['_gid'];
  530. } else {
  531. $req_gid = '';
  532. }
  533. $document_id = $document_data['id'];
  534. $type = $document_data['filetype'];
  535. $is_read_only = $document_data['readonly'];
  536. $path = $document_data['path'];
  537. $parent_id = DocumentManager::get_document_id(api_get_course_info(), dirname($path));
  538. $curdirpath = dirname($document_data['path']);
  539. $is_certificate_mode = DocumentManager::is_certificate_mode($path);
  540. $curdirpath = urlencode($curdirpath);
  541. $extension = pathinfo($path, PATHINFO_EXTENSION);
  542. // Build URL-parameters for table-sorting
  543. $sort_params = array();
  544. if (isset($_GET['column'])) {
  545. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  546. }
  547. if (isset($_GET['page_nr'])) {
  548. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  549. }
  550. if (isset($_GET['per_page'])) {
  551. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  552. }
  553. if (isset($_GET['direction'])) {
  554. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  555. }
  556. $sort_params = implode('&amp;', $sort_params);
  557. $visibility_icon = ($visibility == 0) ? 'invisible' : 'visible';
  558. $visibility_command = ($visibility == 0) ? 'set_visible' : 'set_invisible';
  559. $modify_icons = '';
  560. // If document is read only *or* we're in a session and the document
  561. // is from a non-session context, hide the edition capabilities
  562. if ($is_read_only /* or ($session_id!=api_get_session_id()) */) {
  563. if (api_is_course_admin() || api_is_platform_admin()) {
  564. if ($extension == 'svg' && api_browser_support('svg') && api_get_setting('enabled_support_svg') == 'true') {
  565. $modify_icons = '<a href="edit_draw.php?'.api_get_cidreq(
  566. ).'&id='.$document_id.$req_gid.'">'.Display::return_icon(
  567. 'edit.png',
  568. get_lang('Modify'),
  569. '',
  570. ICON_SIZE_SMALL
  571. ).'</a>';
  572. } elseif ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg' || $extension == 'bmp' || $extension == 'gif' || $extension == 'pxd' && api_get_setting(
  573. 'enabled_support_pixlr'
  574. ) == 'true'
  575. ) {
  576. $modify_icons = '<a href="edit_paint.php?'.api_get_cidreq(
  577. ).'&id='.$document_id.$req_gid.'">'.Display::return_icon(
  578. 'edit.png',
  579. get_lang('Modify'),
  580. '',
  581. ICON_SIZE_SMALL
  582. ).'</a>';
  583. } else {
  584. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq(
  585. ).'&id='.$document_id.$req_gid.'">'.Display::return_icon(
  586. 'edit.png',
  587. get_lang('Modify'),
  588. '',
  589. ICON_SIZE_SMALL
  590. ).'</a>';
  591. }
  592. } else {
  593. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  594. }
  595. $modify_icons .= '&nbsp;'.Display::return_icon('move_na.png', get_lang('Move'), array(), ICON_SIZE_SMALL);
  596. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  597. $modify_icons .= '&nbsp;'.Display::return_icon(
  598. $visibility_icon.'.png',
  599. get_lang('VisibilityCannotBeChanged'),
  600. '',
  601. ICON_SIZE_SMALL
  602. );
  603. }
  604. $modify_icons .= '&nbsp;'.Display::return_icon('delete_na.png', get_lang('Delete'), array(), ICON_SIZE_SMALL);
  605. } else {
  606. //Edit button
  607. if (in_array($path, DocumentManager::get_system_folders())) {
  608. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  609. } elseif ($is_certificate_mode) {
  610. // gradebook category doesn't seem to be taken into account
  611. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq(
  612. ).'&amp;id='.$document_id.$req_gid.'&curdirpath=/certificates">'.Display::return_icon(
  613. 'edit.png',
  614. get_lang('Modify'),
  615. '',
  616. ICON_SIZE_SMALL
  617. ).'</a>';
  618. } else {
  619. if (api_get_session_id()) {
  620. if ($document_data['session_id'] == api_get_session_id()) {
  621. if ($extension == 'svg' && api_browser_support('svg') && api_get_setting(
  622. 'enabled_support_svg'
  623. ) == 'true'
  624. ) {
  625. $modify_icons = '<a href="edit_draw.php?'.api_get_cidreq(
  626. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  627. 'edit.png',
  628. get_lang('Modify'),
  629. '',
  630. ICON_SIZE_SMALL
  631. ).'</a>';
  632. } elseif ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg' || $extension == 'bmp' || $extension == 'gif' || $extension == 'pxd' && api_get_setting(
  633. 'enabled_support_pixlr'
  634. ) == 'true'
  635. ) {
  636. $modify_icons = '<a href="edit_paint.php?'.api_get_cidreq(
  637. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  638. 'edit.png',
  639. get_lang('Modify'),
  640. '',
  641. ICON_SIZE_SMALL
  642. ).'</a>';
  643. } else {
  644. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq(
  645. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  646. 'edit.png',
  647. get_lang('Modify'),
  648. '',
  649. ICON_SIZE_SMALL
  650. ).'</a>';
  651. }
  652. } else {
  653. $modify_icons .= '&nbsp;'.Display::return_icon(
  654. 'edit_na.png',
  655. get_lang('Edit'),
  656. array(),
  657. ICON_SIZE_SMALL
  658. ).'</a>';
  659. }
  660. } else {
  661. if ($extension == 'svg' && api_browser_support('svg') && api_get_setting(
  662. 'enabled_support_svg'
  663. ) == 'true'
  664. ) {
  665. $modify_icons = '<a href="edit_draw.php?'.api_get_cidreq(
  666. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  667. 'edit.png',
  668. get_lang('Modify'),
  669. '',
  670. ICON_SIZE_SMALL
  671. ).'</a>';
  672. } elseif ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg' || $extension == 'bmp' || $extension == 'gif' || $extension == 'pxd' && api_get_setting(
  673. 'enabled_support_pixlr'
  674. ) == 'true'
  675. ) {
  676. $modify_icons = '<a href="edit_paint.php?'.api_get_cidreq(
  677. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  678. 'edit.png',
  679. get_lang('Modify'),
  680. '',
  681. ICON_SIZE_SMALL
  682. ).'</a>';
  683. } else {
  684. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq(
  685. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  686. 'edit.png',
  687. get_lang('Modify'),
  688. '',
  689. ICON_SIZE_SMALL
  690. ).'</a>';
  691. }
  692. }
  693. }
  694. //Move button
  695. if ($is_certificate_mode || in_array($path, DocumentManager::get_system_folders())) {
  696. $modify_icons .= '&nbsp;'.Display::return_icon(
  697. 'move_na.png',
  698. get_lang('Move'),
  699. array(),
  700. ICON_SIZE_SMALL
  701. ).'</a>';
  702. } else {
  703. if (api_get_session_id()) {
  704. if ($document_data['session_id'] == api_get_session_id()) {
  705. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  706. ).'&amp;id='.$parent_id.'&amp;move='.$document_id.$req_gid.'">'.Display::return_icon(
  707. 'move.png',
  708. get_lang('Move'),
  709. array(),
  710. ICON_SIZE_SMALL
  711. ).'</a>';
  712. } else {
  713. $modify_icons .= '&nbsp;'.Display::return_icon(
  714. 'move_na.png',
  715. get_lang('Move'),
  716. array(),
  717. ICON_SIZE_SMALL
  718. ).'</a>';
  719. }
  720. } else {
  721. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  722. ).'&amp;id='.$parent_id.'&amp;move='.$document_id.$req_gid.'">'.Display::return_icon(
  723. 'move.png',
  724. get_lang('Move'),
  725. array(),
  726. ICON_SIZE_SMALL
  727. ).'</a>';
  728. }
  729. }
  730. //Visibility button
  731. if ($is_certificate_mode) {
  732. $modify_icons .= '&nbsp;'.Display::return_icon(
  733. $visibility_icon.'.png',
  734. get_lang('VisibilityCannotBeChanged'),
  735. array(),
  736. ICON_SIZE_SMALL
  737. ).'</a>';
  738. } else {
  739. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  740. if ($visibility_icon == 'invisible') {
  741. $tip_visibility = get_lang('Show');
  742. } else {
  743. $tip_visibility = get_lang('Hide');
  744. }
  745. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  746. ).'&amp;id='.$parent_id.'&amp;'.$visibility_command.'='.$id.$req_gid.'&amp;'.$sort_params.'">'.Display::return_icon(
  747. $visibility_icon.'.png',
  748. $tip_visibility,
  749. '',
  750. ICON_SIZE_SMALL
  751. ).'</a>';
  752. }
  753. }
  754. //Delete button
  755. if (in_array($path, DocumentManager::get_system_folders())) {
  756. $modify_icons .= '&nbsp;'.Display::return_icon(
  757. 'delete_na.png',
  758. get_lang('ThisFolderCannotBeDeleted'),
  759. array(),
  760. ICON_SIZE_SMALL
  761. );
  762. } else {
  763. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && DocumentManager::get_default_certificate_id(
  764. api_get_course_id()
  765. ) == $id
  766. ) {
  767. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  768. ).'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode(
  769. $path
  770. ).$req_gid.'&amp;'.$sort_params.'delete_certificate_id='.$id.'" onclick="return confirmation(\''.basename(
  771. $path
  772. ).'\');">'.Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  773. } else {
  774. if ($is_certificate_mode) {
  775. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  776. ).'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode(
  777. $path
  778. ).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename(
  779. $path
  780. ).'\');">'.Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  781. } else {
  782. if (api_get_session_id()) {
  783. if ($document_data['session_id'] == api_get_session_id()) {
  784. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  785. ).'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode(
  786. $path
  787. ).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename(
  788. $path
  789. ).'\');">'.Display::return_icon(
  790. 'delete.png',
  791. get_lang('Delete'),
  792. array(),
  793. ICON_SIZE_SMALL
  794. ).'</a>';
  795. } else {
  796. $modify_icons .= '&nbsp;'.Display::return_icon(
  797. 'delete_na.png',
  798. get_lang('ThisFolderCannotBeDeleted'),
  799. array(),
  800. ICON_SIZE_SMALL
  801. );
  802. }
  803. } else {
  804. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  805. ).'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode(
  806. $path
  807. ).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename(
  808. $path
  809. ).'\');">'.Display::return_icon(
  810. 'delete.png',
  811. get_lang('Delete'),
  812. array(),
  813. ICON_SIZE_SMALL
  814. ).'</a>';
  815. }
  816. }
  817. }
  818. }
  819. }
  820. if ($type == 'file' && ($extension == 'html' || $extension == 'htm')) {
  821. if ($is_template == 0) {
  822. if ((isset($_GET['curdirpath']) && $_GET['curdirpath'] != '/certificates') || !isset($_GET['curdirpath'])) {
  823. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  824. ).'&amp;curdirpath='.$curdirpath.'&amp;add_as_template='.$id.$req_gid.'&amp;'.$sort_params.'">'.Display::return_icon(
  825. 'wizard.png',
  826. get_lang('AddAsTemplate'),
  827. array(),
  828. ICON_SIZE_SMALL
  829. ).'</a>';
  830. }
  831. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates') { //allow attach certificate to course
  832. $visibility_icon_certificate = 'nocertificate';
  833. if (DocumentManager::get_default_certificate_id(api_get_course_id()) == $id) {
  834. $visibility_icon_certificate = 'certificate';
  835. $certificate = get_lang('DefaultCertificate');
  836. $preview = get_lang('PreviewCertificate');
  837. $is_preview = true;
  838. } else {
  839. $is_preview = false;
  840. $certificate = get_lang('NoDefaultCertificate');
  841. }
  842. if (isset($_GET['selectcat'])) {
  843. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  844. ).'&amp;curdirpath='.$curdirpath.'&amp;selectcat='.Security::remove_XSS(
  845. $_GET['selectcat']
  846. ).'&amp;set_certificate='.$id.$req_gid.'&amp;'.$sort_params.'"><img src="../img/'.$visibility_icon_certificate.'.png" border="0" title="'.$certificate.'" alt="" /></a>';
  847. if ($is_preview) {
  848. $modify_icons .= '&nbsp;<a target="_blank" href="'.api_get_self().'?'.api_get_cidreq(
  849. ).'&amp;curdirpath='.$curdirpath.'&amp;set_preview='.$id.$req_gid.'&amp;'.$sort_params.'" >'.
  850. Display::return_icon('preview_view.png', $preview, '', ICON_SIZE_SMALL).'</a>';
  851. }
  852. }
  853. }
  854. } else {
  855. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  856. ).'&curdirpath='.$curdirpath.'&amp;remove_as_template='.$id.$req_gid.'&amp;'.$sort_params.'">'.
  857. Display::return_icon('wizard_na.png', get_lang('RemoveAsTemplate'), '', ICON_SIZE_SMALL).'</a>';
  858. }
  859. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  860. ).'&action=export_to_pdf&id='.$id.'">'.Display::return_icon(
  861. 'pdf.png',
  862. get_lang('Export2PDF'),
  863. array(),
  864. ICON_SIZE_SMALL
  865. ).'</a>';
  866. }
  867. return $modify_icons;
  868. }
  869. function build_move_to_selector($folders, $curdirpath, $move_file, $group_dir = '')
  870. {
  871. $form = new FormValidator('move_to', 'post', api_get_self());
  872. // Form title
  873. $form->addElement('hidden', 'move_file', $move_file);
  874. $options = array();
  875. // Group documents cannot be uploaded in the root
  876. if ($group_dir == '') {
  877. if ($curdirpath != '/') {
  878. $options['/'] = get_lang('Documents');
  879. }
  880. if (is_array($folders)) {
  881. foreach ($folders as & $folder) {
  882. //Hide some folders
  883. if ($folder == '/HotPotatoes_files' || $folder == '/certificates' || basename($folder) == 'css') {
  884. continue;
  885. }
  886. //Admin setting for Hide/Show the folders of all users
  887. if (api_get_setting('show_users_folders') == 'false' && (strstr($folder, '/shared_folder') || strstr(
  888. $folder,
  889. 'shared_folder_session_'
  890. ))
  891. ) {
  892. continue;
  893. }
  894. //Admin setting for Hide/Show Default folders to all users
  895. if (api_get_setting(
  896. 'show_default_folders'
  897. ) == 'false' && ($folder == '/images' || $folder == '/flash' || $folder == '/audio' || $folder == '/video' || strstr(
  898. $folder,
  899. '/images/gallery'
  900. ) || $folder == '/video/flv')
  901. ) {
  902. continue;
  903. }
  904. //Admin setting for Hide/Show chat history folder
  905. if (api_get_setting('show_chat_folder') == 'false' && $folder == '/chat_files') {
  906. continue;
  907. }
  908. // You cannot move a file to:
  909. // 1. current directory
  910. // 2. inside the folder you want to move
  911. // 3. inside a subfolder of the folder you want to move
  912. if (($curdirpath != $folder) && ($folder != $move_file) && (substr(
  913. $folder,
  914. 0,
  915. strlen($move_file) + 1
  916. ) != $move_file.'/')
  917. ) {
  918. $path_displayed = $folder;
  919. // If document title is used, we have to display titles instead of real paths...
  920. $path_displayed = get_titles_of_path($folder);
  921. if (empty($path_displayed)) {
  922. $path_displayed = get_lang('Untitled');
  923. }
  924. $options[$folder] = $path_displayed;
  925. //$form .= '<option value="'.$folder.'">'.$path_displayed.'</option>';
  926. }
  927. }
  928. }
  929. } else {
  930. foreach ($folders as $folder) {
  931. if (($curdirpath != $folder) && ($folder != $move_file) && (substr(
  932. $folder,
  933. 0,
  934. strlen($move_file) + 1
  935. ) != $move_file.'/')
  936. ) { // Cannot copy dir into his own subdir
  937. $path_displayed = get_titles_of_path($folder);
  938. $display_folder = substr($path_displayed, strlen($group_dir));
  939. $display_folder = ($display_folder == '') ? get_lang('Documents') : $display_folder;
  940. //$form .= '<option value="'.$folder.'">'.$display_folder.'</option>';
  941. $options[$folder] = $display_folder;
  942. }
  943. }
  944. }
  945. $form->addElement('select', 'move_to', get_lang('MoveTo'), $options);
  946. $form->addElement('button', 'move_file_submit', get_lang('MoveElement'));
  947. return $form->return_form();
  948. }
  949. /**
  950. * Gets the path translated with title of docs and folders
  951. * @param string the real path
  952. * @return the path which should be displayed
  953. */
  954. function get_titles_of_path($path)
  955. {
  956. global $tmp_folders_titles;
  957. $course_id = api_get_course_int_id();
  958. $nb_slashes = substr_count($path, '/');
  959. $tmp_path = '';
  960. $current_slash_pos = 0;
  961. $path_displayed = '';
  962. for ($i = 0; $i < $nb_slashes; $i++) {
  963. // For each folder of the path, retrieve title.
  964. $current_slash_pos = strpos($path, '/', $current_slash_pos + 1);
  965. $tmp_path = substr($path, strpos($path, '/', 0), $current_slash_pos);
  966. if (empty($tmp_path)) {
  967. // If empty, then we are in the final part of the path
  968. $tmp_path = $path;
  969. }
  970. if (!empty($tmp_folders_titles[$tmp_path])) {
  971. // If this path has soon been stored here we don't need a new query
  972. $path_displayed .= $tmp_folders_titles[$tmp_path];
  973. } else {
  974. $sql = 'SELECT title FROM '.Database::get_course_table(TABLE_DOCUMENT).'
  975. WHERE c_id = '.$course_id.' AND path LIKE BINARY "'.$tmp_path.'"';
  976. $rs = Database::query($sql);
  977. $tmp_title = '/'.Database::result($rs, 0, 0);
  978. $path_displayed .= $tmp_title;
  979. $tmp_folders_titles[$tmp_path] = $tmp_title;
  980. }
  981. }
  982. return $path_displayed;
  983. }
  984. /**
  985. * This function displays the name of the user and makes the link tothe user tool.
  986. *
  987. * @param $user_id
  988. * @param $name
  989. * @return a link to the userInfo.php
  990. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  991. * @version february 2006, dokeos 1.8
  992. */
  993. function display_user_link_document($user_id, $name)
  994. {
  995. if ($user_id != 0) {
  996. return '<a href="../user/userInfo.php?uInfo='.$user_id.'">'.$name.'</a>';
  997. } else {
  998. return get_lang('Anonymous');
  999. }
  1000. }
  1001. /**
  1002. * Creates form that asks for the directory name.
  1003. * @return string html-output text for the form
  1004. */
  1005. function create_dir_form($current_dir_id)
  1006. {
  1007. global $document_id;
  1008. $form = new FormValidator('create_dir_form', 'post', '', '', null, false);
  1009. $form->addElement('hidden', 'create_dir', 1);
  1010. $form->addElement('hidden', 'dir_id', intval($document_id));
  1011. $form->addElement('hidden', 'id', intval($current_dir_id));
  1012. $form->addElement('header', '', get_lang('CreateDir'));
  1013. $form->addElement('text', 'dirname', get_lang('NewDir'));
  1014. $form->addElement('style_submit_button', 'submit', get_lang('CreateFolder'), 'class="add"');
  1015. $new_folder_text = $form->return_form();
  1016. return $new_folder_text;
  1017. }
  1018. /**
  1019. * Checks whether the user is in shared folder
  1020. * @return return bool Return true when user is into shared folder
  1021. */
  1022. function is_shared_folder($curdirpath, $current_session_id)
  1023. {
  1024. $clean_curdirpath = Security::remove_XSS($curdirpath);
  1025. if ($clean_curdirpath == '/shared_folder') {
  1026. return true;
  1027. } elseif ($clean_curdirpath == '/shared_folder_session_'.$current_session_id) {
  1028. return true;
  1029. } else {
  1030. return false;
  1031. }
  1032. }
  1033. /**
  1034. * Checks whether the user is into any user shared folder
  1035. * @return return bool Return true when user is in any user shared folder
  1036. */
  1037. function is_any_user_shared_folder($path, $current_session_id)
  1038. {
  1039. $clean_path = Security::remove_XSS($path);
  1040. if (strpos($clean_path, 'shared_folder/sf_user_')) {
  1041. return true;
  1042. } elseif (strpos($clean_path, 'shared_folder_session_'.$current_session_id.'/sf_user_')) {
  1043. return true;
  1044. } else {
  1045. return false;
  1046. }
  1047. }
  1048. /**
  1049. * Checks whether the user is into his shared folder or into a subfolder
  1050. * @return return bool Return true when user is in his user shared folder or into a subforder
  1051. */
  1052. function is_my_shared_folder($user_id, $path, $current_session_id)
  1053. {
  1054. $clean_path = Security::remove_XSS($path).'/';
  1055. $main_user_shared_folder = '/shared_folder\/sf_user_'.$user_id.'\//'; //for security does not remove the last slash
  1056. $main_user_shared_folder_session = '/shared_folder_session_'.$current_session_id.'\/sf_user_'.$user_id.'\//'; //for security does not remove the last slash
  1057. if (preg_match($main_user_shared_folder, $clean_path)) {
  1058. return true;
  1059. } elseif (preg_match($main_user_shared_folder_session, $clean_path)) {
  1060. return true;
  1061. } else {
  1062. return false;
  1063. }
  1064. }
  1065. /**
  1066. * Check if the file name or folder searched exist
  1067. * @return return bool Return true when exist
  1068. */
  1069. function search_keyword($document_name, $keyword)
  1070. {
  1071. if (api_strripos($document_name, $keyword) !== false) {
  1072. return true;
  1073. } else {
  1074. return false;
  1075. }
  1076. }
  1077. /**
  1078. * Checks whether a document can be previewed by using the browser.
  1079. * @param string $file_extension The filename extension of the document (it must be in lower case).
  1080. * @return bool Returns TRUE or FALSE.
  1081. */
  1082. function is_browser_viewable($file_extension)
  1083. {
  1084. static $allowed_extensions = array(
  1085. 'htm',
  1086. 'html',
  1087. 'xhtml',
  1088. 'gif',
  1089. 'jpg',
  1090. 'jpeg',
  1091. 'png',
  1092. 'tif',
  1093. 'tiff',
  1094. 'pdf',
  1095. 'svg',
  1096. 'swf',
  1097. 'txt',
  1098. 'log',
  1099. 'mp4',
  1100. 'ogg',
  1101. 'ogv',
  1102. 'ogx',
  1103. 'mpg',
  1104. 'mpeg',
  1105. 'mov',
  1106. 'avi',
  1107. 'webm',
  1108. 'wmv',
  1109. 'mp3',
  1110. 'oga',
  1111. 'wav',
  1112. 'au',
  1113. 'wma',
  1114. 'mid',
  1115. 'kar'
  1116. );
  1117. /*
  1118. //TODO: make a admin swich to strict mode
  1119. 1. global default $allowed_extensions only: 'htm', 'html', 'xhtml', 'gif', 'jpg', 'jpeg', 'png', 'bmp', 'txt', 'log'
  1120. if (in_array($file_extension, $allowed_extensions)) { // Assignment + a logical check.
  1121. return true;
  1122. }
  1123. 2. check native support
  1124. 3. check plugins: quicktime, mediaplayer, vlc, acrobat, flash, java
  1125. */
  1126. if (!($result = in_array($file_extension, $allowed_extensions))) { // Assignment + a logical check.
  1127. return false;
  1128. }
  1129. //check native support (Explorer, Opera, Firefox, Chrome, Safari)
  1130. if ($file_extension == "pdf") {
  1131. return api_browser_support('pdf');
  1132. } elseif ($file_extension == "mp3") {
  1133. return api_browser_support('mp3');
  1134. } elseif ($file_extension == "mp4") {
  1135. return api_browser_support('mp4');
  1136. } elseif ($file_extension == "ogg" || $file_extension == "ogx" || $file_extension == "ogv" || $file_extension == "oga") {
  1137. return api_browser_support('ogg');
  1138. } elseif ($file_extension == "svg") {
  1139. return api_browser_support('svg');
  1140. } elseif ($file_extension == "mpg" || $file_extension == "mpeg") {
  1141. return api_browser_support('mpg');
  1142. } elseif ($file_extension == "mov") {
  1143. return api_browser_support('mov');
  1144. } elseif ($file_extension == "wav") {
  1145. return api_browser_support('wav');
  1146. } elseif ($file_extension == "mid" || $file_extension == "kar") {
  1147. return api_browser_support('mid');
  1148. } elseif ($file_extension == "avi") {
  1149. return api_browser_support('avi');
  1150. } elseif ($file_extension == "wma") {
  1151. return api_browser_support('wma');
  1152. } elseif ($file_extension == "wmv") {
  1153. return api_browser_support('wmv');
  1154. } elseif ($file_extension == "tif" || $file_extension == "tiff") {
  1155. return api_browser_support('tif');
  1156. } elseif ($file_extension == "mov") {
  1157. return api_browser_support('mov');
  1158. } elseif ($file_extension == "au") {
  1159. return api_browser_support('au');
  1160. } elseif ($file_extension == "webm") {
  1161. return api_browser_support('webm');
  1162. }
  1163. return $result;
  1164. }
  1165. function getlist($directory)
  1166. {
  1167. //global $delim, $win;
  1168. if ($d = @opendir($directory)) {
  1169. while (($filename = @readdir($d)) !== false) {
  1170. $path = $directory.$filename;
  1171. if ($filename != '.' && $filename != '..' && $filename != '.svn') {
  1172. $file = array(
  1173. "lastedit_date" => date("Y-m-d H:i:s", 9876), //date ("Y-m-d H:i:s", filemtime($path)),
  1174. "visibility" => 1,
  1175. "path" => $path,
  1176. "title" => basename($path),
  1177. "filetype" => filetype($path),
  1178. "size" => filesize($path)
  1179. );
  1180. $files[] = $file;
  1181. }
  1182. }
  1183. return $files;
  1184. } else {
  1185. return false;
  1186. }
  1187. }
  1188. function check_and_create_resource_directory($repository_path, $resource_directory, $resource_directory_name)
  1189. {
  1190. global $permissions_for_new_directories;
  1191. $resource_directory_full_path = substr($repository_path, 0, strlen($repository_path) - 1).$resource_directory.'/';
  1192. if (!is_dir($resource_directory_full_path)) {
  1193. if (@mkdir($resource_directory_full_path, $permissions_for_new_directories)) {
  1194. // While we are in a course: Registering the newly created folder in the course's database.
  1195. if (api_is_in_course()) {
  1196. global $_user;
  1197. $_course = api_get_course_info();
  1198. global $group_properties, $to_group_id;
  1199. $group_directory = !empty($group_properties['directory']) ? $group_properties['directory'] : '';
  1200. $doc_id = FileManager::add_document(
  1201. $_course,
  1202. $group_directory.$resource_directory,
  1203. 'folder',
  1204. 0,
  1205. $resource_directory_name
  1206. );
  1207. api_item_property_update(
  1208. $_course,
  1209. TOOL_DOCUMENT,
  1210. $doc_id,
  1211. 'FolderCreated',
  1212. $_user['user_id'],
  1213. $to_group_id
  1214. );
  1215. }
  1216. return true;
  1217. }
  1218. return false;
  1219. }
  1220. return true;
  1221. }