GlisWeb framework
_output.tools.php
Vai alla documentazione di questo file.
1 <?php
2 
15  // costanti HTML
16  define( 'HTML_EOL' , '<br>' . PHP_EOL );
17  define( 'HTML_2EOL' , '<br>' . HTML_EOL );
18 
19  // costanti XHTML
20  define( 'XHTML_EOL' , '<\br>' . PHP_EOL );
21  define( 'XHTML_2EOL' , '<\br>' . XHTML_EOL );
22 
23  // costanti per il contenuto
24  define( 'MIME_TEXT_PLAIN' , 'text/plain' );
25  define( 'MIME_TEXT_HTML' , 'text/html' );
26  define( 'MIME_APPLICATION_JSON' , 'application/json' );
27  define( 'MIME_APPLICATION_XML' , 'application/xml' );
28  define( 'MIME_MULTIPART_FORM_DATA' , 'multipart/form-data' );
29 
30  // costanti per l'encoding
31  define( 'ENCODING_UTF8' , 'utf-8' );
32 
38  function buildJson( $content , $encoding = ENCODING_UTF8 , $headers = array() ) {
39 
40  // generazione del contenuto
41  $json = json_encode( string2utf8( $content ) );
42 
43  // log
44  if( ! empty( json_last_error() ) ) { logWrite( 'errore #'.json_last_error().' '.json_last_error_msg(), 'json', LOG_ERR ); }
45 
46  // genero l'output
47  build( $json , MIME_APPLICATION_JSON , $encoding , $headers );
48 
49  }
50 
51 
52 
58  function buildXml( $content , $encoding = ENCODING_UTF8 , $headers = array() ) {
59 
60  // genero l'output
61  build( $content , MIME_APPLICATION_XML , $encoding , $headers );
62 
63  }
64 
70  function buildText( $content , $encoding = ENCODING_UTF8 , $headers = array() ) {
71 
72  // genero l'output
73  build( $content , MIME_TEXT_PLAIN , $encoding , $headers );
74 
75  }
76 
82  function dieText( $content ) {
83 
84  header( 'Content-type: text/plain;' );
85  die( $content );
86 
87  }
88 
97  function buildHTML( $content , $encoding = ENCODING_UTF8 , $headers = array() ) {
98 
99  // preparazione del documento
100  $dom = new DOMImplementation;
101  $doctype = $dom->createDocumentType("html", "-//W3C//DTD HTML 4.01//EN", "http://www.w3.org/TR/html4/strict.dtd");
102  $document = $dom->createDocument( NULL, 'html', $doctype );
103 
104  $document->preserveWhiteSpace = false;
105  $document->formatOutput = true;
106 
107  $html = $document->documentElement;
108  $head = $document->createElement('head');
109  $title = $document->createElement('title');
110  $text = $document->createTextNode('documento generato '.date('r'));
111  $body = $document->createElement('body');
112 
113  $contentFragment = $document->createDocumentFragment();
114  $contentFragment->appendXML( $content );
115 
116  $title->appendChild( $text );
117  $head->appendChild( $title );
118  $html->appendChild( $head );
119  $html->appendChild( $body );
120  $body->appendChild( $contentFragment );
121 
122  // genero l'output
123  build( urldecode( $document->saveHTML() ) , MIME_TEXT_HTML , $encoding , $headers );
124 
125  }
126 
132  function build( $content , $type = MIME_TEXT_PLAIN , $encoding = ENCODING_UTF8 , $headers = array() ) {
133 
134  // invio gli headers
136 
137  // invio l'header per il contenuto
138  buildContentHeader( $type, $encoding );
139 
140  // invio l'output
141  echo $content;
142 
143  }
144 
150  function buildHeaders( $headers ) {
151 
152  // invio gli headers
153  foreach( $headers as $header ) {
154  header( $header );
155  }
156 
157  }
158 
165 
166  // invio gli headers
167  buildHeaders( array( 'Content-Type: ' . $t . '; charset=' . $e ) );
168 
169  }
170 
171 ?>
dieText( $content)
buildContentHeader( $t=MIME_TEXT_PLAIN, $e=ENCODING_UTF8)
string2utf8( $mixed)
buildHeaders( $headers)
logWrite( $m, $f='site', $l=LOG_NOTICE, $d=DIRECTORY_LOG, $t=CURRENT_LOG_LEVEL, $s=SITE_STATUS)
scrive un messaggio nei log del sito
Definition: _log.utils.php:48
const XHTML_EOL
const ENCODING_UTF8
const MIME_APPLICATION_XML
buildXml( $content, $encoding=ENCODING_UTF8, $headers=array())
$e
Definition: _slack.php:121
buildHTML( $content, $encoding=ENCODING_UTF8, $headers=array())
buildJson( $content, $encoding=ENCODING_UTF8, $headers=array())
const MIME_TEXT_PLAIN
buildText( $content, $encoding=ENCODING_UTF8, $headers=array())
const MIME_APPLICATION_JSON
const MIME_TEXT_HTML
build( $content, $type=MIME_TEXT_PLAIN, $encoding=ENCODING_UTF8, $headers=array())
const HTML_EOL