ILIAS  Release_5_0_x_branch Revision 61816
 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 
26  {
27  $this->gui = $gui;
28  }
29 
35  public function executeDefault($requestedMethod)
36  {
37 
38  }
39 
45  public function uploadFile()
46  {
47  global $ilCtrl;
48 
49  if ( !ilChatroom::checkUserPermissions( 'read', $this->gui->ref_id ) )
50  {
51  $ilCtrl->setParameterByClass("ilrepositorygui", "ref_id", ROOT_FOLDER_ID);
52  $ilCtrl->redirectByClass("ilrepositorygui", "");
53  }
54 
55  $upload_path = $this->getUploadPath();
56 
57  $this->checkUploadPath($upload_path);
58 
62  $file = $_FILES['file_to_upload']['tmp_name'];
63  $filename = $_FILES['file_to_upload']['name'];
64  $type = $_FILES['file_to_upload']['type'];
65  $target = $upload_path . $filename;
66 
67  if( ilUtil::moveUploadedFile( $file, $filename, $target ) )
68  {
69  global $ilUser;
70 
71  require_once 'Modules/Chatroom/classes/class.ilChatroom.php';
72  require_once 'Modules/Chatroom/classes/class.ilChatroomUser.php';
73 
74  $room = ilChatroom::byObjectId( $this->gui->object->getId() );
75  $chat_user = new ilChatroomUser($ilUser, $room);
76  $user_id = $chat_user->getUserId();
77 
78  if( !$room )
79  {
80  throw new Exception('unkown room');
81  }
82  else if( !$room->isSubscribed( $chat_user->getUserId() ) )
83  {
84  throw new Exception('not subscribed');
85  }
86 
87  $room->saveFileUploadToDb($user_id, $filename, $type);
88  $this->displayLinkToUploadedFile($room, $chat_user);
89  }
90 
91  }
92 
93  protected function displayLinkToUploadedFile($room, $chat_user)
94  {
95  global $ilCtrl;
96 
97  $scope = $room->getRoomId();
98  $params = array();
99  $params['public'] = 1;
103  $message = json_encode( $this->buildMessage(
104  json_encode(array(
105  'format' => array(),
106  'content' => ilUtil::stripSlashes(
107  'Eine neue Datei mit dem Link ' .
108  $ilCtrl->getLinkTarget($this->gui, 'uploadFile-deliverFile') .
109  ' wurde hochgeladen'
110  )
111  )) , $params, $chat_user
112  ) );
113 
114  $params = array_merge( $params, array('message' => $message) );
115  $query = http_build_query( $params );
116 
117  $connector = $this->gui->getConnector();
118  $response = $connector->post( $scope, $query );
119  $responseObject = json_decode( $response );
120  /*
121  if( $responseObject->success == true && $room->getSetting( 'enable_history' ) )
122  {
123  $room->addHistoryEntry( $message, $recipient, $publicMessage );
124  }
125  */
126  echo $response;
127  exit;
128  }
129 
130  public function deliverFile()
131  {
132  // send file
133 
134  echo "hello world";
135  }
136 
137  private function buildMessage($messageString, $params, ilChatroomUser $chat_user)
138  {
139  $data = new stdClass();
140 
141  $data->user = $this->gui->object->getPersonalInformation( $chat_user );
142  $data->message = $messageString;
143  $data->timestamp = date( 'c' );
144  $data->type = 'message';
145  $data->public = (int)$params['public'];
146  $data->recipients = $params['recipients']; // ? explode(",", $params['recipients']) : array();
147 
148  return $data;
149  }
150 
156  public function checkUploadPath($path)
157  {
158  $err = false;
159 
160  switch( true )
161  {
162  case !file_exists( $path ):
163  if( ! ilUtil::makeDirParents( $path ) )
164  {
165  $err = true;
166  $msg = 'Error: Upload path could not be created!';
167  }
168  break;
169 
170  case !is_dir( $path ):
171  $err = true;
172  $msg = 'Error: Upload path is not a directory!';
173  break;
174 
175  case !is_readable( $path ):
176  $err = true;
177  $msg = 'Error: Upload path is not readable!';
178  break;
179 
180  default:
181  }
182 
183  if( $err )
184  {
185  throw new Exception( $msg );
186  }
187  }
188 
194  public function getUploadPath()
195  {
196  $path = ilUtil::getDataDir() . "/chatroom/" . $this->gui->object->getId() . "/uploads/";
197 
198  return $path;
199  }
200 
201 }
202 
203 ?>