LTI Integration Library 5.2.0
PHP class library for building LTI integrations
 
Loading...
Searching...
No Matches
ceLTIc\LTI\Util Class Reference

Class to implement utility methods. More...

Static Public Member Functions

static bool isLtiMessage ()
 Check whether the request received could be an LTI message.
 
static array getRequestParameters ()
 Return GET and POST request parameters (POST parameters take precedence).
 
static void logError (string $message, bool $showSource=true)
 Log an error message.
 
static void logInfo (string $message, bool $showSource=false)
 Log an information message.
 
static void logDebug (string $message, bool $showSource=false)
 Log a debug message.
 
static void logRequest (bool $debugLevel=false)
 Log a request received.
 
static void logForm (string $url, array $params, string $method='POST', bool $debugLevel=false)
 Log a form submission.
 
static void log (string $message, bool $showSource=false)
 Log an error message irrespective of the logging level.
 
static void setMessage (bool $isError, string $message)
 Set an error or warning message.
 
static array getMessages (bool $isError)
 Get the system error or warning messages.
 
static string sendForm (string $url, array $params, string $target='', string $javascript='')
 Generate a web page containing an auto-submitted form of parameters.
 
static never redirect (string $url, array $params)
 Redirect to a URL with query parameters.
 
static void setTestCookie (bool $delete=false)
 Set or delete a test cookie.
 
static string getRandomString (int $length=8)
 Generate a random string.
 
static string stripHtml (string $html)
 Strip HTML tags from a string.
 
static string urlEncode (?string $val)
 URL encode a value.
 
static string valToString (mixed $val, ?string $default='')
 Convert a value to a string.
 
static int float null valToNumber (mixed $val)
 Convert a value to a numeric.
 
static bool valToBoolean (mixed $val)
 Convert a value to a boolean.
 
static object null checkObject (object $obj, string $fullname, bool $required=false, bool $stringValues=false)
 Get the named object element from object.
 
static array checkArray (object $obj, string $fullname, bool $required=false, bool $notEmpty=false)
 Get the named array element from object.
 
static string checkString (object $obj, string $fullname, bool $required=false, ?bool $notEmpty=false, string|array $fixedValue='', bool $overrideStrictMode=false, ?string $default='')
 Get the named string element from object.
 
static string checkUrl (object $obj, string $fullname, bool $required=false)
 Get the named filly-qualified URL element from object.
 
static int float null checkNumber (object $obj, string $fullname, bool $required=false, int|false $minimum=false, bool $minimumExclusive=false, bool $overrideStrictMode=false)
 Get the named number element from object.
 
static int null checkInteger (object $obj, string $fullname, bool $required=false, int|false $minimum=false, bool $minimumExclusive=false, bool $overrideStrictMode=false)
 Get the named integer element from object.
 
static bool null checkBoolean (object $obj, string $fullname, bool $required=false, ?bool $default=null)
 Get the named boolean element from object.
 
static int checkDateTime (object $obj, string $fullname, bool $required=false)
 Get the named number element from object.
 
static mixed jsonDecode (?string $str, bool $associative=false)
 Decode a JSON string.
 
static object cloneObject (object $obj)
 Clone an object and any objects it contains.
 

Public Attributes

const JWT_CLAIM_PREFIX = 'https://purl.imsglobal.org/spec/lti'
 Prefix for standard JWT message claims.
 
const MESSAGE_TYPE_MAPPING
 Mapping for standard message types.
 
const JWT_CLAIM_MAPPING
 Mapping for standard message parameters to JWT claim.
 
const TEST_COOKIE_NAME = 'celtic_lti_test_cookie'
 Name of test cookie.
 

Static Public Attributes

static array $METHOD_NAMES
 List of supported message types and associated class methods.
 
static array $requestParameters = null
 GET and POST request parameters.
 
static LogLevel $logLevel = LogLevel::None
 Current logging level.
 
static bool $strictMode = false
 Whether full compliance with the LTI specification is required.
 
static bool $disableFetchedPublicKeysSave = false
 Whether the automatic saving of fetched valid public keys should be disabled.
 
static int $formSubmissionTimeout = 2
 Delay (in seconds) before a manual button is displayed in case a browser is blocking a form submission.
 

Detailed Description

Class to implement utility methods.

Author
Stephen P Vickers steph.nosp@m.en@s.nosp@m.pvsof.nosp@m.twar.nosp@m.eprod.nosp@m.ucts.nosp@m..com
Licence: http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3

Member Function Documentation

◆ checkArray()

static array ceLTIc\LTI\Util::checkArray ( object $obj,
string $fullname,
bool $required = false,
bool $notEmpty = false )
static

