<?php
        
        # Change a 'posted' file to 'draft'
        
        include_once 'data_connect.php';
        include 'log.php';
        
        
        if (isset($_GET['editfile'])) {
            
            $nu_file = $_GET['editfile'];
            
            
            if (is_file($nu_file)) {
                
                # get the 'normal' and 'nu' xml files
                $normal_file = str_replace('nu','',$nu_file);
            
                # check if the normal file is there
                if (is_file($normal_file)) {

                    # update the information of the normal file
                    $_draft = getxmlpost($normal_file);

                    $_draft['type'] = 'Draft';
                    $_draft['ptime'] = '';
                    $_draft['modifed'] = time();
                    $creator = $_draft['createdby'];

                    
                    # if the post is created by the user or the user has admin rights
                    if ( get_account($_SESSION['userID'])['rights'] == 'administrator' || $creator == $_SESSION['userID'] ) {

                        # delete the nu file
                        unlink ($nu_file);

                        # save normal file
                        simplexml_object_save($normal_file,$_draft,'post');

                        # keep the latest saved version of a post
                        $cfile = 'autosaves/content/nu'.$normal_file.'.htm';
                        if (is_file($cfile)) {

                            $ccfile = file_get_contents($cfile);
                            file_put_contents('autosaves/content/'.$normal_file.'.htm',$ccfile);

                        }

                        # delete the post file
                        if (isset($_GET['link'])) {

                            $pl = '../'.$_GET['link'].'.php';
                            if (is_file($pl))
                                unlink($pl);

                            $_SESSION['change_to_draft'] = true;
                            

                        }
                    }
                }
            }
        }


        # Keep or remove some GET values


        $plink = '';


        # Get existing GET parameters
        if (count($_GET) > 2) {

            #'link' 'editfile'
            # We don't want to resend these values, so we remove them from $plink
            $x=0;
            foreach($_GET as $param=>$val) {

                if ($param != 'editfile' && $param != 'link') {
                
                    $front = ($x == 0) ? '?' : '&';
                    $plink .= $front.$param.'='.$val;
                    $x++;
                
                }
            }
        }



        header('Location:opensaved.php'.$plink)
        
        
    
?>