Пример #1
0
function file_cache_insert_file($url, $location, $content_type, $content, $cache_type = 'HTTP', $overwrite = FALSE)
{
    $cache_config_r = file_cache_get_config($cache_type);
    if (is_array($cache_config_r) && $cache_config_r['enable'] !== FALSE) {
        $directory = $cache_config_r['directory'];
        if ($directory === FALSE) {
            return FALSE;
        }
        // we need to know whether we are overwriting existing URL or not
        $file_cache_r = fetch_url_file_cache_r($url, $cache_type, INCLUDE_EXPIRED);
        if (is_array($file_cache_r)) {
            if (!file_cache_get_cache_file($file_cache_r)) {
                opendb_logger(OPENDB_LOG_INFO, __FILE__, __FUNCTION__, 'Cache file record exists, but there is no cache file - will recreate', $file_cache_r);
                $file_cache_r['cache_file'] = NULL;
                // delete thumbnail if it exists
                if (!($thumbnail_file = file_cache_get_cache_file_thumbnail($file_cache_r))) {
                    delete_file($thumbnail_file);
                }
                $file_cache_r['cache_file_thumb'] = NULL;
            } else {
                if (!$overwrite) {
                    opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, 'File cache record exists and cannot be overwritten', $file_cache_r);
                    return FALSE;
                }
            }
        } else {
            $file_cache_r['cache_type'] = $cache_type;
        }
        if ($content == NULL && is_url_absolute($url)) {
            $httpClient = new OpenDbSnoopy();
            $content = $httpClient->fetchURI($url, FALSE);
            if ($content !== FALSE) {
                $location = $httpClient->getLocation();
                $content_type = $httpClient->getContentType();
                unset($httpClient);
            } else {
                unset($httpClient);
                // http client logs error
                return FALSE;
            }
        }
        $file_cache_r['url'] = ifempty($url, $file_cache_r['url']);
        $file_cache_r['location'] = ifempty($location, $file_cache_r['location']);
        $file_cache_r['content_type'] = validate_content_type(ifempty($content_type, $file_cache_r['content_type']));
        $thumbnail_support = FALSE;
        if (strlen($file_cache_r['content_type']) > 0) {
            $file_type_r = fetch_file_type_r($file_cache_r['content_type']);
            $file_cache_r['extension'] = $file_type_r['extension'];
            $thumbnail_support = $file_type_r['thumbnail_support_ind'] == 'Y';
        } else {
            $extension = get_file_ext($file_cache_r['url']);
            if (strlen($extension) > 0 && ($ext_content_type = fetch_file_type_for_extension($extension)) !== FALSE) {
                $content_type = $ext_content_type;
            } else {
                $content_type = validate_content_type($content_type);
            }
            $file_type_r = fetch_file_type_r($content_type);
            if (!is_array($file_type_r)) {
                opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, 'Content type not supported', array($url, $location, $content_type, $cache_type, $overwrite));
                return FALSE;
            }
            $file_cache_r['content_type'] = $content_type;
            $file_cache_r['extension'] = $file_type_r['extension'];
            $thumbnail_support = $file_type_r['thumbnail_support_ind'] == 'Y';
        }
        $file_cache_r['content_length'] = 0;
        if (($uploadFile = get_item_input_file_upload_url($url)) !== FALSE) {
            $file_cache_r['content_length'] = @filesize($uploadFile);
            $file_cache_r['cache_file'] = basename($uploadFile);
            $file_cache_r['upload_file_ind'] = 'Y';
        } else {
            if (strlen($content) > 0) {
                $file_cache_r['content_length'] = strlen($content);
                $file_cache_r['upload_file_ind'] = 'N';
            }
        }
        if ($file_cache_r['content_length'] > 0) {
            if (!is_numeric($file_cache_r['sequence_number'])) {
                $file_cache_r['sequence_number'] = insert_file_cache($cache_type, $file_cache_r['upload_file_ind'], $file_cache_r['url'], $file_cache_r['location'], $file_cache_r['content_type']);
                if ($file_cache_r['sequence_number'] === FALSE) {
                    return FALSE;
                }
            }
            if ($file_cache_r['cache_file'] == NULL) {
                $file_cache_r['cache_file'] = filecache_generate_cache_filename($file_cache_r);
            }
            if ($content != NULL) {
                $directory = file_cache_get_cache_type_directory($file_cache_r['cache_type']);
                $cacheFile = $directory . '/' . $file_cache_r['cache_file'];
                if (!file_put_contents($cacheFile, $content) !== FALSE) {
                    opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, 'Cache file not written', array($cacheFile));
                    return FALSE;
                }
            }
            if ($thumbnail_support) {
                if (strlen($file_cache_r['cache_file_thumb']) == 0) {
                    $file_cache_r['cache_file_thumb'] = filecache_generate_cache_filename($file_cache_r, TRUE);
                }
            }
            if ($file_cache_r['upload_file_ind'] != 'Y') {
                $expire_date = is_numeric($cache_config_r['lifetime']) ? "NOW()+ INTERVAL " . $cache_config_r['lifetime'] . " SECOND" : NULL;
            } else {
                $expire_date = NULL;
            }
            // do not expire uploaded file records.
            if (!update_file_cache($file_cache_r['sequence_number'], $file_cache_r['content_length'], $expire_date, $file_cache_r['cache_file'], $file_cache_r['cache_file_thumb'])) {
                return FALSE;
            }
            if ($thumbnail_support) {
                file_cache_save_thumbnail_file($file_cache_r, $errors);
            }
            return TRUE;
        } else {
            opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, 'File content length is zero', array($url, $location, $content_type, $cache_type, $overwrite));
            return FALSE;
        }
    } else {
        if (!is_array($cache_config_r)) {
            opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, 'No file cache configuration', array($url, $location, $content_type, $cache_type, $overwrite));
            return FALSE;
        } else {
            // file cache is disabled
            return TRUE;
        }
    }
}
Пример #2
0
function get_item_display_field($item_r, $item_attribute_type_r, $value = NULL, $dowrap = TRUE, $prompt_mask = NULL)
{
    if ($item_attribute_type_r['display_type'] == 'hidden') {
        return '';
    } else {
        if ($item_attribute_type_r['display_type'] == 'fileviewer') {
            $format_mask = ifempty($item_attribute_type_r['display_type_arg1'], '%value%');
            $width = ifempty($item_attribute_type_r['display_type_arg2'], '400');
            $height = ifempty($item_attribute_type_r['display_type_arg3'], '300');
            $target = ifempty($item_attribute_type_r['display_type_arg4'], '_blank');
            if (is_array($value)) {
                $values = $value;
            } else {
                $values[] = $value;
            }
            if (count($values) > 0) {
                $display_value_r = array();
                while (list(, $value) = each($values)) {
                    $value = trim($value);
                    $value_format_mask = $format_mask;
                    if (strpos($value_format_mask, '%img%') !== FALSE) {
                        $file_type_r = fetch_file_type_r(fetch_file_type_for_extension(get_file_ext($value)));
                        if (strlen($file_type_r['image']) > 0 && ($image_src = theme_image_src($file_type_r['image'])) !== FALSE) {
                            $img = '<img src="' . $image_src . '" title="' . $value . '">';
                        } else {
                            $img = '';
                        }
                        $value_format_mask = str_replace('%img%', $img, $value_format_mask);
                    }
                    if (strpos($value_format_mask, '%value%') !== FALSE) {
                        $value_format_mask = str_replace('%value%', $value, $value_format_mask);
                    }
                    $file_r = file_cache_get_image_r($value, 'display');
                    $url = $file_r['fullsize']['url'];
                    $display_value_r[] = "<a href=\"" . $value . "\" onclick=\"fileviewer('{$url}' ,'" . ($width + 20) . "', '" . ($height + 25) . "', '" . $target . "'); return false;\" title=\"" . $item_attribute_type_r['prompt'] . "\" class=\"popuplink\">{$value_format_mask}</a>";
                }
                $field = format_multivalue_block($display_value_r, 'fileviewer');
                if ($dowrap) {
                    return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                } else {
                    return $field;
                }
            } else {
                return '';
            }
        } else {
            if ($item_attribute_type_r['display_type'] == 'datetime') {
                if (is_array($value)) {
                    $values = $value;
                } else {
                    $values[] = $value;
                }
                if (count($values) > 0) {
                    $display_value_r = array();
                    while (list(, $value) = each($values)) {
                        $value = trim($value);
                        $timestamp = get_timestamp_for_datetime($value, 'YYYYMMDDHH24MISS');
                        if ($timestamp !== FALSE) {
                            if (strlen($item_attribute_type_r['display_type_arg1']) == 0) {
                                $item_attribute_type_r['display_type_arg1'] = 'DD/MM/YYYY';
                            }
                            $datetime = get_localised_timestamp($item_attribute_type_r['display_type_arg1'], $timestamp);
                            if ($datetime !== FALSE) {
                                $display_value_r[] = $datetime;
                            } else {
                                $display_value_r[] = $value;
                            }
                        } else {
                            $display_value_r[] = $value;
                        }
                    }
                    $field = format_multivalue_block($display_value_r, 'datetime');
                    if ($dowrap) {
                        return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                    } else {
                        return $field;
                    }
                } else {
                    return '';
                }
            } else {
                if ($item_attribute_type_r['display_type'] == 'format_mins') {
                    if (is_array($value)) {
                        $values = $value;
                    } else {
                        $values[] = $value;
                    }
                    if (count($values) > 0) {
                        $display_value_r = array();
                        while (list(, $value) = each($values)) {
                            $value = trim($value);
                            if (is_numeric($value)) {
                                // Ensure we have a mask to work with.
                                $display_mask = $item_attribute_type_r['display_type_arg1'];
                                if (strlen($display_mask) == 0) {
                                    $display_mask = '%h %H %m %M';
                                }
                                $hrs = floor($value / 60);
                                // hours
                                $mins = $value % 60;
                                // minutes
                                // Process display_mask and remove any bits that are not needed because the hour/minute is zero.
                                if ($mins == 0 && $hrs > 0) {
                                    // only get rid of minutes if $hrs is a value.
                                    $index = strpos($display_mask, '%H');
                                    if ($index !== FALSE) {
                                        $display_mask = substr($display_mask, 0, $index + 2);
                                    } else {
                                        $index = strpos($display_mask, '%m');
                                        if ($index != FALSE) {
                                            $display_mask = substr($display_mask, 0, $index);
                                        }
                                        //include the %H
                                    }
                                } else {
                                    if ($hrs == 0) {
                                        $index = strpos($display_mask, '%m');
                                        if ($index != FALSE) {
                                            $display_mask = substr($display_mask, $index);
                                        }
                                        //include the %H
                                    }
                                }
                                // Unfortunately we need to do $mins>0 and $hrs>0 if's twice, because otherwise once we
                                // replace the %h and %H the test for $mins>0 would not be able to cut the display_mask,
                                // based on the %h/%H...
                                if ($hrs > 0) {
                                    // Now do all replacements.
                                    $display_mask = str_replace('%h', $hrs, $display_mask);
                                    if ($hrs != 1) {
                                        $display_mask = str_replace('%H', get_opendb_lang_var('hours'), $display_mask);
                                    } else {
                                        $display_mask = str_replace('%H', get_opendb_lang_var('hour'), $display_mask);
                                    }
                                }
                                if ($mins >= 0 || $hrs === 0 && $mins === 0) {
                                    // Now do minute replacements only.
                                    $display_mask = str_replace('%m', $mins, $display_mask);
                                    if ($mins != 1) {
                                        $display_mask = str_replace('%M', get_opendb_lang_var('minutes'), $display_mask);
                                    } else {
                                        $display_mask = str_replace('%M', get_opendb_lang_var('minute'), $display_mask);
                                    }
                                }
                                $display_value_r[] = $display_mask;
                            } else {
                                // what else can we do here?!
                                $display_value_r[] = $value;
                            }
                        }
                        $field = format_multivalue_block($display_value_r, 'format_mins');
                        if ($dowrap) {
                            return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                        } else {
                            return $field;
                        }
                    } else {
                        return '';
                    }
                } else {
                    if ($item_attribute_type_r['display_type'] == 'star_rating') {
                        // arg[0] = rating range
                        if (is_array($value)) {
                            $values = $value;
                        } else {
                            $values[] = $value;
                        }
                        if (count($values) > 0) {
                            $display_value_r = array();
                            while (list(, $value) = each($values)) {
                                $value = trim($value);
                                // no point unless numeric
                                if (is_numeric($value)) {
                                    $total_count = $item_attribute_type_r['display_type_arg1'];
                                    if (is_numeric($total_count)) {
                                        $display_value = '';
                                        $j = $value;
                                        for ($i = 0; $i < $total_count; ++$i) {
                                            if ($j >= 0.75) {
                                                $display_value .= theme_image('rs.gif');
                                            } else {
                                                if ($j >= 0.25) {
                                                    $display_value .= theme_image('rgs.gif');
                                                } else {
                                                    $display_value .= theme_image('gs.gif');
                                                }
                                            }
                                            $j = $j - 1;
                                        }
                                        $ratingmask = $item_attribute_type_r['display_type_arg2'];
                                        if (strlen($ratingmask) > 0) {
                                            $ratingmask = str_replace('%value%', $value, $ratingmask);
                                            $ratingmask = str_replace('%maxrange%', $total_count, $ratingmask);
                                            $display_value = str_replace('%starrating%', $display_value, $ratingmask);
                                        }
                                        if ($item_attribute_type_r['listing_link_ind'] == 'Y') {
                                            $display_value = format_listing_link($value, $display_value, $item_attribute_type_r, NULL);
                                        }
                                    }
                                    $display_value_r[] = $display_value;
                                }
                            }
                            $field = format_multivalue_block($display_value_r, 'starrating');
                            if ($dowrap) {
                                return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                            } else {
                                return $field;
                            }
                        } else {
                            return '';
                            // nothing to do!
                        }
                    } else {
                        if (!is_array($value) && $item_attribute_type_r['display_type'] == 'display' && ifempty($item_attribute_type_r['display_type_arg1'], '%value%') == '%value%') {
                            // Support newline formatting by default.
                            $value = nl2br(trim($value));
                            if ($item_attribute_type_r['listing_link_ind'] == 'Y') {
                                $field = format_listing_links($value, $item_attribute_type_r, 'exact');
                            } else {
                                $field = $value;
                            }
                            if ($dowrap) {
                                return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                            } else {
                                return $field;
                            }
                        } else {
                            if ($item_attribute_type_r['display_type'] == 'list') {
                                //list(list_type [,delimiter])
                                if (is_array($value)) {
                                    $values = $value;
                                    $attr_match = 'exact';
                                } else {
                                    $value = trim($value);
                                    if (strlen($item_attribute_type_r['display_type_arg2']) == 0) {
                                        // Use newline!
                                        $values = explode_lines($value);
                                        $attr_match = 'partial';
                                    } else {
                                        $values = explode($item_attribute_type_r['display_type_arg2'], $value);
                                        if (strlen(trim($item_attribute_type_r['display_type_arg2'])) === 0) {
                                            $attr_match = 'word';
                                        } else {
                                            $attr_match = 'partial';
                                        }
                                    }
                                }
                                $field = format_list_from_array($values, $item_attribute_type_r, $item_attribute_type_r['listing_link_ind'] == 'Y' ? $attr_match : FALSE);
                                if ($dowrap) {
                                    return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                                } else {
                                    return $field;
                                }
                            } else {
                                if ($item_attribute_type_r['display_type'] == 'category' || $item_attribute_type_r['display_type'] == 'display') {
                                    $field = '';
                                    if (is_array($value)) {
                                        $value_array = $value;
                                    } else {
                                        $value_array[] = $value;
                                    }
                                    $attribute_value_rs = array();
                                    if ($item_attribute_type_r['lookup_attribute_ind'] == 'Y') {
                                        $results = fetch_value_match_attribute_type_lookup_rs($item_attribute_type_r['s_attribute_type'], $value_array, get_lookup_order_by($item_attribute_type_r['display_type_arg1']), 'asc');
                                        if ($results) {
                                            while ($lookup_r = db_fetch_assoc($results)) {
                                                $lookup_key = array_search2($lookup_r['value'], $value_array, TRUE);
                                                if ($lookup_key !== FALSE) {
                                                    // Remove the matched element
                                                    array_splice($value_array, $lookup_key, 1);
                                                    $attribute_value_rs[] = array(value => $lookup_r['value'], display => $lookup_r['display'], img => $lookup_r['img']);
                                                }
                                            }
                                            db_free_result($results);
                                        }
                                    }
                                    // where extra items that do not have a matching lookup value.
                                    if (is_not_empty_array($value_array)) {
                                        reset($value_array);
                                        while (list(, $value) = each($value_array)) {
                                            if (strlen(trim($value)) > 0) {
                                                // In case there are extra spaces
                                                $attribute_value_rs[] = array(value => $value, display => $value);
                                            }
                                        }
                                    }
                                    if (is_not_empty_array($attribute_value_rs)) {
                                        $field = format_lookup_display_block($item_attribute_type_r, $attribute_value_rs);
                                        if (strlen($field) > 0) {
                                            if ($dowrap) {
                                                return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                                            } else {
                                                return $field;
                                            }
                                        } else {
                                            return NULL;
                                        }
                                    }
                                } else {
                                    if ($item_attribute_type_r['display_type'] == 'review') {
                                        $total_count = fetch_attribute_type_cnt('S_RATING');
                                        if (is_numeric($total_count)) {
                                            $value = trim($value);
                                            if (!is_numeric($value)) {
                                                $value = 0;
                                            }
                                            $field = '';
                                            $j = $value;
                                            for ($i = 0; $i < $total_count; ++$i) {
                                                if ($j >= 0.75) {
                                                    $field .= theme_image('rs.gif');
                                                } else {
                                                    if ($j >= 0.25) {
                                                        $field .= theme_image('rgs.gif');
                                                    } else {
                                                        $field .= theme_image('gs.gif');
                                                    }
                                                }
                                                $j = $j - 1;
                                            }
                                            // If a mask is defined, format the display value.
                                            if (strlen($item_attribute_type_r['display_type_arg1']) > 0) {
                                                $lookup_r = fetch_attribute_type_lookup_r('S_RATING', $value);
                                                if (is_not_empty_array($lookup_r)) {
                                                    $field .= format_display_value($item_attribute_type_r['display_type_arg1'], $lookup_r['img'], $lookup_r['value'], $lookup_r['display']);
                                                }
                                            }
                                            return $field;
                                            // this is only used in a few places.
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    //else -- no display type match.
    if ($dowrap) {
        return format_field($item_attribute_type_r['prompt'], nl2br($value), $prompt_mask);
    } else {
        return nl2br($value);
    }
}