Example #1
0
 /**
  * Initializes a new instance of the broker from specified parameters
  *
  * @param array $params An array with connect information
  * @return Keruald\Broker\AMQPBroker A connected instance of the broker
  */
 public static function makeFromConfig($params)
 {
     $instance = new self();
     $params += self::getDefaultValues();
     $instance->connect($params['host'], $params['port'], $params['username'], $params['password'], $params['vhost']);
     return $instance;
 }
Example #2
0
 public function connect($params = array())
 {
     if (!isset($this->status)) {
         $obj = new self($params);
         if ($obj->connect()) {
             $obj->status = SMTP_STATUS_CONNECTED;
         }
         return $obj;
     } else {
         if (!empty($GLOBALS['_CFG']['smtp_ssl'])) {
             $this->host = 'ssl://' . $this->host;
         }
         $this->connection = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
         if ($this->connection === false) {
             $this->errors[] = 'Access is denied.';
             return false;
         }
         @socket_set_timeout($this->connection, 0, 250000);
         $greeting = $this->get_data();
         if (is_resource($this->connection)) {
             $this->status = 2;
             return $this->auth ? $this->ehlo() : $this->helo();
         } else {
             log_write($errstr, __FILE__, __LINE__);
             $this->errors[] = 'Failed to connect to server: ' . $errstr;
             return false;
         }
     }
 }
Example #3
0
 public static function singleton()
 {
     if (is_null(self::$singleton)) {
         $driver = new self();
         return $driver->connect('config', array('dir' => CONFIG_PATH, 'humanReadable' => true));
     }
     return self::$singleton;
 }
Example #4
0
 public static function initServer($config)
 {
     $class = new self();
     $class->connect($config['host'] . (empty($config['port']) ? '' : ':' . $config['port']), $config['user'], $config['pass']);
     if (!empty($config['dbname'])) {
         $class->select_db($config['dbname']);
     }
     return $class;
 }
