Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / auth / fc / fcFPP.php
blob7b844478e5fec4e2ad081681498372538b41fae7
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. */
20 /************************************************************************/
21 /* Author: Torsten Anderson, torsten.anderson@skeria.skelleftea.se
24 class fcFPP
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
31 // class constructor
32 function fcFPP($host="localhost", $port="3333")
34 $this->_hostname = $host;
35 $this->_port = $port;
36 $this->_user = "";
37 $this->_pwd = "";
40 // open a connection to the FirstClass server
41 function open()
43 if ($this->_debug) echo "Connecting to host ";
44 $host = $this->_hostname;
45 $port = $this->_port;
47 if ($this->_debug) echo "[$host:$port]..";
49 // open the connection to the FirstClass server
50 $conn = fsockopen($host, $port, $errno, $errstr, 5);
51 if (!$conn)
53 print_error('auth_fcconnfail','auth', '', array($errno, $errstr));
54 return false;
57 // We are connected
58 if ($this->_debug) echo "connected!";
60 // Read connection message.
61 $line = fgets ($conn); //+0
62 $line = fgets ($conn); //new line
64 // store the connection in this class, so we can use it later
65 $this->_conn = & $conn;
67 return true;
70 // close any open connections
71 function close()
73 // get the current connection
74 $conn = &$this->_conn;
76 // close it if it's open
77 if ($conn)
79 fclose($conn);
81 // cleanup the variable
82 unset($this->_conn);
83 return true;
85 return;
89 // Authenticate to the FirstClass server
90 function login($userid, $passwd)
92 // we did have a connection right?!
93 if ($this->_conn)
95 # Send username
96 fputs($this->_conn,"$userid\r\n");
98 $line = fgets ($this->_conn); //new line
99 $line = fgets ($this->_conn); //+0
100 $line = fgets ($this->_conn); //new line
102 # Send password
103 fputs($this->_conn,"$passwd\r\n");
104 $line = fgets ($this->_conn); //new line
105 $line = fgets ($this->_conn); //+0
106 $line = fgets ($this->_conn); //+0 or message
108 if ($this->_debug) echo $line;
110 if (preg_match ("/^\+0/", $line)) { //+0, user with subadmin privileges
111 $this->_user = $userid;
112 $this->_pwd = $passwd;
113 return TRUE;
114 } elseif (strpos($line, 'You are not allowed')) { // Denied access but a valid user and password
115 // "Sorry. You are not allowed to login with the FPP interface"
116 return TRUE;
117 } else { //Invalid user or password
118 return FALSE;
123 return FALSE;
126 // Get the list of groups the user is a member of
127 function getGroups($userid) {
129 $groups = array();
131 // we must be logged in as a user with subadmin privileges
132 if ($this->_conn AND $this->_user) {
133 # Send BA-command to get groups
134 fputs($this->_conn,"GET USER '" . $userid . "' 4 -1\r");
135 $line = "";
136 while (!$line) {
137 $line = trim(fgets ($this->_conn));
139 $n = 0;
140 while ($line AND !preg_match("/^\+0/", $line) AND $line != "-1003") {
141 list( , , $groups[$n++]) = explode(" ",$line,3);
142 $line = trim(fgets ($this->_conn));
144 if ($this->_debug) echo "getGroups:" . implode(",",$groups);
147 return $groups;
150 // Check if the user is member of any of the groups.
151 // Return the list of groups the user is member of.
152 function isMemberOf($userid, $groups) {
154 $usergroups = array_map("strtolower",$this->getGroups($userid));
155 $groups = array_map("strtolower",$groups);
157 $result = array_intersect($groups,$usergroups);
159 if ($this->_debug) echo "isMemberOf:" . implode(",",$result);
161 return $result;
165 function getUserInfo($userid, $field) {
167 $userinfo = "";
169 if ($this->_conn AND $this->_user) {
170 # Send BA-command to get data
171 fputs($this->_conn,"GET USER '" . $userid . "' " . $field . "\r");
172 $line = "";
173 while (!$line) {
174 $line = trim(fgets ($this->_conn));
176 $n = 0;
177 while ($line AND !preg_match("/^\+0/", $line)) {
178 list( , , $userinfo) = explode(" ",$line,3);
179 $line = trim(fgets ($this->_conn));
181 if ($this->_debug) echo "getUserInfo:" . $userinfo;
184 return str_replace('\r',' ',trim($userinfo,'"'));
188 function getResume($userid) {
190 $resume = "";
192 $pattern = "/\[.+:.+\..+\]/"; // Remove references to pictures in resumes
194 if ($this->_conn AND $this->_user) {
195 # Send BA-command to get data
196 fputs($this->_conn,"GET RESUME '" . $userid . "' 6\r");
197 $line = "";
198 while (!$line) {
199 $line = trim(fgets ($this->_conn));
201 $n = 0;
202 while ($line AND !preg_match("/^\+0/", $line)) {
203 $resume .= preg_replace($pattern,"",str_replace('\r',"\n",trim($line,'6 ')));
204 $line = trim(fgets ($this->_conn));
205 //print $line;
208 if ($this->_debug) echo "getResume:" . $resume;
211 return $resume;