require_once ("../lib/setting.inc.php");
require_once ("../lib/user.inc.php");
require_once ("../lib/group.inc.php");
require_once ("../lib/history.inc.php");
require_once ("../lib/i18n.inc.php");
class Edit
{
private $elements = array ();
private $tabs = array ();
private $winid = 0;
private $values;
public function __construct ($request=null)
{
session_id () || session_start ();
$request = $request ? $request : $_REQUEST;
// Hauptfenster
if (isset ($request['name']) && isset ($request['site']) && isset ($request['group']) && isset ($_SESSION['cms']['group'][$request['site']][$request['group']])) {
$_SESSION['cms']['edit'] = Array ();
$_SESSION['cms']['edit']['count'] = 0;
$_SESSION['cms']['edit'][0]['group_number'] = $request['group'];
$_SESSION['cms']['edit'][0]['group'] = $_SESSION['cms']['group'][$request['site']][$request['group']];
$_SESSION['cms']['edit'][0]['name'] = $request['name'];
$_SESSION['cms']['edit'][0]['parent_name'] = "";
}
// Es ist ein unterfenster
if (isset ($request['name']) && isset ($request['parent_name']) && isset ($request['group_id'])) {
$this->winid = ++$_SESSION['cms']['edit']['count'];
$_SESSION['cms']['edit'][$this->winid] = $_SESSION['cms']['edit'][0];
$_SESSION['cms']['edit'][$this->winid]['name'] = $request['name'];
$_SESSION['cms']['edit'][$this->winid]['parent_name'] = $request['parent_name'];
$_SESSION['cms']['edit'][$this->winid]['group']['id'] = $request['group_id'];
}
if (isset ($request['winid'])) {
$this->winid = $request['winid'];
}
if (isset ($request['name'])) {
$_SESSION['cms']['edit'][$this->winid]['activ_name'] = $request['name'];
}
$this->values = &$_SESSION['cms']['edit'][$this->winid];
if (!$this->winid && ($names = Setting::get ("group/{$this->values['group']['name']}/{$_SESSION['cms']['style']}{$this->values['name']}", "names"))) {
$names = explode (",", $names);
} else {
$names = Array ($this->values['name']);
}
$tab = "";
foreach ($names as $name) {
$element = $this->status ($name);
$this->elements[$name] = $element;
if (!$tab || !empty ($element['settings']['openintab'])) {
$tab = $name;
}
$this->tabs[$tab][$name] = &$this->elements[$name];
$this->tabs[$tab][$tab]['settings']['elements'][$name] = $name;
if ($this->values['activ_name'] == $name) {
$this->values['activ_name'] = $tab;
}
}
}
//***************************
// GETTER / SETTER
//***************************
public function __get ($name)
{
switch ($name) {
case "is_admin":
return User::get ("is_admin");
case "may_edit":
return $this->values['group']['edit_right'] & Base::MODE_EDIT;
case "language":
$lc = I18n::lc ();
return (!$lc || $lc=="none") ? i18n::DEFAULT_LC : $lc;
case "dotId":
return $_REQUEST['id'];
case "activ_name":
return $this->values['activ_name'];
case "group_number":
return $this->values['group_number'];
case "group_id":
return $this->values['group']['id'] ? $this->values['group']['id'] : ($this->values['parent_name'] ? $this->getChild ($this->values['parent_name'], $_SESSION['cms']['edit'][0]) : 0);
case "has_parent":
return $this->values['parent_name'] != "";
case "parent_name":
return $this->values['parent_name'];
case "max_imagesize":
return Setting::get ("system", "maxImagesize") * 1024;
case "max_filesize":
return Setting::get ("system", "maxFilesize") * 1024;
case "filetypes":
return array ('gif' => "image/gif",
'jpg' => "image/jpg",
'png' => "image/png",
'swf' => "application/x-shockwave-flash",
'flv' => "application/octet-stream",
'pdf' => "application/pdf",
'mp3' => "audio/mpeg"
);
case "css_file":
return Setting::get ("system", "htmlCssFile");
case "window":
$win['name'] = implode ("_", array_keys ($this->elements));
$win['width'] = 0;
$win['height'] = 0;
$win['elements'] = count ($this->tabs[$this->values['activ_name']]);
foreach ($this->tabs[$this->values['activ_name']] as $tab) {
$size = isset ($this->size[$tab['template']]) ? $this->size[$tab['template']] : $this->size['default'];
$win['width'] = $size['width'];
$win['height'] += $size['height'];
}
return $win;
case "size":
return array ('text' => array ('width' => 320, 'height' => 32),
'textarea' => array ('width' => 550, 'height' => 280, 'autosize' => true),
'html' => array ('width' => 550, 'height' => 280, 'autosize' => true),
'date' => array ('width' => 250, 'height' => 32),
'select' => array ('width' => 320, 'height' => 32),
'selectmultiple' => array ('width' => 250, 'height' => 180, 'autosize' => true),
'link' => array ('width' => 450, 'height' => 100),
'image' => array ('width' => 360, 'height' => 190, 'autosize' => true),
'file' => array ('width' => 360, 'height' => 150),
'default' => array ('width' => 300, 'height' => 80)
);
case "count_elements":
return count ($this->elements);
case "status":
$status = array ();
foreach ($this->tabs as $tab) {
foreach ($tab as $key => $element) {
$status[$key] = $element['status'];
}
}
return $status;
case "settings":
$settings = array ();
foreach ($this->tabs as $tab) {
foreach ($tab as $key => $element) {
$settings[$key] = $element['settings'];
}
}
return $settings;
case "messages":
$messages = array ();
foreach ($this->tabs as $tab) {
foreach ($tab as $key => $element) {
if ($element['messages']) {
$messages = array_merge ($messages, $element['messages']);
}
}
}
return $messages;
default:
return $this->$name;
}
}
private function status ($name="")
{
$group = new Group (array ('id' => $this->values['group']['id'], 'link_id' => $this->values['group']['link_id'], 'parent_id' => $this->values['group']['parent_id'], 'name' => $this->values['group']['name']));
$status = array ();
// Alle einstellungen zu diesem Element
if ($this->values['parent_name']) {
foreach (Setting::get ("group/{$this->values['group']['name']}/{$_SESSION['cms']['style']}{$this->values['parent_name']}") as $key => $value) {
if (strstr ($key, "{$name}_")) {
// $key = preg_replace ("/{$name}_/", "", $key);
$status['settings'][$key] = $value;
}
}
$status['settings']['type'] = $name;
} else {
$status['settings'] = Setting::get ("group/{$this->values['group']['name']}/{$_SESSION['cms']['style']}$name");
}
$status['name'] = I18n::tr ("dot", $name);
$status['tabname'] = !empty ($status['settings']['tabname']) ? I18n::tr ("dot", $status['settings']['tabname']) : $status['name'];
$status['id'] = $group->element ($name)->id;
$status['value'] = $group->element ($name)->getValue (true);
$status['messages'] = array ();
$status['template'] = isset ($status['settings']['type']) ? $status['settings']['type'] : "";
if (!$status['template']) {
$status['messages'][] = array ('type' => "error", 'text' => I18n::tr ("edit", "undefine template"), 'tab' => $name);
} else if (!file_exists ("tpl_edit_{$status['template']}.php")) {
$status['messages'][] = array ('type' => "error", 'text' => sprintf (I18n::tr ("edit", "unknown template %s"), $status['template']), 'tab' => $name);
$status['template'] = "";
}
$status['status']['autosize'] = !empty ($this->size[$status['template']]['autosize']);
// $status['history_show'] = (isset ($_REQUEST['history_show'][$name]) && $_REQUEST['history_show'][$name]);
$status['status']['quickbar']['disabledButtons']['save'] = true;
if (!History::getFirstUndo ($group->element ($name)->id, "element")) {
$status['status']['quickbar']['disabledButtons']['undo'] = true;
}
if (!History::getFirstRedo ($group->element ($name)->id, "element")) {
$status['status']['quickbar']['disabledButtons']['redo'] = true;
}
if (!$group->element ($name)->status == Base::STATUS_OFFLINE) {
$status['status']['quickbar']['disabledButtons']['del'] = true;
}
$status['status']['statusbar']['value'] = $status['name']; // User, letzte Änderung,...
if (User::get ("is_admin")) {
$status['status']['statusbar']['value'] .= " – #{$group->group_number} ({$group->id} {$group->offline_id}) / {$status['id']}";
}
$status['status']['hide_rule'] = isset ($status['settings']['hide_rule']) ? $status['settings']['hide_rule'] : "";
$status['description'] = isset ($status['settings']['description']) ? $status['settings']['description'] : "";
$status['placeholder'] = $this->placeholderList ($name);
$status['status']['placeholder'] = !empty ($status['placeholder']);
return $status;
}
public function save ($tabId, $value)
{
$group = new Group (array ('id' => $this->group_id, 'link_id' => $this->values['group']['link_id'], 'parent_id' => $this->values['group']['parent_id'], 'name' => $this->values['group']['name'], 'url' => $this->values['group']['url']));
$type = Setting::get ("group/{$this->values['group']['name']}/{$_SESSION['cms']['style']}$tabId", "type");
if ($type == "html") {
// check if all images are childs of the element
preg_match_all ("/]+src=\"((https?:\/\/|file:\/\/|\/cms\/file\.php\?{$group->childName}=(\d*)&name=([^&]*))[^\"]*)\"/", $value, $erg, PREG_SET_ORDER);
foreach ($erg as $row) {
if (empty ($row[3])) {
$edit = new Edit (array ('name'=>"image", 'parent_name'=>$tabId, 'group_id'=>""));
$url = strtr ($row[1], array ("&"=>"&"));
if (!$edit->saveUrl ("image", $url)) {
continue;
}
} else {
$img_group = new Group (array ('id' => $row[3], 'name' => "/$tabId"));
if ($img_group->parent_id == $group->id) {
// image is a child of this group
continue;
}
$element = new Element (array ('group_id' => $row[3], 'name' => $row[4]));
$edit = new Edit (array ('name'=>"image", 'parent_name'=>$tabId, 'group_id'=>""));
$file = array ('error'=>0, 'tmp_name'=>$element->fileName ());
if (!$element->existsBinary () || !$edit->saveFile ("image", $file)) {
continue;
}
}
if ($edit->elements["image"]['value']) {
$url = preg_quote ($row[1], "/");
$value = preg_replace ("/$url/", $edit->elements["image"]['value'], $value);
}
}
}
$group->setElementValue ($tabId, $value);
$this->values['group_number'] = $group->id;
$this->values['group']['id'] = $group->offline_id;
$this->elements[$tabId] = $this->status ($tabId);
}
// Datei von URL laden
public function saveUrl ($tabId, $url, $value="")
{
$file['error'] = 0;
$file['tmp_name'] = tempnam (Setting::get ("config", "filePath"), "img");
$file['url'] = $url;
if (!$this->copyUrl ($url, $file['tmp_name'], Setting::get ("config", "proxy"))) {
$file['error'] = 11;
}
$file['error'] = !$this->saveFile ($tabId, $file, $value);
unlink ($file['tmp_name']);
return !$file['error'];
}
// Datei speichern
public function saveFile ($tabId, $file=array (), $value="")
{
$filesize = isset ($file['tmp_name']) ? filesize ($file['tmp_name']) : 0;
$maxFilesize = Setting::get ("system", $this->elements[$tabId]['template'] == 'image' ? "maxImagesize" : "maxFilesize");
$maxUploadFilesize = min (
strtr (ini_get ("upload_max_filesize"), array ('M'=>"000000", 'K'=>'000'))/1000,
strtr (ini_get ("post_max_size"), array ('M'=>"000000", 'K'=>'000'))/1000
);
$file['error'] = (!$file || !isset ($file['error']) || !$file['error'] && !$filesize) ? 4 : ($filesize > ($maxFilesize*1024) ? 2 : $file['error']);
$file['error'] = ($file['error']==4 && $_SERVER['CONTENT_LENGTH'] > $maxUploadFilesize) ? 1 : $file['error'];
$file['filename'] = "";
$file['imgsize'] = "";
if (!$file['error']) {
if ($this->elements[$tabId]['template'] == 'image') {
// entsprechen bildabmessungen den vorgaben
$imgsize = getimagesize ($file['tmp_name']);
$width = isset ($this->elements[$tabId]['settings']['image_width']) ? $this->elements[$tabId]['settings']['image_width'] : 0;
$height = isset ($this->elements[$tabId]['settings']['image_height']) ? $this->elements[$tabId]['settings']['image_height'] : 0;
if (!$imgsize) {
$file['imgsize'] = "unknown";
if ($width || $height) {
$file['error'] = 7;
}
$value .= isset ($file['type']) ? "&type={$file['type']}" : "";
} else {
if (!$this->testImagesize ($imgsize[0], $width) || !$this->testImagesize ($imgsize[1], $height)) {
$file['tmp_name'] = $this->resizeImage ($file['tmp_name'], $width, $height);
if (!$file['tmp_name']) {
$file['error'] = 8;
} else {
$file['error'] = 9;
}
} else {
$value .= "&width=$imgsize[0]&height=$imgsize[1]&type={$imgsize['mime']}";
}
}
}
if (!$file['error'] || $file['error'] == 7 || $file['error'] == 9) {
// save File
$data = file_get_contents ($file['tmp_name']);
$group = new Group (array ('id' => $this->group_id, 'link_id' => $this->values['parent_name'] ? 0 : $this->values['group']['link_id'], 'parent_id' => $this->values['parent_name'] ? 0 : $this->values['group']['parent_id'], 'name' => $this->values['group']['name'], 'edit_mode' => $this->values['group']['edit_mode'], 'url' => $this->values['group']['url']));
if ($group->setElementValue ($tabId, $value, $data)) {
$this->values['group_number'] = $group->group_number;
$this->values['group']['id'] = $group->offline_id;
if ($this->values['parent_name']) {
$this->elements[$tabId]['value'] = $group->element ($tabId)->getValue (true);
} else {
$this->elements[$tabId] = $this->status ($tabId);
$this->elements[$tabId]['messages'][] = array ('type' => "ready", 'text' => I18n::tr ("edit", "message/upload completed", "upload completed"), 'tab' => $tabId);
}
} else {
$file['error'] = 10;
}
$file['filename'] = $group->element ($tabId)->id;
}
if ($file['error'] == 9) {
unlink ($file['tmp_name']);
}
}
if ($file['error']) {
$messages = array (
1 => "The uploaded file exceeds the upload_max_filesize or post_max_size directive of [maxUploadFilesize]kB in the php.ini file",
2 => "The uploaded file is larger then the given max filesize of [maxFilesize]kB",
3 => "The uploaded file was only partially uploaded",
4 => "No file was uploaded",
6 => "Missing a temporary folder",
7 => "The image dimensions can not detect",
8 => "Can not scal image to the given presetting",
9 => "Image was scaled to the given presetting",
10 => "Can not save file [fileName] in cms file folder [fileFolder]",
11=> "Can not copy given URL [url]"
);
$values = array (
'maxFilesize' => $maxFilesize,
'maxUploadFilesize' => $maxUploadFilesize,
'fileName' => $file['filename'],
'fileFolder' => Setting::get ("config", "filePath"),
'fileType' => isset ($file['type']) ? $file['type'] : "",
'url' => isset ($file['url']) ? $file['url'] : ""
);
foreach ($messages as $key => $message) {
$messages[$key] = I18n::replace (I18n::tr ("edit", "message/$message", $message), $values);
}
$this->elements[$tabId]['messages'][] = array ('type' => ($file['error']==7 || $file['error']==9) ? "warning" : (($file['error']==4) ? "info" : "error"), 'text' => $messages[$file['error']], 'tab' => $tabId);
}
return (!$file['error'] || $file['error']==9);
}
public function undo ($tabId)
{
if ($undo = History::getFirstUndo ($this->elements[$tabId]['id'], "element")) {
History::restore ($undo['id']);
}
$this->elements[$tabId] = $this->status ($tabId);
}
public function redo ($tabId)
{
if ($redo = History::getFirstRedo ($this->elements[$tabId]['id'], "element")) {
History::restore ($redo['id']);
}
$this->elements[$tabId] = $this->status ($tabId);
}
public function del ($tabId)
{
$element = new Element (array ('id' => $this->elements[$tabId]['id']));
// print_r ($element->status);
if ($element->status == Base::STATUS_OFFLINE) {
$element->del ();
}
$this->elements[$tabId] = $this->status ($tabId);
}
public function switchMultilingual ($tabId)
{
$this->elements[$tabId]['settings']['multilingual'] = !Setting::get ("group/{$this->values['group']['name']}/{$_SESSION['cms']['style']}$tabId", "multilingual");
Setting::set ("group/{$this->values['group']['name']}/{$_SESSION['cms']['style']}$tabId", "multilingual", $this->elements[$tabId]['settings']['multilingual']);
}
public function getChild ($tabId, &$values=NULL)
{
if (!$values) {
$values = &$_SESSION['cms']['edit'][0];
}
$group = new Group (array ('id' => $values['group']['id'], 'link_id' => $values['group']['link_id'], 'parent_id' => $values['group']['parent_id'], 'name' => $values['group']['name'], 'url' => $values['group']['url']));
if (!$group->id && $group->create ()) {
$group->offline ();
$group->move (0);
}
$values['group_number'] = $group->group_number;
$values['group']['id'] = $group->offline_id;
return $group->childId ($tabId, true);
}
public function getDate ($tabId, $value)
{
if (!empty ($this->elements[$tabId]['settings']['dateformat']) && strtotime ($value)) {
if (!empty ($this->elements[$tabId]['settings']['datelocale'])) {
setlocale (LC_TIME, $this->elements[$tabId]['settings']['datelocale']);
}
$value = utf8_encode (strftime ($this->elements[$tabId]['settings']['dateformat'], strtotime ($value)));
}
return $value;
}
public function replacePlaceholder ($tabId, $value)
{
return isset ($this->values['group']['placeholders'][$tabId]) ? Element::replacePlaceholder ($value, $this->values['group']['placeholders'][$tabId]) : $value;
}
public function message ($text)
{
return I18n::tr ("edit", "edit/$text", $text);
}
// Wandelt das gegebene Array/Text in eine Form um, die direkt einer JavaScript Variablen zugewiesen werden kann
public function toJavaScript ($value)
{
if (is_array ($value)) {
$values = array ();
foreach ($value as $key => $val) {
$values[] = "'$key':".$this->toJavaScript ($val);
}
return "{".implode (",", $values)."}";
} else {
$value = Database::escape ($value);
return ($value===true) ? "true" : (($value===false) ? "false" : "'$value'");
}
}
// Gibt eine Liste der gespeicherten Platzhaltern zurück
private function placeholderList ($name, $values=0, $parent=array())
{
$text = "";
if (!$values && isset ($this->values['group']['placeholders'][$name])) {
$values = $this->values['group']['placeholders'][$name];
if ($values) {
$text .= "[IF test] ";
}
}
if (is_array ($values)) {
foreach ($values as $key => $value) {
if (is_array ($value)) {
if (is_int (key ($value))) {
$text .= "