1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
<?php
/*
* (C) 2010 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for furhter details.
*
*
*/
/** Object representing a MySQL query
*/
class MySQLQuery
{
/* Our query */
private $Query;
/* The result */
private $Result;
/* Socket used to connect to MySQL */
private $MysqlSock;
/** Constructor
* @param MysqlSock The MySQL socket
*/
function __construct($MysqlSock)
{
$this->MysqlSock = $MysqlSock;
}
/** Destructor
*/
function __destruct()
{
}
/** Execute a query
* @return true or false
*/
private function Execute()
{
$Res = mysql_query($this->Query, $this->MysqlSock);
$this->Result = array();
if (!empty($Res))
{
while (($Result = @mysql_fetch_assoc($Res)))
{
$this->Result[] = $Result;
}
return true;
}
return false;
}
/** Get the result for the query
* @return The result
*/
public function Result()
{
return $this->Result;
}
/** Execute a query
* @param A formatted string
* @param ... Args
* @return true or false
*/
public function Query($String, $P1 = NULL, $P2 = NULL, $P3 = NULL, $P4 = NULL, $P5 = NULL, $P6 = NULL, $P7 = NULL,
$P8 = NULL, $P9 = NULL, $P10 = NULL, $P11 = NULL, $P12 = NULL, $P13 = NULL, $P14 = NULL)
{
$this->Query = sprintf($String, $P1, $P2, $P3, $P4, $P5, $P6, $P7, $P8, $P9, $P10, $P11, $P12, $P13, $P14);
return $this->Execute();
}
/** Escape a string to by MySQL safe
* @return A new, MySQL safe string
*/
public function Escape($String)
{
return mysql_real_escape_string($String, $this->MysqlSock);
}
}
/** Main Anope class
*/
class Anope
{
/* Socket used to connect to MySQL */
private $MysqlSock;
/* True if we were able to connect successfully */
private $Connected;
/** Constructor
* @param MysqlHost The host of the MySQLd server, port can be included on this too
* @param MysqlUser The username to authenticate to MySQL with
* @param MysqlPassword The password to authenticate with
* @param MysqlDatabase The name of the Anope database
*/
function __construct($MysqlHost, $MysqlUser, $MysqlPassword, $MysqlDatabase)
{
$this->Connected = false;
$this->MysqlSock = @mysql_connect($MysqlHost, $MysqlUser, $MysqlPassword);
if ($this->MysqlSock)
$this->Connected = @mysql_select_db($MysqlDatabase, $this->MysqlSock);
}
/** Destructor
* Closes the connection to the MySQL server
*/
function __destruct()
{
if ($this->MysqlSock)
@mysql_close($this->MysqlSock);
}
/** Check if we are connected successfully
* @return true or false
*/
public function Connected()
{
return $this->Connected;
}
/** Retrieve a new query object
* @return A new Query object
*/
public function Query()
{
return new MySQLQuery($this->MysqlSock);
}
/** Anope Functions **/
/** Execute a command in Anope
* For more information read docs/MYSQL
* @param Nick The nickname to execute the command from
* @param Service The service to execute the command on
* @param Command The command to execute
*/
public function Command($Nick, $Service, $Command)
{
$Query = $this->Query();
return $Query->Query("INSERT DELAYED INTO `anope_commands` (nick, service, command) VALUES('%s', '%s', '%s')", $Query->Escape($Nick), $Query->Escape($Service), $Query->Escape($Command));
}
/** Register a nick
* @param Nick The nick to be registered
* @param Password The password
* @param Email The email address to use, defaults to NULL
* @param Returns a message confirming or denying the registration process
*/
public function Register($Nick, $Password, $Email = NULL)
{
$Query = $this->Query();
$Query->Query("SELECT nick FROM `anope_ns_alias` WHERE `nick` = '%s'", $Query->Escape($Nick));
$Result = $Query->Result();
if (isset($Result[0]['nick']))
{
return "Nickname already registered";
}
if ($this->Command($Nick, "NickServ", "REGISTER ".$Password." ".$Email))
{
return "Nick registration successful. If your network has email registration enabled check your inbox for the next step of the registration process.";
}
return "Error registering nick";
}
}
?>
|