You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

687 lines
20 KiB

  1. <?php
  2. namespace modl;
  3. class ContactDAO extends SQL {
  4. function __construct() {
  5. parent::__construct();
  6. }
  7. function get($jid = null, $empty = false) {
  8. $this->_sql = '
  9. select *, privacy.value as privacy from contact
  10. left outer join privacy
  11. on contact.jid = privacy.pkey
  12. where jid = :jid';
  13. if($jid == null) $jid = $this->_user;
  14. $this->prepare(
  15. 'Contact',
  16. array(
  17. 'jid' => $jid
  18. )
  19. );
  20. $contact = $this->run('Contact', 'item');
  21. // If we cannot find the contact
  22. if($contact == null && $empty == false) {
  23. $contact = new Contact;
  24. $contact->jid = $jid;
  25. return $contact;
  26. }
  27. return $contact;
  28. }
  29. function set(Contact $contact) {
  30. if(!isset($contact->created)) {
  31. $contact->created = date(DATE_ISO8601);
  32. }
  33. $this->_sql = '
  34. update contact
  35. set fn = :fn,
  36. name = :name,
  37. date = :date,
  38. url = :url,
  39. email = :email,
  40. adrlocality = :adrlocality,
  41. adrpostalcode = :adrpostalcode,
  42. adrcountry = :adrcountry,
  43. gender = :gender,
  44. marital = :marital,
  45. description = :description,
  46. mood = :mood,
  47. activity = :activity,
  48. nickname = :nickname,
  49. tuneartist = :tuneartist,
  50. tunelenght = :tunelenght,
  51. tunerating = :tunerating,
  52. tunesource = :tunesource,
  53. tunetitle = :tunetitle,
  54. tunetrack = :tunetrack,
  55. loclatitude = :loclatitude,
  56. loclongitude = :loclongitude,
  57. localtitude = :localtitude,
  58. loccountry = :loccountry,
  59. loccountrycode = :loccountrycode,
  60. locregion = :locregion,
  61. locpostalcode = :locpostalcode,
  62. loclocality = :loclocality,
  63. locstreet = :locstreet,
  64. locbuilding = :locbuilding,
  65. loctext = :loctext,
  66. locuri = :locuri,
  67. loctimestamp = :loctimestamp,
  68. twitter = :twitter,
  69. skype = :skype,
  70. yahoo = :yahoo,
  71. avatarhash = :avatarhash,
  72. created = :created,
  73. updated = :updated
  74. where contact.jid = :jid';
  75. $this->prepare(
  76. 'Contact',
  77. array(
  78. 'fn' => $contact->fn,
  79. 'name' => $contact->name,
  80. 'date' => $contact->date,
  81. 'url' => $contact->url,
  82. 'email' => $contact->email,
  83. 'adrlocality' => $contact->adrlocality,
  84. 'adrpostalcode' => $contact->adrpostalcode,
  85. 'adrcountry' => $contact->adrcountry,
  86. 'gender' => $contact->gender,
  87. 'marital' => $contact->marital,
  88. 'description' => $contact->description,
  89. // User Mood (contain serialized array) - XEP 0107
  90. 'mood' => $contact->mood,
  91. // User Activity (contain serialized array) - XEP 0108
  92. 'activity' => $contact->activity,
  93. // User Nickname - XEP 0172
  94. 'nickname' => $contact->nickname,
  95. // User Tune - XEP 0118
  96. 'tuneartist' => $contact->tuneartist,
  97. 'tunelenght' => $contact->tunelenght,
  98. 'tunerating' => $contact->tunerating,
  99. 'tunesource' => $contact->tunesource,
  100. 'tunetitle' => $contact->tunetitle,
  101. 'tunetrack' => $contact->tunetrack,
  102. // User Location
  103. 'loclatitude' => $contact->loclatitude,
  104. 'loclongitude' => $contact->loclongitude,
  105. 'localtitude' => $contact->localtitude,
  106. 'loccountry' => $contact->loccountry,
  107. 'loccountrycode' => $contact->loccountrycode,
  108. 'locregion' => $contact->locregion,
  109. 'locpostalcode' => $contact->locpostalcode,
  110. 'loclocality' => $contact->loclocality,
  111. 'locstreet' => $contact->locstreet,
  112. 'locbuilding' => $contact->locbuilding,
  113. 'loctext' => $contact->loctext,
  114. 'locuri' => $contact->locuri,
  115. 'loctimestamp' => $contact->loctimestamp,
  116. 'twitter' => $contact->twitter,
  117. 'skype' => $contact->skype,
  118. 'yahoo' => $contact->yahoo,
  119. 'avatarhash' => $contact->avatarhash,
  120. 'created' => $contact->created,
  121. 'updated' => date(DATE_ISO8601),
  122. 'jid' => $contact->jid
  123. )
  124. );
  125. $this->run('Contact');
  126. if(!$this->_effective) {
  127. $this->_sql = '
  128. insert into contact
  129. (
  130. fn,
  131. name,
  132. date,
  133. url,
  134. email,
  135. adrlocality,
  136. adrpostalcode,
  137. adrcountry,
  138. gender,
  139. marital,
  140. description,
  141. mood,
  142. activity,
  143. nickname,
  144. tuneartist,
  145. tunelenght,
  146. tunerating,
  147. tunesource,
  148. tunetitle,
  149. tunetrack,
  150. loclatitude,
  151. loclongitude,
  152. localtitude,
  153. loccountry,
  154. loccountrycode,
  155. locregion,
  156. locpostalcode,
  157. loclocality,
  158. locstreet,
  159. locbuilding,
  160. loctext,
  161. locuri,
  162. loctimestamp,
  163. twitter,
  164. skype,
  165. yahoo,
  166. avatarhash,
  167. created,
  168. updated,
  169. jid)
  170. values (
  171. :fn,
  172. :name,
  173. :date,
  174. :url,
  175. :email,
  176. :adrlocality,
  177. :adrpostalcode,
  178. :adrcountry,
  179. :gender,
  180. :marital,
  181. :description,
  182. :mood,
  183. :activity,
  184. :nickname,
  185. :tuneartist,
  186. :tunelenght,
  187. :tunerating,
  188. :tunesource,
  189. :tunetitle,
  190. :tunetrack,
  191. :loclatitude,
  192. :loclongitude,
  193. :localtitude,
  194. :loccountry,
  195. :loccountrycode,
  196. :locregion,
  197. :locpostalcode,
  198. :loclocality,
  199. :locstreet,
  200. :locbuilding,
  201. :loctext,
  202. :locuri,
  203. :loctimestamp,
  204. :twitter,
  205. :skype,
  206. :yahoo,
  207. :avatarhash,
  208. :created,
  209. :updated,
  210. :jid)';
  211. $this->prepare(
  212. 'Contact',
  213. array(
  214. 'fn' => $contact->fn,
  215. 'name' => $contact->name,
  216. 'date' => $contact->date,
  217. 'url' => $contact->url,
  218. 'email' => $contact->email,
  219. 'adrlocality' => $contact->adrlocality,
  220. 'adrpostalcode' => $contact->adrpostalcode,
  221. 'adrcountry' => $contact->adrcountry,
  222. 'gender' => $contact->gender,
  223. 'marital' => $contact->marital,
  224. 'description' => $contact->description,
  225. // User Mood (contain serialized array) - XEP 0107
  226. 'mood' => $contact->mood,
  227. // User Activity (contain serialized array) - XEP 0108
  228. 'activity' => $contact->activity,
  229. // User Nickname - XEP 0172
  230. 'nickname' => $contact->nickname,
  231. // User Tune - XEP 0118
  232. 'tuneartist' => $contact->tuneartist,
  233. 'tunelenght' => $contact->tunelenght,
  234. 'tunerating' => $contact->tunerating,
  235. 'tunesource' => $contact->tunesource,
  236. 'tunetitle' => $contact->tunetitle,
  237. 'tunetrack' => $contact->tunetrack,
  238. // User Location
  239. 'loclatitude' => $contact->loclatitude,
  240. 'loclongitude' => $contact->loclongitude,
  241. 'localtitude' => $contact->localtitude,
  242. 'loccountry' => $contact->loccountry,
  243. 'loccountrycode' => $contact->loccountrycode,
  244. 'locregion' => $contact->locregion,
  245. 'locpostalcode' => $contact->locpostalcode,
  246. 'loclocality' => $contact->loclocality,
  247. 'locstreet' => $contact->locstreet,
  248. 'locbuilding' => $contact->locbuilding,
  249. 'loctext' => $contact->loctext,
  250. 'locuri' => $contact->locuri,
  251. 'loctimestamp' => $contact->loctimestamp,
  252. 'twitter' => $contact->twitter,
  253. 'skype' => $contact->skype,
  254. 'yahoo' => $contact->yahoo,
  255. 'avatarhash' => $contact->avatarhash,
  256. 'created' => date(DATE_ISO8601),
  257. 'updated' => date(DATE_ISO8601),
  258. 'jid' => $contact->jid
  259. )
  260. );
  261. $this->run('Contact');
  262. }
  263. }
  264. function getAll() {
  265. $this->_sql =
  266. 'select *, privacy.value as privacy from contact
  267. left outer join privacy
  268. on contact.jid = privacy.pkey';
  269. $this->prepare('Contact');
  270. return $this->run('Contact');
  271. }
  272. function searchJid($search) {
  273. $this->_sql =
  274. 'select *, privacy.value as privacy from contact
  275. left outer join privacy
  276. on contact.jid = privacy.pkey
  277. where jid like :jid
  278. and privacy.value = 1
  279. order by jid';
  280. $this->prepare(
  281. 'Contact',
  282. array(
  283. 'jid' => '%'.$search.'%'
  284. )
  285. );
  286. return $this->run('Contact');
  287. }
  288. function getAllPublic($limitf = false, $limitr = false) {
  289. $this->_sql =
  290. 'select *, privacy.value as privacy from contact
  291. left outer join privacy
  292. on contact.jid = privacy.pkey
  293. where privacy.value = 1
  294. order by updated desc';
  295. if($limitr)
  296. $this->_sql = $this->_sql.' limit '.$limitr.' offset '.$limitf;
  297. $this->prepare('Contact');
  298. return $this->run('Contact');
  299. }
  300. function countAllPublic() {
  301. $this->_sql =
  302. 'select count(*) from contact
  303. left outer join privacy
  304. on contact.jid = privacy.pkey
  305. where privacy.value = 1';
  306. $this->prepare('Contact');
  307. $results = $this->run(null, 'array');
  308. $results = array_values($results[0]);
  309. return (int)$results[0];
  310. }
  311. function cleanRoster() {
  312. $this->_sql = '
  313. delete from rosterlink
  314. where session = :session';
  315. $this->prepare(
  316. 'RosterLink',
  317. array(
  318. 'session' => $this->_user
  319. )
  320. );
  321. return $this->run('RosterLink');
  322. }
  323. function getRoster() {
  324. $this->_sql = '
  325. select
  326. rosterlink.jid,
  327. contact.fn,
  328. contact.name,
  329. contact.nickname,
  330. contact.tuneartist,
  331. contact.tunetitle,
  332. rosterlink.rostername,
  333. rosterlink.rostersubscription,
  334. rosterlink.groupname,
  335. rosterlink.chaton,
  336. presence.status,
  337. presence.resource,
  338. presence.value,
  339. presence.delay,
  340. presence.node,
  341. presence.ver,
  342. presence.last
  343. from rosterlink
  344. left outer join presence
  345. on rosterlink.jid = presence.jid and rosterlink.session = presence.session
  346. left outer join contact
  347. on rosterlink.jid = contact.jid
  348. where rosterlink.session = :session
  349. order by groupname, rosterlink.jid, presence.value';
  350. $this->prepare(
  351. 'RosterLink',
  352. array(
  353. 'session' => $this->_user
  354. )
  355. );
  356. return $this->run('RosterContact');
  357. }
  358. // Get the roster without the presences
  359. function getRosterSimple() {
  360. $this->_sql = '
  361. select
  362. rosterlink.jid,
  363. contact.fn,
  364. contact.name,
  365. contact.nickname,
  366. contact.tuneartist,
  367. contact.tunetitle,
  368. rosterlink.rostername,
  369. rosterlink.rostersubscription,
  370. rosterlink.groupname,
  371. rosterlink.chaton
  372. from rosterlink
  373. left outer join contact
  374. on rosterlink.jid = contact.jid
  375. where rosterlink.session = :session
  376. order by groupname, rosterlink.jid';
  377. $this->prepare(
  378. 'RosterLink',
  379. array(
  380. 'session' => $this->_user
  381. )
  382. );
  383. return $this->run('RosterContact');
  384. }
  385. function getRosterChat() {
  386. $this->_sql = '
  387. select * from rosterlink
  388. left outer join (
  389. select * from presence
  390. order by presence.priority desc
  391. ) as presence
  392. on rosterlink.jid = presence.jid
  393. left outer join contact
  394. on rosterlink.jid = contact.jid
  395. where rosterlink.session = :session
  396. and rosterlink.chaton > 0
  397. order by rosterlink.groupname, rosterlink.jid, presence.value';
  398. $this->prepare(
  399. 'RosterLink',
  400. array(
  401. 'session' => $this->_user
  402. )
  403. );
  404. return $this->run('RosterContact');
  405. }
  406. function getRosterFrom() {
  407. $this->_sql = '
  408. select * from rosterlink
  409. left outer join contact
  410. on rosterlink.jid = contact.jid
  411. where rosterlink.session = :session
  412. and rosterlink.rostersubscription = :rostersubscription';
  413. $this->prepare(
  414. 'RosterLink',
  415. array(
  416. 'session' => $this->_user,
  417. 'rostersubscription' => 'from'
  418. )
  419. );
  420. return $this->run('RosterContact');
  421. }
  422. function getRosterItem($jid, $item = false) {
  423. $this->_sql = '
  424. select
  425. rosterlink.jid,
  426. contact.fn,
  427. contact.name,
  428. contact.nickname,
  429. contact.tuneartist,
  430. contact.tunetitle,
  431. rosterlink.rostername,
  432. rosterlink.rostersubscription,
  433. rosterlink.groupname,
  434. rosterlink.chaton,
  435. presence.status,
  436. presence.resource,
  437. presence.value,
  438. presence.delay,
  439. presence.node,
  440. presence.ver,
  441. presence.last
  442. from rosterlink
  443. left outer join presence
  444. on rosterlink.jid = presence.jid and rosterlink.session = presence.session
  445. left outer join contact
  446. on rosterlink.jid = contact.jid
  447. where rosterlink.session = :session
  448. and rosterlink.jid = :jid
  449. order by groupname, rosterlink.jid, presence.value';
  450. $this->prepare(
  451. 'RosterLink',
  452. array(
  453. 'session' => $this->_user,
  454. 'jid' => $jid
  455. )
  456. );
  457. if($item)
  458. return $this->run('RosterContact');
  459. else
  460. return $this->run('RosterContact', 'item');
  461. }
  462. function getPresence($jid, $resource) {
  463. $this->_sql = '
  464. select * from contact
  465. right outer join presence on contact.jid = presence.mucjid
  466. where presence.session = :session
  467. and presence.jid = :jid
  468. and presence.resource = :resource
  469. order by mucaffiliation desc';
  470. $this->prepare(
  471. 'Presence',
  472. array(
  473. 'session' => $this->_user,
  474. 'jid' => $jid,
  475. 'resource' => $resource
  476. )
  477. );
  478. return $this->run('PresenceContact', 'item');
  479. }
  480. function getPresences($jid) {
  481. $this->_sql = '
  482. select * from contact
  483. right outer join presence on contact.jid = presence.mucjid
  484. where presence.session = :session
  485. and presence.jid = :jid
  486. order by mucaffiliation desc';
  487. $this->prepare(
  488. 'Presence',
  489. array(
  490. 'session' => $this->_user,
  491. 'jid' => $jid
  492. )
  493. );
  494. return $this->run('PresenceContact');
  495. }
  496. function getMe($item = false) {
  497. $this->_sql = '
  498. select * from contact
  499. left outer join presence on contact.jid = presence.jid
  500. where contact.jid = :jid
  501. and presence.session = :session';
  502. $this->prepare(
  503. 'RosterLink',
  504. array(
  505. 'session' => $this->_user,
  506. 'jid' => $this->_user
  507. )
  508. );
  509. if($item)
  510. return $this->run('RosterContact');
  511. else
  512. return $this->run('RosterContact', 'item');
  513. }
  514. function getTop($limit = 6) {
  515. $this->_sql = '
  516. select *, jidfrom from (
  517. select jidfrom, count(*) as count from message
  518. where jidfrom not like :jid
  519. and session = :jid
  520. and type != \'groupchat\'
  521. group by jidfrom
  522. order by count desc
  523. ) as top
  524. join (
  525. select *
  526. from rosterlink
  527. where session = :jid
  528. ) as rosterlink on jidfrom = rosterlink.jid
  529. left outer join contact on jidfrom = contact.jid
  530. left outer join (
  531. select a.*
  532. from presence a
  533. join (
  534. select jid, min( id ) as id
  535. from presence
  536. where session = :jid
  537. group by jid
  538. ) as b on ( a.id = b.id )
  539. ) presence on jidfrom = presence.jid
  540. order by presence.value, count desc
  541. limit :tunelenght';
  542. $this->prepare(
  543. 'Contact',
  544. array(
  545. 'jid' => $this->_user,
  546. 'tunelenght' => $limit // And an another hack…
  547. )
  548. );
  549. return $this->run('RosterContact');
  550. }
  551. function getStatistics() {
  552. $this->_sql = '
  553. select
  554. (select count(*) from postn where postn.session = :session ) as post,
  555. (select count(*) from rosterlink where rosterlink.session= :session ) as rosterlink,
  556. (select count(*) from presence where presence.session= :session ) as presence,
  557. (select count(*) from message where message.session = :session) as message;';
  558. $this->prepare(
  559. 'Postn',
  560. array(
  561. 'session' => $this->_user
  562. )
  563. );
  564. return $this->run(null, 'array');
  565. }
  566. }