e09Div.php readfile and mime types

  1. define
  2. readFile jpg
  3. readFile pdf
  4. readFile img

demo: request_method=GET

info

Source with linenums

*** code does not have a span berfore first <br>***
<?php 
    if (isset($_POST['sub']) and $_POST['sub'] != '' )
        $fun = $_POST['sub'];
    elseif (isset($_GET['sub']) and $_GET['sub'] != '' )
        $fun = $_GET['sub'];
    else
        $fun = 'request_method=' . $_SERVER['REQUEST_METHOD'];
    if ( substr($fun, 0, 6) == 'readFi') {
        if ( function_exists($fun)) {
            makeMime();
            $fun();
        } else  {
?>

<h1>readFile only for <?php echo $fun . ' missing for ' . basename(__file__); ?> </h1>

<?php } } else { ?>

<form action="<?php echo basename(__file__); ?>" method="post">
<h2><?php echo basename(__file__); ?> readfile and mime types</h3>
<ol>
    <li> <input type="submit" name="sub" value="phpDemo2"/> define</li>
    <li> <input type="submit" name="sub" value="readFileJpg"/> readFile jpg</li>
    <li> <input type="submit" name="sub" value="readFilePdf"/> readFile pdf</li>
    <li> <input type="submit" name="sub" value="embedImg"/> readFile img</li>
</ol>
<?php 
    echo "<h2> demo: $fun </h2>";
    if (isset($_POST['sub']))
        $fun();
?>
</form>
<h2>info</h2>
<ul>
<?php
echo '<li>$_SERVER[REQUEST_METHOD] = ' . $_SERVER['REQUEST_METHOD'];
echo '</li><li> $_GET = '; print_r($_GET); 
echo '</li><li> $_POST = '; print_r($_POST); 
?> 
</li></ul>
<h2>Source with linenums</h2>
<?php echo highlightNum(__file__); ?>
<h2>Source</h2>
<?php highlight_file(__file__) ?>
<?php } ?>
<?php

function makeMime() { # just in global scope, does not work here, because assignment will be executed only after call of readFileJpg ....
    global $mimeTypes;
    $mimeTypes = [
        'pdf' => 'application/pdf',
        'txt' => 'text/plain',
        'html' => 'text/html',
        'exe' => 'application/octet-stream',
        'zip' => 'application/zip',
        'doc' => 'application/msword',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'gif' => 'image/gif',
        'png' => 'image/png',
        'jpeg' => 'image/jpg',
        'jpg' => 'image/jpg',
        'php' => 'text/plain'
    ];
}

function readFileJpg() {
    global $mimeTypes;
    # say("mime $mimeTypes gl {$GLOBALS['mimeTypes']}" );
    $f = substr(__file__, 0, strlen(__file__) - 3) . 'jpg';
    header('Content-Type: ' . $mimeTypes['jpg']);
    # header('Content-Disposition: inline');
    # header('Content-Length: ' . filesize($f));
    readFile($f);
}

function readFilePdf() {
    global $mimeTypes;
    header('Content-Type: ' . $mimeTypes['pdf']);
    readFile(substr(__file__, 0, strlen(__file__) - 3) . 'pdf');
}

function embedImg() {
    $src = basename(__FILE__) . '?sub=readFileJpg';
    $src = '?sub=readFileJpg';
    echo "<a href='$src'>src = $src<br>";
    echo "<img src='$src' width='200px'></a>";
}

function phpDemo2() {
    say('phpDemo2 begin: undefined constant abc =' . abc . ', defined=' . defined('abc'), 1);
    say('phpDemo2 begin: undefined constant @abc =' . @abc . ', defined=' . defined('abc'));
    define('abc', 'defined value of constant abc');
    say('phpDemo2 end: after define constant abc =' . abc . ', defined=' . defined('abc'));
    define('abc', 'second define of constant abc');
    say('phpDemo2 end: after second define constant abc =' . abc . ', defined=' . defined('abc'), 2);
}

function assertSay($txt) {
    say("assert failed: $txt " . backtrace());
}

