if ($postid) {
        $post = forum_post::get_from_id($postid, $cloneid);
        $post->require_view();
        $forum = $post->get_forum();
    } else {
        $forum = forum::get_from_cmid($cmid, $cloneid);
        $forum->require_view(forum::NO_GROUPS);
    }
    print_header();
    $um = $forum->get_upload_manager('file');
    $um->config->allownull = false;
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $playspaceid = required_param('attachmentplayspace', PARAM_SEQUENCE);
        $ok = $um->preprocess_files();
        if ($ok && ($name = $um->get_new_filename())) {
            $um->save_files(forum::get_attachment_playspace_folder($playspaceid));
            ?>
<script type="text/javascript">
window.opener.currentform.addattachment("<?php 
            print addslashes_js($name);
            ?>
");
window.close();
</script><?php 
            print_footer('empty');
            exit;
        } else {
            print $um->get_errors();
        }
    }
    $playspaceid = optional_param('attachmentplayspace', 0, PARAM_SEQUENCE);
 $draftid = optional_param('draft', 0, PARAM_INT);
 if ($draftid) {
     $draft = forum_draft::get_from_id($draftid);
     if (!$draft->is_reply() || $draft->get_discussion_id() != $discussionid) {
         print_error('draft_mismatch', 'forumng', $forum->get_url(forum::PARAM_HTML));
     }
     $root = $discussion->get_root_post();
     $inreplyto = $root->find_child($draft->get_parent_post_id(), false);
     if (!$inreplyto || !$inreplyto->can_reply($whynot) || !$discussion->can_view()) {
         print_error('draft_cannotreply', 'forumng', $forum->get_url(forum::PARAM_HTML), get_string($whynot, 'forumng'));
     }
     $inreplyto->force_expand();
     $draftplayspaceid = 0;
     if ($draft->has_attachments()) {
         $draftplayspaceid = forum::create_attachment_playspace();
         $target = forum::get_attachment_playspace_folder($draftplayspaceid);
         $source = $draft->get_attachment_folder();
         foreach ($draft->get_attachment_names() as $name) {
             forum_utils::copy("{$source}/{$name}", "{$target}/{$name}");
         }
     }
 }
 // Check that discussion can be viewed [Handles all other permissions]
 $discussion->require_view();
 // Search form for header
 $buttontext = $forum->display_search_form();
 // Atom header meta tag
 $feedtype = $forum->get_effective_feed_option();
 if ($feedtype == forum::FEEDTYPE_ALL_POSTS) {
     $atomurl = $discussion->get_feed_url(forum::FEEDFORMAT_ATOM);
     $meta = '<link rel="alternate" type="application/atom+xml" ' . 'title="Atom feed" href="' . htmlspecialchars($atomurl) . '" />';
 /**
  * If you add an attachment to a post then do not finish it, this will
  * create a 'zombie playspace' where the attachment is stored. This function
  * deletes all playspaces older than 24 hours.
  */
 public static function delete_old_playspaces()
 {
     $start = microtime(true);
     mtrace('Deleting old attachment playspaces ', '');
     // Find folder used for attachment playspaces
     $folder = dirname(forum::get_attachment_playspace_folder('1,1'));
     if (!is_dir($folder)) {
         // No playspaces at all
         mtrace('[folder not found]');
         return;
     }
     self::clear_old_folders($start, $folder);
 }