/auth/fc/fcFPP.php

https://bitbucket.org/kudutest1/moodlegit · PHP · 220 lines · 129 code · 46 blank · 45 comment · 23 complexity · d405b6eb016fba6dd84f920a1c308143 MD5 · raw file

  1. <?php
  2. /************************************************************************/
  3. /* fcFPP: Php class for FirstClass Flexible Provisining Protocol */
  4. /* ============================================================= */
  5. /* */
  6. /* Copyright (c) 2004 SKERIA Utveckling, Teknous */
  7. /* http://skeria.skelleftea.se */
  8. /* */
  9. /* Flexible Provisioning Protocol is a real-time, IP based protocol */
  10. /* which provides direct access to the scriptable remote administration */
  11. /* subsystem of the core FirstClass Server. Using FPP, it is possible to*/
  12. /* implement automated provisioning and administration systems for */
  13. /* FirstClass, avoiding the need for a point and click GUI. FPP can also*/
  14. /* be used to integrate FirstClass components into a larger unified */
  15. /* system. */
  16. /* */
  17. /* This program is free software. You can redistribute it and/or modify */
  18. /* it under the terms of the GNU General Public License as published by */
  19. /* the Free Software Foundation; either version 2 of the License or any */
  20. /* later version. */
  21. /************************************************************************/
  22. /* Author: Torsten Anderson, torsten.anderson@skeria.skelleftea.se
  23. */
  24. class fcFPP
  25. {
  26. var $_hostname; // hostname of FirstClass server we are connection to
  27. var $_port; // port on which fpp is running
  28. var $_conn = 0; // socket we are connecting on
  29. var $_debug = FALSE; // set to true to see some debug info
  30. // class constructor
  31. function fcFPP($host="localhost", $port="3333")
  32. {
  33. $this->_hostname = $host;
  34. $this->_port = $port;
  35. $this->_user = "";
  36. $this->_pwd = "";
  37. }
  38. // open a connection to the FirstClass server
  39. function open()
  40. {
  41. if ($this->_debug) echo "Connecting to host ";
  42. $host = $this->_hostname;
  43. $port = $this->_port;
  44. if ($this->_debug) echo "[$host:$port]..";
  45. // open the connection to the FirstClass server
  46. $conn = fsockopen($host, $port, $errno, $errstr, 5);
  47. if (!$conn)
  48. {
  49. print_error('auth_fcconnfail','auth_fc', '', array('no'=>$errno, 'str'=>$errstr));
  50. return false;
  51. }
  52. // We are connected
  53. if ($this->_debug) echo "connected!";
  54. // Read connection message.
  55. $line = fgets ($conn); //+0
  56. $line = fgets ($conn); //new line
  57. // store the connection in this class, so we can use it later
  58. $this->_conn = & $conn;
  59. return true;
  60. }
  61. // close any open connections
  62. function close()
  63. {
  64. // get the current connection
  65. $conn = &$this->_conn;
  66. // close it if it's open
  67. if ($conn)
  68. {
  69. fclose($conn);
  70. // cleanup the variable
  71. unset($this->_conn);
  72. return true;
  73. }
  74. return;
  75. }
  76. // Authenticate to the FirstClass server
  77. function login($userid, $passwd)
  78. {
  79. // we did have a connection right?!
  80. if ($this->_conn)
  81. {
  82. # Send username
  83. fputs($this->_conn,"$userid\r\n");
  84. $line = fgets ($this->_conn); //new line
  85. $line = fgets ($this->_conn); //+0
  86. $line = fgets ($this->_conn); //new line
  87. # Send password
  88. fputs($this->_conn,"$passwd\r\n");
  89. $line = fgets ($this->_conn); //new line
  90. $line = fgets ($this->_conn); //+0
  91. $line = fgets ($this->_conn); //+0 or message
  92. if ($this->_debug) echo $line;
  93. if (preg_match ("/^\+0/", $line)) { //+0, user with subadmin privileges
  94. $this->_user = $userid;
  95. $this->_pwd = $passwd;
  96. return TRUE;
  97. } elseif (strpos($line, 'You are not allowed')) { // Denied access but a valid user and password
  98. // "Sorry. You are not allowed to login with the FPP interface"
  99. return TRUE;
  100. } else { //Invalid user or password
  101. return FALSE;
  102. }
  103. }
  104. return FALSE;
  105. }
  106. // Get the list of groups the user is a member of
  107. function getGroups($userid) {
  108. $groups = array();
  109. // we must be logged in as a user with subadmin privileges
  110. if ($this->_conn AND $this->_user) {
  111. # Send BA-command to get groups
  112. fputs($this->_conn,"GET USER '" . $userid . "' 4 -1\r");
  113. $line = "";
  114. while (!$line) {
  115. $line = trim(fgets ($this->_conn));
  116. }
  117. $n = 0;
  118. while ($line AND !preg_match("/^\+0/", $line) AND $line != "-1003") {
  119. list( , , $groups[$n++]) = explode(" ",$line,3);
  120. $line = trim(fgets ($this->_conn));
  121. }
  122. if ($this->_debug) echo "getGroups:" . implode(",",$groups);
  123. }
  124. return $groups;
  125. }
  126. // Check if the user is member of any of the groups.
  127. // Return the list of groups the user is member of.
  128. function isMemberOf($userid, $groups) {
  129. $usergroups = array_map("strtolower",$this->getGroups($userid));
  130. $groups = array_map("strtolower",$groups);
  131. $result = array_intersect($groups,$usergroups);
  132. if ($this->_debug) echo "isMemberOf:" . implode(",",$result);
  133. return $result;
  134. }
  135. function getUserInfo($userid, $field) {
  136. $userinfo = "";
  137. if ($this->_conn AND $this->_user) {
  138. # Send BA-command to get data
  139. fputs($this->_conn,"GET USER '" . $userid . "' " . $field . "\r");
  140. $line = "";
  141. while (!$line) {
  142. $line = trim(fgets ($this->_conn));
  143. }
  144. $n = 0;
  145. while ($line AND !preg_match("/^\+0/", $line)) {
  146. list( , , $userinfo) = explode(" ",$line,3);
  147. $line = trim(fgets ($this->_conn));
  148. }
  149. if ($this->_debug) echo "getUserInfo:" . $userinfo;
  150. }
  151. return str_replace('\r',' ',trim($userinfo,'"'));
  152. }
  153. function getResume($userid) {
  154. $resume = "";
  155. $pattern = "/\[.+:.+\..+\]/"; // Remove references to pictures in resumes
  156. if ($this->_conn AND $this->_user) {
  157. # Send BA-command to get data
  158. fputs($this->_conn,"GET RESUME '" . $userid . "' 6\r");
  159. $line = "";
  160. while (!$line) {
  161. $line = trim(fgets ($this->_conn));
  162. }
  163. $n = 0;
  164. while ($line AND !preg_match("/^\+0/", $line)) {
  165. $resume .= preg_replace($pattern,"",str_replace('\r',"\n",trim($line,'6 ')));
  166. $line = trim(fgets ($this->_conn));
  167. //print $line;
  168. }
  169. if ($this->_debug) echo "getResume:" . $resume;
  170. }
  171. return $resume;
  172. }
  173. }
  174. ?>