Skip to content
This repository was archived by the owner on Jul 17, 2018. It is now read-only.

Commit a585f58

Browse files
author
Kasai
authored
Edit deleteUser/getChannels & the messages functions (#2)
* Modifying the messages functions - Edited the deleteMessage function. - Replace $data[2] by $data[5] cf: users.csv - Removed the "admins" rights for "acs-dds" user. - Changed the resetMessages function since apache send me errors about it. - Added a new "notification": "Soon" * Changed the getChannels function Upgrated the old-school function for the getChannels function to a new one in POO. * Edit deleteUser/getChannels + Initialize new object "Controller" + Trim the username sent by post. * Replace the old getChannels function by the new one, in POO
1 parent 8599a82 commit a585f58

File tree

9 files changed

+52
-23
lines changed

9 files changed

+52
-23
lines changed

.classes/channelmapper.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ public function __construct(){}
77
public function createChannel($name){
88
$n = trim($name);
99
if($n != "null") :
10-
var_dump($n);
1110
if($n != "") :
12-
var_dump($n);
1311
if(!file_exists($this->fichier = __DIR__ . "/../.data/db/channels/" . $n . ".csv")) :
14-
var_dump($n);
1512
touch($this->fichier);
1613
endif;
1714
endif;
@@ -25,4 +22,16 @@ public function deleteChannel($name){
2522
endif;
2623
endif;
2724
}
25+
26+
public function getChannels(){
27+
$channel = str_replace(array(".csv"),"",array_diff(scandir(".data/db/channels"),array(".","..")));
28+
29+
ob_start();
30+
require(".data/tpl/channels.tpl");
31+
return ob_get_clean();
32+
}
33+
34+
public function __toString(){
35+
return $this->getChannels();
36+
}
2837
}

.classes/controller.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,21 @@ public function changeChannel(){
5454
return $this->message_mapper->getMessages();
5555
}
5656

57+
public function getChannels(){
58+
if(!$this->isRegistered()) return;
59+
60+
return $this->channel_mapper->getChannels();
61+
}
62+
5763
public function createChannel($name){
5864
if(!$this->isRegistered()) return;
5965

6066
return $this->channel_mapper->createChannel($name);
6167
}
6268

6369
public function deleteChannel($name){
70+
if(!$this->isRegistered()) return;
71+
6472
return $this->channel_mapper->deleteChannel($name);
6573
}
6674

.classes/messagemapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function deleteMessage($name,$time){
5959

6060
public function resetMessages(){
6161
$file = fopen($this->fichier,"w");
62-
fputcsv($file,"");
62+
ftruncate($file,0);
6363
fclose($file);
6464
}
6565
}

.classes/usermapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function deleteUser($name){
6565
$temp_table = fopen($this->file_temp,"w");
6666

6767
while(($data = fgetcsv($table,1000)) !== FALSE){
68-
if($data[2] == $name){
68+
if($data[5] == $name){
6969
continue;
7070
}
7171

.data/db/users.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
acs,dds,8e0d92e98814d1cba96d8848fe709d36f94499ce,acs@ofp.com,azeaze.jpg,acs-dds,logged-out
2-
PERROT,Corentin,8e0d92e98814d1cba96d8848fe709d36f94499ce,kasaidot@gmail.com,img.png,Kasai.,logged
1+
PERROT,Corentin,8e0d92e98814d1cba96d8848fe709d36f94499ce,kasaidot@gmail.com,img.png,Kasai.,logged-out
2+
latti,lol,63982e54a7aeb0d89910475ba6dbd3ca6dd4e5a1,unknow@gmail.com,1234,CrackJ,logged-out

.data/tpl/channels.tpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php foreach($channel as $id => $b) : ?>
2+
<li>
3+
<a id="ch-<?=$id;?>" name="<?=$b;?>" href="#" onclick="channel=this.name;changeChannel(this.name)"><?=$b;?></a>
4+
<?php if($_SESSION["username"] == "PERROT Corentin") : ?>
5+
<input type="button" id="suppr" name="<?=$b;?>" onclick="deleteChannel(this.name)" value="">
6+
<?php endif;?>
7+
</li>
8+
<?php endforeach;?>

.data/tpl/users.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<li class="<?=$this->status;?>">
2-
<a id="pm-<?=$this->pseudo;?>" href="#"><?=$this->pseudo;?></a>
3-
<?php if($_SESSION["username"] == "PERROT Corentin" || $_SESSION["username"] == "acs dds") : ?>
2+
<a id="pm-<?=$this->pseudo;?>" href="#" onclick="soon();"><?=$this->pseudo;?></a>
3+
<?php if($_SESSION["username"] == "PERROT Corentin") : ?>
44
<input type="button" id="suppr" name="<?=$this->pseudo;?>" onclick="deleteUser(this.name);" value="">
55
<?php endif;?>
66
</li>

act.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,22 @@
8484

8585
// USERS DELETE
8686
if(isset($_POST["usersdelete"])) :
87-
$_SESSION["TChat"]->deleteUser($_POST["usersdelete"]);
87+
$_SESSION["TChat"] = new Controller();
88+
$_SESSION["TChat"]->deleteUser(trim($_POST["usersdelete"]));
8889
endif;
8990

9091
// CHANNELS
9192
if(isset($_POST["channels"])) :
92-
$dir = ".data/db/channels";
93-
$files = array_diff(scandir($dir),array(".",".."));
94-
$ext = array(".csv");
95-
96-
$out = str_replace($ext,"",$files);
97-
foreach($out as $id => $channel) : ?>
98-
<li>
99-
<a id="ch-<?=$id;?>" name="<?=$channel;?>" href="#" onclick="channel=this.name;changeChannel(this.name)"><?=$channel;?></a>
100-
<?php if($_SESSION["username"] == "PERROT Corentin" || $_SESSION["username"] == "acs dds") : ?>
101-
<input type="button" id="suppr" name="<?=$channel;?>" onclick="deleteChannel(this.name)" value="✖">
102-
<?php endif;?>
103-
</li>
104-
<?php endforeach;endif;
93+
$_SESSION["TChat"] = new Controller();
94+
95+
// CHANNELS GET
96+
if(isset($_POST["get"])) :
97+
$a = $_SESSION["TChat"]->getChannels();
98+
if(!empty($a)) :
99+
echo $a;
100+
endif;
101+
endif;
102+
endif;
105103

106104
// CHANGE
107105
if(isset($_POST["change"])) :

index.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
<p id="bienvenue">Bonjour et bienvenue sur TChat, <b><?=$_SESSION["username"];?> !</b></p>
2020
</div>
2121
</div>
22+
<div class="notif" id="soon">
23+
<div class="success">
24+
<p id="bienvenue">Fonctionalitée en cours de développement !</b></p>
25+
</div>
26+
</div>
2227
<div id="header">
2328
<h1 id="ch-name"></h1>
2429
<?php if($_SESSION["username"] == "PERROT Corentin" || $_SESSION["username"] == "acs dds") : ?>
@@ -75,6 +80,7 @@
7580
login();
7681
});
7782
var d=new Date();
83+
var soon=function(){window.location="#soon";setTimeout(function(){window.location="#";},3000);};
7884
var login=function(){$.ajax({url:"act.php",method:"POST",data:"login=<?=$_SESSION['username'];?>"})};
7985
var logout=function(){$.ajax({url:"act.php",method:"POST",data:"logout=<?=$_SESSION['username'];?>",success:function(){window.location="./login";}})};
8086
var send=function(e){a=encodeURIComponent($("#author").val());c=encodeURIComponent($("#content").val());if(a!=""&&c!=""){$.ajax({url:"act.php",method:"POST",data:"channel="+channel+"&author="+a+"&content="+c,success:function(){$("#content").val("")}})}e.preventDefault();};

0 commit comments

Comments
 (0)