Skip to content

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
sibprogrammer committed Jul 10, 2014
0 parents commit 49cbd95
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
4 changes: 4 additions & 0 deletions htdocs/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

$application = new pm_Application();
$application->run();
10 changes: 10 additions & 0 deletions meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<module>
<id>nat</id>
<name>NAT Manager</name>
<description>Plugin provides an ability to manage public/private IP addresses for proper working behind NAT.</description>
<version>1.0</version>
<release>1</release>
<vendor>Parallels, Inc.</vendor>
<plesk_min_version>11.5.30</plesk_min_version>
</module>
81 changes: 81 additions & 0 deletions plib/controllers/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

class IndexController extends pm_Controller_Action
{

public function indexAction()
{
$this->view->pageTitle = $this->lmsg('indexPageTitle');
$this->view->list = $this->_getIpsList();
}

public function indexDataAction()
{
$this->_helper->json($this->_getIpsList()->fetchData());
}

public function updateAddressAction()
{
$ips = Modules_Nat_NatManager::getIpAddresses();
// TODO: add validation
$mainIp = $this->_request->getParam('ip');
$publicIp = $ips[$mainIp];

$this->view->pageTitle = $this->lmsg('updateAddressPageTitle');

// TODO: add localization
$form = new pm_Form_Simple();
$form->addElement('text', 'mainIp', array(
'label' => 'Main (or private IP)',
'value' => $mainIp,
'required' => true,
'validators' => array(
array('NotEmpty', true),
),
));
$form->addElement('text', 'publicIp', array(
'label' => 'Public IP',
'value' => $publicIp,
));
$form->addControlButtons(array(
'cancelLink' => pm_Context::getBaseUrl(),
));

if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getPost())) {
Modules_Nat_NatManager::updateAddress($form->getValue('mainIp'), $form->getValue('publicIp'));

$this->_status->addMessage('info', 'IP address was successfully updated.');
$this->_helper->json(array('redirect' => pm_Context::getBaseUrl()));
}

$this->view->form = $form;
}

private function _getIpsList()
{
$ips = Modules_Nat_NatManager::getIpAddresses();
$data = array();
foreach (Modules_Nat_NatManager::getIpAddresses() as $mainIp => $publicIp) {
$data[] = array(
'column-1' => '<a href="' . pm_Context::getActionUrl('index', 'update-address') . '?ip=' . $mainIp . '">' . $mainIp . '</a>',
'column-2' => $publicIp,
);
}

$list = new pm_View_List_Simple($this->view, $this->_request);
$list->setData($data);
$list->setColumns(array(
'column-1' => array(
'title' => 'Main (or private IP)',
'noEscape' => true,
),
'column-2' => array(
'title' => 'Public IP',
),
));
$list->setDataUrl(array('action' => 'list-data'));

return $list;
}

}
30 changes: 30 additions & 0 deletions plib/library/NatManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

class Modules_Nat_NatManager
{

public static function getIpAddresses()
{
$apiResponse = pm_ApiRpc::getService()->call('<ip><get/></ip>');
$list = array();
foreach ($apiResponse->ip->get->result->addresses->ip_info as $addressNode) {
$list[(string)$addressNode->ip_address] = (string)$addressNode->public_ip_address;
}

return $list;
}

public static function updateAddress($mainIp, $publicIp)
{
$apiResponse = pm_ApiRpc::getService()->call(
'<ip>' .
'<set>' .
"<filter><ip_address>$mainIp</ip_address></filter>" .
"<public_ip_address>$publicIp</public_ip_address>" .
'</set>' .
'</ip>');

// TODO: add error handler
}

}
6 changes: 6 additions & 0 deletions plib/resources/locales/en-US.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$messages = array(
'indexPageTitle' => 'IP Addresses for NAT',
'updateAddressPageTitle' => 'IP Address Traslation',
);
1 change: 1 addition & 0 deletions plib/views/scripts/index/index.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php echo $this->renderList($this->list); ?>
1 change: 1 addition & 0 deletions plib/views/scripts/index/update-address.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php echo $this->form; ?>

0 comments on commit 49cbd95

Please sign in to comment.