· 9 years ago · Nov 03, 2016, 12:16 AM
1<?php
2
3define('CLOG_SOURCE_GATE', 'gate');
4define('CLOG_SOURCE_REPORT', 'report');
5define('CLOG_SOURCE_LOGIN', 'login');
6define('CPONY_FTP_TABLE', 'pony_ftp');
7define('CPONY_REPORT_TABLE', 'pony_report');
8define('CPONY_REPORT_DATA_TABLE', 'pony_report_data');
9define('CPONY_DOMAIN_TABLE', 'pony_domain');
10define('CPONY_LOG_TABLE', 'pony_system_log');
11define('CPONY_USER_TABLE', 'pony_user');
12define('CPONY_CERT_TABLE', 'pony_cert');
13define('CPONY_EMAIL_TABLE', 'pony_email');
14
15class pony_db
16{
17 public $db_link;
18 protected $database;
19 public $state;
20 public $privileges;
21 public $auth_cookie;
22 public $user_id;
23 public $login;
24
25 function __construct()
26 {
27 $this->state = true;
28 $this->db_link = null;
29 $this->privileges = '';
30 }
31
32 function connect($host, $user, $pass)
33 {
34 // establish the connection
35 $this->db_link = mysql_connect($host, $user, $pass, true);
36
37 if (!$this->db_link)
38 {
39 $this->state = false;
40 return false;
41 }
42
43 return true;
44 }
45
46 function select_db($database)
47 {
48 if (!$this->state)
49 return false;
50
51 $select_result = mysql_select_db($database, $this->db_link);
52
53 if (!$select_result)
54 {
55 $select_result = mysql_query(sprintf('CREATE DATABASE IF NOT EXISTS %s CHARACTER SET cp1251 COLLATE cp1251_general_ci', mysql_real_escape_string($database)), $this->db_link);
56 $select_result = $select_result && mysql_select_db($database, $this->db_link);
57 }
58 $this->state = $select_result;
59 $this->state = $this->state && mysql_query('SET NAMES cp1251', $this->db_link);
60
61 $this->database = $database;
62 return $this->state;
63 }
64
65 var $required_tables = array('pony_user', 'pony_ftp', 'pony_report', 'pony_report_data', 'pony_system_log', 'pony_domain', 'pony_chart_helper', 'pony_cert', 'pony_email');
66
67 function all_tables_exist()
68 {
69 if (!$this->state)
70 return false;
71
72 $result = mysql_query("SHOW TABLES", $this->db_link);
73 if (!$result)
74 {
75 $this->state = false;
76 return false;
77 }
78
79 $actual_tables = array();
80
81
82 while ($row = mysql_fetch_assoc($result))
83 {
84 foreach ($row as $table)
85 {
86 array_push($actual_tables, $table);
87 }
88 }
89
90 $diff = array_diff($this->required_tables, $actual_tables);
91
92 // some required tables found
93 if (count($diff) != 0)
94 {
95 return false;
96 }
97
98 return true;
99 }
100
101 function some_tables_exist()
102 {
103 if (!$this->state)
104 return false;
105
106 $result = mysql_query("SHOW TABLES", $this->db_link);
107 if (!$result)
108 {
109 $this->state = false;
110 return false;
111 }
112
113 $actual_tables = array();
114
115 while ($row = mysql_fetch_assoc($result))
116 {
117 foreach ($row as $table)
118 {
119 array_push($actual_tables, $table);
120 }
121 }
122
123 $diff = array_intersect($this->required_tables, $actual_tables);
124
125 // some required tables found
126 if (count($diff) != 0)
127 {
128 return true;
129 }
130
131 return false;
132 }
133
134 function delete_tables()
135 {
136 if (!$this->state)
137 return false;
138
139 // some required tables found
140 foreach ($this->required_tables as $table_name)
141 {
142 $this->drop_table($table_name);
143 }
144
145 return $this->state;
146 }
147
148 function upgrade()
149 {
150 if (!$this->state)
151 return false;
152
153 $result = mysql_query("SHOW TABLES", $this->db_link);
154 if (!$result)
155 {
156 $this->state = false;
157 return false;
158 }
159
160 $actual_tables = array();
161
162 while ($row = mysql_fetch_assoc($result))
163 {
164 foreach ($row as $table)
165 {
166 array_push($actual_tables, $table);
167 }
168 }
169
170 if (!count($actual_tables))
171 return false;
172
173 if (array_search(CPONY_CERT_TABLE, $actual_tables) === false)
174 {
175 $this->create_data_tables();
176 }
177
178 if (array_search(CPONY_EMAIL_TABLE, $actual_tables) === false)
179 {
180 $this->create_data_tables();
181 }
182 }
183
184 function connect_db($host, $user, $pass, $database, $verbose = false)
185 {
186 if (!$this->connect($host, $user, $pass))
187 {
188 if ($verbose)
189 die('cannot connect to mysql database');
190 else
191 die();
192 }
193
194 if (!$this->select_db($database))
195 {
196 if ($verbose)
197 die('cannot select mysql database');
198 else
199 die();
200 }
201
202 $this->upgrade();
203 $tables_exist = $this->all_tables_exist();
204
205 if (!$this->state)
206 {
207 if ($verbose)
208 die('mysql database error');
209 else
210 die();
211 }
212
213 if (!$tables_exist)
214 {
215 if ($verbose)
216 die ('missing required mysql database tables');
217 else
218 die();
219 }
220
221 return true;
222 }
223
224 // close connection
225 function close()
226 {
227 // check connection
228 if ($this->db_link)
229 {
230 mysql_close($this->db_link);
231 $this->state = false;
232 }
233 }
234
235 function create_data_tables()
236 {
237 $result = mysql_query("
238 CREATE TABLE IF NOT EXISTS pony_report
239 (
240 report_id INT NOT NULL AUTO_INCREMENT,
241 PRIMARY KEY(report_id),
242 parsed BOOL DEFAULT FALSE,
243 import_time DATETIME,
244 KEY(import_time),
245 report_os_name VARCHAR(150),
246 report_country CHAR(2),
247 report_is_win64 BOOL,
248 report_admin BOOL,
249 report_source_ip CHAR(15),
250 report_hwid CHAR(40),
251 report_version VARCHAR(10),
252 data_id INT NOT NULL,
253 KEY(data_id)
254 )
255 CHARACTER SET cp1251
256 COLLATE cp1251_general_ci
257 ENGINE = MYISAM
258 ", $this->db_link);
259
260 $result = $result && mysql_query("
261 CREATE TABLE IF NOT EXISTS pony_report_data
262 (
263 data_id INT NOT NULL AUTO_INCREMENT,
264 PRIMARY KEY(data_id),
265 data_hash CHAR(40) NOT NULL,
266 UNIQUE KEY (data_hash),
267 data LONGBLOB
268 )
269 CHARACTER SET cp1251
270 COLLATE cp1251_general_ci
271 ENGINE = MYISAM
272 ", $this->db_link);
273
274 $result = $result && mysql_query("
275 CREATE TABLE IF NOT EXISTS pony_ftp
276 (
277 ftp_id INT NOT NULL AUTO_INCREMENT,
278 PRIMARY KEY(ftp_id),
279 report_id INT NOT NULL,
280 KEY(report_id),
281 url TEXT NOT NULL,
282 url_hash CHAR(40) NOT NULL,
283 UNIQUE KEY(url_hash),
284 url_type ENUM('ftp', 'ssh', 'http', 'https', 'rdp', 'vnc') NOT NULL,
285 KEY(url_type),
286 ftp_client VARCHAR(50),
287 import_time DATETIME,
288 KEY(import_time)
289 )
290 CHARACTER SET cp1251
291 COLLATE cp1251_general_ci
292 ENGINE = MYISAM
293 ", $this->db_link);
294
295 $result = $result && mysql_query(sprintf("
296 CREATE TABLE IF NOT EXISTS pony_system_log
297 (
298 log_id INT NOT NULL AUTO_INCREMENT,
299 PRIMARY KEY(log_id),
300 report_id INT DEFAULT NULL,
301 KEY(report_id),
302 log_line VARCHAR(250),
303 log_source ENUM('%s', '%s', '%s') NOT NULL,
304 log_type ENUM('notify', 'error', 'other') NOT NULL,
305 log_extra VARCHAR(250),
306 import_time DATETIME
307 )
308 CHARACTER SET cp1251
309 COLLATE cp1251_general_ci
310 ENGINE = MYISAM
311 ", CLOG_SOURCE_GATE, CLOG_SOURCE_REPORT, CLOG_SOURCE_LOGIN),
312 $this->db_link);
313
314 $result = $result && mysql_query("
315 CREATE TABLE IF NOT EXISTS pony_cert
316 (
317 cert_id INT NOT NULL AUTO_INCREMENT,
318 PRIMARY KEY(cert_id),
319 report_id INT NOT NULL,
320 KEY(report_id),
321 import_time DATETIME,
322 KEY(import_time),
323 cert_client VARCHAR(50),
324 data_hash CHAR(40) NOT NULL,
325 UNIQUE KEY(data_hash),
326 cert_data LONGBLOB,
327 pvtkey_data LONGBLOB
328 )
329 CHARACTER SET cp1251
330 COLLATE cp1251_general_ci
331 ENGINE = MYISAM
332 ",
333 $this->db_link);
334
335 $result = $result && mysql_query("
336 CREATE TABLE IF NOT EXISTS pony_email
337 (
338 email_id INT NOT NULL AUTO_INCREMENT,
339 PRIMARY KEY(email_id),
340 report_id INT NOT NULL,
341 KEY(report_id),
342 import_time DATETIME,
343 KEY(import_time),
344 email_client VARCHAR(50),
345 email_hash CHAR(40) NOT NULL,
346 UNIQUE KEY(email_hash),
347 protocol ENUM('smtp', 'imap', 'nntp', 'http', 'pop3', 'other') NOT NULL,
348 email VARCHAR(250) NOT NULL,
349 server VARCHAR(250) NOT NULL,
350 port INT DEFAULT '0',
351 user VARCHAR(250) NOT NULL,
352 pass VARCHAR(250) NOT NULL
353 )
354 CHARACTER SET cp1251
355 COLLATE cp1251_general_ci
356 ENGINE = MYISAM
357 ",
358 $this->db_link);
359
360
361 return $result;
362 }
363
364 function create_tables()
365 {
366 if (!$this->state)
367 return false;
368
369 $result = mysql_query("
370 CREATE TABLE pony_user
371 (
372 user_id INT NOT NULL AUTO_INCREMENT,
373 PRIMARY KEY(user_id),
374 username VARCHAR(100) NOT NULL UNIQUE,
375 password VARCHAR(50) NOT NULL,
376 privileges VARCHAR(200) NOT NULL,
377 auth_cookie VARCHAR(50) DEFAULT NULL,
378 lang VARCHAR(50) NULL,
379 settings TEXT,
380 time_offset INT
381 )
382 CHARACTER SET cp1251
383 COLLATE cp1251_general_ci
384 ENGINE = MYISAM
385 ", $this->db_link);
386
387 $result = $result && $this->create_data_tables();
388
389 $result = $result && mysql_query("
390 CREATE TABLE pony_domain
391 (
392 domain_id INT NOT NULL AUTO_INCREMENT,
393 PRIMARY KEY(domain_id),
394 url TEXT,
395 url_hash CHAR(40) NOT NULL UNIQUE,
396 ping_status VARCHAR(100),
397 ping_time DATETIME,
398 import_time DATETIME
399 )
400 CHARACTER SET cp1251
401 COLLATE cp1251_general_ci
402 ENGINE = MYISAM
403 ", $this->db_link);
404
405 $result = $result && mysql_query("
406 CREATE TABLE pony_chart_helper
407 (
408 chart_id INT NOT NULL AUTO_INCREMENT,
409 PRIMARY KEY(chart_id),
410 chart_type VARCHAR(50),
411 update_time DATETIME,
412 image_hash CHAR(40),
413 KEY(image_hash)
414 )
415 CHARACTER SET cp1251
416 COLLATE cp1251_general_ci
417 ENGINE = MYISAM
418 ", $this->db_link);
419
420 if (!$result)
421 {
422 $this->state = false;
423 }
424
425 return $result;
426 }
427
428 function priv_is_admin()
429 {
430 return $this->privileges == 'admin_all';
431 }
432
433 function priv_can_delete()
434 {
435 return strpos($this->privileges, 'all') !== false;
436 }
437
438 function priv_is_user($privileges)
439 {
440 return strpos($privileges, 'user') !== false;
441 }
442
443 function domain_exists($url_hash)
444 {
445 $url_hash = trim($url_hash);
446
447 if (!$this->state || !strlen($url_hash))
448 return false;
449
450 $query = sprintf("SELECT domain_id FROM pony_domain WHERE (url_hash='%s') LIMIT 1",
451 mysql_real_escape_string($url_hash));
452
453 $result = mysql_query($query, $this->db_link);
454
455 if (!$result)
456 {
457 $this->state = false;
458 } else
459 {
460 if (mysql_num_rows($result) == 1)
461 return true;
462 }
463
464 return false;
465 }
466
467 function add_domain($domain)
468 {
469 $domain = trim($domain);
470
471 $hash = mixed_sha1($domain);
472 if (!$this->state || !strlen($domain) || $this->domain_exists($hash))
473 return false;
474
475 $query = sprintf("INSERT INTO pony_domain
476 (url, url_hash, import_time)
477 VALUES ('%s', '%s', '%s')",
478 mysql_real_escape_string($domain), // url
479 mysql_real_escape_string($hash), // url_hash
480 mysql_real_escape_string(mysql_now_date()) // import_time
481 );
482
483 $result = mysql_query($query, $this->db_link);
484
485 if (!$result)
486 {
487 $this->state = false;
488 return false;
489 }
490
491 return true;
492 }
493
494 function user_exists($login)
495 {
496 $login = trim($login);
497
498 if (!$this->state || !strlen($login))
499 return false;
500
501 $query = sprintf("SELECT privileges FROM pony_user WHERE (username='%s') LIMIT 1",
502 mysql_real_escape_string($login));
503
504 $result = mysql_query($query, $this->db_link);
505
506 if (!$result)
507 {
508 $this->state = false;
509 } else
510 {
511 if (mysql_num_rows($result) == 1)
512 return true;
513 }
514
515 return false;
516 }
517
518 function user_id_exists($id)
519 {
520 $id = intval($id);
521
522 if (!$this->state || $id <= 0)
523 return false;
524
525 $query = sprintf("SELECT user_id FROM pony_user WHERE (user_id='%s') LIMIT 1",
526 mysql_real_escape_string($id));
527
528 $result = mysql_query($query, $this->db_link);
529
530 if (!$result)
531 {
532 $this->state = false;
533 } else
534 {
535 if (mysql_num_rows($result) == 1)
536 return true;
537 }
538
539 return false;
540 }
541
542 function add_user($login, $password, $privileges = 'user_all', $lang = '')
543 {
544 $login = trim($login);
545 $password = trim($password);
546 $privileges = trim($privileges);
547 $lang = trim($lang);
548
549 if (!$this->state || !strlen($login) || !strlen($password) || !strlen($privileges))
550 return false;
551
552 // check for duplicate addition
553 if ($this->user_exists($login))
554 return false;
555
556 $query = sprintf("INSERT INTO pony_user(username, password, privileges, lang) VALUES ('%s', '%s', '%s', '%s')",
557 mysql_real_escape_string($login),
558 mysql_real_escape_string(mixed_sha1($password)),
559 mysql_real_escape_string($privileges),
560 mysql_real_escape_string($lang));
561
562 $result = mysql_query($query, $this->db_link);
563
564 if (!$result)
565 {
566 $this->state = false;
567 return false;
568 }
569
570 return true;
571 }
572
573 function update_user($user_id, $password, $privileges = 'user_all', $lang = '')
574 {
575 $user_id = intval($user_id);
576 $password = trim($password);
577 $privileges = trim($privileges);
578 $lang = trim($lang);
579
580 if (!$this->state || !strlen($privileges) || $user_id <= 0)
581 return false;
582
583 // check for duplicate addition
584 if (!$this->user_id_exists($user_id))
585 return false;
586
587 $query = sprintf("UPDATE %s SET privileges='%s'",
588 mysql_real_escape_string(CPONY_USER_TABLE),
589 mysql_real_escape_string($privileges));
590
591 if (strlen($password))
592 {
593 $query .= sprintf(", password='%s'", mysql_real_escape_string(mixed_sha1($password)));
594 $query .= sprintf(", auth_cookie=NULL"); // force user to relogin
595 }
596
597 if (strlen($lang))
598 $query .= sprintf(", lang='%s'", mysql_real_escape_string($lang));
599
600 $query .= sprintf(" WHERE user_id='%s'", mysql_real_escape_string($user_id));
601
602 $result = mysql_query($query, $this->db_link);
603
604 if (!$result)
605 {
606 $this->state = false;
607 return false;
608 }
609
610 return true;
611 }
612
613 function delete_user($user_id)
614 {
615 $user_id = intval($user_id);
616
617 if (!$this->state || $user_id <= 0 || !$this->user_id_exists($user_id))
618 return false;
619
620 $query = sprintf("DELETE FROM %s WHERE (user_id='%s')",
621 mysql_real_escape_string(CPONY_USER_TABLE),
622 mysql_real_escape_string($user_id));
623
624 $result = mysql_query($query, $this->db_link);
625
626 if (!$result)
627 {
628 $this->state = false;
629 return false;
630 }
631
632 return true;
633 }
634
635 function get_user_list(&$user_list)
636 {
637 if (!$this->state)
638 return false;
639
640 $query = sprintf("SELECT * FROM %s WHERE (privileges!='admin_all')",
641 mysql_real_escape_string(CPONY_USER_TABLE));
642
643 $result = mysql_query($query, $this->db_link);
644
645
646 if (!$result)
647 {
648 $this->state = false;
649 } else
650 {
651 while ($row = mysql_fetch_assoc($result))
652 {
653 array_push($user_list,
654 array('user_id' => $row['user_id'],
655 'username' => $row['username'],
656 'password' => $row['password'],
657 'privileges' => $row['privileges']
658 )
659 );
660 }
661 return true;
662 }
663
664 return false;
665 }
666
667 function get_user_data($user_id)
668 {
669 $user_id = intval($user_id);
670 if (!$this->state || $user_id <= 0)
671 return false;
672
673 $query = sprintf("SELECT * FROM %s WHERE (user_id='%s') LIMIT 1",
674 mysql_real_escape_string(CPONY_USER_TABLE),
675 mysql_real_escape_string($user_id));
676
677 $result = mysql_query($query, $this->db_link);
678
679
680 if (!$result)
681 {
682 $this->state = false;
683 } else
684 {
685 if ($row = mysql_fetch_assoc($result))
686 {
687 return array('user_id' => $row['user_id'],
688 'username' => $row['username'],
689 'password' => $row['password'],
690 'privileges' => $row['privileges']
691 );
692 }
693 }
694
695 return false;
696 }
697
698 function autneticate_cookie($cookie)
699 {
700 $cookie = trim($cookie);
701 if (!$this->state || !strlen($cookie))
702 return false;
703
704 $query = sprintf("SELECT user_id, privileges, username FROM pony_user WHERE (auth_cookie='%s') LIMIT 1",
705 mysql_real_escape_string($cookie));
706
707 $result = mysql_query($query, $this->db_link);
708
709 if (!$result)
710 {
711 $this->state = false;
712 } else
713 {
714 if (mysql_num_rows($result) == 1)
715 {
716 $row = mysql_fetch_assoc($result);
717 if ($row)
718 {
719 $this->privileges = $row['privileges'];
720 $this->auth_cookie = $cookie;
721 $this->login = $row['username'];
722 $this->user_id = $row['user_id'];
723 return true;
724 }
725 }
726 }
727
728 return false;
729 }
730
731 function update_auth_cookie($user_id, $new_cookie)
732 {
733 $user_id = intval($user_id);
734 $new_cookie = trim($new_cookie);
735 if (!$this->state || $user_id < 0 || !strlen($new_cookie))
736 return false;
737
738 $query = sprintf("UPDATE pony_user SET auth_cookie='%s' WHERE (user_id='%s')",
739 mysql_real_escape_string($new_cookie),
740 mysql_real_escape_string($user_id));
741
742 $result = mysql_query($query, $this->db_link);
743
744 if (!$result)
745 {
746 $this->state = false;
747 } else
748 {
749 $this->auth_cookie = $new_cookie;
750 return true;
751 }
752
753 return false;
754 }
755
756 function remove_auth_cookie($cookie)
757 {
758 $cookie = trim($cookie);
759 if (!$this->state || !strlen($cookie))
760 return false;
761
762 $query = sprintf("UPDATE pony_user SET auth_cookie=NULL WHERE (auth_cookie='%s')",
763 mysql_real_escape_string($cookie));
764
765 $result = mysql_query($query, $this->db_link);
766
767 if (!$result)
768 {
769 $this->state = false;
770 } else
771 {
772 return true;
773 }
774
775 return false;
776 }
777
778 function check_password($password)
779 {
780 $password = trim($password);
781 if (!$this->state || !strlen($password))
782 return false;
783
784 $query = sprintf("SELECT password FROM pony_user WHERE (user_id='%s' AND auth_cookie='%s') LIMIT 1",
785 mysql_real_escape_string($this->user_id),
786 mysql_real_escape_string($this->auth_cookie));
787
788 $result = mysql_query($query, $this->db_link);
789
790 if (!$result)
791 {
792 $this->state = false;
793 } else
794 {
795 if (mysql_num_rows($result) == 1)
796 {
797 $row = mysql_fetch_assoc($result);
798 if ($row)
799 {
800 if ($row['password'] == mixed_sha1($password))
801 return true;
802 }
803 }
804 }
805
806 return false;
807 }
808
809 function change_password($password)
810 {
811 $password = trim($password);
812 if (!$this->state || !strlen($password))
813 return false;
814
815 $query = sprintf("UPDATE pony_user SET password='%s' WHERE (user_id='%s' AND auth_cookie='%s') LIMIT 1",
816 mysql_real_escape_string(mixed_sha1($password)),
817 mysql_real_escape_string($this->user_id),
818 mysql_real_escape_string($this->auth_cookie));
819
820 $result = mysql_query($query, $this->db_link);
821
822 if (!$result)
823 {
824 $this->state = false;
825 } else
826 {
827 return true;
828 }
829
830 return false;
831 }
832
833 function authenticate($login, $password)
834 {
835 $login = trim($login);
836 $password = trim($password);
837
838 if (!$this->state || !strlen($login) || !strlen($password))
839 return false;
840
841 $password = mixed_sha1($password);
842
843 $query = sprintf("SELECT user_id, privileges FROM pony_user WHERE (username='%s' AND password='%s') LIMIT 1",
844 mysql_real_escape_string($login),
845 mysql_real_escape_string($password));
846
847 $result = mysql_query($query, $this->db_link);
848
849 if (!$result)
850 {
851 $this->state = false;
852 } else
853 {
854 if (mysql_num_rows($result) == 1)
855 {
856 $row = mysql_fetch_assoc($result);
857 if ($row)
858 {
859 $this->user_id = $row['user_id'];
860 $this->update_auth_cookie($row['user_id'], mixed_sha1(12345*microtime()));
861 $this->privileges = $row['privileges'];
862 $this->login = $login;
863 return true;
864 }
865 }
866 }
867
868 return false;
869 }
870
871 function import_url_list($url_list, $report_id)
872 {
873 $report_id = intval($report_id);
874
875 if (!$this->state || $report_id <= 0)
876 return false;
877
878 $query_values = '';
879 foreach ($url_list as $ftp_list_item)
880 {
881 list($url, $ftp_client) = $ftp_list_item;
882
883 $url = trim($url);
884 $ftp_client = trim($ftp_client);
885
886 if (!strlen($url) || !strlen($ftp_client))
887 continue;
888
889 $hash = mixed_sha1($url);
890
891 if (str_begins($url, 'rdp://'))
892 {
893 $url_type = 'rdp';
894 } elseif (str_begins($url, 'http://') || str_begins($url, 'https://'))
895 {
896 $url_type = 'http';
897 }
898 elseif (str_begins($url, 'sftp://'))
899 {
900 $url_type = 'ssh';
901 $url = 'ftp://'.substr($url, strlen('sftp://'));
902 }
903 else
904 {
905 $url_type = 'ftp';
906 }
907
908 if (!strlen($query_values))
909 $query_values .= 'VALUES';
910
911 $query_values .= sprintf("('%s','%s','%s','%s','%s','%s'),",
912 mysql_real_escape_string($report_id), // report_id
913 mysql_real_escape_string($url), // url
914 mysql_real_escape_string($url_type), // url_type
915 mysql_real_escape_string($hash), // url_hash
916 mysql_real_escape_string($ftp_client), // ftp_client
917 mysql_real_escape_string(mysql_now_date()) // import_time
918 );
919 }
920 $query_values = substr($query_values, 0, -1);
921
922 if (strlen($query_values))
923 {
924 $query = "INSERT DELAYED IGNORE INTO pony_ftp (report_id, url, url_type, url_hash, ftp_client, import_time) ".
925 $query_values;
926
927 $result = mysql_query($query, $this->db_link);
928
929 if (!$result)
930 {
931 return false;
932 }
933 }
934
935 return true;
936 }
937
938 function import_email_list($email_list, $report_id)
939 {
940 $report_id = intval($report_id);
941
942 if (!$this->state || $report_id <= 0)
943 return false;
944
945 $query_values = '';
946 foreach ($email_list as $email_list_item)
947 {
948 list($email, $email_client) = $email_list_item;
949
950 $email_client = trim($email_client);
951
952 $email_hash = mixed_sha1(report_parser::flat_email_array($email));
953
954 if (!strlen($query_values))
955 $query_values .= 'VALUES';
956
957 $query_values .= sprintf("('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s'),",
958 mysql_real_escape_string($report_id), // report_id
959 mysql_real_escape_string(mysql_now_date()), // import_time
960 mysql_real_escape_string($email_client), // email_client
961 mysql_real_escape_string($email_hash), // dupe-check hash
962 mysql_real_escape_string($email['protocol']), // protocol
963 mysql_real_escape_string($email['email']), // email
964 mysql_real_escape_string($email['server']), // server
965 mysql_real_escape_string(strval(intval($email['port']))), // port
966 mysql_real_escape_string($email['user']), // user
967 mysql_real_escape_string($email['pass']) // pass
968 );
969 }
970 $query_values = substr($query_values, 0, -1);
971
972 if (strlen($query_values))
973 {
974 $query = "INSERT DELAYED IGNORE INTO pony_email (report_id, import_time, email_client, email_hash, protocol, email, server, port, user, pass) ".
975 $query_values;
976
977 $result = mysql_query($query, $this->db_link);
978
979 if (!$result)
980 {
981 return false;
982 }
983 }
984
985 return true;
986 }
987
988 function report_exists($data_hash)
989 {
990 $data_hash = trim($data_hash);
991
992 if (!$this->state || !strlen($data_hash))
993 return false;
994
995 $query = sprintf("SELECT data_id FROM pony_report_data WHERE data_hash='%s' LIMIT 1",
996 mysql_real_escape_string($data_hash));
997
998 $result = mysql_query($query, $this->db_link);
999
1000 if (!$result)
1001 {
1002 $this->state = false;
1003 } else
1004 {
1005 if (mysql_num_rows($result) == 1)
1006 return true;
1007 }
1008
1009 return false;
1010 }
1011
1012 function add_log_line($log_line, $log_source, $report_id = null, $log_extra = '')
1013 {
1014 $log_line = trim($log_line);
1015 $log_source = trim($log_source);
1016
1017 if (!$this->state || !strlen($log_line) || !strlen($log_source))
1018 return false;
1019
1020 if (preg_match('/^NOTIFY_/', $log_line))
1021 $log_type = 'notify';
1022 elseif (preg_match('/^ERR_/', $log_line))
1023 $log_type = 'error';
1024 elseif (preg_match('/^ERROR_/', $log_line))
1025 $log_type = 'error';
1026 else
1027 $log_type = 'other';
1028
1029 $query = sprintf("INSERT DELAYED INTO pony_system_log
1030 (report_id, log_line, log_source, log_type, log_extra, import_time)
1031 VALUES (%s, '%s', '%s', '%s', '%s', '%s')",
1032 ($report_id === null)?'NULL':"'".mysql_real_escape_string($report_id)."'", // report_id
1033 mysql_real_escape_string($log_line), // log_line
1034 mysql_real_escape_string($log_source), // log_source
1035 mysql_real_escape_string($log_type), // log_type
1036 mysql_real_escape_string($log_extra), // log_extra
1037 mysql_real_escape_string(mysql_now_date()) // import_time
1038 );
1039
1040 $result = mysql_query($query, $this->db_link);
1041
1042 if (!$result)
1043 {
1044 $this->state = false;
1045 return false;
1046 }
1047
1048 return true;
1049 }
1050
1051 function import_log_list($log_list, $log_source, $report_id = null)
1052 {
1053
1054 if (!$this->state)
1055 return false;
1056
1057 if (!is_array($log_list) || !count($log_list))
1058 return true;
1059
1060 $query_values = '';
1061
1062 foreach ($log_list as $log_item)
1063 {
1064 list($log_line, $log_extra) = $log_item;
1065 $log_line = trim($log_line);
1066 $log_extra = trim($log_extra);
1067
1068 if (preg_match('/^NOTIFY_/', $log_line))
1069 $log_type = 'notify';
1070 elseif (preg_match('/^ERR_/', $log_line))
1071 $log_type = 'error';
1072 elseif (preg_match('/^ERROR_/', $log_line))
1073 $log_type = 'error';
1074 else
1075 $log_type = 'other';
1076
1077 if (!strlen($log_line) || !strlen($log_source))
1078 continue;
1079
1080 $query_values .= sprintf(
1081 "(%s,'%s','%s','%s','%s','%s'),",
1082 ($report_id === null)?'NULL':"'".mysql_real_escape_string($report_id)."'", // report_id
1083 mysql_real_escape_string($log_line), // log_line
1084 mysql_real_escape_string($log_source), // log_source
1085 mysql_real_escape_string($log_type), // log_type
1086 mysql_real_escape_string($log_extra), // log_extra
1087 mysql_real_escape_string(mysql_now_date()) // import_time
1088 );
1089 }
1090
1091 $query_values = substr($query_values, 0, -1);
1092
1093 if (strlen($query_values))
1094 {
1095 $query = "INSERT DELAYED INTO pony_system_log (report_id, log_line, log_source, log_type, log_extra, import_time) VALUES ".
1096 $query_values;
1097
1098 $result = mysql_query($query, $this->db_link);
1099
1100 if (!$result)
1101 {
1102 return false;
1103 }
1104 }
1105
1106 return true;
1107 }
1108
1109 function update_parsed_report($report_id, $os_name, $is_win64, $is_admin, $hwid, $version, $ftp_list, $log, $cert_list = null, $email_list = null)
1110 {
1111 $report_id = intval($report_id);
1112 if ($report_id <= 0)
1113 return false;
1114
1115 $query = sprintf("UPDATE pony_report SET
1116 parsed='1', report_os_name='%s', report_is_win64='%s', report_admin='%s', report_hwid='%s', report_version='%s'
1117 WHERE report_id='%s'",
1118 mysql_real_escape_string($os_name), // report_os_name
1119 mysql_real_escape_string(intval($is_win64 == 1)), // report_is_win64
1120 mysql_real_escape_string(intval($is_admin == 1)), // report_admin
1121 mysql_real_escape_string($hwid), // report_hwid
1122 mysql_real_escape_string($version), // report_version
1123 mysql_real_escape_string($report_id)
1124 );
1125
1126 $result = mysql_query($query, $this->db_link);
1127
1128 if (!$result)
1129 {
1130 $this->state = false;
1131 return false;
1132 } else
1133 {
1134 // write url (ftp/http) list
1135 $this->import_url_list($ftp_list, $report_id);
1136 if (!$this->state)
1137 return false;
1138
1139 // write cert list
1140 if ($cert_list !== null)
1141 {
1142 foreach ($cert_list as $cert_list_item)
1143 {
1144 list($cert_data, $cert_client) = $cert_list_item;
1145 $this->add_cert($cert_data[0], $cert_data[1], $cert_client, $report_id);
1146 if (!$this->state)
1147 return false;
1148 }
1149 }
1150
1151 // write email list
1152 if ($email_list !== null)
1153 {
1154 $this->import_email_list($email_list, $report_id);
1155 if (!$this->state)
1156 return false;
1157 }
1158
1159 // write logs
1160 if ($log !== null)
1161 {
1162 $this->import_log_list($log, CLOG_SOURCE_REPORT, $report_id);
1163 if (!$this->state)
1164 return false;
1165 }
1166 }
1167
1168 return true;
1169 }
1170
1171 function add_nonparsed_report($ip, $country, $data)
1172 {
1173 if (strlen($data) == 0)
1174 return false;
1175
1176 $hash = mixed_sha1($data);
1177 if (!$this->state || $this->report_exists($hash))
1178 {
1179 return false;
1180 }
1181
1182 $query = sprintf("INSERT INTO pony_report_data(data_hash, data) VALUES('%s', '%s')",
1183 mysql_real_escape_string($hash),
1184 mysql_real_escape_string($data));
1185
1186 $result = mysql_query($query, $this->db_link);
1187
1188 if (!$result)
1189 {
1190 $this->state = false;
1191 return false;
1192 } else
1193 {
1194 $data_id = mysql_insert_id($this->db_link);
1195 }
1196
1197 if ($data_id == 0)
1198 {
1199 $this->state = false;
1200 return false;
1201 }
1202
1203 $query = sprintf("INSERT INTO pony_report
1204 (parsed, import_time, report_source_ip, report_country, data_id)
1205 VALUES ('%s', '%s', '%s', '%s', '%s')",
1206 mysql_real_escape_string(intval(0)), // parsed
1207 mysql_real_escape_string(mysql_now_date()), // import_time
1208 mysql_real_escape_string($ip), // report_source_ip
1209 mysql_real_escape_string($country), // report_country
1210 mysql_real_escape_string($data_id) // data_id
1211 );
1212
1213 $result = mysql_query($query, $this->db_link);
1214
1215 if (!$result)
1216 {
1217 $this->state = false;
1218 return false;
1219 } else
1220 {
1221 return mysql_insert_id($this->db_link);
1222 }
1223 }
1224
1225 function add_parsed_report($os_name, $country, $is_win64, $is_admin, $ip, $hwid, $version, $data, $ftp_list, $log)
1226 {
1227 if (strlen($data) == 0)
1228 return false;
1229
1230 $hash = mixed_sha1($data);
1231 if (!$this->state || $this->report_exists($hash))
1232 return false;
1233
1234 $query = sprintf("INSERT INTO pony_report
1235 (parsed, import_time, report_os_name, report_country, report_is_win64, report_admin, report_source_ip, report_hwid, report_version, data_hash, data)
1236 VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
1237 mysql_real_escape_string(intval(1)), // parsed
1238 mysql_real_escape_string(mysql_now_date()), // import_time
1239 mysql_real_escape_string($os_name), // report_os_name
1240 mysql_real_escape_string($country), // report_country
1241 mysql_real_escape_string(intval($is_win64 == 1)), // report_is_win64
1242 mysql_real_escape_string(intval($is_admin == 1)), // report_admin
1243 mysql_real_escape_string($ip), // report_source_ip
1244 mysql_real_escape_string($hwid), // report_hwid
1245 mysql_real_escape_string($version), // report_version
1246 mysql_real_escape_string($hash), // data_hash
1247 mysql_real_escape_string($data) // data
1248 );
1249
1250 $result = mysql_query($query, $this->db_link);
1251
1252 if (!$result)
1253 {
1254 $this->state = false;
1255 return false;
1256 } else
1257 {
1258 $report_id = mysql_insert_id($this->db_link);
1259 if ($report_id)
1260 {
1261 // write url (ftp/http) list
1262 $this->import_url_list($ftp_list, $report_id);
1263 if (!$this->state)
1264 return false;
1265
1266 // write logs
1267 $this->import_log_list($log, CLOG_SOURCE_REPORT, $report_id);
1268 if (!$this->state)
1269 return false;
1270 }
1271 }
1272
1273 return true;
1274 }
1275
1276
1277 function cert_exists($data_hash)
1278 {
1279 $data_hash = trim($data_hash);
1280
1281 if (!$this->state || !strlen($data_hash))
1282 return false;
1283
1284 $query = sprintf("SELECT data_hash FROM pony_cert WHERE data_hash='%s' LIMIT 1",
1285 mysql_real_escape_string($data_hash));
1286
1287 $result = mysql_query($query, $this->db_link);
1288
1289 if (!$result)
1290 {
1291 $this->state = false;
1292 } else
1293 {
1294 if (mysql_num_rows($result) == 1)
1295 return true;
1296 }
1297
1298 return false;
1299 }
1300
1301 function add_cert($cert, $pvt_key, $cert_client, $report_id)
1302 {
1303 $report_id = intval($report_id);
1304
1305 if (!$this->state || !strlen($cert) || !strlen($pvt_key) || $report_id <= 0)
1306 return false;
1307
1308 $data_hash = mixed_sha1($cert."<!__!>".$pvt_key);
1309
1310 $query = sprintf("INSERT DELAYED IGNORE INTO pony_cert
1311 (report_id, cert_data, pvtkey_data, data_hash, cert_client, import_time)
1312 VALUES ('%s', '%s', '%s', '%s', '%s', '%s')",
1313 mysql_real_escape_string($report_id), // source report id
1314 mysql_real_escape_string($cert), // certificate data
1315 mysql_real_escape_string($pvt_key), // private key data
1316 mysql_real_escape_string($data_hash),
1317 mysql_real_escape_string($cert_client),
1318 mysql_real_escape_string(mysql_now_date()) // import_time
1319 );
1320
1321 $result = mysql_query($query, $this->db_link);
1322
1323 if (!$result)
1324 {
1325 return false;
1326 }
1327
1328 return true;
1329 }
1330
1331 function get_cert_zip()
1332 {
1333 if (!$this->state)
1334 return false;
1335
1336 global $global_temporary_directory;
1337
1338 $tmp_name = tempnam($global_temporary_directory, 'zip');
1339
1340 if (!is_writable($global_temporary_directory))
1341 {
1342 return false;
1343 }
1344
1345 $zip = new ZipArchive();
1346
1347 if ($zip->open($tmp_name, ZIPARCHIVE::CREATE)!==TRUE)
1348 {
1349 return false;
1350 }
1351
1352 $query = sprintf("SELECT cert_id, cert_data, pvtkey_data FROM pony_cert");
1353 $result = mysql_query($query, $this->db_link);
1354
1355 if (!$result)
1356 {
1357 $this->state = false;
1358 } else
1359 {
1360 while ($row = mysql_fetch_assoc($result))
1361 {
1362 $zip->addFromString($row['cert_id']."_cert.crt", $row['cert_data']);
1363 $zip->addFromString($row['cert_id']."_pvtkey.blob", $row['pvtkey_data']);
1364 }
1365 }
1366
1367 $zip->close();
1368 unset($zip);
1369
1370 if ($this->state)
1371 {
1372 $fp = fopen($tmp_name, "rb");
1373 if ($fp)
1374 {
1375 while (!feof($fp))
1376 {
1377 echo fread($fp, 8192);
1378 }
1379 fclose($fp);
1380 }
1381 }
1382 unlink($tmp_name);
1383
1384 return true;
1385 }
1386
1387 function get_last_cert_date()
1388 {
1389 if (!$this->state)
1390 return false;
1391
1392 $query = "SELECT import_time FROM pony_cert ORDER BY cert_id DESC LIMIT 1";
1393 $result = mysql_query($query, $this->db_link);
1394
1395 if (!$result)
1396 {
1397 return false;
1398 }
1399
1400 if ($row = mysql_fetch_assoc($result))
1401 {
1402 return $row['import_time'];
1403 }
1404
1405 return false;
1406 }
1407
1408 function get_login_log(&$domain_list, $limit_count = 5)
1409 {
1410 $limit_count = intval($limit_count);
1411
1412 if (!$this->state || $limit_count <= 0)
1413 return false;
1414
1415 $query = sprintf("SELECT log_id, log_line, import_time, log_extra FROM pony_system_log WHERE (log_source='".CLOG_SOURCE_LOGIN."') ORDER BY log_id DESC LIMIT %s",
1416 mysql_real_escape_string($limit_count));
1417
1418 $result = mysql_query($query, $this->db_link);
1419
1420 if (!$result)
1421 {
1422 $this->state = false;
1423 } else
1424 {
1425 while ($row = mysql_fetch_assoc($result))
1426 {
1427 array_push($domain_list, array('user' => $row['log_extra'], 'ip' => $row['log_line'], 'import_time' => $row['import_time']));
1428 }
1429 return true;
1430 }
1431
1432 return false;
1433 }
1434
1435 function get_url_list($url_type, $raw_output, &$domain_list = null, $limit_count = 0, $url_subtype = 'both', $offset = 0, $filter_date_from = '', $filter_date_to = '', $filter_country = array(), $filter_domains_include = '', $filter_domains_exclude = '', $filter_trim_dirs = '', $count = false, $filter_text = '')
1436 {
1437 $limit_count = intval($limit_count);
1438 $offset = intval($offset);
1439
1440 if (!$this->state || $limit_count < 0)
1441 return false;
1442
1443 $query = '';
1444 if (count($filter_country))
1445 {
1446 $query .= ' INNER JOIN pony_report USING (report_id) ';
1447 }
1448
1449 if ($url_type == 'rdp')
1450 {
1451 $query .= " WHERE url_type='rdp' ";
1452 }
1453 else if ($url_type == 'ftp')
1454 {
1455 if ($url_subtype === 'both')
1456 $query .= " WHERE (url_type='ftp' OR url_type='ssh') ";
1457 else if ($url_subtype == 'ssh')
1458 $query .= " WHERE url_type='ssh' ";
1459 else
1460 $query .= " WHERE url_type='ftp' ";
1461 } else if ($url_type == 'http')
1462 {
1463 $query .= " WHERE (url_type='http' OR url_type='https') ";
1464 if ($url_subtype === 'both')
1465 {
1466 }
1467 else if ($url_subtype == 'https')
1468 $query .= " AND url LIKE 'https://%' ";
1469 else
1470 $query .= " AND url LIKE 'http://%' ";
1471 }
1472
1473 if ($offset > 0)
1474 {
1475 $query .= " AND ftp_id >= '".mysql_real_escape_string($offset)."'";
1476 }
1477
1478 if (count($filter_country))
1479 {
1480 $filter_country_escaped = array();
1481 foreach ($filter_country as $key=>$value)
1482 {
1483 $filter_country_escaped["'".mysql_real_escape_string($key)."'"] = 1;
1484 }
1485 $arrK = array_keys($filter_country_escaped);
1486 $country_list = implode(",", $arrK);
1487
1488 $query .= ' AND report_country in ('.$country_list.')';
1489 }
1490
1491 if (strlen($filter_date_from))
1492 {
1493 $time = strtotime($filter_date_from);
1494 if ($time !== false)
1495 {
1496 $query .= ' AND import_time >= \''.mysql_real_escape_string(date('Y-m-d H:i:s', $time))."'";
1497 }
1498 }
1499
1500 if (strlen($filter_date_to))
1501 {
1502 $time = strtotime($filter_date_to);
1503 if ($time !== false)
1504 {
1505 $query .= ' AND import_time <= \''.mysql_real_escape_string(date('Y-m-d H:i:s', $time))."'";
1506 }
1507 }
1508
1509 if (strlen($filter_domains_include))
1510 {
1511 $query .= " AND ( FALSE ";
1512 $include_domains_array = explode(",", $filter_domains_include);
1513 foreach ($include_domains_array as $key=>$value)
1514 {
1515 $query .= "OR url REGEXP '.*(://){1}.*(:){1}.*(@)+[^/]*(".mysql_real_escape_string(preg_quote(trim($include_domains_array[$key]))).")'";
1516 }
1517 $query .= " ) ";
1518 }
1519
1520 if (strlen($filter_domains_exclude))
1521 {
1522 $query .= " AND ( TRUE ";
1523 $exclude_domains_array = explode(",", $filter_domains_exclude);
1524 foreach ($exclude_domains_array as $key=>$value)
1525 {
1526 $query .= "AND url NOT REGEXP '.*(://){1}.*(:){1}.*(@)+[^/]*(".mysql_real_escape_string(preg_quote(trim($exclude_domains_array[$key]))).")'";
1527 }
1528 $query .= " ) ";
1529 }
1530
1531 if (strlen($filter_text))
1532 {
1533 $query .= " AND url like '%".mysql_real_escape_string($filter_text)."%'";
1534 }
1535
1536 $query = "SELECT ftp_id, url, pony_ftp.import_time, ftp_client FROM pony_ftp $query ";
1537
1538 $query .= ' ORDER BY ftp_id DESC';
1539
1540 if ($limit_count)
1541 $query .= " LIMIT ".mysql_real_escape_string($limit_count);
1542
1543 $result = mysql_query($query, $this->db_link);
1544
1545 if (!$result)
1546 {
1547 $this->state = false;
1548 } else
1549 {
1550 // additional dupe check is required if 'remove dirs/paths' option was supplied
1551 // as URLs without dirs could be a cause of duplicate lines (lines with char differences in dir/path part only)
1552 $dupe_check = array();
1553 if (!$count)
1554 {
1555 // simple row output
1556 while ($row = mysql_fetch_assoc($result))
1557 {
1558 if ($raw_output)
1559 {
1560 if ($filter_trim_dirs == '1')
1561 {
1562 // remove FTP dirs/paths (required for some FTP iframers)
1563 $url_line = trim_ftp_dir($row['url']);
1564 array_push($dupe_check, $url_line);
1565 } else
1566 {
1567 echo remove_zero_char($row['url'])."\r\n";
1568 }
1569 }
1570 else
1571 {
1572 array_push($domain_list, $row);
1573 }
1574 }
1575
1576 if ($filter_trim_dirs == '1')
1577 {
1578 $dupe_check = array_unique($dupe_check);
1579 foreach ($dupe_check as $url)
1580 {
1581 echo remove_zero_char($url)."\r\n";
1582 }
1583 }
1584 return true;
1585 } else
1586 {
1587 // count all found rows and write up to $row_limit rows into the output list
1588 $row_count = 0;
1589 $row_limit = 10;
1590 $rows = array();
1591 if ($filter_trim_dirs == '1')
1592 {
1593 // remove FTP dirs/paths (required for some FTP iframers)
1594 while ($row = mysql_fetch_assoc($result))
1595 {
1596 $uniq_array[trim_ftp_dir($row['url'])] = array($row['ftp_client'], $row['import_time']);
1597 }
1598
1599 foreach ($uniq_array as $key=>$value)
1600 {
1601 if (!$row_limit--)
1602 break;
1603 $rows[] = array('url'=>remove_zero_char($key), 'ftp_client'=>module_name_to_client_name($value[0]), 'module'=>$value[0], 'import_time'=>$value[1]);
1604 }
1605
1606 $row_count = count($uniq_array);
1607 } else
1608 {
1609 while (($row = mysql_fetch_assoc($result)) && $row_limit--)
1610 {
1611 $rows[] = array('url'=>remove_zero_char($row['url']), 'ftp_client'=>module_name_to_client_name($row['ftp_client']), 'module'=>$row['ftp_client'], 'import_time'=>$row['import_time']);
1612 }
1613 $row_count = mysql_num_rows($result);
1614 }
1615
1616 return array('count'=>$row_count, 'list'=>$rows);
1617 }
1618 }
1619 return false;
1620 }
1621
1622 function get_ftp_list($raw_output, &$domain_list = null, $limit_count = 0, $url_subtype = 'ftp', $offset = 0, $filter_date_from = '', $filter_date_to = '', $filter_country = array(), $filter_domains_include = '', $filter_domains_exclude = '', $filter_trim_dirs = '', $count = false, $filter_text = '')
1623 {
1624 return $this->get_url_list('ftp', $raw_output, $domain_list, $limit_count, $url_subtype, $offset, $filter_date_from, $filter_date_to, $filter_country, $filter_domains_include, $filter_domains_exclude, $filter_trim_dirs, $count, $filter_text);
1625 }
1626
1627 function get_http_list($raw_output, &$domain_list = null, $limit_count = 0, $url_subtype = 'both', $offset = 0, $filter_date_from = '', $filter_date_to = '', $filter_country = array(), $filter_domains_include = '', $filter_domains_exclude = '', $filter_trim_dirs = '', $count = false, $filter_text = '')
1628 {
1629 return $this->get_url_list('http', $raw_output, $domain_list, $limit_count, $url_subtype, $offset, $filter_date_from, $filter_date_to, $filter_country, $filter_domains_include, $filter_domains_exclude, $filter_trim_dirs, $count, $filter_text);
1630 }
1631
1632 function get_rdp_list($raw_output, &$domain_list = null, $limit_count = 0, $url_subtype = 'both', $offset = 0, $filter_date_from = '', $filter_date_to = '', $filter_country = array(), $filter_domains_include = '', $filter_domains_exclude = '', $filter_trim_dirs = '', $count = false, $filter_text = '')
1633 {
1634 return $this->get_url_list('rdp', $raw_output, $domain_list, $limit_count, $url_subtype, $offset, $filter_date_from, $filter_date_to, $filter_country, $filter_domains_include, $filter_domains_exclude, $filter_trim_dirs, $count, $filter_text);
1635 }
1636
1637 function get_email_list($raw_output, &$email_list = null, $limit_count = 0, $protocol = '', $offset = 0)
1638 {
1639 $offset = intval($offset);
1640 $limit_count = intval($limit_count);
1641 $protocol = trim($protocol);
1642
1643 if (!$this->state || $limit_count < 0 || $offset < 0)
1644 return false;
1645
1646 $where = "WHERE email_id >= '".mysql_real_escape_string($offset)."'";
1647
1648 if (strlen($protocol))
1649 {
1650 $where .= sprintf("AND protocol='%s'", mysql_real_escape_string($protocol));
1651 }
1652
1653 if ($limit_count == 0)
1654 $query = sprintf("SELECT * FROM ".CPONY_EMAIL_TABLE." $where ORDER BY email_id DESC");
1655 else
1656 $query = sprintf("SELECT * FROM ".CPONY_EMAIL_TABLE." $where ORDER BY email_id DESC LIMIT %s",
1657 mysql_real_escape_string($limit_count));
1658
1659 $result = mysql_query($query, $this->db_link);
1660
1661 if (!$result)
1662 {
1663 $this->state = false;
1664 } else
1665 {
1666 while ($row = mysql_fetch_assoc($result))
1667 {
1668 if (!$raw_output)
1669 array_push($email_list, $row);
1670 else
1671 {
1672 echo remove_zero_char(report_parser::flat_email_array($row))."\r\n";
1673 }
1674 }
1675 return true;
1676 }
1677
1678 return false;
1679 }
1680
1681 function get_table_row_count($table_name, $where_clause = '')
1682 {
1683 $table_name = trim($table_name);
1684 $where_clause = trim($where_clause);
1685 if (!$this->state || !strlen($table_name))
1686 return false;
1687
1688 if (strlen($where_clause))
1689 $query = sprintf("SELECT COUNT(*) as count FROM %s ", mysql_real_escape_string($table_name)).$where_clause;
1690 else
1691 $query = sprintf("SELECT COUNT(*) as count FROM %s", mysql_real_escape_string($table_name));
1692
1693 $result = mysql_query($query, $this->db_link);
1694
1695 if (!$result)
1696 {
1697 $this->state = false;
1698 } else
1699 {
1700 if ($row = mysql_fetch_assoc($result))
1701 {
1702 return $row['count'];
1703 }
1704 }
1705
1706 return false;
1707 }
1708
1709 function get_report_sum($where_clause = '', $offset = 0)
1710 {
1711 $where_clause = trim($where_clause);
1712 $offset = intval($offset);
1713 if (!$this->state)
1714 return false;
1715
1716 if (strlen($where_clause))
1717 {
1718 $query = sprintf("SELECT SUM(OCTET_LENGTH(data)) as sum FROM pony_report_data ".$where_clause);
1719 if ($offset > 0)
1720 {
1721 $query .= ' AND data_id >= \''.mysql_real_escape_string($offset)."'";
1722 }
1723 }
1724 else
1725 {
1726 $query = sprintf("SELECT SUM(OCTET_LENGTH(data)) as sum FROM pony_report_data");
1727 if ($offset > 0)
1728 {
1729 $query .= ' WHERE data_id >= \''.mysql_real_escape_string($offset)."'";
1730 }
1731
1732 }
1733
1734 $result = mysql_query($query, $this->db_link);
1735
1736 if (!$result)
1737 {
1738 $this->state = false;
1739 } else
1740 {
1741 if ($row = mysql_fetch_assoc($result))
1742 {
1743 return $row['sum'];
1744 }
1745 }
1746
1747 return false;
1748 }
1749
1750 function get_report_sum_linked($where_clause = '', $offset = 0)
1751 {
1752 $where_clause = trim($where_clause);
1753 $offset = intval($offset);
1754 if (!$this->state)
1755 return false;
1756
1757 if (strlen($where_clause))
1758 {
1759 $query = "SELECT SUM(OCTET_LENGTH(data)) as sum FROM pony_report INNER JOIN pony_report_data USING (data_id) ".$where_clause;
1760 if ($offset > 0)
1761 {
1762 $query .= ' AND report_id >= \''.mysql_real_escape_string($offset)."'";
1763 }
1764 }
1765 else
1766 {
1767 $query = "SELECT SUM(OCTET_LENGTH(data)) as sum FROM pony_report_data";
1768 if ($offset > 0)
1769 {
1770 $query .= ' WHERE report_id >= \''.mysql_real_escape_string($offset)."'";
1771 }
1772 }
1773
1774 $result = mysql_query($query, $this->db_link);
1775
1776 if (!$result)
1777 {
1778 $this->state = false;
1779 } else
1780 {
1781 if ($row = mysql_fetch_assoc($result))
1782 {
1783 return $row['sum'];
1784 }
1785 }
1786
1787 return false;
1788 }
1789
1790 function get_db_size()
1791 {
1792 if (!$this->state)
1793 return false;
1794
1795 $query = sprintf("SELECT SUM(data_length + index_length) as sum FROM information_schema.TABLES WHERE (table_schema='%s')", mysql_real_escape_string($this->database));
1796
1797 $result = mysql_query($query, $this->db_link);
1798
1799 if (!$result)
1800 {
1801 $this->state = false;
1802 } else
1803 {
1804 if ($row = mysql_fetch_assoc($result))
1805 {
1806 return $row['sum'];
1807 }
1808 }
1809
1810 return false;
1811 }
1812
1813 function get_table_size($table_name)
1814 {
1815 $table_name = trim($table_name);
1816 if (!$this->state || strlen($table_name) == 0)
1817 return false;
1818
1819 $query = sprintf("SELECT SUM(data_length + index_length) as sum FROM information_schema.TABLES WHERE (table_schema='%s' AND table_name='%s')", mysql_real_escape_string($this->database), mysql_real_escape_string($table_name));
1820
1821 $result = mysql_query($query, $this->db_link);
1822
1823 if (!$result)
1824 {
1825 $this->state = false;
1826 } else
1827 {
1828 if ($row = mysql_fetch_assoc($result))
1829 {
1830 return $row['sum'];
1831 }
1832 }
1833
1834 return false;
1835 }
1836
1837 function export_reports($nonparsed_only)
1838 {
1839 if (!$this->state)
1840 return false;
1841
1842 if ($nonparsed_only)
1843 $query = sprintf("SELECT * FROM %s INNER JOIN pony_report_data USING (data_id) ORDER BY report_id DESC WHERE parsed='0' ", mysql_real_escape_string(CPONY_REPORT_TABLE));
1844 else
1845 $query = sprintf("SELECT * FROM %s INNER JOIN pony_report_data USING (data_id) ORDER BY report_id DESC", mysql_real_escape_string(CPONY_REPORT_TABLE));
1846
1847 $result = mysql_query($query, $this->db_link);
1848
1849 if (!$result)
1850 {
1851 $this->state = false;
1852 } else
1853 {
1854 while ($row = mysql_fetch_assoc($result))
1855 {
1856 echo sprintf("INSERT IGNORE INTO %s (data_hash, data) VALUES('%s', '%s');\r\n".
1857 "INSERT INTO %s (parsed, report_os_name, import_time, report_country, report_is_win64, report_admin, report_source_ip, report_hwid, report_version, data_id) ".
1858 "SELECT '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', LAST_INSERT_ID() FROM DUAL WHERE LAST_INSERT_ID() > 0;\r\n",
1859 mysql_real_escape_string(CPONY_REPORT_DATA_TABLE),
1860 mysql_real_escape_string($row['data_hash']),
1861 mysql_real_escape_string($row['data']),
1862 mysql_real_escape_string(CPONY_REPORT_TABLE),
1863 mysql_real_escape_string($row['parsed']),
1864 mysql_real_escape_string($row['report_os_name']),
1865 mysql_real_escape_string($row['import_time']),
1866 mysql_real_escape_string($row['report_country']),
1867 mysql_real_escape_string($row['report_is_win64']),
1868 mysql_real_escape_string($row['report_admin']),
1869 mysql_real_escape_string($row['report_source_ip']),
1870 mysql_real_escape_string($row['report_hwid']),
1871 mysql_real_escape_string($row['report_version']),
1872 mysql_real_escape_string($row['report_country']));
1873 }
1874 }
1875
1876 return false;
1877 }
1878
1879 function clear_table($table_name, $where_clause = '')
1880 {
1881 $table_name = trim($table_name);
1882 $where_clause = trim($where_clause);
1883 if (!$this->state || !strlen($table_name))
1884 return false;
1885
1886 $query = sprintf("DELETE FROM %s", mysql_real_escape_string($table_name));
1887
1888 if (strlen($where_clause))
1889 $query .= ' '.$where_clause;
1890
1891
1892 $result = mysql_query($query, $this->db_link);
1893
1894 if (!$result)
1895 {
1896 $this->state = false;
1897 } else
1898 {
1899 return true;
1900 }
1901
1902 return false;
1903 }
1904
1905 function truncate_table($table_name)
1906 {
1907 $table_name = trim($table_name);
1908 if (!$this->state || !strlen($table_name))
1909 return false;
1910
1911 $query = sprintf("TRUNCATE %s", mysql_real_escape_string($table_name));
1912
1913 $result = mysql_query($query, $this->db_link);
1914
1915 if (!$result)
1916 {
1917 $this->state = false;
1918 } else
1919 {
1920 return true;
1921 }
1922
1923 return false;
1924 }
1925
1926 function get_domains(&$domain_list)
1927 {
1928 if (!$this->state)
1929 return false;
1930
1931 $query = sprintf("SELECT domain_id, url FROM %s ORDER BY domain_id DESC", mysql_real_escape_string(CPONY_DOMAIN_TABLE));
1932
1933 $result = mysql_query($query, $this->db_link);
1934
1935 if (!$result)
1936 {
1937 $this->state = false;
1938 } else
1939 {
1940 while ($row = mysql_fetch_assoc($result))
1941 {
1942 array_push($domain_list, $row);
1943 }
1944 return true;
1945 }
1946
1947 return false;
1948 }
1949
1950 function delete_domain($domain_id)
1951 {
1952 $domain_id = intval($domain_id);
1953
1954 if (!$this->state || $domain_id < 0)
1955 return false;
1956
1957 $query = sprintf("DELETE FROM %s WHERE domain_id='%s'", mysql_real_escape_string(CPONY_DOMAIN_TABLE),
1958 mysql_real_escape_string($domain_id));
1959
1960 $result = mysql_query($query, $this->db_link);
1961
1962 if (!$result)
1963 {
1964 $this->state = false;
1965 } else
1966 {
1967 return true;
1968 }
1969
1970 return false;
1971 }
1972
1973 function find_domain($domain_id)
1974 {
1975 $domain_id = intval($domain_id);
1976
1977 if (!$this->state || $domain_id < 0)
1978 return false;
1979
1980 $query = sprintf("SELECT url, ping_time, ping_status FROM %s WHERE domain_id='%s'",
1981 mysql_real_escape_string(CPONY_DOMAIN_TABLE),
1982 mysql_real_escape_string($domain_id));
1983
1984 $result = mysql_query($query, $this->db_link);
1985
1986 if (!$result)
1987 {
1988 $this->state = false;
1989 } else
1990 {
1991 while ($row = mysql_fetch_assoc($result))
1992 {
1993 return array($row['url'], $row['ping_time'], $row['ping_status']);
1994 }
1995 return true;
1996 }
1997 }
1998
1999 function update_domain($domain_id, $ping_status)
2000 {
2001 $domain_id = intval($domain_id);
2002 $ping_status = trim($ping_status);
2003
2004 if (!$this->state || $domain_id < 0 || !strlen($ping_status))
2005 return false;
2006
2007 $query = sprintf("UPDATE pony_domain SET ping_status='%s', ping_time='%s' WHERE (domain_id='%s')",
2008 mysql_real_escape_string($ping_status),
2009 mysql_real_escape_string(mysql_now_date()),
2010 mysql_real_escape_string($domain_id));
2011
2012 $result = mysql_query($query, $this->db_link);
2013
2014 if (!$result)
2015 {
2016 $this->state = false;
2017 } else
2018 {
2019 return true;
2020 }
2021
2022 return false;
2023 }
2024
2025 function get_log_item($id)
2026 {
2027 $id = intval($id);
2028
2029 if (!$this->state || $id < 0)
2030 return false;
2031
2032 $query = sprintf("SELECT log_id, report_id, log_line, import_time, log_extra FROM pony_system_log WHERE (log_id='%s' AND (log_source='".CLOG_SOURCE_REPORT."' OR log_source='".CLOG_SOURCE_GATE."'))",
2033 mysql_real_escape_string($id));
2034
2035 $result = mysql_query($query, $this->db_link);
2036
2037 if (!$result)
2038 {
2039 $this->state = false;
2040 } else
2041 {
2042 if ($row = mysql_fetch_assoc($result))
2043 return $row;
2044 return false;
2045 }
2046
2047 return false;
2048 }
2049
2050 function get_report_item($id)
2051 {
2052 $id = intval($id);
2053
2054 if (!$this->state || $id < 0)
2055 return false;
2056
2057 $query = sprintf("SELECT *, OCTET_LENGTH(data) as report_len FROM %s INNER JOIN pony_report_data USING (data_id) WHERE (report_id='%s')",
2058 mysql_real_escape_string(CPONY_REPORT_TABLE),
2059 mysql_real_escape_string($id));
2060
2061 $result = mysql_query($query, $this->db_link);
2062
2063 if (!$result)
2064 {
2065 $this->state = false;
2066 } else
2067 {
2068 if ($row = mysql_fetch_assoc($result))
2069 {
2070 return $row;
2071 }
2072 }
2073
2074 return false;
2075 }
2076
2077 function get_log_list(&$domain_list, $limit_start = 0, $limit_count = 0, $raw_output = false)
2078 {
2079 $limit_start = intval($limit_start);
2080 $limit_count = intval($limit_count);
2081
2082 if (!$this->state || $limit_count < 0 || $limit_start < 0)
2083 return false;
2084
2085 if ($limit_start == 0 && $limit_count == 0)
2086 $query = sprintf("SELECT log_id, report_id, log_line, import_time, log_extra FROM ".CPONY_LOG_TABLE." WHERE (log_source='".CLOG_SOURCE_REPORT."' OR log_source='".CLOG_SOURCE_GATE."') ORDER BY log_id DESC");
2087 else
2088 $query = sprintf("SELECT log_id, report_id, log_line, import_time, log_extra FROM ".CPONY_LOG_TABLE." WHERE (log_source='".CLOG_SOURCE_REPORT."' OR log_source='".CLOG_SOURCE_GATE."') ORDER BY log_id DESC LIMIT %s, %s",
2089 mysql_real_escape_string($limit_start),
2090 mysql_real_escape_string($limit_count));
2091
2092 $result = mysql_query($query, $this->db_link);
2093
2094 if (!$result)
2095 {
2096 $this->state = false;
2097 } else
2098 {
2099 while ($row = mysql_fetch_assoc($result))
2100 {
2101 if (!$raw_output)
2102 array_push($domain_list, array('log_id' => $row['log_id'], 'report_id' => $row['report_id'], 'log_extra' => $row['log_extra'], 'log_line' => $row['log_line'], 'import_time' => $row['import_time']));
2103 else
2104 echo $row['report_id']." | ".$row['log_line']." | ".$row['log_extra']."\r\n";
2105 }
2106 return true;
2107 }
2108
2109 return false;
2110 }
2111
2112 function get_log_list_report_filter(&$domain_list, $limit_start = 0, $limit_count = 0, $raw_output = false, $report_source_ip = '', $report_hwid = '', $filter_notify = '')
2113 {
2114 $limit_start = intval($limit_start);
2115 $limit_count = intval($limit_count);
2116 $report_source_ip = trim($report_source_ip);
2117 $report_hwid = trim($report_hwid);
2118
2119 if (!$this->state || $limit_count < 0 || $limit_start < 0)
2120 return false;
2121
2122 // SELECT
2123 $query = str_replace('%s', CPONY_LOG_TABLE, "(SELECT %s.log_id as log_id, %s.report_id, %s.log_line, %s.import_time as import_time, %s.log_extra FROM ".CPONY_LOG_TABLE);
2124
2125 // JOIN tables
2126 if (strlen($report_source_ip) || strlen($report_hwid))
2127 $query .= ' INNER JOIN pony_report USING (report_id)';
2128
2129 // WHERE clause
2130 $query .= " WHERE ((".CPONY_LOG_TABLE.".log_source='".CLOG_SOURCE_REPORT."' OR ".CPONY_LOG_TABLE.".log_source='".CLOG_SOURCE_GATE."')";
2131 // append IP filter
2132 if (strlen($report_source_ip))
2133 $query .= ' AND '.CPONY_REPORT_TABLE.'.report_source_ip=\''.mysql_real_escape_string($report_source_ip).'\'';
2134 // append HWID filter
2135 if (strlen($report_hwid))
2136 $query .= ' AND '.CPONY_REPORT_TABLE.'.report_hwid=\''.mysql_real_escape_string($report_hwid).'\'';
2137 $query .= ")";
2138
2139 if ($filter_notify != '1')
2140 $query .= " AND (".CPONY_LOG_TABLE.".log_type<>'notify')";
2141
2142 $query .= ')'; // SELECT
2143
2144 if (strlen($report_source_ip) && !strlen($report_hwid))
2145 {
2146 $query .= " UNION (". str_replace('%s', CPONY_LOG_TABLE, "SELECT %s.log_id as log_id, %s.report_id, %s.log_line, %s.import_time as import_time, %s.log_extra FROM ".CPONY_LOG_TABLE).
2147 " WHERE ".CPONY_LOG_TABLE.".log_source='".CLOG_SOURCE_GATE."' AND ".CPONY_LOG_TABLE.".log_extra='".mysql_real_escape_string($report_source_ip)."'";
2148
2149 if ($filter_notify != '1')
2150 $query .= " AND ".CPONY_LOG_TABLE.".log_type<>'notify'";
2151
2152 $query .= ")";
2153 }
2154
2155 // sorting
2156 $query .= " ORDER BY log_id DESC";
2157
2158 // limiting
2159 if ($limit_start != 0 || $limit_count != 0)
2160 $query .= sprintf(" LIMIT %s, %s",
2161 mysql_real_escape_string($limit_start),
2162 mysql_real_escape_string($limit_count));
2163
2164 $result = mysql_query($query, $this->db_link);
2165
2166 if (!$result)
2167 {
2168 $this->state = false;
2169 } else
2170 {
2171 while ($row = mysql_fetch_assoc($result))
2172 {
2173 if (!$raw_output)
2174 array_push($domain_list, array('log_id' => $row['log_id'], 'report_id' => $row['report_id'], 'log_extra' => $row['log_extra'], 'log_line' => $row['log_line'], 'import_time' => $row['import_time']));
2175 else
2176 echo $row['report_id']." | ".$row['log_line']." | ".$row['log_extra']."\r\n";
2177 }
2178 return true;
2179 }
2180
2181 return false;
2182 }
2183
2184 function get_log_row_count_filter($report_source_ip = '', $report_hwid = '', $filter_notify = '')
2185 {
2186 $report_source_ip = trim($report_source_ip);
2187 $report_hwid = trim($report_hwid);
2188 if (!$this->state)
2189 return false;
2190
2191 // SELECT
2192 $query = str_replace('%s', CPONY_LOG_TABLE, "(SELECT %s.log_id as log_id, %s.report_id, %s.log_line, %s.import_time as import_time, %s.log_extra FROM ".CPONY_LOG_TABLE);
2193
2194 // JOIN tables
2195 if (strlen($report_source_ip) || strlen($report_hwid))
2196 $query .= ' INNER JOIN pony_report USING (report_id)';
2197
2198 // WHERE clause
2199 $query .= " WHERE ((".CPONY_LOG_TABLE.".log_source='".CLOG_SOURCE_REPORT."' OR ".CPONY_LOG_TABLE.".log_source='".CLOG_SOURCE_GATE."')";
2200 // append IP filter
2201 if (strlen($report_source_ip))
2202 $query .= ' AND '.CPONY_REPORT_TABLE.'.report_source_ip=\''.mysql_real_escape_string($report_source_ip).'\'';
2203 // append HWID filter
2204 if (strlen($report_hwid))
2205 $query .= ' AND '.CPONY_REPORT_TABLE.'.report_hwid=\''.mysql_real_escape_string($report_hwid).'\'';
2206 $query .= ')';
2207
2208 if ($filter_notify != '1')
2209 $query .= " AND (".CPONY_LOG_TABLE.".log_type<>'notify')";
2210
2211 $query .= ')'; // SELECT
2212
2213 if (strlen($report_source_ip) && !strlen($report_hwid))
2214 {
2215 $query .= " UNION (". str_replace('%s', CPONY_LOG_TABLE, "SELECT %s.log_id as log_id, %s.report_id, %s.log_line, %s.import_time as import_time, %s.log_extra FROM ".CPONY_LOG_TABLE).
2216 " WHERE ".CPONY_LOG_TABLE.".log_source='".CLOG_SOURCE_GATE."' AND ".CPONY_LOG_TABLE.".log_extra='".mysql_real_escape_string($report_source_ip)."'";
2217
2218 if ($filter_notify != '1')
2219 $query .= " AND ".CPONY_LOG_TABLE.".log_type<>'notify'";
2220
2221 $query .= ")";
2222 }
2223
2224 $result = mysql_query($query, $this->db_link);
2225
2226 if (!$result)
2227 {
2228 $this->state = false;
2229 } else
2230 {
2231 return mysql_num_rows($result);
2232 }
2233
2234 return false;
2235 }
2236
2237 function get_report_list_filter(&$domain_list, $limit_start = 0, $limit_count = 0, $report_source_ip, $report_hwid, $filter_nonparsed, $filter_has_passwords, $filter_string = '')
2238 {
2239 $limit_start = intval($limit_start);
2240 $limit_count = intval($limit_count);
2241 $filter_nonparsed = trim($filter_nonparsed);
2242 $report_source_ip = trim($report_source_ip);
2243 $report_hwid = trim($report_hwid);
2244 $filter_has_passwords = trim($filter_has_passwords);
2245 $filter_string = trim($filter_string);
2246 if ($filter_has_passwords == '1')
2247 $filter_has_passwords = '0';
2248 else
2249 $filter_has_passwords = '1';
2250 if ($filter_nonparsed == '1')
2251 $filter_has_passwords = '0';
2252 if (!$this->state || $limit_count < 0 || $limit_start < 0)
2253 return false;
2254
2255 // SELECT
2256 $query = "SELECT pony_report.*, OCTET_LENGTH(data) as report_len, (SELECT COUNT(*) FROM pony_ftp WHERE pony_ftp.report_id=pony_report.report_id)+(SELECT COUNT(*) FROM pony_email WHERE pony_email.report_id=pony_report.report_id)+(SELECT COUNT(*) FROM pony_cert WHERE pony_cert.report_id=pony_report.report_id) as count FROM ".CPONY_REPORT_TABLE." LEFT JOIN pony_report_data USING (data_id)";
2257
2258 // WHERE clause
2259 if ($filter_nonparsed == '1')
2260 $query .= " WHERE pony_report.parsed='0'";
2261 else
2262 {
2263 $query .= ' WHERE TRUE';
2264 }
2265
2266 // append has passwords filter
2267 if ($filter_has_passwords == '1')
2268 {
2269 $query .= ' AND (EXISTS(SELECT * FROM pony_ftp WHERE pony_report.report_id=pony_ftp.report_id LIMIT 1)
2270 OR EXISTS(SELECT * FROM pony_email WHERE pony_report.report_id=pony_email.report_id LIMIT 1)
2271 OR EXISTS(SELECT * FROM pony_cert WHERE pony_report.report_id=pony_cert.report_id LIMIT 1))';
2272 }
2273
2274 // apply string filter
2275 if (strlen($filter_string))
2276 {
2277 $query .= ' AND (EXISTS(SELECT * FROM pony_ftp WHERE pony_report.report_id=pony_ftp.report_id AND pony_ftp.url LIKE \'%'.mysql_real_escape_string($filter_string).'%\' LIMIT 1)';
2278 $query .= ' OR EXISTS(SELECT * FROM pony_email WHERE pony_report.report_id=pony_email.report_id AND ((pony_email.port=0 AND CONCAT(pony_email.protocol,\'://\',pony_email.email,\'|\',pony_email.server,\'|\',pony_email.user,\'|\',pony_email.pass) LIKE \'%'.mysql_real_escape_string($filter_string).'%\')
2279 OR (pony_email.port<>0 AND CONCAT(pony_email.protocol,\'://\',pony_email.email,\'|\',pony_email.server,\':\',pony_email.port,\'|\',pony_email.user,\'|\',pony_email.pass) LIKE \'%'.mysql_real_escape_string($filter_string).'%\'))
2280 LIMIT 1)';
2281 $query .= ')';
2282 }
2283
2284 // append IP filter
2285 if (strlen($report_source_ip))
2286 $query .= ' AND '.CPONY_REPORT_TABLE.'.report_source_ip=\''.mysql_real_escape_string($report_source_ip).'\'';
2287 // append HWID filter
2288 if (strlen($report_hwid))
2289 $query .= ' AND '.CPONY_REPORT_TABLE.'.report_hwid=\''.mysql_real_escape_string($report_hwid).'\'';
2290
2291 // sorting
2292 $query .= " ORDER BY pony_report.report_id DESC";
2293
2294 // limiting
2295 if ($limit_start != 0 || $limit_count != 0)
2296 $query .= sprintf(" LIMIT %s, %s",
2297 mysql_real_escape_string($limit_start),
2298 mysql_real_escape_string($limit_count));
2299
2300 $result = mysql_query($query, $this->db_link);
2301
2302 if (!$result)
2303 {
2304 $this->state = false;
2305 } else
2306 {
2307 while ($row = mysql_fetch_assoc($result))
2308 {
2309 array_push($domain_list, $row);
2310 }
2311 return true;
2312 }
2313
2314 return false;
2315 }
2316
2317 function get_report_row_count_filter($report_source_ip = '', $report_hwid = '', $filter_nonparsed = '', $filter_has_passwords = '', $filter_string = '')
2318 {
2319 $filter_string = trim($filter_string);
2320 $report_source_ip = trim($report_source_ip);
2321 $report_hwid = trim($report_hwid);
2322 $filter_nonparsed = trim($filter_nonparsed);
2323 $filter_has_passwords = trim($filter_has_passwords);
2324 if ($filter_has_passwords == '1')
2325 $filter_has_passwords = '0';
2326 else
2327 $filter_has_passwords = '1';
2328 if ($filter_nonparsed == '1')
2329 $filter_has_passwords = '0';
2330 if (!$this->state)
2331 return false;
2332
2333 // SELECT
2334 $query = "SELECT pony_report.report_id FROM ".CPONY_REPORT_TABLE;
2335
2336 // WHERE clause
2337 if ($filter_nonparsed == '1')
2338 $query .= " WHERE pony_report.parsed='0'";
2339 else
2340 {
2341 $query .= ' WHERE TRUE';
2342 }
2343
2344 if ($filter_has_passwords == '1')
2345 {
2346 $query .= ' AND (EXISTS(SELECT * FROM pony_ftp WHERE pony_report.report_id=pony_ftp.report_id LIMIT 1)
2347 OR EXISTS(SELECT * FROM pony_email WHERE pony_report.report_id=pony_email.report_id LIMIT 1)
2348 OR EXISTS(SELECT * FROM pony_cert WHERE pony_report.report_id=pony_cert.report_id LIMIT 1))';
2349 }
2350
2351 // apply string filter
2352 if (strlen($filter_string))
2353 {
2354 $query .= ' AND (EXISTS(SELECT * FROM pony_ftp WHERE pony_report.report_id=pony_ftp.report_id AND pony_ftp.url LIKE \'%'.mysql_real_escape_string($filter_string).'%\' LIMIT 1)';
2355 $query .= ' OR EXISTS(SELECT * FROM pony_email WHERE pony_report.report_id=pony_email.report_id AND ((pony_email.port=0 AND CONCAT(pony_email.protocol,\'://\',pony_email.email,\'|\',pony_email.server,\'|\',pony_email.user,\'|\',pony_email.pass) LIKE \'%'.mysql_real_escape_string($filter_string).'%\')
2356 OR (pony_email.port<>0 AND CONCAT(pony_email.protocol,\'://\',pony_email.email,\'|\',pony_email.server,\':\',pony_email.port,\'|\',pony_email.user,\'|\',pony_email.pass) LIKE \'%'.mysql_real_escape_string($filter_string).'%\'))
2357 LIMIT 1)';
2358 $query .= ')';
2359 }
2360
2361 // append IP filter
2362 if (strlen($report_source_ip))
2363 $query .= ' AND '.CPONY_REPORT_TABLE.'.report_source_ip=\''.mysql_real_escape_string($report_source_ip).'\'';
2364 // append HWID filter
2365 if (strlen($report_hwid))
2366 $query .= ' AND '.CPONY_REPORT_TABLE.'.report_hwid=\''.mysql_real_escape_string($report_hwid).'\'';
2367
2368 $result = mysql_query($query, $this->db_link);
2369
2370 if (!$result)
2371 {
2372 $this->state = false;
2373 } else
2374 {
2375 return mysql_num_rows($result);
2376 }
2377 return false;
2378 }
2379
2380 function get_report_linked_passwords_count($report_id)
2381 {
2382 $report_id = intval($report_id);
2383 if (!$this->state)
2384 return false;
2385
2386 $query = sprintf("SELECT COUNT(*) as count FROM %s WHERE report_id='%s'", CPONY_FTP_TABLE, $report_id);
2387 $result = mysql_query($query, $this->db_link);
2388
2389 if (!$result)
2390 {
2391 $this->state = false;
2392 } else
2393 {
2394 if ($row = mysql_fetch_assoc($result))
2395 {
2396 return $row['count'];
2397 }
2398 return true;
2399 }
2400
2401 return false;
2402 }
2403
2404 function delete_log_items()
2405 {
2406 if (!$this->state)
2407 return false;
2408
2409 $query = sprintf("DELETE FROM %s WHERE (log_source='%s' OR log_source='%s')", mysql_real_escape_string(CPONY_LOG_TABLE), mysql_real_escape_string(CLOG_SOURCE_REPORT),
2410 mysql_real_escape_string(CLOG_SOURCE_GATE));
2411
2412 $result = mysql_query($query, $this->db_link);
2413
2414 if (!$result)
2415 {
2416 $this->state = false;
2417 } else
2418 {
2419 return true;
2420 }
2421
2422 return false;
2423 }
2424
2425 function get_last_24_mins()
2426 {
2427 $result = 23*60*60;
2428 $time = (time()-23*60*60)%(60*60);
2429 return $result+$time;
2430 }
2431
2432 function get_last_30_days()
2433 {
2434 $result = 24*60*60*27;
2435 $time = (time()-24*60*60*27)%(24*60*60);
2436 return $result+$time;
2437 }
2438
2439 function get_ftp_count_last_24_hours(&$out_array)
2440 {
2441 if (!$this->state)
2442 return false;
2443
2444 $time = $this->get_last_24_mins();
2445 $query = 'SELECT HOUR(import_time) as hour, COUNT(*) as count
2446 FROM pony_ftp
2447 WHERE (url_type=\'ftp\' OR url_type=\'ssh\') AND (import_time >= DATE_SUB(\''.mysql_real_escape_string(mysql_now_date()).'\',INTERVAL '.$time.' SECOND))
2448 GROUP BY HOUR(import_time)';
2449
2450 $result = mysql_query($query, $this->db_link);
2451
2452 if (!$result)
2453 {
2454 $this->state = false;
2455 } else
2456 {
2457 while ($row = mysql_fetch_assoc($result))
2458 $out_array[intval($row['hour'])] = $row['count'];
2459 return true;
2460 }
2461
2462 return false;
2463 }
2464
2465 function get_http_count_last_24_hours(&$out_array)
2466 {
2467 if (!$this->state)
2468 return false;
2469
2470 $time = $this->get_last_24_mins();
2471 $query = 'SELECT HOUR(import_time) as hour, COUNT(*) as count
2472 FROM pony_ftp
2473 WHERE (url_type=\'http\' OR url_type=\'https\') AND (import_time >= DATE_SUB(\''.mysql_real_escape_string(mysql_now_date()).'\',INTERVAL '.$time.' SECOND))
2474 GROUP BY HOUR(import_time)';
2475
2476 $result = mysql_query($query, $this->db_link);
2477
2478 if (!$result)
2479 {
2480 $this->state = false;
2481 } else
2482 {
2483 while ($row = mysql_fetch_assoc($result))
2484 $out_array[intval($row['hour'])] = $row['count'];
2485 return true;
2486 }
2487
2488 return false;
2489 }
2490
2491 function get_email_count_last_24_hours(&$out_array)
2492 {
2493 if (!$this->state)
2494 return false;
2495
2496 $time = $this->get_last_24_mins();
2497 $query = 'SELECT HOUR(import_time) as hour, COUNT(*) as count
2498 FROM pony_email
2499 WHERE (import_time >= DATE_SUB(\''.mysql_real_escape_string(mysql_now_date()).'\',INTERVAL '.$time.' SECOND))
2500 GROUP BY HOUR(import_time)';
2501
2502 $result = mysql_query($query, $this->db_link);
2503
2504 if (!$result)
2505 {
2506 $this->state = false;
2507 } else
2508 {
2509 while ($row = mysql_fetch_assoc($result))
2510 $out_array[intval($row['hour'])] = $row['count'];
2511 return true;
2512 }
2513
2514 return false;
2515 }
2516
2517 function get_report_count_last_24_hours(&$out_array)
2518 {
2519 if (!$this->state)
2520 return false;
2521
2522 $time = $this->get_last_24_mins();
2523 $query = 'SELECT HOUR(import_time) as hour, COUNT(*) as count
2524 FROM pony_report
2525 WHERE (import_time >= DATE_SUB(\''.mysql_real_escape_string(mysql_now_date()).'\',INTERVAL '.$time.' SECOND))
2526 GROUP BY HOUR(import_time)';
2527
2528 $result = mysql_query($query, $this->db_link);
2529
2530 if (!$result)
2531 {
2532 $this->state = false;
2533 } else
2534 {
2535 while ($row = mysql_fetch_assoc($result))
2536 $out_array[intval($row['hour'])] = $row['count'];
2537 return true;
2538 }
2539
2540 return false;
2541 }
2542
2543 function get_email_count_last_month(&$out_array)
2544 {
2545 if (!$this->state)
2546 return false;
2547
2548 $time = $this->get_last_30_days();
2549 $query = 'SELECT DAY(import_time) as day, COUNT(*) as count
2550 FROM pony_email
2551 WHERE (import_time >= DATE_SUB(\''.mysql_real_escape_string(mysql_now_date()).'\',INTERVAL '.$time.' SECOND))
2552 GROUP BY DAY(import_time)';
2553
2554 $result = mysql_query($query, $this->db_link);
2555
2556 if (!$result)
2557 {
2558 $this->state = false;
2559 } else
2560 {
2561 while ($row = mysql_fetch_assoc($result))
2562 $out_array[intval($row['day'])] = $row['count'];
2563 return true;
2564 }
2565 return false;
2566 }
2567
2568 function get_ftp_count_last_month(&$out_array)
2569 {
2570 if (!$this->state)
2571 return false;
2572
2573 $time = $this->get_last_30_days();
2574 $query = 'SELECT DAY(import_time) as day, COUNT(*) as count
2575 FROM pony_ftp
2576 WHERE (url_type=\'ftp\' OR url_type=\'ssh\') AND (import_time >= DATE_SUB(\''.mysql_real_escape_string(mysql_now_date()).'\',INTERVAL '.$time.' SECOND))
2577 GROUP BY DAY(import_time)';
2578
2579 $result = mysql_query($query, $this->db_link);
2580
2581 if (!$result)
2582 {
2583 $this->state = false;
2584 } else
2585 {
2586 while ($row = mysql_fetch_assoc($result))
2587 $out_array[intval($row['day'])] = $row['count'];
2588 return true;
2589 }
2590 return false;
2591 }
2592
2593
2594 function get_http_count_last_month(&$out_array)
2595 {
2596 if (!$this->state)
2597 return false;
2598
2599 $time = $this->get_last_30_days();
2600 $query = 'SELECT DAY(import_time) as day, COUNT(*) as count
2601 FROM pony_ftp
2602 WHERE (url_type=\'http\' OR url_type=\'https\') AND (import_time >= DATE_SUB(\''.mysql_real_escape_string(mysql_now_date()).'\',INTERVAL '.$time.' SECOND))
2603 GROUP BY DAY(import_time)';
2604
2605 $result = mysql_query($query, $this->db_link);
2606
2607 if (!$result)
2608 {
2609 $this->state = false;
2610 } else
2611 {
2612 while ($row = mysql_fetch_assoc($result))
2613 $out_array[intval($row['day'])] = $row['count'];
2614 return true;
2615 }
2616 return false;
2617 }
2618
2619 function get_report_count_last_month(&$out_array)
2620 {
2621 if (!$this->state)
2622 return false;
2623
2624 $time = $this->get_last_30_days();
2625 $query = 'SELECT DAY(import_time) as day, COUNT(*) as count
2626 FROM pony_report
2627 WHERE (import_time >= DATE_SUB(\''.mysql_real_escape_string(mysql_now_date()).'\',INTERVAL '.$time.' SECOND))
2628 GROUP BY DAY(import_time)';
2629
2630 $result = mysql_query($query, $this->db_link);
2631
2632 if (!$result)
2633 {
2634 $this->state = false;
2635 } else
2636 {
2637 while ($row = mysql_fetch_assoc($result))
2638 $out_array[intval($row['day'])] = $row['count'];
2639 return true;
2640 }
2641 return false;
2642 }
2643
2644 function get_url_password_stats(&$out_array, $offset)
2645 {
2646 if (!$this->state)
2647 return false;
2648
2649 $query = 'SELECT url_type, COUNT(*) as count
2650 FROM pony_ftp
2651 WHERE ftp_id >= '.mysql_real_escape_string($offset).'
2652 GROUP BY url_type
2653 ';
2654
2655 $result = mysql_query($query, $this->db_link);
2656
2657 if (!$result)
2658 {
2659 $this->state = false;
2660 } else
2661 {
2662 while ($row = mysql_fetch_assoc($result))
2663 $out_array[$row['url_type']] = $row['count'];
2664 return true;
2665 }
2666 return false;
2667 }
2668
2669 function get_ftp_clients_stats(&$out_array)
2670 {
2671 if (!$this->state)
2672 return false;
2673
2674 $query = 'SELECT ftp_client, COUNT(*) as count
2675 FROM pony_ftp
2676
2677 WHERE (url_type=\'ftp\') OR (url_type=\'ssh\')
2678
2679 GROUP BY ftp_client
2680 ORDER BY count DESC
2681
2682 ';
2683
2684 $result = mysql_query($query, $this->db_link);
2685
2686 if (!$result)
2687 {
2688 $this->state = false;
2689 } else
2690 {
2691 while ($row = mysql_fetch_assoc($result))
2692 $out_array[$row['ftp_client']] = $row['count'];
2693 return true;
2694 }
2695 return false;
2696 }
2697
2698 function get_http_clients_stats(&$out_array)
2699 {
2700 if (!$this->state)
2701 return false;
2702
2703 $query = 'SELECT ftp_client, COUNT(*) as count
2704 FROM pony_ftp
2705
2706 WHERE (url_type=\'http\') OR (url_type=\'https\')
2707
2708 GROUP BY ftp_client
2709 ORDER BY count DESC
2710
2711 ';
2712
2713 $result = mysql_query($query, $this->db_link);
2714
2715 if (!$result)
2716 {
2717 $this->state = false;
2718 } else
2719 {
2720 while ($row = mysql_fetch_assoc($result))
2721 $out_array[$row['ftp_client']] = $row['count'];
2722 return true;
2723 }
2724 return false;
2725 }
2726
2727 function get_email_clients_stats(&$out_array)
2728 {
2729 if (!$this->state)
2730 return false;
2731
2732 $query = 'SELECT email_client, COUNT(*) as count
2733 FROM pony_email
2734
2735 GROUP BY email_client
2736 ORDER BY count DESC
2737
2738 ';
2739
2740 $result = mysql_query($query, $this->db_link);
2741
2742 if (!$result)
2743 {
2744 $this->state = false;
2745 } else
2746 {
2747 while ($row = mysql_fetch_assoc($result))
2748 $out_array[$row['email_client']] = $row['count'];
2749 return true;
2750 }
2751 return false;
2752 }
2753
2754 function get_os_stats(&$out_array)
2755 {
2756 if (!$this->state)
2757 return false;
2758
2759 $query = "SELECT report_os_name, COUNT(*) as count
2760 FROM pony_report
2761
2762 WHERE parsed='1'
2763 GROUP BY report_os_name
2764 ORDER BY count DESC
2765
2766 ";
2767
2768 $result = mysql_query($query, $this->db_link);
2769
2770 if (!$result)
2771 {
2772 $this->state = false;
2773 } else
2774 {
2775 while ($row = mysql_fetch_assoc($result))
2776 $out_array[$row['report_os_name']] = $row['count'];
2777 return true;
2778 }
2779 return false;
2780 }
2781
2782 function get_email_country_stats(&$out_array)
2783 {
2784 if (!$this->state)
2785 return false;
2786
2787 $query = "SELECT report_country, COUNT(*) as email_count FROM pony_email INNER JOIN pony_report USING (report_id)
2788
2789 WHERE (parsed='1')
2790 GROUP BY report_country
2791 ";
2792
2793 $result = mysql_query($query, $this->db_link);
2794
2795 if (!$result)
2796 {
2797 $this->state = false;
2798 } else
2799 {
2800 while ($row = mysql_fetch_assoc($result))
2801 $out_array[$row['report_country']] = array('email_count'=>$row['email_count']);
2802 return true;
2803 }
2804 return false;
2805 }
2806
2807 function get_cert_country_stats(&$out_array)
2808 {
2809 if (!$this->state)
2810 return false;
2811
2812 $query = "SELECT report_country, COUNT(*) as cert_count FROM pony_cert INNER JOIN pony_report USING (report_id)
2813
2814 WHERE (parsed='1')
2815 GROUP BY report_country
2816 ";
2817
2818 $result = mysql_query($query, $this->db_link);
2819
2820 if (!$result)
2821 {
2822 $this->state = false;
2823 } else
2824 {
2825 while ($row = mysql_fetch_assoc($result))
2826 $out_array[$row['report_country']] = array('cert_count'=>$row['cert_count']);
2827 return true;
2828 }
2829 return false;
2830 }
2831
2832 function get_ftp_country_stats(&$out_array)
2833 {
2834 if (!$this->state)
2835 return false;
2836
2837 $query = "SELECT report_country, COUNT(*) as ftp_count FROM pony_ftp INNER JOIN pony_report USING (report_id)
2838
2839 WHERE (url_type='ftp' OR url_type='ssh') AND (parsed='1')
2840 GROUP BY report_country
2841 ";
2842
2843 $result = mysql_query($query, $this->db_link);
2844
2845 if (!$result)
2846 {
2847 $this->state = false;
2848 } else
2849 {
2850 while ($row = mysql_fetch_assoc($result))
2851 $out_array[$row['report_country']] = array('ftp_count'=>$row['ftp_count']);
2852 return true;
2853 }
2854 return false;
2855 }
2856
2857 function get_all_country_stats(&$out_array)
2858 {
2859 if (!$this->state)
2860 return false;
2861
2862 $query = "SELECT report_country, COUNT(*) as ftp_count FROM pony_ftp INNER JOIN pony_report USING (report_id)
2863
2864 WHERE parsed='1'
2865 GROUP BY report_country
2866 ";
2867
2868 $result = mysql_query($query, $this->db_link);
2869
2870 if (!$result)
2871 {
2872 $this->state = false;
2873 } else
2874 {
2875 while ($row = mysql_fetch_assoc($result))
2876 $out_array[$row['report_country']] = array('ftp_count'=>$row['ftp_count']);
2877 return true;
2878 }
2879 return false;
2880 }
2881
2882 function get_country_stats(&$out_array)
2883 {
2884 if (!$this->state)
2885 return false;
2886
2887 $query = "SELECT report_country, COUNT(*) as count
2888 FROM pony_report
2889
2890 WHERE parsed='1'
2891 GROUP BY report_country
2892 ORDER BY count DESC, import_time ASC
2893 ";
2894
2895 $result = mysql_query($query, $this->db_link);
2896
2897 if (!$result)
2898 {
2899 $this->state = false;
2900 } else
2901 {
2902 while ($row = mysql_fetch_assoc($result))
2903 $out_array[$row['report_country']] = array('report_count'=>$row['count']);
2904 return true;
2905 }
2906 return false;
2907 }
2908
2909 function get_64bit_stats(&$out_array)
2910 {
2911 if (!$this->state)
2912 return false;
2913
2914 $query = "SELECT report_is_win64, COUNT(*) as count
2915 FROM pony_report
2916
2917 WHERE parsed='1'
2918 GROUP BY report_is_win64
2919 ORDER BY count DESC
2920
2921 ";
2922
2923 $result = mysql_query($query, $this->db_link);
2924
2925 if (!$result)
2926 {
2927 $this->state = false;
2928 } else
2929 {
2930 while ($row = mysql_fetch_assoc($result))
2931 $out_array[$row['report_is_win64']] = $row['count'];
2932 return true;
2933 }
2934 return false;
2935 }
2936
2937 function get_admin_stats(&$out_array)
2938 {
2939 if (!$this->state)
2940 return false;
2941
2942 $query = "SELECT report_admin, COUNT(*) as count
2943 FROM pony_report
2944
2945 WHERE parsed='1'
2946 GROUP BY report_admin
2947 ORDER BY count DESC
2948
2949 ";
2950
2951 $result = mysql_query($query, $this->db_link);
2952
2953 if (!$result)
2954 {
2955 $this->state = false;
2956 } else
2957 {
2958 while ($row = mysql_fetch_assoc($result))
2959 $out_array[$row['report_admin']] = $row['count'];
2960 return true;
2961 }
2962 return false;
2963 }
2964
2965 function get_duplicate_report_count($offset = 0)
2966 {
2967 if (!$this->state)
2968 return false;
2969
2970 $offset = intval($offset);
2971 $query = sprintf("SELECT COUNT(*) as count
2972 FROM pony_system_log
2973
2974 WHERE (log_source='%s' AND log_line='NOTIFY_GATE_DUPLICATE_REPORT') AND (log_id >= '%s')
2975 ", mysql_real_escape_string(CLOG_SOURCE_GATE), mysql_real_escape_string($offset));
2976
2977 $result = mysql_query($query, $this->db_link);
2978
2979 if (!$result)
2980 {
2981 $this->state = false;
2982 } else
2983 {
2984 if ($row = mysql_fetch_assoc($result))
2985 return $row['count'];
2986 }
2987 return false;
2988 }
2989
2990 function get_admin_name()
2991 {
2992 $query = sprintf("SELECT username FROM pony_user WHERE privileges='admin_all' LIMIT 1");
2993 $result = mysql_query($query, $this->db_link);
2994
2995 if (!$result)
2996 {
2997 $this->state = false;
2998 } else
2999 {
3000 if ($row = mysql_fetch_assoc($result))
3001 {
3002 return $row['username'];
3003 }
3004 }
3005 return false;
3006 }
3007
3008 function get_option($option_name, $user_name = '', $default = '')
3009 {
3010 $user_name = trim($user_name);
3011 if (!$this->state)
3012 return $default;
3013
3014 if ($user_name == '')
3015 {
3016 $user_name = $this->get_admin_name();
3017 if ($user_name === false)
3018 return $default;
3019 }
3020
3021 $query = sprintf("SELECT settings FROM pony_user WHERE username='%s'",
3022 mysql_real_escape_string($user_name));
3023 $result = mysql_query($query, $this->db_link);
3024
3025 if (!$result)
3026 {
3027 $this->state = false;
3028 } else
3029 {
3030 if ($row = mysql_fetch_assoc($result))
3031 {
3032 $options = $row['settings'];
3033 $ini_array = parse_ini($options);
3034 if (is_array($ini_array) && isset($ini_array[$option_name]))
3035 return trim(base64_decode(strtr($ini_array[$option_name], '-_,', '+/='), true));
3036 else
3037 return $default;
3038 }
3039 }
3040 return $default;
3041 }
3042
3043 function get_multi_option($option_name, $option_count, $user_name = '')
3044 {
3045 $return_str = $this->get_option($option_name, $user_name);
3046 if (!$return_str)
3047 $return_str = '0';
3048 $return_array = explode('|', $return_str);
3049 while (count($return_array) < $option_count)
3050 {
3051 $return_array[] = '0';
3052 }
3053 return $return_array;
3054 }
3055
3056 function set_multi_option($option_name, $option_values, $user_name = '')
3057 {
3058 return $this->set_option($option_name, implode("|", $option_values), $user_name);
3059 }
3060
3061 function set_option($option_name, $option_value, $user_name = '')
3062 {
3063 $user_name = trim($user_name);
3064 $option_name = trim($option_name);
3065 $option_value = trim($option_value);
3066 if (!$this->state)
3067 return false;
3068
3069 if ($user_name == '')
3070 {
3071 $user_name = $this->get_admin_name();
3072 if ($user_name === false)
3073 return false;
3074 }
3075
3076 $query = sprintf("SELECT settings FROM pony_user WHERE username='%s'",
3077 mysql_real_escape_string($user_name));
3078 $result = mysql_query($query, $this->db_link);
3079
3080 if (!$result)
3081 {
3082 $this->state = false;
3083 return false;
3084 }
3085
3086 if ($row = mysql_fetch_assoc($result))
3087 {
3088 $settings = $row['settings'];
3089 } else
3090 return false;
3091
3092 if ($option_name == '')
3093 $option_name = 'default';
3094
3095 $ini_array = parse_ini($settings);
3096 $ini_array[$option_name] = strtr(base64_encode($option_value), '+/=', '-_,');
3097 $ini_string = collect_ini_array($ini_array);
3098
3099 $query = sprintf("UPDATE pony_user SET settings='%s' WHERE username='%s'",
3100 mysql_real_escape_string($ini_string),
3101 mysql_real_escape_string($user_name));
3102 $result = mysql_query($query, $this->db_link);
3103
3104 if (!$result)
3105 {
3106 $this->state = false;
3107 } else
3108 {
3109 return true;
3110 }
3111 return false;
3112 }
3113
3114 function optimize_table($table_name)
3115 {
3116 $table_name = trim($table_name);
3117 if (!$this->state || !strlen($table_name))
3118 return false;
3119
3120 $query = sprintf('OPTIMIZE TABLE %s', mysql_real_escape_string($table_name));
3121 $result = mysql_query($query, $this->db_link);
3122
3123 if (!$result)
3124 {
3125 $this->state = false;
3126 } else
3127 {
3128 return true;
3129 }
3130 return false;
3131 }
3132
3133 function drop_table($table_name)
3134 {
3135 $table_name = trim($table_name);
3136 if (!$this->state || !strlen($table_name))
3137 return false;
3138
3139 $query = sprintf('DROP TABLE IF EXISTS %s', mysql_real_escape_string($table_name));
3140 $result = mysql_query($query, $this->db_link);
3141
3142 if (!$result)
3143 {
3144 $this->state = false;
3145 } else
3146 {
3147 return true;
3148 }
3149 return false;
3150 }
3151
3152 function report_remove_errors($report_id)
3153 {
3154 $report_id = intval($report_id);
3155
3156 if (!$this->state || $report_id <= 0)
3157 return false;
3158
3159 $query = sprintf("DELETE FROM pony_system_log WHERE report_id='%s'", mysql_real_escape_string($report_id));
3160 $result = mysql_query($query, $this->db_link);
3161
3162 if (!$result)
3163 {
3164 $this->state = false;
3165 } else
3166 {
3167 return true;
3168 }
3169 return false;
3170 }
3171
3172 function get_auto_value($table_name)
3173 {
3174 $table_name = trim($table_name);
3175 if (!$this->state || !strlen($table_name))
3176 return false;
3177
3178 $query = sprintf('SHOW TABLE STATUS LIKE \'%s\'', mysql_real_escape_string($table_name));
3179 $result = mysql_query($query, $this->db_link);
3180 if (!$result)
3181 {
3182 $this->state = false;
3183 } else
3184 {
3185 if ($row = mysql_fetch_assoc($result))
3186 {
3187 return $row['Auto_increment'];
3188 }
3189 }
3190 return false;
3191 }
3192
3193 function get_offset_value_count($query, $value_id, $table_name)
3194 {
3195 $table_name = trim($table_name);
3196 if (!$this->state || !strlen($table_name))
3197 return false;
3198
3199 $query = sprintf('SELECT MIN(%s) as idx, COUNT(*) as count FROM `%s` ', mysql_real_escape_string($value_id), mysql_real_escape_string($table_name)).$query;
3200 $result = mysql_query($query, $this->db_link);
3201 if (!$result)
3202 {
3203 $this->state = false;
3204 } else
3205 {
3206 if ($row = mysql_fetch_assoc($result))
3207 {
3208 return array($row['idx'], $row['count']);
3209 }
3210 }
3211 return false;
3212 }
3213
3214 function lock_all_tables()
3215 {
3216 if (!$this->state)
3217 return false;
3218
3219 $query = "LOCK TABLES ".implode(' WRITE, ', $this->required_tables)." WRITE";
3220 $result = mysql_query($query, $this->db_link);
3221 if (!$result)
3222 {
3223 $this->state = false;
3224 } else
3225 {
3226 return true;
3227 }
3228 return false;
3229 }
3230
3231 function unlock_all_tables()
3232 {
3233 if (!$this->state)
3234 return false;
3235
3236 $query = "UNLOCK TABLES";
3237 $result = mysql_query($query, $this->db_link);
3238 if (!$result)
3239 {
3240 $this->state = false;
3241 } else
3242 {
3243 return true;
3244 }
3245 return false;
3246 }
3247
3248 function report_remove($report_id)
3249 {
3250 $report_id = intval($report_id);
3251
3252 if (!$this->state || $report_id <= 0)
3253 return false;
3254
3255
3256 $query = sprintf("DELETE FROM pony_report_data USING pony_report_data INNER JOIN pony_report USING (data_id) WHERE report_id='%s'", mysql_real_escape_string($report_id));
3257 $result = mysql_query($query, $this->db_link);
3258
3259 if (!$result)
3260 {
3261 $this->state = false;
3262 } else
3263 {
3264 $query = sprintf("DELETE FROM pony_report WHERE report_id='%s'", mysql_real_escape_string($report_id));
3265 $result = mysql_query($query, $this->db_link);
3266
3267 if (!$result)
3268 {
3269 $this->state = false;
3270 } else
3271 {
3272 return true;
3273 }
3274 }
3275 return false;
3276 }
3277}