First commit

This commit is contained in:
Theodotos Andreou 2018-01-14 13:10:16 +00:00
commit c6e2478c40
13918 changed files with 2303184 additions and 0 deletions

View file

@ -0,0 +1,168 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
namespace Civi\API\Provider;
use Civi\API\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* An adhoc provider is useful for creating mock API implementations.
*/
class AdhocProvider implements EventSubscriberInterface, ProviderInterface {
/**
* @return array
*/
public static function getSubscribedEvents() {
// Using a high priority allows adhoc implementations
// to override standard implementations -- which is
// handy for testing/mocking.
return array(
Events::RESOLVE => array(
array('onApiResolve', Events::W_EARLY),
),
Events::AUTHORIZE => array(
array('onApiAuthorize', Events::W_EARLY),
),
);
}
/**
* @var array (string $name => array('perm' => string, 'callback' => callable))
*/
protected $actions = array();
/**
* @var string
*/
protected $entity;
/**
* @var int
*/
protected $version;
/**
* @param int $version
* API version.
* @param string $entity
* API entity.
*/
public function __construct($version, $entity) {
$this->entity = $entity;
$this->version = $version;
}
/**
* Register a new API.
*
* @param string $name
* Action name.
* @param string $perm
* Permissions required for invoking the action.
* @param mixed $callback
* The function which executes the API.
* @return AdhocProvider
*/
public function addAction($name, $perm, $callback) {
$this->actions[strtolower($name)] = array(
'perm' => $perm,
'callback' => $callback,
);
return $this;
}
/**
* @param \Civi\API\Event\ResolveEvent $event
* API resolution event.
*/
public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
$apiRequest = $event->getApiRequest();
if ($this->matchesRequest($apiRequest)) {
$event->setApiRequest($apiRequest);
$event->setApiProvider($this);
$event->stopPropagation();
}
}
/**
* @param \Civi\API\Event\AuthorizeEvent $event
* API authorization event.
*/
public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
$apiRequest = $event->getApiRequest();
if ($this->matchesRequest($apiRequest) && \CRM_Core_Permission::check($this->actions[strtolower($apiRequest['action'])]['perm'])) {
$event->authorize();
$event->stopPropagation();
}
}
/**
* @inheritDoc
* @param array $apiRequest
* @return array|mixed
*/
public function invoke($apiRequest) {
return call_user_func($this->actions[strtolower($apiRequest['action'])]['callback'], $apiRequest);
}
/**
* @inheritDoc
* @param int $version
* @return array
*/
public function getEntityNames($version) {
return array($this->entity);
}
/**
* @inheritDoc
* @param int $version
* @param string $entity
* @return array
*/
public function getActionNames($version, $entity) {
if ($version == $this->version && $entity == $this->entity) {
return array_keys($this->actions);
}
else {
return array();
}
}
/**
* @param array $apiRequest
* The full description of the API request.
*
* @return bool
*/
public function matchesRequest($apiRequest) {
return $apiRequest['entity'] == $this->entity && $apiRequest['version'] == $this->version && isset($this->actions[strtolower($apiRequest['action'])]);
}
}

View file

