install.lib.php 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\CoreBundle\Entity\AccessUrl;
  4. use Chamilo\CoreBundle\Entity\ExtraField;
  5. use Chamilo\CoreBundle\Framework\Container;
  6. use Chamilo\TicketBundle\Entity\Category as TicketCategory;
  7. use Chamilo\TicketBundle\Entity\Priority as TicketPriority;
  8. use Chamilo\TicketBundle\Entity\Project as TicketProject;
  9. use Doctrine\ORM\EntityManager;
  10. use Sonata\PageBundle\Entity\PageManager;
  11. use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
  12. /**
  13. * Chamilo LMS
  14. * This file contains functions used by the install and upgrade scripts.
  15. *
  16. * Ideas for future additions:
  17. * - a function get_old_version_settings to retrieve the config file settings
  18. * of older versions before upgrading.
  19. */
  20. /* CONSTANTS */
  21. define('SYSTEM_CONFIG_FILENAME', 'configuration.dist.php');
  22. /**
  23. * This function detects whether the system has been already installed.
  24. * It should be used for prevention from second running the installation
  25. * script and as a result - destroying a production system.
  26. *
  27. * @return bool The detected result;
  28. *
  29. * @author Ivan Tcholakov, 2010;
  30. */
  31. function isAlreadyInstalledSystem()
  32. {
  33. global $new_version, $_configuration;
  34. if (empty($new_version)) {
  35. return true; // Must be initialized.
  36. }
  37. $current_config_file = api_get_path(CONFIGURATION_PATH).'configuration.php';
  38. if (!file_exists($current_config_file)) {
  39. return false; // Configuration file does not exist, install the system.
  40. }
  41. require $current_config_file;
  42. $current_version = null;
  43. if (isset($_configuration['system_version'])) {
  44. $current_version = trim($_configuration['system_version']);
  45. }
  46. // If the current version is old, upgrading is assumed, the installer goes ahead.
  47. return empty($current_version) ? false : version_compare($current_version, $new_version, '>=');
  48. }
  49. /**
  50. * This function checks if a php extension exists or not and returns an HTML status string.
  51. *
  52. * @param string $extensionName Name of the PHP extension to be checked
  53. * @param string $returnSuccess Text to show when extension is available (defaults to 'Yes')
  54. * @param string $returnFailure Text to show when extension is available (defaults to 'No')
  55. * @param bool $optional Whether this extension is optional (then show unavailable text in orange rather than red)
  56. * @param string $enabledTerm If this string is not null, then use to check if the corresponding parameter is = 1.
  57. * If not, mention it's present but not enabled. For example, for opcache, this should be 'opcache.enable'
  58. *
  59. * @return string HTML string reporting the status of this extension. Language-aware.
  60. *
  61. * @author Christophe Gesch??
  62. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  63. * @author Yannick Warnier <yannick.warnier@dokeos.com>
  64. */
  65. function checkExtension(
  66. $extensionName,
  67. $returnSuccess = 'Yes',
  68. $returnFailure = 'No',
  69. $optional = false,
  70. $enabledTerm = ''
  71. ) {
  72. if (extension_loaded($extensionName)) {
  73. if (!empty($enabledTerm)) {
  74. $isEnabled = ini_get($enabledTerm);
  75. if ($isEnabled == '1') {
  76. return Display::label($returnSuccess, 'success');
  77. } else {
  78. if ($optional) {
  79. return Display::label(get_lang('Extension installed but not enabled'), 'warning');
  80. } else {
  81. return Display::label(get_lang('Extension installed but not enabled'), 'important');
  82. }
  83. }
  84. } else {
  85. return Display::label($returnSuccess, 'success');
  86. }
  87. } else {
  88. if ($optional) {
  89. return Display::label($returnFailure, 'warning');
  90. } else {
  91. return Display::label($returnFailure, 'important');
  92. }
  93. }
  94. }
  95. /**
  96. * This function checks whether a php setting matches the recommended value.
  97. *
  98. * @param string $phpSetting A PHP setting to check
  99. * @param string $recommendedValue A recommended value to show on screen
  100. * @param mixed $returnSuccess What to show on success
  101. * @param mixed $returnFailure What to show on failure
  102. *
  103. * @return string A label to show
  104. *
  105. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  106. */
  107. function checkPhpSetting(
  108. $phpSetting,
  109. $recommendedValue,
  110. $returnSuccess = false,
  111. $returnFailure = false
  112. ) {
  113. $currentPhpValue = getPhpSetting($phpSetting);
  114. if ($currentPhpValue == $recommendedValue) {
  115. return Display::label($currentPhpValue.' '.$returnSuccess, 'success');
  116. } else {
  117. return Display::label($currentPhpValue.' '.$returnSuccess, 'important');
  118. }
  119. }
  120. /**
  121. * This function return the value of a php.ini setting if not "" or if exists,
  122. * otherwise return false.
  123. *
  124. * @param string $phpSetting The name of a PHP setting
  125. *
  126. * @return mixed The value of the setting, or false if not found
  127. */
  128. function checkPhpSettingExists($phpSetting)
  129. {
  130. if (ini_get($phpSetting) != "") {
  131. return ini_get($phpSetting);
  132. }
  133. return false;
  134. }
  135. /**
  136. * Returns a textual value ('ON' or 'OFF') based on a requester 2-state ini- configuration setting.
  137. *
  138. * @param string $val a php ini value
  139. *
  140. * @return bool ON or OFF
  141. *
  142. * @author Joomla <http://www.joomla.org>
  143. */
  144. function getPhpSetting($val)
  145. {
  146. $value = ini_get($val);
  147. switch ($val) {
  148. case 'display_errors':
  149. global $originalDisplayErrors;
  150. $value = $originalDisplayErrors;
  151. break;
  152. }
  153. return $value == '1' ? 'ON' : 'OFF';
  154. }
  155. /**
  156. * This function returns a string "true" or "false" according to the passed parameter.
  157. *
  158. * @param int $var The variable to present as text
  159. *
  160. * @return string the string "true" or "false"
  161. *
  162. * @author Christophe Gesch??
  163. */
  164. function trueFalse($var)
  165. {
  166. return $var ? 'true' : 'false';
  167. }
  168. /**
  169. * Removes memory and time limits as much as possible.
  170. */
  171. function remove_memory_and_time_limits()
  172. {
  173. if (function_exists('ini_set')) {
  174. ini_set('memory_limit', -1);
  175. ini_set('max_execution_time', 0);
  176. error_log('Update-db script: memory_limit set to -1', 0);
  177. error_log('Update-db script: max_execution_time 0', 0);
  178. } else {
  179. error_log('Update-db script: could not change memory and time limits', 0);
  180. }
  181. }
  182. /**
  183. * Detects browser's language.
  184. *
  185. * @return string Returns a language identificator, i.e. 'english', 'spanish', ...
  186. *
  187. * @author Ivan Tcholakov, 2010
  188. */
  189. function detect_browser_language()
  190. {
  191. static $language_index = [
  192. 'ar' => 'arabic',
  193. 'ast' => 'asturian',
  194. 'bg' => 'bulgarian',
  195. 'bs' => 'bosnian',
  196. 'ca' => 'catalan',
  197. 'zh' => 'simpl_chinese',
  198. 'zh-tw' => 'trad_chinese',
  199. 'cs' => 'czech',
  200. 'da' => 'danish',
  201. 'prs' => 'dari',
  202. 'de' => 'german',
  203. 'el' => 'greek',
  204. 'en' => 'english',
  205. 'es' => 'spanish',
  206. 'eo' => 'esperanto',
  207. 'eu' => 'basque',
  208. 'fa' => 'persian',
  209. 'fr' => 'french',
  210. 'fur' => 'friulian',
  211. 'gl' => 'galician',
  212. 'ka' => 'georgian',
  213. 'hr' => 'croatian',
  214. 'he' => 'hebrew',
  215. 'hi' => 'hindi',
  216. 'id' => 'indonesian',
  217. 'it' => 'italian',
  218. 'ko' => 'korean',
  219. 'lv' => 'latvian',
  220. 'lt' => 'lithuanian',
  221. 'mk' => 'macedonian',
  222. 'hu' => 'hungarian',
  223. 'ms' => 'malay',
  224. 'nl' => 'dutch',
  225. 'ja' => 'japanese',
  226. 'no' => 'norwegian',
  227. 'oc' => 'occitan',
  228. 'ps' => 'pashto',
  229. 'pl' => 'polish',
  230. 'pt' => 'portuguese',
  231. 'pt-br' => 'brazilian',
  232. 'ro' => 'romanian',
  233. 'qu' => 'quechua_cusco',
  234. 'ru' => 'russian',
  235. 'sk' => 'slovak',
  236. 'sl' => 'slovenian',
  237. 'sr' => 'serbian',
  238. 'fi' => 'finnish',
  239. 'sv' => 'swedish',
  240. 'th' => 'thai',
  241. 'tr' => 'turkish',
  242. 'uk' => 'ukrainian',
  243. 'vi' => 'vietnamese',
  244. 'sw' => 'swahili',
  245. 'yo' => 'yoruba',
  246. ];
  247. $system_available_languages = get_language_folder_list();
  248. $accept_languages = strtolower(str_replace('_', '-', $_SERVER['HTTP_ACCEPT_LANGUAGE']));
  249. foreach ($language_index as $code => $language) {
  250. if (strpos($accept_languages, $code) === 0) {
  251. if (!empty($system_available_languages[$language])) {
  252. return $language;
  253. }
  254. }
  255. }
  256. $user_agent = strtolower(str_replace('_', '-', $_SERVER['HTTP_USER_AGENT']));
  257. foreach ($language_index as $code => $language) {
  258. if (@preg_match("/[\[\( ]{$code}[;,_\-\)]/", $user_agent)) {
  259. if (!empty($system_available_languages[$language])) {
  260. return $language;
  261. }
  262. }
  263. }
  264. return 'english';
  265. }
  266. /* FILESYSTEM RELATED FUNCTIONS */
  267. /**
  268. * This function checks if the given folder is writable.
  269. *
  270. * @param string $folder Full path to a folder
  271. * @param bool $suggestion Whether to show a suggestion or not
  272. *
  273. * @return string
  274. */
  275. function check_writable($folder, $suggestion = false)
  276. {
  277. if (is_writable($folder)) {
  278. return Display::label(get_lang('Writable'), 'success');
  279. } else {
  280. if ($suggestion) {
  281. return Display::label(get_lang('Not writable'), 'info');
  282. } else {
  283. return Display::label(get_lang('Not writable'), 'important');
  284. }
  285. }
  286. }
  287. /**
  288. * This function checks if the given folder is readable.
  289. *
  290. * @param string $folder Full path to a folder
  291. * @param bool $suggestion Whether to show a suggestion or not
  292. *
  293. * @return string
  294. */
  295. function checkReadable($folder, $suggestion = false)
  296. {
  297. if (is_readable($folder)) {
  298. return Display::label(get_lang('Readable'), 'success');
  299. } else {
  300. if ($suggestion) {
  301. return Display::label(get_lang('Not readable'), 'info');
  302. } else {
  303. return Display::label(get_lang('Not readable'), 'important');
  304. }
  305. }
  306. }
  307. /**
  308. * This function is similar to the core file() function, except that it
  309. * works with line endings in Windows (which is not the case of file()).
  310. *
  311. * @param string $filename
  312. *
  313. * @return array The lines of the file returned as an array
  314. */
  315. function file_to_array($filename)
  316. {
  317. if (!is_readable($filename) || is_dir($filename)) {
  318. return [];
  319. }
  320. $fp = fopen($filename, 'rb');
  321. $buffer = fread($fp, filesize($filename));
  322. fclose($fp);
  323. return explode('<br />', nl2br($buffer));
  324. }
  325. /**
  326. * We assume this function is called from install scripts that reside inside the install folder.
  327. */
  328. function set_file_folder_permissions()
  329. {
  330. @chmod('.', 0755); //set permissions on install dir
  331. @chmod('..', 0755); //set permissions on parent dir of install dir
  332. }
  333. /**
  334. * Write the main system config file.
  335. *
  336. * @param string $path Path to the config file
  337. */
  338. function write_system_config_file($path)
  339. {
  340. global $dbHostForm;
  341. global $dbPortForm;
  342. global $dbUsernameForm;
  343. global $dbPassForm;
  344. global $dbNameForm;
  345. global $urlForm;
  346. global $pathForm;
  347. global $urlAppendPath;
  348. global $languageForm;
  349. global $encryptPassForm;
  350. global $session_lifetime;
  351. global $new_version;
  352. global $new_version_stable;
  353. $root_sys = api_add_trailing_slash(str_replace('\\', '/', realpath($pathForm)));
  354. $content = file_get_contents(__DIR__.'/'.SYSTEM_CONFIG_FILENAME);
  355. $config['{DATE_GENERATED}'] = date('r');
  356. $config['{DATABASE_HOST}'] = $dbHostForm;
  357. $config['{DATABASE_PORT}'] = $dbPortForm;
  358. $config['{DATABASE_USER}'] = $dbUsernameForm;
  359. $config['{DATABASE_PASSWORD}'] = $dbPassForm;
  360. $config['{DATABASE_MAIN}'] = $dbNameForm;
  361. $config['{ROOT_WEB}'] = $urlForm;
  362. $config['{ROOT_SYS}'] = $root_sys;
  363. $config['{URL_APPEND_PATH}'] = $urlAppendPath;
  364. $config['{PLATFORM_LANGUAGE}'] = $languageForm;
  365. $config['{SECURITY_KEY}'] = md5(uniqid(rand().time()));
  366. $config['{ENCRYPT_PASSWORD}'] = $encryptPassForm;
  367. $config['SESSION_LIFETIME'] = $session_lifetime;
  368. $config['{NEW_VERSION}'] = $new_version;
  369. $config['NEW_VERSION_STABLE'] = trueFalse($new_version_stable);
  370. foreach ($config as $key => $value) {
  371. $content = str_replace($key, $value, $content);
  372. }
  373. $fp = @fopen($path, 'w');
  374. if (!$fp) {
  375. echo '<strong>
  376. <font color="red">Your script doesn\'t have write access to the config directory</font></strong><br />
  377. <em>('.str_replace('\\', '/', realpath($path)).')</em><br /><br />
  378. You probably do not have write access on Chamilo root directory,
  379. i.e. you should <em>CHMOD 777</em> or <em>755</em> or <em>775</em>.<br /><br />
  380. Your problems can be related on two possible causes:<br />
  381. <ul>
  382. <li>Permission problems.<br />Try initially with <em>chmod -R 777</em> and increase restrictions gradually.</li>
  383. <li>PHP is running in <a href="http://www.php.net/manual/en/features.safe-mode.php" target="_blank">Safe-Mode</a>.
  384. If possible, try to switch it off.</li>
  385. </ul>
  386. <a href="http://forum.chamilo.org/" target="_blank">Read about this problem in Support Forum</a><br /><br />
  387. Please go back to step 5.
  388. <p><input type="submit" name="step5" value="&lt; Back" /></p>
  389. </td></tr></table></form></body></html>';
  390. exit;
  391. }
  392. fwrite($fp, $content);
  393. fclose($fp);
  394. }
  395. /**
  396. * Returns a list of language directories.
  397. */
  398. function get_language_folder_list()
  399. {
  400. return [
  401. 'ar' => 'arabic',
  402. 'ast' => 'asturian',
  403. 'bg' => 'bulgarian',
  404. 'bs' => 'bosnian',
  405. 'ca' => 'catalan',
  406. 'zh' => 'simpl_chinese',
  407. 'zh-tw' => 'trad_chinese',
  408. 'cs' => 'czech',
  409. 'da' => 'danish',
  410. 'prs' => 'dari',
  411. 'de' => 'german',
  412. 'el' => 'greek',
  413. 'en' => 'english',
  414. 'es' => 'spanish',
  415. 'eo' => 'esperanto',
  416. 'eu' => 'basque',
  417. 'fa' => 'persian',
  418. 'fr' => 'french',
  419. 'fur' => 'friulian',
  420. 'gl' => 'galician',
  421. 'ka' => 'georgian',
  422. 'hr' => 'croatian',
  423. 'he' => 'hebrew',
  424. 'hi' => 'hindi',
  425. 'id' => 'indonesian',
  426. 'it' => 'italian',
  427. 'ko' => 'korean',
  428. 'lv' => 'latvian',
  429. 'lt' => 'lithuanian',
  430. 'mk' => 'macedonian',
  431. 'hu' => 'hungarian',
  432. 'ms' => 'malay',
  433. 'nl' => 'dutch',
  434. 'ja' => 'japanese',
  435. 'no' => 'norwegian',
  436. 'oc' => 'occitan',
  437. 'ps' => 'pashto',
  438. 'pl' => 'polish',
  439. 'pt' => 'portuguese',
  440. 'pt-br' => 'brazilian',
  441. 'ro' => 'romanian',
  442. 'qu' => 'quechua_cusco',
  443. 'ru' => 'russian',
  444. 'sk' => 'slovak',
  445. 'sl' => 'slovenian',
  446. 'sr' => 'serbian',
  447. 'fi' => 'finnish',
  448. 'sv' => 'swedish',
  449. 'th' => 'thai',
  450. 'tr' => 'turkish',
  451. 'uk' => 'ukrainian',
  452. 'vi' => 'vietnamese',
  453. 'sw' => 'swahili',
  454. 'yo' => 'yoruba',
  455. ];
  456. }
  457. /**
  458. * This function returns the value of a parameter from the configuration file.
  459. *
  460. * WARNING - this function relies heavily on global variables $updateFromConfigFile
  461. * and $configFile, and also changes these globals. This can be rewritten.
  462. *
  463. * @param string $param the parameter of which the value is returned
  464. * @param string $updatePath If we want to give the path rather than take it from POST
  465. *
  466. * @return string the value of the parameter
  467. *
  468. * @author Olivier Brouckaert
  469. * @author Reworked by Ivan Tcholakov, 2010
  470. */
  471. function get_config_param($param, $updatePath = '')
  472. {
  473. global $configFile, $updateFromConfigFile;
  474. // Look if we already have the queried parameter.
  475. if (is_array($configFile) && isset($configFile[$param])) {
  476. return $configFile[$param];
  477. }
  478. if (empty($updatePath) && !empty($_POST['updatePath'])) {
  479. $updatePath = $_POST['updatePath'];
  480. }
  481. if (empty($updatePath)) {
  482. $updatePath = api_get_path(SYS_PATH);
  483. }
  484. $updatePath = api_add_trailing_slash(str_replace('\\', '/', realpath($updatePath)));
  485. $updateFromInstalledVersionFile = '';
  486. if (empty($updateFromConfigFile)) {
  487. // If update from previous install was requested,
  488. // try to recover config file from Chamilo 1.9.x
  489. if (file_exists($updatePath.'main/inc/conf/configuration.php')) {
  490. $updateFromConfigFile = 'main/inc/conf/configuration.php';
  491. } elseif (file_exists($updatePath.'app/config/configuration.php')) {
  492. $updateFromConfigFile = 'app/config/configuration.php';
  493. } elseif (file_exists($updatePath.'config/configuration.php')) {
  494. $updateFromConfigFile = 'config/configuration.php';
  495. } else {
  496. // Give up recovering.
  497. //error_log('Chamilo Notice: Could not find previous config file at '.$updatePath.'main/inc/conf/configuration.php nor at '.$updatePath.'claroline/inc/conf/claro_main.conf.php in get_config_param(). Will start new config (in '.__FILE__.', line '.__LINE__.')', 0);
  498. return null;
  499. }
  500. }
  501. if (file_exists($updatePath.$updateFromConfigFile) &&
  502. !is_dir($updatePath.$updateFromConfigFile)
  503. ) {
  504. require $updatePath.$updateFromConfigFile;
  505. $config = new Zend\Config\Config($_configuration);
  506. return $config->get($param);
  507. }
  508. error_log('Config array could not be found in get_config_param()', 0);
  509. return null;
  510. }
  511. /* DATABASE RELATED FUNCTIONS */
  512. /**
  513. * Gets a configuration parameter from the database. Returns returns null on failure.
  514. *
  515. * @param string $param Name of param we want
  516. *
  517. * @return mixed The parameter value or null if not found
  518. */
  519. function get_config_param_from_db($param = '')
  520. {
  521. $param = Database::escape_string($param);
  522. if (($res = Database::query("SELECT * FROM settings_current WHERE variable = '$param'")) !== false) {
  523. if (Database::num_rows($res) > 0) {
  524. $row = Database::fetch_array($res);
  525. return $row['selected_value'];
  526. }
  527. }
  528. return null;
  529. }
  530. /**
  531. * Connect to the database and returns the entity manager.
  532. *
  533. * @param string $dbHostForm DB host
  534. * @param string $dbUsernameForm DB username
  535. * @param string $dbPassForm DB password
  536. * @param string $dbNameForm DB name
  537. * @param int $dbPortForm DB port
  538. *
  539. * @return \Database
  540. */
  541. function connectToDatabase(
  542. $dbHostForm,
  543. $dbUsernameForm,
  544. $dbPassForm,
  545. $dbNameForm,
  546. $dbPortForm = 3306
  547. ) {
  548. $dbParams = [
  549. 'driver' => 'pdo_mysql',
  550. 'host' => $dbHostForm,
  551. 'port' => $dbPortForm,
  552. 'user' => $dbUsernameForm,
  553. 'password' => $dbPassForm,
  554. 'dbname' => $dbNameForm,
  555. ];
  556. $database = new \Database();
  557. $database->connect($dbParams);
  558. return $database;
  559. }
  560. /* DISPLAY FUNCTIONS */
  561. /**
  562. * This function prints class=active_step $current_step=$param.
  563. *
  564. * @param int $param A step in the installer process
  565. *
  566. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  567. */
  568. function step_active($param)
  569. {
  570. global $current_step;
  571. if ($param == $current_step) {
  572. echo 'active';
  573. }
  574. }
  575. /**
  576. * This function displays the Step X of Y -.
  577. *
  578. * @return string String that says 'Step X of Y' with the right values
  579. */
  580. function display_step_sequence()
  581. {
  582. global $current_step;
  583. return get_lang('Step'.$current_step).' &ndash; ';
  584. }
  585. /**
  586. * Displays a drop down box for selection the preferred language.
  587. */
  588. function display_language_selection_box(
  589. $name = 'language_list',
  590. $default_language = 'english'
  591. ) {
  592. // Reading language list.
  593. $language_list = get_language_folder_list();
  594. // Sanity checks due to the possibility for customizations.
  595. if (!is_array($language_list) || empty($language_list)) {
  596. $language_list = ['en' => 'English'];
  597. }
  598. // Sorting again, if it is necessary.
  599. //asort($language_list);
  600. // More sanity checks.
  601. if (!array_key_exists($default_language, $language_list)) {
  602. if (array_key_exists('en', $language_list)) {
  603. $default_language = 'en';
  604. } else {
  605. $language_keys = array_keys($language_list);
  606. $default_language = $language_keys[0];
  607. }
  608. }
  609. // Displaying the box.
  610. $html = Display::select(
  611. 'language_list',
  612. $language_list,
  613. $default_language,
  614. ['class' => 'form-control selectpicker show-tick form-control'],
  615. false
  616. );
  617. return $html;
  618. }
  619. /**
  620. * This function displays a language dropdown box so that the installatioin
  621. * can be done in the language of the user.
  622. */
  623. function display_language_selection()
  624. {
  625. ?>
  626. <div class="install-icon">
  627. <img width="150px;" src="chamilo-install.svg"/>
  628. </div>
  629. <h2 class="install-title">
  630. <?php echo display_step_sequence(); ?>
  631. <?php echo get_lang('Installation Language'); ?>
  632. </h2>
  633. <label for="language_list"><?php echo get_lang('Please select installation language'); ?></label>
  634. <div class="form-group">
  635. <?php echo display_language_selection_box('language_list', api_get_interface_language()); ?>
  636. </div>
  637. <button type="submit" name="step1" class="btn btn-success" value="<?php echo get_lang('Next'); ?>">
  638. <em class="fa fa-forward"> </em>
  639. <?php echo get_lang('Next'); ?>
  640. </button>
  641. <input type="hidden" name="is_executable" id="is_executable" value="-" />
  642. <div class="RequirementHeading">
  643. <?php echo get_lang('Cannot find your language in the list? Contact us at info@chamilo.org to contribute as a translator.'); ?>
  644. </div>
  645. <?php
  646. }
  647. /**
  648. * This function displays the requirements for installing Chamilo.
  649. *
  650. * @param string $installType
  651. * @param bool $badUpdatePath
  652. * @param bool $badUpdatePath
  653. * @param string $updatePath The updatePath given (if given)
  654. * @param array $update_from_version_8 The different subversions from version 1.9
  655. *
  656. * @author unknow
  657. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  658. */
  659. function display_requirements(
  660. $installType,
  661. $badUpdatePath,
  662. $updatePath = '',
  663. $update_from_version_8 = []
  664. ) {
  665. global $_setting, $originalMemoryLimit;
  666. $dir = api_get_path(SYS_ARCHIVE_PATH).'temp/';
  667. $fileToCreate = 'test';
  668. $perms_dir = [0777, 0755, 0775, 0770, 0750, 0700];
  669. $perms_fil = [0666, 0644, 0664, 0660, 0640, 0600];
  670. $course_test_was_created = false;
  671. $dir_perm_verified = 0777;
  672. foreach ($perms_dir as $perm) {
  673. $r = @mkdir($dir, $perm);
  674. if ($r === true) {
  675. $dir_perm_verified = $perm;
  676. $course_test_was_created = true;
  677. break;
  678. }
  679. }
  680. $fil_perm_verified = 0666;
  681. $file_course_test_was_created = false;
  682. if (is_dir($dir)) {
  683. foreach ($perms_fil as $perm) {
  684. if ($file_course_test_was_created == true) {
  685. break;
  686. }
  687. $r = @touch($dir.'/'.$fileToCreate, $perm);
  688. if ($r === true) {
  689. $fil_perm_verified = $perm;
  690. $file_course_test_was_created = true;
  691. }
  692. }
  693. }
  694. @unlink($dir.'/'.$fileToCreate);
  695. @rmdir($dir);
  696. echo '<h2 class="install-title">'.display_step_sequence().get_lang('Requirements')."</h2>";
  697. echo '<div class="RequirementText">';
  698. echo '<strong>'.get_lang('Please read the following requirements thoroughly.').'</strong><br />';
  699. echo get_lang('For more details').' <a href="../../documentation/installation_guide.html" target="_blank">'.get_lang('Read the installation guide').'</a>.<br />'."\n";
  700. if ($installType == 'update') {
  701. echo get_lang('If you plan to upgrade from an older version of Chamilo, you might want to <a href="../../documentation/changelog.html" target="_blank">have a look at the changelog</a> to know what\'s new and what has been changed').'<br />';
  702. }
  703. echo '</div>';
  704. // SERVER REQUIREMENTS
  705. echo '<h4 class="install-subtitle">'.get_lang('Server requirements').'</h4>';
  706. $timezone = checkPhpSettingExists('date.timezone');
  707. if (!$timezone) {
  708. echo "<div class='alert alert-warning'>
  709. <i class=\"fa fa-exclamation-triangle\" aria-hidden=\"true\"></i>&nbsp;".
  710. get_lang('We have detected that your PHP installation does not define the date.timezone setting. This is a requirement of Chamilo. Please make sure it is configured by checking your php.ini configuration, otherwise you will run into problems. We warned you!')."</div>";
  711. }
  712. echo '<div class="install-requirement">'.get_lang('Server requirementsInfo').'</div>';
  713. echo '<div class="table-responsive">';
  714. echo '<table class="table table-bordered">
  715. <tr>
  716. <td class="requirements-item">'.get_lang('PHP version').' >= '.REQUIRED_PHP_VERSION.'</td>
  717. <td class="requirements-value">';
  718. if (version_compare(phpversion(), REQUIRED_PHP_VERSION, '>=') > 1) {
  719. echo '<strong class="text-danger">'.get_lang('PHP versionError').'</strong>';
  720. } else {
  721. echo '<strong class="text-success">'.get_lang('PHP versionOK').' '.phpversion().'</strong>';
  722. }
  723. echo '</td>
  724. </tr>
  725. <tr>
  726. <td class="requirements-item"><a href="http://php.net/manual/en/book.session.php" target="_blank">Session</a> '.get_lang('Support').'</td>
  727. <td class="requirements-value">'.checkExtension('session', get_lang('Yes'), get_lang('Sessions extension not available')).'</td>
  728. </tr>
  729. <tr>
  730. <td class="requirements-item"><a href="http://php.net/manual/en/book.mysql.php" target="_blank">pdo_mysql</a> '.get_lang('Support').'</td>
  731. <td class="requirements-value">'.checkExtension('pdo_mysql', get_lang('Yes'), get_lang('MySQL extension not available')).'</td>
  732. </tr>
  733. <tr>
  734. <td class="requirements-item"><a href="http://php.net/manual/en/book.zip.php" target="_blank">Zip</a> '.get_lang('Support').'</td>
  735. <td class="requirements-value">'.checkExtension('zip', get_lang('Yes'), get_lang('Extension not available')).'</td>
  736. </tr>
  737. <tr>
  738. <td class="requirements-item"><a href="http://php.net/manual/en/book.zlib.php" target="_blank">Zlib</a> '.get_lang('Support').'</td>
  739. <td class="requirements-value">'.checkExtension('zlib', get_lang('Yes'), get_lang('Zlib extension not available')).'</td>
  740. </tr>
  741. <tr>
  742. <td class="requirements-item"><a href="http://php.net/manual/en/book.pcre.php" target="_blank">Perl-compatible regular expressions</a> '.get_lang('Support').'</td>
  743. <td class="requirements-value">'.checkExtension('pcre', get_lang('Yes'), get_lang('PCRE extension not available')).'</td>
  744. </tr>
  745. <tr>
  746. <td class="requirements-item"><a href="http://php.net/manual/en/book.xml.php" target="_blank">XML</a> '.get_lang('Support').'</td>
  747. <td class="requirements-value">'.checkExtension('xml', get_lang('Yes'), get_lang('No')).'</td>
  748. </tr>
  749. <tr>
  750. <td class="requirements-item"><a href="http://php.net/manual/en/book.intl.php" target="_blank">Internationalization</a> '.get_lang('Support').'</td>
  751. <td class="requirements-value">'.checkExtension('intl', get_lang('Yes'), get_lang('No')).'</td>
  752. </tr>
  753. <tr>
  754. <td class="requirements-item"><a href="http://php.net/manual/en/book.json.php" target="_blank">JSON</a> '.get_lang('Support').'</td>
  755. <td class="requirements-value">'.checkExtension('json', get_lang('Yes'), get_lang('No')).'</td>
  756. </tr>
  757. <tr>
  758. <td class="requirements-item"><a href="http://php.net/manual/en/book.image.php" target="_blank">GD</a> '.get_lang('Support').'</td>
  759. <td class="requirements-value">'.checkExtension('gd', get_lang('Yes'), get_lang('GD Extension not available')).'</td>
  760. </tr>
  761. <tr>
  762. <td class="requirements-item"><a href="http://php.net/manual/en/book.curl.php" target="_blank">cURL</a>'.get_lang('Support').'</td>
  763. <td class="requirements-value">'.checkExtension('curl', get_lang('Yes'), get_lang('No')).'</td>
  764. </tr>
  765. <tr>
  766. <td class="requirements-item"><a href="http://php.net/manual/en/book.mbstring.php" target="_blank">Multibyte string</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
  767. <td class="requirements-value">'.checkExtension('mbstring', get_lang('Yes'), get_lang('MBString extension not available'), true).'</td>
  768. </tr>
  769. <tr>
  770. <td class="requirements-item"><a href="http://php.net/opcache" target="_blank">Zend OpCache</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
  771. <td class="requirements-value">'.checkExtension('Zend OPcache', get_lang('Yes'), get_lang('No'), true, 'opcache.enable').'</td>
  772. </tr>
  773. <tr>
  774. <td class="requirements-item"><a href="http://php.net/apcu" target="_blank">APCu</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
  775. <td class="requirements-value">'.checkExtension('apcu', get_lang('Yes'), get_lang('No'), true, 'apc.enabled').'</td>
  776. </tr>
  777. <tr>
  778. <td class="requirements-item"><a href="http://php.net/manual/en/book.iconv.php" target="_blank">Iconv</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
  779. <td class="requirements-value">'.checkExtension('iconv', get_lang('Yes'), get_lang('No'), true).'</td>
  780. </tr>
  781. <tr>
  782. <td class="requirements-item"><a href="http://php.net/manual/en/book.ldap.php" target="_blank">LDAP</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
  783. <td class="requirements-value">'.checkExtension('ldap', get_lang('Yes'), get_lang('LDAP Extension not available'), true).'</td>
  784. </tr>
  785. <tr>
  786. <td class="requirements-item"><a href="http://xapian.org/" target="_blank">Xapian</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
  787. <td class="requirements-value">'.checkExtension('xapian', get_lang('Yes'), get_lang('No'), true).'</td>
  788. </tr>
  789. </table>';
  790. echo '</div>';
  791. // RECOMMENDED SETTINGS
  792. // Note: these are the settings for Joomla, does this also apply for Chamilo?
  793. // Note: also add upload_max_filesize here so that large uploads are possible
  794. echo '<h4 class="install-subtitle">'.get_lang('(recommended) settings').'</h4>';
  795. echo '<div class="install-requirement">'.get_lang('(recommended) settingsInfo').'</div>';
  796. echo '<div class="table-responsive">';
  797. echo '<table class="table table-bordered">
  798. <tr>
  799. <th>'.get_lang('Setting').'</th>
  800. <th>'.get_lang('(recommended)').'</th>
  801. <th>'.get_lang('Currently').'</th>
  802. </tr>
  803. <tr>
  804. <td class="requirements-item"><a href="http://php.net/manual/ref.errorfunc.php#ini.display-errors">Display Errors</a></td>
  805. <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
  806. <td class="requirements-value">'.checkPhpSetting('display_errors', 'OFF').'</td>
  807. </tr>
  808. <tr>
  809. <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.file-uploads">File Uploads</a></td>
  810. <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
  811. <td class="requirements-value">'.checkPhpSetting('file_uploads', 'ON').'</td>
  812. </tr>
  813. <tr>
  814. <td class="requirements-item"><a href="http://php.net/manual/ref.session.php#ini.session.auto-start">Session auto start</a></td>
  815. <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
  816. <td class="requirements-value">'.checkPhpSetting('session.auto_start', 'OFF').'</td>
  817. </tr>
  818. <tr>
  819. <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.short-open-tag">Short Open Tag</a></td>
  820. <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
  821. <td class="requirements-value">'.checkPhpSetting('short_open_tag', 'OFF').'</td>
  822. </tr>
  823. <tr>
  824. <td class="requirements-item"><a href="http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-httponly">Cookie HTTP Only</a></td>
  825. <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
  826. <td class="requirements-value">'.checkPhpSetting('session.cookie_httponly', 'ON').'</td>
  827. </tr>
  828. <tr>
  829. <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.upload-max-filesize">Maximum upload file size</a></td>
  830. <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M', 'success').'</td>
  831. <td class="requirements-value">'.compare_setting_values(ini_get('upload_max_filesize'), REQUIRED_MIN_UPLOAD_MAX_FILESIZE).'</td>
  832. </tr>
  833. <tr>
  834. <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.post-max-size">Maximum post size</a></td>
  835. <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_POST_MAX_SIZE.'M', 'success').'</td>
  836. <td class="requirements-value">'.compare_setting_values(ini_get('post_max_size'), REQUIRED_MIN_POST_MAX_SIZE).'</td>
  837. </tr>
  838. <tr>
  839. <td class="requirements-item"><a href="http://www.php.net/manual/en/ini.core.php#ini.memory-limit">Memory Limit</a></td>
  840. <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_MEMORY_LIMIT.'M', 'success').'</td>
  841. <td class="requirements-value">'.compare_setting_values($originalMemoryLimit, REQUIRED_MIN_MEMORY_LIMIT).'</td>
  842. </tr>
  843. </table>';
  844. echo '</div>';
  845. // DIRECTORY AND FILE PERMISSIONS
  846. echo '<h4 class="install-subtitle">'.get_lang('Directory and files permissions').'</h4>';
  847. echo '<div class="install-requirement">'.get_lang('Directory and files permissionsInfo').'</div>';
  848. echo '<div class="table-responsive">';
  849. $_SESSION['permissions_for_new_directories'] = $_setting['permissions_for_new_directories'] = $dir_perm_verified;
  850. $_SESSION['permissions_for_new_files'] = $_setting['permissions_for_new_files'] = $fil_perm_verified;
  851. $dir_perm = Display::label('0'.decoct($dir_perm_verified), 'info');
  852. $file_perm = Display::label('0'.decoct($fil_perm_verified), 'info');
  853. $oldConf = '';
  854. if (file_exists(api_get_path(SYS_CODE_PATH).'inc/conf/configuration.php')) {
  855. $oldConf = '<tr>
  856. <td class="requirements-item">'.api_get_path(SYS_CODE_PATH).'inc/conf</td>
  857. <td class="requirements-value">'.check_writable(api_get_path(SYS_CODE_PATH).'inc/conf').'</td>
  858. </tr>';
  859. }
  860. echo '<table class="table table-bordered">
  861. '.$oldConf.'
  862. <tr>
  863. <td class="requirements-item">'.api_get_path(SYS_APP_PATH).'</td>
  864. <td class="requirements-value">'.check_writable(api_get_path(SYS_APP_PATH)).'</td>
  865. </tr>
  866. <tr>
  867. <td class="requirements-item">'.api_get_path(SYS_PATH).'config</td>
  868. <td class="requirements-value">'.check_writable(api_get_path(SYS_PATH).'config').'</td>
  869. </tr>
  870. <tr>
  871. <td class="requirements-item">'.api_get_path(SYS_PATH).'vendor/</td>
  872. <td class="requirements-value">'.checkReadable(api_get_path(SYS_PATH).'vendor').'</td>
  873. </tr>
  874. <tr>
  875. <td class="requirements-item">'.api_get_path(SYS_PUBLIC_PATH).'</td>
  876. <td class="requirements-value">'.check_writable(api_get_path(SYS_PUBLIC_PATH)).'</td>
  877. </tr>
  878. <tr>
  879. <td class="requirements-item">'.get_lang('Permissions for new directories').'</td>
  880. <td class="requirements-value">'.$dir_perm.' </td>
  881. </tr>
  882. <tr>
  883. <td class="requirements-item">'.get_lang('Permissions for new files').'</td>
  884. <td class="requirements-value">'.$file_perm.' </td>
  885. </tr>
  886. </table>';
  887. echo '</div>';
  888. if ($installType === 'update' && (empty($updatePath) || $badUpdatePath)) {
  889. if ($badUpdatePath) {
  890. ?>
  891. <div class="alert alert-warning">
  892. <?php echo get_lang('Error'); ?>!<br />
  893. Chamilo <?php echo implode('|', $update_from_version_8).' '.get_lang('has not been found in that directory'); ?>.
  894. </div>
  895. <?php
  896. } else {
  897. echo '<br />';
  898. } ?>
  899. <div class="row">
  900. <div class="col-md-12">
  901. <p><?php echo get_lang('Old version\'s root path'); ?>:
  902. <input type="text" name="updatePath" size="50" value="<?php echo ($badUpdatePath && !empty($updatePath)) ? htmlentities($updatePath) : ''; ?>" />
  903. </p>
  904. <p>
  905. <div class="btn-group">
  906. <button type="submit" class="btn btn-secondary" name="step1" value="<?php echo get_lang('Back'); ?>" >
  907. <em class="fa fa-backward"> <?php echo get_lang('Back'); ?></em>
  908. </button>
  909. <input type="hidden" name="is_executable" id="is_executable" value="-" />
  910. <button type="submit" class="btn btn-success" name="<?php echo isset($_POST['step2_update_6']) ? 'step2_update_6' : 'step2_update_8'; ?>" value="<?php echo get_lang('Next'); ?> &gt;" >
  911. <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
  912. </button>
  913. </div>
  914. </p>
  915. </div>
  916. </div>
  917. <?php
  918. } else {
  919. $error = false;
  920. // First, attempt to set writing permissions if we don't have them yet
  921. $perm = api_get_permissions_for_new_directories();
  922. $perm_file = api_get_permissions_for_new_files();
  923. $notWritable = [];
  924. $checked_writable = api_get_path(SYS_APP_PATH);
  925. if (!is_writable($checked_writable)) {
  926. $notWritable[] = $checked_writable;
  927. @chmod($checked_writable, $perm);
  928. }
  929. $checked_writable = api_get_path(SYS_PUBLIC_PATH);
  930. if (!is_writable($checked_writable)) {
  931. $notWritable[] = $checked_writable;
  932. @chmod($checked_writable, $perm);
  933. }
  934. if ($course_test_was_created == false) {
  935. $error = true;
  936. }
  937. $checked_writable = api_get_path(CONFIGURATION_PATH).'configuration.php';
  938. if (file_exists($checked_writable) && !is_writable($checked_writable)) {
  939. $notWritable[] = $checked_writable;
  940. @chmod($checked_writable, $perm_file);
  941. }
  942. // Second, if this fails, report an error
  943. //--> The user would have to adjust the permissions manually
  944. if (count($notWritable) > 0) {
  945. $error = true; ?>
  946. <div class="text-danger">
  947. <h3 class="text-center"><?php echo get_lang('Warning !'); ?></h3>
  948. <p>
  949. <?php printf(get_lang('Some files or folders don\'t have writing permission. To be able to install Chamilo you should first change their permissions (using CHMOD). Please read the %s installation guide %s'), '<a href="../../documentation/installation_guide.html" target="blank">', '</a>'); ?>
  950. </p>
  951. </div>
  952. <?php
  953. echo '<ul>';
  954. foreach ($notWritable as $value) {
  955. echo '<li class="text-danger">'.$value.'</li>';
  956. }
  957. echo '</ul>';
  958. } elseif (file_exists(api_get_path(CONFIGURATION_PATH).'configuration.php')) {
  959. // Check wether a Chamilo configuration file already exists.
  960. echo '<div class="alert alert-warning"><h4><center>';
  961. echo get_lang('Warning !ExistingLMSInstallationDetected');
  962. echo '</center></h4></div>';
  963. }
  964. $deprecated = [
  965. api_get_path(SYS_CODE_PATH).'exercice/',
  966. api_get_path(SYS_CODE_PATH).'newscorm/',
  967. api_get_path(SYS_PLUGIN_PATH).'ticket/',
  968. api_get_path(SYS_PLUGIN_PATH).'skype/',
  969. ];
  970. $deprecatedToRemove = [];
  971. foreach ($deprecated as $deprecatedDirectory) {
  972. if (!is_dir($deprecatedDirectory)) {
  973. continue;
  974. }
  975. $deprecatedToRemove[] = $deprecatedDirectory;
  976. }
  977. if (count($deprecatedToRemove) > 0) {
  978. ?>
  979. <p class="text-danger"><?php echo get_lang('Warning !ForDeprecatedDirectoriesForUpgrade'); ?></p>
  980. <ul>
  981. <?php foreach ($deprecatedToRemove as $deprecatedDirectory) {
  982. ?>
  983. <li class="text-danger"><?php echo $deprecatedDirectory; ?></li>
  984. <?php
  985. } ?>
  986. </ul>
  987. <?php
  988. }
  989. // And now display the choice buttons (go back or install)?>
  990. <p align="center" style="padding-top:15px">
  991. <button type="submit" name="step1" class="btn btn-default" onclick="javascript: window.location='index.php'; return false;" value="<?php echo get_lang('Previous'); ?>" >
  992. <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
  993. </button>
  994. <button type="submit" name="step2_install" class="btn btn-success" value="<?php echo get_lang("New installation"); ?>" <?php if ($error) {
  995. echo 'disabled="disabled"';
  996. } ?> >
  997. <em class="fa fa-forward"> </em> <?php echo get_lang('New installation'); ?>
  998. </button>
  999. <input type="hidden" name="is_executable" id="is_executable" value="-" />
  1000. <button type="submit" class="btn btn-default" <?php echo !$error ?: 'disabled="disabled"'; ?> name="step2_update_8" value="Upgrade from Chamilo 1.9.x">
  1001. <em class="fa fa-forward" aria-hidden="true"></em> <?php echo get_lang('Upgrade Chamilo LMS version'); ?>
  1002. </button>
  1003. </p>
  1004. <?php
  1005. }
  1006. }
  1007. /**
  1008. * Displays the license (GNU GPL) as step 2, with
  1009. * - an "I accept" button named step3 to proceed to step 3;
  1010. * - a "Back" button named step1 to go back to the first step.
  1011. */
  1012. function display_license_agreement()
  1013. {
  1014. echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Licence').'</h2>';
  1015. echo '<p>'.get_lang('Chamilo is free software distributed under the GNU General Public licence (GPL).').'</p>';
  1016. echo '<p><a href="../../documentation/license.html" target="_blank">'.get_lang('Printable version').'</a></p>';
  1017. echo '</div>'; ?>
  1018. <div class="form-group">
  1019. <pre style="overflow: auto; height: 200px; margin-top: 5px;">
  1020. <?php echo api_htmlentities(@file_get_contents(api_get_path(SYS_PATH).'documentation/license.txt')); ?>
  1021. </pre>
  1022. </div>
  1023. <div class="form-group form-check">
  1024. <input type="checkbox" name="accept" id="accept_licence" value="1">
  1025. <label for="accept_licence"><?php echo get_lang('I Accept'); ?></label>
  1026. </div>
  1027. <div class="row">
  1028. <div class="col-md-12">
  1029. <p class="alert alert-info"><?php echo get_lang('The images and media galleries of Chamilo use images from Nuvola, Crystal Clear and Tango icon galleries. Other images and media like diagrams and Flash animations are borrowed from Wikimedia and Ali Pakdel\'s and Denis Hoa\'s courses with their agreement and released under BY-SA Creative Commons license. You may find the license details at <a href="http://creativecommons.org/licenses/by-sa/3.0/">the CC website</a>, where a link to the full text of the license is provided at the bottom of the page.'); ?></p>
  1030. </div>
  1031. </div>
  1032. <!-- Contact information form -->
  1033. <div class="section-parameters">
  1034. <a href="javascript://" class = "advanced_parameters" >
  1035. <span id="img_plus_and_minus">&nbsp;<i class="fa fa-eye" aria-hidden="true"></i>&nbsp;<?php echo get_lang('Contact information'); ?></span>
  1036. </a>
  1037. </div>
  1038. <div id="id_contact_form" style="display:block">
  1039. <div class="normal-message"><?php echo get_lang('Contact informationDescription'); ?></div>
  1040. <div id="contact_registration">
  1041. <p><?php echo get_contact_registration_form(); ?></p><br />
  1042. </div>
  1043. </div>
  1044. <div class="text-center">
  1045. <button type="submit" class="btn btn-default" name="step1" value="&lt; <?php echo get_lang('Previous'); ?>" >
  1046. <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
  1047. </button>
  1048. <input type="hidden" name="is_executable" id="is_executable" value="-" />
  1049. <button type="submit" id="license-next" class="btn btn-success" name="step3" onclick="javascript: if(!document.getElementById('accept_licence').checked) { alert('<?php echo get_lang('You must accept the licence'); ?>');return false;}" value="<?php echo get_lang('Next'); ?> &gt;" >
  1050. <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
  1051. </button>
  1052. </div>
  1053. <?php
  1054. }
  1055. /**
  1056. * Get contact registration form.
  1057. */
  1058. function get_contact_registration_form()
  1059. {
  1060. $html = '
  1061. <div class="form-horizontal">
  1062. <div class="panel panel-default">
  1063. <div class="panel-body">
  1064. <div id="div_sent_information"></div>
  1065. <div class="form-group row">
  1066. <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Name').'</label>
  1067. <div class="col-sm-9"><input id="person_name" class="form-control" type="text" name="person_name" size="30" /></div>
  1068. </div>
  1069. <div class="form-group row">
  1070. <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('e-mail').'</label>
  1071. <div class="col-sm-9"><input id="person_email" class="form-control" type="text" name="person_email" size="30" /></div>
  1072. </div>
  1073. <div class="form-group row">
  1074. <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your company\'s name').'</label>
  1075. <div class="col-sm-9"><input id="company_name" class="form-control" type="text" name="company_name" size="30" /></div>
  1076. </div>
  1077. <div class="form-group row">
  1078. <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your company\'s activity').'</label>
  1079. <div class="col-sm-9">
  1080. <select class="selectpicker show-tick" name="company_activity" id="company_activity" >
  1081. <option value="">--- '.get_lang('Select one').' ---</option>
  1082. <Option value="Advertising/Marketing/PR">Advertising/Marketing/PR</Option><Option value="Agriculture/Forestry">Agriculture/Forestry</Option>
  1083. <Option value="Architecture">Architecture</Option><Option value="Banking/Finance">Banking/Finance</Option>
  1084. <Option value="Biotech/Pharmaceuticals">Biotech/Pharmaceuticals</Option><Option value="Business Equipment">Business Equipment</Option>
  1085. <Option value="Business Services">Business Services</Option><Option value="Construction">Construction</Option>
  1086. <Option value="Consulting/Research">Consulting/Research</Option><Option value="Education">Education</Option>
  1087. <Option value="Engineering">Engineering</Option><Option value="Environmental">Environmental</Option>
  1088. <Option value="Government">Government</Option><Option value="Healthcare">Health Care</Option>
  1089. <Option value="Hospitality/Lodging/Travel">Hospitality/Lodging/Travel</Option><Option value="Insurance">Insurance</Option>
  1090. <Option value="Legal">Legal</Option><Option value="Manufacturing">Manufacturing</Option>
  1091. <Option value="Media/Entertainment">Media/Entertainment</Option><Option value="Mortgage">Mortgage</Option>
  1092. <Option value="Non-Profit">Non-Profit</Option><Option value="Real Estate">Real Estate</Option>
  1093. <Option value="Restaurant">Restaurant</Option><Option value="Retail">Retail</Option>
  1094. <Option value="Shipping/Transportation">Shipping/Transportation</Option>
  1095. <Option value="Technology">Technology</Option><Option value="Telecommunications">Telecommunications</Option>
  1096. <Option value="Other">Other</Option>
  1097. </select>
  1098. </div>
  1099. </div>
  1100. <div class="form-group row">
  1101. <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your job\'s description').'</label>
  1102. <div class="col-sm-9">
  1103. <select class="selectpicker show-tick" name="person_role" id="person_role" >
  1104. <option value="">--- '.get_lang('Select one').' ---</option>
  1105. <Option value="Administration">Administration</Option><Option value="CEO/President/ Owner">CEO/President/ Owner</Option>
  1106. <Option value="CFO">CFO</Option><Option value="CIO/CTO">CIO/CTO</Option>
  1107. <Option value="Consultant">Consultant</Option><Option value="Customer Service">Customer Service</Option>
  1108. <Option value="Engineer/Programmer">Engineer/Programmer</Option><Option value="Facilities/Operations">Facilities/Operations</Option>
  1109. <Option value="Finance/ Accounting Manager">Finance/ Accounting Manager</Option><Option value="Finance/ Accounting Staff">Finance/ Accounting Staff</Option>
  1110. <Option value="General Manager">General Manager</Option><Option value="Human Resources">Human Resources</Option>
  1111. <Option value="IS/IT Management">IS/IT Management</Option><Option value="IS/ IT Staff">IS/ IT Staff</Option>
  1112. <Option value="Marketing Manager">Marketing Manager</Option><Option value="Marketing Staff">Marketing Staff</Option>
  1113. <Option value="Partner/Principal">Partner/Principal</Option><Option value="Purchasing Manager">Purchasing Manager</Option>
  1114. <Option value="Sales/ Business Dev. Manager">Sales/ Business Dev. Manager</Option><Option value="Sales/ Business Dev.">Sales/ Business Dev.</Option>
  1115. <Option value="Vice President/Senior Manager">Vice President/Senior Manager</Option><Option value="Other">Other</Option>
  1116. </select>
  1117. </div>
  1118. </div>
  1119. <div class="form-group row">
  1120. <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your company\'s home country').'</label>
  1121. <div class="col-sm-9">'.get_countries_list_from_array(true).'</div>
  1122. </div>
  1123. <div class="form-group row">
  1124. <label class="col-sm-3">'.get_lang('Company city').'</label>
  1125. <div class="col-sm-9">
  1126. <input type="text" class="form-control" id="company_city" name="company_city" size="30" />
  1127. </div>
  1128. </div>
  1129. <div class="form-group row">
  1130. <label class="col-sm-3">'.get_lang('Preferred contact language').'</label>
  1131. <div class="col-sm-9">
  1132. <select class="selectpicker show-tick" id="language" name="language">
  1133. <option value="bulgarian">Bulgarian</option>
  1134. <option value="indonesian">Bahasa Indonesia</option>
  1135. <option value="bosnian">Bosanski</option>
  1136. <option value="german">Deutsch</option>
  1137. <option selected="selected" value="english">English</option>
  1138. <option value="spanish">Spanish</option>
  1139. <option value="french">Français</option>
  1140. <option value="italian">Italian</option>
  1141. <option value="hungarian">Magyar</option>
  1142. <option value="dutch">Nederlands</option>
  1143. <option value="brazilian">Português do Brasil</option>
  1144. <option value="portuguese">Português europeu</option>
  1145. <option value="slovenian">Slovenčina</option>
  1146. </select>
  1147. </div>
  1148. </div>
  1149. <div class="form-group row">
  1150. <label class="col-sm-3">'.get_lang('Do you have the power to take financial decisions on behalf of your company?').'</label>
  1151. <div class="col-sm-9">
  1152. <div class="radio">
  1153. <label>
  1154. <input type="radio" name="financial_decision" id="financial_decision1" value="1" checked /> '.get_lang('Yes').'
  1155. </label>
  1156. </div>
  1157. <div class="radio">
  1158. <label>
  1159. <input type="radio" name="financial_decision" id="financial_decision2" value="0" /> '.get_lang('No').'
  1160. </label>
  1161. </div>
  1162. </div>
  1163. </div>
  1164. <div class="clear"></div>
  1165. <div class="form-group row">
  1166. <div class="col-sm-3">&nbsp;</div>
  1167. <div class="col-sm-9"><button type="button" class="btn btn-default" onclick="javascript:send_contact_information();" value="'.get_lang('Send information').'" ><em class="fa fa-floppy-o"></em> '.get_lang('Send information').'</button> <span id="loader-button"></span></div>
  1168. </div>
  1169. <div class="form-group row">
  1170. <div class="col-sm-3">&nbsp;</div>
  1171. <div class="col-sm-9"><span class="form_required">*</span><small>'.get_lang('Mandatory field').'</small></div>
  1172. </div></div></div>
  1173. </div>';
  1174. return $html;
  1175. }
  1176. /**
  1177. * Displays a parameter in a table row.
  1178. * Used by the display_database_settings_form function.
  1179. *
  1180. * @param string Type of install
  1181. * @param string Name of parameter
  1182. * @param string Field name (in the HTML form)
  1183. * @param string Field value
  1184. * @param string Extra notice (to show on the right side)
  1185. * @param bool Whether to display in update mode
  1186. * @param string Additional attribute for the <tr> element
  1187. */
  1188. function displayDatabaseParameter(
  1189. $installType,
  1190. $parameterName,
  1191. $formFieldName,
  1192. $parameterValue,
  1193. $extra_notice,
  1194. $displayWhenUpdate = true,
  1195. $tr_attribute = ''
  1196. ) {
  1197. //echo "<tr ".$tr_attribute.">";
  1198. echo "<label class='col-sm-4'>$parameterName</label>";
  1199. if ($installType == INSTALL_TYPE_UPDATE && $displayWhenUpdate) {
  1200. echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />'.$parameterValue;
  1201. } else {
  1202. $inputType = $formFieldName == 'dbPassForm' ? 'password' : 'text';
  1203. //Slightly limit the length of the database prefix to avoid having to cut down the databases names later on
  1204. $maxLength = $formFieldName == 'dbPrefixForm' ? '15' : MAX_FORM_FIELD_LENGTH;
  1205. if ($installType == INSTALL_TYPE_UPDATE) {
  1206. echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />';
  1207. echo api_htmlentities($parameterValue);
  1208. } else {
  1209. echo '<div class="col-sm-5"><input type="'.$inputType.'" class="form-control" size="'.DATABASE_FORM_FIELD_DISPLAY_LENGTH.'" maxlength="'.$maxLength.'" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />'."</div>";
  1210. echo '<div class="col-sm-3">'.$extra_notice.'</div>';
  1211. }
  1212. }
  1213. }
  1214. /**
  1215. * Displays step 3 - a form where the user can enter the installation settings
  1216. * regarding the databases - login and password, names, prefixes, single
  1217. * or multiple databases, tracking or not...
  1218. *
  1219. * @param string $installType
  1220. * @param string $dbHostForm
  1221. * @param string $dbUsernameForm
  1222. * @param string $dbPassForm
  1223. * @param string $dbNameForm
  1224. * @param int $dbPortForm
  1225. * @param string $installationProfile
  1226. */
  1227. function display_database_settings_form(
  1228. $installType,
  1229. $dbHostForm,
  1230. $dbUsernameForm,
  1231. $dbPassForm,
  1232. $dbNameForm,
  1233. $dbPortForm = 3306,
  1234. $installationProfile = ''
  1235. ) {
  1236. if ($installType == 'update') {
  1237. global $_configuration;
  1238. $dbHostForm = $_configuration['db_host'];
  1239. $dbUsernameForm = $_configuration['db_user'];
  1240. $dbPassForm = $_configuration['db_password'];
  1241. $dbNameForm = $_configuration['main_database'];
  1242. $dbPortForm = isset($_configuration['db_port']) ? $_configuration['db_port'] : '';
  1243. echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Database settings').'</h2></div>';
  1244. echo '<div class="RequirementContent">';
  1245. echo get_lang('The upgrade script will recover and update the Chamilo database(s). In order to do this, this script will use the databases and settings defined below. Because our software runs on a wide range of systems and because all of them might not have been tested, we strongly recommend you do a full backup of your databases before you proceed with the upgrade!');
  1246. echo '</div>';
  1247. } else {
  1248. echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Database settings').'</h2></div>';
  1249. echo '<div class="RequirementContent">';
  1250. echo get_lang('The install script will create (or use) the Chamilo database using the database name given here. Please make sure the user you give has the right to create the database by the name given here. If a database with this name exists, it will be overwritten. Please do not use the root user as the Chamilo database user. This can lead to serious security issues.');
  1251. echo '</div>';
  1252. } ?>
  1253. <div class="panel panel-default">
  1254. <div class="panel-body">
  1255. <div class="form-group row">
  1256. <label class="col-sm-4"><?php echo get_lang('Database Host'); ?> </label>
  1257. <?php if ($installType == 'update') {
  1258. ?>
  1259. <div class="col-sm-5">
  1260. <input type="hidden" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" /><?php echo $dbHostForm; ?>
  1261. </div>
  1262. <div class="col-sm-3"></div>
  1263. <?php
  1264. } else {
  1265. ?>
  1266. <div class="col-sm-5">
  1267. <input type="text" class="form-control" size="25" maxlength="50" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" />
  1268. </div>
  1269. <div class="col-sm-3"><?php echo get_lang('ex.').' localhost'; ?></div>
  1270. <?php
  1271. } ?>
  1272. </div>
  1273. <div class="form-group row">
  1274. <label class="col-sm-4"><?php echo get_lang('Port'); ?> </label>
  1275. <?php if ($installType == 'update') {
  1276. ?>
  1277. <div class="col-sm-5">
  1278. <input type="hidden" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" /><?php echo $dbPortForm; ?>
  1279. </div>
  1280. <div class="col-sm-3"></div>
  1281. <?php
  1282. } else {
  1283. ?>
  1284. <div class="col-sm-5">
  1285. <input type="text" class="form-control" size="25" maxlength="50" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" />
  1286. </div>
  1287. <div class="col-sm-3"><?php echo get_lang('ex.').' 3306'; ?></div>
  1288. <?php
  1289. } ?>
  1290. </div>
  1291. <div class="form-group row">
  1292. <?php
  1293. //database user username
  1294. $example_login = get_lang('ex.').' root';
  1295. displayDatabaseParameter($installType, get_lang('Database Login'), 'dbUsernameForm', $dbUsernameForm, $example_login); ?>
  1296. </div>
  1297. <div class="form-group row">
  1298. <?php
  1299. //database user password
  1300. $example_password = get_lang('ex.').' '.api_generate_password();
  1301. displayDatabaseParameter($installType, get_lang('Database Password'), 'dbPassForm', $dbPassForm, $example_password); ?>
  1302. </div>
  1303. <div class="form-group row">
  1304. <?php
  1305. //Database Name fix replace weird chars
  1306. if ($installType != INSTALL_TYPE_UPDATE) {
  1307. $dbNameForm = str_replace(['-', '*', '$', ' ', '.'], '', $dbNameForm);
  1308. }
  1309. displayDatabaseParameter(
  1310. $installType,
  1311. get_lang('Main Chamilo database (DB)'),
  1312. 'dbNameForm',
  1313. $dbNameForm,
  1314. '&nbsp;',
  1315. null,
  1316. 'id="optional_param1"'
  1317. ); ?>
  1318. </div>
  1319. <?php if ($installType != INSTALL_TYPE_UPDATE) {
  1320. ?>
  1321. <div class="form-group row">
  1322. <div class="col-sm-4"></div>
  1323. <div class="col-sm-8">
  1324. <button type="submit" class="btn btn-primary" name="step3" value="step3">
  1325. <em class="fa fa-refresh"> </em>
  1326. <?php echo get_lang('Check database connection'); ?>
  1327. </button>
  1328. </div>
  1329. </div>
  1330. <?php
  1331. } ?>
  1332. </div>
  1333. </div>
  1334. <?php
  1335. $database_exists_text = '';
  1336. $manager = null;
  1337. try {
  1338. if ($installType === 'update') {
  1339. /** @var \Database $manager */
  1340. $manager = connectToDatabase(
  1341. $dbHostForm,
  1342. $dbUsernameForm,
  1343. $dbPassForm,
  1344. $dbNameForm,
  1345. $dbPortForm
  1346. );
  1347. $connection = $manager->getConnection();
  1348. $connection->connect();
  1349. $schemaManager = $connection->getSchemaManager();
  1350. // Test create/alter/drop table
  1351. $table = 'zXxTESTxX_'.mt_rand(0, 1000);
  1352. $sql = "CREATE TABLE $table (id INT AUTO_INCREMENT NOT NULL, name varchar(255), PRIMARY KEY(id))";
  1353. $connection->query($sql);
  1354. $tableCreationWorks = false;
  1355. $tableDropWorks = false;
  1356. if ($schemaManager->tablesExist($table)) {
  1357. $tableCreationWorks = true;
  1358. $sql = "ALTER TABLE $table ADD COLUMN name2 varchar(140) ";
  1359. $connection->query($sql);
  1360. $schemaManager->dropTable($table);
  1361. $tableDropWorks = $schemaManager->tablesExist($table) === false;
  1362. }
  1363. } else {
  1364. $manager = connectToDatabase(
  1365. $dbHostForm,
  1366. $dbUsernameForm,
  1367. $dbPassForm,
  1368. null,
  1369. $dbPortForm
  1370. );
  1371. $schemaManager = $manager->getConnection()->getSchemaManager();
  1372. $databases = $schemaManager->listDatabases();
  1373. if (in_array($dbNameForm, $databases)) {
  1374. $database_exists_text = '<div class="alert alert-warning">'.get_lang('A database with the same name <b>already exists</b>.').'</div>';
  1375. }
  1376. }
  1377. } catch (Exception $e) {
  1378. $database_exists_text = $e->getMessage();
  1379. $manager = false;
  1380. }
  1381. if ($manager && $manager->getConnection()->isConnected()): ?>
  1382. <?php echo $database_exists_text; ?>
  1383. <div id="db_status" class="alert alert-success">
  1384. Database host: <strong><?php echo $manager->getConnection()->getHost(); ?></strong><br/>
  1385. Database port: <strong><?php echo $manager->getConnection()->getPort(); ?></strong><br/>
  1386. Database driver: <strong><?php echo $manager->getConnection()->getDriver()->getName(); ?></strong><br/>
  1387. <?php
  1388. if ($installType === 'update') {
  1389. echo get_lang('CreateTableWorks').' <strong>Ok</strong>';
  1390. echo '<br/ >';
  1391. echo get_lang('AlterTableWorks').' <strong>Ok</strong>';
  1392. echo '<br/ >';
  1393. echo get_lang('DropColumnWorks').' <strong>Ok</strong>';
  1394. } ?>
  1395. </div>
  1396. <?php else: ?>
  1397. <div id="db_status" class="alert alert-danger">
  1398. <p><?php echo get_lang('The database connection has failed. This is generally due to the wrong user, the wrong password or the wrong database prefix being set above. Please review these settings and try again.'); ?></strong></p>
  1399. <code><?php echo $database_exists_text; ?></code>
  1400. </div>
  1401. <?php endif; ?>
  1402. <div class="btn-group" role="group">
  1403. <button type="submit" name="step2"
  1404. class="btn btn-secondary float-right" value="&lt; <?php echo get_lang('Previous'); ?>" >
  1405. <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
  1406. </button>
  1407. <input type="hidden" name="is_executable" id="is_executable" value="-" />
  1408. <?php if ($manager) {
  1409. ?>
  1410. <button type="submit" class="btn btn-success" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
  1411. <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
  1412. </button>
  1413. <?php
  1414. } else {
  1415. ?>
  1416. <button
  1417. disabled="disabled"
  1418. type="submit" class="btn btn-success disabled" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
  1419. <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
  1420. </button>
  1421. <?php
  1422. } ?>
  1423. </div>
  1424. <?php
  1425. }
  1426. function panel($content = null, $title = null, $id = null, $style = null)
  1427. {
  1428. $html = '';
  1429. if (empty($style)) {
  1430. $style = 'default';
  1431. }
  1432. if (!empty($title)) {
  1433. $panelTitle = Display::div($title, ['class' => 'panel-heading']);
  1434. $panelBody = Display::div($content, ['class' => 'panel-body']);
  1435. $panelParent = Display::div($panelTitle.$panelBody, ['id' => $id, 'class' => 'panel panel-'.$style]);
  1436. } else {
  1437. $panelBody = Display::div($html, ['class' => 'panel-body']);
  1438. $panelParent = Display::div($panelBody, ['id' => $id, 'class' => 'panel panel-'.$style]);
  1439. }
  1440. $html .= $panelParent;
  1441. return $html;
  1442. }
  1443. /**
  1444. * Displays a parameter in a table row.
  1445. * Used by the display_configuration_settings_form function.
  1446. *
  1447. * @param string $installType
  1448. * @param string $parameterName
  1449. * @param string $formFieldName
  1450. * @param string $parameterValue
  1451. * @param string $displayWhenUpdate
  1452. *
  1453. * @return string
  1454. */
  1455. function display_configuration_parameter(
  1456. $installType,
  1457. $parameterName,
  1458. $formFieldName,
  1459. $parameterValue,
  1460. $displayWhenUpdate = 'true'
  1461. ) {
  1462. $html = '<div class="form-group row">';
  1463. $html .= '<label class="col-sm-6 control-label">'.$parameterName.'</label>';
  1464. if ($installType == INSTALL_TYPE_UPDATE && $displayWhenUpdate) {
  1465. $html .= '<input type="hidden" name="'.$formFieldName.'" value="'.api_htmlentities($parameterValue, ENT_QUOTES).'" />'.$parameterValue;
  1466. } else {
  1467. $html .= '<div class="col-sm-6"><input class="form-control" type="text" size="'.FORM_FIELD_DISPLAY_LENGTH.'" maxlength="'.MAX_FORM_FIELD_LENGTH.'" name="'.$formFieldName.'" value="'.api_htmlentities($parameterValue, ENT_QUOTES).'" />'."</div>";
  1468. }
  1469. $html .= "</div>";
  1470. return $html;
  1471. }
  1472. /**
  1473. * Displays step 4 of the installation - configuration settings about Chamilo itself.
  1474. *
  1475. * @param string $installType
  1476. * @param string $urlForm
  1477. * @param string $languageForm
  1478. * @param string $emailForm
  1479. * @param string $adminFirstName
  1480. * @param string $adminLastName
  1481. * @param string $adminPhoneForm
  1482. * @param string $campusForm
  1483. * @param string $institutionForm
  1484. * @param string $institutionUrlForm
  1485. * @param string $encryptPassForm
  1486. * @param bool $allowSelfReg
  1487. * @param bool $allowSelfRegProf
  1488. * @param string $loginForm
  1489. * @param string $passForm
  1490. */
  1491. function display_configuration_settings_form(
  1492. $installType,
  1493. $urlForm,
  1494. $languageForm,
  1495. $emailForm,
  1496. $adminFirstName,
  1497. $adminLastName,
  1498. $adminPhoneForm,
  1499. $campusForm,
  1500. $institutionForm,
  1501. $institutionUrlForm,
  1502. $encryptPassForm,
  1503. $allowSelfReg,
  1504. $allowSelfRegProf,
  1505. $loginForm,
  1506. $passForm
  1507. ) {
  1508. if ($installType != 'update' && empty($languageForm)) {
  1509. $languageForm = $_SESSION['install_language'];
  1510. }
  1511. echo '<div class="RequirementHeading">';
  1512. echo "<h2>".display_step_sequence().get_lang("ConfigurationSettings")."</h2>";
  1513. echo '</div>';
  1514. // Parameter 1: administrator's login
  1515. $html = '';
  1516. $html .= display_configuration_parameter(
  1517. $installType,
  1518. get_lang('Administrator login'),
  1519. 'loginForm',
  1520. $loginForm,
  1521. $installType == 'update'
  1522. );
  1523. // Parameter 2: administrator's password
  1524. if ($installType != 'update') {
  1525. $html .= display_configuration_parameter($installType, get_lang('Administrator password (<font color="red">you may want to change this</font>)'), 'passForm', $passForm, false);
  1526. }
  1527. // Parameters 3 and 4: administrator's names
  1528. $html .= display_configuration_parameter(
  1529. $installType,
  1530. get_lang('Administrator first name'),
  1531. 'adminFirstName',
  1532. $adminFirstName
  1533. );
  1534. $html .= display_configuration_parameter($installType, get_lang('Administrator last name'), 'adminLastName', $adminLastName);
  1535. //Parameter 3: administrator's email
  1536. $html .= display_configuration_parameter($installType, get_lang('Admine-mail'), 'emailForm', $emailForm);
  1537. //Parameter 6: administrator's telephone
  1538. $html .= display_configuration_parameter($installType, get_lang('Administrator telephone'), 'adminPhoneForm', $adminPhoneForm);
  1539. echo panel($html, get_lang('Administrator'), 'administrator');
  1540. //First parameter: language
  1541. $html = '<div class="form-group row">';
  1542. $html .= '<label class="col-sm-6 control-label">'.get_lang('Main language')."</label>";
  1543. if ($installType == 'update') {
  1544. $html .= '<input type="hidden" name="languageForm" value="'.api_htmlentities($languageForm, ENT_QUOTES).'" />'.$languageForm;
  1545. } else { // new installation
  1546. $html .= '<div class="col-sm-6">';
  1547. $html .= display_language_selection_box('languageForm', $languageForm);
  1548. $html .= '</div>';
  1549. }
  1550. $html .= "</div>";
  1551. //Second parameter: Chamilo URL
  1552. $html .= '<div class="form-group row">';
  1553. $html .= '<label class="col-sm-6 control-label">'.get_lang('Chamilo URL').get_lang('Required field').'</label>';
  1554. if ($installType == 'update') {
  1555. $html .= api_htmlentities($urlForm, ENT_QUOTES)."\n";
  1556. } else {
  1557. $html .= '<div class="col-sm-6">';
  1558. $html .= '<input class="form-control" type="text" size="40" maxlength="100" name="urlForm" value="'.api_htmlentities($urlForm, ENT_QUOTES).'" />';
  1559. $html .= '</div>';
  1560. }
  1561. $html .= '</div>';
  1562. //Parameter 9: campus name
  1563. $html .= display_configuration_parameter(
  1564. $installType,
  1565. get_lang('Your portal name'),
  1566. 'campusForm',
  1567. $campusForm
  1568. );
  1569. //Parameter 10: institute (short) name
  1570. $html .= display_configuration_parameter(
  1571. $installType,
  1572. get_lang('Your company short name'),
  1573. 'institutionForm',
  1574. $institutionForm
  1575. );
  1576. //Parameter 11: institute (short) name
  1577. $html .= display_configuration_parameter(
  1578. $installType,
  1579. get_lang('URL of this company'),
  1580. 'institutionUrlForm',
  1581. $institutionUrlForm
  1582. );
  1583. $html .= '<div class="form-group row">
  1584. <label class="col-sm-6 control-label">'.get_lang("Encryption method").'</label>
  1585. <div class="col-sm-6">';
  1586. if ($installType == 'update') {
  1587. $html .= '<input type="hidden" name="encryptPassForm" value="'.$encryptPassForm.'" />'.$encryptPassForm;
  1588. } else {
  1589. $html .= '<div class="checkbox">
  1590. <label>
  1591. <input type="radio" name="encryptPassForm" value="bcrypt" id="encryptPass1" '.($encryptPassForm == 'bcrypt' ? 'checked="checked" ' : '').'/> bcrypt
  1592. </label>';
  1593. $html .= '<label>
  1594. <input type="radio" name="encryptPassForm" value="sha1" id="encryptPass1" '.($encryptPassForm == 'sha1' ? 'checked="checked" ' : '').'/> sha1
  1595. </label>';
  1596. $html .= '<label>
  1597. <input type="radio" name="encryptPassForm" value="md5" id="encryptPass0" '.($encryptPassForm == 'md5' ? 'checked="checked" ' : '').'/> md5
  1598. </label>';
  1599. $html .= '<label>
  1600. <input type="radio" name="encryptPassForm" value="none" id="encryptPass2" '.($encryptPassForm == 'none' ? 'checked="checked" ' : '').'/>'.get_lang('none').'
  1601. </label>';
  1602. $html .= '</div>';
  1603. }
  1604. $html .= '</div></div>';
  1605. $html .= '<div class="form-group row">
  1606. <label class="col-sm-6 control-label">'.get_lang('Allow self-registration').'</label>
  1607. <div class="col-sm-6">';
  1608. if ($installType == 'update') {
  1609. if ($allowSelfReg == 'true') {
  1610. $label = get_lang('Yes');
  1611. } elseif ($allowSelfReg == 'false') {
  1612. $label = get_lang('No');
  1613. } else {
  1614. $label = get_lang('After approval');
  1615. }
  1616. $html .= '<input type="hidden" name="allowSelfReg" value="'.$allowSelfReg.'" />'.$label;
  1617. } else {
  1618. $html .= '<div class="control-group">';
  1619. $html .= '<label class="checkbox-inline">
  1620. <input type="radio" name="allowSelfReg" value="true" id="allowSelfReg1" '.($allowSelfReg == 'true' ? 'checked="checked" ' : '').' /> '.get_lang('Yes').'
  1621. </label>';
  1622. $html .= '<label class="checkbox-inline">
  1623. <input type="radio" name="allowSelfReg" value="false" id="allowSelfReg0" '.($allowSelfReg == 'false' ? '' : 'checked="checked" ').' /> '.get_lang('No').'
  1624. </label>';
  1625. $html .= '<label class="checkbox-inline">
  1626. <input type="radio" name="allowSelfReg" value="approval" id="allowSelfReg2" '.($allowSelfReg == 'approval' ? '' : 'checked="checked" ').' /> '.get_lang('After approval').'
  1627. </label>';
  1628. $html .= '</div>';
  1629. }
  1630. $html .= '</div>';
  1631. $html .= '</div>';
  1632. $html .= '<div class="form-group row">';
  1633. $html .= '<label class="col-sm-6 control-label">'.get_lang('Allow self-registrationProf').'</label>
  1634. <div class="col-sm-6">';
  1635. if ($installType == 'update') {
  1636. if ($allowSelfRegProf == 'true') {
  1637. $label = get_lang('Yes');
  1638. } else {
  1639. $label = get_lang('No');
  1640. }
  1641. $html .= '<input type="hidden" name="allowSelfRegProf" value="'.$allowSelfRegProf.'" />'.$label;
  1642. } else {
  1643. $html .= '<div class="control-group">
  1644. <label class="checkbox-inline">
  1645. <input type="radio" name="allowSelfRegProf" value="1" id="allowSelfRegProf1" '.($allowSelfRegProf ? 'checked="checked" ' : '').'/>
  1646. '.get_lang('Yes').'
  1647. </label>';
  1648. $html .= '<label class="checkbox-inline">
  1649. <input type="radio" name="allowSelfRegProf" value="0" id="allowSelfRegProf0" '.($allowSelfRegProf ? '' : 'checked="checked" ').' />
  1650. '.get_lang('No').'
  1651. </label>';
  1652. $html .= '</div>';
  1653. }
  1654. $html .= '</div>
  1655. </div>';
  1656. echo panel($html, get_lang('Portal'), 'platform'); ?>
  1657. <div class='btn-group'>
  1658. <button type="submit" class="btn btn-secondary pull-right" name="step3" value="&lt; <?php echo get_lang('Previous'); ?>" ><em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?></button>
  1659. <input type="hidden" name="is_executable" id="is_executable" value="-" />
  1660. <button class="btn btn-success" type="submit" name="step5">
  1661. <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
  1662. </button>
  1663. </div>
  1664. <?php
  1665. }
  1666. /**
  1667. * After installation is completed (step 6), this message is displayed.
  1668. */
  1669. function display_after_install_message()
  1670. {
  1671. $container = Container::$container;
  1672. $trans = $container->get('translator');
  1673. $html = '<div class="RequirementContent">'.
  1674. $trans->trans(
  1675. 'When you enter your portal for the first time, the best way to understand it is to create a course with the \'Create course\' link in the menu and play around a little.').'</div>';
  1676. $html .= '<div class="alert alert-warning">';
  1677. $html .= '<strong>'.$trans->trans('Security advice').'</strong>';
  1678. $html .= ': ';
  1679. $html .= sprintf($trans->trans(
  1680. 'To protect your site, make the whole %s directory read-only (chmod -R 0555 on Linux) and delete the %s directory.'), 'var/config/', 'main/install/');
  1681. $html .= '</div></form>
  1682. <br />
  1683. <a class="btn btn-success btn-block" href="../../index.php">
  1684. '.$trans->trans('Go to your newly created portal.').'
  1685. </a>';
  1686. return $html;
  1687. }
  1688. /**
  1689. * This function return countries list from array (hardcoded).
  1690. *
  1691. * @param bool $combo (Optional) True for returning countries list with select html
  1692. *
  1693. * @return array|string countries list
  1694. */
  1695. function get_countries_list_from_array($combo = false)
  1696. {
  1697. $a_countries = [
  1698. "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan",
  1699. "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
  1700. "Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Republic", "Chad", "Chile", "China", "Colombi", "Comoros", "Congo (Brazzaville)", "Congo", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus", "Czech Republic",
  1701. "Denmark", "Djibouti", "Dominica", "Dominican Republic",
  1702. "East Timor (Timor Timur)", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
  1703. "Fiji", "Finland", "France",
  1704. "Gabon", "Gambia, The", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana",
  1705. "Haiti", "Honduras", "Hungary",
  1706. "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy",
  1707. "Jamaica", "Japan", "Jordan",
  1708. "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan",
  1709. "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
  1710. "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Morocco", "Mozambique", "Myanmar",
  1711. "Namibia", "Nauru", "Nepa", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway",
  1712. "Oman",
  1713. "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal",
  1714. "Qatar",
  1715. "Romania", "Russia", "Rwanda",
  1716. "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia and Montenegro", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria",
  1717. "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu",
  1718. "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan",
  1719. "Vanuatu", "Vatican City", "Venezuela", "Vietnam",
  1720. "Yemen",
  1721. "Zambia", "Zimbabwe",
  1722. ];
  1723. if ($combo) {
  1724. $country_select = '<select class="selectpicker show-tick" id="country" name="country">';
  1725. $country_select .= '<option value="">--- '.get_lang('Select one').' ---</option>';
  1726. foreach ($a_countries as $country) {
  1727. $country_select .= '<option value="'.$country.'">'.$country.'</option>';
  1728. }
  1729. $country_select .= '</select>';
  1730. return $country_select;
  1731. }
  1732. return $a_countries;
  1733. }
  1734. /**
  1735. * Lock settings that can't be changed in other portals.
  1736. */
  1737. function lockSettings()
  1738. {
  1739. $settings = api_get_locked_settings();
  1740. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  1741. foreach ($settings as $setting) {
  1742. $sql = "UPDATE $table SET access_url_locked = 1 WHERE variable = '$setting'";
  1743. Database::query($sql);
  1744. }
  1745. }
  1746. /**
  1747. * Update dir values.
  1748. */
  1749. function updateDirAndFilesPermissions()
  1750. {
  1751. $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  1752. $permissions_for_new_directories = isset($_SESSION['permissions_for_new_directories']) ? $_SESSION['permissions_for_new_directories'] : 0770;
  1753. $permissions_for_new_files = isset($_SESSION['permissions_for_new_files']) ? $_SESSION['permissions_for_new_files'] : 0660;
  1754. // use decoct() to store as string
  1755. Database::update(
  1756. $table,
  1757. ['selected_value' => '0'.decoct($permissions_for_new_directories)],
  1758. ['variable = ?' => 'permissions_for_new_directories']
  1759. );
  1760. Database::update(
  1761. $table,
  1762. ['selected_value' => '0'.decoct($permissions_for_new_files)],
  1763. ['variable = ?' => 'permissions_for_new_files']
  1764. );
  1765. if (isset($_SESSION['permissions_for_new_directories'])) {
  1766. unset($_SESSION['permissions_for_new_directories']);
  1767. }
  1768. if (isset($_SESSION['permissions_for_new_files'])) {
  1769. unset($_SESSION['permissions_for_new_files']);
  1770. }
  1771. }
  1772. /**
  1773. * @param $current_value
  1774. * @param $wanted_value
  1775. *
  1776. * @return string
  1777. */
  1778. function compare_setting_values($current_value, $wanted_value)
  1779. {
  1780. $current_value_string = $current_value;
  1781. $current_value = (float) $current_value;
  1782. $wanted_value = (float) $wanted_value;
  1783. if ($current_value >= $wanted_value) {
  1784. return Display::label($current_value_string, 'success');
  1785. } else {
  1786. return Display::label($current_value_string, 'important');
  1787. }
  1788. }
  1789. /**
  1790. * Save settings values.
  1791. *
  1792. * @param string $organizationName
  1793. * @param string $organizationUrl
  1794. * @param string $siteName
  1795. * @param string $adminEmail
  1796. * @param string $adminLastName
  1797. * @param string $adminFirstName
  1798. * @param string $language
  1799. * @param string $allowRegistration
  1800. * @param string $allowTeacherSelfRegistration
  1801. * @param string $installationProfile The name of an installation profile file in main/install/profiles/
  1802. */
  1803. function installSettings(
  1804. $organizationName,
  1805. $organizationUrl,
  1806. $siteName,
  1807. $adminEmail,
  1808. $adminLastName,
  1809. $adminFirstName,
  1810. $language,
  1811. $allowRegistration,
  1812. $allowTeacherSelfRegistration,
  1813. $installationProfile = ''
  1814. ) {
  1815. $allowTeacherSelfRegistration = $allowTeacherSelfRegistration ? 'true' : 'false';
  1816. $settings = [
  1817. 'institution' => $organizationName,
  1818. 'institution_url' => $organizationUrl,
  1819. 'site_name' => $siteName,
  1820. 'administrator_email' => $adminEmail,
  1821. 'administrator_surname' => $adminLastName,
  1822. 'administrator_name' => $adminFirstName,
  1823. 'platform_language' => $language,
  1824. 'allow_registration' => $allowRegistration,
  1825. 'allow_registration_as_teacher' => $allowTeacherSelfRegistration,
  1826. ];
  1827. foreach ($settings as $variable => $value) {
  1828. $sql = "UPDATE settings_current
  1829. SET selected_value = '$value'
  1830. WHERE variable = '$variable'";
  1831. Database::query($sql);
  1832. }
  1833. installProfileSettings($installationProfile);
  1834. }
  1835. /**
  1836. * Executes DB changes based in the classes defined in
  1837. * src/CoreBundle/Migrations/Schema/*.
  1838. *
  1839. * @param string $chamiloVersion
  1840. * @param EntityManager $manager
  1841. *
  1842. * @throws \Doctrine\DBAL\DBALException
  1843. *
  1844. * @return bool
  1845. */
  1846. function migrate($chamiloVersion, EntityManager $manager)
  1847. {
  1848. $debug = true;
  1849. $connection = $manager->getConnection();
  1850. $config = new \Doctrine\DBAL\Migrations\Configuration\Configuration($connection);
  1851. // Table name that will store migrations log (will be created automatically,
  1852. // default name is: doctrine_migration_versions)
  1853. $config->setMigrationsTableName('version');
  1854. // Namespace of your migration classes, do not forget escape slashes, do not add last slash
  1855. $config->setMigrationsNamespace('Chamilo\CoreBundle\Migrations\Schema\V'.$chamiloVersion);
  1856. // Directory where your migrations are located
  1857. $versionPath = api_get_path(SYS_PATH).'src/CoreBundle/Migrations/Schema/V'.$chamiloVersion;
  1858. error_log("Reading files from dir: $versionPath");
  1859. $config->setMigrationsDirectory($versionPath);
  1860. // Load your migrations
  1861. $config->registerMigrationsFromDirectory($config->getMigrationsDirectory());
  1862. $migration = new \Doctrine\DBAL\Migrations\Migration($config);
  1863. $versions = $config->getMigrations();
  1864. /** @var Doctrine\DBAL\Migrations\Version $migrationItem */
  1865. foreach ($versions as $version) {
  1866. $version->getMigration()->setEntityManager($manager);
  1867. }
  1868. $to = null; // if $to == null then schema will be migrated to latest version
  1869. echo '<pre>';
  1870. try {
  1871. // Execute migration!
  1872. $migratedSQL = $migration->migrate($to);
  1873. if ($debug) {
  1874. foreach ($migratedSQL as $version => $sqlList) {
  1875. echo "VERSION: $version<br>";
  1876. echo '----------------------------------------------<br />';
  1877. $total = count($sqlList);
  1878. error_log("VERSION: $version");
  1879. error_log("# queries: $total");
  1880. $counter = 1;
  1881. foreach ($sqlList as $sql) {
  1882. echo "<code>$sql</code><br>";
  1883. error_log("$counter/$total : $sql");
  1884. $counter++;
  1885. }
  1886. }
  1887. echo "<br>DONE!<br>";
  1888. }
  1889. return true;
  1890. } catch (Exception $ex) {
  1891. if ($debug) {
  1892. echo "ERROR: {$ex->getMessage()}<br>";
  1893. return false;
  1894. }
  1895. }
  1896. echo '</pre>';
  1897. return false;
  1898. }
  1899. /**
  1900. * @param EntityManager $em
  1901. *
  1902. * @throws \Doctrine\DBAL\DBALException
  1903. */
  1904. function fixIds(EntityManager $em)
  1905. {
  1906. $connection = $em->getConnection();
  1907. $database = new Database();
  1908. $database->setManager($em);
  1909. $debug = true;
  1910. if ($debug) {
  1911. error_log('fixIds');
  1912. }
  1913. // Create temporary indexes to increase speed of the following operations
  1914. // Adding and removing indexes will usually take much less time than
  1915. // the execution without indexes of the queries in this function, particularly
  1916. // for large tables
  1917. $sql = "ALTER TABLE c_document ADD INDEX tmpidx_doc(c_id, id)";
  1918. $connection->executeQuery($sql);
  1919. $sql = "ALTER TABLE c_student_publication ADD INDEX tmpidx_stud (c_id, id)";
  1920. $connection->executeQuery($sql);
  1921. $sql = "ALTER TABLE c_quiz ADD INDEX tmpidx_quiz (c_id, id)";
  1922. $connection->executeQuery($sql);
  1923. $sql = "ALTER TABLE c_item_property ADD INDEX tmpidx_ip (to_group_id)";
  1924. $connection->executeQuery($sql);
  1925. $sql = "SELECT * FROM c_lp_item";
  1926. $result = $connection->fetchAll($sql);
  1927. foreach ($result as $item) {
  1928. $courseId = $item['c_id'];
  1929. $iid = isset($item['iid']) ? intval($item['iid']) : 0;
  1930. $ref = isset($item['ref']) ? intval($item['ref']) : 0;
  1931. $sql = null;
  1932. $newId = '';
  1933. switch ($item['item_type']) {
  1934. case TOOL_LINK:
  1935. $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref";
  1936. $data = $connection->fetchAssoc($sql);
  1937. if ($data) {
  1938. $newId = $data['iid'];
  1939. }
  1940. break;
  1941. case TOOL_STUDENTPUBLICATION:
  1942. $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
  1943. $data = $connection->fetchAssoc($sql);
  1944. if ($data) {
  1945. $newId = $data['iid'];
  1946. }
  1947. break;
  1948. case TOOL_QUIZ:
  1949. $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
  1950. $data = $connection->fetchAssoc($sql);
  1951. if ($data) {
  1952. $newId = $data['iid'];
  1953. }
  1954. break;
  1955. case TOOL_DOCUMENT:
  1956. $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
  1957. $data = $connection->fetchAssoc($sql);
  1958. if ($data) {
  1959. $newId = $data['iid'];
  1960. }
  1961. break;
  1962. case TOOL_FORUM:
  1963. $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND forum_id = $ref";
  1964. $data = $connection->fetchAssoc($sql);
  1965. if ($data) {
  1966. $newId = $data['iid'];
  1967. }
  1968. break;
  1969. case 'thread':
  1970. $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
  1971. $data = $connection->fetchAssoc($sql);
  1972. if ($data) {
  1973. $newId = $data['iid'];
  1974. }
  1975. break;
  1976. }
  1977. if (!empty($sql) && !empty($newId) && !empty($iid)) {
  1978. $sql = "UPDATE c_lp_item SET ref = $newId WHERE iid = $iid";
  1979. $connection->executeQuery($sql);
  1980. }
  1981. }
  1982. // Set NULL if session = 0
  1983. $sql = "UPDATE c_item_property SET session_id = NULL WHERE session_id = 0";
  1984. $connection->executeQuery($sql);
  1985. // Set NULL if group = 0
  1986. $sql = "UPDATE c_item_property SET to_group_id = NULL WHERE to_group_id = 0";
  1987. $connection->executeQuery($sql);
  1988. // Set NULL if insert_user_id = 0
  1989. $sql = "UPDATE c_item_property SET insert_user_id = NULL WHERE insert_user_id = 0";
  1990. $connection->executeQuery($sql);
  1991. // Delete session data of sessions that don't exist.
  1992. $sql = "DELETE FROM c_item_property
  1993. WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)";
  1994. $connection->executeQuery($sql);
  1995. // Delete group data of groups that don't exist.
  1996. $sql = "DELETE FROM c_item_property
  1997. WHERE to_group_id <> 0 AND to_group_id IS NOT NULL AND to_group_id NOT IN (SELECT DISTINCT iid FROM c_group_info)";
  1998. $connection->executeQuery($sql);
  1999. // This updates the group_id with c_group_info.iid instead of c_group_info.id
  2000. if ($debug) {
  2001. error_log('update iids');
  2002. }
  2003. $groupTableToFix = [
  2004. 'c_group_rel_user',
  2005. 'c_group_rel_tutor',
  2006. 'c_permission_group',
  2007. 'c_role_group',
  2008. 'c_survey_invitation',
  2009. 'c_attendance_calendar_rel_group',
  2010. ];
  2011. foreach ($groupTableToFix as $table) {
  2012. $sql = "SELECT * FROM $table";
  2013. $result = $connection->fetchAll($sql);
  2014. foreach ($result as $item) {
  2015. $iid = $item['iid'];
  2016. $courseId = $item['c_id'];
  2017. $groupId = intval($item['group_id']);
  2018. // Fix group id
  2019. if (!empty($groupId)) {
  2020. $sql = "SELECT * FROM c_group_info
  2021. WHERE c_id = $courseId AND id = $groupId
  2022. LIMIT 1";
  2023. $data = $connection->fetchAssoc($sql);
  2024. if (!empty($data)) {
  2025. $newGroupId = $data['iid'];
  2026. $sql = "UPDATE $table SET group_id = $newGroupId
  2027. WHERE iid = $iid";
  2028. $connection->executeQuery($sql);
  2029. } else {
  2030. // The group does not exists clean this record
  2031. $sql = "DELETE FROM $table WHERE iid = $iid";
  2032. $connection->executeQuery($sql);
  2033. }
  2034. }
  2035. }
  2036. }
  2037. // Fix c_item_property
  2038. if ($debug) {
  2039. error_log('update c_item_property');
  2040. }
  2041. $sql = "SELECT * FROM course";
  2042. $courseList = $connection->fetchAll($sql);
  2043. if ($debug) {
  2044. error_log('Getting course list');
  2045. }
  2046. $totalCourse = count($courseList);
  2047. $counter = 0;
  2048. foreach ($courseList as $courseData) {
  2049. $courseId = $courseData['id'];
  2050. if ($debug) {
  2051. error_log('Updating course: '.$courseData['code']);
  2052. }
  2053. $sql = "SELECT * FROM c_item_property WHERE c_id = $courseId";
  2054. $result = $connection->fetchAll($sql);
  2055. foreach ($result as $item) {
  2056. $sessionId = intval($item['session_id']);
  2057. $groupId = intval($item['to_group_id']);
  2058. $iid = $item['iid'];
  2059. $ref = $item['ref'];
  2060. // Fix group id
  2061. // Commented group id is already fixed in Version20150603181728.php
  2062. /*if (!empty($groupId)) {
  2063. $sql = "SELECT * FROM c_group_info
  2064. WHERE c_id = $courseId AND id = $groupId";
  2065. $data = $connection->fetchAssoc($sql);
  2066. if (!empty($data)) {
  2067. $newGroupId = $data['iid'];
  2068. $sql = "UPDATE c_item_property SET to_group_id = $newGroupId
  2069. WHERE iid = $iid";
  2070. $connection->executeQuery($sql);
  2071. } else {
  2072. // The group does not exists clean this record
  2073. $sql = "DELETE FROM c_item_property WHERE iid = $iid";
  2074. $connection->executeQuery($sql);
  2075. }
  2076. }*/
  2077. $sql = '';
  2078. $newId = '';
  2079. switch ($item['tool']) {
  2080. case TOOL_LINK:
  2081. $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
  2082. break;
  2083. case TOOL_STUDENTPUBLICATION:
  2084. $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
  2085. break;
  2086. case TOOL_QUIZ:
  2087. $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
  2088. break;
  2089. case TOOL_DOCUMENT:
  2090. $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
  2091. break;
  2092. case TOOL_FORUM:
  2093. $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND id = $ref";
  2094. break;
  2095. case 'thread':
  2096. $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND id = $ref";
  2097. break;
  2098. }
  2099. if (!empty($sql) && !empty($newId)) {
  2100. $data = $connection->fetchAssoc($sql);
  2101. if (isset($data['iid'])) {
  2102. $newId = $data['iid'];
  2103. }
  2104. $sql = "UPDATE c_item_property SET ref = $newId WHERE iid = $iid";
  2105. error_log($sql);
  2106. $connection->executeQuery($sql);
  2107. }
  2108. }
  2109. if ($debug) {
  2110. // Print a status in the log once in a while
  2111. error_log("Course process #$counter/$totalCourse");
  2112. }
  2113. $counter++;
  2114. }
  2115. if ($debug) {
  2116. error_log('update gradebook_link');
  2117. }
  2118. // Fix gradebook_link
  2119. $sql = "SELECT * FROM gradebook_link";
  2120. $result = $connection->fetchAll($sql);
  2121. foreach ($result as $item) {
  2122. $courseCode = $item['course_code'];
  2123. $categoryId = (int) $item['category_id'];
  2124. $sql = "SELECT * FROM course WHERE code = '$courseCode'";
  2125. $courseInfo = $connection->fetchAssoc($sql);
  2126. if (empty($courseInfo)) {
  2127. continue;
  2128. }
  2129. $courseId = $courseInfo['id'];
  2130. $ref = $item['ref_id'];
  2131. $iid = $item['id'];
  2132. $sql = '';
  2133. switch ($item['type']) {
  2134. case LINK_LEARNPATH:
  2135. $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
  2136. break;
  2137. case LINK_STUDENTPUBLICATION:
  2138. $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
  2139. break;
  2140. case LINK_EXERCISE:
  2141. $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
  2142. break;
  2143. case LINK_ATTENDANCE:
  2144. //$sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
  2145. break;
  2146. case LINK_FORUM_THREAD:
  2147. $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
  2148. break;
  2149. }
  2150. if (!empty($sql)) {
  2151. $data = $connection->fetchAssoc($sql);
  2152. if (isset($data) && isset($data['iid'])) {
  2153. $newId = $data['iid'];
  2154. $sql = "UPDATE gradebook_link SET ref_id = $newId
  2155. WHERE id = $iid AND course_code = '$courseCode' AND category_id = $categoryId ";
  2156. $connection->executeQuery($sql);
  2157. }
  2158. }
  2159. }
  2160. if ($debug) {
  2161. error_log('update groups');
  2162. }
  2163. $sql = 'SELECT * FROM groups';
  2164. $result = $connection->executeQuery($sql);
  2165. $groups = $result->fetchAll();
  2166. $oldGroups = [];
  2167. if (!empty($groups)) {
  2168. foreach ($groups as $group) {
  2169. if (empty($group['name'])) {
  2170. continue;
  2171. }
  2172. $params = [
  2173. 'name' => $group['name'],
  2174. 'description' => $group['description'],
  2175. 'group_type' => 1,
  2176. 'picture' => $group['picture_uri'],
  2177. 'url' => $group['url'],
  2178. 'visibility' => $group['visibility'],
  2179. 'updated_at' => $group['updated_on'],
  2180. 'created_at' => $group['created_on'],
  2181. ];
  2182. $connection->insert('usergroup', $params);
  2183. $id = $connection->lastInsertId('id');
  2184. $oldGroups[$group['id']] = $id;
  2185. }
  2186. }
  2187. if (!empty($oldGroups)) {
  2188. error_log('Moving group files');
  2189. foreach ($oldGroups as $oldId => $newId) {
  2190. $path = get_group_picture_path_by_id(
  2191. $oldId,
  2192. 'system'
  2193. );
  2194. if (!empty($path)) {
  2195. $newPath = str_replace(
  2196. "groups/$oldId/",
  2197. "groups/$newId/",
  2198. $path['dir']
  2199. );
  2200. $command = "mv {$path['dir']} $newPath ";
  2201. error_log("Executing $command");
  2202. system($command);
  2203. }
  2204. }
  2205. $sql = "SELECT * FROM group_rel_user";
  2206. $result = $connection->executeQuery($sql);
  2207. $dataList = $result->fetchAll();
  2208. if (!empty($dataList)) {
  2209. foreach ($dataList as $data) {
  2210. if (isset($oldGroups[$data['group_id']])) {
  2211. $data['group_id'] = $oldGroups[$data['group_id']];
  2212. $userId = $data['user_id'];
  2213. $sql = "SELECT id FROM user WHERE user_id = $userId";
  2214. $userResult = $connection->executeQuery($sql);
  2215. $userInfo = $userResult->fetch();
  2216. if (empty($userInfo)) {
  2217. continue;
  2218. }
  2219. $sql = "INSERT INTO usergroup_rel_user (usergroup_id, user_id, relation_type)
  2220. VALUES ('{$data['group_id']}', '{$userId}', '{$data['relation_type']}')";
  2221. $connection->executeQuery($sql);
  2222. }
  2223. }
  2224. }
  2225. $sql = "SELECT * FROM group_rel_group";
  2226. $result = $connection->executeQuery($sql);
  2227. $dataList = $result->fetchAll();
  2228. if (!empty($dataList)) {
  2229. foreach ($dataList as $data) {
  2230. if (isset($oldGroups[$data['group_id']]) && isset($oldGroups[$data['subgroup_id']])) {
  2231. $data['group_id'] = $oldGroups[$data['group_id']];
  2232. $data['subgroup_id'] = $oldGroups[$data['subgroup_id']];
  2233. $sql = "INSERT INTO usergroup_rel_usergroup (group_id, subgroup_id, relation_type)
  2234. VALUES ('{$data['group_id']}', '{$data['subgroup_id']}', '{$data['relation_type']}')";
  2235. $connection->executeQuery($sql);
  2236. }
  2237. }
  2238. }
  2239. $sql = "SELECT * FROM announcement_rel_group";
  2240. $result = $connection->executeQuery($sql);
  2241. $dataList = $result->fetchAll();
  2242. if (!empty($dataList)) {
  2243. foreach ($dataList as $data) {
  2244. if (isset($oldGroups[$data['group_id']])) {
  2245. // Deleting relation
  2246. $sql = "DELETE FROM announcement_rel_group WHERE group_id = {$data['group_id']}";
  2247. $connection->executeQuery($sql);
  2248. // Add new relation
  2249. $data['group_id'] = $oldGroups[$data['group_id']];
  2250. $sql = "INSERT INTO announcement_rel_group(group_id, announcement_id)
  2251. VALUES ('{$data['group_id']}', '{$data['announcement_id']}')";
  2252. $connection->executeQuery($sql);
  2253. }
  2254. }
  2255. }
  2256. $sql = "SELECT * FROM group_rel_tag";
  2257. $result = $connection->executeQuery($sql);
  2258. $dataList = $result->fetchAll();
  2259. if (!empty($dataList)) {
  2260. foreach ($dataList as $data) {
  2261. if (isset($oldGroups[$data['group_id']])) {
  2262. $data['group_id'] = $oldGroups[$data['group_id']];
  2263. $sql = "INSERT INTO usergroup_rel_tag (tag_id, usergroup_id)
  2264. VALUES ('{$data['tag_id']}', '{$data['group_id']}')";
  2265. $connection->executeQuery($sql);
  2266. }
  2267. }
  2268. }
  2269. }
  2270. if ($debug) {
  2271. error_log('update extra fields');
  2272. }
  2273. // Extra fields
  2274. $extraFieldTables = [
  2275. ExtraField::USER_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_USER_FIELD),
  2276. ExtraField::COURSE_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_COURSE_FIELD),
  2277. //ExtraField::LP_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_LP_FIELD),
  2278. ExtraField::SESSION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_SESSION_FIELD),
  2279. //ExtraField::CALENDAR_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
  2280. //ExtraField::QUESTION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
  2281. //ExtraField::USER_FIELD_TYPE => //Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD),
  2282. ];
  2283. foreach ($extraFieldTables as $type => $table) {
  2284. $sql = "SELECT * FROM $table ";
  2285. if ($debug) {
  2286. error_log($sql);
  2287. }
  2288. $result = $connection->query($sql);
  2289. $fields = $result->fetchAll();
  2290. foreach ($fields as $field) {
  2291. if ($debug) {
  2292. error_log("Loading field: ".$field['field_variable']);
  2293. }
  2294. $originalId = $field['id'];
  2295. $params = [
  2296. 'extra_field_type' => $type,
  2297. 'variable' => $field['field_variable'],
  2298. 'field_type' => $field['field_type'],
  2299. 'display_text' => $field['field_display_text'],
  2300. 'default_value' => $field['field_default_value'],
  2301. 'field_order' => $field['field_order'],
  2302. 'visible' => $field['field_visible'],
  2303. 'changeable' => $field['field_changeable'],
  2304. 'filter' => $field['field_filter'],
  2305. ];
  2306. $connection->insert('extra_field', $params);
  2307. $newExtraFieldId = $connection->lastInsertId();
  2308. $values = [];
  2309. $handlerId = null;
  2310. switch ($type) {
  2311. case ExtraField::USER_FIELD_TYPE:
  2312. $optionTable = Database::get_main_table(
  2313. TABLE_MAIN_USER_FIELD_OPTIONS
  2314. );
  2315. $valueTable = Database::get_main_table(
  2316. TABLE_MAIN_USER_FIELD_VALUES
  2317. );
  2318. $handlerId = 'user_id';
  2319. break;
  2320. case ExtraField::COURSE_FIELD_TYPE:
  2321. $optionTable = Database::get_main_table(
  2322. TABLE_MAIN_COURSE_FIELD_OPTIONS
  2323. );
  2324. $valueTable = Database::get_main_table(
  2325. TABLE_MAIN_COURSE_FIELD_VALUES
  2326. );
  2327. $handlerId = 'c_id';
  2328. break;
  2329. case ExtraField::SESSION_FIELD_TYPE:
  2330. $optionTable = Database::get_main_table(
  2331. TABLE_MAIN_SESSION_FIELD_OPTIONS
  2332. );
  2333. $valueTable = Database::get_main_table(
  2334. TABLE_MAIN_SESSION_FIELD_VALUES
  2335. );
  2336. $handlerId = 'session_id';
  2337. break;
  2338. }
  2339. if (!empty($optionTable)) {
  2340. $sql = "SELECT * FROM $optionTable WHERE field_id = $originalId ";
  2341. $result = $connection->query($sql);
  2342. $options = $result->fetchAll();
  2343. foreach ($options as $option) {
  2344. $params = [
  2345. 'display_text' => $option['option_display_text'],
  2346. 'field_id' => $newExtraFieldId,
  2347. 'option_order' => $option['option_order'],
  2348. 'option_value' => $option['option_value'],
  2349. ];
  2350. $connection->insert('extra_field_options', $params);
  2351. }
  2352. $sql = "SELECT * FROM $valueTable WHERE field_id = $originalId ";
  2353. $result = $connection->query($sql);
  2354. $values = $result->fetchAll();
  2355. if ($debug) {
  2356. error_log("Fetch all values for field");
  2357. }
  2358. }
  2359. if (!empty($values)) {
  2360. if ($debug) {
  2361. error_log("Saving field value in new table");
  2362. }
  2363. $k = 0;
  2364. foreach ($values as $value) {
  2365. if (isset($value[$handlerId])) {
  2366. // Insert without the use of the entity as it reduces
  2367. // speed to 2 records per second (much too slow)
  2368. $params = [
  2369. 'field_id' => $newExtraFieldId,
  2370. 'value' => $value['field_value'],
  2371. 'item_id' => $value[$handlerId],
  2372. ];
  2373. $connection->insert('extra_field_values', $params);
  2374. if ($debug && ($k % 10000 == 0)) {
  2375. error_log("Saving field $k");
  2376. }
  2377. $k++;
  2378. }
  2379. }
  2380. }
  2381. }
  2382. }
  2383. if ($debug) {
  2384. error_log('Remove index');
  2385. }
  2386. // Drop temporary indexes added to increase speed of this function's queries
  2387. $sql = "ALTER TABLE c_document DROP INDEX tmpidx_doc";
  2388. $connection->executeQuery($sql);
  2389. $sql = "ALTER TABLE c_student_publication DROP INDEX tmpidx_stud";
  2390. $connection->executeQuery($sql);
  2391. $sql = "ALTER TABLE c_quiz DROP INDEX tmpidx_quiz";
  2392. $connection->executeQuery($sql);
  2393. $sql = "ALTER TABLE c_item_property DROP INDEX tmpidx_ip";
  2394. $connection->executeQuery($sql);
  2395. if ($debug) {
  2396. error_log('Finish fixId function');
  2397. }
  2398. fixLpId($connection, true);
  2399. }
  2400. /**
  2401. * @param \Doctrine\DBAL\Connection $connection
  2402. * @param $debug
  2403. *
  2404. * @throws \Doctrine\DBAL\DBALException
  2405. */
  2406. function fixLpId($connection, $debug)
  2407. {
  2408. if ($debug) {
  2409. error_log('Fix lp.id lp.iids');
  2410. }
  2411. $sql = 'SELECT id, title, code FROM course';
  2412. $result = $connection->query($sql);
  2413. $courses = $result->fetchAll();
  2414. $sql = 'SELECT id FROM session';
  2415. $result = $connection->query($sql);
  2416. $sessions = $result->fetchAll();
  2417. $tblCLp = Database::get_course_table(TABLE_LP_MAIN);
  2418. $tblCLpItem = Database::get_course_table(TABLE_LP_ITEM);
  2419. $toolTable = Database::get_course_table(TABLE_TOOL_LIST);
  2420. if (!empty($sessions)) {
  2421. $sessions = array_column($sessions, 'id');
  2422. $sessions[] = 0;
  2423. } else {
  2424. $sessions = [0];
  2425. }
  2426. foreach ($courses as $course) {
  2427. $courseId = $course['id'];
  2428. $sql = "SELECT * FROM $tblCLp WHERE c_id = $courseId AND iid <> id ORDER by iid";
  2429. $result = $connection->query($sql);
  2430. if ($debug) {
  2431. error_log('-------------');
  2432. error_log("Entering Lps in course #$courseId");
  2433. error_log($sql);
  2434. }
  2435. $lpList = $result->fetchAll();
  2436. $myOnlyLpList = [];
  2437. if (!empty($lpList)) {
  2438. foreach ($lpList as $lpInfo) {
  2439. $oldId = $lpInfo['id'];
  2440. $sql = "SELECT * FROM $tblCLpItem WHERE c_id = $courseId AND lp_id = $oldId ORDER by iid";
  2441. $result = $connection->query($sql);
  2442. $items = $result->fetchAll();
  2443. $lpInfo['lp_list'] = $items;
  2444. $myOnlyLpList[] = $lpInfo;
  2445. }
  2446. }
  2447. if (!empty($myOnlyLpList)) {
  2448. foreach ($myOnlyLpList as $lpInfo) {
  2449. $lpIid = $lpInfo['iid'];
  2450. $oldId = $lpInfo['id'];
  2451. $items = $lpInfo['lp_list'];
  2452. if (empty($items)) {
  2453. continue;
  2454. }
  2455. $itemList = [];
  2456. foreach ($items as $subItem) {
  2457. $itemList[$subItem['id']] = $subItem['iid'];
  2458. }
  2459. $variablesToFix = [
  2460. 'parent_item_id',
  2461. 'next_item_id',
  2462. 'prerequisite',
  2463. 'previous_item_id',
  2464. ];
  2465. foreach ($sessions as $sessionId) {
  2466. $correctLink = "lp/lp_controller.php?action=view&lp_id=$lpIid&id_session=$sessionId";
  2467. $link = "newscorm/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
  2468. $secondLink = "lp/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
  2469. $sql = "UPDATE $toolTable
  2470. SET link = '$correctLink'
  2471. WHERE c_id = $courseId AND (link = '$link' OR link ='$secondLink')";
  2472. $connection->query($sql);
  2473. }
  2474. foreach ($items as $item) {
  2475. $itemIid = $item['iid'];
  2476. $itemId = $item['id'];
  2477. foreach ($variablesToFix as $variable) {
  2478. if (!empty($item[$variable]) && isset($itemList[$item[$variable]])) {
  2479. $newId = $itemList[$item[$variable]];
  2480. $sql = "UPDATE $tblCLpItem SET $variable = $newId
  2481. WHERE iid = $itemIid AND c_id = $courseId AND lp_id = $oldId";
  2482. $connection->query($sql);
  2483. }
  2484. }
  2485. if ($item['item_type'] == 'document' && !empty($item['path'])) {
  2486. $oldDocumentId = $item['path'];
  2487. $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $oldDocumentId";
  2488. $result = $connection->query($sql);
  2489. $document = $result->fetch();
  2490. if (!empty($document)) {
  2491. $newDocumentId = $document['iid'];
  2492. if (!empty($newDocumentId)) {
  2493. $sql = "UPDATE $tblCLpItem SET path = $newDocumentId
  2494. WHERE iid = $itemIid AND c_id = $courseId";
  2495. $connection->query($sql);
  2496. if ($debug) {
  2497. //error_log("Fix document: ");
  2498. //error_log($sql);
  2499. }
  2500. }
  2501. }
  2502. }
  2503. // c_lp_view
  2504. $sql = "UPDATE c_lp_view SET last_item = $itemIid
  2505. WHERE c_id = $courseId AND last_item = $itemId AND lp_id = $oldId";
  2506. $connection->query($sql);
  2507. // c_lp_item_view
  2508. $sql = "UPDATE c_lp_item_view SET lp_item_id = $itemIid
  2509. WHERE c_id = $courseId AND lp_item_id = $itemId";
  2510. $connection->query($sql);
  2511. // Update track_exercises
  2512. $sql = "UPDATE track_e_exercises SET orig_lp_item_id = $itemIid
  2513. WHERE c_id = $courseId AND orig_lp_id = $oldId AND orig_lp_item_id = $itemId";
  2514. $connection->query($sql);
  2515. // c_forum_thread
  2516. $sql = "UPDATE c_forum_thread SET lp_item_id = $itemIid
  2517. WHERE c_id = $courseId AND lp_item_id = $itemId";
  2518. $connection->query($sql);
  2519. // orig_lp_item_view_id
  2520. $sql = "SELECT * FROM c_lp_view
  2521. WHERE c_id = $courseId AND lp_id = $oldId";
  2522. $result = $connection->query($sql);
  2523. $itemViewList = $result->fetchAll();
  2524. if ($itemViewList) {
  2525. foreach ($itemViewList as $itemView) {
  2526. $userId = $itemView['user_id'];
  2527. $oldItemViewId = $itemView['id'];
  2528. $newItemView = $itemView['iid'];
  2529. if (empty($oldItemViewId)) {
  2530. continue;
  2531. }
  2532. $sql = "UPDATE track_e_exercises
  2533. SET orig_lp_item_view_id = $newItemView
  2534. WHERE
  2535. c_id = $courseId AND
  2536. orig_lp_id = $oldId AND
  2537. orig_lp_item_id = $itemIid AND
  2538. orig_lp_item_view_id = $oldItemViewId AND
  2539. exe_user_id = $userId
  2540. ";
  2541. $connection->query($sql);
  2542. /*$sql = "UPDATE c_lp_item_view
  2543. SET lp_view_id = $newItemView
  2544. WHERE
  2545. lp_view_id = $oldItemViewId AND
  2546. c_id = $courseId
  2547. ";
  2548. $connection->query($sql);*/
  2549. }
  2550. }
  2551. $sql = "UPDATE $tblCLpItem SET lp_id = $lpIid
  2552. WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
  2553. $connection->query($sql);
  2554. $sql = "UPDATE $tblCLpItem SET id = iid
  2555. WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
  2556. $connection->query($sql);
  2557. }
  2558. $sql = "UPDATE c_lp_view SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
  2559. $connection->query($sql);
  2560. $sql = "UPDATE c_forum_forum SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
  2561. $connection->query($sql);
  2562. // Update track_exercises.
  2563. $sql = "UPDATE track_e_exercises SET orig_lp_id = $lpIid
  2564. WHERE c_id = $courseId AND orig_lp_id = $oldId";
  2565. $connection->query($sql);
  2566. $sql = "UPDATE $tblCLp SET id = iid WHERE c_id = $courseId AND id = $oldId ";
  2567. $connection->query($sql);
  2568. }
  2569. }
  2570. }
  2571. if ($debug) {
  2572. error_log('END Fix lp.id lp.iids');
  2573. }
  2574. }
  2575. /**
  2576. * @param string $distFile
  2577. * @param string $envFile
  2578. * @param array $params
  2579. */
  2580. function updateEnvFile($distFile, $envFile, $params)
  2581. {
  2582. $requirements = [
  2583. 'DATABASE_HOST',
  2584. 'DATABASE_PORT',
  2585. 'DATABASE_NAME',
  2586. 'DATABASE_USER',
  2587. 'DATABASE_PASSWORD',
  2588. 'APP_INSTALLED',
  2589. 'APP_ENCRYPT_METHOD',
  2590. 'APP_URL_APPEND',
  2591. ];
  2592. foreach ($requirements as $requirement) {
  2593. if (!isset($params['{{'.$requirement.'}}'])) {
  2594. throw new \Exception("The parameter $requirement is needed in order to edit the .env.local file");
  2595. }
  2596. }
  2597. $contents = file_get_contents($distFile);
  2598. $contents = str_replace(array_keys($params), array_values($params), $contents);
  2599. file_put_contents($envFile, $contents);
  2600. }
  2601. /**
  2602. * @param SymfonyContainer $container
  2603. * @param EntityManager $manager
  2604. */
  2605. function installGroups($container, $manager)
  2606. {
  2607. // Creating fos_group (groups and roles)
  2608. $groupManager = $container->get('fos_user.group_manager');
  2609. $groups = [
  2610. [
  2611. 'code' => 'ADMIN',
  2612. 'title' => 'Administrators',
  2613. 'roles' => ['ROLE_ADMIN'],
  2614. ],
  2615. [
  2616. 'code' => 'STUDENT',
  2617. 'title' => 'Students',
  2618. 'roles' => ['ROLE_STUDENT'],
  2619. ],
  2620. [
  2621. 'code' => 'TEACHER',
  2622. 'title' => 'Teachers',
  2623. 'roles' => ['ROLE_TEACHER'],
  2624. ],
  2625. [
  2626. 'code' => 'RRHH',
  2627. 'title' => 'Human resources manager',
  2628. 'roles' => ['ROLE_RRHH'],
  2629. ],
  2630. [
  2631. 'code' => 'SESSION_MANAGER',
  2632. 'title' => 'Session',
  2633. 'roles' => ['ROLE_SESSION_MANAGER'],
  2634. ],
  2635. [
  2636. 'code' => 'QUESTION_MANAGER',
  2637. 'title' => 'Question manager',
  2638. 'roles' => ['ROLE_QUESTION_MANAGER'],
  2639. ],
  2640. [
  2641. 'code' => 'STUDENT_BOSS',
  2642. 'title' => 'Student boss',
  2643. 'roles' => ['ROLE_STUDENT_BOSS'],
  2644. ],
  2645. [
  2646. 'code' => 'INVITEE',
  2647. 'title' => 'Invitee',
  2648. 'roles' => ['ROLE_INVITEE'],
  2649. ],
  2650. ];
  2651. foreach ($groups as $groupData) {
  2652. $criteria = ['code' => $groupData['code']];
  2653. $groupExists = $groupManager->findGroupBy($criteria);
  2654. if (!$groupExists) {
  2655. $group = $groupManager->createGroup($groupData['title']);
  2656. $group->setCode($groupData['code']);
  2657. foreach ($groupData['roles'] as $role) {
  2658. $group->addRole($role);
  2659. }
  2660. $manager->persist($group);
  2661. $groupManager->updateGroup($group, true);
  2662. }
  2663. }
  2664. }
  2665. /**
  2666. * @param SymfonyContainer $container
  2667. */
  2668. function installPages($container)
  2669. {
  2670. error_log('installPages');
  2671. $siteManager = Container::getSiteManager();
  2672. // Create site
  2673. /** @var Chamilo\PageBundle\Entity\Site $site */
  2674. $site = $siteManager->create();
  2675. $site->setHost('localhost');
  2676. $site->setEnabled(true);
  2677. $site->setName('localhost');
  2678. $site->setEnabledFrom(new \DateTime('now'));
  2679. $site->setEnabledTo(new \DateTime('+20 years'));
  2680. $site->setRelativePath('');
  2681. $site->setIsDefault(true);
  2682. $site->setLocale('en');
  2683. $siteManager->save($site);
  2684. // Create home page
  2685. /** @var PageManager $pageManager */
  2686. $pageManager = $container->get('sonata.page.manager.page');
  2687. /** @var \Sonata\PageBundle\Model\Page $page */
  2688. $page = $pageManager->create();
  2689. $page->setSlug('homepage');
  2690. $page->setUrl('/');
  2691. $page->setName('homepage');
  2692. $page->setTitle('home');
  2693. $page->setEnabled(true);
  2694. $page->setDecorate(1);
  2695. $page->setRequestMethod('GET|POST|HEAD|DELETE|PUT');
  2696. $page->setTemplateCode('default');
  2697. $page->setRouteName('homepage');
  2698. //$page->setParent($this->getReference('page-homepage'));
  2699. $page->setSite($site);
  2700. $pageManager->save($page);
  2701. // Create welcome page
  2702. $pageWelcome = $pageManager->create();
  2703. $pageWelcome->setSlug('welcome');
  2704. $pageWelcome->setUrl('/welcome');
  2705. $pageWelcome->setName('welcome');
  2706. $pageWelcome->setTitle('welcome');
  2707. $pageWelcome->setEnabled(true);
  2708. $pageWelcome->setDecorate(1);
  2709. $pageWelcome->setRequestMethod('GET');
  2710. $pageWelcome->setTemplateCode('default');
  2711. $pageWelcome->setRouteName('welcome');
  2712. $pageWelcome->setParent($page);
  2713. $pageWelcome->setSite($site);
  2714. $pageManager->save($pageWelcome);
  2715. // Creating page blocks
  2716. $templateManager = $container->get('sonata.page.template_manager');
  2717. $template = $templateManager->get('default');
  2718. $templateContainers = $template->getContainers();
  2719. $containers = [];
  2720. foreach ($templateContainers as $id => $area) {
  2721. $containers[$id] = [
  2722. 'area' => $area,
  2723. 'block' => false,
  2724. ];
  2725. }
  2726. // Create blocks for this page
  2727. $blockInteractor = $container->get('sonata.page.block_interactor');
  2728. $parentBlock = null;
  2729. foreach ($containers as $id => $area) {
  2730. if (false === $area['block'] && $templateContainers[$id]['shared'] === false) {
  2731. $block = $blockInteractor->createNewContainer(
  2732. [
  2733. 'page' => $pageWelcome,
  2734. 'name' => $templateContainers[$id]['name'],
  2735. 'code' => $id,
  2736. ]
  2737. );
  2738. if ($id === 'content' && $templateContainers[$id]['name'] == 'Main content') {
  2739. $parentBlock = $block;
  2740. }
  2741. }
  2742. }
  2743. // Create block in main content
  2744. $block = $container->get('sonata.page.manager.block');
  2745. /** @var \Sonata\BlockBundle\Model\Block $myBlock */
  2746. $myBlock = $block->create();
  2747. $myBlock->setType('sonata.formatter.block.formatter');
  2748. $myBlock->setSetting('format', 'richhtml');
  2749. $myBlock->setSetting('content', '');
  2750. $myBlock->setSetting('rawContent', '');
  2751. $myBlock->setSetting('template', '@SonataFormatter/Block/block_formatter.html.twig');
  2752. $myBlock->setParent($parentBlock);
  2753. $pageWelcome->addBlocks($myBlock);
  2754. $pageManager->save($pageWelcome);
  2755. }
  2756. /**
  2757. * @param SymfonyContainer $container
  2758. * @param EntityManager $manager
  2759. * @param bool $upgrade
  2760. */
  2761. function installSchemas($container, $manager, $upgrade = false)
  2762. {
  2763. error_log('installSchemas');
  2764. $settingsManager = Container::getSettingsManager();
  2765. // Install course tools (table "tool")
  2766. $toolChain = $container->get('chamilo_core.tool_chain');
  2767. $toolChain->createTools($manager);
  2768. $urlRepo = $container->get('Chamilo\CoreBundle\Repository\AccessUrlRepository');
  2769. $accessUrl = $urlRepo->find(1);
  2770. if (!$accessUrl) {
  2771. $em = $urlRepo->getEntityManager();
  2772. // Creating AccessUrl
  2773. $accessUrl = new AccessUrl();
  2774. $accessUrl
  2775. ->setUrl('http://localhost/')
  2776. ->setDescription('')
  2777. ->setActive(1)
  2778. ;
  2779. $em->persist($accessUrl);
  2780. $em->flush();
  2781. }
  2782. if ($upgrade) {
  2783. $settingsManager->updateSchemas($accessUrl);
  2784. } else {
  2785. // Installing schemas (filling settings_current table)
  2786. $settingsManager->installSchemas($accessUrl);
  2787. }
  2788. }
  2789. /**
  2790. * @param SymfonyContainer $container
  2791. */
  2792. function upgradeWithContainer($container)
  2793. {
  2794. Container::setContainer($container);
  2795. Container::setLegacyServices($container, false);
  2796. error_log('setLegacyServices');
  2797. $manager = Database::getManager();
  2798. installGroups($container, $manager);
  2799. error_log('installGroups');
  2800. // @todo check if adminId = 1
  2801. installSchemas($container, $manager, 1, true);
  2802. installPages($container);
  2803. fixMedia($container);
  2804. }
  2805. function fixMedia($container)
  2806. {
  2807. error_log('fix medias');
  2808. $pool = $container->get('sonata.media.pool');
  2809. $contextManager = $container->get('sonata.classification.manager.context');
  2810. $categoryManager = $container->get('sonata.media.manager.category');
  2811. foreach ($pool->getContexts() as $context => $contextAttrs) {
  2812. /** @var ContextInterface $defaultContext */
  2813. $defaultContext = $contextManager->findOneBy([
  2814. 'id' => $context,
  2815. ]);
  2816. if (!$defaultContext) {
  2817. $defaultContext = $contextManager->create();
  2818. $defaultContext->setId($context);
  2819. $defaultContext->setName(ucfirst($context));
  2820. $defaultContext->setEnabled(true);
  2821. $contextManager->save($defaultContext);
  2822. }
  2823. $defaultCategory = $categoryManager->getRootCategory($defaultContext);
  2824. if (!$defaultCategory) {
  2825. $defaultCategory = $categoryManager->create();
  2826. $defaultCategory->setContext($defaultContext);
  2827. $defaultCategory->setName(ucfirst($context));
  2828. $defaultCategory->setEnabled(true);
  2829. $defaultCategory->setPosition(0);
  2830. $categoryManager->save($defaultCategory);
  2831. }
  2832. }
  2833. }
  2834. /**
  2835. * After the schema was created (table creation), the function adds
  2836. * admin/platform information.
  2837. *
  2838. * @param \Psr\Container\ContainerInterface $container
  2839. * @param string $sysPath
  2840. * @param string $encryptPassForm
  2841. * @param string $passForm
  2842. * @param string $adminLastName
  2843. * @param string $adminFirstName
  2844. * @param string $loginForm
  2845. * @param string $emailForm
  2846. * @param string $adminPhoneForm
  2847. * @param string $languageForm
  2848. * @param string $institutionForm
  2849. * @param string $institutionUrlForm
  2850. * @param string $siteName
  2851. * @param string $allowSelfReg
  2852. * @param string $allowSelfRegProf
  2853. * @param string $installationProfile Installation profile, if any was provided
  2854. */
  2855. function finishInstallationWithContainer(
  2856. $container,
  2857. $sysPath,
  2858. $encryptPassForm,
  2859. $passForm,
  2860. $adminLastName,
  2861. $adminFirstName,
  2862. $loginForm,
  2863. $emailForm,
  2864. $adminPhoneForm,
  2865. $languageForm,
  2866. $institutionForm,
  2867. $institutionUrlForm,
  2868. $siteName,
  2869. $allowSelfReg,
  2870. $allowSelfRegProf,
  2871. $installationProfile = ''
  2872. ) {
  2873. $sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYS_PATH);
  2874. Container::setContainer($container);
  2875. Container::setLegacyServices($container, false);
  2876. $manager = Database::getManager();
  2877. $connection = $manager->getConnection();
  2878. $sql = getVersionTable();
  2879. // Add version table
  2880. $connection->executeQuery($sql);
  2881. // Add tickets defaults
  2882. $ticketProject = new TicketProject();
  2883. $ticketProject
  2884. ->setId(1)
  2885. ->setName('Ticket System')
  2886. ->setInsertUserId(1);
  2887. $manager->persist($ticketProject);
  2888. $manager->flush();
  2889. $trans = $container->get('translator');
  2890. $categories = [
  2891. $trans->trans('Enrollment') => $trans->trans('Tickets about enrollment'),
  2892. $trans->trans('General information') => $trans->trans('Tickets about general information'),
  2893. $trans->trans('Requests and paperwork') => $trans->trans('Tickets about requests and paperwork'),
  2894. $trans->trans('Academic Incidents') => $trans->trans('Tickets about academic incidents, like exams, practices, tasks, etc.'),
  2895. $trans->trans('Virtual campus') => $trans->trans('Tickets about virtual campus'),
  2896. $trans->trans('Online evaluation') => $trans->trans('Tickets about online evaluation'),
  2897. ];
  2898. $i = 1;
  2899. foreach ($categories as $category => $description) {
  2900. // Online evaluation requires a course
  2901. $ticketCategory = new TicketCategory();
  2902. $ticketCategory
  2903. ->setId($i)
  2904. ->setName($category)
  2905. ->setDescription($description)
  2906. ->setProject($ticketProject)
  2907. ->setInsertUserId(1);
  2908. $isRequired = $i == 6;
  2909. $ticketCategory->setCourseRequired($isRequired);
  2910. $manager->persist($ticketCategory);
  2911. $manager->flush();
  2912. $i++;
  2913. }
  2914. // Default Priorities
  2915. $defaultPriorities = [
  2916. TicketManager::PRIORITY_NORMAL => $trans->trans('Normal'),
  2917. TicketManager::PRIORITY_HIGH => $trans->trans('High'),
  2918. TicketManager::PRIORITY_LOW => $trans->trans('Low'),
  2919. ];
  2920. $i = 1;
  2921. foreach ($defaultPriorities as $code => $priority) {
  2922. $ticketPriority = new TicketPriority();
  2923. $ticketPriority
  2924. ->setId($i)
  2925. ->setName($priority)
  2926. ->setCode($code)
  2927. ->setInsertUserId(1);
  2928. $manager->persist($ticketPriority);
  2929. $manager->flush();
  2930. $i++;
  2931. }
  2932. $table = Database::get_main_table(TABLE_TICKET_STATUS);
  2933. // Default status
  2934. $defaultStatus = [
  2935. TicketManager::STATUS_NEW => $trans->trans('New'),
  2936. TicketManager::STATUS_PENDING => $trans->trans('Pending'),
  2937. TicketManager::STATUS_UNCONFIRMED => $trans->trans('Unconfirmed'),
  2938. TicketManager::STATUS_CLOSE => $trans->trans('Close'),
  2939. TicketManager::STATUS_FORWARDED => $trans->trans('Forwarded'),
  2940. ];
  2941. $i = 1;
  2942. foreach ($defaultStatus as $code => $status) {
  2943. $attributes = [
  2944. 'id' => $i,
  2945. 'code' => $code,
  2946. 'name' => $status,
  2947. ];
  2948. Database::insert($table, $attributes);
  2949. $i++;
  2950. }
  2951. installGroups($container, $manager);
  2952. installSchemas($container, $manager, false);
  2953. installPages($container);
  2954. // Inserting default data
  2955. $data = file_get_contents($sysPath.'main/install/data.sql');
  2956. $result = $manager->getConnection()->prepare($data);
  2957. $result->execute();
  2958. $result->closeCursor();
  2959. UserManager::setPasswordEncryption($encryptPassForm);
  2960. // Create admin user.
  2961. $adminId = @UserManager::create_user(
  2962. $adminFirstName,
  2963. $adminLastName,
  2964. 1,
  2965. $emailForm,
  2966. $loginForm,
  2967. $passForm,
  2968. 'ADMIN', //$official_code = '',
  2969. $languageForm,
  2970. $adminPhoneForm,
  2971. '', //$picture_uri = '',
  2972. PLATFORM_AUTH_SOURCE,
  2973. '', //$expirationDate,
  2974. 1,
  2975. 0,
  2976. null,
  2977. '',
  2978. false, //$send_mail = false,
  2979. true, //$isAdmin = false
  2980. '',
  2981. false,
  2982. '',
  2983. 1
  2984. );
  2985. // Create anonymous user.
  2986. @UserManager::create_user(
  2987. 'Joe',
  2988. 'Anonymous',
  2989. 6,
  2990. 'anonymous@localhost',
  2991. 'anon',
  2992. 'anon',
  2993. 'anonymous', //$official_code = '',
  2994. $languageForm,
  2995. '',
  2996. '', //$picture_uri = '',
  2997. PLATFORM_AUTH_SOURCE,
  2998. '',
  2999. 1,
  3000. 0,
  3001. null,
  3002. '',
  3003. false, //$send_mail = false,
  3004. false, //$isAdmin = false
  3005. '',
  3006. false,
  3007. '',
  3008. 1
  3009. );
  3010. // Set default language
  3011. Database::update(
  3012. Database::get_main_table(TABLE_MAIN_LANGUAGE),
  3013. ['available' => 1],
  3014. ['dokeos_folder = ?' => $languageForm]
  3015. );
  3016. $userManager = Container::getUserManager();
  3017. $urlRepo = $container->get('Chamilo\CoreBundle\Repository\AccessUrlRepository');
  3018. $accessUrl = $urlRepo->find(1);
  3019. $admin = $userManager->find($adminId);
  3020. $urlRepo->addResourceNode($accessUrl, $admin);
  3021. // Install settings
  3022. installSettings(
  3023. $institutionForm,
  3024. $institutionUrlForm,
  3025. $siteName,
  3026. $emailForm,
  3027. $adminLastName,
  3028. $adminFirstName,
  3029. $languageForm,
  3030. $allowSelfReg,
  3031. $allowSelfRegProf,
  3032. $installationProfile
  3033. );
  3034. lockSettings();
  3035. updateDirAndFilesPermissions();
  3036. fixMedia($container);
  3037. // Set the latest version
  3038. /*$path = $sysPath.'app/Migrations/Schema/V111/';
  3039. $finder = new \Symfony\Component\Finder\Finder();
  3040. $files = $finder->files()->in($path);
  3041. // Needed for chash
  3042. createVersionTable();
  3043. foreach ($files as $version) {
  3044. $version = str_replace(['Version', '.php'], '', $version->getFilename());
  3045. $sql = "INSERT INTO version (version) VALUES ('$version')";
  3046. Database::query($sql);
  3047. }*/
  3048. }
  3049. /**
  3050. * Creates 'version' table.
  3051. */
  3052. function createVersionTable()
  3053. {
  3054. $sql = getVersionTable();
  3055. Database::query($sql);
  3056. }
  3057. /**
  3058. * Get version creation table query.
  3059. *
  3060. * @return string
  3061. */
  3062. function getVersionTable()
  3063. {
  3064. return 'CREATE TABLE IF NOT EXISTS version (id int unsigned NOT NULL AUTO_INCREMENT, version varchar(20), PRIMARY KEY(id), UNIQUE(version));';
  3065. }
  3066. /**
  3067. * Update settings based on installation profile defined in a JSON file.
  3068. *
  3069. * @param string $installationProfile The name of the JSON file in main/install/profiles/ folder
  3070. *
  3071. * @return bool false on failure (no bad consequences anyway, just ignoring profile)
  3072. */
  3073. function installProfileSettings($installationProfile = '')
  3074. {
  3075. if (empty($installationProfile)) {
  3076. return false;
  3077. }
  3078. $jsonPath = api_get_path(SYS_PATH).'main/install/profiles/'.$installationProfile.'.json';
  3079. // Make sure the path to the profile is not hacked
  3080. if (!Security::check_abs_path($jsonPath, api_get_path(SYS_PATH).'main/install/profiles/')) {
  3081. return false;
  3082. }
  3083. if (!is_file($jsonPath)) {
  3084. return false;
  3085. }
  3086. if (!is_readable($jsonPath)) {
  3087. return false;
  3088. }
  3089. if (!function_exists('json_decode')) {
  3090. // The php-json extension is not available. Ignore profile.
  3091. return false;
  3092. }
  3093. $json = file_get_contents($jsonPath);
  3094. $params = json_decode($json);
  3095. if ($params === false or $params === null) {
  3096. return false;
  3097. }
  3098. $settings = $params->params;
  3099. if (!empty($params->parent)) {
  3100. installProfileSettings($params->parent);
  3101. }
  3102. $tblSettings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  3103. foreach ($settings as $id => $param) {
  3104. $conditions = ['variable = ? ' => $param->variable];
  3105. if (!empty($param->subkey)) {
  3106. $conditions['AND subkey = ? '] = $param->subkey;
  3107. }
  3108. Database::update(
  3109. $tblSettings,
  3110. ['selected_value' => $param->selected_value],
  3111. $conditions
  3112. );
  3113. }
  3114. return true;
  3115. }
  3116. /**
  3117. * Quick function to remove a directory with its subdirectories.
  3118. *
  3119. * @param $dir
  3120. */
  3121. function rrmdir($dir)
  3122. {
  3123. if (is_dir($dir)) {
  3124. $objects = scandir($dir);
  3125. foreach ($objects as $object) {
  3126. if ($object != "." && $object != "..") {
  3127. if (filetype($dir."/".$object) == "dir") {
  3128. @rrmdir($dir."/".$object);
  3129. } else {
  3130. @unlink($dir."/".$object);
  3131. }
  3132. }
  3133. }
  3134. reset($objects);
  3135. rmdir($dir);
  3136. }
  3137. }
  3138. /**
  3139. * @param $id
  3140. * @param string $type
  3141. * @param bool $preview
  3142. * @param bool $anonymous
  3143. *
  3144. * @throws \Doctrine\DBAL\DBALException
  3145. *
  3146. * @return array
  3147. */
  3148. function get_group_picture_path_by_id($id, $type = 'web', $preview = false, $anonymous = false)
  3149. {
  3150. switch ($type) {
  3151. case 'system': // Base: absolute system path.
  3152. $base = api_get_path(SYS_UPLOAD_PATH);
  3153. break;
  3154. case 'web': // Base: absolute web path.
  3155. default:
  3156. $base = api_get_path(WEB_UPLOAD_PATH);
  3157. break;
  3158. }
  3159. $noPicturePath = ['dir' => $base.'img/', 'file' => 'unknown.jpg'];
  3160. if (empty($id) || empty($type)) {
  3161. return $anonymous ? $noPicturePath : ['dir' => '', 'file' => ''];
  3162. }
  3163. $id = intval($id);
  3164. //$group_table = Database::get_main_table(TABLE_MAIN_GROUP);
  3165. $group_table = 'groups';
  3166. $sql = "SELECT picture_uri FROM $group_table WHERE id=".$id;
  3167. $res = Database::query($sql);
  3168. if (!Database::num_rows($res)) {
  3169. return $anonymous ? $noPicturePath : ['dir' => '', 'file' => ''];
  3170. }
  3171. $user = Database::fetch_array($res);
  3172. $picture_filename = trim($user['picture_uri']);
  3173. if (api_get_setting('split_users_upload_directory') === 'true') {
  3174. if (!empty($picture_filename)) {
  3175. $dir = $base.'groups/'.substr($picture_filename, 0, 1).'/'.$id.'/';
  3176. } elseif ($preview) {
  3177. $dir = $base.'groups/'.substr((string) $id, 0, 1).'/'.$id.'/';
  3178. } else {
  3179. $dir = $base.'groups/'.$id.'/';
  3180. }
  3181. } else {
  3182. $dir = $base.'groups/'.$id.'/';
  3183. }
  3184. if (empty($picture_filename) && $anonymous) {
  3185. return $noPicturePath;
  3186. }
  3187. return ['dir' => $dir, 'file' => $picture_filename];
  3188. }
  3189. /**
  3190. * Control the different steps of the migration through a big switch.
  3191. *
  3192. * @param string $fromVersion
  3193. * @param EntityManager $manager
  3194. * @param bool $processFiles
  3195. *
  3196. * @throws \Doctrine\DBAL\DBALException
  3197. *
  3198. * @return bool Always returns true except if the process is broken
  3199. */
  3200. function migrateSwitch($fromVersion, $manager, $processFiles = true)
  3201. {
  3202. error_log('Starting migration process from '.$fromVersion.' ('.date('Y-m-d H:i:s').')');
  3203. echo '<a class="btn btn-secondary" href="javascript:void(0)" id="details_button">'.get_lang('Details').'</a><br />';
  3204. echo '<div id="details" style="display:none">';
  3205. $connection = $manager->getConnection();
  3206. $database = new Database();
  3207. $database->setManager($manager);
  3208. switch ($fromVersion) {
  3209. case '1.9.0':
  3210. case '1.9.2':
  3211. case '1.9.4':
  3212. case '1.9.6':
  3213. case '1.9.6.1':
  3214. case '1.9.8':
  3215. case '1.9.8.1':
  3216. case '1.9.8.2':
  3217. case '1.9.10':
  3218. case '1.9.10.2':
  3219. case '1.9.10.4':
  3220. case '1.9.10.6':
  3221. $database = new Database();
  3222. $database->setManager($manager);
  3223. // Fix type "enum" before running the migration with Doctrine
  3224. $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_course_child VARCHAR(40) DEFAULT 'TRUE'");
  3225. $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_cat_child VARCHAR(40) DEFAULT 'TRUE'");
  3226. $connection->executeQuery('ALTER TABLE c_quiz_answer MODIFY COLUMN hotspot_type varchar(40) default NULL');
  3227. $connection->executeQuery("ALTER TABLE c_tool MODIFY COLUMN target varchar(20) NOT NULL default '_self'");
  3228. $connection->executeQuery("ALTER TABLE c_link MODIFY COLUMN on_homepage char(10) NOT NULL default '0'");
  3229. $connection->executeQuery("ALTER TABLE c_blog_rating MODIFY COLUMN rating_type char(40) NOT NULL default 'post'");
  3230. $connection->executeQuery("ALTER TABLE c_survey MODIFY COLUMN anonymous char(10) NOT NULL default '0'");
  3231. $connection->executeQuery("ALTER TABLE c_document MODIFY COLUMN filetype char(10) NOT NULL default 'file'");
  3232. $connection->executeQuery("ALTER TABLE c_student_publication MODIFY COLUMN filetype char(10) NOT NULL default 'file'");
  3233. // Migrate using the migration files located in:
  3234. // src/Chamilo/CoreBundle/Migrations/Schema/V110
  3235. $result = migrate(
  3236. 110,
  3237. $manager
  3238. );
  3239. if ($result) {
  3240. error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
  3241. fixIds($manager);
  3242. error_log('fixIds finished ('.date('Y-m-d H:i:s').')');
  3243. $connection->executeQuery("UPDATE settings_current SET selected_value = '1.10.0' WHERE variable = 'chamilo_database_version'");
  3244. if ($processFiles) {
  3245. $fromVersionShort = '1.9';
  3246. include __DIR__.'/update-files-1.9.0-1.10.0.inc.php';
  3247. // Only updates the configuration.inc.php with the new version
  3248. include __DIR__.'/update-configuration.inc.php';
  3249. }
  3250. error_log('Upgrade 1.10.x process concluded! ('.date('Y-m-d H:i:s').')');
  3251. } else {
  3252. error_log('There was an error during running migrations. Check error.log');
  3253. break;
  3254. }
  3255. // no break
  3256. case '1.10.0':
  3257. case '1.10.2':
  3258. case '1.10.4':
  3259. case '1.10.6':
  3260. case '1.10.8':
  3261. $database = new Database();
  3262. $database->setManager($manager);
  3263. // Migrate using the migration files located in:
  3264. // src/Chamilo/CoreBundle/Migrations/Schema/V111
  3265. $result = migrate(111, $manager);
  3266. if ($result) {
  3267. error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
  3268. fixPostGroupIds($connection);
  3269. $sql = "UPDATE settings_current SET selected_value = '1.11.0' WHERE variable = 'chamilo_database_version'";
  3270. $connection->executeQuery($sql);
  3271. if ($processFiles) {
  3272. error_log('Update config files');
  3273. $fromVersionShort = '1.10';
  3274. include __DIR__.'/update-files-1.10.0-1.11.0.inc.php';
  3275. // Only updates the configuration.inc.php with the new version
  3276. include __DIR__.'/update-configuration.inc.php';
  3277. }
  3278. error_log('Upgrade 1.11.x process concluded! ('.date('Y-m-d H:i:s').')');
  3279. } else {
  3280. error_log('There was an error during running migrations. Check error.log');
  3281. }
  3282. // no break
  3283. case '1.11.0':
  3284. case '1.11.1':
  3285. case '1.11.2':
  3286. case '1.11.4':
  3287. case '1.11.6':
  3288. case '1.11.8':
  3289. case '1.11.10':
  3290. $database = new Database();
  3291. $database->setManager($manager);
  3292. // Migrate using the migration files located in:
  3293. // src/Chamilo/CoreBundle/Migrations/Schema/V111
  3294. $result = migrate(200, $manager);
  3295. if ($result) {
  3296. error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
  3297. $sql = "UPDATE settings_current SET selected_value = '2.0.0' WHERE variable = 'chamilo_database_version'";
  3298. $connection->executeQuery($sql);
  3299. if ($processFiles) {
  3300. error_log('Update config files');
  3301. $fromVersionShort = '1.11';
  3302. include __DIR__.'/update-files-1.11.0-2.0.0.inc.php';
  3303. // Only updates the configuration.inc.php with the new version
  3304. include __DIR__.'/update-configuration.inc.php';
  3305. }
  3306. error_log('Upgrade 2.0.0 process concluded! ('.date('Y-m-d H:i:s').')');
  3307. } else {
  3308. error_log('There was an error during running migrations. Check error.log');
  3309. }
  3310. break;
  3311. default:
  3312. break;
  3313. }
  3314. echo '</div>';
  3315. return true;
  3316. }
  3317. /**
  3318. * @param \Doctrine\DBAL\Connection $connection
  3319. *
  3320. * @throws \Doctrine\DBAL\DBALException
  3321. */
  3322. function fixPostGroupIds($connection)
  3323. {
  3324. $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_course_child VARCHAR(40) DEFAULT 'TRUE'");
  3325. error_log('Fix c_student_publication.post_group_id');
  3326. // Fix post_group_id
  3327. $sql = "SELECT * FROM c_student_publication
  3328. WHERE (post_group_id <> 0 AND post_group_id is not null)";
  3329. $statement = $connection->executeQuery($sql);
  3330. $result = $statement->fetchAll();
  3331. foreach ($result as $row) {
  3332. $groupId = $row['post_group_id'];
  3333. $courseId = $row['c_id'];
  3334. $workIid = $row['iid'];
  3335. $sql = "SELECT iid from c_group_info
  3336. WHERE c_id = $courseId AND id = $groupId";
  3337. $statement = $connection->executeQuery($sql);
  3338. $count = $statement->rowCount();
  3339. if ($count > 0) {
  3340. $rowGroup = $statement->fetch();
  3341. $newGroupId = $rowGroup['iid'];
  3342. if ($newGroupId == $groupId) {
  3343. continue;
  3344. }
  3345. if ($newGroupId) {
  3346. $sql = "UPDATE c_student_publication
  3347. SET post_group_id = $newGroupId
  3348. WHERE
  3349. c_id = $courseId AND
  3350. iid = $workIid
  3351. ";
  3352. $connection->executeQuery($sql);
  3353. }
  3354. }
  3355. }
  3356. error_log('End - Fix c_student_publication.post_group_id');
  3357. // Delete c_student_publication from any session that doesn't exist anymore
  3358. $sql = "DELETE FROM c_student_publication
  3359. WHERE session_id NOT IN (SELECT id FROM session) AND (session_id <> 0 AND session_id is not null)";
  3360. $connection->executeQuery($sql);
  3361. error_log('Fix work documents');
  3362. // Fix work documents that don't have c_item_property value
  3363. $sql = "SELECT * FROM c_student_publication WHERE parent_id IS NOT NULL";
  3364. $statement = $connection->executeQuery($sql);
  3365. $result = $statement->fetchAll();
  3366. foreach ($result as $row) {
  3367. $groupId = $row['post_group_id'];
  3368. $courseId = $row['c_id'];
  3369. $sessionId = $row['session_id'];
  3370. $workId = $row['id'];
  3371. $sessionCondition = " session_id = $sessionId";
  3372. if (empty($sessionId)) {
  3373. $sessionCondition = ' (session_id = 0 OR session_id IS NULL) ';
  3374. }
  3375. $sql = "SELECT * FROM c_item_property
  3376. WHERE
  3377. c_id = $courseId AND
  3378. tool = 'work' AND
  3379. ref = $workId AND
  3380. $sessionCondition ";
  3381. $itemInfo = $connection->fetchAssoc($sql);
  3382. if (empty($itemInfo)) {
  3383. $params = [
  3384. 'c_id' => $courseId,
  3385. 'to_group_id' => $groupId,
  3386. //'to_user_id' => null,
  3387. 'insert_user_id' => 1,
  3388. 'session_id' => $sessionId,
  3389. 'tool' => 'work',
  3390. 'insert_date' => api_get_utc_datetime(),
  3391. 'lastedit_date' => api_get_utc_datetime(),
  3392. 'ref' => $workId,
  3393. 'lastedit_type' => 'visible',
  3394. 'lastedit_user_id' => 1,
  3395. 'visibility' => 1,
  3396. ];
  3397. $connection->insert('c_item_property', $params);
  3398. $id = $connection->lastInsertId();
  3399. $sql = "UPDATE c_item_property SET id = iid WHERE iid = $id";
  3400. $connection->executeQuery($sql);
  3401. }
  3402. }
  3403. error_log('End - Fix work documents');
  3404. }