2017-11-15 14:47:28 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2020-02-09 14:45:36 +00:00
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2017-11-15 14:47:28 +00:00
|
|
|
*/
|
2020-02-09 14:45:36 +00:00
|
|
|
|
2017-11-15 14:47:28 +00:00
|
|
|
namespace Friendica\Model;
|
|
|
|
|
2018-07-20 02:15:21 +00:00
|
|
|
use Exception;
|
2018-08-11 20:40:44 +00:00
|
|
|
use Friendica\Core\Protocol;
|
2018-07-20 12:19:26 +00:00
|
|
|
use Friendica\Database\DBA;
|
2018-01-27 02:38:34 +00:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2017-11-15 14:47:28 +00:00
|
|
|
|
|
|
|
/**
|
2020-01-19 06:05:23 +00:00
|
|
|
* This class handles GlobalContact related functions
|
2017-11-15 14:47:28 +00:00
|
|
|
*/
|
2017-12-07 14:09:28 +00:00
|
|
|
class GContact
|
2017-11-15 14:47:28 +00:00
|
|
|
{
|
2017-11-19 22:04:40 +00:00
|
|
|
/**
|
|
|
|
* @param integer $uid id
|
|
|
|
* @param integer $cid id
|
|
|
|
* @return integer
|
2019-01-07 15:24:06 +00:00
|
|
|
* @throws Exception
|
2017-11-19 22:04:40 +00:00
|
|
|
*/
|
2017-11-15 14:47:28 +00:00
|
|
|
public static function countCommonFriends($uid, $cid)
|
|
|
|
{
|
|
|
|
$r = q(
|
|
|
|
"SELECT count(*) as `total`
|
|
|
|
FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
|
|
|
|
WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
|
2020-07-19 11:42:23 +00:00
|
|
|
NOT `gcontact`.`failed`
|
2019-10-16 12:58:09 +00:00
|
|
|
AND `gcontact`.`nurl` IN (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d) ",
|
2017-11-15 14:47:28 +00:00
|
|
|
intval($cid),
|
|
|
|
intval($uid),
|
|
|
|
intval($uid),
|
|
|
|
intval($cid)
|
|
|
|
);
|
|
|
|
|
2018-07-21 12:46:04 +00:00
|
|
|
if (DBA::isResult($r)) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return $r[0]['total'];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-19 22:04:40 +00:00
|
|
|
/**
|
|
|
|
* @param integer $uid id
|
|
|
|
* @param integer $zcid zcid
|
|
|
|
* @return integer
|
2019-01-07 15:24:06 +00:00
|
|
|
* @throws Exception
|
2017-11-19 22:04:40 +00:00
|
|
|
*/
|
2017-11-15 14:47:28 +00:00
|
|
|
public static function countCommonFriendsZcid($uid, $zcid)
|
|
|
|
{
|
|
|
|
$r = q(
|
|
|
|
"SELECT count(*) as `total`
|
|
|
|
FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
|
|
|
|
where `glink`.`zcid` = %d
|
2019-10-16 12:58:09 +00:00
|
|
|
and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0) ",
|
2017-11-15 14:47:28 +00:00
|
|
|
intval($zcid),
|
|
|
|
intval($uid)
|
|
|
|
);
|
|
|
|
|
2018-07-21 12:46:04 +00:00
|
|
|
if (DBA::isResult($r)) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return $r[0]['total'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-19 22:04:40 +00:00
|
|
|
/**
|
2018-01-05 00:42:48 +00:00
|
|
|
* @param integer $uid user
|
|
|
|
* @param integer $cid cid
|
2017-11-19 22:04:40 +00:00
|
|
|
* @param integer $start optional, default 0
|
|
|
|
* @param integer $limit optional, default 9999
|
|
|
|
* @param boolean $shuffle optional, default false
|
|
|
|
* @return object
|
2019-01-07 15:24:06 +00:00
|
|
|
* @throws Exception
|
2017-11-19 22:04:40 +00:00
|
|
|
*/
|
2017-11-15 14:47:28 +00:00
|
|
|
public static function commonFriends($uid, $cid, $start = 0, $limit = 9999, $shuffle = false)
|
|
|
|
{
|
|
|
|
if ($shuffle) {
|
|
|
|
$sql_extra = " order by rand() ";
|
|
|
|
} else {
|
|
|
|
$sql_extra = " order by `gcontact`.`name` asc ";
|
|
|
|
}
|
|
|
|
|
|
|
|
$r = q(
|
|
|
|
"SELECT `gcontact`.*, `contact`.`id` AS `cid`
|
|
|
|
FROM `glink`
|
|
|
|
INNER JOIN `gcontact` ON `glink`.`gcid` = `gcontact`.`id`
|
|
|
|
INNER JOIN `contact` ON `gcontact`.`nurl` = `contact`.`nurl`
|
|
|
|
WHERE `glink`.`cid` = %d and `glink`.`uid` = %d
|
|
|
|
AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `contact`.`blocked` = 0
|
|
|
|
AND `contact`.`hidden` = 0 AND `contact`.`id` != %d
|
2020-07-19 11:42:23 +00:00
|
|
|
AND NOT `gcontact`.`failed`
|
2017-11-15 14:47:28 +00:00
|
|
|
$sql_extra LIMIT %d, %d",
|
|
|
|
intval($cid),
|
|
|
|
intval($uid),
|
|
|
|
intval($uid),
|
|
|
|
intval($cid),
|
|
|
|
intval($start),
|
|
|
|
intval($limit)
|
|
|
|
);
|
|
|
|
|
2018-07-21 12:46:04 +00:00
|
|
|
/// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
|
2017-11-15 14:47:28 +00:00
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
2017-11-19 22:04:40 +00:00
|
|
|
/**
|
2018-01-05 00:42:48 +00:00
|
|
|
* @param integer $uid user
|
|
|
|
* @param integer $zcid zcid
|
2017-11-19 22:04:40 +00:00
|
|
|
* @param integer $start optional, default 0
|
|
|
|
* @param integer $limit optional, default 9999
|
|
|
|
* @param boolean $shuffle optional, default false
|
|
|
|
* @return object
|
2019-01-07 15:24:06 +00:00
|
|
|
* @throws Exception
|
2017-11-19 22:04:40 +00:00
|
|
|
*/
|
|
|
|
public static function commonFriendsZcid($uid, $zcid, $start = 0, $limit = 9999, $shuffle = false)
|
2017-11-15 14:47:28 +00:00
|
|
|
{
|
|
|
|
if ($shuffle) {
|
|
|
|
$sql_extra = " order by rand() ";
|
|
|
|
} else {
|
|
|
|
$sql_extra = " order by `gcontact`.`name` asc ";
|
|
|
|
}
|
|
|
|
|
|
|
|
$r = q(
|
|
|
|
"SELECT `gcontact`.*
|
|
|
|
FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
|
|
|
|
where `glink`.`zcid` = %d
|
2019-10-16 12:58:09 +00:00
|
|
|
and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0)
|
2017-11-15 14:47:28 +00:00
|
|
|
$sql_extra limit %d, %d",
|
|
|
|
intval($zcid),
|
|
|
|
intval($uid),
|
|
|
|
intval($start),
|
|
|
|
intval($limit)
|
|
|
|
);
|
|
|
|
|
2018-07-21 12:46:04 +00:00
|
|
|
/// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
|
2017-11-15 14:47:28 +00:00
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
|
2017-11-19 22:04:40 +00:00
|
|
|
/**
|
2018-01-05 00:42:48 +00:00
|
|
|
* @param integer $uid user
|
|
|
|
* @param integer $cid cid
|
2017-11-19 22:04:40 +00:00
|
|
|
* @return integer
|
2019-01-07 15:24:06 +00:00
|
|
|
* @throws Exception
|
2017-11-19 22:04:40 +00:00
|
|
|
*/
|
2017-11-15 14:47:28 +00:00
|
|
|
public static function countAllFriends($uid, $cid)
|
|
|
|
{
|
|
|
|
$r = q(
|
|
|
|
"SELECT count(*) as `total`
|
|
|
|
FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
|
|
|
|
where `glink`.`cid` = %d and `glink`.`uid` = %d AND
|
2020-07-19 11:42:23 +00:00
|
|
|
NOT `gcontact`.`failed`",
|
2017-11-15 14:47:28 +00:00
|
|
|
intval($cid),
|
|
|
|
intval($uid)
|
|
|
|
);
|
|
|
|
|
2018-07-21 12:46:04 +00:00
|
|
|
if (DBA::isResult($r)) {
|
2017-11-15 14:47:28 +00:00
|
|
|
return $r[0]['total'];
|
|
|
|
}
|
2017-11-19 22:06:18 +00:00
|
|
|
|
2017-11-15 14:47:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-19 22:04:40 +00:00
|
|
|
/**
|
2018-01-05 00:42:48 +00:00
|
|
|
* @param integer $uid user
|
|
|
|
* @param integer $cid cid
|
2017-11-19 22:04:40 +00:00
|
|
|
* @param integer $start optional, default 0
|
|
|
|
* @param integer $limit optional, default 80
|
2018-01-05 00:42:48 +00:00
|
|
|
* @return array
|
2019-01-07 15:24:06 +00:00
|
|
|
* @throws Exception
|
2017-11-19 22:04:40 +00:00
|
|
|
*/
|
2017-11-15 14:47:28 +00:00
|
|
|
public static function allFriends($uid, $cid, $start = 0, $limit = 80)
|
|
|
|
{
|
|
|
|
$r = q(
|
|
|
|
"SELECT `gcontact`.*, `contact`.`id` AS `cid`
|
|
|
|
FROM `glink`
|
|
|
|
INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
|
|
|
|
LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d
|
|
|
|
WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
|
2020-07-19 11:42:23 +00:00
|
|
|
NOT `gcontact`.`failed`
|
2017-11-15 14:47:28 +00:00
|
|
|
ORDER BY `gcontact`.`name` ASC LIMIT %d, %d ",
|
|
|
|
intval($uid),
|
|
|
|
intval($cid),
|
|
|
|
intval($uid),
|
|
|
|
intval($start),
|
|
|
|
intval($limit)
|
|
|
|
);
|
|
|
|
|
2018-07-21 12:46:04 +00:00
|
|
|
/// @TODO Check all calling-findings of this function if they properly use DBA::isResult()
|
2017-11-15 14:47:28 +00:00
|
|
|
return $r;
|
|
|
|
}
|
|
|
|
}
|