<?php
/**
 *
 * @category       modules
 * @package        ckeditor
 * @authors        WebsiteBaker Project, Michael Tenschert, Dietrich Roland Pehlke, Marmot, Luisehahne
 * @copyright      WebsiteBaker Org. e.V.
 * @link           http://websitebaker.org/
 * @license        http://www.gnu.org/licenses/gpl.html
 * @platform       WebsiteBaker 2.8.3
 * @requirements   PHP 5.3.6 and higher
 * @version        $Id: include.php 2 2017-07-02 15:14:29Z Manuela $
 * @filesource     $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/branches/main/modules/ckeditor/include.php $
 * @lastmodified   $Date: 2017-07-02 17:14:29 +0200 (Sun, 02 Jul 2017) $
 *
 */
/**
 *    Function called by parent, default by the wysiwyg-module
 *
 *    @param    string    The name of the textarea to watch
 *    @param    mixed    The "id" - some other modules handel this param differ
 *    @param    string    Optional the width, default "100%" of given space.
 *    @param    string    Optional the height of the editor - default is '250px'
 *
 *
 */
function show_wysiwyg_editor(
    $name,
    $id,
    $content,
    $width = '100%',
    $height = '250px',
    $toolbar = false,
    $OutputAsBuffer=false
    ) {
    global $database,$admin, $section_id;

    $modAbsPath = str_replace('\\','/',dirname(__FILE__));
    $ckeAbsPath = $modAbsPath.'/ckeditor/';
    if (isset($_SERVER['SCRIPT_FILENAME'])) {
        $realPath = str_replace('\\','/',dirname($_SERVER['SCRIPT_FILENAME']));
    } else {
        /**
         * realpath - Returns canonicalized absolute pathname
         */
        $realPath = str_replace('\\','/',realpath( './' )) ;
    }

    $selfPath = str_replace('\\','/',dirname($_SERVER['SCRIPT_NAME']));
    $documentRoot = str_replace('\\','/',realpath(substr($realPath, 0, strlen($realPath) - strlen($selfPath))));
    $tplAbsPath = str_replace('\\','/',$documentRoot.'/templates');
    $tplRelPath = str_replace($documentRoot,'',$tplAbsPath);
    $modRelPath = str_replace($documentRoot,'',$modAbsPath);
    $ckeRelPath = $modRelPath.'/ckeditor/';

    $url = parse_url(WB_URL);
    $url['path'] = (isset($url['path']) ? $url['path'] : '');
    $ModPath = str_replace($url['path'],'',$modRelPath).'/';
    $ckeModPath = str_replace($url['path'],'',$ckeRelPath);
    $tplPath = str_replace($url['path'],'',$tplRelPath).'/';
    require ( $modAbsPath.'/info.php' );
/**
 * http://docs.ckeditor.com/#!/api/CKEDITOR.config
 *
 * @param boolean
 * true: set some config.index by wb_config.js
 * false: set some config['index'] by include.php
 *
 * possible config.indexes for setting in wb_config.js
 * that were normaly set in include.php
 * format_tags, resize_dir, autoParagraph, skin, toolbar,
 * extraPlugins, removePlugins, browserContextMenuOnCtrl, entities,
 * scayt_autoStartup,
 *
 *
 */
    $bWbConfigSetting = false;

/**
 *    Create new CKeditor instance.
 *    But first - we've got to revamp this pretty old class a little bit.
 *
 */
if (!file_exists($ckeAbsPath.'CKEditor.php')) {
    throw new RuntimeException('Error loading editor file CKEditor.php, please check configuration');
}
    if ( !class_exists('CKEditor', false ))     { require ($ckeAbsPath.'CKEditor.php'); }
    if ( !class_exists('CKEditorPlus', false )) { require ($ckeAbsPath.'CKEditorPlus.php'); }   // $ckeAbsPath ends with /

    $ckeditor = new CKEditorPlus( $ckeRelPath );
/******************************************************************************************/

    $ckeditor->config['ModulVersion'] = isset($module_version) ? $module_version :  'none';
    $ckeditor->config['WBrevision'] = defined('REVISION') ? REVISION :  '';
    $ckeditor->config['WBversion'] = defined('VERSION') ? VERSION :  '';

/******************************************************************************************/

    $temp = '';
    if (isset($admin->page_id)) {
        $query = "SELECT `template` from `".TABLE_PREFIX."pages` where `page_id`='".intval($page_id)."'";
        $temp = $database->get_one( $query );
    }
    $templateFolder = ($temp == "") ? DEFAULT_TEMPLATE : $temp;
    $ckeditor->setTemplatePath($templateFolder);

  // The language to be used if config.language is empty and it's not possible to localize the editor to the user language.
//     $ckeditor->config['defaultLanguage']  = strtolower(DEFAULT_LANGUAGE);
  $ckeditor->config['defaultLanguage']  = 'en';

/**
 *    Setup the CKE language
 *
 */
$ckeditor->config['language'] = strtolower( (@LANGUAGE ?: 'en') );

/**
 * A list of semi colon separated style names (by default tags) representing
 * the style definition for each entry to be displayed in the Format combo in
 * the toolbar. Each entry must have its relative definition configuration in a
 * setting named "format_(tagName)". For example, the "p" entry has its
 * definition taken from config.format_p.
 * @type String
 * @default 'p;h1;h2;h3;h4;h5;h6;pre;address;div'
 */
if (!$bWbConfigSetting ) { $ckeditor->config['format_tags'] = 'p;div;h1;h2;h3;h4;h5;h6;pre'; }

if (!$bWbConfigSetting ) { $ckeditor->config['resize_dir'] = 'both'; }

if (!$bWbConfigSetting ) { $ckeditor->config['autoParagraph'] = false; }

/**
* The skin to load. It may be the name of the skin folder inside the editor installation path,
* or the name and the path separated by a comma.
* Available skins: moono, moonocolor, kama, bootstrapck
*
*/
if (!$bWbConfigSetting ) { $ckeditor->config['skin'] = 'moonocolor'; }

/**
 *    Additional test for wysiwyg-admin
 *
 */
$ckeditor->looking_for_wysiwyg_admin( $database );

/**
 *    Define all extra CKEditor plugins in _yourwb_/modules/ckeditor067/ckeditor/plugins here
 *
 */
if (!$bWbConfigSetting ) {
    $ckeditor->config['extraPlugins'] = 'justify,find,flash,colorbutton,colordialog,dialogadvtab,autogrow'
                                      . ',div,font,forms,iframe,indentblock,bidi,liststyle,pagebreak'
                                      . ',selectall,showblocks,smiley,templates,codemirror,syntaxhighlight'
                                      . ',wblink,wbdroplets,oembed,backup,wbabout,wbrelation,filebrowser'
                                      . ''
                                      . '';

    $ckeditor->config['removePlugins']  = 'link,wsc,save,newpage,print,shybutton,preview,wbsave,youtube'
                                        . ',sourcearea,sourcedialog,imageresponsive,image2,language,flash,alphamanager'
                                        . ',placeholder';
 }

if (!$bWbConfigSetting ) { $ckeditor->config['uiColor'] = '#BFD7EB'; }

//if ($toolbar && !$bWbConfigSetting) {$ckeditor->config['toolbar'] = $toolbar;}
if ($toolbar) {$ckeditor->config['toolbar'] = $toolbar;}

/**
 *  Whether to show the browser native context menu when the Ctrl
 *  or Meta (Mac) key is pressed on opening the context menu with the right mouse button click or the Menu key.
 *
 */
if (!$bWbConfigSetting ) { $ckeditor->config['browserContextMenuOnCtrl'] = true; }

/**
 *    Force the object to print/echo direct instead of returning the
 *    HTML source string.
 *
 */
$ckeditor->bOutputAsBuffer = $OutputAsBuffer;

if (!$bWbConfigSetting ) { $ckeditor->config['entities'] = false; }

/**
 * Sets the DOCTYPE to be used when loading the editor content as HTML.
 * <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 * <!DOCTYPE html>
 */
    $ckeditor->config['docType'] = '<!DOCTYPE html>';

    /**
     *
     * Define Marmots CKEditor plugin in ../ckeditor/plugins/backup
     * backup_on_start true or false
     * backup_save_delay in ms
     *
     */
    $ckeditor->config['backup_on_start'] = true;
    $ckeditor->config['backup_save_delay'] = 500;


/**
 *    SCAYT
 *    Spellchecker settings.
 *
 */
    $ckeditor->config['scayt_sLang'] = strtolower(LANGUAGE)."_".(LANGUAGE == "EN" ? "US" : LANGUAGE);
if (!$bWbConfigSetting ) {
    $ckeditor->config['scayt_autoStartup'] = false;
 }
/**
$ckeditor->config['filebrowserBrowseUrl'] = $ckeditor->basePath.'filemanager/browser/elfinder/elfinder.html';
 */

$connectorPath = $ckeditor->basePath.'/filemanager/connectors/php/connector.php';
$ckeditor->config['filebrowserBrowseUrl'] = $ckeditor->basePath.'filemanager/browser/default/browser.html?Connector='.$connectorPath;
$ckeditor->config['filebrowserImageBrowseUrl'] = $ckeditor->basePath.'filemanager/browser/default/browser.html?Type=Image&Connector='.$connectorPath;
$ckeditor->config['filebrowserFlashBrowseUrl'] = $ckeditor->basePath.'filemanager/browser/default/browser.html?Type=Flash&Connector='.$connectorPath;
/**
 *    The filebrowser are called in the include, because later on we can make switches, use WB_URL and so on
   $ckeditor->config['filebrowserBrowseUrl'] = $ckeditor->basePath.'/filemanager/kcfinder/browse.php?opener=ckeditor&type=files';
   $ckeditor->config['filebrowserImageBrowseUrl'] = $ckeditor->basePath.'/filemanager/kcfinder/browse.php?opener=ckeditor&type=images';
   $ckeditor->config['filebrowserFlashBrowseUrl'] = $ckeditor->basePath.'/filemanager/kcfinder/browse.php?opener=ckeditor&type=flash';
 */

$ckeditor->config['uploader'] = false; // disabled for security reasons

/**
 *    The Uploader has to be called, too.
if($ckeditor->config['uploader']==true) {
   $ckeditor->config['filebrowserUploadUrl'] = $ckeditor->basePath.'/filemanager/kcfinder/upload.php?opener=ckeditor&type=files';
   $ckeditor->config['filebrowserImageUploadUrl'] = $ckeditor->basePath.'/filemanager/kcfinder/upload.php?opener=ckeditor&type=images';
   $ckeditor->config['filebrowserFlashUploadUrl'] = $ckeditor->basePath.'/filemanager/kcfinder/upload.php?opener=ckeditor&type=flash';
}
 */

/**
 */
if($ckeditor->config['uploader']==true) {
    $uploadPath = $ckeditor->basePath.'filemanager/connectors/php/upload.php?Type=';
    $ckeditor->config['filebrowserUploadUrl'] = $uploadPath.'File';
    $ckeditor->config['filebrowserImageUploadUrl'] = $uploadPath.'Image';
    $ckeditor->config['filebrowserFlashUploadUrl'] = $uploadPath.'Flash';
}
/******************************************************************************************/
/**
 *    Looking for the styles
 *
 */
$ckeditor->resolve_path(
    'contentsCss',
    $tplPath.'wb_config/editor.css',
    $ModPath.'wb_config/editor.css'
);

/**
 *    Looking for the editor.styles at all ...
 *
 */
$ckeditor->resolve_path(
    'stylesSet',
    $tplPath.'wb_config/editor.styles.js',
    $ModPath.'wb_config/editor.styles.js',
    'wb:'
);

/**
 *    The list of templates definition files to load.
 *
 */
$ckeditor->resolve_path(
    'templates_files',
    $tplPath.'wb_config/editor.templates.js',
    $ModPath.'wb_config/editor.templates.js'
);

/**
 *    Bugfix for the template files as the ckeditor want an array instead a string ...
 *
 */
$ckeditor->config['templates_files'] = array($ckeditor->config['templates_files']);

/**
 *    Get the config file
 *
 */
$ckeditor->resolve_path(
    'customConfig',
    $tplPath.'wb_config/wb_ckconfig.js',
    $ModPath.'wb_config/wb_ckconfig.js'
);
/******************************************************************************************/

/**
 *    To avoid a double "else" inside the following condition, we set the
 *    default toolbar here to "WB_Full". Keep in mind, that the config will overwrite all
 *    settings inside the config.js or wb_config.js BUT you will have to define the toolbar inside
 *    them at all!
 *
 */

if (isset($database) && $ckeditor->wysiwyg_admin_exists ) {
    $aSection = $GLOBALS['section'];
    $data = null;
    $query = 'SELECT * from `'.TABLE_PREFIX.'mod_editor_admin` WHERE `editor`="ckeditor"';
    if (method_exists($database,'doQuery')) {
        if (($result = $database->query($query))) {
            $data = $result->fetchArray(MYSQLI_ASSOC);
        }
    } else {
        if (($result = $database->query($query))) {
            $data = $result->fetchRow(MYSQLI_ASSOC);
        }
    }
// import data into $ckeditor->config
    if ( $data ) {
        foreach ($data as $key => $value) {
            $ckeditor->config[$key] = $value;
            if(!$ckeditor->config[$key]){unset($ckeditor->config[$key]);}
        }
    }
}
if (isset($ckeditor->config['menu'])) {$ckeditor->config['toolbar'] = $ckeditor->config['menu'];}
/*
if( !$bWbConfigSetting ) {
    if ( (!$ckeditor->wysiwyg_admin_exists) || ($ckeditor->force) ) {
    }
 }
*/
$ckeditor->config['height'] = $height;
$ckeditor->config['width']  = $width;

$ckeditor->config['autoGrow_minHeight'] =  200;
$ckeditor->config['autoGrow_maxHeight'] = $height;
$ckeditor->config['autoGrow_bottomSpace'] = 50;
$ckeditor->config['autoGrow_onStartup'] = false;

$ckeditor->reverse_htmlentities($content);

$output = $ckeditor->to_HTML( $name, $content, $ckeditor->config);

if (!$OutputAsBuffer){
    echo $output;
} else {
    return $output;
}

}
