From 591bd15098743b722f2dd684daff8e288036e376 Mon Sep 17 00:00:00 2001 From: Andreas <6977712+AndreasK79@users.noreply.github.com> Date: Wed, 3 May 2023 09:38:28 +0200 Subject: [PATCH] [Notes backup] Added support for backing up notes with an API key --- application/controllers/Backup.php | 21 ++++++++++++++------- application/models/Note.php | 15 +++++++++++++-- application/views/backup/notes_view.php | 4 ++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/application/controllers/Backup.php b/application/controllers/Backup.php index ba6f928a..2a34eacf 100644 --- a/application/controllers/Backup.php +++ b/application/controllers/Backup.php @@ -38,11 +38,11 @@ class Backup extends CI_Controller { if ( ! write_file($data['filename'], $this->load->view('backup/exportall', $data, true))) { - $data['status'] = false; + $data['status'] = false; } else { - $data['status'] = true; + $data['status'] = true; } $data['page_title'] = "ADIF - Backup"; @@ -55,19 +55,26 @@ class Backup extends CI_Controller { } /* Export the notes to XML */ - public function notes() { + public function notes($key = null) { + if ($key == null) { + $this->load->model('user_model'); + if(!$this->user_model->authorize(2)) { $this->session->set_flashdata('notice', 'You\'re not allowed to do that!'); redirect('dashboard'); } + } + $this->load->helper('file'); $this->load->model('note'); - $data['list_note'] = $this->note->list_all(); + $data['list_note'] = $this->note->list_all($key); - if ( ! write_file('backup/notes.xml', $this->load->view('backup/notes', $data, true))) + $data['filename'] = 'backup/notes'. date('_Y_m_d_H_i_s') .'.xml'; + + if ( ! write_file($data['filename'], $this->load->view('backup/notes', $data, true))) { - $data['status'] = false; + $data['status'] = false; } else { - $data['status'] = true; + $data['status'] = true; } $data['page_title'] = "Notes - Backup"; diff --git a/application/models/Note.php b/application/models/Note.php index 8c03ccaa..8f4a05fe 100644 --- a/application/models/Note.php +++ b/application/models/Note.php @@ -2,8 +2,19 @@ class Note extends CI_Model { - function list_all() { - $this->db->where('user_id', $this->session->userdata('user_id')); + function list_all($api_key = null) { + if ($api_key == null) { + $user_id = $this->session->userdata('user_id'); + } else { + $CI =& get_instance(); + $CI->load->model('api_model'); + if (strpos($this->api_model->access($api_key), 'r') !== false) { + $this->api_model->update_last_used($api_key); + $user_id = $this->api_model->key_userid($api_key); + } + } + + $this->db->where('user_id', $user_id); return $this->db->get('notes'); } diff --git a/application/views/backup/notes_view.php b/application/views/backup/notes_view.php index e9698c20..d2320fd0 100644 --- a/application/views/backup/notes_view.php +++ b/application/views/backup/notes_view.php @@ -3,8 +3,8 @@ - -

The backup of your notes completed successfully. The output can be found at: backup/notes.xml

+ +

The backup of your notes completed successfully. The output can be found at:

You could automate this process by making it a cronjob.