Skip to content

Commit

Permalink
just next
Browse files Browse the repository at this point in the history
  • Loading branch information
energy-coresky committed May 24, 2021
1 parent f39cc34 commit b979968
Show file tree
Hide file tree
Showing 21 changed files with 443 additions and 258 deletions.
113 changes: 49 additions & 64 deletions heaven.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
//////////////////////////////////////////////////////////////////////////
class HEAVEN extends SKY
{
private $referer = [];

public $jump = false;
public $methods = ['POST', 'GET', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD', 'TRACE', 'CONNECT'];
public $method = false;
Expand All @@ -19,12 +17,13 @@ class HEAVEN extends SKY
public $admins = 1; # root only has admin access or list pids in array
public $lg = false; # no languages initialy
public $has_public = true; # web-site or CRM
public $adm_able = false;
public $page_p = 'p';
public $adm_able = false;
public $ajax = 0;
public $surl = [];

function __get($name) {
if (2 == strlen($name) && '_' == $name[0] && is_num($name[1])) {
if ('_' == $name[0] && 2 == strlen($name) && is_num($name[1])) {
$v = (int)$name[1];
if ('' === URI)
return $v ? '' : 'main';
Expand Down Expand Up @@ -58,9 +57,11 @@ private function extra_file($sn) {

function load() {
header('Content-Type: text/html; charset=' . ENC);
defined('DESIGN') or define('DESIGN', false);
define('DIR_V', DESIGN ? WWW . 'view' : 'view');/////////
$this->method = array_search($_SERVER['REQUEST_METHOD'], $this->methods);
if (false === $this->method)
throw new Err('Unknown method');
throw new Err('Unknown request method');

define('PATH', preg_replace("|[^/]*$|", '', $_SERVER['SCRIPT_NAME']));
define('URI', (string)substr($_SERVER['REQUEST_URI'], strlen(PATH))); # (string) required!
Expand All @@ -79,11 +80,12 @@ function load() {
define('PROTO', @$_SERVER['HTTPS'] ? 'https' : 'http');
define('DOMAIN', $this->sname[3]);

$hook = parent::load(); # database connection start from here
parent::load(); # database connection start from here

$this->ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) ? 2 : 0;
//2do: fetch
$this->is_front = true;
$this->origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : false;
$this->origin = $_SERVER['HTTP_ORIGIN'] ?? false;
$this->orientation = 0;
if (isset($_SERVER['HTTP_X_ORIENTATION'])) {
in_array($wo = (int)$_SERVER['HTTP_X_ORIENTATION'], [0, 1, 2]) or $wo = 0;
Expand All @@ -94,20 +96,20 @@ function load() {
$this->s_statp = preg_match("/^\d{4}$/", $s) ? $s . 'p' : '1000p';
}

$referer = @$_SERVER['HTTP_REFERER'];
$referer = $_SERVER['HTTP_REFERER'] ?? '';
$this->re = "~^https?://$re" . preg_quote(DOMAIN . PATH);
$this->lref = preg_match("$this->re(.*)$~", $referer, $m) ? $m[3] : false;
$this->eref = !$m ? $referer : false;
$ajax_adm = false;
// $ajax_adm = false;

if ('' !== URI) { # not main page
$this->surl = explode('/', explode('?', URI)[0]);
$hook->h_rewrite($cnt_s = count($this->surl));
common_c::rewrite_h($cnt_s = count($this->surl));
if (1 == $cnt_s && '' === $this->surl[0]) {
$this->surl = [];
if ('AJAX' == key($_GET) && $this->ajax) {
if ($this->ajax && 'AJAX' == key($_GET)) {
$this->ajax = 1; # j_ template
$ajax_adm = 'adm' == $_GET['AJAX'];
$this->ajaxv = $_GET['AJAX']; // 'adm'
array_shift($_GET);
}
}
Expand All @@ -116,63 +118,25 @@ function load() {
if ($this->debug)
$this->gpc = Debug::gpc();

global $user;
$user = new USER;
$hook->h_load();
if (12 == $this->error_no && !$this->ajax && '' !== URI) // 2do:check for api calls
jump();
if ($this->error_no && '_exception' != URI)
throw new Exception(11);

$adm = $this->admins && $user->allow($this->admins) ? Admin::access($ajax_adm) : false;
$type = in_array($this->_1, ['list', 'edit', 'new']);
$fn = ($this->style ? "$this->style/" : '') . ($this->is_mobile ? 'mobile' : 'desktop');
$tz = !$user->vid || '' === $user->v_tz ? "''" : (float)('' === $user->u_tz ? $user->v_tz : $user->u_tz);
//if (12 == $this->error_no && !$this->ajax && '' !== URI) // 2do:check for api calls
// jump();
//if ($this->error_no && '_exception' != URI)
// throw new Exception(11);

SKY::$vars = [
'k_tkd' => [$this->s_title, $this->s_keywords, $this->s_description],
'k_type' => $type = $type ? $this->_1 : ($this->_1 && 'p' != $this->_1 ? 'show' : 'list'),
'k_list' => 'list' == $type,
'k_static' => [[], ["~/$fn.js"], ["~/$fn.css"]], # default app meta_tags, js, css files
'k_js' => "sky.tz=$tz; sky.is_debug=$this->debug; var addr='" . LINK . "';",
'k_list' => 'list' == $this->_1 || in_array($this->page_p, [$this->_1, $this->_2, $this->_3]),

// 'k_js' => "sky.is_debug=$this->debug; var addr='" . LINK . "';",
];
if (1 == $this->extra)
is_file($fn = 'var/extra.txt') && in_array($this->fn_extra, array_map('trim', file($fn))) or $this->extra = 2;

return $adm;
}

function log($mode, $data) {
if (!in_array($this->s_test_mode, [$mode, 'all']))
return;
sqlf('update $_memory set dt=' . SKY::$dd('dt') . ', tmemo=substr(' . SKY::$dd->f_cc('%s', 'tmemo') . ',1,15000) where id=10', date(DATE_DT) . " $mode $data\n");
}

function qs($url) {
return preg_match("$this->re(.*)$~", $url, $m) ? $m[3] : false;
}

function flag($flag = 0, $val = null, $char = 'v') {
$storage =& SKY::$mem[$char][2]['flags'];
if ($val == null)
return $flag ? $storage & $flag : $storage;
SKY::$char(null, ['flags' => $val ? $flag | $storage : ~$flag & $storage]);
function adm() {
return $this->admins && $user->allow($this->admins) ? Admin::access($ajax_adm) : false;
}

function csrf($skip = []) {
if (INPUT_POST != $this->method || in_array($this->_0, $skip) || $this->error_no)
return false;
$csrf = 1;
if (isset($_POST['_csrf'])) {
$csrf = $_POST['_csrf'];
unset($_POST['_csrf']);
} elseif (isset($_SERVER['HTTP_X_CSRF_TOKEN'])) {
$csrf = $_SERVER['HTTP_X_CSRF_TOKEN'];
}
return $csrf;
}

function h_load() { # default processing
function h_load() { # default processing //////////////////
global $user;

if ($this->is_mobile = $this->sname[2]) {
Expand All @@ -184,6 +148,9 @@ function h_load() { # default processing
define('LG', !$this->sname[1] ? ($user->id && $user->u_lang ? $user->u_lang : $user->v_lg) : substr(SNAME, 0, 2));
if (LG != $user->v_lg && !$this->sname[2])
$user->v_lg = LG;
require 'main/lng/' . LG . '.php';
SKY::$reg['trans_late'] = $lgt ?? [];
SKY::$reg['trans_coll'] = [];
}
$link = PROTO . '://' . (DEFAULT_LG ? LG . '.' : '') . ($this->is_mobile ? 'm.' : '') . DOMAIN;
define('LINK', $link . PATH);
Expand All @@ -197,13 +164,30 @@ function h_load() { # default processing
}
}

function h_rewrite($cnt) {
function h_rewrite($cnt) {///////////////////////////
if (1 == $cnt && 'robots.txt' == $this->surl[0] && !$_GET) {
$this->surl = [''];
$_GET = ['_etc' => 'robots.txt'];
}
}

function log($mode, $data) {
if (!in_array($this->s_test_mode, [$mode, 'all']))
return;
sqlf('update $_memory set dt=' . SKY::$dd('dt') . ', tmemo=substr(' . SKY::$dd->f_cc('%s', 'tmemo') . ',1,15000) where id=10', date(DATE_DT) . " $mode $data\n");
}

function qs($url) {
return preg_match("$this->re(.*)$~", $url, $m) ? $m[3] : false;
}

function flag($flag = 0, $val = null, $char = 'v') {
$storage =& SKY::$mem[$char][2]['flags'];
if ($val == null)
return $flag ? $storage & $flag : $storage;
SKY::$char(null, ['flags' => $val ? $flag | $storage : ~$flag & $storage]);
}

function tail_x($plus = '', $stdout = '') {
if ($plus) # if not tailed correctly
trace($this->except ? $this->except['title'] : 'Unexpected Exit', true, 3);
Expand Down Expand Up @@ -341,8 +325,8 @@ function tracing($plus = '', $trace_x = false) {

function shutdown() {
chdir(DIR); # restore dir!
$dd = SQL::$dd = SKY::$dd; # main database driver
if (!$this->tailed) {
$dd = SQL::$dd = SKY::$dd; # set main database driver if SKY loaded
if (!$this->tailed) { # possible result of `die` function
global $user;
if (isset($user)) {
$this->flag(USER::NOT_TAILED, 1);
Expand Down Expand Up @@ -419,7 +403,8 @@ function pagination($ipp, $cnt = null, $ipl = 5, $current = null, $throw = true)
}
$sky->is_front or $throw = false;
$e = function ($no) use ($throw, $cnt, $sky) {
if ($throw && (404 != $no || $sky->s_error_404)) throw new Exception("pagination error $no");
if ($throw && (404 != $no || $sky->s_error_404))
throw new Exception("pagination error $no");
return [0, $no, $cnt];
};
if ($cnt <= $ipp) {
Expand Down
49 changes: 49 additions & 0 deletions sky
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env php
<?php

if (1 == $argc) {
echo <<<DOC
Usage: sky command [param ...]
Commands are:
d - list dirs (from current dir)
php - lint PHP files (from current dir)
jet - lint Jet files
m [id] - read tmemo cell from memory
c - Reparse controllers list
cache - Drop all cache
a - show actions
DOC;
exit;
}

define('DIR_R', getcwd());
do {
$dir = getcwd(); # add this dir to the PATH
if ($found = is_file('main/app/gate.php') && is_dir('var'))
break;
chdir('..');
} while($dir != getcwd());

$d = 'd' == $argv[1];
if ($d || 'php' == $argv[1]) {
require __DIR__ . '/w2/rare.php';
$dirs = Rare::walk_dirs(DIR_R);
if ($d) {
print_r($dirs);
} else foreach($dirs as $dir) {
$files = Rare::list_path($dir, 'is_file');
foreach($files as $file)
system("php -l $file");
}
exit("\n");
} if (!$found) {
exit("SKY application not found\n");
}

define('DIR', $dir);
require 'main/conf.php';
$sky = new SKY;
$sky->load();

new Console($argv[1]);
4 changes: 4 additions & 0 deletions sky.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@ECHO OFF
setlocal DISABLEDELAYEDEXPANSION
SET BIN_TARGET=%~dp0/sky
php "%BIN_TARGET%" %*
Loading

0 comments on commit b979968

Please sign in to comment.