PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/baser/controllers/theme_files_controller.php

https://github.com/hashing/basercms
PHP | 948 lines | 657 code | 106 blank | 185 comment | 123 complexity | 6532f47b5d4ff99a7fda135df1a9e55a 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. class ThemeFilesController extends AppController {
  21. /**
  22. * クラス名
  23. * @var string
  24. * @access public
  25. */
  26. var $name = 'ThemeFiles';
  27. /**
  28. * モデル
  29. *
  30. * @var array
  31. * @access public
  32. */
  33. var $uses = array('ThemeFile', 'ThemeFolder');
  34. /**
  35. * ヘルパー
  36. *
  37. * @var array
  38. * @access public
  39. */
  40. var $helpers = array(BC_FORM_HELPER, BC_CKEDITOR_HELPER);
  41. /**
  42. * テーマファイルタイプ
  43. *
  44. * @var array
  45. * @public protected
  46. */
  47. var $_tempalteTypes = array('layouts'=>'レイアウトテンプレート',
  48. 'elements'=>'エレメントテンプレート',
  49. 'etc'=>'コンテンツテンプレート',
  50. 'css'=>'スタイルシート',
  51. 'js'=>'Javascript',
  52. 'img'=>'イメージ');
  53. /**
  54. * コンポーネント
  55. *
  56. * @var array
  57. * @access public
  58. */
  59. var $components = array('BcAuth','Cookie','BcAuthConfigure');
  60. /**
  61. * ぱんくずナビ
  62. *
  63. * @var array
  64. * @access public
  65. */
  66. var $crumbs = array(
  67. array('name' => 'テーマ管理', 'url' => array('admin' => true, 'controller' => 'themes', 'action' => 'index'))
  68. );
  69. /**
  70. * テーマファイル一覧
  71. *
  72. * @return void
  73. * @access public
  74. */
  75. function admin_index() {
  76. $args = $this->_parseArgs(func_get_args());
  77. extract($args);
  78. if(!$theme){
  79. $this->notFound();
  80. }
  81. // タイトル設定
  82. $pageTitle = Inflector::camelize($theme);
  83. if($plugin) {
  84. $pageTitle .= ':'.Inflector::camelize($plugin);
  85. }
  86. $this->pageTitle = '['.$pageTitle.'] ';
  87. if(!empty($this->_tempalteTypes[$type])) {
  88. $this->pageTitle .= $this->_tempalteTypes[$type].' 一覧';
  89. }
  90. if($type!='etc') {
  91. /* レイアウト/エレメント */
  92. $folder = new Folder($fullpath);
  93. $files = $folder->read(true,true);
  94. $themeFiles = array();
  95. $folders = array();
  96. $excludeList = array('_notes');
  97. foreach($files[0] as $file) {
  98. if(!in_array($file, $excludeList)) {
  99. $folder = array();
  100. $folder['name'] = $file;
  101. $folder['type'] = 'folder';
  102. $folders[] = $folder;
  103. }
  104. }
  105. foreach($files[1] as $file) {
  106. $themeFile = array();
  107. $themeFile['name'] = $file;
  108. $themeFile['type'] = $this->_getFileType($file);
  109. $themeFiles[] = $themeFile;
  110. }
  111. $themeFiles = am($folders,$themeFiles);
  112. }else {
  113. /* その他テンプレート */
  114. $folder = new Folder($fullpath);
  115. $files = $folder->read(true,true);
  116. $themeFiles = array();
  117. $folders = array();
  118. if(!$path) {
  119. $excludeList = array('css','elements','img','layouts','pages','_notes','helpers','js');
  120. } else {
  121. $excludeList = array();
  122. }
  123. foreach($files[0] as $file) {
  124. if(!in_array($file, $excludeList)) {
  125. $folder = array();
  126. $folder['name'] = $file;
  127. $folder['type'] = 'folder';
  128. $folders[] = $folder;
  129. }
  130. }
  131. foreach($files[1] as $file) {
  132. if($file=='screenshot.png') {
  133. continue;
  134. }
  135. $themeFile = array();
  136. $themeFile['name'] = $file;
  137. $themeFile['type'] = $this->_getFileType($file);
  138. $themeFiles[] = $themeFile;
  139. }
  140. $themeFiles = am($folders,$themeFiles);
  141. }
  142. $currentPath = str_replace(ROOT, '', $fullpath);
  143. $this->subMenuElements = array('theme_files');
  144. $this->set('themeFiles',$themeFiles);
  145. $this->set('currentPath',$currentPath);
  146. $this->set('fullpath',$fullpath);
  147. $this->set('path',$path);
  148. $this->set('theme',$theme);
  149. $this->set('plugin',$plugin);
  150. $this->set('type',$type);
  151. $this->help = 'theme_files_index';
  152. }
  153. /**
  154. * ファイルタイプを取得する
  155. *
  156. * @param string $file
  157. * @return mixed false / type
  158. */
  159. function _getFileType($file) {
  160. if(preg_match('/^(.+?)(\.ctp|\.php|\.css|\.js)$/is',$file)) {
  161. return 'text';
  162. } elseif(preg_match('/^(.+?)(\.png|\.gif|\.jpg)$/is',$file)) {
  163. return 'image';
  164. } else {
  165. return 'file';
  166. }
  167. return false;
  168. }
  169. /**
  170. * テーマファイル作成
  171. *
  172. * @return void
  173. * @access public
  174. */
  175. function admin_add() {
  176. $args = $this->_parseArgs(func_get_args());
  177. extract($args);
  178. if(!isset($this->_tempalteTypes[$type])) {
  179. $this->notFound();
  180. }
  181. if (!$this->data) {
  182. if($type=='css' || $type == 'js') {
  183. $ext = $type;
  184. }else {
  185. $ext = 'php';
  186. }
  187. $this->data['ThemeFile']['ext'] = $ext;
  188. $this->data['ThemeFile']['parent'] = $fullpath;
  189. } else {
  190. $this->ThemeFile->set($this->data);
  191. if($this->ThemeFile->validates()) {
  192. $fullpath = $fullpath.$this->data['ThemeFile']['name'].'.'.$this->data['ThemeFile']['ext'];
  193. if(!is_dir(dirname($fullpath))) {
  194. $folder = new Folder();
  195. $folder->create(dirname($fullpath),0777);
  196. }
  197. $file = new File($fullpath);
  198. if($file->open('w')) {
  199. $file->append($this->data['ThemeFile']['contents']);
  200. $file->close();
  201. unset($file);
  202. $result = true;
  203. }else {
  204. $result = false;
  205. }
  206. } else {
  207. $result = false;
  208. }
  209. if ($result) {
  210. $this->Session->setFlash('ファイル ' .basename($fullpath). ' を作成しました。');
  211. $this->redirect(array('action' => 'edit', $theme, $type, $path, $this->data['ThemeFile']['name'].'.'.$this->data['ThemeFile']['ext']));
  212. } else {
  213. $this->Session->setFlash('ファイル ' .basename($fullpath). ' の作成に失敗しました。');
  214. }
  215. }
  216. $this->pageTitle = '['.Inflector::camelize($theme).'] '.$this->_tempalteTypes[$type].' 作成';
  217. $this->crumbs[] = array('name' => $this->_tempalteTypes[$type], 'url' => array('controller' => 'theme_files', 'action' => 'index', $theme, $type));
  218. $this->subMenuElements = array('theme_files');
  219. $this->set('currentPath', str_replace(ROOT, '', $fullpath));
  220. $this->set('theme',$theme);
  221. $this->set('plugin',$plugin);
  222. $this->set('type', $type);
  223. $this->set('path', $path);
  224. $this->help = 'theme_files_form';
  225. $this->render('form');
  226. }
  227. /**
  228. * テーマファイル編集
  229. *
  230. * @return void
  231. * @access public
  232. */
  233. function admin_edit() {
  234. $args = $this->_parseArgs(func_get_args());
  235. extract($args);
  236. if(!isset($this->_tempalteTypes[$type])) {
  237. $this->notFound();
  238. }
  239. $filename = urldecode(basename($path));
  240. if (!$this->data) {
  241. $file = new File($fullpath);
  242. $pathinfo = pathinfo($fullpath);
  243. $this->data['ThemeFile']['name'] = urldecode(basename($file->name,'.'.$pathinfo['extension']));
  244. $this->data['ThemeFile']['type'] = $this->_getFileType(urldecode(basename($file->name)));
  245. $this->data['ThemeFile']['ext'] = $pathinfo['extension'];
  246. if($this->data['ThemeFile']['type'] == 'text') {
  247. $this->data['ThemeFile']['contents'] = $file->read();
  248. }
  249. } else {
  250. $this->ThemeFile->set($this->data);
  251. if($this->ThemeFile->validates()) {
  252. $oldPath = urldecode($fullpath);
  253. $newPath = dirname($fullpath).DS.urldecode($this->data['ThemeFile']['name']).'.'.$this->data['ThemeFile']['ext'];
  254. $this->data['ThemeFile']['type'] = $this->_getFileType(basename($newPath));
  255. if($this->data['ThemeFile']['type'] == 'text') {
  256. $file = new File($oldPath);
  257. if($file->open('w')) {
  258. $file->append($this->data['ThemeFile']['contents']);
  259. $file->close();
  260. unset($file);
  261. $result = true;
  262. }else {
  263. $result = false;
  264. }
  265. }else {
  266. $result = true;
  267. }
  268. if($oldPath != $newPath) {
  269. rename($oldPath, $newPath);
  270. }
  271. } else {
  272. $result = false;
  273. }
  274. if ($result) {
  275. $this->Session->setFlash('ファイル ' .$filename. ' を更新しました。');
  276. $this->redirect(array($theme, $plugin, $type, dirname($path), basename($newPath)));
  277. } else {
  278. $this->Session->setFlash('ファイル ' .$filename. ' の更新に失敗しました。');
  279. }
  280. }
  281. $this->pageTitle = '['.Inflector::camelize($theme).'] '.$this->_tempalteTypes[$type].' 編集: '.$filename;
  282. $this->crumbs[] = array('name' => $this->_tempalteTypes[$type], 'url' => array('controller' => 'theme_files', 'action' => 'index', $theme, $type));
  283. $this->subMenuElements = array('theme_files');
  284. $this->set('currentPath', str_replace(ROOT, '', dirname($fullpath)).DS);
  285. $this->set('theme',$theme);
  286. $this->set('plugin',$plugin);
  287. $this->set('type', $type);
  288. $this->set('path', $path);
  289. $this->help = 'theme_files_form';
  290. $this->render('form');
  291. }
  292. /**
  293. * ファイルを削除する
  294. *
  295. * @return void
  296. * @access public
  297. */
  298. function admin_del () {
  299. $args = $this->_parseArgs(func_get_args());
  300. extract($args);
  301. if(!isset($this->_tempalteTypes[$type])) {
  302. $this->notFound();
  303. }
  304. if(is_dir($fullpath)) {
  305. $folder = new Folder();
  306. $result = $folder->delete($fullpath);
  307. $target = 'フォルダ';
  308. }else {
  309. $result = @unlink($fullpath);
  310. $target = 'ファイル';
  311. }
  312. if ($result) {
  313. $this->Session->setFlash($target .' '.$path .' を削除しました。');
  314. } else {
  315. $this->Session->setFlash($target .' '.$path .' の削除に失敗しました。');
  316. }
  317. $this->redirect(array('action' => 'index', $theme, $type, dirname($path)));
  318. }
  319. /**
  320. * ファイルを削除する (ajax)
  321. *
  322. * @return void
  323. * @access public
  324. */
  325. function admin_ajax_del () {
  326. $args = $this->_parseArgs(func_get_args());
  327. if(!$args) {
  328. $this->ajaxError(500, '無効な処理です。');
  329. }
  330. extract($args);
  331. if(!isset($this->_tempalteTypes[$type])) {
  332. $this->ajaxError(500, '無効な処理です。');
  333. }
  334. if($this->_del($args)){
  335. exit(true);
  336. }else{
  337. exit();
  338. }
  339. }
  340. /**
  341. * 削除
  342. *
  343. * @return void
  344. * @access public
  345. */
  346. function _del($args) {
  347. extract($args);
  348. if(is_dir($fullpath)) {
  349. $folder = new Folder();
  350. $result = $folder->delete($fullpath);
  351. $target = 'フォルダ';
  352. }else {
  353. $result = @unlink($fullpath);
  354. $target = 'ファイル';
  355. }
  356. if ($result) {
  357. $this->ThemeFile->saveDblog($target .' '.$path .' を削除しました。');
  358. return true;
  359. } else {
  360. return false;
  361. }
  362. }
  363. /**
  364. * 一括削除
  365. *
  366. * @return void
  367. * @access public
  368. */
  369. function _batch_del($ids) {
  370. if($ids) {
  371. $result = true;
  372. foreach($ids as $id) {
  373. $args = $this->params['pass'];
  374. $args[] = $id;
  375. $args = $this->_parseArgs($args);
  376. extract($args);
  377. if(!isset($this->_tempalteTypes[$type])) {
  378. exit();
  379. }
  380. if(is_dir($fullpath)) {
  381. $folder = new Folder();
  382. $result = $folder->delete($fullpath);
  383. $target = 'フォルダ';
  384. }else {
  385. $result = @unlink($fullpath);
  386. $target = 'ファイル';
  387. }
  388. if ($result) {
  389. $this->ThemeFile->saveDblog($target .' '.$path .' を削除しました。');
  390. } else {
  391. $result = false;
  392. }
  393. }
  394. }
  395. return true;
  396. }
  397. /**
  398. * テーマファイル表示
  399. *
  400. * @return void
  401. * @access public
  402. */
  403. function admin_view() {
  404. $args = $this->_parseArgs(func_get_args());
  405. extract($args);
  406. if(!isset($this->_tempalteTypes[$type])) {
  407. $this->notFound();
  408. }
  409. $pathinfo = pathinfo($fullpath);
  410. $file = new File($fullpath);
  411. $this->data['ThemeFile']['name'] = basename($file->name,'.'.$pathinfo['extension']);
  412. $this->data['ThemeFile']['ext'] = $pathinfo['extension'];
  413. $this->data['ThemeFile']['contents'] = $file->read();
  414. $this->data['ThemeFile']['type'] = $this->_getFileType($file->name);
  415. $pageTitle = Inflector::camelize($theme);
  416. if($plugin) {
  417. $pageTitle .= ':'.Inflector::camelize($plugin);
  418. }
  419. $this->pageTitle = '['.$pageTitle.'] '.$this->_tempalteTypes[$type].' 表示: '.basename($path);
  420. $this->crumbs[] = array('name' => $this->_tempalteTypes[$type], 'url' => array('controller' => 'theme_files', 'action' => 'index', $theme, $type));
  421. $this->subMenuElements = array('theme_files');
  422. $this->set('currentPath', str_replace(ROOT, '', dirname($fullpath)).'/');
  423. $this->set('theme',$theme);
  424. $this->set('plugin',$plugin);
  425. $this->set('type', $type);
  426. $this->set('path', $path);
  427. $this->render('form');
  428. }
  429. /**
  430. * テーマファイルをコピーする
  431. *
  432. * @return void
  433. * @access public
  434. */
  435. function admin_ajax_copy() {
  436. $args = $this->_parseArgs(func_get_args());
  437. if(!$args) {
  438. $this->ajaxError(500, '無効な処理です。');
  439. }
  440. extract($args);
  441. if(!isset($this->_tempalteTypes[$type])) {
  442. $this->ajaxError(500, '無効な処理です。');
  443. }
  444. $themeFile = array();
  445. if(is_dir($fullpath)) {
  446. $newPath = preg_replace('/\/$/is', '', $fullpath).'_copy';
  447. while(true) {
  448. if(!is_dir($newPath)) {
  449. break;
  450. }
  451. $newPath .= '_copy';
  452. }
  453. $folder = new Folder();
  454. $result = $folder->copy(array('from'=>$fullpath,'to'=>$newPath,'chmod'=>0777,'skip'=>array('_notes')));
  455. $folder = null;
  456. $target = 'フォルダ';
  457. $themeFile['name'] = basename(urldecode($newPath));
  458. $themeFile['type'] = 'folder';
  459. } else {
  460. $pathinfo = pathinfo($fullpath);
  461. $newPath = $pathinfo['dirname'].DS.urldecode(basename($fullpath,'.'.$pathinfo['extension'])).'_copy';
  462. while(true) {
  463. if(!file_exists($newPath.'.'.$pathinfo['extension'])) {
  464. $newPath .= '.'.$pathinfo['extension'];
  465. break;
  466. }
  467. $newPath .= '_copy';
  468. }
  469. $result = @copy(urldecode($fullpath),$newPath);
  470. if($result) {
  471. chmod($newPath, 0666);
  472. }
  473. $target = 'ファイル';
  474. $themeFile['name'] = basename(urldecode($newPath));
  475. $themeFile['type'] = $this->_getFileType($themeFile['name'] );
  476. }
  477. if($result) {
  478. $this->ThemeFile->saveDblog($target.' '.urldecode($path) .' をコピーしました。');
  479. $this->set('fullpath',$fullpath);
  480. $this->set('path',dirname($path));
  481. $this->set('theme',$theme);
  482. $this->set('plugin',$plugin);
  483. $this->set('type',$type);
  484. $this->set('data', $themeFile);
  485. }else {
  486. $this->ThemeFile->saveDblog($target.' '.urldecode($path) .' のコピーに失敗しました。');
  487. $this->ajaxError(500, '上位フォルダのアクセス権限を見直してください。');
  488. }
  489. }
  490. /**
  491. * ファイルをアップロードする
  492. *
  493. * @return void
  494. * @access public
  495. */
  496. function admin_upload() {
  497. if(!$this->data) {
  498. $this->notFound();
  499. }
  500. $args = $this->_parseArgs(func_get_args());
  501. extract($args);
  502. if(!isset($this->_tempalteTypes[$type])) {
  503. $this->notFound();
  504. }
  505. $pathinfo = pathinfo($this->data['ThemeFile']['file']['name']);
  506. $ext = $pathinfo['extension'];
  507. //if(in_array($ext, array('ctp', 'css', 'js', 'png', 'gif', 'jpg'))) {
  508. $filePath = $fullpath .DS. $this->data['ThemeFile']['file']['name'];
  509. $Folder = new Folder();
  510. $Folder->create(dirname($filePath), 0777);
  511. if(@move_uploaded_file($this->data['ThemeFile']['file']['tmp_name'], $filePath)) {
  512. $this->Session->setFlash('アップロードに成功しました。');
  513. }else {
  514. $this->Session->setFlash('アップロードに失敗しました。');
  515. }
  516. //} else {
  517. //$this->Session->setFlash('アップロードに失敗しました。<br />アップロードできるファイルは、拡張子が、ctp / css / js / png / gif / jpg のファイルのみです。');
  518. //}
  519. $this->redirect(array('action' => 'index', $theme, $type, $path));
  520. }
  521. /**
  522. * フォルダ追加
  523. *
  524. * @return void
  525. * @access public
  526. */
  527. function admin_add_folder() {
  528. $args = $this->_parseArgs(func_get_args());
  529. extract($args);
  530. if(!isset($this->_tempalteTypes[$type])) {
  531. $this->notFound();
  532. }
  533. if (!$this->data) {
  534. $this->data['ThemeFolder']['parent'] = dirname($fullpath);
  535. } else {
  536. $folder = new Folder();
  537. $this->ThemeFolder->set($this->data);
  538. if ($this->ThemeFolder->validates() && $folder->create($fullpath.$this->data['ThemeFolder']['name'], 0777)) {
  539. $this->Session->setFlash('フォルダ '.$this->data['ThemeFolder']['name'].' を作成しました。');
  540. $this->redirect(array('action' => 'index', $theme, $type, $path));
  541. } else {
  542. $this->Session->setFlash('フォルダの作成に失敗しました。');
  543. }
  544. }
  545. $this->crumbs[] = array('name' => $this->_tempalteTypes[$type], 'url' => array('controller' => 'theme_files', 'action' => 'index', $theme, $type));
  546. $this->pageTitle = '['.$theme.'] フォルダ作成: '.$path;
  547. $this->subMenuElements = array('theme_files');
  548. $this->set('currentPath', str_replace(ROOT, '', $fullpath));
  549. $this->set('theme',$theme);
  550. $this->set('plugin',$plugin);
  551. $this->set('type',$type);
  552. $this->set('path',$path);
  553. $this->help = 'theme_files_form_folder';
  554. $this->render('form_folder');
  555. }
  556. /**
  557. * フォルダ編集
  558. *
  559. * @return void
  560. * @access public
  561. */
  562. function admin_edit_folder() {
  563. $args = $this->_parseArgs(func_get_args());
  564. extract($args);
  565. if(!isset($this->_tempalteTypes[$type])) {
  566. $this->notFound();
  567. }
  568. if (!$this->data) {
  569. $this->data['ThemeFolder']['name'] = basename($path);
  570. $this->data['ThemeFolder']['parent'] = dirname($fullpath);
  571. $this->data['ThemeFolder']['pastname'] = basename($path);
  572. } else {
  573. $newPath = dirname($fullpath).DS.$this->data['ThemeFolder']['name'].DS;
  574. $folder = new Folder();
  575. $this->ThemeFolder->set($this->data);
  576. if ($this->ThemeFolder->validates()) {
  577. if($fullpath != $newPath) {
  578. if($folder->move(array('from'=>$fullpath,'to'=>$newPath,'chmod'=>0777,'skip'=>array('_notes')))) {
  579. $this->Session->setFlash('フォルダ名を '.$this->data['ThemeFolder']['name'].' に変更しました。');
  580. $this->redirect(array('action' => 'index', $theme, $type, dirname($path)));
  581. }else {
  582. $this->Session->setFlash('フォルダ名の変更に失敗しました。');
  583. }
  584. }else {
  585. $this->Session->setFlash('フォルダ名に変更はありませんでした。');
  586. $this->redirect(array('action' => 'index', $theme, $type, dirname($path)));
  587. }
  588. } else {
  589. $this->Session->setFlash('フォルダ名の変更に失敗しました。');
  590. }
  591. }
  592. $pageTitle = Inflector::camelize($theme);
  593. $this->pageTitle = '['.$pageTitle.'] フォルダ表示: '.basename($path);
  594. $this->crumbs[] = array('name' => $this->_tempalteTypes[$type], 'url' => array('controller' => 'theme_files', 'action' => 'index', $theme, $type));
  595. $this->subMenuElements = array('theme_files');
  596. $this->set('currentPath', str_replace(ROOT, '', dirname($fullpath)).'/');
  597. $this->set('theme',$theme);
  598. $this->set('plugin',$plugin);
  599. $this->set('type',$type);
  600. $this->set('path',$path);
  601. $this->help = 'theme_files_form_folder';
  602. $this->render('form_folder');
  603. }
  604. /**
  605. * フォルダ表示
  606. *
  607. * @return void
  608. * @access public
  609. */
  610. function admin_view_folder() {
  611. $args = $this->_parseArgs(func_get_args());
  612. extract($args);
  613. if(!isset($this->_tempalteTypes[$type])) {
  614. $this->notFound();
  615. }
  616. $this->data['ThemeFolder']['name'] = basename($path);
  617. $this->data['ThemeFolder']['parent'] = dirname($fullpath);
  618. $this->data['ThemeFolder']['pastname'] = basename($path);
  619. $pageTitle = Inflector::camelize($theme);
  620. if($plugin) {
  621. $pageTitle .= ':'.Inflector::camelize($plugin);
  622. }
  623. $this->pageTitle = '['.$pageTitle.'] フォルダ表示: '.basename($path);
  624. $this->crumbs[] = array('name' => $this->_tempalteTypes[$type], 'url' => array('controller' => 'theme_files', 'action' => 'index', $theme, $type));
  625. $this->subMenuElements = array('theme_files');
  626. $this->set('currentPath', str_replace(ROOT, '', dirname($fullpath)).'/');
  627. $this->set('theme',$theme);
  628. $this->set('plugin',$plugin);
  629. $this->set('type',$type);
  630. $this->set('path',$path);
  631. $this->render('form_folder');
  632. }
  633. /**
  634. * 引き数を解析する
  635. *
  636. * @param array $args
  637. * @return array
  638. * @access protected
  639. */
  640. function _parseArgs($args) {
  641. $data = array('plugin' => '', 'theme' => '', 'type' => '', 'path' => '', 'fullpath' => '', 'assets' => false);
  642. $assets = array('css', 'js', 'img');
  643. if(!empty($args[1]) && !isset($this->_tempalteTypes[$args[1]])) {
  644. $folder = new Folder(BASER_PLUGINS);
  645. $files = $folder->read(true,true);
  646. foreach($files[0] as $file) {
  647. if($args[1]==$file) {
  648. $data['plugin'] = $args[1];
  649. unset($args[1]);
  650. break;
  651. }
  652. }
  653. }
  654. if($data['plugin']) {
  655. if(!empty($args[0])) {
  656. $data['theme'] = $args[0];
  657. unset($args[0]);
  658. }
  659. if(!empty($args[2])) {
  660. $data['type'] = $args[2];
  661. unset($args[2]);
  662. }
  663. } else {
  664. if(!empty($args[0])) {
  665. $data['theme'] = $args[0];
  666. unset($args[0]);
  667. }
  668. if(!empty($args[1])) {
  669. $data['type'] = $args[1];
  670. unset($args[1]);
  671. }
  672. }
  673. if(empty($data['type'])) {
  674. $data['type'] = 'layouts';
  675. }
  676. if(!empty($args)) {
  677. $data['path'] = implode('/', $args);
  678. }
  679. if($data['plugin']) {
  680. if(in_array($data['type'],$assets)) {
  681. $data['assets'] = true;
  682. $viewPath = BASER_PLUGINS.$data['plugin'].DS.'vendors'.DS;
  683. }else {
  684. $viewPath = BASER_PLUGINS.$data['plugin'].DS.'views'.DS;
  685. }
  686. }elseif($data['theme'] == 'core') {
  687. if(in_array($data['type'],$assets)) {
  688. $data['assets'] = true;
  689. $viewPath = BASER_VENDORS;
  690. }else {
  691. $viewPath = BASER_VIEWS;
  692. }
  693. }else {
  694. $viewPath = WWW_ROOT.'themed'.DS.$data['theme'].DS;
  695. }
  696. if($data['type']!='etc') {
  697. $data['fullpath'] = $viewPath.$data['type'].DS.$data['path'];
  698. if($data['path']) {
  699. $pathinfo = pathinfo($data['fullpath']);
  700. if(empty($pathinfo['extension'])) {
  701. $data['fullpath'] .= '/';
  702. }
  703. }
  704. }else {
  705. $data['fullpath'] = $viewPath.$data['path'];
  706. }
  707. return $data;
  708. }
  709. /**
  710. * コアファイルを現在のテーマにコピーする
  711. *
  712. * @return void
  713. * @access public
  714. */
  715. function admin_copy_to_theme() {
  716. $args = $this->_parseArgs(func_get_args());
  717. extract($args);
  718. if(!isset($this->_tempalteTypes[$type])) {
  719. $this->notFound();
  720. }
  721. if($type!='etc') {
  722. if($plugin && $assets) {
  723. $themePath = WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.$plugin.DS.$type.DS.$path;
  724. } else {
  725. $themePath = WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.$type.DS.$path;
  726. }
  727. }else {
  728. $themePath = WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.$path;
  729. }
  730. $folder = new Folder();
  731. $folder->create(dirname($themePath),0777);
  732. if(copy($fullpath,$themePath)) {
  733. chmod($themePath,0666);
  734. $_themePath = str_replace(ROOT,'',$themePath);
  735. $this->Session->setFlash('コアファイル '.basename($path).' を テーマ '.Inflector::camelize($this->siteConfigs['theme']).' の次のパスとしてコピーしました。<br />'.$_themePath);
  736. // 現在のテーマにリダイレクトする場合、混乱するおそれがあるのでとりあえずそのまま
  737. //$this->redirect(array('action' => 'edit', $this->siteConfigs['theme'], $type, $path));
  738. }else {
  739. $this->Session->setFlash('コアファイル '.basename($path).' のコピーに失敗しました。');
  740. }
  741. $this->redirect(array('action' => 'view', $theme, $plugin, $type, $path));
  742. }
  743. /**
  744. * コアファイルのフォルダを現在のテーマにコピーする
  745. *
  746. * @return void
  747. * @access public
  748. */
  749. function admin_copy_folder_to_theme() {
  750. $args = $this->_parseArgs(func_get_args());
  751. extract($args);
  752. if(!isset($this->_tempalteTypes[$type])) {
  753. $this->notFound();
  754. }
  755. if($type!='etc') {
  756. if($plugin && $assets) {
  757. $themePath = WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.$plugin.DS.$type.DS;
  758. } else {
  759. $themePath = WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.$type.DS;
  760. }
  761. if($path) {
  762. $themePath .= $path.DS;
  763. }
  764. }else {
  765. $themePath = WWW_ROOT.'themed'.DS.$this->siteConfigs['theme'].DS.$path.DS;
  766. }
  767. $folder = new Folder();
  768. $folder->create(dirname($themePath),0777);
  769. if($folder->copy(array('from'=>$fullpath,'to'=>$themePath,'chmod'=>0777,'skip'=>array('_notes')))) {
  770. $_themePath = str_replace(ROOT,'',$themePath);
  771. $this->Session->setFlash('コアフォルダ '.basename($path).' を テーマ '.Inflector::camelize($this->siteConfigs['theme']).' の次のパスとしてコピーしました。<br />'.$_themePath);
  772. // 現在のテーマにリダイレクトする場合、混乱するおそれがあるのでとりあえずそのまま
  773. //$this->redirect(array('action' => 'edit', $this->siteConfigs['theme'], $type, $path));
  774. }else {
  775. $this->Session->setFlash('コアフォルダ '.basename($path).' のコピーに失敗しました。');
  776. }
  777. $this->redirect(array('action' => 'view_folder', $theme, $plugin, $type, $path));
  778. }
  779. /**
  780. * 画像を表示する
  781. * コアの画像等も表示可
  782. *
  783. * @param array パス情報
  784. * @return void
  785. * @access public
  786. */
  787. function admin_img() {
  788. $args = $this->_parseArgs(func_get_args());
  789. $contents = array('jpg'=>'jpeg','gif'=>'gif','png'=>'png');
  790. extract($args);
  791. $pathinfo = pathinfo($fullpath);
  792. if(!isset($this->_tempalteTypes[$type]) || !isset($contents[$pathinfo['extension']]) || !file_exists($fullpath)) {
  793. $this->notFound();
  794. }
  795. $file = new File($fullpath);
  796. if($file->open('r')) {
  797. header("Content-Length: ".$file->size());
  798. header("Content-type: image/".$contents[$pathinfo['extension']]);
  799. echo $file->read();
  800. exit();
  801. }else {
  802. $this->notFound();
  803. }
  804. }
  805. /**
  806. * 画像を表示する
  807. * コアの画像等も表示可
  808. *
  809. * @param int $width
  810. * @param int $height
  811. * @param array パス情報
  812. * @return void
  813. * @access public
  814. */
  815. function admin_img_thumb() {
  816. $args = func_get_args();
  817. $width = $args[0];
  818. $height = $args[1];
  819. unset($args[0]);
  820. unset($args[1]);
  821. $args = array_values($args);
  822. if($width == 0) {
  823. $width = 100;
  824. }
  825. if($height == 0) {
  826. $height = 100;
  827. }
  828. $args = $this->_parseArgs($args);
  829. $contents = array('jpg'=>'jpeg','gif'=>'gif','png'=>'png');
  830. extract($args);
  831. $pathinfo = pathinfo($fullpath);
  832. if(!isset($this->_tempalteTypes[$type]) || !isset($contents[$pathinfo['extension']]) || !file_exists($fullpath)) {
  833. $this->notFound();
  834. }
  835. header("Content-type: image/".$contents[$pathinfo['extension']]);
  836. App::import('Vendor','Imageresizer');
  837. $Imageresizer = new Imageresizer();
  838. $Imageresizer->resize($fullpath,'',$width,$height);
  839. exit();
  840. }
  841. }
  842. ?>