PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/service/design/srv/PwDesignImportZip.php

https://github.com/cuijinquan/nextwind
PHP | 198 lines | 176 code | 5 blank | 17 comment | 2 complexity | 26eada825d4281a6fe635a3b62be3883 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: PwDesignImportZip.php 24904 2013-02-26 04:01:46Z gao.wanggao $
  8. * @package
  9. */
  10. class PwDesignImportZip {
  11. public $newIds = array();
  12. protected $themesPath = '';
  13. protected $pageid = 0;
  14. protected $tplPath = '';
  15. private $_files = array();
  16. private $_tplExt = '.htm';
  17. public function __construct($pageBo) {
  18. $this->pageid = $pageBo->pageid;
  19. $this->themesPath = Wind::getRealDir('THEMES:portal.local.');
  20. $this->tplPath = $this->themesPath . $pageBo->getTplPath();
  21. }
  22. /**
  23. * 检查目录
  24. * Enter description here ...
  25. */
  26. public function checkDirectory() {
  27. if (is_writable($this->themesPath)) return true;
  28. return false;
  29. }
  30. /**
  31. * 检查并导入zip文件
  32. * Enter description here ...
  33. * @param string $filename
  34. */
  35. public function checkZip($filename) {
  36. Wind::import("WIND:parser.WindXmlParser");
  37. Wind::import('LIB:utility.PwZip');
  38. $config = array();
  39. $_isTpl = false;
  40. $extension = array('htm','js','gif','jpg','jpeg','txt','png','css','xml');
  41. $zip = new PwZip();
  42. $xml = new WindXmlParser('1.0', Wekit::app()->charset);
  43. if (!$fileData = $zip->extract($filename)) return new PwError("DESIGN:upload.file.error");
  44. foreach ($fileData AS &$file) {
  45. $file['filename'] = str_replace('\\', '/', $file['filename']);
  46. $pos = strpos($file['filename'], '/');
  47. $lenth = strlen($file['filename']);
  48. $file['filename'] = substr($file['filename'], (int)$pos+1, $lenth-$pos);
  49. if (strtolower($file['filename']) == 'manifest.xml') {
  50. $config = $xml->parseXmlStream($file['data'], 0);
  51. }
  52. //过滤文件类型
  53. $ext = strtolower(substr(strrchr($file['filename'], '.'), 1));
  54. if (!in_array($ext, $extension)) {
  55. unset($file);
  56. continue;
  57. }
  58. //过滤中文文件名
  59. if (preg_match('/^[\x7f-\xff]+$/', $file['filename'])) {
  60. unset($file);
  61. continue;
  62. }
  63. //导入模块数据并记录新ID
  64. if ($file['filename'] == 'module/data.txt') {
  65. $this->importTxt($file['data']);
  66. unset($file);
  67. }
  68. }
  69. //if (!$config) return new PwError("DESIGN:file.check.fail");
  70. foreach ($fileData AS &$_file) {
  71. $ext = strrchr($_file['filename'], ".");
  72. if($ext != $this->_tplExt) continue;
  73. $_file['data'] = $this->replaceTpl($_file['data']);
  74. $_file['data'] = $this->compileStyle($_file['data']);
  75. if ($_file['data']) $_isTpl = true;
  76. }
  77. WindFile::del($filename);
  78. //TODO 版本号验证
  79. if (!$fileData) return new PwError("DESIGN:file.check.fail");
  80. if (!$_isTpl) return new PwError("DESIGN:file.check.fail");
  81. if (!$this->writeFile($fileData)) return true;
  82. return false;
  83. }
  84. /**
  85. * 导入应用中心模版
  86. * Enter description here ...
  87. * @param string $folder
  88. */
  89. public function appcenterToLocal($folder) {
  90. if (!$folder) return false;
  91. $appPath = Wind::getRealDir('THEMES:portal.appcenter.'.$folder.'.');
  92. $fileData = $this->read($appPath);
  93. $ifTpl = false;
  94. foreach ($fileData AS &$file) {
  95. if ($file['filename'] == $appPath . 'module/data.txt') {
  96. $this->importTxt($file['data']);
  97. unset($file);
  98. }
  99. }
  100. foreach ($fileData AS &$_file) {
  101. $_file['filename'] = str_replace($appPath, '', $_file['filename']);
  102. $ext = strrchr($_file['filename'], ".");
  103. if($ext != $this->_tplExt) continue;
  104. $_file['data'] = $this->replaceTpl($_file['data'], $config);
  105. $_file['data'] = $this->compileStyle($_file['data']);
  106. $ifTpl = true;
  107. }
  108. if (!$ifTpl) return false;
  109. if (!$this->writeFile($fileData)) return true;
  110. return false;
  111. }
  112. protected function replaceTpl($section) {
  113. Wind::import('SRV:design.dm.PwDesignModuleDm');
  114. $ds = Wekit::load('design.PwDesignModule');
  115. if (preg_match_all('/\<pw-list\s*id=\"(\d+)\"\s*>/isU',$section, $matches)) {
  116. foreach ($matches[1] AS $k=>$v) {
  117. if (isset($this->newIds[$v])) {
  118. $section = str_replace($matches[0][$k], '<pw-list id="'.$this->newIds[$v].'">', $section);
  119. } else {
  120. $section = str_replace($matches[0][$k], '<pw-list>', $section);
  121. }
  122. }
  123. }
  124. return $section;
  125. }
  126. /**
  127. * 给style.css 加个随机码
  128. * Enter description here ...
  129. * @param unknown_type $section
  130. */
  131. protected function compileStyle($section) {
  132. $in = '{@G:design.url.css}/style.css';
  133. $out = '{@G:design.url.css}/style.css?rand='.Pw::getTime();
  134. return str_replace($in, $out, $section);
  135. }
  136. protected function importTxt($content) {
  137. $srv = Wekit::load('design.srv.PwDesignImportTxt');
  138. $pageInfo = $this->_getPageDs()->getPage($this->pageid);
  139. $srv = new PwDesignImportTxt();
  140. $srv->setPageInfo($pageInfo);
  141. $resource = $srv->checkTxt('', $content);
  142. if ($resource instanceof PwError) return false;
  143. $resource = $srv->importTxt();
  144. $this->newIds = $srv->newIds;
  145. }
  146. protected function writeFile($fileData) {
  147. $failArray = array();
  148. $dir = $this->tplPath;
  149. WindFolder::rm($dir, true);
  150. WindFolder::mk($dir);
  151. foreach ($fileData AS $file) {
  152. WindFolder::mkRecur($dir . '/' . dirname($file['filename']));
  153. if (!WindFile::write($dir . '/' . $file['filename'], $file['data'])) $failArray[] = $file['filename'];
  154. }
  155. return $failArray;
  156. }
  157. protected function read($dir) {
  158. if (!is_dir($dir)) return array();
  159. if (!$handle = @opendir($dir)) return array();
  160. while (false !== ($file = @readdir($handle))) {
  161. if ('.' === $file || '..' === $file) continue;
  162. $fileName = $dir . $file;
  163. if (is_file($fileName)){
  164. if (!$_handle = fopen($fileName, 'rb')) continue;
  165. $data = '';
  166. while (!feof($_handle))
  167. $data .= fgets($_handle, 4096);
  168. fclose($_handle);
  169. $this->_files[] = array('filename'=>$fileName, 'data'=>$data);
  170. } elseif (is_dir($fileName)){
  171. $this->read($fileName .'/');
  172. }
  173. }
  174. unset($data);
  175. @closedir($handle);
  176. return $this->_files;
  177. }
  178. private function _getPageDs() {
  179. return Wekit::load('design.PwDesignPage');
  180. }
  181. }
  182. ?>