Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class m_mysql extends Module
- {
- private $m_sNick;
- private $conn;
- private $sqlInfo = array(
- 'host' => 'localhost',
- 'user' => 'VIKI',
- 'pass' => 'dlkjsdkjdskjfsdjfksdlkfjd',
- 'db' => 'ksdjfkjsdhfkjhskdhfjsdf'
- );
- public function __construct($sNick)
- {
- $this -> m_sNick = $sNick;
- $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.');
- }
- public function __destruct( )
- {
- }
- public function onPrivMsg( $sFrom, $sMsg )
- {
- }
- public function onPrivNotice( $sFrom, $sMsg )
- {
- // echo $sFrom . '<=N ' . $sMsg . chr( 10 );
- }
- public function onMsg( $sFrom, $sTo, $sMsg, $sSpecial = null )
- {
- $sNick = $this->m_sNick;
- $oBot = Bots::obj()->getBot($sNick);
- $aMsg = explode(' ', $sMsg);
- $sCmd = strtolower($aMsg[0]);
- if($sCmd == '@#testcon') {
- $query = 'SELECT id, name, password FROM users WHERE id = 1';
- if($stmt = $this->conn->prepare($query)) {
- $stmt->execute();
- $stmt->bind_result($id, $name, $password);
- $users = array();
- while($row = $stmt->fetch()) {
- $users['id'] = $id;
- $users['name'] = $name;
- $users['password'] = $password;
- }
- $stmt->close();
- $oBot->msg($oBot['in']->Channel, $users['id'] .'|'. $users['name'] .'|'. $users['password']);
- }
- }
- else if($sCmd == '@#listusers') {
- $query = 'SELECT id, name, password FROM users ORDER BY id ASC';
- if($stmt = $this->conn->prepare($query)) {
- $stmt->execute();
- $stmt->bind_result($id, $name, $password);
- $users = array();
- while($row = $stmt->fetch()) {
- $users[$id]['id'] = $id;
- $users[$id]['name'] = $name;
- $users[$id]['password'] = $password;
- }
- $stmt->close();
- $oBot->msg($oBot['in']->Channel, '----| User List |----');
- foreach($users as $user) {
- $oBot->msg($oBot['in']->Channel, $user['id'] .'|'. $user['name'] .'|'. $user['password']);
- }
- }
- }
- else if($sCmd == '@#adduser') {
- if(!isset($aMsg[1]) || !isset($aMsg[2])) {
- return $oBot->msg($oBot['in']->Channel, CMD_ERROR.' USAGE: @#adduser [username] [password]');
- } else {
- $username = $aMsg[1];
- $password = hash('whirlpool', $aMsg[2]);
- $query = 'INSERT INTO users VALUES (NULL, ?, ?)';
- if($stmt = $this->conn->prepare($query)) {
- $stmt->bind_param('ss', $username, $password);
- if($stmt->execute()) {
- $oBot->msg($oBot['in']->Channel, CMD_SUCCESS.' User added successfully!');
- $stmt->close();
- } else {
- $oBot->msg($oBot['in']->Channel, CMD_ERROR.' User not added successfully.');
- $stmt->close();
- }
- }
- }
- }
- else if($sCmd == '@#remuser') {
- if($aMsg[1] == NULL) {
- return $oBot->msg($oBot['in']->Channel, CMD_ERROR.' USAGE: @#remuser [username]');
- } else {
- $username = $aMsg[1];
- if(!$this->isValidUser($username)) {
- return $oBot->msg($oBot['in']->Channel, CMD_ERROR.' Invalid Username.');
- } else {
- $query = "DELETE FROM users WHERE name = ?";
- if($stmt = $this->conn->prepare($query)) {
- $stmt->bind_param('s', $username);
- if($stmt->execute()) {
- $oBot->msg($oBot['in']->Channel, CMD_SUCCESS.' User removed successfully!');
- $stmt->close();
- } else {
- $oBot->msg($oBot['in']->Channel, CMD_ERROR.' User not removed successfully.');
- $stmt->close();
- }
- }
- }
- }
- }
- }
- public function isValidUser($username) {
- $query = "SELECT id FROM users WHERE name = ?";
- if($stmt = $this->conn->prepare($query)) {
- $stmt->bind_param('s', $username);
- $stmt->execute();
- $stmt->store_result();
- if($stmt->num_rows <= 0) {
- $stmt->close();
- return false;
- } else {
- $stmt->close();
- return true;
- }
- } else {
- return false;
- }
- }
- public function __toString( )
- {
- return 'MySQL Test Module';
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement