2 /************************************************************************/
3 /* fcFPP: Php class for FirstClass Flexible Provisining Protocol */
4 /* ============================================================= */
6 /* Copyright (c) 2004 SKERIA Utveckling, Teknous */
7 /* http://skeria.skelleftea.se */
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 */
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
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
32 function fcFPP($host="localhost", $port="3333")
34 $this->_hostname
= $host;
40 // open a connection to the FirstClass server
43 if ($this->_debug
) echo "Connecting to host ";
44 $host = $this->_hostname
;
47 if ($this->_debug
) echo "[$host:$port]..";
49 // open the connection to the FirstClass server
50 $conn = fsockopen($host, $port, $errno, $errstr, 5);
53 print_error('auth_fcconnfail','auth', '', array($errno, $errstr));
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;
70 // close any open connections
73 // get the current connection
74 $conn = &$this->_conn
;
76 // close it if it's open
81 // cleanup the variable
89 // Authenticate to the FirstClass server
90 function login($userid, $passwd)
92 // we did have a connection right?!
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
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;
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"
117 } else { //Invalid user or password
126 // Get the list of groups the user is a member of
127 function getGroups($userid) {
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");
137 $line = trim(fgets ($this->_conn
));
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);
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);
165 function getUserInfo($userid, $field) {
169 if ($this->_conn
AND $this->_user
) {
170 # Send BA-command to get data
171 fputs($this->_conn
,"GET USER '" . $userid . "' " . $field . "\r");
174 $line = trim(fgets ($this->_conn
));
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) {
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");
199 $line = trim(fgets ($this->_conn
));
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
));
208 if ($this->_debug
) echo "getResume:" . $resume;