<?php
/**
 * Show a list of images in a long horizontal table.
 * Modified for TinyMCE - Ryan Demmer Dec 2004
 * Portions from remository.php
 * @Original author $Author: Wei Zhuo $
 * @version $Id: images.php 27 2004-04-01 08:31:57Z Wei Zhuo $
 * @package ImageManager
 */
define( '_VALID_MOS', 1 );

$base_path = '../../../../../../../../';

//Start Authorisation
//Some includes
include_once ($base_path."/configuration.php");
$tinyMCE_path = $mosConfig_absolute_path."/mambots/editors/mosce/jscripts/tiny_mce/";
$tinyMCE_URL = $mosConfig_live_site."/mambots/editors/mosce/jscripts/tiny_mce";

include_once ($tinyMCE_path."auth_plugin.php");
include ($tinyMCE_path."mosce_config.php");
include ($tinyMCE_path."plugins/imgmanager/langs/$editor_lang.php");

//Reset auth variables
$isAdmin=False;
$isUser=False;
$upload_auth=False;
$new_folder_auth=False;

// Check for access rights

// Check for Admin rights
$isAdmin=checkType('admin');

// Check for User rights
if (!$isAdmin) {
     $isUser=checkType('user');
     $image_delete_auth=checkType('IM_image_delete');
     $image_edit_auth=checkType('IM_image_edit');
     $folder_delete_auth=checkType('IM_folder_delete');
   }

