Plug-in example: templates engine management
Our task is to add the checkbox "1C-Bitrix" on the domain edit form. Depending on the checkbox status, the configuration file Apache will contain different values for the directive open_basedir:
- the checkbox is selected — open_basedir is "none";
- the checkbox is not selected — the open_basedir value equals the domain home directory.
Usually a plug-in consists of the XML file that describes the plug-in and where you can add/hide interface elements, and a handler. The handler is a script that will be called before or after a certain action of the control panel. Add the XML to the directory /usr/local/mgr5/etc/xml/ and name it as ispmgr_mod_.xml, eg. /usr/local/mgr5/etc/xml/ispmgr_mod_bitrix.xml
XML description of the plugin
<?xml version="1.0" encoding="UTF-8"?>
<mgrdata>
<metadata name="site.edit" type="form">
<form>
<page name="additional">
<field name="site_drupal_nginx">
<input type="checkbox" name="site_drupal_nginx" />
</field>
</page>
</form>
</metadata>
<metadata name="webdomain.edit" type="form">
<form>
<page name="additional">
<field name="drupal_nginx">
<input type="checkbox" name="drupal_nginx" />
</field>
</page>
</form>
</metadata>
<lang name="ru">
<messages name="site.edit">
<msg name="site_drupal_nginx">Drupal NGINX</msg>
<msg name="hint_site_drupal_nginx">Отметьте, чтобы конфигурации NGINX были оптимизированы под Drupal.</msg>
</messages>
</lang>
<lang name="en">
<messages name="site.edit">
<msg name="site_drupal_nginx">Drupal NGINX</msg>
<msg name="hint_site_drupal_nginx">Check for optimized Drupal NGINX config.</msg>
</messages>
</lang>
</mgrdata>
The metadata section describes that a new field (name="bitrix") of the checkbox type (type="checkbox") will be added to the WWW-domain tab (page name="domain") on the edit form (type="form") of the web domain (name="webdomain.edit")
The handler section describes that after (after="yes") the web domain is edited (webdomain.edit) the script named bitrix (handler name="bitrix") of the xml type (type="xml") will be called.
The type xml means that the script receives the XML file with parameters to stdin from the control panel. The plug-in must return the same XML to stdout where custom parameters can be added or existing parameters can be modified.
The lang section defines description and a hint in the Russian and English languages.
After you have added the custom xml, restart the control panel
/usr/local/mgr5/sbin/mgrctl -m ispmgr exit
Handler
Add the handler script to the directory /usr/local/mgr5/addon . The file must have the same name as it is specified in the handler section of the xml. In our example, this is the file /usr/local/mgr5/addon/bitrix:
#!/bin/bash
if [[ "$PARAM_bitrix" = "on" ]]
then
cat | sed 's|</doc>$|<params><CUSTOM_BASEDIR>none</CUSTOM_BASEDIR></params></doc>|'
else
cat | sed 's|</doc>$|<params><CUSTOM_BASEDIR>'$PARAM_docroot':.</CUSTOM_BASEDIR></params></doc>|'
fi
Set permissions for the handler with the following commands:
chmod 750 /usr/local/mgr5/addon/<handler_file_name>
chown 0:0 /usr/local/mgr5/addon/<handler_file_name>
The value of the variable $PARAM_bitrix is checked in the condition. The control panel sends some parameters to the environment variables and they can be used in the plug-in handler. $PARAM_bitrix is the variable that can be on, if the checkbox "1C-Bitrix" is selected on the domain edit form and can be off, if the checkbox is not selected.
The plug-in sends to the control panel the same xml file that it has received to stdin, but it adds its custom parameter CUSTOM_BASEDIR to the xml. This parameter can have the value none, if the checkbox "1C-Bitrix" is selected or , if the checkbox is not selected.
Finally, edit the template engine file that will define what directives will be specified in the configuration file Apache.
In the file /usr/local/mgr5/etc/templates/apache2-vhosts.template change
php_admin_value open_basedir "{% $BASEDIR_PATH %}"
into
php_admin_value open_basedir "{% $CUSTOM_BASEDIR %}"
Now the value open_basedir will be defined by the variable CUSTOM_BASEDIR that we add with the plug-in and define its value ourselves depending on the checkbox "1C-Bitrix" on the domain edit form.