/usr/share/horde/turba/lib/Driver/Facebook.php is in php-horde-turba 4.2.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | <?php
/**
* Read-only Turba directory driver for Facebook friends. Requires a Horde
* application to be setup on Facebook and configured in horde/config/conf.php.
*
* Of limited utility since email addresses are not retrievable via the
* Facebook API, unless the user allows the Horde application to access it -
* and even then, it's a proxied email address.
*
* Copyright 2009-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (ASL). If you did
* did not receive this file, see http://www.horde.org/licenses/apache.
*
* @author Michael J. Rubinsky <mrubinsk@horde.org>
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/apache ASL
* @package Turba
*/
class Turba_Driver_Facebook extends Turba_Driver
{
/**
* TODO
*
* @var Horde_Service_Facebook
*/
protected $_facebook;
/**
* Cache
*
* @var Horde_Cache
*/
protected $_cache;
/**
* Constructor
*
* @param string $name
* @param array $params
*/
public function __construct($name = '', array $params = array())
{
parent::__construct($name, $params);
$this->_facebook = $params['storage'];
unset($params['storage']);
$this->_cache = $GLOBALS['injector']->getInstance('Horde_Cache');
}
/**
* Checks if the current user has the requested permissions on this
* source.
*
* @param integer $perm The permission to check for.
*
* @return boolean True if the user has permission, otherwise false.
*/
public function hasPermission($perm)
{
switch ($perm) {
case Horde_Perms::DELETE:
case Horde_Perms::EDIT:
return false;
default:
return true;
}
}
/**
* Searches with the given criteria and returns a
* filtered list of results. If the criteria parameter is an empty array,
* all records will be returned.
*
* @param array $criteria Array containing the search criteria.
* @param array $fields List of fields to return.
* @param array $blobFields A list of fields that contain binary data.
* @param boolean $count_only Only return the count of matching items.
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _search(array $criteria, array $fields, array $blobFields = array(), $count_only = false)
{
$cid = implode('|', array(
'turba_fb_search',
$GLOBALS['registry']->getAuth(),
md5(json_encode($criteria) . '_' .
implode('.' , $fields) . '_' .
implode('.', $blobFields))
));
if ($values = $this->_cache->get($cid, 3600)) {
$values = json_decode($values, true);
return $count_only ? count($values) : $values;
}
$results = array();
foreach ($this->_getAddressBook($fields) as $key => $contact) {
// If no search criteria, return full list.
if (!count($criteria) ||
$this->_criteriaFound($criteria, $contact)) {
$results[$key] = $contact;
}
}
$this->_cache->set($cid, json_encode($results));
return $count_only ? count($results) : $results;
}
/**
*/
protected function _criteriaFound($criteria, $contact, $and = false)
{
foreach ($criteria as $key => $val) {
switch (strval($key)) {
case 'AND':
return count($val)
? $this->_criteriaFound($val, $contact, true)
: false;
case 'OR':
return $this->_criteriaFound($val, $contact, false);
default:
if (!isset($val['field'])) {
if ($this->_criteriaFound($val, $contact)) {
if (!$and) {
return true;
}
} elseif ($and) {
return false;
}
} elseif (isset($contact[$val['field']])) {
switch ($val['op']) {
case 'LIKE':
if (stristr($contact[$val['field']], $val['test']) === false) {
if ($and) {
return false;
}
} elseif (!$and) {
return true;
}
break;
}
}
break;
}
}
return $and;
}
/**
* Reads the given data from the address book and returns the results.
*
* @param string $key The primary key field to use.
* @param mixed $ids The ids of the contacts to load.
* @param string $owner Only return contacts owned by this user.
* @param array $fields List of fields to return.
* @param array $blobFields Array of fields containing binary data.
* @param array $dateFields Array of fields containing date data.
* @since 4.2.0
*
* @return array Hash containing the search results.
* @throws Turba_Exception
*/
protected function _read($key, $ids, $owner, array $fields,
array $blobFields = array(),
array $dateFields = array())
{
return $this->_getEntry($ids, $fields);
}
/**
* TODO
*/
protected function _getEntry(array $keys, array $fields)
{
$key = 'turba_fb_getEntry|' . $GLOBALS['registry']->getAuth() . '|' . md5(implode('.', $keys) . '|' . implode('.', $fields));
if ($values = $this->_cache->get($key, 3600)) {
$values = json_decode($values, true);
return $values;
}
$cleanfields = implode(', ', $this->_prepareFields($fields));
$fql = 'SELECT ' . $cleanfields . ' FROM user WHERE uid IN (' . implode(', ', $keys) . ')';
try {
$results = array($this->_fqlToTurba($fields, current($this->_facebook->fql->run($fql))));
$this->_cache->set($key, json_encode($results));
return $results;
} catch (Horde_Service_Facebook_Exception $e) {
Horde::log($e, 'ERR');
throw new Turba_Exception($e);
}
}
/**
* TODO
*/
protected function _getAddressBook(array $fields = array())
{
$key = 'turba_fb_getAddressBook|' . $GLOBALS['registry']->getAuth() . '|' . md5(implode('.', $fields));
if ($values = $this->_cache->get($key, 3600)) {
return json_decode($values, true);
}
$cleanfields = implode(', ', $this->_prepareFields($fields));
try {
$fql = 'SELECT ' . $cleanfields . ' FROM user WHERE uid IN ('
. 'SELECT uid2 FROM friend WHERE uid1=' . $this->_facebook->auth->getLoggedInUser() . ')';
$results = $this->_facebook->fql->run($fql);
} catch (Horde_Service_Facebook_Exception $e) {
Horde::log($e, 'ERR');
if ($e->getCode() == Horde_Service_Facebook_ErrorCodes::API_EC_PARAM_SESSION_KEY) {
throw new Turba_Exception(_("You are not connected to Facebook. Create a Facebook connection in the Global Preferences."));
}
throw new Turba_Exception($e);
}
// Now pull out the results that are arrays
$addressbook = array();
foreach ($results as &$result) {
$addressbook[$result['uid']] = $this->_fqlToTurba($fields, $result);
}
$this->_cache->set($key, json_encode($addressbook));
return $addressbook;
}
protected function _fqlToTurba($fields, $result)
{
//$remove = array();
foreach ($fields as $field) {
if (strpos($field, '.') !== false) {
$key = substr($field, 0, strpos($field, '.'));
$subfield = substr($field, strpos($field, '.') + 1);
$result[$field] = $result[$key][$subfield];
}
}
if (!empty($result['birthday_date'])) {
// Make sure the birthdate is in a standard format that
// listDateObjects will understand.
$bday = new Horde_Date($result['birthday_date']);
$result['birthday_date'] = $bday->format('Y-m-d');
}
return $result;
}
/**
* Parse out a fields array for use in a FB FQL query.
*
* @param array $fields The fields, as configured in backends.php
*
*
*/
protected function _prepareFields($fields)
{
return array_map(array($this, '_prepareCallback'), $fields);
}
static public function _prepareCallback($field)
{
if (strpos($field, '.') === false) {
return $field;
}
return substr($field, 0, strpos($field, '.'));
}
}
|