function say($tx, $fu=0) {
    global $trace;
    if ($fu==1)
        echo "<ol><li>$tx";
    elseif ($fu == 0)
        echo "</li><li>$tx";
    elseif ($fu == 2)
        echo "</li><li>$tx</li></ol>";
    elseif ($fu == 4)
        echo "$tx";
    elseif ($fu == 5)
        echo ", $tx";
    else
        echo "sayBadFun $fu $tx";
}

function backtrace($num=7) { // returns backtrace, html formatted
        ob_start();
        debug_print_backtrace(0 , $num);
        for ($tr = ob_get_contents(); substr($tr, strlen($tr)-strlen(PHP_EOL)) == PHP_EOL; $tr = substr($tr, 0, strlen($tr)-strlen(PHP_EOL))) {}
        ob_end_clean();
        return "<ul><li>" . strtr($tr, [PHP_EOL => "</li><li>"]) . "</li></ul>";
}

function highlightNum($fi) { # highlight file with lineNumbers
    $s = highlight_file($fi, true);
    $ln = 1;
    $cy = strpos($s, '<br />');
    if (false !== ($sx = strpos($s, '<span')) and false !== ($sy = strpos($s, '>', $sx)) and $sy < $cy) { # lineNo 1 after first span and possibly \n which must remain after first span
        $cx = $sy + 1 + (substr($s, $sy, 2) === ">\n");
        $r = substr($s, 0, $cx) . highlightNum1($ln) . substr($s, $cx, $cy + 6 -$cx);
        }
    else {
        $r = '*** code does not have a span berfore first &lt;br&gt;***' . substr($s, 0, $cy + 6);
        }
    
    while (FALSE !== ($cy = strpos($s, '<br />', $cx=$cy+6))) {
        $r .= highlightNum1(++$ln) . substr($s, $cx, $cy + 6 - $cx);
        } 
    return $r . substr($s, $cx);
}

function highlightNum1($ln) { # format one lineNo
    return '<span style="color: #ffffff; background-color: #a0e0a0;">' . strtr(sprintf('%6u  ', $ln), [' ' => '&nbsp;']) . '</span>&nbsp;&nbsp;';
}
?>

Source

<?php 
    if (isset($_POST['sub']) and $_POST['sub'] != '' )
        $fun = $_POST['sub'];
    elseif (isset($_GET['sub']) and $_GET['sub'] != '' )
        $fun = $_GET['sub'];
    else
        $fun = 'request_method=' . $_SERVER['REQUEST_METHOD'];
    if ( substr($fun, 0, 6) == 'readFi') {
        if ( function_exists($fun)) {
            makeMime();
            $fun();
        } else  {
?>

<h1>readFile only for <?php echo $fun . ' missing for ' . basename(__file__); ?> </h1>

<?php } } else { ?>

<form action="<?php echo basename(__file__); ?>" method="post">
<h2><?php echo basename(__file__); ?> readfile and mime types</h3>
<ol>
    <li> <input type="submit" name="sub" value="phpDemo2"/> define</li>
    <li> <input type="submit" name="sub" value="readFileJpg"/> readFile jpg</li>
    <li> <input type="submit" name="sub" value="readFilePdf"/> readFile pdf</li>
    <li> <input type="submit" name="sub" value="embedImg"/> readFile img</li>
</ol>
<?php 
    echo "<h2> demo: $fun </h2>";
    if (isset($_POST['sub']))
        $fun();
?>
</form>
<h2>info</h2>
<ul>
<?php
echo '<li>$_SERVER[REQUEST_METHOD] = ' . $_SERVER['REQUEST_METHOD'];
echo '</li><li> $_GET = '; print_r($_GET); 
echo '</li><li> $_POST = '; print_r($_POST); 
?> 
</li></ul>
<h2>Source with linenums</h2>
<?php echo highlightNum(__file__); ?>
<h2>Source</h2>
<?php highlight_file(__file__) ?>
<?php } ?>
<?php

