Source for file html.php

Documentation is available at html.php

  1. <?php
  2. /**
  3.  * Tools for HTML generation
  4.  *
  5.  * @author    Jean-Philippe Brunon <jp75018@free.fr>
  6.  * @copyright    2007-2009 Jean-Philippe Brunon
  7.  * @license    http://www.opensource.org/licenses/gpl-license.php GPL
  8.  * @package    php-endurance
  9.  * @version    $Id: html.php 68 2009-03-28 22:57:30Z jp75018 $
  10.  */
  11.  
  12. /**
  13.  * General configuration file
  14.  */
  15. require_once ('conf/config.php');
  16.  
  17. /**
  18.  * Output is language dependant
  19.  */
  20. require_once ('lang/strings_' $GLOBALS['lang''.inc.php');
  21.  
  22. /**
  23.  * Display HTML page header
  24.  *
  25.  * @param       string  $title Page title
  26.  * @param       array $options Associative array of options :
  27.  *         - 'ajax_tips' : true to use Ajax tips, else false (default)
  28.  *         - 'show_menu' : true to display user menu (default), else false
  29.  *           (only if user logged in)
  30.  *         - 'hide_home' : true to hide home button, else false (default)
  31.  *         - 'keywords' : META tag string : List of keywords
  32.  *         - 'description' : META tag string : Page description
  33.  *         - 'onload' : Java script to call after loading HTML page
  34.  *
  35.  * @return      void 
  36.  */
  37. function html_page_header($title$options null)
  38. {
  39.   header(sprintf("Content-type: text/html; Charset=%s"$GLOBALS['charset']));
  40.  
  41.   echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
  42.   printf("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"%s\">\n",
  43.     $GLOBALS['language']);
  44.   echo "<head>\n";
  45.   printf("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\" />\n",
  46.     $GLOBALS['charset']);
  47.   printf("<meta http-equiv=\"content-language\" content=\"%s\" />\n",
  48.     $GLOBALS['language']);
  49.   if ($options['keywords'])
  50.   {
  51.     printf("<meta http-equiv=\"keywords\" content=\"%s\" />\n",
  52.       str_replace('"''\''$options['keywords']));
  53.   }
  54.   if ($options['description'])
  55.   {
  56.     printf("<meta http-equiv=\"description\" content=\"%s\" />\n",
  57.       str_replace('"''\''$options['description']));
  58.   }
  59.   echo "<link rel=\"SHORTCUT ICON\" href=\"favicon.ico\" />\n";
  60.   echo "<title>$title</title>\n";
  61.  
  62.   $css $GLOBALS['css_default_file'];
  63.   echo "<link href=\"css/$css\" rel=\"stylesheet\" type=\"text/css\" />\n";
  64.  
  65.   echo "<script src=\"js/tools.js\" type=\"text/javascript\"></script>\n";
  66.   if ($options['ajax_tips'])
  67.   {
  68.     echo "<script src=\"js/ajax_functions.js\" type=\"text/javascript\"></script>\n";
  69.   }
  70.   echo "</head>\n";
  71.  
  72.   $onload_list array('setBrowserWidthCookie()');
  73.   if ($options['ajax_tips'])
  74.   array_push($onload_list'Tooltip.init()')}
  75.   if ($options['onload'])
  76.   array_push($onload_list$options['onload'])}
  77.   printf("<body onload=\"%s\">\n"implode('; '$onload_list));
  78.  
  79. // Display menu
  80.   if ($options['show_menu'!== false)
  81.   echo html_main_menu($options['ajax_tips']$options['hide_home'])}
  82. }
  83.  
  84. /**
  85.  * Display HTML page footer
  86.  *
  87.  * @param       boolean    $ajax_tips true to enable Ajax tips, else false
  88.  * @param       boolean    $hide_home true to hide home button, else false
  89.  * @return      string    Buffer containing HTML menu
  90.  */
  91. function html_main_menu($ajax_tips$hide_home)
  92. {
  93.   $buffer sprintf("<table class=\"menu_table%s\"><tr>",
  94.     $_SESSION['my_user_id''_logged' '');
  95.  
  96. // Home button if not hidden
  97.   if ($hide_home)
  98.   $buffer .= sprintf("<td  width=\"%d\">&nbsp;</td>"ICON_LARGE_SIZE 20)}
  99.   else
  100.   {
  101.     $tip '';
  102.     if ($ajax_tips)
  103.     {
  104.       $tip sprintf(" onmouseover=\"doAjaxText('tip_picto',event,'%s' )\" onmouseout=\"closeAjax()\"",
  105.     str_replace('\'''\\\''ucfirst($GLOBALS['str_home'])));
  106.     }
  107.     $buffer .= sprintf("<td width=\"%d\"><a href=\"home.php\"><img src=\"img/home.png\" border=\"0\" width=\"%d\" height=\"%d\"%s /></a></td>",
  108.       ICON_LARGE_SIZE 20ICON_LARGE_SIZEICON_LARGE_SIZE$tip);
  109.   }
  110.  
  111.   if ($_SESSION['my_user_id'])
  112.   {
  113. // Welcome message or nickname (if logged in)
  114.     if ($_POST['login'])
  115.     {
  116.     // Just logged in
  117.       $user_text sprintf($GLOBALS['str_welcome_message'],
  118.     $_SESSION['user_firstname'?
  119.       $_SESSION['user_firstname'$_SESSION['user_nickname']);
  120.     }
  121.     else
  122.     $user_text $_SESSION['user_nickname']}
  123.     $buffer .= sprintf("<td align=\"center\" width=\"%d\"><b>%s</b></td>",
  124.       200$user_text);
  125.  
  126. // Calendar button (if logged in)
  127.     $tip '';
  128.     if ($ajax_tips)
  129.     {
  130.       $tip sprintf(" onmouseover=\"doAjaxText('tip_picto',event,'%s' )\" onmouseout=\"closeAjax()\"",
  131.     str_replace('\'''\\\''$GLOBALS['str_my_training_booknote']));
  132.     }
  133.     $buffer .= sprintf("<td width=\"%d\"><a href=\"calendar.php?uid=%d\"><img src=\"img/calendar.png\" border=\"0\" width=\"%d\" height=\"%d\"%s /></a></td>",
  134.       ICON_LARGE_SIZE 20$_SESSION['my_user_id'],
  135.       ICON_LARGE_SIZEICON_LARGE_SIZE$tip);
  136.  
  137. // Upload button (if logged in)
  138.     $tip '';
  139.     if ($ajax_tips)
  140.     {
  141.       $tip sprintf(" onmouseover=\"doAjaxText('tip_picto',event,'%s' )\" onmouseout=\"closeAjax()\"",
  142.     str_replace('\'''\\\''$GLOBALS['str_upload_files']));
  143.     }
  144.     $buffer .= sprintf("<td width=\"%d\"><a href=\"upload.php\"><img src=\"img/upload.png\" border=\"0\" width=\"%d\" height=\"%d\"%s /></a></td>",
  145.       ICON_LARGE_SIZE 20ICON_LARGE_SIZEICON_LARGE_SIZE$tip);
  146.  
  147. // Data manager button (if logged in)
  148.     $tip '';
  149.     if ($ajax_tips)
  150.     {
  151.       $tip sprintf(" onmouseover=\"doAjaxText('tip_picto',event,'%s' )\" onmouseout=\"closeAjax()\"",
  152.     str_replace('\'''\\\''$GLOBALS['str_data_manager']));
  153.     }
  154.     $buffer .= sprintf("<td width=\"%d\"><a href=\"data_manager.php\"><img src=\"img/data_mgr.png\" border=\"0\" width=\"%d\" height=\"%d\"%s /></a></td>",
  155.       ICON_LARGE_SIZE 20ICON_LARGE_SIZEICON_LARGE_SIZE$tip);
  156.  
  157. // User profile button (if logged in)
  158.     $tip '';
  159.     if ($ajax_tips)
  160.     {
  161.       $tip sprintf(" onmouseover=\"doAjaxText('tip_picto',event,'%s' )\" onmouseout=\"closeAjax()\"",
  162.     str_replace('\'''\\\''$GLOBALS['str_my_profile_and_pref']));
  163.     }
  164.     $buffer .= sprintf("<td width=\"%d\"><a href=\"user_profile.php\"><img src=\"img/profile.png\" border=\"0\" width=\"%d\" height=\"%d\"%s /></a></td>",
  165.       ICON_LARGE_SIZE 20ICON_LARGE_SIZEICON_LARGE_SIZE$tip);
  166.   }
  167.  
  168. // Fill remaining horizontal space
  169.   $buffer .= sprintf("<td align=\"center\"><b>%s</b></td>"PACKAGE_NAME);
  170.  
  171. // Language select / flag (only if > 1 language available)
  172.   $lang_tab explode(','$GLOBALS['all_lang']);
  173.   if (count($lang_tab1)
  174.   {
  175.     $select_opts '';
  176.     for ($i 0$i count($lang_tab)$i++)
  177.     {
  178.       $lang strtolower(trim($lang_tab[$i]));
  179.       $select_opts .= sprintf("<option value=\"%s\"%s>%s</option>",
  180.     $lang($lang == $_SESSION['lang']' selected' ''$lang);
  181.     }
  182.     $buffer .= sprintf("<td align=\"right\" width=\"200\"><form method=\"post\" action=\"\"><select name=\"lang\" onchange=\"submit();\">%s</select></form></td>",
  183.       $select_opts);
  184.     $tip '';
  185.     if ($ajax_tips)
  186.     {
  187.       $tip sprintf(" onmouseover=\"doAjaxText('tip_picto',event,'%s' )\" onmouseout=\"closeAjax()\"",
  188.     str_replace('\'''\\\'',
  189.       ucfirst($GLOBALS['val_lang'][$GLOBALS['lang']])));
  190.     }
  191.     $buffer .= sprintf("<td width=\"%d\"><img src=\"img/lang_%s.png\" width=\"%d\" height=\"%d\"%s /></td>",
  192.       ICON_LARGE_SIZE$GLOBALS['lang'],
  193.       ICON_LARGE_SIZEICON_LARGE_SIZE$tip);
  194.   }
  195.   else
  196.   $buffer .= sprintf("<td width=\"%d\">&nbsp;</td>"ICON_LARGE_SIZE 200)}
  197.  
  198. // About button
  199.   $tip '';
  200.   if ($ajax_tips)
  201.   {
  202.     $tip sprintf(" onmouseover=\"doAjaxText('tip_picto',event,'%s' )\" onmouseout=\"closeAjax();\"",
  203.       str_replace('\'''\\\''sprintf($GLOBALS['str_about_package'],
  204.     PACKAGE_FULLNAME_SVN)) '<br /><br />' .
  205.       str_replace(array('\'',"\n")array('\\\'','<br />'),
  206.     sprintf($GLOBALS['str_about_package_text'],
  207.   }
  208.   $buffer .= sprintf("<td align=\"right\" width=\"%d\"><a href=\"http://%s\" target=\"phpendurance_org\"><img src=\"img/about.png\" border=\"0\" width=\"%d\" height=\"%d\"%s /></a></td>",
  209.  
  210.   if ($_SESSION['my_user_id'])
  211.   {
  212. // Disconnect button (if logged in)
  213.     $buffer .= sprintf("<td align=\"right\" width=\"%d\">%s</td>",
  214.       ICON_LARGE_SIZE 20html_logout_form('home.php'$ajax_tips));
  215.   }
  216.  
  217.   $buffer .= "</tr></table>\n";
  218.  
  219.   return $buffer;
  220. }
  221.  
  222. /**
  223.  * Display HTML page footer
  224.  *
  225.  * @param       integer    $break_lines Number of <br /> to display to avoid
  226.  *             scroll problem with tips
  227.  * @param       boolean    $footer true to display footer content, else false
  228.  * @return      void 
  229.  */
  230. function html_page_footer($break_lines 0$footer true)
  231. {
  232.   if ($footer)
  233.   {
  234.   // Contact webmaster by email
  235.     if ($GLOBALS['webmaster_email'])
  236.     {
  237.       printf("<hr /><a href=\"mailto:%s\">%s</a>"$GLOBALS['webmaster_email'],
  238.     $GLOBALS['str_email_webmaster']);
  239.     }
  240.  
  241.     for ($i 0$i $break_lines$i++)
  242.     echo '<br />'}
  243.   }
  244.  
  245.   echo "</body>\n";
  246.   echo "</html>\n";
  247. }
  248.  
  249. /**
  250.  * Generate HTML login form
  251.  *
  252.  * @param    string    $action Page to navigate to (default : current page)
  253.  * @param    boolean    $ajax_tips true to use Ajax tips, else false (default)
  254.  * @param    string    $email To fill email input value instead of
  255.  *             $_POST['email'] if not null
  256.  * @param    string    $password To fill password input value if not null
  257.  * @return    string Buffer containing HTML
  258.  */
  259. function html_login_form($action ''$ajax_tips false,
  260.   $email null$password null)
  261. {
  262.   $tip '';
  263.   $buffer "<form method=\"post\" action=\"$action\">\n";
  264.   $buffer .= "<input type=\"hidden\" name=\"login\" value=\"1\" />\n";
  265.   $buffer .= "<table class=\"login_table\">\n";
  266.   $buffer .= sprintf("<tr><td>%s&nbsp;:</td><td><input type=\"text\" size=\"30\" name=\"email\" value=\"%s\" tabindex=\"1\" /></td><td rowspan=\"2\">\n",
  267.     ucfirst($GLOBALS['str_email'])$email $email $_POST['email']);
  268.   if ($ajax_tips)
  269.   {
  270.     $tip sprintf(" onmouseover=\"doAjaxText('tip_picto',event,'%s' )\" onmouseout=\"closeAjax()\" onclick=\"submit();\"",
  271.       str_replace('\'''\\\''ucfirst($GLOBALS['str_login'])));
  272.   }
  273.   $buffer .= sprintf("<input type=\"image\" name=\"login\" src=\"img/login.png\" width=\"%d\" height=\"%d\"%s tabindex=\"3\" />\n",
  274.     ICON_LARGE_SIZEICON_LARGE_SIZE$tip);
  275.   $buffer .= sprintf("</td></tr>\n<tr><td>%s&nbsp;:</td><td><input type=\"password\" size=\"30\" name=\"password\" value=\"%s\" tabindex=\"2\" /></td></tr>\n",
  276.     ucfirst($GLOBALS['str_password'])$password);
  277.   $buffer .= sprintf("<tr><td align=\"right\"><input type=\"checkbox\" name=\"email_cookie\" value=\"1\"%s /></td><td colspan=\"2\">%s</td></tr>\n",
  278.     isset($_COOKIE['email']' checked' '',
  279.     $GLOBALS['str_remember_my_email']);
  280.   $buffer .= sprintf("<tr><td align=\"right\"><input type=\"checkbox\" name=\"password_cookie\" value=\"1\"%s /></td><td colspan=\"2\">%s</td></tr>\n",
  281.     isset($_COOKIE['password']' checked' '',
  282.     $GLOBALS['str_remember_my_password']);
  283.   $buffer .= "</table>\n";
  284.   $buffer .= "</form>\n";
  285.  
  286.   return $buffer;
  287. }
  288.  
  289. /**
  290.  * Generate HTML logout form
  291.  *
  292.  * @param    string    $action Page to navigate to (default : current page)
  293.  * @param    boolean    $ajax_tips true to use Ajax tips, else false (default)
  294.  * @return    string Buffer containing HTML
  295.  */
  296. function html_logout_form($action ''$ajax_tips false)
  297. {
  298.   $tip '';
  299.   $buffer "<form method=\"post\" action=\"$action\">\n";
  300.   $buffer .= "<input type=\"hidden\" name=\"logout\" value=\"1\" />\n";
  301.   if ($ajax_tips)
  302.   {
  303.     $tip sprintf(" onmouseover=\"doAjaxText('tip_picto',event,'%s' )\" onmouseout=\"closeAjax()\" onclick=\"submit();\"",
  304.       str_replace('\'''\\\''ucfirst($GLOBALS['str_logout'])));
  305.   }
  306.   $buffer .= sprintf("<input type=\"image\" name=\"logout\" src=\"img/logout.png\" width=\"%d\" height=\"%d\"%s />\n",
  307.     ICON_LARGE_SIZEICON_LARGE_SIZE$tip);
  308.   $buffer .= "</form>";
  309.  
  310.   return $buffer;
  311. }
  312.  
  313. /**
  314.  * Format duration string
  315.  *
  316.  * Output format is : [[<h>h]<m>']<s>"<t>
  317.  *
  318.  * @param    integer    $elapsed_10 Duration in seconds * 10
  319.  * @return    string Duration formatted string
  320.  */
  321. function format_duration($elapsed_10)
  322. {
  323.   $elapsed floor($elapsed_10 10);
  324.   if ($elapsed >= 3600)
  325.   {
  326.     $str_elapsed sprintf("%dh%02d'%02d\"",
  327.       floor($elapsed/3600)floor($elapsed/6060$elapsed%60);
  328.   }
  329.   else
  330.   {
  331.     if ($elapsed >= 60)
  332.     $str_elapsed sprintf("%d'%02d\""floor($elapsed/60)$elapsed%60)}
  333.     else
  334.     $str_elapsed sprintf("%02d\""$elapsed)}
  335.   }
  336.   $str_elapsed .= $elapsed_10 10;
  337.  
  338.   return $str_elapsed;
  339. }
  340.  
  341. /**
  342.  * Add a number of days to a date
  343.  *
  344.  * @param       string    $date Input date (format : YYYY-MM-DD, must be valid)
  345.  * @param       integer    $nb Number of days to add
  346.  * @return      string    Result date
  347.  */
  348. function date_add_days($date$nb)
  349. {
  350.   $year  substr($date04);
  351.   $month substr($date52);
  352.   $day   substr($date82);
  353.   $ts mktime(1200$month$day$year$nb 86400;
  354.   $tab getdate($ts);
  355.  
  356.   return sprintf("%04d-%02d-%02d"$tab['year']$tab['mon']$tab['mday']);
  357. }

Documentation generated on Sat, 28 Mar 2009 23:15:25 +0000 by phpDocumentor 1.4.1