settings.php 43 KB

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