2023-09-12 13:12:30 +00:00
< ? php
2024-03-03 15:00:13 +00:00
/* Copyright ( C ) 2023 - 2024 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2024 Frédéric France < frederic . france @ free . fr >
2026-08-05 08:19:17 +00:00
* Copyright ( C ) 2024 - 2026 MDW < mdeweerd @ users . noreply . github . com >
2025-09-20 17:11:31 +00:00
* Copyright ( C ) 2025 Schaffhauser sébastien < sebastien @ webmaster67 . fr >
2024-03-03 15:00:13 +00:00
* 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 />.
*/
2024-03-19 00:29:52 +00:00
/**
* \file htdocs / webportal / class / context . class . php
* \ingroup webportal
* \brief File of context class for WebPortal
*/
2023-09-12 13:12:30 +00:00
require_once __DIR__ . '/controller.class.php' ;
2023-09-28 08:44:22 +00:00
require_once __DIR__ . '/webPortalTheme.class.php' ;
2023-09-12 13:12:30 +00:00
/**
* Class Context
*/
class Context
{
2023-09-12 13:26:12 +00:00
/**
2024-10-29 21:44:47 +00:00
* @ var ? Context Singleton
2023-09-12 13:26:12 +00:00
* @ access private
* @ static
*/
private static $_instance = null ;
2023-09-12 13:12:30 +00:00
2023-09-12 14:50:13 +00:00
/**
2024-10-29 21:44:47 +00:00
* @ var DoliDB $db Database handler
2023-09-12 14:50:13 +00:00
*/
public $db ;
2024-03-03 15:00:13 +00:00
/**
* @ var string
*/
2023-09-12 13:26:12 +00:00
public $title ;
2024-03-03 15:00:13 +00:00
/**
* @ var string
*/
2023-09-12 13:26:12 +00:00
public $desc ;
2023-09-12 13:12:30 +00:00
2024-03-03 15:00:13 +00:00
/**
* @ var string
*/
2023-09-12 13:26:12 +00:00
public $meta_title ;
2024-03-03 15:00:13 +00:00
/**
* @ var string
*/
2023-09-12 13:26:12 +00:00
public $meta_desc ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
/**
* The application name
2024-01-22 13:33:35 +00:00
* @ var string $appliName
2023-09-12 13:26:12 +00:00
*/
public $appliName ;
2023-09-12 13:12:30 +00:00
2024-03-03 15:00:13 +00:00
/**
* @ var string
*/
2023-09-12 13:26:12 +00:00
public $controller ;
2024-03-03 15:00:13 +00:00
/**
* @ var boolean
*/
2023-09-12 13:26:12 +00:00
public $controller_found = false ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
/**
* @ var stdClass []
*/
private $controllers = array ();
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
/**
* @ var Controller $controllerInstance
*/
public $controllerInstance ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
/**
* for internal error msg
* @ var string error
*/
public $error ;
2023-09-12 13:12:30 +00:00
2024-01-23 15:56:43 +00:00
/**
2024-09-30 08:07:20 +00:00
* @ var string [] errors
2024-01-23 15:56:43 +00:00
*/
public $errors = array ();
2024-01-22 13:33:35 +00:00
/**
* @ var string Action
*/
2023-09-12 13:26:12 +00:00
public $action ;
2023-09-12 13:12:30 +00:00
2024-09-30 08:07:20 +00:00
/**
* @ var string tpl directory
*/
2023-09-12 13:26:12 +00:00
public $tplDir ;
2024-09-30 08:07:20 +00:00
/**
* @ var string tpl path
*/
2023-09-12 13:26:12 +00:00
public $tplPath ;
2024-09-30 08:07:20 +00:00
/**
* @ var stdClass
*/
2023-09-12 13:26:12 +00:00
public $topMenu ;
2023-09-12 13:12:30 +00:00
2024-09-30 08:07:20 +00:00
/**
* @ var string root url
*/
2023-09-12 13:26:12 +00:00
public $rootUrl ;
2023-09-12 13:12:30 +00:00
2025-12-05 11:13:24 +00:00
/**
* @ var string cdn url for public / includes
*/
public $cdnUrl ;
2024-10-28 18:49:01 +00:00
/**
* @ var string []
*/
2023-09-12 13:26:12 +00:00
public $menu_active = array ();
2023-09-12 13:12:30 +00:00
2024-09-30 08:07:20 +00:00
/**
* @ var array { mesgs : string [], warnings : string [], errors : string []} | array {} event messages
*/
2023-09-12 13:26:12 +00:00
public $eventMessages = array ();
2023-09-12 13:12:30 +00:00
2024-09-30 08:07:20 +00:00
/**
* @ var string token key
*/
2023-09-28 08:44:22 +00:00
public $tokenKey = 'token' ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
/**
2024-01-22 12:59:49 +00:00
* Current object of page
2023-09-12 13:26:12 +00:00
* @ var object $object
*/
public $object ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
/**
2025-09-29 18:41:20 +00:00
* @ var User Logged user
2023-09-12 13:26:12 +00:00
*/
public $logged_user = null ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
/**
2025-09-29 18:41:20 +00:00
* @ var Societe Logged third - party
2023-09-12 13:26:12 +00:00
*/
public $logged_thirdparty = null ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
/**
2025-09-29 18:41:20 +00:00
* @ var WebPortalMember Logged member
2023-09-12 13:26:12 +00:00
*/
public $logged_member = null ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
/**
2025-09-29 18:41:20 +00:00
* @ var WebPortalPartnership Logged partnership
2023-09-12 13:26:12 +00:00
*/
public $logged_partnership = null ;
2023-09-12 13:12:30 +00:00
2023-09-28 08:44:22 +00:00
/**
* @ var WebPortalTheme Theme data
*/
public $theme ;
2023-09-12 13:12:30 +00:00
/**
* Constructor
*
* @ return void
*/
2023-09-12 13:26:12 +00:00
private function __construct ()
{
2025-09-14 17:45:41 +00:00
global $db ;
2023-09-12 14:50:13 +00:00
$this -> db = $db ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
$this -> tplDir = __DIR__ . '/../' ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
$this -> getControllerUrl ();
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
$this -> topMenu = new stdClass ();
2023-09-12 13:12:30 +00:00
2023-09-28 10:32:11 +00:00
$this -> tplPath = realpath ( __DIR__ . '/../../public/webportal/tpl' );
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
$this -> controller = GETPOST ( 'controller' , 'aZ09' ); // for security, limited to 'aZ09'
$this -> action = GETPOST ( 'action' , 'aZ09' ); // for security, limited to 'aZ09'
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
if ( empty ( $this -> controller )) {
$this -> controller = 'default' ;
}
2023-09-12 13:12:30 +00:00
2024-02-02 22:46:12 +00:00
$this -> appliName = getDolGlobalString ( 'WEBPORTAL_TITLE' , getDolGlobalString ( 'MAIN_INFO_SOCIETE_NOM' ));
2023-09-12 13:12:30 +00:00
2023-11-17 21:15:22 +00:00
//$this->generateNewToken();
2023-09-12 13:12:30 +00:00
2025-11-19 14:14:53 +00:00
$this -> initController ( false );
2023-09-12 13:12:30 +00:00
2025-09-14 17:45:41 +00:00
// Init of base URL. Must be the public URL.
2023-09-12 13:26:12 +00:00
$this -> rootUrl = self :: getRootConfigUrl ();
2023-09-28 08:44:22 +00:00
2025-12-05 11:13:24 +00:00
// Init of CDN URL. Must be the public URL.
// BECAUSE IN SOME CASES IT COULD BE IMPORTANT TO HIDE PUBLIC URL BUT YOU CAN SET A CDN URL IN HIDDEN CONF
$this -> cdnUrl = getDolGlobalString ( 'WEBPORTAL_CDN_URL' , dol_buildpath ( '/public/includes/' , 3 ));
$this -> cdnUrl = rtrim ( trim ( $this -> cdnUrl ), '/' );
2023-09-28 08:44:22 +00:00
2025-11-19 14:14:53 +00:00
$this -> theme = new WebPortalTheme ( false );
2023-09-12 13:26:12 +00:00
}
2023-09-12 13:12:30 +00:00
/**
* Singleton method to create one instance of this object
*
2023-09-12 14:50:13 +00:00
* @ return Context Instance
2023-09-12 13:12:30 +00:00
*/
public static function getInstance ()
{
if ( is_null ( self :: $_instance )) {
2023-09-12 13:26:12 +00:00
self :: $_instance = new Context ();
2023-09-12 13:12:30 +00:00
}
return self :: $_instance ;
}
/**
2023-09-12 13:26:12 +00:00
* Init controller
*
2025-11-19 14:14:53 +00:00
* @ param bool $init_theme Init theme properties
2024-01-22 13:33:35 +00:00
* @ return void
2023-09-12 13:12:30 +00:00
*/
2025-11-19 14:14:53 +00:00
public function initController ( $init_theme = true )
2023-09-12 13:12:30 +00:00
{
2025-09-12 13:20:32 +00:00
global $hookmanager ;
2023-09-12 13:12:30 +00:00
$defaultControllersPath = __DIR__ . '/../controllers/' ;
// define controllers definition
2023-09-28 09:47:18 +00:00
$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' );
2026-03-20 15:45:18 +00:00
$this -> addControllerDefinition ( 'ficheinterlist' , $defaultControllersPath . 'ficheinterlist.controller.class.php' , 'FicheinterListController' );
$this -> addControllerDefinition ( 'ticketlist' , $defaultControllersPath . 'ticketlist.controller.class.php' , 'TicketListController' );
2023-09-28 09:47:18 +00:00
$this -> addControllerDefinition ( 'membercard' , $defaultControllersPath . 'membercard.controller.class.php' , 'MemberCardController' );
$this -> addControllerDefinition ( 'partnershipcard' , $defaultControllersPath . 'partnershipcard.controller.class.php' , 'PartnershipCardController' );
2025-09-20 17:11:31 +00:00
//** below the addition of DocumentListController adding files by third party attached documents
$this -> addControllerDefinition ( 'documentlist' , $defaultControllersPath . 'documentlist.controller.class.php' , 'DocumentListController' );
//** Below is the addition to the menu of the DocumentUtileController.class.php controller in order to share via the GED (documents) "Documentscomptes"
2026-08-12 21:04:50 +00:00
$this -> addControllerDefinition ( 'shareddocuments' , $defaultControllersPath . 'shareddocuments.controller.class.php' , 'SharedDocumentsController' );
2025-11-19 14:14:53 +00:00
$this -> addControllerDefinition ( 'viewimage' , $defaultControllersPath . 'viewimage.controller.class.php' , 'ViewImageController' );
2023-09-12 13:12:30 +00:00
2025-09-12 13:20:32 +00:00
// Hooks for init controller
$hookmanager -> initHooks ( array ( 'webportaldao' ));
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'initController' , $parameters , $this );
2023-09-12 13:12:30 +00:00
// search for controller
$this -> controllerInstance = new Controller ();
if ( isset ( $this -> controllers [ $this -> controller ]) && file_exists ( $this -> controllers [ $this -> controller ] -> path )) {
2023-09-12 13:26:12 +00:00
require_once $this -> controllers [ $this -> controller ] -> path ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
if ( class_exists ( $this -> controllers [ $this -> controller ] -> class )) {
$this -> controllerInstance = new $this -> controllers [ $this -> controller ] -> class ();
$this -> setControllerFound ();
}
}
2025-11-19 14:14:53 +00:00
if ( $init_theme ) {
$this -> theme -> init ();
}
2023-09-12 13:12:30 +00:00
}
/**
2023-09-12 13:26:12 +00:00
* Add controller definition
*
2023-09-12 14:50:13 +00:00
* @ param string $controller Name
* @ param string $path Path
* @ param string $className Class name
2023-09-12 13:12:30 +00:00
* @ return bool
*/
2023-09-12 13:26:12 +00:00
public function addControllerDefinition ( $controller , $path , $className )
{
$fileName = basename ( $path );
2023-09-28 09:47:18 +00:00
$needle = '.controller.class.php' ;
2023-09-12 13:26:12 +00: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 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
$this -> controllers [ $controller ] = new stdClass ();
$this -> controllers [ $controller ] -> path = $path ;
$this -> controllers [ $controller ] -> class = $className ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
return true ;
}
2023-09-12 13:12:30 +00:00
/**
* Set controller found
*
* @ return void
*/
2023-09-12 13:26:12 +00:00
public function setControllerFound ()
{
$this -> controller_found = true ;
}
2023-09-12 13:12:30 +00:00
/**
2023-09-12 13:26:12 +00:00
* Get WebPortal root url
*
2023-09-12 13:12:30 +00:00
* @ return string Web Portal root url
*/
2024-03-19 00:29:52 +00:00
public static function getRootConfigUrl ()
2023-09-12 13:26:12 +00:00
{
2025-09-14 17:45:41 +00:00
// Init of base URL
2024-02-09 14:58:49 +00:00
if ( getDolGlobalString ( 'WEBPORTAL_ROOT_URL' )) {
$rootUrl = getDolGlobalString ( 'WEBPORTAL_ROOT_URL' );
2023-09-12 13:26:12 +00:00
if ( substr ( $rootUrl , - 1 ) !== '/' ) {
$rootUrl .= '/' ;
}
} else {
2025-09-14 17:45:41 +00:00
$rootUrl = dol_buildpath ( '/public/webportal/' , 3 ); // Return the public URL for external access
2023-09-12 13:26:12 +00:00
}
return $rootUrl ;
}
/**
* Get root url
*
2023-09-12 14:50:13 +00:00
* @ param string $controller Controller name
2024-11-06 22:57:45 +00:00
* @ param string | array < string , mixed > $moreParams More parameters
2024-01-22 12:59:49 +00:00
* @ param bool $addToken Add token hash only if $controller is set
2023-09-12 14:50:13 +00:00
* @ return string
2023-09-12 13:12:30 +00:00
* @ deprecated see getControllerUrl ()
*/
2024-03-03 15:00:13 +00:00
public function getRootUrl ( $controller = '' , $moreParams = '' , $addToken = true )
2023-09-12 13:26:12 +00:00
{
2023-09-12 13:12:30 +00:00
return self :: getControllerUrl ( $controller , $moreParams , $addToken );
}
/**
* Get controller url according to context
2023-09-12 13:26:12 +00:00
*
2023-09-12 14:50:13 +00:00
* @ param string $controller Controller name
2024-11-06 22:57:45 +00:00
* @ param string | array < string , mixed > $moreParams More parameters
2023-09-12 14:50:13 +00:00
* @ param bool $addToken Add token hash only if controller is set
* @ return string
2023-09-12 13:12:30 +00:00
*/
2024-03-03 15:00:13 +00:00
public function getControllerUrl ( $controller = '' , $moreParams = '' , $addToken = true )
2023-09-12 13:12:30 +00:00
{
2023-11-17 21:27:39 +00:00
// TODO : addToken parameter on auto to detect (create or edit) action and add token on url
2023-09-12 13:26:12 +00:00
$url = $this -> rootUrl ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
if ( empty ( $controller )) {
// because can be called without params to get only rootUrl
return $url ;
}
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
$Tparams = array ();
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
$Tparams [ 'controller' ] = $controller ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
if ( ! empty ( $addToken )) {
$Tparams [ $this -> tokenKey ] = $this -> newToken ();
}
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
return self :: getPublicControllerUrl ( $controller , $moreParams , $Tparams );
2023-09-12 13:12:30 +00:00
}
/**
* Generate public controller URL
* Used for external link ( like email or web page )
* so remove token and contextual behavior associate with current user
*
2024-11-06 22:57:45 +00:00
* @ param string $controller Controller
* @ param string | array < string , mixed > $moreParams More parameters
* @ param array < string , mixed > $Tparams Parameters
2023-09-12 14:05:24 +00:00
* @ return string
2023-09-12 13:12:30 +00:00
*/
2024-03-19 00:29:52 +00:00
public static function getPublicControllerUrl ( $controller = '' , $moreParams = '' , $Tparams = array ())
2023-09-12 13:12:30 +00: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 13:26:12 +00:00
if ( ! empty ( $moreParams ) && is_array ( $moreParams )) {
2024-03-19 00:29:52 +00:00
if ( isset ( $moreParams [ 'controller' ])) {
unset ( $moreParams [ 'controller' ]);
}
2023-09-12 13:26:12 +00:00
if ( ! empty ( $moreParams )) {
foreach ( $moreParams as $paramKey => $paramVal ) {
2023-09-12 13:12:30 +00:00
$Tparams [ $paramKey ] = $paramVal ;
}
}
}
2023-09-12 13:26:12 +00:00
if ( ! empty ( $Tparams )) {
2023-09-12 13:12:30 +00:00
$TCompiledAttr = array ();
foreach ( $Tparams as $key => $value ) {
2023-09-12 13:26:12 +00:00
$TCompiledAttr [] = $key . '=' . $value ;
2023-09-12 13:12:30 +00:00
}
2023-09-12 13:26:12 +00:00
$url .= '?' . implode ( " & " , $TCompiledAttr );
2023-09-12 13:12:30 +00:00
}
// if $moreParams is a string
if ( ! empty ( $moreParams ) && ! is_array ( $moreParams )) {
if ( empty ( $Tparams )) {
2024-03-19 00:29:52 +00:00
if ( $moreParams [ 0 ] !== '?' ) {
$url .= '?' ;
}
if ( $moreParams [ 0 ] === '&' ) {
$moreParams = substr ( $moreParams , 1 );
}
2023-09-12 13:12:30 +00:00
}
$url .= $moreParams ;
}
return $url ;
}
/**
2023-09-12 13:26:12 +00:00
* Url origin
*
2023-09-12 14:50:13 +00:00
* @ param bool $withRequestUri With request URI
* @ param bool $use_forwarded_host Use formatted host
* @ return string
2023-09-12 13:12:30 +00:00
*/
2024-03-19 00:29:52 +00:00
public static function urlOrigin ( $withRequestUri = true , $use_forwarded_host = false )
2023-09-12 13:26:12 +00:00
{
$s = $_SERVER ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00: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 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
$url = $protocol . '://' . $host ;
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
if ( $withRequestUri ) {
$url .= $s [ 'REQUEST_URI' ];
}
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
return $url ;
}
2023-09-12 13:12:30 +00:00
/**
2023-09-12 13:26:12 +00:00
* Check if user is logged
*
2023-09-12 14:50:13 +00:00
* @ return bool
2023-09-12 13:12:30 +00:00
*/
2023-09-12 13:26:12 +00:00
public function userIsLog ()
{
2025-09-12 13:20:32 +00:00
global $hookmanager ;
// Hooks for security access
$hookmanager -> initHooks ( array ( 'webportaldao' ));
$parameters = array ();
$reshook = $hookmanager -> executeHooks ( 'userIsLog' , $parameters , $this );
if ( $reshook > 0 ) {
return ! empty ( $hookmanager -> resArray [ 'userIsLog' ]);
}
2023-09-12 13:26:12 +00:00
if ( ! empty ( $_SESSION [ " webportal_logged_thirdparty_account_id " ])) {
return true ;
2025-11-25 13:39:30 +00:00
} elseif ( ! empty ( $_SESSION [ " webportal_logged_member_account_id " ])) {
return true ;
2023-09-12 13:26:12 +00:00
} else {
return false ;
}
}
2023-09-12 13:12:30 +00:00
/**
2023-09-12 13:26:12 +00:00
* Is menu enabled ?
*
2023-09-12 14:50:13 +00:00
* @ param string $menuName Menu name
2023-09-12 13:12:30 +00:00
* @ return bool
*/
public function menuIsActive ( $menuName )
{
return in_array ( $menuName , $this -> menu_active );
}
/**
2023-09-12 13:26:12 +00:00
* Set errors
*
2024-09-30 08:07:20 +00:00
* @ param string | string [] $errors Errors
2023-09-12 14:50:13 +00:00
* @ return void
2023-09-12 13:12:30 +00:00
*/
public function setError ( $errors )
{
2024-03-19 00:29:52 +00:00
if ( ! is_array ( $errors )) {
$errors = array ( $errors );
}
if ( ! isset ( $_SESSION [ 'webportal_errors' ])) {
$_SESSION [ 'webportal_errors' ] = array ();
}
2023-09-12 13:26:12 +00:00
foreach ( $errors as $msg ) {
2024-03-19 00:29:52 +00:00
if ( ! in_array ( $msg , $_SESSION [ 'webportal_errors' ])) {
$_SESSION [ 'webportal_errors' ][] = $msg ;
}
2023-09-12 13:12:30 +00:00
}
}
/**
2023-09-12 13:26:12 +00:00
* Get errors
*
2023-09-12 13:12:30 +00: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 13:26:12 +00:00
*
2023-09-12 13:12:30 +00:00
* @ return void
*/
public function clearErrors ()
{
unset ( $_SESSION [ 'webportal_errors' ]);
$this -> errors = array ();
}
/**
2023-09-12 14:50:13 +00: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 13:26:12 +00:00
*
2023-09-12 14:50:13 +00:00
* @ param string | string [] $mesgs Message string or array
2026-08-05 08:19:17 +00:00
* @ param '' | 'mesgs' | 'warnings' | 'errors' $style Which style to use ( 'mesgs' by default , 'warnings' , 'errors' )
2023-09-12 14:50:13 +00:00
* @ return void
2023-09-12 13:26:12 +00: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 14:50:13 +00: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 13:12:30 +00:00
*
2024-09-30 08:07:20 +00:00
* @ param string $mesg Message string
* @ param string [] | null $mesgs Message array
2026-08-05 08:19:17 +00:00
* @ param '' | 'mesgs' | 'warnings' | 'errors' $style Which style to use ( 'mesgs' by default , 'warnings' , 'errors' )
2023-09-12 14:05:24 +00:00
* @ return void
2023-09-12 13:26:12 +00:00
*/
2023-09-12 14:05:24 +00:00
public function setEventMessages ( $mesg , $mesgs , $style = 'mesgs' )
2023-09-12 13:26:12 +00: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 14:05:24 +00:00
if ( ! in_array (( string ) $style , array ( 'mesgs' , 'warnings' , 'errors' ))) {
2024-07-03 00:30:53 +00:00
dol_print_error ( null , 'Bad parameter style=' . $style . ' for setEventMessages' );
2023-09-12 13:26:12 +00: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 13:12:30 +00:00
* @ return int
*/
2023-09-12 13:26:12 +00:00
public function loadEventMessages ()
{
if ( ! empty ( $_SESSION [ 'webportal_events' ])) {
$this -> eventMessages = $_SESSION [ 'webportal_events' ];
return 1 ;
}
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
return 0 ;
}
2023-09-12 13:12:30 +00:00
/**
* Clear event messages
2023-09-12 13:26:12 +00:00
*
2023-09-12 13:12:30 +00: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 14:50:13 +00: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 13:12:30 +00:00
*
* @ return string
*/
2023-11-17 21:15:22 +00:00
public function newToken ()
2023-09-12 13:26:12 +00:00
{
2023-11-17 21:15:22 +00:00
return newToken ();
2023-09-12 13:26:12 +00:00
}
2023-09-12 13:12:30 +00:00
/**
* Generate new token .
2023-11-17 21:15:22 +00:00
* @ deprecated see main
2023-09-12 14:50:13 +00:00
* @ return string
2023-09-12 13:12:30 +00:00
*/
2023-11-17 21:15:22 +00:00
protected function generateNewToken ()
2023-09-12 13:26:12 +00:00
{
2023-11-17 21:15:22 +00:00
$currentToken = $this -> newToken ();
2023-09-12 13:26:12 +00:00
// Creation of a token against CSRF vulnerabilities
2024-03-19 00:29:52 +00:00
if ( ! defined ( 'NOTOKENRENEWAL' ) || empty ( $currentToken )) {
2023-09-12 13:26:12 +00:00
// Rolling token at each call ($_SESSION['token'] contains token of previous page)
2023-11-17 21:15:22 +00:00
if ( isset ( $_SESSION [ 'newtoken' ])) {
$_SESSION [ 'token' ] = $_SESSION [ 'newtoken' ];
2023-09-12 13:26:12 +00:00
}
// Save what will be next token. Into forms, we will add param $context->newToken();
2026-06-06 14:51:13 +00:00
$token = bin2hex ( random_bytes ( 32 ));
2023-11-17 21:15:22 +00:00
$_SESSION [ 'newtoken' ] = $token ;
2023-09-12 13:26:12 +00:00
return $token ;
} else {
2023-11-17 21:15:22 +00:00
return $this -> newToken ();
2023-09-12 13:26:12 +00:00
}
}
2023-09-12 13:12:30 +00:00
2023-09-12 13:26:12 +00:00
/**
* Get token url
*
2023-09-12 14:50:13 +00:00
* @ return string | null
2023-09-12 13:12:30 +00:00
*/
2024-01-22 13:33:35 +00:00
public function getUrlToken ()
2023-09-12 13:26:12 +00:00
{
2024-01-22 13:33:35 +00:00
$token = $this -> newToken ();
2023-09-12 13:26:12 +00:00
if ( $token ) {
2024-01-22 13:33:35 +00:00
return '&' . $this -> tokenKey . '=' . $this -> newToken ();
2023-09-12 13:26:12 +00:00
}
2024-01-22 13:33:35 +00:00
return null ;
2023-09-12 13:26:12 +00:00
}
2023-09-12 13:12:30 +00:00
/**
2023-09-12 13:26:12 +00:00
* Get token input for form
*
2023-09-12 13:12:30 +00:00
* @ return string | null
*/
2024-01-22 13:33:35 +00:00
public function getFormToken ()
2023-09-12 13:26:12 +00:00
{
2024-01-22 13:33:35 +00:00
$token = $this -> newToken ();
2023-09-12 13:26:12 +00:00
if ( $token ) {
2024-01-22 13:33:35 +00:00
return '<input type="hidden" name="' . $this -> tokenKey . '" value="' . $this -> newToken () . '" />' ;
2023-09-12 13:26:12 +00:00
}
2024-01-22 13:33:35 +00:00
return null ;
2023-09-12 13:26:12 +00:00
}
/**
* Try to find the third - party account id from
*
2023-09-12 14:50:13 +00:00
* @ param string $login Login
* @ param string $pass Password
2024-01-10 08:20:49 +00:00
* @ return int Third - party account id || < 0 if error
2023-09-12 13:26:12 +00:00
*/
public function getThirdPartyAccountFromLogin ( $login , $pass )
{
$id = 0 ;
2024-01-10 08:20:49 +00:00
$sql = " SELECT sa.rowid as id, sa.pass_crypted " ;
2023-09-12 14:50:13 +00:00
$sql .= " FROM " . $this -> db -> prefix () . " societe_account as sa " ;
2024-11-15 23:33:54 +00:00
$sql .= " WHERE sa.login = ' " . $this -> db -> escape ( $login ) . " ' " ;
2024-01-10 08:20:49 +00:00
//$sql .= " AND BINARY sa.pass_crypted = '" . $this->db->escape($pass) . "'"; // case sensitive
2023-09-12 13:26:12 +00: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 14:50:13 +00:00
$result = $this -> db -> query ( $sql );
2023-09-12 13:26:12 +00:00
if ( $result ) {
2023-09-12 14:50:13 +00:00
if ( $this -> db -> num_rows ( $result ) == 1 ) {
2024-01-10 08:20:49 +00:00
$passok = false ;
2023-09-12 14:50:13 +00:00
$obj = $this -> db -> fetch_object ( $result );
2024-01-10 08:20:49 +00:00
if ( $obj ) {
$passcrypted = $obj -> pass_crypted ;
2025-11-25 13:39:30 +00:00
// Check crypted password according to crypt algorithm
if ( $passcrypted && dol_verifyHash ( $pass , $passcrypted , '0' )) {
$passok = true ;
2024-01-10 08:20:49 +00:00
}
2025-11-25 13:39:30 +00:00
// Password ok ?
if ( $passok ) {
$id = $obj -> id ;
} else {
dol_syslog ( __METHOD__ . ' Authentication KO bad password for ' . $login . ', cryptType=auto' , LOG_NOTICE );
sleep ( 1 ); // Brut force protection. Must be same delay when login is not valid
return - 3 ;
2024-01-10 08:20:49 +00:00
}
2025-11-25 13:39:30 +00:00
}
} else {
dol_syslog ( __METHOD__ . ' Many third-party account found for login"' . $login . '" and site="dolibarr_portal"' , LOG_ERR );
return - 2 ;
}
} else {
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
return $id ;
}
/**
* Try to find the member account id from
*
* @ param string $login Login
* @ param string $pass Password
* @ return int Member account id || < 0 if error
*/
public function getMemberAccountFromLogin ( $login , $pass )
{
$id = 0 ;
$sql = " SELECT a.rowid as id, a.pass_crypted " ;
$sql .= " FROM " . $this -> db -> prefix () . " adherent as a " ;
$sql .= " WHERE a.login = ' " . $this -> db -> escape ( $login ) . " ' " ;
$sql .= " AND a.statut = 1 " ;
$sql .= " AND a.entity IN ( " . getEntity ( 'member' ) . " ) " ;
dol_syslog ( __METHOD__ . ' Try to find the member account id for login"' . $login . '"' , LOG_DEBUG );
$result = $this -> db -> query ( $sql );
if ( $result ) {
if ( $this -> db -> num_rows ( $result ) == 1 ) {
$passok = false ;
$obj = $this -> db -> fetch_object ( $result );
if ( $obj ) {
$passcrypted = $obj -> pass_crypted ;
2024-01-10 08:20:49 +00:00
// Check crypted password according to crypt algorithm
2025-11-25 13:39:30 +00:00
if ( $passcrypted && dol_verifyHash ( $pass , $passcrypted , '0' )) {
$passok = true ;
2024-01-10 08:20:49 +00:00
}
// Password ok ?
if ( $passok ) {
$id = $obj -> id ;
} else {
2025-11-25 13:39:30 +00:00
dol_syslog ( __METHOD__ . ' Authentication KO bad password for ' . $login . ', cryptType=auto' , LOG_NOTICE );
2024-01-10 08:20:49 +00:00
sleep ( 1 ); // Brut force protection. Must be same delay when login is not valid
return - 3 ;
}
}
} else {
2025-11-25 13:39:30 +00:00
dol_syslog ( __METHOD__ . ' Many member account found for login"' . $login . '"' , LOG_ERR );
2024-01-10 08:20:49 +00:00
return - 2 ;
2023-09-12 13:26:12 +00:00
}
} else {
2023-09-12 14:50:13 +00:00
$this -> error = $this -> db -> lasterror ();
2023-09-12 13:26:12 +00:00
return - 1 ;
}
return $id ;
}
2023-09-12 13:12:30 +00:00
}