PageRenderTime 55ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/jorge/trunk/class.db.php

https://github.com/stockr-labs/ejabberd-modules
PHP | 3096 lines | 2289 code | 770 blank | 37 comment | 119 complexity | 9299eb25185afcd817a249c005b803b6 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?
  2. /*
  3. Copyright (C) 2009 Zbigniew Zolkiewski
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. ###########################################################################
  16. This class provide usefull methods for managing message archives created with mod_logdb.
  17. This is pre-release. Do not use outside project Jorge.
  18. Documentation is available in API.txt.
  19. NOTICE: By default class discards any query with is execuded after query error has occured. This is for debug purposes only.
  20. */
  21. class db_manager {
  22. private $db_host;
  23. private $db_name;
  24. private $db_user;
  25. private $db_password;
  26. private $db_driver;
  27. private $xmpp_host;
  28. private $vhost;
  29. private $messages_table = "logdb_messages_";
  30. private $is_error = false;
  31. private $id_query;
  32. private $query_type;
  33. private $is_debug = false;
  34. private $user_id = null;
  35. private $peer_name_id = null;
  36. private $peer_server_id = null;
  37. private $tslice = null;
  38. private $time_start = null;
  39. private $time_result = null;
  40. private $user_query = null;
  41. private $ignore_id = null;
  42. private $spec_ignore = false;
  43. private $ext_idx;
  44. public $result;
  45. public function __construct($db_host,$db_name,$db_user,$db_password,$db_driver,$xmpp_host = null) {
  46. $this->setData($db_host,$db_name,$db_user,$db_password,$db_driver,$xmpp_host);
  47. }
  48. private function setData($db_host,$db_name,$db_user,$db_password,$db_driver,$xmpp_host) {
  49. $this->db_host = $db_host;
  50. $this->db_name = $db_name;
  51. $this->db_user = $db_user;
  52. $this->db_password = $db_password;
  53. $this->db_driver = $db_driver;
  54. $this->xmpp_host = $this->sql_validate($xmpp_host,"string");
  55. $this->vhost = str_replace("_",".", $this->sql_validate($xmpp_host,"string"));
  56. try {
  57. $this->db_connect();
  58. }
  59. catch(Exception $e) {
  60. echo "<br>Exception: ".$e->getMessage();
  61. echo ", Code: ".$e->getCode();
  62. }
  63. if ($this->vhost) {
  64. $this->set_ignore_id();
  65. }
  66. }
  67. private function db_mysql() {
  68. $conn = mysql_connect("$this->db_host", "$this->db_user", "$this->db_password");
  69. if (!$conn) {
  70. return false;
  71. }
  72. if (mysql_select_db($this->db_name)) {
  73. return true;
  74. }
  75. else {
  76. return false;
  77. }
  78. }
  79. private function do_query($query) {
  80. $this->show_debug_info($query, $time = false);
  81. if ($this->is_error === false) {
  82. $this->time_start();
  83. $result = mysql_query($query);
  84. $this->time_end();
  85. $this->show_debug_info($query = null, $time = true);
  86. }
  87. elseif($this->is_error === true) {
  88. if ($this->is_debug === true) {
  89. if ($this->is_debug === true) {
  90. throw new Exception("Error before queryID:".$this->id_query,3);
  91. }
  92. }
  93. return false;
  94. }
  95. if ($result === false ) {
  96. $this->is_error = true;
  97. if ($this->is_debug === true) {
  98. throw new Exception("Query error in QueryID:".$this->id_query,2);
  99. }
  100. return false;
  101. }
  102. else {
  103. if ($this->query_type === "select" OR $this->query_type="create_table") {
  104. return $result;
  105. }
  106. elseif($this->query_type === "update" OR $this->query_type === "insert" OR $this->query_type === "replace") {
  107. return mysql_affected_rows();
  108. }
  109. elseif($this->query_type === "delete") {
  110. return $result;
  111. }
  112. elseif($this->query_type === "transaction") {
  113. return $result;
  114. }
  115. }
  116. return false;
  117. }
  118. private function db_query($query) {
  119. try {
  120. $result = $this->do_query($query);
  121. }
  122. catch (Exception $e) {
  123. echo "<br>Exception: ".$e->getMessage();
  124. echo ", Code: ".$e->getCode();
  125. return false;
  126. }
  127. return $result;
  128. }
  129. private function db_connect() {
  130. if ($this->db_driver === "mysql") {
  131. if ($this->db_mysql() === true) {
  132. return true;
  133. }
  134. else {
  135. $this->is_error = true;
  136. if ($this->is_debug === true) {
  137. throw new Exception("<br>DB Connection failed!",1);
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. private function select($query,$return_type = null) {
  144. $this->query_type="select";
  145. if (strpos(strtolower($query),"select") === 0) {
  146. try{
  147. $this->result = $this->db_query($query);
  148. }
  149. catch(Exception $e) {
  150. echo "<br>Exception: ".$e->getMessage();
  151. echo ", Code: ".$e->getCode();
  152. }
  153. if($this->is_error===false) {
  154. if($return_type === null) {
  155. $this->result = mysql_fetch_object($this->result);
  156. }
  157. elseif($return_type === "raw") {
  158. return true;
  159. }
  160. return true;
  161. }
  162. else{
  163. return false;
  164. }
  165. }
  166. else {
  167. return false;
  168. }
  169. }
  170. private function update($query) {
  171. $this->query_type = "update";
  172. if (strpos(strtolower($query),"update") === 0) {
  173. try{
  174. $this->result = $this->db_query($query);
  175. }
  176. catch(Exception $e) {
  177. echo "<br>Exception: ".$e->getMessage();
  178. echo ", Code: ".$e->getCode();
  179. }
  180. if($this->is_error===false) {
  181. return true;
  182. }
  183. else{
  184. return false;
  185. }
  186. }
  187. else {
  188. return false;
  189. }
  190. }
  191. private function insert($query) {
  192. $this->query_type = "insert";
  193. if (strpos(strtolower($query),"insert") === 0) {
  194. try{
  195. $this->result = $this->db_query($query);
  196. }
  197. catch(Exception $e) {
  198. echo "<br>Exception: ".$e->getMessage();
  199. echo ", Code: ".$e->getCode();
  200. }
  201. if($this->is_error===false) {
  202. return true;
  203. }
  204. else{
  205. return false;
  206. }
  207. }
  208. else {
  209. return false;
  210. }
  211. }
  212. private function delete($query) {
  213. $this->query_type = "delete";
  214. if (strpos(strtolower($query),"delete") === 0) {
  215. try{
  216. $this->result = $this->db_query($query);
  217. }
  218. catch(Exception $e) {
  219. echo "<br>Exception: ".$e->getMessage();
  220. echo ", Code: ".$e->getCode();
  221. }
  222. if ($this->is_error===false) {
  223. return true;
  224. }
  225. else {
  226. return false;
  227. }
  228. }
  229. else {
  230. return false;
  231. }
  232. }
  233. private function replace_q($query) {
  234. $this->query_type = "replace";
  235. if (strpos(strtolower($query),"replace") === 0) {
  236. try{
  237. $this->result = $this->db_query($query);
  238. }
  239. catch(Exception $e) {
  240. echo "<br>Exception: ".$e->getMessage();
  241. echo ", Code: ".$e->getCode();
  242. }
  243. if ($this->is_error === false) {
  244. return true;
  245. }
  246. else {
  247. return false;
  248. }
  249. }
  250. else {
  251. return false;
  252. }
  253. }
  254. public function begin() {
  255. $this->id_query = "Q001";
  256. $this->query_type = "transaction";
  257. return $this->db_query("begin");
  258. }
  259. public function commit() {
  260. $this->id_query = "Q002";
  261. $this->query_type = "transaction";
  262. return $this->db_query("commit");
  263. }
  264. public function rollback() {
  265. $this->id_query = "Q003";
  266. $this->query_type = "transaction";
  267. $this->is_error = false;
  268. return $this->db_query("rollback");
  269. }
  270. public function get_mylinks_count() {
  271. $this->id_query = "Q004";
  272. $this->vital_check();
  273. $query="SELECT
  274. count(id_link) as cnt
  275. FROM
  276. jorge_mylinks
  277. WHERE
  278. owner_id='".$this->user_id."'
  279. AND
  280. vhost='".$this->vhost."'
  281. AND
  282. ext is NULL
  283. ";
  284. return $this->select($query);
  285. }
  286. public function get_trash_count() {
  287. $this->id_query = "Q005";
  288. $this->vital_check();
  289. $query="SELECT
  290. count(*) as cnt
  291. FROM
  292. pending_del
  293. WHERE
  294. owner_id='".$this->user_id."'
  295. AND
  296. vhost='".$this->vhost."'
  297. ";
  298. return $this->select($query);
  299. }
  300. private function row_count($query) {
  301. $this->id_query = "Q006";
  302. $result = mysql_num_rows($this->db_query($query));
  303. if ($result === false) {
  304. return false;
  305. }
  306. else{
  307. $this->result = $result;
  308. return true;
  309. }
  310. }
  311. public function get_user_id($user) {
  312. $this->id_query = "Q007";
  313. $user = $this->sql_validate($user,"string");
  314. $query="SELECT
  315. user_id
  316. FROM
  317. `logdb_users_".$this->xmpp_host."`
  318. WHERE
  319. username = '$user'
  320. ";
  321. return $this->select($query);
  322. }
  323. public function get_user_name($user_id,$vhost = null) {
  324. $this->id_query = "Q008";
  325. $user_id = $this->sql_validate($user_id,"integer");
  326. if ($vhost !== null) {
  327. $vh = $this->vh($vhost,true);
  328. }
  329. else{
  330. $vh = $this->xmpp_host;
  331. }
  332. $query="SELECT
  333. username
  334. FROM
  335. `logdb_users_".$vh."`
  336. WHERE
  337. user_id = '$user_id'
  338. ";
  339. return $this->select($query);
  340. }
  341. public function get_server_id($server) {
  342. $this->id_query = "Q009";
  343. $server = $this->sql_validate($server,"string");
  344. $query="SELECT
  345. server_id
  346. FROM
  347. `logdb_servers_".$this->xmpp_host."`
  348. WHERE
  349. server = '$server'
  350. ";
  351. return $this->select($query);
  352. }
  353. public function get_server_name($server_id,$vhost = null){
  354. $this->id_query = "Q010";
  355. $server_id = $this->sql_validate($server_id,"integer");
  356. if ($vhost !== null) {
  357. $vh = $this->vh($vhost,true);
  358. }
  359. else{
  360. $vh = $this->xmpp_host;
  361. }
  362. $query="SELECT
  363. server as server_name
  364. FROM
  365. `logdb_servers_".$vh."`
  366. WHERE
  367. server_id = '$server_id'
  368. ";
  369. return $this->select($query);
  370. }
  371. public function get_resource_name($resource_id) {
  372. $this->id_query = "Q012";
  373. $resource_id = $this->sql_validate($resource_id,"integer");
  374. $query="SELECT
  375. resource as resource_name
  376. FROM
  377. `logdb_resources_".$this->xmpp_host."`
  378. WHERE
  379. resource_id = '$resource_id'
  380. ";
  381. return $this->select($query);
  382. }
  383. public function get_resource_id($resource) {
  384. $this->id_query = "Q013";
  385. $resource = $this->sql_validate($resource,"string");
  386. $query="SELECT
  387. resource_id
  388. FROM
  389. `logdb_resources_".$this->xmpp_host."`
  390. WHERE
  391. resource = '$resource'
  392. ";
  393. return $this->select($query);
  394. }
  395. public function get_user_talker_stats($peer_name_id,$peer_server_id){
  396. $this->id_query = "Q014";
  397. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  398. $query="SELECT
  399. at
  400. FROM
  401. `logdb_stats_".$this->xmpp_host."`
  402. WHERE
  403. owner_id='".$this->user_id."'
  404. AND
  405. peer_name_id='".$this->peer_name_id."'
  406. AND
  407. peer_server_id='".$this->peer_server_id."'
  408. ORDER BY
  409. str_to_date(at,'%Y-%m-%d')
  410. ASC
  411. ";
  412. $this->select($query,"raw");
  413. return $this->commit_select(array("at"));
  414. }
  415. public function get_num_lines($tslice,$peer_name_id,$peer_server_id) {
  416. $this->id_query = "Q015";
  417. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  418. $table = $this->construct_table($this->tslice);
  419. $query="SELECT
  420. count(timestamp) as cnt
  421. FROM
  422. `$table`
  423. WHERE
  424. owner_id = '".$this->user_id."'
  425. AND
  426. peer_name_id='".$this->peer_name_id."'
  427. AND
  428. peer_server_id='".$this->peer_server_id."'
  429. ";
  430. return $this->select($query);
  431. }
  432. public function is_log_enabled() {
  433. $this->id_query = "Q016";
  434. // Return false on non-digit characters, workaround for user not being in sql dictionary.
  435. if (!ctype_digit($this->user_id)) {
  436. return false;
  437. }
  438. $query="SELECT
  439. dolog_default as is_enabled
  440. FROM
  441. `logdb_settings_".$this->xmpp_host."`
  442. WHERE
  443. owner_id='".$this->user_id."'
  444. ";
  445. if ($this->select($query) === true) {
  446. if ($this->result->is_enabled === "0") {
  447. $this->result->is_enabled = false;
  448. return true;
  449. }
  450. elseif($this->result->is_enabled === "1") {
  451. $this->result->is_enabled = true;
  452. return true;
  453. }
  454. else{
  455. $this->result->is_enabled = null;
  456. return true;
  457. }
  458. }
  459. return false;
  460. }
  461. public function total_messages($vhost = null) {
  462. $this->id_query = "Q017";
  463. if ($vhost === null) {
  464. $vh = $this->xmpp_host;
  465. }
  466. else{
  467. $vh = $this->vh($vhost,true);
  468. }
  469. $query="SELECT
  470. sum(count) as total_messages,
  471. count(owner_id) as total_chats
  472. FROM
  473. `logdb_stats_".$vh."`
  474. ";
  475. $this->select($query,"raw");
  476. return $this->commit_select(array("total_messages","total_chats"));
  477. }
  478. public function total_chats() {
  479. $this->id_query = "Q018";
  480. $query="SELECT
  481. count(owner_id) as total_chats
  482. FROM
  483. `logdb_stats_".$this->xmpp_host."`
  484. ";
  485. return $this->select($query);
  486. }
  487. public function get_log_list() {
  488. $this->id_query = "Q019";
  489. $this->vital_check();
  490. $query="SELECT
  491. donotlog_list as donotlog
  492. FROM
  493. `logdb_settings_".$this->xmpp_host."`
  494. WHERE
  495. owner_id = '".$this->user_id."'
  496. ";
  497. $this->select($query);
  498. $split = explode("\n",$this->result->donotlog);
  499. $this->result = $split;
  500. return true;
  501. }
  502. public function set_log($bool) {
  503. $this->id_query = "Q020";
  504. $this->vital_check();
  505. if ($bool === true) {
  506. $val = 1;
  507. }
  508. elseif($bool === false) {
  509. $val = 0;
  510. }
  511. else{
  512. return false;
  513. }
  514. $query="UPDATE
  515. `logdb_settings_".$this->xmpp_host."`
  516. SET
  517. dolog_default = '$val'
  518. WHERE
  519. owner_id = '".$this->user_id."'
  520. ";
  521. return $this->update($query);
  522. }
  523. public function set_logger($event_id,$event_level,$extra = null) {
  524. $this->id_query = "Q021";
  525. $this->vital_check();
  526. $id_log_detail = $this->sql_validate($event_id,"integer");
  527. $id_log_level = $this->sql_validate($event_level,"integer");
  528. $extra = $this->sql_validate($extra,"string");
  529. $query="INSERT INTO
  530. jorge_logger (id_user,id_log_detail,id_log_level,log_time,extra,vhost)
  531. VALUES
  532. ('".$this->user_id."','$id_log_detail','$id_log_level',NOW(),'$extra','".$this->vhost."')
  533. ";
  534. return $this->insert($query);
  535. }
  536. public function get_user_stats_drop_down() {
  537. $this->id_query = "Q022";
  538. $this->vital_check();
  539. if ($this->spec_ignore === true) {
  540. $sql = "AND peer_name_id != '".$this->ignore_id."'";
  541. }
  542. $query="SELECT
  543. substring(at,1,7) as at_send,
  544. at
  545. FROM
  546. `logdb_stats_".$this->xmpp_host."`
  547. WHERE
  548. owner_id = '".$this->user_id."'
  549. $sql
  550. GROUP BY
  551. substring(at,1,7)
  552. ORDER BY
  553. str_to_date(at,'%Y-%m-%d')
  554. DESC
  555. ";
  556. $this->select($query,"raw");
  557. return $this->commit_select(array("at_send","at"));
  558. }
  559. public function get_user_stats_calendar($mo) {
  560. $this->id_query = "Q023";
  561. $this->vital_check();
  562. $mo = $this->sql_validate($mo,"string");
  563. if ($this->spec_ignore === true) {
  564. $sql = "AND peer_name_id != '".$this->ignore_id."'";
  565. }
  566. $query="SELECT
  567. distinct(substring(at,8,9)) as days
  568. FROM
  569. `logdb_stats_".$this->xmpp_host."`
  570. WHERE
  571. owner_id = '".$this->user_id."'
  572. AND
  573. at like '$mo-%'
  574. $sql
  575. ORDER BY
  576. str_to_date(at,'%Y-%m-%d')
  577. DESC
  578. ";
  579. $this->select($query,"raw");
  580. return $this->commit_select(array("days"));
  581. }
  582. public function get_user_chats($tslice) {
  583. $this->id_query = "Q024";
  584. $this->vital_check();
  585. $xmpp_host = $this->xmpp_host;
  586. $tslice_table = $this->sql_validate($tslice,"string");
  587. $query="SELECT
  588. a.username,
  589. b.server as server_name,
  590. c.peer_name_id as todaytalk,
  591. c.peer_server_id as server,
  592. c.count as lcount
  593. FROM
  594. `logdb_users_$xmpp_host` a,
  595. `logdb_servers_$xmpp_host` b,
  596. `logdb_stats_$xmpp_host` c
  597. WHERE
  598. c.owner_id = '".$this->user_id."'
  599. AND
  600. a.user_id=c.peer_name_id
  601. AND
  602. b.server_id=c.peer_server_id
  603. AND
  604. c.at = '$tslice'
  605. ORDER BY
  606. lower(username)
  607. ";
  608. $this->select($query,"raw");
  609. return $this->commit_select(array("username","server_name","todaytalk","server","lcount"));
  610. }
  611. public function get_user_chat($tslice,$peer_name_id,$peer_server_id,$resource_id = null,$start = null,$num_lines = null) {
  612. $this->id_query = "Q025";
  613. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  614. if ($resource_id !== null) {
  615. $resource_id = $this->sql_validate($resource_id,"integer");
  616. $sql = "AND (peer_resource_id='$resource_id' OR peer_resource_id='1')";
  617. }
  618. else{
  619. settype($sql,"null");
  620. }
  621. $offset_start = $start;
  622. if ($offset_start === null) {
  623. $offset_start = "0";
  624. }
  625. $offset_end = $start + $num_lines;
  626. $offset_start = $this->sql_validate($offset_start,"integer");
  627. $offset_end = $this->sql_validate($offset_end,"integer");
  628. $tslice_table = $this->construct_table($this->tslice);
  629. $query="SELECT
  630. from_unixtime(timestamp+0) as ts,
  631. direction,
  632. type,
  633. subject,
  634. peer_name_id,
  635. peer_server_id,
  636. peer_resource_id,
  637. body
  638. FROM
  639. `$tslice_table`
  640. FORCE INDEX
  641. (search_i)
  642. WHERE
  643. owner_id = '".$this->user_id."'
  644. AND
  645. peer_name_id='".$this->peer_name_id."'
  646. AND
  647. peer_server_id='".$this->peer_server_id."'
  648. $sql
  649. AND
  650. ext is NULL
  651. ORDER BY
  652. ts
  653. LIMIT
  654. $offset_start,$offset_end
  655. ";
  656. $this->select($query,"raw");
  657. return $this->commit_select(array("ts","direction","type","subject","peer_name_id","peer_server_id","peer_resource_id","body"));
  658. }
  659. public function get_uniq_chat_dates($limit_start = null, $limit_end = null, $limited = false, $start = null, $peer_name_id = null, $peer_server_id = null) {
  660. $this->id_query = "Q026";
  661. $this->vital_check();
  662. $user_id = $this->user_id;
  663. $xmpp_host = $this->xmpp_host;
  664. if ($limit_start !== null AND $limit_end !== null) {
  665. $limit_start = $this->sql_validate($limit_start,"date");
  666. $limit_end = $this->sql_validate($limit_end,"date");
  667. $sql=" AND str_to_date(at,'%Y-%m-%d') >= str_to_date('$limit_start','%Y-%m-%d') AND str_to_date(at,'%Y-%m-%d') <= str_to_date('$limit_end','%Y-%m-%d')";
  668. }
  669. else{
  670. settype($sql,"null");
  671. }
  672. if ($limited === true) {
  673. if ($start == "" OR $start === null) {
  674. $start = "0";
  675. }
  676. $start = $this->sql_validate($start,"integer");
  677. $sql2=" limit $start,10000";
  678. }
  679. else{
  680. settype($sql2,"null");
  681. }
  682. if ($peer_name_id !== null AND $peer_server_id !== null) {
  683. $peer_name_id = $this->sql_validate($peer_name_id,"integer");
  684. $peer_server_id = $this->sql_validate($peer_server_id,"integer");
  685. $sql3 = "AND peer_name_id = '$peer_name_id' AND peer_server_id = '$peer_server_id'";
  686. }
  687. else{
  688. settype($sql3,"null");
  689. }
  690. $query="SELECT
  691. distinct(at)
  692. FROM
  693. `logdb_stats_$xmpp_host`
  694. WHERE
  695. owner_id='$user_id' $sql3 $sql
  696. ORDER BY
  697. str_to_date(at,'%Y-%m-%d')
  698. ASC
  699. $sql2
  700. ";
  701. $this->select($query,"raw");
  702. return $this->commit_select(array("at"));
  703. }
  704. public function check_thread($tslice,$peer_name_id,$peer_server_id,$begin_hour,$end_hour) {
  705. $this->id_query = "Q027";
  706. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  707. $xmpp_host = $this->xmpp_host;
  708. $tslice_table = $this->construct_table($this->tslice);
  709. $query="SELECT
  710. 1
  711. FROM
  712. `$tslice_table`
  713. WHERE
  714. owner_id='".$this->user_id."'
  715. AND
  716. peer_name_id='".$this->peer_name_id."'
  717. AND
  718. peer_server_id='".$this->peer_server_id."'
  719. AND
  720. from_unixtime(timestamp) >= str_to_date('".$this->tslice." $begin_hour','%Y-%m-%d %H:%i:%s')
  721. AND
  722. from_unixtime(timestamp) <= str_to_date('".$this->tslice." $end_hour','%Y-%m-%d %H:%i:%s')
  723. ORDER BY
  724. from_unixtime(timestamp)
  725. ";
  726. return $this->row_count($query);
  727. }
  728. public function get_chat_map($peer_name_id,$peer_server_id) {
  729. $this->id_query = "Q028";
  730. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  731. $query="SELECT
  732. substring(at,1,7) as at
  733. FROM
  734. `logdb_stats_".$this->xmpp_host."`
  735. WHERE
  736. owner_id='".$this->user_id."'
  737. AND
  738. peer_name_id='".$this->peer_name_id."'
  739. AND
  740. peer_server_id='".$this->peer_server_id."'
  741. GROUP BY
  742. substring(at,1,7)
  743. ORDER BY
  744. str_to_date(at,'%Y-%m-%d')
  745. ASC
  746. ";
  747. $this->select($query,"raw");
  748. return $this->commit_select(array("at"));
  749. }
  750. public function get_chat_map_specyfic($peer_name_id,$peer_server_id,$month) {
  751. $this->id_query = "Q029";
  752. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  753. $mo = $this->sql_validate($month,"string");
  754. $query="SELECT
  755. at
  756. FROM
  757. `logdb_stats_".$this->xmpp_host."`
  758. WHERE
  759. owner_id='".$this->user_id."'
  760. AND
  761. peer_name_id='".$this->peer_name_id."'
  762. AND
  763. peer_server_id='".$this->peer_server_id."'
  764. AND
  765. at like '$mo-%'
  766. ";
  767. $this->select($query,"raw");
  768. return $this->commit_select(array("at"));
  769. }
  770. public function add_mylink($peer_name_id,$peer_server_id,$link_date,$link,$desc) {
  771. $this->id_query = "Q030";
  772. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  773. $datat = $this->sql_validate($link_date,"string");
  774. $lnk = $this->sql_validate($link,"string");
  775. $desc = $this->sql_validate($desc,"string");
  776. $query="INSERT INTO
  777. jorge_mylinks (owner_id,peer_name_id,peer_server_id,datat,link,description,vhost)
  778. VALUES (
  779. '".$this->user_id."',
  780. '".$this->peer_name_id."',
  781. '".$this->peer_server_id."',
  782. '$datat',
  783. '$lnk',
  784. '$desc',
  785. '".$this->vhost."'
  786. )
  787. ";
  788. return $this->insert($query);
  789. }
  790. public function del_mylink($link_id) {
  791. $this->id_query = "Q031";
  792. $this->vital_check();
  793. $link_id = $this->sql_validate($link_id,"integer");
  794. $query="DELETE FROM
  795. jorge_mylinks
  796. WHERE
  797. owner_id='".$this->user_id."'
  798. AND
  799. vhost='".$this->vhost."'
  800. AND
  801. id_link='$link_id'
  802. ";
  803. return $this->delete($query);
  804. }
  805. public function get_mylink() {
  806. $this->id_query = "Q032";
  807. $this->vital_check();
  808. $query="SELECT
  809. id_link,
  810. peer_name_id,
  811. peer_server_id,
  812. datat,
  813. link,
  814. description,
  815. ext
  816. FROM
  817. jorge_mylinks
  818. WHERE
  819. owner_id='".$this->user_id."'
  820. AND
  821. vhost='".$this->vhost."'
  822. AND
  823. ext is NULL
  824. ORDER BY
  825. str_to_date(datat,'%Y-%m-%d')
  826. DESC
  827. ";
  828. $this->select($query,"raw");
  829. return $this->commit_select(array("id_link","peer_name_id","peer_server_id","datat","link","description","ext"));
  830. }
  831. public function update_log_list($log_list) {
  832. $this->id_query = "Q033";
  833. $this->vital_check();
  834. $log_list = $this->sql_validate($log_list,"string");
  835. $query="UPDATE
  836. `logdb_settings_".$this->xmpp_host."`
  837. SET
  838. donotlog_list='$log_list'
  839. WHERE
  840. owner_id='".$this->user_id."'
  841. ";
  842. return $this->update($query);
  843. }
  844. public function logger_get_events($event_id = null,$level_id = null, $offset = null,$lang = null) {
  845. $this->id_query = "Q034";
  846. $this->vital_check();
  847. $offset = $this->sql_validate($offset,"integer");
  848. if ($event_id !== null) {
  849. $event_id = $this->sql_validate($event_id,"integer");
  850. $sql_1 = "and id_log_detail='$event_id'";
  851. }
  852. if ($level_id !== null) {
  853. $level_id = $this->sql_validate($level_id,"integer");
  854. $sql_2 = "and id_log_level='$level_id'";
  855. }
  856. $query="SELECT
  857. b.id_event,
  858. b.event AS event,
  859. c.level AS level,
  860. c.id_level,
  861. a.log_time,
  862. a.extra
  863. FROM
  864. jorge_logger a,
  865. jorge_logger_dict b,
  866. jorge_logger_level_dict c
  867. WHERE
  868. a.id_log_detail=b.id_event
  869. AND
  870. c.id_level=a.id_log_level
  871. AND
  872. id_user='".$this->user_id."'
  873. AND
  874. a.vhost='".$this->vhost."'
  875. AND
  876. b.lang = '$lang'
  877. AND
  878. c.lang = '$lang'
  879. $sql_1
  880. $sql_2
  881. ORDER BY
  882. log_time
  883. DESC LIMIT
  884. $offset,300
  885. ";
  886. $this->select($query,"raw");
  887. return $this->commit_select(array("id_event","event","level","id_level","log_time","extra"));
  888. }
  889. public function get_num_events($event_id = null,$level_id = null) {
  890. $this->id_query = "Q035";
  891. $this->vital_check();
  892. if ($event_id !== null) {
  893. $event_id = $this->sql_validate($event_id,"integer");
  894. $sql_1 = "AND id_log_detail='$event_id'";
  895. }
  896. if ($level_id !== null) {
  897. $level_id = $this->sql_validate($level_id,"integer");
  898. $sql_2 = "AND id_log_level='$level_id'";
  899. }
  900. $query="SELECT
  901. count(id_user) AS cnt
  902. FROM
  903. jorge_logger
  904. WHERE
  905. id_user='".$this->user_id."'
  906. AND
  907. vhost='".$this->vhost."'
  908. $sql_1
  909. $sql_2
  910. ";
  911. return $this->select($query);
  912. }
  913. public function get_trashed_items() {
  914. $this->id_query = "Q036";
  915. $this->vital_check();
  916. $query="SELECT
  917. peer_name_id,
  918. peer_server_id,
  919. date,
  920. timeframe,
  921. type,
  922. idx
  923. FROM
  924. pending_del
  925. WHERE
  926. owner_id = '".$this->user_id."'
  927. AND
  928. vhost='".$this->vhost."'
  929. ORDER BY
  930. str_to_date(date,'%Y-%m-%d')
  931. DESC
  932. ";
  933. $this->select($query,"raw");
  934. return $this->commit_select(array("peer_name_id","peer_server_id","date","timeframe","type","idx"));
  935. }
  936. public function move_chat_to_trash($peer_name_id,$peer_server_id,$tslice,$link) {
  937. $this->id_query = "Q037";
  938. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  939. $xmpp_host = $this->xmpp_host;
  940. $table = $this->construct_table($this->tslice);
  941. if ($this->get_ext_index($table) !== true) {
  942. return false;
  943. }
  944. else{
  945. if (!$this->result->idx) {
  946. $this->ext_idx = "1";
  947. }
  948. else{
  949. $this->ext_idx = $this->result->idx;
  950. }
  951. }
  952. $this->begin();
  953. if ($this->set_undo_table($this->peer_name_id,$this->peer_server_id,$this->tslice,"chat") === false) {
  954. $this->rollback();
  955. return false;
  956. }
  957. if ($this->remove_user_stats($this->peer_name_id,$this->peer_server_id,$this->tslice) === false) {
  958. $this->rollback();
  959. return false;
  960. }
  961. if ($this->move_mylink_to_trash($peer_name_id,$link) === false) {
  962. $this->rollback();
  963. return false;
  964. }
  965. if ($this->move_fav_to_trash($peer_name_id,$peer_server_id,$tslice) === false) {
  966. $this->rollback();
  967. return false;
  968. }
  969. $this->id_query = "Q037a";
  970. $query="UPDATE
  971. `$table`
  972. SET
  973. ext = '".$this->ext_idx."'
  974. WHERE
  975. owner_id='".$this->user_id."'
  976. AND
  977. peer_name_id='".$this->peer_name_id."'
  978. AND
  979. peer_server_id='".$this->peer_server_id."'
  980. AND
  981. ext is NULL
  982. ";
  983. if ($this->update($query) === false) {
  984. $this->rollback();
  985. return false;
  986. }
  987. else{
  988. $this->commit();
  989. $this->set_logger("4","1");
  990. return true;
  991. }
  992. }
  993. private function remove_user_stats($peer_name_id,$peer_server_id,$tslice) {
  994. $this->id_query = "Q038";
  995. $query="DELETE FROM
  996. `logdb_stats_".$this->xmpp_host."`
  997. WHERE
  998. owner_id='".$this->user_id."'
  999. AND
  1000. peer_name_id='$peer_name_id'
  1001. AND
  1002. peer_server_id='$peer_server_id'
  1003. AND
  1004. at='$tslice'
  1005. ";
  1006. return $this->delete($query);
  1007. }
  1008. public function move_mylink_to_trash($peer_name_id,$link) {
  1009. $this->id_query = "Q039";
  1010. $this->vital_check();
  1011. $peer_name_id = $this->sql_validate($peer_name_id,"integer");
  1012. $lnk = $this->sql_validate($link,"string");
  1013. $query="UPDATE
  1014. jorge_mylinks
  1015. SET
  1016. ext='".$this->ext_idx."'
  1017. WHERE
  1018. owner_id ='".$this->user_id."'
  1019. AND
  1020. vhost='".$this->vhost."'
  1021. AND
  1022. peer_name_id='$peer_name_id'
  1023. AND
  1024. link like '$lnk%'
  1025. AND
  1026. ext is NULL
  1027. ";
  1028. return $this->update($query);
  1029. }
  1030. public function move_fav_to_trash($peer_name_id,$peer_server_id,$tslice) {
  1031. $this->id_query = "Q040";
  1032. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  1033. $query="UPDATE
  1034. jorge_favorites
  1035. SET
  1036. ext='".$this->ext_idx."'
  1037. WHERE
  1038. owner_id='".$this->user_id."'
  1039. AND
  1040. peer_name_id='".$this->peer_name_id."'
  1041. AND
  1042. peer_server_id='".$this->peer_server_id."'
  1043. AND
  1044. tslice='".$this->tslice."'
  1045. AND
  1046. vhost='".$this->vhost."'
  1047. AND
  1048. ext is NULL
  1049. ";
  1050. return $this->update($query);
  1051. }
  1052. private function set_undo_table($peer_name_id,$peer_server_id,$tslice,$type = null) {
  1053. $this->id_query = "Q041";
  1054. $query="INSERT INTO
  1055. pending_del(owner_id,peer_name_id,date,peer_server_id,type,idx,vhost)
  1056. VALUES (
  1057. '".$this->user_id."',
  1058. '$peer_name_id',
  1059. '$tslice',
  1060. '$peer_server_id',
  1061. '$type',
  1062. '".$this->ext_idx."',
  1063. '".$this->vhost."'
  1064. )
  1065. ";
  1066. return $this->insert($query);
  1067. }
  1068. private function unset_undo_table($peer_name_id,$peer_server_id,$tslice,$type = null) {
  1069. $this->id_query = "Q042";
  1070. $query="DELETE FROM
  1071. pending_del
  1072. WHERE
  1073. owner_id='".$this->user_id."'
  1074. AND
  1075. peer_name_id='$peer_name_id'
  1076. AND
  1077. date='$tslice'
  1078. AND
  1079. peer_server_id='$peer_server_id'
  1080. AND
  1081. idx = '".$this->ext_idx."'
  1082. AND
  1083. vhost='".$this->vhost."'
  1084. ";
  1085. return $this->delete($query);
  1086. }
  1087. public function move_chat_from_trash($peer_name_id,$peer_server_id,$tslice,$link,$idx = null) {
  1088. $this->id_query = "Q043";
  1089. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  1090. $xmpp_host = $this->xmpp_host;
  1091. $table = $this->construct_table($this->tslice);
  1092. if (!$idx) {
  1093. $this->ext_idx = "1";
  1094. }
  1095. else{
  1096. if (ctype_digit($idx)) {
  1097. $this->ext_idx = $idx;
  1098. }
  1099. else{
  1100. return false;
  1101. }
  1102. }
  1103. // Message tables are not transactional, so this make some trouble for us to control all error conditions :/
  1104. $query="UPDATE
  1105. `$table`
  1106. SET
  1107. ext = NULL
  1108. WHERE
  1109. owner_id='".$this->user_id."'
  1110. AND
  1111. peer_name_id='".$this->peer_name_id."'
  1112. AND
  1113. peer_server_id='".$this->peer_server_id."'
  1114. AND
  1115. ext = '".$this->ext_idx."'
  1116. ";
  1117. if ($this->update($query) === false) {
  1118. return false;
  1119. }
  1120. $this->begin();
  1121. if ($this->unset_undo_table($this->peer_name_id,$this->peer_server_id,$this->tslice) === false) {
  1122. $this->rollback();
  1123. return false;
  1124. }
  1125. if ($this->recount_messages($this->peer_name_id,$this->peer_server_id,$this->tslice) === true) {
  1126. $stats = $this->result->cnt;
  1127. }
  1128. else {
  1129. $this->rollback();
  1130. return false;
  1131. }
  1132. if ($this->if_chat_exist($this->peer_name_id,$this->peer_server_id,$this->tslice) === true) {
  1133. if ($this->result->cnt == 1) {
  1134. if ($this->update_stats($this->peer_name_id,$this->peer_server_id,$this->tslice,$stats) === false) {
  1135. $this->rollback();
  1136. return false;
  1137. }
  1138. }
  1139. else {
  1140. if ($this->insert_stats($this->peer_name_id,$this->peer_server_id,$this->tslice,$stats) === false) {
  1141. $this->rollback();
  1142. return false;
  1143. }
  1144. }
  1145. }
  1146. else{
  1147. $this->rollback();
  1148. return false;
  1149. }
  1150. if ($this->move_mylink_from_trash($peer_name_id,$link) === false) {
  1151. $this->rollback();
  1152. return false;
  1153. }
  1154. if ($this->move_fav_from_trash($peer_name_id,$peer_server_id,$tslice) === false) {
  1155. $this->rollback();
  1156. return false;
  1157. }
  1158. $this->commit();
  1159. return true;
  1160. }
  1161. private function if_chat_exist($peer_name_id,$peer_server_id,$tslice) {
  1162. $this->id_query = "Q044";
  1163. $query="SELECT
  1164. 1 as cnt
  1165. FROM
  1166. `logdb_stats_".$this->xmpp_host."`
  1167. WHERE
  1168. owner_id = '".$this->user_id."'
  1169. AND
  1170. peer_name_id='$peer_name_id'
  1171. AND
  1172. peer_server_id='$peer_server_id'
  1173. AND
  1174. at = '$tslice'
  1175. ";
  1176. return $this->select($query);
  1177. }
  1178. private function insert_stats($peer_name_id,$peer_server_id,$tslice,$stats) {
  1179. $this->id_query = "Q045";
  1180. $query="INSERT INTO
  1181. `logdb_stats_".$this->xmpp_host."` (owner_id,peer_name_id,peer_server_id,at,count)
  1182. VALUES
  1183. (
  1184. '".$this->user_id."',
  1185. '$peer_name_id',
  1186. '$peer_server_id',
  1187. '$tslice',
  1188. '$stats
  1189. ')
  1190. ";
  1191. return $this->insert($query);
  1192. }
  1193. private function update_stats($peer_name_id,$peer_server_id,$tslice,$stats) {
  1194. $this->id_query = "Q046";
  1195. $query="UPDATE
  1196. `logdb_stats_".$this->xmpp_host."`
  1197. SET
  1198. count='$stats'
  1199. WHERE
  1200. owner_id='".$this->user_id."'
  1201. AND
  1202. peer_name_id='$peer_name_id'
  1203. AND
  1204. peer_server_id='$peer_server_id'
  1205. AND
  1206. at='$tslice'
  1207. ";
  1208. return $this->update($query);
  1209. }
  1210. private function recount_messages($peer_name_id,$peer_server_id,$tslice) {
  1211. $this->id_query = "Q047";
  1212. $table = $this->construct_table($tslice);
  1213. $query="SELECT
  1214. count(timestamp) as cnt
  1215. FROM
  1216. `$table`
  1217. WHERE
  1218. owner_id='".$this->user_id."'
  1219. AND
  1220. peer_name_id='$peer_name_id'
  1221. AND
  1222. peer_server_id='$peer_server_id'
  1223. AND
  1224. ext is NULL
  1225. ";
  1226. return $this->select($query);
  1227. }
  1228. private function move_mylink_from_trash($peer_name_id,$link) {
  1229. $this->id_query = "Q048";
  1230. $lnk = $this->sql_validate($link,"string");
  1231. $query="UPDATE
  1232. jorge_mylinks
  1233. SET
  1234. ext = NULL
  1235. WHERE
  1236. owner_id ='".$this->user_id."'
  1237. AND
  1238. vhost='".$this->vhost."'
  1239. AND
  1240. peer_name_id='$peer_name_id'
  1241. AND
  1242. ext = '".$this->ext_idx."'
  1243. AND
  1244. link like '$link%'
  1245. ";
  1246. return $this->update($query);
  1247. }
  1248. private function move_fav_from_trash($peer_name_id,$peer_server_id,$tslice) {
  1249. $this->id_query = "Q049";
  1250. $query="UPDATE
  1251. jorge_favorites
  1252. SET
  1253. ext = NULL
  1254. WHERE
  1255. owner_id='".$this->user_id."'
  1256. AND
  1257. peer_name_id='$peer_name_id'
  1258. AND
  1259. peer_server_id='$peer_server_id'
  1260. AND
  1261. tslice='$tslice'
  1262. AND
  1263. ext = '".$this->ext_idx."'
  1264. AND
  1265. vhost='".$this->vhost."'
  1266. ";
  1267. return $this->update($query);
  1268. }
  1269. public function delete_messages($peer_name_id,$peer_server_id,$tslice) {
  1270. $this->id_query = "Q050";
  1271. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  1272. $table = $this->construct_table($this->tslice,"date");
  1273. $query="DELETE FROM
  1274. `$table`
  1275. WHERE
  1276. owner_id='".$this->user_id."'
  1277. AND
  1278. peer_name_id='".$this->peer_name_id."'
  1279. AND
  1280. peer_server_id='".$this->peer_server_id."'
  1281. AND
  1282. ext = '".$this->ext_idx."'
  1283. ";
  1284. return $this->delete($query);
  1285. }
  1286. public function delete_mylinks($peer_name_id,$peer_server_id,$tslice) {
  1287. $this->id_query = "Q051";
  1288. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  1289. $query="DELETE FROM
  1290. jorge_mylinks
  1291. WHERE
  1292. owner_id='".$this->user_id."'
  1293. AND
  1294. vhost='".$this->vhost."'
  1295. AND
  1296. ext='".$this->ext_idx."'
  1297. AND
  1298. peer_name_id = '".$this->peer_name_id."'
  1299. AND
  1300. peer_server_id='".$this->peer_server_id."'
  1301. AND
  1302. datat = '".$this->tslice."'
  1303. ";
  1304. return $this->delete($query);
  1305. }
  1306. public function delete_favorites($peer_name_id,$peer_server_id,$tslice) {
  1307. $this->id_query = "Q052";
  1308. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  1309. $query="DELETE FROM
  1310. jorge_favorites
  1311. WHERE
  1312. owner_id='".$this->user_id."'
  1313. AND
  1314. peer_name_id='".$this->peer_name_id."'
  1315. AND
  1316. peer_server_id='".$this->peer_server_id."'
  1317. AND
  1318. tslice='".$this->tslice."'
  1319. AND
  1320. ext = '".$this->ext_idx."'
  1321. AND
  1322. vhost='".$this->vhost."'
  1323. ";
  1324. return $this->delete($query);
  1325. }
  1326. public function search_query($tslice) {
  1327. $this->id_query = "Q053";
  1328. $this->vital_check();
  1329. if ($this->user_query === null) {
  1330. return false;
  1331. }
  1332. $table = $this->construct_table($this->sql_validate($tslice,"date"));
  1333. $query="SELECT
  1334. timestamp AS ts,
  1335. peer_name_id,
  1336. peer_server_id,
  1337. direction,
  1338. ext,
  1339. body,
  1340. MATCH(body) AGAINST('".$this->user_query."' IN BOOLEAN MODE) AS score
  1341. FROM
  1342. `$table`
  1343. FORCE INDEX
  1344. (search_i)
  1345. WHERE
  1346. MATCH(body) AGAINST('".$this->user_query."' IN BOOLEAN MODE)
  1347. AND
  1348. owner_id='".$this->user_id."'
  1349. LIMIT
  1350. 0,10000
  1351. ";
  1352. $this->select($query,"raw");
  1353. return $this->commit_select(array("ts","peer_name_id","peer_server_id","direction","ext","body","score"));
  1354. }
  1355. public function search_query_chat_stream($peer_name_id,$peer_server_id,$tslice,$start_tag = null) {
  1356. $this->id_query = "Q054";
  1357. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  1358. $table = $this->construct_table($this->sql_validate($this->tslice,"date"));
  1359. if ($start_tag === null) {
  1360. $start_tag="0";
  1361. }
  1362. $start_tag = $this->sql_validate($start_tag,"integer");
  1363. $query="SELECT
  1364. from_unixtime(timestamp+0) AS ts,
  1365. peer_name_id,
  1366. peer_server_id,
  1367. direction,
  1368. ext,
  1369. body
  1370. FROM
  1371. `$table`
  1372. FORCE INDEX
  1373. (search_i)
  1374. WHERE
  1375. owner_id='".$this->user_id."'
  1376. AND
  1377. peer_name_id='".$this->peer_name_id."'
  1378. AND
  1379. peer_server_id='".$this->peer_server_id."'
  1380. LIMIT
  1381. $start_tag,10000
  1382. ";
  1383. $this->select($query,"raw");
  1384. return $this->commit_select(array("ts","peer_name_id","peer_server_id","direction","ext","body"));
  1385. }
  1386. public function search_query_in_user_chat($peer_name_id,$peer_server_id,$tslice,$start_tag) {
  1387. $this->id_query = "Q055";
  1388. if ($this->user_query === null) {
  1389. return false;
  1390. }
  1391. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  1392. $table = $this->construct_table($this->sql_validate($this->tslice,"date"));
  1393. if ($start_tag === null) {
  1394. $start_tag="0";
  1395. }
  1396. $start_tag = $this->sql_validate($start_tag,"integer");
  1397. $query="SELECT
  1398. timestamp AS ts,
  1399. peer_name_id,
  1400. peer_server_id,
  1401. direction,
  1402. ext,
  1403. body ,
  1404. MATCH(body) AGAINST('".$this->user_query."' IN BOOLEAN MODE) AS score
  1405. FROM
  1406. `$table`
  1407. FORCE INDEX
  1408. (search_i)
  1409. WHERE
  1410. match(body) against('".$this->user_query."' IN BOOLEAN MODE)
  1411. AND
  1412. owner_id='".$this->user_id."'
  1413. AND
  1414. peer_name_id='".$this->peer_name_id."'
  1415. AND
  1416. peer_server_id='".$this->peer_server_id."'
  1417. LIMIT
  1418. $start_tag,10000
  1419. ";
  1420. $this->select($query,"raw");
  1421. return $this->commit_select(array("ts","peer_name_id","peer_server_id","direction","ext","body","score"));
  1422. }
  1423. public function create_search_results_table() {
  1424. $this->id_query = "Q055";
  1425. $this->query_type = "create_table";
  1426. $query="CREATE TEMPORARY TABLE
  1427. jorge_results_table
  1428. (
  1429. ts VARCHAR(30),
  1430. time_slice VARCHAR(10),
  1431. peer_name_id MEDIUMINT,
  1432. peer_server_id SMALLINT,
  1433. direction ENUM('to','from'),
  1434. body TEXT,
  1435. score FLOAT,
  1436. ext TINYINT
  1437. )
  1438. ";
  1439. return $this->db_query($query);
  1440. }
  1441. public function insert_data_to_result_table($ts,$time_slice,$peer_name_id,$peer_server_id,$direction,$body,$score,$ext){
  1442. $this->id_query = "Q056";
  1443. $query="INSERT INTO jorge_results_table
  1444. (ts,time_slice,peer_name_id,peer_server_id,direction,body,score,ext)
  1445. VALUES (
  1446. '$ts',
  1447. '$time_slice',
  1448. '$peer_name_id',
  1449. '$peer_server_id',
  1450. '$direction',
  1451. '$body',
  1452. '$score',
  1453. '$ext'
  1454. )
  1455. ";
  1456. return $this->insert($query);
  1457. }
  1458. public function get_search_results() {
  1459. $this->id_query = "Q057";
  1460. $query="SELECT
  1461. FROM_UNIXTIME(ts+0) AS ts,
  1462. time_slice,
  1463. peer_name_id,
  1464. peer_server_id,
  1465. direction,
  1466. body,
  1467. score,
  1468. ext
  1469. FROM
  1470. jorge_results_table
  1471. ORDER BY
  1472. score
  1473. DESC LIMIT 100
  1474. ";
  1475. $this->select($query,"raw");
  1476. return $this->commit_select(array("ts","time_slice","peer_name_id","peer_server_id","direction","body","score","ext"));
  1477. }
  1478. public function get_folder_content($at) {
  1479. $this->id_query = "Q058";
  1480. $this->vital_check();
  1481. $at = $this->sql_validate($at,"string");
  1482. if ($this->spec_ignore === true) {
  1483. $sql = "AND peer_name_id != '".$this->ignore_id."'";
  1484. }
  1485. $query="SELECT
  1486. distinct(at) AS at
  1487. FROM
  1488. `logdb_stats_".$this->xmpp_host."`
  1489. WHERE
  1490. owner_id = '".$this->user_id."'
  1491. AND
  1492. substring(at,1,7) = '$at'
  1493. $sql
  1494. ORDER BY
  1495. str_to_date(at,'%Y-%m-%d')
  1496. DESC
  1497. ";
  1498. $this->select($query,"raw");
  1499. return $this->commit_select(array("at"));
  1500. }
  1501. public function insert_user_id($user_name) {
  1502. $this->id_query = "Q059";
  1503. $user_name = $this->sql_validate($user_name,"string");
  1504. $query="INSERT INTO
  1505. `logdb_users_".$this->xmpp_host."`
  1506. SET
  1507. username='$user_name'
  1508. ";
  1509. return $this->insert($query);
  1510. }
  1511. public function insert_new_settings($user_name) {
  1512. $this->id_query = "Q060";
  1513. $user_name = $this->sql_validate($user_name,"string");
  1514. $query="INSERT INTO
  1515. `logdb_settings_".$this->xmpp_host."` (owner_id,dolog_default)
  1516. VALUES
  1517. ((SELECT user_id FROM `logdb_users_".$this->xmpp_host."` WHERE username='$user_name'), '1')
  1518. ";
  1519. return $this->insert($query);
  1520. }
  1521. public function get_jorge_pref($pref_id = null) {
  1522. $this->id_query = "Q061";
  1523. if ($pref_id !== null) {
  1524. $pref_id = $this->sql_validate($pref_id,"integer");
  1525. $sql = "AND pref_id = '$pref_id'";
  1526. }
  1527. $query="SELECT
  1528. pref_id,
  1529. pref_value
  1530. FROM
  1531. jorge_pref
  1532. WHERE
  1533. owner_id='".$this->user_id."'
  1534. $sql
  1535. AND
  1536. vhost = '".$this->vhost."'
  1537. ";
  1538. if ($pref_id === null) {
  1539. $this->select($query,"raw");
  1540. return $this->commit_select(array("pref_id","pref_value"));
  1541. }
  1542. else{
  1543. return $this->select($query);
  1544. }
  1545. return false;
  1546. }
  1547. public function set_jorge_pref($pref_id,$pref_value) {
  1548. $this->id_query = "Q062";
  1549. $this->vital_check();
  1550. $pref_id = $this->sql_validate($pref_id,"integer");
  1551. $pref_value = $this->sql_validate($pref_value,"integer");
  1552. if ($this->row_count("SELECT pref_id FROM jorge_pref WHERE owner_id='".$this->user_id."' AND pref_id='$pref_id' AND vhost='".$this->vhost."'") === false) {
  1553. return false;
  1554. }
  1555. if ($this->result > 0) {
  1556. $query="UPDATE
  1557. jorge_pref
  1558. SET
  1559. pref_value='$pref_value'
  1560. WHERE
  1561. owner_id='".$this->user_id."'
  1562. AND
  1563. pref_id='$pref_id'
  1564. AND
  1565. vhost = '".$this->vhost."'
  1566. ";
  1567. return $this->update($query);
  1568. }
  1569. else{
  1570. $query="INSERT INTO
  1571. jorge_pref(owner_id,pref_id,pref_value,vhost)
  1572. VALUES
  1573. ('".$this->user_id."','$pref_id','$pref_value','".$this->vhost."')
  1574. ";
  1575. return $this->insert($query);
  1576. }
  1577. return false;
  1578. }
  1579. public function sphinx_get_single($peer_name_id, $peer_server_id, $peer_resource_id, $timestamp, $tslice) {
  1580. $this->id_query = "Q063";
  1581. $this->vital_check();
  1582. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  1583. $peer_resource_id = $this->sql_validate($peer_resource_id,"integer");
  1584. $timestamp = $this->sql_validate($timestamp,"string");
  1585. $query="SELECT
  1586. body
  1587. FROM
  1588. `".$this->construct_table($this->tslice)."`
  1589. WHERE
  1590. owner_id = '".$this->user_id."'
  1591. AND
  1592. peer_name_id = '".$this->peer_name_id."'
  1593. AND
  1594. peer_server_id = '".$this->peer_server_id."'
  1595. AND
  1596. peer_resource_id = '".$peer_resource_id."'
  1597. AND
  1598. timestamp like '".$timestamp."%'
  1599. ";
  1600. return $this->select($query);
  1601. }
  1602. public function get_favorites() {
  1603. $this->id_query = "Q064";
  1604. $this->vital_check();
  1605. $query="SELECT *
  1606. FROM
  1607. jorge_favorites
  1608. WHERE
  1609. owner_id='".$this->user_id."'
  1610. AND
  1611. vhost='".$this->vhost."'
  1612. AND
  1613. ext is NULL
  1614. ORDER BY
  1615. str_to_date(tslice,'%Y-%m-%d')
  1616. DESC
  1617. ";
  1618. $this->select($query,"raw");
  1619. return $this->commit_select(array("link_id","peer_name_id","peer_server_id","resource_id","tslice","comment"));
  1620. }
  1621. public function set_favorites($peer_name_id,$peer_server_id,$peer_resource_id = null, $tslice,$comment) {
  1622. $this->id_query = "Q065";
  1623. $this->vital_check();
  1624. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  1625. #$peer_resource_id = $this->sql_validate($peer_resource_id,"integer");
  1626. $comment = $this->sql_validate($comment,"string");
  1627. $query="INSERT INTO
  1628. jorge_favorites(owner_id,peer_name_id,peer_server_id,tslice,comment,vhost)
  1629. VALUES(
  1630. '".$this->user_id."',
  1631. '".$this->peer_name_id."',
  1632. '".$this->peer_server_id."',
  1633. '".$this->tslice."',
  1634. '$comment',
  1635. '".$this->vhost."'
  1636. )
  1637. ";
  1638. return $this->insert($query);
  1639. }
  1640. public function delete_favorites_id($link_id) {
  1641. $this->id_query = "Q066";
  1642. $this->vital_check();
  1643. $link_id = $this->sql_validate($link_id,"string");
  1644. $query="DELETE FROM
  1645. jorge_favorites
  1646. WHERE
  1647. owner_id = ".$this->user_id."
  1648. AND
  1649. vhost='".$this->vhost."'
  1650. AND
  1651. link_id = '$link_id';
  1652. ";
  1653. return $this->delete($query);
  1654. }
  1655. public function check_favorite($peer_name_id,$peer_server_id,$tslice) {
  1656. $this->id_query = "Q067";
  1657. $this->vital_check();
  1658. $this->prepare($peer_name_id,$peer_server_id,$tslice);
  1659. $query="SELECT
  1660. count(*) as cnt
  1661. FROM
  1662. jorge_favorites
  1663. WHERE
  1664. owner_id='".$this->user_id."'
  1665. AND
  1666. tslice='".$this->tslice."'
  1667. AND
  1668. peer_name_id='".$this->peer_name_id."'
  1669. AND
  1670. peer_server_id='".$this->peer_server_id."'
  1671. AND
  1672. vhost='".$this->vhost."'
  1673. AND
  1674. ext is null
  1675. ";
  1676. return $this->select($query);
  1677. }
  1678. public function get_favorites_count() {
  1679. $this->id_query = "Q068";
  1680. $this->vital_check();
  1681. $query="SELECT
  1682. count(*) as cnt
  1683. FROM
  1684. jorge_favorites
  1685. WHERE
  1686. owner_id = '".$this->user_id."'
  1687. AND
  1688. vhost='".$this->vhost."'
  1689. AND
  1690. ext is null
  1691. ";
  1692. return $this->select($query);
  1693. }
  1694. public function get_top_ten($date,$vhost = null) {
  1695. $this->id_query = "Q069";
  1696. if ($vhost === null) {
  1697. $vh = $this->xmpp_host;
  1698. }
  1699. else{
  1700. $vh = $this->vh($vhost,true);
  1701. }
  1702. $date = $this->sql_validate($date,"date");
  1703. $query="SELECT
  1704. at,
  1705. owner_id,
  1706. peer_name_id,
  1707. peer_server_id,
  1708. count
  1709. FROM
  1710. `logdb_stats_".$vh."`
  1711. WHERE
  1712. at = '$date'
  1713. ORDER BY
  1714. count
  1715. DESC LIMIT 10
  1716. ";
  1717. $this->select($query,"raw");
  1718. return $this->commit_select(array("at","owner_id","peer_name_id","peer_server_id","count"));
  1719. }
  1720. public function get_monthly_stats($vhost = null) {
  1721. $this->id_query = "Q070";
  1722. if ($vhost === null) {
  1723. $vh = $this->xmpp_host;
  1724. }
  1725. else{
  1726. $vh = $this->vh($vhost,true);
  1727. }
  1728. // This query need tweak to use ex.: where at between '2009-2' and '2009-3', it speeds up and corrects query, also forces to use index, instead full table scan
  1729. $query="SELECT
  1730. count(distinct(owner_id)) AS users_total,
  1731. unix_timestamp(at)*10000 AS time_unix,
  1732. sum(count) AS messages
  1733. FROM
  1734. `logdb_stats_".$vh."`
  1735. GROUP BY
  1736. at
  1737. ORDER BY
  1738. str_to_date(at,'%Y-%m-%d')
  1739. DESC LIMIT 30
  1740. ";
  1741. $this->select($query,"raw");
  1742. return $this->commit_select(array("users_total","time_unix","messages"));
  1743. }
  1744. public function get_hourly_stats($date,$vhost = null) {
  1745. $this->id_query = "Q071";
  1746. if ($vhost === null) {
  1747. $vh = $this->vhost;
  1748. }
  1749. else{
  1750. $vh = $this->vh($vhost);
  1751. }
  1752. $date = $this->sql_validate($date,"date");
  1753. $query="SELECT
  1754. hour,
  1755. value
  1756. FROM
  1757. jorge_stats
  1758. WHERE
  1759. day='$date'
  1760. AND
  1761. vhost='".$vh."'
  1762. ORDER BY
  1763. hour
  1764. ASC
  1765. ";
  1766. $this->select($query,"raw");
  1767. return $this->commit_select(array("hour","value"));
  1768. }
  1769. public function get_weekly_stats($date_start,$date_end,$vhost = null) {
  1770. $this->id_query = "Q072";
  1771. if ($vhost === null) {
  1772. $vh = $this->vhost;
  1773. }
  1774. else{
  1775. $vh = $this->sql_validate($vhost,"string");
  1776. }
  1777. $date_start = $this->sql_validate($date_start,"date");
  1778. $date_end = $this->sql_validate($date_end,"date");
  1779. $query="SELECT
  1780. hour,
  1781. value
  1782. FROM
  1783. jorge_stats
  1784. WHERE
  1785. day<='$date_end'
  1786. AND
  1787. day >= '$date_start'
  1788. AND
  1789. vhost='".$vh."'
  1790. ORDER BY
  1791. day,hour
  1792. ASC
  1793. ";
  1794. $this->select($query,"raw");
  1795. return $this->commit_select(array("hour","value"));
  1796. }
  1797. public function get_personal_top() {
  1798. $this->id_query = "Q073";
  1799. $this->vital_check();
  1800. $query="SELECT
  1801. peer_name_id,
  1802. peer_server_id,
  1803. at,
  1804. count
  1805. FROM
  1806. `logdb_stats_".$this->xmpp_host."`
  1807. WHERE
  1808. owner_id='".$this->user_id."'
  1809. AND
  1810. peer_name_id!='".$this->ignore_id."'
  1811. AND
  1812. ext is NULL
  1813. ORDER BY
  1814. count
  1815. DESC LIMIT 10
  1816. ";
  1817. $this->select($query,"raw");
  1818. return $this->commit_select(array("peer_name_id","peer_server_id","at","count"));
  1819. }
  1820. public function get_personal_sum() {
  1821. $this->id_query = "Q074";
  1822. $this->vital_check();
  1823. $query="SELECT
  1824. sum(count) as cnt
  1825. FROM
  1826. `logdb_stats_".$this->xmpp_host."`
  1827. WHERE
  1828. owner_id='".$this->user_id."'
  1829. AND
  1830. peer_name_id!='".$this->ignore_id."'
  1831. ";
  1832. return $this->select($query);
  1833. }
  1834. public function erase_all() {
  1835. $this->id_query = "Q075";
  1836. $this->vital_check();
  1837. $query="SELECT
  1838. distinct(at)
  1839. FROM
  1840. `logdb_stats_".$this->xmpp_host."`
  1841. WHERE
  1842. owner_id='".$this->user_id."'
  1843. ";
  1844. $this->select($query,"raw");
  1845. $this->commit_select(array("at"));
  1846. $results = $this->result;
  1847. $this->id_query = "Q076";
  1848. foreach ($results as $result) {
  1849. $query="DELETE FROM
  1850. `logdb_messages_$result[at]_".$this->xmpp_host."`
  1851. WHERE
  1852. owner_id='".$this->user_id."'
  1853. ";
  1854. if ($this->delete($query) === false) {
  1855. return false;
  1856. }
  1857. }
  1858. $this->id_query = "Q077";
  1859. $query="DELETE FROM
  1860. `logdb_stats_".$this->xmpp_host."`
  1861. WHERE
  1862. owner_id='".$this->user_id."'
  1863. ";
  1864. if ($this->delete($query) === false) {
  1865. return false;
  1866. }
  1867. if ($this->jorge_cleanup_soft() === false) {
  1868. return false;
  1869. }
  1870. return true;
  1871. }
  1872. public function jorge_cleanup() {
  1873. $this->id_query = "Q081";
  1874. $this->vital_check();
  1875. $query="DELETE FROM
  1876. jorge_pref
  1877. WHERE
  1878. owner_id='".$this->user_id."'
  1879. AND
  1880. vhost='".$this->vhost."'
  1881. ";
  1882. if ($this->delete($query) === false) {
  1883. return false;
  1884. }
  1885. $this->id_query = "Q082";
  1886. $query="DELETE FROM
  1887. `logdb_settings_".$this->xmpp_host."`
  1888. where
  1889. owner_id='".$this->user_id."'
  1890. ";
  1891. if ($this->delete($query) === false) {
  1892. return false;
  1893. }
  1894. return true;
  1895. }

Large files files are truncated, but you can click here to view the full file