Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class managementdb{
- public function getConnection() {
- $dsn = 'mysql:dbname='.Constants::dbname.';host='.Constants::dbhost.'';
- $username = Constants::dbusername;
- $password = Constants::dbpassword;
- try {
- $conn = new PDO($dsn, $username, $password, array(
- PDO::ATTR_PERSISTENT => true
- ));
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- echo "Connection failed: " . $e->getMessage();
- }
- return $conn;
- }
- public function antifurbetti($id1, $id2){
- return 0;
- }
- public function get_rilevazione($id1, $id2){
- $conn= $this->getConnection();
- $stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.Constants::dbtablerivelazioni." WHERE id_from=$id1 and id_to=$id2");
- $stm->execute();
- $res=$stm->fetch();
- $from_a_to_b = $res["res"];
- $stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.Constants::dbtablerivelazioni." WHERE id_from=$id2 and id_to=$id1");
- $stm->execute();
- $res=$stm->fetch();
- $from_b_to_a = $res["res"];
- if(($from_a_to_b==0)&&($from_b_to_a)==0){
- $this->nuova_rivelazione($id1, $id2);
- return Constants::risposta_reveal;
- }
- if(($from_b_to_a!=0)){ //mi voglio rivelare anche io!
- return Constants::attiva_rilevazione;
- }
- return Constants::risposta_reveal2;
- }
- public function classifica($limit){
- $conn= $this->getConnection();
- $query = "select avg(voto) as MEDIA, id_votata from Votazioni group by id_votata having avg(voto) > 4 and avg(voto)<=5 order by avg(voto) DESC limit 0, $limit";
- @$stm=$conn->prepare($query);
- @$stm->execute();
- @$res=$stm->fetchAll();
- return $res;
- }
- public function vota($votante, $votata, $voto){
- $conn = $this->getConnection();
- if($voto > 6)
- $voto = 5;
- if($voto < -2)
- $voto = -2;
- $q = "insert into Votazioni values (NULL, $votante, $votata, $voto);";
- $st = $conn->prepare($q);
- echo $q;
- $st->execute();
- return 1;
- }
- public function inserisci_messaggio_privato($id1, $id2, $text){
- $time = time();
- $conn = $this->getConnection();
- $st = $conn->prepare("insert into ".Constants::prefix_db.Constants::messaggi_privati." values (NULL, :id1, :id2, :text, :time);");
- $st->bindParam(":id1", $id1);
- $st->bindParam(":id2", $id2);
- $st->bindParam(":text", trim($text));
- $st->bindParam(":time", $time);
- $st->execute();
- return 1;
- }
- public function interrogazione_storico($id1, $id2){
- $conn = $this->getConnection();
- $q = "select max(time) as res from ".Constants::prefix_db.Constants::messaggi_privati." where id1 = '".$id1."' AND id2 = '".$id2."';";
- $st = $conn->prepare($q);
- $st->execute();
- $res=$st->fetch();
- return $res["res"];
- }
- public function nuova_rivelazione($id1, $id2){
- $conn = $this->getConnection();
- $st = $conn->prepare("insert into ".Constants::prefix_db.Constants::dbtablerivelazioni." (id_from, id_to) values (:id1, :id2);");
- $st->bindParam(":id1", $id1);
- $st->bindParam(":id2", $id2);
- $st->execute();
- return 1;
- }
- public function elimina_rivelazione($id1, $id2){
- $conn = $this->getConnection();
- $st = $conn->prepare("DELETE FROM ".Constants::prefix_db.Constants::dbtablerivelazioni." WHERE id_from = :id1 AND id_to = :id2 ");
- $st->bindParam(":id1", $id1);
- $st->bindParam(":id2", $id2);
- $st->execute();
- $st = $conn->prepare("DELETE FROM ".Constants::prefix_db.Constants::dbtablerivelazioni." WHERE id_from = :id1 AND id_to = :id2 ");
- $st->bindParam(":id1", $id2);
- $st->bindParam(":id2", $id1);
- $st->execute();
- return 1;
- }
- public function trova_proprietario($id) {
- $id = trim($id);
- $id = str_replace("#", "", $id);
- echo "<br> ------- Sto provando a trovare il proprietario di $id";
- $conn = $this->getConnection();
- $query = "select chi_scrive as X from ".Constants::prefix_db.Constants::dbtablemessages." where id=$id";
- echo "<br> $query </br>";
- $st = $conn->prepare($query);
- $st->execute();
- $res=$st->fetch();
- //print_r($res);
- if($res['X']==NULL)
- return -1093;
- return $res['X'];
- }
- public function id_from_message($id, $message){
- $conn= $this->getConnection();
- $query = "select id as RES from ".Constants::prefix_db.Constants::dbtablemessages." where contenuto =:message";
- $stm=$conn->prepare($query);
- $stm->bindParam("message", $message);
- $stm->execute();
- $res=$stm->fetch();
- return $res['RES'];
- }
- public function id_from_potrebbeservire($n){
- $conn= $this->getConnection();
- $query = "select id as RES from ".Constants::prefix_db.Constants::dbtableusers." where potrebbe_servire=:n";
- $stm=$conn->prepare($query);
- $stm->bindParam("n", $n);
- $stm->execute();
- $res=$stm->fetch();
- return $res['RES'];
- }
- public function idconv_from_id($id) {
- $conn = $this->getConnection();
- $query = "select potrebbe_servire as X from ".Constants::prefix_db.Constants::dbtableusers." where id=$id";
- $st = $conn->prepare($query);
- $st->execute();
- $res=$st->fetch();
- echo "<br> $query </br>";
- if($res['X']==NULL)
- return -1092;
- return $res['X'];
- }
- public function tempo_ultimo_messaggio($id){
- $conn= $this->getConnection();
- $stm=$conn->prepare("select max(quando_scrive) as res from ".Constants::prefix_db.Constants::dbtablemessages." where chi_scrive=:id");
- $stm->bindParam(":id", $id);
- $stm->execute();
- $res=$stm->fetch();
- if($res["res"]==NULL)
- return 0;
- return $res["res"];
- }
- public function conta_nick($nick) {
- $conn= $this->getConnection();
- $stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.Constants::dbtableusers." where nickname_utente = '".$nick."' ");
- $stm->execute();
- $res=$stm->fetch();
- return $res["res"];
- }
- public function conta_sesso($s) {
- $conn= $this->getConnection();
- $stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.Constants::dbtableusers." where sesso_utente IS NOT NULL ");
- if(strlen($s)>2)
- @$stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.Constants::dbtableusers." where sesso_utente='".$s."'");
- if(strlen($s)>10)
- @$stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.Constants::dbtableusers." where sesso_utente IS NULL");
- $stm->execute();
- $res=$stm->fetch();
- return $res["res"];
- }
- public function num_rows($tablename) {
- $conn= $this->getConnection();
- $stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.$tablename." ");
- $stm->execute();
- $res=$stm->fetch();
- return $res["res"];
- }
- public function conta_tutti() {
- $conn= $this->getConnection();
- $stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.Constants::dbtableusers." ");
- $stm->execute();
- $res=$stm->fetch();
- return $res["res"];
- }
- public function conta($id) {
- $conn= $this->getConnection();
- $stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.Constants::dbtableusers." where id=:id");
- $stm->bindParam(":id", $id);
- $stm->execute();
- $res=$stm->fetch();
- return $res["res"];
- }
- public function conta_potrebbe_servire($potrebbe_servire) {
- $conn= $this->getConnection();
- $stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.Constants::dbtableusers." where potrebbe_servire=:potrebbe_servire");
- $stm->bindParam(":potrebbe_servire", $potrebbe_servire);
- $stm->execute();
- $res=$stm->fetch();
- return $res["res"];
- }
- public function checkDuplicateMessage($text) {
- $text = substr($text, 1, strlen($text)-1);
- $vecchio = $text;
- $conn= $this->getConnection();
- $stm=$conn->prepare("select count(*) as res from ".Constants::prefix_db.Constants::dbtablemessages." where contenuto=:text");
- $stm->bindParam(":text", $text);
- $stm->execute();
- $res=$stm->fetch();
- echo "<br>sto confrontando $text </br>";
- return $res["res"];
- }
- public function getColumnName($tablename){
- $conn= $this->getConnection();
- $stm=$conn->prepare("select column_name from information_schema.columns where table_name = '".Constants::prefix_db.$tablename."' order by ordinal_position");
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function getColumnNameNoPrefix($tablename){
- $conn= $this->getConnection();
- $stm=$conn->prepare("select column_name from information_schema.columns where table_name = '".$tablename."' order by ordinal_position");
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function getTd($arr){
- echo "<tr>";
- foreach($arr as $cn){
- echo "<td>".($cn['column_name'])."</td>";
- }
- echo "</tr>";
- }
- public function countNumMessage($min, $max, $id){
- $conn= $this->getConnection();
- $query = "select count(*) as RES from ".Constants::prefix_db.Constants::dbtablemessages." where quando_scrive>$min and quando_scrive<$max and chi_scrive = $id ";
- $stm=$conn->prepare($query);
- $stm->execute();
- $res=$stm->fetch();
- //print_r($res);
- //echo "sto ritornando ".$res['RES']."<br>";
- if($id==Constants::page_id)
- return -1;
- return $res['RES'];
- }
- public function getNumMessagesRange($min, $max){
- $conn= $this->getConnection();
- $query = "select count(*) as RES from ".Constants::prefix_db.Constants::dbtablemessages." where quando_scrive>$min and quando_scrive<$max";
- $stm=$conn->prepare($query);
- $stm->execute();
- $res=$stm->fetch();
- return $res;
- }
- public function getListUsers_senzasesso($limit) {
- $conn= $this->getConnection();
- $query = "select * from ".Constants::prefix_db."utenti WHERE sesso_utente <> 'female' and sesso_utente <> 'male' and sesso_utente <> 'FAKE' and status > -1 ORDER BY id DESC ";
- $stm=$conn->prepare($query);
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function getListUsers($limit) {
- $conn= $this->getConnection();
- if($limit == 0)
- $query = "select ".Constants::prefix_db."utenti.stelle AS STELLUCCE, ".Constants::prefix_db."utenti.diamanti, ".Constants::prefix_db.Constants::dbtableusers.".id, ".Constants::prefix_db.Constants::dbtableusers.".status, max(".Constants::prefix_db.Constants::dbtablemessages.".quando_scrive) as MAXX, nome_utente, cognome_utente, sesso_utente from ".Constants::prefix_db.Constants::dbtableusers.", ".Constants::prefix_db.Constants::dbtablemessages." WHERE ".Constants::prefix_db.Constants::dbtableusers.".id = ".Constants::prefix_db.Constants::dbtablemessages.".chi_scrive GROUP BY ".Constants::prefix_db.Constants::dbtablemessages.".chi_scrive ORDER BY MAXX DESC";
- else
- $query = "select ".Constants::prefix_db."utenti.stelle AS STELLUCCE, ".Constants::prefix_db."utenti.diamanti, ".Constants::prefix_db.Constants::dbtableusers.".id, ".Constants::prefix_db.Constants::dbtableusers.".status, max(".Constants::prefix_db.Constants::dbtablemessages.".quando_scrive) as MAXX, nome_utente, cognome_utente, sesso_utente from ".Constants::prefix_db.Constants::dbtableusers.", ".Constants::prefix_db.Constants::dbtablemessages." WHERE ".Constants::prefix_db.Constants::dbtableusers.".id = ".Constants::prefix_db.Constants::dbtablemessages.".chi_scrive GROUP BY ".Constants::prefix_db.Constants::dbtablemessages.".chi_scrive ORDER BY MAXX DESC LIMIT 0, ".$limit;
- $stm=$conn->prepare($query);
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function getFake($limit) {
- $conn= $this->getConnection();
- $query = "select ".Constants::prefix_db.Constants::dbtableusers.".id, ".Constants::prefix_db.Constants::dbtableusers.".status, max(".Constants::prefix_db.Constants::dbtablemessages.".quando_scrive) as MAXX, nome_utente, sesso_utente from ".Constants::prefix_db.Constants::dbtableusers.", ".Constants::prefix_db.Constants::dbtablemessages." WHERE ".Constants::prefix_db.Constants::dbtableusers.".id = ".Constants::prefix_db.Constants::dbtablemessages.".chi_scrive and sesso_utente='FAKE'";
- $stm=$conn->prepare($query);
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function do_query($q) {
- $conn= $this->getConnection();
- $stm=$conn->prepare($q);
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function getListUsersNOTSEX($limit) {
- $conn= $this->getConnection();
- $query = "select ".Constants::prefix_db.Constants::dbtableusers.".id, ".Constants::prefix_db.Constants::dbtableusers.".status, max(".Constants::prefix_db.Constants::dbtablemessages.".quando_scrive) as MAXX, nome_utente, sesso_utente from ".Constants::prefix_db.Constants::dbtableusers.", ".Constants::prefix_db.Constants::dbtablemessages." WHERE ".Constants::prefix_db.Constants::dbtableusers.".id = ".Constants::prefix_db.Constants::dbtablemessages.".chi_scrive and sesso_utente <> 'male' AND sesso_utente <> 'female' and sesso_utente <> 'FAKE' GROUP BY ".Constants::prefix_db.Constants::dbtablemessages.".chi_scrive ORDER BY RAND() DESC LIMIT 0, ".$limit;
- $query =" select * from ".Constants::prefix_db."utenti where sesso_utente IS NULL ORDER BY RAND() DESC LIMIT 0, ".$limit;
- $stm=$conn->prepare($query);
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function getListUsersDaVotare($limit, $votante, $sex) {
- $conn= $this->getConnection();
- $query = "select ".Constants::prefix_db.Constants::dbtableusers.".id, ".Constants::prefix_db.Constants::dbtableusers.".status, nome_utente, sesso_utente from ".Constants::prefix_db.Constants::dbtableusers." WHERE sesso_utente = '".$sex."' AND ".Constants::prefix_db.Constants::dbtableusers.".id NOT IN (Select Votazioni.id_votata from Votazioni) ORDER BY RAND() DESC LIMIT 0, ".$limit;
- $stm=$conn->prepare($query);
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function getNoPrefix($tablename) {
- $conn= $this->getConnection();
- $stm=$conn->prepare("select * from ".$tablename);
- $stm->execute();
- $res=$stm->fetch();
- return $res;
- }
- public function getAll($tablename) {
- $conn= $this->getConnection();
- $stm=$conn->prepare("select * from ".Constants::prefix_db.$tablename);
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function insert($id, $b, $n) {
- if($id == NULL)
- return -1;
- $conn = $this->getConnection();
- $st = $conn->prepare("insert into ".Constants::prefix_db.Constants::dbtableusers." (id, potrebbe_servire, nome_utente, cognome_utente, sesso_utente, nickname_utente, date_nickname_utente, stelle, regione, caffe, diamanti) values (:id, :b, :n, '', '', '', '', '3', NULL, '0', '0');");
- $st->bindParam(":id", $id);
- $st->bindParam(":b", $b);
- $st->bindParam(":n", $n);
- $st->execute();
- return 1;
- }
- public function insert_message($id, $message) {
- $time = time();
- $conn = $this->getConnection();
- $st = $conn->prepare("insert into ".Constants::prefix_db.Constants::dbtablemessages." (chi_scrive, contenuto, quando_scrive) values (:id, :message, :time);");
- $st->bindParam(":id", $id);
- $st->bindParam(":time", $time );
- $st->bindParam(":message", $message);
- $st->execute();
- return 1;
- }
- public function getListPlugins() {
- $conn= $this->getConnection();
- //$stm=$conn->prepare("select * from ".Constants::prefix_db.Constants::dbtableplugins." ORDER BY h_activation");
- $stm=$conn->prepare("select * from ".Constants::prefix_db.Constants::dbtableplugins." ORDER BY h_activation");
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function getListPluginsCOMUNI($spotted) {
- $conn= $this->getConnection();
- //$stm=$conn->prepare("select * from ".Constants::prefix_db.Constants::dbtableplugins." ORDER BY h_activation");
- $stm=$conn->prepare("select * from ".comuni::dbtablepluginsCOMUNI." ORDER BY h_activation");
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function getConfig($key){
- $conn= $this->getConnection();
- $stm=$conn->prepare("select * from config where prefix_db=:key");
- $stm->bindParam("key", $key);
- $stm->execute();
- $res=$stm->fetchAll();
- return $res;
- }
- public function nuova_emoticon($emoticon, $descrizione) {
- $conn = $this->getConnection();
- $query = "INSERT INTO ".Constants::dbtableemoticon." VALUES (NULL, '".$emoticon."', '".$descrizione."');";
- $st = $conn->prepare($query);
- $st->execute();
- return 1;
- }
- public function insert_table_field($tablename, $value) {
- $conn = $this->getConnection();
- if($tablename == Constants::prefix_db.Constants::dbtableplugins)
- $query = "INSERT INTO ".$tablename." VALUES (NULL, '".$value."', '0', 'privata', '10', '1', '00', '0');";
- if($tablename == Constants::prefix_db.Constants::dbtablelists)
- $query = "INSERT INTO ".$tablename." VALUES (NULL, '".$value."', 'blacklist', '0');";
- if($tablename == Constants::prefix_db.Constants::dbtablewords)
- $query = "INSERT INTO ".$tablename." VALUES ('', '".$value."', '1');";
- $st = $conn->prepare($query);
- $st->execute();
- return 1;
- }
- public function update_table_field($tablename, $id, $field, $value) {
- $conn = $this->getConnection();
- if(($value=='NULL')or(strcmp($value, "NULL")==0)){
- $st = $conn->prepare("update ".$tablename." set ".$field."=NULL where id=:id;");
- }
- else{
- $st = $conn->prepare("update ".$tablename." set ".$field."=:value where id=:id;");
- $st->bindParam(":value", $value);
- }
- $st->bindParam(":id", $id);
- $st->execute();
- return 1;
- }
- public function get_table_field($tablename, $id, $field) {
- if($id==NULL)
- return 0;
- $conn = $this->getConnection();
- $query = "select $field from ".$tablename." where id=$id";
- $st = $conn->prepare($query);
- $st->execute();
- $res=$st->fetch();
- //print_r($res);
- return $res[$field];
- }
- public function get_emoticon($tablename, $id, $field) {
- $conn = $this->getConnection();
- $query = "select emoticon from ".$tablename." where descrizione='$id'";
- $st = $conn->prepare($query);
- $st->execute();
- $res=$st->fetch();
- //print_r($res);
- return $res['emoticon'];
- }
- public function deleteRecordById($tablename, $id) {
- $conn = $this->getConnection();
- $query = "DELETE FROM ".$tablename." where id=$id";
- $st = $conn->prepare($query);
- $st->execute();
- return 1;
- }
- public function generateEditForm($method, $action, $id, $idvalue, $field, $fieldvalue){
- return "<form method=\"$method\" action=\"$action\"><input type=\"text\" name=\"$id\" value=\"$idvalue\" style='display: none'><input type=\"text\" name=\"$field\" value=\"$fieldvalue\" ><input type=\"submit\" value=\"OK\"></form>";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement