1
0
Fork 0

Merge branch 'item-activities' of github.com:annando/friendica into item-activities

This commit is contained in:
Michael 2018-07-08 04:56:35 +00:00
commit 74d5eec571
60 changed files with 2256 additions and 748 deletions

View file

@ -181,7 +181,10 @@ class App
$this->process_id = uniqid('log', true);
startup();
set_time_limit(0);
// This has to be quite large to deal with embedded private photos
ini_set('pcre.backtrack_limit', 500000);
$this->scheme = 'http';
@ -290,7 +293,7 @@ class App
$this->is_tablet = $mobile_detect->isTablet();
// Friendica-Client
$this->is_friendica_app = ($_SERVER['HTTP_USER_AGENT'] == 'Apache-HttpClient/UNAVAILABLE (java 1.4)');
$this->is_friendica_app = isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] == 'Apache-HttpClient/UNAVAILABLE (java 1.4)';
// Register template engines
$this->register_template_engine('Friendica\Render\FriendicaSmartyEngine');
@ -863,7 +866,7 @@ class App
return;
}
array_unshift($args, ((x($this->config, 'php_path')) && (strlen($this->config['php_path'])) ? $this->config['php_path'] : 'php'));
array_unshift($args, $this->getConfigValue('config', 'php_path', 'php'));
for ($x = 0; $x < count($args); $x ++) {
$args[$x] = escapeshellarg($args[$x]);
@ -875,7 +878,7 @@ class App
return;
}
if (Config::get('system', 'proc_windows')) {
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$resource = proc_open('cmd /c start /b ' . $cmdline, [], $foo, $this->get_basepath());
} else {
$resource = proc_open($cmdline . ' &', [], $foo, $this->get_basepath());

View file

@ -4,8 +4,7 @@
*/
namespace Friendica\Core;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\Cache\CacheDriverFactory;
/**
* @brief Class for storing data for a short time
@ -24,31 +23,13 @@ class Cache extends \Friendica\BaseObject
/**
* @var Cache\ICacheDriver
*/
static $driver = null;
private static $driver = null;
public static function init()
{
switch(Config::get('system', 'cache_driver', 'database')) {
case 'memcache':
$memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
$memcache_port = Config::get('system', 'memcache_port', 11211);
$driver_name = Config::get('system', 'cache_driver', 'database');
self::$driver = new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
break;
case 'memcached':
$memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]);
self::$driver = new Cache\MemcachedCacheDriver($memcached_hosts);
break;
case 'redis':
$redis_host = Config::get('system', 'redis_host', '127.0.0.1');
$redis_port = Config::get('system', 'redis_port', 6379);
self::$driver = new Cache\RedisCacheDriver($redis_host, $redis_port);
break;
default:
self::$driver = new Cache\DatabaseCacheDriver();
}
self::$driver = CacheDriverFactory::create($driver_name);
}
/**

View file

@ -0,0 +1,24 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\BaseObject;
/**
* Abstract class for common used functions
*
* Class AbstractCacheDriver
*
* @package Friendica\Core\Cache
*/
abstract class AbstractCacheDriver extends BaseObject
{
/**
* @param string $key The original key
* @return string The cache key used for the cache
*/
protected function getCacheKey($key) {
// We fetch with the hostname as key to avoid problems with other applications
return self::getApp()->get_hostname() . ":" . $key;
}
}

View file

@ -0,0 +1,83 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* Implementation of the IMemoryCacheDriver mainly for testing purpose
*
* Class ArrayCache
*
* @package Friendica\Core\Cache
*/
class ArrayCache extends AbstractCacheDriver implements IMemoryCacheDriver
{
use TraitCompareDelete;
/** @var array Array with the cached data */
protected $cachedData = array();
/**
* (@inheritdoc)
*/
public function get($key)
{
if (isset($this->cachedData[$key])) {
return $this->cachedData[$key];
}
return null;
}
/**
* (@inheritdoc)
*/
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
$this->cachedData[$key] = $value;
return true;
}
/**
* (@inheritdoc)
*/
public function delete($key)
{
unset($this->cachedData[$key]);
return true;
}
/**
* (@inheritdoc)
*/
public function clear($outdated = true)
{
$this->cachedData = [];
return true;
}
/**
* (@inheritdoc)
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
{
if (isset($this->cachedData[$key])) {
return false;
} else {
return $this->set($key, $value, $ttl);
}
}
/**
* (@inheritdoc)
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
{
if ($this->get($key) === $oldValue) {
return $this->set($key, $newValue);
} else {
return false;
}
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Config;
/**
* Class CacheDriverFactory
*
* @package Friendica\Core\Cache
*
* A basic class to generate a CacheDriver
*/
class CacheDriverFactory
{
/**
* This method creates a CacheDriver for the given cache driver name
*
* @param string $driver The name of the cache driver
* @return ICacheDriver The instance of the CacheDriver
* @throws \Exception The exception if something went wrong during the CacheDriver creation
*/
public static function create($driver) {
switch ($driver) {
case 'memcache':
$memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
$memcache_port = Config::get('system', 'memcache_port', 11211);
return new MemcacheCacheDriver($memcache_host, $memcache_port);
break;
case 'memcached':
$memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]);
return new MemcachedCacheDriver($memcached_hosts);
break;
case 'redis':
$redis_host = Config::get('system', 'redis_host', '127.0.0.1');
$redis_port = Config::get('system', 'redis_port', 6379);
return new RedisCacheDriver($redis_host, $redis_port);
break;
default:
return new DatabaseCacheDriver();
}
}
}

View file

