Added LinuxChix theme
[moodle-linuxchix.git] / theme / chameleon / ui / ChameleonFileBrowser.class.php
blob7b1aca510b3f1fcdd8afef0563bd1103f2f2b7ca
1 <?php
3 class ChameleonFileBrowser {
4 var $root;
5 var $path;
6 var $dir;
7 var $IMAGE_TYPES;
9 var $founddirs = array();
10 var $foundfiles = array();
12 function ChameleonFileBrowser() {
13 $this->IMAGE_TYPES = array('jpeg', 'jpg', 'gif', 'png');
15 $tmp = explode('/', str_replace('\\', '/', __FILE__));
16 array_pop($tmp);
17 array_pop($tmp);
18 $this->root = implode('/', $tmp);
20 $this->path = $this->sanitisepath($_GET['path']);
21 $this->dir = $this->root . '/' . $this->path;
24 function sanitisepath($path) {
25 if ($path == 'root') {
26 return 'pix';
29 if (substr($path, 0, 3) != 'pix') {
30 $this->send('<chameleon_error>Not a valid directory</chameleon_error>');
33 return preg_replace('/[.]+/', '', $path);
36 function isimage($file) {
37 if (strpos($file, '.') === false) {
38 return false;
40 return in_array(array_pop(explode('.', $file)), $this->IMAGE_TYPES);
43 function readfiles() {
44 if (!is_dir($this->dir)) {
45 $this->send('<chameleon_error>Not a valid directory</chameleon_error>');
48 $handle = opendir($this->dir);
49 while (false !== ($file = readdir($handle))) {
50 if ($file == '.' || $file == '..') {
51 continue;
53 if (is_dir($this->dir . '/' . $file)) {
54 $this->founddirs[] = $file;
55 } else if ($this->isimage($file)) {
56 $this->foundfiles[] = $file;
59 closedir($handle);
61 sort($this->founddirs, SORT_STRING);
62 sort($this->foundfiles, SORT_STRING);
63 $this->sendfiles();
66 function sendfiles() {
67 $out = "<files path=\"$this->path\">\n";
68 foreach ($this->founddirs as $file) {
69 $out .= " <file type=\"dir\">$this->path/$file</file>\n";
71 foreach ($this->foundfiles as $file) {
72 $out .= " <file type=\"img\">$this->path/$file</file>\n";
74 $out .= "</files>";
76 $this->send($out);
79 function send($out) {
80 header("Content-type: application/xml; charset=utf-8");
81 die($out);