status = $this->status (); } //*************************** // GETTER / SETTER //*************************** public function __get ($name) { switch ($name) { case "is_admin": return User::get ("is_admin"); case "may_make_backups": return User::get ("is_admin") || User::get ("may_make_backups"); case "may_edit_i18n": return User::get ("is_admin") || User::get ("may_edit_i18n"); case "may_edit_users": return User::get ("is_admin") || User::get ("may_edit_users"); default: return $this->$name; } } public function __set ($name, $value) { } private function status () { $status = array (); $status['values'] = array (); $status['values'] = isset ($_COOKIE['cms_backup_values']) ? unserialize ($_COOKIE['cms_backup_values']) : array (); $status['values']['exportfile'] = !empty ($status['values']['exportfile']) ? $status['values']['exportfile'] : preg_replace ("/.*\.([^.]*)\..*/", "\\1", $_SERVER['SERVER_NAME']) . ".spitfire.sql"; $status['messages'] = array (); $status['questions']['importBackup'] = I18n::tr ("edit", "backup/insert backup?", "insert backup?"); $status['questions']['noImportFile'] = I18n::tr ("edit", "backup/no import file", "no import file"); $status['questions']['noExportFile'] = I18n::tr ("edit", "backup/no export file", "no export file"); $ids = Setting::getIds ("system"); $status['quickbar']['disabledButtons']['save'] = true; $status['statusbar']['value'] = ""; // Elementname, User, letzte Änderung,... if (User::get ("is_admin")) { $status['statusbar']['value'] .= ""; } return $status; } public function value ($name) { return isset ($this->status['values'][$name]) ? $this->status['values'][$name] : ""; } public function save ($values) { $values = array_merge ($this->status['values'], $values); setcookie ('cms_backup_values', serialize ($values), time()+60*60*24*30); } public function exportBackup ($values) { ini_set ('url_rewriter.tags', ''); if (!empty ($values['files'])) { $filename = !empty ($values['exportfile']) ? $values['exportfile'] : "spitfire.tar"; $type = "application/x-tar"; } else { $filename = !empty ($values['exportfile']) ? $values['exportfile'] : "spitfire.sql"; $type = "text/plain"; } header ("Pragma: public"); header ("Expires: 0"); header ("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header ("Content-Type: $type"); header ("Content-Type: application/force-download"); header ("Content-Disposition: attachment; filename=$filename"); header ("Content-Description: File Transfer"); $fields = array (); foreach ($values as $name => $value) { if ($value && $name != "files" && $name != "exportfile" && $name != "history" && ($name != "data" || empty ($values['online_data'])) && ($name != "i18n" || $this->may_edit_i18n) && ($name != "user" || $this->may_edit_users)) { $fields[$name] = true; } } if (!empty ($values['files'])) { $this->tarData ($fields, !empty ($values['history'])); } else { $this->sqlData ($fields, !empty ($values['history'])); } } public function tarData ($fields, $withHistory=false) { if ($fields) { $fp = fopen (Setting::get ("config", "filePath") . "data.sql", "w"); $this->sqlData ($fields, $withHistory, $fp); fclose ($fp); } $tar = new Tar (); chdir (Setting::get ("config", "filePath")); if (empty ($fields['online_data'])) { $tar->compress (".", $withHistory); } else { $files = Backup::getFileList (true); $files[] = "data.sql"; $tar->compress ($files); } @unlink (Setting::get ("config", "filePath") . "data.sql"); } public function sqlData ($fields, $withHistory=false, $fp=null) { $this->out ($fp, "-- Spitfire SQL dump\n"); $this->out ($fp, "-- Version: ".Setting::VERSION."\n"); $this->out ($fp, "-- \n"); $this->out ($fp, "-- Creation time: ".date ("j. M Y")."\n"); $this->out ($fp, "-- Database version: ".Setting::VERSION_DB."\n"); $this->out ($fp, "-- \n"); foreach ($fields as $name => $value) { $this->out ($fp, "\n"); $this->out ($fp, "-- \n"); $this->out ($fp, "-- Data for Section `$name`\n"); $this->out ($fp, "-- \n"); $this->out ($fp, "\n"); Backup::getData ($name, $withHistory, $fp); } } private function out ($fp, $text) { $fp ? fputs ($fp, $text) : print ($text); } public function importBackup ($file=array ()) { if (!is_array ($file)) { Backup::createTables (); $file = array ('tmp_name'=>$file, 'name'=>$file, 'error'=>0); } $filesize = isset ($file['tmp_name']) ? filesize ($file['tmp_name']) : 0; $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 ); $path = Setting::get ("config", "filePath"); $messages = array ( 1 => "The uploaded file exceeds the upload_max_filesize directive of [maxUploadFilesize]kB in the php.ini file", 3 => "The uploaded file was only partially uploaded", 4 => "No file was uploaded", 6 => "Missing a temporary folder", 9 => "Can not save file [fileName] in cms file folder [fileFolder]", 10=> "Unknown file type [fileType]", ); $values = array ( 'maxUploadFilesize' => $maxUploadFilesize, 'fileFolder' => $path, 'fileName' => isset ($file['name']) ? $file['name'] : "", 'fileType' => isset ($file['type']) ? preg_replace ("/.*\./", "", $file['name']) : "", ); foreach ($messages as $key => $message) { $messages[$key] = I18n::replace (I18n::tr ("edit", "message/$message", $message), $values); } $file['error'] = (!$file || !isset($file['error']) || !$file['error'] && !$filesize) ? 4 : $file['error']; $file['error'] = ($file['error']==4 && $_SERVER['CONTENT_LENGTH'] > $maxUploadFilesize) ? 1 : $file['error']; $file['error'] = !is_writeable ($path) && !$file['error'] ? 9 : $file['error']; $file['error'] = !preg_match ("/.sql$|.tar$/", $file['name']) && !$file['error'] ? 10 : $file['error']; if ($file['error']) { $this->status['messages'][] = array ('type' => ($file['error']==4) ? "info" : "error", 'text' => $messages[$file['error']]); return false; } if (preg_match ("/.tar$/", $file['name'])) { $this->emptyFilepath ($path); $this->emptyFilepath ($path."history/"); $tar = new Tar ($file['tmp_name']); if ($err = $tar->extract ($path)) { $this->status['messages'][] = array ('type' => "error", 'text' => I18n::tr ("edit", "backup/files import abortive", "files import abortive") . "
" . $err); return false; } $this->status['messages'][] = array ('type' => "ready", 'text' => I18n::tr ("edit", "backup/files import completed", "files imported")); $file['tmp_name'] = $path."data.sql"; if (!file_exists ($file['tmp_name'])) { return true; } } if ($err = Backup::insertData ($file['tmp_name'])) { $this->status['messages'][] = array ('type' => "error", 'text' => I18n::tr ("edit", "backup/data import abortive", "data import abortive") . "
" . $err); return false; } $this->status['messages'][] = array ('type' => "ready", 'text' => I18n::tr ("edit", "backup/data import completed", "data import completed")); return true; } private function emptyFilepath ($dir) { if ($handle = opendir ($dir)) { while (false !== ($readdir = readdir ($handle))){ if ($readdir != '.' && $readdir != '..'){ $path = $dir.'/'.$readdir; if (is_file ($path)) { unlink ($path); } } } closedir ($handle); } } public function message ($text) { return I18n::tr ("edit", "backup/$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'"); } } } ?>