Location: PHPKode > projects > Tinbox > contentadmin/build.php
<?php
require(PPATH.CONTENT_ADMIN."bpass1.php");
require(PPATH.CONTENT_ADMIN."bpass2.php");
                            
echo "<h1>Add Courseware</h1>";

//submit
if(isset($XMLfile) && $XMLfile!="")
        {

//take the XML file and temp it
        srand((double)microtime()*1000000);
        $vfile = rand();
        $tfile = TMP_FILE.$vfile;
        if(!copy($XMLfile, $tfile))
                die ("<span class=\"fail\">Error:</span> Problem with file upload");
        $XMLfile = $tfile;

//validate
        if(validXML($XMLfile))
                {
        //1st pass check/update embedded images
                //load XML data from file
                if (!($fp = fopen($XMLfile, "r")))
                        die("could not open XML input");
                $XMLdata = fread($fp, filesize($XMLfile));
                fclose($fp);

                $xml_parser = xml_parser_create();
                xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
                xml_set_element_handler($xml_parser, "tag_open_scan1", "tag_close_scan1");
                xml_set_character_data_handler($xml_parser, "cdata_scan1");
                xml_set_default_handler($xml_parser, "default_scan1");

                $tfi=0;
                $tfigs=0;
                $tgpj=0; //counts GIF/JPG/PNG over complete document
                $parsedXMLdata = "";
                if (!xml_parse($xml_parser, $XMLdata))
                        die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
                xml_parser_free($xml_parser);

                //write new XML data to file
                if (!($fp = fopen($XMLfile, "w")))
                        die("could not open XML input");
                fwrite($fp, $parsedXMLdata);
                fclose($fp);
        //finished 1st pass

        //2nd pass - Get course details
                //load XML data from file
                //if (!($fp = fopen($XMLfile, "r")))
                //        die("could not open XML input");
                //$XMLdata = fread($fp, filesize($XMLfile));
                //fclose($fp);

                //parse meta info (course details) into metadata[]
                $xml_parser = xml_parser_create();
                xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
                xml_set_element_handler($xml_parser, "tag_open_scan2", "tag_close_scan2");
                xml_set_character_data_handler($xml_parser, "cdata_scan2");

                array($metadata);
                $mod=0;
                if (!xml_parse($xml_parser, $XMLdata))
                        die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
                xml_parser_free($xml_parser);
        //finished 2nd pass

        //output meta info
                echo "<h2>".$metadata["title0"]."</h2>";
                echo "<table class=\"tabular\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"98%\">";
                echo "<tr><th>Document No.</th><th>Rev.</th><th>Area</th><th>Level</th><th>Target</th><th>Author</th><th>Description</th></tr>";
                echo "<tr><td>".$metadata["docnumber"]."</td>";
                echo "<td>".$metadata["revision"]."</td>";
                echo "<td>".$metadata["category"]."-".$metadata["subcategory"]."</td>";
                echo "<td>".$metadata["level"]."</td>";
                echo "<td>".$metadata["target"]."</td>";
                echo "<td>".$metadata["author"]."</td>";
                echo "<td>".$metadata["abstract0"]."</td>";
                echo "</tr>";
                if($metadata["copyright"] != "" | $metadata["legal"] != "")
                        {
                        echo "<tr><td colspan=9>";
                        if($metadata["copyright"] != "")
                                echo "<b>&copy;Copyright </b>".$metadata["copyright"]."<br>";
                        if($metadata["legal"] != "")
                                echo "<b>Legal Notice </b>".$metadata["legal"];
                        echo "</td></tr>";
                        }
                echo "</table>";

                echo "<table class=\"tabular\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"98%\">";
                if($mod)
                        {
                        echo "<tr><th colspan=2>Modules</th></tr>";
                        for ($i=1;$i<=$mod;$i++)
                                {
                                echo "<tr><td><a href=\"javascript:popup('editmodule.php?modidx=".$i."&vfile=".$vfile."')\">".$i." - ".$metadata["title".$i]."</a></td>";
                                echo "<td>".$metadata["abstract".$i]."</td>";
                                echo "</tr>";
                                }
                        echo "<tr><td colspan=2>";
                        echo "<form name=\"insertform\" method=\"post\" action=\"\" >";
                        echo "<input type=\"submit\" name=\"submit\" value=\"Add Courseware\" onclick=\"return confirm('Courseware will be added to course database, do you want to continue?')\" title=\"Add courseware to database\">";
                        echo "<input type=\"hidden\" name=\"idx\" value=\"".$idx."\">";
                        echo "<input type=\"hidden\" name=\"vfile\" value=\"".$vfile."\">";
                        echo "<input type=\"hidden\" name=\"menu\" value=\"Manage\">";
                        echo "<input type=\"hidden\" name=\"category\" value=\"".trim($metadata["category"])."\">";
                        echo "<input type=\"hidden\" name=\"blocked\" value=\"1\">";
                        echo "</form>";
                        echo "</td></tr>";
                        }
                else
                        echo "<tr><th colspan=8>No Modules</th></tr>";
                echo "</table>";

                echo "<a href=\"javascript:popup('XMLsource.php?vfile=".$vfile."')\">View XML source</a>";
                }

        }

echo "<h2>Upload XML File</h2>";

echo "<form enctype=\"multipart/form-data\" action=\"\" method=\"post\">";
echo "<table class=\"tabular\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" width=\"98%\">";
echo "<tr><th width=\"10%\">Filename: </th><td> <input name=\"XMLfile\" type=\"file\"> <input type=\"submit\" value=\"Upload\" name=\"submit\">";
echo "<input type=\"hidden\" name=\"idx\" value=\"".$idx."\"><input type=\"hidden\" name=\"menu\" value=\"".$menu."\"><input type=\"hidden\" name=\"category\" value=\"".$category."\"></td></tr>";
echo "</table></form>";


function validXML($XMLfile)
        {
        $cmd="xmllint --noout --dtdvalid ".DTD." ".escapeshellarg($XMLfile)." 2>&1";
        exec($cmd, $output, $return_code);
        if(return_code)
                if(sizeof($output))
                        {
                        echo "<h1>Load Courseware Result</h1>";
                        echo "<span class=\"fail\">Failed :</span> XML is not valid <br><br>";
                        foreach ($output as $error)
                                echo ereg_replace($XMLfile,"<b>Line</b>",htmlentities($error))."<br>";
                        echo "<hr>";
                        return 0;
                        }
        return 1;
        }
?>
Return current item: Tinbox