File: fcFPP.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (218 lines) | stat: -rw-r--r-- 6,536 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/************************************************************************/
/* fcFPP: Php class for FirstClass Flexible Provisining Protocol        */
/* =============================================================        */
/*                                                                      */
/* Copyright (c) 2004 SKERIA Utveckling, Teknous                        */
/* http://skeria.skelleftea.se                                          */
/*                                                                      */
/* Flexible Provisioning Protocol is a real-time, IP based protocol     */
/* which provides direct access to the scriptable remote administration */
/* subsystem of the core FirstClass Server. Using FPP, it is possible to*/
/* implement automated provisioning and administration systems for      */
/* FirstClass, avoiding the need for a point and click GUI. FPP can also*/
/* be used to integrate FirstClass components into a larger unified     */
/* system.                                                              */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Author: Torsten Anderson, torsten.anderson@skeria.skelleftea.se
 */

class fcFPP
{    
    var $_hostname;         // hostname of FirstClass server we are connection to
    var $_port;             // port on which fpp is running
    var $_conn = 0;         // socket we are connecting on
    var $_debug = FALSE;    // set to true to see some debug info
    
    // class constructor
    function fcFPP($host="localhost", $port="3333")
    {
	$this->_hostname = $host;
	$this->_port = $port;
	$this->_user = "";
	$this->_pwd = "";  
    }
    
    // open a connection to the FirstClass server
    function open()
    {
	if($this->_debug) echo "Connecting to host ";
	$host = $this->_hostname;
	$port = $this->_port;

	if($this->_debug) echo "[$host:$port]..";

	// open the connection to the FirstClass server
	$conn = fsockopen($host, $port, $errno, $errstr, 5);
	if(!$conn)
	{
	    echo "connection failed!".$errno. $errstr;
	    return false;
	}
	
	// We are connected
	if($this->_debug) echo "connected!";
	
	// Read connection message.
	$line = fgets ($conn);        //+0
	$line = fgets ($conn);        //new line

	// store the connection in this class, so we can use it later
	$this->_conn = & $conn;

	return true;
    }

    // close any open connections
    function close()
    {        
	// get the current connection
	$conn = &$this->_conn;

	// close it if it's open
    	if($conn)
	{
	    fclose($conn);

	    // cleanup the variable
	    unset($this->_conn);
	    return true;
	}
	return;
    }
    
    
    // Authenticate to the FirstClass server
    function login($userid, $passwd)
    {
	// we did have a connection right?!
        if($this->_conn)
	{
	    # Send username
	    fputs($this->_conn,"$userid\r\n");

	    $line = fgets ($this->_conn);        //new line
	    $line = fgets ($this->_conn);        //+0
	    $line = fgets ($this->_conn);        //new line
    
	    # Send password
	    fputs($this->_conn,"$passwd\r\n");
	    $line = fgets ($this->_conn);        //new line
	    $line = fgets ($this->_conn);        //+0
	    $line = fgets ($this->_conn);        //+0 or message
	    
	    if($this->_debug) echo $line;
	    
	    if (preg_match ("/^\+0/", $line)) {  	//+0, user with subadmin privileges
		$this->_user = $userid;
		$this->_pwd  = $passwd; 
		return TRUE;		
	    } elseif (preg_match ("/^\Sorry/",$line)){	//Denied access but a valid user and password
	    	return TRUE;
	    } else {					//Invalid user or password
		return FALSE;
	    }
    

	}
	return FALSE;
    }

    // Get the list of groups the user is a member of 
    function getGroups($userid){
	
	$groups = array();
	
	// we must be logged in as a user with subadmin privileges 
	if($this->_conn AND $this->_user) {
	    # Send BA-command to get groups
	    fputs($this->_conn,"GET USER '" . $userid . "' 4 -1\r");
	    $line = "";
	    while (!$line) {
		$line = trim(fgets ($this->_conn));
	    }
	    $n = 0;
	    while ($line AND !preg_match("/^\+0/", $line) AND $line != "-1003") {
		list( , , $groups[$n++]) = explode(" ",$line,3);
		$line = trim(fgets ($this->_conn));
	    }
            if($this->_debug) echo "getGroups:" . implode(",",$groups);
	}
	
	return $groups;
    }

    // Check if the user is member of any of the groups.
    // Return the list of groups the user is member of.
    function isMemberOf($userid, $groups){
	
	$usergroups = array_map("strtolower",$this->getGroups($userid));
	$groups = array_map("strtolower",$groups);
	
	$result = array_intersect($groups,$usergroups);
	
        if($this->_debug) echo "isMemberOf:" . implode(",",$result);
	
	return $result;

    }
    
    function getUserInfo($userid, $field){
	
	$userinfo = "";
	
	if($this->_conn AND $this->_user) {
	    # Send BA-command to get data
	    fputs($this->_conn,"GET USER '" . $userid . "' " . $field . "\r");
	    $line = "";
	    while (!$line) {
	        $line = trim(fgets ($this->_conn));
	    }
	    $n = 0;
	    while ($line AND !preg_match("/^\+0/", $line)) {
		list( , , $userinfo) = explode(" ",$line,3);
		$line = trim(fgets ($this->_conn));
	    }
	    if($this->_debug) echo "getUserInfo:" . $userinfo;
	}
	  
	return str_replace('\r',' ',trim($userinfo,'"'));

    }

    function getResume($userid){
	
	$resume = "";

	$pattern = "/\[.+:.+\..+\]/";         // Remove references to pictures in resumes
	
	if($this->_conn AND $this->_user) {
	    # Send BA-command to get data
	    fputs($this->_conn,"GET RESUME '" . $userid . "' 6\r");
	    $line = "";
	    while (!$line) {
               $line = trim(fgets ($this->_conn));
	    }
	    $n = 0;
	    while ($line AND !preg_match("/^\+0/", $line)) {
	    	$resume .= preg_replace($pattern,"",str_replace('\r',"\n",trim($line,'6 ')));
		$line = trim(fgets ($this->_conn));
		//print $line;
		
	    }
	    if($this->_debug) echo "getResume:" . $resume;
	}
	  
	return $resume;

    }
    
    
}
   

?>