public function tar_create($dir, $archive_name = null)
 {
     require_once nc_core('ADMIN_FOLDER') . 'tar.inc.php';
     $additional_path = dirname(str_replace(nc_core('DOCUMENT_ROOT'), '', $dir));
     $file_name = basename($dir);
     if (!$archive_name) {
         $archive_name = $file_name . '.tgz';
     } else {
         $archive_name = rtrim($archive_name, '.tgz') . '.tgz';
     }
     $result = nc_tgz_create($archive_name, $file_name, $additional_path);
     if ($result) {
         remove_dir($dir);
     }
     return $additional_path . '/' . $archive_name;
 }
Exemple #2
0
function CascadeExportTemplate($TemplateID)
{
    // system superior object
    $nc_core = nc_Core::get_object();
    include $nc_core->DOCUMENT_ROOT . $nc_core->ADMIN_PATH . "tar.inc.php";
    $db =& $nc_core->db;
    $VersionNumber = $nc_core->get_settings("VersionNumber");
    $SystemID = $nc_core->get_settings("SystemID");
    $LastPatch = $nc_core->get_settings("LastPatch");
    $TemplateID = intval($TemplateID);
    // Блокируем таблицы
    $LockTables = "LOCK TABLES `Template` WRITE";
    $LockResult = $db->query($LockTables);
    // Экспортируем данные из таблицы Template
    $SelectTemplate = $db->get_row("SELECT * FROM `Template` WHERE `Template_ID` = '" . $TemplateID . "'", ARRAY_A);
    $File_Mode = $SelectTemplate['File_Mode'];
    $FieldsForExport = array_keys($SelectTemplate);
    $Qry = array();
    foreach ($FieldsForExport as $Field) {
        if ($Field == "Template_ID") {
            continue;
        }
        if ($Field == "Description") {
            $ptpl_desc = $SelectTemplate[$Field];
        }
        // component template export aborted!
        $Qry[] = "`" . $Field . "` = '" . addcslashes($SelectTemplate[$Field], "\\'\r\n") . "'";
    }
    $TemplatesArr = $db->get_results("SELECT * FROM `Template` WHERE `File_Path` LIKE '" . $SelectTemplate['File_Path'] . "%'", ARRAY_A);
    if (!empty($TemplatesArr)) {
        $TempText .= "<templates>";
        $Path = "";
        foreach ($TemplatesArr as $row) {
            $path = substr($row['File_Path'], 1, -1);
            $current_id = $row["Template_ID"];
            $current_desc = $row["Description"];
            $Path .= "<path>{$row["File_Path"]}</path>";
            $level = count(explode('/', $path));
            $tplChildren = implode(',', $nc_core->template->get_childs($current_id));
            $Qry = array();
            foreach ($FieldsForExport as $Field) {
                // skip ID
                if ($Field == "Template_ID") {
                    continue;
                }
                // set `Parent_Template_ID`
                if ($Field == "Parent_Template_ID") {
                    $Qry[] = "`Parent_Template_ID` = '%INSERT_ID%'";
                    $parent = $row[$Field];
                    continue;
                }
                $Qry[] = "`" . $Field . "` = '" . addcslashes($row[$Field], "\\'\r\n") . "'";
            }
            if ($current_id == $TemplateID) {
                $parent = 0;
            }
            // template str
            $TempText .= "<template id='" . $current_id . "' level='" . $level . "' parent='" . $parent . "' desc='" . $current_desc . "' children='" . $tplChildren . "'><![CDATA[INSERT INTO `Template` SET " . join(", ", $Qry) . ";]]></template>\n";
        }
        $TempText .= "</templates>";
    }
    $db->query("UNLOCK TABLES");
    list($SystemName, $SystemColor) = nc_system_name_by_id($SystemID);
    $export_id_str = "-- NetCat " . $VersionNumber . " " . $SystemName . " [" . $LastPatch . "] component file, generated " . date("Y-m-d H:i:s");
    $output = "<?xml version=\"1.0\"?>\n<data>\n    <version>{$VersionNumber}</version>\n    <export_id>{$export_id_str}</export_id>\n    <template_id>{$TemplateID}</template_id>";
    if (isset($TempIds)) {
        $output .= $TempIds;
    }
    $output .= "<sql>\n        {$TempText}</sql>\n";
    if ($File_Mode) {
        $tmp_file_name = $nc_core->TMP_FOLDER . "netcat_template_{$TemplateID}.tgz";
        $dump_file = @nc_tgz_create($tmp_file_name, substr($SelectTemplate['File_Path'], 1, -1), $nc_core->HTTP_TEMPLATE_PATH . 'template/');
        $tar_contents = @file_get_contents($tmp_file_name);
        if ($dump_file && $tar_contents) {
            $output .= "<tar>" . base64_encode($tar_contents) . "</tar>\n";
            unlink($tmp_file_name);
        }
    }
    $output .= "</data>";
    // все компоненты в utf-8
    if (!$nc_core->NC_UNICODE) {
        $ret = $nc_core->utf8->win2utf($ret);
    }
    return $output;
}
Exemple #3
0
function nc_widgetclass_export($widget_class_id)
{
    global $db, $nc_core;
    $select = "SELECT * from `Widget_Class`\n             WHERE `Widget_Class_ID` = '" . $widget_class_id . "'";
    if ($result = $db->get_results($select, ARRAY_A)) {
        $File_Mode = $result[0]['File_Mode'];
        if ($File_Mode) {
            $File_Path = $result[0]['File_Path'];
            $file_class = new nc_widget_editor($nc_core->WIDGET_TEMPLATE_FOLDER, $nc_core->db);
            $file_class->load($widget_class_id, $File_Path);
            $file_class->fill_fields();
            include $nc_core->DOCUMENT_ROOT . $nc_core->ADMIN_PATH . "tar.inc.php";
        }
        $fields = $db->get_results("SELECT `Field_Name`, `Description`, `TypeOfData_ID`, `Format`, `NotNull`, `Priority`, `DoSearch`, `DefaultState`, `Inheritance`, `Widget_Class_ID`, `TypeOfEdit_ID` from `Field`\n                                WHERE `Widget_Class_ID` = '" . $widget_class_id . "'", ARRAY_A);
        $ret = "<?xml version='1.0' encoding='utf-8'?>\n" . "<netcatml>\n" . "  <widgetclasses>\n" . "    <widgetclass>\n" . "      <ID>" . $widget_class_id . "</ID>\n" . "      <Name>" . to_uni($result[0]['Name']) . "</Name>\n" . "      <Keyword>" . $result[0]['Keyword'] . "</Keyword>\n" . "      <Description><![CDATA[" . to_uni(strip_new_line($result[0]['Description'])) . "]]></Description>\n" . "      <Category>" . to_uni($result[0]['Category']) . "</Category>\n" . "      <InDevelop>" . $result[0]['InDevelop'] . "</InDevelop>\n" . "      <Template><![CDATA[" . ($File_Mode ? '' : to_uni($result[0]['Template'])) . "]]></Template>\n" . "      <Settings><![CDATA[" . ($File_Mode ? '' : to_uni($result[0]['Settings'])) . "]]></Settings>\n" . "      <AddForm><![CDATA[" . ($File_Mode ? '' : to_uni($result[0]['AddForm'])) . "]]></AddForm>\n" . "      <EditForm><![CDATA[" . ($File_Mode ? '' : to_uni($result[0]['EditForm'])) . "]]></EditForm>\n" . "      <WidgetDisallow>" . $result[0]['WidgetDisallow'] . "</WidgetDisallow>\n" . "      <Update>" . $result[0]['Update'] . "</Update>\n" . "      <File_Mode>" . $result[0]['File_Mode'] . "</File_Mode>\n" . "    </widgetclass>\n" . "  </widgetclasses>\n" . "  <fields>\n";
        if (!empty($fields)) {
            foreach ($fields as $f) {
                $ret .= "    <field type_of_data_id='" . $f['TypeOfData_ID'] . "'>\n";
                $ret .= "      <Field_Name>" . $f['Field_Name'] . "</Field_Name>\n";
                $ret .= "      <Description><![CDATA[" . to_uni(strip_new_line($f['Description'])) . "]]></Description>\n";
                $ret .= "      <Format>" . $f['Format'] . "</Format>\n";
                $ret .= "      <NotNull>" . $f['NotNull'] . "</NotNull>\n";
                $ret .= "      <Priority>" . $f['Priority'] . "</Priority>\n";
                $ret .= "      <DoSearch>" . $f['DoSearch'] . "</DoSearch>\n";
                $ret .= "      <DefaultState>" . $f['DefaultState'] . "</DefaultState>\n";
                $ret .= "      <Inheritance>" . $f['Inheritance'] . "</Inheritance>\n";
                $ret .= "      <TypeOfEdit_ID>" . $f['TypeOfEdit_ID'] . "</TypeOfEdit_ID>\n";
                $ret .= "    </field>\n";
            }
        }
        $ret .= "  </fields>\n";
        if ($File_Mode) {
            $tmp_file_name = $nc_core->TMP_FOLDER . "netcat_widget_{$widget_class_id}.tgz";
            $dump_file = nc_tgz_create($tmp_file_name, $widget_class_id, $nc_core->HTTP_TEMPLATE_PATH . 'widget/');
            $tar_contents = file_get_contents($tmp_file_name);
            $ret .= "  <tar_data>" . base64_encode($tar_contents) . "\n  </tar_data>\n";
        }
        $ret .= "</netcatml>\n";
    }
    return $ret;
}
 protected function export_file($path, $file)
 {
     $path = rtrim($path, DIRECTORY_SEPARATOR);
     if (!file_exists($this->nc_core->DOCUMENT_ROOT . $path . DIRECTORY_SEPARATOR . $file)) {
         return false;
     }
     require_once $this->nc_core->ADMIN_FOLDER . 'tar.inc.php';
     $full_path = $this->nc_core->DOCUMENT_ROOT . $path;
     $archive_name = md5($this->type() . $this->id() . microtime());
     $tmp_file = $this->nc_core->TMP_FOLDER . $archive_name . '.tgz';
     $dump_file = nc_tgz_create($tmp_file, $file, $path);
     $tar_contents = file_get_contents($tmp_file);
     $tar_contents = base64_encode($tar_contents);
     unlink($tmp_file);
     $attr = array('type' => 'tar', 'path' => rtrim($path, '/') . '/', 'file' => $file);
     $this->make_elem('file', 'files', $attr, $tar_contents);
 }
