/**
 * Set company condition for export feature variants
 * @param array $conditions Array of conditions
 */
function fn_exim_feature_variants_set_company_condition(&$conditions)
{
    if (fn_allowed_for('ULTIMATE')) {
        $condition = fn_get_company_condition('?:product_features.company_id');
        $query = db_process("SELECT feature_id FROM ?:product_features WHERE 1 {$condition}");
        fn_ult_db_query_process($query);
        $conditions[] = "product_feature_variants.feature_id IN ({$query})";
    }
}
Exemple #2
0
/**
 * Returns status identifier by status code and type
 *
 * @param string $status     One-letter status code
 * @param string $type       One-letter status type
 * @param mixed  $is_default True if status is default, false if status is not default, null otherwise
 *
 * @return int|null Status identifier
 */
function fn_get_status_id($status, $type, $is_default = null)
{
    $default_condition = '';
    if (isset($is_default)) {
        $default_condition = db_process("AND is_default = ?s", $is_default ? 'Y' : 'N');
    }
    $status_id = db_get_field("SELECT status_id" . " FROM ?:statuses" . " WHERE status = ?s" . " AND type = ?s" . " ?p", $status, $type, $default_condition);
    return $status_id ? (int) $status_id : null;
}
function fn_ult_db_query_process(&$query)
{
    // Automatically add Sharing condition for SELECT queries
    // Condition will be added only for sharing objects. (Share schema)
    // Example:
    //		before: SELECT ?:pages.page_id FROM ?:pages WHERE page_id = 2
    //		after:  SELECT ?:pages.page_id FROM ?:pages INNER JOIN ?:ult_objects_sharing ON (?:ult_objects_sharing.share_object_id = ?:pages.page_id AND ?:ult_objects_sharing.share_company_id = ?:pages.company_id) WHERE page_id = 2
    if (Registry::get('runtime.company_id') && !Registry::get('runtime.skip_sharing_selection')) {
        // Cart was inited
        if (stripos($query, 'select') === 0) {
            // Add condition only for SELECT queries
            static $sharing_schema;
            if (empty($sharing_schema) && Registry::get('addons_initiated') === true) {
                $sharing_schema = fn_get_schema('sharing', 'schema');
            }
            preg_match('/FROM(.*?)((JOIN\\s|WHERE\\s|GROUP\\s|HAVING\\s|ORDER\\s|LIMIT\\s|$).*)/i', $query, $from);
            if (empty($from)) {
                return $query;
            }
            $tables = array();
            $_tables = fn_explode(',', $from[1]);
            foreach ($_tables as $table) {
                $table_parse = explode(' ', $table);
                $tables[] = reset($table_parse);
            }
            $tables = array_unique($tables);
            foreach ($tables as $table) {
                $table = str_replace(Registry::get('config.table_prefix'), '', $table);
                if (isset($sharing_schema[$table])) {
                    // Divide query into separate parts, like SELECT, FROM, etc...
                    preg_match('/SELECT(.*?)FROM/i', $query, $select);
                    preg_match('/FROM(.*?)((WHERE\\s|GROUP\\s|HAVING\\s|ORDER\\s|LIMIT\\s|$).*)/i', $query, $from);
                    preg_match('/WHERE(.*?)((?:GROUP\\s|HAVING\\s|ORDER\\s|LIMIT\\s|$).*)/i', $query, $where);
                    // Check if this query should stay without changes
                    if (isset($sharing_schema[$table]['conditions']['skip_selection'])) {
                        $condition = $sharing_schema[$table]['conditions']['skip_selection'];
                        if (!is_array($condition) && $condition === true) {
                            continue;
                        } elseif (is_array($condition) && !empty($where[1])) {
                            $alias_pref = '(' . Registry::get('config.table_prefix') . $table . '\\.|[^\\.a-zA-Z_])';
                            // field used without alias or with full table name
                            if (preg_match('/' . Registry::get('config.table_prefix') . $table . '\\s+AS\\s+([a-zA-Z_]+)/i', $query, $alias)) {
                                $alias_pref = '(' . $alias[1] . '\\.)';
                            }
                            preg_match_all('/' . $alias_pref . '([a-zA-Z_]+)\\s*(=|!=|<|>|<>|IN)\\s*(?:\'|")?([^ \'"]+)/', $where[1], $params);
                            if (!empty($params[2])) {
                                foreach ($params[2] as $id => $param) {
                                    if (isset($condition[$param])) {
                                        $values = is_array($condition[$param]['value']) ? $condition[$param]['value'] : array($condition[$param]['value']);
                                        foreach ($values as $value) {
                                            if (empty($condition[$param]['condition'])) {
                                                if ($params[3][$id] == 'IN' && $value == '(' . $params[4][$id] . ')' || $params[3][$id] == '=' && $value == $params[4][$id]) {
                                                    continue 3;
                                                }
                                            } elseif ($condition[$param]['condition'] == 'equal' && ($params[3][$id] == '=' && $value == $params[4][$id] || $params[3][$id] == 'IN' && $value == '(' . $params[4][$id] . ')')) {
                                                continue 3;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    $additional_condition = '';
                    if (!empty($where)) {
                        $additional_condition = $where[2];
                    } elseif (!empty($from[2])) {
                        $additional_condition = $from[2];
                    }
                    // Get table alias if defined
                    if (preg_match("/{$table}\\s?as\\s?([a-zA-Z_]+)/i", $query, $alias)) {
                        $alias = $alias[1];
                    } else {
                        $alias = '?:' . $table;
                    }
                    $key_field = $sharing_schema[$table]['table']['key_field'];
                    // Build new query
                    $query = db_quote('SELECT ' . $select[1] . ' FROM' . $from[1] . ' INNER JOIN ?:ult_objects_sharing ON (' . '?:ult_objects_sharing.share_object_id = ' . "{$alias}.{$key_field}" . ' AND ?:ult_objects_sharing.share_company_id = ?i' . ' AND ?:ult_objects_sharing.share_object_type = ?s)' . (!empty($where[1]) ? 'WHERE ' . $where[1] : '') . " {$additional_condition}", Registry::get('runtime.company_id'), $table);
                    $query = db_process($query);
                }
            }
        }
    }
}
Exemple #4
0
/**
 * Get cached resul from query
 * 
 * @param string $query cached query
 */
function db_get_cached_result($query, $args)
{
    return array();
    $memc = new Memcache();
    $memc->connect('127.0.0.1', 11211) or die("Could not connect");
    if ($memc == null) {
        //return false;
    } else {
        $cache_name = serialize(array('query' => $query, 'args' => $args));
        $key = "c" . md5($cache_name);
        $var_key = $memc->get($key);
        $result = unserialize($var_key);
        //var_dump("111111".$key);
        //if(!empty($var_key)) var_dump(11111111);
        return !empty($result) ? $result : array();
    }
    $memc->close();
    if (Registry::get('runtime.database.skip_cache') != true) {
        $cache_name = serialize(array('query' => $query, 'args' => $args));
        Registry::register_cache('cached_queries', array(), CACHE_LEVEL_STATIC, true);
        $cached_queries = Registry::if_get('cached_queries', array());
        if (!empty($cached_queries['cquery_' . md5($cache_name)])) {
            Registry::register_cache('cquery_' . md5($cache_name), array(), CACHE_LEVEL_STATIC);
            if (Registry::is_exist('cquery_' . md5($cache_name)) == true) {
                $result = Registry::get('cquery_' . md5($cache_name));
                $query = db_process($query, array_slice($args, 1));
                Registry::set('runtime.database.last_query', $query);
                Registry::set('runtime.database.long_query', true);
            }
        }
    }
    return !empty($result) ? $result : array();
}