#
# Build command
#
-my $url = ($ENV{$UC_NAME.'_URL'} ? $ENV{$UC_NAME.'_URL'} : 'http://127.0.0.1');
-my @cmd = (
- #'echo',
- 'curl', $url,
-);
-
-if ($url =~ m,^\w+://[\d\.]+/?,) {
- push (@cmd,'-H', 'Host: '.($ENV{$UC_NAME.'_HOST'} ? $ENV{$UC_NAME.'_HOST'} : 'rent'));
-}
-
-#
-# Add args
-#
-push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-';
+my @cmd;
+my $url;
+$url = shift @ARGV if @ARGV and ($ARGV[0] =~ m|^\w+://| or -e $ARGV[0] );
#
# Distinct arg from key=value
#
+push (@cmd,$_) if ($_ = '-'.'v'x($VERBOSE-1) ) ne '-';
my %keys = ();
while ($_ = shift @ARGV) {
#warn 'Uri: '.join("&",@keys)."\n" if $VERBOSE>1;
#warn 'Command:',map{(/^--data/?"\n ":" ").$_} @cmd,"\n" if $VERBOSE;
#die join(" ",@cmd);
+
+#
+# Choice http / php
+#
+$url = ($ENV{$UC_NAME.'_URL'} ? $ENV{$UC_NAME.'_URL'} : '/opt/rent/www/index.php');
+if (-e $url) {
+ @cmd = ('php','-f',$url);
+ # Push key=value
+ push(@cmd,map {$_.'='.$keys{$_}} sort keys %keys);
+
+} elsif($url) {
+ @cmd = ('curl', $url, @cmd);
+
+ # Push key=value
+ push(@cmd,map {('--data-binary',uri_esc($_).'='.uri_esc($keys{$_}))} sort keys %keys);
+
+# NB 01.03.16 if ($url =~ m,^\w+://[\d\.]+/?,) {
+# NB 01.03.16 push (@cmd,'-H', 'Host: '.($ENV{$UC_NAME.'_HOST'} ? $ENV{$UC_NAME.'_HOST'} : 'rent'));
+# NB 01.03.16 }
+
+} else {
+
+ die("$NAME: Please pass the url or the executable or set the ENV ".$UC_NAME.'_URL');
+}
+
#################################################################################
#
# END - Exec command
#
#################################################################################
-# Push key=value
-push(@cmd,map {('--data-binary',uri_esc($_).'='.uri_esc($keys{$_}))} sort keys %keys);
-
warn "$NAME: Command: ",join(" ",map{/\s+/ ? '"'.$_.'"' : $_} @cmd),"\n" if $VERBOSE;
system @cmd;
close STDOUT;
sub parse_debug {
while (<>) {
- if (/DEBUG: /) { print STDERR $_ }
- elsif ( /^<pre class="(\w+)">/) { print STDERR uc($1).': '.html2txt($_) }
+ if (/^(DEBUG|INFO|WARN|ERR|BYE): /) { print STDERR $_ }
+ elsif ( /^<pre class="[^"]*err">/) { print STDERR uc($1).': '.html2txt($_) }
elsif ( /^(<\w+>)?Fatal error/ .. /^\s+thrown in/) { print STDERR html2txt($_) }
elsif ( /^(<\s*\w+\s*\/?>)?(Parse error|Fatal error|Warning|Notice|$)/) { print STDERR $_ if ($_=html2txt($_)) and $_ !~ /^\s*$/ }
else { print $_ }
=head1 SYNOPSIS
-Quick usage:
+Quick usage: $NAME [URL|EXEC] [CURL_OPTIONS] [key=value]...
=over
=head1 DESCRIPTION
-Curl wrapper
+Curl / Php query wrapper
=head1 OPTIONS
function __construct($db) {
+ # Args
$db = is_scalar($db) ? array('name' => $db) : $db;
if (is_array($db)) {
foreach ($db as $k=>$v) $this->$k = $v;
}
- $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
- #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m);
-# NB 12.01.16 try {
-
- if ($this->pdo) {
- $this->conn = new PDO($this->pdo,$this->username,$this->password,$this->options);
- if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error);
- }
-
-# NB 12.01.16 } catch (PDOException $e) {
-# NB 12.01.16
-# NB 12.01.16 err($e->getMessage(), "Db.new()");
-# NB 12.01.16 return false;
-# NB 12.01.16
-# NB 12.01.16 }
+ # Pdo
+ if ($this->pdo) {
+ $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
+ if (!in_array($this->type,array(
+ 'sqlite',
+ 'mysql',
+ 'pgsql',
+ ))) $this->bye("unknow type = ".$this->type);
+ #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m);
+ $this->conn = new PDO($this->pdo,$this->username,$this->password,$this->options);
+ if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error);
+ }
+ # Name
#if (empty($this->name)) $this->name = preg_replace('/^(?:(?:sqlite:.*(.*?)(\.\w+)?)|(?:.*?dbname=([^;]+).*?))$/','\1',$this->pdo);
# NB 22.12.15 if (empty($this->name) and preg_match('/(?:sqlite:|dbname=)([^;\.]+)/',$this->pdo,$m)) {
if (empty($this->name) and preg_match('/(?:sqlite:(?:.*\/)?|dbname=)([^;\.]+)/',$this->pdo,$m)) {
$this->name = $m[1];
}
+
+ # Title
if (empty($this->title)) $this->title = prettyText($this->name);
+ # Types
if ($this->type == 'sqlite') {
$this->conn->sqliteCreateFunction('ip2int', function ($value) { return ip2long($value); });
return $_txt2md->text($txt);
}
-function bye($msg='') {
- if ($msg) err($msg);
+function bye($msg='',$backtrace_deep=0) {
+ if ($msg) err($msg,'bye',1+$backtrace_deep);
exit;
}
file_write("php://stderr","$msg\n");
}
-function err($msg) {
+function err($msg,$preff='err',$backtrace_deep=0) {
$msg = is_scalar($msg) ? $msg : print_r($msg,true);
- $msg = $msg.' '.nb::debug_backtrace_msg();
- echo( preg_match('/ml/i',nb::get_header('Content-type')) ? '<pre class="err">'.$msg.'</pre>' : "ERR: $msg").PHP_EOL;
+ $msg = $msg.' '.nb::debug_backtrace_msg(1+$backtrace_deep,NULL,strtoupper($preff).": ");
+
+ echo( preg_match('/ml/i',nb::get_header('Content-type'))
+ ? '<pre class="'.($preff and $preff!='err' ? 'err ': '').$preff.'">'.$msg.'</pre>'
+ : strtoupper($preff).": $msg"
+ ).PHP_EOL;
+
return false;
}
* @author NB
* Return backtrace message
*/
- static function debug_backtrace_msg($deep=NULL,$one_line=NULL) {
+ static function debug_backtrace_msg($deep=NULL,$one_line=NULL,$preff='') {
- $debug = debug_backtrace();
$msg = '';
- array_shift($debug);
if (isset($_REQUEST['_debug_backtrace_msg'])) $deep = $_REQUEST['_debug_backtrace_msg'];
- if ($deep===NULL or $deep===TRUE) {
+ #if ($deep===NULL or $deep===TRUE) {
- $msg .= ' [';
+ #$msg .= "\n[";
- foreach (array_reverse($debug) as $i=>$call_info)
- $msg .= ( $one_line ? ($i>0 ? ", " : "") : "\n\t" ) . nb::debug_backtrace_info($call_info);
+ $debug = debug_backtrace(); array_shift($debug);
- $msg .= ($one_line ? "" : "\n")."]";
+ $tot = count($debug);
+ foreach (array_reverse($debug) as $i=>$call_info) {
+ $msg .= ( $one_line ? ($preff !=='' ? $preff : ' | ') : "\n$preff " ) . nb::debug_backtrace_info($call_info);
+ if ($deep and ($tot-$i) <= $deep+1) break;
+ }
- } elseif (isset($call_info[(int)$deep])) {
- $msg .= ' '.nb::debug_backtrace_info($call_info[(int)$deep]);
+ #$msg .= ($one_line ? "" : "\n")."]";
- }
+ #} elseif (isset($call_info[(int)$deep])) {
+ #$msg .= ' '.nb::debug_backtrace_info($call_info[(int)$deep]);
+
+ #}
return $msg;
}
#else ($call_info['type']) $call_info['type'] = $call_info['type'];
// Default values
- if (isset($call_info['file'])) $call_info['file'] = str_replace($_SERVER['DOCUMENT_ROOT'].'/','',$call_info['file']);
+ if (isset($call_info['file']) and !empty($_SERVER['DOCUMENT_ROOT']))
+ $call_info['file'] = str_replace($_SERVER['DOCUMENT_ROOT'].'/','',$call_info['file'])
+ ;
// Get infos
$msg = array();
'file',
'line',
'function',
- # NB 21.07.11 'type',
'type',
'message',
'code',
) as $i) {
- die($i);
if (array_key_exists($i,$call_info)) $msg[] = "$i=".$call_info[$i];
}