From c64e8871890d839e230cdf500436ca30b53e0aa7 Mon Sep 17 00:00:00 2001 From: tuz Date: Fri, 17 Oct 2025 00:22:01 -0500 Subject: [PATCH] update --- README.md | 45 +++++++++++++++++ docs/composer.md | 15 ++++++ docs/laravel.md | 97 ++++++++++++++++++++++++++++++++++++ docs/xmpp.md | 15 ++++++ lang/console.md | 6 +++ lang/flow.md | 52 ++++++++++++++++++++ lang/io.md | 18 +++++++ lang/superglobals.php | 13 +++++ lang/var.php | 14 ++++++ src/Markdown.php | 42 ++++++++++++++++ src/Vault.php | 112 ++++++++++++++++++++++++++++++++++++++++++ src/VaultApi.php | 53 ++++++++++++++++++++ 12 files changed, 482 insertions(+) create mode 100644 README.md create mode 100644 docs/composer.md create mode 100644 docs/laravel.md create mode 100644 docs/xmpp.md create mode 100644 lang/console.md create mode 100644 lang/flow.md create mode 100644 lang/io.md create mode 100644 lang/superglobals.php create mode 100644 lang/var.php create mode 100644 src/Markdown.php create mode 100644 src/Vault.php create mode 100644 src/VaultApi.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..ec926cb --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# PHP + +## Install + +```sh +sudo apt update +sudo apt upgrade + + +sudo apt install php php-fpm php-curl php8.2-cli php8.2-curl php8.2-mbstring php8.2-xml php8.2-zip php8.2-gd php8.2-pgsql php-xml + +sudo apt-get install php-xml + +systemctl status php8.2-fpm +systemctl restart php8.2-fpm + + +/run/php/php8.2-fpm.sock + +sudo apt install php-sqlite3 +sudo systemctl restart php8.2-fpm +sudo systemctl start php8.2-fpm +sudo systemctl enable php8.2-fpm +``` + +## Config + +```sh +sudo ln -s /run/php/php8.4-fpm.sock /run/php/php-fpm.sock +sudo ln -s /run/php/php8.2-fpm.sock /run/php/php-fpm.sock + + +/run/php/php-fpm.sock + +php_fastcgi unix//run/php/php8.2-fpm.sock +php_fastcgi unix//run/php/php-fpm.sock +``` + +```sh +sqlite +DB_CONNECTION=sqlite +DB_DATABASE=database/database.sqlite + +SESSION_DRIVER=file +``` diff --git a/docs/composer.md b/docs/composer.md new file mode 100644 index 0000000..cf16858 --- /dev/null +++ b/docs/composer.md @@ -0,0 +1,15 @@ +# Composer + +```sh +sudo apt update +sudo apt install composer + +Composer command + +composer self-update +composer update + +composer dump-autoload +``` + + diff --git a/docs/laravel.md b/docs/laravel.md new file mode 100644 index 0000000..cd4a85d --- /dev/null +++ b/docs/laravel.md @@ -0,0 +1,97 @@ +# Laravel + + +## Install +```sh +composer create-project laravel/laravel [project_name] +cd [project_name] +php artisan config:clear +php artisan make:controller PageController +``` + +```sh +composer create-project laravel/laravel local +cd local +php artisan make:controller +php artisan PageController +``` + +### After deployment +```sh +php install --optimize-autoloader --no-dev +php artisan migrate --force +php artisan config:clear +php artisan route:cache +php artisan view:cache +php artisan event:cache +php artisan optimize +``` + +Faster in production + +php artisan view:cache + +## Commands + +```sh +php artisan server +``` + +## Config (Optional) + +#### Markdown + +```sh +composer require league/commonmark +composer require spatie/laravel-markdown +``` + +## Code + +### How + +#### Rutine + + +template +```sh +resources: +js +css +mk +img + +resources/views/: +blade = html + php +``` + +routes +```sh +routes/web.php: +url page +``` + +routes controls logic +```sh +app/Http/Controllers/PageController.php +``` + + +app/Models/ +database + +.env = configuration + + + +raw + +routes/web.php +app/Http/Controllers/PageController.php + +resources/views/layouts/example.blade.php +resources/views/example.blade.php + + + + diff --git a/docs/xmpp.md b/docs/xmpp.md new file mode 100644 index 0000000..78f4450 --- /dev/null +++ b/docs/xmpp.md @@ -0,0 +1,15 @@ +# XMPP + + +## Config windows + +```sh +xampp +https://xmpp.org/ + +C:\xampp\htdocs +C:\xampp\mysql\bin +http://localhost/php/index.html + +http://localhost/phpmyadmin/ +``` diff --git a/lang/console.md b/lang/console.md new file mode 100644 index 0000000..ff232e3 --- /dev/null +++ b/lang/console.md @@ -0,0 +1,6 @@ +# Console +```php +echo "Print in browser"; + +sprintf("%s \n", "string"); +``` \ No newline at end of file diff --git a/lang/flow.md b/lang/flow.md new file mode 100644 index 0000000..b84a52a --- /dev/null +++ b/lang/flow.md @@ -0,0 +1,52 @@ +# Flow + +```php +$bool = true; + +if ($bool) +{ + +} +elseif ($bool) +{ + +} +else +{ + +} + + +$ret = match($bool) +{ + true => 1, + false => 0, + default => 0 +}; + +$ret = match(true) +{ + $bool && true => 1, + false => 0, + default => 0 +}; + + +while (true) +{ + break; +} + + +for ($i = 0; i < 10; $i += 1) +{ + +} + + +$array = []; +foreach ($arry as $value) +{ + +} +``` \ No newline at end of file diff --git a/lang/io.md b/lang/io.md new file mode 100644 index 0000000..aa47db6 --- /dev/null +++ b/lang/io.md @@ -0,0 +1,18 @@ + w = write +$file_open = fopen("file_open.txt", "w"); + +fwrite($file_open, "Write"); + +fclose($file_open); diff --git a/lang/superglobals.php b/lang/superglobals.php new file mode 100644 index 0000000..728aa39 --- /dev/null +++ b/lang/superglobals.php @@ -0,0 +1,13 @@ + /index.php + +$_SERVER['QUERY_STRING']; +// ret ==> /index.php?id=1 + +$_SERVER['REQUEST_URI']; +// ret ==> id=1 diff --git a/lang/var.php b/lang/var.php new file mode 100644 index 0000000..d2256b7 --- /dev/null +++ b/lang/var.php @@ -0,0 +1,14 @@ + diff --git a/src/Markdown.php b/src/Markdown.php new file mode 100644 index 0000000..4fbf7d7 --- /dev/null +++ b/src/Markdown.php @@ -0,0 +1,42 @@ +style_name = $style_name; + + echo ''; + } + + public function h1(string $str_h1): string { + return sprintf('

