<?php
/*
 #CMS - CMS Made Simple
 #(c)2004 by Simon Brown (simon@uptoeleven.ws)
 #This project's homepage is: http://cmsmadesimple.sf.net
 #
 #This program is free software; you can redistribute it and/or modify
 #it under the terms of the GNU General Public License as published by
 #the Free Software Foundation; either version 2 of the License, or
 #(at your option) any later version.
 #
 #This program is distributed in the hope that it will be useful,
 #but WITHOUT ANY WARRANTY; without even the implied warranty of
 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 #GNU General Public License for more details.
 #You should have received a copy of the GNU General Public License
 #along with this program; if not, write to the Free Software
 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
 #$Id$
 #
 */

class TinyMCE extends CMSModule {
	var $plugins_always_enabled;
	var $plugins_default_enabled="paste,advimage,advlink,contextmenu,inlinepopups";
	var $plugins_ignore="cmsmslink,simplepaste";
  var $templateid;

	function GetName() {
		return 'TinyMCE';
	}

	function GetFriendlyName() {
		return $this->Lang("friendlyname");
	}

	function HasAdmin() {
		return true;
	}

	function GetVersion()	{
		return '2.5.5';
	}

	function IsWYSIWYG() {
		return true;
	}

  function HandlesEvents () {
    return true;
  }

  function HasCapability($capability, $params=array()) {
    if ($capability=="wysiwyg") return true;
    return false;
  }

	function VisibleToAdminUser() {
		return $this->CheckPermission('Modify Site Preferences');
	}

