-
Notifications
You must be signed in to change notification settings - Fork 2
Using the hook
The "hook" allows you to execute any action as the user. This means that instead of renaming directories as the "panel" account, which is the account that HelioPanel is run under, you can execute actions as the user's linux account. The hook is extremely useful in cases like the file manager, which does almost everything through the hook.
Consider this controller:
<?php
namespace YourVendor\YourBundle\Controller;
use HelioNetworks\HelioPanelBundle\Controller\HelioPanelAbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class YourController extends HelioPanelAbstractController
{
/**
* @Route("/your/route", name="your_route")
* @Template()
*/
public function yourAction()
{
$content = $this->getHook()->file_get_contents('/home/user/public_html/index.html');
return array();
}
}
What is this controller doing? Well, as you can see, we're fetching the hook ($this->getHook()
), then calling a built in PHP function to read the contents of a file. We're executing a PHP function as the linux user that's associated with the account, which is what the hook was designed to do.
What if you needed to call a function like phpinfo()
with the hook, and return the contents? Let's try it in the example below:
<?php
namespace YourVendor\YourBundle\Controller;
use HelioNetworks\HelioPanelBundle\Controller\HelioPanelAbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class YourController extends HelioPanelAbstractController
{
/**
* @Route("/your/route", name="your_route")
* @Template()
*/
public function yourAction()
{
$phpinfo = $this->getHook()->phpinfo();
return array();
}
}
Can you see what's wrong with this example? $phpinfo
will be empty, because phpinfo()
doesn't return anything, it outputs it to stdout. Therefore, you cannot execute phpinfo()
using the hook, it won't work, or can you?
What if we had a function like this:
<?php
function getPhpInfo() {
ob_start();
phpinfo();
return ob_get_clean();
}
This function would allow us the get the output of phpinfo()
using the hook, and return it as a string. If only we could add our own custom functions to the hook.
Create the following file (you can infer it's name from the namespace):
<?php
namespace YourVendor\YourBundle\Hook;
use HelioNetworks\HelioPanel\Hook\HookSectionInterface;
class GetPhpInfoSection implements HookSectionInterface
{
public function getName()
{
return 'getPhpInfo()';
}
public function getCode()
{
return <<<'PHP'
ob_start();
phpinfo();
return ob_get_clean();
PHP;
}
}
What we're doing here is creating our own custom function in the hook. Now that we've defined it, let's actually add it in. Define the following service:
heliopanel.hook.section.get_php_info:
class: YourVendor\YourBundle\Hook\GetPhpInfoSection
tags:
- { name: heliopanel.hook_manager }
That's it! Your section will automatically be added to the hook. Then, you can call it like any other function as shown above. Be sure to not name functions the same as built in functions, you will break the hook!