3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 The Nucleus Group
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * (see nucleus/documentation/index.html#license for more info)
13 * This class contains two classes that can be used for importing and
14 * exporting Nucleus skins: SKINIMPORT and SKINEXPORT
16 * @license http://nucleuscms.org/license.txt GNU General Public License
17 * @copyright Copyright (C) 2002-2009 The Nucleus Group
18 * @version $Id: skinie.php 1883 2012-06-17 07:55:47Z sakamocchi $
23 // hardcoded value (see constructor). When 1, interesting info about the
24 // parsing process is sent to the output
27 // parser/file pointer
31 // parset internal charset, US-ASCII/ISO-8859-1/UTF-8
32 private $parse_charset = 'UTF-8';
34 // which data has been read?
35 private $metaDataRead;
43 // to maintain track of where we are inside the XML file
50 private $currentPartName;
54 * constructor initializes data structures
56 public function __construct()
58 // disable magic_quotes_runtime if it's turned on
59 //set_magic_quotes_runtime(0);
60 if ( version_compare(PHP_VERSION, '5.3.0', '<') )
62 ini_set('magic_quotes_runtime', '0');
72 public function __destruct()
77 public function reset()
81 xml_parser_free($this->parser);
87 // which data has been read?
88 $this->metaDataRead = 0;
91 // to maintain track of where we are inside the XML file
96 $this->inTemplate = 0;
97 $this->currentName = '';
98 $this->currentPartName = '';
100 // character data pile
103 // list of skinnames and templatenames (will be array of array)
104 $this->skins = array();
105 $this->templates = array();
107 // extra info included in the XML files (e.g. installation notes)
110 // init XML parser, this parser deal with characters as encoded by UTF-8
111 $this->parser = xml_parser_create($this->parse_charset);
112 xml_set_object($this->parser, $this);
113 xml_set_element_handler($this->parser, 'start_element', 'end_element');
114 xml_set_character_data_handler($this->parser, 'character_data');
115 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
121 * Reads an XML file into memory
126 * Set to 1 when only the metadata needs to be read (optional, default 0)
128 public function readFile($filename, $metaOnly = 0)
131 $this->fp = @fopen($filename, 'r');
134 return _SKINIE_ERROR_FAILEDOPEN_FILEURL;
142 while ( !feof($this->fp) )
144 $tempbuffer .= fread($this->fp, 4096);
149 * NOTE: conver character set.
150 * We hope all characters in the file also includes UTF-8 coded character set,
151 * because this PHP extension implements support for James Clark's expat in PHP
152 * and it supports juust US-ASCII, ISO-8859-1, UTF-8 character coding scheme.
154 if ( i18n::get_current_charset() != $this->parse_charset )
156 $tempbuffer = i18n::convert($tempbuffer, i18n::get_current_charset(), $this->parse_charset);
160 fwrite($temp, $tempbuffer);
163 while ( ($buffer = fread($temp, 4096) )
164 && (!$metaOnly || ($metaOnly && !$this->metaDataRead)) )
166 $err = xml_parse( $this->parser, $buffer, feof($temp) );
167 if ( !$err && $this->debug )
169 echo 'ERROR: ', xml_error_string(xml_get_error_code($this->parser)), '<br />';
181 * Returns the list of skin names
183 public function getSkinNames()
185 return array_keys($this->skins);
189 * Returns the list of template names
191 public function getTemplateNames()
193 return array_keys($this->templates);
197 * Returns the extra information included in the XML file
199 public function getInfo()
205 * Writes the skins and templates to the database
207 * @param $allowOverwrite
208 * set to 1 when allowed to overwrite existing skins with the same name
211 public function writeToDatabase($allowOverwrite = 0)
215 $existingSkins = $this->checkSkinNameClashes();
216 $existingTemplates = $this->checkTemplateNameClashes();
217 $invalidSkinNames = $this->checkSkinNamesValid();
218 $invalidTemplateNames = $this->checkTemplateNamesValid();
220 // if there are invalid skin or template names, stop executioin and return and error
221 if ( (sizeof($invalidSkinNames) > 0) || (sizeof($invalidTemplateNames) > 0) )
223 $inames_error = "<p>"._SKINIE_INVALID_NAMES_DETECTED."</p>\n";
224 $inames_error .= "<ul>";
225 foreach( $invalidSkinNames as $sName )
227 $inames_error .= "<li>".Entity::hsc($sName)."</li>";
229 foreach( $invalidTemplateNames as $sName )
231 $inames_error .= "<li>".Entity::hsc($sName)."</li>";
233 $inames_error .= "</ul>";
234 return $inames_error;
237 // if not allowed to overwrite, check if any nameclashes exists
238 if ( !$allowOverwrite )
240 if ( (sizeof($existingSkins) > 0) || (sizeof($existingTemplates) > 0) )
242 return _SKINIE_NAME_CLASHES_DETECTED;
246 foreach ( $this->skins as $skinName => $data )
248 // 1. if exists: delete all part data, update desc data
249 // if not exists: create desc
250 if ( in_array($skinName, $existingSkins) )
252 $skinObj = SKIN::createFromName($skinName);
254 // delete all parts of the skin
255 $skinObj->deleteAllParts();
257 // update general info
258 $skinObj->updateGeneralInfo(
260 $data['description'],
262 $data['includeMode'],
263 $data['includePrefix']
268 $skinid = SKIN::createNew(
270 $data['description'],
272 $data['includeMode'],
273 $data['includePrefix']
275 $skinObj =& $manager->getSkin($skinid);
279 foreach ( $data['parts'] as $partName => $partContent )
281 $skinObj->update($partName, $partContent);
285 foreach ( $this->templates as $templateName => $data )
287 // 1. if exists: delete all part data, update desc data
288 // if not exists: create desc
289 if ( in_array($templateName, $existingTemplates) )
291 $templateObj = Template::createFromName($templateName);
293 // delete all parts of the template
294 $templateObj->deleteAllParts();
296 // update general info
297 $templateObj->updateGeneralInfo($templateName, $data['description']);
301 $templateid = Template::createNew($templateName, $data['description']);
302 $templateObj = new Template($templateid);
306 foreach ( $data['parts'] as $partName => $partContent )
308 $templateObj->update($partName, $partContent);
315 * returns an array of all the skin nameclashes (empty array when no name clashes)
317 public function checkSkinNameClashes()
321 foreach ( $this->skins as $skinName => $data )
323 if ( SKIN::exists($skinName) )
325 array_push($clashes, $skinName);
332 * returns an array of all the template nameclashes
333 * (empty array when no name clashes)
335 public function checkTemplateNameClashes()
339 foreach ( $this->templates as $templateName => $data )
341 if ( Template::exists($templateName) )
343 array_push($clashes, $templateName);
350 * returns an array of all the invalid skin names (empty array when no invalid names )
352 private function checkSkinNamesValid()
356 foreach ( $this->skins as $skinName => $data )
358 if ( !isValidSkinName($skinName) )
360 array_push($notValid, $skinName);
367 * returns an array of all the invalid template names (empty array when no invalid names )
369 private function checkTemplateNamesValid()
373 foreach ( $this->templates as $templateName => $data )
375 if ( !isValidTemplateName($templateName) )
377 array_push($notValid, $templateName);
384 * Called by XML parser for each new start element encountered
386 private function start_element($parser, $name, $attrs)
388 foreach( $attrs as $key=>$value )
390 if ( $this->parse_charset != i18n::get_current_charset() )
392 $name = i18n::convert($name, $this->parse_charset, i18n::get_current_charset());
393 $value = i18n::convert($value, $this->parse_charset, i18n::get_current_charset());
396 $attrs[$key] = $value;
401 echo 'START: ', Entity::hsc($name), '<br />';
416 if ( !$this->inMeta )
419 $this->currentName = $attrs['name'];
420 $this->skins[$this->currentName]['type'] = $attrs['type'];
421 $this->skins[$this->currentName]['includeMode'] = $attrs['includeMode'];
422 $this->skins[$this->currentName]['includePrefix'] = $attrs['includePrefix'];
423 $this->skins[$this->currentName]['parts'] = array();
427 $this->skins[$attrs['name']] = array();
428 $this->skins[$attrs['name']]['parts'] = array();
432 if ( !$this->inMeta )
434 $this->inTemplate = 1;
435 $this->currentName = $attrs['name'];
436 $this->templates[$this->currentName]['parts'] = array();
440 $this->templates[$attrs['name']] = array();
441 $this->templates[$attrs['name']]['parts'] = array();
448 $this->currentPartName = $attrs['name'];
451 echo _SKINIE_SEELEMENT_UNEXPECTEDTAG . Entity::hsc($name) . '<br />';
454 // character data never contains other tags
455 $this->clear_character_data();
460 * Called by the XML parser for each closing tag encountered
462 private function end_element($parser, $name)
466 echo 'END: ' . Entity::hsc($name) . '<br />';
469 if ( $this->parse_charset != i18n::get_current_charset() )
471 $name = i18n::convert($name, $this->parse_charset, i18n::get_current_charset());
472 $charset_data = i18n::convert($this->get_character_data(), $this->parse_charset, i18n::get_current_charset());
476 $charset_data = $this->get_character_data();
487 $this->metaDataRead = 1;
490 $this->info = $charset_data;
492 if ( !$this->inMeta )
498 if ( !$this->inMeta )
500 $this->inTemplate = 0;
506 $this->skins[$this->currentName]['description'] = $charset_data;
510 $this->templates[$this->currentName]['description'] = $charset_data;
516 $this->skins[$this->currentName]['parts'][$this->currentPartName] = $charset_data;
520 $this->templates[$this->currentName]['parts'][$this->currentPartName] = $charset_data;
524 echo _SKINIE_SEELEMENT_UNEXPECTEDTAG . Entity::hsc($name) . '<br />';
527 $this->clear_character_data();
532 * Called by XML parser for data inside elements
534 private function character_data ($parser, $data)
538 echo 'NEW DATA: ' . Entity::hsc($data) . '<br />';
540 $this->cdata .= $data;
545 * Returns the data collected so far
547 private function get_character_data()
553 * Clears the data buffer
555 private function clear_character_data()
562 * Static method that looks for importable XML files in subdirs of the given dir
564 static public function searchForCandidates($dir)
566 $candidates = array();
568 $dirhandle = opendir($dir);
569 while ( $filename = readdir($dirhandle) )
571 if ( @is_dir($dir . $filename) && ($filename != '.') && ($filename != '..') )
573 $xml_file = $dir . $filename . '/skinbackup.xml';
574 if ( file_exists($xml_file) && is_readable($xml_file) )
577 $candidates[$filename] = $filename;
580 // backwards compatibility
581 $xml_file = $dir . $filename . '/skindata.xml';
582 if ( file_exists($xml_file) && is_readable($xml_file) )
585 $candidates[$filename] = $filename;
589 closedir($dirhandle);
601 * Constructor initializes data structures
603 public function __construct()
605 // list of templateIDs to export
606 $this->templates = array();
608 // list of skinIDs to export
609 $this->skins = array();
611 // extra info to be in XML file
616 * Adds a template to be exported
620 * @result false when no such ID exists
622 public function addTemplate($id)
624 if ( !Template::existsID($id) )
629 $this->templates[$id] = Template::getNameFromId($id);
634 * Adds a skin to be exported
638 * @result false when no such ID exists
640 public function addSkin($id)
642 if ( !SKIN::existsID($id) )
647 $this->skins[$id] = SKIN::getNameFromId($id);
652 * Sets the extra info to be included in the exported file
654 public function setInfo($info)
660 * Outputs the XML contents of the export file
663 * set to 0 if you don't want to send out headers
664 * (optional, default 1)
666 public function export($setHeaders = 1)
672 // make sure the mimetype is correct, and that the data does not show up
673 // in the browser, but gets saved into and XML file (popup download window)
674 header('Content-Type: text/xml; charset=' . i18n::get_current_charset());
675 header('Content-Disposition: attachment; filename="skinbackup.xml"');
676 header('Expires: 0');
677 header('Pragma: no-cache');
680 echo "<nucleusskin>\n";
685 foreach ( $this->skins as $skinId => $skinName )
687 echo "\t\t" . '<skin name="' . Entity::hsc($skinName) . '" />' . "\n";
690 foreach ( $this->templates as $templateId => $templateName )
692 echo "\t\t" . '<template name="' . Entity::hsc($templateName) . '" />' . "\n";
697 echo "\t\t<info><![CDATA[" . $this->info . "]]></info>\n";
699 echo "\t</meta>\n\n\n";
702 foreach ($this->skins as $skinId => $skinName)
704 $skinId = (integer) $skinId;
705 $skinObj =& $manager->getSkin($skinId);
707 echo "\t" . '<skin name="' . Entity::hsc($skinName) . '" type="' . Entity::hsc($skinObj->getContentType()) . '" includeMode="' . Entity::hsc($skinObj->getIncludeMode()) . '" includePrefix="' . Entity::hsc($skinObj->getIncludePrefix()) . '">' . "\n";
708 echo "\t\t<description>" . Entity::hsc($skinObj->getDescription()) . "</description>\n";
710 $res = DB::getResult('SELECT stype, scontent FROM '. sql_table('skin') .' WHERE sdesc=' . $skinId);
711 foreach ( $res as $row )
713 echo "\t\t" . '<part name="',Entity::hsc($row['stype']) . '">';
714 echo '<![CDATA[' . $this->escapeCDATA($row['scontent']) . ']]>';
717 echo "\t</skin>\n\n\n";
720 // contents templates
721 foreach ( $this->templates as $templateId => $templateName )
723 $templateId = intval($templateId);
725 echo "\t" . '<template name="' . Entity::hsc($templateName) . '">' . "\n";
726 echo "\t\t<description>" . Entity::hsc(Template::getDesc($templateId)) . "</description>\n";
728 $res = DB::getResult('SELECT tpartname, tcontent FROM '. sql_table('template') .' WHERE tdesc=' . $templateId);
729 foreach ( $res as $row )
731 echo "\t\t" . '<part name="' . Entity::hsc($row['tpartname']) . '">';
732 echo '<![CDATA[' . $this->escapeCDATA($row['tcontent']) . ']]>';
736 echo "\t</template>\n\n\n";
738 echo '</nucleusskin>';
742 * Escapes CDATA content so it can be included in another CDATA section
744 private function escapeCDATA($cdata)
746 return preg_replace('/]]>/', ']]]]><![CDATA[>', $cdata);