@ -0,0 +1,304 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
namespace Civi\API\Provider;
use Civi\API\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* This class manages the loading of API's using strict file+function naming
* conventions.
*/
class MagicFunctionProvider implements EventSubscriberInterface, ProviderInterface {
/**
* @return array
*/
public static function getSubscribedEvents() {
return array(
Events::RESOLVE => array(
array('onApiResolve', Events::W_MIDDLE),
),
);
}
/**
* @var array (string $cachekey => array('function' => string, 'is_generic' => bool))
*/
private $cache;
/**
*/
public function __construct() {
$this->cache = array();
}
/**
* @param \Civi\API\Event\ResolveEvent $event
* API resolution event.
*/
public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
$apiRequest = $event->getApiRequest();
$resolved = $this->resolve($apiRequest);
if ($resolved['function']) {
$apiRequest += $resolved;
$event->setApiRequest($apiRequest);
$event->setApiProvider($this);
$event->stopPropagation();
}
}
/**
* @inheritDoc
* @param array $apiRequest
* @return array
*/
public function invoke($apiRequest) {
$function = $apiRequest['function'];
if ($apiRequest['function'] && $apiRequest['is_generic']) {
// Unlike normal API implementations, generic implementations require explicit
// knowledge of the entity and action (as well as $params). Bundle up these bits
// into a convenient data structure.
$result = $function($apiRequest);
}
elseif ($apiRequest['function'] && !$apiRequest['is_generic']) {
$result = $function($apiRequest['params']);
}
return $result;
}
/**
* @inheritDoc
* @param int $version
* @return array
*/
public function getEntityNames($version) {
$entities = array();
$include_dirs = array_unique(explode(PATH_SEPARATOR, get_include_path()));
#$include_dirs = array(dirname(__FILE__). '/../../');
foreach ($include_dirs as $include_dir) {
$api_dir = implode(DIRECTORY_SEPARATOR,
array($include_dir, 'api', 'v' . $version));
if (!is_dir($api_dir)) {
continue;
}
$iterator = new \DirectoryIterator($api_dir);
foreach ($iterator as $fileinfo) {
$file = $fileinfo->getFilename();
// Check for entities with a master file ("api/v3/MyEntity.php")
$parts = explode(".", $file);
if (end($parts) == "php" && $file != "utils.php" && !preg_match('/Tests?.php$/', $file)) {
// without the ".php"
$entities[] = substr($file, 0, -4);
}
// Check for entities with standalone action files (eg "api/v3/MyEntity/MyAction.php").
$action_dir = $api_dir . DIRECTORY_SEPARATOR . $file;
if (preg_match('/^[A-Z][A-Za-z0-9]*$/', $file) && is_dir($action_dir)) {
if (count(glob("$action_dir/[A-Z]*.php")) > 0) {
$entities[] = $file;
}
}
}
}
$entities = array_diff($entities, array('Generic'));
$entities = array_unique($entities);
sort($entities);
return $entities;
}
/**
* @inheritDoc
* @param int $version
* @param string $entity
* @return array
*/
public function getActionNames($version, $entity) {
$entity = _civicrm_api_get_camel_name($entity);
$entities = $this->getEntityNames($version);
if (!in_array($entity, $entities)) {
return array();
}
$this->loadEntity($entity, $version);
$functions = get_defined_functions();
$actions = array();
$prefix = 'civicrm_api' . $version . '_' . _civicrm_api_get_entity_name_from_camel($entity) . '_';
$prefixGeneric = 'civicrm_api' . $version . '_generic_';
foreach ($functions['user'] as $fct) {
if (strpos($fct, $prefix) === 0) {
$actions[] = substr($fct, strlen($prefix));
}
elseif (strpos($fct, $prefixGeneric) === 0) {
$actions[] = substr($fct, strlen($prefixGeneric));
}
}
return $actions;
}
/**
* Look up the implementation for a given API request.
*
* @param array $apiRequest
* Array with keys:
* - entity: string, required.
* - action: string, required.
* - params: array.
* - version: scalar, required.
*
* @return array
* Array with keys:
* - function: callback (mixed)
* - is_generic: boolean
*/
protected function resolve($apiRequest) {
$cachekey = strtolower($apiRequest['entity']) . ':' . strtolower($apiRequest['action']) . ':' . $apiRequest['version'];
if (isset($this->cache[$cachekey])) {
return $this->cache[$cachekey];
}
$camelName = _civicrm_api_get_camel_name($apiRequest['entity'], $apiRequest['version']);
$actionCamelName = _civicrm_api_get_camel_name($apiRequest['action']);
// Determine if there is an entity-specific implementation of the action
$stdFunction = $this->getFunctionName($apiRequest['entity'], $apiRequest['action'], $apiRequest['version']);
if (function_exists($stdFunction)) {
// someone already loaded the appropriate file
// FIXME: This has the affect of masking bugs in load order; this is
// included to provide bug-compatibility.
$this->cache[$cachekey] = array('function' => $stdFunction, 'is_generic' => FALSE);
return $this->cache[$cachekey];
}
$stdFiles = array(
// By convention, the $camelName.php is more likely to contain the
// function, so test it first
'api/v' . $apiRequest['version'] . '/' . $camelName . '.php',
'api/v' . $apiRequest['version'] . '/' . $camelName . '/' . $actionCamelName . '.php',
);
foreach ($stdFiles as $stdFile) {
if (\CRM_Utils_File::isIncludable($stdFile)) {
require_once $stdFile;
if (function_exists($stdFunction)) {
$this->cache[$cachekey] = array('function' => $stdFunction, 'is_generic' => FALSE);
return $this->cache[$cachekey];
}
}
}
// Determine if there is a generic implementation of the action
require_once 'api/v3/Generic.php';
# $genericFunction = 'civicrm_api3_generic_' . $apiRequest['action'];
$genericFunction = $this->getFunctionName('generic', $apiRequest['action'], $apiRequest['version']);
$genericFiles = array(
// By convention, the Generic.php is more likely to contain the
// function, so test it first
'api/v' . $apiRequest['version'] . '/Generic.php',
'api/v' . $apiRequest['version'] . '/Generic/' . $actionCamelName . '.php',
);
foreach ($genericFiles as $genericFile) {
if (\CRM_Utils_File::isIncludable($genericFile)) {
require_once $genericFile;
if (function_exists($genericFunction)) {
$this->cache[$cachekey] = array('function' => $genericFunction, 'is_generic' => TRUE);
return $this->cache[$cachekey];
}
}
}
$this->cache[$cachekey] = array('function' => FALSE, 'is_generic' => FALSE);
return $this->cache[$cachekey];
}
/**
* Determine the function name for a given API request.
*
* @param string $entity
* API entity name.
* @param string $action
* API action name.
* @param int $version
* API version.
*
* @return string
*/
protected function getFunctionName($entity, $action, $version) {
$entity = _civicrm_api_get_entity_name_from_camel($entity);
return 'civicrm_api' . $version . '_' . $entity . '_' . $action;
}
/**
* Load/require all files related to an entity.
*
* This should not normally be called because it's does a file-system scan; it's
* only appropriate when introspection is really required (eg for "getActions").
*
* @param string $entity
* API entity name.
* @param int $version
* API version.
*/
protected function loadEntity($entity, $version) {
$camelName = _civicrm_api_get_camel_name($entity, $version);
// Check for master entity file; to match _civicrm_api_resolve(), only load the first one
$stdFile = 'api/v' . $version . '/' . $camelName . '.php';
if (\CRM_Utils_File::isIncludable($stdFile)) {
require_once $stdFile;
}
// Check for standalone action files; to match _civicrm_api_resolve(), only load the first one
$loaded_files = array(); // array($relativeFilePath => TRUE)
$include_dirs = array_unique(explode(PATH_SEPARATOR, get_include_path()));
foreach ($include_dirs as $include_dir) {
foreach (array($camelName, 'Generic') as $name) {
$action_dir = implode(DIRECTORY_SEPARATOR,
array($include_dir, 'api', "v${version}", $name));
if (!is_dir($action_dir)) {
continue;
}
$iterator = new \DirectoryIterator($action_dir);
foreach ($iterator as $fileinfo) {
$file = $fileinfo->getFilename();
if (array_key_exists($file, $loaded_files)) {
continue; // action provided by an earlier item on include_path
}
$parts = explode(".", $file);
if (end($parts) == "php" && !preg_match('/Tests?\.php$/', $file)) {
require_once $action_dir . DIRECTORY_SEPARATOR . $file;
$loaded_files[$file] = TRUE;
}
}
}
}
}
}

