getName( $authentication_type_id ); $session_id = getCookiePostGetVariable( $session_name, false ); if ( is_string( $session_id ) == false ) { $session_id = false; } return $session_id; } /** * Returns Station ID from _COOKIE, _POST, then _GET. * @return bool|mixed */ function getStationID() { $station_id = getCookiePostGetVariable( 'StationID', false ); //Check to see if there is a "sticky" user agent based Station ID defined. if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && $_SERVER['HTTP_USER_AGENT'] != '' && stripos( $_SERVER['HTTP_USER_AGENT'], 'StationID:' ) !== false ) { if ( preg_match( '/StationID:\s?([a-zA-Z0-9]{30,64})/i', $_SERVER['HTTP_USER_AGENT'], $matches ) > 0 ) { if ( isset( $matches[1] ) ) { Debug::Text( ' Found StationID in user agent, forcing to that instead!', __FILE__, __LINE__, __METHOD__, 10 ); $station_id = $matches[1]; } } } if ( is_string( $station_id ) == false ) { $station_id = false; } return $station_id; } /** * Handle temporarily overriding user preferences based on Cookie/Post/Get variables. * This is useful for ensuring there is always consistent date/time formats and timezones when accessing the API for multiple users. * @param $user_obj UserFactory * @return bool */ function handleOverridePreferences( $user_obj ) { $user_pref_obj = $user_obj->getUserPreferenceObject(); /** @var UserPreferenceFactory $user_pref_obj */ //Allow for BASE64 encoding of the JSON string, as flutter only allows RFC 6265 cookie values, which JSON is not. $raw_override_cookie = getCookiePostGetVariable( 'OverrideUserPreference' ); if ( $raw_override_cookie != '' && strpos( $raw_override_cookie, '{' ) !== 0 ) { $raw_override_cookie = base64_decode( $raw_override_cookie ); } if ( $raw_override_cookie != '' ) { $override_preferences = json_decode( $raw_override_cookie, true ); if ( is_array( $override_preferences ) && count( $override_preferences ) > 0 ) { //If a user_id is specified, pull the timezone for that user and default to it, rather than the UI having to do a lookup itself and passing it through. if ( isset( $override_preferences['user_id'] ) && TTUUID::isUUID( $override_preferences['user_id'] ) && $user_obj->getId() != $override_preferences['user_id'] ) { $uplf = TTnew( 'UserPreferenceListFactory' ); /** @var UserPreferenceListFactory $uplf */ $uplf->getByUserID( $override_preferences['user_id'] ); //Cached if ( $uplf->getRecordCount() > 0 ) { $override_preferences = array_merge( $uplf->getCurrent()->getObjectAsArray( [ 'time_zone' => true ] ), $override_preferences ); } //If switching to another users timezone, default to appending the timezone on the end of each timestamp unless otherwise specified. if ( !isset( $override_preferences['time_format'] ) && strpos( $user_pref_obj->getTimeFormat(), 'T' ) === false ) { $override_preferences['time_format'] = $user_pref_obj->getTimeFormat() . ' T'; } } Debug::Arr( $override_preferences, 'Overridden Preferences: ', __FILE__, __LINE__, __METHOD__, 10 ); $user_pref_obj->setObjectFromArray( $override_preferences ); } } $user_pref_obj->setDateTimePreferences(); Debug::text( 'Locale Cookie: ' . TTi18n::getLocaleCookie(), __FILE__, __LINE__, __METHOD__, 10 ); //If override preferences specifies a language, do not save the users preferences, just use it dynamically instead. if ( !isset( $override_preferences['language'] ) && TTi18n::getLocaleCookie() != '' && $user_pref_obj->getLanguage() !== TTi18n::getLanguageFromLocale( TTi18n::getLocaleCookie() ) ) { Debug::text( 'Changing User Preference Language to match cookie...', __FILE__, __LINE__, __METHOD__, 10 ); $user_pref_obj->setLanguage( TTi18n::getLanguageFromLocale( TTi18n::getLocaleCookie() ) ); if ( $user_pref_obj->isValid() ) { $user_pref_obj->Save( false ); } } else { Debug::text( 'User Preference Language matches cookie!', __FILE__, __LINE__, __METHOD__, 10 ); } if ( isset( $_GET['language'] ) && $_GET['language'] != '' ) { TTi18n::setLocale( $_GET['language'] ); //Sets master locale } else { TTi18n::setLanguage( $user_pref_obj->getLanguage() ); TTi18n::setCountry( $user_obj->getCountry() ); TTi18n::setLocale(); //Sets master locale } TTi18n::setLocaleCookie(); //Make sure locale cookie is set so APIGlobal.js.php can read it. return $user_pref_obj; } /** * @return bool|string */ function getJSONError() { $retval = false; if ( function_exists( 'json_last_error' ) ) { //Handle PHP v5.3 and older. switch ( json_last_error() ) { case JSON_ERROR_NONE: break; case JSON_ERROR_DEPTH: $retval = 'Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $retval = 'Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: $retval = 'Unexpected control character found'; break; case JSON_ERROR_SYNTAX: $retval = 'Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: $retval = 'Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: $retval = 'Unknown error'; break; } } return $retval; } //Make sure cron job information is always logged. //Don't do this until log rotation is implemented. /* Debug::setEnable( TRUE ); Debug::setBufferOutput( TRUE ); Debug::setEnableLog( TRUE ); if ( Debug::getVerbosity() <= 1 ) { Debug::setVerbosity( 1 ); } */ ?>