Ajout de plus de classe, on va y arriver
This commit is contained in:
@@ -1,151 +0,0 @@
|
||||
<?php
|
||||
class Discussion{
|
||||
|
||||
protected $ID;
|
||||
protected $name;
|
||||
protected $creatorID;
|
||||
protected $dateCreation;
|
||||
protected $accessibility;
|
||||
|
||||
protected $valid;
|
||||
|
||||
public function __construct($ID){
|
||||
$this->ID = $ID;
|
||||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE ID=?');
|
||||
$req->execute(array($ID));
|
||||
if($rep = $req->fetch()){
|
||||
$this->valid = TRUE;
|
||||
$this->name = $rep['name'];
|
||||
$this->creatorID = $rep['creatorID'];
|
||||
$this->dateCreation = $rep['dateCreation'];
|
||||
$this->accessibility = $rep['accessibility'];
|
||||
}else{
|
||||
$this->valid = FALSE;
|
||||
}
|
||||
}
|
||||
public function exists(){
|
||||
return $this->valid;
|
||||
}
|
||||
public function __toString(){
|
||||
$out = 'Discussion\n';
|
||||
$out .= '\tID:' . $this->ID . '\n';
|
||||
$out .= '\tName:' . $this->name . '\n';
|
||||
$out .= '\tCreator\'s ID:' . $this->creatorID . '\n';
|
||||
$out .= '\tDate of creation:' . $this->dateCreation . '\n';
|
||||
$out .= '\tAccessibility:' . $this->accessibility . '\n';
|
||||
return out;
|
||||
}
|
||||
|
||||
//Getters
|
||||
public function getID(){
|
||||
return $this->ID;
|
||||
}
|
||||
|
||||
public function getName(){
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getCreatorID(){
|
||||
return $creatorID;
|
||||
}
|
||||
|
||||
public function getDateCreation(){
|
||||
return $this->dateCreation;
|
||||
}
|
||||
|
||||
public function getAccessibility(){
|
||||
return $this->accessibility;
|
||||
}
|
||||
|
||||
|
||||
//Useful Getters
|
||||
public function canAccess($membre){
|
||||
$out = $this->accessibility === 'p';
|
||||
if(preg_match ( "#^a[0-9]+$#", $this->accessibility ) == 1)
|
||||
$out = $out or intval ( substr ( $this->accessibility, 1 ) ) <= $membre->getAdminLevel();
|
||||
$out = $out or preg_match ( '#^x([0-9]+;)*' . $membre->getID() . '(;[0-9]+)*$#', $disc ['autorized'] ) == 1;
|
||||
$out = $out or $membre->getAdminLevel() >= 14;
|
||||
return $out;
|
||||
}
|
||||
|
||||
//Setters
|
||||
const NAME_ALREADY_USED = 'Nom déjà utilisé';
|
||||
const ILLEGAL_NAME = 'Le nom de la discussion est incorrect : les caractères autorisés sont :'.
|
||||
'<br/> abcdefghijklmnopqrstuvwxyz<wbr/>ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789<wbr/>'.
|
||||
'éèàμ_-\'()[\\]\\\\/<wbr/>,;:.§!ù%£$¤=+-*\\#~"|ç@';
|
||||
const TOO_LONG_NAME = 'Le nom est trop long : maximum 256 caractères';
|
||||
public function rename($newName){
|
||||
if(!exists())
|
||||
return NULL;
|
||||
if(getFromName($newName))
|
||||
return NAME_ALREADY_USED;
|
||||
$regex = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.
|
||||
'éèàµ_\\-\'()\\[\\]\\\\/,;:.§!ù%£$¤=+\\-*\\#~"|ç@';
|
||||
$regex = '#^['.$regex.']+$#';
|
||||
if(preg_match($regex,$newName) != 1)
|
||||
return ILLEGAL_NAME;
|
||||
}
|
||||
|
||||
//Discussions getters
|
||||
private static function discussionGetterOutput($req){
|
||||
$out = array();
|
||||
while($rep = $req->fetch())
|
||||
$out[] = new Discussion($rep['ID']);
|
||||
if($out)
|
||||
return $out;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
public static function getFromName($name){
|
||||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE name=?');
|
||||
$req->execute(array($name));
|
||||
return discussionGetterOutput($req);
|
||||
}
|
||||
|
||||
public static function getFromCreator($ID){
|
||||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE creatorID=?');
|
||||
$req->execute(array($ID));
|
||||
return discussionGetterOutput($req);
|
||||
}
|
||||
|
||||
public static function getFromDateCreation($date){
|
||||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE dateCreation=?');
|
||||
$req->execute(array($date));
|
||||
return discussionGetterOutput($req);
|
||||
}
|
||||
|
||||
public static function getFromAccessibility($accessibility){
|
||||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE creatorID=?');
|
||||
$req->execute(array($accessibility));
|
||||
return discussionGetterOutput($req);
|
||||
}
|
||||
|
||||
public static function getCreatedLaterThan($date){
|
||||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE dateCreation>?');
|
||||
$req->execute(array($date));
|
||||
return discussionGetterOutput($req);
|
||||
}
|
||||
|
||||
public static function getCreatedEarlierThan($date){
|
||||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions WHERE dateCreation<?');
|
||||
$req->execute(array($date));
|
||||
return discussionGetterOutput($req);
|
||||
}
|
||||
|
||||
public static function getWhichHeCanAccess($he){
|
||||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM discussions');
|
||||
$req->execute(array());
|
||||
$out = array();
|
||||
while ( $rep = $req->fetch()) {
|
||||
$disc = new Discussion($rep['ID']);
|
||||
if ($disc->canAccess($membre))
|
||||
$out[] = $disc;
|
||||
}
|
||||
if($out)
|
||||
return $out;
|
||||
else
|
||||
return NULL;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-107
@@ -1,107 +1 @@
|
||||
<?php
|
||||
class Membre {
|
||||
const DEFAULT_BANNER = "pictures/bande.png";
|
||||
protected $id;
|
||||
protected $password;
|
||||
protected $pseudo;
|
||||
protected $adminLevel;
|
||||
protected $dateCreation;
|
||||
protected $requiredBanner;
|
||||
protected $personnalMessage;
|
||||
protected $connected;
|
||||
public function __construct($id = NULL, $pass = NULL) {
|
||||
$this->id = $id ?? $_SESSION ['session_id'];
|
||||
$this->password = $pass ?? $_SESSION ['session_mdp'];
|
||||
$connected = FALSE;
|
||||
}
|
||||
public function connect() {
|
||||
$req = $GLOBALS ['bdd']->prepare ( 'SELECT * FROM users WHERE ID=?' );
|
||||
$req->execute ( array (
|
||||
$this->id
|
||||
) );
|
||||
if ($rep = $req->fetch ()) {
|
||||
$connected = password_verify ( $this->pass, $rep ['mdp'] );
|
||||
$this->adminLevel = $connected ? $result ['administration'] : - 1;
|
||||
$this->pseudo = $connected ? $result ['pseudo'] : NULL;
|
||||
if ($connected)
|
||||
decodeData ( $rep ['data'] );
|
||||
return $this->connected = $connected;
|
||||
} else {
|
||||
$req->closeCursor ();
|
||||
return $this->connected = FALSE;
|
||||
}
|
||||
}
|
||||
public function hasPersonnalMessage() {
|
||||
return isset ( $this->personnalMessage );
|
||||
}
|
||||
public function showPersonnalMessage() {
|
||||
$msg = $this->personnalMessage;
|
||||
$this->personnalMessage = NULL;
|
||||
return $msg;
|
||||
}
|
||||
private function decodeData($data) {
|
||||
$jsonData = json_decode ( $data );
|
||||
// Set the data's required_banner if it is defined , otherwise sets the DEFAULT_BANNER
|
||||
$this->requiredBanner = $jsonData ['requiredBanner'] ?? self::DEFAULT_BANNER;
|
||||
$this->personnalMessage = $jsonData ['personnalMessage'] ?? NULL;
|
||||
}
|
||||
private function encodeData() {
|
||||
$jsonArray = array ();
|
||||
// Set requiredBanner only if it is different from DEFAULT_BANNER
|
||||
$jsonArray ['requiredBanner'] = $this->requiredBanner !== self::DEFAULT_BANNER ? $this->requiredBanner : NULL;
|
||||
$jsonArray ['personnalMessage'] = $this->personnalMessage ?? NULL;
|
||||
return json_encode ( $jsonArray );
|
||||
}
|
||||
public function getID(){
|
||||
return $this->id;
|
||||
}
|
||||
public function getAdminLevel() {
|
||||
return $this->adminLevel;
|
||||
}
|
||||
public function getBanner() {
|
||||
return $this->requiredBanner;
|
||||
}
|
||||
public function isConnected() {
|
||||
return $connected;
|
||||
}
|
||||
public static function tryToConnect($pseudo = NULL, $mdp = NULL) {
|
||||
$pseudo = $pseudo ?? $_POST ['pseudo'];
|
||||
$mdp = $mdp ?? $_POST ['mdp'];
|
||||
$req = $GLOBALS ['bdd']->prepare ( 'SELECT * FROM users WHERE pseudo=?' );
|
||||
$req->execute ( array (
|
||||
$pseudo
|
||||
) );
|
||||
$reponce = $req->fetch ();
|
||||
if ($reponce != NULL) {
|
||||
if (password_verify ( $mdp, $reponce ['mdp'] )) {
|
||||
$req->closeCursor ();
|
||||
return $reponce ['ID'];
|
||||
} else {
|
||||
$req->closeCursor ();
|
||||
return 'errormdp';
|
||||
}
|
||||
} else {
|
||||
$req->closeCursor ();
|
||||
return 'errorpseudo';
|
||||
}
|
||||
}
|
||||
public static function registerPerson($pseudo, $mdp) {
|
||||
$req = $GLOBALS ['bdd']->prepare ( "SELECT * FROM users WHERE pseudo=?" );
|
||||
$req->execute ( array (
|
||||
$_POST ['pseudo']
|
||||
) );
|
||||
if ($req->fetch ())
|
||||
return 'usedPseudo';
|
||||
$req = $GLOBALS ['bdd']->prepare ( 'INSERT INTO users(pseudo,mdp,date_creation) VALUES (?,?,NOW())' );
|
||||
$req->execute ( array (
|
||||
$_POST ['pseudo'],
|
||||
password_hash ( $_POST ['mdp'], PASSWORD_DEFAULT )
|
||||
) );
|
||||
return 'ok';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<?phpclass Membre{ const DEFAULT_BANNER = "pictures/bande.png"; protected $id; protected $password; protected $pseudo; protected $adminLevel; protected $dateCreation; protected $requiredBanner; protected $personnalMessage; protected $connected; public function __construct($id = NULL,$pass = NULL){ $this->id = $id ?? $_SESSION['session_id']; $this->password = $pass ?? $_SESSION['session_mdp']; $connected = FALSE; } public function connect(){ $req = $GLOBALS ['bdd']->prepare ( 'SELECT * FROM users WHERE ID=?' ); $req->execute (array($this->id)); if ($rep = $req->fetch ()){ $connected = password_verify ( $this->pass, $rep ['mdp'] ); $this->adminLevel = $connected?$result ['administration']:-1; $this->pseudo = $connected?$result['pseudo']:NULL; if($connected)decodeData($rep['data']); return $this->connected = $connected; }else{ $req->closeCursor (); return $this->connected = FALSE; } } public function hasPersonnalMessage(){ return isset($this->personnalMessage); } public function showPersonnalMessage(){ $msg = $this->personnalMessage; $this->personnalMessage = NULL; return $msg; } private function decodeData($data) { $jsonData = json_decode($data); //Set the data's required_banner if it is defined , otherwise sets the DEFAULT_BANNER $this->requiredBanner = $jsonData['requiredBanner'] ?? self::DEFAULT_BANNER; $this->personnalMessage = $jsonData['personnalMessage'] ?? NULL; } private function encodeData(){ $jsonArray = array(); //Set requiredBanner only if it is different from DEFAULT_BANNER $jsonArray['requiredBanner'] = $this->requiredBanner !== self::DEFAULT_BANNER ? $this->requiredBanner : NULL; $jsonArray['personnalMessage'] = $this->personnalMessage ?? NULL; return json_encode($jsonArray); } public function getAdminLevel(){ return $this->adminLevel; } public function getBanner() { return $this->requiredBanner; } public function isConnected(){ return $connected; } public static function tryToConnect($pseudo = NULL,$mdp = NULL){ $pseudo = $pseudo ?? $_POST ['pseudo']; $mdp = $mdp ?? $_POST ['mdp']; $req = $GLOBALS ['bdd']->prepare ( 'SELECT * FROM users WHERE pseudo=?' ); $req->execute ( array ( $pseudo ) ); $reponce = $req->fetch (); if ($reponce != NULL) { if (password_verify ( $mdp, $reponce ['mdp'] )) { $req->closeCursor (); return $reponce ['ID']; } else { $req->closeCursor (); return 'errormdp'; } } else { $req->closeCursor (); return 'errorpseudo'; } } public static function registerPerson($pseudo, $mdp) { $req = $GLOBALS ['bdd']->prepare ( "SELECT * FROM users WHERE pseudo=?" ); $req->execute (array($_POST ['pseudo'])); if ($req->fetch ()) return 'usedPseudo'; $req = $GLOBALS ['bdd']->prepare ( 'INSERT INTO users(pseudo,mdp,date_creation) VALUES (?,?,NOW())' ); $req->execute(array($_POST ['pseudo'],password_hash ( $_POST ['mdp'], PASSWORD_DEFAULT))); return 'ok'; }}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
class Projet{
|
||||
protected $ID;
|
||||
protected $name;
|
||||
protected $dateCreation;
|
||||
protected $owner;
|
||||
protected $secondaryOwners;
|
||||
|
||||
protected $valid;
|
||||
|
||||
public function __construct($ID){
|
||||
$this->ID = $ID;
|
||||
$req = $GLOBALS['bdd']->prepare('SELECT * FROM projets WHERE ID=?');
|
||||
$req->execute(array($ID));
|
||||
if($rep = $req->fetch()){
|
||||
$this->valid = TRUE;
|
||||
$this->name = $rep['name'];
|
||||
$this->owner = new Membre($rep['ownerID']);
|
||||
$this->dateCreation = $rep['dateCreation'];
|
||||
$this->secondaryOwners = array();
|
||||
foreach (explode(';',$rep['secondaryOwners']) AS $secondaryOwner)
|
||||
$this->secondaryOwners[] = new Membre($secondaryOwner);
|
||||
}else{
|
||||
$this->valid = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
public function exists(){
|
||||
return $this->valid;
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
$out = 'Projet\n';
|
||||
$out .= '\tID:' . $this->ID . '\n';
|
||||
$out .= '\tName:' . $this->name . '\n';
|
||||
$out .= '\tOwner:{' . substr(str_replace('\n\t',';',$this->owner->__toString()),0,-1) . '}\n';
|
||||
foreach($this->secondaryOwners AS $secondaryOwner)
|
||||
$out .= '\tSecondary owner:{' . substr(str_replace('\n\t',';',$secondaryOwner->__toString()),0,-1) . '}\n';
|
||||
$out .= '\tDate of creation:' . $this->dateCreation . '\n';
|
||||
return out;
|
||||
}
|
||||
|
||||
//Getters
|
||||
public function getID(){
|
||||
return $this->ID;
|
||||
}
|
||||
|
||||
public function getName(){
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getOwner(){
|
||||
return $this->owner;
|
||||
}
|
||||
|
||||
public function getSecondaryOwners(){
|
||||
return $this->secondaryOwners;
|
||||
}
|
||||
|
||||
public function getDateCreation(){
|
||||
return $this->dateCreation;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user