View file

@ -0,0 +1,63 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
namespace Civi\API\Provider;
use Civi\API\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* An API "provider" provides a means to execute API requests.
*/
interface ProviderInterface {
/**
* @param array $apiRequest
* The full description of the API request.
* @return array
* structured response data (per civicrm_api3_create_success)
* @see civicrm_api3_create_success
* @throws \API_Exception
*/
public function invoke($apiRequest);
/**
* @param int $version
* API version.
* @return array<string>
*/
public function getEntityNames($version);
/**
* @param int $version
* API version.
* @param string $entity
* API entity.
* @return array<string>
*/
public function getActionNames($version, $entity);
}

View file

@ -0,0 +1,150 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
namespace Civi\API\Provider;
use Civi\API\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* This class defines operations for inspecting the API's metadata.
*/
class ReflectionProvider implements EventSubscriberInterface, ProviderInterface {
/**
* @return array
*/
public static function getSubscribedEvents() {
return array(
Events::RESOLVE => array(
// TODO decide if we really want to override others
array('onApiResolve', Events::W_EARLY),
),
Events::AUTHORIZE => array(
// TODO decide if we really want to override others
array('onApiAuthorize', Events::W_EARLY),
),
);
}
/**
* @var \Civi\API\Kernel
*/
private $apiKernel;
/**
* @var array (string $entityName => array(string $actionName))
*/
private $actions;
/**
* @param \Civi\API\Kernel $apiKernel
* The API kernel.
*/
public function __construct($apiKernel) {
$this->apiKernel = $apiKernel;
$this->actions = array(
'Entity' => array('get', 'getactions'),
'*' => array('getactions'), // 'getfields'
);
}
/**
* @param \Civi\API\Event\ResolveEvent $event
* API resolution event.
*/
public function onApiResolve(\Civi\API\Event\ResolveEvent $event) {
$apiRequest = $event->getApiRequest();
$actions = $this->getActionNames($apiRequest['version'], $apiRequest['entity']);
if (in_array($apiRequest['action'], $actions)) {
$apiRequest['is_metadata'] = TRUE;
$event->setApiRequest($apiRequest);
$event->setApiProvider($this);
$event->stopPropagation();
// TODO decide if we really want to override others
}
}
/**
* @param \Civi\API\Event\AuthorizeEvent $event
* API authorization event.
*/
public function onApiAuthorize(\Civi\API\Event\AuthorizeEvent $event) {
$apiRequest = $event->getApiRequest();
if (isset($apiRequest['is_metadata'])) {
// if (\CRM_Core_Permission::check('access AJAX API')
// || \CRM_Core_Permission::check('access CiviCRM')) {
$event->authorize();
$event->stopPropagation();
// }
}
}
/**
* @inheritDoc
* @param array $apiRequest
* @return array
* @throws \API_Exception
*/
public function invoke($apiRequest) {
if (strtolower($apiRequest['entity']) == 'entity' && $apiRequest['action'] == 'get') {
return civicrm_api3_create_success($this->apiKernel->getEntityNames($apiRequest['version']), $apiRequest['params'], 'entity', 'get');
}
switch ($apiRequest['action']) {
case 'getactions':
return civicrm_api3_create_success($this->apiKernel->getActionNames($apiRequest['version'], $apiRequest['entity']), $apiRequest['params'], $apiRequest['entity'], $apiRequest['action']);
//case 'getfields':
// return $this->doGetFields($apiRequest);
default:
}
// We shouldn't get here because onApiResolve() checks $this->actions
throw new \API_Exception("Unsupported action (" . $apiRequest['entity'] . '.' . $apiRequest['action'] . ']');
}
/**
* @inheritDoc
* @param int $version
* @return array
*/
public function getEntityNames($version) {
return array('Entity');
}
/**
* @inheritDoc
* @param int $version
* @param string $entity
* @return array
*/
public function getActionNames($version, $entity) {
$entity = _civicrm_api_get_camel_name($entity, $version);
return isset($this->actions[$entity]) ? $this->actions[$entity] : $this->actions['*'];
}
}

