PageRenderTime 932ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/controllers/tools_controller.php

https://github.com/hashing/basercms
PHP | 329 lines | 183 code | 32 blank | 114 comment | 31 complexity | f168077205abb21ad115ec80c84a2411 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * ツールコントローラー
  5. *
  6. * PHP versions 5
  7. *
  8. * baserCMS : Based Website Development Project <http://basercms.net>
  9. * Copyright 2008 - 2012, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  10. *
  11. * @copyright Copyright 2008 - 2012, baserCMS Users Community
  12. * @link http://basercms.net baserCMS Project
  13. * @package baser.controllers
  14. * @since baserCMS v 0.1.0
  15. * @version $Revision$
  16. * @modifiedby $LastChangedBy$
  17. * @lastmodified $Date$
  18. * @license http://basercms.net/license/index.html
  19. */
  20. /**
  21. * ツールコントローラー
  22. *
  23. * @package baser.controllers
  24. */
  25. class ToolsController extends AppController {
  26. /**
  27. * クラス名
  28. *
  29. * @var string
  30. * @access public
  31. */
  32. var $name = 'Tools';
  33. var $uses = array('Tool', 'Page');
  34. /**
  35. * コンポーネント
  36. *
  37. * @var array
  38. * @access public
  39. */
  40. var $components = array('BcAuth','Cookie','BcAuthConfigure');
  41. /**
  42. * ヘルパ
  43. *
  44. * @var array
  45. * @access public
  46. */
  47. var $helpers = array(BC_FORM_HELPER);
  48. /**
  49. * サブメニュー
  50. *
  51. * @var type
  52. * @access public
  53. */
  54. var $subMenuElements = array('tools');
  55. /**
  56. * ぱんくずナビ
  57. *
  58. * @var array
  59. * @access public
  60. */
  61. var $crumbs = array(
  62. array('name' => 'システム設定', 'url' => array('controller' => 'site_configs', 'action' => 'form'))
  63. );
  64. /**
  65. * データメンテナンス
  66. *
  67. * @param string $mode
  68. * @return void
  69. * @access public
  70. */
  71. function admin_maintenance($mode='') {
  72. switch($mode) {
  73. case 'backup':
  74. set_time_limit (0);
  75. $this->_backupDb();
  76. break;
  77. case 'restore':
  78. set_time_limit (0);
  79. if(!$this->data) {
  80. $this->notFound();
  81. }
  82. $messages = array();
  83. if($this->_restoreDb($this->data)) {
  84. $messages[] = 'データの復元が完了しました。';
  85. } else {
  86. $messages[] = 'データの復元に失敗しました。';
  87. }
  88. if(!$this->Page->createAllPageTemplate()){
  89. $messages[] = 'ページテンプレートの生成に失敗しました。<br />表示できないページはページ管理より更新処理を行ってください。';
  90. }
  91. if($messages) {
  92. $this->Session->setFlash(implode('<br />', $messages));
  93. }
  94. $this->redirect(array('action' => 'maintenance'));
  95. break;
  96. }
  97. $this->pageTitle = 'データメンテナンス';
  98. $this->subMenuElements = array('site_configs');
  99. $this->help = 'tools_maintenance';
  100. }
  101. /**
  102. * バックアップファイルを復元する
  103. *
  104. * @param array $data
  105. * @return boolean
  106. * @access protected
  107. */
  108. function _restoreDb($data){
  109. if(empty($data['Tool']['backup']['tmp_name'])){
  110. return false;
  111. }
  112. $tmpPath = TMP.'schemas'.DS;
  113. $targetPath = $tmpPath.$data['Tool']['backup']['name'];
  114. if(!move_uploaded_file($data['Tool']['backup']['tmp_name'], $targetPath)) {
  115. return false;
  116. }
  117. /* ZIPファイルを解凍する */
  118. App::import('Vendor', 'Simplezip');
  119. $Simplezip = new Simplezip();
  120. if(!$Simplezip->unzip($targetPath, $tmpPath)){
  121. return false;
  122. }
  123. @unlink($targetPath);
  124. if(!$this->_loadBackup($tmpPath.'baser'.DS, 'baser')) {
  125. return false;
  126. }
  127. if(!$this->_loadBackup($tmpPath.'plugin'.DS, 'plugin')) {
  128. return false;
  129. }
  130. $this->_resetTmpSchemaFolder();
  131. clearAllCache();
  132. return true;
  133. }
  134. /**
  135. * データベースをレストア
  136. *
  137. * @param string $path スキーマファイルのパス
  138. * @param string $configKeyName DB接続名
  139. * @return boolean
  140. * @access protected
  141. */
  142. function _loadBackup($path, $configKeyName) {
  143. $Folder = new Folder($path);
  144. $files = $Folder->read(true, true);
  145. if(!is_array($files[1])){
  146. return false;
  147. }
  148. $db =& ConnectionManager::getDataSource($configKeyName);
  149. /* テーブルを削除する */
  150. foreach($files[1] as $file) {
  151. if(preg_match("/\.php$/", $file)) {
  152. if(!$db->loadSchema(array('type'=>'drop','path' => $path, 'file'=> $file))){
  153. continue;
  154. }
  155. }
  156. }
  157. /* テーブルを読み込む */
  158. foreach($files[1] as $file) {
  159. if(preg_match("/\.php$/", $file)) {
  160. if(!$db->loadSchema(array('type'=>'create','path' => $path, 'file'=> $file))){
  161. return false;
  162. }
  163. }
  164. }
  165. /* CSVファイルを読み込む */
  166. foreach($files[1] as $file) {
  167. if(preg_match("/\.csv$/", $file)) {
  168. if(!$db->loadCsv(array('path' => $path.$file, 'encoding' => 'SJIS'))){
  169. return false;
  170. }
  171. }
  172. }
  173. return true;
  174. }
  175. /**
  176. * バックアップデータを作成する
  177. *
  178. * @return void
  179. * @access protected
  180. */
  181. function _backupDb() {
  182. $tmpDir = TMP . 'schemas' . DS;
  183. $version = str_replace(' ', '_', $this->getBaserVersion());
  184. $this->_resetTmpSchemaFolder();
  185. $this->_writeBackup('baser', $tmpDir.'baser'.DS);
  186. $this->_writeBackup('plugin', $tmpDir.'plugin'.DS);
  187. // ZIP圧縮して出力
  188. $fileName = 'baserbackup_'.$version.'_'.date('Ymd_His');
  189. App::import('Vendor','Simplezip');
  190. $Simplezip = new Simplezip();
  191. $Simplezip->addFolder($tmpDir);
  192. $Simplezip->download($fileName);
  193. $this->_resetTmpSchemaFolder();
  194. exit();
  195. }
  196. /**
  197. * バックアップファイルを書きだす
  198. *
  199. * @param string $configKeyName
  200. * @param string $path
  201. * @return boolean
  202. * @access protected
  203. */
  204. function _writeBackup($configKeyName, $path) {
  205. $db =& ConnectionManager::getDataSource($configKeyName);
  206. $db->cacheSources = false;
  207. $tables = $db->listSources();
  208. foreach($tables as $table) {
  209. if(preg_match("/^".$db->config['prefix']."([^_].+)$/", $table, $matches) &&
  210. !preg_match("/^".Configure::read('BcEnv.pluginDbPrefix')."[^_].+$/", $matches[1])) {
  211. $table = $matches[1];
  212. $model = Inflector::classify(Inflector::singularize($table));
  213. if(!$db->writeSchema(array('path'=>$path, 'model'=>$model))){
  214. return false;
  215. }
  216. if(!$db->writeCsv(array('path'=>$path.$table.'.csv', 'encoding'=>'SJIS'))) {
  217. return false;
  218. }
  219. }
  220. }
  221. return true;
  222. }
  223. /**
  224. * モデル名からスキーマファイルを生成する
  225. *
  226. * @return void
  227. * @access public
  228. */
  229. function admin_write_schema() {
  230. $path = TMP.'schemas'.DS;
  231. if(!$this->data) {
  232. $this->data['Tool']['connection'] = 'baser';
  233. } else {
  234. if(empty($this->data['Tool'])) {
  235. $this->Session->setFlash('テーブルを選択してください。');
  236. }else {
  237. if(!$this->_resetTmpSchemaFolder()){
  238. $this->Session->setFlash('フォルダ:'.$path.' が存在するか確認し、存在する場合は、削除するか書込権限を与えてください。');
  239. $this->redirect(array('action' => 'write_schema'));
  240. }
  241. if($this->Tool->writeSchema($this->data, $path)) {
  242. App::import('Vendor','Simplezip');
  243. $Simplezip = new Simplezip();
  244. $Simplezip->addFolder($path);
  245. $Simplezip->download('schemas');
  246. exit();
  247. }else {
  248. $this->Session->setFlash('スキーマファイルの生成に失敗しました。');
  249. }
  250. }
  251. }
  252. /* 表示設定 */
  253. $this->pageTitle = 'スキーマファイル生成';
  254. $this->help = 'tools_write_schema';
  255. }
  256. /**
  257. * スキーマファイルを読み込みテーブルを生成する
  258. *
  259. * @return void
  260. * @access public
  261. */
  262. function admin_load_schema() {
  263. if(!$this->data) {
  264. $this->data['Tool']['schema_type'] = 'create';
  265. } else {
  266. if(is_uploaded_file($this->data['Tool']['schema_file']['tmp_name'])) {
  267. $path = TMP.'schemas'.DS;
  268. if(!$this->_resetTmpSchemaFolder()){
  269. $this->Session->setFlash('フォルダ:'.$path.' が存在するか確認し、存在する場合は、削除するか書込権限を与えてください。');
  270. $this->redirect(array('action' => 'load_schema'));
  271. }
  272. if($this->Tool->loadSchema($this->data, $path)) {
  273. $this->Session->setFlash('スキーマファイルの読み込みに成功しました。');
  274. $this->redirect(array('action' => 'load_schema'));
  275. } else {
  276. $this->Session->setFlash('スキーマファイルの読み込みに失敗しました。');
  277. }
  278. }else {
  279. $this->Session->setFlash('ファイルアップロードに失敗しました。');
  280. }
  281. }
  282. /* 表示設定 */
  283. $this->pageTitle = 'スキーマファイル読込';
  284. $this->help = 'tools_load_schema';
  285. }
  286. /**
  287. * スキーマ用の一時フォルダをリセットする
  288. *
  289. * @return boolean
  290. * @access protected
  291. */
  292. function _resetTmpSchemaFolder() {
  293. $path = TMP.'schemas'.DS;
  294. return emptyFolder($path);
  295. }
  296. }
  297. ?>