From 82bd6b893a3103836326dce7b6e1d698ac54f967 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Thu, 8 Feb 2018 05:06:04 +0000 Subject: [PATCH] lib/php/mime.php --- lib/php/mime.php | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/php/out.php | 2 +- www/dbq/dbq.php | 26 +++++++++-------------- 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/lib/php/mime.php b/lib/php/mime.php index 780da05d..83e47e3c 100644 --- a/lib/php/mime.php +++ b/lib/php/mime.php @@ -703,6 +703,61 @@ class Mime { if (empty(self::Types[$type])) return null; return self::Types[$type]; } + + private static function _finfo(&$content,&$info) { + if (!$content) return; + + static $finfo; + if (!isset($finfo)) $finfo = new finfo(FILEINFO_MIME); + + $type = $finfo->buffer($content); + if (! preg_match('/^([^\/]+\/([^\s;]+)).*$/',$type,$m) ) return; + $info['mime'] = $m[1]; + $info['ext'] = $m[2]; + #if ($info['ext'] == 'jpeg') debug($m); + return true; + } + + public static function getContent($content) { + if (!$content) return; + + static $finfo; + if (!isset($finfo)) $finfo = new finfo(FILEINFO_MIME); + + $type = $finfo->buffer($content); + if (! preg_match('/^([^\/]+\/([^\s;]+)).*$/',$type,$m) ) return; + return $m[1]; + } + + public static function html(&$str) { + + if (!$str) return $str; + + if (is_array($str)) { + foreach ($str as $k => $v) { + $str[$k] = self::html($v); + } + return $str; + } + + $mime = self::getContent($str); + + if (preg_match('@^image/@',$mime)) { + $v = self::is_base64($str) ? $v : base64_encode($str); + return ''; + } + + return $str; + } + + private static function is_base64($str) { + static $done = 0; + if (!$done) { + require_once(realpath(dirname(__FILE__).'/nb.php')); + $done = 1; + } + return Nb::is_base64($str); + } } #var_export(Http::msg(500)); diff --git a/lib/php/out.php b/lib/php/out.php index 439fe570..51ea254d 100644 --- a/lib/php/out.php +++ b/lib/php/out.php @@ -309,7 +309,7 @@ Class Out extends Nb { * format ar variable for scalar output */ public static function row_parse(&$row,$o=[]) { - foreach ($row as $k => $v) { + if (empty($o['no_format'])) foreach ($row as $k => $v) { $row[$k] = self::format($v,null,$o); } if (isset($o['row_parse_post'])) { diff --git a/www/dbq/dbq.php b/www/dbq/dbq.php index 2511446d..254aa922 100644 --- a/www/dbq/dbq.php +++ b/www/dbq/dbq.php @@ -246,20 +246,6 @@ class DbQ extends nb { if (empty($head)) $head = []; if (empty($this->default_limit)) $this->default_limit = $this->db->limits[0]; - # NB 31.01.18: Move to out.php or ldap.php - $exp = '^(jpegPhoto)$'; - if ($fct === null and $this->is_html) $fct = function(&$r) use ($exp) { - static $i; # to detect first header line - if ($i) foreach ($r as $k => $v) { - #$finfo = new finfo(FILEINFO_MIME); debug( $finfo->buffer($v) ); - if ($v and preg_match("/$exp/",$k) and $this->is_base64($v)) { - $r[$k] = ''; - #debug($r); - } - } - $i++; - }; - // Write output if ($this->expires and !preg_match('/^(dump|insert|update|replace|rm|vi)$/',$this->params['action'])) $this->page->expires = $this->expires; if (empty($this->_nopage)) { @@ -492,6 +478,9 @@ class DbQ extends nb { $row_parse_post = $this->db()->row_parse_post; return; } + # nb 31.01.18: move to out.php or ldap.php + if ($this->is_html) mime::html($r); + if (empty($row_parse_post)) return; $row_parse_post($r,$this->table); } @@ -564,8 +553,13 @@ class DbQ extends nb { public static function form_hidden($r) { $h = ''; - foreach ($r as $k => $v) { - $h .= ''; + #debug($r); + foreach ($r as $k => $values) { + $values = (array)$values; + $suff = count($values)>1 ? '[]' : ''; + foreach ($values as $v) { + $h .= ''; + } } return $h; } -- 2.47.3