Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.47 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5. class m_mysql extends Module
  6.  
  7. {
  8.  
  9.  
  10.  
  11.     private $m_sNick;
  12.  
  13.  
  14.  
  15.     private $conn;
  16.  
  17.  
  18.  
  19.     private $sqlInfo = array(
  20.  
  21.  
  22.  
  23.         'host' => 'localhost',
  24.  
  25.         'user' => 'VIKI',
  26.  
  27.         'pass' => 'dlkjsdkjdskjfsdjfksdlkfjd',
  28.  
  29.         'db'   => 'ksdjfkjsdhfkjhskdhfjsdf'
  30.  
  31.  
  32.  
  33.     );
  34.  
  35.  
  36.  
  37.  
  38.  
  39.     public function __construct($sNick)
  40.  
  41.     {
  42.  
  43.         $this -> m_sNick = $sNick;
  44.  
  45.  
  46.  
  47.         $this->conn = new mysqli($this->sqlInfo['host'], $this->sqlInfo['user'], $this->sqlInfo['pass'], $this->sqlInfo['db']) or die('There was a problem connecting to the database.');
  48.  
  49.     }
  50.  
  51.  
  52.  
  53.     public function __destruct( )
  54.  
  55.     {
  56.  
  57.     }
  58.  
  59.    
  60.  
  61.     public function onPrivMsg( $sFrom, $sMsg )
  62.  
  63.     {
  64.  
  65.     }
  66.  
  67.  
  68.  
  69.     public function onPrivNotice( $sFrom, $sMsg )
  70.  
  71.     {
  72.  
  73.             // echo $sFrom . '<=N ' . $sMsg . chr( 10 );
  74.  
  75.     }
  76.  
  77.  
  78.  
  79.     public function onMsg( $sFrom, $sTo, $sMsg, $sSpecial = null )
  80.  
  81.     {
  82.  
  83.         $sNick = $this->m_sNick;
  84.  
  85.         $oBot = Bots::obj()->getBot($sNick);
  86.  
  87.  
  88.  
  89.         $aMsg = explode(' ', $sMsg);
  90.  
  91.  
  92.  
  93.         $sCmd = strtolower($aMsg[0]);
  94.  
  95.  
  96.  
  97.         if($sCmd == '@#testcon') {
  98.  
  99.            $query = 'SELECT id, name, password FROM users WHERE id = 1';
  100.  
  101.  
  102.  
  103.             if($stmt = $this->conn->prepare($query)) {
  104.  
  105.                 $stmt->execute();
  106.  
  107.                 $stmt->bind_result($id, $name, $password);
  108.  
  109.  
  110.  
  111.                 $users = array();
  112.  
  113.  
  114.  
  115.                 while($row = $stmt->fetch()) {
  116.  
  117.                     $users['id'] = $id;
  118.  
  119.                     $users['name'] = $name;
  120.  
  121.                     $users['password'] = $password;
  122.  
  123.                 }
  124.  
  125.  
  126.  
  127.                 $stmt->close();
  128.  
  129.  
  130.  
  131.                 $oBot->msg($oBot['in']->Channel, $users['id'] .'|'. $users['name'] .'|'. $users['password']);
  132.  
  133.             }
  134.  
  135.         }
  136.  
  137.         else if($sCmd == '@#listusers') {
  138.  
  139.             $query = 'SELECT id, name, password FROM users ORDER BY id ASC';
  140.  
  141.  
  142.  
  143.              if($stmt = $this->conn->prepare($query)) {
  144.  
  145.                 $stmt->execute();
  146.  
  147.                 $stmt->bind_result($id, $name, $password);
  148.  
  149.  
  150.  
  151.                 $users = array();
  152.  
  153.  
  154.  
  155.                 while($row = $stmt->fetch()) {
  156.  
  157.                     $users[$id]['id'] = $id;
  158.  
  159.                     $users[$id]['name'] = $name;
  160.  
  161.                     $users[$id]['password'] = $password;
  162.  
  163.                 }
  164.  
  165.  
  166.  
  167.                 $stmt->close();
  168.  
  169.  
  170.  
  171.                 $oBot->msg($oBot['in']->Channel, '----| User List |----');
  172.  
  173.                 foreach($users as $user) {
  174.  
  175.                     $oBot->msg($oBot['in']->Channel, $user['id'] .'|'. $user['name'] .'|'. $user['password']);
  176.  
  177.                 }
  178.  
  179.             }
  180.  
  181.         }
  182.  
  183.         else if($sCmd == '@#adduser') {
  184.  
  185.             if(!isset($aMsg[1]) || !isset($aMsg[2])) {
  186.  
  187.                 return $oBot->msg($oBot['in']->Channel, CMD_ERROR.' USAGE: @#adduser [username] [password]');
  188.  
  189.             } else {
  190.  
  191.  
  192.  
  193.                 $username = $aMsg[1];
  194.  
  195.                 $password = hash('whirlpool', $aMsg[2]);
  196.  
  197.  
  198.  
  199.                 $query = 'INSERT INTO users VALUES (NULL, ?, ?)';
  200.  
  201.  
  202.  
  203.                 if($stmt = $this->conn->prepare($query)) {
  204.  
  205.                     $stmt->bind_param('ss', $username, $password);
  206.  
  207.                     if($stmt->execute()) {
  208.  
  209.                         $oBot->msg($oBot['in']->Channel, CMD_SUCCESS.' User added successfully!');
  210.  
  211.                         $stmt->close();
  212.  
  213.                     } else {
  214.  
  215.                         $oBot->msg($oBot['in']->Channel, CMD_ERROR.' User not added successfully.');
  216.  
  217.                         $stmt->close();
  218.  
  219.                     }
  220.  
  221.                 }
  222.  
  223.             }
  224.  
  225.         }
  226.  
  227.         else if($sCmd == '@#remuser') {
  228.  
  229.             if($aMsg[1] == NULL) {
  230.  
  231.                 return $oBot->msg($oBot['in']->Channel, CMD_ERROR.' USAGE: @#remuser [username]');
  232.  
  233.             } else {
  234.  
  235.                 $username = $aMsg[1];
  236.  
  237.  
  238.  
  239.                 if(!$this->isValidUser($username)) {
  240.  
  241.                     return $oBot->msg($oBot['in']->Channel, CMD_ERROR.' Invalid Username.');
  242.  
  243.                 } else {
  244.  
  245.  
  246.  
  247.                     $query = "DELETE FROM users WHERE name = ?";
  248.  
  249.  
  250.  
  251.                     if($stmt = $this->conn->prepare($query)) {
  252.  
  253.                         $stmt->bind_param('s', $username);
  254.  
  255.                         if($stmt->execute()) {
  256.  
  257.                             $oBot->msg($oBot['in']->Channel, CMD_SUCCESS.' User removed successfully!');
  258.  
  259.                             $stmt->close();
  260.  
  261.                         } else {
  262.  
  263.                             $oBot->msg($oBot['in']->Channel, CMD_ERROR.' User not removed successfully.');
  264.  
  265.                             $stmt->close();
  266.  
  267.                         }
  268.  
  269.                     }
  270.  
  271.                 }
  272.  
  273.             }
  274.  
  275.         }
  276.  
  277.     }
  278.  
  279.     public function isValidUser($username) {
  280.  
  281.  
  282.  
  283.         $query = "SELECT id FROM users WHERE name = ?";
  284.  
  285.  
  286.  
  287.         if($stmt = $this->conn->prepare($query)) {
  288.  
  289.             $stmt->bind_param('s', $username);
  290.  
  291.             $stmt->execute();
  292.  
  293.             $stmt->store_result();
  294.  
  295.  
  296.  
  297.             if($stmt->num_rows <= 0) {
  298.  
  299.                 $stmt->close();
  300.  
  301.                 return false;
  302.  
  303.             } else {
  304.  
  305.                 $stmt->close();
  306.  
  307.                 return true;
  308.  
  309.             }
  310.  
  311.         } else {
  312.  
  313.             return false;
  314.  
  315.         }
  316.  
  317.     }
  318.  
  319.  
  320.  
  321.     public function __toString( )
  322.  
  323.     {
  324.  
  325.         return 'MySQL Test Module';
  326.  
  327.     }
  328.  
  329. }
  330.  
  331.  
  332.  
  333. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement