'Column Label'
* @param string[] $aData Hash array. Data to display in the table: each row is made of 'column_id' => Data. A
* column 'pkey' is expected for each row
* @param array $aParams Hash array. Extra parameters for the table.
*
* @return void
*/
public function table($aConfig, $aData, $aParams = array());
}
/**
*
Simple helper class to ease the production of HTML pages
*
*
This class provide methods to add content, scripts, includes... to a web page
* and renders the full web page by putting the elements in the proper place & order
* when the output() method is called.
*
*
Usage:
* ```php
* $oPage = new WebPage("Title of my page");
* $oPage->p("Hello World !");
* $oPage->output();
* ```
*/
class WebPage implements Page
{
/**
* @since 2.7.0 N°2529
*/
const PAGES_CHARSET = 'utf-8';
protected $s_title;
protected $s_content;
protected $s_deferred_content;
protected $a_scripts;
protected $a_dict_entries;
protected $a_dict_entries_prefixes;
protected $a_styles;
protected $a_linked_scripts;
protected $a_linked_stylesheets;
protected $a_headers;
protected $a_base;
protected $iNextId;
protected $iTransactionId;
protected $sContentType;
protected $sContentDisposition;
protected $sContentFileName;
protected $bTrashUnexpectedOutput;
protected $s_sOutputFormat;
protected $a_OutputOptions;
protected $bPrintable;
protected $bHasCollapsibleSection;
protected $bAddJSDict;
/**
* WebPage constructor.
*
* @param string $s_title
* @param bool $bPrintable
*/
public function __construct($s_title, $bPrintable = false)
{
$this->s_title = $s_title;
$this->s_content = "";
$this->s_deferred_content = '';
$this->a_scripts = array();
$this->a_dict_entries = array();
$this->a_dict_entries_prefixes = array();
$this->a_styles = array();
$this->a_linked_scripts = array();
$this->a_linked_stylesheets = array();
$this->a_headers = array();
$this->a_base = array('href' => '', 'target' => '');
$this->iNextId = 0;
$this->iTransactionId = 0;
$this->sContentType = '';
$this->sContentDisposition = '';
$this->sContentFileName = '';
$this->bTrashUnexpectedOutput = false;
$this->s_OutputFormat = utils::ReadParam('output_format', 'html');
$this->a_OutputOptions = array();
$this->bHasCollapsibleSection = false;
$this->bPrintable = $bPrintable;
$this->bAddJSDict = true;
ob_start(); // Start capturing the output
}
/**
* Change the title of the page after its creation
*
* @param string $s_title
*/
public function set_title($s_title)
{
$this->s_title = $s_title;
}
/**
* Specify a default URL and a default target for all links on a page
*
* @param string $s_href
* @param string $s_target
*/
public function set_base($s_href = '', $s_target = '')
{
$this->a_base['href'] = $s_href;
$this->a_base['target'] = $s_target;
}
/**
* @inheritDoc
*/
public function add($s_html)
{
$this->s_content .= $s_html;
}
/**
* Add any rendered text or HTML fragment to the body of the page using a twig template
*
* @param string $sViewPath Absolute path of the templates folder
* @param string $sTemplateName Name of the twig template, ie MyTemplate for MyTemplate.html.twig
* @param array $aParams Params used by the twig template
* @param string $sDefaultType default type of the template ('html', 'xml', ...)
*
* @throws \Exception
*/
public function add_twig_template($sViewPath, $sTemplateName, $aParams = array(), $sDefaultType = 'html')
{
TwigHelper::RenderIntoPage($this, $sViewPath, $sTemplateName, $aParams, $sDefaultType);
}
/**
* Add any text or HTML fragment (identified by an ID) at the end of the body of the page
* This is useful to add hidden content, DIVs or FORMs that should not
* be embedded into each other.
*
* @param string $s_html
* @param string $sId
*/
public function add_at_the_end($s_html, $sId = '')
{
$this->s_deferred_content .= $s_html;
}
/**
* @inheritDoc
*/
public function p($s_html)
{
$this->add($this->GetP($s_html));
}
/**
* @inheritDoc
*/
public function pre($s_html)
{
$this->add('
'.$s_html.'
');
}
/**
* @inheritDoc
*/
public function add_comment($sText)
{
$this->add('');
}
/**
* Add a paragraph to the body of the page
*
* @param string $s_html
*
* @return string
*/
public function GetP($s_html)
{
return "
";
return $sHtml;
}
/**
* Add some Javascript to the header of the page
*
* @param string $s_script
*/
public function add_script($s_script)
{
$this->a_scripts[] = $s_script;
}
/**
* Add some Javascript to the header of the page
*
* @param $s_script
*/
public function add_ready_script($s_script)
{
// Do nothing silently... this is not supported by this type of page...
}
/**
* Allow a dictionnary entry to be used client side with Dict.S()
*
* @param string $s_entryId a translation label key
*
* @see \WebPage::add_dict_entries()
* @see utils.js
*/
public function add_dict_entry($s_entryId)
{
$this->a_dict_entries[] = $s_entryId;
}
/**
* Add a set of dictionary entries (based on the given prefix) for the Javascript side
*
* @param string $s_entriesPrefix translation label prefix (eg 'UI:Button:' to add all keys beginning with this)
*
* @see \WebPage::add_dict_entry()
* @see utils.js
*/
public function add_dict_entries($s_entriesPrefix)
{
$this->a_dict_entries_prefixes[] = $s_entriesPrefix;
}
/**
* @return string
*/
protected function get_dict_signature()
{
return str_replace('_', '', Dict::GetUserLanguage()).'-'.md5(implode(',',
$this->a_dict_entries).'|'.implode(',', $this->a_dict_entries_prefixes));
}
/**
* @return string
*/
protected function get_dict_file_content()
{
$aEntries = array();
foreach ($this->a_dict_entries as $sCode)
{
$aEntries[$sCode] = Dict::S($sCode);
}
foreach ($this->a_dict_entries_prefixes as $sPrefix)
{
$aEntries = array_merge($aEntries, Dict::ExportEntries($sPrefix));
}
$sJSFile = 'var aDictEntries = '.json_encode($aEntries);
return $sJSFile;
}
/**
* Add some CSS definitions to the header of the page
*
* @param string $s_style
*/
public function add_style($s_style)
{
$this->a_styles[] = $s_style;
}
/**
* Add a script (as an include, i.e. link) to the header of the page.
* Handles duplicates : calling twice with the same script will add the script only once
*
* @param string $s_linked_script
*/
public function add_linked_script($s_linked_script)
{
$this->a_linked_scripts[$s_linked_script] = $s_linked_script;
}
/**
* Add a CSS stylesheet (as an include, i.e. link) to the header of the page
*
* @param string $s_linked_stylesheet
* @param string $s_condition
*/
public function add_linked_stylesheet($s_linked_stylesheet, $s_condition = "")
{
$this->a_linked_stylesheets[] = array('link' => $s_linked_stylesheet, 'condition' => $s_condition);
}
/**
* @param string $sSaasRelPath
*
* @throws \Exception
*/
public function add_saas($sSaasRelPath)
{
$sCssRelPath = utils::GetCSSFromSASS($sSaasRelPath);
$sRootUrl = utils::GetAbsoluteUrlAppRoot();
if ($sRootUrl === '')
{
// We're running the setup of the first install...
$sRootUrl = '../';
}
$sCSSUrl = $sRootUrl.$sCssRelPath;
$this->add_linked_stylesheet($sCSSUrl);
}
/**
* Add some custom header to the page
*
* @param string $s_header
*/
public function add_header($s_header)
{
$this->a_headers[] = $s_header;
}
/**
* @param string|null $sXFrameOptionsHeaderValue passed to {@see add_xframe_options}
*
* @return void
* @since 2.7.10 3.0.4 3.1.2 3.2.0 N°4368 method creation, replace {@see add_xframe_options} consumers call
*/
public function add_http_headers($sXFrameOptionsHeaderValue = null)
{
$this->add_xframe_options($sXFrameOptionsHeaderValue);
$this->add_xcontent_type_options();
}
/**
* @param string|null $sHeaderValue for example `SAMESITE`. If null will set the header using the `security_header_xframe` config parameter value.
*
* @since 2.7.3 3.0.0 N°3416
* @uses \utils::GetConfig()
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options HTTP header MDN documentation
*/
public function add_xframe_options($sHeaderValue = null)
{
if (is_null($sHeaderValue)) {
$sHeaderValue = utils::GetConfig()->Get('security_header_xframe');
}
$this->add_header('X-Frame-Options: '.$sHeaderValue);
}
/**
* Warning : this header will trigger the Cross-Origin Read Blocking (CORB) protection for some mime types (HTML, XML except SVG, JSON, text/plain)
* In consequence some children pages will override this method.
*
* Sending header can be disabled globally using the `security.enable_header_xcontent_type_options` optional config parameter.
*
* @return void
* @since 2.7.10 3.0.4 3.1.2 3.2.0 N°4368 method creation
*
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options HTTP header MDN documentation
* @link https://chromium.googlesource.com/chromium/src/+/master/services/network/cross_origin_read_blocking_explainer.md#determining-whether-a-response-is-corb_protected "Determining whether a response is CORB-protected"
*/
public function add_xcontent_type_options()
{
try {
$oConfig = utils::GetConfig();
} catch (ConfigException|CoreException $e) {
$oConfig = null;
}
if (is_null($oConfig)) {
$bSendXContentTypeOptionsHttpHeader = true;
} else {
$bSendXContentTypeOptionsHttpHeader = $oConfig->Get('security.enable_header_xcontent_type_options');
}
if ($bSendXContentTypeOptionsHttpHeader === false) {
return;
}
$this->add_header('X-Content-Type-Options: nosniff');
}
/**
* Add needed headers to the page so that it will no be cached
*/
public function no_cache()
{
$this->add_header('Cache-control: no-cache, no-store, must-revalidate');
$this->add_header('Pragma: no-cache');
$this->add_header('Expires: 0');
}
/**
* Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
*
* @param array $aFields
*/
public function details($aFields)
{
$this->add($this->GetDetails($aFields));
}
/**
* Whether or not the page is a PDF page
*
* @return boolean
*/
public function is_pdf()
{
return false;
}
/**
* Records the current state of the 'html' part of the page output
*
* @return mixed The current state of the 'html' output
*/
public function start_capture()
{
return strlen($this->s_content);
}
/**
* Returns the part of the html output that occurred since the call to start_capture
* and removes this part from the current html output
*
* @param $offset mixed The value returned by start_capture
*
* @return string The part of the html output that was added since the call to start_capture
*/
public function end_capture($offset)
{
$sCaptured = substr($this->s_content, $offset);
$this->s_content = substr($this->s_content, 0, $offset);
return $sCaptured;
}
/**
* Build a special kind of TABLE useful for displaying the details of an object from a hash array of data
*
* @param array $aFields
*
* @return string
*/
public function GetDetails($aFields)
{
$aPossibleAttFlags = MetaModel::EnumPossibleAttributeFlags();
$sHtml = "
\n";
// By Rom, for csv import, proposed to show several values for column selection
if (is_array($aAttrib['value']))
{
$sHtml .= "
".implode("
", $aAttrib['value'])."
\n";
}
else
{
$sHtml .= "
".$aAttrib['value']."
\n";
}
// Checking if we should add comments & infos
$sComment = (isset($aAttrib['comments'])) ? $aAttrib['comments'] : '';
$sInfo = (isset($aAttrib['infos'])) ? $aAttrib['infos'] : '';
if ($sComment !== '')
{
$sHtml .= "
$sComment
\n";
}
if ($sInfo !== '')
{
$sHtml .= "
$sInfo
\n";
}
$sHtml .= "
\n";
$sHtml .= "
\n";
}
$sHtml .= "
\n";
return $sHtml;
}
/**
* Build a set of radio buttons suitable for editing a field/attribute of an object (including its validation)
*
* @param $aAllowedValues array Array of value => display_value
* @param $value mixed Current value for the field/attribute
* @param $iId mixed Unique Id for the input control in the page
* @param $sFieldName string The name of the field, attr_<$sFieldName> will hold the value for the field
* @param $bMandatory bool Whether or not the field is mandatory
* @param $bVertical bool Disposition of the radio buttons vertical or horizontal
* @param $sValidationField string HTML fragment holding the validation field (exclamation icon...)
*
* @return string The HTML fragment corresponding to the radio buttons
*/
public function GetRadioButtons(
$aAllowedValues, $value, $iId, $sFieldName, $bMandatory, $bVertical, $sValidationField
) {
$idx = 0;
$sHTMLValue = '';
foreach ($aAllowedValues as $key => $display_value)
{
if ((count($aAllowedValues) == 1) && ($bMandatory == 'true'))
{
// When there is only once choice, select it by default
$sSelected = 'checked';
}
else
{
$sSelected = ($value == $key) ? 'checked' : '';
}
$sHTMLValue .= " ";
if ($bVertical)
{
if ($idx == 0)
{
// Validation icon at the end of the first line
$sHTMLValue .= " {$sValidationField}\n";
}
$sHTMLValue .= " \n";
}
$idx++;
}
$sHTMLValue .= "";
if (!$bVertical)
{
// Validation icon at the end of the line
$sHTMLValue .= " {$sValidationField}\n";
}
return $sHTMLValue;
}
/**
* Discard unexpected output data (such as PHP warnings)
* This is a MUST when the Page output is DATA (download of a document, download CSV export, download ...)
*/
public function TrashUnexpectedOutput()
{
$this->bTrashUnexpectedOutput = true;
}
/**
* Read the output buffer and deal with its contents:
* - trash unexpected output if the flag has been set
* - report unexpected behaviors such as the output buffering being stopped
*
* Possible improvement: I've noticed that several output buffers are stacked,
* if they are not empty, the output will be corrupted. The solution would
* consist in unstacking all of them (and concatenate the contents).
*
* @throws \Exception
*/
protected function ob_get_clean_safe()
{
$sOutput = ob_get_contents();
if ($sOutput === false)
{
$sMsg = "Design/integration issue: No output buffer. Some piece of code has called ob_get_clean() or ob_end_clean() without calling ob_start()";
if ($this->bTrashUnexpectedOutput)
{
IssueLog::Error($sMsg);
$sOutput = '';
}
else
{
$sOutput = $sMsg;
}
}
else
{
ob_end_clean(); // on some versions of PHP doing so when the output buffering is stopped can cause a notice
if ($this->bTrashUnexpectedOutput)
{
if (trim($sOutput) != '')
{
if (Utils::GetConfig() && Utils::GetConfig()->Get('debug_report_spurious_chars'))
{
IssueLog::Error("Trashing unexpected output:'$sOutput'\n");
}
}
$sOutput = '';
}
}
return $sOutput;
}
/**
* @inheritDoc
* @throws \Exception
*/
public function output()
{
foreach ($this->a_headers as $s_header)
{
header($s_header);
}
$s_captured_output = $this->ob_get_clean_safe();
echo "\n";
echo "\n";
echo "\n";
echo "\n";
echo "";
echo "".htmlentities($this->s_title, ENT_QUOTES, 'UTF-8')."\n";
echo $this->get_base_tag();
// First put stylesheets so they can be loaded before browser interprets JS files, otherwise visual glitch can occur.
foreach ($this->a_linked_stylesheets as $a_stylesheet)
{
if (strpos($a_stylesheet['link'], '?') === false)
{
$s_stylesheet = $a_stylesheet['link']."?t=".utils::GetCacheBusterTimestamp();
}
else
{
$s_stylesheet = $a_stylesheet['link']."&t=".utils::GetCacheBusterTimestamp();
}
if ($a_stylesheet['condition'] != "")
{
echo "\n";
}
}
// Then inline styles
if (count($this->a_styles) > 0)
{
echo "\n";
}
// Favicon
if (class_exists('MetaModel') && MetaModel::GetConfig())
{
echo "\n";
}
// Dict entries for JS
if ($this->bAddJSDict)
{
$this->output_dict_entries();
}
// JS files
foreach ($this->a_linked_scripts as $s_script)
{
// Make sure that the URL to the script contains the application's version number
// so that the new script do NOT get reloaded from the cache when the application is upgraded
if (strpos($s_script, '?') === false)
{
$s_script .= "?t=".utils::GetCacheBusterTimestamp();
}
else
{
$s_script .= "&t=".utils::GetCacheBusterTimestamp();
}
echo "\n";
}
// JS inline scripts
if (count($this->a_scripts) > 0)
{
echo "\n";
}
echo "\n";
echo "\n";
echo self::FilterXSS($this->s_content);
if (trim($s_captured_output) != "")
{
echo "
".self::FilterXSS($s_captured_output)."
\n";
}
echo '
'.self::FilterXSS($this->s_deferred_content).'
';
echo "\n";
echo "\n";
if (class_exists('DBSearch'))
{
DBSearch::RecordQueryTrace();
}
if (class_exists('ExecutionKPI'))
{
ExecutionKPI::ReportStats();
}
}
/**
* Build a series of hidden field[s] from an array
*
* @param string $sLabel
* @param array $aData
*/
public function add_input_hidden($sLabel, $aData)
{
foreach ($aData as $sKey => $sValue)
{
// Note: protection added to protect against the Notice 'array to string conversion' that appeared with PHP 5.4
// (this function seems unused though!)
if (is_scalar($sValue))
{
$this->add("");
}
}
}
protected function get_base_tag()
{
$sTag = '';
if (($this->a_base['href'] != '') || ($this->a_base['target'] != ''))
{
$sTag = 'a_base['href'] != ''))
{
$sTag .= "href =\"{$this->a_base['href']}\" ";
}
if (($this->a_base['target'] != ''))
{
$sTag .= "target =\"{$this->a_base['target']}\" ";
}
$sTag .= " />\n";
}
return $sTag;
}
/**
* Get an ID (for any kind of HTML tag) that is guaranteed unique in this page
*
* @return int The unique ID (in this page)
*/
public function GetUniqueId()
{
return $this->iNextId++;
}
/**
* Set the content-type (mime type) for the page's content
*
* @param $sContentType string
*
* @return void
*/
public function SetContentType($sContentType)
{
$this->sContentType = $sContentType;
}
/**
* Set the content-disposition (mime type) for the page's content
*
* @param $sDisposition string The disposition: 'inline' or 'attachment'
* @param $sFileName string The original name of the file
*
* @return void
*/
public function SetContentDisposition($sDisposition, $sFileName)
{
$this->sContentDisposition = $sDisposition;
$this->sContentFileName = $sFileName;
}
/**
* Set the transactionId of the current form
*
* @param $iTransactionId integer
*
* @return void
*/
public function SetTransactionId($iTransactionId)
{
$this->iTransactionId = $iTransactionId;
}
/**
* Returns the transactionId of the current form
*
* @return integer The current transactionID
*/
public function GetTransactionId()
{
return $this->iTransactionId;
}
public static function FilterXSS($sHTML)
{
return str_ireplace('