switch from http zu https

This commit is contained in:
oxidiert
2025-10-10 15:33:44 +02:00
parent 4089841336
commit ad09bce68c
3 changed files with 21 additions and 12 deletions

View File

@ -14,7 +14,7 @@ import java.io.FileOutputStream
object DatabaseDownloader { object DatabaseDownloader {
private const val DB_NAME = "questionnaire_database" private const val DB_NAME = "questionnaire_database"
private const val SERVER_DOWNLOAD_URL = "http://49.13.157.44/downloadFull.php" private const val SERVER_DOWNLOAD_URL = "https://daniel-ocks.de/qdb/downloadFull.php"
private val client = OkHttpClient() private val client = OkHttpClient()

View File

@ -1,4 +1,3 @@
// app/src/main/java/com/dano/test1/DatabaseUploader.kt
package com.dano.test1 package com.dano.test1
import android.content.Context import android.content.Context
@ -21,12 +20,11 @@ import kotlin.system.exitProcess
object DatabaseUploader { object DatabaseUploader {
private const val DB_NAME = "questionnaire_database" private const val DB_NAME = "questionnaire_database"
private const val SERVER_DELTA_URL = "http://49.13.157.44/uploadDeltaTest5.php" private const val SERVER_DELTA_URL = "https://daniel-ocks.de/qdb/uploadDeltaTest5.php"
private const val SERVER_CHECK_URL = "http://49.13.157.44/checkDatabaseExists.php" private const val SERVER_CHECK_URL = "https://daniel-ocks.de/qdb/checkDatabaseExists.php"
private val client = OkHttpClient() private val client = OkHttpClient()
/** NEU: Login mit Username+Password, danach Upload wie gehabt */
fun uploadDatabaseWithLogin(context: Context, username: String, password: String) { fun uploadDatabaseWithLogin(context: Context, username: String, password: String) {
LoginManager.loginUserWithCredentials( LoginManager.loginUserWithCredentials(
context = context, context = context,
@ -49,6 +47,7 @@ object DatabaseUploader {
return@launch return@launch
} }
// WAL sauber schließen (falls aktiv)
try { try {
val db = SQLiteDatabase.openDatabase(dbFile.absolutePath, null, SQLiteDatabase.OPEN_READWRITE) val db = SQLiteDatabase.openDatabase(dbFile.absolutePath, null, SQLiteDatabase.OPEN_READWRITE)
db.rawQuery("PRAGMA wal_checkpoint(FULL);", null).use { /* noop */ } db.rawQuery("PRAGMA wal_checkpoint(FULL);", null).use { /* noop */ }
@ -82,11 +81,14 @@ object DatabaseUploader {
put("questionnaires", queryToJsonArray(db, "SELECT id FROM questionnaires")) put("questionnaires", queryToJsonArray(db, "SELECT id FROM questionnaires"))
put("questions", queryToJsonArray(db, "SELECT questionId, questionnaireId, question FROM questions")) put("questions", queryToJsonArray(db, "SELECT questionId, questionnaireId, question FROM questions"))
put("answers", queryToJsonArray(db, "SELECT clientCode, questionId, answerValue FROM answers")) put("answers", queryToJsonArray(db, "SELECT clientCode, questionId, answerValue FROM answers"))
put("completed_questionnaires", put(
queryToJsonArray(db, "SELECT clientCode, questionnaireId, timestamp, isDone, sumPoints FROM completed_questionnaires")) "completed_questionnaires",
queryToJsonArray(db, "SELECT clientCode, questionnaireId, timestamp, isDone, sumPoints FROM completed_questionnaires")
)
} }
db.close() db.close()
// JSON -> verschlüsselte Payload
val tmpJson = File(context.cacheDir, "payload.json").apply { writeText(data.toString()) } val tmpJson = File(context.cacheDir, "payload.json").apply { writeText(data.toString()) }
val tmpEnc = File(context.cacheDir, "payload.enc") val tmpEnc = File(context.cacheDir, "payload.enc")
try { try {
@ -98,11 +100,17 @@ object DatabaseUploader {
val body = MultipartBody.Builder() val body = MultipartBody.Builder()
.setType(MultipartBody.FORM) .setType(MultipartBody.FORM)
.addFormDataPart("token", token) .addFormDataPart("token", token) // bleibt für Kompatibilität enthalten
.addFormDataPart("file", "payload.enc", tmpEnc.asRequestBody("application/octet-stream".toMediaType())) .addFormDataPart("file", "payload.enc", tmpEnc.asRequestBody("application/octet-stream".toMediaType()))
.build() .build()
val request = Request.Builder().url("http://49.13.157.44/uploadDeltaTest5.php").post(body).build() // WICHTIG: Jetzt HTTPS + Konstanten-URL verwenden, plus Bearer-Header
val request = Request.Builder()
.url(SERVER_DELTA_URL)
.post(body)
.header("Authorization", "Bearer $token")
.build()
client.newCall(request).enqueue(object : Callback { client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) { override fun onFailure(call: Call, e: IOException) {
Log.e("UPLOAD", "Fehlgeschlagen: ${e.message}") Log.e("UPLOAD", "Fehlgeschlagen: ${e.message}")
@ -113,7 +121,7 @@ object DatabaseUploader {
if (response.isSuccessful) { if (response.isSuccessful) {
Log.d("UPLOAD", "OK: $respBody") Log.d("UPLOAD", "OK: $respBody")
// <<< alte Logik wieder aktivieren: lokale DB + Neben­dateien löschen // alte Logik: lokale DB + Neben­dateien löschen
try { try {
if (!file.delete()) Log.w("UPLOAD", "Lokale DB nicht gelöscht.") if (!file.delete()) Log.w("UPLOAD", "Lokale DB nicht gelöscht.")
File(file.parent, "${file.name}-journal").delete() File(file.parent, "${file.name}-journal").delete()
@ -122,11 +130,12 @@ object DatabaseUploader {
} catch (e: Exception) { } catch (e: Exception) {
Log.w("UPLOAD", "Fehler beim Löschen lokaler DB-Dateien", e) Log.w("UPLOAD", "Fehler beim Löschen lokaler DB-Dateien", e)
} }
// >>>
} else { } else {
Log.e("UPLOAD", "HTTP ${response.code}: $respBody") Log.e("UPLOAD", "HTTP ${response.code}: $respBody")
} }
tmpJson.delete(); tmpEnc.delete() tmpJson.delete(); tmpEnc.delete()
// unverändert beibehalten
try { exitProcess(0) } catch (_: Exception) {} try { exitProcess(0) } catch (_: Exception) {}
} }
}) })

View File

@ -13,7 +13,7 @@ import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject import org.json.JSONObject
object LoginManager { object LoginManager {
private const val SERVER_LOGIN_URL = "http://49.13.157.44/login.php" private const val SERVER_LOGIN_URL = "https://daniel-ocks.de/qdb/login.php"
private val client = OkHttpClient() private val client = OkHttpClient()
fun loginUserWithCredentials( fun loginUserWithCredentials(