-#!/usr/bin/env bash
+#!/usr/bin/env sh
##############################################################################
#
# Install - NB 18.11.14
#
##############################################################################
-. "${BASH_SOURCE%/*}/../etc/profile" || exit
-[ -z "$NB_ROOT" ] && exit
-declare -r NAME="$(basename "${0}")"
-declare TMP=/tmp/$NAME.$$
-declare INSTALL="rsync --archive --checksum --force"
+# NB 13.12.16 . "${BASH_SOURCE%/*}/../etc/profile" || exit
+. $(dirname "$0")/../etc/profile || exit 1; [ -z "$NB_ROOT" ] && exit
+NAME="$(basename "${0}")"
+TMP=/tmp/$NAME.$$
+INSTALL="rsync --archive --checksum --force"
if [ "$UID" != "0" ] ; then
echo "$NAME: please login as root." 1>&2
case "$@" in *-h*|*-help*) exec echo "Usage: $NAME [-v] [-n]";; esac
case "$@" in *-v*) INSTALL="$INSTALL -v";; esac
case "$@" in *-n*) INSTALL="$INSTALL -n";; esac
+if ! which rsync >/dev/null; then
+ INSTALL="cp -a"
+ case "$@" in *-v*) INSTALL="$INSTALL -v";; esac
+ case "$@" in *-n*) INSTALL="echo DRY:";; esac
+fi
##############################################################################
#
# Functions
#
##############################################################################
-function verbose() {
+verbose() {
[ -z "$@" ] && return
printf '> %s\n' "$@"
}
-function fdiff() {
+fdiff() {
[ -e "$1" ] || return 0
[ -e "$2" ] || return 0
[ -n "$(diff -q $1 $2 2>/dev/null)" ] && return 0
if fdiff $TMP.profile /etc/profile.d/nb.sh 1>/dev/null 2>&1; then
verbose "Install /etc/profile.d/nb.sh"
- $INSTALL -c $TMP.profile /etc/profile.d/nb.sh
+ $INSTALL $TMP.profile /etc/profile.d/nb.sh
chmod 755 /etc/profile.d/nb.sh
fi
-#!/usr/bin/env bash
+#!/usr/bin/env sh
#####################################################################
#
# Pull update and run install script
# Called by cron/etc/nb
#
#####################################################################
-. "${BASH_SOURCE%/*}/../etc/profile" || exit
-declare -r NAME="$(basename "${0}")"
-declare -r TMP="/tmp/$NAME.tmp"
-declare CLEANUP=0
+#echo . "${BASH_SOURCE%/*}/../etc/profile" || exit
+#. "${BASH_SOURCE%/*}/../etc/profile" || exit
+#echo "$0 | $(dirname "$0")/.. | $PATH"
+. $(dirname "$0")/../etc/profile || exit 1; [ -z "$NB_ROOT" ] && exit
+#nb_repos exit;
+NAME="$(basename "${0}")"
+TMP="/tmp/$NAME.tmp"
+CLEANUP=0
[ "$1" = "-cleanup" ] && CLEANUP=1
#####################################################################
# Gits pull
#
+if ! which git > /dev/null; then
+ echo "$NAME: can't find git!" 1>&2
+ exit 1
+fi
ex=0
printf '#!%s\n%s\n' `which sh` 'ssh -o BatchMode=yes $@' > "$TMP" && chmod 755 "$TMP"
export GIT_SSH="$TMP"
-for repo in $(nb_repo); do
+for repo in $(nb_repos); do
[ -d "$repo/.git" ] || continue
cd "$repo" || continue
if [ 1 = 1 ] && ! git_conn_check; then
echo
elif [ "$USER" = "$user" ]; then
- bash -c "$cmd"
+ sh -c "$cmd"
elif [ "$USER" = "root" ]; then
su $user -c "$cmd"
else
#
# SEARCH ROOT
#
-NB_CURRENT_DIR=''
+unset NB_ETC_DIR
#[ "${BASH-no}" != "no" ] && echo Is BASH
+#echo BASH_SOURCE=$BASH_SOURCE ZSH_VERSION=$ZSH_VERSION KSH_VERSION=$KSH_VERSION
if [ -n "$BASH_SOURCE" ]; then
- #NB_CURRENT_DIR="${BASH_SOURCE%/*}"
- NB_CURRENT_DIR="${BASH_SOURCE%/*}"
+ #NB_ETC_DIR="${BASH_SOURCE%/*}"
+ NB_ETC_DIR="${BASH_SOURCE%/*}"
elif [ -n "$ZSH_VERSION" ]; then
- eval 'NB_CURRENT_DIR=${(%):-%N}'
- NB_CURRENT_DIR=${dir%/*}
+ eval 'NB_ETC_DIR=${(%):-%N}'
+ NB_ETC_DIR=${dir%/*}
elif [ "$KSH_VERSION" != "" ]; then
#elif eval '[[ -n ${.sh.file} ]]' 2>/dev/null; then
- eval 'NB_CURRENT_DIR="$(dirname "${.sh.file}")"'
+ eval 'NB_ETC_DIR="$(dirname "${.sh.file}")"'
elif [ -r /etc/profile.d/nb.sh ]; then
- NB_CURRENT_DIR=$(dirname $(grep '\. ' -m1 /etc/profile.d/nb.sh | cut -d'.' -f2 /etc/profile.d/nb.sh))
-
-elif [ -r "$0" ]; then
- NB_CURRENT_DIR="$(dirname "$0")"
+ NB_ETC_DIR=$(dirname $(grep '\. ' -m1 /etc/profile.d/nb.sh | cut -d'.' -f2 /etc/profile.d/nb.sh))
else
# Alpine, busybox, ...
+ # We guess by looking for file !!
+ tmp=$PWD
for i in /*/*/etc/profile.d/functions /*/*/*/etc/profile.d/functions; do
+
cd "$(dirname "$i")"/.. &> /dev/null || continue
+
+ # Check other files
[ -e profile.d/envs ] || continue
- NB_CURRENT_DIR=`pwd` || unset NB_CURRENT_DIR
+ [ -e profile.d/aliases ] || continue
+
+ NB_ETC_DIR=$PWD #|| unset NB_ETC_DIR
break
+
done
+ cd "$tmp"
fi
-if [ -z "$NB_CURRENT_DIR" ]; then
+if [ -z "$NB_ETC_DIR" -o "$NB_ETC_DIR" = "/" ]; then
export NB_UNSUPORTED=1
[ -n "$NB_DEBUG" ] && $NB_DEBUG "ERR: Unsupported shell. Please use bash, ksh93 or zsh."
else
unset NB_UNSUPORTED
- #echo $NB_CURRENT_DIR; #return
+ #echo "+$NB_ETC_DIR"
+ #echo $NB_ETC_DIR; #return
- [ -e "$NB_CURRENT_DIR/profile.d/functions" ] && . "$NB_CURRENT_DIR/profile.d/functions"
- [ -n "$NB_DEBUG" ] && $NB_DEBUG ". $NB_CURRENT_DIR/profile.d/functions"
+ # Check to avoid system crash !
+ [ ! -e "$NB_ETC_DIR/profile.d/functions" ] && echo "ERR: $NB_ETC_DIR: Wrong directory (no profile.d/functions)" 1>&2 && return 1
+ #[ -e "$NB_ETC_DIR/profile.d/functions" ] &&
+ . "$NB_ETC_DIR/profile.d/functions" || return
+ [ -n "$NB_DEBUG" ] && $NB_DEBUG ". $NB_ETC_DIR/profile.d/functions"
- export NB_ROOT=$(realpath $NB_CURRENT_DIR/.. 2>/dev/null)
+ export NB_ROOT=$(realpath $NB_ETC_DIR/.. 2>/dev/null)
[ -n "$NB_DEBUG" ] && $NB_DEBUG "NB_ROOT=$NB_ROOT"
#
if [ -n "$NB_ROOT" ]; then
#[ -e "$NB_ROOT/etc/profile.d/envs" ] && . "$NB_ROOT/etc/profile.d/envs"
- #${NB_ROOT}/etc/profile.d/*.sh
for i in \
${NB_ROOT}/etc/profile.d/envs \
${NB_ROOT}/etc/profile.d/aliases \
- $(nb_repo "etc/profile" "etc/profile.d/*.sh" | grep -vFx "${NB_ROOT}/etc/profile") \
+ $(nb_repos_ls "etc/profile" "etc/profile.d/*.sh" | awk '$0 != "'${NB_ROOT}/etc/profile'"') \
;do
+# NB 13.12.16 $(nb_repos_ls "etc/profile" "etc/profile.d/*.sh" | grep -vFx "${NB_ROOT}/etc/profile") \
[ -n "$NB_DEBUG" ] && $NB_DEBUG ". $i"
[ -r "$i" -a -f "$i" ] && . "$i"
done
fi
#export NB_LASTTIME=$(date +"%Y-%M-%d %T")
-unset i tmp NB_CURRENT_DIR NB_DEBUG NB_LOOP
+unset i tmp NB_ETC_DIR NB_DEBUG NB_LOOP
/usr/local/sbin \
/usr/X11/bin/ \
/usr/share/awstats/tools \
- $(nb_repo bin sbin) \
+ $(nb_repos bin sbin) \
/home/bin \
$HOME/bin \
)
/usr/local/man\
/usr/local/memcached/share/man \
/opt/local/share/man \
- $(nb_repo | xargs -n1 -IZ echo "Z/man" 2>/dev/null) \
+ $(nb_repos | xargs -n1 -IZ echo "Z/man" 2>/dev/null) \
)
export MANPATH
# FUNCTIONS - CORE
#
#################################################################################
+nb_repos() {
+ [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos' # for busybox
+ (
+ # Always !
+ echo "$NB_ROOT"
+
+ # Conf file
+ local conf=/etc/nb/repos.conf
+ [ -s "$conf" ] || conf="$NB_ROOT/etc/repos.conf"
+
+ # Realpath
+ local IFS=$'\n'
+ for dir in $(ls -d1 $(grep -v '^ *\(#\|$\)' "$conf") 2>/dev/null); do
+
+ # Accept directories or files (ex: use to tag a directory: .nb-install)
+ [ -d "$dir" ] || dir=$(dirname "$dir")
+
+ dir=$(realpath "$dir")
+ echo "$dir"
+
+ done
+
+ # no doubles
+ # strong if awk failed
+ ) | awk \!'x[$0]++' || echo "$NB_ROOT"
+}
+
+nb_repos_ls() {
+ [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repos_ls' # for busybox
+
+ # Look for paths
+ local d f i repo IFS=$'\n'
+ for d in `nb_repos`; do
+
+ for f in "$@"; do
+
+ #[ -n "$NB_DEBUG" ] && $NB_DEBUG "====$d/$f"
+ for repo in "$d"/$f; do
+ [ -e "$repo" ] && echo "$repo"
+ done
+
+ done
+
+ done
+}
+
+# NB 13.12.16: TODO replace with nb_repos and nb_repos_ls
nb_repo() {
- [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repo'
+ [ -z "$FUNCNAME" ] && local FUNCNAME='nb_repo' # for busybox
if [ -z "$*" ]; then
# Find repos
(
cron_d_install() {
local usage fname file exp cron tmp test
usage="Usage: $FUNCNAME [-t|--test] [CRON_D_FILE] [IGNORE_REGEXP] or [NAME] [CRON_D_CONTENT] [IGNORE_REGEXP]"
+ if ! perl -e '' 2>/dev/null; then
+ echo "cron_d_install: can't run perl" 1>&2
+ return 1
+ fi
shell_help_noarg "$usage" "$@" && return 1
case "$1" in -t|--test) test=1; shift ;; esac
shell_help "Commit each repos whith a comment" "$@" && return
local status
local pwd=`pwd`
- for i in $(nb_repo); do
+ for i in $(nb_repos); do
cd "$i" || continue
printf ">%s\n" "$i"
local status
local pwd=`pwd`
local i
- for i in $(nb_repo); do
+ for i in $(nb_repos); do
cd "$i" || continue
status=$(git status --short "$i"|sed 's/^/ /')
--- /dev/null
+# NB 13.12.16: Default
+/*/*/.nb-install
+/usr/local/src/*/.nb-install
+/home/*/*/.nb-install
+/Users/*/*/.nb-install
+/home/*/git/*/.nb-install
+/Users/*/git/*/.nb-install
+/home/*/app/*/.nb-install
+/Users/*/app/*/.nb-install
+/home/*/code/*/.nb-install
+/Users/*/code/*/.nb-install
static $header = null;
if ( $header === null ) {
$header = [];
+
if (function_exists('getallheaders')) {
$header = getallheaders();
+
} else {
foreach ($_SERVER as $name => $value) {
- if (substr($name, 0, 5) == 'HTTP_') {
- $header[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
- }
+ if (substr($name, 0, 5) == 'HTTP_') {
+ $header[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
+ }
}
+ #bye( [$name,$header] ); # argcs ????
+
}
+
}
# Array
}
+/*
+# NB 14.12.16: Moved to page
public static function http_user() {
if (!empty($_SERVER['REMOTE_USER'])) return $_SERVER['REMOTE_USER']; # empty since jessie
if (!empty($_SERVER['PHP_AUTH_USER'])) return $_SERVER['PHP_AUTH_USER'];
echo $msg_cancel;
exit;
}
+*/
/*
* Function client_content_type
public static function client_type() {
+ static $type = null;
+ if ($type !== null) return $type;
+ $type = '';
+
foreach (array_keys(self::$types) as $t) {
if (preg_match("@^\w+/$t@",self::client_header('Accept'))) return $t;
}
+
+ return $type;
#return;
}
$links = [];
if (isset($opt['nav'])) {
list($title,$html) = self::nav_parse($opt['nav']);
- if ( empty($this->h1) ) $this->h1 = join($this->sep,$html);
- if ( empty($this->title) ) $this->title = join($this->sep,$title);
unset($opt['nav']);
}
# NB 06.12.16 if ( empty($this->title) ) $this->title = !empty($title) ? join($this->sep,$title) : $this->filename2title();
parent::__construct($opt);
// Defaults
- if (empty($this->title)) $this->title = $this->filename2title();
- if ( empty($this->h1) and ! empty($this->title)) $this->h1 = $this->title;
+ if ( !empty($title) and empty($this->title) ) $this->title = join($this->sep,$title);
+ if ( empty($this->title) ) $this->title = $this->filename2title();
+ if ( empty($this->h1) and ! empty($this->title) ) $this->h1 = $this->title;
// Call / Print
if (!empty($this->call)) $this->call($this->call);
return $content_type;
}
+ public static function header_user() {
+ if (!empty($_SERVER['REMOTE_USER'])) return $_SERVER['REMOTE_USER']; # empty since jessie
+ if (!empty($_SERVER['PHP_AUTH_USER'])) return $_SERVER['PHP_AUTH_USER'];
+ return '';
+ }
+
+ public static function header_login($msg_box='My Realm',$msg_cancel='Please login!') {
+ header('WWW-Authenticate: Basic realm="'.$msg_box.'"');
+ header('HTTP/1.0 401 Unauthorized');
+ echo $msg_cancel;
+ exit;
+ }
+
} return; # < Class
?>
<?php
require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php'));
require_once(NB_ROOT.'/lib/php/http.php');
+
+# Minimum permission access 0 = readonly
define('DBQ_PERM',(int)(isset($_SERVER['DBQ_PERM']) ? $_SERVER['DBQ_PERM'] : 9));
+
define('DBQ_PARAM_DEFAULT',(string)(isset($_SERVER['DBQ_PARAM_DEFAULT']) ? $_SERVER['DBQ_PARAM_DEFAULT'] : 'ls'));
+define('DBQ_PARAM_FORMAT',(string)(isset($_SERVER['DBQ_PARAM_FORMAT']) ? $_SERVER['DBQ_PARAM_FORMAT'] :
+ (!empty($_SERVER['HTTP_USER_AGENT']) and
+ preg_match('/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/',$_SERVER['HTTP_USER_AGENT'])
+ )
+ ? 'html'
+ : 'human'
+));
+
+define('DBQ_CSS',(string)(isset($_SERVER['DBQ_CSS']) ? $_SERVER['DBQ_CSS'] : '/default.css'));
+define('DBQ_TITLE',(string)(isset($_SERVER['DBQ_TITLE']) ? $_SERVER['DBQ_TITLE'] : 'Dbq'));
+
class DbQ extends nb {
+
const ADMIN = 9;
const DELETE = 4;
const WRITE = 3;
public $perm = DBQ_PERM;
public $run = false;
- public $title = 'Dbq';
+ public $title = DBQ_TITLE;
const PARAM_ARGS_SEP = '|';
const PARAM_EXP = '[\w\._:-]{2,100}';
const PARAM_DB_DEFAULT = DBQ_PARAM_DEFAULT;
+
public $params = [
- 'format' => 'human',
+ 'format' => DBQ_PARAM_FORMAT,
'db' => self::PARAM_DB_DEFAULT,
'table' => 'ls',
'action' => 'ls',
public $colors = [
];
- public $css = '/default.css';
+ public $css = DBQ_CSS;
public $ext;
public $uri;
public function __construct($opt=[]) {
#debug(self::VIEW);
+ //
// Pre defaults values
- $this->params['format'] = (!empty($_SERVER['HTTP_USER_AGENT']) and
- preg_match('/^\S+\s+.Windows|iPhone|Android|Macintosh|Linux/',$_SERVER['HTTP_USER_AGENT'])
- )
- ? 'html'
- : $this->params['format']
- ;
-
- // Envs
+
+ // Envs -> params
foreach ([
] as $k) {
$env = 'DBQ_PARAM_'.strtoupper($k);
if (!empty($_SERVER[$env])) $this->env = $_SERVER[$env];
}
- if (!empty($_SERVER['DBQ_CSS'])) $this->css = $_SERVER['DBQ_CSS'];
- if (!empty($_SERVER['DBQ_TITLE'])) $this->title = $_SERVER['DBQ_TITLE'];
+ //
// Init
$this->uri_params();
parent::__construct($opt);
if ($this->params['format'] == 'html') $this->params['format'] = 'table';
+ //
// Page
require_once(NB_ROOT.'/lib/php/page.php');
$this->page = new Page([
'css' => $this->css,
- 'title' => join(' ',$title),
+ 'title' => join(', ',$title),
'nav' => $nav,
]);
require_once(NB_ROOT.'/lib/php/db.php');
// New
- $_SERVER['_DBQ_CONF_FILE'] = '/opt/nb/etc/dbq/000-local.php';
global $DB_CONFS;
if (!empty($_SERVER['DBQ_CONF_DIR'])) {
$DB_CONFS = Db::conf_load(array_merge(
} elseif ($action == 'cryptkey') {
$this->page($this->db,'cryptkey',['key']);
+ } elseif ($action == 'headers' and $this->perm >= self::ADMIN) {
+ # NB 14.12.16: TODO NOT STABLE
+ $rows = [];
+ foreach ($this->page->client_header() as $k=>$v) {
+ $rows[] = [ 'name'=>$k, 'value'=>$v ];
+ }
+ #throw new Exception(var_export('hi',1));
+ #$this->bye($this->page->client_header());
+ $this->page($rows);
+
} elseif ($action == 'phpinfo' and $this->perm >= self::ADMIN) {
$this->page($this->page->phpinfo());