From: Nicolas Boisselier Date: Wed, 14 Dec 2016 00:43:47 +0000 (+0000) Subject: Bed X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=51333865b1b3680dca24170229d2edfffe0e42f0;p=nb.git Bed --- diff --git a/bin/nb-install b/bin/nb-install index d60e5b24..d63d298c 100755 --- a/bin/nb-install +++ b/bin/nb-install @@ -1,14 +1,14 @@ -#!/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 @@ -23,18 +23,23 @@ fi 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 @@ -56,7 +61,7 @@ if [ -d /etc/profile.d ]; then 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 diff --git a/bin/nb-update b/bin/nb-update index c033fe3f..559c039f 100755 --- a/bin/nb-update +++ b/bin/nb-update @@ -1,25 +1,33 @@ -#!/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 @@ -38,7 +46,7 @@ for repo in $(nb_repo); do 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 diff --git a/etc/profile b/etc/profile index 18f3f6f9..152ab35c 100755 --- a/etc/profile +++ b/etc/profile @@ -22,49 +22,60 @@ export NB_LOOP=1 # # 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" # @@ -73,12 +84,12 @@ else 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 @@ -88,4 +99,4 @@ else 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 diff --git a/etc/profile.d/envs b/etc/profile.d/envs index 0bc3d210..a2e66b0f 100644 --- a/etc/profile.d/envs +++ b/etc/profile.d/envs @@ -36,7 +36,7 @@ PATH=$(env_add_path "$PATH" \ /usr/local/sbin \ /usr/X11/bin/ \ /usr/share/awstats/tools \ - $(nb_repo bin sbin) \ + $(nb_repos bin sbin) \ /home/bin \ $HOME/bin \ ) @@ -50,7 +50,7 @@ MANPATH=$(env_add_path "$MANPATH" \ /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 diff --git a/etc/profile.d/functions b/etc/profile.d/functions index 37e512fe..26b792d5 100644 --- a/etc/profile.d/functions +++ b/etc/profile.d/functions @@ -3,8 +3,55 @@ # 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 ( @@ -364,6 +411,10 @@ jpg2avi() { 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 diff --git a/etc/profile.d/nb.sh b/etc/profile.d/nb.sh index b4f2835e..43d68cc5 100644 --- a/etc/profile.d/nb.sh +++ b/etc/profile.d/nb.sh @@ -2,7 +2,7 @@ nb_repos_commit_push() { 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" @@ -21,7 +21,7 @@ nb_repos_status() { 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/^/ /') diff --git a/etc/repos.conf b/etc/repos.conf new file mode 100644 index 00000000..a529b63b --- /dev/null +++ b/etc/repos.conf @@ -0,0 +1,11 @@ +# 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 diff --git a/lib/php/nb.php b/lib/php/nb.php index 158127a7..887bd73e 100644 --- a/lib/php/nb.php +++ b/lib/php/nb.php @@ -227,15 +227,20 @@ class NB { 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 @@ -788,6 +793,8 @@ class NB { } +/* +# 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']; @@ -800,6 +807,7 @@ class NB { echo $msg_cancel; exit; } +*/ /* * Function client_content_type diff --git a/lib/php/out.php b/lib/php/out.php index 45699013..7650ad75 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -437,9 +437,15 @@ Class Out extends Nb { 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; } diff --git a/lib/php/page.php b/lib/php/page.php index 918f2320..37eb71ab 100644 --- a/lib/php/page.php +++ b/lib/php/page.php @@ -72,8 +72,6 @@ class Page extends nb { $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(); @@ -92,8 +90,9 @@ class Page extends nb { 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); @@ -603,5 +602,18 @@ class Page extends nb { 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 ?> diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 479e80fd..72c21bea 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -1,9 +1,24 @@ 'human', + 'format' => DBQ_PARAM_FORMAT, 'db' => self::PARAM_DB_DEFAULT, 'table' => 'ls', 'action' => 'ls', @@ -38,7 +54,7 @@ class DbQ extends nb { public $colors = [ ]; - public $css = '/default.css'; + public $css = DBQ_CSS; public $ext; public $uri; @@ -52,23 +68,17 @@ class DbQ extends nb { 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); @@ -79,6 +89,7 @@ class DbQ extends nb { if ($this->params['format'] == 'html') $this->params['format'] = 'table'; + // // Page require_once(NB_ROOT.'/lib/php/page.php'); @@ -86,7 +97,7 @@ class DbQ extends nb { $this->page = new Page([ 'css' => $this->css, - 'title' => join(' ',$title), + 'title' => join(', ',$title), 'nav' => $nav, ]); @@ -182,7 +193,6 @@ table.rows th, table.rows td { border-color: '.$this->colors['body-border'].'; } 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( @@ -417,6 +427,16 @@ table.rows th, table.rows td { border-color: '.$this->colors['body-border'].'; } } 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());