<?php
/**
* @version $Id: mosce.php,v 1.0 2005/05/09 17:08:08 happy_noodle_boy Exp $
* @package Mambo_4.5.2
* @Based on tinymce.php by stingrey
* @copyright (C) 2000 - 2004 Miro International Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* Mambo is Free Software
*/

/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onInitEditor', 'botmosceEditorInit' );
$_MAMBOTS->registerFunction( 'onGetEditorContents', 'botmosceEditorGetContents' );
$_MAMBOTS->registerFunction( 'onEditorArea', 'botmosceEditorEditorArea' );

Global $mosConfig_live_site, $jspath;
include ($mosConfig_absolute_path."/mambots/editors/mosce/jscripts/tiny_mce/mosce_config.php");
// --- Start custom code DHS Informatisering ---
// ---     opensource@dhs.nl, www.dhs.nl     ---

// This code makes it possible to use mosce in a
// website that combines normal and SSL-connections

//if(isset($_SERVER['HTTPS'])) {
if (isset($_SERVER['HTTPS']) &&  $_SERVER['HTTPS'] == "on") { //Modified by Ryan Demmer 01/06/2005 ryandemmer@gmail.com
	$jspath = "https:" . stristr ( $editor_base_path, "//" );
} else {
	$jspath = "http:" . stristr ( $editor_base_path, "//" );
}

// With search and replace we replaced every occurrence
// of '$mosConfig_live_site' with '$jspath' in this file
// --- End custom code DHS Informatisering ---


