beifenModel.php 3.8 KB

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