Getting started with DiscuzX3 plug-in production

Source: Internet
Author: User
I. required materials and tools for developing plug-ins 1. DiscuzX2 plug-in mechanism plugin (specially used to handle plug-ins installed with multiple encodings) 3. database management tool navicat [baidu] (convenient data 1. required materials and tools for plug-ins development
1. DiscuzX2 plug-in mechanism http: // dev. Discuz. Org/wiki/#. E6.8F. 92. E4.BB. B6.E6.9C. BA. E5.88.B6
2. convertz, a language pack encoding conversion tool [baidu] (used to handle multiple encoding plug-ins for installation)
3. database management tool navicat [baidu] (to facilitate database viewing and operations)
4. basic website data, including system constants, global variables, and system calls. start with system constants.
4.1. system constants

DISCUZ_ROOT // website root directory

TIMESTAMP // TIMESTAMP of program execution

CHARSET // language encoding type of the program

IS_ROBOT // whether it is a machine access
FORMHASH // HASH value
Other items can be printed and viewed directly, for example:

Require_once './source/class/class_core.php ';

$ Discuz = & discuz_core: instance ();

$ Discuz-> init ();

Print_r (get_defined_constants ());

?>

4.2 Global variables
Print $ _ G directly, as shown in the following figure:

Require_once './source/class/class_core.php ';

$ Discuz = & discuz_core: instance ();

$ Discuz-> init ();

Print_r ($ _ G );

?>

4.3 database configuration parameters
Print $ _ G ['config'] ['DB'] directly, as shown in the following figure:

Require_once './source/class/class_core.php ';

$ Discuz = & discuz_core: instance ();

$ Discuz-> init ();

Print_r ($ _ G ['config'] ['DB']);

?>

4.4 database operations
DB: table () // added the pre database table name.
DB: delete () // delete table data
DB: insert () // insert data to the table
DB: update () // update table data
DB: fetch () // used with DB: query to obtain data resources
DB: query () // execute a database statement
DB: fetch_first () // obtain the first record of the result set

For more operations, see the document: http://dev.discuz.org/wiki/index.php? Title = Discuz % E6 % 95% B0 % E6 % 8D % AE % E5 % BA % 93% E7 % B1 % BB

5. be familiar with./source/function/function_admincp.php to manage the functions in the background file and know what functions the specific function implements.
6. understand and master the basic general class, function http://dev.discuz.org/wiki/#.E5.9F.BA.E7.A1.80.E6.A1.86.E6.9E.B6.E3.80.81.E7.B1.BB.E3.80.81.E5.87.BD.E6.95.B0

II. security processing
1. file security
All plug-in-related programs, including all front and back-end programs, must be included in the first line because all are called using shell.
If (! Defined ('in _ discuz ')){
Exit ('accesssdenied ');
}
To avoid security issues caused by direct request calls from URLs.

2. processing numeric values
2.1, intval (); // process non-negative integers, such as: $ uid = intval ($ uid );
Note: after using intval, do you have to think about whether your variable should be non-negative? If yes, you must add a judgment statement or use functions such as abs and max to process the statement.
2.2 trim (); // remove left and right spaces, for example, $ username = trim ($ username );
2.3 for text content, remember to use htmlspecialchars (dhtmlspecialchars)
2.4 considerations for database writing
The variables to be imported into the database must be addslashes (daddslashes in DZ. for example, DZ does not need to filter again, and DZ has filtered all $ _ POST and $ _ GET). of course, if you enter a number (for example, uid, and intval already exists), or some other items that will certainly not go wrong, you can skip this step.
2.5 when writing SQL statements, you must remember to use the 'box for variables. If the variable is a string, an error occurs if you do not perform this operation. If the number is not used, no error will be prompted, but there may be a risk of injection.
2.6 before using the array, remember to write $ xxx = array (). The reason is very simple, to prevent users from submitting malicious values.

3. statement query optimization
When writing SQL statements, try to comply with the SQL rules. The statement query should be optimized first and then, and the data table should be indexed to speed up the query.

III. Examples of plug-ins
1. First, determine the plug-ins that need to be developed, whether the plug-in mechanism can be developed, and whether it can achieve the expected results. Otherwise, everything will be skipped.
2. what menus, parameters, options to be configured, how to design the data structure, and functions to be implemented in the front and back
3. what functions are needed? is there any function in discuz? try to use the internal function to achieve the expected results.
4. it is recommended that you do not change the original layout of discuz. you would rather Coty code to avoid adding functions to the original function to prevent future upgrade problems.
5. develop smart plug-ins as much as possible. use variables instead of constants to enhance program portability and maintainability.

IV. basic configurations of instances
1. install the new DiscuzX2
2. open the config/config_global.php file and add the following code at the end of the file to enable the plug-in designer mode.

$ _ Config ['inindeveloper'] = 2; // 1 indicates that the plug-in designer mode is enabled; 2 indicates that the plug-in designer mode is enabled and the entry points on the front-end page are displayed.


3. update the cache (to display the entry point), refresh the front-end page to see what the embedded point is. if there is an embedded point, the content can be output by the way, including the source code.
 
V. management center for instance explanation
1. Here we take "100 gold coins automatically after member registration" as an example (mainly to teach database operations)
2. enter the most basic information about the plug-in.

(Note: After submission, remember to create a folder named as an identifier under the plug-in Directory, for example,./source/plugin/send_jinbi)
3. select plug-in language pack

4. add plug-in variable configuration

Click "details" at the end to edit
5. Find and enable the "100 new gold coins for registered members" plug-in the plug-in list

VI. page embedding for instance explanation
1. plug-in page embedding-General edition

2. create the send_jinbi.class.php file in the./source/plugin/send_jinbi/directory.

If (! Defined ('in _ discuz ')){

Exit ('accesssdenied ');

}

Class plugin_send_jinbi {

Function global_header (){

Global $ _ G;

$ SendConfig = array ();

$ SendConfig = $ _ G ['cache'] ['plugin'] ['send _ jinbi ']; // cache plug-in variable value

If (intval ($ sendConfig ['status']) = 1) {// whether to start the plug-in

If (isset ($ _ POST ['regsubmit ']) {// after member registration

$ Uid = intval ($ _ G ['member'] ['uid']);

If ($ uid ){

$ Jinbi_num = intval ($ sendConfig ['jinbi _ num']); // number of gold coins

Updatemembercount ($ uid, array ("extcredits2" => $ jinbi_num); // update the number of coins (this is a ready-made function of function_core.php)

// Any database operations can be performed here

}

}

}

}

}

?>

3. basically, more than half of it has been completed. you only need to enable the plug-in, update the cache (reliability point), and then register a member at the front-end. then, 100 gold coins will be automatically sent.

VII. installation, uninstallation and upgrade of instances
1. install (install. php), uninstall (uninstall. php) the page jump can be designed at will in the program. you only need to add $ finish = TRUE at the end of plug-in installation and uninstallation. the runquery () function can be used in the script to execute SQL statements.

If (! Defined ('in _ discuz ')){

Exit ('Access Denied ');

}

/* Arbitrary code execution: start */

//..........

//..........

/* Arbitrary code execution: End */

$ Finish = TRUE;

?>

2. upgrade (upgrade. php) in the program, you can use the $ fromversion and $ toversion variables to determine the specific version number of the upgrade, and design the page jump at will, as long as you add $ finish = TRUE at the end of the plug-in upgrade, the runquery () function can be used in the script to execute SQL statements.

If (! Defined ('in _ discuz ')){

Exit ('accesssdenied ');

}

/* Arbitrary code execution: start */

//..........

//..........

/* Arbitrary code execution: End */

$ Finish = TRUE;

?>

3. for other functions, see other plug-ins.

8. plug-in language pack for instance explanation
1. open the previously created language pack file (data/plugindata/send_jinbi.lang.php) and add the following code. the red part is the unique identifier of the plug-in:
// Language pack for the script file
$ Scriptlang ['send _ jinbi '] = array (
'Info' => 'language pack of the script file'
);

// Language Pack of the template file
$ Templatelang ['send _ jinbi '] = array (
'Info' => 'language pack for template file'
);

// Language Pack for script installation, upgrade, and uninstallation
$ Installlang ['send _ jinbi '] = array (
'Info' => 'language packs for script installation, upgrade, and uninstallation'
);
?>

2. call the language pack
The template file language pack is called by {lang send_jinbi: info.
The language pack of the script file is called in the program script by lang ('In in/send_jinbi ', 'info.
The installation script calls the language pack of the installation script file and gets it directly using the $ installlang variable. For example, $ installlang ['info'].

3. export the language pack
 
The exported language package is discuz_plugin_send_jinbi.xml.
Next, convert discuz_plugin_send_jinbi.xml:
Discuz_plugin_send_jinbi_ SC _GBK.xml (simplified Chinese GBK)
Discuz_plugin_send_jinbi_ SC _UTF8.xml (simplified Chinese UTF8)
Discuz_plugin_send_jinbi_TC_BIG5.xml (BIG5, traditional Chinese)
Discuz_plugin_send_jinbi_TC_UTF8.xml (traditional Chinese UTF8)
Put these four files in the plug-in./source/plugin/send_jinbi/directory.

4. during installation:
 

IX. page functions added for instance explanation

1. add features on the original page (here we use the registration page as an example)
 

2. add the following code at the end of the previous./source/plugin/send_jinbi/send_jinbi.class.php file:
Class plugin_send_jinbi_member extends plugin_send_jinbi {// the class name plugin_send_jinbi_member. The last member should be the corresponding module name! I am not very clear about the specifics. I hope you can give me some advice! In short, it will change. if it is a forum, it will be forum.
Function register_input () {// the function name is actually the name of the embedded point on the foreground page.
$ Lang = lang ('In in/send_jinbi '); // gets the language pack for this plug-in.
$ Bind = "". $ lang ['info']. ""; // display the call Language Pack
Return $ bind;
}
}
3. update the cache and go to the registration page to check the effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.