Example #5
0
 /**
  * @return bool
  */
 private static function getCache()
 {
     if (!Globals::get('MongularCache')) {
         return FALSE;
     }
     try {
         $cache = new self();
         if ($cache->connect()) {
             return $cache;
         }
         return FALSE;
     } catch (\Exception $e) {
         return FALSE;
     }
 }
 /** Converts an array of parameters into a query string to be appended to a URL.
  *
  * @param $group_dn        dn of the group to import
  * @param $options   array for
  *             - authldaps_id
  *             - entities_id where group must to be imported
  *             - is_recursive
  *
  * @return  nothing
  **/
 static function ldapImportGroup($group_dn, $options = array())
 {
     $config_ldap = new self();
     $res = $config_ldap->getFromDB($options['authldaps_id']);
     $ldap_users = array();
     $group_dn = $group_dn;
     // we prevent some delay...
     if (!$res) {
         return false;
     }
     //Connect to the directory
     $ds = $config_ldap->connect();
     if ($ds) {
         $group_infos = self::getGroupByDn($ds, stripslashes($group_dn));
         $group = new Group();
         if ($options['type'] == "groups") {
             return $group->add(array("name" => addslashes($group_infos["cn"][0]), "ldap_group_dn" => addslashes($group_infos["dn"]), "entities_id" => $options['entities_id'], "is_recursive" => $options['is_recursive']));
         }
         return $group->add(array("name" => addslashes($group_infos["cn"][0]), "ldap_field" => $config_ldap->fields["group_field"], "ldap_value" => addslashes($group_infos["dn"]), "entities_id" => $options['entities_id'], "is_recursive" => $options['is_recursive']));
     }
     return false;
 }
 /** Get the list of LDAP users to add/synchronize
  *
  * @param $options array options
  *          - authldaps_id ID of the server to use
  *          - mode user to synchronise or add ?
  *          - ldap_filter ldap filter to use
  *          - basedn force basedn (default authldaps_id one)
  *          - order display order
  *          - operator operator used to limit user updates days
  *          - days number of days to limit (with operator)
  *          - script true if called by an external script
  * @param $results result stats
  * @param $limitexceeded limit exceeded exception
  *
  * @return  array of the user
  **/
 static function getAllUsers($options = array(), &$results, &$limitexceeded)
 {
     global $DB, $LANG, $CFG_GLPI;
     $config_ldap = new self();
     $res = $config_ldap->getFromDB($options['authldaps_id']);
     $values['order'] = 'DESC';
     $values['mode'] = self::ACTION_SYNCHRONIZE;
     $values['ldap_filter'] = '';
     $values['basedn'] = $config_ldap->fields['basedn'];
     $values['days'] = 0;
     $values['operator'] = '<';
     //Called by an external script or not
     $values['script'] = 0;
     // TODO change loop ? : foreach ($values...) if isset($options[...])
     foreach ($options as $option => $value) {
         // this test break mode detection - if ($value != '') {
         $values[$option] = $value;
         //}
     }
     $ldap_users = array();
     $limitexceeded = false;
     // we prevent some delay...
     if (!$res) {
         return false;
     }
     if ($values['order'] != "DESC") {
         $values['order'] = "ASC";
     }
     $ds = $config_ldap->connect();
     if ($ds) {
         //Search for ldap login AND modifyTimestamp,
         //which indicates the last update of the object in directory
         $attrs = array($config_ldap->fields['login_field'], "modifyTimestamp");
         // Tenter une recherche pour essayer de retrouver le DN
         if ($values['ldap_filter'] == '') {
             $filter = "(" . $config_ldap->fields['login_field'] . "=*)";
         } else {
             $filter = $values['ldap_filter'];
         }
         if ($values['script'] && $values['days']) {
             $filter_timestamp = self::addTimestampRestrictions($values['operator'], $values['days']);
             $filter = "(&{$filter} {$filter_timestamp})";
         }
         $sr = @ldap_search($ds, $values['basedn'], $filter, $attrs);
         if ($sr) {
             if (in_array(ldap_errno($ds), array(4, 11))) {
                 // openldap return 4 for Size limit exceeded
                 $limitexceeded = true;
             }
             $info = ldap_get_entries_clean($ds, $sr);
             if (in_array(ldap_errno($ds), array(4, 11))) {
                 $limitexceeded = true;
             }
             $user_infos = array();
             for ($ligne = 0; $ligne < $info["count"]; $ligne++) {
                 //If ldap add
                 if ($values['mode'] == self::ACTION_IMPORT) {
                     if (in_array($config_ldap->fields['login_field'], $info[$ligne])) {
                         $ldap_users[$info[$ligne][$config_ldap->fields['login_field']][0]] = $info[$ligne][$config_ldap->fields['login_field']][0];
                         $user_infos[$info[$ligne][$config_ldap->fields['login_field']][0]]["timestamp"] = self::ldapStamp2UnixStamp($info[$ligne]['modifytimestamp'][0], $config_ldap->fields['time_offset']);
                     }
                 } else {
                     //If ldap synchronisation
                     if (in_array($config_ldap->fields['login_field'], $info[$ligne])) {
                         $ldap_users[$info[$ligne][$config_ldap->fields['login_field']][0]] = self::ldapStamp2UnixStamp($info[$ligne]['modifytimestamp'][0], $config_ldap->fields['time_offset']);
                         $user_infos[$info[$ligne][$config_ldap->fields['login_field']][0]]["timestamp"] = self::ldapStamp2UnixStamp($info[$ligne]['modifytimestamp'][0], $config_ldap->fields['time_offset']);
                     }
                 }
             }
         } else {
             return false;
         }
     } else {
         return false;
     }
     $glpi_users = array();
     $sql = "SELECT *\n                     FROM `glpi_users`";
     if ($values['mode'] != self::ACTION_IMPORT) {
         $sql .= " WHERE `authtype` IN (-1," . Auth::LDAP . "," . Auth::EXTERNAL . ", " . Auth::CAS . ")\n                         AND `auths_id` = '" . $options['authldaps_id'] . "'";
     }
     $sql .= " ORDER BY `name` " . $values['order'];
     foreach ($DB->request($sql) as $user) {
         //Ldap add : fill the array with the login of the user
         if ($values['mode'] == self::ACTION_IMPORT) {
             $glpi_users[$user['name']] = $user['name'];
         } else {
             //Ldap synchronisation : look if the user exists in the directory
             //and compares the modifications dates (ldap and glpi db)
             if (!empty($ldap_users[$user['name']])) {
                 //If entry was modified or if script should synchronize all the users
                 if ($values['action'] == self::ACTION_ALL || $ldap_users[$user['name']] - strtotime($user['date_sync']) > 0) {
                     $glpi_users[] = array('id' => $user['id'], 'user' => $user['name'], 'timestamp' => $user_infos[$user['name']]['timestamp'], 'date_sync' => $user['date_sync']);
                 }
                 // Only manage deleted user if ALL (because of entity visibility in delegated mode)
             } else {
                 if ($values['action'] == self::ACTION_ALL && !$limitexceeded) {
                     //If user is marked as coming from LDAP, but is not present in it anymore
                     if (!$user['is_deleted'] && $user['auths_id'] == $options['ldapservers_id']) {
                         User::manageDeletedUserInLdap($user['id']);
                         $results[self::USER_DELETED_LDAP]++;
                     }
                 }
             }
         }
     }
     //If add, do the difference between ldap users and glpi users
     if ($values['mode'] == self::ACTION_IMPORT) {
         $diff = array_diff_ukey($ldap_users, $glpi_users, 'strcasecmp');
         $list = array();
         foreach ($diff as $user) {
             $list[] = array("user" => $user, "timestamp" => $user_infos[$user]["timestamp"], "date_sync" => DROPDOWN_EMPTY_VALUE);
         }
         if ($values['order'] == 'DESC') {
             rsort($list);
         } else {
             sort($list);
         }
         return $list;
     }
     return $glpi_users;
 }