View file

@ -0,0 +1,156 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
namespace Civi\API\Provider;
use Civi\API\Events;
/**
* A static provider is useful for creating mock API implementations which
* manages records in-memory.
*
* TODO Add a static provider to SyntaxConformanceTest to ensure that it's
* representative.
*/
class StaticProvider extends AdhocProvider {
protected $records;
protected $fields;
/**
* @return array
*/
public static function getSubscribedEvents() {
return array(
Events::RESOLVE => array(
array('onApiResolve', Events::W_MIDDLE),
),
Events::AUTHORIZE => array(
array('onApiAuthorize', Events::W_MIDDLE),
),
);
}
/**
* @param int $version
* API version.
* @param string $entity
* API entity.
* @param array $fields
* List of fields in this fake entity.
* @param array $perms
* Array(string $action => string $perm).
* @param array $records
* List of mock records to be read/updated by API calls.
*/
public function __construct($version, $entity, $fields, $perms = array(), $records = array()) {
parent::__construct($version, $entity);
$perms = array_merge(array(
'create' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
'get' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
'delete' => \CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION,
), $perms);
$this->records = \CRM_Utils_Array::index(array('id'), $records);
$this->fields = $fields;
$this->addAction('create', $perms['create'], array($this, 'doCreate'));
$this->addAction('get', $perms['get'], array($this, 'doGet'));
$this->addAction('delete', $perms['delete'], array($this, 'doDelete'));
}
/**
* @return array
*/
public function getRecords() {
return $this->records;
}
/**
* @param array $records
* List of mock records to be read/updated by API calls.
*/
public function setRecords($records) {
$this->records = $records;
}
/**
* @param array $apiRequest
* The full description of the API request.
* @return array
* Formatted API result
* @throws \API_Exception
*/
public function doCreate($apiRequest) {
if (isset($apiRequest['params']['id'])) {
$id = $apiRequest['params']['id'];
}
else {
$id = max(array_keys($this->records)) + 1;
$this->records[$id] = array();
}
if (!isset($this->records[$id])) {
throw new \API_Exception("Invalid ID: $id");
}
foreach ($this->fields as $field) {
if (isset($apiRequest['params'][$field])) {
$this->records[$id][$field] = $apiRequest['params'][$field];
}
}
return civicrm_api3_create_success($this->records[$id]);
}
/**
* @param array $apiRequest
* The full description of the API request.
* @return array
* Formatted API result
* @throws \API_Exception
*/
public function doGet($apiRequest) {
return _civicrm_api3_basic_array_get($apiRequest['entity'], $apiRequest['params'], $this->records, 'id', $this->fields);
}
/**
* @param array $apiRequest
* The full description of the API request.
* @return array
* Formatted API result
* @throws \API_Exception
*/
public function doDelete($apiRequest) {
$id = @$apiRequest['params']['id'];
if ($id && isset($this->records[$id])) {
unset($this->records[$id]);
}
return civicrm_api3_create_success(array());
}
}