mize03
Posts: 3
Joined: Wed Nov 21, 2012 7:21 pm

joomla plugin to control gpios

Wed Nov 21, 2012 7:50 pm

Hello,

i installed joomla on my raspberry pi and use WiringPi to control the gpios.
I want a easy use of the gpios via joomla.

So i manipulate an existing joomla plugin for my use and it works fine :-)
When i write {gpio}23{/gpio} in the editor of joomla it creates a a button on the homepage to toggle the pin :-)

Here a screenshot:
Image

But if i try to build my own plugin it does not work.
I can install the plugin but it has no effect.

The code of the xml file:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<extension version="1.7" type="plugin" group="content">
        <name>Raspberry Pi Gpio Toggle Plugin</name>
        <author>mize03</author>
        <creationDate>2012-11-21</creationDate>
        <copyright></copyright>
        <license></license>
        <authorEmail></authorEmail>
        <authorUrl></authorUrl>
        <version>1.0</version>
        <description>A plugin to toggle the gpios of the raspberry pi. example: {gpio}15{/gpio}</description>
        <files>
                <filename plugin="raspberryPiGpioTooglePlugin">raspberryPiGpioTooglePlugin.php</filename>
                <filename>index.html</filename>
        </files>
</extension>
Code of the php file:

Code: Select all

<?php
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');

class rasppigpio extends JPlugin {
  public function onContentPrepare( $context, &$article, &$params, $limitstart=0 )
  {
    $txt = $article->text;
	//check message to set a pin
    if(isset($_POST['Button'])) 
   {
        if(substr($_POST['Button'], 0, 4) == "gpio")
        {
                shell_exec($_POST['Button']);
        }
   }
	//replace all {gpio}NBR{/gpio} with a button
    if(preg_match_all("@{gpio}(.*){/gpio}@Us", $article->text, $matches, PREG_PATTERN_ORDER) > 0)
    {
        foreach($matches[0] as $match)
        {
           $pos = strpos($articel->text, $match);
           $pin_nbr = preg_replace("@{.+?}@", "", $match);
           shell_exec("gpio -g mode $pin_nbr out");
           $pin_status = trim(@shell_exec("gpio -g read $pin_nbr"));
           $butt_text = "<FORM method=\"post\" action\"window.location.href\"
                  </td><button type=\"submit\" name=\"Button\"
                  value=\"gpio -g write $pin_nbr ".(($pin_status==0)?"1":"0")."\">".
                  "GPIO $pin_nbr: ".(($pin_status == 0) ? "LOW" : "HIGH")."</button></FORM>";
           $txt = str_replace($match, $butt_text, $txt);
        }
    }
    $article->text = $txt;
    return true;
  }
}
Does anybody has experience with the developing of joomla plugins?
I dont understand why it works with the existing plugin!

Thank you for your help ;-)
Attachments
raspberryPiGpioTooglePlugin.zip
joomla plugin for raspberry to control the gpios, does not work!
(1.37 KiB) Downloaded 46 times

c.cam108
Posts: 34
Joined: Thu Aug 16, 2012 9:17 pm

Re: joomla plugin to control gpios

Wed Nov 21, 2012 11:45 pm

You are allowing any POSTed data to be executed to the shell. This is a Very Bad Thing. Imagine what I could do with this HTML:

Code: Select all

<form method="post"><input name="Button" type="text"><input type="submit"></form>
Other than that, nice idea.

I don't know much about Joomla plugins to help you, but your HTML for creating the button is malformed: You have a stray </TD> and the opening <FORM tag is missing its '>'.

mize03
Posts: 3
Joined: Wed Nov 21, 2012 7:21 pm

Re: joomla plugin to control gpios

Thu Nov 22, 2012 9:55 pm

Hey,

thanks for your reply.
You are right, that's a big security problem.

Today i solved my problem to install a plugin.
It is forbidden to use uppercase letters in the filenames and you must use a special php classname.

I will post the files after fixing the security problem.

mize03
Posts: 3
Joined: Wed Nov 21, 2012 7:21 pm

Re: joomla plugin to control gpios

Fri Nov 23, 2012 10:11 am

Here is the finale plugin :-)

php file:

Code: Select all

<?php
defined('_JEXEC') or die;
jimport('joomla.plugin.plugin');

class plgContentraspgpio extends JPlugin {
  public function onContentPrepare( $context, &$article, &$params, $limitstart=0 )
  {
    //save the text of the article in txt
    $txt = $article->text;
	
	//all gpio numbers
    $gpio_numbers = array(0, 1, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23, 24, 25);
	
    //check if there is a post to toggle a pin and check if the pinnumber is valid
    if(isset($_POST['gpio']) and in_array($_POST['gpio'], $gpio_numbers))
    {
	    //read the pin status to toggle the pin
		$pin_status = trim(@shell_exec("gpio -g read $_POST[gpio]"));
		$pin_status = ($pin_status == "0") ? "1" : "0";
        shell_exec("gpio -g write $_POST[gpio] $pin_status");
    }
	//if the pinstatus is not valid show a error message
    elseif(! in_array($_POST['gpio'], $gpio_numbers))
    {
		$txt = "POST ERROR, please contact admin. GPIO number $_POST[gpio] is not valid!".$txt;
    }
    //search all {gpio}NBR_OF_GPIO{/gpio}
    if(preg_match_all("@{gpio}(.*){/gpio}@Us", $article->text, $matches, PREG_PATTERN_ORDER) > 0)
    {
	    //match example: {gpio}15{/gpio}
        foreach($matches[0] as $match)
        {
           $pos = strpos($articel->text, $match);
           $pin_nbr = preg_replace("@{.+?}@", "", $match);
		   //check if pinnumber is valid
           if(in_array($pin_nbr, $gpio_numbers))
		   {
				//set pin as output
				shell_exec("gpio -g mode $pin_nbr out"); 
				//read the current pin status, 
				$pin_status = trim(@shell_exec("gpio -g read $pin_nbr"));
				$butt_text = "<form method=\"post\" action\"window.location.href\"
						></td><button type=\"submit\" name=\"gpio\"
						value=\"$pin_nbr\">".
						  "GPIO $pin_nbr: ".(($pin_status == 0) ? "LOW" : "HIGH")."</button></FORM>";
				//replace the match with the button
				$txt = str_replace($match, $butt_text, $txt);
		   }
		   //show error message if pinnumber is not valid
		   else
		   {
				$txt = "ARTICLEERROR, please contact admin. GPIO number $pin_nbr is not valid!".$txt;
		   }
        }
    }
	//replace the article text with txt
    $article->text = $txt;
    return true;
  }
}
xml file:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<extension version="1.7" type="plugin" group="content">
        <name>Raspberry Pi Gpio Toggle Plugin</name>
        <author>mize03</author>
        <creationDate>2012-11-21</creationDate>
        <copyright></copyright>
        <license>GNU General Public License. </license>
        <authorEmail></authorEmail>
        <authorUrl></authorUrl>
        <version>1.1</version>
        <description>A plugin to toggle the gpios of the raspberry pi. example: {gpio}15{/gpio}</description>
        <files>
                <filename plugin="raspgpio">raspgpio.php</filename>
                <filename>index.html</filename>
        </files>
</extension>
Attachments
raspgpio.zip
Joomla plugin to toggle the raspberry gpios, example for the editor to create button to toggle pin 23
(1.64 KiB) Downloaded 79 times

Return to “General discussion”