settings.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <?php
  2. // $Id: settings.php 14372 2008-02-25 23:42:16Z yannoo $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004-2008 Dokeos S.A.
  7. Copyright (c) 2003 Ghent University
  8. Copyright (c) Patrick Cool, Ghent University
  9. Copyright (c) Roan Embrechts, Vrije Universiteit Brussel
  10. Copyright (c) Bart Mollet, Hogeschool Gent
  11. For a full list of contributors, see "credits.txt".
  12. The full license can be read in "license.txt".
  13. This program is free software; you can redistribute it and/or
  14. modify it under the terms of the GNU General Public License
  15. as published by the Free Software Foundation; either version 2
  16. of the License, or (at your option) any later version.
  17. See the GNU General Public License for more details.
  18. Contact address: Dokeos, 44 rue des palais, B-1030 Brussels, Belgium
  19. Mail: info@dokeos.com
  20. ==============================================================================
  21. */
  22. /**
  23. ==============================================================================
  24. * With this tool you can easily adjust non critical configuration settings.
  25. * Non critical means that changing them will not result in a broken campus.
  26. *
  27. * @author Patrick Cool
  28. * @since Dokeos 1.6
  29. * @package dokeos.admin
  30. ==============================================================================
  31. */
  32. /*
  33. ==============================================================================
  34. INIT SECTION
  35. ==============================================================================
  36. */
  37. // name of the language file that needs to be included
  38. $language_file = 'admin';
  39. // including some necessary dokeos files
  40. include_once ('../inc/global.inc.php');
  41. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  42. // setting the section (for the tabs)
  43. $this_section = SECTION_PLATFORM_ADMIN;
  44. // Access restrictions
  45. api_protect_admin_script();
  46. // Submit Stylesheets
  47. if ($_POST['submit_stylesheets'])
  48. {
  49. $message = store_stylesheets();
  50. header("Location: ".api_get_self()."?category=stylesheets");
  51. exit;
  52. }
  53. // Database Table Definitions
  54. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  55. // setting breadcrumbs
  56. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  57. // setting the name of the tool
  58. $tool_name = get_lang('DokeosConfigSettings');
  59. // Build the form
  60. if ($_GET['category'] and $_GET['category'] <> "Plugins" and $_GET['category'] <> "stylesheets")
  61. {
  62. $form = new FormValidator('settings', 'post', 'settings.php?category='.$_GET['category']);
  63. $renderer = & $form->defaultRenderer();
  64. $renderer->setHeaderTemplate('<div class="settingtitle">{header}</div>'."\n");
  65. $renderer->setElementTemplate('<div class="settingcomment">{label}</div>'."\n".'<div class="settingvalue">{element}</div>'."\n");
  66. $my_category = mysql_real_escape_string($_GET['category']);
  67. $sqlsettings = "SELECT DISTINCT * FROM $table_settings_current WHERE category='$my_category' GROUP BY variable ORDER BY id ASC";
  68. $resultsettings = api_sql_query($sqlsettings, __FILE__, __LINE__);
  69. while ($row = mysql_fetch_array($resultsettings))
  70. {
  71. $form->addElement('header', null, get_lang($row['title']));
  72. switch ($row['type'])
  73. {
  74. case 'textfield' :
  75. $form->addElement('text', $row['variable'], get_lang($row['comment']));
  76. $default_values[$row['variable']] = $row['selected_value'];
  77. break;
  78. case 'textarea' :
  79. $form->addElement('textarea', $row['variable'], get_lang($row['comment']));
  80. $default_values[$row['variable']] = $row['selected_value'];
  81. break;
  82. case 'radio' :
  83. $values = get_settings_options($row['variable']);
  84. $group = array ();
  85. foreach ($values as $key => $value)
  86. {
  87. $group[] = $form->createElement('radio', $row['variable'], '', get_lang($value['display_text']), $value['value']);
  88. }
  89. $form->addGroup($group, $row['variable'], get_lang($row['comment']), '<br />', false);
  90. $default_values[$row['variable']] = $row['selected_value'];
  91. break;
  92. case 'checkbox';
  93. $sql = "SELECT * FROM settings_current WHERE variable='".$row['variable']."'";
  94. $result = api_sql_query($sql, __FILE__, __LINE__);
  95. $group = array ();
  96. while ($rowkeys = mysql_fetch_array($result))
  97. {
  98. $element = & $form->createElement('checkbox', $rowkeys['subkey'], '', get_lang($rowkeys['subkeytext']));
  99. if ($rowkeys['selected_value'] == 'true' && ! $form->isSubmitted())
  100. {
  101. $element->setChecked(true);
  102. }
  103. $group[] = $element;
  104. }
  105. $form->addGroup($group, $row['variable'], get_lang($row['comment']), '<br />'."\n");
  106. break;
  107. case "link" :
  108. $form->addElement('static', null, get_lang($row['comment']), get_lang('CurrentValue').' : '.$row['selected_value']);
  109. }
  110. }
  111. $form->addElement('submit', null, get_lang('Ok'));
  112. $form->setDefaults($default_values);
  113. if ($form->validate())
  114. {
  115. $values = $form->exportValues();
  116. // the first step is to set all the variables that have type=checkbox of the category
  117. // to false as the checkbox that is unchecked is not in the $_POST data and can
  118. // therefore not be set to false
  119. $sql = "UPDATE $table_settings_current SET selected_value='false' WHERE category='$my_category' AND type='checkbox'";
  120. $result = api_sql_query($sql, __FILE__, __LINE__);
  121. // Save the settings
  122. foreach ($values as $key => $value)
  123. {
  124. if (!is_array($value))
  125. {
  126. $sql = "UPDATE $table_settings_current SET selected_value='".mysql_real_escape_string($value)."' WHERE variable='$key'";
  127. $result = api_sql_query($sql, __FILE__, __LINE__);
  128. }
  129. else
  130. {
  131. foreach ($value as $subkey => $subvalue)
  132. {
  133. $sql = "UPDATE $table_settings_current SET selected_value='true' WHERE variable='$key' AND subkey = '$subkey'";
  134. $result = api_sql_query($sql, __FILE__, __LINE__);
  135. }
  136. }
  137. }
  138. header('Location: settings.php?action=stored&category='.$_GET['category']);
  139. exit;
  140. }
  141. }
  142. // including the header (banner)
  143. Display :: display_header($tool_name);
  144. //api_display_tool_title($tool_name);
  145. // displaying the message that the settings have been stored
  146. if ($_GET['action'] == "stored")
  147. {
  148. Display :: display_normal_message($SettingsStored);
  149. }
  150. // grabbing the categories
  151. $selectcategories = "SELECT DISTINCT category FROM ".$table_settings_current." WHERE category NOT IN ('stylesheets','Plugins')";
  152. $resultcategories = api_sql_query($selectcategories, __FILE__, __LINE__);
  153. echo "\n<div><ul>";
  154. while ($row = mysql_fetch_array($resultcategories))
  155. {
  156. echo "\n\t<li><a href=\"".api_get_self()."?category=".$row['category']."\">".ucfirst(get_lang($row['category']))."</a></li>";
  157. }
  158. echo "\n\t<li><a href=\"".api_get_self()."?category=Plugins\">".ucfirst(get_lang('Plugins'))."</a></li>";
  159. echo "\n\t<li><a href=\"".api_get_self()."?category=stylesheets\">".ucfirst(get_lang('Stylesheets'))."</a></li>";
  160. echo "\n</ul></div>";
  161. if (isset ($_GET['category']))
  162. {
  163. switch ($_GET['category'])
  164. {
  165. // displaying the extensions: plugins
  166. case 'Plugins' :
  167. handle_plugins();
  168. break;
  169. // displaying the extensions: Stylesheets
  170. case 'stylesheets' :
  171. handle_stylesheets();
  172. break;
  173. default :
  174. $form->display();
  175. }
  176. }
  177. /*
  178. ==============================================================================
  179. FOOTER
  180. ==============================================================================
  181. */
  182. Display :: display_footer();
  183. /*
  184. ==============================================================================
  185. FUNCTIONS
  186. ==============================================================================
  187. */
  188. /**
  189. * The function that retrieves all the possible settings for a certain config setting
  190. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  191. */
  192. function get_settings_options($var)
  193. {
  194. $table_settings_options = Database :: get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  195. $sql = "SELECT * FROM $table_settings_options WHERE variable='$var'";
  196. $result = api_sql_query($sql, __FILE__, __LINE__);
  197. while ($row = mysql_fetch_array($result))
  198. {
  199. $temp_array = array ('value' => $row['value'], 'display_text' => $row['display_text']);
  200. $settings_options_array[] = $temp_array;
  201. }
  202. return $settings_options_array;
  203. }
  204. /**
  205. * This function allows easy activating and inactivating of plugins
  206. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  207. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  208. */
  209. function handle_plugins()
  210. {
  211. global $SettingsStored;
  212. $userplugins = array();
  213. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  214. if ($_POST['submit_plugins'])
  215. {
  216. store_plugins();
  217. Display :: display_normal_message($SettingsStored);
  218. }
  219. echo get_lang('AvailablePlugins')."<br/><br/>";
  220. /* We scan the plugin directory. Each folder is a potential plugin. */
  221. $pluginpath = api_get_path(SYS_PLUGIN_PATH);
  222. $handle = opendir($pluginpath);
  223. while (false !== ($file = readdir($handle)))
  224. {
  225. if ($file <> '.' AND $file <> '..' AND is_dir(api_get_path(SYS_PLUGIN_PATH).$file))
  226. {
  227. $possibleplugins[] = $file;
  228. }
  229. }
  230. closedir($handle);
  231. /* for each of the possible plugin dirs we check if a file plugin.php (that contains all the needed information about this plugin)
  232. can be found in the dir.
  233. this plugin.php file looks like
  234. $plugin_info['title']='The title of the plugin'; //
  235. $plugin_info['comment']="Some comment about the plugin";
  236. $plugin_info['location']=array("loginpage_menu", "campushomepage_menu","banner"); // the possible locations where the plugins can be used
  237. $plugin_info['version']='0.1 alpha'; // The version number of the plugin
  238. $plugin_info['author']='Patrick Cool'; // The author of the plugin
  239. */
  240. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.$_GET['category'].'">';
  241. echo "<table class=\"data_table\">\n";
  242. echo "\t<tr>\n";
  243. echo "\t\t<th>\n";
  244. echo get_lang('Plugin');
  245. echo "\t\t</th>\n";
  246. echo "\t\t<th>\n";
  247. echo get_lang('LoginPageMainArea');
  248. echo "\t\t</th>\n";
  249. echo "\t\t<th>\n";
  250. echo get_lang('LoginPageMenu');
  251. echo "\t\t</th>\n";
  252. echo "\t\t<th>\n";
  253. echo get_lang('CampusHomepageMainArea');
  254. echo "\t\t</th>\n";
  255. echo "\t\t<th>\n";
  256. echo get_lang('CampusHomepageMenu');
  257. echo "\t\t</th>\n";
  258. echo "\t\t<th>\n";
  259. echo get_lang('MyCoursesMainArea');
  260. echo "\t\t</th>\n";
  261. echo "\t\t<th>\n";
  262. echo get_lang('MyCoursesMenu');
  263. echo "\t\t</th>\n";
  264. echo "\t\t<th>\n";
  265. echo get_lang('Header');
  266. echo "\t\t</th>\n";
  267. echo "\t\t<th>\n";
  268. echo get_lang('Footer');
  269. echo "\t\t</th>\n";
  270. echo "\t</tr>\n";
  271. /* We retrieve all the active plugins. */
  272. $sql = "SELECT * FROM $table_settings_current WHERE category='Plugins'";
  273. $result = api_sql_query($sql);
  274. while ($row = mysql_fetch_array($result))
  275. {
  276. $usedplugins[$row['variable']][] = $row['selected_value'];
  277. }
  278. /* We display all the possible plugins and the checkboxes */
  279. foreach ($possibleplugins as $testplugin)
  280. {
  281. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$testplugin."/plugin.php";
  282. if (file_exists($plugin_info_file))
  283. {
  284. $plugin_info = array();
  285. include ($plugin_info_file);
  286. echo "\t<tr>\n";
  287. echo "\t\t<td>\n";
  288. foreach ($plugin_info as $key => $value)
  289. {
  290. if ($key <> 'location')
  291. {
  292. if ($key == 'title')
  293. {
  294. $value = '<strong>'.$value.'</strong>';
  295. }
  296. echo get_lang(ucwords($key)).': '.$value.'<br />';
  297. }
  298. }
  299. if (file_exists(api_get_path(SYS_PLUGIN_PATH).$testplugin.'/readme.txt'))
  300. {
  301. echo "<a href='".api_get_path(WEB_PLUGIN_PATH).$testplugin."/readme.txt'>readme.txt</a>";
  302. }
  303. echo "\t\t</td>\n";
  304. // column: LoginPageMainArea
  305. display_plugin_cell('loginpage_main', $plugin_info, $testplugin, $usedplugins);
  306. display_plugin_cell('loginpage_menu', $plugin_info, $testplugin, $usedplugins);
  307. display_plugin_cell('campushomepage_main', $plugin_info, $testplugin, $usedplugins);
  308. display_plugin_cell('campushomepage_menu', $plugin_info, $testplugin, $usedplugins);
  309. display_plugin_cell('mycourses_main', $plugin_info, $testplugin, $usedplugins);
  310. display_plugin_cell('mycourses_menu', $plugin_info, $testplugin, $usedplugins);
  311. display_plugin_cell('header', $plugin_info, $testplugin, $usedplugins);
  312. display_plugin_cell('footer', $plugin_info, $testplugin, $usedplugins);
  313. echo "\t</tr>\n";
  314. }
  315. }
  316. echo '</table>';
  317. echo '<input type="submit" name="submit_plugins" value="Submit" /></form>';
  318. }
  319. function display_plugin_cell($location, $plugin_info, $current_plugin, $active_plugins)
  320. {
  321. echo "\t\t<td align=\"center\">\n";
  322. if (in_array($location, $plugin_info['location']))
  323. {
  324. if (is_array($active_plugins[$location])
  325. && in_array($current_plugin, $active_plugins[$location]))
  326. {
  327. $checked = "checked";
  328. }
  329. else
  330. {
  331. $checked = '';
  332. }
  333. echo '<input type="checkbox" name="'.$current_plugin.'-'.$location.'" value="true" '.$checked.'/>';
  334. }
  335. echo "\t\t</td>\n";
  336. }
  337. /**
  338. * This function allows the platform admin to choose the default stylesheet
  339. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  340. */
  341. function handle_stylesheets()
  342. {
  343. // Current style
  344. $currentstyle = api_get_setting('stylesheets');
  345. // Preview of the stylesheet
  346. echo '<div><iframe src="style_preview.php" width="100%" height="300" name="preview"></iframe></div>';
  347. echo '<form name="stylesheets" method="post" action="'.api_get_self().'?category='.$_GET['category'].'">';
  348. if ($handle = opendir(api_get_path(SYS_PATH).'main/css/'))
  349. {
  350. $counter=1;
  351. while (false !== ($style_dir = readdir($handle)))
  352. {
  353. if(substr($style_dir,0,1)=='.') //skip dirs starting with a '.'
  354. {
  355. continue;
  356. }
  357. $dirpath = api_get_path(SYS_PATH).'main/css/'.$style_dir;
  358. if (is_dir($dirpath))
  359. {
  360. if ($style_dir != '.' && $style_dir != '..')
  361. {
  362. if ($currentstyle == $style_dir OR ($style_dir == 'default' AND !$currentstyle))
  363. {
  364. $selected = 'checked="checked"';
  365. }
  366. else
  367. {
  368. $selected = '';
  369. }
  370. echo "<input type=\"radio\" name=\"style\" value=\"".$style_dir."\" ".$selected." onClick=\"parent.preview.location='style_preview.php?style=".$style_dir."';\"/>";
  371. echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$style_dir.'</a>';
  372. //echo '<div id="Layer'.$counter.'" style="position:relative; width:687px; z-index:2; visibility: hidden;">';
  373. //echo '<a href="#" onClick="MM_showHideLayers(\'Layer'.$counter.'\',\'\',\'hide\')">'.get_lang('Close').'</a>';
  374. //echo '<iframe src="style_preview.php?style='.$file.'" width="100%" style="float:right;"></iframe></div>';
  375. echo "<br />\n";
  376. $counter++;
  377. }
  378. }
  379. }
  380. closedir($handle);
  381. }
  382. echo '<input type="submit" name="submit_stylesheets" value="'.get_lang('Ok').'" /></form>';
  383. }
  384. /**
  385. * This function allows easy activating and inactivating of plugins
  386. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  387. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  388. */
  389. function store_plugins()
  390. {
  391. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  392. // Step 1 : we remove all the plugins
  393. $sql = "DELETE FROM $table_settings_current WHERE category='Plugins'";
  394. api_sql_query($sql, __LINE__, __FILE__);
  395. // step 2: looping through all the post values we only store these which are really a valid plugin location.
  396. foreach ($_POST as $form_name => $formvalue)
  397. {
  398. $form_name_elements = explode("-", $form_name);
  399. if (is_valid_plugin_location($form_name_elements[1]))
  400. {
  401. $sql = "INSERT into $table_settings_current (variable,category,selected_value) VALUES ('".$form_name_elements['1']."','Plugins','".$form_name_elements['0']."')";
  402. api_sql_query($sql, __LINE__, __FILE__);
  403. }
  404. }
  405. }
  406. /**
  407. * Check if the post information is really a valid plugin location.
  408. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  409. */
  410. function is_valid_plugin_location($location)
  411. {
  412. $valid_locations=array('loginpage_main', 'loginpage_menu', 'campushomepage_main', 'campushomepage_menu', 'mycourses_main', 'mycourses_menu','header', 'footer');
  413. if (in_array($location, $valid_locations))
  414. {
  415. return true;
  416. }
  417. else
  418. {
  419. return false;
  420. }
  421. }
  422. /**
  423. * This function allows the platform admin to choose which should be the default stylesheet
  424. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  425. */
  426. function store_stylesheets()
  427. {
  428. // Database Table Definitions
  429. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  430. // Insert the stylesheet
  431. $style = Database::escape_string($_POST['style']);
  432. if (is_style($style))
  433. {
  434. $sql = 'UPDATE '.$table_settings_current.' SET
  435. selected_value = "'.$style.'"
  436. WHERE variable = "stylesheets"
  437. AND category = "stylesheets"';
  438. api_sql_query($sql, __LINE__, __FILE__);
  439. }
  440. return true;
  441. }
  442. /**
  443. * This function checks if the given style is a recognize style that exists in the css directory as
  444. * a standalone directory.
  445. * @param string Style
  446. * @return bool True if this style is recognized, false otherwise
  447. */
  448. function is_style($style)
  449. {
  450. $dir = api_get_path(SYS_PATH).'main/css/';
  451. $dirs = scandir($dir);
  452. $style = str_replace(array('/','\\'),array('',''),$style); //avoid slashes or backslashes
  453. if (in_array($style,$dirs) && is_dir($dir.$style))
  454. {
  455. return true;
  456. }
  457. return false;
  458. }
  459. ?>