Scripts
iDisk Connectie class
Met deze class kan je bestanden uploaden, downloaden, verplaatsen, kopieren en deleten van je iDisk. Je kan ook een lijst met bestanden in de mappen krijgen. Het kan verbinding maken met zowel de public folder als met de volledige idisk, met of zonder wachtwoord. Voor degenen die niet weten wat iDisk is of er meer informatie willen over hebben, ga even naar http://www.apple.com/benl/dotmac/idisk.html. Een 60-dagen trial is beschikbaar.
idisk-connectie-class
<?php // voorbeeldcode
try {
$results = array();
$idisk = new iDisk('Hipska',null, true);
$idisk->debug();
$results['getFile'] = $idisk->getFile('Screenshots/programma.jpg');
$results['putFile'] = $idisk->putFile('Screenshots/test2.php',file_get_contents('test.php'));
$results['move'] = $idisk->move('Screenshots/test2.php','/test.php',true);
$results['copy'] = $idisk->copy('test.php','test2.php',true);
$results['delete'] = $idisk->delete('test2.php');
$results['readDir'] = $idisk->readDir('/');
}catch (Exception $e){
echo '<pre>'. $e->getCode() .' '. $e->getMessage() .'</pre>'.EOL;
}
?>
[code]<?php
/**
* class iDisk
*
* Maak snel en eenvoudig verbinding met je iDisk
* Uploaden/Downloaden/Kopiƫren/Verplaatsen/Verwijderen
*
* @author Hipska
* @version 1.0
* @link http://www.hipska.be.tc
*/
class iDisk {
private $host = 'idisk.mac.com';
private $user = '';
private $pass = '';
private $dir = '/';
private $debug = false;
private $log = array();
private $request = array();
private $res;
/**
* iDisk::__construct()
*
* Maak een verbinding met een iDisk
*
* @param string $username: de gebruikersnaam van de iDisk waarmee je wil verbinden
* @param string $password: het wachtwoord van de iDisk
* @param boolean $public: maak je verbinding met de public folder of niet?
* @access public
* @author Hipska
*/
public function __construct( $username, $password = null, $public = false ){
if(!defined('EOL')) define('EOL',"\r\n"); // maak een End Of Line
$this->dir .= ($public)? $username.'-Public' : strtolower($username); // stel de map in
$this->user = ($public)? 'public' : strtolower($username);
$this->pass = $password;
$this->init(); // maak verbinding met de iDisk
}
/**
* iDisk::debug()
*
* Zet het debuggen aan of uit (enkel in testomgevingen)
*
* @param boolean $debug: zet debug mode aan of uit
* @access public
* @author Hipska
*/
public function debug($debug = true){
$this->debug = $debug;
if($debug){
@set_time_limit(15); // maak dat het systeem niet blijft hangen
$this->log('Debug mode ON');
}
}
/**
* iDisk::readDir()
*
* Toont alle bestanden/mappen en hun eigenschappen van een map
*
* @param string $path: de map waarvan je de bestanden wil van zien
* @access public
* @author Hipska
*/
public function readDir($path = '/'){
$this->newRequest('PROPFIND', array('Depth: 1','Content-Type: text/xml'));
$path = $this->dir($path);
$xml = '<?xml version="1.0"?>'.EOL;
$xml .= '<A:propfind xmlns:A="DAV:">'.EOL;
$xml .= ' <A:allprop/>'.EOL;
$xml .= '</A:propfind>'.EOL;
list($header,$content) = $this->exec($xml);
if($header['Status'] != 207) $this->error($header['Reason'],$header['Status']);
$items = array(); // de resultaten wat verwerken tot een mooi overzicht
foreach ($content->response as $item){
$url = str_replace($path,'',$item->href);
$name = rawurldecode(basename($url));
$size = (int) $item->propstat->prop->getcontentlength;
$modification = (string) $item->propstat->prop->modificationdate;
$modification = str_replace('T',' ',$modification);
$modification = str_replace('Z','',$modification);
$creation = (string) $item->propstat->prop->creationdate;
$creation = str_replace('T',' ',$creation);
$creation = str_replace('Z','',$creation);
if($creation == '') $creation = $modification;
$type = isset($item->propstat->prop->resourcetype->collection)? 'dir' : 'file';
if($name != '') $items[] = array(
'name' => $name, 'url' => $url,
'size' => $size, 'type' => $type,
'creation' => $creation,
'modification' => $modification);
}
return $items;
}
/**
* iDisk::getFile()
*
* Download een bestand van de iDisk
*
* @param string $path: de locatie waar het bestand staat
* @param boolean $return: data returnen of meteen printen
* @access public
* @author Hipska
*/
public function getFile($path, $return = false){
$this->newRequest('GET');
$path = $this->dir($path);
list($header,$content) = $this->exec();
if($header['Status'] == 200 and strlen($content) == $header['Content-Length']){
if($this->debug){
return 'Debug mode: Succesfully received '.strlen($content).' Bytes from '.$path;
}elseif($return){
return $content;
}else{
header('Content-Type: Application/octet-stream');
header('Content-Length: '.$header['Content-Length']);
header('Content-Disposition: attachment; filename='.basename($path));
print $content;
exit;
}
}
else $this->error($header['Reason'],$header['Status']);
}
/**
* iDisk::putFile()
*
* Upload een bestand naar de iDisk
*
* @param string $path: de locatie waar het bestand moet komen
* @param string $data: de inhoud van het bestand
* @access public
* @author Hipska
*/
public function putFile($path, $data){
$this->newRequest('PUT',array('Content-Type: Application/octet-stream'));
$path = $this->dir($path);
list($header) = $this->exec($data);
if($header['Status'] > 204){
$this->error($header['Reason'],$header['Status']);
}else{
if($this->debug){
return 'Debug mode: Succesfully sent '.basename($path).' ('.strlen($data).' Bytes)';
}else return true;
}
}
/**
* iDisk::move()
*
* Verplaats bestanden/mappen op de iDisk
*
* @param string $source: de locatie om te verplaatsen
* @param string $destination: de locatie van de verplaatse bestanden/mappen
* @param boolean $overwrite: mag het overschreven worden
* @access public
* @author Hipska
*/
public function move($source, $destination, $overwrite = false){
$destination = $this->dir($destination);
$headers = array('Destination: http://'.$this->host.$destination);
$headers[] = 'Overwrite: '. ($overwrite ? 'T' : 'F');
$this->newRequest('MOVE',$headers);
$source = $this->dir($source);
list($header,$content) = $this->exec();
if($header['Status'] > 204) $this->error($header['Reason'],$header['Status']);
else return ($this->debug)? 'Debug mode: Moved '.$source.' to '.$destination : true;
}
/**
* iDisk::copy()
*
* Kopieer bestanden/mappen op de iDisk
*
* @param string $source: de locatie om te kopieren
* @param string $destination: de locatie van de kopie
* @param boolean $overwrite: mag locatie overschreven worden
* @access public
* @author Hipska
*/
public function copy($source, $destination, $overwrite = false){
$destination = $this->dir($destination);
$headers = array('Destination: http://'.$this->host.$destination);
$headers[] = 'Overwrite: '. ($overwrite ? 'T' : 'F');
$this->newRequest('COPY', $headers);
$source = $this->dir($source);
list($header,$content) = $this->exec();
if($header['Status'] > 204) $this->error($header['Reason'],$header['Status']);
else return ($this->debug)? 'Debug mode: Copied '.$source.' to '.$destination : true;
}
/**
* iDisk::delete()
*
* Verwijder bestanden/mappen op de iDisk
*
* @param string $path: de locatie om te verwijderen
* @access public
* @author Hipska
*/
public function delete($path){
$this->newRequest('DELETE');
$path = $this->dir($path);
list($header,$content) = $this->exec();
if($header['Status'] > 204) $this->error($header['Reason'],$header['Status']);
else return ($this->debug)? 'Debug mode: Deleted '.$path : true;
}
// ====================================================================================
private function init(){
$this->log('Initiating cURL');
$this->res = curl_init(); // cURL initialiseren
curl_setopt($this->res, CURLOPT_RETURNTRANSFER, true); //response returnen ipv echo
curl_setopt($this->res, CURLOPT_HEADER, true);
}
private function exec($data = null){
$this->log('Sending HTTP-request',$this->request);
curl_setopt($this->res, CURLOPT_HTTPHEADER, $this->request); // de headers instellen
if(!is_null($data)){
$this->log('Sending data',print_r($data,true));
curl_setopt($this->res, CURLOPT_POST, true); // request klaarmaken om data te zenden
curl_setopt($this->res, CURLOPT_POSTFIELDS, $data); // data meezenden
}
$result = curl_exec($this->res); // request uitvoeren
list($content,$header) = array_reverse(explode(EOL.EOL,$result,2));
$this->log('Received '.strlen($result).' Bytes');
$headers = explode(EOL,ltrim($header));
if(strpos($header,'HTTP') === 0){ // headers opmaken
list($http,$status,$reason) = explode(' ',$headers[0],3);
unset($headers[0]);
$header = array('HTTP' => substr($http,5), 'Status' => $status, 'Reason' => $reason);
}else $this->error('Bad HTTP Response');
foreach ($headers as $line){ // headers in een array zetten
list($key,$value) = explode(': ',$line,2);
$header[$key] = $value;
}
if(isset($header['Content-Type']) and strpos($header['Content-Type'],'text/xml') !== false){
$content = str_replace(' xmlns:D="DAV:"','',$content);
$content = str_replace(' xmlns="DAV:"','',$content);
$content = str_replace('D:','',$content);
$content = new SimpleXMLElement($content); // content xml omzetten
}
$result = array('header' => $header,'content' => $content);
$this->log('Processing response',$result);
return array_merge(array($header,$content),$result);
}
private function newRequest($type, $headers = null){
$this->log('Making HTTP-request of type '.$type);
curl_setopt($this->res, CURLOPT_CUSTOMREQUEST , $type); // type request instellen
$request = array();
$request[] = 'Host: ' . $this->host;
$request[] = 'Connection: Keep-Alive';
if($this->pass != '') // gebruikersnaam en wachtwoord meezenden
$request[] = 'Authorization: Basic '.base64_encode($this->user.':'.$this->pass);
if(is_array($headers)) $this->request = array_merge($request,$headers);
else $this->request = $request;
}
private function dir($path = '/'){
$uri = html_entity_decode($this->dir.'/'.$path);
$folder = explode('/',$uri);
$uri = array();
foreach($folder as $dir) if(strlen($dir) != 0) $uri[] = rawurlencode($dir);
$path = '/'.implode('/',$uri);
curl_setopt($this->res, CURLOPT_URL, 'http://'.$this->host.$path);
$this->log('Location set to '.$path);
return $path;
}
private function error($errtitle, $errno = 0){
$message = 'ERROR: <b>'.$errtitle.'</b>';
if($this->debug){
$trace = array_reverse(debug_backtrace());
$message .= EOL.'Locatie:'.EOL;
foreach ($trace as $error){
$message.= 'In '.$error['file'].' op lijn '.$error['line'].EOL;
}
}
$this->log[] = $message;
throw new Exception($message,$errno);
}
private function log($notice,$extra = null){
$message = 'NOTICE: <b>'.$notice.'</b>';
if(!is_null($extra) and $this->debug){
$message .= EOL.htmlentities(print_r($extra,true));
}
$this->log[] = $message;
}
public function __destruct(){
$this->log('Closing connection');
curl_close($this->res);
if($this->debug){
foreach ($this->log as $message) {
echo '<pre>'.$message.'</pre>'.EOL;
}
}
}
}
?>[/code]
Reacties
0