nextcloud/files/ajax/autocomplete.php

62 lines
1.2 KiB
PHP
Raw Normal View History

2011-05-28 21:31:36 +04:00
<?php
//provide auto completion of paths for use with jquer ui autocomplete
// Init owncloud
require_once('../../lib/base.php');
// We send json data
// header( "Content-Type: application/jsonrequest" );
// Check if we are a user
if( !OC_USER::isLoggedIn()){
echo json_encode( array( "status" => "error", "data" => array( "message" => "Authentication error" )));
exit();
}
// 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));
2011-06-01 16:05:30 +04:00
if($base!='/'){
$query=substr($query,1);
}
2011-05-28 21:31:36 +04:00
$queryLen=strlen($query);
$query=strtolower($query);
2011-05-28 21:31:36 +04:00
// echo "$base - $query";
$files=array();
if(OC_FILESYSTEM::is_dir($base)){
$dh = OC_FILESYSTEM::opendir($base);
if(substr($base,-1,1)!='/'){
$base=$base.'/';
}
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != ".."){
if(substr(strtolower($file),0,$queryLen)==$query){
2011-05-28 21:31:36 +04:00
$item=$base.$file;
if((!$dirOnly or OC_FILESYSTEM::is_dir($item))){
$files[]=(object)array('id'=>$item,'label'=>$item,'name'=>$item);
}
}
}
}
}
echo json_encode($files);
?>