test commit for lineending cleanup
[moodle.git] / auth / fc / fcFPP.php
CommitLineData
1e9e22e8 1<?php\r
2/************************************************************************/\r
3/* fcFPP: Php class for FirstClass Flexible Provisining Protocol */\r
4/* ============================================================= */\r
5/* */\r
6/* Copyright (c) 2004 SKERIA Utveckling, Teknous */\r
7/* http://skeria.skelleftea.se */\r
8/* */\r
9/* Flexible Provisioning Protocol is a real-time, IP based protocol */\r
10/* which provides direct access to the scriptable remote administration */\r
11/* subsystem of the core FirstClass Server. Using FPP, it is possible to*/\r
12/* implement automated provisioning and administration systems for */\r
13/* FirstClass, avoiding the need for a point and click GUI. FPP can also*/\r
14/* be used to integrate FirstClass components into a larger unified */\r
15/* system. */\r
16/* */\r
17/* This program is free software. You can redistribute it and/or modify */\r
18/* it under the terms of the GNU General Public License as published by */\r
19/* the Free Software Foundation; either version 2 of the License. */\r
20/************************************************************************/\r
21/* Author: Torsten Anderson, torsten.anderson@skeria.skelleftea.se\r
22 */\r
23\r
24class fcFPP\r
25{ \r
26 var $_hostname; // hostname of FirstClass server we are connection to\r
27 var $_port; // port on which fpp is running\r
28 var $_conn = 0; // socket we are connecting on\r
29 var $_debug = FALSE; // set to true to see some debug info\r
30 \r
31 // class constructor\r
32 function fcFPP($host="localhost", $port="3333")\r
33 {\r
34 $this->_hostname = $host;\r
35 $this->_port = $port;\r
36 $this->_user = "";\r
37 $this->_pwd = ""; \r
38 }\r
39 \r
40 // open a connection to the FirstClass server\r
41 function open()\r
42 {\r
43 if($this->_debug) echo "Connecting to host ";\r
44 $host = $this->_hostname;\r
45 $port = $this->_port;\r
46\r
47 if($this->_debug) echo "[$host:$port]..";\r
48\r
49 // open the connection to the FirstClass server\r
50 $conn = fsockopen($host, $port, $errno, $errstr, 5);\r
51 if(!$conn)\r
52 {\r
53 echo "connection failed!".$errno. $errstr;\r
54 return false;\r
55 }\r
56 \r
57 // We are connected\r
58 if($this->_debug) echo "connected!";\r
59 \r
60 // Read connection message.\r
61 $line = fgets ($conn); //+0\r
62 $line = fgets ($conn); //new line\r
63\r
64 // store the connection in this class, so we can use it later\r
65 $this->_conn = & $conn;\r
66\r
67 return true;\r
68 }\r
69\r
70 // close any open connections\r
71 function close()\r
72 { \r
73 // get the current connection\r
74 $conn = &$this->_conn;\r
75\r
76 // close it if it's open\r
77 if($conn)\r
78 {\r
79 fclose($conn);\r
80\r
81 // cleanup the variable\r
82 unset($this->_conn);\r
83 return true;\r
84 }\r
85 return;\r
86 }\r
87 \r
88 \r
89 // Authenticate to the FirstClass server\r
90 function login($userid, $passwd)\r
91 {\r
92 // we did have a connection right?!\r
93 if($this->_conn)\r
94 {\r
95 # Send username\r
96 fputs($this->_conn,"$userid\r\n");\r
97\r
98 $line = fgets ($this->_conn); //new line\r
99 $line = fgets ($this->_conn); //+0\r
100 $line = fgets ($this->_conn); //new line\r
101 \r
102 # Send password\r
103 fputs($this->_conn,"$passwd\r\n");\r
104 $line = fgets ($this->_conn); //new line\r
105 $line = fgets ($this->_conn); //+0\r
106 $line = fgets ($this->_conn); //+0 or message\r
107 \r
108 if($this->_debug) echo $line;\r
109 \r
110 if (preg_match ("/^\+0/", $line)) { //+0, user with subadmin privileges\r
111 $this->_user = $userid;\r
112 $this->_pwd = $passwd; \r
113 return TRUE; \r
114 } elseif (preg_match ("/^\Sorry/",$line)){ //Denied access but a valid user and password\r
115 return TRUE;\r
116 } else { //Invalid user or password\r
117 return FALSE;\r
118 }\r
119 \r
120\r
121 }\r
122 return FALSE;\r
123 }\r
124\r
125 // Get the list of groups the user is a member of \r
126 function getGroups($userid){\r
127 \r
128 $groups = array();\r
129 \r
130 // we must be logged in as a user with subadmin privileges \r
131 if($this->_conn AND $this->_user) {\r
132 # Send BA-command to get groups\r
133 fputs($this->_conn,"GET USER '" . $userid . "' 4 -1\r");\r
134 $line = "";\r
135 while (!$line) {\r
136 $line = trim(fgets ($this->_conn));\r
137 }\r
138 $n = 0;\r
139 while ($line AND !preg_match("/^\+0/", $line) AND $line != "-1003") {\r
140 list( , , $groups[$n++]) = explode(" ",$line,3);\r
141 $line = trim(fgets ($this->_conn));\r
142 }\r
143 if($this->_debug) echo "getGroups:" . implode(",",$groups);\r
144 }\r
145 \r
146 return $groups;\r
147 }\r
148\r
149 // Check if the user is member of any of the groups.\r
150 // Return the list of groups the user is member of.\r
151 function isMemberOf($userid, $groups){\r
152 \r
153 $usergroups = array_map("strtolower",$this->getGroups($userid));\r
154 $groups = array_map("strtolower",$groups);\r
155 \r
156 $result = array_intersect($groups,$usergroups);\r
157 \r
158 if($this->_debug) echo "isMemberOf:" . implode(",",$result);\r
159 \r
160 return $result;\r
161\r
162 }\r
163 \r
164 function getUserInfo($userid, $field){\r
165 \r
166 $userinfo = "";\r
167 \r
168 if($this->_conn AND $this->_user) {\r
169 # Send BA-command to get data\r
170 fputs($this->_conn,"GET USER '" . $userid . "' " . $field . "\r");\r
171 $line = "";\r
172 while (!$line) {\r
173 $line = trim(fgets ($this->_conn));\r
174 }\r
175 $n = 0;\r
176 while ($line AND !preg_match("/^\+0/", $line)) {\r
177 list( , , $userinfo) = explode(" ",$line,3);\r
178 $line = trim(fgets ($this->_conn));\r
179 }\r
180 if($this->_debug) echo "getUserInfo:" . $userinfo;\r
181 }\r
182 \r
183 return str_replace('\r',' ',trim($userinfo,'"'));\r
184\r
185 }\r
186\r
187 function getResume($userid){\r
188 \r
189 $resume = "";\r
190\r
191 $pattern = "/\[.+:.+\..+\]/"; // Remove references to pictures in resumes\r
192 \r
193 if($this->_conn AND $this->_user) {\r
194 # Send BA-command to get data\r
195 fputs($this->_conn,"GET RESUME '" . $userid . "' 6\r");\r
196 $line = "";\r
197 while (!$line) {\r
198 $line = trim(fgets ($this->_conn));\r
199 }\r
200 $n = 0;\r
201 while ($line AND !preg_match("/^\+0/", $line)) {\r
202 $resume .= preg_replace($pattern,"",str_replace('\r',"\n",trim($line,'6 ')));\r
203 $line = trim(fgets ($this->_conn));\r
204 //print $line;\r
205 \r
206 }\r
207 if($this->_debug) echo "getResume:" . $resume;\r
208 }\r
209 \r
210 return $resume;\r
211\r
212 }\r
213 \r
214 \r
215}\r
216 \r
217\r
218?>