1- <?php if ( ! defined ('BASEPATH ' )) exit ('No direct script access allowed ' );
1+ <?php if (! defined ('BASEPATH ' )) exit ('No direct script access allowed ' );
22
3- class Debug extends CI_Controller {
3+ class Debug extends CI_Controller
4+ {
45 function __construct ()
56 {
67 parent ::__construct ();
78
89 $ this ->load ->model ('user_model ' );
9- if (!$ this ->user_model ->authorize (2 )) { $ this ->session ->set_flashdata ('notice ' , 'You \'re not allowed to do that! ' ); redirect ('dashboard ' ); }
10+ if (!$ this ->user_model ->authorize (2 )) {
11+ $ this ->session ->set_flashdata ('notice ' , 'You \'re not allowed to do that! ' );
12+ redirect ('dashboard ' );
13+ }
14+
15+ $ this ->load ->library ('Permissions ' );
1016 }
1117
1218 /* User Facing Links to Backup URLs */
1319 public function index ()
1420 {
15- $ this ->load ->library ('Permissions ' );
1621 $ this ->load ->helper ('file ' );
1722
1823 $ this ->load ->model ('MigrationVersion ' );
24+ $ this ->load ->model ('Logbook_model ' );
25+ $ this ->load ->model ('Stations ' );
26+
27+ $ footerData = [];
28+ $ footerData ['scripts ' ] = ['assets/js/sections/debug.js ' ];
29+
30+ $ data ['stations ' ] = $ this ->Stations ->all ();
31+
32+ $ data ['qsos_with_no_station_id ' ] = $ this ->Logbook_model ->check_for_station_id ();
33+ if ($ data ['qsos_with_no_station_id ' ]) {
34+ $ data ['calls_wo_sid ' ] = $ this ->Logbook_model ->calls_without_station_id ();
35+ }
1936
2037 $ data ['migration_version ' ] = $ this ->MigrationVersion ->getMigrationVersion ();
2138
@@ -31,11 +48,130 @@ public function index()
3148 $ uploads_folder = $ this ->permissions ->is_really_writable ('uploads ' );
3249 $ data ['uploads_folder ' ] = $ uploads_folder ;
3350
51+ // Check if userdata config is enabled
52+ $ userdata_enabled = $ this ->config ->item ('userdata ' );
53+ $ data ['userdata_enabled ' ] = $ userdata_enabled ;
54+
55+ if (isset ($ userdata_enabled )) {
56+ // Test writing to userdata folder if option is enabled
57+ $ userdata_folder = $ this ->permissions ->is_really_writable ('userdata ' );
58+ $ data ['userdata_folder ' ] = $ userdata_folder ;
59+
60+ // run the status check and return the array to the view
61+ $ userdata_status = $ this ->check_userdata_status ($ userdata_folder );
62+ $ data ['userdata_status ' ] = $ userdata_status ;
63+ }
64+
3465 $ data ['page_title ' ] = "Debug " ;
3566
3667 $ this ->load ->view ('interface_assets/header ' , $ data );
37- $ this ->load ->view ('debug/main ' );
38- $ this ->load ->view ('interface_assets/footer ' );
68+ $ this ->load ->view ('debug/index ' );
69+ $ this ->load ->view ('interface_assets/footer ' , $ footerData );
70+ }
71+
72+ function check_userdata_status ($ userdata_folder )
73+ {
74+ $ this ->load ->model ('debug_model ' );
75+
76+ $ status = array ();
77+
78+ // Check if the folder is writable
79+ if ($ userdata_folder === true ) {
80+
81+ // Check if the qsl and eqsl folders are accessible and if there is any data the user could migrate
82+ $ qsl_dir = $ this ->permissions ->is_really_writable ('assets/qslcard ' );
83+ $ eqsl_dir = $ this ->permissions ->is_really_writable ('images/eqsl_card_images ' );
84+
85+ $ flag_file = $ this ->debug_model ->check_migrated_flag ();
86+
87+ if ($ qsl_dir && $ eqsl_dir ) {
88+
89+ // Check for content of the qsl card folder other than *.html files
90+ $ qsl_files = glob ('assets/qslcard/* ' );
91+ $ qsl_files_filtered = array_filter ($ qsl_files , function ($ file ) {
92+ return !is_dir ($ file ) && pathinfo ($ file , PATHINFO_EXTENSION ) !== 'html ' ;
93+ });
94+
95+ // Check for content of the eqsl card folder other than *.html files
96+ $ eqsl_files = glob ('images/eqsl_card_images/* ' );
97+ $ eqsl_files_filtered = array_filter ($ eqsl_files , function ($ file ) {
98+ return !is_dir ($ file ) && pathinfo ($ file , PATHINFO_EXTENSION ) !== 'html ' ;
99+ });
100+
101+ // Set the status info
102+ if (!empty ($ qsl_files_filtered ) || !empty ($ eqsl_files_filtered )) {
103+ if (!$ flag_file ) {
104+ $ status ['btn_class ' ] = '' ;
105+ $ status ['btn_text ' ] = 'Migrate data now ' ;
106+ } else {
107+ $ status ['btn_class ' ] = '' ;
108+ $ status ['btn_text ' ] = 'Migration already done. Run again? ' ;
109+ }
110+ } else {
111+ $ status ['btn_class ' ] = 'disabled ' ;
112+ $ status ['btn_text ' ] = 'No data to migrate ' ;
113+ }
114+ } else {
115+ $ status ['btn_class ' ] = 'disabled ' ;
116+ $ status ['btn_text ' ] = 'No migration possible ' ;
117+ }
118+ } else {
119+ // If the folder is not writable, we don't need to continue
120+ $ status ['btn_class ' ] = 'disabled ' ;
121+ $ status ['btn_text ' ] = 'No migration possible ' ;
122+ }
123+
124+ return $ status ;
39125 }
40126
127+ public function reassign ()
128+ {
129+ $ this ->load ->model ('Logbook_model ' );
130+ $ this ->load ->model ('Stations ' );
131+
132+ $ call = xss_clean (($ this ->input ->post ('call ' )));
133+ $ qsoids = xss_clean (($ this ->input ->post ('qsoids ' )));
134+ $ station_profile_id = xss_clean (($ this ->input ->post ('station_id ' )));
135+
136+ log_message ('debug ' , 'station_profile_id: ' , $ station_profile_id );
137+ // Check if target-station-id exists
138+ $ allowed = false ;
139+ $ status = false ;
140+ $ stations = $ this ->Stations ->all ();
141+ foreach ($ stations ->result () as $ station ) {
142+ if ($ station ->station_id == $ station_profile_id ) {
143+ $ allowed = true ;
144+ }
145+ }
146+ if ($ allowed ) {
147+ $ status = $ this ->Logbook_model ->update_station_ids ($ station_profile_id , $ call , $ qsoids );
148+ } else {
149+ $ status = false ;
150+ }
151+
152+ header ('Content-Type: application/json ' );
153+ echo json_encode (array ('status ' => $ status ));
154+ return ;
155+ }
156+
157+ public function migrate_userdata ()
158+ {
159+ // Check if users logged in
160+ $ this ->load ->model ('user_model ' );
161+ if ($ this ->user_model ->validate_session () == 0 ) {
162+ // user is not logged in
163+ redirect ('user/login ' );
164+ } else {
165+ $ this ->load ->model ('debug_model ' );
166+ $ migrate = $ this ->debug_model ->migrate_userdata ();
167+
168+ if ($ migrate == true ) {
169+ $ this ->session ->set_flashdata ('success ' , 'File Migration was successfull, but please check also manually. If everything seems right you can delete the folders "assets/qslcard" and "images/eqsl_card_images". ' );
170+ redirect ('debug ' );
171+ } else {
172+ $ this ->session ->set_flashdata ('error ' , 'File Migration failed. Please check the Error Log. ' );
173+ redirect ('debug ' );
174+ }
175+ }
176+ }
41177}
0 commit comments