Merge branch 'w07_MDL-26379_20_minify' of git://github.com/skodak/moodle
[moodle.git] / auth / fc / fcFPP.php
blobc08220215d5237d41a709bea730b15c78ff71df0
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
25 class fcFPP
27 var $_hostname; // hostname of FirstClass server we are connection to
28 var $_port; // port on which fpp is running
29 var $_conn = 0; // socket we are connecting on
30 var $_debug = FALSE; // set to true to see some debug info
32 // class constructor
33 function fcFPP($host="localhost", $port="3333")
35 $this->_hostname = $host;
36 $this->_port = $port;
37 $this->_user = "";
38 $this->_pwd = "";
41 // open a connection to the FirstClass server
42 function open()
44 if ($this->_debug) echo "Connecting to host ";
45 $host = $this->_hostname;
46 $port = $this->_port;
48 if ($this->_debug) echo "[$host:$port]..";
50 // open the connection to the FirstClass server
51 $conn = fsockopen($host, $port, $errno, $errstr, 5);
52 if (!$conn)
54 print_error('auth_fcconnfail','auth_fc', '', array('no'=>$errno, 'str'=>$errstr));
55 return false;
58 // We are connected
59 if ($this->_debug) echo "connected!";
61 // Read connection message.
62 $line = fgets ($conn); //+0
63 $line = fgets ($conn); //new line
65 // store the connection in this class, so we can use it later
66 $this->_conn = & $conn;
68 return true;
71 // close any open connections
72 function close()
74 // get the current connection
75 $conn = &$this->_conn;
77 // close it if it's open
78 if ($conn)
80 fclose($conn);
82 // cleanup the variable
83 unset($this->_conn);
84 return true;
86 return;
90 // Authenticate to the FirstClass server
91 function login($userid, $passwd)
93 // we did have a connection right?!
94 if ($this->_conn)
96 # Send username
97 fputs($this->_conn,"$userid\r\n");
99 $line = fgets ($this->_conn); //new line
100 $line = fgets ($this->_conn); //+0
101 $line = fgets ($this->_conn); //new line
103 # Send password
104 fputs($this->_conn,"$passwd\r\n");
105 $line = fgets ($this->_conn); //new line
106 $line = fgets ($this->_conn); //+0
107 $line = fgets ($this->_conn); //+0 or message
109 if ($this->_debug) echo $line;
111 if (preg_match ("/^\+0/", $line)) { //+0, user with subadmin privileges
112 $this->_user = $userid;
113 $this->_pwd = $passwd;
114 return TRUE;
115 } elseif (strpos($line, 'You are not allowed')) { // Denied access but a valid user and password
116 // "Sorry. You are not allowed to login with the FPP interface"
117 return TRUE;
118 } else { //Invalid user or password
119 return FALSE;
124 return FALSE;
127 // Get the list of groups the user is a member of
128 function getGroups($userid) {
130 $groups = array();
132 // we must be logged in as a user with subadmin privileges
133 if ($this->_conn AND $this->_user) {
134 # Send BA-command to get groups
135 fputs($this->_conn,"GET USER '" . $userid . "' 4 -1\r");
136 $line = "";
137 while (!$line) {
138 $line = trim(fgets ($this->_conn));
140 $n = 0;
141 while ($line AND !preg_match("/^\+0/", $line) AND $line != "-1003") {
142 list( , , $groups[$n++]) = explode(" ",$line,3);
143 $line = trim(fgets ($this->_conn));
145 if ($this->_debug) echo "getGroups:" . implode(",",$groups);
148 return $groups;
151 // Check if the user is member of any of the groups.
152 // Return the list of groups the user is member of.
153 function isMemberOf($userid, $groups) {
155 $usergroups = array_map("strtolower",$this->getGroups($userid));
156 $groups = array_map("strtolower",$groups);
158 $result = array_intersect($groups,$usergroups);
160 if ($this->_debug) echo "isMemberOf:" . implode(",",$result);
162 return $result;
166 function getUserInfo($userid, $field) {
168 $userinfo = "";
170 if ($this->_conn AND $this->_user) {
171 # Send BA-command to get data
172 fputs($this->_conn,"GET USER '" . $userid . "' " . $field . "\r");
173 $line = "";
174 while (!$line) {
175 $line = trim(fgets ($this->_conn));
177 $n = 0;
178 while ($line AND !preg_match("/^\+0/", $line)) {
179 list( , , $userinfo) = explode(" ",$line,3);
180 $line = trim(fgets ($this->_conn));
182 if ($this->_debug) echo "getUserInfo:" . $userinfo;
185 return str_replace('\r',' ',trim($userinfo,'"'));
189 function getResume($userid) {
191 $resume = "";
193 $pattern = "/\[.+:.+\..+\]/"; // Remove references to pictures in resumes
195 if ($this->_conn AND $this->_user) {
196 # Send BA-command to get data
197 fputs($this->_conn,"GET RESUME '" . $userid . "' 6\r");
198 $line = "";
199 while (!$line) {
200 $line = trim(fgets ($this->_conn));
202 $n = 0;
203 while ($line AND !preg_match("/^\+0/", $line)) {
204 $resume .= preg_replace($pattern,"",str_replace('\r',"\n",trim($line,'6 ')));
205 $line = trim(fgets ($this->_conn));
206 //print $line;
209 if ($this->_debug) echo "getResume:" . $resume;
212 return $resume;