From 170f39e70736baff4b7c0ffe7d91c1bdf409532b Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 25 Dec 2024 12:32:15 -0500 Subject: make shim a container once again --- build/shim/Dockerfile | 17 +++ build/shim/shim.php | 298 ++++++++++++++++++++++++++++++++++++++++++++ docker-compose.override.yml | 32 +++++ shim/README.md | 9 -- shim/shim.php | 298 -------------------------------------------- site | 2 +- 6 files changed, 348 insertions(+), 308 deletions(-) create mode 100644 build/shim/Dockerfile create mode 100755 build/shim/shim.php create mode 100644 docker-compose.override.yml delete mode 100644 shim/README.md delete mode 100755 shim/shim.php diff --git a/build/shim/Dockerfile b/build/shim/Dockerfile new file mode 100644 index 0000000..986b931 --- /dev/null +++ b/build/shim/Dockerfile @@ -0,0 +1,17 @@ +FROM alpine:latest + +# install packages +RUN apk add --no-cache \ + php84 \ + php84-pdo \ + php84-pdo_sqlite \ + php84-pdo_pgsql \ + tini + +# copy scripts +COPY ./shim.php /usr/local/bin/shim.php +RUN chmod +x /usr/local/bin/shim.php + +# run +ENTRYPOINT ["/sbin/tini", "--"] +CMD ["/usr/local/bin/shim.php"] diff --git a/build/shim/shim.php b/build/shim/shim.php new file mode 100755 index 0000000..0c91513 --- /dev/null +++ b/build/shim/shim.php @@ -0,0 +1,298 @@ +#!/usr/bin/env php84 +setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + + return $conn; +} + +function connect_sqlite() { + $conn_str = sprintf("sqlite:/data/xssbook.db"); + $conn = new \PDO($conn_str); + $conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + + return $conn; +} + +$psql = connect_psql(); +$sqlite = connect_sqlite(); + +function error() { + $psql->rollBack(); + die(); +} + +function get_date( + $day, $month, $year +) { + if (checkdate($month, $day, $year)) { + $date = "{$year}-{$month}-{$day}"; + return $date; + } else { + return "1970-01-01"; + } +} + +function clear_all() { + echo "clearing database\n"; + extract($GLOBALS); + $psql->beginTransaction(); + $psql->exec( + 'DELETE FROM xssbook.user; + DELETE FROM xssbook.post; + DELETE FROM xssbook.comment; + DELETE FROM xssbook.like; + DELETE FROM xssbook.follow; + DELETE FROM xssbook.user_media;' + ); +} + +function migrate_users() { + echo "migrating users\n"; + extract($GLOBALS); + + // load queries + $query = $sqlite->prepare( + 'SELECT user_id, firstname, lastname, email, password, gender, date, day, month, year FROM users;' + ); + $submit = $psql->prepare( + 'INSERT INTO xssbook.user + (id, username, password, first_name, last_name, email, gender, birth_date, created) + VALUES + (?, ?, ?, ?, ?, ?, ?, ?, to_timestamp(?));' + ); + + // load users + $query->execute(); + $rows = $query->fetchAll(); + foreach ($rows as $user) { + // submit each user + $date = get_date( + $user['day'], $user['month'], $user['year'] + ); + $joined = $user['date'] / 1000; + $submit->execute(array( + $user['user_id'], + $user['email'], + $user['password'], + $user['firstname'], + $user['lastname'], + $user['email'], + $user['gender'], + $date, + $joined + )); + } +} + +function migrate_posts() { + echo "migrating posts\n"; + extract($GLOBALS); + + // load queries + $query = $sqlite->prepare( + 'SELECT post_id, user_id, content, date FROM posts;' + ); + $submit = $psql->prepare( + 'INSERT INTO xssbook.post + (id, user_id, content, created) + VALUES + (?, ?, ?, to_timestamp(?));' + ); + + // load posts + $query->execute(); + $rows = $query->fetchall(); + foreach ($rows as $post) { + $created = $post['date'] / 1000; + $submit->execute(array( + $post['post_id'], + $post['user_id'], + $post['content'], + $created + )); + } +} + +function migrate_comments() { + echo "migrating comments\n"; + extract($GLOBALS); + + // load queries + $query = $sqlite->prepare( + 'SELECT comment_id, user_id, post_id, content, date FROM comments;' + ); + $submit = $psql->prepare( + 'INSERT INTO xssbook.comment + (id, user_id, post_id, content, created) + VALUES + (?, ?, ?, ?, to_timestamp(?));' + ); + + // load comments + $query->execute(); + $rows = $query->fetchall(); + foreach ($rows as $comment) { + $created = $comment['date'] / 1000; + $submit->execute(array( + $comment['comment_id'], + $comment['user_id'], + $comment['post_id'], + $comment['content'], + $created + )); + } +} + +function migrate_likes() { + echo "migrating likes\n"; + extract($GLOBALS); + + // load queries + $query = $sqlite->prepare( + 'SELECT user_id, post_id FROM likes;' + ); + $submit = $psql->prepare( + 'INSERT INTO xssbook.like + (user_id, post_id) + VALUES + (?, ?);' + ); + + // load likes + $query->execute(); + $rows = $query->fetchall(); + foreach ($rows as $like) { + $submit->execute(array( + $like['user_id'], + $like['post_id'] + )); + } +} + +function migrate_follow() { + echo "migrating follow\n"; + extract($GLOBALS); + + // load queries + $query = $sqlite->prepare( + 'SELECT follower_id, followee_id FROM friends;' + ); + $submit = $psql->prepare( + 'INSERT INTO xssbook.follow + (follower_id, followee_id) + VALUES + (?, ?);' + ); + + // load follows + $query->execute(); + $rows = $query->fetchall(); + foreach ($rows as $follow) { + $submit->execute(array( + $follow['follower_id'], + $follow['followee_id'] + )); + } +} + +function migrate_user_media($type) { + echo "migrating user media ($type)\n"; + extract($GLOBALS); + + // load queries + $submit = $psql->prepare( + 'INSERT INTO xssbook.user_media + (user_id, content, mime, type) + VALUES + (?, decode(?, \'base64\'), ?, ?);' + ); + + // get dir + $dir = "/data/custom/{$type}"; + if (!is_dir($dir)) { + return; + } + + // load user media + if ($handle = opendir($dir)) { + while (FALSE !== ($file = readdir($handle))) { + if ('.' === $file) continue; + if ('..' === $file) continue; + $n = strpos($file, ".png"); + $uid = substr($file, 0, $n); + $path = "{$dir}/{$file}"; + $data = base64_encode(file_get_contents($path)); + + $submit->execute(array( + $uid, + $data, + 'image/png', + $type + )); + } + closedir($handle); + } +} + +function migrate_seq() { + echo "migrating sequences\n"; + extract($GLOBALS); + + $tables = array('user', 'post', 'like', 'comment', 'follow', 'user_media'); + + foreach ($tables as $table) { + $sql = "SELECT setval('sys.{$table}_id_seq', (SELECT MAX(id) FROM xssbook.{$table}), true);"; + $psql->exec($sql); + } +} + +try { + wait_until_ready(); + clear_all(); + migrate_users(); + migrate_posts(); + migrate_comments(); + migrate_likes(); + migrate_follow(); + migrate_user_media('avatar'); + migrate_user_media('banner'); + migrate_seq(); +} catch (Exception $ex) { + echo "$ex\n"; + $psql->rollBack(); + die(); +} + +$psql->commit(); diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 0000000..0a46e5b --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,32 @@ +### XSSBook --- Post posts postfully. +### Copyright © 2024 Freya Murphy +### +### This file is part of XSSBook. +### +### CRIMSON is free software; you can redistribute it and/or modify it +### under the terms of the GNU General Public License as published by +### the Free Software Foundation; either version 3 of the License, or (at +### your option) any later version. +### +### CRIMSON is distributed in the hope that it will be useful, but +### WITHOUT ANY WARRANTY; without even the implied warranty of +### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +### GNU General Public License for more details. +### +### You should have received a copy of the GNU General Public License +### along with CRIMSON. If not, see . + +services: + # XSSBook service extensions + # - Adds websocket service + + shim: + build: ${PROJECT_ROOT}/build/shim + restart: no + env_file: DOCKER_ENV_FILES + volumes: + - ${PROJECT_ROOT} + - ${PROJECT_DATA}/crimson:/var/run/crimson + depends_on: + postgres: + condition: service_healthy diff --git a/shim/README.md b/shim/README.md deleted file mode 100644 index 80dfb3a..0000000 --- a/shim/README.md +++ /dev/null @@ -1,9 +0,0 @@ -## shim - -TODO: update beacuse this is now invalid :( - -If you are runing a xssbook v1 setup, the database is fully incompatible with xssbook v2. Luckily there is a migration that exists to port over your data. XSSBook v1 has a single sqlite database file and a custom assets directory likly called `custom`. You will know you have the right directory if there are two sub directories called `avatar` and `banner`. Place the sqlite db file (called `xssbook.db`) and the `custom` directory in the `data/shim` folder of the xssbook v2 directory. If this doesnt exist please do a full setup of v2 first. Then run `docker compose up -d shim`, and you should be all set. - -> WARNING: This will delete ALL data in the database if you specify xssbook v1 files in the data path. Make sure yo only run this once and remove the files once completed. - -> NOTE: the migration will never run if the database files are not supplied. diff --git a/shim/shim.php b/shim/shim.php deleted file mode 100755 index eb84412..0000000 --- a/shim/shim.php +++ /dev/null @@ -1,298 +0,0 @@ -#!/usr/bin/env php -setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - - return $conn; -} - -function connect_sqlite() { - $conn_str = sprintf("sqlite:/data/xssbook.db"); - $conn = new \PDO($conn_str); - $conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - - return $conn; -} - -$psql = connect_psql(); -$sqlite = connect_sqlite(); - -function error() { - $psql->rollBack(); - die(); -} - -function get_date( - $day, $month, $year -) { - if (checkdate($month, $day, $year)) { - $date = "{$year}-{$month}-{$day}"; - return $date; - } else { - return "1970-01-01"; - } -} - -function clear_all() { - echo "clearing database\n"; - extract($GLOBALS); - $psql->beginTransaction(); - $psql->exec( - 'DELETE FROM xssbook.user; - DELETE FROM xssbook.post; - DELETE FROM xssbook.comment; - DELETE FROM xssbook.like; - DELETE FROM xssbook.follow; - DELETE FROM xssbook.user_media;' - ); -} - -function migrate_users() { - echo "migrating users\n"; - extract($GLOBALS); - - // load queries - $query = $sqlite->prepare( - 'SELECT user_id, firstname, lastname, email, password, gender, date, day, month, year FROM users;' - ); - $submit = $psql->prepare( - 'INSERT INTO xssbook.user - (id, username, password, first_name, last_name, email, gender, birth_date, created) - VALUES - (?, ?, ?, ?, ?, ?, ?, ?, to_timestamp(?));' - ); - - // load users - $query->execute(); - $rows = $query->fetchAll(); - foreach ($rows as $user) { - // submit each user - $date = get_date( - $user['day'], $user['month'], $user['year'] - ); - $joined = $user['date'] / 1000; - $submit->execute(array( - $user['user_id'], - $user['email'], - $user['password'], - $user['firstname'], - $user['lastname'], - $user['email'], - $user['gender'], - $date, - $joined - )); - } -} - -function migrate_posts() { - echo "migrating posts\n"; - extract($GLOBALS); - - // load queries - $query = $sqlite->prepare( - 'SELECT post_id, user_id, content, date FROM posts;' - ); - $submit = $psql->prepare( - 'INSERT INTO xssbook.post - (id, user_id, content, created) - VALUES - (?, ?, ?, to_timestamp(?));' - ); - - // load posts - $query->execute(); - $rows = $query->fetchall(); - foreach ($rows as $post) { - $created = $post['date'] / 1000; - $submit->execute(array( - $post['post_id'], - $post['user_id'], - $post['content'], - $created - )); - } -} - -function migrate_comments() { - echo "migrating comments\n"; - extract($GLOBALS); - - // load queries - $query = $sqlite->prepare( - 'SELECT comment_id, user_id, post_id, content, date FROM comments;' - ); - $submit = $psql->prepare( - 'INSERT INTO xssbook.comment - (id, user_id, post_id, content, created) - VALUES - (?, ?, ?, ?, to_timestamp(?));' - ); - - // load comments - $query->execute(); - $rows = $query->fetchall(); - foreach ($rows as $comment) { - $created = $comment['date'] / 1000; - $submit->execute(array( - $comment['comment_id'], - $comment['user_id'], - $comment['post_id'], - $comment['content'], - $created - )); - } -} - -function migrate_likes() { - echo "migrating likes\n"; - extract($GLOBALS); - - // load queries - $query = $sqlite->prepare( - 'SELECT user_id, post_id FROM likes;' - ); - $submit = $psql->prepare( - 'INSERT INTO xssbook.like - (user_id, post_id) - VALUES - (?, ?);' - ); - - // load likes - $query->execute(); - $rows = $query->fetchall(); - foreach ($rows as $like) { - $submit->execute(array( - $like['user_id'], - $like['post_id'] - )); - } -} - -function migrate_follow() { - echo "migrating follow\n"; - extract($GLOBALS); - - // load queries - $query = $sqlite->prepare( - 'SELECT follower_id, followee_id FROM friends;' - ); - $submit = $psql->prepare( - 'INSERT INTO xssbook.follow - (follower_id, followee_id) - VALUES - (?, ?);' - ); - - // load follows - $query->execute(); - $rows = $query->fetchall(); - foreach ($rows as $follow) { - $submit->execute(array( - $follow['follower_id'], - $follow['followee_id'] - )); - } -} - -function migrate_user_media($type) { - echo "migrating user media ($type)\n"; - extract($GLOBALS); - - // load queries - $submit = $psql->prepare( - 'INSERT INTO xssbook.user_media - (user_id, content, mime, type) - VALUES - (?, decode(?, \'base64\'), ?, ?);' - ); - - // get dir - $dir = "/data/custom/{$type}"; - if (!is_dir($dir)) { - return; - } - - // load user media - if ($handle = opendir($dir)) { - while (FALSE !== ($file = readdir($handle))) { - if ('.' === $file) continue; - if ('..' === $file) continue; - $n = strpos($file, ".png"); - $uid = substr($file, 0, $n); - $path = "{$dir}/{$file}"; - $data = base64_encode(file_get_contents($path)); - - $submit->execute(array( - $uid, - $data, - 'image/png', - $type - )); - } - closedir($handle); - } -} - -function migrate_seq() { - echo "migrating sequences\n"; - extract($GLOBALS); - - $tables = array('user', 'post', 'like', 'comment', 'follow', 'user_media'); - - foreach ($tables as $table) { - $sql = "SELECT setval('sys.{$table}_id_seq', (SELECT MAX(id) FROM xssbook.{$table}), true);"; - $psql->exec($sql); - } -} - -try { - wait_until_ready(); - clear_all(); - migrate_users(); - migrate_posts(); - migrate_comments(); - migrate_likes(); - migrate_follow(); - migrate_user_media('avatar'); - migrate_user_media('banner'); - migrate_seq(); -} catch (Exception $ex) { - echo "$ex\n"; - $psql->rollBack(); - die(); -} - -$psql->commit(); diff --git a/site b/site index 45b58da..d929d8d 160000 --- a/site +++ b/site @@ -1 +1 @@ -Subproject commit 45b58da72187a7b1ed4d75fc25bf5dd59a86a9c4 +Subproject commit d929d8de67edc8881828bddfc817acebaf5d6e80 -- cgit v1.2.3-freya