BxDolXmlParser.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php defined('BX_DOL') or defined('BX_DOL_INSTALL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolXmlParser extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $rParser;
  12. protected function __construct()
  13. {
  14. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  15. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  16. parent::__construct();
  17. }
  18. /**
  19. * Prevent cloning the instance
  20. */
  21. public function __clone()
  22. {
  23. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  24. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  25. }
  26. /**
  27. * Get singleton instance of the class
  28. */
  29. public static function getInstance()
  30. {
  31. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  32. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolXmlParser();
  33. return $GLOBALS['bxDolClasses'][__CLASS__];
  34. }
  35. /**
  36. * Get the value of specified attribute for specified tag.
  37. */
  38. function getAttribute($sXmlContent, $sXmlTag, $sXmlAttribute)
  39. {
  40. $aValues = $aIndexes = array();
  41. $rParser = xml_parser_create("UTF-8");
  42. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  43. xml_parser_free($rParser);
  44. $sTag = strtoupper($sXmlTag);
  45. if(!isset($aIndexes[$sTag]))
  46. return false;
  47. $iFieldIndex = $aIndexes[$sTag][0];
  48. $sAttribute = strtoupper($sXmlAttribute);
  49. if(!isset($aValues[$iFieldIndex]['attributes'][$sAttribute]))
  50. return false;
  51. return $aValues[$iFieldIndex]['attributes'][$sAttribute];
  52. }
  53. /**
  54. * Get an array of attributes for specified tag or an array of tags with the same name.
  55. */
  56. function getAttributes($sXmlContent, $sXmlTagName, $sXmlTagIndex = -1)
  57. {
  58. $aValues = $aIndexes = array();
  59. $rParser = xml_parser_create("UTF-8");
  60. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  61. xml_parser_free($rParser);
  62. /**
  63. * gets two-dimensional array of attributes.
  64. * tags-attributes
  65. */
  66. if($sXmlTagIndex == -1) {
  67. $aResult = array();
  68. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  69. if(count($aTagIndexes) <= 0) return NULL;
  70. foreach($aTagIndexes as $iTagIndex)
  71. $aResult[] = $aValues[$iTagIndex]['attributes'];
  72. return $aResult;
  73. } else {
  74. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$sXmlTagIndex];
  75. return $aValues[$iTagIndex]['attributes'];
  76. }
  77. }
  78. /**
  79. * Get an array of tags or one tag if its index is specified.
  80. */
  81. function getTags($sXmlContent, $sXmlTagName, $iXmlTagIndex = -1)
  82. {
  83. $aValues = $aIndexes = array();
  84. $rParser = xml_parser_create("UTF-8");
  85. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  86. xml_parser_free($rParser);
  87. //--- Get an array of tags ---//
  88. if($iXmlTagIndex == -1) {
  89. $aResult = array();
  90. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  91. if(count($aTagIndexes) <= 0) return NULL;
  92. foreach($aTagIndexes as $iTagIndex)
  93. $aResult[] = $aValues[$iTagIndex];
  94. return $aResult;
  95. } else {
  96. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$iXmlTagIndex];
  97. return $aValues[$iTagIndex];
  98. }
  99. }
  100. /**
  101. * Gets the values of the given tag with some attribute. 'name' is used as default attribute.
  102. *
  103. * Usage:
  104. * Input:
  105. * <string name="string1"><![CDATA[value1]]></string>
  106. * <string name="string2"><![CDATA[value2]]></string>
  107. * <string name="string3"><![CDATA[value3]]></string>
  108. * Output:
  109. * array(
  110. * 'string1' => 'value1',
  111. * 'string2' => 'value2',
  112. * 'string3' => 'value3'
  113. * )
  114. */
  115. function getValues($sXmlContent, $sXmlTagName, $sXmlAttrName = 'name')
  116. {
  117. $sXmlAttrName = strtoupper($sXmlAttrName);
  118. $aValues = $aIndexes = array();
  119. $rParser = xml_parser_create("UTF-8");
  120. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  121. xml_parser_free($rParser);
  122. $sTag = strtoupper($sXmlTagName);
  123. $aTagIndexes = isset($aIndexes[$sTag]) ? $aIndexes[$sTag] : array();
  124. $aReturnValues = array();
  125. foreach($aTagIndexes as $iTagIndex) {
  126. if(!isset($aValues[$iTagIndex]['attributes'][$sXmlAttrName]))
  127. continue;
  128. $aReturnValues[$aValues[$iTagIndex]['attributes'][$sXmlAttrName]] = isset($aValues[$iTagIndex]['value']) ? $aValues[$iTagIndex]['value'] : NULL;
  129. }
  130. return $aReturnValues;
  131. }
  132. /**
  133. * Sets the values of tag where attribute "key" equals to specified.
  134. */
  135. function setValues($sXmlContent, $sXmlTagName, $aKeyValues)
  136. {
  137. $aValues = $aIndexes = array();
  138. $rParser = xml_parser_create("UTF-8");
  139. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  140. xml_parser_free($rParser);
  141. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  142. if(count($aTagIndexes) == 0) return $this->getContent();
  143. foreach($aTagIndexes as $iTagIndex)
  144. foreach($aKeyValues as $sKey => $sValue)
  145. if($aValues[$iTagIndex]['attributes']['KEY'] == $sKey) {
  146. $aValues[$iTagIndex]['value'] = $sValue;
  147. break;
  148. }
  149. return $this->getContent($aValues);
  150. }
  151. /**
  152. * Adds given values to XML content.
  153. */
  154. function addValues($sXmlContent, $sXmlTagName, $aKeyValues)
  155. {
  156. $aValues = $aIndexes = array();
  157. $rParser = xml_parser_create("UTF-8");
  158. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  159. xml_parser_free($rParser);
  160. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  161. $iLastTagIndex = $aTagIndexes[count($aTagIndexes) - 1];
  162. $iAddsCount = count($aKeyValues);
  163. $iLevel = $aValues[$iLastTagIndex]["level"];
  164. for($i=count($aValues)-1; $i>$iLastTagIndex; $i--)
  165. $aValues[$i+$iAddsCount] = $aValues[$i];
  166. $i = $iLastTagIndex;
  167. foreach($aKeyValues as $sKey => $sValue) {
  168. $i++;
  169. $aValues[$i] = Array("tag" => $sXmlTagName, "type" => "complete", "level" => $iLevel, "attributes" => Array("KEY" => $sKey), "value" => $sValue);
  170. }
  171. return $this->getContent($aValues);
  172. }
  173. /**
  174. * get content in XML format from given values array
  175. */
  176. function getContent($aValues = array())
  177. {
  178. $sContent = "";
  179. foreach($aValues as $aValue) {
  180. $sTagName = strtolower($aValue['tag']);
  181. switch($aValue['type']) {
  182. case "open":
  183. $sContent .= "<" . $sTagName . ">";
  184. break;
  185. case "complete":
  186. $sContent .= "<" . $sTagName;
  187. if(isset($aValue['attributes']))
  188. foreach($aValue['attributes'] as $sAttrKey => $sAttrValue)
  189. $sContent .= " " . strtolower($sAttrKey) . "=\"" . $sAttrValue . "\"";
  190. $sContent .= isset($aValue['value']) ? "><![CDATA[" . $aValue['value'] . "]]></" . $sTagName . ">" : " />";
  191. break;
  192. case "close":
  193. $sContent .= "</" . $sTagName . ">";
  194. break;
  195. }
  196. }
  197. return $sContent;
  198. }
  199. }
  200. /** @} */