protected function loadMDTemplate($doc, $transform = false, $erase = false) { //doc is either a file name or a DOMDocument if ($transform) { //transform if (is_string($doc)) { $file = $doc; $doc = new DOMDocument(); if (!($contents = file_get_contents($file))) { $this->userMessage("Could not load source file"); return false; } if (!$doc->loadXML($contents)) { $this->userMessage("Could not load file source contents"); return false; } } if (!$doc instanceof DOMDocument) { $this->userMessage("Could not load xml into document"); return false; } $proc = new XSLTProcessor(); $xslt_doc = new DOMDocument(); if (!$xslt_doc->loadXML($transform)) { $this->userMessage("Could not load transform: " . $_FILES['xsl']['name']); return false; } if (!$proc->importStylesheet($xslt_doc)) { $this->userMessage("Could not import style sheet"); return false; } $trans_doc = new DOMDocument('1.0', 'UTF-8'); $trans_doc->appendChild($trans_doc->importNode($doc->documentElement, true)); if (($trans_out = $proc->transformToXML($trans_doc)) === false) { $this->userMessage("Could not transform accoring to xsl"); return false; } } else { $trans_doc = $doc; } if ($trans_doc instanceof DOMDocument) { $temp_file = tempnam(sys_get_temp_dir(), 'MDN_UPLOAD'); if (!file_put_contents($temp_file, $trans_out)) { $this->userMessage("Could not save transformed files"); return false; } } else { $temp_file = $trans_doc; } $template = new I2CE_MagicDataTemplate(); $template->setVerboseErrors(true); if (!$template->loadRootFile($temp_file)) { I2CE::raiseError("Unable to load transformed file as Magic Data"); $this->userMessage("Unable to load transformed file as Magic Data"); return false; } if (!$template->validate()) { I2CE::raiseError("Unable to validate transformed file as Magic Data"); $this->userMessage("Unable to validate transformed file as Magic Data"); return false; } $store = new I2CE_MagicDataStorageMem(); $mem_config = I2CE_MagicData::instance("mdn_load"); $mem_config->addStorage($store); $nodeList = $template->query("/configurationGroup"); if (!$nodeList instanceof DOMNodeList || $nodeList->length == 0) { $nodeList = $template->query("/I2CEConfiguration/configurationGroup"); //perhaps we really need to do something more if this is a module } foreach ($nodeList as $configNode) { $locale = false; $status = $template->getDefaultStatus(); if ($configNode->hasAttribute('locale')) { $locale = $configNode->getAttribute('locale'); } $vers = '0'; if ($template->setConfigValues($configNode, $mem_config, $status, $vers) === false) { I2CE::raiseError("Could not load configuration values"); $this->userMessage("Could not load configuration values"); return false; } } //I2CE::raiseError(print_r($mem_config->getAsArray(),true)); if ($erase) { $this->config->eraseChildren(); } $merges = $template->getMerges(); foreach ($merges as $path => $merge) { if ($this->config->is_scalar($path)) { I2CE::raiseError("Trying to merge arrays into {$path} where target is scalar valued. Skipping"); continue; } if ($mem_config->is_scalar($path)) { I2CE::raiseError("Trying to merge arrays into {$path} where source is scalar valued. Skipping"); continue; } $old_arr = $this->config->getAsArray($path); $new_arr = $mem_config->getAsArray($path); $mem_config->__unset($path); if (!is_array($old_arr)) { //in case the target did not exist $old_arr = array(); } if (!is_array($new_arr)) { //in case no values were set for the source $new_arr = array(); } switch ($merge) { case 'uniquemerge': $new_arr = I2CE_Util::array_unique(array_merge($old_arr, $new_arr)); break; case 'merge': $new_arr = array_merge($old_arr, $new_arr); break; case 'mergerecursive': I2CE_Util::merge_recursive($old_arr, $new_arr); $new_arr = $old_arr; break; } $this->config->__unset($path); $this->config->{$path} = $new_arr; } //we took care of all array merges. anything that is left is an overwrite. foreach ($mem_config as $k => $v) { if (is_scalar($v) && $mem_config->is_translatable($k) && !$this->config->is_parent($k)) { $this->config->setTranslatable($k); $translations = $mem_config->traverse($k, true, false)->getTranslations(); foreach ($translations as $locale => $trans) { if (strlen($trans) == 0) { continue; } $this->config->setTranslation($locale, $trans, $k); } } else { $this->config->{$k}->setValue($v, null, false); } if ($this->config->{$k} instanceof I2CE_MagicDataNode) { //free up some memory. $this->config->{$k}->unpopulate(true); } } return true; }
/** * Load a config file with various extensions. * * @param string $file Path to the config file * @param boolean $verbose_errors (true) * * @returns booolean true on success */ protected function loadConfigFile($file, $verbose_errors = true) { $template = new I2CE_MagicDataTemplate(); $template->setVerboseErrors($verbose_errors); if ($template->loadRootFile($file)) { $this->template = $template; } else { if ($verbose_errors) { I2CE::raiseError("Unable to load config file {$file}"); } return false; } return true; }