summaryrefslogtreecommitdiffstats
path: root/files/ajax/autocomplete.php
blob: 8d7a5b482bd490903705b1661c586d4339f2ab9b (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
<?php
//provide auto completion of paths for use with jquer ui autocomplete


// Init owncloud
require_once('../../lib/base.php');

OC_JSON::checkLoggedIn();

// Get data
$query = $_GET['term'];
$dirOnly=(isset($_GET['dironly']))?($_GET['dironly']=='true'):false;

if($query[0]!='/'){
	$query='/'.$query;
}

if(substr($query,-1,1)=='/'){
	$base=$query;
}else{
	$base=dirname($query);
}

$query=substr($query,strlen($base));

if($base!='/'){
	$query=substr($query,1);
}
$queryLen=strlen($query);
$query=strtolower($query);

// echo "$base - $query";

$files=array();

if(OC_Filesystem::file_exists($base) and OC_Filesystem::is_dir($base)){
	$dh = OC_Filesystem::opendir($base);
	if($dh){
		if(substr($base,-1,1)!='/'){
			$base=$base.'/';
		}
		while (($file = readdir($dh)) !== false) {
			if ($file != "." && $file != ".."){
				if(substr(strtolower($file),0,$queryLen)==$query){
					$item=$base.$file;
					if((!$dirOnly or OC_Filesystem::is_dir($item))){
						$files[]=(object)array('id'=>$item,'label'=>$item,'name'=>$item);
					}
				}
			}
		}
	}
}
OC_JSON::encodedPrint($files);

?>