Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
O
OpnSense
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
OpnSense
Commits
6f022153
Commit
6f022153
authored
Mar 11, 2015
by
Ad Schellevis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
remove complexity in fromArray, write new file in stead of update xml object approach.
parent
23abb70c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
63 deletions
+19
-63
Config.php
src/opnsense/mvc/app/library/OPNsense/Core/Config.php
+19
-63
No files found.
src/opnsense/mvc/app/library/OPNsense/Core/Config.php
View file @
6f022153
...
...
@@ -111,87 +111,43 @@ class Config extends Singleton
}
/**
* update config with array structure (backwards compatibility mode)
* update
(reset)
config with array structure (backwards compatibility mode)
* @param $source source array structure
* @param null $node simplexml node
* @
return number of updates
* @
param null|string $parentTagName
* @throws ConfigException
*/
public
function
fromArray
(
$source
,
$node
=
null
)
public
function
fromArray
(
$source
,
$node
=
null
,
$parentTagName
=
null
)
{
$this
->
checkvalid
();
$updatecount
=
0
;
// root node
if
(
$node
==
null
)
{
$node
=
$this
->
simplexml
;
$this
->
configxml
=
new
\DOMDocument
(
'1.0'
);
$this
->
configxml
->
loadXML
(
'<'
.
$this
->
simplexml
[
0
]
->
getName
()
.
'/>'
);
$this
->
simplexml
=
simplexml_import_dom
(
$this
->
configxml
);
$node
=
$this
->
simplexml
;
}
$activeNodes
=
array
();
$useIndex
=
false
;
// tag recurring, use index
foreach
(
$source
as
$itemKey
=>
$itemValue
)
{
// find / create target node
if
(
is_numeric
(
$itemKey
))
{
$useIndex
=
true
;
// recurring item, select by number
$items
=
$node
->
xpath
(
"./*"
);
if
(
count
(
$items
)
>
$itemKey
)
{
$targetNode
=
$items
[
$itemKey
];
}
else
{
// new sequence, add item
$targetNode
=
$node
->
addChild
(
$node
->
getName
());
}
}
elseif
(
!
isset
(
$node
->
{
$itemKey
}))
{
// new node, add
if
(
is_numeric
(
$itemKey
))
{
$targetNode
=
$node
->
xpath
(
".."
)[
0
]
->
addChild
(
$node
->
getName
());
}
else
{
$targetNode
=
$node
->
addChild
(
$itemKey
);
}
// recurring tag (content), use parent tagname.
$childNode
=
$node
->
addChild
(
$parentTagName
);
}
elseif
(
is_array
(
$itemValue
)
&&
!
(
array_keys
(
$itemValue
)
!==
range
(
0
,
count
(
$itemValue
)
-
1
)))
{
// recurring tag, skip placeholder.
$childNode
=
$node
;
}
else
{
// if items are a recurring list, skip one level.
$targetNode
=
$node
->
xpath
(
"//"
.
$itemKey
);
if
(
count
(
$targetNode
)
>
1
)
{
$targetNode
=
$node
;
}
else
{
$targetNode
=
$node
->
{
$itemKey
};
}
// add new child
$childNode
=
$node
->
addChild
(
$itemKey
);
}
// add active node
$activeNodes
[]
=
$itemKey
;
// (propagate) update
// set content, propagate container items.
if
(
is_array
(
$itemValue
))
{
$updatecount
+=
$this
->
fromArray
(
$itemValue
,
$targetNode
);
}
elseif
(
$itemValue
!=
$targetNode
->
{
$itemKey
}
->
__toString
())
{
$targetNode
->
{
$itemKey
}
=
$itemValue
;
$updatecount
++
;
}
}
// remove nodes not in source
$removeItems
=
array
();
$itemId
=
0
;
foreach
(
$node
->
children
()
as
$xmlNode
)
{
if
((
!
in_array
(
$itemId
,
$activeNodes
)
&&
$useIndex
)
||
!
in_array
(
$xmlNode
->
getName
(),
$activeNodes
)
)
{
$removeItems
[]
=
$xmlNode
;
$this
->
fromArray
(
$itemValue
,
$childNode
,
$itemKey
);
}
else
{
$childNode
[
0
]
=
$itemValue
;
}
$itemId
++
;
}
foreach
(
$removeItems
as
$xmlNode
)
{
$dom
=
dom_import_simplexml
(
$xmlNode
);
$dom
->
parentNode
->
removeChild
(
$dom
);
$updatecount
++
;
}
// return number of changes
return
$updatecount
;
}
/**
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment