Difference between revisions of "Addons"

From GExtension Wiki
Jump to navigation Jump to search
Line 76: Line 76:
 
*install.php: Optional. If existent, gives the user the ability to run a one-time setup to complete the addon installation. Possible by navigating to '''Admin -> Settings -> Addons'''. Must contain a function Install(). The process will create a installed.txt in the main addon folder, containing the date of the installation and the steamid64 of the user who ran it.
 
*install.php: Optional. If existent, gives the user the ability to run a one-time setup to complete the addon installation. Possible by navigating to '''Admin -> Settings -> Addons'''. Must contain a function Install(). The process will create a installed.txt in the main addon folder, containing the date of the installation and the steamid64 of the user who ran it.
 
*uninstall.php: Optional. If existent, this script will be run when the addon gets uninstalled (by navigating to '''Admin -> Settings -> Addons'''). Should be used to remove any leftovers. Must contain a function Uninstall()
 
*uninstall.php: Optional. If existent, this script will be run when the addon gets uninstalled (by navigating to '''Admin -> Settings -> Addons'''). Should be used to remove any leftovers. Must contain a function Uninstall()
 +
 +
== Section: pages ==
 +
=== Name and first line ===
 +
The pages section represents all pages a user could be able to visit. The filename should be <rawname>.php, where <rawname> is a lowercase identifiert. Pages for administrative purposes should begin with "admin_".
 +
 +
The first line of a page PHP file should always be the following:
 +
<pre><!--{"language":"<language-identifier>","icon":"<fontawesome-icon>","place":"<main|help|admin|user|none>","position":<position>}--></pre>
 +
Example:
 +
<pre><!--{"language":"search","icon":"fa-search","place":"main","position":40,"nologin":true}--></pre>
 +
 +
Additionally is it possible to add the "nonlogin" paramter, to define that a page can be used without being logged in. See [http://fontawesome.io/icons/ FontAwesome] for icons.
 +
 +
=== Security Check ===
 +
 +
To verify that the file was included by GExtension, it's required to include the following code after the first line:
 +
<pre>
 +
<?php
 +
if(!isset($G_MAIN)){
 +
die(json_encode(array("error" => "authentification failed")));
 +
}
 +
?>
 +
</pre>
 +
 +
=== Other ===
 +
 +
After creating a page, it is available under index.php?t=<rawname>. <rawname> is the name of the file without ".php".
 +
 +
You may want to allow a common page for all groups:
 +
Permissions::AllowPagesForEveryone($rawname1, $rawname2, ...);
 +
 +
Check out existing pages for examples.
  
 
== Working In Progress ==
 
== Working In Progress ==

Revision as of 12:45, 19 April 2017

This article explais, how to create an addon for GExtension.

Folder Structure

The folder structure is similar to the one of Garry's Mod. First, you should create a folder: "addons/[your-addon]"

That's your main addon folder, where all files of your addon should be in. Additionally, you can create the following subfolders:

/api
/assets
/language
/main 
/pages
/request
/settings
/themes

The meaning and function of those folders are explained below. Please try to imitate the folder structure of the GExtension core files.

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 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 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 if 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).

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
}

Addon Folder

Additionaly to the subfolders named above, the main addon folder can contain some more files:

  • addon.txt: Optional. Contains a json object with information about the addon, important at the moment are, "author" and "version".
  • install.php: Optional. If existent, gives the user the ability to run a one-time setup to complete the addon installation. Possible by navigating to Admin -> Settings -> Addons. Must contain a function Install(). The process will create a installed.txt in the main addon folder, containing the date of the installation and the steamid64 of the user who ran it.
  • uninstall.php: Optional. If existent, this script will be run when the addon gets uninstalled (by navigating to Admin -> Settings -> Addons). Should be used to remove any leftovers. Must contain a function Uninstall()

Section: pages

Name and first line

The pages section represents all pages a user could be able to visit. The filename should be <rawname>.php, where <rawname> is a lowercase identifiert. Pages for administrative purposes should begin with "admin_".

The first line of a page PHP file should always be the following:

<!--{"language":"<language-identifier>","icon":"<fontawesome-icon>","place":"<main|help|admin|user|none>","position":<position>}-->

Example:

<!--{"language":"search","icon":"fa-search","place":"main","position":40,"nologin":true}-->

Additionally is it possible to add the "nonlogin" paramter, to define that a page can be used without being logged in. See FontAwesome for icons.

Security Check

To verify that the file was included by GExtension, it's required to include the following code after the first line:

<?php 
	if(!isset($G_MAIN)){
		die(json_encode(array("error" => "authentification failed")));
	}
?>

Other

After creating a page, it is available under index.php?t=<rawname>. <rawname> is the name of the file without ".php".

You may want to allow a common page for all groups:

Permissions::AllowPagesForEveryone($rawname1, $rawname2, ...);

Check out existing pages for examples.

Working In Progress

This artile will be extended soon.