Utils.php
This contain all functions that do not fit into other Wizard’s Toolkit libraries.
All rights reserved.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Tags
Table of Contents
- wtkTimeTrack() : null
- Time Track is a debug method
- wtkShowTimeTracks() : echoes
- Show Time Tracks
- wtkProtoType() : null
- This takes passed HTML and writes it to /tst/ folder for HTML/CSS debugging
- wtkAddUserHistory() : null
- This function saves to User History how long it took to generate page.
- wtkLogError() : null
- Easy error logging
- wtkInsFailedAttempt() : null
- If there is a failed attack on server it will save information into wtkFailedAttempts table
- wtkErrorHandler() : null
- Custom Error Handling
- wtkExceptionHandler() : html
- Custom Exception Handling
- wtkCheckForFatal() : null
- Trap fatal errors
- wtkSetCookie() : null
- Set Cookie
- wtkGetCookie() : string
- Get Cookie - returns either the cookie value or a blank '' value
- wtkDeleteCookie() : null
- Delete Cookie
- wtkParseCurrencyToNumber() : number
- Changes Currency into simple Number
- wtkRedirect() : null
- Redirect to passed URL regardless of whether headers have been sent or not
- wtkRemoveStyle() : string
- This will remove HTML encoding from the value passed in.
- wtkLoadInclude() : file
- Load and return include file contents
- wtkReplace() : string
- Similar to str_replace except it replaces values within arrays.
- wtkGetEnviron() : string
- Get Environ
- wtkGetParam() : string
- Returns GET if it exists, otherwise returns POSTed value.
- wtkGetGet() : string
- Get $_GET[] of passed parameter.
- wtkGetPost() : string
- Get $_POST[]
- wtkGetServer() : string
- Get $_SERVER[]
- wtkSetSession() : null
- Set $_SESSION variable.
- wtkGetSession() : string
- Get $_SESSION value or '' blank if not set.
- wtkDeleteSession() : null
- unset($_SESSION[$fncSessVariable])
- wtkFormatPercent() : value
- Format passed number as a percent.
- wtkFormatCurrency() : mixed
- Format passed number as currency.
- wtkFormatDateTime() : formatted
- Format passed value for Date and Time
- wtkHtmlEncode() : encoded
- HTML Encode using htmlentities($fncString, ENT_QUOTES)
- wtkHtmlDecode() : decoded
- HTML Decode
- wtkInsertSpaces() : string
- Insert Spaces
- wtkTruncate() : string
- wtkTruncate
- wtkGetIPaddress() : mixed
- wtkGetIPaddress
- wtkSecondsToDays() : string
- Pass in number of seconds and returns value like: "3 Days 8 Hours 38 Minutes"
- wtkTimeZoneAdjust() : string
- Checks server's time-zone and if different than passed time-zone, converts time.
- wtkDeadPage() : html
- Call this function to show a dead-end page.
- wtkIsWindows() : bool
- Is Windows - determines whether servers is a Windows server
- wtkIsSSL() : bool
- This detects whether current URL is SSL or not.
- wtkConvertLinks() : properly
- Based current URL adjusts link to use similar SSL or not SSL link.
Functions
wtkTimeTrack()
Time Track is a debug method
wtkTimeTrack(
string
$fncLocation)
: null
This debug method allows displaying debug information and then by changing the $gloTrackTime flag all debug will stop displaying. It also shows exact time for each call so you can check and debug speed issues.
$gloTrackTime = true; // defaults to false; set to true to enable debug mode
wtkTimeTrack('just after some code');
Parameters
- $fncLocation : string
Tags
Return values
null —wtkShowTimeTracks()
Show Time Tracks
wtkShowTimeTracks()
: echoes
This shows everything that was sent to wtkTimeTrack plus all internal Wizard’s Toolkit debug logging. It is automatically called at end of each WTK library PHP page but will not show anything unless $gloTrackTime = true. You can also call this earlier for debugging if necessary.
Tags
Return values
echoes —all time tracks
wtkProtoType()
This takes passed HTML and writes it to /tst/ folder for HTML/CSS debugging
wtkProtoType(
string
$fncHtm)
: null
The generated HTML files can be given to UI/HTML/CSS developers so they can work on the UI without needing PHP nor SQL skills. This should be at bottom of your page.
Parameters
- $fncHtm : string
Return values
null —wtkAddUserHistory()
This function saves to User History how long it took to generate page.
wtkAddUserHistory(
[
string
$fncPageTitle =
'' ]
) : null
Data is saved to wtkUserHistory
table.
This is called via wtkBuildDataBrowse, wtkFormWriteUpdField and wtkMergePage. If it is called more
than once it only updates the SecondsTaken
instead of making a new data row.
Parameters
- $fncPageTitle : string = ''
-
optionally pass Page Title
Return values
null —wtkLogError()
Easy error logging
wtkLogError(
string
$fncErrType
,
string
$fncErrMsg
[,
string
$fncLineNum =
'NULL'
]) :
null
Pass in Error Type and Error Message and this inserts that plus additional information into the wtkErrorLog table. This includes logged-in user, Current Page, and Referer Page in addition to the Type and Message passed. Several functions in Wizard’s Toolkit use this so check your wtkErrorLog table any time you have problems.
Parameters
- $fncErrType : string
- $fncErrMsg : string
- $fncLineNum : string = 'NULL'
-
defaults to NULL
Tags
Return values
null —wtkInsFailedAttempt()
If there is a failed attack on server it will save information into wtkFailedAttempts table
wtkInsFailedAttempt(
string
$fncFailCode
[,
string
$fncFailNote =
'' ]
) : null
Pass in 4-character code that we want to log this failed attempt for. This includes logged-in user, IP Address, Browser Type, Current Page, and Referer Page in addition to the Message passed.
Parameters
- $fncFailCode : string
-
4-character code so you can easily count types of failures
- $fncFailNote : string = ''
-
optionally mention what caused failure
Tags
Return values
null —wtkErrorHandler()
Custom Error Handling
wtkErrorHandler
(
string
$fncNum
,
mixed
$fncStr
,
mixed
$fncFile
,
mixed
$fncLine
[,
mixed
$fncContext =
null ]
) : null
This is called from wtkCheckForFatal. This calls wtkExceptionHandler to show the error using the client's GUI templates.
Parameters
- $fncNum : string
- $fncStr : mixed
- $fncFile : mixed
- $fncLine : mixed
- $fncContext : mixed = null
Return values
null —wtkExceptionHandler()
Custom Exception Handling
wtkExceptionHandler(mixed $fncErr) : html
Uncaught exception handler set using set_exception_handler. This shows the error using the client's GUI templates after saving the error to the wtkErrorLog table.
Parameters
- $fncErr : mixed
Return values
html —with error information; if user's SecurityLevel is > 94 then shows additional error->getMessage()
wtkCheckForFatal()
Trap fatal errors
wtkCheckForFatal()
: null
This captures fatal errors and calls wtkExceptionHandler to display. This prevents White Screens with no error messaging. This uses $fncError = error_get_last() then if 'type' >= 256 then calls wtkErrorHandler and wtkExceptionHandler.
Checks for a fatal error, work around for set_error_handler not working on fatal errors. This is set using register_shutdown_function.
Return values
null —wtkSetCookie()
Set Cookie
wtkSetCookie(
string
$fncCookieName
,
string
$fncValue
,
string
$fncExpires
[,
string
$fncPath =
'/' ]
) : null
For the 'Expires' parameter you can pass in any of these: 1day, 1week, 1month, 1year. If you pass nothing the cookie expires at end of session.
Parameters
- $fncCookieName : string
- $fncValue : string
- $fncExpires : string
-
Defaults to "0". Switch cases: "1day, 1week, 1month, 1year". If numeric value is passed it is added to time() for when to expire.
- $fncPath : string = '/'
-
Defaults to "/"
Return values
null —wtkGetCookie()
Get Cookie - returns either the cookie value or a blank '' value
wtkGetCookie(
string
$fncCookieName
) : string
Parameters
- $fncCookieName : string
Return values
string —wtkDeleteCookie()
Delete Cookie
wtkDeleteCookie(
string
$fncCookieName
) : null
Parameters
- $fncCookieName : string
Return values
null —wtkParseCurrencyToNumber()
Changes Currency into simple Number
wtkParseCurrencyToNumber(
[
string
$fncVal =
'$0'
]) :
number
For example will change '$12,345.67' into 12345.67
Parameters
- $fncVal : string = '$0'
Return values
number —from passed currency value
wtkRedirect()
Redirect to passed URL regardless of whether headers have been sent or not
wtkRedirect(
string
$fncURL)
: null
To prevent redirection errors this will change go-to link to use https: or http: based on what current web page user is on. Redirects as a 301 but if pass 'N' as second parameter ($fncPermanent) then redirects as 302.
Parameters
- $fncURL : string
- $fncPermanent : string
- defaults to 'Y'
Return values
null —wtkRemoveStyle()
This will remove HTML encoding from the value passed in.
wtkRemoveStyle(
string
$fncThisValue)
: string
Parameters
- $fncThisValue : string
Return values
string —wtkLoadInclude()
Load and return include file contents
wtkLoadInclude(
string
$fncIncludeFile)
: file
Parameters
- $fncIncludeFile : string
Return values
file —contents
wtkReplace()
Similar to str_replace except it replaces values within arrays.
wtkReplace(
string
$fncSubject
,
string
$fncSearchFor
,
string
$fncReplaceWith)
: string
Case sensitive search and replace. It does not affect the passed-in value to be searched. The parameters are in a more intuitive order. It returns the result after replacing all matches even in recursive arrays. For arrays it replaces values, not keys. If array is passed in, array is returned. If a string is passed in, a string is returned.
Example:
$pgNewStr = wtkReplace($Subject, $SearchFor, $ReplaceWith);
Parameters
- $fncSubject : string
- $fncSearchFor : string
- $fncReplaceWith : string
Return values
string —or array depending on what was passed in
wtkGetEnviron()
Get Environ
wtkGetEnviron(
string
$fncEnvVariable)
: string
Parameters
- $fncEnvVariable : string
Return values
string —Returns a variable from ENV or '' if no value
wtkGetParam()
Returns GET if it exists, otherwise returns POSTed value.
wtkGetParam(
string
$fncParameter
[,
string
$fncDefault =
'' ]
) : string
wtkGetParam returns default if otherwise would return ''.
Parameters
- $fncParameter : string
- $fncDefault : string = ''
-
which defaults to '' blank
Return values
string —wtkGetGet()
Get $_GET[] of passed parameter.
wtkGetGet(
string
$fncParameter
[,
string
$fncDefault =
'' ]
) : string
If not blank, returns stripslashes(urldecode($_GET[$fncParameter])). If result is blank, then returns $fncDefault.
Parameters
- $fncParameter : string
- $fncDefault : string = ''
-
which defaults to '' blank
Return values
string —wtkGetPost()
Get $_POST[]
wtkGetPost(
string
$fncParameter
[,
string
$fncDefault =
'' ]
) : string
If result is blank, then returns $fncDefault.
Parameters
- $fncParameter : string
- $fncDefault : string = ''
-
which defaults to '' blank
Return values
string —wtkGetServer()
Get $_SERVER[]
wtkGetServer(
mixed $fncParameter) :
string
Parameters
- $fncParameter : mixed
Return values
string —returns a variable from SERVER or '' if no value
wtkSetSession()
Set $_SESSION variable.
wtkSetSession(
string
$fncSessVariable
,
string
$fncValue)
: null
If setting 'UserLevel' then to prevent other sites on same server from sharing UserLevel it prepends $gloAuthStatus
Parameters
- $fncSessVariable : string
- $fncValue : string
Tags
Return values
null —wtkGetSession()
Get $_SESSION value or '' blank if not set.
wtkGetSession(
string
$fncSessVariable)
: string
If retrieving 'UserLevel' then to prevent other sites on same server from sharing UserLevel it prepends $gloAuthStatus
Parameters
- $fncSessVariable : string
Tags
Return values
string —returns a variable from session or '' if no value
wtkDeleteSession()
unset($_SESSION[$fncSessVariable])
wtkDeleteSession(
string
$fncSessVariable
) : null
If retrieving 'UserLevel' then to prevent other sites on same server from sharing UserLevel it prepends $gloAuthStatus
Parameters
- $fncSessVariable : string
Tags
Return values
null —wtkFormatPercent()
Format passed number as a percent.
wtkFormatPercent
(
string
$fncValue
[,
numeric
$fncRoundDigits =
2 ]
[,
string
$fncGroupSep =
'' ]
[,
string
$fncDecimalSep =
'.' ]
[,
string
$fncPercentChar =
'after' ]
) : value
This assumes number needs to be multiplied by 100. Only first parameter of number is required.
Parameters
- $fncValue : string
- $fncRoundDigits : numeric = 2
-
Defaults to '2'
- $fncGroupSep : string = ''
-
Defaults to '' blank
- $fncDecimalSep : string = '.'
-
Defaults to '.'
- $fncPercentChar : string = 'after'
-
Defaults to 'after' to determine whether % should be before or after value
Return values
value —with percentage using above passed parameters
wtkFormatCurrency()
Format passed number as currency.
wtkFormatCurrency(
string
$fncValue
[,
numeric
$fncRoundDigits =
2 ]
[,
string
$fncMoneyChar =
'$' ]
[,
string
$fncGroupSep =
',' ]
[,
mixed
$fncDecimalSep =
'.' ]
) : mixed
This assumes number needs to be multiplied by 100. Only first parameter of number is required.
Parameters
- $fncValue : string
- $fncRoundDigits : numeric = 2
-
Defaults to '2'
- $fncMoneyChar : string = '$'
-
Defaults to '$'
- $fncGroupSep : string = ','
-
Defaults to blank
- $fncDecimalSep : mixed = '.'
Return values
mixed —wtkFormatDateTime()
Format passed value for Date and Time
wtkFormatDateTime(
string
$fncFormat
[,
numeric
$fncVal =
''
])
: formatted
Returns '' if either parameter is null, blank, '0000-00-00', or '00/00/0000'
otherwise sets date format via: date($fncFormat, strtotime($fncVal))
Parameters
- $fncFormat : string
- $fncVal : numeric = ''
-
Defaults to blank
Return values
formatted —date or ''
wtkHtmlEncode()
HTML Encode using htmlentities($fncString, ENT_QUOTES)
wtkHtmlEncode(
string
$fncString)
: encoded
Parameters
- $fncString : string
Return values
encoded —HTML
wtkHtmlDecode()
HTML Decode
wtkHtmlDecode(
string
$fncString)
: decoded
Parameters
- $fncString : string
Return values
decoded —HTML
wtkInsertSpaces()
Insert Spaces
wtkInsertSpaces(
string
$fncFieldName
) : string
This converts under_scored_fields into Under Scored Fields and it also converts WordCappedSentences into 'Word Capped Sentences'
Parameters
- $fncFieldName : string
Return values
string —with spaces before every capital letter
wtkTruncate()
wtkTruncate
wtkTruncate(
string
$fncStr
[,
numeric
$fncSize =
80 ]
) : string
This is called by BrowsePDO function to truncate text and add ellipsis. It makes certain to truncate at last space so it doesn't break in middle of word.
Here is an example:
$fncThisValue = wtkTruncate($fncThisValue, 80);
Parameters
- $fncStr : string
-
string passed in to possibly truncate
- $fncSize : numeric = 80
-
defaults to 80; if fncStr is greater than this it will be truncated and an ellipsis appended
Return values
string —original value unless too long in which case truncated and ellipsis added
wtkGetIPaddress()
wtkGetIPaddress
wtkGetIPaddress()
: mixed
get IP address of web surfer
Tags
Return values
mixed —wtkSecondsToDays()
Pass in number of seconds and returns value like: "3 Days 8 Hours 38 Minutes"
wtkSecondsToDays(
string
$fncSeconds) :
string
Parameters
- $fncSeconds : string
Return values
string —like 3 Days 8 Hours 38 Minutes
wtkTimeZoneAdjust()
Checks server's time-zone and if different than passed time-zone, converts time.
wtkTimeZoneAdjust(
string
$fncDateTime
,
string
$fncTimeZone
[,
string
$fncDateFormat =
'' ]
) : string
Pass in Date-Time and desired TimeZone to receive adjusted Date-Time in preferred format. Uses date_default_timezone_get to determine server's time-zone.
Parameters
- $fncDateTime : string
- $fncTimeZone : string
- $fncDateFormat : string = ''
-
- optionally send your preferred PHP date format
Return values
string —wtkDeadPage()
Call this function to show a dead-end page.
wtkDeadPage() :
html
If the IP Address is known it is shown with a message about "Nefarious Action Detected". This is used internally when user does something that looks like they are trying to hack the web server.
Return values
html —page telling hackers they cannot continue
wtkIsWindows()
Is Windows - determines whether servers is a Windows server
wtkIsWindows() :
bool
Return values
bool —eturns true if Windows, otherwise returns false
wtkIsSSL()
This detects whether current URL is SSL or not.
wtkIsSSL() :
bool
Return values
bool —true if SSL, else false
wtkConvertLinks()
Based current URL adjusts link to use similar SSL or not SSL link.
wtkConvertLinks(
string
$fncLinks) :
properly
This is used by wtkRedirect to determine whether http:// or https:// should be used when redirecting.
Parameters
- $fncLinks : string
Return values
properly —formed link