function makeMime() { # just in global scope, does not work here, because assignment will be executed only after call of readFileJpg ....
    global $mimeTypes;
    $mimeTypes = [
        'pdf' => 'application/pdf',
        'txt' => 'text/plain',
        'html' => 'text/html',
        'exe' => 'application/octet-stream',
        'zip' => 'application/zip',
        'doc' => 'application/msword',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'gif' => 'image/gif',
        'png' => 'image/png',
        'jpeg' => 'image/jpg',
        'jpg' => 'image/jpg',
        'php' => 'text/plain'
    ];
}

function readFileJpg() {
    global $mimeTypes;
    # say("mime $mimeTypes gl {$GLOBALS['mimeTypes']}" );
    $f = substr(__file__, 0, strlen(__file__) - 3) . 'jpg';
    header('Content-Type: ' . $mimeTypes['jpg']);
    # header('Content-Disposition: inline');
    # header('Content-Length: ' . filesize($f));
    readFile($f);
}

function readFilePdf() {
    global $mimeTypes;
    header('Content-Type: ' . $mimeTypes['pdf']);
    readFile(substr(__file__, 0, strlen(__file__) - 3) . 'pdf');
}

function embedImg() {
    $src = basename(__FILE__) . '?sub=readFileJpg';
    $src = '?sub=readFileJpg';
    echo "<a href='$src'>src = $src<br>";
    echo "<img src='$src' width='200px'></a>";
}

function phpDemo2() {
    say('phpDemo2 begin: undefined constant abc =' . abc . ', defined=' . defined('abc'), 1);
    say('phpDemo2 begin: undefined constant @abc =' . @abc . ', defined=' . defined('abc'));
    define('abc', 'defined value of constant abc');
    say('phpDemo2 end: after define constant abc =' . abc . ', defined=' . defined('abc'));
    define('abc', 'second define of constant abc');
    say('phpDemo2 end: after second define constant abc =' . abc . ', defined=' . defined('abc'), 2);
}

function assertSay($txt) {
    say("assert failed: $txt " . backtrace());
}

function say($tx, $fu=0) {
    global $trace;
    if ($fu==1)
        echo "<ol><li>$tx";
    elseif ($fu == 0)
        echo "</li><li>$tx";
    elseif ($fu == 2)
        echo "</li><li>$tx</li></ol>";
    elseif ($fu == 4)
        echo "$tx";
    elseif ($fu == 5)
        echo ", $tx";
    else
        echo "sayBadFun $fu $tx";
}

function backtrace($num=7) { // returns backtrace, html formatted
        ob_start();
        debug_print_backtrace(0 , $num);
        for ($tr = ob_get_contents(); substr($tr, strlen($tr)-strlen(PHP_EOL)) == PHP_EOL; $tr = substr($tr, 0, strlen($tr)-strlen(PHP_EOL))) {}
        ob_end_clean();
        return "<ul><li>" . strtr($tr, [PHP_EOL => "</li><li>"]) . "</li></ul>";
}

function highlightNum($fi) { # highlight file with lineNumbers
    $s = highlight_file($fi, true);
    $ln = 1;
    $cy = strpos($s, '<br />');
    if (false !== ($sx = strpos($s, '<span')) and false !== ($sy = strpos($s, '>', $sx)) and $sy < $cy) { # lineNo 1 after first span and possibly \n which must remain after first span
        $cx = $sy + 1 + (substr($s, $sy, 2) === ">\n");
        $r = substr($s, 0, $cx) . highlightNum1($ln) . substr($s, $cx, $cy + 6 -$cx);
        }
    else {
        $r = '*** code does not have a span berfore first &lt;br&gt;***' . substr($s, 0, $cy + 6);
        }
    
    while (FALSE !== ($cy = strpos($s, '<br />', $cx=$cy+6))) {
        $r .= highlightNum1(++$ln) . substr($s, $cx, $cy + 6 - $cx);
        } 
    return $r . substr($s, $cx);
}

function highlightNum1($ln) { # format one lineNo
    return '<span style="color: #ffffff; background-color: #a0e0a0;">' . strtr(sprintf('%6u  ', $ln), [' ' => '&nbsp;']) . '</span>&nbsp;&nbsp;';
}
?>