[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/auth/fc/ -> fcFPP.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1