settings.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. // $Id: settings.php 9589 2006-10-19 13:53:43Z bmol $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004-2005 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. // stating the language file
  38. $langFile = 'admin';
  39. // including some necessary dokeos files
  40. include ('../inc/global.inc.php');
  41. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  42. $this_section = SECTION_PLATFORM_ADMIN;
  43. api_protect_admin_script();
  44. // Submit Stylesheets
  45. if ($_POST['SubmitStylesheets'])
  46. {
  47. $message = store_stylesheets();
  48. header("Location: http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?category=stylesheets");
  49. exit;
  50. }
  51. $table_settings_current = Database :: get_main_table(MAIN_SETTINGS_CURRENT_TABLE);
  52. // setting breadcrumbs
  53. //$interbreadcrumb[] = array ("url" => "index.php", "name" => get_lang('PlatformAdmin'));
  54. // setting the name of the tool
  55. $tool_name = get_lang('DokeosConfigSettings');
  56. // Build the form
  57. if ($_GET['category'] and $_GET['category'] <> "Plugins" and $_GET['category'] <> "stylesheets")
  58. {
  59. $form = new FormValidator('settings', 'post', 'settings.php?category='.$_GET['category']);
  60. $renderer = & $form->defaultRenderer();
  61. $renderer->setHeaderTemplate('<div class="settingtitle">{header}</div>'."\n");
  62. $renderer->setElementTemplate('<div class="settingcomment">{label}</div>'."\n".'<div class="settingvalue">{element}</div>'."\n");
  63. $sqlsettings = "SELECT DISTINCT * FROM $table_settings_current WHERE category='".$_GET['category']."' GROUP BY variable ORDER BY id ASC";
  64. $resultsettings = api_sql_query($sqlsettings, __FILE__, __LINE__);
  65. while ($row = mysql_fetch_array($resultsettings))
  66. {
  67. $form->addElement('header', null, get_lang($row['title']));
  68. switch ($row['type'])
  69. {
  70. case 'textfield' :
  71. $form->addElement('text', $row['variable'], get_lang($row['comment']));
  72. $default_values[$row['variable']] = $row['selected_value'];
  73. break;
  74. case 'textarea' :
  75. $form->addElement('textarea', $row['variable'], get_lang($row['comment']));
  76. $default_values[$row['variable']] = $row['selected_value'];
  77. break;
  78. case 'radio' :
  79. $values = get_settings_options($row['variable']);
  80. $group = array ();
  81. foreach ($values as $key => $value)
  82. {
  83. $group[] = $form->createElement('radio', $row['variable'], '', get_lang($value['display_text']), $value['value']);
  84. }
  85. $form->addGroup($group, $row['variable'], get_lang($row['comment']), '<br />', false);
  86. $default_values[$row['variable']] = $row['selected_value'];
  87. break;
  88. case 'checkbox';
  89. $sql = "SELECT * FROM settings_current WHERE variable='".$row['variable']."'";
  90. $result = api_sql_query($sql, __FILE__, __LINE__);
  91. $group = array ();
  92. while ($rowkeys = mysql_fetch_array($result))
  93. {
  94. $element = & $form->createElement('checkbox', $rowkeys['subkey'], '', get_lang($rowkeys['subkeytext']));
  95. if ($rowkeys['selected_value'] == 'true' && ! $form->isSubmitted())
  96. {
  97. $element->setChecked(true);
  98. }
  99. $group[] = $element;
  100. }
  101. $form->addGroup($group, $row['variable'], get_lang($row['comment']), '<br />'."\n");
  102. break;
  103. case "link" :
  104. $form->addElement('static', null, get_lang($row['comment']), get_lang('CurrentValue').' : '.$row['selected_value']);
  105. }
  106. }
  107. $form->addElement('submit', null, get_lang('Ok'));
  108. $form->setDefaults($default_values);
  109. if ($form->validate())
  110. {
  111. $values = $form->exportValues();
  112. // the first step is to set all the variables that have type=checkbox of the category
  113. // to false as the checkbox that is unchecked is not in the $_POST data and can
  114. // therefore not be set to false
  115. $sql = "UPDATE $table_settings_current SET selected_value='false' WHERE category='".mysql_real_escape_string($_GET['category'])."' AND type='checkbox'";
  116. $result = api_sql_query($sql, __FILE__, __LINE__);
  117. // Save the settings
  118. foreach ($values as $key => $value)
  119. {
  120. if (!is_array($value))
  121. {
  122. $sql = "UPDATE $table_settings_current SET selected_value='".mysql_real_escape_string($value)."' WHERE variable='$key'";
  123. $result = api_sql_query($sql, __FILE__, __LINE__);
  124. }
  125. else
  126. {
  127. foreach ($value as $subkey => $subvalue)
  128. {
  129. $sql = "UPDATE $table_settings_current SET selected_value='true' WHERE variable='$key' AND subkey = '$subkey'";
  130. $result = api_sql_query($sql, __FILE__, __LINE__);
  131. }
  132. }
  133. }
  134. header('Location: settings.php?action=stored&category='.$_GET['category']);
  135. exit;
  136. }
  137. }
  138. // including the header (banner)
  139. Display :: display_header($tool_name);
  140. //api_display_tool_title($tool_name);
  141. // displaying the message that the settings have been stored
  142. if ($_GET['action'] == "stored")
  143. {
  144. Display :: display_normal_message($SettingsStored);
  145. }
  146. // grabbing the categories
  147. $selectcategories = "SELECT DISTINCT category FROM ".$table_settings_current." WHERE category NOT IN ('stylesheets','Plugins')";
  148. $resultcategories = api_sql_query($selectcategories, __FILE__, __LINE__);
  149. echo "\n<div><ul>";
  150. while ($row = mysql_fetch_array($resultcategories))
  151. {
  152. echo "\n\t<li><a href=\"".$_SERVER['PHP_SELF']."?category=".$row['category']."\">".get_lang($row['category'])."</a></li>";
  153. }
  154. echo "\n\t<li><a href=\"".$_SERVER['PHP_SELF']."?category=Plugins\">".get_lang('Plugins')."</a></li>";
  155. echo "\n\t<li><a href=\"".$_SERVER['PHP_SELF']."?category=stylesheets\">".get_lang('Stylesheets')."</a></li>";
  156. echo "\n</ul></div>";
  157. if (isset ($_GET['category']))
  158. {
  159. switch ($_GET['category'])
  160. {
  161. // displaying the extensions: plugins
  162. case 'Plugins' :
  163. handle_plugins();
  164. break;
  165. // displaying the extensions: Stylesheets
  166. case 'stylesheets' :
  167. handle_stylesheets();
  168. break;
  169. default :
  170. $form->display();
  171. }
  172. }
  173. /*
  174. ==============================================================================
  175. FOOTER
  176. ==============================================================================
  177. */
  178. Display :: display_footer();
  179. /**
  180. * The function that retrieves all the possible settings for a certain config setting
  181. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  182. */
  183. function get_settings_options($var)
  184. {
  185. $table_settings_options = Database :: get_main_table(MAIN_SETTINGS_OPTIONS_TABLE);
  186. $sql = "SELECT * FROM $table_settings_options WHERE variable='$var'";
  187. $result = api_sql_query($sql, __FILE__, __LINE__);
  188. while ($row = mysql_fetch_array($result))
  189. {
  190. $temp_array = array ('value' => $row['value'], 'display_text' => $row['display_text']);
  191. $settings_options_array[] = $temp_array;
  192. }
  193. return $settings_options_array;
  194. }
  195. /**
  196. * This function allows easy activating and inactivating of plugins
  197. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  198. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  199. */
  200. function handle_plugins()
  201. {
  202. global $SettingsStored;
  203. $table_settings_current = Database :: get_main_table(MAIN_SETTINGS_CURRENT_TABLE);
  204. if ($_POST['SubmitPlugins'])
  205. {
  206. store_plugins();
  207. Display :: display_normal_message($SettingsStored);
  208. }
  209. echo get_lang('AvailablePlugins')."<br/><br/>";
  210. /* We scan the plugin directory. Each folder is a potential plugin. */
  211. $pluginpath = api_get_path(SYS_PLUGIN_PATH);
  212. $handle = opendir($pluginpath);
  213. while (false !== ($file = readdir($handle)))
  214. {
  215. if (is_dir(api_get_path(SYS_PLUGIN_PATH).$file) AND $file <> '.' AND $file <> '..')
  216. {
  217. $possibleplugins[] = $file;
  218. }
  219. }
  220. closedir($handle);
  221. /* for each of the possible plugin dirs we check if a file plugin.php (that contains all the needed information about this plugin)
  222. can be found in the dir.
  223. this plugin.php file looks like
  224. $plugin_info['title']='The title of the plugin'; //
  225. $plugin_info['comment']="Some comment about the plugin";
  226. $plugin_info['location']=array("loginpage_menu", "campushomepage_menu","banner"); // the possible locations where the plugins can be used
  227. $plugin_info['version']='0.1 alpha'; // The version number of the plugin
  228. $plugin_info['author']='Patrick Cool'; // The author of the plugin
  229. */
  230. echo '<form name="plugins" method="post" action="'.$_SERVER['PHP_SELF'].'?category='.$_GET['category'].'">';
  231. echo "<table class=\"data_table\">\n";
  232. echo "\t<tr>\n";
  233. echo "\t\t<th>\n";
  234. echo get_lang('Plugin');
  235. echo "\t\t</th>\n";
  236. echo "\t\t<th>\n";
  237. echo get_lang('MainMenu');
  238. echo "\t\t</th>\n";
  239. echo "\t\t<th>\n";
  240. echo get_lang('MainMenuLogged');
  241. echo "\t\t</th>\n";
  242. echo "\t\t<th>\n";
  243. echo get_lang('Banner');
  244. echo "\t\t</th>\n";
  245. echo "\t</tr>\n";
  246. /* We retrieve all the active plugins. */
  247. $sql = "SELECT * FROM $table_settings_current WHERE category='Plugins'";
  248. $result = api_sql_query($sql);
  249. while ($row = mysql_fetch_array($result))
  250. {
  251. $usedplugins[$row['variable']][] = $row['selected_value'];
  252. }
  253. /* We display all the possible plugins and the checkboxes */
  254. foreach ($possibleplugins as $testplugin)
  255. {
  256. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$testplugin."/plugin.php";
  257. if (file_exists($plugin_info_file))
  258. {
  259. include ($plugin_info_file);
  260. echo "\t<tr>\n";
  261. echo "\t\t<td>\n";
  262. foreach ($plugin_info as $key => $value)
  263. {
  264. if ($key <> 'location')
  265. {
  266. if ($key == 'title')
  267. {
  268. $value = '<strong>'.$value.'</strong>';
  269. }
  270. echo get_lang($key).': '.$value.'<br />';
  271. }
  272. }
  273. if (file_exists(api_get_path(SYS_PLUGIN_PATH).$testplugin.'/readme.txt'))
  274. {
  275. echo "<a href='".api_get_path(WEB_PLUGIN_PATH).$testplugin."/readme.txt'>readme.txt</a>";
  276. }
  277. echo "\t\t</td>\n";
  278. echo "\t\t<td align=\"center\">\n";
  279. if (in_array('loginpage_menu', $plugin_info['location']))
  280. {
  281. if (in_array($testplugin, $usedplugins['loginpage_menu']))
  282. {
  283. $checked = "checked";
  284. }
  285. else
  286. {
  287. $checked = '';
  288. }
  289. echo '<input type="checkbox" name="'.$testplugin.'-loginpage_menu" value="true" '.$checked.'/>';
  290. }
  291. echo "\t\t</td>\n";
  292. echo "\t\t<td align=\"center\">\n";
  293. if (in_array('campushomepage_menu', $plugin_info['location']))
  294. {
  295. if (in_array($testplugin, $usedplugins['campushomepage_menu']))
  296. {
  297. $checked = "checked";
  298. }
  299. else
  300. {
  301. $checked = '';
  302. }
  303. echo '<input type="checkbox" name="'.$testplugin.'-campushomepage_menu" value="true" '.$checked.'/>';
  304. }
  305. echo "\t\t</td>\n";
  306. echo "\t\t<td align=\"center\">\n";
  307. if (in_array('banner', $plugin_info['location']))
  308. {
  309. if (in_array($testplugin, $usedplugins['banner']))
  310. {
  311. $checked = "checked";
  312. }
  313. else
  314. {
  315. $checked = '';
  316. }
  317. echo '<input type="checkbox" name="'.$testplugin.'-banner" value="true"'.$checked.'/>';
  318. }
  319. echo "\t\t</td>\n";
  320. echo "\t</tr>\n";
  321. }
  322. }
  323. echo '</table>';
  324. echo '<input type="submit" name="SubmitPlugins" value="Submit" /></form>';
  325. }
  326. /**
  327. * This function allows the platform admin to choose the default stylesheet
  328. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  329. */
  330. function handle_stylesheets()
  331. {
  332. // Current style
  333. $currentstyle = api_get_setting('stylesheets');
  334. // Preview of the stylesheet
  335. echo '<div><iframe src="style_preview.php" width="100%" height="300" name="preview"></iframe></div>';
  336. echo '<form name="stylesheets" method="post" action="'.$_SERVER['PHP_SELF'].'?category='.$_GET['category'].'">';
  337. if ($handle = opendir(api_get_path(SYS_PATH).'main/css/'))
  338. {
  339. $counter=1;
  340. while (false !== ($style_dir = readdir($handle)))
  341. {
  342. if(substr($style_dir,0,1)=='.'){continue;} //skip dirs starting with a '.'
  343. $dirpath = api_get_path(SYS_PATH).'main/css/'.$style_dir;
  344. if (is_dir($dirpath))
  345. {
  346. if ($style_dir != '.' && $style_dir != '..')
  347. {
  348. if ($currentstyle == $style_dir OR ($style_dir == 'default' AND !$currentstyle))
  349. {
  350. $selected = 'checked="checked"';
  351. }
  352. else
  353. {
  354. $selected = '';
  355. }
  356. echo "<input type=\"radio\" name=\"style\" value=\"".$style_dir."\" ".$selected." onClick=\"parent.preview.location='style_preview.php?style=".$style_dir."';\"/>";
  357. echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$style_dir.'</a>';
  358. //echo '<div id="Layer'.$counter.'" style="position:relative; width:687px; z-index:2; visibility: hidden;">';
  359. //echo '<a href="#" onClick="MM_showHideLayers(\'Layer'.$counter.'\',\'\',\'hide\')">'.get_lang('Close').'</a>';
  360. //echo '<iframe src="style_preview.php?style='.$file.'" width="100%" style="float:right;"></iframe></div>';
  361. echo "<br />\n";
  362. $counter++;
  363. }
  364. }
  365. }
  366. closedir($handle);
  367. }
  368. echo '<input type="submit" name="SubmitStylesheets" value="Submit" /></form>';
  369. }
  370. /**
  371. * This function allows easy activating and inactivating of plugins
  372. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  373. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  374. */
  375. function store_plugins()
  376. {
  377. $table_settings_current = Database :: get_main_table(MAIN_SETTINGS_CURRENT_TABLE);
  378. // Step 1 : we remove all the plugins
  379. $sql = "DELETE FROM $table_settings_current WHERE category='Plugins'";
  380. api_sql_query($sql, __LINE__, __FILE__);
  381. // step 2: looping through all the post values we only store these which end on loginpage_menu or campushomepage_menu or banner
  382. foreach ($_POST as $form_name => $formvalue)
  383. {
  384. $form_name_elements = explode("-", $form_name);
  385. if (in_array('loginpage_menu', $form_name_elements) OR in_array('campushomepage_menu', $form_name_elements) OR in_array('banner', $form_name_elements))
  386. {
  387. $sql = "INSERT into $table_settings_current (variable,category,selected_value) VALUES ('".$form_name_elements['1']."','Plugins','".$form_name_elements['0']."')";
  388. api_sql_query($sql, __LINE__, __FILE__);
  389. }
  390. }
  391. }
  392. /**
  393. * This function allows the platform admin to choose which should be the default stylesheet
  394. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  395. */
  396. function store_stylesheets()
  397. {
  398. $table_settings_current = Database :: get_main_table(MAIN_SETTINGS_CURRENT_TABLE);
  399. // Delete the current stylesheet (if there is one). We are not sure there is one
  400. $sql = "DELETE FROM $table_settings_current WHERE category='stylesheets'";
  401. api_sql_query($sql, __LINE__, __FILE__);
  402. // Insert the stylesheet
  403. if ($_POST['style'] <> 'default')
  404. {
  405. $sql = "INSERT into $table_settings_current (variable,category,selected_value) VALUES ('stylesheets','stylesheets','".$_POST['style']."')";
  406. api_sql_query($sql, __LINE__, __FILE__);
  407. }
  408. return true;
  409. }
  410. ?>