document.lib.php 157 KB

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