function backup()
 {
     $settings = Plugin::getAllSettings('backup_restore');
     // All of the tablesnames that belong to Fresh CMS core.
     $tablenames = array('layout', 'page', 'page_part', 'page_tag', 'permission', 'plugin_settings', 'setting', 'snippet', 'tag', 'user', 'user_permission');
     // All fields that should be wrapped as CDATA
     $cdata_fields = array('content', 'content_html');
     // Setup XML for backup
     $xmltext = '<?xml version="1.0" encoding="UTF-8"?><freshcms></freshcms>';
     $xmlobj = new SimpleXMLExtended($xmltext);
     $xmlobj->addAttribute('version', CMS_VERSION);
     // Retrieve all database information for placement in XML backup
     global $__CMS_CONN__;
     Record::connection($__CMS_CONN__);
     $lasttable = '';
     // Generate XML file entry for each table
     foreach ($tablenames as $tablename) {
         $table = Record::query('SELECT * FROM ' . TABLE_PREFIX . $tablename);
         while ($entry = $table->fetchObject()) {
             if ($lasttable !== $tablename) {
                 $lasttable = $tablename;
                 $child = $xmlobj->addChild($tablename . 's');
             }
             $subchild = $child->addChild($tablename);
             while (list($key, $value) = each($entry)) {
                 if ($key === 'password' && $settings['pwd'] === '0') {
                     $value = '';
                 }
                 if (in_array($key, $cdata_fields, true)) {
                     $subchild->addCData($key, $value);
                 } else {
                     $subchild->addChild($key, $value);
                 }
             }
         }
     }
     // Create the XML file
     $file = $xmlobj->asXML();
     $filename = 'freshcms-backup-' . date($settings['stamp']);
     // Offer a plain XML file or a zip file for download
     if ($settings['zip'] == '1') {
         // Create a note file
         $note = "---[ NOTES for {$filename}.xml ]---\n\n";
         $note .= "This backup was created for a specific Fresh CMS version, please only restore it\n";
         $note .= "on the same version.\n\n";
         $note .= "When restoring a backup, upload the UNzipped XML file, not this zip file.\n\n";
         $note .= 'Created on ' . date('Y-m-d') . ' at ' . date('H:i:s') . ' GTM ' . date('O') . ".\n";
         $note .= 'Created with BackupRestore plugin version ' . BR_VERSION . "\n";
         $note .= 'Created for Fresh CMS version ' . CMS_VERSION . "\n\n";
         $note .= '---[ END NOTES ]---';
         use_helper('Zip');
         $zip = new Zip();
         $zip->clear();
         $zip->addFile($note, 'readme.txt');
         $zip->addFile($file, $filename . '.xml');
         $zip->download($filename . '.zip');
     } else {
         header('Pragma: public');
         header('Expires: 0');
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Cache-Control: private', false);
         header('Content-Type: text/xml; charset=UTF-8');
         header('Content-Disposition: attachment; filename=' . $filename . '.xml;');
         header('Content-Transfer-Encoding: 8bit');
         header('Content-Length: ' . strlen($file));
         echo $file;
     }
 }
 function backup()
 {
     $settings = Plugin::getAllSettings('backup_restore');
     // All of the tablesnames that belong to Wolf CMS core.
     $tablenames = array();
     if (strpos(DB_DSN, 'mysql') !== false) {
         $sql = 'show tables';
     }
     if (strpos(DB_DSN, 'sqlite') !== false) {
         $sql = 'SELECT name FROM SQLITE_MASTER WHERE type="table" ORDER BY name';
     }
     if (strpos(DB_DSN, 'pgsql') !== false) {
         $sql = "select tablename from pg_tables where schemaname='public'";
     }
     Record::logQuery($sql);
     $pdo = Record::getConnection();
     $result = $pdo->query($sql);
     while ($col = $result->fetchColumn()) {
         $tablenames[] = $col;
     }
     // All fields that should be wrapped as CDATA
     $cdata_fields = array('title', 'content', 'content_html');
     // Setup XML for backup
     $xmltext = '<?xml version="1.0" encoding="UTF-8"?><wolfcms></wolfcms>';
     $xmlobj = new SimpleXMLExtended($xmltext);
     $xmlobj->addAttribute('version', CMS_VERSION);
     // Retrieve all database information for placement in XML backup
     global $__CMS_CONN__;
     Record::connection($__CMS_CONN__);
     // Generate XML file entry for each table
     foreach ($tablenames as $tablename) {
         $table = Record::query('SELECT * FROM ' . $tablename);
         $child = $xmlobj->addChild($tablename . 's');
         while ($entry = $table->fetch(PDO::FETCH_ASSOC)) {
             $subchild = $child->addChild($tablename);
             foreach ($entry as $key => $value) {
                 if ($key == 'password' && $settings['pwd'] === '0') {
                     $value = '';
                 }
                 if (in_array($key, $cdata_fields, true)) {
                     $valueChild = $subchild->addCData($key, $value);
                 } else {
                     $valueChild = $subchild->addChild($key, str_replace('&', '&amp;', $value));
                 }
                 if ($value === null) {
                     $valueChild->addAttribute('null', true);
                 }
             }
         }
     }
     // Add XML files entries for all files in upload directory
     if ($settings['backupfiles'] == '1') {
         $dir = realpath(FILES_DIR);
         $this->_backup_directory($xmlobj->addChild('files'), $dir, $dir);
     }
     // Create the XML file
     $file = $xmlobj->asXML();
     $filename = 'wolfcms-backup-' . date($settings['stamp']) . '.' . $settings['extension'];
     // Offer a plain XML file or a zip file for download
     if ($settings['zip'] == '1') {
         // Create a note file
         $note = "---[ NOTES for {$filename} ]---\n\n";
         $note .= "This backup was created for a specific Wolf CMS version, please only restore it\n";
         $note .= "on the same version.\n\n";
         $note .= "When restoring a backup, upload the UNzipped XML backup file, not this zip file.\n\n";
         $note .= 'Created on ' . date('Y-m-d') . ' at ' . date('H:i:s') . ' GTM ' . date('O') . ".\n";
         $note .= 'Created with BackupRestore plugin version ' . BR_VERSION . "\n";
         $note .= 'Created for Wolf CMS version ' . CMS_VERSION . "\n\n";
         $note .= '---[ END NOTES ]---';
         use_helper('Zip');
         $zip = new Zip();
         $zip->clear();
         $zip->addFile($note, 'readme.txt');
         $zip->addFile($file, $filename);
         $zip->download($filename . '.zip');
     } else {
         header('Pragma: public');
         header('Expires: 0');
         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
         header('Cache-Control: private', false);
         header('Content-Type: text/xml; charset=UTF-8');
         header('Content-Disposition: attachment; filename=' . $filename . ';');
         header('Content-Transfer-Encoding: 8bit');
         header('Content-Length: ' . strlen($file));
         echo $file;
     }
 }