/**
* TinyMCE WYSIWYG Editor - javascript initialisation
*/
function botmosceEditorInit() {

        global $Itemid, $database, $mosConfig_live_site, $mosConfig_absolute_path, $jspath;

        include ($mosConfig_absolute_path."/mambots/editors/mosce/jscripts/tiny_mce/mosce_config.php");
        require_once ($mosConfig_absolute_path."/mambots/editors/mosce/jscripts/tiny_mce/auth_mosce.php");

        //Reset auth variables
        $isAdmin=False;
        $flashAuth=False;
        $imgmanagerAuth=False;
        $filemanagerAuth=False;
        $fontAuth=False;
        $emotionsAuth=False;
        $advlinkAuth=False;
        $scriptAuth=False;
        $iframeAuth=False;
        $tableAuth=False;
        //Check for Admin rights
        $isAdmin=checkType('admin');
        //Check for User rights
        if (!$isAdmin) {
             $imgmanagerAuth=checktype('imgmanager');
             $filemanagerAuth=checktype('filemanager');
             $flashAuth=checkType('flash');
             $fontAuth=checkType('font');
             $emotionsAuth=checkType('emotions');
             $advlinkAuth=checkType('advlink');
             $scriptAuth=checkType('script');
             $iframeAuth=checkType('iframe');
             $tableAuth=checkType('table');
           }
        // Get the default stylesheet
        $query = "SELECT template FROM #__templates_menu WHERE client_id='0' AND menuid='0'";
        $database->setQuery($query );
        $cur_template = $database->loadResult();
        // Assigned template
        if (isset($Itemid) && $Itemid!="" && $Itemid!=0) {
            $query = "SELECT template FROM #__templates_menu WHERE client_id='0' AND menuid='$Itemid' LIMIT 1";
            $database->setQuery($query);
            $cur_template = $database->loadResult() ? $database->loadResult() : $cur_template;
        }
        //Plugin settings and Authorisation
        //Image Manager
        if ($isAdmin || $imgmanagerAuth){
            $editor_plugin_imgmanager = $editor_plugin_imgmanager;
        }
        //File Manager
        if ($isAdmin || $filemanagerAuth){
            $editor_plugin_filemanager = $editor_plugin_filemanager;
        }
        //Flash Manager
        if ($isAdmin || $flashAuth){
            $editor_plugin_flash = $editor_plugin_flash;
        }
        //Font Options
        if ($editor_font_tools == '1' && ($isAdmin || $fontAuth)){
            $font_elm = "font[face|size|color|style]";
            $row_search = array("([\r\n])");
            $row_replace = array("");
       }else{
            $row_search = array("([\r\n])","/fontselect/","/fontsizeselect/","/forecolor/","/backcolor/");
            $row_replace = array("","","","","");
            $font_elm = "";
       }
       //Emotions
        if ($isAdmin || $emotionsAuth){
            $editor_plugin_emotions = $editor_plugin_emotions;
        }
        //Advanced Link
        if ($isAdmin || $advlinkAuth){
            $editor_plugin_advlink = $editor_plugin_advlink;
        }
        //Tables
        if ($isAdmin || $tableAuth){
            $editor_plugin_table = $editor_plugin_table;
        }
        $table_elm = $editor_table_colour == 'true' ? 'table[bgcolor|border=0|cellspacing|cellpadding|width|height|class|align],tr[bgcolor|class|rowspan|width|height|align|valign],td[bgcolor|class|colspan|rowspan|width|height|align|valign]' : '';

        //Paragraphs or breaks
        if ($editor_newlines == 'p'){
            $p_newlines = "true";
            $br_newlines = "false";
        }
        if ($editor_newlines == 'br'){
            $p_newlines = "false";
            $br_newlines = "true";
        }
        //Plugins List
        $plugins = "$editor_plugin_imgmanager,$editor_plugin_advlink,$editor_plugin_emotions,$editor_plugin_flash,$editor_plugin_print,$editor_plugin_searchreplace,$editor_plugin_filemanager,$editor_plugin_table";

        if ($editor_script == '1' && ($isAdmin || $scriptAuth)){
            $script_invalid = "null";
            $jscript_elm = "script[$editor_script_elms]";
        } else {
          $script_invalid = "script";
          $jscript_elm = "";
        }

        if ($editor_iframe == '1' && ($isAdmin || $iframeAuth)){
            $iframe_invalid = "null";
            $iframe_elm = "iframe[$editor_iframe_elms]";
        } else {
          $iframe_invalid = "iframe";
          $iframe_elm = "";
        }

        $content_css = $mosConfig_live_site."/templates/".$cur_template."/css/template_css.css";
        $custom_css = $mosConfig_live_site."/templates/".$cur_template."/css/".$editor_custom_css;

        if ($editor_css_override == '1' ){
            $content_css = $custom_css;
        }
        
        $editor_layout_row1 = preg_replace($row_search, $row_replace, $editor_layout_row1);
        $editor_layout_row2 = preg_replace($row_search, $row_replace, $editor_layout_row2);
        $editor_layout_row3 = preg_replace($row_search, $row_replace, $editor_layout_row3);
        $editor_layout_row4 = preg_replace($row_search, $row_replace, $editor_layout_row4);
        
        return <<<EOD
<script type="text/javascript" src="$mosConfig_live_site/mambots/editors/mosce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
        tinyMCE.init({
                theme : "advanced",
                language : "$editor_lang",
                lang_list : "$editor_lang_list",
                mode : "specific_textareas",
                browsers : "msie,safari,gecko",
                width : $editor_width,
                height : $editor_height,
                document_base_url: "$jspath/",
                relative_urls : false,
                remove_script_host : false,
                save_callback : "MamboSave",
                inline_styles : false,
                content_css : "$content_css",
                plugin_preview_pageurl : "../../plugins/preview/preview.php",
                plugin_preview_width : "600",
                plugin_preview_height : "400",
                table_color_fields : $editor_table_colour,
                invalid_elements: "applet,object,$script_invalid,$iframe_invalid",
                debug : false,
                button_tile_map : true,
                force_br_newlines : $br_newlines,
                force_p_newlines : $p_newlines,
                directionality : "$editor_direction",
                paste_use_dialog : true,
                advimage_constrain_proportions : true,
                theme_advanced_layout_manager : "RowLayout",
                theme_advanced_containers : "top1,top2,top3,top4,mceEditor,mceElementpath",
                theme_advanced_containers_default_class : "mceToolbar",
                theme_advanced_containers_default_align : "center",
                theme_advanced_container_top1_align : "left",
                theme_advanced_container_top2_align : "left",
                theme_advanced_container_top3_align : "left",
                theme_advanced_container_top4_align : "left",
                theme_advanced_container_top1 : "$editor_layout_row1",
                theme_advanced_container_top2 : "$editor_layout_row2",
                theme_advanced_container_top3 : "$editor_layout_row3",
                theme_advanced_container_top4 : "$editor_layout_row4",
                theme_advanced_container_top1_class : "mceToolbarTop",
                theme_advanced_container_top2_class : "mceToolbarTop",
                theme_advanced_container_top3_class : "mceToolbarTop",
                theme_advanced_container_top4_class : "mceToolbarTop",
                theme_advanced_container_mceElementpath_class : "mcePathBottom",
                plugins : "$plugins,preview,advimage,paste,fullscreen,directionality,mambo,contextmenu,$editor_extra_plugins",
                extended_valid_elements : ""
                +"a[name|href|target|title|onclick],"
                +"img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|"
                +"onmouseout|name|style|usemap|ismap|qua|men|bgc|sca|wmo|pla|scr|loo],"
                +"span[class|align|style|id],"
                +"$table_elm,"
                +"$font_elm,"
                +"$jscript_elm,"
                +"$iframe_elm,"
                +"$editor_xtd_elms"
        });
        function MamboSave(editor_id, html, body)
        {
            var convert_urls = $editor_convert_urls;
            if(convert_urls){
            base_url = tinyMCE.settings['document_base_url'];
                    html = tinyMCE.regexpReplace(html, 'href\s*=\s*"?'+base_url+'', 'href="', 'gi');
                    html = tinyMCE.regexpReplace(html, 'src\s*=\s*"?'+base_url+'', 'src="', 'gi');
                    html = tinyMCE.regexpReplace(html, 'value\s*=\s*"?'+base_url+'', 'value="', 'gi');
            }
            return html;
        }
</script>
EOD;
}
/**
* TinyMCE WYSIWYG Editor - copy editor contents to form field
* @param string The name of the editor area
* @param string The name of the form field
*/
function botmosceEditorGetContents( $editorArea, $hiddenField ) {
        global $jspath;

        return <<<EOD
        tinyMCE.triggerSave();
EOD;
}
/**
* mosce WYSIWYG Editor - display the editor
* @param string The name of the editor area
* @param string The content of the field
* @param string The name of the form field
* @param string The width of the editor area
* @param string The height of the editor area
* @param int The number of columns for the editor area
* @param int The number of rows for the editor area
*/
function botmosceEditorEditorArea( $name, $content, $hiddenField, $width, $height, $col, $row ) {
        global $jspath, $_MAMBOTS;

        $results = $_MAMBOTS->trigger( 'onCustomEditorButton' );
        $buttons = array();
        foreach ($results as $result) {
               $buttons[] = '<img src="'.$jspath.'/mambots/editors-xtd/'.$result[0].'" onclick="tinyMCE.execCommand(\'mceInsertContent\',false,\''.$result[1].'\')" />';
        }
            $buttons = implode( "", $buttons );

        return <<<EOD
<textarea id="$hiddenField" name="$hiddenField" cols="$col" rows="$row" style="width:{$width}px; height:{$height}px;" mce_editable="true">$content</textarea>
<br />$buttons
EOD;
}
?>
