#!/usr/bin/env php
<?php
+require_once(realpath(dirname(__FILE__).'/../lib/php/nb.php'));
/**
* @copyright (C) 2022 Nicolas Boisselier
* @author Nicolas Boisselier nicolas.boisselier@gmail.com
$_SERVER['REQUEST_URI'] = $GLOBALS['argv'][1];
for($i=2;$i<count($GLOBALS['argv']);$i++) {
+
list($k,$v) = explode('=',trim($GLOBALS['argv'][$i]));
- $_GET[$k]= $v;
- $_SERVER['QUERY_STRING'] .= (empty($_SERVER['QUERY_STRING']) ? '' : '&').trim($GLOBALS['argv'][$i]);
+
+ if ($k == '__YAML__') {
+ #$str = file_get_contents($GLOBALS['argv'][$i+1]);
+ $str = file_get_contents($v);
+ #bye($str);
+ $_GET = nb::yaml_decode($str)[0];
+
+ } else {
+ $_GET[$k]= $v;
+
+ # NB 05.09.24 $_SERVER['QUERY_STRING'] .= (empty($_SERVER['QUERY_STRING']) ? '' : '&').trim($GLOBALS['argv'][$i]);
+ }
+
}
- if (!empty($_SERVER['QUERY_STRING'])) $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
+ # NB 05.09.24 if (!empty($_SERVER['QUERY_STRING'])) $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING'];
}
+#bye($_GET);
+$GLOBALS['argv'] = []; # to cancel argv2request
require(realpath(dirname(__FILE__).'/../www/dbq/dbq.php'));
?>
foreach (['json','yaml'] as $type) {
if (!strpos($content_type,'/'.$type)) continue;
-# NB 20.07.18 if ($raw) $_POST = [ $type => $raw ];
-# NB 20.07.18 if (empty($_POST[$type])) continue;
-# NB 20.07.18 #bye($_POST);
-# NB 20.07.18 $post = $_POST;
-# NB 20.07.18
-# NB 20.07.18 unset($post[$type]);
$fct = $type . '_decode';
$_POST = (array)$this->$fct(file_get_contents("php://input"));
# NB 30.08.24: Done in bin/dbq.php
//if (empty($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = join('/',( count($GLOBALS['argv'])>0 ) ? array_slice($GLOBALS['argv'],1) : []);
+ if (!isset($_SERVER['REQUEST_URI'])) $_SERVER['REQUEST_URI'] = '/';
list ($path,$args) = strpos($_SERVER['REQUEST_URI'],'?') ? explode('?',$_SERVER['REQUEST_URI']) : [$_SERVER['REQUEST_URI'],''];
$this->uri = $path;
$this->uri_params = explode($this->explode_args,$args);
foreach (['table','action','args'] as $k) { if (!empty($this->params[$k])) $arg[] = $this->params[$k]; }
$arg = join('/',$arg);
-/*
-# NB 14.12.22: What use ???
- $conf = [];
- $fct_help = function() use ($conf,$arg) {
- $this->page($this->db,$conf['root']);
- };
- $conf = [
- 'root' => [
- [ 'help', 'This help', $fct_help ],
- # NB 14.12.22 [ 'help', 'This help', function() use ($conf) { $this->page($this->db,$conf['root']); } ],
- [ 'ls', 'List configured databases', function() {
- $this->page($this->db,'ls',[],function(&$r){
- if ($this->perm < self::ADMIN) unset($r['host'],$r['type']);
- $r['id'] = $this->page->tag('a',$r['id'],'href="'.$this->db->base.'/'.$r['id'].'"');
- });
- } ],
- [ 'random_str', 'Generate a random string', function() { $this->page($this->random_str()); } ],
-# NB 30.01.19 [ 'databases', 'List others databases', function() {
-# NB 30.01.19 $this->page($this->db,'databases');
-# NB 30.01.19 }, self::ADMIN ],
- ],
- ];
- #$conf['root'][0][2] = function() use ($conf) { $this->page($this->db,$conf['root']); };
- foreach ($conf['root'] as $cmd) {
- $action_ = $cmd[0];
- $laius = $cmd[1];
- $fct = $cmd[2];
- $perm = empty($cmd[3]) ? 0 : $cmd[3];
-
- if ($action != $action_) continue;
- $this->deniedUnless(!$perm or $this->perm >= $perm);
- #debug($action_);
- $fct();
- }
-*/
if ($action == 'help') {
$this->page($this->db
} elseif ($action == 'insert') {
$this->deniedUnless($this->perm >= self::WRITE);
- if (!$this->table->insert(empty($_POST) ? $_GET : $_POST,$info)) $this->error('insert: '.print_r($info,true));
+ if (!$this->table->insert($this->getData(),$info)) $this->error('insert: '.print_r($info,true));
header('Location: '.$this->table->base.'/');
$this->page($info);
} elseif ($action == 'update') {
$this->deniedUnless($this->perm >= self::WRITE);
- if (!$this->table->update(empty($_POST) ? $_GET : $_POST,$info)) $this->error('update: '.print_r($info,true));
+ if (!$this->table->update($this->getData(),$info)) $this->error('update: '.print_r($info,true));
$this->redirect_if_no_referer($this->table->base.'/');
$this->page($info);
} elseif ($action == 'replace') {
$this->deniedUnless($this->perm >= self::WRITE);
- if (!$this->table->replace(empty($_POST) ? $_GET : $_POST,$info)) $this->error('replace: '.print_r($info,true));
+ if (!$this->table->replace($this->getData(),$info)) $this->error('replace: '.print_r($info,true));
$this->redirect_if_no_referer($this->table->base.'/');
$this->page($info);
} elseif ($action == 'rm') {
$this->deniedUnless($this->perm >= self::DELETE);
- if (!$this->table->delete(empty($_POST) ? $_GET : $_POST,$info)) $this->error('rm: '.print_r($info,true));
+ if (!$this->table->delete($this->getData(),$info)) $this->error('rm: '.print_r($info,true));
$this->redirect_if_no_referer($this->table->base.'/');
$this->page($info);
}
+ private function getData() {
+ $data = [];
+ if (empty($data) and !empty($_POST)) $data = $_POST;
+ if (empty($data) and !empty($_GET)) $data = $_GET;
+ # NB 05.09.24 if (!empty($data['__YAML__'])) $data = $this->yaml_decode($data['__YAML__']);
+ # NB 05.09.24 if (!empty($data['__JSON__'])) $data = $this->yaml_decode($data['__JSON__']);
+#bye($data);
+ return $data;
+ }
+
public function redirect_if_no_referer($url) {
if (!empty($_SERVER['HTTP_REFERER'])) $url = $_SERVER['HTTP_REFERER'];
if ($this->p('referer')) $url = urldecode($this->p('referer'));