<?php

    # editpost.php
    # Delete a post file or change a 'Posted' or 'Posted/Not updated' file, to 'Draft'


    include_once 'data_connect.php';
    include_once 'log.php';

    # Change to draft
    if (isset($_GET['editfile'])) {


        # This can be the 'nu' file, if the file has an 'nu' version
        $draft_file = $_GET['editfile'];

        if (is_file('autosaves/'.$draft_file)) {


            $normal_file = str_replace('nu.xml','.xml',$draft_file);

            $draft = simplexml_object($draft_file,'e','post');

            $type = (string)$draft->postinfo->post->type;
            $creator = (string)$draft->postinfo->post->createdby;
            $filename = (string)$draft->postinfo->post->filename;
            $path = (string)$draft->postinfo->post->path;

            $draft->postinfo->post->type = 'Draft';
            $draft->postinfo->post->path = '';
            $draft->postinfo->post->ptime = '';
            $draft->postinfo->post->modified = time();



            # The post must be created by the user or the user must have admin rights
            if (get_account($_SESSION['userID'])['rights'] == 'administrator' || $creator == $_SESSION['userID']) {
                //echo $path.$filename;exit;

                $files = array($draft_file,$normal_file);

                foreach($files as $file)
                    if (is_file('autosaves/'.$file));
                        unlink('autosaves/'.$file);

                unset($file);



                simplexml_object_save($normal_file,$draft,'post');
                simplexml_object_save($draft_file,$draft,'post');


                # Keep the latest saved version of a post
                $nu_cfile = 'autosaves/content/'.$normal_file.'nu.htm';
                if (is_file($nu_cfile)) {
                    $contents_of_nu_cfile = file_get_contents($nu_cfile);
                    file_put_contents('autosaves/content/'.$normal_file.'.htm',$contents_of_nu_cfile);
                }



                # Delete the published file
                $pf = '../'.$path.$filename.'.php';

                if (is_file($pf) && $type != 'Draft') {

                    unlink($pf);
                    $_SESSION['MONchanged_to_draft'] = true;
                    
                }
            }
        }
    }



    else {


        # Delete
        if (isset($_GET['file'])) {


            $del = $_GET['file'];


            //$del_link = $_GET['link'];
            //link -> $path.$filename.$nes at opensaved.php
            
            delete_post($del);



            $_SESSION['MONdeleted'] = true;
                    

            $sum = get_log()['posts'];
            $sum -= 1;

            update_log('posts',$sum);
        }
    }



    # File already changed to draft or deleted
    # Keep or remove some GET values
    $plink = '';


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

        # build get values
        # 'editfile' 'file' 'link'
        # We don't want to resend these values, so we remove them from $plink
        $param_not = ['editfile','file','link'/*,'max'*/];
        $x=0;
        foreach($_GET as $param=>$val) {

            if (!in_array($param,$param_not)) {
                $f = $x == 0 ? '?' : '&';
                $plink .= $f.$param.'='.$val;
                $x++;
            }
        }
    }
    

    header('Location:opensaved.php'.$plink);
    

    include 'execute.php'

?>