|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /************************************************************************/ 00003 /* fcFPP: Php class for FirstClass Flexible Provisining Protocol */ 00004 /* ============================================================= */ 00005 /* */ 00006 /* Copyright (c) 2004 SKERIA Utveckling, Teknous */ 00007 /* http://skeria.skelleftea.se */ 00008 /* */ 00009 /* Flexible Provisioning Protocol is a real-time, IP based protocol */ 00010 /* which provides direct access to the scriptable remote administration */ 00011 /* subsystem of the core FirstClass Server. Using FPP, it is possible to*/ 00012 /* implement automated provisioning and administration systems for */ 00013 /* FirstClass, avoiding the need for a point and click GUI. FPP can also*/ 00014 /* be used to integrate FirstClass components into a larger unified */ 00015 /* system. */ 00016 /* */ 00017 /* This program is free software. You can redistribute it and/or modify */ 00018 /* it under the terms of the GNU General Public License as published by */ 00019 /* the Free Software Foundation; either version 2 of the License or any */ 00020 /* later version. */ 00021 /************************************************************************/ 00022 /* Author: Torsten Anderson, torsten.anderson@skeria.skelleftea.se 00023 */ 00024 00025 class fcFPP 00026 { 00027 var $_hostname; // hostname of FirstClass server we are connection to 00028 var $_port; // port on which fpp is running 00029 var $_conn = 0; // socket we are connecting on 00030 var $_debug = FALSE; // set to true to see some debug info 00031 00032 // class constructor 00033 function fcFPP($host="localhost", $port="3333") 00034 { 00035 $this->_hostname = $host; 00036 $this->_port = $port; 00037 $this->_user = ""; 00038 $this->_pwd = ""; 00039 } 00040 00041 // open a connection to the FirstClass server 00042 function open() 00043 { 00044 if ($this->_debug) echo "Connecting to host "; 00045 $host = $this->_hostname; 00046 $port = $this->_port; 00047 00048 if ($this->_debug) echo "[$host:$port].."; 00049 00050 // open the connection to the FirstClass server 00051 $conn = fsockopen($host, $port, $errno, $errstr, 5); 00052 if (!$conn) 00053 { 00054 print_error('auth_fcconnfail','auth_fc', '', array('no'=>$errno, 'str'=>$errstr)); 00055 return false; 00056 } 00057 00058 // We are connected 00059 if ($this->_debug) echo "connected!"; 00060 00061 // Read connection message. 00062 $line = fgets ($conn); //+0 00063 $line = fgets ($conn); //new line 00064 00065 // store the connection in this class, so we can use it later 00066 $this->_conn = & $conn; 00067 00068 return true; 00069 } 00070 00071 // close any open connections 00072 function close() 00073 { 00074 // get the current connection 00075 $conn = &$this->_conn; 00076 00077 // close it if it's open 00078 if ($conn) 00079 { 00080 fclose($conn); 00081 00082 // cleanup the variable 00083 unset($this->_conn); 00084 return true; 00085 } 00086 return; 00087 } 00088 00089 00090 // Authenticate to the FirstClass server 00091 function login($userid, $passwd) 00092 { 00093 // we did have a connection right?! 00094 if ($this->_conn) 00095 { 00096 # Send username 00097 fputs($this->_conn,"$userid\r\n"); 00098 00099 $line = fgets ($this->_conn); //new line 00100 $line = fgets ($this->_conn); //+0 00101 $line = fgets ($this->_conn); //new line 00102 00103 # Send password 00104 fputs($this->_conn,"$passwd\r\n"); 00105 $line = fgets ($this->_conn); //new line 00106 $line = fgets ($this->_conn); //+0 00107 $line = fgets ($this->_conn); //+0 or message 00108 00109 if ($this->_debug) echo $line; 00110 00111 if (preg_match ("/^\+0/", $line)) { //+0, user with subadmin privileges 00112 $this->_user = $userid; 00113 $this->_pwd = $passwd; 00114 return TRUE; 00115 } elseif (strpos($line, 'You are not allowed')) { // Denied access but a valid user and password 00116 // "Sorry. You are not allowed to login with the FPP interface" 00117 return TRUE; 00118 } else { //Invalid user or password 00119 return FALSE; 00120 } 00121 00122 00123 } 00124 return FALSE; 00125 } 00126 00127 // Get the list of groups the user is a member of 00128 function getGroups($userid) { 00129 00130 $groups = array(); 00131 00132 // we must be logged in as a user with subadmin privileges 00133 if ($this->_conn AND $this->_user) { 00134 # Send BA-command to get groups 00135 fputs($this->_conn,"GET USER '" . $userid . "' 4 -1\r"); 00136 $line = ""; 00137 while (!$line) { 00138 $line = trim(fgets ($this->_conn)); 00139 } 00140 $n = 0; 00141 while ($line AND !preg_match("/^\+0/", $line) AND $line != "-1003") { 00142 list( , , $groups[$n++]) = explode(" ",$line,3); 00143 $line = trim(fgets ($this->_conn)); 00144 } 00145 if ($this->_debug) echo "getGroups:" . implode(",",$groups); 00146 } 00147 00148 return $groups; 00149 } 00150 00151 // Check if the user is member of any of the groups. 00152 // Return the list of groups the user is member of. 00153 function isMemberOf($userid, $groups) { 00154 00155 $usergroups = array_map("strtolower",$this->getGroups($userid)); 00156 $groups = array_map("strtolower",$groups); 00157 00158 $result = array_intersect($groups,$usergroups); 00159 00160 if ($this->_debug) echo "isMemberOf:" . implode(",",$result); 00161 00162 return $result; 00163 00164 } 00165 00166 function getUserInfo($userid, $field) { 00167 00168 $userinfo = ""; 00169 00170 if ($this->_conn AND $this->_user) { 00171 # Send BA-command to get data 00172 fputs($this->_conn,"GET USER '" . $userid . "' " . $field . "\r"); 00173 $line = ""; 00174 while (!$line) { 00175 $line = trim(fgets ($this->_conn)); 00176 } 00177 $n = 0; 00178 while ($line AND !preg_match("/^\+0/", $line)) { 00179 list( , , $userinfo) = explode(" ",$line,3); 00180 $line = trim(fgets ($this->_conn)); 00181 } 00182 if ($this->_debug) echo "getUserInfo:" . $userinfo; 00183 } 00184 00185 return str_replace('\r',' ',trim($userinfo,'"')); 00186 00187 } 00188 00189 function getResume($userid) { 00190 00191 $resume = ""; 00192 00193 $pattern = "/\[.+:.+\..+\]/"; // Remove references to pictures in resumes 00194 00195 if ($this->_conn AND $this->_user) { 00196 # Send BA-command to get data 00197 fputs($this->_conn,"GET RESUME '" . $userid . "' 6\r"); 00198 $line = ""; 00199 while (!$line) { 00200 $line = trim(fgets ($this->_conn)); 00201 } 00202 $n = 0; 00203 while ($line AND !preg_match("/^\+0/", $line)) { 00204 $resume .= preg_replace($pattern,"",str_replace('\r',"\n",trim($line,'6 '))); 00205 $line = trim(fgets ($this->_conn)); 00206 //print $line; 00207 00208 } 00209 if ($this->_debug) echo "getResume:" . $resume; 00210 } 00211 00212 return $resume; 00213 00214 } 00215 00216 00217 } 00218 00219 00220 ?>