设为首页 收藏本站
查看: 515|回复: 0

[经验分享] 用PHP写的一个web升级脚本

[复制链接]

尚未签到

发表于 2017-4-5 06:31:03 | 显示全部楼层 |阅读模式
  管理web项目一直是件很困难的事情,程序使用时间越长里面的spam文件也越多。如何保证web目录的清洁是个重要的工作,结合这个脚本和subversion的管理功能,可以方便你的日常升级管理。

<?php
/**
* Website upgrade script
* Author: victorwmh
* Email: victorwmh@gmail.com
* Date: 2011-08-23
*
* Description:
* $argc and $argv is default variables of CLI mode.
* $argc is int, $argv is array. $argv[0] is self file name.
*/
$ublocal = "/home/df3c/upgrade/bundles/";
$uapplocal = "/home/df3c/web";
if ($argc < 2) exit('no argument.');
$ubname = $argv[1];
$ufox = new UpgradeFox();
$ufox->init($uapplocal, $ublocal, $ubname);
$ufox->upgrade();

/**
* Upgrade-fox class.
* @author victorwmh
*
*/
class UpgradeFox {
/* update bundle name */
private $bname = '';
/* update bundle location */
private $blocal = '';
/* application location for upgrade */
private $applocal = '';
/* bundle extract directory */
private $extdir = '';
/* all directory of boundle */
private $directories = array();
/* all file of boundle */
private $files = array();

/**
* Initialization basic information.
* @param string $uapplocal
* @param string $ublocal
* @param string $ubname
*/
public function init($uapplocal, $ublocal, $ubname) {
$this->applocal = rtrim($uapplocal, '/');
$this->bname = $ubname;
$this->blocal = rtrim($ublocal, '/');
$this->extdir = rtrim($this->blocal . '/' . $this->bname, '.zip');
}

/**
* Update files in the bundle and delete files in the list for dellist.txt.
*/
public function upgrade() {
ob_start();
echo "Upgrade processing...\n";
echo "Upgrade will to overwrite or remove a files.\n";
echo "Are you want to continue? press (y or n):";
ob_flush();
if (trim(fgets(STDIN)) != 'y') {
exit("Canceled.\n\n");
};
echo "Check: ";
ob_flush();
if ($this->check() === FALSE) {
exit("bundle not exist.\nUpgrade failure.\n\n");
} elseif($this->check() == 'zip') {
echo "Ok!\nExtract bundle: ";
ob_flush();
$this->extract();
}
$this->uverify();
echo "Ok!\nUpdate files: ";
ob_flush();
$this->update();
echo "Ok!\nClean files: ";
ob_flush();
$this->clean();
echo "Ok!\nFinished.\n\n";
ob_end_flush();
}

/**
* Check upgrade bundle.
*/
private function check() {
if (file_exists($this->blocal . '/' . $this->bname)) {
if (preg_match('/(\.zip)+$/', $this->bname)) {
return 'zip';
} elseif(is_dir($this->blocal . '/' . $this->bname)) {
return 'dir';
} else {
return FALSE;
}
} else {
return FALSE;
}
}

/**
* Extract upgrade bundle.
*/
private function extract() {
$zip = new ZipArchive();
$zip->open($this->blocal . '/' . $this->bname);
if (! $zip->extractTo($this->blocal)) {
$zip->close();
exit("extract is failure.\nUpgrade failure.\n\n");
}
}

/**
* Update files in the bundle.
*/
private function update() {
$this->sortDirFile($this->extdir);
$this->mkDirAppend();
$this->uoverwrite();
$this->udelete();
}

/**
* Verify bundles.
*/
private function uverify() {
$ufoxfile = $this->extdir . '/ufox-clean.ini';
if ( ! file_exists($ufoxfile)) {
exit("bundle rootdir is not ufox-clean.ini\n");
}
}
/**
* Delete trash files of applocal.
*/
private function udelete() {
$ufoxfile = $this->extdir . '/ufox-clean.ini';
$delfiles = $this->getFileContent($ufoxfile);
foreach ($delfiles as $file) {
$file = preg_replace('/\s.*/', '', $file);
$tmp = $this->applocal . '/' . $file;
if ('' == $file) {
continue;
} elseif (file_exists($tmp)) {
is_dir($tmp) ? $this->rmdir($tmp) : unlink($tmp);
}
}
unlink($this->applocal . '/ufox-clean.ini');
}

/**
* add or overwrite files.
*/
private function uoverwrite() {
$bdirname = rtrim($this->bname, '.zip');
foreach ($this->files as $file) {
$tmp = $this->applocal . '/' . substr($file, strpos($file, $bdirname)+strlen($bdirname));
copy($file, $tmp);
}
}

/**
* Clean extract files.
*/
private function clean() {
$this->rmdir($this->extdir);
}

/**
* Get contents of the file.
* @param string $file
* @return array
*/
private function getFileContent($file) {
/* Line breaks for each OS. win:“\r\n”,0x0D0A; linux:“\n”,0x0A; mac:"\r",0x0D; */
if (file_exists($file)) {
$contents = file_get_contents($file);
if (strpos($contents, "\r\n") > 0) {
$separator = "\r\n";
} elseif (strpos($contents, "\n") > 0) {
$separator = "\n";
} elseif (strpos($contents, "\r") > 0) {
$separator = "\r";
}
return explode($separator, $contents);
} else {
return FALSE;
}
}

/**
* sort all file and sub directory of dir.
* @param string $dir
*/
private function sortDirFile($dir) {
$handle = opendir($dir);
if ($handle) {
while (($name = readdir($handle)) !== false) {
if ($name === '.' || $name === '..') continue;
$tmp = realpath($dir . '/' . $name );
if (is_dir($tmp)) {
$this->directories[] = $tmp;
$this->sortDirFile($tmp);
} else {
$this->files[] = $tmp;
}
}
closedir($handle);
}
}

/**
* mk appended sub directories of bundle.
*/
private function mkDirAppend() {
$bdirname = rtrim($this->bname, '.zip');
foreach ($this->directories as $dir) {
$dir = substr($dir, strpos($dir, $bdirname)+strlen($bdirname));
$tmp = $this->applocal . '/' . $dir;
if (! file_exists($tmp)) {
mkdir($tmp);
}
}
}

/**
* Remove directory.
* @param string $dir
*/
private function rmdir($dir) {
if (! is_dir($dir)) return false;
$handle = opendir($dir);
while (($file = readdir($handle)) !== false) {
if ($file !== '.' && $file !== '..') {
$tmp = $dir . '/' . $file;
is_dir($tmp) ? $this->rmdir($tmp) : @unlink($tmp);
}
}
closedir($handle);
return rmdir($dir);
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-360238-1-1.html 上篇帖子: [转]php通用连接数据库类 下篇帖子: php后退一页表单内容的保存
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表