[ -z "$UID" ] && UID=`id -u`
[ -z "$USER" ] && USER=`whoami`
[ -z "$OSTYPE" ] && OSTYPE=`uname|tr [:upper:] [:lower:]`
+[ -z "PWD" ] && PWD=`pwd`
#
# PATHS
}
' $noheader
}
+
+mail_open_relay() {
+ shell_help "Usage: $FUNCNAME MAIL_SERVER" "$@" && return;
+ cat <<EOF | nc $@ 25
+helo client.server.com
+mail from: rockyjr@vsnl.com
+rcpt to: vivek@nixcraft.in
+quit
+EOF
+}
+
+shell_push_arg() {
+ local ret="$1"
+ local to_var="$2"
+ case "$1" in
+ #) echo "Escape: $i" ;;
+ *[^[:alpha:]./_-]*) ret='"'${1//\"/\\\"}'"' ;;
+ esac
+ [ -z "$to_var" ] && echo "$ret" && return
+ eval "$to_var=\"\$$to_var \$ret\"; $to_var=\${$to_var## };"
+}
# Use the module out when format unknow
$out_conf = null;
- if (!preg_match('/^text|csv|yaml|json|table|div$/',$format)) {
+ if ($this->p('out') or !preg_match('/^text|csv|yaml|json|table|div$/',$format)) {
if (!($out_conf = out::$types[$format])) $this->bye("Unknow format `$format`");
+ if (empty($out_conf['enclose'])) $out_conf['enclose'] = array('','');
+ debug('Using out module!');
}
$count = 0;
while ($row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
- if ($count === 0) {
+ $count++;
+
+ #
+ # Head
+ #
+ if ($count === 1) {
+ if ($opt['is_html']) echo $this->html_nav_top();
+
if ($out_conf) {
- echo out::begin($out_conf,$row);
+ out::begin($out_conf,$this->fields());
+
} else {
- if ($opt['is_html']) echo $this->html_nav_top();
echo $this->{"rows_begin_$format"}($this->fields());
}
+
}
+
+ #
+ # Row
+ #
+ if ($out_conf and $count !== 1) out::row_end($out_conf);
- $count++;
$count_fields = 0;
foreach ($this->fields() as $f => $field) {
}
if ($out_conf) {
- echo out::row($out_conf,$row);
+ out::row($out_conf,$row);
} else {
echo $this->{"rows_rec_$format"}($row);
}
$opt['tot'] = $count;
$opt['count'] = $count;
$opt['limit'] = $limit;
- if ($count and $opt['is_html']) {
- //
- // Tot
- //
- if (!$where and !$limit) {
- debug("Table.rows(): Not using count(*)",1);
- $query = $this->db->conn->query("SELECT $count");
-
- } elseif ($select_count) {
- $query = $this->db->conn->query('SELECT FOUND_ROWS()');
-
- } elseif ($where) {
- $sql_count = $sql;
- $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count);
- $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count);
- $query = $this->db->conn->query($sql_count);
- } else {
- $query = $this->db->conn->query('SELECT count(*) FROM '.$this->sql_name());
+ if ($count) {
- }
+ if ($opt['is_html']) {
+ //
+ // Tot
+ //
+ if (!$where and !$limit) {
+ debug("Table.rows(): Not using count(*)",1);
+ $query = $this->db->conn->query("SELECT $count");
- if (!$query) $this->err_sql($sql);
+ } elseif ($select_count) {
+ $query = $this->db->conn->query('SELECT FOUND_ROWS()');
- $tot = $query->fetch(PDO::FETCH_COLUMN);
- $opt['tot'] = $tot;
- #if (!$tot) return;
+ } elseif ($where) {
+ $sql_count = $sql;
+ $sql_count = preg_replace('/ (ORDER|LIMIT) .*?$/s','',$sql_count);
+ $sql_count = preg_replace('/^SELECT .*FROM/s','SELECT count(*) FROM ',$sql_count);
+ $query = $this->db->conn->query($sql_count);
- $opt['count'] = $count;
- $opt['limit'] = $limit;
- }
+ } else {
+ $query = $this->db->conn->query('SELECT count(*) FROM '.$this->sql_name());
- if ($count === 0 and $this->p('header') === 'force') {
- echo $this->{"rows_begin_$format"}($this->fields());
- }
+ }
- if ($count) {
- if ($out_conf) {
- out::end($out_conf);
- } else {
- echo $this->{"rows_end_$format"}();
+ if (!$query) $this->err_sql($sql);
- if ($opt['is_html']) {
+ $tot = $query->fetch(PDO::FETCH_COLUMN);
+ $opt['tot'] = $tot;
+ #if (!$tot) return;
- echo '<div class="nav bottom">'
- .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
- .'</div>'.TABLE_EOL
- ;
- }
+ $opt['count'] = $count;
+ $opt['limit'] = $limit;
}
+
+ if ($count === 0 and $this->p('header') === 'force') {
+ echo $this->{"rows_begin_$format"}($this->fields());
+ }
+
+ if ($count) {
+ if ($out_conf) {
+ out::end($out_conf);
+ } else {
+ echo $this->{"rows_end_$format"}();
+
+ if ($opt['is_html']) {
+
+ echo '<div class="nav bottom">'
+ .$this->nav($opt['count'],$opt['tot'],$opt['limit'])
+ .'</div>'.TABLE_EOL
+ ;
+ }
+ }
+
+ } # < is_html
+
} # < count
$st->closeCursor();
* @return BOOL
*/
public static function is_hash(&$arr) {
- if (is_scalar($arr)) return false;
- //debug (array_values($arr));
+ if (!is_array($arr)) return false;
foreach ($arr as $k=>$v) {
return ($k === 0) ? false : true;
}
'head' => 'out_csv_head',
'function' => 'out_csv',
),
- 'labels' => array(
+ 'center' => array(
'enclose' => array("<center>".OUT_EOL,"</center>".PHP_EOL),
- 'tag_enclose' => 'labels',
+ 'tag_enclose' => 'div',
'tag' => 'label',
'function' => 'out_tag',
'head' => 'out_tag_head',
),
'json' => array(
'enclose' => array('['.OUT_EOL,OUT_EOL.']'),
- 'function' => 'json_encode',
+ 'function.ext' => 'json_encode',
'eol' => OUT_EOL,
'rec' => ',',
),
if (!empty($type) and !empty($data)) return $this->rows($type,$data,$head);
}
- public static function row($conf,&$row) {
+ public static function begin($o,$head) {
+ if (!empty($o['enclose'])) echo $o['enclose'][0];
+ if (!isset($o['head'])) return;
- #if (isset($conf['eol'])) $row = rtrim($row,$conf['eol']);
- if (!isset($conf['function'])) return;
- echo $conf['function']($row,$conf);
- }
+ if ($head === false) return;
+ if (empty($head) and self::is_hash($data[0])) {
+ $head = array_keys($data[0]);
+ }
+ echo $o['head']($head,$o) . empty($o['eol']) ? '' : $o['eol'];
- public static function end($conf) {
- if (!empty($conf['enclose'])) echo $conf['enclose'][1];
}
- public static function begin($conf,&$head) {
- if (!isset($conf['head'])) return;
+ public static function row($o,&$row) {
- if (!empty($conf['enclose'])) echo $conf['enclose'][0];
- if (empty($head) and self::is_hash($data[0])) {
- $head = array_keys($data[0]);
+ #if (isset($o['eol'])) $row = rtrim($row,$o['eol']);
+ #bye($o['function']);
+ if (isset($o['function.ext'])) {
+ echo $o['function.ext']($row);
+ return true;
}
- echo $conf['head']($head,$conf) . empty($conf['eol']) ? '' : $conf['eol'];
+ if (isset($o['function'])) {
+ echo $o['function']($row,$o);
+ return true;
+ }
+ #if (!isset($o['sep'])) $o['sep'] = "\t"; out_csv($row,$o);
+ out_tag($row,$o);
+ return false;
+ }
+ public static function row_end($o) {
+ if (!empty($o['rec'])) echo $o['rec'];
+ if (!empty($o['eol'])) echo $o['eol'];
+ }
+
+ public static function end($o) {
+ if (!empty($o['enclose'])) echo $o['enclose'][1];
}
public static function rows($type,&$data,$head=array()) {
$tot = count($data);
$count = 0;
- echo $conf['enclose'][0];
-
# Function head
- if ($head !== false) self::begin($conf,$head);
+ self::begin($conf,$head);
foreach ($data as $row) {
+ if ($count>0) out::row_end($conf);
$count++;
# Transform simple array into hash
self::row($conf,$row);
#echo " ($count)";
- if (!empty($conf['rec']) and $count<$tot) echo $conf['rec'];
- if (!empty($conf['eol'])) echo $conf['eol'];
}
- print $conf['enclose'][1];
+ if ($count) self::end($conf);
}
} Out::types() ; # < Class
function out_tag_head(&$data,$o) {
#return var_dump($row,true); return '';
$o['tag'] = isset($o['tag_head']) ? $o['tag_head'] : $o['tag'];
- #echo out_tag($row,$o); return true;
$row = out::ar_first($data);
if (!out::is_hash($row)) $row = $data;
#if (!is_array($row)) return out_csv(array_fill(0,count($row),'?'));