true,
'deleteServer' => true,
'editServer' => true
);
public function config( $params = false )
{
$GLOBALS['phpgw']->common->phpgw_header();
echo parse_navbar();
if( !$GLOBALS['phpgw']->acl->check('run',1,'admin') )
{
$GLOBALS['phpgw']->redirect_link('/admin/index.php');
}
$table_servers = "";
if( $_POST['back'] )
{
$GLOBALS['phpgw']->redirect($GLOBALS['phpgw']->link('/admin/index.php'));
}
else
{
if( $_POST && $params == false )
{
$this->save($_POST);
}
$servers = $this->readFile();
if( count($servers) > 0 )
{
$serversTable = "";
foreach( $servers as $key => $value )
{
if( substr($key, 0, 11) == "ServersRest")
{
$table_servers .= "
";
$table_servers .= " ".$value['serverName']." | ";
$table_servers .= " ".$value['serverDescription']." | ";
$table_servers .= " ".$value['serverUrl']." | ";
$table_servers .= " ".$value['serverContext']." | ";
$table_servers .= " ".( ($value['serverStatus'] == 1 ) ? "Habilitado" : "Desabilitado" )." | ";
$table_servers .= " link('/index.php','menuaction=rest.uiconfig.editServer&serverID='.$value['serverID'].'')."'>Editar | ";
$table_servers .= " link('/index.php','menuaction=rest.uiconfig.deleteServer&serverID='.$value['serverID'].'')."'>Excluir | ";
$table_servers .= "
";
}
}
}
$GLOBALS['phpgw']->template->set_file(array('rest'=>'config.tpl'));
$GLOBALS['phpgw']->template->set_block('rest','pagina');
$GLOBALS['phpgw']->template->set_var( array(
'lang_Server_Name' => lang('Server Name'),
'lang_Description' => lang('Description'),
'lang_URL' => lang('URL'),
'lang_API_Rest' => lang('API REST'),
'lang_Server_Status' => lang('Server Status'),
'lang_Enabled' => lang('Enabled'),
'lang_Disabled' => lang('Disabled'),
'lang_Save' => lang('Save'),
'lang_Back' => lang('Back'),
'lang_Configuring_the_REST_API' => lang('Configuring the REST API'),
'lang_Edit' => lang('Edit'),
'lang_Delete' => lang('Delete'),
'table_servers' => $table_servers,
'server_id' => (($params && is_array($params) ) ? $params['serverID'] : ""),
'server_name' => (($params && is_array($params) ) ? $params['serverName'] : ""),
'server_description' => (($params && is_array($params) ) ? $params['serverDescription'] : ""),
'server_url' => (($params && is_array($params) ) ? $params['serverUrl'] : ""),
'server_rest' => (($params && is_array($params) ) ? $params['serverContext'] : ""),
'selected_statusServer_true' => (($params && is_array($params) ) ? (($params['serverStatus'] == "0" ) ? "selected=true" : "") : ""),
'selected_statusServer_false' => (($params && is_array($params) ) ? (($params['serverStatus'] == "0" ) ? "selected=true" : "") : "")
));
$GLOBALS['phpgw']->template->pparse('out','pagina');
}
}
public function deleteServer()
{
if( $_GET['serverID'] )
{
$serverID = $_GET['serverID'];
$servers = $this->readFile();
foreach( $servers as $key => $value )
{
if( substr($key, 0, 11) == "ServersRest")
{
if( $value['serverID'] == $serverID )
{
unset( $servers[$key] );
}
}
}
if( !$this->writeFile($servers) )
echo "Error : Permission failure when trying to write in the file ";
else
$this->config();
}
}
public function editServer()
{
if($_GET['serverID'] )
{
$serverID = $_GET['serverID'];
$servers = $this->readFile();
foreach( $servers as $key => $value )
{
if( substr($key, 0, 11) == "ServersRest")
{
if( $value['serverID'] == $serverID )
{
$server = $servers[$key];
}
}
}
}
$this->config($server);
}
private function save($params)
{
$fileConfig = parse_ini_file( __DIR__ . '/../../api/config/REST.ini', true );
if( $params['server_id'] )
{
$serverID = $params['server_id'];
$fileConfig['ServersRest-'.$serverID]['serverID'] = $serverID;
$fileConfig['ServersRest-'.$serverID]['serverName'] = $params['server_name'];
$fileConfig['ServersRest-'.$serverID]['serverDescription'] = $params['server_description'];
$fileConfig['ServersRest-'.$serverID]['serverUrl'] = $params['server_url'];
$fileConfig['ServersRest-'.$serverID]['serverContext'] = $params['server_rest'];
$fileConfig['ServersRest-'.$serverID]['serverStatus'] = ( $params['server_status'] == 'true' ) ? "1" : "0";
}
else
{
$keys = array_keys($fileConfig);
foreach($keys as $k => $v)
{
if( substr($v, 0, 11) == "ServersRest")
$ID = $fileConfig[$v]['serverID'];
}
$serverID = ((int)$ID + 1);
if( strlen($serverID) <= 2 )
$serverID = ( strlen($serverID) > 1 ) ? "0".$serverID : "00".$serverID;
$fileConfig['ServersRest-'.$serverID]['serverID'] = $serverID;
$fileConfig['ServersRest-'.$serverID]['serverName'] = $params['server_name'];
$fileConfig['ServersRest-'.$serverID]['serverDescription'] = $params['server_description'];
$fileConfig['ServersRest-'.$serverID]['serverUrl'] = $params['server_url'];
$fileConfig['ServersRest-'.$serverID]['serverContext'] = $params['server_rest'];
$fileConfig['ServersRest-'.$serverID]['serverStatus'] = ( $params['server_status'] == 'true' ) ? "1" : "0";
}
$this->writeFile($fileConfig);
}
private function readFile()
{
$fileConfig = array();
if( file_exists(__DIR__ . '/../../api/config/REST.ini') )
{
$fileConfig = parse_ini_file( __DIR__ . '/../../api/config/REST.ini', true );
}
return $fileConfig;
}
private function writeFile($config)
{
$path = __DIR__ . '/../../api/config/REST.ini';
$content = "";
foreach( $config as $key => $value )
{
if(is_array($value))
{
$content .= "[".$key."]\n";
foreach( $value as $ikey => $ivalue )
$content .= $ikey ."=". $ivalue . "\n";
}
else
{
$content .= $key . "=" . $value . "\n";
}
}
if( file_put_contents($path, $content) === false)
{
return false;
}
return true;
}
}
?>