Example #8
0
 /**
  * @param array $params
  * @return UTIL_Ftp
  */
 public static function getConnection(array $params)
 {
     if (!function_exists('ftp_connect')) {
         throw new LogicException(self::ERROR_FTP_FUNCTION_IS_NOT_AVAILABLE);
     }
     if (empty($params['host'])) {
         throw new InvalidArgumentException(self::ERROR_EMPTY_HOST_PROVIDED);
     }
     if (empty($params['login']) || empty($params['password'])) {
         throw new InvalidArgumentException(self::ERROR_EMPTY_CREDENTIALS_PROVIDED);
     }
     $connection = new self();
     if (!empty($params['timeout'])) {
         $connection->setTimeout((int) $params['timeout']);
     }
     if (!$connection->connect(trim($params['host']), !empty($params['port']) ? (int) $params['port'] : 21)) {
         throw new LogicException(self::ERROR_CANT_CONNECT_TO_HOST);
     }
     if (!$connection->login(trim($params['login']), trim($params['password']))) {
         throw new LogicException(self::ERROR_INVALID_CREDENTIALS_PROVIDED);
     }
     $connection->init();
     return $connection;
 }
Example #9
0
 public static function Dispose($cache_name, $options = array())
 {
     unset($options["save_on_destruction"]);
     $mc = new self($cache_name, $options);
     if (!$mc->is_dirty()) {
         return;
     }
     $mc->connect();
     if (!($res = $mc->mem_cache_instace->replace($mc->genKey(), $mc->get_internal_cache()))) {
         $res = $mc->mem_cache_instace->set($mc->genKey(), $mc->get_internal_cache());
     }
     if ($mc->mem_cache_instace->get($mc->genKey()) != $mc->get_internal_cache()) {
         $mc->disconnect();
         throw new \Exception("It seems race condition happened on saving cache updates, updates not saved!");
     }
     $mc->disconnect();
 }
Example #10
0
 /**
  * 
  * @param common_ext_Extension $extension
  * @return common_persistence_KeyValuePersistence
  */
 public static function getPersistence(common_ext_Extension $extension)
 {
     $driver = new self($extension);
     return $driver->connect('config_' . $extension->getId(), array('dir' => CONFIG_PATH . $extension->getId(), 'humanReadable' => true));
 }
 /**
  * Static initializer.
  *
  * Returns IMAP resource on success using the connect() method.
  *
  * For parameters, see constructor.
  *
  * @return resource|bool The IMAP resource on success. Boolean false on failure.
  */
 public static function init($args = array(), $connection = false)
 {
     $instance = new self($args, $connection);
     return $instance->connect();
 }
Example #12
0
 /** @ignore */
 public static function __test()
 {
     #parent::$debug = true;
     error_reporting(E_ALL);
     # Test basics and GET
     $c = new self('http://hunch.se/');
     $c->method = 'GeT';
     $c->url = 'http://apple.spotify.net/ping.php';
     $c->connect();
     assert($c->responseProtocol == '1.1');
     assert((int) ($c->responseStatus / 100) == 2);
     # Test POST
     $c->method = 'POST';
     $c->connect('unittest=1');
     # Test reset
     $c->url = 'http://apple.spotify.net/ping.php';
     $c->method = 'post';
     assert($c->connect('unittest=2'));
     # Test outputStream
     $c->outputStream = new StringOutputStream();
     $c->url = 'http://apple.spotify.net/ping.php?unittest=3';
     $c->method = 'GET';
     assert($c->connect());
     assert($c->outputStream->string != '');
     # Test auth
     #$c->method = 'GET';
     #$c->url = 'http://*****:*****@apple.spotify.net/ui/?unittest=4';
     #$c->connect();
     # Test unsupported protocol error
     $c->url = 'azbx://something';
     try {
         $c->connect();
         assert(!'We should have gotten an exception thrown at us');
     } catch (Exception $e) {
         assert($e instanceof IllegalArgumentException);
     }
     # Test connection timeout handling
     $c->url = 'http://127.126.125.124/no-such-host/';
     $c->curlOptions[CURLOPT_CONNECTTIMEOUT] = 1;
     try {
         $c->connect();
         assert(!'We should have gotten an exception thrown at us');
     } catch (Exception $e) {
         assert($e instanceof ConnectException);
     }
 }
Example #13
0
 /**
  * @param array $params
  * @return UTIL_Ftp
  */
 public static function getConnection(array $params)
 {
     if (empty($params['host'])) {
         throw new InvalidArgumentException('Empty host provided for connection');
     }
     $connection = new self();
     if (!empty($params['timeout'])) {
         $connection->setTimeout((int) $params['timeout']);
     }
     $connected = $connection->connect(trim($params['host']), !empty($params['port']) ? (int) $params['port'] : 21);
     if (!$connected) {
         throw new LogicException("Can't connect to host `" . trim($params['host']) . "` by FTP");
     }
     if (!empty($params['login']) && !empty($params['password'])) {
         $connection->login(trim($params['login']), trim($params['password']));
     }
     $connection->init();
     return $connection;
 }