]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #7636 from annando/manage
authorHypolite Petovan <hypolite@mrpetovan.com>
Mon, 30 Sep 2019 15:12:46 +0000 (11:12 -0400)
committerGitHub <noreply@github.com>
Mon, 30 Sep 2019 15:12:46 +0000 (11:12 -0400)
Restructured "manage" functionality, added count parameters

64 files changed:
.codecov.yml
.drone.yml [new file with mode: 0644]
.travis.yml
autotest.sh [new file with mode: 0755]
bin/wait-for-connection [new file with mode: 0755]
boot.php
include/api.php
include/conversation.php
include/items.php
mod/api.php
mod/cal.php
mod/community.php
mod/dfrn_poll.php
mod/dfrn_request.php
mod/display.php
mod/hcard.php
mod/item.php
mod/photos.php
mod/redir.php
mod/search.php
mod/subthread.php
mod/tagger.php
mod/videos.php
mod/wall_attach.php
mod/wall_upload.php
phpunit.xml [deleted file]
src/Content/Nav.php
src/Content/Widget.php
src/Core/ACL.php
src/Core/Cache/MemcacheCache.php
src/Core/Cache/MemcachedCache.php
src/Core/Cache/RedisCache.php
src/Core/Cache/TraitMemcacheCommand.php [new file with mode: 0644]
src/Core/Session.php
src/Database/DBStructure.php
src/Database/Database.php
src/Model/Contact.php
src/Model/Item.php
src/Model/PermissionSet.php
src/Model/Photo.php
src/Model/Profile.php
src/Module/Debug/Localtime.php
src/Module/Directory.php
src/Module/Like.php
src/Module/Logout.php
src/Module/Magic.php
src/Module/Profile.php
src/Module/Profile/Contacts.php
src/Object/Post.php
src/Protocol/DFRN.php
src/Protocol/Diaspora.php
src/Util/Security.php
tests/Util/Database/StaticDatabase.php
tests/Util/VFSTrait.php
tests/phpunit.xml [new file with mode: 0644]
tests/src/Core/Cache/APCuCacheTest.php
tests/src/Core/Cache/MemcacheCacheTest.php
tests/src/Core/Cache/MemcachedCacheTest.php
tests/src/Core/Cache/RedisCacheTest.php
tests/src/Core/Lock/APCuCacheLockTest.php
tests/src/Core/Lock/MemcacheCacheLockTest.php
tests/src/Core/Lock/MemcachedCacheLockTest.php
tests/src/Core/Lock/RedisCacheLockTest.php
tests/src/Util/Logger/StreamLoggerTest.php

index 35509a879e44cf6727aac171a52f2ceffb44960b..0c54747c9534fc66815cdf8c6578b38d8f99a8c8 100644 (file)
@@ -1,10 +1,14 @@
+codecov:
+  branch: develop
+  ci:
+    - drone.friendi.ca
 
 coverage:
+  precision: 2
+  round: down
+  range: "70...100"
   status:
-    project:
-      default:
-        target: auto
-        threshold: null
-        base: auto
+    project: off
+    patch: off
 
 comment: off
diff --git a/.drone.yml b/.drone.yml
new file mode 100644 (file)
index 0000000..65211da
--- /dev/null
@@ -0,0 +1,439 @@
+kind: pipeline
+name: mysql8.0-php7.1
+
+steps:
+- name: mysql8.0-php7.1
+  image: friendicaci/php7.1:php7.1.32
+  commands:
+      - NOCOVERAGE=true ./autotest.sh mysql
+  environment:
+    MYSQL_USERNAME: friendica
+    MYSQL_PASSWORD: friendica
+    MYSQL_DATABASE: friendica
+    MYSQL_HOST: mysql
+
+services:
+- name: mysql
+  image: mysql:8.0
+  command: [ "--default-authentication-plugin=mysql_native_password" ]
+  environment:
+    MYSQL_ROOT_PASSWORD: friendica
+    MYSQL_USER: friendica
+    MYSQL_PASSWORD: friendica
+    MYSQL_DATABASE: friendica
+  volumes: 
+    - name: cache
+      path: /var/lib/mysql
+
+volumes:
+- name: cache
+  temp: {}
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: mysql8.0-php7.2
+
+steps:
+- name: mysql8.0-php7.2
+  image: friendicaci/php7.2:php7.2.22
+  commands:
+    - NOCOVERAGE=true ./autotest.sh mysql
+  environment:
+    MYSQL_USERNAME: friendica
+    MYSQL_PASSWORD: friendica
+    MYSQL_DATABASE: friendica
+    MYSQL_HOST: mysql
+
+services:
+- name: mysql
+  image: mysql:8.0
+  command: [ "--default-authentication-plugin=mysql_native_password" ]
+  environment:
+    MYSQL_ROOT_PASSWORD: friendica
+    MYSQL_USER: friendica
+    MYSQL_PASSWORD: friendica
+    MYSQL_DATABASE: friendica
+  volumes:
+    - name: cache
+      path: /var/lib/mysql
+
+volumes:
+  - name: cache
+    temp: {}
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: mysql8.0-php7.3
+
+steps:
+- name: mysql8.0-php7.3
+  image: friendicaci/php7.3:php7.3.9
+  commands:
+      - NOCOVERAGE=true ./autotest.sh mysql
+  environment:
+      MYSQL_USERNAME: friendica
+      MYSQL_PASSWORD: friendica
+      MYSQL_DATABASE: friendica
+      MYSQL_HOST: mysql
+
+services:
+- name: mysql
+  image: mysql:8.0
+  command: [ "--default-authentication-plugin=mysql_native_password" ]
+  environment:
+      MYSQL_ROOT_PASSWORD: friendica
+      MYSQL_USER: friendica
+      MYSQL_PASSWORD: friendica
+      MYSQL_DATABASE: friendica
+  volumes:
+    - name: cache
+      path: /var/lib/mysql
+
+volumes:
+  - name: cache
+    temp: {}
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: mariadb10.1-php7.1
+
+steps:
+- name: mariadb10.1-php7.1
+  image: friendicaci/php7.1:php7.1.32
+  commands:
+      - phpenmod xdebug
+      - sleep 20
+      - ./autotest.sh mariadb
+      - wget https://codecov.io/bash -O codecov.sh
+      - sh -c "if [ '$DRONE_BUILD_EVENT' = 'pull_request' ]; then bash codecov.sh -B $DRONE_BRANCH -C $DRONE_COMMIT -P $DRONE_PULL_REQUEST -t 5ce7d64e-07b4-4adf-8700-e2eae27e14ec -f tests/autotest-clover.xml; fi"
+      - sh -c "if [ '$DRONE_BUILD_EVENT' != 'pull_request' ]; then bash codecov.sh -B $DRONE_BRANCH -C $DRONE_COMMIT -t 5ce7d64e-07b4-4adf-8700-e2eae27e14ec -f tests/autotest-clover.xml; fi"
+  environment:
+      MYSQL_USER: friendica
+      MYSQL_PASSWORD: friendica
+      MYSQL_DATABASE: friendica
+      MYSQL_HOST: mariadb
+
+services:
+- name: mariadb
+  image: mariadb:10.1
+  environment:
+      MYSQL_ROOT_PASSWORD: friendica
+      MYSQL_USER: friendica
+      MYSQL_PASSWORD: friendica
+      MYSQL_DATABASE: friendica
+  volumes:
+    - name: cache
+      path: /var/lib/mysql
+
+volumes:
+  - name: cache
+    temp: {}
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: mariadb10.1-php7.2
+
+steps:
+- name: mariadb10.1-php7.2
+  image: friendicaci/php7.2:php7.2.22
+  commands:
+      - NOCOVERAGE=true ./autotest.sh mariadb
+  environment:
+      MYSQL_USER: friendica
+      MYSQL_PASSWORD: friendica
+      MYSQL_DATABASE: friendica
+      MYSQL_HOST: mariadb
+
+services:
+- name: mariadb
+  image: mariadb:10.1
+  environment:
+      MYSQL_ROOT_PASSWORD: friendica
+      MYSQL_USER: friendica
+      MYSQL_PASSWORD: friendica
+      MYSQL_DATABASE: friendica
+  volumes:
+    - name: cache
+      path: /var/lib/mysql
+
+volumes:
+  - name: cache
+    temp: {}
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: mariadb10.1-php7.3
+
+steps:
+- name: mariadb10.1-php7.3
+  image: friendicaci/php7.3:php7.3.9
+  commands:
+      - NOCOVERAGE=true ./autotest.sh mariadb
+  environment:
+      MYSQL_USER: friendica
+      MYSQL_PASSWORD: friendica
+      MYSQL_DATABASE: friendica
+      MYSQL_HOST: mariadb
+
+services:
+- name: mariadb
+  image: mariadb:10.1
+  environment:
+      MYSQL_ROOT_PASSWORD: friendica
+      MYSQL_USER: friendica
+      MYSQL_PASSWORD: friendica
+      MYSQL_DATABASE: friendica
+  volumes:
+    - name: cache
+      path: /var/lib/mysql
+
+volumes:
+  - name: cache
+    temp: {}
+
+---
+kind: pipeline
+name: redis-php7.1
+
+steps:
+- name: redis-php7.1
+  image: friendicaci/php7.1:php7.1.32
+  commands:
+    - phpenmod xdebug
+    - sleep 20
+    - NOINSTALL=true TEST_SELECTION=REDIS ./autotest.sh mysql
+    - wget https://codecov.io/bash -O codecov.sh
+    - sh -c "if [ '$DRONE_BUILD_EVENT' = 'pull_request' ]; then bash codecov.sh -B $DRONE_BRANCH -C $DRONE_COMMIT -P $DRONE_PULL_REQUEST -t 5ce7d64e-07b4-4adf-8700-e2eae27e14ec -f tests/autotest-clover.xml; fi"
+    - sh -c "if [ '$DRONE_BUILD_EVENT' != 'pull_request' ]; then bash codecov.sh -B $DRONE_BRANCH -C $DRONE_COMMIT -t 5ce7d64e-07b4-4adf-8700-e2eae27e14ec -f tests/autotest-clover.xml; fi"
+  environment:
+      REDIS_HOST: redis
+
+services:
+- name: redis
+  image: redis
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: redis-php7.2
+
+steps:
+- name: redis-php7.2
+  image: friendicaci/php7.2:php7.2.22
+  commands:
+      - NOCOVERAGE=true NOINSTALL=true TEST_SELECTION=REDIS ./autotest.sh mysql
+  environment:
+    REDIS_HOST: redis
+
+services:
+- name: redis
+  image: redis
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: redis-php7.3
+
+steps:
+- name: redis-php7.3
+  image: friendicaci/php7.3:php7.3.9
+  commands:
+      - NOCOVERAGE=true NOINSTALL=true TEST_SELECTION=REDIS ./autotest.sh mysql
+  environment:
+    REDIS_HOST: redis
+
+services:
+- name: redis
+  image: redis
+
+---
+kind: pipeline
+name: memcache-php7.1
+
+steps:
+- name: memcache-php7.1
+  image: friendicaci/php7.1:php7.1.32
+  commands:
+      - phpenmod xdebug
+      - sleep 20
+      - NOINSTALL=true TEST_SELECTION=MEMCACHE ./autotest.sh mysql
+      - wget https://codecov.io/bash -O codecov.sh
+      - sh -c "if [ '$DRONE_BUILD_EVENT' = 'pull_request' ]; then bash codecov.sh -B $DRONE_BRANCH -C $DRONE_COMMIT -P $DRONE_PULL_REQUEST -t 5ce7d64e-07b4-4adf-8700-e2eae27e14ec -f tests/autotest-clover.xml; fi"
+      - sh -c "if [ '$DRONE_BUILD_EVENT' != 'pull_request' ]; then bash codecov.sh -B $DRONE_BRANCH -C $DRONE_COMMIT -t 5ce7d64e-07b4-4adf-8700-e2eae27e14ec -f tests/autotest-clover.xml; fi"
+  environment:
+      MEMCACHE_HOST: memcached
+
+services:
+- name: memcached
+  image: memcached
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: memcache-php7.2
+
+steps:
+- name: memcache-php7.2
+  image: friendicaci/php7.2:php7.2.22
+  commands:
+      - NOCOVERAGE=true NOINSTALL=true TEST_SELECTION=MEMCACHE ./autotest.sh mysql
+  environment:
+      MEMCACHE_HOST: memcached
+
+services:
+- name: memcached
+  image: memcached
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: memcache-php7.3
+
+steps:
+- name: memcache-php7.3
+  image: friendicaci/php7.3:php7.3.9
+  commands:
+      - NOCOVERAGE=true NOINSTALL=true TEST_SELECTION=MEMCACHE ./autotest.sh mysql
+  environment:
+      MEMCACHE_HOST: memcached
+
+services:
+- name: memcached
+  image: memcached
+
+---
+kind: pipeline
+name: memcached-php7.1
+
+steps:
+- name: memcached-php7.1
+  image: friendicaci/php7.1:php7.1.32
+  commands:
+      - phpenmod xdebug
+      - sleep 20
+      - NOINSTALL=true TEST_SELECTION=MEMCACHED ./autotest.sh mysql
+      - wget https://codecov.io/bash -O codecov.sh
+      - sh -c "if [ '$DRONE_BUILD_EVENT' = 'pull_request' ]; then bash codecov.sh -B $DRONE_BRANCH -C $DRONE_COMMIT -P $DRONE_PULL_REQUEST -t 5ce7d64e-07b4-4adf-8700-e2eae27e14ec -f tests/autotest-clover.xml; fi"
+      - sh -c "if [ '$DRONE_BUILD_EVENT' != 'pull_request' ]; then bash codecov.sh -B $DRONE_BRANCH -C $DRONE_COMMIT -t 5ce7d64e-07b4-4adf-8700-e2eae27e14ec -f tests/autotest-clover.xml; fi"
+  environment:
+      MEMCACHED_HOST: memcached
+
+services:
+- name: memcached
+  image: memcached
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: memcached-php7.2
+
+steps:
+- name: memcached-php7.2
+  image: friendicaci/php7.2:php7.2.22
+  commands:
+      - NOCOVERAGE=true NOINSTALL=true TEST_SELECTION=MEMCACHED ./autotest.sh mysql
+  environment:
+      MEMCACHED_HOST: memcached
+
+services:
+- name: memcached
+  image: memcached
+
+trigger:
+  branch:
+    - master
+    - develop
+    - "*-rc"
+  event:
+    - pull_request
+    - push
+---
+kind: pipeline
+name: memcached-php7.3
+
+steps:
+- name: memcached-php7.3
+  image: friendicaci/php7.3:php7.3.9
+  commands:
+      - NOCOVERAGE=true NOINSTALL=true TEST_SELECTION=MEMCACHED ./autotest.sh mysql
+  environment:
+      MEMCACHED_HOST: memcached
+
+services:
+- name: memcached
+  image: memcached
index e2aa84f5c81f91438475c579feb4064a770b2a0a..376748bcb9bc0b62f847d9f90de7bddd7d09f3b5 100644 (file)
@@ -26,4 +26,4 @@ before_script:
  - phpenv config-add .travis/redis.ini
  - phpenv config-add .travis/memcached.ini
 
