document.lib.php 261 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * Class DocumentManager
  6. * This is the document library for Chamilo.
  7. * It is / will be used to provide a service layer to all document-using tools.
  8. * and eliminate code duplication fro group documents, scorm documents, main documents.
  9. * Include/require it in your code to use its functionality.
  10. *
  11. * @package chamilo.library
  12. */
  13. class DocumentManager
  14. {
  15. /**
  16. * Construct.
  17. */
  18. private function __construct()
  19. {
  20. }
  21. /**
  22. * @param string $course_code
  23. *
  24. * @return int the document folder quota for the current course in bytes
  25. * or the default quota
  26. */
  27. public static function get_course_quota($course_code = null)
  28. {
  29. if (empty($course_code)) {
  30. $course_info = api_get_course_info();
  31. } else {
  32. $course_info = api_get_course_info($course_code);
  33. }
  34. $course_quota = null;
  35. if (empty($course_info)) {
  36. return DEFAULT_DOCUMENT_QUOTA;
  37. } else {
  38. $course_quota = $course_info['disk_quota'];
  39. }
  40. if (is_null($course_quota) || empty($course_quota)) {
  41. // Course table entry for quota was null, then use default value
  42. $course_quota = DEFAULT_DOCUMENT_QUOTA;
  43. }
  44. return $course_quota;
  45. }
  46. /**
  47. * Get the content type of a file by checking the extension
  48. * We could use mime_content_type() with php-versions > 4.3,
  49. * but this doesn't work as it should on Windows installations.
  50. *
  51. * @param string $filename or boolean TRUE to return complete array
  52. *
  53. * @author ? first version
  54. * @author Bert Vanderkimpen
  55. *
  56. * @return string
  57. */
  58. public static function file_get_mime_type($filename)
  59. {
  60. // All MIME types in an array (from 1.6, this is the authorative source)
  61. // Please, keep this alphabetical if you add something to this list!
  62. $mimeTypes = [
  63. 'ai' => 'application/postscript',
  64. 'aif' => 'audio/x-aiff',
  65. 'aifc' => 'audio/x-aiff',
  66. 'aiff' => 'audio/x-aiff',
  67. 'asf' => 'video/x-ms-asf',
  68. 'asc' => 'text/plain',
  69. 'au' => 'audio/basic',
  70. 'avi' => 'video/x-msvideo',
  71. 'bcpio' => 'application/x-bcpio',
  72. 'bin' => 'application/octet-stream',
  73. 'bmp' => 'image/bmp',
  74. 'cdf' => 'application/x-netcdf',
  75. 'class' => 'application/octet-stream',
  76. 'cpio' => 'application/x-cpio',
  77. 'cpt' => 'application/mac-compactpro',
  78. 'csh' => 'application/x-csh',
  79. 'css' => 'text/css',
  80. 'dcr' => 'application/x-director',
  81. 'dir' => 'application/x-director',
  82. 'djv' => 'image/vnd.djvu',
  83. 'djvu' => 'image/vnd.djvu',
  84. 'dll' => 'application/octet-stream',
  85. 'dmg' => 'application/x-diskcopy',
  86. 'dms' => 'application/octet-stream',
  87. 'doc' => 'application/msword',
  88. 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
  89. 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  90. 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
  91. 'dvi' => 'application/x-dvi',
  92. 'dwg' => 'application/vnd.dwg',
  93. 'dwf' => 'application/vnd.dwf',
  94. 'dxf' => 'application/vnd.dxf',
  95. 'dxr' => 'application/x-director',
  96. 'eps' => 'application/postscript',
  97. 'epub' => 'application/epub+zip',
  98. 'etx' => 'text/x-setext',
  99. 'exe' => 'application/octet-stream',
  100. 'ez' => 'application/andrew-inset',
  101. 'flv' => 'video/flv',
  102. 'gif' => 'image/gif',
  103. 'gtar' => 'application/x-gtar',
  104. 'gz' => 'application/x-gzip',
  105. 'hdf' => 'application/x-hdf',
  106. 'hqx' => 'application/mac-binhex40',
  107. 'htm' => 'text/html',
  108. 'html' => 'text/html',
  109. 'ice' => 'x-conference-xcooltalk',
  110. 'ief' => 'image/ief',
  111. 'iges' => 'model/iges',
  112. 'igs' => 'model/iges',
  113. 'jar' => 'application/java-archiver',
  114. 'jpe' => 'image/jpeg',
  115. 'jpeg' => 'image/jpeg',
  116. 'jpg' => 'image/jpeg',
  117. 'js' => 'application/x-javascript',
  118. 'kar' => 'audio/midi',
  119. 'lam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
  120. 'latex' => 'application/x-latex',
  121. 'lha' => 'application/octet-stream',
  122. 'log' => 'text/plain',
  123. 'lzh' => 'application/octet-stream',
  124. 'm1a' => 'audio/mpeg',
  125. 'm2a' => 'audio/mpeg',
  126. 'm3u' => 'audio/x-mpegurl',
  127. 'man' => 'application/x-troff-man',
  128. 'me' => 'application/x-troff-me',
  129. 'mesh' => 'model/mesh',
  130. 'mid' => 'audio/midi',
  131. 'midi' => 'audio/midi',
  132. 'mov' => 'video/quicktime',
  133. 'movie' => 'video/x-sgi-movie',
  134. 'mp2' => 'audio/mpeg',
  135. 'mp3' => 'audio/mpeg',
  136. 'mp4' => 'video/mp4',
  137. 'mpa' => 'audio/mpeg',
  138. 'mpe' => 'video/mpeg',
  139. 'mpeg' => 'video/mpeg',
  140. 'mpg' => 'video/mpeg',
  141. 'mpga' => 'audio/mpeg',
  142. 'ms' => 'application/x-troff-ms',
  143. 'msh' => 'model/mesh',
  144. 'mxu' => 'video/vnd.mpegurl',
  145. 'nc' => 'application/x-netcdf',
  146. 'oda' => 'application/oda',
  147. 'oga' => 'audio/ogg',
  148. 'ogg' => 'application/ogg',
  149. 'ogx' => 'application/ogg',
  150. 'ogv' => 'video/ogg',
  151. 'pbm' => 'image/x-portable-bitmap',
  152. 'pct' => 'image/pict',
  153. 'pdb' => 'chemical/x-pdb',
  154. 'pdf' => 'application/pdf',
  155. 'pgm' => 'image/x-portable-graymap',
  156. 'pgn' => 'application/x-chess-pgn',
  157. 'pict' => 'image/pict',
  158. 'png' => 'image/png',
  159. 'pnm' => 'image/x-portable-anymap',
  160. 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
  161. 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
  162. 'pps' => 'application/vnd.ms-powerpoint',
  163. 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
  164. 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
  165. 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
  166. 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
  167. 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
  168. 'ppm' => 'image/x-portable-pixmap',
  169. 'ppt' => 'application/vnd.ms-powerpoint',
  170. 'ps' => 'application/postscript',
  171. 'qt' => 'video/quicktime',
  172. 'ra' => 'audio/x-realaudio',
  173. 'ram' => 'audio/x-pn-realaudio',
  174. 'rar' => 'image/x-rar-compressed',
  175. 'ras' => 'image/x-cmu-raster',
  176. 'rgb' => 'image/x-rgb',
  177. 'rm' => 'audio/x-pn-realaudio',
  178. 'roff' => 'application/x-troff',
  179. 'rpm' => 'audio/x-pn-realaudio-plugin',
  180. 'rtf' => 'text/rtf',
  181. 'rtx' => 'text/richtext',
  182. 'sgm' => 'text/sgml',
  183. 'sgml' => 'text/sgml',
  184. 'sh' => 'application/x-sh',
  185. 'shar' => 'application/x-shar',
  186. 'silo' => 'model/mesh',
  187. 'sib' => 'application/X-Sibelius-Score',
  188. 'sit' => 'application/x-stuffit',
  189. 'skd' => 'application/x-koan',
  190. 'skm' => 'application/x-koan',
  191. 'skp' => 'application/x-koan',
  192. 'skt' => 'application/x-koan',
  193. 'smi' => 'application/smil',
  194. 'smil' => 'application/smil',
  195. 'snd' => 'audio/basic',
  196. 'so' => 'application/octet-stream',
  197. 'spl' => 'application/x-futuresplash',
  198. 'src' => 'application/x-wais-source',
  199. 'sv4cpio' => 'application/x-sv4cpio',
  200. 'sv4crc' => 'application/x-sv4crc',
  201. 'svf' => 'application/vnd.svf',
  202. 'svg' => 'image/svg+xml',
  203. //'svgz' => 'image/svg+xml',
  204. 'swf' => 'application/x-shockwave-flash',
  205. 'sxc' => 'application/vnd.sun.xml.calc',
  206. 'sxi' => 'application/vnd.sun.xml.impress',
  207. 'sxw' => 'application/vnd.sun.xml.writer',
  208. 't' => 'application/x-troff',
  209. 'tar' => 'application/x-tar',
  210. 'tcl' => 'application/x-tcl',
  211. 'tex' => 'application/x-tex',
  212. 'texi' => 'application/x-texinfo',
  213. 'texinfo' => 'application/x-texinfo',
  214. 'tga' => 'image/x-targa',
  215. 'tif' => 'image/tif',
  216. 'tiff' => 'image/tiff',
  217. 'tr' => 'application/x-troff',
  218. 'tsv' => 'text/tab-seperated-values',
  219. 'txt' => 'text/plain',
  220. 'ustar' => 'application/x-ustar',
  221. 'vcd' => 'application/x-cdlink',
  222. 'vrml' => 'model/vrml',
  223. 'wav' => 'audio/x-wav',
  224. 'wbmp' => 'image/vnd.wap.wbmp',
  225. 'wbxml' => 'application/vnd.wap.wbxml',
  226. 'webp' => 'image/webp',
  227. 'wml' => 'text/vnd.wap.wml',
  228. 'wmlc' => 'application/vnd.wap.wmlc',
  229. 'wmls' => 'text/vnd.wap.wmlscript',
  230. 'wmlsc' => 'application/vnd.wap.wmlscriptc',
  231. 'wma' => 'audio/x-ms-wma',
  232. 'wmv' => 'video/x-ms-wmv',
  233. 'wrl' => 'model/vrml',
  234. 'xbm' => 'image/x-xbitmap',
  235. 'xht' => 'application/xhtml+xml',
  236. 'xhtml' => 'application/xhtml+xml',
  237. 'xls' => 'application/vnd.ms-excel',
  238. 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
  239. 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
  240. 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  241. 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
  242. 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
  243. 'xml' => 'text/xml',
  244. 'xpm' => 'image/x-xpixmap',
  245. 'xsl' => 'text/xml',
  246. 'xwd' => 'image/x-windowdump',
  247. 'xyz' => 'chemical/x-xyz',
  248. 'zip' => 'application/zip',
  249. ];
  250. if ($filename === true) {
  251. return $mimeTypes;
  252. }
  253. // Get the extension of the file
  254. $extension = explode('.', $filename);
  255. // $filename will be an array if a . was found
  256. if (is_array($extension)) {
  257. $extension = strtolower($extension[count($extension) - 1]);
  258. } else {
  259. //file without extension
  260. $extension = 'empty';
  261. }
  262. //if the extension is found, return the content type
  263. if (isset($mimeTypes[$extension])) {
  264. return $mimeTypes[$extension];
  265. }
  266. return 'application/octet-stream';
  267. }
  268. /**
  269. * This function smart streams a file to the client using HTTP headers.
  270. *
  271. * @param string $fullFilename The full path of the file to be sent
  272. * @param string $filename The name of the file as shown to the client
  273. * @param string $contentType The MIME type of the file
  274. *
  275. * @return bool false if file doesn't exist, true if stream succeeded
  276. */
  277. public static function smartReadFile($fullFilename, $filename, $contentType = 'application/octet-stream')
  278. {
  279. if (!file_exists($fullFilename)) {
  280. header("HTTP/1.1 404 Not Found");
  281. return false;
  282. }
  283. $size = filesize($fullFilename);
  284. $time = date('r', filemtime($fullFilename));
  285. $fm = @fopen($fullFilename, 'rb');
  286. if (!$fm) {
  287. header("HTTP/1.1 505 Internal server error");
  288. return false;
  289. }
  290. $begin = 0;
  291. $end = $size - 1;
  292. if (isset($_SERVER['HTTP_RANGE'])) {
  293. if (preg_match('/bytes=\h*(\d+)-(\d*)[\D.*]?/i', $_SERVER['HTTP_RANGE'], $matches)) {
  294. $begin = intval($matches[1]);
  295. if (!empty($matches[2])) {
  296. $end = intval($matches[2]);
  297. }
  298. }
  299. }
  300. if (isset($_SERVER['HTTP_RANGE'])) {
  301. header('HTTP/1.1 206 Partial Content');
  302. } else {
  303. header('HTTP/1.1 200 OK');
  304. }
  305. header("Content-Type: $contentType");
  306. header('Cache-Control: public, must-revalidate, max-age=0');
  307. header('Pragma: no-cache');
  308. header('Accept-Ranges: bytes');
  309. header('Content-Length:'.(($end - $begin) + 1));
  310. if (isset($_SERVER['HTTP_RANGE'])) {
  311. header("Content-Range: bytes $begin-$end/$size");
  312. }
  313. header("Content-Disposition: inline; filename=$filename");
  314. header("Content-Transfer-Encoding: binary");
  315. header("Last-Modified: $time");
  316. $cur = $begin;
  317. fseek($fm, $begin, 0);
  318. while (!feof($fm) && $cur <= $end && (connection_status() == 0)) {
  319. echo fread($fm, min(1024 * 16, ($end - $cur) + 1));
  320. $cur += 1024 * 16;
  321. }
  322. }
  323. /**
  324. * This function streams a file to the client.
  325. *
  326. * @param string $full_file_name
  327. * @param bool $forced
  328. * @param string $name
  329. * @param bool $fixLinksHttpToHttps change file content from http to https
  330. *
  331. * @return false if file doesn't exist, true if stream succeeded
  332. */
  333. public static function file_send_for_download(
  334. $full_file_name,
  335. $forced = false,
  336. $name = '',
  337. $fixLinksHttpToHttps = false
  338. ) {
  339. session_write_close(); //we do not need write access to session anymore
  340. if (!is_file($full_file_name)) {
  341. return false;
  342. }
  343. $filename = $name == '' ? basename($full_file_name) : api_replace_dangerous_char($name);
  344. $len = filesize($full_file_name);
  345. // Fixing error when file name contains a ","
  346. $filename = str_replace(',', '', $filename);
  347. $sendFileHeaders = api_get_configuration_value('enable_x_sendfile_headers');
  348. // Allows chrome to make videos and audios seekable
  349. header('Accept-Ranges: bytes');
  350. if ($forced) {
  351. // Force the browser to save the file instead of opening it
  352. if (isset($sendFileHeaders) &&
  353. !empty($sendFileHeaders)) {
  354. header("X-Sendfile: $filename");
  355. }
  356. header('Content-type: application/octet-stream');
  357. header('Content-length: '.$len);
  358. if (preg_match("/MSIE 5.5/", $_SERVER['HTTP_USER_AGENT'])) {
  359. header('Content-Disposition: filename= '.$filename);
  360. } else {
  361. header('Content-Disposition: attachment; filename= '.$filename);
  362. }
  363. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  364. header('Pragma: ');
  365. header('Cache-Control: ');
  366. header('Cache-Control: public'); // IE cannot download from sessions without a cache
  367. }
  368. header('Content-Description: '.$filename);
  369. header('Content-Transfer-Encoding: binary');
  370. if (function_exists('ob_end_clean') && ob_get_length()) {
  371. // Use ob_end_clean() to avoid weird buffering situations
  372. // where file is sent broken/incomplete for download
  373. ob_end_clean();
  374. }
  375. $res = fopen($full_file_name, 'r');
  376. fpassthru($res);
  377. return true;
  378. } else {
  379. // no forced download, just let the browser decide what to do according to the mimetype
  380. $lpFixedEncoding = api_get_configuration_value('lp_fixed_encoding');
  381. // Commented to let courses content to be cached in order to improve performance:
  382. //header('Expires: Wed, 01 Jan 1990 00:00:00 GMT');
  383. //header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
  384. // Commented to avoid double caching declaration when playing with IE and HTTPS
  385. //header('Cache-Control: no-cache, must-revalidate');
  386. //header('Pragma: no-cache');
  387. $contentType = self::file_get_mime_type($filename);
  388. switch ($contentType) {
  389. case 'text/html':
  390. if (isset($lpFixedEncoding) && $lpFixedEncoding === 'true') {
  391. $contentType .= '; charset=UTF-8';
  392. } else {
  393. $encoding = @api_detect_encoding_html(file_get_contents($full_file_name));
  394. if (!empty($encoding)) {
  395. $contentType .= '; charset='.$encoding;
  396. }
  397. }
  398. break;
  399. case 'text/plain':
  400. if (isset($lpFixedEncoding) && $lpFixedEncoding === 'true') {
  401. $contentType .= '; charset=UTF-8';
  402. } else {
  403. $encoding = @api_detect_encoding(strip_tags(file_get_contents($full_file_name)));
  404. if (!empty($encoding)) {
  405. $contentType .= '; charset='.$encoding;
  406. }
  407. }
  408. break;
  409. case 'video/mp4':
  410. case 'audio/mpeg':
  411. case 'audio/mp4':
  412. case 'audio/ogg':
  413. case 'audio/webm':
  414. case 'audio/wav':
  415. case 'video/ogg':
  416. case 'video/webm':
  417. self::smartReadFile($full_file_name, $filename, $contentType);
  418. exit;
  419. case 'application/vnd.dwg':
  420. case 'application/vnd.dwf':
  421. header('Content-type: application/octet-stream');
  422. break;
  423. }
  424. header('Content-type: '.$contentType);
  425. header('Content-Length: '.$len);
  426. $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
  427. if (strpos($userAgent, 'msie')) {
  428. header('Content-Disposition: ; filename= '.$filename);
  429. } else {
  430. //header('Content-Disposition: inline');
  431. header('Content-Disposition: inline;');
  432. }
  433. if ($fixLinksHttpToHttps) {
  434. $content = file_get_contents($full_file_name);
  435. $content = str_replace(
  436. ['http%3A%2F%2F', 'http://'],
  437. ['https%3A%2F%2F', 'https://'],
  438. $content
  439. );
  440. echo $content;
  441. } else {
  442. if (function_exists('ob_end_clean') && ob_get_length()) {
  443. // Use ob_end_clean() to avoid weird buffering situations
  444. // where file is sent broken/incomplete for download
  445. ob_end_clean();
  446. }
  447. readfile($full_file_name);
  448. }
  449. return true;
  450. }
  451. }
  452. /**
  453. * Session folder filters.
  454. *
  455. * @param string $path
  456. * @param int $sessionId
  457. *
  458. * @return string|null
  459. */
  460. public static function getSessionFolderFilters($path, $sessionId)
  461. {
  462. $sessionId = (int) $sessionId;
  463. $condition = null;
  464. if (!empty($sessionId)) {
  465. // Chat folder filter
  466. if ($path == '/chat_files') {
  467. $condition .= " AND (docs.session_id = '$sessionId') ";
  468. }
  469. // share_folder filter
  470. $condition .= " AND docs.path != '/shared_folder' ";
  471. }
  472. return $condition;
  473. }
  474. /**
  475. * Fetches all document data for the given user/group.
  476. *
  477. * @param array $courseInfo
  478. * @param string $path
  479. * @param int $toGroupId iid
  480. * @param int $toUserId
  481. * @param bool $canSeeInvisible
  482. * @param bool $search
  483. * @param int $sessionId
  484. *
  485. * @return array with all document data
  486. */
  487. public static function getAllDocumentData(
  488. $courseInfo,
  489. $path = '/',
  490. $toGroupId = 0,
  491. $toUserId = null,
  492. $canSeeInvisible = false,
  493. $search = false,
  494. $sessionId = 0
  495. ) {
  496. if (empty($courseInfo)) {
  497. return [];
  498. }
  499. $tblItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  500. $tblDocument = Database::get_course_table(TABLE_DOCUMENT);
  501. if (!is_null($toUserId)) {
  502. $toUserId = (int) $toUserId;
  503. $userGroupFilter = "last.to_user_id = $toUserId";
  504. if (empty($toUserId)) {
  505. $userGroupFilter = ' (last.to_user_id = 0 OR last.to_user_id IS NULL) ';
  506. }
  507. } else {
  508. $toGroupId = (int) $toGroupId;
  509. $userGroupFilter = "last.to_group_id = $toGroupId";
  510. if (empty($toGroupId)) {
  511. $userGroupFilter = '( last.to_group_id = 0 OR last.to_group_id IS NULL) ';
  512. }
  513. }
  514. // Escape underscores in the path so they don't act as a wildcard
  515. $originalPath = $path;
  516. $path = str_replace('_', '\_', $path);
  517. $visibilityBit = ' <> 2';
  518. // The given path will not end with a slash, unless it's the root '/'
  519. // so no root -> add slash
  520. $addedSlash = $path == '/' ? '' : '/';
  521. // Condition for the session
  522. $sessionId = $sessionId ?: api_get_session_id();
  523. $conditionSession = " AND (last.session_id = '$sessionId' OR (last.session_id = '0' OR last.session_id IS NULL) )";
  524. $conditionSession .= self::getSessionFolderFilters($originalPath, $sessionId);
  525. $sharedCondition = null;
  526. if ($originalPath == '/shared_folder') {
  527. $students = CourseManager::get_user_list_from_course_code($courseInfo['code'], $sessionId);
  528. if (!empty($students)) {
  529. $conditionList = [];
  530. foreach ($students as $studentInfo) {
  531. $conditionList[] = '/shared_folder/sf_user_'.$studentInfo['user_id'];
  532. }
  533. $sharedCondition .= ' AND docs.path IN ("'.implode('","', $conditionList).'")';
  534. }
  535. }
  536. $sql = "SELECT
  537. docs.id,
  538. docs.filetype,
  539. docs.path,
  540. docs.title,
  541. docs.comment,
  542. docs.size,
  543. docs.readonly,
  544. docs.session_id,
  545. last.session_id item_property_session_id,
  546. last.lastedit_date,
  547. last.visibility,
  548. last.insert_user_id
  549. FROM $tblItemProperty AS last
  550. INNER JOIN $tblDocument AS docs
  551. ON (
  552. docs.id = last.ref AND
  553. docs.c_id = last.c_id
  554. )
  555. WHERE
  556. last.tool = '".TOOL_DOCUMENT."' AND
  557. docs.c_id = {$courseInfo['real_id']} AND
  558. last.c_id = {$courseInfo['real_id']} AND
  559. docs.path LIKE '".Database::escape_string($path.$addedSlash.'%')."' AND
  560. docs.path NOT LIKE '".Database::escape_string($path.$addedSlash.'%/%')."' AND
  561. docs.path NOT LIKE '%_DELETED_%' AND
  562. $userGroupFilter AND
  563. last.visibility $visibilityBit
  564. $conditionSession
  565. $sharedCondition
  566. ORDER BY last.iid DESC, last.session_id DESC
  567. ";
  568. $result = Database::query($sql);
  569. $documentData = [];
  570. $isAllowedToEdit = api_is_allowed_to_edit(null, true);
  571. $isCoach = api_is_coach();
  572. if ($result !== false && Database::num_rows($result) != 0) {
  573. $rows = [];
  574. $hideInvisibleDocuments = api_get_configuration_value('hide_invisible_course_documents_in_sessions');
  575. while ($row = Database::fetch_array($result, 'ASSOC')) {
  576. if (isset($rows[$row['id']])) {
  577. continue;
  578. }
  579. // If we are in session and hide_invisible_course_documents_in_sessions is enabled
  580. // Then we avoid the documents that have visibility in session but that they come from a base course
  581. if ($hideInvisibleDocuments && $sessionId) {
  582. if ($row['item_property_session_id'] == $sessionId && empty($row['session_id'])) {
  583. continue;
  584. }
  585. }
  586. if (self::isBasicCourseFolder($row['path'], $sessionId)) {
  587. $basicCourseDocumentsContent = self::getAllDocumentData(
  588. $courseInfo,
  589. $row['path']
  590. );
  591. if (empty($basicCourseDocumentsContent)) {
  592. continue;
  593. }
  594. }
  595. $rows[$row['id']] = $row;
  596. }
  597. // If we are in session and hide_invisible_course_documents_in_sessions is enabled
  598. // Or if we are students
  599. // Then don't list the invisible or deleted documents
  600. if (($sessionId && $hideInvisibleDocuments) || (!$isCoach && !$isAllowedToEdit)) {
  601. $rows = array_filter($rows, function ($row) {
  602. if (in_array($row['visibility'], ['0', '2'])) {
  603. return false;
  604. }
  605. return true;
  606. });
  607. }
  608. foreach ($rows as $row) {
  609. if ($row['filetype'] == 'file' &&
  610. pathinfo($row['path'], PATHINFO_EXTENSION) == 'html'
  611. ) {
  612. // Templates management
  613. $tblTemplate = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  614. $sql = "SELECT id FROM $tblTemplate
  615. WHERE
  616. course_code = '".$courseInfo['code']."' AND
  617. user_id = '".api_get_user_id()."' AND
  618. ref_doc = '".$row['id']."'";
  619. $templateResult = Database::query($sql);
  620. $row['is_template'] = (Database::num_rows($templateResult) > 0) ? 1 : 0;
  621. }
  622. $row['basename'] = basename($row['path']);
  623. // Just filling $document_data.
  624. $documentData[$row['id']] = $row;
  625. }
  626. // Only for the student we filter the results see BT#1652
  627. if (!$isCoach && !$isAllowedToEdit) {
  628. // Checking parents visibility.
  629. $finalDocumentData = [];
  630. foreach ($documentData as $row) {
  631. $isVisible = self::check_visibility_tree(
  632. $row['id'],
  633. $courseInfo,
  634. $sessionId,
  635. api_get_user_id(),
  636. $toGroupId
  637. );
  638. if ($isVisible) {
  639. $finalDocumentData[$row['id']] = $row;
  640. }
  641. }
  642. } else {
  643. $finalDocumentData = $documentData;
  644. }
  645. return $finalDocumentData;
  646. } else {
  647. return [];
  648. }
  649. }
  650. /**
  651. * Gets the paths of all folders in a course
  652. * can show all folders (except for the deleted ones) or only visible ones.
  653. *
  654. * @param array $_course
  655. * @param int $groupIid iid
  656. * @param bool $can_see_invisible
  657. * @param bool $getInvisibleList
  658. * @param string $path current path
  659. *
  660. * @return array with paths
  661. */
  662. public static function get_all_document_folders(
  663. $_course,
  664. $groupIid = 0,
  665. $can_see_invisible = false,
  666. $getInvisibleList = false,
  667. $path = ''
  668. ) {
  669. if (empty($_course)) {
  670. return [];
  671. }
  672. $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  673. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  674. $groupIid = (int) $groupIid;
  675. $document_folders = [];
  676. $students = CourseManager::get_user_list_from_course_code(
  677. $_course['code'],
  678. api_get_session_id()
  679. );
  680. $conditionList = [];
  681. if (!empty($students)) {
  682. foreach ($students as $studentId => $studentInfo) {
  683. $conditionList[] = '/shared_folder/sf_user_'.$studentInfo['user_id'];
  684. }
  685. }
  686. $groupCondition = " last.to_group_id = $groupIid";
  687. if (empty($groupIid)) {
  688. $groupCondition = ' (last.to_group_id = 0 OR last.to_group_id IS NULL)';
  689. }
  690. $show_users_condition = '';
  691. if (api_get_setting('show_users_folders') === 'false') {
  692. $show_users_condition = " AND docs.path NOT LIKE '%shared_folder%'";
  693. }
  694. if ($can_see_invisible) {
  695. // condition for the session
  696. $session_id = api_get_session_id();
  697. //$condition_session = api_get_session_condition($session_id, true, false, 'docs.session_id');
  698. $session_id = $session_id ?: api_get_session_id();
  699. $condition_session = " AND (last.session_id = '$session_id' OR (last.session_id = '0' OR last.session_id IS NULL) )";
  700. $condition_session .= self::getSessionFolderFilters($path, $session_id);
  701. if ($groupIid != 0) {
  702. $sql = "SELECT DISTINCT docs.id, path
  703. FROM $TABLE_ITEMPROPERTY AS last
  704. INNER JOIN $TABLE_DOCUMENT AS docs
  705. ON (
  706. docs.id = last.ref AND
  707. docs.c_id = last.c_id
  708. )
  709. WHERE
  710. last.tool = '".TOOL_DOCUMENT."' AND
  711. last.c_id = {$_course['real_id']} AND
  712. docs.c_id = {$_course['real_id']} AND
  713. docs.filetype = 'folder' AND
  714. $groupCondition AND
  715. docs.path NOT LIKE '%shared_folder%' AND
  716. docs.path NOT LIKE '%_DELETED_%' AND
  717. last.visibility <> 2
  718. $condition_session ";
  719. } else {
  720. $sql = "SELECT DISTINCT docs.id, path
  721. FROM $TABLE_ITEMPROPERTY AS last
  722. INNER JOIN $TABLE_DOCUMENT AS docs
  723. ON (
  724. docs.id = last.ref AND
  725. docs.c_id = last.c_id
  726. )
  727. WHERE
  728. last.tool = '".TOOL_DOCUMENT."' AND
  729. last.c_id = {$_course['real_id']} AND
  730. docs.c_id = {$_course['real_id']} AND
  731. docs.filetype = 'folder' AND
  732. docs.path NOT LIKE '%_DELETED_%' AND
  733. $groupCondition AND
  734. last.visibility <> 2
  735. $show_users_condition
  736. $condition_session
  737. ";
  738. }
  739. $result = Database::query($sql);
  740. if ($result && Database::num_rows($result) != 0) {
  741. while ($row = Database::fetch_array($result, 'ASSOC')) {
  742. if (self::is_folder_to_avoid($row['path'])) {
  743. continue;
  744. }
  745. if (strpos($row['path'], '/shared_folder/') !== false) {
  746. if (!in_array($row['path'], $conditionList)) {
  747. continue;
  748. }
  749. }
  750. $document_folders[$row['id']] = $row['path'];
  751. }
  752. if (!empty($document_folders)) {
  753. natsort($document_folders);
  754. }
  755. return $document_folders;
  756. } else {
  757. return false;
  758. }
  759. } else {
  760. // No invisible folders
  761. // Condition for the session
  762. $session_id = api_get_session_id();
  763. $condition_session = api_get_session_condition(
  764. $session_id,
  765. true,
  766. true, // needed to don't show files in elfinder browser
  767. 'docs.session_id'
  768. );
  769. $visibilityCondition = 'last.visibility = 1';
  770. $fileType = "docs.filetype = 'folder' AND";
  771. if ($getInvisibleList) {
  772. $visibilityCondition = 'last.visibility = 0';
  773. $fileType = '';
  774. }
  775. //get visible folders
  776. $sql = "SELECT DISTINCT docs.id, path
  777. FROM
  778. $TABLE_ITEMPROPERTY AS last
  779. INNER JOIN $TABLE_DOCUMENT AS docs
  780. ON (docs.id = last.ref AND last.c_id = docs.c_id)
  781. WHERE
  782. $fileType
  783. last.tool = '".TOOL_DOCUMENT."' AND
  784. $groupCondition AND
  785. $visibilityCondition
  786. $show_users_condition
  787. $condition_session AND
  788. last.c_id = {$_course['real_id']} AND
  789. docs.c_id = {$_course['real_id']} ";
  790. $result = Database::query($sql);
  791. $visibleFolders = [];
  792. while ($row = Database::fetch_array($result, 'ASSOC')) {
  793. $visibleFolders[$row['id']] = $row['path'];
  794. }
  795. if ($getInvisibleList) {
  796. return $visibleFolders;
  797. }
  798. //get invisible folders
  799. $sql = "SELECT DISTINCT docs.id, path
  800. FROM $TABLE_ITEMPROPERTY AS last
  801. INNER JOIN $TABLE_DOCUMENT AS docs
  802. ON (docs.id = last.ref AND last.c_id = docs.c_id)
  803. WHERE
  804. docs.filetype = 'folder' AND
  805. last.tool = '".TOOL_DOCUMENT."' AND
  806. $groupCondition AND
  807. last.visibility = 0 $condition_session AND
  808. last.c_id = {$_course['real_id']} AND
  809. docs.c_id = {$_course['real_id']} ";
  810. $result = Database::query($sql);
  811. $invisibleFolders = [];
  812. while ($row = Database::fetch_array($result, 'ASSOC')) {
  813. //get visible folders in the invisible ones -> they are invisible too
  814. $sql = "SELECT DISTINCT docs.id, path
  815. FROM $TABLE_ITEMPROPERTY AS last
  816. INNER JOIN $TABLE_DOCUMENT AS docs
  817. ON (docs.id = last.ref AND docs.c_id = last.c_id)
  818. WHERE
  819. docs.path LIKE '".Database::escape_string($row['path'].'/%')."' AND
  820. docs.filetype = 'folder' AND
  821. last.tool = '".TOOL_DOCUMENT."' AND
  822. $groupCondition AND
  823. last.visibility = 1 $condition_session AND
  824. last.c_id = {$_course['real_id']} AND
  825. docs.c_id = {$_course['real_id']} ";
  826. $folder_in_invisible_result = Database::query($sql);
  827. while ($folders_in_invisible_folder = Database::fetch_array($folder_in_invisible_result, 'ASSOC')) {
  828. $invisibleFolders[$folders_in_invisible_folder['id']] = $folders_in_invisible_folder['path'];
  829. }
  830. }
  831. // If both results are arrays -> //calculate the difference between the 2 arrays -> only visible folders are left :)
  832. if (is_array($visibleFolders) && is_array($invisibleFolders)) {
  833. $document_folders = array_diff($visibleFolders, $invisibleFolders);
  834. natsort($document_folders);
  835. return $document_folders;
  836. } elseif (is_array($visibleFolders)) {
  837. natsort($visibleFolders);
  838. return $visibleFolders;
  839. } else {
  840. //no visible folders found
  841. return false;
  842. }
  843. }
  844. }
  845. /**
  846. * This check if a document has the readonly property checked, then see if the user
  847. * is the owner of this file, if all this is true then return true.
  848. *
  849. * @param array $_course
  850. * @param int $user_id id of the current user
  851. * @param string $file path stored in the database (if not defined, $documentId must be used)
  852. * @param int $document_id in case you dont have the file path ,
  853. * insert the id of the file here and leave $file in blank ''
  854. * @param bool $to_delete
  855. * @param int $sessionId
  856. *
  857. * @return bool true/false
  858. * */
  859. public static function check_readonly(
  860. $_course,
  861. $user_id,
  862. $file = null,
  863. $document_id = 0,
  864. $to_delete = false,
  865. $sessionId = null,
  866. $documentId = null
  867. ) {
  868. if (empty($sessionId)) {
  869. $sessionId = api_get_session_id();
  870. } else {
  871. $sessionId = intval($sessionId);
  872. }
  873. if (empty($document_id) || !is_numeric($document_id)) {
  874. $document_id = self::get_document_id($_course, $file, $sessionId);
  875. } else {
  876. $document_id = intval($document_id);
  877. }
  878. $TABLE_PROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  879. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  880. $course_id = $_course['real_id'];
  881. if ($to_delete) {
  882. if (self::is_folder($_course, $document_id)) {
  883. if (!empty($file)) {
  884. $path = Database::escape_string($file);
  885. // Check
  886. $sql = "SELECT td.id, readonly, tp.insert_user_id
  887. FROM $TABLE_DOCUMENT td
  888. INNER JOIN $TABLE_PROPERTY tp
  889. ON (td.c_id = tp.c_id AND tp.ref= td.id)
  890. WHERE
  891. td.c_id = $course_id AND
  892. tp.c_id = $course_id AND
  893. td.session_id = $sessionId AND
  894. (path='".$path."' OR path LIKE BINARY '".$path."/%' ) ";
  895. // Get all id's of documents that are deleted
  896. $what_to_check_result = Database::query($sql);
  897. if ($what_to_check_result && Database::num_rows($what_to_check_result) != 0) {
  898. // file with readonly set to 1 exist?
  899. $readonly_set = false;
  900. while ($row = Database::fetch_array($what_to_check_result)) {
  901. //query to delete from item_property table
  902. if ($row['readonly'] == 1) {
  903. if (!($row['insert_user_id'] == $user_id)) {
  904. $readonly_set = true;
  905. break;
  906. }
  907. }
  908. }
  909. if ($readonly_set) {
  910. return true;
  911. }
  912. }
  913. }
  914. return false;
  915. }
  916. }
  917. if (!empty($document_id)) {
  918. $sql = "SELECT a.insert_user_id, b.readonly
  919. FROM $TABLE_PROPERTY a
  920. INNER JOIN $TABLE_DOCUMENT b
  921. ON (a.c_id = b.c_id AND a.ref= b.id)
  922. WHERE
  923. a.c_id = $course_id AND
  924. b.c_id = $course_id AND
  925. a.ref = $document_id
  926. LIMIT 1";
  927. $result = Database::query($sql);
  928. $doc_details = Database::fetch_array($result, 'ASSOC');
  929. if ($doc_details['readonly'] == 1) {
  930. return !($doc_details['insert_user_id'] == $user_id || api_is_platform_admin());
  931. }
  932. }
  933. return false;
  934. }
  935. /**
  936. * This check if a document is a folder or not.
  937. *
  938. * @param array $_course
  939. * @param int $id document id
  940. *
  941. * @return bool true/false
  942. * */
  943. public static function is_folder($_course, $id)
  944. {
  945. $table = Database::get_course_table(TABLE_DOCUMENT);
  946. if (empty($_course)) {
  947. return false;
  948. }
  949. $course_id = $_course['real_id'];
  950. $id = (int) $id;
  951. $sql = "SELECT filetype FROM $table
  952. WHERE c_id = $course_id AND id= $id";
  953. $result = Database::fetch_array(Database::query($sql), 'ASSOC');
  954. return $result['filetype'] == 'folder';
  955. }
  956. /**
  957. * @param int $document_id
  958. * @param array $course_info
  959. * @param int $session_id
  960. * @param bool $remove_content_from_db
  961. */
  962. public static function deleteDocumentFromDb(
  963. $document_id,
  964. $course_info = [],
  965. $session_id = 0,
  966. $remove_content_from_db = false
  967. ) {
  968. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  969. $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  970. // Deleting from the DB
  971. $user_id = api_get_user_id();
  972. $document_id = intval($document_id);
  973. if (empty($course_info)) {
  974. $course_info = api_get_course_info();
  975. }
  976. if (empty($session_id)) {
  977. $session_id = api_get_session_id();
  978. }
  979. // Soft DB delete
  980. api_item_property_update(
  981. $course_info,
  982. TOOL_DOCUMENT,
  983. $document_id,
  984. 'delete',
  985. $user_id,
  986. null,
  987. null,
  988. null,
  989. null,
  990. $session_id
  991. );
  992. self::delete_document_from_search_engine($course_info['code'], $document_id);
  993. self::unset_document_as_template($document_id, $course_info['code'], $user_id);
  994. //Hard DB delete
  995. if ($remove_content_from_db) {
  996. $sql = "DELETE FROM $TABLE_ITEMPROPERTY
  997. WHERE
  998. c_id = {$course_info['real_id']} AND
  999. ref = ".$document_id." AND
  1000. tool='".TOOL_DOCUMENT."'";
  1001. Database::query($sql);
  1002. $sql = "DELETE FROM $TABLE_DOCUMENT
  1003. WHERE c_id = {$course_info['real_id']} AND id = ".$document_id;
  1004. Database::query($sql);
  1005. }
  1006. }
  1007. /**
  1008. * This deletes a document by changing visibility to 2, renaming it to filename_DELETED_#id
  1009. * Files/folders that are inside a deleted folder get visibility 2.
  1010. *
  1011. * @param array $_course
  1012. * @param string $path Path stored in the database
  1013. * @param string $base_work_dir Path to the documents folder (if not defined, $documentId must be used)
  1014. * @param int $sessionId The ID of the session, if any
  1015. * @param int $documentId The document id, if available
  1016. * @param int $groupId iid
  1017. *
  1018. * @return bool true/false
  1019. *
  1020. * @todo now only files/folders in a folder get visibility 2, we should rename them too.
  1021. * @todo We should be able to get rid of this later when using only documentId (check further usage)
  1022. */
  1023. public static function delete_document(
  1024. $_course,
  1025. $path = null,
  1026. $base_work_dir = null,
  1027. $sessionId = null,
  1028. $documentId = null,
  1029. $groupId = 0
  1030. ) {
  1031. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  1032. $groupId = intval($groupId);
  1033. if (empty($groupId)) {
  1034. $groupId = api_get_group_id();
  1035. }
  1036. $sessionId = intval($sessionId);
  1037. if (empty($sessionId)) {
  1038. $sessionId = api_get_session_id();
  1039. }
  1040. $course_id = $_course['real_id'];
  1041. if (empty($course_id)) {
  1042. return false;
  1043. }
  1044. if (empty($base_work_dir)) {
  1045. return false;
  1046. }
  1047. if (empty($documentId)) {
  1048. $documentId = self::get_document_id($_course, $path, $sessionId);
  1049. $docInfo = self::get_document_data_by_id(
  1050. $documentId,
  1051. $_course['code'],
  1052. false,
  1053. $sessionId
  1054. );
  1055. $path = $docInfo['path'];
  1056. } else {
  1057. $docInfo = self::get_document_data_by_id(
  1058. $documentId,
  1059. $_course['code'],
  1060. false,
  1061. $sessionId
  1062. );
  1063. if (empty($docInfo)) {
  1064. return false;
  1065. }
  1066. $path = $docInfo['path'];
  1067. }
  1068. $documentId = intval($documentId);
  1069. if (empty($path) || empty($docInfo) || empty($documentId)) {
  1070. return false;
  1071. }
  1072. $itemInfo = api_get_item_property_info(
  1073. $_course['real_id'],
  1074. TOOL_DOCUMENT,
  1075. $documentId,
  1076. $sessionId,
  1077. $groupId
  1078. );
  1079. if (empty($itemInfo)) {
  1080. return false;
  1081. }
  1082. // File was already deleted.
  1083. if ($itemInfo['lastedit_type'] == 'DocumentDeleted' ||
  1084. $itemInfo['lastedit_type'] == 'delete' ||
  1085. $itemInfo['visibility'] == 2
  1086. ) {
  1087. return false;
  1088. }
  1089. // Filtering by group.
  1090. if ($itemInfo['to_group_id'] != $groupId) {
  1091. return false;
  1092. }
  1093. $document_exists_in_disk = file_exists($base_work_dir.$path);
  1094. $new_path = $path.'_DELETED_'.$documentId;
  1095. $file_deleted_from_db = false;
  1096. $file_deleted_from_disk = false;
  1097. $file_renamed_from_disk = false;
  1098. if ($documentId) {
  1099. // Deleting doc from the DB.
  1100. self::deleteDocumentFromDb($documentId, $_course, $sessionId);
  1101. // Checking
  1102. // $file_exists_in_db = self::get_document_data_by_id($documentId, $_course['code']);
  1103. $file_deleted_from_db = true;
  1104. }
  1105. // Looking for children.
  1106. if ($docInfo['filetype'] == 'folder') {
  1107. $cleanPath = Database::escape_string($path);
  1108. // Deleted files inside this folder.
  1109. $sql = "SELECT id FROM $TABLE_DOCUMENT
  1110. WHERE
  1111. c_id = $course_id AND
  1112. session_id = $sessionId AND
  1113. path LIKE BINARY '".$cleanPath."/%'";
  1114. // Get all id's of documents that are deleted.
  1115. $result = Database::query($sql);
  1116. if ($result && Database::num_rows($result) != 0) {
  1117. // Recursive delete.
  1118. while ($row = Database::fetch_array($result)) {
  1119. self::delete_document(
  1120. $_course,
  1121. null,
  1122. $base_work_dir,
  1123. $sessionId,
  1124. $row['id'],
  1125. $groupId
  1126. );
  1127. }
  1128. }
  1129. }
  1130. if ($document_exists_in_disk) {
  1131. if (api_get_setting('permanently_remove_deleted_files') === 'true') {
  1132. // Delete documents, do it like this so metadata gets deleted too
  1133. my_delete($base_work_dir.$path);
  1134. // Hard delete.
  1135. self::deleteDocumentFromDb($documentId, $_course, $sessionId, true);
  1136. $file_deleted_from_disk = true;
  1137. } else {
  1138. // Set visibility to 2 and rename file/folder to xxx_DELETED_#id (soft delete)
  1139. if (is_file($base_work_dir.$path) || is_dir($base_work_dir.$path)) {
  1140. if (rename($base_work_dir.$path, $base_work_dir.$new_path)) {
  1141. $new_path = Database::escape_string($new_path);
  1142. $sql = "UPDATE $TABLE_DOCUMENT
  1143. SET path = '".$new_path."'
  1144. WHERE
  1145. c_id = $course_id AND
  1146. session_id = $sessionId AND
  1147. id = ".$documentId;
  1148. Database::query($sql);
  1149. // Soft delete.
  1150. self::deleteDocumentFromDb($documentId, $_course, $sessionId);
  1151. // Change path of sub folders and documents in database.
  1152. $old_item_path = $docInfo['path'];
  1153. $new_item_path = $new_path.substr($old_item_path, strlen($path));
  1154. $new_item_path = Database::escape_string($new_item_path);
  1155. $sql = "UPDATE $TABLE_DOCUMENT
  1156. SET path = '".$new_item_path."'
  1157. WHERE
  1158. c_id = $course_id AND
  1159. session_id = $sessionId AND
  1160. id = ".$documentId;
  1161. Database::query($sql);
  1162. $file_renamed_from_disk = true;
  1163. } else {
  1164. // Couldn't rename - file permissions problem?
  1165. error_log(
  1166. __FILE__.' '.__LINE__.': Error renaming '.$base_work_dir.$path.' to '.$base_work_dir.$new_path.'. This is probably due to file permissions',
  1167. 0
  1168. );
  1169. }
  1170. }
  1171. }
  1172. }
  1173. // Checking inconsistency
  1174. //error_log('Doc status: (1 del db :'.($file_deleted_from_db?'yes':'no').') - (2 del disk: '.($file_deleted_from_disk?'yes':'no').') - (3 ren disk: '.($file_renamed_from_disk?'yes':'no').')');
  1175. if ($file_deleted_from_db && $file_deleted_from_disk ||
  1176. $file_deleted_from_db && $file_renamed_from_disk
  1177. ) {
  1178. return true;
  1179. } else {
  1180. //Something went wrong
  1181. //The file or directory isn't there anymore (on the filesystem)
  1182. // This means it has been removed externally. To prevent a
  1183. // blocking error from happening, we drop the related items from the
  1184. // item_property and the document table.
  1185. error_log(
  1186. __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',
  1187. 0
  1188. );
  1189. return false;
  1190. }
  1191. }
  1192. /**
  1193. * Removes documents from search engine database.
  1194. *
  1195. * @param string $course_id Course code
  1196. * @param int $document_id Document id to delete
  1197. */
  1198. public static function delete_document_from_search_engine($course_id, $document_id)
  1199. {
  1200. // remove from search engine if enabled
  1201. if (api_get_setting('search_enabled') === 'true') {
  1202. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  1203. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  1204. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_DOCUMENT, $document_id);
  1205. $res = Database::query($sql);
  1206. if (Database::num_rows($res) > 0) {
  1207. $row2 = Database::fetch_array($res);
  1208. $di = new ChamiloIndexer();
  1209. $di->remove_document($row2['search_did']);
  1210. }
  1211. $sql = 'DELETE FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  1212. $sql = sprintf($sql, $tbl_se_ref, $course_id, TOOL_DOCUMENT, $document_id);
  1213. Database::query($sql);
  1214. // remove terms from db
  1215. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  1216. delete_all_values_for_item($course_id, TOOL_DOCUMENT, $document_id);
  1217. }
  1218. }
  1219. /**
  1220. * Gets the id of a document with a given path.
  1221. *
  1222. * @param array $courseInfo
  1223. * @param string $path
  1224. * @param int $sessionId
  1225. *
  1226. * @return int id of document / false if no doc found
  1227. */
  1228. public static function get_document_id($courseInfo, $path, $sessionId = null)
  1229. {
  1230. $table = Database::get_course_table(TABLE_DOCUMENT);
  1231. $courseId = $courseInfo['real_id'];
  1232. if (!isset($sessionId)) {
  1233. $sessionId = api_get_session_id();
  1234. } else {
  1235. $sessionId = intval($sessionId);
  1236. }
  1237. $path = Database::escape_string($path);
  1238. if (!empty($courseId) && !empty($path)) {
  1239. $sql = "SELECT id FROM $table
  1240. WHERE
  1241. c_id = $courseId AND
  1242. path LIKE BINARY '$path' AND
  1243. session_id = $sessionId
  1244. LIMIT 1";
  1245. $result = Database::query($sql);
  1246. if (Database::num_rows($result)) {
  1247. $row = Database::fetch_array($result);
  1248. return intval($row['id']);
  1249. }
  1250. }
  1251. return false;
  1252. }
  1253. /**
  1254. * Gets the document data with a given id.
  1255. *
  1256. * @param int $id Document Id (id field in c_document table)
  1257. * @param string $course_code Course code
  1258. * @param bool $load_parents load folder parents
  1259. * @param int $session_id The session ID,
  1260. * 0 if requires context *out of* session, and null to use global context
  1261. * @param bool $ignoreDeleted
  1262. *
  1263. * @return array document content
  1264. */
  1265. public static function get_document_data_by_id(
  1266. $id,
  1267. $course_code,
  1268. $load_parents = false,
  1269. $session_id = null,
  1270. $ignoreDeleted = false
  1271. ) {
  1272. $course_info = api_get_course_info($course_code);
  1273. $course_id = $course_info['real_id'];
  1274. if (empty($course_info)) {
  1275. return false;
  1276. }
  1277. $session_id = empty($session_id) ? api_get_session_id() : (int) $session_id;
  1278. $groupId = api_get_group_id();
  1279. $www = api_get_path(WEB_COURSE_PATH).$course_info['path'].'/document';
  1280. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  1281. $id = (int) $id;
  1282. $sessionCondition = api_get_session_condition($session_id, true, true);
  1283. $sql = "SELECT * FROM $TABLE_DOCUMENT
  1284. WHERE c_id = $course_id $sessionCondition AND id = $id";
  1285. if ($ignoreDeleted) {
  1286. $sql .= " AND path NOT LIKE '%_DELETED_%' ";
  1287. }
  1288. $result = Database::query($sql);
  1289. $courseParam = '&cidReq='.$course_code.'&id='.$id.'&id_session='.$session_id.'&gidReq='.$groupId;
  1290. if ($result && Database::num_rows($result) == 1) {
  1291. $row = Database::fetch_array($result, 'ASSOC');
  1292. //@todo need to clarify the name of the URLs not nice right now
  1293. $url_path = urlencode($row['path']);
  1294. $path = str_replace('%2F', '/', $url_path);
  1295. $pathinfo = pathinfo($row['path']);
  1296. $row['url'] = api_get_path(WEB_CODE_PATH).'document/showinframes.php?id='.$id.$courseParam;
  1297. $row['document_url'] = api_get_path(WEB_CODE_PATH).'document/document.php?id='.$id.$courseParam;
  1298. $row['absolute_path'] = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$row['path'];
  1299. $row['absolute_path_from_document'] = '/document'.$row['path'];
  1300. $row['absolute_parent_path'] = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$pathinfo['dirname'].'/';
  1301. $row['direct_url'] = $www.$path;
  1302. $row['basename'] = basename($row['path']);
  1303. if (dirname($row['path']) == '.') {
  1304. $row['parent_id'] = '0';
  1305. } else {
  1306. $row['parent_id'] = self::get_document_id($course_info, dirname($row['path']), $session_id);
  1307. if (empty($row['parent_id'])) {
  1308. // Try one more with session id = 0
  1309. $row['parent_id'] = self::get_document_id($course_info, dirname($row['path']), 0);
  1310. }
  1311. }
  1312. $parents = [];
  1313. //Use to generate parents (needed for the breadcrumb)
  1314. //@todo sorry but this for is here because there's not a parent_id in the document table so we parsed the path!!
  1315. if ($load_parents) {
  1316. $dir_array = explode('/', $row['path']);
  1317. $dir_array = array_filter($dir_array);
  1318. $array_len = count($dir_array) + 1;
  1319. $real_dir = '';
  1320. for ($i = 1; $i < $array_len; $i++) {
  1321. $real_dir .= '/'.(isset($dir_array[$i]) ? $dir_array[$i] : '');
  1322. $parent_id = self::get_document_id($course_info, $real_dir);
  1323. if ($session_id != 0 && empty($parent_id)) {
  1324. $parent_id = self::get_document_id($course_info, $real_dir, 0);
  1325. }
  1326. if (!empty($parent_id)) {
  1327. $sub_document_data = self::get_document_data_by_id(
  1328. $parent_id,
  1329. $course_code,
  1330. false,
  1331. $session_id
  1332. );
  1333. if ($session_id != 0 and !$sub_document_data) {
  1334. $sub_document_data = self::get_document_data_by_id(
  1335. $parent_id,
  1336. $course_code,
  1337. false,
  1338. 0
  1339. );
  1340. }
  1341. //@todo add visibility here
  1342. $parents[] = $sub_document_data;
  1343. }
  1344. }
  1345. }
  1346. $row['parents'] = $parents;
  1347. return $row;
  1348. }
  1349. return false;
  1350. }
  1351. /**
  1352. * Allow to set a specific document as a new template for CKeditor
  1353. * for a particular user in a particular course.
  1354. *
  1355. * @param string $title
  1356. * @param string $description
  1357. * @param int $document_id_for_template the document id
  1358. * @param string $course_code
  1359. * @param int $user_id
  1360. * @param string $image
  1361. *
  1362. * @return bool
  1363. */
  1364. public static function set_document_as_template(
  1365. $title,
  1366. $description,
  1367. $document_id_for_template,
  1368. $course_code,
  1369. $user_id,
  1370. $image
  1371. ) {
  1372. // Database table definition
  1373. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  1374. $params = [
  1375. 'title' => $title,
  1376. 'description' => $description,
  1377. 'course_code' => $course_code,
  1378. 'user_id' => $user_id,
  1379. 'ref_doc' => $document_id_for_template,
  1380. 'image' => $image,
  1381. ];
  1382. Database::insert($table_template, $params);
  1383. return true;
  1384. }
  1385. /**
  1386. * Unset a document as template.
  1387. *
  1388. * @param int $document_id
  1389. * @param string $course_code
  1390. * @param int $user_id
  1391. */
  1392. public static function unset_document_as_template(
  1393. $document_id,
  1394. $course_code,
  1395. $user_id
  1396. ) {
  1397. $table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);
  1398. $course_code = Database::escape_string($course_code);
  1399. $user_id = intval($user_id);
  1400. $document_id = intval($document_id);
  1401. $sql = 'SELECT id FROM '.$table_template.'
  1402. WHERE
  1403. course_code="'.$course_code.'" AND
  1404. user_id="'.$user_id.'" AND
  1405. ref_doc="'.$document_id.'"';
  1406. $result = Database::query($sql);
  1407. $template_id = Database::result($result, 0, 0);
  1408. my_delete(api_get_path(SYS_CODE_PATH).'upload/template_thumbnails/'.$template_id.'.jpg');
  1409. $sql = 'DELETE FROM '.$table_template.'
  1410. WHERE
  1411. course_code="'.$course_code.'" AND
  1412. user_id="'.$user_id.'" AND
  1413. ref_doc="'.$document_id.'"';
  1414. Database::query($sql);
  1415. }
  1416. /**
  1417. * Return true if the documentpath have visibility=1 as
  1418. * item_property (you should use the is_visible_by_id).
  1419. *
  1420. * @param string $doc_path the relative complete path of the document
  1421. * @param array $course the _course array info of the document's course
  1422. * @param int
  1423. * @param string
  1424. *
  1425. * @return bool
  1426. */
  1427. public static function is_visible(
  1428. $doc_path,
  1429. $course,
  1430. $session_id = 0,
  1431. $file_type = 'file'
  1432. ) {
  1433. $docTable = Database::get_course_table(TABLE_DOCUMENT);
  1434. $propTable = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1435. $course_id = $course['real_id'];
  1436. // note the extra / at the end of doc_path to match every path in
  1437. // the document table that is part of the document path
  1438. $session_id = intval($session_id);
  1439. $condition = "AND d.session_id IN ('$session_id', '0') ";
  1440. // The " d.filetype='file' " let the user see a file even if the folder is hidden see #2198
  1441. /*
  1442. When using hotpotatoes files, a new html files are generated
  1443. in the hotpotatoes folder to display the test.
  1444. The genuine html file is copied to math4.htm(user_id).t.html
  1445. Images files are not copied, and keep same name.
  1446. To check the html file visibility, we don't have to check file math4.htm(user_id).t.html but file math4.htm
  1447. In this case, we have to remove (user_id).t.html to check the visibility of the file
  1448. For images, we just check the path of the image file.
  1449. Exemple of hotpotatoes folder :
  1450. A.jpg
  1451. maths4-consigne.jpg
  1452. maths4.htm
  1453. maths4.htm1.t.html
  1454. maths4.htm52.t.html
  1455. maths4.htm654.t.html
  1456. omega.jpg
  1457. theta.jpg
  1458. */
  1459. if (strpos($doc_path, 'HotPotatoes_files') && preg_match("/\.t\.html$/", $doc_path)) {
  1460. $doc_path = substr($doc_path, 0, strlen($doc_path) - 7 - strlen(api_get_user_id()));
  1461. }
  1462. if (!in_array($file_type, ['file', 'folder'])) {
  1463. $file_type = 'file';
  1464. }
  1465. $doc_path = Database::escape_string($doc_path).'/';
  1466. $sql = "SELECT visibility
  1467. FROM $docTable d
  1468. INNER JOIN $propTable ip
  1469. ON (d.id = ip.ref AND d.c_id = ip.c_id)
  1470. WHERE
  1471. d.c_id = $course_id AND
  1472. ip.c_id = $course_id AND
  1473. ip.tool = '".TOOL_DOCUMENT."' $condition AND
  1474. filetype = '$file_type' AND
  1475. locate(concat(path,'/'), '$doc_path')=1
  1476. ";
  1477. $result = Database::query($sql);
  1478. $is_visible = false;
  1479. if (Database::num_rows($result) > 0) {
  1480. $row = Database::fetch_array($result, 'ASSOC');
  1481. if ($row['visibility'] == 1) {
  1482. $is_visible = api_is_allowed_in_course() || api_is_platform_admin();
  1483. }
  1484. }
  1485. /* improved protection of documents viewable directly through the url:
  1486. incorporates the same protections of the course at the url of
  1487. documents:
  1488. access allowed for the whole world Open, access allowed for
  1489. users registered on the platform Private access, document accessible
  1490. only to course members (see the Users list), Completely closed;
  1491. the document is only accessible to the course admin and
  1492. teaching assistants.*/
  1493. //return $_SESSION ['is_allowed_in_course'] || api_is_platform_admin();
  1494. return $is_visible;
  1495. }
  1496. /**
  1497. * Return true if user can see a file.
  1498. *
  1499. * @param int document id
  1500. * @param array course info
  1501. * @param int
  1502. * @param int
  1503. * @param bool
  1504. *
  1505. * @return bool
  1506. */
  1507. public static function is_visible_by_id(
  1508. $doc_id,
  1509. $course_info,
  1510. $session_id,
  1511. $user_id,
  1512. $admins_can_see_everything = true,
  1513. $userIsSubscribed = null
  1514. ) {
  1515. $user_in_course = false;
  1516. //1. Checking the course array
  1517. if (empty($course_info)) {
  1518. $course_info = api_get_course_info();
  1519. if (empty($course_info)) {
  1520. return false;
  1521. }
  1522. }
  1523. $doc_id = intval($doc_id);
  1524. $session_id = intval($session_id);
  1525. //2. Course and Session visibility are handle in local.inc.php/global.inc.php
  1526. //3. Checking if user exist in course/session
  1527. if ($session_id == 0) {
  1528. if (is_null($userIsSubscribed)) {
  1529. $userIsSubscribed = CourseManager::is_user_subscribed_in_course(
  1530. $user_id,
  1531. $course_info['code']
  1532. );
  1533. }
  1534. if ($userIsSubscribed === true || api_is_platform_admin()) {
  1535. $user_in_course = true;
  1536. }
  1537. // Check if course is open then we can consider that the student is registered to the course
  1538. if (isset($course_info) &&
  1539. in_array(
  1540. $course_info['visibility'],
  1541. [COURSE_VISIBILITY_OPEN_PLATFORM, COURSE_VISIBILITY_OPEN_WORLD]
  1542. )
  1543. ) {
  1544. $user_in_course = true;
  1545. }
  1546. } else {
  1547. $user_status = SessionManager::get_user_status_in_course_session(
  1548. $user_id,
  1549. $course_info['real_id'],
  1550. $session_id
  1551. );
  1552. if (in_array($user_status, ['0', '2', '6'])) {
  1553. //is true if is an student, course session teacher or coach
  1554. $user_in_course = true;
  1555. }
  1556. if (api_is_platform_admin()) {
  1557. $user_in_course = true;
  1558. }
  1559. }
  1560. // 4. Checking document visibility (i'm repeating the code in order to be more clear when reading ) - jm
  1561. if ($user_in_course) {
  1562. // 4.1 Checking document visibility for a Course
  1563. if ($session_id == 0) {
  1564. $item_info = api_get_item_property_info(
  1565. $course_info['real_id'],
  1566. 'document',
  1567. $doc_id,
  1568. 0
  1569. );
  1570. if (isset($item_info['visibility'])) {
  1571. // True for admins if document exists
  1572. if ($admins_can_see_everything && api_is_platform_admin()) {
  1573. return true;
  1574. }
  1575. if ($item_info['visibility'] == 1) {
  1576. return true;
  1577. }
  1578. }
  1579. } else {
  1580. // 4.2 Checking document visibility for a Course in a Session
  1581. $item_info = api_get_item_property_info(
  1582. $course_info['real_id'],
  1583. 'document',
  1584. $doc_id,
  1585. 0
  1586. );
  1587. $item_info_in_session = api_get_item_property_info(
  1588. $course_info['real_id'],
  1589. 'document',
  1590. $doc_id,
  1591. $session_id
  1592. );
  1593. // True for admins if document exists
  1594. if (isset($item_info['visibility'])) {
  1595. if ($admins_can_see_everything && api_is_platform_admin()) {
  1596. return true;
  1597. }
  1598. }
  1599. if (isset($item_info_in_session['visibility'])) {
  1600. if ($item_info_in_session['visibility'] == 1) {
  1601. return true;
  1602. }
  1603. } else {
  1604. if ($item_info['visibility'] == 1) {
  1605. return true;
  1606. }
  1607. }
  1608. }
  1609. } elseif ($admins_can_see_everything && api_is_platform_admin()) {
  1610. return true;
  1611. }
  1612. return false;
  1613. }
  1614. /**
  1615. * Allow attach a certificate to a course.
  1616. *
  1617. * @todo move to certificate.lib.php
  1618. *
  1619. * @param string $course_id
  1620. * @param int $document_id
  1621. * @param int $session_id
  1622. */
  1623. public static function attach_gradebook_certificate($course_id, $document_id, $session_id = 0)
  1624. {
  1625. $tbl_category = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1626. $session_id = intval($session_id);
  1627. if (empty($session_id)) {
  1628. $session_id = api_get_session_id();
  1629. }
  1630. if (empty($session_id)) {
  1631. $sql_session = 'AND (session_id = 0 OR isnull(session_id)) ';
  1632. } elseif ($session_id > 0) {
  1633. $sql_session = 'AND session_id='.intval($session_id);
  1634. } else {
  1635. $sql_session = '';
  1636. }
  1637. $sql = 'UPDATE '.$tbl_category.' SET document_id="'.intval($document_id).'"
  1638. WHERE course_code="'.Database::escape_string($course_id).'" '.$sql_session;
  1639. Database::query($sql);
  1640. }
  1641. /**
  1642. * get the document id of default certificate.
  1643. *
  1644. * @todo move to certificate.lib.php
  1645. *
  1646. * @param string $course_id
  1647. * @param int $session_id
  1648. *
  1649. * @return int The default certificate id
  1650. */
  1651. public static function get_default_certificate_id($course_id, $session_id = 0)
  1652. {
  1653. $tbl_category = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1654. $session_id = intval($session_id);
  1655. if (empty($session_id)) {
  1656. $session_id = api_get_session_id();
  1657. }
  1658. if (empty($session_id)) {
  1659. $sql_session = 'AND (session_id = 0 OR isnull(session_id)) ';
  1660. } elseif ($session_id > 0) {
  1661. $sql_session = 'AND session_id='.intval($session_id);
  1662. } else {
  1663. $sql_session = '';
  1664. }
  1665. $sql = 'SELECT document_id FROM '.$tbl_category.'
  1666. WHERE course_code="'.Database::escape_string($course_id).'" '.$sql_session;
  1667. $rs = Database::query($sql);
  1668. $num = Database::num_rows($rs);
  1669. if ($num == 0) {
  1670. return null;
  1671. }
  1672. $row = Database::fetch_array($rs);
  1673. return $row['document_id'];
  1674. }
  1675. /**
  1676. * Allow replace user info in file html.
  1677. *
  1678. * @param int $user_id
  1679. * @param string $course_code
  1680. * @param int $sessionId
  1681. * @param bool $is_preview
  1682. *
  1683. * @return array
  1684. */
  1685. public static function replace_user_info_into_html(
  1686. $user_id,
  1687. $course_code,
  1688. $sessionId,
  1689. $is_preview = false
  1690. ) {
  1691. $user_id = intval($user_id);
  1692. $course_info = api_get_course_info($course_code);
  1693. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1694. $course_id = $course_info['real_id'];
  1695. $document_id = self::get_default_certificate_id(
  1696. $course_code,
  1697. $sessionId
  1698. );
  1699. $my_content_html = null;
  1700. if ($document_id) {
  1701. $sql = "SELECT path FROM $tbl_document
  1702. WHERE c_id = $course_id AND id = $document_id";
  1703. $rs = Database::query($sql);
  1704. $new_content = '';
  1705. $all_user_info = [];
  1706. if (Database::num_rows($rs)) {
  1707. $row = Database::fetch_array($rs);
  1708. $filepath = api_get_path(SYS_COURSE_PATH).$course_info['path'].'/document'.$row['path'];
  1709. if (is_file($filepath)) {
  1710. $my_content_html = file_get_contents($filepath);
  1711. }
  1712. $all_user_info = self::get_all_info_to_certificate(
  1713. $user_id,
  1714. $course_code,
  1715. $is_preview
  1716. );
  1717. $info_to_be_replaced_in_content_html = $all_user_info[0];
  1718. $info_to_replace_in_content_html = $all_user_info[1];
  1719. $new_content = str_replace(
  1720. $info_to_be_replaced_in_content_html,
  1721. $info_to_replace_in_content_html,
  1722. $my_content_html
  1723. );
  1724. }
  1725. return [
  1726. 'content' => $new_content,
  1727. 'variables' => $all_user_info,
  1728. ];
  1729. }
  1730. return [];
  1731. }
  1732. /**
  1733. * Return all content to replace and all content to be replace.
  1734. *
  1735. * @param int $user_id
  1736. * @param int $course_id
  1737. * @param bool $is_preview
  1738. *
  1739. * @return array
  1740. */
  1741. public static function get_all_info_to_certificate($user_id, $course_id, $is_preview = false)
  1742. {
  1743. $info_list = [];
  1744. $user_id = intval($user_id);
  1745. $course_info = api_get_course_info($course_id);
  1746. // Portal info
  1747. $organization_name = api_get_setting('Institution');
  1748. $portal_name = api_get_setting('siteName');
  1749. // Extra user data information
  1750. $extra_user_info_data = UserManager::get_extra_user_data(
  1751. $user_id,
  1752. false,
  1753. false,
  1754. false,
  1755. true
  1756. );
  1757. // get extra fields
  1758. $extraField = new ExtraField('user');
  1759. $extraFields = $extraField->get_all(['filter = ? AND visible_to_self = ?' => [1, 1]]);
  1760. // Student information
  1761. $user_info = api_get_user_info($user_id);
  1762. $first_name = $user_info['firstname'];
  1763. $last_name = $user_info['lastname'];
  1764. $username = $user_info['username'];
  1765. $official_code = $user_info['official_code'];
  1766. // Teacher information
  1767. $info_teacher_id = UserManager::get_user_id_of_course_admin_or_session_admin($course_info);
  1768. $teacher_info = api_get_user_info($info_teacher_id);
  1769. $teacher_first_name = $teacher_info['firstname'];
  1770. $teacher_last_name = $teacher_info['lastname'];
  1771. // info gradebook certificate
  1772. $info_grade_certificate = UserManager::get_info_gradebook_certificate($course_id, $user_id);
  1773. $date_certificate = $info_grade_certificate['created_at'];
  1774. $date_long_certificate = '';
  1775. $date_no_time = api_convert_and_format_date(api_get_utc_datetime(), DATE_FORMAT_LONG_NO_DAY);
  1776. if (!empty($date_certificate)) {
  1777. $date_long_certificate = api_convert_and_format_date($date_certificate);
  1778. $date_no_time = api_convert_and_format_date($date_certificate, DATE_FORMAT_LONG_NO_DAY);
  1779. }
  1780. if ($is_preview) {
  1781. $date_long_certificate = api_convert_and_format_date(api_get_utc_datetime());
  1782. $date_no_time = api_convert_and_format_date(api_get_utc_datetime(), DATE_FORMAT_LONG_NO_DAY);
  1783. }
  1784. $url = api_get_path(WEB_PATH).'certificates/index.php?id='.$info_grade_certificate['id'];
  1785. $externalStyleFile = api_get_path(SYS_CSS_PATH).'themes/'.api_get_visual_theme().'/certificate.css';
  1786. $externalStyle = '';
  1787. if (is_file($externalStyleFile)) {
  1788. $externalStyle = file_get_contents($externalStyleFile);
  1789. }
  1790. // Replace content
  1791. $info_to_replace_in_content_html = [
  1792. $first_name,
  1793. $last_name,
  1794. $username,
  1795. $organization_name,
  1796. $portal_name,
  1797. $teacher_first_name,
  1798. $teacher_last_name,
  1799. $official_code,
  1800. $date_long_certificate,
  1801. $date_no_time,
  1802. $course_id,
  1803. $course_info['name'],
  1804. $info_grade_certificate['grade'],
  1805. $url,
  1806. '<a href="'.$url.'" target="_blank">'.get_lang('CertificateOnlineLink').'</a>',
  1807. '((certificate_barcode))',
  1808. $externalStyle,
  1809. ];
  1810. $tags = [
  1811. '((user_firstname))',
  1812. '((user_lastname))',
  1813. '((user_username))',
  1814. '((gradebook_institution))',
  1815. '((gradebook_sitename))',
  1816. '((teacher_firstname))',
  1817. '((teacher_lastname))',
  1818. '((official_code))',
  1819. '((date_certificate))',
  1820. '((date_certificate_no_time))',
  1821. '((course_code))',
  1822. '((course_title))',
  1823. '((gradebook_grade))',
  1824. '((certificate_link))',
  1825. '((certificate_link_html))',
  1826. '((certificate_barcode))',
  1827. '((external_style))',
  1828. ];
  1829. if (!empty($extraFields)) {
  1830. foreach ($extraFields as $extraField) {
  1831. $valueExtra = isset($extra_user_info_data[$extraField['variable']]) ? $extra_user_info_data[$extraField['variable']] : '';
  1832. $tags[] = '(('.strtolower($extraField['variable']).'))';
  1833. $info_to_replace_in_content_html[] = $valueExtra;
  1834. }
  1835. }
  1836. $info_list[] = $tags;
  1837. $info_list[] = $info_to_replace_in_content_html;
  1838. return $info_list;
  1839. }
  1840. /**
  1841. * Remove default certificate.
  1842. *
  1843. * @param string $course_id The course code
  1844. * @param int $default_certificate_id The document id of the default certificate
  1845. */
  1846. public static function remove_attach_certificate($course_id, $default_certificate_id)
  1847. {
  1848. if (empty($default_certificate_id)) {
  1849. return false;
  1850. }
  1851. $default_certificate = self::get_default_certificate_id($course_id);
  1852. if ((int) $default_certificate == (int) $default_certificate_id) {
  1853. $tbl_category = Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY);
  1854. $session_id = api_get_session_id();
  1855. if ($session_id == 0 || is_null($session_id)) {
  1856. $sql_session = 'AND (session_id='.intval($session_id).' OR isnull(session_id)) ';
  1857. } elseif ($session_id > 0) {
  1858. $sql_session = 'AND session_id='.intval($session_id);
  1859. } else {
  1860. $sql_session = '';
  1861. }
  1862. $sql = 'UPDATE '.$tbl_category.' SET document_id = null
  1863. WHERE
  1864. course_code = "'.Database::escape_string($course_id).'" AND
  1865. document_id="'.$default_certificate_id.'" '.$sql_session;
  1866. Database::query($sql);
  1867. }
  1868. }
  1869. /**
  1870. * Create directory certificate.
  1871. *
  1872. * @param array $courseInfo
  1873. */
  1874. public static function create_directory_certificate_in_course($courseInfo)
  1875. {
  1876. if (!empty($courseInfo)) {
  1877. $to_group_id = 0;
  1878. $to_user_id = null;
  1879. $course_dir = $courseInfo['path']."/document/";
  1880. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1881. $base_work_dir = $sys_course_path.$course_dir;
  1882. $dir_name = '/certificates';
  1883. $post_dir_name = get_lang('CertificatesFiles');
  1884. $visibility_command = 'invisible';
  1885. $id = self::get_document_id_of_directory_certificate();
  1886. if (empty($id)) {
  1887. create_unexisting_directory(
  1888. $courseInfo,
  1889. api_get_user_id(),
  1890. api_get_session_id(),
  1891. $to_group_id,
  1892. $to_user_id,
  1893. $base_work_dir,
  1894. $dir_name,
  1895. $post_dir_name,
  1896. null,
  1897. false,
  1898. false
  1899. );
  1900. $id = self::get_document_id_of_directory_certificate();
  1901. if (empty($id)) {
  1902. $id = add_document(
  1903. $courseInfo,
  1904. $dir_name,
  1905. 'folder',
  1906. 0,
  1907. $post_dir_name,
  1908. null,
  1909. 0,
  1910. true,
  1911. $to_group_id,
  1912. 0,
  1913. 0,
  1914. false
  1915. );
  1916. }
  1917. if (!empty($id)) {
  1918. api_item_property_update(
  1919. $courseInfo,
  1920. TOOL_DOCUMENT,
  1921. $id,
  1922. $visibility_command,
  1923. api_get_user_id()
  1924. );
  1925. }
  1926. }
  1927. }
  1928. }
  1929. /**
  1930. * Get the document id of the directory certificate.
  1931. *
  1932. * @return int The document id of the directory certificate
  1933. *
  1934. * @todo move to certificate.lib.php
  1935. */
  1936. public static function get_document_id_of_directory_certificate()
  1937. {
  1938. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  1939. $course_id = api_get_course_int_id();
  1940. $sql = "SELECT id FROM $tbl_document
  1941. WHERE c_id = $course_id AND path='/certificates' ";
  1942. $rs = Database::query($sql);
  1943. $row = Database::fetch_array($rs);
  1944. return $row['id'];
  1945. }
  1946. /**
  1947. * Check if a directory given is for certificate.
  1948. *
  1949. * @todo move to certificate.lib.php
  1950. *
  1951. * @param string $dir path of directory
  1952. *
  1953. * @return bool true if is a certificate or false otherwise
  1954. */
  1955. public static function is_certificate_mode($dir)
  1956. {
  1957. // I'm in the certification module?
  1958. $is_certificate_mode = false;
  1959. $is_certificate_array = explode('/', $dir);
  1960. array_shift($is_certificate_array);
  1961. if (isset($is_certificate_array[0]) && $is_certificate_array[0] == 'certificates') {
  1962. $is_certificate_mode = true;
  1963. }
  1964. return $is_certificate_mode || (isset($_GET['certificate']) && $_GET['certificate'] === 'true');
  1965. }
  1966. /**
  1967. * Gets the list of included resources as a list of absolute or relative paths from a html file or string html
  1968. * This allows for a better SCORM export or replace urls inside content html from copy course
  1969. * The list will generally include pictures, flash objects, java applets, or any other
  1970. * stuff included in the source of the current item. The current item is expected
  1971. * to be an HTML file or string html. If it is not, then the function will return and empty list.
  1972. *
  1973. * @param string source html (content or path)
  1974. * @param bool is file or string html
  1975. * @param string type (one of the app tools) - optional (otherwise takes the current item's type)
  1976. * @param int level of recursivity we're in
  1977. *
  1978. * @return array List of file paths. An additional field containing 'local' or 'remote' helps determine
  1979. * if the file should be copied into the zip or just linked
  1980. */
  1981. public static function get_resources_from_source_html(
  1982. $source_html,
  1983. $is_file = false,
  1984. $type = null,
  1985. $recursivity = 1
  1986. ) {
  1987. $max = 5;
  1988. $attributes = [];
  1989. $wanted_attributes = [
  1990. 'src',
  1991. 'url',
  1992. '@import',
  1993. 'href',
  1994. 'value',
  1995. 'flashvars',
  1996. 'poster',
  1997. ];
  1998. $explode_attributes = ['flashvars' => 'file'];
  1999. $abs_path = '';
  2000. if ($recursivity > $max) {
  2001. return [];
  2002. }
  2003. if (!isset($type)) {
  2004. $type = TOOL_DOCUMENT;
  2005. }
  2006. if (!$is_file) {
  2007. $attributes = self::parse_HTML_attributes(
  2008. $source_html,
  2009. $wanted_attributes,
  2010. $explode_attributes
  2011. );
  2012. } else {
  2013. if (is_file($source_html)) {
  2014. $abs_path = $source_html;
  2015. //for now, read the whole file in one go (that's gonna be a problem when the file is too big)
  2016. $info = pathinfo($abs_path);
  2017. $ext = $info['extension'];
  2018. switch (strtolower($ext)) {
  2019. case 'html':
  2020. case 'htm':
  2021. case 'shtml':
  2022. case 'css':
  2023. $file_content = file_get_contents($abs_path);
  2024. // get an array of attributes from the HTML source
  2025. $attributes = self::parse_HTML_attributes(
  2026. $file_content,
  2027. $wanted_attributes,
  2028. $explode_attributes
  2029. );
  2030. break;
  2031. default:
  2032. break;
  2033. }
  2034. } else {
  2035. return false;
  2036. }
  2037. }
  2038. $files_list = [];
  2039. switch ($type) {
  2040. case TOOL_DOCUMENT:
  2041. case TOOL_QUIZ:
  2042. case 'sco':
  2043. foreach ($wanted_attributes as $attr) {
  2044. if (isset($attributes[$attr])) {
  2045. //find which kind of path these are (local or remote)
  2046. $sources = $attributes[$attr];
  2047. foreach ($sources as $source) {
  2048. //skip what is obviously not a resource
  2049. if (strpos($source, '+this.')) {
  2050. continue; //javascript code - will still work unaltered
  2051. }
  2052. if (strpos($source, '.') === false) {
  2053. continue; //no dot, should not be an external file anyway
  2054. }
  2055. if (strpos($source, 'mailto:')) {
  2056. continue; //mailto link
  2057. }
  2058. if (strpos($source, ';') && !strpos($source, '&amp;')) {
  2059. continue; //avoid code - that should help
  2060. }
  2061. if ($attr == 'value') {
  2062. if (strpos($source, 'mp3file')) {
  2063. $files_list[] = [
  2064. substr($source, 0, strpos($source, '.swf') + 4),
  2065. 'local',
  2066. 'abs',
  2067. ];
  2068. $mp3file = substr($source, strpos($source, 'mp3file=') + 8);
  2069. if (substr($mp3file, 0, 1) == '/') {
  2070. $files_list[] = [$mp3file, 'local', 'abs'];
  2071. } else {
  2072. $files_list[] = [$mp3file, 'local', 'rel'];
  2073. }
  2074. } elseif (strpos($source, 'flv=') === 0) {
  2075. $source = substr($source, 4);
  2076. if (strpos($source, '&') > 0) {
  2077. $source = substr($source, 0, strpos($source, '&'));
  2078. }
  2079. if (strpos($source, '://') > 0) {
  2080. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  2081. //we found the current portal url
  2082. $files_list[] = [$source, 'local', 'url'];
  2083. } else {
  2084. //we didn't find any trace of current portal
  2085. $files_list[] = [$source, 'remote', 'url'];
  2086. }
  2087. } else {
  2088. $files_list[] = [$source, 'local', 'abs'];
  2089. }
  2090. /* skipping anything else to avoid two entries
  2091. (while the others can have sub-files in their url, flv's can't)*/
  2092. continue;
  2093. }
  2094. }
  2095. if (strpos($source, '://') > 0) {
  2096. //cut at '?' in a URL with params
  2097. if (strpos($source, '?') > 0) {
  2098. $second_part = substr($source, strpos($source, '?'));
  2099. if (strpos($second_part, '://') > 0) {
  2100. //if the second part of the url contains a url too, treat the second one before cutting
  2101. $pos1 = strpos($second_part, '=');
  2102. $pos2 = strpos($second_part, '&');
  2103. $second_part = substr($second_part, $pos1 + 1, $pos2 - ($pos1 + 1));
  2104. if (strpos($second_part, api_get_path(WEB_PATH)) !== false) {
  2105. //we found the current portal url
  2106. $files_list[] = [$second_part, 'local', 'url'];
  2107. $in_files_list[] = self::get_resources_from_source_html(
  2108. $second_part,
  2109. true,
  2110. TOOL_DOCUMENT,
  2111. $recursivity + 1
  2112. );
  2113. if (count($in_files_list) > 0) {
  2114. $files_list = array_merge($files_list, $in_files_list);
  2115. }
  2116. } else {
  2117. //we didn't find any trace of current portal
  2118. $files_list[] = [$second_part, 'remote', 'url'];
  2119. }
  2120. } elseif (strpos($second_part, '=') > 0) {
  2121. if (substr($second_part, 0, 1) === '/') {
  2122. //link starts with a /, making it absolute (relative to DocumentRoot)
  2123. $files_list[] = [$second_part, 'local', 'abs'];
  2124. $in_files_list[] = self::get_resources_from_source_html(
  2125. $second_part,
  2126. true,
  2127. TOOL_DOCUMENT,
  2128. $recursivity + 1
  2129. );
  2130. if (count($in_files_list) > 0) {
  2131. $files_list = array_merge($files_list, $in_files_list);
  2132. }
  2133. } elseif (strstr($second_part, '..') === 0) {
  2134. //link is relative but going back in the hierarchy
  2135. $files_list[] = [$second_part, 'local', 'rel'];
  2136. //$dir = api_get_path(SYS_CODE_PATH);//dirname($abs_path);
  2137. //$new_abs_path = realpath($dir.'/'.$second_part);
  2138. $dir = '';
  2139. if (!empty($abs_path)) {
  2140. $dir = dirname($abs_path).'/';
  2141. }
  2142. $new_abs_path = realpath($dir.$second_part);
  2143. $in_files_list[] = self::get_resources_from_source_html(
  2144. $new_abs_path,
  2145. true,
  2146. TOOL_DOCUMENT,
  2147. $recursivity + 1
  2148. );
  2149. if (count($in_files_list) > 0) {
  2150. $files_list = array_merge($files_list, $in_files_list);
  2151. }
  2152. } else {
  2153. //no starting '/', making it relative to current document's path
  2154. if (substr($second_part, 0, 2) == './') {
  2155. $second_part = substr($second_part, 2);
  2156. }
  2157. $files_list[] = [$second_part, 'local', 'rel'];
  2158. $dir = '';
  2159. if (!empty($abs_path)) {
  2160. $dir = dirname($abs_path).'/';
  2161. }
  2162. $new_abs_path = realpath($dir.$second_part);
  2163. $in_files_list[] = self::get_resources_from_source_html(
  2164. $new_abs_path,
  2165. true,
  2166. TOOL_DOCUMENT,
  2167. $recursivity + 1
  2168. );
  2169. if (count($in_files_list) > 0) {
  2170. $files_list = array_merge($files_list, $in_files_list);
  2171. }
  2172. }
  2173. }
  2174. //leave that second part behind now
  2175. $source = substr($source, 0, strpos($source, '?'));
  2176. if (strpos($source, '://') > 0) {
  2177. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  2178. //we found the current portal url
  2179. $files_list[] = [$source, 'local', 'url'];
  2180. $in_files_list[] = self::get_resources_from_source_html(
  2181. $source,
  2182. true,
  2183. TOOL_DOCUMENT,
  2184. $recursivity + 1
  2185. );
  2186. if (count($in_files_list) > 0) {
  2187. $files_list = array_merge($files_list, $in_files_list);
  2188. }
  2189. } else {
  2190. //we didn't find any trace of current portal
  2191. $files_list[] = [$source, 'remote', 'url'];
  2192. }
  2193. } else {
  2194. //no protocol found, make link local
  2195. if (substr($source, 0, 1) === '/') {
  2196. //link starts with a /, making it absolute (relative to DocumentRoot)
  2197. $files_list[] = [$source, 'local', 'abs'];
  2198. $in_files_list[] = self::get_resources_from_source_html(
  2199. $source,
  2200. true,
  2201. TOOL_DOCUMENT,
  2202. $recursivity + 1
  2203. );
  2204. if (count($in_files_list) > 0) {
  2205. $files_list = array_merge($files_list, $in_files_list);
  2206. }
  2207. } elseif (strstr($source, '..') === 0) {
  2208. //link is relative but going back in the hierarchy
  2209. $files_list[] = [$source, 'local', 'rel'];
  2210. $dir = '';
  2211. if (!empty($abs_path)) {
  2212. $dir = dirname($abs_path).'/';
  2213. }
  2214. $new_abs_path = realpath($dir.$source);
  2215. $in_files_list[] = self::get_resources_from_source_html(
  2216. $new_abs_path,
  2217. true,
  2218. TOOL_DOCUMENT,
  2219. $recursivity + 1
  2220. );
  2221. if (count($in_files_list) > 0) {
  2222. $files_list = array_merge($files_list, $in_files_list);
  2223. }
  2224. } else {
  2225. //no starting '/', making it relative to current document's path
  2226. if (substr($source, 0, 2) == './') {
  2227. $source = substr($source, 2);
  2228. }
  2229. $files_list[] = [$source, 'local', 'rel'];
  2230. $dir = '';
  2231. if (!empty($abs_path)) {
  2232. $dir = dirname($abs_path).'/';
  2233. }
  2234. $new_abs_path = realpath($dir.$source);
  2235. $in_files_list[] = self::get_resources_from_source_html(
  2236. $new_abs_path,
  2237. true,
  2238. TOOL_DOCUMENT,
  2239. $recursivity + 1
  2240. );
  2241. if (count($in_files_list) > 0) {
  2242. $files_list = array_merge($files_list, $in_files_list);
  2243. }
  2244. }
  2245. }
  2246. }
  2247. //found some protocol there
  2248. if (strpos($source, api_get_path(WEB_PATH)) !== false) {
  2249. //we found the current portal url
  2250. $files_list[] = [$source, 'local', 'url'];
  2251. $in_files_list[] = self::get_resources_from_source_html(
  2252. $source,
  2253. true,
  2254. TOOL_DOCUMENT,
  2255. $recursivity + 1
  2256. );
  2257. if (count($in_files_list) > 0) {
  2258. $files_list = array_merge($files_list, $in_files_list);
  2259. }
  2260. } else {
  2261. //we didn't find any trace of current portal
  2262. $files_list[] = [$source, 'remote', 'url'];
  2263. }
  2264. } else {
  2265. //no protocol found, make link local
  2266. if (substr($source, 0, 1) === '/') {
  2267. //link starts with a /, making it absolute (relative to DocumentRoot)
  2268. $files_list[] = [$source, 'local', 'abs'];
  2269. $in_files_list[] = self::get_resources_from_source_html(
  2270. $source,
  2271. true,
  2272. TOOL_DOCUMENT,
  2273. $recursivity + 1
  2274. );
  2275. if (count($in_files_list) > 0) {
  2276. $files_list = array_merge($files_list, $in_files_list);
  2277. }
  2278. } elseif (strpos($source, '..') === 0) {
  2279. //link is relative but going back in the hierarchy
  2280. $files_list[] = [$source, 'local', 'rel'];
  2281. $dir = '';
  2282. if (!empty($abs_path)) {
  2283. $dir = dirname($abs_path).'/';
  2284. }
  2285. $new_abs_path = realpath($dir.$source);
  2286. $in_files_list[] = self::get_resources_from_source_html(
  2287. $new_abs_path,
  2288. true,
  2289. TOOL_DOCUMENT,
  2290. $recursivity + 1
  2291. );
  2292. if (count($in_files_list) > 0) {
  2293. $files_list = array_merge($files_list, $in_files_list);
  2294. }
  2295. } else {
  2296. //no starting '/', making it relative to current document's path
  2297. if (substr($source, 0, 2) == './') {
  2298. $source = substr($source, 2);
  2299. }
  2300. $files_list[] = [$source, 'local', 'rel'];
  2301. $dir = '';
  2302. if (!empty($abs_path)) {
  2303. $dir = dirname($abs_path).'/';
  2304. }
  2305. $new_abs_path = realpath($dir.$source);
  2306. $in_files_list[] = self::get_resources_from_source_html(
  2307. $new_abs_path,
  2308. true,
  2309. TOOL_DOCUMENT,
  2310. $recursivity + 1
  2311. );
  2312. if (count($in_files_list) > 0) {
  2313. $files_list = array_merge($files_list, $in_files_list);
  2314. }
  2315. }
  2316. }
  2317. }
  2318. }
  2319. }
  2320. break;
  2321. default: //ignore
  2322. break;
  2323. }
  2324. $checked_files_list = [];
  2325. $checked_array_list = [];
  2326. if (count($files_list) > 0) {
  2327. foreach ($files_list as $idx => $file) {
  2328. if (!empty($file[0])) {
  2329. if (!in_array($file[0], $checked_files_list)) {
  2330. $checked_files_list[] = $files_list[$idx][0];
  2331. $checked_array_list[] = $files_list[$idx];
  2332. }
  2333. }
  2334. }
  2335. }
  2336. return $checked_array_list;
  2337. }
  2338. /**
  2339. * Parses the HTML attributes given as string.
  2340. *
  2341. * @param string HTML attribute string
  2342. * @param array List of attributes that we want to get back
  2343. * @param array
  2344. *
  2345. * @return array An associative array of attributes
  2346. *
  2347. * @author Based on a function from the HTML_Common2 PEAR module *
  2348. */
  2349. public static function parse_HTML_attributes($attrString, $wanted = [], $explode_variables = [])
  2350. {
  2351. $attributes = [];
  2352. $regs = [];
  2353. $reduced = false;
  2354. if (count($wanted) > 0) {
  2355. $reduced = true;
  2356. }
  2357. try {
  2358. //Find all occurences of something that looks like a URL
  2359. // The structure of this regexp is:
  2360. // (find protocol) then
  2361. // (optionally find some kind of space 1 or more times) then
  2362. // find (either an equal sign or a bracket) followed by an optional space
  2363. // followed by some text without quotes (between quotes itself or not)
  2364. // then possible closing brackets if we were in the opening bracket case
  2365. // OR something like @import()
  2366. $res = preg_match_all(
  2367. '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]*))'.
  2368. // '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]|[^\x00-\x7F])*)' . -> seems to be taking too much
  2369. // '/(((([A-Za-z_:])([^\x00-\x7F])*)' . -> takes only last letter of parameter name
  2370. '([ \n\t\r]+)?('.
  2371. // '(=([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+))' . -> doesn't restrict close enough to the url itself
  2372. '(=([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+))'.
  2373. '|'.
  2374. // '(\(([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)\))' . -> doesn't restrict close enough to the url itself
  2375. '(\(([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+)\))'.
  2376. '))'.
  2377. '|'.
  2378. // '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))?/', -> takes a lot (like 100's of thousands of empty possibilities)
  2379. '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/',
  2380. $attrString,
  2381. $regs
  2382. );
  2383. } catch (Exception $e) {
  2384. error_log('Caught exception: '.$e->getMessage(), 0);
  2385. }
  2386. if ($res) {
  2387. for ($i = 0; $i < count($regs[1]); $i++) {
  2388. $name = trim($regs[3][$i]);
  2389. $check = trim($regs[0][$i]);
  2390. $value = trim($regs[10][$i]);
  2391. if (empty($value) and !empty($regs[13][$i])) {
  2392. $value = $regs[13][$i];
  2393. }
  2394. if (empty($name) && !empty($regs[16][$i])) {
  2395. $name = '@import';
  2396. $value = trim($regs[16][$i]);
  2397. }
  2398. if (!empty($name)) {
  2399. if (!$reduced || in_array(strtolower($name), $wanted)) {
  2400. if ($name == $check) {
  2401. $attributes[strtolower($name)][] = strtolower($name);
  2402. } else {
  2403. if (!empty($value) && ($value[0] == '\'' || $value[0] == '"')) {
  2404. $value = substr($value, 1, -1);
  2405. }
  2406. if ($value == 'API.LMSGetValue(name') {
  2407. $value = 'API.LMSGetValue(name)';
  2408. }
  2409. //Gets the xx.flv value from the string flashvars="width=320&height=240&autostart=false&file=xxx.flv&repeat=false"
  2410. if (isset($explode_variables[$name])) {
  2411. $value_modified = str_replace('&amp;', '&', $value);
  2412. $value_array = explode('&', $value_modified);
  2413. foreach ($value_array as $item) {
  2414. $itemParts = explode('=', $item);
  2415. $key = $itemParts[0];
  2416. $item_value = !empty($itemParts[1]) ? $itemParts[1] : '';
  2417. if ($key == $explode_variables[$name]) {
  2418. $attributes[strtolower($name)][] = $item_value;
  2419. }
  2420. }
  2421. }
  2422. $attributes[strtolower($name)][] = $value;
  2423. }
  2424. }
  2425. }
  2426. }
  2427. }
  2428. return $attributes;
  2429. }
  2430. /**
  2431. * Replace urls inside content html from a copy course.
  2432. *
  2433. * @param string $content_html
  2434. * @param string $origin_course_code
  2435. * @param string $destination_course_directory
  2436. * @param string $origin_course_path_from_zip
  2437. * @param string $origin_course_info_path
  2438. *
  2439. * @return string new content html with replaced urls or return false if content is not a string
  2440. */
  2441. public static function replaceUrlWithNewCourseCode(
  2442. $content_html,
  2443. $origin_course_code,
  2444. $destination_course_directory,
  2445. $origin_course_path_from_zip = null,
  2446. $origin_course_info_path = null
  2447. ) {
  2448. if (empty($content_html)) {
  2449. return false;
  2450. }
  2451. $orig_source_html = self::get_resources_from_source_html($content_html);
  2452. $orig_course_info = api_get_course_info($origin_course_code);
  2453. // Course does not exist in the current DB probably this came from a zip file?
  2454. if (empty($orig_course_info)) {
  2455. if (!empty($origin_course_path_from_zip)) {
  2456. $orig_course_path = $origin_course_path_from_zip.'/';
  2457. $orig_course_info_path = $origin_course_info_path;
  2458. }
  2459. } else {
  2460. $orig_course_path = api_get_path(SYS_COURSE_PATH).$orig_course_info['path'].'/';
  2461. $orig_course_info_path = $orig_course_info['path'];
  2462. }
  2463. $destination_course_code = CourseManager::getCourseCodeFromDirectory($destination_course_directory);
  2464. $destination_course_info = api_get_course_info($destination_course_code);
  2465. $dest_course_path = api_get_path(SYS_COURSE_PATH).$destination_course_directory.'/';
  2466. $dest_course_path_rel = api_get_path(REL_COURSE_PATH).$destination_course_directory.'/';
  2467. $user_id = api_get_user_id();
  2468. if (!empty($orig_source_html)) {
  2469. foreach ($orig_source_html as $source) {
  2470. // Get information about source url
  2471. $real_orig_url = $source[0]; // url
  2472. $scope_url = $source[1]; // scope (local, remote)
  2473. $type_url = $source[2]; // type (rel, abs, url)
  2474. // Get path and query from origin url
  2475. $orig_parse_url = parse_url($real_orig_url);
  2476. $real_orig_path = isset($orig_parse_url['path']) ? $orig_parse_url['path'] : null;
  2477. $real_orig_query = isset($orig_parse_url['query']) ? $orig_parse_url['query'] : null;
  2478. // Replace origin course code by destination course code from origin url query
  2479. $dest_url_query = '';
  2480. if (!empty($real_orig_query)) {
  2481. $dest_url_query = '?'.$real_orig_query;
  2482. if (strpos($dest_url_query, $origin_course_code) !== false) {
  2483. $dest_url_query = str_replace($origin_course_code, $destination_course_code, $dest_url_query);
  2484. }
  2485. }
  2486. if ($scope_url == 'local') {
  2487. if ($type_url == 'abs' || $type_url == 'rel') {
  2488. $document_file = strstr($real_orig_path, 'document');
  2489. if (strpos($real_orig_path, $document_file) !== false) {
  2490. $origin_filepath = $orig_course_path.$document_file;
  2491. $destination_filepath = $dest_course_path.$document_file;
  2492. // copy origin file inside destination course
  2493. if (file_exists($origin_filepath)) {
  2494. $filepath_dir = dirname($destination_filepath);
  2495. if (!is_dir($filepath_dir)) {
  2496. $perm = api_get_permissions_for_new_directories();
  2497. $result = @mkdir($filepath_dir, $perm, true);
  2498. if ($result) {
  2499. $filepath_to_add = str_replace(
  2500. [$dest_course_path, 'document'],
  2501. '',
  2502. $filepath_dir
  2503. );
  2504. //Add to item properties to the new folder
  2505. $doc_id = add_document(
  2506. $destination_course_info,
  2507. $filepath_to_add,
  2508. 'folder',
  2509. 0,
  2510. basename($filepath_to_add)
  2511. );
  2512. api_item_property_update(
  2513. $destination_course_info,
  2514. TOOL_DOCUMENT,
  2515. $doc_id,
  2516. 'FolderCreated',
  2517. $user_id,
  2518. null,
  2519. null,
  2520. null,
  2521. null
  2522. );
  2523. }
  2524. }
  2525. if (!file_exists($destination_filepath)) {
  2526. $result = @copy($origin_filepath, $destination_filepath);
  2527. if ($result) {
  2528. $filepath_to_add = str_replace(
  2529. [$dest_course_path, 'document'],
  2530. '',
  2531. $destination_filepath
  2532. );
  2533. $size = filesize($destination_filepath);
  2534. // Add to item properties to the file
  2535. $doc_id = add_document(
  2536. $destination_course_info,
  2537. $filepath_to_add,
  2538. 'file',
  2539. $size,
  2540. basename($filepath_to_add)
  2541. );
  2542. api_item_property_update(
  2543. $destination_course_info,
  2544. TOOL_DOCUMENT,
  2545. $doc_id,
  2546. 'FolderCreated',
  2547. $user_id,
  2548. null,
  2549. null,
  2550. null,
  2551. null
  2552. );
  2553. }
  2554. }
  2555. }
  2556. // Replace origin course path by destination course path.
  2557. if (strpos($content_html, $real_orig_url) !== false) {
  2558. $url_course_path = str_replace(
  2559. $orig_course_info_path.'/'.$document_file,
  2560. '',
  2561. $real_orig_path
  2562. );
  2563. // See BT#7780
  2564. $destination_url = $dest_course_path_rel.$document_file.$dest_url_query;
  2565. // If the course code doesn't exist in the path? what we do? Nothing! see BT#1985
  2566. if (strpos($real_orig_path, $origin_course_code) === false) {
  2567. $url_course_path = $real_orig_path;
  2568. $destination_url = $real_orig_path;
  2569. }
  2570. $content_html = str_replace($real_orig_url, $destination_url, $content_html);
  2571. }
  2572. }
  2573. // replace origin course code by destination course code from origin url
  2574. if (strpos($real_orig_url, '?') === 0) {
  2575. $dest_url = str_replace($origin_course_code, $destination_course_code, $real_orig_url);
  2576. $content_html = str_replace($real_orig_url, $dest_url, $content_html);
  2577. }
  2578. }
  2579. }
  2580. }
  2581. }
  2582. return $content_html;
  2583. }
  2584. /**
  2585. * Export document to PDF.
  2586. *
  2587. * @param int $document_id
  2588. * @param string $courseCode
  2589. * @param string $orientation
  2590. * @param bool $showHeaderAndFooter
  2591. */
  2592. public static function export_to_pdf(
  2593. $document_id,
  2594. $courseCode,
  2595. $orientation = 'landscape',
  2596. $showHeaderAndFooter = true
  2597. ) {
  2598. $course_data = api_get_course_info($courseCode);
  2599. $document_data = self::get_document_data_by_id($document_id, $courseCode);
  2600. $file_path = api_get_path(SYS_COURSE_PATH).$course_data['path'].'/document'.$document_data['path'];
  2601. if ($orientation == 'landscape') {
  2602. $pageFormat = 'A4-L';
  2603. $pdfOrientation = 'L';
  2604. } else {
  2605. $pageFormat = 'A4';
  2606. $pdfOrientation = 'P';
  2607. }
  2608. $pdf = new PDF(
  2609. $pageFormat,
  2610. $pdfOrientation,
  2611. $showHeaderAndFooter ? [] : ['top' => 0, 'left' => 0, 'bottom' => 0, 'right' => 0]
  2612. );
  2613. if (api_get_configuration_value('use_alternative_document_pdf_footer')) {
  2614. $view = new Template('', false, false, false, true, false, false);
  2615. $template = $view->get_template('export/alt_pdf_footer.tpl');
  2616. $pdf->set_custom_footer([
  2617. 'html' => $view->fetch($template),
  2618. ]);
  2619. }
  2620. $pdf->html_to_pdf(
  2621. $file_path,
  2622. $document_data['title'],
  2623. $courseCode,
  2624. false,
  2625. $showHeaderAndFooter
  2626. );
  2627. }
  2628. /**
  2629. * Uploads a document.
  2630. *
  2631. * @param array $files the $_FILES variable
  2632. * @param string $path
  2633. * @param string $title
  2634. * @param string $comment
  2635. * @param int $unzip unzip or not the file
  2636. * @param string $ifExists overwrite, rename or warn (default)
  2637. * @param bool $index_document index document (search xapian module)
  2638. * @param bool $show_output print html messages
  2639. * @param string $fileKey
  2640. * @param bool $treat_spaces_as_hyphens
  2641. *
  2642. * @return array|bool
  2643. */
  2644. public static function upload_document(
  2645. $files,
  2646. $path,
  2647. $title = '',
  2648. $comment = '',
  2649. $unzip = 0,
  2650. $ifExists = '',
  2651. $index_document = false,
  2652. $show_output = false,
  2653. $fileKey = 'file',
  2654. $treat_spaces_as_hyphens = true
  2655. ) {
  2656. $course_info = api_get_course_info();
  2657. $sessionId = api_get_session_id();
  2658. $course_dir = $course_info['path'].'/document';
  2659. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  2660. $base_work_dir = $sys_course_path.$course_dir;
  2661. if (isset($files[$fileKey])) {
  2662. $uploadOk = process_uploaded_file($files[$fileKey], $show_output);
  2663. if ($uploadOk) {
  2664. $new_path = handle_uploaded_document(
  2665. $course_info,
  2666. $files[$fileKey],
  2667. $base_work_dir,
  2668. $path,
  2669. api_get_user_id(),
  2670. api_get_group_id(),
  2671. null,
  2672. $unzip,
  2673. $ifExists,
  2674. $show_output,
  2675. false,
  2676. null,
  2677. $sessionId,
  2678. $treat_spaces_as_hyphens
  2679. );
  2680. // When sending zip files
  2681. if ($new_path === true && $unzip == 1) {
  2682. return [
  2683. 'title' => $files[$fileKey]['name'],
  2684. 'url' => '#',
  2685. ];
  2686. }
  2687. if ($new_path) {
  2688. $documentId = self::get_document_id(
  2689. $course_info,
  2690. $new_path,
  2691. $sessionId
  2692. );
  2693. if (!empty($documentId)) {
  2694. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  2695. $params = [];
  2696. if (!empty($title)) {
  2697. $params['title'] = $title;
  2698. }
  2699. if (!empty($comment)) {
  2700. $params['comment'] = trim($comment);
  2701. }
  2702. Database::update(
  2703. $table_document,
  2704. $params,
  2705. [
  2706. 'id = ? AND c_id = ? ' => [
  2707. $documentId,
  2708. $course_info['real_id'],
  2709. ],
  2710. ]
  2711. );
  2712. }
  2713. if ($index_document) {
  2714. self::index_document(
  2715. $documentId,
  2716. $course_info['code'],
  2717. null,
  2718. $_POST['language'],
  2719. $_REQUEST,
  2720. $ifExists
  2721. );
  2722. }
  2723. if (!empty($documentId) && is_numeric($documentId)) {
  2724. $documentData = self::get_document_data_by_id(
  2725. $documentId,
  2726. $course_info['code'],
  2727. false,
  2728. $sessionId
  2729. );
  2730. return $documentData;
  2731. }
  2732. }
  2733. }
  2734. }
  2735. return false;
  2736. }
  2737. /**
  2738. * Obtains the text inside the file with the right parser.
  2739. */
  2740. public static function get_text_content($doc_path, $doc_mime)
  2741. {
  2742. // TODO: review w$ compatibility
  2743. // Use usual exec output lines array to store stdout instead of a temp file
  2744. // because we need to store it at RAM anyway before index on ChamiloIndexer object
  2745. $ret_val = null;
  2746. switch ($doc_mime) {
  2747. case 'text/plain':
  2748. $handle = fopen($doc_path, 'r');
  2749. $output = [fread($handle, filesize($doc_path))];
  2750. fclose($handle);
  2751. break;
  2752. case 'application/pdf':
  2753. exec("pdftotext $doc_path -", $output, $ret_val);
  2754. break;
  2755. case 'application/postscript':
  2756. $temp_file = tempnam(sys_get_temp_dir(), 'chamilo');
  2757. exec("ps2pdf $doc_path $temp_file", $output, $ret_val);
  2758. if ($ret_val !== 0) { // shell fail, probably 127 (command not found)
  2759. return false;
  2760. }
  2761. exec("pdftotext $temp_file -", $output, $ret_val);
  2762. unlink($temp_file);
  2763. break;
  2764. case 'application/msword':
  2765. exec("catdoc $doc_path", $output, $ret_val);
  2766. break;
  2767. case 'text/html':
  2768. exec("html2text $doc_path", $output, $ret_val);
  2769. break;
  2770. case 'text/rtf':
  2771. // Note: correct handling of code pages in unrtf
  2772. // on debian lenny unrtf v0.19.2 can not, but unrtf v0.20.5 can
  2773. exec("unrtf --text $doc_path", $output, $ret_val);
  2774. if ($ret_val == 127) { // command not found
  2775. return false;
  2776. }
  2777. // Avoid index unrtf comments
  2778. if (is_array($output) && count($output) > 1) {
  2779. $parsed_output = [];
  2780. foreach ($output as &$line) {
  2781. if (!preg_match('/^###/', $line, $matches)) {
  2782. if (!empty($line)) {
  2783. $parsed_output[] = $line;
  2784. }
  2785. }
  2786. }
  2787. $output = $parsed_output;
  2788. }
  2789. break;
  2790. case 'application/vnd.ms-powerpoint':
  2791. exec("catppt $doc_path", $output, $ret_val);
  2792. break;
  2793. case 'application/vnd.ms-excel':
  2794. exec("xls2csv -c\" \" $doc_path", $output, $ret_val);
  2795. break;
  2796. }
  2797. $content = '';
  2798. if (!is_null($ret_val)) {
  2799. if ($ret_val !== 0) { // shell fail, probably 127 (command not found)
  2800. return false;
  2801. }
  2802. }
  2803. if (isset($output)) {
  2804. foreach ($output as &$line) {
  2805. $content .= $line."\n";
  2806. }
  2807. return $content;
  2808. } else {
  2809. return false;
  2810. }
  2811. }
  2812. /**
  2813. * Calculates the total size of all documents in a course.
  2814. *
  2815. * @author Bert vanderkimpen
  2816. *
  2817. * @param int $course_id
  2818. * @param int $group_id (to calculate group document space)
  2819. * @param int $session_id
  2820. *
  2821. * @return int total size
  2822. */
  2823. public static function documents_total_space($course_id = null, $group_id = null, $session_id = null)
  2824. {
  2825. $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2826. $TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
  2827. $session_id = (int) $session_id;
  2828. $group_id = (int) $group_id;
  2829. $course_id = (int) $course_id;
  2830. if (!$course_id) {
  2831. $course_id = api_get_course_int_id();
  2832. }
  2833. $group_condition = '';
  2834. if ($group_id) {
  2835. $group_condition = " AND props.to_group_id='".$group_id."' ";
  2836. }
  2837. $session_condition = '';
  2838. if ($session_id) {
  2839. $session_condition = " AND props.session_id='".$session_id."' ";
  2840. }
  2841. $sql = "SELECT SUM(size)
  2842. FROM $TABLE_ITEMPROPERTY AS props
  2843. INNER JOIN $TABLE_DOCUMENT AS docs
  2844. ON (docs.id = props.ref AND props.c_id = docs.c_id)
  2845. WHERE
  2846. props.c_id = $course_id AND
  2847. docs.c_id = $course_id AND
  2848. props.tool = '".TOOL_DOCUMENT."' AND
  2849. props.visibility <> 2
  2850. $group_condition
  2851. $session_condition
  2852. ";
  2853. $result = Database::query($sql);
  2854. if ($result && Database::num_rows($result) != 0) {
  2855. $row = Database::fetch_row($result);
  2856. return (int) $row[0];
  2857. } else {
  2858. return 0;
  2859. }
  2860. }
  2861. /**
  2862. * Display the document quota in a simple way.
  2863. *
  2864. * Here we count 1 Kilobyte = 1024 Bytes, 1 Megabyte = 1048576 Bytes
  2865. */
  2866. public static function displaySimpleQuota($course_quota, $already_consumed_space)
  2867. {
  2868. $course_quota_m = round($course_quota / 1048576);
  2869. $already_consumed_space_m = round($already_consumed_space / 1048576, 2);
  2870. $percentage = $already_consumed_space / $course_quota * 100;
  2871. $percentage = round($percentage, 1);
  2872. $message = get_lang('YouAreCurrentlyUsingXOfYourX');
  2873. $message = sprintf($message, $already_consumed_space_m, $percentage.'%', $course_quota_m.' ');
  2874. return Display::div($message, ['id' => 'document_quota']);
  2875. }
  2876. /**
  2877. * Checks if there is enough place to add a file on a directory
  2878. * on the base of a maximum directory size allowed.
  2879. *
  2880. * @author Bert Vanderkimpen
  2881. *
  2882. * @param int $file_size size of the file in byte
  2883. * @param int $max_dir_space maximum size
  2884. *
  2885. * @return bool true if there is enough space, false otherwise
  2886. *
  2887. * @see enough_space() uses documents_total_space() function
  2888. */
  2889. public static function enough_space($file_size, $max_dir_space)
  2890. {
  2891. if ($max_dir_space) {
  2892. $already_filled_space = self::documents_total_space();
  2893. if (($file_size + $already_filled_space) > $max_dir_space) {
  2894. return false;
  2895. }
  2896. }
  2897. return true;
  2898. }
  2899. /**
  2900. * @param array $params count, url, extension
  2901. *
  2902. * @return string
  2903. */
  2904. public static function generateAudioJavascript($params = [])
  2905. {
  2906. $js = '
  2907. $(\'audio.audio_preview\').mediaelementplayer({
  2908. features: [\'playpause\'],
  2909. audioWidth: 30,
  2910. audioHeight: 30,
  2911. success: function(mediaElement, originalNode, instance) {
  2912. }
  2913. });';
  2914. return $js;
  2915. }
  2916. /**
  2917. * Shows a play icon next to the document title in the document list.
  2918. *
  2919. * @param string $documentWebPath
  2920. * @param array $documentInfo
  2921. *
  2922. * @return string
  2923. */
  2924. public static function generateAudioPreview($documentWebPath, $documentInfo)
  2925. {
  2926. $filePath = $documentWebPath.$documentInfo['path'];
  2927. $extension = $documentInfo['file_extension'];
  2928. $html = '<span class="preview"> <audio class="audio_preview skip" src="'.$filePath.'" type="audio/'.$extension.'" > </audio></span>';
  2929. return $html;
  2930. }
  2931. /**
  2932. * @param string $file
  2933. * @param string $extension
  2934. *
  2935. * @return string
  2936. */
  2937. public static function generateMediaPreview($file, $extension)
  2938. {
  2939. $id = api_get_unique_id();
  2940. switch ($extension) {
  2941. case 'mp3':
  2942. $document_data['file_extension'] = $extension;
  2943. $html = '<div style="margin: 0; position: absolute; top: 50%; left: 35%;">';
  2944. $html .= '<audio id="'.$id.'" controls="controls" src="'.$file.'" type="audio/mp3" ></audio></div>';
  2945. break;
  2946. default:
  2947. $html = '<video id="'.$id.'" controls>';
  2948. $html .= '<source src="'.$file.'" >';
  2949. $html .= '</video>';
  2950. break;
  2951. }
  2952. return $html;
  2953. }
  2954. /**
  2955. * @param array $course_info
  2956. * @param bool $lp_id
  2957. * @param string $target
  2958. * @param int $session_id
  2959. * @param bool $add_move_button
  2960. * @param string $filter_by_folder
  2961. * @param string $overwrite_url
  2962. * @param bool $showInvisibleFiles
  2963. * @param bool $showOnlyFolders
  2964. * @param int $folderId
  2965. * @param bool $addCloseButton
  2966. *
  2967. * @return string
  2968. */
  2969. public static function get_document_preview(
  2970. $course_info,
  2971. $lp_id = false,
  2972. $target = '',
  2973. $session_id = 0,
  2974. $add_move_button = false,
  2975. $filter_by_folder = null,
  2976. $overwrite_url = '',
  2977. $showInvisibleFiles = false,
  2978. $showOnlyFolders = false,
  2979. $folderId = false,
  2980. $addCloseButton = true
  2981. ) {
  2982. if (empty($course_info['real_id']) || empty($course_info['code']) || !is_array($course_info)) {
  2983. return '';
  2984. }
  2985. $user_id = api_get_user_id();
  2986. $userInfo = api_get_user_info();
  2987. $user_in_course = api_is_platform_admin();
  2988. if (!$user_in_course) {
  2989. if (CourseManager::is_course_teacher($user_id, $course_info['code'])) {
  2990. $user_in_course = true;
  2991. }
  2992. }
  2993. // Condition for the session
  2994. $session_id = (int) $session_id;
  2995. if (!$user_in_course) {
  2996. if (empty($session_id)) {
  2997. if (CourseManager::is_user_subscribed_in_course($user_id, $course_info['code'])) {
  2998. $user_in_course = true;
  2999. }
  3000. // Check if course is open then we can consider that the student is registered to the course
  3001. if (isset($course_info) && in_array($course_info['visibility'], [2, 3])) {
  3002. $user_in_course = true;
  3003. }
  3004. } else {
  3005. $user_status = SessionManager::get_user_status_in_course_session(
  3006. $user_id,
  3007. $course_info['real_id'],
  3008. $session_id
  3009. );
  3010. //is true if is an student, course session teacher or coach
  3011. if (in_array($user_status, ['0', '2', '6'])) {
  3012. $user_in_course = true;
  3013. }
  3014. }
  3015. }
  3016. $tbl_doc = Database::get_course_table(TABLE_DOCUMENT);
  3017. $tbl_item_prop = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3018. $condition_session = " AND (last.session_id = '$session_id' OR last.session_id = '0' OR last.session_id IS NULL)";
  3019. $add_folder_filter = null;
  3020. if (!empty($filter_by_folder)) {
  3021. $add_folder_filter = " AND docs.path LIKE '".Database::escape_string($filter_by_folder)."%'";
  3022. }
  3023. // If we are in LP display hidden folder https://support.chamilo.org/issues/6679
  3024. $lp_visibility_condition = null;
  3025. if ($lp_id) {
  3026. if ($showInvisibleFiles) {
  3027. $lp_visibility_condition .= ' OR last.visibility = 0';
  3028. }
  3029. }
  3030. $folderCondition = " AND docs.path LIKE '/%' ";
  3031. if (!api_is_allowed_to_edit()) {
  3032. $protectedFolders = self::getProtectedFolderFromStudent();
  3033. foreach ($protectedFolders as $folder) {
  3034. $folderCondition .= " AND docs.path NOT LIKE '$folder' ";
  3035. }
  3036. }
  3037. $parentData = [];
  3038. if ($folderId !== false) {
  3039. $parentData = self::get_document_data_by_id(
  3040. $folderId,
  3041. $course_info['code'],
  3042. false,
  3043. $session_id
  3044. );
  3045. if (!empty($parentData)) {
  3046. $cleanedPath = $parentData['path'];
  3047. $num = substr_count($cleanedPath, '/');
  3048. $notLikeCondition = '';
  3049. for ($i = 1; $i <= $num; $i++) {
  3050. $repeat = str_repeat('/%', $i + 1);
  3051. $notLikeCondition .= " AND docs.path NOT LIKE '".Database::escape_string($cleanedPath.$repeat)."' ";
  3052. }
  3053. $folderId = (int) $folderId;
  3054. $folderCondition = " AND
  3055. docs.id <> $folderId AND
  3056. docs.path LIKE '".$cleanedPath."/%'
  3057. $notLikeCondition
  3058. ";
  3059. } else {
  3060. $folderCondition = " AND docs.filetype = 'file' ";
  3061. }
  3062. }
  3063. $levelCondition = '';
  3064. if ($folderId === false) {
  3065. $levelCondition = " AND docs.path NOT LIKE'/%/%'";
  3066. }
  3067. $sql = "SELECT DISTINCT last.visibility, docs.*
  3068. FROM $tbl_item_prop AS last
  3069. INNER JOIN $tbl_doc AS docs
  3070. ON (docs.id = last.ref AND docs.c_id = last.c_id)
  3071. WHERE
  3072. docs.path NOT LIKE '%_DELETED_%' AND
  3073. last.tool = '".TOOL_DOCUMENT."' $condition_session AND
  3074. (last.visibility = '1' $lp_visibility_condition) AND
  3075. last.visibility <> 2 AND
  3076. docs.c_id = {$course_info['real_id']} AND
  3077. last.c_id = {$course_info['real_id']}
  3078. $folderCondition
  3079. $levelCondition
  3080. $add_folder_filter
  3081. ORDER BY docs.filetype DESC, docs.title ASC";
  3082. $res_doc = Database::query($sql);
  3083. $resources = Database::store_result($res_doc, 'ASSOC');
  3084. $return = '';
  3085. if ($lp_id == false && $addCloseButton) {
  3086. if ($folderId === false) {
  3087. $return .= Display::div(
  3088. Display::url(
  3089. Display::return_icon('close.png', get_lang('Close'), [], ICON_SIZE_SMALL),
  3090. ' javascript:void(0);',
  3091. ['id' => 'close_div_'.$course_info['real_id'].'_'.$session_id, 'class' => 'close_div']
  3092. ),
  3093. ['style' => 'position:absolute;right:10px']
  3094. );
  3095. }
  3096. }
  3097. // If you want to debug it, I advise you to do "echo" on the eval statements.
  3098. $newResources = [];
  3099. if (!empty($resources) && $user_in_course) {
  3100. foreach ($resources as $resource) {
  3101. $is_visible = self::is_visible_by_id(
  3102. $resource['id'],
  3103. $course_info,
  3104. $session_id,
  3105. api_get_user_id()
  3106. );
  3107. if ($showInvisibleFiles === false) {
  3108. if (!$is_visible) {
  3109. continue;
  3110. }
  3111. }
  3112. $newResources[] = $resource;
  3113. }
  3114. }
  3115. $label = get_lang('Documents');
  3116. $documents = [];
  3117. if ($folderId === false) {
  3118. $documents[$label] = [
  3119. 'id' => 0,
  3120. 'files' => $newResources,
  3121. ];
  3122. } else {
  3123. if (is_array($parentData)) {
  3124. $documents[$parentData['title']] = [
  3125. 'id' => (int) $folderId,
  3126. 'files' => $newResources,
  3127. ];
  3128. }
  3129. }
  3130. $writeResult = self::write_resources_tree(
  3131. $userInfo,
  3132. $course_info,
  3133. $session_id,
  3134. $documents,
  3135. $lp_id,
  3136. $target,
  3137. $add_move_button,
  3138. $overwrite_url,
  3139. $folderId
  3140. );
  3141. $return .= $writeResult;
  3142. $lpAjaxUrl = api_get_path(WEB_AJAX_PATH).'lp.ajax.php';
  3143. if ($lp_id === false) {
  3144. $url = $lpAjaxUrl.'?a=get_documents&lp_id=&cidReq='.$course_info['code'];
  3145. $return .= "<script>
  3146. $(function() {
  3147. $('.close_div').click(function() {
  3148. var course_id = this.id.split('_')[2];
  3149. var session_id = this.id.split('_')[3];
  3150. $('#document_result_'+course_id+'_'+session_id).hide();
  3151. $('.lp_resource').remove();
  3152. $('.document_preview_container').html('');
  3153. });
  3154. });
  3155. </script>";
  3156. } else {
  3157. // For LPs
  3158. $url = $lpAjaxUrl.'?a=get_documents&lp_id='.$lp_id.'&'.api_get_cidreq();
  3159. }
  3160. if (!empty($overwrite_url)) {
  3161. $url .= '&url='.urlencode(Security::remove_XSS($overwrite_url));
  3162. }
  3163. if ($add_move_button) {
  3164. $url .= '&add_move_button=1';
  3165. }
  3166. $return .= "<script>
  3167. function testResources(id, img) {
  3168. var numericId = id.split('_')[1];
  3169. var parentId = 'doc_id_'+numericId;
  3170. var tempId = 'temp_'+numericId;
  3171. var image = $('#'+img);
  3172. if (image.hasClass('open')) {
  3173. image.removeClass('open');
  3174. image.attr('src', '".Display::returnIconPath('nolines_plus.gif')."');
  3175. $('#'+id).show();
  3176. $('#'+tempId).hide();
  3177. } else {
  3178. image.addClass('open');
  3179. image.attr('src', '".Display::returnIconPath('nolines_minus.gif')."');
  3180. $('#'+id).hide();
  3181. $('#'+tempId).show();
  3182. var tempDiv = $('#'+parentId).find('#'+tempId);
  3183. if (tempDiv.length == 0) {
  3184. $.ajax({
  3185. type: 'GET',
  3186. async: false,
  3187. url: '".$url."',
  3188. data: 'folder_id='+numericId,
  3189. success: function(data) {
  3190. tempDiv = $('#doc_id_'+numericId).append('<div id='+tempId+'>'+data+'</div>');
  3191. }
  3192. });
  3193. }
  3194. }
  3195. }
  3196. </script>";
  3197. if (!$user_in_course) {
  3198. $return = '';
  3199. }
  3200. return $return;
  3201. }
  3202. /**
  3203. * Generate and return an HTML list of resources based on a given array.
  3204. * This list is used to show the course creator a list of available resources to choose from
  3205. * when creating a learning path.
  3206. *
  3207. * @param array $userInfo current user info
  3208. * @param array $course_info
  3209. * @param int $session_id
  3210. * @param array $documents
  3211. * @param bool $lp_id
  3212. * @param string $target
  3213. * @param bool $add_move_button
  3214. * @param string $overwrite_url
  3215. * @param int $folderId
  3216. *
  3217. * @return string
  3218. */
  3219. public static function write_resources_tree(
  3220. $userInfo,
  3221. $course_info,
  3222. $session_id,
  3223. $documents,
  3224. $lp_id = false,
  3225. $target = '',
  3226. $add_move_button = false,
  3227. $overwrite_url = '',
  3228. $folderId = false
  3229. ) {
  3230. $return = '';
  3231. if (!empty($documents)) {
  3232. foreach ($documents as $key => $resource) {
  3233. if (isset($resource['id']) && is_int($resource['id'])) {
  3234. $mainFolderResource = [
  3235. 'id' => $resource['id'],
  3236. 'title' => $key,
  3237. ];
  3238. if ($folderId === false) {
  3239. $return .= self::parseFolder($folderId, $mainFolderResource, $lp_id);
  3240. }
  3241. if (isset($resource['files'])) {
  3242. $return .= self::write_resources_tree(
  3243. $userInfo,
  3244. $course_info,
  3245. $session_id,
  3246. $resource['files'],
  3247. $lp_id,
  3248. $target,
  3249. $add_move_button,
  3250. $overwrite_url
  3251. );
  3252. }
  3253. $return .= '</div>';
  3254. $return .= '</ul>';
  3255. } else {
  3256. if ($resource['filetype'] === 'folder') {
  3257. $return .= self::parseFolder($folderId, $resource, $lp_id);
  3258. } else {
  3259. $return .= self::parseFile(
  3260. $userInfo,
  3261. $course_info,
  3262. $session_id,
  3263. $resource,
  3264. $lp_id,
  3265. $add_move_button,
  3266. $target,
  3267. $overwrite_url
  3268. );
  3269. }
  3270. }
  3271. }
  3272. }
  3273. return $return;
  3274. }
  3275. /**
  3276. * @param int $doc_id
  3277. * @param array $courseInfo
  3278. * @param int $sessionId
  3279. * @param int $user_id
  3280. * @param int $groupId iid
  3281. * @param bool $checkParentVisibility
  3282. *
  3283. * @return bool
  3284. */
  3285. public static function check_visibility_tree(
  3286. $doc_id,
  3287. $courseInfo,
  3288. $sessionId,
  3289. $user_id,
  3290. $groupId = 0,
  3291. $checkParentVisibility = true
  3292. ) {
  3293. if (empty($courseInfo)) {
  3294. return false;
  3295. }
  3296. $courseCode = $courseInfo['code'];
  3297. if (empty($courseCode)) {
  3298. return false;
  3299. }
  3300. $document_data = self::get_document_data_by_id(
  3301. $doc_id,
  3302. $courseCode,
  3303. null,
  3304. $sessionId
  3305. );
  3306. if ($sessionId != 0 && !$document_data) {
  3307. $document_data = self::get_document_data_by_id(
  3308. $doc_id,
  3309. $courseCode,
  3310. null,
  3311. 0
  3312. );
  3313. }
  3314. if (!empty($document_data)) {
  3315. // If admin or course teacher, allow anyway
  3316. if (api_is_platform_admin() || CourseManager::is_course_teacher($user_id, $courseCode)) {
  3317. return true;
  3318. }
  3319. if ($document_data['parent_id'] == false || empty($document_data['parent_id'])) {
  3320. if (!empty($groupId)) {
  3321. return true;
  3322. }
  3323. $visible = self::is_visible_by_id($doc_id, $courseInfo, $sessionId, $user_id);
  3324. return $visible;
  3325. } else {
  3326. $visible = self::is_visible_by_id($doc_id, $courseInfo, $sessionId, $user_id);
  3327. if (!$visible) {
  3328. return false;
  3329. } else {
  3330. if ($checkParentVisibility) {
  3331. return self::check_visibility_tree(
  3332. $document_data['parent_id'],
  3333. $courseInfo,
  3334. $sessionId,
  3335. $user_id,
  3336. $groupId
  3337. );
  3338. }
  3339. return true;
  3340. }
  3341. }
  3342. } else {
  3343. return false;
  3344. }
  3345. }
  3346. /**
  3347. * Index a given document.
  3348. *
  3349. * @param int Document ID inside its corresponding course
  3350. * @param string Course code
  3351. * @param int Session ID (not used yet)
  3352. * @param string Language of document's content (defaults to course language)
  3353. * @param array Array of specific fields (['code'=>'value',...])
  3354. * @param string What to do if the file already exists (default or overwrite)
  3355. * @param bool When set to true, this runs the indexer without actually saving anything to any database
  3356. *
  3357. * @return bool Returns true on presumed success, false on failure
  3358. */
  3359. public static function index_document(
  3360. $docid,
  3361. $course_code,
  3362. $session_id = 0,
  3363. $lang = 'english',
  3364. $specific_fields_values = [],
  3365. $if_exists = '',
  3366. $simulation = false
  3367. ) {
  3368. if (api_get_setting('search_enabled') !== 'true') {
  3369. return false;
  3370. }
  3371. if (empty($docid) or $docid != intval($docid)) {
  3372. return false;
  3373. }
  3374. if (empty($session_id)) {
  3375. $session_id = api_get_session_id();
  3376. }
  3377. $course_info = api_get_course_info($course_code);
  3378. $course_dir = $course_info['path'].'/document';
  3379. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  3380. $base_work_dir = $sys_course_path.$course_dir;
  3381. $course_id = $course_info['real_id'];
  3382. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  3383. $qry = "SELECT path, title FROM $table_document WHERE c_id = $course_id AND id = '$docid' LIMIT 1";
  3384. $result = Database::query($qry);
  3385. if (Database::num_rows($result) == 1) {
  3386. $row = Database::fetch_array($result);
  3387. $doc_path = api_get_path(SYS_COURSE_PATH).$course_dir.$row['path'];
  3388. //TODO: mime_content_type is deprecated, fileinfo php extension is enabled by default as of PHP 5.3.0
  3389. // now versions of PHP on Debian testing(5.2.6-5) and Ubuntu(5.2.6-2ubuntu) are lower, so wait for a while
  3390. $doc_mime = mime_content_type($doc_path);
  3391. $allowed_mime_types = self::file_get_mime_type(true);
  3392. // mime_content_type does not detect correctly some formats that
  3393. // are going to be supported for index, so an extensions array is used for the moment
  3394. if (empty($doc_mime)) {
  3395. $allowed_extensions = [
  3396. 'doc',
  3397. 'docx',
  3398. 'ppt',
  3399. 'pptx',
  3400. 'pps',
  3401. 'ppsx',
  3402. 'xls',
  3403. 'xlsx',
  3404. 'odt',
  3405. 'odp',
  3406. 'ods',
  3407. 'pdf',
  3408. 'txt',
  3409. 'rtf',
  3410. 'msg',
  3411. 'csv',
  3412. 'html',
  3413. 'htm',
  3414. ];
  3415. $extensions = preg_split("/[\/\\.]/", $doc_path);
  3416. $doc_ext = strtolower($extensions[count($extensions) - 1]);
  3417. if (in_array($doc_ext, $allowed_extensions)) {
  3418. switch ($doc_ext) {
  3419. case 'ppt':
  3420. case 'pps':
  3421. $doc_mime = 'application/vnd.ms-powerpoint';
  3422. break;
  3423. case 'xls':
  3424. $doc_mime = 'application/vnd.ms-excel';
  3425. break;
  3426. }
  3427. }
  3428. }
  3429. //@todo move this nightmare in a search controller or something like that!!! J.M
  3430. if (in_array($doc_mime, $allowed_mime_types)) {
  3431. $file_title = $row['title'];
  3432. $file_content = self::get_text_content($doc_path, $doc_mime);
  3433. $course_code = Database::escape_string($course_code);
  3434. $ic_slide = new IndexableChunk();
  3435. $ic_slide->addValue('title', $file_title);
  3436. $ic_slide->addCourseId($course_code);
  3437. $ic_slide->addToolId(TOOL_DOCUMENT);
  3438. $xapian_data = [
  3439. SE_COURSE_ID => $course_code,
  3440. SE_TOOL_ID => TOOL_DOCUMENT,
  3441. SE_DATA => ['doc_id' => $docid],
  3442. SE_USER => api_get_user_id(),
  3443. ];
  3444. $ic_slide->xapian_data = serialize($xapian_data);
  3445. $di = new ChamiloIndexer();
  3446. $return = $di->connectDb(null, null, $lang);
  3447. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  3448. $specific_fields = get_specific_field_list();
  3449. // process different depending on what to do if file exists
  3450. /**
  3451. * @TODO Find a way to really verify if the file had been
  3452. * overwriten. Now all work is done at
  3453. * handle_uploaded_document() and it's difficult to verify it
  3454. */
  3455. if (!empty($if_exists) && $if_exists == 'overwrite') {
  3456. // Overwrite the file on search engine
  3457. // Actually, it consists on a delete of terms from db,
  3458. // insert new ones, create a new search engine document,
  3459. // and remove the old one
  3460. // Get search_did
  3461. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  3462. $sql = 'SELECT * FROM %s WHERE course_code=\'%s\' AND tool_id=\'%s\' AND ref_id_high_level=%s LIMIT 1';
  3463. $sql = sprintf($sql, $tbl_se_ref, $course_code, TOOL_DOCUMENT, $docid);
  3464. $res = Database::query($sql);
  3465. if (Database::num_rows($res) > 0) {
  3466. $se_ref = Database::fetch_array($res);
  3467. if (!$simulation) {
  3468. $di->remove_document($se_ref['search_did']);
  3469. }
  3470. $all_specific_terms = '';
  3471. foreach ($specific_fields as $specific_field) {
  3472. if (!$simulation) {
  3473. delete_all_specific_field_value($course_code, $specific_field['id'], TOOL_DOCUMENT, $docid);
  3474. }
  3475. // Update search engine
  3476. if (isset($specific_fields_values[$specific_field['code']])) {
  3477. $sterms = trim($specific_fields_values[$specific_field['code']]);
  3478. } else { //if the specific field is not defined, force an empty one
  3479. $sterms = '';
  3480. }
  3481. $all_specific_terms .= ' '.$sterms;
  3482. $sterms = explode(',', $sterms);
  3483. foreach ($sterms as $sterm) {
  3484. $sterm = trim($sterm);
  3485. if (!empty($sterm)) {
  3486. $ic_slide->addTerm($sterm, $specific_field['code']);
  3487. // updated the last param here from $value to $sterm without being sure - see commit15464
  3488. if (!$simulation) {
  3489. add_specific_field_value(
  3490. $specific_field['id'],
  3491. $course_code,
  3492. TOOL_DOCUMENT,
  3493. $docid,
  3494. $sterm
  3495. );
  3496. }
  3497. }
  3498. }
  3499. }
  3500. // Add terms also to content to make terms findable by probabilistic search
  3501. $file_content = $all_specific_terms.' '.$file_content;
  3502. if (!$simulation) {
  3503. $ic_slide->addValue('content', $file_content);
  3504. $di->addChunk($ic_slide);
  3505. // Index and return a new search engine document id
  3506. $did = $di->index();
  3507. if ($did) {
  3508. // update the search_did on db
  3509. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  3510. $sql = 'UPDATE %s SET search_did=%d WHERE id=%d LIMIT 1';
  3511. $sql = sprintf($sql, $tbl_se_ref, (int) $did, (int) $se_ref['id']);
  3512. Database::query($sql);
  3513. }
  3514. }
  3515. }
  3516. } else {
  3517. // Add all terms
  3518. $all_specific_terms = '';
  3519. foreach ($specific_fields as $specific_field) {
  3520. if (isset($specific_fields_values[$specific_field['code']])) {
  3521. $sterms = trim($specific_fields_values[$specific_field['code']]);
  3522. } else { //if the specific field is not defined, force an empty one
  3523. $sterms = '';
  3524. }
  3525. $all_specific_terms .= ' '.$sterms;
  3526. if (!empty($sterms)) {
  3527. $sterms = explode(',', $sterms);
  3528. foreach ($sterms as $sterm) {
  3529. if (!$simulation) {
  3530. $ic_slide->addTerm(trim($sterm), $specific_field['code']);
  3531. add_specific_field_value(
  3532. $specific_field['id'],
  3533. $course_code,
  3534. TOOL_DOCUMENT,
  3535. $docid,
  3536. $sterm
  3537. );
  3538. }
  3539. }
  3540. }
  3541. }
  3542. // Add terms also to content to make terms findable by probabilistic search
  3543. $file_content = $all_specific_terms.' '.$file_content;
  3544. if (!$simulation) {
  3545. $ic_slide->addValue('content', $file_content);
  3546. $di->addChunk($ic_slide);
  3547. // Index and return search engine document id
  3548. $did = $di->index();
  3549. if ($did) {
  3550. // Save it to db
  3551. $tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
  3552. $sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, search_did)
  3553. VALUES (NULL , \'%s\', \'%s\', %s, %s)';
  3554. $sql = sprintf($sql, $tbl_se_ref, $course_code, TOOL_DOCUMENT, $docid, $did);
  3555. Database::query($sql);
  3556. } else {
  3557. return false;
  3558. }
  3559. }
  3560. }
  3561. } else {
  3562. return false;
  3563. }
  3564. }
  3565. return true;
  3566. }
  3567. /**
  3568. * @return array
  3569. */
  3570. public static function get_web_odf_extension_list()
  3571. {
  3572. return ['ods', 'odt', 'odp'];
  3573. }
  3574. /**
  3575. * Set of extension allowed to use Jodconverter.
  3576. *
  3577. * @param $mode 'from'
  3578. * 'to'
  3579. * 'all'
  3580. * @param $format 'text'
  3581. * 'spreadsheet'
  3582. * 'presentation'
  3583. * 'drawing'
  3584. * 'all'
  3585. *
  3586. * @return array
  3587. */
  3588. public static function getJodconverterExtensionList($mode, $format)
  3589. {
  3590. $extensionList = [];
  3591. $extensionListFromText = [
  3592. 'odt',
  3593. 'sxw',
  3594. 'rtf',
  3595. 'doc',
  3596. 'docx',
  3597. 'wpd',
  3598. 'txt',
  3599. ];
  3600. $extensionListToText = [
  3601. 'pdf',
  3602. 'odt',
  3603. 'sxw',
  3604. 'rtf',
  3605. 'doc',
  3606. 'docx',
  3607. 'txt',
  3608. ];
  3609. $extensionListFromSpreadsheet = [
  3610. 'ods',
  3611. 'sxc',
  3612. 'xls',
  3613. 'xlsx',
  3614. 'csv',
  3615. 'tsv',
  3616. ];
  3617. $extensionListToSpreadsheet = [
  3618. 'pdf',
  3619. 'ods',
  3620. 'sxc',
  3621. 'xls',
  3622. 'xlsx',
  3623. 'csv',
  3624. 'tsv',
  3625. ];
  3626. $extensionListFromPresentation = [
  3627. 'odp',
  3628. 'sxi',
  3629. 'ppt',
  3630. 'pptx',
  3631. ];
  3632. $extensionListToPresentation = [
  3633. 'pdf',
  3634. 'swf',
  3635. 'odp',
  3636. 'sxi',
  3637. 'ppt',
  3638. 'pptx',
  3639. ];
  3640. $extensionListFromDrawing = ['odg'];
  3641. $extensionListToDrawing = ['svg', 'swf'];
  3642. if ($mode === 'from') {
  3643. if ($format === 'text') {
  3644. $extensionList = array_merge($extensionList, $extensionListFromText);
  3645. } elseif ($format === 'spreadsheet') {
  3646. $extensionList = array_merge($extensionList, $extensionListFromSpreadsheet);
  3647. } elseif ($format === 'presentation') {
  3648. $extensionList = array_merge($extensionList, $extensionListFromPresentation);
  3649. } elseif ($format === 'drawing') {
  3650. $extensionList = array_merge($extensionList, $extensionListFromDrawing);
  3651. } elseif ($format === 'all') {
  3652. $extensionList = array_merge($extensionList, $extensionListFromText);
  3653. $extensionList = array_merge($extensionList, $extensionListFromSpreadsheet);
  3654. $extensionList = array_merge($extensionList, $extensionListFromPresentation);
  3655. $extensionList = array_merge($extensionList, $extensionListFromDrawing);
  3656. }
  3657. } elseif ($mode === 'to') {
  3658. if ($format === 'text') {
  3659. $extensionList = array_merge($extensionList, $extensionListToText);
  3660. } elseif ($format === 'spreadsheet') {
  3661. $extensionList = array_merge($extensionList, $extensionListToSpreadsheet);
  3662. } elseif ($format === 'presentation') {
  3663. $extensionList = array_merge($extensionList, $extensionListToPresentation);
  3664. } elseif ($format === 'drawing') {
  3665. $extensionList = array_merge($extensionList, $extensionListToDrawing);
  3666. } elseif ($format === 'all') {
  3667. $extensionList = array_merge($extensionList, $extensionListToText);
  3668. $extensionList = array_merge($extensionList, $extensionListToSpreadsheet);
  3669. $extensionList = array_merge($extensionList, $extensionListToPresentation);
  3670. $extensionList = array_merge($extensionList, $extensionListToDrawing);
  3671. }
  3672. } elseif ($mode === 'all') {
  3673. if ($format === 'text') {
  3674. $extensionList = array_merge($extensionList, $extensionListFromText);
  3675. $extensionList = array_merge($extensionList, $extensionListToText);
  3676. } elseif ($format === 'spreadsheet') {
  3677. $extensionList = array_merge($extensionList, $extensionListFromSpreadsheet);
  3678. $extensionList = array_merge($extensionList, $extensionListToSpreadsheet);
  3679. } elseif ($format === 'presentation') {
  3680. $extensionList = array_merge($extensionList, $extensionListFromPresentation);
  3681. $extensionList = array_merge($extensionList, $extensionListToPresentation);
  3682. } elseif ($format === 'drawing') {
  3683. $extensionList = array_merge($extensionList, $extensionListFromDrawing);
  3684. $extensionList = array_merge($extensionList, $extensionListToDrawing);
  3685. } elseif ($format === 'all') {
  3686. $extensionList = array_merge($extensionList, $extensionListFromText);
  3687. $extensionList = array_merge($extensionList, $extensionListToText);
  3688. $extensionList = array_merge($extensionList, $extensionListFromSpreadsheet);
  3689. $extensionList = array_merge($extensionList, $extensionListToSpreadsheet);
  3690. $extensionList = array_merge($extensionList, $extensionListFromPresentation);
  3691. $extensionList = array_merge($extensionList, $extensionListToPresentation);
  3692. $extensionList = array_merge($extensionList, $extensionListFromDrawing);
  3693. $extensionList = array_merge($extensionList, $extensionListToDrawing);
  3694. }
  3695. }
  3696. return $extensionList;
  3697. }
  3698. /**
  3699. * Get Format type list by extension and mode.
  3700. *
  3701. * @param string $mode Mode to search format type list
  3702. *
  3703. * @example 'from'
  3704. * @example 'to'
  3705. *
  3706. * @param string $extension file extension to check file type
  3707. *
  3708. * @return array
  3709. */
  3710. public static function getFormatTypeListConvertor($mode = 'from', $extension)
  3711. {
  3712. $formatTypesList = [];
  3713. $formatTypes = ['text', 'spreadsheet', 'presentation', 'drawing'];
  3714. foreach ($formatTypes as $formatType) {
  3715. if (in_array($extension, self::getJodconverterExtensionList($mode, $formatType))) {
  3716. $formatTypesList[] = $formatType;
  3717. }
  3718. }
  3719. return $formatTypesList;
  3720. }
  3721. /**
  3722. * @param string $path
  3723. * @param bool $is_certificate_mode
  3724. *
  3725. * @return bool
  3726. */
  3727. public static function is_folder_to_avoid($path, $is_certificate_mode = false)
  3728. {
  3729. $foldersToAvoid = [
  3730. '/HotPotatoes_files',
  3731. '/certificates',
  3732. ];
  3733. $systemFolder = api_get_course_setting('show_system_folders');
  3734. if ($systemFolder == 1) {
  3735. $foldersToAvoid = [];
  3736. }
  3737. if (basename($path) == 'css') {
  3738. return true;
  3739. }
  3740. if ($is_certificate_mode == false) {
  3741. //Certificate results
  3742. if (strstr($path, 'certificates')) {
  3743. return true;
  3744. }
  3745. }
  3746. // Admin setting for Hide/Show the folders of all users
  3747. if (api_get_setting('show_users_folders') == 'false') {
  3748. $foldersToAvoid[] = '/shared_folder';
  3749. if (strstr($path, 'shared_folder_session_')) {
  3750. return true;
  3751. }
  3752. }
  3753. // Admin setting for Hide/Show Default folders to all users
  3754. if (api_get_setting('show_default_folders') == 'false') {
  3755. $foldersToAvoid[] = '/images';
  3756. $foldersToAvoid[] = '/flash';
  3757. $foldersToAvoid[] = '/audio';
  3758. $foldersToAvoid[] = '/video';
  3759. }
  3760. // Admin setting for Hide/Show chat history folder
  3761. if (api_get_setting('show_chat_folder') == 'false') {
  3762. $foldersToAvoid[] = '/chat_files';
  3763. }
  3764. if (is_array($foldersToAvoid)) {
  3765. return in_array($path, $foldersToAvoid);
  3766. } else {
  3767. return false;
  3768. }
  3769. }
  3770. /**
  3771. * @return array
  3772. */
  3773. public static function get_system_folders()
  3774. {
  3775. return [
  3776. '/certificates',
  3777. '/HotPotatoes_files',
  3778. '/chat_files',
  3779. '/images',
  3780. '/flash',
  3781. '/audio',
  3782. '/video',
  3783. '/shared_folder',
  3784. '/learning_path',
  3785. ];
  3786. }
  3787. /**
  3788. * @return array
  3789. */
  3790. public static function getProtectedFolderFromStudent()
  3791. {
  3792. return [
  3793. '/certificates',
  3794. '/HotPotatoes_files',
  3795. '/chat_files',
  3796. '/shared_folder',
  3797. '/learning_path',
  3798. ];
  3799. }
  3800. /**
  3801. * @param string $courseCode
  3802. *
  3803. * @return string 'visible' or 'invisible' string
  3804. */
  3805. public static function getDocumentDefaultVisibility($courseCode)
  3806. {
  3807. $settings = api_get_setting('tool_visible_by_default_at_creation');
  3808. $defaultVisibility = 'visible';
  3809. if (isset($settings['documents'])) {
  3810. $portalDefaultVisibility = 'invisible';
  3811. if ($settings['documents'] == 'true') {
  3812. $portalDefaultVisibility = 'visible';
  3813. }
  3814. $defaultVisibility = $portalDefaultVisibility;
  3815. }
  3816. if (api_get_setting('documents_default_visibility_defined_in_course') == 'true') {
  3817. $courseVisibility = api_get_course_setting('documents_default_visibility', $courseCode);
  3818. if (!empty($courseVisibility) && in_array($courseVisibility, ['visible', 'invisible'])) {
  3819. $defaultVisibility = $courseVisibility;
  3820. }
  3821. }
  3822. return $defaultVisibility;
  3823. }
  3824. /**
  3825. * @param array $courseInfo
  3826. * @param int $id doc id
  3827. * @param string $visibility visible/invisible
  3828. * @param int $userId
  3829. */
  3830. public static function updateVisibilityFromAllSessions($courseInfo, $id, $visibility, $userId)
  3831. {
  3832. $sessionList = SessionManager::get_session_by_course($courseInfo['real_id']);
  3833. if (!empty($sessionList)) {
  3834. foreach ($sessionList as $session) {
  3835. $sessionId = $session['id'];
  3836. api_item_property_update(
  3837. $courseInfo,
  3838. TOOL_DOCUMENT,
  3839. $id,
  3840. $visibility,
  3841. $userId,
  3842. null,
  3843. null,
  3844. null,
  3845. null,
  3846. $sessionId
  3847. );
  3848. }
  3849. }
  3850. }
  3851. /**
  3852. * @param string $filePath
  3853. * @param string $path
  3854. * @param array $courseInfo
  3855. * @param int $sessionId
  3856. * @param string $whatIfFileExists overwrite|rename
  3857. * @param int $userId
  3858. * @param int $groupId
  3859. * @param int $toUserId
  3860. * @param string $comment
  3861. *
  3862. * @return bool|path
  3863. */
  3864. public static function addFileToDocumentTool(
  3865. $filePath,
  3866. $path,
  3867. $courseInfo,
  3868. $sessionId,
  3869. $userId,
  3870. $whatIfFileExists = 'overwrite',
  3871. $groupId = null,
  3872. $toUserId = null,
  3873. $comment = null
  3874. ) {
  3875. if (!file_exists($filePath)) {
  3876. return false;
  3877. }
  3878. $fileInfo = pathinfo($filePath);
  3879. $file = [
  3880. 'name' => $fileInfo['basename'],
  3881. 'tmp_name' => $filePath,
  3882. 'size' => filesize($filePath),
  3883. 'from_file' => true,
  3884. ];
  3885. $course_dir = $courseInfo['path'].'/document';
  3886. $baseWorkDir = api_get_path(SYS_COURSE_PATH).$course_dir;
  3887. $filePath = handle_uploaded_document(
  3888. $courseInfo,
  3889. $file,
  3890. $baseWorkDir,
  3891. $path,
  3892. $userId,
  3893. $groupId,
  3894. $toUserId,
  3895. false,
  3896. $whatIfFileExists,
  3897. false,
  3898. false,
  3899. $comment,
  3900. $sessionId
  3901. );
  3902. if ($filePath) {
  3903. return self::get_document_id(
  3904. $courseInfo,
  3905. $filePath,
  3906. $sessionId
  3907. );
  3908. }
  3909. return false;
  3910. }
  3911. /**
  3912. * Converts wav to mp3 file.
  3913. * Requires the ffmpeg lib. In ubuntu: sudo apt-get install ffmpeg.
  3914. *
  3915. * @param string $wavFile
  3916. * @param bool $removeWavFileIfSuccess
  3917. *
  3918. * @return bool
  3919. */
  3920. public static function convertWavToMp3($wavFile, $removeWavFileIfSuccess = false)
  3921. {
  3922. if (file_exists($wavFile)) {
  3923. try {
  3924. $ffmpeg = \FFMpeg\FFMpeg::create();
  3925. $video = $ffmpeg->open($wavFile);
  3926. $mp3File = str_replace('wav', 'mp3', $wavFile);
  3927. $result = $video->save(new FFMpeg\Format\Audio\Mp3(), $mp3File);
  3928. if ($result && $removeWavFileIfSuccess) {
  3929. unlink($wavFile);
  3930. }
  3931. if (file_exists($mp3File)) {
  3932. return $mp3File;
  3933. }
  3934. } catch (Exception $e) {
  3935. error_log($e->getMessage());
  3936. error_log($e->getPrevious()->getMessage());
  3937. }
  3938. }
  3939. return false;
  3940. }
  3941. /**
  3942. * @param string $documentData wav document information
  3943. * @param array $courseInfo
  3944. * @param int $sessionId
  3945. * @param int $userId user that adds the document
  3946. * @param string $whatIfFileExists
  3947. * @param bool $deleteWavFile
  3948. *
  3949. * @return bool
  3950. */
  3951. public static function addAndConvertWavToMp3(
  3952. $documentData,
  3953. $courseInfo,
  3954. $sessionId,
  3955. $userId,
  3956. $whatIfFileExists = 'overwrite',
  3957. $deleteWavFile = false
  3958. ) {
  3959. if (empty($documentData)) {
  3960. return false;
  3961. }
  3962. if (isset($documentData['absolute_path']) &&
  3963. file_exists($documentData['absolute_path'])
  3964. ) {
  3965. $mp3FilePath = self::convertWavToMp3($documentData['absolute_path']);
  3966. if (!empty($mp3FilePath) && file_exists($mp3FilePath)) {
  3967. $documentId = self::addFileToDocumentTool(
  3968. $mp3FilePath,
  3969. dirname($documentData['path']),
  3970. $courseInfo,
  3971. $sessionId,
  3972. $userId,
  3973. $whatIfFileExists,
  3974. null,
  3975. null,
  3976. $documentData['comment']
  3977. );
  3978. if (!empty($documentId)) {
  3979. if ($deleteWavFile) {
  3980. $coursePath = $courseInfo['directory'].'/document';
  3981. $documentPath = api_get_path(SYS_COURSE_PATH).$coursePath;
  3982. self::delete_document(
  3983. $courseInfo,
  3984. null,
  3985. $documentPath,
  3986. $sessionId,
  3987. $documentData['id']
  3988. );
  3989. }
  3990. return $documentId;
  3991. }
  3992. }
  3993. }
  3994. return false;
  3995. }
  3996. /**
  3997. * Sets.
  3998. *
  3999. * @param string $file ($document_data['path'])
  4000. * @param string $file_url_sys
  4001. *
  4002. * @return string
  4003. */
  4004. public static function generateAudioTempFile($file, $file_url_sys)
  4005. {
  4006. //make temp audio
  4007. $temp_folder = api_get_path(SYS_ARCHIVE_PATH).'temp/audio';
  4008. if (!file_exists($temp_folder)) {
  4009. @mkdir($temp_folder, api_get_permissions_for_new_directories(), true);
  4010. }
  4011. //make htaccess with allow from all, and file index.html into temp/audio
  4012. $htaccess = api_get_path(SYS_ARCHIVE_PATH).'temp/audio/.htaccess';
  4013. if (!file_exists($htaccess)) {
  4014. $htaccess_content = "order deny,allow\r\nallow from all\r\nOptions -Indexes";
  4015. $fp = @fopen(api_get_path(SYS_ARCHIVE_PATH).'temp/audio/.htaccess', 'w');
  4016. if ($fp) {
  4017. fwrite($fp, $htaccess_content);
  4018. fclose($fp);
  4019. }
  4020. }
  4021. //encript temp name file
  4022. $name_crip = sha1(uniqid()); //encript
  4023. $findext = explode(".", $file);
  4024. $extension = $findext[count($findext) - 1];
  4025. $file_crip = $name_crip.'.'.$extension;
  4026. //copy file to temp/audio directory
  4027. $from_sys = $file_url_sys;
  4028. $to_sys = api_get_path(SYS_ARCHIVE_PATH).'temp/audio/'.$file_crip;
  4029. if (file_exists($from_sys)) {
  4030. copy($from_sys, $to_sys);
  4031. }
  4032. // get file from tmp directory
  4033. Session::write('temp_audio_nanogong', $to_sys);
  4034. return api_get_path(WEB_ARCHIVE_PATH).'temp/audio/'.$file_crip;
  4035. }
  4036. /**
  4037. * Erase temp nanogong audio.
  4038. */
  4039. public static function removeGeneratedAudioTempFile()
  4040. {
  4041. $tempAudio = Session::read('temp_audio_nanogong');
  4042. if (!empty(isset($tempAudio)) && is_file($tempAudio)) {
  4043. unlink($tempAudio);
  4044. Session::erase('temp_audio_nanogong');
  4045. }
  4046. }
  4047. /**
  4048. * Check if the path is used in this course.
  4049. *
  4050. * @param array $courseInfo
  4051. * @param string $path
  4052. *
  4053. * @return array
  4054. */
  4055. public static function getDocumentByPathInCourse($courseInfo, $path)
  4056. {
  4057. $table = Database::get_course_table(TABLE_DOCUMENT);
  4058. $path = Database::escape_string($path);
  4059. $courseId = $courseInfo['real_id'];
  4060. if (empty($courseId)) {
  4061. return false;
  4062. }
  4063. $sql = "SELECT * FROM $table WHERE c_id = $courseId AND path = '$path'";
  4064. $result = Database::query($sql);
  4065. return Database::store_result($result, 'ASSOC');
  4066. }
  4067. /**
  4068. * @param array $_course
  4069. *
  4070. * @return int
  4071. */
  4072. public static function createDefaultAudioFolder($_course)
  4073. {
  4074. if (!isset($_course['path'])) {
  4075. return false;
  4076. }
  4077. $audioId = null;
  4078. $path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document/';
  4079. if (!is_dir($path.'audio')) {
  4080. mkdir($path.'audio', api_get_permissions_for_new_directories());
  4081. $audioId = add_document($_course, '/audio', 'folder', 0, get_lang('Audio'));
  4082. api_item_property_update(
  4083. $_course,
  4084. TOOL_DOCUMENT,
  4085. $audioId,
  4086. 'FolderCreated',
  4087. api_get_user_id(),
  4088. null,
  4089. null,
  4090. null,
  4091. null,
  4092. api_get_session_id()
  4093. );
  4094. }
  4095. return $audioId;
  4096. }
  4097. /**
  4098. * Generate a default certificate for a courses.
  4099. *
  4100. * @todo move to certificate lib
  4101. *
  4102. * @global string $css CSS directory
  4103. * @global string $img_dir image directory
  4104. * @global string $default_course_dir Course directory
  4105. * @global string $js JS directory
  4106. *
  4107. * @param array $courseData The course info
  4108. * @param bool $fromBaseCourse
  4109. * @param int $sessionId
  4110. */
  4111. public static function generateDefaultCertificate(
  4112. $courseData,
  4113. $fromBaseCourse = false,
  4114. $sessionId = 0
  4115. ) {
  4116. if (empty($courseData)) {
  4117. return false;
  4118. }
  4119. global $css, $img_dir, $default_course_dir, $js;
  4120. $codePath = api_get_path(REL_CODE_PATH);
  4121. $dir = '/certificates';
  4122. $comment = null;
  4123. $title = get_lang('DefaultCertificate');
  4124. $fileName = api_replace_dangerous_char($title);
  4125. $filePath = api_get_path(SYS_COURSE_PATH)."{$courseData['directory']}/document$dir";
  4126. if (!is_dir($filePath)) {
  4127. mkdir($filePath, api_get_permissions_for_new_directories());
  4128. }
  4129. $fileFullPath = "$filePath/$fileName.html";
  4130. $fileType = 'file';
  4131. $templateContent = file_get_contents(api_get_path(SYS_CODE_PATH).'gradebook/certificate_template/template.html');
  4132. $search = ['{CSS}', '{IMG_DIR}', '{REL_CODE_PATH}', '{COURSE_DIR}'];
  4133. $replace = [$css.$js, $img_dir, $codePath, $default_course_dir];
  4134. $fileContent = str_replace($search, $replace, $templateContent);
  4135. $saveFilePath = "$dir/$fileName.html";
  4136. if ($fromBaseCourse) {
  4137. $defaultCertificateId = self::get_default_certificate_id(
  4138. $courseData['code'],
  4139. 0
  4140. );
  4141. if (!empty($defaultCertificateId)) {
  4142. // We have a certificate from the course base
  4143. $documentData = self::get_document_data_by_id(
  4144. $defaultCertificateId,
  4145. $courseData['code'],
  4146. false,
  4147. 0
  4148. );
  4149. if ($documentData) {
  4150. $fileContent = file_get_contents($documentData['absolute_path']);
  4151. }
  4152. }
  4153. }
  4154. if (file_exists($fileFullPath) === false) {
  4155. $result = file_put_contents($fileFullPath, $fileContent);
  4156. if ($result) {
  4157. $fileSize = filesize($fileFullPath);
  4158. $documentId = add_document(
  4159. $courseData,
  4160. $saveFilePath,
  4161. $fileType,
  4162. $fileSize,
  4163. $title,
  4164. $comment,
  4165. 0, //$readonly = 0,
  4166. true, //$save_visibility = true,
  4167. null, //$group_id = null,
  4168. $sessionId
  4169. );
  4170. api_item_property_update(
  4171. $courseData,
  4172. TOOL_DOCUMENT,
  4173. $documentId,
  4174. 'DocumentAdded',
  4175. api_get_user_id(),
  4176. null,
  4177. null,
  4178. null,
  4179. null,
  4180. $sessionId
  4181. );
  4182. $defaultCertificateId = self::get_default_certificate_id(
  4183. $courseData['code'],
  4184. $sessionId
  4185. );
  4186. if (!isset($defaultCertificateId)) {
  4187. self::attach_gradebook_certificate(
  4188. $courseData['code'],
  4189. $documentId,
  4190. $sessionId
  4191. );
  4192. }
  4193. }
  4194. }
  4195. }
  4196. /**
  4197. * Update the document name.
  4198. *
  4199. * @param int $documentId The document id
  4200. * @param string $newName The new name
  4201. */
  4202. public static function renameDocument($documentId, $newName)
  4203. {
  4204. $documentId = intval($documentId);
  4205. $newName = Database::escape_string($newName);
  4206. $docuentTable = Database::get_course_table(TABLE_DOCUMENT);
  4207. $values = [
  4208. 'title' => $newName,
  4209. ];
  4210. $whereConditions = [
  4211. 'id = ?' => $documentId,
  4212. ];
  4213. Database::update($docuentTable, $values, $whereConditions);
  4214. }
  4215. /**
  4216. * Get folder/file suffix.
  4217. *
  4218. * @param array $courseInfo
  4219. * @param int $sessionId
  4220. * @param int $groupId
  4221. *
  4222. * @return string
  4223. */
  4224. public static function getDocumentSuffix($courseInfo, $sessionId, $groupId)
  4225. {
  4226. // If no session or group, then no suffix.
  4227. if (empty($sessionId) && empty($groupId)) {
  4228. return '';
  4229. }
  4230. return '__'.intval($sessionId).'__'.intval($groupId);
  4231. }
  4232. /**
  4233. * Fix a document name adding session id and group id
  4234. * Turns picture.jpg -> picture__1__2.jpg
  4235. * Where 1 = session id and 2 group id
  4236. * Of session id and group id are empty then the function returns:
  4237. * picture.jpg -> picture.jpg.
  4238. *
  4239. * @param string $name folder or file name
  4240. * @param string $type 'folder' or 'file'
  4241. * @param array $courseInfo
  4242. * @param int $sessionId
  4243. * @param int $groupId
  4244. *
  4245. * @return string
  4246. */
  4247. public static function fixDocumentName($name, $type, $courseInfo, $sessionId, $groupId)
  4248. {
  4249. $suffix = self::getDocumentSuffix($courseInfo, $sessionId, $groupId);
  4250. switch ($type) {
  4251. case 'folder':
  4252. $name = $name.$suffix;
  4253. break;
  4254. case 'file':
  4255. $name = self::addSuffixToFileName($name, $suffix);
  4256. break;
  4257. }
  4258. return $name;
  4259. }
  4260. /**
  4261. * Add a suffix to a file Example:
  4262. * /folder/picture.jpg => to /folder/picture_this.jpg
  4263. * where "_this" is the suffix.
  4264. *
  4265. * @param string $name
  4266. * @param string $suffix
  4267. *
  4268. * @return string
  4269. */
  4270. public static function addSuffixToFileName($name, $suffix)
  4271. {
  4272. $extension = pathinfo($name, PATHINFO_EXTENSION);
  4273. $fileName = pathinfo($name, PATHINFO_FILENAME);
  4274. $dir = pathinfo($name, PATHINFO_DIRNAME);
  4275. if ($dir == '.') {
  4276. $dir = null;
  4277. }
  4278. if (!empty($dir) && $dir != '/') {
  4279. $dir = $dir.'/';
  4280. }
  4281. $name = $dir.$fileName.$suffix.'.'.$extension;
  4282. return $name;
  4283. }
  4284. /**
  4285. * Check if folder exist in the course base or in the session course.
  4286. *
  4287. * @param string $folder Example: /folder/folder2
  4288. * @param array $courseInfo
  4289. * @param int $sessionId
  4290. * @param int $groupId group.id
  4291. *
  4292. * @return bool
  4293. */
  4294. public static function folderExists(
  4295. $folder,
  4296. $courseInfo,
  4297. $sessionId,
  4298. $groupId
  4299. ) {
  4300. $courseId = $courseInfo['real_id'];
  4301. if (empty($courseId)) {
  4302. return false;
  4303. }
  4304. $sessionId = intval($sessionId);
  4305. $folderWithSuffix = self::fixDocumentName(
  4306. $folder,
  4307. 'folder',
  4308. $courseInfo,
  4309. $sessionId,
  4310. $groupId
  4311. );
  4312. $folder = Database::escape_string($folder);
  4313. $folderWithSuffix = Database::escape_string($folderWithSuffix);
  4314. // Check if pathname already exists inside document table
  4315. $tbl_document = Database::get_course_table(TABLE_DOCUMENT);
  4316. $sql = "SELECT id, path FROM $tbl_document
  4317. WHERE
  4318. filetype = 'folder' AND
  4319. c_id = $courseId AND
  4320. (path = '$folder' OR path = '$folderWithSuffix') AND
  4321. (session_id = 0 OR session_id = $sessionId)
  4322. ";
  4323. $rs = Database::query($sql);
  4324. if (Database::num_rows($rs)) {
  4325. return true;
  4326. }
  4327. return false;
  4328. }
  4329. /**
  4330. * Check if file exist in the course base or in the session course.
  4331. *
  4332. * @param string $fileName Example: /folder/picture.jpg
  4333. * @param array $courseInfo
  4334. * @param int $sessionId
  4335. * @param int $groupId
  4336. *
  4337. * @return bool
  4338. */
  4339. public static function documentExists(
  4340. $fileName,
  4341. $courseInfo,
  4342. $sessionId,
  4343. $groupId
  4344. ) {
  4345. $courseId = $courseInfo['real_id'];
  4346. if (empty($courseId)) {
  4347. return false;
  4348. }
  4349. $sessionId = intval($sessionId);
  4350. $fileNameEscape = Database::escape_string($fileName);
  4351. $fileNameWithSuffix = self::fixDocumentName(
  4352. $fileName,
  4353. 'file',
  4354. $courseInfo,
  4355. $sessionId,
  4356. $groupId
  4357. );
  4358. $fileNameWithSuffix = Database::escape_string($fileNameWithSuffix);
  4359. // Check if pathname already exists inside document table
  4360. $table = Database::get_course_table(TABLE_DOCUMENT);
  4361. $sql = "SELECT id, path FROM $table
  4362. WHERE
  4363. filetype = 'file' AND
  4364. c_id = $courseId AND
  4365. (
  4366. path = '".$fileNameEscape."' OR
  4367. path = '$fileNameWithSuffix'
  4368. ) AND
  4369. (session_id = 0 OR session_id = $sessionId)
  4370. ";
  4371. $rs = Database::query($sql);
  4372. if (Database::num_rows($rs)) {
  4373. return true;
  4374. }
  4375. return false;
  4376. }
  4377. /**
  4378. * Undo the suffix applied to a file example:
  4379. * turns picture__1__1.jpg to picture.jpg.
  4380. *
  4381. * @param string $name
  4382. * @param int $courseId
  4383. * @param int $sessionId
  4384. * @param int $groupId
  4385. *
  4386. * @return string
  4387. */
  4388. public static function undoFixDocumentName(
  4389. $name,
  4390. $courseId,
  4391. $sessionId,
  4392. $groupId
  4393. ) {
  4394. if (empty($sessionId) && empty($groupId)) {
  4395. return $name;
  4396. }
  4397. $suffix = self::getDocumentSuffix(
  4398. ['real_id' => $courseId],
  4399. $sessionId,
  4400. $groupId
  4401. );
  4402. $name = str_replace($suffix, '', $name);
  4403. return $name;
  4404. }
  4405. /**
  4406. * @param string $path
  4407. * @param string $name
  4408. * @param array $courseInfo
  4409. * @param int $sessionId
  4410. * @param int $groupId
  4411. *
  4412. * @return string
  4413. */
  4414. public static function getUniqueFileName($path, $name, $courseInfo, $sessionId, $groupId)
  4415. {
  4416. $counter = 1;
  4417. $filePath = $path.$name;
  4418. $uniqueName = $name;
  4419. while ($documentExists = self::documentExists(
  4420. $filePath,
  4421. $courseInfo,
  4422. $sessionId,
  4423. $groupId
  4424. )) {
  4425. $uniqueName = self::addSuffixToFileName($name, '_'.$counter);
  4426. $filePath = $path.$uniqueName;
  4427. $counter++;
  4428. }
  4429. return $uniqueName;
  4430. }
  4431. /**
  4432. * Builds the form that enables the user to
  4433. * select a directory to browse/upload in.
  4434. *
  4435. * @param array An array containing the folders we want to be able to select
  4436. * @param string The current folder (path inside of the "document" directory, including the prefix "/")
  4437. * @param string Group directory, if empty, prevents documents to be uploaded
  4438. * (because group documents cannot be uploaded in root)
  4439. * @param bool Whether to change the renderer (this will add a template <span>
  4440. * to the QuickForm object displaying the form)
  4441. *
  4442. * @return string html form
  4443. */
  4444. public static function build_directory_selector(
  4445. $folders,
  4446. $document_id,
  4447. $group_dir = '',
  4448. $change_renderer = false,
  4449. &$form = null,
  4450. $selectName = 'id'
  4451. ) {
  4452. $doc_table = Database::get_course_table(TABLE_DOCUMENT);
  4453. $course_id = api_get_course_int_id();
  4454. $folder_titles = [];
  4455. if (is_array($folders)) {
  4456. $escaped_folders = [];
  4457. foreach ($folders as $key => &$val) {
  4458. $escaped_folders[$key] = Database::escape_string($val);
  4459. }
  4460. $folder_sql = implode("','", $escaped_folders);
  4461. $sql = "SELECT path, title
  4462. FROM $doc_table
  4463. WHERE
  4464. filetype = 'folder' AND
  4465. c_id = $course_id AND
  4466. path IN ('".$folder_sql."')";
  4467. $res = Database::query($sql);
  4468. $folder_titles = [];
  4469. while ($obj = Database::fetch_object($res)) {
  4470. $folder_titles[$obj->path] = $obj->title;
  4471. }
  4472. }
  4473. $attributes = [];
  4474. if (empty($form)) {
  4475. $form = new FormValidator('selector', 'GET', api_get_self().'?'.api_get_cidreq());
  4476. $attributes = ['onchange' => 'javascript: document.selector.submit();'];
  4477. }
  4478. $form->addElement('hidden', 'cidReq', api_get_course_id());
  4479. $form->addElement('hidden', 'id_session', api_get_session_id());
  4480. $form->addElement('hidden', 'gidReq', api_get_group_id());
  4481. $parent_select = $form->addSelect(
  4482. $selectName,
  4483. get_lang('CurrentDirectory'),
  4484. '',
  4485. $attributes
  4486. );
  4487. // Group documents cannot be uploaded in the root
  4488. if (empty($group_dir)) {
  4489. $parent_select->addOption(get_lang('Documents'), '/');
  4490. if (is_array($folders)) {
  4491. foreach ($folders as $folder_id => &$folder) {
  4492. $selected = ($document_id == $folder_id) ? ' selected="selected"' : '';
  4493. $path_parts = explode('/', $folder);
  4494. $folder_titles[$folder] = cut($folder_titles[$folder], 80);
  4495. $counter = count($path_parts) - 2;
  4496. if ($counter > 0) {
  4497. $label = str_repeat('&nbsp;&nbsp;&nbsp;', $counter).' &mdash; '.$folder_titles[$folder];
  4498. } else {
  4499. $label = ' &mdash; '.$folder_titles[$folder];
  4500. }
  4501. $parent_select->addOption($label, $folder_id);
  4502. if ($selected != '') {
  4503. $parent_select->setSelected($folder_id);
  4504. }
  4505. }
  4506. }
  4507. } else {
  4508. if (!empty($folders)) {
  4509. foreach ($folders as $folder_id => &$folder) {
  4510. $selected = ($document_id == $folder_id) ? ' selected="selected"' : '';
  4511. $label = $folder_titles[$folder];
  4512. if ($folder == $group_dir) {
  4513. $label = get_lang('Documents');
  4514. } else {
  4515. $path_parts = explode('/', str_replace($group_dir, '', $folder));
  4516. $label = cut($label, 80);
  4517. $label = str_repeat('&nbsp;&nbsp;&nbsp;', count($path_parts) - 2).' &mdash; '.$label;
  4518. }
  4519. $parent_select->addOption($label, $folder_id);
  4520. if ($selected != '') {
  4521. $parent_select->setSelected($folder_id);
  4522. }
  4523. }
  4524. }
  4525. }
  4526. $html = $form->toHtml();
  4527. return $html;
  4528. }
  4529. /**
  4530. * Create a html hyperlink depending on if it's a folder or a file.
  4531. *
  4532. * @param string $documentWebPath
  4533. * @param array $document_data
  4534. * @param bool $show_as_icon - if it is true, only a clickable icon will be shown
  4535. * @param int $visibility (1/0)
  4536. * @param int $counter
  4537. * @param int $size
  4538. * @param bool $isAllowedToEdit
  4539. * @param bool $isCertificateMode
  4540. *
  4541. * @return string url
  4542. */
  4543. public static function create_document_link(
  4544. $documentWebPath,
  4545. $document_data,
  4546. $show_as_icon = false,
  4547. $counter = null,
  4548. $visibility,
  4549. $size = 0,
  4550. $isAllowedToEdit = false,
  4551. $isCertificateMode = false
  4552. ) {
  4553. global $dbl_click_id;
  4554. $www = $documentWebPath;
  4555. $sessionId = api_get_session_id();
  4556. $courseParams = api_get_cidreq();
  4557. $webODFList = self::get_web_odf_extension_list();
  4558. // Get the title or the basename depending on what we're using
  4559. if ($document_data['title'] != '') {
  4560. $title = $document_data['title'];
  4561. } else {
  4562. $title = basename($document_data['path']);
  4563. }
  4564. $filetype = $document_data['filetype'];
  4565. $path = $document_data['path'];
  4566. $url_path = urlencode($document_data['path']);
  4567. $basePageUrl = api_get_path(WEB_CODE_PATH).'document/';
  4568. $pageUrl = $basePageUrl.'document.php';
  4569. // Add class="invisible" on invisible files
  4570. $visibility_class = $visibility == false ? ' class="muted"' : '';
  4571. $forcedownload_link = '';
  4572. $forcedownload_icon = '';
  4573. $prevent_multiple_click = '';
  4574. $force_download_html = '';
  4575. if (!$show_as_icon) {
  4576. // Build download link (icon)
  4577. $forcedownload_link = $filetype == 'folder'
  4578. ? $pageUrl.'?'.$courseParams.'&action=downloadfolder&id='.$document_data['id']
  4579. : $pageUrl.'?'.$courseParams.'&amp;action=download&amp;id='.$document_data['id'];
  4580. // Folder download or file download?
  4581. $forcedownload_icon = $filetype == 'folder' ? 'save_pack.png' : 'save.png';
  4582. // Prevent multiple clicks on zipped folder download
  4583. $prevent_multiple_click = $filetype == 'folder' ? " onclick=\"javascript: if(typeof clic_$dbl_click_id == 'undefined' || !clic_$dbl_click_id) { clic_$dbl_click_id=true; window.setTimeout('clic_".($dbl_click_id++)."=false;',10000); } else { return false; }\"" : '';
  4584. }
  4585. $target = '_self';
  4586. $is_browser_viewable_file = false;
  4587. if ($filetype == 'file') {
  4588. // Check the extension
  4589. $ext = explode('.', $path);
  4590. $ext = strtolower($ext[sizeof($ext) - 1]);
  4591. // HTML-files an some other types are shown in a frameset by default.
  4592. $is_browser_viewable_file = self::isBrowserViewable($ext);
  4593. if ($is_browser_viewable_file) {
  4594. if ($ext == 'pdf' || in_array($ext, $webODFList)) {
  4595. $url = $pageUrl.'?'.$courseParams.'&amp;action=download&amp;id='.$document_data['id'];
  4596. } else {
  4597. $url = $basePageUrl.'showinframes.php?'.$courseParams.'&id='.$document_data['id'];
  4598. }
  4599. } else {
  4600. // url-encode for problematic characters (we may not call them dangerous characters...)
  4601. //$path = str_replace('%2F', '/', $url_path).'?'.$courseParams;
  4602. $url = $www.str_replace('%2F', '/', $url_path).'?'.$courseParams;
  4603. }
  4604. } else {
  4605. $url = $pageUrl.'?'.$courseParams.'&id='.$document_data['id'];
  4606. }
  4607. if ($isCertificateMode) {
  4608. $url .= '&certificate=true&selectcat='.(isset($_GET['selectcat']) ? $_GET['selectcat'] : '');
  4609. }
  4610. // The little download icon
  4611. $tooltip_title = $title;
  4612. $tooltip_title_alt = $tooltip_title;
  4613. if ($filetype == 'link') {
  4614. $tooltip_title_alt = $title;
  4615. $url = $document_data['comment'].'" target="_blank';
  4616. }
  4617. if ($path == '/shared_folder') {
  4618. $tooltip_title_alt = get_lang('UserFolders');
  4619. } elseif (strstr($path, 'shared_folder_session_')) {
  4620. $tooltip_title_alt = get_lang('UserFolders').' ('.api_get_session_name(api_get_session_id()).')';
  4621. } elseif (strstr($tooltip_title, 'sf_user_')) {
  4622. $userinfo = api_get_user_info(substr($tooltip_title, 8));
  4623. $tooltip_title_alt = get_lang('UserFolder').' '.$userinfo['complete_name'];
  4624. } elseif ($path == '/chat_files') {
  4625. $tooltip_title_alt = get_lang('ChatFiles');
  4626. } elseif ($path == '/learning_path') {
  4627. $tooltip_title_alt = get_lang('LearningPaths');
  4628. } elseif ($path == '/video') {
  4629. $tooltip_title_alt = get_lang('Video');
  4630. } elseif ($path == '/audio') {
  4631. $tooltip_title_alt = get_lang('Audio');
  4632. } elseif ($path == '/flash') {
  4633. $tooltip_title_alt = get_lang('Flash');
  4634. } elseif ($path == '/images') {
  4635. $tooltip_title_alt = get_lang('Images');
  4636. } elseif ($path == '/images/gallery') {
  4637. $tooltip_title_alt = get_lang('DefaultCourseImages');
  4638. }
  4639. $copyToMyFiles = $open_in_new_window_link = '';
  4640. $curdirpath = isset($_GET['curdirpath']) ? Security::remove_XSS($_GET['curdirpath']) : null;
  4641. $send_to = null;
  4642. $checkExtension = $path;
  4643. $extension = pathinfo($path, PATHINFO_EXTENSION);
  4644. $document_data['file_extension'] = $extension;
  4645. if (!$show_as_icon) {
  4646. if ($filetype == 'folder') {
  4647. if ($isAllowedToEdit ||
  4648. api_is_platform_admin() ||
  4649. api_get_setting('students_download_folders') == 'true'
  4650. ) {
  4651. // filter: when I am into a shared folder, I can only show "my shared folder" for donwload
  4652. if (self::is_shared_folder($curdirpath, $sessionId)) {
  4653. if (preg_match('/shared_folder\/sf_user_'.api_get_user_id().'$/', urldecode($forcedownload_link)) ||
  4654. preg_match('/shared_folder_session_'.$sessionId.'\/sf_user_'.api_get_user_id().'$/', urldecode($forcedownload_link)) ||
  4655. $isAllowedToEdit || api_is_platform_admin()
  4656. ) {
  4657. $force_download_html = ($size == 0) ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.
  4658. Display::return_icon($forcedownload_icon, get_lang('Download'), [], ICON_SIZE_SMALL).'</a>';
  4659. }
  4660. } elseif (!preg_match('/shared_folder/', urldecode($forcedownload_link)) ||
  4661. $isAllowedToEdit ||
  4662. api_is_platform_admin()
  4663. ) {
  4664. $force_download_html = ($size == 0) ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.'>'.
  4665. Display::return_icon($forcedownload_icon, get_lang('Download'), [], ICON_SIZE_SMALL).'</a>';
  4666. }
  4667. }
  4668. } else {
  4669. $force_download_html = ($size == 0) ? '' : '<a href="'.$forcedownload_link.'" style="float:right"'.$prevent_multiple_click.' download="'.$document_data['basename'].'">'.
  4670. Display::return_icon($forcedownload_icon, get_lang('Download'), [], ICON_SIZE_SMALL).'</a>';
  4671. }
  4672. // Copy files to user's myfiles
  4673. if (api_get_setting('allow_my_files') === 'true' &&
  4674. api_get_setting('users_copy_files') === 'true' && api_is_anonymous() === false
  4675. ) {
  4676. $copy_myfiles_link = $filetype == 'file' ? $pageUrl.'?'.$courseParams.'&action=copytomyfiles&id='.$document_data['id'] : api_get_self().'?'.$courseParams;
  4677. if ($filetype == 'file') {
  4678. $copyToMyFiles = '<a href="'.$copy_myfiles_link.'" style="float:right"'.$prevent_multiple_click.'>'.
  4679. Display::return_icon('briefcase.png', get_lang('CopyToMyFiles'), [], ICON_SIZE_SMALL).'&nbsp;&nbsp;</a>';
  4680. if (api_get_setting('allow_my_files') === 'false') {
  4681. $copyToMyFiles = '';
  4682. }
  4683. }
  4684. }
  4685. $pdf_icon = '';
  4686. if (!$isAllowedToEdit &&
  4687. api_get_setting('students_export2pdf') == 'true' &&
  4688. $filetype == 'file' &&
  4689. in_array($extension, ['html', 'htm'])
  4690. ) {
  4691. $pdf_icon = ' <a style="float:right".'.$prevent_multiple_click.' href="'.$pageUrl.'?'.$courseParams.'&action=export_to_pdf&id='.$document_data['id'].'&curdirpath='.$curdirpath.'">'.
  4692. Display::return_icon('pdf.png', get_lang('Export2PDF'), [], ICON_SIZE_SMALL).'</a> ';
  4693. }
  4694. if ($is_browser_viewable_file) {
  4695. $open_in_new_window_link = '<a href="'.$www.str_replace('%2F', '/', $url_path).'?'.$courseParams.'" style="float:right"'.$prevent_multiple_click.' target="_blank">'.
  4696. Display::return_icon('open_in_new_window.png', get_lang('OpenInANewWindow'), [], ICON_SIZE_SMALL).'&nbsp;&nbsp;</a>';
  4697. }
  4698. if ($filetype == 'file') {
  4699. // Sound preview
  4700. if (preg_match('/mp3$/i', urldecode($checkExtension)) ||
  4701. (preg_match('/wav$/i', urldecode($checkExtension))) ||
  4702. preg_match('/ogg$/i', urldecode($checkExtension))
  4703. ) {
  4704. return '<span style="float:left" '.$visibility_class.'>'.
  4705. $title.
  4706. '</span>'.$force_download_html.$send_to.$copyToMyFiles.$open_in_new_window_link.$pdf_icon;
  4707. } elseif (
  4708. // Show preview
  4709. preg_match('/swf$/i', urldecode($checkExtension)) ||
  4710. preg_match('/png$/i', urldecode($checkExtension)) ||
  4711. preg_match('/gif$/i', urldecode($checkExtension)) ||
  4712. preg_match('/jpg$/i', urldecode($checkExtension)) ||
  4713. preg_match('/jpeg$/i', urldecode($checkExtension)) ||
  4714. preg_match('/bmp$/i', urldecode($checkExtension)) ||
  4715. preg_match('/svg$/i', urldecode($checkExtension))
  4716. ) {
  4717. // Simpler version of showinframesmin.php with no headers
  4718. $url = 'show_content.php?'.$courseParams.'&id='.$document_data['id'];
  4719. $class = 'ajax';
  4720. if ($visibility == false) {
  4721. $class = "ajax text-muted";
  4722. }
  4723. return Display::url(
  4724. $title,
  4725. $url,
  4726. [
  4727. 'class' => $class,
  4728. 'title' => $tooltip_title_alt,
  4729. 'data-title' => $title,
  4730. 'style' => 'float:left;',
  4731. ]
  4732. )
  4733. .$force_download_html.$send_to.$copyToMyFiles
  4734. .$open_in_new_window_link.$pdf_icon;
  4735. } else {
  4736. // For a "PDF Download" of the file.
  4737. $pdfPreview = null;
  4738. if ($ext != 'pdf' && !in_array($ext, $webODFList)) {
  4739. $url = $basePageUrl.'showinframes.php?'.$courseParams.'&id='.$document_data['id'];
  4740. } else {
  4741. $pdfPreview = Display::url(
  4742. Display::return_icon('preview.png', get_lang('Preview'), null, ICON_SIZE_SMALL),
  4743. api_get_path(WEB_CODE_PATH).'document/showinframes.php?'.$courseParams.'&id='.$document_data['id'],
  4744. ['style' => 'float:right']
  4745. );
  4746. }
  4747. // No plugin just the old and good showinframes.php page
  4748. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" style="float:left" '.$visibility_class.' >'.$title.'</a>'.
  4749. $pdfPreview.$force_download_html.$send_to.$copyToMyFiles.$open_in_new_window_link.$pdf_icon;
  4750. }
  4751. } else {
  4752. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.$title.'</a>'.
  4753. $force_download_html.$send_to.$copyToMyFiles.$open_in_new_window_link.$pdf_icon;
  4754. }
  4755. // end copy files to users myfiles
  4756. } else {
  4757. // Icon column
  4758. if (preg_match('/shared_folder/', urldecode($checkExtension)) &&
  4759. preg_match('/shared_folder$/', urldecode($checkExtension)) == false &&
  4760. preg_match('/shared_folder_session_'.$sessionId.'$/', urldecode($url)) == false
  4761. ) {
  4762. if ($filetype == 'file') {
  4763. //Sound preview
  4764. if (preg_match('/mp3$/i', urldecode($checkExtension)) ||
  4765. (preg_match('/wav$/i', urldecode($checkExtension))) ||
  4766. preg_match('/ogg$/i', urldecode($checkExtension))) {
  4767. $soundPreview = self::generateAudioPreview($documentWebPath, $document_data);
  4768. return $soundPreview;
  4769. } elseif (
  4770. // Show preview
  4771. preg_match('/swf$/i', urldecode($checkExtension)) ||
  4772. preg_match('/png$/i', urldecode($checkExtension)) ||
  4773. preg_match('/gif$/i', urldecode($checkExtension)) ||
  4774. preg_match('/jpg$/i', urldecode($checkExtension)) ||
  4775. preg_match('/jpeg$/i', urldecode($checkExtension)) ||
  4776. preg_match('/bmp$/i', urldecode($checkExtension)) ||
  4777. preg_match('/svg$/i', urldecode($checkExtension))
  4778. ) {
  4779. $url = $basePageUrl.'showinframes.php?'.$courseParams.'&id='.$document_data['id'];
  4780. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.
  4781. self::build_document_icon_tag($filetype, $path, $isAllowedToEdit).
  4782. Display::return_icon('shared.png', get_lang('ResourceShared'), []).
  4783. '</a>';
  4784. } else {
  4785. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.
  4786. self::build_document_icon_tag($filetype, $path, $isAllowedToEdit).
  4787. Display::return_icon('shared.png', get_lang('ResourceShared'), []).
  4788. '</a>';
  4789. }
  4790. } else {
  4791. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.
  4792. self::build_document_icon_tag($filetype, $path, $isAllowedToEdit).
  4793. Display::return_icon('shared.png', get_lang('ResourceShared'), []).
  4794. '</a>';
  4795. }
  4796. } else {
  4797. if ($filetype == 'file') {
  4798. // Sound preview with jplayer
  4799. if (preg_match('/mp3$/i', urldecode($checkExtension)) ||
  4800. (preg_match('/wav$/i', urldecode($checkExtension))) ||
  4801. preg_match('/ogg$/i', urldecode($checkExtension))) {
  4802. $soundPreview = self::generateAudioPreview($documentWebPath, $document_data);
  4803. return $soundPreview;
  4804. } elseif (
  4805. //Show preview
  4806. preg_match('/html$/i', urldecode($checkExtension)) ||
  4807. preg_match('/htm$/i', urldecode($checkExtension)) ||
  4808. preg_match('/swf$/i', urldecode($checkExtension)) ||
  4809. preg_match('/png$/i', urldecode($checkExtension)) ||
  4810. preg_match('/gif$/i', urldecode($checkExtension)) ||
  4811. preg_match('/jpg$/i', urldecode($checkExtension)) ||
  4812. preg_match('/jpeg$/i', urldecode($checkExtension)) ||
  4813. preg_match('/bmp$/i', urldecode($checkExtension)) ||
  4814. preg_match('/svg$/i', urldecode($checkExtension))
  4815. ) {
  4816. $url = $basePageUrl.'showinframes.php?'.$courseParams.'&id='.$document_data['id']; //without preview
  4817. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.
  4818. self::build_document_icon_tag($filetype, $path, $isAllowedToEdit).
  4819. '</a>';
  4820. } else {
  4821. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" '.$visibility_class.' style="float:left">'.
  4822. self::build_document_icon_tag($filetype, $path, $isAllowedToEdit).
  4823. '</a>';
  4824. }
  4825. } else {
  4826. return '<a href="'.$url.'" title="'.$tooltip_title_alt.'" target="'.$target.'"'.$visibility_class.' style="float:left">'.
  4827. self::build_document_icon_tag($filetype, $path, $isAllowedToEdit).
  4828. '</a>';
  4829. }
  4830. }
  4831. }
  4832. }
  4833. /**
  4834. * Builds an img html tag for the file type.
  4835. *
  4836. * @param string $type (file/folder)
  4837. * @param string $path
  4838. * @param bool $isAllowedToEdit
  4839. *
  4840. * @return string img html tag
  4841. */
  4842. public static function build_document_icon_tag($type, $path, $isAllowedToEdit = null)
  4843. {
  4844. $basename = basename($path);
  4845. $sessionId = api_get_session_id();
  4846. if (is_null($isAllowedToEdit)) {
  4847. $isAllowedToEdit = api_is_allowed_to_edit(null, true);
  4848. }
  4849. $user_image = false;
  4850. if ($type == 'file') {
  4851. $icon = choose_image($basename);
  4852. $basename = substr(strrchr($basename, '.'), 1);
  4853. } elseif ($type == 'link') {
  4854. $icon = 'clouddoc.png';
  4855. $basename = get_lang('CloudFileLink');
  4856. } else {
  4857. if ($path == '/shared_folder') {
  4858. $icon = 'folder_users.png';
  4859. if ($isAllowedToEdit) {
  4860. $basename = get_lang('HelpUsersFolder');
  4861. } else {
  4862. $basename = get_lang('UserFolders');
  4863. }
  4864. } elseif (strstr($basename, 'sf_user_')) {
  4865. $userInfo = api_get_user_info(substr($basename, 8));
  4866. $icon = $userInfo['avatar_small'];
  4867. $basename = get_lang('UserFolder').' '.$userInfo['complete_name'];
  4868. $user_image = true;
  4869. } elseif (strstr($path, 'shared_folder_session_')) {
  4870. $sessionName = api_get_session_name($sessionId);
  4871. if ($isAllowedToEdit) {
  4872. $basename = '***('.$sessionName.')*** '.get_lang('HelpUsersFolder');
  4873. } else {
  4874. $basename = get_lang('UserFolders').' ('.$sessionName.')';
  4875. }
  4876. $icon = 'folder_users.png';
  4877. } else {
  4878. $icon = 'folder_document.png';
  4879. if ($path == '/audio') {
  4880. $icon = 'folder_audio.png';
  4881. if ($isAllowedToEdit) {
  4882. $basename = get_lang('HelpDefaultDirDocuments');
  4883. } else {
  4884. $basename = get_lang('Audio');
  4885. }
  4886. } elseif ($path == '/flash') {
  4887. $icon = 'folder_flash.png';
  4888. if ($isAllowedToEdit) {
  4889. $basename = get_lang('HelpDefaultDirDocuments');
  4890. } else {
  4891. $basename = get_lang('Flash');
  4892. }
  4893. } elseif ($path == '/images') {
  4894. $icon = 'folder_images.png';
  4895. if ($isAllowedToEdit) {
  4896. $basename = get_lang('HelpDefaultDirDocuments');
  4897. } else {
  4898. $basename = get_lang('Images');
  4899. }
  4900. } elseif ($path == '/video') {
  4901. $icon = 'folder_video.png';
  4902. if ($isAllowedToEdit) {
  4903. $basename = get_lang('HelpDefaultDirDocuments');
  4904. } else {
  4905. $basename = get_lang('Video');
  4906. }
  4907. } elseif ($path == '/images/gallery') {
  4908. $icon = 'folder_gallery.png';
  4909. if ($isAllowedToEdit) {
  4910. $basename = get_lang('HelpDefaultDirDocuments');
  4911. } else {
  4912. $basename = get_lang('Gallery');
  4913. }
  4914. } elseif ($path == '/chat_files') {
  4915. $icon = 'folder_chat.png';
  4916. if ($isAllowedToEdit) {
  4917. $basename = get_lang('HelpFolderChat');
  4918. } else {
  4919. $basename = get_lang('ChatFiles');
  4920. }
  4921. } elseif ($path == '/learning_path') {
  4922. $icon = 'folder_learningpath.png';
  4923. if ($isAllowedToEdit) {
  4924. $basename = get_lang('HelpFolderLearningPaths');
  4925. } else {
  4926. $basename = get_lang('LearningPaths');
  4927. }
  4928. }
  4929. }
  4930. }
  4931. if ($user_image) {
  4932. return Display::img($icon, $basename, [], false);
  4933. }
  4934. return Display::return_icon($icon, $basename, [], ICON_SIZE_SMALL);
  4935. }
  4936. /**
  4937. * Creates the row of edit icons for a file/folder.
  4938. *
  4939. * @param array $document_data
  4940. * @param int $id
  4941. * @param bool $is_template
  4942. * @param int $is_read_only
  4943. * @param int $visibility (1/0)
  4944. *
  4945. * @return string html img tags with hyperlinks
  4946. */
  4947. public static function build_edit_icons($document_data, $id, $is_template, $is_read_only = 0, $visibility)
  4948. {
  4949. $sessionId = api_get_session_id();
  4950. $courseParams = api_get_cidreq();
  4951. $document_id = $document_data['id'];
  4952. $type = $document_data['filetype'];
  4953. $is_read_only = $document_data['readonly'];
  4954. $path = $document_data['path'];
  4955. if ($type == 'link') {
  4956. $parent_id = self::get_document_id(
  4957. api_get_course_info(),
  4958. rtrim($path, '/'),
  4959. 0
  4960. );
  4961. } else {
  4962. $parent_id = self::get_document_id(
  4963. api_get_course_info(),
  4964. dirname($path),
  4965. 0
  4966. );
  4967. }
  4968. if (empty($parent_id) && !empty($sessionId)) {
  4969. $parent_id = self::get_document_id(
  4970. api_get_course_info(),
  4971. dirname($path),
  4972. $sessionId
  4973. );
  4974. }
  4975. $curdirpath = dirname($document_data['path']);
  4976. $is_certificate_mode = self::is_certificate_mode($path);
  4977. $curdirpath = urlencode($curdirpath);
  4978. $extension = pathinfo($path, PATHINFO_EXTENSION);
  4979. //@todo Implement remote support for converter
  4980. $usePpt2lp = api_get_setting('service_ppt2lp', 'active') == 'true' && api_get_setting('service_ppt2lp', 'host') == 'localhost';
  4981. $formatTypeList = self::getFormatTypeListConvertor('from', $extension);
  4982. $formatType = current($formatTypeList);
  4983. // If document is read only *or* we're in a session and the document
  4984. // is from a non-session context, hide the edition capabilities
  4985. $modify_icons = [];
  4986. $modify_icons[] = self::getButtonEdit($is_read_only, $document_data, $extension, $is_certificate_mode);
  4987. $modify_icons[] = self::getButtonMove($is_read_only, $document_data, $is_certificate_mode, $parent_id);
  4988. $modify_icons[] = self::getButtonVisibility(
  4989. $is_read_only,
  4990. $visibility,
  4991. $document_data,
  4992. $is_certificate_mode,
  4993. $parent_id
  4994. );
  4995. $modify_icons[] = self::getButtonDelete(
  4996. $is_read_only,
  4997. $document_data,
  4998. $is_certificate_mode,
  4999. $curdirpath,
  5000. $parent_id
  5001. );
  5002. if (!$is_read_only /* or ($session_id!=api_get_session_id()) */) {
  5003. // Add action to covert to PDF, will create a new document whit same filename but .pdf extension
  5004. // @TODO: add prompt to select a format target
  5005. if (!in_array($path, self::get_system_folders())) {
  5006. if ($usePpt2lp && $formatType) {
  5007. $modify_icons[] = Display::url(
  5008. Display::return_icon('convert.png', get_lang('Convert')),
  5009. '#',
  5010. ['class' => 'convertAction', 'data-documentId' => $document_id, 'data-formatType' => $formatType]
  5011. );
  5012. }
  5013. }
  5014. }
  5015. if ($type == 'file' && ($extension == 'html' || $extension == 'htm')) {
  5016. if ($is_template == 0) {
  5017. if ((isset($_GET['curdirpath']) && $_GET['curdirpath'] != '/certificates') || !isset($_GET['curdirpath'])) {
  5018. $modify_icons[] = Display::url(
  5019. Display::return_icon('wizard.png', get_lang('AddAsTemplate')),
  5020. api_get_self()."?$courseParams&curdirpath=$curdirpath&add_as_template=$id"
  5021. );
  5022. }
  5023. if ((isset($_GET['curdirpath']) && $_GET['curdirpath'] == '/certificates') || $is_certificate_mode) {//allow attach certificate to course
  5024. $visibility_icon_certificate = 'nocertificate';
  5025. if (self::get_default_certificate_id(api_get_course_id()) == $id) {
  5026. $visibility_icon_certificate = 'certificate';
  5027. $certificate = get_lang('DefaultCertificate');
  5028. $preview = get_lang('PreviewCertificate');
  5029. $is_preview = true;
  5030. } else {
  5031. $is_preview = false;
  5032. $certificate = get_lang('NoDefaultCertificate');
  5033. }
  5034. if (isset($_GET['selectcat'])) {
  5035. $modify_icons[] = Display::url(
  5036. Display::return_icon($visibility_icon_certificate.'.png', $certificate),
  5037. api_get_self()."?$courseParams&curdirpath=$curdirpath&selectcat=".intval($_GET['selectcat'])."&set_certificate=$id"
  5038. );
  5039. if ($is_preview) {
  5040. $modify_icons[] = Display::url(
  5041. Display::return_icon('preview_view.png', $preview),
  5042. api_get_self()."?$courseParams&curdirpath=$curdirpath&set_preview=$id"
  5043. );
  5044. }
  5045. }
  5046. }
  5047. } else {
  5048. $modify_icons[] = Display::url(
  5049. Display::return_icon('wizard_na.png', get_lang('RemoveAsTemplate')),
  5050. api_get_self()."?$courseParams&curdirpath=$curdirpath&remove_as_template=$id"
  5051. );
  5052. }
  5053. $modify_icons[] = Display::url(
  5054. Display::return_icon('pdf.png', get_lang('Export2PDF')),
  5055. api_get_self()."?$courseParams&action=export_to_pdf&id=$id&curdirpath=$curdirpath"
  5056. );
  5057. }
  5058. return implode(PHP_EOL, $modify_icons);
  5059. }
  5060. /**
  5061. * @param $folders
  5062. * @param $curdirpath
  5063. * @param $move_file
  5064. * @param string $group_dir
  5065. *
  5066. * @return string
  5067. */
  5068. public static function build_move_to_selector($folders, $curdirpath, $move_file, $group_dir = '')
  5069. {
  5070. $form = new FormValidator('move_to', 'post', api_get_self().'?'.api_get_cidreq());
  5071. // Form title
  5072. $form->addHidden('move_file', $move_file);
  5073. $options = [];
  5074. // Group documents cannot be uploaded in the root
  5075. if ($group_dir == '') {
  5076. if ($curdirpath != '/') {
  5077. $options['/'] = get_lang('Documents');
  5078. }
  5079. if (is_array($folders)) {
  5080. foreach ($folders as &$folder) {
  5081. // Hide some folders
  5082. if ($folder == '/HotPotatoes_files' ||
  5083. $folder == '/certificates' ||
  5084. basename($folder) == 'css'
  5085. ) {
  5086. continue;
  5087. }
  5088. // Admin setting for Hide/Show the folders of all users
  5089. if (api_get_setting('show_users_folders') == 'false' &&
  5090. (strstr($folder, '/shared_folder') || strstr($folder, 'shared_folder_session_'))
  5091. ) {
  5092. continue;
  5093. }
  5094. // Admin setting for Hide/Show Default folders to all users
  5095. if (api_get_setting('show_default_folders') == 'false' &&
  5096. (
  5097. $folder == '/images' ||
  5098. $folder == '/flash' ||
  5099. $folder == '/audio' ||
  5100. $folder == '/video' ||
  5101. strstr($folder, '/images/gallery') ||
  5102. $folder == '/video/flv'
  5103. )
  5104. ) {
  5105. continue;
  5106. }
  5107. // Admin setting for Hide/Show chat history folder
  5108. if (api_get_setting('show_chat_folder') == 'false' &&
  5109. $folder == '/chat_files') {
  5110. continue;
  5111. }
  5112. // You cannot move a file to:
  5113. // 1. current directory
  5114. // 2. inside the folder you want to move
  5115. // 3. inside a subfolder of the folder you want to move
  5116. if (($curdirpath != $folder) &&
  5117. ($folder != $move_file) &&
  5118. (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')
  5119. ) {
  5120. // If document title is used, we have to display titles instead of real paths...
  5121. $path_displayed = self::get_titles_of_path($folder);
  5122. if (empty($path_displayed)) {
  5123. $path_displayed = get_lang('Untitled');
  5124. }
  5125. $options[$folder] = $path_displayed;
  5126. }
  5127. }
  5128. }
  5129. } else {
  5130. foreach ($folders as $folder) {
  5131. if (($curdirpath != $folder) &&
  5132. ($folder != $move_file) &&
  5133. (substr($folder, 0, strlen($move_file) + 1) != $move_file.'/')
  5134. ) {
  5135. // Cannot copy dir into his own subdir
  5136. $path_displayed = self::get_titles_of_path($folder);
  5137. $display_folder = substr($path_displayed, strlen($group_dir));
  5138. $display_folder = $display_folder == '' ? get_lang('Documents') : $display_folder;
  5139. $options[$folder] = $display_folder;
  5140. }
  5141. }
  5142. }
  5143. $form->addElement('select', 'move_to', get_lang('MoveTo'), $options);
  5144. $form->addButtonNext(get_lang('MoveElement'), 'move_file_submit');
  5145. return $form->returnForm();
  5146. }
  5147. /**
  5148. * Gets the path translated with title of docs and folders.
  5149. *
  5150. * @param string $path the real path
  5151. *
  5152. * @return the path which should be displayed
  5153. */
  5154. public static function get_titles_of_path($path)
  5155. {
  5156. global $tmp_folders_titles;
  5157. $course_id = api_get_course_int_id();
  5158. $nb_slashes = substr_count($path, '/');
  5159. $current_slash_pos = 0;
  5160. $path_displayed = '';
  5161. for ($i = 0; $i < $nb_slashes; $i++) {
  5162. // For each folder of the path, retrieve title.
  5163. $current_slash_pos = strpos($path, '/', $current_slash_pos + 1);
  5164. $tmp_path = substr($path, strpos($path, '/', 0), $current_slash_pos);
  5165. if (empty($tmp_path)) {
  5166. // If empty, then we are in the final part of the path
  5167. $tmp_path = $path;
  5168. }
  5169. if (!empty($tmp_folders_titles[$tmp_path])) {
  5170. // If this path has soon been stored here we don't need a new query
  5171. $path_displayed .= $tmp_folders_titles[$tmp_path];
  5172. } else {
  5173. $sql = 'SELECT title FROM '.Database::get_course_table(TABLE_DOCUMENT).'
  5174. WHERE c_id = '.$course_id.' AND path LIKE BINARY "'.$tmp_path.'"';
  5175. $rs = Database::query($sql);
  5176. $tmp_title = '/'.Database::result($rs, 0, 0);
  5177. $path_displayed .= $tmp_title;
  5178. $tmp_folders_titles[$tmp_path] = $tmp_title;
  5179. }
  5180. }
  5181. return $path_displayed;
  5182. }
  5183. /**
  5184. * Creates form that asks for the directory name.
  5185. *
  5186. * @return string html-output text for the form
  5187. */
  5188. public static function create_dir_form($dirId)
  5189. {
  5190. global $document_id;
  5191. $form = new FormValidator('create_dir_form', 'post', api_get_self().'?'.api_get_cidreq());
  5192. $form->addElement('hidden', 'create_dir', 1);
  5193. $form->addElement('hidden', 'dir_id', intval($document_id));
  5194. $form->addElement('hidden', 'id', intval($dirId));
  5195. $form->addElement('header', get_lang('CreateDir'));
  5196. $form->addText('dirname', get_lang('NewDir'), ['autofocus' => 'autofocus']);
  5197. $form->addButtonCreate(get_lang('CreateFolder'));
  5198. return $form->returnForm();
  5199. }
  5200. /**
  5201. * Checks whether the user is in shared folder.
  5202. *
  5203. * @param string $curdirpath
  5204. * @param int $sessionId
  5205. *
  5206. * @return bool Return true when user is into shared folder
  5207. */
  5208. public static function is_shared_folder($curdirpath, $sessionId)
  5209. {
  5210. $clean_curdirpath = Security::remove_XSS($curdirpath);
  5211. if ($clean_curdirpath == '/shared_folder') {
  5212. return true;
  5213. } elseif ($clean_curdirpath == '/shared_folder_session_'.$sessionId) {
  5214. return true;
  5215. } else {
  5216. return false;
  5217. }
  5218. }
  5219. /**
  5220. * Checks whether the user is into any user shared folder.
  5221. *
  5222. * @param string $path
  5223. * @param int $sessionId
  5224. *
  5225. * @return bool Return true when user is in any user shared folder
  5226. */
  5227. public static function is_any_user_shared_folder($path, $sessionId)
  5228. {
  5229. $clean_path = Security::remove_XSS($path);
  5230. if (strpos($clean_path, 'shared_folder/sf_user_')) {
  5231. return true;
  5232. } elseif (strpos($clean_path, 'shared_folder_session_'.$sessionId.'/sf_user_')) {
  5233. return true;
  5234. } else {
  5235. return false;
  5236. }
  5237. }
  5238. /**
  5239. * Create users shared folder for course.
  5240. *
  5241. * @param int $userId
  5242. * @param array $courseInfo
  5243. * @param int $sessionId
  5244. */
  5245. public static function createUserSharedFolder($userId, array $courseInfo, $sessionId = 0)
  5246. {
  5247. $documentDirectory = api_get_path(SYS_COURSE_PATH).$courseInfo['directory'].'/document';
  5248. $userInfo = api_get_user_info($userId);
  5249. if (!$sessionId) {
  5250. //Create shared folder. Necessary for recycled courses.
  5251. if (!file_exists($documentDirectory.'/shared_folder')) {
  5252. create_unexisting_directory(
  5253. $courseInfo,
  5254. $userId,
  5255. 0,
  5256. 0,
  5257. 0,
  5258. $documentDirectory,
  5259. '/shared_folder',
  5260. get_lang('UserFolders'),
  5261. 0,
  5262. false,
  5263. false
  5264. );
  5265. }
  5266. // Create dynamic user shared folder
  5267. if (!file_exists($documentDirectory.'/shared_folder/sf_user_'.$userId)) {
  5268. create_unexisting_directory(
  5269. $courseInfo,
  5270. $userId,
  5271. 0,
  5272. 0,
  5273. 0,
  5274. $documentDirectory,
  5275. '/shared_folder/sf_user_'.$userId,
  5276. $userInfo['complete_name'],
  5277. 1,
  5278. false,
  5279. false
  5280. );
  5281. }
  5282. return;
  5283. }
  5284. // Create shared folder session.
  5285. if (!file_exists($documentDirectory.'/shared_folder_session_'.$sessionId)) {
  5286. create_unexisting_directory(
  5287. $courseInfo,
  5288. api_get_user_id(),
  5289. $sessionId,
  5290. 0,
  5291. 0,
  5292. $documentDirectory,
  5293. '/shared_folder_session_'.$sessionId,
  5294. get_lang('UserFolders').' ('.api_get_session_name($sessionId).')',
  5295. 0,
  5296. false,
  5297. false
  5298. );
  5299. }
  5300. //Create dynamic user shared folder into a shared folder session
  5301. if (!file_exists($documentDirectory.'/shared_folder_session_'.$sessionId.'/sf_user_'.$userId)) {
  5302. create_unexisting_directory(
  5303. $courseInfo,
  5304. $userId,
  5305. $sessionId,
  5306. 0,
  5307. 0,
  5308. $documentDirectory,
  5309. '/shared_folder_session_'.$sessionId.'/sf_user_'.$userId,
  5310. $userInfo['complete_name'].'('.api_get_session_name($sessionId).')',
  5311. 1,
  5312. false,
  5313. false
  5314. );
  5315. }
  5316. }
  5317. /**
  5318. * Checks whether the user is into his shared folder or into a subfolder.
  5319. *
  5320. * @param int $user_id
  5321. * @param string $path
  5322. * @param int $sessionId
  5323. *
  5324. * @return bool Return true when user is in his user shared folder or into a subfolder
  5325. */
  5326. public static function is_my_shared_folder($user_id, $path, $sessionId)
  5327. {
  5328. $clean_path = Security::remove_XSS($path).'/';
  5329. //for security does not remove the last slash
  5330. $main_user_shared_folder = '/shared_folder\/sf_user_'.$user_id.'\//';
  5331. //for security does not remove the last slash
  5332. $main_user_shared_folder_session = '/shared_folder_session_'.$sessionId.'\/sf_user_'.$user_id.'\//';
  5333. if (preg_match($main_user_shared_folder, $clean_path)) {
  5334. return true;
  5335. } elseif (preg_match($main_user_shared_folder_session, $clean_path)) {
  5336. return true;
  5337. } else {
  5338. return false;
  5339. }
  5340. }
  5341. public static function isBasicCourseFolder($path, $sessionId)
  5342. {
  5343. $cleanPath = Security::remove_XSS($path);
  5344. $basicCourseFolder = '/basic-course-documents__'.$sessionId.'__0';
  5345. return $cleanPath == $basicCourseFolder;
  5346. }
  5347. /**
  5348. * Check if the file name or folder searched exist.
  5349. *
  5350. * @return bool Return true when exist
  5351. */
  5352. public static function search_keyword($document_name, $keyword)
  5353. {
  5354. if (api_strripos($document_name, $keyword) !== false) {
  5355. return true;
  5356. } else {
  5357. return false;
  5358. }
  5359. }
  5360. /**
  5361. * Checks whether a document can be previewed by using the browser.
  5362. *
  5363. * @param string $file_extension the filename extension of the document (it must be in lower case)
  5364. *
  5365. * @return bool returns TRUE or FALSE
  5366. */
  5367. public static function isBrowserViewable($file_extension)
  5368. {
  5369. static $allowed_extensions = [
  5370. 'htm', 'html', 'xhtml',
  5371. 'gif', 'jpg', 'jpeg', 'png', 'tif', 'tiff',
  5372. 'pdf', 'svg', 'swf',
  5373. 'txt', 'log',
  5374. 'mp4', 'ogg', 'ogv', 'ogx', 'mpg', 'mpeg', 'mov', 'avi', 'webm', 'wmv',
  5375. 'mp3', 'oga', 'wav', 'au', 'wma', 'mid', 'kar',
  5376. ];
  5377. /*
  5378. //TODO: make a admin switch to strict mode
  5379. 1. global default $allowed_extensions
  5380. if (in_array($file_extension, $allowed_extensions)) { // Assignment + a logical check.
  5381. return true;
  5382. }
  5383. 2. check native support
  5384. 3. check plugins: quicktime, mediaplayer, vlc, acrobat, flash, java
  5385. */
  5386. if (!($result = in_array($file_extension, $allowed_extensions))) {
  5387. // Assignment + a logical check.
  5388. return false;
  5389. }
  5390. //check native support (Explorer, Opera, Firefox, Chrome, Safari)
  5391. if ($file_extension == "pdf") {
  5392. return api_browser_support('pdf');
  5393. } elseif ($file_extension == "mp3") {
  5394. return api_browser_support('mp3');
  5395. } elseif ($file_extension == "mp4") {
  5396. return api_browser_support('mp4');
  5397. } elseif ($file_extension == "ogg" || $file_extension == "ogx" || $file_extension == "ogv" || $file_extension == "oga") {
  5398. return api_browser_support('ogg');
  5399. } elseif ($file_extension == "svg") {
  5400. return api_browser_support('svg');
  5401. } elseif ($file_extension == "mpg" || $file_extension == "mpeg") {
  5402. return api_browser_support('mpg');
  5403. } elseif ($file_extension == "mov") {
  5404. return api_browser_support('mov');
  5405. } elseif ($file_extension == "wav") {
  5406. return api_browser_support('wav');
  5407. } elseif ($file_extension == "mid" || $file_extension == "kar") {
  5408. return api_browser_support('mid');
  5409. } elseif ($file_extension == "avi") {
  5410. return api_browser_support('avi');
  5411. } elseif ($file_extension == "wma") {
  5412. return api_browser_support('wma');
  5413. } elseif ($file_extension == "wmv") {
  5414. return api_browser_support('wmv');
  5415. } elseif ($file_extension == "tif" || $file_extension == "tiff") {
  5416. return api_browser_support('tif');
  5417. } elseif ($file_extension == "mov") {
  5418. return api_browser_support('mov');
  5419. } elseif ($file_extension == "au") {
  5420. return api_browser_support('au');
  5421. } elseif ($file_extension == "webm") {
  5422. return api_browser_support('webm');
  5423. }
  5424. return $result;
  5425. }
  5426. /**
  5427. * @param array $courseInfo
  5428. * @param int $sessionId
  5429. *
  5430. * @return array
  5431. */
  5432. public static function getDeletedDocuments($courseInfo, $sessionId = 0)
  5433. {
  5434. $table = Database::get_course_table(TABLE_DOCUMENT);
  5435. $courseId = $courseInfo['real_id'];
  5436. $sessionCondition = api_get_session_condition($sessionId);
  5437. $sql = "SELECT * FROM $table
  5438. WHERE
  5439. path LIKE '%DELETED%' AND
  5440. c_id = $courseId
  5441. $sessionCondition
  5442. ORDER BY path
  5443. ";
  5444. $result = Database::query($sql);
  5445. $files = [];
  5446. while ($document = Database::fetch_array($result, 'ASSOC')) {
  5447. $files[] = $document;
  5448. }
  5449. return $files;
  5450. }
  5451. /**
  5452. * @param int $id
  5453. * @param array $courseInfo
  5454. * @param int $sessionId
  5455. *
  5456. * @return array
  5457. */
  5458. public static function getDeletedDocument($id, $courseInfo, $sessionId = 0)
  5459. {
  5460. if (empty($courseInfo)) {
  5461. return false;
  5462. }
  5463. $table = Database::get_course_table(TABLE_DOCUMENT);
  5464. $courseId = $courseInfo['real_id'];
  5465. $sessionCondition = api_get_session_condition($sessionId);
  5466. $sql = "SELECT * FROM $table
  5467. WHERE
  5468. path LIKE '%DELETED%' AND
  5469. id = $id AND
  5470. c_id = $courseId
  5471. $sessionCondition
  5472. LIMIT 1
  5473. ";
  5474. $result = Database::query($sql);
  5475. if (Database::num_rows($result)) {
  5476. $result = Database::fetch_array($result, 'ASSOC');
  5477. return $result;
  5478. }
  5479. return [];
  5480. }
  5481. /**
  5482. * @param int $id
  5483. * @param array $courseInfo
  5484. * @param int $sessionId
  5485. *
  5486. * @return bool
  5487. */
  5488. public static function purgeDocument($id, $courseInfo, $sessionId = 0)
  5489. {
  5490. $document = self::getDeletedDocument($id, $courseInfo, $sessionId);
  5491. if (!empty($document)) {
  5492. $path = $document['path'];
  5493. $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/';
  5494. my_delete($coursePath.$path);
  5495. // Hard delete.
  5496. self::deleteDocumentFromDb($id, $courseInfo, $sessionId, true);
  5497. return true;
  5498. }
  5499. return false;
  5500. }
  5501. /**
  5502. * @param array $courseInfo
  5503. * @param int $sessionId
  5504. */
  5505. public static function purgeDocuments($courseInfo, $sessionId)
  5506. {
  5507. $files = self::getDeletedDocuments($courseInfo, $sessionId);
  5508. foreach ($files as $file) {
  5509. self::purgeDocument($file['id'], $courseInfo, $sessionId);
  5510. }
  5511. }
  5512. /**
  5513. * @param int $id
  5514. * @param array $courseInfo
  5515. * @param int $sessionId
  5516. */
  5517. public static function downloadDeletedDocument($id, $courseInfo, $sessionId)
  5518. {
  5519. $document = self::getDeletedDocument($id, $courseInfo, $sessionId);
  5520. if (!empty($document)) {
  5521. $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document/';
  5522. if (Security::check_abs_path($coursePath.$document['path'], $coursePath)) {
  5523. self::file_send_for_download($coursePath.$document['path']);
  5524. exit;
  5525. }
  5526. }
  5527. }
  5528. /**
  5529. * @param array $courseInfo
  5530. * @param int $sessionId
  5531. *
  5532. * @return bool
  5533. */
  5534. public static function downloadAllDeletedDocument($courseInfo, $sessionId)
  5535. {
  5536. $files = self::getDeletedDocuments($courseInfo, $sessionId);
  5537. if (empty($files)) {
  5538. return false;
  5539. }
  5540. $coursePath = api_get_path(SYS_COURSE_PATH).$courseInfo['path'].'/document';
  5541. // Creating a ZIP file.
  5542. $tempZipFile = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  5543. $zip = new PclZip($tempZipFile);
  5544. foreach ($files as $file) {
  5545. $zip->add(
  5546. $coursePath.$file['path'],
  5547. PCLZIP_OPT_REMOVE_PATH,
  5548. $coursePath
  5549. );
  5550. }
  5551. if (Security::check_abs_path($tempZipFile, api_get_path(SYS_ARCHIVE_PATH))) {
  5552. self::file_send_for_download($tempZipFile, true);
  5553. @unlink($tempZipFile);
  5554. exit;
  5555. }
  5556. }
  5557. /**
  5558. * Delete documents from a session in a course.
  5559. *
  5560. * @param array $courseInfo
  5561. * @param int $sessionId
  5562. *
  5563. * @return bool
  5564. */
  5565. public static function deleteDocumentsFromSession($courseInfo, $sessionId)
  5566. {
  5567. if (empty($courseInfo)) {
  5568. return false;
  5569. }
  5570. if (empty($sessionId)) {
  5571. return false;
  5572. }
  5573. $itemPropertyTable = Database::get_course_table(TABLE_ITEM_PROPERTY);
  5574. $documentTable = Database::get_course_table(TABLE_DOCUMENT);
  5575. $conditionSession = api_get_session_condition($sessionId, true, false, 'd.session_id');
  5576. $courseId = $courseInfo['real_id'];
  5577. // get invisible folders
  5578. $sql = "SELECT DISTINCT d.id, path
  5579. FROM $itemPropertyTable i
  5580. INNER JOIN $documentTable d
  5581. ON (i.c_id = d.c_id)
  5582. WHERE
  5583. d.id = i.ref AND
  5584. i.tool = '".TOOL_DOCUMENT."'
  5585. $conditionSession AND
  5586. i.c_id = $courseId AND
  5587. d.c_id = $courseId ";
  5588. $result = Database::query($sql);
  5589. $documents = Database::store_result($result, 'ASSOC');
  5590. if ($documents) {
  5591. $course_dir = $courseInfo['directory'].'/document';
  5592. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  5593. $base_work_dir = $sys_course_path.$course_dir;
  5594. foreach ($documents as $document) {
  5595. $documentId = $document['id'];
  5596. self::delete_document(
  5597. $courseInfo,
  5598. null,
  5599. $base_work_dir,
  5600. $sessionId,
  5601. $documentId
  5602. );
  5603. }
  5604. }
  5605. $sql = "DELETE FROM $documentTable
  5606. WHERE c_id = $courseId AND session_id = $sessionId";
  5607. Database::query($sql);
  5608. $sql = "DELETE FROM $itemPropertyTable
  5609. WHERE c_id = $courseId AND session_id = $sessionId AND tool = '".TOOL_DOCUMENT."'";
  5610. Database::query($sql);
  5611. }
  5612. /**
  5613. * Update the file or directory path in the document db document table.
  5614. *
  5615. * @author - Hugues Peeters <peeters@ipm.ucl.ac.be>
  5616. *
  5617. * @param string $action - action type require : 'delete' or 'update'
  5618. * @param string $old_path - old path info stored to change
  5619. * @param string $new_path - new path info to substitute
  5620. *
  5621. * @desc Update the file or directory path in the document db document table
  5622. */
  5623. public static function updateDbInfo($action, $old_path, $new_path = '')
  5624. {
  5625. $dbTable = Database::get_course_table(TABLE_DOCUMENT);
  5626. $course_id = api_get_course_int_id();
  5627. $old_path = Database::escape_string($old_path);
  5628. switch ($action) {
  5629. case 'delete':
  5630. $query = "DELETE FROM $dbTable
  5631. WHERE
  5632. c_id = $course_id AND
  5633. (
  5634. path LIKE BINARY '".$old_path."' OR
  5635. path LIKE BINARY '".$old_path."/%'
  5636. )";
  5637. Database::query($query);
  5638. break;
  5639. case 'update':
  5640. if ($new_path[0] == '.') {
  5641. $new_path = substr($new_path, 1);
  5642. }
  5643. $new_path = str_replace('//', '/', $new_path);
  5644. // Attempt to update - tested & working for root dir
  5645. $new_path = Database::escape_string($new_path);
  5646. $query = "UPDATE $dbTable SET
  5647. path = CONCAT('".$new_path."', SUBSTRING(path, LENGTH('".$old_path."')+1) )
  5648. WHERE
  5649. c_id = $course_id AND
  5650. (path LIKE BINARY '".$old_path."' OR path LIKE BINARY '".$old_path."/%')";
  5651. Database::query($query);
  5652. break;
  5653. }
  5654. }
  5655. /**
  5656. * This function calculates the resized width and resized heigt
  5657. * according to the source and target widths
  5658. * and heights, height so that no distortions occur
  5659. * parameters.
  5660. *
  5661. * @param $image = the absolute path to the image
  5662. * @param $target_width = how large do you want your resized image
  5663. * @param $target_height = how large do you want your resized image
  5664. * @param $slideshow (default=0) =
  5665. * indicates weither we are generating images for a slideshow or not,
  5666. * this overrides the $_SESSION["image_resizing"] a bit so that a thumbnail
  5667. * view is also possible when you choose not to resize the source images
  5668. *
  5669. * @return array
  5670. */
  5671. public static function resizeImageSlideShow(
  5672. $image,
  5673. $target_width,
  5674. $target_height,
  5675. $slideshow = 0
  5676. ) {
  5677. // Modifications by Ivan Tcholakov, 04-MAY-2009.
  5678. $result = [];
  5679. $imageResize = Session::read('image_resizing');
  5680. if ($imageResize == 'resizing' || $slideshow == 1) {
  5681. $new_sizes = api_resize_image($image, $target_width, $target_height);
  5682. $result[] = $new_sizes['height'];
  5683. $result[] = $new_sizes['width'];
  5684. } else {
  5685. $size = api_getimagesize($image);
  5686. $result[] = $size['height'];
  5687. $result[] = $size['width'];
  5688. }
  5689. return $result;
  5690. }
  5691. /**
  5692. * Calculates the total size of a directory by adding the sizes (that
  5693. * are stored in the database) of all files & folders in this directory.
  5694. *
  5695. * @param string $path
  5696. * @param bool $can_see_invisible
  5697. *
  5698. * @return int Total size
  5699. */
  5700. public static function getTotalFolderSize($path, $can_see_invisible = false)
  5701. {
  5702. $table_itemproperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  5703. $table_document = Database::get_course_table(TABLE_DOCUMENT);
  5704. $tool_document = TOOL_DOCUMENT;
  5705. $course_id = api_get_course_int_id();
  5706. $session_id = api_get_session_id();
  5707. $session_condition = api_get_session_condition(
  5708. $session_id,
  5709. true,
  5710. true,
  5711. 'props.session_id'
  5712. );
  5713. if (empty($course_id)) {
  5714. return 0;
  5715. }
  5716. $path = Database::escape_string($path);
  5717. $visibility_rule = ' props.visibility '.($can_see_invisible ? '<> 2' : '= 1');
  5718. $sql = "SELECT SUM(table1.size) FROM (
  5719. SELECT props.ref, size
  5720. FROM $table_itemproperty AS props
  5721. INNER JOIN $table_document AS docs
  5722. ON (docs.id = props.ref AND docs.c_id = props.c_id)
  5723. WHERE
  5724. docs.c_id = $course_id AND
  5725. docs.path LIKE '$path/%' AND
  5726. props.c_id = $course_id AND
  5727. props.tool = '$tool_document' AND
  5728. $visibility_rule
  5729. $session_condition
  5730. GROUP BY ref
  5731. ) as table1";
  5732. $result = Database::query($sql);
  5733. if ($result && Database::num_rows($result) != 0) {
  5734. $row = Database::fetch_row($result);
  5735. return $row[0] == null ? 0 : $row[0];
  5736. } else {
  5737. return 0;
  5738. }
  5739. }
  5740. /**
  5741. * Adds a cloud link to the database.
  5742. *
  5743. * @author - Aquilino Blanco Cores <aqblanco@gmail.com>
  5744. *
  5745. * @param array $_course
  5746. * @param string $path
  5747. * @param string $url
  5748. * @param string $name
  5749. *
  5750. * @return int id of document or 0 if already exists or there was a problem creating it
  5751. */
  5752. public static function addCloudLink($_course, $path, $url, $name)
  5753. {
  5754. $file_path = $path;
  5755. if (!self::cloudLinkExists($_course, $path, $url)) {
  5756. $doc_id = add_document($_course, $file_path, 'link', 0, $name, $url);
  5757. if ($doc_id) {
  5758. // Update document item_property
  5759. api_item_property_update(
  5760. $_course,
  5761. TOOL_DOCUMENT,
  5762. $doc_id,
  5763. 'DocumentAdded',
  5764. api_get_user_id(),
  5765. api_get_group_id(),
  5766. api_get_user_id(),
  5767. null,
  5768. null,
  5769. api_get_session_id()
  5770. );
  5771. }
  5772. // If the file is in a folder, we need to update all parent folders
  5773. item_property_update_on_folder($_course, $file_path, api_get_user_id());
  5774. return $doc_id;
  5775. } else {
  5776. return 0;
  5777. }
  5778. }
  5779. /**
  5780. * Deletes a cloud link from the database.
  5781. *
  5782. * @author - Aquilino Blanco Cores <aqblanco@gmail.com>
  5783. *
  5784. * @param array $courseInfo
  5785. * @param string $documentId
  5786. *
  5787. * @return bool true if success / false if an error occurred
  5788. */
  5789. public static function deleteCloudLink($courseInfo, $documentId)
  5790. {
  5791. if (empty($documentId) || empty($courseInfo)) {
  5792. return false;
  5793. }
  5794. $documentId = (int) $documentId;
  5795. $fileDeletedFromDb = false;
  5796. if (!empty($documentId)) {
  5797. self::deleteDocumentFromDb($documentId, $courseInfo, 0, true);
  5798. // checking
  5799. $table = Database::get_course_table(TABLE_DOCUMENT);
  5800. $courseId = $courseInfo['real_id'];
  5801. echo $sql = "SELECT * FROM $table WHERE id = $documentId AND c_id = $courseId";
  5802. $result = Database::query($sql);
  5803. $exists = Database::num_rows($result) > 0;
  5804. $fileDeletedFromDb = !$exists;
  5805. }
  5806. return $fileDeletedFromDb;
  5807. }
  5808. /**
  5809. * Gets the id of a cloud link with a given path.
  5810. *
  5811. * @author - Aquilino Blanco Cores <aqblanco@gmail.com>
  5812. *
  5813. * @param array $courseInfo
  5814. * @param string $path
  5815. * @param string $url
  5816. *
  5817. * @return int link's id / false if no link found
  5818. */
  5819. public static function getCloudLinkId($courseInfo, $path, $url)
  5820. {
  5821. $table = Database::get_course_table(TABLE_DOCUMENT);
  5822. if (empty($courseInfo)) {
  5823. return false;
  5824. }
  5825. $courseId = (int) $courseInfo['real_id'];
  5826. $path = Database::escape_string($path);
  5827. if (substr($path, -1) != '/') {
  5828. // Add final slash to path if not present
  5829. $path .= '/';
  5830. }
  5831. if (!empty($courseId) && !empty($path)) {
  5832. $sql = "SELECT id FROM $table
  5833. WHERE
  5834. c_id = $courseId AND
  5835. path LIKE BINARY '$path' AND
  5836. comment = '$url' AND
  5837. filetype = 'link'
  5838. LIMIT 1";
  5839. $result = Database::query($sql);
  5840. if ($result && Database::num_rows($result)) {
  5841. $row = Database::fetch_array($result);
  5842. return intval($row[0]);
  5843. }
  5844. }
  5845. return false;
  5846. }
  5847. /**
  5848. * Checks if a cloud link exists.
  5849. *
  5850. * @author - Aquilino Blanco Cores <aqblanco@gmail.com>
  5851. *
  5852. * @param array $courseInfo
  5853. * @param string $path
  5854. * @param string $url
  5855. *
  5856. * @return bool true if it exists false in other case
  5857. */
  5858. public static function cloudLinkExists($courseInfo, $path, $url)
  5859. {
  5860. $exists = self::getCloudLinkId($courseInfo, $path, $url);
  5861. return $exists;
  5862. }
  5863. /**
  5864. * Gets the wellformed URLs regular expression in order to use it on forms' verifications.
  5865. *
  5866. * @author Aquilino Blanco Cores <aqblanco@gmail.com>
  5867. *
  5868. * @return string the well formed URLs regular expressions string
  5869. */
  5870. public static function getWellFormedUrlRegex()
  5871. {
  5872. return '/\(?((http|https|ftp):\/\/)(?:((?:[^\W\s]|\.|-|[:]{1})+)@{1})?((?:www.)?(?:[^\W\s]|\.|-)+[\.][^\W\s]{2,4}|localhost(?=\/)|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::(\d*))?([\/]?[^\s\?]*[\/]{1})*(?:\/?([^\s\n\?\[\]\{\}\#]*(?:(?=\.)){1}|[^\s\n\?\[\]\{\}\.\#]*)?([\.]{1}[^\s\?\#]*)?)?(?:\?{1}([^\s\n\#\[\]]*))?([\#][^\s\n]*)?\)?/i';
  5873. }
  5874. /**
  5875. * Gets the files hosting sites' whitelist.
  5876. *
  5877. * @author Aquilino Blanco Cores <aqblanco@gmail.com>
  5878. *
  5879. * @return array the sites list
  5880. */
  5881. public static function getFileHostingWhiteList()
  5882. {
  5883. return [
  5884. 'asuswebstorage.com',
  5885. 'dropbox.com',
  5886. 'dropboxusercontent.com',
  5887. 'fileserve.com',
  5888. 'drive.google.com',
  5889. 'docs.google.com',
  5890. 'icloud.com',
  5891. 'mediafire.com',
  5892. 'mega.nz',
  5893. 'onedrive.live.com',
  5894. 'slideshare.net',
  5895. 'scribd.com',
  5896. 'wetransfer.com',
  5897. 'box.com',
  5898. 'livefilestore.com', // OneDrive
  5899. ];
  5900. }
  5901. /**
  5902. * @param int $userId
  5903. *
  5904. * @return array Example [ 0 => ['code' => 'ABC', 'directory' => 'ABC0', 'path' => '/images/gallery/test.png', 'code_path' => 'ABC:/images/gallery/test.png'], 1 => ...]
  5905. */
  5906. public static function getAllDocumentsCreatedByUser($userId)
  5907. {
  5908. $tblItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  5909. $tblDocument = Database::get_course_table(TABLE_DOCUMENT);
  5910. $tblCourse = Database::get_main_table(TABLE_MAIN_COURSE);
  5911. $userId = (int) $userId;
  5912. $sql = "SELECT DISTINCT c.code, c.directory, docs.path
  5913. FROM $tblItemProperty AS last
  5914. INNER JOIN $tblDocument AS docs
  5915. ON (
  5916. docs.id = last.ref AND
  5917. docs.c_id = last.c_id AND
  5918. docs.filetype <> 'folder'
  5919. )
  5920. INNER JOIN $tblCourse as c
  5921. ON (
  5922. docs.c_id = c.id
  5923. )
  5924. WHERE
  5925. last.tool = '".TOOL_DOCUMENT."' AND
  5926. last.insert_user_id = $userId AND
  5927. docs.path NOT LIKE '%_DELETED_%'
  5928. ORDER BY c.directory, docs.path
  5929. ";
  5930. $result = Database::query($sql);
  5931. $list = [];
  5932. if (Database::num_rows($result) != 0) {
  5933. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5934. $row['code_path'] = $row['code'].':'.$row['path'];
  5935. $list[] = $row;
  5936. }
  5937. }
  5938. return $list;
  5939. }
  5940. /**
  5941. * Parse file information into a link.
  5942. *
  5943. * @param array $userInfo Current user info
  5944. * @param array $course_info
  5945. * @param int $session_id
  5946. * @param array $resource
  5947. * @param int $lp_id
  5948. * @param bool $add_move_button
  5949. * @param string $target
  5950. * @param string $overwrite_url
  5951. *
  5952. * @return string|null
  5953. */
  5954. private static function parseFile(
  5955. $userInfo,
  5956. $course_info,
  5957. $session_id,
  5958. $resource,
  5959. $lp_id,
  5960. $add_move_button,
  5961. $target,
  5962. $overwrite_url
  5963. ) {
  5964. $img_sys_path = api_get_path(SYS_CODE_PATH).'img/';
  5965. $web_code_path = api_get_path(WEB_CODE_PATH);
  5966. $documentId = $resource['id'];
  5967. $path = $resource['path'];
  5968. if (empty($path)) {
  5969. $num = 0;
  5970. } else {
  5971. $num = substr_count($path, '/') - 1;
  5972. }
  5973. // It's a file.
  5974. $icon = choose_image($path);
  5975. $position = strrpos($icon, '.');
  5976. $icon = substr($icon, 0, $position).'_small.gif';
  5977. $my_file_title = $resource['title'];
  5978. $visibility = $resource['visibility'];
  5979. // If title is empty we try to use the path
  5980. if (empty($my_file_title)) {
  5981. $my_file_title = basename($path);
  5982. }
  5983. // Show the "image name" not the filename of the image.
  5984. if ($lp_id) {
  5985. // LP URL
  5986. $url = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq().'&action=add_item&type='.TOOL_DOCUMENT.'&file='.$documentId.'&lp_id='.$lp_id;
  5987. } else {
  5988. // Direct document URL
  5989. $url = $web_code_path.'document/document.php?cidReq='.$course_info['code'].'&id_session='.$session_id.'&id='.$documentId;
  5990. }
  5991. if (!empty($overwrite_url)) {
  5992. $overwrite_url = Security::remove_XSS($overwrite_url);
  5993. $url = $overwrite_url.'&cidReq='.$course_info['code'].'&id_session='.$session_id.'&document_id='.$documentId;
  5994. }
  5995. $img = Display::returnIconPath($icon);
  5996. if (!file_exists($img_sys_path.$icon)) {
  5997. $img = Display::returnIconPath('default_small.gif');
  5998. }
  5999. $link = Display::url(
  6000. '<img alt="" src="'.$img.'" title="" />&nbsp;'.$my_file_title,
  6001. $url,
  6002. ['target' => $target, 'class' => 'moved']
  6003. );
  6004. $directUrl = $web_code_path.'document/document.php?cidReq='.$course_info['code'].'&id_session='.$session_id.'&id='.$documentId;
  6005. $link .= '&nbsp;'.Display::url(
  6006. Display::return_icon('preview_view.png', get_lang('Preview')),
  6007. $directUrl,
  6008. ['target' => '_blank']
  6009. );
  6010. $visibilityClass = null;
  6011. if ($visibility == 0) {
  6012. $visibilityClass = ' text-muted ';
  6013. }
  6014. $return = null;
  6015. if ($lp_id == false) {
  6016. $return .= '<li class="doc_resource '.$visibilityClass.' " data_id="'.$documentId.'" data_type="document" title="'.$my_file_title.'" >';
  6017. } else {
  6018. $return .= '<li class="doc_resource lp_resource_element '.$visibilityClass.' " data_id="'.$documentId.'" data_type="document" title="'.$my_file_title.'" >';
  6019. }
  6020. $return .= '<div class="item_data" style="margin-left:'.($num * 5).'px;margin-right:5px;">';
  6021. if ($add_move_button) {
  6022. $return .= '<a class="moved" href="#">';
  6023. $return .= Display::return_icon('move_everywhere.png', get_lang('Move'), [], ICON_SIZE_TINY);
  6024. $return .= '</a> ';
  6025. }
  6026. $return .= $link;
  6027. $sessionStar = api_get_session_image($resource['session_id'], $userInfo['status']);
  6028. $return .= $sessionStar;
  6029. $return .= '</div></li>';
  6030. return $return;
  6031. }
  6032. /**
  6033. * @param int $folderId
  6034. * @param array $resource
  6035. * @param int $lp_id
  6036. *
  6037. * @return string|null
  6038. */
  6039. private static function parseFolder($folderId, $resource, $lp_id)
  6040. {
  6041. $title = isset($resource['title']) ? $resource['title'] : null;
  6042. $path = isset($resource['path']) ? $resource['path'] : null;
  6043. if (empty($path)) {
  6044. $num = 0;
  6045. } else {
  6046. $num = substr_count($path, '/');
  6047. }
  6048. // It's a folder.
  6049. //hide some folders
  6050. if (in_array(
  6051. $path,
  6052. ['shared_folder', 'chat_files', 'HotPotatoes_files', 'css', 'certificates']
  6053. )) {
  6054. return null;
  6055. } elseif (preg_match('/_groupdocs/', $path)) {
  6056. return null;
  6057. } elseif (preg_match('/sf_user_/', $path)) {
  6058. return null;
  6059. } elseif (preg_match('/shared_folder_session_/', $path)) {
  6060. return null;
  6061. }
  6062. //$onclick = '';
  6063. // if in LP, hidden folder are displayed in grey
  6064. $folder_class_hidden = '';
  6065. if ($lp_id) {
  6066. if (isset($resource['visible']) && $resource['visible'] == 0) {
  6067. $folder_class_hidden = ' doc_folder_hidden'; // in base.css
  6068. }
  6069. }
  6070. $onclick = 'onclick="javascript: testResources(\'res_'.$resource['id'].'\',\'img_'.$resource['id'].'\')"';
  6071. $return = null;
  6072. if (empty($path)) {
  6073. $return = '<ul class="lp_resource">';
  6074. }
  6075. $return .= '<li class="doc_folder'.$folder_class_hidden.'" id="doc_id_'.$resource['id'].'" style="margin-left:'.($num * 18).'px; ">';
  6076. $image = Display::returnIconPath('nolines_plus.gif');
  6077. if (empty($path)) {
  6078. $image = Display::returnIconPath('nolines_minus.gif');
  6079. }
  6080. $return .= '<img style="cursor: pointer;" src="'.$image.'" align="absmiddle" id="img_'.$resource['id'].'" '.$onclick.'>';
  6081. $return .= Display::return_icon('lp_folder.gif').'&nbsp;';
  6082. $return .= '<span '.$onclick.' style="cursor: pointer;" >'.$title.'</span>';
  6083. $return .= '</li>';
  6084. if (empty($path)) {
  6085. if ($folderId == false) {
  6086. $return .= '<div id="res_'.$resource['id'].'" >';
  6087. } else {
  6088. $return .= '<div id="res_'.$resource['id'].'" style="display: none;" >';
  6089. }
  6090. }
  6091. return $return;
  6092. }
  6093. /**
  6094. * Get the button to edit document.
  6095. *
  6096. * @param bool $isReadOnly
  6097. * @param array $documentData
  6098. * @param string $extension
  6099. * @param bool $isCertificateMode
  6100. *
  6101. * @return string
  6102. */
  6103. private static function getButtonEdit($isReadOnly, array $documentData, $extension, $isCertificateMode)
  6104. {
  6105. $extension = strtolower($extension);
  6106. $iconEn = Display::return_icon('edit.png', get_lang('Modify'));
  6107. $iconDis = Display::return_icon('edit_na.png', get_lang('Modify'));
  6108. $courseParams = api_get_cidreq();
  6109. $webOdfExtensionList = self::get_web_odf_extension_list();
  6110. $path = $documentData['path'];
  6111. $document_id = $documentData['id'];
  6112. if ($isReadOnly) {
  6113. if (!api_is_course_admin() && !api_is_platform_admin()) {
  6114. return $iconDis;
  6115. }
  6116. if (
  6117. $extension == 'svg' && api_browser_support('svg') &&
  6118. api_get_setting('enabled_support_svg') == 'true'
  6119. ) {
  6120. return Display::url($iconEn, "edit_draw.php?$courseParams&id=$document_id");
  6121. }
  6122. if (
  6123. in_array($extension, $webOdfExtensionList) &&
  6124. api_get_configuration_value('enabled_support_odf') === true
  6125. ) {
  6126. return Display::url($iconEn, "edit_odf.php?$courseParams&id=$document_id");
  6127. }
  6128. if (
  6129. in_array($extension, ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'pxd']) &&
  6130. api_get_setting('enabled_support_pixlr') == 'true'
  6131. ) {
  6132. return Display::url($iconEn, "edit_paint.php?$courseParams&id=$document_id");
  6133. }
  6134. return Display::url($iconEn, "edit_document.php?$courseParams&id=$document_id");
  6135. }
  6136. if (in_array($path, self::get_system_folders())) {
  6137. return $iconDis;
  6138. }
  6139. if ($isCertificateMode) {
  6140. return Display::url($iconEn, "edit_document.php?$courseParams&id=$document_id&curdirpath=/certificates");
  6141. }
  6142. $sessionId = api_get_session_id();
  6143. if ($sessionId && $documentData['session_id'] != $sessionId) {
  6144. return $iconDis;
  6145. }
  6146. if (
  6147. $extension == 'svg' && api_browser_support('svg') &&
  6148. api_get_setting('enabled_support_svg') == 'true'
  6149. ) {
  6150. return Display::url($iconEn, "edit_draw.php?$courseParams&id=$document_id");
  6151. }
  6152. if (
  6153. in_array($extension, $webOdfExtensionList) &&
  6154. api_get_configuration_value('enabled_support_odf') === true
  6155. ) {
  6156. return Display::url($iconEn, "edit_odf.php?$courseParams&id=$document_id");
  6157. }
  6158. if (
  6159. in_array($extension, ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'pxd']) &&
  6160. api_get_setting('enabled_support_pixlr') == 'true'
  6161. ) {
  6162. return Display::url($iconEn, "edit_paint.php?$courseParams&id=$document_id");
  6163. }
  6164. return Display::url($iconEn, "edit_document.php?$courseParams&id=$document_id");
  6165. }
  6166. /**
  6167. * Get the button to move document.
  6168. *
  6169. * @param bool $isReadOnly
  6170. * @param array $documentData
  6171. * @param bool $isCertificateMode
  6172. * @param int $parentId
  6173. *
  6174. * @return string
  6175. */
  6176. private static function getButtonMove($isReadOnly, array $documentData, $isCertificateMode, $parentId)
  6177. {
  6178. $iconEn = Display::return_icon('move.png', get_lang('Move'));
  6179. $iconDis = Display::return_icon('move_na.png', get_lang('Move'));
  6180. if ($isReadOnly) {
  6181. return $iconDis;
  6182. }
  6183. $path = $documentData['path'];
  6184. $document_id = $documentData['id'];
  6185. $sessionId = api_get_session_id();
  6186. $courseParams = api_get_cidreq();
  6187. if ($isCertificateMode || in_array($path, self::get_system_folders())) {
  6188. return $iconDis;
  6189. }
  6190. if ($sessionId) {
  6191. if ($documentData['session_id'] != $sessionId) {
  6192. return $iconDis;
  6193. }
  6194. }
  6195. $urlMoveParams = http_build_query(['id' => $parentId, 'move' => $document_id]);
  6196. return Display::url(
  6197. $iconEn,
  6198. api_get_self()."?$courseParams&$urlMoveParams"
  6199. );
  6200. }
  6201. /**
  6202. * Get the button to set visibility to document.
  6203. *
  6204. * @param bool $isReadOnly
  6205. * @param int $visibility
  6206. * @param array $documentData
  6207. * @param bool $isCertificateMode
  6208. * @param int $parentId
  6209. *
  6210. * @return string|null
  6211. */
  6212. private static function getButtonVisibility(
  6213. $isReadOnly,
  6214. $visibility,
  6215. array $documentData,
  6216. $isCertificateMode,
  6217. $parentId
  6218. ) {
  6219. $visibility_icon = $visibility == 0 ? 'invisible' : 'visible';
  6220. $visibility_command = $visibility == 0 ? 'set_visible' : 'set_invisible';
  6221. $courseParams = api_get_cidreq();
  6222. if ($isReadOnly) {
  6223. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  6224. return Display::return_icon($visibility_icon.'.png', get_lang('VisibilityCannotBeChanged'));
  6225. }
  6226. return null;
  6227. }
  6228. if ($isCertificateMode) {
  6229. return Display::return_icon($visibility_icon.'.png', get_lang('VisibilityCannotBeChanged'));
  6230. }
  6231. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  6232. $tip_visibility = $visibility_icon == 'invisible' ? get_lang('Show') : get_lang('Hide');
  6233. return Display::url(
  6234. Display::return_icon($visibility_icon.'.png', $tip_visibility),
  6235. api_get_self()."?$courseParams&id=$parentId&$visibility_command={$documentData['id']}"
  6236. );
  6237. }
  6238. return null;
  6239. }
  6240. /**
  6241. * GEt the button to delete a document.
  6242. *
  6243. * @param bool $isReadOnly
  6244. * @param array $documentData
  6245. * @param bool $isCertificateMode
  6246. * @param string $curDirPath
  6247. * @param int $parentId
  6248. *
  6249. * @return string
  6250. */
  6251. private static function getButtonDelete(
  6252. $isReadOnly,
  6253. array $documentData,
  6254. $isCertificateMode,
  6255. $curDirPath,
  6256. $parentId
  6257. ) {
  6258. $iconEn = Display::return_icon('delete.png', get_lang('Delete'));
  6259. $iconDis = Display::return_icon('delete_na.png', get_lang('ThisFolderCannotBeDeleted'));
  6260. $path = $documentData['path'];
  6261. $id = $documentData['id'];
  6262. $courseParams = api_get_cidreq();
  6263. if ($isReadOnly) {
  6264. return $iconDis;
  6265. }
  6266. if (in_array($path, self::get_system_folders())) {
  6267. return $iconDis;
  6268. }
  6269. $titleToShow = addslashes(basename($documentData['title']));
  6270. $urlDeleteParams = http_build_query([
  6271. 'curdirpath' => $curDirPath,
  6272. 'action' => 'delete_item',
  6273. 'id' => $parentId,
  6274. 'deleteid' => $documentData['id'],
  6275. ]);
  6276. $btn = Display::url(
  6277. $iconEn,
  6278. '#',
  6279. [
  6280. 'data-item-title' => $titleToShow,
  6281. 'data-href' => api_get_self()."?$courseParams&$urlDeleteParams",
  6282. 'data-toggle' => 'modal',
  6283. 'data-target' => '#confirm-delete',
  6284. ]
  6285. );
  6286. if (
  6287. isset($_GET['curdirpath']) &&
  6288. $_GET['curdirpath'] == '/certificates' &&
  6289. self::get_default_certificate_id(api_get_course_id()) == $id
  6290. ) {
  6291. return $btn;
  6292. }
  6293. if ($isCertificateMode) {
  6294. return $btn;
  6295. }
  6296. $sessionId = api_get_session_id();
  6297. if ($sessionId) {
  6298. if ($documentData['session_id'] != $sessionId) {
  6299. return $iconDis;
  6300. }
  6301. }
  6302. return $btn;
  6303. }
  6304. }