PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/collections/configure.php

https://github.com/agnesrambaud/yacs
PHP | 313 lines | 196 code | 47 blank | 70 comment | 21 complexity | a46ac3746268b2bd044bb2ab0619d936 MD5 | raw file
  1. <?php
  2. /**
  3. * define collections and related parameters
  4. *
  5. * Use this script to modify following parameters:
  6. *
  7. * - [code]collection_names[][/code] - list of names
  8. *
  9. * - [code]collection_titles[][/code] - list of titles
  10. *
  11. * - [code]collection_paths[][/code] - list of paths to files (used internally to access files)
  12. *
  13. * - [code]collection_urls[][/code] - list of URLs to files (to be used by browsers to fetch files)
  14. *
  15. * - [code]collection_introductions[][/code] - list of introduction blocks of text (used in collection lists)
  16. *
  17. * - [code]collection_descriptions[][/code] - list of description blocks of text (used at collection index pages)
  18. *
  19. * - [code]collection_prefixes[][/code] - list of prefix blocks of text (inserted at each collection page)
  20. *
  21. * - [code]collection_suffixes[][/code] - list of suffix blocks of text (inserted at each collection page)
  22. *
  23. * - [code]collection_visibilities[][/code] - to control access to the collection
  24. *
  25. * Configuration information is saved into [code]parameters/collections.include.php[/code].
  26. * If YACS is prevented to write to the file, it displays parameters to allow for a manual update.
  27. *
  28. * The file [code]parameters/collections.include.php.bak[/code] can be used to restore
  29. * the active configuration before the last change.
  30. *
  31. * If the file [code]parameters/demo.flag[/code] exists, the script assumes that this instance
  32. * of YACS runs in demonstration mode.
  33. * In this mode the edit form is displayed, but parameters are not saved in the configuration file.
  34. *
  35. * @author Bernard Paques
  36. * @author GnapZ
  37. * @tester Ghjmora
  38. * @reference
  39. * @license http://www.gnu.org/copyleft/lesser.txt GNU Lesser General Public License
  40. */
  41. // common definitions and initial processing
  42. include_once '../shared/global.php';
  43. // load localized strings
  44. i18n::bind('collections');
  45. // load the skin
  46. load_skin('collections');
  47. // the path to this page
  48. $context['path_bar'] = array( 'control/' => i18n::s('Control Panel') );
  49. // the title of the page
  50. $context['page_title'] = sprintf(i18n::s('%s: %s'), i18n::s('Configure'), i18n::s('File collections'));
  51. // anonymous users are invited to log in or to register
  52. if(!Surfer::is_logged())
  53. Safe::redirect($context['url_to_home'].$context['url_to_root'].'users/login.php?url='.urlencode('collections/configure.php'));
  54. // only associates can proceed
  55. elseif(!Surfer::is_associate()) {
  56. Safe::header('Status: 401 Forbidden', TRUE, 401);
  57. Logger::error(i18n::s('You are not allowed to perform this operation.'));
  58. // display the input form
  59. } elseif(isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] != 'POST')) {
  60. // load current parameters, if any
  61. Safe::load('parameters/collections.include.php');
  62. // the form
  63. $context['text'] .= '<form method="post" action="'.$context['script_url'].'" id="main_form"><div>';
  64. // one tab per collection
  65. $all_tabs = array();
  66. // list existing collections
  67. if(isset($context['collections']) && is_array($context['collections'])) {
  68. $index = 1;
  69. foreach($context['collections'] as $name => $attributes) {
  70. list($title, $path, $url, $introduction, $description, $prefix, $suffix, $visibility) = $attributes;
  71. $label = i18n::s('Collection nick name');
  72. $input = '<input type="text" name="collection_names['.$name.']" size="32" value="'.encode_field($name).'" maxlength="32" />';
  73. $hint = i18n::s('Delete to suppress');
  74. $fields[] = array($label, $input, $hint);
  75. $label = i18n::s('Access');
  76. $input = '<input type="radio" name="collection_visibilities['.$name.']" value="Y"';
  77. if(!isset($visibility) || !$visibility || ($visibility == 'Y'))
  78. $input .= ' checked="checked"';
  79. $input .= '/> '.i18n::s('Public - Access is granted to anonymous surfers');
  80. $input .= BR.'<input type="radio" name="collection_visibilities['.$name.']" value="R"';
  81. if(isset($visibility) && ($visibility == 'R'))
  82. $input .= ' checked="checked"';
  83. $input .= '/> '.i18n::s('Community - Access is restricted to authenticated persons');
  84. $input .= BR.'<input type="radio" name="collection_visibilities['.$name.']" value="N"';
  85. if(isset($visibility) && ($visibility == 'N'))
  86. $input .= ' checked="checked"';
  87. $input .= '/> '.i18n::s('Private - Access is restricted to selected persons');
  88. $fields[] = array($label, $input);
  89. $label = i18n::s('Label');
  90. $input = '<input type="text" name="collection_titles['.$name.']" size="45" value="'.encode_field($title).'" maxlength="255" />';
  91. $hint = i18n::s('Please provide a meaningful title.');
  92. $fields[] = array($label, $input, $hint);
  93. $label = i18n::s('Path prefix');
  94. $input = '<input type="text" name="collection_paths['.$name.']" size="45" value="'.encode_field($path).'" maxlength="255" />';
  95. $hint = sprintf(i18n::s('Local access to files; YACS installation directory is at "%s"'), $context['path_to_root']);
  96. $fields[] = array($label, $input, $hint);
  97. $label = i18n::s('URL prefix');
  98. $input = '<input type="text" name="collection_urls['.$name.']" size="45" value="'.encode_field($url).'" maxlength="255" />';
  99. $hint = i18n::s('The ftp:// or http:// address used to access the collection');
  100. $fields[] = array($label, $input, $hint);
  101. $label = i18n::s('Introduction');
  102. $input = '<textarea name="collection_introductions['.$name.']" cols="40" rows="2">'.encode_field($introduction).'</textarea>';
  103. $hint = i18n::s('To be used at the front page and on the collections index page');
  104. $fields[] = array($label, $input, $hint);
  105. $label = i18n::s('Description');
  106. $input = '<textarea name="collection_descriptions['.$name.']" cols="40" rows="3">'.encode_field($description).'</textarea>';
  107. $hint = i18n::s('To be inserted on the index page of this collection');
  108. $fields[] = array($label, $input, $hint);
  109. $label = i18n::s('Prefix');
  110. $input = '<textarea name="collection_prefixes['.$name.']" cols="40" rows="2">'.encode_field($prefix).'</textarea>';
  111. $hint = i18n::s('Inserted on top of pages for this collection');
  112. $fields[] = array($label, $input, $hint);
  113. $label = i18n::s('Suffix');
  114. $input = '<textarea name="collection_suffixes['.$name.']" cols="40" rows="2">'.encode_field($suffix).'</textarea>';
  115. $hint = i18n::s('Inserted at bottom of pages for this collection');
  116. $fields[] = array($label, $input, $hint);
  117. // add a tab
  118. $label = ucfirst($title ? $title : $name);
  119. $panel = Skin::build_form($fields);
  120. $fields = array();
  121. $all_tabs[] = array('collection_tab_'.$index, $label, 'collection_panel_'.$index, $panel);
  122. $index++;
  123. }
  124. }
  125. // append one remote collection
  126. $label = i18n::s('Collection nick name');
  127. $input = '<input type="text" name="collection_names[]" size="32" maxlength="32" />';
  128. $hint = i18n::s('Use a short nick name');
  129. $fields[] = array($label, $input, $hint);
  130. $label = i18n::s('Access');
  131. $input = '<input type="radio" name="collection_visibilities[]" value="Y" checked="checked"';
  132. $input .= '/> '.i18n::s('Public - Access is granted to anonymous surfers');
  133. $input .= BR.'<input type="radio" name="collection_visibilities[]" value="R"';
  134. $input .= '/> '.i18n::s('Community - Access is restricted to authenticated persons');
  135. $input .= BR.'<input type="radio" name="collection_visibilities[]" value="N"';
  136. $input .= '/> '.i18n::s('Private - Access is restricted to selected persons');
  137. $fields[] = array($label, $input);
  138. $label = i18n::s('Label');
  139. $input = '<input type="text" name="collection_titles[]" size="45" maxlength="255" />';
  140. $hint = i18n::s('Please provide a meaningful title.');
  141. $fields[] = array($label, $input, $hint);
  142. $label = i18n::s('Path prefix');
  143. $input = '<input type="text" name="collection_paths[]" size="45" maxlength="255" />';
  144. $hint = sprintf(i18n::s('Local access to files; YACS installation directory is at "%s"'), $context['path_to_root']);
  145. $fields[] = array($label, $input, $hint);
  146. $label = i18n::s('URL prefix');
  147. $input = '<input type="text" name="collection_urls[]" size="45" maxlength="255" />';
  148. $hint = i18n::s('The ftp:// or http:// address used to access the collection');
  149. $fields[] = array($label, $input, $hint);
  150. $label = i18n::s('Introduction');
  151. $input = '<textarea name="collection_introductions[]" cols="40" rows="2"></textarea>';
  152. $hint = i18n::s('To be used at the front page and on the collections index page');
  153. $fields[] = array($label, $input, $hint);
  154. $label = i18n::s('Description');
  155. $input = '<textarea name="collection_descriptions[]" cols="40" rows="3"></textarea>';
  156. $hint = i18n::s('To be inserted on the index page of this collection');
  157. $fields[] = array($label, $input, $hint);
  158. $label = i18n::s('Prefix');
  159. $input = '<textarea name="collection_prefixes[]" cols="40" rows="2"></textarea>';
  160. $hint = i18n::s('Inserted on top of pages for this collection');
  161. $fields[] = array($label, $input, $hint);
  162. $label = i18n::s('Suffix');
  163. $input = '<textarea name="collection_suffixes[]" cols="40" rows="2"></textarea>';
  164. $hint = i18n::s('Inserted at bottom of pages for this collection');
  165. $fields[] = array($label, $input, $hint);
  166. // add a tab
  167. $label = i18n::s('Add a collection');
  168. $panel = Skin::build_form($fields);
  169. $fields = array();
  170. $all_tabs[] = array('new', $label, 'new_panel', $panel);
  171. //
  172. // assemble all tabs
  173. //
  174. $context['text'] .= Skin::build_tabs($all_tabs);
  175. //
  176. // bottom commands
  177. //
  178. $menu = array();
  179. // the submit button
  180. $menu[] = Skin::build_submit_button(i18n::s('Submit'), i18n::s('Press [s] to submit data'), 's');
  181. // control panel
  182. if(file_exists('../parameters/control.include.php'))
  183. $menu[] = Skin::build_link('control/', i18n::s('Control Panel'), 'span');
  184. // all skins
  185. if(file_exists('../parameters/control.include.php'))
  186. $menu[] = Skin::build_link('collections/', i18n::s('File collections'), 'span');
  187. // insert the menu in the page
  188. $context['text'] .= Skin::finalize_list($menu, 'assistant_bar');
  189. // end of the form
  190. $context['text'] .= '</div></form>';
  191. // general help on this form
  192. $help = '<p>'.i18n::s('Use this form to give remote access to selected folders.')."</p>\n";
  193. $context['components']['boxes'] = Skin::build_box(i18n::s('Help'), $help, 'boxes', 'help');
  194. // no modifications in demo mode
  195. } elseif(file_exists($context['path_to_root'].'parameters/demo.flag')) {
  196. Safe::header('Status: 401 Forbidden', TRUE, 401);
  197. Logger::error(i18n::s('You are not allowed to perform this operation in demonstration mode.'));
  198. // save updated parameters
  199. } else {
  200. // backup the old version
  201. Safe::unlink($context['path_to_root'].'parameters/collections.include.php.bak');
  202. Safe::rename($context['path_to_root'].'parameters/collections.include.php', $context['path_to_root'].'parameters/collections.include.php.bak');
  203. // build the new configuration file
  204. $content = '<?php'."\n"
  205. .'// This file has been created by the configuration script collections/configure.php'."\n"
  206. .'// on '.gmdate("F j, Y, g:i a").' GMT, for '.Surfer::get_name().'. Please do not modify it manually.'."\n"
  207. .'global $context;'."\n";
  208. foreach($_REQUEST['collection_urls'] as $index => $value) {
  209. $name = addcslashes($_REQUEST['collection_names'][$index], "\\'");
  210. $title = addcslashes($_REQUEST['collection_titles'][$index], "\\'");
  211. $path = addcslashes($_REQUEST['collection_paths'][$index], "\\'");
  212. $url = addcslashes($_REQUEST['collection_urls'][$index], "\\'");
  213. $introduction = addcslashes($_REQUEST['collection_introductions'][$index], "\\'");
  214. $description = addcslashes($_REQUEST['collection_descriptions'][$index], "\\'");
  215. $prefix = addcslashes($_REQUEST['collection_prefixes'][$index], "\\'");
  216. $suffix = addcslashes($_REQUEST['collection_suffixes'][$index], "\\'");
  217. $visibility = addcslashes($_REQUEST['collection_visibilities'][$index], "\\'");
  218. if($name && $path && $url) {
  219. $content .= '$context[\'collections\'][\''.$name.'\']=array(\''.$title.'\', \''
  220. .$path.'\', \''.$url.'\', \''
  221. .$introduction.'\', \''.$description.'\', \''
  222. .$prefix.'\', \''.$suffix.'\', \''.$visibility."');\n";
  223. }
  224. }
  225. $content .= '?>'."\n";
  226. // update the parameters file
  227. if(!Safe::file_put_contents('parameters/collections.include.php', $content)) {
  228. Logger::error(sprintf(i18n::s('ERROR: Impossible to write to the file %s. The configuration has not been saved.'), 'parameters/collections.include.php'));
  229. // allow for a manual update
  230. $context['text'] .= '<p style="text-decoration: blink;">'.sprintf(i18n::s('To actually change the configuration, please copy and paste following lines by yourself in file %s.'), 'parameters/collections.include.php')."</p>\n";
  231. // job done
  232. } else {
  233. $context['text'] .= '<p>'.sprintf(i18n::s('The following configuration has been saved into the file %s.'), 'parameters/collections.include.php')."</p>\n";
  234. // purge the cache
  235. Cache::clear();
  236. // remember the change
  237. $label = sprintf(i18n::c('%s has been updated'), 'parameters/collections.include.php');
  238. Logger::remember('collections/configure.php', $label);
  239. }
  240. // display updated parameters
  241. $context['text'] .= Skin::build_box(i18n::s('Configuration parameters'), Safe::highlight_string($content), 'folded');
  242. // follow-up commands
  243. $follow_up = i18n::s('Where do you want to go now?');
  244. $menu = array();
  245. $menu = array_merge($menu, array( 'collections/' => i18n::s('File collections') ));
  246. $menu = array_merge($menu, array( 'control/' => i18n::s('Control Panel') ));
  247. $menu = array_merge($menu, array( 'collections/configure.php' => i18n::s('Configure again') ));
  248. $follow_up .= Skin::build_list($menu, 'menu_bar');
  249. $context['text'] .= Skin::build_block($follow_up, 'bottom');
  250. }
  251. // render the skin
  252. render_skin();
  253. ?>