-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathjson.php
More file actions
94 lines (82 loc) · 2.41 KB
/
json.php
File metadata and controls
94 lines (82 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
require_once('__autoload.php');
header("Content-Type: application/json; charset=UTF-8");
// TODO: Limit class selection from $_GET
function getTemplates()
{
return array(
'BasicRouter' => 'Basic Cisco router, 2 interfaces',
'TelenetCFN' => 'Router with switch EHWIC for Telenet CFN',
'StackableSwitch' => 'Stackable layer 2 switch',
'DMVPN' => 'DMVPN',
'StandaloneAP' => 'Standalone access point'
);
}
function getTemplateOptions($templateName)
{
$ClassName = "Cisco\\{$templateName}";
$C = new $ClassName;
return $C->GetOpts();
}
function getTemplateOptionsByGroup($templateName)
{
$ClassName = "Cisco\\{$templateName}";
$C = new $ClassName;
return $C->GetOptsByGroup();
}
function generate($templateName, $Opts = array())
{
$ClassName = "Cisco\\{$templateName}";
$C = new $ClassName;
$Options = array();
foreach($Opts as $Entry) {
if(preg_match("#^(.*)\[(.*)\]\[\]$#", $Entry['name'], $Matches)) {
$Options[$Matches[1]][$Matches[2]][] = $Entry['value'];
} else {
$Options[$Entry['name']] = $Entry['value'];
}
}
foreach ($C->GetOpts() as $Name => $Spec) {
if(!isset($Options[$Name])) {
continue;
}
if($Spec->getType() == 'bool') {
if($Options[$Name] == 'true') {
$C->setOptVal($Name, true);
} else {
$C->setOptVal($Name, false);
}
} else {
$C->setOptVal($Name, $Options[$Name]);
}
}
$C->Generate();
$C->sortBlocks();
return htmlentities($C->getConfig());
}
if(isset($_GET['template'])) {
if(!array_key_exists($_GET['template'], getTemplates())){
$Response['success'] = false;
$Response['errortype'] = 'generic';
$Response['errormsg'] = 'Invalid template';
echo json_encode($Response);
exit;
}
}
switch ($_GET['f']) {
case "getTemplates":
$Return = getTemplates();
break;
case "getTemplateOptions":
$Return = getTemplateOptions($_GET['template']);
break;
case "getTemplateOptionsByGroup":
$Return = getTemplateOptionsByGroup($_GET['template']);
break;
case "generate":
$Return = generate($_GET['template'], $_GET['options']);
break;
}
$Response['success'] = true;
$Response['data'] = $Return;
echo json_encode($Response);