PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/service/design/srv/PwDesignExportZip.php

https://github.com/cuijinquan/nextwind
PHP | 228 lines | 145 code | 15 blank | 68 comment | 24 complexity | 125b0034af23377eea7df6556c353a91 MD5 | raw file
  1. <?php
  2. /**
  3. * the last known user to change this file in the repository <$LastChangedBy: gao.wanggao $>
  4. * @author $Author: gao.wanggao $ Foxsee@aliyun.com
  5. * @copyright ?2003-2103 phpwind.com
  6. * @license http://www.phpwind.com
  7. * @version $Id: PwDesignExportZip.php 24989 2013-02-28 02:53:30Z gao.wanggao $
  8. * @package
  9. */
  10. class PwDesignExportZip {
  11. protected $dir = '';
  12. protected $pageid = 0;
  13. protected $folder = '';
  14. private $_tplExt = '.htm';
  15. private $_files = array();
  16. private $_moduleConf = array();
  17. public function __construct($pageBo) {
  18. $this->pageid = $pageBo->pageid;
  19. $pageInfo = $pageBo->getPage();
  20. $this->folder = str_replace('/', '_', $pageInfo['page_router']);
  21. $this->dir = Wind::getRealDir('THEMES:portal.local.'). $pageBo->getTplPath() . '/';
  22. }
  23. public function zip($charset = 'utf-8') {
  24. Wind::import('LIB:utility.PwZip');
  25. $zip = new PwZip();
  26. $files = $this->read($this->dir);
  27. $fromCharset = Wekit::app()->charset;
  28. foreach ($files AS &$v) {
  29. $v['filename'] = str_replace($this->dir, '', $v['filename']);
  30. $ext = strrchr($v['filename'], ".");
  31. if($ext != $this->_tplExt) continue;
  32. //$v['data'] = $this->decompilePw($v['data']);
  33. $v['data'] = $this->decompileTitle($v['data']);
  34. //$v['data'] = $this->decompileList($v['data']);
  35. $v['data'] = $this->decompileTpl($v['data']);
  36. $v['data'] = $this->decompileStyle($v['data']);
  37. }
  38. foreach ($files AS $file) {
  39. if ($file['filename'] == 'module/data.txt') continue;
  40. if (strtolower($file['filename']) == 'manifest.xml') {
  41. Wind::import("WIND:parser.WindXmlParser");
  42. $xml = new WindXmlParser('1.0', $fromCharset);
  43. $config = $xml->parseXmlStream($file['data'], 0);
  44. unset($config['module']);
  45. $file['data'] = $this->xmlFormat($config, $charset);
  46. } else {
  47. $ext = strtolower(substr(strrchr($file['filename'], '.'), 1));
  48. if (in_array($ext, array('css', 'js', 'htm'))) {
  49. $file['data'] = WindConvert::convert($file['data'], $charset, $fromCharset);
  50. }
  51. }
  52. $file['filename'] = $this->folder.'/'.$file['filename'];
  53. if (!$zip->addFile($file['data'], $file['filename'])) return new PwError("DESIGN:zlib.error");
  54. }
  55. $txt = $this->doTxt($charset);
  56. $txtfile = $this->folder.'/module/data.txt';
  57. $zip->addFile($txt['content'], $txtfile);
  58. return $zip->getCompressedFile();
  59. }
  60. protected function decompileTitle($section) {
  61. if (preg_match_all('/\<pw-title\s*id=\"(\w+)\"\s*>/isU',$section, $matches)) {
  62. foreach ($matches[1] AS $k=>$v) {
  63. if (!$v) continue;
  64. $section = str_replace($matches[0][$k], '<pw-title>', $section);
  65. }
  66. }
  67. return $section;
  68. }
  69. /*protected function decompileList($section) {
  70. Wind::import("SRV:design.bo.PwDesignModuleBo");
  71. if (preg_match_all('/\<pw-list\s*id=\"(\d+)\"\s*>/isU',$section, $matches)) {
  72. foreach ($matches[1] AS $k=>$v) {
  73. if (!$v) continue;
  74. $section = str_replace($matches[0][$k], '<pw-list role="'.$v.'">', $section);
  75. }
  76. }
  77. return $section;
  78. }*/
  79. protected function decompileTpl($section) {
  80. Wind::import("SRV:design.bo.PwDesignModuleBo");
  81. if(preg_match_all('/\<design\s*id=\"*D_mod_(\d+)\"*\s*role=\"*module\"*\s*[>|\/>]<\/design>/isU', $section, $matches)) {
  82. foreach ($matches[1] AS $k=>$v) {
  83. $bo = new PwDesignModuleBo($v);
  84. $module = $bo->getModule();
  85. $property = $bo->getView();
  86. $_html = '<pw-list id="'.$k.'">';
  87. $_html .= $bo->getTemplate();
  88. $_html .= '</pw-list>';
  89. $section = str_replace($matches[0][$k], $_html, $section);
  90. $this->_moduleConf[$k] = array(
  91. 'itemid'=>$k,
  92. 'name'=>$module['module_name'],
  93. 'model'=>$bo->getModel(),
  94. 'id'=>$v,
  95. 'titlenum'=>strval($property['titlenum']),
  96. 'desnum'=>strval($property['desnum']),
  97. 'timefmt'=>$property['timefmt'],
  98. 'limit'=>strval($property['limit']));
  99. }
  100. }
  101. return $section;
  102. }
  103. /*
  104. protected function decompilePw($section) {
  105. $in = array(
  106. '<design role="start"/>',
  107. '<!--# if($portal[\'header\']){ #--><template source=\'TPL:common.header\' load=\'true\' /><!--# } #-->',
  108. '<!--# if($portal[\'navigate\']){ #--><div class="bread_crumb">{@$headguide|html}</div><!--# } #-->',
  109. '<!--# if($portal[\'footer\']){ #--><template source=\'TPL:common.footer\' load=\'true\' /><!--# } #-->',
  110. '<design role="end"/>',
  111. );
  112. $out = array(
  113. '<pw-start/>',
  114. '<pw-head/>',
  115. '<pw-navigate/>',
  116. '<pw-footer/>',
  117. '<pw-end/>',
  118. );
  119. return str_replace($in, $out, $section);
  120. }
  121. protected function decompileSegment($section) {
  122. $segment = $this->_getSegmentDs()->getSegmentByPageid($this->pageid);
  123. if(preg_match_all('/\<design\s*role=\"segment\"\s*id=\"(.+)\"[^>]+>/isU', $section, $matches)) {
  124. foreach ($matches[1] AS $k=>$v) {
  125. if (!$v) continue;
  126. if (isset($segment[$v])) {
  127. $section = str_replace($matches[0][$k], $segment[$v]['segment_struct'], $section);
  128. } else {
  129. $section = str_replace($matches[0][$k], '<pw-drag/>', $section);
  130. }
  131. }
  132. }
  133. return $section;
  134. }
  135. protected function decompileStruct($section) {
  136. $ds = Wekit::load('design.PwDesignStructure');
  137. if(preg_match_all('/\<design\s*role=\"title\"\s*id=\"(.+)\"[^>]+>/isU', $section, $matches)) {
  138. foreach ($matches[1] AS $k=>$v) {
  139. if (!$v) continue;
  140. $struct = $ds->getStruct($v);
  141. $_html = '<pw-title>';
  142. $_html .= unserialize($struct['struct_title']);
  143. $_html .= '</pw-title>';
  144. $section = str_replace($matches[0][$k], $_html, $section);
  145. }
  146. }
  147. return $section;
  148. }*/
  149. protected function decompileStyle($section) {
  150. $in = '/["|\']{\@G\:design\.url\.css}\/style\.css\?rand\=(\d+)["|\']/U';
  151. $out = '"{@G:design.url.css}/style.css"';
  152. return preg_replace($in, $out, $section);
  153. }
  154. protected function read($dir) {
  155. if (!is_dir($dir)) return array();
  156. if (!$handle = @opendir($dir)) return array();
  157. while (false !== ($file = @readdir($handle))) {
  158. if ('.' === $file || '..' === $file) continue;
  159. $fileName = $dir . $file;
  160. if (is_file($fileName)){
  161. if (!$_handle = fopen($fileName, 'rb')) continue;
  162. $data = '';
  163. while (!feof($_handle))
  164. $data .= fgets($_handle, 4096);
  165. fclose($_handle);
  166. $this->_files[] = array('filename'=>$fileName, 'data'=>$data);
  167. }elseif (is_dir($fileName . '/')) {
  168. $this->read($fileName. '/');
  169. }
  170. }
  171. unset($data);
  172. @closedir($handle);
  173. return $this->_files;
  174. }
  175. protected function doTxt($charset = 'utf-8') {
  176. $pageInfo = $this->_getPageDs()->getPage($this->pageid);
  177. Wind::import('SRV:design.srv.PwDesignExportTxt');
  178. $srv = new PwDesignExportTxt($pageInfo);
  179. return $srv->txt($charset);
  180. }
  181. protected function xmlFormat($array, $charset = 'utf-8') {
  182. $array['application']['charset'] = $charset;
  183. $dom = new DOMDocument('1.0', $charset);
  184. $root = $dom->createElement('manifest');
  185. $dom->appendChild($root);
  186. $this->_creatDom($root, $dom, $array);
  187. return $dom->saveXML();
  188. }
  189. private function _creatDom($root, $dom, $array) {
  190. foreach ($array AS $k=>$v) {
  191. if (is_numeric($k)) {
  192. $child = $dom->createElement('item');
  193. } else {
  194. $child = $dom->createElement($k);
  195. }
  196. $root->appendChild($child);
  197. if (!is_array($v)){
  198. $child->appendChild($dom->createTextNode($v));
  199. } else {
  200. $this->_creatDom($child, $dom, $v);
  201. }
  202. }
  203. }
  204. private function _getSegmentDs() {
  205. return Wekit::load('design.PwDesignSegment');
  206. }
  207. private function _getPageDs() {
  208. return Wekit::load('design.PwDesignPage');
  209. }
  210. }
  211. ?>