Exemple #5
0
function mkDump($console_run = false, $dump_options = NULL)
{
    global $DOCUMENT_ROOT, $SUB_FOLDER, $DOMAIN_NAME, $HTTP_FILES_PATH, $HTTP_ROOT_PATH, $HTTP_DUMP_PATH, $TMP_FOLDER, $HTTP_IMAGES_PATH, $DUMP_FOLDER, $HTTP_TEMPLATE_PATH, $ADMIN_PATH;
    $nc_core = nc_Core::get_object();
    // Check permissions if running from web
    if (!$console_run) {
        if (!checkPermissions($HTTP_DUMP_PATH, $DOCUMENT_ROOT)) {
            return;
        }
        if (!checkPermissions($HTTP_ROOT_PATH . "tmp/", $DOCUMENT_ROOT)) {
            return;
        }
    }
    // Determine what items to backup
    // Default settings (as in 5.0.2):
    $dump_items = array('template' => true, 'files' => true, 'images' => true, 'modules' => true, 'everything' => false, 'sql' => true);
    $include_standalone_script = false;
    // Modify settings if options specified
    //
    // NOTE: modifications rely on default settings. If you're changing default settings above, you have to review this part of code too.
    if ($dump_options) {
        // Modify items based on mode selection
        $backup_mode = "data";
        if (isset($dump_options['mode'])) {
            switch ($dump_options['mode']) {
                case 'full':
                    $dump_items['everything'] = true;
                    $backup_mode = "full";
                    break;
                case 'sql':
                    $backup_mode = "sql";
                    $dump_items['template'] = false;
                    $dump_items['files'] = false;
                    $dump_items['images'] = false;
                    $dump_items['modules'] = false;
                    break;
            }
        }
        // Determine if standalone restore script should be included
        if (isset($dump_options['standalone']) && ($dump_options['standalone'] = true)) {
            $include_standalone_script = true;
        }
    }
    DeleteFilesInDirectory($TMP_FOLDER);
    $dump_file = array();
    // Pack appropriate items
    if ($dump_items['template']) {
        $dump_file[] = nc_tgz_create($TMP_FOLDER . "netcat_template.tgz", ltrim($HTTP_TEMPLATE_PATH, "/"));
    }
    if ($dump_items['files']) {
        $dump_file[] = nc_tgz_create($TMP_FOLDER . "netcat_files.tgz", ltrim($HTTP_FILES_PATH, "/"));
    }
    if ($dump_items['images']) {
        $dump_file[] = nc_tgz_create($TMP_FOLDER . "images.tgz", trim($HTTP_IMAGES_PATH, "/"));
    }
    if ($dump_items['modules']) {
        $dump_file[] = nc_tgz_create($TMP_FOLDER . "modules.tgz", ltrim($HTTP_ROOT_PATH, "/") . "modules");
    }
    if ($dump_items['everything']) {
        // "everything" mode includes all existing items altogether. Maybe, just exclude it?
        $exclude_dirs = array($nc_core->TMP_FOLDER, $nc_core->DOCUMENT_ROOT . $nc_core->SUB_FOLDER . $nc_core->HTTP_TEMPLATE_PATH, $nc_core->FILES_FOLDER, $nc_core->DOCUMENT_ROOT . $nc_core->SUB_FOLDER . $nc_core->HTTP_IMAGES_PATH, $nc_core->MODULE_FOLDER, $nc_core->DUMP_FOLDER);
        foreach ($exclude_dirs as $ex_dir) {
            //echo "Creating tag $ex_dir/.exclude_from_everything<br />\n";
            touch($ex_dir . '.exclude_from_everything');
        }
        $dump_file[] = nc_tgz_create($TMP_FOLDER . "everything.tgz", '.', '', '.exclude_from_everything');
        foreach ($exclude_dirs as $ex_dir) {
            unlink($ex_dir . '/.exclude_from_everything');
        }
    }
    // Create SQL dump
    if ($dump_items['sql']) {
        MakeBackUp();
    }
    // Add standalone script if requested
    if ($include_standalone_script) {
        // Hosts that miss tar, mysql and/or mysqldump utilities (such as Windows) needs these classes to make restore script work
        file_put_contents($TMP_FOLDER . '/restore.php', file_get_contents($DOCUMENT_ROOT . $ADMIN_PATH . 'dump/mysql.class.php') . "\n\n\n");
        file_put_contents($TMP_FOLDER . '/restore.php', file_get_contents($DOCUMENT_ROOT . $ADMIN_PATH . 'dump/PEAR5.php') . "\n?>\n\n", FILE_APPEND);
        file_put_contents($TMP_FOLDER . '/restore.php', preg_replace('/include_once\\ .PEAR5.php.\\;/', '', file_get_contents($DOCUMENT_ROOT . $ADMIN_PATH . 'dump/PEAR.php')) . "\n?>\n\n", FILE_APPEND);
        file_put_contents($TMP_FOLDER . '/restore.php', preg_replace('/require_once\\ .PEAR.php.\\;/', '', file_get_contents($DOCUMENT_ROOT . $SUB_FOLDER . $HTTP_ROOT_PATH . 'require/lib/Tar.php')) . "\n?>\n\n", FILE_APPEND);
        file_put_contents($TMP_FOLDER . '/restore.php', '<?php $dump_options = ' . var_export($dump_options, true) . '; ?>' . "\n\n\n", FILE_APPEND);
        file_put_contents($TMP_FOLDER . '/restore.php', file_get_contents($DOCUMENT_ROOT . $ADMIN_PATH . 'dump/functions.php') . "\n\n\n", FILE_APPEND);
        file_put_contents($TMP_FOLDER . '/restore.php', file_get_contents($DOCUMENT_ROOT . $ADMIN_PATH . 'dump/restore.php') . "\n\n\n", FILE_APPEND);
    }
    // Compress final archive
    $file = date("YmdHis") . "_" . $DOMAIN_NAME . "_" . $backup_mode . "_" . GetRandom(5);
    $dump_file[] = nc_tgz_create($DUMP_FOLDER . "{$file}.tgz", ltrim($HTTP_ROOT_PATH, "/") . "tmp");
    DeleteFilesInDirectory($TMP_FOLDER);
    // Check results
    $dump_success = true;
    foreach ($dump_file as $_check) {
        if (!$_check) {
            $dump_success = false;
            break;
        }
    }
    if ($dump_success) {
        if (!$console_run) {
            nc_print_status(str_replace("%FILE", "{$file}.tgz", TOOLS_DUMP_CREATED), "ok");
        } else {
            echo "Backup created: {$file}.tgz\n";
            return 0;
        }
    } else {
        if (!$console_run) {
            nc_print_status(TOOLS_DUMP_CREATION_FAILED, "error");
        } else {
            echo "Backup creation failed: {$file}.tgz\n";
            return 1;
        }
    }
}
 protected function export_step_end()
 {
     require_once $this->nc_core->ADMIN_FOLDER . 'tar.inc.php';
     $export_path = $this->backup->export_dir();
     $current_path = $this->export_dir();
     $ext = '.tgz';
     $suffix = '';
     $archive = $this->backup->file_name($this->type(), $this->id(), '', '');
     $index = 1;
     while (file_exists($export_path . $archive . $suffix . $ext)) {
         $index++;
         $suffix = '_' . $index;
     }
     $dirname = $archive . $suffix;
     $archive = $archive . $suffix . $ext;
     rename($current_path, $export_path . $dirname);
     nc_tgz_create($archive, $dirname, $this->backup->export_http_path());
     $this->result('file', $export_path . $archive);
     remove_dir($export_path . $dirname);
     $this->backup->file_rotation();
 }
