Ejemplo n.º 1
0
 function __construct(array $allowedExtensions = null, $sizeLimit = null)
 {
     if ($allowedExtensions === null) {
         $allowedExtensions = osc_allowed_extension();
     }
     if ($sizeLimit === null) {
         $sizeLimit = 1024 * osc_max_size_kb();
     }
     $this->_allowedExtensions = $allowedExtensions;
     $this->_sizeLimit = $sizeLimit;
     if (!Params::existServerParam('CONTENT_TYPE')) {
         $this->_file = false;
     } else {
         if (strpos(strtolower(Params::getServerParam('CONTENT_TYPE')), 'multipart/') === 0) {
             $this->_file = new AjaxUploadedFileForm();
         } else {
             $this->_file = new AjaxUploadedFileXhr();
         }
     }
 }
Ejemplo n.º 2
0
        private function checkSize($aResources)
        {
            $success = true;

            if($aResources != '') {
                // get allowedExt
                $maxSize = osc_max_size_kb() * 1024;
                foreach ($aResources['error'] as $key => $error) {
                    $bool_img = false;
                    if ($error == UPLOAD_ERR_OK) {
                        $size = $aResources['size'][$key];
                        if($size >= $maxSize){
                            $success = false;
                        }
                    }
                }
                if(!$success){
                    osc_add_flash_error_message( _m("One of the files you tried to upload exceeds the maximum size"));
                }
            }
            return $success;
        }
Ejemplo n.º 3
0
?>
" method="post" enctype="multipart/form-data">
                            <input type="hidden" name="page" value="settings" />
                            <input type="hidden" name="action" value="media_post" />
                            <fieldset>
                                <legend><?php 
_e('Restrictions');
?>
</legend>
                                <p>
                                    <label for="maxSize"><?php 
_e('Maximum size, in KB');
?>
</label><br />
                                    <input type="text" name="maxSizeKb" id="maxSize" value="<?php 
echo osc_max_size_kb();
?>
" />
                                </p>

                                <p>
                                    <label for="allowedExt"><?php 
_e('Allowed format extensions (eg: png, jpg, gif)');
?>
</label><br />
                                    <input type="text" name="allowedExt" id="allowedExt" value="<?php 
echo osc_allowed_extension();
?>
" />
                                </p>
                                <?php 
Ejemplo n.º 4
0
</label>
                            <span class="help-box"><?php 
_e('No white background will be added to keep the size.');
?>
</span>
                        </div>
                    </div>
                </div>
                <div class="form-row">
                    <div class="form-label"><?php 
_e('Maximum size');
?>
</div>
                    <div class="form-controls">
                        <input type="text" class="input-medium" name="maxSizeKb" value="<?php 
echo osc_esc_html(osc_max_size_kb());
?>
" />
                        <span class="help-box"><?php 
_e('Size in KB');
?>
</span>
                        <div class="flashmessage flashmessage-warning flashmessage-inline">
                            <p><?php 
printf(__('Maximum size PHP configuration allows: %d KB'), $maxPHPsize);
?>
</p>
                        </div>
                    </div>
                </div>
                <div class="form-row">
