settings.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  1. <?php // $Id: settings.php 22316 2009-07-23 16:18:33Z cfasanando $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2009 Dokeos SPRL
  6. Copyright (c) 2003 Ghent University
  7. Copyright (c) Patrick Cool, Ghent University
  8. Copyright (c) Julio Montoya, Dokeos
  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, rue du Corbeau, 108, 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. * @author Julio Montoya - Multiple URL site
  30. * @package dokeos.admin
  31. ==============================================================================
  32. */
  33. /*
  34. ==============================================================================
  35. INIT SECTION
  36. ==============================================================================
  37. */
  38. // name of the language file that needs to be included
  39. if ($_GET['category']=='Templates') {
  40. $language_file = array('admin','document');
  41. } else {
  42. $language_file = array('admin');
  43. }
  44. // resetting the course id
  45. $cidReset=true;
  46. // including some necessary dokeos files
  47. include_once ('../inc/global.inc.php');
  48. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  49. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  50. require_once (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  51. // setting the section (for the tabs)
  52. $this_section = SECTION_PLATFORM_ADMIN;
  53. $_SESSION['this_section'] = $this_section;
  54. // Access restrictions
  55. api_protect_admin_script();
  56. // Submit Stylesheets
  57. if (isset($_POST['submit_stylesheets']))
  58. {
  59. $message = store_stylesheets();
  60. header("Location: ".api_get_self()."?category=stylesheets");
  61. exit;
  62. }
  63. // Database Table Definitions
  64. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  65. // setting breadcrumbs
  66. $interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
  67. // setting the name of the tool
  68. $tool_name = get_lang('DokeosConfigSettings');
  69. // Build the form
  70. if (!empty($_GET['category']) && !in_array($_GET['category'], array('Plugins', 'stylesheets', 'Search')))
  71. {
  72. $form = new FormValidator('settings', 'post', 'settings.php?category='.$_GET['category']);
  73. $renderer = & $form->defaultRenderer();
  74. $renderer->setHeaderTemplate('<div class="sectiontitle">{header}</div>'."\n");
  75. $renderer->setElementTemplate('<div class="sectioncomment">{label}</div>'."\n".'<div class="sectionvalue">{element}</div>'."\n");
  76. $my_category = Database::escape_string($_GET['category']);
  77. $sqlcountsettings = "SELECT COUNT(*) FROM $table_settings_current WHERE category='".$my_category."' AND type<>'checkbox'";
  78. $resultcountsettings = Database::query($sqlcountsettings, __FILE__, __LINE__);
  79. $countsetting = Database::fetch_array($resultcountsettings);
  80. if ($_configuration['access_url']==1)
  81. {
  82. $settings = api_get_settings($my_category,'group',$_configuration['access_url']);
  83. }
  84. else
  85. {
  86. $url_info = api_get_access_url($_configuration['access_url']);
  87. if ($url_info['active']==1)
  88. {
  89. //the default settings of Dokeos
  90. $settings = api_get_settings($my_category,'group',1,0);
  91. //the settings that are changeable from a particular site
  92. $settings_by_access = api_get_settings($my_category,'group',$_configuration['access_url'],1);
  93. //echo '<pre>';
  94. //print_r($settings_by_access);
  95. $settings_by_access_list=array();
  96. foreach($settings_by_access as $row)
  97. {
  98. if (empty($row['variable']))
  99. $row['variable']=0;
  100. if (empty($row['subkey']))
  101. $row['subkey']=0;
  102. if (empty($row['category']))
  103. $row['category']=0;
  104. // one more validation if is changeable
  105. if ($row['access_url_changeable']==1)
  106. $settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ] = $row;
  107. else
  108. $settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ] = array();
  109. }
  110. }
  111. }
  112. //print_r($settings_by_access_list);echo '</pre>';
  113. //$sqlsettings = "SELECT DISTINCT * FROM $table_settings_current WHERE category='$my_category' GROUP BY variable ORDER BY id ASC";
  114. //$resultsettings = Database::query($sqlsettings, __FILE__, __LINE__);
  115. //while ($row = Database::fetch_array($resultsettings))
  116. $default_values = array();
  117. foreach($settings as $row) {
  118. if ($row['variable'] == 'search_enabled') { continue; }
  119. $anchor_name = $row['variable'].(!empty($row['subkey']) ? '_'.$row['subkey'] : '');
  120. $form->addElement('html',"\n<a name=\"$anchor_name\"></a>\n");
  121. ($countsetting['0']%10) < 5 ?$b=$countsetting['0']-10:$b=$countsetting['0'];
  122. if ($i % 10 == 0 and $i<$b){
  123. if ($_GET['category'] <> "Languages"){
  124. $form->addElement('html','<div align="right">');
  125. $form->addElement('style_submit_button', null,get_lang('SaveSettings'), 'class="save"');
  126. $form->addElement('html','</div>');
  127. }
  128. }
  129. $i++;
  130. $form->addElement('header', null, get_lang($row['title']));
  131. if ($row['access_url_changeable']=='1' && $_configuration['multiple_access_urls']==true) {
  132. $form->addElement('html', '<div style="float:right;">'.Display::return_icon('shared_setting.png',get_lang('SharedSettingIconComment')).'</div>');
  133. }
  134. $hideme=array();
  135. $hide_element=false;
  136. if ($_configuration['access_url']!=1)
  137. {
  138. if ($row['access_url_changeable']==0)
  139. {
  140. //we hide the element in other cases (checkbox, radiobutton) we 'freeze' the element
  141. $hide_element=true;
  142. $hideme=array('disabled');
  143. }
  144. elseif($url_info['active']==1)
  145. {
  146. // we show the elements
  147. if (empty($row['variable']))
  148. $row['variable']=0;
  149. if (empty($row['subkey']))
  150. $row['subkey']=0;
  151. if (empty($row['category']))
  152. $row['category']=0;
  153. if (is_array ($settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ]))
  154. {
  155. // we are sure that the other site have a selected value
  156. if ($settings_by_access_list[ $row['variable'] ] [ $row['subkey'] ] [ $row['category'] ]['selected_value']!='')
  157. $row['selected_value'] =$settings_by_access_list[$row['variable']] [$row['subkey']] [ $row['category'] ]['selected_value'];
  158. }
  159. // there is no else because we load the default $row['selected_value'] of the main Dokeos site
  160. }
  161. }
  162. switch ($row['type']) {
  163. case 'textfield' :
  164. if ($row['variable']=='account_valid_duration') {
  165. $form->addElement('text', $row['variable'], get_lang($row['comment']),array('maxlength'=>'5'));
  166. $form->applyFilter($row['variable'],'html_filter');
  167. $default_values[$row['variable']] = $row['selected_value'];
  168. // For platform character set selection: Conversion of the textfield to a select box with valid values.
  169. } elseif ($row['variable'] == 'platform_charset') {
  170. $current_system_encoding = api_refine_encoding_id(trim($row['selected_value']));
  171. $valid_encodings = array_flip(api_get_valid_encodings());
  172. if (!isset($valid_encodings[$current_system_encoding])) {
  173. $is_alias_encoding = false;
  174. foreach ($valid_encodings as $encoding) {
  175. if (api_equal_encodings($encoding, $current_system_encoding)) {
  176. $is_alias_encoding = true;
  177. $current_system_encoding = $encoding;
  178. break;
  179. }
  180. }
  181. if (!$is_alias_encoding) {
  182. $valid_encodings[$current_system_encoding] = $current_system_encoding;
  183. }
  184. }
  185. foreach ($valid_encodings as $key => &$encoding) {
  186. $encoding = api_is_encoding_supported($key) ? $key : $key.' (n.a.)';
  187. }
  188. $form->addElement('select', $row['variable'], get_lang($row['comment']), $valid_encodings);
  189. $default_values[$row['variable']] = $current_system_encoding;
  190. //
  191. } else {
  192. $form->addElement('text', $row['variable'], get_lang($row['comment']),$hideme);
  193. $form->applyFilter($row['variable'],'html_filter');
  194. $default_values[$row['variable']] = $row['selected_value'];
  195. }
  196. break;
  197. case 'textarea' :
  198. $form->addElement('textarea', $row['variable'], get_lang($row['comment']),$hideme);
  199. $default_values[$row['variable']] = $row['selected_value'];
  200. break;
  201. case 'radio' :
  202. $values = get_settings_options($row['variable']);
  203. $group = array ();
  204. if (is_array($values )) {
  205. foreach ($values as $key => $value) {
  206. $element = & $form->createElement('radio', $row['variable'], '', get_lang($value['display_text']), $value['value']);
  207. if ($hide_element) {
  208. $element->freeze();
  209. }
  210. $group[] = $element;
  211. }
  212. }
  213. $form->addGroup($group, $row['variable'], get_lang($row['comment']), '<br />', false);
  214. $default_values[$row['variable']] = $row['selected_value'];
  215. break;
  216. case 'checkbox';
  217. //1. we collect all the options of this variable
  218. $sql = "SELECT * FROM settings_current WHERE variable='".$row['variable']."' AND access_url = 1";
  219. $result = Database::query($sql, __FILE__, __LINE__);
  220. $group = array ();
  221. while ($rowkeys = Database::fetch_array($result)) {
  222. if ($rowkeys['variable'] == 'course_create_active_tools' && $rowkeys['subkey'] == 'enable_search') {continue;}
  223. $element = & $form->createElement('checkbox', $rowkeys['subkey'], '', get_lang($rowkeys['subkeytext']));
  224. if ($row['access_url_changeable']==1) {
  225. //2. we look into the DB if there is a setting for a specific access_url
  226. $access_url = $_configuration['access_url'];
  227. if(empty($access_url )) $access_url =1;
  228. $sql = "SELECT selected_value FROM settings_current WHERE variable='".$rowkeys['variable']."' AND subkey='".$rowkeys['subkey']."' AND subkeytext='".$rowkeys['subkeytext']."' AND access_url = $access_url";
  229. $result_access = Database::query($sql, __FILE__, __LINE__);
  230. $row_access = Database::fetch_array($result_access);
  231. if ($row_access['selected_value'] == 'true' && ! $form->isSubmitted()) {
  232. $element->setChecked(true);
  233. }
  234. } else {
  235. if ($rowkeys['selected_value'] == 'true' && ! $form->isSubmitted()) {
  236. $element->setChecked(true);
  237. }
  238. }
  239. if ($hide_element) {
  240. $element->freeze();
  241. }
  242. $group[] = $element;
  243. }
  244. $form->addGroup($group, $row['variable'], get_lang($row['comment']), '<br />'."\n");
  245. break;
  246. case "link" :
  247. $form->addElement('static', null, get_lang($row['comment']), get_lang('CurrentValue').' : '.$row['selected_value'],$hideme);
  248. }
  249. }
  250. if ($_GET['category'] <> "Languages"){
  251. $form->addElement('html','<div align="right">');
  252. $form->addElement('style_submit_button', null,get_lang('SaveSettings'), 'class="save"');
  253. $form->addElement('html','</div>');
  254. }
  255. $form->setDefaults($default_values);
  256. if ($form->validate())
  257. {
  258. $values = $form->exportValues();
  259. // the first step is to set all the variables that have type=checkbox of the category
  260. // to false as the checkbox that is unchecked is not in the $_POST data and can
  261. // therefore not be set to false.
  262. // This, however, also means that if the process breaks on the third of five checkboxes, the others
  263. // will be set to false.
  264. $r = api_set_settings_category($my_category,'false',$_configuration['access_url']);
  265. //$sql = "UPDATE $table_settings_current SET selected_value='false' WHERE category='$my_category' AND type='checkbox'";
  266. //$result = Database::query($sql, __FILE__, __LINE__);
  267. // Save the settings
  268. $keys = array();
  269. foreach ($values as $key => $value)
  270. {
  271. if (!is_array($value))
  272. {
  273. //$sql = "UPDATE $table_settings_current SET selected_value='".Database::escape_string($value)."' WHERE variable='$key'";
  274. //$result = Database::query($sql, __FILE__, __LINE__);
  275. if (api_get_setting($key) != $value) $keys[] = $key;
  276. $result = api_set_setting($key,$value,null,null,$_configuration['access_url']);
  277. }
  278. else
  279. {
  280. $sql = "SELECT subkey FROM $table_settings_current WHERE variable = '$key'";
  281. $res = Database::query($sql,__FILE__,__LINE__);
  282. $subkeys = array();
  283. while ($row_subkeys = Database::fetch_array($res)) {
  284. // if subkey is changed
  285. if ( (isset($value[$row_subkeys['subkey']]) && api_get_setting($key,$row_subkeys['subkey']) == 'false') ||
  286. (!isset($value[$row_subkeys['subkey']]) && api_get_setting($key,$row_subkeys['subkey']) == 'true')) {
  287. $keys[] = $key;
  288. break;
  289. }
  290. }
  291. foreach ($value as $subkey => $subvalue)
  292. {
  293. //$sql = "UPDATE $table_settings_current SET selected_value='true' WHERE variable='$key' AND subkey = '$subkey'";
  294. //$result = Database::query($sql, __FILE__, __LINE__);
  295. $result = api_set_setting($key,'true',$subkey,null,$_configuration['access_url']);
  296. }
  297. }
  298. }
  299. // add event configuration settings category to system log
  300. $time = time();
  301. $user_id = api_get_user_id();
  302. $category = $_GET['category'];
  303. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  304. // add event configuration settings variable to system log
  305. if (is_array($keys) && count($keys) > 0) {
  306. foreach($keys as $variable) {
  307. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_VARIABLE, $variable, $time, $user_id);
  308. }
  309. }
  310. header('Location: settings.php?action=stored&category='.Security::remove_XSS($_GET['category']));
  311. exit;
  312. }
  313. }
  314. // including the header (banner)
  315. Display :: display_header($tool_name);
  316. //api_display_tool_title($tool_name);
  317. // displaying the message that the settings have been stored
  318. if (!empty($_GET['action']) && $_GET['action'] == "stored")
  319. {
  320. Display :: display_confirmation_message(get_lang('SettingsStored'));
  321. }
  322. // the action images
  323. $action_images['platform'] = 'dokeos.gif';
  324. $action_images['course'] = 'course.gif';
  325. $action_images['tools'] = 'reference.gif';
  326. $action_images['user'] = 'students.gif';
  327. $action_images['gradebook'] = 'gradebook_eval_not_empty.gif';
  328. $action_images['ldap'] = 'loginmanager.gif';
  329. $action_images['security'] = 'passwordprotected.gif';
  330. $action_images['languages'] = 'forum.gif';
  331. $action_images['tuning'] = 'tuning.gif';
  332. $action_images['plugins'] = 'plugin.gif';
  333. $action_images['stylesheets'] = 'theme.gif';
  334. $action_images['templates'] = 'template.gif';
  335. $action_images['search'] = 'search.gif';
  336. // grabbing the categories
  337. //$selectcategories = "SELECT DISTINCT category FROM ".$table_settings_current." WHERE category NOT IN ('stylesheets','Plugins')";
  338. //$resultcategories = Database::query($selectcategories, __FILE__, __LINE__);
  339. $resultcategories = api_get_settings_categories(array('stylesheets','Plugins', 'Templates', 'Search'));
  340. echo "\n<div class=\"actions\">";
  341. //while ($row = Database::fetch_array($resultcategories))
  342. foreach($resultcategories as $row)
  343. {
  344. echo "\n\t<a href=\"".api_get_self()."?category=".$row['category']."\">".Display::return_icon($action_images[strtolower($row['category'])], api_ucfirst(get_lang($row['category']))).api_ucfirst(get_lang($row['category']))."</a>";
  345. }
  346. echo "\n\t<a href=\"".api_get_self()."?category=Plugins\">".Display::return_icon($action_images['plugins'], api_ucfirst(get_lang('Plugins'))).api_ucfirst(get_lang('Plugins'))."</a>";
  347. echo "\n\t<a href=\"".api_get_self()."?category=stylesheets\">".Display::return_icon($action_images['stylesheets'], api_ucfirst(get_lang('Stylesheets'))).api_ucfirst(get_lang('Stylesheets'))."</a>";
  348. echo "\n\t<a href=\"".api_get_self()."?category=Templates\">".Display::return_icon($action_images['templates'], api_ucfirst(get_lang('Templates'))).api_ucfirst(get_lang('Templates'))."</a>";
  349. echo "\n\t<a href=\"".api_get_self()."?category=Search\">".Display::return_icon($action_images['search'], api_ucfirst(get_lang('Search'))).api_ucfirst(get_lang('Search'))."</a>";
  350. echo "\n</div>";
  351. if (!empty($_GET['category']))
  352. {
  353. switch ($_GET['category'])
  354. {
  355. // displaying the extensions: plugins
  356. // this will be available to all the sites (access_urls)
  357. case 'Plugins' :
  358. handle_plugins();
  359. break;
  360. // displaying the extensions: Stylesheets
  361. case 'stylesheets' :
  362. handle_stylesheets();
  363. break;
  364. case 'Search' :
  365. handle_search();
  366. break;
  367. case 'Templates' :
  368. handle_templates();
  369. break;
  370. default :
  371. $form->display();
  372. }
  373. }
  374. /*
  375. ==============================================================================
  376. FOOTER
  377. ==============================================================================
  378. */
  379. Display :: display_footer();
  380. /*
  381. ==============================================================================
  382. FUNCTIONS
  383. ==============================================================================
  384. */
  385. /**
  386. * The function that retrieves all the possible settings for a certain config setting
  387. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  388. */
  389. function get_settings_options($var)
  390. {
  391. $table_settings_options = Database :: get_main_table(TABLE_MAIN_SETTINGS_OPTIONS);
  392. $sql = "SELECT * FROM $table_settings_options WHERE variable='$var'";
  393. $result = Database::query($sql, __FILE__, __LINE__);
  394. while ($row = Database::fetch_array($result))
  395. {
  396. $temp_array = array ('value' => $row['value'], 'display_text' => $row['display_text']);
  397. $settings_options_array[] = $temp_array;
  398. }
  399. return $settings_options_array;
  400. }
  401. /**
  402. * This function allows easy activating and inactivating of plugins
  403. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  404. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  405. */
  406. function handle_plugins()
  407. {
  408. global $SettingsStored;
  409. $userplugins = array();
  410. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  411. if (isset($_POST['submit_plugins']))
  412. {
  413. store_plugins();
  414. // add event to system log
  415. $time = time();
  416. $user_id = api_get_user_id();
  417. $category = $_GET['category'];
  418. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  419. Display :: display_confirmation_message(get_lang('SettingsStored'));
  420. }
  421. echo get_lang('AvailablePlugins')."<br/><br/>";
  422. /* We scan the plugin directory. Each folder is a potential plugin. */
  423. $pluginpath = api_get_path(SYS_PLUGIN_PATH);
  424. $handle = @opendir($pluginpath);
  425. while (false !== ($file = readdir($handle)))
  426. {
  427. if ($file <> '.' AND $file <> '..' AND is_dir(api_get_path(SYS_PLUGIN_PATH).$file))
  428. {
  429. $possibleplugins[] = $file;
  430. }
  431. }
  432. @closedir($handle);
  433. /* for each of the possible plugin dirs we check if a file plugin.php (that contains all the needed information about this plugin)
  434. can be found in the dir.
  435. this plugin.php file looks like
  436. $plugin_info['title']='The title of the plugin'; //
  437. $plugin_info['comment']="Some comment about the plugin";
  438. $plugin_info['location']=array("loginpage_menu", "campushomepage_menu","banner"); // the possible locations where the plugins can be used
  439. $plugin_info['version']='0.1 alpha'; // The version number of the plugin
  440. $plugin_info['author']='Patrick Cool'; // The author of the plugin
  441. */
  442. echo '<form name="plugins" method="post" action="'.api_get_self().'?category='.$_GET['category'].'">';
  443. echo "<table class=\"data_table\">\n";
  444. echo "\t<tr>\n";
  445. echo "\t\t<th>\n";
  446. echo get_lang('Plugin');
  447. echo "\t\t</th>\n";
  448. echo "\t\t<th>\n";
  449. echo get_lang('LoginPageMainArea');
  450. echo "\t\t</th>\n";
  451. echo "\t\t<th>\n";
  452. echo get_lang('LoginPageMenu');
  453. echo "\t\t</th>\n";
  454. echo "\t\t<th>\n";
  455. echo get_lang('CampusHomepageMainArea');
  456. echo "\t\t</th>\n";
  457. echo "\t\t<th>\n";
  458. echo get_lang('CampusHomepageMenu');
  459. echo "\t\t</th>\n";
  460. echo "\t\t<th>\n";
  461. echo get_lang('MyCoursesMainArea');
  462. echo "\t\t</th>\n";
  463. echo "\t\t<th>\n";
  464. echo get_lang('MyCoursesMenu');
  465. echo "\t\t</th>\n";
  466. echo "\t\t<th>\n";
  467. echo get_lang('Header');
  468. echo "\t\t</th>\n";
  469. echo "\t\t<th>\n";
  470. echo get_lang('Footer');
  471. echo "\t\t</th>\n";
  472. echo "\t</tr>\n";
  473. /* We retrieve all the active plugins. */
  474. //$sql = "SELECT * FROM $table_settings_current WHERE category='Plugins'";
  475. //$result = Database::query($sql);
  476. $result = api_get_settings('Plugins');
  477. //while ($row = Database::fetch_array($result))
  478. foreach($result as $row)
  479. {
  480. $usedplugins[$row['variable']][] = $row['selected_value'];
  481. }
  482. /* We display all the possible plugins and the checkboxes */
  483. foreach ($possibleplugins as $testplugin)
  484. {
  485. $plugin_info_file = api_get_path(SYS_PLUGIN_PATH).$testplugin."/plugin.php";
  486. if (file_exists($plugin_info_file))
  487. {
  488. $plugin_info = array();
  489. include ($plugin_info_file);
  490. echo "\t<tr>\n";
  491. echo "\t\t<td>\n";
  492. foreach ($plugin_info as $key => $value)
  493. {
  494. if ($key <> 'location')
  495. {
  496. if ($key == 'title')
  497. {
  498. $value = '<strong>'.$value.'</strong>';
  499. }
  500. echo get_lang(ucwords($key)).': '.$value.'<br />';
  501. }
  502. }
  503. if (file_exists(api_get_path(SYS_PLUGIN_PATH).$testplugin.'/readme.txt'))
  504. {
  505. echo "<a href='".api_get_path(WEB_PLUGIN_PATH).$testplugin."/readme.txt'>readme.txt</a>";
  506. }
  507. echo "\t\t</td>\n";
  508. // column: LoginPageMainArea
  509. if(empty($usedplugins))
  510. {
  511. $usedplugins = array();
  512. }
  513. display_plugin_cell('loginpage_main', $plugin_info, $testplugin, $usedplugins);
  514. display_plugin_cell('loginpage_menu', $plugin_info, $testplugin, $usedplugins);
  515. display_plugin_cell('campushomepage_main', $plugin_info, $testplugin, $usedplugins);
  516. display_plugin_cell('campushomepage_menu', $plugin_info, $testplugin, $usedplugins);
  517. display_plugin_cell('mycourses_main', $plugin_info, $testplugin, $usedplugins);
  518. display_plugin_cell('mycourses_menu', $plugin_info, $testplugin, $usedplugins);
  519. display_plugin_cell('header', $plugin_info, $testplugin, $usedplugins);
  520. display_plugin_cell('footer', $plugin_info, $testplugin, $usedplugins);
  521. echo "\t</tr>\n";
  522. }
  523. }
  524. echo '</table>';
  525. echo '<button class="save" type="submit" name="submit_plugins">'.get_lang('EnablePlugins').'</button></form>';
  526. }
  527. function display_plugin_cell($location, $plugin_info, $current_plugin, $active_plugins)
  528. {
  529. echo "\t\t<td align=\"center\">\n";
  530. if (in_array($location, $plugin_info['location']))
  531. {
  532. if (isset($active_plugins[$location]) && is_array($active_plugins[$location])
  533. && in_array($current_plugin, $active_plugins[$location]))
  534. {
  535. $checked = "checked";
  536. }
  537. else
  538. {
  539. $checked = '';
  540. }
  541. echo '<input type="checkbox" name="'.$current_plugin.'-'.$location.'" value="true" '.$checked.'/>';
  542. }
  543. echo "\t\t</td>\n";
  544. }
  545. /**
  546. * This function allows the platform admin to choose the default stylesheet
  547. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  548. */
  549. function handle_stylesheets()
  550. {
  551. global $_configuration;
  552. // Current style
  553. $currentstyle = api_get_setting('stylesheets');
  554. $is_style_changeable=false;
  555. if ($_configuration['access_url']!=1)
  556. {
  557. $style_info = api_get_settings('stylesheets','',1,0);
  558. $url_info = api_get_access_url($_configuration['access_url']);
  559. if ($style_info[0]['access_url_changeable']==1 && $url_info['active']==1)
  560. {
  561. $is_style_changeable=true;
  562. echo '<div class="actions" id="stylesheetuploadlink">';
  563. Display::display_icon('theme_add.gif');
  564. echo '<a href="" onclick="document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\';return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  565. echo '</div>';
  566. }
  567. }
  568. else
  569. {
  570. $is_style_changeable=true;
  571. echo '<div class="actions" id="stylesheetuploadlink">';
  572. Display::display_icon('theme_add.gif');
  573. echo '<a href="" onclick="document.getElementById(\'newstylesheetform\').style.display = \'block\'; document.getElementById(\'stylesheetuploadlink\').style.display = \'none\';return false; ">'.get_lang('UploadNewStylesheet').'</a>';
  574. echo '</div>';
  575. }
  576. $form = new FormValidator('stylesheet_upload','post','settings.php?category=stylesheets&showuploadform=true');
  577. $form->addElement('text','name_stylesheet',get_lang('NameStylesheet'),array('size' => '40', 'maxlength' => '40'));
  578. $form->addRule('name_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  579. $form->addElement('file', 'new_stylesheet', get_lang('UploadNewStylesheet'));
  580. $allowed_file_types = array ('css');
  581. $form->addRule('new_stylesheet', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
  582. $form->addRule('new_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
  583. $form->addElement('style_submit_button', 'stylesheet_upload', get_lang('Ok'), array('class'=>'save'));
  584. if( $form->validate() AND is_writable(api_get_path(SYS_CODE_PATH).'css/'))
  585. {
  586. $values = $form->exportValues();
  587. $picture_element = & $form->getElement('new_stylesheet');
  588. $picture = $picture_element->getValue();
  589. upload_stylesheet($values, $picture);
  590. // add event to system log
  591. $time = time();
  592. $user_id = api_get_user_id();
  593. $category = $_GET['category'];
  594. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  595. Display::display_confirmation_message(get_lang('StylesheetAdded'));
  596. }
  597. else
  598. {
  599. if (!is_writable(api_get_path(SYS_CODE_PATH).'css/'))
  600. {
  601. Display::display_error_message(api_get_path(SYS_CODE_PATH).'css/'.get_lang('IsNotWritable'));
  602. }
  603. else
  604. {
  605. if ($_GET['showuploadform'] == 'true')
  606. {
  607. echo '<div id="newstylesheetform">';
  608. }
  609. else
  610. {
  611. echo '<div id="newstylesheetform" style="display: none;">';
  612. }
  613. // uploading a new stylesheet
  614. if ($_configuration['access_url']==1)
  615. {
  616. $form->display();
  617. }
  618. else
  619. {
  620. if ($is_style_changeable)
  621. {
  622. $form->display();
  623. }
  624. }
  625. echo '</div>';
  626. }
  627. }
  628. // Preview of the stylesheet
  629. echo '<div><iframe src="style_preview.php" width="100%" height="300" name="preview"></iframe></div>';
  630. echo '<form name="stylesheets" method="post" action="'.api_get_self().'?category='.Security::remove_XSS($_GET['category']).'">';
  631. if ($handle = @opendir(api_get_path(SYS_PATH).'main/css/'))
  632. {
  633. $counter=1;
  634. while (false !== ($style_dir = readdir($handle)))
  635. {
  636. if(substr($style_dir,0,1)=='.') //skip dirs starting with a '.'
  637. {
  638. continue;
  639. }
  640. $dirpath = api_get_path(SYS_PATH).'main/css/'.$style_dir;
  641. if (is_dir($dirpath))
  642. {
  643. if ($style_dir != '.' && $style_dir != '..')
  644. {
  645. if ($currentstyle == $style_dir OR ($style_dir == 'dokeos_classic' AND !$currentstyle))
  646. {
  647. $selected = 'checked="checked"';
  648. }
  649. else
  650. {
  651. $selected = '';
  652. }
  653. $show_name=get_lang(str_replace(' ','', ucwords(str_replace('_',' ', $style_dir))), '');
  654. if ($is_style_changeable)
  655. {
  656. echo "<input type=\"radio\" name=\"style\" value=\"".$style_dir."\" ".$selected." onClick=\"parent.preview.location='style_preview.php?style=".$style_dir."';\"/>";
  657. echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  658. }
  659. else
  660. echo '<a href="style_preview.php?style='.$style_dir.'" target="preview">'.$show_name.'</a>';
  661. //echo '<div id="Layer'.$counter.'" style="position:relative; width:687px; z-index:2; visibility: hidden;">';
  662. //echo '<a href="#" onClick="MM_showHideLayers(\'Layer'.$counter.'\',\'\',\'hide\')">'.get_lang('Close').'</a>';
  663. //echo '<iframe src="style_preview.php?style='.$file.'" width="100%" style="float:right;"></iframe></div>';
  664. echo "<br />\n";
  665. $counter++;
  666. }
  667. }
  668. }
  669. @closedir($handle);
  670. }
  671. if ($is_style_changeable)
  672. {
  673. echo '<button class="save" type="submit" name="submit_stylesheets"> '.get_lang('SaveSettings').' </button></form>';
  674. }
  675. }
  676. /**
  677. * creates the folder (if needed) and uploads the stylesheet in it
  678. *
  679. * @param array $values the values of the form
  680. * @param array $picture the values of the uploaded file
  681. *
  682. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  683. * @version May 2008
  684. * @since Dokeos 1.8.5
  685. */
  686. function upload_stylesheet($values,$picture)
  687. {
  688. // valid name for the stylesheet folder
  689. $style_name = api_ereg_replace("[^A-Za-z0-9]", "", $values['name_stylesheet'] );
  690. // create the folder if needed
  691. if(!is_dir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'))
  692. {
  693. if(mkdir(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'))
  694. {
  695. $perm = api_get_setting('permissions_for_new_directories');
  696. $perm = octdec(!empty($perm)?$perm:'0770');
  697. chmod(api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/', $perm);
  698. }
  699. }
  700. // move the file in the folder
  701. move_uploaded_file($picture['tmp_name'], api_get_path(SYS_CODE_PATH).'css/'.$style_name.'/'.$picture['name']);
  702. }
  703. /**
  704. * This function allows easy activating and inactivating of plugins
  705. * @todo: a similar function needs to be written to activate or inactivate additional tools.
  706. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  707. */
  708. function store_plugins()
  709. {
  710. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  711. global $_configuration;
  712. // Step 1 : we remove all the plugins
  713. //$sql = "DELETE FROM $table_settings_current WHERE category='Plugins'";
  714. //Database::query($sql, __LINE__, __FILE__);
  715. $r = api_delete_category_settings('Plugins',$_configuration['access_url']);
  716. // step 2: looping through all the post values we only store these which are really a valid plugin location.
  717. foreach ($_POST as $form_name => $formvalue)
  718. {
  719. $form_name_elements = explode("-", $form_name);
  720. if (is_valid_plugin_location($form_name_elements[1]))
  721. {
  722. //$sql = "INSERT into $table_settings_current (variable,category,selected_value) VALUES ('".$form_name_elements['1']."','Plugins','".$form_name_elements['0']."')";
  723. //Database::query($sql, __LINE__, __FILE__);
  724. api_add_setting($form_name_elements['0'],$form_name_elements['1'],$form_name_elements['0'],null,'Plugins',$form_name_elements['0'],null,null,null,$_configuration['access_url'],1);
  725. }
  726. }
  727. }
  728. /**
  729. * Check if the post information is really a valid plugin location.
  730. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  731. */
  732. function is_valid_plugin_location($location)
  733. {
  734. $valid_locations=array('loginpage_main', 'loginpage_menu', 'campushomepage_main', 'campushomepage_menu', 'mycourses_main', 'mycourses_menu','header', 'footer');
  735. if (in_array($location, $valid_locations))
  736. {
  737. return true;
  738. }
  739. else
  740. {
  741. return false;
  742. }
  743. }
  744. /**
  745. * This function allows the platform admin to choose which should be the default stylesheet
  746. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  747. */
  748. function store_stylesheets()
  749. {
  750. global $_configuration;
  751. // Database Table Definitions
  752. $table_settings_current = Database :: get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
  753. // Insert the stylesheet
  754. $style = Database::escape_string($_POST['style']);
  755. if (is_style($style))
  756. {
  757. /*
  758. $sql = 'UPDATE '.$table_settings_current.' SET
  759. selected_value = "'.$style.'"
  760. WHERE variable = "stylesheets"
  761. AND category = "stylesheets"';
  762. Database::query($sql, __LINE__, __FILE__);
  763. */
  764. api_set_setting('stylesheets',$style,null,'stylesheets',$_configuration['access_url']);
  765. }
  766. return true;
  767. }
  768. /**
  769. * This function checks if the given style is a recognize style that exists in the css directory as
  770. * a standalone directory.
  771. * @param string Style
  772. * @return bool True if this style is recognized, false otherwise
  773. */
  774. function is_style($style)
  775. {
  776. $dir = api_get_path(SYS_PATH).'main/css/';
  777. $dirs = scandir($dir);
  778. $style = str_replace(array('/','\\'),array('',''),$style); //avoid slashes or backslashes
  779. if (in_array($style,$dirs) && is_dir($dir.$style))
  780. {
  781. return true;
  782. }
  783. return false;
  784. }
  785. /**
  786. * Search options
  787. * TODO: support for multiple site. aka $_configuration['access_url'] == 1
  788. * @author Marco Villegas <marvil07@gmail.com>
  789. */
  790. function handle_search() {
  791. global $SettingsStored, $_configuration;
  792. $search_enabled = api_get_setting('search_enabled');
  793. $settings = api_get_settings('Search');
  794. if ($search_enabled !== 'true' || count($settings) < 1) {
  795. Display::display_error_message(get_lang('SearchFeatureNotEnabledComment'));
  796. return;
  797. }
  798. require_once api_get_path(LIBRARY_PATH) . 'specific_fields_manager.lib.php';
  799. $form = new FormValidator('search-options', 'post', api_get_self().'?category=Search');
  800. $renderer = & $form->defaultRenderer();
  801. $renderer->setHeaderTemplate('<div class="sectiontitle">{header}</div>'."\n");
  802. $renderer->setElementTemplate('<div class="sectioncomment">{label}</div>'."\n".'<div class="sectionvalue">{element}</div>'."\n");
  803. //search_show_unlinked_results
  804. $form->addElement('header', null, get_lang('SearchShowUnlinkedResultsTitle'));
  805. $form->addElement('label', null, get_lang('SearchShowUnlinkedResultsComment'));
  806. $values = get_settings_options('search_show_unlinked_results');
  807. $group = array ();
  808. foreach ($values as $key => $value) {
  809. $element = & $form->createElement('radio', 'search_show_unlinked_results', '', get_lang($value['display_text']), $value['value']);
  810. $group[] = $element;
  811. }
  812. $form->addGroup($group, 'search_show_unlinked_results', get_lang('SearchShowUnlinkedResultsComment'), '<br />', false);
  813. $default_values['search_show_unlinked_results'] = api_get_setting('search_show_unlinked_results');
  814. //search_prefilter_prefix
  815. $form->addElement('header', null, get_lang('SearchPrefilterPrefix'));
  816. $form->addElement('label', null, get_lang('SearchPrefilterPrefixComment'));
  817. $specific_fields = get_specific_field_list();
  818. $sf_values = array();
  819. foreach ($specific_fields as $sf) {
  820. $sf_values[$sf['code']] = $sf['name'];
  821. }
  822. $group = array ();
  823. $form->addElement('select', 'search_prefilter_prefix', get_lang('SearchPrefilterPrefix'), $sf_values, '');
  824. $default_values['search_prefilter_prefix'] = api_get_setting('search_prefilter_prefix');
  825. //$form->addRule('search_show_unlinked_results', get_lang('ThisFieldIsRequired'), 'required');
  826. $form->addElement('style_submit_button', 'search-options-save', get_lang('Ok'));
  827. $form->setDefaults($default_values);
  828. if( $form->validate()) {
  829. $formvalues = $form->exportValues();
  830. $r = api_set_settings_category('Search','false',$_configuration['access_url']);
  831. // Save the settings
  832. foreach ($formvalues as $key => $value)
  833. {
  834. $result = api_set_setting($key,$value,null,null);
  835. }
  836. Display :: display_confirmation_message($SettingsStored);
  837. } else {
  838. echo '<div id="search-options-form">';
  839. $form->display();
  840. echo '</div>';
  841. }
  842. }
  843. /**
  844. * wrapper for the templates
  845. *
  846. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  847. * @version August 2008
  848. * @since Dokeos 1.8.6
  849. */
  850. function handle_templates()
  851. {
  852. if ($_GET['action'] != 'add') {
  853. echo "\n<div class=\"actions\" style=\"margin-left:1px\" >";
  854. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('template_add.gif', get_lang('AddTemplate')).get_lang('AddTemplate').'</a>';
  855. echo "\n</div>";
  856. }
  857. if ($_GET['action'] == 'add' OR ( $_GET['action'] == 'edit' AND is_numeric($_GET['id']))) {
  858. add_edit_template();
  859. // add event to system log
  860. $time = time();
  861. $user_id = api_get_user_id();
  862. $category = $_GET['category'];
  863. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  864. } else {
  865. if ($_GET['action'] == 'delete' and is_numeric($_GET['id'])) {
  866. delete_template($_GET['id']);
  867. // add event to system log
  868. $time = time();
  869. $user_id = api_get_user_id();
  870. $category = $_GET['category'];
  871. event_system(LOG_CONFIGURATION_SETTINGS_CHANGE, LOG_CONFIGURATION_SETTINGS_CATEGORY, $category, $time, $user_id);
  872. }
  873. display_templates();
  874. }
  875. }
  876. /**
  877. * Display a sortable table with all the templates that the platform administrator has defined.
  878. *
  879. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  880. * @version August 2008
  881. * @since Dokeos 1.8.6
  882. */
  883. function display_templates()
  884. {
  885. $table = new SortableTable('templates', 'get_number_of_templates', 'get_template_data',1);
  886. $table->set_additional_parameters(array('category'=>Security::remove_XSS($_GET['category'])));
  887. $table->set_header(0, get_lang('Image'), true, array ('style' => 'width:101px;'));
  888. $table->set_header(1, get_lang('Title'));
  889. $table->set_header(2, get_lang('Actions'), false, array ('style' => 'width:50px;'));
  890. $table->set_column_filter(2,'actions_filter');
  891. $table->set_column_filter(0,'image_filter');
  892. $table->display();
  893. }
  894. /**
  895. * Get the number of templates that are defined by the platform admin.
  896. *
  897. * @return integer
  898. *
  899. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  900. * @version August 2008
  901. * @since Dokeos 1.8.6
  902. */
  903. function get_number_of_templates()
  904. {
  905. // Database table definition
  906. $table_system_template = Database :: get_main_table('system_template');
  907. // The sql statement
  908. $sql = "SELECT COUNT(id) AS total FROM $table_system_template";
  909. $result = Database::query($sql, __FILE__, __LINE__);
  910. $row = Database::fetch_array($result);
  911. // returning the number of templates
  912. return $row['total'];
  913. }
  914. /**
  915. * Get all the template data for the sortable table
  916. *
  917. * @param integer $from the start of the limit statement
  918. * @param integer $number_of_items the number of elements that have to be retrieved from the database
  919. * @param integer $column the column that is
  920. * @param string $direction the sorting direction (ASC or DESC�
  921. * @return array
  922. *
  923. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  924. * @version August 2008
  925. * @since Dokeos 1.8.6
  926. */
  927. function get_template_data($from, $number_of_items, $column, $direction)
  928. {
  929. // Database table definition
  930. $table_system_template = Database :: get_main_table('system_template');
  931. // the sql statement
  932. $sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
  933. $sql .= " ORDER BY col$column $direction ";
  934. $sql .= " LIMIT $from,$number_of_items";
  935. $result = Database::query($sql, __FILE__, __LINE__);
  936. while ($row = Database::fetch_array($result)) {
  937. $row['1'] = get_lang($row['1']);
  938. $return[]=$row;
  939. }
  940. // returning all the information for the sortable table
  941. return $return;
  942. }
  943. /**
  944. * display the edit and delete icons in the sortable table
  945. *
  946. * @param integer $id the id of the template
  947. * @return html code for the link to edit and delete the template
  948. *
  949. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  950. * @version August 2008
  951. * @since Dokeos 1.8.6
  952. */
  953. function actions_filter($id)
  954. {
  955. $return .= '<a href="settings.php?category=Templates&amp;action=edit&amp;id='.Security::remove_XSS($id).'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  956. $return .= '<a href="settings.php?category=Templates&amp;action=delete&amp;id='.Security::remove_XSS($id).'" onclick="javascript:if(!confirm('."'".get_lang("ConfirmYourChoice")."'".')) return false;">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
  957. return $return;
  958. }
  959. /**
  960. * Display the image of the template in the sortable table
  961. *
  962. * @param string $image the image
  963. * @return html code for the image
  964. *
  965. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  966. * @version August 2008
  967. * @since Dokeos 1.8.6
  968. */
  969. function image_filter($image)
  970. {
  971. if (!empty($image))
  972. {
  973. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/'.$image.'" alt="'.get_lang('TemplatePreview').'"/>';
  974. }
  975. else
  976. {
  977. return '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>';
  978. }
  979. }
  980. /**
  981. * Add (or edit) a template. This function displays the form and also takes care of uploading the image and storing the information in the database
  982. *
  983. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  984. * @version August 2008
  985. * @since Dokeos 1.8.6
  986. */
  987. function add_edit_template()
  988. {
  989. // initiate the object
  990. $form = new FormValidator('template', 'post', 'settings.php?category=Templates&action='.$_GET['action'].'&id='.$_GET['id']);
  991. // settting the form elements: the header
  992. if ($_GET['action'] == 'add') {
  993. $title = get_lang('AddTemplate');
  994. }
  995. else
  996. {
  997. $title = get_lang('EditTemplate');
  998. }
  999. $form->addElement('header', '', $title);
  1000. // settting the form elements: the title of the template
  1001. $form->add_textfield('title', get_lang('Title'), false);
  1002. // settting the form elements: the content of the template (wysiwyg editor)
  1003. $form->addElement('html_editor', 'template_text', get_lang('Text'), null, array('ToolbarSet' => 'AdminTemplates', 'Width' => '100%', 'Height' => '400'));
  1004. // settting the form elements: the form to upload an image to be used with the template
  1005. $form->addElement('file','template_image',get_lang('Image'),'');
  1006. // settting the form elements: a little bit information about the template image
  1007. $form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
  1008. // getting all the information of the template when editing a template
  1009. if ($_GET['action'] == 'edit') {
  1010. // Database table definition
  1011. $table_system_template = Database :: get_main_table('system_template');
  1012. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($_GET['id'])."'";
  1013. $result = Database::query($sql, __FILE__, __LINE__);
  1014. $row = Database::fetch_array($result);
  1015. $defaults['template_id'] = intval($_GET['id']);
  1016. $defaults['template_text'] = $row['content'];
  1017. //forcing a get_lang
  1018. $defaults['title'] = get_lang($row['title']);
  1019. // adding an extra field: a hidden field with the id of the template we are editing
  1020. $form->addElement('hidden','template_id');
  1021. // adding an extra field: a preview of the image that is currently used
  1022. if (!empty($row['image']))
  1023. {
  1024. $form->addElement('static','template_image_preview', '', '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/'.$row['image'].'" alt="'.get_lang('TemplatePreview').'"/>');
  1025. }
  1026. else
  1027. {
  1028. $form->addElement('static','template_image_preview', '', '<img src="'.api_get_path(WEB_PATH).'home/default_platform_document/template_thumb/noimage.gif" alt="'.get_lang('NoTemplatePreview').'"/>');
  1029. }
  1030. // setting the information of the template that we are editing
  1031. $form->setDefaults($defaults);
  1032. }
  1033. // settting the form elements: the submit button
  1034. $form->addElement('style_submit_button' , 'submit', get_lang('Ok') ,'class="save"');
  1035. // setting the rules: the required fields
  1036. $form->addRule('title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  1037. $form->addRule('template_text', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  1038. // if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
  1039. if( $form->validate() )
  1040. {
  1041. $check = Security::check_token('post');
  1042. if ($check) {
  1043. // exporting the values
  1044. $values = $form->exportValues();
  1045. // upload the file
  1046. if (!empty($_FILES['template_image']['name']))
  1047. {
  1048. include_once (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  1049. $upload_ok = process_uploaded_file($_FILES['template_image']);
  1050. if ($upload_ok)
  1051. {
  1052. // Try to add an extension to the file if it hasn't one
  1053. $new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);
  1054. // upload dir
  1055. $upload_dir = api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/';
  1056. // create dir if not exists
  1057. if (!is_dir($upload_dir)) {
  1058. $perm = api_get_setting('permissions_for_new_directories');
  1059. $perm = octdec(!empty($perm)?$perm:'0770');
  1060. $res = @mkdir($upload_dir,$perm);
  1061. }
  1062. // resize image to max default and upload
  1063. require_once (api_get_path(LIBRARY_PATH).'image.lib.php');
  1064. $temp = new image($_FILES['template_image']['tmp_name']);
  1065. $picture_infos=@getimagesize($_FILES['template_image']['tmp_name']);
  1066. $max_width_for_picture = 100;
  1067. if ($picture_infos[0]>$max_width_for_picture)
  1068. {
  1069. $thumbwidth = $max_width_for_picture;
  1070. if (empty($thumbwidth) or $thumbwidth==0) {
  1071. $thumbwidth=$max_width_for_picture;
  1072. }
  1073. $new_height = round(($thumbwidth/$picture_infos[0])*$picture_infos[1]);
  1074. $temp->resize($thumbwidth,$new_height,0);
  1075. }
  1076. $type=$picture_infos[2];
  1077. switch (!empty($type))
  1078. {
  1079. case 2 : $temp->send_image('JPG',$upload_dir.$new_file_name);
  1080. break;
  1081. case 3 : $temp->send_image('PNG',$upload_dir.$new_file_name);
  1082. break;
  1083. case 1 : $temp->send_image('GIF',$upload_dir.$new_file_name);
  1084. break;
  1085. }
  1086. }
  1087. }
  1088. // store the information in the database (as insert or as update)
  1089. $table_system_template = Database :: get_main_table('system_template');
  1090. if ($_GET['action'] == 'add') {
  1091. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  1092. $sql = "INSERT INTO $table_system_template (title, content, image) VALUES ('".Database::escape_string($values['title'])."','".$content_template."','".Database::escape_string($new_file_name)."')";
  1093. $result = Database::query($sql, __FILE__, __LINE__);
  1094. // display a feedback message
  1095. Display::display_confirmation_message(get_lang('TemplateAdded'));
  1096. echo '<a href="settings.php?category=Templates&amp;action=add">'.Display::return_icon('template_add.gif', get_lang('AddTemplate')).get_lang('AddTemplate').'</a>';
  1097. } else {
  1098. $content_template = '<head>{CSS}<style type="text/css">.text{font-weight: normal;}</style></head><body>'.Database::escape_string($values['template_text']).'</body>';
  1099. $sql = "UPDATE $table_system_template set title = '".Database::escape_string($values['title'])."',
  1100. content = '".$content_template."'";
  1101. if (!empty($new_file_name))
  1102. {
  1103. $sql .= ", image = '".Database::escape_string($new_file_name)."'";
  1104. }
  1105. $sql .= " WHERE id='".Database::escape_string($_GET['id'])."'";
  1106. $result = Database::query($sql, __FILE__, __LINE__);
  1107. // display a feedback message
  1108. Display::display_confirmation_message(get_lang('TemplateEdited'));
  1109. }
  1110. }
  1111. Security::clear_token();
  1112. display_templates();
  1113. }
  1114. else
  1115. {
  1116. $token = Security::get_token();
  1117. $form->addElement('hidden','sec_token');
  1118. $form->setConstants(array('sec_token' => $token));
  1119. // display the form
  1120. $form->display();
  1121. }
  1122. }
  1123. /**
  1124. * Delete a template
  1125. *
  1126. * @param integer $id the id of the template that has to be deleted
  1127. *
  1128. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  1129. * @version August 2008
  1130. * @since Dokeos 1.8.6
  1131. */
  1132. function delete_template($id)
  1133. {
  1134. // first we remove the image
  1135. $table_system_template = Database :: get_main_table('system_template');
  1136. $sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  1137. $result = Database::query($sql, __FILE__, __LINE__);
  1138. $row = Database::fetch_array($result);
  1139. if (!empty($row['image']))
  1140. {
  1141. unlink(api_get_path(SYS_PATH).'home/default_platform_document/template_thumb/'.$row['image']);
  1142. }
  1143. // now we remove it from the database
  1144. $sql = "DELETE FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
  1145. $result = Database::query($sql, __FILE__, __LINE__);
  1146. // display a feedback message
  1147. Display::display_confirmation_message(get_lang('TemplateDeleted'));
  1148. }
  1149. ?>