beifenModel.php 4.2 KB

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