beifenModel.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. class beifenClassModel extends Model
  3. {
  4. /**
  5. * 备份到upload/data下
  6. */
  7. public function start()
  8. {
  9. $alltabls = $this->db->getalltable();
  10. $nobeifne = array(''.PREFIX.'log',''.PREFIX.'logintoken',''.PREFIX.'kqanay',''.PREFIX.'email_cont',''.PREFIX.'dailyfx',''.PREFIX.'todo',''.PREFIX.'city',''.PREFIX.'kqjcmd'); //不备份的表;
  11. $beidir = ''.UPDIR.'/data/'.date('Y.m.d.H.i.s').'.';
  12. $beidir .= substr(str_shuffle(md5($this->rock->jm->getRandkey())),0,15);
  13. foreach($alltabls as $tabs){
  14. if(in_array($tabs, $nobeifne))continue;
  15. $rows = $this->db->getall('select * from `'.$tabs.'`');
  16. $data = array();
  17. $sqla = $this->db->getall('show create table `'.$tabs.'`');
  18. $createsql = $sqla[0]['Create Table'];
  19. $data[$tabs] = array(
  20. //'fields' => $fields,
  21. 'createsql' => $createsql,
  22. 'data' => $rows,
  23. );
  24. $fzshu = 0;
  25. if(isset($rows[0]))foreach($rows[0] as $k1=>$v1)$fzshu++;
  26. //if($fzshu==0){
  27. // $fields = $this->db->gettablefields($tabs);
  28. // $fzshu = count($fields);
  29. //}
  30. $file = ''.$tabs.'_'.$fzshu.'_'.count($rows).'.json';
  31. //$str = $this->rock->jm->encrypt(json_encode($data));
  32. $str = json_encode($data);
  33. $bo = $this->rock->createtxt(''.$beidir.'/'.$file.'', $str);
  34. if(!$bo){echo '无权限写入:'.$beidir.'';break;return false;}
  35. }
  36. return true;
  37. }
  38. /**
  39. * 获取备份的数据
  40. */
  41. public function getbfdata($file, $path='')
  42. {
  43. $str = array();
  44. if($path=='')$path = ''.ROOT_PATH.'/'.UPDIR.'/data/'.$file.'';
  45. if(file_exists($path)){
  46. $cont = file_get_contents($path);
  47. if(substr($cont, 0, 2) != '{"'){
  48. $cont = $this->rock->jm->mcrypt_decrypt($cont);
  49. }
  50. $str = json_decode($cont, true);
  51. }
  52. return $str;
  53. }
  54. public function updatefabric($cont, $ylx=0)
  55. {
  56. $bos = $this->updatefabricfile($cont, $ylx);
  57. if(!$bos)return 'dberr:'.$this->db->lasterror();
  58. return 'ok';
  59. }
  60. public function updatefabricfile($cont='', $ylx=0)
  61. {
  62. if($cont=='')return false;
  63. $data = json_decode($cont, true);
  64. foreach($data as $tabe=>$da){
  65. $table = str_replace('xinhu_', PREFIX, $tabe);
  66. if($ylx==1)$table = PREFIX.$tabe;
  67. $fields = $da['fields'];
  68. $nowfiel= $this->getfieldsa($table);
  69. $str = '';
  70. $sql = '';
  71. if(!$nowfiel){
  72. $str = '`id` int(11) NOT NULL AUTO_INCREMENT';
  73. foreach($fields as $k=>$frs){
  74. $fname = $frs['name'];
  75. $nstr = $this->getfielstr($frs);
  76. if($fname!='id')$str.=','.$nstr.'';
  77. }
  78. $str .=',PRIMARY KEY (`id`)';
  79. $sql = "CREATE TABLE `$table`($str)ENGINE=".getconfig('db_engine','MyISAM')." DEFAULT CHARSET=utf8";
  80. if(isset($da['createsql'])){
  81. $sql = $da['createsql'];
  82. $sql = str_replace('`xinhu_','`'.PREFIX.'', $sql);
  83. }
  84. }else{
  85. foreach($fields as $k=>$frs){
  86. $fname = $frs['name'];
  87. if($fname=='id')continue;
  88. $nstr = $this->getfielstr($frs);
  89. if(!isset($nowfiel[$fname])){
  90. $str.=',add '.$nstr.'';
  91. }else{
  92. $ofrs = $nowfiel[$fname]; //系统上字段类型
  93. $ostr = $this->getfielstr($ofrs);
  94. $lxarr= array('text','mediumtext','bigint');
  95. //如果自己字段长度大于官网就不更新
  96. if($frs['type']==$ofrs['type'] && !isempt($ofrs['lens']) && $ofrs['lens']>$frs['lens']){
  97. }else if($nstr != $ostr && !in_array($ofrs['type'], $lxarr) ){
  98. $str.=',MODIFY '.$nstr.'';
  99. }
  100. }
  101. }
  102. if($str!=''){
  103. $str = substr($str, 1);
  104. $sql = "alter table `$table` $str";
  105. }
  106. }
  107. if($sql!=''){
  108. $bo = $this->db->query($sql);
  109. if($bo)$this->rock->debugs($sql, 'upgmysql');
  110. if(!$bo)return false;
  111. }
  112. }
  113. return true;
  114. }
  115. private function getfieldsa($table)
  116. {
  117. $nowfiel= $this->db->gettablefields($table);
  118. $a = array();
  119. foreach($nowfiel as $k=>$rs){
  120. $a[$rs['name']] = $rs;
  121. }
  122. return $a;
  123. }
  124. private function getfielstr($rs)
  125. {
  126. $str = '`'.$rs['name'].'` '.$rs['types'].'';
  127. $dev = $rs['dev'];
  128. $isnull = $rs['isnull'];
  129. if($isnull=='NO')$str.=' NOT NULL';
  130. if(is_null($dev)){
  131. if($isnull != 'NO')$str.=' DEFAULT NULL';
  132. }else{
  133. $str.=" DEFAULT '$dev'";
  134. }
  135. if(!isempt($rs['explain']))$str.=" COMMENT '".$rs['explain']."'";
  136. return $str;
  137. }
  138. }
粤ICP备19079148号