Show:

Q_Utils Class

Module: Q

Functions for doing various things

Item Index

Methods

Methods

canWriteToPath

(
  • $path
  • [$throwIfNotWritable=false]
  • [$mkdirIfMissing]
)
Boolean static

Checks whether the path can be used for writing files by the current session

Parameters:

  • $path String

    The path to check

  • [$throwIfNotWritable=false] Mixed optional

    Defaults to false. Set to true to throw a Q_Exception_CantWriteToPath if no user is logged in. Set to null to skip firing the "before" event, thereby skipping custom access checks. The null value is useful for when the filename is generated by the app, not the user.

  • [$mkdirIfMissing] Boolean optional

    Defaults to false. Pass true here to make a directory at the specified path, if it's writeable but missing. Pass a string here to override the umask before making the directory.

Returns:

Boolean:

chr

(
  • $intval
)
String static

Like chr but handles utf-8 encoding

Parameters:

  • $intval Integer

Returns:

String:

copy

(
  • $source
  • $dest
)
static

Copies a file or directory from path to another. May overwrite existing files.

Parameters:

  • $source String
  • $dest String

csvLines

(
  • $input
  • [$enclosure='"']
  • [$escape="\\"]
)

Get the lines from a csv file

Parameters:

  • $input String
  • [$enclosure='"'] String optional
  • [$escape="\\"] String optional

Returns:

array

get

(
  • $url
  • [$user_agent=null]
  • [$follow_redirects=true]
  • [$header=null]
)
String static

Issues a GET request, and returns the response

Parameters:

  • $url String | Array

    The URL to post to This can also be an array of ($url, $ip) to send the request to a particular IP, while retaining the hostname and request URI

  • [$user_agent=null] String optional

    The user-agent string to send. Defaults to Mozilla.

  • [$follow_redirects=true] String optional

    Whether to follow redirects when getting a response.

  • [$header=null] String optional

    Optional string to replace the entire GET header

Returns:

String:

The response.

NOTE: The function waits for it, which might take a while!

hash

(
  • $test
)
String static

Hashes text in a standard way. It uses md5, which is fast and irreversible, so it's good for things like indexes, but not for obscuring information.

Parameters:

  • $test String

Returns:

String:

hmac

(
  • $algo
  • $data
  • $key
  • [$raw_output=false]
)
String static

Calculates hmac

Parameters:

  • $algo String
  • $data String
  • $key String
  • [$raw_output=false] Boolean optional

Returns:

String:

nodeUrl

() static

Returns base url for node.js requests

normalize

(
  • $text
  • [$replacement='_']
  • [$characters=null]
  • [$numChars=200]
  • [$keepCaseIntact=false]
)
static

Normalizes text by converting it to lower case, and replacing all non-accepted characters with underscores.

Parameters:

  • $text String

    The text to normalize

  • [$replacement='_'] String optional

    A string to replace one or more unacceptable characters. You can also change this default using the config Db/normalize/replacement

  • [$characters=null] String optional

    Defaults to '/[^A-Za-z0-9]+/'. A regexp characters that are not acceptable. You can also change this default using the config Db/normalize/characters

  • [$numChars=200] Integer optional

    Defaults to 200, maximum length of normalized string

  • [$keepCaseIntact=false] Boolean optional

    If true, doesn't convert to lowercase

normalizePath

(
  • $path
)
static

Normalize paths to use DS, used mostly on Windows

Parameters:

  • $path String | Array

    the path or paths to normalize

obfuscate

(
  • $text
  • [$key="blah"]
)
Text static

Some basic obfuscation to thwart scrapers from getting emails, phone numbers, etc.

Parameters:

  • $text String

    The text to obfuscate

  • [$key="blah"] String optional

    Some key to use for obfuscation

Returns:

Text:

ord

(
  • $text
)
Integer static

Like ord but handles utf-8 encoding

Parameters:

  • $text String

Returns:

Integer:

parts

(
  • $source
  • $ofset
  • $length
  • $separator
)
String static

Takes parts of a string

Parameters:

  • $source String

    the string to split by the separator

  • $ofset Integer

    just like in array_slice

  • $length Integer

    just like in array_slice

  • $separator String

    the separator, defaults to '/'

Returns:

String:

the extracted parts, joined together again by the separator

post

(
  • $url
  • $data
  • [$user_agent=null]
  • [$follow_redirects=true]
  • [$header=null]
)
String static

Issues a POST request, and returns the response

Parameters:

  • $url String | Array

    The URL to post to This can also be an array of ($url, $ip) to send the request to a particular IP, while retaining the hostname and request URI

  • $data Array | String

    The data content to post or an array of ($field => $value) pairs

  • [$user_agent=null] String optional

    The user-agent string to send. Defaults to Mozilla.

  • [$follow_redirects=true] String optional

    Whether to follow redirects when getting a response.

  • [$header=null] String optional

    Optional string to replace the entire POST header

Returns:

String:

The response.

NOTE: The function waits for it, which might take a while!

postAsync

(
  • $uri
  • $params
  • [$user_agent=null]
  • [$timeout=Q_UTILS_CONNECTION_TIMEOUT]
  • [$throwIfRefused=false]
  • [$closeSocket=false]
)
Boolean static

