update
This commit is contained in:
commit
c64e887189
|
|
@ -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
|
||||
```
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
# Composer
|
||||
|
||||
```sh
|
||||
sudo apt update
|
||||
sudo apt install composer
|
||||
|
||||
Composer command
|
||||
|
||||
composer self-update
|
||||
composer update
|
||||
|
||||
composer dump-autoload
|
||||
```
|
||||
|
||||
|
||||
|
|
@ -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
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -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/
|
||||
```
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
# Console
|
||||
```php
|
||||
echo "Print in browser";
|
||||
|
||||
sprintf("%s \n", "string");
|
||||
```
|
||||
|
|
@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
```
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
|
||||
// Write in the same content
|
||||
file_put_contents("file_append.txt", "Data_write", FILE_APPEND);
|
||||
|
||||
|
||||
$contents = file_get_contents("file_get_content.txt");
|
||||
|
||||
echo nl2br($contents);
|
||||
|
||||
|
||||
// File open ==> w = write
|
||||
$file_open = fopen("file_open.txt", "w");
|
||||
|
||||
fwrite($file_open, "Write");
|
||||
|
||||
fclose($file_open);
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
|
||||
$url = "/index.php?id=1";
|
||||
|
||||
$_SERVER['PHP_SELF'];
|
||||
// ret ==> /index.php
|
||||
|
||||
$_SERVER['QUERY_STRING'];
|
||||
// ret ==> /index.php?id=1
|
||||
|
||||
$_SERVER['REQUEST_URI'];
|
||||
// ret ==> id=1
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
$var = "String var";
|
||||
|
||||
enum Enumeration
|
||||
{
|
||||
case One;
|
||||
case Two;
|
||||
case Tree;
|
||||
}
|
||||
|
||||
$array = [];
|
||||
|
||||
?>
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
namespace api\web;
|
||||
|
||||
|
||||
class Markdown {
|
||||
private string $style_name;
|
||||
private string $style_raw;
|
||||
|
||||
public function __construct(string $style_name = 'none') {
|
||||
$this->style_name = $style_name;
|
||||
|
||||
echo '<link rel="stylesheet" href="/assets/terminal_vintage.css" />';
|
||||
}
|
||||
|
||||
public function h1(string $str_h1): string {
|
||||
return sprintf('<h1>%s</h1>', htmlspecialchars($str_h1));
|
||||
}
|
||||
|
||||
public function h2(string $str_h2): string {
|
||||
return sprintf('<h2>%s</h2>', htmlspecialchars($str_h2));
|
||||
}
|
||||
|
||||
public function h3(string $str_h3): string {
|
||||
return sprintf('<h3>%s</h3>', htmlspecialchars($str_h3));
|
||||
}
|
||||
|
||||
public function h4(string $str_h4): string {
|
||||
return sprintf('<h4>%s</h4>', htmlspecialchars($str_h4));
|
||||
}
|
||||
|
||||
|
||||
public function code(string $str_code): string {
|
||||
return sprintf('<pre><code>%s</code></pre>', htmlspecialchars($str_code));
|
||||
}
|
||||
|
||||
public function link(string $str_title, string $str_url): string {
|
||||
return sprintf('
|
||||
<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>
|
||||
', htmlspecialchars($str_url), htmlspecialchars($str_title));
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
<?php
|
||||
declare(strict_type=1);
|
||||
namespace std\api\key;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
require '/dev/shm/vault-env.php';
|
||||
|
||||
class Vault {
|
||||
private string $role_id;
|
||||
private string $secret_id;
|
||||
private string $vault_addr;
|
||||
|
||||
private string $token;
|
||||
|
||||
public function __construct(string $vault_addr) {
|
||||
$this->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");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
namespace std\api\key;
|
||||
|
||||
use Exception;
|
||||
|
||||
|
||||
|
||||
require_once dirname(__DIR__, 5) . '/vendor/autoload.php';
|
||||
|
||||
use VaultPHP\VaultClient;
|
||||
use VaultPHP\Authentication\Provider\Token;
|
||||
use VaultPHP\SecretEngines\Engines\Transit\Transit;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
|
||||
use Vault\Vault;
|
||||
use Vault\AuthenticationStrategies\TokenAuthentication;
|
||||
|
||||
|
||||
class VaultApi {
|
||||
public static function read_secret(string $read_path): array {
|
||||
$vault = new Vault("http://127.0.0.1:8200"); // your Vault address
|
||||
$vault->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());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue