class.xmlsql.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. All rights
  5. # reserved.
  6. #
  7. # DotClear is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # DotClear is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with DotClear; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. # ***** END LICENSE BLOCK *****
  22. class xmlsql
  23. {
  24. var $job = array();
  25. var $xml;
  26. var $con;
  27. var $_action;
  28. var $_current_tag_cdata;
  29. var $_subtable = array(
  30. 'test' => array(
  31. 'sql' => NULL,
  32. 'eq' => 'eq',
  33. 'value' => NULL,
  34. 'label' => NULL,
  35. 'string' => NULL,
  36. 'type' => 'err'
  37. ),
  38. 'request' => array(
  39. 'label' => NULL,
  40. 'string' => NULL,
  41. 'type' => NULL,
  42. 'sql' => NULL
  43. )
  44. );
  45. function xmlsql(&$con,$xml)
  46. {
  47. $this->xml = $xml;
  48. $this->con = $con;
  49. $this->_current_tag_cdata = '';
  50. }
  51. function replace($needle,$str)
  52. {
  53. $this->xml = str_replace($needle,$str,$this->xml);
  54. }
  55. function execute(&$checklist)
  56. {
  57. $this->_parse();
  58. $test = true;
  59. foreach ($this->job as $k => $v)
  60. {
  61. if ($test === NULL) { $test = true; }
  62. $ok = $err = '';
  63. $silent = false;
  64. # Si $test n'est pas faux et qu'on a un test SQL et une action non silencieuse
  65. if ($test !== false && $v['test']['sql'] != NULL
  66. && $v['test']['value'] != NULL && $v['request']['type'] != 'silent')
  67. {
  68. $req = $v['test']['sql'];
  69. $err = sprintf($v['test']['label'],$v['test']['string']);
  70. if (($rs = $this->con->select($req)) === false)
  71. {
  72. $test = false;
  73. $err = $this->con->error();
  74. }
  75. else
  76. {
  77. if ($v['test']['eq'] == 'neq') {
  78. $test = $rs->f(0) != $v['test']['value'];
  79. } else {
  80. $test = $rs->f(0) == $v['test']['value'];
  81. }
  82. if ($test == false && $v['test']['type'] == 'wrn') {
  83. $test = NULL;
  84. }
  85. }
  86. }
  87. # Si le test est passé, on tente la requête
  88. if ($test === true)
  89. {
  90. $ok = sprintf($v['request']['label'],$v['request']['string']);
  91. $req = $v['request']['sql'];
  92. if ($this->con->execute($req) === false) {
  93. $test = false;
  94. $err = sprintf($v['request']['label'],
  95. $v['request']['string']).' - '.
  96. $this->con->error();
  97. } else {
  98. $test = true;
  99. }
  100. if ($v['request']['type'] == 'silent') {
  101. $silent = true;
  102. $test = true;
  103. $ok = $err = '';
  104. }
  105. }
  106. elseif ($err == '')
  107. {
  108. $err = sprintf($v['request']['label'],$v['request']['string']);
  109. }
  110. if (!$silent) {
  111. $checklist->addItem($k,$test,$ok,$err);
  112. }
  113. }
  114. }
  115. function _parse()
  116. {
  117. $xp = xml_parser_create('ISO-8859-1');
  118. xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, false);
  119. xml_set_object($xp,$this);
  120. xml_set_element_handler($xp,'_openTag','_closeTag');
  121. xml_set_character_data_handler($xp, '_cdata');
  122. xml_parse($xp,$this->xml);
  123. xml_parser_free($xp);
  124. }
  125. function _openTag($p,$tag,$attr)
  126. {
  127. if ($tag == 'action' && !empty($attr['id']))
  128. {
  129. $id = $this->_action = $attr['id'];
  130. $this->job[$id] = $this->_subtable;
  131. if (!empty($attr['label'])) {
  132. $this->job[$id]['request']['label'] = __($attr['label']);
  133. }
  134. if (!empty($attr['string'])) {
  135. $this->job[$id]['request']['string'] = $attr['string'];
  136. }
  137. if (!empty($attr['type'])) {
  138. $this->job[$id]['request']['type'] = $attr['type'];
  139. }
  140. }
  141. elseif($tag == 'test')
  142. {
  143. $id = $this->_action;
  144. if (!empty($attr['eq'])) {
  145. $this->job[$id]['test']['eq'] = $attr['eq'];
  146. }
  147. if (!empty($attr['value'])) {
  148. $this->job[$id]['test']['value'] = $attr['value'];
  149. }
  150. if (!empty($attr['label'])) {
  151. $this->job[$id]['test']['label'] = __($attr['label']);
  152. }
  153. if (!empty($attr['string'])) {
  154. $this->job[$id]['test']['string'] = $attr['string'];
  155. }
  156. if (!empty($attr['type'])) {
  157. $this->job[$id]['test']['type'] = $attr['type'];
  158. }
  159. }
  160. }
  161. function _closeTag($p,$tag)
  162. {
  163. if ($tag == 'action') {
  164. $this->job[$this->_action]['request']['sql'] = trim($this->_current_tag_cdata);
  165. }
  166. elseif ($tag == 'test') {
  167. $this->job[$this->_action]['test']['sql'] = trim($this->_current_tag_cdata);
  168. }
  169. $this->_current_tag_cdata = '';
  170. }
  171. function _cdata($p,$cdata)
  172. {
  173. $this->_current_tag_cdata .= $cdata;
  174. }
  175. }
  176. ?>