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.

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