Skip to content

Instantly share code, notes, and snippets.

@amon-ra
Created September 14, 2022 11:06
Show Gist options
  • Save amon-ra/5688b9afc4ed73453e7351fa1ba35c71 to your computer and use it in GitHub Desktop.
Save amon-ra/5688b9afc4ed73453e7351fa1ba35c71 to your computer and use it in GitHub Desktop.
<?php
// This plugin insert variables in form of {PROXY} to templates
//
// /usr/local/ispconfig/server/lib/classes/plugins.inc.php:120
// $ret = call_user_func(array($app->loaded_plugins[$plugin_name], $function_name), $event_name, $data);
// if (isset($ret['data'])) $data = $ret['data'];
//
// install in /usr/local/ispconfig/server/pugins_enabled
function get_trans($name, $val)
{
return array(
'{' . $name . '}' => $val,
'{upperCase ' . $name . '}' => strtoupper($val),
'{lowerCase ' . $name . '}' => strtolower($val),
'{camelCase ' . $name . '}' => lcfirst(
preg_replace('/[^a-zA-Z0-9]/', '', ucwords($val))
),
'{pascalCase ' . $name . '}' =>
preg_replace('/[^a-zA-Z0-9]/', '', ucwords($val)),
'{snakeCase ' . $name . '}' => preg_replace(
'/[^a-zA-Z0-9]/',
'_',
$val
),
'{upperSnakeCase ' . $name . '}' =>
preg_replace(
'/[^a-zA-Z0-9]/',
'_',
strtoupper($val)
),
'{kebabCase ' . $name . '}' =>
preg_replace(
'/[^a-zA-Z0-9]/',
'-',
$val
),
'{lowerDotCase ' . $name . '}' =>
preg_replace(
'/[^a-zA-Z0-9]/',
'.',
$val
),
);
}
class aaa_proxy_plugin
{
var $plugin_name = 'aaa_proxy_plugin';
var $class_name = 'aaa_proxy_plugin';
function onInstall()
{
global $conf;
return $conf['services']['web'] == true;
}
/*
This function is called when the plugin is loaded
*/
function onLoad()
{
global $app;
$app->plugins->registerEvent('web_domain_insert', $this->plugin_name, 'update');
$app->plugins->registerEvent('web_domain_update', $this->plugin_name, 'update');
// $app->plugins->registerEvent('web_domain_delete', $this->plugin_name, 'delete');
}
function update($event_name, $data)
{
global $app, $conf;
$ret = array('data' => $data);
$app->uses('system');
// load the server configuration options
$app->uses('getconf');
// file_put_contents(
// "/tmp/proxy.log",
// $event_name,
// FILE_APPEND
// );
file_put_contents("/tmp/proxy.log", print_r($data, true), FILE_APPEND);
$web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
$vhost_file = $web_config['nginx_vhost_conf_dir'] . '/' . $data['new']['domain'] . '.vhost';
$proxy_url = $data['new']['redirect_path'];
if ($proxy_url != "") {
$domain = parse_url($proxy_url);
$proxy_server = $domain['host'];
} else {
$domain = "";
$proxy_server = "";
}
$nginx_directives = $data['new']['nginx_directives'];
if (intval($data['new']['directive_snippets_id']) > 0) {
$snippet = $app->db->queryOneRecord("SELECT * FROM directive_snippets WHERE directive_snippets_id = ? AND type = 'nginx' AND active = 'y' AND customer_viewable = 'y'", $data['new']['directive_snippets_id']);
if (isset($snippet['snippet'])) {
$nginx_directives = $snippet['snippet'];
} else {
$nginx_directives = $data['new']['nginx_directives'];
}
$ret['data']['new']['directive_snippets_id'] = 0;
}
$folder_directives = $data['new']['folder_directive_snippets'];
$proxy_directives = $data['new']['proxy_directives'];
$trans = array_merge(
get_trans(
'PROXY',
$proxy_server
),
get_trans('PROXYURL', $proxy_url),
get_trans('DOMAIN', $data['new']['domain']),
get_trans(
'USER',
$data['new']['system_user']
),
get_trans('GROUP', $data['new']['system_group']),
);
// file_put_contents("/tmp/proxy.log", print_r($trans, true), FILE_APPEND);
// $str = file_get_contents($vhost_file);
// $str = strtr($str, $trans);
// file_put_contents($vhost_file, $str);
$ret['data']['new']['nginx_directives'] = strtr($nginx_directives, $trans);
$ret['data']['new']['folder_directive_snippets'] = strtr($folder_directives, $trans);
$ret['data']['new']['proxy_directives'] = strtr($proxy_directives, $trans);
// file_put_contents("/tmp/proxy.log", print_r($ret, true), FILE_APPEND);
return $ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment