protected function postUpload($tmp_name, $filename) { if (strpos($filename, '..') !== false || strpos($filename, '.php.') !== false || preg_match('/\\.php$/', $filename)) { exit('illegal file type!'); } WindFolder::mkRecur(dirname($filename)); if (function_exists("move_uploaded_file") && @move_uploaded_file($tmp_name, $filename)) { @unlink($tmp_name); @chmod($filename, 0777); return filesize($filename); } elseif (@copy($tmp_name, $filename)) { @unlink($tmp_name); @chmod($filename, 0777); return filesize($filename); } elseif (is_readable($tmp_name)) { Wind::import('WIND:utility.WindFile'); WindFile::write($filename, WindFile::read($tmp_name)); @unlink($tmp_name); if (file_exists($filename)) { @chmod($filename, 0777); return filesize($filename); } } return false; }
public function install($patch) { $tmpfiles = $this->bakFiles = array(); WindFolder::mkRecur($this->tmpPath); if ($this->ftp && !is_object($this->ftp)) { try { $this->ftp = $this->ftp['sftp'] ? new PwSftpSave($this->ftp) : new PwFtpSave($this->ftp); } catch (WindFtpException $e) { return false; } } foreach ($patch['rule'] as $rule) { $rule['filename'] = $this->sortFile($rule['filename']); $filename = ROOT_PATH . $rule['filename']; $search = base64_decode($rule['search']); $replace = base64_decode($rule['replace']); $count = $rule['count']; $nums = $rule['nums']; $str = WindFile::read($filename); $realCount = substr_count($str, $search); if ($realCount != $count) { return new PwError('APPCENTER:upgrade.patch.update.fail', array($patch['id'])); } $bakfile = basename($rule['filename']) . '.' . Pw::time2str(WEKIT_TIMESTAMP, 'Ymd') . '.bak'; $bakfile = $this->ftp ? dirname($rule['filename']) . '/' . $bakfile : dirname($filename) . '/' . $bakfile; $tmpfile = tempnam($this->tmpPath, 'patch'); $replacestr = PwSystemHelper::replaceStr($str, $search, $replace, $count, $nums); WindFile::write($tmpfile, $replacestr); if ($this->ftp) { try { $this->ftp->upload($filename, $bakfile); $this->ftp->upload($tmpfile, $rule['filename']); } catch (WindFtpException $e) { return false; } } else { if (!@copy($filename, $bakfile)) { return new PwError('APPCENTER:upgrade.copy.fail', array($rule['filename'])); } if (!@copy($tmpfile, $filename)) { return new PwError('APPCENTER:upgrade.copy.fail', array($rule['filename'])); } } $tmpfiles[] = $tmpfile; $this->bakFiles[] = $bakfile; } $this->_ds()->addLog($patch['id'], $patch, 2); return true; }
/** * step 1: 请求升级信息,获取列表,写入upgrade.temp文件 */ public function checkAction() { $this->_clear(); WindFolder::mkRecur(dirname($this->upgrade_temp)); $r = $this->installService->checkUpgrade(); $result = array(); if (is_array($r)) { foreach ($r as $v) { $result[$v['version']] = $v; } Wekit::cache()->set('system_upgrade_info', $result); } else { $this->setOutput($r, 'connect_fail'); } $this->setOutput($result, 'result'); }
/** * step 1: 请求升级信息,获取列表,写入upgrade.temp文件 */ public function checkAction() { $this->_clear(); WindFolder::mkRecur(dirname($this->upgrade_temp)); $r = $this->installService->checkUpgrade(); $result = array(); if (is_array($r)) { foreach ($r as $v) { $result[$v['version']] = $v; } WindFile::savePhpData(DATA_PATH . 'upgrade/info.tmp', $result); } else { $this->setOutput($r, 'connect_fail'); } $this->setOutput($result, 'result'); }
/** * 编译模板并返回编译后模板地址及内容 * <pre> * <i>$output==true</i>返回编译文件绝对路径地址和内容,不生成编译文件; * <i>$output==false</i>返回编译文件绝对路径地址和内容,生成编译文件 * </pre> * * @param string $template 模板名称 必填 * @param string $suffix 模板后缀 默认为空 * @param boolean $readOnly 是否直接输出模板内容,接受两个值true,false 默认值为false * @param boolean $forceOutput 是否强制返回模板内容,默认为不强制 * @return array(compileFile,content) <pre> * <i>compileFile</i>模板编译文件绝对地址, * <i>content</i>编译后模板输出内容,当<i>$output</i> * 为false时将content写入compileFile</pre> */ public function compile($template, $suffix = '', $readOnly = false, $forceOutput = false) { list($templateFile, $compileFile, $this->currentThemeKey) = $this->windView->getViewTemplate($template, $suffix); if (!is_file($templateFile)) { throw new WindViewException('[viewer.resolver.WindViewerResolver.compile] ' . $templateFile, WindViewException::VIEW_NOT_EXIST); } if (!$this->checkReCompile($templateFile, $compileFile)) { return array($compileFile, $forceOutput || $readOnly ? WindFile::read($compileFile) : ''); } /* @var $_windTemplate WindViewTemplate */ $_windTemplate = Wind::getComponent('template'); $_output = $_windTemplate->compile($templateFile, $this); if (false === $readOnly) { WindFolder::mkRecur(dirname($compileFile)); WindFile::write($compileFile, $_output); } return array($compileFile, $_output); }
public function moveUploadedFile($tmp_name, $filename) { if (strpos($filename, '..') !== false || strpos($filename, '.php.') !== false || preg_match('/\\.php$/i', $filename)) { return false; } WindFolder::mkRecur(dirname($filename)); if (function_exists("move_uploaded_file") && @move_uploaded_file($tmp_name, $filename)) { @chmod($filename, 0777); return true; } elseif (@copy($tmp_name, $filename)) { @chmod($filename, 0777); return true; } elseif (is_readable($tmp_name)) { file_put_contents($filename, file_get_contents($tmp_name)); if (file_exists($filename)) { @chmod($filename, 0777); return true; } } return false; }
/** * @param string $stylePackage * @param booelan $isManifestChanged * @return boolean */ private function _doCss($stylePackage) { $file = $stylePackage . '/' . $this->manifest; $dir = $stylePackage . '/' . $this->cssDevDir; $_dir = $stylePackage . '/' . $this->cssDir; WindFolder::mkRecur($_dir); $files = WindFolder::read($dir, WindFolder::READ_FILE); foreach ($files as $v) { if (WindFile::getSuffix($v) === 'css') { $dev_css = $dir . '/' . $v; //待编译文件 $css = $_dir . '/' . $v; //编译后文件 $data = WindFile::read($dir . '/' . $v); $_data = $this->_compress($data); if (WindFile::write($css, $_data) === false) { return new PwError('STYLE:style.css.write.fail'); } } } return true; }
public function doCompile() { $JS_DEV_PATH = Wind::getRealDir('PUBLIC:res.js.dev'); $JS_BUILD_PATH = Wind::getRealDir('PUBLIC:res.js.build'); Wind::import('Wind:utility.WindFolder'); $files = $this->_getFiles($JS_DEV_PATH); foreach ($files as $file) { $newfile = $JS_BUILD_PATH . substr($file, strlen($JS_DEV_PATH)); WindFolder::mkRecur(dirname($newfile)); if (substr($file, -3) != '.js') { if (!copy($file, $newfile)) { return new PwError('copy failed'); } continue; } $content = WindFile::read($file); $compress = jscompress::pack($content); if (!WindFile::write($newfile, $compress)) { return new PwError('write failed'); } } }
/** * 生成www目录及index.php * */ protected function generateWww() { $content = <<<EOS <?php error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING); define('WIND_DEBUG', 1); require_once '%s'; Wind::register(realpath('%s'), '%s'); Wind::application('%s', '%s')->run(); EOS; $dir = $this->dir . '/' . $this->wwwDir; WindFolder::mkRecur($dir); $content = sprintf($content, $this->_resolveRelativePath($dir, Wind::getRealPath('WIND:Wind.php', true)), $this->_resolveRelativePath($dir, $this->dir), strtoupper($this->name), $this->name, $this->_resolveRelativePath($dir, $this->confFile)); if (!WindFile::write($dir . '/index.php', $content)) { return false; } return true; }
/** * 设置缓存目录 * * @param string $dir 缓存目录,必须是<b>可写可读</b>权限 */ public function setCacheDir($dir) { $_dir = Wind::getRealPath($dir, false, true); WindFolder::mkRecur($_dir); $this->cacheDir = realpath($_dir); }
public static function log($msg, $version, $start = false) { static $log; if (!$log) { $log = Wind::getRealDir('DATA:upgrade.log', true) . '/' . $version . '.log'; WindFolder::mkRecur(dirname($log)); } $status = $start ? WindFile::READWRITE : WindFile::APPEND_WRITEREAD; WindFile::write($log, "\r\n" . date('Y-m-d H:i') . ' ' . $msg, $status); }
/** * 备份 */ public function backUp($fileList) { $root = Wind::getRealDir('ROOT:', true); $dest = Wind::getRealDir('DATA:backup', true) . DIRECTORY_SEPARATOR . $this->local; foreach ($fileList as $v => $hash) { if (is_file($root . DIRECTORY_SEPARATOR . $v)) { WindFolder::mkRecur(dirname($dest . DIRECTORY_SEPARATOR . $v)); $r = @copy($root . DIRECTORY_SEPARATOR . $v, $dest . DIRECTORY_SEPARATOR . $v); if (!$r) { return new PwError('APPCENTER:upgrade.backup.fail'); } } } $this->_log('back up file'); return true; }
protected function _generateService() { if (!$this->need_service) { return true; } $prefix = 'app_' . $this->alias . '_table'; $classFrefix = str_replace(' ', '', ucwords('app_ ' . $this->alias . '_ ' . $this->alias)); WindFolder::mkRecur($this->baseDir . '/service/dao/'); WindFolder::mkRecur($this->baseDir . '/service/dm/'); $dao_file = $this->defaultDir . '/service/dao/defaultdao'; $class_dao = $classFrefix . 'Dao'; $dao = strtr(WindFile::read($dao_file), array('{{classname}}' => $class_dao, '{{prefix}}' => $prefix, '{{author}}' => $this->author, '{{email}}' => $this->email, '{{website}}' => $this->website)); WindFile::write($this->baseDir . '/service/dao/' . $class_dao . '.php', $dao); $dm_file = $this->defaultDir . '/service/dm/defaultdm'; $class_dm = $classFrefix . 'Dm'; $dm = strtr(WindFile::read($dm_file), array('{{classname}}' => $class_dm, '{{author}}' => $this->author, '{{email}}' => $this->email, '{{website}}' => $this->website)); WindFile::write($this->baseDir . '/service/dm/' . $class_dm . '.php', $dm); $ds_file = $this->defaultDir . '/service/defaultds'; $class_ds = $classFrefix; $ds = strtr(WindFile::read($ds_file), array('{{classname}}' => $class_ds, '{{alias}}' => $this->alias, '{{class_dm}}' => $class_dm, '{{class_dao}}' => $class_dao, '{{author}}' => $this->author, '{{email}}' => $this->email, '{{website}}' => $this->website)); WindFile::write($this->baseDir . '/service/' . $class_ds . '.php', $ds); }
public function extract2($zipPack, $target) { if (!$zipPack || !is_file($zipPack)) { return false; } $extractedData = array(); $target = rtrim($target, '/'); WindFolder::mkRecur($target, 0777); $this->fileHandle = fopen($zipPack, 'rb'); $filesize = sprintf('%u', filesize($zipPack)); $EofCentralDirData = $this->_findEOFCentralDirectoryRecord($filesize); if (!is_array($EofCentralDirData)) { return false; } $centralDirectoryHeaderOffset = $EofCentralDirData['centraldiroffset']; for ($i = 0; $i < $EofCentralDirData['totalentries']; $i++) { rewind($this->fileHandle); fseek($this->fileHandle, $centralDirectoryHeaderOffset); $centralDirectoryData = $this->_readCentralDirectoryData(); if (!is_array($centralDirectoryData)) { $centralDirectoryHeaderOffset += 46; continue; } $centralDirectoryHeaderOffset += 46 + $centralDirectoryData['filenamelength'] + $centralDirectoryData['extrafieldlength'] + $centralDirectoryData['commentlength']; if (substr($centralDirectoryData['filename'], -1) === '/') { WindFolder::mkRecur($target . '/' . $centralDirectoryData['filename'], 0777); $extractedData['folder'][$i] = $centralDirectoryData['filename']; continue; } else { $data = $this->_readLocalFileHeaderAndData($centralDirectoryData); if ($data === false) { continue; } WindFile::write($target . '/' . $centralDirectoryData['filename'], $data); $extractedData['file'][$i] = $centralDirectoryData['filename']; } } fclose($this->fileHandle); return $extractedData; }
public function generate() { $addons = Wekit::load('APPS:appcenter.service.srv.PwInstallApplication')->getConfig('style-type'); $base = str_replace('/', '.', $addons[$this->style_type][1]); $this->defaultDir = Wind::getRealDir('THEMES:' . $base . '.default'); if (!is_dir($this->defaultDir)) { return new PwError('APPCENTER:generate.style.unsupport'); } $this->baseDir = Wind::getRealDir('THEMES:' . $base . '.' . $this->alias); if (is_dir($this->baseDir)) { return new PwError('APPCENTER:alias.exist'); } WindFolder::mkRecur($this->baseDir); Wind::import('APPS:appcenter.service.srv.helper.PwSystemHelper'); $writable = PwSystemHelper::checkWriteAble($this->baseDir . '/'); if (!$writable) { return new PwError('APPCENTER:generate.copy.fail'); } PwApplicationHelper::copyRecursive($this->defaultDir, $this->baseDir); $file = $this->baseDir . '/Manifest.xml'; Wind::import('WIND:parser.WindXmlParser'); $parser = new WindXmlParser(); $manifest = $parser->parse($file); $manifest['application']['name'] = $this->name; $manifest['application']['alias'] = $this->alias; $manifest['application']['description'] = $this->description; $manifest['application']['version'] = $this->version; $manifest['application']['pw-version'] = $this->pwversion; $manifest['application']['website'] = $this->website; $manifest['application']['author-name'] = $this->author; $manifest['application']['author-email'] = $this->email; $parser = new WindXmlParser(); $manifest = str_replace('><', ">\n\t<", $parser->parseToXml(array('manifest' => $manifest), Wind::getApp()->getResponse()->getCharset())); WindFile::write($this->baseDir . '/Manifest.xml', $manifest); return 'THEMES:' . $base . '.' . $this->alias; }
/** * 保存敏感词字典 * * @param array $nodes */ public function saveData($nodes) { WindFolder::mkRecur($this->file); WindFile::write($this->file . '/word.txt', serialize($nodes)); }
/** * 对pw-tpl标签进行编译 * Enter description here ... * @param unknown_type $tplId */ public function compileTpl($section, $compile = false) { if (preg_match_all('/\\<pw-tpl\\s*id=\\"([\\w.]+)\\"\\s*\\/>/isU', $section, $matches)) { $ds = Wekit::load('design.PwDesignSegment'); foreach ($matches[1] as $k => $matche) { if (!$matche) { continue; } list($common, $tpl) = explode('.', $matche, 2); //解析pw-tpl id="common.segment" 如果模版目录内文件不存在,使用公共的 if ($common == 'common') { $file = $this->dir . $tpl . '.htm'; $v = $tpl; $dir = $this->dir; if (!WindFile::isFile($file)) { $v = $tpl; $dir = $this->commonDir; } } else { $v = $matche; $dir = $this->dir; } $file = $dir . $v . '.htm'; if (!WindFile::isFile($file)) { WindFolder::mkRecur($dir); $isAble = $this->_checkRealWriteAble($dir); if (!$isAble) { return $section; } WindFolder::mkRecur(dirname($dir) . '/images/'); WindFolder::mkRecur(dirname($dir) . '/css/'); $this->write('<pw-drag id="' . $v . '"/>', $file); } $xmlFile = dirname($dir) . '/Manifest.xml'; if (!WindFile::isFile($xmlFile)) { $fromFile = Wind::getRealDir('TPL:special.default.') . 'Manifest.xml'; @copy($fromFile, $xmlFile); @chmod($xmlFile, 0777); } $content = $this->read($file); if ($compile) { $content = $this->compileDesign($content, $v); $ds->replaceSegment($v . '__tpl', $this->pageid, '', $content); if ($this->isCompile) { $this->write($content, $file); } } $section = str_replace($matches[0][$k], $content, $section); } } return $section; }
/** * 返回模板的编译文件绝对路径地址 * * 根据模板的逻辑名称,返回模板的绝对路径信息,支持命名空间方式定义模板信息.<code> * $template='templateName'; //return $compileDir/templateName.$ext * $template='subTemplateDir.templateName'; //return $compileDir/subTemplateDir_templateName.$ext * $template='namespace:templateName'; //return $compileDir/__external_subDir_templateName.$ext</code> * <note><b>注意:</b>$template为空则返回当前的模板的路径信息.</note> * @param string $template 模板名称, 默认值为空, 为空则返回当前模板的编译文件 * @return string */ public function getCompileFile($template = '') { if (!$this->compileDir) { return; } if ($this->compileDir == $this->templateDir) { throw new WindViewException('[wind.viewer.WindView.getCompileFile] the same directory compile and template.'); } if (!$template) { $template = $this->templateName; } if (false !== ($pos = strpos($template, ':'))) { $template = str_replace('.', '_', '__external.' . substr($template, $pos + 1)); } if (!empty($this->theme['theme']) && !empty($this->theme['package'])) { $template = $this->compileDir . '.' . $this->theme['theme'] . '.' . $template; } else { $template = $this->compileDir . '.' . $template; } $dir = Wind::getRealPath($template, false, true); WindFolder::mkRecur(dirname($dir)); return $this->compileExt ? $dir . '.' . $this->compileExt : $dir; }
/** * 设置日志保存的路径 * * @param string $logFile 日志保存的路径 * @return void */ public function setLogDir($logDir) { $this->_logDir = Wind::getRealDir($logDir); WindFolder::mkRecur($this->_logDir); }
/** * 头像转移 */ public function avatarAction() { $end_uid = $this->getMaxUid(); ini_set('max_execution_time', 0); $time_start = microtime(true); list($ftp, $attachDir) = $this->_getFtp(); $defauleDir = rtrim(Wind::getRealDir('PUBLIC:res.images.face', true), '/'); list($start_uid, $end) = $this->_getStartAndLimit(intval($this->getInput('uid', 'get')), $end_uid, $ftp ? true : false); while ($start_uid < $end) { $res = $this->_getOldAvatarPath($attachDir, $start_uid); $big = $res['big']; $middle = $res['middle']; $small = $res['small']; if (!$this->checkFile($middle)) { $big = $defauleDir . '/face_big.jpg'; $middle = $defauleDir . '/face_middle.jpg'; $small = $defauleDir . '/face_small.jpg'; } $_toPath = '/avatar/' . Pw::getUserDir($start_uid) . '/'; $to_big = $_toPath . $start_uid . '.jpg'; $to_middle = $_toPath . $start_uid . '_middle.jpg'; $to_small = $_toPath . $start_uid . '_small.jpg'; if ($ftp) { $ftp->mkdirs($_toPath); $ftp->upload($big, $to_big); $ftp->upload($middle, $to_middle); $ftp->upload($small, $to_small); } else { WindFolder::mkRecur($attachDir . $_toPath); copy($big, $attachDir . $to_big); copy($middle, $attachDir . $to_middle); copy($small, $attachDir . $to_small); } $start_uid++; } if ($end < $end_uid) { $this->setOutput($end, 'uid'); $this->setOutput($this->getInput('token', 'get'), 'token'); $this->setTemplate('upgrade_avatar'); } else { $this->showMessage('升级成功!'); } }
/** * 编译模板片段,并将模板片段放在name缓存文件中 * 模板文件中允许存在如下三种形式: * <pre> * 1、第一种: * <hook-action name="hook1" args='a,b,c'> * <div>i am from hook1 {$a} |{$b}|{$c}</div> * </hook-action> * 如上将会被编译成: * function templateName_hook1($a, $b, $c){ * } * 2、第二种: * <hook-action name="hook2"> * <div> i am from hook2 {$data} </div> * </hook-action> * 如上将会编译成: * function templateName_hook2($data){ * } * 3、第三种: * <div> i am from segment {$data}</div> * 如上将会被编译成: * function templateName($data) { * } * * 模板标签: * <segment alias='' name='' args='' tpl='' /> * tpl文件中的模板内容按照如上三种规则被编译之后,将会保存到__segment_alias文件中 * 调用方法根据: * tpl_name来调用,如果func没有写,则调用方法为tpl,否则为tpl_func,传入参数为args * </pre> * * @param string $template * @param array $args * @param string $func * @param string $alias * @param WindViewResolve $viewer * @return */ public static function segment($template, $args, $func = '', $alias = '', $viewer = null) { if ($viewer instanceof WindViewerResolver) { self::$viewer = $viewer; } $_prefix = str_replace(array(':', "."), '_', $template); $alias = '__segment_' . strtolower($alias ? $alias : $_prefix); list($templateFile, $cacheCompileFile) = self::$viewer->getWindView()->getViewTemplate($template); $pathinfo = pathinfo($cacheCompileFile); $cacheCompileFile = $pathinfo['dirname'] . '/' . $alias . '.' . $pathinfo['extension']; $_method = strtoupper($func ? $_prefix . '_' . $func : $_prefix); if (WIND_DEBUG) { WindFolder::mkRecur(dirname($cacheCompileFile)); WindFile::write($cacheCompileFile, '', WindFile::READWRITE); } else { if (!function_exists($_method) && is_file($cacheCompileFile)) { include $cacheCompileFile; } if (function_exists($_method)) { call_user_func_array($_method, $args); return; } } if (!($content = self::_resolveTemplate($templateFile, strtoupper($_prefix)))) { return; } $_content = array(); foreach ($content as $method => $_item) { $_tmpArgs = ''; foreach ($_item[1] as $_k) { $_tmpArgs .= '$' . trim($_k) . ','; } $windTemplate = Wind::getComponent('template'); $_content[] = '<?php if (!function_exists("' . $method . '")) {function ' . $method . '(' . trim($_tmpArgs, ',') . '){?>'; $_content[] = $windTemplate->compileStream($_item[0], self::$viewer); $_content[] = '<?php }}?>'; } WindFolder::mkRecur(dirname($cacheCompileFile)); WindFile::write($cacheCompileFile, implode("\r\n", $_content), WindFile::APPEND_WRITE); include $cacheCompileFile; call_user_func_array($_method, $args); }
protected function writeFile($fileData) { $failArray = array(); $dir = $this->tplPath; WindFolder::rm($dir, true); WindFolder::mk($dir); foreach ($fileData as $file) { WindFolder::mkRecur($dir . '/' . dirname($file['filename'])); if (!WindFile::write($dir . '/' . $file['filename'], $file['data'])) { $failArray[] = $file['filename']; } } return $failArray; }
/** * 解压压缩包,将源文件解压至目标文件 * 目前只支持zip文件的解压,返回解后包文件绝对路径地址 * * @param string $source * @param string $target * @return string */ public static function extract($source, $target) { Wind::import('APPS:appcenter.service.srv.helper.PwExtractZip'); $zip = new PwExtractZip(); if (!($data = $zip->extract($source))) { return false; } $_tmp = ''; foreach ($data as $value) { if ($_tmp === '') { list($_tmp) = explode('/', $value['filename'], 2); } $filename = $target . '/' . $value['filename']; WindFolder::mkRecur(dirname($filename)); WindFile::write($filename, $value['data']); } return $_tmp ? $target . '/' . $_tmp : false; }