Get the named array element from object.

Parameters
object$objObject containing the element
string$fullnameName of element
bool$requiredTrue if the element must be present
bool$notEmptyTrue if the element must not have an empty value
Returns
array Value of element (or empty string if not found)

References checkString().

Referenced by checkObject(), and ceLTIc\LTI\Service\Groups\get().

◆ checkBoolean()

static bool null ceLTIc\LTI\Util::checkBoolean ( object $obj,
string $fullname,
bool $required = false,
?bool $default = null )
static

Get the named boolean element from object.

Parameters
object$objObject containing the element
string$fullnameName of element (may include a path)
bool$requiredTrue if the element must be present
string | null$defaultValue to return when a conversion is not possible (optional, default is an empty string)
Returns
bool|null Value of element (or null if not found or valid)

Referenced by ceLTIc\LTI\Service\Groups\get(), ceLTIc\LTI\Service\LineItem\get(), and ceLTIc\LTI\Content\FileItem\toJsonldObject().

◆ checkDateTime()

static int ceLTIc\LTI\Util::checkDateTime ( object $obj,
string $fullname,
bool $required = false )
static

Get the named number element from object.

Parameters
object$objObject containing the element
string$fullnameName of element (may include a path)
bool$requiredTrue if the element must be present
Returns
int Value of element (or 0 if not found or valid)

Referenced by ceLTIc\LTI\Service\LineItem\get().

◆ checkInteger()

static int null ceLTIc\LTI\Util::checkInteger ( object $obj,
string $fullname,
bool $required = false,
int|false $minimum = false,
bool $minimumExclusive = false,
bool $overrideStrictMode = false )
static

Get the named integer element from object.

Parameters
object$objObject containing the element
string$fullnameName of element (may include a path)
bool$requiredTrue if the element must be present
int | false$minimumMinimum value (or false is none)
bool$minimumExclusiveTrue if value must exceed the minimum
bool$overrideStrictModeIgnore strict mode setting
Returns
int|null Value of element (or null if not found or valid)

Referenced by checkNumber(), and ceLTIc\LTI\Content\Image\toJsonldObject().

◆ checkNumber()

static int float null ceLTIc\LTI\Util::checkNumber ( object $obj,
string $fullname,
bool $required = false,
int|false $minimum = false,
bool $minimumExclusive = false,
bool $overrideStrictMode = false )
static

Get the named number element from object.

Parameters
object$objObject containing the element
string$fullnameName of element (may include a path)
bool$requiredTrue if the element must be present
int | false$minimumMinimum value (or false is none)
bool$minimumExclusiveTrue if value must exceed the minimum
bool$overrideStrictModeIgnore strict mode setting
Returns
int|float|null Value of element (or null if not found or valid)

References checkInteger().

Referenced by ceLTIc\LTI\Service\LineItem\get(), and ceLTIc\LTI\Content\LineItem\toJsonObject().

◆ checkObject()

static object null ceLTIc\LTI\Util::checkObject ( object $obj,
string $fullname,
bool $required = false,
bool $stringValues = false )
static

Get the named object element from object.

Parameters
object$objObject containing the element
string$fullnameName of element
bool$requiredTrue if the element must be present
bool$stringValuesTrue if the values must be strings
Returns
object|null Value of element (or null if not found)

References checkArray().

◆ checkString()

static string ceLTIc\LTI\Util::checkString ( object $obj,
string $fullname,
bool $required = false,
?bool $notEmpty = false,
string|array $fixedValue = '',
bool $overrideStrictMode = false,
?string $default = '' )
static

Get the named string element from object.

Parameters
object$objObject containing the element
string$fullnameName of element (may include a path)
bool$requiredTrue if the element must be present
bool | null$notEmptyTrue if the element must not have an empty value (use null to issue a warning message when it does)
string$fixedValueRequired value of element (empty string if none)
bool$overrideStrictModeIgnore strict mode setting
string | null$defaultValue to return when a conversion is not possible (optional, default is an empty string)
Returns
string Value of element (or default value if not found or valid)

Referenced by checkArray(), ceLTIc\LTI\Service\Groups\get(), ceLTIc\LTI\Service\LineItem\get(), and ceLTIc\LTI\Content\LineItem\toJsonObject().

◆ checkUrl()

static string ceLTIc\LTI\Util::checkUrl ( object $obj,
string $fullname,
bool $required = false )
static

Get the named filly-qualified URL element from object.

Parameters
object$objObject containing the element
string$fullnameName of element (may include a path)
bool$requiredTrue if the element must be present
Returns
string Value of element (or default value if not found or valid)

