Index: trunk/extensions/ExternalData/ED_Utils.php |
— | — | @@ -9,7 +9,7 @@ |
10 | 10 | |
11 | 11 | class EDUtils { |
12 | 12 | // how many times to try an HTTP request |
13 | | - private $http_number_of_tries=3; |
| 13 | + private static $http_number_of_tries=3; |
14 | 14 | |
15 | 15 | // XML-handling functions based on code found at |
16 | 16 | // http://us.php.net/xml_set_element_handler |
— | — | @@ -39,6 +39,146 @@ |
40 | 40 | $edgXMLValues[$edgCurrentXMLTag] = array( $content ); |
41 | 41 | } |
42 | 42 | |
| 43 | + static function parseParams( $params ) { |
| 44 | + $args = Array(); |
| 45 | + foreach ($params as $param) { |
| 46 | + $param = preg_replace ( "/\s\s+/" , " " , $param ); //whitespace |
| 47 | + list($name, $value) = split("=", $param, 2); |
| 48 | + $args[$name] = $value; |
| 49 | + } |
| 50 | + return $args; |
| 51 | + } |
| 52 | + |
| 53 | + // This function parses the data argument |
| 54 | + static function parseMappings( $dataArg ) { |
| 55 | + $dataArg = preg_replace ( "/\s\s+/" , " " , $dataArg ); //whitespace |
| 56 | + $rawMappings = split(",", $dataArg); |
| 57 | + $mappings = Array(); |
| 58 | + foreach ($rawMappings as $rawMapping) { |
| 59 | + $vals = split("=", $rawMapping, 2); |
| 60 | + if (count($vals) == 2) { |
| 61 | + $intValue = trim($vals[0]); |
| 62 | + $extValue = trim($vals[1]); |
| 63 | + $mappings[$intValue] = $extValue; |
| 64 | + } |
| 65 | + } |
| 66 | + return $mappings; |
| 67 | + } |
| 68 | + |
| 69 | + static function getLDAPData ($filter, $domain, $params) { |
| 70 | + global $edgLDAPServer; |
| 71 | + global $edgLDAPUser; |
| 72 | + global $edgLDAPPass; |
| 73 | + |
| 74 | + $ds = EDUtils::connectLDAP($edgLDAPServer[$domain], $edgLDAPUser[$domain], $edgLDAPPass[$domain]); |
| 75 | + $results = EDUtils::searchLDAP($ds, $domain, $filter, $params); |
| 76 | + |
| 77 | + return $results; |
| 78 | + } |
| 79 | + |
| 80 | + static function connectLDAP($server, $username, $password) { |
| 81 | + $ds = ldap_connect($server); |
| 82 | + if ($ds) { |
| 83 | + // these options for Active Directory only? |
| 84 | + ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION,3); |
| 85 | + ldap_set_option($ds, LDAP_OPT_REFERRALS, 0); |
| 86 | + |
| 87 | + if ($username) { |
| 88 | + $r = ldap_bind($ds, $username, $password); |
| 89 | + } else { |
| 90 | + # no username, so do anonymous bind |
| 91 | + $r = ldap_bind($ds); |
| 92 | + } |
| 93 | + |
| 94 | + # should check the result of the bind here |
| 95 | + return $ds; |
| 96 | + } else { |
| 97 | + echo "Unable to connect to $server\n"; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + static function searchLDAP($ds, $domain, $filter, $attributes) { |
| 102 | + global $edgLDAPBaseDN; |
| 103 | + |
| 104 | + $sr = ldap_search($ds, $edgLDAPBaseDN[$domain], $filter, $attributes); |
| 105 | + $results = ldap_get_entries($ds, $sr); |
| 106 | + return $results; |
| 107 | + } |
| 108 | + |
| 109 | + static function getDBData ($server_id, $from, $where, $columns) { |
| 110 | + global $edgDBServerType; |
| 111 | + global $edgDBServer; |
| 112 | + global $edgDBName; |
| 113 | + global $edgDBUser; |
| 114 | + global $edgDBPass; |
| 115 | + |
| 116 | + if ((! array_key_exists($server_id, $edgDBServerType)) || |
| 117 | + (! array_key_exists($server_id, $edgDBServer)) || |
| 118 | + (! array_key_exists($server_id, $edgDBName)) || |
| 119 | + (! array_key_exists($server_id, $edgDBUser)) || |
| 120 | + (! array_key_exists($server_id, $edgDBPass))) { |
| 121 | + echo "<p>ERROR: Incomplete information for this server ID.</p>\n"; |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + $db_type = $edgDBServerType[$server_id]; |
| 127 | + $db_server = $edgDBServer[$server_id]; |
| 128 | + $db_name = $edgDBName[$server_id]; |
| 129 | + $db_username = $edgDBUser[$server_id]; |
| 130 | + $db_password = $edgDBPass[$server_id]; |
| 131 | + |
| 132 | + if ($db_type == "mysql") { |
| 133 | + $db = new Database($db_server, $db_username, $db_password, $db_name); |
| 134 | + } elseif ($db_type == "postgres") { |
| 135 | + $db = new DatabasePostgres($db_server, $db_username, $db_password, $db_name); |
| 136 | + } elseif ($db_type == "mssql") { |
| 137 | + $db = new DatabaseMssql($db_server, $db_username, $db_password, $db_name); |
| 138 | + } else { |
| 139 | + echo "<p>ERROR: Unknown database type.</p>\n"; |
| 140 | + return; |
| 141 | + } |
| 142 | + if (! $db->isOpen()) { |
| 143 | + echo "<p>ERROR: Could not connect to database.</p>\n"; |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + if (count($columns) == 0) { |
| 148 | + echo "<p>ERROR: No return values specified.</p>\n"; |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + $rows = EDUtils::searchDB($db, $from, $where, $columns); |
| 153 | + $db->close(); |
| 154 | + |
| 155 | + $values = Array(); |
| 156 | + foreach ($rows as $row) { |
| 157 | + foreach ($columns as $column) { |
| 158 | + $values[$column][] = $row[$column]; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return $values; |
| 163 | + } |
| 164 | + |
| 165 | + static function searchDB ($db, $from, $where, $columns) { |
| 166 | + $sql = "SELECT " . implode(",", $columns) . " "; |
| 167 | + $sql .= "FROM " . $from . " "; |
| 168 | + $sql .= "WHERE " . $where; |
| 169 | + |
| 170 | + $result = $db->query($sql); |
| 171 | + if (!$result) { |
| 172 | + echo "Invalid query."; |
| 173 | + return false; |
| 174 | + } else { |
| 175 | + $rows = Array(); |
| 176 | + while ($row = $db->fetchRow($result)) { |
| 177 | + $rows[] = $row; |
| 178 | + } |
| 179 | + return $rows; |
| 180 | + } |
| 181 | + } |
| 182 | + |
43 | 183 | static function getXMLData ( $xml ) { |
44 | 184 | global $edgXMLValues; |
45 | 185 | $edgXMLValues = array(); |
— | — | @@ -154,8 +294,8 @@ |
155 | 295 | $page = Http::get( $url ); |
156 | 296 | if ( $page === false ) { |
157 | 297 | sleep( 1 ); |
158 | | - if( $try_count >= $this->http_number_of_tries ){ |
159 | | - echo "could not get URL after {$this->http_number_of_tries} tries.\n\n"; |
| 298 | + if( $try_count >= self::$http_number_of_tries ){ |
| 299 | + echo "could not get URL after " . self::$http_number_of_tries . " tries.\n\n"; |
160 | 300 | return ''; |
161 | 301 | } |
162 | 302 | $try_count++; |