12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233 |
- <?php
- class DocumentManager {
- private function __construct() {
- }
-
- public static function get_course_quota($course_code = null) {
- if (empty($course_code)) {
- $course_info = api_get_course_info();
- } else {
- $course_info = api_get_course_info($course_code);
- }
-
- $course_quota = null;
- if (empty($course_info)) {
- return DEFAULT_DOCUMENT_QUOTA;
- } else {
- $course_quota = $course_info['disk_quota'];
- }
- if (is_null($course_quota) || empty($course_quota)) {
-
- $course_quota = DEFAULT_DOCUMENT_QUOTA;
- }
- return $course_quota;
- }
-
- public static function file_get_mime_type($filename) {
-
-
- $mime_types = array(
- 'ai' => 'application/postscript',
- 'aif' => 'audio/x-aiff',
- 'aifc' => 'audio/x-aiff',
- 'aiff' => 'audio/x-aiff',
- 'asf' => 'video/x-ms-asf',
- 'asc' => 'text/plain',
- 'au' => 'audio/basic',
- 'avi' => 'video/x-msvideo',
- 'bcpio' => 'application/x-bcpio',
- 'bin' => 'application/octet-stream',
- 'bmp' => 'image/bmp',
- 'cdf' => 'application/x-netcdf',
- 'class' => 'application/octet-stream',
- 'cpio' => 'application/x-cpio',
- 'cpt' => 'application/mac-compactpro',
- 'csh' => 'application/x-csh',
- 'css' => 'text/css',
- 'dcr' => 'application/x-director',
- 'dir' => 'application/x-director',
- 'djv' => 'image/vnd.djvu',
- 'djvu' => 'image/vnd.djvu',
- 'dll' => 'application/octet-stream',
- 'dmg' => 'application/x-diskcopy',
- 'dms' => 'application/octet-stream',
- 'doc' => 'application/msword',
- 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
- 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
- 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
- 'dvi' => 'application/x-dvi',
- 'dwg' => 'application/vnd.dwg',
- 'dxf' => 'application/vnd.dxf',
- 'dxr' => 'application/x-director',
- 'eps' => 'application/postscript',
- 'epub' => 'application/epub+zip',
- 'etx' => 'text/x-setext',
- 'exe' => 'application/octet-stream',
- 'ez' => 'application/andrew-inset',
- 'gif' => 'image/gif',
- 'gtar' => 'application/x-gtar',
- 'gz' => 'application/x-gzip',
- 'hdf' => 'application/x-hdf',
- 'hqx' => 'application/mac-binhex40',
- 'htm' => 'text/html',
- 'html' => 'text/html',
- 'ice' => 'x-conference-xcooltalk',
- 'ief' => 'image/ief',
- 'iges' => 'model/iges',
- 'igs' => 'model/iges',
- 'jar' => 'application/java-archiver',
- 'jpe' => 'image/jpeg',
- 'jpeg' => 'image/jpeg',
- 'jpg' => 'image/jpeg',
- 'js' => 'application/x-javascript',
- 'kar' => 'audio/midi',
- 'lam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
- 'latex' => 'application/x-latex',
- 'lha' => 'application/octet-stream',
- 'log' => 'text/plain',
- 'lzh' => 'application/octet-stream',
- 'm1a' => 'audio/mpeg',
- 'm2a' => 'audio/mpeg',
- 'm3u' => 'audio/x-mpegurl',
- 'man' => 'application/x-troff-man',
- 'me' => 'application/x-troff-me',
- 'mesh' => 'model/mesh',
- 'mid' => 'audio/midi',
- 'midi' => 'audio/midi',
- 'mov' => 'video/quicktime',
- 'movie' => 'video/x-sgi-movie',
- 'mp2' => 'audio/mpeg',
- 'mp3' => 'audio/mpeg',
- 'mp4' => 'video/mpeg4-generic',
- 'mpa' => 'audio/mpeg',
- 'mpe' => 'video/mpeg',
- 'mpeg' => 'video/mpeg',
- 'mpg' => 'video/mpeg',
- 'mpga' => 'audio/mpeg',
- 'ms' => 'application/x-troff-ms',
- 'msh' => 'model/mesh',
- 'mxu' => 'video/vnd.mpegurl',
- 'nc' => 'application/x-netcdf',
- 'oda' => 'application/oda',
- 'oga'=> 'audio/ogg',
- 'ogg'=> 'application/ogg',
- 'ogx'=> 'application/ogg',
- 'ogv'=> 'video/ogg',
- 'pbm' => 'image/x-portable-bitmap',
- 'pct' => 'image/pict',
- 'pdb' => 'chemical/x-pdb',
- 'pdf' => 'application/pdf',
- 'pgm' => 'image/x-portable-graymap',
- 'pgn' => 'application/x-chess-pgn',
- 'pict' => 'image/pict',
- 'png' => 'image/png',
- 'pnm' => 'image/x-portable-anymap',
- 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
- 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
- 'pps' => 'application/vnd.ms-powerpoint',
- 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
- 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
- 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
- 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
- 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
- 'ppm' => 'image/x-portable-pixmap',
- 'ppt' => 'application/vnd.ms-powerpoint',
- 'pps' => 'application/vnd.ms-powerpoint',
- 'ps' => 'application/postscript',
- 'qt' => 'video/quicktime',
- 'ra' => 'audio/x-realaudio',
- 'ram' => 'audio/x-pn-realaudio',
- 'rar' => 'image/x-rar-compressed',
- 'ras' => 'image/x-cmu-raster',
- 'rgb' => 'image/x-rgb',
- 'rm' => 'audio/x-pn-realaudio',
- 'roff' => 'application/x-troff',
- 'rpm' => 'audio/x-pn-realaudio-plugin',
- 'rtf' => 'text/rtf',
- 'rtx' => 'text/richtext',
- 'sgm' => 'text/sgml',
- 'sgml' => 'text/sgml',
- 'sh' => 'application/x-sh',
- 'shar' => 'application/x-shar',
- 'silo' => 'model/mesh',
- 'sib' => 'application/X-Sibelius-Score',
- 'sit' => 'application/x-stuffit',
- 'skd' => 'application/x-koan',
- 'skm' => 'application/x-koan',
- 'skp' => 'application/x-koan',
- 'skt' => 'application/x-koan',
- 'smi' => 'application/smil',
- 'smil' => 'application/smil',
- 'snd' => 'audio/basic',
- 'so' => 'application/octet-stream',
- 'spl' => 'application/x-futuresplash',
- 'src' => 'application/x-wais-source',
- 'sv4cpio' => 'application/x-sv4cpio',
- 'sv4crc' => 'application/x-sv4crc',
- 'svf' => 'application/vnd.svf',
- 'svg' => 'image/svg+xml',
-
- 'swf' => 'application/x-shockwave-flash',
- 'sxc' => 'application/vnd.sun.xml.calc',
- 'sxi' => 'application/vnd.sun.xml.impress',
- 'sxw' => 'application/vnd.sun.xml.writer',
- 't' => 'application/x-troff',
- 'tar' => 'application/x-tar',
- 'tcl' => 'application/x-tcl',
- 'tex' => 'application/x-tex',
- 'texi' => 'application/x-texinfo',
- 'texinfo' => 'application/x-texinfo',
- 'tga' => 'image/x-targa',
- 'tif' => 'image/tif',
- 'tiff' => 'image/tiff',
- 'tr' => 'application/x-troff',
- 'tsv' => 'text/tab-seperated-values',
- 'txt' => 'text/plain',
- 'ustar' => 'application/x-ustar',
- 'vcd' => 'application/x-cdlink',
- 'vrml' => 'model/vrml',
- 'wav' => 'audio/x-wav',
- 'wbmp' => 'image/vnd.wap.wbmp',
- 'wbxml' => 'application/vnd.wap.wbxml',
- 'wml' => 'text/vnd.wap.wml',
- 'wmlc' => 'application/vnd.wap.wmlc',
- 'wmls' => 'text/vnd.wap.wmlscript',
- 'wmlsc' => 'application/vnd.wap.wmlscriptc',
- 'wma' => 'audio/x-ms-wma',
- 'wmv' => 'video/x-ms-wmv',
- 'wrl' => 'model/vrml',
- 'xbm' => 'image/x-xbitmap',
- 'xht' => 'application/xhtml+xml',
- 'xhtml' => 'application/xhtml+xml',
- 'xls' => 'application/vnd.ms-excel',
- 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
- 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
- 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
- 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
- 'xml' => 'text/xml',
- 'xpm' => 'image/x-xpixmap',
- 'xsl' => 'text/xml',
- 'xwd' => 'image/x-windowdump',
- 'xyz' => 'chemical/x-xyz',
- 'zip' => 'application/zip'
- );
- if ($filename === true) {
- return $mime_types;
- }
-
- $extension = explode('.', $filename);
-
- if (is_array($extension)) {
- $extension = strtolower($extension[sizeof($extension) - 1]);
- }
-
- else {
- $extension = 'empty';
- }
-
- if (isset($mime_types[$extension])) {
- return $mime_types[$extension];
- }
-
- return 'application/octet-stream';
- }
-
- public static function file_visible_to_user ($this_course, $doc_url) {
- $current_session_id = api_get_session_id();
- $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
- if ($is_allowed_to_edit) {
- return true;
- } else {
- $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
- $tbl_item_property = $this_course.'item_property';
- $doc_url = Database::escape_string($doc_url);
-
- $query = "SELECT 1 FROM $tbl_document AS docs,$tbl_item_property AS props
- WHERE props.tool = 'document' AND docs.id=props.ref AND props.visibility <> '1' AND docs.path = '$doc_url'";
-
- $result = Database::query($query);
- return (Database::num_rows($result) == 0);
- }
- }
-
- public static function file_send_for_download($full_file_name, $forced = false, $name = '') {
- if (!is_file($full_file_name)) {
- return false;
- }
- $filename = ($name == '') ? basename($full_file_name) : replace_dangerous_char($name);
- $len = filesize($full_file_name);
- if ($forced) {
-
- header('Content-type: application/octet-stream');
-
- header('Content-length: '.$len);
- if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
- header('Content-Disposition: filename= '.$filename);
- } else {
- header('Content-Disposition: attachment; filename= '.$filename);
- }
- if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
- header('Pragma: ');
- header('Cache-Control: ');
- header('Cache-Control: public');
- }
- header('Content-Description: '.$filename);
- header('Content-transfer-encoding: binary');
- $fp = fopen($full_file_name, 'r');
- fpassthru($fp);
- return true;
- } else {
-
- $content_type = self::file_get_mime_type($filename);
- header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
- header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
-
-
-
- switch ($content_type) {
- case 'text/html':
- $encoding = @api_detect_encoding_html(file_get_contents($full_file_name));
- if (!empty($encoding)) {
- $content_type .= '; charset='.$encoding;
- }
- break;
- case 'text/plain':
- $encoding = @api_detect_encoding(strip_tags(file_get_contents($full_file_name)));
- if (!empty($encoding)) {
- $content_type .= '; charset='.$encoding;
- }
- break;
- }
- header('Content-type: '.$content_type);
- header('Content-Length: '.$len);
- $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
- if (strpos($user_agent, 'msie')) {
- header('Content-Disposition: ; filename= '.$filename);
- } else {
- header('Content-Disposition: inline; filename= '.$filename);
- }
- readfile($full_file_name);
- return true;
- }
- }
-
- public static function string_send_for_download($full_string, $forced = false, $name = '') {
- $filename = $name;
- $len = strlen($full_string);
- if ($forced) {
-
- header('Content-type: application/octet-stream');
-
- header('Content-length: '.$len);
- if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
- header('Content-Disposition: filename= '.$filename);
- } else {
- header('Content-Disposition: attachment; filename= '.$filename);
- }
- if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
- header('Pragma: ');
- header('Cache-Control: ');
- header('Cache-Control: public');
- }
- header('Content-Description: '.$filename);
- header('Content-transfer-encoding: binary');
-
-
- echo $full_string;
- return true;
-
-
-
- } else {
-
- $content_type = self::file_get_mime_type($filename);
- header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
- header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
- header('Cache-Control: no-cache, must-revalidate');
- header('Pragma: no-cache');
- switch ($content_type) {
- case 'text/html':
- $encoding = @api_detect_encoding_html($full_string);
- if (!empty($encoding)) {
- $content_type .= '; charset='.$encoding;
- }
- break;
- case 'text/plain':
- $encoding = @api_detect_encoding(strip_tags($full_string));
- if (!empty($encoding)) {
- $content_type .= '; charset='.$encoding;
- }
- break;
- }
- header('Content-type: '.$content_type);
- header('Content-Length: '.$len);
- $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
- if (strpos($user_agent, 'msie')) {
- header('Content-Disposition: ; filename= '.$filename);
- } else {
- header('Content-Disposition: inline; filename= '.$filename);
- }
- echo($full_string);
-
-
-
- return true;
- }
- }
-
- public static function get_all_document_data($_course, $path = '/', $to_group_id = 0, $to_user_id = NULL, $can_see_invisible = false, $search = false) {
- $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
-
- if (!is_null($to_user_id)) {
- $to_field = 'last.to_user_id';
- $to_value = $to_user_id;
- } else {
- $to_field = 'last.to_group_id';
- $to_value = $to_group_id;
- }
-
- $path = Database::escape_string(str_replace('_', '\_', $path));
- $to_user_id = Database::escape_string($to_user_id);
- $to_value = Database::escape_string($to_value);
- $visibility_bit = ' <> 2';
-
-
- $added_slash = ($path == '/') ? '' : '/';
-
- $current_session_id = api_get_session_id();
- $condition_session = " AND (id_session = '$current_session_id' OR id_session = '0')";
-
- $sql = "SELECT docs.id,
- docs.filetype,
- docs.path,
- docs.title,
- docs.comment,
- docs.size,
- docs.readonly,
- docs.session_id,
- last.id_session item_property_session_id,
- last.lastedit_date,
- last.visibility,
- last.insert_user_id
- FROM ".$TABLE_ITEMPROPERTY." AS last INNER JOIN ".$TABLE_DOCUMENT." AS docs
- ON (docs.id = last.ref AND last.tool = '" . TOOL_DOCUMENT . "' AND docs.c_id = {$_course['real_id']} AND last.c_id = {$_course['real_id']})
- WHERE
- docs.path LIKE '".$path.$added_slash."%' AND
- docs.path NOT LIKE '".$path.$added_slash."%/%' AND
- ".$to_field." = ".$to_value." AND
- last.visibility".$visibility_bit.$condition_session;
- $result = Database::query($sql);
- $doc_list = array();
- $document_data = array();
- $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
- if ($result !== false && Database::num_rows($result) != 0) {
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- if (api_is_coach()) {
-
- if (in_array($row['id'], array_keys($doc_list))) {
- if ($doc_list[$row['id']]['item_property_session_id'] == 0 && $doc_list[$row['id']]['session_id'] == 0) {
- if ($doc_list[$row['id']]['visibility'] == 0) {
- unset($document_data[$row['id']]);
- continue;
- }
- }
- }
- $doc_list[$row['id']] = $row;
- }
- if (!api_is_coach() && !$is_allowed_to_edit) {
- $doc_list[] = $row;
- }
- if ($row['filetype'] == 'file' && pathinfo($row['path'], PATHINFO_EXTENSION) == 'html') {
-
- $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
- $sql_is_template = "SELECT id FROM $table_template
- WHERE course_code = '".$_course['code']."'
- AND user_id='".api_get_user_id()."'
- AND ref_doc='".$row['id']."'";
- $template_result = Database::query($sql_is_template);
- $row['is_template'] = (Database::num_rows($template_result) > 0) ? 1 : 0;
- }
-
- $document_data[$row['id']] = $row;
- }
-
- if (!api_is_coach() && !$is_allowed_to_edit) {
- $ids_to_remove = array();
- $my_repeat_ids = $temp= array();
-
- foreach($doc_list as $row ) {
- if (in_array($row['id'], array_keys($temp))) {
- $my_repeat_ids[] = $row['id'];
- }
- $temp[$row['id']] = $row;
- }
-
-
- foreach($my_repeat_ids as $id) {
- foreach($doc_list as $row ) {
- if ($id == $row['id']) {
- if ($row['visibility'] == 0 && $row['item_property_session_id'] == 0) {
- $delete_repeated[$id] = true;
- }
- if ($row['visibility'] == 0 && $row['item_property_session_id'] != 0) {
- $delete_repeated[$id] = true;
- }
- }
- }
- }
- foreach($doc_list as $key=>$row) {
- if (in_array($row['visibility'], array('0','2')) && !in_array($row['id'],$my_repeat_ids) ) {
- $ids_to_remove[] = $row['id'];
- unset($doc_list[$key]);
- }
- }
- foreach($document_data as $row) {
- if (in_array($row['id'], $ids_to_remove)) {
- unset($document_data[$row['id']]);
- }
- if (isset($delete_repeated[$row['id']]) && $delete_repeated[$row['id']]) {
- unset($document_data[$row['id']]);
- }
- }
-
- $final_document_data = array();
- foreach($document_data as $row) {
- $is_visible = DocumentManager::check_visibility_tree($row['id'], $_course['code'], $current_session_id, api_get_user_id());
- if ($is_visible) {
- $final_document_data[$row['id']]=$row;
- }
- }
- } else {
- $final_document_data = $document_data;
- }
- return $final_document_data;
- } else {
-
- return false;
- }
- }
-
- public static function get_all_document_folders ($_course, $to_group_id = '0', $can_see_invisible = false) {
- $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
- $to_group_id = intval($to_group_id);
- if ($can_see_invisible) {
-
- $session_id = api_get_session_id();
- $condition_session = api_get_session_condition($session_id);
- if ($to_group_id <> 0) {
- $sql = "SELECT DISTINCT docs.id, path
- FROM $TABLE_ITEMPROPERTY AS last INNER JOIN $TABLE_DOCUMENT AS docs
- ON (docs.id = last.ref AND last.tool = '" . TOOL_DOCUMENT . "' AND last.c_id = {$_course['real_id']} AND docs.c_id = {$_course['real_id']} )
- WHERE
- docs.filetype = 'folder' AND
- last.to_group_id = ".$to_group_id." AND
- docs.path NOT LIKE '%shared_folder%' AND
- last.visibility <> 2 $condition_session ";
- } else {
- $sql = "SELECT DISTINCT docs.id, path
- FROM $TABLE_ITEMPROPERTY AS last INNER JOIN $TABLE_DOCUMENT AS docs
- ON (docs.id = last.ref AND last.tool = '" . TOOL_DOCUMENT . "' AND last.c_id = {$_course['real_id']} AND docs.c_id = {$_course['real_id']} )
- WHERE
- docs.filetype = 'folder' AND
- last.to_group_id = 0 AND
- last.visibility <> 2 $condition_session ";
- }
- $result = Database::query($sql);
- if ($result && Database::num_rows($result) != 0) {
- while ($row = Database::fetch_array($result, 'ASSOC')) {
- if (DocumentManager::is_folder_to_avoid($row['path'])) {
- continue;
- }
- $document_folders[$row['id']] = $row['path'];
- }
-
- natsort($document_folders);
-
- return $document_folders;
- } else {
- return false;
- }
- } else {
-
-
- $session_id = api_get_session_id();
- $condition_session = api_get_session_condition($session_id);
-
- $visible_sql = "SELECT DISTINCT docs.id, path
- FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
- WHERE docs.id = last.ref
- AND docs.filetype = 'folder'
- AND last.tool = '".TOOL_DOCUMENT."'
- AND last.to_group_id = ".$to_group_id."
- AND last.visibility = 1 $condition_session AND
- last.c_id = {$_course['real_id']} AND
- docs.c_id = {$_course['real_id']} ";
- $visibleresult = Database::query($visible_sql);
- while ($all_visible_folders = Database::fetch_array($visibleresult, 'ASSOC')) {
- $visiblefolders[$all_visible_folders['id']] = $all_visible_folders['path'];
- }
-
- $session_id = api_get_session_id();
- $condition_session = api_get_session_condition($session_id);
-
- $invisible_sql = "SELECT DISTINCT docs.id, path
- FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
- WHERE docs.id = last.ref
- AND docs.filetype = 'folder'
- AND last.tool = '".TOOL_DOCUMENT."'
- AND last.to_group_id = ".$to_group_id."
- AND last.visibility = 0 $condition_session AND
- last.c_id = {$_course['real_id']} AND
- docs.c_id = {$_course['real_id']} ";
- $invisibleresult = Database::query($invisible_sql);
- while ($invisible_folders = Database::fetch_array($invisibleresult, 'ASSOC')) {
-
- $session_id = api_get_session_id();
- $condition_session = api_get_session_condition($session_id);
-
- $folder_in_invisible_sql = "SELECT DISTINCT docs.id, path
- FROM ".$TABLE_ITEMPROPERTY." AS last, ".$TABLE_DOCUMENT." AS docs
- WHERE docs.id = last.ref
- AND docs.path LIKE '".Database::escape_string($invisible_folders['path'])."/%'
- AND docs.filetype = 'folder'
- AND last.tool = '".TOOL_DOCUMENT."'
- AND last.to_group_id = ".$to_group_id."
- AND last.visibility = 1 $condition_session AND
- last.c_id = {$_course['real_id']} AND
- docs.c_id = {$_course['real_id']} ";
- $folder_in_invisible_result = Database::query($folder_in_invisible_sql);
- while ($folders_in_invisible_folder = Database::fetch_array($folder_in_invisible_result, 'ASSOC')) {
- $invisiblefolders[$folders_in_invisible_folder['id']] = $folders_in_invisible_folder['path'];
- }
- }
-
- if (is_array($visiblefolders) && is_array($invisiblefolders)) {
- $document_folders = array_diff($visiblefolders, $invisiblefolders);
- natsort($document_folders);
- return $document_folders;
- } elseif (is_array($visiblefolders)) {
-
-
- natsort($visiblefolders);
- return $visiblefolders;
- } else {
-
- return false;
- }
- }
- }
-
- public static function check_readonly($_course, $user_id, $file,$document_id = '', $to_delete = false) {
- if (!(!empty($document_id) && is_numeric($document_id))) {
- $document_id = self::get_document_id($_course, $file);
- }
- $TABLE_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
- $course_id = $_course['real_id'];
- if ($to_delete) {
- if (self::is_folder($_course, $document_id)) {
- if (!empty($file)) {
- $path = Database::escape_string($file);
- $what_to_check_sql = "SELECT td.id, readonly, tp.insert_user_id FROM ".$TABLE_DOCUMENT." td , $TABLE_PROPERTY tp
- WHERE td.c_id = $course_id AND
- tp.c_id = $course_id AND
- tp.ref= td.id AND
- (path='".$path."' OR path LIKE BINARY '".$path."/%' ) ";
-
- $what_to_check_result = Database::query($what_to_check_sql);
- if ($what_to_check_result && Database::num_rows($what_to_check_result) != 0) {
-
- $readonly_set = false;
- while ($row = Database::fetch_array($what_to_check_result)) {
-
- if ($row['readonly'] == 1) {
- if (!($row['insert_user_id'] == $user_id)) {
- $readonly_set = true;
- break;
- }
- }
- }
- if ($readonly_set) {
- return true;
- }
- }
- }
- return false;
- }
- }
- if (!empty($document_id)) {
- $sql= "SELECT a.insert_user_id, b.readonly FROM $TABLE_PROPERTY a, $TABLE_DOCUMENT b
- WHERE
- a.c_id = $course_id AND
- b.c_id = $course_id AND
- a.ref = b.id and a.ref= $document_id LIMIT 1";
- $resultans = Database::query($sql);
- $doc_details = Database ::fetch_array($resultans, 'ASSOC');
- if ($doc_details['readonly'] == 1) {
- return !($doc_details['insert_user_id'] == $user_id || api_is_platform_admin());
- }
- }
- return false;
- }
-
- public static function is_folder($_course, $document_id) {
- $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
- $course_id = $_course['real_id'];
- $document_id = Database::escape_string($document_id);
- $result = Database::fetch_array(Database::query("SELECT filetype FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND id= $document_id"), 'ASSOC');
- return $result['filetype'] == 'folder';
- }
-
- public static function delete_document_from_db($document_id, $course_info = array(), $session_id = 0, $remove_content_from_db = false) {
- $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
- $TABLE_ITEMPROPERTY = Database :: get_course_table(TABLE_ITEM_PROPERTY);
-
-
- $user_id = api_get_user_id();
-
- if (empty($course_info)) {
- $course_info = api_get_course_info();
- }
- if (empty($session_id)) {
- $session_id = api_get_session_id();
- }
-
-
- api_item_property_update($course_info, TOOL_DOCUMENT, $document_id, 'delete', $user_id, null, null, null, null, $session_id);
- self::delete_document_from_search_engine($course_info['code'], $document_id);
- self::unset_document_as_template($document_id, $course_info['code'], $user_id);
-
-
- if ($remove_content_from_db) {
- $sql = "DELETE FROM $TABLE_ITEMPROPERTY WHERE c_id = {$course_info['real_id']} AND ref = ".$document_id." AND tool='".TOOL_DOCUMENT."'";
- Database::query($sql);
-
- $sql = "DELETE FROM ".$TABLE_DOCUMENT." WHERE c_id = {$course_info['real_id']} AND id = ".$document_id;
- Database::query($sql);
-
- self::delete_document_metadata($document_id);
- }
- }
-
- public static function delete_document_metadata($document_id) {
-
- require_once api_get_path(SYS_CODE_PATH).'metadata/md_funcs.php';
- require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
- $mdStore = new mdstore(true);
-
-
- $eid = 'Document'.'.'.$document_id;
- $mdStore->mds_delete($eid);
- $mdStore->mds_delete_offspring($eid);
- }
-
-
- public static function delete_document($_course, $path, $base_work_dir) {
- $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
-
- if (empty($path) || empty($base_work_dir)) {
- return false;
- }
-
- $course_id = $_course['real_id'];
-
- $document_id = self :: get_document_id($_course, $path);
- $document_exists_in_disk = file_exists($base_work_dir.$path);
-
- $new_path = $path.'_DELETED_'.$document_id;
- $current_session_id = api_get_session_id();
-
- $file_deleted_from_db = false;
- $file_deleted_from_disk = false;
- $file_renamed_from_disk = false;
-
- if ($document_id) {
- self::delete_document_from_db($document_id);
-
-
- $file_deleted_from_db = true;
- }
-
- if ($document_exists_in_disk) {
-
- if (api_get_setting('permanently_remove_deleted_files') == 'true') {
-
- $what_to_delete_sql = "SELECT id FROM ".$TABLE_DOCUMENT." WHERE c_id = $course_id AND path='".$path."' OR path LIKE BINARY '".$path."/%'";
-
- $what_to_delete_result = Database::query($what_to_delete_sql);
- if ($what_to_delete_result && Database::num_rows($what_to_delete_result) != 0) {
-
- while ($row = Database::fetch_array($what_to_delete_result)) {
-
- self::delete_document_from_db($row['id'], $_course, $current_session_id, true);
- }
-
-
-
- my_delete($base_work_dir.$path);
- $file_deleted_from_disk = true;
- }
- } else {
-
-
- if (is_file($base_work_dir.$path) || is_dir($base_work_dir.$path)) {
- if (rename($base_work_dir.$path, $base_work_dir.$new_path)) {
-
- $sql = "UPDATE $TABLE_DOCUMENT set path='".$new_path."' WHERE c_id = $course_id AND id='".$document_id."'";
- Database::query($sql);
-
- $sql = "SELECT id, path FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND path LIKE BINARY '".$path."/%'";
- $result = Database::query($sql);
- if ($result && Database::num_rows($result) > 0) {
- while ($deleted_items = Database::fetch_array($result, 'ASSOC')) {
- self::delete_document_from_db($deleted_items['id']);
-
-
- $old_item_path = $deleted_items['path'];
- $new_item_path = $new_path.substr($old_item_path, strlen($path));
- $sql = "UPDATE $TABLE_DOCUMENT set path = '".$new_item_path."' WHERE c_id = $course_id AND id = ".$deleted_items['id'];
- Database::query($sql);
- }
- }
- $file_renamed_from_disk = true;
- } else {
-
- error_log(__FILE__.' '.__LINE__.': Error renaming '.$base_work_dir.$path.' to '.$base_work_dir.$new_path.'. This is probably due to file permissions',0);
- }
- }
- }
- }
-
-
- if ($file_deleted_from_db && $file_deleted_from_disk ||
- $file_deleted_from_db && $file_renamed_from_disk) {
- return true;
- } else {
-
-
-
-
-
-
- error_log(__FILE__.' '.__LINE__.': System inconsistency detected. The file or directory '.$base_work_dir.$path.' seems to have been removed from the filesystem independently from the web platform. To restore consistency, the elements using the same path will be removed from the database',0);
- return false;
- }
- }
-
- public static function delete_document_from_search_engine ($course_id, $document_id) {
-
- if (api_get_setting('search_enabled') == 'true') {
- $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
- $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
- $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_DOCUMENT, $document_id);
- $res = Database::query($sql);
- if (Database::num_rows($res) > 0) {
- $row2 = Database::fetch_array($res);
- require_once api_get_path(LIBRARY_PATH) .'search/ChamiloIndexer.class.php';
- $di = new ChamiloIndexer();
- $di->remove_document((int)$row2['search_did']);
- }
- $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
- $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_DOCUMENT, $document_id);
- Database::query($sql);
-
- require_once api_get_path(LIBRARY_PATH) .'specific_fields_manager.lib.php';
- delete_all_values_for_item($course_id, TOOL_DOCUMENT, $document_id);
- }
- }
-
- public static function get_document_id($course_info, $path) {
- $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
- $course_id = $course_info['real_id'];
- $path = Database::escape_string($path);
- if (!empty($course_id) && !empty($path)) {
- $sql = "SELECT id FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND path LIKE BINARY '$path' LIMIT 1";
- $result = Database::query($sql);
- if ($result && Database::num_rows($result)) {
- $row = Database::fetch_array($result);
- return intval($row[0]);
- }
- }
- return false;
- }
-
- public static function get_document_data_by_id($id, $course_code, $load_parents = false) {
- $course_info = api_get_course_info($course_code);
- $course_id = $course_info['real_id'];
- if (empty($course_info)) {
- return false;
- }
- $www = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document';
- $TABLE_DOCUMENT = Database :: get_course_table(TABLE_DOCUMENT);
- $id = intval($id);
- $sql = "SELECT * FROM $TABLE_DOCUMENT WHERE c_id = $course_id AND id = $id ";
- $result = Database::query($sql);
- if ($result && Database::num_rows($result) == 1) {
- $row = Database::fetch_array($result,'ASSOC');
-
- $url_path = urlencode($row['path']);
- $path = str_replace('%2F', '/',$url_path);
- $row['url'] = api_get_path(WEB_CODE_PATH).'document/showinframes.php?cidReq='.$course_code.'&id='.$id;
- $row['document_url'] = api_get_path(WEB_CODE_PATH).'document/document.php?cidReq='.$course_code.'&id='.$id;
- $row['absolute_path'] = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$row['path'];
-
- $row['absolute_path_from_document'] = '/document'.$row['path'];
-
- $pathinfo = pathinfo($row['path']);
- $row['absolute_parent_path'] = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$pathinfo['dirname'].'/';
- $row['direct_url'] = $www.$path;
- if (dirname($row['path']) == '.') {
- $row['parent_id'] = '0';
- } else {
- $row['parent_id'] = self::get_document_id($course_info, dirname($row['path']));
- }
- $parents = array();
-
-
- $visibility = true;
- if ($load_parents) {
- $dir_array = explode('/', $row['path']);
- $dir_array = array_filter($dir_array);
- $array_len = count($dir_array) +1 ;
- $real_dir = '';
- for ($i = 1; $i < $array_len; $i++) {
-
- $real_dir .= '/'.$dir_array[$i];
- $parent_id = self::get_document_id($course_info, $real_dir);
- if (!empty($parent_id)) {
- $sub_document_data = self::get_document_data_by_id($parent_id, $course_code, false);
-
-
- $parents[] = $sub_document_data;
- }
- }
- }
-
- $row['parents'] = $parents;
- return $row;
- }
- return false;
- }
-
- public static function set_document_as_template($title, $description, $document_id_for_template, $couse_code, $user_id, $image) {
-
- $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
-
- $sql = "INSERT INTO ".$table_template."
- (title, description, course_code, user_id, ref_doc, image)
- VALUES (
- '".Database::escape_string($title)."',
- '".Database::escape_string($description)."',
- '".Database::escape_string($couse_code)."',
- '".Database::escape_string($user_id)."',
- '".Database::escape_string($document_id_for_template)."',
- '".Database::escape_string($image)."')";
- Database::query($sql);
- return true;
- }
-
- public static function unset_document_as_template($document_id, $course_code, $user_id) {
- $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
- $course_code = Database::escape_string($course_code);
- $user_id = Database::escape_string($user_id);
- $document_id = Database::escape_string($document_id);
- $sql = 'SELECT id FROM '.$table_template.' WHERE course_code="'.$course_code.'" AND user_id="'.$user_id.'" AND ref_doc="'.$document_id.'"';
- $result = Database::query($sql);
- $template_id = Database::result($result,0,0);
- include_once(api_get_path(LIBRARY_PATH) . 'fileManage.lib.php');
- my_delete(api_get_path(SYS_CODE_PATH).'upload/template_thumbnails/'.$template_id.'.jpg');
- $sql = 'DELETE FROM '.$table_template.' WHERE course_code="'.$course_code.'" AND user_id="'.$user_id.'" AND ref_doc="'.$document_id.'"';
- Database::query($sql);
- }
-
- public static function is_visible($doc_path, $course, $session_id = 0, $file_type = 'file') {
- $docTable = Database::get_course_table(TABLE_DOCUMENT);
- $propTable = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $course_id = $course['real_id'];
-
- $doc_path = Database::escape_string($doc_path);
- $session_id = intval($session_id);
- $condition = "AND id_session IN ('$session_id', '0') ";
-
-
- if (strpos($doc_path, 'HotPotatoes_files') && preg_match("/\.t\.html$/", $doc_path)) {
- $doc_path = substr($doc_path, 0, strlen($doc_path) - 7 - strlen(api_get_user_id()));
- }
- if (!in_array($file_type, array('file','folder'))) {
- $file_type = 'file';
- }
- $sql = "SELECT visibility FROM $docTable d INNER JOIN $propTable ip
- ON (d.id = ip.ref AND d.c_id = $course_id AND ip.c_id = $course_id)
- WHERE ip.tool = '" . TOOL_DOCUMENT . "' $condition AND
- filetype = '$file_type' AND locate(concat(path,'/'),'".$doc_path."/')=1";
- $result = Database::query($sql);
- $is_visible = false;
- if (Database::num_rows($result) > 0) {
- $row = Database::fetch_array($result,'ASSOC');
- if ($row['visibility'] == 1) {
- $is_visible = $_SESSION['is_allowed_in_course'] || api_is_platform_admin();
- }
- }
-
-
- return $is_visible;
- }
-
- public static function is_visible_by_id($doc_id, $course_info, $session_id, $user_id, $admins_can_see_everything = true) {
- $is_visible = false;
- $user_in_course = false;
-
- if (empty($course_info)) {
- $course_info = api_get_course_info();
- if (empty($course_info)) {
- return false;
- }
- }
- $doc_id = intval($doc_id);
- $session_id = intval($session_id);
-
-
- if ($session_id == 0 ) {
- if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code']) || api_is_platform_admin()) {
- $user_in_course = true;
- }
-
- if (isset($course_info) && in_array($course_info['visibility'], array(2, 3))) {
- $user_in_course = true;
- }
- } else {
- $user_status = SessionManager::get_user_status_in_course_session($user_id, $course_info['code'], $session_id);
- if (in_array($user_status, array('0', '2', '6'))) {
-
- $user_in_course = true;
- }
- }
-
- if ($user_in_course) {
-
- if ($session_id == 0) {
- $item_info = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, 0);
- if (isset($item_info['visibility'])) {
-
- if ($admins_can_see_everything && api_is_platform_admin()) {
- return true;
- }
- if ($item_info['visibility'] == 1) {
- return true;
- }
- }
- } else {
-
- $item_info = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, 0);
- $item_info_in_session = api_get_item_property_info($course_info['real_id'], 'document', $doc_id, $session_id);
-
- if (isset($item_info['visibility'])) {
- if ($admins_can_see_everything && api_is_platform_admin())
- return true;
- }
- if (isset($item_info_in_session['visibility'])) {
-
- if ($item_info_in_session['visibility'] == 1) {
- return true;
- }
- } else {
- if ($item_info['visibility'] == 1) {
- return true;
- }
- }
- }
- } elseif ($admins_can_see_everything && api_is_platform_admin()) {
- return true;
- }
- return false;
- }
-
- function attach_gradebook_certificate ($course_id, $document_id) {
- $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
- $session_id = api_get_session_id();
- if ($session_id==0 || is_null($session_id)) {
- $sql_session='AND (session_id='.Database::escape_string($session_id).' OR isnull(session_id)) ';
- } elseif ($session_id>0) {
- $sql_session='AND session_id='.Database::escape_string($session_id);
- } else {
- $sql_session='';
- }
- $sql='UPDATE '.$tbl_category.' SET document_id="'.Database::escape_string($document_id).'"
- WHERE course_code="'.Database::escape_string($course_id).'" '.$sql_session;
- Database::query($sql);
- }
-
- static function get_default_certificate_id($course_id) {
- $tbl_category = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
- $session_id = api_get_session_id();
- if ($session_id==0 || is_null($session_id)) {
- $sql_session='AND (session_id='.Database::escape_string($session_id).' OR isnull(session_id)) ';
- } elseif ($session_id>0) {
- $sql_session='AND session_id='.Database::escape_string($session_id);
- } else {
- $sql_session='';
- }
- $sql = 'SELECT document_id FROM '.$tbl_category.' WHERE course_code="'.Database::escape_string($course_id).'" '.$sql_session;
- $rs = Database::query($sql);
- $num = Database::num_rows($rs);
- if ($num == 0) {
- return null;
- }
- $row = Database::fetch_array($rs);
- return $row['document_id'];
- }
-
- static function replace_user_info_into_html($user_id, $course_code, $is_preview = false) {
- $user_id = intval($user_id);
- $course_info = api_get_course_info($course_code);
- $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
- $course_id = $course_info['real_id'];
- $document_id = self::get_default_certificate_id($course_code);
- if ($document_id) {
- $sql = "SELECT path FROM $tbl_document WHERE c_id = $course_id AND id = $document_id";
- $rs = Database::query($sql);
- $new_content = '';
- $all_user_info = array();
- if (Database::num_rows($rs)) {
- $row=Database::fetch_array($rs);
- $filepath = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$row['path'];
- if (is_file($filepath)) {
- $my_content_html = file_get_contents($filepath);
- }
- $all_user_info = self::get_all_info_to_certificate($user_id, $course_code, $is_preview);
- $info_to_be_replaced_in_content_html=$all_user_info[0];
- $info_to_replace_in_content_html=$all_user_info[1];
- $new_content=str_replace($info_to_be_replaced_in_content_html,$info_to_replace_in_content_html, $my_content_html);
- }
- return array('content' => $new_content, 'variables' => $all_user_info);
- }
- return array();
- }
-
- static function get_all_info_to_certificate($user_id, $course_id, $is_preview = false) {
- $info_list = array();
- $user_id = intval($user_id);
- $course_info = api_get_course_info($course_id);
-
- $organization_name = api_get_setting('Institution');
- $portal_name = api_get_setting('siteName');
-
- $extra_user_info_data = UserManager::get_extra_user_data($user_id, false, false, false, true);
-
- $user_info = api_get_user_info($user_id);
- $first_name = $user_info['firstname'];
- $last_name = $user_info['lastname'];
- $official_code = $user_info['official_code'];
-
- $info_teacher_id = UserManager::get_user_id_of_course_admin_or_session_admin($course_id);
- $teacher_info = api_get_user_info($info_teacher_id);
- $teacher_first_name = $teacher_info['firstname'];
- $teacher_last_name = $teacher_info['lastname'];
-
- $info_grade_certificate = UserManager::get_info_gradebook_certificate($course_id, $user_id);
- $date_certificate = $info_grade_certificate['created_at'];
- $date_long_certificate = '';
- if (!empty($date_certificate)) {
- $date_long_certificate = api_convert_and_format_date($date_certificate);
- $date_no_time = api_convert_and_format_date($date_certificate, DATE_FORMAT_LONG_NO_DAY);
- }
- if ($is_preview) {
- $date_long_certificate = api_convert_and_format_date(api_get_utc_datetime());
- $date_no_time = api_convert_and_format_date(api_get_utc_datetime(), DATE_FORMAT_LONG_NO_DAY);
- }
- $url = api_get_path(WEB_PATH).'certificates/index.php?id='.$info_grade_certificate['id'];
-
- $info_to_replace_in_content_html = array($first_name,
- $last_name,
- $organization_name,
- $portal_name,
- $teacher_first_name,
- $teacher_last_name,
- $official_code,
- $date_long_certificate,
- $date_no_time,
- $course_id,
- $course_info['name'],
- $info_grade_certificate['grade'],
- $url,
- '<a href="'.$url.'" target="_blank">'.get_lang('CertificateOnlineLink').'</a>',
- '((certificate_barcode))',
- );
- $info_to_be_replaced_in_content_html = array('((user_firstname))',
- '((user_lastname))',
- '((gradebook_institution))',
- '((gradebook_sitename))',
- '((teacher_firstname))',
- '((teacher_lastname))',
- '((official_code))',
- '((date_certificate))',
- '((date_certificate_no_time))',
- '((course_code))',
- '((course_title))',
- '((gradebook_grade))',
- '((certificate_link))',
- '((certificate_link_html))',
- '((certificate_barcode))',
- );
- if (!empty($extra_user_info_data)) {
- foreach ($extra_user_info_data as $key_extra=>$value_extra) {
- $info_to_be_replaced_in_content_html[]='(('.strtolower($key_extra).'))';
- $info_to_replace_in_content_html[]=$value_extra;
- }
- }
- $info_list[]=$info_to_be_replaced_in_content_html;
- $info_list[]=$info_to_replace_in_content_html;
- return $info_list;
- }
-
- function remove_attach_certificate ($course_id,$default_certificate_id) {
- $default_certificate=self::get_default_certificate_id($course_id);
- if ((int)$default_certificate==(int)$default_certificate_id) {
- $tbl_category=Database :: get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
- $session_id = api_get_session_id();
- if ($session_id==0 || is_null($session_id)) {
- $sql_session='AND (session_id='.Database::escape_string($session_id).' OR isnull(session_id)) ';
- } elseif ($session_id>0) {
- $sql_session='AND session_id='.Database::escape_string($session_id);
- } else {
- $sql_session='';
- }
- $sql='UPDATE '.$tbl_category.' SET document_id=null
- WHERE course_code="'.Database::escape_string($course_id).'" AND document_id="'.$default_certificate_id.'" '.$sql_session;
- Database::query($sql);
- }
- }
-
- static function create_directory_certificate_in_course ($course_id) {
- $course_info = api_get_course_info($course_id);
- if (!empty($course_info)) {
- $to_group_id=0;
- $to_user_id=null;
- $course_dir = $course_info['path']."/document/";
- $sys_course_path = api_get_path(SYS_COURSE_PATH);
- $base_work_dir=$sys_course_path.$course_dir;
- $base_work_dir_test=$base_work_dir.'certificates';
- $dir_name='/certificates';
- $post_dir_name=get_lang('CertificatesFiles');
- $visibility_command = 'invisible';
-
- if (!is_dir($base_work_dir_test)) {
- $created_dir = create_unexisting_directory($course_info, api_get_user_id(), api_get_session_id(), $to_group_id,$to_user_id,$base_work_dir,$dir_name,$post_dir_name);
- $update_id = self::get_document_id_of_directory_certificate();
- api_item_property_update($course_info, TOOL_DOCUMENT, $update_id, $visibility_command, api_get_user_id());
- }
- }
- }
-
- static function get_document_id_of_directory_certificate () {
- $tbl_document=Database::get_course_table(TABLE_DOCUMENT);
- $course_id = api_get_course_int_id();
- $sql = "SELECT id FROM $tbl_document WHERE c_id = $course_id AND path='/certificates' ";
- $rs = Database::query($sql);
- $row = Database::fetch_array($rs);
- return $row['id'];
- }
-
- static function is_certificate_mode($dir) {
-
- $is_certificate_mode = false;
- $is_certificate_array = explode('/',$dir);
- array_shift($is_certificate_array);
- if (isset($is_certificate_array[0]) && $is_certificate_array[0] == 'certificates') {
- $is_certificate_mode = true;
- }
- return $is_certificate_mode;
- }
-
- static function get_resources_from_source_html($source_html, $is_file = false, $type = null, $recursivity = 1) {
- $max = 5;
- $attributes = array();
- $wanted_attributes = array('src', 'url', '@import', 'href', 'value', 'flashvars');
- $explode_attributes = array('flashvars' => 'file');
- $abs_path = '';
- if ($recursivity > $max) {
- return array();
- }
- if (!isset($type)) {
- $type = TOOL_DOCUMENT;
- }
- if (!$is_file) {
- $attributes = self::parse_HTML_attributes($source_html, $wanted_attributes, $explode_attributes);
- } else {
- if (is_file($source_html)) {
- $abs_path = $source_html;
-
- $info = pathinfo($abs_path);
- $ext = $info['extension'];
- switch (strtolower($ext)) {
- case 'html' :
- case 'htm' :
- case 'shtml':
- case 'css' :
- $file_content = file_get_contents($abs_path);
-
- $attributes = self::parse_HTML_attributes($file_content, $wanted_attributes, $explode_attributes);
- break;
- default :
- break;
- }
- } else {
- return false;
- }
- }
- switch ($type) {
- case TOOL_DOCUMENT :
- case TOOL_QUIZ:
- case 'sco':
- foreach ($wanted_attributes as $attr) {
- if (isset($attributes[$attr])) {
-
- $sources = $attributes[$attr];
- foreach ($sources as $source) {
-
- if (strpos($source, '+this.'))
- continue;
- if (strpos($source, '.') === false)
- continue;
- if (strpos($source, 'mailto:'))
- continue;
- if (strpos($source, ';') && !strpos($source, '&'))
- continue;
- if ($attr == 'value') {
- if (strpos($source , 'mp3file')) {
- $files_list[] = array(substr($source, 0, strpos($source, '.swf') + 4), 'local', 'abs');
- $mp3file = substr($source , strpos($source, 'mp3file=') + 8);
- if (substr($mp3file, 0, 1) == '/') {
- $files_list[] = array($mp3file, 'local', 'abs');
- } else {
- $files_list[] = array($mp3file, 'local', 'rel');
- }
- } elseif (strpos($source, 'flv=') === 0) {
- $source = substr($source, 4);
- if (strpos($source, '&') > 0) {
- $source = substr($source, 0, strpos($source, '&'));
- }
- if (strpos($source,'://') > 0) {
- if (strpos($source, api_get_path(WEB_PATH)) !== false) {
-
- $files_list[] = array($source, 'local', 'url');
- } else {
-
- $files_list[] = array($source, 'remote', 'url');
- }
- } else {
- $files_list[] = array($source, 'local', 'abs');
- }
- continue;
- }
- }
- if (strpos($source, '://') > 0) {
-
- if (strpos($source, '?') > 0) {
- $second_part = substr($source,strpos($source, '?'));
- if(strpos($second_part, '://') > 0) {
-
- $pos1 = strpos($second_part, '=');
- $pos2 = strpos($second_part, '&');
- $second_part = substr($second_part, $pos1 + 1, $pos2 - ($pos1 + 1));
- if (strpos($second_part, api_get_path(WEB_PATH)) !== false) {
-
- $files_list[] = array($second_part, 'local', 'url');
- $in_files_list[] = self::get_resources_from_source_html($second_part, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- } else {
-
- $files_list[] = array($second_part, 'remote', 'url');
- }
- } elseif (strpos($second_part, '=') > 0) {
- if (substr($second_part, 0, 1) === '/') {
-
- $files_list[] = array($second_part, 'local', 'abs');
- $in_files_list[] = self::get_resources_from_source_html($second_part, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- } elseif(strstr($second_part, '..') === 0) {
-
- $files_list[] = array($second_part, 'local', 'rel');
-
-
- $dir = '';
- if (!empty($abs_path)) {
- $dir = dirname($abs_path).'/';
- }
- $new_abs_path = realpath($dir.$second_part);
- $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- } else {
-
- if (substr($second_part, 0, 2) == './') {
- $second_part = substr($second_part, 2);
- }
- $files_list[] = array($second_part, 'local', 'rel');
- $dir = '';
- if (!empty($abs_path)) {
- $dir = dirname($abs_path).'/';
- }
- $new_abs_path = realpath($dir.$second_part);
- $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- }
- }
-
- $source = substr($source, 0, strpos($source, '?'));
- if (strpos($source, '://') > 0) {
- if (strpos($source, api_get_path(WEB_PATH)) !== false) {
-
- $files_list[] = array($source, 'local', 'url');
- $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity+1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- } else {
-
- $files_list[] = array($source, 'remote', 'url');
- }
- } else {
-
- if (substr($source, 0, 1) === '/') {
-
- $files_list[] = array($source, 'local', 'abs');
- $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- } elseif (strstr($source, '..') === 0) {
- $files_list[] = array($source, 'local', 'rel');
- $dir = '';
- if (!empty($abs_path)) {
- $dir = dirname($abs_path).'/';
- }
- $new_abs_path = realpath($dir.$source);
- $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- } else {
-
- if (substr($source, 0, 2) == './') {
- $source = substr($source, 2);
- }
- $files_list[] = array($source, 'local', 'rel');
- $dir = '';
- if (!empty($abs_path)) {
- $dir = dirname($abs_path).'/';
- }
- $new_abs_path = realpath($dir.$source);
- $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- }
- }
- }
-
- if (strpos($source, api_get_path(WEB_PATH)) !== false) {
-
- $files_list[] = array($source, 'local', 'url');
- $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- } else {
-
- $files_list[] = array($source, 'remote', 'url');
- }
- } else {
-
- if (substr($source, 0, 1) === '/') {
-
- $files_list[] = array($source, 'local', 'abs');
- $in_files_list[] = self::get_resources_from_source_html($source, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- } elseif (strpos($source, '..') === 0) {
-
- $files_list[] = array($source, 'local', 'rel');
- $dir = '';
- if (!empty($abs_path)) {
- $dir = dirname($abs_path).'/';
- }
- $new_abs_path = realpath($dir.$source);
- $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- } else {
-
- if (substr($source, 0, 2) == './') {
- $source = substr($source, 2);
- }
- $files_list[] = array($source, 'local', 'rel');
- $dir = '';
- if (!empty($abs_path)) {
- $dir = dirname($abs_path).'/';
- }
- $new_abs_path = realpath($dir.$source);
- $in_files_list[] = self::get_resources_from_source_html($new_abs_path, true, TOOL_DOCUMENT, $recursivity + 1);
- if (count($in_files_list) > 0) {
- $files_list = array_merge($files_list, $in_files_list);
- }
- }
- }
- }
- }
- }
- break;
- default:
- break;
- }
- $checked_files_list = array();
- $checked_array_list = array();
- if (count($files_list ) > 0) {
- foreach ($files_list as $idx => $file) {
- if (!empty($file[0])) {
- if (!in_array($file[0], $checked_files_list)) {
- $checked_files_list[] = $files_list[$idx][0];
- $checked_array_list[] = $files_list[$idx];
- }
- }
- }
- }
- return $checked_array_list;
- }
-
- static function parse_HTML_attributes($attrString, $wanted = array(), $explode_variables = array()) {
- $attributes = array();
- $regs = array();
- $reduced = false;
- if (count($wanted) > 0) {
- $reduced = true;
- }
- try {
-
-
-
-
-
-
-
-
- $res = preg_match_all(
- '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]*))' .
-
-
- '([ \n\t\r]+)?(' .
-
- '(=([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+))' .
- '|' .
-
- '(\(([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+)\))' .
- '))' .
- '|' .
-
- '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/',
- $attrString,
- $regs
- );
- } catch (Exception $e) {
- error_log('Caught exception: '. $e->getMessage(), 0) ;
- }
- if ($res) {
- for ($i = 0; $i < count($regs[1]); $i++) {
- $name = trim($regs[3][$i]);
- $check = trim($regs[0][$i]);
- $value = trim($regs[10][$i]);
- if (empty($value) and !empty($regs[13][$i])) {
- $value = $regs[13][$i];
- }
- if (empty($name) && !empty($regs[16][$i])) {
- $name = '@import';
- $value = trim($regs[16][$i]);
- }
- if (!empty($name)) {
- if (!$reduced OR in_array(strtolower($name), $wanted)) {
- if ($name == $check) {
- $attributes[strtolower($name)][] = strtolower($name);
- } else {
- if (!empty($value) && ($value[0] == '\'' || $value[0] == '"')) {
- $value = substr($value, 1, -1);
- }
- if ($value == 'API.LMSGetValue(name') {
- $value = 'API.LMSGetValue(name)';
- }
-
- if (isset($explode_variables[$name])) {
- $value_modified = str_replace('&', '&', $value);
- $value_array = explode('&', $value_modified);
- foreach ($value_array as $item) {
- list($key, $item_value) = explode('=', $item);
- if ($key == $explode_variables[$name]) {
- $attributes[strtolower($name)][] = $item_value;
- }
- }
- }
- $attributes[strtolower($name)][] = $value;
- }
- }
- }
- }
- } else {
-
- }
- return $attributes;
- }
-
- static function replace_urls_inside_content_html_from_copy_course($content_html, $origin_course_code, $destination_course_directory, $origin_course_path_from_zip = null, $origin_course_info_path = null) {
- require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
- if (empty($content_html)) {
- return false;
- }
- $orig_source_html = DocumentManager::get_resources_from_source_html($content_html);
- $orig_course_info = api_get_course_info($origin_course_code);
-
- if (empty($orig_course_info)) {
- if (!empty($origin_course_path_from_zip)) {
- $orig_course_path = $origin_course_path_from_zip.'/';
- $orig_course_info_path = $origin_course_info_path;
- }
- } else {
- $orig_course_path = api_get_path(SYS_PATH).'courses/'.$orig_course_info['path'].'/';
- $orig_course_info_path = $orig_course_info['path'];
- }
- $destination_course_code = CourseManager::get_course_id_from_path ($destination_course_directory);
- $destination_course_info = api_get_course_info($destination_course_code);
- $dest_course_path = api_get_path(SYS_COURSE_PATH).$destination_course_directory.'/';
-
- $user_id = api_get_user_id();
-
- if (!empty($orig_source_html)) {
- foreach ($orig_source_html as $source) {
-
- $real_orig_url = $source[0];
- $scope_url = $source[1];
- $type_url = $source[2];
-
- $orig_parse_url = parse_url($real_orig_url);
- $real_orig_path = $orig_parse_url['path'];
- $real_orig_query = $orig_parse_url['query'];
-
- $dest_url_query = '';
-
- if (!empty($real_orig_query)) {
- $dest_url_query = '?'.$real_orig_query;
- if (strpos($dest_url_query,$origin_course_code) !== false) {
- $dest_url_query = str_replace($origin_course_code, $destination_course_code, $dest_url_query);
- }
- }
- if ($scope_url == 'local') {
- if ($type_url == 'abs' || $type_url == 'rel') {
- $document_file = strstr($real_orig_path, 'document');
-
- if (strpos($real_orig_path,$document_file) !== false) {
- $origin_filepath = $orig_course_path.$document_file;
- $destination_filepath = $dest_course_path.$document_file;
-
- if (file_exists($origin_filepath)) {
- $filepath_dir = dirname($destination_filepath);
-
- if (!is_dir($filepath_dir)) {
- $perm = api_get_permissions_for_new_directories();
- $result = @mkdir($filepath_dir, $perm, true);
- if ($result) {
- $filepath_to_add = str_replace(array($dest_course_path, 'document'), '', $filepath_dir);
-
-
- $doc_id = add_document($destination_course_info, $filepath_to_add, 'folder', 0, basename($filepath_to_add));
- api_item_property_update($destination_course_info, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $user_id, null, null, null, null);
- }
- }
-
- if (!file_exists($destination_filepath)) {
- $result = @copy($origin_filepath, $destination_filepath);
- if ($result) {
-
- $filepath_to_add = str_replace(array($dest_course_path, 'document'), '', $destination_filepath);
- $size = filesize($destination_filepath);
-
-
- $doc_id = add_document($destination_course_info, $filepath_to_add, 'file', $size, basename($filepath_to_add));
- api_item_property_update($destination_course_info, TOOL_DOCUMENT, $doc_id, 'FolderCreated', $user_id, null, null, null, null);
- }
-
- }
- }
-
- if (strpos($content_html,$real_orig_url) !== false) {
-
- $url_course_path = str_replace($orig_course_info_path.'/'.$document_file, '', $real_orig_path);
- $destination_url = $url_course_path.$destination_course_directory.'/'.$document_file.$dest_url_query;
-
- if (strpos($real_orig_path, $origin_course_code) === false) {
- $url_course_path = $real_orig_path;
- $destination_url = $real_orig_path;
- }
- $content_html = str_replace($real_orig_url, $destination_url, $content_html);
- }
- }
-
- if (strpos($real_orig_url, '?') === 0) {
- $dest_url = str_replace($origin_course_code, $destination_course_code, $real_orig_url);
- $content_html = str_replace($real_orig_url, $dest_url, $content_html);
- }
- } else {
- if ($type_url == 'url') {
- }
- }
- }
- }
- }
- return $content_html;
- }
-
- function replace_urls_inside_content_html_when_moving_file($file_name, $original_path, $destiny_path) {
- if (substr($original_path, strlen($original_path) -1 , strlen($original_path)) == '/') {
- $original = $original_path.$file_name;
- } else {
- $original = $original_path.'/'.$file_name;
- }
- if (substr($destiny_path, strlen($destiny_path) -1 , strlen($destiny_path)) == '/') {
- $destination = $destiny_path.$file_name;
- } else {
- $destination = $destiny_path.'/'.$file_name;
- }
-
- $original_count = count(explode('/', $original));
- $destination_count = count(explode('/', $destination));
- if ($original_count == $destination_count) {
-
- return true;
- }
- $mode = '';
- if ($original_count > $destination_count) {
- $mode = 'outside';
- } else {
- $mode = 'inside';
- }
-
-
- $content_html = file_get_contents($destiny_path.'/'.$file_name);
- $destination_file = $destiny_path.'/'.$file_name;
- $pre_original = strstr($original_path, 'document');
- $pre_destin = strstr($destiny_path, 'document');
-
-
- $pre_original = substr($pre_original, 8, strlen($pre_original));
- $pre_destin = substr($pre_destin, 8, strlen($pre_destin));
-
-
- $levels = count(explode('/', $pre_destin)) - 1 ;
- $link_to_add = '';
- for ($i=1; $i <=$levels ; $i++) {
- $link_to_add .= '../';
- }
- if ($pre_original == '/') {
- $pre_original = '';
- }
- if ($pre_destin == '/') {
- $pre_destin = '';
- }
- if ($pre_original != '') {
- $pre_original = '..'.$pre_original.'/';
- }
- if ($pre_destin != '') {
- $pre_destin = '..'.$pre_destin.'/';
- }
-
- $levels = explode('/', $pre_original);
-
- $count_pre_destination_levels = 0;
- foreach($levels as $item) {
- if (!empty($item) && $item != '..') {
- $count_pre_destination_levels++;
- }
- }
- $count_pre_destination_levels--;
-
- if ($count_pre_destination_levels == 0 ) {
- $count_pre_destination_levels = 1;
- }
-
- $pre_remove = '';
- for ($i=1; $i <= $count_pre_destination_levels; $i++) {
- $pre_remove .='..\/';
- }
- $orig_source_html = DocumentManager::get_resources_from_source_html($content_html);
- foreach ($orig_source_html as $source) {
-
- $real_orig_url = $source[0];
- $scope_url = $source[1];
- $type_url = $source[2];
-
- $orig_parse_url = parse_url($real_orig_url);
- $real_orig_path = $orig_parse_url['path'];
- $real_orig_query = $orig_parse_url['query'];
-
-
- if ($scope_url == 'local') {
- if ( $type_url == 'abs' || $type_url == 'rel') {
- $document_file = strstr($real_orig_path, 'document');
- if (strpos($real_orig_path, $document_file) !== false) {
- echo 'continue1';
- continue;
- } else {
- $real_orig_url_temp = '';
- if ($mode == 'inside') {
- $real_orig_url_temp = str_replace('../', '', $real_orig_url);
- $destination_url = $link_to_add.$real_orig_url_temp;
- } else {
- $real_orig_url_temp = $real_orig_url;
- $destination_url = preg_replace("/".$pre_remove."/", '', $real_orig_url, 1);
- }
- if ($real_orig_url == $destination_url) {
-
- continue;
- }
- $content_html = str_replace($real_orig_url, $destination_url, $content_html);
- }
- } else {
- echo 'continue3';
- continue;
- }
- }
- }
- $return = file_put_contents($destination, $content_html);
- return $return;
- }
- public static function export_to_pdf($document_id, $course_code) {
- require_once api_get_path(LIBRARY_PATH).'pdf.lib.php';
- $course_data = api_get_course_info($course_code);
- $document_data = self::get_document_data_by_id($document_id, $course_code);
- $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$document_data['path'];
- $pdf = new PDF();
- $pdf->html_to_pdf($file_path, $document_data['title'], $course_code);
- }
-
- public static function upload_document($files, $path, $title = null, $comment = null, $unzip = 0, $if_exists = null, $index_document = false, $show_output = false) {
- require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
- $course_info = api_get_course_info();
- $course_dir = $course_info['path'].'/document';
- $sys_course_path = api_get_path(SYS_COURSE_PATH);
- $base_work_dir = $sys_course_path.$course_dir;
- if (isset($files['file'])) {
- $upload_ok = process_uploaded_file($files['file'], $show_output);
-
- if ($upload_ok) {
-
- $new_path = handle_uploaded_document($course_info, $files['file'], $base_work_dir, $path, api_get_user_id(), api_get_group_id(), null, $unzip, $if_exists, $show_output);
-
- if ($new_path) {
- $docid = DocumentManager::get_document_id($course_info, $new_path);
- if (!empty($docid)) {
- $table_document = Database::get_course_table(TABLE_DOCUMENT);
- $params = array();
-
- if (!empty($title)) {
- $params['title'] = get_document_title($title);
- } else {
- if (isset($if_exists) && $if_exists == 'rename') {
- $new_path = basename($new_path);
- $params['title'] = get_document_title($new_path);
- } else {
- $params['title'] = get_document_title($files['file']['name']);
- }
- }
-
- if (!empty($comment)) {
- $params['comment'] = trim($comment);
- }
- Database::update($table_document, $params, array('id = ? AND c_id = ? ' => array($docid, $course_info['real_id'])));
- }
-
- if ($new_path === true && $unzip == 1 && $show_output) {
- Display::display_confirmation_message(get_lang('UplUploadSucceeded').'<br />', false);
- }
-
- if ($index_document) {
- self::index_document($docid,$course_info['code'],null,$_POST['language'],$_REQUEST,$if_exists);
- }
- if (!empty($docid) && is_numeric($docid)) {
- $document_data = self::get_document_data_by_id($docid, $course_info['code']);
- return $document_data;
- }
- }
- }
- }
- return false;
- }
-
- function get_text_content($doc_path, $doc_mime) {
-
-
-
- $ret_val = null;
- switch ($doc_mime) {
- case 'text/plain':
- $handle = fopen($doc_path, 'r');
- $output = array(fread($handle, filesize($doc_path)));
- fclose($handle);
- break;
- case 'application/pdf':
- exec("pdftotext $doc_path -", $output, $ret_val);
- break;
- case 'application/postscript':
- $temp_file = tempnam(sys_get_temp_dir(), 'chamilo');
- exec("ps2pdf $doc_path $temp_file", $output, $ret_val);
- if ($ret_val !== 0) {
- return false;
- }
- exec("pdftotext $temp_file -", $output, $ret_val);
- unlink($temp_file);
- break;
- case 'application/msword':
- exec("catdoc $doc_path", $output, $ret_val);
- break;
- case 'text/html':
- exec("html2text $doc_path", $output, $ret_val);
- break;
- case 'text/rtf':
-
-
- exec("unrtf --text $doc_path", $output, $ret_val);
- if ($ret_val == 127) {
- return false;
- }
-
- if (is_array($output) && count($output) > 1) {
- $parsed_output = array();
- foreach ($output as & $line) {
- if (!preg_match('/^###/', $line, $matches)) {
- if (!empty($line)) {
- $parsed_output[] = $line;
- }
- }
- }
- $output = $parsed_output;
- }
- break;
- case 'application/vnd.ms-powerpoint':
- exec("catppt $doc_path", $output, $ret_val);
- break;
- case 'application/vnd.ms-excel':
- exec("xls2csv -c\" \" $doc_path", $output, $ret_val);
- break;
- }
- $content = '';
- if (!is_null($ret_val)) {
- if ($ret_val !== 0) {
- return false;
- }
- }
- if (isset($output)) {
- foreach ($output as & $line) {
- $content .= $line."\n";
- }
- return $content;
- } else {
- return false;
- }
- }
-
- static function documents_total_space($course_id = null, $group_id = null, $session_id = null) {
- $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
-
- if (isset($course_id)) {
- $course_id = intval($course_id);
- } else {
- $course_id = api_get_course_int_id();
- }
-
- $group_condition = null;
-
- if (isset($group_id)) {
- $group_id = intval($group_id);
- $group_condition = " AND props.to_group_id='".$group_id."' ";
- }
-
- $session_condition = null;
-
- if (isset($session_id)) {
- $session_id = intval($session_id);
- $session_condition = " AND props.id_session='".$session_id."' ";
- }
-
- $sql = "SELECT SUM(size) FROM ".$TABLE_ITEMPROPERTY." AS props, ".$TABLE_DOCUMENT." AS docs
- WHERE props.c_id = $course_id AND
- docs.c_id = $course_id AND
- docs.id = props.ref AND
- props.tool = '".TOOL_DOCUMENT."' AND
- props.visibility <> 2
- $group_condition
- $session_condition
- ";
- $result = Database::query($sql);
- if ($result && Database::num_rows($result) != 0) {
- $row = Database::fetch_row($result);
- return $row[0];
- } else {
- return 0;
- }
- }
-
- static function display_quota($course_quota, $already_consumed_space) {
- $course_quota_m = round($course_quota / 1000000);
- $already_consumed_space_m = round($already_consumed_space / 1000000);
- $message = get_lang('MaximumAllowedQuota') . ' <strong>'.$course_quota_m.' megabyte</strong>.<br />';
- $message .= get_lang('CourseCurrentlyUses') . ' <strong>' . $already_consumed_space_m . ' megabyte</strong>.<br />';
- $percentage = round( ($already_consumed_space / $course_quota * 100), 1);
- $other_percentage = $percentage < 100 ? 100 - $percentage : 0;
-
- if ($percentage >= 50) {
- $text_in_filled = ' '.$other_percentage.'%';
- $text_in_unfilled = '';
- } else {
- $text_in_unfilled = ' '.$other_percentage.'%';
- $text_in_filled = '';
- }
-
- if ($percentage < 65) {
- $colour = '#00BB00';
- } elseif ($percentage < 90) {
- $colour = '#ffd400';
- } else {
- $colour = '#DD0000';
- }
-
- $visual_percentage = 4 * $percentage;
- $visual_other_percentage = 4 * $other_percentage;
- $message .= get_lang('PercentageQuotaInUse') . ': <strong>'.$percentage.'%</strong>.<br />' .
- get_lang('PercentageQuotaFree') . ': <strong>'.$other_percentage.'%</strong>.<br />';
- $show_percentage = ' '.$percentage.'%';
- $message .= '<div style="width: 80%; text-align: center; -moz-border-radius: 5px 5px 5px 5px; border: 1px solid #aaa; background-image: url(\''.api_get_path(WEB_CODE_PATH).'css/'.api_get_visual_theme().'/images/bg-header4.png\');" class="document-quota-bar">'.
- '<div style="width:'.$percentage.'%; background-color: #bbb; border-right:3px groove #bbb; -moz-border-radius:5px;"> </div>'.
- '<span style="margin-top: -15px; margin-left:-15px; position: absolute;font-weight:bold;">'.$show_percentage.'</span></div>';
- echo $message;
- }
-
- static function display_simple_quota($course_quota, $already_consumed_space) {
- $course_quota_m = round($course_quota / 1000000);
- $already_consumed_space_m = round($already_consumed_space / 1000000, 2);
- $percentage = $already_consumed_space / $course_quota * 100;
- $percentage = round($percentage, 1);
- $message = get_lang('YouAreCurrentlyUsingXOfYourX');
- $message = sprintf($message, $already_consumed_space_m, $percentage.'%',$course_quota_m.' ');
- echo Display::div($message, array('id'=>'document_quota'));
- }
-
- static function enough_space($file_size, $max_dir_space) {
- if ($max_dir_space) {
- $already_filled_space = self::documents_total_space();
- if (($file_size + $already_filled_space) > $max_dir_space) {
- return false;
- }
- }
- return true;
- }
-
-
-
- static function generate_jplayer_jquery($params = array()) {
- $js_path = api_get_path(WEB_LIBRARY_PATH).'javascript/';
-
- $jplayer_definition = ' $("#jquery_jplayer_' . $params['count'] . '").jPlayer({
- ready: function() {
- $(this).jPlayer("setMedia", {
- ' . $params['extension'] . ' : "' . $params['url'] . '"
- });
- },
- play: function() { // To avoid both jPlayers playing together.
- $(this).jPlayer("pauseOthers");
- },
- //errorAlerts: true,
- //warningAlerts: true,
- swfPath: "' . $js_path . 'jquery-jplayer",
- //supplied: "m4a, oga, mp3, ogg, wav",
- supplied: "' . $params['extension'] . '",
- wmode: "window",
- solution: "flash, html", // Do not change this setting
- cssSelectorAncestor: "#jp_container_' . $params['count'] . '",
- }); ' . "\n\n";
- return $jplayer_definition;
- }
-
- static function generate_media_preview($i, $type = 'simple') {
- $i = intval($i);
-
- $extra_controls = $progress = '';
- if ($type == 'advanced') {
- $extra_controls = ' <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
- <li><a href="#" class="jp-mute" tabindex="1">mute</a></li>
- <li><a href="#" class="jp-unmute" tabindex="1">unmute</a></li>';
- $progress = '<div class="jp-progress">
- <div class="jp-seek-bar">
- <div class="jp-play-bar"></div>
- </div>
- </div>';
- }
-
-
- $html = '<div id="jquery_jplayer_'.$i.'" class="jp-jplayer"></div>
- <div id="jp_container_'.$i.'" class="jp-audio">
- <div class="jp-type-single">
- <div class="jp-gui jp-interface">
- <ul class="jp-controls">
- <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
- <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
- '.$extra_controls.'
- </ul>
- '.$progress.'
- </div>
- </div>
- </div>';
-
- return $html;
- }
- static function generate_video_preview($document_data = array()) {
- $html = '
- <div id="jp_container_1" class="jp-video">
- <div class="jp-type-single">
- <div id="jquery_jplayer_1" class="jp-jplayer"></div>
- <div class="jp-gui">
- <div class="jp-video-play">
- <a href="javascript:;" class="jp-video-play-icon" tabindex="1">play</a>
- </div>
- <div class="jp-interface">
- <div class="jp-progress">
- <div class="jp-seek-bar">
- <div class="jp-play-bar"></div>
- </div>
- </div>
- <div class="jp-current-time"></div>
- <div class="jp-controls-holder">
- <ul class="jp-controls">
- <li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>
- <li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>
- <li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>
- <li><a href="javascript:;" class="jp-mute" tabindex="1" title="mute">mute</a></li>
- <li><a href="javascript:;" class="jp-unmute" tabindex="1" title="unmute">unmute</a></li>
- <li><a href="javascript:;" class="jp-volume-max" tabindex="1" title="max volume">max volume</a></li>
- </ul>
- <div class="jp-volume-bar">
- <div class="jp-volume-bar-value"></div>
- </div>
- <ul class="jp-toggles">
- <li><a href="javascript:;" class="jp-full-screen" tabindex="1" title="full screen">full screen</a></li>
- <li><a href="javascript:;" class="jp-restore-screen" tabindex="1" title="restore screen">restore screen</a></li>
- <li><a href="javascript:;" class="jp-repeat" tabindex="1" title="repeat">repeat</a></li>
- <li><a href="javascript:;" class="jp-repeat-off" tabindex="1" title="repeat off">repeat off</a></li>
- </ul>
- </div>
- <div class="jp-title">
- <ul>
- <li>'.$document_data['title'].'</li>
- </ul>
- </div>
- </div>
- </div>
- <div class="jp-no-solution">
- <span>'.get_lang('UpdateRequire').'</span>
- '.get_lang("ToPlayTheMediaYouWillNeedToUpdateYourBrowserToARecentVersionYouCanAlsoDownloadTheFile").'
- </div>
- </div>
- </div>';
- return $html;
- }
- static function get_document_preview($course_info, $lp_id = false, $target = '', $session_id = 0, $add_move_button = false, $filter_by_folder = null, $overwrite_url = null) {
- if (empty($course_info['real_id']) || empty($course_info['code']) || !is_array($course_info)) {
- return '';
- }
- $user_id = api_get_user_id();
- $user_in_course = false;
- if (api_is_platform_admin()) {
- $user_in_course = true;
- }
- if (!$user_in_course) {
- if (CourseManager::is_course_teacher($user_id, $course_info['code'])) {
- $user_in_course = true;
- }
- }
-
- $session_id = intval($session_id);
- if (!$user_in_course) {
- if (empty($session_id)) {
- if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code'])) {
- $user_in_course = true;
- }
-
- if (isset($course_info) && in_array($course_info['visibility'], array(2, 3))) {
- $user_in_course = true;
- }
- } else {
- $user_status = SessionManager::get_user_status_in_course_session($user_id, $course_info['code'], $session_id);
-
- if (in_array($user_status, array('0', '2', '6'))) {
- $user_in_course = true;
- }
- }
- }
- $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
- $tbl_item_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
- $path = '/';
- $path = Database::escape_string(str_replace('_', '\_', $path));
- $added_slash = ($path == '/') ? '' : '/';
-
- $condition_session = " AND (id_session = '$session_id' OR id_session = '0' )";
-
- $add_folder_filter = null;
- if (!empty($filter_by_folder)) {
- $add_folder_filter = " AND docs.path LIKE '".Database::escape_string($filter_by_folder)."%'";
- }
-
- $sql_doc = "SELECT last.visibility, docs.*
- FROM $tbl_item_prop AS last, $tbl_doc AS docs
- WHERE docs.id = last.ref AND
- docs.path LIKE '".$path.$added_slash."%' AND
- docs.path NOT LIKE '%_DELETED_%' AND
- last.tool = '".TOOL_DOCUMENT."' $condition_session AND
- last.visibility = '1' AND
- docs.c_id = {$course_info['real_id']} AND
- last.c_id = {$course_info['real_id']}
- $add_folder_filter
- ORDER BY docs.title ASC";
-
- $res_doc = Database::query($sql_doc);
- $resources = Database::store_result($res_doc, 'ASSOC');
- $resources_sorted = array();
- $return = '';
-
- if ($lp_id) {
- $return .= '<div class="lp_resource_element">';
- $return .= Display::return_icon('new_doc.gif', '', array(), ICON_SIZE_SMALL);
- $return .= Display::url(get_lang('NewDocument'), api_get_self().'?'.api_get_cidreq().'&action=add_item&type='.TOOL_DOCUMENT.'&lp_id='.$_SESSION['oLP']->lp_id);
- $return .= '</div>';
- } else {
- $return .= Display::div(Display::url(Display::return_icon('close.png', get_lang('Close'), array(), ICON_SIZE_SMALL), ' javascript:void(0);', array('id'=>'close_div_'.$course_info['real_id'].'_'.$session_id,'class' =>'close_div')), array('style' => 'position:absolute;right:10px'));
- }
-
- if (!empty($resources) && $user_in_course) {
- foreach ($resources as $resource) {
- $is_visible = self::is_visible_by_id($resource['id'], $course_info, $session_id, api_get_user_id());
- if (!$is_visible) {
- continue;
- }
- $resource_paths = explode('/', $resource['path']);
- array_shift($resource_paths);
- $path_to_eval = $last_path = '';
- $is_file = false;
- if ($resource['filetype'] == 'file') {
- foreach ($resource_paths as $key => $resource_path) {
- if ($key != count($resource_paths)-1) {
-
- $path_to_eval .= "['$resource_path']['files']";
- }
- $is_file = true;
- }
- } else {
- foreach ($resource_paths as $key => $resource_path) {
- if ($key != count($resource_paths) - 1) {
-
- $path_to_eval .= "['$resource_path']['files']";
- }
- }
- }
- $last_path = $resource_path;
-
-
-
-
- $data = base64_encode($resource['title'].'|@j@|'.$last_path);
- if ($is_file) {
-
- if (empty($resource['title'])) {
- $resource['title'] = basename($resource['path']);
- }
- eval ('$resources_sorted'.$path_to_eval.'['.$resource['id'].'] = "'.$data.'" ; ');
- } else {
- eval ('$resources_sorted'.$path_to_eval.'["'.$last_path.'"]["id"]='.$resource['id'].';');
- eval ('$resources_sorted'.$path_to_eval.'["'.$last_path.'"]["title"]= "'.api_htmlentities($resource['title']).'";');
- }
- }
- }
- $label = get_lang('Documents');
- $new_array[$label] = array('id' => 0, 'files' => $resources_sorted);
- $write_result = self::write_resources_tree($course_info, $session_id, $new_array, 0, $lp_id, $target, $add_move_button, $overwrite_url);
- $return .= $write_result ;
- $img_path = api_get_path(WEB_IMG_PATH);
- if ($lp_id == false) {
- $return .= "<script>
- $('.doc_folder').mouseover(function() {
- var my_id = this.id.split('_')[2];
- $('#res_'+my_id).show();
- });
- $('.close_div').click(function() {
- var course_id = this.id.split('_')[2];
- var session_id = this.id.split('_')[3];
- $('#document_result_'+course_id+'_'+session_id).hide();
- $('.lp_resource').remove();
- });
- </script>";
- } else {
-
- $return .= "<script>
- function testResources(id, img) {
- if (document.getElementById(id).style.display=='block'){
- document.getElementById(id).style.display='none';
- var id = id.split('_')[1];
- document.getElementById('img_'+id).src='".$img_path."nolines_plus.gif';
- } else {
- document.getElementById(id).style.display='block';
- var id = id.split('_')[1];
- document.getElementById('img_'+id).src='".$img_path."nolines_minus.gif';
- }
- }
- </script>";
- }
- if(!$user_in_course) {
- $return = '';
- }
- return $return;
- }
-
- public static function write_resources_tree($course_info, $session_id, $resources_sorted, $num = 0, $lp_id = false, $target = '', $add_move_button = false, $overwrite_url = null) {
- require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php';
- $img_path = api_get_path(WEB_IMG_PATH);
- $img_sys_path = api_get_path(SYS_CODE_PATH).'img/';
- $web_code_path = api_get_path(WEB_CODE_PATH);
- $return = '';
- if (count($resources_sorted) > 0) {
- foreach ($resources_sorted as $key => $resource) {
- $title = isset($resource['title']) ? $resource['title'] : null;
- if (empty($title)) {
- $title = $key;
- }
-
- if (isset($resource['id']) && is_int($resource['id'])) {
-
-
- if (in_array($key, array('shared_folder','chat_files', 'HotPotatoes_files', 'css', 'certificates'))){
- continue;
- } elseif(preg_match('/_groupdocs/', $key)){
- continue;
- } elseif(preg_match('/sf_user_/', $key)){
- continue;
- } elseif(preg_match('/shared_folder_session_/', $key)){
- continue;
- }
-
- if ($key=='images') {
- $key=get_lang('Images');
- } elseif($key=='gallery') {
- $key=get_lang('Gallery');
- } elseif($key=='flash') {
- $key=get_lang('Flash');
- } elseif($key=='audio'){
- $key=get_lang('Audio');
- } elseif($key=='video') {
- $key=get_lang('Video');
- }
- $onclick = '';
- if ($lp_id) {
- $onclick = 'onclick="javascript: testResources(\'res_' . $resource['id'] . '\',\'img_' . $resource['id'] . '\')"';
- }
- $return .= '<ul class="lp_resource">';
- $return .= '<li class="doc_folder" id="doc_id_'.$resource['id'].'" style="margin-left:'.($num * 18).'px; ">';
- if ($lp_id) {
- $return .= '<img style="cursor: pointer;" src="'.$img_path.'nolines_plus.gif" align="absmiddle" id="img_' . $resource['id'] . '" '.$onclick.' >';
- } else {
- $return .= '<span style="margin-left:16px"> </span>';
- }
- $return .= '<img alt="" src="'.$img_path.'lp_folder.gif" title="" align="absmiddle" /> ';
- $return .= '<span '.$onclick.' style="cursor: pointer;" >'.$title.'</span>';
- $return .= '</li>';
- $return .= '<div id="res_'.$resource['id'].'" style="display: none;" >';
- if (isset($resource['files'])) {
- $return .= self::write_resources_tree($course_info, $session_id, $resource['files'], $num +1, $lp_id, $target, $add_move_button, $overwrite_url);
- }
- $return .= '</div>';
- $return .= '</ul>';
- } else {
- if (!is_array($resource)) {
- $resource = base64_decode($resource);
-
- $icon = choose_image($resource);
- $position = strrpos($icon, '.');
- $icon = substr($icon, 0, $position) . '_small.gif';
- $file_info = explode('|@j@|', $resource);
- $my_file_title = $file_info[0];
-
- if (empty($my_file_title)) {
- $my_file_title = $file_info[1];
- }
-
- if ($lp_id) {
-
- $url = api_get_self() . '?cidReq=' . Security::remove_XSS($_GET['cidReq']) . '&action=add_item&type=' . TOOL_DOCUMENT . '&file=' . $key . '&lp_id=' .$lp_id;
- if (!empty($overwrite_url)) {
- $url = $overwrite_url.'&document_id='.$key;
- }
- } else {
-
- $url = $web_code_path.'document/document.php?cidReq='.$course_info['code'].'&id_session='.$session_id.'&id='.$key;
- if (!empty($overwrite_url)) {
- $url = $overwrite_url.'&document_id='.$key;
- }
- }
- $img = $img_path.$icon;
- if (!file_exists($img_sys_path.$icon)) {
- $img = $img_path.'icons/16/default_small.gif';
- }
- $link = Display::url('<img alt="" src="'.$img.'" title="" /> ' . $my_file_title, $url, array('target' => $target));
- if ($lp_id == false) {
- $return .= '<li class="doc_resource" data_id="'.$key.'" data_type="document" title="'.$my_file_title.'" >';
- } else {
- $return .= '<li class="doc_resource lp_resource_element" data_id="'.$key.'" data_type="document" title="'.$my_file_title.'" >';
- }
- $return .= '<div class="item_data" style="margin-left:' . (($num +1) * 18) . 'px;margin-right:5px;">';
- if ($add_move_button) {
- $return .= '<a class="moved" href="#">';
- $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), array(), ICON_SIZE_TINY);
- $return .= '</a> ';
- }
- $return .= $link;
- $return .= '</div></li>';
- }
- }
- }
- }
- return $return;
- }
- public static function check_visibility_tree($doc_id, $course_code, $session_id, $user_id) {
- $document_data = self::get_document_data_by_id($doc_id, $course_code);
- if (!empty($document_data)) {
-
- if (api_is_platform_admin() || CourseManager::is_course_teacher($user_id, $course_code)) {
- return true;
- }
- $course_info = api_get_course_info($course_code);
- if ($document_data['parent_id'] == false || empty($document_data['parent_id'])) {
- $visible = self::is_visible_by_id($doc_id, $course_info, $session_id, $user_id);
- return $visible;
- } else {
- $course_info = api_get_course_info($course_code);
- $visible = self::is_visible_by_id($doc_id, $course_info, $session_id, $user_id);
- if (!$visible) {
- return false;
- } else {
- return self::check_visibility_tree($document_data['parent_id'], $course_code, $session_id, $user_id);
- }
- }
- } else {
- return false;
- }
- }
-
- public function index_document($docid, $course_code, $session_id=0, $lang='english', $specific_fields_values=array(), $if_exists = '', $simulation = false) {
- if (api_get_setting('search_enabled') !== 'true') {
- return false;
- }
- if (empty($docid) or $docid != intval($docid)) {
- return false;
- }
- if (empty($session_id)) {
- $session_id = api_get_session_id();
- }
- $course_info = api_get_course_info($course_code);
- $course_dir = $course_info['path'].'/document';
- $sys_course_path = api_get_path(SYS_COURSE_PATH);
- $base_work_dir = $sys_course_path.$course_dir;
- $course_id = $course_info['real_id'];
- $table_document = Database::get_course_table(TABLE_DOCUMENT);
- $qry = "SELECT path, title FROM $table_document WHERE c_id = $course_id AND id = '$docid' LIMIT 1";
- $result = Database::query($qry);
- if (Database::num_rows($result) == 1) {
- $row = Database::fetch_array($result);
- $doc_path = api_get_path(SYS_COURSE_PATH).$course_dir.$row['path'];
-
-
- $doc_mime = mime_content_type($doc_path);
-
- $allowed_mime_types = self::file_get_mime_type(true);
-
- if (empty($doc_mime)) {
- $allowed_extensions = array('doc', 'docx', 'ppt', 'pptx', 'pps', 'ppsx', 'xls', 'xlsx', 'odt', 'odp', 'ods', 'pdf', 'txt', 'rtf', 'msg', 'csv', 'html', 'htm');
- $extensions = preg_split("/[\/\\.]/", $doc_path) ;
- $doc_ext = strtolower($extensions[count($extensions) - 1]);
- if (in_array($doc_ext, $allowed_extensions)) {
- switch ($doc_ext) {
- case 'ppt':
- case 'pps':
- $doc_mime = 'application/vnd.ms-powerpoint';
- break;
- case 'xls':
- $doc_mime = 'application/vnd.ms-excel';
- break;
- }
- }
- }
-
- if (in_array($doc_mime, $allowed_mime_types)) {
- $file_title = $row['title'];
- $file_content = self::get_text_content($doc_path, $doc_mime);
- $course_code = Database::escape_string($course_code);
- require_once api_get_path(LIBRARY_PATH).'search/ChamiloIndexer.class.php';
- require_once api_get_path(LIBRARY_PATH).'search/IndexableChunk.class.php';
- $ic_slide = new IndexableChunk();
- $ic_slide->addValue('title', $file_title);
- $ic_slide->addCourseId($course_code);
- $ic_slide->addToolId(TOOL_DOCUMENT);
- $xapian_data = array(
- SE_COURSE_ID => $course_code,
- SE_TOOL_ID => TOOL_DOCUMENT,
- SE_DATA => array('doc_id' => $docid),
- SE_USER => api_get_user_id(),
- );
- $ic_slide->xapian_data = serialize($xapian_data);
- $di = new ChamiloIndexer();
- $return = $di->connectDb(null, null, $lang);
- require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
- $specific_fields = get_specific_field_list();
-
-
- if (!empty($if_exists) && $if_exists == 'overwrite') {
-
-
-
-
-
- $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
- $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
- $sql = sprintf($sql, $tbl_se_ref, $course_code, TOOL_DOCUMENT, $docid);
- $res = Database::query($sql);
- if (Database::num_rows($res) > 0) {
- $se_ref = Database::fetch_array($res);
- if (!$simulation) {
- $di->remove_document($se_ref['search_did']);
- }
- $all_specific_terms = '';
- foreach ($specific_fields as $specific_field) {
- if (!$simulation) {
- delete_all_specific_field_value($course_code, $specific_field['id'], TOOL_DOCUMENT, $docid);
- }
-
- if (isset($specific_fields_values[$specific_field['code']])) {
- $sterms = trim($specific_fields_values[$specific_field['code']]);
- } else {
- $sterms = '';
- }
- $all_specific_terms .= ' '. $sterms;
- $sterms = explode(',', $sterms);
- foreach ($sterms as $sterm) {
- $sterm = trim($sterm);
- if (!empty($sterm)) {
- $ic_slide->addTerm($sterm, $specific_field['code']);
-
- if (!$simulation) {
- add_specific_field_value($specific_field['id'], $course_code, TOOL_DOCUMENT, $docid, $sterm);
- }
- }
- }
- }
-
- $file_content = $all_specific_terms .' '. $file_content;
- if (!$simulation) {
- $ic_slide->addValue('content', $file_content);
- $di->addChunk($ic_slide);
-
- $did = $di->index();
- if ($did) {
-
- $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
- $sql = 'UPDATE %s SET search_did=%d WHERE id=%d LIMIT 1';
- $sql = sprintf($sql, $tbl_se_ref, (int)$did, (int)$se_ref['id']);
- Database::query($sql);
- }
- }
- }
- } else {
-
- $all_specific_terms = '';
- foreach ($specific_fields as $specific_field) {
- if (isset($specific_fields_values[$specific_field['code']])) {
- $sterms = trim($specific_fields_values[$specific_field['code']]);
- } else {
- $sterms = '';
- }
- $all_specific_terms .= ' '. $sterms;
- if (!empty($sterms)) {
- $sterms = explode(',', $sterms);
- foreach ($sterms as $sterm) {
- if (!$simulation) {
- $ic_slide->addTerm(trim($sterm), $specific_field['code']);
- add_specific_field_value($specific_field['id'], $course_code, TOOL_DOCUMENT, $docid, $sterm);
- }
- }
- }
- }
-
- $file_content = $all_specific_terms .' '. $file_content;
- if (!$simulation) {
- $ic_slide->addValue('content', $file_content);
- $di->addChunk($ic_slide);
-
- $did = $di->index();
- if ($did) {
-
- $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
- $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
- VALUES (NULL , \'%s\', \'%s\', %s, %s)';
- $sql = sprintf($sql, $tbl_se_ref, $course_code, TOOL_DOCUMENT, $docid, $did);
- Database::query($sql);
- } else {
- return false;
- }
- }
- }
- } else {
- return false;
- }
- }
- return true;
- }
- public static function get_web_odf_extension_list(){
- return array('ods', 'odt');
- }
- public static function is_folder_to_avoid($path, $is_certificate_mode = false) {
- $folders_to_avoid = array(
- '/HotPotatoes_files',
- '/certificates',
- );
- if (basename($path) == 'css') {
- return true;
- }
-
- if (strstr($path, 'HotPotatoes_files')) {
- return true;
- }
- if ($is_certificate_mode == false) {
-
- if (strstr($path, 'certificates')) {
- return true;
- }
- }
-
- if (api_get_setting('show_users_folders') == 'false') {
- $folders_to_avoid[] = '/shared_folder';
- if (strstr($path, 'shared_folder_session_')) {
- return true;
- }
- }
-
- if (api_get_setting('show_default_folders') == 'false') {
- $folders_to_avoid[] = '/images';
- $folders_to_avoid[] = '/flash';
- $folders_to_avoid[] = '/audio';
- $folders_to_avoid[] = '/video';
- }
-
- if (api_get_setting('show_chat_folder') == 'false') {
- $folders_to_avoid[] = '/chat_files';
- }
- return in_array($path, $folders_to_avoid);
- }
- static function get_system_folders() {
- $system_folders = array(
- '/certificates',
- '/chat_files',
- '/images',
- '/flash',
- '/audio',
- '/video',
- '/shared_folder'
- );
- return $system_folders;
- }
- }
|