fileUpload.lib.php 72 KB

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