proxitok/app/Helpers/Misc.php

117 lines
4.3 KiB
PHP
Raw Normal View History

<?php
2022-01-30 18:02:52 -05:00
namespace App\Helpers;
2022-01-13 10:51:45 -05:00
2022-01-30 18:02:52 -05:00
use App\Cache\JSONCache;
use App\Cache\RedisCache;
class Misc {
2022-01-30 18:02:52 -05:00
static public function getCursor(): int {
return isset($_GET['cursor']) && is_numeric($_GET['cursor']) ? (int) $_GET['cursor'] : 0;
}
2022-02-05 18:58:30 -05:00
static public function url(string $endpoint = '') {
return self::env('APP_URL', '') . $endpoint;
2022-01-30 18:02:52 -05:00
}
static public function env(string $key, $default_value) {
2022-02-15 07:55:36 -05:00
return $_ENV[$key] ?? $default_value;
}
2022-01-28 09:54:09 -05:00
/**
* Returns absolute path for view
*/
static public function getView(string $template): string {
2022-01-30 18:02:52 -05:00
return __DIR__ . "/../../views/{$template}.latte";
}
2022-01-28 09:54:09 -05:00
/**
* Setup of TikTok Api wrapper
* @return \TikScraper\Api|\TikScraper\Legacy
2022-01-28 09:54:09 -05:00
*/
static public function api() {
2022-02-13 16:21:08 -05:00
$options = [
2022-02-15 07:55:36 -05:00
'remote_signer' => self::env('SIGNER_URL', 'http://localhost:8080/signature'),
'use_test_endpoints' => self::env('USE_TEST_ENDPOINTS', false),
// Instance level proxy config
'proxy' => [
'host' => self::env('PROXY_HOST', null),
'port' => self::env('PROXY_PORT', null),
'user' => self::env('PROXY_USER', null),
'password' => self::env('PROXY_PASSWORD', null)
]
2022-02-13 16:21:08 -05:00
];
// User level proxy config, will overwrite instance config
2022-01-30 18:02:52 -05:00
foreach(Cookies::PROXY as $proxy_element) {
2022-01-13 10:51:45 -05:00
if (isset($_COOKIE[$proxy_element])) {
$options['proxy'][$proxy_element] = $_COOKIE[$proxy_element];
}
}
// Cache config
$cacheEngine = false;
if (isset($_ENV['API_CACHE'])) {
switch ($_ENV['API_CACHE']) {
2022-01-13 10:51:45 -05:00
case 'json':
$cacheEngine = new JSONCache();
break;
2022-01-14 13:26:07 -05:00
case 'redis':
if (!(isset($_ENV['REDIS_URL']) || isset($_ENV['REDIS_HOST'], $_ENV['REDIS_PORT']))) {
throw new \Exception('You need to set REDIS_URL or REDIS_HOST and REDIS_PORT to use Redis Cache!');
2022-01-14 13:26:07 -05:00
}
if (isset($_ENV['REDIS_URL'])) {
$url = parse_url($_ENV['REDIS_URL']);
$host = $url['host'];
$port = $url['port'];
$password = $url['pass'] ?? null;
} else {
$host = $_ENV['REDIS_HOST'];
$port = (int) $_ENV['REDIS_PORT'];
2022-02-16 09:20:35 -05:00
$password = isset($_ENV['REDIS_PASSWORD']) ? $_ENV['REDIS_PASSWORD'] : null;
}
2022-01-14 13:26:07 -05:00
$cacheEngine = new RedisCache($host, $port, $password);
break;
}
}
// Legacy mode
$legacy = self::env('FORCE_LEGACY', false); // Instance level
$_COOKIE['enable_legacy'] ?? $legacy = true; // User level
return $legacy === false ? new \TikScraper\Api($options, $cacheEngine) : new \TikScraper\Legacy($options, $cacheEngine);
}
2022-01-28 09:54:09 -05:00
/**
* Setup of Latte template engine
*/
static public function latte(): \Latte\Engine {
$latte = new \Latte\Engine;
2022-01-30 18:02:52 -05:00
$cache_path = self::env('LATTE_CACHE', __DIR__ . '/../../cache/latte');
$latte->setTempDirectory($cache_path);
// -- CUSTOM FUNCTIONS -- //
2022-02-05 18:58:30 -05:00
// Get URL with optional endpoint
$latte->addFunction('path', function (string $endpoint = ''): string {
return self::url($endpoint);
});
// Version being used
2022-02-05 18:58:30 -05:00
$latte->addFunction('version', function (): string {
return \Composer\InstalledVersions::getVersion('pablouser1/proxitok');
});
// https://stackoverflow.com/a/36365553
$latte->addFunction('number', function (float $x) {
if($x > 1000) {
$x_number_format = number_format($x);
$x_array = explode(',', $x_number_format);
$x_parts = array('K', 'M', 'B', 'T');
$x_count_parts = count($x_array) - 1;
$x_display = $x;
$x_display = $x_array[0] . ((int) $x_array[1][0] !== 0 ? '.' . $x_array[1][0] : '');
$x_display .= $x_parts[$x_count_parts - 1];
return $x_display;
}
return $x;
});
return $latte;
}
}