about summary refs log tree commit diff stats
path: root/central/trunk/includes/instadisc.php
blob: 99d06b1673f616ba0f2be9696d96b41cf643258c (plain) (blame)
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
175
176
177
178
179
180
181
182
<?php

/* InstaDisc Server - A Four Island Project */

include_once('includes/db.php');

function instaDisc_checkVerification($username, $verification, $verificationID, $table, $nameField, $passField)
{
	$getverid = "SELECT * FROM oldVerID WHERE username = \"" . mysql_real_escape_string($username) . "\" AND verID = " . $verificationID;
	$getverid2 = mysql_query($getverid);
	$getverid3 = mysql_fetch_array($getverid2);
	if ($getverid3['id'] != $verificationID)
	{
		$getitem = "SELECT * FROM " . $table . " WHERE " . $nameField . " = \"" . mysql_real_escape_string($username) . "\"";
		$getitem2 = mysql_query($getitem);
		$getitem3 = mysql_fetch_array($getitem2);
		if ($getitem3[$nameField] == $username)
		{
			$test = $username . ':' . $getitem3[$passField] . ':' . $verificationID;

			if (md5($test) == $verification)
			{
				$cntverid = "SELECT COUNT(*) FROM oldVerID WHERE username = \"" . mysql_real_escape_string($username) . "\"";
				$cntverid2 = mysql_query($cntverid);
				$cntverid3 = mysql_fetch_array($cntverid2);
				if ($cntverid3[0] >= intval(instaDisc_getConfig('verIDBufferSize')))
				{
					$delverid = "DELETE FROM oldVerID WHERE username = \"" . mysql_real_escape_string($username) . "\" LIMIT 0,1";
					$delverid2 = mysql_query($delverid);
				}

				$insverid = "INSERT INTO oldVerID (username, verID) VALUES (\"" . mysql_real_escape_string($username) . "\", " . $verificationID . ")";
				$insverid2 = mysql_query($insverid);

				if (($table == 'users') && ($getitem3['ip'] != $_SERVER['REMOTE_ADDR']))
				{
					$setuser = "UPDATE users SET ip = \"" . $_SERVER['REMOTE_ADDR'] . "\" WHERE id = " . $getitem3['id'];
					$setuser2 = mysql_query($setuser);
				}

				return true;
			}
		}
	}

	return false;
}

function instaDisc_sendItem($username, $id)
{
	$getitem = "SELECT * FROM inbox WHERE username = \"" . mysql_real_escape_string($username) . "\" AND itemID = " . $id;
	$getitem2 = mysql_query($getitem);
	$getitem3 = mysql_fetch_array($getitem2);
	if ($getitem3['username'] == $username)
	{
		$getuser = "SELECT * FROM users WHERE username = \"" . mysql_real_escape_string($username) . "\"";
		$getuser2 = mysql_query($getuser);
		$getuser3 = mysql_fetch_array($getuser2);

		if (($getuser3['downloadItemMode'] == 'Push') && ($getuser3['port'] != 0))
		{
			$fp = @fsockopen($getuser3['ip'], $getuser3['port'], $errno, $errstr);
			if ($fp)
			{
				$title = str_replace(': ', '__INSTADISC__', $getitem3['title']);

				$out = instaDisc_formItem($username, $id, "\r\n") . "\r\n\r\n";

				fwrite($fp, $out);
				fclose($fp);

				return true;
			} else {
				return false;
			}
		}
	}
}

