document.lib.php 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the document library for Chamilo.
  5. * It is / will be used to provide a service layer to all document-using tools.
  6. * and eliminate code duplication fro group documents, scorm documents, main documents.
  7. * Include/require it in your code to use its functionality.
  8. *
  9. * @package chamilo.library
  10. */
  11. /**
  12. * Code
  13. */
  14. class DocumentManager {
  15. private function __construct() {
  16. }
  17. /**
  18. * @return the document folder quota for the current course, in bytes, or the default quota
  19. */
  20. public static function get_course_quota($course_code = null) {
  21. if (empty($course_code)) {
  22. $course_info = api_get_course_info();
  23. } else {
  24. $course_info = api_get_course_info($course_code);
  25. }
  26. $course_quota = null;
  27. if (empty($course_info)) {
  28. return DEFAULT_DOCUMENT_QUOTA;
  29. } else {
  30. $course_quota = $course_info['disk_quota'];
  31. }
  32. if (is_null($course_quota) || empty($course_quota)) {
  33. // Course table entry for quota was null, then use default value
  34. $course_quota = DEFAULT_DOCUMENT_QUOTA;
  35. }
  36. return $course_quota;
  37. }
  38. /**
  39. * Get the content type of a file by checking the extension
  40. * We could use mime_content_type() with php-versions > 4.3,
  41. * but this doesn't work as it should on Windows installations
  42. *
  43. * @param string $filename or boolean TRUE to return complete array
  44. * @author ? first version
  45. * @author Bert Vanderkimpen
  46. *
  47. */
  48. public static function file_get_mime_type($filename) {
  49. // All MIME types in an array (from 1.6, this is the authorative source)
  50. // Please, keep this alphabetical if you add something to this list!
  51. $mime_types = array(
  52. 'ai' => 'application/postscript',
  53. 'aif' => 'audio/x-aiff',
  54. 'aifc' => 'audio/x-aiff',
  55. 'aiff' => 'audio/x-aiff',
  56. 'asf' => 'video/x-ms-asf',
  57. 'asc' => 'text/plain',
  58. 'au' => 'audio/basic',
  59. 'avi' => 'video/x-msvideo',
  60. 'bcpio' => 'application/x-bcpio',
  61. 'bin' => 'application/octet-stream',
  62. 'bmp' => 'image/bmp',
  63. 'cdf' => 'application/x-netcdf',
  64. 'class' => 'application/octet-stream',
  65. 'cpio' => 'application/x-cpio',
  66. 'cpt' => 'application/mac-compactpro',
  67. 'csh' => 'application/x-csh',
  68. 'css' => 'text/css',
  69. 'dcr' => 'application/x-director',
  70. 'dir' => 'application/x-director',
  71. 'djv' => 'image/vnd.djvu',
  72. 'djvu' => 'image/vnd.djvu',
  73. 'dll' => 'application/octet-stream',
  74. 'dmg' => 'application/x-diskcopy',
  75. 'dms' => 'application/octet-stream',
  76. 'doc' => 'application/msword',
  77. 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
  78. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  79. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  80. 'dvi' => 'application/x-dvi',
  81. 'dwg' => 'application/vnd.dwg',
  82. 'dxf' => 'application/vnd.dxf',
  83. 'dxr' => 'application/x-director',
  84. 'eps' => 'application/postscript',
  85. 'epub' => 'application/epub+zip',
  86. 'etx' => 'text/x-setext',
  87. 'exe' => 'application/octet-stream',
  88. 'ez' => 'application/andrew-inset',
  89. 'gif' => 'image/gif',
  90. 'gtar' => 'application/x-gtar',
  91. 'gz' => 'application/x-gzip',
  92. 'hdf' => 'application/x-hdf',
  93. 'hqx' => 'application/mac-binhex40',
  94. 'htm' => 'text/html',
  95. 'html' => 'text/html',
  96. 'ice' => 'x-conference-xcooltalk',
  97. 'ief' => 'image/ief',
  98. 'iges' => 'model/iges',
  99. 'igs' => 'model/iges',
  100. 'jar' => 'application/java-archiver',
  101. 'jpe' => 'image/jpeg',
  102. 'jpeg' => 'image/jpeg',
  103. 'jpg' => 'image/jpeg',
  104. 'js' => 'application/x-javascript',
  105. 'kar' => 'audio/midi',
  106. 'lam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  107. 'latex' => 'application/x-latex',
  108. 'lha' => 'application/octet-stream',
  109. 'log' => 'text/plain',
  110. 'lzh' => 'application/octet-stream',
  111. 'm1a' => 'audio/mpeg',
  112. 'm2a' => 'audio/mpeg',
  113. 'm3u' => 'audio/x-mpegurl',
  114. 'man' => 'application/x-troff-man',
  115. 'me' => 'application/x-troff-me',
  116. 'mesh' => 'model/mesh',
  117. 'mid' => 'audio/midi',
  118. 'midi' => 'audio/midi',
  119. 'mov' => 'video/quicktime',
  120. 'movie' => 'video/x-sgi-movie',
  121. 'mp2' => 'audio/mpeg',
  122. 'mp3' => 'audio/mpeg',
  123. 'mp4' => 'video/mpeg4-generic',
  124. 'mpa' => 'audio/mpeg',
  125. 'mpe' => 'video/mpeg',
  126. 'mpeg' => 'video/mpeg',
  127. 'mpg' => 'video/mpeg',
  128. 'mpga' => 'audio/mpeg',
  129. 'ms' => 'application/x-troff-ms',
  130. 'msh' => 'model/mesh',
  131. 'mxu' => 'video/vnd.mpegurl',
  132. 'nc' => 'application/x-netcdf',
  133. 'oda' => 'application/oda',
  134. 'oga' => 'audio/ogg',
  135. 'ogg' => 'application/ogg',
  136. 'ogx' => 'application/ogg',
  137. 'ogv' => 'video/ogg',
  138. 'pbm' => 'image/x-portable-bitmap',
  139. 'pct' => 'image/pict',
  140. 'pdb' => 'chemical/x-pdb',
  141. 'pdf' => 'application/pdf',
  142. 'pgm' => 'image/x-portable-graymap',
  143. 'pgn' => 'application/x-chess-pgn',
  144. 'pict' => 'image/pict',
  145. 'png' => 'image/png',
  146. 'pnm' => 'image/x-portable-anymap',
  147. 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  148. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  149. 'pps' => 'application/vnd.ms-powerpoint',
  150. 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  151. 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  152. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  153. 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  154. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  155. 'ppm' => 'image/x-portable-pixmap',
  156. 'ppt' => 'application/vnd.ms-powerpoint',
  157. 'pps' => 'application/vnd.ms-powerpoint',
  158. 'ps' => 'application/postscript',
  159. 'qt' => 'video/quicktime',
  160. 'ra' => 'audio/x-realaudio',
  161. 'ram' => 'audio/x-pn-realaudio',
  162. 'rar' => 'image/x-rar-compressed',
  163. 'ras' => 'image/x-cmu-raster',
  164. 'rgb' => 'image/x-rgb',
  165. 'rm' => 'audio/x-pn-realaudio',
  166. 'roff' => 'application/x-troff',
  167. 'rpm' => 'audio/x-pn-realaudio-plugin',
  168. 'rtf' => 'text/rtf',
  169. 'rtx' => 'text/richtext',
  170. 'sgm' => 'text/sgml',
  171. 'sgml' => 'text/sgml',
  172. 'sh' => 'application/x-sh',
  173. 'shar' => 'application/x-shar',
  174. 'silo' => 'model/mesh',
  175. 'sib' => 'application/X-Sibelius-Score',
  176. 'sit' => 'application/x-stuffit',
  177. 'skd' => 'application/x-koan',
  178. 'skm' => 'application/x-koan',
  179. 'skp' => 'application/x-koan',
  180. 'skt' => 'application/x-koan',
  181. 'smi' => 'application/smil',
  182. 'smil' => 'application/smil',
  183. 'snd' => 'audio/basic',
  184. 'so' => 'application/octet-stream',
  185. 'spl' => 'application/x-futuresplash',
  186. 'src' => 'application/x-wais-source',
  187. 'sv4cpio' => 'application/x-sv4cpio',
  188. 'sv4crc' => 'application/x-sv4crc',
  189. 'svf' => 'application/vnd.svf',
  190. 'svg' => 'image/svg+xml',
  191. //'svgz' => 'image/svg+xml',
  192. 'swf' => 'application/x-shockwave-flash',
  193. 'sxc' => 'application/vnd.sun.xml.calc',
  194. 'sxi' => 'application/vnd.sun.xml.impress',
  195. 'sxw' => 'application/vnd.sun.xml.writer',
  196. 't' => 'application/x-troff',
  197. 'tar' => 'application/x-tar',
  198. 'tcl' => 'application/x-tcl',
  199. 'tex' => 'application/x-tex',
  200. 'texi' => 'application/x-texinfo',
  201. 'texinfo' => 'application/x-texinfo',
  202. 'tga' => 'image/x-targa',
  203. 'tif' => 'image/tif',
  204. 'tiff' => 'image/tiff',
  205. 'tr' => 'application/x-troff',
  206. 'tsv' => 'text/tab-seperated-values',
  207. 'txt' => 'text/plain',
  208. 'ustar' => 'application/x-ustar',
  209. 'vcd' => 'application/x-cdlink',
  210. 'vrml' => 'model/vrml',
  211. 'wav' => 'audio/x-wav',
  212. 'wbmp' => 'image/vnd.wap.wbmp',
  213. 'wbxml' => 'application/vnd.wap.wbxml',
  214. 'wml' => 'text/vnd.wap.wml',
  215. 'wmlc' => 'application/vnd.wap.wmlc',
  216. 'wmls' => 'text/vnd.wap.wmlscript',
  217. 'wmlsc' => 'application/vnd.wap.wmlscriptc',
  218. 'wma' => 'audio/x-ms-wma',
  219. 'wmv' => 'video/x-ms-wmv',
  220. 'wrl' => 'model/vrml',
  221. 'xbm' => 'image/x-xbitmap',
  222. 'xht' => 'application/xhtml+xml',
  223. 'xhtml' => 'application/xhtml+xml',
  224. 'xls' => 'application/vnd.ms-excel',
  225. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  226. 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  227. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  228. 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
  229. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  230. 'xml' => 'text/xml',
  231. 'xpm' => 'image/x-xpixmap',
  232. 'xsl' => 'text/xml',
  233. 'xwd' => 'image/x-windowdump',
  234. 'xyz' => 'chemical/x-xyz',
  235. 'zip' => 'application/zip'
  236. );
  237. if ($filename === true) {
  238. return $mime_types;
  239. }
  240. //get the extension of the file
  241. $extension = explode('.', $filename);
  242. //$filename will be an array if a . was found
  243. if (is_array($extension)) {
  244. $extension = strtolower($extension[sizeof($extension) - 1]);
  245. }
  246. //file without extension
  247. else {
  248. $extension = 'empty';
  249. }
  250. //if the extension is found, return the content type
  251. if (isset($mime_types[$extension])) {
  252. return $mime_types[$extension];
  253. }
  254. //else return octet-stream
  255. return 'application/octet-stream';
  256. }
  257. /**
  258. * @return true if the user is allowed to see the document, false otherwise
  259. * @author Sergio A Kessler, first version
  260. * @author Roan Embrechts, bugfix
  261. * @todo ??not only check if a file is visible, but also check if the user is allowed to see the file??
  262. */
  263. public static function file_visible_to_user($this_course, $doc_url) {
  264. $current_session_id = api_get_session_id();
  265. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  266. if ($is_allowed_to_edit) {
  267. return true;
  268. } else {
  269. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  270. $tbl_item_property = $this_course . 'item_property';
  271. $doc_url = Database::escape_string($doc_url);
  272. //$doc_url = addslashes($doc_url);
  273. $query = "SELECT 1 FROM $tbl_document AS docs,$tbl_item_property AS props
  274. WHERE props.tool = 'document' AND docs.id=props.ref AND props.visibility <> '1' AND docs.path = '$doc_url'";
  275. //echo $query;
  276. $result = Database::query($query);
  277. return (Database::num_rows($result) == 0);
  278. }
  279. }
  280. /**
  281. * This function streams a file to the client
  282. *
  283. * @param string $full_file_name
  284. * @param boolean $forced
  285. * @param string $name
  286. * @return false if file doesn't exist, true if stream succeeded
  287. */
  288. public static function file_send_for_download($full_file_name, $forced = false, $name = '') {
  289. if (!is_file($full_file_name)) {
  290. return false;
  291. }
  292. $filename = ($name == '') ? basename($full_file_name) : replace_dangerous_char($name);
  293. $len = filesize($full_file_name);
  294. if ($forced) {
  295. //force the browser to save the file instead of opening it
  296. header('Content-type: application/octet-stream');
  297. //header('Content-Type: application/force-download');
  298. header('Content-length: ' . $len);
  299. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  300. header('Content-Disposition: filename= ' . $filename);
  301. } else {
  302. header('Content-Disposition: attachment; filename= ' . $filename);
  303. }
  304. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  305. header('Pragma: ');
  306. header('Cache-Control: ');
  307. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  308. }
  309. header('Content-Description: ' . $filename);
  310. header('Content-transfer-encoding: binary');
  311. $fp = fopen($full_file_name, 'r');
  312. fpassthru($fp);
  313. return true;
  314. } else {
  315. //no forced download, just let the browser decide what to do according to the mimetype
  316. $content_type = self::file_get_mime_type($filename);
  317. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  318. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  319. // Commented to avoid double caching declaration when playing with IE and HTTPS
  320. //header('Cache-Control: no-cache, must-revalidate');
  321. //header('Pragma: no-cache');
  322. switch ($content_type) {
  323. case 'text/html':
  324. $encoding = @api_detect_encoding_html(file_get_contents($full_file_name));
  325. if (!empty($encoding)) {
  326. $content_type .= '; charset=' . $encoding;
  327. }
  328. break;
  329. case 'text/plain':
  330. $encoding = @api_detect_encoding(strip_tags(file_get_contents($full_file_name)));
  331. if (!empty($encoding)) {
  332. $content_type .= '; charset=' . $encoding;
  333. }
  334. break;
  335. }
  336. header('Content-type: ' . $content_type);
  337. header('Content-Length: ' . $len);
  338. $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  339. if (strpos($user_agent, 'msie')) {
  340. header('Content-Disposition: ; filename= ' . $filename);
  341. } else {
  342. header('Content-Disposition: inline; filename= ' . $filename);
  343. }
  344. readfile($full_file_name);
  345. return true;
  346. }
  347. }
  348. /**
  349. * This function streams a string to the client for download.
  350. * You have to ensure that the calling script then stops processing (exit();)
  351. * otherwise it may cause subsequent use of the page to want to download
  352. * other pages in php rather than interpreting them.
  353. *
  354. * @param string The string contents
  355. * @param boolean Whether "save" mode is forced (or opening directly authorized)
  356. * @param string The name of the file in the end (including extension)
  357. * @return false if file doesn't exist, true if stream succeeded
  358. */
  359. public static function string_send_for_download($full_string, $forced = false, $name = '') {
  360. $filename = $name;
  361. $len = strlen($full_string);
  362. if ($forced) {
  363. //force the browser to save the file instead of opening it
  364. header('Content-type: application/octet-stream');
  365. //header('Content-Type: application/force-download');
  366. header('Content-length: ' . $len);
  367. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  368. header('Content-Disposition: filename= ' . $filename);
  369. } else {
  370. header('Content-Disposition: attachment; filename= ' . $filename);
  371. }
  372. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  373. header('Pragma: ');
  374. header('Cache-Control: ');
  375. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  376. }
  377. header('Content-Description: ' . $filename);
  378. header('Content-transfer-encoding: binary');
  379. //$fp = fopen($full_string, 'r');
  380. //fpassthru($fp);
  381. echo $full_string;
  382. return true;
  383. //You have to ensure that the calling script then stops processing (exit();)
  384. //otherwise it may cause subsequent use of the page to want to download
  385. //other pages in php rather than interpreting them.
  386. } else {
  387. //no forced download, just let the browser decide what to do according to the mimetype
  388. $content_type = self::file_get_mime_type($filename);
  389. header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  390. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  391. header('Cache-Control: no-cache, must-revalidate');
  392. header('Pragma: no-cache');
  393. switch ($content_type) {
  394. case 'text/html':
  395. $encoding = @api_detect_encoding_html($full_string);
  396. if (!empty($encoding)) {
  397. $content_type .= '; charset=' . $encoding;
  398. }
  399. break;
  400. case 'text/plain':
  401. $encoding = @api_detect_encoding(strip_tags($full_string));
  402. if (!empty($encoding)) {
  403. $content_type .= '; charset=' . $encoding;
  404. }
  405. break;
  406. }
  407. header('Content-type: ' . $content_type);
  408. header('Content-Length: ' . $len);
  409. $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  410. if (strpos($user_agent, 'msie')) {
  411. header('Content-Disposition: ; filename= ' . $filename);
  412. } else {
  413. header('Content-Disposition: inline; filename= ' . $filename);
  414. }
  415. echo($full_string);
  416. //You have to ensure that the calling script then stops processing (exit();)
  417. //otherwise it may cause subsequent use of the page to want to download
  418. //other pages in php rather than interpreting them.
  419. return true;
  420. }
  421. }
  422. /**
  423. * Fetches all document data for the given user/group
  424. *
  425. * @param array $_course
  426. * @param string $path
  427. * @param int $to_group_id
  428. * @param int $to_user_id
  429. * @param boolean $can_see_invisible
  430. * @return array with all document data
  431. */
  432. public static function get_all_document_data($_course, $path = '/', $to_group_id = 0, $to_user_id = NULL, $can_see_invisible = false, $search = false) {
  433. $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  434. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  435. if (!is_null($to_user_id)) {
  436. $to_field = 'last.to_user_id';
  437. $to_value = $to_user_id;
  438. } else {
  439. $to_field = 'last.to_group_id';
  440. $to_value = $to_group_id;
  441. }
  442. //escape underscores in the path so they don't act as a wildcard
  443. $path = Database::escape_string(str_replace('_', '\_', $path));
  444. $to_user_id = Database::escape_string($to_user_id);
  445. $to_value = Database::escape_string($to_value);
  446. $visibility_bit = ' <> 2';
  447. //the given path will not end with a slash, unless it's the root '/'
  448. //so no root -> add slash
  449. $added_slash = ($path == '/') ? '' : '/';
  450. //condition for the session
  451. $current_session_id = api_get_session_id();
  452. $condition_session = " AND (id_session = '$current_session_id' OR id_session = '0')";
  453. //condition for search (get ALL folders and documents)
  454. $sql = "SELECT docs.id,
  455. docs.filetype,
  456. docs.path,
  457. docs.title,
  458. docs.comment,
  459. docs.size,
  460. docs.readonly,
  461. docs.session_id,
  462. last.id_session item_property_session_id,
  463. last.lastedit_date,
  464. last.visibility,
  465. last.insert_user_id
  466. FROM " . $TABLE_ITEMPROPERTY . " AS last INNER JOIN " . $TABLE_DOCUMENT . " AS docs
  467. ON (docs.id = last.ref AND last.tool = '" . TOOL_DOCUMENT . "' AND docs.c_id = {$_course['real_id']} AND last.c_id = {$_course['real_id']})
  468. WHERE
  469. docs.path LIKE '" . $path . $added_slash . "%' AND
  470. docs.path NOT LIKE '" . $path . $added_slash . "%/%' AND
  471. " . $to_field . " = " . $to_value . " AND
  472. last.visibility" . $visibility_bit . $condition_session;
  473. $result = Database::query($sql);
  474. $doc_list = array();
  475. $document_data = array();
  476. $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
  477. if ($result !== false && Database::num_rows($result) != 0) {
  478. while ($row = Database::fetch_array($result, 'ASSOC')) {
  479. if (api_is_coach()) {
  480. //Looking for course items that are invisible to hide it in the session
  481. if (in_array($row['id'], array_keys($doc_list))) {
  482. if ($doc_list[$row['id']]['item_property_session_id'] == 0 && $doc_list[$row['id']]['session_id'] == 0) {
  483. if ($doc_list[$row['id']]['visibility'] == 0) {
  484. unset($document_data[$row['id']]);
  485. continue;
  486. }
  487. }
  488. }
  489. $doc_list[$row['id']] = $row;
  490. }
  491. if (!api_is_coach() && !$is_allowed_to_edit) {
  492. $doc_list[] = $row;
  493. }
  494. if ($row['filetype'] == 'file' && pathinfo($row['path'], PATHINFO_EXTENSION) == 'html') {
  495. //Templates management
  496. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  497. $sql_is_template = "SELECT id FROM $table_template
  498. WHERE course_code = '" . $_course['code'] . "'
  499. AND user_id = '" . api_get_user_id() . "'
  500. AND ref_doc = '" . $row['id'] . "'";
  501. $template_result = Database::query($sql_is_template);
  502. $row['is_template'] = (Database::num_rows($template_result) > 0) ? 1 : 0;
  503. }
  504. //just filling $document_data
  505. $document_data[$row['id']] = $row;
  506. }
  507. //Only for the student we filter the results see BT#1652
  508. if (!api_is_coach() && !$is_allowed_to_edit) {
  509. $ids_to_remove = array();
  510. $my_repeat_ids = $temp = array();
  511. //Selecting repetead ids
  512. foreach ($doc_list as $row) {
  513. if (in_array($row['id'], array_keys($temp))) {
  514. $my_repeat_ids[] = $row['id'];
  515. }
  516. $temp[$row['id']] = $row;
  517. }
  518. //@todo use the DocumentManager::is_visible function
  519. //Checking disponibility in a session
  520. foreach ($my_repeat_ids as $id) {
  521. foreach ($doc_list as $row) {
  522. if ($id == $row['id']) {
  523. if ($row['visibility'] == 0 && $row['item_property_session_id'] == 0) {
  524. $delete_repeated[$id] = true;
  525. }
  526. if ($row['visibility'] == 0 && $row['item_property_session_id'] != 0) {
  527. $delete_repeated[$id] = true;
  528. }
  529. }
  530. }
  531. }
  532. foreach ($doc_list as $key => $row) {
  533. if (in_array($row['visibility'], array('0', '2')) && !in_array($row['id'], $my_repeat_ids)) {
  534. $ids_to_remove[] = $row['id'];
  535. unset($doc_list[$key]);
  536. }
  537. }
  538. foreach ($document_data as $row) {
  539. if (in_array($row['id'], $ids_to_remove)) {
  540. unset($document_data[$row['id']]);
  541. }
  542. if (isset($delete_repeated[$row['id']]) && $delete_repeated[$row['id']]) {
  543. unset($document_data[$row['id']]);
  544. }
  545. }
  546. //Checking parents visibility
  547. $final_document_data = array();
  548. foreach ($document_data as $row) {
  549. $is_visible = DocumentManager::check_visibility_tree($row['id'], $_course['code'], $current_session_id, api_get_user_id());
  550. if ($is_visible) {
  551. $final_document_data[$row['id']] = $row;
  552. }
  553. }
  554. } else {
  555. $final_document_data = $document_data;
  556. }
  557. return $final_document_data;
  558. } else {
  559. //display_error("Error getting document info from database (".Database::error().")!");
  560. return false;
  561. }
  562. }
  563. /**
  564. * Gets the paths of all folders in a course
  565. * can show all folders (exept for the deleted ones) or only visible ones
  566. * @param array $_course
  567. * @param boolean $can_see_invisible
  568. * @param int $to_group_id
  569. * @return array with paths
  570. */
  571. public static function get_all_document_folders($_course, $to_group_id = '0', $can_see_invisible = false) {
  572. $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  573. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  574. $to_group_id = intval($to_group_id);
  575. if ($can_see_invisible) {
  576. //condition for the session
  577. $session_id = api_get_session_id();
  578. $condition_session = api_get_session_condition($session_id);
  579. $sql = "SELECT DISTINCT docs.id, path
  580. FROM $TABLE_ITEMPROPERTY AS last INNER JOIN $TABLE_DOCUMENT AS docs
  581. ON (docs.id = last.ref AND last.tool = '" . TOOL_DOCUMENT . "' AND last.c_id = {$_course['real_id']} AND docs.c_id = {$_course['real_id']} )
  582. WHERE
  583. docs.filetype = 'folder' AND
  584. last.to_group_id = " . $to_group_id . " AND
  585. last.visibility <> 2 $condition_session ";
  586. $result = Database::query($sql);
  587. if ($result && Database::num_rows($result) != 0) {
  588. while ($row = Database::fetch_array($result, 'ASSOC')) {
  589. if (DocumentManager::is_folder_to_avoid($row['path'])) {
  590. continue;
  591. }
  592. $document_folders[$row['id']] = $row['path'];
  593. }
  594. //sort($document_folders);
  595. natsort($document_folders);
  596. //return results
  597. return $document_folders;
  598. } else {
  599. return false;
  600. }
  601. } else {
  602. //no invisible folders
  603. //condition for the session
  604. $session_id = api_get_session_id();
  605. $condition_session = api_get_session_condition($session_id);
  606. //get visible folders
  607. $visible_sql = "SELECT DISTINCT docs.id, path
  608. FROM " . $TABLE_ITEMPROPERTY . " AS last, " . $TABLE_DOCUMENT . " AS docs
  609. WHERE docs.id = last.ref
  610. AND docs.filetype = 'folder'
  611. AND last.tool = '" . TOOL_DOCUMENT . "'
  612. AND last.to_group_id = " . $to_group_id . "
  613. AND last.visibility = 1 $condition_session AND
  614. last.c_id = {$_course['real_id']} AND
  615. docs.c_id = {$_course['real_id']} ";
  616. $visibleresult = Database::query($visible_sql);
  617. while ($all_visible_folders = Database::fetch_array($visibleresult, 'ASSOC')) {
  618. $visiblefolders[$all_visible_folders['id']] = $all_visible_folders['path'];
  619. }
  620. //condition for the session
  621. $session_id = api_get_session_id();
  622. $condition_session = api_get_session_condition($session_id);
  623. //get invisible folders
  624. $invisible_sql = "SELECT DISTINCT docs.id, path
  625. FROM " . $TABLE_ITEMPROPERTY . " AS last, " . $TABLE_DOCUMENT . " AS docs
  626. WHERE docs.id = last.ref
  627. AND docs.filetype = 'folder'
  628. AND last.tool = '" . TOOL_DOCUMENT . "'
  629. AND last.to_group_id = " . $to_group_id . "
  630. AND last.visibility = 0 $condition_session AND
  631. last.c_id = {$_course['real_id']} AND
  632. docs.c_id = {$_course['real_id']} ";
  633. $invisibleresult = Database::query($invisible_sql);
  634. while ($invisible_folders = Database::fetch_array($invisibleresult, 'ASSOC')) {
  635. //condition for the session
  636. $session_id = api_get_session_id();
  637. $condition_session = api_get_session_condition($session_id);
  638. //get visible folders in the invisible ones -> they are invisible too
  639. $folder_in_invisible_sql = "SELECT DISTINCT docs.id, path
  640. FROM " . $TABLE_ITEMPROPERTY . " AS last, " . $TABLE_DOCUMENT . " AS docs
  641. WHERE docs.id = last.ref
  642. AND docs.path LIKE '" . Database::escape_string($invisible_folders['path']) . "/%'
  643. AND docs.filetype = 'folder'
  644. AND last.tool = '" . TOOL_DOCUMENT . "'
  645. AND last.to_group_id = " . $to_group_id . "
  646. AND last.visibility = 1 $condition_session AND
  647. last.c_id = {$_course['real_id']} AND
  648. docs.c_id = {$_course['real_id']} ";
  649. $folder_in_invisible_result = Database::query($folder_in_invisible_sql);
  650. while ($folders_in_invisible_folder = Database::fetch_array($folder_in_invisible_result, 'ASSOC')) {
  651. $invisiblefolders[$folders_in_invisible_folder['id']] = $folders_in_invisible_folder['path'];
  652. }
  653. }
  654. //if both results are arrays -> //calculate the difference between the 2 arrays -> only visible folders are left :)
  655. if (is_array($visiblefolders) && is_array($invisiblefolders)) {
  656. $document_folders = array_diff($visiblefolders, $invisiblefolders);
  657. natsort($document_folders);
  658. return $document_folders;
  659. } elseif (is_array($visiblefolders)) {
  660. //only visible folders found
  661. //sort($visiblefolders);
  662. natsort($visiblefolders);
  663. return $visiblefolders;
  664. } else {
  665. //no visible folders found
  666. return false;
  667. }
  668. }
  669. }
  670. /**
  671. * This check if a document has the readonly property checked, then see if the user
  672. * is the owner of this file, if all this is true then return true.
  673. *
  674. * @param array $_course
  675. * @param int $user_id id of the current user
  676. * @param string $file path stored in the database
  677. * @param int $document_id in case you dont have the file path ,insert the id of the file here and leave $file in blank ''
  678. * @return boolean true/false
  679. * */
  680. public static function check_readonly($_course, $user_id, $file, $document_id = '', $to_delete = false) {
  681. if (!(!empty($document_id) && is_numeric($document_id))) {
  682. $document_id = self::get_document_id($_course, $file);
  683. }
  684. $TABLE_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  685. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  686. $course_id = $_course['real_id'];
  687. if ($to_delete) {
  688. if (self::is_folder($_course, $document_id)) {
  689. if (!empty($file)) {
  690. $path = Database::escape_string($file);
  691. $what_to_check_sql = "SELECT td.id, readonly, tp.insert_user_id FROM " . $TABLE_DOCUMENT . " td , $TABLE_PROPERTY tp
  692. WHERE td.c_id = $course_id AND
  693. tp.c_id = $course_id AND
  694. tp.ref= td.id AND
  695. (path='" . $path . "' OR path LIKE BINARY '" . $path . "/%' ) ";
  696. //get all id's of documents that are deleted
  697. $what_to_check_result = Database::query($what_to_check_sql);
  698. if ($what_to_check_result && Database::num_rows($what_to_check_result) != 0) {
  699. // file with readonly set to 1 exist?
  700. $readonly_set = false;
  701. while ($row = Database::fetch_array($what_to_check_result)) {
  702. //query to delete from item_property table
  703. if ($row['readonly'] == 1) {
  704. if (!($row['insert_user_id'] == $user_id)) {
  705. $readonly_set = true;
  706. break;
  707. }
  708. }
  709. }
  710. if ($readonly_set) {
  711. return true;
  712. }
  713. }
  714. }
  715. return false;
  716. }
  717. }
  718. if (!empty($document_id)) {
  719. $sql = "SELECT a.insert_user_id, b.readonly FROM $TABLE_PROPERTY a, $TABLE_DOCUMENT b
  720. WHERE
  721. a.c_id = $course_id AND
  722. b.c_id = $course_id AND
  723. a.ref = b.id and a.ref= $document_id LIMIT 1";
  724. $resultans = Database::query($sql);
  725. $doc_details = Database ::fetch_array($resultans, 'ASSOC');
  726. if ($doc_details['readonly'] == 1) {
  727. return !($doc_details['insert_user_id'] == $user_id || api_is_platform_admin());
  728. }
  729. }
  730. return false;
  731. }
  732. /**
  733. * This check if a document is a folder or not
  734. * @param array $_course
  735. * @param int $document_id of the item
  736. * @return boolean true/false
  737. * */
  738. public static function is_folder($_course, $document_id) {
  739. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  740. $course_id = $_course['real_id'];
  741. $document_id = Database::escape_string($document_id);
  742. $result = Database::fetch_array(Database::query("SELECT filetype FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND id= $document_id"), 'ASSOC');
  743. return $result['filetype'] == 'folder';
  744. }
  745. public static function delete_document_from_db($document_id, $course_info = array(), $session_id = 0, $remove_content_from_db = false) {
  746. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  747. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  748. //Deleting from the DB
  749. $user_id = api_get_user_id();
  750. if (empty($course_info)) {
  751. $course_info = api_get_course_info();
  752. }
  753. if (empty($session_id)) {
  754. $session_id = api_get_session_id();
  755. }
  756. //Soft DB delete
  757. api_item_property_update($course_info, TOOL_DOCUMENT, $document_id, 'delete', $user_id, null, null, null, null, $session_id);
  758. self::delete_document_from_search_engine($course_info['code'], $document_id);
  759. self::unset_document_as_template($document_id, $course_info['code'], $user_id);
  760. //Hard DB delete
  761. if ($remove_content_from_db) {
  762. $sql = "DELETE FROM $TABLE_ITEMPROPERTY WHERE c_id = {$course_info['real_id']} AND ref = " . $document_id . " AND tool='" . TOOL_DOCUMENT . "'";
  763. Database::query($sql);
  764. $sql = "DELETE FROM " . $TABLE_DOCUMENT . " WHERE c_id = {$course_info['real_id']} AND id = " . $document_id;
  765. Database::query($sql);
  766. self::delete_document_metadata($document_id);
  767. }
  768. }
  769. public static function delete_document_metadata($document_id) {
  770. //needed to deleted medadata
  771. require_once api_get_path(SYS_CODE_PATH) . 'metadata/md_funcs.php';
  772. require_once api_get_path(LIBRARY_PATH) . 'fileManage.lib.php';
  773. $mdStore = new mdstore(true);
  774. //delete metadata
  775. $eid = 'Document' . '.' . $document_id;
  776. $mdStore->mds_delete($eid);
  777. $mdStore->mds_delete_offspring($eid);
  778. }
  779. /**
  780. * This deletes a document by changing visibility to 2, renaming it to filename_DELETED_#id
  781. * Files/folders that are inside a deleted folder get visibility 2
  782. *
  783. * @param array $_course
  784. * @param string $path, path stored in the database
  785. * @param string ,$base_work_dir, path to the documents folder
  786. * @return boolean true/false
  787. * @todo now only files/folders in a folder get visibility 2, we should rename them too.
  788. */
  789. public static function delete_document($_course, $path, $base_work_dir) {
  790. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  791. if (empty($path) || empty($base_work_dir)) {
  792. return false;
  793. }
  794. $course_id = $_course['real_id'];
  795. //first, delete the actual document...
  796. $document_id = self :: get_document_id($_course, $path);
  797. $document_exists_in_disk = file_exists($base_work_dir . $path);
  798. $new_path = $path . '_DELETED_' . $document_id;
  799. $current_session_id = api_get_session_id();
  800. $file_deleted_from_db = false;
  801. $file_deleted_from_disk = false;
  802. $file_renamed_from_disk = false;
  803. if ($document_id) {
  804. self::delete_document_from_db($document_id);
  805. //checking
  806. //$file_exists_in_db = self::get_document_data_by_id($document_id, $_course['code']);
  807. $file_deleted_from_db = true;
  808. }
  809. if ($document_exists_in_disk) {
  810. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  811. //Deleted files are *really* deleted
  812. $what_to_delete_sql = "SELECT id FROM " . $TABLE_DOCUMENT . " WHERE c_id = $course_id AND path='" . $path . "' OR path LIKE BINARY '" . $path . "/%'";
  813. //get all id's of documents that are deleted
  814. $what_to_delete_result = Database::query($what_to_delete_sql);
  815. if ($what_to_delete_result && Database::num_rows($what_to_delete_result) != 0) {
  816. //delete all item_property entries
  817. while ($row = Database::fetch_array($what_to_delete_result)) {
  818. //query to delete from item_property table (hard way)
  819. self::delete_document_from_db($row['id'], $_course, $current_session_id, true);
  820. }
  821. //delete documents, do it like this so metadata get's deleted too
  822. //update_db_info('delete', $path);
  823. //throw it away
  824. my_delete($base_work_dir . $path);
  825. $file_deleted_from_disk = true;
  826. }
  827. } else {
  828. //Set visibility to 2 and rename file/folder to xxx_DELETED_#id (soft delete)
  829. if (is_file($base_work_dir . $path) || is_dir($base_work_dir . $path)) {
  830. if (rename($base_work_dir . $path, $base_work_dir . $new_path)) {
  831. $sql = "UPDATE $TABLE_DOCUMENT set path='" . $new_path . "' WHERE c_id = $course_id AND id='" . $document_id . "'";
  832. Database::query($sql);
  833. $sql = "SELECT id, path FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND path LIKE BINARY '" . $path . "/%'";
  834. $result = Database::query($sql);
  835. if ($result && Database::num_rows($result) > 0) {
  836. while ($deleted_items = Database::fetch_array($result, 'ASSOC')) {
  837. self::delete_document_from_db($deleted_items['id']);
  838. //Change path of subfolders and documents in database
  839. $old_item_path = $deleted_items['path'];
  840. $new_item_path = $new_path . substr($old_item_path, strlen($path));
  841. $sql = "UPDATE $TABLE_DOCUMENT set path = '" . $new_item_path . "' WHERE c_id = $course_id AND id = " . $deleted_items['id'];
  842. Database::query($sql);
  843. }
  844. }
  845. $file_renamed_from_disk = true;
  846. } else {
  847. //Couldn't rename - file permissions problem?
  848. error_log(__FILE__ . ' ' . __LINE__ . ': Error renaming ' . $base_work_dir . $path . ' to ' . $base_work_dir . $new_path . '. This is probably due to file permissions', 0);
  849. }
  850. }
  851. }
  852. }
  853. //Checking inconsistency
  854. if ($file_deleted_from_db && $file_deleted_from_disk ||
  855. $file_deleted_from_db && $file_renamed_from_disk) {
  856. return true;
  857. } else {
  858. //Something went wrong
  859. //The file or directory isn't there anymore (on the filesystem)
  860. // This means it has been removed externally. To prevent a
  861. // blocking error from happening, we drop the related items from the
  862. // item_property and the document table.
  863. error_log(__FILE__ . ' ' . __LINE__ . ': System inconsistency detected. The file or directory ' . $base_work_dir . $path . ' seems to have been removed from the filesystem independently from the web platform. To restore consistency, the elements using the same path will be removed from the database', 0);
  864. return false;
  865. }
  866. }
  867. /**
  868. * Removes documents from search engine database
  869. *
  870. * @param string $course_id Course code
  871. * @param int $document_id Document id to delete
  872. */
  873. public static function delete_document_from_search_engine($course_id, $document_id) {
  874. // remove from search engine if enabled
  875. if (api_get_setting('search_enabled') == 'true') {
  876. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  877. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  878. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_DOCUMENT, $document_id);
  879. $res = Database::query($sql);
  880. if (Database::num_rows($res) > 0) {
  881. $row2 = Database::fetch_array($res);
  882. require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
  883. $di = new ChamiloIndexer();
  884. $di->remove_document((int) $row2['search_did']);
  885. }
  886. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  887. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_DOCUMENT, $document_id);
  888. Database::query($sql);
  889. // remove terms from db
  890. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  891. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $document_id);
  892. }
  893. }
  894. /**
  895. * Gets the id of a document with a given path
  896. *
  897. * @param array $_course
  898. * @param string $path
  899. * @return int id of document / false if no doc found
  900. */
  901. public static function get_document_id($course_info, $path) {
  902. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  903. $course_id = $course_info['real_id'];
  904. $path = Database::escape_string($path);
  905. if (!empty($course_id) && !empty($path)) {
  906. $sql = "SELECT id FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND path LIKE BINARY '$path' LIMIT 1";
  907. $result = Database::query($sql);
  908. if ($result && Database::num_rows($result)) {
  909. $row = Database::fetch_array($result);
  910. return intval($row[0]);
  911. }
  912. }
  913. return false;
  914. }
  915. /**
  916. * Gets the document data with a given id
  917. *
  918. * @param array $_course
  919. * @param string $path
  920. * @todo load parent_id
  921. * @return int id of document / false if no doc found
  922. */
  923. public static function get_document_data_by_id($id, $course_code, $load_parents = false) {
  924. $course_info = api_get_course_info($course_code);
  925. $course_id = $course_info['real_id'];
  926. if (empty($course_info)) {
  927. return false;
  928. }
  929. $www = api_get_path(WEB_COURSE_PATH) . $course_info['path'] . '/document';
  930. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  931. $id = intval($id);
  932. $sql = "SELECT * FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND id = $id ";
  933. $result = Database::query($sql);
  934. if ($result && Database::num_rows($result) == 1) {
  935. $row = Database::fetch_array($result, 'ASSOC');
  936. //@todo need to clarify the name of the URLs not nice right now
  937. $url_path = urlencode($row['path']);
  938. $path = str_replace('%2F', '/', $url_path);
  939. $row['url'] = api_get_path(WEB_CODE_PATH) . 'document/showinframes.php?cidReq=' . $course_code . '&id=' . $id;
  940. $row['document_url'] = api_get_path(WEB_CODE_PATH) . 'document/document.php?cidReq=' . $course_code . '&id=' . $id;
  941. $row['absolute_path'] = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/document' . $row['path'];
  942. $row['absolute_path_from_document'] = '/document' . $row['path'];
  943. $pathinfo = pathinfo($row['path']);
  944. $row['absolute_parent_path'] = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/document' . $pathinfo['dirname'] . '/';
  945. $row['direct_url'] = $www . $path;
  946. if (dirname($row['path']) == '.') {
  947. $row['parent_id'] = '0';
  948. } else {
  949. $row['parent_id'] = self::get_document_id($course_info, dirname($row['path']));
  950. }
  951. $parents = array();
  952. //Use to generate parents (needed for the breadcrumb)
  953. //@todo sorry but this for is here because there's not a parent_id in the document table so we parsed the path!!
  954. $visibility = true;
  955. if ($load_parents) {
  956. $dir_array = explode('/', $row['path']);
  957. $dir_array = array_filter($dir_array);
  958. $array_len = count($dir_array) + 1;
  959. $real_dir = '';
  960. for ($i = 1; $i < $array_len; $i++) {
  961. //$sub_visibility = true;
  962. $real_dir .= '/' . $dir_array[$i];
  963. $parent_id = self::get_document_id($course_info, $real_dir);
  964. if (!empty($parent_id)) {
  965. $sub_document_data = self::get_document_data_by_id($parent_id, $course_code, false);
  966. //@todo add visibility here
  967. /* $sub_visibility = self::is_visible_by_id($parent_id, $course_info, api_get_session_id(), api_get_user_id());
  968. if ($visibility && $sub_visibility == false) {
  969. $visibility = false;
  970. }
  971. */
  972. $parents[] = $sub_document_data;
  973. }
  974. }
  975. }
  976. //$row['visibility_for_user'] = $visibility;
  977. $row['parents'] = $parents;
  978. return $row;
  979. }
  980. return false;
  981. }
  982. /**
  983. * Allow to set a specific document as a new template for FCKEditor for a particular user in a particular course
  984. *
  985. * @param string $title
  986. * @param string $description
  987. * @param int $document_id_for_template the document id
  988. * @param string $couse_code
  989. * @param int $user_id
  990. */
  991. public static function set_document_as_template($title, $description, $document_id_for_template, $couse_code, $user_id, $image) {
  992. // Database table definition
  993. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  994. // creating the sql statement
  995. $sql = "INSERT INTO " . $table_template . "
  996. (title, description, course_code, user_id, ref_doc, image)
  997. VALUES (
  998. '" . Database::escape_string($title) . "',
  999. '" . Database::escape_string($description) . "',
  1000. '" . Database::escape_string($couse_code) . "',
  1001. '" . Database::escape_string($user_id) . "',
  1002. '" . Database::escape_string($document_id_for_template) . "',
  1003. '" . Database::escape_string($image) . "')";
  1004. Database::query($sql);
  1005. return true;
  1006. }
  1007. /**
  1008. * Unset a document as template
  1009. *
  1010. * @param int $document_id
  1011. * @param string $couse_code
  1012. * @param int $user_id
  1013. */
  1014. public static function unset_document_as_template($document_id, $course_code, $user_id) {
  1015. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  1016. $course_code = Database::escape_string($course_code);
  1017. $user_id = Database::escape_string($user_id);
  1018. $document_id = Database::escape_string($document_id);
  1019. $sql = 'SELECT id FROM ' . $table_template . ' WHERE course_code="' . $course_code . '" AND user_id="' . $user_id . '" AND ref_doc="' . $document_id . '"';
  1020. $result = Database::query($sql);
  1021. $template_id = Database::result($result, 0, 0);
  1022. include_once(api_get_path(LIBRARY_PATH) . 'fileManage.lib.php');
  1023. my_delete(api_get_path(SYS_CODE_PATH) . 'upload/template_thumbnails/' . $template_id . '.jpg');
  1024. $sql = 'DELETE FROM ' . $table_template . ' WHERE course_code="' . $course_code . '" AND user_id="' . $user_id . '" AND ref_doc="' . $document_id . '"';
  1025. Database::query($sql);
  1026. }
  1027. /**
  1028. * Return true if the documentpath have visibility=1 as item_property (you should use the is_visible_by_id)
  1029. *
  1030. * @param string $document_path the relative complete path of the document
  1031. * @param array $course the _course array info of the document's course
  1032. */
  1033. public static function is_visible($doc_path, $course, $session_id = 0, $file_type = 'file') {
  1034. $docTable = Database::get_course_table(TABLE_DOCUMENT);
  1035. $propTable = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1036. $course_id = $course['real_id'];
  1037. //note the extra / at the end of doc_path to match every path in the document table that is part of the document path
  1038. $doc_path = Database::escape_string($doc_path);
  1039. $session_id = intval($session_id);
  1040. $condition = "AND id_session IN ('$session_id', '0') ";
  1041. // The " d.filetype='file' " let the user see a file even if the folder is hidden see #2198
  1042. /*
  1043. When using hotpotatoes files, a new html files are generated in the hotpotatoes folder
  1044. to display the test.
  1045. The genuine html file is copied to math4.htm(user_id).t.html
  1046. Images files are not copied, and keep same name.
  1047. To check the html file visibility, we don't have to check file math4.htm(user_id).t.html but file math4.htm
  1048. In this case, we have to remove (user_id).t.html to check the visibility of the file
  1049. For images, we just check the path of the image file.
  1050. Exemple of hotpotatoes folder :
  1051. A.jpg
  1052. maths4-consigne.jpg
  1053. maths4.htm
  1054. maths4.htm1.t.html
  1055. maths4.htm52.t.html
  1056. maths4.htm654.t.html
  1057. omega.jpg
  1058. theta.jpg
  1059. */
  1060. if (strpos($doc_path, 'HotPotatoes_files') && preg_match("/\.t\.html$/", $doc_path)) {
  1061. $doc_path = substr($doc_path, 0, strlen($doc_path) - 7 - strlen(api_get_user_id()));
  1062. }
  1063. if (!in_array($file_type, array('file', 'folder'))) {
  1064. $file_type = 'file';
  1065. }
  1066. $sql = "SELECT visibility FROM $docTable d, $propTable ip
  1067. WHERE d.c_id = $course_id AND
  1068. ip.c_id = $course_id AND
  1069. d.id = ip.ref AND
  1070. ip.tool = '" . TOOL_DOCUMENT . "' $condition AND
  1071. filetype = '$file_type' AND locate(concat(path,'/'),'" . $doc_path . "/')=1";
  1072. $result = Database::query($sql);
  1073. $is_visible = false;
  1074. if (Database::num_rows($result) > 0) {
  1075. $row = Database::fetch_array($result, 'ASSOC');
  1076. if ($row['visibility'] == 1) {
  1077. $is_visible = $_SESSION['is_allowed_in_course'] || api_is_platform_admin();
  1078. }
  1079. }
  1080. //improved protection of documents viewable directly through the url: incorporates the same protections of the course at the url of documents: access allowed for the whole world Open, access allowed for users registered on the platform Private access, document accessible only to course members (see the Users list), Completely closed; the document is only accessible to the course admin and teaching assistants.
  1081. //return $_SESSION ['is_allowed_in_course'] || api_is_platform_admin();
  1082. return $is_visible;
  1083. }
  1084. /**
  1085. * Return true if user can see a file
  1086. *
  1087. * @param int document id
  1088. * @param array course info
  1089. * @param array $course the _course array info of the document's course
  1090. * @return bool
  1091. */
  1092. public static function is_visible_by_id($doc_id, $course_info, $session_id, $user_id, $admins_can_see_everything = true) {
  1093. $is_visible = false;
  1094. $user_in_course = false;
  1095. //1. Checking the course array
  1096. if (empty($course_info)) {
  1097. $course_info = api_get_course_info();
  1098. if (empty($course_info)) {
  1099. return false;
  1100. }
  1101. }
  1102. $doc_id = intval($doc_id);
  1103. $session_id = intval($session_id);
  1104. //2. Course and Session visibility are handle in local.inc.php/global.inc.php
  1105. //3. Checking if user exist in course/session
  1106. if ($session_id == 0) {
  1107. if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code']) || api_is_platform_admin()) {
  1108. $user_in_course = true;
  1109. }
  1110. //Check if course is open then we can consider that the student is regitered to the course
  1111. if (isset($course_info) && in_array($course_info['visibility'], array(2, 3))) {
  1112. $user_in_course = true;
  1113. }
  1114. } else {
  1115. $user_status = SessionManager::get_user_status_in_course_session($user_id, $course_info['code'], $session_id);
  1116. if (in_array($user_status, array('0', '2', '6'))) {
  1117. //is true if is an student, course session teacher or coach
  1118. $user_in_course = true;
  1119. }
  1120. }
  1121. //4. Checking document visibility (i'm repeating the code in order to be more clear when reading ) - jm
  1122. if ($user_in_course) {
  1123. //4.1 Checking document visibility for a Course
  1124. if ($session_id == 0) {
  1125. $item_info = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, 0);
  1126. if (isset($item_info['visibility'])) {
  1127. // True for admins if document exists
  1128. if ($admins_can_see_everything && api_is_platform_admin()) {
  1129. return true;
  1130. }
  1131. if ($item_info['visibility'] == 1) {
  1132. return true;
  1133. }
  1134. }
  1135. } else {
  1136. //4.2 Checking document visibility for a Course in a Session
  1137. $item_info = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, 0);
  1138. $item_info_in_session = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, $session_id);
  1139. // True for admins if document exists
  1140. if (isset($item_info['visibility'])) {
  1141. if ($admins_can_see_everything && api_is_platform_admin())
  1142. return true;
  1143. }
  1144. if (isset($item_info_in_session['visibility'])) {
  1145. //if ($doc_id == 85) { var_dump($item_info_in_session);}
  1146. if ($item_info_in_session['visibility'] == 1) {
  1147. return true;
  1148. }
  1149. } else {
  1150. if ($item_info['visibility'] == 1) {
  1151. return true;
  1152. }
  1153. }
  1154. }
  1155. } elseif ($admins_can_see_everything && api_is_platform_admin()) {
  1156. return true;
  1157. }
  1158. return false;
  1159. }
  1160. /**
  1161. * Allow attach a certificate to a course
  1162. * @param string The course id
  1163. * @param int The document id
  1164. * @return void()
  1165. */
  1166. function attach_gradebook_certificate($course_id, $document_id) {
  1167. $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1168. $session_id = api_get_session_id();
  1169. if ($session_id == 0 || is_null($session_id)) {
  1170. $sql_session = 'AND (session_id=' . Database::escape_string($session_id) . ' OR isnull(session_id)) ';
  1171. } elseif ($session_id > 0) {
  1172. $sql_session = 'AND session_id=' . Database::escape_string($session_id);
  1173. } else {
  1174. $sql_session = '';
  1175. }
  1176. $sql = 'UPDATE ' . $tbl_category . ' SET document_id="' . Database::escape_string($document_id) . '"
  1177. WHERE course_code="' . Database::escape_string($course_id) . '" ' . $sql_session;
  1178. Database::query($sql);
  1179. }
  1180. /**
  1181. * get the document id of default certificate
  1182. * @param string The course id
  1183. * @return int The default certificate id
  1184. */
  1185. static function get_default_certificate_id($course_id) {
  1186. $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1187. $session_id = api_get_session_id();
  1188. if ($session_id == 0 || is_null($session_id)) {
  1189. $sql_session = 'AND (session_id=' . Database::escape_string($session_id) . ' OR isnull(session_id)) ';
  1190. } elseif ($session_id > 0) {
  1191. $sql_session = 'AND session_id=' . Database::escape_string($session_id);
  1192. } else {
  1193. $sql_session = '';
  1194. }
  1195. $sql = 'SELECT document_id FROM ' . $tbl_category . ' WHERE course_code="' . Database::escape_string($course_id) . '" ' . $sql_session;
  1196. $rs = Database::query($sql);
  1197. $num = Database::num_rows($rs);
  1198. if ($num == 0) {
  1199. return null;
  1200. }
  1201. $row = Database::fetch_array($rs);
  1202. return $row['document_id'];
  1203. }
  1204. /**
  1205. * allow replace user info in file html
  1206. * @param string The course code
  1207. * @return string The html content of the certificate
  1208. */
  1209. static function replace_user_info_into_html($user_id, $course_code, $is_preview = false) {
  1210. $user_id = intval($user_id);
  1211. $course_info = api_get_course_info($course_code);
  1212. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1213. $course_id = $course_info['real_id'];
  1214. $document_id = self::get_default_certificate_id($course_code);
  1215. if ($document_id) {
  1216. $sql = "SELECT path FROM $tbl_document WHERE c_id = $course_id AND id = $document_id";
  1217. $rs = Database::query($sql);
  1218. $new_content = '';
  1219. $all_user_info = array();
  1220. if (Database::num_rows($rs)) {
  1221. $row = Database::fetch_array($rs);
  1222. $filepath = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/document' . $row['path'];
  1223. if (is_file($filepath)) {
  1224. $my_content_html = file_get_contents($filepath);
  1225. }
  1226. $all_user_info = self::get_all_info_to_certificate($user_id, $course_code, $is_preview);
  1227. $info_to_be_replaced_in_content_html = $all_user_info[0];
  1228. $info_to_replace_in_content_html = $all_user_info[1];
  1229. $new_content = str_replace($info_to_be_replaced_in_content_html, $info_to_replace_in_content_html, $my_content_html);
  1230. }
  1231. return array('content' => $new_content, 'variables' => $all_user_info);
  1232. }
  1233. return array();
  1234. }
  1235. /**
  1236. * return all content to replace and all content to be replace
  1237. */
  1238. static function get_all_info_to_certificate($user_id, $course_id, $is_preview = false) {
  1239. $info_list = array();
  1240. $user_id = intval($user_id);
  1241. $course_info = api_get_course_info($course_id);
  1242. //info portal
  1243. $organization_name = api_get_setting('Institution');
  1244. $portal_name = api_get_setting('siteName');
  1245. //Extra user data information
  1246. $extra_user_info_data = UserManager::get_extra_user_data($user_id, false, false, false, true);
  1247. //Student information
  1248. $user_info = api_get_user_info($user_id);
  1249. $first_name = $user_info['firstname'];
  1250. $last_name = $user_info['lastname'];
  1251. $official_code = $user_info['official_code'];
  1252. //Teacher information
  1253. $info_teacher_id = UserManager::get_user_id_of_course_admin_or_session_admin($course_id);
  1254. $teacher_info = api_get_user_info($info_teacher_id);
  1255. $teacher_first_name = $teacher_info['firstname'];
  1256. $teacher_last_name = $teacher_info['lastname'];
  1257. // info gradebook certificate
  1258. $info_grade_certificate = UserManager::get_info_gradebook_certificate($course_id, $user_id);
  1259. $date_certificate = $info_grade_certificate['created_at'];
  1260. $date_long_certificate = '';
  1261. if (!empty($date_certificate)) {
  1262. $date_long_certificate = api_convert_and_format_date($date_certificate);
  1263. $date_no_time = api_convert_and_format_date($date_certificate, DATE_FORMAT_LONG_NO_DAY);
  1264. }
  1265. if ($is_preview) {
  1266. $date_long_certificate = api_convert_and_format_date(api_get_utc_datetime());
  1267. $date_no_time = api_convert_and_format_date(api_get_utc_datetime(), DATE_FORMAT_LONG_NO_DAY);
  1268. }
  1269. $url = api_get_path(WEB_PATH) . 'certificates/index.php?id=' . $info_grade_certificate['id'];
  1270. //replace content
  1271. $info_to_replace_in_content_html = array($first_name,
  1272. $last_name,
  1273. $organization_name,
  1274. $portal_name,
  1275. $teacher_first_name,
  1276. $teacher_last_name,
  1277. $official_code,
  1278. $date_long_certificate,
  1279. $date_no_time,
  1280. $course_id,
  1281. $course_info['name'],
  1282. $info_grade_certificate['grade'],
  1283. $url,
  1284. '<a href="' . $url . '" target="_blank">' . get_lang('CertificateOnlineLink') . '</a>',
  1285. '((certificate_barcode))',
  1286. );
  1287. $info_to_be_replaced_in_content_html = array('((user_firstname))',
  1288. '((user_lastname))',
  1289. '((gradebook_institution))',
  1290. '((gradebook_sitename))',
  1291. '((teacher_firstname))',
  1292. '((teacher_lastname))',
  1293. '((official_code))',
  1294. '((date_certificate))',
  1295. '((date_certificate_no_time))',
  1296. '((course_code))',
  1297. '((course_title))',
  1298. '((gradebook_grade))',
  1299. '((certificate_link))',
  1300. '((certificate_link_html))',
  1301. '((certificate_barcode))',
  1302. );
  1303. if (!empty($extra_user_info_data)) {
  1304. foreach ($extra_user_info_data as $key_extra => $value_extra) {
  1305. $info_to_be_replaced_in_content_html[] = '((' . strtolower($key_extra) . '))';
  1306. $info_to_replace_in_content_html[] = $value_extra;
  1307. }
  1308. }
  1309. $info_list[] = $info_to_be_replaced_in_content_html;
  1310. $info_list[] = $info_to_replace_in_content_html;
  1311. return $info_list;
  1312. }
  1313. /**
  1314. * Remove default certificate
  1315. * @param string The course id
  1316. * @param int The document id of the default certificate
  1317. * @return void()
  1318. */
  1319. function remove_attach_certificate($course_id, $default_certificate_id) {
  1320. $default_certificate = self::get_default_certificate_id($course_id);
  1321. if ((int) $default_certificate == (int) $default_certificate_id) {
  1322. $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1323. $session_id = api_get_session_id();
  1324. if ($session_id == 0 || is_null($session_id)) {
  1325. $sql_session = 'AND (session_id=' . Database::escape_string($session_id) . ' OR isnull(session_id)) ';
  1326. } elseif ($session_id > 0) {
  1327. $sql_session = 'AND session_id=' . Database::escape_string($session_id);
  1328. } else {
  1329. $sql_session = '';
  1330. }
  1331. $sql = 'UPDATE ' . $tbl_category . ' SET document_id=null
  1332. WHERE course_code="' . Database::escape_string($course_id) . '" AND document_id="' . $default_certificate_id . '" ' . $sql_session;
  1333. Database::query($sql);
  1334. }
  1335. }
  1336. /**
  1337. * Create directory certificate
  1338. * @param string The course id
  1339. * @return void()
  1340. */
  1341. static function create_directory_certificate_in_course($course_id) {
  1342. $course_info = api_get_course_info($course_id);
  1343. if (!empty($course_info)) {
  1344. $to_group_id = 0;
  1345. $to_user_id = null;
  1346. $course_dir = $course_info['path'] . "/document/";
  1347. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1348. $base_work_dir = $sys_course_path . $course_dir;
  1349. $base_work_dir_test = $base_work_dir . 'certificates';
  1350. $dir_name = '/certificates';
  1351. $post_dir_name = get_lang('CertificatesFiles');
  1352. $visibility_command = 'invisible';
  1353. if (!is_dir($base_work_dir_test)) {
  1354. $created_dir = create_unexisting_directory($course_info, api_get_user_id(), api_get_session_id(), $to_group_id, $to_user_id, $base_work_dir, $dir_name, $post_dir_name);
  1355. $update_id = self::get_document_id_of_directory_certificate();
  1356. api_item_property_update($course_info, TOOL_DOCUMENT, $update_id, $visibility_command, api_get_user_id());
  1357. }
  1358. }
  1359. }
  1360. /**
  1361. * Get the document id of the directory certificate
  1362. * @param string The course id
  1363. * @return int The document id of the directory certificate
  1364. */
  1365. static function get_document_id_of_directory_certificate() {
  1366. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1367. $course_id = api_get_course_int_id();
  1368. $sql = "SELECT id FROM $tbl_document WHERE c_id = $course_id AND path='/certificates' ";
  1369. $rs = Database::query($sql);
  1370. $row = Database::fetch_array($rs);
  1371. return $row['id'];
  1372. }
  1373. /**
  1374. * Check if a directory given is for certificate
  1375. * @param string path of directory
  1376. * @return bool true if is a certificate or false otherwise
  1377. */
  1378. static function is_certificate_mode($dir) {
  1379. //I'm in the certification module?
  1380. $is_certificate_mode = false;
  1381. $is_certificate_array = explode('/', $dir);
  1382. array_shift($is_certificate_array);
  1383. if ($is_certificate_array[0] == 'certificates') {
  1384. $is_certificate_mode = true;
  1385. }
  1386. return $is_certificate_mode;
  1387. }
  1388. /**
  1389. * Gets the list of included resources as a list of absolute or relative paths from a html file or string html
  1390. * This allows for a better SCORM export or replace urls inside content html from copy course
  1391. * The list will generally include pictures, flash objects, java applets, or any other
  1392. * stuff included in the source of the current item. The current item is expected
  1393. * to be an HTML file or string html. If it is not, then the function will return and empty list.
  1394. * @param string source html (content or path)
  1395. * @param bool is file or string html
  1396. * @param string type (one of the Dokeos tools) - optional (otherwise takes the current item's type)
  1397. * @param int level of recursivity we're in
  1398. * @return array List of file paths. An additional field containing 'local' or 'remote' helps determine if the file should be copied into the zip or just linked
  1399. */
  1400. static function get_resources_from_source_html($source_html, $is_file = false, $type = null, $recursivity = 1) {
  1401. $max = 5;
  1402. $attributes = array();
  1403. $wanted_attributes = array('src', 'url', '@import', 'href', 'value', 'flashvars');
  1404. $abs_path = '';
  1405. if ($recursivity > $max) {
  1406. return array();
  1407. }
  1408. if (!isset($type)) {
  1409. $type = TOOL_DOCUMENT;
  1410. }
  1411. if (!$is_file) {
  1412. $attributes = self::parse_HTML_attributes($source_html, $wanted_attributes);
  1413. } else {
  1414. if (is_file($source_html)) {
  1415. $abs_path = $source_html;
  1416. //for now, read the whole file in one go (that's gonna be a problem when the file is too big)
  1417. $info = pathinfo($abs_path);
  1418. $ext = $info['extension'];
  1419. switch (strtolower($ext)) {
  1420. case 'html' :
  1421. case 'htm' :
  1422. case 'shtml':
  1423. case 'css' :
  1424. $file_content = file_get_contents($abs_path);
  1425. //get an array of attributes from the HTML source
  1426. $attributes = self::parse_HTML_attributes($file_content, $wanted_attributes);
  1427. break;
  1428. default :
  1429. break;
  1430. }
  1431. } else {
  1432. return false;
  1433. }
  1434. }
  1435. switch ($type) {
  1436. case TOOL_DOCUMENT :
  1437. case TOOL_QUIZ:
  1438. case 'sco':
  1439. foreach ($wanted_attributes as $attr) {
  1440. if (isset($attributes[$attr])) {
  1441. //find which kind of path these are (local or remote)
  1442. $sources = $attributes[$attr];
  1443. foreach ($sources as $source) {
  1444. //skip what is obviously not a resource
  1445. if (strpos($source, '+this.'))
  1446. continue; //javascript code - will still work unaltered
  1447. if (strpos($source, '.') === false)
  1448. continue; //no dot, should not be an external file anyway
  1449. if (strpos($source, 'mailto:'))
  1450. continue; //mailto link
  1451. if (strpos($source, ';') && !strpos($source, '&amp;'))
  1452. continue; //avoid code - that should help
  1453. if ($attr == 'value') {
  1454. if (strpos($source, 'mp3file')) {
  1455. $files_list[] = array(substr($source, 0, strpos($source, '.swf') + 4), 'local', 'abs');
  1456. $mp3file = substr($source, strpos($source, 'mp3file=') + 8);
  1457. if (substr($mp3file, 0, 1) == '/') {
  1458. $files_list[] = array($mp3file, 'local', 'abs');
  1459. } else {
  1460. $files_list[] = array($mp3file, 'local', 'rel');
  1461. }
  1462. } elseif (strpos($source, 'flv=') === 0) {
  1463. $source = substr($source, 4);
  1464. if (strpos($source, '&') > 0) {
  1465. $source = substr($source, 0, strpos($source, '&'));
  1466. }
  1467. if (strpos($source, '://') > 0) {
  1468. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  1469. //we found the current portal url
  1470. $files_list[] = array($source, 'local', 'url');
  1471. } else {
  1472. //we didn't find any trace of current portal
  1473. $files_list[] = array($source, 'remote', 'url');
  1474. }
  1475. } else {
  1476. $files_list[] = array($source, 'local', 'abs');
  1477. }
  1478. continue; //skipping anything else to avoid two entries (while the others can have sub-files in their url, flv's can't)
  1479. }
  1480. }
  1481. if (strpos($source, '://') > 0) {
  1482. //cut at '?' in a URL with params
  1483. if (strpos($source, '?') > 0) {
  1484. $second_part = substr($source, strpos($source, '?'));
  1485. if (strpos($second_part, '://') > 0) {
  1486. //if the second part of the url contains a url too, treat the second one before cutting
  1487. $pos1 = strpos($second_part, '=');
  1488. $pos2 = strpos($second_part, '&');
  1489. $second_part = substr($second_part, $pos1 + 1, $pos2 - ($pos1 + 1));
  1490. if (strpos($second_part, api_get_path(WEB_PATH)) !== false) {
  1491. //we found the current portal url
  1492. $files_list[] = array($second_part, 'local', 'url');
  1493. $in_files_list[] = self::get_resources_from_source_html($second_part, true, TOOL_DOCUMENT, $recursivity + 1);
  1494. if (count($in_files_list) > 0) {
  1495. $files_list = array_merge($files_list, $in_files_list);
  1496. }
  1497. } else {
  1498. //we didn't find any trace of current portal
  1499. $files_list[] = array($second_part, 'remote', 'url');
  1500. }
  1501. } elseif (strpos($second_part, '=') > 0) {
  1502. if (substr($second_part, 0, 1) === '/') {
  1503. //link starts with a /, making it absolute (relative to DocumentRoot)
  1504. $files_list[] = array($second_part, 'local', 'abs');
  1505. $in_files_list[] = self::get_resources_from_source_html($second_part, true, TOOL_DOCUMENT, $recursivity + 1);
  1506. if (count($in_files_list) > 0) {
  1507. $files_list = array_merge($files_list, $in_files_list);
  1508. }
  1509. } elseif (strstr($second_part, '..') === 0) {
  1510. //link is relative but going back in the hierarchy
  1511. $files_list[] = array($second_part, 'local', 'rel');
  1512. //$dir = api_get_path(SYS_CODE_PATH);//dirname($abs_path);
  1513. //$new_abs_path = realpath($dir.'/'.$second_part);
  1514. $dir = '';
  1515. if (!empty($abs_path)) {
  1516. $dir = dirname($abs_path) . '/';
  1517. }
  1518. $new_abs_path = realpath($dir . $second_part);
  1519. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1520. if (count($in_files_list) > 0) {
  1521. $files_list = array_merge($files_list, $in_files_list);
  1522. }
  1523. } else {
  1524. //no starting '/', making it relative to current document's path
  1525. if (substr($second_part, 0, 2) == './') {
  1526. $second_part = substr($second_part, 2);
  1527. }
  1528. $files_list[] = array($second_part, 'local', 'rel');
  1529. $dir = '';
  1530. if (!empty($abs_path)) {
  1531. $dir = dirname($abs_path) . '/';
  1532. }
  1533. $new_abs_path = realpath($dir . $second_part);
  1534. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1535. if (count($in_files_list) > 0) {
  1536. $files_list = array_merge($files_list, $in_files_list);
  1537. }
  1538. }
  1539. }
  1540. //leave that second part behind now
  1541. $source = substr($source, 0, strpos($source, '?'));
  1542. if (strpos($source, '://') > 0) {
  1543. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  1544. //we found the current portal url
  1545. $files_list[] = array($source, 'local', 'url');
  1546. $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
  1547. if (count($in_files_list) > 0) {
  1548. $files_list = array_merge($files_list, $in_files_list);
  1549. }
  1550. } else {
  1551. //we didn't find any trace of current portal
  1552. $files_list[] = array($source, 'remote', 'url');
  1553. }
  1554. } else {
  1555. //no protocol found, make link local
  1556. if (substr($source, 0, 1) === '/') {
  1557. //link starts with a /, making it absolute (relative to DocumentRoot)
  1558. $files_list[] = array($source, 'local', 'abs');
  1559. $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
  1560. if (count($in_files_list) > 0) {
  1561. $files_list = array_merge($files_list, $in_files_list);
  1562. }
  1563. } elseif (strstr($source, '..') === 0) { //link is relative but going back in the hierarchy
  1564. $files_list[] = array($source, 'local', 'rel');
  1565. $dir = '';
  1566. if (!empty($abs_path)) {
  1567. $dir = dirname($abs_path) . '/';
  1568. }
  1569. $new_abs_path = realpath($dir . $source);
  1570. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1571. if (count($in_files_list) > 0) {
  1572. $files_list = array_merge($files_list, $in_files_list);
  1573. }
  1574. } else {
  1575. //no starting '/', making it relative to current document's path
  1576. if (substr($source, 0, 2) == './') {
  1577. $source = substr($source, 2);
  1578. }
  1579. $files_list[] = array($source, 'local', 'rel');
  1580. $dir = '';
  1581. if (!empty($abs_path)) {
  1582. $dir = dirname($abs_path) . '/';
  1583. }
  1584. $new_abs_path = realpath($dir . $source);
  1585. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1586. if (count($in_files_list) > 0) {
  1587. $files_list = array_merge($files_list, $in_files_list);
  1588. }
  1589. }
  1590. }
  1591. }
  1592. //found some protocol there
  1593. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  1594. //we found the current portal url
  1595. $files_list[] = array($source, 'local', 'url');
  1596. $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
  1597. if (count($in_files_list) > 0) {
  1598. $files_list = array_merge($files_list, $in_files_list);
  1599. }
  1600. } else {
  1601. //we didn't find any trace of current portal
  1602. $files_list[] = array($source, 'remote', 'url');
  1603. }
  1604. } else {
  1605. //no protocol found, make link local
  1606. if (substr($source, 0, 1) === '/') {
  1607. //link starts with a /, making it absolute (relative to DocumentRoot)
  1608. $files_list[] = array($source, 'local', 'abs');
  1609. $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
  1610. if (count($in_files_list) > 0) {
  1611. $files_list = array_merge($files_list, $in_files_list);
  1612. }
  1613. } elseif (strpos($source, '..') === 0) {
  1614. //link is relative but going back in the hierarchy
  1615. $files_list[] = array($source, 'local', 'rel');
  1616. $dir = '';
  1617. if (!empty($abs_path)) {
  1618. $dir = dirname($abs_path) . '/';
  1619. }
  1620. $new_abs_path = realpath($dir . $source);
  1621. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1622. if (count($in_files_list) > 0) {
  1623. $files_list = array_merge($files_list, $in_files_list);
  1624. }
  1625. } else {
  1626. //no starting '/', making it relative to current document's path
  1627. if (substr($source, 0, 2) == './') {
  1628. $source = substr($source, 2);
  1629. }
  1630. $files_list[] = array($source, 'local', 'rel');
  1631. $dir = '';
  1632. if (!empty($abs_path)) {
  1633. $dir = dirname($abs_path) . '/';
  1634. }
  1635. $new_abs_path = realpath($dir . $source);
  1636. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1637. if (count($in_files_list) > 0) {
  1638. $files_list = array_merge($files_list, $in_files_list);
  1639. }
  1640. }
  1641. }
  1642. }
  1643. }
  1644. }
  1645. break;
  1646. default: //ignore
  1647. break;
  1648. }
  1649. $checked_files_list = array();
  1650. $checked_array_list = array();
  1651. if (count($files_list) > 0) {
  1652. foreach ($files_list as $idx => $file) {
  1653. if (!empty($file[0])) {
  1654. if (!in_array($file[0], $checked_files_list)) {
  1655. $checked_files_list[] = $files_list[$idx][0];
  1656. $checked_array_list[] = $files_list[$idx];
  1657. }
  1658. }
  1659. }
  1660. }
  1661. return $checked_array_list;
  1662. }
  1663. /**
  1664. * Parses the HTML attributes given as string.
  1665. *
  1666. * @param string HTML attribute string
  1667. * @param array List of attributes that we want to get back
  1668. * @return array An associative array of attributes
  1669. * @author Based on a function from the HTML_Common2 PEAR module
  1670. */
  1671. static function parse_HTML_attributes($attrString, $wanted = array()) {
  1672. $attributes = array();
  1673. $regs = array();
  1674. $reduced = false;
  1675. if (count($wanted) > 0) {
  1676. $reduced = true;
  1677. }
  1678. try {
  1679. //Find all occurences of something that looks like a URL
  1680. // The structure of this regexp is:
  1681. // (find protocol) then
  1682. // (optionally find some kind of space 1 or more times) then
  1683. // find (either an equal sign or a bracket) followed by an optional space
  1684. // followed by some text without quotes (between quotes itself or not)
  1685. // then possible closing brackets if we were in the opening bracket case
  1686. // OR something like @import()
  1687. $res = preg_match_all(
  1688. '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]*))' .
  1689. // '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]|[^\x00-\x7F])*)' . -> seems to be taking too much
  1690. // '/(((([A-Za-z_:])([^\x00-\x7F])*)' . -> takes only last letter of parameter name
  1691. '([ \n\t\r]+)?(' .
  1692. // '(=([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+))' . -> doesn't restrict close enough to the url itself
  1693. '(=([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+))' .
  1694. '|' .
  1695. // '(\(([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)\))' . -> doesn't restrict close enough to the url itself
  1696. '(\(([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+)\))' .
  1697. '))' .
  1698. '|' .
  1699. // '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))?/', -> takes a lot (like 100's of thousands of empty possibilities)
  1700. '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/', $attrString, $regs
  1701. );
  1702. } catch (Exception $e) {
  1703. error_log('Caught exception: ' . $e->getMessage(), 0);
  1704. }
  1705. if ($res) {
  1706. for ($i = 0; $i < count($regs[1]); $i++) {
  1707. $name = trim($regs[3][$i]);
  1708. $check = trim($regs[0][$i]);
  1709. $value = trim($regs[10][$i]);
  1710. if (empty($value) and !empty($regs[13][$i])) {
  1711. $value = $regs[13][$i];
  1712. }
  1713. if (empty($name) && !empty($regs[16][$i])) {
  1714. $name = '@import';
  1715. $value = trim($regs[16][$i]);
  1716. }
  1717. if (!empty($name)) {
  1718. if (!$reduced OR in_array(strtolower($name), $wanted)) {
  1719. if ($name == $check) {
  1720. $attributes[strtolower($name)][] = strtolower($name);
  1721. } else {
  1722. if (!empty($value) && ($value[0] == '\'' || $value[0] == '"')) {
  1723. $value = substr($value, 1, -1);
  1724. }
  1725. if ($value == 'API.LMSGetValue(name') {
  1726. $value = 'API.LMSGetValue(name)';
  1727. }
  1728. $attributes[strtolower($name)][] = $value;
  1729. }
  1730. }
  1731. }
  1732. }
  1733. } else {
  1734. error_log('preg_match did not find anything', 0);
  1735. }
  1736. return $attributes;
  1737. }
  1738. /**
  1739. * Replace urls inside content html from a copy course
  1740. * @param string content html
  1741. * @param string origin course code
  1742. * @param string destination course directory
  1743. * @return string new content html with replaced urls or return false if content is not a string
  1744. */
  1745. static function replace_urls_inside_content_html_from_copy_course($content_html, $origin_course_code, $destination_course_directory) {
  1746. require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
  1747. if (!is_string($content_html)) {
  1748. return false;
  1749. }
  1750. $orig_source_html = DocumentManager::get_resources_from_source_html($content_html);
  1751. $orig_course_info = api_get_course_info($origin_course_code);
  1752. $orig_course_path = api_get_path(SYS_PATH) . 'courses/' . $orig_course_info['path'] . '/';
  1753. $destination_course_code = CourseManager::get_course_id_from_path($destination_course_directory);
  1754. $destination_course_info = api_get_course_info($destination_course_code);
  1755. $dest_course_path = api_get_path(SYS_COURSE_PATH) . $destination_course_directory . '/';
  1756. $user_id = api_get_user_id();
  1757. if (!empty($orig_source_html)) {
  1758. foreach ($orig_source_html as $source) {
  1759. // get information about source url
  1760. $real_orig_url = $source[0]; // url
  1761. $scope_url = $source[1]; // scope (local, remote)
  1762. $type_url = $source[2]; // tyle (rel, abs, url)
  1763. // Get path and query from origin url
  1764. $orig_parse_url = parse_url($real_orig_url);
  1765. $real_orig_path = $orig_parse_url['path'];
  1766. $real_orig_query = $orig_parse_url['query'];
  1767. // Replace origin course code by destination course code from origin url query
  1768. $dest_url_query = '';
  1769. if (!empty($real_orig_query)) {
  1770. $dest_url_query = '?' . $real_orig_query;
  1771. if (strpos($dest_url_query, $origin_course_code) !== false) {
  1772. $dest_url_query = str_replace($origin_course_code, $destination_course_code, $dest_url_query);
  1773. }
  1774. }
  1775. if ($scope_url == 'local') {
  1776. if ($type_url == 'abs' || $type_url == 'rel') {
  1777. $document_file = strstr($real_orig_path, 'document');
  1778. if (strpos($real_orig_path, $document_file) !== false) {
  1779. $origin_filepath = $orig_course_path . $document_file;
  1780. $destination_filepath = $dest_course_path . $document_file;
  1781. // copy origin file inside destination course
  1782. if (file_exists($origin_filepath)) {
  1783. $filepath_dir = dirname($destination_filepath);
  1784. if (!is_dir($filepath_dir)) {
  1785. $perm = api_get_permissions_for_new_directories();
  1786. $result = @mkdir($filepath_dir, $perm, true);
  1787. if ($result) {
  1788. $filepath_to_add = str_replace(array($dest_course_path, 'document'), '', $filepath_dir);
  1789. //Add to item properties to the new folder
  1790. $doc_id = add_document($destination_course_info, $filepath_to_add, 'folder', 0, basename($filepath_to_add));
  1791. api_item_property_update($destination_course_info, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $user_id, null, null, null, null);
  1792. }
  1793. }
  1794. if (!file_exists($destination_filepath)) {
  1795. $result = @copy($origin_filepath, $destination_filepath);
  1796. if ($result) {
  1797. $filepath_to_add = str_replace(array($dest_course_path, 'document'), '', $destination_filepath);
  1798. $size = filesize($destination_filepath);
  1799. //Add to item properties to the file
  1800. $doc_id = add_document($destination_course_info, $filepath_to_add, 'file', $size, basename($filepath_to_add));
  1801. api_item_property_update($destination_course_info, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $user_id, null, null, null, null);
  1802. }
  1803. }
  1804. }
  1805. // Replace origin course path by destination course path
  1806. if (strpos($content_html, $real_orig_url) !== false) {
  1807. //$origin_course_code
  1808. $url_course_path = str_replace($orig_course_info['path'] . '/' . $document_file, '', $real_orig_path);
  1809. $destination_url = $url_course_path . $destination_course_directory . '/' . $document_file . $dest_url_query;
  1810. //If the course code doesn't exist in the path? what we do? Nothing! see BT#1985
  1811. if (strpos($real_orig_path, $origin_course_code) === false) {
  1812. $url_course_path = $real_orig_path;
  1813. $destination_url = $real_orig_path;
  1814. }
  1815. $content_html = str_replace($real_orig_url, $destination_url, $content_html);
  1816. }
  1817. }
  1818. // replace origin course code by destination course code from origin url
  1819. if (strpos($real_orig_url, '?') === 0) {
  1820. $dest_url = str_replace($origin_course_code, $destination_course_code, $real_orig_url);
  1821. $content_html = str_replace($real_orig_url, $dest_url, $content_html);
  1822. }
  1823. } else {
  1824. if ($type_url == 'url') {
  1825. }
  1826. }
  1827. }
  1828. }
  1829. }
  1830. return $content_html;
  1831. }
  1832. /**
  1833. * Replace urls inside content html when moving a file
  1834. * @todo this code is only called in document.php but is commented
  1835. * @param string content html
  1836. * @param string origin
  1837. * @param string destination
  1838. * @return string new content html with replaced urls or return false if content is not a string
  1839. */
  1840. function replace_urls_inside_content_html_when_moving_file($file_name, $original_path, $destiny_path) {
  1841. if (substr($original_path, strlen($original_path) - 1, strlen($original_path)) == '/') {
  1842. $original = $original_path . $file_name;
  1843. } else {
  1844. $original = $original_path . '/' . $file_name;
  1845. }
  1846. if (substr($destiny_path, strlen($destiny_path) - 1, strlen($destiny_path)) == '/') {
  1847. $destination = $destiny_path . $file_name;
  1848. } else {
  1849. $destination = $destiny_path . '/' . $file_name;
  1850. }
  1851. //var_dump("From $original ", "to $destination");
  1852. $original_count = count(explode('/', $original));
  1853. $destination_count = count(explode('/', $destination));
  1854. if ($original_count == $destination_count) {
  1855. //Nothing to change
  1856. return true;
  1857. }
  1858. $mode = '';
  1859. if ($original_count > $destination_count) {
  1860. $mode = 'outside';
  1861. } else {
  1862. $mode = 'inside';
  1863. }
  1864. //echo $original_count.' '.$destination_count; var_dump($mode);
  1865. //We do not select the $original_path becayse the file was already moved
  1866. $content_html = file_get_contents($destiny_path . '/' . $file_name);
  1867. $destination_file = $destiny_path . '/' . $file_name;
  1868. $pre_original = strstr($original_path, 'document');
  1869. $pre_destin = strstr($destiny_path, 'document');
  1870. //var_dump ("pre_original $pre_original");
  1871. //var_dump ("pre_destin $pre_destin");
  1872. $pre_original = substr($pre_original, 8, strlen($pre_original));
  1873. $pre_destin = substr($pre_destin, 8, strlen($pre_destin));
  1874. //var_dump ("pre_original $pre_original");
  1875. //var_dump ("pre_destin $pre_destin");
  1876. $levels = count(explode('/', $pre_destin)) - 1;
  1877. $link_to_add = '';
  1878. for ($i = 1; $i <= $levels; $i++) {
  1879. $link_to_add .= '../';
  1880. }
  1881. if ($pre_original == '/') {
  1882. $pre_original = '';
  1883. }
  1884. if ($pre_destin == '/') {
  1885. $pre_destin = '';
  1886. }
  1887. if ($pre_original != '') {
  1888. $pre_original = '..' . $pre_original . '/';
  1889. }
  1890. if ($pre_destin != '') {
  1891. $pre_destin = '..' . $pre_destin . '/';
  1892. }
  1893. //var_dump($pre_original);
  1894. $levels = explode('/', $pre_original);
  1895. //var_dump($levels);
  1896. $count_pre_destination_levels = 0;
  1897. foreach ($levels as $item) {
  1898. if (!empty($item) && $item != '..') {
  1899. $count_pre_destination_levels++;
  1900. }
  1901. }
  1902. $count_pre_destination_levels--;
  1903. //$count_pre_destination_levels = count() - 3;
  1904. if ($count_pre_destination_levels == 0) {
  1905. $count_pre_destination_levels = 1;
  1906. }
  1907. //echo '$count_pre_destination_levels '. $count_pre_destination_levels;
  1908. $pre_remove = '';
  1909. for ($i = 1; $i <= $count_pre_destination_levels; $i++) {
  1910. $pre_remove .='..\/';
  1911. }
  1912. //var_dump(' link to add '.$link_to_add.' -- remove '.$pre_remove);
  1913. $orig_source_html = DocumentManager::get_resources_from_source_html($content_html);
  1914. //var_dump($orig_source_html);
  1915. foreach ($orig_source_html as $source) {
  1916. // get information about source url
  1917. $real_orig_url = $source[0]; // url
  1918. $scope_url = $source[1]; // scope (local, remote)
  1919. $type_url = $source[2]; // tyle (rel, abs, url)
  1920. // Get path and query from origin url
  1921. $orig_parse_url = parse_url($real_orig_url);
  1922. $real_orig_path = $orig_parse_url['path'];
  1923. $real_orig_query = $orig_parse_url['query'];
  1924. // Replace origin course code by destination course code from origin url query
  1925. /*
  1926. $dest_url_query = '';
  1927. if (!empty($real_orig_query)) {
  1928. $dest_url_query = '?'.$real_orig_query;
  1929. if (strpos($dest_url_query,$origin_course_code) !== false) {
  1930. $dest_url_query = str_replace($origin_course_code, $destination_course_code, $dest_url_query);
  1931. }
  1932. } */
  1933. if ($scope_url == 'local') {
  1934. if ($type_url == 'abs' || $type_url == 'rel') {
  1935. $document_file = strstr($real_orig_path, 'document');
  1936. if (strpos($real_orig_path, $document_file) !== false) {
  1937. echo 'continue1';
  1938. continue;
  1939. } else {
  1940. $real_orig_url_temp = '';
  1941. if ($mode == 'inside') {
  1942. $real_orig_url_temp = str_replace('../', '', $real_orig_url);
  1943. $destination_url = $link_to_add . $real_orig_url_temp;
  1944. } else {
  1945. $real_orig_url_temp = $real_orig_url;
  1946. $destination_url = preg_replace("/" . $pre_remove . "/", '', $real_orig_url, 1);
  1947. }
  1948. if ($real_orig_url == $destination_url) {
  1949. echo 'continue2';
  1950. continue;
  1951. }
  1952. var_dump($real_orig_url_temp . ' - ' . $destination_url);
  1953. $content_html = str_replace($real_orig_url, $destination_url, $content_html);
  1954. }
  1955. } else {
  1956. echo 'continue3';
  1957. continue;
  1958. }
  1959. }
  1960. }
  1961. $return = file_put_contents($destination, $content_html);
  1962. return $return;
  1963. }
  1964. public static function export_to_pdf($document_id, $course_code) {
  1965. require_once api_get_path(LIBRARY_PATH) . 'pdf.lib.php';
  1966. $course_data = api_get_course_info($course_code);
  1967. $document_data = self::get_document_data_by_id($document_id, $course_code);
  1968. $file_path = api_get_path(SYS_COURSE_PATH) . $course_data['path'] . '/document' . $document_data['path'];
  1969. $pdf = new PDF();
  1970. $pdf->html_to_pdf($file_path, $document_data['title'], $course_code);
  1971. }
  1972. /**
  1973. * Uploads a document
  1974. *
  1975. * @param array the $_FILES variable
  1976. * @param string $path
  1977. * @param string title
  1978. * @param string comment
  1979. * @param int unzip or not the file
  1980. * @param int if_exists overwrite, rename or warn if exists (default)
  1981. * @param bool index document (search xapian module)
  1982. * @param bool print html messages
  1983. * @return unknown_type
  1984. */
  1985. public static function upload_document($files, $path, $title = null, $comment = null, $unzip = 0, $if_exists = null, $index_document = false, $show_output = false) {
  1986. require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
  1987. $course_info = api_get_course_info();
  1988. $course_dir = $course_info['path'] . '/document';
  1989. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1990. $base_work_dir = $sys_course_path . $course_dir;
  1991. if (isset($files['file'])) {
  1992. $upload_ok = process_uploaded_file($files['file'], $show_output);
  1993. if ($upload_ok) {
  1994. // File got on the server without problems, now process it
  1995. $new_path = handle_uploaded_document($course_info, $files['file'], $base_work_dir, $path, api_get_user_id(), api_get_group_id(), null, $unzip, $if_exists, $show_output);
  1996. if ($new_path) {
  1997. $docid = DocumentManager::get_document_id($course_info, $new_path);
  1998. if (!empty($docid)) {
  1999. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  2000. $params = array();
  2001. if (!empty($title)) {
  2002. $params['title'] = get_document_title($title);
  2003. } else {
  2004. if (isset($if_exists) && $if_exists == 'rename') {
  2005. $new_path = basename($new_path);
  2006. $params['title'] = get_document_title($new_path);
  2007. } else {
  2008. $params['title'] = get_document_title($files['file']['name']);
  2009. }
  2010. }
  2011. if (!empty($comment)) {
  2012. $params['comment'] = trim($comment);
  2013. }
  2014. Database::update($table_document, $params, array('id = ? AND c_id = ? ' => array($docid, $course_info['real_id'])));
  2015. }
  2016. // Showing message when sending zip files
  2017. if ($new_path === true && $unzip == 1 && $show_output) {
  2018. Display::display_confirmation_message(get_lang('UplUploadSucceeded') . '<br />', false);
  2019. }
  2020. /* // Check for missing images in html files
  2021. $missing_files = check_for_missing_files($base_work_dir.$new_path);
  2022. if ($missing_files && $show_output) {
  2023. // Show a form to upload the missing files
  2024. Display::display_normal_message(build_missing_files_form($missing_files, $path, $files['file']['name']), false);
  2025. } */
  2026. if ($index_document) {
  2027. self::index_document($docid, $course_info['code'], null, $_POST['language'], $_REQUEST, $if_exists);
  2028. }
  2029. if (!empty($docid) && is_numeric($docid)) {
  2030. $document_data = self::get_document_data_by_id($docid, $course_info['code']);
  2031. return $document_data;
  2032. }
  2033. }
  2034. }
  2035. }
  2036. return false;
  2037. }
  2038. /**
  2039. * Obtains the text inside the file with the right parser
  2040. */
  2041. function get_text_content($doc_path, $doc_mime) {
  2042. // TODO: review w$ compatibility
  2043. // Use usual exec output lines array to store stdout instead of a temp file
  2044. // because we need to store it at RAM anyway before index on ChamiloIndexer object
  2045. $ret_val = null;
  2046. switch ($doc_mime) {
  2047. case 'text/plain':
  2048. $handle = fopen($doc_path, 'r');
  2049. $output = array(fread($handle, filesize($doc_path)));
  2050. fclose($handle);
  2051. break;
  2052. case 'application/pdf':
  2053. exec("pdftotext $doc_path -", $output, $ret_val);
  2054. break;
  2055. case 'application/postscript':
  2056. $temp_file = tempnam(sys_get_temp_dir(), 'chamilo');
  2057. exec("ps2pdf $doc_path $temp_file", $output, $ret_val);
  2058. if ($ret_val !== 0) { // shell fail, probably 127 (command not found)
  2059. return false;
  2060. }
  2061. exec("pdftotext $temp_file -", $output, $ret_val);
  2062. unlink($temp_file);
  2063. break;
  2064. case 'application/msword':
  2065. exec("catdoc $doc_path", $output, $ret_val);
  2066. break;
  2067. case 'text/html':
  2068. exec("html2text $doc_path", $output, $ret_val);
  2069. break;
  2070. case 'text/rtf':
  2071. // Note: correct handling of code pages in unrtf
  2072. // on debian lenny unrtf v0.19.2 can not, but unrtf v0.20.5 can
  2073. exec("unrtf --text $doc_path", $output, $ret_val);
  2074. if ($ret_val == 127) { // command not found
  2075. return false;
  2076. }
  2077. // Avoid index unrtf comments
  2078. if (is_array($output) && count($output) > 1) {
  2079. $parsed_output = array();
  2080. foreach ($output as & $line) {
  2081. if (!preg_match('/^###/', $line, $matches)) {
  2082. if (!empty($line)) {
  2083. $parsed_output[] = $line;
  2084. }
  2085. }
  2086. }
  2087. $output = $parsed_output;
  2088. }
  2089. break;
  2090. case 'application/vnd.ms-powerpoint':
  2091. exec("catppt $doc_path", $output, $ret_val);
  2092. break;
  2093. case 'application/vnd.ms-excel':
  2094. exec("xls2csv -c\" \" $doc_path", $output, $ret_val);
  2095. break;
  2096. }
  2097. $content = '';
  2098. if (!is_null($ret_val)) {
  2099. if ($ret_val !== 0) { // shell fail, probably 127 (command not found)
  2100. return false;
  2101. }
  2102. }
  2103. if (isset($output)) {
  2104. foreach ($output as & $line) {
  2105. $content .= $line . "\n";
  2106. }
  2107. return $content;
  2108. } else {
  2109. return false;
  2110. }
  2111. }
  2112. /**
  2113. * Calculates the total size of all documents in a course
  2114. *
  2115. * @author Bert vanderkimpen
  2116. * @param int $to_group_id (to calculate group document space)
  2117. * @return int total size
  2118. */
  2119. static function documents_total_space($course_id = null, $group_id = null, $session_id = null) {
  2120. $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2121. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  2122. if (isset($course_id)) {
  2123. $course_id = intval($course_id);
  2124. } else {
  2125. $course_id = api_get_course_int_id();
  2126. }
  2127. $group_condition = null;
  2128. if (isset($group_id)) {
  2129. $group_id = intval($group_id);
  2130. $group_condition = " AND props.to_group_id='" . $group_id . "' ";
  2131. }
  2132. $session_condition = null;
  2133. if (isset($session_id)) {
  2134. $session_id = intval($session_id);
  2135. $session_condition = " AND props.id_session='" . $session_id . "' ";
  2136. }
  2137. $sql = "SELECT SUM(size) FROM " . $TABLE_ITEMPROPERTY . " AS props, " . $TABLE_DOCUMENT . " AS docs
  2138. WHERE props.c_id = $course_id AND
  2139. docs.c_id = $course_id AND
  2140. docs.id = props.ref AND
  2141. props.tool = '" . TOOL_DOCUMENT . "' AND
  2142. props.visibility <> 2
  2143. $group_condition
  2144. $session_condition
  2145. ";
  2146. $result = Database::query($sql);
  2147. if ($result && Database::num_rows($result) != 0) {
  2148. $row = Database::fetch_row($result);
  2149. return $row[0];
  2150. } else {
  2151. return 0;
  2152. }
  2153. }
  2154. /**
  2155. * Here we count 1 kilobyte = 1000 byte, 12 megabyte = 1000 kilobyte.
  2156. */
  2157. static function display_quota($course_quota, $already_consumed_space) {
  2158. $course_quota_m = round($course_quota / 1000000);
  2159. $already_consumed_space_m = round($already_consumed_space / 1000000);
  2160. $message = get_lang('MaximumAllowedQuota') . ' <strong>' . $course_quota_m . ' megabyte</strong>.<br />';
  2161. $message .= get_lang('CourseCurrentlyUses') . ' <strong>' . $already_consumed_space_m . ' megabyte</strong>.<br />';
  2162. $percentage = round(($already_consumed_space / $course_quota * 100), 1);
  2163. $other_percentage = $percentage < 100 ? 100 - $percentage : 0;
  2164. // Decide where to place percentage in graph
  2165. if ($percentage >= 50) {
  2166. $text_in_filled = '&nbsp;' . $other_percentage . '%';
  2167. $text_in_unfilled = '';
  2168. } else {
  2169. $text_in_unfilled = '&nbsp;' . $other_percentage . '%';
  2170. $text_in_filled = '';
  2171. }
  2172. // Decide the background colour of the graph
  2173. if ($percentage < 65) {
  2174. $colour = '#00BB00'; // Safe - green
  2175. } elseif ($percentage < 90) {
  2176. $colour = '#ffd400'; // Filling up - yelloworange
  2177. } else {
  2178. $colour = '#DD0000'; // Full - red
  2179. }
  2180. // This is used for the table width: a table of only 100 pixels looks too small
  2181. $visual_percentage = 4 * $percentage;
  2182. $visual_other_percentage = 4 * $other_percentage;
  2183. $message .= get_lang('PercentageQuotaInUse') . ': <strong>' . $percentage . '%</strong>.<br />' .
  2184. get_lang('PercentageQuotaFree') . ': <strong>' . $other_percentage . '%</strong>.<br />';
  2185. $show_percentage = '&nbsp;' . $percentage . '%';
  2186. $message .= '<div style="width: 80%; text-align: center; -moz-border-radius: 5px 5px 5px 5px; border: 1px solid #aaa; background-image: url(\'' . api_get_path(WEB_CODE_PATH) . 'css/' . api_get_visual_theme() . '/images/bg-header4.png\');" class="document-quota-bar">' .
  2187. '<div style="width:' . $percentage . '%; background-color: #bbb; border-right:3px groove #bbb; -moz-border-radius:5px;">&nbsp;</div>' .
  2188. '<span style="margin-top: -15px; margin-left:-15px; position: absolute;font-weight:bold;">' . $show_percentage . '</span></div>';
  2189. echo $message;
  2190. }
  2191. /**
  2192. * Display the document quota in a simple way
  2193. *
  2194. * Here we count 1 kilobyte = 1000 byte, 12 megabyte = 1000 kilobyte.
  2195. */
  2196. static function display_simple_quota($course_quota, $already_consumed_space) {
  2197. $course_quota_m = round($course_quota / 1000000);
  2198. $already_consumed_space_m = round($already_consumed_space / 1000000, 2);
  2199. $percentage = $already_consumed_space / $course_quota * 100;
  2200. $percentage = round($percentage, 1);
  2201. $message = get_lang('YouAreCurrentlyUsingXOfYourX');
  2202. $message = sprintf($message, $already_consumed_space_m, $percentage . '%', $course_quota_m . ' ');
  2203. echo Display::div($message, array('id' => 'document_quota'));
  2204. }
  2205. /**
  2206. * Checks if there is enough place to add a file on a directory
  2207. * on the base of a maximum directory size allowed
  2208. *
  2209. * @author Bert Vanderkimpen
  2210. * @param int file_size size of the file in byte
  2211. * @param array $_course
  2212. * @param int max_dir_space maximum size
  2213. * @return boolean true if there is enough space, false otherwise
  2214. *
  2215. * @see enough_space() uses documents_total_space() function
  2216. */
  2217. static function enough_space($file_size, $max_dir_space) {
  2218. if ($max_dir_space) {
  2219. $already_filled_space = self::documents_total_space();
  2220. if (($file_size + $already_filled_space) > $max_dir_space) {
  2221. return false;
  2222. }
  2223. }
  2224. return true;
  2225. }
  2226. /**
  2227. *
  2228. * @param array paremeters: count, url, extension
  2229. * @return string
  2230. */
  2231. static function generate_jplayer_jquery($params = array()) {
  2232. $js_path = api_get_path(WEB_LIBRARY_PATH) . 'javascript/';
  2233. $jplayer_definition = ' $("#jquery_jplayer_' . $params['count'] . '").jPlayer({
  2234. ready: function() {
  2235. $(this).jPlayer("setMedia", {
  2236. ' . $params['extension'] . ' : "' . $params['url'] . '"
  2237. });
  2238. },
  2239. play: function() { // To avoid both jPlayers playing together.
  2240. $(this).jPlayer("pauseOthers");
  2241. },
  2242. //errorAlerts: true,
  2243. //warningAlerts: true,
  2244. swfPath: "' . $js_path . 'jquery-jplayer",
  2245. //supplied: "m4a, oga, mp3, ogg, wav",
  2246. supplied: "' . $params['extension'] . '",
  2247. wmode: "window",
  2248. solution: "flash, html", // Do not change this setting
  2249. cssSelectorAncestor: "#jp_container_' . $params['count'] . '",
  2250. }); ' . "\n\n";
  2251. return $jplayer_definition;
  2252. }
  2253. /**
  2254. *
  2255. * Shows a play icon next to the document title in the document list
  2256. * @param int
  2257. * @return string html content
  2258. */
  2259. static function generate_media_preview($i, $type = 'simple') {
  2260. $i = intval($i);
  2261. $extra_controls = $progress = '';
  2262. if ($type == 'advanced') {
  2263. $extra_controls = ' <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
  2264. <li><a href="#" class="jp-mute" tabindex="1">mute</a></li>
  2265. <li><a href="#" class="jp-unmute" tabindex="1">unmute</a></li>';
  2266. $progress = '<div class="jp-progress">
  2267. <div class="jp-seek-bar">
  2268. <div class="jp-play-bar"></div>
  2269. </div>
  2270. </div>';
  2271. }
  2272. //Shows only the play button
  2273. $html = '<div id="jquery_jplayer_' . $i . '" class="jp-jplayer"></div>
  2274. <div id="jp_container_' . $i . '" class="jp-audio">
  2275. <div class="jp-type-single">
  2276. <div class="jp-gui jp-interface">
  2277. <ul class="jp-controls">
  2278. <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
  2279. <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
  2280. ' . $extra_controls . '
  2281. </ul>
  2282. ' . $progress . '
  2283. </div>
  2284. </div>
  2285. </div>';
  2286. //<div id="jplayer_inspector_'.$i.'"></div>
  2287. return $html;
  2288. }
  2289. static function generate_video_preview($document_data = array()) {
  2290. $html = '
  2291. <div id="jp_container_1" class="jp-video">
  2292. <div class="jp-type-single">
  2293. <div id="jquery_jplayer_1" class="jp-jplayer"></div>
  2294. <div class="jp-gui">
  2295. <div class="jp-video-play">
  2296. <a href="javascript:;" class="jp-video-play-icon" tabindex="1">play</a>
  2297. </div>
  2298. <div class="jp-interface">
  2299. <div class="jp-progress">
  2300. <div class="jp-seek-bar">
  2301. <div class="jp-play-bar"></div>
  2302. </div>
  2303. </div>
  2304. <div class="jp-current-time"></div>
  2305. <div class="jp-controls-holder">
  2306. <ul class="jp-controls">
  2307. <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
  2308. <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
  2309. <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
  2310. <li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>
  2311. <li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>
  2312. <li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>
  2313. </ul>
  2314. <div class="jp-volume-bar">
  2315. <div class="jp-volume-bar-value"></div>
  2316. </div>
  2317. <ul class="jp-toggles">
  2318. <li><a href="javascript:;" class="jp-full-screen" tabindex="1" title="full screen">full screen</a></li>
  2319. <li><a href="javascript:;" class="jp-restore-screen" tabindex="1" title="restore screen">restore screen</a></li>
  2320. <li><a href="javascript:;" class="jp-repeat" tabindex="1" title="repeat">repeat</a></li>
  2321. <li><a href="javascript:;" class="jp-repeat-off" tabindex="1" title="repeat off">repeat off</a></li>
  2322. </ul>
  2323. </div>
  2324. <div class="jp-title">
  2325. <ul>
  2326. <li>' . $document_data['title'] . '</li>
  2327. </ul>
  2328. </div>
  2329. </div>
  2330. </div>
  2331. <div class="jp-no-solution">
  2332. <span>' . get_lang('UpdateRequire') . '</span>
  2333. ' . get_lang("ToPlayTheMediaYouWillNeedToUpdateYourBrowserToARecentVersionYouCanAlsoDownloadTheFile") . '
  2334. </div>
  2335. </div>
  2336. </div>';
  2337. return $html;
  2338. }
  2339. static function get_document_preview($course_info, $lp_id = false, $target = '', $session_id = 0, $add_move_button = false, $filter_by_folder = null, $overwrite_url = null) {
  2340. if (empty($course_info['real_id']) || empty($course_info['code']) || !is_array($course_info)) {
  2341. return '';
  2342. }
  2343. $user_id = api_get_user_id();
  2344. $user_in_course = false;
  2345. if (api_is_platform_admin()) {
  2346. $user_in_course = true;
  2347. }
  2348. if (!$user_in_course) {
  2349. if (CourseManager::is_course_teacher($user_id, $course_info['code'])) {
  2350. $user_in_course = true;
  2351. }
  2352. }
  2353. //condition for the session
  2354. $session_id = intval($session_id);
  2355. if (!$user_in_course) {
  2356. if (empty($session_id)) {
  2357. if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code'])) {
  2358. $user_in_course = true;
  2359. }
  2360. //Check if course is open then we can consider that the student is regitered to the course
  2361. if (isset($course_info) && in_array($course_info['visibility'], array(2, 3))) {
  2362. $user_in_course = true;
  2363. }
  2364. } else {
  2365. $user_status = SessionManager::get_user_status_in_course_session($user_id, $course_info['code'], $session_id);
  2366. //is true if is an student, course session teacher or coach
  2367. if (in_array($user_status, array('0', '2', '6'))) {
  2368. $user_in_course = true;
  2369. }
  2370. }
  2371. }
  2372. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  2373. $tbl_item_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2374. $path = '/';
  2375. $path = Database::escape_string(str_replace('_', '\_', $path));
  2376. $added_slash = ($path == '/') ? '' : '/';
  2377. //$condition_session = " AND (id_session = '$session_id' OR (id_session = '0' AND insert_date <= (SELECT creation_date FROM $tbl_course WHERE code = '".$course_info['code']."' )))";
  2378. $condition_session = " AND (id_session = '$session_id' OR id_session = '0' )";
  2379. $add_folder_filter = null;
  2380. if (!empty($filter_by_folder)) {
  2381. $add_folder_filter = " AND docs.path LIKE '" . Database::escape_string($filter_by_folder) . "%'";
  2382. }
  2383. $sql_doc = "SELECT last.visibility, docs.*
  2384. FROM $tbl_item_prop AS last, $tbl_doc AS docs
  2385. WHERE docs.id = last.ref AND
  2386. docs.path LIKE '" . $path . $added_slash . "%' AND
  2387. docs.path NOT LIKE '%_DELETED_%' AND
  2388. last.tool = '" . TOOL_DOCUMENT . "' $condition_session AND
  2389. last.visibility = '1' AND
  2390. docs.c_id = {$course_info['real_id']} AND
  2391. last.c_id = {$course_info['real_id']}
  2392. $add_folder_filter
  2393. ORDER BY docs.title ASC";
  2394. $res_doc = Database::query($sql_doc);
  2395. $resources = Database::store_result($res_doc, 'ASSOC');
  2396. $resources_sorted = array();
  2397. $return = '';
  2398. if ($lp_id) {
  2399. $return .= '<div class="lp_resource_element">';
  2400. $return .= Display::return_icon('new_doc.gif', '', array(), ICON_SIZE_SMALL);
  2401. $return .= Display::url(get_lang('NewDocument'), api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_DOCUMENT . '&lp_id=' . $_SESSION['oLP']->lp_id);
  2402. $return .= '</div>';
  2403. } else {
  2404. $return .= Display::div(Display::url(Display::return_icon('close.png', get_lang('Close'), array(), ICON_SIZE_SMALL), ' javascript:void(0);', array('id' => 'close_div_' . $course_info['real_id'] . '_' . $session_id, 'class' => 'close_div')), array('style' => 'position:absolute;right:10px'));
  2405. }
  2406. // If you want to debug it, I advise you to do "echo" on the eval statements.
  2407. if (!empty($resources) && $user_in_course) {
  2408. foreach ($resources as $resource) {
  2409. $is_visible = self::is_visible_by_id($resource['id'], $course_info, $session_id, api_get_user_id());
  2410. if (!$is_visible) {
  2411. continue;
  2412. }
  2413. $resource_paths = explode('/', $resource['path']);
  2414. array_shift($resource_paths);
  2415. $path_to_eval = $last_path = '';
  2416. $is_file = false;
  2417. if ($resource['filetype'] == 'file') {
  2418. foreach ($resource_paths as $key => $resource_path) {
  2419. if ($key != count($resource_paths) - 1) {
  2420. // It's a folder.
  2421. $path_to_eval .= "['$resource_path']['files']";
  2422. }
  2423. $is_file = true;
  2424. }
  2425. } else {
  2426. foreach ($resource_paths as $key => $resource_path) {
  2427. if ($key != count($resource_paths) - 1) {
  2428. // It's a folder.
  2429. $path_to_eval .= "['$resource_path']['files']";
  2430. }
  2431. }
  2432. }
  2433. $last_path = $resource_path;
  2434. //$data = json_encode(array('title'=>$resource['title'], 'path'=>$last_path));
  2435. //@todo not sure if it's a good thing using base64_encode. I tried with json_encode but i received the same error
  2436. //Some testing is needed in order to prove the performance
  2437. //Also change the explode to value from "/" to "|@j@|" it fixes #3780
  2438. $data = base64_encode($resource['title'] . '|@j@|' . $last_path);
  2439. if ($is_file) {
  2440. //for backward compatibility
  2441. if (empty($resource['title'])) {
  2442. $resource['title'] = basename($resource['path']);
  2443. }
  2444. eval('$resources_sorted' . $path_to_eval . '[' . $resource['id'] . '] = "' . $data . '" ; ');
  2445. } else {
  2446. eval('$resources_sorted' . $path_to_eval . '["' . $last_path . '"]["id"]=' . $resource['id'] . ';');
  2447. eval('$resources_sorted' . $path_to_eval . '["' . $last_path . '"]["title"]= "' . api_htmlentities($resource['title']) . '";');
  2448. }
  2449. }
  2450. }
  2451. $label = get_lang('Documents');
  2452. $new_array[$label] = array('id' => 0, 'files' => $resources_sorted);
  2453. $write_result = self::write_resources_tree($course_info, $session_id, $new_array, 0, $lp_id, $target, $add_move_button, $overwrite_url);
  2454. $return .= $write_result;
  2455. $img_path = api_get_path(WEB_IMG_PATH);
  2456. if ($lp_id == false) {
  2457. $return .= "<script>
  2458. $('.doc_folder').mouseover(function() {
  2459. var my_id = this.id.split('_')[2];
  2460. $('#res_'+my_id).show();
  2461. });
  2462. $('.close_div').click(function() {
  2463. var course_id = this.id.split('_')[2];
  2464. var session_id = this.id.split('_')[3];
  2465. $('#document_result_'+course_id+'_'+session_id).hide();
  2466. $('.lp_resource').remove();
  2467. });
  2468. </script>";
  2469. } else {
  2470. //For LPs
  2471. $return .= "<script>
  2472. function testResources(id, img) {
  2473. if (document.getElementById(id).style.display=='block'){
  2474. document.getElementById(id).style.display='none';
  2475. var id = id.split('_')[1];
  2476. document.getElementById('img_'+id).src='" . $img_path . "nolines_plus.gif';
  2477. } else {
  2478. document.getElementById(id).style.display='block';
  2479. var id = id.split('_')[1];
  2480. document.getElementById('img_'+id).src='" . $img_path . "nolines_minus.gif';
  2481. }
  2482. }
  2483. </script>";
  2484. }
  2485. if (!$user_in_course) {
  2486. $return = '';
  2487. }
  2488. return $return;
  2489. }
  2490. /**
  2491. * Generate and return an HTML list of resources based on a given array.
  2492. * This list is used to show the course creator a list of available resources to choose from
  2493. * when creating a learning path.
  2494. * @param array Array of elements to add to the list
  2495. * @param integer Enables the tree display by shifting the new elements a certain distance to the right
  2496. * @return string The HTML list
  2497. */
  2498. public static function write_resources_tree($course_info, $session_id, $resources_sorted, $num = 0, $lp_id = false, $target = '', $add_move_button = false, $overwrite_url = null) {
  2499. require_once api_get_path(LIBRARY_PATH) . 'fileDisplay.lib.php';
  2500. $img_path = api_get_path(WEB_IMG_PATH);
  2501. $img_sys_path = api_get_path(SYS_CODE_PATH) . 'img/';
  2502. $web_code_path = api_get_path(WEB_CODE_PATH);
  2503. $return = '';
  2504. if (count($resources_sorted) > 0) {
  2505. foreach ($resources_sorted as $key => $resource) {
  2506. $title = isset($resource['title']) ? $resource['title'] : null;
  2507. if (empty($title)) {
  2508. $title = $key;
  2509. }
  2510. //echo '<pre>'; print_r($resource);
  2511. if (isset($resource['id']) && is_int($resource['id'])) {
  2512. // It's a folder.
  2513. //hide some folders
  2514. if (in_array($key, array('shared_folder', 'chat_files', 'HotPotatoes_files', 'css', 'certificates'))) {
  2515. continue;
  2516. } elseif (preg_match('/_groupdocs/', $key)) {
  2517. continue;
  2518. } elseif (preg_match('/sf_user_/', $key)) {
  2519. continue;
  2520. } elseif (preg_match('/shared_folder_session_/', $key)) {
  2521. continue;
  2522. }
  2523. //trad some titles
  2524. if ($key == 'images') {
  2525. $key = get_lang('Images');
  2526. } elseif ($key == 'gallery') {
  2527. $key = get_lang('Gallery');
  2528. } elseif ($key == 'flash') {
  2529. $key = get_lang('Flash');
  2530. } elseif ($key == 'audio') {
  2531. $key = get_lang('Audio');
  2532. } elseif ($key == 'video') {
  2533. $key = get_lang('Video');
  2534. }
  2535. $onclick = '';
  2536. if ($lp_id) {
  2537. $onclick = 'onclick="javascript: testResources(\'res_' . $resource['id'] . '\',\'img_' . $resource['id'] . '\')"';
  2538. }
  2539. $return .= '<ul class="lp_resource">';
  2540. $return .= '<li class="doc_folder" id="doc_id_' . $resource['id'] . '" style="margin-left:' . ($num * 18) . 'px; ">';
  2541. if ($lp_id) {
  2542. $return .= '<img style="cursor: pointer;" src="' . $img_path . 'nolines_plus.gif" align="absmiddle" id="img_' . $resource['id'] . '" ' . $onclick . ' >';
  2543. } else {
  2544. $return .= '<span style="margin-left:16px">&nbsp;</span>';
  2545. }
  2546. $return .= '<img alt="" src="' . $img_path . 'lp_folder.gif" title="" align="absmiddle" />&nbsp;';
  2547. $return .= '<span ' . $onclick . ' style="cursor: pointer;" >' . $title . '</span>';
  2548. $return .= '</li>';
  2549. $return .= '<div id="res_' . $resource['id'] . '" style="display: none;" >';
  2550. if (isset($resource['files'])) {
  2551. $return .= self::write_resources_tree($course_info, $session_id, $resource['files'], $num + 1, $lp_id, $target, $add_move_button, $overwrite_url);
  2552. }
  2553. $return .= '</div>';
  2554. $return .= '</ul>';
  2555. } else {
  2556. if (!is_array($resource)) {
  2557. $resource = base64_decode($resource);
  2558. // It's a file.
  2559. $icon = choose_image($resource);
  2560. $position = strrpos($icon, '.');
  2561. $icon = substr($icon, 0, $position) . '_small.gif';
  2562. $file_info = explode('|@j@|', $resource);
  2563. $my_file_title = $file_info[0];
  2564. //If title is empty we try to use the path
  2565. if (empty($my_file_title)) {
  2566. $my_file_title = $file_info[1];
  2567. }
  2568. // Show the "image name" not the filename of the image.
  2569. if ($lp_id) {
  2570. //LP URL
  2571. $url = api_get_self() . '?cidReq=' . Security::remove_XSS($_GET['cidReq']) . '&amp;action=add_item&amp;type=' . TOOL_DOCUMENT . '&amp;file=' . $key . '&amp;lp_id=' . $lp_id;
  2572. if (!empty($overwrite_url)) {
  2573. $url = $overwrite_url . '&document_id=' . $key;
  2574. }
  2575. } else {
  2576. //Direct document URL
  2577. $url = $web_code_path . 'document/document.php?cidReq=' . $course_info['code'] . '&id_session=' . $session_id . '&id=' . $key;
  2578. if (!empty($overwrite_url)) {
  2579. $url = $overwrite_url . '&document_id=' . $key;
  2580. }
  2581. }
  2582. $img = $img_path . $icon;
  2583. if (!file_exists($img_sys_path . $icon)) {
  2584. $img = $img_path . 'icons/16/default_small.gif';
  2585. }
  2586. $link = Display::url('<img alt="" src="' . $img . '" title="" />&nbsp;' . $my_file_title, $url, array('target' => $target));
  2587. if ($lp_id == false) {
  2588. $return .= '<li class="doc_resource" data_id="' . $key . '" data_type="document" title="' . $my_file_title . '" >';
  2589. } else {
  2590. $return .= '<li class="doc_resource lp_resource_element" data_id="' . $key . '" data_type="document" title="' . $my_file_title . '" >';
  2591. }
  2592. $return .= '<div class="item_data" style="margin-left:' . (($num + 1) * 18) . 'px;margin-right:5px;">';
  2593. if ($add_move_button) {
  2594. $return .= '<a class="moved" href="#">';
  2595. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  2596. $return .= '</a> ';
  2597. }
  2598. $return .= $link;
  2599. $return .= '</div></li>';
  2600. }
  2601. }
  2602. }
  2603. }
  2604. return $return;
  2605. }
  2606. public static function check_visibility_tree($doc_id, $course_code, $session_id, $user_id) {
  2607. $document_data = self::get_document_data_by_id($doc_id, $course_code);
  2608. if (!empty($document_data)) {
  2609. //if admin or course teacher, allow anyway
  2610. if (api_is_platform_admin() || CourseManager::is_course_teacher($user_id, $course_code)) {
  2611. return true;
  2612. }
  2613. $course_info = api_get_course_info($course_code);
  2614. if ($document_data['parent_id'] == false || empty($document_data['parent_id'])) {
  2615. $visible = self::is_visible_by_id($doc_id, $course_info, $session_id, $user_id);
  2616. return $visible;
  2617. } else {
  2618. $course_info = api_get_course_info($course_code);
  2619. $visible = self::is_visible_by_id($doc_id, $course_info, $session_id, $user_id);
  2620. if (!$visible) {
  2621. return false;
  2622. } else {
  2623. return self::check_visibility_tree($document_data['parent_id'], $course_code, $session_id, $user_id);
  2624. }
  2625. }
  2626. } else {
  2627. return false;
  2628. }
  2629. }
  2630. /**
  2631. * Index a given document.
  2632. * @param int Document ID inside its corresponding course
  2633. * @param string Course code
  2634. * @param int Session ID (not used yet)
  2635. * @param string Language of document's content (defaults to course language)
  2636. * @param array Array of specific fields (['code'=>'value',...])
  2637. * @param string What to do if the file already exists (default or overwrite)
  2638. * @param bool When set to true, this runs the indexer without actually saving anything to any database
  2639. * @return bool Returns true on presumed success, false on failure
  2640. */
  2641. public function index_document($docid, $course_code, $session_id = 0, $lang = 'english', $specific_fields_values = array(), $if_exists = '', $simulation = false) {
  2642. if (api_get_setting('search_enabled') !== 'true') {
  2643. return false;
  2644. }
  2645. if (empty($docid) or $docid != intval($docid)) {
  2646. return false;
  2647. }
  2648. if (empty($session_id)) {
  2649. $session_id = api_get_session_id();
  2650. }
  2651. $course_info = api_get_course_info($course_code);
  2652. $course_dir = $course_info['path'] . '/document';
  2653. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  2654. $base_work_dir = $sys_course_path . $course_dir;
  2655. $course_id = $course_info['real_id'];
  2656. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  2657. $qry = "SELECT path, title FROM $table_document WHERE c_id = $course_id AND id = '$docid' LIMIT 1";
  2658. $result = Database::query($qry);
  2659. if (Database::num_rows($result) == 1) {
  2660. $row = Database::fetch_array($result);
  2661. $doc_path = api_get_path(SYS_COURSE_PATH) . $course_dir . $row['path'];
  2662. //TODO: mime_content_type is deprecated, fileinfo php extension is enabled by default as of PHP 5.3.0
  2663. // now versions of PHP on Debian testing(5.2.6-5) and Ubuntu(5.2.6-2ubuntu) are lower, so wait for a while
  2664. $doc_mime = mime_content_type($doc_path);
  2665. //echo $doc_mime;
  2666. $allowed_mime_types = self::file_get_mime_type(true);
  2667. // mime_content_type does not detect correctly some formats that are going to be supported for index, so an extensions array is used for the moment
  2668. if (empty($doc_mime)) {
  2669. $allowed_extensions = array('doc', 'docx', 'ppt', 'pptx', 'pps', 'ppsx', 'xls', 'xlsx', 'odt', 'odp', 'ods', 'pdf', 'txt', 'rtf', 'msg', 'csv', 'html', 'htm');
  2670. $extensions = preg_split("/[\/\\.]/", $doc_path);
  2671. $doc_ext = strtolower($extensions[count($extensions) - 1]);
  2672. if (in_array($doc_ext, $allowed_extensions)) {
  2673. switch ($doc_ext) {
  2674. case 'ppt':
  2675. case 'pps':
  2676. $doc_mime = 'application/vnd.ms-powerpoint';
  2677. break;
  2678. case 'xls':
  2679. $doc_mime = 'application/vnd.ms-excel';
  2680. break;
  2681. }
  2682. }
  2683. }
  2684. //@todo move this nightmare in a search controller or something like that!!! J.M
  2685. if (in_array($doc_mime, $allowed_mime_types)) {
  2686. $file_title = $row['title'];
  2687. $file_content = self::get_text_content($doc_path, $doc_mime);
  2688. $course_code = Database::escape_string($course_code);
  2689. require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
  2690. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  2691. $ic_slide = new IndexableChunk();
  2692. $ic_slide->addValue('title', $file_title);
  2693. $ic_slide->addCourseId($course_code);
  2694. $ic_slide->addToolId(TOOL_DOCUMENT);
  2695. $xapian_data = array(
  2696. SE_COURSE_ID => $course_code,
  2697. SE_TOOL_ID => TOOL_DOCUMENT,
  2698. SE_DATA => array('doc_id' => $docid),
  2699. SE_USER => api_get_user_id(),
  2700. );
  2701. $ic_slide->xapian_data = serialize($xapian_data);
  2702. $di = new ChamiloIndexer();
  2703. $return = $di->connectDb(null, null, $lang);
  2704. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  2705. $specific_fields = get_specific_field_list();
  2706. // process different depending on what to do if file exists
  2707. /**
  2708. * @TODO Find a way to really verify if the file had been
  2709. * overwriten. Now all work is done at
  2710. * handle_uploaded_document() and it's difficult to verify it
  2711. */
  2712. if (!empty($if_exists) && $if_exists == 'overwrite') {
  2713. // Overwrite the file on search engine
  2714. // Actually, it consists on a delete of terms from db,
  2715. // insert new ones, create a new search engine document,
  2716. // and remove the old one
  2717. // Get search_did
  2718. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  2719. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  2720. $sql = sprintf($sql, $tbl_se_ref, $course_code, TOOL_DOCUMENT, $docid);
  2721. $res = Database::query($sql);
  2722. if (Database::num_rows($res) > 0) {
  2723. $se_ref = Database::fetch_array($res);
  2724. if (!$simulation) {
  2725. $di->remove_document($se_ref['search_did']);
  2726. }
  2727. $all_specific_terms = '';
  2728. foreach ($specific_fields as $specific_field) {
  2729. if (!$simulation) {
  2730. delete_all_specific_field_value($course_code, $specific_field['id'], TOOL_DOCUMENT, $docid);
  2731. }
  2732. // Update search engine
  2733. if (isset($specific_fields_values[$specific_field['code']])) {
  2734. $sterms = trim($specific_fields_values[$specific_field['code']]);
  2735. } else { //if the specific field is not defined, force an empty one
  2736. $sterms = '';
  2737. }
  2738. $all_specific_terms .= ' ' . $sterms;
  2739. $sterms = explode(',', $sterms);
  2740. foreach ($sterms as $sterm) {
  2741. $sterm = trim($sterm);
  2742. if (!empty($sterm)) {
  2743. $ic_slide->addTerm($sterm, $specific_field['code']);
  2744. // updated the last param here from $value to $sterm without being sure - see commit15464
  2745. if (!$simulation) {
  2746. add_specific_field_value($specific_field['id'], $course_code, TOOL_DOCUMENT, $docid, $sterm);
  2747. }
  2748. }
  2749. }
  2750. }
  2751. // Add terms also to content to make terms findable by probabilistic search
  2752. $file_content = $all_specific_terms . ' ' . $file_content;
  2753. if (!$simulation) {
  2754. $ic_slide->addValue('content', $file_content);
  2755. $di->addChunk($ic_slide);
  2756. // Index and return a new search engine document id
  2757. $did = $di->index();
  2758. //var_dump($did);
  2759. if ($did) {
  2760. // update the search_did on db
  2761. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  2762. $sql = 'UPDATE %s SET search_did=%d WHERE id=%d LIMIT 1';
  2763. $sql = sprintf($sql, $tbl_se_ref, (int) $did, (int) $se_ref['id']);
  2764. Database::query($sql);
  2765. }
  2766. }
  2767. }
  2768. } else {
  2769. // Add all terms
  2770. $all_specific_terms = '';
  2771. foreach ($specific_fields as $specific_field) {
  2772. if (isset($specific_fields_values[$specific_field['code']])) {
  2773. $sterms = trim($specific_fields_values[$specific_field['code']]);
  2774. } else { //if the specific field is not defined, force an empty one
  2775. $sterms = '';
  2776. }
  2777. $all_specific_terms .= ' ' . $sterms;
  2778. if (!empty($sterms)) {
  2779. $sterms = explode(',', $sterms);
  2780. foreach ($sterms as $sterm) {
  2781. if (!$simulation) {
  2782. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  2783. add_specific_field_value($specific_field['id'], $course_code, TOOL_DOCUMENT, $docid, $sterm);
  2784. }
  2785. }
  2786. }
  2787. }
  2788. // Add terms also to content to make terms findable by probabilistic search
  2789. $file_content = $all_specific_terms . ' ' . $file_content;
  2790. if (!$simulation) {
  2791. $ic_slide->addValue('content', $file_content);
  2792. $di->addChunk($ic_slide);
  2793. // Index and return search engine document id
  2794. $did = $di->index();
  2795. if ($did) {
  2796. // Save it to db
  2797. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  2798. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  2799. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  2800. $sql = sprintf($sql, $tbl_se_ref, $course_code, TOOL_DOCUMENT, $docid, $did);
  2801. Database::query($sql);
  2802. } else {
  2803. return false;
  2804. }
  2805. }
  2806. }
  2807. } else {
  2808. return false;
  2809. }
  2810. }
  2811. return true;
  2812. }
  2813. public static function get_web_odf_extension_list() {
  2814. return array('ods', 'odt');
  2815. }
  2816. public static function is_folder_to_avoid($path, $is_certificate_mode = false) {
  2817. $folders_to_avoid = array(
  2818. '/HotPotatoes_files',
  2819. '/certificates',
  2820. );
  2821. if (basename($path) == 'css') {
  2822. return true;
  2823. }
  2824. //Skip hotpotatoes results
  2825. if (strstr($path, 'HotPotatoes_files')) {
  2826. return true;
  2827. }
  2828. if ($is_certificate_mode == false) {
  2829. //Certificate results
  2830. if (strstr($path, 'certificates')) {
  2831. return true;
  2832. }
  2833. }
  2834. //Admin setting for Hide/Show the folders of all users
  2835. if (api_get_setting('show_users_folders') == 'false') {
  2836. $folders_to_avoid[] = '/shared_folder';
  2837. if (strstr($path, 'shared_folder_session_')) {
  2838. return true;
  2839. }
  2840. }
  2841. //Admin setting for Hide/Show Default folders to all users
  2842. if (api_get_setting('show_default_folders') == 'false') {
  2843. $folders_to_avoid[] = '/images';
  2844. $folders_to_avoid[] = '/flash';
  2845. $folders_to_avoid[] = '/audio';
  2846. $folders_to_avoid[] = '/video';
  2847. }
  2848. //Admin setting for Hide/Show chat history folder
  2849. if (api_get_setting('show_chat_folder') == 'false') {
  2850. $folders_to_avoid[] = '/chat_files';
  2851. }
  2852. return in_array($path, $folders_to_avoid);
  2853. }
  2854. static function get_system_folders() {
  2855. $system_folders = array(
  2856. '/certificates',
  2857. '/chat_files',
  2858. '/images',
  2859. '/flash',
  2860. '/audio',
  2861. '/video',
  2862. '/shared_folder'
  2863. );
  2864. return $system_folders;
  2865. }
  2866. }
  2867. //end class DocumentManager