	function ResetSettings($whatsettings="all") {
		if ($whatsettings=="all" || $whatsettings=="basic") {
			$this->SetPreference('skin',"default");
			$this->SetPreference('source_formatting',"1");
		
			$this->SetPreference('editor_width',"800");
			$this->SetPreference('editor_width_auto',"1");
			$this->SetPreference('editor_width_unit',"px");
			$this->SetPreference('editor_height',"400");
			$this->SetPreference('editor_height_auto',"1");
			$this->SetPreference('editor_height_unit',"px");
			$this->SetPreference('show_path',"1");
			$this->SetPreference('striptags',"true");
			$this->SetPreference('imagebrowserstyle',"both");
			
      $this->SetPreference('allowscaling',0);
      $this->SetPreference('scalingwidth',"800");
      $this->SetPreference('scalingheight',"600");
			$this->SetPreference("filepickerstyle","both");
			$this->SetPreference('fpwidth',"700");
			$this->SetPreference('fpheight',"500");
		}

		if ($whatsettings=="all" || $whatsettings=="toolbars") {
			$this->SetPreference('toolbar1',"cut,paste,pastetext,pasteword,copy,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,styleselect,formatselect,fontselect,fontsizeselect");
			$this->SetPreference('toolbar2',"bold,italic,underline,strikethrough,advhr,separator,bullist,numlist,separator,outdent,indent,separator,undo,redo,separator,customdropdown,cmslinker,link,unlink,anchor,image,charmap,cleanup,separator,forecolor,backcolor,separator,code,fullscreen,help");
      $this->SetPreference('toolbar3',"");
			$this->SetPreference('allow_tables',0);
      $this->SetPreference('allowupload',0);
      $this->SetPreference("showtogglebutton","1");
      $this->SetPreference('advanced_toolbar1',"cut,paste,pastetext,pasteword,copy,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,styleselect,formatselect,fontselect,fontsizeselect");
			$this->SetPreference('advanced_toolbar2',"bold,italic,underline,strikethrough,advhr,separator,bullist,numlist,separator,outdent,indent,separator,undo,redo,separator,customdropdown,cmslinker,link,unlink,anchor,image,charmap,cleanup,separator,forecolor,backcolor,separator,code,fullscreen,help");
      $this->SetPreference('advanced_toolbar3',"");
			$this->SetPreference('advanced_allow_tables',0);
      $this->SetPreference('advanced_allowupload',0);
      $this->SetPreference("advanced_showtogglebutton","1");
			$this->SetPreference('front_toolbar1',"cut,paste,pastetext,pasteword,copy,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,styleselect,formatselect,fontselect,fontsizeselect");
			$this->SetPreference('front_toolbar2',"bold,italic,underline,strikethrough,advhr,separator,bullist,numlist,separator,outdent,indent,separator,undo,redo,separator,cmslinker,link,unlink,anchor,image,charmap,cleanup,separator,forecolor,backcolor,separator,code,fullscreen,help");
      $this->SetPreference('front_toolbar3',"");
			$this->SetPreference('front_allow_tables',0);
      $this->SetPreference("front_showtogglebutton","1");
		}
		if ($whatsettings=="all" || $whatsettings=="plugins") {
			$this->SetPreference("plugins",$this->plugins_default_enabled);
		}
		if ($whatsettings=="all" || $whatsettings=="advanced") {
			$this->SetPreference('newlinestyle',"p");
			$this->SetPreference('usecompression',"0");
			$this->SetPreference('entityencoding',"raw");
			$this->GetPreference('skinvariation',"");
			$this->SetPreference('bodycss',"");
			$this->SetPreference('forcedrootblock',"false");
			$this->SetPreference('customdropdown',
	   "Start expand/collapse-area|{startExpandCollapse id=\'expand1\' title=\'This is my expandable area\'}
End expand/collapse-area|{stopExpandCollapse}
---|---
Insert CMS version info|{cms_version} {cms_versionname}
---|---
Insert Smarty {literal} around selection|{literal}|{/literal}");
			$this->SetPreference('extraconfig',"");
			$this->SetPreference('forcecleanpaste',1);
			$this->SetPreference('startenabled',1);
			$this->SetPreference('loadcmslinker',1);
			$this->SetPreference('cmslinkerstyle',1);
			$this->SetPreference('usestaticconfig', 0 );

			$this->SetPreference('dropdownblockformats','p,div,h1,h2,h3,h4,h5,h6,div,blockquote,dt,dd,code,samp');
		}
	}

	function WYSIWYGTextarea($name='textarea',$columns='80',$rows='15',$encoding='',$content='',$stylesheet='',$addtext='') {
    
		if (!$this->wysiwygactive && isset($_SESSION["tiny_live_textareas"])) {
			$_SESSION["tiny_live_textareas"]="";
			unset($_SESSION["tiny_live_textareas"]);
		}

		$this->wysiwygactive=true;
		global $gCms;
		$variables = &$gCms->variables;

		if ($stylesheet != '') {
			//$_SESSION["tiny_live_templateid"]=substr($stylesheet, strpos($stylesheet,"=")+1);
      $this->templateid=substr($stylesheet, strpos($stylesheet,"=")+1);
		} else {
			$tplops=$gCms->GetTemplateOperations();
			$template=$tplops->LoadDefaultTemplate();
			//$_SESSION["tiny_live_templateid"]=$template->id;
      $this->templateid=$template->id;
		}
    
		if (!isset($_SESSION["tiny_live_textareas"])) {
			$_SESSION["tiny_live_textareas"]=$name;
		} else {
			$_SESSION["tiny_live_textareas"].=",".$name;
		}
		$result='<textarea id="'.$name.'" name="'.$name.'" cols="'.$columns.'" rows="'.($rows+5).'" '.$addtext.'>'.cms_htmlentities($content,ENT_NOQUOTES,get_encoding($encoding)).'</textarea>';

		$checked="";
	  if ($this->GetPreference("startenabled","1")=="1") {		  
			$checked='checked="checked"';			
		}
    
		//Handle togglebutton
    $showtoggle=false;
    //Having profile-decided toggle button is simply not possible presently as we cannot at this point
    //know if we're in frontend or not and CheckPermission forces admin-login
    /*if (isset($_SESSION["tiny_live_frontend"]) && $_SESSION["tiny_live_frontend"]!="") {
      $showtoggle=($this->GetPreference("front_showtogglebutton",0)==1);
    } else {
      //die();
      if ($this->CheckPermission('allowadvancedprofile')) {
        $showtoggle=($this->GetPreference("advanced_showtogglebutton",0)==1);
      } else {
        
      }
    }*/
    $showtoggle=($this->GetPreference("showtogglebutton",0)==1);
		if ($showtoggle) {
			$result.='<br/><input type="checkbox" '.$checked.' onclick="toggleEditor(\''.$name.'\');" id="check_'.$name.'" /><label for="check_'.$name.'">'.$this->Lang("togglewysiwyg").'</label>';
		}
		return $result;
	}

	function WYSIWYGPageFormSubmit() {
		return 'tinyMCE.triggerSave();';
	}


	function WYSIWYGGenerateHeader($htmlresult='', $frontend=false) {
	  global $gCms;
	  
    $languageid=$this->GetLanguageId($frontend);
    
    //Rewriting config-file every time, fixing the reversed toggle-box buf
    if ($this->GetPreference("usestaticconfig")=="1") {
      if (!$this->IsTempWritable()) {
	echo $this->ShowErrors($this->Lang("usestaticconfigwarning"));
      } else {
	if ($this->GetPreference("usestaticconfig")) {
	  $this->SaveStaticConfig($frontend,$this->templateid,$languageid);
	}
      }
    }
    
    //if ($frontend) echo "hi";
    //$basepath = $gCms->config["root_url"].'/modules/TinyMCE/tinymce/jscripts/tiny_mce/';
    $output="";
    
    if ($this->wysiwygactive) {
      $output='
		    <script type="text/javascript" src="'.$gCms->config['root_url'].'/modules/TinyMCE/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>';
      if ($this->GetPreference("usestaticconfig")!="1") {
	$front="";
	$parms='';
	if ($frontend)
	  {
	    $parms = '?';
	    $front="&amp;frontend=yes";
	  }
	else
	  {
	    $parms = get_secure_param();
	  }
	$output.='<script type="text/javascript" src="'.$gCms->config['root_url'].'/modules/TinyMCE/tinyconfig.php'.$parms.'&amp;templateid='.$this->templateid.'&amp;languageid='.$languageid.$front.'"></script>
        ';
      } else {
	
	$output.='
			  <script type="text/javascript" src="'.$gCms->config['root_url'].'/tmp/tinyconfig.js"></script>
        ';
      }
    } else {
      $output="<!-- TinyMCE Session vars empty -->";
    }

    return $output;
	}

	function MinimumCMSVersion() {
		return "1.6";
	}

	function GetHelp($lang='en_US') {
		return $this->Lang('help');
	}

	function GetAuthor() {
		return 'Simon Brown, Stefan R&ouml;llin and Morten Poulsen';
	}

	function GetAuthorEmail()	{
		return '&lt;morten@poulsen.org&gt;';
	}

	function GetChangeLog()	{
		return $this->ProcessTemplate('changelog.tpl');
	}

	function GetLanguageId($frontend=false) {
    $mylang="";
    global $gCms;
    if ($frontend) {
      $mylang=get_site_preference('frontendlang');
    } else {
		  $mylang=$gCms->userprefs["default_cms_language"];
    }
		if ($mylang=="") return "en"; //Lang setting "No default selected"
		$mylang=substr($mylang,0,2);
    
		switch ($mylang) {
			case "lt" : return "en";
			case "tw" : return strtolower($gCms->userprefs["default_cms_language"]);
			default : return $mylang;
		}
	}

	function GetThumbnailFile($file,$path,$url) {
		$image="";
		$imagepath=$this->Slashes($path."/thumb_".$file);
		$imageurl=$this->Slashes($url."/thumb_".$file);
		//echo "<pre>".$imageurl."</pre><br/>";
		if (!file_exists($imagepath)) {
			//echo $imagepath;
			$image="";//$this->GetFileIcon($file["ext"],$file["dir"]);
		} else {
			$image="<img src='".$imageurl."' alt='".$file."' title='".$file."' />";
		}
		return $image;
	}

	function Slashes($url) {
		$result=str_replace("\\","/",$url);

		return $result;
	}

	function GetCustomDropdown() {	
		if ($this->GetPreference("customdropdown")=="") return "";
//echo "hi";	
$config=$this->GetConfig();
$result="";
	$lines=explode("\n",$this->GetPreference("customdropdown"));
	if (count($lines)>0) {
		$inserts=array();
		foreach($lines as $line) {
			$parts=explode("|",$line);
			if (count($parts)==2) {
				$inserts[]=array("name"=>$parts[0],"snippet"=>$parts[1]);
			} elseif (count($parts)==3) {
        $inserts[]=array("name"=>$parts[0],"snippet1"=>$parts[1],"snippet2"=>$parts[2]);
      }
		}
		if (count($inserts)>0) {
$result.="

tinymce.create('tinymce.plugins.CustomDropDown', {
	createControl: function(n, cm) {
		switch (n) {
			case 'customdropdown': {
			  var c = cm.createMenuButton('customdropdown', {
					title : '".$this->Lang("customdropdown")."',
					image : '".$config['root_url']."/modules/TinyMCE/images/customdropdown.gif',
					icons : false
				});
				c.onRenderMenu.add(function(c, m) {				
";
     foreach($inserts as $insert) {
     	if ($insert["name"]=='---') {
     		$result.="  					m.addSeparator();
";
     	} else {
        if (count($insert)==2) {
     	    $result.="    					m.add({title : '".$insert["name"]."', onclick : function() {
	  					tinyMCE.activeEditor.execCommand('mceInsertContent', false, '".trim($insert["snippet"])."');
					}});
";
        } else { //count==3
          $result.="  	  				m.add({title : '".$insert["name"]."', onclick : function() {
              var sel=tinyMCE.activeEditor.selection.getContent();
	  					tinyMCE.activeEditor.execCommand('mceInsertContent', false, '".trim($insert["snippet1"])."'+sel+'".trim($insert["snippet2"])."');
					}});
";

        }
     	}
    }
$result.="
 				});
 				return c;
			}			
		}
		return null;
		
	}
});
// Register plugin with a short name

