. */ /** * @defgroup mcc Maarch Capture Connector */ /** * get datas for mcc for the form * * @file * @author Laurent Giovanonni * @date $date$ * @version $Revision$ * @ingroup mcc * @brief get datas for mcc for the form */ //Error mode and function error_reporting(E_ERROR); set_error_handler(errorHandler); //Global var of the program /** * maarch capture connector path */ $_ENV['mccPath'] = ""; /** * maarch tmp path */ $_ENV['maarchTmpPath'] = ""; /** * target path for preprocess */ $_ENV['targetPath'] = ""; /** * location of the database server */ $_ENV['databaseserver'] = ""; /** * port of the database server */ $_ENV['databaseport'] = 0; /** * name of the database */ $_ENV['database'] = ""; /** * type of the database */ $_ENV['databasetype'] = ""; /** * user name of the database */ $_ENV['databaseuser'] = ""; /** * password of the database */ $_ENV['databasepwd'] = ""; /** * path to the log file */ $_ENV['log'] = ""; /** * Form template */ $_ENV['mccForm'] = ""; /** * * Array 2 XML class * Convert an array or multi-dimentional array to XML * * @author Kevin Waterson * @copyright 2009 PHPRO.ORG * */ class array2xml extends DomDocument { public $nodeName; private $xpath; private $root; private $node_name; /** * Constructor, duh * * Set up the DOM environment * * @param string $root The name of the root node * @param string $nod_name The name numeric keys are called * */ public function __construct($root='root', $node_name='node') { parent::__construct(); /*** set the encoding ***/ $this->encoding = "UTF-8"; /*** format the output ***/ $this->formatOutput = true; /*** set the node names ***/ $this->node_name = $node_name; /*** create the root element ***/ $this->root = $this->appendChild($this->createElement( $root )); $this->xpath = new DomXPath($this); } /* * creates the XML representation of the array * * @access public * @param array $arr The array to convert * @aparam string $node The name given to child nodes when recursing * */ public function createNode( $arr, $node = null) { if (is_null($node)) { $node = $this->root; } foreach($arr as $element => $value) { $element = is_numeric( $element ) ? $this->node_name : $element; $child = $this->createElement($element, (is_array($value) ? null : $value)); $node->appendChild($child); if (is_array($value)) { self::createNode($value, $child); } } } /* * Return the generated XML as a string * * @access public * @return string * */ public function __toString() { return $this->saveXML(); } /* * array2xml::query() - perform an XPath query on the XML representation of the array * @param str $query - query to perform * @return mixed */ public function query($query) { return $this->xpath->evaluate($query); } } // end of class /** * Creation of the log file */ function loginCreation() { if(!is_dir($_ENV['mccPath'].DIRECTORY_SEPARATOR."logs".DIRECTORY_SEPARATOR)) { mkdir($_ENV['mccPath'].DIRECTORY_SEPARATOR."logs".DIRECTORY_SEPARATOR,0777); } $_ENV['log'] = $_ENV['mccPath'].DIRECTORY_SEPARATOR."logs".DIRECTORY_SEPARATOR."mcc_server.log"; writeLog("Application start with : ".$_SERVER['SCRIPT_FILENAME']); } /** * Write on the log file * @param $eventInfo string text which is written in the log file */ function writeLog($eventInfo) { $logFileOpened = fopen($_ENV['log'], "a"); fwrite($logFileOpened, "[".date("d")."/".date("m")."/".date("Y")." ".date("H").":".date("i").":".date("s")."] ".$eventInfo."\r\n"); fclose($logFileOpened); } /** * Write on the log file errors * @param $eventInfo string text which is written in the log file * @param $errorLevel integer text return code of an error */ function manageError($eventInfo, $errorLevel) { $logFileOpened = fopen($_ENV['log'], "a"); fwrite($logFileOpened, "[".date("d")."/".date("m")."/".date("Y")." ".date("H").":".date("i").":".date("s")."][ERRNO]".$errorLevel."[ERRNO][ERROR]".$eventInfo."[ERROR]\r\n"); fclose($logFileOpened); echo $errorLevel." ".$eventInfo; exit($errorLevel); } /** * Managing of errors * @param $errno integer number of the error * @param $errstr string text of the error * @param $errfile string file concerned with the error * @param $errline integer line of the error * @param $errcontext string context of the error */ function errorHandler($errno, $errstr, $errfile, $errline, $errcontext) { $string = $errstr; $search1="'xml_file_loaded'"; $search2="'Undefined index'"; preg_match($search1,$string,$out); $count=count($out[0]); if($count == 1) { $find1 = true; } preg_match($search2,$string,$out); $count=count($out[0]); if($count == 1) { $find2 = true; } if(!$find1 && !$find2) { manageError("from line ".$errline." : ". $errstr, 1); $_ENV['errorLevel'] = 1; } } /** * Loading of the xml config file * @param $conf string path to the config file */ function loadConfig($conf) { //loading of the xml config file. $xmlConfig = simplexml_load_file($conf); $CONFIG =$xmlConfig -> CONFIG; $_ENV['mccPath'] = $CONFIG -> MCC_PATH; loginCreation(); writeLog("Get datas process"); writeLog("Loading xml config file : ".$conf); writeLog("mccPath : ".$_ENV['mccPath']); $_ENV['maarchTmpPath'] = $CONFIG -> MAARCH_TMP_PATH; writeLog("maarchTmpPath : ".$_ENV['maarchTmpPath']); $_ENV['targetPath'] = $CONFIG -> TARGET_PATH; writeLog("targetPath : ".$_ENV['targetPath']); $_ENV['mccForm'] = $CONFIG -> FORM; writeLog("mccForm : ".$_ENV['mccForm']); $DATABASE =$xmlConfig -> DATABASE; $_ENV['databaseserver'] = $DATABASE->LOCATION; //writeLog("databaseserver : ".$_ENV['databaseserver']); $_ENV['databaseport'] = $DATABASE->DATABASE_PORT; //writeLog("databaseport : ".$_ENV['databaseport']); $_ENV['database'] = $DATABASE ->DATABASE; //writeLog("database : ".$_ENV['database']); $_ENV['databasetype'] = $DATABASE->DATABASETYPE; //writeLog("databasetype : ".$_ENV['databasetype']); $_ENV['databaseuser'] = $DATABASE -> USER_NAME; //writeLog("databaseuser : ".$_ENV['databaseuser']); $_ENV['databasepwd'] = $DATABASE->PASSWORD; //writeLog("databasepwd : ".$_ENV['databasepwd']); $_ENV['databaseworkspace'] = $DATABASE->DATABASEWORKSPACE; //writeLog("databaseworkspace : ".$_ENV['databaseworkspace']); } //begin date_default_timezone_set('Europe/Paris'); $conf = "config".DIRECTORY_SEPARATOR."config_".$_REQUEST['configName'][0].".xml"; if(!file_exists($conf)) { die ("The read of the configuration file has been stopped by Maarch Capture Connector Server (Xml Error)"); } loadConfig($conf); $xml = new array2xml('root'); if(file_exists($_ENV['mccForm'])) { //Valid the xml form file by dtd $dom = new DOMDocument(); $dom->validateOnParse = true; $dom->load($_ENV['mccForm']); if($dom->validate() == false) { manageError("Form file not valid : ". $_ENV['mccForm'], 1); unset($dom); //$dom = NULL; exit(); } require("class_db.php"); $_ENV['db'] = new dbquery(); $_ENV['db']->connect(); $xmlForm = simplexml_load_file($_ENV['mccForm']); $formStructure = array(); $FORM = $xmlForm->FORM; $formStructure['id'] = (string) $FORM->ID; $formStructure['title'] = (string) $FORM->TITLE; $formStructure['width'] = (string) $FORM->WIDTH; $formStructure['height'] = (string) $FORM->HEIGHT; $formStructure['center'] = (string) $FORM->CENTER; $formStructure['fields'] = array(); $FIELDS = $FORM->FIELDS; $i = 0; foreach($FIELDS->INPUT as $input) { $formStructure['fields'][$i]['type'] = (string) $input->TYPE; $formStructure['fields'][$i]['id'] = (string) $input->ID; $formStructure['fields'][$i]['label'] = (string) $input->LABEL; //Autocomplete if(isset($input->AUTOCOMPLETE) && !empty($input->AUTOCOMPLETE)) { $formStructure['fields'][$i]['autocomplete'] = (string) $input->AUTOCOMPLETE; } //Values if(isset($input->VALUES)) { $VALUES = $input->VALUES; $formStructure['fields'][$i]['values'] = array(); foreach($VALUES->VALUE as $val) { array_push($formStructure['fields'][$i]['values'], array('id' => (string) $val->ID, 'label' => (string) $val->LABEL)); } } elseif(isset($input->TABLE)) //Values from database { $TABLE = $input->TABLE; $tableName = $TABLE->TABLE_NAME; $foreignKey = $TABLE->FOREIGN_KEY; $foreignLabel = $TABLE->FOREIGN_LABEL; $whereClause = $TABLE->WHERE_CLAUSE; $order = $TABLE->ORDER; $query = "select ".$foreignKey.", ".$foreignLabel." from ".$tableName; if(isset($whereClause) && !empty($whereClause)) { $query .= " where ".$whereClause; } if(isset($order) && !empty($order)) { $query .= ' '.$order; } //Show query in logs writeLog("Query for ".$formStructure['fields'][$i]['label']." : ".$query); $_ENV['db']->query($query); $formStructure['fields'][$i]['values'] = array(); while($res = $_ENV['db']->fetch_object()) { array_push($formStructure['fields'][$i]['values'], array('id' => (string) utf8_encode($res->$foreignKey), 'label' => utf8_encode($res->$foreignLabel))); } } //Default value for list if(isset($input->DEFAULT_VALUE) && !empty($input->DEFAULT_VALUE)) { $formStructure['fields'][$i]['default_value'] = (string) $input->DEFAULT_VALUE; } //Show id in combo list if(isset($input->SHOW_ID) && !empty($input->SHOW_ID)) { $formStructure['fields'][$i]['show_id'] = (string) $input->SHOW_ID; } //Mandatory if(isset($input->MANDATORY) && !empty($input->MANDATORY)) { $formStructure['fields'][$i]['mandatory'] = (string) $input->MANDATORY; } $i++; } //print_r($formStructure); $xml->createNode( $formStructure ); } else { die ("No form file. The process has been stopped by Maarch Capture Connector Server (Xml form file Error)"); } writeLog("End of the application"); header('Content-type: application/xml; charset=utf-8'); echo $xml; exit(0); ?>