Ejemplo n.º 5
0
    public static function ajax_photos($resources = null)
    {
        if ($resources == null) {
            $resources = osc_get_item_resources();
        }
        $aImages = array();
        if (Session::newInstance()->_getForm('photos') != '') {
            $aImages = Session::newInstance()->_getForm('photos');
            $aImages = $aImages['name'];
            Session::newInstance()->_drop('photos');
            Session::newInstance()->_dropKeepForm('photos');
        }
        ?>
            <div id="restricted-fine-uploader"></div>
            <div style="clear:both;"></div>
            <?php 
        if (count($aImages) > 0 || $resources != null && is_array($resources) && count($resources) > 0) {
            ?>
                <h3><?php 
            _e('Images already uploaded');
            ?>
</h3>
                <ul class="qq-upload-list">
                    <?php 
            foreach ($resources as $_r) {
                $img = $_r['pk_i_id'] . '.' . $_r['s_extension'];
                ?>
                        <li class=" qq-upload-success">
                            <span class="qq-upload-file"><?php 
                echo $img;
                ?>
</span>
                            <a class="qq-upload-delete" href="#" photoid="<?php 
                echo $_r['pk_i_id'];
                ?>
" itemid="<?php 
                echo $_r['fk_i_item_id'];
                ?>
" photoname="<?php 
                echo $_r['s_name'];
                ?>
" photosecret="<?php 
                echo Params::getParam('secret');
                ?>
" style="display: inline; cursor:pointer;"><?php 
                _e('Delete');
                ?>
</a>
                            <div class="ajax_preview_img"><img src="<?php 
                echo osc_apply_filter('resource_path', osc_base_url() . $_r['s_path']) . $_r['pk_i_id'] . '_thumbnail.' . $_r['s_extension'];
                ?>
" alt="<?php 
                echo osc_esc_html($img);
                ?>
"></div>
                        </li>
                    <?php 
            }
            ?>
                    <?php 
            foreach ($aImages as $img) {
                ?>
                        <li class=" qq-upload-success">
                            <span class="qq-upload-file"><?php 
                echo $img;
                $img = osc_esc_html($img);
                ?>
</span>
                            <a class="qq-upload-delete" href="#" ajaxfile="<?php 
                echo $img;
                ?>
" style="display: inline; cursor:pointer;"><?php 
                _e('Delete');
                ?>
</a>
                            <div class="ajax_preview_img"><img src="<?php 
                echo osc_base_url();
                ?>
oc-content/uploads/temp/<?php 
                echo $img;
                ?>
" alt="<?php 
                echo $img;
                ?>
"></div>
                            <input type="hidden" name="ajax_photos[]" value="<?php 
                echo $img;
                ?>
">
                        </li>
                    <?php 
            }
            ?>
                </ul>
            <?php 
        }
        ?>
            <div style="clear:both;"></div>
            <?php 
        $aExt = explode(',', osc_allowed_extension());
        foreach ($aExt as $key => $value) {
            $aExt[$key] = "'" . $value . "'";
        }
        $allowedExtensions = join(',', $aExt);
        $maxSize = (int) osc_max_size_kb() * 1024;
        $maxImages = (int) osc_max_images_per_item();
        ?>

            <script>
                $(document).ready(function() {

                    $('.qq-upload-delete').on('click', function(evt) {
                        evt.preventDefault();
                        var parent = $(this).parent()
                        var result = confirm('<?php 
        echo osc_esc_js(__("This action can't be undone. Are you sure you want to continue?"));
        ?>
');
                        var urlrequest = '';
                        if($(this).attr('ajaxfile')!=undefined) {
                            urlrequest = 'ajax_photo='+$(this).attr('ajaxfile');
                        } else {
                            urlrequest = 'id='+$(this).attr('photoid')+'&item='+$(this).attr('itemid')+'&code='+$(this).attr('photoname')+'&secret='+$(this).attr('photosecret');
                        }
                        if(result) {
                            $.ajax({
                                type: "POST",
                                url: '<?php 
        echo osc_base_url(true);
        ?>
?page=ajax&action=delete_image&'+urlrequest,
                                dataType: 'json',
                                success: function(data){
                                    parent.remove();
                                }
                            });
                        }
                    });

                    $('#restricted-fine-uploader').on('click','.primary_image', function(event){
                        if(parseInt($("div.primary_image").index(this))>0){

                            var a_src   = $(this).parent().find('.ajax_preview_img img').attr('src');
                            var a_title = $(this).parent().find('.ajax_preview_img img').attr('alt');
                            var a_input = $(this).parent().find('input').attr('value');
                            // info
                            var a1 = $(this).parent().find('span.qq-upload-file').text();
                            var a2 = $(this).parent().find('span.qq-upload-size').text();

                            var li_first =  $('ul.qq-upload-list li').get(0);

                            var b_src   = $(li_first).find('.ajax_preview_img img').attr('src');
                            var b_title = $(li_first).find('.ajax_preview_img img').attr('alt');
                            var b_input = $(li_first).find('input').attr('value');
                            var b1      = $(li_first).find('span.qq-upload-file').text();
                            var b2      = $(li_first).find('span.qq-upload-size').text();

                            $(li_first).find('.ajax_preview_img img').attr('src', a_src);
                            $(li_first).find('.ajax_preview_img img').attr('alt', a_title);
                            $(li_first).find('input').attr('value', a_input);
                            $(li_first).find('span.qq-upload-file').text(a1);
                            $(li_first).find('span.qq-upload-size').text(a2);

                            $(this).parent().find('.ajax_preview_img img').attr('src', b_src);
                            $(this).parent().find('.ajax_preview_img img').attr('alt', b_title);
                            $(this).parent().find('input').attr('value', b_input);
                            $(this).parent().find('span.qq-upload-file').text(b1);
                            $(this).parent().find('span.qq-upload-file').text(b2);
                        }
                    });

                    $('#restricted-fine-uploader').on('click','.primary_image', function(event){
                        $(this).addClass('over primary');
                    });

                    $('#restricted-fine-uploader').on('mouseenter mouseleave','.primary_image', function(event){
                        if(event.type=='mouseenter') {
                            if(!$(this).hasClass('primary')) {
                                $(this).addClass('primary');
                            }
                        } else {
                            if(parseInt($("div.primary_image").index(this))>0){
                                $(this).removeClass('primary');
                            }
                        }
                    });


                    $('#restricted-fine-uploader').on('mouseenter mouseleave','li.qq-upload-success', function(event){
                        if(parseInt($("li.qq-upload-success").index(this))>0){

                            if(event.type=='mouseenter') {
                                $(this).find('div.primary_image').addClass('over');
                            } else {
                                $(this).find('div.primary_image').removeClass('over');
                            }
                        }
                    });

                    window.removed_images = 0;
                    $('#restricted-fine-uploader').on('click', 'a.qq-upload-delete', function(event) {
                        window.removed_images = window.removed_images+1;
                        $('#restricted-fine-uploader .flashmessage-error').remove();
                    });

                    $('#restricted-fine-uploader').fineUploader({
                        request: {
                            endpoint: '<?php 
        echo osc_base_url(true) . "?page=ajax&action=ajax_upload";
        ?>
'
                        },
                        multiple: true,
                        validation: {
                            allowedExtensions: [<?php 
        echo $allowedExtensions;
        ?>
],
                            sizeLimit: <?php 
        echo $maxSize;
        ?>
,
                            itemLimit: <?php 
        echo $maxImages;
        ?>
                        },
                        messages: {
                            tooManyItemsError: '<?php 
        echo osc_esc_js(__('Too many items ({netItems}) would be uploaded. Item limit is {itemLimit}.'));
        ?>
',
                            onLeave: '<?php 
        echo osc_esc_js(__('The files are being uploaded, if you leave now the upload will be cancelled.'));
        ?>
',
                            typeError: '<?php 
        echo osc_esc_js(__('{file} has an invalid extension. Valid extension(s): {extensions}.'));
        ?>
',
                            sizeError: '<?php 
        echo osc_esc_js(__('{file} is too large, maximum file size is {sizeLimit}.'));
        ?>
',
                            emptyError: '<?php 
        echo osc_esc_js(__('{file} is empty, please select files again without it.'));
        ?>
'
                        },
                        deleteFile: {
                            enabled: true,
                            method: "POST",
                            forceConfirm: false,
                            endpoint: '<?php 
        echo osc_base_url(true) . "?page=ajax&action=delete_ajax_upload";
        ?>
'
                        },
                        retry: {
                            showAutoRetryNote : true,
                            showButton: true
                        },
                        text: {
                            uploadButton: '<?php 
        echo osc_esc_js(__('Click or Drop for upload images'));
        ?>
',
                            waitingForResponse: '<?php 
        echo osc_esc_js(__('Processing...'));
        ?>
',
                            retryButton: '<?php 
        echo osc_esc_js(__('Retry'));
        ?>
',
                            cancelButton: '<?php 
        echo osc_esc_js(__('Cancel'));
        ?>
',
                            failUpload: '<?php 
        echo osc_esc_js(__('Upload failed'));
        ?>
',
                            deleteButton: '<?php 
        echo osc_esc_js(__('Delete'));
        ?>
',
                            deletingStatusText: '<?php 
        echo osc_esc_js(__('Deleting...'));
        ?>
',
                            formatProgress: '<?php 
        echo osc_esc_js(__('{percent}% of {total_size}'));
        ?>
'
                        }
                    }).on('error', function (event, id, name, errorReason, xhrOrXdr) {
                            $('#restricted-fine-uploader .flashmessage-error').remove();
                            $('#restricted-fine-uploader').append('<div class="flashmessage flashmessage-error">' + errorReason + '<a class="close" onclick="javascript:$(\'.flashmessage-error\').remove();" >X</a></div>');
                    }).on('statusChange', function(event, id, old_status, new_status) {
                        $(".alert.alert-error").remove();
                    }).on('complete', function(event, id, fileName, responseJSON) {
                        if (responseJSON.success) {
                            var new_id = id - removed_images;
                            var li = $('.qq-upload-list li')[new_id];
                            <?php 
        if (Params::getParam('action') == 'item_add') {
            ?>
                            if(parseInt(new_id)==0) {
                                $(li).append('<div class="primary_image primary"></div>');
                            } else {
                                $(li).append('<div class="primary_image"><a title="<?php 
            echo osc_esc_js(osc_esc_html(__('Make primary image')));
            ?>
"></a></div>');
                            }
                            <?php 
        }
        // @TOFIX @FIXME escape $responseJSON_uploadName below
        // need a js function similar to osc_esc_js(osc_esc_html())
        ?>
                            $(li).append('<div class="ajax_preview_img"><img src="<?php 
        echo osc_base_url();
        ?>
oc-content/uploads/temp/'+responseJSON.uploadName+'" alt="' + responseJSON.uploadName + '"></div>');
                            $(li).append('<input type="hidden" name="ajax_photos[]" value="'+responseJSON.uploadName+'"></input>');
                        }
                        <?php 
        if (Params::getParam('action') == 'item_edit') {
            ?>
                    }).on('validateBatch', function(event, fileOrBlobDataArray) {
                        // clear alert messages
                        if($('#restricted-fine-uploader .alert-error').size()>0) {
                            $('#restricted-fine-uploader .alert-error').remove();
                        }

                        var len = fileOrBlobDataArray.length;
                        var result = canContinue(len);
                        return result.success;

                    });

                    function canContinue(numUpload) {
                        // strUrl is whatever URL you need to call
                        var strUrl      = "<?php 
            echo osc_base_url(true) . "?page=ajax&action=ajax_validate&id=" . osc_item_id() . "&secret=" . osc_item_secret();
            ?>
";
                        var strReturn   = {};

                        jQuery.ajax({
                            url: strUrl,
                            success: function(html) {
                                strReturn = html;
                            },
                            async:false
                        });
                        var json  = JSON.parse(strReturn);
                        var total = parseInt(json.count) + $("#restricted-fine-uploader input[name='ajax_photos[]']").size() + (numUpload);
                        <?php 
            if ($maxImages > 0) {
                ?>
                            if(total<=<?php 
                echo $maxImages;
                ?>
) {
                                json.success = true;
                            } else {
                                json.success = false;
                                $('#restricted-fine-uploader .qq-uploader').after($('<div class="alert alert-error"><?php 
                echo osc_esc_js(sprintf(__('Too many items were uploaded. Item limit is %d.'), $maxImages));
                ?>
</div>'));
                            }
                        <?php 
            } else {
                ?>
                            json.success = true;
                        <?php 
            }
            ?>
                        return json;
                    }

                    <?php 
        } else {
            ?>
                });
                <?php 
        }
        ?>
                });

            </script>
        <?php 
    }
Ejemplo n.º 6
0
function digitalgoods_upload_files($item)
{
    if ($item['fk_i_category_id'] != null) {
        if (osc_is_this_category('digitalgoods', $item['fk_i_category_id'])) {
            $files = Params::getFiles('dg_files');
            if (count($files) > 0) {
                require LIB_PATH . 'osclass/mimes.php';
                $aMimesAllowed = array();
                $aExt = explode(',', osc_get_preference('allowed_ext', 'digitalgoods'));
                foreach ($aExt as $ext) {
                    $mime = $mimes[$ext];
                    if (is_array($mime)) {
                        foreach ($mime as $aux) {
                            if (!in_array($aux, $aMimesAllowed)) {
                                array_push($aMimesAllowed, $aux);
                            }
                        }
                    } else {
                        if (!in_array($mime, $aMimesAllowed)) {
                            array_push($aMimesAllowed, $mime);
                        }
                    }
                }
                $failed = false;
                $maxSize = osc_max_size_kb() * 1024;
                foreach ($files['error'] as $key => $error) {
                    $bool_img = false;
                    if ($error == UPLOAD_ERR_OK) {
                        $size = $files['size'][$key];
                        if ($size <= $maxSize) {
                            $fileMime = $files['type'][$key];
                            if (in_array($fileMime, $aMimesAllowed)) {
                                $date = date('YmdHis');
                                $file_name = $date . '_' . $item['pk_i_id'] . '_' . $files['name'][$key];
                                $path = osc_get_preference('upload_path', 'digitalgoods') . $file_name;
                                if (move_uploaded_file($files['tmp_name'][$key], $path)) {
                                    DGModel::newInstance()->insertFile($item['pk_i_id'], $files['name'][$key], $date);
                                } else {
                                    $failed = true;
                                }
                            } else {
                                $failed = true;
                            }
                        } else {
                            $failed = true;
                        }
                    }
                }
                if ($failed) {
                    osc_add_flash_error_message(__('Some of the files were not uploaded because they have incorrect extension', 'digitalgoods'), 'admin');
                }
            }
        }
    }
}
Ejemplo n.º 7
0
 public function uploadItemResources($aResources, $itemId)
 {
     if ($aResources != '') {
         $wat = new Watermark();
         $itemResourceManager = ItemResource::newInstance();
         $numImagesItems = osc_max_images_per_item();
         $numImages = $itemResourceManager->countResources($itemId);
         foreach ($aResources['error'] as $key => $error) {
             if ($numImagesItems == 0 || $numImagesItems > 0 && $numImages < $numImagesItems) {
                 if ($error == UPLOAD_ERR_OK) {
                     $freedisk = 4 * osc_max_size_kb() * 1024;
                     if (function_exists('disk_free_space')) {
                         $freedisk = @disk_free_space(osc_content_path() . 'uploads/');
                     }
                     if ($freedisk != false) {
                         $tmpName = $aResources['tmp_name'][$key];
                         $total_size = 0;
                         // Create normal size
                         $normal_path = $path = $tmpName . "_normal";
                         $size = explode('x', osc_normal_dimensions());
                         ImageResizer::fromFile($tmpName)->resizeTo($size[0], $size[1])->saveToFile($path);
                         if (osc_is_watermark_text()) {
                             $wat->doWatermarkText($path, osc_watermark_text_color(), osc_watermark_text(), 'image/jpeg');
                         } elseif (osc_is_watermark_image()) {
                             $wat->doWatermarkImage($path, 'image/jpeg');
                         }
                         $sizeTmp = filesize($path);
                         $total_size += $sizeTmp !== false ? $sizeTmp : osc_max_size_kb() * 1024;
                         // Create preview
                         $path = $tmpName . "_preview";
                         $size = explode('x', osc_preview_dimensions());
                         ImageResizer::fromFile($normal_path)->resizeTo($size[0], $size[1])->saveToFile($path);
                         $sizeTmp = filesize($path);
                         $total_size += $sizeTmp !== false ? $sizeTmp : osc_max_size_kb() * 1024;
                         // Create thumbnail
                         $path = $tmpName . "_thumbnail";
                         $size = explode('x', osc_thumbnail_dimensions());
                         ImageResizer::fromFile($normal_path)->resizeTo($size[0], $size[1])->saveToFile($path);
                         $sizeTmp = filesize($path);
                         $total_size += $sizeTmp !== false ? $sizeTmp : osc_max_size_kb() * 1024;
                         if (osc_keep_original_image()) {
                             $sizeTmp = filesize($tmpName);
                             $total_size += $sizeTmp !== false ? $sizeTmp : osc_max_size_kb() * 1024;
                         }
                         if ($total_size <= $freedisk) {
                             $numImages++;
                             $itemResourceManager->insert(array('fk_i_item_id' => $itemId));
                             $resourceId = $itemResourceManager->dao->insertedId();
                             osc_copy($tmpName . '_normal', osc_content_path() . 'uploads/' . $resourceId . '.jpg');
                             osc_copy($tmpName . '_preview', osc_content_path() . 'uploads/' . $resourceId . '_preview.jpg');
                             osc_copy($tmpName . '_thumbnail', osc_content_path() . 'uploads/' . $resourceId . '_thumbnail.jpg');
                             if (osc_keep_original_image()) {
                                 $path = osc_content_path() . 'uploads/' . $resourceId . '_original.jpg';
                                 move_uploaded_file($tmpName, $path);
                             }
                             $s_path = 'oc-content/uploads/';
                             $resourceType = 'image/jpeg';
                             $itemResourceManager->update(array('s_path' => $s_path, 's_name' => osc_genRandomPassword(), 's_extension' => 'jpg', 's_content_type' => $resourceType), array('pk_i_id' => $resourceId, 'fk_i_item_id' => $itemId));
                             osc_run_hook('uploaded_file', ItemResource::newInstance()->findByPrimaryKey($resourceId));
                         } else {
                             return 2;
                             // IMAGES ARE BIGGER THAN SPACE
                         }
                     } else {
                         return 1;
                         // NO SPACE LEFT
                     }
                 }
             }
         }
         unset($itemResourceManager);
     }
     return 0;
     // NO PROBLEMS
 }