From 4879fdf36dd91b0ebe42d52b8857d44838326046 Mon Sep 17 00:00:00 2001 From: netkas Date: Tue, 24 Sep 2024 14:20:49 -0400 Subject: [PATCH] Updated Project Source code (I don't remember what i did.) --- .github/workflows/ncc_workflow.yml | 162 ++++++++++++++++++ main | 24 +++ src/Socialbox/Abstracts/Method.php | 6 +- src/Socialbox/Classes/Configuration.php | 27 +++ src/Socialbox/Classes/Database.php | 4 +- src/Socialbox/Managers/SessionManager.php | 4 +- src/Socialbox/Program.php | 18 ++ src/Socialbox/Socialbox.php | 8 +- src/Socialbox/Socialbox/Socialbox.php | 8 + tests/Socialbox/Classes/ConfigurationTest.php | 38 ++++ 10 files changed, 289 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/ncc_workflow.yml create mode 100644 main create mode 100644 src/Socialbox/Classes/Configuration.php create mode 100644 src/Socialbox/Program.php create mode 100644 src/Socialbox/Socialbox/Socialbox.php create mode 100644 tests/Socialbox/Classes/ConfigurationTest.php diff --git a/.github/workflows/ncc_workflow.yml b/.github/workflows/ncc_workflow.yml new file mode 100644 index 0000000..5b1177e --- /dev/null +++ b/.github/workflows/ncc_workflow.yml @@ -0,0 +1,162 @@ +name: CI + +on: + push: + branches: + - '**' + release: + types: [created] + workflow_dispatch: + +jobs: + build: + runs-on: ubuntu-latest + container: + image: php:8.3 + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + apt update -yqq + apt install git libpq-dev libzip-dev zip make wget gnupg -yqq + + - name: Install phive + run: | + wget -O phive.phar https://phar.io/releases/phive.phar + wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc + gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79 + gpg --verify phive.phar.asc phive.phar + chmod +x phive.phar + mv phive.phar /usr/local/bin/phive + + - name: Install phab + run: | + phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C + + - name: Install latest version of NCC + run: | + git clone https://git.n64.cc/nosial/ncc.git + cd ncc + make redist + NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1) + if [ -z "$NCC_DIR" ]; then + echo "NCC build directory not found" + exit 1 + fi + php "$NCC_DIR/INSTALL" --auto + cd .. && rm -rf ncc + + - name: Build project + run: | + ncc build --config release --log-level debug + + - name: Upload build artifacts + uses: actions/upload-artifact@v4 + with: + name: Socialbox_build + path: build/release/net.nosial.socialbox.ncc + + check-phpunit: + runs-on: ubuntu-latest + outputs: + phpunit-exists: ${{ steps.check.outputs.phpunit-exists }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Check for phpunit.xml + id: check + run: | + if [ -f phpunit.xml ]; then + echo "phpunit-exists=true" >> $GITHUB_OUTPUT + else + echo "phpunit-exists=false" >> $GITHUB_OUTPUT + fi + + test: + needs: [build, check-phpunit] + runs-on: ubuntu-latest + container: + image: php:8.3 + if: needs.check-phpunit.outputs.phpunit-exists == 'true' + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Download build artifacts + uses: actions/download-artifact@v4 + with: + name: Socialbox_build + path: Socialbox_build # Adjust this to download the artifact directly under 'Socialbox_build' + + - name: Install dependencies + run: | + apt update -yqq + apt install git libpq-dev libzip-dev zip make wget gnupg -yqq + curl -sSLf -o /usr/local/bin/install-php-extensions https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions + chmod +x /usr/local/bin/install-php-extensions + install-php-extensions zip + + - name: Install phive + run: | + wget -O phive.phar https://phar.io/releases/phive.phar + wget -O phive.phar.asc https://phar.io/releases/phive.phar.asc + gpg --keyserver hkps://keys.openpgp.org --recv-keys 0x9D8A98B29B2D5D79 + gpg --verify phive.phar.asc phive.phar + chmod +x phive.phar + mv phive.phar /usr/local/bin/phive + + - name: Install phab + run: | + phive install phpab --global --trust-gpg-keys 0x2A8299CE842DD38C + + - name: Install latest version of NCC + run: | + git clone https://git.n64.cc/nosial/ncc.git + cd ncc + make redist + NCC_DIR=$(find build/ -type d -name "ncc_*" | head -n 1) + if [ -z "$NCC_DIR" ]; then + echo "NCC build directory not found" + exit 1 + fi + php "$NCC_DIR/INSTALL" --auto + cd .. && rm -rf ncc + + - name: Install NCC packages + run: | + ncc package install --package="Socialbox_build/net.nosial.socialbox.ncc" --build-source --reinstall -y --log-level debug + + - name: Run PHPUnit tests + run: | + wget https://phar.phpunit.de/phpunit-11.3.phar + php phpunit-11.3.phar --configuration phpunit.xml + + release: + needs: [build, test] + permissions: write-all + runs-on: ubuntu-latest + container: + image: php:8.3 + if: github.event_name == 'release' + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Download build artifacts + uses: actions/download-artifact@v4 + with: + name: Socialbox_build + path: Socialbox_build + + - name: Upload to GitHub Release + uses: softprops/action-gh-release@v1 + with: + files: | + Socialbox_build/net.nosial.socialbox.ncc + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/main b/main new file mode 100644 index 0000000..9b7b8c3 --- /dev/null +++ b/main @@ -0,0 +1,24 @@ +getSessionUuid())) - { - throw new StandardException(sprintf("The requested session %s was not found", $request->getSessionUuid()), StandardError::SESSION_NOT_FOUND); - } - + // NOTE: If the session UUID was provided, it has already been validated up until this point return SessionManager::getSession($request->getSessionUuid()); } catch(DatabaseOperationException $e) diff --git a/src/Socialbox/Classes/Configuration.php b/src/Socialbox/Classes/Configuration.php new file mode 100644 index 0000000..963dc22 --- /dev/null +++ b/src/Socialbox/Classes/Configuration.php @@ -0,0 +1,27 @@ +setDefault('database.host', '127.0.0.1'); + $config->setDefault('database.port', 3306); + $config->setDefault('database.username', 'root'); + $config->setDefault('database.password', null); + $config->setDefault('database.name', 'test'); + $config->save(); + + self::$configuration = $config->getConfiguration(); + } + + return self::$configuration; + } +} \ No newline at end of file diff --git a/src/Socialbox/Classes/Database.php b/src/Socialbox/Classes/Database.php index 04ff2f5..47fdcbc 100644 --- a/src/Socialbox/Classes/Database.php +++ b/src/Socialbox/Classes/Database.php @@ -13,7 +13,7 @@ class Database private static ?PDO $instance = null; /** - * @return mysqli + * @return PDO * @throws DatabaseOperationException */ public static function getConnection(): PDO @@ -22,7 +22,7 @@ class Database { try { - $dsn = 'mysql:host=172.27.0.1;dbname=socialbox;port=3306;charset=utf8mb4'; + $dsn = 'mysql:host=127.0.0.1;dbname=socialbox;port=3306;charset=utf8mb4'; self::$instance = new PDO($dsn, 'root', 'root'); // Set some common PDO attributes for better error handling diff --git a/src/Socialbox/Managers/SessionManager.php b/src/Socialbox/Managers/SessionManager.php index 3bf60c6..92ea03a 100644 --- a/src/Socialbox/Managers/SessionManager.php +++ b/src/Socialbox/Managers/SessionManager.php @@ -32,12 +32,12 @@ { if($publicKey === '') { - throw new InvalidArgumentException('The public key cannot be empty', StandardError::RPC_INVALID_ARGUMENTS); + throw new InvalidArgumentException('The public key cannot be empty', 400); } if(!Cryptography::validatePublicKey($publicKey)) { - throw new InvalidArgumentException('The given public key is invalid', StandardError::INVALID_PUBLIC_KEY); + throw new InvalidArgumentException('The given public key is invalid', 400); } $publicKey = Utilities::base64decode($publicKey); diff --git a/src/Socialbox/Program.php b/src/Socialbox/Program.php new file mode 100644 index 0000000..15dfa8e --- /dev/null +++ b/src/Socialbox/Program.php @@ -0,0 +1,18 @@ +produceError(StandardError::RPC_METHOD_NOT_FOUND, 'The requested method does not exist');; + $response = $rpcRequest->produceError(StandardError::RPC_METHOD_NOT_FOUND, 'The requested method does not exist'); if($response !== null) { $results[] = $response; diff --git a/src/Socialbox/Socialbox/Socialbox.php b/src/Socialbox/Socialbox/Socialbox.php new file mode 100644 index 0000000..f0ca775 --- /dev/null +++ b/src/Socialbox/Socialbox/Socialbox.php @@ -0,0 +1,8 @@ +assertIsArray($config, "Configuration should be an array."); + + //Assert that all the default configuration exists + $this->assertArrayHasKey('database.host', $config); + $this->assertEquals($config['database.host'], '127.0.0.1'); + + $this->assertArrayHasKey('database.port', $config); + $this->assertEquals($config['database.port'], 3306); + + $this->assertArrayHasKey('database.username', $config); + $this->assertEquals($config['database.username'], 'root'); + + $this->assertArrayHasKey('database.password', $config); + $this->assertNull($config['database.password'], 'null should be the value of database.password.'); + + $this->assertArrayHasKey('database.name', $config); + $this->assertEquals($config['database.name'], 'test'); + } +} \ No newline at end of file