<?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


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

                $check_file = simplexml_object($del,'l','post');


                $type = $check_file->postinfo->post->type;
                $creator = $check_file->postinfo->post->createdby;
                $filename = $check_file->postinfo->post->filename;
                $normalname = str_replace('nu.xml','.xml',$del);

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

                    $truename = simplexml_object($normalname,'l','post');
                    $filename = $truename->postinfo->post->filename;

                }

                $path = $check_file->postinfo->post->path;


                # If file belongs to the author or user is an administrator
                if ( ($_SESSION['userID'] == $creator) || (get_account($_SESSION['username'])['rights']) == 'administrator'  && $creator != '') {


                    $del = str_replace(array('nu.xml','.xml'),'',$del);
                    $files_to_del = ['autosaves/'.$del.'.xml',
                                     '../'.$path.$filename.'.php',
                                     'autosaves/'.$del.'nu.xml',
                                     'autosaves/content/'.$del.'.htm',
                                     'autosaves/content/'.$del.'nu.htm'
                                    ];

                    foreach ($files_to_del as $file)
                        if (is_file($file))
                            unlink($file);




                    $_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';

?>