00001 <?php
00007 class GenieGate_XMLImport {
00011 var $DUMMY_EMAIL = "@example.com";
00012 var $DBH;
00013 var $user;
00014 var $TEXT;
00015 var $parser;
00016 var $UA;
00017 var $GM;
00018 var $PM;
00019 var $LOG = array();
00020 var $ERRORS = array();
00021 var $USER_COUNT = 0;
00022
00030 function GenieGate_XMLImport(&$dbh,&$ua,&$gm,&$pm){
00031 $this->DBH = $dbh;
00032 $this->UA = $ua;
00033 $this->GM = $gm;
00034 $this->PM = $pm;
00035 $this->parser = xml_parser_create();
00036 xml_set_object($this->parser,$this);
00037 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, FALSE);
00038 xml_set_element_handler($this->parser,"_start_element","_end_element");
00039 xml_set_character_data_handler($this->parser,"_xml_chars");
00040 }
00049 function more($data){
00050 if(! xml_parse($this->parser,$data,FALSE)){
00051 die(sprintf("XML Parser: %s %d",
00052 xml_error_string(xml_get_error_code($this->parser)),
00053 xml_get_current_line_number($this->parser)));
00054 }
00055 }
00060 function finish(){
00061 if(! xml_parse($this->parser,"",TRUE)){
00062 die(sprintf("XML Parser: %s %d",
00063 xml_error_string(xml_get_error_code($this->parser)),
00064 xml_get_current_line_number($this->parser)));
00065 }
00066 xml_parser_free($this->parser);
00067 }
00074 function error($message = FALSE){
00075 if($message){
00076 array_push($this->ERRORS,$message);
00077 }
00078 return($this->ERRORS);
00079 }
00085 function logit($message = FALSE){
00086 if($message){
00087 array_push($this->LOG,$message);
00088 }
00089 return($this->LOG);
00090 }
00094 function user_count(){
00095 return($this->USER_COUNT);
00096 }
00097
00098 function _start_element($parser,$tag,$attr){
00099 unset($this->TEXT);
00100
00101 if($tag == "Users"){
00102 xml_set_element_handler($parser,"_user_start","_user_end");
00103 return;
00104 }
00105 if($tag == "GroupDefinition"){
00106 $this->_defineGroup($attr);
00107 return;
00108 }
00109 if($tag == "PropertyDefinitions"){
00110 xml_set_element_handler($parser,"_prop_definition_start","_prop_definition_end");
00111 return;
00112 }
00113 }
00114
00115 function _defineGroup($attr){
00116 if($attr[gid] == "admin"){
00117 if($attr[signup] == "Y"){
00118 $this->error("Attribute [signup] should always be [N] for group admin (setting N)");
00119 }
00120 $attr[signup] = "N";
00121 }
00122 if($attr[signup] != "Y"){
00123 $attr[signup] = "N";
00124 }
00125 $this->GM->setGroup($attr[gid],$attr[name],$attr[signup]);
00126 $this->logit("Created/Updated group: $attr[gid] ($attr[name])");
00127 }
00128
00129 function _xml_chars($parser,$data){ $this->TEXT .= $data; }
00130
00131 function _end_element($parser,$tag){ }
00132
00133
00134 function _prop_definition_start($parser,$tag,$attr){
00135 if($tag == "PropertySection"){
00136 $this->skey = $attr[id];
00137 if(! $attr[id]){
00138 $this->skey = FALSE;
00139 $this->error("Property section missing id=\"section.id\"");
00140 }else{
00141 $this->PM->updateSection($attr[id],$attr[title]);
00142 $this->current_props = $this->PM->getSectionProperties($attr[id]);
00143 $this->logit("Found property section: $attr[id] Textual name: $attr[title]");
00144 }
00145 return;
00146 }
00147 if($tag == "PropertyDefinition"){
00148 $prop = $attr[property];
00149 if(! $this->skey){
00150 $this->error("Missing valid PropertySection for ($prop) skipping ");
00151 return;
00152 }
00153 if(! $prop){
00154 $this->error("PropertyDefinition [$this->skey] missing property=\"id\", skipping");
00155 return;
00156 }
00157 $title = $attr[title];
00158 if($this->current_props[$prop]){
00159 if($this->current_props[$prop] != $title){
00160 $this->PM->changePropertyLabel($this->skey,$prop,$title);
00161 $this->logit("Altering property label; section: [$this->skey] Property: [$prop] ($title)");
00162 }
00163 }else{
00164 $this->logit("Define new property; section: [$this->skey] Property: [$prop] ($title)");
00165 $this->PM->createProperty($this->skey,$prop,$title);
00166 }
00167 }
00168 }
00169 function _prop_definition_end($parser,$tag){
00170 if($tag == "PropertyDefinitions"){
00171 xml_set_element_handler($parser,"_start_element","_end_element");
00172 return;
00173 }
00174 if($tag == "PropertySection"){
00175 unset($this->current_props);
00176 unset($this->skey);
00177 }
00178 }
00179
00180
00181 function _user_start($parser,$tag,$attr){
00182 unset($this->TEXT);
00183 if($tag == "User"){
00184 if(! $attr[email]){
00185 $attr[email] = $attr[uid] . "@example.com";
00186 $this->error("User: \"$attr[uid]\" has no email address, using \"$attr[email]\"");
00187 }
00188 if(! $attr[name]){
00189 $attr[name] = $attr[uid];
00190 $this->logit("User \"$attr[uid]\" has no name using \"$attr[name]\"");
00191 }
00192 $this->user = array(FIELDS => $attr, GROUPS => array(), PROP => array());
00193 $u = $this->UA->lookupUid($attr[uid]);
00194 if($u){
00195 $this->UA->updateUserAccount($attr[uid],$attr);
00196 }else{
00197 if($attr[confirm] != 'Y'){
00198 $this->error("User: \"$attr[uid]\" missing confirm=\"Y\" (Not creating)");
00199 }else{
00200 $err = $this->UA->createConfirmedUserAccount($attr);
00201 if(count($err)){
00202 foreach($err as $msg){
00203 $this->error("User: \"$attr[uid]\" " . $msg);
00204 }
00205 $this->current_uid = FALSE;
00206 }else{
00207 $this->logit("Create User: $attr[uid]");
00208 }
00209 }
00210 }
00211 $this->user[GROUPS] = array();
00212 $this->user[PROP] = array();
00213 $this->current_uid = $attr[uid];
00214 return;
00215 }
00216
00217 if($tag == "Group"){
00218 if($this->current_uid){
00219 array_push($this->user[GROUPS],$attr[gid]);
00220 }
00221 }
00222 if($tag == "UserProperties"){
00223 if($this->current_uid){
00224 $this->skey = $attr[section];
00225 }
00226 return;
00227 }
00228 if($tag == "UserProperty"){
00229 $this->TEXT = "";
00230 if($this->current_uid){
00231 $prop = $attr[property];
00232 $this->user[PROP][$prop] = &$this->TEXT;
00233 }
00234 }
00235 }
00236 function _user_end($parser,$tag){
00237 if($tag == "UserProperty"){
00238 unset($this->TEXT);
00239 }
00240 if($tag == "Groups"){
00241 if($this->current_uid){
00242 if($this->current_uid == "root"){
00243 if(! in_array("admin",$this->user[GROUPS])){
00244 array_push($this->user[GROUPS],"admin");
00245 }
00246 }
00247 $this->GM->setMember($this->current_user,$this->user[GROUPS]);
00248 }
00249 }
00250 if($tag == "UserProperties"){
00251 if($this->current_uid){
00252 $this->_set_properties($this->current_uid,$this->skey,$this->user[PROP]);
00253 unset($this->skey);
00254 $this->user[PROP] = array();
00255 }
00256 }
00257 if($tag == "User"){
00258 if($this->current_uid){
00259 ++$this->USER_COUNT;
00260 }
00261 unset($this->user);
00262 unset($this->current_uid);
00263 }
00264 if($tag == "Users"){
00265 xml_set_element_handler($parser,"_start_element","_end_element");
00266 }
00267 }
00268 function _set_properties($uid,$skey,$props){
00269 $section = $this->PM->getSection($skey,$uid);
00270 foreach($props as $p => $v){
00271 $v = ltrim(rtrim($v));
00272 $section->setProperty($p,$v);
00273 }
00274 }
00275
00276 }
00277 ?>