]> git.nbdom.net Git - nb.git/commitdiff
lib/php/mime.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 8 Feb 2018 05:06:04 +0000 (05:06 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 8 Feb 2018 05:06:04 +0000 (05:06 +0000)
lib/php/mime.php
lib/php/out.php
www/dbq/dbq.php

index 780da05dd88d61724bd019d6fdf8c25e6dc263ce..83e47e3cb83644e4cfbf26d43670072b04a15053 100644 (file)
@@ -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 '<img src="data:image/jpeg;base64,'.$v.'" />';
+               }
+
+               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));
index 439fe570d2556e69c04e25880ea9cacf37a76ffe..51ea254dd79291404d1b3da15a7be30b50f2a02a 100644 (file)
@@ -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'])) {
index 2511446d2ed33ac52c456b13aec33edbd718cb60..254aa922e2935e0b5ce7886cf74ae5dd7f011a50 100644 (file)
@@ -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] = '<img src="data:image/jpeg;base64,'.$v.'" />';
-                                       #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 .= '<input type="hidden" name="'.htmlentities($k).'" value="'.htmlentities($v).'"/>';
+               #debug($r);
+               foreach ($r as $k => $values) {
+                       $values = (array)$values;
+                       $suff = count($values)>1 ? '[]' : '';
+                       foreach ($values as $v) {
+                               $h .= '<input type="hidden" name="'.htmlentities($k).$suff.'" value="'.htmlentities($v).'"/>';
+                       }
                }
                return $h;
        }