public static function addById($intId, $intParentId = 0) { global $_CONF; $objDoc = Structure::getXmlDoc($intId); $arrUserIds = array(); $arrGroupIds = array(); $arrStorageIds = array(); $arrFeedIds = array(); $arrLanguageIds[0] = 0; $arrTemplateIds[0] = 0; $arrTemplateFieldIds[0] = 0; $arrLinkFieldIds = array(); $arrElementIds[0] = 0; $arrElementFieldIds = array(); $arrElementFieldIds["link"][0] = 0; $arrElementFieldIds["largeText"][0] = 0; $intTemplateParentId = 0; $intElementParentId = 0; //*** Get structure fields from selects. if (Structure::hasSelect($intId)) { $objSelects = Structure::getSelectsById($intId); foreach ($objSelects as $objSelect) { switch ($objSelect->getType()) { case "language": $intId = Request::get("frm_select_{$objSelect->getId()}"); $arrLanguageIds[$objSelect->getLogicId()] = $intId; break; case "element": $intId = Request::get("frm_select_{$objSelect->getId()}"); if ($objSelect->getLogicId() == "PARENT") { $intElementParentId = $intId; } else { $arrElementIds[$objSelect->getLogicId()] = $intId; } break; } } } foreach ($objDoc->childNodes as $rootNode) { if ($rootNode->nodeName == "structure") { //*** Valid structure XML. switch ($rootNode->getAttribute("type")) { case "template": $intTemplateParentId = $intParentId; break; case "element": $intElementParentId = $intParentId; break; } foreach ($rootNode->childNodes as $logicNode) { if ($logicNode->nodeName == "logic") { foreach ($logicNode->childNodes as $childNode) { switch ($childNode->nodeName) { case "languages": //*** Add languages to the account. foreach ($childNode->childNodes as $languageNode) { $objLanguage = new ContentLanguage(); $objLanguage->setAccountId($_CONF['app']['account']->getId()); $objLanguage->setName($languageNode->getAttribute("name")); $objLanguage->setAbbr($languageNode->getAttribute("abbr")); $objLanguage->default = $languageNode->getAttribute("default"); $objLanguage->setActive($languageNode->getAttribute("active")); $objLanguage->setSort($languageNode->getAttribute("sort")); $objLanguage->setUsername($languageNode->getAttribute("username")); $objLanguage->save(); $arrLanguageIds[$languageNode->getAttribute("id")] = $objLanguage->getId(); if ($languageNode->getAttribute("default") == 1) { $intDefaultLanguage = $objLanguage->getId(); } } break; case "templates": //*** Add templates to the account. ImpEx::importTemplates($childNode, $_CONF['app']['account']->getId(), $arrTemplateIds, $arrTemplateFieldIds, $arrLinkFieldIds, $intTemplateParentId); break; case "elements": //*** Add elements to the account. ImpEx::importElements($childNode, $_CONF['app']['account']->getId(), $arrTemplateIds, $arrTemplateFieldIds, $arrElementIds, $arrElementFieldIds, $arrLinkFieldIds, $arrLanguageIds, $arrUserIds, $arrGroupIds, $arrStorageIds, $arrFeedIds, $intElementParentId); break; case "aliases": //*** Add aliases to the account. foreach ($childNode->childNodes as $aliasNode) { $objAlias = new Alias(); $objAlias->setAccountId($_CONF['app']['account']->getId()); $objAlias->setAlias($aliasNode->getAttribute("alias")); if (array_key_exists($aliasNode->getAttribute("url"), $arrElementIds)) { $objAlias->setUrl($arrElementIds[$aliasNode->getAttribute("url")]); } else { $objAlias->setUrl(0); } if (array_key_exists($aliasNode->getAttribute("language"), $arrLanguageIds)) { $objAlias->setLanguageId($arrLanguageIds[$aliasNode->getAttribute("language")]); } else { $objAlias->setLanguageId(0); } $objAlias->setCascade($aliasNode->getAttribute("cascade")); $objAlias->setActive($aliasNode->getAttribute("active")); $objAlias->setSort($aliasNode->getAttribute("sort")); $objAlias->setCreated($aliasNode->getAttribute("created")); $objAlias->setModified($aliasNode->getAttribute("modified")); $objAlias->save(); } break; } //*** Adjust the links for deeplink fields. ImpEx::adjustDeeplinks($arrElementFieldIds["link"], $arrElementIds, $arrLanguageIds); //*** Adjust the links in large text fields. ImpEx::adjustTextlinks($arrElementFieldIds["largeText"], $arrElementIds, $arrLanguageIds, array(0)); } } } } } }
public static function import($strXml, $blnOverwrite = false, $blnKeepSettings = false) { global $objLiveAdmin, $intDefaultLanguage, $_CONF; $objReturn = NULL; $objSettings = NULL; $blnZip = false; //*** Init DOM object. $objDoc = new DOMDocument("1.0", "UTF-8"); $objDoc->formatOutput = false; $objDoc->preserveWhiteSpace = true; if (is_file($strXml)) { $objZip = new dUnzip2($strXml); if (is_object($objZip)) { //*** Zip file. $strXml = $objZip->unzip('data.xml'); if ($strXml !== false) { //*** Fix a unicode bug. Replace forbidden characters (The first 8). for ($intCount = 1; $intCount < 9; $intCount++) { $strHex = str_pad(dechex($intCount), 4, "0", STR_PAD_LEFT); $strXml = preg_replace('/\\x{' . $strHex . '}/u', "", $strXml); } $strXml = preg_replace('/\\x{001f}/u', "", $strXml); $objDoc->loadXML($strXml); $blnZip = true; } } else { //*** XML file. $objDoc->load($strXml); } } else { $objDoc->loadXML($strXml); } //*** Build data structure. foreach ($objDoc->childNodes as $rootNode) { if ($rootNode->nodeName == "Punch") { //*** Valid Punch XML. foreach ($rootNode->childNodes as $accountNode) { if ($accountNode->nodeName == "account") { //*** Account node. if ($blnOverwrite) { $objAccount = Account::getByPunchId($accountNode->getAttribute("punchId")); if (is_object($objAccount) && $blnKeepSettings) { //*** Save settings. $objSettings = Settings::getByAccount($objAccount->getId()); } //*** Remove account. if (is_object($objAccount)) { $objAccount->delete(); } } //*** Create account. $objAccount = new Account(); $objAccount->setPunchId($accountNode->getAttribute("punchId")); $objAccount->setName($accountNode->getAttribute("name")); $objAccount->setUri($accountNode->getAttribute("uri")); $objAccount->setTimeZoneId(42); $objAccount->save(); //*** Create temporary account object. $_CONF['app']['account'] = $objAccount; foreach ($accountNode->childNodes as $childNode) { $arrUserIds = array(); $arrGroupIds = array(); switch ($childNode->nodeName) { case "acl": self::importAcl($childNode, $objAccount->getId(), $arrUserIds, $arrGroupIds); break; case "products": //*** Add products to the account. foreach ($childNode->childNodes as $productNode) { switch ($productNode->nodeName) { case "pcms": //*** Add PunchCMS product to the account. $objAccountProduct = new AccountProduct(); $objAccountProduct->setAccountId($objAccount->getId()); $objAccountProduct->setProductId(PRODUCT_PCMS); $objAccountProduct->setExpires($productNode->getAttribute("expires")); $objAccountProduct->save(); $arrStorageIds[0] = 0; $arrFeedIds[0] = 0; //*** Add PunchCMS data to the account. foreach ($productNode->childNodes as $pcmsNode) { switch ($pcmsNode->nodeName) { case "settings": //*** Add settings to the account. if ($blnKeepSettings && is_object($objSettings)) { foreach ($objSettings as $objSetting) { $objSetting->setId(0); $objSetting->setAccountId($objAccount->getId()); $objSetting->save(); } } else { foreach ($pcmsNode->childNodes as $settingNode) { $objSettingTemplate = SettingTemplate::selectByName($settingNode->getAttribute("name")); if (is_object($objSettingTemplate)) { $objSetting = new Setting(); $objSetting->setAccountId($objAccount->getId()); $objSetting->setSettingId($objSettingTemplate->getId()); $objSetting->setValue($settingNode->getAttribute("value")); $objSetting->save(); } } } break; case "languages": //*** Add languages to the account. $arrLanguageIds[0] = 0; foreach ($pcmsNode->childNodes as $languageNode) { $objLanguage = new ContentLanguage(); $objLanguage->setAccountId($objAccount->getId()); $objLanguage->setName($languageNode->getAttribute("name")); $objLanguage->setAbbr($languageNode->getAttribute("abbr")); $objLanguage->default = $languageNode->getAttribute("default"); $objLanguage->setActive($languageNode->getAttribute("active")); $objLanguage->setSort($languageNode->getAttribute("sort")); $objLanguage->setUsername($languageNode->getAttribute("username")); $objLanguage->save(); $arrLanguageIds[$languageNode->getAttribute("id")] = $objLanguage->getId(); if ($languageNode->getAttribute("default") == 1) { $intDefaultLanguage = $objLanguage->getId(); } } break; case "feeds": //*** Add feeds to the account. $arrFeedIds[0] = 0; foreach ($pcmsNode->childNodes as $feedNode) { $objFeed = new Feed(); $objFeed->setAccountId($objAccount->getId()); $objFeed->setName($feedNode->getAttribute("name")); $objFeed->setFeed($feedNode->getAttribute("feed")); $objFeed->setBasePath($feedNode->getAttribute("basepath")); $objFeed->setRefresh($feedNode->getAttribute("refresh")); $objFeed->setLastUpdate($feedNode->getAttribute("lastUpdate")); $objFeed->setActive($feedNode->getAttribute("active")); $objFeed->setSort($feedNode->getAttribute("sort")); $objFeed->save(); $arrFeedIds[$feedNode->getAttribute("id")] = $objFeed->getId(); } break; case "storage": //*** Add media items to the account. self::importStorage($pcmsNode, $objAccount->getId(), $arrStorageIds); break; case "templates": //*** Add templates to the account. $arrTemplateIds[0] = 0; $arrTemplateFieldIds[0] = 0; $arrLinkFieldIds = array(); self::importTemplates($pcmsNode, $objAccount->getId(), $arrTemplateIds, $arrTemplateFieldIds, $arrLinkFieldIds); break; case "elements": //*** Add elements to the account. $arrElementIds[0] = 0; $arrElementFieldIds["link"][0] = 0; $arrElementFieldIds["largeText"][0] = 0; self::importElements($pcmsNode, $objAccount->getId(), $arrTemplateIds, $arrTemplateFieldIds, $arrElementIds, $arrElementFieldIds, $arrLinkFieldIds, $arrLanguageIds, $arrUserIds, $arrGroupIds, $arrStorageIds, $arrFeedIds); break; case "aliases": //*** Add aliases to the account. foreach ($pcmsNode->childNodes as $aliasNode) { $objAlias = new Alias(); $objAlias->setAccountId($objAccount->getId()); $objAlias->setAlias($aliasNode->getAttribute("alias")); if (array_key_exists($aliasNode->getAttribute("url"), $arrElementIds)) { $objAlias->setUrl($arrElementIds[$aliasNode->getAttribute("url")]); } else { $objAlias->setUrl(0); } if (array_key_exists($aliasNode->getAttribute("language"), $arrLanguageIds)) { $objAlias->setLanguageId($arrLanguageIds[$aliasNode->getAttribute("language")]); } else { $objAlias->setLanguageId(0); } $objAlias->setCascade($aliasNode->getAttribute("cascade")); $objAlias->setActive($aliasNode->getAttribute("active")); $objAlias->setSort($aliasNode->getAttribute("sort")); $objAlias->setCreated($aliasNode->getAttribute("created")); $objAlias->setModified($aliasNode->getAttribute("modified")); $objAlias->save(); } break; } } //*** Adjust the links for deeplink fields. self::adjustDeeplinks($arrElementFieldIds["link"], $arrElementIds, $arrLanguageIds); //*** Adjust the links in large text fields. self::adjustTextlinks($arrElementFieldIds["largeText"], $arrElementIds, $arrLanguageIds, $arrStorageIds); break; } } break; } } //*** Destroy temporary account object. unset($_CONF['app']['account']); $objReturn = $objAccount; } } } } //*** Files. if ($blnZip && is_object($objReturn)) { self::importFiles($objZip, $objReturn); if ($blnKeepSettings) { //*** Move files to remote server. self::moveImportedFiles($objReturn); } } return $objReturn; }