@ -12,7 +12,7 @@ use Friendica\Util\DateTimeFormat;
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class DatabaseCacheDriver implements ICacheDriver
class DatabaseCacheDriver extends AbstractCacheDriver implements ICacheDriver
{
public function get($key)
{
@ -33,11 +33,11 @@ class DatabaseCacheDriver implements ICacheDriver
return null;
}
public function set($key, $value, $duration = Cache::MONTH)
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
$fields = [
'v' => serialize($value),
'expires' => DateTimeFormat::utc('now + ' . $duration . ' seconds'),
'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds'),
'updated' => DateTimeFormat::utcNow()
];
@ -49,8 +49,12 @@ class DatabaseCacheDriver implements ICacheDriver
return dba::delete('cache', ['k' => $key]);
}
public function clear()
public function clear($outdated = true)
{
return dba::delete('cache', ['`expires` < NOW()']);
if ($outdated) {
return dba::delete('cache', ['`expires` < NOW()']);
} else {
return dba::delete('cache', ['`k` IS NOT NULL ']);
}
}
}

View file

@ -25,17 +25,16 @@ interface ICacheDriver
*
* @param string $key The cache key
* @param mixed $value The value to store
* @param integer $duration The cache lifespan, must be one of the Cache constants
* @param integer $ttl The cache lifespan, must be one of the Cache constants
*
* @return bool
*/
public function set($key, $value, $duration = Cache::MONTH);
public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
* Delete a key from the cache
*
* @param string $key
* @param string $key The cache key
*
* @return bool
*/
@ -43,8 +42,9 @@ interface ICacheDriver
/**
* Remove outdated data from the cache
* @param boolean $outdated just remove outdated values
*
* @return bool
*/
public function clear();
public function clear($outdated = true);
}

View file

@ -0,0 +1,45 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* This interface defines methods for Memory-Caches only
*
* Interface IMemoryCacheDriver
*
* @package Friendica\Core\Cache
*/
interface IMemoryCacheDriver extends ICacheDriver
{
/**
* Sets a value if it's not already stored
*
* @param string $key The cache key
* @param mixed $value The old value we know from the cache
* @param int $ttl The cache lifespan, must be one of the Cache constants
* @return bool
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
* Compares if the old value is set and sets the new value
*
* @param string $key The cache key
* @param mixed $oldValue The old value we know from the cache
* @param mixed $newValue The new value we want to set
* @param int $ttl The cache lifespan, must be one of the Cache constants
*
* @return bool
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES);
/**
* Compares if the old value is set and removes it
*
* @param string $key The cache key
* @param mixed $value The old value we know and want to delete
* @return bool
*/
public function compareDelete($key, $value);
}

View file

