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?'.api_get_cidreq(
  309. ).'&id='.$document_data['id'].$req_gid.'&width=700&height=500';
  310. $class = 'ajax';
  311. if ($visibility == false) {
  312. $class = "ajax invisible";
  313. }
  314. 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;
  315. } else {
  316. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  317. //No plugin just the old and good showinframes.php page
  318. 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;
  319. }
  320. } else {
  321. 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;
  322. }
  323. //end copy files to users myfiles
  324. } else {
  325. //Icon column
  326. if (preg_match('/shared_folder/', urldecode($url)) && preg_match(
  327. '/shared_folder$/',
  328. urldecode($url)
  329. ) == false && preg_match('/shared_folder_session_'.$current_session_id.'$/', urldecode($url)) == false
  330. ) {
  331. if ($filetype == 'file') {
  332. //Sound preview with jplayer
  333. if (preg_match('/mp3$/i', urldecode($url)) ||
  334. (preg_match('/wav$/i', urldecode($url)) && !preg_match('/_chnano_.wav$/i', urldecode($url))) ||
  335. preg_match('/ogg$/i', urldecode($url))
  336. ) {
  337. $sound_preview = DocumentManager::generate_media_preview($counter);
  338. return $sound_preview;
  339. } elseif (
  340. //Show preview
  341. //preg_match('/html$/i', urldecode($url)) ||
  342. //preg_match('/htm$/i', urldecode($url)) ||
  343. preg_match('/swf$/i', urldecode($url)) ||
  344. preg_match('/png$/i', urldecode($url)) ||
  345. preg_match('/gif$/i', urldecode($url)) ||
  346. preg_match('/jpg$/i', urldecode($url)) ||
  347. preg_match('/jpeg$/i', urldecode($url)) ||
  348. preg_match('/bmp$/i', urldecode($url)) ||
  349. preg_match('/svg$/i', urldecode($url)) ||
  350. (preg_match('/wav$/i', urldecode($url)) && preg_match(
  351. '/_chnano_.wav$/i',
  352. urldecode($url)
  353. ) && api_get_setting('enable_nanogong') == 'true')
  354. ) {
  355. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid;
  356. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag(
  357. $filetype,
  358. $path
  359. ).Display::return_icon('shared.png', get_lang('ResourceShared'), array()).'</a>';
  360. } else {
  361. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag(
  362. $filetype,
  363. $path
  364. ).Display::return_icon('shared.png', get_lang('ResourceShared'), array()).'</a>';
  365. }
  366. } else {
  367. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.build_document_icon_tag(
  368. $filetype,
  369. $path
  370. ).Display::return_icon('shared.png', get_lang('ResourceShared'), array()).'</a>';
  371. }
  372. } else {
  373. if ($filetype == 'file') {
  374. //Sound preview with jplayer
  375. if (preg_match('/mp3$/i', urldecode($url)) ||
  376. (preg_match('/wav$/i', urldecode($url)) && !preg_match('/_chnano_.wav$/i', urldecode($url))) ||
  377. preg_match('/ogg$/i', urldecode($url))
  378. ) {
  379. $sound_preview = DocumentManager::generate_media_preview($counter);
  380. return $sound_preview;
  381. } elseif (
  382. //Show preview
  383. preg_match('/html$/i', urldecode($url)) ||
  384. preg_match('/htm$/i', urldecode($url)) ||
  385. preg_match('/swf$/i', urldecode($url)) ||
  386. preg_match('/png$/i', urldecode($url)) ||
  387. preg_match('/gif$/i', urldecode($url)) ||
  388. preg_match('/jpg$/i', urldecode($url)) ||
  389. preg_match('/jpeg$/i', urldecode($url)) ||
  390. preg_match('/bmp$/i', urldecode($url)) ||
  391. preg_match('/svg$/i', urldecode($url)) ||
  392. (preg_match('/wav$/i', urldecode($url)) && preg_match(
  393. '/_chnano_.wav$/i',
  394. urldecode($url)
  395. ) && api_get_setting('enable_nanogong') == 'true')
  396. ) {
  397. $url = 'showinframes.php?'.api_get_cidreq().'&id='.$document_data['id'].$req_gid; //without preview
  398. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag(
  399. $filetype,
  400. $path
  401. ).'</a>';
  402. } else {
  403. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.build_document_icon_tag(
  404. $filetype,
  405. $path
  406. ).'</a>';
  407. }
  408. } else {
  409. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.build_document_icon_tag(
  410. $filetype,
  411. $path
  412. ).'</a>';
  413. }
  414. }
  415. }
  416. }
  417. /**
  418. * Builds an img html tag for the filetype
  419. *
  420. * @param string $type (file/folder)
  421. * @param string $path
  422. * @return string img html tag
  423. */
  424. function build_document_icon_tag($type, $path)
  425. {
  426. $basename = basename($path);
  427. $current_session_id = api_get_session_id();
  428. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  429. if ($type == 'file') {
  430. $icon = FileManager::choose_image($basename);
  431. if (preg_match('/_chnano_.wav$/i', $basename)) {
  432. $icon = "jplayer_play.png";
  433. $basename = 'wav'.' '.'(Nanogong)';
  434. } else {
  435. $basename = substr(strrchr($basename, '.'), 1);
  436. }
  437. } else {
  438. if ($path == '/shared_folder') {
  439. $icon = 'folder_users.gif';
  440. if ($is_allowed_to_edit) {
  441. $basename = get_lang('HelpUsersFolder');
  442. } else {
  443. $basename = get_lang('UserFolders');
  444. }
  445. } elseif (strstr($basename, 'sf_user_')) {
  446. $userinfo = api_get_user_info(substr($basename, 8));
  447. $image_path = UserManager::get_user_picture_path_by_id(substr($basename, 8), 'web', false, true);
  448. if ($image_path['file'] == 'unknown.jpg') {
  449. $icon = $image_path['file'];
  450. } else {
  451. $icon = '../upload/users/'.substr($basename, 8).'/'.$image_path['file'];
  452. }
  453. $basename = get_lang('UserFolder').' '.$userinfo['complete_name'];
  454. } elseif (strstr($path, 'shared_folder_session_')) {
  455. if ($is_allowed_to_edit) {
  456. $basename = '***('.api_get_session_name($current_session_id).')*** '.get_lang('HelpUsersFolder');
  457. } else {
  458. $basename = get_lang('UserFolders').' ('.api_get_session_name($current_session_id).')';
  459. }
  460. $icon = 'folder_users.gif';
  461. } else {
  462. $icon = 'folder_document.gif';
  463. if ($path == '/audio') {
  464. $icon = 'folder_audio.gif';
  465. if (api_is_allowed_to_edit()) {
  466. $basename = get_lang('HelpDefaultDirDocuments');
  467. } else {
  468. $basename = get_lang('Audio');
  469. }
  470. } elseif ($path == '/flash') {
  471. $icon = 'folder_flash.gif';
  472. if (api_is_allowed_to_edit()) {
  473. $basename = get_lang('HelpDefaultDirDocuments');
  474. } else {
  475. $basename = get_lang('Flash');
  476. }
  477. } elseif ($path == '/images') {
  478. $icon = 'folder_images.gif';
  479. if (api_is_allowed_to_edit()) {
  480. $basename = get_lang('HelpDefaultDirDocuments');
  481. } else {
  482. $basename = get_lang('Images');
  483. }
  484. } elseif ($path == '/video') {
  485. $icon = 'folder_video.gif';
  486. if (api_is_allowed_to_edit()) {
  487. $basename = get_lang('HelpDefaultDirDocuments');
  488. } else {
  489. $basename = get_lang('Video');
  490. }
  491. } elseif ($path == '/images/gallery') {
  492. $icon = 'folder_gallery.gif';
  493. if (api_is_allowed_to_edit()) {
  494. $basename = get_lang('HelpDefaultDirDocuments');
  495. } else {
  496. $basename = get_lang('Gallery');
  497. }
  498. } elseif ($path == '/chat_files') {
  499. $icon = 'folder_chat.gif';
  500. if (api_is_allowed_to_edit()) {
  501. $basename = get_lang('HelpFolderChat');
  502. } else {
  503. $basename = get_lang('ChatFiles');
  504. }
  505. } elseif ($path == '/learning_path') {
  506. $icon = 'folder_learningpath.gif';
  507. if (api_is_allowed_to_edit()) {
  508. $basename = get_lang('HelpFolderLearningPaths');
  509. } else {
  510. $basename = get_lang('LearningPaths');
  511. }
  512. }
  513. }
  514. }
  515. return Display::return_icon($icon, $basename, array());
  516. }
  517. /**
  518. * Creates the row of edit icons for a file/folder
  519. *
  520. * @param string $curdirpath current path (cfr open folder)
  521. * @param string $type (file/folder)
  522. * @param string $path dbase path of file/folder
  523. * @param int $visibility (1/0)
  524. * @param int $id dbase id of the document
  525. * @return string html img tags with hyperlinks
  526. */
  527. function build_edit_icons($document_data, $id, $is_template, $is_read_only = 0, $visibility)
  528. {
  529. if (isset($_SESSION['_gid'])) {
  530. $req_gid = '&gidReq='.$_SESSION['_gid'];
  531. } else {
  532. $req_gid = '';
  533. }
  534. $document_id = $document_data['id'];
  535. $type = $document_data['filetype'];
  536. $is_read_only = $document_data['readonly'];
  537. $path = $document_data['path'];
  538. $parent_id = DocumentManager::get_document_id(api_get_course_info(), dirname($path));
  539. $curdirpath = dirname($document_data['path']);
  540. $is_certificate_mode = DocumentManager::is_certificate_mode($path);
  541. $curdirpath = urlencode($curdirpath);
  542. $extension = pathinfo($path, PATHINFO_EXTENSION);
  543. // Build URL-parameters for table-sorting
  544. $sort_params = array();
  545. if (isset($_GET['column'])) {
  546. $sort_params[] = 'column='.Security::remove_XSS($_GET['column']);
  547. }
  548. if (isset($_GET['page_nr'])) {
  549. $sort_params[] = 'page_nr='.Security::remove_XSS($_GET['page_nr']);
  550. }
  551. if (isset($_GET['per_page'])) {
  552. $sort_params[] = 'per_page='.Security::remove_XSS($_GET['per_page']);
  553. }
  554. if (isset($_GET['direction'])) {
  555. $sort_params[] = 'direction='.Security::remove_XSS($_GET['direction']);
  556. }
  557. $sort_params = implode('&amp;', $sort_params);
  558. $visibility_icon = ($visibility == 0) ? 'invisible' : 'visible';
  559. $visibility_command = ($visibility == 0) ? 'set_visible' : 'set_invisible';
  560. $modify_icons = '';
  561. // If document is read only *or* we're in a session and the document
  562. // is from a non-session context, hide the edition capabilities
  563. if ($is_read_only /* or ($session_id!=api_get_session_id()) */) {
  564. if (api_is_course_admin() || api_is_platform_admin()) {
  565. if ($extension == 'svg' && api_browser_support('svg') && api_get_setting('enabled_support_svg') == 'true') {
  566. $modify_icons = '<a href="edit_draw.php?'.api_get_cidreq(
  567. ).'&id='.$document_id.$req_gid.'">'.Display::return_icon(
  568. 'edit.png',
  569. get_lang('Modify'),
  570. '',
  571. ICON_SIZE_SMALL
  572. ).'</a>';
  573. } elseif ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg' || $extension == 'bmp' || $extension == 'gif' || $extension == 'pxd' && api_get_setting(
  574. 'enabled_support_pixlr'
  575. ) == 'true'
  576. ) {
  577. $modify_icons = '<a href="edit_paint.php?'.api_get_cidreq(
  578. ).'&id='.$document_id.$req_gid.'">'.Display::return_icon(
  579. 'edit.png',
  580. get_lang('Modify'),
  581. '',
  582. ICON_SIZE_SMALL
  583. ).'</a>';
  584. } else {
  585. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq(
  586. ).'&id='.$document_id.$req_gid.'">'.Display::return_icon(
  587. 'edit.png',
  588. get_lang('Modify'),
  589. '',
  590. ICON_SIZE_SMALL
  591. ).'</a>';
  592. }
  593. } else {
  594. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  595. }
  596. $modify_icons .= '&nbsp;'.Display::return_icon('move_na.png', get_lang('Move'), array(), ICON_SIZE_SMALL);
  597. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  598. $modify_icons .= '&nbsp;'.Display::return_icon(
  599. $visibility_icon.'.png',
  600. get_lang('VisibilityCannotBeChanged'),
  601. '',
  602. ICON_SIZE_SMALL
  603. );
  604. }
  605. $modify_icons .= '&nbsp;'.Display::return_icon('delete_na.png', get_lang('Delete'), array(), ICON_SIZE_SMALL);
  606. } else {
  607. //Edit button
  608. if (in_array($path, DocumentManager::get_system_folders())) {
  609. $modify_icons = Display::return_icon('edit_na.png', get_lang('Modify'), '', ICON_SIZE_SMALL);
  610. } elseif ($is_certificate_mode) {
  611. // gradebook category doesn't seem to be taken into account
  612. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq(
  613. ).'&amp;id='.$document_id.$req_gid.'&curdirpath=/certificates">'.Display::return_icon(
  614. 'edit.png',
  615. get_lang('Modify'),
  616. '',
  617. ICON_SIZE_SMALL
  618. ).'</a>';
  619. } else {
  620. if (api_get_session_id()) {
  621. if ($document_data['session_id'] == api_get_session_id()) {
  622. if ($extension == 'svg' && api_browser_support('svg') && api_get_setting(
  623. 'enabled_support_svg'
  624. ) == 'true'
  625. ) {
  626. $modify_icons = '<a href="edit_draw.php?'.api_get_cidreq(
  627. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  628. 'edit.png',
  629. get_lang('Modify'),
  630. '',
  631. ICON_SIZE_SMALL
  632. ).'</a>';
  633. } elseif ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg' || $extension == 'bmp' || $extension == 'gif' || $extension == 'pxd' && api_get_setting(
  634. 'enabled_support_pixlr'
  635. ) == 'true'
  636. ) {
  637. $modify_icons = '<a href="edit_paint.php?'.api_get_cidreq(
  638. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  639. 'edit.png',
  640. get_lang('Modify'),
  641. '',
  642. ICON_SIZE_SMALL
  643. ).'</a>';
  644. } else {
  645. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq(
  646. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  647. 'edit.png',
  648. get_lang('Modify'),
  649. '',
  650. ICON_SIZE_SMALL
  651. ).'</a>';
  652. }
  653. } else {
  654. $modify_icons .= '&nbsp;'.Display::return_icon(
  655. 'edit_na.png',
  656. get_lang('Edit'),
  657. array(),
  658. ICON_SIZE_SMALL
  659. ).'</a>';
  660. }
  661. } else {
  662. if ($extension == 'svg' && api_browser_support('svg') && api_get_setting(
  663. 'enabled_support_svg'
  664. ) == 'true'
  665. ) {
  666. $modify_icons = '<a href="edit_draw.php?'.api_get_cidreq(
  667. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  668. 'edit.png',
  669. get_lang('Modify'),
  670. '',
  671. ICON_SIZE_SMALL
  672. ).'</a>';
  673. } elseif ($extension == 'png' || $extension == 'jpg' || $extension == 'jpeg' || $extension == 'bmp' || $extension == 'gif' || $extension == 'pxd' && api_get_setting(
  674. 'enabled_support_pixlr'
  675. ) == 'true'
  676. ) {
  677. $modify_icons = '<a href="edit_paint.php?'.api_get_cidreq(
  678. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  679. 'edit.png',
  680. get_lang('Modify'),
  681. '',
  682. ICON_SIZE_SMALL
  683. ).'</a>';
  684. } else {
  685. $modify_icons = '<a href="edit_document.php?'.api_get_cidreq(
  686. ).'&amp;id='.$document_id.$req_gid.'">'.Display::return_icon(
  687. 'edit.png',
  688. get_lang('Modify'),
  689. '',
  690. ICON_SIZE_SMALL
  691. ).'</a>';
  692. }
  693. }
  694. }
  695. //Move button
  696. if ($is_certificate_mode || in_array($path, DocumentManager::get_system_folders())) {
  697. $modify_icons .= '&nbsp;'.Display::return_icon(
  698. 'move_na.png',
  699. get_lang('Move'),
  700. array(),
  701. ICON_SIZE_SMALL
  702. ).'</a>';
  703. } else {
  704. if (api_get_session_id()) {
  705. if ($document_data['session_id'] == api_get_session_id()) {
  706. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  707. ).'&amp;id='.$parent_id.'&amp;move='.$document_id.$req_gid.'">'.Display::return_icon(
  708. 'move.png',
  709. get_lang('Move'),
  710. array(),
  711. ICON_SIZE_SMALL
  712. ).'</a>';
  713. } else {
  714. $modify_icons .= '&nbsp;'.Display::return_icon(
  715. 'move_na.png',
  716. get_lang('Move'),
  717. array(),
  718. ICON_SIZE_SMALL
  719. ).'</a>';
  720. }
  721. } else {
  722. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  723. ).'&amp;id='.$parent_id.'&amp;move='.$document_id.$req_gid.'">'.Display::return_icon(
  724. 'move.png',
  725. get_lang('Move'),
  726. array(),
  727. ICON_SIZE_SMALL
  728. ).'</a>';
  729. }
  730. }
  731. //Visibility button
  732. if ($is_certificate_mode) {
  733. $modify_icons .= '&nbsp;'.Display::return_icon(
  734. $visibility_icon.'.png',
  735. get_lang('VisibilityCannotBeChanged'),
  736. array(),
  737. ICON_SIZE_SMALL
  738. ).'</a>';
  739. } else {
  740. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  741. if ($visibility_icon == 'invisible') {
  742. $tip_visibility = get_lang('Show');
  743. } else {
  744. $tip_visibility = get_lang('Hide');
  745. }
  746. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  747. ).'&amp;id='.$parent_id.'&amp;'.$visibility_command.'='.$id.$req_gid.'&amp;'.$sort_params.'">'.Display::return_icon(
  748. $visibility_icon.'.png',
  749. $tip_visibility,
  750. '',
  751. ICON_SIZE_SMALL
  752. ).'</a>';
  753. }
  754. }
  755. //Delete button
  756. if (in_array($path, DocumentManager::get_system_folders())) {
  757. $modify_icons .= '&nbsp;'.Display::return_icon(
  758. 'delete_na.png',
  759. get_lang('ThisFolderCannotBeDeleted'),
  760. array(),
  761. ICON_SIZE_SMALL
  762. );
  763. } else {
  764. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates' && DocumentManager::get_default_certificate_id(
  765. api_get_course_id()
  766. ) == $id
  767. ) {
  768. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  769. ).'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode(
  770. $path
  771. ).$req_gid.'&amp;'.$sort_params.'delete_certificate_id='.$id.'" onclick="return confirmation(\''.basename(
  772. $path
  773. ).'\');">'.Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  774. } else {
  775. if ($is_certificate_mode) {
  776. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  777. ).'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode(
  778. $path
  779. ).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename(
  780. $path
  781. ).'\');">'.Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL).'</a>';
  782. } else {
  783. if (api_get_session_id()) {
  784. if ($document_data['session_id'] == api_get_session_id()) {
  785. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  786. ).'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode(
  787. $path
  788. ).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename(
  789. $path
  790. ).'\');">'.Display::return_icon(
  791. 'delete.png',
  792. get_lang('Delete'),
  793. array(),
  794. ICON_SIZE_SMALL
  795. ).'</a>';
  796. } else {
  797. $modify_icons .= '&nbsp;'.Display::return_icon(
  798. 'delete_na.png',
  799. get_lang('ThisFolderCannotBeDeleted'),
  800. array(),
  801. ICON_SIZE_SMALL
  802. );
  803. }
  804. } else {
  805. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  806. ).'&amp;curdirpath='.$curdirpath.'&amp;delete='.urlencode(
  807. $path
  808. ).$req_gid.'&amp;'.$sort_params.'" onclick="return confirmation(\''.basename(
  809. $path
  810. ).'\');">'.Display::return_icon(
  811. 'delete.png',
  812. get_lang('Delete'),
  813. array(),
  814. ICON_SIZE_SMALL
  815. ).'</a>';
  816. }
  817. }
  818. }
  819. }
  820. }
  821. if ($type == 'file' && ($extension == 'html' || $extension == 'htm')) {
  822. if ($is_template == 0) {
  823. if ((isset($_GET['curdirpath']) && $_GET['curdirpath'] != '/certificates') || !isset($_GET['curdirpath'])) {
  824. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  825. ).'&amp;curdirpath='.$curdirpath.'&amp;add_as_template='.$id.$req_gid.'&amp;'.$sort_params.'">'.Display::return_icon(
  826. 'wizard.png',
  827. get_lang('AddAsTemplate'),
  828. array(),
  829. ICON_SIZE_SMALL
  830. ).'</a>';
  831. }
  832. if (isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates') { //allow attach certificate to course
  833. $visibility_icon_certificate = 'nocertificate';
  834. if (DocumentManager::get_default_certificate_id(api_get_course_id()) == $id) {
  835. $visibility_icon_certificate = 'certificate';
  836. $certificate = get_lang('DefaultCertificate');
  837. $preview = get_lang('PreviewCertificate');
  838. $is_preview = true;
  839. } else {
  840. $is_preview = false;
  841. $certificate = get_lang('NoDefaultCertificate');
  842. }
  843. if (isset($_GET['selectcat'])) {
  844. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  845. ).'&amp;curdirpath='.$curdirpath.'&amp;selectcat='.Security::remove_XSS(
  846. $_GET['selectcat']
  847. ).'&amp;set_certificate='.$id.$req_gid.'&amp;'.$sort_params.'"><img src="../img/'.$visibility_icon_certificate.'.png" border="0" title="'.$certificate.'" alt="" /></a>';
  848. if ($is_preview) {
  849. $modify_icons .= '&nbsp;<a target="_blank" href="'.api_get_self().'?'.api_get_cidreq(
  850. ).'&amp;curdirpath='.$curdirpath.'&amp;set_preview='.$id.$req_gid.'&amp;'.$sort_params.'" >'.
  851. Display::return_icon('preview_view.png', $preview, '', ICON_SIZE_SMALL).'</a>';
  852. }
  853. }
  854. }
  855. } else {
  856. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  857. ).'&curdirpath='.$curdirpath.'&amp;remove_as_template='.$id.$req_gid.'&amp;'.$sort_params.'">'.
  858. Display::return_icon('wizard_na.png', get_lang('RemoveAsTemplate'), '', ICON_SIZE_SMALL).'</a>';
  859. }
  860. $modify_icons .= '&nbsp;<a href="'.api_get_self().'?'.api_get_cidreq(
  861. ).'&action=export_to_pdf&id='.$id.'">'.Display::return_icon(
  862. 'pdf.png',
  863. get_lang('Export2PDF'),
  864. array(),
  865. ICON_SIZE_SMALL
  866. ).'</a>';
  867. }
  868. return $modify_icons;
  869. }
  870. function build_move_to_selector($folders, $curdirpath, $move_file, $group_dir = '')
  871. {
  872. $form = new FormValidator('move_to', 'post', api_get_self());
  873. // Form title
  874. $form->addElement('hidden', 'move_file', $move_file);
  875. $options = array();
  876. // Group documents cannot be uploaded in the root
  877. if ($group_dir == '') {
  878. if ($curdirpath != '/') {
  879. $options['/'] = get_lang('Documents');
  880. }
  881. if (is_array($folders)) {
  882. foreach ($folders as & $folder) {
  883. //Hide some folders
  884. if ($folder == '/HotPotatoes_files' || $folder == '/certificates' || basename($folder) == 'css') {
  885. continue;
  886. }
  887. //Admin setting for Hide/Show the folders of all users
  888. if (api_get_setting('show_users_folders') == 'false' && (strstr($folder, '/shared_folder') || strstr(
  889. $folder,
  890. 'shared_folder_session_'
  891. ))
  892. ) {
  893. continue;
  894. }
  895. //Admin setting for Hide/Show Default folders to all users
  896. if (api_get_setting(
  897. 'show_default_folders'
  898. ) == 'false' && ($folder == '/images' || $folder == '/flash' || $folder == '/audio' || $folder == '/video' || strstr(
  899. $folder,
  900. '/images/gallery'
  901. ) || $folder == '/video/flv')
  902. ) {
  903. continue;
  904. }
  905. //Admin setting for Hide/Show chat history folder
  906. if (api_get_setting('show_chat_folder') == 'false' && $folder == '/chat_files') {
  907. continue;
  908. }
  909. // You cannot move a file to:
  910. // 1. current directory
  911. // 2. inside the folder you want to move
  912. // 3. inside a subfolder of the folder you want to move
  913. if (($curdirpath != $folder) && ($folder != $move_file) && (substr(
  914. $folder,
  915. 0,
  916. strlen($move_file) + 1
  917. ) != $move_file.'/')
  918. ) {
  919. $path_displayed = $folder;
  920. // If document title is used, we have to display titles instead of real paths...
  921. $path_displayed = get_titles_of_path($folder);
  922. if (empty($path_displayed)) {
  923. $path_displayed = get_lang('Untitled');
  924. }
  925. $options[$folder] = $path_displayed;
  926. //$form .= '<option value="'.$folder.'">'.$path_displayed.'</option>';
  927. }
  928. }
  929. }
  930. } else {
  931. foreach ($folders as $folder) {
  932. if (($curdirpath != $folder) && ($folder != $move_file) && (substr(
  933. $folder,
  934. 0,
  935. strlen($move_file) + 1
  936. ) != $move_file.'/')
  937. ) { // Cannot copy dir into his own subdir
  938. $path_displayed = get_titles_of_path($folder);
  939. $display_folder = substr($path_displayed, strlen($group_dir));
  940. $display_folder = ($display_folder == '') ? get_lang('Documents') : $display_folder;
  941. //$form .= '<option value="'.$folder.'">'.$display_folder.'</option>';
  942. $options[$folder] = $display_folder;
  943. }
  944. }
  945. }
  946. $form->addElement('select', 'move_to', get_lang('MoveTo'), $options);
  947. $form->addElement('button', 'move_file_submit', get_lang('MoveElement'));
  948. return $form->return_form();
  949. }
  950. /**
  951. * Gets the path translated with title of docs and folders
  952. * @param string the real path
  953. * @return the path which should be displayed
  954. */
  955. function get_titles_of_path($path)
  956. {
  957. global $tmp_folders_titles;
  958. $course_id = api_get_course_int_id();
  959. $nb_slashes = substr_count($path, '/');
  960. $tmp_path = '';
  961. $current_slash_pos = 0;
  962. $path_displayed = '';
  963. for ($i = 0; $i < $nb_slashes; $i++) {
  964. // For each folder of the path, retrieve title.
  965. $current_slash_pos = strpos($path, '/', $current_slash_pos + 1);
  966. $tmp_path = substr($path, strpos($path, '/', 0), $current_slash_pos);
  967. if (empty($tmp_path)) {
  968. // If empty, then we are in the final part of the path
  969. $tmp_path = $path;
  970. }
  971. if (!empty($tmp_folders_titles[$tmp_path])) {
  972. // If this path has soon been stored here we don't need a new query
  973. $path_displayed .= $tmp_folders_titles[$tmp_path];
  974. } else {
  975. $sql = 'SELECT title FROM '.Database::get_course_table(TABLE_DOCUMENT).'
  976. WHERE c_id = '.$course_id.' AND path LIKE BINARY "'.$tmp_path.'"';
  977. $rs = Database::query($sql);
  978. $tmp_title = '/'.Database::result($rs, 0, 0);
  979. $path_displayed .= $tmp_title;
  980. $tmp_folders_titles[$tmp_path] = $tmp_title;
  981. }
  982. }
  983. return $path_displayed;
  984. }
  985. /**
  986. * This function displays the name of the user and makes the link tothe user tool.
  987. *
  988. * @param $user_id
  989. * @param $name
  990. * @return a link to the userInfo.php
  991. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  992. * @version february 2006, dokeos 1.8
  993. */
  994. function display_user_link_document($user_id, $name)
  995. {
  996. if ($user_id != 0) {
  997. return '<a href="../user/userInfo.php?uInfo='.$user_id.'">'.$name.'</a>';
  998. } else {
  999. return get_lang('Anonymous');
  1000. }
  1001. }
  1002. /**
  1003. * Creates form that asks for the directory name.
  1004. * @return string html-output text for the form
  1005. */
  1006. function create_dir_form($current_dir_id)
  1007. {
  1008. global $document_id;
  1009. $form = new FormValidator('create_dir_form', 'post', '', '', null, false);
  1010. $form->addElement('hidden', 'create_dir', 1);
  1011. $form->addElement('hidden', 'dir_id', intval($document_id));
  1012. $form->addElement('hidden', 'id', intval($current_dir_id));
  1013. $form->addElement('header', '', get_lang('CreateDir'));
  1014. $form->addElement('text', 'dirname', get_lang('NewDir'));
  1015. $form->addElement('style_submit_button', 'submit', get_lang('CreateFolder'), 'class="add"');
  1016. $new_folder_text = $form->return_form();
  1017. return $new_folder_text;
  1018. }
  1019. /**
  1020. * Checks whether the user is in shared folder
  1021. * @return return bool Return true when user is into shared folder
  1022. */
  1023. function is_shared_folder($curdirpath, $current_session_id)
  1024. {
  1025. $clean_curdirpath = Security::remove_XSS($curdirpath);
  1026. if ($clean_curdirpath == '/shared_folder') {
  1027. return true;
  1028. } elseif ($clean_curdirpath == '/shared_folder_session_'.$current_session_id) {
  1029. return true;
  1030. } else {
  1031. return false;
  1032. }
  1033. }
  1034. /**
  1035. * Checks whether the user is into any user shared folder
  1036. * @return return bool Return true when user is in any user shared folder
  1037. */
  1038. function is_any_user_shared_folder($path, $current_session_id)
  1039. {
  1040. $clean_path = Security::remove_XSS($path);
  1041. if (strpos($clean_path, 'shared_folder/sf_user_')) {
  1042. return true;
  1043. } elseif (strpos($clean_path, 'shared_folder_session_'.$current_session_id.'/sf_user_')) {
  1044. return true;
  1045. } else {
  1046. return false;
  1047. }
  1048. }
  1049. /**
  1050. * Checks whether the user is into his shared folder or into a subfolder
  1051. * @return return bool Return true when user is in his user shared folder or into a subforder
  1052. */
  1053. function is_my_shared_folder($user_id, $path, $current_session_id)
  1054. {
  1055. $clean_path = Security::remove_XSS($path).'/';
  1056. $main_user_shared_folder = '/shared_folder\/sf_user_'.$user_id.'\//'; //for security does not remove the last slash
  1057. $main_user_shared_folder_session = '/shared_folder_session_'.$current_session_id.'\/sf_user_'.$user_id.'\//'; //for security does not remove the last slash
  1058. if (preg_match($main_user_shared_folder, $clean_path)) {
  1059. return true;
  1060. } elseif (preg_match($main_user_shared_folder_session, $clean_path)) {
  1061. return true;
  1062. } else {
  1063. return false;
  1064. }
  1065. }
  1066. /**
  1067. * Check if the file name or folder searched exist
  1068. * @return return bool Return true when exist
  1069. */
  1070. function search_keyword($document_name, $keyword)
  1071. {
  1072. if (api_strripos($document_name, $keyword) !== false) {
  1073. return true;
  1074. } else {
  1075. return false;
  1076. }
  1077. }
  1078. /**
  1079. * Checks whether a document can be previewed by using the browser.
  1080. * @param string $file_extension The filename extension of the document (it must be in lower case).
  1081. * @return bool Returns TRUE or FALSE.
  1082. */
  1083. function is_browser_viewable($file_extension)
  1084. {
  1085. static $allowed_extensions = array(
  1086. 'htm',
  1087. 'html',
  1088. 'xhtml',
  1089. 'gif',
  1090. 'jpg',
  1091. 'jpeg',
  1092. 'png',
  1093. 'tif',
  1094. 'tiff',
  1095. 'pdf',
  1096. 'svg',
  1097. 'swf',
  1098. 'txt',
  1099. 'log',
  1100. 'mp4',
  1101. 'ogg',
  1102. 'ogv',
  1103. 'ogx',
  1104. 'mpg',
  1105. 'mpeg',
  1106. 'mov',
  1107. 'avi',
  1108. 'webm',
  1109. 'wmv',
  1110. 'mp3',
  1111. 'oga',
  1112. 'wav',
  1113. 'au',
  1114. 'wma',
  1115. 'mid',
  1116. 'kar'
  1117. );
  1118. /*
  1119. //TODO: make a admin swich to strict mode
  1120. 1. global default $allowed_extensions only: 'htm', 'html', 'xhtml', 'gif', 'jpg', 'jpeg', 'png', 'bmp', 'txt', 'log'
  1121. if (in_array($file_extension, $allowed_extensions)) { // Assignment + a logical check.
  1122. return true;
  1123. }
  1124. 2. check native support
  1125. 3. check plugins: quicktime, mediaplayer, vlc, acrobat, flash, java
  1126. */
  1127. if (!($result = in_array($file_extension, $allowed_extensions))) { // Assignment + a logical check.
  1128. return false;
  1129. }
  1130. //check native support (Explorer, Opera, Firefox, Chrome, Safari)
  1131. if ($file_extension == "pdf") {
  1132. return api_browser_support('pdf');
  1133. } elseif ($file_extension == "mp3") {
  1134. return api_browser_support('mp3');
  1135. } elseif ($file_extension == "mp4") {
  1136. return api_browser_support('mp4');
  1137. } elseif ($file_extension == "ogg" || $file_extension == "ogx" || $file_extension == "ogv" || $file_extension == "oga") {
  1138. return api_browser_support('ogg');
  1139. } elseif ($file_extension == "svg") {
  1140. return api_browser_support('svg');
  1141. } elseif ($file_extension == "mpg" || $file_extension == "mpeg") {
  1142. return api_browser_support('mpg');
  1143. } elseif ($file_extension == "mov") {
  1144. return api_browser_support('mov');
  1145. } elseif ($file_extension == "wav") {
  1146. return api_browser_support('wav');
  1147. } elseif ($file_extension == "mid" || $file_extension == "kar") {
  1148. return api_browser_support('mid');
  1149. } elseif ($file_extension == "avi") {
  1150. return api_browser_support('avi');
  1151. } elseif ($file_extension == "wma") {
  1152. return api_browser_support('wma');
  1153. } elseif ($file_extension == "wmv") {
  1154. return api_browser_support('wmv');
  1155. } elseif ($file_extension == "tif" || $file_extension == "tiff") {
  1156. return api_browser_support('tif');
  1157. } elseif ($file_extension == "mov") {
  1158. return api_browser_support('mov');
  1159. } elseif ($file_extension == "au") {
  1160. return api_browser_support('au');
  1161. } elseif ($file_extension == "webm") {
  1162. return api_browser_support('webm');
  1163. }
  1164. return $result;
  1165. }
  1166. function getlist($directory)
  1167. {
  1168. //global $delim, $win;
  1169. if ($d = @opendir($directory)) {
  1170. while (($filename = @readdir($d)) !== false) {
  1171. $path = $directory.$filename;
  1172. if ($filename != '.' && $filename != '..' && $filename != '.svn') {
  1173. $file = array(
  1174. "lastedit_date" => date("Y-m-d H:i:s", 9876), //date ("Y-m-d H:i:s", filemtime($path)),
  1175. "visibility" => 1,
  1176. "path" => $path,
  1177. "title" => basename($path),
  1178. "filetype" => filetype($path),
  1179. "size" => filesize($path)
  1180. );
  1181. $files[] = $file;
  1182. }
  1183. }
  1184. return $files;
  1185. } else {
  1186. return false;
  1187. }
  1188. }
  1189. function check_and_create_resource_directory($repository_path, $resource_directory, $resource_directory_name)
  1190. {
  1191. global $permissions_for_new_directories;
  1192. $resource_directory_full_path = substr($repository_path, 0, strlen($repository_path) - 1).$resource_directory.'/';
  1193. if (!is_dir($resource_directory_full_path)) {
  1194. if (@mkdir($resource_directory_full_path, $permissions_for_new_directories)) {
  1195. // While we are in a course: Registering the newly created folder in the course's database.
  1196. if (api_is_in_course()) {
  1197. global $_course, $_user;
  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. }