PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/wuhang2003/phpwind
PHP | 223 lines | 191 code | 10 blank | 22 comment | 12 complexity | 4d0136fdb8a035c587be1cd0607117e0 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->filterTemplate($_file['data']);
  74. $_file['data'] = $this->replaceTpl($_file['data']);
  75. $_file['data'] = $this->compileStyle($_file['data']);
  76. if ($_file['data']) $_isTpl = true;
  77. }
  78. WindFile::del($filename);
  79. //TODO 版本号验证
  80. if (!$fileData) return new PwError("DESIGN:file.check.fail");
  81. if (!$_isTpl) return new PwError("DESIGN:file.check.fail");
  82. if (!$this->writeFile($fileData)) return true;
  83. return false;
  84. }
  85. /**
  86. * 导入应用中心模版
  87. * Enter description here ...
  88. * @param string $folder
  89. */
  90. public function appcenterToLocal($folder) {
  91. if (!$folder) return false;
  92. $appPath = Wind::getRealDir('THEMES:portal.appcenter.'.$folder.'.');
  93. $fileData = $this->read($appPath);
  94. $ifTpl = false;
  95. foreach ($fileData AS &$file) {
  96. if ($file['filename'] == $appPath . 'module/data.txt') {
  97. $this->importTxt($file['data']);
  98. unset($file);
  99. }
  100. }
  101. foreach ($fileData AS &$_file) {
  102. $_file['filename'] = str_replace($appPath, '', $_file['filename']);
  103. $ext = strrchr($_file['filename'], ".");
  104. if($ext != $this->_tplExt) continue;
  105. $_file['data'] = $this->replaceTpl($_file['data'], $config);
  106. $_file['data'] = $this->compileStyle($_file['data']);
  107. $ifTpl = true;
  108. }
  109. if (!$ifTpl) return false;
  110. if (!$this->writeFile($fileData)) return true;
  111. return false;
  112. }
  113. protected function replaceTpl($section) {
  114. Wind::import('SRV:design.dm.PwDesignModuleDm');
  115. $ds = Wekit::load('design.PwDesignModule');
  116. if (preg_match_all('/\<pw-list\s*id=\"(\d+)\"\s*>/isU',$section, $matches)) {
  117. foreach ($matches[1] AS $k=>$v) {
  118. if (isset($this->newIds[$v])) {
  119. $section = str_replace($matches[0][$k], '<pw-list id="'.$this->newIds[$v].'">', $section);
  120. } else {
  121. $section = str_replace($matches[0][$k], '<pw-list>', $section);
  122. }
  123. }
  124. }
  125. return $section;
  126. }
  127. /**
  128. * 替换frame标签
  129. */
  130. protected function filterTemplate($string) {
  131. $string = str_replace('<?', '&lt;?', $string);
  132. $in = array(
  133. '/<!--#(.*)#-->/isU',
  134. '/<!--\{(.*)\}-->/isU',
  135. '/<frame(.*)>/isU',
  136. '/<\/fram(.*)>/isU',
  137. '/<iframe(.*)>/isU',
  138. '/<\/ifram(.*)>/isU'
  139. );
  140. $out = array(
  141. '&lt;!--# \\1 #--&gt',
  142. '&lt;!--{ \\1 }--&gt',
  143. '&lt;frame\\1&gt;',
  144. '&lt;/fram\\1&gt;',
  145. '&lt;iframe\\1&gt;',
  146. '&lt;/ifram\\1&gt;'
  147. );
  148. return preg_replace($in ,$out, $string);
  149. }
  150. /**
  151. * 给style.css 加个随机码
  152. * Enter description here ...
  153. * @param unknown_type $section
  154. */
  155. protected function compileStyle($section) {
  156. $in = '{@G:design.url.css}/style.css';
  157. $out = '{@G:design.url.css}/style.css?rand='.Pw::getTime();
  158. return str_replace($in, $out, $section);
  159. }
  160. protected function importTxt($content) {
  161. $srv = Wekit::load('design.srv.PwDesignImportTxt');
  162. $pageInfo = $this->_getPageDs()->getPage($this->pageid);
  163. $srv = new PwDesignImportTxt();
  164. $srv->setPageInfo($pageInfo);
  165. $resource = $srv->checkTxt('', $content);
  166. if ($resource instanceof PwError) return false;
  167. $resource = $srv->importTxt();
  168. $this->newIds = $srv->newIds;
  169. }
  170. protected function writeFile($fileData) {
  171. $failArray = array();
  172. $dir = $this->tplPath;
  173. WindFolder::rm($dir, true);
  174. WindFolder::mk($dir);
  175. foreach ($fileData AS $file) {
  176. WindFolder::mkRecur($dir . '/' . dirname($file['filename']));
  177. if (!WindFile::write($dir . '/' . $file['filename'], $file['data'])) $failArray[] = $file['filename'];
  178. }
  179. return $failArray;
  180. }
  181. protected function read($dir) {
  182. if (!is_dir($dir)) return array();
  183. if (!$handle = @opendir($dir)) return array();
  184. while (false !== ($file = @readdir($handle))) {
  185. if ('.' === $file || '..' === $file) continue;
  186. $fileName = $dir . $file;
  187. if (is_file($fileName)){
  188. if (!$_handle = fopen($fileName, 'rb')) continue;
  189. $data = '';
  190. while (!feof($_handle))
  191. $data .= fgets($_handle, 4096);
  192. fclose($_handle);
  193. $this->_files[] = array('filename'=>$fileName, 'data'=>$data);
  194. } elseif (is_dir($fileName)){
  195. $this->read($fileName .'/');
  196. }
  197. }
  198. unset($data);
  199. @closedir($handle);
  200. return $this->_files;
  201. }
  202. private function _getPageDs() {
  203. return Wekit::load('design.PwDesignPage');
  204. }
  205. }
  206. ?>