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 or any */
21 /************************************************************************/
22 /* Author: Torsten Anderson, torsten.anderson@skeria.skelleftea.se
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
33 function fcFPP($host="localhost", $port="3333")
35 $this->_hostname
= $host;
41 // open a connection to the FirstClass server
44 if ($this->_debug
) echo "Connecting to host ";
45 $host = $this->_hostname
;
48 if ($this->_debug
) echo "[$host:$port]..";
50 // open the connection to the FirstClass server
51 $conn = fsockopen($host, $port, $errno, $errstr, 5);
54 print_error('auth_fcconnfail','auth_fc', '', array('no'=>$errno, 'str'=>$errstr));
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;
71 // close any open connections
74 // get the current connection
75 $conn = &$this->_conn
;
77 // close it if it's open
82 // cleanup the variable
90 // Authenticate to the FirstClass server
91 function login($userid, $passwd)
93 // we did have a connection right?!
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
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;
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"
118 } else { //Invalid user or password
127 // Get the list of groups the user is a member of
128 function getGroups($userid) {
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");
138 $line = trim(fgets ($this->_conn
));
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);
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);
166 function getUserInfo($userid, $field) {
170 if ($this->_conn
AND $this->_user
) {
171 # Send BA-command to get data
172 fputs($this->_conn
,"GET USER '" . $userid . "' " . $field . "\r");
175 $line = trim(fgets ($this->_conn
));
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) {
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");
200 $line = trim(fgets ($this->_conn
));
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
));
209 if ($this->_debug
) echo "getResume:" . $resume;