idisk-connectie-class

Gesponsorde koppelingen

PHP script bestanden

  1. idisk-connectie-class

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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)
PHP script in nieuw venster Selecteer het PHP script
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?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;
            }
        }
    }
    
}


?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.