document.lib.php 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197
  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. if ($to_group_id <> 0) {
  580. $sql = "SELECT DISTINCT docs.id, path
  581. FROM $TABLE_ITEMPROPERTY AS last INNER JOIN $TABLE_DOCUMENT AS docs
  582. ON (docs.id = last.ref AND last.tool = '" . TOOL_DOCUMENT . "' AND last.c_id = {$_course['real_id']} AND docs.c_id = {$_course['real_id']} )
  583. WHERE
  584. docs.filetype = 'folder' AND
  585. last.to_group_id = " . $to_group_id . " AND
  586. docs.path NOT LIKE '%shared_folder%' AND
  587. last.visibility <> 2 $condition_session ";
  588. } else {
  589. $sql = "SELECT DISTINCT docs.id, path
  590. FROM $TABLE_ITEMPROPERTY AS last INNER JOIN $TABLE_DOCUMENT AS docs
  591. ON (docs.id = last.ref AND last.tool = '" . TOOL_DOCUMENT . "' AND last.c_id = {$_course['real_id']} AND docs.c_id = {$_course['real_id']} )
  592. WHERE
  593. docs.filetype = 'folder' AND
  594. last.to_group_id = " . $to_group_id . " AND
  595. last.visibility <> 2 $condition_session ";
  596. }
  597. $result = Database::query($sql);
  598. if ($result && Database::num_rows($result) != 0) {
  599. while ($row = Database::fetch_array($result, 'ASSOC')) {
  600. if (DocumentManager::is_folder_to_avoid($row['path'])) {
  601. continue;
  602. }
  603. $document_folders[$row['id']] = $row['path'];
  604. }
  605. //sort($document_folders);
  606. natsort($document_folders);
  607. //return results
  608. return $document_folders;
  609. } else {
  610. return false;
  611. }
  612. } else {
  613. //no invisible folders
  614. //condition for the session
  615. $session_id = api_get_session_id();
  616. $condition_session = api_get_session_condition($session_id);
  617. //get visible folders
  618. $visible_sql = "SELECT DISTINCT docs.id, path
  619. FROM " . $TABLE_ITEMPROPERTY . " AS last, " . $TABLE_DOCUMENT . " AS docs
  620. WHERE docs.id = last.ref
  621. AND docs.filetype = 'folder'
  622. AND last.tool = '" . TOOL_DOCUMENT . "'
  623. AND last.to_group_id = " . $to_group_id . "
  624. AND last.visibility = 1 $condition_session AND
  625. last.c_id = {$_course['real_id']} AND
  626. docs.c_id = {$_course['real_id']} ";
  627. $visibleresult = Database::query($visible_sql);
  628. while ($all_visible_folders = Database::fetch_array($visibleresult, 'ASSOC')) {
  629. $visiblefolders[$all_visible_folders['id']] = $all_visible_folders['path'];
  630. }
  631. //condition for the session
  632. $session_id = api_get_session_id();
  633. $condition_session = api_get_session_condition($session_id);
  634. //get invisible folders
  635. $invisible_sql = "SELECT DISTINCT docs.id, path
  636. FROM " . $TABLE_ITEMPROPERTY . " AS last, " . $TABLE_DOCUMENT . " AS docs
  637. WHERE docs.id = last.ref
  638. AND docs.filetype = 'folder'
  639. AND last.tool = '" . TOOL_DOCUMENT . "'
  640. AND last.to_group_id = " . $to_group_id . "
  641. AND last.visibility = 0 $condition_session AND
  642. last.c_id = {$_course['real_id']} AND
  643. docs.c_id = {$_course['real_id']} ";
  644. $invisibleresult = Database::query($invisible_sql);
  645. while ($invisible_folders = Database::fetch_array($invisibleresult, 'ASSOC')) {
  646. //condition for the session
  647. $session_id = api_get_session_id();
  648. $condition_session = api_get_session_condition($session_id);
  649. //get visible folders in the invisible ones -> they are invisible too
  650. $folder_in_invisible_sql = "SELECT DISTINCT docs.id, path
  651. FROM " . $TABLE_ITEMPROPERTY . " AS last, " . $TABLE_DOCUMENT . " AS docs
  652. WHERE docs.id = last.ref
  653. AND docs.path LIKE '" . Database::escape_string($invisible_folders['path']) . "/%'
  654. AND docs.filetype = 'folder'
  655. AND last.tool = '" . TOOL_DOCUMENT . "'
  656. AND last.to_group_id = " . $to_group_id . "
  657. AND last.visibility = 1 $condition_session AND
  658. last.c_id = {$_course['real_id']} AND
  659. docs.c_id = {$_course['real_id']} ";
  660. $folder_in_invisible_result = Database::query($folder_in_invisible_sql);
  661. while ($folders_in_invisible_folder = Database::fetch_array($folder_in_invisible_result, 'ASSOC')) {
  662. $invisiblefolders[$folders_in_invisible_folder['id']] = $folders_in_invisible_folder['path'];
  663. }
  664. }
  665. //if both results are arrays -> //calculate the difference between the 2 arrays -> only visible folders are left :)
  666. if (is_array($visiblefolders) && is_array($invisiblefolders)) {
  667. $document_folders = array_diff($visiblefolders, $invisiblefolders);
  668. natsort($document_folders);
  669. return $document_folders;
  670. } elseif (is_array($visiblefolders)) {
  671. //only visible folders found
  672. //sort($visiblefolders);
  673. natsort($visiblefolders);
  674. return $visiblefolders;
  675. } else {
  676. //no visible folders found
  677. return false;
  678. }
  679. }
  680. }
  681. /**
  682. * This check if a document has the readonly property checked, then see if the user
  683. * is the owner of this file, if all this is true then return true.
  684. *
  685. * @param array $_course
  686. * @param int $user_id id of the current user
  687. * @param string $file path stored in the database
  688. * @param int $document_id in case you dont have the file path ,insert the id of the file here and leave $file in blank ''
  689. * @return boolean true/false
  690. * */
  691. public static function check_readonly($_course, $user_id, $file, $document_id = '', $to_delete = false) {
  692. if (!(!empty($document_id) && is_numeric($document_id))) {
  693. $document_id = self::get_document_id($_course, $file);
  694. }
  695. $TABLE_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  696. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  697. $course_id = $_course['real_id'];
  698. if ($to_delete) {
  699. if (self::is_folder($_course, $document_id)) {
  700. if (!empty($file)) {
  701. $path = Database::escape_string($file);
  702. $what_to_check_sql = "SELECT td.id, readonly, tp.insert_user_id FROM " . $TABLE_DOCUMENT . " td , $TABLE_PROPERTY tp
  703. WHERE td.c_id = $course_id AND
  704. tp.c_id = $course_id AND
  705. tp.ref= td.id AND
  706. (path='" . $path . "' OR path LIKE BINARY '" . $path . "/%' ) ";
  707. //get all id's of documents that are deleted
  708. $what_to_check_result = Database::query($what_to_check_sql);
  709. if ($what_to_check_result && Database::num_rows($what_to_check_result) != 0) {
  710. // file with readonly set to 1 exist?
  711. $readonly_set = false;
  712. while ($row = Database::fetch_array($what_to_check_result)) {
  713. //query to delete from item_property table
  714. if ($row['readonly'] == 1) {
  715. if (!($row['insert_user_id'] == $user_id)) {
  716. $readonly_set = true;
  717. break;
  718. }
  719. }
  720. }
  721. if ($readonly_set) {
  722. return true;
  723. }
  724. }
  725. }
  726. return false;
  727. }
  728. }
  729. if (!empty($document_id)) {
  730. $sql = "SELECT a.insert_user_id, b.readonly FROM $TABLE_PROPERTY a, $TABLE_DOCUMENT b
  731. WHERE
  732. a.c_id = $course_id AND
  733. b.c_id = $course_id AND
  734. a.ref = b.id and a.ref= $document_id LIMIT 1";
  735. $resultans = Database::query($sql);
  736. $doc_details = Database ::fetch_array($resultans, 'ASSOC');
  737. if ($doc_details['readonly'] == 1) {
  738. return !($doc_details['insert_user_id'] == $user_id || api_is_platform_admin());
  739. }
  740. }
  741. return false;
  742. }
  743. /**
  744. * This check if a document is a folder or not
  745. * @param array $_course
  746. * @param int $document_id of the item
  747. * @return boolean true/false
  748. * */
  749. public static function is_folder($_course, $document_id) {
  750. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  751. $course_id = $_course['real_id'];
  752. $document_id = Database::escape_string($document_id);
  753. $result = Database::fetch_array(Database::query("SELECT filetype FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND id= $document_id"), 'ASSOC');
  754. return $result['filetype'] == 'folder';
  755. }
  756. public static function delete_document_from_db($document_id, $course_info = array(), $session_id = 0, $remove_content_from_db = false) {
  757. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  758. $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  759. //Deleting from the DB
  760. $user_id = api_get_user_id();
  761. if (empty($course_info)) {
  762. $course_info = api_get_course_info();
  763. }
  764. if (empty($session_id)) {
  765. $session_id = api_get_session_id();
  766. }
  767. //Soft DB delete
  768. api_item_property_update($course_info, TOOL_DOCUMENT, $document_id, 'delete', $user_id, null, null, null, null, $session_id);
  769. self::delete_document_from_search_engine($course_info['code'], $document_id);
  770. self::unset_document_as_template($document_id, $course_info['code'], $user_id);
  771. //Hard DB delete
  772. if ($remove_content_from_db) {
  773. $sql = "DELETE FROM $TABLE_ITEMPROPERTY WHERE c_id = {$course_info['real_id']} AND ref = " . $document_id . " AND tool='" . TOOL_DOCUMENT . "'";
  774. Database::query($sql);
  775. $sql = "DELETE FROM " . $TABLE_DOCUMENT . " WHERE c_id = {$course_info['real_id']} AND id = " . $document_id;
  776. Database::query($sql);
  777. self::delete_document_metadata($document_id);
  778. }
  779. }
  780. public static function delete_document_metadata($document_id) {
  781. //needed to deleted medadata
  782. require_once api_get_path(SYS_CODE_PATH) . 'metadata/md_funcs.php';
  783. require_once api_get_path(LIBRARY_PATH) . 'fileManage.lib.php';
  784. $mdStore = new mdstore(true);
  785. //delete metadata
  786. $eid = 'Document' . '.' . $document_id;
  787. $mdStore->mds_delete($eid);
  788. $mdStore->mds_delete_offspring($eid);
  789. }
  790. /**
  791. * This deletes a document by changing visibility to 2, renaming it to filename_DELETED_#id
  792. * Files/folders that are inside a deleted folder get visibility 2
  793. *
  794. * @param array $_course
  795. * @param string $path, path stored in the database
  796. * @param string ,$base_work_dir, path to the documents folder
  797. * @return boolean true/false
  798. * @todo now only files/folders in a folder get visibility 2, we should rename them too.
  799. */
  800. public static function delete_document($_course, $path, $base_work_dir) {
  801. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  802. if (empty($path) || empty($base_work_dir)) {
  803. return false;
  804. }
  805. $course_id = $_course['real_id'];
  806. //first, delete the actual document...
  807. $document_id = self :: get_document_id($_course, $path);
  808. $document_exists_in_disk = file_exists($base_work_dir . $path);
  809. $new_path = $path . '_DELETED_' . $document_id;
  810. $current_session_id = api_get_session_id();
  811. $file_deleted_from_db = false;
  812. $file_deleted_from_disk = false;
  813. $file_renamed_from_disk = false;
  814. if ($document_id) {
  815. self::delete_document_from_db($document_id);
  816. //checking
  817. //$file_exists_in_db = self::get_document_data_by_id($document_id, $_course['code']);
  818. $file_deleted_from_db = true;
  819. }
  820. if ($document_exists_in_disk) {
  821. if (api_get_setting('permanently_remove_deleted_files') == 'true') {
  822. //Deleted files are *really* deleted
  823. $what_to_delete_sql = "SELECT id FROM " . $TABLE_DOCUMENT . " WHERE c_id = $course_id AND path='" . $path . "' OR path LIKE BINARY '" . $path . "/%'";
  824. //get all id's of documents that are deleted
  825. $what_to_delete_result = Database::query($what_to_delete_sql);
  826. if ($what_to_delete_result && Database::num_rows($what_to_delete_result) != 0) {
  827. //delete all item_property entries
  828. while ($row = Database::fetch_array($what_to_delete_result)) {
  829. //query to delete from item_property table (hard way)
  830. self::delete_document_from_db($row['id'], $_course, $current_session_id, true);
  831. }
  832. //delete documents, do it like this so metadata get's deleted too
  833. //update_db_info('delete', $path);
  834. //throw it away
  835. my_delete($base_work_dir . $path);
  836. $file_deleted_from_disk = true;
  837. }
  838. } else {
  839. //Set visibility to 2 and rename file/folder to xxx_DELETED_#id (soft delete)
  840. if (is_file($base_work_dir . $path) || is_dir($base_work_dir . $path)) {
  841. if (rename($base_work_dir . $path, $base_work_dir . $new_path)) {
  842. $sql = "UPDATE $TABLE_DOCUMENT set path='" . $new_path . "' WHERE c_id = $course_id AND id='" . $document_id . "'";
  843. Database::query($sql);
  844. $sql = "SELECT id, path FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND path LIKE BINARY '" . $path . "/%'";
  845. $result = Database::query($sql);
  846. if ($result && Database::num_rows($result) > 0) {
  847. while ($deleted_items = Database::fetch_array($result, 'ASSOC')) {
  848. self::delete_document_from_db($deleted_items['id']);
  849. //Change path of subfolders and documents in database
  850. $old_item_path = $deleted_items['path'];
  851. $new_item_path = $new_path . substr($old_item_path, strlen($path));
  852. $sql = "UPDATE $TABLE_DOCUMENT set path = '" . $new_item_path . "' WHERE c_id = $course_id AND id = " . $deleted_items['id'];
  853. Database::query($sql);
  854. }
  855. }
  856. $file_renamed_from_disk = true;
  857. } else {
  858. //Couldn't rename - file permissions problem?
  859. error_log(__FILE__ . ' ' . __LINE__ . ': Error renaming ' . $base_work_dir . $path . ' to ' . $base_work_dir . $new_path . '. This is probably due to file permissions', 0);
  860. }
  861. }
  862. }
  863. }
  864. //Checking inconsistency
  865. if ($file_deleted_from_db && $file_deleted_from_disk ||
  866. $file_deleted_from_db && $file_renamed_from_disk) {
  867. return true;
  868. } else {
  869. //Something went wrong
  870. //The file or directory isn't there anymore (on the filesystem)
  871. // This means it has been removed externally. To prevent a
  872. // blocking error from happening, we drop the related items from the
  873. // item_property and the document table.
  874. 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);
  875. return false;
  876. }
  877. }
  878. /**
  879. * Removes documents from search engine database
  880. *
  881. * @param string $course_id Course code
  882. * @param int $document_id Document id to delete
  883. */
  884. public static function delete_document_from_search_engine($course_id, $document_id) {
  885. // remove from search engine if enabled
  886. if (api_get_setting('search_enabled') == 'true') {
  887. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  888. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  889. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_DOCUMENT, $document_id);
  890. $res = Database::query($sql);
  891. if (Database::num_rows($res) > 0) {
  892. $row2 = Database::fetch_array($res);
  893. require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
  894. $di = new ChamiloIndexer();
  895. $di->remove_document((int) $row2['search_did']);
  896. }
  897. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  898. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_DOCUMENT, $document_id);
  899. Database::query($sql);
  900. // remove terms from db
  901. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  902. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $document_id);
  903. }
  904. }
  905. /**
  906. * Gets the id of a document with a given path
  907. *
  908. * @param array $_course
  909. * @param string $path
  910. * @return int id of document / false if no doc found
  911. */
  912. public static function get_document_id($course_info, $path) {
  913. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  914. $course_id = $course_info['real_id'];
  915. $path = Database::escape_string($path);
  916. if (!empty($course_id) && !empty($path)) {
  917. $sql = "SELECT id FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND path LIKE BINARY '$path' LIMIT 1";
  918. $result = Database::query($sql);
  919. if ($result && Database::num_rows($result)) {
  920. $row = Database::fetch_array($result);
  921. return intval($row[0]);
  922. }
  923. }
  924. return false;
  925. }
  926. /**
  927. * Gets the document data with a given id
  928. *
  929. * @param array $_course
  930. * @param string $path
  931. * @todo load parent_id
  932. * @return int id of document / false if no doc found
  933. */
  934. public static function get_document_data_by_id($id, $course_code, $load_parents = false) {
  935. $course_info = api_get_course_info($course_code);
  936. $course_id = $course_info['real_id'];
  937. if (empty($course_info)) {
  938. return false;
  939. }
  940. $www = api_get_path(WEB_COURSE_PATH) . $course_info['path'] . '/document';
  941. $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
  942. $id = intval($id);
  943. $sql = "SELECT * FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND id = $id ";
  944. $result = Database::query($sql);
  945. if ($result && Database::num_rows($result) == 1) {
  946. $row = Database::fetch_array($result, 'ASSOC');
  947. //@todo need to clarify the name of the URLs not nice right now
  948. $url_path = urlencode($row['path']);
  949. $path = str_replace('%2F', '/', $url_path);
  950. $row['url'] = api_get_path(WEB_CODE_PATH) . 'document/showinframes.php?cidReq=' . $course_code . '&id=' . $id;
  951. $row['document_url'] = api_get_path(WEB_CODE_PATH) . 'document/document.php?cidReq=' . $course_code . '&id=' . $id;
  952. $row['absolute_path'] = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/document' . $row['path'];
  953. $row['absolute_path_from_document'] = '/document' . $row['path'];
  954. $pathinfo = pathinfo($row['path']);
  955. $row['absolute_parent_path'] = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/document' . $pathinfo['dirname'] . '/';
  956. $row['direct_url'] = $www . $path;
  957. if (dirname($row['path']) == '.') {
  958. $row['parent_id'] = '0';
  959. } else {
  960. $row['parent_id'] = self::get_document_id($course_info, dirname($row['path']));
  961. }
  962. $parents = array();
  963. //Use to generate parents (needed for the breadcrumb)
  964. //@todo sorry but this for is here because there's not a parent_id in the document table so we parsed the path!!
  965. $visibility = true;
  966. if ($load_parents) {
  967. $dir_array = explode('/', $row['path']);
  968. $dir_array = array_filter($dir_array);
  969. $array_len = count($dir_array) + 1;
  970. $real_dir = '';
  971. for ($i = 1; $i < $array_len; $i++) {
  972. //$sub_visibility = true;
  973. $real_dir .= '/' . $dir_array[$i];
  974. $parent_id = self::get_document_id($course_info, $real_dir);
  975. if (!empty($parent_id)) {
  976. $sub_document_data = self::get_document_data_by_id($parent_id, $course_code, false);
  977. //@todo add visibility here
  978. /* $sub_visibility = self::is_visible_by_id($parent_id, $course_info, api_get_session_id(), api_get_user_id());
  979. if ($visibility && $sub_visibility == false) {
  980. $visibility = false;
  981. }
  982. */
  983. $parents[] = $sub_document_data;
  984. }
  985. }
  986. }
  987. //$row['visibility_for_user'] = $visibility;
  988. $row['parents'] = $parents;
  989. return $row;
  990. }
  991. return false;
  992. }
  993. /**
  994. * Allow to set a specific document as a new template for FCKEditor for a particular user in a particular course
  995. *
  996. * @param string $title
  997. * @param string $description
  998. * @param int $document_id_for_template the document id
  999. * @param string $couse_code
  1000. * @param int $user_id
  1001. */
  1002. public static function set_document_as_template($title, $description, $document_id_for_template, $couse_code, $user_id, $image) {
  1003. // Database table definition
  1004. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  1005. // creating the sql statement
  1006. $sql = "INSERT INTO " . $table_template . "
  1007. (title, description, course_code, user_id, ref_doc, image)
  1008. VALUES (
  1009. '" . Database::escape_string($title) . "',
  1010. '" . Database::escape_string($description) . "',
  1011. '" . Database::escape_string($couse_code) . "',
  1012. '" . Database::escape_string($user_id) . "',
  1013. '" . Database::escape_string($document_id_for_template) . "',
  1014. '" . Database::escape_string($image) . "')";
  1015. Database::query($sql);
  1016. return true;
  1017. }
  1018. /**
  1019. * Unset a document as template
  1020. *
  1021. * @param int $document_id
  1022. * @param string $couse_code
  1023. * @param int $user_id
  1024. */
  1025. public static function unset_document_as_template($document_id, $course_code, $user_id) {
  1026. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  1027. $course_code = Database::escape_string($course_code);
  1028. $user_id = Database::escape_string($user_id);
  1029. $document_id = Database::escape_string($document_id);
  1030. $sql = 'SELECT id FROM ' . $table_template . ' WHERE course_code="' . $course_code . '" AND user_id="' . $user_id . '" AND ref_doc="' . $document_id . '"';
  1031. $result = Database::query($sql);
  1032. $template_id = Database::result($result, 0, 0);
  1033. include_once(api_get_path(LIBRARY_PATH) . 'fileManage.lib.php');
  1034. my_delete(api_get_path(SYS_CODE_PATH) . 'upload/template_thumbnails/' . $template_id . '.jpg');
  1035. $sql = 'DELETE FROM ' . $table_template . ' WHERE course_code="' . $course_code . '" AND user_id="' . $user_id . '" AND ref_doc="' . $document_id . '"';
  1036. Database::query($sql);
  1037. }
  1038. /**
  1039. * Return true if the documentpath have visibility=1 as item_property (you should use the is_visible_by_id)
  1040. *
  1041. * @param string $document_path the relative complete path of the document
  1042. * @param array $course the _course array info of the document's course
  1043. */
  1044. public static function is_visible($doc_path, $course, $session_id = 0, $file_type = 'file') {
  1045. $docTable = Database::get_course_table(TABLE_DOCUMENT);
  1046. $propTable = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1047. $course_id = $course['real_id'];
  1048. //note the extra / at the end of doc_path to match every path in the document table that is part of the document path
  1049. $doc_path = Database::escape_string($doc_path);
  1050. $session_id = intval($session_id);
  1051. $condition = "AND id_session IN ('$session_id', '0') ";
  1052. // The " d.filetype='file' " let the user see a file even if the folder is hidden see #2198
  1053. /*
  1054. When using hotpotatoes files, a new html files are generated in the hotpotatoes folder
  1055. to display the test.
  1056. The genuine html file is copied to math4.htm(user_id).t.html
  1057. Images files are not copied, and keep same name.
  1058. To check the html file visibility, we don't have to check file math4.htm(user_id).t.html but file math4.htm
  1059. In this case, we have to remove (user_id).t.html to check the visibility of the file
  1060. For images, we just check the path of the image file.
  1061. Exemple of hotpotatoes folder :
  1062. A.jpg
  1063. maths4-consigne.jpg
  1064. maths4.htm
  1065. maths4.htm1.t.html
  1066. maths4.htm52.t.html
  1067. maths4.htm654.t.html
  1068. omega.jpg
  1069. theta.jpg
  1070. */
  1071. if (strpos($doc_path, 'HotPotatoes_files') && preg_match("/\.t\.html$/", $doc_path)) {
  1072. $doc_path = substr($doc_path, 0, strlen($doc_path) - 7 - strlen(api_get_user_id()));
  1073. }
  1074. if (!in_array($file_type, array('file', 'folder'))) {
  1075. $file_type = 'file';
  1076. }
  1077. $sql = "SELECT visibility FROM $docTable d INNER JOIN $propTable ip
  1078. ON (d.id = ip.ref AND d.c_id = $course_id AND ip.c_id = $course_id)
  1079. WHERE ip.tool = '" . TOOL_DOCUMENT . "' $condition AND
  1080. filetype = '$file_type' AND locate(concat(path,'/'),'" . $doc_path . "/')=1";
  1081. $result = Database::query($sql);
  1082. $is_visible = false;
  1083. if (Database::num_rows($result) > 0) {
  1084. $row = Database::fetch_array($result, 'ASSOC');
  1085. if ($row['visibility'] == 1) {
  1086. $is_visible = $_SESSION['is_allowed_in_course'] || api_is_platform_admin();
  1087. }
  1088. }
  1089. //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.
  1090. //return $_SESSION ['is_allowed_in_course'] || api_is_platform_admin();
  1091. return $is_visible;
  1092. }
  1093. /**
  1094. * Return true if user can see a file
  1095. *
  1096. * @param int document id
  1097. * @param array course info
  1098. * @param array $course the _course array info of the document's course
  1099. * @return bool
  1100. */
  1101. public static function is_visible_by_id($doc_id, $course_info, $session_id, $user_id, $admins_can_see_everything = true) {
  1102. $is_visible = false;
  1103. $user_in_course = false;
  1104. //1. Checking the course array
  1105. if (empty($course_info)) {
  1106. $course_info = api_get_course_info();
  1107. if (empty($course_info)) {
  1108. return false;
  1109. }
  1110. }
  1111. $doc_id = intval($doc_id);
  1112. $session_id = intval($session_id);
  1113. //2. Course and Session visibility are handle in local.inc.php/global.inc.php
  1114. //3. Checking if user exist in course/session
  1115. if ($session_id == 0) {
  1116. if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code']) || api_is_platform_admin()) {
  1117. $user_in_course = true;
  1118. }
  1119. //Check if course is open then we can consider that the student is regitered to the course
  1120. if (isset($course_info) && in_array($course_info['visibility'], array(2, 3))) {
  1121. $user_in_course = true;
  1122. }
  1123. } else {
  1124. $user_status = SessionManager::get_user_status_in_course_session($user_id, $course_info['code'], $session_id);
  1125. if (in_array($user_status, array('0', '2', '6'))) {
  1126. //is true if is an student, course session teacher or coach
  1127. $user_in_course = true;
  1128. }
  1129. }
  1130. //4. Checking document visibility (i'm repeating the code in order to be more clear when reading ) - jm
  1131. if ($user_in_course) {
  1132. //4.1 Checking document visibility for a Course
  1133. if ($session_id == 0) {
  1134. $item_info = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, 0);
  1135. if (isset($item_info['visibility'])) {
  1136. // True for admins if document exists
  1137. if ($admins_can_see_everything && api_is_platform_admin()) {
  1138. return true;
  1139. }
  1140. if ($item_info['visibility'] == 1) {
  1141. return true;
  1142. }
  1143. }
  1144. } else {
  1145. //4.2 Checking document visibility for a Course in a Session
  1146. $item_info = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, 0);
  1147. $item_info_in_session = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, $session_id);
  1148. // True for admins if document exists
  1149. if (isset($item_info['visibility'])) {
  1150. if ($admins_can_see_everything && api_is_platform_admin())
  1151. return true;
  1152. }
  1153. if (isset($item_info_in_session['visibility'])) {
  1154. //if ($doc_id == 85) { var_dump($item_info_in_session);}
  1155. if ($item_info_in_session['visibility'] == 1) {
  1156. return true;
  1157. }
  1158. } else {
  1159. if ($item_info['visibility'] == 1) {
  1160. return true;
  1161. }
  1162. }
  1163. }
  1164. } elseif ($admins_can_see_everything && api_is_platform_admin()) {
  1165. return true;
  1166. }
  1167. return false;
  1168. }
  1169. /**
  1170. * Allow attach a certificate to a course
  1171. * @param string The course id
  1172. * @param int The document id
  1173. * @return void()
  1174. */
  1175. function attach_gradebook_certificate($course_id, $document_id) {
  1176. $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1177. $session_id = api_get_session_id();
  1178. if ($session_id == 0 || is_null($session_id)) {
  1179. $sql_session = 'AND (session_id=' . Database::escape_string($session_id) . ' OR isnull(session_id)) ';
  1180. } elseif ($session_id > 0) {
  1181. $sql_session = 'AND session_id=' . Database::escape_string($session_id);
  1182. } else {
  1183. $sql_session = '';
  1184. }
  1185. $sql = 'UPDATE ' . $tbl_category . ' SET document_id="' . Database::escape_string($document_id) . '"
  1186. WHERE course_code="' . Database::escape_string($course_id) . '" ' . $sql_session;
  1187. Database::query($sql);
  1188. }
  1189. /**
  1190. * get the document id of default certificate
  1191. * @param string The course id
  1192. * @return int The default certificate id
  1193. */
  1194. static function get_default_certificate_id($course_id) {
  1195. $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1196. $session_id = api_get_session_id();
  1197. if ($session_id == 0 || is_null($session_id)) {
  1198. $sql_session = 'AND (session_id=' . Database::escape_string($session_id) . ' OR isnull(session_id)) ';
  1199. } elseif ($session_id > 0) {
  1200. $sql_session = 'AND session_id=' . Database::escape_string($session_id);
  1201. } else {
  1202. $sql_session = '';
  1203. }
  1204. $sql = 'SELECT document_id FROM ' . $tbl_category . ' WHERE course_code="' . Database::escape_string($course_id) . '" ' . $sql_session;
  1205. $rs = Database::query($sql);
  1206. $num = Database::num_rows($rs);
  1207. if ($num == 0) {
  1208. return null;
  1209. }
  1210. $row = Database::fetch_array($rs);
  1211. return $row['document_id'];
  1212. }
  1213. /**
  1214. * allow replace user info in file html
  1215. * @param string The course code
  1216. * @return string The html content of the certificate
  1217. */
  1218. static function replace_user_info_into_html($user_id, $course_code, $is_preview = false) {
  1219. $user_id = intval($user_id);
  1220. $course_info = api_get_course_info($course_code);
  1221. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1222. $course_id = $course_info['real_id'];
  1223. $document_id = self::get_default_certificate_id($course_code);
  1224. if ($document_id) {
  1225. $sql = "SELECT path FROM $tbl_document WHERE c_id = $course_id AND id = $document_id";
  1226. $rs = Database::query($sql);
  1227. $new_content = '';
  1228. $all_user_info = array();
  1229. if (Database::num_rows($rs)) {
  1230. $row = Database::fetch_array($rs);
  1231. $filepath = api_get_path(SYS_COURSE_PATH) . $course_info['path'] . '/document' . $row['path'];
  1232. if (is_file($filepath)) {
  1233. $my_content_html = file_get_contents($filepath);
  1234. }
  1235. $all_user_info = self::get_all_info_to_certificate($user_id, $course_code, $is_preview);
  1236. $info_to_be_replaced_in_content_html = $all_user_info[0];
  1237. $info_to_replace_in_content_html = $all_user_info[1];
  1238. $new_content = str_replace($info_to_be_replaced_in_content_html, $info_to_replace_in_content_html, $my_content_html);
  1239. }
  1240. return array('content' => $new_content, 'variables' => $all_user_info);
  1241. }
  1242. return array();
  1243. }
  1244. /**
  1245. * return all content to replace and all content to be replace
  1246. */
  1247. static function get_all_info_to_certificate($user_id, $course_id, $is_preview = false) {
  1248. $info_list = array();
  1249. $user_id = intval($user_id);
  1250. $course_info = api_get_course_info($course_id);
  1251. //info portal
  1252. $organization_name = api_get_setting('Institution');
  1253. $portal_name = api_get_setting('siteName');
  1254. //Extra user data information
  1255. $extra_user_info_data = UserManager::get_extra_user_data($user_id, false, false, false, true);
  1256. //Student information
  1257. $user_info = api_get_user_info($user_id);
  1258. $first_name = $user_info['firstname'];
  1259. $last_name = $user_info['lastname'];
  1260. $official_code = $user_info['official_code'];
  1261. //Teacher information
  1262. $info_teacher_id = UserManager::get_user_id_of_course_admin_or_session_admin($course_id);
  1263. $teacher_info = api_get_user_info($info_teacher_id);
  1264. $teacher_first_name = $teacher_info['firstname'];
  1265. $teacher_last_name = $teacher_info['lastname'];
  1266. // info gradebook certificate
  1267. $info_grade_certificate = UserManager::get_info_gradebook_certificate($course_id, $user_id);
  1268. $date_certificate = $info_grade_certificate['created_at'];
  1269. $date_long_certificate = '';
  1270. if (!empty($date_certificate)) {
  1271. $date_long_certificate = api_convert_and_format_date($date_certificate);
  1272. $date_no_time = api_convert_and_format_date($date_certificate, DATE_FORMAT_LONG_NO_DAY);
  1273. }
  1274. if ($is_preview) {
  1275. $date_long_certificate = api_convert_and_format_date(api_get_utc_datetime());
  1276. $date_no_time = api_convert_and_format_date(api_get_utc_datetime(), DATE_FORMAT_LONG_NO_DAY);
  1277. }
  1278. $url = api_get_path(WEB_PATH) . 'certificates/index.php?id=' . $info_grade_certificate['id'];
  1279. //replace content
  1280. $info_to_replace_in_content_html = array($first_name,
  1281. $last_name,
  1282. $organization_name,
  1283. $portal_name,
  1284. $teacher_first_name,
  1285. $teacher_last_name,
  1286. $official_code,
  1287. $date_long_certificate,
  1288. $date_no_time,
  1289. $course_id,
  1290. $course_info['name'],
  1291. $info_grade_certificate['grade'],
  1292. $url,
  1293. '<a href="' . $url . '" target="_blank">' . get_lang('CertificateOnlineLink') . '</a>',
  1294. '((certificate_barcode))',
  1295. );
  1296. $info_to_be_replaced_in_content_html = array('((user_firstname))',
  1297. '((user_lastname))',
  1298. '((gradebook_institution))',
  1299. '((gradebook_sitename))',
  1300. '((teacher_firstname))',
  1301. '((teacher_lastname))',
  1302. '((official_code))',
  1303. '((date_certificate))',
  1304. '((date_certificate_no_time))',
  1305. '((course_code))',
  1306. '((course_title))',
  1307. '((gradebook_grade))',
  1308. '((certificate_link))',
  1309. '((certificate_link_html))',
  1310. '((certificate_barcode))',
  1311. );
  1312. if (!empty($extra_user_info_data)) {
  1313. foreach ($extra_user_info_data as $key_extra => $value_extra) {
  1314. $info_to_be_replaced_in_content_html[] = '((' . strtolower($key_extra) . '))';
  1315. $info_to_replace_in_content_html[] = $value_extra;
  1316. }
  1317. }
  1318. $info_list[] = $info_to_be_replaced_in_content_html;
  1319. $info_list[] = $info_to_replace_in_content_html;
  1320. return $info_list;
  1321. }
  1322. /**
  1323. * Remove default certificate
  1324. * @param string The course id
  1325. * @param int The document id of the default certificate
  1326. * @return void()
  1327. */
  1328. function remove_attach_certificate($course_id, $default_certificate_id) {
  1329. $default_certificate = self::get_default_certificate_id($course_id);
  1330. if ((int) $default_certificate == (int) $default_certificate_id) {
  1331. $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1332. $session_id = api_get_session_id();
  1333. if ($session_id == 0 || is_null($session_id)) {
  1334. $sql_session = 'AND (session_id=' . Database::escape_string($session_id) . ' OR isnull(session_id)) ';
  1335. } elseif ($session_id > 0) {
  1336. $sql_session = 'AND session_id=' . Database::escape_string($session_id);
  1337. } else {
  1338. $sql_session = '';
  1339. }
  1340. $sql = 'UPDATE ' . $tbl_category . ' SET document_id=null
  1341. WHERE course_code="' . Database::escape_string($course_id) . '" AND document_id="' . $default_certificate_id . '" ' . $sql_session;
  1342. Database::query($sql);
  1343. }
  1344. }
  1345. /**
  1346. * Create directory certificate
  1347. * @param string The course id
  1348. * @return void()
  1349. */
  1350. static function create_directory_certificate_in_course($course_id) {
  1351. $course_info = api_get_course_info($course_id);
  1352. if (!empty($course_info)) {
  1353. $to_group_id = 0;
  1354. $to_user_id = null;
  1355. $course_dir = $course_info['path'] . "/document/";
  1356. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1357. $base_work_dir = $sys_course_path . $course_dir;
  1358. $base_work_dir_test = $base_work_dir . 'certificates';
  1359. $dir_name = '/certificates';
  1360. $post_dir_name = get_lang('CertificatesFiles');
  1361. $visibility_command = 'invisible';
  1362. if (!is_dir($base_work_dir_test)) {
  1363. $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);
  1364. $update_id = self::get_document_id_of_directory_certificate();
  1365. api_item_property_update($course_info, TOOL_DOCUMENT, $update_id, $visibility_command, api_get_user_id());
  1366. }
  1367. }
  1368. }
  1369. /**
  1370. * Get the document id of the directory certificate
  1371. * @param string The course id
  1372. * @return int The document id of the directory certificate
  1373. */
  1374. static function get_document_id_of_directory_certificate() {
  1375. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1376. $course_id = api_get_course_int_id();
  1377. $sql = "SELECT id FROM $tbl_document WHERE c_id = $course_id AND path='/certificates' ";
  1378. $rs = Database::query($sql);
  1379. $row = Database::fetch_array($rs);
  1380. return $row['id'];
  1381. }
  1382. /**
  1383. * Check if a directory given is for certificate
  1384. * @param string path of directory
  1385. * @return bool true if is a certificate or false otherwise
  1386. */
  1387. static function is_certificate_mode($dir) {
  1388. //I'm in the certification module?
  1389. $is_certificate_mode = false;
  1390. $is_certificate_array = explode('/', $dir);
  1391. array_shift($is_certificate_array);
  1392. if (isset($is_certificate_array[0]) && $is_certificate_array[0] == 'certificates') {
  1393. $is_certificate_mode = true;
  1394. }
  1395. return $is_certificate_mode;
  1396. }
  1397. /**
  1398. * Gets the list of included resources as a list of absolute or relative paths from a html file or string html
  1399. * This allows for a better SCORM export or replace urls inside content html from copy course
  1400. * The list will generally include pictures, flash objects, java applets, or any other
  1401. * stuff included in the source of the current item. The current item is expected
  1402. * to be an HTML file or string html. If it is not, then the function will return and empty list.
  1403. * @param string source html (content or path)
  1404. * @param bool is file or string html
  1405. * @param string type (one of the Dokeos tools) - optional (otherwise takes the current item's type)
  1406. * @param int level of recursivity we're in
  1407. * @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
  1408. */
  1409. static function get_resources_from_source_html($source_html, $is_file = false, $type = null, $recursivity = 1) {
  1410. $max = 5;
  1411. $attributes = array();
  1412. $wanted_attributes = array('src', 'url', '@import', 'href', 'value', 'flashvars');
  1413. $abs_path = '';
  1414. if ($recursivity > $max) {
  1415. return array();
  1416. }
  1417. if (!isset($type)) {
  1418. $type = TOOL_DOCUMENT;
  1419. }
  1420. if (!$is_file) {
  1421. $attributes = self::parse_HTML_attributes($source_html, $wanted_attributes);
  1422. } else {
  1423. if (is_file($source_html)) {
  1424. $abs_path = $source_html;
  1425. //for now, read the whole file in one go (that's gonna be a problem when the file is too big)
  1426. $info = pathinfo($abs_path);
  1427. $ext = $info['extension'];
  1428. switch (strtolower($ext)) {
  1429. case 'html' :
  1430. case 'htm' :
  1431. case 'shtml':
  1432. case 'css' :
  1433. $file_content = file_get_contents($abs_path);
  1434. //get an array of attributes from the HTML source
  1435. $attributes = self::parse_HTML_attributes($file_content, $wanted_attributes);
  1436. break;
  1437. default :
  1438. break;
  1439. }
  1440. } else {
  1441. return false;
  1442. }
  1443. }
  1444. switch ($type) {
  1445. case TOOL_DOCUMENT :
  1446. case TOOL_QUIZ:
  1447. case 'sco':
  1448. foreach ($wanted_attributes as $attr) {
  1449. if (isset($attributes[$attr])) {
  1450. //find which kind of path these are (local or remote)
  1451. $sources = $attributes[$attr];
  1452. foreach ($sources as $source) {
  1453. //skip what is obviously not a resource
  1454. if (strpos($source, '+this.'))
  1455. continue; //javascript code - will still work unaltered
  1456. if (strpos($source, '.') === false)
  1457. continue; //no dot, should not be an external file anyway
  1458. if (strpos($source, 'mailto:'))
  1459. continue; //mailto link
  1460. if (strpos($source, ';') && !strpos($source, '&amp;'))
  1461. continue; //avoid code - that should help
  1462. if ($attr == 'value') {
  1463. if (strpos($source, 'mp3file')) {
  1464. $files_list[] = array(substr($source, 0, strpos($source, '.swf') + 4), 'local', 'abs');
  1465. $mp3file = substr($source, strpos($source, 'mp3file=') + 8);
  1466. if (substr($mp3file, 0, 1) == '/') {
  1467. $files_list[] = array($mp3file, 'local', 'abs');
  1468. } else {
  1469. $files_list[] = array($mp3file, 'local', 'rel');
  1470. }
  1471. } elseif (strpos($source, 'flv=') === 0) {
  1472. $source = substr($source, 4);
  1473. if (strpos($source, '&') > 0) {
  1474. $source = substr($source, 0, strpos($source, '&'));
  1475. }
  1476. if (strpos($source, '://') > 0) {
  1477. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  1478. //we found the current portal url
  1479. $files_list[] = array($source, 'local', 'url');
  1480. } else {
  1481. //we didn't find any trace of current portal
  1482. $files_list[] = array($source, 'remote', 'url');
  1483. }
  1484. } else {
  1485. $files_list[] = array($source, 'local', 'abs');
  1486. }
  1487. continue; //skipping anything else to avoid two entries (while the others can have sub-files in their url, flv's can't)
  1488. }
  1489. }
  1490. if (strpos($source, '://') > 0) {
  1491. //cut at '?' in a URL with params
  1492. if (strpos($source, '?') > 0) {
  1493. $second_part = substr($source, strpos($source, '?'));
  1494. if (strpos($second_part, '://') > 0) {
  1495. //if the second part of the url contains a url too, treat the second one before cutting
  1496. $pos1 = strpos($second_part, '=');
  1497. $pos2 = strpos($second_part, '&');
  1498. $second_part = substr($second_part, $pos1 + 1, $pos2 - ($pos1 + 1));
  1499. if (strpos($second_part, api_get_path(WEB_PATH)) !== false) {
  1500. //we found the current portal url
  1501. $files_list[] = array($second_part, 'local', 'url');
  1502. $in_files_list[] = self::get_resources_from_source_html($second_part, true, TOOL_DOCUMENT, $recursivity + 1);
  1503. if (count($in_files_list) > 0) {
  1504. $files_list = array_merge($files_list, $in_files_list);
  1505. }
  1506. } else {
  1507. //we didn't find any trace of current portal
  1508. $files_list[] = array($second_part, 'remote', 'url');
  1509. }
  1510. } elseif (strpos($second_part, '=') > 0) {
  1511. if (substr($second_part, 0, 1) === '/') {
  1512. //link starts with a /, making it absolute (relative to DocumentRoot)
  1513. $files_list[] = array($second_part, 'local', 'abs');
  1514. $in_files_list[] = self::get_resources_from_source_html($second_part, true, TOOL_DOCUMENT, $recursivity + 1);
  1515. if (count($in_files_list) > 0) {
  1516. $files_list = array_merge($files_list, $in_files_list);
  1517. }
  1518. } elseif (strstr($second_part, '..') === 0) {
  1519. //link is relative but going back in the hierarchy
  1520. $files_list[] = array($second_part, 'local', 'rel');
  1521. //$dir = api_get_path(SYS_CODE_PATH);//dirname($abs_path);
  1522. //$new_abs_path = realpath($dir.'/'.$second_part);
  1523. $dir = '';
  1524. if (!empty($abs_path)) {
  1525. $dir = dirname($abs_path) . '/';
  1526. }
  1527. $new_abs_path = realpath($dir . $second_part);
  1528. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1529. if (count($in_files_list) > 0) {
  1530. $files_list = array_merge($files_list, $in_files_list);
  1531. }
  1532. } else {
  1533. //no starting '/', making it relative to current document's path
  1534. if (substr($second_part, 0, 2) == './') {
  1535. $second_part = substr($second_part, 2);
  1536. }
  1537. $files_list[] = array($second_part, 'local', 'rel');
  1538. $dir = '';
  1539. if (!empty($abs_path)) {
  1540. $dir = dirname($abs_path) . '/';
  1541. }
  1542. $new_abs_path = realpath($dir . $second_part);
  1543. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1544. if (count($in_files_list) > 0) {
  1545. $files_list = array_merge($files_list, $in_files_list);
  1546. }
  1547. }
  1548. }
  1549. //leave that second part behind now
  1550. $source = substr($source, 0, strpos($source, '?'));
  1551. if (strpos($source, '://') > 0) {
  1552. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  1553. //we found the current portal url
  1554. $files_list[] = array($source, 'local', 'url');
  1555. $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
  1556. if (count($in_files_list) > 0) {
  1557. $files_list = array_merge($files_list, $in_files_list);
  1558. }
  1559. } else {
  1560. //we didn't find any trace of current portal
  1561. $files_list[] = array($source, 'remote', 'url');
  1562. }
  1563. } else {
  1564. //no protocol found, make link local
  1565. if (substr($source, 0, 1) === '/') {
  1566. //link starts with a /, making it absolute (relative to DocumentRoot)
  1567. $files_list[] = array($source, 'local', 'abs');
  1568. $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
  1569. if (count($in_files_list) > 0) {
  1570. $files_list = array_merge($files_list, $in_files_list);
  1571. }
  1572. } elseif (strstr($source, '..') === 0) { //link is relative but going back in the hierarchy
  1573. $files_list[] = array($source, 'local', 'rel');
  1574. $dir = '';
  1575. if (!empty($abs_path)) {
  1576. $dir = dirname($abs_path) . '/';
  1577. }
  1578. $new_abs_path = realpath($dir . $source);
  1579. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1580. if (count($in_files_list) > 0) {
  1581. $files_list = array_merge($files_list, $in_files_list);
  1582. }
  1583. } else {
  1584. //no starting '/', making it relative to current document's path
  1585. if (substr($source, 0, 2) == './') {
  1586. $source = substr($source, 2);
  1587. }
  1588. $files_list[] = array($source, 'local', 'rel');
  1589. $dir = '';
  1590. if (!empty($abs_path)) {
  1591. $dir = dirname($abs_path) . '/';
  1592. }
  1593. $new_abs_path = realpath($dir . $source);
  1594. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1595. if (count($in_files_list) > 0) {
  1596. $files_list = array_merge($files_list, $in_files_list);
  1597. }
  1598. }
  1599. }
  1600. }
  1601. //found some protocol there
  1602. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  1603. //we found the current portal url
  1604. $files_list[] = array($source, 'local', 'url');
  1605. $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
  1606. if (count($in_files_list) > 0) {
  1607. $files_list = array_merge($files_list, $in_files_list);
  1608. }
  1609. } else {
  1610. //we didn't find any trace of current portal
  1611. $files_list[] = array($source, 'remote', 'url');
  1612. }
  1613. } else {
  1614. //no protocol found, make link local
  1615. if (substr($source, 0, 1) === '/') {
  1616. //link starts with a /, making it absolute (relative to DocumentRoot)
  1617. $files_list[] = array($source, 'local', 'abs');
  1618. $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
  1619. if (count($in_files_list) > 0) {
  1620. $files_list = array_merge($files_list, $in_files_list);
  1621. }
  1622. } elseif (strpos($source, '..') === 0) {
  1623. //link is relative but going back in the hierarchy
  1624. $files_list[] = array($source, 'local', 'rel');
  1625. $dir = '';
  1626. if (!empty($abs_path)) {
  1627. $dir = dirname($abs_path) . '/';
  1628. }
  1629. $new_abs_path = realpath($dir . $source);
  1630. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1631. if (count($in_files_list) > 0) {
  1632. $files_list = array_merge($files_list, $in_files_list);
  1633. }
  1634. } else {
  1635. //no starting '/', making it relative to current document's path
  1636. if (substr($source, 0, 2) == './') {
  1637. $source = substr($source, 2);
  1638. }
  1639. $files_list[] = array($source, 'local', 'rel');
  1640. $dir = '';
  1641. if (!empty($abs_path)) {
  1642. $dir = dirname($abs_path) . '/';
  1643. }
  1644. $new_abs_path = realpath($dir . $source);
  1645. $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
  1646. if (count($in_files_list) > 0) {
  1647. $files_list = array_merge($files_list, $in_files_list);
  1648. }
  1649. }
  1650. }
  1651. }
  1652. }
  1653. }
  1654. break;
  1655. default: //ignore
  1656. break;
  1657. }
  1658. $checked_files_list = array();
  1659. $checked_array_list = array();
  1660. if (count($files_list) > 0) {
  1661. foreach ($files_list as $idx => $file) {
  1662. if (!empty($file[0])) {
  1663. if (!in_array($file[0], $checked_files_list)) {
  1664. $checked_files_list[] = $files_list[$idx][0];
  1665. $checked_array_list[] = $files_list[$idx];
  1666. }
  1667. }
  1668. }
  1669. }
  1670. return $checked_array_list;
  1671. }
  1672. /**
  1673. * Parses the HTML attributes given as string.
  1674. *
  1675. * @param string HTML attribute string
  1676. * @param array List of attributes that we want to get back
  1677. * @return array An associative array of attributes
  1678. * @author Based on a function from the HTML_Common2 PEAR module
  1679. */
  1680. static function parse_HTML_attributes($attrString, $wanted = array()) {
  1681. $attributes = array();
  1682. $regs = array();
  1683. $reduced = false;
  1684. if (count($wanted) > 0) {
  1685. $reduced = true;
  1686. }
  1687. try {
  1688. //Find all occurences of something that looks like a URL
  1689. // The structure of this regexp is:
  1690. // (find protocol) then
  1691. // (optionally find some kind of space 1 or more times) then
  1692. // find (either an equal sign or a bracket) followed by an optional space
  1693. // followed by some text without quotes (between quotes itself or not)
  1694. // then possible closing brackets if we were in the opening bracket case
  1695. // OR something like @import()
  1696. $res = preg_match_all(
  1697. '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]*))' .
  1698. // '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]|[^\x00-\x7F])*)' . -> seems to be taking too much
  1699. // '/(((([A-Za-z_:])([^\x00-\x7F])*)' . -> takes only last letter of parameter name
  1700. '([ \n\t\r]+)?(' .
  1701. // '(=([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+))' . -> doesn't restrict close enough to the url itself
  1702. '(=([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+))' .
  1703. '|' .
  1704. // '(\(([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)\))' . -> doesn't restrict close enough to the url itself
  1705. '(\(([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+)\))' .
  1706. '))' .
  1707. '|' .
  1708. // '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))?/', -> takes a lot (like 100's of thousands of empty possibilities)
  1709. '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/', $attrString, $regs
  1710. );
  1711. } catch (Exception $e) {
  1712. error_log('Caught exception: ' . $e->getMessage(), 0);
  1713. }
  1714. if ($res) {
  1715. for ($i = 0; $i < count($regs[1]); $i++) {
  1716. $name = trim($regs[3][$i]);
  1717. $check = trim($regs[0][$i]);
  1718. $value = trim($regs[10][$i]);
  1719. if (empty($value) and !empty($regs[13][$i])) {
  1720. $value = $regs[13][$i];
  1721. }
  1722. if (empty($name) && !empty($regs[16][$i])) {
  1723. $name = '@import';
  1724. $value = trim($regs[16][$i]);
  1725. }
  1726. if (!empty($name)) {
  1727. if (!$reduced OR in_array(strtolower($name), $wanted)) {
  1728. if ($name == $check) {
  1729. $attributes[strtolower($name)][] = strtolower($name);
  1730. } else {
  1731. if (!empty($value) && ($value[0] == '\'' || $value[0] == '"')) {
  1732. $value = substr($value, 1, -1);
  1733. }
  1734. if ($value == 'API.LMSGetValue(name') {
  1735. $value = 'API.LMSGetValue(name)';
  1736. }
  1737. $attributes[strtolower($name)][] = $value;
  1738. }
  1739. }
  1740. }
  1741. }
  1742. } else {
  1743. //error_log('preg_match did not find anything', 0);
  1744. }
  1745. return $attributes;
  1746. }
  1747. /**
  1748. * Replace urls inside content html from a copy course
  1749. * @param string content html
  1750. * @param string origin course code
  1751. * @param string destination course directory
  1752. * @return string new content html with replaced urls or return false if content is not a string
  1753. */
  1754. static function replace_urls_inside_content_html_from_copy_course($content_html, $origin_course_code, $destination_course_directory) {
  1755. require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
  1756. if (!is_string($content_html)) {
  1757. return false;
  1758. }
  1759. $orig_source_html = DocumentManager::get_resources_from_source_html($content_html);
  1760. $orig_course_info = api_get_course_info($origin_course_code);
  1761. $orig_course_path = api_get_path(SYS_PATH) . 'courses/' . $orig_course_info['path'] . '/';
  1762. $destination_course_code = CourseManager::get_course_id_from_path($destination_course_directory);
  1763. $destination_course_info = api_get_course_info($destination_course_code);
  1764. $dest_course_path = api_get_path(SYS_COURSE_PATH) . $destination_course_directory . '/';
  1765. $user_id = api_get_user_id();
  1766. if (!empty($orig_source_html)) {
  1767. foreach ($orig_source_html as $source) {
  1768. // get information about source url
  1769. $real_orig_url = $source[0]; // url
  1770. $scope_url = $source[1]; // scope (local, remote)
  1771. $type_url = $source[2]; // tyle (rel, abs, url)
  1772. // Get path and query from origin url
  1773. $orig_parse_url = parse_url($real_orig_url);
  1774. $real_orig_path = $orig_parse_url['path'];
  1775. $real_orig_query = $orig_parse_url['query'];
  1776. // Replace origin course code by destination course code from origin url query
  1777. $dest_url_query = '';
  1778. if (!empty($real_orig_query)) {
  1779. $dest_url_query = '?' . $real_orig_query;
  1780. if (strpos($dest_url_query, $origin_course_code) !== false) {
  1781. $dest_url_query = str_replace($origin_course_code, $destination_course_code, $dest_url_query);
  1782. }
  1783. }
  1784. if ($scope_url == 'local') {
  1785. if ($type_url == 'abs' || $type_url == 'rel') {
  1786. $document_file = strstr($real_orig_path, 'document');
  1787. if (strpos($real_orig_path, $document_file) !== false) {
  1788. $origin_filepath = $orig_course_path . $document_file;
  1789. $destination_filepath = $dest_course_path . $document_file;
  1790. // copy origin file inside destination course
  1791. if (file_exists($origin_filepath)) {
  1792. $filepath_dir = dirname($destination_filepath);
  1793. if (!is_dir($filepath_dir)) {
  1794. $perm = api_get_permissions_for_new_directories();
  1795. $result = @mkdir($filepath_dir, $perm, true);
  1796. if ($result) {
  1797. $filepath_to_add = str_replace(array($dest_course_path, 'document'), '', $filepath_dir);
  1798. //Add to item properties to the new folder
  1799. $doc_id = add_document($destination_course_info, $filepath_to_add, 'folder', 0, basename($filepath_to_add));
  1800. api_item_property_update($destination_course_info, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $user_id, null, null, null, null);
  1801. }
  1802. }
  1803. if (!file_exists($destination_filepath)) {
  1804. $result = @copy($origin_filepath, $destination_filepath);
  1805. if ($result) {
  1806. $filepath_to_add = str_replace(array($dest_course_path, 'document'), '', $destination_filepath);
  1807. $size = filesize($destination_filepath);
  1808. //Add to item properties to the file
  1809. $doc_id = add_document($destination_course_info, $filepath_to_add, 'file', $size, basename($filepath_to_add));
  1810. api_item_property_update($destination_course_info, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $user_id, null, null, null, null);
  1811. }
  1812. }
  1813. }
  1814. // Replace origin course path by destination course path
  1815. if (strpos($content_html, $real_orig_url) !== false) {
  1816. //$origin_course_code
  1817. $url_course_path = str_replace($orig_course_info['path'] . '/' . $document_file, '', $real_orig_path);
  1818. $destination_url = $url_course_path . $destination_course_directory . '/' . $document_file . $dest_url_query;
  1819. //If the course code doesn't exist in the path? what we do? Nothing! see BT#1985
  1820. if (strpos($real_orig_path, $origin_course_code) === false) {
  1821. $url_course_path = $real_orig_path;
  1822. $destination_url = $real_orig_path;
  1823. }
  1824. $content_html = str_replace($real_orig_url, $destination_url, $content_html);
  1825. }
  1826. }
  1827. // replace origin course code by destination course code from origin url
  1828. if (strpos($real_orig_url, '?') === 0) {
  1829. $dest_url = str_replace($origin_course_code, $destination_course_code, $real_orig_url);
  1830. $content_html = str_replace($real_orig_url, $dest_url, $content_html);
  1831. }
  1832. } else {
  1833. if ($type_url == 'url') {
  1834. }
  1835. }
  1836. }
  1837. }
  1838. }
  1839. return $content_html;
  1840. }
  1841. /**
  1842. * Replace urls inside content html when moving a file
  1843. * @todo this code is only called in document.php but is commented
  1844. * @param string content html
  1845. * @param string origin
  1846. * @param string destination
  1847. * @return string new content html with replaced urls or return false if content is not a string
  1848. */
  1849. function replace_urls_inside_content_html_when_moving_file($file_name, $original_path, $destiny_path) {
  1850. if (substr($original_path, strlen($original_path) - 1, strlen($original_path)) == '/') {
  1851. $original = $original_path . $file_name;
  1852. } else {
  1853. $original = $original_path . '/' . $file_name;
  1854. }
  1855. if (substr($destiny_path, strlen($destiny_path) - 1, strlen($destiny_path)) == '/') {
  1856. $destination = $destiny_path . $file_name;
  1857. } else {
  1858. $destination = $destiny_path . '/' . $file_name;
  1859. }
  1860. //var_dump("From $original ", "to $destination");
  1861. $original_count = count(explode('/', $original));
  1862. $destination_count = count(explode('/', $destination));
  1863. if ($original_count == $destination_count) {
  1864. //Nothing to change
  1865. return true;
  1866. }
  1867. $mode = '';
  1868. if ($original_count > $destination_count) {
  1869. $mode = 'outside';
  1870. } else {
  1871. $mode = 'inside';
  1872. }
  1873. //echo $original_count.' '.$destination_count; var_dump($mode);
  1874. //We do not select the $original_path becayse the file was already moved
  1875. $content_html = file_get_contents($destiny_path . '/' . $file_name);
  1876. $destination_file = $destiny_path . '/' . $file_name;
  1877. $pre_original = strstr($original_path, 'document');
  1878. $pre_destin = strstr($destiny_path, 'document');
  1879. //var_dump ("pre_original $pre_original");
  1880. //var_dump ("pre_destin $pre_destin");
  1881. $pre_original = substr($pre_original, 8, strlen($pre_original));
  1882. $pre_destin = substr($pre_destin, 8, strlen($pre_destin));
  1883. //var_dump ("pre_original $pre_original");
  1884. //var_dump ("pre_destin $pre_destin");
  1885. $levels = count(explode('/', $pre_destin)) - 1;
  1886. $link_to_add = '';
  1887. for ($i = 1; $i <= $levels; $i++) {
  1888. $link_to_add .= '../';
  1889. }
  1890. if ($pre_original == '/') {
  1891. $pre_original = '';
  1892. }
  1893. if ($pre_destin == '/') {
  1894. $pre_destin = '';
  1895. }
  1896. if ($pre_original != '') {
  1897. $pre_original = '..' . $pre_original . '/';
  1898. }
  1899. if ($pre_destin != '') {
  1900. $pre_destin = '..' . $pre_destin . '/';
  1901. }
  1902. //var_dump($pre_original);
  1903. $levels = explode('/', $pre_original);
  1904. //var_dump($levels);
  1905. $count_pre_destination_levels = 0;
  1906. foreach ($levels as $item) {
  1907. if (!empty($item) && $item != '..') {
  1908. $count_pre_destination_levels++;
  1909. }
  1910. }
  1911. $count_pre_destination_levels--;
  1912. //$count_pre_destination_levels = count() - 3;
  1913. if ($count_pre_destination_levels == 0) {
  1914. $count_pre_destination_levels = 1;
  1915. }
  1916. //echo '$count_pre_destination_levels '. $count_pre_destination_levels;
  1917. $pre_remove = '';
  1918. for ($i = 1; $i <= $count_pre_destination_levels; $i++) {
  1919. $pre_remove .='..\/';
  1920. }
  1921. //var_dump(' link to add '.$link_to_add.' -- remove '.$pre_remove);
  1922. $orig_source_html = DocumentManager::get_resources_from_source_html($content_html);
  1923. //var_dump($orig_source_html);
  1924. foreach ($orig_source_html as $source) {
  1925. // get information about source url
  1926. $real_orig_url = $source[0]; // url
  1927. $scope_url = $source[1]; // scope (local, remote)
  1928. $type_url = $source[2]; // tyle (rel, abs, url)
  1929. // Get path and query from origin url
  1930. $orig_parse_url = parse_url($real_orig_url);
  1931. $real_orig_path = $orig_parse_url['path'];
  1932. $real_orig_query = $orig_parse_url['query'];
  1933. // Replace origin course code by destination course code from origin url query
  1934. /*
  1935. $dest_url_query = '';
  1936. if (!empty($real_orig_query)) {
  1937. $dest_url_query = '?'.$real_orig_query;
  1938. if (strpos($dest_url_query,$origin_course_code) !== false) {
  1939. $dest_url_query = str_replace($origin_course_code, $destination_course_code, $dest_url_query);
  1940. }
  1941. } */
  1942. if ($scope_url == 'local') {
  1943. if ($type_url == 'abs' || $type_url == 'rel') {
  1944. $document_file = strstr($real_orig_path, 'document');
  1945. if (strpos($real_orig_path, $document_file) !== false) {
  1946. echo 'continue1';
  1947. continue;
  1948. } else {
  1949. $real_orig_url_temp = '';
  1950. if ($mode == 'inside') {
  1951. $real_orig_url_temp = str_replace('../', '', $real_orig_url);
  1952. $destination_url = $link_to_add . $real_orig_url_temp;
  1953. } else {
  1954. $real_orig_url_temp = $real_orig_url;
  1955. $destination_url = preg_replace("/" . $pre_remove . "/", '', $real_orig_url, 1);
  1956. }
  1957. if ($real_orig_url == $destination_url) {
  1958. echo 'continue2';
  1959. continue;
  1960. }
  1961. var_dump($real_orig_url_temp . ' - ' . $destination_url);
  1962. $content_html = str_replace($real_orig_url, $destination_url, $content_html);
  1963. }
  1964. } else {
  1965. echo 'continue3';
  1966. continue;
  1967. }
  1968. }
  1969. }
  1970. $return = file_put_contents($destination, $content_html);
  1971. return $return;
  1972. }
  1973. public static function export_to_pdf($document_id, $course_code) {
  1974. require_once api_get_path(LIBRARY_PATH) . 'pdf.lib.php';
  1975. $course_data = api_get_course_info($course_code);
  1976. $document_data = self::get_document_data_by_id($document_id, $course_code);
  1977. $file_path = api_get_path(SYS_COURSE_PATH) . $course_data['path'] . '/document' . $document_data['path'];
  1978. $pdf = new PDF();
  1979. $pdf->html_to_pdf($file_path, $document_data['title'], $course_code);
  1980. }
  1981. /**
  1982. * Uploads a document
  1983. *
  1984. * @param array the $_FILES variable
  1985. * @param string $path
  1986. * @param string title
  1987. * @param string comment
  1988. * @param int unzip or not the file
  1989. * @param int if_exists overwrite, rename or warn if exists (default)
  1990. * @param bool index document (search xapian module)
  1991. * @param bool print html messages
  1992. * @return unknown_type
  1993. */
  1994. public static function upload_document($files, $path, $title = null, $comment = null, $unzip = 0, $if_exists = null, $index_document = false, $show_output = false) {
  1995. require_once api_get_path(LIBRARY_PATH) . 'fileUpload.lib.php';
  1996. $course_info = api_get_course_info();
  1997. $course_dir = $course_info['path'] . '/document';
  1998. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1999. $base_work_dir = $sys_course_path . $course_dir;
  2000. if (isset($files['file'])) {
  2001. $upload_ok = process_uploaded_file($files['file'], $show_output);
  2002. if ($upload_ok) {
  2003. // File got on the server without problems, now process it
  2004. $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);
  2005. if ($new_path) {
  2006. $docid = DocumentManager::get_document_id($course_info, $new_path);
  2007. if (!empty($docid)) {
  2008. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  2009. $params = array();
  2010. if (!empty($title)) {
  2011. $params['title'] = get_document_title($title);
  2012. } else {
  2013. if (isset($if_exists) && $if_exists == 'rename') {
  2014. $new_path = basename($new_path);
  2015. $params['title'] = get_document_title($new_path);
  2016. } else {
  2017. $params['title'] = get_document_title($files['file']['name']);
  2018. }
  2019. }
  2020. if (!empty($comment)) {
  2021. $params['comment'] = trim($comment);
  2022. }
  2023. Database::update($table_document, $params, array('id = ? AND c_id = ? ' => array($docid, $course_info['real_id'])));
  2024. }
  2025. // Showing message when sending zip files
  2026. if ($new_path === true && $unzip == 1 && $show_output) {
  2027. Display::display_confirmation_message(get_lang('UplUploadSucceeded') . '<br />', false);
  2028. }
  2029. /* // Check for missing images in html files
  2030. $missing_files = check_for_missing_files($base_work_dir.$new_path);
  2031. if ($missing_files && $show_output) {
  2032. // Show a form to upload the missing files
  2033. Display::display_normal_message(build_missing_files_form($missing_files, $path, $files['file']['name']), false);
  2034. } */
  2035. if ($index_document) {
  2036. self::index_document($docid, $course_info['code'], null, $_POST['language'], $_REQUEST, $if_exists);
  2037. }
  2038. if (!empty($docid) && is_numeric($docid)) {
  2039. $document_data = self::get_document_data_by_id($docid, $course_info['code']);
  2040. return $document_data;
  2041. }
  2042. }
  2043. }
  2044. }
  2045. return false;
  2046. }
  2047. /**
  2048. * Obtains the text inside the file with the right parser
  2049. */
  2050. function get_text_content($doc_path, $doc_mime) {
  2051. // TODO: review w$ compatibility
  2052. // Use usual exec output lines array to store stdout instead of a temp file
  2053. // because we need to store it at RAM anyway before index on ChamiloIndexer object
  2054. $ret_val = null;
  2055. switch ($doc_mime) {
  2056. case 'text/plain':
  2057. $handle = fopen($doc_path, 'r');
  2058. $output = array(fread($handle, filesize($doc_path)));
  2059. fclose($handle);
  2060. break;
  2061. case 'application/pdf':
  2062. exec("pdftotext $doc_path -", $output, $ret_val);
  2063. break;
  2064. case 'application/postscript':
  2065. $temp_file = tempnam(sys_get_temp_dir(), 'chamilo');
  2066. exec("ps2pdf $doc_path $temp_file", $output, $ret_val);
  2067. if ($ret_val !== 0) { // shell fail, probably 127 (command not found)
  2068. return false;
  2069. }
  2070. exec("pdftotext $temp_file -", $output, $ret_val);
  2071. unlink($temp_file);
  2072. break;
  2073. case 'application/msword':
  2074. exec("catdoc $doc_path", $output, $ret_val);
  2075. break;
  2076. case 'text/html':
  2077. exec("html2text $doc_path", $output, $ret_val);
  2078. break;
  2079. case 'text/rtf':
  2080. // Note: correct handling of code pages in unrtf
  2081. // on debian lenny unrtf v0.19.2 can not, but unrtf v0.20.5 can
  2082. exec("unrtf --text $doc_path", $output, $ret_val);
  2083. if ($ret_val == 127) { // command not found
  2084. return false;
  2085. }
  2086. // Avoid index unrtf comments
  2087. if (is_array($output) && count($output) > 1) {
  2088. $parsed_output = array();
  2089. foreach ($output as & $line) {
  2090. if (!preg_match('/^###/', $line, $matches)) {
  2091. if (!empty($line)) {
  2092. $parsed_output[] = $line;
  2093. }
  2094. }
  2095. }
  2096. $output = $parsed_output;
  2097. }
  2098. break;
  2099. case 'application/vnd.ms-powerpoint':
  2100. exec("catppt $doc_path", $output, $ret_val);
  2101. break;
  2102. case 'application/vnd.ms-excel':
  2103. exec("xls2csv -c\" \" $doc_path", $output, $ret_val);
  2104. break;
  2105. }
  2106. $content = '';
  2107. if (!is_null($ret_val)) {
  2108. if ($ret_val !== 0) { // shell fail, probably 127 (command not found)
  2109. return false;
  2110. }
  2111. }
  2112. if (isset($output)) {
  2113. foreach ($output as & $line) {
  2114. $content .= $line . "\n";
  2115. }
  2116. return $content;
  2117. } else {
  2118. return false;
  2119. }
  2120. }
  2121. /**
  2122. * Calculates the total size of all documents in a course
  2123. *
  2124. * @author Bert vanderkimpen
  2125. * @param int $to_group_id (to calculate group document space)
  2126. * @return int total size
  2127. */
  2128. static function documents_total_space($course_id = null, $group_id = null, $session_id = null) {
  2129. $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2130. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  2131. if (isset($course_id)) {
  2132. $course_id = intval($course_id);
  2133. } else {
  2134. $course_id = api_get_course_int_id();
  2135. }
  2136. $group_condition = null;
  2137. if (isset($group_id)) {
  2138. $group_id = intval($group_id);
  2139. $group_condition = " AND props.to_group_id='" . $group_id . "' ";
  2140. }
  2141. $session_condition = null;
  2142. if (isset($session_id)) {
  2143. $session_id = intval($session_id);
  2144. $session_condition = " AND props.id_session='" . $session_id . "' ";
  2145. }
  2146. $sql = "SELECT SUM(size) FROM " . $TABLE_ITEMPROPERTY . " AS props, " . $TABLE_DOCUMENT . " AS docs
  2147. WHERE props.c_id = $course_id AND
  2148. docs.c_id = $course_id AND
  2149. docs.id = props.ref AND
  2150. props.tool = '" . TOOL_DOCUMENT . "' AND
  2151. props.visibility <> 2
  2152. $group_condition
  2153. $session_condition
  2154. ";
  2155. $result = Database::query($sql);
  2156. if ($result && Database::num_rows($result) != 0) {
  2157. $row = Database::fetch_row($result);
  2158. return $row[0];
  2159. } else {
  2160. return 0;
  2161. }
  2162. }
  2163. /**
  2164. * Here we count 1 kilobyte = 1000 byte, 12 megabyte = 1000 kilobyte.
  2165. */
  2166. static function display_quota($course_quota, $already_consumed_space) {
  2167. $course_quota_m = round($course_quota / 1000000);
  2168. $already_consumed_space_m = round($already_consumed_space / 1000000);
  2169. $message = get_lang('MaximumAllowedQuota') . ' <strong>' . $course_quota_m . ' megabyte</strong>.<br />';
  2170. $message .= get_lang('CourseCurrentlyUses') . ' <strong>' . $already_consumed_space_m . ' megabyte</strong>.<br />';
  2171. $percentage = round(($already_consumed_space / $course_quota * 100), 1);
  2172. $other_percentage = $percentage < 100 ? 100 - $percentage : 0;
  2173. // Decide where to place percentage in graph
  2174. if ($percentage >= 50) {
  2175. $text_in_filled = '&nbsp;' . $other_percentage . '%';
  2176. $text_in_unfilled = '';
  2177. } else {
  2178. $text_in_unfilled = '&nbsp;' . $other_percentage . '%';
  2179. $text_in_filled = '';
  2180. }
  2181. // Decide the background colour of the graph
  2182. if ($percentage < 65) {
  2183. $colour = '#00BB00'; // Safe - green
  2184. } elseif ($percentage < 90) {
  2185. $colour = '#ffd400'; // Filling up - yelloworange
  2186. } else {
  2187. $colour = '#DD0000'; // Full - red
  2188. }
  2189. // This is used for the table width: a table of only 100 pixels looks too small
  2190. $visual_percentage = 4 * $percentage;
  2191. $visual_other_percentage = 4 * $other_percentage;
  2192. $message .= get_lang('PercentageQuotaInUse') . ': <strong>' . $percentage . '%</strong>.<br />' .
  2193. get_lang('PercentageQuotaFree') . ': <strong>' . $other_percentage . '%</strong>.<br />';
  2194. $show_percentage = '&nbsp;' . $percentage . '%';
  2195. $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">' .
  2196. '<div style="width:' . $percentage . '%; background-color: #bbb; border-right:3px groove #bbb; -moz-border-radius:5px;">&nbsp;</div>' .
  2197. '<span style="margin-top: -15px; margin-left:-15px; position: absolute;font-weight:bold;">' . $show_percentage . '</span></div>';
  2198. echo $message;
  2199. }
  2200. /**
  2201. * Display the document quota in a simple way
  2202. *
  2203. * Here we count 1 kilobyte = 1000 byte, 12 megabyte = 1000 kilobyte.
  2204. */
  2205. static function display_simple_quota($course_quota, $already_consumed_space) {
  2206. $course_quota_m = round($course_quota / 1000000);
  2207. $already_consumed_space_m = round($already_consumed_space / 1000000, 2);
  2208. $percentage = $already_consumed_space / $course_quota * 100;
  2209. $percentage = round($percentage, 1);
  2210. $message = get_lang('YouAreCurrentlyUsingXOfYourX');
  2211. $message = sprintf($message, $already_consumed_space_m, $percentage . '%', $course_quota_m . ' ');
  2212. echo Display::div($message, array('id' => 'document_quota'));
  2213. }
  2214. /**
  2215. * Checks if there is enough place to add a file on a directory
  2216. * on the base of a maximum directory size allowed
  2217. *
  2218. * @author Bert Vanderkimpen
  2219. * @param int file_size size of the file in byte
  2220. * @param array $_course
  2221. * @param int max_dir_space maximum size
  2222. * @return boolean true if there is enough space, false otherwise
  2223. *
  2224. * @see enough_space() uses documents_total_space() function
  2225. */
  2226. static function enough_space($file_size, $max_dir_space) {
  2227. if ($max_dir_space) {
  2228. $already_filled_space = self::documents_total_space();
  2229. if (($file_size + $already_filled_space) > $max_dir_space) {
  2230. return false;
  2231. }
  2232. }
  2233. return true;
  2234. }
  2235. /**
  2236. *
  2237. * @param array paremeters: count, url, extension
  2238. * @return string
  2239. */
  2240. static function generate_jplayer_jquery($params = array()) {
  2241. $js_path = api_get_path(WEB_LIBRARY_PATH) . 'javascript/';
  2242. $jplayer_definition = ' $("#jquery_jplayer_' . $params['count'] . '").jPlayer({
  2243. ready: function() {
  2244. $(this).jPlayer("setMedia", {
  2245. ' . $params['extension'] . ' : "' . $params['url'] . '"
  2246. });
  2247. },
  2248. play: function() { // To avoid both jPlayers playing together.
  2249. $(this).jPlayer("pauseOthers");
  2250. },
  2251. //errorAlerts: true,
  2252. //warningAlerts: true,
  2253. swfPath: "' . $js_path . 'jquery-jplayer",
  2254. //supplied: "m4a, oga, mp3, ogg, wav",
  2255. supplied: "' . $params['extension'] . '",
  2256. wmode: "window",
  2257. solution: "flash, html", // Do not change this setting
  2258. cssSelectorAncestor: "#jp_container_' . $params['count'] . '",
  2259. }); ' . "\n\n";
  2260. return $jplayer_definition;
  2261. }
  2262. /**
  2263. *
  2264. * Shows a play icon next to the document title in the document list
  2265. * @param int
  2266. * @return string html content
  2267. */
  2268. static function generate_media_preview($i, $type = 'simple') {
  2269. $i = intval($i);
  2270. $extra_controls = $progress = '';
  2271. if ($type == 'advanced') {
  2272. $extra_controls = ' <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
  2273. <li><a href="#" class="jp-mute" tabindex="1">mute</a></li>
  2274. <li><a href="#" class="jp-unmute" tabindex="1">unmute</a></li>';
  2275. $progress = '<div class="jp-progress">
  2276. <div class="jp-seek-bar">
  2277. <div class="jp-play-bar"></div>
  2278. </div>
  2279. </div>';
  2280. }
  2281. //Shows only the play button
  2282. $html = '<div id="jquery_jplayer_' . $i . '" class="jp-jplayer"></div>
  2283. <div id="jp_container_' . $i . '" class="jp-audio">
  2284. <div class="jp-type-single">
  2285. <div class="jp-gui jp-interface">
  2286. <ul class="jp-controls">
  2287. <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
  2288. <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
  2289. ' . $extra_controls . '
  2290. </ul>
  2291. ' . $progress . '
  2292. </div>
  2293. </div>
  2294. </div>';
  2295. //<div id="jplayer_inspector_'.$i.'"></div>
  2296. return $html;
  2297. }
  2298. static function generate_video_preview($document_data = array()) {
  2299. $html = '
  2300. <div id="jp_container_1" class="jp-video">
  2301. <div class="jp-type-single">
  2302. <div id="jquery_jplayer_1" class="jp-jplayer"></div>
  2303. <div class="jp-gui">
  2304. <div class="jp-video-play">
  2305. <a href="javascript:;" class="jp-video-play-icon" tabindex="1">play</a>
  2306. </div>
  2307. <div class="jp-interface">
  2308. <div class="jp-progress">
  2309. <div class="jp-seek-bar">
  2310. <div class="jp-play-bar"></div>
  2311. </div>
  2312. </div>
  2313. <div class="jp-current-time"></div>
  2314. <div class="jp-controls-holder">
  2315. <ul class="jp-controls">
  2316. <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
  2317. <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
  2318. <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
  2319. <li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>
  2320. <li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>
  2321. <li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>
  2322. </ul>
  2323. <div class="jp-volume-bar">
  2324. <div class="jp-volume-bar-value"></div>
  2325. </div>
  2326. <ul class="jp-toggles">
  2327. <li><a href="javascript:;" class="jp-full-screen" tabindex="1" title="full screen">full screen</a></li>
  2328. <li><a href="javascript:;" class="jp-restore-screen" tabindex="1" title="restore screen">restore screen</a></li>
  2329. <li><a href="javascript:;" class="jp-repeat" tabindex="1" title="repeat">repeat</a></li>
  2330. <li><a href="javascript:;" class="jp-repeat-off" tabindex="1" title="repeat off">repeat off</a></li>
  2331. </ul>
  2332. </div>
  2333. <div class="jp-title">
  2334. <ul>
  2335. <li>' . $document_data['title'] . '</li>
  2336. </ul>
  2337. </div>
  2338. </div>
  2339. </div>
  2340. <div class="jp-no-solution">
  2341. <span>' . get_lang('UpdateRequire') . '</span>
  2342. ' . get_lang("ToPlayTheMediaYouWillNeedToUpdateYourBrowserToARecentVersionYouCanAlsoDownloadTheFile") . '
  2343. </div>
  2344. </div>
  2345. </div>';
  2346. return $html;
  2347. }
  2348. 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) {
  2349. if (empty($course_info['real_id']) || empty($course_info['code']) || !is_array($course_info)) {
  2350. return '';
  2351. }
  2352. $user_id = api_get_user_id();
  2353. $user_in_course = false;
  2354. if (api_is_platform_admin()) {
  2355. $user_in_course = true;
  2356. }
  2357. if (!$user_in_course) {
  2358. if (CourseManager::is_course_teacher($user_id, $course_info['code'])) {
  2359. $user_in_course = true;
  2360. }
  2361. }
  2362. //condition for the session
  2363. $session_id = intval($session_id);
  2364. if (!$user_in_course) {
  2365. if (empty($session_id)) {
  2366. if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code'])) {
  2367. $user_in_course = true;
  2368. }
  2369. //Check if course is open then we can consider that the student is regitered to the course
  2370. if (isset($course_info) && in_array($course_info['visibility'], array(2, 3))) {
  2371. $user_in_course = true;
  2372. }
  2373. } else {
  2374. $user_status = SessionManager::get_user_status_in_course_session($user_id, $course_info['code'], $session_id);
  2375. //is true if is an student, course session teacher or coach
  2376. if (in_array($user_status, array('0', '2', '6'))) {
  2377. $user_in_course = true;
  2378. }
  2379. }
  2380. }
  2381. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  2382. $tbl_item_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2383. $path = '/';
  2384. $path = Database::escape_string(str_replace('_', '\_', $path));
  2385. $added_slash = ($path == '/') ? '' : '/';
  2386. //$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']."' )))";
  2387. $condition_session = " AND (id_session = '$session_id' OR id_session = '0' )";
  2388. $add_folder_filter = null;
  2389. if (!empty($filter_by_folder)) {
  2390. $add_folder_filter = " AND docs.path LIKE '" . Database::escape_string($filter_by_folder) . "%'";
  2391. }
  2392. $sql_doc = "SELECT last.visibility, docs.*
  2393. FROM $tbl_item_prop AS last, $tbl_doc AS docs
  2394. WHERE docs.id = last.ref AND
  2395. docs.path LIKE '" . $path . $added_slash . "%' AND
  2396. docs.path NOT LIKE '%_DELETED_%' AND
  2397. last.tool = '" . TOOL_DOCUMENT . "' $condition_session AND
  2398. last.visibility = '1' AND
  2399. docs.c_id = {$course_info['real_id']} AND
  2400. last.c_id = {$course_info['real_id']}
  2401. $add_folder_filter
  2402. ORDER BY docs.title ASC";
  2403. $res_doc = Database::query($sql_doc);
  2404. $resources = Database::store_result($res_doc, 'ASSOC');
  2405. $resources_sorted = array();
  2406. $return = '';
  2407. if ($lp_id) {
  2408. $return .= '<div class="lp_resource_element">';
  2409. $return .= Display::return_icon('new_doc.gif', '', array(), ICON_SIZE_SMALL);
  2410. $return .= Display::url(get_lang('NewDocument'), api_get_self() . '?' . api_get_cidreq() . '&action=add_item&type=' . TOOL_DOCUMENT . '&lp_id=' . $_SESSION['oLP']->lp_id);
  2411. $return .= '</div>';
  2412. } else {
  2413. $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'));
  2414. }
  2415. // If you want to debug it, I advise you to do "echo" on the eval statements.
  2416. if (!empty($resources) && $user_in_course) {
  2417. foreach ($resources as $resource) {
  2418. $is_visible = self::is_visible_by_id($resource['id'], $course_info, $session_id, api_get_user_id());
  2419. if (!$is_visible) {
  2420. continue;
  2421. }
  2422. $resource_paths = explode('/', $resource['path']);
  2423. array_shift($resource_paths);
  2424. $path_to_eval = $last_path = '';
  2425. $is_file = false;
  2426. if ($resource['filetype'] == 'file') {
  2427. foreach ($resource_paths as $key => $resource_path) {
  2428. if ($key != count($resource_paths) - 1) {
  2429. // It's a folder.
  2430. $path_to_eval .= "['$resource_path']['files']";
  2431. }
  2432. $is_file = true;
  2433. }
  2434. } else {
  2435. foreach ($resource_paths as $key => $resource_path) {
  2436. if ($key != count($resource_paths) - 1) {
  2437. // It's a folder.
  2438. $path_to_eval .= "['$resource_path']['files']";
  2439. }
  2440. }
  2441. }
  2442. $last_path = $resource_path;
  2443. //$data = json_encode(array('title'=>$resource['title'], 'path'=>$last_path));
  2444. //@todo not sure if it's a good thing using base64_encode. I tried with json_encode but i received the same error
  2445. //Some testing is needed in order to prove the performance
  2446. //Also change the explode to value from "/" to "|@j@|" it fixes #3780
  2447. $data = base64_encode($resource['title'] . '|@j@|' . $last_path);
  2448. if ($is_file) {
  2449. //for backward compatibility
  2450. if (empty($resource['title'])) {
  2451. $resource['title'] = basename($resource['path']);
  2452. }
  2453. eval('$resources_sorted' . $path_to_eval . '[' . $resource['id'] . '] = "' . $data . '" ; ');
  2454. } else {
  2455. eval('$resources_sorted' . $path_to_eval . '["' . $last_path . '"]["id"]=' . $resource['id'] . ';');
  2456. eval('$resources_sorted' . $path_to_eval . '["' . $last_path . '"]["title"]= "' . api_htmlentities($resource['title']) . '";');
  2457. }
  2458. }
  2459. }
  2460. $label = get_lang('Documents');
  2461. $new_array[$label] = array('id' => 0, 'files' => $resources_sorted);
  2462. $write_result = self::write_resources_tree($course_info, $session_id, $new_array, 0, $lp_id, $target, $add_move_button, $overwrite_url);
  2463. $return .= $write_result;
  2464. $img_path = api_get_path(WEB_IMG_PATH);
  2465. if ($lp_id == false) {
  2466. $return .= "<script>
  2467. $('.doc_folder').mouseover(function() {
  2468. var my_id = this.id.split('_')[2];
  2469. $('#res_'+my_id).show();
  2470. });
  2471. $('.close_div').click(function() {
  2472. var course_id = this.id.split('_')[2];
  2473. var session_id = this.id.split('_')[3];
  2474. $('#document_result_'+course_id+'_'+session_id).hide();
  2475. $('.lp_resource').remove();
  2476. });
  2477. </script>";
  2478. } else {
  2479. //For LPs
  2480. $return .= "<script>
  2481. function testResources(id, img) {
  2482. if (document.getElementById(id).style.display=='block'){
  2483. document.getElementById(id).style.display='none';
  2484. var id = id.split('_')[1];
  2485. document.getElementById('img_'+id).src='" . $img_path . "nolines_plus.gif';
  2486. } else {
  2487. document.getElementById(id).style.display='block';
  2488. var id = id.split('_')[1];
  2489. document.getElementById('img_'+id).src='" . $img_path . "nolines_minus.gif';
  2490. }
  2491. }
  2492. </script>";
  2493. }
  2494. if (!$user_in_course) {
  2495. $return = '';
  2496. }
  2497. return $return;
  2498. }
  2499. /**
  2500. * Generate and return an HTML list of resources based on a given array.
  2501. * This list is used to show the course creator a list of available resources to choose from
  2502. * when creating a learning path.
  2503. * @param array Array of elements to add to the list
  2504. * @param integer Enables the tree display by shifting the new elements a certain distance to the right
  2505. * @return string The HTML list
  2506. */
  2507. 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) {
  2508. require_once api_get_path(LIBRARY_PATH) . 'fileDisplay.lib.php';
  2509. $img_path = api_get_path(WEB_IMG_PATH);
  2510. $img_sys_path = api_get_path(SYS_CODE_PATH) . 'img/';
  2511. $web_code_path = api_get_path(WEB_CODE_PATH);
  2512. $return = '';
  2513. if (count($resources_sorted) > 0) {
  2514. foreach ($resources_sorted as $key => $resource) {
  2515. $title = isset($resource['title']) ? $resource['title'] : null;
  2516. if (empty($title)) {
  2517. $title = $key;
  2518. }
  2519. //echo '<pre>'; print_r($resource);
  2520. if (isset($resource['id']) && is_int($resource['id'])) {
  2521. // It's a folder.
  2522. //hide some folders
  2523. if (in_array($key, array('shared_folder', 'chat_files', 'HotPotatoes_files', 'css', 'certificates'))) {
  2524. continue;
  2525. } elseif (preg_match('/_groupdocs/', $key)) {
  2526. continue;
  2527. } elseif (preg_match('/sf_user_/', $key)) {
  2528. continue;
  2529. } elseif (preg_match('/shared_folder_session_/', $key)) {
  2530. continue;
  2531. }
  2532. //trad some titles
  2533. if ($key == 'images') {
  2534. $key = get_lang('Images');
  2535. } elseif ($key == 'gallery') {
  2536. $key = get_lang('Gallery');
  2537. } elseif ($key == 'flash') {
  2538. $key = get_lang('Flash');
  2539. } elseif ($key == 'audio') {
  2540. $key = get_lang('Audio');
  2541. } elseif ($key == 'video') {
  2542. $key = get_lang('Video');
  2543. }
  2544. $onclick = '';
  2545. if ($lp_id) {
  2546. $onclick = 'onclick="javascript: testResources(\'res_' . $resource['id'] . '\',\'img_' . $resource['id'] . '\')"';
  2547. }
  2548. $return .= '<ul class="lp_resource">';
  2549. $return .= '<li class="doc_folder" id="doc_id_' . $resource['id'] . '" style="margin-left:' . ($num * 18) . 'px; ">';
  2550. if ($lp_id) {
  2551. $return .= '<img style="cursor: pointer;" src="' . $img_path . 'nolines_plus.gif" align="absmiddle" id="img_' . $resource['id'] . '" ' . $onclick . ' >';
  2552. } else {
  2553. $return .= '<span style="margin-left:16px">&nbsp;</span>';
  2554. }
  2555. $return .= '<img alt="" src="' . $img_path . 'lp_folder.gif" title="" align="absmiddle" />&nbsp;';
  2556. $return .= '<span ' . $onclick . ' style="cursor: pointer;" >' . $title . '</span>';
  2557. $return .= '</li>';
  2558. $return .= '<div id="res_' . $resource['id'] . '" style="display: none;" >';
  2559. if (isset($resource['files'])) {
  2560. $return .= self::write_resources_tree($course_info, $session_id, $resource['files'], $num + 1, $lp_id, $target, $add_move_button, $overwrite_url);
  2561. }
  2562. $return .= '</div>';
  2563. $return .= '</ul>';
  2564. } else {
  2565. if (!is_array($resource)) {
  2566. $resource = base64_decode($resource);
  2567. // It's a file.
  2568. $icon = choose_image($resource);
  2569. $position = strrpos($icon, '.');
  2570. $icon = substr($icon, 0, $position) . '_small.gif';
  2571. $file_info = explode('|@j@|', $resource);
  2572. $my_file_title = $file_info[0];
  2573. //If title is empty we try to use the path
  2574. if (empty($my_file_title)) {
  2575. $my_file_title = $file_info[1];
  2576. }
  2577. // Show the "image name" not the filename of the image.
  2578. if ($lp_id) {
  2579. //LP URL
  2580. $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;
  2581. if (!empty($overwrite_url)) {
  2582. $url = $overwrite_url . '&document_id=' . $key;
  2583. }
  2584. } else {
  2585. //Direct document URL
  2586. $url = $web_code_path . 'document/document.php?cidReq=' . $course_info['code'] . '&id_session=' . $session_id . '&id=' . $key;
  2587. if (!empty($overwrite_url)) {
  2588. $url = $overwrite_url . '&document_id=' . $key;
  2589. }
  2590. }
  2591. $img = $img_path . $icon;
  2592. if (!file_exists($img_sys_path . $icon)) {
  2593. $img = $img_path . 'icons/16/default_small.gif';
  2594. }
  2595. $link = Display::url('<img alt="" src="' . $img . '" title="" />&nbsp;' . $my_file_title, $url, array('target' => $target));
  2596. if ($lp_id == false) {
  2597. $return .= '<li class="doc_resource" data_id="' . $key . '" data_type="document" title="' . $my_file_title . '" >';
  2598. } else {
  2599. $return .= '<li class="doc_resource lp_resource_element" data_id="' . $key . '" data_type="document" title="' . $my_file_title . '" >';
  2600. }
  2601. $return .= '<div class="item_data" style="margin-left:' . (($num + 1) * 18) . 'px;margin-right:5px;">';
  2602. if ($add_move_button) {
  2603. $return .= '<a class="moved" href="#">';
  2604. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
  2605. $return .= '</a> ';
  2606. }
  2607. $return .= $link;
  2608. $return .= '</div></li>';
  2609. }
  2610. }
  2611. }
  2612. }
  2613. return $return;
  2614. }
  2615. public static function check_visibility_tree($doc_id, $course_code, $session_id, $user_id) {
  2616. $document_data = self::get_document_data_by_id($doc_id, $course_code);
  2617. if (!empty($document_data)) {
  2618. //if admin or course teacher, allow anyway
  2619. if (api_is_platform_admin() || CourseManager::is_course_teacher($user_id, $course_code)) {
  2620. return true;
  2621. }
  2622. $course_info = api_get_course_info($course_code);
  2623. if ($document_data['parent_id'] == false || empty($document_data['parent_id'])) {
  2624. $visible = self::is_visible_by_id($doc_id, $course_info, $session_id, $user_id);
  2625. return $visible;
  2626. } else {
  2627. $course_info = api_get_course_info($course_code);
  2628. $visible = self::is_visible_by_id($doc_id, $course_info, $session_id, $user_id);
  2629. if (!$visible) {
  2630. return false;
  2631. } else {
  2632. return self::check_visibility_tree($document_data['parent_id'], $course_code, $session_id, $user_id);
  2633. }
  2634. }
  2635. } else {
  2636. return false;
  2637. }
  2638. }
  2639. /**
  2640. * Index a given document.
  2641. * @param int Document ID inside its corresponding course
  2642. * @param string Course code
  2643. * @param int Session ID (not used yet)
  2644. * @param string Language of document's content (defaults to course language)
  2645. * @param array Array of specific fields (['code'=>'value',...])
  2646. * @param string What to do if the file already exists (default or overwrite)
  2647. * @param bool When set to true, this runs the indexer without actually saving anything to any database
  2648. * @return bool Returns true on presumed success, false on failure
  2649. */
  2650. public function index_document($docid, $course_code, $session_id = 0, $lang = 'english', $specific_fields_values = array(), $if_exists = '', $simulation = false) {
  2651. if (api_get_setting('search_enabled') !== 'true') {
  2652. return false;
  2653. }
  2654. if (empty($docid) or $docid != intval($docid)) {
  2655. return false;
  2656. }
  2657. if (empty($session_id)) {
  2658. $session_id = api_get_session_id();
  2659. }
  2660. $course_info = api_get_course_info($course_code);
  2661. $course_dir = $course_info['path'] . '/document';
  2662. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  2663. $base_work_dir = $sys_course_path . $course_dir;
  2664. $course_id = $course_info['real_id'];
  2665. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  2666. $qry = "SELECT path, title FROM $table_document WHERE c_id = $course_id AND id = '$docid' LIMIT 1";
  2667. $result = Database::query($qry);
  2668. if (Database::num_rows($result) == 1) {
  2669. $row = Database::fetch_array($result);
  2670. $doc_path = api_get_path(SYS_COURSE_PATH) . $course_dir . $row['path'];
  2671. //TODO: mime_content_type is deprecated, fileinfo php extension is enabled by default as of PHP 5.3.0
  2672. // now versions of PHP on Debian testing(5.2.6-5) and Ubuntu(5.2.6-2ubuntu) are lower, so wait for a while
  2673. $doc_mime = mime_content_type($doc_path);
  2674. //echo $doc_mime;
  2675. $allowed_mime_types = self::file_get_mime_type(true);
  2676. // 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
  2677. if (empty($doc_mime)) {
  2678. $allowed_extensions = array('doc', 'docx', 'ppt', 'pptx', 'pps', 'ppsx', 'xls', 'xlsx', 'odt', 'odp', 'ods', 'pdf', 'txt', 'rtf', 'msg', 'csv', 'html', 'htm');
  2679. $extensions = preg_split("/[\/\\.]/", $doc_path);
  2680. $doc_ext = strtolower($extensions[count($extensions) - 1]);
  2681. if (in_array($doc_ext, $allowed_extensions)) {
  2682. switch ($doc_ext) {
  2683. case 'ppt':
  2684. case 'pps':
  2685. $doc_mime = 'application/vnd.ms-powerpoint';
  2686. break;
  2687. case 'xls':
  2688. $doc_mime = 'application/vnd.ms-excel';
  2689. break;
  2690. }
  2691. }
  2692. }
  2693. //@todo move this nightmare in a search controller or something like that!!! J.M
  2694. if (in_array($doc_mime, $allowed_mime_types)) {
  2695. $file_title = $row['title'];
  2696. $file_content = self::get_text_content($doc_path, $doc_mime);
  2697. $course_code = Database::escape_string($course_code);
  2698. require_once api_get_path(LIBRARY_PATH) . 'search/ChamiloIndexer.class.php';
  2699. require_once api_get_path(LIBRARY_PATH) . 'search/IndexableChunk.class.php';
  2700. $ic_slide = new IndexableChunk();
  2701. $ic_slide->addValue('title', $file_title);
  2702. $ic_slide->addCourseId($course_code);
  2703. $ic_slide->addToolId(TOOL_DOCUMENT);
  2704. $xapian_data = array(
  2705. SE_COURSE_ID => $course_code,
  2706. SE_TOOL_ID => TOOL_DOCUMENT,
  2707. SE_DATA => array('doc_id' => $docid),
  2708. SE_USER => api_get_user_id(),
  2709. );
  2710. $ic_slide->xapian_data = serialize($xapian_data);
  2711. $di = new ChamiloIndexer();
  2712. $return = $di->connectDb(null, null, $lang);
  2713. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  2714. $specific_fields = get_specific_field_list();
  2715. // process different depending on what to do if file exists
  2716. /**
  2717. * @TODO Find a way to really verify if the file had been
  2718. * overwriten. Now all work is done at
  2719. * handle_uploaded_document() and it's difficult to verify it
  2720. */
  2721. if (!empty($if_exists) && $if_exists == 'overwrite') {
  2722. // Overwrite the file on search engine
  2723. // Actually, it consists on a delete of terms from db,
  2724. // insert new ones, create a new search engine document,
  2725. // and remove the old one
  2726. // Get search_did
  2727. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  2728. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  2729. $sql = sprintf($sql, $tbl_se_ref, $course_code, TOOL_DOCUMENT, $docid);
  2730. $res = Database::query($sql);
  2731. if (Database::num_rows($res) > 0) {
  2732. $se_ref = Database::fetch_array($res);
  2733. if (!$simulation) {
  2734. $di->remove_document($se_ref['search_did']);
  2735. }
  2736. $all_specific_terms = '';
  2737. foreach ($specific_fields as $specific_field) {
  2738. if (!$simulation) {
  2739. delete_all_specific_field_value($course_code, $specific_field['id'], TOOL_DOCUMENT, $docid);
  2740. }
  2741. // Update search engine
  2742. if (isset($specific_fields_values[$specific_field['code']])) {
  2743. $sterms = trim($specific_fields_values[$specific_field['code']]);
  2744. } else { //if the specific field is not defined, force an empty one
  2745. $sterms = '';
  2746. }
  2747. $all_specific_terms .= ' ' . $sterms;
  2748. $sterms = explode(',', $sterms);
  2749. foreach ($sterms as $sterm) {
  2750. $sterm = trim($sterm);
  2751. if (!empty($sterm)) {
  2752. $ic_slide->addTerm($sterm, $specific_field['code']);
  2753. // updated the last param here from $value to $sterm without being sure - see commit15464
  2754. if (!$simulation) {
  2755. add_specific_field_value($specific_field['id'], $course_code, TOOL_DOCUMENT, $docid, $sterm);
  2756. }
  2757. }
  2758. }
  2759. }
  2760. // Add terms also to content to make terms findable by probabilistic search
  2761. $file_content = $all_specific_terms . ' ' . $file_content;
  2762. if (!$simulation) {
  2763. $ic_slide->addValue('content', $file_content);
  2764. $di->addChunk($ic_slide);
  2765. // Index and return a new search engine document id
  2766. $did = $di->index();
  2767. //var_dump($did);
  2768. if ($did) {
  2769. // update the search_did on db
  2770. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  2771. $sql = 'UPDATE %s SET search_did=%d WHERE id=%d LIMIT 1';
  2772. $sql = sprintf($sql, $tbl_se_ref, (int) $did, (int) $se_ref['id']);
  2773. Database::query($sql);
  2774. }
  2775. }
  2776. }
  2777. } else {
  2778. // Add all terms
  2779. $all_specific_terms = '';
  2780. foreach ($specific_fields as $specific_field) {
  2781. if (isset($specific_fields_values[$specific_field['code']])) {
  2782. $sterms = trim($specific_fields_values[$specific_field['code']]);
  2783. } else { //if the specific field is not defined, force an empty one
  2784. $sterms = '';
  2785. }
  2786. $all_specific_terms .= ' ' . $sterms;
  2787. if (!empty($sterms)) {
  2788. $sterms = explode(',', $sterms);
  2789. foreach ($sterms as $sterm) {
  2790. if (!$simulation) {
  2791. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  2792. add_specific_field_value($specific_field['id'], $course_code, TOOL_DOCUMENT, $docid, $sterm);
  2793. }
  2794. }
  2795. }
  2796. }
  2797. // Add terms also to content to make terms findable by probabilistic search
  2798. $file_content = $all_specific_terms . ' ' . $file_content;
  2799. if (!$simulation) {
  2800. $ic_slide->addValue('content', $file_content);
  2801. $di->addChunk($ic_slide);
  2802. // Index and return search engine document id
  2803. $did = $di->index();
  2804. if ($did) {
  2805. // Save it to db
  2806. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  2807. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  2808. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  2809. $sql = sprintf($sql, $tbl_se_ref, $course_code, TOOL_DOCUMENT, $docid, $did);
  2810. Database::query($sql);
  2811. } else {
  2812. return false;
  2813. }
  2814. }
  2815. }
  2816. } else {
  2817. return false;
  2818. }
  2819. }
  2820. return true;
  2821. }
  2822. public static function get_web_odf_extension_list() {
  2823. return array('ods', 'odt');
  2824. }
  2825. public static function is_folder_to_avoid($path, $is_certificate_mode = false) {
  2826. $folders_to_avoid = array(
  2827. '/HotPotatoes_files',
  2828. '/certificates',
  2829. );
  2830. if (basename($path) == 'css') {
  2831. return true;
  2832. }
  2833. //Skip hotpotatoes results
  2834. if (strstr($path, 'HotPotatoes_files')) {
  2835. return true;
  2836. }
  2837. if ($is_certificate_mode == false) {
  2838. //Certificate results
  2839. if (strstr($path, 'certificates')) {
  2840. return true;
  2841. }
  2842. }
  2843. //Admin setting for Hide/Show the folders of all users
  2844. if (api_get_setting('show_users_folders') == 'false') {
  2845. $folders_to_avoid[] = '/shared_folder';
  2846. if (strstr($path, 'shared_folder_session_')) {
  2847. return true;
  2848. }
  2849. }
  2850. //Admin setting for Hide/Show Default folders to all users
  2851. if (api_get_setting('show_default_folders') == 'false') {
  2852. $folders_to_avoid[] = '/images';
  2853. $folders_to_avoid[] = '/flash';
  2854. $folders_to_avoid[] = '/audio';
  2855. $folders_to_avoid[] = '/video';
  2856. }
  2857. //Admin setting for Hide/Show chat history folder
  2858. if (api_get_setting('show_chat_folder') == 'false') {
  2859. $folders_to_avoid[] = '/chat_files';
  2860. }
  2861. return in_array($path, $folders_to_avoid);
  2862. }
  2863. static function get_system_folders() {
  2864. $system_folders = array(
  2865. '/certificates',
  2866. '/chat_files',
  2867. '/images',
  2868. '/flash',
  2869. '/audio',
  2870. '/video',
  2871. '/shared_folder'
  2872. );
  2873. return $system_folders;
  2874. }
  2875. }
  2876. //end class DocumentManager