TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
FileTransfer.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: FileTransfer.php 8/31/2012 11:06:09 scp@orilla $
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @package TeamSpeak3
23  * @version 1.1.16
24  * @author Sven 'ScP' Paulsen
25  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
26  */
27 
28 /**
29  * @class TeamSpeak3_Adapter_FileTransfer
30  * @brief Provides low-level methods for file transfer communication with a TeamSpeak 3 Server.
31  */
33 {
34  /**
35  * Connects the TeamSpeak3_Transport_Abstract object and performs initial actions on the remote
36  * server.
37  *
38  * @throws TeamSpeak3_Adapter_Exception
39  * @return void
40  */
41  public function syn()
42  {
43  $this->initTransport($this->options);
44  $this->transport->setAdapter($this);
45 
46  TeamSpeak3_Helper_Profiler::init(spl_object_hash($this));
47 
48  TeamSpeak3_Helper_Signal::getInstance()->emit("filetransferConnected", $this);
49  }
50 
51  /**
52  * The TeamSpeak3_Adapter_FileTransfer destructor.
53  *
54  * @return void
55  */
56  public function __destruct()
57  {
58  if($this->getTransport() instanceof TeamSpeak3_Transport_Abstract && $this->getTransport()->isConnected())
59  {
60  $this->getTransport()->disconnect();
61  }
62  }
63 
64  /**
65  * Sends a valid file transfer key to the server to initialize the file transfer.
66  *
67  * @param string $ftkey
68  * @throws TeamSpeak3_Adapter_FileTransfer_Exception
69  * @return void
70  */
71  protected function init($ftkey)
72  {
73  if(strlen($ftkey) != 32)
74  {
75  throw new TeamSpeak3_Adapter_FileTransfer_Exception("invalid file transfer key format");
76  }
77 
78  $this->getProfiler()->start();
79  $this->getTransport()->send($ftkey);
80 
81  TeamSpeak3_Helper_Signal::getInstance()->emit("filetransferHandshake", $this);
82  }
83 
84  /**
85  * Sends the content of a file to the server.
86  *
87  * @param string $ftkey
88  * @param integer $seek
89  * @param string $data
90  * @throws TeamSpeak3_Adapter_FileTransfer_Exception
91  * @return void
92  */
93  public function upload($ftkey, $seek, $data)
94  {
95  $this->init($ftkey);
96 
97  $size = strlen($data);
98  $seek = intval($seek);
99  $pack = 4096;
100 
101  TeamSpeak3_Helper_Signal::getInstance()->emit("filetransferUploadStarted", $ftkey, $seek, $size);
102 
103  for(;$seek < $size;)
104  {
105  $rest = $size-$seek;
106  $pack = $rest < $pack ? $rest : $pack;
107  $buff = substr($data, $seek, $pack);
108  $seek = $seek+$pack;
109 
110  $this->getTransport()->send($buff);
111 
112  TeamSpeak3_Helper_Signal::getInstance()->emit("filetransferUploadProgress", $ftkey, $seek, $size);
113  }
114 
115  $this->getProfiler()->stop();
116 
117  TeamSpeak3_Helper_Signal::getInstance()->emit("filetransferUploadFinished", $ftkey, $seek, $size);
118 
119  if($seek < $size)
120  {
121  throw new TeamSpeak3_Adapter_FileTransfer_Exception("incomplete file upload (" . $seek . " of " . $size . " bytes)");
122  }
123  }
124 
125  /**
126  * Returns the content of a downloaded file as a TeamSpeak3_Helper_String object.
127  *
128  * @param string $ftkey
129  * @param integer $size
130  * @param boolean $passthru
131  * @throws TeamSpeak3_Adapter_FileTransfer_Exception
132  * @return TeamSpeak3_Helper_String
133  */
134  public function download($ftkey, $size, $passthru = FALSE)
135  {
136  $this->init($ftkey);
137 
138  if($passthru)
139  {
140  return $this->passthru($size);
141  }
142 
143  $buff = new TeamSpeak3_Helper_String("");
144  $size = intval($size);
145  $pack = 4096;
146 
147  TeamSpeak3_Helper_Signal::getInstance()->emit("filetransferDownloadStarted", $ftkey, count($buff), $size);
148 
149  for($seek = 0;$seek < $size;)
150  {
151  $rest = $size-$seek;
152  $pack = $rest < $pack ? $rest : $pack;
153  $data = $this->getTransport()->read($rest < $pack ? $rest : $pack);
154  $seek = $seek+$pack;
155 
156  $buff->append($data);
157 
158  TeamSpeak3_Helper_Signal::getInstance()->emit("filetransferDownloadProgress", $ftkey, count($buff), $size);
159  }
160 
161  $this->getProfiler()->stop();
162 
163  TeamSpeak3_Helper_Signal::getInstance()->emit("filetransferDownloadFinished", $ftkey, count($buff), $size);
164 
165  if(strlen($buff) != $size)
166  {
167  throw new TeamSpeak3_Adapter_FileTransfer_Exception("incomplete file download (" . count($buff) . " of " . $size . " bytes)");
168  }
169 
170  return $buff;
171  }
172 
173  /**
174  * Outputs all remaining data on a TeamSpeak 3 file transfer stream using PHP's fpassthru()
175  * function.
176  *
177  * @param integer $size
178  * @throws TeamSpeak3_Adapter_FileTransfer_Exception
179  * @return void
180  */
181  protected function passthru($size)
182  {
183  $buff_size = fpassthru($this->getTransport()->getStream());
184 
185  if($buff_size != $size)
186  {
187  throw new TeamSpeak3_Adapter_FileTransfer_Exception("incomplete file download (" . intval($buff_size) . " of " . $size . " bytes)");
188  }
189  }
190 }