Sends a post and returns right away. In the url being called, make sure it ignores user aborts. For example, in PHP call ignore_user_abort(true) at the top of that script.

Parameters:

  • $uri String | Array

    The url to post to

  • $params Array

    An associative array of params

  • [$user_agent=null] String optional

    The user-agent string to send. If null, is replaced by default of "Mozilla/5.0 ..." If false, not sent.

  • [$timeout=Q_UTILS_CONNECTION_TIMEOUT] Integer optional
  • [$throwIfRefused=false] Boolean optional

    Pass true here to throw an exception whenever Node process is not running or refuses the request

  • [$closeSocket=false] Boolean optional

    Pass true to close the socket after sending. The default is to do HTTP pipelining.

Returns:

Boolean:

Returns whether the post succeeded.

queryExternal

(
  • $handler
  • [$data=array()]
  • [$url=null]
)
Mixed static

Queries a server externally to the specified handler. Expects json array with either ['slots']['data'] or ['error'] fields filled

Parameters:

  • $handler String

    the handler to call

  • [$data=array()] Array optional

    Associative array of data of the message to send.

  • [$url=null] String | Array optional

    and url to query. Default to 'Q/web/appRootUrl' config value

Returns:

Mixed:

The response from the server

queryInternal

(
  • $handler
  • [$data=array()]
  • [$url=null]
)
Mixed static

Sends a query to Node.js internal server and gets the response This method shall make communications behind firewal

Parameters:

  • $handler String

    is used as 'Q/method' while querying $url

  • [$data=array()] Array optional

    Associative array of data of the message to send.

  • [$url=null] String | Array optional

    and url to query. Default to 'Q/nodeInternal' config value and path '/Q_Utils/query'

Returns:

Mixed:

The response from the server

request

(
  • $method
  • $url
  • $data
  • [$user_agent=null]
  • [$follow_redirects=true]
  • [$header=null]
)
String private static

Issues an http request, and returns the response

Parameters:

  • $method String

    The http method to use

  • $url String | Array

    The URL to request This can also be an array of ($url, $ip) to send the request to a particular IP, while retaining the hostname and request URI

  • $data Array | String

    The data content to post or an array of ($field => $value) pairs

  • [$user_agent=null] String optional

    The user-agent string to send. Defaults to Mozilla.

  • [$follow_redirects=true] String optional

    Whether to follow redirects when getting a response.

  • [$header=null] String optional

    Optional string to replace the entire header

Returns:

String:

The response.

NOTE: The function waits for it, which might take a while!

sendToNode

(
  • $data
  • [$url=null]
  • [$throwIfRefused=false]
)
static

Sends asynchronous internal message to Node.js If "Q.clientId" is in $_REQUEST, adds it into the data

Parameters:

  • $data Array

    Associative array of data of the message to send. It should contain the key "Q/method" so Node can decide what to do with the message.

  • [$url=null] String | Array optional

    and url to query. Default to 'Q/nodeInternal' config value and path '/Q/node'

  • [$throwIfRefused=false] Boolean optional

    Pass true here to throw an exception whenever Node process is not running or refuses the request

sign

(
  • $data
  • [$fieldKeys]
)
Array static

Sign the data

Parameters:

  • $data Array

    The array of data

  • [$fieldKeys] Array | String optional

    Path of the key under which to save signature

Returns:

Array:

signature

(
  • $data
  • $secret
)
String static

Generates signature for the data

Parameters:

  • $data Array | String
  • $secret String

Returns:

String:

socketPath

() static

Returns path option for socket.io connection

splitId

(
  • $id
  • [$lengths=3]
  • [$delimiter=DIRECTORY_SEPARATOR]
  • [$internalDelimiter='/']
)
String static

Used to split ids into one or more segments, in order to store millions of files under a directory, without running into limits of various filesystems on the number of files in a directory. Consider using Amazon S3 or another service for uploading files in production.

Parameters:

  • $id String

    the id to split

  • [$lengths=3] Integer optional

    the lengths of each segment (the last one can be smaller)

  • [$delimiter=DIRECTORY_SEPARATOR] String optional

    the delimiter to put between segments

  • [$internalDelimiter='/'] String optional

    the internal delimiter, if it is set then only the last part is split, and instances of internalDelimiter are replaced by delimiter

Returns:

String:

the segments, delimited by the delimiter

unique

(
  • [$len=8]
  • [$characters='abcdefghijklmnopqrstuvwxyz']
)
String static

Generates random letter sequence

Parameters:

  • [$len=8] Integer optional
  • [$characters='abcdefghijklmnopqrstuvwxyz'] String optional

    All the characters from which to construct possible ids

Returns:

String:

unserializeSession

(
  • $val
)
Array static

Unserializes session stored in PHP 5.2 and 5.3 format

Parameters:

  • $val String

Returns:

Array:

uuid

() String static

Generates a Universally Unique IDentifier, version 4. This function generates a truly random UUID.

Returns:

String:

A UUID, made up of 32 hex digits and 4 hyphens.

Events

Q/Utils/sendToNode

Before

Event Payload:

  • data Array
  • 'url' String | Array