tinymce.PluginManager.add('customdropdown', tinymce.plugins.CustomDropDown);
";
      } 
    }		
    return $result;
	}
	
	
	function AddEntry($menu,$entry) {
	$config=$this->GetConfig();
	
	$link="";
	$result="";
   $menutext=$entry->Hierarchy()." ".$entry->MenuText();
  if (strlen($menutext)>30) {
    $menutext=htmlspecialchars(substr($menutext,0,30),ENT_QUOTES)." &#133;";
  } else {
    $menutext=htmlspecialchars($menutext,ENT_QUOTES);
  }
	$result.="
					".$menu.".add({title : '".$menutext."', onclick : function() {
						var sel=tinyMCE.activeEditor.selection.getContent();
						if (sel=='') sel='".htmlentities($entry->MenuText(),ENT_QUOTES)."';";
	
	if ($this->GetPreference("cmslinkerstyle","selflink")=="a") {
		if (($config['url_rewriting'] != 'none') 
	     /*&& $config['use_hierarchy'] == true*/) {
      if ($config['url_rewriting'] == 'mod_rewrite') {
  		  $link = $config['root_url']."/".$entry->HierarchyPath().$config['page_extension'];
	  	} else {	  		
		  	$link= $config['root_url']."/index.php/".$entry->HierarchyPath().$config['page_extension'];
		  }
	  }	else {
	    //if ($tiny->GetPreference("cmslinkerstyle","selflink")=="a") {
	    $link="index.php?".$config['query_var']."=".$entry->Alias();
	    //}
	  }
		$result.="
						tinyMCE.activeEditor.execCommand('mceInsertContent', false, '<a href=\"".$link."\">'+sel+'</a>');";
	} else {
		$result.="
						tinyMCE.activeEditor.execCommand('mceInsertContent', false, \"{cms_selflink page='".$entry->Alias()."' text='\"+sel+\"'}\");";
	}
	$result.="
					}});
";
	return $result;
}
function AddSub($menu,$entry,&$result) {
  $menutext=$entry->Hierarchy()." ".$entry->MenuText();
  if (strlen($menutext)>30) {
    $menutext=htmlspecialchars(substr($menutext,0,30),ENT_QUOTES)." &#133;";
  } else {
    $menutext=htmlspecialchars($menutext,ENT_QUOTES);
  }
	$newmenu=$menu."m";
	$result.="					var ".$newmenu." = ".$menu.".addMenu({title : '".$menutext."'});
";
	if ($entry->Type()!="sectionheader") {
	  $result.=$this->AddEntry($newmenu,$entry);
	  $result.="					".$newmenu.".addSeparator();
";
	}
	return $newmenu;
}
	
	function GetCMSLinker() {
		
  if ($this->GetPreference("loadcmslinker","0")!="1") return "";
  $result="";
  $config=$this->GetConfig();
	$result.="
		
//Creates a new plugin class and a custom listbox
tinymce.create('tinymce.plugins.CMSLinkerPlugin', {
	createControl: function(n, cm) {	
		switch (n) {
			case 'cmslinker':			
				var c = cm.createMenuButton('cmslinker', {
					title : '".$this->Lang("cmsmslinker")."',
					image : '".$config["root_url"]."/modules/TinyMCE/images/cmsmslink.gif',
					icons : false
				});

				c.onRenderMenu.add(function(c, m) {
";
global $gCms;
$content_ops=$gCms->GetContentOperations();
$content_array=$content_ops->GetAllContent();

$level=0;
$menu="m";




foreach ($content_array as $one) {
	if ($one->Active()!=1) continue;
	if ($one->FriendlyName() == 'Separator') {
		continue;
	}
		//print_r($one);
	$thislevel=substr_count($one->Hierarchy(),".");
	//echo $thislevel.":".$level;
  if ($thislevel<$level) {
  	$menu=substr($menu,($level-$thislevel));
			$level-=($level-$thislevel);
			
		}
	//if ($thislevel==$level) {
		if ($one->ChildCount()>0) {
			$menu=$this->AddSub($menu,$one,$result);
			$level++;
		} else {
			$result.=$this->AddEntry($menu,$one);
		}
}

$result.="
				});

				// Return the new menu button instance
				return c;
		}

		return null;
	}
});

// Register plugin with a short name
tinymce.PluginManager.add('cmslinker', tinymce.plugins.CMSLinkerPlugin);
		";
return $result;
	}
	
	function GenerateConfig($frontend=false, $templateid="", $languageid="") {
		$result="";
		$result.=$this->GetCustomDropDown();
		//die($result);
		$result.=$this->GetCMSLinker();
//die();
	/*	if(session_id() == "") {
			session_start();
		}*/
		/*$frontend=false;
    $this->smarty->assign("isfrontend","false");
		if (isset($_SESSION["tiny_live_frontend"]) && $_SESSION["tiny_live_frontend"]!="") {
			$frontend=true;
      $this->smarty->assign("isfrontend","true");
			$_SESSION["tiny_live_frontend"]="";
		}
*/
//print_r($_SESSION);

		$config=$this->GetConfig();
    $urlext = '?';
    if ($frontend) {
      $this->smarty->assign("isfrontend","true");
    } else {
      $urlext=get_secure_param();
      $this->smarty->assign("isfrontend","false");
    }


	//	$templateid="";
		//if (isset($_SESSION["tiny_live_templateid"])) $templateid=$_SESSION["tiny_live_templateid"];

		if ($this->GetPreference("startenabled","1")=="1") {
		  $this->smarty->assign("startupmode","exact");
		} else {
			$this->smarty->assign("startupmode","none");
		}
		  
		if( isset($_SESSION['tiny_live_textareas']) )
		  $this->smarty->assign("textareas",$_SESSION["tiny_live_textareas"]);

		$this->smarty->assign("css",$config['root_url']."/modules/TinyMCE/stylesheet.php?templateid=".$templateid."&amp;mediatype=screen&amp;bogus=".time());

		$this->smarty->assign("rooturl",$config["root_url"]);
		$this->smarty->assign("encoding",$this->GetPreference('entityencoding','raw'));

		$this->smarty->assign("skin",$this->GetPreference('skin','default'));
		$this->smarty->assign("skinvariation",$this->GetPreference('skinvariation',''));

		$plugins=$this->GetPreference('plugins',$this->plugins_default_enabled);
		if (($this->GetPreference("allow_tables","0")=="1") || ($this->GetPreference("advanced_allow_tables","0")=="1") || ($this->GetPreference("front_allow_tables","0")=="1")) {
			$plugins.=",table";
		}
		
		$this->smarty->assign("urlext",$urlext);

		$this->smarty->assign("plugins",$plugins);

    if ($this->GetPreference("forcecleanpaste","1")=="1") {
		  $this->smarty->assign("pasteautoclean","true");
		}

    
		if ($frontend) {
      $toolbar=1;    
      if ($this->GetPreference('front_toolbar1')!='') {
			  $this->smarty->assign("toolbar".$toolbar,$this->GetPreference('front_toolbar1'));
        $toolbar++;
      }
      if ($this->GetPreference('front_toolbar2')!='') {
        $this->smarty->assign("toolbar".$toolbar,$this->GetPreference('front_toolbar2'));
        $toolbar++;
      }
      if ($this->GetPreference('front_toolbar3')!='') {
        $this->smarty->assign("toolbar".$toolbar,$this->GetPreference('front_toolbar3'));
        $toolbar++;
      }			
			if ($this->GetPreference("front_allow_tables","0")=="1") {
        $this->smarty->assign("toolbar".$toolbar,"tablecontrols");
			}
		} else {
      if ($this->CheckPermission('allowadvancedprofile')) {
        $toolbar=1;
        if ($this->GetPreference('advanced_toolbar1')!='') {
          $this->smarty->assign("toolbar".$toolbar,$this->GetPreference('advanced_toolbar1'));
          $toolbar++;
        }
        if ($this->GetPreference('advanced_toolbar2')!='') {
          $this->smarty->assign("toolbar".$toolbar,$this->GetPreference('advanced_toolbar2'));
          $toolbar++;
        }
        if ($this->GetPreference('advanced_toolbar3')!='') {
          $this->smarty->assign("toolbar".$toolbar,$this->GetPreference('advanced_toolbar3'));
          $toolbar++;
        }
        if ($this->GetPreference("advanced_allow_tables","0")=="1") {
          $this->smarty->assign("toolbar".$toolbar,"tablecontrols");
        }
      } else {
        $toolbar=1;
        
        if ($this->GetPreference('toolbar1')!='') {
          $this->smarty->assign("toolbar".$toolbar,$this->GetPreference('toolbar1'));
          $toolbar++;
        }
        if ($this->GetPreference('toolbar2')!='') {
          $this->smarty->assign("toolbar".$toolbar,$this->GetPreference('toolbar2'));
          $toolbar++;
        }
        if ($this->GetPreference('toolbar3')!='') {
          $this->smarty->assign("toolbar".$toolbar,$this->GetPreference('toolbar3'));
          $toolbar++;
        }
        if ($this->GetPreference("allow_tables","0")=="1") {
          $this->smarty->assign("toolbar".$toolbar,"tablecontrols");
        }
      }
		}

		$this->smarty->assign("blockformats", $this->GetPreference("dropdownblockformats","p,div,h1,h2,h3,h4,h5,h6,div,blockquote,dt,dd,code,samp"));

		if ($this->GetPreference("relativeurls",1)==1	) {
			$this->smarty->assign("relativeurls","true");
		} else {
			$this->smarty->assign("relativeurls","false");
		}

		/*if (isset($_SESSION["tiny_live_language"])) {
			$this->smarty->assign("language",$_SESSION["tiny_live_language"]);
		} else {
			$this->smarty->assign("language","en");
		}*/
    $this->smarty->assign("language",$languageid);

		if($this->GetPreference('source_formatting',1) == 1) {
			$this->smarty->assign("sourceformatting","true");
		} else {
			$this->smarty->assign("sourceformatting","false");
		}

		if($this->GetPreference('show_path', 1) == 1) {
			$this->smarty->assign("showpath","1");
		}

		$this->smarty->assign("editorwidth","");
		if ($this->GetPreference("editor_width_auto",1)!=1) {
			$this->smarty->assign("editorwidth","'".$this->GetPreference("editor_width").$this->GetPreference("editor_width_unit")."'");
		}
		$this->smarty->assign("editorheight","");
		if ($this->GetPreference("editor_height_auto",1)!=1) {
			$this->smarty->assign("editorheight","'".$this->GetPreference("editor_height").$this->GetPreference("editor_height_unit")."'");
		}

			
		if ($this->GetPreference("newlinestyle","p")!="p") {
			$this->smarty->assign("force_br_newlines","true");
			$this->smarty->assign("force_p_newlines","false");
		} else {
			$this->smarty->assign("force_br_newlines","false");
			$this->smarty->assign("force_p_newlines","true");
		}

		if ($this->GetPreference("forcedrootblock","false")=="false") {
			$this->smarty->assign("forcedrootblock","false");
		} else {
			$this->smarty->assign("forcedrootblock","'".$this->GetPreference("forcedrootblock")."'");
		}


		$this->smarty->assign("dateformat",$this->GetPreference('dateformat',"%Y-%m-%d"));
		$this->smarty->assign("timeformat",$this->GetPreference('timeformat',"%H:%M:%S"));

		$css_styles = $this->GetPreference('css_styles');
		$css_styles =str_replace("\n","",$css_styles);
		$css_styles =str_replace("\r","",$css_styles);

		$this->smarty->assign("css_styles",$css_styles);

		$extraconfig="";
		if ($this->GetPreference("extraconfig")!="") {
			$lines=explode("\n",$this->GetPreference("extraconfig"));
			if (count($lines)>0) {
				$extraconfig="//Extra configuration\n";
				foreach($lines as $line) {
					$line=trim($line);
					//if (substr($line,-1,1)!=",") $line.=",";
          //$line=",".$line;
					$extraconfig.=",".$line."\n";
				}
			}
		}
		$this->smarty->assign("extraconfig",$extraconfig);

		$this->smarty->assign("filepickertitle",$this->Lang("filepickertitle"));
		$this->smarty->assign("fpwidth",$this->GetPreference("fpwidth","700"));
		$this->smarty->assign("fpheight",$this->GetPreference("fpheight","500"));

		$result.=$this->ProcessTemplate('tinyconfig.tpl');
		return $result;

	}

	function IsTempWritable() {
		$config=$this->GetConfig();
		$filename=$this->Slashes($config["root_path"]."/tmp/tinyconfig.js");
		//$file=@fopen($filename,"w"));
		//if (!$file) return false;
		//echo $filename;
		if (!@touch($filename)) return false;
		return true;
	}

	function SaveStaticConfig($frontend=false, $templateid="", $languageid="") {
		$config=$this->config;
		$configcontent=$this->GenerateConfig($frontend, $templateid, $languageid);
		$filename=$this->Slashes($config["root_path"]."/tmp/tinyconfig.js");
		return file_put_contents($filename,$configcontent);
	}

  function HandleFileResizing($tempname,$outname,$size_x,$size_y, $switchdimensions=true) {
		$tempname=$this->Slashes($tempname);
		$outname=$this->Slashes($outname);
		//$config=$this->cms->GetConfig();
		//$rooturl=$config["root_url"];
		
		$img_data = getimagesize($tempname);
    if (!$img_data) return false;
		$mime = $img_data['mime'];
		$image="";
		switch($mime) {
			case 'image/jpeg': $image = imagecreatefromjpeg($tempname); break;
			case 'image/gif': $image = imagecreatefromgif($tempname);	break;
			case 'image/png': $image = imagecreatefrompng($tempname);	break;
			default: return false;
		}
    $width = imagesx($image);
    $height = imagesy($image);
    $whratio=$width/$height;
    $hwratio=$height/$width;
    $newwidth="";$newheight="";
    /*
     * 		$this->Lang("forcewidth")=>1,
		$this->Lang("forceheight")=>2,
		$this->Lang("forcelargest")=>3,
		$this->Lang("forceboth")=>4,*/
    $resizing="1";
	  if ($switchdimensions) {
	  	if ($width<$height) {
	  		$resizing="2";
        $tmp=$size_x;
        $size_x=$size_y;
        $size_y=$tmp;
	  	}
	  }
    
    switch ($resizing) {
    	case "1" : {

    		  if ($width<$size_x) {
    		  	$newwidth=$width;
    		  	$newheight=$height;
    		  	break;
    		  }

    		$newwidth=$size_x;
    		$newheight=$newwidth*$hwratio;
    		break;
    	}
      case "2" : {

    		  if ($height<$size_y) {
    		  	$newwidth=$width;
    		  	$newheight=$height;
    		  	break;
    		  }

    		$newheight=$size_y;
    		$newwidth=$newheight*$whratio;
    		break;
    	}      
    }
    //echo $resizing;
    //echo round($newwidth)."x".round($newheight);die();

    $newimage = imagecreatetruecolor(round($newwidth),round($newheight));
    imagecopyresampled($newimage, $image, 0, 0, 0, 0, $newwidth, $newheight,$width, $height);
    $format="unchanged";
    if ($format=="unchanged") {
		  switch($mime) {
			  case 'image/jpeg': return imagejpeg($newimage,$outname,90);
			  case 'image/gif': return imagegif($newimage,$outname);
			  case 'image/png': return imagepng($newimage,$outname);
			  default: return false;
		  }
		} else {
			switch ($format) {
				case "jpg" : return imagejpeg($newimage,$outname,90);
				case 'gif': return imagegif($newimage,$outname);
			  case 'png': return imagepng($newimage,$outname);
			}
		}
	}

	function Slash($str,$str2="",$str3="") {
		if ($str=="") return $str2;
		if ($str2=="") return $str;
		if ($str[strlen($str)-1]!="/") {
			if ($str2[0]!="/") {
				return $str."/".$str2;
			} else {
				return $str.$str2;
			}
		} else {
			if ($str2[0]!="/") {
				return $str.$str2;
			} else {
				return $str.substr($str2,1); //trim away one of the slashes
			}
		}
		//Three strings not supported yet...
		return "Error in Slash-function. Please report";
	}
}

?>
