VERSION = $version; } public function getTodo($pid, $stmt, $req, $id) { $msg='';$pgm='';$fonc='';$this->pid=$pid;$this->stmt=$stmt; $pgm=basename(__FILE__, '.php'); $fonc='.getIDTodo() ';$this->fonc=$fonc; $this->pgm=$pgm; $msg=' -deb getTodo('.$id.') req: '. $req;$this->msg=$msg; $ok='ko';$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); //$msg=' -Req postdta:'. $req;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); //Recup des donnees dans mysql - - - - - - - - - - - - - - - - - $sql = 'SELECT * FROM todo ' ; $sql .= " Where titre ='" . $id . "'"; if(strlen($req)>0)$sql=$req;//on surcharge avec la req recu du POST if($id === 'value')$this->bolColumn=false;//pas de header si on veut recuperer que la valeur $msg=' -Req:'. $sql;//$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); $bolok= $this->execConn($sql); $bolok= $this->snd_response($this->arRows); } function getAllTodo($pid, $stmt, $req) { $msg='';$pgm='';$fonc='';$this->pid=$pid;$this->stmt=$stmt; $pgm=basename(__FILE__, '.php');$this->pgm=$pgm; $fonc='.getAllTodo() ';$this->fonc=$fonc; $msg=' -deb getAllTodo(all): req recue:' .$req;$this->msg=$msg; $ok='ko';$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); //$msg=' -Req postdta:'. $req;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); //Recup des donnees dans mysql - - - - - - - - - - - - - - - - - $sql = 'SELECT * FROM todo' ; if(strlen($req)>0)$sql=$req; $msg=' -Req:'. $sql;//$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); $bolok= $this->execConn($sql); $bolok= $this->snd_response($this->arRows); }//fin getAllTodo() function saveAllPages($pid, $stmt, $req) { $msg='';$pgm='';$fonc='';$this->pid=$pid;$this->stmt=$stmt; $pgm=basename(__FILE__, '.php');$this->pgm=$pgm; $fonc='.saveAllPages() ';$this->fonc=$fonc; $msg=' -deb saveAllPages(all): req recue:' .$req;$this->msg=$msg; $ok='ko';$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); //Update des donnees dans mysql - - - - - - - - - - - - - - - - - $sql = ''; if(strlen($req)>0){ $sql=$req; $msg=' -Req:'. $sql;//$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); $bolok= $this->execConn($sql); $bolok= $this->snd_response($this->arRows); } }//fin saveAllPages() function putAllTodo($pid, $stmt, $req) { $msg='';$pgm='';$fonc='';$this->pid=$pid;$this->stmt=$stmt; $pgm=basename(__FILE__, '.php');$this->pgm=$pgm; $fonc='.putAllTodo() ';$this->fonc=$fonc; $msg=' -deb putAllTodo(all): req recue:' .$req;$this->msg=$msg; $ok='ko';$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); //Update des donnees dans mysql - - - - - - - - - - - - - - - - - $sql = ''; if(strlen($req)>0){ $sql=$req; $msg=' -Req:'. $sql;//$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); $bolok= $this->execConn($sql); $bolok= $this->snd_response($this->arRows); } }//fin putAllTodo() function putTodo($pid, $stmt, $req, $id) { // $msg='';$pgm='';$fonc='';$this->pid=$pid;$this->stmt=$stmt; $pgm=basename(__FILE__, '.php');$this->pgm=$pgm; $fonc='.putAllTodo() ';$this->fonc=$fonc; $msg=' -deb putAllTodo(all): req recue:' .$req;$this->msg=$msg; $ok='ko';$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); //Update des donnees dans mysql - - - - - - - - - - - - - - - - - $sql = ''; if(strlen($req)>0){ $sql=$req; $msg=' -Req:'. $sql;//$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); $bolok= $this->execConn($sql); $bolok= $this->snd_response($this->arRows); } }//fin putTodo() public function execConn($sql){ $msg=""; $pgm=basename(__FILE__, '.php'); $fonc='.execConn() '; $this->msg=""; $this->pgm=$pgm; $this->fonc=$fonc; $stmt=$this->stmt;// {select, update, delete, ...} $conn = $this->open_connexion(); $bolok= $this->log_connexion($conn);//log connexion OK (ou) KO $arrawsData=array();//tableau d'objet d'array des values if (strpos($stmt, 'select') !== false){ $bolok=$this->execMysqlReqSelect($conn, $sql);// exec requete si {Select} } if (strpos($stmt, 'update') !== false || strpos($stmt, 'save') !== false ){ $bolok=$this->execMysqlReqCMIS($conn, $sql);// exec requete si {insert,delete,update,create,drop} } $msg=' -connexion close : ' . $this->msgret; $conn->close(); $ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); /* $bolok= $this->snd_response($stmt); $prt = print_r($this->arRows, true); $msg=' -Prt:'. $prt;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); $strprt = ""; foreach ($this->arRows as $rowarr) { foreach ($rowarr as $key => $value) { $strprt .= "k:=" . $key . " -v:=" . $value .PHP_EOL ; } } $msg=' -strPrt:'. $strprt;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); */ return true; } public function open_connexion() { $servername = "localhost"; $username = "obhc6145_FR028923"; $password = "qVd8-faxQ-UU2?"; $dbname = "obhc6145_TODO"; return mysqli_connect($servername, $username, $password, $dbname); }//fin open_connexion() public function log_connexion($conn) { $msg="";$pgm=$this->pgm;$fonc=$this->fonc; $bolaff=false; if (!$conn) { echo 'Echec de la connexion : ' . mysqli_connect_error(); echo PHP_EOL; echo ' -DOCUMENT_ROOT: ' . $DOCUMENT_ROOT; echo PHP_EOL; echo ' -servername: ' . $servername . ' -username:' . $username . ' -password: ' . $password; echo PHP_EOL; $bolaff=true; $msg=' -connexion KO:' . mysqli_connect_error() ;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); }else{ if ($bolaff){ echo ' connexion OK'; echo PHP_EOL; } $msg=' -connexion OK';$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); } return true; }//fin log_connexion() // execMysqlReqCMIS =================================================== public function execMysqlReqCMIS($conn, $sql) { $msg="";$pgm=$this->pgm;$fonc="execMysqlU";$this->fonc=$fonc; $result = mysqli_query($conn, $sql); /* En cas d'{insert,drop,create,delete}, mysqli_info() returns un empty string ce qui peut preter a confusion, tandis que mysqli_affected_rows() returns 1 si (delete, insert) ou 0 si (create, drop). */ $this->msgret=$conn->info; if(strlen($this->msgret)<1){ $b = explode(' ', $sql);//split sur le blanc (insert,delete,update, create, drop) $this->msgret="Affected rows (" . $b[0] . "): ". mysqli_affected_rows($conn); } return true; } // execMysqlReqSelect =================================================== public function execMysqlReqSelect($conn, $sql) { $msg="";$pgm=$this->pgm;$fonc=".execMysqlS ";$this->fonc=$fonc; $msg=' -deb execMysqlReqSel: req recue:' .$sql;$this->msg=$msg; $ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); // recup result dans this.arRows array d'objets $result = mysqli_query($conn, $sql); //ajout pour debug $nread=mysqli_num_rows ( $result ); $msg=' .nb lus:' .$nread;$this->msg=$msg; $ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); $v='';$nbr=0; // Liste le nom,type,.. de la table et ensuite, le nom du champ /* for ($i = 0; $i < mysqli_num_fields($result); ++$i) { // $table = mysqli_field_table($result, $i); // nom de la table // $field = mysqli_field_name($result, $i); // nom du champ // totalite des infos des colonnes $table_info = mysqli_fetch_field_direct($result, $i); $lenght = $table_info->length; $field = $table_info->name; // nom du champ $table =$table_info->table; // todo $type = $table_info->type; // Text,integer, $flag = $table_info->flags;// if ($i<1)$v.=$field; //else $v.='xxx'.$table . '.' . $field; else $v.='xxx' . $field; //$msg=$v;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); } */ $msg=' .recupInfos: nom des colones!';$this->msg=$msg; $ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); $info = $result->fetch_fields(); $i=0; foreach ($info as $table_info) { $lenght = $table_info->length; $field = $table_info->name; // nom du champ $table =$table_info->table; // todo $type = $table_info->type; // Text,integer, $flag = $table_info->flags;// if ($i<1)$v.=$field; else $v.='xxx' . $field; $i=$i+1; } $msg=' .liste le contenu des champs!';$this->msg=$msg; $ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); $this->arRowsHeadColumn= explode('xxx', $v);//split sur le - // liste le contenu des champs while ($line = mysqli_fetch_assoc($result)) { array_push($this->arRows, (object)$line);//ajout objet.array au tableau resultat $nbr=$nbr+1; } $msg=' .memo dans classe !';$this->msg=$msg; $ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); $this->nbr=$nbr; //$this->rawData=$this->arRows; //return $this->arRows; $this->msgret=$conn->info; return true; }//fin execMysqlReq() public function snd_response($stmt) { // renvoi des donnees suivant le Accept demandé ------------------------------------ $msg="";$pgm=$this->pgm;$fonc='.snd_reponse()';$this->fonc=$fonc; try{ $statusCode = 200; $requestAccept = $_SERVER['HTTP_ACCEPT']; // on renvoie le status-code{200;404;500} .................. // et le content-type avec la meme valeur que le accept recu! $this->setHttpHeaders($requestAccept, $statusCode); if(strpos($this->stmt, 'select') !== false) { // suivant Accept on encode la reponse ..................... if (strpos($requestAccept, 'application/json') !== false) { $this->action='Select'; $response = $this->encodeJson2(); $msg=$requestAccept . ':'.$response;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); echo $response; } else if (strpos($requestAccept, 'application/xml') !== false) { $response = $this->encodeXml2(); $msg=$requestAccept . ':'.$response;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); echo $response; } else if (strpos($requestAccept, 'text/html') !== false) { //mise en tableau pendant la recup du result $response = $this->encodeHtml2(); $msg=$requestAccept . ':'.$response;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); echo $response; } else if (strpos($requestAccept, '*/*') !== false) { $response = $this->encodeTxt2(); $msg=$requestAccept . ':'.$response;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); echo $response; } else{ $response = $this->encodeTxt2(); $msg=$requestAccept . ':'.$response;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); echo $response; } } if(strpos($this->stmt, 'update') !== false || strpos($this->stmt, 'save') !== false ) { // suivant Accept on encode la reponse ..................... if (strpos($requestAccept, 'application/json') !== false) { $response = "{\"result\":\"".$this->msgret."\"}"; $msg=$requestAccept . ':'.$response;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); echo $response; } else if (strpos($requestAccept, 'application/xml') !== false) { $response = "".$this->msgret.""; $msg=$requestAccept . ':'.$response;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); echo $response; } else if (strpos($requestAccept, 'text/html') !== false) { $response = "".$this->msgret.""; $msg=$requestAccept . ':'.$response;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); echo $response; } else if (strpos($requestAccept, '*/*') !== false) { $response = $this->msgret; $msg=$requestAccept . ':'.$response;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); echo $response; } else{ $response = $this->msgret; $msg=$requestAccept . ':'.$response;$ok=logmsg($this->pid,'log.txt',$msg, $pgm, $fonc); echo $response; } } } catch (Throwable $e) { $statusCode = 500; $this->setHttpHeaders($requestAccept, $statusCode);// on renvoie le status-code{200;404;500} echo "--catch Throwable: " . $e->getMessage() . PHP_EOL; } return true; }//fin snd_response() public function encodeTxt2() { $txtResponse = ""; foreach ($this->arRows as $rowarr) { foreach ($rowarr as $key => $value) { //on ne renvoi que la valeur si id=value if($this->bolColumn==false)$txtResponse .= $value . PHP_EOL; else $txtResponse .= $key . ":=" . $value . PHP_EOL; } } return $txtResponse; } public function encodeJson2() { $json = json_encode($this->arRows);//on encode l'objet::array[] en string.json $array = json_decode($json, true); //on recupere un array() du string.json $dateselect =date('Y-m-d H:i:s'); //on formatte la reponse {"result":{ "schedules":[{...},...,{...}]} // ,"_metadata":{...,...}} $jsonResponse = json_encode(new Result($this->VERSION, $this->action, $this->nbr, $array, $dateselect)); return $jsonResponse; } public function encodeHtml2() { $htmlResponse=""; $htmlResponse .= ""; //TR du tableau des entetes $htmlResponse .=""; foreach ($this->arRowsHeadColumn as $etc) { $htmlResponse .= ""; } $htmlResponse .= ""; //TR(s) du tableau des donnees foreach ($this->arRows as $rowarr) { $htmlResponse .= ""; foreach ($rowarr as $key => $value) { //$htmlResponse .= "".""; $htmlResponse .= ""; } $htmlResponse .= ""; } $htmlResponse .= "
" . $etc . "
" . $key . "" . $value . "" . $value . "
"; return $htmlResponse; } public function encodeXml2() { // creating object of SimpleXMLElement $meta=array( 'call' => $this->action . $this->nbr . ' row(s)', 'date' => date('Y-m-d H:i:s'), 'version'=> $this->VERSION ); $xml = new SimpleXMLElement('');//encapsulage racine $boleach=true; if(!$boleach){ $xml->addChild('schedule',''); $iid=0; foreach ($this->arRows as $rowarr) { $iid++; $sid='ID'.$iid; $xml->schedule->addChild($sid,''); foreach ($rowarr as $key => $value) { $xml->schedule->$sid->addChild($key, $value); } } $xml->addChild('meta',''); foreach ($meta as $key=>$value) { $xml->meta->addChild($key, $value); } return $xml->asXML(); } if($boleach){ $json = json_encode($this->arRows);//on encode l'objet::array[] en string.json $array = json_decode($json, true); //on recupere un array() du string au format json $this->to_xml($xml, $array); return $xml->asXML(); } //push un 2eme array dans le premier /* array_walk_recursive($array, function($value, $key)use($xml){ $xml->addChild($key, $value); }); array_walk_recursive($meta, function($value, $key)use($xml){ $xml->addChild($key, $value); }); return $xml->asXML(); */ /* $dom = new DOMDocument(); $dom->encoding = 'utf-8'; $dom->xmlVersion = '1.0'; $dom->formatOutput = true; $root = $dom->createElement('Result'); $prt = print_r($array, true);//conversion de Array en string $child_node_schedule = $dom->createElement('schedule', $prt); $root->appendChild($child_node_schedule); $prt = print_r($meta, true);//conversion de Array en string $child_node_meta = $dom->createElement('Meta', $prt); $root->appendChild($child_node_meta); $dom->appendChild($root); return $dom->saveXML(); */ } function to_xml($object, $data){ foreach ($data as $key => $value) { if (is_array($value)) { $new_object = $object->addChild($key); $this->to_xml($new_object, $value); } else { // if the key is an integer, it needs text with it to actually work. if ($key != 0 && $key == (int) $key) { $key = "k_".$key; } $object->addChild($key, $value); } } } // sauvegarde log ===================================================== /* fonction defini dans controller public function logmsg($msg, $pgm, $fonc){ $ok='ok'; $ficlog='log.txt'; date_default_timezone_set('Europe/Paris'); $dateselect =date('Y-m-d H:i:s'); $ficl=fopen($ficlog, 'a');fwrite($ficl,date('Y-m-d H:i:s').' '.$pgm.$fonc.':'.$msg.'\r\n');fclose($ficl); return $ok; } */ } // classes pour un retour au format JSON: // {"result":{"schedules":[{"_id":"1","basep ... _number":"2"}]} // ,"_metadata":{"call":"Select 2 row(s).","date":"2022-09-25 17:00:17","version":"1.2.5"}} class result { public $result; public $_metadata; function __construct($version, $sel, $nb, $todo, $dateselect){ $this->result=new Schedule($sel, $nb, $todo, $dateselect); $this->_metadata=new Meta($version, $sel , $nb, $dateselect); } } class Schedule { public $schedules = array(); function __construct($sel,$nb, $todo, $dateselect){ //array_push($this->schedules, $todo); $this->schedules=$todo; } } class Meta { public $call = '1'; public $date = 'azerty'; public $version = '1.0.0'; function __construct($version, $action, $nombre, $dateselect){ $this->call=$action . ' ' . $nombre . ' row(s).'; $this->date= $dateselect; $this->version= $version; } } ?>