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

/articles/duplicate.php

https://github.com/bernard357/yacs
PHP | 253 lines | 124 code | 53 blank | 76 comment | 30 complexity | fc7763001bb94fa7cd8f52895042a0de MD5 | raw file
  1. <?php
  2. /**
  3. * duplicate an article
  4. *
  5. * This script calls for confirmation, then actually duplicates an article.
  6. * Images of the original article are duplicated as well, as other attached
  7. * items such as files, links, or comments, but also tables and locations.
  8. * It updates the database, then redirects to the anchor page, or to the
  9. * index page for articles.
  10. *
  11. * Restrictions apply on this page:
  12. * - associates and authenticated editors are allowed to move forward
  13. * - permission is denied if the anchor is not viewable by this surfer
  14. * - logged surfers may decide to duplicate their own posts
  15. * - else permission is denied
  16. *
  17. * Accept following invocations:
  18. * - duplicate.php/12
  19. * - duplicate.php?id=12
  20. *
  21. * If this article, or one of its anchor, specifies a specific skin (option keyword '[code]skin_xyz[/code]'),
  22. * or a specific variant (option keyword '[code]variant_xyz[/code]'), they are used instead default values.
  23. *
  24. * @author Bernard Paques
  25. * @author GnapZ
  26. * @tester Fernand Le Chien
  27. * @tester Christophe Battarel [email]christophe.battarel@altairis.fr[/email]
  28. * @reference
  29. * @license http://www.gnu.org/copyleft/lesser.txt GNU Lesser General Public License
  30. */
  31. // common definitions and initial processing
  32. include_once '../shared/global.php';
  33. // look for the article id
  34. $id = NULL;
  35. if(isset($_REQUEST['id']))
  36. $id = $_REQUEST['id'];
  37. elseif(isset($context['arguments'][0]))
  38. $id = $context['arguments'][0];
  39. $id = strip_tags($id);
  40. // get the item from the database
  41. $item = Articles::get($id);
  42. // get the related anchor
  43. $anchor = NULL;
  44. if(isset($item['anchor']) && $item['anchor'])
  45. $anchor = Anchors::get($item['anchor']);
  46. // get the related overlay, if any
  47. $overlay = NULL;
  48. if(isset($item['overlay']))
  49. $overlay = Overlay::load($item, 'article:'.$item['id']);
  50. // load the skin, maybe with a variant
  51. load_skin('articles', $anchor, isset($item['options']) ? $item['options'] : '');
  52. // clear the tab we are in, if any
  53. if(is_object($anchor))
  54. $context['current_focus'] = $anchor->get_focus();
  55. // path to this page
  56. $context['path_bar'] = Surfer::get_path_bar($anchor);
  57. if(isset($item['id']))
  58. $context['path_bar'] = array_merge($context['path_bar'], array(Articles::get_permalink($item) => $item['title']));
  59. // page title
  60. if(isset($item['id']))
  61. $context['page_title'] = sprintf(i18n::s('%s: %s'), i18n::s('Duplicate'), $item['title']);
  62. // not found
  63. if(!isset($item['id'])) {
  64. include '../error.php';
  65. // permission denied
  66. } elseif(!Articles::is_owned($item, $anchor)) {
  67. Safe::header('Status: 401 Unauthorized', TRUE, 401);
  68. Logger::error(i18n::s('You are not allowed to perform this operation.'));
  69. // action is confirmed
  70. } elseif(isset($_REQUEST['action']) && ($_REQUEST['action'] == 'duplicate')) {
  71. // to duplicate related items
  72. $original_anchor = 'article:'.$item['id'];
  73. // we will get a new id and a new handle
  74. unset($item['id']);
  75. unset($item['handle']);
  76. // the duplicator becomes the author
  77. unset($item['create_address']);
  78. unset($item['create_date']);
  79. unset($item['create_id']);
  80. unset($item['create_name']);
  81. unset($item['edit_address']);
  82. unset($item['edit_date']);
  83. unset($item['edit_id']);
  84. unset($item['edit_name']);
  85. // page will have to be published after modification
  86. unset($item['publish_address']);
  87. unset($item['publish_date']);
  88. unset($item['publish_id']);
  89. unset($item['publish_name']);
  90. // ensure this is a copy
  91. $item['title'] = sprintf(i18n::s('Copy of %s'), $item['title']);
  92. // create a new page
  93. if($item['id'] = Articles::post($item)) {
  94. // also duplicate the provided overlay, if any -- re-use 'overlay_type' only
  95. $overlay = Overlay::load($item, 'article:'.$item['id']);
  96. // post an overlay, with the new article id
  97. if(is_object($overlay))
  98. $overlay->remember('insert', $item, 'article:'.$item['id']);
  99. // duplicate all related items, images, etc.
  100. Anchors::duplicate_related_to($original_anchor, 'article:'.$item['id']);
  101. // if poster is a registered user
  102. if(Surfer::get_id()) {
  103. // increment the post counter of the surfer
  104. Users::increment_posts(Surfer::get_id());
  105. // add this page to watch list
  106. Members::assign('article:'.$item['id'], 'user:'.Surfer::get_id());
  107. }
  108. // get the new item
  109. $article = Anchors::get('article:'.$item['id'], TRUE);
  110. $context['page_title'] = i18n::s('Thank you for your contribution');
  111. // the page has been duplicated
  112. $context['text'] .= '<p>'.i18n::s('The page has been duplicated.').'</p>';
  113. // follow-up commands
  114. $follow_up = i18n::s('What do you want to do now?');
  115. $menu = array();
  116. $menu = array_merge($menu, array($article->get_url() => i18n::s('View the page')));
  117. $menu = array_merge($menu, array($article->get_url('edit') => i18n::s('Edit the page')));
  118. if(Surfer::may_upload()) {
  119. $menu = array_merge($menu, array('images/edit.php?anchor='.urlencode($article->get_reference()) => i18n::s('Add an image')));
  120. $menu = array_merge($menu, array('files/edit.php?anchor='.urlencode($article->get_reference()) => i18n::s('Add a file')));
  121. }
  122. $menu = array_merge($menu, array('links/edit.php?anchor='.urlencode($article->get_reference()) => i18n::s('Add a link')));
  123. $follow_up .= Skin::build_list($menu, 'menu_bar');
  124. $context['text'] .= Skin::build_block($follow_up, 'bottom');
  125. // log the creation of a new article
  126. $label = sprintf(i18n::c('Article copy: %s'), strip_tags($article->get_title()));
  127. // poster and target section
  128. if(is_object($anchor))
  129. $description = sprintf(i18n::c('Sent by %s in %s'), Surfer::get_name(), $anchor->get_title());
  130. else
  131. $description = sprintf(i18n::c('Sent by %s'), Surfer::get_name());
  132. // title and link
  133. if($title = $article->get_title())
  134. $description .= $title."\n";
  135. $description = '<a href="'.$context['url_to_home'].$context['url_to_root'].$article->get_url().'">'.$article->get_title().'</a>';
  136. // notify sysops
  137. Logger::notify('articles/duplicate.php: '.$label, $description);
  138. }
  139. // action has to be confirmed
  140. } elseif(isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] == 'POST')) {
  141. Logger::error(i18n::s('The action has not been confirmed.'));
  142. // please confirm
  143. } else {
  144. // the article or the anchor icon, if any
  145. $context['page_image'] = $item['icon_url'];
  146. if(!$context['page_image'] && is_object($anchor))
  147. $context['page_image'] = $anchor->get_icon_url();
  148. // commands
  149. $menu = array();
  150. $menu[] = Skin::build_submit_button(i18n::s('Yes, I want to duplicate this page'), NULL, NULL, 'confirmed');
  151. if(isset($item['id']))
  152. $menu[] = Skin::build_link(Articles::get_permalink($item), i18n::s('Cancel'), 'span');
  153. // render commands
  154. $context['text'] .= '<form method="post" action="'.$context['script_url'].'" id="main_form"><p>'."\n"
  155. .Skin::finalize_list($menu, 'menu_bar')
  156. .'<input type="hidden" name="id" value="'.$item['id'].'" />'."\n"
  157. .'<input type="hidden" name="action" value="duplicate" />'."\n"
  158. .'</p></form>'."\n";
  159. // set the focus
  160. $context['text'] .= JS_PREFIX
  161. .'// set the focus on first form field'."\n"
  162. .'$("#confirmed").focus();'."\n"
  163. .JS_SUFFIX;
  164. // the title of the action
  165. $context['text'] .= Skin::build_block($item['title'], 'title');
  166. // the introduction text, if any
  167. $context['text'] .= '<div style="margin: 1em 0;">'.Codes::beautify($item['introduction']).'</div>'."\n";
  168. // get text related to the overlay, if any
  169. if(is_object($overlay))
  170. $context['text'] .= $overlay->get_text('view', $item);
  171. // details
  172. $details = array();
  173. // last edition
  174. if($item['edit_name'])
  175. $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']));
  176. // hits
  177. if($item['hits'] > 1)
  178. $details[] = Skin::build_number($item['hits'], i18n::s('hits'));
  179. // all details
  180. if(@count($details))
  181. $context['text'] .= '<p class="details">'.ucfirst(implode(', ', $details))."</p>\n";
  182. // display the source, if any
  183. if($item['source']) {
  184. if(preg_match('/http:\/\/([^\s]+)/', $item['source'], $matches))
  185. $item['source'] = Skin::build_link($matches[0], $matches[0], 'external');
  186. else {
  187. include_once '../links/links.php';
  188. if($attributes = Links::transform_reference($item['source'])) {
  189. list($link, $title, $description) = $attributes;
  190. $item['source'] = Skin::build_link($link, $title);
  191. }
  192. }
  193. $context['text'] .= '<p class="details">'.sprintf(i18n::s('Source: %s'), $item['source'])."</p>\n";
  194. }
  195. // count items related to this article
  196. $context['text'] .= Anchors::stat_related_to('article:'.$item['id'], i18n::s('Following items are attached to this record and will be duplicated as well.'));
  197. }
  198. // render the skin
  199. render_skin();
  200. ?>