![]() |
FOSSology Advancing open source analysis and development |
In this tutorial example, we will extend FOSSology through the addition of a “Hello World” plugin, written in php. Our new plugin will be accessed and executed through the user interface (Fossology UI) by clicking on the top level Help menu and selecting “Hello World” from its pulldown menu.
Every plugin, including this “Hello World” example, extends a common FOSSology plugin template called FO_Plugin. In this example, we use several variables defined by FO_Plugin. FO_Plugin will be discussed in more detail in the following tutorial section.
Click here to see the complete code for the “Hello World” example.
To start, every plugin must always include the following code to check for global readiness. The GlobalReady variable is set during FOSSology UI initialization and must be checked by plugins to ensure code is being executed on the same system where the UI is running (this prevents hacking attempts).
/*************************************************
Restrict usage: Every PHP file should have this
at the very beginning.
This prevents hacking attempts.
*************************************************/
global $GlobalReady; /* Always include these two lines at the start to insure the plugin */
if (!isset($GlobalReady)) { exit; } /* is running on the same system on which the UI is running */
...
The class and variable declaration section defines the class name; it should look like this:
class ui_hello extends FO_Plugin /* This is the class name (ui_hello) and */
/* it extends functionality of the FO_Plugin */
{
public $Name = "hello"; /* This is the name by which FOSSology identifies the plugin */
public $Title = "Hello World Example"; /* This is the title that will be displayed in the UI */
public $MenuList = "Help::Hello World"; /* This is the description that will be displayed in the pulldown menu */
public $LoginFlag = 0; /* you do not need to be logged into the UI to execute this plugin */
protected $_Text="Hello World"; /* This is the output message that will be displayed in the UI */
Next, we include an Output() function to display our “Hello World” string. The function is written to handle 3 different output types, XML, HTML and text.
function Output()
{
if ($this->State != PLUGIN_STATE_READY) { return; } /* State is set by FO_Plugin */
$V="";
switch($this->OutputType) /* OutputType is set by FO_Plugin */
{
case "XML":
$V .= "<text>$this->_Text</text>\n";
break;
case "HTML":
$V .= "<b>$this->_Text</b>\n";
break;
case "Text":
$V .= "$this->_Text\n";
break;
default:
break;
}
if (!$this->OutputToStdout) { return($V); } /* OutputToStdout is a function defined by FO_Plugin */
print($V);
return;
}
Finally, you'll need to:
$NewPlugin = new ui_hello; $NewPlugin->Initialize(); /* Initialize is a function defined by FO_Plugin */
In order to test your new plugin, copy it to the UI plugin directory.
sudo cp hello.php /usr/local/share/fossology/www/plugins/hello.php
Open a browser to the FOSSology UI (http://<your_server>/repo). Place your cursor on the Help tab at the top of the UI window to display the Help pulldown menu. You should see an option in the pulldown menu for “Hello World”. Click on this option to run your plugin.