Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
5.13% covered (danger)
5.13%
2 / 39
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
iaErrorReporterDisplay
5.13% covered (danger)
5.13%
2 / 39
0.00% covered (danger)
0.00%
0 / 5
697.46
0.00% covered (danger)
0.00%
0 / 1
 reportErrors
18.18% covered (danger)
18.18%
2 / 11
0.00% covered (danger)
0.00%
0 / 1
43.05
 pageDisplayErrors
n/a
0 / 0
n/a
0 / 0
4
 emailSend
n/a
0 / 0
n/a
0 / 0
3
 ramUsage
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 timeUsage
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 bytes2units
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 milliSecondsFormat
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 *
4 *
5 * Display all errors and/or email new or thought fixed errors collected by iacErrorReporter
6 *
7 */
8class iaErrorReporterDisplay {
9
10    /**
11     * report errors to screen
12     *
13     * @param bool $display
14     * @param string $emailTo
15     * @param string $html
16     * @param array $count
17     * @param string $info
18     * @return void
19     */
20    public function reportErrors($display, $emailTo, $html, $count, $info) {
21        if($display) {
22            $this->pageDisplayErrors($html,$count['Bug'] + $count['newbug'] +$count['Fixed'] , $info);
23            global $gDebugging;
24            if($gDebugging || usuarioTipoRony()) {
25                try {
26                    if(file_exists("../uploads/fragmentLastModified.html")) {
27                        echo file_get_contents("../uploads/fragmentLastModified.html");
28                        echo file_get_contents("../uploads/last");
29                        echo "</pre></div></details>";
30                    }
31                } catch(Throwable) {echo "err";}
32                echo usedFilesTimes();
33            }
34        }
35        if( !empty($emailTo) && ($count['newbug'] + $count['Fixed']) > 0) {
36            $this->emailSend($emailTo, $html,$count);
37        }
38
39    }
40
41    /**
42     * Display errors in html
43     *
44     *
45     * @codeCoverageIgnore
46     *
47     * @param string $html
48     * @param int $count total number of errors
49     * @param string $info additional html to output
50     * @return void
51     */
52    protected function pageDisplayErrors($html,$count,$info) {
53        global $gDebugging;
54        if(!usuarioTipoRony() && !$gDebugging) $info='';
55        $style = $count == 0 ? '' : ' style="color:red" ';
56        echo "
57<script>
58function iacHtmlDetialsClick(e) {e.nextElementSibling.style.display=(e.nextElementSibling.style.display=='none' ? 'block':'none');e.firstElementChild.innerHTML=(e.nextElementSibling.style.display=='none' ? '&#8680;' : '&#8681;');}
59</script>
60<div class='iacDetailsSeparator np lazy-content'></div><div class='iacDetailsByDiv np' style='margin:1em'>
61<span style='cursor:pointer;color:silver;background-color:white;' title='Click to view' onclick=\"iacHtmlDetialsClick(this);\">
62<b>&#8680;</b> <span$style>Page Info ($count)</span>
63</span>
64<div style='display:none;border:1px silver solid;background-color:white;padding:0 1em 1em 1em;margin:0 1em auto 1em;'>
65".$this->ramUsage().$this->timeUsage()."<ol style='color:red;'>$html</ol>$info
66</div></div>
67";
68    }
69
70    /**
71     * Send email with errors
72     *
73     * @codeCoverageIgnore
74     *
75     * @param string $error
76     * @param string $html
77     * @param array $count
78     * @return void
79     */
80    protected function emailSend($emailTo, $html, $count) {
81
82       $subject = $_SERVER["HTTP_HOST"]." iac detected Errors.";
83       if($count['Fixed']>0)
84        $subject.=" $count[Fixed] Unfixed errors.";
85       if($count['newbug']>0)
86        $subject.=" $count[newbug] New errors.";
87
88       @mail($emailTo,
89            $subject,
90            "<html><body><p>IacErrorReporter reporting:<p><ul><li>".$_SERVER["HTTP_HOST"]."<li>".PHP_VERSION."<li>".php_uname().
91                "<li><ul style='color:red;'>".$html."</ul></ul>".$this->ramUsage().$this->timeUsage()."</body></html>"
92        );
93    }
94
95    /**
96     * Reports memory usage
97     *
98     * @return string a div with memory usage information
99     */
100    protected function ramUsage() {
101        return "<div style='font-family:courier;margin-left:16px;'>"
102            ."Used: ".self::bytes2units( memory_get_usage() )." Real: ".self::bytes2units( memory_get_usage(true) )
103            ." / "
104            ."Peak Used: ".self::bytes2units( memory_get_peak_usage() )." Real: ".self::bytes2units( memory_get_peak_usage(true) )
105            ."</div>"
106            ;
107    }
108
109    /**
110     * Reports current timing from $_SERVER['REQUEST_TIME'] to current time
111     *
112     * @return string html code with timing information
113     */
114    protected function timeUsage() {
115        if(!array_key_exists('REQUEST_TIME',$_SERVER)) {
116            return '';
117        }
118        return "<div style='font-family:courier,serif;margin-left:16px;'>"."Total: ".self::milliSecondsFormat(abs( microtime(true) - $_SERVER['REQUEST_TIME'] ))."</div>";
119    }
120
121
122    /**
123     * Format bytes into readable units Kb, Mb,..
124     *
125     * @param int $bytes
126     * @return string
127     */
128    protected static function bytes2units($bytes) {
129        if(empty($bytes) || !is_numeric($bytes))
130            return $bytes;
131        if($bytes < 0) {
132            $signo = '-';
133            $bytes *= -1;
134        } else
135            $signo = '';
136        if($bytes < 1024) {
137            $decs = 0;
138            $punto = '';
139        } else {
140            $decs = 2;
141            $punto = '.';
142        }
143        $unit =['b', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb'];
144        return $signo.number_format($bytes/pow(1024,($i=floor(log($bytes,1024)))),$decs,$punto,',').' '.$unit[$i];
145    }
146
147    /**
148     * Formats milliseconds strings with time units
149     *
150     * @param number $t0 the number of milliseconds to format
151     * @return string formatted lapsed time in milliseconds
152     */
153    protected static function milliSecondsFormat($t0) {
154        $t0 =  (float)$t0;
155        if(empty($t0) || !is_numeric($t0)) {
156            $t0 = 0;
157        }
158        if($t0 < 0) {
159            $t0 = -1 * $t0;
160        }
161        if($t0 >= 60*60*24) {
162            return ">= 1 day not supported"; //@TODO milliSecondsFormat support 1 day adnd more
163        }
164        return trim( str_replace(
165             array('0 h','00 min','00 sec',' 01',' 02',' 03',' 04',' 05',' 06',' 07',' 08',' 09')
166             ,array('','',''               ,' 1',' 2',' 3',' 4',' 5',' 6',' 7',' 8',' 9')
167            ,gmDate(' G \h i \m\i\n s \s\e\c '.(round($t0-floor($t0),3)*1000).' \m\s',floor($t0) )
168        ));
169    }
170}
171