document.lib.php 260 KB

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