Referenced by ceLTIc\LTI\Content\Image\toJsonldObject().

◆ cloneObject()

static object ceLTIc\LTI\Util::cloneObject ( object $obj)
static

Clone an object and any objects it contains.

Parameters
object$objObject to be cloned
Returns
object

◆ getMessages()

static array ceLTIc\LTI\Util::getMessages ( bool $isError)
static

Get the system error or warning messages.

Parameters
bool$isErrorTrue if error messages are to be returned
Returns
array Array of messages

◆ getRandomString()

static string ceLTIc\LTI\Util::getRandomString ( int $length = 8)
static

Generate a random string.

The generated string will only comprise letters (upper- and lower-case) and digits.

Parameters
int$lengthLength of string to be generated (optional, default is 8 characters)
Returns
string Random string

Referenced by ceLTIc\LTI\ResourceLinkShareKey\__construct(), and ceLTIc\LTI\Tool\findService().

◆ getRequestParameters()

static array ceLTIc\LTI\Util::getRequestParameters ( )
static

Return GET and POST request parameters (POST parameters take precedence).

Returns
array

References log(), and logDebug().

Referenced by ceLTIc\LTI\Tool\getMessageParameters(), and ceLTIc\LTI\Platform\getTools().

◆ isLtiMessage()

static bool ceLTIc\LTI\Util::isLtiMessage ( )
static

Check whether the request received could be an LTI message.

Returns
bool

References log(), and logInfo().

◆ jsonDecode()

◆ log()

static void ceLTIc\LTI\Util::log ( string $message,
bool $showSource = false )
static

Log an error message irrespective of the logging level.

Parameters
string$messageMessage to be logged
bool$showSourceTrue if the name and line number of the current file are to be included

Referenced by getRequestParameters(), and isLtiMessage().

◆ logDebug()

static void ceLTIc\LTI\Util::logDebug ( string $message,
bool $showSource = false )
static

Log a debug message.

Parameters
string$messageMessage to be logged
bool$showSourceTrue if the name and line number of the current file are to be included

References logInfo().

Referenced by ceLTIc\LTI\Http\HttpMessage\__construct(), ceLTIc\LTI\Service\LineItem\deleteLineItem(), ceLTIc\LTI\Jwt\FirebaseClient\getClaim(), ceLTIc\LTI\Jwt\WebTokenClient\getClaim(), getRequestParameters(), logError(), and logRequest().

◆ logError()

static void ceLTIc\LTI\Util::logError ( string $message,
bool $showSource = true )
static

Log an error message.

Parameters
string$messageMessage to be logged
bool$showSourceTrue if the name and line number of the current file are to be included

References ceLTIc\LTI\OAuth\OAuthRequest\$POST_INPUT, logDebug(), logInfo(), and ceLTIc\LTI\OAuth\OAuthUtil\parse_parameters().

Referenced by ceLTIc\LTI\Jwt\FirebaseClient\verify().

◆ logForm()

static void ceLTIc\LTI\Util::logForm ( string $url,
array $params,
string $method = 'POST',
bool $debugLevel = false )
static

Log a form submission.

Parameters
string$urlURL to which the form should be submitted
array$paramsArray of form parameters
string$methodHTTP Method used to submit form (optional, default is POST)
bool$debugLevelTrue if the form details should always be logged (optional, default is false to use current log level)

◆ logInfo()

static void ceLTIc\LTI\Util::logInfo ( string $message,
bool $showSource = false )
static

Log an information message.

Parameters
string$messageMessage to be logged
bool$showSourceTrue if the name and line number of the current file are to be included

Referenced by isLtiMessage(), logDebug(), and logError().

◆ logRequest()

static void ceLTIc\LTI\Util::logRequest ( bool $debugLevel = false)
static

Log a request received.

Parameters
bool$debugLevelTrue if the request details should be logged at the debug level (optional, default is false for information level)

References logDebug().

Referenced by ceLTIc\LTI\Tool\getMessageParameters().

◆ redirect()

static never ceLTIc\LTI\Util::redirect ( string $url,
array $params )
static

Redirect to a URL with query parameters.

Parameters
string$urlURL to which the form should be submitted
array$paramsArray of form parameters
Returns
never

◆ sendForm()

static string ceLTIc\LTI\Util::sendForm ( string $url,
array $params,
string $target = '',
string $javascript = '' )
static

Generate a web page containing an auto-submitted form of parameters.

Parameters
string$urlURL to which the form should be submitted
array$paramsArray of form parameters
string$targetName of target (optional)
string$javascriptJavascript to be inserted (optional, default is to just auto-submit form)
Returns
string

