major whitespace cleanup - fixed \r line-ending
[moodle.git] / auth / fc / fcFPP.php
CommitLineData
03f5a0f8 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
22 */
23
24class 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
31 // class constructor
32 function fcFPP($host="localhost", $port="3333")
33 {
34 $this->_hostname = $host;
35 $this->_port = $port;
36 $this->_user = "";
37 $this->_pwd = "";
38 }
39
40 // open a connection to the FirstClass server
41 function open()
42 {
43 if($this->_debug) echo "Connecting to host ";
44 $host = $this->_hostname;
45 $port = $this->_port;
46
47 if($this->_debug) echo "[$host:$port]..";
48
49 // open the connection to the FirstClass server
50 $conn = fsockopen($host, $port, $errno, $errstr, 5);
51 if(!$conn)
52 {
53 echo "connection failed!".$errno. $errstr;
54 return false;
55 }
56
57 // We are connected
58 if($this->_debug) echo "connected!";
59
60 // Read connection message.
61 $line = fgets ($conn); //+0
62 $line = fgets ($conn); //new line
63
64 // store the connection in this class, so we can use it later
65 $this->_conn = & $conn;
66
67 return true;
68 }
69
70 // close any open connections
71 function close()
72 {
73 // get the current connection
74 $conn = &$this->_conn;
75
76 // close it if it's open
77 if($conn)
78 {
79 fclose($conn);
80
81 // cleanup the variable
82 unset($this->_conn);
83 return true;
84 }
85 return;
86 }
87
88
89 // Authenticate to the FirstClass server
90 function login($userid, $passwd)
91 {
92 // we did have a connection right?!
93 if($this->_conn)
94 {
95 # Send username
96 fputs($this->_conn,"$userid\r\n");
97
98 $line = fgets ($this->_conn); //new line
99 $line = fgets ($this->_conn); //+0
100 $line = fgets ($this->_conn); //new line
101
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
107
108 if($this->_debug) echo $line;
109
110 if (preg_match ("/^\+0/", $line)) { //+0, user with subadmin privileges
111 $this->_user = $userid;
112 $this->_pwd = $passwd;
113 return TRUE;
114 } elseif (preg_match ("/^\Sorry/",$line)){ //Denied access but a valid user and password
115 return TRUE;
116 } else { //Invalid user or password
117 return FALSE;
118 }
119
120
121 }
122 return FALSE;
123 }
124
125 // Get the list of groups the user is a member of
126 function getGroups($userid){
127
128 $groups = array();
129
130 // we must be logged in as a user with subadmin privileges
131 if($this->_conn AND $this->_user) {
132 # Send BA-command to get groups
133 fputs($this->_conn,"GET USER '" . $userid . "' 4 -1\r");
134 $line = "";
135 while (!$line) {
136 $line = trim(fgets ($this->_conn));
137 }
138 $n = 0;
139 while ($line AND !preg_match("/^\+0/", $line) AND $line != "-1003") {
140 list( , , $groups[$n++]) = explode(" ",$line,3);
141 $line = trim(fgets ($this->_conn));
142 }
143 if($this->_debug) echo "getGroups:" . implode(",",$groups);
144 }
145
146 return $groups;
147 }
148
149 // Check if the user is member of any of the groups.
150 // Return the list of groups the user is member of.
151 function isMemberOf($userid, $groups){
152
153 $usergroups = array_map("strtolower",$this->getGroups($userid));
154 $groups = array_map("strtolower",$groups);
155
156 $result = array_intersect($groups,$usergroups);
157
158 if($this->_debug) echo "isMemberOf:" . implode(",",$result);
159
160 return $result;
161
162 }
163
164 function getUserInfo($userid, $field){
165
166 $userinfo = "";
167
168 if($this->_conn AND $this->_user) {
169 # Send BA-command to get data
170 fputs($this->_conn,"GET USER '" . $userid . "' " . $field . "\r");
171 $line = "";
172 while (!$line) {
173 $line = trim(fgets ($this->_conn));
174 }
175 $n = 0;
176 while ($line AND !preg_match("/^\+0/", $line)) {
177 list( , , $userinfo) = explode(" ",$line,3);
178 $line = trim(fgets ($this->_conn));
179 }
180 if($this->_debug) echo "getUserInfo:" . $userinfo;
181 }
182
183 return str_replace('\r',' ',trim($userinfo,'"'));
184
185 }
186
187 function getResume($userid){
188
189 $resume = "";
190
191 $pattern = "/\[.+:.+\..+\]/"; // Remove references to pictures in resumes
192
193 if($this->_conn AND $this->_user) {
194 # Send BA-command to get data
195 fputs($this->_conn,"GET RESUME '" . $userid . "' 6\r");
196 $line = "";
197 while (!$line) {
198 $line = trim(fgets ($this->_conn));
199 }
200 $n = 0;
201 while ($line AND !preg_match("/^\+0/", $line)) {
202 $resume .= preg_replace($pattern,"",str_replace('\r',"\n",trim($line,'6 ')));
203 $line = trim(fgets ($this->_conn));
204 //print $line;
205
206 }
207 if($this->_debug) echo "getResume:" . $resume;
208 }
209
210 return $resume;
211
212 }
213
214
215}
216
217
1e9e22e8 218?>