1<?php
2/**
3* Libreria di funzioni per la creazione del layout di pagina dell'applicazione.
4*
5* @package VFront
6* @subpackage Function-Libraries
7* @author Mario Marcello Verona <marcelloverona@gmail.com>
8* @copyright 2007-2010 M.Marcello Verona
9* @version 0.96 $Id: layouts.php 940 2011-03-30 10:31:26Z marciuz $
10* @license http://www.gnu.org/licenses/gpl.html GNU Public License
11*/
12
13
14/**
15 * Scrive l'HTML di apertura di una pagina
16 *
17 * @param string $title Il titolo della pagina mostrato nel tag TITLE
18 * @param array $files Un array di files da includere. Possono essere javascript o css, la funzione li smister� automaticamente
19 * @param string $tipo Pu� essere ad esempio "sottomaschera". In tal caso il layout apparir� differente.
20 * @return string HTML
21 */
22function openLayout1($title,$files=array(),$tipo=''){
23
24	global $pagina,$tipo_nav, $vmsql, $vmreg;
25
26	$GLOBALS['layout_APERTO']=1;
27
28	$css = array();
29	$js  = array();
30
31// SMISTA I FILES CSS E JS MANDATI ALLA FUNZIONE
32	foreach($files as $file){
33
34		if(substr($file,-4,4)==".css"){
35			$css[]=$file;
36		}
37		elseif(substr($file,-3,3)==".js" || preg_match("|\.js\??|i",$file)){
38			$js[]=$file;
39		}
40
41	}
42
43
44$THEME=(isset($_SESSION['VF_VARS']['layout']) && is_dir(FRONT_ROOT."/themes/".$_SESSION['VF_VARS']['layout']."/"))
45	   ? $_SESSION['VF_VARS']['layout'] : 'default';
46
47if(function_exists('browser_detection')){
48
49	$mobile_test=browser_detection('mobile_test');
50}
51else{
52	include_once(FRONT_ROOT."/inc/func.browser_detection.php");
53	$mobile_test=browser_detection('mobile_test');
54}
55
56if($mobile_test){
57
58	$meta_mobile="\n=<meta name=\"viewport\" content=\"width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\" />\n";
59	$meta_mobile.="<meta name=\"format-detection\" content=\"telephone=no\" />\n";
60}
61else{
62	$meta_mobile='';
63}
64
65
66
67$OUT= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
68<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"it\">
69<head>
70<title>$title</title>
71<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".FRONT_ENCODING."\" />$meta_mobile
72<link rel=\"shortcut icon\" href=\"".FRONT_DOCROOT."/themes/$THEME/favicon.png\" type=\"image/x-icon\" />
73<style type=\"text/css\" media=\"all\">
74";
75
76
77
78
79
80	# CSS
81
82	if(isset($_SESSION['VF_VARS']['join_css']) && $_SESSION['VF_VARS']['join_css']==1){
83
84		$array_files[]=FRONT_DOCROOT."/sty/base.css";
85		$array_files[]=FRONT_DOCROOT."/themes/".$THEME."/".$THEME.".css";
86
87		if($tipo=="sottomaschera"){
88
89			$array_files[]= FRONT_DOCROOT."/sty/sottomaschera.css";
90		}
91
92		foreach($css as $css){
93			$array_files[]=FRONT_DOCROOT."/$css";
94		}
95
96		$OUT.= "@import \""._PATH_TMP_HTTP."/".file_cat($array_files,'css');
97	}
98
99	else{
100
101		# DEFAULT CSS
102			$OUT.= "@import \"".FRONT_DOCROOT."/sty/base.css\";\n";
103
104		# THEMES
105			$OUT.= "@import \"".FRONT_DOCROOT."/themes/".$THEME."/".$THEME.".css\";\n";
106
107
108			if($tipo=="sottomaschera"){
109
110				$OUT.= "@import \"".FRONT_DOCROOT."/sty/sottomaschera.css\";\n";
111			}
112
113
114		# CSS
115		foreach($css as $css){
116			$OUT.= "@import \"".FRONT_DOCROOT."/$css\";\n";
117		}
118
119	}
120
121$OUT.= "</style>\n";
122
123
124
125	// JS LANG
126
127	if(isset($_SESSION['VF_VARS']['lang']) && $_SESSION['VF_VARS']['lang']!=''){
128
129		$jslang = strtolower(substr($_SESSION['VF_VARS']['lang'],0,2));
130	}
131	else{
132
133		$jslang = strtolower(substr(FRONT_LANG,0,2));
134	}
135
136	$OUT.="<script type=\"text/javascript\" src=\"".FRONT_DOCROOT."/js/lang/vfront-".$jslang.".js\"></script>\n";
137
138
139
140
141
142	# JS
143	if(count($js)>0){
144
145		$cache_js=0;
146
147		###########################
148		// cache js
149
150		if($cache_js==1){
151
152			$cache_js_hash=md5(implode("",$js));
153
154			$cache_js_path=_PATH_TMP."/".$cache_js_hash.".js";
155			$cache_js_path_abs=_PATH_TMP_HTTP."/".$cache_js_hash.".js";
156
157			if(is_file($cache_js_path)){
158
159				$OUT.= "<script type=\"text/javascript\" src=\"$cache_js_path_abs\" ></script>\n";
160			}
161			else{
162
163				// create the file
164				$fpjs=fopen($cache_js_path,'a');
165
166				foreach($js as $jsf){
167
168					list($jsf)=explode("?",$jsf);
169
170					fwrite($fpjs,join("",file(FRONT_ROOT."/".$jsf))."\n");
171				}
172				fclose($fpjs);
173
174				$OUT.= "<script type=\"text/javascript\" src=\"$cache_js_path_abs\" ></script>\n";
175			}
176
177
178		} # end cache js
179		else{
180
181			foreach($js as $js){
182				$OUT.= "<script type=\"text/javascript\" src=\"".FRONT_DOCROOT."/$js\" ></script>\n";
183			}
184
185		}
186
187
188	}
189
190
191
192$OUT.= "
193</head>";
194
195
196
197
198
199
200// LAYOUT
201
202
203
204	if($tipo!="sottomaschera" && $tipo!='popup' && $tipo!='popup1'){
205
206
207		$OUT.= "<body>";
208
209		$OUTMENU =(isset($_SESSION['user']['livello'])) ?  menu($_SESSION['user']['livello']) : "";
210
211		$OUT.="<div id=\"header\"><span>&nbsp;</span>".$OUTMENU."</div>\n";
212
213	}
214	elseif ($tipo=='popup'){
215
216	    $OUT.= "<body class=\"popup\">";
217	    $OUT.="<div id=\"header\"><span>&nbsp;</span></div>\n";
218	}
219	elseif ($tipo=='popup1'){
220
221	    $OUT.= "<body class=\"popup1\">";
222	    $OUT.="<div id=\"header-popup\"></div>\n";
223	}
224	else{
225		$OUT.= "<body>";
226		$OUT.="<div id=\"header\"><span>&nbsp;</span></div>\n";
227	}
228
229
230
231
232
233$OUT.= "<div id=\"contenuto\">
234	<!--CONTENUTO-->
235	";
236
237
238
239	return $OUT;
240}
241
242
243
244
245
246/**
247 * Scrive l'HTML di chiusura di una pagina
248 *
249 * @param mixed $back
250 * @return string HTML
251 */
252function closeLayout1($back=""){
253
254	$OUTPUT_CLOSE="<!-- Fine contenuto-->
255	</div>";
256
257
258	if(isset($_SESSION['user'])){
259
260		if(!function_exists('gid2nome_gruppo')){
261			require(FRONT_REALPATH."/inc/func.comuni.php");
262		}
263
264		$nome_visualizzato= ($_SESSION['user']['nome']." ".$_SESSION['user']['cognome']!=' ') ? $_SESSION['user']['nome']." ".$_SESSION['user']['cognome'] : $_SESSION['user']['email'];
265
266		$OUTPUT_CLOSE.="<div id=\"identita\">"._("Logged in as")." <br /><a href=\"".FRONT_DOCROOT."/dati_personali.php\">".$nome_visualizzato."</a> ("._('group').":".gid2nome_gruppo($_SESSION['gid']).")</div>\n";
267	}
268
269	if($back!=""){
270		$OUTPUT_CLOSE.="<div id=\"back-dx\">&nbsp;</div>\n";
271
272	}
273
274
275	if($GLOBALS['DEBUG_SQL_SHOW_QUERY']===true){
276
277		if(!isset($GLOBALS['DEBUG_SQL_STRING'])){
278			$GLOBALS['DEBUG_SQL_STRING']=array();
279		}
280
281		$LISTA_SQL = str_replace(array("\n","\t")," ",implode("<br /><br />",$GLOBALS['DEBUG_SQL_STRING']));
282
283		$sum_time=0;
284
285		foreach($GLOBALS['DEBUG_SQL_STRING'] as $str){
286			list($ms,$null)=explode(" ---",$str,2);
287			$sum_time+= (double) $ms;
288		}
289
290		$OUTPUT_CLOSE.= "
291		<script type=\"text/javascript\">
292
293			/* <![CDATA[ */
294
295			var xdebug = window.open('', 'xdebug', 'width=800,height=600,toolbar=yes, location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes');
296			with (xdebug.document) {
297			  open('text/html', 'replace');
298			  write(\"<html><head><title>VFront DEBUG</title></head><body><code>"
299				.str_replace("&lt;br /&gt;","<br />",htmlentities(addslashes($LISTA_SQL), ENT_QUOTES, FRONT_ENCODING))
300				."<br /><br />"
301				."<strong>". count($GLOBALS['DEBUG_SQL_STRING'])."</strong> queries in <strong>".$sum_time."</strong> sec"
302				."</code></body></html>\");
303			  close();
304			}
305
306			/* ]]> */
307
308		</script>\n";
309	}
310
311	$OUTPUT_CLOSE.="\n</body>\n</html>";
312
313	return $OUTPUT_CLOSE;
314}
315
316/**
317 * Scrive l'HTML di chiusura di una pagina
318 *
319 * @return string HTML
320 */
321function closeLayout2(){
322
323	return "\n</div>\n</body>\n</html>";
324
325}
326
327
328/**
329 * Scrive l'HTML di chiusura di una pagina
330 *
331 * @param int $livello Livello di amministrazione dell'utente che ha fatto login
332 * @return string HTML
333 */
334function menu($livello=0){
335
336	$MENU=array();
337
338/*
339	if($livello>0){
340		$MENU[]="<a href=\"statistiche.php\">Statistiche</a>";
341
342	}
343*/
344	if($livello>0){
345		$MENU[]="<a href=\"".FRONT_DOCROOT."/\">"._('Home')."</a>";
346	}
347
348
349	if($livello>1){
350		$MENU[]="<a href=\"".FRONT_DOCROOT."/admin/\">"._('Administration')."</a>";
351	}
352
353
354	$MENU[]="<a href=\"".FRONT_DOCROOT."/index.php?logout\">"._('Logout')."</a>";
355
356	return "<ul><li>".implode("</li><li>", $MENU)."</li></ul>";
357
358}
359
360
361
362/**
363 * Funzione di gestione a video degli errori.
364 * Se lo script in azione va in errore � possibile interrompere lo script e generare l'apertura
365 * di un errore visibile su pagina.
366 * La funzione mostra un errore generico a video e manda un'email all'amministratore ed
367 * allo sviluppatore con il dettaglio dell'errore.
368 *
369 * @param string $tipo_query
370 * @param string $messaggio_completo
371 */
372function openError($err){
373
374
375	// Elimina l'output generato nel contenuto di pagina e vai oltre...
376	if(isset($GLOBALS['layout_APERTO']) && $GLOBALS['layout_APERTO']==1)
377	ob_clean();
378
379	$msg='';
380
381	foreach ($err as $k=>$v){
382
383		$msg.=$k.": $v\n";
384	}
385
386
387	if(isset($_SESSION['VF_VARS']['send_debug_email'])){
388
389		// manda una email per il debug
390		@mail(_SYS_ADMIN_MAIL.","._DEV_MAIL,"["._NOME_PROJ." DB] "._('DB error in'),$msg);
391	}
392
393
394	$OUT= openLayout1(_("Database query problem"));
395	$OUT.= "<h1 class=\"var\">"._('Database query problem')."</h1>\n";
396
397	$OUT.= "<p>"._("The operation generated an anomaly.")."<br />
398	"._("If this event persists, contact")."
399	<a href=\"mailto:"._SYS_ADMIN_MAIL."\">"._("the system administrator")."</a>.<br />
400	"._("We apologise for the inconvenience.")."</p>
401	<p><a href=\"index.php\">"._("Back to home")."</a></p>\n";
402
403	if(function_exists('is_admin') && is_admin()){
404
405		$OUT.= "<code>".nl2br($msg)."</code>\n";
406		$OUT.= "<p>"._('<em>Note</em>: this debug message is showed only because you\'re admin!')."</p>\n";
407
408	}
409
410
411	$OUT.= closeLayout1();
412
413	print $OUT;
414
415	exit;
416
417}
418
419
420
421/**
422 * Funzione che richiama un errore esplicitamante
423 * Pu� essere richiamata nel codice dove si voglia.
424 * In caso di condizioni critiche si pu� scegliere di mandare una email all'amministratore di sistema (di default=true)
425 * E' possibile inoltre impostare un testo specifico, in caso contrario verr� stampato il messaggio di default
426 *
427 * @param string $messaggio
428 * @param bool $email
429 * @param string $testo_custom
430 */
431function openErrorGenerico($messaggio,$email=true, $testo_custom=""){
432
433
434	// Elimina l'output generato nel contenuto di pagina e vai oltre...
435	if(isset($GLOBALS['layout_APERTO']) && $GLOBALS['layout_APERTO']==1)
436	ob_clean();
437
438	if($email){
439		$testo=sprintf(_("This problem occurred on %s"),date("d/m/Y \a\l\l\e H:i:s")).": $messaggio\n".
440			   _("on server ").$_SERVER['HTTP_HOST']." (".$_SERVER['REMOTE_ADDR'].")\n";
441
442		// manda una email per il debug
443		mail(_SYS_ADMIN_MAIL.","._DEV_MAIL,"["._NOME_PROJ." Error] $messaggio",$testo);
444	}
445
446	echo openLayout1($messaggio, array("sty/base.css"));
447	echo "<h1 class=\"var\">$messaggio</h1>\n";
448
449	if($testo_custom==""){
450		echo "<p>"._("The operation generated an anomaly.")."<br />
451		"._("If this event persists, contact")."
452		<a href=\"mailto:"._SYS_ADMIN_MAIL."\">"._("the system administrator")."</a>.<br />
453		"._("We apologise for the inconvenience.")."</p>
454		<p><a href=\"index.php\">"._("Back to home")."</a></p>\n";
455	}
456	else{
457
458		echo "<p>".$testo_custom."</p>
459		<p><a href=\"".FRONT_DOCROOT."/index.php\">"._("Back to home")."</a></p>\n";
460	}
461
462
463
464	echo closeLayout1();
465
466	exit;
467
468}
469
470
471
472
473function file_cat($array_files,$type='css'){
474
475	$file_cat[$type]='';
476
477	for($i=0;$i<count($array_files);$i++){
478
479		$file_cat[$type].=file_get_contents($array_files[$i]);
480	}
481
482	$file_cat[$type]=preg_replace("/(\n)+|(\t)+| +/si",' ',$file_cat[$type]);
483
484	$hash=md5($file_cat[$type]);
485
486	$fn=_PATH_TMP."/".$hash.".$type";
487
488	if(!file_exists($fn)){
489		$fp=fopen($fn,'w');
490		fwrite($fp,$file_cat[$type]);
491		fclose($fp);
492	}
493
494	return $hash.".".$type;
495}
496
497
498
499
500
501function breadcrumbs($array,$separator=" &raquo; "){
502
503	$OUT="<div id=\"briciole\">";
504
505	$c=1;
506
507	foreach($array as $url=>$name){
508
509		if($c>1) $OUT.=$separator;
510
511		if($name=='HOME'){
512
513			$OUT.="<a href=\"".FRONT_DOCROOT."/\">"._('home')."</a>";
514		}
515		else if($name=='ADMIN'){
516
517			$OUT.= ($c==count($array)) ? _('administration')
518				: "<a href=\"".FRONT_DOCROOT."/admin/\">"._('administration')."</a>";
519		}
520		else{
521			$OUT.=($c==count($array)) ? strtolower($name) : "<a href=\"$url\">".strtolower($name)."</a>";
522		}
523
524		$c++;
525	}
526
527	$OUT.="</div>\n";
528
529	return $OUT;
530
531}
532
533
534
535
536?>