<?php
// get user
$Auth = Auth::getAuth();

// load plugin details
$pluginConfig   = pluginHelper::pluginSpecificConfiguration('ftpupload');
$pluginSettings = json_decode($pluginConfig['data']['plugin_settings'], true);

$doRedirect = true;
if (($pluginSettings['paid_only'] == 1) && ($Auth->level == 'free user'))
{
    $doRedirect = false;
}

$showTab = false;
if (($pluginSettings['show_ftp_tab'] == 1) || ($doRedirect == true))
{
    $showTab = true;
}

if ($showTab == true)
{
    ?>

    <script>
        var ftpFileList = null;
        $(document).ready(function() {
            // check for pending files
            $('#pendingFilesWrapper').load('<?php echo PLUGIN_WEB_ROOT . '/' . $pluginConfig['data']['folder_name'] . '/site/_pending_ftp_files.ajax.php'; ?>');
        });

        function transferFtpFiles()
        {
            // get pending file listing
            ftpFileList = $('#ftp_file_listing').val().split("|");

            // create table listing
            html = '';
            for (i in ftpFileList)
            {
                html += '<tr id="ftpRowId' + i + '"><td class="cancel"><a href="#" onClick="return false;"><img src="<?php echo SITE_IMAGE_PATH; ?>/processing_small.gif" class="processingIcon" height="16" width="16" alt="<?php echo str_replace("'", "\'", t('processing', 'processing')); ?>"/>';
                html += '</a></td><td class="name" colspan="3">' + ftpFileList[i] + '</td></tr>';
            }
            $('#pendingFilesWrapper #urls').html(html);

            // show file uploader screen
            $('#ftpUpload #ftpFileListingWrapper').removeClass('hidden');
            $('#ftpUpload #ftpFileUploader').addClass('hidden');
            $('#ftpUpload #fileUploadBadge').addClass('hidden');
            $('#ftpUpload #ftpUserConnectionDetails').addClass('hidden');

            // loop over urls and try to retieve the file
            for (i in ftpFileList)
            {
                // call ajax request to get file
                var request = $.ajax({
                    url: "<?php echo file::getUploadUrl() . '/'.PLUGIN_DIRECTORY_NAME.'/' . $pluginConfig['data']['folder_name'] . '/site/ftpUploadHandler.ajax.php'; ?>",
                    type: "POST",
                    ysrowId: i,
                    xhrFields: {
                        withCredentials: true
                    },
                    data: {fileName: ftpFileList[i], rowId: i},
                    dataType: "json"
                });

                request.done(function(data) {
                    var isSuccess = true;
                    if (data.error != null)
                    {
                        isSuccess = false;
                    }

                    var html = '';
                    html += '<tr class="template-download';
                    if (isSuccess == false)
                    {
                        html += ' errorText';
                    }
                    html += '" onClick="return showAdditionalInformation(this);">'
                    if (isSuccess == false)
                    {
                        // add result html
                        html += data.error_result_html;
                    }
                    else
                    {
                        // add result html
                        html += data.success_result_html;

                        // keep a copy of the urls globally
                        fileUrls.push(data.url);
                        fileDeleteHashes.push(data.delete_hash);
                        fileShortUrls.push(data.short_url);
                    }

                    html += '</tr>';

                    $('#ftpRowId' + data.rowId).replaceWith(html);

                    // completed uploading
                    if (i == ftpFileList.length - 1)
                    {
                        // show footer
                        $('#ftpUpload .fileSectionFooterText').removeClass('hidden');
                        
                        <?php if((float)_CONFIG_SCRIPT_VERSION >= 3.3): ?>
                            // update email/password
                            sendAdditionalOptions();
                        <?php endif; ?>
                    }
                });

                request.fail(function(jqXHR, textStatus) {
                    $('#ftpRowId'+this.ysrowId+' .cancel .processingIcon').attr('src', '<?php echo SITE_IMAGE_PATH; ?>/red_error_small.png');
                    $('#ftpRowId'+this.ysrowId+' .name').html('Failed to get file, possible ajax issue');
                });
            }
        }
    </script>

    <!-- FTP UPLOAD -->
    <div id="ftpUpload">
        <div class="urlUploadMain ui-corner-all">
            <div id="fileUploadBadge" class="fileUploadBadge"></div>
            <div class="urlUploadMainInternal contentPageWrapper" style="width: auto;">
                <div>
                    <div class="initialUploadText">
                        <div class="uploadText">
                            <h2><?php echo t('ftp_upload', 'FTP Upload'); ?>:</h2>
                        </div>
                        <div class="clearLeft"><!-- --></div>

                        <div>
                            <?php
                            $showContent = true;
                            if (($pluginSettings['paid_only'] == 1) && ($Auth->level == 'free user'))
                            {
                                $showContent = false;
                            }

                            if (($showContent == true) && ($Auth->loggedIn() == false))
                            {
                                $showContent = false;
                            }

                            if ($showContent)
                            {
                                // make sure we have an ftp account for the logged in user
                                $pluginObj = pluginHelper::getInstance('ftpupload');
                                $rs        = $pluginObj->getFTPAccountDetails($Auth->id);
                                if ($rs['success'] == false)
                                {
                                    echo t("plugin_ftp_error_loading_ftp_details", "There was an error loading your FTP details, please contact support for more information.");
                                    echo '<br/><br/>';
                                    echo t("error_message", "Error message");
                                    echo ": ";
                                    echo $rs['msg'];
                                }
                                else
                                {
                                    // max allowed upload size & max permitted urls
                                    $maxUploadSize = SITE_CONFIG_FREE_USER_MAX_UPLOAD_FILESIZE;
                                    if ($Auth->loggedIn())
                                    {
                                        // check if user is a premium/paid user
                                        if ($Auth->level != 'free user')
                                        {
                                            $maxUploadSize = SITE_CONFIG_PREMIUM_USER_MAX_UPLOAD_FILESIZE;
                                        }
                                    }

                                    // get accepted file types
                                    $acceptedFileTypes = getAcceptedFileTypes();
                                    ?>

                                    <div id="pendingFilesWrapper"><!-- --></div>

                                    <div id="ftpUserConnectionDetails">
                                        <div style="width: 79%;">
                                            <?php echo t("plugin_ftp_use_the_ftp_details_below_to_connect", "Please use the FTP details below to connect and upload files using an FTP client such as <a href=\"http://filezilla-project.org/\" target=\"_blank\">FileZilla</a>. Once you've completed your uploads, return to this page to import them into your account."); ?>
                                        </div>
                                        <div style="width: 79%; padding-top: 22px; padding-bottom: 22px;">
                                            <table class="accountStateTable">
                                                <tbody>
                                                    <tr>
                                                        <td class="first"><?php echo t("plugin_ftp_ftp_host", "FTP Host"); ?>:</td>
                                                        <td><?php echo $pluginSettings['connection_cpanel_host']; ?></td>
                                                    </tr>
                                                    <tr>
                                                        <td class="first"><?php echo t("plugin_ftp_ftp_username", "FTP Username"); ?>:</td>
                                                        <td><?php echo $rs['ftp_user']; ?>@<?php echo $pluginSettings['connection_cpanel_host']; ?></td>
                                                    </tr>
                                                    <tr>
                                                        <td class="first"><?php echo t("plugin_ftp_ftp_password", "FTP Password"); ?>:</td>
                                                        <td><?php echo $rs['ftp_password']; ?></td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="baseText" style="margin-top: 12px;">
                                            <?php if((float)_CONFIG_SCRIPT_VERSION >= 3.3): ?><a class="showAdditionalOptionsLink"><?php echo UCFirst(t('options', 'options')); ?></a>&nbsp;&nbsp;|&nbsp;&nbsp;<?php endif; ?><?php echo t("plugin_ftp_maximum_filesize_of", "Maximum file size of"); ?> <?php echo formatSize($maxUploadSize); ?> <?php echo t("plugin_ftp_applies", "applies"); ?>. <?php echo COUNT($acceptedFileTypes) ? (t('allowed_file_types', 'Allowed file types') . ': ' . str_replace(".", "", implode(", ", $acceptedFileTypes)) . '.') : ''; ?>
                                        </div>
                                    </div>
                                    <?
                                }
                                ?>

                                <?php
                            }
                            else
                            {
                                echo t("plugin_ftp_you_need_an_account_upload_file_via_ftp", "You need an account to upload files using FTP. Go to the <a href='[[[WEB_ROOT]]]/register.[[[SITE_CONFIG_PAGE_EXTENSION]]]'>registration page</a> to create an account now.", array('WEB_ROOT'                   => WEB_ROOT, 'SITE_CONFIG_PAGE_EXTENSION' => SITE_CONFIG_PAGE_EXTENSION));
                            }
                            ?>
                        </div>
                    </div>
                    <div class="clear"><!-- --></div>
                </div>

                <div class="clear"><!-- --></div>
            </div>
        </div>
    </div>
    <?php
}
?>