api.lib.php 302 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\SettingsCurrent;
  4. use Chamilo\CourseBundle\Entity\CItemProperty;
  5. use Chamilo\UserBundle\Entity\User;
  6. use ChamiloSession as Session;
  7. use Symfony\Component\Finder\Finder;
  8. /**
  9. * This is a code library for Chamilo.
  10. * It is included by default in every Chamilo file (through including the global.inc.php)
  11. * This library is in process of being transferred to src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.
  12. * Whenever a function is transferred to the ChamiloApi class, the places where it is used should include
  13. * the "use Chamilo\CoreBundle\Component\Utils\ChamiloApi;" statement.
  14. *
  15. * @package chamilo.library
  16. */
  17. /**
  18. * Constants declaration.
  19. */
  20. // PHP version requirement.
  21. define('REQUIRED_PHP_VERSION', '5.5');
  22. define('REQUIRED_MIN_MEMORY_LIMIT', '128');
  23. define('REQUIRED_MIN_UPLOAD_MAX_FILESIZE', '10');
  24. define('REQUIRED_MIN_POST_MAX_SIZE', '10');
  25. // USER STATUS CONSTANTS
  26. /** global status of a user: student */
  27. define('STUDENT', 5);
  28. /** global status of a user: course manager */
  29. define('COURSEMANAGER', 1);
  30. /** global status of a user: session admin */
  31. define('SESSIONADMIN', 3);
  32. /** global status of a user: human ressource manager */
  33. define('DRH', 4);
  34. /** global status of a user: human ressource manager */
  35. define('ANONYMOUS', 6);
  36. /** global status of a user: low security, necessary for inserting data from
  37. * the teacher through HTMLPurifier */
  38. define('COURSEMANAGERLOWSECURITY', 10);
  39. // Soft user status
  40. define('PLATFORM_ADMIN', 11);
  41. define('SESSION_COURSE_COACH', 12);
  42. define('SESSION_GENERAL_COACH', 13);
  43. define('COURSE_STUDENT', 14); //student subscribed in a course
  44. define('SESSION_STUDENT', 15); //student subscribed in a session course
  45. define('COURSE_TUTOR', 16); // student is tutor of a course (NOT in session)
  46. define('STUDENT_BOSS', 17); // student is boss
  47. define('INVITEE', 20);
  48. define('HRM_REQUEST', 21); //HRM has request for vinculation with user
  49. // Table of status
  50. $_status_list[COURSEMANAGER] = 'teacher'; // 1
  51. $_status_list[SESSIONADMIN] = 'session_admin'; // 3
  52. $_status_list[DRH] = 'drh'; // 4
  53. $_status_list[STUDENT] = 'user'; // 5
  54. $_status_list[ANONYMOUS] = 'anonymous'; // 6
  55. $_status_list[INVITEE] = 'invited'; // 20
  56. // COURSE VISIBILITY CONSTANTS
  57. /** only visible for course admin */
  58. define('COURSE_VISIBILITY_CLOSED', 0);
  59. /** only visible for users registered in the course */
  60. define('COURSE_VISIBILITY_REGISTERED', 1);
  61. /** Open for all registered users on the platform */
  62. define('COURSE_VISIBILITY_OPEN_PLATFORM', 2);
  63. /** Open for the whole world */
  64. define('COURSE_VISIBILITY_OPEN_WORLD', 3);
  65. /** Invisible to all except admin */
  66. define('COURSE_VISIBILITY_HIDDEN', 4);
  67. define('COURSE_REQUEST_PENDING', 0);
  68. define('COURSE_REQUEST_ACCEPTED', 1);
  69. define('COURSE_REQUEST_REJECTED', 2);
  70. define('DELETE_ACTION_ENABLED', false);
  71. // EMAIL SENDING RECIPIENT CONSTANTS
  72. define('SEND_EMAIL_EVERYONE', 1);
  73. define('SEND_EMAIL_STUDENTS', 2);
  74. define('SEND_EMAIL_TEACHERS', 3);
  75. // SESSION VISIBILITY CONSTANTS
  76. define('SESSION_VISIBLE_READ_ONLY', 1);
  77. define('SESSION_VISIBLE', 2);
  78. define('SESSION_INVISIBLE', 3); // not available
  79. define('SESSION_AVAILABLE', 4);
  80. define('SESSION_LINK_TARGET', '_self');
  81. define('SUBSCRIBE_ALLOWED', 1);
  82. define('SUBSCRIBE_NOT_ALLOWED', 0);
  83. define('UNSUBSCRIBE_ALLOWED', 1);
  84. define('UNSUBSCRIBE_NOT_ALLOWED', 0);
  85. // SURVEY VISIBILITY CONSTANTS
  86. define('SURVEY_VISIBLE_TUTOR', 0);
  87. define('SURVEY_VISIBLE_TUTOR_STUDENT', 1);
  88. define('SURVEY_VISIBLE_PUBLIC', 2);
  89. // CONSTANTS defining all tools, using the english version
  90. /* When you add a new tool you must add it into function api_get_tools_lists() too */
  91. define('TOOL_DOCUMENT', 'document');
  92. define('TOOL_LP_FINAL_ITEM', 'final_item');
  93. define('TOOL_READOUT_TEXT', 'readout_text');
  94. define('TOOL_THUMBNAIL', 'thumbnail');
  95. define('TOOL_HOTPOTATOES', 'hotpotatoes');
  96. define('TOOL_CALENDAR_EVENT', 'calendar_event');
  97. define('TOOL_LINK', 'link');
  98. define('TOOL_LINK_CATEGORY', 'link_category');
  99. define('TOOL_COURSE_DESCRIPTION', 'course_description');
  100. define('TOOL_SEARCH', 'search');
  101. define('TOOL_LEARNPATH', 'learnpath');
  102. define('TOOL_LEARNPATH_CATEGORY', 'learnpath_category');
  103. define('TOOL_AGENDA', 'agenda');
  104. define('TOOL_ANNOUNCEMENT', 'announcement');
  105. define('TOOL_FORUM', 'forum');
  106. define('TOOL_FORUM_CATEGORY', 'forum_category');
  107. define('TOOL_FORUM_THREAD', 'forum_thread');
  108. define('TOOL_FORUM_POST', 'forum_post');
  109. define('TOOL_FORUM_ATTACH', 'forum_attachment');
  110. define('TOOL_FORUM_THREAD_QUALIFY', 'forum_thread_qualify');
  111. define('TOOL_THREAD', 'thread');
  112. define('TOOL_POST', 'post');
  113. define('TOOL_DROPBOX', 'dropbox');
  114. define('TOOL_QUIZ', 'quiz');
  115. define('TOOL_TEST_CATEGORY', 'test_category');
  116. define('TOOL_USER', 'user');
  117. define('TOOL_GROUP', 'group');
  118. define('TOOL_BLOGS', 'blog_management');
  119. define('TOOL_CHAT', 'chat');
  120. define('TOOL_STUDENTPUBLICATION', 'student_publication');
  121. define('TOOL_TRACKING', 'tracking');
  122. define('TOOL_HOMEPAGE_LINK', 'homepage_link');
  123. define('TOOL_COURSE_SETTING', 'course_setting');
  124. define('TOOL_BACKUP', 'backup');
  125. define('TOOL_COPY_COURSE_CONTENT', 'copy_course_content');
  126. define('TOOL_RECYCLE_COURSE', 'recycle_course');
  127. define('TOOL_COURSE_HOMEPAGE', 'course_homepage');
  128. define('TOOL_COURSE_RIGHTS_OVERVIEW', 'course_rights');
  129. define('TOOL_UPLOAD', 'file_upload');
  130. define('TOOL_COURSE_MAINTENANCE', 'course_maintenance');
  131. define('TOOL_SURVEY', 'survey');
  132. define('TOOL_WIKI', 'wiki');
  133. define('TOOL_GLOSSARY', 'glossary');
  134. define('TOOL_GRADEBOOK', 'gradebook');
  135. define('TOOL_NOTEBOOK', 'notebook');
  136. define('TOOL_ATTENDANCE', 'attendance');
  137. define('TOOL_COURSE_PROGRESS', 'course_progress');
  138. define('TOOL_PORTFOLIO', 'portfolio');
  139. // CONSTANTS defining Chamilo interface sections
  140. define('SECTION_CAMPUS', 'mycampus');
  141. define('SECTION_COURSES', 'mycourses');
  142. define('SECTION_CATALOG', 'catalog');
  143. define('SECTION_MYPROFILE', 'myprofile');
  144. define('SECTION_MYAGENDA', 'myagenda');
  145. define('SECTION_COURSE_ADMIN', 'course_admin');
  146. define('SECTION_PLATFORM_ADMIN', 'platform_admin');
  147. define('SECTION_MYGRADEBOOK', 'mygradebook');
  148. define('SECTION_TRACKING', 'session_my_space');
  149. define('SECTION_SOCIAL', 'social-network');
  150. define('SECTION_DASHBOARD', 'dashboard');
  151. define('SECTION_REPORTS', 'reports');
  152. define('SECTION_GLOBAL', 'global');
  153. define('SECTION_INCLUDE', 'include');
  154. // CONSTANT name for local authentication source
  155. define('PLATFORM_AUTH_SOURCE', 'platform');
  156. define('CAS_AUTH_SOURCE', 'cas');
  157. define('LDAP_AUTH_SOURCE', 'extldap');
  158. // CONSTANT defining the default HotPotatoes files directory
  159. define('DIR_HOTPOTATOES', '/HotPotatoes_files');
  160. // event logs types
  161. define('LOG_COURSE_DELETE', 'course_deleted');
  162. define('LOG_COURSE_CREATE', 'course_created');
  163. // @todo replace 'soc_gr' with social_group
  164. define('LOG_GROUP_PORTAL_CREATED', 'soc_gr_created');
  165. define('LOG_GROUP_PORTAL_UPDATED', 'soc_gr_updated');
  166. define('LOG_GROUP_PORTAL_DELETED', 'soc_gr_deleted');
  167. define('LOG_GROUP_PORTAL_USER_DELETE_ALL', 'soc_gr_delete_users');
  168. define('LOG_GROUP_PORTAL_ID', 'soc_gr_portal_id');
  169. define('LOG_GROUP_PORTAL_REL_USER_ARRAY', 'soc_gr_user_array');
  170. define('LOG_GROUP_PORTAL_USER_SUBSCRIBED', 'soc_gr_u_subs');
  171. define('LOG_GROUP_PORTAL_USER_UNSUBSCRIBED', 'soc_gr_u_unsubs');
  172. define('LOG_GROUP_PORTAL_USER_UPDATE_ROLE', 'soc_gr_update_role');
  173. define('LOG_USER_DELETE', 'user_deleted');
  174. define('LOG_USER_CREATE', 'user_created');
  175. define('LOG_USER_ENABLE', 'user_enable');
  176. define('LOG_USER_DISABLE', 'user_disable');
  177. define('LOG_USER_ANONYMIZE', 'user_anonymized');
  178. define('LOG_USER_FIELD_CREATE', 'user_field_created');
  179. define('LOG_USER_FIELD_DELETE', 'user_field_deleted');
  180. define('LOG_SESSION_CREATE', 'session_created');
  181. define('LOG_SESSION_DELETE', 'session_deleted');
  182. define('LOG_SESSION_ADD_USER_COURSE', 'session_add_user_course');
  183. define('LOG_SESSION_DELETE_USER_COURSE', 'session_delete_user_course');
  184. define('LOG_SESSION_ADD_USER', 'session_add_user');
  185. define('LOG_SESSION_DELETE_USER', 'session_delete_user');
  186. define('LOG_SESSION_ADD_COURSE', 'session_add_course');
  187. define('LOG_SESSION_DELETE_COURSE', 'session_delete_course');
  188. define('LOG_SESSION_CATEGORY_CREATE', 'session_cat_created'); //changed in 1.9.8
  189. define('LOG_SESSION_CATEGORY_DELETE', 'session_cat_deleted'); //changed in 1.9.8
  190. define('LOG_CONFIGURATION_SETTINGS_CHANGE', 'settings_changed');
  191. define('LOG_PLATFORM_LANGUAGE_CHANGE', 'platform_lng_changed'); //changed in 1.9.8
  192. define('LOG_SUBSCRIBE_USER_TO_COURSE', 'user_subscribed');
  193. define('LOG_UNSUBSCRIBE_USER_FROM_COURSE', 'user_unsubscribed');
  194. define('LOG_ATTEMPTED_FORCED_LOGIN', 'attempted_forced_login');
  195. define('LOG_HOMEPAGE_CHANGED', 'homepage_changed');
  196. define('LOG_PROMOTION_CREATE', 'promotion_created');
  197. define('LOG_PROMOTION_DELETE', 'promotion_deleted');
  198. define('LOG_CAREER_CREATE', 'career_created');
  199. define('LOG_CAREER_DELETE', 'career_deleted');
  200. define('LOG_USER_PERSONAL_DOC_DELETED', 'user_doc_deleted');
  201. define('LOG_WIKI_ACCESS', 'wiki_page_view');
  202. // All results from an exercise
  203. define('LOG_EXERCISE_RESULT_DELETE', 'exe_result_deleted');
  204. // Logs only the one attempt
  205. define('LOG_EXERCISE_ATTEMPT_DELETE', 'exe_attempt_deleted');
  206. define('LOG_LP_ATTEMPT_DELETE', 'lp_attempt_deleted');
  207. define('LOG_QUESTION_RESULT_DELETE', 'qst_attempt_deleted');
  208. define('LOG_MY_FOLDER_CREATE', 'my_folder_created');
  209. define('LOG_MY_FOLDER_CHANGE', 'my_folder_changed');
  210. define('LOG_MY_FOLDER_DELETE', 'my_folder_deleted');
  211. define('LOG_MY_FOLDER_COPY', 'my_folder_copied');
  212. define('LOG_MY_FOLDER_CUT', 'my_folder_cut');
  213. define('LOG_MY_FOLDER_PASTE', 'my_folder_pasted');
  214. define('LOG_MY_FOLDER_UPLOAD', 'my_folder_uploaded');
  215. // Event logs data types (max 20 chars)
  216. define('LOG_COURSE_CODE', 'course_code');
  217. define('LOG_COURSE_ID', 'course_id');
  218. define('LOG_USER_ID', 'user_id');
  219. define('LOG_USER_OBJECT', 'user_object');
  220. define('LOG_USER_FIELD_VARIABLE', 'user_field_variable');
  221. define('LOG_SESSION_ID', 'session_id');
  222. define('LOG_QUESTION_ID', 'question_id');
  223. define('LOG_SESSION_CATEGORY_ID', 'session_category_id');
  224. define('LOG_CONFIGURATION_SETTINGS_CATEGORY', 'settings_category');
  225. define('LOG_CONFIGURATION_SETTINGS_VARIABLE', 'settings_variable');
  226. define('LOG_PLATFORM_LANGUAGE', 'default_platform_language');
  227. define('LOG_CAREER_ID', 'career_id');
  228. define('LOG_PROMOTION_ID', 'promotion_id');
  229. define('LOG_GRADEBOOK_LOCKED', 'gradebook_locked');
  230. define('LOG_GRADEBOOK_UNLOCKED', 'gradebook_unlocked');
  231. define('LOG_GRADEBOOK_ID', 'gradebook_id');
  232. define('LOG_WIKI_PAGE_ID', 'wiki_page_id');
  233. define('LOG_EXERCISE_ID', 'exercise_id');
  234. define('LOG_EXERCISE_AND_USER_ID', 'exercise_and_user_id');
  235. define('LOG_LP_ID', 'lp_id');
  236. define('LOG_EXERCISE_ATTEMPT_QUESTION_ID', 'exercise_a_q_id');
  237. define('LOG_EXERCISE_ATTEMPT', 'exe_id');
  238. define('LOG_WORK_DIR_DELETE', 'work_dir_delete');
  239. define('LOG_WORK_FILE_DELETE', 'work_file_delete');
  240. define('LOG_WORK_DATA', 'work_data_array');
  241. define('LOG_MY_FOLDER_PATH', 'path');
  242. define('LOG_MY_FOLDER_NEW_PATH', 'new_path');
  243. define('LOG_TERM_CONDITION_ACCEPTED', 'term_condition_accepted');
  244. define('LOG_USER_CONFIRMED_EMAIL', 'user_confirmed_email');
  245. define('LOG_USER_REMOVED_LEGAL_ACCEPT', 'user_removed_legal_accept');
  246. define('LOG_USER_DELETE_ACCOUNT_REQUEST', 'user_delete_account_request');
  247. define('LOG_QUESTION_CREATED', 'question_created');
  248. define('LOG_QUESTION_UPDATED', 'question_updated');
  249. define('USERNAME_PURIFIER', '/[^0-9A-Za-z_\.]/');
  250. //used when login_is_email setting is true
  251. define('USERNAME_PURIFIER_MAIL', '/[^0-9A-Za-z_\.@]/');
  252. define('USERNAME_PURIFIER_SHALLOW', '/\s/');
  253. // This constant is a result of Windows OS detection, it has a boolean value:
  254. // true whether the server runs on Windows OS, false otherwise.
  255. define('IS_WINDOWS_OS', api_is_windows_os());
  256. // Checks for installed optional php-extensions.
  257. // intl extension (from PECL), it is installed by default as of PHP 5.3.0.
  258. define('INTL_INSTALLED', function_exists('intl_get_error_code'));
  259. // iconv extension, for PHP5 on Windows it is installed by default.
  260. define('ICONV_INSTALLED', function_exists('iconv'));
  261. define('MBSTRING_INSTALLED', function_exists('mb_strlen')); // mbstring extension.
  262. // Patterns for processing paths. Examples.
  263. define('REPEATED_SLASHES_PURIFIER', '/\/{2,}/'); // $path = preg_replace(REPEATED_SLASHES_PURIFIER, '/', $path);
  264. define('VALID_WEB_PATH', '/https?:\/\/[^\/]*(\/.*)?/i'); // $is_valid_path = preg_match(VALID_WEB_PATH, $path);
  265. // $new_path = preg_replace(VALID_WEB_SERVER_BASE, $new_base, $path);
  266. define('VALID_WEB_SERVER_BASE', '/https?:\/\/[^\/]*/i');
  267. // Constants for api_get_path() and api_get_path_type(), etc. - registered path types.
  268. // basic (leaf elements)
  269. define('REL_CODE_PATH', 'REL_CODE_PATH');
  270. define('REL_COURSE_PATH', 'REL_COURSE_PATH');
  271. define('REL_HOME_PATH', 'REL_HOME_PATH');
  272. // Constants for api_get_path() and api_get_path_type(), etc. - registered path types.
  273. define('WEB_PATH', 'WEB_PATH');
  274. define('WEB_APP_PATH', 'WEB_APP_PATH');
  275. define('SYS_PATH', 'SYS_PATH');
  276. define('SYS_APP_PATH', 'SYS_APP_PATH');
  277. define('SYS_UPLOAD_PATH', 'SYS_UPLOAD_PATH');
  278. define('WEB_UPLOAD_PATH', 'WEB_UPLOAD_PATH');
  279. define('REL_PATH', 'REL_PATH');
  280. define('WEB_COURSE_PATH', 'WEB_COURSE_PATH');
  281. define('SYS_COURSE_PATH', 'SYS_COURSE_PATH');
  282. define('WEB_CODE_PATH', 'WEB_CODE_PATH');
  283. define('SYS_CODE_PATH', 'SYS_CODE_PATH');
  284. define('SYS_LANG_PATH', 'SYS_LANG_PATH');
  285. define('WEB_IMG_PATH', 'WEB_IMG_PATH');
  286. define('WEB_CSS_PATH', 'WEB_CSS_PATH');
  287. define('WEB_PUBLIC_PATH', 'WEB_PUBLIC_PATH');
  288. define('SYS_CSS_PATH', 'SYS_CSS_PATH');
  289. define('SYS_PLUGIN_PATH', 'SYS_PLUGIN_PATH');
  290. define('WEB_PLUGIN_PATH', 'WEB_PLUGIN_PATH');
  291. define('WEB_PLUGIN_ASSET_PATH', 'WEB_PLUGIN_ASSET_PATH');
  292. define('SYS_ARCHIVE_PATH', 'SYS_ARCHIVE_PATH');
  293. define('WEB_ARCHIVE_PATH', 'WEB_ARCHIVE_PATH');
  294. define('SYS_INC_PATH', 'SYS_INC_PATH');
  295. define('LIBRARY_PATH', 'LIBRARY_PATH');
  296. define('CONFIGURATION_PATH', 'CONFIGURATION_PATH');
  297. define('WEB_LIBRARY_PATH', 'WEB_LIBRARY_PATH');
  298. define('WEB_LIBRARY_JS_PATH', 'WEB_LIBRARY_JS_PATH');
  299. define('WEB_AJAX_PATH', 'WEB_AJAX_PATH');
  300. define('SYS_TEST_PATH', 'SYS_TEST_PATH');
  301. define('WEB_TEMPLATE_PATH', 'WEB_TEMPLATE_PATH');
  302. define('SYS_TEMPLATE_PATH', 'SYS_TEMPLATE_PATH');
  303. define('SYS_PUBLIC_PATH', 'SYS_PUBLIC_PATH');
  304. define('SYS_HOME_PATH', 'SYS_HOME_PATH');
  305. define('WEB_HOME_PATH', 'WEB_HOME_PATH');
  306. define('WEB_FONTS_PATH', 'WEB_FONTS_PATH');
  307. define('SYS_FONTS_PATH', 'SYS_FONTS_PATH');
  308. define('SYS_DEFAULT_COURSE_DOCUMENT_PATH', 'SYS_DEFAULT_COURSE_DOCUMENT_PATH');
  309. define('REL_DEFAULT_COURSE_DOCUMENT_PATH', 'REL_DEFAULT_COURSE_DOCUMENT_PATH');
  310. define('WEB_DEFAULT_COURSE_DOCUMENT_PATH', 'WEB_DEFAULT_COURSE_DOCUMENT_PATH');
  311. // Relations type with Course manager
  312. define('COURSE_RELATION_TYPE_COURSE_MANAGER', 1);
  313. define('SESSION_RELATION_TYPE_COURSE_MANAGER', 1);
  314. // Relations type with Human resources manager
  315. define('COURSE_RELATION_TYPE_RRHH', 1);
  316. define('SESSION_RELATION_TYPE_RRHH', 1);
  317. //User image sizes
  318. define('USER_IMAGE_SIZE_ORIGINAL', 1);
  319. define('USER_IMAGE_SIZE_BIG', 2);
  320. define('USER_IMAGE_SIZE_MEDIUM', 3);
  321. define('USER_IMAGE_SIZE_SMALL', 4);
  322. // Relation type between users
  323. define('USER_UNKNOWN', 0);
  324. define('USER_RELATION_TYPE_UNKNOWN', 1);
  325. define('USER_RELATION_TYPE_PARENT', 2); // should be deprecated is useless
  326. define('USER_RELATION_TYPE_FRIEND', 3);
  327. define('USER_RELATION_TYPE_GOODFRIEND', 4); // should be deprecated is useless
  328. define('USER_RELATION_TYPE_ENEMY', 5); // should be deprecated is useless
  329. define('USER_RELATION_TYPE_DELETED', 6);
  330. define('USER_RELATION_TYPE_RRHH', 7);
  331. define('USER_RELATION_TYPE_BOSS', 8);
  332. define('USER_RELATION_TYPE_HRM_REQUEST', 9);
  333. // Gradebook link constants
  334. // Please do not change existing values, they are used in the database !
  335. define('GRADEBOOK_ITEM_LIMIT', 1000);
  336. define('LINK_EXERCISE', 1);
  337. define('LINK_DROPBOX', 2);
  338. define('LINK_STUDENTPUBLICATION', 3);
  339. define('LINK_LEARNPATH', 4);
  340. define('LINK_FORUM_THREAD', 5);
  341. //define('LINK_WORK',6);
  342. define('LINK_ATTENDANCE', 7);
  343. define('LINK_SURVEY', 8);
  344. define('LINK_HOTPOTATOES', 9);
  345. // Score display types constants
  346. define('SCORE_DIV', 1); // X / Y
  347. define('SCORE_PERCENT', 2); // XX %
  348. define('SCORE_DIV_PERCENT', 3); // X / Y (XX %)
  349. define('SCORE_AVERAGE', 4); // XX %
  350. define('SCORE_DECIMAL', 5); // 0.50 (X/Y)
  351. define('SCORE_BAR', 6); // Uses the Display::bar_progress function
  352. define('SCORE_SIMPLE', 7); // X
  353. define('SCORE_IGNORE_SPLIT', 8); // ??
  354. define('SCORE_DIV_PERCENT_WITH_CUSTOM', 9); // X / Y (XX %) - Good!
  355. define('SCORE_CUSTOM', 10); // Good!
  356. define('SCORE_DIV_SIMPLE_WITH_CUSTOM', 11); // X - Good!
  357. define('SCORE_DIV_SIMPLE_WITH_CUSTOM_LETTERS', 12); // X - Good!
  358. define('SCORE_ONLY_SCORE', 13); // X - Good!
  359. define('SCORE_NUMERIC', 14);
  360. define('SCORE_BOTH', 1);
  361. define('SCORE_ONLY_DEFAULT', 2);
  362. define('SCORE_ONLY_CUSTOM', 3);
  363. // From display.lib.php
  364. define('MAX_LENGTH_BREADCRUMB', 100);
  365. define('ICON_SIZE_ATOM', 8);
  366. define('ICON_SIZE_TINY', 16);
  367. define('ICON_SIZE_SMALL', 22);
  368. define('ICON_SIZE_MEDIUM', 32);
  369. define('ICON_SIZE_LARGE', 48);
  370. define('ICON_SIZE_BIG', 64);
  371. define('ICON_SIZE_HUGE', 128);
  372. define('SHOW_TEXT_NEAR_ICONS', false);
  373. // Session catalog
  374. define('CATALOG_COURSES', 0);
  375. define('CATALOG_SESSIONS', 1);
  376. define('CATALOG_COURSES_SESSIONS', 2);
  377. // Hook type events, pre-process and post-process.
  378. // All means to be executed for both hook event types
  379. define('HOOK_EVENT_TYPE_PRE', 0);
  380. define('HOOK_EVENT_TYPE_POST', 1);
  381. define('HOOK_EVENT_TYPE_ALL', 10);
  382. define('CAREER_STATUS_ACTIVE', 1);
  383. define('CAREER_STATUS_INACTIVE', 0);
  384. define('PROMOTION_STATUS_ACTIVE', 1);
  385. define('PROMOTION_STATUS_INACTIVE', 0);
  386. // Group permissions
  387. define('GROUP_PERMISSION_OPEN', '1');
  388. define('GROUP_PERMISSION_CLOSED', '2');
  389. // Group user permissions
  390. define('GROUP_USER_PERMISSION_ADMIN', '1'); // the admin of a group
  391. define('GROUP_USER_PERMISSION_READER', '2'); // a normal user
  392. define('GROUP_USER_PERMISSION_PENDING_INVITATION', '3'); // When an admin/moderator invites a user
  393. define('GROUP_USER_PERMISSION_PENDING_INVITATION_SENT_BY_USER', '4'); // an user joins a group
  394. define('GROUP_USER_PERMISSION_MODERATOR', '5'); // a moderator
  395. define('GROUP_USER_PERMISSION_ANONYMOUS', '6'); // an anonymous user
  396. define('GROUP_USER_PERMISSION_HRM', '7'); // a human resources manager
  397. define('GROUP_IMAGE_SIZE_ORIGINAL', 1);
  398. define('GROUP_IMAGE_SIZE_BIG', 2);
  399. define('GROUP_IMAGE_SIZE_MEDIUM', 3);
  400. define('GROUP_IMAGE_SIZE_SMALL', 4);
  401. define('GROUP_TITLE_LENGTH', 50);
  402. // Exercise
  403. // @todo move into a class
  404. define('ALL_ON_ONE_PAGE', 1);
  405. define('ONE_PER_PAGE', 2);
  406. define('EXERCISE_FEEDBACK_TYPE_END', 0); //Feedback - show score and expected answers
  407. define('EXERCISE_FEEDBACK_TYPE_DIRECT', 1); //DirectFeedback - Do not show score nor answers
  408. define('EXERCISE_FEEDBACK_TYPE_EXAM', 2); //NoFeedback - Show score only
  409. define('RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS', 0); //show score and expected answers
  410. define('RESULT_DISABLE_NO_SCORE_AND_EXPECTED_ANSWERS', 1); //Do not show score nor answers
  411. define('RESULT_DISABLE_SHOW_SCORE_ONLY', 2); //Show score only
  412. define('RESULT_DISABLE_SHOW_FINAL_SCORE_ONLY_WITH_CATEGORIES', 3); //Show final score only with categories
  413. define('RESULT_DISABLE_SHOW_SCORE_ATTEMPT_SHOW_ANSWERS_LAST_ATTEMPT', 4);
  414. define('RESULT_DISABLE_DONT_SHOW_SCORE_ONLY_IF_USER_FINISHES_ATTEMPTS_SHOW_ALWAYS_FEEDBACK', 5);
  415. define('RESULT_DISABLE_RANKING', 6);
  416. define('RESULT_DISABLE_SHOW_ONLY_IN_CORRECT_ANSWER', 7);
  417. define('RESULT_DISABLE_SHOW_SCORE_AND_EXPECTED_ANSWERS_AND_RANKING', 8);
  418. define('EXERCISE_MAX_NAME_SIZE', 80);
  419. // Question types (edit next array as well when adding values)
  420. // @todo move into a class
  421. define('UNIQUE_ANSWER', 1);
  422. define('MULTIPLE_ANSWER', 2);
  423. define('FILL_IN_BLANKS', 3);
  424. define('MATCHING', 4);
  425. define('FREE_ANSWER', 5);
  426. define('HOT_SPOT', 6);
  427. define('HOT_SPOT_ORDER', 7);
  428. define('HOT_SPOT_DELINEATION', 8);
  429. define('MULTIPLE_ANSWER_COMBINATION', 9);
  430. define('UNIQUE_ANSWER_NO_OPTION', 10);
  431. define('MULTIPLE_ANSWER_TRUE_FALSE', 11);
  432. define('MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE', 12);
  433. define('ORAL_EXPRESSION', 13);
  434. define('GLOBAL_MULTIPLE_ANSWER', 14);
  435. define('MEDIA_QUESTION', 15);
  436. define('CALCULATED_ANSWER', 16);
  437. define('UNIQUE_ANSWER_IMAGE', 17);
  438. define('DRAGGABLE', 18);
  439. define('MATCHING_DRAGGABLE', 19);
  440. define('ANNOTATION', 20);
  441. define('READING_COMPREHENSION', 21);
  442. define('MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY', 22);
  443. define('EXERCISE_CATEGORY_RANDOM_SHUFFLED', 1);
  444. define('EXERCISE_CATEGORY_RANDOM_ORDERED', 2);
  445. define('EXERCISE_CATEGORY_RANDOM_DISABLED', 0);
  446. // Question selection type
  447. define('EX_Q_SELECTION_ORDERED', 1);
  448. define('EX_Q_SELECTION_RANDOM', 2);
  449. define('EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_ORDERED', 3);
  450. define('EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED', 4);
  451. define('EX_Q_SELECTION_CATEGORIES_ORDERED_QUESTIONS_RANDOM', 5);
  452. define('EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM', 6);
  453. define('EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_ORDERED_NO_GROUPED', 7);
  454. define('EX_Q_SELECTION_CATEGORIES_RANDOM_QUESTIONS_RANDOM_NO_GROUPED', 8);
  455. define('EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_ORDERED', 9);
  456. define('EX_Q_SELECTION_CATEGORIES_ORDERED_BY_PARENT_QUESTIONS_RANDOM', 10);
  457. // Used to save the skill_rel_item table
  458. define('ITEM_TYPE_EXERCISE', 1);
  459. define('ITEM_TYPE_HOTPOTATOES', 2);
  460. define('ITEM_TYPE_LINK', 3);
  461. define('ITEM_TYPE_LEARNPATH', 4);
  462. define('ITEM_TYPE_GRADEBOOK', 5);
  463. define('ITEM_TYPE_STUDENT_PUBLICATION', 6);
  464. //define('ITEM_TYPE_FORUM', 7);
  465. define('ITEM_TYPE_ATTENDANCE', 8);
  466. define('ITEM_TYPE_SURVEY', 9);
  467. define('ITEM_TYPE_FORUM_THREAD', 10);
  468. // one big string with all question types, for the validator in pear/HTML/QuickForm/Rule/QuestionType
  469. define(
  470. 'QUESTION_TYPES',
  471. UNIQUE_ANSWER.':'.
  472. MULTIPLE_ANSWER.':'.
  473. FILL_IN_BLANKS.':'.
  474. MATCHING.':'.
  475. FREE_ANSWER.':'.
  476. HOT_SPOT.':'.
  477. HOT_SPOT_ORDER.':'.
  478. HOT_SPOT_DELINEATION.':'.
  479. MULTIPLE_ANSWER_COMBINATION.':'.
  480. UNIQUE_ANSWER_NO_OPTION.':'.
  481. MULTIPLE_ANSWER_TRUE_FALSE.':'.
  482. MULTIPLE_ANSWER_COMBINATION_TRUE_FALSE.':'.
  483. ORAL_EXPRESSION.':'.
  484. GLOBAL_MULTIPLE_ANSWER.':'.
  485. MEDIA_QUESTION.':'.
  486. CALCULATED_ANSWER.':'.
  487. UNIQUE_ANSWER_IMAGE.':'.
  488. DRAGGABLE.':'.
  489. MATCHING_DRAGGABLE.':'.
  490. MULTIPLE_ANSWER_TRUE_FALSE_DEGREE_CERTAINTY.':'.
  491. ANNOTATION
  492. );
  493. //Some alias used in the QTI exports
  494. define('MCUA', 1);
  495. define('TF', 1);
  496. define('MCMA', 2);
  497. define('FIB', 3);
  498. // Skills
  499. define('SKILL_TYPE_REQUIREMENT', 'required');
  500. define('SKILL_TYPE_ACQUIRED', 'acquired');
  501. define('SKILL_TYPE_BOTH', 'both');
  502. // Message
  503. define('MESSAGE_STATUS_NEW', '0');
  504. define('MESSAGE_STATUS_UNREAD', '1');
  505. //2 ??
  506. define('MESSAGE_STATUS_DELETED', '3');
  507. define('MESSAGE_STATUS_OUTBOX', '4');
  508. define('MESSAGE_STATUS_INVITATION_PENDING', '5');
  509. define('MESSAGE_STATUS_INVITATION_ACCEPTED', '6');
  510. define('MESSAGE_STATUS_INVITATION_DENIED', '7');
  511. define('MESSAGE_STATUS_WALL', '8');
  512. define('MESSAGE_STATUS_WALL_DELETE', '9');
  513. define('MESSAGE_STATUS_WALL_POST', '10');
  514. define('MESSAGE_STATUS_CONVERSATION', '11');
  515. define('MESSAGE_STATUS_FORUM', '12');
  516. // Images
  517. define('IMAGE_WALL_SMALL_SIZE', 200);
  518. define('IMAGE_WALL_MEDIUM_SIZE', 500);
  519. define('IMAGE_WALL_BIG_SIZE', 2000);
  520. define('IMAGE_WALL_SMALL', 'small');
  521. define('IMAGE_WALL_MEDIUM', 'medium');
  522. define('IMAGE_WALL_BIG', 'big');
  523. // Social PLUGIN PLACES
  524. define('SOCIAL_LEFT_PLUGIN', 1);
  525. define('SOCIAL_CENTER_PLUGIN', 2);
  526. define('SOCIAL_RIGHT_PLUGIN', 3);
  527. define('CUT_GROUP_NAME', 50);
  528. /**
  529. * FormValidator Filter.
  530. */
  531. define('NO_HTML', 1);
  532. define('STUDENT_HTML', 2);
  533. define('TEACHER_HTML', 3);
  534. define('STUDENT_HTML_FULLPAGE', 4);
  535. define('TEACHER_HTML_FULLPAGE', 5);
  536. // Timeline
  537. define('TIMELINE_STATUS_ACTIVE', '1');
  538. define('TIMELINE_STATUS_INACTIVE', '2');
  539. // Event email template class
  540. define('EVENT_EMAIL_TEMPLATE_ACTIVE', 1);
  541. define('EVENT_EMAIL_TEMPLATE_INACTIVE', 0);
  542. // Course home
  543. define('SHORTCUTS_HORIZONTAL', 0);
  544. define('SHORTCUTS_VERTICAL', 1);
  545. // Image class
  546. define('IMAGE_PROCESSOR', 'gd'); // 'imagick' or 'gd' strings
  547. // Course copy
  548. define('FILE_SKIP', 1);
  549. define('FILE_RENAME', 2);
  550. define('FILE_OVERWRITE', 3);
  551. define('UTF8_CONVERT', false); //false by default
  552. define('DOCUMENT', 'file');
  553. define('FOLDER', 'folder');
  554. define('RESOURCE_ASSET', 'asset');
  555. define('RESOURCE_DOCUMENT', 'document');
  556. define('RESOURCE_GLOSSARY', 'glossary');
  557. define('RESOURCE_EVENT', 'calendar_event');
  558. define('RESOURCE_LINK', 'link');
  559. define('RESOURCE_COURSEDESCRIPTION', 'course_description');
  560. define('RESOURCE_LEARNPATH', 'learnpath');
  561. define('RESOURCE_LEARNPATH_CATEGORY', 'learnpath_category');
  562. define('RESOURCE_ANNOUNCEMENT', 'announcement');
  563. define('RESOURCE_FORUM', 'forum');
  564. define('RESOURCE_FORUMTOPIC', 'thread');
  565. define('RESOURCE_FORUMPOST', 'post');
  566. define('RESOURCE_QUIZ', 'quiz');
  567. define('RESOURCE_TEST_CATEGORY', 'test_category');
  568. define('RESOURCE_QUIZQUESTION', 'Exercise_Question');
  569. define('RESOURCE_TOOL_INTRO', 'Tool introduction');
  570. define('RESOURCE_LINKCATEGORY', 'Link_Category');
  571. define('RESOURCE_FORUMCATEGORY', 'Forum_Category');
  572. define('RESOURCE_SCORM', 'Scorm');
  573. define('RESOURCE_SURVEY', 'survey');
  574. define('RESOURCE_SURVEYQUESTION', 'survey_question');
  575. define('RESOURCE_SURVEYINVITATION', 'survey_invitation');
  576. define('RESOURCE_WIKI', 'wiki');
  577. define('RESOURCE_THEMATIC', 'thematic');
  578. define('RESOURCE_ATTENDANCE', 'attendance');
  579. define('RESOURCE_WORK', 'work');
  580. define('RESOURCE_SESSION_COURSE', 'session_course');
  581. define('RESOURCE_GRADEBOOK', 'gradebook');
  582. define('ADD_THEMATIC_PLAN', 6);
  583. // Max online users to show per page (whoisonline)
  584. define('MAX_ONLINE_USERS', 12);
  585. // Number of characters maximum to show in preview of course blog posts
  586. define('BLOG_MAX_PREVIEW_CHARS', 800);
  587. // HTML string to replace with a 'Read more...' link
  588. define('BLOG_PAGE_BREAK', '<div style="page-break-after: always"><span style="display: none;">&nbsp;</span></div>');
  589. // Make sure the CHAMILO_LOAD_WYSIWYG constant is defined
  590. // To remove CKeditor libs from HTML, set this constant to true before loading
  591. if (!defined('CHAMILO_LOAD_WYSIWYG')) {
  592. define('CHAMILO_LOAD_WYSIWYG', true);
  593. }
  594. /* Constants for course home */
  595. define('TOOL_PUBLIC', 'Public');
  596. define('TOOL_PUBLIC_BUT_HIDDEN', 'PublicButHide');
  597. define('TOOL_COURSE_ADMIN', 'courseAdmin');
  598. define('TOOL_PLATFORM_ADMIN', 'platformAdmin');
  599. define('TOOL_AUTHORING', 'toolauthoring');
  600. define('TOOL_INTERACTION', 'toolinteraction');
  601. define('TOOL_COURSE_PLUGIN', 'toolcourseplugin'); //all plugins that can be enabled in courses
  602. define('TOOL_ADMIN', 'tooladmin');
  603. define('TOOL_ADMIN_PLATFORM', 'tooladminplatform');
  604. define('TOOL_DRH', 'tool_drh');
  605. define('TOOL_STUDENT_VIEW', 'toolstudentview');
  606. define('TOOL_ADMIN_VISIBLE', 'tooladminvisible');
  607. /**
  608. * Inclusion of internationalization libraries.
  609. */
  610. require_once __DIR__.'/internationalization.lib.php';
  611. /**
  612. * Returns a path to a certain resource within the Chamilo area, specifyed through a parameter.
  613. * Also, this function provides conversion between path types, in this case the input path points inside the Chamilo area too.
  614. *
  615. * See $_configuration['course_folder'] in the configuration.php to alter the WEB_COURSE_PATH and SYS_COURSE_PATH parameters.
  616. *
  617. * @param string $path (optional) A path which type is to be converted. Also, it may be a defined constant for a path.
  618. * This parameter has meaning when $type parameter has one of the following values: TO_WEB, TO_SYS, TO_REL. Otherwise it is ignored.
  619. *
  620. * @return string the requested path or the converted path
  621. *
  622. *
  623. * Notes about the current behaviour model:
  624. * 1. Windows back-slashes are converted to slashes in the result.
  625. * 2. A semi-absolute web-path is detected by its leading slash. On Linux systems, absolute system paths start with
  626. * a slash too, so an additional check about presence of leading system server base is implemented. For example, the function is
  627. * able to distinguish type difference between /var/www/chamilo/courses/ (SYS) and /chamilo/courses/ (REL).
  628. * 3. The function api_get_path() returns only these three types of paths, which in some sense are absolute. The function has
  629. * no a mechanism for processing relative web/system paths, such as: lesson01.html, ./lesson01.html, ../css/my_styles.css.
  630. * It has not been identified as needed yet.
  631. * 4. Also, resolving the meta-symbols "." and ".." within paths has not been implemented, it is to be identified as needed.
  632. *
  633. * For examples go to: *
  634. * See main/admin/system_status.php?section=paths
  635. *
  636. * Vchamilo changes : allow using an alternate configuration
  637. * to get vchamilo instance paths
  638. */
  639. function api_get_path($path = '', $configuration = [])
  640. {
  641. global $paths;
  642. // get proper configuration data if exists
  643. global $_configuration;
  644. $emptyConfigurationParam = false;
  645. if (empty($configuration)) {
  646. $configuration = (array) $_configuration;
  647. $emptyConfigurationParam = true;
  648. }
  649. $course_folder = 'courses/';
  650. static $root_web = '';
  651. $root_sys = $_configuration['root_sys'];
  652. // If no $root_web has been set so far *and* no custom config has been passed to the function
  653. // then re-use the previously-calculated (run-specific) $root_web and skip this complex calculation
  654. if (empty($root_web) || $emptyConfigurationParam === false || empty($configuration)) {
  655. // Resolve master hostname.
  656. if (!empty($configuration) && array_key_exists('root_web', $configuration)) {
  657. $root_web = $configuration['root_web'];
  658. } else {
  659. $root_web = '';
  660. // Try guess it from server.
  661. if (defined('SYSTEM_INSTALLATION') && SYSTEM_INSTALLATION) {
  662. if (($pos = strpos(($requested_page_rel = api_get_self()), 'main/install')) !== false) {
  663. $root_rel = substr($requested_page_rel, 0, $pos);
  664. // See http://www.mediawiki.org/wiki/Manual:$wgServer
  665. $server_protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
  666. $server_name =
  667. isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME']
  668. : (isset($_SERVER['HOSTNAME']) ? $_SERVER['HOSTNAME']
  669. : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST']
  670. : (isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR']
  671. : 'localhost')));
  672. if (isset($_SERVER['SERVER_PORT']) &&
  673. !strpos($server_name, ':') &&
  674. (($server_protocol == 'http' && $_SERVER['SERVER_PORT'] != 80) ||
  675. ($server_protocol == 'https' && $_SERVER['SERVER_PORT'] != 443))
  676. ) {
  677. $server_name .= ":".$_SERVER['SERVER_PORT'];
  678. }
  679. $root_web = $server_protocol.'://'.$server_name.$root_rel;
  680. $root_sys = str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/';
  681. }
  682. // Here we give up, so we don't touch anything.
  683. }
  684. }
  685. }
  686. if (isset($configuration['multiple_access_urls']) &&
  687. $configuration['multiple_access_urls']
  688. ) {
  689. // To avoid that the api_get_access_url() function fails since global.inc.php also calls the api.lib.php.
  690. if (isset($configuration['access_url']) && !empty($configuration['access_url'])) {
  691. // We look into the DB the function api_get_access_url
  692. $urlInfo = api_get_access_url($configuration['access_url']);
  693. // Avoid default value
  694. $defaultValues = ['http://localhost/', 'https://localhost/'];
  695. if (!empty($urlInfo['url']) && !in_array($urlInfo['url'], $defaultValues)) {
  696. $root_web = $urlInfo['active'] == 1 ? $urlInfo['url'] : $configuration['root_web'];
  697. }
  698. }
  699. }
  700. $paths = [];
  701. // Initialise cache with default values.
  702. if (!array_key_exists($root_web, $paths)) {
  703. $paths[$root_web] = [
  704. WEB_PATH => '',
  705. SYS_PATH => '',
  706. REL_PATH => '',
  707. WEB_COURSE_PATH => '',
  708. SYS_COURSE_PATH => '',
  709. REL_COURSE_PATH => '',
  710. WEB_CODE_PATH => 'main/',
  711. SYS_CODE_PATH => 'main/',
  712. REL_CODE_PATH => '/main/',
  713. SYS_LANG_PATH => 'lang/',
  714. WEB_IMG_PATH => 'img/',
  715. WEB_CSS_PATH => 'web/css/',
  716. SYS_CSS_PATH => 'app/Resources/public/css/',
  717. SYS_PLUGIN_PATH => 'plugin/',
  718. WEB_PLUGIN_PATH => 'plugin/',
  719. WEB_PLUGIN_ASSET_PATH => 'public/plugins/',
  720. SYS_ARCHIVE_PATH => 'app/cache/',
  721. WEB_ARCHIVE_PATH => 'app/cache/',
  722. SYS_HOME_PATH => 'app/home/',
  723. WEB_HOME_PATH => 'app/home/',
  724. REL_HOME_PATH => 'app/home/',
  725. SYS_APP_PATH => 'app/',
  726. WEB_APP_PATH => 'app/',
  727. SYS_UPLOAD_PATH => 'app/upload/',
  728. SYS_INC_PATH => 'inc/',
  729. CONFIGURATION_PATH => 'app/config/',
  730. LIBRARY_PATH => 'inc/lib/',
  731. WEB_LIBRARY_PATH => 'inc/lib/',
  732. WEB_LIBRARY_JS_PATH => 'inc/lib/javascript/',
  733. WEB_AJAX_PATH => 'inc/ajax/',
  734. SYS_TEST_PATH => 'tests/',
  735. WEB_TEMPLATE_PATH => 'template/',
  736. SYS_TEMPLATE_PATH => 'template/',
  737. WEB_UPLOAD_PATH => 'app/upload/',
  738. WEB_PUBLIC_PATH => 'web/',
  739. SYS_PUBLIC_PATH => 'web/',
  740. WEB_FONTS_PATH => 'fonts/',
  741. SYS_FONTS_PATH => 'fonts/',
  742. ];
  743. }
  744. $isInitialized = [];
  745. $course_folder = isset($configuration['course_folder']) ? $configuration['course_folder'] : $course_folder;
  746. $root_rel = isset($configuration['url_append']) ? $configuration['url_append'] : '';
  747. if (!empty($root_rel)) {
  748. // Adds "/" to the root_rel
  749. $hasSlash = substr($configuration['url_append'], 0, 1);
  750. if ($hasSlash !== '/') {
  751. $root_rel = '/'.$root_rel;
  752. }
  753. }
  754. // Web server base and system server base.
  755. if (!array_key_exists($root_web, $isInitialized)) {
  756. // process absolute global roots
  757. if (!empty($configuration)) {
  758. $code_folder = 'main';
  759. } else {
  760. $code_folder = $paths[$root_web][REL_CODE_PATH];
  761. }
  762. // Support for the installation process.
  763. // Developers might use the function api_get_path() directly or indirectly (this is difficult to be traced), at the moment when
  764. // configuration has not been created yet. This is why this function should be upgraded to return correct results in this case.
  765. // Dealing with trailing slashes.
  766. $slashed_root_web = api_add_trailing_slash($root_web);
  767. $root_sys = api_add_trailing_slash($root_sys);
  768. $root_rel = api_add_trailing_slash($root_rel);
  769. $code_folder = api_add_trailing_slash($code_folder);
  770. $course_folder = api_add_trailing_slash($course_folder);
  771. // Initialization of a table that contains common-purpose paths.
  772. $paths[$root_web][REL_PATH] = $root_rel;
  773. $paths[$root_web][REL_COURSE_PATH] = $root_rel.$course_folder;
  774. $paths[$root_web][REL_CODE_PATH] = $root_rel.$code_folder;
  775. $paths[$root_web][REL_DEFAULT_COURSE_DOCUMENT_PATH] = $paths[$root_web][REL_PATH].'main/default_course_document/';
  776. $paths[$root_web][WEB_PATH] = $slashed_root_web;
  777. $paths[$root_web][WEB_CODE_PATH] = $paths[$root_web][WEB_PATH].$code_folder;
  778. $paths[$root_web][WEB_COURSE_PATH] = $paths[$root_web][WEB_PATH].$course_folder;
  779. $paths[$root_web][WEB_DEFAULT_COURSE_DOCUMENT_PATH] = $paths[$root_web][WEB_CODE_PATH].'default_course_document/';
  780. $paths[$root_web][WEB_APP_PATH] = $paths[$root_web][WEB_PATH].$paths[$root_web][WEB_APP_PATH];
  781. $paths[$root_web][WEB_PLUGIN_PATH] = $paths[$root_web][WEB_PATH].$paths[$root_web][WEB_PLUGIN_PATH];
  782. $paths[$root_web][WEB_PLUGIN_ASSET_PATH] = $paths[$root_web][WEB_PATH].$paths[$root_web][WEB_PLUGIN_ASSET_PATH];
  783. $paths[$root_web][WEB_ARCHIVE_PATH] = $paths[$root_web][WEB_PATH].$paths[$root_web][WEB_ARCHIVE_PATH];
  784. $paths[$root_web][WEB_CSS_PATH] = $paths[$root_web][WEB_PATH].$paths[$root_web][WEB_CSS_PATH];
  785. $paths[$root_web][WEB_IMG_PATH] = $paths[$root_web][WEB_CODE_PATH].$paths[$root_web][WEB_IMG_PATH];
  786. $paths[$root_web][WEB_LIBRARY_PATH] = $paths[$root_web][WEB_CODE_PATH].$paths[$root_web][WEB_LIBRARY_PATH];
  787. $paths[$root_web][WEB_LIBRARY_JS_PATH] = $paths[$root_web][WEB_CODE_PATH].$paths[$root_web][WEB_LIBRARY_JS_PATH];
  788. $paths[$root_web][WEB_AJAX_PATH] = $paths[$root_web][WEB_CODE_PATH].$paths[$root_web][WEB_AJAX_PATH];
  789. $paths[$root_web][WEB_FONTS_PATH] = $paths[$root_web][WEB_CODE_PATH].$paths[$root_web][WEB_FONTS_PATH];
  790. $paths[$root_web][WEB_TEMPLATE_PATH] = $paths[$root_web][WEB_CODE_PATH].$paths[$root_web][WEB_TEMPLATE_PATH];
  791. $paths[$root_web][WEB_UPLOAD_PATH] = $paths[$root_web][WEB_PATH].$paths[$root_web][WEB_UPLOAD_PATH];
  792. $paths[$root_web][WEB_PUBLIC_PATH] = $paths[$root_web][WEB_PATH].$paths[$root_web][WEB_PUBLIC_PATH];
  793. $paths[$root_web][WEB_HOME_PATH] = $paths[$root_web][WEB_PATH].$paths[$root_web][REL_HOME_PATH];
  794. $paths[$root_web][SYS_PATH] = $root_sys;
  795. $paths[$root_web][SYS_CODE_PATH] = $root_sys.$code_folder;
  796. $paths[$root_web][SYS_TEST_PATH] = $paths[$root_web][SYS_PATH].$paths[$root_web][SYS_TEST_PATH];
  797. $paths[$root_web][SYS_TEMPLATE_PATH] = $paths[$root_web][SYS_CODE_PATH].$paths[$root_web][SYS_TEMPLATE_PATH];
  798. $paths[$root_web][SYS_PUBLIC_PATH] = $paths[$root_web][SYS_PATH].$paths[$root_web][SYS_PUBLIC_PATH];
  799. $paths[$root_web][SYS_CSS_PATH] = $paths[$root_web][SYS_PATH].$paths[$root_web][SYS_CSS_PATH];
  800. $paths[$root_web][SYS_FONTS_PATH] = $paths[$root_web][SYS_CODE_PATH].$paths[$root_web][SYS_FONTS_PATH];
  801. $paths[$root_web][SYS_ARCHIVE_PATH] = $paths[$root_web][SYS_PATH].$paths[$root_web][SYS_ARCHIVE_PATH];
  802. $paths[$root_web][SYS_APP_PATH] = $paths[$root_web][SYS_PATH].$paths[$root_web][SYS_APP_PATH];
  803. $paths[$root_web][SYS_COURSE_PATH] = $paths[$root_web][SYS_APP_PATH].$course_folder;
  804. $paths[$root_web][SYS_UPLOAD_PATH] = $paths[$root_web][SYS_PATH].$paths[$root_web][SYS_UPLOAD_PATH];
  805. $paths[$root_web][SYS_LANG_PATH] = $paths[$root_web][SYS_CODE_PATH].$paths[$root_web][SYS_LANG_PATH];
  806. $paths[$root_web][SYS_HOME_PATH] = $paths[$root_web][SYS_PATH].$paths[$root_web][SYS_HOME_PATH];
  807. $paths[$root_web][SYS_PLUGIN_PATH] = $paths[$root_web][SYS_PATH].$paths[$root_web][SYS_PLUGIN_PATH];
  808. $paths[$root_web][SYS_INC_PATH] = $paths[$root_web][SYS_CODE_PATH].$paths[$root_web][SYS_INC_PATH];
  809. $paths[$root_web][LIBRARY_PATH] = $paths[$root_web][SYS_CODE_PATH].$paths[$root_web][LIBRARY_PATH];
  810. $paths[$root_web][CONFIGURATION_PATH] = $paths[$root_web][SYS_PATH].$paths[$root_web][CONFIGURATION_PATH];
  811. global $virtualChamilo;
  812. if (!empty($virtualChamilo)) {
  813. $paths[$root_web][SYS_ARCHIVE_PATH] = api_add_trailing_slash($virtualChamilo[SYS_ARCHIVE_PATH]);
  814. $paths[$root_web][SYS_HOME_PATH] = api_add_trailing_slash($virtualChamilo[SYS_HOME_PATH]);
  815. $paths[$root_web][SYS_COURSE_PATH] = api_add_trailing_slash($virtualChamilo[SYS_COURSE_PATH]);
  816. $paths[$root_web][SYS_UPLOAD_PATH] = api_add_trailing_slash($virtualChamilo[SYS_UPLOAD_PATH]);
  817. $paths[$root_web][WEB_HOME_PATH] = api_add_trailing_slash($virtualChamilo[WEB_HOME_PATH]);
  818. $paths[$root_web][WEB_UPLOAD_PATH] = api_add_trailing_slash($virtualChamilo[WEB_UPLOAD_PATH]);
  819. $paths[$root_web][WEB_ARCHIVE_PATH] = api_add_trailing_slash($virtualChamilo[WEB_ARCHIVE_PATH]);
  820. //$paths[$root_web][WEB_COURSE_PATH] = api_add_trailing_slash($virtualChamilo[WEB_COURSE_PATH]);
  821. // WEB_UPLOAD_PATH should be handle by apache htaccess in the vhost
  822. // RewriteEngine On
  823. // RewriteRule /app/upload/(.*)$ http://localhost/other/upload/my-chamilo111-net/$1 [QSA,L]
  824. //$paths[$root_web][WEB_UPLOAD_PATH] = api_add_trailing_slash($virtualChamilo[WEB_UPLOAD_PATH]);
  825. //$paths[$root_web][REL_PATH] = $virtualChamilo[REL_PATH];
  826. //$paths[$root_web][REL_COURSE_PATH] = $virtualChamilo[REL_COURSE_PATH];
  827. }
  828. $isInitialized[$root_web] = true;
  829. }
  830. $path = trim($path);
  831. // Retrieving a common-purpose path.
  832. if (isset($paths[$root_web][$path])) {
  833. return $paths[$root_web][$path];
  834. }
  835. // Second purification.
  836. // Replacing Windows back slashes.
  837. $path = str_replace('\\', '/', $path);
  838. // Query strings sometimes mighth wrongly appear in non-URLs.
  839. // Let us check remove them from all types of paths.
  840. if (($pos = strpos($path, '?')) !== false) {
  841. $path = substr($path, 0, $pos);
  842. }
  843. // Detection of the input path type. Conversion to semi-absolute type ( /chamilo/main/inc/.... ).
  844. if (preg_match(VALID_WEB_PATH, $path)) {
  845. // A special case: When a URL points to the document download script directly, without
  846. // mod-rewrite translation, we have to translate it into an "ordinary" web path.
  847. // For example:
  848. // http://localhost/chamilo/main/document/download.php?doc_url=/image.png&cDir=/
  849. // becomes
  850. // http://localhost/chamilo/courses/TEST/document/image.png
  851. // TEST is a course directory name, so called "system course code".
  852. if (strpos($path, 'download.php') !== false) { // Fast detection first.
  853. $path = urldecode($path);
  854. if (preg_match('/(.*)main\/document\/download.php\?doc_url=\/(.*)&cDir=\/(.*)?/', $path, $matches)) {
  855. $sys_course_code =
  856. isset($_SESSION['_course']['sysCode']) // User is inside a course?
  857. ? $_SESSION['_course']['sysCode'] // Yes, then use course's directory name.
  858. : '{SYS_COURSE_CODE}'; // No, then use a fake code, it may be processed later.
  859. $path = $matches[1].'courses/'.$sys_course_code.'/document/'.str_replace('//', '/', $matches[3].'/'.$matches[2]);
  860. }
  861. }
  862. // Replacement of the present web server base with a slash '/'.
  863. $path = preg_replace(VALID_WEB_SERVER_BASE, '/', $path);
  864. }
  865. // Path now is semi-absolute. It is convenient at this moment repeated slashes to be removed.
  866. $path = preg_replace(REPEATED_SLASHES_PURIFIER, '/', $path);
  867. return $path;
  868. }
  869. /**
  870. * Gets a modified version of the path for the CDN, if defined in
  871. * configuration.php.
  872. *
  873. * @param string $web_path The path of the resource without CDN
  874. *
  875. * @return string The path of the resource converted to CDN
  876. *
  877. * @author Yannick Warnier <ywarnier@beeznst.org>
  878. */
  879. function api_get_cdn_path($web_path)
  880. {
  881. global $_configuration;
  882. if (!empty($_configuration['cdn_enable'])) {
  883. $web_root = api_get_path(WEB_PATH);
  884. $ext = substr($web_path, strrpos($web_path, '.'));
  885. if (isset($ext[2])) { // faster version of strlen to check if len>2
  886. // Check for CDN definitions
  887. if (!empty($ext)) {
  888. foreach ($_configuration['cdn'] as $host => $exts) {
  889. if (in_array($ext, $exts)) {
  890. //Use host as defined in $_configuration['cdn'], without
  891. // trailing slash
  892. return str_replace($web_root, $host.'/', $web_path);
  893. }
  894. }
  895. }
  896. }
  897. }
  898. return $web_path;
  899. }
  900. /**
  901. * @return bool Return true if CAS authentification is activated
  902. */
  903. function api_is_cas_activated()
  904. {
  905. return api_get_setting('cas_activate') == "true";
  906. }
  907. /**
  908. * @return bool Return true if LDAP authentification is activated
  909. */
  910. function api_is_ldap_activated()
  911. {
  912. global $extAuthSource;
  913. return is_array($extAuthSource[LDAP_AUTH_SOURCE]);
  914. }
  915. /**
  916. * @return bool Return true if Facebook authentification is activated
  917. */
  918. function api_is_facebook_auth_activated()
  919. {
  920. global $_configuration;
  921. return isset($_configuration['facebook_auth']) && $_configuration['facebook_auth'] == 1;
  922. }
  923. /**
  924. * Adds to a given path a trailing slash if it is necessary (adds "/" character at the end of the string).
  925. *
  926. * @param string $path the input path
  927. *
  928. * @return string returns the modified path
  929. */
  930. function api_add_trailing_slash($path)
  931. {
  932. return substr($path, -1) == '/' ? $path : $path.'/';
  933. // This code is about 20% faster than the preg_replace equivalent
  934. //return preg_replace('/([^\/])$/', '$1/', $path);
  935. }
  936. /**
  937. * Removes from a given path the trailing slash if it is necessary (removes "/" character from the end of the string).
  938. *
  939. * @param string $path the input path
  940. *
  941. * @return string returns the modified path
  942. */
  943. function api_remove_trailing_slash($path)
  944. {
  945. return substr($path, -1) == '/' ? substr($path, 0, -1) : $path;
  946. }
  947. /**
  948. * Checks the RFC 3986 syntax of a given URL.
  949. *
  950. * @param string $url the URL to be checked
  951. * @param bool $absolute whether the URL is absolute (beginning with a scheme such as "http:")
  952. *
  953. * @return string|false Returns the URL if it is valid, FALSE otherwise.
  954. * This function is an adaptation from the function valid_url(), Drupal CMS.
  955. *
  956. * @see http://drupal.org
  957. * Note: The built-in function filter_var($urs, FILTER_VALIDATE_URL) has a bug for some versions of PHP.
  958. * @see http://bugs.php.net/51192
  959. */
  960. function api_valid_url($url, $absolute = false)
  961. {
  962. if ($absolute) {
  963. if (preg_match("
  964. /^ # Start at the beginning of the text
  965. (?:ftp|https?|feed):\/\/ # Look for ftp, http, https or feed schemes
  966. (?: # Userinfo (optional) which is typically
  967. (?:(?:[\w\.\-\+!$&'\(\)*\+,;=]|%[0-9a-f]{2})+:)* # a username or a username and password
  968. (?:[\w\.\-\+%!$&'\(\)*\+,;=]|%[0-9a-f]{2})+@ # combination
  969. )?
  970. (?:
  971. (?:[a-z0-9\-\.]|%[0-9a-f]{2})+ # A domain name or a IPv4 address
  972. |(?:\[(?:[0-9a-f]{0,4}:)*(?:[0-9a-f]{0,4})\]) # or a well formed IPv6 address
  973. )
  974. (?::[0-9]+)? # Server port number (optional)
  975. (?:[\/|\?]
  976. (?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2}) # The path and query (optional)
  977. *)?
  978. $/xi", $url)) {
  979. return $url;
  980. }
  981. return false;
  982. } else {
  983. return preg_match("/^(?:[\w#!:\.\?\+=&@$'~*,;\/\(\)\[\]\-]|%[0-9a-f]{2})+$/i", $url) ? $url : false;
  984. }
  985. }
  986. /**
  987. * Checks whether a given string looks roughly like an email address.
  988. *
  989. * @param string $address the e-mail address to be checked
  990. *
  991. * @return mixed returns the e-mail if it is valid, FALSE otherwise
  992. */
  993. function api_valid_email($address)
  994. {
  995. return filter_var($address, FILTER_VALIDATE_EMAIL);
  996. }
  997. /* PROTECTION FUNCTIONS
  998. Use these functions to protect your scripts. */
  999. /**
  1000. * Function used to protect a course script.
  1001. * The function blocks access when
  1002. * - there is no $_SESSION["_course"] defined; or
  1003. * - $is_allowed_in_course is set to false (this depends on the course
  1004. * visibility and user status).
  1005. *
  1006. * This is only the first proposal, test and improve!
  1007. *
  1008. * @param bool Option to print headers when displaying error message. Default: false
  1009. * @param bool whether session admins should be allowed or not
  1010. *
  1011. * @return bool True if the user has access to the current course or is out of a course context, false otherwise
  1012. *
  1013. * @todo replace global variable
  1014. *
  1015. * @author Roan Embrechts
  1016. */
  1017. function api_protect_course_script($print_headers = false, $allow_session_admins = false, $allow_drh = false)
  1018. {
  1019. $course_info = api_get_course_info();
  1020. if (empty($course_info)) {
  1021. api_not_allowed($print_headers);
  1022. return false;
  1023. }
  1024. if (api_is_drh()) {
  1025. return true;
  1026. }
  1027. // Session admin has access to course
  1028. $sessionAccess = api_get_configuration_value('session_admins_access_all_content');
  1029. if ($sessionAccess) {
  1030. $allow_session_admins = true;
  1031. }
  1032. if (api_is_platform_admin($allow_session_admins)) {
  1033. return true;
  1034. }
  1035. $is_allowed_in_course = api_is_allowed_in_course();
  1036. $is_visible = false;
  1037. if (isset($course_info) && isset($course_info['visibility'])) {
  1038. switch ($course_info['visibility']) {
  1039. default:
  1040. case COURSE_VISIBILITY_CLOSED:
  1041. // Completely closed: the course is only accessible to the teachers. - 0
  1042. if (api_get_user_id() && !api_is_anonymous() && $is_allowed_in_course) {
  1043. $is_visible = true;
  1044. }
  1045. break;
  1046. case COURSE_VISIBILITY_REGISTERED:
  1047. // Private - access authorized to course members only - 1
  1048. if (api_get_user_id() && !api_is_anonymous() && $is_allowed_in_course) {
  1049. $is_visible = true;
  1050. }
  1051. break;
  1052. case COURSE_VISIBILITY_OPEN_PLATFORM:
  1053. // Open - access allowed for users registered on the platform - 2
  1054. if (api_get_user_id() && !api_is_anonymous() && $is_allowed_in_course) {
  1055. $is_visible = true;
  1056. }
  1057. break;
  1058. case COURSE_VISIBILITY_OPEN_WORLD:
  1059. //Open - access allowed for the whole world - 3
  1060. $is_visible = true;
  1061. break;
  1062. case COURSE_VISIBILITY_HIDDEN:
  1063. //Completely closed: the course is only accessible to the teachers. - 0
  1064. if (api_is_platform_admin()) {
  1065. $is_visible = true;
  1066. }
  1067. break;
  1068. }
  1069. //If password is set and user is not registered to the course then the course is not visible
  1070. if ($is_allowed_in_course == false &&
  1071. isset($course_info['registration_code']) &&
  1072. !empty($course_info['registration_code'])
  1073. ) {
  1074. $is_visible = false;
  1075. }
  1076. }
  1077. // Check session visibility
  1078. $session_id = api_get_session_id();
  1079. if (!empty($session_id)) {
  1080. //$is_allowed_in_course was set in local.inc.php
  1081. if (!$is_allowed_in_course) {
  1082. $is_visible = false;
  1083. }
  1084. }
  1085. if (!$is_visible) {
  1086. api_not_allowed($print_headers);
  1087. return false;
  1088. }
  1089. return true;
  1090. }
  1091. /**
  1092. * Function used to protect an admin script.
  1093. *
  1094. * The function blocks access when the user has no platform admin rights
  1095. * with an error message printed on default output
  1096. *
  1097. * @param bool Whether to allow session admins as well
  1098. * @param bool Whether to allow HR directors as well
  1099. * @param string An optional message (already passed through get_lang)
  1100. *
  1101. * @return bool True if user is allowed, false otherwise.
  1102. * The function also outputs an error message in case not allowed
  1103. *
  1104. * @author Roan Embrechts (original author)
  1105. */
  1106. function api_protect_admin_script($allow_sessions_admins = false, $allow_drh = false, $message = null)
  1107. {
  1108. if (!api_is_platform_admin($allow_sessions_admins, $allow_drh)) {
  1109. api_not_allowed(true, $message);
  1110. return false;
  1111. }
  1112. return true;
  1113. }
  1114. /**
  1115. * Function used to protect a teacher script.
  1116. * The function blocks access when the user has no teacher rights.
  1117. *
  1118. * @return bool True if the current user can access the script, false otherwise
  1119. *
  1120. * @author Yoselyn Castillo
  1121. */
  1122. function api_protect_teacher_script()
  1123. {
  1124. if (!api_is_allowed_to_edit()) {
  1125. api_not_allowed(true);
  1126. return false;
  1127. }
  1128. return true;
  1129. }
  1130. /**
  1131. * Function used to prevent anonymous users from accessing a script.
  1132. *
  1133. * @param bool|true $printHeaders
  1134. *
  1135. * @author Roan Embrechts
  1136. *
  1137. * @return bool
  1138. */
  1139. function api_block_anonymous_users($printHeaders = true)
  1140. {
  1141. $user = api_get_user_info();
  1142. if (!(isset($user['user_id']) && $user['user_id']) || api_is_anonymous($user['user_id'], true)) {
  1143. api_not_allowed($printHeaders);
  1144. return false;
  1145. }
  1146. return true;
  1147. }
  1148. /**
  1149. * Returns a rough evaluation of the browser's name and version based on very
  1150. * simple regexp.
  1151. *
  1152. * @return array with the navigator name and version ['name' => '...', 'version' => '...']
  1153. */
  1154. function api_get_navigator()
  1155. {
  1156. $navigator = 'Unknown';
  1157. $version = 0;
  1158. if (!isset($_SERVER['HTTP_USER_AGENT'])) {
  1159. return ['name' => 'Unknown', 'version' => '0.0.0'];
  1160. }
  1161. if (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false) {
  1162. $navigator = 'Opera';
  1163. list(, $version) = explode('Opera', $_SERVER['HTTP_USER_AGENT']);
  1164. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Edge') !== false) {
  1165. $navigator = 'Edge';
  1166. list(, $version) = explode('Edge', $_SERVER['HTTP_USER_AGENT']);
  1167. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
  1168. $navigator = 'Internet Explorer';
  1169. list(, $version) = explode('MSIE ', $_SERVER['HTTP_USER_AGENT']);
  1170. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== false) {
  1171. $navigator = 'Chrome';
  1172. list(, $version) = explode('Chrome', $_SERVER['HTTP_USER_AGENT']);
  1173. } elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== false) {
  1174. $navigator = 'Safari';
  1175. if (stripos($_SERVER['HTTP_USER_AGENT'], 'Version/') !== false) {
  1176. // If this Safari does have the "Version/" string in its user agent
  1177. // then use that as a version indicator rather than what's after
  1178. // "Safari/" which is rather a "build number" or something
  1179. list(, $version) = explode('Version/', $_SERVER['HTTP_USER_AGENT']);
  1180. } else {
  1181. list(, $version) = explode('Safari/', $_SERVER['HTTP_USER_AGENT']);
  1182. }
  1183. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== false) {
  1184. $navigator = 'Firefox';
  1185. list(, $version) = explode('Firefox', $_SERVER['HTTP_USER_AGENT']);
  1186. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Netscape') !== false) {
  1187. $navigator = 'Netscape';
  1188. if (stripos($_SERVER['HTTP_USER_AGENT'], 'Netscape/') !== false) {
  1189. list(, $version) = explode('Netscape', $_SERVER['HTTP_USER_AGENT']);
  1190. } else {
  1191. list(, $version) = explode('Navigator', $_SERVER['HTTP_USER_AGENT']);
  1192. }
  1193. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Konqueror') !== false) {
  1194. $navigator = 'Konqueror';
  1195. list(, $version) = explode('Konqueror', $_SERVER['HTTP_USER_AGENT']);
  1196. } elseif (stripos($_SERVER['HTTP_USER_AGENT'], 'applewebkit') !== false) {
  1197. $navigator = 'AppleWebKit';
  1198. list(, $version) = explode('Version/', $_SERVER['HTTP_USER_AGENT']);
  1199. } elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false) {
  1200. $navigator = 'Mozilla';
  1201. list(, $version) = explode('; rv:', $_SERVER['HTTP_USER_AGENT']);
  1202. }
  1203. // Now cut extra stuff around (mostly *after*) the version number
  1204. $version = preg_replace('/^([\/\s])?([\d\.]+)?.*/', '\2', $version);
  1205. if (strpos($version, '.') === false) {
  1206. $version = number_format(doubleval($version), 1);
  1207. }
  1208. $return = ['name' => $navigator, 'version' => $version];
  1209. return $return;
  1210. }
  1211. /**
  1212. * @return true if user self registration is allowed, false otherwise
  1213. */
  1214. function api_is_self_registration_allowed()
  1215. {
  1216. return isset($GLOBALS['allowSelfReg']) ? $GLOBALS['allowSelfReg'] : false;
  1217. }
  1218. /**
  1219. * This function returns the id of the user which is stored in the $_user array.
  1220. *
  1221. * example: The function can be used to check if a user is logged in
  1222. * if (api_get_user_id())
  1223. *
  1224. * @return int the id of the current user, 0 if is empty
  1225. */
  1226. function api_get_user_id()
  1227. {
  1228. $userInfo = Session::read('_user');
  1229. if ($userInfo && isset($userInfo['user_id'])) {
  1230. return (int) $userInfo['user_id'];
  1231. }
  1232. return 0;
  1233. }
  1234. /**
  1235. * Gets the list of courses a specific user is subscribed to.
  1236. *
  1237. * @param int User ID
  1238. * @param bool $fetch_session Whether to get session courses or not - NOT YET IMPLEMENTED
  1239. *
  1240. * @return array Array of courses in the form [0]=>('code'=>xxx,'db'=>xxx,'dir'=>xxx,'status'=>d)
  1241. *
  1242. * @deprecated use CourseManager::get_courses_list_by_user_id()
  1243. */
  1244. function api_get_user_courses($userId, $fetch_session = true)
  1245. {
  1246. // Get out if not integer
  1247. if ($userId != strval(intval($userId))) {
  1248. return [];
  1249. }
  1250. $t_course = Database::get_main_table(TABLE_MAIN_COURSE);
  1251. $t_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1252. $sql = "SELECT cc.id as real_id, cc.code code, cc.directory dir, cu.status status
  1253. FROM $t_course cc, $t_course_user cu
  1254. WHERE
  1255. cc.id = cu.c_id AND
  1256. cu.user_id = $userId AND
  1257. cu.relation_type <> ".COURSE_RELATION_TYPE_RRHH;
  1258. $result = Database::query($sql);
  1259. if ($result === false) {
  1260. return [];
  1261. }
  1262. $courses = [];
  1263. while ($row = Database::fetch_array($result)) {
  1264. // we only need the database name of the course
  1265. $courses[] = $row;
  1266. }
  1267. return $courses;
  1268. }
  1269. /**
  1270. * Formats user information into a standard array
  1271. * This function should be only used inside api_get_user_info().
  1272. *
  1273. * @param array Non-standard user array
  1274. * @param bool $add_password
  1275. * @param bool $loadAvatars turn off to improve performance
  1276. *
  1277. * @return array Standard user array
  1278. */
  1279. function _api_format_user($user, $add_password = false, $loadAvatars = true)
  1280. {
  1281. $result = [];
  1282. $result['firstname'] = null;
  1283. $result['lastname'] = null;
  1284. if (isset($user['firstname']) && isset($user['lastname'])) { // with only lowercase
  1285. $result['firstname'] = $user['firstname'];
  1286. $result['lastname'] = $user['lastname'];
  1287. } elseif (isset($user['firstName']) && isset($user['lastName'])) { // with uppercase letters
  1288. $result['firstname'] = isset($user['firstName']) ? $user['firstName'] : null;
  1289. $result['lastname'] = isset($user['lastName']) ? $user['lastName'] : null;
  1290. }
  1291. if (isset($user['email'])) {
  1292. $result['mail'] = isset($user['email']) ? $user['email'] : null;
  1293. $result['email'] = isset($user['email']) ? $user['email'] : null;
  1294. } else {
  1295. $result['mail'] = isset($user['mail']) ? $user['mail'] : null;
  1296. $result['email'] = isset($user['mail']) ? $user['mail'] : null;
  1297. }
  1298. $result['complete_name'] = api_get_person_name($result['firstname'], $result['lastname']);
  1299. $result['complete_name_with_username'] = $result['complete_name'];
  1300. if (!empty($user['username']) && !api_get_configuration_value('hide_username_with_complete_name')) {
  1301. $result['complete_name_with_username'] = $result['complete_name'].' ('.$user['username'].')';
  1302. }
  1303. $showEmail = api_get_setting('show_email_addresses') === 'true';
  1304. if (!empty($user['email'])) {
  1305. $result['complete_name_with_email_forced'] = $result['complete_name'].' ('.$user['email'].')';
  1306. if ($showEmail) {
  1307. $result['complete_name_with_email'] = $result['complete_name'].' ('.$user['email'].')';
  1308. }
  1309. } else {
  1310. $result['complete_name_with_email'] = $result['complete_name'];
  1311. $result['complete_name_with_email_forced'] = $result['complete_name'];
  1312. }
  1313. // Kept for historical reasons
  1314. $result['firstName'] = $result['firstname'];
  1315. $result['lastName'] = $result['lastname'];
  1316. $attributes = [
  1317. 'phone',
  1318. 'address',
  1319. 'picture_uri',
  1320. 'official_code',
  1321. 'status',
  1322. 'active',
  1323. 'auth_source',
  1324. 'username',
  1325. 'theme',
  1326. 'language',
  1327. 'creator_id',
  1328. 'registration_date',
  1329. 'hr_dept_id',
  1330. 'expiration_date',
  1331. 'last_login',
  1332. 'user_is_online',
  1333. ];
  1334. if (api_get_setting('extended_profile') === 'true') {
  1335. $attributes[] = 'competences';
  1336. $attributes[] = 'diplomas';
  1337. $attributes[] = 'teach';
  1338. $attributes[] = 'openarea';
  1339. }
  1340. foreach ($attributes as $attribute) {
  1341. $result[$attribute] = isset($user[$attribute]) ? $user[$attribute] : null;
  1342. }
  1343. $user_id = (int) $user['user_id'];
  1344. // Maintain the user_id index for backwards compatibility
  1345. $result['user_id'] = $result['id'] = $user_id;
  1346. $hasCertificates = Certificate::getCertificateByUser($user_id);
  1347. $result['has_certificates'] = 0;
  1348. if (!empty($hasCertificates)) {
  1349. $result['has_certificates'] = 1;
  1350. }
  1351. $result['icon_status'] = '';
  1352. $result['icon_status_medium'] = '';
  1353. $result['is_admin'] = UserManager::is_admin($user_id);
  1354. // Getting user avatar.
  1355. if ($loadAvatars) {
  1356. $result['avatar'] = '';
  1357. $result['avatar_no_query'] = '';
  1358. $result['avatar_small'] = '';
  1359. $result['avatar_medium'] = '';
  1360. if (!isset($user['avatar'])) {
  1361. $originalFile = UserManager::getUserPicture(
  1362. $user_id,
  1363. USER_IMAGE_SIZE_ORIGINAL,
  1364. null,
  1365. $result
  1366. );
  1367. $result['avatar'] = $originalFile;
  1368. $avatarString = explode('?', $result['avatar']);
  1369. $result['avatar_no_query'] = reset($avatarString);
  1370. } else {
  1371. $result['avatar'] = $user['avatar'];
  1372. $avatarString = explode('?', $user['avatar']);
  1373. $result['avatar_no_query'] = reset($avatarString);
  1374. }
  1375. if (!isset($user['avatar_small'])) {
  1376. $smallFile = UserManager::getUserPicture(
  1377. $user_id,
  1378. USER_IMAGE_SIZE_SMALL,
  1379. null,
  1380. $result
  1381. );
  1382. $result['avatar_small'] = $smallFile;
  1383. } else {
  1384. $result['avatar_small'] = $user['avatar_small'];
  1385. }
  1386. if (!isset($user['avatar_medium'])) {
  1387. $mediumFile = UserManager::getUserPicture(
  1388. $user_id,
  1389. USER_IMAGE_SIZE_MEDIUM,
  1390. null,
  1391. $result
  1392. );
  1393. $result['avatar_medium'] = $mediumFile;
  1394. } else {
  1395. $result['avatar_medium'] = $user['avatar_medium'];
  1396. }
  1397. $urlImg = api_get_path(WEB_IMG_PATH);
  1398. $iconStatus = '';
  1399. $iconStatusMedium = '';
  1400. switch ($result['status']) {
  1401. case STUDENT:
  1402. if ($result['has_certificates']) {
  1403. $iconStatus = $urlImg.'icons/svg/identifier_graduated.svg';
  1404. } else {
  1405. $iconStatus = $urlImg.'icons/svg/identifier_student.svg';
  1406. }
  1407. break;
  1408. case COURSEMANAGER:
  1409. if ($result['is_admin']) {
  1410. $iconStatus = $urlImg.'icons/svg/identifier_admin.svg';
  1411. } else {
  1412. $iconStatus = $urlImg.'icons/svg/identifier_teacher.svg';
  1413. }
  1414. break;
  1415. case STUDENT_BOSS:
  1416. $iconStatus = $urlImg.'icons/svg/identifier_teacher.svg';
  1417. break;
  1418. }
  1419. if (!empty($iconStatus)) {
  1420. $iconStatusMedium = '<img src="'.$iconStatus.'" width="32px" height="32px">';
  1421. $iconStatus = '<img src="'.$iconStatus.'" width="22px" height="22px">';
  1422. }
  1423. $result['icon_status'] = $iconStatus;
  1424. $result['icon_status_medium'] = $iconStatusMedium;
  1425. }
  1426. if (isset($user['user_is_online'])) {
  1427. $result['user_is_online'] = $user['user_is_online'] == true ? 1 : 0;
  1428. }
  1429. if (isset($user['user_is_online_in_chat'])) {
  1430. $result['user_is_online_in_chat'] = (int) $user['user_is_online_in_chat'];
  1431. }
  1432. if ($add_password) {
  1433. $result['password'] = $user['password'];
  1434. }
  1435. if (isset($result['profile_completed'])) {
  1436. $result['profile_completed'] = $user['profile_completed'];
  1437. }
  1438. $result['profile_url'] = api_get_path(WEB_CODE_PATH).'social/profile.php?u='.$user_id;
  1439. // Send message link
  1440. $sendMessage = api_get_path(WEB_AJAX_PATH).'user_manager.ajax.php?a=get_user_popup&user_id='.$user_id;
  1441. $result['complete_name_with_message_link'] = Display::url(
  1442. $result['complete_name_with_username'],
  1443. $sendMessage,
  1444. ['class' => 'ajax']
  1445. );
  1446. if (isset($user['extra'])) {
  1447. $result['extra'] = $user['extra'];
  1448. }
  1449. return $result;
  1450. }
  1451. /**
  1452. * Finds all the information about a user.
  1453. * If no parameter is passed you find all the information about the current user.
  1454. *
  1455. * @param int $user_id
  1456. * @param bool $checkIfUserOnline
  1457. * @param bool $showPassword
  1458. * @param bool $loadExtraData
  1459. * @param bool $loadOnlyVisibleExtraData Get the user extra fields that are visible
  1460. * @param bool $loadAvatars turn off to improve performance and if avatars are not needed
  1461. * @param bool $updateCache update apc cache if exists
  1462. *
  1463. * @return mixed $user_info user_id, lastname, firstname, username, email, etc or false on error
  1464. *
  1465. * @author Patrick Cool <patrick.cool@UGent.be>
  1466. * @author Julio Montoya
  1467. *
  1468. * @version 21 September 2004
  1469. */
  1470. function api_get_user_info(
  1471. $user_id = 0,
  1472. $checkIfUserOnline = false,
  1473. $showPassword = false,
  1474. $loadExtraData = false,
  1475. $loadOnlyVisibleExtraData = false,
  1476. $loadAvatars = true,
  1477. $updateCache = false
  1478. ) {
  1479. $apcVar = null;
  1480. $user = false;
  1481. $cacheAvailable = api_get_configuration_value('apc');
  1482. if (empty($user_id)) {
  1483. $userFromSession = Session::read('_user');
  1484. if (isset($userFromSession)) {
  1485. if ($cacheAvailable === true &&
  1486. (
  1487. empty($userFromSession['is_anonymous']) &&
  1488. (isset($userFromSession['status']) && $userFromSession['status'] != ANONYMOUS)
  1489. )
  1490. ) {
  1491. $apcVar = api_get_configuration_value('apc_prefix').'userinfo_'.$userFromSession['user_id'];
  1492. if (apcu_exists($apcVar)) {
  1493. if ($updateCache) {
  1494. apcu_store($apcVar, $userFromSession, 60);
  1495. }
  1496. $user = apcu_fetch($apcVar);
  1497. } else {
  1498. $user = _api_format_user(
  1499. $userFromSession,
  1500. $showPassword,
  1501. $loadAvatars
  1502. );
  1503. apcu_store($apcVar, $user, 60);
  1504. }
  1505. } else {
  1506. $user = _api_format_user(
  1507. $userFromSession,
  1508. $showPassword,
  1509. $loadAvatars
  1510. );
  1511. }
  1512. return $user;
  1513. }
  1514. return false;
  1515. }
  1516. // Make sure user_id is safe
  1517. $user_id = (int) $user_id;
  1518. // Re-use user information if not stale and already stored in APCu
  1519. if ($cacheAvailable === true) {
  1520. $apcVar = api_get_configuration_value('apc_prefix').'userinfo_'.$user_id;
  1521. if (apcu_exists($apcVar) && $updateCache == false && $checkIfUserOnline == false) {
  1522. $user = apcu_fetch($apcVar);
  1523. return $user;
  1524. }
  1525. }
  1526. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_USER)."
  1527. WHERE id = $user_id";
  1528. $result = Database::query($sql);
  1529. if (Database::num_rows($result) > 0) {
  1530. $result_array = Database::fetch_array($result);
  1531. $result_array['user_is_online_in_chat'] = 0;
  1532. if ($checkIfUserOnline) {
  1533. $use_status_in_platform = user_is_online($user_id);
  1534. $result_array['user_is_online'] = $use_status_in_platform;
  1535. $user_online_in_chat = 0;
  1536. if ($use_status_in_platform) {
  1537. $user_status = UserManager::get_extra_user_data_by_field(
  1538. $user_id,
  1539. 'user_chat_status',
  1540. false,
  1541. true
  1542. );
  1543. if ((int) $user_status['user_chat_status'] == 1) {
  1544. $user_online_in_chat = 1;
  1545. }
  1546. }
  1547. $result_array['user_is_online_in_chat'] = $user_online_in_chat;
  1548. }
  1549. if ($loadExtraData) {
  1550. $fieldValue = new ExtraFieldValue('user');
  1551. $result_array['extra'] = $fieldValue->getAllValuesForAnItem(
  1552. $user_id,
  1553. $loadOnlyVisibleExtraData
  1554. );
  1555. }
  1556. $user = _api_format_user($result_array, $showPassword, $loadAvatars);
  1557. }
  1558. if ($cacheAvailable === true) {
  1559. apcu_store($apcVar, $user, 60);
  1560. }
  1561. return $user;
  1562. }
  1563. /**
  1564. * @param int $userId
  1565. *
  1566. * @return User
  1567. */
  1568. function api_get_user_entity($userId)
  1569. {
  1570. $userId = (int) $userId;
  1571. $repo = UserManager::getRepository();
  1572. /** @var User $user */
  1573. $user = $repo->find($userId);
  1574. return $user;
  1575. }
  1576. /**
  1577. * Finds all the information about a user from username instead of user id.
  1578. *
  1579. * @param string $username
  1580. *
  1581. * @return mixed $user_info array user_id, lastname, firstname, username, email or false on error
  1582. *
  1583. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  1584. */
  1585. function api_get_user_info_from_username($username = '')
  1586. {
  1587. if (empty($username)) {
  1588. return false;
  1589. }
  1590. $username = trim($username);
  1591. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_USER)."
  1592. WHERE username='".Database::escape_string($username)."'";
  1593. $result = Database::query($sql);
  1594. if (Database::num_rows($result) > 0) {
  1595. $resultArray = Database::fetch_array($result);
  1596. return _api_format_user($resultArray);
  1597. }
  1598. return false;
  1599. }
  1600. /**
  1601. * Get first user with an email.
  1602. *
  1603. * @param string $email
  1604. *
  1605. * @return array|bool
  1606. */
  1607. function api_get_user_info_from_email($email = '')
  1608. {
  1609. if (empty($email)) {
  1610. return false;
  1611. }
  1612. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_USER)."
  1613. WHERE email ='".Database::escape_string($email)."' LIMIT 1";
  1614. $result = Database::query($sql);
  1615. if (Database::num_rows($result) > 0) {
  1616. $resultArray = Database::fetch_array($result);
  1617. return _api_format_user($resultArray);
  1618. }
  1619. return false;
  1620. }
  1621. /**
  1622. * @return string
  1623. */
  1624. function api_get_course_id()
  1625. {
  1626. return Session::read('_cid', null);
  1627. }
  1628. /**
  1629. * Returns the current course id (integer).
  1630. *
  1631. * @param string $code Optional course code
  1632. *
  1633. * @return int
  1634. */
  1635. function api_get_course_int_id($code = null)
  1636. {
  1637. if (!empty($code)) {
  1638. $code = Database::escape_string($code);
  1639. $row = Database::select(
  1640. 'id',
  1641. Database::get_main_table(TABLE_MAIN_COURSE),
  1642. ['where' => ['code = ?' => [$code]]],
  1643. 'first'
  1644. );
  1645. if (is_array($row) && isset($row['id'])) {
  1646. return $row['id'];
  1647. } else {
  1648. return false;
  1649. }
  1650. }
  1651. return Session::read('_real_cid', 0);
  1652. }
  1653. /**
  1654. * Returns the current course directory.
  1655. *
  1656. * This function relies on api_get_course_info()
  1657. *
  1658. * @param string The course code - optional (takes it from session if not given)
  1659. *
  1660. * @return string The directory where the course is located inside the Chamilo "courses" directory
  1661. *
  1662. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  1663. */
  1664. function api_get_course_path($course_code = null)
  1665. {
  1666. $info = !empty($course_code) ? api_get_course_info($course_code) : api_get_course_info();
  1667. return $info['path'];
  1668. }
  1669. /**
  1670. * Gets a course setting from the current course_setting table. Try always using integer values.
  1671. *
  1672. * @param string $setting_name The name of the setting we want from the table
  1673. * @param string $course_code
  1674. *
  1675. * @return mixed The value of that setting in that table. Return -1 if not found.
  1676. */
  1677. function api_get_course_setting($setting_name, $course_code = null)
  1678. {
  1679. $course_info = api_get_course_info($course_code);
  1680. $table = Database::get_course_table(TABLE_COURSE_SETTING);
  1681. $setting_name = Database::escape_string($setting_name);
  1682. if (!empty($course_info['real_id']) && !empty($setting_name)) {
  1683. $sql = "SELECT value FROM $table
  1684. WHERE c_id = {$course_info['real_id']} AND variable = '$setting_name'";
  1685. $res = Database::query($sql);
  1686. if (Database::num_rows($res) > 0) {
  1687. $row = Database::fetch_array($res);
  1688. if ($setting_name === 'email_alert_manager_on_new_quiz') {
  1689. if (!is_null($row['value'])) {
  1690. $result = explode(',', $row['value']);
  1691. $row['value'] = $result;
  1692. }
  1693. }
  1694. return $row['value'];
  1695. }
  1696. }
  1697. return -1;
  1698. }
  1699. /**
  1700. * Gets an anonymous user ID.
  1701. *
  1702. * For some tools that need tracking, like the learnpath tool, it is necessary
  1703. * to have a usable user-id to enable some kind of tracking, even if not
  1704. * perfect. An anonymous ID is taken from the users table by looking for a
  1705. * status of "6" (anonymous).
  1706. *
  1707. * @return int User ID of the anonymous user, or O if no anonymous user found
  1708. */
  1709. function api_get_anonymous_id()
  1710. {
  1711. // Find if another anon is connected now
  1712. $table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  1713. $tableU = Database::get_main_table(TABLE_MAIN_USER);
  1714. $ip = Database::escape_string(api_get_real_ip());
  1715. $max = (int) api_get_configuration_value('max_anonymous_users');
  1716. if ($max >= 2) {
  1717. $sql = "SELECT * FROM $table as TEL
  1718. JOIN $tableU as U
  1719. ON U.user_id = TEL.login_user_id
  1720. WHERE TEL.user_ip = '$ip'
  1721. AND U.status = ".ANONYMOUS."
  1722. AND U.user_id != 2 ";
  1723. $result = Database::query($sql);
  1724. if (empty(Database::num_rows($result))) {
  1725. $login = uniqid('anon_');
  1726. $anonList = UserManager::get_user_list(['status' => ANONYMOUS], ['registration_date ASC']);
  1727. if (count($anonList) >= $max) {
  1728. foreach ($anonList as $userToDelete) {
  1729. UserManager::delete_user($userToDelete['user_id']);
  1730. break;
  1731. }
  1732. }
  1733. $userId = UserManager::create_user(
  1734. $login,
  1735. 'anon',
  1736. ANONYMOUS,
  1737. ' anonymous@localhost',
  1738. $login,
  1739. $login
  1740. );
  1741. return $userId;
  1742. } else {
  1743. $row = Database::fetch_array($result, 'ASSOC');
  1744. return $row['user_id'];
  1745. }
  1746. }
  1747. $table = Database::get_main_table(TABLE_MAIN_USER);
  1748. $sql = "SELECT user_id
  1749. FROM $table
  1750. WHERE status = ".ANONYMOUS." ";
  1751. $res = Database::query($sql);
  1752. if (Database::num_rows($res) > 0) {
  1753. $row = Database::fetch_array($res, 'ASSOC');
  1754. return $row['user_id'];
  1755. }
  1756. // No anonymous user was found.
  1757. return 0;
  1758. }
  1759. /**
  1760. * @param string $courseCode
  1761. * @param int $sessionId
  1762. * @param int $groupId
  1763. *
  1764. * @return string
  1765. */
  1766. function api_get_cidreq_params($courseCode, $sessionId = 0, $groupId = 0)
  1767. {
  1768. $courseCode = !empty($courseCode) ? htmlspecialchars($courseCode) : '';
  1769. $sessionId = !empty($sessionId) ? (int) $sessionId : 0;
  1770. $groupId = !empty($groupId) ? (int) $groupId : 0;
  1771. $url = 'cidReq='.$courseCode;
  1772. $url .= '&id_session='.$sessionId;
  1773. $url .= '&gidReq='.$groupId;
  1774. return $url;
  1775. }
  1776. /**
  1777. * Returns the current course url part including session, group, and gradebook params.
  1778. *
  1779. * @param bool $addSessionId
  1780. * @param bool $addGroupId
  1781. * @param string $origin
  1782. *
  1783. * @return string Course & session references to add to a URL
  1784. */
  1785. function api_get_cidreq($addSessionId = true, $addGroupId = true, $origin = '')
  1786. {
  1787. $courseCode = api_get_course_id();
  1788. $url = empty($courseCode) ? '' : 'cidReq='.htmlspecialchars($courseCode);
  1789. $origin = empty($origin) ? api_get_origin() : Security::remove_XSS($origin);
  1790. if ($addSessionId) {
  1791. if (!empty($url)) {
  1792. $url .= api_get_session_id() == 0 ? '&id_session=0' : '&id_session='.api_get_session_id();
  1793. }
  1794. }
  1795. if ($addGroupId) {
  1796. if (!empty($url)) {
  1797. $url .= api_get_group_id() == 0 ? '&gidReq=0' : '&gidReq='.api_get_group_id();
  1798. }
  1799. }
  1800. if (!empty($url)) {
  1801. $url .= '&gradebook='.intval(api_is_in_gradebook());
  1802. $url .= '&origin='.$origin;
  1803. }
  1804. return $url;
  1805. }
  1806. /**
  1807. * Get if we visited a gradebook page.
  1808. *
  1809. * @return bool
  1810. */
  1811. function api_is_in_gradebook()
  1812. {
  1813. return Session::read('in_gradebook', false);
  1814. }
  1815. /**
  1816. * Set that we are in a page inside a gradebook.
  1817. */
  1818. function api_set_in_gradebook()
  1819. {
  1820. Session::write('in_gradebook', true);
  1821. }
  1822. /**
  1823. * Remove gradebook session.
  1824. */
  1825. function api_remove_in_gradebook()
  1826. {
  1827. Session::erase('in_gradebook');
  1828. }
  1829. /**
  1830. * Returns the current course info array see api_format_course_array()
  1831. * If the course_code is given, the returned array gives info about that
  1832. * particular course, if none given it gets the course info from the session.
  1833. *
  1834. * @param string $course_code
  1835. * @param bool $strict
  1836. *
  1837. * @return array
  1838. */
  1839. function api_get_course_info($course_code = null, $strict = false)
  1840. {
  1841. if (!empty($course_code)) {
  1842. $course_code = Database::escape_string($course_code);
  1843. $courseId = api_get_course_int_id($course_code);
  1844. if (empty($courseId)) {
  1845. return [];
  1846. }
  1847. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  1848. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  1849. $sql = "SELECT
  1850. course.*,
  1851. course_category.code faCode,
  1852. course_category.name faName
  1853. FROM $course_table
  1854. LEFT JOIN $course_cat_table
  1855. ON course.category_code = course_category.code
  1856. WHERE course.id = $courseId";
  1857. $result = Database::query($sql);
  1858. $courseInfo = [];
  1859. if (Database::num_rows($result) > 0) {
  1860. $data = Database::fetch_array($result);
  1861. $courseInfo = api_format_course_array($data);
  1862. }
  1863. return $courseInfo;
  1864. }
  1865. global $_course;
  1866. if ($_course == '-1') {
  1867. $_course = [];
  1868. }
  1869. return $_course;
  1870. }
  1871. /**
  1872. * @param int $courseId
  1873. *
  1874. * @return \Chamilo\CoreBundle\Entity\Course
  1875. */
  1876. function api_get_course_entity($courseId = 0)
  1877. {
  1878. if (empty($courseId)) {
  1879. $courseId = api_get_course_int_id();
  1880. }
  1881. return Database::getManager()->getRepository('ChamiloCoreBundle:Course')->find($courseId);
  1882. }
  1883. /**
  1884. * @param int $id
  1885. *
  1886. * @return \Chamilo\CoreBundle\Entity\Session
  1887. */
  1888. function api_get_session_entity($id = 0)
  1889. {
  1890. if (empty($id)) {
  1891. $id = api_get_session_id();
  1892. }
  1893. return Database::getManager()->getRepository('ChamiloCoreBundle:Session')->find($id);
  1894. }
  1895. /**
  1896. * Returns the current course info array.
  1897. * Now if the course_code is given, the returned array gives info about that
  1898. * particular course, not specially the current one.
  1899. *
  1900. * @param int $id Numeric ID of the course
  1901. *
  1902. * @return array The course info as an array formatted by api_format_course_array, including category.name
  1903. */
  1904. function api_get_course_info_by_id($id = null)
  1905. {
  1906. if (!empty($id)) {
  1907. $id = (int) $id;
  1908. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  1909. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  1910. $sql = "SELECT
  1911. course.*,
  1912. course_category.code faCode,
  1913. course_category.name faName
  1914. FROM $course_table
  1915. LEFT JOIN $course_cat_table
  1916. ON course.category_code = course_category.code
  1917. WHERE course.id = $id";
  1918. $result = Database::query($sql);
  1919. $_course = [];
  1920. if (Database::num_rows($result) > 0) {
  1921. $row = Database::fetch_array($result);
  1922. $_course = api_format_course_array($row);
  1923. }
  1924. return $_course;
  1925. }
  1926. global $_course;
  1927. if ($_course == '-1') {
  1928. $_course = [];
  1929. }
  1930. return $_course;
  1931. }
  1932. /**
  1933. * Reformat the course array (output by api_get_course_info()) in order, mostly,
  1934. * to switch from 'code' to 'id' in the array. This is a legacy feature and is
  1935. * now possibly causing massive confusion as a new "id" field has been added to
  1936. * the course table in 1.9.0.
  1937. *
  1938. * @param $course_data
  1939. *
  1940. * @return array
  1941. *
  1942. * @todo eradicate the false "id"=code field of the $_course array and use the int id
  1943. */
  1944. function api_format_course_array($course_data)
  1945. {
  1946. if (empty($course_data)) {
  1947. return [];
  1948. }
  1949. $_course = [];
  1950. $_course['id'] = $course_data['code'];
  1951. $_course['real_id'] = $course_data['id'];
  1952. // Added
  1953. $_course['code'] = $course_data['code'];
  1954. $_course['name'] = $course_data['title'];
  1955. $_course['title'] = $course_data['title'];
  1956. $_course['official_code'] = $course_data['visual_code'];
  1957. $_course['visual_code'] = $course_data['visual_code'];
  1958. $_course['sysCode'] = $course_data['code'];
  1959. $_course['path'] = $course_data['directory']; // Use as key in path.
  1960. $_course['directory'] = $course_data['directory'];
  1961. $_course['creation_date'] = $course_data['creation_date'];
  1962. $_course['titular'] = $course_data['tutor_name'];
  1963. $_course['language'] = $course_data['course_language'];
  1964. $_course['extLink']['url'] = $course_data['department_url'];
  1965. $_course['extLink']['name'] = $course_data['department_name'];
  1966. $_course['categoryCode'] = $course_data['faCode'];
  1967. $_course['categoryName'] = $course_data['faName'];
  1968. $_course['visibility'] = $course_data['visibility'];
  1969. $_course['subscribe_allowed'] = $course_data['subscribe'];
  1970. $_course['subscribe'] = $course_data['subscribe'];
  1971. $_course['unsubscribe'] = $course_data['unsubscribe'];
  1972. $_course['course_language'] = $course_data['course_language'];
  1973. $_course['activate_legal'] = isset($course_data['activate_legal']) ? $course_data['activate_legal'] : false;
  1974. $_course['legal'] = $course_data['legal'];
  1975. $_course['show_score'] = $course_data['show_score']; //used in the work tool
  1976. $_course['department_name'] = $course_data['department_name'];
  1977. $_course['department_url'] = $course_data['department_url'];
  1978. $courseSys = api_get_path(SYS_COURSE_PATH).$course_data['directory'];
  1979. $webCourseHome = api_get_path(WEB_COURSE_PATH).$course_data['directory'];
  1980. // Course password
  1981. $_course['registration_code'] = !empty($course_data['registration_code']) ? sha1($course_data['registration_code']) : null;
  1982. $_course['disk_quota'] = $course_data['disk_quota'];
  1983. $_course['course_public_url'] = $webCourseHome.'/index.php';
  1984. if (array_key_exists('add_teachers_to_sessions_courses', $course_data)) {
  1985. $_course['add_teachers_to_sessions_courses'] = $course_data['add_teachers_to_sessions_courses'];
  1986. }
  1987. // Course image
  1988. $_course['course_image_source'] = '';
  1989. if (file_exists($courseSys.'/course-pic85x85.png')) {
  1990. $url_image = $webCourseHome.'/course-pic85x85.png';
  1991. $_course['course_image_source'] = $courseSys.'/course-pic85x85.png';
  1992. } else {
  1993. $url_image = Display::return_icon(
  1994. 'course.png',
  1995. null,
  1996. null,
  1997. ICON_SIZE_BIG,
  1998. null,
  1999. true,
  2000. false
  2001. );
  2002. }
  2003. $_course['course_image'] = $url_image;
  2004. // Course large image
  2005. $_course['course_image_large_source'] = '';
  2006. if (file_exists($courseSys.'/course-pic.png')) {
  2007. $url_image = $webCourseHome.'/course-pic.png';
  2008. $_course['course_image_large_source'] = $courseSys.'/course-pic.png';
  2009. } else {
  2010. $url_image = Display::return_icon(
  2011. 'session_default.png',
  2012. null,
  2013. null,
  2014. null,
  2015. null,
  2016. true,
  2017. true
  2018. );
  2019. }
  2020. $_course['course_image_large'] = $url_image;
  2021. return $_course;
  2022. }
  2023. /**
  2024. * Returns a difficult to guess password.
  2025. *
  2026. * @param int $length the length of the password
  2027. *
  2028. * @return string the generated password
  2029. */
  2030. function api_generate_password($length = 8)
  2031. {
  2032. if ($length < 2) {
  2033. $length = 2;
  2034. }
  2035. $charactersLowerCase = 'abcdefghijkmnopqrstuvwxyz';
  2036. $charactersUpperCase = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
  2037. $minNumbers = 2;
  2038. $length = $length - $minNumbers;
  2039. $minLowerCase = round($length / 2);
  2040. $minUpperCase = $length - $minLowerCase;
  2041. $password = '';
  2042. $passwordRequirements = api_get_configuration_value('password_requirements');
  2043. $factory = new RandomLib\Factory();
  2044. $generator = $factory->getGenerator(new SecurityLib\Strength(SecurityLib\Strength::MEDIUM));
  2045. if (!empty($passwordRequirements)) {
  2046. $length = $passwordRequirements['min']['length'];
  2047. $minNumbers = $passwordRequirements['min']['numeric'];
  2048. $minLowerCase = $passwordRequirements['min']['lowercase'];
  2049. $minUpperCase = $passwordRequirements['min']['uppercase'];
  2050. $rest = $length - $minNumbers - $minLowerCase - $minUpperCase;
  2051. // Add the rest to fill the length requirement
  2052. if ($rest > 0) {
  2053. $password .= $generator->generateString($rest, $charactersLowerCase.$charactersUpperCase);
  2054. }
  2055. }
  2056. // Min digits default 2
  2057. for ($i = 0; $i < $minNumbers; $i++) {
  2058. $password .= $generator->generateInt(2, 9);
  2059. }
  2060. // Min lowercase
  2061. $password .= $generator->generateString($minLowerCase, $charactersLowerCase);
  2062. // Min uppercase
  2063. $password .= $generator->generateString($minUpperCase, $charactersUpperCase);
  2064. $password = str_shuffle($password);
  2065. return $password;
  2066. }
  2067. /**
  2068. * Checks a password to see wether it is OK to use.
  2069. *
  2070. * @param string $password
  2071. *
  2072. * @return bool if the password is acceptable, false otherwise
  2073. * Notes about what a password "OK to use" is:
  2074. * 1. The password should be at least 5 characters long.
  2075. * 2. Only English letters (uppercase or lowercase, it doesn't matter) and digits are allowed.
  2076. * 3. The password should contain at least 3 letters.
  2077. * 4. It should contain at least 2 digits.
  2078. * Settings will change if the configuration value is set: password_requirements
  2079. */
  2080. function api_check_password($password)
  2081. {
  2082. $passwordRequirements = Security::getPasswordRequirements();
  2083. $minLength = $passwordRequirements['min']['length'];
  2084. $minNumbers = $passwordRequirements['min']['numeric'];
  2085. // Optional
  2086. $minLowerCase = $passwordRequirements['min']['lowercase'];
  2087. $minUpperCase = $passwordRequirements['min']['uppercase'];
  2088. $minLetters = $minLowerCase + $minUpperCase;
  2089. $passwordLength = api_strlen($password);
  2090. $conditions = [
  2091. 'min_length' => $passwordLength >= $minLength,
  2092. ];
  2093. $digits = 0;
  2094. $lowerCase = 0;
  2095. $upperCase = 0;
  2096. for ($i = 0; $i < $passwordLength; $i++) {
  2097. $currentCharacterCode = api_ord(api_substr($password, $i, 1));
  2098. if ($currentCharacterCode >= 65 && $currentCharacterCode <= 90) {
  2099. $upperCase++;
  2100. }
  2101. if ($currentCharacterCode >= 97 && $currentCharacterCode <= 122) {
  2102. $lowerCase++;
  2103. }
  2104. if ($currentCharacterCode >= 48 && $currentCharacterCode <= 57) {
  2105. $digits++;
  2106. }
  2107. }
  2108. // Min number of digits
  2109. $conditions['min_numeric'] = $digits >= $minNumbers;
  2110. if (!empty($minUpperCase)) {
  2111. // Uppercase
  2112. $conditions['min_uppercase'] = $upperCase >= $minUpperCase;
  2113. }
  2114. if (!empty($minLowerCase)) {
  2115. // Lowercase
  2116. $conditions['min_lowercase'] = $upperCase >= $minLowerCase;
  2117. }
  2118. // Min letters
  2119. $letters = $upperCase + $lowerCase;
  2120. $conditions['min_letters'] = $letters >= $minLetters;
  2121. $isPasswordOk = true;
  2122. foreach ($conditions as $condition) {
  2123. if ($condition === false) {
  2124. $isPasswordOk = false;
  2125. break;
  2126. }
  2127. }
  2128. if ($isPasswordOk === false) {
  2129. $output = get_lang('NewPasswordRequirementsNotMatched').'<br />';
  2130. $output .= Security::getPasswordRequirementsToString($conditions);
  2131. Display::addFlash(Display::return_message($output, 'warning', false));
  2132. }
  2133. return $isPasswordOk;
  2134. }
  2135. /**
  2136. * Clears the user ID from the session if it was the anonymous user. Generally
  2137. * used on out-of-tools pages to remove a user ID that could otherwise be used
  2138. * in the wrong context.
  2139. * This function is to be used in conjunction with the api_set_anonymous()
  2140. * function to simulate the user existence in case of an anonymous visit.
  2141. *
  2142. * @param bool database check switch - passed to api_is_anonymous()
  2143. *
  2144. * @return bool true if succesfully unregistered, false if not anonymous
  2145. */
  2146. function api_clear_anonymous($db_check = false)
  2147. {
  2148. global $_user;
  2149. if (api_is_anonymous($_user['user_id'], $db_check)) {
  2150. unset($_user['user_id']);
  2151. Session::erase('_uid');
  2152. return true;
  2153. }
  2154. return false;
  2155. }
  2156. /**
  2157. * Returns the status string corresponding to the status code.
  2158. *
  2159. * @author Noel Dieschburg
  2160. *
  2161. * @param the int status code
  2162. *
  2163. * @return string
  2164. */
  2165. function get_status_from_code($status_code)
  2166. {
  2167. switch ($status_code) {
  2168. case STUDENT:
  2169. return get_lang('Student', '');
  2170. case COURSEMANAGER:
  2171. return get_lang('Teacher', '');
  2172. case SESSIONADMIN:
  2173. return get_lang('SessionsAdmin', '');
  2174. case DRH:
  2175. return get_lang('Drh', '');
  2176. }
  2177. }
  2178. /**
  2179. * Sets the current user as anonymous if it hasn't been identified yet. This
  2180. * function should be used inside a tool only. The function api_clear_anonymous()
  2181. * acts in the opposite direction by clearing the anonymous user's data every
  2182. * time we get on a course homepage or on a neutral page (index, admin, my space).
  2183. *
  2184. * @return bool true if set user as anonymous, false if user was already logged in or anonymous id could not be found
  2185. */
  2186. function api_set_anonymous()
  2187. {
  2188. global $_user;
  2189. if (!empty($_user['user_id'])) {
  2190. return false;
  2191. }
  2192. $user_id = api_get_anonymous_id();
  2193. if ($user_id == 0) {
  2194. return false;
  2195. }
  2196. if (isset($_user['is_anonymous'])) {
  2197. return false;
  2198. }
  2199. Session::erase('_user');
  2200. $_user['user_id'] = $user_id;
  2201. $_user['is_anonymous'] = true;
  2202. $GLOBALS['_user'] = $_user;
  2203. Session::write('_user', $_user);
  2204. return true;
  2205. }
  2206. /**
  2207. * Gets the current Chamilo (not PHP/cookie) session ID.
  2208. *
  2209. * @return int O if no active session, the session ID otherwise
  2210. */
  2211. function api_get_session_id()
  2212. {
  2213. return (int) Session::read('id_session', 0);
  2214. }
  2215. /**
  2216. * Gets the current Chamilo (not social network) group ID.
  2217. *
  2218. * @return int O if no active session, the session ID otherwise
  2219. */
  2220. function api_get_group_id()
  2221. {
  2222. return Session::read('_gid', 0);
  2223. }
  2224. /**
  2225. * Gets the current or given session name.
  2226. *
  2227. * @param int Session ID (optional)
  2228. *
  2229. * @return string The session name, or null if not found
  2230. */
  2231. function api_get_session_name($session_id = 0)
  2232. {
  2233. if (empty($session_id)) {
  2234. $session_id = api_get_session_id();
  2235. if (empty($session_id)) {
  2236. return null;
  2237. }
  2238. }
  2239. $t = Database::get_main_table(TABLE_MAIN_SESSION);
  2240. $s = "SELECT name FROM $t WHERE id = ".(int) $session_id;
  2241. $r = Database::query($s);
  2242. $c = Database::num_rows($r);
  2243. if ($c > 0) {
  2244. //technically, there can be only one, but anyway we take the first
  2245. $rec = Database::fetch_array($r);
  2246. return $rec['name'];
  2247. }
  2248. return null;
  2249. }
  2250. /**
  2251. * Gets the session info by id.
  2252. *
  2253. * @param int $id Session ID
  2254. *
  2255. * @return array information of the session
  2256. */
  2257. function api_get_session_info($id)
  2258. {
  2259. return SessionManager::fetch($id);
  2260. }
  2261. /**
  2262. * Gets the session visibility by session id.
  2263. *
  2264. * @param int $session_id
  2265. * @param int $courseId
  2266. * @param bool $ignore_visibility_for_admins
  2267. *
  2268. * @return int
  2269. * 0 = session still available,
  2270. * SESSION_VISIBLE_READ_ONLY = 1,
  2271. * SESSION_VISIBLE = 2,
  2272. * SESSION_INVISIBLE = 3
  2273. */
  2274. function api_get_session_visibility(
  2275. $session_id,
  2276. $courseId = null,
  2277. $ignore_visibility_for_admins = true
  2278. ) {
  2279. if (api_is_platform_admin()) {
  2280. if ($ignore_visibility_for_admins) {
  2281. return SESSION_AVAILABLE;
  2282. }
  2283. }
  2284. $now = time();
  2285. if (empty($session_id)) {
  2286. return 0; // Means that the session is still available.
  2287. }
  2288. $session_id = (int) $session_id;
  2289. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2290. $result = Database::query("SELECT * FROM $tbl_session WHERE id = $session_id");
  2291. if (Database::num_rows($result) <= 0) {
  2292. return SESSION_INVISIBLE;
  2293. }
  2294. $row = Database::fetch_array($result, 'ASSOC');
  2295. $visibility = $original_visibility = $row['visibility'];
  2296. // I don't care the session visibility.
  2297. if (empty($row['access_start_date']) && empty($row['access_end_date'])) {
  2298. // Session duration per student.
  2299. if (isset($row['duration']) && !empty($row['duration'])) {
  2300. $duration = $row['duration'] * 24 * 60 * 60;
  2301. $courseAccess = CourseManager::getFirstCourseAccessPerSessionAndUser($session_id, api_get_user_id());
  2302. // If there is a session duration but there is no previous
  2303. // access by the user, then the session is still available
  2304. if (count($courseAccess) == 0) {
  2305. return SESSION_AVAILABLE;
  2306. }
  2307. $currentTime = time();
  2308. $firstAccess = isset($courseAccess['login_course_date'])
  2309. ? api_strtotime($courseAccess['login_course_date'], 'UTC')
  2310. : 0;
  2311. $userDurationData = SessionManager::getUserSession(
  2312. api_get_user_id(),
  2313. $session_id
  2314. );
  2315. $userDuration = isset($userDurationData['duration'])
  2316. ? (intval($userDurationData['duration']) * 24 * 60 * 60)
  2317. : 0;
  2318. $totalDuration = $firstAccess + $duration + $userDuration;
  2319. return $totalDuration > $currentTime ? SESSION_AVAILABLE : SESSION_VISIBLE_READ_ONLY;
  2320. }
  2321. return SESSION_AVAILABLE;
  2322. }
  2323. // If start date was set.
  2324. if (!empty($row['access_start_date'])) {
  2325. $visibility = $now > api_strtotime($row['access_start_date'], 'UTC') ? SESSION_AVAILABLE : SESSION_INVISIBLE;
  2326. }
  2327. // If the end date was set.
  2328. if (!empty($row['access_end_date'])) {
  2329. // Only if date_start said that it was ok
  2330. if ($visibility === SESSION_AVAILABLE) {
  2331. $visibility = $now < api_strtotime($row['access_end_date'], 'UTC')
  2332. ? SESSION_AVAILABLE // Date still available
  2333. : $row['visibility']; // Session ends
  2334. }
  2335. }
  2336. // If I'm a coach the visibility can change in my favor depending in the coach dates.
  2337. $isCoach = api_is_coach($session_id, $courseId);
  2338. if ($isCoach) {
  2339. // Test start date.
  2340. if (!empty($row['coach_access_start_date'])) {
  2341. $start = api_strtotime($row['coach_access_start_date'], 'UTC');
  2342. $visibility = $start < $now ? SESSION_AVAILABLE : SESSION_INVISIBLE;
  2343. }
  2344. // Test end date.
  2345. if (!empty($row['coach_access_end_date'])) {
  2346. if ($visibility === SESSION_AVAILABLE) {
  2347. $endDateCoach = api_strtotime($row['coach_access_end_date'], 'UTC');
  2348. $visibility = $endDateCoach >= $now ? SESSION_AVAILABLE : $row['visibility'];
  2349. }
  2350. }
  2351. }
  2352. return $visibility;
  2353. }
  2354. /**
  2355. * This function returns a (star) session icon if the session is not null and
  2356. * the user is not a student.
  2357. *
  2358. * @param int $session_id
  2359. * @param int $status_id User status id - if 5 (student), will return empty
  2360. *
  2361. * @return string Session icon
  2362. */
  2363. function api_get_session_image($session_id, $status_id)
  2364. {
  2365. $session_id = (int) $session_id;
  2366. $session_img = '';
  2367. if ((int) $status_id != 5) { //check whether is not a student
  2368. if ($session_id > 0) {
  2369. $session_img = "&nbsp;&nbsp;".Display::return_icon(
  2370. 'star.png',
  2371. get_lang('SessionSpecificResource'),
  2372. ['align' => 'absmiddle'],
  2373. ICON_SIZE_SMALL
  2374. );
  2375. }
  2376. }
  2377. return $session_img;
  2378. }
  2379. /**
  2380. * This function add an additional condition according to the session of the course.
  2381. *
  2382. * @param int $session_id session id
  2383. * @param bool $and optional, true if more than one condition false if the only condition in the query
  2384. * @param bool $with_base_content optional, true to accept content with session=0 as well,
  2385. * false for strict session condition
  2386. * @param string $session_field
  2387. *
  2388. * @return string condition of the session
  2389. */
  2390. function api_get_session_condition(
  2391. $session_id,
  2392. $and = true,
  2393. $with_base_content = false,
  2394. $session_field = 'session_id'
  2395. ) {
  2396. $session_id = (int) $session_id;
  2397. if (empty($session_field)) {
  2398. $session_field = "session_id";
  2399. }
  2400. // Condition to show resources by session
  2401. $condition_add = $and ? " AND " : " WHERE ";
  2402. if ($with_base_content) {
  2403. $condition_session = $condition_add." ( $session_field = $session_id OR $session_field = 0 OR $session_field IS NULL) ";
  2404. } else {
  2405. if (empty($session_id)) {
  2406. $condition_session = $condition_add." ($session_field = $session_id OR $session_field IS NULL)";
  2407. } else {
  2408. $condition_session = $condition_add." $session_field = $session_id ";
  2409. }
  2410. }
  2411. return $condition_session;
  2412. }
  2413. /**
  2414. * Returns the value of a setting from the web-adjustable admin config settings.
  2415. *
  2416. * WARNING true/false are stored as string, so when comparing you need to check e.g.
  2417. * if (api_get_setting('show_navigation_menu') == 'true') //CORRECT
  2418. * instead of
  2419. * if (api_get_setting('show_navigation_menu') == true) //INCORRECT
  2420. *
  2421. * @param string $variable The variable name
  2422. * @param string $key The subkey (sub-variable) if any. Defaults to NULL
  2423. *
  2424. * @return string
  2425. *
  2426. * @author René Haentjens
  2427. * @author Bart Mollet
  2428. */
  2429. function api_get_setting($variable, $key = null)
  2430. {
  2431. global $_setting;
  2432. if ($variable == 'header_extra_content') {
  2433. $filename = api_get_home_path().'header_extra_content.txt';
  2434. if (file_exists($filename)) {
  2435. $value = file_get_contents($filename);
  2436. return $value;
  2437. } else {
  2438. return '';
  2439. }
  2440. }
  2441. if ($variable == 'footer_extra_content') {
  2442. $filename = api_get_home_path().'footer_extra_content.txt';
  2443. if (file_exists($filename)) {
  2444. $value = file_get_contents($filename);
  2445. return $value;
  2446. } else {
  2447. return '';
  2448. }
  2449. }
  2450. $value = null;
  2451. if (is_null($key)) {
  2452. $value = ((isset($_setting[$variable]) && $_setting[$variable] != '') ? $_setting[$variable] : null);
  2453. } else {
  2454. if (isset($_setting[$variable][$key])) {
  2455. $value = $_setting[$variable][$key];
  2456. }
  2457. }
  2458. return $value;
  2459. }
  2460. /**
  2461. * @param string $plugin
  2462. * @param string $variable
  2463. *
  2464. * @return string
  2465. */
  2466. function api_get_plugin_setting($plugin, $variable)
  2467. {
  2468. $variableName = $plugin.'_'.$variable;
  2469. $result = api_get_setting($variableName);
  2470. if (isset($result[$plugin])) {
  2471. $value = $result[$plugin];
  2472. $unserialized = UnserializeApi::unserialize('not_allowed_classes', $value, true);
  2473. if (false !== $unserialized) {
  2474. $value = $unserialized;
  2475. }
  2476. return $value;
  2477. }
  2478. return null;
  2479. }
  2480. /**
  2481. * Returns the value of a setting from the web-adjustable admin config settings.
  2482. */
  2483. function api_get_settings_params($params)
  2484. {
  2485. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  2486. $result = Database::select('*', $table, ['where' => $params]);
  2487. return $result;
  2488. }
  2489. /**
  2490. * @param array $params example: [id = ? => '1']
  2491. *
  2492. * @return array
  2493. */
  2494. function api_get_settings_params_simple($params)
  2495. {
  2496. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  2497. $result = Database::select('*', $table, ['where' => $params], 'one');
  2498. return $result;
  2499. }
  2500. /**
  2501. * Returns the value of a setting from the web-adjustable admin config settings.
  2502. */
  2503. function api_delete_settings_params($params)
  2504. {
  2505. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  2506. $result = Database::delete($table, $params);
  2507. return $result;
  2508. }
  2509. /**
  2510. * Returns an escaped version of $_SERVER['PHP_SELF'] to avoid XSS injection.
  2511. *
  2512. * @return string Escaped version of $_SERVER['PHP_SELF']
  2513. */
  2514. function api_get_self()
  2515. {
  2516. return htmlentities($_SERVER['PHP_SELF']);
  2517. }
  2518. /* USER PERMISSIONS */
  2519. /**
  2520. * Checks whether current user is a platform administrator.
  2521. *
  2522. * @param bool $allowSessionAdmins Whether session admins should be considered admins or not
  2523. * @param bool $allowDrh Whether HR directors should be considered admins or not
  2524. *
  2525. * @return bool true if the user has platform admin rights,
  2526. * false otherwise
  2527. *
  2528. * @see usermanager::is_admin(user_id) for a user-id specific function
  2529. */
  2530. function api_is_platform_admin($allowSessionAdmins = false, $allowDrh = false)
  2531. {
  2532. $isAdmin = Session::read('is_platformAdmin');
  2533. if ($isAdmin) {
  2534. return true;
  2535. }
  2536. $user = api_get_user_info();
  2537. return
  2538. isset($user['status']) &&
  2539. (
  2540. ($allowSessionAdmins && $user['status'] == SESSIONADMIN) ||
  2541. ($allowDrh && $user['status'] == DRH)
  2542. );
  2543. }
  2544. /**
  2545. * Checks whether the user given as user id is in the admin table.
  2546. *
  2547. * @param int $user_id If none provided, will use current user
  2548. * @param int $url URL ID. If provided, also check if the user is active on given URL
  2549. *
  2550. * @return bool True if the user is admin, false otherwise
  2551. */
  2552. function api_is_platform_admin_by_id($user_id = null, $url = null)
  2553. {
  2554. $user_id = (int) $user_id;
  2555. if (empty($user_id)) {
  2556. $user_id = api_get_user_id();
  2557. }
  2558. $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
  2559. $sql = "SELECT * FROM $admin_table WHERE user_id = $user_id";
  2560. $res = Database::query($sql);
  2561. $is_admin = Database::num_rows($res) === 1;
  2562. if (!$is_admin || !isset($url)) {
  2563. return $is_admin;
  2564. }
  2565. // We get here only if $url is set
  2566. $url = (int) $url;
  2567. $url_user_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  2568. $sql = "SELECT * FROM $url_user_table
  2569. WHERE access_url_id = $url AND user_id = $user_id";
  2570. $res = Database::query($sql);
  2571. $result = Database::num_rows($res) === 1;
  2572. return $result;
  2573. }
  2574. /**
  2575. * Returns the user's numeric status ID from the users table.
  2576. *
  2577. * @param int $user_id If none provided, will use current user
  2578. *
  2579. * @return int User's status (1 for teacher, 5 for student, etc)
  2580. */
  2581. function api_get_user_status($user_id = null)
  2582. {
  2583. $user_id = (int) $user_id;
  2584. if (empty($user_id)) {
  2585. $user_id = api_get_user_id();
  2586. }
  2587. $table = Database::get_main_table(TABLE_MAIN_USER);
  2588. $sql = "SELECT status FROM $table WHERE user_id = $user_id ";
  2589. $result = Database::query($sql);
  2590. $status = null;
  2591. if (Database::num_rows($result)) {
  2592. $row = Database::fetch_array($result);
  2593. $status = $row['status'];
  2594. }
  2595. return $status;
  2596. }
  2597. /**
  2598. * Checks whether current user is allowed to create courses.
  2599. *
  2600. * @return bool true if the user has course creation rights,
  2601. * false otherwise
  2602. */
  2603. function api_is_allowed_to_create_course()
  2604. {
  2605. if (api_is_platform_admin()) {
  2606. return true;
  2607. }
  2608. // Teachers can only create courses
  2609. if (api_is_teacher()) {
  2610. if (api_get_setting('allow_users_to_create_courses') === 'true') {
  2611. return true;
  2612. } else {
  2613. return false;
  2614. }
  2615. }
  2616. return Session::read('is_allowedCreateCourse');
  2617. }
  2618. /**
  2619. * Checks whether the current user is a course administrator.
  2620. *
  2621. * @return bool True if current user is a course administrator
  2622. */
  2623. function api_is_course_admin()
  2624. {
  2625. if (api_is_platform_admin()) {
  2626. return true;
  2627. }
  2628. return Session::read('is_courseAdmin');
  2629. }
  2630. /**
  2631. * Checks whether the current user is a course coach
  2632. * Based on the presence of user in session.id_coach (session general coach).
  2633. *
  2634. * @return bool True if current user is a course coach
  2635. */
  2636. function api_is_session_general_coach()
  2637. {
  2638. return Session::read('is_session_general_coach');
  2639. }
  2640. /**
  2641. * Checks whether the current user is a course tutor
  2642. * Based on the presence of user in session_rel_course_rel_user.user_id with status = 2.
  2643. *
  2644. * @return bool True if current user is a course tutor
  2645. */
  2646. function api_is_course_tutor()
  2647. {
  2648. return Session::read('is_courseTutor');
  2649. }
  2650. /**
  2651. * @param int $user_id
  2652. * @param int $courseId
  2653. * @param int $session_id
  2654. *
  2655. * @return bool
  2656. */
  2657. function api_is_course_session_coach($user_id, $courseId, $session_id)
  2658. {
  2659. $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
  2660. $session_rel_course_rel_user_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2661. $user_id = (int) $user_id;
  2662. $session_id = (int) $session_id;
  2663. $courseId = (int) $courseId;
  2664. $sql = "SELECT DISTINCT session.id
  2665. FROM $session_table
  2666. INNER JOIN $session_rel_course_rel_user_table session_rc_ru
  2667. ON session.id = session_rc_ru.session_id
  2668. WHERE
  2669. session_rc_ru.user_id = '".$user_id."' AND
  2670. session_rc_ru.c_id = '$courseId' AND
  2671. session_rc_ru.status = 2 AND
  2672. session_rc_ru.session_id = '$session_id'";
  2673. $result = Database::query($sql);
  2674. return Database::num_rows($result) > 0;
  2675. }
  2676. /**
  2677. * Checks whether the current user is a course or session coach.
  2678. *
  2679. * @param int $session_id
  2680. * @param int $courseId
  2681. * @param bool Check whether we are in student view and, if we are, return false
  2682. *
  2683. * @return bool True if current user is a course or session coach
  2684. */
  2685. function api_is_coach($session_id = 0, $courseId = null, $check_student_view = true)
  2686. {
  2687. $userId = api_get_user_id();
  2688. if (!empty($session_id)) {
  2689. $session_id = (int) $session_id;
  2690. } else {
  2691. $session_id = api_get_session_id();
  2692. }
  2693. // The student preview was on
  2694. if ($check_student_view && api_is_student_view_active()) {
  2695. return false;
  2696. }
  2697. if (!empty($courseId)) {
  2698. $courseId = (int) $courseId;
  2699. } else {
  2700. $courseId = api_get_course_int_id();
  2701. }
  2702. $session_table = Database::get_main_table(TABLE_MAIN_SESSION);
  2703. $session_rel_course_rel_user_table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  2704. $sessionIsCoach = [];
  2705. if (!empty($courseId)) {
  2706. $sql = "SELECT DISTINCT s.id, name, access_start_date, access_end_date
  2707. FROM $session_table s
  2708. INNER JOIN $session_rel_course_rel_user_table session_rc_ru
  2709. ON session_rc_ru.session_id = s.id AND session_rc_ru.user_id = '".$userId."'
  2710. WHERE
  2711. session_rc_ru.c_id = '$courseId' AND
  2712. session_rc_ru.status = 2 AND
  2713. session_rc_ru.session_id = '$session_id'";
  2714. $result = Database::query($sql);
  2715. $sessionIsCoach = Database::store_result($result);
  2716. }
  2717. if (!empty($session_id)) {
  2718. $sql = "SELECT DISTINCT id, name, access_start_date, access_end_date
  2719. FROM $session_table
  2720. WHERE session.id_coach = $userId AND id = $session_id
  2721. ORDER BY access_start_date, access_end_date, name";
  2722. $result = Database::query($sql);
  2723. if (!empty($sessionIsCoach)) {
  2724. $sessionIsCoach = array_merge(
  2725. $sessionIsCoach,
  2726. Database::store_result($result)
  2727. );
  2728. } else {
  2729. $sessionIsCoach = Database::store_result($result);
  2730. }
  2731. }
  2732. return count($sessionIsCoach) > 0;
  2733. }
  2734. /**
  2735. * Checks whether the current user is a session administrator.
  2736. *
  2737. * @return bool True if current user is a course administrator
  2738. */
  2739. function api_is_session_admin()
  2740. {
  2741. $user = api_get_user_info();
  2742. return isset($user['status']) && $user['status'] == SESSIONADMIN;
  2743. }
  2744. /**
  2745. * Checks whether the current user is a human resources manager.
  2746. *
  2747. * @return bool True if current user is a human resources manager
  2748. */
  2749. function api_is_drh()
  2750. {
  2751. $user = api_get_user_info();
  2752. return isset($user['status']) && $user['status'] == DRH;
  2753. }
  2754. /**
  2755. * Checks whether the current user is a student.
  2756. *
  2757. * @return bool True if current user is a human resources manager
  2758. */
  2759. function api_is_student()
  2760. {
  2761. $user = api_get_user_info();
  2762. return isset($user['status']) && $user['status'] == STUDENT;
  2763. }
  2764. /**
  2765. * Checks whether the current user has the status 'teacher'.
  2766. *
  2767. * @return bool True if current user is a human resources manager
  2768. */
  2769. function api_is_teacher()
  2770. {
  2771. $user = api_get_user_info();
  2772. return isset($user['status']) && $user['status'] == COURSEMANAGER;
  2773. }
  2774. /**
  2775. * Checks whether the current user is a invited user.
  2776. *
  2777. * @return bool
  2778. */
  2779. function api_is_invitee()
  2780. {
  2781. $user = api_get_user_info();
  2782. return isset($user['status']) && $user['status'] == INVITEE;
  2783. }
  2784. /**
  2785. * This function checks whether a session is assigned into a category.
  2786. *
  2787. * @param int - session id
  2788. * @param string - category name
  2789. *
  2790. * @return bool - true if is found, otherwise false
  2791. */
  2792. function api_is_session_in_category($session_id, $category_name)
  2793. {
  2794. $session_id = (int) $session_id;
  2795. $category_name = Database::escape_string($category_name);
  2796. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  2797. $tbl_session_category = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  2798. $sql = "SELECT 1
  2799. FROM $tbl_session
  2800. WHERE $session_id IN (
  2801. SELECT s.id FROM $tbl_session s, $tbl_session_category sc
  2802. WHERE
  2803. s.session_category_id = sc.id AND
  2804. sc.name LIKE '%$category_name'
  2805. )";
  2806. $rs = Database::query($sql);
  2807. if (Database::num_rows($rs) > 0) {
  2808. return true;
  2809. } else {
  2810. return false;
  2811. }
  2812. }
  2813. /**
  2814. * Displays the title of a tool.
  2815. * Normal use: parameter is a string:
  2816. * api_display_tool_title("My Tool").
  2817. *
  2818. * Optionally, there can be a subtitle below
  2819. * the normal title, and / or a supra title above the normal title.
  2820. *
  2821. * e.g. supra title:
  2822. * group
  2823. * GROUP PROPERTIES
  2824. *
  2825. * e.g. subtitle:
  2826. * AGENDA
  2827. * calender & events tool
  2828. *
  2829. * @author Hugues Peeters <hugues.peeters@claroline.net>
  2830. *
  2831. * @param mixed $title_element - it could either be a string or an array
  2832. * containing 'supraTitle', 'mainTitle',
  2833. * 'subTitle'
  2834. */
  2835. function api_display_tool_title($title_element)
  2836. {
  2837. if (is_string($title_element)) {
  2838. $tit = $title_element;
  2839. unset($title_element);
  2840. $title_element['mainTitle'] = $tit;
  2841. }
  2842. echo '<h3>';
  2843. if (!empty($title_element['supraTitle'])) {
  2844. echo '<small>'.$title_element['supraTitle'].'</small><br />';
  2845. }
  2846. if (!empty($title_element['mainTitle'])) {
  2847. echo $title_element['mainTitle'];
  2848. }
  2849. if (!empty($title_element['subTitle'])) {
  2850. echo '<br /><small>'.$title_element['subTitle'].'</small>';
  2851. }
  2852. echo '</h3>';
  2853. }
  2854. /**
  2855. * Displays options for switching between student view and course manager view.
  2856. *
  2857. * Changes in version 1.2 (Patrick Cool)
  2858. * Student view switch now behaves as a real switch. It maintains its current state until the state
  2859. * is changed explicitly
  2860. *
  2861. * Changes in version 1.1 (Patrick Cool)
  2862. * student view now works correctly in subfolders of the document tool
  2863. * student view works correctly in the new links tool
  2864. *
  2865. * Example code for using this in your tools:
  2866. * //if ($is_courseAdmin && api_get_setting('student_view_enabled') == 'true') {
  2867. * // display_tool_view_option($isStudentView);
  2868. * //}
  2869. * //and in later sections, use api_is_allowed_to_edit()
  2870. *
  2871. * @author Roan Embrechts
  2872. * @author Patrick Cool
  2873. * @author Julio Montoya, changes added in Chamilo
  2874. *
  2875. * @version 1.2
  2876. *
  2877. * @todo rewrite code so it is easier to understand
  2878. */
  2879. function api_display_tool_view_option()
  2880. {
  2881. if (api_get_setting('student_view_enabled') != 'true') {
  2882. return '';
  2883. }
  2884. $sourceurl = '';
  2885. $is_framed = false;
  2886. // Exceptions apply for all multi-frames pages
  2887. if (strpos($_SERVER['REQUEST_URI'], 'chat/chat_banner.php') !== false) {
  2888. // The chat is a multiframe bit that doesn't work too well with the student_view, so do not show the link
  2889. return '';
  2890. }
  2891. // Uncomment to remove student view link from document view page
  2892. if (strpos($_SERVER['REQUEST_URI'], 'lp/lp_header.php') !== false) {
  2893. if (empty($_GET['lp_id'])) {
  2894. return '';
  2895. }
  2896. $sourceurl = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?'));
  2897. $sourceurl = str_replace(
  2898. 'lp/lp_header.php',
  2899. 'lp/lp_controller.php?'.api_get_cidreq().'&action=view&lp_id='.intval($_GET['lp_id']).'&isStudentView='.($_SESSION['studentview'] == 'studentview' ? 'false' : 'true'),
  2900. $sourceurl
  2901. );
  2902. //showinframes doesn't handle student view anyway...
  2903. //return '';
  2904. $is_framed = true;
  2905. }
  2906. // Check whether the $_SERVER['REQUEST_URI'] contains already url parameters (thus a questionmark)
  2907. if (!$is_framed) {
  2908. if (strpos($_SERVER['REQUEST_URI'], '?') === false) {
  2909. $sourceurl = api_get_self().'?'.api_get_cidreq();
  2910. } else {
  2911. $sourceurl = $_SERVER['REQUEST_URI'];
  2912. }
  2913. }
  2914. $output_string = '';
  2915. if (!empty($_SESSION['studentview'])) {
  2916. if ($_SESSION['studentview'] == 'studentview') {
  2917. // We have to remove the isStudentView=true from the $sourceurl
  2918. $sourceurl = str_replace('&isStudentView=true', '', $sourceurl);
  2919. $sourceurl = str_replace('&isStudentView=false', '', $sourceurl);
  2920. $output_string .= '<a class="btn btn-primary btn-sm" href="'.$sourceurl.'&isStudentView=false" target="_self">'.
  2921. Display::returnFontAwesomeIcon('eye').' '.get_lang('SwitchToTeacherView').'</a>';
  2922. } elseif ($_SESSION['studentview'] == 'teacherview') {
  2923. // Switching to teacherview
  2924. $sourceurl = str_replace('&isStudentView=true', '', $sourceurl);
  2925. $sourceurl = str_replace('&isStudentView=false', '', $sourceurl);
  2926. $output_string .= '<a class="btn btn-default btn-sm" href="'.$sourceurl.'&isStudentView=true" target="_self">'.
  2927. Display::returnFontAwesomeIcon('eye').' '.get_lang('SwitchToStudentView').'</a>';
  2928. }
  2929. } else {
  2930. $output_string .= '<a class="btn btn-default btn-sm" href="'.$sourceurl.'&isStudentView=true" target="_self">'.
  2931. Display::returnFontAwesomeIcon('eye').' '.get_lang('SwitchToStudentView').'</a>';
  2932. }
  2933. $output_string = Security::remove_XSS($output_string);
  2934. $html = Display::tag('div', $output_string, ['class' => 'view-options']);
  2935. return $html;
  2936. }
  2937. // TODO: This is for the permission section.
  2938. /**
  2939. * Function that removes the need to directly use is_courseAdmin global in
  2940. * tool scripts. It returns true or false depending on the user's rights in
  2941. * this particular course.
  2942. * Optionally checking for tutor and coach roles here allows us to use the
  2943. * student_view feature altogether with these roles as well.
  2944. *
  2945. * @param bool Whether to check if the user has the tutor role
  2946. * @param bool Whether to check if the user has the coach role
  2947. * @param bool Whether to check if the user has the session coach role
  2948. * @param bool check the student view or not
  2949. *
  2950. * @author Roan Embrechts
  2951. * @author Patrick Cool
  2952. * @author Julio Montoya
  2953. *
  2954. * @version 1.1, February 2004
  2955. *
  2956. * @return bool true: the user has the rights to edit, false: he does not
  2957. */
  2958. function api_is_allowed_to_edit(
  2959. $tutor = false,
  2960. $coach = false,
  2961. $session_coach = false,
  2962. $check_student_view = true
  2963. ) {
  2964. $allowSessionAdminEdit = api_get_configuration_value('session_admins_edit_courses_content') === true;
  2965. // Admins can edit anything.
  2966. if (api_is_platform_admin($allowSessionAdminEdit)) {
  2967. //The student preview was on
  2968. if ($check_student_view && api_is_student_view_active()) {
  2969. return false;
  2970. } else {
  2971. return true;
  2972. }
  2973. }
  2974. $sessionId = api_get_session_id();
  2975. if ($sessionId && api_get_configuration_value('session_courses_read_only_mode')) {
  2976. $efv = new ExtraFieldValue('course');
  2977. $lockExrafieldField = $efv->get_values_by_handler_and_field_variable(
  2978. api_get_course_int_id(),
  2979. 'session_courses_read_only_mode'
  2980. );
  2981. if (!empty($lockExrafieldField['value'])) {
  2982. return false;
  2983. }
  2984. }
  2985. $is_allowed_coach_to_edit = api_is_coach(null, null, $check_student_view);
  2986. $session_visibility = api_get_session_visibility($sessionId);
  2987. $is_courseAdmin = api_is_course_admin();
  2988. if (!$is_courseAdmin && $tutor) {
  2989. // If we also want to check if the user is a tutor...
  2990. $is_courseAdmin = $is_courseAdmin || api_is_course_tutor();
  2991. }
  2992. if (!$is_courseAdmin && $coach) {
  2993. // If we also want to check if the user is a coach...';
  2994. // Check if session visibility is read only for coaches.
  2995. if ($session_visibility == SESSION_VISIBLE_READ_ONLY) {
  2996. $is_allowed_coach_to_edit = false;
  2997. }
  2998. if (api_get_setting('allow_coach_to_edit_course_session') == 'true') {
  2999. // Check if coach is allowed to edit a course.
  3000. $is_courseAdmin = $is_courseAdmin || $is_allowed_coach_to_edit;
  3001. }
  3002. }
  3003. if (!$is_courseAdmin && $session_coach) {
  3004. $is_courseAdmin = $is_courseAdmin || $is_allowed_coach_to_edit;
  3005. }
  3006. // Check if the student_view is enabled, and if so, if it is activated.
  3007. if (api_get_setting('student_view_enabled') == 'true') {
  3008. if (!empty($sessionId)) {
  3009. // Check if session visibility is read only for coaches.
  3010. if ($session_visibility == SESSION_VISIBLE_READ_ONLY) {
  3011. $is_allowed_coach_to_edit = false;
  3012. }
  3013. if (api_get_setting('allow_coach_to_edit_course_session') == 'true') {
  3014. // Check if coach is allowed to edit a course.
  3015. $is_allowed = $is_allowed_coach_to_edit;
  3016. } else {
  3017. $is_allowed = false;
  3018. }
  3019. if ($check_student_view) {
  3020. $is_allowed = $is_allowed && $_SESSION['studentview'] != 'studentview';
  3021. }
  3022. } else {
  3023. if ($check_student_view) {
  3024. $is_allowed = $is_courseAdmin && $_SESSION['studentview'] != 'studentview';
  3025. } else {
  3026. $is_allowed = $is_courseAdmin;
  3027. }
  3028. }
  3029. return $is_allowed;
  3030. } else {
  3031. return $is_courseAdmin;
  3032. }
  3033. }
  3034. /**
  3035. * Returns true if user is a course coach of at least one course in session.
  3036. *
  3037. * @param int $sessionId
  3038. *
  3039. * @return bool
  3040. */
  3041. function api_is_coach_of_course_in_session($sessionId)
  3042. {
  3043. if (api_is_platform_admin()) {
  3044. return true;
  3045. }
  3046. $userId = api_get_user_id();
  3047. $courseList = UserManager::get_courses_list_by_session(
  3048. $userId,
  3049. $sessionId
  3050. );
  3051. // Session visibility.
  3052. $visibility = api_get_session_visibility(
  3053. $sessionId,
  3054. null,
  3055. false
  3056. );
  3057. if ($visibility != SESSION_VISIBLE && !empty($courseList)) {
  3058. // Course Coach session visibility.
  3059. $blockedCourseCount = 0;
  3060. $closedVisibilityList = [
  3061. COURSE_VISIBILITY_CLOSED,
  3062. COURSE_VISIBILITY_HIDDEN,
  3063. ];
  3064. foreach ($courseList as $course) {
  3065. // Checking session visibility
  3066. $sessionCourseVisibility = api_get_session_visibility(
  3067. $sessionId,
  3068. $course['real_id']
  3069. );
  3070. $courseIsVisible = !in_array(
  3071. $course['visibility'],
  3072. $closedVisibilityList
  3073. );
  3074. if ($courseIsVisible === false || $sessionCourseVisibility == SESSION_INVISIBLE) {
  3075. $blockedCourseCount++;
  3076. }
  3077. }
  3078. // If all courses are blocked then no show in the list.
  3079. if ($blockedCourseCount === count($courseList)) {
  3080. $visibility = SESSION_INVISIBLE;
  3081. } else {
  3082. $visibility = SESSION_VISIBLE;
  3083. }
  3084. }
  3085. switch ($visibility) {
  3086. case SESSION_VISIBLE_READ_ONLY:
  3087. case SESSION_VISIBLE:
  3088. case SESSION_AVAILABLE:
  3089. return true;
  3090. break;
  3091. case SESSION_INVISIBLE:
  3092. return false;
  3093. }
  3094. return false;
  3095. }
  3096. /**
  3097. * Checks if a student can edit contents in a session depending
  3098. * on the session visibility.
  3099. *
  3100. * @param bool $tutor Whether to check if the user has the tutor role
  3101. * @param bool $coach Whether to check if the user has the coach role
  3102. *
  3103. * @return bool true: the user has the rights to edit, false: he does not
  3104. */
  3105. function api_is_allowed_to_session_edit($tutor = false, $coach = false)
  3106. {
  3107. if (api_is_allowed_to_edit($tutor, $coach)) {
  3108. // If I'm a teacher, I will return true in order to not affect the normal behaviour of Chamilo tools.
  3109. return true;
  3110. } else {
  3111. $sessionId = api_get_session_id();
  3112. if ($sessionId == 0) {
  3113. // I'm not in a session so i will return true to not affect the normal behaviour of Chamilo tools.
  3114. return true;
  3115. } else {
  3116. // I'm in a session and I'm a student
  3117. // Get the session visibility
  3118. $session_visibility = api_get_session_visibility($sessionId);
  3119. // if 5 the session is still available
  3120. switch ($session_visibility) {
  3121. case SESSION_VISIBLE_READ_ONLY: // 1
  3122. return false;
  3123. case SESSION_VISIBLE: // 2
  3124. return true;
  3125. case SESSION_INVISIBLE: // 3
  3126. return false;
  3127. case SESSION_AVAILABLE: //5
  3128. return true;
  3129. }
  3130. }
  3131. }
  3132. }
  3133. /**
  3134. * Checks whether the user is allowed in a specific tool for a specific action.
  3135. *
  3136. * @param string $tool the tool we are checking if the user has a certain permission
  3137. * @param string $action the action we are checking (add, edit, delete, move, visibility)
  3138. *
  3139. * @return bool
  3140. *
  3141. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  3142. * @author Julio Montoya
  3143. *
  3144. * @version 1.0
  3145. */
  3146. function api_is_allowed($tool, $action, $task_id = 0)
  3147. {
  3148. $_user = api_get_user_info();
  3149. $_course = api_get_course_info();
  3150. if (api_is_course_admin()) {
  3151. return true;
  3152. }
  3153. if (is_array($_course) and count($_course) > 0) {
  3154. require_once api_get_path(SYS_CODE_PATH).'permissions/permissions_functions.inc.php';
  3155. // Getting the permissions of this user.
  3156. if ($task_id == 0) {
  3157. $user_permissions = get_permissions('user', $_user['user_id']);
  3158. $_SESSION['total_permissions'][$_course['code']] = $user_permissions;
  3159. }
  3160. // Getting the permissions of the task.
  3161. if ($task_id != 0) {
  3162. $task_permissions = get_permissions('task', $task_id);
  3163. /* !!! */$_SESSION['total_permissions'][$_course['code']] = $task_permissions;
  3164. }
  3165. //print_r($_SESSION['total_permissions']);
  3166. // Getting the permissions of the groups of the user
  3167. //$groups_of_user = GroupManager::get_group_ids($_course['db_name'], $_user['user_id']);
  3168. //foreach($groups_of_user as $group)
  3169. // $this_group_permissions = get_permissions('group', $group);
  3170. // Getting the permissions of the courseroles of the user
  3171. $user_courserole_permissions = get_roles_permissions('user', $_user['user_id']);
  3172. // Getting the permissions of the platformroles of the user
  3173. //$user_platformrole_permissions = get_roles_permissions('user', $_user['user_id'], ', platform');
  3174. // Getting the permissions of the roles of the groups of the user
  3175. //foreach($groups_of_user as $group)
  3176. // $this_group_courserole_permissions = get_roles_permissions('group', $group);
  3177. // Getting the permissions of the platformroles of the groups of the user
  3178. //foreach($groups_of_user as $group)
  3179. // $this_group_platformrole_permissions = get_roles_permissions('group', $group, 'platform');
  3180. }
  3181. // If the permissions are limited, we have to map the extended ones to the limited ones.
  3182. if (api_get_setting('permissions') == 'limited') {
  3183. if ($action == 'Visibility') {
  3184. $action = 'Edit';
  3185. }
  3186. if ($action == 'Move') {
  3187. $action = 'Edit';
  3188. }
  3189. }
  3190. // The session that contains all the permissions already exists for this course
  3191. // so there is no need to requery everything.
  3192. //my_print_r($_SESSION['total_permissions'][$_course['code']][$tool]);
  3193. if (is_array($_SESSION['total_permissions'][$_course['code']][$tool])) {
  3194. if (in_array($action, $_SESSION['total_permissions'][$_course['code']][$tool])) {
  3195. return true;
  3196. } else {
  3197. return false;
  3198. }
  3199. }
  3200. }
  3201. /**
  3202. * Tells whether this user is an anonymous user.
  3203. *
  3204. * @param int $user_id User ID (optional, will take session ID if not provided)
  3205. * @param bool $db_check Whether to check in the database (true) or simply in
  3206. * the session (false) to see if the current user is the anonymous user
  3207. *
  3208. * @return bool true if this user is anonymous, false otherwise
  3209. */
  3210. function api_is_anonymous($user_id = null, $db_check = false)
  3211. {
  3212. if (!isset($user_id)) {
  3213. $user_id = api_get_user_id();
  3214. }
  3215. if ($db_check) {
  3216. $info = api_get_user_info($user_id);
  3217. if ($info['status'] == ANONYMOUS) {
  3218. return true;
  3219. }
  3220. }
  3221. $_user = api_get_user_info();
  3222. if (isset($_user['status']) && $_user['status'] == ANONYMOUS) {
  3223. //if ($_user['user_id'] == 0) {
  3224. // In some cases, api_set_anonymous doesn't seem to be triggered in local.inc.php. Make sure it is.
  3225. // Occurs in agenda for admin links - YW
  3226. global $use_anonymous;
  3227. if (isset($use_anonymous) && $use_anonymous) {
  3228. api_set_anonymous();
  3229. }
  3230. return true;
  3231. }
  3232. return (isset($_user['is_anonymous']) && $_user['is_anonymous'] === true) || $_user === false;
  3233. }
  3234. /**
  3235. * Displays message "You are not allowed here..." and exits the entire script.
  3236. *
  3237. * @param bool $print_headers Whether or not to print headers (default = false -> does not print them)
  3238. * @param string $message
  3239. * @param int $responseCode
  3240. */
  3241. function api_not_allowed(
  3242. $print_headers = false,
  3243. $message = null,
  3244. $responseCode = 0
  3245. ) {
  3246. if (api_get_setting('sso_authentication') === 'true') {
  3247. global $osso;
  3248. if ($osso) {
  3249. $osso->logout();
  3250. }
  3251. }
  3252. $home_url = api_get_path(WEB_PATH);
  3253. $user_id = api_get_user_id();
  3254. $course = api_get_course_id();
  3255. global $this_section;
  3256. if (CustomPages::enabled() && !isset($user_id)) {
  3257. if (empty($user_id)) {
  3258. // Why the CustomPages::enabled() need to be to set the request_uri
  3259. $_SESSION['request_uri'] = $_SERVER['REQUEST_URI'];
  3260. }
  3261. CustomPages::display(CustomPages::INDEX_UNLOGGED);
  3262. }
  3263. $origin = api_get_origin();
  3264. $msg = null;
  3265. if (isset($message)) {
  3266. $msg = $message;
  3267. } else {
  3268. $msg = Display::return_message(
  3269. get_lang('NotAllowedClickBack').'
  3270. <script>function goBack(){window.history.back();}</script>',
  3271. 'error',
  3272. false
  3273. );
  3274. $msg .= '<p class="text-center">
  3275. <a onclick="goBack();" class="btn btn-default" href="'.$home_url.'">'.get_lang('GoBack').'</a>
  3276. </p>';
  3277. }
  3278. $msg = Display::div($msg, ['align' => 'center']);
  3279. $show_headers = 0;
  3280. if ($print_headers && $origin != 'learnpath') {
  3281. $show_headers = 1;
  3282. }
  3283. $tpl = new Template(null, $show_headers, $show_headers, false, true, false, true, $responseCode);
  3284. $tpl->assign('hide_login_link', 1);
  3285. $tpl->assign('content', $msg);
  3286. if (($user_id != 0 && !api_is_anonymous()) &&
  3287. (!isset($course) || $course == -1) &&
  3288. empty($_GET['cidReq'])
  3289. ) {
  3290. // if the access is not authorized and there is some login information
  3291. // but the cidReq is not found, assume we are missing course data and send the user
  3292. // to the user_portal
  3293. $tpl->display_one_col_template();
  3294. exit;
  3295. }
  3296. if (!empty($_SERVER['REQUEST_URI']) &&
  3297. (
  3298. !empty($_GET['cidReq']) ||
  3299. $this_section == SECTION_MYPROFILE ||
  3300. $this_section == SECTION_PLATFORM_ADMIN
  3301. )
  3302. ) {
  3303. $courseCode = api_get_course_id();
  3304. // Only display form and return to the previous URL if there was a course ID included
  3305. if ($user_id != 0 && !api_is_anonymous()) {
  3306. //if there is a user ID, then the user is not allowed but the session is still there. Say so and exit
  3307. $tpl->assign('content', $msg);
  3308. $tpl->display_one_col_template();
  3309. exit;
  3310. }
  3311. if (!is_null($courseCode)) {
  3312. api_set_firstpage_parameter($courseCode);
  3313. }
  3314. // If the user has no user ID, then his session has expired
  3315. $form = api_get_not_allowed_login_form();
  3316. // see same text in auth/gotocourse.php and main_api.lib.php function api_not_allowed (above)
  3317. $content = Display::return_message(get_lang('NotAllowed'), 'error', false);
  3318. if (!empty($courseCode)) {
  3319. $content .= '<h4>'.get_lang('LoginToGoToThisCourse').'</h4>';
  3320. }
  3321. if (api_is_cas_activated()) {
  3322. $content .= Display::return_message(sprintf(get_lang('YouHaveAnInstitutionalAccount'), api_get_setting("Institution")), '', false);
  3323. $content .= Display::div(
  3324. "<br/><a href='".get_cas_direct_URL(api_get_course_id())."'>".sprintf(get_lang('LoginWithYourAccount'), api_get_setting("Institution"))."</a><br/><br/>",
  3325. ['align' => 'center']
  3326. );
  3327. $content .= Display::return_message(get_lang('YouDontHaveAnInstitutionAccount'));
  3328. $content .= "<p style='text-align:center'><a href='#' onclick='$(this).parent().next().toggle()'>".get_lang('LoginWithExternalAccount')."</a></p>";
  3329. $content .= "<div style='display:none;'>";
  3330. }
  3331. $content .= '<div class="well">';
  3332. $content .= $form->returnForm();
  3333. $content .= '</div>';
  3334. if (api_is_cas_activated()) {
  3335. $content .= "</div>";
  3336. }
  3337. if (!empty($courseCode)) {
  3338. $content .= '<hr/><p style="text-align:center"><a href="'.$home_url.'">'.
  3339. get_lang('ReturnToCourseHomepage').'</a></p>';
  3340. } else {
  3341. $content .= '<hr/><p style="text-align:center"><a href="'.$home_url.'">'.
  3342. get_lang('BackHome').'</a></p>';
  3343. }
  3344. $tpl->setLoginBodyClass();
  3345. $tpl->assign('content', $content);
  3346. $tpl->display_one_col_template();
  3347. exit;
  3348. }
  3349. if ($user_id != 0 && !api_is_anonymous()) {
  3350. $tpl->display_one_col_template();
  3351. exit;
  3352. }
  3353. $msg = null;
  3354. // The session is over and we were not in a course,
  3355. // or we try to get directly to a private course without being logged
  3356. $courseId = api_get_course_int_id();
  3357. if (!empty($courseId)) {
  3358. api_set_firstpage_parameter(api_get_course_id());
  3359. $tpl->setLoginBodyClass();
  3360. // see same text in auth/gotocourse.php and main_api.lib.php function api_not_allowed (bellow)
  3361. $msg = Display::return_message(get_lang('NotAllowed'), 'error', false);
  3362. $msg .= '<h4>'.get_lang('LoginToGoToThisCourse').'</h4>';
  3363. $casEnabled = api_is_cas_activated();
  3364. if ($casEnabled) {
  3365. $msg .= Display::return_message(
  3366. sprintf(get_lang('YouHaveAnInstitutionalAccount'), api_get_setting("Institution")),
  3367. '',
  3368. false
  3369. );
  3370. $msg .= Display::div("<br/><a href='".get_cas_direct_URL(api_get_course_int_id())."'>".getCASLogoHTML()." ".sprintf(get_lang('LoginWithYourAccount'), api_get_setting("Institution"))."</a><br/><br/>", ['align' => 'center']);
  3371. $msg .= Display::return_message(get_lang('YouDontHaveAnInstitutionAccount'));
  3372. $msg .= "<p style='text-align:center'><a href='#' onclick='$(this).parent().next().toggle()'>".get_lang('LoginWithExternalAccount')."</a></p>";
  3373. $msg .= "<div style='display:none;'>";
  3374. }
  3375. $form = api_get_not_allowed_login_form();
  3376. $msg .= '<div class="well">';
  3377. $msg .= $form->returnForm();
  3378. $msg .= '</div>';
  3379. if ($casEnabled) {
  3380. $msg .= "</div>";
  3381. }
  3382. } else {
  3383. // we were not in a course, return to home page
  3384. $msg = Display::return_message(
  3385. get_lang('NotAllowed'),
  3386. 'error',
  3387. false
  3388. );
  3389. $msg .= '<p class="text-center">
  3390. <a class="btn btn-default" href="'.$home_url.'">'.get_lang('BackHome').'</a>
  3391. </p>';
  3392. if (!empty($message)) {
  3393. $msg = $message;
  3394. }
  3395. if (api_is_anonymous()) {
  3396. $form = api_get_not_allowed_login_form();
  3397. $msg .= '<div class="well">';
  3398. $msg .= $form->returnForm();
  3399. $msg .= '</div>';
  3400. }
  3401. }
  3402. $tpl->assign('content', $msg);
  3403. $tpl->display_one_col_template();
  3404. exit;
  3405. }
  3406. /**
  3407. * @return FormValidator
  3408. */
  3409. function api_get_not_allowed_login_form()
  3410. {
  3411. $action = api_get_self().'?'.Security::remove_XSS($_SERVER['QUERY_STRING']);
  3412. $action = str_replace('&amp;', '&', $action);
  3413. Session::write('redirect_after_not_allow_page', $action);
  3414. $action .= '&redirect_after_not_allow_page=1';
  3415. $form = new FormValidator(
  3416. 'formLogin',
  3417. 'post',
  3418. $action,
  3419. null,
  3420. ['class' => 'form-stacked']
  3421. );
  3422. $params = [
  3423. 'placeholder' => get_lang('UserName'),
  3424. 'class' => 'col-md-3',
  3425. ];
  3426. if (api_browser_support('autocapitalize')) {
  3427. $params['autocapitalize'] = 'none';
  3428. }
  3429. $form->addElement(
  3430. 'text',
  3431. 'login',
  3432. null,
  3433. $params
  3434. );
  3435. $form->addElement(
  3436. 'password',
  3437. 'password',
  3438. null,
  3439. ['placeholder' => get_lang('Password'), 'class' => 'col-md-3']
  3440. ); //new
  3441. $form->addButtonNext(get_lang('LoginEnter'), 'submitAuth');
  3442. return $form;
  3443. }
  3444. /**
  3445. * Gets a UNIX timestamp from a database (MySQL) datetime format string.
  3446. *
  3447. * @param $last_post_datetime standard output date in a sql query
  3448. *
  3449. * @return int timestamp
  3450. *
  3451. * @author Toon Van Hoecke <Toon.VanHoecke@UGent.be>
  3452. *
  3453. * @version October 2003
  3454. * @desc convert sql date to unix timestamp
  3455. */
  3456. function convert_sql_date($last_post_datetime)
  3457. {
  3458. list($last_post_date, $last_post_time) = explode(' ', $last_post_datetime);
  3459. list($year, $month, $day) = explode('-', $last_post_date);
  3460. list($hour, $min, $sec) = explode(':', $last_post_time);
  3461. return mktime((int) $hour, (int) $min, (int) $sec, (int) $month, (int) $day, (int) $year);
  3462. }
  3463. /**
  3464. * Gets item visibility from the item_property table.
  3465. *
  3466. * Getting the visibility is done by getting the last updated visibility entry,
  3467. * using the largest session ID found if session 0 and another was found (meaning
  3468. * the only one that is actually from the session, in case there are results from
  3469. * session 0 *AND* session n).
  3470. *
  3471. * @param array $_course Course properties array (result of api_get_course_info())
  3472. * @param string $tool Tool (learnpath, document, etc)
  3473. * @param int $id The item ID in the given tool
  3474. * @param int $session The session ID (optional)
  3475. * @param int $user_id
  3476. * @param string $type
  3477. * @param string $group_id
  3478. *
  3479. * @return int -1 on error, 0 if invisible, 1 if visible
  3480. */
  3481. function api_get_item_visibility(
  3482. $_course,
  3483. $tool,
  3484. $id,
  3485. $session = 0,
  3486. $user_id = null,
  3487. $type = null,
  3488. $group_id = null
  3489. ) {
  3490. if (!is_array($_course) || count($_course) == 0 || empty($tool) || empty($id)) {
  3491. return -1;
  3492. }
  3493. $tool = Database::escape_string($tool);
  3494. $id = (int) $id;
  3495. $session = (int) $session;
  3496. $TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3497. $course_id = (int) $_course['real_id'];
  3498. $userCondition = '';
  3499. if (!empty($user_id)) {
  3500. $user_id = (int) $user_id;
  3501. $userCondition = " AND to_user_id = $user_id ";
  3502. }
  3503. $typeCondition = '';
  3504. if (!empty($type)) {
  3505. $type = Database::escape_string($type);
  3506. $typeCondition = " AND lastedit_type = '$type' ";
  3507. }
  3508. $groupCondition = '';
  3509. if (!empty($group_id)) {
  3510. $group_id = (int) $group_id;
  3511. $groupCondition = " AND to_group_id = '$group_id' ";
  3512. }
  3513. $sql = "SELECT visibility
  3514. FROM $TABLE_ITEMPROPERTY
  3515. WHERE
  3516. c_id = $course_id AND
  3517. tool = '$tool' AND
  3518. ref = $id AND
  3519. (session_id = $session OR session_id = 0 OR session_id IS NULL)
  3520. $userCondition $typeCondition $groupCondition
  3521. ORDER BY session_id DESC, lastedit_date DESC
  3522. LIMIT 1";
  3523. $res = Database::query($sql);
  3524. if ($res === false || Database::num_rows($res) == 0) {
  3525. return -1;
  3526. }
  3527. $row = Database::fetch_array($res);
  3528. return (int) $row['visibility'];
  3529. }
  3530. /**
  3531. * Delete a row in the c_item_property table.
  3532. *
  3533. * @param array $courseInfo
  3534. * @param string $tool
  3535. * @param int $itemId
  3536. * @param int $userId
  3537. * @param int $groupId group.iid
  3538. * @param int $sessionId
  3539. *
  3540. * @return false|null
  3541. */
  3542. function api_item_property_delete(
  3543. $courseInfo,
  3544. $tool,
  3545. $itemId,
  3546. $userId,
  3547. $groupId = 0,
  3548. $sessionId = 0
  3549. ) {
  3550. if (empty($courseInfo)) {
  3551. return false;
  3552. }
  3553. $courseId = (int) $courseInfo['real_id'];
  3554. if (empty($courseId) || empty($tool) || empty($itemId)) {
  3555. return false;
  3556. }
  3557. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3558. $tool = Database::escape_string($tool);
  3559. $itemId = intval($itemId);
  3560. $userId = intval($userId);
  3561. $groupId = intval($groupId);
  3562. $sessionId = intval($sessionId);
  3563. $groupCondition = " AND to_group_id = $groupId ";
  3564. if (empty($groupId)) {
  3565. $groupCondition = " AND (to_group_id is NULL OR to_group_id = 0) ";
  3566. }
  3567. $userCondition = " AND to_user_id = $userId ";
  3568. if (empty($userId)) {
  3569. $userCondition = " AND (to_user_id is NULL OR to_user_id = 0) ";
  3570. }
  3571. $sessionCondition = api_get_session_condition($sessionId, true, false, 'session_id');
  3572. $sql = "DELETE FROM $table
  3573. WHERE
  3574. c_id = $courseId AND
  3575. tool = '$tool' AND
  3576. ref = $itemId
  3577. $sessionCondition
  3578. $userCondition
  3579. $groupCondition
  3580. ";
  3581. Database::query($sql);
  3582. }
  3583. /**
  3584. * Updates or adds item properties to the Item_propetry table
  3585. * Tool and lastedit_type are language independant strings (langvars->get_lang!).
  3586. *
  3587. * @param array $_course array with course properties
  3588. * @param string $tool tool id, linked to 'rubrique' of the course tool_list (Warning: language sensitive !!)
  3589. * @param int $item_id id of the item itself, linked to key of every tool ('id', ...)
  3590. * @param string $last_edit_type add or update action
  3591. * (1) message to be translated (in trad4all) : e.g. DocumentAdded, DocumentUpdated;
  3592. * (2) "delete"
  3593. * (3) "visible"
  3594. * (4) "invisible"
  3595. * @param int $user_id id of the editing/adding user
  3596. * @param array $groupInfo must include group.iid/group.od
  3597. * @param int $to_user_id id of the intended user (always has priority over $to_group_id !), only relevant for $type (1)
  3598. * @param string $start_visible 0000-00-00 00:00:00 format
  3599. * @param string $end_visible 0000-00-00 00:00:00 format
  3600. * @param int $session_id The session ID, if any, otherwise will default to 0
  3601. *
  3602. * @return bool false if update fails
  3603. *
  3604. * @author Toon Van Hoecke <Toon.VanHoecke@UGent.be>, Ghent University
  3605. *
  3606. * @version January 2005
  3607. * @desc update the item_properties table (if entry not exists, insert) of the course
  3608. */
  3609. function api_item_property_update(
  3610. $_course,
  3611. $tool,
  3612. $item_id,
  3613. $last_edit_type,
  3614. $user_id,
  3615. $groupInfo = [],
  3616. $to_user_id = null,
  3617. $start_visible = '',
  3618. $end_visible = '',
  3619. $session_id = 0
  3620. ) {
  3621. if (empty($_course)) {
  3622. return false;
  3623. }
  3624. $course_id = $_course['real_id'];
  3625. if (empty($course_id)) {
  3626. return false;
  3627. }
  3628. $to_group_id = 0;
  3629. if (!empty($groupInfo) && isset($groupInfo['iid'])) {
  3630. $to_group_id = (int) $groupInfo['iid'];
  3631. }
  3632. $em = Database::getManager();
  3633. // Definition of variables.
  3634. $tool = Database::escape_string($tool);
  3635. $item_id = (int) $item_id;
  3636. $lastEditTypeNoFilter = $last_edit_type;
  3637. $last_edit_type = Database::escape_string($last_edit_type);
  3638. $user_id = (int) $user_id;
  3639. $startVisible = "NULL";
  3640. if (!empty($start_visible)) {
  3641. $start_visible = Database::escape_string($start_visible);
  3642. $startVisible = "'$start_visible'";
  3643. }
  3644. $endVisible = "NULL";
  3645. if (!empty($end_visible)) {
  3646. $end_visible = Database::escape_string($end_visible);
  3647. $endVisible = "'$end_visible'";
  3648. }
  3649. $to_filter = '';
  3650. $time = api_get_utc_datetime();
  3651. if (!empty($session_id)) {
  3652. $session_id = (int) $session_id;
  3653. } else {
  3654. $session_id = api_get_session_id();
  3655. }
  3656. // Definition of tables.
  3657. $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3658. if ($to_user_id <= 0) {
  3659. $to_user_id = null; // No to_user_id set
  3660. }
  3661. if (!is_null($to_user_id)) {
  3662. // $to_user_id has more priority than $to_group_id
  3663. $to_user_id = (int) $to_user_id;
  3664. $to_field = 'to_user_id';
  3665. $to_value = $to_user_id;
  3666. } else {
  3667. // $to_user_id is not set.
  3668. $to_field = 'to_group_id';
  3669. $to_value = $to_group_id;
  3670. }
  3671. $toValueCondition = empty($to_value) ? 'NULL' : "'$to_value'";
  3672. // Set filters for $to_user_id and $to_group_id, with priority for $to_user_id
  3673. $condition_session = " AND session_id = $session_id ";
  3674. if (empty($session_id)) {
  3675. $condition_session = ' AND (session_id = 0 OR session_id IS NULL) ';
  3676. }
  3677. $filter = " c_id = $course_id AND tool = '$tool' AND ref = $item_id $condition_session ";
  3678. // Check whether $to_user_id and $to_group_id are passed in the function call.
  3679. // If both are not passed (both are null) then it is a message for everybody and $to_group_id should be 0 !
  3680. if (is_null($to_user_id) && is_null($to_group_id)) {
  3681. $to_group_id = 0;
  3682. }
  3683. if (!is_null($to_user_id)) {
  3684. // Set filter to intended user.
  3685. $to_filter = " AND to_user_id = $to_user_id $condition_session";
  3686. } else {
  3687. // Set filter to intended group.
  3688. if (($to_group_id != 0) && $to_group_id == strval(intval($to_group_id))) {
  3689. $to_filter = " AND to_group_id = $to_group_id $condition_session";
  3690. }
  3691. }
  3692. // Adding filter if set.
  3693. $filter .= $to_filter;
  3694. // Update if possible
  3695. $set_type = '';
  3696. switch ($lastEditTypeNoFilter) {
  3697. case 'delete':
  3698. // delete = make item only visible for the platform admin.
  3699. $visibility = '2';
  3700. if (!empty($session_id)) {
  3701. // Check whether session id already exist into item_properties for updating visibility or add it.
  3702. $sql = "SELECT session_id FROM $tableItemProperty
  3703. WHERE
  3704. c_id = $course_id AND
  3705. tool = '$tool' AND
  3706. ref = $item_id AND
  3707. session_id = $session_id";
  3708. $rs = Database::query($sql);
  3709. if (Database::num_rows($rs) > 0) {
  3710. $sql = "UPDATE $tableItemProperty
  3711. SET lastedit_type = '".str_replace('_', '', ucwords($tool))."Deleted',
  3712. lastedit_date = '$time',
  3713. lastedit_user_id = $user_id,
  3714. visibility = $visibility,
  3715. session_id = $session_id $set_type
  3716. WHERE $filter";
  3717. $result = Database::query($sql);
  3718. } else {
  3719. $sql = "INSERT INTO $tableItemProperty (c_id, tool, ref, insert_date, insert_user_id, lastedit_date, lastedit_type, lastedit_user_id, $to_field, visibility, start_visible, end_visible, session_id)
  3720. VALUES ($course_id, '$tool',$item_id, '$time', $user_id, '$time', '$last_edit_type',$user_id, $toValueCondition, $visibility, $startVisible, $endVisible, $session_id)";
  3721. $result = Database::query($sql);
  3722. $id = Database::insert_id();
  3723. if ($id) {
  3724. $sql = "UPDATE $tableItemProperty SET id = iid WHERE iid = $id";
  3725. Database::query($sql);
  3726. }
  3727. }
  3728. } else {
  3729. $sql = "UPDATE $tableItemProperty
  3730. SET
  3731. lastedit_type='".str_replace('_', '', ucwords($tool))."Deleted',
  3732. lastedit_date='$time',
  3733. lastedit_user_id = $user_id,
  3734. visibility = $visibility $set_type
  3735. WHERE $filter";
  3736. $result = Database::query($sql);
  3737. }
  3738. break;
  3739. case 'visible': // Change item to visible.
  3740. $visibility = '1';
  3741. if (!empty($session_id)) {
  3742. // Check whether session id already exist into item_properties for updating visibility or add it.
  3743. $sql = "SELECT session_id FROM $tableItemProperty
  3744. WHERE
  3745. c_id = $course_id AND
  3746. tool = '$tool' AND
  3747. ref = $item_id AND
  3748. session_id = $session_id";
  3749. $rs = Database::query($sql);
  3750. if (Database::num_rows($rs) > 0) {
  3751. $sql = "UPDATE $tableItemProperty
  3752. SET
  3753. lastedit_type='".str_replace('_', '', ucwords($tool))."Visible',
  3754. lastedit_date='$time',
  3755. lastedit_user_id = $user_id,
  3756. visibility = $visibility,
  3757. session_id = $session_id $set_type
  3758. WHERE $filter";
  3759. $result = Database::query($sql);
  3760. } else {
  3761. $sql = "INSERT INTO $tableItemProperty (c_id, tool, ref, insert_date, insert_user_id, lastedit_date, lastedit_type, lastedit_user_id, $to_field, visibility, start_visible, end_visible, session_id)
  3762. VALUES ($course_id, '$tool', $item_id, '$time', $user_id, '$time', '$last_edit_type', $user_id, $toValueCondition, $visibility, $startVisible, $endVisible, $session_id)";
  3763. $result = Database::query($sql);
  3764. $id = Database::insert_id();
  3765. if ($id) {
  3766. $sql = "UPDATE $tableItemProperty SET id = iid WHERE iid = $id";
  3767. Database::query($sql);
  3768. }
  3769. }
  3770. } else {
  3771. $sql = "UPDATE $tableItemProperty
  3772. SET
  3773. lastedit_type='".str_replace('_', '', ucwords($tool))."Visible',
  3774. lastedit_date='$time',
  3775. lastedit_user_id = $user_id,
  3776. visibility = $visibility $set_type
  3777. WHERE $filter";
  3778. $result = Database::query($sql);
  3779. }
  3780. break;
  3781. case 'invisible': // Change item to invisible.
  3782. $visibility = '0';
  3783. if (!empty($session_id)) {
  3784. // Check whether session id already exist into item_properties for updating visibility or add it
  3785. $sql = "SELECT session_id FROM $tableItemProperty
  3786. WHERE
  3787. c_id = $course_id AND
  3788. tool = '$tool' AND
  3789. ref = $item_id AND
  3790. session_id = $session_id";
  3791. $rs = Database::query($sql);
  3792. if (Database::num_rows($rs) > 0) {
  3793. $sql = "UPDATE $tableItemProperty
  3794. SET
  3795. lastedit_type = '".str_replace('_', '', ucwords($tool))."Invisible',
  3796. lastedit_date = '$time',
  3797. lastedit_user_id = $user_id,
  3798. visibility = $visibility,
  3799. session_id = $session_id $set_type
  3800. WHERE $filter";
  3801. $result = Database::query($sql);
  3802. } else {
  3803. $sql = "INSERT INTO $tableItemProperty (c_id, tool, ref, insert_date, insert_user_id, lastedit_date, lastedit_type, lastedit_user_id,$to_field, visibility, start_visible, end_visible, session_id)
  3804. VALUES ($course_id, '$tool', $item_id, '$time', $user_id, '$time', '$last_edit_type', $user_id, $toValueCondition, $visibility, $startVisible, $endVisible, $session_id)";
  3805. $result = Database::query($sql);
  3806. $id = Database::insert_id();
  3807. if ($id) {
  3808. $sql = "UPDATE $tableItemProperty SET id = iid WHERE iid = $id";
  3809. Database::query($sql);
  3810. }
  3811. }
  3812. } else {
  3813. $sql = "UPDATE $tableItemProperty
  3814. SET
  3815. lastedit_type = '".str_replace('_', '', ucwords($tool))."Invisible',
  3816. lastedit_date = '$time',
  3817. lastedit_user_id = $user_id,
  3818. visibility = $visibility $set_type
  3819. WHERE $filter";
  3820. $result = Database::query($sql);
  3821. }
  3822. break;
  3823. default: // The item will be added or updated.
  3824. $set_type = ", lastedit_type = '$last_edit_type' ";
  3825. $visibility = '1';
  3826. //$filter .= $to_filter; already added
  3827. $sql = "UPDATE $tableItemProperty
  3828. SET
  3829. lastedit_date = '$time',
  3830. lastedit_user_id = $user_id $set_type
  3831. WHERE $filter";
  3832. $result = Database::query($sql);
  3833. }
  3834. // Insert if no entries are found (can only happen in case of $last_edit_type switch is 'default').
  3835. if ($result == false || Database::affected_rows($result) == 0) {
  3836. $objCourse = $em->find('ChamiloCoreBundle:Course', intval($course_id));
  3837. $objTime = new DateTime('now', new DateTimeZone('UTC'));
  3838. $objUser = api_get_user_entity($user_id);
  3839. if (empty($objUser)) {
  3840. // Use anonymous
  3841. $user_id = api_get_anonymous_id();
  3842. $objUser = api_get_user_entity($user_id);
  3843. }
  3844. $objGroup = null;
  3845. if (!empty($to_group_id)) {
  3846. $objGroup = $em->find('ChamiloCourseBundle:CGroupInfo', $to_group_id);
  3847. }
  3848. $objToUser = api_get_user_entity($to_user_id);
  3849. $objSession = $em->find('ChamiloCoreBundle:Session', intval($session_id));
  3850. $startVisibleDate = !empty($start_visible) ? new DateTime($start_visible, new DateTimeZone('UTC')) : null;
  3851. $endVisibleDate = !empty($endVisibleDate) ? new DateTime($endVisibleDate, new DateTimeZone('UTC')) : null;
  3852. $cItemProperty = new CItemProperty($objCourse);
  3853. $cItemProperty
  3854. ->setTool($tool)
  3855. ->setRef($item_id)
  3856. ->setInsertDate($objTime)
  3857. ->setInsertUser($objUser)
  3858. ->setLasteditDate($objTime)
  3859. ->setLasteditType($last_edit_type)
  3860. ->setGroup($objGroup)
  3861. ->setToUser($objToUser)
  3862. ->setVisibility($visibility)
  3863. ->setStartVisible($startVisibleDate)
  3864. ->setEndVisible($endVisibleDate)
  3865. ->setSession($objSession);
  3866. $em->persist($cItemProperty);
  3867. $em->flush();
  3868. $id = $cItemProperty->getIid();
  3869. if ($id) {
  3870. $cItemProperty->setId($id);
  3871. $em->merge($cItemProperty);
  3872. $em->flush();
  3873. return false;
  3874. }
  3875. }
  3876. return true;
  3877. }
  3878. /**
  3879. * Gets item property by tool.
  3880. *
  3881. * @param string course code
  3882. * @param string tool name, linked to 'rubrique' of the course tool_list (Warning: language sensitive !!)
  3883. * @param int id of the item itself, linked to key of every tool ('id', ...), "*" = all items of the tool
  3884. * @param int $session_id
  3885. * @param string $tool
  3886. * @param string $course_code
  3887. *
  3888. * @return array All fields from c_item_property (all rows found) or empty array
  3889. */
  3890. function api_get_item_property_by_tool($tool, $course_code, $session_id = null)
  3891. {
  3892. $course_info = api_get_course_info($course_code);
  3893. $tool = Database::escape_string($tool);
  3894. // Definition of tables.
  3895. $item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3896. $session_id = (int) $session_id;
  3897. $session_condition = ' AND session_id = '.$session_id;
  3898. if (empty($session_id)) {
  3899. $session_condition = " AND (session_id = 0 OR session_id IS NULL) ";
  3900. }
  3901. $course_id = $course_info['real_id'];
  3902. $sql = "SELECT * FROM $item_property_table
  3903. WHERE
  3904. c_id = $course_id AND
  3905. tool = '$tool'
  3906. $session_condition ";
  3907. $rs = Database::query($sql);
  3908. $list = [];
  3909. if (Database::num_rows($rs) > 0) {
  3910. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  3911. $list[] = $row;
  3912. }
  3913. }
  3914. return $list;
  3915. }
  3916. /**
  3917. * Gets item property by tool and user.
  3918. *
  3919. * @param int $userId
  3920. * @param int $tool
  3921. * @param int $courseId
  3922. * @param int $session_id
  3923. *
  3924. * @return array
  3925. */
  3926. function api_get_item_property_list_by_tool_by_user(
  3927. $userId,
  3928. $tool,
  3929. $courseId,
  3930. $session_id = 0
  3931. ) {
  3932. $userId = intval($userId);
  3933. $tool = Database::escape_string($tool);
  3934. $session_id = intval($session_id);
  3935. $courseId = intval($courseId);
  3936. // Definition of tables.
  3937. $item_property_table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3938. $session_condition = ' AND session_id = '.$session_id;
  3939. if (empty($session_id)) {
  3940. $session_condition = " AND (session_id = 0 OR session_id IS NULL) ";
  3941. }
  3942. $sql = "SELECT * FROM $item_property_table
  3943. WHERE
  3944. insert_user_id = $userId AND
  3945. c_id = $courseId AND
  3946. tool = '$tool'
  3947. $session_condition ";
  3948. $rs = Database::query($sql);
  3949. $list = [];
  3950. if (Database::num_rows($rs) > 0) {
  3951. while ($row = Database::fetch_array($rs, 'ASSOC')) {
  3952. $list[] = $row;
  3953. }
  3954. }
  3955. return $list;
  3956. }
  3957. /**
  3958. * Gets item property id from tool of a course.
  3959. *
  3960. * @param string $course_code course code
  3961. * @param string $tool tool name, linked to 'rubrique' of the course tool_list (Warning: language sensitive !!)
  3962. * @param int $ref id of the item itself, linked to key of every tool ('id', ...), "*" = all items of the tool
  3963. * @param int $sessionId Session ID (optional)
  3964. *
  3965. * @return int
  3966. */
  3967. function api_get_item_property_id($course_code, $tool, $ref, $sessionId = 0)
  3968. {
  3969. $course_info = api_get_course_info($course_code);
  3970. $tool = Database::escape_string($tool);
  3971. $ref = (int) $ref;
  3972. // Definition of tables.
  3973. $tableItemProperty = Database::get_course_table(TABLE_ITEM_PROPERTY);
  3974. $course_id = $course_info['real_id'];
  3975. $sessionId = (int) $sessionId;
  3976. $sessionCondition = " AND session_id = $sessionId ";
  3977. if (empty($sessionId)) {
  3978. $sessionCondition = ' AND (session_id = 0 OR session_id IS NULL) ';
  3979. }
  3980. $sql = "SELECT id FROM $tableItemProperty
  3981. WHERE
  3982. c_id = $course_id AND
  3983. tool = '$tool' AND
  3984. ref = $ref
  3985. $sessionCondition";
  3986. $rs = Database::query($sql);
  3987. $item_property_id = '';
  3988. if (Database::num_rows($rs) > 0) {
  3989. $row = Database::fetch_array($rs);
  3990. $item_property_id = $row['id'];
  3991. }
  3992. return $item_property_id;
  3993. }
  3994. /**
  3995. * Inserts a record in the track_e_item_property table (No update).
  3996. *
  3997. * @param string $tool
  3998. * @param int $ref
  3999. * @param string $title
  4000. * @param string $content
  4001. * @param int $progress
  4002. *
  4003. * @return bool|int
  4004. */
  4005. function api_track_item_property_update($tool, $ref, $title, $content, $progress)
  4006. {
  4007. $tbl_stats_item_property = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY);
  4008. $course_id = api_get_course_int_id(); //numeric
  4009. $course_code = api_get_course_id(); //alphanumeric
  4010. $item_property_id = api_get_item_property_id($course_code, $tool, $ref);
  4011. if (!empty($item_property_id)) {
  4012. $sql = "INSERT IGNORE INTO $tbl_stats_item_property SET
  4013. course_id = '$course_id',
  4014. item_property_id = '$item_property_id',
  4015. title = '".Database::escape_string($title)."',
  4016. content = '".Database::escape_string($content)."',
  4017. progress = '".intval($progress)."',
  4018. lastedit_date = '".api_get_utc_datetime()."',
  4019. lastedit_user_id = '".api_get_user_id()."',
  4020. session_id = '".api_get_session_id()."'";
  4021. $result = Database::query($sql);
  4022. $affected_rows = Database::affected_rows($result);
  4023. return $affected_rows;
  4024. }
  4025. return false;
  4026. }
  4027. /**
  4028. * @param string $tool
  4029. * @param int $ref
  4030. *
  4031. * @return array|resource
  4032. */
  4033. function api_get_track_item_property_history($tool, $ref)
  4034. {
  4035. $tbl_stats_item_property = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY);
  4036. $course_id = api_get_course_int_id(); //numeric
  4037. $course_code = api_get_course_id(); //alphanumeric
  4038. $item_property_id = api_get_item_property_id($course_code, $tool, $ref);
  4039. $sql = "SELECT * FROM $tbl_stats_item_property
  4040. WHERE item_property_id = $item_property_id AND course_id = $course_id
  4041. ORDER BY lastedit_date DESC";
  4042. $result = Database::query($sql);
  4043. if ($result === false or $result === null) {
  4044. $result = [];
  4045. } else {
  4046. $result = Database::store_result($result, 'ASSOC');
  4047. }
  4048. return $result;
  4049. }
  4050. /**
  4051. * Gets item property data from tool of a course id.
  4052. *
  4053. * @param int $course_id
  4054. * @param string $tool tool name, linked to 'rubrique' of the course tool_list (Warning: language sensitive !!)
  4055. * @param int $ref id of the item itself, linked to key of every tool ('id', ...), "*" = all items of the tool
  4056. * @param int $session_id
  4057. * @param int $groupId
  4058. *
  4059. * @return array with all fields from c_item_property, empty array if not found or false if course could not be found
  4060. */
  4061. function api_get_item_property_info($course_id, $tool, $ref, $session_id = 0, $groupId = 0)
  4062. {
  4063. $courseInfo = api_get_course_info_by_id($course_id);
  4064. if (empty($courseInfo)) {
  4065. return false;
  4066. }
  4067. $tool = Database::escape_string($tool);
  4068. $course_id = $courseInfo['real_id'];
  4069. $ref = (int) $ref;
  4070. $session_id = (int) $session_id;
  4071. $sessionCondition = " session_id = $session_id";
  4072. if (empty($session_id)) {
  4073. $sessionCondition = ' (session_id = 0 OR session_id IS NULL) ';
  4074. }
  4075. // Definition of tables.
  4076. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  4077. $sql = "SELECT * FROM $table
  4078. WHERE
  4079. c_id = $course_id AND
  4080. tool = '$tool' AND
  4081. ref = $ref AND
  4082. $sessionCondition ";
  4083. if (!empty($groupId)) {
  4084. $groupId = (int) $groupId;
  4085. $sql .= " AND to_group_id = $groupId ";
  4086. }
  4087. $rs = Database::query($sql);
  4088. $row = [];
  4089. if (Database::num_rows($rs) > 0) {
  4090. $row = Database::fetch_array($rs, 'ASSOC');
  4091. }
  4092. return $row;
  4093. }
  4094. /**
  4095. * Displays a combo box so the user can select his/her preferred language.
  4096. *
  4097. * @param string The desired name= value for the select
  4098. * @param bool Whether we use the JQuery Chozen library or not
  4099. * (in some cases, like the indexing language picker, it can alter the presentation)
  4100. *
  4101. * @return string
  4102. */
  4103. function api_get_languages_combo($name = 'language')
  4104. {
  4105. $ret = '';
  4106. $platformLanguage = api_get_setting('platformLanguage');
  4107. // Retrieve a complete list of all the languages.
  4108. $language_list = api_get_languages();
  4109. if (count($language_list['name']) < 2) {
  4110. return $ret;
  4111. }
  4112. // The the current language of the user so that his/her language occurs as selected in the dropdown menu.
  4113. if (isset($_SESSION['user_language_choice'])) {
  4114. $default = $_SESSION['user_language_choice'];
  4115. } else {
  4116. $default = $platformLanguage;
  4117. }
  4118. $languages = $language_list['name'];
  4119. $folder = $language_list['folder'];
  4120. $ret .= '<select name="'.$name.'" id="language_chosen" class="selectpicker form-control">';
  4121. foreach ($languages as $key => $value) {
  4122. if ($folder[$key] == $default) {
  4123. $selected = ' selected="selected"';
  4124. } else {
  4125. $selected = '';
  4126. }
  4127. $ret .= sprintf('<option value=%s" %s>%s</option>', $folder[$key], $selected, $value);
  4128. }
  4129. $ret .= '</select>';
  4130. return $ret;
  4131. }
  4132. /**
  4133. * Displays a form (drop down menu) so the user can select his/her preferred language.
  4134. * The form works with or without javascript.
  4135. *
  4136. * @param bool Hide form if only one language available (defaults to false = show the box anyway)
  4137. * @param bool $showAsButton
  4138. *
  4139. * @return string|null Display the box directly
  4140. */
  4141. function api_display_language_form($hide_if_no_choice = false, $showAsButton = false)
  4142. {
  4143. // Retrieve a complete list of all the languages.
  4144. $language_list = api_get_languages();
  4145. if (count($language_list['name']) <= 1 && $hide_if_no_choice) {
  4146. return; //don't show any form
  4147. }
  4148. // The the current language of the user so that his/her language occurs as selected in the dropdown menu.
  4149. if (isset($_SESSION['user_language_choice'])) {
  4150. $user_selected_language = $_SESSION['user_language_choice'];
  4151. }
  4152. if (empty($user_selected_language)) {
  4153. $user_selected_language = api_get_setting('platformLanguage');
  4154. }
  4155. $currentLanguageId = api_get_language_id($user_selected_language);
  4156. $currentLanguageInfo = api_get_language_info($currentLanguageId);
  4157. $countryCode = languageToCountryIsoCode($currentLanguageInfo['isocode']);
  4158. $url = api_get_self();
  4159. if ($showAsButton) {
  4160. $html = '<div class="btn-group">
  4161. <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
  4162. <span class="flag-icon flag-icon-'.$countryCode.'"></span>
  4163. '.$currentLanguageInfo['original_name'].'
  4164. <span class="caret">
  4165. </span>
  4166. </button>';
  4167. } else {
  4168. $html = '
  4169. <a href="'.$url.'" class="dropdown-toggle" data-toggle="dropdown" role="button">
  4170. <span class="flag-icon flag-icon-'.$countryCode.'"></span>
  4171. '.$currentLanguageInfo['original_name'].'
  4172. <span class="caret"></span>
  4173. </a>
  4174. ';
  4175. }
  4176. $html .= '<ul class="dropdown-menu" role="menu">';
  4177. foreach ($language_list['all'] as $key => $data) {
  4178. $urlLink = $url.'?language='.$data['english_name'];
  4179. $html .= '<li><a href="'.$urlLink.'"><span class="flag-icon flag-icon-'.languageToCountryIsoCode($data['isocode']).'"></span> '.$data['original_name'].'</a></li>';
  4180. }
  4181. $html .= '</ul>';
  4182. if ($showAsButton) {
  4183. $html .= '</div>';
  4184. }
  4185. return $html;
  4186. }
  4187. /**
  4188. * @param string $languageIsoCode
  4189. *
  4190. * @return string
  4191. */
  4192. function languageToCountryIsoCode($languageIsoCode)
  4193. {
  4194. // @todo save in DB
  4195. switch ($languageIsoCode) {
  4196. case 'ko':
  4197. $country = 'kr';
  4198. break;
  4199. case 'ja':
  4200. $country = 'jp';
  4201. break;
  4202. case 'ca':
  4203. $country = 'es';
  4204. break;
  4205. case 'gl':
  4206. $country = 'es';
  4207. break;
  4208. case 'ka':
  4209. $country = 'ge';
  4210. break;
  4211. case 'sl':
  4212. $country = 'si';
  4213. break;
  4214. case 'eu':
  4215. $country = 'es';
  4216. break;
  4217. case 'cs':
  4218. $country = 'cz';
  4219. break;
  4220. case 'el':
  4221. $country = 'ae';
  4222. break;
  4223. case 'ar':
  4224. $country = 'ae';
  4225. break;
  4226. case 'en':
  4227. $country = 'gb';
  4228. break;
  4229. case 'he':
  4230. $country = 'il';
  4231. break;
  4232. case 'uk':
  4233. $country = 'ua'; //Ukraine
  4234. break;
  4235. case 'da':
  4236. $country = 'dk';
  4237. break;
  4238. case 'pt-BR':
  4239. $country = 'br';
  4240. break;
  4241. case 'qu':
  4242. $country = 'pe';
  4243. break;
  4244. case 'sv':
  4245. $country = 'se';
  4246. break;
  4247. case 'zh-TW':
  4248. case 'zh':
  4249. $country = 'cn';
  4250. break;
  4251. default:
  4252. $country = $languageIsoCode;
  4253. break;
  4254. }
  4255. $country = strtolower($country);
  4256. return $country;
  4257. }
  4258. /**
  4259. * Returns a list of all the languages that are made available by the admin.
  4260. *
  4261. * @return array An array with all languages. Structure of the array is
  4262. * array['name'] = An array with the name of every language
  4263. * array['folder'] = An array with the corresponding names of the language-folders in the filesystem
  4264. */
  4265. function api_get_languages()
  4266. {
  4267. $tbl_language = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  4268. $sql = "SELECT * FROM $tbl_language WHERE available='1'
  4269. ORDER BY original_name ASC";
  4270. $result = Database::query($sql);
  4271. $language_list = [];
  4272. while ($row = Database::fetch_array($result)) {
  4273. $language_list['name'][] = $row['original_name'];
  4274. $language_list['folder'][] = $row['dokeos_folder'];
  4275. $language_list['all'][] = $row;
  4276. }
  4277. return $language_list;
  4278. }
  4279. /**
  4280. * Returns a list of all the languages that are made available by the admin.
  4281. *
  4282. * @return array
  4283. */
  4284. function api_get_languages_to_array()
  4285. {
  4286. $tbl_language = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  4287. $sql = "SELECT * FROM $tbl_language WHERE available='1' ORDER BY original_name ASC";
  4288. $result = Database::query($sql);
  4289. $languages = [];
  4290. while ($row = Database::fetch_array($result)) {
  4291. $languages[$row['dokeos_folder']] = $row['original_name'];
  4292. }
  4293. return $languages;
  4294. }
  4295. /**
  4296. * Returns the id (the database id) of a language.
  4297. *
  4298. * @param string language name (the corresponding name of the language-folder in the filesystem)
  4299. *
  4300. * @return int id of the language
  4301. */
  4302. function api_get_language_id($language)
  4303. {
  4304. $tbl_language = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  4305. if (empty($language)) {
  4306. return null;
  4307. }
  4308. $language = Database::escape_string($language);
  4309. $sql = "SELECT id FROM $tbl_language
  4310. WHERE dokeos_folder = '$language' LIMIT 1";
  4311. $result = Database::query($sql);
  4312. $row = Database::fetch_array($result);
  4313. return $row['id'];
  4314. }
  4315. /**
  4316. * Gets language of the requested type for the current user. Types are :
  4317. * user_profil_lang : profile language of current user
  4318. * user_select_lang : language selected by user at login
  4319. * course_lang : language of the current course
  4320. * platform_lang : default platform language.
  4321. *
  4322. * @param string $lang_type
  4323. *
  4324. * @return string
  4325. */
  4326. function api_get_language_from_type($lang_type)
  4327. {
  4328. $return = false;
  4329. switch ($lang_type) {
  4330. case 'platform_lang':
  4331. $temp_lang = api_get_setting('platformLanguage');
  4332. if (!empty($temp_lang)) {
  4333. $return = $temp_lang;
  4334. }
  4335. break;
  4336. case 'user_profil_lang':
  4337. $_user = api_get_user_info();
  4338. if (isset($_user['language']) && !empty($_user['language'])) {
  4339. $return = $_user['language'];
  4340. }
  4341. break;
  4342. case 'user_selected_lang':
  4343. if (isset($_SESSION['user_language_choice']) && !empty($_SESSION['user_language_choice'])) {
  4344. $return = $_SESSION['user_language_choice'];
  4345. }
  4346. break;
  4347. case 'course_lang':
  4348. global $_course;
  4349. $cidReq = null;
  4350. if (empty($_course)) {
  4351. // Code modified because the local.inc.php file it's declarated after this work
  4352. // causing the function api_get_course_info() returns a null value
  4353. $cidReq = isset($_GET["cidReq"]) ? Database::escape_string($_GET["cidReq"]) : null;
  4354. $cDir = (!empty($_GET['cDir']) ? $_GET['cDir'] : null);
  4355. if (empty($cidReq) && !empty($cDir)) {
  4356. $c = CourseManager::getCourseCodeFromDirectory($cDir);
  4357. if ($c) {
  4358. $cidReq = $c;
  4359. }
  4360. }
  4361. }
  4362. $_course = api_get_course_info($cidReq);
  4363. if (isset($_course['language']) && !empty($_course['language'])) {
  4364. $return = $_course['language'];
  4365. $showCourseInUserLanguage = api_get_course_setting('show_course_in_user_language');
  4366. if ($showCourseInUserLanguage == 1) {
  4367. $userInfo = api_get_user_info();
  4368. if (isset($userInfo['language'])) {
  4369. $return = $userInfo['language'];
  4370. }
  4371. }
  4372. }
  4373. break;
  4374. default:
  4375. $return = false;
  4376. break;
  4377. }
  4378. return $return;
  4379. }
  4380. /**
  4381. * Get the language information by its id.
  4382. *
  4383. * @param int $languageId
  4384. *
  4385. * @throws Exception
  4386. *
  4387. * @return array
  4388. */
  4389. function api_get_language_info($languageId)
  4390. {
  4391. if (empty($languageId)) {
  4392. return [];
  4393. }
  4394. $language = Database::getManager()
  4395. ->find('ChamiloCoreBundle:Language', $languageId);
  4396. if (!$language) {
  4397. return [];
  4398. }
  4399. return [
  4400. 'id' => $language->getId(),
  4401. 'original_name' => $language->getOriginalName(),
  4402. 'english_name' => $language->getEnglishName(),
  4403. 'isocode' => $language->getIsocode(),
  4404. 'dokeos_folder' => $language->getDokeosFolder(),
  4405. 'available' => $language->getAvailable(),
  4406. 'parent_id' => $language->getParent() ? $language->getParent()->getId() : null,
  4407. ];
  4408. }
  4409. /**
  4410. * Returns the name of the visual (CSS) theme to be applied on the current page.
  4411. * The returned name depends on the platform, course or user -wide settings.
  4412. *
  4413. * @return string The visual theme's name, it is the name of a folder inside web/css/themes
  4414. */
  4415. function api_get_visual_theme()
  4416. {
  4417. static $visual_theme;
  4418. if (!isset($visual_theme)) {
  4419. // Get style directly from DB
  4420. $styleFromDatabase = api_get_settings_params_simple(
  4421. [
  4422. 'variable = ? AND access_url = ?' => [
  4423. 'stylesheets',
  4424. api_get_current_access_url_id(),
  4425. ],
  4426. ]
  4427. );
  4428. if ($styleFromDatabase) {
  4429. $platform_theme = $styleFromDatabase['selected_value'];
  4430. } else {
  4431. $platform_theme = api_get_setting('stylesheets');
  4432. }
  4433. // Platform's theme.
  4434. $visual_theme = $platform_theme;
  4435. if (api_get_setting('user_selected_theme') == 'true') {
  4436. $user_info = api_get_user_info();
  4437. if (isset($user_info['theme'])) {
  4438. $user_theme = $user_info['theme'];
  4439. if (!empty($user_theme)) {
  4440. $visual_theme = $user_theme;
  4441. // User's theme.
  4442. }
  4443. }
  4444. }
  4445. $course_id = api_get_course_id();
  4446. if (!empty($course_id)) {
  4447. if (api_get_setting('allow_course_theme') == 'true') {
  4448. $course_theme = api_get_course_setting('course_theme', $course_id);
  4449. if (!empty($course_theme) && $course_theme != -1) {
  4450. if (!empty($course_theme)) {
  4451. // Course's theme.
  4452. $visual_theme = $course_theme;
  4453. }
  4454. }
  4455. $allow_lp_theme = api_get_course_setting('allow_learning_path_theme');
  4456. if ($allow_lp_theme == 1) {
  4457. global $lp_theme_css, $lp_theme_config;
  4458. // These variables come from the file lp_controller.php.
  4459. if (!$lp_theme_config) {
  4460. if (!empty($lp_theme_css)) {
  4461. // LP's theme.
  4462. $visual_theme = $lp_theme_css;
  4463. }
  4464. }
  4465. }
  4466. }
  4467. }
  4468. if (empty($visual_theme)) {
  4469. $visual_theme = 'chamilo';
  4470. }
  4471. global $lp_theme_log;
  4472. if ($lp_theme_log) {
  4473. $visual_theme = $platform_theme;
  4474. }
  4475. }
  4476. return $visual_theme;
  4477. }
  4478. /**
  4479. * Returns a list of CSS themes currently available in the CSS folder
  4480. * The folder must have a default.css file.
  4481. *
  4482. * @param bool $getOnlyThemeFromVirtualInstance Used by the vchamilo plugin
  4483. *
  4484. * @return array list of themes directories from the css folder
  4485. * Note: Directory names (names of themes) in the file system should contain ASCII-characters only
  4486. */
  4487. function api_get_themes($getOnlyThemeFromVirtualInstance = false)
  4488. {
  4489. // This configuration value is set by the vchamilo plugin
  4490. $virtualTheme = api_get_configuration_value('virtual_css_theme_folder');
  4491. $readCssFolder = function ($dir) use ($virtualTheme) {
  4492. $finder = new Finder();
  4493. $themes = $finder->directories()->in($dir)->depth(0)->sortByName();
  4494. $list = [];
  4495. /** @var Symfony\Component\Finder\SplFileInfo $theme */
  4496. foreach ($themes as $theme) {
  4497. $folder = $theme->getFilename();
  4498. // A theme folder is consider if there's a default.css file
  4499. if (!file_exists($theme->getPathname().'/default.css')) {
  4500. continue;
  4501. }
  4502. $name = ucwords(str_replace('_', ' ', $folder));
  4503. if ($folder == $virtualTheme) {
  4504. continue;
  4505. }
  4506. $list[$folder] = $name;
  4507. }
  4508. return $list;
  4509. };
  4510. $dir = api_get_path(SYS_CSS_PATH).'themes/';
  4511. $list = $readCssFolder($dir);
  4512. if (!empty($virtualTheme)) {
  4513. $newList = $readCssFolder($dir.'/'.$virtualTheme);
  4514. if ($getOnlyThemeFromVirtualInstance) {
  4515. return $newList;
  4516. }
  4517. $list = $list + $newList;
  4518. asort($list);
  4519. }
  4520. return $list;
  4521. }
  4522. /**
  4523. * Find the largest sort value in a given user_course_category
  4524. * This function is used when we are moving a course to a different category
  4525. * and also when a user subscribes to courses (the new course is added at the end of the main category.
  4526. *
  4527. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  4528. *
  4529. * @param int $user_course_category the id of the user_course_category
  4530. * @param int $user_id
  4531. *
  4532. * @return int the value of the highest sort of the user_course_category
  4533. */
  4534. function api_max_sort_value($user_course_category, $user_id)
  4535. {
  4536. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  4537. $sql = "SELECT max(sort) as max_sort FROM $tbl_course_user
  4538. WHERE
  4539. user_id='".intval($user_id)."' AND
  4540. relation_type<>".COURSE_RELATION_TYPE_RRHH." AND
  4541. user_course_cat='".intval($user_course_category)."'";
  4542. $result_max = Database::query($sql);
  4543. if (Database::num_rows($result_max) == 1) {
  4544. $row_max = Database::fetch_array($result_max);
  4545. return $row_max['max_sort'];
  4546. }
  4547. return 0;
  4548. }
  4549. /**
  4550. * Transforms a number of seconds in hh:mm:ss format.
  4551. *
  4552. * @author Julian Prud'homme
  4553. *
  4554. * @param int the number of seconds
  4555. *
  4556. * @return string the formated time
  4557. */
  4558. function api_time_to_hms($seconds, $space = ':')
  4559. {
  4560. // $seconds = -1 means that we have wrong data in the db.
  4561. if ($seconds == -1) {
  4562. return
  4563. get_lang('Unknown').
  4564. Display::return_icon(
  4565. 'info2.gif',
  4566. get_lang('WrongDatasForTimeSpentOnThePlatform'),
  4567. ['align' => 'absmiddle', 'hspace' => '3px']
  4568. );
  4569. }
  4570. // How many hours ?
  4571. $hours = floor($seconds / 3600);
  4572. // How many minutes ?
  4573. $min = floor(($seconds - ($hours * 3600)) / 60);
  4574. // How many seconds
  4575. $sec = floor($seconds - ($hours * 3600) - ($min * 60));
  4576. if ($hours < 10) {
  4577. $hours = "0$hours";
  4578. }
  4579. if ($sec < 10) {
  4580. $sec = "0$sec";
  4581. }
  4582. if ($min < 10) {
  4583. $min = "0$min";
  4584. }
  4585. return $hours.$space.$min.$space.$sec;
  4586. }
  4587. /* FILE SYSTEM RELATED FUNCTIONS */
  4588. /**
  4589. * Returns the permissions to be assigned to every newly created directory by the web-server.
  4590. * The return value is based on the platform administrator's setting
  4591. * "Administration > Configuration settings > Security > Permissions for new directories".
  4592. *
  4593. * @return int returns the permissions in the format "Owner-Group-Others, Read-Write-Execute", as an integer value
  4594. */
  4595. function api_get_permissions_for_new_directories()
  4596. {
  4597. static $permissions;
  4598. if (!isset($permissions)) {
  4599. $permissions = trim(api_get_setting('permissions_for_new_directories'));
  4600. // The default value 0777 is according to that in the platform administration panel after fresh system installation.
  4601. $permissions = octdec(!empty($permissions) ? $permissions : '0777');
  4602. }
  4603. return $permissions;
  4604. }
  4605. /**
  4606. * Returns the permissions to be assigned to every newly created directory by the web-server.
  4607. * The return value is based on the platform administrator's setting
  4608. * "Administration > Configuration settings > Security > Permissions for new files".
  4609. *
  4610. * @return int returns the permissions in the format
  4611. * "Owner-Group-Others, Read-Write-Execute", as an integer value
  4612. */
  4613. function api_get_permissions_for_new_files()
  4614. {
  4615. static $permissions;
  4616. if (!isset($permissions)) {
  4617. $permissions = trim(api_get_setting('permissions_for_new_files'));
  4618. // The default value 0666 is according to that in the platform
  4619. // administration panel after fresh system installation.
  4620. $permissions = octdec(!empty($permissions) ? $permissions : '0666');
  4621. }
  4622. return $permissions;
  4623. }
  4624. /**
  4625. * Deletes a file, or a folder and its contents.
  4626. *
  4627. * @author Aidan Lister <aidan@php.net>
  4628. *
  4629. * @version 1.0.3
  4630. *
  4631. * @param string $dirname Directory to delete
  4632. * @param bool Deletes only the content or not
  4633. * @param bool $strict if one folder/file fails stop the loop
  4634. *
  4635. * @return bool Returns TRUE on success, FALSE on failure
  4636. *
  4637. * @see http://aidanlister.com/2004/04/recursively-deleting-a-folder-in-php/
  4638. *
  4639. * @author Yannick Warnier, adaptation for the Chamilo LMS, April, 2008
  4640. * @author Ivan Tcholakov, a sanity check about Directory class creation has been added, September, 2009
  4641. */
  4642. function rmdirr($dirname, $delete_only_content_in_folder = false, $strict = false)
  4643. {
  4644. $res = true;
  4645. // A sanity check.
  4646. if (!file_exists($dirname)) {
  4647. return false;
  4648. }
  4649. $php_errormsg = '';
  4650. // Simple delete for a file.
  4651. if (is_file($dirname) || is_link($dirname)) {
  4652. $res = unlink($dirname);
  4653. if ($res === false) {
  4654. error_log(__FILE__.' line '.__LINE__.': '.((bool) ini_get('track_errors') ? $php_errormsg : 'Error not recorded because track_errors is off in your php.ini'), 0);
  4655. }
  4656. return $res;
  4657. }
  4658. // Loop through the folder.
  4659. $dir = dir($dirname);
  4660. // A sanity check.
  4661. $is_object_dir = is_object($dir);
  4662. if ($is_object_dir) {
  4663. while (false !== $entry = $dir->read()) {
  4664. // Skip pointers.
  4665. if ($entry == '.' || $entry == '..') {
  4666. continue;
  4667. }
  4668. // Recurse.
  4669. if ($strict) {
  4670. $result = rmdirr("$dirname/$entry");
  4671. if ($result == false) {
  4672. $res = false;
  4673. break;
  4674. }
  4675. } else {
  4676. rmdirr("$dirname/$entry");
  4677. }
  4678. }
  4679. }
  4680. // Clean up.
  4681. if ($is_object_dir) {
  4682. $dir->close();
  4683. }
  4684. if ($delete_only_content_in_folder == false) {
  4685. $res = rmdir($dirname);
  4686. if ($res === false) {
  4687. error_log(__FILE__.' line '.__LINE__.': '.((bool) ini_get('track_errors') ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0);
  4688. }
  4689. }
  4690. return $res;
  4691. }
  4692. // TODO: This function is to be simplified. File access modes to be implemented.
  4693. /**
  4694. * function adapted from a php.net comment
  4695. * copy recursively a folder.
  4696. *
  4697. * @param the source folder
  4698. * @param the dest folder
  4699. * @param an array of excluded file_name (without extension)
  4700. * @param copied_files the returned array of copied files
  4701. * @param string $source
  4702. * @param string $dest
  4703. */
  4704. function copyr($source, $dest, $exclude = [], $copied_files = [])
  4705. {
  4706. if (empty($dest)) {
  4707. return false;
  4708. }
  4709. // Simple copy for a file
  4710. if (is_file($source)) {
  4711. $path_info = pathinfo($source);
  4712. if (!in_array($path_info['filename'], $exclude)) {
  4713. copy($source, $dest);
  4714. }
  4715. return true;
  4716. } elseif (!is_dir($source)) {
  4717. //then source is not a dir nor a file, return
  4718. return false;
  4719. }
  4720. // Make destination directory.
  4721. if (!is_dir($dest)) {
  4722. mkdir($dest, api_get_permissions_for_new_directories());
  4723. }
  4724. // Loop through the folder.
  4725. $dir = dir($source);
  4726. while (false !== $entry = $dir->read()) {
  4727. // Skip pointers
  4728. if ($entry == '.' || $entry == '..') {
  4729. continue;
  4730. }
  4731. // Deep copy directories.
  4732. if ($dest !== "$source/$entry") {
  4733. $files = copyr("$source/$entry", "$dest/$entry", $exclude, $copied_files);
  4734. }
  4735. }
  4736. // Clean up.
  4737. $dir->close();
  4738. return true;
  4739. }
  4740. /**
  4741. * @todo: Using DIRECTORY_SEPARATOR is not recommended, this is an obsolete approach.
  4742. * Documentation header to be added here.
  4743. *
  4744. * @param string $pathname
  4745. * @param string $base_path_document
  4746. * @param int $session_id
  4747. *
  4748. * @return mixed True if directory already exists, false if a file already exists at
  4749. * the destination and null if everything goes according to plan
  4750. */
  4751. function copy_folder_course_session(
  4752. $pathname,
  4753. $base_path_document,
  4754. $session_id,
  4755. $course_info,
  4756. $document,
  4757. $source_course_id
  4758. ) {
  4759. $table = Database::get_course_table(TABLE_DOCUMENT);
  4760. $session_id = intval($session_id);
  4761. $source_course_id = intval($source_course_id);
  4762. // Check whether directory already exists.
  4763. if (is_dir($pathname) || empty($pathname)) {
  4764. return true;
  4765. }
  4766. // Ensure that a file with the same name does not already exist.
  4767. if (is_file($pathname)) {
  4768. trigger_error('copy_folder_course_session(): File exists', E_USER_WARNING);
  4769. return false;
  4770. }
  4771. $course_id = $course_info['real_id'];
  4772. $folders = explode(DIRECTORY_SEPARATOR, str_replace($base_path_document.DIRECTORY_SEPARATOR, '', $pathname));
  4773. $new_pathname = $base_path_document;
  4774. $path = '';
  4775. foreach ($folders as $folder) {
  4776. $new_pathname .= DIRECTORY_SEPARATOR.$folder;
  4777. $path .= DIRECTORY_SEPARATOR.$folder;
  4778. if (!file_exists($new_pathname)) {
  4779. $path = Database::escape_string($path);
  4780. $sql = "SELECT * FROM $table
  4781. WHERE
  4782. c_id = $source_course_id AND
  4783. path = '$path' AND
  4784. filetype = 'folder' AND
  4785. session_id = '$session_id'";
  4786. $rs1 = Database::query($sql);
  4787. $num_rows = Database::num_rows($rs1);
  4788. if ($num_rows == 0) {
  4789. mkdir($new_pathname, api_get_permissions_for_new_directories());
  4790. // Insert new folder with destination session_id.
  4791. $params = [
  4792. 'c_id' => $course_id,
  4793. 'path' => $path,
  4794. 'comment' => $document->comment,
  4795. 'title' => basename($new_pathname),
  4796. 'filetype' => 'folder',
  4797. 'size' => '0',
  4798. 'session_id' => $session_id,
  4799. ];
  4800. $document_id = Database::insert($table, $params);
  4801. if ($document_id) {
  4802. $sql = "UPDATE $table SET id = iid WHERE iid = $document_id";
  4803. Database::query($sql);
  4804. api_item_property_update(
  4805. $course_info,
  4806. TOOL_DOCUMENT,
  4807. $document_id,
  4808. 'FolderCreated',
  4809. api_get_user_id(),
  4810. 0,
  4811. 0,
  4812. null,
  4813. null,
  4814. $session_id
  4815. );
  4816. }
  4817. }
  4818. }
  4819. } // en foreach
  4820. }
  4821. // TODO: chmodr() is a better name. Some corrections are needed. Documentation header to be added here.
  4822. /**
  4823. * @param string $path
  4824. */
  4825. function api_chmod_R($path, $filemode)
  4826. {
  4827. if (!is_dir($path)) {
  4828. return chmod($path, $filemode);
  4829. }
  4830. $handler = opendir($path);
  4831. while ($file = readdir($handler)) {
  4832. if ($file != '.' && $file != '..') {
  4833. $fullpath = "$path/$file";
  4834. if (!is_dir($fullpath)) {
  4835. if (!chmod($fullpath, $filemode)) {
  4836. return false;
  4837. }
  4838. } else {
  4839. if (!api_chmod_R($fullpath, $filemode)) {
  4840. return false;
  4841. }
  4842. }
  4843. }
  4844. }
  4845. closedir($handler);
  4846. return chmod($path, $filemode);
  4847. }
  4848. // TODO: Where the following function has been copy/pased from? There is no information about author and license. Style, coding conventions...
  4849. /**
  4850. * Parse info file format. (e.g: file.info).
  4851. *
  4852. * Files should use an ini-like format to specify values.
  4853. * White-space generally doesn't matter, except inside values.
  4854. * e.g.
  4855. *
  4856. * @verbatim
  4857. * key = value
  4858. * key = "value"
  4859. * key = 'value'
  4860. * key = "multi-line
  4861. *
  4862. * value"
  4863. * key = 'multi-line
  4864. *
  4865. * value'
  4866. * key
  4867. * =
  4868. * 'value'
  4869. * @endverbatim
  4870. *
  4871. * Arrays are created using a GET-like syntax:
  4872. *
  4873. * @verbatim
  4874. * key[] = "numeric array"
  4875. * key[index] = "associative array"
  4876. * key[index][] = "nested numeric array"
  4877. * key[index][index] = "nested associative array"
  4878. * @endverbatim
  4879. *
  4880. * PHP constants are substituted in, but only when used as the entire value:
  4881. *
  4882. * Comments should start with a semi-colon at the beginning of a line.
  4883. *
  4884. * This function is NOT for placing arbitrary module-specific settings. Use
  4885. * variable_get() and variable_set() for that.
  4886. *
  4887. * Information stored in the module.info file:
  4888. * - name: The real name of the module for display purposes.
  4889. * - description: A brief description of the module.
  4890. * - dependencies: An array of shortnames of other modules this module depends on.
  4891. * - package: The name of the package of modules this module belongs to.
  4892. *
  4893. * Example of .info file:
  4894. * <code>
  4895. * @verbatim
  4896. * name = Forum
  4897. * description = Enables threaded discussions about general topics.
  4898. * dependencies[] = taxonomy
  4899. * dependencies[] = comment
  4900. * package = Core - optional
  4901. * version = VERSION
  4902. * @endverbatim
  4903. * </code>
  4904. *
  4905. * @param string $filename
  4906. * The file we are parsing. Accepts file with relative or absolute path.
  4907. *
  4908. * @return
  4909. * The info array
  4910. */
  4911. function api_parse_info_file($filename)
  4912. {
  4913. $info = [];
  4914. if (!file_exists($filename)) {
  4915. return $info;
  4916. }
  4917. $data = file_get_contents($filename);
  4918. if (preg_match_all('
  4919. @^\s* # Start at the beginning of a line, ignoring leading whitespace
  4920. ((?:
  4921. [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
  4922. \[[^\[\]]*\] # unless they are balanced and not nested
  4923. )+?)
  4924. \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
  4925. (?:
  4926. ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
  4927. (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
  4928. ([^\r\n]*?) # Non-quoted string
  4929. )\s*$ # Stop at the next end of a line, ignoring trailing whitespace
  4930. @msx', $data, $matches, PREG_SET_ORDER)) {
  4931. $key = $value1 = $value2 = $value3 = '';
  4932. foreach ($matches as $match) {
  4933. // Fetch the key and value string.
  4934. $i = 0;
  4935. foreach (['key', 'value1', 'value2', 'value3'] as $var) {
  4936. $$var = isset($match[++$i]) ? $match[$i] : '';
  4937. }
  4938. $value = stripslashes(substr($value1, 1, -1)).stripslashes(substr($value2, 1, -1)).$value3;
  4939. // Parse array syntax.
  4940. $keys = preg_split('/\]?\[/', rtrim($key, ']'));
  4941. $last = array_pop($keys);
  4942. $parent = &$info;
  4943. // Create nested arrays.
  4944. foreach ($keys as $key) {
  4945. if ($key == '') {
  4946. $key = count($parent);
  4947. }
  4948. if (!isset($parent[$key]) || !is_array($parent[$key])) {
  4949. $parent[$key] = [];
  4950. }
  4951. $parent = &$parent[$key];
  4952. }
  4953. // Handle PHP constants.
  4954. if (defined($value)) {
  4955. $value = constant($value);
  4956. }
  4957. // Insert actual value.
  4958. if ($last == '') {
  4959. $last = count($parent);
  4960. }
  4961. $parent[$last] = $value;
  4962. }
  4963. }
  4964. return $info;
  4965. }
  4966. /**
  4967. * Gets Chamilo version from the configuration files.
  4968. *
  4969. * @return string A string of type "1.8.4", or an empty string if the version could not be found
  4970. */
  4971. function api_get_version()
  4972. {
  4973. return (string) api_get_configuration_value('system_version');
  4974. }
  4975. /**
  4976. * Gets the software name (the name/brand of the Chamilo-based customized system).
  4977. *
  4978. * @return string
  4979. */
  4980. function api_get_software_name()
  4981. {
  4982. $name = api_get_configuration_value('software_name');
  4983. if (!empty($name)) {
  4984. return $name;
  4985. } else {
  4986. return 'Chamilo';
  4987. }
  4988. }
  4989. /**
  4990. * Checks whether status given in parameter exists in the platform.
  4991. *
  4992. * @param mixed the status (can be either int either string)
  4993. *
  4994. * @return bool if the status exists, else returns false
  4995. */
  4996. function api_status_exists($status_asked)
  4997. {
  4998. global $_status_list;
  4999. return in_array($status_asked, $_status_list) ? true : isset($_status_list[$status_asked]);
  5000. }
  5001. /**
  5002. * Checks whether status given in parameter exists in the platform. The function
  5003. * returns the status ID or false if it does not exist, but given the fact there
  5004. * is no "0" status, the return value can be checked against
  5005. * if(api_status_key()) to know if it exists.
  5006. *
  5007. * @param mixed The status (can be either int or string)
  5008. *
  5009. * @return mixed Status ID if exists, false otherwise
  5010. */
  5011. function api_status_key($status)
  5012. {
  5013. global $_status_list;
  5014. return isset($_status_list[$status]) ? $status : array_search($status, $_status_list);
  5015. }
  5016. /**
  5017. * Gets the status langvars list.
  5018. *
  5019. * @return string[] the list of status with their translations
  5020. */
  5021. function api_get_status_langvars()
  5022. {
  5023. return [
  5024. COURSEMANAGER => get_lang('Teacher', ''),
  5025. SESSIONADMIN => get_lang('SessionsAdmin', ''),
  5026. DRH => get_lang('Drh', ''),
  5027. STUDENT => get_lang('Student', ''),
  5028. ANONYMOUS => get_lang('Anonymous', ''),
  5029. STUDENT_BOSS => get_lang('RoleStudentBoss', ''),
  5030. INVITEE => get_lang('Invited'),
  5031. ];
  5032. }
  5033. /**
  5034. * The function that retrieves all the possible settings for a certain config setting.
  5035. *
  5036. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  5037. */
  5038. function api_get_settings_options($var)
  5039. {
  5040. $table_settings_options = Database::get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  5041. $var = Database::escape_string($var);
  5042. $sql = "SELECT * FROM $table_settings_options
  5043. WHERE variable = '$var'
  5044. ORDER BY id";
  5045. $result = Database::query($sql);
  5046. $settings_options_array = [];
  5047. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5048. $settings_options_array[] = $row;
  5049. }
  5050. return $settings_options_array;
  5051. }
  5052. /**
  5053. * @param array $params
  5054. */
  5055. function api_set_setting_option($params)
  5056. {
  5057. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  5058. if (empty($params['id'])) {
  5059. Database::insert($table, $params);
  5060. } else {
  5061. Database::update($table, $params, ['id = ? ' => $params['id']]);
  5062. }
  5063. }
  5064. /**
  5065. * @param array $params
  5066. */
  5067. function api_set_setting_simple($params)
  5068. {
  5069. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  5070. $url_id = api_get_current_access_url_id();
  5071. if (empty($params['id'])) {
  5072. $params['access_url'] = $url_id;
  5073. Database::insert($table, $params);
  5074. } else {
  5075. Database::update($table, $params, ['id = ? ' => [$params['id']]]);
  5076. }
  5077. }
  5078. /**
  5079. * @param int $id
  5080. */
  5081. function api_delete_setting_option($id)
  5082. {
  5083. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  5084. if (!empty($id)) {
  5085. Database::delete($table, ['id = ? ' => $id]);
  5086. }
  5087. }
  5088. /**
  5089. * Sets a platform configuration setting to a given value.
  5090. *
  5091. * @param string The variable we want to update
  5092. * @param string The value we want to record
  5093. * @param string The sub-variable if any (in most cases, this will remain null)
  5094. * @param string The category if any (in most cases, this will remain null)
  5095. * @param int The access_url for which this parameter is valid
  5096. * @param string $cat
  5097. *
  5098. * @return bool|null
  5099. */
  5100. function api_set_setting($var, $value, $subvar = null, $cat = null, $access_url = 1)
  5101. {
  5102. if (empty($var)) {
  5103. return false;
  5104. }
  5105. $t_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  5106. $var = Database::escape_string($var);
  5107. $value = Database::escape_string($value);
  5108. $access_url = (int) $access_url;
  5109. if (empty($access_url)) {
  5110. $access_url = 1;
  5111. }
  5112. $select = "SELECT id FROM $t_settings WHERE variable = '$var' ";
  5113. if (!empty($subvar)) {
  5114. $subvar = Database::escape_string($subvar);
  5115. $select .= " AND subkey = '$subvar'";
  5116. }
  5117. if (!empty($cat)) {
  5118. $cat = Database::escape_string($cat);
  5119. $select .= " AND category = '$cat'";
  5120. }
  5121. if ($access_url > 1) {
  5122. $select .= " AND access_url = $access_url";
  5123. } else {
  5124. $select .= " AND access_url = 1 ";
  5125. }
  5126. $res = Database::query($select);
  5127. if (Database::num_rows($res) > 0) {
  5128. // Found item for this access_url.
  5129. $row = Database::fetch_array($res);
  5130. $sql = "UPDATE $t_settings SET selected_value = '$value'
  5131. WHERE id = ".$row['id'];
  5132. Database::query($sql);
  5133. } else {
  5134. // Item not found for this access_url, we have to check if it exist with access_url = 1
  5135. $select = "SELECT * FROM $t_settings
  5136. WHERE variable = '$var' AND access_url = 1 ";
  5137. // Just in case
  5138. if ($access_url == 1) {
  5139. if (!empty($subvar)) {
  5140. $select .= " AND subkey = '$subvar'";
  5141. }
  5142. if (!empty($cat)) {
  5143. $select .= " AND category = '$cat'";
  5144. }
  5145. $res = Database::query($select);
  5146. if (Database::num_rows($res) > 0) {
  5147. // We have a setting for access_url 1, but none for the current one, so create one.
  5148. $row = Database::fetch_array($res);
  5149. $insert = "INSERT INTO $t_settings (variable, subkey, type,category, selected_value, title, comment, scope, subkeytext, access_url)
  5150. VALUES
  5151. ('".$row['variable']."',".(!empty($row['subkey']) ? "'".$row['subkey']."'" : "NULL").",".
  5152. "'".$row['type']."','".$row['category']."',".
  5153. "'$value','".$row['title']."',".
  5154. "".(!empty($row['comment']) ? "'".$row['comment']."'" : "NULL").",".(!empty($row['scope']) ? "'".$row['scope']."'" : "NULL").",".
  5155. "".(!empty($row['subkeytext']) ? "'".$row['subkeytext']."'" : "NULL").",$access_url)";
  5156. Database::query($insert);
  5157. } else {
  5158. // Such a setting does not exist.
  5159. //error_log(__FILE__.':'.__LINE__.': Attempting to update setting '.$var.' ('.$subvar.') which does not exist at all', 0);
  5160. }
  5161. } else {
  5162. // Other access url.
  5163. if (!empty($subvar)) {
  5164. $select .= " AND subkey = '$subvar'";
  5165. }
  5166. if (!empty($cat)) {
  5167. $select .= " AND category = '$cat'";
  5168. }
  5169. $res = Database::query($select);
  5170. if (Database::num_rows($res) > 0) {
  5171. // We have a setting for access_url 1, but none for the current one, so create one.
  5172. $row = Database::fetch_array($res);
  5173. if ($row['access_url_changeable'] == 1) {
  5174. $insert = "INSERT INTO $t_settings (variable,subkey, type,category, selected_value,title, comment,scope, subkeytext,access_url, access_url_changeable) VALUES
  5175. ('".$row['variable']."',".
  5176. (!empty($row['subkey']) ? "'".$row['subkey']."'" : "NULL").",".
  5177. "'".$row['type']."','".$row['category']."',".
  5178. "'$value','".$row['title']."',".
  5179. "".(!empty($row['comment']) ? "'".$row['comment']."'" : "NULL").",".
  5180. (!empty($row['scope']) ? "'".$row['scope']."'" : "NULL").",".
  5181. "".(!empty($row['subkeytext']) ? "'".$row['subkeytext']."'" : "NULL").",$access_url,".$row['access_url_changeable'].")";
  5182. Database::query($insert);
  5183. }
  5184. } else { // Such a setting does not exist.
  5185. //error_log(__FILE__.':'.__LINE__.': Attempting to update setting '.$var.' ('.$subvar.') which does not exist at all. The access_url is: '.$access_url.' ',0);
  5186. }
  5187. }
  5188. }
  5189. }
  5190. /**
  5191. * Sets a whole category of settings to one specific value.
  5192. *
  5193. * @param string Category
  5194. * @param string Value
  5195. * @param int Access URL. Optional. Defaults to 1
  5196. * @param array Optional array of filters on field type
  5197. * @param string $category
  5198. * @param string $value
  5199. *
  5200. * @return bool
  5201. */
  5202. function api_set_settings_category($category, $value = null, $access_url = 1, $fieldtype = [])
  5203. {
  5204. if (empty($category)) {
  5205. return false;
  5206. }
  5207. $category = Database::escape_string($category);
  5208. $t_s = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  5209. $access_url = (int) $access_url;
  5210. if (empty($access_url)) {
  5211. $access_url = 1;
  5212. }
  5213. if (isset($value)) {
  5214. $value = Database::escape_string($value);
  5215. $sql = "UPDATE $t_s SET selected_value = '$value'
  5216. WHERE category = '$category' AND access_url = $access_url";
  5217. if (is_array($fieldtype) && count($fieldtype) > 0) {
  5218. $sql .= " AND ( ";
  5219. $i = 0;
  5220. foreach ($fieldtype as $type) {
  5221. if ($i > 0) {
  5222. $sql .= ' OR ';
  5223. }
  5224. $type = Database::escape_string($type);
  5225. $sql .= " type='".$type."' ";
  5226. $i++;
  5227. }
  5228. $sql .= ")";
  5229. }
  5230. $res = Database::query($sql);
  5231. return $res !== false;
  5232. } else {
  5233. $sql = "UPDATE $t_s SET selected_value = NULL
  5234. WHERE category = '$category' AND access_url = $access_url";
  5235. if (is_array($fieldtype) && count($fieldtype) > 0) {
  5236. $sql .= " AND ( ";
  5237. $i = 0;
  5238. foreach ($fieldtype as $type) {
  5239. if ($i > 0) {
  5240. $sql .= ' OR ';
  5241. }
  5242. $type = Database::escape_string($type);
  5243. $sql .= " type='".$type."' ";
  5244. $i++;
  5245. }
  5246. $sql .= ")";
  5247. }
  5248. $res = Database::query($sql);
  5249. return $res !== false;
  5250. }
  5251. }
  5252. /**
  5253. * Gets all available access urls in an array (as in the database).
  5254. *
  5255. * @return array An array of database records
  5256. */
  5257. function api_get_access_urls($from = 0, $to = 1000000, $order = 'url', $direction = 'ASC')
  5258. {
  5259. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
  5260. $from = (int) $from;
  5261. $to = (int) $to;
  5262. $order = Database::escape_string($order, null, false);
  5263. $direction = Database::escape_string($direction, null, false);
  5264. $sql = "SELECT id, url, description, active, created_by, tms
  5265. FROM $table
  5266. ORDER BY $order $direction
  5267. LIMIT $to OFFSET $from";
  5268. $res = Database::query($sql);
  5269. return Database::store_result($res);
  5270. }
  5271. /**
  5272. * Gets the access url info in an array.
  5273. *
  5274. * @param int $id Id of the access url
  5275. * @param bool $returnDefault Set to false if you want the real URL if URL 1 is still 'http://localhost/'
  5276. *
  5277. * @return array All the info (url, description, active, created_by, tms)
  5278. * from the access_url table
  5279. *
  5280. * @author Julio Montoya
  5281. */
  5282. function api_get_access_url($id, $returnDefault = true)
  5283. {
  5284. static $staticResult;
  5285. $id = (int) $id;
  5286. if (isset($staticResult[$id])) {
  5287. $result = $staticResult[$id];
  5288. } else {
  5289. // Calling the Database:: library dont work this is handmade.
  5290. $table_access_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
  5291. $sql = "SELECT url, description, active, created_by, tms
  5292. FROM $table_access_url WHERE id = '$id' ";
  5293. $res = Database::query($sql);
  5294. $result = @Database::fetch_array($res);
  5295. $staticResult[$id] = $result;
  5296. }
  5297. // If the result url is 'http://localhost/' (the default) and the root_web
  5298. // (=current url) is different, and the $id is = 1 (which might mean
  5299. // api_get_current_access_url_id() returned 1 by default), then return the
  5300. // root_web setting instead of the current URL
  5301. // This is provided as an option to avoid breaking the storage of URL-specific
  5302. // homepages in home/localhost/
  5303. if ($id === 1 && $returnDefault === false) {
  5304. $currentUrl = api_get_current_access_url_id();
  5305. // only do this if we are on the main URL (=1), otherwise we could get
  5306. // information on another URL instead of the one asked as parameter
  5307. if ($currentUrl === 1) {
  5308. $rootWeb = api_get_path(WEB_PATH);
  5309. $default = 'http://localhost/';
  5310. if ($result['url'] === $default && $rootWeb != $default) {
  5311. $result['url'] = $rootWeb;
  5312. }
  5313. }
  5314. }
  5315. return $result;
  5316. }
  5317. /**
  5318. * Gets all the current settings for a specific access url.
  5319. *
  5320. * @param string The category, if any, that we want to get
  5321. * @param string Whether we want a simple list (display a category) or
  5322. * a grouped list (group by variable as in settings.php default). Values: 'list' or 'group'
  5323. * @param int Access URL's ID. Optional. Uses 1 by default, which is the unique URL
  5324. *
  5325. * @return array Array of database results for the current settings of the current access URL
  5326. */
  5327. function &api_get_settings($cat = null, $ordering = 'list', $access_url = 1, $url_changeable = 0)
  5328. {
  5329. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  5330. $access_url = (int) $access_url;
  5331. $where_condition = '';
  5332. if ($url_changeable == 1) {
  5333. $where_condition = " AND access_url_changeable= '1' ";
  5334. }
  5335. if (empty($access_url) || $access_url == -1) {
  5336. $access_url = 1;
  5337. }
  5338. $sql = "SELECT * FROM $table
  5339. WHERE access_url = $access_url $where_condition ";
  5340. if (!empty($cat)) {
  5341. $cat = Database::escape_string($cat);
  5342. $sql .= " AND category='$cat' ";
  5343. }
  5344. if ($ordering == 'group') {
  5345. $sql .= " ORDER BY id ASC";
  5346. } else {
  5347. $sql .= " ORDER BY 1,2 ASC";
  5348. }
  5349. $result = Database::query($sql);
  5350. if ($result === null) {
  5351. return [];
  5352. }
  5353. $result = Database::store_result($result, 'ASSOC');
  5354. return $result;
  5355. }
  5356. /**
  5357. * @param string $value The value we want to record
  5358. * @param string $variable The variable name we want to insert
  5359. * @param string $subKey The subkey for the variable we want to insert
  5360. * @param string $type The type for the variable we want to insert
  5361. * @param string $category The category for the variable we want to insert
  5362. * @param string $title The title
  5363. * @param string $comment The comment
  5364. * @param string $scope The scope
  5365. * @param string $subKeyText The subkey text
  5366. * @param int $accessUrlId The access_url for which this parameter is valid
  5367. * @param int $visibility The changeability of this setting for non-master urls
  5368. *
  5369. * @return int The setting ID
  5370. */
  5371. function api_add_setting(
  5372. $value,
  5373. $variable,
  5374. $subKey = '',
  5375. $type = 'textfield',
  5376. $category = '',
  5377. $title = '',
  5378. $comment = '',
  5379. $scope = '',
  5380. $subKeyText = '',
  5381. $accessUrlId = 1,
  5382. $visibility = 0
  5383. ) {
  5384. $em = Database::getManager();
  5385. $settingRepo = $em->getRepository('ChamiloCoreBundle:SettingsCurrent');
  5386. $accessUrlId = (int) $accessUrlId ?: 1;
  5387. if (is_array($value)) {
  5388. $value = serialize($value);
  5389. } else {
  5390. $value = trim($value);
  5391. }
  5392. $criteria = ['variable' => $variable, 'accessUrl' => $accessUrlId];
  5393. if (!empty($subKey)) {
  5394. $criteria['subkey'] = $subKey;
  5395. }
  5396. // Check if this variable doesn't exist already
  5397. /** @var SettingsCurrent $setting */
  5398. $setting = $settingRepo->findOneBy($criteria);
  5399. if ($setting) {
  5400. $setting->setSelectedValue($value);
  5401. $em->persist($setting);
  5402. $em->flush();
  5403. return $setting->getId();
  5404. }
  5405. // Item not found for this access_url, we have to check if the whole thing is missing
  5406. // (in which case we ignore the insert) or if there *is* a record but just for access_url = 1
  5407. $setting = new SettingsCurrent();
  5408. $setting
  5409. ->setVariable($variable)
  5410. ->setSelectedValue($value)
  5411. ->setType($type)
  5412. ->setCategory($category)
  5413. ->setSubkey($subKey)
  5414. ->setTitle($title)
  5415. ->setComment($comment)
  5416. ->setScope($scope)
  5417. ->setSubkeytext($subKeyText)
  5418. ->setAccessUrl($accessUrlId)
  5419. ->setAccessUrlChangeable($visibility);
  5420. $em->persist($setting);
  5421. $em->flush();
  5422. return $setting->getId();
  5423. }
  5424. /**
  5425. * Checks wether a user can or can't view the contents of a course.
  5426. *
  5427. * @deprecated use CourseManager::is_user_subscribed_in_course
  5428. *
  5429. * @param int $userid User id or NULL to get it from $_SESSION
  5430. * @param int $cid course id to check whether the user is allowed
  5431. *
  5432. * @return bool
  5433. */
  5434. function api_is_course_visible_for_user($userid = null, $cid = null)
  5435. {
  5436. if ($userid === null) {
  5437. $userid = api_get_user_id();
  5438. }
  5439. if (empty($userid) || strval(intval($userid)) != $userid) {
  5440. if (api_is_anonymous()) {
  5441. $userid = api_get_anonymous_id();
  5442. } else {
  5443. return false;
  5444. }
  5445. }
  5446. $cid = Database::escape_string($cid);
  5447. $courseInfo = api_get_course_info($cid);
  5448. $courseId = $courseInfo['real_id'];
  5449. $is_platformAdmin = api_is_platform_admin();
  5450. $course_table = Database::get_main_table(TABLE_MAIN_COURSE);
  5451. $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY);
  5452. $sql = "SELECT
  5453. $course_table.category_code,
  5454. $course_table.visibility,
  5455. $course_table.code,
  5456. $course_cat_table.code
  5457. FROM $course_table
  5458. LEFT JOIN $course_cat_table
  5459. ON $course_table.category_code = $course_cat_table.code
  5460. WHERE
  5461. $course_table.code = '$cid'
  5462. LIMIT 1";
  5463. $result = Database::query($sql);
  5464. if (Database::num_rows($result) > 0) {
  5465. $visibility = Database::fetch_array($result);
  5466. $visibility = $visibility['visibility'];
  5467. } else {
  5468. $visibility = 0;
  5469. }
  5470. // Shortcut permissions in case the visibility is "open to the world".
  5471. if ($visibility === COURSE_VISIBILITY_OPEN_WORLD) {
  5472. return true;
  5473. }
  5474. $tbl_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  5475. $sql = "SELECT
  5476. is_tutor, status
  5477. FROM $tbl_course_user
  5478. WHERE
  5479. user_id = '$userid' AND
  5480. relation_type <> '".COURSE_RELATION_TYPE_RRHH."' AND
  5481. c_id = $courseId
  5482. LIMIT 1";
  5483. $result = Database::query($sql);
  5484. if (Database::num_rows($result) > 0) {
  5485. // This user has got a recorded state for this course.
  5486. $cuData = Database::fetch_array($result);
  5487. $is_courseMember = true;
  5488. $is_courseAdmin = ($cuData['status'] == 1);
  5489. }
  5490. if (!$is_courseAdmin) {
  5491. // This user has no status related to this course.
  5492. // Is it the session coach or the session admin?
  5493. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  5494. $tbl_session_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  5495. $tbl_session_course_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  5496. $sql = "SELECT
  5497. session.id_coach, session_admin_id, session.id
  5498. FROM
  5499. $tbl_session as session
  5500. INNER JOIN $tbl_session_course
  5501. ON session_rel_course.session_id = session.id
  5502. AND session_rel_course.c_id = '$courseId'
  5503. LIMIT 1";
  5504. $result = Database::query($sql);
  5505. $row = Database::store_result($result);
  5506. if ($row[0]['id_coach'] == $userid) {
  5507. $is_courseMember = true;
  5508. $is_courseAdmin = false;
  5509. } elseif ($row[0]['session_admin_id'] == $userid) {
  5510. $is_courseMember = false;
  5511. $is_courseAdmin = false;
  5512. } else {
  5513. // Check if the current user is the course coach.
  5514. $sql = "SELECT 1
  5515. FROM $tbl_session_course
  5516. WHERE session_rel_course.c_id = '$courseId'
  5517. AND session_rel_course.id_coach = '$userid'
  5518. LIMIT 1";
  5519. $result = Database::query($sql);
  5520. //if ($row = Database::fetch_array($result)) {
  5521. if (Database::num_rows($result) > 0) {
  5522. $is_courseMember = true;
  5523. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  5524. $sql = "SELECT status FROM $tbl_user
  5525. WHERE user_id = $userid
  5526. LIMIT 1";
  5527. $result = Database::query($sql);
  5528. if (Database::result($result, 0, 0) == 1) {
  5529. $is_courseAdmin = true;
  5530. } else {
  5531. $is_courseAdmin = false;
  5532. }
  5533. } else {
  5534. // Check if the user is a student is this session.
  5535. $sql = "SELECT id
  5536. FROM $tbl_session_course_user
  5537. WHERE
  5538. user_id = '$userid' AND
  5539. c_id = '$courseId'
  5540. LIMIT 1";
  5541. if (Database::num_rows($result) > 0) {
  5542. // This user haa got a recorded state for this course.
  5543. while ($row = Database::fetch_array($result)) {
  5544. $is_courseMember = true;
  5545. $is_courseAdmin = false;
  5546. }
  5547. }
  5548. }
  5549. }
  5550. }
  5551. switch ($visibility) {
  5552. case COURSE_VISIBILITY_OPEN_WORLD:
  5553. return true;
  5554. case COURSE_VISIBILITY_OPEN_PLATFORM:
  5555. return isset($userid);
  5556. case COURSE_VISIBILITY_REGISTERED:
  5557. case COURSE_VISIBILITY_CLOSED:
  5558. return $is_platformAdmin || $is_courseMember || $is_courseAdmin;
  5559. case COURSE_VISIBILITY_HIDDEN:
  5560. return $is_platformAdmin;
  5561. }
  5562. return false;
  5563. }
  5564. /**
  5565. * Returns whether an element (forum, message, survey ...) belongs to a session or not.
  5566. *
  5567. * @param string the tool of the element
  5568. * @param int the element id in database
  5569. * @param int the session_id to compare with element session id
  5570. *
  5571. * @return bool true if the element is in the session, false else
  5572. */
  5573. function api_is_element_in_the_session($tool, $element_id, $session_id = null)
  5574. {
  5575. if (is_null($session_id)) {
  5576. $session_id = api_get_session_id();
  5577. }
  5578. $element_id = (int) $element_id;
  5579. if (empty($element_id)) {
  5580. return false;
  5581. }
  5582. // Get information to build query depending of the tool.
  5583. switch ($tool) {
  5584. case TOOL_SURVEY:
  5585. $table_tool = Database::get_course_table(TABLE_SURVEY);
  5586. $key_field = 'survey_id';
  5587. break;
  5588. case TOOL_ANNOUNCEMENT:
  5589. $table_tool = Database::get_course_table(TABLE_ANNOUNCEMENT);
  5590. $key_field = 'id';
  5591. break;
  5592. case TOOL_AGENDA:
  5593. $table_tool = Database::get_course_table(TABLE_AGENDA);
  5594. $key_field = 'id';
  5595. break;
  5596. case TOOL_GROUP:
  5597. $table_tool = Database::get_course_table(TABLE_GROUP);
  5598. $key_field = 'id';
  5599. break;
  5600. default:
  5601. return false;
  5602. }
  5603. $course_id = api_get_course_int_id();
  5604. $sql = "SELECT session_id FROM $table_tool
  5605. WHERE c_id = $course_id AND $key_field = ".$element_id;
  5606. $rs = Database::query($sql);
  5607. if ($element_session_id = Database::result($rs, 0, 0)) {
  5608. if ($element_session_id == intval($session_id)) {
  5609. // The element belongs to the session.
  5610. return true;
  5611. }
  5612. }
  5613. return false;
  5614. }
  5615. /**
  5616. * Replaces "forbidden" characters in a filename string.
  5617. *
  5618. * @param string $filename
  5619. * @param bool $treat_spaces_as_hyphens
  5620. *
  5621. * @return string
  5622. */
  5623. function api_replace_dangerous_char($filename, $treat_spaces_as_hyphens = true)
  5624. {
  5625. // Some non-properly encoded file names can cause the whole file to be
  5626. // skipped when uploaded. Avoid this by detecting the encoding and
  5627. // converting to UTF-8, setting the source as ASCII (a reasonably
  5628. // limited characters set) if nothing could be found (BT#
  5629. $encoding = api_detect_encoding($filename);
  5630. if (empty($encoding)) {
  5631. $encoding = 'ASCII';
  5632. if (!api_is_valid_ascii($filename)) {
  5633. // try iconv and try non standard ASCII a.k.a CP437
  5634. // see BT#15022
  5635. if (function_exists('iconv')) {
  5636. $result = iconv('CP437', 'UTF-8', $filename);
  5637. if (api_is_valid_utf8($result)) {
  5638. $filename = $result;
  5639. $encoding = 'UTF-8';
  5640. }
  5641. }
  5642. }
  5643. }
  5644. $filename = api_to_system_encoding($filename, $encoding);
  5645. $url = URLify::filter(
  5646. $filename,
  5647. 250,
  5648. '',
  5649. true,
  5650. true,
  5651. false,
  5652. false,
  5653. $treat_spaces_as_hyphens
  5654. );
  5655. return $url;
  5656. }
  5657. /**
  5658. * Fixes the $_SERVER['REQUEST_URI'] that is empty in IIS6.
  5659. *
  5660. * @author Ivan Tcholakov, 28-JUN-2006.
  5661. */
  5662. function api_request_uri()
  5663. {
  5664. if (!empty($_SERVER['REQUEST_URI'])) {
  5665. return $_SERVER['REQUEST_URI'];
  5666. }
  5667. $uri = $_SERVER['SCRIPT_NAME'];
  5668. if (!empty($_SERVER['QUERY_STRING'])) {
  5669. $uri .= '?'.$_SERVER['QUERY_STRING'];
  5670. }
  5671. $_SERVER['REQUEST_URI'] = $uri;
  5672. return $uri;
  5673. }
  5674. /** Gets the current access_url id of the Chamilo Platform
  5675. * @author Julio Montoya <gugli100@gmail.com>
  5676. *
  5677. * @return int access_url_id of the current Chamilo Installation
  5678. */
  5679. function api_get_current_access_url_id()
  5680. {
  5681. $access_url_table = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
  5682. $path = Database::escape_string(api_get_path(WEB_PATH));
  5683. $sql = "SELECT id FROM $access_url_table WHERE url = '".$path."'";
  5684. $result = Database::query($sql);
  5685. if (Database::num_rows($result) > 0) {
  5686. $access_url_id = Database::result($result, 0, 0);
  5687. if ($access_url_id === false) {
  5688. return -1;
  5689. }
  5690. return (int) $access_url_id;
  5691. }
  5692. //if the url in WEB_PATH was not found, it can only mean that there is
  5693. // either a configuration problem or the first URL has not been defined yet
  5694. // (by default it is http://localhost/). Thus the more sensible thing we can
  5695. // do is return 1 (the main URL) as the user cannot hack this value anyway
  5696. return 1;
  5697. }
  5698. /**
  5699. * Gets the registered urls from a given user id.
  5700. *
  5701. * @author Julio Montoya <gugli100@gmail.com>
  5702. *
  5703. * @return int user id
  5704. */
  5705. function api_get_access_url_from_user($user_id)
  5706. {
  5707. $user_id = (int) $user_id;
  5708. $table_url_rel_user = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  5709. $table_url = Database::get_main_table(TABLE_MAIN_ACCESS_URL);
  5710. $sql = "SELECT access_url_id
  5711. FROM $table_url_rel_user url_rel_user
  5712. INNER JOIN $table_url u
  5713. ON (url_rel_user.access_url_id = u.id)
  5714. WHERE user_id = ".intval($user_id);
  5715. $result = Database::query($sql);
  5716. $list = [];
  5717. while ($row = Database::fetch_array($result, 'ASSOC')) {
  5718. $list[] = $row['access_url_id'];
  5719. }
  5720. return $list;
  5721. }
  5722. /**
  5723. * Gets the status of a user in a course.
  5724. *
  5725. * @param int $user_id
  5726. * @param int $courseId
  5727. *
  5728. * @return int user status
  5729. */
  5730. function api_get_status_of_user_in_course($user_id, $courseId)
  5731. {
  5732. $tbl_rel_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  5733. if (!empty($user_id) && !empty($courseId)) {
  5734. $user_id = intval($user_id);
  5735. $courseId = intval($courseId);
  5736. $sql = 'SELECT status
  5737. FROM '.$tbl_rel_course_user.'
  5738. WHERE user_id='.$user_id.' AND c_id = '.$courseId;
  5739. $result = Database::query($sql);
  5740. $row_status = Database::fetch_array($result, 'ASSOC');
  5741. return $row_status['status'];
  5742. } else {
  5743. return 0;
  5744. }
  5745. }
  5746. /**
  5747. * Checks whether the curent user is in a group or not.
  5748. *
  5749. * @param string The group id - optional (takes it from session if not given)
  5750. * @param string The course code - optional (no additional check by course if course code is not given)
  5751. *
  5752. * @return bool
  5753. *
  5754. * @author Ivan Tcholakov
  5755. */
  5756. function api_is_in_group($groupIdParam = null, $courseCodeParam = null)
  5757. {
  5758. if (!empty($courseCodeParam)) {
  5759. $courseCode = api_get_course_id();
  5760. if (!empty($courseCode)) {
  5761. if ($courseCodeParam != $courseCode) {
  5762. return false;
  5763. }
  5764. } else {
  5765. return false;
  5766. }
  5767. }
  5768. $groupId = api_get_group_id();
  5769. if (isset($groupId) && $groupId != '') {
  5770. if (!empty($groupIdParam)) {
  5771. return $groupIdParam == $groupId;
  5772. } else {
  5773. return true;
  5774. }
  5775. }
  5776. return false;
  5777. }
  5778. /**
  5779. * Checks whether a secret key is valid.
  5780. *
  5781. * @param string $original_key_secret - secret key from (webservice) client
  5782. * @param string $security_key - security key from Chamilo
  5783. *
  5784. * @return bool - true if secret key is valid, false otherwise
  5785. */
  5786. function api_is_valid_secret_key($original_key_secret, $security_key)
  5787. {
  5788. return $original_key_secret == sha1($security_key);
  5789. }
  5790. /**
  5791. * Checks whether a user is into course.
  5792. *
  5793. * @param int $course_id - the course id
  5794. * @param int $user_id - the user id
  5795. *
  5796. * @return bool
  5797. */
  5798. function api_is_user_of_course($course_id, $user_id)
  5799. {
  5800. $tbl_course_rel_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  5801. $sql = 'SELECT user_id FROM '.$tbl_course_rel_user.'
  5802. WHERE
  5803. c_id ="'.intval($course_id).'" AND
  5804. user_id = "'.intval($user_id).'" AND
  5805. relation_type <> '.COURSE_RELATION_TYPE_RRHH.' ';
  5806. $result = Database::query($sql);
  5807. return Database::num_rows($result) == 1;
  5808. }
  5809. /**
  5810. * Checks whether the server's operating system is Windows (TM).
  5811. *
  5812. * @return bool - true if the operating system is Windows, false otherwise
  5813. */
  5814. function api_is_windows_os()
  5815. {
  5816. if (function_exists('php_uname')) {
  5817. // php_uname() exists as of PHP 4.0.2, according to the documentation.
  5818. // We expect that this function will always work for Chamilo 1.8.x.
  5819. $os = php_uname();
  5820. }
  5821. // The following methods are not needed, but let them stay, just in case.
  5822. elseif (isset($_ENV['OS'])) {
  5823. // Sometimes $_ENV['OS'] may not be present (bugs?)
  5824. $os = $_ENV['OS'];
  5825. } elseif (defined('PHP_OS')) {
  5826. // PHP_OS means on which OS PHP was compiled, this is why
  5827. // using PHP_OS is the last choice for detection.
  5828. $os = PHP_OS;
  5829. } else {
  5830. return false;
  5831. }
  5832. return strtolower(substr((string) $os, 0, 3)) == 'win';
  5833. }
  5834. /**
  5835. * This function informs whether the sent request is XMLHttpRequest.
  5836. */
  5837. function api_is_xml_http_request()
  5838. {
  5839. return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
  5840. }
  5841. /**
  5842. * This wrapper function has been implemented for avoiding some known problems about the function getimagesize().
  5843. *
  5844. * @see http://php.net/manual/en/function.getimagesize.php
  5845. * @see http://www.dokeos.com/forum/viewtopic.php?t=12345
  5846. * @see http://www.dokeos.com/forum/viewtopic.php?t=16355
  5847. *
  5848. * @return int
  5849. */
  5850. function api_getimagesize($path)
  5851. {
  5852. $image = new Image($path);
  5853. return $image->get_image_size();
  5854. }
  5855. /**
  5856. * This function resizes an image, with preserving its proportions (or aspect ratio).
  5857. *
  5858. * @author Ivan Tcholakov, MAY-2009.
  5859. *
  5860. * @param int $image System path or URL of the image
  5861. * @param int $target_width Targeted width
  5862. * @param int $target_height Targeted height
  5863. *
  5864. * @return array Calculated new width and height
  5865. */
  5866. function api_resize_image($image, $target_width, $target_height)
  5867. {
  5868. $image_properties = api_getimagesize($image);
  5869. return api_calculate_image_size(
  5870. $image_properties['width'],
  5871. $image_properties['height'],
  5872. $target_width,
  5873. $target_height
  5874. );
  5875. }
  5876. /**
  5877. * This function calculates new image size, with preserving image's proportions (or aspect ratio).
  5878. *
  5879. * @author Ivan Tcholakov, MAY-2009.
  5880. * @author The initial idea has been taken from code by Patrick Cool, MAY-2004.
  5881. *
  5882. * @param int $image_width Initial width
  5883. * @param int $image_height Initial height
  5884. * @param int $target_width Targeted width
  5885. * @param int $target_height Targeted height
  5886. *
  5887. * @return array Calculated new width and height
  5888. */
  5889. function api_calculate_image_size(
  5890. $image_width,
  5891. $image_height,
  5892. $target_width,
  5893. $target_height
  5894. ) {
  5895. // Only maths is here.
  5896. $result = ['width' => $image_width, 'height' => $image_height];
  5897. if ($image_width <= 0 || $image_height <= 0) {
  5898. return $result;
  5899. }
  5900. $resize_factor_width = $target_width / $image_width;
  5901. $resize_factor_height = $target_height / $image_height;
  5902. $delta_width = $target_width - $image_width * $resize_factor_height;
  5903. $delta_height = $target_height - $image_height * $resize_factor_width;
  5904. if ($delta_width > $delta_height) {
  5905. $result['width'] = ceil($image_width * $resize_factor_height);
  5906. $result['height'] = ceil($image_height * $resize_factor_height);
  5907. } elseif ($delta_width < $delta_height) {
  5908. $result['width'] = ceil($image_width * $resize_factor_width);
  5909. $result['height'] = ceil($image_height * $resize_factor_width);
  5910. } else {
  5911. $result['width'] = ceil($target_width);
  5912. $result['height'] = ceil($target_height);
  5913. }
  5914. return $result;
  5915. }
  5916. /**
  5917. * Returns a list of Chamilo's tools or
  5918. * checks whether a given identificator is a valid Chamilo's tool.
  5919. *
  5920. * @author Isaac flores paz
  5921. *
  5922. * @param string The tool name to filter
  5923. *
  5924. * @return mixed Filtered string or array
  5925. */
  5926. function api_get_tools_lists($my_tool = null)
  5927. {
  5928. $tools_list = [
  5929. TOOL_DOCUMENT,
  5930. TOOL_THUMBNAIL,
  5931. TOOL_HOTPOTATOES,
  5932. TOOL_CALENDAR_EVENT,
  5933. TOOL_LINK,
  5934. TOOL_COURSE_DESCRIPTION,
  5935. TOOL_SEARCH,
  5936. TOOL_LEARNPATH,
  5937. TOOL_ANNOUNCEMENT,
  5938. TOOL_FORUM,
  5939. TOOL_THREAD,
  5940. TOOL_POST,
  5941. TOOL_DROPBOX,
  5942. TOOL_QUIZ,
  5943. TOOL_USER,
  5944. TOOL_GROUP,
  5945. TOOL_BLOGS,
  5946. TOOL_CHAT,
  5947. TOOL_STUDENTPUBLICATION,
  5948. TOOL_TRACKING,
  5949. TOOL_HOMEPAGE_LINK,
  5950. TOOL_COURSE_SETTING,
  5951. TOOL_BACKUP,
  5952. TOOL_COPY_COURSE_CONTENT,
  5953. TOOL_RECYCLE_COURSE,
  5954. TOOL_COURSE_HOMEPAGE,
  5955. TOOL_COURSE_RIGHTS_OVERVIEW,
  5956. TOOL_UPLOAD,
  5957. TOOL_COURSE_MAINTENANCE,
  5958. TOOL_SURVEY,
  5959. TOOL_WIKI,
  5960. TOOL_GLOSSARY,
  5961. TOOL_GRADEBOOK,
  5962. TOOL_NOTEBOOK,
  5963. TOOL_ATTENDANCE,
  5964. TOOL_COURSE_PROGRESS,
  5965. ];
  5966. if (empty($my_tool)) {
  5967. return $tools_list;
  5968. }
  5969. return in_array($my_tool, $tools_list) ? $my_tool : '';
  5970. }
  5971. /**
  5972. * Checks whether we already approved the last version term and condition.
  5973. *
  5974. * @param int user id
  5975. *
  5976. * @return bool true if we pass false otherwise
  5977. */
  5978. function api_check_term_condition($userId)
  5979. {
  5980. if (api_get_setting('allow_terms_conditions') === 'true') {
  5981. // Check if exists terms and conditions
  5982. if (LegalManager::count() == 0) {
  5983. return true;
  5984. }
  5985. $extraFieldValue = new ExtraFieldValue('user');
  5986. $data = $extraFieldValue->get_values_by_handler_and_field_variable(
  5987. $userId,
  5988. 'legal_accept'
  5989. );
  5990. if (!empty($data) && isset($data['value']) && !empty($data['value'])) {
  5991. $result = $data['value'];
  5992. $user_conditions = explode(':', $result);
  5993. $version = $user_conditions[0];
  5994. $langId = $user_conditions[1];
  5995. $realVersion = LegalManager::get_last_version($langId);
  5996. return $version >= $realVersion;
  5997. }
  5998. return false;
  5999. }
  6000. return false;
  6001. }
  6002. /**
  6003. * Gets all information of a tool into course.
  6004. *
  6005. * @param int The tool id
  6006. *
  6007. * @return array
  6008. */
  6009. function api_get_tool_information_by_name($name)
  6010. {
  6011. $t_tool = Database::get_course_table(TABLE_TOOL_LIST);
  6012. $course_id = api_get_course_int_id();
  6013. $sql = "SELECT * FROM $t_tool
  6014. WHERE c_id = $course_id AND name = '".Database::escape_string($name)."' ";
  6015. $rs = Database::query($sql);
  6016. return Database::fetch_array($rs, 'ASSOC');
  6017. }
  6018. /**
  6019. * Function used to protect a "global" admin script.
  6020. * The function blocks access when the user has no global platform admin rights.
  6021. * Global admins are the admins that are registered in the main.admin table
  6022. * AND the users who have access to the "principal" portal.
  6023. * That means that there is a record in the main.access_url_rel_user table
  6024. * with his user id and the access_url_id=1.
  6025. *
  6026. * @author Julio Montoya
  6027. *
  6028. * @param int $user_id
  6029. *
  6030. * @return bool
  6031. */
  6032. function api_is_global_platform_admin($user_id = null)
  6033. {
  6034. $user_id = (int) $user_id;
  6035. if (empty($user_id)) {
  6036. $user_id = api_get_user_id();
  6037. }
  6038. if (api_is_platform_admin_by_id($user_id)) {
  6039. $urlList = api_get_access_url_from_user($user_id);
  6040. // The admin is registered in the first "main" site with access_url_id = 1
  6041. if (in_array(1, $urlList)) {
  6042. return true;
  6043. } else {
  6044. return false;
  6045. }
  6046. }
  6047. return false;
  6048. }
  6049. /**
  6050. * @param int $admin_id_to_check
  6051. * @param int $my_user_id
  6052. * @param bool $allow_session_admin
  6053. *
  6054. * @return bool
  6055. */
  6056. function api_global_admin_can_edit_admin(
  6057. $admin_id_to_check,
  6058. $my_user_id = null,
  6059. $allow_session_admin = false
  6060. ) {
  6061. if (empty($my_user_id)) {
  6062. $my_user_id = api_get_user_id();
  6063. }
  6064. $iam_a_global_admin = api_is_global_platform_admin($my_user_id);
  6065. $user_is_global_admin = api_is_global_platform_admin($admin_id_to_check);
  6066. if ($iam_a_global_admin) {
  6067. // Global admin can edit everything
  6068. return true;
  6069. } else {
  6070. // If i'm a simple admin
  6071. $is_platform_admin = api_is_platform_admin_by_id($my_user_id);
  6072. if ($allow_session_admin) {
  6073. $is_platform_admin = api_is_platform_admin_by_id($my_user_id) || (api_get_user_status($my_user_id) == SESSIONADMIN);
  6074. }
  6075. if ($is_platform_admin) {
  6076. if ($user_is_global_admin) {
  6077. return false;
  6078. } else {
  6079. return true;
  6080. }
  6081. } else {
  6082. return false;
  6083. }
  6084. }
  6085. }
  6086. /**
  6087. * @param int $admin_id_to_check
  6088. * @param int $my_user_id
  6089. * @param bool $allow_session_admin
  6090. *
  6091. * @return bool|null
  6092. */
  6093. function api_protect_super_admin($admin_id_to_check, $my_user_id = null, $allow_session_admin = false)
  6094. {
  6095. if (api_global_admin_can_edit_admin($admin_id_to_check, $my_user_id, $allow_session_admin)) {
  6096. return true;
  6097. } else {
  6098. api_not_allowed();
  6099. }
  6100. }
  6101. /**
  6102. * Function used to protect a global admin script.
  6103. * The function blocks access when the user has no global platform admin rights.
  6104. * See also the api_is_global_platform_admin() function wich defines who's a "global" admin.
  6105. *
  6106. * @author Julio Montoya
  6107. */
  6108. function api_protect_global_admin_script()
  6109. {
  6110. if (!api_is_global_platform_admin()) {
  6111. api_not_allowed();
  6112. return false;
  6113. }
  6114. return true;
  6115. }
  6116. /**
  6117. * Get active template.
  6118. *
  6119. * @param string theme type (optional: default)
  6120. * @param string path absolute(abs) or relative(rel) (optional:rel)
  6121. *
  6122. * @return string actived template path
  6123. */
  6124. function api_get_template($path_type = 'rel')
  6125. {
  6126. $path_types = ['rel', 'abs'];
  6127. $template_path = '';
  6128. if (in_array($path_type, $path_types)) {
  6129. if ($path_type == 'rel') {
  6130. $template_path = api_get_path(SYS_TEMPLATE_PATH);
  6131. } else {
  6132. $template_path = api_get_path(WEB_TEMPLATE_PATH);
  6133. }
  6134. }
  6135. $actived_theme = 'default';
  6136. if (api_get_setting('active_template')) {
  6137. $actived_theme = api_get_setting('active_template');
  6138. }
  6139. $actived_theme_path = $template_path.$actived_theme.DIRECTORY_SEPARATOR;
  6140. return $actived_theme_path;
  6141. }
  6142. /**
  6143. * Check browser support for specific file types or features
  6144. * This function checks if the user's browser supports a file format or given
  6145. * feature, or returns the current browser and major version when
  6146. * $format=check_browser. Only a limited number of formats and features are
  6147. * checked by this method. Make sure you check its definition first.
  6148. *
  6149. * @param string $format Can be a file format (extension like svg, webm, ...) or a feature (like autocapitalize, ...)
  6150. *
  6151. * @return bool or return text array if $format=check_browser
  6152. *
  6153. * @author Juan Carlos Raña Trabado
  6154. */
  6155. function api_browser_support($format = '')
  6156. {
  6157. $browser = new Browser();
  6158. $current_browser = $browser->getBrowser();
  6159. $a_versiontemp = explode('.', $browser->getVersion());
  6160. $current_majorver = $a_versiontemp[0];
  6161. static $result;
  6162. if (isset($result[$format])) {
  6163. return $result[$format];
  6164. }
  6165. // Native svg support
  6166. if ($format == 'svg') {
  6167. if (($current_browser == 'Internet Explorer' && $current_majorver >= 9) ||
  6168. ($current_browser == 'Firefox' && $current_majorver > 1) ||
  6169. ($current_browser == 'Safari' && $current_majorver >= 4) ||
  6170. ($current_browser == 'Chrome' && $current_majorver >= 1) ||
  6171. ($current_browser == 'Opera' && $current_majorver >= 9)
  6172. ) {
  6173. $result[$format] = true;
  6174. return true;
  6175. } else {
  6176. $result[$format] = false;
  6177. return false;
  6178. }
  6179. } elseif ($format == 'pdf') {
  6180. // native pdf support
  6181. if ($current_browser == 'Chrome' && $current_majorver >= 6) {
  6182. $result[$format] = true;
  6183. return true;
  6184. } else {
  6185. $result[$format] = false;
  6186. return false;
  6187. }
  6188. } elseif ($format == 'tif' || $format == 'tiff') {
  6189. //native tif support
  6190. if ($current_browser == 'Safari' && $current_majorver >= 5) {
  6191. $result[$format] = true;
  6192. return true;
  6193. } else {
  6194. $result[$format] = false;
  6195. return false;
  6196. }
  6197. } elseif ($format == 'ogg' || $format == 'ogx' || $format == 'ogv' || $format == 'oga') {
  6198. //native ogg, ogv,oga support
  6199. if (($current_browser == 'Firefox' && $current_majorver >= 3) ||
  6200. ($current_browser == 'Chrome' && $current_majorver >= 3) ||
  6201. ($current_browser == 'Opera' && $current_majorver >= 9)) {
  6202. $result[$format] = true;
  6203. return true;
  6204. } else {
  6205. $result[$format] = false;
  6206. return false;
  6207. }
  6208. } elseif ($format == 'mpg' || $format == 'mpeg') {
  6209. //native mpg support
  6210. if (($current_browser == 'Safari' && $current_majorver >= 5)) {
  6211. $result[$format] = true;
  6212. return true;
  6213. } else {
  6214. $result[$format] = false;
  6215. return false;
  6216. }
  6217. } elseif ($format == 'mp4') {
  6218. //native mp4 support (TODO: Android, iPhone)
  6219. if ($current_browser == 'Android' || $current_browser == 'iPhone') {
  6220. $result[$format] = true;
  6221. return true;
  6222. } else {
  6223. $result[$format] = false;
  6224. return false;
  6225. }
  6226. } elseif ($format == 'mov') {
  6227. //native mov support( TODO:check iPhone)
  6228. if ($current_browser == 'Safari' && $current_majorver >= 5 || $current_browser == 'iPhone') {
  6229. $result[$format] = true;
  6230. return true;
  6231. } else {
  6232. $result[$format] = false;
  6233. return false;
  6234. }
  6235. } elseif ($format == 'avi') {
  6236. //native avi support
  6237. if ($current_browser == 'Safari' && $current_majorver >= 5) {
  6238. $result[$format] = true;
  6239. return true;
  6240. } else {
  6241. $result[$format] = false;
  6242. return false;
  6243. }
  6244. } elseif ($format == 'wmv') {
  6245. //native wmv support
  6246. if ($current_browser == 'Firefox' && $current_majorver >= 4) {
  6247. $result[$format] = true;
  6248. return true;
  6249. } else {
  6250. $result[$format] = false;
  6251. return false;
  6252. }
  6253. } elseif ($format == 'webm') {
  6254. //native webm support (TODO:check IE9, Chrome9, Android)
  6255. if (($current_browser == 'Firefox' && $current_majorver >= 4) ||
  6256. ($current_browser == 'Opera' && $current_majorver >= 9) ||
  6257. ($current_browser == 'Internet Explorer' && $current_majorver >= 9) ||
  6258. ($current_browser == 'Chrome' && $current_majorver >= 9) ||
  6259. $current_browser == 'Android'
  6260. ) {
  6261. $result[$format] = true;
  6262. return true;
  6263. } else {
  6264. $result[$format] = false;
  6265. return false;
  6266. }
  6267. } elseif ($format == 'wav') {
  6268. //native wav support (only some codecs !)
  6269. if (($current_browser == 'Firefox' && $current_majorver >= 4) ||
  6270. ($current_browser == 'Safari' && $current_majorver >= 5) ||
  6271. ($current_browser == 'Opera' && $current_majorver >= 9) ||
  6272. ($current_browser == 'Internet Explorer' && $current_majorver >= 9) ||
  6273. ($current_browser == 'Chrome' && $current_majorver > 9) ||
  6274. $current_browser == 'Android' ||
  6275. $current_browser == 'iPhone'
  6276. ) {
  6277. $result[$format] = true;
  6278. return true;
  6279. } else {
  6280. $result[$format] = false;
  6281. return false;
  6282. }
  6283. } elseif ($format == 'mid' || $format == 'kar') {
  6284. //native midi support (TODO:check Android)
  6285. if ($current_browser == 'Opera' && $current_majorver >= 9 || $current_browser == 'Android') {
  6286. $result[$format] = true;
  6287. return true;
  6288. } else {
  6289. $result[$format] = false;
  6290. return false;
  6291. }
  6292. } elseif ($format == 'wma') {
  6293. //native wma support
  6294. if ($current_browser == 'Firefox' && $current_majorver >= 4) {
  6295. $result[$format] = true;
  6296. return true;
  6297. } else {
  6298. $result[$format] = false;
  6299. return false;
  6300. }
  6301. } elseif ($format == 'au') {
  6302. //native au support
  6303. if ($current_browser == 'Safari' && $current_majorver >= 5) {
  6304. $result[$format] = true;
  6305. return true;
  6306. } else {
  6307. $result[$format] = false;
  6308. return false;
  6309. }
  6310. } elseif ($format == 'mp3') {
  6311. //native mp3 support (TODO:check Android, iPhone)
  6312. if (($current_browser == 'Safari' && $current_majorver >= 5) ||
  6313. ($current_browser == 'Chrome' && $current_majorver >= 6) ||
  6314. ($current_browser == 'Internet Explorer' && $current_majorver >= 9) ||
  6315. $current_browser == 'Android' ||
  6316. $current_browser == 'iPhone' ||
  6317. $current_browser == 'Firefox'
  6318. ) {
  6319. $result[$format] = true;
  6320. return true;
  6321. } else {
  6322. $result[$format] = false;
  6323. return false;
  6324. }
  6325. } elseif ($format == 'autocapitalize') {
  6326. // Help avoiding showing the autocapitalize option if the browser doesn't
  6327. // support it: this attribute is against the HTML5 standard
  6328. if ($current_browser == 'Safari' || $current_browser == 'iPhone') {
  6329. return true;
  6330. } else {
  6331. return false;
  6332. }
  6333. } elseif ($format == "check_browser") {
  6334. $array_check_browser = [$current_browser, $current_majorver];
  6335. return $array_check_browser;
  6336. } else {
  6337. $result[$format] = false;
  6338. return false;
  6339. }
  6340. }
  6341. /**
  6342. * This function checks if exist path and file browscap.ini
  6343. * In order for this to work, your browscap configuration setting in php.ini
  6344. * must point to the correct location of the browscap.ini file on your system
  6345. * http://php.net/manual/en/function.get-browser.php.
  6346. *
  6347. * @return bool
  6348. *
  6349. * @author Juan Carlos Raña Trabado
  6350. */
  6351. function api_check_browscap()
  6352. {
  6353. $setting = ini_get('browscap');
  6354. if ($setting) {
  6355. $browser = get_browser($_SERVER['HTTP_USER_AGENT'], true);
  6356. if (strpos($setting, 'browscap.ini') && !empty($browser)) {
  6357. return true;
  6358. }
  6359. }
  6360. return false;
  6361. }
  6362. /**
  6363. * Returns the <script> HTML tag.
  6364. */
  6365. function api_get_js($file)
  6366. {
  6367. return '<script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/'.$file.'"></script>'."\n";
  6368. }
  6369. /**
  6370. * Returns the <script> HTML tag.
  6371. *
  6372. * @return string
  6373. */
  6374. function api_get_asset($file)
  6375. {
  6376. return '<script type="text/javascript" src="'.api_get_path(WEB_PUBLIC_PATH).'assets/'.$file.'"></script>'."\n";
  6377. }
  6378. /**
  6379. * Returns the <script> HTML tag.
  6380. *
  6381. * @param string $file
  6382. * @param string $media
  6383. *
  6384. * @return string
  6385. */
  6386. function api_get_css_asset($file, $media = 'screen')
  6387. {
  6388. return '<link href="'.api_get_path(WEB_PUBLIC_PATH).'assets/'.$file.'" rel="stylesheet" media="'.$media.'" type="text/css" />'."\n";
  6389. }
  6390. /**
  6391. * Returns the <link> HTML tag.
  6392. *
  6393. * @param string $file
  6394. * @param string $media
  6395. */
  6396. function api_get_css($file, $media = 'screen')
  6397. {
  6398. return '<link href="'.$file.'" rel="stylesheet" media="'.$media.'" type="text/css" />'."\n";
  6399. }
  6400. /**
  6401. * Returns the js header to include the jquery library.
  6402. */
  6403. function api_get_jquery_js()
  6404. {
  6405. return api_get_asset('jquery/dist/jquery.min.js');
  6406. }
  6407. /**
  6408. * Returns the jquery path.
  6409. *
  6410. * @return string
  6411. */
  6412. function api_get_jquery_web_path()
  6413. {
  6414. return api_get_path(WEB_PUBLIC_PATH).'assets/jquery/dist/jquery.min.js';
  6415. }
  6416. /**
  6417. * @return string
  6418. */
  6419. function api_get_jquery_ui_js_web_path()
  6420. {
  6421. return api_get_path(WEB_PUBLIC_PATH).'assets/jquery-ui/jquery-ui.min.js';
  6422. }
  6423. /**
  6424. * @return string
  6425. */
  6426. function api_get_jquery_ui_css_web_path()
  6427. {
  6428. return api_get_path(WEB_PUBLIC_PATH).'assets/jquery-ui/themes/smoothness/jquery-ui.min.css';
  6429. }
  6430. /**
  6431. * Returns the jquery-ui library js headers.
  6432. *
  6433. * @param bool add the jqgrid library
  6434. *
  6435. * @return string html tags
  6436. */
  6437. function api_get_jquery_ui_js($include_jqgrid = false)
  6438. {
  6439. $libraries = [];
  6440. if ($include_jqgrid) {
  6441. $libraries[] = 'jqgrid';
  6442. }
  6443. return api_get_jquery_libraries_js($libraries);
  6444. }
  6445. function api_get_jqgrid_js()
  6446. {
  6447. return api_get_jquery_libraries_js(['jqgrid']);
  6448. }
  6449. /**
  6450. * Returns the jquery library js and css headers.
  6451. *
  6452. * @param array list of jquery libraries supported jquery-ui, jqgrid
  6453. * @param bool add the jquery library
  6454. *
  6455. * @return string html tags
  6456. */
  6457. function api_get_jquery_libraries_js($libraries)
  6458. {
  6459. $js = '';
  6460. $js_path = api_get_path(WEB_LIBRARY_PATH).'javascript/';
  6461. //jqgrid js and css
  6462. if (in_array('jqgrid', $libraries)) {
  6463. $languaje = 'en';
  6464. $platform_isocode = strtolower(api_get_language_isocode());
  6465. //languages supported by jqgrid see files in main/inc/lib/javascript/jqgrid/js/i18n
  6466. $jqgrid_langs = [
  6467. 'bg', 'bg1251', 'cat', 'cn', 'cs', 'da', 'de', 'el', 'en', 'es', 'fa', 'fi', 'fr', 'gl', 'he', 'hu', 'is', 'it', 'ja', 'nl', 'no', 'pl', 'pt-br', 'pt', 'ro', 'ru', 'sk', 'sr', 'sv', 'tr', 'ua',
  6468. ];
  6469. if (in_array($platform_isocode, $jqgrid_langs)) {
  6470. $languaje = $platform_isocode;
  6471. }
  6472. //$js .= '<link rel="stylesheet" href="'.$js_path.'jqgrid/css/ui.jqgrid.css" type="text/css">';
  6473. $js .= api_get_css($js_path.'jqgrid/css/ui.jqgrid.css');
  6474. $js .= api_get_js('jqgrid/js/i18n/grid.locale-'.$languaje.'.js');
  6475. $js .= api_get_js('jqgrid/js/jquery.jqGrid.min.js');
  6476. }
  6477. //Document multiple upload funcionality
  6478. if (in_array('jquery-upload', $libraries)) {
  6479. $js .= api_get_asset('blueimp-load-image/js/load-image.all.min.js');
  6480. $js .= api_get_asset('blueimp-canvas-to-blob/js/canvas-to-blob.min.js');
  6481. $js .= api_get_asset('jquery-file-upload/js/jquery.iframe-transport.js');
  6482. $js .= api_get_asset('jquery-file-upload/js/jquery.fileupload.js');
  6483. $js .= api_get_asset('jquery-file-upload/js/jquery.fileupload-process.js');
  6484. $js .= api_get_asset('jquery-file-upload/js/jquery.fileupload-image.js');
  6485. $js .= api_get_asset('jquery-file-upload/js/jquery.fileupload-audio.js');
  6486. $js .= api_get_asset('jquery-file-upload/js/jquery.fileupload-video.js');
  6487. $js .= api_get_asset('jquery-file-upload/js/jquery.fileupload-validate.js');
  6488. $js .= api_get_css(api_get_path(WEB_PUBLIC_PATH).'assets/jquery-file-upload/css/jquery.fileupload.css');
  6489. $js .= api_get_css(api_get_path(WEB_PUBLIC_PATH).'assets/jquery-file-upload/css/jquery.fileupload-ui.css');
  6490. }
  6491. // jquery datepicker
  6492. if (in_array('datepicker', $libraries)) {
  6493. $languaje = 'en-GB';
  6494. $platform_isocode = strtolower(api_get_language_isocode());
  6495. // languages supported by jqgrid see files in main/inc/lib/javascript/jqgrid/js/i18n
  6496. $datapicker_langs = [
  6497. 'af', 'ar', 'ar-DZ', 'az', 'bg', 'bs', 'ca', 'cs', 'cy-GB', 'da', 'de', 'el', 'en-AU', 'en-GB', 'en-NZ', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'fr-CH', 'gl', 'he', 'hi', 'hr', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'ka', 'kk', 'km', 'ko', 'lb', 'lt', 'lv', 'mk', 'ml', 'ms', 'nl', 'nl-BE', 'no', 'pl', 'pt', 'pt-BR', 'rm', 'ro', 'ru', 'sk', 'sl', 'sq', 'sr', 'sr-SR', 'sv', 'ta', 'th', 'tj', 'tr', 'uk', 'vi', 'zh-CN', 'zh-HK', 'zh-TW',
  6498. ];
  6499. if (in_array($platform_isocode, $datapicker_langs)) {
  6500. $languaje = $platform_isocode;
  6501. }
  6502. $js .= api_get_js('jquery-ui/jquery-ui-i18n.min.js');
  6503. $script = '<script>
  6504. $(function(){
  6505. $.datepicker.setDefaults($.datepicker.regional["'.$languaje.'"]);
  6506. $.datepicker.regional["local"] = $.datepicker.regional["'.$languaje.'"];
  6507. });
  6508. </script>
  6509. ';
  6510. $js .= $script;
  6511. }
  6512. return $js;
  6513. }
  6514. /**
  6515. * Returns the URL to the course or session, removing the complexity of the URL
  6516. * building piece by piece.
  6517. *
  6518. * This function relies on api_get_course_info()
  6519. *
  6520. * @param string $courseCode The course code - optional (takes it from context if not given)
  6521. * @param int $sessionId The session ID - optional (takes it from context if not given)
  6522. * @param int $groupId The group ID - optional (takes it from context if not given)
  6523. *
  6524. * @return string The URL to a course, a session, or empty string if nothing works e.g. https://localhost/courses/ABC/index.php?session_id=3&gidReq=1
  6525. *
  6526. * @author Julio Montoya <gugli100@gmail.com>
  6527. */
  6528. function api_get_course_url($courseCode = null, $sessionId = null, $groupId = null)
  6529. {
  6530. $courseDirectory = '';
  6531. $url = '';
  6532. // If courseCode not set, get context or []
  6533. if (empty($courseCode)) {
  6534. $courseInfo = api_get_course_info();
  6535. } else {
  6536. $courseInfo = api_get_course_info($courseCode);
  6537. }
  6538. // If course defined, get directory, otherwise keep empty string
  6539. if (!empty($courseInfo['directory'])) {
  6540. $courseDirectory = $courseInfo['directory'];
  6541. }
  6542. // If sessionId not set, get context or 0
  6543. if (empty($sessionId)) {
  6544. $sessionId = api_get_session_id();
  6545. }
  6546. // If groupId not set, get context or 0
  6547. if (empty($groupId)) {
  6548. $groupId = api_get_group_id();
  6549. }
  6550. // Build the URL
  6551. if (!empty($courseDirectory)) {
  6552. // directory not empty, so we do have a course
  6553. $url = api_get_path(WEB_COURSE_PATH).$courseDirectory.'/index.php?id_session='.$sessionId.'&gidReq='.$groupId;
  6554. } elseif (!empty($sessionId) && api_get_configuration_value('remove_session_url') !== true) {
  6555. // if the course was unset and the session was set, send directly to the session
  6556. $url = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$sessionId;
  6557. }
  6558. // if not valid combination was found, return an empty string
  6559. return $url;
  6560. }
  6561. /**
  6562. * Check if the current portal has the $_configuration['multiple_access_urls'] parameter on.
  6563. *
  6564. * @return bool true if multi site is enabled
  6565. */
  6566. function api_get_multiple_access_url()
  6567. {
  6568. global $_configuration;
  6569. if (isset($_configuration['multiple_access_urls']) && $_configuration['multiple_access_urls']) {
  6570. return true;
  6571. }
  6572. return false;
  6573. }
  6574. /**
  6575. * @return bool
  6576. */
  6577. function api_is_multiple_url_enabled()
  6578. {
  6579. return api_get_multiple_access_url();
  6580. }
  6581. /**
  6582. * Returns a md5 unique id.
  6583. *
  6584. * @todo add more parameters
  6585. */
  6586. function api_get_unique_id()
  6587. {
  6588. $id = md5(time().uniqid().api_get_user_id().api_get_course_id().api_get_session_id());
  6589. return $id;
  6590. }
  6591. /**
  6592. * Get home path.
  6593. *
  6594. * @return string
  6595. */
  6596. function api_get_home_path()
  6597. {
  6598. // FIX : Start the routing determination from central path definition
  6599. $home = api_get_path(SYS_HOME_PATH);
  6600. if (api_get_multiple_access_url()) {
  6601. $access_url_id = api_get_current_access_url_id();
  6602. $url_info = api_get_access_url($access_url_id);
  6603. $url = api_remove_trailing_slash(preg_replace('/https?:\/\//i', '', $url_info['url']));
  6604. $clean_url = api_replace_dangerous_char($url);
  6605. $clean_url = str_replace('/', '-', $clean_url);
  6606. $clean_url .= '/';
  6607. if ($clean_url != 'localhost/') {
  6608. // means that the multiple URL was not well configured we don't rename the $home variable
  6609. return "{$home}{$clean_url}";
  6610. }
  6611. }
  6612. return $home;
  6613. }
  6614. /**
  6615. * @param int Course id
  6616. * @param int tool id: TOOL_QUIZ, TOOL_FORUM, TOOL_STUDENTPUBLICATION, TOOL_LEARNPATH
  6617. * @param int the item id (tool id, exercise id, lp id)
  6618. *
  6619. * @return bool
  6620. */
  6621. function api_resource_is_locked_by_gradebook($item_id, $link_type, $course_code = null)
  6622. {
  6623. if (api_is_platform_admin()) {
  6624. return false;
  6625. }
  6626. if (api_get_setting('gradebook_locking_enabled') == 'true') {
  6627. if (empty($course_code)) {
  6628. $course_code = api_get_course_id();
  6629. }
  6630. $table = Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK);
  6631. $item_id = intval($item_id);
  6632. $link_type = intval($link_type);
  6633. $course_code = Database::escape_string($course_code);
  6634. $sql = "SELECT locked FROM $table
  6635. WHERE locked = 1 AND ref_id = $item_id AND type = $link_type AND course_code = '$course_code' ";
  6636. $result = Database::query($sql);
  6637. if (Database::num_rows($result)) {
  6638. return true;
  6639. }
  6640. }
  6641. return false;
  6642. }
  6643. /**
  6644. * Blocks a page if the item was added in a gradebook.
  6645. *
  6646. * @param int exercise id, work id, thread id,
  6647. * @param int LINK_EXERCISE, LINK_STUDENTPUBLICATION, LINK_LEARNPATH LINK_FORUM_THREAD, LINK_ATTENDANCE
  6648. * see gradebook/lib/be/linkfactory
  6649. * @param string course code
  6650. *
  6651. * @return false|null
  6652. */
  6653. function api_block_course_item_locked_by_gradebook($item_id, $link_type, $course_code = null)
  6654. {
  6655. if (api_is_platform_admin()) {
  6656. return false;
  6657. }
  6658. if (api_resource_is_locked_by_gradebook($item_id, $link_type, $course_code)) {
  6659. $message = Display::return_message(get_lang('ResourceLockedByGradebook'), 'warning');
  6660. api_not_allowed(true, $message);
  6661. }
  6662. }
  6663. /**
  6664. * Checks the PHP version installed is enough to run Chamilo.
  6665. *
  6666. * @param string Include path (used to load the error page)
  6667. */
  6668. function api_check_php_version($my_inc_path = null)
  6669. {
  6670. if (!function_exists('version_compare') || version_compare(phpversion(), REQUIRED_PHP_VERSION, '<')) {
  6671. $global_error_code = 1;
  6672. // Incorrect PHP version
  6673. $global_page = $my_inc_path.'global_error_message.inc.php';
  6674. if (file_exists($global_page)) {
  6675. require $global_page;
  6676. }
  6677. exit;
  6678. }
  6679. }
  6680. /**
  6681. * Checks whether the Archive directory is present and writeable. If not,
  6682. * prints a warning message.
  6683. */
  6684. function api_check_archive_dir()
  6685. {
  6686. if (is_dir(api_get_path(SYS_ARCHIVE_PATH)) && !is_writable(api_get_path(SYS_ARCHIVE_PATH))) {
  6687. $message = Display::return_message(get_lang('ArchivesDirectoryNotWriteableContactAdmin'), 'warning');
  6688. api_not_allowed(true, $message);
  6689. }
  6690. }
  6691. /**
  6692. * Returns an array of global configuration settings which should be ignored
  6693. * when printing the configuration settings screens.
  6694. *
  6695. * @return array Array of strings, each identifying one of the excluded settings
  6696. */
  6697. function api_get_locked_settings()
  6698. {
  6699. return [
  6700. 'server_type',
  6701. 'permanently_remove_deleted_files',
  6702. 'account_valid_duration',
  6703. 'service_ppt2lp',
  6704. 'wcag_anysurfer_public_pages',
  6705. 'upload_extensions_list_type',
  6706. 'upload_extensions_blacklist',
  6707. 'upload_extensions_whitelist',
  6708. 'upload_extensions_skip',
  6709. 'upload_extensions_replace_by',
  6710. 'hide_dltt_markup',
  6711. 'split_users_upload_directory',
  6712. 'permissions_for_new_directories',
  6713. 'permissions_for_new_files',
  6714. 'platform_charset',
  6715. 'ldap_description',
  6716. 'cas_activate',
  6717. 'cas_server',
  6718. 'cas_server_uri',
  6719. 'cas_port',
  6720. 'cas_protocol',
  6721. 'cas_add_user_activate',
  6722. 'update_user_info_cas_with_ldap',
  6723. 'languagePriority1',
  6724. 'languagePriority2',
  6725. 'languagePriority3',
  6726. 'languagePriority4',
  6727. 'login_is_email',
  6728. 'chamilo_database_version',
  6729. ];
  6730. }
  6731. /**
  6732. * Checks if the user is corrently logged in. Returns the user ID if he is, or
  6733. * false if he isn't. If the user ID is given and is an integer, then the same
  6734. * ID is simply returned.
  6735. *
  6736. * @param int User ID
  6737. *
  6738. * @return bool Integer User ID is logged in, or false otherwise
  6739. */
  6740. function api_user_is_login($user_id = null)
  6741. {
  6742. $user_id = empty($user_id) ? api_get_user_id() : (int) $user_id;
  6743. return $user_id && !api_is_anonymous();
  6744. }
  6745. /**
  6746. * Guess the real ip for register in the database, even in reverse proxy cases.
  6747. * To be recognized, the IP has to be found in either $_SERVER['REMOTE_ADDR'] or
  6748. * in $_SERVER['HTTP_X_FORWARDED_FOR'], which is in common use with rproxies.
  6749. * Note: the result of this function is not SQL-safe. Please escape it before
  6750. * inserting in a database.
  6751. *
  6752. * @return string the user's real ip (unsafe - escape it before inserting to db)
  6753. *
  6754. * @author Jorge Frisancho Jibaja <jrfdeft@gmail.com>, USIL - Some changes to allow the use of real IP using reverse proxy
  6755. *
  6756. * @version CEV CHANGE 24APR2012
  6757. */
  6758. function api_get_real_ip()
  6759. {
  6760. $ip = trim($_SERVER['REMOTE_ADDR']);
  6761. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  6762. if (preg_match('/,/', $_SERVER['HTTP_X_FORWARDED_FOR'])) {
  6763. @list($ip1, $ip2) = @explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  6764. } else {
  6765. $ip1 = $_SERVER['HTTP_X_FORWARDED_FOR'];
  6766. }
  6767. $ip = trim($ip1);
  6768. }
  6769. return $ip;
  6770. }
  6771. /**
  6772. * Checks whether an IP is included inside an IP range.
  6773. *
  6774. * @param string IP address
  6775. * @param string IP range
  6776. * @param string $ip
  6777. *
  6778. * @return bool True if IP is in the range, false otherwise
  6779. *
  6780. * @author claudiu at cnixs dot com on http://www.php.net/manual/fr/ref.network.php#55230
  6781. * @author Yannick Warnier for improvements and managment of multiple ranges
  6782. *
  6783. * @todo check for IPv6 support
  6784. */
  6785. function api_check_ip_in_range($ip, $range)
  6786. {
  6787. if (empty($ip) or empty($range)) {
  6788. return false;
  6789. }
  6790. $ip_ip = ip2long($ip);
  6791. // divide range param into array of elements
  6792. if (strpos($range, ',') !== false) {
  6793. $ranges = explode(',', $range);
  6794. } else {
  6795. $ranges = [$range];
  6796. }
  6797. foreach ($ranges as $range) {
  6798. $range = trim($range);
  6799. if (empty($range)) {
  6800. continue;
  6801. }
  6802. if (strpos($range, '/') === false) {
  6803. if (strcmp($ip, $range) === 0) {
  6804. return true; // there is a direct IP match, return OK
  6805. }
  6806. continue; //otherwise, get to the next range
  6807. }
  6808. // the range contains a "/", so analyse completely
  6809. list($net, $mask) = explode("/", $range);
  6810. $ip_net = ip2long($net);
  6811. // mask binary magic
  6812. $ip_mask = ~((1 << (32 - $mask)) - 1);
  6813. $ip_ip_net = $ip_ip & $ip_mask;
  6814. if ($ip_ip_net == $ip_net) {
  6815. return true;
  6816. }
  6817. }
  6818. return false;
  6819. }
  6820. function api_check_user_access_to_legal($course_visibility)
  6821. {
  6822. $course_visibility_list = [COURSE_VISIBILITY_OPEN_WORLD, COURSE_VISIBILITY_OPEN_PLATFORM];
  6823. return in_array($course_visibility, $course_visibility_list) || api_is_drh();
  6824. }
  6825. /**
  6826. * Checks if the global chat is enabled or not.
  6827. *
  6828. * @return bool
  6829. */
  6830. function api_is_global_chat_enabled()
  6831. {
  6832. return
  6833. !api_is_anonymous() &&
  6834. api_get_setting('allow_global_chat') === 'true' &&
  6835. api_get_setting('allow_social_tool') === 'true';
  6836. }
  6837. /**
  6838. * @todo Fix tool_visible_by_default_at_creation labels
  6839. *
  6840. * @param int $item_id
  6841. * @param int $tool_id
  6842. * @param int $group_id id
  6843. * @param array $courseInfo
  6844. * @param int $sessionId
  6845. * @param int $userId
  6846. */
  6847. function api_set_default_visibility(
  6848. $item_id,
  6849. $tool_id,
  6850. $group_id = 0,
  6851. $courseInfo = [],
  6852. $sessionId = 0,
  6853. $userId = 0
  6854. ) {
  6855. $courseInfo = empty($courseInfo) ? api_get_course_info() : $courseInfo;
  6856. $courseId = $courseInfo['real_id'];
  6857. $courseCode = $courseInfo['code'];
  6858. $sessionId = empty($sessionId) ? api_get_session_id() : $sessionId;
  6859. $userId = empty($userId) ? api_get_user_id() : $userId;
  6860. // if group is null force group_id = 0, this force is needed to create a LP folder with group = 0
  6861. if (is_null($group_id)) {
  6862. $group_id = 0;
  6863. } else {
  6864. $group_id = empty($group_id) ? api_get_group_id() : $group_id;
  6865. }
  6866. $groupInfo = [];
  6867. if (!empty($group_id)) {
  6868. $groupInfo = GroupManager::get_group_properties($group_id);
  6869. }
  6870. $original_tool_id = $tool_id;
  6871. switch ($tool_id) {
  6872. case TOOL_LINK:
  6873. case TOOL_LINK_CATEGORY:
  6874. $tool_id = 'links';
  6875. break;
  6876. case TOOL_DOCUMENT:
  6877. $tool_id = 'documents';
  6878. break;
  6879. case TOOL_LEARNPATH:
  6880. $tool_id = 'learning';
  6881. break;
  6882. case TOOL_ANNOUNCEMENT:
  6883. $tool_id = 'announcements';
  6884. break;
  6885. case TOOL_FORUM:
  6886. case TOOL_FORUM_CATEGORY:
  6887. case TOOL_FORUM_THREAD:
  6888. $tool_id = 'forums';
  6889. break;
  6890. case TOOL_QUIZ:
  6891. $tool_id = 'quiz';
  6892. break;
  6893. }
  6894. $setting = api_get_setting('tool_visible_by_default_at_creation');
  6895. if (isset($setting[$tool_id])) {
  6896. $visibility = 'invisible';
  6897. if ($setting[$tool_id] == 'true') {
  6898. $visibility = 'visible';
  6899. }
  6900. // Read the portal and course default visibility
  6901. if ($tool_id == 'documents') {
  6902. $visibility = DocumentManager::getDocumentDefaultVisibility($courseCode);
  6903. }
  6904. api_item_property_update(
  6905. $courseInfo,
  6906. $original_tool_id,
  6907. $item_id,
  6908. $visibility,
  6909. $userId,
  6910. $groupInfo,
  6911. null,
  6912. null,
  6913. null,
  6914. $sessionId
  6915. );
  6916. // Fixes default visibility for tests
  6917. switch ($original_tool_id) {
  6918. case TOOL_QUIZ:
  6919. if (empty($sessionId)) {
  6920. $objExerciseTmp = new Exercise($courseId);
  6921. $objExerciseTmp->read($item_id);
  6922. if ($visibility == 'visible') {
  6923. $objExerciseTmp->enable();
  6924. $objExerciseTmp->save();
  6925. } else {
  6926. $objExerciseTmp->disable();
  6927. $objExerciseTmp->save();
  6928. }
  6929. }
  6930. break;
  6931. }
  6932. }
  6933. }
  6934. /**
  6935. * @return string
  6936. */
  6937. function api_get_security_key()
  6938. {
  6939. return api_get_configuration_value('security_key');
  6940. }
  6941. /**
  6942. * @param int $user_id
  6943. * @param int $courseId
  6944. * @param int $session_id
  6945. *
  6946. * @return array
  6947. */
  6948. function api_detect_user_roles($user_id, $courseId, $session_id = 0)
  6949. {
  6950. $user_roles = [];
  6951. $courseInfo = api_get_course_info_by_id($courseId);
  6952. $course_code = $courseInfo['code'];
  6953. $url_id = api_get_current_access_url_id();
  6954. if (api_is_platform_admin_by_id($user_id, $url_id)) {
  6955. $user_roles[] = PLATFORM_ADMIN;
  6956. }
  6957. /*if (api_is_drh()) {
  6958. $user_roles[] = DRH;
  6959. }*/
  6960. if (!empty($session_id)) {
  6961. if (SessionManager::user_is_general_coach($user_id, $session_id)) {
  6962. $user_roles[] = SESSION_GENERAL_COACH;
  6963. }
  6964. }
  6965. if (!empty($course_code)) {
  6966. if (empty($session_id)) {
  6967. if (CourseManager::is_course_teacher($user_id, $course_code)) {
  6968. $user_roles[] = COURSEMANAGER;
  6969. }
  6970. if (CourseManager::get_tutor_in_course_status($user_id, $courseInfo['real_id'])) {
  6971. $user_roles[] = COURSE_TUTOR;
  6972. }
  6973. if (CourseManager::is_user_subscribed_in_course($user_id, $course_code)) {
  6974. $user_roles[] = COURSE_STUDENT;
  6975. }
  6976. } else {
  6977. $user_status_in_session = SessionManager::get_user_status_in_course_session(
  6978. $user_id,
  6979. $courseId,
  6980. $session_id
  6981. );
  6982. if (!empty($user_status_in_session)) {
  6983. if ($user_status_in_session == 0) {
  6984. $user_roles[] = SESSION_STUDENT;
  6985. }
  6986. if ($user_status_in_session == 2) {
  6987. $user_roles[] = SESSION_COURSE_COACH;
  6988. }
  6989. }
  6990. /*if (api_is_course_session_coach($user_id, $course_code, $session_id)) {
  6991. $user_roles[] = SESSION_COURSE_COACH;
  6992. }*/
  6993. }
  6994. }
  6995. return $user_roles;
  6996. }
  6997. /**
  6998. * @param int $courseId
  6999. * @param int $session_id
  7000. *
  7001. * @return bool
  7002. */
  7003. function api_coach_can_edit_view_results($courseId = null, $session_id = null)
  7004. {
  7005. if (api_is_platform_admin()) {
  7006. return true;
  7007. }
  7008. $user_id = api_get_user_id();
  7009. if (empty($courseId)) {
  7010. $courseId = api_get_course_int_id();
  7011. }
  7012. if (empty($session_id)) {
  7013. $session_id = api_get_session_id();
  7014. }
  7015. $roles = api_detect_user_roles($user_id, $courseId, $session_id);
  7016. if (in_array(SESSION_COURSE_COACH, $roles)) {
  7017. //return api_get_setting('session_tutor_reports_visibility') == 'true';
  7018. return true;
  7019. } else {
  7020. if (in_array(COURSEMANAGER, $roles)) {
  7021. return true;
  7022. }
  7023. return false;
  7024. }
  7025. }
  7026. /**
  7027. * @param string $file
  7028. *
  7029. * @return string
  7030. */
  7031. function api_get_js_simple($file)
  7032. {
  7033. return '<script type="text/javascript" src="'.$file.'"></script>'."\n";
  7034. }
  7035. function api_set_settings_and_plugins()
  7036. {
  7037. global $_configuration;
  7038. $_setting = [];
  7039. $_plugins = [];
  7040. // access_url == 1 is the default chamilo location
  7041. $settings_by_access_list = [];
  7042. $access_url_id = api_get_current_access_url_id();
  7043. if ($access_url_id != 1) {
  7044. $url_info = api_get_access_url($_configuration['access_url']);
  7045. if ($url_info['active'] == 1) {
  7046. $settings_by_access = &api_get_settings(null, 'list', $_configuration['access_url'], 1);
  7047. foreach ($settings_by_access as &$row) {
  7048. if (empty($row['variable'])) {
  7049. $row['variable'] = 0;
  7050. }
  7051. if (empty($row['subkey'])) {
  7052. $row['subkey'] = 0;
  7053. }
  7054. if (empty($row['category'])) {
  7055. $row['category'] = 0;
  7056. }
  7057. $settings_by_access_list[$row['variable']][$row['subkey']][$row['category']] = $row;
  7058. }
  7059. }
  7060. }
  7061. $result = api_get_settings(null, 'list', 1);
  7062. foreach ($result as &$row) {
  7063. if ($access_url_id != 1) {
  7064. if ($url_info['active'] == 1) {
  7065. $var = empty($row['variable']) ? 0 : $row['variable'];
  7066. $subkey = empty($row['subkey']) ? 0 : $row['subkey'];
  7067. $category = empty($row['category']) ? 0 : $row['category'];
  7068. }
  7069. if ($row['access_url_changeable'] == 1 && $url_info['active'] == 1) {
  7070. if (isset($settings_by_access_list[$var]) &&
  7071. $settings_by_access_list[$var][$subkey][$category]['selected_value'] != '') {
  7072. if ($row['subkey'] == null) {
  7073. $_setting[$row['variable']] = $settings_by_access_list[$var][$subkey][$category]['selected_value'];
  7074. } else {
  7075. $_setting[$row['variable']][$row['subkey']] = $settings_by_access_list[$var][$subkey][$category]['selected_value'];
  7076. }
  7077. } else {
  7078. if ($row['subkey'] == null) {
  7079. $_setting[$row['variable']] = $row['selected_value'];
  7080. } else {
  7081. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  7082. }
  7083. }
  7084. } else {
  7085. if ($row['subkey'] == null) {
  7086. $_setting[$row['variable']] = $row['selected_value'];
  7087. } else {
  7088. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  7089. }
  7090. }
  7091. } else {
  7092. if ($row['subkey'] == null) {
  7093. $_setting[$row['variable']] = $row['selected_value'];
  7094. } else {
  7095. $_setting[$row['variable']][$row['subkey']] = $row['selected_value'];
  7096. }
  7097. }
  7098. }
  7099. $result = api_get_settings('Plugins', 'list', $access_url_id);
  7100. $_plugins = [];
  7101. foreach ($result as &$row) {
  7102. $key = &$row['variable'];
  7103. if (is_string($_setting[$key])) {
  7104. $_setting[$key] = [];
  7105. }
  7106. $_setting[$key][] = $row['selected_value'];
  7107. $_plugins[$key][] = $row['selected_value'];
  7108. }
  7109. $_SESSION['_setting'] = $_setting;
  7110. $_SESSION['_plugins'] = $_plugins;
  7111. }
  7112. /**
  7113. * Modify default memory_limit and max_execution_time limits
  7114. * Needed when processing long tasks.
  7115. */
  7116. function api_set_more_memory_and_time_limits()
  7117. {
  7118. if (function_exists('ini_set')) {
  7119. api_set_memory_limit('256M');
  7120. ini_set('max_execution_time', 1800);
  7121. }
  7122. }
  7123. /**
  7124. * Tries to set memory limit, if authorized and new limit is higher than current.
  7125. *
  7126. * @param string $mem New memory limit
  7127. *
  7128. * @return bool True on success, false on failure or current is higher than suggested
  7129. * @assert (null) === false
  7130. * @assert (-1) === false
  7131. * @assert (0) === true
  7132. * @assert ('1G') === true
  7133. */
  7134. function api_set_memory_limit($mem)
  7135. {
  7136. //if ini_set() not available, this function is useless
  7137. if (!function_exists('ini_set') || is_null($mem) || $mem == -1) {
  7138. return false;
  7139. }
  7140. $memory_limit = ini_get('memory_limit');
  7141. if (api_get_bytes_memory_limit($mem) > api_get_bytes_memory_limit($memory_limit)) {
  7142. ini_set('memory_limit', $mem);
  7143. return true;
  7144. }
  7145. return false;
  7146. }
  7147. /**
  7148. * Gets memory limit in bytes.
  7149. *
  7150. * @param string The memory size (128M, 1G, 1000K, etc)
  7151. *
  7152. * @return int
  7153. * @assert (null) === false
  7154. * @assert ('1t') === 1099511627776
  7155. * @assert ('1g') === 1073741824
  7156. * @assert ('1m') === 1048576
  7157. * @assert ('100k') === 102400
  7158. */
  7159. function api_get_bytes_memory_limit($mem)
  7160. {
  7161. $size = strtolower(substr($mem, -1));
  7162. switch ($size) {
  7163. case 't':
  7164. $mem = intval(substr($mem, -1)) * 1024 * 1024 * 1024 * 1024;
  7165. break;
  7166. case 'g':
  7167. $mem = intval(substr($mem, 0, -1)) * 1024 * 1024 * 1024;
  7168. break;
  7169. case 'm':
  7170. $mem = intval(substr($mem, 0, -1)) * 1024 * 1024;
  7171. break;
  7172. case 'k':
  7173. $mem = intval(substr($mem, 0, -1)) * 1024;
  7174. break;
  7175. default:
  7176. // we assume it's integer only
  7177. $mem = intval($mem);
  7178. break;
  7179. }
  7180. return $mem;
  7181. }
  7182. /**
  7183. * Finds all the information about a user from username instead of user id.
  7184. *
  7185. * @param string $officialCode
  7186. *
  7187. * @return array $user_info user_id, lastname, firstname, username, email, ...
  7188. *
  7189. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  7190. */
  7191. function api_get_user_info_from_official_code($officialCode)
  7192. {
  7193. if (empty($officialCode)) {
  7194. return false;
  7195. }
  7196. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_USER)."
  7197. WHERE official_code ='".Database::escape_string($officialCode)."'";
  7198. $result = Database::query($sql);
  7199. if (Database::num_rows($result) > 0) {
  7200. $result_array = Database::fetch_array($result);
  7201. return _api_format_user($result_array);
  7202. }
  7203. return false;
  7204. }
  7205. /**
  7206. * @param string $usernameInputId
  7207. * @param string $passwordInputId
  7208. *
  7209. * @return string|null
  7210. */
  7211. function api_get_password_checker_js($usernameInputId, $passwordInputId)
  7212. {
  7213. $checkPass = api_get_setting('allow_strength_pass_checker');
  7214. $useStrengthPassChecker = $checkPass === 'true';
  7215. if ($useStrengthPassChecker === false) {
  7216. return null;
  7217. }
  7218. $translations = [
  7219. 'wordLength' => get_lang('PasswordIsTooShort'),
  7220. 'wordNotEmail' => get_lang('YourPasswordCannotBeTheSameAsYourEmail'),
  7221. 'wordSimilarToUsername' => get_lang('YourPasswordCannotContainYourUsername'),
  7222. 'wordTwoCharacterClasses' => get_lang('WordTwoCharacterClasses'),
  7223. 'wordRepetitions' => get_lang('TooManyRepetitions'),
  7224. 'wordSequences' => get_lang('YourPasswordContainsSequences'),
  7225. 'errorList' => get_lang('ErrorsFound'),
  7226. 'veryWeak' => get_lang('PasswordVeryWeak'),
  7227. 'weak' => get_lang('PasswordWeak'),
  7228. 'normal' => get_lang('PasswordNormal'),
  7229. 'medium' => get_lang('PasswordMedium'),
  7230. 'strong' => get_lang('PasswordStrong'),
  7231. 'veryStrong' => get_lang('PasswordVeryStrong'),
  7232. ];
  7233. $js = api_get_asset('pwstrength-bootstrap/dist/pwstrength-bootstrap.min.js');
  7234. $js .= "<script>
  7235. var errorMessages = {
  7236. password_to_short : \"".get_lang('PasswordIsTooShort')."\",
  7237. same_as_username : \"".get_lang('YourPasswordCannotBeTheSameAsYourUsername')."\"
  7238. };
  7239. $(function() {
  7240. var lang = ".json_encode($translations).";
  7241. var options = {
  7242. onLoad : function () {
  7243. //$('#messages').text('Start typing password');
  7244. },
  7245. onKeyUp: function (evt) {
  7246. $(evt.target).pwstrength('outputErrorList');
  7247. },
  7248. errorMessages : errorMessages,
  7249. viewports: {
  7250. progress: '#password_progress',
  7251. verdict: '#password-verdict',
  7252. errors: '#password-errors'
  7253. },
  7254. usernameField: '$usernameInputId'
  7255. };
  7256. options.i18n = {
  7257. t: function (key) {
  7258. var result = lang[key];
  7259. return result === key ? '' : result; // This assumes you return the
  7260. }
  7261. };
  7262. $('".$passwordInputId."').pwstrength(options);
  7263. });
  7264. </script>";
  7265. return $js;
  7266. }
  7267. /**
  7268. * create an user extra field called 'captcha_blocked_until_date'.
  7269. *
  7270. * @param string $username
  7271. *
  7272. * @return bool
  7273. */
  7274. function api_block_account_captcha($username)
  7275. {
  7276. $userInfo = api_get_user_info_from_username($username);
  7277. if (empty($userInfo)) {
  7278. return false;
  7279. }
  7280. $minutesToBlock = api_get_setting('captcha_time_to_block');
  7281. $time = time() + $minutesToBlock * 60;
  7282. UserManager::update_extra_field_value(
  7283. $userInfo['user_id'],
  7284. 'captcha_blocked_until_date',
  7285. api_get_utc_datetime($time)
  7286. );
  7287. return true;
  7288. }
  7289. /**
  7290. * @param string $username
  7291. *
  7292. * @return bool
  7293. */
  7294. function api_clean_account_captcha($username)
  7295. {
  7296. $userInfo = api_get_user_info_from_username($username);
  7297. if (empty($userInfo)) {
  7298. return false;
  7299. }
  7300. Session::erase('loginFailedCount');
  7301. UserManager::update_extra_field_value(
  7302. $userInfo['user_id'],
  7303. 'captcha_blocked_until_date',
  7304. null
  7305. );
  7306. return true;
  7307. }
  7308. /**
  7309. * @param string $username
  7310. *
  7311. * @return bool
  7312. */
  7313. function api_get_user_blocked_by_captcha($username)
  7314. {
  7315. $userInfo = api_get_user_info_from_username($username);
  7316. if (empty($userInfo)) {
  7317. return false;
  7318. }
  7319. $data = UserManager::get_extra_user_data_by_field(
  7320. $userInfo['user_id'],
  7321. 'captcha_blocked_until_date'
  7322. );
  7323. if (isset($data) && isset($data['captcha_blocked_until_date'])) {
  7324. return $data['captcha_blocked_until_date'];
  7325. }
  7326. return false;
  7327. }
  7328. /**
  7329. * Remove tags from HTML anf return the $in_number_char first non-HTML char
  7330. * Postfix the text with "..." if it has been truncated.
  7331. *
  7332. * @param string $text
  7333. * @param int $number
  7334. *
  7335. * @return string
  7336. *
  7337. * @author hubert borderiou
  7338. */
  7339. function api_get_short_text_from_html($text, $number)
  7340. {
  7341. // Delete script and style tags
  7342. $text = preg_replace('/(<(script|style)\b[^>]*>).*?(<\/\2>)/is', "$1$3", $text);
  7343. $text = api_html_entity_decode($text);
  7344. $out_res = api_remove_tags_with_space($text, false);
  7345. $postfix = "...";
  7346. if (strlen($out_res) > $number) {
  7347. $out_res = substr($out_res, 0, $number).$postfix;
  7348. }
  7349. return $out_res;
  7350. }
  7351. /**
  7352. * Replace tags with a space in a text.
  7353. * If $in_double_quote_replace, replace " with '' (for HTML attribute purpose, for exemple).
  7354. *
  7355. * @return string
  7356. *
  7357. * @author hubert borderiou
  7358. */
  7359. function api_remove_tags_with_space($in_html, $in_double_quote_replace = true)
  7360. {
  7361. $out_res = $in_html;
  7362. if ($in_double_quote_replace) {
  7363. $out_res = str_replace('"', "''", $out_res);
  7364. }
  7365. // avoid text stuck together when tags are removed, adding a space after >
  7366. $out_res = str_replace(">", "> ", $out_res);
  7367. $out_res = strip_tags($out_res);
  7368. return $out_res;
  7369. }
  7370. /**
  7371. * If true, the drh can access all content (courses, users) inside a session.
  7372. *
  7373. * @return bool
  7374. */
  7375. function api_drh_can_access_all_session_content()
  7376. {
  7377. $value = api_get_setting('drh_can_access_all_session_content');
  7378. return $value === 'true';
  7379. }
  7380. /**
  7381. * @param string $tool
  7382. * @param string $setting
  7383. * @param int $defaultValue
  7384. *
  7385. * @return string
  7386. */
  7387. function api_get_default_tool_setting($tool, $setting, $defaultValue)
  7388. {
  7389. global $_configuration;
  7390. if (isset($_configuration[$tool]) &&
  7391. isset($_configuration[$tool]['default_settings']) &&
  7392. isset($_configuration[$tool]['default_settings'][$setting])
  7393. ) {
  7394. return $_configuration[$tool]['default_settings'][$setting];
  7395. }
  7396. return $defaultValue;
  7397. }
  7398. /**
  7399. * Checks if user can login as another user.
  7400. *
  7401. * @param int $loginAsUserId the user id to log in
  7402. * @param int $userId my user id
  7403. *
  7404. * @return bool
  7405. */
  7406. function api_can_login_as($loginAsUserId, $userId = null)
  7407. {
  7408. if (empty($userId)) {
  7409. $userId = api_get_user_id();
  7410. }
  7411. if ($loginAsUserId == $userId) {
  7412. return false;
  7413. }
  7414. if (empty($loginAsUserId)) {
  7415. return false;
  7416. }
  7417. if ($loginAsUserId != strval(intval($loginAsUserId))) {
  7418. return false;
  7419. }
  7420. // Check if the user to login is an admin
  7421. if (api_is_platform_admin_by_id($loginAsUserId)) {
  7422. // Only super admins can login to admin accounts
  7423. if (!api_global_admin_can_edit_admin($loginAsUserId)) {
  7424. return false;
  7425. }
  7426. }
  7427. $userInfo = api_get_user_info($loginAsUserId);
  7428. $isDrh = function () use ($loginAsUserId) {
  7429. if (api_is_drh()) {
  7430. if (api_drh_can_access_all_session_content()) {
  7431. $users = SessionManager::getAllUsersFromCoursesFromAllSessionFromStatus(
  7432. 'drh_all',
  7433. api_get_user_id()
  7434. );
  7435. $userList = [];
  7436. if (is_array($users)) {
  7437. foreach ($users as $user) {
  7438. $userList[] = $user['user_id'];
  7439. }
  7440. }
  7441. if (in_array($loginAsUserId, $userList)) {
  7442. return true;
  7443. }
  7444. } else {
  7445. if (api_is_drh() &&
  7446. UserManager::is_user_followed_by_drh($loginAsUserId, api_get_user_id())
  7447. ) {
  7448. return true;
  7449. }
  7450. }
  7451. }
  7452. return false;
  7453. };
  7454. $loginAsStatusForSessionAdmins = [STUDENT];
  7455. if (api_get_configuration_value('allow_session_admin_login_as_teacher')) {
  7456. $loginAsStatusForSessionAdmins[] = COURSEMANAGER;
  7457. }
  7458. return api_is_platform_admin() ||
  7459. (api_is_session_admin() && in_array($userInfo['status'], $loginAsStatusForSessionAdmins)) ||
  7460. $isDrh();
  7461. }
  7462. /**
  7463. * @return bool
  7464. */
  7465. function api_is_allowed_in_course()
  7466. {
  7467. if (api_is_platform_admin()) {
  7468. return true;
  7469. }
  7470. return Session::read('is_allowed_in_course');
  7471. }
  7472. /**
  7473. * Set the cookie to go directly to the course code $in_firstpage
  7474. * after login.
  7475. *
  7476. * @param string $value is the course code of the course to go
  7477. */
  7478. function api_set_firstpage_parameter($value)
  7479. {
  7480. setcookie('GotoCourse', $value);
  7481. }
  7482. /**
  7483. * Delete the cookie to go directly to the course code $in_firstpage
  7484. * after login.
  7485. */
  7486. function api_delete_firstpage_parameter()
  7487. {
  7488. setcookie('GotoCourse', '', time() - 3600);
  7489. }
  7490. /**
  7491. * @return bool if course_code for direct course access after login is set
  7492. */
  7493. function exist_firstpage_parameter()
  7494. {
  7495. return isset($_COOKIE['GotoCourse']) && $_COOKIE['GotoCourse'] != '';
  7496. }
  7497. /**
  7498. * @return return the course_code of the course where user login
  7499. */
  7500. function api_get_firstpage_parameter()
  7501. {
  7502. return $_COOKIE['GotoCourse'];
  7503. }
  7504. /**
  7505. * Return true on https install.
  7506. *
  7507. * @return bool
  7508. */
  7509. function api_is_https()
  7510. {
  7511. if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
  7512. $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_configuration['force_https_forwarded_proto'])
  7513. ) {
  7514. $isSecured = true;
  7515. } else {
  7516. if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  7517. $isSecured = true;
  7518. } else {
  7519. $isSecured = false;
  7520. // last chance
  7521. if (!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) {
  7522. $isSecured = true;
  7523. }
  7524. }
  7525. }
  7526. return $isSecured;
  7527. }
  7528. /**
  7529. * Return protocol (http or https).
  7530. *
  7531. * @return string
  7532. */
  7533. function api_get_protocol()
  7534. {
  7535. return api_is_https() ? 'https' : 'http';
  7536. }
  7537. /**
  7538. * Return a string where " are replaced with 2 '
  7539. * It is useful when you pass a PHP variable in a Javascript browser dialog
  7540. * e.g. : alert("<?php get_lang('Message') ?>");
  7541. * and message contains character ".
  7542. *
  7543. * @param string $in_text
  7544. *
  7545. * @return string
  7546. */
  7547. function convert_double_quote_to_single($in_text)
  7548. {
  7549. return api_preg_replace('/"/', "''", $in_text);
  7550. }
  7551. /**
  7552. * Get origin.
  7553. *
  7554. * @param string
  7555. *
  7556. * @return string
  7557. */
  7558. function api_get_origin()
  7559. {
  7560. $origin = isset($_REQUEST['origin']) ? Security::remove_XSS($_REQUEST['origin']) : '';
  7561. return $origin;
  7562. }
  7563. /**
  7564. * Warns an user that the portal reach certain limit.
  7565. *
  7566. * @param string $limitName
  7567. */
  7568. function api_warn_hosting_contact($limitName)
  7569. {
  7570. $hostingParams = api_get_configuration_value(1);
  7571. $email = null;
  7572. if (!empty($hostingParams)) {
  7573. if (isset($hostingParams['hosting_contact_mail'])) {
  7574. $email = $hostingParams['hosting_contact_mail'];
  7575. }
  7576. }
  7577. if (!empty($email)) {
  7578. $subject = get_lang('HostingWarningReached');
  7579. $body = get_lang('PortalName').': '.api_get_path(WEB_PATH)." \n ";
  7580. $body .= get_lang('PortalLimitType').': '.$limitName." \n ";
  7581. if (isset($hostingParams[$limitName])) {
  7582. $body .= get_lang('Value').': '.$hostingParams[$limitName];
  7583. }
  7584. api_mail_html(null, $email, $subject, $body);
  7585. }
  7586. }
  7587. /**
  7588. * Gets value of a variable from app/config/configuration.php
  7589. * Variables that are not set in the configuration.php file but set elsewhere:
  7590. * - virtual_css_theme_folder (vchamilo plugin)
  7591. * - access_url (global.inc.php)
  7592. * - apc/apc_prefix (global.inc.php).
  7593. *
  7594. * @param string $variable
  7595. *
  7596. * @return bool|mixed
  7597. */
  7598. function api_get_configuration_value($variable)
  7599. {
  7600. global $_configuration;
  7601. // Check the current url id, id = 1 by default
  7602. $urlId = isset($_configuration['access_url']) ? (int) $_configuration['access_url'] : 1;
  7603. $variable = trim($variable);
  7604. // Check if variable exists
  7605. if (isset($_configuration[$variable])) {
  7606. if (is_array($_configuration[$variable])) {
  7607. // Check if it exists for the sub portal
  7608. if (array_key_exists($urlId, $_configuration[$variable])) {
  7609. return $_configuration[$variable][$urlId];
  7610. } else {
  7611. // Try to found element with id = 1 (master portal)
  7612. if (array_key_exists(1, $_configuration[$variable])) {
  7613. return $_configuration[$variable][1];
  7614. }
  7615. }
  7616. }
  7617. return $_configuration[$variable];
  7618. }
  7619. return false;
  7620. }
  7621. /**
  7622. * Returns supported image extensions in the portal.
  7623. *
  7624. * @param bool $supportVectors Whether vector images should also be accepted or not
  7625. *
  7626. * @return array Supported image extensions in the portal
  7627. */
  7628. function api_get_supported_image_extensions($supportVectors = true)
  7629. {
  7630. // jpg can also be called jpeg, jpe, jfif and jif. See https://en.wikipedia.org/wiki/JPEG#JPEG_filename_extensions
  7631. $supportedImageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'jpe', 'jfif', 'jif'];
  7632. if ($supportVectors) {
  7633. array_push($supportedImageExtensions, 'svg');
  7634. }
  7635. if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
  7636. array_push($supportedImageExtensions, 'webp');
  7637. }
  7638. return $supportedImageExtensions;
  7639. }
  7640. /**
  7641. * This setting changes the registration status for the campus.
  7642. *
  7643. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  7644. *
  7645. * @version August 2006
  7646. *
  7647. * @param bool $listCampus Whether we authorize
  7648. *
  7649. * @todo the $_settings should be reloaded here. => write api function for this and use this in global.inc.php also.
  7650. */
  7651. function api_register_campus($listCampus = true)
  7652. {
  7653. $tbl_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  7654. $sql = "UPDATE $tbl_settings SET selected_value='true' WHERE variable='registered'";
  7655. Database::query($sql);
  7656. if (!$listCampus) {
  7657. $sql = "UPDATE $tbl_settings SET selected_value='true' WHERE variable='donotlistcampus'";
  7658. Database::query($sql);
  7659. }
  7660. }
  7661. /**
  7662. * Checks whether current user is a student boss.
  7663. *
  7664. * @global array $_user
  7665. *
  7666. * @return bool
  7667. */
  7668. function api_is_student_boss()
  7669. {
  7670. $_user = api_get_user_info();
  7671. return isset($_user['status']) && $_user['status'] == STUDENT_BOSS;
  7672. }
  7673. /**
  7674. * Check whether the user type should be exclude.
  7675. * Such as invited or anonymous users.
  7676. *
  7677. * @param bool $checkDB Optional. Whether check the user status
  7678. * @param int $userId Options. The user id
  7679. *
  7680. * @return bool
  7681. */
  7682. function api_is_excluded_user_type($checkDB = false, $userId = 0)
  7683. {
  7684. if ($checkDB) {
  7685. $userId = empty($userId) ? api_get_user_id() : (int) $userId;
  7686. if ($userId == 0) {
  7687. return true;
  7688. }
  7689. $userInfo = api_get_user_info($userId);
  7690. switch ($userInfo['status']) {
  7691. case INVITEE:
  7692. case ANONYMOUS:
  7693. return true;
  7694. default:
  7695. return false;
  7696. }
  7697. }
  7698. $isInvited = api_is_invitee();
  7699. $isAnonymous = api_is_anonymous();
  7700. if ($isInvited || $isAnonymous) {
  7701. return true;
  7702. }
  7703. return false;
  7704. }
  7705. /**
  7706. * Get the user status to ignore in reports.
  7707. *
  7708. * @param string $format Optional. The result type (array or string)
  7709. *
  7710. * @return array|string
  7711. */
  7712. function api_get_users_status_ignored_in_reports($format = 'array')
  7713. {
  7714. $excludedTypes = [
  7715. INVITEE,
  7716. ANONYMOUS,
  7717. ];
  7718. if ($format == 'string') {
  7719. return implode(', ', $excludedTypes);
  7720. }
  7721. return $excludedTypes;
  7722. }
  7723. /**
  7724. * Set the Site Use Cookie Warning for 1 year.
  7725. */
  7726. function api_set_site_use_cookie_warning_cookie()
  7727. {
  7728. setcookie('ChamiloUsesCookies', 'ok', time() + 31556926);
  7729. }
  7730. /**
  7731. * Return true if the Site Use Cookie Warning Cookie warning exists.
  7732. *
  7733. * @return bool
  7734. */
  7735. function api_site_use_cookie_warning_cookie_exist()
  7736. {
  7737. return isset($_COOKIE['ChamiloUsesCookies']);
  7738. }
  7739. /**
  7740. * Given a number of seconds, format the time to show hours, minutes and seconds.
  7741. *
  7742. * @param int $time The time in seconds
  7743. * @param string $originFormat Optional. PHP o JS
  7744. *
  7745. * @return string (00h00'00")
  7746. */
  7747. function api_format_time($time, $originFormat = 'php')
  7748. {
  7749. $h = get_lang('h');
  7750. $hours = $time / 3600;
  7751. $mins = ($time % 3600) / 60;
  7752. $secs = ($time % 60);
  7753. if ($time < 0) {
  7754. $hours = 0;
  7755. $mins = 0;
  7756. $secs = 0;
  7757. }
  7758. if ($originFormat == 'js') {
  7759. $formattedTime = trim(sprintf("%02d : %02d : %02d", $hours, $mins, $secs));
  7760. } else {
  7761. $formattedTime = trim(sprintf("%02d$h%02d'%02d\"", $hours, $mins, $secs));
  7762. }
  7763. return $formattedTime;
  7764. }
  7765. /**
  7766. * Create a new empty directory with index.html file.
  7767. *
  7768. * @param string $name The new directory name
  7769. * @param string $parentDirectory Directory parent directory name
  7770. *
  7771. * @return bool Return true if the directory was create. Otherwise return false
  7772. */
  7773. function api_create_protected_dir($name, $parentDirectory)
  7774. {
  7775. $isCreated = false;
  7776. if (!is_writable($parentDirectory)) {
  7777. return false;
  7778. }
  7779. $fullPath = $parentDirectory.api_replace_dangerous_char($name);
  7780. if (mkdir($fullPath, api_get_permissions_for_new_directories(), true)) {
  7781. $fp = fopen($fullPath.'/index.html', 'w');
  7782. if ($fp) {
  7783. if (fwrite($fp, '<html><head></head><body></body></html>')) {
  7784. $isCreated = true;
  7785. }
  7786. }
  7787. fclose($fp);
  7788. }
  7789. return $isCreated;
  7790. }
  7791. /**
  7792. * Sends an HTML email using the phpmailer class (and multipart/alternative to downgrade gracefully)
  7793. * Sender name and email can be specified, if not specified
  7794. * name and email of the platform admin are used.
  7795. *
  7796. * @author Bert Vanderkimpen ICT&O UGent
  7797. * @author Yannick Warnier <yannick.warnier@beeznest.com>
  7798. *
  7799. * @param string name of recipient
  7800. * @param string email of recipient
  7801. * @param string email subject
  7802. * @param string email body
  7803. * @param string sender name
  7804. * @param string sender e-mail
  7805. * @param array extra headers in form $headers = array($name => $value) to allow parsing
  7806. * @param array data file (path and filename)
  7807. * @param bool True for attaching a embedded file inside content html (optional)
  7808. * @param array Additional parameters
  7809. *
  7810. * @return int true if mail was sent
  7811. *
  7812. * @see class.phpmailer.php
  7813. */
  7814. function api_mail_html(
  7815. $recipient_name,
  7816. $recipient_email,
  7817. $subject,
  7818. $message,
  7819. $senderName = '',
  7820. $senderEmail = '',
  7821. $extra_headers = [],
  7822. $data_file = [],
  7823. $embedded_image = false,
  7824. $additionalParameters = []
  7825. ) {
  7826. global $platform_email;
  7827. $mail = new PHPMailer();
  7828. $mail->Mailer = $platform_email['SMTP_MAILER'];
  7829. $mail->Host = $platform_email['SMTP_HOST'];
  7830. $mail->Port = $platform_email['SMTP_PORT'];
  7831. $mail->CharSet = isset($platform_email['SMTP_CHARSET']) ? $platform_email['SMTP_CHARSET'] : 'UTF-8';
  7832. // Stay far below SMTP protocol 980 chars limit.
  7833. $mail->WordWrap = 200;
  7834. if ($platform_email['SMTP_AUTH']) {
  7835. $mail->SMTPAuth = 1;
  7836. $mail->Username = $platform_email['SMTP_USER'];
  7837. $mail->Password = $platform_email['SMTP_PASS'];
  7838. if (isset($platform_email['SMTP_SECURE'])) {
  7839. $mail->SMTPSecure = $platform_email['SMTP_SECURE'];
  7840. }
  7841. }
  7842. $mail->SMTPDebug = isset($platform_email['SMTP_DEBUG']) ? $platform_email['SMTP_DEBUG'] : 0;
  7843. // 5 = low, 1 = high
  7844. $mail->Priority = 3;
  7845. $mail->SMTPKeepAlive = true;
  7846. api_set_noreply_and_from_address_to_mailer(
  7847. $mail,
  7848. ['name' => $senderName, 'email' => $senderEmail],
  7849. !empty($extra_headers['reply_to']) ? $extra_headers['reply_to'] : []
  7850. );
  7851. unset($extra_headers['reply_to']);
  7852. $mail->Subject = $subject;
  7853. $mail->AltBody = strip_tags(
  7854. str_replace('<br />', "\n", api_html_entity_decode($message))
  7855. );
  7856. $list = api_get_configuration_value('send_all_emails_to');
  7857. if (!empty($list) && isset($list['emails'])) {
  7858. foreach ($list['emails'] as $email) {
  7859. //$mail->AddBCC($email);
  7860. $mail->AddAddress($email);
  7861. }
  7862. }
  7863. // Send embedded image.
  7864. if ($embedded_image) {
  7865. // Get all images html inside content.
  7866. preg_match_all("/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i", $message, $m);
  7867. // Prepare new tag images.
  7868. $new_images_html = [];
  7869. $i = 1;
  7870. if (!empty($m[1])) {
  7871. foreach ($m[1] as $image_path) {
  7872. $real_path = realpath($image_path);
  7873. $filename = basename($image_path);
  7874. $image_cid = $filename.'_'.$i;
  7875. $encoding = 'base64';
  7876. $image_type = mime_content_type($real_path);
  7877. $mail->AddEmbeddedImage(
  7878. $real_path,
  7879. $image_cid,
  7880. $filename,
  7881. $encoding,
  7882. $image_type
  7883. );
  7884. $new_images_html[] = '<img src="cid:'.$image_cid.'" />';
  7885. $i++;
  7886. }
  7887. }
  7888. // Replace origin image for new embedded image html.
  7889. $x = 0;
  7890. if (!empty($m[0])) {
  7891. foreach ($m[0] as $orig_img) {
  7892. $message = str_replace($orig_img, $new_images_html[$x], $message);
  7893. $x++;
  7894. }
  7895. }
  7896. }
  7897. $mailView = new Template(null, false, false, false, false, false, false);
  7898. $noReply = api_get_setting('noreply_email_address');
  7899. if (!empty($noReply)) {
  7900. $message .= "<br />".get_lang('ThisIsAutomaticEmailNoReply');
  7901. }
  7902. $mailView->assign('content', $message);
  7903. if (isset($additionalParameters['link'])) {
  7904. $mailView->assign('link', $additionalParameters['link']);
  7905. }
  7906. $mailView->assign('mail_header_style', api_get_configuration_value('mail_header_style'));
  7907. $mailView->assign('mail_content_style', api_get_configuration_value('mail_content_style'));
  7908. $layout = $mailView->get_template('mail/mail.tpl');
  7909. $mail->Body = $mailView->fetch($layout);
  7910. // Attachment ...
  7911. if (!empty($data_file)) {
  7912. foreach ($data_file as $file_attach) {
  7913. if (!empty($file_attach['path']) && !empty($file_attach['filename'])) {
  7914. $mail->AddAttachment($file_attach['path'], $file_attach['filename']);
  7915. }
  7916. }
  7917. }
  7918. // Only valid addresses are accepted.
  7919. if (is_array($recipient_email)) {
  7920. foreach ($recipient_email as $dest) {
  7921. if (api_valid_email($dest)) {
  7922. $mail->AddAddress($dest, $recipient_name);
  7923. }
  7924. }
  7925. } else {
  7926. if (api_valid_email($recipient_email)) {
  7927. $mail->AddAddress($recipient_email, $recipient_name);
  7928. } else {
  7929. return 0;
  7930. }
  7931. }
  7932. if (is_array($extra_headers) && count($extra_headers) > 0) {
  7933. foreach ($extra_headers as $key => $value) {
  7934. switch (strtolower($key)) {
  7935. case 'encoding':
  7936. case 'content-transfer-encoding':
  7937. $mail->Encoding = $value;
  7938. break;
  7939. case 'charset':
  7940. $mail->Charset = $value;
  7941. break;
  7942. case 'contenttype':
  7943. case 'content-type':
  7944. $mail->ContentType = $value;
  7945. break;
  7946. default:
  7947. $mail->AddCustomHeader($key.':'.$value);
  7948. break;
  7949. }
  7950. }
  7951. } else {
  7952. if (!empty($extra_headers)) {
  7953. $mail->AddCustomHeader($extra_headers);
  7954. }
  7955. }
  7956. // WordWrap the html body (phpMailer only fixes AltBody) FS#2988
  7957. $mail->Body = $mail->WrapText($mail->Body, $mail->WordWrap);
  7958. // Send the mail message.
  7959. if (!$mail->Send()) {
  7960. error_log('ERROR: mail not sent to '.$recipient_name.' ('.$recipient_email.') because of '.$mail->ErrorInfo.'<br />');
  7961. if ($mail->SMTPDebug) {
  7962. error_log(
  7963. "Connection details :: ".
  7964. "Protocol: ".$mail->Mailer.' :: '.
  7965. "Host/Port: ".$mail->Host.':'.$mail->Port.' :: '.
  7966. "Authent/Open: ".($mail->SMTPAuth ? 'Authent' : 'Open').' :: '.
  7967. ($mail->SMTPAuth ? " User/Pass: ".$mail->Username.':'.$mail->Password : '').' :: '.
  7968. "Sender: ".$mail->Sender
  7969. );
  7970. }
  7971. return 0;
  7972. }
  7973. if (!empty($additionalParameters)) {
  7974. $plugin = new AppPlugin();
  7975. $smsPlugin = $plugin->getSMSPluginLibrary();
  7976. if ($smsPlugin) {
  7977. $smsPlugin->send($additionalParameters);
  7978. }
  7979. }
  7980. // Clear all the addresses.
  7981. $mail->ClearAddresses();
  7982. // Clear all attachments
  7983. $mail->ClearAttachments();
  7984. return 1;
  7985. }
  7986. /**
  7987. * @param string $tool Possible values: GroupManager::GROUP_TOOL_*
  7988. * @param bool $showHeader
  7989. */
  7990. function api_protect_course_group($tool, $showHeader = true)
  7991. {
  7992. $groupId = api_get_group_id();
  7993. if (!empty($groupId)) {
  7994. $userId = api_get_user_id();
  7995. $groupInfo = GroupManager::get_group_properties($groupId);
  7996. // Group doesn't exists
  7997. if (empty($groupInfo)) {
  7998. api_not_allowed($showHeader);
  7999. }
  8000. // Check group access
  8001. $allow = GroupManager::user_has_access(
  8002. $userId,
  8003. $groupInfo['iid'],
  8004. $tool
  8005. );
  8006. if (!$allow) {
  8007. api_not_allowed($showHeader);
  8008. }
  8009. }
  8010. }
  8011. /**
  8012. * Check if a date is in a date range.
  8013. *
  8014. * @param datetime $startDate
  8015. * @param datetime $endDate
  8016. * @param datetime $currentDate
  8017. *
  8018. * @return bool true if date is in rage, false otherwise
  8019. */
  8020. function api_is_date_in_date_range($startDate, $endDate, $currentDate = null)
  8021. {
  8022. $startDate = strtotime(api_get_local_time($startDate));
  8023. $endDate = strtotime(api_get_local_time($endDate));
  8024. $currentDate = strtotime(api_get_local_time($currentDate));
  8025. if ($currentDate >= $startDate && $currentDate <= $endDate) {
  8026. return true;
  8027. }
  8028. return false;
  8029. }
  8030. /**
  8031. * Eliminate the duplicates of a multidimensional array by sending the key.
  8032. *
  8033. * @param array $array multidimensional array
  8034. * @param int $key key to find to compare
  8035. *
  8036. * @return array
  8037. */
  8038. function api_unique_multidim_array($array, $key)
  8039. {
  8040. $temp_array = [];
  8041. $i = 0;
  8042. $key_array = [];
  8043. foreach ($array as $val) {
  8044. if (!in_array($val[$key], $key_array)) {
  8045. $key_array[$i] = $val[$key];
  8046. $temp_array[$i] = $val;
  8047. }
  8048. $i++;
  8049. }
  8050. return $temp_array;
  8051. }
  8052. /**
  8053. * Limit the access to Session Admins when the limit_session_admin_role
  8054. * configuration variable is set to true.
  8055. */
  8056. function api_protect_limit_for_session_admin()
  8057. {
  8058. $limitAdmin = api_get_setting('limit_session_admin_role');
  8059. if (api_is_session_admin() && $limitAdmin === 'true') {
  8060. api_not_allowed(true);
  8061. }
  8062. }
  8063. /**
  8064. * Limits that a session admin has access to list users.
  8065. * When limit_session_admin_list_users configuration variable is set to true.
  8066. */
  8067. function api_protect_session_admin_list_users()
  8068. {
  8069. $limitAdmin = api_get_configuration_value('limit_session_admin_list_users');
  8070. if (api_is_session_admin() && true === $limitAdmin) {
  8071. api_not_allowed(true);
  8072. }
  8073. }
  8074. /**
  8075. * @return bool
  8076. */
  8077. function api_is_student_view_active()
  8078. {
  8079. $studentView = Session::read('studentview');
  8080. return $studentView == 'studentview';
  8081. }
  8082. /**
  8083. * Adds a file inside the upload/$type/id.
  8084. *
  8085. * @param string $type
  8086. * @param array $file
  8087. * @param int $itemId
  8088. * @param string $cropParameters
  8089. *
  8090. * @return array|bool
  8091. */
  8092. function api_upload_file($type, $file, $itemId, $cropParameters = '')
  8093. {
  8094. $upload = process_uploaded_file($file);
  8095. if ($upload) {
  8096. $name = api_replace_dangerous_char($file['name']);
  8097. // No "dangerous" files
  8098. $name = disable_dangerous_file($name);
  8099. $pathId = '/'.substr((string) $itemId, 0, 1).'/'.$itemId.'/';
  8100. $path = api_get_path(SYS_UPLOAD_PATH).$type.$pathId;
  8101. if (!is_dir($path)) {
  8102. mkdir($path, api_get_permissions_for_new_directories(), true);
  8103. }
  8104. $pathToSave = $path.$name;
  8105. $result = moveUploadedFile($file, $pathToSave);
  8106. if ($result) {
  8107. if (!empty($cropParameters)) {
  8108. $image = new Image($pathToSave);
  8109. $image->crop($cropParameters);
  8110. }
  8111. return ['path_to_save' => $pathId.$name];
  8112. }
  8113. return false;
  8114. }
  8115. }
  8116. /**
  8117. * @param string $type
  8118. * @param int $itemId
  8119. * @param string $file
  8120. *
  8121. * @return bool
  8122. */
  8123. function api_get_uploaded_web_url($type, $itemId, $file)
  8124. {
  8125. return api_get_uploaded_file($type, $itemId, $file, true);
  8126. }
  8127. /**
  8128. * @param string $type
  8129. * @param int $itemId
  8130. * @param string $file
  8131. * @param bool $getUrl
  8132. *
  8133. * @return bool
  8134. */
  8135. function api_get_uploaded_file($type, $itemId, $file, $getUrl = false)
  8136. {
  8137. $itemId = (int) $itemId;
  8138. $pathId = '/'.substr((string) $itemId, 0, 1).'/'.$itemId.'/';
  8139. $path = api_get_path(SYS_UPLOAD_PATH).$type.$pathId;
  8140. $file = basename($file);
  8141. $file = $path.'/'.$file;
  8142. if (Security::check_abs_path($file, $path) && is_file($file) && file_exists($file)) {
  8143. if ($getUrl) {
  8144. return str_replace(api_get_path(SYS_UPLOAD_PATH), api_get_path(WEB_UPLOAD_PATH), $file);
  8145. }
  8146. return $file;
  8147. }
  8148. return false;
  8149. }
  8150. /**
  8151. * @param string $type
  8152. * @param int $itemId
  8153. * @param string $file
  8154. * @param string $title
  8155. */
  8156. function api_download_uploaded_file($type, $itemId, $file, $title = '')
  8157. {
  8158. $file = api_get_uploaded_file($type, $itemId, $file);
  8159. if ($file) {
  8160. if (Security::check_abs_path($file, api_get_path(SYS_UPLOAD_PATH).$type)) {
  8161. DocumentManager::file_send_for_download($file, true, $title);
  8162. exit;
  8163. }
  8164. }
  8165. api_not_allowed(true);
  8166. }
  8167. /**
  8168. * @param string $type
  8169. * @param string $file
  8170. */
  8171. function api_remove_uploaded_file($type, $file)
  8172. {
  8173. $typePath = api_get_path(SYS_UPLOAD_PATH).$type;
  8174. $path = $typePath.'/'.$file;
  8175. if (Security::check_abs_path($path, $typePath) && file_exists($path) && is_file($path)) {
  8176. unlink($path);
  8177. }
  8178. }
  8179. /**
  8180. * @param string $type
  8181. * @param int $itemId
  8182. * @param string $file
  8183. *
  8184. * @return bool
  8185. */
  8186. function api_remove_uploaded_file_by_id($type, $itemId, $file)
  8187. {
  8188. $file = api_get_uploaded_file($type, $itemId, $file, false);
  8189. $typePath = api_get_path(SYS_UPLOAD_PATH).$type;
  8190. if (Security::check_abs_path($file, $typePath) && file_exists($file) && is_file($file)) {
  8191. unlink($file);
  8192. return true;
  8193. }
  8194. return false;
  8195. }
  8196. /**
  8197. * Converts string value to float value.
  8198. *
  8199. * 3.141516 => 3.141516
  8200. * 3,141516 => 3.141516
  8201. *
  8202. * @todo WIP
  8203. *
  8204. * @param string $number
  8205. *
  8206. * @return float
  8207. */
  8208. function api_float_val($number)
  8209. {
  8210. $number = (float) str_replace(',', '.', trim($number));
  8211. return $number;
  8212. }
  8213. /**
  8214. * Converts float values
  8215. * Example if $decimals = 2.
  8216. *
  8217. * 3.141516 => 3.14
  8218. * 3,141516 => 3,14
  8219. *
  8220. * @param string $number number in iso code
  8221. * @param int $decimals
  8222. * @param string $decimalSeparator
  8223. * @param string $thousandSeparator
  8224. *
  8225. * @return bool|string
  8226. */
  8227. function api_number_format($number, $decimals = 0, $decimalSeparator = '.', $thousandSeparator = ',')
  8228. {
  8229. $number = api_float_val($number);
  8230. return number_format($number, $decimals, $decimalSeparator, $thousandSeparator);
  8231. }
  8232. /**
  8233. * Set location url with a exit break by default.
  8234. *
  8235. * @param $url
  8236. * @param bool $exit
  8237. */
  8238. function location($url, $exit = true)
  8239. {
  8240. header('Location: '.$url);
  8241. if ($exit) {
  8242. exit;
  8243. }
  8244. }
  8245. /**
  8246. * @return string
  8247. */
  8248. function api_get_web_url()
  8249. {
  8250. if (api_get_setting('server_type') === 'test') {
  8251. return api_get_path(WEB_PATH).'web/app_dev.php/';
  8252. } else {
  8253. return api_get_path(WEB_PATH).'web/';
  8254. }
  8255. }
  8256. /**
  8257. * @param string $from
  8258. * @param string $to
  8259. *
  8260. * @return string
  8261. */
  8262. function api_get_relative_path($from, $to)
  8263. {
  8264. // some compatibility fixes for Windows paths
  8265. $from = is_dir($from) ? rtrim($from, '\/').'/' : $from;
  8266. $to = is_dir($to) ? rtrim($to, '\/').'/' : $to;
  8267. $from = str_replace('\\', '/', $from);
  8268. $to = str_replace('\\', '/', $to);
  8269. $from = explode('/', $from);
  8270. $to = explode('/', $to);
  8271. $relPath = $to;
  8272. foreach ($from as $depth => $dir) {
  8273. // find first non-matching dir
  8274. if ($dir === $to[$depth]) {
  8275. // ignore this directory
  8276. array_shift($relPath);
  8277. } else {
  8278. // get number of remaining dirs to $from
  8279. $remaining = count($from) - $depth;
  8280. if ($remaining > 1) {
  8281. // add traversals up to first matching dir
  8282. $padLength = (count($relPath) + $remaining - 1) * -1;
  8283. $relPath = array_pad($relPath, $padLength, '..');
  8284. break;
  8285. } else {
  8286. $relPath[0] = './'.$relPath[0];
  8287. }
  8288. }
  8289. }
  8290. return implode('/', $relPath);
  8291. }
  8292. /**
  8293. * Unserialize content using Brummann\Polyfill\Unserialize.
  8294. *
  8295. * @param string $type
  8296. * @param string $serialized
  8297. * @param bool $ignoreErrors. Optional.
  8298. *
  8299. * @return mixed
  8300. */
  8301. function api_unserialize_content($type, $serialized, $ignoreErrors = false)
  8302. {
  8303. switch ($type) {
  8304. case 'career':
  8305. case 'sequence_graph':
  8306. $allowedClasses = [Graph::class, VerticesMap::class, Vertices::class, Edges::class];
  8307. break;
  8308. case 'lp':
  8309. $allowedClasses = [
  8310. learnpath::class,
  8311. learnpathItem::class,
  8312. aicc::class,
  8313. aiccBlock::class,
  8314. aiccItem::class,
  8315. aiccObjective::class,
  8316. aiccResource::class,
  8317. scorm::class,
  8318. scormItem::class,
  8319. scormMetadata::class,
  8320. scormOrganization::class,
  8321. scormResource::class,
  8322. Link::class,
  8323. LpItem::class,
  8324. ];
  8325. break;
  8326. case 'course':
  8327. $allowedClasses = [
  8328. Course::class,
  8329. Announcement::class,
  8330. Attendance::class,
  8331. CalendarEvent::class,
  8332. CourseCopyLearnpath::class,
  8333. CourseCopyTestCategory::class,
  8334. CourseDescription::class,
  8335. CourseSession::class,
  8336. Document::class,
  8337. Forum::class,
  8338. ForumCategory::class,
  8339. ForumPost::class,
  8340. ForumTopic::class,
  8341. Glossary::class,
  8342. GradeBookBackup::class,
  8343. Link::class,
  8344. LinkCategory::class,
  8345. Quiz::class,
  8346. QuizQuestion::class,
  8347. QuizQuestionOption::class,
  8348. ScormDocument::class,
  8349. Survey::class,
  8350. SurveyInvitation::class,
  8351. SurveyQuestion::class,
  8352. Thematic::class,
  8353. ToolIntro::class,
  8354. Wiki::class,
  8355. Work::class,
  8356. stdClass::class,
  8357. ];
  8358. break;
  8359. case 'not_allowed_classes':
  8360. default:
  8361. $allowedClasses = false;
  8362. }
  8363. if ($ignoreErrors) {
  8364. return @Unserialize::unserialize(
  8365. $serialized,
  8366. ['allowed_classes' => $allowedClasses]
  8367. );
  8368. }
  8369. return Unserialize::unserialize(
  8370. $serialized,
  8371. ['allowed_classes' => $allowedClasses]
  8372. );
  8373. }
  8374. /**
  8375. * Set the From and ReplyTo properties to PHPMailer instance.
  8376. *
  8377. * @param PHPMailer $mailer
  8378. * @param array $sender
  8379. * @param array $replyToAddress
  8380. *
  8381. * @throws phpmailerException
  8382. */
  8383. function api_set_noreply_and_from_address_to_mailer(PHPMailer $mailer, array $sender, array $replyToAddress = [])
  8384. {
  8385. $platformEmail = $GLOBALS['platform_email'];
  8386. $noReplyAddress = api_get_setting('noreply_email_address');
  8387. $avoidReplyToAddress = false;
  8388. if (!empty($noReplyAddress)) {
  8389. $avoidReplyToAddress = api_get_configuration_value('mail_no_reply_avoid_reply_to');
  8390. }
  8391. $notification = new Notification();
  8392. // If the parameter is set don't use the admin.
  8393. $senderName = !empty($sender['name']) ? $sender['name'] : $notification->getDefaultPlatformSenderName();
  8394. $senderEmail = !empty($sender['email']) ? $sender['email'] : $notification->getDefaultPlatformSenderEmail();
  8395. // Reply to first
  8396. if (!$avoidReplyToAddress) {
  8397. $mailer->AddCustomHeader('Errors-To: '.$notification->getDefaultPlatformSenderEmail());
  8398. if (
  8399. !empty($replyToAddress) &&
  8400. isset($platformEmail['SMTP_UNIQUE_REPLY_TO']) && $platformEmail['SMTP_UNIQUE_REPLY_TO'] &&
  8401. PHPMailer::ValidateAddress($replyToAddress['mail'])
  8402. ) {
  8403. $mailer->AddReplyTo($replyToAddress['email'], $replyToAddress['name']);
  8404. // Errors to sender
  8405. $mailer->AddCustomHeader('Errors-To: '.$replyToAddress['mail']);
  8406. $mailer->Sender = $replyToAddress['mail'];
  8407. }
  8408. }
  8409. //If the SMTP configuration only accept one sender
  8410. if (
  8411. isset($platformEmail['SMTP_UNIQUE_SENDER']) &&
  8412. $platformEmail['SMTP_UNIQUE_SENDER']
  8413. ) {
  8414. $senderName = $platformEmail['SMTP_FROM_NAME'];
  8415. $senderEmail = $platformEmail['SMTP_FROM_EMAIL'];
  8416. if (PHPMailer::ValidateAddress($senderEmail)) {
  8417. //force-set Sender to $senderEmail, otherwise SetFrom only does it if it is currently empty
  8418. $mailer->Sender = $senderEmail;
  8419. }
  8420. }
  8421. $mailer->SetFrom($senderEmail, $senderName, !$avoidReplyToAddress);
  8422. }
  8423. /**
  8424. * @param string $template
  8425. *
  8426. * @return string
  8427. */
  8428. function api_find_template($template)
  8429. {
  8430. return Template::findTemplateFilePath($template);
  8431. }
  8432. /**
  8433. * @return array
  8434. */
  8435. function api_get_language_list_for_flag()
  8436. {
  8437. $table = Database::get_main_table(TABLE_MAIN_LANGUAGE);
  8438. $sql = "SELECT english_name, isocode FROM $table
  8439. ORDER BY original_name ASC";
  8440. static $languages = [];
  8441. if (empty($languages)) {
  8442. $result = Database::query($sql);
  8443. while ($row = Database::fetch_array($result)) {
  8444. $languages[$row['english_name']] = $row['isocode'];
  8445. }
  8446. $languages['english'] = 'gb';
  8447. }
  8448. return $languages;
  8449. }
  8450. /**
  8451. * @return string
  8452. */
  8453. function api_get_language_translate_html()
  8454. {
  8455. $translate = api_get_configuration_value('translate_html');
  8456. if (!$translate) {
  8457. return '';
  8458. }
  8459. $languageList = api_get_languages();
  8460. $hideAll = '';
  8461. foreach ($languageList['all'] as $language) {
  8462. $hideAll .= '
  8463. $("span:lang('.$language['isocode'].')").filter(
  8464. function(e, val) {
  8465. // Only find the spans if they have set the lang
  8466. if ($(this).attr("lang") == null) {
  8467. return false;
  8468. }
  8469. // Ignore ckeditor classes
  8470. return !this.className.match(/cke(.*)/);
  8471. }).hide();'."\n";
  8472. }
  8473. $userInfo = api_get_user_info();
  8474. $languageId = api_get_language_id($userInfo['language']);
  8475. $languageInfo = api_get_language_info($languageId);
  8476. $isoCode = 'en';
  8477. if (!empty($languageInfo)) {
  8478. $isoCode = $languageInfo['isocode'];
  8479. }
  8480. return '
  8481. $(function() {
  8482. '.$hideAll.'
  8483. var defaultLanguageFromUser = "'.$isoCode.'";
  8484. $("span:lang('.$isoCode.')").filter(
  8485. function() {
  8486. // Ignore ckeditor classes
  8487. return !this.className.match(/cke(.*)/);
  8488. }).show();
  8489. var defaultLanguage = "";
  8490. var langFromUserFound = false;
  8491. $(this).find("span").filter(
  8492. function() {
  8493. // Ignore ckeditor classes
  8494. return !this.className.match(/cke(.*)/);
  8495. }).each(function() {
  8496. defaultLanguage = $(this).attr("lang");
  8497. if (defaultLanguage) {
  8498. $(this).before().next("br").remove();
  8499. if (defaultLanguageFromUser == defaultLanguage) {
  8500. langFromUserFound = true;
  8501. }
  8502. }
  8503. });
  8504. // Show default language
  8505. if (langFromUserFound == false && defaultLanguage) {
  8506. $("span:lang("+defaultLanguage+")").filter(
  8507. function() {
  8508. // Ignore ckeditor classes
  8509. return !this.className.match(/cke(.*)/);
  8510. }).show();
  8511. }
  8512. });
  8513. ';
  8514. }