-after_success: bash <(curl -s https://codecov.io/bash)
+script: vendor/bin/phpunit --configuration tests/phpunit.xml
diff --git a/autotest.sh b/autotest.sh
new file mode 100755 (executable)
index 0000000..15067bf
--- /dev/null
@@ -0,0 +1,279 @@
+#!/usr/bin/env bash
+#
+# This script is used for autotesting the Friendica codebase with different
+# types of tests and environments.
+#
+# Currently, there are three types of autotesting possibilities:
+# - "USEDOCKER=true ./autotest.sh" will start a database docker container for testing
+# - "./autotest.sh" on the Drone CI environment will use the database container of the drone CI pipeline
+# - "./autotest.sh" on a local environment will try to use the local database instance for testing
+#
+# You can specify a database (mysql, mariadb currently) for the db backend of Friendica ("./autotest.sh mysql")
+# And you can specify some parameters for the test, like:
+# - NOCOVERAGE=true ... Don't create a coverage XML (this is only useful if you will send coverage to codecov.io)
+# - NOINSTALL=true  ... Skip the whole Friendica installation process (e.g. you just test Caching drivers)
+# - TEST_SELECTION= ... Specify which tests are used to run (based on the test-labeling)
+# - XDEBUG_CONFIG=  ... Set some XDEBUG specific environment settings for development
+
+DATABASENAME=${MYSQL_DATABASE:-test}
+DATABASEUSER=${MYSQL_USERNAME:-friendica}
+DATABASEHOST=${MYSQL_HOST:-localhost}
+BASEDIR=$PWD
+
+DBCONFIGS="mysql mariadb"
+TESTS="REDIS MEMCACHE MEMCACHED APCU NODB"
+
+export MYSQL_DATABASE="$DATABASENAME"
+export MYSQL_USERNAME="$DATABASEUSER"
+export MYSQL_PASSWORD="friendica"
+
+if [ -z "$PHP_EXE" ]; then
+  PHP_EXE=php
+fi
+PHP=$(which "$PHP_EXE")
+# Use the Friendica internal composer
+COMPOSER="$BASEDIR/bin/composer.phar"
+
+set -e
+
+_XDEBUG_CONFIG=$XDEBUG_CONFIG
+unset XDEBUG_CONFIG
+
+function show_syntax() {
+  echo -e "Syntax: ./autotest.sh [dbconfigname] [testfile]\n" >&2
+  echo -e "\t\"dbconfigname\" can be one of: $DBCONFIGS" >&2
+  echo -e "\t\"testfile\" is the name of a test file, for example lib/template.php" >&2
+  echo -e "\nDatabase environment variables:\n" >&2
+  echo -e "\t\"MYSQL_HOST\" Mysql Hostname (Default: localhost)" >&2
+  echo -e "\t\"MYSQL_USDRNAME\" Mysql Username (Default: friendica)" >&2
+  echo -e "\t\"MYSQL_DATABASE\" Mysql Database (Default: test)" >&2
+  echo -e "\nOther environment variables:\n" >&2
+  echo -e "\t\"TEST_SELECTION\" test a specific group of tests, can be one of: $TESTS" >&2
+  echo -e "\t\"NOINSTALL\" If set to true, skip the db and install process" >&2
+  echo -e "\t\"NOCOVERAGE\" If set to true, don't create a coverage output" >&2
+  echo -e "\t\"USEDOCKER\" If set to true, the DB server will be executed inside a docker container" >&2
+  echo -e "\nExample: NOCOVERAGE=true ./autotest.sh mysql src/Core/Cache/MemcacheTest.php" >&2
+  echo "will run the test suite from \"tests/src/Core/Cache/MemcacheTest.php\" without a Coverage" >&2
+  echo -e "\nIf no arguments are specified, all tests will be run with all database configs" >&2
+}
+
+if [ -x "$PHP" ]; then
+  echo "Using PHP executable $PHP"
+else
+  echo "Could not find PHP executable $PHP_EXE" >&2
+  exit 3
+fi
+
+echo "Installing depdendencies"
+$PHP "$COMPOSER" install
+
+PHPUNIT="$BASEDIR/vendor/bin/phpunit"
+
+if [ -x "$PHPUNIT" ]; then
+  echo "Using PHPUnit executable $PHPUNIT"
+else
+  echo "Could not find PHPUnit executable after composer $PHPUNIT" >&2
+  exit 3
+fi
+
+if ! [ \( -w config -a ! -f config/local.config.php \) -o \( -f config/local.config.php -a -w config/local.config.php \) ]; then
+  echo "Please enable write permissions on config and config/config.php" >&2
+  exit 1
+fi
+
+if [ "$1" ]; then
+  FOUND=0
+  for DBCONFIG in $DBCONFIGS; do
+    if [ "$1" = "$DBCONFIG" ]; then
+      FOUND=1
+      break
+    fi
+  done
+  if [ $FOUND = 0 ]; then
+    echo -e "Unknown database config name \"$1\"\n" >&2
+    show_syntax
+    exit 2
+  fi
+fi
+
+# Back up existing (dev) config if one exists and backup not already there
+if [ -f config/local.config.php ] && [ ! -f config/local.config-autotest-backup.php ]; then
+  mv config/local.config.php config/local.config-autotest-backup.php
+fi
+
+function cleanup_config() {
+
+  if [ -n "$DOCKER_CONTAINER_ID" ]; then
+    echo "Kill the docker $DOCKER_CONTAINER_ID"
+    docker stop "$DOCKER_CONTAINER_ID"
+    docker rm -f "$DOCKER_CONTAINER_ID"
+  fi
+
+  cd "$BASEDIR"
+
+  # Restore existing config
+  if [ -f config/local.config-autotest-backup.php ]; then
+    mv config/local.config-autotest-backup.php config/local.config.php
+  fi
+}
+
+# restore config on exit
+trap cleanup_config EXIT
+
+function execute_tests() {
+  DB=$1
+  echo "Setup environment for $DB testing ..."
+  # back to root folder
+  cd "$BASEDIR"
+
+  # backup current config
+  if [ -f config/local.config.php ]; then
+    mv config/local.config.php config/local.config-autotest-backup.php
+  fi
+
+  if [ -z "$NOINSTALL" ]; then
+    #drop database
+    if [ "$DB" == "mysql" ]; then
+      if [ -n "$USEDOCKER" ]; then
+        echo "Fire up the mysql docker"
+        DOCKER_CONTAINER_ID=$(docker run \
+          -e MYSQL_ROOT_PASSWORD=friendica \
+          -e MYSQL_USER="$DATABASEUSER" \
+          -e MYSQL_PASSWORD=friendica \
+          -e MYSQL_DATABASE="$DATABASENAME" \
+          -d mysql)
+        DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
+
+      else
+        if [ -z "$DRONE" ]; then # no need to drop the DB when we are on CI
+          if [ "mysql" != "$(mysql --version | grep -o mysql)" ]; then
+            echo "Your mysql binary is not provided by mysql"
+            echo "To use the docker container set the USEDOCKER environment variable"
+            exit 3
+          fi
+          mysql -u "$DATABASEUSER" -pfriendica -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
+          mysql -u "$DATABASEUSER" -pfriendica -e "CREATE DATABASE $DATABASENAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h $DATABASEHOST
+        else
+          DATABASEHOST=mysql
+        fi
+      fi
+
+      echo "Waiting for MySQL $DATABASEHOST initialization..."
+      if ! bin/wait-for-connection $DATABASEHOST 3306 300; then
+        echo "[ERROR] Waited 300 seconds, no response" >&2
+        exit 1
+      fi
+
+      echo "MySQL is up."
+    fi
+    if [ "$DB" == "mariadb" ]; then
+      if [ -n "$USEDOCKER" ]; then
+        echo "Fire up the mariadb docker"
+        DOCKER_CONTAINER_ID=$(docker run \
+          -e MYSQL_ROOT_PASSWORD=friendica \
+          -e MYSQL_USER="$DATABASEUSER" \
+          -e MYSQL_PASSWORD=friendica \
+          -e MYSQL_DATABASE="$DATABASENAME" \
+          -d mariadb)
+        DATABASEHOST=$(docker inspect --format="{{.NetworkSettings.IPAddress}}" "$DOCKER_CONTAINER_ID")
+
+      else
+        if [ -z "$DRONE" ]; then # no need to drop the DB when we are on CI
+          if [ "MariaDB" != "$(mysql --version | grep -o MariaDB)" ]; then
+            echo "Your mysql binary is not provided by mysql"
+            echo "To use the docker container set the USEDOCKER environment variable"
+            exit 3
+          fi
+          mysql -u "$DATABASEUSER" -pfriendica -e "DROP DATABASE IF EXISTS $DATABASENAME" -h $DATABASEHOST || true
+          mysql -u "$DATABASEUSER" -pfriendica -e "CREATE DATABASE $DATABASENAME DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci" -h $DATABASEHOST
+        else
+          DATABASEHOST=mariadb
+        fi
+      fi
+
+      echo "Waiting for MariaDB $DATABASEHOST initialization..."
+      if ! bin/wait-for-connection $DATABASEHOST 3306 300; then
+        echo "[ERROR] Waited 300 seconds, no response" >&2
+        exit 1
+      fi
+
+      echo "MariaDB is up."
+    fi
+
+    if [ -n "$USEDOCKER" ]; then
+      echo "Initialize database..."
+      docker exec $DOCKER_CONTAINER_ID mysql -u root -pfriendica -e 'CREATE DATABASE IF NOT EXISTS $DATABASENAME;'
+    fi
+
+    export MYSQL_HOST="$DATABASEHOST"
+
+    #call installer
+    echo "Installing Friendica..."
+    "$PHP" ./bin/console.php autoinstall --dbuser="$DATABASEUSER" --dbpass=friendica --dbdata="$DATABASENAME" --dbhost="$DATABASEHOST" --url=https://friendica.local --admin=admin@friendica.local
+  fi
+
+  #test execution
+  echo "Testing..."
+  rm -fr "coverage-html"
+  mkdir "coverage-html"
+  if [[ "$_XDEBUG_CONFIG" ]]; then
+    export XDEBUG_CONFIG=$_XDEBUG_CONFIG
+  fi
+
+  COVER=''
+  if [ -z "$NOCOVERAGE" ]; then
+    COVER="--coverage-clover tests/autotest-clover.xml"
+  else
+    echo "No coverage"
+  fi
+
+  # per default, there is no cache installed
+  GROUP='--exclude-group REDIS,MEMCACHE,MEMCACHED,APCU'
+  if [ "$TEST_SELECTION" == "REDIS" ]; then
+    GROUP="--group REDIS"
+  fi
+  if [ "$TEST_SELECTION" == "MEMCACHE" ]; then
+    GROUP="--group MEMCACHE"
+  fi
+  if [ "$TEST_SELECTION" == "MEMCACHED" ]; then
+    GROUP="--group MEMCACHED"
+  fi
+  if [ "$TEST_SELECTION" == "APCU" ]; then
+    GROUP="--group APCU"
+  fi
+  if [ "$TEST_SELECTION" == "NODB" ]; then
+    GROUP="--exclude-group DB,SLOWDB"
+  fi
+
+  INPUT="$BASEDIR/tests"
+  if [ -n "$2" ]; then
+    INPUT="$INPUT/$2"
+  fi
+
+  echo "${PHPUNIT[@]}" --configuration tests/phpunit.xml $GROUP $COVER --log-junit "autotest-results.xml" "$INPUT" "$3"
+  "${PHPUNIT[@]}" --configuration tests/phpunit.xml $GROUP $COVER --log-junit "autotest-results.xml" "$INPUT" "$3"
+  RESULT=$?
+
+  if [ -n "$DOCKER_CONTAINER_ID" ]; then
+    echo "Kill the docker $DOCKER_CONTAINER_ID"
+    docker stop $DOCKER_CONTAINER_ID
+    docker rm -f $DOCKER_CONTAINER_ID
+    unset $DOCKER_CONTAINER_ID
+  fi
+}
+
+#
+# Start the test execution
+#
+if [ -z "$1" ] && [ -n "$TEST_SELECTION" ]; then
+  # run all known database configs
+  for DBCONFIG in $DBCONFIGS; do
+    execute_tests "$DBCONFIG"
+  done
+else
+  FILENAME="$2"
+  if [ -n "$2" ] && [ ! -f "tests/$FILENAME" ] && [ "${FILENAME:0:2}" != "--" ]; then
+    FILENAME="../$FILENAME"
+  fi
+  execute_tests "$1" "$FILENAME" "$3"
+fi
diff --git a/bin/wait-for-connection b/bin/wait-for-connection
new file mode 100755 (executable)
index 0000000..eeb9ba9
--- /dev/null
@@ -0,0 +1,45 @@
+#!/usr/bin/php
+#
+# This script tries to connect to a database for a given interval
+# Useful in case of installation e.g. to wait for the database to not generate unnecessary errors
+#
+# Usage: php bin/wait-for-connection {HOST} {PORT} [{TIMEOUT}]
+
+<?php
+$timeout = 60;
+switch ($argc) {
+       case 4:
+               $timeout = (float)$argv[3];
+       case 3:
+               $host = $argv[1];
+               $port = (int)$argv[2];
+               break;
+       default:
+               fwrite(STDERR, 'Usage: '.$argv[0].' host port [timeout]'."\n");
+               exit(2);
+}
+if ($timeout < 0) {
+       fwrite(STDERR, 'Timeout must be greater than zero'."\n");
+       exit(2);
+}
+if ($port < 1) {
+       fwrite(STDERR, 'Port must be an integer greater than zero'."\n");
+       exit(2);
+}
+$socketTimeout = (float)ini_get('default_socket_timeout');
+if ($socketTimeout > $timeout) {
+       $socketTimeout = $timeout;
+}
+$stopTime = time() + $timeout;
+do {
+       $sock = @fsockopen($host, $port, $errno, $errstr, $socketTimeout);
+       if ($sock !== false) {
+               fclose($sock);
+               fwrite(STDOUT, "\n");
+               exit(0);
+       }
+       sleep(1);
+       fwrite(STDOUT, '.');
+} while (time() < $stopTime);
+fwrite(STDOUT, "\n");
+exit(1);
index ff1ae7fb3eadfab9c79000bd435abf46a95a1639..9d7ee2b0923beeac4d95d97a79d20700887196ca 100644 (file)
--- a/boot.php
+++ b/boot.php
@@ -23,6 +23,7 @@ use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Term;
@@ -413,28 +414,13 @@ function public_contact()
  *
  * @return int|bool visitor_id or false
  */
-function remote_user($uid = null)
+function remote_user()
 {
-       // You cannot be both local and remote.
-       // Unncommented by rabuzarus because remote authentication to local
-       // profiles wasn't possible anymore (2018-04-12).
-//     if (local_user()) {
-//             return false;
-//     }
-
        if (empty($_SESSION['authenticated'])) {
                return false;
        }
 
-       if (!is_null($uid) && !empty($_SESSION['remote'])) {
-               /// @todo replace it with this:
-               // if (!empty($_SESSION['remote'][$uid])) ...
-               foreach ($_SESSION['remote'] as $visitor) {
-                       if ($visitor['uid'] == $uid) {
-                               return $visitor['cid'];
-                       }
-               }
-       } elseif (is_null($uid) && !empty($_SESSION['visitor_id'])) {
+       if (!empty($_SESSION['visitor_id'])) {
                return intval($_SESSION['visitor_id']);
        }
 
index bdab20b75a2231c477676d78ff93db7343914d69..8b938508bd64283943cbaeada6ff0fd15e40bab7 100644 (file)
@@ -48,9 +48,9 @@ use Friendica\Util\Proxy as ProxyUtils;
 use Friendica\Util\Strings;
 use Friendica\Util\XML;
 
-require_once 'mod/share.php';
-require_once 'mod/item.php';
-require_once 'mod/wall_upload.php';
+require_once __DIR__ . '/../mod/share.php';
+require_once __DIR__ . '/../mod/item.php';
+require_once __DIR__ . '/../mod/wall_upload.php';
 
 define('API_METHOD_ANY', '*');
 define('API_METHOD_GET', 'GET');
index 66b6d2a11cc48cb6de5862c534f34406b706c8ec..74c8a6d27243ef9a6ac09361707783a2d26f3abd 100644 (file)
@@ -365,7 +365,7 @@ function localize_item(&$item)
                'network' => $item['author-network'], 'url' => $item['author-link']];
 
        // Only create a redirection to a magic link when logged in
-       if (!empty($item['plink']) && (local_user() || remote_user())) {
+       if (!empty($item['plink']) && Session::isAuthenticated()) {
                $item['plink'] = Contact::magicLinkByContact($author, $item['plink']);
        }
 }
index 25c857f1156a615a0efbbf914b0f815a37fb7fef..4cc10e628cd50ec95f56884f2c4b6e58f856e50e 100644 (file)
@@ -13,6 +13,7 @@ use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Model\Item;
 use Friendica\Protocol\DFRN;
@@ -326,7 +327,7 @@ function drop_items(array $items)
 {
        $uid = 0;
 
-       if (!local_user() && !remote_user()) {
+       if (!Session::isAuthenticated()) {
                return;
        }
 
@@ -362,14 +363,8 @@ function drop_item($id, $return = '')
        $contact_id = 0;
 
        // check if logged in user is either the author or owner of this item
-
-       if (!empty($_SESSION['remote'])) {
-               foreach ($_SESSION['remote'] as $visitor) {
-                       if ($visitor['uid'] == $item['uid'] && $visitor['cid'] == $item['contact-id']) {
-                               $contact_id = $visitor['cid'];
-                               break;
-                       }
-               }
+       if (Session::getRemoteContactID($item['uid']) == $item['contact-id']) {
+               $contact_id = $item['contact-id'];
        }
 
        if ((local_user() == $item['uid']) || $contact_id) {
index 4a1db1be5511c7a0f7f6d19daeae38065cc14d6d..9a802b515a6a1a0f1cd7a504336f38c7e3e5e532 100644 (file)
@@ -2,6 +2,7 @@
 /**
  * @file mod/api.php
  */
+
 use Friendica\App;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
@@ -9,7 +10,7 @@ use Friendica\Core\Renderer;
 use Friendica\Database\DBA;
 use Friendica\Module\Login;
 
-require_once 'include/api.php';
+require_once __DIR__ . '/../include/api.php';
 
 function oauth_get_client(OAuthRequest $request)
 {
index 05ad314b039a351dc2a790cbac33a34882542364..eba8d975865b2728a114b8981be0035375d8216b 100644 (file)
@@ -14,6 +14,7 @@ use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Event;
@@ -26,11 +27,7 @@ use Friendica\Util\Temporal;
 
 function cal_init(App $a)
 {
-       if ($a->argc > 1) {
-               DFRN::autoRedir($a, $a->argv[1]);
-       }
-
-       if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
+       if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                throw new \Friendica\Network\HTTPException\ForbiddenException(L10n::t('Access denied.'));
        }
 
@@ -113,18 +110,11 @@ function cal_content(App $a)
        $owner_uid = intval($a->data['user']['uid']);
        $nick = $a->data['user']['nickname'];
 
-       if (!empty($_SESSION['remote']) && is_array($_SESSION['remote'])) {
-               foreach ($_SESSION['remote'] as $v) {
-                       if ($v['uid'] == $a->profile['profile_uid']) {
-                               $contact_id = $v['cid'];
-                               break;
-                       }
-               }
+       if (!empty(Session::getRemoteContactID($a->profile['profile_uid']))) {
+               $contact_id = Session::getRemoteContactID($a->profile['profile_uid']);
        }
 
-       $groups = [];
        if ($contact_id) {
-               $groups = Group::getIdsByContactId($contact_id);
                $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
                        intval($contact_id),
                        intval($a->profile['profile_uid'])
@@ -142,7 +132,7 @@ function cal_content(App $a)
        }
 
        // get the permissions
-       $sql_perms = Item::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
+       $sql_perms = Item::getPermissionsSQLByUserId($owner_uid);
        // we only want to have the events of the profile owner
        $sql_extra = " AND `event`.`cid` = 0 " . $sql_perms;
 
index 3cafced5426469192c3fbfa3d4260e461fcb4f32..d29fa729ddf4d109c6c5403954b1547b2722d8f1 100644 (file)
@@ -29,7 +29,7 @@ function community_content(App $a, $update = 0)
 {
        $o = '';
 
-       if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
+       if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                notice(L10n::t('Public access denied.') . EOL);
                return;
        }
index d805bcfd49e7db8266beb3cb9e510a70899c775e..c6134bb45d48ee636606a0059377d0776c376a84 100644 (file)
@@ -9,6 +9,7 @@ use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Module\Login;
 use Friendica\Protocol\DFRN;
@@ -49,7 +50,7 @@ function dfrn_poll_init(App $a)
        $hidewall = false;
 
        if (($dfrn_id === '') && empty($_POST['dfrn_id'])) {
-               if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
+               if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                        throw new \Friendica\Network\HTTPException\ForbiddenException();
                }
 
@@ -110,17 +111,14 @@ function dfrn_poll_init(App $a)
 
                                if ((int)$xml->status === 1) {
                                        $_SESSION['authenticated'] = 1;
-                                       if (empty($_SESSION['remote'])) {
-                                               $_SESSION['remote'] = [];
-                                       }
-
-                                       $_SESSION['remote'][$r[0]['uid']] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid']];
-
                                        $_SESSION['visitor_id'] = $r[0]['id'];
                                        $_SESSION['visitor_home'] = $r[0]['url'];
                                        $_SESSION['visitor_handle'] = $r[0]['addr'];
                                        $_SESSION['visitor_visiting'] = $r[0]['uid'];
                                        $_SESSION['my_url'] = $r[0]['url'];
+
+                                       Session::setVisitorsContacts();
+
                                        if (!$quiet) {
                                                info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
                                        }
@@ -517,15 +515,13 @@ function dfrn_poll_content(App $a)
 
                                if (((int) $xml->status == 0) && ($xml->challenge == $hash) && ($xml->sec == $sec)) {
                                        $_SESSION['authenticated'] = 1;
-                                       if (empty($_SESSION['remote'])) {
-                                               $_SESSION['remote'] = [];
-                                       }
-
-                                       $_SESSION['remote'][$r[0]['uid']] = ['cid' => $r[0]['id'], 'uid' => $r[0]['uid']];
                                        $_SESSION['visitor_id'] = $r[0]['id'];
                                        $_SESSION['visitor_home'] = $r[0]['url'];
                                        $_SESSION['visitor_visiting'] = $r[0]['uid'];
                                        $_SESSION['my_url'] = $r[0]['url'];
+
+                                       Session::setVisitorsContacts();
+
                                        if (!$quiet) {
                                                info(L10n::t('%1$s welcomes %2$s', $r[0]['username'], $r[0]['name']) . EOL);
                                        }
index 19879c21bb334ddc35d94b98505cee1bd2e6f3c6..f78da7fb04d70a40c5b841de3d2b82c5edf5da75 100644 (file)
@@ -19,6 +19,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Group;
@@ -592,7 +593,7 @@ function dfrn_request_content(App $a)
                exit();
        } else {
                // Normal web request. Display our user's introduction form.
-               if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
+               if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                        if (!Config::get('system', 'local_block')) {
                                notice(L10n::t('Public access denied.') . EOL);
                                return;
index 6b5edd987c2b2d7e0e4f3f61c3fe71b28ad96774..8513788dfca9eb9244434b67b1d13bd3868a323e 100644 (file)
@@ -14,6 +14,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Group;
@@ -31,7 +32,7 @@ function display_init(App $a)
                Objects::rawContent();
        }
 
-       if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
+       if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                return;
        }
 
@@ -52,9 +53,11 @@ function display_init(App $a)
                        if (DBA::isResult($item)) {
                                $nick = $a->user["nickname"];
                        }
+               }
+
                // Is this item private but could be visible to the remove visitor?
-               } elseif (remote_user()) {
-                       $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1]);
+               if (!DBA::isResult($item) && remote_user()) {
+                       $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1, 'origin' => true]);
                        if (DBA::isResult($item)) {
                                if (!Contact::isFollower(remote_user(), $item['uid'])) {
                                        $item = null;
@@ -84,10 +87,6 @@ function display_init(App $a)
                displayShowFeed($item['id'], $a->argc > 3 && $a->argv[3] == 'conversation.atom');
        }
 
-       if ($a->argc >= 3 && $nick == 'feed-item') {
-               displayShowFeed($item['id'], $a->argc > 3 && $a->argv[3] == 'conversation.atom');
-       }
-
        if (!empty($_SERVER['HTTP_ACCEPT']) && strstr($_SERVER['HTTP_ACCEPT'], 'application/atom+xml')) {
                Logger::log('Directly serving XML for id '.$item["id"], Logger::DEBUG);
                displayShowFeed($item["id"], false);
@@ -102,7 +101,7 @@ function display_init(App $a)
        if (strstr(Strings::normaliseLink($profiledata["url"]), Strings::normaliseLink(System::baseUrl()))) {
                $nickname = str_replace(Strings::normaliseLink(System::baseUrl())."/profile/", "", Strings::normaliseLink($profiledata["url"]));
 
-               if (($nickname != $a->user["nickname"])) {
+               if ($nickname != $a->user["nickname"]) {
                        $profile = DBA::fetchFirst("SELECT `profile`.`uid` AS `profile_uid`, `profile`.* , `contact`.`avatar-date` AS picdate, `user`.* FROM `profile`
                                INNER JOIN `contact` on `contact`.`uid` = `profile`.`uid` INNER JOIN `user` ON `profile`.`uid` = `user`.`uid`
                                WHERE `user`.`nickname` = ? AND `profile`.`is-default` AND `contact`.`self` LIMIT 1",
@@ -197,7 +196,7 @@ function display_fetchauthor($a, $item)
 
 function display_content(App $a, $update = false, $update_uid = 0)
 {
-       if (Config::get('system','block_public') && !local_user() && !remote_user()) {
+       if (Config::get('system','block_public') && !Session::isAuthenticated()) {
                throw new HTTPException\ForbiddenException(L10n::t('Public access denied.'));
        }
 
@@ -229,8 +228,10 @@ function display_content(App $a, $update = false, $update_uid = 0)
                                        $item_parent = $item["parent"];
                                        $item_parent_uri = $item['parent-uri'];
                                }
-                       } elseif (remote_user()) {
-                               $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1]);
+                       }
+
+                       if (($item_parent == 0) && remote_user()) {
+                               $item = Item::selectFirst($fields, ['guid' => $a->argv[1], 'private' => 1, 'origin' => true]);
                                if (DBA::isResult($item) && Contact::isFollower(remote_user(), $item['uid'])) {
                                        $item_id = $item["id"];
                                        $item_parent = $item["parent"];
@@ -269,8 +270,6 @@ function display_content(App $a, $update = false, $update_uid = 0)
                                ['$alternate' => $alternate,
                                        '$conversation' => $conversation]);
 
-       $groups = [];
-       $remote_cid = null;
        $is_remote_contact = false;
        $item_uid = local_user();
 
@@ -279,24 +278,18 @@ function display_content(App $a, $update = false, $update_uid = 0)
                if (DBA::isResult($parent)) {
                        $a->profile['uid'] = defaults($a->profile, 'uid', $parent['uid']);
                        $a->profile['profile_uid'] = defaults($a->profile, 'profile_uid', $parent['uid']);
-                       $is_remote_contact = Contact::isFollower(remote_user(), $a->profile['profile_uid']);
-
+                       $is_remote_contact = Session::getRemoteContactID($a->profile['profile_uid']);
                        if ($is_remote_contact) {
-                               $cdata = Contact::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
-                               if (!empty($cdata['user'])) {
-                                       $groups = Group::getIdsByContactId($cdata['user']);
-                                       $remote_cid = $cdata['user'];
-                                       $item_uid = $parent['uid'];
-                               }
+                               $item_uid = $parent['uid'];
                        }
                }
        }
 
-
        $page_contact = DBA::selectFirst('contact', [], ['self' => true, 'uid' => $a->profile['uid']]);
        if (DBA::isResult($page_contact)) {
                $a->page_contact = $page_contact;
        }
+
        $is_owner = (local_user() && (in_array($a->profile['profile_uid'], [local_user(), 0])) ? true : false);
 
        if (!empty($a->profile['hidewall']) && !$is_owner && !$is_remote_contact) {
@@ -318,7 +311,7 @@ function display_content(App $a, $update = false, $update_uid = 0)
                ];
                $o .= status_editor($a, $x, 0, true);
        }
-       $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid'], $is_remote_contact, $groups, $remote_cid);
+       $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid']);
 
        if (local_user() && (local_user() == $a->profile['profile_uid'])) {
                $condition = ['parent-uri' => $item_parent_uri, 'uid' => local_user(), 'unseen' => true];
@@ -333,7 +326,7 @@ function display_content(App $a, $update = false, $update_uid = 0)
 
        $condition = ["`id` = ? AND `item`.`uid` IN (0, ?) " . $sql_extra, $item_id, $item_uid];
        $fields = ['parent-uri', 'body', 'title', 'author-name', 'author-avatar', 'plink', 'author-id', 'owner-id', 'contact-id'];
-       $item = Item::selectFirstForUser(local_user(), $fields, $condition);
+       $item = Item::selectFirstForUser($a->profile['profile_uid'], $fields, $condition);
 
        if (!DBA::isResult($item)) {
                throw new HTTPException\NotFoundException(L10n::t('The requested item doesn\'t exist or has been deleted.'));
index 828eeaf0910a3fdd5338d529eb49fbc6bb51283e..ad84e24e545dd506911edf9ce7fc78adc689c7fa 100644 (file)
@@ -6,13 +6,14 @@ use Friendica\App;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile;
 use Friendica\Model\User;
 
 function hcard_init(App $a)
 {
-       $blocked = Config::get('system', 'block_public') && !local_user() && !remote_user();
+       $blocked = Config::get('system', 'block_public') && !Session::isAuthenticated();
 
        if ($a->argc > 1) {
                $which = $a->argv[1];
index 8bc394bcb9ef1aef42d5db73863a522c41e24216..b1c0355e01a5c2032fd73a1aac76c5f7056aa486 100644 (file)
@@ -25,6 +25,7 @@ use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Model\Attach;
@@ -42,10 +43,10 @@ use Friendica\Util\Security;
 use Friendica\Util\Strings;
 use Friendica\Worker\Delivery;
 
-require_once 'include/items.php';
+require_once __DIR__ . '/../include/items.php';
 
 function item_post(App $a) {
-       if (!local_user() && !remote_user()) {
+       if (!Session::isAuthenticated()) {
                return 0;
        }
 
@@ -348,18 +349,8 @@ function item_post(App $a) {
        if (local_user() && ((local_user() == $profile_uid) || $allow_comment)) {
                $self = true;
                $author = DBA::selectFirst('contact', [], ['uid' => local_user(), 'self' => true]);
-       } elseif (remote_user()) {
-               if (!empty($_SESSION['remote']) && is_array($_SESSION['remote'])) {
-                       foreach ($_SESSION['remote'] as $v) {
-                               if ($v['uid'] == $profile_uid) {
-                                       $contact_id = $v['cid'];
-                                       break;
-                               }
-                       }
-               }
-               if ($contact_id) {
-                       $author = DBA::selectFirst('contact', [], ['id' => $contact_id]);
-               }
+       } elseif (!empty(Session::getRemoteContactID($profile_uid))) {
+               $author = DBA::selectFirst('contact', [], ['id' => Session::getRemoteContactID($profile_uid)]);
        }
 
        if (DBA::isResult($author)) {
@@ -870,7 +861,7 @@ function item_post_return($baseurl, $api_source, $return_path)
 
 function item_content(App $a)
 {
-       if (!local_user() && !remote_user()) {
+       if (!Session::isAuthenticated()) {
                return;
        }
 
index 50f40b248ccf2aea22b7b00ab92f3e9433ebf5b5..84be1c8285d0f0080bbdbe1e71470379c1e226c2 100644 (file)
@@ -15,6 +15,7 @@ use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Group;
@@ -35,11 +36,7 @@ use Friendica\Util\XML;
 
 function photos_init(App $a) {
 
-       if ($a->argc > 1) {
-               DFRN::autoRedir($a, $a->argv[1]);
-       }
-
-       if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
+       if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                return;
        }
 
@@ -73,7 +70,7 @@ function photos_init(App $a) {
 
                $albums = Photo::getAlbums($a->data['user']['uid']);
 
-               $albums_visible = ((intval($a->data['user']['hidewall']) && !local_user() && !remote_user()) ? false : true);
+               $albums_visible = ((intval($a->data['user']['hidewall']) && !Session::isAuthenticated()) ? false : true);
 
                // add various encodings to the array so we can just loop through and pick them out in a template
                $ret = ['success' => false];
@@ -88,7 +85,7 @@ function photos_init(App $a) {
                        $ret['albums'] = [];
                        foreach ($albums as $k => $album) {
                                //hide profile photos to others
-                               if (!$is_owner && !remote_user() && ($album['album'] == L10n::t('Profile Photos')))
+                               if (!$is_owner && !Session::getRemoteContactID($a->profile_uid) && ($album['album'] == L10n::t('Profile Photos')))
                                        continue;
                                $entry = [
                                        'text'      => $album['album'],
@@ -154,15 +151,10 @@ function photos_post(App $a)
 
        if (local_user() && (local_user() == $page_owner_uid)) {
                $can_post = true;
-       } elseif ($community_page && remote_user($page_owner_uid)) {
-               $contact_id = remote_user($page_owner_uid);
-
-               if ($contact_id > 0) {
-                       if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $page_owner_uid, 'blocked' => false, 'pending' => false])) {
-                               $can_post = true;
-                               $visitor = $contact_id;
-                       }
-               }
+       } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
+               $contact_id = Session::getRemoteContactID($page_owner_uid);
+               $can_post = true;
+               $visitor = $contact_id;
        }
 
        if (!$can_post) {
@@ -837,7 +829,7 @@ function photos_content(App $a)
        // photos/name/image/xxxxx/edit
        // photos/name/image/xxxxx/drop
 
-       if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
+       if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                notice(L10n::t('Public access denied.') . EOL);
                return;
        }
@@ -883,50 +875,24 @@ function photos_content(App $a)
 
        if (local_user() && (local_user() == $owner_uid)) {
                $can_post = true;
-       } else {
-               if ($community_page && remote_user()) {
-                       if (is_array($_SESSION['remote'])) {
-                               foreach ($_SESSION['remote'] as $v) {
-                                       if ($v['uid'] == $owner_uid) {
-                                               $contact_id = $v['cid'];
-                                               break;
-                                       }
-                               }
-                       }
+       } elseif ($community_page && !empty(Session::getRemoteContactID($owner_uid))) {
+               $contact_id = Session::getRemoteContactID($owner_uid);
+               $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
 
-                       if ($contact_id) {
-                               $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
-
-                               if (DBA::isResult($contact)) {
-                                       $can_post = true;
-                                       $remote_contact = true;
-                                       $visitor = $contact_id;
-                               }
-                       }
+               if (DBA::isResult($contact)) {
+                       $can_post = true;
+                       $remote_contact = true;
+                       $visitor = $contact_id;
                }
        }
 
-       $groups = [];
-
        // perhaps they're visiting - but not a community page, so they wouldn't have write access
-       if (remote_user() && !$visitor) {
-               $contact_id = 0;
-               if (is_array($_SESSION['remote'])) {
-                       foreach ($_SESSION['remote'] as $v) {
-                               if ($v['uid'] == $owner_uid) {
-                                       $contact_id = $v['cid'];
-                                       break;
-                               }
-                       }
-               }
+       if (!empty(Session::getRemoteContactID($owner_uid)) && !$visitor) {
+               $contact_id = Session::getRemoteContactID($owner_uid);
 
-               if ($contact_id) {
-                       $groups = Group::getIdsByContactId($contact_id);
+               $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
 
-                       $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => $owner_uid, 'blocked' => false, 'pending' => false]);
-
-                       $remote_contact = DBA::isResult($contact);
-               }
+               $remote_contact = DBA::isResult($contact);
        }
 
        if (!$remote_contact && local_user()) {
@@ -939,7 +905,7 @@ function photos_content(App $a)
                return;
        }
 
-       $sql_extra = Security::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
+       $sql_extra = Security::getPermissionsSQLByUserId($owner_uid);
 
        $o = "";
 
@@ -1598,7 +1564,7 @@ function photos_content(App $a)
                $twist = false;
                foreach ($r as $rr) {
                        //hide profile photos to others
-                       if (!$is_owner && !remote_user() && ($rr['album'] == L10n::t('Profile Photos'))) {
+                       if (!$is_owner && !Session::getRemoteContactID($owner_uid) && ($rr['album'] == L10n::t('Profile Photos'))) {
                                continue;
                        }
 
index 45e2791693fff464fab3c20fec1ef9f7aa9fab67..12d93186e29d65e9a02052fa599ef9661cd59961 100644 (file)
@@ -35,7 +35,7 @@ function redir_init(App $a) {
 
                $contact_url = $contact['url'];
 
-               if ((!local_user() && !remote_user()) // Visitors (not logged in or not remotes) can't authenticate.
+               if (!Session::isAuthenticated() // Visitors (not logged in or not remotes) can't authenticate.
                        || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
                {
                        $a->redirect(defaults($url, $contact_url));
@@ -66,34 +66,16 @@ function redir_init(App $a) {
                        // with the local contact. Otherwise the local user would ask the local contact
                        // for authentification everytime he/she is visiting a profile page of the local
                        // contact.
-                       if ($host == $remotehost
-                               && !empty($_SESSION['remote'])
-                               && is_array($_SESSION['remote']))
-                       {
-                               foreach ($_SESSION['remote'] as $v) {
-                                       if (!empty($v['uid']) && !empty($v['cid']) &&
-                                           $v['uid'] == Session::get('visitor_visiting') &&
-                                           $v['cid'] == Session::get('visitor_id')) {
-                                               // Remote user is already authenticated.
-                                               $target_url = defaults($url, $contact_url);
-                                               Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
-                                               $a->redirect($target_url);
-                                       }
-                               }
+                       if (($host == $remotehost) && (Session::getRemoteContactID(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
+                               // Remote user is already authenticated.
+                               $target_url = defaults($url, $contact_url);
+                               Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
+                               $a->redirect($target_url);
                        }
                }
 
-               // When the remote page does support OWA, then we enforce the use of it
-               $basepath = Contact::getBasepath($contact_url);
-               if (Strings::compareLink($basepath, System::baseUrl())) {
-                       $use_magic = true;
-               } else {
-                       $serverret = Network::curl($basepath . '/magic');
-                       $use_magic = $serverret->isSuccess();
-               }
-
                // Doing remote auth with dfrn.
-               if (local_user() && !$use_magic && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
+               if (local_user() && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
                        $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
 
                        if ($contact['duplex'] && $contact['issued-id']) {
@@ -148,23 +130,6 @@ function redir_magic($a, $cid, $url)
                Logger::info('Got my url', ['visitor' => $visitor]);
        }
 
-       /// @todo Most likely these lines are superfluous. We will remove them in the next version
-       if (empty($visitor) && remote_user()) {
-               $contact = DBA::selectFirst('contact', ['url'], ['id' => remote_user()]);
-               if (!empty($contact['url'])) {
-                       $visitor = $contact['url'];
-                       Logger::info('Got remote user', ['visitor' => $visitor]);
-               }
-       }
-
-       if (empty($visitor) && local_user()) {
-               $contact = DBA::selectFirst('contact', ['url'], ['id' => local_user()]);
-               if (!empty($contact['url'])) {
-                       $visitor = $contact['url'];
-                       Logger::info('Got local user', ['visitor' => $visitor]);
-               }
-       }
-
        $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid]);
        if (!DBA::isResult($contact)) {
                Logger::info('Contact not found', ['id' => $cid]);
index 4a911b4fd9828a51aaf05459bdad0dfc67174c15..9ac59502241ae903916d7ca9b873627e89277c33 100644 (file)
@@ -11,6 +11,7 @@ use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
+use Friendica\Core\Session;
 use Friendica\Core\Renderer;
 use Friendica\Database\DBA;
 use Friendica\Model\Item;
@@ -83,18 +84,18 @@ function search_init(App $a) {
 }
 
 function search_content(App $a) {
-       if (Config::get('system','block_public') && !local_user() && !remote_user()) {
+       if (Config::get('system','block_public') && !Session::isAuthenticated()) {
                notice(L10n::t('Public access denied.') . EOL);
                return;
        }
 
-       if (Config::get('system','local_search') && !local_user() && !remote_user()) {
+       if (Config::get('system','local_search') && !Session::isAuthenticated()) {
                $e = new \Friendica\Network\HTTPException\ForbiddenException(L10n::t("Only logged in users are permitted to perform a search."));
                $e->httpdesc = L10n::t("Public access denied.");
                throw $e;
        }
 
-       if (Config::get('system','permit_crawling') && !local_user() && !remote_user()) {
+       if (Config::get('system','permit_crawling') && !Session::isAuthenticated()) {
                // Default values:
                // 10 requests are "free", after the 11th only a call per minute is allowed
 
index 9fa1a410d37130eff5ad646e84f840c0c1011ba5..29d3f5e06eedda6f0cc34f863d78b743233ff171 100644 (file)
@@ -15,7 +15,7 @@ use Friendica\Util\XML;
 
 function subthread_content(App $a) {
 
-       if (!local_user() && !remote_user()) {
+       if (!Session::isAuthenticated()) {
                return;
        }
 
index 2c15cdd28c68d62f4d836b905713d1c30b435fa7..bc8b712970680761a748519e3207281b7a32fb43 100644 (file)
@@ -7,6 +7,7 @@ use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Model\Item;
@@ -16,7 +17,7 @@ use Friendica\Worker\Delivery;
 
 function tagger_content(App $a) {
 
-       if (!local_user() && !remote_user()) {
+       if (!Session::isAuthenticated()) {
                return;
        }
 
index 9e19ecf11744d4d53da9b6d163095007919b2e52..48027a603e9450711b8b89d1dea4303740c1f58c 100644 (file)
@@ -10,6 +10,7 @@ use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Model\Attach;
 use Friendica\Model\Contact;
@@ -22,11 +23,7 @@ use Friendica\Util\Security;
 
 function videos_init(App $a)
 {
-       if ($a->argc > 1) {
-               DFRN::autoRedir($a, $a->argv[1]);
-       }
-
-       if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
+       if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                return;
        }
 
@@ -114,7 +111,7 @@ function videos_content(App $a)
        // videos/name/video/xxxxx/edit
 
 
-       if ((Config::get('system', 'block_public')) && (!local_user()) && (!remote_user())) {
+       if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                notice(L10n::t('Public access denied.') . EOL);
                return;
        }
@@ -154,64 +151,25 @@ function videos_content(App $a)
 
        if ((local_user()) && (local_user() == $owner_uid)) {
                $can_post = true;
-       } elseif ($community_page && remote_user()) {
-               if (!empty($_SESSION['remote'])) {
-                       foreach ($_SESSION['remote'] as $v) {
-                               if ($v['uid'] == $owner_uid) {
-                                       $contact_id = $v['cid'];
-                                       break;
-                               }
-                       }
-               }
-
-               if ($contact_id > 0) {
-                       $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
-                               intval($contact_id),
-                               intval($owner_uid)
-                       );
-
-                       if (DBA::isResult($r)) {
-                               $can_post = true;
-                               $remote_contact = true;
-                               $visitor = $contact_id;
-                       }
-               }
+       } elseif ($community_page && !empty(Session::getRemoteContactID($owner_uid))) {
+               $contact_id = Session::getRemoteContactID($owner_uid);
+               $can_post = true;
+               $remote_contact = true;
+               $visitor = $contact_id;
        }
 
-       $groups = [];
-
        // perhaps they're visiting - but not a community page, so they wouldn't have write access
-       if (remote_user() && (!$visitor)) {
-               $contact_id = 0;
-
-               if (!empty($_SESSION['remote'])) {
-                       foreach($_SESSION['remote'] as $v) {
-                               if($v['uid'] == $owner_uid) {
-                                       $contact_id = $v['cid'];
-                                       break;
-                               }
-                       }
-               }
-
-               if ($contact_id > 0) {
-                       $groups = Group::getIdsByContactId($contact_id);
-                       $r = q("SELECT * FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
-                               intval($contact_id),
-                               intval($owner_uid)
-                       );
-
-                       if (DBA::isResult($r)) {
-                               $remote_contact = true;
-                       }
-               }
+       if (!empty(Session::getRemoteContactID($owner_uid)) && !$visitor) {
+               $contact_id = Session::getRemoteContactID($owner_uid);
+               $remote_contact = true;
        }
 
-       if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && (!$remote_contact)) {
+       if ($a->data['user']['hidewall'] && (local_user() != $owner_uid) && !$remote_contact) {
                notice(L10n::t('Access to this item is restricted.') . EOL);
                return;
        }
 
-       $sql_extra = Security::getPermissionsSQLByUserId($owner_uid, $remote_contact, $groups);
+       $sql_extra = Security::getPermissionsSQLByUserId($owner_uid);
 
        $o = "";
 
index c4ee33bd18f97468b614bc5c598db8fad5091688..0324a5581ca9a674d48ac08381edd6289e392964 100644 (file)
@@ -6,6 +6,7 @@
 use Friendica\App;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Model\Attach;
 use Friendica\Model\User;
@@ -43,35 +44,21 @@ function wall_attach_post(App $a) {
        $page_owner_cid   = $r[0]['id'];
        $community_page   = (($r[0]['page-flags'] == User::PAGE_FLAGS_COMMUNITY) ? true : false);
 
-       if ((local_user()) && (local_user() == $page_owner_uid)) {
+       if (local_user() && (local_user() == $page_owner_uid)) {
                $can_post = true;
-       } else {
-               if ($community_page && remote_user()) {
-                       $contact_id = 0;
-
-                       if (is_array($_SESSION['remote'])) {
-                               foreach ($_SESSION['remote'] as $v) {
-                                       if ($v['uid'] == $page_owner_uid) {
-                                               $contact_id = $v['cid'];
-                                               break;
-                                       }
-                               }
-                       }
-
-                       if ($contact_id > 0) {
-                               $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
-                                       intval($contact_id),
-                                       intval($page_owner_uid)
-                               );
+       } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
+               $contact_id = Session::getRemoteContactID($page_owner_uid);
+               $r = q("SELECT `uid` FROM `contact` WHERE `blocked` = 0 AND `pending` = 0 AND `id` = %d AND `uid` = %d LIMIT 1",
+                       intval($contact_id),
+                       intval($page_owner_uid)
+               );
 
-                               if (DBA::isResult($r)) {
-                                       $can_post = true;
-                               }
-                       }
+               if (DBA::isResult($r)) {
+                       $can_post = true;
                }
        }
 
-       if (! $can_post) {
+       if (!$can_post) {
                if ($r_json) {
                        echo json_encode(['error' => L10n::t('Permission denied.')]);
                        exit();
index a245ca739cdb80b39acbab56d2e06fc96bf51840..1224b6dab0a6cb00fbaea2c0ceacf1c20fda365f 100644 (file)
@@ -12,6 +12,7 @@ use Friendica\App;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Core\Config;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
@@ -74,34 +75,21 @@ function wall_upload_post(App $a, $desktopmode = true)
 
        if ((local_user()) && (local_user() == $page_owner_uid)) {
                $can_post = true;
-       } else {
-               if ($community_page && remote_user()) {
-                       $contact_id = 0;
-                       if (is_array($_SESSION['remote'])) {
-                               foreach ($_SESSION['remote'] as $v) {
-                                       if ($v['uid'] == $page_owner_uid) {
-                                               $contact_id = $v['cid'];
-                                               break;
-                                       }
-                               }
-                       }
-
-                       if ($contact_id) {
-                               $r = q("SELECT `uid` FROM `contact`
-                                       WHERE `blocked` = 0 AND `pending` = 0
-                                       AND `id` = %d AND `uid` = %d LIMIT 1",
-                                       intval($contact_id),
-                                       intval($page_owner_uid)
-                               );
-                               if (DBA::isResult($r)) {
-                                       $can_post = true;
-                                       $visitor = $contact_id;
-                               }
-                       }
+       } elseif ($community_page && !empty(Session::getRemoteContactID($page_owner_uid))) {
+               $contact_id = Session::getRemoteContactID($page_owner_uid);
+
+               $r = q("SELECT `uid` FROM `contact`
+                       WHERE `blocked` = 0 AND `pending` = 0
+                       AND `id` = %d AND `uid` = %d LIMIT 1",
+                       intval($contact_id),
+                       intval($page_owner_uid)
+               );
+               if (DBA::isResult($r)) {
+                       $can_post = true;
+                       $visitor = $contact_id;
                }
        }
 
-
        if (!$can_post) {
                if ($r_json) {
                        echo json_encode(['error' => L10n::t('Permission denied.')]);
diff --git a/phpunit.xml b/phpunit.xml
deleted file mode 100644 (file)
index a46f7be..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-<?xml version="1.0"?>
-<phpunit
-       bootstrap="tests/bootstrap.php"
-       verbose="true">
-    <testsuites>
-        <testsuite>
-            <directory>tests/</directory>
-        </testsuite>
-    </testsuites>
-       <!-- Filters for Code Coverage -->
-       <filter>
-               <whitelist>
-                       <directory suffix=".php">.</directory>
-                       <exclude>
-                               <directory suffix=".php">config/</directory>
-                               <directory suffix=".php">doc/</directory>
-                               <directory suffix=".php">images/</directory>
-                               <directory suffix=".php">library/</directory>
-                               <directory suffix=".php">spec/</directory>
-                               <directory suffix=".php">tests/</directory>
-                               <directory suffix=".php">view/</directory>
-                       </exclude>
-               </whitelist>
-       </filter>
-       <logging>
-               <log type="coverage-clover" target="clover.xml" />
-       </logging>
-       <listeners>
-               <listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
-       </listeners>
-</phpunit>
index ea5c0bbc05230c77984392b5171f1f2846383825..8140c5f3c13780a77da77160e95a264b7b4c86ef 100644 (file)
@@ -149,7 +149,7 @@ class Nav
                $nav['usermenu'] = [];
                $userinfo = null;
 
-               if (local_user() || remote_user()) {
+               if (Session::isAuthenticated()) {
                        $nav['logout'] = ['logout', L10n::t('Logout'), '', L10n::t('End this session')];
                } else {
                        $nav['login'] = ['login', L10n::t('Login'), ($a->module == 'login' ? 'selected' : ''), L10n::t('Sign in')];
@@ -182,7 +182,7 @@ class Nav
                        $nav['home'] = [$homelink, L10n::t('Home'), '', L10n::t('Home Page')];
                }
 
-               if (intval(Config::get('config', 'register_policy')) === \Friendica\Module\Register::OPEN && !local_user() && !remote_user()) {
+               if (intval(Config::get('config', 'register_policy')) === \Friendica\Module\Register::OPEN && !Session::isAuthenticated()) {
                        $nav['register'] = ['register', L10n::t('Register'), '', L10n::t('Create an account')];
                }
 
index 90974427965538d5425637361e7c437a21bad759..09a5fc6345b75bbe01e25ff10b30ec195935a126 100644 (file)
@@ -11,6 +11,7 @@ use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\FileTag;
@@ -339,7 +340,7 @@ class Widget
 
                $zcid = 0;
 
-               $cid = remote_user($profile_uid);
+               $cid = Session::getRemoteContactID($profile_uid);
 
                if (!$cid) {
                        if (Profile::getMyURL()) {
index 6d9a95a72525cbefb7c4c690086a34fe80558131..55c174d96b51563e8b50cdf6fd9c8b231e17da2e 100644 (file)
@@ -11,6 +11,7 @@ use Friendica\Content\Feature;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\GContact;
+use Friendica\Core\Session;
 use Friendica\Util\Network;
 
 /**
@@ -333,7 +334,7 @@ class ACL extends BaseObject
         */
        public static function contactAutocomplete($search, $mode, int $page = 1)
        {
-               if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
+               if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                        return [];
                }
 
index 717166952010d851bd50cf497a2738e77b45363c..6797a70c2b9a919ae40b73b86f5d390a312a8fce 100644 (file)
@@ -15,6 +15,7 @@ class MemcacheCache extends Cache implements IMemoryCache
 {
        use TraitCompareSet;
        use TraitCompareDelete;
+       use TraitMemcacheCommand;
 
        /**
         * @var Memcache
@@ -34,11 +35,11 @@ class MemcacheCache extends Cache implements IMemoryCache
 
                $this->memcache = new Memcache();
 
-               $memcache_host = $config->get('system', 'memcache_host');
-               $memcache_port = $config->get('system', 'memcache_port');
+               $this->server = $config->get('system', 'memcache_host');;
+               $this->port = $config->get('system', 'memcache_port');
 
-               if (!$this->memcache->connect($memcache_host, $memcache_port)) {
-                       throw new Exception('Expected Memcache server at ' . $memcache_host . ':' . $memcache_port . ' isn\'t available');
+               if (!@$this->memcache->connect($this->server, $this->port)) {
+                       throw new Exception('Expected Memcache server at ' . $this->server . ':' . $this->port . ' isn\'t available');
                }
        }
 
@@ -47,21 +48,7 @@ class MemcacheCache extends Cache implements IMemoryCache
         */
        public function getAllKeys($prefix = null)
        {
-               $keys     = [];
-               $allSlabs = $this->memcache->getExtendedStats('slabs');
-               foreach ($allSlabs as $slabs) {
-                       foreach (array_keys($slabs) as $slabId) {
-                               $cachedump = $this->memcache->getExtendedStats('cachedump', (int)$slabId);
-                               foreach ($cachedump as $key => $arrVal) {
-                                       if (!is_array($arrVal)) {
-                                               continue;
-                                       }
-                                       $keys = array_merge($keys, array_keys($arrVal));
-                               }
-                       }
-               }
-
-               $keys = $this->getOriginalKeys($keys);
+               $keys = $this->getOriginalKeys($this->getMemcacheKeys());
 
                return $this->filterArrayKeysByPrefix($keys, $prefix);
        }
index 69f6b9a0a7c0eea4c1da800ac2ba224c5bf7a058..95bfae39f25d0100f31118aaf5166c412e8c81c5 100644 (file)
@@ -16,6 +16,7 @@ class MemcachedCache extends Cache implements IMemoryCache
 {
        use TraitCompareSet;
        use TraitCompareDelete;
+       use TraitMemcacheCommand;
 
        /**
         * @var \Memcached
@@ -27,17 +28,6 @@ class MemcachedCache extends Cache implements IMemoryCache
         */
        private $logger;
 
-       /**
-        * @var string First server address
-        */
-
-       private $firstServer;
-
-       /**
-        * @var int First server port
-        */
-       private $firstPort;
-
        /**
         * Due to limitations of the INI format, the expected configuration for Memcached servers is the following:
         * array {
@@ -69,8 +59,8 @@ class MemcachedCache extends Cache implements IMemoryCache
                        }
                });
 
-               $this->firstServer = $memcached_hosts[0][0] ?? 'localhost';
-               $this->firstPort   = $memcached_hosts[0][1] ?? 11211;
+               $this->server = $memcached_hosts[0][0] ?? 'localhost';
+               $this->port = $memcached_hosts[0][1] ?? 11211;
 
                $this->memcached->addServers($memcached_hosts);
 
@@ -84,97 +74,11 @@ class MemcachedCache extends Cache implements IMemoryCache
         */
        public function getAllKeys($prefix = null)
        {
-               $keys = $this->getOriginalKeys($this->getMemcachedKeys());
+               $keys = $this->getOriginalKeys($this->getMemcacheKeys());
 
                return $this->filterArrayKeysByPrefix($keys, $prefix);
        }
 
-       /**
-        * Get all memcached keys.
-        * Special function because getAllKeys() is broken since memcached 1.4.23.
-        *
-        * cleaned up version of code found on Stackoverflow.com by Maduka Jayalath
-        * @see https://stackoverflow.com/a/34724821
-        *
-        * @return array|int - all retrieved keys (or negative number on error)
-        */
-       private function getMemcachedKeys()
-       {
-               $mem = @fsockopen($this->firstServer, $this->firstPort);
-               if ($mem === false) {
-                       return -1;
-               }
-
-               // retrieve distinct slab
-               $r = @fwrite($mem, 'stats items' . chr(10));
-               if ($r === false) {
-                       return -2;
-               }
-
-               $slab = [];
-               while (($l = @fgets($mem, 1024)) !== false) {
-                       // finished?
-                       $l = trim($l);
-                       if ($l == 'END') {
-                               break;
-                       }
-
-                       $m = [];
-                       // <STAT items:22:evicted_nonzero 0>
-                       $r = preg_match('/^STAT\sitems\:(\d+)\:/', $l, $m);
-                       if ($r != 1) {
-                               return -3;
-                       }
-                       $a_slab = $m[1];
-
-                       if (!array_key_exists($a_slab, $slab)) {
-                               $slab[$a_slab] = [];
-                       }
-               }
-
-               reset($slab);
-               foreach ($slab as $a_slab_key => &$a_slab) {
-                       $r = @fwrite($mem, 'stats cachedump ' . $a_slab_key . ' 100' . chr(10));
-                       if ($r === false) {
-                               return -4;
-                       }
-
-                       while (($l = @fgets($mem, 1024)) !== false) {
-                               // finished?
-                               $l = trim($l);
-                               if ($l == 'END') {
-                                       break;
-                               }
-
-                               $m = [];
-                               // ITEM 42 [118 b; 1354717302 s]
-                               $r = preg_match('/^ITEM\s([^\s]+)\s/', $l, $m);
-                               if ($r != 1) {
-                                       return -5;
-                               }
-                               $a_key = $m[1];
-
-                               $a_slab[] = $a_key;
-                       }
-               }
-
-               // close the connection
-               @fclose($mem);
-               unset($mem);
-
-               $keys = [];
-               reset($slab);
-               foreach ($slab AS &$a_slab) {
-                       reset($a_slab);
-                       foreach ($a_slab AS &$a_key) {
-                               $keys[] = $a_key;
-                       }
-               }
-               unset($slab);
-
-               return $keys;
-       }
-
        /**
         * (@inheritdoc)
         */
index b2638c49f3ef5647831157553cc7db008c2e7b87..3558a38464ca9c20acdf343b04e31b1a207c9c04 100644 (file)
@@ -37,9 +37,9 @@ class RedisCache extends Cache implements IMemoryCache
                $redis_pw   = $config->get('system', 'redis_password');
                $redis_db   = $config->get('system', 'redis_db', 0);
 
-               if (isset($redis_port) && !$this->redis->connect($redis_host, $redis_port)) {
+               if (isset($redis_port) && !@$this->redis->connect($redis_host, $redis_port)) {
                        throw new Exception('Expected Redis server at ' . $redis_host . ':' . $redis_port . ' isn\'t available');
-               } elseif (!$this->redis->connect($redis_host)) {
+               } elseif (!@$this->redis->connect($redis_host)) {
                        throw new Exception('Expected Redis server at ' . $redis_host . ' isn\'t available');
                }
 
diff --git a/src/Core/Cache/TraitMemcacheCommand.php b/src/Core/Cache/TraitMemcacheCommand.php
new file mode 100644 (file)
index 0000000..0bbab79
--- /dev/null
@@ -0,0 +1,104 @@
+<?php
+
+namespace Friendica\Core\Cache;
+
+use Friendica\Network\HTTPException\InternalServerErrorException;
+
+/**
+ * Trait for Memcache to add a custom version of the
+ * method getAllKeys() since this isn't working anymore
+ *
+ * Adds the possibility to directly communicate with the memcache too
+ */
+trait TraitMemcacheCommand
+{
+       /**
+        * @var string server address
+        */
+       protected $server;
+
+       /**
+        * @var int server port
+        */
+       protected $port;
+
+       /**
+        * Retrieves the stored keys of the memcache instance
+        * Uses custom commands, which aren't bound to the used instance of the class
+        *
+        * @todo Due the fact that we use a custom command, there are race conditions possible:
+        *       - $this->memcache(d) adds a key
+        *       - $this->getMemcacheKeys is called directly "after"
+        *       - But $this->memcache(d) isn't finished adding the key, so getMemcacheKeys doesn't find it
+        *
+        * @return array All keys of the memcache instance
+        *
+        * @throws InternalServerErrorException
+        */
+       protected function getMemcacheKeys()
+       {
+               $string = $this->sendMemcacheCommand("stats items");
+               $lines  = explode("\r\n", $string);
+               $slabs  = [];
+               $keys   = [];
+
+               foreach ($lines as $line) {
+
+                       if (preg_match("/STAT items:([\d]+):number ([\d]+)/", $line, $matches) &&
+                           isset($matches[1]) &&
+                           !in_array($matches[1], $keys)) {
+
+                               $slabs[] = $matches[1];
+                               $string  = $this->sendMemcacheCommand("stats cachedump " . $matches[1] . " " . $matches[2]);
+                               preg_match_all("/ITEM (.*?) /", $string, $matches);
+                               $keys = array_merge($keys, $matches[1]);
+                       }
+               }
+
+               return $keys;
+       }
+
+       /**
+        * Taken directly from memcache PECL source
+        * Sends a command to the memcache instance and returns the result
+        * as a string
+        *
+        * http://pecl.php.net/package/memcache
+        *
+        * @param string $command The command to send to the Memcache server
+        *
+        * @return string The returned buffer result
+        *
+        * @throws InternalServerErrorException In case the memcache server isn't available (anymore)
+        */
+       protected function sendMemcacheCommand(string $command)
+       {
+               $s = @fsockopen($this->server, $this->port);
+               if (!$s) {
+                       throw new InternalServerErrorException("Cant connect to:" . $this->server . ':' . $this->port);
+               }
+
+               fwrite($s, $command . "\r\n");
+               $buf = '';
+
+               while (!feof($s)) {
+
+                       $buf .= fgets($s, 256);
+
+                       if (strpos($buf, "END\r\n") !== false) { // stat says end
+                               break;
+                       }
+
+                       if (strpos($buf, "DELETED\r\n") !== false || strpos($buf, "NOT_FOUND\r\n") !== false) { // delete says these
+                               break;
+                       }
+
+                       if (strpos($buf, "OK\r\n") !== false) { // flush_all says ok
+                               break;
+                       }
+               }
+
+               fclose($s);
+               return ($buf);
+       }
+}
index 9927fca189336f539bfc339f0e1b6222fa5326f3..b44ff3e2ea2fa928a69c4213b63c3339a20c4303 100644 (file)
@@ -53,7 +53,7 @@ class Session
 
        /**
         * Retrieves a key from the session super global or the defaults if the key is missing or the value is falsy.
-        * 
+        *
         * Handle the case where session_start() hasn't been called and the super global isn't available.
         *
         * @param string $name
@@ -119,21 +119,10 @@ class Session
                        'page_flags'    => $user_record['page-flags'],
                        'my_url'        => $a->getBaseURL() . '/profile/' . $user_record['nickname'],
                        'my_address'    => $user_record['nickname'] . '@' . substr($a->getBaseURL(), strpos($a->getBaseURL(), '://') + 3),
-                       'addr'          => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0'),
-                       'remote'        => []
+                       'addr'          => defaults($_SERVER, 'REMOTE_ADDR', '0.0.0.0')
                ]);
 
-               $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($_SESSION['my_url']), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
-               while ($contact = DBA::fetch($remote_contacts)) {
-                       if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
-                               continue;
-                       }
-
-                       /// @todo Change it to this format to save space
-                       // $_SESSION['remote'][$contact['uid']] = $contact['id'];
-                       $_SESSION['remote'][$contact['uid']] = ['cid' => $contact['id'], 'uid' => $contact['uid']];
-               }
-               DBA::close($remote_contacts);
+               self::setVisitorsContacts();
 
                $member_since = strtotime($user_record['register_date']);
                self::set('new_member', time() < ($member_since + ( 60 * 60 * 24 * 14)));
@@ -216,4 +205,68 @@ class Session
                        }
                }
        }
+
+       /**
+        * Returns contact ID for given user ID
+        *
+        * @param integer $uid User ID
+        * @return integer Contact ID of visitor for given user ID
+        */
+       public static function getRemoteContactID($uid)
+       {
+               if (empty($_SESSION['remote'][$uid])) {
+                       return false;
+               }
+
+               return $_SESSION['remote'][$uid];
+       }
+
+       /**
+        * Returns User ID for given contact ID of the visitor
+        *
+        * @param integer $cid Contact ID
+        * @return integer User ID for given contact ID of the visitor
+        */
+       public static function getUserIDForVisitorContactID($cid)
+       {
+               if (empty($_SESSION['remote'])) {
+                       return false;
+               }
+
+               return array_search($cid, $_SESSION['remote']);
+       }
+
+       /**
+        * Set the session variable that contains the contact IDs for the visitor's contact URL
+        *
+        * @param string $url Contact URL
+        */
+       public static function setVisitorsContacts()
+       {
+               $_SESSION['remote'] = [];
+
+               $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => Strings::normaliseLink($_SESSION['my_url']), 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
+               while ($contact = DBA::fetch($remote_contacts)) {
+                       if (($contact['uid'] == 0) || Contact::isBlockedByUser($contact['id'], $contact['uid'])) {
+                               continue;
+                       }
+
+                       $_SESSION['remote'][$contact['uid']] = $contact['id'];
+               }
+               DBA::close($remote_contacts);
+       }
+
+       /**
+        * Returns if the current visitor is authenticated
+        *
+        * @return boolean "true" when visitor is either a local or remote user
+        */
+       public static function isAuthenticated()
+       {
+               if (empty($_SESSION['authenticated'])) {
+                       return false;
+               }
+
+               return $_SESSION['authenticated'];
+       }
 }
index cf707f205528aa4a1086884710f1deba573ea1f2..72b903e076d6e6e931ff41981848a386b3a7752f 100644 (file)
@@ -12,7 +12,7 @@ use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Util\DateTimeFormat;
 
-require_once 'include/dba.php';
+require_once __DIR__ . '/../../include/dba.php';
 
 /**
  * @brief This class contain functions for the database management
index bf819e7e80ee35116f89425e28aaf111940d1820..72ce6bbe6c51616ad33f2c41d9b17f5411b41733 100644 (file)
@@ -67,7 +67,7 @@ class Database
        {
                // Use environment variables for mysql if they are set beforehand
                if (!empty($server['MYSQL_HOST'])
-                   && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
+                   && (!empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER'])))
                    && $server['MYSQL_PASSWORD'] !== false
                    && !empty($server['MYSQL_DATABASE']))
                {
index 1e3d0b9154e1e537e808d46d744395e81ce9ee37..74534fc37151b92008f7d22671ad18c49b87ee6e 100644 (file)
@@ -13,6 +13,7 @@ use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Network\Probe;
@@ -1213,7 +1214,7 @@ class Contact extends BaseObject
                }
 
                if (($contact['network'] == Protocol::DFRN) && !$contact['self'] && empty($contact['pending'])) {
-                       $poke_link = System::baseUrl() . '/poke/?f=&c=' . $contact['id'];
+                       $poke_link = System::baseUrl() . '/poke/?c=' . $contact['id'];
                }
 
                $contact_url = System::baseUrl() . '/contact/' . $contact['id'];
@@ -2679,7 +2680,7 @@ class Contact extends BaseObject
         */
        public static function magicLink($contact_url, $url = '')
        {
-               if (!local_user() && !remote_user()) {
+               if (!Session::isAuthenticated()) {
                        return $url ?: $contact_url; // Equivalent to: ($url != '') ? $url : $contact_url;
                }
 
@@ -2725,7 +2726,7 @@ class Contact extends BaseObject
        {
                $destination = $url ?: $contact['url']; // Equivalent to ($url != '') ? $url : $contact['url'];
 
-               if ((!local_user() && !remote_user()) || ($contact['network'] != Protocol::DFRN)) {
+               if (!Session::isAuthenticated() || ($contact['network'] != Protocol::DFRN)) {
                        return $destination;
                }
 
index b5c68d9ab7cbfc87a57bbf1c09be3eb130d7ae23..c73ea99b1bfb3d863207f1827253fb6ad59ec7c4 100644 (file)
@@ -18,6 +18,7 @@ use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
 use Friendica\Core\System;
+use Friendica\Core\Session;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Protocol\ActivityPub;
@@ -3028,7 +3029,7 @@ class Item extends BaseObject
         */
        public static function performLike($item_id, $verb)
        {
-               if (!local_user() && !remote_user()) {
+               if (!Session::isAuthenticated()) {
                        return false;
                }
 
@@ -3260,10 +3261,10 @@ class Item extends BaseObject
                }
        }
 
-       public static function getPermissionsSQLByUserId($owner_id, $remote_verified = false, $groups = null, $remote_cid = null)
+       public static function getPermissionsSQLByUserId($owner_id)
        {
                $local_user = local_user();
-               $remote_user = remote_user();
+               $remote_user = Session::getRemoteContactID($owner_id);
 
                /*
                 * Construct permissions
@@ -3283,7 +3284,7 @@ class Item extends BaseObject
                         * If pre-verified, the caller is expected to have already
                         * done this and passed the groups into this function.
                         */
-                       $set = PermissionSet::get($owner_id, $remote_cid, $groups);
+                       $set = PermissionSet::get($owner_id, $remote_user);
 
                        if (!empty($set)) {
                                $sql_set = " OR (`item`.`private` IN (1,2) AND `item`.`wall` AND `item`.`psid` IN (" . implode(',', $set) . "))";
@@ -3427,7 +3428,7 @@ class Item extends BaseObject
                }
 
                // Update the cached values if there is no "zrl=..." on the links.
-               $update = (!local_user() && !remote_user() && ($item["uid"] == 0));
+               $update = (!Session::isAuthenticated() && ($item["uid"] == 0));
 
                // Or update it if the current viewer is the intented viewer.
                if (($item["uid"] == local_user()) && ($item["uid"] != 0)) {
index 3148d4da03360330f7127b729e7a56f98788838d..90448806d8bd1e8cc62022bf33117dc1e0662310 100644 (file)
@@ -67,21 +67,20 @@ class PermissionSet extends BaseObject
         *
         * @param integer $uid        User id whom the items belong
         * @param integer $contact_id Contact id of the visitor
-        * @param array   $groups     Possibly previously fetched group ids for that contact
         *
         * @return array of permission set ids.
         * @throws \Exception
         */
-
-       static public function get($uid, $contact_id, $groups = null)
+       static public function get($uid, $contact_id)
        {
-               if (empty($groups) && DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
+               if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) {
                        $groups = Group::getIdsByContactId($contact_id);
                }
 
                if (empty($groups) || !is_array($groups)) {
                        return [];
                }
+
                $group_str = '<<>>'; // should be impossible to match
 
                foreach ($groups as $g) {
@@ -90,11 +89,9 @@ class PermissionSet extends BaseObject
 
                $contact_str = '<' . $contact_id . '>';
 
-               $condition = ["`uid` = ? AND (`allow_cid` = '' OR`allow_cid` REGEXP ?)
-                       AND (`deny_cid` = '' OR NOT `deny_cid` REGEXP ?)
-                       AND (`allow_gid` = '' OR `allow_gid` REGEXP ?)
-                       AND (`deny_gid` = '' OR NOT `deny_gid` REGEXP ?)",
-                       $uid, $contact_str, $contact_str, $group_str, $group_str];
+               $condition = ["`uid` = ? AND (NOT (`deny_cid` REGEXP ? OR deny_gid REGEXP ?)
+                       AND (allow_cid REGEXP ? OR allow_gid REGEXP ? OR (allow_cid = '' AND allow_gid = '')))",
+                       $uid, $contact_str, $group_str, $contact_str, $group_str];
 
                $ret = DBA::select('permissionset', ['id'], $condition);
                $set = [];
index 11721c81fdb092dc21267e13ec6362215a1d3c2f..34a5acfc9f3694d4e35fce63587aa870b147bd1e 100644 (file)
@@ -131,31 +131,17 @@ class Photo extends BaseObject
         */
        public static function getPhoto($resourceid, $scale = 0)
        {
-               $r = self::selectFirst(["uid", "allow_cid", "allow_gid", "deny_cid", "deny_gid"], ["resource-id" => $resourceid]);
-               if ($r === false) {
+               $r = self::selectFirst(["uid"], ["resource-id" => $resourceid]);
+               if (!DBA::isResult($r)) {
                        return false;
                }
-               $uid = $r["uid"];
 
-               // This is the first place, when retrieving just a photo, that we know who owns the photo.
-               // Check if the photo is public (empty allow and deny means public), if so, skip auth attempt, if not
-               // make sure that the requester's session is appropriately authenticated to that user
-               // otherwise permissions checks done by getPermissionsSQLByUserId() won't work correctly
-               if (!empty($r["allow_cid"]) || !empty($r["allow_gid"]) || !empty($r["deny_cid"]) || !empty($r["deny_gid"])) {
-                       $r = DBA::selectFirst("user", ["nickname"], ["uid" => $uid], []);
-                       // this will either just return (if auth all ok) or will redirect and exit (starting over)
-                       DFRN::autoRedir(self::getApp(), $r["nickname"]);
-               }
+               $uid = $r["uid"];
 
                $sql_acl = Security::getPermissionsSQLByUserId($uid);
 
-               $conditions = [
-                       "`resource-id` = ? AND `scale` <= ? " . $sql_acl,
-                       $resourceid, $scale
-               ];
-
+               $conditions = ["`resource-id` = ? AND `scale` <= ? " . $sql_acl, $resourceid, $scale];
                $params = ["order" => ["scale" => true]];
-
                $photo = self::selectFirst([], $conditions, $params);
 
                return $photo;
index 290b6d349018526de27774ffebd61ddcb82e1b02..cf39a02a913d9d120fd0e487e314ec6aa4e629d0 100644 (file)
@@ -215,7 +215,7 @@ class Profile
                        );
                }
 
-               $block = ((Config::get('system', 'block_public') && !local_user() && !remote_user()) ? true : false);
+               $block = ((Config::get('system', 'block_public') && !Session::isAuthenticated()) ? true : false);
 
                /**
                 * @todo
@@ -248,15 +248,10 @@ class Profile
         */
        public static function getByNickname($nickname, $uid = 0, $profile_id = 0)
        {
-               if (remote_user($uid) && !empty($_SESSION['remote'])) {
-                       foreach ($_SESSION['remote'] as $visitor) {
-                               if ($visitor['uid'] == $uid) {
-                                       $contact = DBA::selectFirst('contact', ['profile-id'], ['id' => $visitor['cid']]);
-                                       if (DBA::isResult($contact)) {
-                                               $profile_id = $contact['profile-id'];
-                                       }
-                                       break;
-                               }
+               if (!empty(Session::getRemoteContactID($uid))) {
+                       $contact = DBA::selectFirst('contact', ['profile-id'], ['id' => Session::getRemoteContactID($uid)]);
+                       if (DBA::isResult($contact)) {
+                               $profile_id = $contact['profile-id'];
                        }
                }
 
@@ -453,7 +448,7 @@ class Profile
                $about    = !empty($profile['about'])    ? L10n::t('About:')    : false;
                $xmpp     = !empty($profile['xmpp'])     ? L10n::t('XMPP:')     : false;
 
-               if ((!empty($profile['hidewall']) || $block) && !local_user() && !remote_user()) {
+               if ((!empty($profile['hidewall']) || $block) && !Session::isAuthenticated()) {
                        $location = $gender = $marital = $homepage = $about = false;
                }
 
@@ -1029,6 +1024,12 @@ class Profile
         *
         * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/include/channel.php
         *
+        * The implementation for Friendica sadly differs in some points from the one for Hubzilla:
+        * - Hubzilla uses the "zid" parameter, while for Friendica it had been replaced with "zrl"
+        * - There seem to be some reverse authentication (rmagic) that isn't implemented in Friendica at all
+        *
+        * It would be favourable to harmonize the two implementations.
+        *
         * @param App $a Application instance.
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
@@ -1042,6 +1043,8 @@ class Profile
                        return;
                }
 
+               $addr = $_GET['addr'] ?? $my_url;
+
                $arr = ['zrl' => $my_url, 'url' => $a->cmd];
                Hook::callAll('zrl_init', $arr);
 
@@ -1072,19 +1075,19 @@ class Profile
 
                Worker::add(PRIORITY_LOW, 'GProbe', $my_url);
 
-               // Try to avoid recursion - but send them home to do a proper magic auth.
-               $query = str_replace(array('?zrl=', '&zid='), array('?rzrl=', '&rzrl='), $a->query_string);
+               // Remove the "addr" parameter from the destination. It is later added as separate parameter again.
+               $addr_request = 'addr=' . urlencode($addr);
+               $query = rtrim(str_replace($addr_request, '', $a->query_string), '?&');
+
                // The other instance needs to know where to redirect.
                $dest = urlencode($a->getBaseURL() . '/' . $query);
 
                // We need to extract the basebath from the profile url
                // to redirect the visitors '/magic' module.
-               // Note: We should have the basepath of a contact also in the contact table.
-               $urlarr = explode('/profile/', $contact['url']);
-               $basepath = $urlarr[0];
+               $basepath = Contact::getBasepath($contact['url']);
 
-               if ($basepath != $a->getBaseURL() && !strstr($dest, '/magic') && !strstr($dest, '/rmagic')) {
-                       $magic_path = $basepath . '/magic' . '?f=&owa=1&dest=' . $dest;
+               if ($basepath != $a->getBaseURL() && !strstr($dest, '/magic')) {
+                       $magic_path = $basepath . '/magic' . '?owa=1&dest=' . $dest . '&' . $addr_request;
 
                        // We have to check if the remote server does understand /magic without invoking something
                        $serverret = Network::curl($basepath . '/magic');
@@ -1121,17 +1124,7 @@ class Profile
                $_SESSION['visitor_home'] = $visitor['url'];
                $_SESSION['my_url'] = $visitor['url'];
 
-               /// @todo replace this and the query for this variable with some cleaner functionality
-               $_SESSION['remote'] = [];
-
-               $remote_contacts = DBA::select('contact', ['id', 'uid'], ['nurl' => $visitor['nurl'], 'rel' => [Contact::FOLLOWER, Contact::FRIEND], 'self' => false]);
-               while ($contact = DBA::fetch($remote_contacts)) {
-                       if (($contact['uid'] == 0) || Contact::isBlockedByUser($visitor['id'], $contact['uid'])) {
-                               continue;
-                       }
-
-                       $_SESSION['remote'][$contact['uid']] = ['cid' => $contact['id'], 'uid' => $contact['uid']];
-               }
+               Session::setVisitorsContacts();
 
                $a->contact = $visitor;
 
@@ -1193,7 +1186,7 @@ class Profile
                if (!strlen($s)) {
                        return $s;
                }
-               if ((!strpos($s, '/profile/')) && (!$force)) {
+               if (!strpos($s, '/profile/') && !$force) {
                        return $s;
                }
                if ($force && substr($s, -1, 1) !== '/') {
@@ -1250,7 +1243,7 @@ class Profile
 
                if (!empty($search)) {
                        $searchTerm = '%' . $search . '%';
-                       $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` 
+                       $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total`
                                FROM `profile`
                                LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
                                WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed`
@@ -1272,7 +1265,7 @@ class Profile
                                $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm,
                                $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm, $searchTerm);
                } else {
-                       $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total` 
+                       $cnt = DBA::fetchFirst("SELECT COUNT(*) AS `total`
                                FROM `profile`
                                LEFT JOIN `user` ON `user`.`uid` = `profile`.`uid`
                                WHERE `is-default` $publish AND NOT `user`.`blocked` AND NOT `user`.`account_removed`");
index 36eaa17ac87254df049785655b73c809d5671bf4..7af9cb8dad2d56970cc96cd0990b433b4c72f9d1 100644 (file)
@@ -39,7 +39,7 @@ class Localtime extends BaseModule
                        $output .= '<p>' . L10n::t('Converted localtime: %s', $app->data['mod-localtime']) . '</p>';
                }
 
-               $output .= '<form action ="' . $app->getBaseURL() . '/localtime?f=&time=' . $time . '" method="post" >';
+               $output .= '<form action ="' . $app->getBaseURL() . '/localtime?time=' . $time . '" method="post" >';
                $output .= '<p>' . L10n::t('Please select your timezone:') . '</p>';
                $output .= Temporal::getTimezoneSelect(defaults($_REQUEST, 'timezone', Installer::DEFAULT_TZ));
                $output .= '<input type="submit" name="submit" value="' . L10n::t('Submit') . '" /></form>';
index 4674a9f8575548fd7f73ad3aa4a9b43e287de146..7d75e4d477aa0f7cfa4ea5b2601b1074122d433a 100644 (file)
@@ -8,6 +8,7 @@ use Friendica\Content\Pager;
 use Friendica\Content\Widget;
 use Friendica\Core\Hook;
 use Friendica\Core\L10n;
+use Friendica\Core\Session;
 use Friendica\Core\Renderer;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile;
@@ -25,8 +26,8 @@ class Directory extends BaseModule
                $app = self::getApp();
                $config = $app->getConfig();
 
-               if (($config->get('system', 'block_public') && !local_user() && !remote_user()) ||
-                       ($config->get('system', 'block_local_dir') && !local_user() && !remote_user())) {
+               if (($config->get('system', 'block_public') && !Session::isAuthenticated()) ||
+                       ($config->get('system', 'block_local_dir') && !Session::isAuthenticated())) {
                        throw new HTTPException\ForbiddenException(L10n::t('Public access denied.'));
                }
 
index f57cbadfd357516abe30977c63f3d6faf4a74484..a43e38045c8c611213a81ba7b240ebd2a98603c9 100644 (file)
@@ -4,6 +4,7 @@ namespace Friendica\Module;
 
 use Friendica\BaseModule;
 use Friendica\Model\Item;
+use Friendica\Core\Session;
 use Friendica\Network\HTTPException;
 use Friendica\Util\Strings;
 
@@ -14,7 +15,7 @@ class Like extends BaseModule
 {
        public static function rawContent()
        {
-               if (!local_user() && !remote_user()) {
+               if (!Session::isAuthenticated()) {
                        throw new HTTPException\ForbiddenException();
                }
 
index 1f60e6dc191ceda75990d76dc5878d7182841c29..bf6a39e19f961a55a38a64b2e9d8ee5a4ecaa77c 100644 (file)
@@ -7,6 +7,7 @@ namespace Friendica\Module;
 
 use Friendica\BaseModule;
 use Friendica\Core\Authentication;
+use Friendica\Core\Cache;
 use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\System;
@@ -27,6 +28,7 @@ class Logout extends BaseModule
                $visitor_home = null;
                if (remote_user()) {
                        $visitor_home = Profile::getMyURL();
+                       Cache::delete('zrlInit:' . $visitor_home);
                }
 
                Hook::callAll("logging_out");
index 4510d8d934f63d6f351e091d263e8bfec772b631..4cb3dc7328052df1f9fe89da9cb603b24c753267 100644 (file)
@@ -32,15 +32,16 @@ class Magic extends BaseModule
                $dest = defaults($_REQUEST, 'dest', '');
                $test = (!empty($_REQUEST['test']) ? intval($_REQUEST['test']) : 0);
                $owa  = (!empty($_REQUEST['owa'])  ? intval($_REQUEST['owa'])  : 0);
+               $cid  = 0;
 
                if (!empty($addr)) {
                        $cid = Contact::getIdForURL($addr);
-               } else {
+               } elseif (!empty($dest)) {
                        $cid = Contact::getIdForURL($dest);
                }
 
                if (!$cid) {
-                       Logger::log('No contact record found: ' . json_encode($_REQUEST), Logger::DEBUG);
+                       Logger::info('No contact record found', $_REQUEST);
                        // @TODO Finding a more elegant possibility to redirect to either internal or external URL
                        $a->redirect($dest);
                }
@@ -96,9 +97,9 @@ class Magic extends BaseModule
                                                } else {
                                                        $token = $j['token'];
                                                }
-                                               $x = strpbrk($dest, '?&');
-                                               $args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
+                                               $args = (strpbrk($dest, '?&') ? '&' : '?') . 'owt=' . $token;
 
+                                               Logger::info('Redirecting', ['path' => $dest . $args]);
                                                System::externalRedirect($dest . $args);
                                        }
                                }
index d103c614e67ebae35ebbcffabd89713421d65cdf..cb710b10ba97bee752cc13cd3dcf7aab7156603c 100644 (file)
@@ -48,8 +48,6 @@ class Profile extends BaseModule
                if (local_user() && $a->argc > 2 && $a->argv[2] === 'view') {
                        self::$which = $a->user['nickname'];
                        self::$profile = filter_var($a->argv[1], FILTER_SANITIZE_NUMBER_INT);
-               } else {
-                       DFRN::autoRedir($a, self::$which);
                }
        }
 
@@ -86,8 +84,8 @@ class Profile extends BaseModule
 
                        $a->page['htmlhead'] .= "\n";
 
-                       $blocked   = !local_user() && !remote_user() && Config::get('system', 'block_public');
-                       $userblock = !local_user() && !remote_user() && $a->profile['hidewall'];
+                       $blocked   = !local_user() && !Session::getRemoteContactID($a->profile['profile_uid']) && Config::get('system', 'block_public');
+                       $userblock = !local_user() && !Session::getRemoteContactID($a->profile['profile_uid']) && $a->profile['hidewall'];
 
                        if (!empty($a->profile['page-flags']) && $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY) {
                                $a->page['htmlhead'] .= '<meta name="friendica.community" content="true" />' . "\n";
@@ -153,13 +151,10 @@ class Profile extends BaseModule
 
                $hashtags = defaults($_GET, 'tag', '');
 
-               if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
+               if (Config::get('system', 'block_public') && !local_user() && !Session::getRemoteContactID($a->profile['profile_uid'])) {
                        return Login::form();
                }
 
-               $groups = [];
-               $remote_cid = null;
-
                $o = '';
 
                if ($update) {
@@ -169,17 +164,9 @@ class Profile extends BaseModule
                        Nav::setSelected('home');
                }
 
-               $remote_contact = ContactModel::isFollower(remote_user(), $a->profile['profile_uid']);
+               $remote_contact = Session::getRemoteContactID($a->profile['profile_uid']);
                $is_owner = local_user() == $a->profile['profile_uid'];
-               $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . remote_user();
-
-               if ($remote_contact) {
-                       $cdata = ContactModel::getPublicAndUserContacID(remote_user(), $a->profile['profile_uid']);
-                       if (!empty($cdata['user'])) {
-                               $groups = Group::getIdsByContactId($cdata['user']);
-                               $remote_cid = $cdata['user'];
-                       }
-               }
+               $last_updated_key = "profile:" . $a->profile['profile_uid'] . ":" . local_user() . ":" . $remote_contact;
 
                if (!empty($a->profile['hidewall']) && !$is_owner && !$remote_contact) {
                        notice(L10n::t('Access to this profile has been restricted.') . EOL);
@@ -229,7 +216,7 @@ class Profile extends BaseModule
                }
 
                // Get permissions SQL - if $remote_contact is true, our remote user has been pre-verified and we already have fetched his/her groups
-               $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid'], $remote_contact, $groups, $remote_cid);
+               $sql_extra = Item::getPermissionsSQLByUserId($a->profile['profile_uid']);
                $sql_extra2 = '';
 
                $last_updated_array = Session::get('last_updated', []);
index 7463d4043f8565d453dc6c2b2929a3fcb73bad5e..ed41f421b12a8dec7c81de7d4458bbd08e9e5f04 100644 (file)
@@ -10,6 +10,7 @@ use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
+use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Profile;
@@ -19,7 +20,7 @@ class Contacts extends BaseModule
 {
        public static function content()
        {
-               if (Config::get('system', 'block_public') && !local_user() && !remote_user()) {
+               if (Config::get('system', 'block_public') && !Session::isAuthenticated()) {
                        throw new \Friendica\Network\HTTPException\NotFoundException(L10n::t('User not found.'));
                }
 
index 36be9c4e6b8577cb90b5838cd62bcfa01f0aa11d..2ef53d075ce1c467770539a3a145a569ced16803 100644 (file)
@@ -14,8 +14,8 @@ use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\PConfig;
 use Friendica\Core\Protocol;
-use Friendica\Core\Renderer;
 use Friendica\Core\Session;
+use Friendica\Core\Renderer;
 use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Item;
@@ -71,13 +71,8 @@ class Post extends BaseObject
                $this->setTemplate('wall');
                $this->toplevel = $this->getId() == $this->getDataValue('parent');
 
-               if (!empty($_SESSION['remote']) && is_array($_SESSION['remote'])) {
-                       foreach ($_SESSION['remote'] as $visitor) {
-                               if ($visitor['cid'] == $this->getDataValue('contact-id')) {
-                                       $this->visiting = true;
-                                       break;
-                               }
-                       }
+               if (!empty(Session::getUserIDForVisitorContactID($this->getDataValue('contact-id')))) {
+                       $this->visiting = true;
                }
 
                $this->writable = $this->getDataValue('writable') || $this->getDataValue('self');
@@ -224,7 +219,7 @@ class Post extends BaseObject
                $author = ['uid' => 0, 'id' => $item['author-id'],
                        'network' => $item['author-network'], 'url' => $item['author-link']];
 
-               if (local_user() || remote_user()) {
+               if (Session::isAuthenticated()) {
                        $profile_link = Contact::magicLinkByContact($author);
                } else {
                        $profile_link = $item['author-link'];
index f55a80a6fddeb6995ff330bc25e21f84826c2c0d..7dee12b56ca498d0bdb61f9c177a0f774b59859c 100644 (file)
@@ -2849,115 +2849,6 @@ class DFRN
                return 200;
        }
 
-       /**
-        * @param App    $a            App
-        * @param string $contact_nick contact nickname
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       public static function autoRedir(App $a, $contact_nick)
-       {
-               // prevent looping
-               if (!empty($_REQUEST['redir'])) {
-                       Logger::log('autoRedir might be looping because redirect has been redirected', Logger::DEBUG);
-                       // looping prevention also appears to sometimes prevent authentication for images
-                       // because browser may have multiple connections open and load an image on a connection
-                       // whose session wasn't updated when a previous redirect authenticated
-                       // Leaving commented in case looping reappears
-                       //return;
-               }
-
-               if ((! $contact_nick) || ($contact_nick === $a->user['nickname'])) {
-                       return;
-               }
-
-               if (local_user()) {
-                       // We need to find out if $contact_nick is a user on this hub, and if so, if I
-                       // am a contact of that user. However, that user may have other contacts with the
-                       // same nickname as me on other hubs or other networks. Exclude these by requiring
-                       // that the contact have a local URL. I will be the only person with my nickname at
-                       // this URL, so if a result is found, then I am a contact of the $contact_nick user.
-                       //
-                       // We also have to make sure that I'm a legitimate contact--I'm not blocked or pending.
-
-                       $baseurl = System::baseUrl();
-                       $domain_st = strpos($baseurl, "://");
-                       if ($domain_st === false) {
-                               return;
-                       }
-                       $baseurl = substr($baseurl, $domain_st + 3);
-                       $nurl = Strings::normaliseLink($baseurl);
-
-                       $r = User::getByNickname($contact_nick, ["uid"]);
-                       $contact_uid = $r["uid"];
-
-                       /// @todo Why is there a query for "url" *and* "nurl"? Especially this normalising is strange.
-                       $r = q("SELECT `id` FROM `contact` WHERE `uid` = (SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1)
-                                       AND `nick` = '%s' AND NOT `self` AND (`url` LIKE '%%%s%%' OR `nurl` LIKE '%%%s%%') AND NOT `blocked` AND NOT `pending` LIMIT 1",
-                               DBA::escape($contact_nick),
-                               DBA::escape($a->user['nickname']),
-                               DBA::escape($baseurl),
-                               DBA::escape($nurl)
-                       );
-                       if ((! DBA::isResult($r))) {
-                               return;
-                       }
-                       // test if redirect authentication already succeeded
-                       // Note that "contact" in the sense used in the $contact_nick argument to this function
-                       // and the sense in the $remote[]["cid"] in the session are opposite.
-                       // In the session variable the user currently fetching is the contact
-                       // while $contact_nick is the nick of tho user who owns the stuff being fetched.
-                       foreach (Session::get('remote', []) as $visitor) {
-                               if ($visitor['uid'] == $contact_uid && $visitor['cid'] == $r[0]['id']) {
-                                       return;
-                               }
-                       }
-
-                       $r = q("SELECT * FROM contact WHERE nick = '%s'
-                                       AND network = '%s' AND uid = %d  AND url LIKE '%%%s%%' LIMIT 1",
-                               DBA::escape($contact_nick),
-                               DBA::escape(Protocol::DFRN),
-                               intval(local_user()),
-                               DBA::escape($baseurl)
-                       );
-                       if (! DBA::isResult($r)) {
-                               return;
-                       }
-
-                       $cid = $r[0]['id'];
-
-                       $dfrn_id = (($r[0]['issued-id']) ? $r[0]['issued-id'] : $r[0]['dfrn-id']);
-
-                       if ($r[0]['duplex'] && $r[0]['issued-id']) {
-                               $orig_id = $r[0]['issued-id'];
-                               $dfrn_id = '1:' . $orig_id;
-                       }
-                       if ($r[0]['duplex'] && $r[0]['dfrn-id']) {
-                               $orig_id = $r[0]['dfrn-id'];
-                               $dfrn_id = '0:' . $orig_id;
-                       }
-
-                       // ensure that we've got a valid ID. There may be some edge cases with forums and non-duplex mode
-                       // that may have triggered some of the "went to {profile/intro} and got an RSS feed" issues
-
-                       if (strlen($dfrn_id) < 3) {
-                               return;
-                       }
-
-                       $sec = Strings::getRandomHex();
-
-                       DBA::insert('profile_check', ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id, 'sec' => $sec, 'expire' => time() + 45]);
-
-                       $url = curPageURL();
-
-                       Logger::log('auto_redir: ' . $r[0]['name'] . ' ' . $sec, Logger::DEBUG);
-                       $dest = (($url) ? '&destination_url=' . $url : '');
-                       System::externalRedirect($r[0]['poll'] . '?dfrn_id=' . $dfrn_id
-                               . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest);
-               }
-
-               return;
-       }
-
        /**
         * @brief Returns the activity verb
         *
index 9e074e8a9a1c3a0b820938151fe7d18ac9ce6dae..587b34c3fe3b4839927e52a536fb09042f39ef40 100644 (file)
@@ -1608,7 +1608,7 @@ class Diaspora
                }
 
                if (self::isRedmatrix($contact["url"])) {
-                       return $contact["url"] . "/?f=&mid=" . $guid;
+                       return $contact["url"] . "/?mid=" . $guid;
                }
 
                if ($parent_guid != '') {
index 0c09b745d8ea1f862de7e153eae1b24816a1cc62..043c59d84f20a0fd1ed1023dd48dcd1ff9829017 100644 (file)
@@ -10,6 +10,7 @@ use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\Group;
 use Friendica\Model\User;
+use Friendica\Core\Session;
 
 /**
  * Secures that User is allow to do requests
@@ -20,7 +21,7 @@ class Security extends BaseObject
        {
                static $verified = 0;
 
-               if (!local_user() && !remote_user()) {
+               if (!Session::isAuthenticated()) {
                        return false;
                }
 
@@ -33,7 +34,7 @@ class Security extends BaseObject
                        return true;
                }
 
-               if (remote_user($owner)) {
+               if (!empty(Session::getRemoteContactID($owner))) {
                        // use remembered decision and avoid a DB lookup for each and every display item
                        // DO NOT use this function if there are going to be multiple owners
                        // We have a contact-id for an authenticated remote user, this block determines if the contact
@@ -44,14 +45,14 @@ class Security extends BaseObject
                        } elseif ($verified === 1) {
                                return false;
                        } else {
-                               $cid = remote_user($owner);
+                               $cid = Session::getRemoteContactID($owner);
                                if (!$cid) {
                                        return false;
                                }
 
                                $r = q("SELECT `contact`.*, `user`.`page-flags` FROM `contact` INNER JOIN `user` on `user`.`uid` = `contact`.`uid`
                                        WHERE `contact`.`uid` = %d AND `contact`.`id` = %d AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
-                                       AND `user`.`blockwall` = 0 AND `readonly` = 0  AND ( `contact`.`rel` IN ( %d , %d ) OR `user`.`page-flags` = %d ) LIMIT 1",
+                                       AND `user`.`blockwall` = 0 AND `readonly` = 0  AND (`contact`.`rel` IN (%d , %d) OR `user`.`page-flags` = %d) LIMIT 1",
                                        intval($owner),
                                        intval($cid),
                                        intval(Contact::SHARING),
@@ -71,11 +72,10 @@ class Security extends BaseObject
                return false;
        }
 
-       /// @TODO $groups should be array
-       public static function getPermissionsSQLByUserId($owner_id, $remote_verified = false, $groups = null)
+       public static function getPermissionsSQLByUserId($owner_id)
        {
                $local_user = local_user();
-               $remote_user = remote_user();
+               $remote_contact = Session::getRemoteContactID($owner_id);
 
                /*
                 * Construct permissions
@@ -83,10 +83,9 @@ class Security extends BaseObject
                 * default permissions - anonymous user
                 */
                $sql = " AND allow_cid = ''
-                                AND allow_gid = ''
-                                AND deny_cid  = ''
-                                AND deny_gid  = ''
-               ";
+                        AND allow_gid = ''
+                        AND deny_cid  = ''
+                        AND deny_gid  = '' ";
 
                /*
                 * Profile owner - everything is visible
@@ -94,59 +93,28 @@ class Security extends BaseObject
                if ($local_user && $local_user == $owner_id) {
                        $sql = '';
                /*
-                * Authenticated visitor. Unless pre-verified,
-                * check that the contact belongs to this $owner_id
-                * and load the groups the visitor belongs to.
-                * If pre-verified, the caller is expected to have already
-                * done this and passed the groups into this function.
+                * Authenticated visitor. Load the groups the visitor belongs to.
                 */
-               } elseif ($remote_user) {
-                       /*
-                        * Authenticated visitor. Unless pre-verified,
-                        * check that the contact belongs to this $owner_id
-                        * and load the groups the visitor belongs to.
-                        * If pre-verified, the caller is expected to have already
-                        * done this and passed the groups into this function.
-                        */
-
-                       if (!$remote_verified) {
-                               $cid = 0;
-
-                               foreach (\Friendica\Core\Session::get('remote', []) as $visitor) {
-                                       if ($visitor['uid'] == $owner_id) {
-                                               $cid = $visitor['cid'];
-                                               break;
-                                       }
-                               }
+               } elseif ($remote_contact) {
+                       $gs = '<<>>'; // should be impossible to match
 
-                               if ($cid && DBA::exists('contact', ['id' => $cid, 'uid' => $owner_id, 'blocked' => false])) {
-                                       $remote_verified = true;
-                                       $groups = Group::getIdsByContactId($cid);
-                               }
-                       }
+                       $groups = Group::getIdsByContactId($remote_contact);
 
-                       if ($remote_verified) {
-                               $gs = '<<>>'; // should be impossible to match
-
-                               if (is_array($groups)) {
-                                       foreach ($groups as $g) {
-                                               $gs .= '|<' . intval($g) . '>';
-                                       }
+                       if (is_array($groups)) {
+                               foreach ($groups as $g) {
+                                       $gs .= '|<' . intval($g) . '>';
                                }
-
-                               $sql = sprintf(
-                                       " AND ( NOT (deny_cid REGEXP '<%d>' OR deny_gid REGEXP '%s')
-                                         AND ( allow_cid REGEXP '<%d>' OR allow_gid REGEXP '%s' OR ( allow_cid = '' AND allow_gid = '') )
-                                         )
-                                       ",
-                                       intval($cid),
-                                       DBA::escape($gs),
-                                       intval($cid),
-                                       DBA::escape($gs)
-                               );
                        }
+
+                       $sql = sprintf(
+                               " AND (NOT (deny_cid REGEXP '<%d>' OR deny_gid REGEXP '%s')
+                                 AND (allow_cid REGEXP '<%d>' OR allow_gid REGEXP '%s' OR (allow_cid = '' AND allow_gid = ''))) ",
+                               intval($remote_contact),
+                               DBA::escape($gs),
+                               intval($remote_contact),
+                               DBA::escape($gs)
+                       );
                }
                return $sql;
        }
-
 }
index e4ea1122f07adcca81b2e872809f942e1f5a211c..128ecc88c79ecb98cd7f533e374c9eeb5f72da33 100644 (file)
@@ -80,7 +80,7 @@ class StaticDatabase extends Database
        {
                // Use environment variables for mysql if they are set beforehand
                if (!empty($server['MYSQL_HOST'])
-                   && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
+                   && (!empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER'])))
                    && $server['MYSQL_PASSWORD'] !== false
                    && !empty($server['MYSQL_DATABASE']))
                {
index 565e693c95e322d5502d794acba7341637097de2..ecf0880d29b68729da0ec46e41003521639e3165 100644 (file)
@@ -23,6 +23,7 @@ trait VFSTrait
                        'bin' => [],
                        'static' => [],
                        'test' => [],
+                       'logs' => [],
                ];
 
                // create a virtual directory and copy all needed files and folders to it
diff --git a/tests/phpunit.xml b/tests/phpunit.xml
new file mode 100644 (file)
index 0000000..73b643e
--- /dev/null
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<phpunit
+       bootstrap="bootstrap.php"
+       verbose="true">
+       <testsuite name='friendica'>
+               <directory suffix='.php'>functional/</directory>
+               <directory suffix='.php'>include/</directory>
+               <directory suffix='.php'>src/</directory>
+               <directory suffix='.php'>./</directory>
+       </testsuite>
+       <!-- Filters for Code Coverage -->
+       <filter>
+               <whitelist>
+                       <directory suffix=".php">..</directory>
+                       <exclude>
+                               <directory suffix=".php">config/</directory>
+                               <directory suffix=".php">doc/</directory>
+                               <directory suffix=".php">images/</directory>
+                               <directory suffix=".php">library/</directory>
+                               <directory suffix=".php">spec/</directory>
+                               <directory suffix=".php">tests/</directory>
+                               <directory suffix=".php">view/</directory>
+                       </exclude>
+               </whitelist>
+       </filter>
+       <listeners>
+               <listener class="JohnKary\PHPUnit\Listener\SpeedTrapListener" />
+       </listeners>
+</phpunit>
index 1b90be574ec2167b7d403ea12dbebcfcbb6cb892..dfb81d9c6ce57a5707a8614cb2002f42b1df78f9 100644 (file)
@@ -4,6 +4,9 @@ namespace Friendica\Test\src\Core\Cache;
 
 use Friendica\Core\Cache\APCuCache;
 
+/**
+ * @group APCU
+ */
 class APCuCacheTest extends MemoryCacheTest
 {
        protected function setUp()
index ccc37231532414c7fc7c1bb73d13c544ccd3e3b9..2865effb17ad9c9cfb9acf6f9b814414be1c5a54 100644 (file)
@@ -7,6 +7,7 @@ use Friendica\Core\Config\Configuration;
 
 /**
  * @requires extension memcache
+ * @group MEMCACHE
  */
 class MemcacheCacheTest extends MemoryCacheTest
 {
@@ -14,16 +15,22 @@ class MemcacheCacheTest extends MemoryCacheTest
        {
                $configMock = \Mockery::mock(Configuration::class);
 
+               $host = $_SERVER['MEMCACHE_HOST'] ?? 'localhost';
+
                $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcache_host')
-                       ->andReturn('localhost');
+                       ->andReturn($host);
                $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcache_port')
                        ->andReturn(11211);
 
-               $this->cache = new MemcacheCache('localhost', $configMock);
+               try {
+                       $this->cache = new MemcacheCache($host, $configMock);
+               } catch (\Exception $e) {
+                       $this->markTestSkipped('Memcache is not available');
+               }
                return $this->cache;
        }
 
@@ -32,4 +39,14 @@ class MemcacheCacheTest extends MemoryCacheTest
                $this->cache->clear(false);
                parent::tearDown();
        }
+
+       /**
+        * @small
+        *
+        * @dataProvider dataSimple
+        */
+       public function testGetAllKeys($value1, $value2, $value3)
+       {
+               $this->markTestIncomplete('Race condition because of too fast getAllKeys() which uses a workaround');
+       }
 }
index d887250197386a1c1ad69e03464bc49e59ddc590..c9eb02be6e74c94ae68b2b8303989fa5f6304a63 100644 (file)
@@ -9,6 +9,7 @@ use Psr\Log\NullLogger;
 
 /**
  * @requires extension memcached
+ * @group MEMCACHED
  */
 class MemcachedCacheTest extends MemoryCacheTest
 {
@@ -16,14 +17,20 @@ class MemcachedCacheTest extends MemoryCacheTest
        {
                $configMock = \Mockery::mock(Configuration::class);
 
+               $host = $_SERVER['MEMCACHED_HOST'] ?? 'localhost';
+
                $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcached_hosts')
-                       ->andReturn([0 => 'localhost, 11211']);
+                       ->andReturn([0 => $host . ', 11211']);
 
                $logger = new NullLogger();
 
-               $this->cache = new MemcachedCache('localhost', $configMock, $logger);
+               try {
+                       $this->cache = new MemcachedCache($host, $configMock, $logger);
+               } catch (\Exception $exception) {
+                       $this->markTestSkipped('Memcached is not available');
+               }
                return $this->cache;
        }
 
@@ -32,4 +39,14 @@ class MemcachedCacheTest extends MemoryCacheTest
                $this->cache->clear(false);
                parent::tearDown();
        }
+
+       /**
+        * @small
+        *
+        * @dataProvider dataSimple
+        */
+       public function testGetAllKeys($value1, $value2, $value3)
+       {
+               $this->markTestIncomplete('Race condition because of too fast getAllKeys() which uses a workaround');
+       }
 }
index df353252df5dc4875d3a15f864a7f6971bdb12a7..75891cd1b8f1d7514694d48b5fd0147d92292cde 100644 (file)
@@ -8,6 +8,7 @@ use Friendica\Core\Config\Configuration;
 
 /**
  * @requires extension redis
+ * @group REDIS
  */
 class RedisCacheTest extends MemoryCacheTest
 {
@@ -15,10 +16,12 @@ class RedisCacheTest extends MemoryCacheTest
        {
                $configMock = \Mockery::mock(Configuration::class);
 
+               $host = $_SERVER['REDIS_HOST'] ?? 'localhost';
+
                $configMock
                        ->shouldReceive('get')
                        ->with('system', 'redis_host')
-                       ->andReturn('localhost');
+                       ->andReturn($host);
                $configMock
                        ->shouldReceive('get')
                        ->with('system', 'redis_port')
@@ -33,7 +36,11 @@ class RedisCacheTest extends MemoryCacheTest
                        ->with('system', 'redis_password')
                        ->andReturn(null);
 
-               $this->cache = new RedisCache('localhost', $configMock);
+               try {
+                       $this->cache = new RedisCache($host, $configMock);
+               } catch (\Exception $e) {
+                       $this->markTestSkipped('Redis is not available.');
+               }
                return $this->cache;
        }
 
index 3fbb3605a195eac0c2e2ea86a72cc03678ab9a56..c2437178108ff0cae6ea8b4a271b9486d6c6013b 100644 (file)
@@ -5,6 +5,9 @@ namespace Friendica\Test\src\Core\Lock;
 use Friendica\Core\Cache\APCuCache;
 use Friendica\Core\Lock\CacheLock;
 
+/**
+ * @group APCU
+ */
 class APCuCacheLockTest extends LockTest
 {
        protected function setUp()
index f550ac51a6949554dbe6f17f083d66025005256b..e66c4725c17be7f87a9d691915db0a8beceb4133 100644 (file)
@@ -9,6 +9,7 @@ use Friendica\Core\Lock\CacheLock;
 
 /**
  * @requires extension Memcache
+ * @group MEMCACHE
  */
 class MemcacheCacheLockTest extends LockTest
 {
@@ -16,15 +17,42 @@ class MemcacheCacheLockTest extends LockTest
        {
                $configMock = \Mockery::mock(Configuration::class);
 
+               $host = $_SERVER['MEMCACHE_HOST'] ?? 'localhost';
+
                $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcache_host')
-                       ->andReturn('localhost');
+                       ->andReturn($host);
                $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcache_port')
                        ->andReturn(11211);
 
-               return new CacheLock(new MemcacheCache('localhost', $configMock));
+               $lock = null;
+
+               try {
+                       $cache = new MemcacheCache($host, $configMock);
+                       $lock = new CacheLock($cache);
+               } catch (\Exception $e) {
+                       $this->markTestSkipped('Memcache is not available');
+               }
+
+               return $lock;
+       }
+
+       /**
+        * @small
+        */
+       public function testGetLocks()
+       {
+               $this->markTestIncomplete('Race condition because of too fast getAllKeys() which uses a workaround');
+       }
+
+       /**
+        * @small
+        */
+       public function testGetLocksWithPrefix()
+       {
+               $this->markTestIncomplete('Race condition because of too fast getAllKeys() which uses a workaround');
        }
 }
index 8b59f91bb79a14b09f2670cc4fb3b4d71ddd47c7..c217b47f59a8673aa7f755e7edecb8fea384603f 100644 (file)
@@ -10,6 +10,7 @@ use Psr\Log\NullLogger;
 
 /**
  * @requires extension memcached
+ * @group MEMCACHED
  */
 class MemcachedCacheLockTest extends LockTest
 {
@@ -17,13 +18,34 @@ class MemcachedCacheLockTest extends LockTest
        {
                $configMock = \Mockery::mock(Configuration::class);
 
+               $host = $_SERVER['MEMCACHED_HOST'] ?? 'localhost';
+
                $configMock
                        ->shouldReceive('get')
                        ->with('system', 'memcached_hosts')
-                       ->andReturn([0 => 'localhost, 11211']);
+                       ->andReturn([0 => $host . ', 11211']);
 
                $logger = new NullLogger();
 
-               return new CacheLock(new MemcachedCache('localhost', $configMock, $logger));
+               $lock = null;
+
+               try {
+                       $cache = new MemcachedCache($host, $configMock, $logger);
+                       $lock = new CacheLock($cache);
+               } catch (\Exception $e) {
+                       $this->markTestSkipped('Memcached is not available');
+               }
+
+               return $lock;
+       }
+
+       public function testGetLocks()
+       {
+               $this->markTestIncomplete('Race condition because of too fast getLocks() which uses a workaround');
+       }
+
+       public function testGetLocksWithPrefix()
+       {
+               $this->markTestIncomplete('Race condition because of too fast getLocks() which uses a workaround');
        }
 }
index 0ebc02160e6a6915055ef34655f9460d5f5cf8ff..95f7206e2c79d73367c2f12bd9b8da98c2b54462 100644 (file)
@@ -9,6 +9,7 @@ use Friendica\Core\Lock\CacheLock;
 
 /**
  * @requires extension redis
+ * @group REDIS
  */
 class RedisCacheLockTest extends LockTest
 {
@@ -16,10 +17,12 @@ class RedisCacheLockTest extends LockTest
        {
                $configMock = \Mockery::mock(Configuration::class);
 
+               $host = $_SERVER['REDIS_HOST'] ?? 'localhost';
+
                $configMock
                        ->shouldReceive('get')
                        ->with('system', 'redis_host')
-                       ->andReturn('localhost');
+                       ->andReturn($host);
                $configMock
                        ->shouldReceive('get')
                        ->with('system', 'redis_port')
@@ -34,6 +37,15 @@ class RedisCacheLockTest extends LockTest
                        ->with('system', 'redis_password')
                        ->andReturn(null);
 
-               return new CacheLock(new RedisCache('localhost', $configMock));
+               $lock = null;
+
+               try {
+                       $cache = new RedisCache($host, $configMock);
+                       $lock = new CacheLock($cache);
+               } catch (\Exception $e) {
+                       $this->markTestSkipped('Redis is not available');
+               }
+
+               return $lock;
        }
 }
index bbf94419a6b6b970ec5703a566c41e3cf237d7f1..d42ba1d914c2cfd36344de72f4bc3b1ecfbbddbc 100644 (file)
@@ -121,7 +121,9 @@ class StreamLoggerTest extends AbstractLoggerTest
         */
        public function testWrongDir()
        {
-               $logger = new StreamLogger('test', '/a/wrong/directory/file.txt', $this->introspection);
+               $this->markTestIncomplete('We need a platform independent way to set directory to readonly');
+
+               $logger = new StreamLogger('test', '/$%/wrong/directory/file.txt', $this->introspection);
 
                $logger->emergency('not working');
        }