Пример #1
0
function fancy_design($fname)
{
    global $HTML_PATH, $FANCY_PATH, $LANGUAGE, $LANGUAGES, $counter, $original_index, $publication_date;
    // Get the contents of the file from $HTML_PATH
    $content = oneLiner("{$HTML_PATH}/{$fname}", true);
    // CSS file linking
    $content = preg_replace("|</HEAD|", '<LINK REL="stylesheet" HREF="style.css"></HEAD', $content);
    // No margins around
    $content = preg_replace("/<BODY/", '<BODY TOPMARGIN="0" LEFTMARGIN="0"', $content);
    // HR dropout
    $content = preg_replace("/<HR\\s+ALIGN=\"LEFT\"\\s+WIDTH=\"100%\">/", '', $content);
    // Whole page table and backgrounds
    $wpbegin = '<TABLE BORDER="0" WIDTH="100%" HEIGHT="100%" CELLSPACING="0" CELLPADDING="0"><TR><TD COLSPAN="3">';
    $bnavt = '<TABLE BGCOLOR="#CCCCFF" BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">';
    $lnavt = '<TR BGCOLOR="#333366"><TD><IMG SRC="spacer.gif" BORDER="0" WIDTH="1" HEIGHT="1"><BR></TD></TR>';
    $space = '<IMG SRC="spacer.gif" WIDTH="10" HEIGHT="1">';
    // Navheader backgound
    $content = preg_replace("/<DIV\\s+CLASS=\"NAVHEADER\"\\s*><TABLE(.*)CELLPADDING=\"0\"(.*)<\\/TABLE\\s*><\\/DIV\\s*>/Us", $wpbegin . '<DIV CLASS="NAVHEADER">' . $bnavt . '<TR><TD><TABLE\\1CELLPADDING="3"\\2</TABLE></TD></TR>' . $lnavt . '</TABLE></DIV></TD></TR><TR><TD>' . $space . '</TD><TD HEIGHT="100%" VALIGN="TOP" WIDTH="100%"><BR>', $content);
    // Navfooter backgound
    $content = preg_replace("/<DIV\\s+CLASS=\"NAVFOOTER\"\\s*><TABLE(.*)CELLPADDING=\"0\"(.*)<\\/TABLE\\s*><\\/DIV\\s*>/Us", '<BR></TD><TD>' . $space . '</TD></TR><TR><TD COLSPAN="3"><DIV CLASS="NAVFOOTER">' . $bnavt . $lnavt . '<TR><TD><TABLE\\1CELLPADDING="3"\\2</TABLE></TD></TR></TABLE></DIV></TD></TR></TABLE>', $content);
    // Fix copyright page fault...
    if ($fname == "copyright.html") {
        $content = preg_replace("/&#38;copy;/", "&copy;", $content);
        $content = preg_replace("/<A\\s+HREF=\"{$original_index}#(authors|translators)\"/U", "<A HREF=\"fancy-index.html\"", $content);
        $content = preg_replace("|(</TH\\s*></TR\\s*>)|", "\\1<TR><TH COLSPAN=\"3\" ALIGN=\"center\">&nbsp;</TH></TR>", $content);
        $content = preg_replace("|(&nbsp;</TD\\s*></TR\\s*>)|", "\\1<TR><TD COLSPAN=\"3\" ALIGN=\"center\">&nbsp;</TD></TR>", $content);
    } elseif ($fname == "{$original_index}") {
        // Find out manual generation date
        if (preg_match('|<P\\s+CLASS="pubdate"\\s*>([\\d-]+)<BR></P\\s*>|U', $content, $match)) {
            $publication_date = $match[1];
        } else {
            $publication_date = 'n/a';
        }
        // Modify the index file to meet our needs
        preg_match('|CLASS=\\"title\\"\\s*><A\\s+NAME=\\"manual\\"\\s*>(.*)</A\\s*>(.*)</H1|U', $content, $match);
        $indexchange = '<TABLE BORDER="0" WIDTH="100%" HEIGHT="100%" CELLSPACING="0" CELLPADDING="0"><TR><TD COLSPAN="3"><DIV CLASS="NAVHEADER"><TABLE BGCOLOR="#CCCCFF" BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%"><TR><TD><TABLE
        WIDTH="100%" BORDER="0" CELLPADDING="3" CELLSPACING="0"><TR><TH COLSPAN="3">' . $match[2] . '</TH></TR><TR><TD COLSPAN="3" ALIGN="center">&nbsp;</TD></TR></TABLE></TD></TR><TR BGCOLOR="#333366"><TD><IMG SRC="spacer.gif" BORDER="0" WIDTH="1" HEIGHT="1"><BR></TD></TR></TABLE>
        </DIV></TD></TR><TR><TD><IMG SRC="spacer.gif" WIDTH="10" HEIGHT="1"></TD><TD HEIGHT="100%" VALIGN="TOP" WIDTH="100%"><BR>';
        $content = preg_replace("/(<DIV\\s+CLASS=\"BOOK\")/", "{$indexchange}\\1", $content);
        $content = preg_replace("/(<DIV\\s+CLASS=\"author\").*<HR>/Us", "", $content);
        preg_match('|<DIV\\s+CLASS="TOC"\\s*><DL\\s*><DT\\s*><B\\s*>(.*)</B\\s*>|U', $content, $match);
        $content = preg_replace("|(CLASS=\"title\"\\s+><A\\s+NAME=\"manual\"\\s*>).*(</A\\s*>).*(</H1)|U", "\\1{$match['1']}\\2\\3", $content);
        $content = preg_replace("|<DT\\s*><B\\s*>(.*)</B\\s*></DT\\s*>|U", "", $content);
    }
    // Print out that new file to $FANCY_PATH
    $fp = fopen("{$FANCY_PATH}/{$fname}", "w");
    $content = setDocumentCharset($content, $LANGUAGES[$LANGUAGE]['mime_charset_name']);
    fputs_wrapper($fp, $content);
    fclose($fp);
    // Print out a message to see the progress
    echo "{$FANCY_PATH}/{$fname} ready...\n";
    $counter++;
}
Пример #2
0
function RunImport()
{
    $taglist = isset($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
    $objectnames = $_POST['objectname'];
    global $dbxlink;
    $log = emptyLog();
    foreach ($objectnames as $objectname) {
        // FIXME: This reads the entire directory for each object. Not very efficient.
        if ($handle = opendir('./yamls/')) {
            while (false !== ($file = readdir($handle))) {
                # puppet names the files $FQDN.yaml, not PSMN
                if ($file == $objectname . ".yaml") {
                    # SPYC is not happy with the puppet header. Hence read it as string, strip the header and feed it to SPYC
                    $file_contents = file_get_contents("./yamls/{$file}");
                    $file_contents = substr($file_contents, strpos($file_contents, "\n") + 1);
                    $yaml_file_array = Spyc::YAMLLoadString($file_contents);
                    // FIXME: Is this the correct way to narrow in on an array?
                    // At this point, $yaml_file_array contains all the data from the YAML files in a indexed array.
                    $yaml_name = $yaml_file_array['name'];
                    // switch to the 2nd part of the array
                    $yaml_file_array = $yaml_file_array['parameters'];
                    // getSearchResultByField ($tname, $rcolumns, $scolumn, $terms, $ocolumn = '', $exactness = 0|1|2)
                    $object = getSearchResultByField('RackObject', array('id'), 'name', $yaml_name, '', 2);
                    if ($object) {
                        # Object exists. Do NOT modify.
                        $id = $object[0]['id'];
                        $log = mergeLogs($log, oneLiner(202, array("{$objectname} exists. No modifications!")));
                    } else {
                        // Object does not exist. Create new.
                        // Syntax: commitAddObject ($new_name, $new_label, $new_type_id, $new_asset_no, $taglist = array())
                        // Type is 4, server, by default.
                        $new_yamlobject = commitAddObject($yaml_name, '', 4, $yaml_file_array['serialnumber'], $taglist);
                        // Hardware type (i.e. ProLiant DL380 G6a), Dict Chapter ID is '11';
                        $hw_dict_key = getdict($hw = $yaml_file_array['productname'], $chapter = 11);
                        commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '2', $value = $hw_dict_key);
                        // Operating system string, Dict Chapter ID is '13'.
                        $osrelease = $yaml_file_array['operatingsystem'] . " " . $yaml_file_array['operatingsystemrelease'];
                        $os_dict_key = getdict($hw = $osrelease, $chapter = 13);
                        commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '4', $value = $os_dict_key);
                        /*
                                    // FIXME: The IDs should be looked up, and not preset.
                                    // Architecture. Attribute ID is '10000'.
                                    commitUpdateAttrValue ($object_id = $new_yamlobject, $attr_id = '10000', $value = $yaml_file_array['hardwareisa']);
                                    // Memory. Attribute ID is 17.
                                    commitUpdateAttrValue ($object_id = $new_yamlobject, $attr_id = '17', $value = (int)$yaml_file_array['memorysize']);
                                    // CPU. Attribute ID is 100001
                                    $cpu = $yaml_file_array['processorcount'] . " x " . $yaml_file_array['processor0'];
                                    commitUpdateAttrValue ($object_id = $new_yamlobject, $attr_id = '10001', $value = $cpu);
                        */
                        // OEM S/N 1. Attribute ID is '1'.
                        commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '1', $value = $yaml_file_array['serialnumber']);
                        // FQDN. Attribute ID is '3'.
                        commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '3', $value = $yaml_file_array['fqdn']);
                        // UUID. Attribute ID is '25'.
                        commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '25', $value = $yaml_file_array['uuid']);
                        // Hypervisor. Attribute ID is '26', Dict Chapter ID is '29'.
                        // Hypervisor key does not exist in standard Puppet yaml file, added by PSMN
                        if (isset($yaml_file_array['hypervisor'])) {
                            $hv_dict_key = getdict($hw = $yaml_file_array['hypervisor'], $chapter = 29);
                            commitUpdateAttrValue($object_id = $new_yamlobject, $attr_id = '26', $value = $hv_dict_key);
                        }
                        // NICS
                        // Warning! This part only work if default Configuration is modified
                        // Go to "MainPage -> Configuration -> User Interface"
                        // Modify "AutoPorts configuration": Change "4 = 1*33*kvm + 2*24*eth%u;15 = 1*446*kvm" to "15 = 1*446*kvm"
                        // Ref: http://www.freelists.org/post/racktables-users/Automatic-insertions-of-servers-in-the-db,7
                        $nics = explode(',', $yaml_file_array['interfaces'], 9);
                        $count = count($nics);
                        for ($i = 0; $i < $count; $i++) {
                            switch ($nics[$i]) {
                                case "sit0":
                                    break 1;
                                case "ib0":
                                    // infiniband
                                    if (isset($yaml_file_array['ipaddress_' . $nics[$i]])) {
                                        $ip = $yaml_file_array['ipaddress_' . $nics[$i]];
                                    }
                                    // do NOT import infiniband MAC for now
                                    //                if (isset($yaml_file_array['macaddress_' . $nics[$i]]))
                                    //                {
                                    //                  $mac = $yaml_file_array['macaddress_' . $nics[$i]];
                                    //                }
                                    // Add port to object. Type 40 is 10GBase-CX4, MAC can be NULL
                                    commitAddPort($object_id = $new_yamlobject, $nics[$i], 40, 'infiniband', $mac);
                                    // Add IP to object.
                                    if (isset($ip)) {
                                        bindIpToObject($ip, $new_yamlobject, $nics[$i], 'regular');
                                    }
                                    break 1;
                                default:
                                    if (preg_match("eth", $nics[$i]) === 0) {
                                        break 1;
                                    }
                                    # this one might be bad for non-linux OSes ?
                                    if (isset($yaml_file_array['ipaddress_' . $nics[$i]])) {
                                        $ip = $yaml_file_array['ipaddress_' . $nics[$i]];
                                    }
                                    if (isset($yaml_file_array['macaddress_' . $nics[$i]])) {
                                        $mac = $yaml_file_array['macaddress_' . $nics[$i]];
                                    }
                                    // Add port to object. Type 24 is 1000Base-T
                                    commitAddPort($object_id = $new_yamlobject, $nics[$i], 24, 'Ethernet port', "{$mac}");
                                    // Add IP to object.
                                    if (isset($ip)) {
                                        bindIpToObject($ip, $new_yamlobject, $nics[$i], 'regular');
                                    }
                                    break 1;
                            }
                            unset($ip);
                            unset($mac);
                        }
                        // Create a URL for the log message.
                        $url = makeHref(array('page' => 'object', 'tab' => 'default', 'object_id' => $new_yamlobject));
                        $loginstance = "<a href=\"{$url}\">" . $objectname . "</a>";
                        $log = mergeLogs($log, oneLiner(80, array("{$loginstance}")));
                    }
                }
            }
        }
    }
    return showSuccess($log);
}
Пример #3
0
function findDeeperLinks($filename, $toc, $index)
{
    global $HTML_PATH;
    $contents = oneLiner("{$HTML_PATH}/{$filename}");
    // Find all sublinks
    if (preg_match_all("!<DT\\s*><A\\s+HREF=\"(([\\w\\.-]+\\.)+html)(\\#[\\w\\.-]+)?\"\\s*>(.*)</A\\s*>!U", $contents, $matches, PREG_SET_ORDER)) {
        // Print out the file informations for all the links
        fputs_wrapper($toc, "\n        <ul>");
        foreach ($matches as $onematch) {
            $param["html"] = $onematch[1];
            if (!empty($onematch[3])) {
                $param["html"] .= $onematch[3];
            }
            $param["title"] = strip_tags($onematch[4]);
            mapAndIndex($param["title"], $param["html"], "          ", $toc, $index);
        }
        fputs_wrapper($toc, "        </ul>\n");
    } else {
        // Uncomment this if you want to debug the above pregs
        // Note: there are many files normally without deeper
        // TOC info, eg. language.expressions.html
        // echo "no deeper TOC info found in $filename\n";
        // return;
    }
}
function copyLotOfObjects()
{
    global $dbxlink;
    $dbrollback = 0;
    if (!$dbxlink->beginTransaction()) {
        throw new RTDatabaseError("can not start transaction");
    }
    // do we need this ?
    $log = emptyLog();
    $taglist = isset($_REQUEST['taglist']) ? $_REQUEST['taglist'] : array();
    assertUIntArg('global_type_id', TRUE);
    assertStringArg('namelist', TRUE);
    $global_type_id = $_REQUEST['global_type_id'];
    $source_object_id = $_REQUEST['object_id'];
    $source_object = spotEntity('object', $source_object_id);
    amplifyCell($source_object);
    // only call amplifyCell_object_Backend_Port if we have function linkmgmt_linkPorts from linkmgmt.php
    if (function_exists('amplifyCell_object_Backend_Port') && function_exists('linkmgmt_linkPorts')) {
        amplifyCell_object_Backend_Port($source_object);
    }
    if ($global_type_id == 0 or !strlen($_REQUEST['namelist'])) {
        // Log something reasonable with showError Here
        // We do not have names to copy our object to !
        // Pls check what makes $global_type_id == 0  an error
        $log = mergeLogs($log, oneLiner(186));
        return;
    }
    // The name extractor below was stolen from ophandlers.php:addMultiPorts()
    $names1 = explode("\n", $_REQUEST['namelist']);
    $names2 = array();
    foreach ($names1 as $line) {
        $parts = explode('\\r', $line);
        reset($parts);
        if (!strlen($parts[0])) {
            continue;
        } else {
            $names2[] = rtrim($parts[0]);
        }
    }
    foreach ($names2 as $name_or_csv) {
        $label = '';
        $asset_no = '';
        $object_name = '';
        $regexp = '/^\\"([^\\"]*)\\","([^\\"]*)\\","([^\\"]*)\\"/';
        $object_name_or_csv = htmlspecialchars_decode($name_or_csv, ENT_QUOTES);
        // error_log( "$regexp $object_name" );
        if (preg_match($regexp, $object_name_or_csv, $matches)) {
            $object_name = $matches[1];
            $label = $matches[2];
            $asset_no = $matches[3];
        } else {
            $object_name = $name_or_csv;
        }
        try {
            $object_id = commitAddObject($object_name, $label, $global_type_id, $asset_no, $taglist);
            if (!$object_id) {
                throw new RTDatabaseError("could not create {$object_name}");
            }
            $info = spotEntity('object', $object_id);
            amplifyCell($info);
            foreach ($source_object['ports'] as $source_port) {
                $update_port = 0;
                foreach ($info['ports'] as $new_port) {
                    if ($new_port['name'] == $source_port['name']) {
                        commitUpdatePort($object_id, $new_port['id'], $new_port['name'], $new_port['oif_id'], $source_port['label'], "");
                        $update_port = 1;
                    }
                }
                if ($update_port) {
                    true;
                } else {
                    commitAddPort($object_id, $source_port['name'], sprintf("%s-%s", $source_port['iif_id'], $source_port['oif_id']), $source_port['label'], "");
                }
            }
            // Copy Backendlinks only start if we ghave function linkmgmt_linkPorts from linkmgmt.php
            if (function_exists('amplifyCell_object_Backend_Port') && function_exists('linkmgmt_linkPorts')) {
                $info = spotEntity('object', $object_id);
                amplifyCell($info);
                amplifyCell_object_Backend_Port($info);
                /*	 showError( '<div align="left"><pre>\n===== Source Object ======\n\n' .
                				 		 varDumpToString ( $source_object ) .
                						'\n\n===== New Object ======\n\n' .
                						 varDumpToString ( $info )  .  '</pre></div>' );
                			*/
                $name_by_id = array();
                foreach ($info['BackendPorts'] as $new_be_port) {
                    $name_by_id[$new_be_port['name']] = $new_be_port['id'];
                }
                $linked_ports = array();
                foreach ($source_object['BackendPorts'] as $source_be_port) {
                    if ($source_be_port['object_id'] == $source_be_port['remote_object_id']) {
                        // We have a Port that has the own object as remote object we want to copy this type of Linko
                        // We have backend Links
                        $new_be_port_a = $name_by_id[$source_be_port['name']];
                        $new_be_port_b = $name_by_id[$source_be_port['remote_name']];
                        if ($new_be_port_a && $new_be_port_b && !array_key_exists($new_be_port_a, $linked_ports) && !array_key_exists($new_be_port_b, $linked_ports)) {
                            // error_log ( sprintf ('new_be_port_a %s // new_be_port_b %s // cableid %s', $new_be_port_a  , $new_be_port_b, $source_be_port['cableid'] ));
                            $ret_val = linkmgmt_linkPorts($new_be_port_a, $new_be_port_b, 'back', $source_be_port['cableid']);
                            // error_log ( sprintf (' linkmgmt_linkPorts ret val: "%s" ', $ret_val)) ;
                            if ($ret_val) {
                                throw new RTDatabaseError("could not copy Backend Links for {$object_name} because: {$ret_val}");
                            } else {
                                $linked_ports[$new_be_port_a] = True;
                                $linked_ports[$new_be_port_b] = True;
                            }
                        }
                    }
                }
            }
            // Copy attributes
            foreach (getAttrValues($source_object_id) as $record) {
                $value = $record['value'];
                switch ($record['type']) {
                    case 'uint':
                    case 'float':
                    case 'string':
                        $value = $record['value'];
                        break;
                    case 'dict':
                        $value = $record['key'];
                        break;
                    default:
                }
                if (permitted(NULL, NULL, NULL, array(array('tag' => '$attr_' . $record['id'])))) {
                    if (empty($value)) {
                        commitUpdateAttrValue($object_id, $record['id']);
                    } else {
                        commitUpdateAttrValue($object_id, $record['id'], $value);
                    }
                } else {
                    showError('Permission denied, "' . $record['name'] . '" can not be set');
                }
            }
            //$log = mergeLogs ($log, oneLiner (5, array ('<a href="' .
            //	makeHref (array ('page' => 'object', 'tab' => 'default', 'object_id' => $object_id)) .
            //	'">' . $info['dname'] . '</a>'))
            //);
            showSuccess(sprintf("Copied Object %s ; new Object: %s", $source_object['name'], formatPortLink($object_id, $info['dname'], 1, '', '')));
        } catch (RTDatabaseError $e) {
            error_log("rolling back DB");
            $dbrollback = 1;
            $dbxlink->rollBack();
            $log = mergeLogs($log, oneLiner(147, array($object_name)));
            throw new RTDatabaseError($e->getMessage() . sprintf(' (%s)', $name_or_csv));
        }
    }
    if (!$dbrollback) {
        $dbxlink->commit();
    }
    // return buildWideRedirectURL ($log);
}