/** * Log user on backend and relay cookie * * @param bool $params Login result of ESB login * @return boolean Success * @throws Exception */ protected static function backendLog($params) { $ini = eZIni::instance('merck.ini'); // Create backend request $backendUrl = $ini->variable('WSMobile', 'BackEndUrl'); $parameters = array( 'params' => MMUserLogin::encryptText(json_encode($params)), 'action' => 'update' ); $url = rtrim($backendUrl, '/') . '/loginActions.php?' . http_build_query($parameters); // Send Backend request $c = curl_init(); curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_NOBODY, true); curl_setopt($c, CURLOPT_HEADER, true); if ( $ini->hasVariable('WSMobile', 'BackEndHeader') ) { /* @type $headers array */ $headersList = array(); $headers = $ini->variable('WSMobile', 'BackEndHeader'); foreach ( $headers as $header => $value ) { $headersList[] = sprintf('%s: %s', $header, $value); } curl_setopt($c, CURLOPT_HTTPHEADER, $headersList); } $basicAuthEnv = ( $ini->hasSection('WSMobile') && $ini->hasVariable('WSMobile', 'BasicAuthEnv') ) ? $ini->variable( 'WSMobile', 'BasicAuthEnv' ) : null; $env = ( eZINI::instance()->hasSection( 'Environment' ) && eZINI::instance()->hasVariable( 'Environment', 'Type' ) ) ? eZINI::instance()->variable( 'Environment', 'Type' ) : null; if ( is_array( $basicAuthEnv ) && in_array( $env, $basicAuthEnv ) ) { $username = $ini->variable( 'WSMobile', "BasicAuthUsername" ); $username = $username[$env]; $password = $ini->variable( 'WSMobile', "BasicAuthPassword" ); $password = $password[$env]; curl_setopt($c, CURLOPT_USERPWD, $username . ':' . $password); } $result = curl_exec($c); $errno = curl_errno($c); $error = curl_error($c); curl_close($c); if ( $result === false ) { throw new Exception(sprintf('%s (%s)', $error, $errno), 25); } // Relayed backend cookies $setCookies = array(); if ( preg_match_all('/Set-Cookie: (.*)\b/', $result, $setCookies) !== false ) { $cookies = self::cookieHeaderParse($setCookies[0]); foreach ( $cookies as $cookie ) { $expire = $cookie['expires'] ? $cookie['expires'] : 0; $path = $cookie['path'] ? $cookie['path'] : '/'; // Quick Fix : #38428 $domain = $cookie['domain'] ? str_replace('backendphp', 'www', $cookie['domain']) : ContextTool::domain(); // $domain = $cookie['domain'] ? $cookie['domain'] : ContextTool::domain(); setcookie($cookie['value']['key'], $cookie['value']['value'], $expire, $path, $domain); } } return true; }