79 lines
1.3 KiB
PHP
79 lines
1.3 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace router\controller;
|
||
|
|
||
|
|
||
|
class js{
|
||
|
|
||
|
|
||
|
private $page;
|
||
|
|
||
|
/* PRE-CALL
|
||
|
*
|
||
|
* @url<String> Calling URI
|
||
|
*
|
||
|
*/
|
||
|
public function __construct($url){
|
||
|
$this->page = isset($url['page']) ? $url['page'] : '';
|
||
|
$this->page = preg_match('/^@[a-z]+$/', $this->page) ? substr($this->page, 1) : null;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* Generate _SERVER.js
|
||
|
*
|
||
|
*/
|
||
|
public function server(){
|
||
|
header('Content-Type: text/javascript; charset=utf-8');
|
||
|
|
||
|
echo "window._SERVER = ".json_encode([
|
||
|
|
||
|
'session' => [
|
||
|
'name' => $_SESSION['NAME'],
|
||
|
'connected' => isset($_SESSION['USER']) ? count($_SESSION['USER']) > 0 : false
|
||
|
]
|
||
|
|
||
|
])."\n";
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
/* Manage bundle hash
|
||
|
*
|
||
|
*/
|
||
|
public function bundle(){
|
||
|
if( is_null($this->page) ){
|
||
|
http_response_code(404);
|
||
|
die();
|
||
|
}
|
||
|
|
||
|
/* (1) Extract /public_html/js/ all .js files */
|
||
|
$js_scripts = glob(__PUBLIC__.'/js/bundle/'.$this->page.'@*.js');
|
||
|
|
||
|
/* (2) If match pattern 'bundle@home@*.js' */
|
||
|
foreach($js_scripts as $fname){
|
||
|
|
||
|
$bname = basename($fname);
|
||
|
|
||
|
// if match -> load it and exit
|
||
|
header("Location: /js/bundle/$bname");
|
||
|
die();
|
||
|
|
||
|
}
|
||
|
|
||
|
/* (3) If nothing found */
|
||
|
http_response_code(404);
|
||
|
die();
|
||
|
|
||
|
}
|
||
|
|
||
|
/* POST-CALL
|
||
|
*
|
||
|
*/
|
||
|
public function __destruct(){
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
}
|