zen.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * The zen file of backup module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Yidong Wang <yidong@easycorp.ltd>
  8. * @package backup
  9. * @link https://www.zentao.net
  10. */
  11. class backupZen extends backup
  12. {
  13. /**
  14. * 获取备份文件列表
  15. * Get backup files list.
  16. *
  17. * @access protected
  18. * @return array
  19. */
  20. protected function getBackupList()
  21. {
  22. $backupPath = $this->backup->getBackupPath();
  23. $sqlFiles = glob("{$backupPath}*.sql*");
  24. if(empty($sqlFiles)) return array();
  25. $backupList = array();
  26. foreach($sqlFiles as $file)
  27. {
  28. $fileName = basename($file);
  29. $backupFile = new stdclass();
  30. $backupFile->time = filemtime($file);
  31. $backupFile->name = substr($fileName, 0, strpos($fileName, '.'));
  32. $backupFile->files[$file] = $this->backup->getBackupSummary($file);
  33. $fileBackup = $this->backup->getBackupFile($backupFile->name, 'file');
  34. if($fileBackup) $backupFile->files[$fileBackup] = $this->backup->getBackupSummary($fileBackup);
  35. $codeBackup = $this->backup->getBackupFile($backupFile->name, 'code');
  36. if($codeBackup) $backupFile->files[$codeBackup] = $this->backup->getBackupSummary($codeBackup);
  37. $backupList[$backupFile->name] = $backupFile;
  38. }
  39. krsort($backupList);
  40. return $backupList;
  41. }
  42. /**
  43. * 备份SQL文件
  44. * backupSQL
  45. *
  46. * @param string $fileName
  47. * @param string $reload
  48. * @access protected
  49. * @return array
  50. */
  51. protected function backupSQL($fileName, $reload = 'no')
  52. {
  53. $backFileName = "{$this->backupPath}{$fileName}.sql";
  54. $nosafe = strpos($this->config->backup->setting, 'nosafe') !== false;
  55. if(!$nosafe) $backFileName .= '.php';
  56. $result = $this->backup->backSQL($backFileName);
  57. if(!$result->result) return array('result' => 'fail', 'message' => sprintf($this->lang->backup->error->noWritable, $this->backupPath));
  58. if(!$result->result)
  59. {
  60. if($reload == 'yes')
  61. {
  62. return array('result' => 'fail', 'message' => sprintf($this->lang->backup->error->backupFile, $this->backupPath));
  63. }
  64. else
  65. {
  66. printf($this->lang->backup->error->noWritable, $this->backupPath);
  67. }
  68. }
  69. if(!$nosafe) $this->backup->addFileHeader($backFileName);
  70. return array('result' => 'success');
  71. }
  72. /**
  73. * 备份附件
  74. * Backup appendix file.
  75. *
  76. * @param string $fileName
  77. * @param string $reload
  78. * @access protected
  79. * @return array
  80. */
  81. protected function backupFile($fileName, $reload = 'no')
  82. {
  83. if(strpos($this->config->backup->setting, 'nofile') !== false) return array('result' => 'success');
  84. $result = $this->backup->backFile("{$this->backupPath}{$fileName}.file");
  85. if(!$result->result)
  86. {
  87. if($reload == 'yes')
  88. {
  89. return array('result' => 'fail', 'message' => sprintf($this->lang->backup->error->backupFile, $result->error));
  90. }
  91. else
  92. {
  93. printf($this->lang->backup->error->backupFile, $result->error);
  94. }
  95. }
  96. return array('result' => 'success');
  97. }
  98. /**
  99. * 备份代码
  100. * Backup code
  101. *
  102. * @param string $fileName
  103. * @param string $reload
  104. * @access protected
  105. * @return array
  106. */
  107. protected function backupCode($fileName, $reload = 'no')
  108. {
  109. if(strpos($this->config->backup->setting, 'nofile') !== false) return array('result' => 'success');
  110. $result = $this->backup->backCode("{$this->backupPath}{$fileName}.code");
  111. if(!$result->result) return array('result' => 'fail', 'message' => sprintf($this->lang->backup->error->backupCode, $result->error));
  112. if(!$result->result)
  113. {
  114. if($reload == 'yes')
  115. {
  116. return array('result' => 'fail', 'message' => sprintf($this->lang->backup->error->backupCode, $result->error));
  117. }
  118. else
  119. {
  120. printf($this->lang->backup->error->backupCode, $result->error);
  121. }
  122. }
  123. return array('result' => 'success');
  124. }
  125. /**
  126. * 删除过期文件。
  127. * Remove expired backup files.
  128. *
  129. * @access protected
  130. * @return void
  131. */
  132. protected function removeExpiredFiles()
  133. {
  134. $backupFiles = glob("{$this->backupPath}*.*");
  135. if(empty($backupFiles)) return;
  136. $time = time();
  137. $zfile = $this->app->loadClass('zfile');
  138. foreach($backupFiles as $file)
  139. {
  140. /* Only delete backup file. */
  141. $fileName = basename($file);
  142. if(!preg_match('/[0-9]+\.(sql|file|code)/', $fileName)) continue;
  143. /* Remove before holdDays file. */
  144. if($time - filemtime($file) > $this->config->backup->holdDays * 24 * 3600)
  145. {
  146. $rmFunc = is_file($file) ? 'removeFile' : 'removeDir';
  147. $zfile->{$rmFunc}($file);
  148. if($rmFunc == 'removeDir') $this->backup->processSummary($file, 0, 0, array(), 0, 'delete');
  149. }
  150. }
  151. }
  152. /**
  153. * 还原SQL
  154. * Restore SQL
  155. *
  156. * @param string $fileName
  157. * @access protected
  158. * @return array
  159. */
  160. protected function restoreSQL($fileName)
  161. {
  162. $backupFile = $this->backup->getBackupFile($fileName, 'sql');
  163. if(empty($backupFile)) return array('result' => 'success');
  164. $extension = substr($backupFile, -3);
  165. if($extension == 'php') $this->backup->removeFileHeader($backupFile);
  166. $result = $this->backup->restoreSQL($backupFile);
  167. if($extension == 'php') $this->backup->addFileHeader($backupFile);
  168. if(!$result->result) return array('result' => 'fail', 'message' => sprintf($this->lang->backup->error->restoreSQL, $result->error));
  169. return array('result' => 'success');
  170. }
  171. /**
  172. * 还原附件
  173. * Restore File.
  174. *
  175. * @param string $fileName
  176. * @access protected
  177. * @return array
  178. */
  179. protected function restoreFile($fileName)
  180. {
  181. $backupFile = $this->backup->getBackupFile($fileName, 'file');
  182. if(empty($backupFile)) return array('result' => 'success');
  183. $extension = substr($backupFile, -3);
  184. if($extension == 'php') $this->backup->removeFileHeader($backupFile);
  185. $result = $this->backup->restoreFile($backupFile);
  186. if($extension == 'php') $this->backup->addFileHeader($backupFile);
  187. if(!$result->result) return array('result' => 'fail', 'message' => sprintf($this->lang->backup->error->restoreFile, $result->error));
  188. return array('result' => 'success');
  189. }
  190. /**
  191. * 设置保留天数。
  192. * Set hold days.
  193. *
  194. * @param object $data
  195. * @access public
  196. * @return bool
  197. */
  198. public function setHoldDays($data)
  199. {
  200. if(empty($data->holdDays))
  201. {
  202. dao::$errors['holdDays'] = sprintf($this->lang->error->notempty, $this->lang->backup->change);
  203. return false;
  204. }
  205. if(!preg_match("/^-?\d+$/", (string)$data->holdDays) || $data->holdDays <= 0)
  206. {
  207. dao::$errors['holdDays'] = sprintf($this->lang->backup->error->int, $this->lang->backup->change);
  208. return false;
  209. }
  210. $this->loadModel('setting')->setItem('system.backup.holdDays', $data->holdDays);
  211. return !dao::isError();
  212. }
  213. }