theme, plugins und config für die Webseite der Baptisten Hohenacker https://baptisten-hohenacker.de
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.9 KiB

<?php
namespace Grav\Plugin;
use \Grav\Common\Grav;
use Grav\Common\Utils;
class PythonScriptTwigExtension extends \Twig_Extension
{
/**
* Returns extension name.
*
* @return string
*/
public function getName()
{
return 'PythonScriptTwigExtension';
}
public function getFilters()
{
//associate filter name with php callable (here: class method)
return [
new \Twig_SimpleFilter('pythonscript', [$this, 'pythonscript_paramfirst'])
];
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('pythonscript', [$this, 'pythonscript'])
];
}
public function pythonscript_paramfirst($param,$path)
{
//return $path . escapeshellarg($param);
return $this->pythonscript($path,escapeshellarg($param));
}
public function pythonscript($path,$param)
{
$path_info = pathinfo($path);
$config = Grav::instance()['config']->get('plugins.python-script');
$command = "python " . $path;
//$command = "test";
if (in_array($path_info['extension'], $config['allowed_extensions'])) {
if (Utils::startsWith($path, '/')) {
if ($config['allow_in_grav'] && file_exists(GRAV_ROOT . $path)) {
$command = "python " .GRAV_ROOT. $path;
} elseif ($config['allow_in_filesystem'] && file_exists($path)) {
$command = "python " . $path;
}
} else {
$page_path = Grav::instance()['page']->path() . '/' . $path;
//$param=Grav::instance()['page']->path() . '/' . $param;
if ($config['allow_in_page'] && file_exists($page_path)) {
$command = "python " . $page_path;
}
}
}
$command .= " ". $param;
$output = shell_exec($command);
//return $command ."<br>" . $output;
return $output;
}
}