if ($isAdmin || $isUser) {

//End Authorisation

include_once ($tinyMCE_path."plugins/imgmanager/ImageManager/config.inc.php");
include_once ($tinyMCE_path."plugins/imgmanager/ImageManager/Classes/ImageManager.php");
include_once ($tinyMCE_path."plugins/imgmanager/ImageManager/Classes/Functions.php");

//default path is /
$relative = '/';
$manager = new ImageManager($IMConfig);

//process any file uploads
$manager->processUploads();

$manager->deleteFiles();
$manager->new_thumb();
$manager->del_thumb();

$refreshDir = false;
//process any directory functions
if($manager->deleteDirs() || $manager->processNewDir())
        $refreshDir = true;

//check for any sub-directory request
//check that the requested sub-directory exists
//and valid
if(isset($_REQUEST['dir']))
{
        $path = rawurldecode($_REQUEST['dir']);
        if($manager->validRelativePath($path))
                $relative = $path;
}

//get the list of files and directories
$list = $manager->getFiles($relative);

//Define some variables - Ryan Demmer. Added for TinyMCE

$imagepath = $IMConfig['img_url'];
$thumbnail_prefix = $IMConfig['thumbnail_prefix'];

/* ================= OUTPUT/DRAW FUNCTIONS ======================= */

/**
 * Draw the files in an table.
 */
//Added modifications for TinyMCE - Ryan Demmer
function drawFiles($list, &$manager)
{
        global $relative, $imagepath, $image_delete_auth, $image_edit_auth, $isAdmin, $editor_im_image, $editor_im_image_edit, $thumbnail_prefix, $editor_im_thumbs;

        foreach($list as $entry => $file)
        {
                //Return relative thumbnail path
                $thumb = $manager->getThumbnail($file['relative']);
                if (empty($thumb)){
                    $display_thumb = $file['url'];
                }else{
                    $display_thumb = $thumb;
                }
                $thumbData = $manager->processThumb($thumb);
                $prevImage = $manager->processThumb($display_thumb);
                if(!empty($thumbData)){
                    $thumbSrc = $thumbData['src'];
                    $thumbAlt = $thumbData['name'];
                    $thumbW = $thumbData['width'];
                    $thumbH = $thumbData['height'];
                }else{
                    $thumbSrc = 'null';
                    $thumbAlt = 'null';
                    $thumbW = 'null';
                    $thumbH = 'null';
                }
                $maxWH = '72';
                $prev_width = $maxWH;
                if(($prevImage['width'] > $maxWH) && ($prevImage['width'] > $prevImage['height'])){
                    $prev_width = $maxWH;
                    $prev_height = intval(($maxWH/$prevImage['width'])*$prevImage['height']);
                }
                if(($prevImage['height'] > $maxWH) && ($prevImage['height'] > $prevImage['width'])){
                    $prev_height = $maxWH;
                    $prev_width = intval(($maxWH/$prevImage['height'])*$prevImage['width']);
                }

        ?>
                <td><table width="100" cellpadding="0" cellspacing="0"><tr><td class="block">
                <a href="javascript:;"onclick="selectImage('<?php echo $file['url'];?>', '<?php echo $entry; ?>', '<?php echo $file['image'][0];?>', '<?php echo $file['image'][1]; ?>', '<?php echo $thumbSrc; ?>', '<?php echo $thumbAlt; ?>', '<?php echo $thumbW; ?>', '<?php echo $thumbH; ?>');"title="<?php echo $entry; ?> - <?php echo Files::formatSize($file['stat']['size']); ?>"><img src="<?php echo $display_thumb; ?>" alt="<?php echo $entry; ?> - <?php echo Files::formatSize($file['stat']['size']); ?>" height="<?php echo $prev_height;?>px" width="<?php echo $prev_width;?>px" /></a>
                <div style="position: bottom; float:right">
                <?php if(!empty($thumb) && !$manager->isThumb($entry) && $manager->getThumbnail($file['relative']) != $file['url'] && $editor_im_thumbs == 'true'){ ?>
                    <a href="images.php?dir=<?php echo $relative; ?>&amp;delThumb=<?php echo rawurlencode($file['relative']);?>" title="<?php echo _imgmanager_del ?>" onclick="return confirmDelThumb('<?php echo "$thumbnail_prefix".$entry; ?>');"><img src="img/thumb_del.gif" width="16" height="16" title="<?php echo _imgmanager_del_thumb ?>" /></a>
                <?php }
                      if(empty($thumb) && !$manager->isThumb($entry) && $editor_im_thumbs == 'true'){ ?>
                    <a href="images.php?dir=<?php echo $relative; ?>&amp;newThumb=<?php echo rawurlencode($file['relative']);?>" title="<?php echo _imgmanager_del ?>" onclick="return confirmNewThumb('<?php echo "$thumbnail_prefix".$entry; ?>');"><img src="img/thumb_create.gif" width="16" height="16" title="<?php echo _imgmanager_create_thumb ?>" /></a>
                <?php }
                      if($editor_im_thumbs == 'true' && $manager->getThumbnail($file['relative']) == $file['url'] || $manager->isThumb($file['relative'])){ ?>
                    <img src="img/thumb.gif" title="<?php echo _imgmanager_is_thumb ?>" />
                <?php } ?>
                </div>
                </td></tr>
                <tr>
                    <td class="edit" align="center"><?php echo $entry; ?></td>
                </tr>
                <tr>
                <td align="center" class="edit">
                <?php if(($image_delete_auth || $isAdmin) && $editor_im_image){?>
                            <a href="images.php?dir=<?php echo $relative; ?>&amp;delf=<?php echo rawurlencode($file['relative']);?>" title="<?php echo _imgmanager_del ?>" onclick="return confirmDeleteFile('<?php echo $entry; ?>');"><img src="img/del.gif" height="16" width="16" border="0" alt="<?php echo _imgmanager_del ?>" /></a>
                <?php } ?>
                <?php if ($editor_im_thumbs == 'true' && $manager->validImageType($imagepath.$file['relative']) && ($image_edit_auth || $isAdmin) && $editor_im_image_edit){ ?>
                        <a href="javascript:;" title="<?php echo _imgmanager_edit ?>" onclick="editImage('<?php echo rawurlencode($file['relative']);?>');"><img src="img/edit.gif" height="16" width="16" border="0" alt="<?php echo _imgmanager_edit ?>" /></a>
                <?php } ?>
                        <a href="javascript:;" title="<?php echo _imgmanager_view ?>" onclick="viewImage('<?php echo "$imagepath".$file['relative'];?>', '<?php echo $entry; ?>', <?php echo $file['image'][0];?>, <?php echo $file['image'][1]; ?>, '<?php echo Files::formatSize($file['stat']['size']); ?>');"><img src="img/prev.gif" height="16" width="16" border="0" alt="<?php echo _imgmanager_view ?>" /></a>
                </td>
                </tr></table></td>
          <?php
        }//foreach
}//function drawFiles

/**
 * Draw the directory.
 */
function drawDirs($list, &$manager)
{
        global $relative, $folder_delete_auth, $isAdmin, $editor_im_folder;

        foreach($list as $path => $dir)
        {
          ?>
                <td><table width="100" cellpadding="0" cellspacing="0"><tr><td class="block">
                <a href="images.php?dir=<?php echo rawurlencode($path); ?>" onclick="updateDir('<?php echo $path; ?>')" title="<?php echo $dir['entry']; ?>"><img src="img/folder.gif" height="80" width="80" alt="<?php echo $dir['entry']; ?>" /></a>
                </td></tr><tr>
                <td class="edit">
                <?php if (($folder_delete_auth || $isAdmin) && $editor_im_folder){ ?>
                        <a href="images.php?dir=<?php echo $relative; ?>&amp;deld=<?php echo rawurlencode($path); ?>" title="<?php echo _imgmanager_del ?>" onclick="return confirmDeleteDir('<?php echo $dir['entry']; ?>', <?php echo $dir['count']; ?>);"><img src="img/del.gif" height="16" width="16" alt="<?php echo _imgmanager_del ?>"/></a>
                        <?php } ?>
                        <?php echo $dir['entry']; ?>
                </td>
                </tr></table></td>
          <?php
        } //foreach
}//function drawDirs

/**
 * No directories and no files.
 */
function drawNoResults()
{
?>
<table width="100%">
  <tr>
    <td class="noResult"><?php echo _imgmanager_noImages ?></td>
  </tr>
</table>
<?php
}

/**
 * No directories and no files.
 */
function drawErrorBase(&$manager)
{
?>
<table width="100%">
  <tr>
    <td class="error"><?php echo _imgmanager_invalidDir ?>: <?php echo $manager->config['base_dir']; ?></td>
  </tr>
</table>
<?php
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>Image List</title>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo _imgmanager_iso;?>" />
<link href="<?php echo $tinyMCE_URL;?>/plugins/imgmanager/ImageManager/assets/imagelist.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="<?php echo $tinyMCE_URL;?>/plugins/imgmanager/ImageManager/assets/dialog.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
        function hideMessage()
        {
                var topDoc = window.top.document;
                var messages = topDoc.getElementById('messages');
                if(messages)
                        messages.style.display = "none";
        }

        function init()
        {
                hideMessage();
                var topDoc = window.top.document;

<?php
        //we need to refesh the drop directory list
        //save the current dir, delete all select options
        //add the new list, re-select the saved dir.
        if($refreshDir)
        {
                $dirs = $manager->getDirs();
?>
                var selection = topDoc.getElementById('dirPath');
                var currentDir = selection.options[selection.selectedIndex].text;

                while(selection.length > 0)
                {        selection.remove(0); }

                selection.options[selection.length] = new Option("/","<?php echo rawurlencode('/'); ?>");
                <?php foreach($dirs as $relative=>$fullpath) { ?>
                selection.options[selection.length] = new Option("<?php echo $relative; ?>","<?php echo rawurlencode($relative); ?>");
                <?php } ?>

                for(var i = 0; i < selection.length; i++)
                {
                        var thisDir = selection.options[i].text;
                        if(thisDir == currentDir)
                        {
                                selection.selectedIndex = i;
                                break;
                        }
                }
<?php } ?>
        }

        function editImage(image)
        {
                var url = "editor.php?img="+image;
                Dialog(url, function(param)
                {
                        if (!param) // user must have pressed Cancel
                                return false;
                        else
                        {
                                return true;
                        }
                }, null);
        }

        //Added option to view full image in popup - Ryan Demmer Feb 2005
        function viewImage(image, alt,imgwidth,imgheight,imgsize)
        {
               var url = "preview.php?img="+image+"&alt="+alt+"&imgsize="+imgsize;
               window.open(url,'Preview','menubar=no,toolbar=no,scrollbars=no,resizable=no, left='+(screen.availWidth/2-(imgwidth/2))+',top='+(screen.availHeight/2-(imgheight/2))+',width='+imgwidth+',height='+imgheight+'');
        }
        function changeDir(newDir)
        {
                showMessage('Loading');
                location.href = "images.php?dir="+newDir;
        }

        function newFolder(dir, newDir)
        {
                location.href = "images.php?dir="+dir+"&newDir="+newDir;
        }
        function doUpload(dir)
        {
                location.href = "images.php?dir="+dir;
        }

        //update the dir list in the parent window.
        function updateDir(newDir)
        {
                var selection = window.top.document.getElementById('dirPath');
                if(selection)
                {
                        for(var i = 0; i < selection.length; i++)
                        {
                                var thisDir = selection.options[i].text;
                                if(thisDir == newDir)
                                {
                                        selection.selectedIndex = i;
                                        showMessage('Loading');
                                        break;
                                }
                        }
                }
        }
        function selectImage(filename, alt, width, height, thumbSrc, thumbAlt, thumbWidth, thumbHeight)
        {
                var topDoc = window.top.document;

                if (topDoc.getElementById('main').checked)
                {
                    topDoc.getElementById('f_url').value = filename;
                    topDoc.getElementById('f_width').value = width;
                    topDoc.getElementById('f_height').value = height;
                    topDoc.getElementById('f_alt').value = alt;
                    topDoc.getElementById('original_width').value = width;
                    topDoc.getElementById('original_height').value = height;
                    if (topDoc.getElementById('onmouseout').disabled == false){
                        topDoc.getElementById('onmouseout').value = filename;
                    }
                }
                if (topDoc.getElementById('option').checked && topDoc.getElementById('popupenable').checked == true)
                {
                    if(thumbSrc != 'null'){
                        if(confirm('Use image\'s thumbnail?')){
                            topDoc.getElementById('f_url').value = thumbSrc;
                            topDoc.getElementById('f_width').value = thumbWidth;
                            topDoc.getElementById('f_height').value = thumbHeight;
                            topDoc.getElementById('f_alt').value = thumbAlt;
                            topDoc.getElementById('original_width').value = thumbWidth;
                            topDoc.getElementById('original_height').value = thumbHeight;
                        }
                    }
                    topDoc.getElementById('popupthumb').value = filename;
                    topDoc.getElementById('thumbWidth').value = width;
                    topDoc.getElementById('thumbHeight').value = height;
                    topDoc.getElementById('thumbname').value = alt;
                    topDoc.getElementById('original_width_thumb').value = width;
                    topDoc.getElementById('original_height_thumb').value = height;
                }
                if (topDoc.getElementById('option').checked && topDoc.getElementById('onmousemove').checked)
                {
                    topDoc.getElementById('onmouseover').value = filename;
                }
        }
        function showMessage(newMessage)
        {
                var topDoc = window.top.document;

                var message = topDoc.getElementById('message');
                var messages = topDoc.getElementById('messages');
                if(message && messages)
                {
                        if(message.firstChild)
                                message.removeChild(message.firstChild);

                        message.appendChild(topDoc.createTextNode(newMessage));

                        messages.style.display = "block";
                }
        }

        function confirmNewThumb(file)
        {
                if(confirm("Create Thumbnail "+file+"?"))
                        return true;

                return false;
        }

        function confirmDelThumb(file)
        {
                if(confirm("Delete Thumbnail "+file+"?"))
                        return true;

                return false;
        }

        function addEvent(obj, evType, fn)
        {
                if (obj.addEventListener) { obj.addEventListener(evType, fn, true); return true; }
                else if (obj.attachEvent) {  var r = obj.attachEvent("on"+evType, fn);  return r;  }
                else {  return false; }
        }

        function confirmDeleteFile(file)
        {
                if(confirm("Delete file?"))
                        return true;

                return false;
        }

        function confirmDeleteDir(dir, count)
        {
                if(count > 0)
                {
                        alert("Please delete all files/folders inside the folder you wish to delete first.");
                        return;
                }

                if(confirm("Delete folder?"))
                        return true;

                return false;
        }

/*]]>*/
</script>
</head>

<body style="background-color:#FFFFFF" onload="init();">
<?php if ($manager->isValidBase() == false) { drawErrorBase($manager); }
        elseif(count($list[0]) > 0 || count($list[1]) > 0) { ?>
<table>
        <tr>
        <?php drawDirs($list[0], $manager); ?>
        <?php drawFiles($list[1], $manager); ?>
        </tr>
</table>
<?php } else { drawNoResults(); } ?>
<script type="text/javascript" src="<?php echo $tinyMCE_URL;?>/plugins/imgmanager/ImageManager/assets/wz_tooltip.js"></script>
</body>
</html>
<?php
}else{
die( 'You are not authorised to view this page.' );
}
?>