%s

', htmlspecialchars($str_h1)); + } + + public function h2(string $str_h2): string { + return sprintf('

%s

', htmlspecialchars($str_h2)); + } + + public function h3(string $str_h3): string { + return sprintf('

%s

', htmlspecialchars($str_h3)); + } + + public function h4(string $str_h4): string { + return sprintf('

%s

', htmlspecialchars($str_h4)); + } + + + public function code(string $str_code): string { + return sprintf('
%s
', htmlspecialchars($str_code)); + } + + public function link(string $str_title, string $str_url): string { + return sprintf(' + %s + ', htmlspecialchars($str_url), htmlspecialchars($str_title)); + } +} +?> diff --git a/src/Vault.php b/src/Vault.php new file mode 100644 index 0000000..9d5f4be --- /dev/null +++ b/src/Vault.php @@ -0,0 +1,112 @@ +role_id = getenv('VAULT_ROLE_ID') ?: 'ERR'; + $this->secret_id = getenv('VAULT_SECRET_ID') ?: 'ERR'; + $this->vault_addr = $vault_addr; + + + if (!$this->role_id || !$this->secret_id) { + throw new Exception("VAULT_ROLE_ID or VAULT_SECRET_ID environment variable not set."); + } + + $this->token = $this->login(); + } + + public function login(): string { + $login_data = json_encode([ + 'role_id' => $this->role_id, + 'secret_id' => $this->secret_id, + ]); + + + + $curl = curl_init("{$this->vault_addr}/v1/auth/approle/login"); + curl_setopt_array($curl, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $login_data, + CURLOPT_HTTPHEADER => ['Content-Type: application/json'], + + + CURLOPT_SSL_VERIFYPEER => false, // false Disable test only => true for production + CURLOPT_VERBOSE => true + + ]); + + + $response = curl_exec($curl); + if ($response === false) { + $capture_error = curl_error($curl); + throw new Exception(sprintf("[[Curl error]: $capture_error]")); + } + curl_close($curl); + + $json = json_decode($response, true); + if (isset($json['auth']['client_token'])) { + return $json['auth']['client_token']; + } + + throw new Exception("[[Vault login failed: $response]"); + } + + public function token_is_valid():bool { + + + + $curl = curl_init("{$this->vault_addr}/v1/auth/token/lookup-self"); + curl_setopt_array($curl, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => ['Content-Type: application/json'], + CURLOPT_SSL_VERIFYPEER => false, // false Disable test only => true for production + + ]); + } + + public function read_secret(string $read_path): array { + // $url = "{$this->vault_addr}/v1/{$read_path}"; + + // $url = "$this->vault_addr/v1/$read_path"; + // $url = "https://vault.local/v1/secret/data/debug-role/config"; + $url = "https://vault.local/v1/secret/data/approle/config"; + + $curl = curl_init(); + curl_setopt_array($curl, [ + CURLOPT_URL => $url, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => ["X-Vault-Token: {$this->token}"], + CURLOPT_SSL_VERIFYPEER => false + ]); + + $response = curl_exec($curl); + if ($response === false) { + throw new Exception("Curl error: " . curl_error($curl)); + } + curl_close($curl); + + + $json = json_decode($response, true); + + // echo json_decode($response, JSON_PRETTY_PRINT); + // echo print_r($response); + if (isset($json['data']['data'])) { + return $json['data']['data']; + } + + throw new Exception("Failed to read secret at $read_path: $response"); + } +} diff --git a/src/VaultApi.php b/src/VaultApi.php new file mode 100644 index 0000000..aab0cb1 --- /dev/null +++ b/src/VaultApi.php @@ -0,0 +1,53 @@ +setAuthenticationStrategy(new TokenAuthentication("your-vault-token")); // set your token here + + // Get secret + $secret = $vault->read("secret/data/myapp/config"); + $data = $secret->getData(); + + // Use the secret values + echo "API Key: " . $data['data']['api_key']; + + + + // setting up independent http client - example with guzzle http client + $httpClient = new Client(['verify' => false]); + + // setting up desired vault strategy + $authProvider = new Token('dummyToken'); + + // Initialize Vault client + $client = new VaultClient( + $httpClient, + $authProvider, + 'http://127.0.0.1:8200' + ); + + // List all keys from Transit Secret engine + $api = new Transit($client); + var_dump($api->listKeys()); + } +}