function instaDisc_addItem($username, $subscription, $title, $author, $url, $semantics, $encryptionID = 0)
{
	$getuser = "SELECT * FROM users WHERE username = \"" . mysql_real_escape_string($username) . "\"";
	$getuser2 = mysql_query($getuser);
	$getuser3 = mysql_fetch_array($getuser2);
	if ($getuser3['username'] == $username)
	{
		$itemID = $getuser3['nextItemID'];
		$setuser = "UPDATE users SET nextItemID = nextItemID+1 WHERE username = \"" . mysql_real_escape_string($username) . "\"";
		$setuser2 = mysql_query($setuser);

		$insitem = "INSERT INTO inbox (username, itemID, subscription, title, author, url, semantics, encryptionID) VALUES (\"" . mysql_real_escape_string($username) . "\", " . $itemID . ", \"" . mysql_real_escape_string($subscription) . "\", \"" . mysql_real_escape_string($title) . "\", \"" . mysql_real_escape_string($author) . "\", \"" . mysql_real_escape_string($url) . "\", \"" . mysql_real_escape_string($semantics) . "\"," . $encryptionID . ")";
		$insitem2 = mysql_query($insitem);

		if ($getuser3['downloadItemMode'] == 'Push')
		{
			instaDisc_sendItem($username, $itemID);
		}
	}
}

function instaDisc_createUser($username, $password)
{
	$insuser = "INSERT INTO users (username, password) VALUES (\"" . mysql_real_escape_string($username) . "\", \"" . mysql_real_escape_string($password) . "\")";
	$insuser2 = mysql_query($insuser);
}

function instaDisc_getConfig($key)
{
	$getconfig = "SELECT * FROM config WHERE name = \"" . mysql_real_escape_string($key) . "\"";
	$getconfig2 = mysql_query($getconfig);
	$getconfig3 = mysql_fetch_array($getconfig2);

	return $getconfig3['value'];
}

function instaDisc_changePassword($username, $password)
{
	$setpass = "UPDATE users WHERE username = \"" . mysql_real_escape_string($username) . "\" SET password = \"" . mysql_real_escape_string(md5($password)) . "\"";
	$setpass2 = mysql_query($setpass);
}

function instaDisc_initalizePort($username)
{
	$getports = "SELECT * FROM users WHERE ip = \"" . mysql_real_escape_string($username) . "\" AND port <> 0 ORDER BY port ASC";
	$getports2 = mysql_query($getports);
	$i=0;
	while ($getports3[$i] = mysql_fetch_array($getports2))
	{
		$i++;
	}

	if ($i==0)
	{
		$port = 1204;
	} else if ($i>=4331)
	{
		return 0;
	} else {
		$port = (61204 + ($i-1));
	}

	$setuser = "UPDATE users SET port = " . $port . " WHERE username = \"" . mysql_real_escape_string($username) . "\"";
	$setuser2 = mysql_query($setuser);

	return $port;
}

function instaDisc_formItem($username, $id, $ln = "\n")
{
	$getitem = "SELECT * FROM inbox WHERE username = \"" . mysql_real_escape_string($username) . "\" AND itemID = " . $id;
	$getitem2 = mysql_query($getitem);
	$getitem3 = mysql_fetch_array($getitem2);
	if ($getitem3['username'] == $username)
	{
		$getuser = "SELECT * FROM users WHERE username = \"" . mysql_real_escape_string($username) . "\"";
		$getuser2 = mysql_query($getuser);
		$getuser3 = mysql_fetch_array($getuser2);

		$verID = rand(1,2147483647);

		$out = 'ID: ' . $id . $ln;
		$out .= 'Verification: ' . md5($username . ':' . $getuser3['password'] . ':' . $verID) . $ln;
		$out .= 'Verification-ID: ' . $verID . $ln;
		$out .= 'Subscription: ' . $getitem3['subscription'] . $ln;
		$out .= 'Title: ' . $title . $ln;
		$out .= 'Author: ' . $getitem3['author'] . $ln;
		$out .= 'URL: ' . $getitem3['url'] . $ln;

		$semantics = unserialize($getitem3['semantics']);
		foreach ($semantics as $name => $value)
		{
			$value = str_replace(': ', '__INSTADISC__', $value);
			$out .= $name . ': ' . $value . $ln;
		}
		if ($getitem3['encryptionID'] != 0)
		{
			$out .= 'Encryption-ID: ' . $getitem3['encryptionID'] . $ln;
		}
	}
}

?>