private function listContents($sub_path = '')
 {
     $lib = new eatStaticMediaLibrary(DATA_ROOT . '/images/', $sub_path . '/');
     $page = new adminPage('images.php');
     // handle image upload
     if (eatStatic::getValue('postback', 'post') == "1") {
         $image = $lib->upload('images', $sub_path, 'file', 'image');
         if ($image != '') {
             $page->context['message'] = $image . ' uploaded';
         } else {
             $page->context['message'] = 'file not uploaded';
         }
     }
     // create new folder
     if (eatStatic::getValue('postback', 'post') == "2") {
         if (eatStatic::getValue('folder', 'post')) {
             $folder = $lib->createSubFolder(eatStatic::getValue('folder', 'post'));
         }
     }
     $page->context['contents'] = $lib->getContents();
     $page->context['title'] = "Images";
     $page->context['sub_path'] = $sub_path;
     $page->render();
 }
 function __construct($path)
 {
     require_once EATSTATIC_ROOT . '/eatStaticCSRF.class.php';
     $csrf = new eatStaticCSRF();
     switch ($path[2]) {
         case "":
             $page = new adminPage('login_form.php');
             if (eatStatic::getValue('postback', 'post') == '1') {
                 $csrf->verifyRequest();
                 $email = eatStatic::getValue('email', 'post');
                 $password = eatStatic::getValue('password', 'post');
                 if ($this->validUser($email, $password)) {
                     $_SESSION['admin'] = 1;
                     $_SESSION['admin_user'] = $email;
                     eatStaticAdminController::redirect("");
                 } else {
                     $page->context['error_message'] = 'Invalid username or password';
                 }
             }
             $page->context['title'] = "Log in";
             $page->context['show_navbar'] = false;
             $page->context['body_class'] = 'login-page';
             $page->context['csrf'] = $csrf;
             $page->render();
             break;
     }
 }
 private function editRawPost($slug, $draft = false)
 {
     $post_folder = DATA_ROOT . '/posts/';
     if ($draft) {
         $post_folder = $post_folder . 'draft/';
     }
     //die($post_folder);
     $page = new adminPage('post_raw_edit.php');
     $post = new eatStaticBlogPost();
     if (file_exists($post_folder . $slug . '.txt')) {
         $post->data_file_path = $post_folder . $slug . '.txt';
     }
     if (file_exists($post_folder . $slug . '.md')) {
         $post->data_file_path = $post_folder . $slug . '.md';
     }
     if (file_exists($post->data_file_path)) {
         $page->context['title'] = "Edit Post";
         $post->hydrate();
     } else {
         $page->context['title'] = "New Post";
     }
     if (eatStatic::getValue('postback') == '1') {
         //die($slug);
         $post->raw_data = trim(eatStatic::getValue('raw_data', 'post'));
         $post->file_name = trim(eatStatic::getValue('file_name', 'post'));
         $post->original_file_name = trim(eatStatic::getValue('original_file_name', 'post'));
         //die($slug);
         // copy current file data to backups
         if ($slug != 'new') {
             copy($post->data_file_path, DATA_ROOT . '/posts/backup/' . $post->file_name . '.' . eatStatic::timestamp() . '.bak');
             if ($post->original_file_name != $post->file_name) {
                 //die($post->data_file_path);
                 // remove original
                 unlink($post->data_file_path);
                 $new_data_file_path = $post_folder . $post->file_name;
                 eatStatic::write_file($post->raw_data, $new_data_file_path);
             } else {
                 eatStatic::write_file($post->raw_data, $post->data_file_path);
             }
         } else {
             $post->data_file_path = $post_folder . $post->file_name;
             eatStatic::write_file($post->raw_data, $post->data_file_path);
             header('location:' . ADMIN_ROOT . 'posts/drafts/');
             die;
         }
     }
     $page->context['post'] = $post;
     //print_r($page);
     //die();
     $page->render();
 }