def ip_address(reset=False): """ If Drupal is behind a reverse proxy, we use the X-Forwarded-For header instead of $_SERVER['REMOTE_ADDR'], which would be the IP address of the proxy server, and not the client's. If Drupal is run in a cluster we use the X-Cluster-Client-Ip header instead. @param $reset Reset the current IP address saved in static. @return IP address of client machine, adjusted for reverse proxy and/or cluster environments. """ php.static(ip_address, "ip_address") if ip_address.ip_address is None or reset: ip_address.ip_address = php.SERVER["REMOTE_ADDR"] if variable_get("reverse_proxy", 0): if php.array_key_exists("HTTP_X_FORWARDED_FOR", php.SERVER): # If an array of known reverse proxy IPs is provided, then trust # the XFF header if request really comes from one of them. reverse_proxy_addresses = variable_get("reverse_proxy_addresses", tuple()) if not php.empty(reverse_proxy_addresses) and php.in_array( ip_address.ip_address, reverse_proxy_addresses, True ): # If there are several arguments, we need to check the most # recently added one, i.e. the last one. ip_address.ip_address = php.array_pop(php.explode(",", php.SERVER["HTTP_X_FORWARDED_FOR"])) # When Drupal is run in a cluster environment, # REMOTE_ADDR contains the IP # address of a server in the cluster, while the IP address # of the client is # stored in HTTP_X_CLUSTER_CLIENT_IP. if php.array_key_exists("HTTP_X_CLUSTER_CLIENT_IP", php.SERVER): ip_address.ip_address = php.SERVER["HTTP_X_CLUSTER_CLIENT_IP"] return ip_address.ip_address
def prefix_tables(sql): """ Append a database prefix to all tables in a query. Queries sent to Drupal should wrap all table names in curly brackets. This function searches for this syntax and adds Drupal's table prefix to all tables, allowing Drupal to coexist with other systems in the same database if necessary. @param sql A string containing a partial or entire SQL query. @return The properly-prefixed string. """ if (php.is_array(settings.db_prefix)): if (php.array_key_exists('default', settings.db_prefix)): tmp = settings.db_prefix; del(tmp['default']); for key,val in tmp.items(): sql = php.strtr(sql, {('{' + key + '}') : (val + key)}); return php.strtr(sql, {'{' : settings.db_prefix['default'], '}' : ''}); else: for key,val in settings.db_prefix.items(): sql = php.strtr(sql, {('{' + key + '}') : (val + key)}); return php.strtr(sql, {'{' : '', '}' : ''}); else: return php.strtr(sql, {'{' : settings.db_prefix, '}' : ''});
def set_active(name = 'default'): """ Activate a database for future queries. If it is necessary to use external databases in a project, this function can be used to change where database queries are sent. If the database has not yet been used, it is initialized using the URL specified for that name in Drupal's configuration file. If this name is not defined, a duplicate of the default connection is made instead. Be sure to change the connection back to the default when done with custom code. @param name The name assigned to the newly active database connection. If omitted, the default connection will be made active. @return the name of the previously active database or FALSE if non was found. @todo BC: Need to eventually resolve the database importing mechanism here right now we are statically loading mysql at the top, but eventually we need to get this figured out """ php.static(set_active, 'db_conns', {}) php.static(set_active, 'active_name', False) if (settings.db_url == None): install_goto('install.py'); if (not php.isset(set_active.db_conns, name)): # Initiate a new connection, using the named DB URL specified. if (isinstance(settings.db_url, dict)): connect_url = (settings.db_url[name] if \ php.array_key_exists(name, settings.db_url) else \ settings.db_url['default']); else: connect_url = settings.db_url; lib_appglobals.db_type = \ php.substr(connect_url, 0, php.strpos(connect_url, '://')); #handler = "includes/database_%(db_type)s.py" % {'db_type' : db_type}; #try: # import db file here #except ImportError: # _db_error_page("The database type '" + db_type + \ # "' is unsupported. Please use either 'mysql' or " + \ # "'mysqli' for MySQL, or 'pgsql' for PostgreSQL databases."); set_active.db_conns[name] = db.connect(connect_url); # We need to pass around the simpletest database prefix in the request # and we put that in the user_agent php.header. if (php.preg_match("/^simpletest\d+$/", php.SERVER['HTTP_USER_AGENT'])): settings.db_prefix = php.SERVER['HTTP_USER_AGENT']; previous_name = set_active.active_name; # Set the active connection. set_active.active_name = name; lib_appglobals.active_db = set_active.db_conns[name]; return previous_name;
def ip_address(reset=False): """ If Drupal is behind a reverse proxy, we use the X-Forwarded-For header instead of $_SERVER['REMOTE_ADDR'], which would be the IP address of the proxy server, and not the client's. If Drupal is run in a cluster we use the X-Cluster-Client-Ip header instead. @param $reset Reset the current IP address saved in static. @return IP address of client machine, adjusted for reverse proxy and/or cluster environments. """ php.static(ip_address, 'ip_address') if (ip_address.ip_address is None or reset): ip_address.ip_address = php.SERVER['REMOTE_ADDR'] if (variable_get('reverse_proxy', 0)): if (php.array_key_exists('HTTP_X_FORWARDED_FOR', php.SERVER)): # If an array of known reverse proxy IPs is provided, then trust # the XFF header if request really comes from one of them. reverse_proxy_addresses = variable_get('reverse_proxy_addresses', \ tuple()) if (not php.empty(reverse_proxy_addresses) and \ php.in_array(ip_address.ip_address, reverse_proxy_addresses, \ True)): # If there are several arguments, we need to check the most # recently added one, i.e. the last one. ip_address.ip_address = php.array_pop(\ php.explode(',', php.SERVER['HTTP_X_FORWARDED_FOR'])) # When Drupal is run in a cluster environment, # REMOTE_ADDR contains the IP # address of a server in the cluster, while the IP address # of the client is # stored in HTTP_X_CLUSTER_CLIENT_IP. if (php.array_key_exists('HTTP_X_CLUSTER_CLIENT_IP', php.SERVER)): ip_address.ip_address = php.SERVER['HTTP_X_CLUSTER_CLIENT_IP'] return ip_address.ip_address
def get_sort(headers): """ Determine the current sort direction. @param headers An array of column headers in the format described in theme_table(). @return The current sort direction ("asc" or "desc"). """ if php.isset(php.GET['sort']): return 'desc' if (php.GET['sort'] == 'desc') else 'asc'; # User has not specified a sort. Use default if specified; otherwise use # "asc". else: for header in headers: if php.is_array(header) and php.array_key_exists('sort', header): return header['sort'] return 'asc'