Code Structure

From GExtension Wiki
Revision as of 14:24, 16 January 2018 by Ibot3 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Useful Functions

  • string Lang(string $identifier): Returns the language string for the given identifier.
  • string AP(__DIR__): Returns the path of your addon folder (e.g. "addons/[you-addon]/"), useful if you want to access the "assets" folder.
  • string ValidateHTML(string $html): Validates a given HTML code, prevents XSS.
  • void RequiredArguments(string $arg1, string $arg2, ...): Checks if the given strings are available as $_REQUEST paramters. If true, they will be transformed to global variables with the same name. If not, a die() with error message will be executed.
  • void OptionalArguments(string $arg1, string $arg2, ...): Same as RequiredArguments, but does not die() if argument not present. Sets an empty string as global variable instead.
  • void Error(string $reason, bool $debug): die() with format {"error":"$reason"}. Writes error in the gex_debug mysql table if $debug is set to true.
  • void Success(): die() with format {"success":true}
  • void Result(array $result): die() with the array in json format
  • string Hostname(): Returns the url to the domain (e.g. https://www.community.com/).
  • string GetCurrentLocation(): Returns the url to the GExtension directory (e.g. https://www.community.com/gmodweb/).
  • array FromJson(string $json): Same as json_decode() but with save return
  • string ToJson(array $array): Same as json_encode() but wuth save return
  • void Redirect(string $url, bool $delay = false): Initiates a JavaScript redirect, with 500ms delay if wanted
  • bool StartsWith(string $haystack, string $needle)
  • bool EndsWith(string $haystack, string $needle)
  • string FormatDate(string $date): Formats a given date string (not unix).
  • float FormatPrice(float $price): Formats a given price to two decimals
  • void Debug(string $text): Writes a text into the gex_debug mysql table.
  • void PermissionsError(bool $queue = false): Display a permissions error warning, queued for the next redirect if wanted.
  • string SteamIDTo64(string $steamid32): Converts a 32 Bit SteamID into a 64 Bit SteamID.
  • string SteamIDFrom64(string $steamid62): Converts a 64 Bit SteamID into a 32 Bit SteamID.

See assets/php/util.php and assets/php/classes/*.class.php for more functions. Especially the classes CURLRequester, DirectNotifications, User, Group, Notifications, Permissions and Settings. You can find usage examples for those classes in the core files, just use the search function.

Global Variables

Database

GExtension uses a MySQLi class which is documentated here.

The object is named $db and can be used by functions with:

$db = MysqliDb::getInstance();

or

global $db;

User

The current user is available as a variable named $auth_user, null when not logged in.

global $auth_user;

DBTableClass

The DBTableClass is used as a processor for mysql data. It's used by several classes, for example User.

All classes using the DBTableClass have the methods GetValue($key), SetValue($key, $value), Update($data) and Delete() and also the attributes $valid (there is data for the given id) and $data (raw data array).

Example for the class "User":

$user = new User($steamid64or32);

if($user->valid){
    $nick = $user->GetValue('nick'); //Getting a value
    $user->SetValue('email', '[email protected]'); //Updating a value
    $user->Update(array('email' => '[email protected]', 'nick' => 'Test')) //Updating multiple values
    $user->Delete() //You may should not delete a user from database
}