@ -2,7 +2,6 @@
namespace Friendica\Core\Cache;
use Friendica\BaseObject;
use Friendica\Core\Cache;
/**
@ -10,10 +9,13 @@ use Friendica\Core\Cache;
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class MemcacheCacheDriver extends BaseObject implements ICacheDriver
class MemcacheCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
{
use TraitCompareSet;
use TraitCompareDelete;
/**
* @var Memcache
* @var \Memcache
*/
private $memcache;
@ -30,12 +32,16 @@ class MemcacheCacheDriver extends BaseObject implements ICacheDriver
}
}
/**
* (@inheritdoc)
*/
public function get($key)
{
$return = null;
$cachekey = $this->getCacheKey($key);
// We fetch with the hostname as key to avoid problems with other applications
$cached = $this->memcache->get(self::getApp()->get_hostname() . ':' . $key);
$cached = $this->memcache->get($cachekey);
// @see http://php.net/manual/en/memcache.get.php#84275
if (is_bool($cached) || is_double($cached) || is_long($cached)) {
@ -54,24 +60,57 @@ class MemcacheCacheDriver extends BaseObject implements ICacheDriver
return $return;
}
public function set($key, $value, $duration = Cache::MONTH)
/**
* (@inheritdoc)
*/
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
$cachekey = $this->getCacheKey($key);
// We store with the hostname as key to avoid problems with other applications
return $this->memcache->set(
self::getApp()->get_hostname() . ":" . $key,
serialize($value),
MEMCACHE_COMPRESSED,
time() + $duration
);
if ($ttl > 0) {
return $this->memcache->set(
$cachekey,
serialize($value),
MEMCACHE_COMPRESSED,
time() + $ttl
);
} else {
return $this->memcache->set(
$cachekey,
serialize($value),
MEMCACHE_COMPRESSED
);
}
}
/**
* (@inheritdoc)
*/
public function delete($key)
{
return $this->memcache->delete($key);
$cachekey = $this->getCacheKey($key);
return $this->memcache->delete($cachekey);
}
public function clear()
/**
* (@inheritdoc)
*/
public function clear($outdated = true)
{
return true;
if ($outdated) {
return true;
} else {
return $this->memcache->flush();
}
}
/**
* (@inheritdoc)
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
{
$cachekey = $this->getCacheKey($key);
return $this->memcache->add($cachekey, serialize($value), MEMCACHE_COMPRESSED, $ttl);
}
}

View file

@ -2,7 +2,6 @@
namespace Friendica\Core\Cache;
use Friendica\BaseObject;
use Friendica\Core\Cache;
/**
@ -10,10 +9,13 @@ use Friendica\Core\Cache;
*
* @author Hypolite Petovan <mrpetovan@gmail.com>
*/
class MemcachedCacheDriver extends BaseObject implements ICacheDriver
class MemcachedCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
{
use TraitCompareSet;
use TraitCompareDelete;
/**
* @var Memcached
* @var \Memcached
*/
private $memcached;
@ -35,9 +37,10 @@ class MemcachedCacheDriver extends BaseObject implements ICacheDriver
public function get($key)
{
$return = null;
$cachekey = $this->getCacheKey($key);
// We fetch with the hostname as key to avoid problems with other applications
$value = $this->memcached->get(self::getApp()->get_hostname() . ':' . $key);
$value = $this->memcached->get($cachekey);
if ($this->memcached->getResultCode() === \Memcached::RES_SUCCESS) {
$return = $value;
@ -46,25 +49,52 @@ class MemcachedCacheDriver extends BaseObject implements ICacheDriver
return $return;
}
public function set($key, $value, $duration = Cache::MONTH)
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
$cachekey = $this->getCacheKey($key);
// We store with the hostname as key to avoid problems with other applications
return $this->memcached->set(
self::getApp()->get_hostname() . ':' . $key,
$value,
time() + $duration
);
if ($ttl > 0) {
return $this->memcached->set(
$cachekey,
$value,
$ttl
);
} else {
return $this->memcached->set(
$cachekey,
$value
);
}
}
public function delete($key)
{
$return = $this->memcached->delete(self::getApp()->get_hostname() . ':' . $key);
return $return;
$cachekey = $this->getCacheKey($key);
return $this->memcached->delete($cachekey);
}
public function clear()
public function clear($outdated = true)
{
return true;
if ($outdated) {
return true;
} else {
return $this->memcached->flush();
}
}
/**
* @brief Sets a value if it's not already stored
*
* @param string $key The cache key
* @param mixed $value The old value we know from the cache
* @param int $ttl The cache lifespan, must be one of the Cache constants
* @return bool
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
{
$cachekey = $this->getCacheKey($key);
return $this->memcached->add($cachekey, $value, $ttl);
}
}

View file

@ -2,7 +2,6 @@
namespace Friendica\Core\Cache;
use Friendica\BaseObject;
use Friendica\Core\Cache;
/**
@ -11,10 +10,10 @@ use Friendica\Core\Cache;
* @author Hypolite Petovan <mrpetovan@gmail.com>
* @author Roland Haeder <roland@mxchange.org>
*/
class RedisCacheDriver extends BaseObject implements ICacheDriver
class RedisCacheDriver extends AbstractCacheDriver implements IMemoryCacheDriver
{
/**
* @var Redis
* @var \Redis
*/
private $redis;
@ -34,16 +33,14 @@ class RedisCacheDriver extends BaseObject implements ICacheDriver
public function get($key)
{
$return = null;
$cachekey = $this->getCacheKey($key);
// We fetch with the hostname as key to avoid problems with other applications
$cached = $this->redis->get(self::getApp()->get_hostname() . ':' . $key);
// @see http://php.net/manual/en/redis.get.php#84275
if (is_bool($cached) || is_double($cached) || is_long($cached)) {
return $return;
$cached = $this->redis->get($cachekey);
if ($cached === false && !$this->redis->exists($cachekey)) {
return null;
}
$value = @unserialize($cached);
$value = json_decode($cached);
// Only return a value if the serialized value is valid.
// We also check if the db entry is a serialized
@ -55,23 +52,94 @@ class RedisCacheDriver extends BaseObject implements ICacheDriver
return $return;
}
public function set($key, $value, $duration = Cache::MONTH)
public function set($key, $value, $ttl = Cache::FIVE_MINUTES)
{
// We store with the hostname as key to avoid problems with other applications
return $this->redis->set(
self::getApp()->get_hostname() . ":" . $key,
serialize($value),
time() + $duration
);
$cachekey = $this->getCacheKey($key);
$cached = json_encode($value);
if ($ttl > 0) {
return $this->redis->setex(
$cachekey,
$ttl,
$cached
);
} else {
return $this->redis->set(
$cachekey,
$cached
);
}
}
public function delete($key)
{
return $this->redis->delete($key);
$cachekey = $this->getCacheKey($key);
return ($this->redis->delete($cachekey) > 0);
}
public function clear()
public function clear($outdated = true)
{
return true;
if ($outdated) {
return true;
} else {
return $this->redis->flushAll();
}
}
/**
* (@inheritdoc)
*/
public function add($key, $value, $ttl = Cache::FIVE_MINUTES)
{
$cachekey = $this->getCacheKey($key);
$cached = json_encode($value);
return $this->redis->setnx($cachekey, $cached);
}
/**
* (@inheritdoc)
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES)
{
$cachekey = $this->getCacheKey($key);
$newCached = json_encode($newValue);
$this->redis->watch($cachekey);
// If the old value isn't what we expected, somebody else changed the key meanwhile
if ($this->get($key) === $oldValue) {
if ($ttl > 0) {
$result = $this->redis->multi()
->setex($cachekey, $ttl, $newCached)
->exec();
} else {
$result = $this->redis->multi()
->set($cachekey, $newValue)
->exec();
}
return $result !== false;
}
$this->redis->unwatch();
return false;
}
/**
* (@inheritdoc)
*/
public function compareDelete($key, $value)
{
$cachekey = $this->getCacheKey($key);
$this->redis->watch($cachekey);
// If the old value isn't what we expected, somebody else changed the key meanwhile
if ($this->get($key) === $value) {
$result = $this->redis->multi()
->del($cachekey)
->exec();
return $result !== false;
}
$this->redis->unwatch();
return false;
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* Trait TraitCompareSetDelete
*
* This Trait is to compensate non native "exclusive" sets/deletes in caches
*
* @package Friendica\Core\Cache
*/
trait TraitCompareDelete
{
abstract public function get($key);
abstract public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
abstract public function delete($key);
abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
* NonNative - Compares if the old value is set and removes it
*
* @param string $key The cache key
* @param mixed $value The old value we know and want to delete
* @return bool
*/
public function compareDelete($key, $value) {
if ($this->add($key . "_lock", true)) {
if ($this->get($key) === $value) {
$this->delete($key);
$this->delete($key . "_lock");
return true;
} else {
$this->delete($key . "_lock");
return false;
}
} else {
return false;
}
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace Friendica\Core\Cache;
use Friendica\Core\Cache;
/**
* Trait TraitCompareSetDelete
*
* This Trait is to compensate non native "exclusive" sets/deletes in caches
*
* @package Friendica\Core\Cache
*/
trait TraitCompareSet
{
abstract public function get($key);
abstract public function set($key, $value, $ttl = Cache::FIVE_MINUTES);
abstract public function delete($key);
abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES);
/**
* NonNative - Compares if the old value is set and sets the new value
*
* @param string $key The cache key
* @param mixed $oldValue The old value we know from the cache
* @param mixed $newValue The new value we want to set
* @param int $ttl The cache lifespan, must be one of the Cache constants
*
* @return bool
*/
public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES) {
if ($this->add($key . "_lock", true)) {
if ($this->get($key) === $oldValue) {
$this->set($key, $newValue, $ttl);
$this->delete($key . "_lock");
return true;
} else {
$this->delete($key . "_lock");
return false;
}
} else {
return false;
}
}
}

140
src/Core/Lock.php Normal file
View file

@ -0,0 +1,140 @@
<?php
namespace Friendica\Core;
/**
* @file src/Core/Lock.php
* @brief Functions for preventing parallel execution of functions
*/
use Friendica\Core\Cache\CacheDriverFactory;
use Friendica\Core\Cache\IMemoryCacheDriver;
/**
* @brief This class contain Functions for preventing parallel execution of functions
*/
class Lock
{
/**
* @var Lock\ILockDriver;
*/
static $driver = null;
public static function init()
{
$lock_driver = Config::get('system', 'lock_driver', 'default');
try {
switch ($lock_driver) {
case 'memcache':
case 'memcached':
case 'redis':
$cache_driver = CacheDriverFactory::create($lock_driver);
if ($cache_driver instanceof IMemoryCacheDriver) {
self::$driver = new Lock\CacheLockDriver($cache_driver);
}
break;
case 'database':
self::$driver = new Lock\DatabaseLockDriver();
break;
case 'semaphore':
self::$driver = new Lock\SemaphoreLockDriver();
break;
default:
self::useAutoDriver();
}
} catch (\Exception $exception) {
logger ('Driver \'' . $lock_driver . '\' failed - Fallback to \'useAutoDriver()\'');
self::useAutoDriver();
}
}
/**
* @brief This method tries to find the best - local - locking method for Friendica
*
* The following sequence will be tried:
* 1. Semaphore Locking
* 2. Cache Locking
* 3. Database Locking
*
*/
private static function useAutoDriver() {
// 1. Try to use Semaphores for - local - locking
if (function_exists('sem_get')) {
try {
self::$driver = new Lock\SemaphoreLockDriver();
return;
} catch (\Exception $exception) {
logger ('Using Semaphore driver for locking failed: ' . $exception->getMessage());
}
}
// 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
$cache_driver = Config::get('system', 'cache_driver', 'database');
if ($cache_driver != 'database') {
try {
$lock_driver = CacheDriverFactory::create($cache_driver);
if ($lock_driver instanceof IMemoryCacheDriver) {
self::$driver = new Lock\CacheLockDriver($lock_driver);
}
return;
} catch (\Exception $exception) {
logger('Using Cache driver for locking failed: ' . $exception->getMessage());
}
}
// 3. Use Database Locking as a Fallback
self::$driver = new Lock\DatabaseLockDriver();
}
/**
* Returns the current cache driver
*
* @return Lock\ILockDriver;
*/
private static function getDriver()
{
if (self::$driver === null) {
self::init();
}
return self::$driver;
}
/**
* @brief Acquires a lock for a given name
*
* @param string $key Name of the lock
* @param integer $timeout Seconds until we give up
*
* @return boolean Was the lock successful?
*/
public static function acquire($key, $timeout = 120)
{
return self::getDriver()->acquireLock($key, $timeout);
}
/**
* @brief Releases a lock if it was set by us
*
* @param string $key Name of the lock
* @return void
*/
public static function release($key)
{
self::getDriver()->releaseLock($key);
}
/**
* @brief Releases all lock that were set by us
* @return void
*/
public static function releaseAll()
{
self::getDriver()->releaseAll();
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace Friendica\Core\Lock;
use Friendica\BaseObject;
/**
* Class AbstractLockDriver
*
* @package Friendica\Core\Lock
*
* Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
*/
abstract class AbstractLockDriver extends BaseObject implements ILockDriver
{
/**
* @var array The local acquired locks
*/
protected $acquiredLocks = [];
/**
* Check if we've locally acquired a lock
*
* @param string key The Name of the lock
* @return bool Returns true if the lock is set
*/
protected function hasAcquiredLock($key) {
return isset($this->acquireLock[$key]) && $this->acquiredLocks[$key] === true;
}
/**
* Mark a locally acquired lock
*
* @param string $key The Name of the lock
*/
protected function markAcquire($key) {
$this->acquiredLocks[$key] = true;
}
/**
* Mark a release of a locally acquired lock
*
* @param string $key The Name of the lock
*/
protected function markRelease($key) {
unset($this->acquiredLocks[$key]);
}
/**
* Releases all lock that were set by us
*
* @return void
*/
public function releaseAll() {
foreach ($this->acquiredLocks as $acquiredLock => $hasLock) {
$this->releaseLock($acquiredLock);
}
}
}

View file

@ -0,0 +1,89 @@
<?php
namespace Friendica\Core\Lock;
use Friendica\Core\Cache;
use Friendica\Core\Cache\IMemoryCacheDriver;
class CacheLockDriver extends AbstractLockDriver
{
/**
* @var \Friendica\Core\Cache\ICacheDriver;
*/
private $cache;
/**
* CacheLockDriver constructor.
*
* @param IMemoryCacheDriver $cache The CacheDriver for this type of lock
*/
public function __construct(IMemoryCacheDriver $cache)
{
$this->cache = $cache;
}
/**
* (@inheritdoc)
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
{
$got_lock = false;
$start = time();
$cachekey = self::getLockKey($key);
do {
$lock = $this->cache->get($cachekey);
// When we do want to lock something that was already locked by us.
if ((int)$lock == getmypid()) {
$got_lock = true;
}
// When we do want to lock something new
if (is_null($lock)) {
// At first initialize it with "0"
$this->cache->add($cachekey, 0);
// Now the value has to be "0" because otherwise the key was used by another process meanwhile
if ($this->cache->compareSet($cachekey, 0, getmypid(), $ttl)) {
$got_lock = true;
$this->markAcquire($key);
}
}
if (!$got_lock && ($timeout > 0)) {
usleep(rand(10000, 200000));
}
} while (!$got_lock && ((time() - $start) < $timeout));
return $got_lock;
}
/**
* (@inheritdoc)
*/
public function releaseLock($key)
{
$cachekey = self::getLockKey($key);
$this->cache->compareDelete($cachekey, getmypid());
$this->markRelease($key);
}
/**
* (@inheritdoc)
*/
public function isLocked($key)
{
$cachekey = self::getLockKey($key);
$lock = $this->cache->get($cachekey);
return isset($lock) && ($lock !== false);
}
/**
* @param string $key The original key
* @return string The cache key used for the cache
*/
private static function getLockKey($key) {
return "lock:" . $key;
}
}

View file

@ -0,0 +1,89 @@
<?php
namespace Friendica\Core\Lock;
use dba;
use Friendica\Core\Cache;
use Friendica\Database\DBM;
use Friendica\Util\DateTimeFormat;
/**
* Locking driver that stores the locks in the database
*/
class DatabaseLockDriver extends AbstractLockDriver
{
/**
* (@inheritdoc)
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
{
$got_lock = false;
$start = time();
do {
dba::lock('locks');
$lock = dba::selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
if (DBM::is_result($lock)) {
if ($lock['locked']) {
// We want to lock something that was already locked by us? So we got the lock.
if ($lock['pid'] == getmypid()) {
$got_lock = true;
}
}
if (!$lock['locked']) {
dba::update('locks', ['locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
$got_lock = true;
}
} else {
dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
$got_lock = true;
$this->markAcquire($key);
}
dba::unlock();
if (!$got_lock && ($timeout > 0)) {
usleep(rand(100000, 2000000));
}
} while (!$got_lock && ((time() - $start) < $timeout));
return $got_lock;
}
/**
* (@inheritdoc)
*/
public function releaseLock($key)
{
dba::delete('locks', ['name' => $key, 'pid' => getmypid()]);
$this->markRelease($key);
return;
}
/**
* (@inheritdoc)
*/
public function releaseAll()
{
dba::delete('locks', ['pid' => getmypid()]);
$this->acquiredLocks = [];
}
/**
* (@inheritdoc)
*/
public function isLocked($key)
{
$lock = dba::selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
if (DBM::is_result($lock)) {
return $lock['locked'] !== false;
} else {
return false;
}
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace Friendica\Core\Lock;
use Friendica\Core\Cache;
/**
* Lock Driver Interface
*
* @author Philipp Holzer <admin@philipp.info>
*/
interface ILockDriver
{
/**
* Checks, if a key is currently locked to a or my process
*
* @param string $key The name of the lock
* @return bool
*/
public function isLocked($key);
/**
*
* Acquires a lock for a given name
*
* @param string $key The Name of the lock
* @param integer $timeout Seconds until we give up
* @param integer $ttl Seconds The lock lifespan, must be one of the Cache constants
*
* @return boolean Was the lock successful?
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES);
/**
* Releases a lock if it was set by us
*
* @param string $key The Name of the lock
*
* @return void
*/
public function releaseLock($key);
/**
* Releases all lock that were set by us
*
* @return void
*/
public function releaseAll();
}

View file

@ -0,0 +1,72 @@
<?php
namespace Friendica\Core\Lock;
use Friendica\Core\Cache;
class SemaphoreLockDriver extends AbstractLockDriver
{
private static $semaphore = [];
public function __construct()
{
if (!function_exists('sem_get')) {
throw new \Exception('Semaphore lock not supported');
}
}
/**
* (@inheritdoc)
*/
private static function semaphoreKey($key)
{
$temp = get_temppath();
$file = $temp . '/' . $key . '.sem';
if (!file_exists($file)) {
file_put_contents($file, $key);
}
return ftok($file, 'f');
}
/**
* (@inheritdoc)
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
{
self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
if (self::$semaphore[$key]) {
if (sem_acquire(self::$semaphore[$key], ($timeout == 0))) {
$this->markAcquire($key);
return true;
}
}
return false;
}
/**
* (@inheritdoc)
*/
public function releaseLock($key)
{
if (empty(self::$semaphore[$key])) {
return false;
} else {
$success = @sem_release(self::$semaphore[$key]);
unset(self::$semaphore[$key]);
$this->markRelease($key);
return $success;
}
}
/**
* (@inheritdoc)
*/
public function isLocked($key)
{
return isset(self::$semaphore[$key]);
}
}

View file

@ -72,6 +72,7 @@ class System extends BaseObject
}
} elseif (!in_array($func['function'], $ignore)) {
$callstack[] = $func['function'];
$func['class'] = '';
$previous = $func;
}
}

View file

@ -4,15 +4,11 @@
*/
namespace Friendica\Core;
use Friendica\Core\Addon;
use Friendica\Core\Config;
use Friendica\Core\System;
use dba;
use Friendica\Database\DBM;
use Friendica\Model\Process;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Lock;
use Friendica\Util\Network;
use dba;
require_once 'include/dba.php';
@ -108,16 +104,16 @@ class Worker
}
// If possible we will fetch new jobs for this worker
if (!$refetched && Lock::set('worker_process', 0)) {
if (!$refetched && Lock::acquire('worker_process', 0)) {
$stamp = (float)microtime(true);
$refetched = self::findWorkerProcesses($passing_slow);
self::$db_duration += (microtime(true) - $stamp);
Lock::remove('worker_process');
Lock::release('worker_process');
}
}
// To avoid the quitting of multiple workers only one worker at a time will execute the check
if (Lock::set('worker', 0)) {
if (Lock::acquire('worker', 0)) {
$stamp = (float)microtime(true);
// Count active workers and compare them with a maximum value that depends on the load
if (self::tooMuchWorkers()) {
@ -130,7 +126,7 @@ class Worker
logger('Memory limit reached, quitting.', LOGGER_DEBUG);
return;
}
Lock::remove('worker');
Lock::release('worker');
self::$db_duration += (microtime(true) - $stamp);
}
@ -883,7 +879,7 @@ class Worker
dba::close($r);
$stamp = (float)microtime(true);
if (!Lock::set('worker_process')) {
if (!Lock::acquire('worker_process')) {
return false;
}
self::$lock_duration = (microtime(true) - $stamp);
@ -892,7 +888,7 @@ class Worker
$found = self::findWorkerProcesses($passing_slow);
self::$db_duration += (microtime(true) - $stamp);
Lock::remove('worker_process');
Lock::release('worker_process');
if ($found) {
$r = dba::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
@ -1097,13 +1093,13 @@ class Worker
}
// If there is a lock then we don't have to check for too much worker
if (!Lock::set('worker', 0)) {
if (!Lock::acquire('worker', 0)) {
return true;
}
// If there are already enough workers running, don't fork another one
$quit = self::tooMuchWorkers();
Lock::remove('worker');
Lock::release('worker');
if ($quit) {
return true;

View file

@ -4,10 +4,9 @@
*/
namespace Friendica\Database;
use dba;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Database\DBM;
use dba;
require_once 'boot.php';
require_once 'include/dba.php';
@ -413,7 +412,7 @@ class DBStructure
$field_definition = $database[$name]["fields"][$fieldname];
// Define the default collation if not given
if (!isset($parameters['Collation']) && !is_null($field_definition['Collation'])) {
if (!isset($parameters['Collation']) && !empty($field_definition['Collation'])) {
$parameters['Collation'] = 'utf8mb4_general_ci';
} else {
$parameters['Collation'] = null;
@ -537,26 +536,26 @@ class DBStructure
private static function FieldCommand($parameters, $create = true) {
$fieldstruct = $parameters["type"];
if (!is_null($parameters["Collation"])) {
if (!empty($parameters["Collation"])) {
$fieldstruct .= " COLLATE ".$parameters["Collation"];
}
if ($parameters["not null"]) {
if (!empty($parameters["not null"])) {
$fieldstruct .= " NOT NULL";
}
if (isset($parameters["default"])) {
if (!empty($parameters["default"])) {
if (strpos(strtolower($parameters["type"]),"int")!==false) {
$fieldstruct .= " DEFAULT ".$parameters["default"];
} else {
$fieldstruct .= " DEFAULT '".$parameters["default"]."'";
}
}
if ($parameters["extra"] != "") {
if (!empty($parameters["extra"])) {
$fieldstruct .= " ".$parameters["extra"];
}
if (!is_null($parameters["comment"])) {
if (!empty($parameters["comment"])) {
$fieldstruct .= " COMMENT '".dbesc($parameters["comment"])."'";
}
@ -580,7 +579,7 @@ class DBStructure
}
}
if (!is_null($structure["indexes"])) {
if (!empty($structure["indexes"])) {
foreach ($structure["indexes"] AS $indexname => $fieldnames) {
$sql_index = self::createIndex($indexname, $fieldnames, "");
if (!is_null($sql_index)) {
@ -589,11 +588,11 @@ class DBStructure
}
}
if (!is_null($structure["engine"])) {
if (!empty($structure["engine"])) {
$engine = " ENGINE=" . $structure["engine"];
}
if (!is_null($structure["comment"])) {
if (!empty($structure["comment"])) {
$comment = " COMMENT='" . dbesc($structure["comment"]) . "'";
}
@ -1299,9 +1298,11 @@ class DBStructure
"name" => ["type" => "varchar(128)", "not null" => "1", "default" => "", "comment" => ""],
"locked" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""],
"pid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "comment" => "Process ID"],
],
"expires" => ["type" => "datetime", "not null" => "1", "default" => NULL_DATE, "comment" => "datetime of cache expiration"],
],
"indexes" => [
"PRIMARY" => ["id"],
"name_expires" => ["name", "expires"]
]
];
$database["mail"] = [

View file

@ -61,7 +61,7 @@ class Conversation
unset($old_conv['source']);
}
// Update structure data all the time but the source only when its from a better protocol.
if (($old_conv['protocol'] < $conversation['protocol']) && ($old_conv['protocol'] != 0)) {
if (isset($conversation['protocol']) && isset($conversation['source']) && ($old_conv['protocol'] < $conversation['protocol']) && ($old_conv['protocol'] != 0)) {
unset($conversation['protocol']);
unset($conversation['source']);
}

View file

@ -6,26 +6,22 @@
namespace Friendica\Model;
use dba;
use Friendica\BaseObject;
use Friendica\Content\Text;
use Friendica\Core\Addon;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Lock;
use Friendica\Core\PConfig;
use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Model\Contact;
use Friendica\Model\Conversation;
use Friendica\Model\Group;
use Friendica\Model\Term;
use Friendica\Object\Image;
use Friendica\Protocol\Diaspora;
use Friendica\Protocol\OStatus;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\XML;
use Friendica\Util\Lock;
use dba;
use Text_LanguageDetect;
require_once 'boot.php';
@ -941,7 +937,9 @@ class Item extends BaseObject
// If item has attachments, drop them
foreach (explode(", ", $item['attach']) as $attach) {
preg_match("|attach/(\d+)|", $attach, $matches);
dba::delete('attach', ['id' => $matches[1], 'uid' => $item['uid']]);
if (is_array($matches) && count($matches) > 1) {
dba::delete('attach', ['id' => $matches[1], 'uid' => $item['uid']]);
}
}
// Delete tags that had been attached to other items
@ -1774,7 +1772,7 @@ class Item extends BaseObject
}
// To avoid timing problems, we are using locks.
$locked = Lock::set('item_insert_content');
$locked = Lock::acquire('item_insert_content');
if (!$locked) {
logger("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway.");
}
@ -1794,7 +1792,7 @@ class Item extends BaseObject
logger('Could not insert content for URI ' . $item['uri'] . ' - trying asynchronously');
}
if ($locked) {
Lock::remove('item_insert_content');
Lock::release('item_insert_content');
}
}
@ -2209,7 +2207,7 @@ class Item extends BaseObject
Contact::unmarkForArchival($contact);
}
$update = (!$arr['private'] && (($arr["author-link"] === $arr["owner-link"]) || ($arr["parent-uri"] === $arr["uri"])));
$update = (!$arr['private'] && ((defaults($arr, 'author-link', '') === defaults($arr, 'owner-link', '')) || ($arr["parent-uri"] === $arr["uri"])));
// Is it a forum? Then we don't care about the rules from above
if (!$update && ($arr["network"] == NETWORK_DFRN) && ($arr["parent-uri"] === $arr["uri"])) {

View file

@ -304,6 +304,33 @@ class User
return dba::update('user', $fields, ['uid' => $uid]);
}
/**
* @brief Checks if a nickname is in the list of the forbidden nicknames
*
* Check if a nickname is forbidden from registration on the node by the
* admin. Forbidden nicknames (e.g. role namess) can be configured in the
* admin panel.
*
* @param string $nickname The nickname that should be checked
* @return boolean True is the nickname is blocked on the node
*/
public static function isNicknameBlocked($nickname)
{
$forbidden_nicknames = Config::get('system', 'forbidden_nicknames', '');
// if the config variable is empty return false
if (!x($forbidden_nicknames)) {
return false;
}
// check if the nickname is in the list of blocked nicknames
$forbidden = explode(',', $forbidden_nicknames);
$forbidden = array_map('trim', $forbidden);
if (in_array(strtolower($nickname), $forbidden)) {
return true;
}
// else return false
return false;
}
/**
* @brief Catch-all user creation function
*
@ -417,6 +444,9 @@ class User
if (!valid_email($email) || !Network::isEmailDomainValid($email)) {
throw new Exception(L10n::t('Not a valid email address.'));
}
if (self::isNicknameBlocked($nickname)) {
throw new Exception(L10n::t('The nickname was blocked from registration by the nodes admin.'));
}
if (Config::get('system', 'block_extended_register', false) && dba::exists('user', ['email' => $email])) {
throw new Exception(L10n::t('Cannot use that email.'));

View file

@ -109,6 +109,7 @@ class Probe
$redirects = 0;
logger("Probing for ".$host, LOGGER_DEBUG);
$xrd = null;
$ret = Network::curl($ssl_url, false, $redirects, ['timeout' => $xrd_timeout, 'accept_content' => 'application/xrd+xml']);
if ($ret['success']) {
@ -1510,6 +1511,7 @@ class Probe
return false;
}
$feed = $ret['body'];
$dummy1 = $dummy2 = $dummy3 = null;
$feed_data = Feed::import($feed, $dummy1, $dummy2, $dummy3, true);
if (!$feed_data) {

View file

@ -4,11 +4,15 @@
*/
namespace Friendica\Protocol;
use dba;
use DOMDocument;
use DOMXPath;
use Friendica\Content\Text\BBCode;
use Friendica\Content\Text\HTML;
use Friendica\Core\Cache;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Lock;
use Friendica\Core\System;
use Friendica\Database\DBM;
use Friendica\Model\Contact;
@ -19,12 +23,8 @@ use Friendica\Model\User;
use Friendica\Network\Probe;
use Friendica\Object\Image;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Lock;
use Friendica\Util\Network;
use Friendica\Util\XML;
use dba;
use DOMDocument;
use DOMXPath;
require_once 'include/dba.php';
require_once 'include/items.php';
@ -515,9 +515,9 @@ class OStatus
logger("Item with uri ".$item["uri"]." is from a blocked contact.", LOGGER_DEBUG);
} else {
// We are having duplicated entries. Hopefully this solves it.
if (Lock::set('ostatus_process_item_insert')) {
if (Lock::acquire('ostatus_process_item_insert')) {
$ret = Item::insert($item);
Lock::remove('ostatus_process_item_insert');
Lock::release('ostatus_process_item_insert');
logger("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);
} else {
$ret = Item::insert($item);

View file

@ -1,211 +0,0 @@
<?php
/**
* @file src/Util/Lock.php
*/
namespace Friendica\Util;
/**
* @file src/Util/Lock.php
* @brief Functions for preventing parallel execution of functions
*/
use Friendica\Core\Config;
use Friendica\Database\DBM;
use Memcache;
use dba;
require_once 'include/dba.php';
/**
* @brief This class contain Functions for preventing parallel execution of functions
*/
class Lock
{
private static $semaphore = [];
/**
* @brief Check for memcache and open a connection if configured
*
* @return object|boolean The memcache object - or "false" if not successful
*/
private static function connectMemcache()
{
if (!function_exists('memcache_connect')) {
return false;
}
if (!Config::get('system', 'memcache')) {
return false;
}
$memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
$memcache_port = Config::get('system', 'memcache_port', 11211);
$memcache = new Memcache;
if (!$memcache->connect($memcache_host, $memcache_port)) {
return false;
}
return $memcache;
}
/**
* @brief Creates a semaphore key
*
* @param string $fn_name Name of the lock
*
* @return ressource the semaphore key
*/
private static function semaphoreKey($fn_name)
{
$temp = get_temppath();
$file = $temp.'/'.$fn_name.'.sem';
if (!file_exists($file)) {
file_put_contents($file, $fn_name);
}
return ftok($file, 'f');
}
/**
* @brief Sets a lock for a given name
*
* @param string $fn_name Name of the lock
* @param integer $timeout Seconds until we give up
*
* @return boolean Was the lock successful?
*/
public static function set($fn_name, $timeout = 120)
{
$got_lock = false;
$start = time();
// The second parameter for "sem_acquire" doesn't exist before 5.6.1
if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
self::$semaphore[$fn_name] = sem_get(self::semaphoreKey($fn_name));
if (self::$semaphore[$fn_name]) {
return sem_acquire(self::$semaphore[$fn_name], ($timeout == 0));
}
}
$memcache = self::connectMemcache();
if (is_object($memcache)) {
$cachekey = get_app()->get_hostname().";lock:".$fn_name;
do {
// We only lock to be sure that nothing happens at exactly the same time
dba::lock('locks');
$lock = $memcache->get($cachekey);
if (!is_bool($lock)) {
$pid = (int)$lock;
// When the process id isn't used anymore, we can safely claim the lock for us.
// Or we do want to lock something that was already locked by us.
if (!posix_kill($pid, 0) || ($pid == getmypid())) {
$lock = false;
}
}
if (is_bool($lock)) {
$memcache->set($cachekey, getmypid(), MEMCACHE_COMPRESSED, 300);
$got_lock = true;
}
dba::unlock();
if (!$got_lock && ($timeout > 0)) {
usleep(rand(10000, 200000));
}
} while (!$got_lock && ((time() - $start) < $timeout));
return $got_lock;
}
do {
dba::lock('locks');
$lock = dba::selectFirst('locks', ['locked', 'pid'], ['name' => $fn_name]);
if (DBM::is_result($lock)) {
if ($lock['locked']) {
// When the process id isn't used anymore, we can safely claim the lock for us.
if (!posix_kill($lock['pid'], 0)) {
$lock['locked'] = false;
}
// We want to lock something that was already locked by us? So we got the lock.
if ($lock['pid'] == getmypid()) {
$got_lock = true;
}
}
if (!$lock['locked']) {
dba::update('locks', ['locked' => true, 'pid' => getmypid()], ['name' => $fn_name]);
$got_lock = true;
}
} elseif (!DBM::is_result($lock)) {
dba::insert('locks', ['name' => $fn_name, 'locked' => true, 'pid' => getmypid()]);
$got_lock = true;
}
dba::unlock();
if (!$got_lock && ($timeout > 0)) {
usleep(rand(100000, 2000000));
}
} while (!$got_lock && ((time() - $start) < $timeout));
return $got_lock;
}
/**
* @brief Removes a lock if it was set by us
*
* @param string $fn_name Name of the lock
* @return mixed
*/
public static function remove($fn_name)
{
if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
if (empty(self::$semaphore[$fn_name])) {
return false;
} else {
$success = @sem_release(self::$semaphore[$fn_name]);
unset(self::$semaphore[$fn_name]);
return $success;
}
}
$memcache = self::connectMemcache();
if (is_object($memcache)) {
$cachekey = get_app()->get_hostname().";lock:".$fn_name;
$lock = $memcache->get($cachekey);
if (!is_bool($lock)) {
if ((int)$lock == getmypid()) {
$memcache->delete($cachekey);
}
}
return;
}
dba::update('locks', ['locked' => false, 'pid' => 0], ['name' => $fn_name, 'pid' => getmypid()]);
return;
}
/**
* @brief Removes all lock that were set by us
* @return void
*/
public static function removeAll()
{
$memcache = self::connectMemcache();
if (is_object($memcache)) {
// We cannot delete all cache entries, but this doesn't matter with memcache
return;
}
dba::update('locks', ['locked' => false, 'pid' => 0], ['pid' => getmypid()]);
return;
}
}