fileUpload.lib.php 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * FILE UPLOAD LIBRARY
  5. *
  6. * This is the file upload library for Chamilo.
  7. * Include/require it in your code to use its functionality.
  8. *
  9. * @package chamilo.library
  10. * @todo test and reorganise
  11. */
  12. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  13. /**
  14. * Changes the file name extension from .php to .phps
  15. * Useful for securing a site.
  16. *
  17. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  18. * @param - file_name (string) name of a file
  19. * @return - the filenam phps'ized
  20. */
  21. function php2phps($file_name) {
  22. return preg_replace('/\.(php.?|phtml.?)(\.){0,1}.*$/i', '.phps', $file_name);
  23. }
  24. /**
  25. * Renames .htaccess & .HTACCESS to htaccess.txt
  26. *
  27. * @param string $filename
  28. * @return string
  29. */
  30. function htaccess2txt($filename) {
  31. return str_replace(array('.htaccess', '.HTACCESS'), array('htaccess.txt', 'htaccess.txt'), $filename);
  32. }
  33. /**
  34. * This function executes our safety precautions
  35. * more functions can be added
  36. *
  37. * @param string $filename
  38. * @return string
  39. * @see php2phps()
  40. * @see htaccess2txt()
  41. */
  42. function disable_dangerous_file($filename) {
  43. return htaccess2txt(php2phps($filename));
  44. }
  45. /**
  46. * This function generates a unique name for a file on a given location
  47. * file names are changed to name_#.ext
  48. *
  49. * @param string $path
  50. * @param string $name
  51. *
  52. * @deprecated
  53. *
  54. * @return string new unique name
  55. */
  56. function unique_name($path, $name)
  57. {
  58. $ext = substr(strrchr($name, '.'), 0);
  59. $name_no_ext = substr($name, 0, strlen($name) - strlen(strstr($name, $ext)));
  60. $n = 0;
  61. $unique = '';
  62. while (file_exists($path . $name_no_ext . $unique . $ext)) {
  63. $unique = '_' . ++$n;
  64. }
  65. return $name_no_ext . $unique . $ext;
  66. }
  67. /**
  68. * Returns the name without extension, used for the title
  69. *
  70. * @param string $name
  71. * @return name without the extension
  72. */
  73. function get_document_title($name) {
  74. // If they upload .htaccess...
  75. $name = disable_dangerous_file($name);
  76. $ext = substr(strrchr($name, '.'), 0);
  77. return substr($name, 0, strlen($name) - strlen(strstr($name, $ext)));
  78. }
  79. /**
  80. * This function checks if the upload succeeded
  81. *
  82. * @param array $uploaded_file ($_FILES)
  83. * @return true if upload succeeded
  84. */
  85. function process_uploaded_file($uploaded_file, $show_output = true)
  86. {
  87. // Checking the error code sent with the file upload.
  88. if (isset($uploaded_file['error'])) {
  89. switch ($uploaded_file['error']) {
  90. case 1:
  91. // The uploaded file exceeds the upload_max_filesize directive in php.ini.
  92. if ($show_output) {
  93. Display::display_error_message(get_lang('UplExceedMaxServerUpload').ini_get('upload_max_filesize'));
  94. }
  95. return false;
  96. case 2:
  97. // The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
  98. // Not used at the moment, but could be handy if we want to limit the size of an upload (e.g. image upload in html editor).
  99. $max_file_size = intval($_POST['MAX_FILE_SIZE']);
  100. if ($show_output) {
  101. Display::display_error_message(get_lang('UplExceedMaxPostSize'). format_file_size($max_file_size));
  102. }
  103. return false;
  104. case 3:
  105. // The uploaded file was only partially uploaded.
  106. if ($show_output) {
  107. Display::display_error_message(get_lang('UplPartialUpload').' '.get_lang('PleaseTryAgain'));
  108. }
  109. return false;
  110. case 4:
  111. // No file was uploaded.
  112. if ($show_output) {
  113. Display::display_error_message(get_lang('UplNoFileUploaded').' '. get_lang('UplSelectFileFirst'));
  114. }
  115. return false;
  116. }
  117. }
  118. if (!file_exists($uploaded_file['tmp_name'])) {
  119. // No file was uploaded.
  120. if ($show_output) {
  121. Display::display_error_message(get_lang('UplUploadFailed'));
  122. }
  123. return false;
  124. }
  125. if (file_exists($uploaded_file['tmp_name'])) {
  126. $filesize = filesize($uploaded_file['tmp_name']);
  127. if (empty($filesize)) {
  128. // No file was uploaded.
  129. if ($show_output) {
  130. Display::display_error_message(get_lang('UplUploadFailedSizeIsZero'));
  131. }
  132. return false;
  133. }
  134. }
  135. $course_id = api_get_course_id();
  136. //Checking course quota if we are in a course
  137. if (!empty($course_id)) {
  138. $max_filled_space = DocumentManager::get_course_quota();
  139. // Check if there is enough space to save the file
  140. if (!DocumentManager::enough_space($uploaded_file['size'], $max_filled_space)) {
  141. if ($show_output) {
  142. Display::display_error_message(get_lang('UplNotEnoughSpace'));
  143. }
  144. return false;
  145. }
  146. }
  147. // case 0: default: We assume there is no error, the file uploaded with success.
  148. return true;
  149. }
  150. /**
  151. * This function does the save-work for the documents.
  152. * It handles the uploaded file and adds the properties to the database
  153. * If unzip=1 and the file is a zipfile, it is extracted
  154. * If we decide to save ALL kinds of documents in one database,
  155. * we could extend this with a $type='document', 'scormdocument',...
  156. *
  157. * @param array $courseInfo
  158. * @param array $uploadedFile ($_FILES)
  159. * array(
  160. * 'name' => 'picture.jpg',
  161. * 'tmp_name' => '...', // absolute path
  162. * );
  163. * @param string $documentDir Example: /var/www/chamilo/courses/ABC/document
  164. * @param string $uploadPath Example: /folder1/folder2/
  165. * @param int $userId
  166. * @param int $groupId, 0 for everybody
  167. * @param int $toUserId, NULL for everybody
  168. * @param int $unzip 1/0
  169. * @param string $whatIfFileExists overwrite, rename or warn if exists (default)
  170. * @param boolean $output Optional output parameter.
  171. * @param bool $onlyUploadFile
  172. * @param string $comment
  173. * @param int $sessionId
  174. *
  175. * So far only use for unzip_uploaded_document function.
  176. * If no output wanted on success, set to false.
  177. * @param string $comment
  178. * @return string path of the saved file
  179. */
  180. function handle_uploaded_document(
  181. $courseInfo,
  182. $uploadedFile,
  183. $documentDir,
  184. $uploadPath,
  185. $userId,
  186. $groupId = 0,
  187. $toUserId = null,
  188. $unzip = 0,
  189. $whatIfFileExists = '',
  190. $output = true,
  191. $onlyUploadFile = false,
  192. $comment = null,
  193. $sessionId = null
  194. ) {
  195. if (!$userId) {
  196. return false;
  197. }
  198. $userInfo = api_get_user_info();
  199. $uploadedFile['name'] = stripslashes($uploadedFile['name']);
  200. // Add extension to files without one (if possible)
  201. $uploadedFile['name'] = add_ext_on_mime($uploadedFile['name'], $uploadedFile['type']);
  202. if (empty($sessionId)) {
  203. $sessionId = api_get_session_id();
  204. } else {
  205. $sessionId = intval($sessionId);
  206. }
  207. // Just in case process_uploaded_file is not called
  208. $maxSpace = DocumentManager::get_course_quota();
  209. // Check if there is enough space to save the file
  210. if (!DocumentManager::enough_space($uploadedFile['size'], $maxSpace)) {
  211. if ($output) {
  212. Display::display_error_message(get_lang('UplNotEnoughSpace'));
  213. }
  214. return false;
  215. }
  216. // If the want to unzip, check if the file has a .zip (or ZIP,Zip,ZiP,...) extension
  217. if ($unzip == 1 && preg_match('/.zip$/', strtolower($uploadedFile['name']))) {
  218. return unzip_uploaded_document(
  219. $courseInfo,
  220. $userInfo,
  221. $uploadedFile,
  222. $uploadPath,
  223. $documentDir,
  224. $maxSpace,
  225. $sessionId,
  226. $groupId,
  227. $output
  228. );
  229. } elseif ($unzip == 1 && !preg_match('/.zip$/', strtolower($uploadedFile['name']))) {
  230. // We can only unzip ZIP files (no gz, tar,...)
  231. if ($output) {
  232. Display::display_error_message(
  233. get_lang('UplNotAZip')." ".get_lang('PleaseTryAgain')
  234. );
  235. }
  236. return false;
  237. } else {
  238. // Clean up the name, only ASCII characters should stay. (and strict)
  239. $cleanName = replace_dangerous_char($uploadedFile['name'], 'strict');
  240. // No "dangerous" files
  241. $cleanName = disable_dangerous_file($cleanName);
  242. // Checking file extension
  243. if (!filter_extension($cleanName)) {
  244. if ($output) {
  245. Display::display_error_message(
  246. get_lang('UplUnableToSaveFileFilteredExtension')
  247. );
  248. }
  249. return false;
  250. } else {
  251. // If the upload path differs from / (= root) it will need a slash at the end
  252. if ($uploadPath != '/') {
  253. $uploadPath = $uploadPath.'/';
  254. }
  255. // Full path to where we want to store the file with trailing slash
  256. $whereToSave = $documentDir.$uploadPath;
  257. // At least if the directory doesn't exist, tell so
  258. if (!is_dir($whereToSave)) {
  259. if (!mkdir($whereToSave, api_get_permissions_for_new_directories())) {
  260. if ($output) {
  261. Display::display_error_message(
  262. get_lang('DestDirectoryDoesntExist').' ('.$uploadPath.')'
  263. );
  264. }
  265. return false;
  266. }
  267. }
  268. // Just upload the file "as is"
  269. if ($onlyUploadFile) {
  270. $errorResult = moveUploadedFile($uploadedFile, $whereToSave.$cleanName);
  271. if ($errorResult) {
  272. return $whereToSave.$cleanName;
  273. } else {
  274. return $errorResult;
  275. }
  276. }
  277. /*
  278. Based in the clean name we generate a new filesystem name
  279. Using the session_id and group_id if values are not empty
  280. */
  281. /*$fileExists = DocumentManager::documentExists(
  282. $uploadPath.$cleanName,
  283. $courseInfo,
  284. $sessionId,
  285. $groupId
  286. );*/
  287. $fileSystemName = DocumentManager::fixDocumentName(
  288. $cleanName,
  289. 'file',
  290. $courseInfo,
  291. $sessionId,
  292. $groupId
  293. );
  294. // Name of the document without the extension (for the title)
  295. $documentTitle = get_document_title($uploadedFile['name']);
  296. // Size of the uploaded file (in bytes)
  297. $fileSize = $uploadedFile['size'];
  298. // File permissions
  299. $filePermissions = api_get_permissions_for_new_files();
  300. // Example: /var/www/chamilo/courses/xxx/document/folder/picture.jpg
  301. $fullPath = $whereToSave.$fileSystemName;
  302. // Example: /folder/picture.jpg
  303. $filePath = $uploadPath.$fileSystemName;
  304. $docId = DocumentManager::get_document_id(
  305. $courseInfo,
  306. $filePath,
  307. $sessionId
  308. );
  309. $documentList = DocumentManager::getDocumentByPathInCourse(
  310. $courseInfo,
  311. $filePath //$filePath
  312. );
  313. // This means that the path already exists in this course.
  314. if (!empty($documentList) && $whatIfFileExists != 'overwrite') {
  315. //$found = false;
  316. // Checking if we are talking about the same course + session
  317. /*foreach ($documentList as $document) {
  318. if ($document['session_id'] == $sessionId) {
  319. $found = true;
  320. break;
  321. }
  322. }*/
  323. //if ($found == false) {
  324. $whatIfFileExists = 'rename';
  325. //}
  326. }
  327. // What to do if the target file exists
  328. switch ($whatIfFileExists) {
  329. // Overwrite the file if it exists
  330. case 'overwrite':
  331. // Check if the target file exists, so we can give another message
  332. $fileExists = file_exists($fullPath);
  333. if (moveUploadedFile($uploadedFile, $fullPath)) {
  334. chmod($fullPath, $filePermissions);
  335. if ($fileExists && $docId) {
  336. // UPDATE DATABASE
  337. $documentId = DocumentManager::get_document_id(
  338. $courseInfo,
  339. $filePath
  340. );
  341. if (is_numeric($documentId)) {
  342. // Update file size
  343. update_existing_document(
  344. $courseInfo,
  345. $documentId,
  346. $uploadedFile['size']
  347. );
  348. // Update document item_property
  349. api_item_property_update(
  350. $courseInfo,
  351. TOOL_DOCUMENT,
  352. $documentId,
  353. 'DocumentUpdated',
  354. $userId,
  355. $groupId,
  356. $toUserId,
  357. null,
  358. null,
  359. $sessionId
  360. );
  361. // Redo visibility
  362. api_set_default_visibility($documentId, TOOL_DOCUMENT);
  363. } else {
  364. // There might be cases where the file exists on disk but there is no registration of that in the database
  365. // In this case, and if we are in overwrite mode, overwrite and create the db record
  366. $documentId = add_document(
  367. $courseInfo,
  368. $filePath,
  369. 'file',
  370. $fileSize,
  371. $documentTitle,
  372. $comment,
  373. 0,
  374. true,
  375. $groupId,
  376. $sessionId
  377. );
  378. if ($documentId) {
  379. // Put the document in item_property update
  380. api_item_property_update(
  381. $courseInfo,
  382. TOOL_DOCUMENT,
  383. $documentId,
  384. 'DocumentAdded',
  385. $userId,
  386. $groupId,
  387. $toUserId,
  388. null,
  389. null,
  390. $sessionId
  391. );
  392. // Redo visibility
  393. api_set_default_visibility($documentId, TOOL_DOCUMENT);
  394. }
  395. }
  396. // If the file is in a folder, we need to update all parent folders
  397. item_property_update_on_folder($courseInfo, $uploadPath, $userId);
  398. // Display success message with extra info to user
  399. if ($output) {
  400. Display::display_confirmation_message(
  401. get_lang('UplUploadSucceeded') . '<br /> ' . $documentTitle . ' ' . get_lang('UplFileOverwritten'),
  402. false
  403. );
  404. }
  405. return $filePath;
  406. } else {
  407. // Put the document data in the database
  408. $documentId = add_document(
  409. $courseInfo,
  410. $filePath,
  411. 'file',
  412. $fileSize,
  413. $documentTitle,
  414. $comment,
  415. 0,
  416. true,
  417. $groupId,
  418. $sessionId
  419. );
  420. if ($documentId) {
  421. // Put the document in item_property update
  422. api_item_property_update(
  423. $courseInfo,
  424. TOOL_DOCUMENT,
  425. $documentId,
  426. 'DocumentAdded',
  427. $userId,
  428. $groupId,
  429. $toUserId,
  430. null,
  431. null,
  432. $sessionId
  433. );
  434. // Redo visibility
  435. api_set_default_visibility($documentId, TOOL_DOCUMENT);
  436. }
  437. // If the file is in a folder, we need to update all parent folders
  438. item_property_update_on_folder($courseInfo, $uploadPath, $userId);
  439. // Display success message to user
  440. if ($output) {
  441. Display::display_confirmation_message(get_lang('UplUploadSucceeded').'<br /> '.$documentTitle, false);
  442. }
  443. return $filePath;
  444. }
  445. } else {
  446. if ($output) {
  447. Display::display_error_message(get_lang('UplUnableToSaveFile'));
  448. }
  449. return false;
  450. }
  451. break;
  452. // Rename the file if it exists
  453. case 'rename':
  454. // Always rename.
  455. $cleanName = DocumentManager::getUniqueFileName(
  456. $uploadPath,
  457. $cleanName,
  458. $courseInfo,
  459. $sessionId,
  460. $groupId
  461. );
  462. $fileSystemName = DocumentManager::fixDocumentName(
  463. $cleanName,
  464. 'file',
  465. $courseInfo,
  466. $sessionId,
  467. $groupId
  468. );
  469. $documentTitle = get_document_title($cleanName);
  470. $fullPath = $whereToSave.$fileSystemName;
  471. $filePath = $uploadPath.$fileSystemName;
  472. if (moveUploadedFile($uploadedFile, $fullPath)) {
  473. chmod($fullPath, $filePermissions);
  474. // Put the document data in the database
  475. $documentId = add_document(
  476. $courseInfo,
  477. $filePath,
  478. 'file',
  479. $fileSize,
  480. $documentTitle,
  481. $comment, // comment
  482. 0, // read only
  483. true, // save visibility
  484. $groupId,
  485. $sessionId
  486. );
  487. if ($documentId) {
  488. // Update document item_property
  489. api_item_property_update(
  490. $courseInfo,
  491. TOOL_DOCUMENT,
  492. $documentId,
  493. 'DocumentAdded',
  494. $userId,
  495. $groupId,
  496. $toUserId,
  497. null,
  498. null,
  499. $sessionId
  500. );
  501. // Redo visibility
  502. api_set_default_visibility($documentId, TOOL_DOCUMENT);
  503. }
  504. // If the file is in a folder, we need to update all parent folders
  505. item_property_update_on_folder($courseInfo, $uploadPath, $userId);
  506. // Display success message to user
  507. if ($output) {
  508. Display::display_confirmation_message(
  509. get_lang('UplUploadSucceeded') . '<br />' . get_lang('UplFileSavedAs') .' '.$documentTitle,
  510. false
  511. );
  512. }
  513. return $filePath;
  514. } else {
  515. if ($output) {
  516. Display::display_error_message(get_lang('UplUnableToSaveFile'));
  517. }
  518. return false;
  519. }
  520. break;
  521. default:
  522. // Only save the file if it doesn't exist or warn user if it does exist
  523. if (file_exists($fullPath) && $docId) {
  524. if ($output) {
  525. Display::display_error_message($cleanName.' '.get_lang('UplAlreadyExists'));
  526. }
  527. } else {
  528. if (moveUploadedFile($uploadedFile, $fullPath)) {
  529. chmod($fullPath, $filePermissions);
  530. // Put the document data in the database
  531. $documentId = add_document(
  532. $courseInfo,
  533. $filePath,
  534. 'file',
  535. $fileSize,
  536. $documentTitle,
  537. $comment,
  538. 0,
  539. true,
  540. $groupId,
  541. $sessionId
  542. );
  543. if ($documentId) {
  544. // Update document item_property
  545. api_item_property_update(
  546. $courseInfo,
  547. TOOL_DOCUMENT,
  548. $documentId,
  549. 'DocumentAdded',
  550. $userId,
  551. $groupId,
  552. $toUserId,
  553. null,
  554. null,
  555. $sessionId
  556. );
  557. // Redo visibility
  558. api_set_default_visibility($documentId, TOOL_DOCUMENT);
  559. }
  560. // If the file is in a folder, we need to update all parent folders
  561. item_property_update_on_folder(
  562. $courseInfo,
  563. $uploadPath,
  564. $userId
  565. );
  566. // Display success message to user
  567. if ($output) {
  568. Display::display_confirmation_message(
  569. get_lang('UplUploadSucceeded').'<br /> '.$documentTitle,
  570. false
  571. );
  572. }
  573. return $filePath;
  574. } else {
  575. if ($output) {
  576. Display::display_error_message(get_lang('UplUnableToSaveFile'));
  577. }
  578. return false;
  579. }
  580. }
  581. break;
  582. }
  583. }
  584. }
  585. }
  586. /**
  587. * @param string $file
  588. * @param string $storePath
  589. *
  590. * @return bool
  591. */
  592. function moveUploadedFile($file, $storePath)
  593. {
  594. $handleFromFile = isset($file['from_file']) && $file['from_file'] ? true : false;
  595. $moveFile = isset($file['move_file']) && $file['move_file'] ? true : false;
  596. if ($moveFile) {
  597. copy($file['tmp_name'], $storePath);
  598. }
  599. if ($handleFromFile) {
  600. return file_exists($file['tmp_name']);
  601. } else {
  602. return move_uploaded_file($file['tmp_name'], $storePath);
  603. }
  604. }
  605. /**
  606. * Checks if there is enough place to add a file on a directory
  607. * on the base of a maximum directory size allowed
  608. * deprecated: use enough_space instead!
  609. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  610. * @param - file_size (int) - size of the file in byte
  611. * @param - dir (string) - Path of the directory
  612. * whe the file should be added
  613. * @param - max_dir_space (int) - maximum size of the diretory in byte
  614. * @return - boolean true if there is enough space,
  615. * boolean false otherwise
  616. *
  617. * @see - enough_size() uses dir_total_space() function
  618. */
  619. function enough_size($file_size, $dir, $max_dir_space)
  620. {
  621. // If the directory is the archive directory, safely ignore the size limit
  622. if (api_get_path(SYS_ARCHIVE_PATH) == $dir) {
  623. return true;
  624. }
  625. if ($max_dir_space) {
  626. $already_filled_space = dir_total_space($dir);
  627. if (($file_size + $already_filled_space) > $max_dir_space) {
  628. return false;
  629. }
  630. }
  631. return true;
  632. }
  633. /**
  634. * Computes the size already occupied by a directory and is subdirectories
  635. *
  636. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  637. * @param - dir_path (string) - size of the file in byte
  638. * @return - int - return the directory size in bytes
  639. */
  640. function dir_total_space($dir_path)
  641. {
  642. $save_dir = getcwd();
  643. chdir($dir_path) ;
  644. $handle = opendir($dir_path);
  645. $sumSize = 0;
  646. $dirList = array();
  647. while ($element = readdir($handle)) {
  648. if ( $element == '.' || $element == '..') {
  649. continue; // Skip the current and parent directories
  650. }
  651. if (is_file($element)) {
  652. $sumSize += filesize($element);
  653. }
  654. if (is_dir($element)) {
  655. $dirList[] = $dir_path.'/'.$element;
  656. }
  657. }
  658. closedir($handle) ;
  659. if (sizeof($dirList) > 0) {
  660. foreach ($dirList as $j) {
  661. $sizeDir = dir_total_space($j); // Recursivity
  662. $sumSize += $sizeDir;
  663. }
  664. }
  665. chdir($save_dir); // Return to initial position
  666. return $sumSize;
  667. }
  668. /**
  669. * Tries to add an extension to files without extension
  670. * Some applications on Macintosh computers don't add an extension to the files.
  671. * This subroutine try to fix this on the basis of the MIME type sent
  672. * by the browser.
  673. *
  674. * Note : some browsers don't send the MIME Type (e.g. Netscape 4).
  675. * We don't have solution for this kind of situation
  676. *
  677. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  678. * @author - Bert Vanderkimpen
  679. * @param - file_name (string) - Name of the file
  680. * @param - file_type (string) - Type of the file
  681. * @return - file_name (string)
  682. */
  683. function add_ext_on_mime($file_name, $file_type)
  684. {
  685. // Check whether the file has an extension AND whether the browser has sent a MIME Type
  686. if (!preg_match('/^.*\.[a-zA-Z_0-9]+$/', $file_name) && $file_type) {
  687. // Build a "MIME-types / extensions" connection table
  688. static $mime_type = array();
  689. $mime_type[] = 'application/msword'; $extension[] = '.doc';
  690. $mime_type[] = 'application/rtf'; $extension[] = '.rtf';
  691. $mime_type[] = 'application/vnd.ms-powerpoint'; $extension[] = '.ppt';
  692. $mime_type[] = 'application/vnd.ms-excel'; $extension[] = '.xls';
  693. $mime_type[] = 'application/pdf'; $extension[] = '.pdf';
  694. $mime_type[] = 'application/postscript'; $extension[] = '.ps';
  695. $mime_type[] = 'application/mac-binhex40'; $extension[] = '.hqx';
  696. $mime_type[] = 'application/x-gzip'; $extension[] = 'tar.gz';
  697. $mime_type[] = 'application/x-shockwave-flash'; $extension[] = '.swf';
  698. $mime_type[] = 'application/x-stuffit'; $extension[] = '.sit';
  699. $mime_type[] = 'application/x-tar'; $extension[] = '.tar';
  700. $mime_type[] = 'application/zip'; $extension[] = '.zip';
  701. $mime_type[] = 'application/x-tar'; $extension[] = '.tar';
  702. $mime_type[] = 'text/html'; $extension[] = '.html';
  703. $mime_type[] = 'text/plain'; $extension[] = '.txt';
  704. $mime_type[] = 'text/rtf'; $extension[] = '.rtf';
  705. $mime_type[] = 'img/gif'; $extension[] = '.gif';
  706. $mime_type[] = 'img/jpeg'; $extension[] = '.jpg';
  707. $mime_type[] = 'img/png'; $extension[] = '.png';
  708. $mime_type[] = 'audio/midi'; $extension[] = '.mid';
  709. $mime_type[] = 'audio/mpeg'; $extension[] = '.mp3';
  710. $mime_type[] = 'audio/x-aiff'; $extension[] = '.aif';
  711. $mime_type[] = 'audio/x-pn-realaudio'; $extension[] = '.rm';
  712. $mime_type[] = 'audio/x-pn-realaudio-plugin'; $extension[] = '.rpm';
  713. $mime_type[] = 'audio/x-wav'; $extension[] = '.wav';
  714. $mime_type[] = 'video/mpeg'; $extension[] = '.mpg';
  715. $mime_type[] = 'video/mpeg4-generic'; $extension[] = '.mp4';
  716. $mime_type[] = 'video/quicktime'; $extension[] = '.mov';
  717. $mime_type[] = 'video/x-msvideo'; $extension[] = '.avi';
  718. $mime_type[] = 'video/x-ms-wmv'; $extension[] = '.wmv';
  719. $mime_type[] = 'video/x-flv'; $extension[] = '.flv';
  720. $mime_type[] = 'image/svg+xml'; $extension[] = '.svg';
  721. $mime_type[] = 'image/svg+xml'; $extension[] = '.svgz';
  722. $mime_type[] = 'video/ogg'; $extension[] = '.ogv';
  723. $mime_type[] = 'audio/ogg'; $extension[] = '.oga';
  724. $mime_type[] = 'application/ogg'; $extension[] = '.ogg';
  725. $mime_type[] = 'application/ogg'; $extension[] = '.ogx';
  726. $mime_type[] = 'application/x-freemind'; $extension[] = '.mm';
  727. $mime_type[] = 'application/vnd.ms-word.document.macroEnabled.12'; $extension[] = '.docm';
  728. $mime_type[] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; $extension[] = '.docx';
  729. $mime_type[] = 'application/vnd.ms-word.template.macroEnabled.12'; $extension[] = '.dotm';
  730. $mime_type[] = 'application/vnd.openxmlformats-officedocument.wordprocessingml.template'; $extension[] = '.dotx';
  731. $mime_type[] = 'application/vnd.ms-powerpoint.template.macroEnabled.12'; $extension[] = '.potm';
  732. $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.template'; $extension[] = '.potx';
  733. $mime_type[] = 'application/vnd.ms-powerpoint.addin.macroEnabled.12'; $extension[] = '.ppam';
  734. $mime_type[] = 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12'; $extension[] = '.ppsm';
  735. $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.slideshow'; $extension[] = '.ppsx';
  736. $mime_type[] = 'application/vnd.ms-powerpoint.presentation.macroEnabled.12'; $extension[] = '.pptm';
  737. $mime_type[] = 'application/vnd.openxmlformats-officedocument.presentationml.presentation'; $extension[] = '.pptx';
  738. $mime_type[] = 'application/vnd.ms-excel.addin.macroEnabled.12'; $extension[] = '.xlam';
  739. $mime_type[] = 'application/vnd.ms-excel.sheet.binary.macroEnabled.12'; $extension[] = '.xlsb';
  740. $mime_type[] = 'application/vnd.ms-excel.sheet.macroEnabled.12'; $extension[] = '.xlsm';
  741. $mime_type[] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; $extension[] = '.xlsx';
  742. $mime_type[] = 'application/vnd.ms-excel.template.macroEnabled.12'; $extension[] = '.xltm';
  743. $mime_type[] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.template'; $extension[] = '.xltx';
  744. // Test on PC (files with no extension get application/octet-stream)
  745. //$mime_type[] = 'application/octet-stream'; $extension[] = '.ext';
  746. // Check whether the MIME type sent by the browser is within the table
  747. foreach ($mime_type as $key => & $type) {
  748. if ($type == $file_type) {
  749. $file_name .= $extension[$key];
  750. break;
  751. }
  752. }
  753. unset($mime_type, $extension, $type, $key); // Delete to eschew possible collisions
  754. }
  755. return $file_name;
  756. }
  757. /**
  758. *
  759. * @author Hugues Peeters <hugues.peeters@claroline.net>
  760. *
  761. * @param array $uploaded_file - follows the $_FILES Structure
  762. * @param string $base_work_dir - base working directory of the module
  763. * @param string $upload_path - destination of the upload.
  764. * This path is to append to $base_work_dir
  765. * @param int $max_filled_space - amount of bytes to not exceed in the base
  766. * working directory
  767. *
  768. * @return boolean true if it succeds, false otherwise
  769. */
  770. function treat_uploaded_file($uploaded_file, $base_work_dir, $upload_path, $max_filled_space, $uncompress = '')
  771. {
  772. $uploaded_file['name'] = stripslashes($uploaded_file['name']);
  773. if (!enough_size($uploaded_file['size'], $base_work_dir, $max_filled_space)) {
  774. return api_failure::set_failure('not_enough_space');
  775. }
  776. if ($uncompress == 'unzip' && preg_match('/.zip$/', strtolower($uploaded_file['name']))) {
  777. return unzip_uploaded_file($uploaded_file, $upload_path, $base_work_dir, $max_filled_space);
  778. } else {
  779. $file_name = trim($uploaded_file['name']);
  780. // CHECK FOR NO DESIRED CHARACTERS
  781. $file_name = replace_dangerous_char($file_name, 'strict');
  782. // TRY TO ADD AN EXTENSION TO FILES WITOUT EXTENSION
  783. $file_name = add_ext_on_mime($file_name, $uploaded_file['type']);
  784. // HANDLE PHP FILES
  785. $file_name = ($file_name);
  786. // COPY THE FILE TO THE DESIRED DESTINATION
  787. if (move_uploaded_file($uploaded_file['tmp_name'], $base_work_dir.$upload_path.'/'.$file_name)) {
  788. set_default_settings($upload_path, $file_name);
  789. }
  790. return true;
  791. }
  792. }
  793. /**
  794. * Manages all the unzipping process of an uploaded file
  795. *
  796. * @author Hugues Peeters <hugues.peeters@claroline.net>
  797. *
  798. * @param array $uploaded_file - follows the $_FILES Structure
  799. * @param string $upload_path - destination of the upload.
  800. * This path is to append to $base_work_dir
  801. * @param string $base_work_dir - base working directory of the module
  802. * @param int $max_filled_space - amount of bytes to not exceed in the base
  803. * working directory
  804. *
  805. * @return boolean true if it succeeds false otherwise
  806. */
  807. function unzip_uploaded_file($uploaded_file, $upload_path, $base_work_dir, $max_filled_space)
  808. {
  809. $zip_file = new PclZip($uploaded_file['tmp_name']);
  810. // Check the zip content (real size and file extension)
  811. if (file_exists($uploaded_file['tmp_name'])) {
  812. $zip_content_array = $zip_file->listContent();
  813. $ok_scorm = false;
  814. $realFileSize = 0;
  815. foreach ($zip_content_array as & $this_content) {
  816. if (preg_match('~.(php.*|phtml)$~i', $this_content['filename'])) {
  817. return api_failure::set_failure('php_file_in_zip_file');
  818. } elseif (stristr($this_content['filename'], 'imsmanifest.xml')) {
  819. $ok_scorm = true;
  820. } elseif (stristr($this_content['filename'], 'LMS')) {
  821. $ok_plantyn_scorm1 = true;
  822. } elseif (stristr($this_content['filename'], 'REF')) {
  823. $ok_plantyn_scorm2 = true;
  824. } elseif (stristr($this_content['filename'], 'SCO')) {
  825. $ok_plantyn_scorm3 = true;
  826. } elseif (stristr($this_content['filename'], 'AICC')) {
  827. $ok_aicc_scorm = true;
  828. }
  829. $realFileSize += $this_content['size'];
  830. }
  831. if (($ok_plantyn_scorm1 && $ok_plantyn_scorm2 && $ok_plantyn_scorm3) || $ok_aicc_scorm) {
  832. $ok_scorm = true;
  833. }
  834. if (!$ok_scorm && defined('CHECK_FOR_SCORM') && CHECK_FOR_SCORM) {
  835. return api_failure::set_failure('not_scorm_content');
  836. }
  837. if (!enough_size($realFileSize, $base_work_dir, $max_filled_space)) {
  838. return api_failure::set_failure('not_enough_space');
  839. }
  840. // It happens on Linux that $upload_path sometimes doesn't start with '/'
  841. if ($upload_path[0] != '/' && substr($base_work_dir,-1,1) != '/') {
  842. $upload_path = '/'.$upload_path;
  843. }
  844. if ($upload_path[strlen($upload_path) - 1] == '/') {
  845. $upload_path=substr($upload_path, 0, -1);
  846. }
  847. /* Uncompressing phase */
  848. /*
  849. The first version, using OS unzip, is not used anymore
  850. because it does not return enough information.
  851. We need to process each individual file in the zip archive to
  852. - add it to the database
  853. - parse & change relative html links
  854. */
  855. if (PHP_OS == 'Linux' && ! get_cfg_var('safe_mode') && false) { // *** UGent, changed by OC ***
  856. // Shell Method - if this is possible, it gains some speed
  857. exec("unzip -d \"".$base_work_dir.$upload_path."/\"".$uploaded_file['name']." " .$uploaded_file['tmp_name']);
  858. } else {
  859. // PHP method - slower...
  860. $save_dir = getcwd();
  861. chdir($base_work_dir.$upload_path);
  862. $unzippingState = $zip_file->extract();
  863. for ($j=0; $j < count($unzippingState); $j++) {
  864. $state = $unzippingState[$j];
  865. // Fix relative links in html files
  866. $extension = strrchr($state['stored_filename'], '.');
  867. }
  868. if ($dir = @opendir($base_work_dir.$upload_path)) {
  869. while ($file = readdir($dir)) {
  870. if ($file != '.' && $file != '..') {
  871. $filetype = 'file';
  872. if (is_dir($base_work_dir.$upload_path.'/'.$file)) $filetype = 'folder';
  873. $safe_file = replace_dangerous_char($file, 'strict');
  874. @rename($base_work_dir.$upload_path.'/'.$file,$base_work_dir.$upload_path.'/'.$safe_file);
  875. set_default_settings($upload_path, $safe_file,$filetype);
  876. }
  877. }
  878. closedir($dir);
  879. } else {
  880. error_log('Could not create directory '.$base_work_dir.$upload_path.' to unzip files');
  881. }
  882. chdir($save_dir); // Back to previous dir position
  883. }
  884. }
  885. return true;
  886. }
  887. /**
  888. * Manages all the unzipping process of an uploaded document
  889. * This uses the item_property table for properties of documents
  890. *
  891. * @author Hugues Peeters <hugues.peeters@claroline.net>
  892. * @author Bert Vanderkimpen
  893. *
  894. * @param array $courseInfo
  895. * @param array $userInfo
  896. * @param array $uploaded_file - follows the $_FILES Structure
  897. * @param string $upload_path - destination of the upload.
  898. * This path is to append to $base_work_dir
  899. * @param string $base_work_dir - base working directory of the module
  900. * @param int $maxFilledSpace - amount of bytes to not exceed in the base
  901. * working directory
  902. * @param int $sessionId
  903. * @param int $groupId
  904. * @param boolean $output Optional. If no output not wanted on success, set to false.
  905. *
  906. * @return boolean true if it succeeds false otherwise
  907. */
  908. function unzip_uploaded_document(
  909. $courseInfo,
  910. $userInfo,
  911. $uploaded_file,
  912. $uploadPath,
  913. $base_work_dir,
  914. $maxFilledSpace,
  915. $sessionId = 0,
  916. $groupId = 0,
  917. $output = true
  918. ) {
  919. $zip = new PclZip($uploaded_file['tmp_name']);
  920. // Check the zip content (real size and file extension)
  921. $zip_content_array = (array)$zip->listContent();
  922. $realSize = 0;
  923. foreach ($zip_content_array as & $this_content) {
  924. $realSize += $this_content['size'];
  925. }
  926. if (!DocumentManager::enough_space($realSize, $maxFilledSpace)) {
  927. Display::display_error_message(get_lang('UplNotEnoughSpace'));
  928. return false;
  929. }
  930. $folder = api_get_unique_id();
  931. $destinationDir = api_get_path(SYS_ARCHIVE_PATH).$folder;
  932. mkdir($destinationDir, api_get_permissions_for_new_directories(), true);
  933. /* Uncompress zip file*/
  934. // We extract using a callback function that "cleans" the path
  935. $zip->extract(
  936. PCLZIP_OPT_PATH,
  937. $destinationDir,
  938. PCLZIP_CB_PRE_EXTRACT,
  939. 'clean_up_files_in_zip',
  940. PCLZIP_OPT_REPLACE_NEWER
  941. );
  942. // Add all documents in the unzipped folder to the database
  943. add_all_documents_in_folder_to_database(
  944. $courseInfo,
  945. $userInfo,
  946. $base_work_dir,
  947. $destinationDir,
  948. $sessionId,
  949. $groupId,
  950. $output,
  951. array('path' => $uploadPath)
  952. );
  953. if (is_dir($destinationDir)) {
  954. rmdirr($destinationDir);
  955. }
  956. return true;
  957. }
  958. /**
  959. * This function is a callback function that is used while extracting a zipfile
  960. * http://www.phpconcept.net/pclzip/man/en/index.php?options-pclzip_cb_pre_extract
  961. *
  962. * @param $p_event
  963. * @param $p_header
  964. * @return 1 (If the function returns 1, then the extraction is resumed)
  965. */
  966. function clean_up_files_in_zip($p_event, &$p_header)
  967. {
  968. $res = clean_up_path($p_header['filename']);
  969. return $res;
  970. }
  971. /**
  972. * This function cleans up a given path
  973. * by eliminating dangerous file names and cleaning them
  974. *
  975. * @param string $path
  976. * @return $path
  977. * @see disable_dangerous_file()
  978. * @see replace_dangerous_char()
  979. */
  980. function clean_up_path(&$path)
  981. {
  982. // Split the path in folders and files
  983. $path_array = explode('/', $path);
  984. // Clean up every foler and filename in the path
  985. foreach ($path_array as $key => & $val) {
  986. // We don't want to lose the dots in ././folder/file (cfr. zipfile)
  987. if ($val != '.') {
  988. $val = disable_dangerous_file(replace_dangerous_char($val));
  989. }
  990. }
  991. // Join the "cleaned" path (modified in-place as passed by reference)
  992. $path = implode('/', $path_array);
  993. $res = filter_extension($path);
  994. return $res;
  995. }
  996. /**
  997. * Checks if the file is dangerous, based on extension and/or mimetype.
  998. * The list of extensions accepted/rejected can be found from
  999. * api_get_setting('upload_extensions_exclude') and api_get_setting('upload_extensions_include')
  1000. * @param string filename passed by reference. The filename will be modified
  1001. * if filter rules say so! (you can include path but the filename should look like 'abc.html')
  1002. * @return int 0 to skip file, 1 to keep file
  1003. */
  1004. function filter_extension(&$filename)
  1005. {
  1006. if (substr($filename, -1) == '/') {
  1007. return 1; // Authorize directories
  1008. }
  1009. $blacklist = api_get_setting('upload_extensions_list_type');
  1010. if ($blacklist != 'whitelist') { // if = blacklist
  1011. $extensions = explode(';', strtolower(api_get_setting('upload_extensions_blacklist')));
  1012. $skip = api_get_setting('upload_extensions_skip');
  1013. $ext = strrchr($filename, '.');
  1014. $ext = substr($ext, 1);
  1015. if (empty($ext)) {
  1016. return 1; // We're in blacklist mode, so accept empty extensions
  1017. }
  1018. if (in_array(strtolower($ext), $extensions)) {
  1019. if ($skip == 'true') {
  1020. return 0;
  1021. } else {
  1022. $new_ext = api_get_setting('upload_extensions_replace_by');
  1023. $filename = str_replace('.'.$ext, '.'.$new_ext, $filename);
  1024. return 1;
  1025. }
  1026. } else {
  1027. return 1;
  1028. }
  1029. } else {
  1030. $extensions = split(';', strtolower(api_get_setting('upload_extensions_whitelist')));
  1031. $skip = api_get_setting('upload_extensions_skip');
  1032. $ext = strrchr($filename, '.');
  1033. $ext = substr($ext, 1);
  1034. if (empty($ext)) {
  1035. return 1; // Accept empty extensions
  1036. }
  1037. if (!in_array(strtolower($ext), $extensions)) {
  1038. if ($skip == 'true') {
  1039. return 0;
  1040. } else {
  1041. $new_ext = api_get_setting('upload_extensions_replace_by');
  1042. $filename = str_replace('.'.$ext, '.'.$new_ext, $filename);
  1043. return 1;
  1044. }
  1045. } else {
  1046. return 1;
  1047. }
  1048. }
  1049. }
  1050. /**
  1051. * Adds a new document to the database
  1052. *
  1053. * @param array $_course
  1054. * @param string $path
  1055. * @param string $filetype
  1056. * @param int $filesize
  1057. * @param string $title
  1058. * @param string $comment
  1059. * @param int $readonly
  1060. * @param bool $save_visibility
  1061. * @param int $group_id
  1062. * @param int $session_id Session ID, if any
  1063. * @return int id if inserted document
  1064. */
  1065. function add_document(
  1066. $_course,
  1067. $path,
  1068. $filetype,
  1069. $filesize,
  1070. $title,
  1071. $comment = null,
  1072. $readonly = 0,
  1073. $save_visibility = true,
  1074. $group_id = null,
  1075. $session_id = 0
  1076. ) {
  1077. $session_id = intval($session_id);
  1078. if (empty($session_id)) {
  1079. $session_id = api_get_session_id();
  1080. }
  1081. $readonly = intval($readonly);
  1082. $comment = Database::escape_string($comment);
  1083. $path = Database::escape_string($path);
  1084. $filetype = Database::escape_string($filetype);
  1085. $filesize = Database::escape_string($filesize);
  1086. $title = Database::escape_string(htmlspecialchars($title));
  1087. $c_id = $_course['real_id'];
  1088. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  1089. $sql = "INSERT INTO $table_document (c_id, path, filetype, size, title, comment, readonly, session_id)
  1090. VALUES ($c_id, '$path','$filetype','$filesize','$title', '$comment', $readonly, $session_id)";
  1091. if (Database::query($sql)) {
  1092. $documentId = Database::insert_id();
  1093. if ($documentId) {
  1094. if ($save_visibility) {
  1095. api_set_default_visibility($documentId, TOOL_DOCUMENT, $group_id, $_course);
  1096. }
  1097. }
  1098. return $documentId;
  1099. } else {
  1100. return false;
  1101. }
  1102. }
  1103. /**
  1104. * Updates an existing document in the database
  1105. * as the file exists, we only need to change the size
  1106. *
  1107. * @param array $_course
  1108. * @param int $documentId
  1109. * @param int $filesize
  1110. * @param int $readonly
  1111. * @return boolean true /false
  1112. */
  1113. function update_existing_document($_course, $documentId, $filesize, $readonly = 0)
  1114. {
  1115. $document_table = Database::get_course_table(TABLE_DOCUMENT);
  1116. $documentId = intval($documentId);
  1117. $filesize = intval($filesize);
  1118. $readonly = intval($readonly);
  1119. $course_id = $_course['real_id'];
  1120. $sql = "UPDATE $document_table SET
  1121. size = '$filesize',
  1122. readonly = '$readonly'
  1123. WHERE c_id = $course_id AND id = $documentId";
  1124. if (Database::query($sql)) {
  1125. return true;
  1126. } else {
  1127. return false;
  1128. }
  1129. }
  1130. /**
  1131. * This function updates the last_edit_date, last edit user id on all folders in a given path
  1132. *
  1133. * @param array $_course
  1134. * @param string $path
  1135. * @param int $user_id
  1136. */
  1137. function item_property_update_on_folder($_course, $path, $user_id)
  1138. {
  1139. // If we are in the root, just return... no need to update anything
  1140. if ($path == '/') {
  1141. return;
  1142. }
  1143. $user_id = intval($user_id);
  1144. // If the given path ends with a / we remove it
  1145. $endchar = substr($path, strlen($path) - 1, 1);
  1146. if ($endchar == '/') {
  1147. $path = substr($path, 0, strlen($path) - 1);
  1148. }
  1149. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1150. // Get the time
  1151. $time = date('Y-m-d H:i:s', time());
  1152. // Det all paths in the given path
  1153. // /folder/subfolder/subsubfolder/file
  1154. // if file is updated, subsubfolder, subfolder and folder are updated
  1155. $exploded_path = explode('/', $path);
  1156. $course_id = api_get_course_int_id();
  1157. $newpath = '';
  1158. foreach ($exploded_path as $key => & $value) {
  1159. // We don't want a slash before our first slash
  1160. if ($key != 0) {
  1161. $newpath .= '/'.$value;
  1162. //echo 'path= '.$newpath.'<br />';
  1163. // Select ID of given folder
  1164. $folder_id = DocumentManager::get_document_id($_course, $newpath);
  1165. if ($folder_id) {
  1166. $sql = "UPDATE $table SET
  1167. lastedit_date='$time',lastedit_type='DocumentInFolderUpdated', lastedit_user_id='$user_id'
  1168. WHERE c_id = $course_id AND tool='".TOOL_DOCUMENT."' AND ref='$folder_id'";
  1169. Database::query($sql);
  1170. }
  1171. }
  1172. }
  1173. }
  1174. /**
  1175. * Returns the directory depth of the file.
  1176. *
  1177. * @author Olivier Cauberghe <olivier.cauberghe@ugent.be>
  1178. * @param path+filename eg: /main/document/document.php
  1179. * @return The directory depth
  1180. */
  1181. function get_levels($filename) {
  1182. $levels=explode('/', $filename);
  1183. if (empty($levels[count($levels) - 1])) {
  1184. unset($levels[count($levels) - 1]);
  1185. }
  1186. return count($levels);
  1187. }
  1188. /**
  1189. * Adds file to document table in database
  1190. * deprecated: use file_set_default_settings instead
  1191. *
  1192. * @author Olivier Cauberghe <olivier.cauberghe@ugent.be>
  1193. * @param path,filename
  1194. * action: Adds an entry to the document table with the default settings.
  1195. */
  1196. function set_default_settings($upload_path, $filename, $filetype = 'file')
  1197. {
  1198. global $dbTable,$_configuration;
  1199. global $default_visibility;
  1200. if (!$default_visibility) {
  1201. $default_visibility = 'v';
  1202. }
  1203. $filetype = Database::escape_string($filetype);
  1204. $upload_path = str_replace('\\', '/', $upload_path);
  1205. $upload_path = str_replace('//', '/', $upload_path);
  1206. if ($upload_path == '/') {
  1207. $upload_path='';
  1208. } elseif (!empty($upload_path) && $upload_path[0] != '/') {
  1209. $upload_path="/$upload_path";
  1210. }
  1211. $endchar = substr($filename, strlen($filename) - 1, 1);
  1212. if ($endchar == '/') {
  1213. $filename = substr($filename, 0, -1);
  1214. }
  1215. $filename = Database::escape_string($filename);
  1216. $query = "SELECT count(*) as bestaat FROM $dbTable
  1217. WHERE path='$upload_path/$filename'";
  1218. $result = Database::query($query);
  1219. $row = Database::fetch_array($result);
  1220. if ($row['bestaat'] > 0) {
  1221. $query = "UPDATE $dbTable SET
  1222. path='$upload_path/$filename',
  1223. visibility='$default_visibility',
  1224. filetype='$filetype'
  1225. WHERE path='$upload_path/$filename'";
  1226. } else {
  1227. $query = "INSERT INTO $dbTable (path,visibility,filetype)
  1228. VALUES('$upload_path/$filename','$default_visibility','$filetype')";
  1229. }
  1230. Database::query($query);
  1231. }
  1232. /**
  1233. * Retrieves the image path list in a html file
  1234. *
  1235. * @author Hugues Peeters <hugues.peeters@claroline.net>
  1236. * @param string $html_file
  1237. * @return array - images path list
  1238. */
  1239. function search_img_from_html($html_file) {
  1240. $img_path_list = array();
  1241. if (!$fp = fopen($html_file, 'r')) {
  1242. return ;
  1243. }
  1244. // Aearch and store occurences of the <img> tag in an array
  1245. $size_file = (filesize($html_file) === 0) ? 1 : filesize($html_file);
  1246. if (isset($fp) && $fp !== false) {
  1247. $buffer = fread($fp, $size_file);
  1248. if (strlen($buffer) >= 0 && $buffer !== false) {
  1249. //
  1250. } else {
  1251. die('<center>Can not read file.</center>');
  1252. }
  1253. } else {
  1254. die('<center>Can not read file.</center>');
  1255. }
  1256. $matches = array();
  1257. if (preg_match_all('~<[[:space:]]*img[^>]*>~i', $buffer, $matches)) {
  1258. $img_tag_list = $matches[0];
  1259. }
  1260. fclose ($fp);
  1261. unset($buffer);
  1262. // Search the image file path from all the <IMG> tag detected
  1263. if (sizeof($img_tag_list) > 0) {
  1264. foreach ($img_tag_list as & $this_img_tag) {
  1265. if (preg_match('~src[[:space:]]*=[[:space:]]*[\"]{1}([^\"]+)[\"]{1}~i', $this_img_tag, $matches)) {
  1266. $img_path_list[] = $matches[1];
  1267. }
  1268. }
  1269. $img_path_list = array_unique($img_path_list); // Remove duplicate entries
  1270. }
  1271. return $img_path_list;
  1272. }
  1273. /**
  1274. * Creates a new directory trying to find a directory name
  1275. * that doesn't already exist
  1276. * (we could use unique_name() here...)
  1277. *
  1278. * @author Hugues Peeters <hugues.peeters@claroline.net>
  1279. * @author Bert Vanderkimpen
  1280. * @param array $_course current course information
  1281. * @param int $user_id current user id
  1282. * @param int $session_id
  1283. * @param int $to_group_id
  1284. * @param int $to_user_id
  1285. * @param string $base_work_dir /var/www/chamilo/courses/ABC/document
  1286. * @param string $desired_dir_name complete path of the desired name
  1287. * Example: /folder1/folder2
  1288. * @param string $title "folder2"
  1289. * @param int $visibility (0 for invisible, 1 for visible, 2 for deleted)
  1290. * @param bool $generateNewNameIfExists
  1291. * @return string actual directory name if it succeeds,
  1292. * boolean false otherwise
  1293. */
  1294. function create_unexisting_directory(
  1295. $_course,
  1296. $user_id,
  1297. $session_id,
  1298. $to_group_id,
  1299. $to_user_id,
  1300. $base_work_dir,
  1301. $desired_dir_name,
  1302. $title = null,
  1303. $visibility = null,
  1304. $generateNewNameIfExists = false,
  1305. $useSuffix = true
  1306. ) {
  1307. $course_id = $_course['real_id'];
  1308. $session_id = intval($session_id);
  1309. $folderExists = DocumentManager::folderExists(
  1310. $desired_dir_name,
  1311. $_course,
  1312. $session_id,
  1313. $to_group_id,
  1314. $useSuffix
  1315. );
  1316. if ($folderExists == true) {
  1317. if ($generateNewNameIfExists) {
  1318. $counter = 1;
  1319. while (1) {
  1320. $folderExists = DocumentManager::folderExists(
  1321. $desired_dir_name.'_'.$counter,
  1322. $_course,
  1323. $session_id,
  1324. $to_group_id
  1325. );
  1326. if ($folderExists == false) {
  1327. break;
  1328. }
  1329. $counter++;
  1330. }
  1331. $desired_dir_name = $desired_dir_name.'_'.$counter;
  1332. } else {
  1333. return false;
  1334. }
  1335. }
  1336. $systemFolderName = $desired_dir_name;
  1337. // Adding suffix
  1338. $suffix = '';
  1339. if ($useSuffix) {
  1340. $suffix = DocumentManager::getDocumentSuffix(
  1341. $_course,
  1342. $session_id,
  1343. $to_group_id
  1344. );
  1345. }
  1346. $systemFolderName .= $suffix;
  1347. if ($title == null) {
  1348. $title = basename($desired_dir_name);
  1349. }
  1350. if (!is_dir($base_work_dir.$systemFolderName)) {
  1351. $result = mkdir(
  1352. $base_work_dir.$systemFolderName,
  1353. api_get_permissions_for_new_directories(),
  1354. true
  1355. );
  1356. if ($result) {
  1357. // Check if pathname already exists inside document table
  1358. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1359. $sql = "SELECT id, path FROM $tbl_document
  1360. WHERE
  1361. c_id = $course_id AND
  1362. (
  1363. path = '" . $systemFolderName . "'
  1364. )
  1365. ";
  1366. $rs = Database::query($sql);
  1367. if (Database::num_rows($rs) == 0) {
  1368. $document_id = add_document(
  1369. $_course,
  1370. $systemFolderName,
  1371. 'folder',
  1372. 0,
  1373. $title,
  1374. null,
  1375. 0,
  1376. true,
  1377. $to_group_id
  1378. );
  1379. if ($document_id) {
  1380. // Update document item_property
  1381. if (!empty($visibility)) {
  1382. $visibilities = array(
  1383. 0 => 'invisible',
  1384. 1 => 'visible',
  1385. 2 => 'delete'
  1386. );
  1387. api_item_property_update(
  1388. $_course,
  1389. TOOL_DOCUMENT,
  1390. $document_id,
  1391. $visibilities[$visibility],
  1392. $user_id,
  1393. $to_group_id,
  1394. $to_user_id,
  1395. null,
  1396. null,
  1397. $session_id
  1398. );
  1399. } else {
  1400. api_item_property_update(
  1401. $_course,
  1402. TOOL_DOCUMENT,
  1403. $document_id,
  1404. 'FolderCreated',
  1405. $user_id,
  1406. $to_group_id,
  1407. $to_user_id,
  1408. null,
  1409. null,
  1410. $session_id
  1411. );
  1412. }
  1413. $documentData = DocumentManager::get_document_data_by_id(
  1414. $document_id,
  1415. $_course['code'],
  1416. false,
  1417. $session_id
  1418. );
  1419. return $documentData;
  1420. }
  1421. } else {
  1422. $document = Database::fetch_array($rs);
  1423. $documentData = DocumentManager::get_document_data_by_id(
  1424. $document['id'],
  1425. $_course['code'],
  1426. false,
  1427. $session_id
  1428. );
  1429. /* This means the folder NOT exist in the filesystem
  1430. (now this was created) but there is a record in the Database*/
  1431. return $documentData;
  1432. }
  1433. }
  1434. }
  1435. return false;
  1436. }
  1437. /**
  1438. * Handles uploaded missing images
  1439. *
  1440. * @author Hugues Peeters <hugues.peeters@claroline.net>
  1441. * @author Bert Vanderkimpen
  1442. * @param array $_course
  1443. * @param array $uploaded_file_collection - follows the $_FILES Structure
  1444. * @param string $base_work_dir
  1445. * @param string $missing_files_dir
  1446. * @param int $user_id
  1447. * @param int $max_filled_space
  1448. */
  1449. function move_uploaded_file_collection_into_directory(
  1450. $_course,
  1451. $uploaded_file_collection,
  1452. $base_work_dir,
  1453. $missing_files_dir,
  1454. $user_id,
  1455. $to_group_id,
  1456. $to_user_id,
  1457. $max_filled_space
  1458. ) {
  1459. $number_of_uploaded_images = count($uploaded_file_collection['name']);
  1460. $new_file_list = array();
  1461. for ($i = 0; $i < $number_of_uploaded_images; $i++) {
  1462. $missing_file['name'] = $uploaded_file_collection['name'][$i];
  1463. $missing_file['type'] = $uploaded_file_collection['type'][$i];
  1464. $missing_file['tmp_name'] = $uploaded_file_collection['tmp_name'][$i];
  1465. $missing_file['error'] = $uploaded_file_collection['error'][$i];
  1466. $missing_file['size'] = $uploaded_file_collection['size'][$i];
  1467. $upload_ok = process_uploaded_file($missing_file);
  1468. if ($upload_ok) {
  1469. $new_file_list[] = handle_uploaded_document(
  1470. $_course,
  1471. $missing_file,
  1472. $base_work_dir,
  1473. $missing_files_dir,
  1474. $user_id,
  1475. $to_group_id,
  1476. $to_user_id,
  1477. $max_filled_space,
  1478. 0,
  1479. 'overwrite'
  1480. );
  1481. }
  1482. unset($missing_file);
  1483. }
  1484. return $new_file_list;
  1485. }
  1486. /**
  1487. * Opens the old html file and replace the src path into the img tag
  1488. * This also works for files in subdirectories.
  1489. * @param $original_img_path is an array
  1490. * @param $new_img_path is an array
  1491. */
  1492. function replace_img_path_in_html_file($original_img_path, $new_img_path, $html_file)
  1493. {
  1494. $_course = api_get_course_info();
  1495. // Open the file
  1496. $fp = fopen($html_file, 'r');
  1497. $buffer = fread($fp, filesize($html_file));
  1498. $new_html_content = '';
  1499. // Fix the image tags
  1500. for ($i = 0, $fileNb = count($original_img_path); $i < $fileNb ; $i++) {
  1501. $replace_what = $original_img_path[$i];
  1502. // We only need the directory and the filename /path/to/file_html_files/missing_file.gif -> file_html_files/missing_file.gif
  1503. $exploded_file_path = explode('/', $new_img_path[$i]);
  1504. $replace_by = $exploded_file_path[count($exploded_file_path) - 2].'/'.$exploded_file_path[count($exploded_file_path) - 1];
  1505. //$message .= "Element [$i] <b>" . $replace_what . "</b> replaced by <b>" . $replace_by . "</b><br />"; //debug
  1506. //api_display_debug_info($message);
  1507. $buffer = str_replace($replace_what, $replace_by, $buffer);
  1508. }
  1509. $new_html_content .= $buffer;
  1510. @fclose($fp);
  1511. // Write the resulted new file
  1512. if (!$fp = fopen($html_file, 'w')) {
  1513. return;
  1514. }
  1515. if (!fwrite($fp, $new_html_content)) {
  1516. return;
  1517. }
  1518. }
  1519. /**
  1520. * Creates a file containing an html redirection to a given url
  1521. *
  1522. * @author Hugues Peeters <hugues.peeters@claroline.net>
  1523. * @param string $file_path
  1524. * @param string $url
  1525. * @return void
  1526. */
  1527. function create_link_file($file_path, $url)
  1528. {
  1529. $file_content = '<html>'
  1530. .'<head>'
  1531. .'<meta http-equiv="refresh" content="1;url='.$url.'">'
  1532. .'</head>'
  1533. .'<body>'
  1534. .'</body>'
  1535. .'</html>';
  1536. if (file_exists($file_path)) {
  1537. if (!($fp = fopen ($file_path, 'w'))) {
  1538. return false;
  1539. }
  1540. return fwrite($fp, $file_content);
  1541. }
  1542. }
  1543. /**
  1544. * Opens html file $full_file_name;
  1545. * Parses the hyperlinks; and
  1546. * Writes the result back in the html file.
  1547. *
  1548. * @author Roan Embrechts
  1549. * @version 0.1
  1550. */
  1551. function api_replace_links_in_html($upload_path, $full_file_name)
  1552. {
  1553. // Open the file
  1554. if (file_exists($full_file_name)) {
  1555. $fp = fopen($full_file_name, 'r');
  1556. $buffer = fread ($fp, filesize ($full_file_name));
  1557. // Parse the contents
  1558. $new_html_content = api_replace_links_in_string($upload_path, $buffer);
  1559. // Write the result
  1560. $fp = fopen($full_file_name, 'w');
  1561. fwrite($fp, $new_html_content);
  1562. }
  1563. }
  1564. /**
  1565. deprecated: use api_replace_parameter instead
  1566. Parse the buffer string provided as parameter
  1567. Replace the a href tags so they are displayed correctly.
  1568. - works for files in root and subdirectories
  1569. - replace relative hyperlinks to use showinframes.php?file= ...
  1570. - add target="_self" to all absolute hyperlinks
  1571. - leave local anchors untouched (e.g. #CHAPTER1)
  1572. - leave links with download.php and showinframes.php untouched
  1573. @author Roan Embrechts
  1574. @version 0.6
  1575. */
  1576. function api_replace_links_in_string($upload_path, $buffer) {
  1577. // Search for hyperlinks
  1578. $matches = array();
  1579. if (preg_match_all('/<a[\s]*href[^<]*>/i', $buffer, $matches)) {
  1580. $tag_list = $matches[0];
  1581. }
  1582. // Search the filepath of all detected <a href> tags
  1583. if (sizeof($tag_list) > 0) {
  1584. $file_path_list = array();
  1585. $href_list = array();
  1586. foreach ($tag_list as & $this_tag) {
  1587. /* Match case insensitive, the stuff between the two ~ :
  1588. a href = <exactly one quote><one or more non-quotes><exactly one ">
  1589. e.g. a href="www.google.be", A HREF = "info.html"
  1590. to match ["] escape the " or else PHP interprets it
  1591. [\"]{1} --> matches exactly one "
  1592. + 1 or more (like * is 0 or more)
  1593. [\s]* matches whitespace
  1594. $matches contains captured subpatterns
  1595. the only one here is ([^\"]+) --> matches[1]
  1596. */
  1597. if (preg_match("~a href[\s]*=[\s]*[\"]{1}([^\"]+)[\"]{1}~i", $this_tag, $matches)) {
  1598. $file_path_list[] = $matches[1]; // older
  1599. $href_list[] = $matches[0]; // to also add target="_self"
  1600. }
  1601. }
  1602. }
  1603. // Replace the original hyperlinks by the correct ones
  1604. for ($count = 0; $count < sizeof($href_list); $count++) {
  1605. $replace_what[$count] = $href_list[$count];
  1606. $is_absolute_hyperlink = strpos($replace_what[$count], 'http');
  1607. $is_local_anchor = strpos($replace_what[$count], "#");
  1608. if (!$is_absolute_hyperlink && !$is_local_anchor) {
  1609. // This is a relative hyperlink
  1610. if ((strpos($replace_what[$count], 'showinframes.php') === false) &&
  1611. (strpos($replace_what[$count], 'download.php') === false)
  1612. ) {
  1613. // Fix the link to use showinframes.php
  1614. $replace_by[$count] = 'a href = "showinframes.php?file='.$upload_path.'/'.$file_path_list[$count].'" target="_self"';
  1615. } else {
  1616. // URL has been already fixed, leave it as is
  1617. $replace_by[$count] = $replace_what[$count];
  1618. }
  1619. } elseif ($is_absolute_hyperlink) {
  1620. $replace_by[$count] = 'a href="'.$file_path_list[$count].'" target ="_self"';
  1621. } else {
  1622. // Don't change anything
  1623. $replace_by[$count] = $replace_what[$count];
  1624. }
  1625. //Display::display_normal_message('Link replaced by ' . $replace_by[$count]); // debug
  1626. }
  1627. $buffer = str_replace($replace_what, $replace_by, $buffer);
  1628. return $buffer;
  1629. }
  1630. /**
  1631. EXPERIMENTAL - function seems to work, needs more testing
  1632. @param $upload_path is the path where the document is stored, like "/archive/"
  1633. if it is the root level, the function expects "/"
  1634. otherwise "/path/"
  1635. This function parses all tags with $param_name parameters.
  1636. so the tags are displayed correctly.
  1637. --------------
  1638. Algorithm v1.0
  1639. --------------
  1640. given a string and a parameter,
  1641. * OK find all tags in that string with the specified parameter (like href or src)
  1642. * OK for every one of these tags, find the src|href|... part to edit it
  1643. * OK change the src|href|... part to use download.php (or showinframes.php)
  1644. * OK do some special stuff for hyperlinks
  1645. Exceptions
  1646. * OK if download.php or showinframes.php is already in the tag, leave it alone
  1647. * OK if mailto is in the tag, leave it alone
  1648. * OK if the src|href param contains http://, it's absolute --> leave it alone
  1649. Special for hyperlinks (a href...)
  1650. * OK add target="_self"
  1651. * OK use showinframes.php instead of download.php
  1652. @author Roan Embrechts
  1653. @version 1.1
  1654. */
  1655. function api_replace_parameter($upload_path, $buffer, $param_name = 'src')
  1656. {
  1657. // Search for tags with $param_name as a parameter
  1658. /*
  1659. // [\s]* matches whitespace
  1660. // [\"=a-z] matches ", = and a-z
  1661. // ([\s]*[a-z]*)* matches all whitespace and normal alphabet
  1662. // characters a-z combinations but seems too slow
  1663. // perhaps ([\s]*[a-z]*) a maximum number of times ?
  1664. // [\s]*[a-z]*[\s]* matches many tags
  1665. // the ending "i" means to match case insensitive (a matches a and A)
  1666. */
  1667. $matches = array();
  1668. if (preg_match_all('/<[a-z]+[^<]*'.$param_name.'[^<]*>/i', $buffer, $matches)) {
  1669. $tag_list = $matches[0];
  1670. }
  1671. // Search the filepath of parameter $param_name in all detected tags
  1672. if (sizeof($tag_list) > 0) {
  1673. $file_path_list = array();
  1674. $href_list = array();
  1675. foreach ($tag_list as & $this_tag) {
  1676. //Display::display_normal_message(htmlentities($this_tag)); //debug
  1677. if ( preg_match("~".$param_name."[\s]*=[\s]*[\"]{1}([^\"]+)[\"]{1}~i", $this_tag, $matches)) {
  1678. $file_path_list[] = $matches[1]; // older
  1679. $href_list[] = $matches[0]; // to also add target="_self"
  1680. }
  1681. }
  1682. }
  1683. // Replace the original tags by the correct ones
  1684. for ($count = 0; $count < sizeof($href_list); $count++) {
  1685. $replace_what[$count] = $href_list[$count];
  1686. $is_absolute_hyperlink = strpos($replace_what[$count], 'http');
  1687. $is_local_anchor = strpos($replace_what[$count], '#');
  1688. if (!$is_absolute_hyperlink && !$is_local_anchor) {
  1689. if ((strpos($replace_what[$count], 'showinframes.php') === false)
  1690. && (strpos($replace_what[$count], 'download.php') === false)
  1691. && (strpos($replace_what[$count], 'mailto') === false)) {
  1692. // Fix the link to use download.php or showinframes.php
  1693. if (preg_match("/<a([\s]*[\"\/:'=a-z0-9]*){5}href[^<]*>/i", $tag_list[$count])) {
  1694. $replace_by[$count] = " $param_name =\"showinframes.php?file=" . $upload_path.$file_path_list[$count]."\" target=\"_self\" ";
  1695. } else {
  1696. $replace_by[$count] = " $param_name =\"download.php?doc_url=" . $upload_path.$file_path_list[$count]."\" ";
  1697. }
  1698. } else {
  1699. // "mailto" or url already fixed, leave as is
  1700. //$message .= "Already fixed or contains mailto: ";
  1701. $replace_by[$count] = $replace_what[$count];
  1702. }
  1703. } elseif ($is_absolute_hyperlink) {
  1704. //$message .= "Absolute hyperlink, don't change, add target=_self: ";
  1705. $replace_by[$count] = " $param_name=\"" . $file_path_list[$count] . "\" target =\"_self\"";
  1706. } else {
  1707. // Don't change anything
  1708. //$message .= "Local anchor, don't change: ";
  1709. $replace_by[$count] = $replace_what[$count];
  1710. }
  1711. //$message .= "In tag $count, <b>" . htmlentities($tag_list[$count])
  1712. // . "</b>, parameter <b>" . $replace_what[$count] . "</b> replaced by <b>" . $replace_by[$count] . "</b><br>"; //debug
  1713. }
  1714. //if ($message) api_display_debug_info($message); //debug
  1715. $buffer = str_replace($replace_what, $replace_by, $buffer);
  1716. return $buffer;
  1717. }
  1718. /**
  1719. * Checks the extension of a file, if it's .htm or .html
  1720. * we use search_img_from_html to get all image paths in the file
  1721. *
  1722. * @param string $file
  1723. * @return array paths
  1724. * @see check_for_missing_files() uses search_img_from_html()
  1725. */
  1726. function check_for_missing_files($file)
  1727. {
  1728. if (strrchr($file, '.') == '.htm' || strrchr($file, '.') == '.html') {
  1729. $img_file_path = search_img_from_html($file);
  1730. return $img_file_path;
  1731. }
  1732. return false;
  1733. }
  1734. /**
  1735. * This function builds a form that asks for the missing images in a html file
  1736. * maybe we should do this another way?
  1737. *
  1738. * @param array $missing_files
  1739. * @param string $upload_path
  1740. * @param string $file_name
  1741. * @return string the form
  1742. */
  1743. function build_missing_files_form($missing_files, $upload_path, $file_name)
  1744. {
  1745. // Do we need a / or not?
  1746. $added_slash = ($upload_path == '/') ? '' : '/';
  1747. $folder_id = DocumentManager::get_document_id(api_get_course_info(), $upload_path);
  1748. // Build the form
  1749. $form = "<p><strong>".get_lang('MissingImagesDetected')."</strong></p>"
  1750. ."<form method=\"post\" action=\"".api_get_self()."\" enctype=\"multipart/form-data\">"
  1751. // Related_file is the path to the file that has missing images
  1752. ."<input type=\"hidden\" name=\"related_file\" value=\"".$upload_path.$added_slash.$file_name."\" />"
  1753. ."<input type=\"hidden\" name=\"upload_path\" value=\"".$upload_path."\" />"
  1754. ."<input type=\"hidden\" name=\"id\" value=\"".$folder_id."\" />"
  1755. ."<table border=\"0\">";
  1756. foreach ($missing_files as & $this_img_file_path) {
  1757. $form .= "<tr>"
  1758. ."<td>".basename($this_img_file_path)." : </td>"
  1759. ."<td>"
  1760. ."<input type=\"file\" name=\"img_file[]\"/>"
  1761. ."<input type=\"hidden\" name=\"img_file_path[]\" value=\"".$this_img_file_path."\" />"
  1762. ."</td>"
  1763. ."</tr>";
  1764. }
  1765. $form .= "</table>"
  1766. ."<button type='submit' name=\"cancel_submit_image\" value=\"".get_lang('Cancel')."\" class=\"cancel\">".get_lang('Cancel')."</button>"
  1767. ."<button type='submit' name=\"submit_image\" value=\"".get_lang('Ok')."\" class=\"save\">".get_lang('Ok')."</button>"
  1768. ."</form>";
  1769. return $form;
  1770. }
  1771. /**
  1772. * This recursive function can be used during the upgrade process form older
  1773. * versions of Chamilo
  1774. * It crawls the given directory, checks if the file is in the DB and adds
  1775. * it if it's not
  1776. *
  1777. * @param array $courseInfo
  1778. * @param array $userInfo
  1779. * @param string $base_work_dir
  1780. * @param string $folderPath
  1781. * @param int $sessionId
  1782. * @param int $groupId
  1783. * @param bool $output
  1784. * @param array $parent
  1785. * @param string $uploadPath
  1786. *
  1787. */
  1788. function add_all_documents_in_folder_to_database(
  1789. $courseInfo,
  1790. $userInfo,
  1791. $base_work_dir,
  1792. $folderPath,
  1793. $sessionId = 0,
  1794. $groupId = 0,
  1795. $output = false,
  1796. $parent = array()
  1797. ) {
  1798. if (empty($userInfo) || empty($courseInfo)) {
  1799. return false;
  1800. }
  1801. $userId = $userInfo['user_id'];
  1802. // Open dir
  1803. $handle = opendir($folderPath);
  1804. $files = array();
  1805. if (is_dir($folderPath)) {
  1806. // Run trough
  1807. while ($file = readdir($handle)) {
  1808. if ($file == '.' || $file == '..') {
  1809. continue;
  1810. }
  1811. $parentPath = null;
  1812. if (!empty($parent) && isset($parent['path'])) {
  1813. $parentPath = $parent['path'];
  1814. if ($parentPath == '/') {
  1815. $parentPath = null;
  1816. }
  1817. }
  1818. $completePath = $parentPath.'/'.$file;
  1819. $sysFolderPath = $folderPath.'/'.$file;
  1820. // Is directory?
  1821. if (is_dir($sysFolderPath)) {
  1822. $newFolderData = create_unexisting_directory(
  1823. $courseInfo,
  1824. $userId,
  1825. $sessionId,
  1826. $groupId,
  1827. null,
  1828. $base_work_dir,
  1829. $completePath,
  1830. null,
  1831. null,
  1832. true
  1833. );
  1834. $files[$file] = $newFolderData;
  1835. // Recursive
  1836. add_all_documents_in_folder_to_database(
  1837. $courseInfo,
  1838. $userInfo,
  1839. $base_work_dir,
  1840. $sysFolderPath,
  1841. $sessionId,
  1842. $groupId,
  1843. $output,
  1844. $newFolderData
  1845. );
  1846. } else {
  1847. // Rename
  1848. $uploadedFile = array(
  1849. 'name' => $file,
  1850. 'tmp_name' => $sysFolderPath,
  1851. 'size' => filesize($sysFolderPath),
  1852. 'type' => null,
  1853. 'from_file' => true,
  1854. 'move_file' => true
  1855. );
  1856. handle_uploaded_document(
  1857. $courseInfo,
  1858. $uploadedFile,
  1859. $base_work_dir,
  1860. $parentPath,
  1861. $userId,
  1862. $groupId,
  1863. null,
  1864. 0,
  1865. 'overwrite',
  1866. $output,
  1867. false,
  1868. null,
  1869. $sessionId
  1870. );
  1871. }
  1872. }
  1873. }
  1874. }