Referenced by ceLTIc\LTI\Tool\onLtiEndAssessment().

◆ setMessage()

static void ceLTIc\LTI\Util::setMessage ( bool $isError,
string $message )
static

Set an error or warning message.

Parameters
bool$isErrorTrue if the message represents an error
string$messageMessage

Referenced by ceLTIc\LTI\Service\Groups\get(), ceLTIc\LTI\Service\ToolSettings\set(), and valToNumber().

◆ setTestCookie()

static void ceLTIc\LTI\Util::setTestCookie ( bool $delete = false)
static

Set or delete a test cookie.

Parameters
bool$deleteTrue if the cookie is to be deleted (optional, default is false)

◆ stripHtml()

static string ceLTIc\LTI\Util::stripHtml ( string $html)
static

Strip HTML tags from a string.

Parameters
string$htmlHTML string to be stripped
Returns
string

◆ urlEncode()

static string ceLTIc\LTI\Util::urlEncode ( ?string $val)
static

URL encode a value.

Parameters
string | null$valThe value to be encoded
Returns
string

◆ valToBoolean()

static bool ceLTIc\LTI\Util::valToBoolean ( mixed $val)
static

Convert a value to a boolean.

Parameters
mixed$valThe value to be converted
Returns
bool

◆ valToNumber()

static int float null ceLTIc\LTI\Util::valToNumber ( mixed $val)
static

Convert a value to a numeric.

Parameters
mixed$valThe value to be converted
Returns
int|float|null

References setMessage().

◆ valToString()

static string ceLTIc\LTI\Util::valToString ( mixed $val,
?string $default = '' )
static

Convert a value to a string.

Parameters
mixed$valThe value to be converted
string | null$defaultValue to return when a conversion is not possible (optional, default is an empty string)
Returns
string

Referenced by ceLTIc\LTI\Service\ToolSettings\set(), and ceLTIc\LTI\Content\TimePeriod\toJsonldObject().

Member Data Documentation

◆ $disableFetchedPublicKeysSave

bool ceLTIc\LTI\Util::$disableFetchedPublicKeysSave = false
static

Whether the automatic saving of fetched valid public keys should be disabled.

◆ $formSubmissionTimeout

int ceLTIc\LTI\Util::$formSubmissionTimeout = 2
static

Delay (in seconds) before a manual button is displayed in case a browser is blocking a form submission.

◆ $logLevel

◆ $METHOD_NAMES

array ceLTIc\LTI\Util::$METHOD_NAMES
static
Initial value:
= [
'basic-lti-launch-request' => 'onLaunch',
'ConfigureLaunchRequest' => 'onConfigure',
'DashboardRequest' => 'onDashboard',
'ContentItemSelectionRequest' => 'onContentItem',
'ContentItemSelection' => 'onContentItem',
'ContentItemUpdateRequest' => 'onContentItemUpdate',
'LtiSubmissionReviewRequest' => 'onSubmissionReview',
'ToolProxyRegistrationRequest' => 'onRegister',
'LtiStartProctoring' => 'onLtiStartProctoring',
'LtiStartAssessment' => 'onLtiStartAssessment',
'LtiEndAssessment' => 'onLtiEndAssessment'
]

List of supported message types and associated class methods.

◆ $requestParameters

array ceLTIc\LTI\Util::$requestParameters = null
static

GET and POST request parameters.

◆ $strictMode

bool ceLTIc\LTI\Util::$strictMode = false
static

◆ JWT_CLAIM_MAPPING

const ceLTIc\LTI\Util::JWT_CLAIM_MAPPING

Mapping for standard message parameters to JWT claim.

◆ JWT_CLAIM_PREFIX

const ceLTIc\LTI\Util::JWT_CLAIM_PREFIX = 'https://purl.imsglobal.org/spec/lti'

Prefix for standard JWT message claims.

◆ MESSAGE_TYPE_MAPPING

const ceLTIc\LTI\Util::MESSAGE_TYPE_MAPPING
Initial value:
= [
'basic-lti-launch-request' => 'LtiResourceLinkRequest',
'ContentItemSelectionRequest' => 'LtiDeepLinkingRequest',
'ContentItemSelection' => 'LtiDeepLinkingResponse',
'ContentItemUpdateRequest' => 'LtiDeepLinkingUpdateRequest'
]

Mapping for standard message types.

Referenced by ceLTIc\LTI\System\hasJwt().

◆ TEST_COOKIE_NAME

const ceLTIc\LTI\Util::TEST_COOKIE_NAME = 'celtic_lti_test_cookie'

Name of test cookie.