cls_upgrade_util.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /*
  3. * Krpano100 升级信息工具类
  4. * ============================================================================
  5. * 技术支持:2015-2099 成都世纪川翔科技有限公司
  6. * 官网地址: http://www.krpano100.com
  7. * ----------------------------------------------------------------------------
  8. * $Author: wh 932625974#qq.com $
  9. * $Id: cls_upgrade.php 28028 2016-04-27Z wh $
  10. */
  11. class UpgradeUtil{
  12. private $root;
  13. /*
  14. * 构造方法,传入项目跟路径
  15. **/
  16. public function __construct($rootpath){
  17. $this->root = $rootpath;
  18. }
  19. /*
  20. * 将MD5的数组写入到文件中
  21. */
  22. public function md5_list_write($doc){
  23. $cont = var_export($doc,true);
  24. $cont ="<?php\n\$hash_list = $cont \n?>";
  25. if(file_put_contents($this->root.'upgrade/hash_list.php',$cont))
  26. return true;
  27. }
  28. /*
  29. * 生成文件的MD5值,并写入到数组中
  30. * @except 计算MD5值时,忽略的目录
  31. */
  32. public function generate_md5_list($except =null){
  33. if (!file_exists($this->root)) {
  34. return false;
  35. }
  36. if ($except == null) {
  37. $except = array();
  38. }
  39. $except[] = $this->root.'data/';
  40. $except[] = $this->root.'temp/';
  41. $except[] = $this->root.'.git/';
  42. $except[] = $this->root.'upgrade/';
  43. $except[] = $this->root.'docs/';
  44. $res = array();
  45. $this->do_generate($this->root,$except,$res);
  46. $this->md5_list_write($res);
  47. return true;
  48. }
  49. /**
  50. * 得到要写入的目录是否有写入权限
  51. */
  52. public function list_validate_dir($up_code_dir){
  53. $dirs = array();
  54. $this->do_list_dir($up_code_dir,"",$dirs);
  55. $res = array();
  56. foreach ($dirs as $dir) {
  57. $writable = $this->is_writable($this->root.$dir);
  58. $res[$dir] = $writable;
  59. }
  60. return $res;
  61. }
  62. /*
  63. * 校验需要被替换的文件
  64. * @up_code_dir 升级文件的目录
  65. * @hash_list 原目录的所有hash列表
  66. */
  67. public function list_validate_file($up_code_dir,$hash_list){
  68. $res = array();
  69. $this->do_validate($up_code_dir,'',$hash_list,$res);
  70. //对数组按是否修改降序排列 是否可写升序排列
  71. foreach ($res as $v) {
  72. $key1[] = $v;
  73. }
  74. array_multisort($key1,SORT_NUMERIC,SORT_DESC,$res);
  75. return $res;
  76. }
  77. /*
  78. * @dir_ab 绝对路径
  79. * @dir_re 相对路径
  80. */
  81. private function do_list_dir($dir_ab,$dir_re,&$res){
  82. if(is_dir($dir_ab))
  83. {
  84. if ($dh = opendir($dir_ab))
  85. {
  86. while (($file = readdir($dh)) !== false)
  87. {
  88. if((is_dir($dir_ab.$file)) && $file!="." && $file!="..")
  89. {
  90. $old_ab = $dir_re.$file; //用户真实路径
  91. if (file_exists($this->root.$old_ab)) {
  92. if (!in_array($old_ab, $res)) {
  93. $res[] = $old_ab;
  94. }
  95. }
  96. $this->do_list_dir($dir_ab.$file.'/',$dir_re.$file.'/',$res);
  97. }
  98. }
  99. closedir($dh);
  100. }
  101. }
  102. }
  103. /*
  104. * @dir_ab 绝对路径
  105. * @dir_re 相对路径
  106. */
  107. private function do_validate($dir_ab,$dir_re,$hash_list,&$res){
  108. if(is_dir($dir_ab))
  109. {
  110. if ($dh = opendir($dir_ab))
  111. {
  112. while (($file = readdir($dh)) !== false)
  113. {
  114. if((is_dir($dir_ab.$file)) && $file!="." && $file!="..")
  115. {
  116. //目录
  117. $this->do_validate($dir_ab.$file.'/',$dir_re.$file.'/',$hash_list,$res);
  118. }
  119. else
  120. {
  121. if($file!="." && $file!="..")
  122. {
  123. $modify = 0; //是否修改过 0 未修改
  124. $new_re = $dir_re.$file; //相对路径
  125. $old_ab = $this->root.$new_re;//被替换文件的绝对路径
  126. if (file_exists($old_ab) && $hash_list[$new_re] != md5_file($old_ab) && $file!="version.conf") {
  127. $modify = 1 ;
  128. }
  129. $res[$new_re] = $modify;
  130. }
  131. }
  132. }
  133. closedir($dh);
  134. }
  135. }
  136. }
  137. private function do_generate($dir,$except,&$res){
  138. if(is_dir($dir))
  139. {
  140. $flag = true;
  141. foreach ($except as $e) {
  142. if ($e==$dir) {
  143. $flag = false;
  144. break;
  145. }
  146. }
  147. if ($flag&&$dh = opendir($dir))
  148. {
  149. while (($file = readdir($dh)) !== false)
  150. {
  151. if((is_dir($dir.$file)) && $file!="." && $file!="..")
  152. {
  153. //目录
  154. $this->do_generate($dir.$file.'/',$except,$res);
  155. }
  156. else
  157. {
  158. if($file!="." && $file!="..")
  159. {
  160. $path = $dir.$file;
  161. $res[substr($path, mb_strlen($this->root))] = md5_file($path);
  162. }
  163. }
  164. }
  165. closedir($dh);
  166. }
  167. }
  168. }
  169. private function validate_noexists_writable($file){
  170. $file = str_replace("\\", "/", $file);
  171. $file = str_replace($this->root, "", $file);
  172. $arr = explode("/", $file);
  173. $writeable = 0;
  174. $temp;
  175. foreach ($arr as $v) {
  176. if ($this->is_writable($this->root.$temp.$v)==0) {
  177. $writeable = 0;
  178. break;
  179. }
  180. $temp.=$v.'/';
  181. $writeable = 1;
  182. }
  183. return $writeable;
  184. }
  185. private function is_writable($file){
  186. if (is_dir($file)){
  187. $dir = $file;
  188. if ($fp = @fopen("$dir/test.txt", 'w')) {
  189. @fclose($fp);
  190. @unlink("$dir/test.txt");
  191. $writeable = 1;
  192. } else {
  193. $writeable = 0;
  194. }
  195. } else {
  196. if(!file_exists($file)){
  197. $writeable = 1;
  198. }else{
  199. if ($fp = @fopen($file, 'a+')) {
  200. @fclose($fp);
  201. $writeable = 1;
  202. } else {
  203. $writeable = 0;
  204. }
  205. }
  206. }
  207. return $writeable;
  208. }
  209. }
  210. ?>