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.

320 lines
9.2 KiB

8 years ago
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Capsule\Manager as DB;
  5. use Respect\Validation\Validator;
  6. use Movim\Picture;
  7. class Contact extends Model
  8. {
  9. protected $fillable = ['id', 'nickname', 'mood'];
  10. protected $keyType = 'string';
  11. public $incrementing = false;
  12. public function user()
  13. {
  14. return $this->belongsTo('App\User', 'id');
  15. }
  16. public function scopePublic($query, $like = false)
  17. {
  18. return $query->whereIn('id', function ($query) use ($like) {
  19. $query->select('id')
  20. ->from('users')
  21. ->where('public', true)
  22. ->when($like !== false, function ($query) use ($like) {
  23. $query->where('id', 'like', '%'. $like . '%');
  24. });
  25. });
  26. }
  27. public function scopeNotInRoster($query, $sessionId)
  28. {
  29. return $query->whereNotIn('id', function ($query) use ($sessionId) {
  30. $query->select('jid')
  31. ->from('rosters')
  32. ->where('session_id', $sessionId);
  33. });
  34. }
  35. public function scopeOrderByPresence($query)
  36. {
  37. return $query->leftJoin(DB::raw('(
  38. select min(value) as value, jid
  39. from presences
  40. group by jid) as presences
  41. '), 'presences.jid', '=', 'contacts.id')
  42. ->orderBy('presences.value');
  43. }
  44. public function save(array $options = [])
  45. {
  46. try {
  47. unset($this->photobin);
  48. parent::save($options);
  49. } catch (\Exception $e) {
  50. /*
  51. * Multi processes simultanous save
  52. */
  53. }
  54. }
  55. public function set($vcard, $jid)
  56. {
  57. $this->id = $jid;
  58. $validate_date = Validator::date('Y-m-d');
  59. if (isset($vcard->vCard->BDAY)
  60. && $validate_date->validate($vcard->vCard->BDAY)) {
  61. $this->date = (string)$vcard->vCard->BDAY;
  62. }
  63. if ($vcard->vCard->NICKNAME) {
  64. $this->name = (string)$vcard->vCard->NICKNAME;
  65. }
  66. if ($vcard->vCard->FN) {
  67. $this->fn = (string)$vcard->vCard->FN;
  68. }
  69. if ($vcard->vCard->URL) {
  70. $this->url = (string)$vcard->vCard->URL;
  71. }
  72. if ($vcard->vCard->EMAIL) {
  73. $this->email = (string)$vcard->vCard->EMAIL->USERID;
  74. }
  75. if ($vcard->vCard->ADR) {
  76. $this->adrlocality = (string)$vcard->vCard->ADR->LOCALITY;
  77. $this->adrpostalcode = (string)$vcard->vCard->ADR->PCODE;
  78. $this->adrcountry = (string)$vcard->vCard->ADR->CTRY;
  79. }
  80. if (filter_var((string)$vcard->vCard->PHOTO, FILTER_VALIDATE_URL)) {
  81. $this->photobin = base64_encode(
  82. requestUrl((string)$vcard->vCard->PHOTO, 1)
  83. );
  84. } elseif ($vcard->vCard->PHOTO) {
  85. $this->photobin = (string)$vcard->vCard->PHOTO->BINVAL;
  86. $this->avatarhash = sha1(base64_decode($this->photobin));
  87. }
  88. if ($vcard->vCard->DESC) {
  89. $this->description = (string)$vcard->vCard->DESC;
  90. }
  91. }
  92. public function createThumbnails()
  93. {
  94. if (!$this->photobin) {
  95. return;
  96. }
  97. $p = new Picture;
  98. $p->fromBase($this->photobin);
  99. $p->set($this->id);
  100. unset($this->photobin);
  101. }
  102. public function getPhoto($size = 'm')
  103. {
  104. return getPhoto($this->id, $size);
  105. }
  106. public function setLocation($stanza)
  107. {
  108. $this->loclatitude = (string)$stanza->items->item->geoloc->lat;
  109. $this->loclongitude = (string)$stanza->items->item->geoloc->lon;
  110. $this->localtitude = (int)$stanza->items->item->geoloc->alt;
  111. $this->loccountry = (string)$stanza->items->item->geoloc->country;
  112. $this->loccountrycode = (string)$stanza->items->item->geoloc->countrycode;
  113. $this->locregion = (string)$stanza->items->item->geoloc->region;
  114. $this->locpostalcode = (string)$stanza->items->item->geoloc->postalcode;
  115. $this->loclocality = (string)$stanza->items->item->geoloc->locality;
  116. $this->locstreet = (string)$stanza->items->item->geoloc->street;
  117. $this->locbuilding = (string)$stanza->items->item->geoloc->building;
  118. $this->loctext = (string)$stanza->items->item->geoloc->text;
  119. $this->locuri = (string)$stanza->items->item->geoloc->uri;
  120. $this->loctimestamp = date(
  121. 'Y-m-d H:i:s',
  122. strtotime((string)$stanza->items->item->geoloc->timestamp)
  123. );
  124. }
  125. public function setTune($stanza)
  126. {
  127. $this->tuneartist = (string)$stanza->items->item->tune->artist;
  128. $this->tunelenght = (int)$stanza->items->item->tune->lenght;
  129. $this->tunerating = (int)$stanza->items->item->tune->rating;
  130. $this->tunesource = (string)$stanza->items->item->tune->source;
  131. $this->tunetitle = (string)$stanza->items->item->tune->title;
  132. $this->tunetrack = (string)$stanza->items->item->tune->track;
  133. }
  134. public function setVcard4($vcard)
  135. {
  136. if (isset($vcard->bday->date)
  137. && Validator::date('Y-m-d')->validate($vcard->bday->date)) {
  138. $this->date = (string)$vcard->bday->date;
  139. }
  140. $this->nickname = !empty($vcard->nickname->text)
  141. ? (string)$vcard->nickname->text
  142. : null;
  143. $this->fn = !empty($vcard->fn->text)
  144. ? (string)$vcard->fn->text
  145. : null;
  146. $this->url = !empty($vcard->url->uri)
  147. ? (string)$vcard->url->uri
  148. : null;
  149. $this->adrlocality = (string)$vcard->adr->locality;
  150. $this->adrcountry = (string)$vcard->adr->country;
  151. $this->adrpostalcode = (string)$vcard->adr->code;
  152. $this->email = !empty($vcard->email->text)
  153. ? (string)$vcard->email->text
  154. : null;
  155. $this->description = trim((string)$vcard->note->text);
  156. }
  157. public function getPlace(): string
  158. {
  159. $place = '';
  160. if ($this->loctext != '') {
  161. $place .= $this->loctext.' ';
  162. } else {
  163. if ($this->locbuilding != '') {
  164. $place .= $this->locbuilding.' ';
  165. }
  166. if ($this->locstreet != '') {
  167. $place .= $this->locstreet.'<br />';
  168. }
  169. if ($this->locpostalcode != '') {
  170. $place .= $this->locpostalcode.' ';
  171. }
  172. if ($this->loclocality != '') {
  173. $place .= $this->loclocality.'<br />';
  174. }
  175. if ($this->locregion != '') {
  176. $place .= $this->locregion.' - ';
  177. }
  178. if ($this->loccountry != '') {
  179. $place .= $this->loccountry;
  180. }
  181. }
  182. return $place;
  183. }
  184. public function getTruenameAttribute(): string
  185. {
  186. if ($this->fn) {
  187. return $this->fn;
  188. }
  189. if ($this->nickname) {
  190. return $this->nickname;
  191. }
  192. if ($this->name) {
  193. return $this->name;
  194. }
  195. return explodeJid($this->id)['username'] ?? $this->id;
  196. }
  197. public function getJidAttribute(): ?string
  198. {
  199. return $this->id;
  200. }
  201. public function getAge()
  202. {
  203. if ($this->isValidDate()) {
  204. $age = intval(substr(date('Ymd') - date('Ymd', strtotime($this->date)), 0, -4));
  205. if ($age != 0) {
  206. return $age;
  207. }
  208. }
  209. }
  210. public function getDate()
  211. {
  212. if ($this->date == null) {
  213. return null;
  214. }
  215. $dt = new \DateTime($this->date);
  216. return $dt->format('Y-m-d');
  217. }
  218. public function getSearchTerms()
  219. {
  220. return cleanupId($this->id).'-'.
  221. cleanupId($this->truename).'-'.
  222. cleanupId($this->groupname);
  223. }
  224. public function getBlogUrl()
  225. {
  226. return \Movim\Route::urlize(
  227. 'blog',
  228. ($this->user && isset($this->user->nickname))
  229. ? $this->user->nickname
  230. : $this->id
  231. );
  232. }
  233. public function isEmpty(): bool
  234. {
  235. $this->isValidDate();
  236. return ($this->fn == null
  237. && $this->name == null
  238. && $this->date == null
  239. && $this->url == null
  240. && $this->email == null
  241. && $this->description == null);
  242. }
  243. public function isValidDate(): bool
  244. {
  245. if (isset($this->date)
  246. && $this->date != '0000-00-00T00:00:00+0000'
  247. && $this->date != '1970-01-01 00:00:00'
  248. && $this->date != '1970-01-01 01:00:00'
  249. && $this->date != '1970-01-01T00:00:00+0000') {
  250. return true;
  251. }
  252. $this->date = null;
  253. return false;
  254. }
  255. public function isFromMuc(): bool
  256. {
  257. return strpos($this->jid, '/') !== false;
  258. }
  259. public function isOld(): bool
  260. {
  261. return (strtotime($this->updated) < mktime( // We update the 1 day old vcards
  262. gmdate("H"),
  263. gmdate("i")-10,
  264. gmdate("s"),
  265. gmdate("m"),
  266. gmdate("d"),
  267. gmdate("Y")
  268. )
  269. );
  270. }
  271. public function isMe(): bool
  272. {
  273. return ($this->id == \App\User::me()->id);
  274. }
  275. }