Exemple #7
0
function CascadeExportClass($ClassID)
{
    // system superior object
    $nc_core = nc_Core::get_object();
    include $nc_core->DOCUMENT_ROOT . $nc_core->ADMIN_PATH . "tar.inc.php";
    $db =& $nc_core->db;
    $VersionNumber = $nc_core->get_settings("VersionNumber");
    $SystemID = $nc_core->get_settings("SystemID");
    $LastPatch = $nc_core->get_settings("LastPatch");
    $ClassID = intval($ClassID);
    // Блокируем таблицы
    $LockTables = "LOCK TABLES `Class` WRITE, `Field` WRITE, ";
    $LockTables .= "`Message" . $ClassID . "` WRITE, ";
    $LockTables .= "`Sub_Class` WRITE";
    $LockResult = $db->query($LockTables);
    // Экспортируем данные из таблицы Class
    $SelectClass = $db->get_row("SELECT * FROM `Class` WHERE `Class_ID` = '" . $ClassID . "'", ARRAY_A);
    $File_Mode = $SelectClass['File_Mode'];
    $FieldsForExport = array_keys($SelectClass);
    $Qry = array();
    foreach ($FieldsForExport as $Field) {
        if ($Field == "Class_ID") {
            continue;
        }
        // component template export aborted!
        if ($Field == "ClassTemplate" && $SelectClass[$Field] != 0) {
            return false;
        }
        $Qry[] = "`" . $Field . "` = '" . addcslashes($SelectClass[$Field], "\\'\r\n") . "'";
    }
    $TempText .= "<class><![CDATA[INSERT INTO `Class` SET " . join(", ", $Qry) . ";]]></class>\n";
    // component templates
    $ClassTemplatesArr = $db->get_results("SELECT * FROM `Class` WHERE `ClassTemplate` = '" . $ClassID . "'", ARRAY_A);
    if (!empty($ClassTemplatesArr)) {
        $TempText .= "<templates>";
        $TempIds = "<tpl_ids>";
        foreach ($ClassTemplatesArr as $row) {
            $Qry = array();
            foreach ($FieldsForExport as $Field) {
                // skip ID
                if ($Field == "Class_ID") {
                    $TempIds .= "<tpl_id>{$row[$Field]}</tpl_id>";
                    continue;
                }
                // set `ClassTemplate`
                if ($Field == "ClassTemplate") {
                    $Qry[] = "`ClassTemplate` = '%INSERT_ID%'";
                    continue;
                }
                if (!$File_Mode && strpos($row[$Field], '<![CDATA[')) {
                    $row[$Field] = str_replace('<![CDATA[', '%CDATA_START%', $row[$Field]);
                    $row[$Field] = str_replace(']]>', '%CDATA_END%', $row[$Field]);
                }
                $Qry[] = "`" . $Field . "` = '" . addcslashes($row[$Field], "\\'\r\n") . "'";
            }
            // template str
            $TempText .= "<template><![CDATA[INSERT INTO `Class` SET " . join(", ", $Qry) . ";]]></template>\n";
        }
        $TempText .= "</templates>";
        $TempIds .= "</tpl_ids>";
    }
    #Экспортируем данные из таблицы Field
    $classFields = $db->get_results("SELECT * FROM `Field` WHERE `Class_ID` = '" . $ClassID . "'");
    $db->query("SET SQL_QUOTE_SHOW_CREATE = 1");
    $temp_result = $db->get_row("SHOW CREATE TABLE `Message" . $ClassID . "`", ARRAY_N);
    $lastpos = strrpos($temp_result[1], ")");
    $CreateTable = nc_substr($temp_result[1], 0, $lastpos);
    $CreateTable = str_ireplace("CREATE TABLE `message" . $ClassID . "`", "CREATE TABLE `Message%INSERT_ID%`", $CreateTable);
    $CreateTable = str_ireplace(array("\r", "\n"), "", $CreateTable);
    $CreateTable .= ") ENGINE=MyISAM;\n";
    $TempText .= '<message_tbl>' . $CreateTable . '</message_tbl>';
    // Экспортируем данные из таблицы Field
    $classFields = $db->get_results("SELECT * FROM `Field` WHERE `Class_ID` = '" . $ClassID . "'");
    if (!empty($classFields)) {
        $insert = "<fields>";
        foreach ($classFields as $SelectField) {
            //определяем тип данных
            $alter = " ";
            switch ($SelectField->TypeOfData_ID) {
                case 1:
                    $alter .= "char(255)";
                    break;
                case 2:
                    $alter .= "int";
                    break;
                case 3:
                    $alter .= "text";
                    break;
                case 4:
                    $alter .= "int";
                    break;
                case 5:
                    $alter .= "tinyint";
                    break;
                case 6:
                    $alter .= "char(255)";
                    break;
                case 7:
                    $alter .= "double";
                    break;
                case 8:
                    $alter .= "datetime";
                    break;
                case 9:
                    $alter .= "int";
                    break;
                case 10:
                    $alter .= "text";
                    break;
            }
            if ($SelectField->DefaultState != "" && $SelectField->TypeOfData_ID != 3) {
                $alter .= " NOT NULL DEFAULT '" . $SelectField->DefaultState . "'";
            } elseif ($SelectField->NotNull) {
                $alter .= " NOT NULL";
            } else {
                $alter .= " NULL";
            }
            $insert .= "<field>INSERT INTO Field (`Class_ID`, `Field_Name`, `Description`, `TypeOfData_ID`, `Format`, `NotNull`, `Priority`, `DoSearch`, `DefaultState`, `TypeOfEdit_ID`) VALUES";
            $insert .= " (%INSERT_ID%, '" . str_replace("'", "\\'", $SelectField->Field_Name) . "', '" . str_replace("'", "\\'", $SelectField->Description) . "', " . $SelectField->TypeOfData_ID . ", '" . str_replace("'", "\\'", $SelectField->Format) . "', " . $SelectField->NotNull . ", " . $SelectField->Priority . ", " . $SelectField->DoSearch . ", '" . str_replace("'", "\\'", $SelectField->DefaultState) . "', " . $SelectField->TypeOfEdit_ID . ");</field>\n";
        }
        $insert .= "</fields>";
    }
    $TempText .= $insert;
    $db->query("UNLOCK TABLES");
    list($SystemName, $SystemColor) = nc_system_name_by_id($SystemID);
    $export_id_str = "-- NetCat " . $VersionNumber . " " . $SystemName . " [" . $LastPatch . "] component file, generated " . date("Y-m-d H:i:s");
    $output = "<?xml version=\"1.0\"?>\n<data>\n    <version>{$VersionNumber}</version>\n    <export_id>{$export_id_str}</export_id>\n    <class_id>{$ClassID}</class_id>";
    if (isset($TempIds)) {
        $output .= $TempIds;
    }
    $output .= "<sql_data>\n        {$TempText}</sql_data>\n";
    if ($File_Mode) {
        $tmp_file_name = $nc_core->TMP_FOLDER . "netcat_class_{$ClassID}.tgz";
        $dump_file = nc_tgz_create($tmp_file_name, $ClassID, $nc_core->HTTP_TEMPLATE_PATH . 'class/');
        $tar_contents = file_get_contents($tmp_file_name);
        $output .= "<tar_data>" . base64_encode($tar_contents) . "</tar_data>\n";
        unlink($tmp_file_name);
    }
    $output .= "</data>";
    // все компоненты в utf-8
    if (!$nc_core->NC_UNICODE) {
        $ret = $nc_core->utf8->win2utf($ret);
    }
    return $output;
}
Exemple #8
0
function CascadeExportClass($ClassID)
{
    // system superior object
    $nc_core = nc_Core::get_object();
    include $nc_core->DOCUMENT_ROOT . $nc_core->ADMIN_PATH . "tar.inc.php";
    $db =& $nc_core->db;
    $VersionNumber = $nc_core->get_settings("VersionNumber");
    $SystemID = $nc_core->get_settings("SystemID");
    $LastPatch = $nc_core->get_settings("LastPatch");
    $ClassID = intval($ClassID);
    // Блокируем таблицы
    $LockTables = "LOCK TABLES `Class` WRITE, `Field` WRITE, ";
    $LockTables .= "`Message" . $ClassID . "` WRITE, ";
    $LockTables .= "`Sub_Class` WRITE";
    $LockResult = $db->query($LockTables);
    // Экспортируем данные из таблицы Class
    $SelectClass = $db->get_row("SELECT * FROM `Class` WHERE `Class_ID` = '" . $ClassID . "'", ARRAY_A);
    $File_Mode = $SelectClass['File_Mode'];
    $FieldsForExport = array_keys($SelectClass);
    $Qry = array();
    foreach ($FieldsForExport as $Field) {
        // skip ID
        if ($Field == "Class_ID") {
            continue;
        }
        // component template export aborted!
        if ($Field == "ClassTemplate" && $SelectClass[$Field] != 0) {
            return false;
        }
        $Qry[] = "`" . $Field . "` = '" . addcslashes($SelectClass[$Field], "\\'\r\n") . "'";
    }
    $TempText .= "<class>INSERT INTO `Class` SET " . join(", ", $Qry) . ";</class>\n";
    // component templates
    $ClassTemplatesArr = $db->get_results("SELECT * FROM `Class` WHERE `ClassTemplate` = '" . $ClassID . "'", ARRAY_A);
    if (!empty($ClassTemplatesArr)) {
        $TempText .= "<templates>";
        $TempIds .= "<tpl_ids>";
        foreach ($ClassTemplatesArr as $row) {
            $Qry = array();
            foreach ($FieldsForExport as $Field) {
                // skip ID
                if ($Field == "Class_ID") {
                    $TempIds .= "<tpl_id>{$row[$Field]}</tpl_id>";
                    continue;
                }
                // set `ClassTemplate`
                if ($Field == "ClassTemplate") {
                    $Qry[] = "`ClassTemplate` = '%INSERT_ID%'";
                    continue;
                }
                $Qry[] = "`" . $Field . "` = '" . addcslashes($row[$Field], "\\'\r\n") . "'";
            }
            // template str
            $TempText .= htmlspecialchars("INSERT INTO `Class` SET " . join(", ", $Qry) . ";\n");
        }
        $TempText .= "</templates>";
        $TempIds .= "</tpl_ids>";
    }
    #Экспортируем данные из таблицы Field
    $classFields = $db->get_results("SELECT * FROM `Field` WHERE `Class_ID` = '" . $ClassID . "'");
    $TempText .= "CREATE TABLE Message%INSERT_ID% (" . "`Message_ID` int(11) NOT NULL auto_increment, " . "`User_ID` int(11) NOT NULL, " . "`Subdivision_ID` int(11) NOT NULL, " . "`Sub_Class_ID` int(11) NOT NULL, " . "`Priority` int(11) NOT NULL default '0', " . "`Keyword` char(255) NOT NULL, " . "`Checked` tinyint(4) NOT NULL default '1', " . "`IP` char(15) default NULL, " . "`UserAgent` char(255) default NULL, " . "`Parent_Message_ID` int(11) NOT NULL default '0', " . "`Created` datetime NOT NULL, " . "`LastUpdated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, " . "`LastUser_ID` int(11) NOT NULL, " . "`LastIP` char(15) default NULL, " . "`LastUserAgent` char(255) default NULL, ";
    $insert = "";
    if (!empty($classFields)) {
        $insert = "<fields>";
        foreach ($classFields as $SelectField) {
            $TempText .= "`" . $SelectField->Field_Name . "`";
            //определяем тип данных
            $alter = " ";
            switch ($SelectField->TypeOfData_ID) {
                case 1:
                    $alter .= "char(255)";
                    break;
                case 2:
                    $alter .= "int";
                    break;
                case 3:
                    $alter .= "text";
                    break;
                case 4:
                    $alter .= "int";
                    break;
                case 5:
                    $alter .= "tinyint";
                    break;
                case 6:
                    $alter .= "char(255)";
                    break;
                case 7:
                    $alter .= "double";
                    break;
                case 8:
                    $alter .= "datetime";
                    break;
                case 9:
                    $alter .= "int";
                    break;
                case 10:
                    $alter .= "text";
                    break;
            }
            if ($SelectField->DefaultState != "" && $SelectField->TypeOfData_ID != 3) {
                $alter .= " NOT NULL DEFAULT '" . $SelectField->DefaultState . "'";
            } elseif ($SelectField->NotNull) {
                $alter .= " NOT NULL";
            } else {
                $alter .= " NULL";
            }
            $TempText .= $alter . ", ";
            $insert .= "<field>INSERT INTO Field (`Class_ID`, `Field_Name`, `Description`, `TypeOfData_ID`, `Format`, `NotNull`, `Priority`, `DoSearch`, `DefaultState`, `TypeOfEdit_ID`) VALUES";
            $insert .= " (%INSERT_ID%, '" . str_replace("'", "\\'", $SelectField->Field_Name) . "', '" . str_replace("'", "\\'", $SelectField->Description) . "', " . $SelectField->TypeOfData_ID . ", '" . str_replace("'", "\\'", $SelectField->Format) . "', " . $SelectField->NotNull . ", " . $SelectField->Priority . ", " . $SelectField->DoSearch . ", '" . str_replace("'", "\\'", $SelectField->DefaultState) . "', " . $SelectField->TypeOfEdit_ID . ");</field>\n";
        }
        $insert = "</fields>";
    }
    $TempText .= "PRIMARY KEY (`Message_ID`), ";
    $TempText .= "UNIQUE KEY `Sub_Class_ID` (`Sub_Class_ID`,`Message_ID`,`Keyword`), ";
    $TempText .= "KEY `User_ID` (`User_ID`), ";
    $TempText .= "KEY `LastUser_ID` (`LastUser_ID`), ";
    $TempText .= "KEY `Subdivision_ID` (`Subdivision_ID`), ";
    $TempText .= "KEY `Parent_Message_ID` (`Parent_Message_ID`) ";
    $TempText .= ") TYPE=MyISAM;\n";
    $TempText .= $insert;
    $db->query("UNLOCK TABLES");
    list($SystemName, $SystemColor) = nc_system_name_by_id($SystemID);
    $export_id_str = "-- NetCat " . $VersionNumber . " " . $SystemName . " [" . $LastPatch . "] component file, generated " . date("Y-m-d H:i:s");
    $output = "<?xml version=\"1.0\"?>\n<data>\n    <version>{$VersionNumber}</version>\n    <export_id>{$export_id_str}</export_id>\n    <class_id>{$ClassID}</class_id>";
    if (isset($TempIds)) {
        $output .= $TempIds;
    }
    $output .= "<sql_data>\n        {$TempText}</sql_data>\n";
    if ($File_Mode) {
        $tmp_file_name = $nc_core->TMP_FOLDER . "netcat_widget_{$ClassID}.tgz";
        $dump_file = nc_tgz_create($tmp_file_name, $ClassID, $nc_core->HTTP_TEMPLATE_PATH . 'widget/');
        $tar_contents = file_get_contents($tmp_file_name);
        $output .= "<tar_data>" . base64_encode($tar_contents) . "</tar_data>\n";
    }
    $output .= "</data>";
    // все компоненты в utf-8
    if (!$nc_core->NC_UNICODE) {
        $ret = $nc_core->utf8->win2utf($ret);
    }
    return $ret;
}