Core lib of filesite.io, a small PHP Framework. https://fsc.filesite.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.3 KiB

3 years ago
<?php
/**
* Controller
*/
Class Controller {
protected $layout = 'main';
function __construct() {
//set default layout
if (!empty(FSC::$app['config']['default_layout'])) {
$this->layout = FSC::$app['config']['default_layout'];
}
}
function __destruct() {
$this->logTimeCost();
}
//redirect url
3 years ago
protected function redirect($url, $code = 302) {
3 years ago
header("Location: {$url}", true, $code);
exit;
3 years ago
}
3 years ago
//render view
3 years ago
protected function render($viewName, $viewData = array(), $pageTitle = '') {
3 years ago
$layoutFile = __DIR__ . '/../views/layout/' . $this->layout . '.php';
if (!empty(FSC::$app['config']['theme'])) {
$layoutFile = __DIR__ . '/../themes/' . FSC::$app['config']['theme'] . '/views/layout/' . $this->layout . '.php';
}
//include layout and view
if (file_exists($layoutFile)) {
//show time cost
$end_time = microtime(true);
$page_time_cost = ceil( ($end_time - FSC::$app['start_time']) * 1000 ); //ms
ob_start();
include_once $layoutFile;
$htmlCode = ob_get_contents();
ob_end_clean();
//enable gzip
ob_start('ob_gzhandler');
echo $htmlCode;
ob_end_flush();
}else {
throw Exception("Layout file not exist: {$layoutFile}", 500);
}
3 years ago
}
3 years ago
//render json data
3 years ago
protected function renderJson($data) {
3 years ago
header("Content-Type: application/json; charset=utf-8");
echo json_encode($data);
exit;
3 years ago
}
3 years ago
//render m3u8 file
3 years ago
protected function renderM3u8($content) {
3 years ago
header("Content-Type: application/x-mpegURL; charset=utf-8");
echo $content;
exit;
3 years ago
}
3 years ago
//get params by key
3 years ago
protected function get($key = '', $defaultValue = '') {
3 years ago
if (empty($key)) {
return $_GET;
}
return !empty($_GET[$key]) ? $_GET[$key] : $defaultValue;
3 years ago
}
3 years ago
//post params by key
3 years ago
protected function post($key = '', $defaultValue = '') {
3 years ago
if (empty($key)) {
return $_POST;
}
return !empty($_POST[$key]) ? $_POST[$key] : $defaultValue;
3 years ago
}
3 years ago
//debug log
3 years ago
protected function logTimeCost() {
3 years ago
if (!empty(FSC::$app['config']['debug'])) {
$end_time = microtime(true);
$timeCost = ceil( ($end_time - FSC::$app['start_time']) * 1000 ); //ms
$thisUrl = FSC::$app['requestUrl'];
$logTime = date('Y-m-d H:i:s');
$logDir = __DIR__ . '/../runtime/logs/';
$logOk = @error_log("{$logTime}\t{$thisUrl}\ttime cost: {$timeCost} ms\n", 3, "{$logDir}debug.log");
if (!$logOk) { //try to mkdir
@mkdir($logDir, 0700, true);
@error_log("{$logTime}\t{$thisUrl}\ttime cost: {$timeCost} ms\n", 3, "{$logDir}debug.log");
}
}
3 years ago
}
3 years ago
//get user real ip
3 years ago
protected function getUserIp() {
3 years ago
$ip = false;
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
if (!empty($ip)) {
array_unshift($ips, $ip);
$ip = false;
}
if (!empty($ips)) {
for ($i = 0; $i < count($ips); $i++) {
if (!preg_match("/^(10│172\.16│192\.168)\./", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
}
return !empty($ip) ? $ip : $_SERVER['REMOTE_ADDR'];
3 years ago
}
3 years ago
//request url via curl
3 years ago
protected function request($url, $postFields = array(), $timeout = 10, $pc = false) {
3 years ago
$s = curl_init();
curl_setopt($s, CURLOPT_URL, $url);
curl_setopt($s, CURLOPT_TIMEOUT, $timeout);
curl_setopt($s, CURLOPT_RETURNTRANSFER, true);
if (!empty($postFields)) {
curl_setopt($s, CURLOPT_POST, true);
curl_setopt($s, CURLOPT_POSTFIELDS, $postFields);
}
//iphone client
$user_agent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1';
//mac os client
if ($pc) {
$user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36';
}
curl_setopt($s, CURLOPT_USERAGENT, $user_agent);
curl_setopt($s, CURLOPT_REFERER, '-');
$curlResult = curl_exec($s);
$curlStatus = curl_getinfo($s, CURLINFO_HTTP_CODE);
curl_close($s);
return array(
'status' => $curlStatus,
'result' => $curlResult,
);
3 years ago
}
3 years ago
//set cookie for message show
//type: info, warning, danger, success
3 years ago
protected function sendMsgToClient($msg, $type = 'info') {
3 years ago
$cookieKey = "alert_msg_{$type}";
$expires = time() + 15;
$path = '/';
//empty character replace to "%20"
$encoded = urlencode($msg);
$noempty = str_replace('+', '%20', $encoded);
$val = base64_encode( $noempty );
setcookie($cookieKey, $val, $expires, $path);
3 years ago
}
3 years ago
}