]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 14 Dec 2016 00:43:47 +0000 (00:43 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 14 Dec 2016 00:43:47 +0000 (00:43 +0000)
bin/nb-install
bin/nb-update
etc/profile
etc/profile.d/envs
etc/profile.d/functions
etc/profile.d/nb.sh
etc/repos.conf [new file with mode: 0644]
lib/php/nb.php
lib/php/out.php
lib/php/page.php
www/dbq/dbq.php

index d60e5b24cd2ed7d2a9e89f4b122f7abd09992375..d63d298c2ba592b3c3a90c8b203c238ef2fa8575 100755 (executable)
@@ -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
 
index c033fe3fb7250a20072b6df1151aed89c07b07a3..559c039f216a96b6d89ad1cfd557ef5fda0509f7 100755 (executable)
@@ -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
index 18f3f6f9e8680ecf895e29d389576f60534ce404..152ab35c9ab2c945ddeb854384bf664eb208d4eb 100755 (executable)
@@ -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
index 0bc3d210696d466969d77b3a0d672f571f67b2da..a2e66b0fad20fc4d534b0c75d3c145db24da1524 100644 (file)
@@ -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
 
index 37e512fed9866929c83ac1eb79b047bd0be82c2f..26b792d5d497d4495a7983417663636bbee0f2ce 100644 (file)
@@ -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
index b4f2835e31a6dafd74deb45e14961250dd006821..43d68cc57c330ce85183c6905ef285606d9273aa 100644 (file)
@@ -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 (file)
index 0000000..a529b63
--- /dev/null
@@ -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
index 158127a79e7791766a4aa3d91109989dcdc9ce96..887bd73e0682485307e6c15224c5ebb2963b5c5a 100644 (file)
@@ -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
index 4569901352e16a6306100d3cd800129ca162657e..7650ad75a85812d5be5f826622f85fd5127ae717 100644 (file)
@@ -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;
   }
 
index 918f232023a025450e206cf0edd9d59504769ffe..37eb71abe5e12d3a72cae06817e1252bf93541e1 100644 (file)
@@ -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
 ?>
index 479e80fd6b8a2e098ead1514f917a53c8d6d2e07..72c21bea0def34b3466b697bd110b015ba938001 100644 (file)
@@ -1,9 +1,24 @@
 <?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;
@@ -11,13 +26,14 @@ class DbQ extends nb {
   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',
@@ -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());