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