summaryrefslogtreecommitdiff
path: root/demos/quickstart/protected/pages/ViewSource.php
blob: 9a8d7d09d369b78431b3364fef003d3225569e93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php

class ViewSource extends TPage
{
	private $_path=null;
	private $_fullPath=null;
	private $_fileType=null;

	public function onLoad($param)
	{
		parent::onLoad($param);
		$path=$this->Request['path'];
		$fullPath=realpath($this->Service->BasePath.'/'.$path);
		$fileExt=$this->getFileExtension($fullPath);
		if($fullPath!==false && is_file($fullPath) && strpos($fullPath,$this->Service->BasePath)!==false)
		{
 			if($this->isFileTypeAllowed($fileExt))
 			{
				$this->_fullPath=strtr($fullPath,'\\','/');
				$this->_path=strtr(substr($fullPath,strlen($this->Service->BasePath)),'\\','/');
 			}
		}
		if($this->_fullPath===null)
			throw new THttpException(500,'File Not Found: %s',$path);

		$this->SourceList->DataSource=$this->SourceFiles;
		$this->SourceList->dataBind();

		$this->Highlighter->Language=$this->getFileLanguage($fileExt);
		$this->SourceView->Text=file_get_contents($this->_fullPath);
	}

	public function getFilePath()
	{
		return $this->_path;
	}

	protected function getSourceFiles()
	{
		$list=array();
		$basePath=dirname($this->_fullPath);
		if($dh=opendir($basePath))
		{
			while(($file=readdir($dh))!==false)
			{
				if(is_file($basePath.'/'.$file))
				{
					$extension=$this->getFileExtension($basePath.'/'.$file);
					if($this->isFileTypeAllowed($extension))
					{
						$fileType=$this->getFileType($extension);
						$list[]=array(
							'name'=>$file,
							'type'=>$fileType,
							'active'=>basename($this->_fullPath)===$file,
							'url'=>'?page=ViewSource&amp;path=/'.ltrim(strtr(dirname($this->_path),'\\','/').'/'.$file,'/')
						);
					}
				}

			}
			closedir($dh);
		}
		foreach($list as $item)
			$aux[]=$item['name'];
		array_multisort($aux, SORT_ASC, $list);
		return $list;
	}

	protected function isFileTypeAllowed($extension)
	{
		return in_array($extension,array('tpl','page','php','html'));
	}

	protected function getFileExtension($fileName)
	{
		if(($pos=strrpos($fileName,'.'))===false)
			return '';
		else
			return substr($fileName,$pos+1);
	}

	protected function getFileType($extension)
	{
		if($extension==='tpl' || $extension==='page')
			return 'Template file';
		else
			return 'Class file';
	}

	protected function getFileLanguage($extension)
	{
		switch($extension)
		{
			case 'page' :
			case 'tpl' :
				return 'prado';
			case 'php' :
				return 'php';
				break;
			case 'xml' :
				return 'xml';
				break;
			default :
				return 'html';
		}
	}
}

?>