PageRenderTime 75ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tables/edit.php

https://github.com/yacs/yacs
PHP | 283 lines | 147 code | 57 blank | 79 comment | 48 complexity | f1d6048e13e0b62123ac8dc2ef1236b6 MD5 | raw file
  1. <?php
  2. /**
  3. * upload a new table or update an existing one
  4. *
  5. * This script attempts to validate the new or updated article description against a standard PHP XML parser.
  6. * The objective is to spot malformed or unordered HTML and XHTML tags. No more, no less.
  7. *
  8. * Only associates and owners can post and edit tables.
  9. *
  10. * Accepted calls:
  11. * - edit.php/&lt;type&gt;/&lt;id&gt; create a new table for this anchor
  12. * - edit.php?anchor=&lt;type&gt;:&lt;id&gt; create a new table for the anchor
  13. * - edit.php/&lt;id&gt; modify an existing table
  14. * - edit.php?id=&lt;id&gt; modify an existing table
  15. *
  16. * If the anchor for this item specifies a specific skin (option keyword '[code]skin_xyz[/code]'),
  17. * or a specific variant (option keyword '[code]variant_xyz[/code]'), they are used instead default values.
  18. *
  19. * @author Bernard Paques
  20. * @author Vincent No&euml;l
  21. * @author GnapZ
  22. * @reference
  23. * @license http://www.gnu.org/copyleft/lesser.txt GNU Lesser General Public License
  24. */
  25. // common definitions and initial processing
  26. include_once '../shared/global.php';
  27. include_once 'tables.php';
  28. // look for the id
  29. $id = NULL;
  30. if(isset($_REQUEST['id']))
  31. $id = $_REQUEST['id'];
  32. elseif(isset($context['arguments'][0]) && !isset($context['arguments'][1]))
  33. $id = $context['arguments'][0];
  34. $id = strip_tags($id);
  35. // get the item from the database
  36. $item = Tables::get($id);
  37. // look for the target anchor on item creation
  38. $target_anchor = NULL;
  39. if(isset($_REQUEST['anchor']))
  40. $target_anchor = $_REQUEST['anchor'];
  41. if(!isset($target_anchor) && isset($context['arguments'][1]))
  42. $target_anchor = $context['arguments'][0].':'.$context['arguments'][1];
  43. // get the related anchor, if any
  44. $anchor = NULL;
  45. if(isset($item['anchor']))
  46. $anchor = Anchors::get($item['anchor']);
  47. elseif($target_anchor)
  48. $anchor = Anchors::get($target_anchor);
  49. // associates and owners can do what they want
  50. if(Surfer::is_associate() || (is_object($anchor) && $anchor->is_owned()))
  51. $permitted = TRUE;
  52. // the default is to disallow access
  53. else
  54. $permitted = FALSE;
  55. // do not always show the edition form
  56. $with_form = FALSE;
  57. // load the skin, maybe with a variant
  58. load_skin('tables', $anchor);
  59. // do not index this page
  60. $context->sif('robots','noindex');
  61. // clear the tab we are in, if any
  62. if(is_object($anchor))
  63. $context['current_focus'] = $anchor->get_focus();
  64. // the path to this page
  65. if(is_object($anchor) && $anchor->is_viewable())
  66. $context['path_bar'] = $anchor->get_path_bar();
  67. else
  68. $context['path_bar'] = array( 'tables/' => i18n::s('Tables') );
  69. // the title of the page
  70. if(isset($item['id']))
  71. $context['page_title'] = i18n::s('Edit a table');
  72. else
  73. $context['page_title'] = i18n::s('Add a table');
  74. // stop crawlers
  75. if(Surfer::is_crawler()) {
  76. Safe::header('Status: 401 Unauthorized', TRUE, 401);
  77. Logger::error(i18n::s('You are not allowed to perform this operation.'));
  78. // an anchor is mandatory
  79. } elseif(!is_object($anchor)) {
  80. Safe::header('Status: 404 Not Found', TRUE, 404);
  81. Logger::error(i18n::s('No anchor has been found.'));
  82. // permission denied
  83. } elseif(!$permitted) {
  84. // anonymous users are invited to log in or to register
  85. if(!Surfer::is_logged()) {
  86. if(isset($item['id']))
  87. $link = Tables::get_url($item['id'], 'edit');
  88. elseif(isset($_REQUEST['anchor']))
  89. $link = 'tables/edit.php?anchor='.urlencode($_REQUEST['anchor']);
  90. else
  91. $link = 'tables/edit.php';
  92. Safe::redirect($context['url_to_home'].$context['url_to_root'].'users/login.php?url='.urlencode($link));
  93. }
  94. // permission denied to authenticated user
  95. Safe::header('Status: 401 Unauthorized', TRUE, 401);
  96. Logger::error(i18n::s('You are not allowed to perform this operation.'));
  97. // maybe posts are not allowed here
  98. } elseif(!isset($item['id']) && $anchor->has_option('locked') && !Surfer::is_empowered()) {
  99. Safe::header('Status: 401 Unauthorized', TRUE, 401);
  100. Logger::error(i18n::s('This page has been locked.'));
  101. // an error occured
  102. } elseif(count($context['error'])) {
  103. $item = $_REQUEST;
  104. $with_form = TRUE;
  105. // process uploaded data
  106. } elseif(isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'POST')) {
  107. // the follow-up page
  108. $next = $anchor->get_permalink();
  109. // display the form on error
  110. if(!$_REQUEST['id'] = Tables::post($_REQUEST)) {
  111. $item = $_REQUEST;
  112. $with_form = TRUE;
  113. // post-processing
  114. } else {
  115. // a new post
  116. if(!$item['id']) {
  117. // touch the related anchor
  118. $anchor->touch('table:create', $_REQUEST['id'], isset($_REQUEST['silent']) && ($_REQUEST['silent'] == 'Y'));
  119. // clear cache
  120. Tables::clear($_REQUEST);
  121. // increment the post counter of the surfer
  122. Users::increment_posts(Surfer::get_id());
  123. // an update
  124. } else {
  125. // touch the related anchor
  126. $anchor->touch('table:update', $_REQUEST['id'], isset($_REQUEST['silent']) && ($_REQUEST['silent'] == 'Y'));
  127. // clear cache
  128. Tables::clear($_REQUEST);
  129. }
  130. // go to the updated page
  131. Safe::redirect($next);
  132. }
  133. // display the form on GET
  134. } else
  135. $with_form = TRUE;
  136. // display the form
  137. if($with_form) {
  138. // reference the anchor page
  139. if(is_object($anchor) && $anchor->is_viewable())
  140. $context['text'] .= '<p>'.sprintf(i18n::s('In: %s'), Skin::build_link($anchor->get_url(), $anchor->get_title()))."</p>\n";
  141. // the form to edit an table
  142. $context['text'] .= '<form method="post" action="'.$context['script_url'].'" onsubmit="return validateDocumentPost(this)" id="main_form"><div>';
  143. // encode fields
  144. $fields = array();
  145. // display info on current version
  146. if(isset($item['id'])) {
  147. // the last poster
  148. if(isset($item['edit_id'])) {
  149. $text = Users::get_link($item['edit_name'], $item['edit_address'], $item['edit_id'])
  150. .' '.Skin::build_date($item['edit_date']);
  151. $fields[] = array(i18n::s('Posted by'), $text);
  152. }
  153. }
  154. // the title
  155. $label = i18n::s('Title');
  156. $input = '<textarea name="title" id="title" rows="2" cols="50">'.encode_field(isset($item['title']) ? $item['title'] : '').'</textarea>';
  157. $hint = i18n::s('Please provide a meaningful title.');
  158. $fields[] = array($label, $input, $hint);
  159. // the query
  160. $label = i18n::s('SQL Query');
  161. $input = '<textarea name="query" rows="15" cols="50">'.encode_field(isset($item['query']) ? $item['query'] : '').'</textarea>';
  162. $hint = i18n::s('The SELECT command submitted to the database');
  163. $fields[] = array($label, $input, $hint);
  164. // is the first row an url to the zoom page?
  165. $label = i18n::s('First column');
  166. $input = '<input type="radio" name="with_zoom" value="N"';
  167. if(!isset($item['with_zoom']) || ($item['with_zoom'] == 'N'))
  168. $input .= ' checked="checked"';
  169. $input .= '/> '.i18n::s('First column contains useful data')
  170. .BR."\n".'<input type="radio" name="with_zoom" value="T"';
  171. if(isset($item['with_zoom']) && ($item['with_zoom'] == 'T'))
  172. $input .= ' checked="checked"';
  173. $input .= '/> '.i18n::s('First column refers to time')
  174. .BR."\n".'<input type="radio" name="with_zoom" value="Y"';
  175. if(isset($item['with_zoom']) && ($item['with_zoom'] == 'Y'))
  176. $input .= ' checked="checked"';
  177. $input .= '/> '.i18n::s('First column provides a web address');
  178. $fields[] = array($label, $input);
  179. // the description
  180. $label = i18n::s('Description');
  181. $input = '<textarea name="description" rows="5" cols="50">'.encode_field(isset($item['description']) ? $item['description'] : '').'</textarea>';
  182. $hint = i18n::s('As this field may be searched by surfers, please choose adequate searchable words');
  183. $fields[] = array($label, $input, $hint);
  184. // build the form
  185. $context['text'] .= Skin::build_form($fields);
  186. // bottom commands
  187. $menu = array();
  188. $menu[] = Skin::build_submit_button(i18n::s('Submit'), i18n::s('Press [s] to submit data'), 's');
  189. if(is_object($anchor) && $anchor->is_viewable())
  190. $menu[] = Skin::build_link($anchor->get_url(), i18n::s('Cancel'), 'span');
  191. $context['text'] .= Skin::finalize_list($menu, 'assistant_bar');
  192. // associates may decide to not stamp changes -- complex command
  193. if(Surfer::is_associate() && Surfer::has_all())
  194. $context['text'] .= '<p><input type="checkbox" name="silent" value="Y" /> '.i18n::s('Do not change modification date of the main page.').'</p>';
  195. // transmit the id as a hidden field
  196. if(isset($item['id']) && $item['id'])
  197. $context['text'] .= '<input type="hidden" name="id" value="'.$item['id'].'" />';
  198. // other hidden fields
  199. $context['text'] .= '<input type="hidden" name="anchor" value="'.$anchor->get_reference().'" />';
  200. // end of the form
  201. $context['text'] .= '</div></form>';
  202. // the script used for form handling at the browser
  203. Page::insert_script(
  204. // check that main fields are not empty
  205. ' func'.'tion validateDocumentPost(container) {'."\n"
  206. // query is mandatory
  207. .' if(!container.query.value) {'."\n"
  208. .' alert("'.i18n::s('Please type a valid SQL query.').'");'."\n"
  209. .' Yacs.stopWorking();'."\n"
  210. .' return false;'."\n"
  211. .' }'."\n"
  212. // successful check
  213. .' return true;'."\n"
  214. .' }'."\n"
  215. // set the focus on first form field
  216. .'$("#title").focus();'."\n"
  217. );
  218. // the help panel
  219. $help = '<p>'.i18n::s('Please ensure you are using a compliant and complete SQL SELECT statement.').'</p>'
  220. .'<p>'.sprintf(i18n::s('For more information check the %s.'), Skin::build_link('http://dev.mysql.com/doc/mysql/en/select.html', i18n::s('MySQL reference page'), 'external')).'</p>'
  221. .'<p>'.sprintf(i18n::s('%s and %s are available to enhance text rendering.'), Skin::build_link('codes/', i18n::s('YACS codes'), 'open'), Skin::build_link('smileys/', i18n::s('smileys'), 'open')).'</p>';
  222. $context['components']['boxes'] = Skin::build_box(i18n::s('Help'), $help, 'boxes', 'help');
  223. }
  224. // render the skin
  225. render_skin();
  226. ?>