dolibarr/htdocs/webportal/class/context.class.php

744 lines
18 KiB
PHP
Raw Permalink Normal View History

2023-09-12 15:12:30 +02:00
<?php
/* Copyright (C) 2023-2024 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/webportal/class/context.class.php
* \ingroup webportal
* \brief File of context class for WebPortal
*/
2023-09-12 15:12:30 +02:00
require_once __DIR__ . '/controller.class.php';
2023-09-28 10:44:22 +02:00
require_once __DIR__ . '/webPortalTheme.class.php';
2023-09-12 15:12:30 +02:00
/**
* Class Context
*/
class Context
{
2023-09-12 15:26:12 +02:00
/**
* @var ?Context Singleton
2023-09-12 15:26:12 +02:00
* @access private
* @static
*/
private static $_instance = null;
2023-09-12 15:12:30 +02:00
2023-09-12 16:50:13 +02:00
/**
* @var DoliDB $db Database handler
2023-09-12 16:50:13 +02:00
*/
public $db;
/**
* @var string
*/
2023-09-12 15:26:12 +02:00
public $title;
/**
* @var string
*/
2023-09-12 15:26:12 +02:00
public $desc;
2023-09-12 15:12:30 +02:00
/**
* @var string
*/
2023-09-12 15:26:12 +02:00
public $meta_title;
/**
* @var string
*/
2023-09-12 15:26:12 +02:00
public $meta_desc;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
/**
* The application name
2024-01-22 14:33:35 +01:00
* @var string $appliName
2023-09-12 15:26:12 +02:00
*/
public $appliName;
2023-09-12 15:12:30 +02:00
/**
* @var string
*/
2023-09-12 15:26:12 +02:00
public $controller;
/**
* @var boolean
*/
2023-09-12 15:26:12 +02:00
public $controller_found = false;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
/**
* @var stdClass[]
*/
private $controllers = array();
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
/**
* @var Controller $controllerInstance
*/
public $controllerInstance;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
/**
* for internal error msg
* @var string error
*/
public $error;
2023-09-12 15:12:30 +02:00
2024-01-23 16:56:43 +01:00
/**
* @var string[] errors
2024-01-23 16:56:43 +01:00
*/
public $errors = array();
2024-01-22 14:33:35 +01:00
/**
* @var string Action
*/
2023-09-12 15:26:12 +02:00
public $action;
2023-09-12 15:12:30 +02:00
/**
* @var string tpl directory
*/
2023-09-12 15:26:12 +02:00
public $tplDir;
/**
* @var string tpl path
*/
2023-09-12 15:26:12 +02:00
public $tplPath;
/**
* @var stdClass
*/
2023-09-12 15:26:12 +02:00
public $topMenu;
2023-09-12 15:12:30 +02:00
/**
* @var string root url
*/
2023-09-12 15:26:12 +02:00
public $rootUrl;
2023-09-12 15:12:30 +02:00
/**
* @var string[]
*/
2023-09-12 15:26:12 +02:00
public $menu_active = array();
2023-09-12 15:12:30 +02:00
/**
* @var array{mesgs:string[],warnings:string[],errors:string[]}|array{} event messages
*/
2023-09-12 15:26:12 +02:00
public $eventMessages = array();
2023-09-12 15:12:30 +02:00
/**
* @var string token key
*/
2023-09-28 10:44:22 +02:00
public $tokenKey = 'token';
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
/**
* Current object of page
2023-09-12 15:26:12 +02:00
* @var object $object
*/
public $object;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
/**
* @var CommonObject Logged user
*/
public $logged_user = null;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
/**
* @var CommonObject Logged third-party
*/
public $logged_thirdparty = null;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
/**
* @var CommonObject Logged member
*/
public $logged_member = null;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
/**
* @var CommonObject Logged partnership
*/
public $logged_partnership = null;
2023-09-12 15:12:30 +02:00
2023-09-28 10:44:22 +02:00
/**
* @var WebPortalTheme Theme data
*/
public $theme;
2023-09-12 15:12:30 +02:00
/**
* Constructor
*
* @return void
*/
2023-09-12 15:26:12 +02:00
private function __construct()
{
2023-09-12 16:50:13 +02:00
global $conf, $db;
$this->db = $db;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
$this->tplDir = __DIR__ . '/../';
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
$this->getControllerUrl();
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
$this->topMenu = new stdClass();
2023-09-12 15:12:30 +02:00
$this->tplPath = realpath(__DIR__ . '/../../public/webportal/tpl');
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
$this->controller = GETPOST('controller', 'aZ09'); // for security, limited to 'aZ09'
$this->action = GETPOST('action', 'aZ09');// for security, limited to 'aZ09'
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
if (empty($this->controller)) {
$this->controller = 'default';
}
2023-09-12 15:12:30 +02:00
$this->appliName = getDolGlobalString('WEBPORTAL_TITLE', getDolGlobalString('MAIN_INFO_SOCIETE_NOM'));
2023-09-12 15:12:30 +02:00
//$this->generateNewToken();
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
$this->initController();
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
// Init de l'url de base
$this->rootUrl = self::getRootConfigUrl();
2023-09-28 10:44:22 +02:00
$this->theme = new WebPortalTheme();
2023-09-12 15:26:12 +02:00
}
2023-09-12 15:12:30 +02:00
/**
* Singleton method to create one instance of this object
*
2023-09-12 16:50:13 +02:00
* @return Context Instance
2023-09-12 15:12:30 +02:00
*/
public static function getInstance()
{
if (is_null(self::$_instance)) {
2023-09-12 15:26:12 +02:00
self::$_instance = new Context();
2023-09-12 15:12:30 +02:00
}
return self::$_instance;
}
/**
2023-09-12 15:26:12 +02:00
* Init controller
*
2024-01-22 14:33:35 +01:00
* @return void
2023-09-12 15:12:30 +02:00
*/
public function initController()
{
global $db;
2023-09-12 15:12:30 +02:00
$defaultControllersPath = __DIR__ . '/../controllers/';
// define controllers definition
$this->addControllerDefinition('login', $defaultControllersPath . 'login.controller.class.php', 'LoginController');
$this->addControllerDefinition('default', $defaultControllersPath . 'default.controller.class.php', 'DefaultController');
$this->addControllerDefinition('document', $defaultControllersPath . 'document.controller.class.php', 'DocumentController');
$this->addControllerDefinition('propallist', $defaultControllersPath . 'propallist.controller.class.php', 'PropalListController');
$this->addControllerDefinition('orderlist', $defaultControllersPath . 'orderlist.controller.class.php', 'OrderListController');
$this->addControllerDefinition('invoicelist', $defaultControllersPath . 'invoicelist.controller.class.php', 'InvoiceListController');
$this->addControllerDefinition('membercard', $defaultControllersPath . 'membercard.controller.class.php', 'MemberCardController');
$this->addControllerDefinition('partnershipcard', $defaultControllersPath . 'partnershipcard.controller.class.php', 'PartnershipCardController');
2023-09-12 15:12:30 +02:00
// call triggers
//include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
//$interface=new Interfaces($db);
//$interface->run_triggers('WebPortalInitController', $this, $logged_user, $langs, $conf);
// search for controller
$this->controllerInstance = new Controller();
if (isset($this->controllers[$this->controller]) && file_exists($this->controllers[$this->controller]->path)) {
2023-09-12 15:26:12 +02:00
require_once $this->controllers[$this->controller]->path;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
if (class_exists($this->controllers[$this->controller]->class)) {
$this->controllerInstance = new $this->controllers[$this->controller]->class();
$this->setControllerFound();
}
}
2023-09-12 15:12:30 +02:00
}
/**
2023-09-12 15:26:12 +02:00
* Add controller definition
*
2023-09-12 16:50:13 +02:00
* @param string $controller Name
* @param string $path Path
* @param string $className Class name
2023-09-12 15:12:30 +02:00
* @return bool
*/
2023-09-12 15:26:12 +02:00
public function addControllerDefinition($controller, $path, $className)
{
$fileName = basename($path);
$needle = '.controller.class.php';
2023-09-12 15:26:12 +02:00
$length = strlen($needle);
$isControllerFile = $length > 0 ? substr($fileName, -$length) === $needle : true;
if (!$isControllerFile) {
$this->setError('Error: controller definition ' . $fileName);
return false;
}
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
$this->controllers[$controller] = new stdClass();
$this->controllers[$controller]->path = $path;
$this->controllers[$controller]->class = $className;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
return true;
}
2023-09-12 15:12:30 +02:00
/**
* Set controller found
*
* @return void
*/
2023-09-12 15:26:12 +02:00
public function setControllerFound()
{
$this->controller_found = true;
}
2023-09-12 15:12:30 +02:00
/**
2023-09-12 15:26:12 +02:00
* Get WebPortal root url
*
2023-09-12 15:12:30 +02:00
* @return string Web Portal root url
*/
public static function getRootConfigUrl()
2023-09-12 15:26:12 +02:00
{
global $conf;
// Init de l'url de base
Fix #28071 - New branch to fix bad merge (#28083) * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Qual: Introduce getDataToShowPhoto to prepare generic code * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Fix missing trans * Fix langs * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Remove useless files in web templates * Clean code * Fix duplicate translation key * Fix duplicate translation key * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Fix duplicate key * Fix $object * Debug v19 * WIP SMSing * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * WIP EMAILINGS_SUPPORT_ALSO_SMS * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * WIP SMSing * Debug the "validate" feature * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Clean code * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Enhance rector to fix empty($user->rights->modulex->perm1) * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Fix template to use v19 dev rules * Fix use v19 dev rules * Fix phpunit * Debug v19 * Clean code * Use rector to convert user->rights into user->hasRight * Clean code * Use rector to convert user->rights into user->hasRight * Use rector to convert user->rights into user->hasRight * Clean code * Fix phpcs * add editorconfig for sql files (#27999) Co-authored-by: Laurent Destailleur <eldy@destailleur.fr> * add model_pdf field in llx_ticket-ticket.sql (#27996) * add model_pdf field in llx_ticket-ticket.sql * Update 19.0.0-20.0.0.sql * Update 19.0.0-20.0.0.sql * Improve wording in README (#27994) * fix phpstan (#27989) * fix phpstan * Update UserRightsToFunction.php --------- Co-authored-by: Laurent Destailleur <eldy@destailleur.fr> * Qual: Fix spelling/working in datapolicy translations (#28006) # Qual: Fix spelling/wording in datapolicy translations Fixed some spelling and wording in datapolicy translations. * qual: phpstan for htdocs/ticket/class/ticketstats.class.php (#27986) htdocs/ticket/class/ticketstats.class.php 98 Parameter #1 $year (string) of method TicketStats::getNbByMonth() should be compatible with parameter $year (int) of method Stats::getNbByMonth() * Merge branch '19.0' of git@github.com:Dolibarr/dolibarr.git into develop * Fix user with readonly perm on email template must be able to read. * Fix doc * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Better message * Add missing fields in merge of thirdparty * Debug v19 selection of ticket printer per terminal * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Use constant * NEW: Adding a recipient on emails sent, change status to sent partialy. * fix travis (#28052) * fix travis * Update partnership.class.php * fix php doc (#28047) * fix undefined array key (#28048) * Add region and departament for Cuba (#28046) * Update llx_10_c_regions.sql Add Cuba Regions (id_country=77) * Update llx_20_c_departements.sql Add Provinces Cuba (id country=77) * Find the typo (#28050) * Find the typo * clean code * add last_main_doc field to product (#28045) * add las_main_doc field to product * add field fetch * NEW Add Categorie filter for ActionComm (#28041) * New Add Categorie filter for ActionComm New Add Categorie filter for ActionComm * Fix space errors Fix space errors * Fix space errors 2 Fix space errors 2 * Update cunits.class.php (#28056) FIX: error SQL when creating a Cunit * Update codespell-lines-ignore.txt to avoid PR merge conflict --------- Co-authored-by: Laurent Destailleur <eldy@destailleur.fr> Co-authored-by: Frédéric FRANCE <frederic34@users.noreply.github.com> Co-authored-by: thibdrev <thibault.drevet@gmail.com> Co-authored-by: sonikf <93765174+sonikf@users.noreply.github.com> Co-authored-by: Ikarus <44511582+LeKarSol@users.noreply.github.com> Co-authored-by: Anthony Damhet <73399671+EchoLoGeek@users.noreply.github.com> Co-authored-by: Quentin-Seekness <72733832+Quentin-Seekness@users.noreply.github.com>
2024-02-09 15:58:49 +01:00
if (getDolGlobalString('WEBPORTAL_ROOT_URL')) {
$rootUrl = getDolGlobalString('WEBPORTAL_ROOT_URL');
2023-09-12 15:26:12 +02:00
if (substr($rootUrl, -1) !== '/') {
$rootUrl .= '/';
}
} else {
$rootUrl = dol_buildpath('/public/webportal/', 2);
2023-09-12 15:26:12 +02:00
}
return $rootUrl;
}
/**
* Get root url
*
2023-09-12 16:50:13 +02:00
* @param string $controller Controller name
* @param string|array<string,mixed> $moreParams More parameters
* @param bool $addToken Add token hash only if $controller is set
2023-09-12 16:50:13 +02:00
* @return string
2023-09-12 15:12:30 +02:00
* @deprecated see getControllerUrl()
*/
public function getRootUrl($controller = '', $moreParams = '', $addToken = true)
2023-09-12 15:26:12 +02:00
{
2023-09-12 15:12:30 +02:00
return self::getControllerUrl($controller, $moreParams, $addToken);
}
/**
* Get controller url according to context
2023-09-12 15:26:12 +02:00
*
2023-09-12 16:50:13 +02:00
* @param string $controller Controller name
* @param string|array<string,mixed> $moreParams More parameters
2023-09-12 16:50:13 +02:00
* @param bool $addToken Add token hash only if controller is set
* @return string
2023-09-12 15:12:30 +02:00
*/
public function getControllerUrl($controller = '', $moreParams = '', $addToken = true)
2023-09-12 15:12:30 +02:00
{
// TODO : addToken parameter on auto to detect (create or edit) action and add token on url
2023-09-12 15:26:12 +02:00
$url = $this->rootUrl;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
if (empty($controller)) {
// because can be called without params to get only rootUrl
return $url;
}
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
$Tparams = array();
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
$Tparams['controller'] = $controller;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
if (!empty($addToken)) {
$Tparams[$this->tokenKey] = $this->newToken();
}
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
return self::getPublicControllerUrl($controller, $moreParams, $Tparams);
2023-09-12 15:12:30 +02:00
}
/**
* Generate public controller URL
* Used for external link (like email or web page)
* so remove token and contextual behavior associate with current user
*
* @param string $controller Controller
* @param string|array<string,mixed> $moreParams More parameters
* @param array<string,mixed> $Tparams Parameters
2023-09-12 16:05:24 +02:00
* @return string
2023-09-12 15:12:30 +02:00
*/
public static function getPublicControllerUrl($controller = '', $moreParams = '', $Tparams = array())
2023-09-12 15:12:30 +02:00
{
$url = self::getRootConfigUrl();
if (empty($controller)) {
// because can be called without params to get only rootUrl
return $url;
}
$Tparams['controller'] = $controller;
// if $moreParams is an array
2023-09-12 15:26:12 +02:00
if (!empty($moreParams) && is_array($moreParams)) {
if (isset($moreParams['controller'])) {
unset($moreParams['controller']);
}
2023-09-12 15:26:12 +02:00
if (!empty($moreParams)) {
foreach ($moreParams as $paramKey => $paramVal) {
2023-09-12 15:12:30 +02:00
$Tparams[$paramKey] = $paramVal;
}
}
}
2023-09-12 15:26:12 +02:00
if (!empty($Tparams)) {
2023-09-12 15:12:30 +02:00
$TCompiledAttr = array();
foreach ($Tparams as $key => $value) {
2023-09-12 15:26:12 +02:00
$TCompiledAttr[] = $key . '=' . $value;
2023-09-12 15:12:30 +02:00
}
2023-09-12 15:26:12 +02:00
$url .= '?' . implode("&", $TCompiledAttr);
2023-09-12 15:12:30 +02:00
}
// if $moreParams is a string
if (!empty($moreParams) && !is_array($moreParams)) {
if (empty($Tparams)) {
if ($moreParams[0] !== '?') {
$url .= '?';
}
if ($moreParams[0] === '&') {
$moreParams = substr($moreParams, 1);
}
2023-09-12 15:12:30 +02:00
}
$url .= $moreParams;
}
return $url;
}
/**
2023-09-12 15:26:12 +02:00
* Url origin
*
2023-09-12 16:50:13 +02:00
* @param bool $withRequestUri With request URI
* @param bool $use_forwarded_host Use formatted host
* @return string
2023-09-12 15:12:30 +02:00
*/
public static function urlOrigin($withRequestUri = true, $use_forwarded_host = false)
2023-09-12 15:26:12 +02:00
{
$s = $_SERVER;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
$ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on');
$sp = strtolower($s['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
$port = $s['SERVER_PORT'];
$port = ((!$ssl && $port == '80') || ($ssl && $port == '443')) ? '' : ':' . $port;
$host = ($use_forwarded_host && isset($s['HTTP_X_FORWARDED_HOST'])) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null);
$host = isset($host) ? $host : $s['SERVER_NAME'] . $port;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
$url = $protocol . '://' . $host;
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
if ($withRequestUri) {
$url .= $s['REQUEST_URI'];
}
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
return $url;
}
2023-09-12 15:12:30 +02:00
/**
2023-09-12 15:26:12 +02:00
* Check if user is logged
*
2023-09-12 16:50:13 +02:00
* @return bool
2023-09-12 15:12:30 +02:00
*/
2023-09-12 15:26:12 +02:00
public function userIsLog()
{
if (!empty($_SESSION["webportal_logged_thirdparty_account_id"])) {
return true;
} else {
return false;
}
}
2023-09-12 15:12:30 +02:00
/**
2023-09-12 15:26:12 +02:00
* Is menu enabled ?
*
2023-09-12 16:50:13 +02:00
* @param string $menuName Menu name
2023-09-12 15:12:30 +02:00
* @return bool
*/
public function menuIsActive($menuName)
{
return in_array($menuName, $this->menu_active);
}
/**
2023-09-12 15:26:12 +02:00
* Set errors
*
* @param string|string[] $errors Errors
2023-09-12 16:50:13 +02:00
* @return void
2023-09-12 15:12:30 +02:00
*/
public function setError($errors)
{
if (!is_array($errors)) {
$errors = array($errors);
}
if (!isset($_SESSION['webportal_errors'])) {
$_SESSION['webportal_errors'] = array();
}
2023-09-12 15:26:12 +02:00
foreach ($errors as $msg) {
if (!in_array($msg, $_SESSION['webportal_errors'])) {
$_SESSION['webportal_errors'][] = $msg;
}
2023-09-12 15:12:30 +02:00
}
}
/**
2023-09-12 15:26:12 +02:00
* Get errors
*
2023-09-12 15:12:30 +02:00
* @return int
*/
public function getErrors()
{
if (!empty($_SESSION['webportal_errors'])) {
$this->errors = array_values($_SESSION['webportal_errors']);
return count($this->errors);
}
return 0;
}
/**
* Clear errors
2023-09-12 15:26:12 +02:00
*
2023-09-12 15:12:30 +02:00
* @return void
*/
public function clearErrors()
{
unset($_SESSION['webportal_errors']);
$this->errors = array();
}
/**
2023-09-12 16:50:13 +02:00
* Set event messages in dol_events session object. Will be output by calling dol_htmloutput_events.
* Note: Calling dol_htmloutput_events is done into pages by standard llxFooter() function.
2023-09-12 15:26:12 +02:00
*
2023-09-12 16:50:13 +02:00
* @param string|string[] $mesgs Message string or array
* @param string $style Which style to use ('mesgs' by default, 'warnings', 'errors')
* @return void
2023-09-12 15:26:12 +02:00
*/
public function setEventMessage($mesgs, $style = 'mesgs')
{
$TAcceptedStyle = array('mesgs', 'warnings', 'errors');
if (!in_array($style, $TAcceptedStyle)) {
$style = 'mesgs';
}
if (!is_array($mesgs)) {
$mesgs = array($mesgs);
}
if (!isset($_SESSION['webportal_events'])) {
$_SESSION['webportal_events'] = array(
'mesgs' => array(), 'warnings' => array(), 'errors' => array()
);
}
foreach ($mesgs as $msg) {
if (!in_array($msg, $_SESSION['webportal_events'][$style])) {
$_SESSION['webportal_events'][$style][] = $msg;
}
}
}
/**
2023-09-12 16:50:13 +02:00
* Set event messages in dol_events session object. Will be output by calling dol_htmloutput_events.
* Note: Calling dol_htmloutput_events is done into pages by standard llxFooter() function.
2023-09-12 15:12:30 +02:00
*
* @param string $mesg Message string
* @param string[]|null $mesgs Message array
* @param string $style Which style to use ('mesgs' by default, 'warnings', 'errors')
2023-09-12 16:05:24 +02:00
* @return void
2023-09-12 15:26:12 +02:00
*/
2023-09-12 16:05:24 +02:00
public function setEventMessages($mesg, $mesgs, $style = 'mesgs')
2023-09-12 15:26:12 +02:00
{
if (empty($mesg) && empty($mesgs)) {
dol_syslog(__METHOD__ . ' Try to add a message in stack, but value to add is empty message', LOG_WARNING);
} else {
2023-09-12 16:05:24 +02:00
if (!in_array((string) $style, array('mesgs', 'warnings', 'errors'))) {
dol_print_error(null, 'Bad parameter style=' . $style . ' for setEventMessages');
2023-09-12 15:26:12 +02:00
}
if (empty($mesgs)) {
$this->setEventMessage($mesg, $style);
} else {
if (!empty($mesg) && !in_array($mesg, $mesgs)) {
$this->setEventMessage($mesg, $style); // Add message string if not already into array
}
$this->setEventMessage($mesgs, $style);
}
}
}
/**
* Load event messages
*
2023-09-12 15:12:30 +02:00
* @return int
*/
2023-09-12 15:26:12 +02:00
public function loadEventMessages()
{
if (!empty($_SESSION['webportal_events'])) {
$this->eventMessages = $_SESSION['webportal_events'];
return 1;
}
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
return 0;
}
2023-09-12 15:12:30 +02:00
/**
* Clear event messages
2023-09-12 15:26:12 +02:00
*
2023-09-12 15:12:30 +02:00
* @return void
*/
public function clearEventMessages()
{
unset($_SESSION['webportal_events']);
$this->eventMessages = array();
}
/**
* Return the value of token currently saved into session with name 'newToken'.
2023-09-12 16:50:13 +02:00
* This token must be sent by any POST as it will be used by next page for comparison with value in session.
* This token depends on controller
2023-09-12 15:12:30 +02:00
*
* @return string
*/
public function newToken()
2023-09-12 15:26:12 +02:00
{
return newToken();
2023-09-12 15:26:12 +02:00
}
2023-09-12 15:12:30 +02:00
/**
* Generate new token.
* @deprecated see main
2023-09-12 16:50:13 +02:00
* @return string
2023-09-12 15:12:30 +02:00
*/
protected function generateNewToken()
2023-09-12 15:26:12 +02:00
{
$currentToken = $this->newToken();
2023-09-12 15:26:12 +02:00
// Creation of a token against CSRF vulnerabilities
if (!defined('NOTOKENRENEWAL') || empty($currentToken)) {
2023-09-12 15:26:12 +02:00
// Rolling token at each call ($_SESSION['token'] contains token of previous page)
if (isset($_SESSION['newtoken'])) {
$_SESSION['token'] = $_SESSION['newtoken'];
2023-09-12 15:26:12 +02:00
}
// Save what will be next token. Into forms, we will add param $context->newToken();
$token = dol_hash(uniqid((string) mt_rand(), true)); // Generate
$_SESSION['newtoken'] = $token;
2023-09-12 15:26:12 +02:00
return $token;
} else {
return $this->newToken();
2023-09-12 15:26:12 +02:00
}
}
2023-09-12 15:12:30 +02:00
2023-09-12 15:26:12 +02:00
/**
* Get token url
*
2023-09-12 16:50:13 +02:00
* @return string|null
2023-09-12 15:12:30 +02:00
*/
2024-01-22 14:33:35 +01:00
public function getUrlToken()
2023-09-12 15:26:12 +02:00
{
2024-01-22 14:33:35 +01:00
$token = $this->newToken();
2023-09-12 15:26:12 +02:00
if ($token) {
2024-01-22 14:33:35 +01:00
return '&' . $this->tokenKey . '=' . $this->newToken();
2023-09-12 15:26:12 +02:00
}
2024-01-22 14:33:35 +01:00
return null;
2023-09-12 15:26:12 +02:00
}
2023-09-12 15:12:30 +02:00
/**
2023-09-12 15:26:12 +02:00
* Get token input for form
*
2023-09-12 15:12:30 +02:00
* @return string|null
*/
2024-01-22 14:33:35 +01:00
public function getFormToken()
2023-09-12 15:26:12 +02:00
{
2024-01-22 14:33:35 +01:00
$token = $this->newToken();
2023-09-12 15:26:12 +02:00
if ($token) {
2024-01-22 14:33:35 +01:00
return '<input type="hidden" name="' . $this->tokenKey . '" value="' . $this->newToken() . '" />';
2023-09-12 15:26:12 +02:00
}
2024-01-22 14:33:35 +01:00
return null;
2023-09-12 15:26:12 +02:00
}
/**
* Try to find the third-party account id from
*
2023-09-12 16:50:13 +02:00
* @param string $login Login
* @param string $pass Password
* @return int Third-party account id || <0 if error
2023-09-12 15:26:12 +02:00
*/
public function getThirdPartyAccountFromLogin($login, $pass)
{
$id = 0;
$sql = "SELECT sa.rowid as id, sa.pass_crypted";
2023-09-12 16:50:13 +02:00
$sql .= " FROM " . $this->db->prefix() . "societe_account as sa";
$sql .= " WHERE sa.login = '" . $this->db->escape($login) . "'";
//$sql .= " AND BINARY sa.pass_crypted = '" . $this->db->escape($pass) . "'"; // case sensitive
2023-09-12 15:26:12 +02:00
$sql .= " AND sa.site = 'dolibarr_portal'";
$sql .= " AND sa.status = 1";
$sql .= " AND sa.entity IN (" . getEntity('societe') . ")";
dol_syslog(__METHOD__ . ' Try to find the third-party account id for login"' . $login . '" and site="dolibarr_portal"', LOG_DEBUG);
2023-09-12 16:50:13 +02:00
$result = $this->db->query($sql);
2023-09-12 15:26:12 +02:00
if ($result) {
2023-09-12 16:50:13 +02:00
if ($this->db->num_rows($result) == 1) {
$passok = false;
2023-09-12 16:50:13 +02:00
$obj = $this->db->fetch_object($result);
if ($obj) {
$passcrypted = $obj->pass_crypted;
// Check crypted password
$cryptType = '';
if (getDolGlobalString('DATABASE_PWD_ENCRYPTED')) {
$cryptType = getDolGlobalString('DATABASE_PWD_ENCRYPTED');
}
// By default, we use default setup for encryption rule
if (!in_array($cryptType, array('auto'))) {
$cryptType = 'auto';
}
// Check crypted password according to crypt algorithm
if ($cryptType == 'auto') {
if ($passcrypted && dol_verifyHash($pass, $passcrypted, '0')) {
$passok = true;
}
}
// Password ok ?
if ($passok) {
$id = $obj->id;
} else {
dol_syslog(__METHOD__ .' Authentication KO bad password for ' . $login . ', cryptType=' . $cryptType, LOG_NOTICE);
sleep(1); // Brut force protection. Must be same delay when login is not valid
return -3;
}
}
} else {
dol_syslog(__METHOD__ . ' Many third-party account found for login"' . $login . '" and site="dolibarr_portal"', LOG_ERR);
return -2;
2023-09-12 15:26:12 +02:00
}
} else {
2023-09-12 16:50:13 +02:00
$this->error = $this->db->lasterror();
2023-09-12 15:26:12 +02:00
return -1;
}
return $id;
}
2023-09-12 15:12:30 +02:00
}