Files
wavelog/application/models/Debug_model.php
HB9HIL c1325591cc QSL Files Migration (#174)
* remove unnessesary file

* prepared debug view with logic

* typo

* add additional case

* moved Maintenance Page to Debug

* copy function

* comment

* html file for eqsl card images folder

* only copy files if they have a qso_id in db

* not_assigned folder

* xss_clean

* $CI to $this

* removed status field, no need for that

* class wide variabled to reduce redundancy

* adjusted view

* allow run migration agn

* missing case if user_id is also empty (deleted user/qso)

* just commented

* only allow jpg, png and gif (same as in upload)
2024-02-26 17:42:46 +01:00

157 lines
5.6 KiB
PHP

<?php
class Debug_model extends CI_Model
{
private $userdata_dir;
private $flag_file;
private $src_eqsl;
private $eqsl_dir;
private $src_qsl;
private $qsl_dir;
public function __construct()
{
$this->userdata_dir = $this->config->item('userdata');
$this->flag_file = '.migrated'; // we use this flag file to determine if the migration already run through
$this->src_eqsl = 'images/eqsl_card_images';
$this->eqsl_dir = 'eqsl_card'; // make sure this is the same as in Eqsl_images.php function get_imagePath()
$this->src_qsl = 'assets/qslcard';
$this->qsl_dir = 'qsl_card'; // make sure this is the same as in Qsl_model.php function get_imagePath()
}
function migrate_userdata()
{
$this->load->model('Logbook_model');
$allowed_file_extensions = ['jpg', 'jpeg', 'gif', 'png'];
// ***** EQSL ***** //
// Let's scan the whole folder and get necessary data for each file
foreach (scandir($this->src_eqsl) as $file) {
// Ignore files if they are not jpg, png or gif
$file_extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (!in_array($file_extension, $allowed_file_extensions)) continue;
if (!is_readable($this->src_eqsl . '/' . $file)) continue;
if ($file != '.' && $file != '..') {
// we need the qso_id from the database to get the necessary user_id
$qso_id = $this->get_qsoid_from_eqsl_filename($file) ?? '';
// check if the qso_id is empty, if yes we create a folder 'not assigned' instead of 'user_id'
if (!empty($qso_id)) {
// get the user_id for this qso_id
$get_user_id = $this->Logbook_model->get_user_id_from_qso($qso_id);
// it can happen that the user_id is empty even there is a qso_id (deleted qso or deleted user)
if(!empty($get_user_id)) {
$user_id = $get_user_id;
} else {
$user_id = 'not_assigned';
}
} else {
$user_id = 'not_assigned';
}
// make sure the target path exists
$target = $this->userdata_dir . '/' . $user_id . '/' . $this->eqsl_dir;
if (!file_exists(realpath(APPPATH . '../') . '/' . $target)) {
mkdir(realpath(APPPATH . '../') . '/' . $target, 0755, true);
}
// then copy the file
if (!copy($this->src_eqsl . '/' . $file, $target . '/' . $file)) {
return false; // Failed to copy file
}
}
}
// ***** QSL Cards ***** //
// Let's scan the whole folder and get necessary data for each file
foreach (scandir($this->src_qsl) as $file) {
// Ignore files if they are not jpg, png or gif
$file_extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (!in_array($file_extension, $allowed_file_extensions)) continue;
if (!is_readable($this->src_qsl . '/' . $file)) continue;
if ($file != '.' && $file != '..') {
// we need the qso_id from the database to get the necessary user_id
$qso_id = $this->get_qsoid_from_qsl_filename($file) ?? '';
// check if the qso_id is empty, if yes we create a folder 'not assigned' instead of 'user_id'
if (!empty($qso_id)) {
// get the user_id for this qso_id
$get_user_id = $this->Logbook_model->get_user_id_from_qso($qso_id);
// it can happen that the user_id is empty even there is a qso_id (deleted qso or deleted user)
if(!empty($get_user_id)) {
$user_id = $get_user_id;
} else {
$user_id = 'not_assigned';
}
} else {
$user_id = 'not_assigned';
}
// make sure the target path exists
$target = $this->userdata_dir . '/' . $user_id . '/' . $this->qsl_dir;
if (!file_exists(realpath(APPPATH . '../') . '/' . $target)) {
mkdir(realpath(APPPATH . '../') . '/' . $target, 0755, true);
}
// then copy the file
if (!copy($this->src_qsl . '/' . $file, $target . '/' . $file)) {
return false; // Failed to copy file
}
}
}
// here we create the 'migrated' flag
if (!file_exists(realpath(APPPATH . '../') . '/' . $this->userdata_dir . '/' . $this->flag_file)) {
touch(realpath(APPPATH . '../') . '/' . $this->userdata_dir . '/' . $this->flag_file);
}
return true;
}
function check_migrated_flag()
{
if (!file_exists(realpath(APPPATH . '../') . '/' . $this->userdata_dir . '/' . $this->flag_file)) {
return false;
} else {
return true;
}
}
function get_qsoid_from_eqsl_filename($filename)
{
$sql = "SELECT qso_id FROM eQSL_images WHERE image_file = ?";
$result = $this->db->query($sql, $filename);
$row = $result->row();
return $row->qso_id;
}
function get_qsoid_from_qsl_filename($filename)
{
$sql = "SELECT qsoid FROM qsl_images WHERE filename = ?";
$result = $this->db->query($sql, $filename);
$row = $result->row();
return $row->qsoid;
}
}