Esempio n. 1
0
 /**
  * Checks if file with this name is already exist and generate new name if it is so
  *
  * @param  string $file path to file
  * @return string unique file name
  */
 public function generateName($file)
 {
     $info = fn_pathinfo($file);
     while ($this->isExist($file)) {
         $info['filename'] .= '_' . fn_strtolower(fn_generate_code('', $this->_file_suffix_length));
         $file = $info['dirname'] . '/' . $info['filename'] . '.' . $info['extension'];
     }
     return $file;
 }
Esempio n. 2
0
    $pname = fn_te_normalize_path($_REQUEST, $root_dir);
    $pname_to = dirname($pname) . '/' . fn_basename($_REQUEST['rename_to']);
    $object = @is_dir($pname) ? 'directory' : 'file';
    $ext_from = fn_get_file_ext($pname);
    $ext_to = fn_get_file_ext($pname_to);
    if (in_array(fn_strtolower($ext_from), Registry::get('config.forbidden_file_extensions')) || in_array(fn_strtolower($ext_to), Registry::get('config.forbidden_file_extensions'))) {
        fn_set_notification('E', __('error'), __('text_forbidden_file_extension', array('[ext]' => $ext_to)));
    } elseif (fn_te_check_path($pname) && fn_rename($pname, $pname_to)) {
        fn_set_notification('N', __('notice'), __("text_{$object}_renamed", array("[{$object}]" => fn_basename($pname), "[to_{$object}]" => fn_basename($pname_to))));
    } else {
        fn_set_notification('E', __('error'), __("text_cannot_rename_{$object}", array("[{$object}]" => fn_basename($pname), "[to_{$object}]" => fn_basename($pname_to))));
    }
    return array(CONTROLLER_STATUS_REDIRECT, 'templates.init_view?dir=' . $_REQUEST['file_path']);
} elseif ($mode == 'create_file') {
    $file_path = fn_te_normalize_path($_REQUEST, $root_dir);
    $file_info = fn_pathinfo($file_path);
    if (in_array(fn_strtolower($file_info['extension']), Registry::get('config.forbidden_file_extensions')) || empty($file_info['filename'])) {
        fn_set_notification('E', __('error'), __('text_forbidden_file_extension', array('[ext]' => $file_info['extension'])));
    } elseif (fn_te_check_path($file_path) && @touch($file_path)) {
        fn_te_chmod($file_path, DEFAULT_FILE_PERMISSIONS, false);
        fn_set_notification('N', __('notice'), __('text_file_created', array('[file]' => fn_basename($file_path))));
    } else {
        fn_set_notification('E', __('error'), __('text_cannot_create_file', array('[file]' => fn_basename($file_path))));
    }
    return array(CONTROLLER_STATUS_REDIRECT, 'templates.init_view?dir=' . $_REQUEST['file_path']);
} elseif ($mode == 'create_folder') {
    $folder_path = fn_te_normalize_path($_REQUEST, $root_dir);
    if (fn_te_check_path($folder_path) && fn_mkdir($folder_path)) {
        fn_set_notification('N', __('notice'), __('text_directory_created', array('[directory]' => fn_basename($folder_path))));
    } else {
        fn_set_notification('E', __('error'), __('text_cannot_create_directory', array('[directory]' => fn_basename($folder_path))));
Esempio n. 3
0
 /**
  * Completes url from base url if needed
  *
  * @param  string $url      url to be completed
  * @param  string $base_url basic url to complete checked urls
  * @return string completed url or false if url can not be completed
  */
 private static function _completeUrl($url, $base_url)
 {
     $result = false;
     $parts = parse_url($url);
     $base_parts = parse_url($base_url);
     if (!empty($parts['scheme']) && !empty($parts['host'])) {
         // url is already complete
         $result = $url;
     } elseif (!empty($parts['path']) && !empty($base_parts['scheme']) && !empty($base_parts['host'])) {
         $result = $base_parts['scheme'] . '://' . $base_parts['host'];
         if (!empty($base_parts['port'])) {
             $result .= ':' . $base_parts['port'];
         }
         if (strpos($parts['path'], '/') === 0) {
             // absolute path passed
             $result .= $parts['path'];
         } else {
             $pathinfo = fn_pathinfo($base_parts['path']);
             $result .= $pathinfo['dirname'] . '/' . $parts['path'];
         }
         if (!empty($parts['query'])) {
             $result .= '?' . $parts['query'];
         }
     }
     return $result;
 }
Esempio n. 4
0
function fn_add_product_options_files($product_data, &$cart, &$auth, $update = false, $location = 'cart')
{
    // Check if products have cusom images
    if (!$update) {
        $uploaded_data = fn_filter_uploaded_data('product_data');
    } else {
        $uploaded_data = fn_filter_uploaded_data('cart_products');
    }
    // Check for the already uploaded files
    if (!empty($product_data['custom_files']['uploaded'])) {
        foreach ($product_data['custom_files']['uploaded'] as $file_id => $file_data) {
            if (Storage::instance('images')->isExist('sess_data/' . fn_basename($file_data['path']))) {
                $id = $file_data['product_id'] . $file_data['option_id'] . $file_id;
                $uploaded_data[$id] = array('name' => $file_data['name'], 'path' => 'sess_data/' . fn_basename($file_data['path']));
                $product_data['custom_files'][$id] = $file_data['product_id'] . '_' . $file_data['option_id'];
            }
        }
    }
    if (!empty($uploaded_data) && !empty($product_data['custom_files'])) {
        $files_data = array();
        foreach ($uploaded_data as $key => $file) {
            $file_info = fn_pathinfo($file['name']);
            $file['extension'] = empty($file_info['extension']) ? '' : $file_info['extension'];
            $file['is_image'] = fn_get_image_extension($file['type']);
            $_data = explode('_', $product_data['custom_files'][$key]);
            $product_id = empty($_data[0]) ? 0 : $_data[0];
            $option_id = empty($_data[1]) ? 0 : $_data[1];
            $file_id = str_replace($option_id . $product_id, '', $key);
            if (empty($file_id)) {
                $files_data[$product_id][$option_id][] = $file;
            } else {
                $files_data[$product_id][$option_id][$file_id] = $file;
            }
        }
    }
    unset($product_data['custom_files']);
    foreach ($product_data as $key => $data) {
        $product_id = !empty($data['product_id']) ? $data['product_id'] : $key;
        // Check if product has cusom images
        if ($update || isset($files_data[$key])) {
            $hash = $key;
        } else {
            $hash = $product_id;
        }
        $_options = fn_get_product_options($product_id);
        if (!empty($files_data[$hash]) && is_array($files_data[$hash])) {
            foreach ($files_data[$hash] as $option_id => $files) {
                foreach ($files as $file_id => $file) {
                    // Check for the allowed extensions
                    if (!empty($_options[$option_id]['allowed_extensions'])) {
                        if (empty($file['extension']) && !empty($_options[$option_id]['allowed_extensions']) || !preg_match("/\\b" . $file['extension'] . "\\b/i", $_options[$option_id]['allowed_extensions'])) {
                            fn_set_notification('E', __('error'), $file['name'] . ': ' . __('text_forbidden_uploaded_file_extension', array('[ext]' => $file['extension'], '[exts]' => $_options[$option_id]['allowed_extensions'])));
                            unset($files_data[$hash][$option_id][$file_id]);
                            continue;
                        }
                    }
                    // Check for the max file size
                    if (!empty($_options[$option_id]['max_file_size'])) {
                        if (empty($file['size'])) {
                            $file['size'] = filesize($file['path']);
                        }
                        if ($file['size'] > $_options[$option_id]['max_file_size'] * 1024) {
                            fn_set_notification('E', __('error'), $file['name'] . ': ' . __('text_forbidden_uploaded_file_size', array('[size]' => $_options[$option_id]['max_file_size'] . ' kb')));
                            unset($files_data[$hash][$option_id][$file_id]);
                            continue;
                        }
                    }
                    $_file_path = 'sess_data/file_' . uniqid(TIME);
                    list(, $_file_path) = Storage::instance('custom_files')->put($_file_path, array('file' => $file['path']));
                    if (!$_file_path) {
                        fn_set_notification('E', __('error'), __('text_cannot_create_file', array('[file]' => $file['name'])));
                        unset($files_data[$hash][$option_id][$file_id]);
                        continue;
                    }
                    $file['path'] = $_file_path;
                    $file['file'] = fn_basename($file['path']);
                    if ($file['is_image']) {
                        $file['thumbnail'] = 'image.custom_image?image=' . $file['file'] . '&type=T';
                        $file['detailed'] = 'image.custom_image?image=' . $file['file'] . '&type=D';
                    }
                    $file['location'] = $location;
                    if ($update) {
                        $cart['products'][$key]['extra']['custom_files'][$option_id][] = $file;
                    } else {
                        $data['extra']['custom_files'][$option_id][] = $file;
                    }
                }
                if ($update) {
                    if (!empty($cart['products'][$key]['product_options'][$option_id])) {
                        $cart['products'][$key]['product_options'][$option_id] = md5(serialize($cart['products'][$key]['extra']['custom_files'][$option_id]));
                    }
                } else {
                    if (!empty($data['extra']['custom_files'][$option_id])) {
                        $data['product_options'][$option_id] = md5(serialize($data['extra']['custom_files'][$option_id]));
                    }
                }
            }
            // Check the required options
            if (empty($data['extra']['parent'])) {
                foreach ($_options as $option) {
                    if ($option['option_type'] == 'F' && $option['required'] == 'Y' && !$update) {
                        if (empty($data['product_options'][$option['option_id']])) {
                            fn_set_notification('E', __('error'), __('product_cannot_be_added'));
                            unset($product_data[$key]);
                            return array($product_data, $cart);
                        }
                    }
                }
            }
        } else {
            if (empty($data['extra']['parent'])) {
                foreach ($_options as $option) {
                    if ($option['option_type'] == 'F' && $option['required'] == 'Y' && empty($cart['products'][$hash]['extra']['custom_files'][$option['option_id']]) && empty($data['extra']['custom_files'][$option['option_id']])) {
                        fn_set_notification('E', __('error'), __('product_cannot_be_added'));
                        unset($product_data[$key]);
                        return array($product_data, $cart);
                    }
                }
            }
        }
        if ($update) {
            foreach ($_options as $option) {
                if ($option['option_type'] == 'F' && empty($cart['products'][$key]['extra']['custom_files'][$option['option_id']])) {
                    unset($cart['products'][$key]['extra']['custom_files'][$option['option_id']]);
                    unset($cart['products'][$key]['product_options'][$option['option_id']]);
                    unset($data['product_options'][$option['option_id']]);
                }
            }
        }
        if (isset($cart['products'][$key]['extra']['custom_files'])) {
            foreach ($cart['products'][$key]['extra']['custom_files'] as $option_id => $files) {
                foreach ($files as $file) {
                    $data['extra']['custom_files'][$option_id][] = $file;
                }
                $data['product_options'][$option_id] = md5(serialize($files));
            }
        }
        $product_data[$key] = $data;
    }
    return array($product_data, $cart);
}
Esempio n. 5
0
function fn_watermarks_generate_thumbnail_post(&$relative_path, &$lazy)
{
    static $init_cache = false;
    $image_path_info = fn_pathinfo($relative_path);
    $image_name = $image_path_info['filename'];
    $key = 'wt_data_' . fn_crc32($image_name);
    $condition = array('images', 'images_links');
    if (fn_allowed_for('ULTIMATE')) {
        $condition[] = 'products';
        $condition[] = 'categories';
    }
    $cache_name = 'watermarks_cache_static';
    if (!$init_cache) {
        Registry::registerCache($cache_name, $condition, Registry::cacheLevel('static'), true);
        $init_cache = true;
    }
    $image_data = Registry::get($cache_name . '.' . $key);
    if (empty($image_data)) {
        $image_data = db_get_row("SELECT l.* FROM ?:images AS i, ?:images_links AS l WHERE (l.image_id = i.image_id OR detailed_id = i.image_id) AND image_path LIKE ?l", $image_name . '.%');
        if (empty($image_data)) {
            return true;
        }
        if (fn_allowed_for('ULTIMATE')) {
            $image_data['company_id'] = fn_wt_get_image_company_id($image_data);
        }
        Registry::set($cache_name . '.' . $key, $image_data);
    }
    $company_id = null;
    if (fn_allowed_for('ULTIMATE')) {
        $company_id = Registry::get('runtime.company_id');
        if ($company_id == null) {
            $company_id = $image_data['company_id'];
        }
    }
    if (!empty($image_data['object_type']) && fn_is_need_watermark($image_data['object_type'], $image_data['object_type'] == 'detailed', $company_id)) {
        $prefix = WATERMARKS_DIR_NAME;
        if (fn_allowed_for('ULTIMATE') && !Registry::get('runtime.company_id')) {
            $prefix = WATERMARKS_DIR_NAME . $company_id . '/';
        }
        if (!Storage::instance('images')->isExist($prefix . $relative_path)) {
            fn_watermark_create($relative_path, $prefix . $relative_path, false, $company_id);
        }
        $relative_path = $prefix . $relative_path;
    }
    return true;
}