ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilChatroomUploadFileTask.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
16 {
17 
18  private $gui;
19 
25  public function __construct(ilDBayObjectGUI $gui)
26  {
27  $this->gui = $gui;
28  }
29 
35  public function executeDefault($requestedMethod)
36  {
37 
38  }
39 
45  public function uploadFile()
46  {
47  if ( !ilChatroom::checkUserPermissions( 'read', $this->gui->ref_id ) )
48  {
49  ilUtil::redirect("repository.php");
50  }
51 
52  $upload_path = $this->getUploadPath();
53 
54  $this->checkUploadPath($upload_path);
55 
59  $file = $_FILES['file_to_upload']['tmp_name'];
60  $filename = $_FILES['file_to_upload']['name'];
61  $type = $_FILES['file_to_upload']['type'];
62  $target = $upload_path . $filename;
63 
64  if( ilUtil::moveUploadedFile( $file, $filename, $target ) )
65  {
66  global $ilUser;
67 
68  require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
69  require_once 'Modules/Chatroom/classes/class.ilChatroomUser.php';
70 
71  $room = ilChatroom::byObjectId( $this->gui->object->getId() );
72  $chat_user = new ilChatroomUser($ilUser, $room);
73  $user_id = $chat_user->getUserId();
74 
75  if( !$room )
76  {
77  throw new Exception('unkown room');
78  }
79  else if( !$room->isSubscribed( $chat_user->getUserId() ) )
80  {
81  throw new Exception('not subscribed');
82  }
83 
84  $room->saveFileUploadToDb($user_id, $filename, $type);
85  $this->displayLinkToUploadedFile($room, $chat_user);
86  }
87 
88  }
89 
90  protected function displayLinkToUploadedFile($room, $chat_user)
91  {
92  global $ilCtrl;
93 
94  $scope = $room->getRoomId();
95  $params = array();
96  $params['public'] = 1;
100  $message = json_encode( $this->buildMessage(
101  json_encode(array(
102  'format' => array(),
103  'content' => ilUtil::stripSlashes(
104  'Eine neue Datei mit dem Link ' .
105  $ilCtrl->getLinkTarget($this->gui, 'uploadFile-deliverFile') .
106  ' wurde hochgeladen'
107  )
108  )) , $params, $chat_user
109  ) );
110 
111  $params = array_merge( $params, array('message' => $message) );
112  $query = http_build_query( $params );
113 
114  $connector = $this->gui->getConnector();
115  $response = $connector->post( $scope, $query );
116  $responseObject = json_decode( $response );
117  /*
118  if( $responseObject->success == true && $room->getSetting( 'enable_history' ) )
119  {
120  $room->addHistoryEntry( $message, $recipient, $publicMessage );
121  }
122  */
123  echo $response;
124  exit;
125  }
126 
127  public function deliverFile()
128  {
129  // send file
130 
131  echo "hello world";
132  }
133 
134  private function buildMessage($messageString, $params, ilChatroomUser $chat_user)
135  {
136  $data = new stdClass();
137 
138  $data->user = $this->gui->object->getPersonalInformation( $chat_user );
139  $data->message = $messageString;
140  $data->timestamp = date( 'c' );
141  $data->type = 'message';
142  $data->public = (int)$params['public'];
143  $data->recipients = $params['recipients']; // ? explode(",", $params['recipients']) : array();
144 
145  return $data;
146  }
147 
153  public function checkUploadPath($path)
154  {
155  $err = false;
156 
157  switch( true )
158  {
159  case !file_exists( $path ):
160  if( ! ilUtil::makeDirParents( $path ) )
161  {
162  $err = true;
163  $msg = 'Error: Upload path could not be created!';
164  }
165  break;
166 
167  case !is_dir( $path ):
168  $err = true;
169  $msg = 'Error: Upload path is not a directory!';
170  break;
171 
172  case !is_readable( $path ):
173  $err = true;
174  $msg = 'Error: Upload path is not readable!';
175  break;
176 
177  default:
178  }
179 
180  if( $err )
181  {
182  throw new Exception( $msg );
183  }
184  }
185 
191  public function getUploadPath()
192  {
193  $path = ilUtil::getDataDir() . "/chatroom/" . $this->gui->object->getId() . "/uploads/";
194 
195  return $path;
196  }
197 
198 }
199 
200 ?>