openmeetings_gateway.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License") + you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. /**
  21. * @package chamilo.plugin.openmeetings
  22. */
  23. /**
  24. * Init.
  25. */
  26. require_once 'openmeetings_rest_service.php';
  27. /**
  28. * Class OpenMeetingsGateway.
  29. */
  30. class OpenMeetingsGateway
  31. {
  32. public $sessionId = "";
  33. public $config;
  34. private $rest;
  35. private $_user;
  36. private $_pass;
  37. private $_url;
  38. public function __construct($host, $user, $pass)
  39. {
  40. $this->_user = urlencode($user);
  41. $this->_pass = urlencode($pass);
  42. $this->_url = $host;
  43. if (substr($this->_url, -1, 1) == '/') {
  44. $this->_url = substr($this->_url, 0, -1);
  45. }
  46. $this->rest = new OpenMeetingsRestService();
  47. $err = $this->rest->getError();
  48. if ($err) {
  49. error_log('Constructor error: '.$err);
  50. error_log('Debug: '.$this->rest->getDebug());
  51. exit();
  52. }
  53. }
  54. /**
  55. * @param string $name
  56. *
  57. * @return string
  58. */
  59. public function getRestUrl($name)
  60. {
  61. return $this->getUrl()."/services/".$name."/";
  62. }
  63. /**
  64. * @return string
  65. */
  66. public function getUrl()
  67. {
  68. return $this->_url;
  69. }
  70. /**
  71. * @param bool $in
  72. *
  73. * @return string
  74. */
  75. public function var_to_str($in)
  76. {
  77. if (is_bool($in)) {
  78. return $in ? "true" : "false";
  79. } else {
  80. return $in;
  81. }
  82. }
  83. /**
  84. * TODO: Get Error Service and show detailed Error Message.
  85. */
  86. public function loginUser()
  87. {
  88. $returnValue = 0;
  89. $response = $this->rest->call($this->getRestUrl("UserService")."getSession", "session_id");
  90. if ($this->rest->getError()) {
  91. error_log('Fault (Expect - The request contains an invalid SOAP body) '.print_r($response, 1));
  92. } else {
  93. $err = $this->rest->getError();
  94. if ($err) {
  95. error_log('Error: '.$err);
  96. } else {
  97. $this->sessionId = $response;
  98. $url = $this->getRestUrl("UserService")
  99. ."loginUser?"
  100. ."SID=".$this->sessionId
  101. ."&username=".$this->_user
  102. ."&userpass=".$this->_pass;
  103. $result = $this->rest->call($url);
  104. if ($this->rest->getError()) {
  105. error_log('Fault (Expect - The request contains an invalid SOAP body) '.print_r($result, 1));
  106. } else {
  107. $err = $this->rest->getError();
  108. if ($err) {
  109. error_log('Error '.$err);
  110. } else {
  111. $returnValue = $result;
  112. }
  113. }
  114. }
  115. }
  116. if ($returnValue > 0) {
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. }
  122. /**
  123. * @param Room $room
  124. *
  125. * @return array|bool|int|null
  126. */
  127. public function updateRoomWithModeration($room)
  128. {
  129. $err = $this->rest->getError();
  130. if ($err) {
  131. error_log('Constructor error: '.$err);
  132. error_log('Debug: '.$this->rest->getDebug());
  133. exit();
  134. }
  135. $isModeratedRoom = false;
  136. if ($room->isModeratedRoom == 1) {
  137. $isModeratedRoom = true;
  138. }
  139. $url = $this->getRestUrl($this->getRestUrl("RoomService")
  140. ."updateRoomWithModeration?SID=".$this->sessionId
  141. ."&room_id=".$room->room_id
  142. ."&name=".urlencode($room->name)
  143. ."&roomtypes_id=".$room->roomtypes_id
  144. ."&comment=".$room->comment
  145. ."&numberOfPartizipants=".$room->numberOfPartizipants
  146. ."&ispublic=false"
  147. ."&appointment=false"
  148. ."&isDemoRoom=false"
  149. ."&demoTime=0"
  150. ."&isModeratedRoom=".$this->var_to_str($isModeratedRoom));
  151. $result = $this->rest->call($url);
  152. if ($result->fault) {
  153. error_log('Fault (Expect - The request contains an invalid SOAP body) '.print_r($result, 1));
  154. } else {
  155. $err = $this->rest->getError();
  156. if ($err) {
  157. error_log('Error: '.$err);
  158. } else {
  159. return $result;
  160. }
  161. }
  162. return -1;
  163. }
  164. /**
  165. * @param $username
  166. * @param $firstname
  167. * @param $lastname
  168. * @param $userId
  169. * @param $systemType
  170. * @param $recording_id
  171. *
  172. * @return array|bool|int|null
  173. */
  174. public function setUserObjectAndGenerateRecordingHashByURL($username, $firstname, $lastname, $userId, $systemType, $recording_id)
  175. {
  176. $result = $this->rest->call($this->getRestUrl("UserService")
  177. .'setUserObjectAndGenerateRecordingHashByURL?'
  178. .'SID='.$this->sessionId
  179. .'&username='.urlencode($username)
  180. .'&firstname='.urlencode($firstname)
  181. .'&lastname='.urlencode($lastname)
  182. .'&externalUserId='.$userId
  183. .'&externalUserType='.urlencode($systemType)
  184. .'&recording_id='.$recording_id, 'return');
  185. if ($result->fault) {
  186. error_log('Fault (Expect - The request contains an invalid SOAP body) '.print_r($result, 1));
  187. } else {
  188. $err = $this->rest->getError();
  189. if ($err) {
  190. error_log('Error: '.$err);
  191. } else {
  192. return $result;
  193. }
  194. }
  195. return -1;
  196. }
  197. /**
  198. * @param $username
  199. * @param $firstname
  200. * @param $lastname
  201. * @param $profilePictureUrl
  202. * @param $email
  203. * @param $userId
  204. * @param $systemType
  205. * @param $room_id
  206. * @param $becomeModerator
  207. * @param $allowRecording
  208. *
  209. * @return array|bool|int|null
  210. */
  211. public function setUserObjectAndGenerateRoomHashByURLAndRecFlag($username, $firstname, $lastname, $profilePictureUrl, $email, $userId, $systemType, $room_id, $becomeModerator, $allowRecording)
  212. {
  213. $err = $this->rest->getError();
  214. if ($err) {
  215. error_log('Constructor error: '.$err);
  216. error_log('Debug: '.$this->rest->getDebug());
  217. exit();
  218. }
  219. $result = $this->rest->call($this->getRestUrl("UserService")
  220. ."setUserObjectAndGenerateRoomHashByURLAndRecFlag?"
  221. ."SID=".$this->sessionId
  222. ."&username=".urlencode($username)
  223. ."&firstname=".urlencode($firstname)
  224. ."&lastname=".urlencode($lastname)
  225. ."&profilePictureUrl=".urlencode($profilePictureUrl)
  226. ."&email=".urlencode($email)
  227. ."&externalUserId=".urlencode($userId)
  228. ."&externalUserType=".urlencode($systemType)
  229. ."&room_id=".urlencode($room_id)
  230. ."&becomeModeratorAsInt=".$becomeModerator
  231. ."&showAudioVideoTestAsInt=1"
  232. ."&allowRecording=".$this->var_to_str($allowRecording));
  233. if ($result->fault) {
  234. error_log('Fault (Expect - The request contains an invalid SOAP body) '.print_r($result, 1));
  235. } else {
  236. $err = $this->rest->getError();
  237. if ($err) {
  238. error_log('Error: '.$err);
  239. } else {
  240. // echo '<h2>Result</h2><pre>'; print_r($result["return"]); echo '</pre>';
  241. return $result;
  242. }
  243. }
  244. return -1;
  245. }
  246. /**
  247. * @param Room $openmeetings
  248. *
  249. * @return array|bool|int|null
  250. */
  251. public function deleteRoom($openmeetings)
  252. {
  253. $err = $this->rest->getError();
  254. if ($err) {
  255. error_log('Constructor error: '.$err);
  256. error_log('Debug: '.$this->rest->getDebug());
  257. exit();
  258. }
  259. $url = $this->getRestUrl("RoomService")."deleteRoom?SID=".$this->sessionId."&rooms_id=".$openmeetings->room_id;
  260. $result = $this->rest->call($url);
  261. if ($result->fault) {
  262. error_log('Fault (Expect - The request contains an invalid SOAP body) '.print_r($result, 1));
  263. } else {
  264. $err = $this->rest->getError();
  265. if ($err) {
  266. error_log('Error: '.$err);
  267. } else {
  268. // echo '<h2>Result</h2><pre>'; print_r($result["return"]); echo '</pre>';
  269. // return $result["return"];
  270. return $result;
  271. }
  272. }
  273. return -1;
  274. }
  275. /**
  276. * Generate a new room hash for entering a conference room.
  277. */
  278. public function setUserObjectAndGenerateRoomHash($username, $firstname, $lastname, $profilePictureUrl, $email, $externalUserId, $externalUserType, $room_id, $becomeModeratorAsInt, $showAudioVideoTestAsInt)
  279. {
  280. $result = $this->rest->call($this->getRestUrl("UserService")
  281. ."setUserObjectAndGenerateRoomHash?"
  282. ."SID=".$this->sessionId
  283. ."&username=".urlencode($username)
  284. ."&firstname=".urlencode($firstname)
  285. ."&lastname=".urlencode($lastname)
  286. ."&profilePictureUrl=".urlencode($profilePictureUrl)
  287. ."&email=".urlencode($email)
  288. ."&externalUserId=".urlencode($externalUserId)
  289. ."&externalUserType=".urlencode($externalUserType)
  290. ."&room_id=".$room_id
  291. ."&becomeModeratorAsInt=".$becomeModeratorAsInt
  292. ."&showAudioVideoTestAsInt=".$showAudioVideoTestAsInt);
  293. if ($result->getError()) {
  294. error_log('Fault (Expect - The request contains an invalid SOAP body) '.print_r($result, 1));
  295. } else {
  296. $err = $this->rest->getError();
  297. if ($err) {
  298. error_log('Error: '.$err);
  299. } else {
  300. // echo '<h2>Result</h2><pre>'; print_r($result["return"]); echo '</pre>';
  301. return $result;
  302. }
  303. }
  304. return -1;
  305. }
  306. /**
  307. * Create a new conference room.
  308. *
  309. * @param Room The room object
  310. *
  311. * @return The REST call's result
  312. */
  313. public function createRoomWithModAndType($room)
  314. {
  315. $service = 'addRoomWithModerationAndExternalType';
  316. if ($room->allowRecording) {
  317. $service = 'addRoomWithModerationAndRecordingFlags';
  318. } elseif ($room->isAudioOnly) {
  319. $service = 'addRoomWithModerationExternalTypeAndAudioType';
  320. }
  321. $url = $this->getRestUrl("RoomService")
  322. .$service.'?'
  323. .'SID='.$room->SID
  324. .'&name='.$room->name
  325. .'&roomtypes_id='.$room->roomtypes_id
  326. .'&comment='.$room->comment
  327. .'&numberOfPartizipants='.$room->numberOfPartizipants
  328. .'&ispublic='.$this->var_to_str($room->ispublic)
  329. .'&appointment='.$this->var_to_str($room->appointment)
  330. .'&isDemoRoom='.$this->var_to_str($room->isDemoRoom)
  331. .'&demoTime='.$room->demoTime
  332. .'&isModeratedRoom='.$this->var_to_str($room->isModeratedRoom)
  333. .'&externalRoomType='.$room->externalRoomType;
  334. if ($room->allowRecording) {
  335. $url .= '&allowUserQuestions='.$this->var_to_str($room->allowUserQuestions)
  336. .'&isAudioOnly='.$this->var_to_str($room->isAudioOnly)
  337. .'&waitForRecording='.$this->var_to_str($room->waitForRecording)
  338. .'&allowRecording='.$this->var_to_str($room->allowRecording);
  339. } elseif ($room->isAudioOnly) {
  340. $url .= '&isAudioOnly='.$this->var_to_str($room->isAudioOnly);
  341. }
  342. $result = $this->rest->call($url);
  343. if ($this->rest->fault) {
  344. error_log('Fault (Expect - The request contains an invalid SOAP body) '.print_r($result, 1));
  345. } else {
  346. $err = $this->rest->getError();
  347. if ($err) {
  348. error_log('Error: '.$err);
  349. } else {
  350. return $result;
  351. }
  352. }
  353. return -1;
  354. }
  355. /**
  356. * Gets the list of open rooms of type "Chamilo".
  357. *
  358. * @param string $type The type of external system connecting to OpenMeetings
  359. *
  360. * @return bool
  361. */
  362. public function getRoomsWithCurrentUsersByType($type = 'chamilolms')
  363. {
  364. //$this->loginUser();
  365. if (empty($this->sessionId)) {
  366. return false;
  367. }
  368. $url = $this->getRestUrl("RoomService")."getRoomsWithCurrentUsersByListAndType?SID=".$this->sessionId
  369. ."&start=1&max=1000&orderby=name&asc=true&externalRoomType=chamilolms";
  370. //$url = $this->getRestUrl("RoomService")
  371. // . "getRoomTypes?"
  372. // . "SID=" . $this->sessionId;
  373. //$url = $this->getRestUrl('JabberService') . 'getAvailableRooms?SID=' . $this->sessionId;
  374. $result = $this->rest->call($url, "return");
  375. $rooms = [];
  376. foreach ($result as $room) {
  377. if ($room['externalRoomType'] == $type && count($room['currentusers']) > 0) {
  378. $rooms[] = $room;
  379. }
  380. }
  381. return $result;
  382. }
  383. /**
  384. * Gets details of a remote room by room ID.
  385. *
  386. * @param int $roomId The ID of the room, as of plugin_openmeetings.room_id
  387. *
  388. * @return mixed Room object
  389. */
  390. public function getRoomById($roomId = 0)
  391. {
  392. //$this->loginUser();
  393. if (empty($this->sessionId) or empty($roomId)) {
  394. return false;
  395. }
  396. $roomId = intval($roomId);
  397. $url = $this->getRestUrl("RoomService")
  398. ."getRoomById?"
  399. ."SID=".$this->sessionId
  400. ."&rooms_id=".$roomId;
  401. $result = $this->rest->call($url, "return");
  402. return $result;
  403. }
  404. /**
  405. * Get list of available recordings made by this instance.
  406. */
  407. public function getRecordingsByExternalRooms()
  408. {
  409. $url = $this->getRestUrl("RoomService")
  410. ."getFlvRecordingByExternalRoomType?"
  411. ."SID=".$this->sessionId
  412. ."&externalRoomType=".urlencode($this->config["moduleKey"]);
  413. $result = $this->rest->call($url, "return");
  414. return $result;
  415. }
  416. /**
  417. * Get the recording from the room.
  418. *
  419. * @param int $id Room ID
  420. *
  421. * @return array
  422. */
  423. public function getFlvRecordingByRoomId($id)
  424. {
  425. $url = $this->getRestUrl("RoomService")
  426. ."getFlvRecordingByRoomId?"
  427. ."SID=".$this->sessionId
  428. ."&roomId=".urlencode($id);
  429. $result = $this->rest->call($url, "return");
  430. return $result;
  431. }
  432. /**
  433. * Get list of available recordings made by user.
  434. */
  435. public function getRecordingsByExternalUser($id)
  436. {
  437. $url = $this->getRestUrl("RoomService")
  438. ."getFlvRecordingByExternalUserId?"
  439. ."SID=".$this->sessionId
  440. ."&externalUserId=".$id;
  441. $result = $this->rest->call($url, "return");
  442. return $result;
  443. }
  444. }