PageRenderTime 62ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/images/delete.php

https://github.com/agnesrambaud/yacs
PHP | 188 lines | 94 code | 35 blank | 59 comment | 32 complexity | a5995cce19c8d71504563932a9fd7b8f MD5 | raw file
  1. <?php
  2. /**
  3. * delete an image
  4. *
  5. * This script calls for confirmation, then actually deletes the image.
  6. * It updates the database, then redirects to the anchor page.
  7. *
  8. * Restrictions apply on this page:
  9. * - associates and authenticated editors are allowed to move forward
  10. * - permission is denied if the anchor is not viewable by this surfer
  11. * - permission is granted if the anchor is the profile of this member
  12. * - authenticated users may suppress their own posts
  13. * - else permission is denied
  14. *
  15. * Accept following invocations:
  16. * - delete.php/12
  17. * - delete.php?id=12
  18. *
  19. * If the anchor for this item specifies a specific skin (option keyword '[code]skin_xyz[/code]'),
  20. * or a specific variant (option keyword '[code]variant_xyz[/code]'), they are used instead default values.
  21. *
  22. * @author Bernard Paques
  23. * @author GnapZ
  24. * @tester Guillaume Perez
  25. * @reference
  26. * @license http://www.gnu.org/copyleft/lesser.txt GNU Lesser General Public License
  27. */
  28. // common definitions and initial processing
  29. include_once '../shared/global.php';
  30. include_once 'images.php';
  31. // look for the id
  32. $id = NULL;
  33. if(isset($_REQUEST['id']))
  34. $id = $_REQUEST['id'];
  35. elseif(isset($context['arguments'][0]))
  36. $id = $context['arguments'][0];
  37. $id = strip_tags($id);
  38. // get the item from the database
  39. $item =& Images::get($id);
  40. // get the related anchor, if any
  41. $anchor = NULL;
  42. if(isset($item['anchor']) && $item['anchor'])
  43. $anchor =& Anchors::get($item['anchor']);
  44. // associates and authenticated editors can do what they want
  45. if(Surfer::is_associate() || (Surfer::is_member() && is_object($anchor) && $anchor->is_assigned()))
  46. $permitted = TRUE;
  47. // the anchor has to be viewable by this surfer
  48. elseif(is_object($anchor) && !$anchor->is_viewable())
  49. $permitted = FALSE;
  50. // the item is anchored to the profile of this member
  51. elseif(Surfer::is_member() && !strcmp($item['anchor'], 'user:'.Surfer::get_id()))
  52. $permitted = TRUE;
  53. // authenticated surfers may suppress their own posts --no create_id yet...
  54. elseif(isset($item['edit_id']) && Surfer::is($item['edit_id']))
  55. $permitted = TRUE;
  56. // the default is to deny access
  57. else
  58. $permitted = FALSE;
  59. // load the skin, maybe with a variant
  60. load_skin('images', $anchor);
  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( 'images/' => i18n::s('Images') );
  69. // the title of the page
  70. $context['page_title'] = i18n::s('Delete an image');
  71. // not found
  72. if(!isset($item['id'])) {
  73. include '../error.php';
  74. // permission denied
  75. } elseif(!$permitted) {
  76. Safe::header('Status: 401 Forbidden', TRUE, 401);
  77. Logger::error(i18n::s('You are not allowed to perform this operation.'));
  78. // deletion is confirmed
  79. } elseif(isset($_REQUEST['confirm']) && ($_REQUEST['confirm'] == 'yes')) {
  80. // touch the related anchor before actual deletion, since the image has to be accessible at that time
  81. if(is_object($anchor))
  82. $anchor->touch('image:delete', $item['id']);
  83. // if no error, back to the anchor or to the index page
  84. if(Images::delete($item['id'])) {
  85. Images::clear($item);
  86. if(is_object($anchor))
  87. Safe::redirect($context['url_to_home'].$context['url_to_root'].$anchor->get_url());
  88. else
  89. Safe::redirect($context['url_to_home'].$context['url_to_root'].'images/');
  90. }
  91. // deletion has to be confirmed
  92. } elseif(isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'POST'))
  93. Logger::error(i18n::s('The action has not been confirmed.'));
  94. // ask for confirmation
  95. else {
  96. // commands
  97. $menu = array();
  98. $menu[] = Skin::build_submit_button(i18n::s('Yes, I want to delete this image'), NULL, NULL, 'confirmed');
  99. if(isset($item['id']))
  100. $menu[] = Skin::build_link(Images::get_url($item['id']), i18n::s('Cancel'), 'span');
  101. // the submit button
  102. $context['text'] .= '<form method="post" action="'.$context['script_url'].'" id="main_form"><p>'."\n"
  103. .Skin::finalize_list($menu, 'menu_bar')
  104. .'<input type="hidden" name="id" value="'.$item['id'].'" />'."\n"
  105. .'<input type="hidden" name="confirm" value="yes" />'."\n"
  106. .'</p></form>'."\n";
  107. // set the focus
  108. $context['text'] .= JS_PREFIX
  109. .'// set the focus on first form field'."\n"
  110. .'$("confirmed").focus();'."\n"
  111. .JS_SUFFIX."\n";
  112. // the title of the image
  113. if($item['title'])
  114. $context['text'] .= Skin::build_block($item['title'], 'title');
  115. else
  116. $context['text'] .= Skin::build_block($item['image_name'], 'title');
  117. // display the full text
  118. $context['text'] .= '<div style="margin: 1em 0;">'.Codes::beautify($item['description']).'</div>'."\n";
  119. // build the path to the image file
  120. list($anchor_type, $anchor_id) = explode(':', $item['anchor']);
  121. $url = $anchor_type.'/'.$anchor_id.'/'.$item['image_name'];
  122. $context['text'] .= "\n<p>".'<img src="'.$context['url_to_root'].'images/'.$url.'" alt="" /></p>';
  123. // details
  124. $details = array();
  125. // the image name, if it has not already been used as title
  126. if($item['title'])
  127. $details[] = $item['image_name'];
  128. // file size
  129. if($item['image_size'] > 1)
  130. $details[] = number_format($item['image_size']).'&nbsp;'.i18n::s('bytes');
  131. // information on uploader
  132. if(Surfer::is_member())
  133. $details[] = sprintf(i18n::s('edited by %s %s'), Users::get_link($item['edit_name'], $item['edit_address'], $item['edit_id']), Skin::build_date($item['edit_date']));
  134. // the complete details
  135. if($details)
  136. $context['text'] .= '<p class="details">'.ucfirst(implode(', ', $details))."</p>\n";
  137. // display the source, if any
  138. if($item['source']) {
  139. if(preg_match('/http:\/\/([^\s]+)/', $item['source'], $matches))
  140. $item['source'] = Skin::build_link($matches[0], $matches[0], 'external');
  141. else {
  142. include_once '../links/links.php';
  143. if($attributes = Links::transform_reference($item['source'])) {
  144. list($link, $title, $description) = $attributes;
  145. $item['source'] = Skin::build_link($link, $title);
  146. }
  147. }
  148. $context['text'] .= '<p class="details">'.sprintf(i18n::s('Source: %s'), $item['source'])."</p>\n";
  149. }
  150. }
  151. // render the skin
  152. render_skin();
  153. ?>