diff options
Diffstat (limited to 'engine/classes')
-rw-r--r-- | engine/classes/ElggBatch.php | 75 | ||||
-rw-r--r-- | engine/classes/ElggEntity.php | 8 | ||||
-rw-r--r-- | engine/classes/ElggFileCache.php | 19 | ||||
-rw-r--r-- | engine/classes/ElggGroup.php | 27 | ||||
-rw-r--r-- | engine/classes/ElggObject.php | 27 | ||||
-rw-r--r-- | engine/classes/ElggPluginManifest.php | 19 | ||||
-rw-r--r-- | engine/classes/ElggSite.php | 28 | ||||
-rw-r--r-- | engine/classes/ElggUser.php | 35 | ||||
-rw-r--r-- | engine/classes/ElggWidget.php | 32 | ||||
-rw-r--r-- | engine/classes/XMLRPCArrayParameter.php | 56 | ||||
-rw-r--r-- | engine/classes/XMLRPCBase64Parameter.php | 28 | ||||
-rw-r--r-- | engine/classes/XMLRPCBoolParameter.php | 30 | ||||
-rw-r--r-- | engine/classes/XMLRPCDateParameter.php | 33 | ||||
-rw-r--r-- | engine/classes/XMLRPCDoubleParameter.php | 29 | ||||
-rw-r--r-- | engine/classes/XMLRPCErrorResponse.php | 36 | ||||
-rw-r--r-- | engine/classes/XMLRPCIntParameter.php | 29 | ||||
-rw-r--r-- | engine/classes/XMLRPCParameter.php | 16 | ||||
-rw-r--r-- | engine/classes/XMLRPCResponse.php | 71 | ||||
-rw-r--r-- | engine/classes/XMLRPCStringParameter.php | 30 | ||||
-rw-r--r-- | engine/classes/XMLRPCStructParameter.php | 55 | ||||
-rw-r--r-- | engine/classes/XMLRPCSuccessResponse.php | 22 |
21 files changed, 631 insertions, 74 deletions
diff --git a/engine/classes/ElggBatch.php b/engine/classes/ElggBatch.php index 3d01133fa..0cb13eb32 100644 --- a/engine/classes/ElggBatch.php +++ b/engine/classes/ElggBatch.php @@ -3,47 +3,51 @@ * Efficiently run operations on batches of results for any function * that supports an options array. * - * This is usually used with elgg_get_entities() and friends, elgg_get_annotations() - * and elgg_get_metadata(). + * This is usually used with elgg_get_entities() and friends, + * elgg_get_annotations(), and elgg_get_metadata(). * - * If you pass a valid PHP callback, all results will be run through that callback. - * You can still foreach() through the result set after. Valid PHP callbacks - * can be a string, an array, or a closure. + * If you pass a valid PHP callback, all results will be run through that + * callback. You can still foreach() through the result set after. Valid + * PHP callbacks can be a string, an array, or a closure. * {@link http://php.net/manual/en/language.pseudo-types.php} * - * The callback function must accept 3 arguments: an entity, the getter used, and the options used. + * The callback function must accept 3 arguments: an entity, the getter + * used, and the options used. * - * Results from the callback are stored in callbackResult. - * If the callback returns only booleans, callbackResults will be the combined - * result of all calls. + * Results from the callback are stored in callbackResult. If the callback + * returns only booleans, callbackResults will be the combined result of + * all calls. * - * If the callback returns anything else, callbackresult will be an indexed array - * of whatever the callback returns. If returning error handling information, - * you should include enough information to determine which result you're referring - * to. + * If the callback returns anything else, callbackresult will be an indexed + * array of whatever the callback returns. If returning error handling + * information, you should include enough information to determine which + * result you're referring to. * * Don't combine returning bools and returning something else. * * Note that returning false will not stop the foreach. * + * @warning If your callback or foreach loop deletes or disable entities + * you MUST call setIncrementOffset(false) or set that when instantiating. + * This forces the offset to stay what it was in the $options array. + * * @example * <code> + * // using foreach * $batch = new ElggBatch('elgg_get_entities', array()); + * $batch->setIncrementOffset(false); * * foreach ($batch as $entity) { * $entity->disable(); * } * + * // using both a callback * $callback = function($result, $getter, $options) { - * var_dump("Going to delete annotation id: $result->id"); + * var_dump("Looking at annotation id: $result->id"); * return true; * } * * $batch = new ElggBatch('elgg_get_annotations', array('guid' => 2), $callback); - * - * foreach ($batch as $annotation) { - * $annotation->delete(); - * } * </code> * * @package Elgg.Core @@ -139,6 +143,13 @@ class ElggBatch public $callbackResult = null; /** + * If false, offset will not be incremented. This is used for callbacks/loops that delete. + * + * @var bool + */ + private $incrementOffset = true; + + /** * Batches operations on any elgg_get_*() or compatible function that supports * an options array. * @@ -156,12 +167,18 @@ class ElggBatch * @param int $chunk_size The number of entities to pull in before requesting more. * You have to balance this between running out of memory in PHP * and hitting the db server too often. + * @param bool $inc_offset Increment the offset on each fetch. This must be false for + * callbacks that delete rows. You can set this after the + * object is created with {@see ElggBatch::setIncrementOffset()}. */ - public function __construct($getter, $options, $callback = null, $chunk_size = 25) { + public function __construct($getter, $options, $callback = null, $chunk_size = 25, + $inc_offset = true) { + $this->getter = $getter; $this->options = $options; $this->callback = $callback; $this->chunkSize = $chunk_size; + $this->setIncrementOffset($inc_offset); if ($this->chunkSize <= 0) { $this->chunkSize = 25; @@ -174,7 +191,7 @@ class ElggBatch // if passed a callback, create a new ElggBatch with the same options // and pass each to the callback. if ($callback && is_callable($callback)) { - $batch = new ElggBatch($getter, $options, null, $chunk_size); + $batch = new ElggBatch($getter, $options, null, $chunk_size, $inc_offset); $all_results = null; @@ -245,9 +262,15 @@ class ElggBatch } } + if ($this->incrementOffset) { + $offset = $this->offset + $this->retrievedResults; + } else { + $offset = $this->offset; + } + $current_options = array( 'limit' => $limit, - 'offset' => $this->offset + $this->retrievedResults + 'offset' => $offset ); $options = array_merge($this->options, $current_options); @@ -270,6 +293,16 @@ class ElggBatch } /** + * Increment the offset from the original options array? Setting to + * false is required for callbacks that delete rows. + * + * @param bool $increment + */ + public function setIncrementOffset($increment = true) { + $this->incrementOffset = (bool) $increment; + } + + /** * Implements Iterator */ diff --git a/engine/classes/ElggEntity.php b/engine/classes/ElggEntity.php index 2fa8e9939..dc38dafbe 100644 --- a/engine/classes/ElggEntity.php +++ b/engine/classes/ElggEntity.php @@ -1311,12 +1311,16 @@ abstract class ElggEntity extends ElggData implements /** * Loads attributes from the entities table into the object. * - * @param int $guid GUID of Entity + * @param mixed $guid GUID of entity or stdClass object from entities table * * @return bool */ protected function load($guid) { - $row = get_entity_as_row($guid); + if ($guid instanceof stdClass) { + $row = $guid; + } else { + $row = get_entity_as_row($guid); + } if ($row) { // Create the array if necessary - all subclasses should test before creating diff --git a/engine/classes/ElggFileCache.php b/engine/classes/ElggFileCache.php index 8304372dc..34178d452 100644 --- a/engine/classes/ElggFileCache.php +++ b/engine/classes/ElggFileCache.php @@ -161,12 +161,25 @@ class ElggFileCache extends ElggCache { } /** - * This was probably meant to delete everything? + * Delete all files in the directory of this file cache * * @return void */ public function clear() { - // @todo writeme + $dir = $this->getVariable("cache_path"); + + $exclude = array(".", ".."); + + $files = scandir($dir); + if (!$files) { + return; + } + + foreach ($files as $f) { + if (!in_array($f, $exclude)) { + unlink($dir . $f); + } + } } /** @@ -184,7 +197,7 @@ class ElggFileCache extends ElggCache { return; } - $exclude = array(".",".."); + $exclude = array(".", ".."); $files = scandir($dir); if (!$files) { diff --git a/engine/classes/ElggGroup.php b/engine/classes/ElggGroup.php index ab223e1a4..f7f67bf41 100644 --- a/engine/classes/ElggGroup.php +++ b/engine/classes/ElggGroup.php @@ -29,12 +29,12 @@ class ElggGroup extends ElggEntity } /** - * Construct a new user entity, optionally from a given id value. + * Construct a new group entity, optionally from a given guid value. * * @param mixed $guid If an int, load that GUID. - * If a db row then will attempt to load the rest of the data. + * If an entity table db row, then will load the rest of the data. * - * @throws Exception if there was a problem creating the user. + * @throws Exception if there was a problem creating the group. */ function __construct($guid = null) { $this->initializeAttributes(); @@ -43,15 +43,15 @@ class ElggGroup extends ElggEntity $this->initialise_attributes(false); if (!empty($guid)) { - // Is $guid is a DB row - either a entity row, or a user table row. + // Is $guid is a entity table DB row if ($guid instanceof stdClass) { // Load the rest - if (!$this->load($guid->guid)) { + if (!$this->load($guid)) { $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid)); throw new IOException($msg); } - // Is $guid is an ElggGroup? Use a copy constructor + // Is $guid is an ElggGroup? Use a copy constructor } else if ($guid instanceof ElggGroup) { elgg_deprecated_notice('This type of usage of the ElggGroup constructor was deprecated. Please use the clone method.', 1.7); @@ -59,11 +59,11 @@ class ElggGroup extends ElggEntity $this->attributes[$key] = $value; } - // Is this is an ElggEntity but not an ElggGroup = ERROR! + // Is this is an ElggEntity but not an ElggGroup = ERROR! } else if ($guid instanceof ElggEntity) { throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggGroup')); - // We assume if we have got this far, $guid is an int + // Is it a GUID } else if (is_numeric($guid)) { if (!$this->load($guid)) { throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid))); @@ -319,11 +319,9 @@ class ElggGroup extends ElggEntity } /** - * Override the load function. - * This function will ensure that all data is loaded (were possible), so - * if only part of the ElggGroup is loaded, it'll load the rest. + * Load the ElggGroup data from the database * - * @param int $guid GUID of an ElggGroup entity + * @param mixed $guid GUID of an ElggGroup entity or database row from entity table * * @return bool */ @@ -333,6 +331,11 @@ class ElggGroup extends ElggEntity return false; } + // Only work with GUID from here + if ($guid instanceof stdClass) { + $guid = $guid->guid; + } + // Check the type if ($this->attributes['type'] != 'group') { $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class())); diff --git a/engine/classes/ElggObject.php b/engine/classes/ElggObject.php index 649e32490..b4bae6825 100644 --- a/engine/classes/ElggObject.php +++ b/engine/classes/ElggObject.php @@ -41,12 +41,12 @@ class ElggObject extends ElggEntity { * * If no arguments are passed, create a new entity. * - * If an argument is passed attempt to load a full Object entity. Arguments - * can be: + * If an argument is passed, attempt to load a full ElggObject entity. + * Arguments can be: * - The GUID of an object entity. - * - A DB result object with a guid property + * - A DB result object from the entities table with a guid property * - * @param mixed $guid If an int, load that GUID. If a db row then will attempt to + * @param mixed $guid If an int, load that GUID. If a db row, then will attempt to * load the rest of the data. * * @throws IOException If passed an incorrect guid @@ -59,15 +59,15 @@ class ElggObject extends ElggEntity { $this->initialise_attributes(false); if (!empty($guid)) { - // Is $guid is a DB row - either a entity row, or a object table row. + // Is $guid is a DB row from the entity table if ($guid instanceof stdClass) { // Load the rest - if (!$this->load($guid->guid)) { + if (!$this->load($guid)) { $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid)); throw new IOException($msg); } - // Is $guid is an ElggObject? Use a copy constructor + // Is $guid is an ElggObject? Use a copy constructor } else if ($guid instanceof ElggObject) { elgg_deprecated_notice('This type of usage of the ElggObject constructor was deprecated. Please use the clone method.', 1.7); @@ -75,11 +75,11 @@ class ElggObject extends ElggEntity { $this->attributes[$key] = $value; } - // Is this is an ElggEntity but not an ElggObject = ERROR! + // Is this is an ElggEntity but not an ElggObject = ERROR! } else if ($guid instanceof ElggEntity) { throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggObject')); - // We assume if we have got this far, $guid is an int + // Is it a GUID } else if (is_numeric($guid)) { if (!$this->load($guid)) { throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid))); @@ -93,17 +93,22 @@ class ElggObject extends ElggEntity { /** * Loads the full ElggObject when given a guid. * - * @param int $guid Guid of an ElggObject + * @param mixed $guid GUID of an ElggObject or the stdClass object from entities table * * @return bool * @throws InvalidClassException */ protected function load($guid) { - // Test to see if we have the generic stuff + // Load data from entity table if needed if (!parent::load($guid)) { return false; } + // Only work with GUID from here + if ($guid instanceof stdClass) { + $guid = $guid->guid; + } + // Check the type if ($this->attributes['type'] != 'object') { $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class())); diff --git a/engine/classes/ElggPluginManifest.php b/engine/classes/ElggPluginManifest.php index 7592eb667..7aa702d47 100644 --- a/engine/classes/ElggPluginManifest.php +++ b/engine/classes/ElggPluginManifest.php @@ -592,4 +592,23 @@ class ElggPluginManifest { return $return; } + + /** + * Returns a category's friendly name. This can be localized by + * defining the string 'admin:plugins:category:<category>'. If no + * localization is found, returns the category with _ and - converted to ' ' + * and then ucwords()'d. + * + * @param str $category The category as defined in the manifest. + * @return str A human-readable category + */ + static public function getFriendlyCategory($category) { + $cat_raw_string = "admin:plugins:category:$category"; + $cat_display_string = elgg_echo($cat_raw_string); + if ($cat_display_string == $cat_raw_string) { + $category = str_replace(array('-', '_'), ' ', $category); + $cat_display_string = ucwords($category); + } + return $cat_display_string; + } } diff --git a/engine/classes/ElggSite.php b/engine/classes/ElggSite.php index ac0c87bf7..6d07778a9 100644 --- a/engine/classes/ElggSite.php +++ b/engine/classes/ElggSite.php @@ -57,8 +57,8 @@ class ElggSite extends ElggEntity { * - A URL as stored in ElggSite->url * - A DB result object with a guid property * - * @param mixed $guid If an int, load that GUID. If a db row then will attempt - * to load the rest of the data. + * @param mixed $guid If an int, load that GUID. If a db row then will + * load the rest of the data. * * @throws IOException If passed an incorrect guid * @throws InvalidParameterException If passed an Elgg* Entity that isn't an ElggSite @@ -70,15 +70,15 @@ class ElggSite extends ElggEntity { $this->initialise_attributes(false); if (!empty($guid)) { - // Is $guid is a DB row - either a entity row, or a site table row. + // Is $guid is a DB entity table row if ($guid instanceof stdClass) { // Load the rest - if (!$this->load($guid->guid)) { + if (!$this->load($guid)) { $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid)); throw new IOException($msg); } - // Is $guid is an ElggSite? Use a copy constructor + // Is $guid is an ElggSite? Use a copy constructor } else if ($guid instanceof ElggSite) { elgg_deprecated_notice('This type of usage of the ElggSite constructor was deprecated. Please use the clone method.', 1.7); @@ -86,18 +86,18 @@ class ElggSite extends ElggEntity { $this->attributes[$key] = $value; } - // Is this is an ElggEntity but not an ElggSite = ERROR! + // Is this is an ElggEntity but not an ElggSite = ERROR! } else if ($guid instanceof ElggEntity) { throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggSite')); - // See if this is a URL + // See if this is a URL } else if (strpos($guid, "http") !== false) { $guid = get_site_by_url($guid); foreach ($guid->attributes as $key => $value) { $this->attributes[$key] = $value; } - // We assume if we have got this far, $guid is an int + // Is it a GUID } else if (is_numeric($guid)) { if (!$this->load($guid)) { throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid))); @@ -111,7 +111,7 @@ class ElggSite extends ElggEntity { /** * Loads the full ElggSite when given a guid. * - * @param int $guid Guid of ElggSite entity + * @param mixed $guid GUID of ElggSite entity or database row object * * @return bool * @throws InvalidClassException @@ -122,6 +122,11 @@ class ElggSite extends ElggEntity { return false; } + // Only work with GUID from here + if ($guid instanceof stdClass) { + $guid = $guid->guid; + } + // Check the type if ($this->attributes['type'] != 'site') { $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class())); @@ -417,6 +422,7 @@ class ElggSite extends ElggEntity { // default public pages $defaults = array( + 'walled_garden/.*', 'action/login', 'register', 'action/register', @@ -427,10 +433,14 @@ class ElggSite extends ElggEntity { 'action/security/refreshtoken', 'ajax/view/js/languages', 'upgrade\.php', + 'xml-rpc\.php', + 'mt/mt-xmlrpc\.cgi', 'css/.*', 'js/.*', 'cache/css/.*', 'cache/js/.*', + 'cron/.*', + 'services/.*', ); // include a hook for plugin authors to include public pages diff --git a/engine/classes/ElggUser.php b/engine/classes/ElggUser.php index bdf57c2c3..d7bb89265 100644 --- a/engine/classes/ElggUser.php +++ b/engine/classes/ElggUser.php @@ -47,7 +47,7 @@ class ElggUser extends ElggEntity * Construct a new user entity, optionally from a given id value. * * @param mixed $guid If an int, load that GUID. - * If a db row then will attempt to load the rest of the data. + * If an entity table db row then will load the rest of the data. * * @throws Exception if there was a problem creating the user. */ @@ -58,15 +58,15 @@ class ElggUser extends ElggEntity $this->initialise_attributes(false); if (!empty($guid)) { - // Is $guid is a DB row - either a entity row, or a user table row. + // Is $guid is a DB entity row if ($guid instanceof stdClass) { // Load the rest - if (!$this->load($guid->guid)) { + if (!$this->load($guid)) { $msg = elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid->guid)); throw new IOException($msg); } - // See if this is a username + // See if this is a username } else if (is_string($guid)) { $user = get_user_by_username($guid); if ($user) { @@ -75,7 +75,7 @@ class ElggUser extends ElggEntity } } - // Is $guid is an ElggUser? Use a copy constructor + // Is $guid is an ElggUser? Use a copy constructor } else if ($guid instanceof ElggUser) { elgg_deprecated_notice('This type of usage of the ElggUser constructor was deprecated. Please use the clone method.', 1.7); @@ -83,11 +83,11 @@ class ElggUser extends ElggEntity $this->attributes[$key] = $value; } - // Is this is an ElggEntity but not an ElggUser = ERROR! + // Is this is an ElggEntity but not an ElggUser = ERROR! } else if ($guid instanceof ElggEntity) { throw new InvalidParameterException(elgg_echo('InvalidParameterException:NonElggUser')); - // We assume if we have got this far, $guid is an int + // Is it a GUID } else if (is_numeric($guid)) { if (!$this->load($guid)) { throw new IOException(elgg_echo('IOException:FailedToLoadGUID', array(get_class(), $guid))); @@ -99,11 +99,9 @@ class ElggUser extends ElggEntity } /** - * Override the load function. - * This function will ensure that all data is loaded (were possible), so - * if only part of the ElggUser is loaded, it'll load the rest. + * Load the ElggUser data from the database * - * @param int $guid ElggUser GUID + * @param mixed $guid ElggUser GUID or stdClass database row from entity table * * @return bool */ @@ -113,6 +111,11 @@ class ElggUser extends ElggEntity return false; } + // Only work with GUID from here + if ($guid instanceof stdClass) { + $guid = $guid->guid; + } + // Check the type if ($this->attributes['type'] != 'user') { $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class())); @@ -384,7 +387,6 @@ class ElggUser extends ElggEntity 'relationship' => 'friend', 'relationship_guid' => $this->guid, 'limit' => $limit, - 'offset' => get_input('offset', 0), 'full_view' => false, ); @@ -458,7 +460,14 @@ class ElggUser extends ElggEntity * @return array|false */ public function getObjects($subtype = "", $limit = 10, $offset = 0) { - return get_user_objects($this->getGUID(), $subtype, $limit, $offset); + $params = array( + 'type' => 'object', + 'subtype' => $subtype, + 'owner_guid' => $this->getGUID(), + 'limit' => $limit, + 'offset' => $offset + ); + return elgg_get_entities($params); } /** diff --git a/engine/classes/ElggWidget.php b/engine/classes/ElggWidget.php index 7914fa140..99708f66a 100644 --- a/engine/classes/ElggWidget.php +++ b/engine/classes/ElggWidget.php @@ -131,11 +131,21 @@ class ElggWidget extends ElggObject { usort($widgets, create_function('$a,$b','return (int)$a->order > (int)$b->order;')); + // remove widgets from inactive plugins + $widget_types = elgg_get_widget_types($this->context); + $inactive_widgets = array(); + foreach ($widgets as $index => $widget) { + if (!array_key_exists($widget->handler, $widget_types)) { + $inactive_widgets[] = $widget; + unset($widgets[$index]); + } + } + if ($rank == 0) { // top of the column - $this->order = $widgets[0]->order - 10; + $this->order = reset($widgets)->order - 10; } elseif ($rank == (count($widgets) - 1)) { - // bottom of the column + // bottom of the column of active widgets $this->order = end($widgets)->order + 10; } else { // reorder widgets @@ -147,7 +157,7 @@ class ElggWidget extends ElggObject { } } - // split the array in two and recombine with the moved array in middle + // split the array in two and recombine with the moved widget in middle $before = array_slice($widgets, 0, $rank); array_push($before, $this); $after = array_slice($widgets, $rank); @@ -159,6 +169,22 @@ class ElggWidget extends ElggObject { $order += 10; } } + + // put inactive widgets at the bottom + if ($inactive_widgets) { + $bottom = 0; + foreach ($widgets as $widget) { + if ($widget->order > $bottom) { + $bottom = $widget->order; + } + } + $bottom += 10; + foreach ($inactive_widgets as $widget) { + $widget->order = $bottom; + $bottom += 10; + } + } + $this->column = $column; } diff --git a/engine/classes/XMLRPCArrayParameter.php b/engine/classes/XMLRPCArrayParameter.php new file mode 100644 index 000000000..a8edccba7 --- /dev/null +++ b/engine/classes/XMLRPCArrayParameter.php @@ -0,0 +1,56 @@ +<?php + +/** + * An array containing other XMLRPCParameter objects. + * + * @package Elgg.Core + * @subpackage XMLRPC + * + */ +class XMLRPCArrayParameter extends XMLRPCParameter +{ + /** + * Construct an array. + * + * @param array $parameters Optional array of parameters, if not provided + * then addField must be used. + */ + function __construct($parameters = NULL) { + parent::__construct(); + + if (is_array($parameters)) { + foreach ($parameters as $v) { + $this->addField($v); + } + } + } + + /** + * Add a field to the container. + * + * @param XMLRPCParameter $value The value. + * + * @return void + */ + public function addField(XMLRPCParameter $value) { + if (!is_array($this->value)) { + $this->value = array(); + } + + $this->value[] = $value; + } + + /** + * Converts XML array to string + * + * @return string + */ + function __toString() { + $params = ""; + foreach ($this->value as $value) { + $params .= "$value"; + } + + return "<array><data>$params</data></array>"; + } +} diff --git a/engine/classes/XMLRPCBase64Parameter.php b/engine/classes/XMLRPCBase64Parameter.php new file mode 100644 index 000000000..7db0a761c --- /dev/null +++ b/engine/classes/XMLRPCBase64Parameter.php @@ -0,0 +1,28 @@ +<?php +/** + * A base 64 encoded blob of binary. + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +class XMLRPCBase64Parameter extends XMLRPCParameter { + /** + * Construct a base64 encoded block + * + * @param string $blob Unencoded binary blob + */ + function __construct($blob) { + parent::__construct(); + + $this->value = base64_encode($blob); + } + + /** + * Convert to string + * + * @return string + */ + function __toString() { + return "<value><base64>{$value}</base64></value>"; + } +} diff --git a/engine/classes/XMLRPCBoolParameter.php b/engine/classes/XMLRPCBoolParameter.php new file mode 100644 index 000000000..607841cb8 --- /dev/null +++ b/engine/classes/XMLRPCBoolParameter.php @@ -0,0 +1,30 @@ +<?php +/** + * A boolean. + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +class XMLRPCBoolParameter extends XMLRPCParameter { + + /** + * New bool parameter + * + * @param bool $value Value + */ + function __construct($value) { + parent::__construct(); + + $this->value = (bool)$value; + } + + /** + * Convert to string + * + * @return string + */ + function __toString() { + $code = ($this->value) ? "1" : "0"; + return "<value><boolean>{$code}</boolean></value>"; + } +} diff --git a/engine/classes/XMLRPCDateParameter.php b/engine/classes/XMLRPCDateParameter.php new file mode 100644 index 000000000..93bbbd8f5 --- /dev/null +++ b/engine/classes/XMLRPCDateParameter.php @@ -0,0 +1,33 @@ +<?php +/** + * An ISO8601 data and time. + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +class XMLRPCDateParameter extends XMLRPCParameter { + /** + * Construct a date + * + * @param int $timestamp The unix timestamp, or blank for "now". + */ + function __construct($timestamp = 0) { + parent::__construct(); + + $this->value = $timestamp; + + if (!$timestamp) { + $this->value = time(); + } + } + + /** + * Convert to string + * + * @return string + */ + function __toString() { + $value = date('c', $this->value); + return "<value><dateTime.iso8601>{$value}</dateTime.iso8601></value>"; + } +} diff --git a/engine/classes/XMLRPCDoubleParameter.php b/engine/classes/XMLRPCDoubleParameter.php new file mode 100644 index 000000000..b7834650e --- /dev/null +++ b/engine/classes/XMLRPCDoubleParameter.php @@ -0,0 +1,29 @@ +<?php +/** + * A double precision signed floating point number. + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +class XMLRPCDoubleParameter extends XMLRPCParameter { + + /** + * New XML Double + * + * @param int $value Value + */ + function __construct($value) { + parent::__construct(); + + $this->value = (float)$value; + } + + /** + * Convert to string + * + * @return string + */ + function __toString() { + return "<value><double>{$this->value}</double></value>"; + } +} diff --git a/engine/classes/XMLRPCErrorResponse.php b/engine/classes/XMLRPCErrorResponse.php new file mode 100644 index 000000000..425c075cc --- /dev/null +++ b/engine/classes/XMLRPCErrorResponse.php @@ -0,0 +1,36 @@ +<?php + +/** + * XMLRPC Error Response + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +class XMLRPCErrorResponse extends XMLRPCResponse { + /** + * Set the error response and error code. + * + * @param string $message The message + * @param int $code Error code (default = system error as defined by + * http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php) + */ + function __construct($message, $code = -32400) { + $this->addParameter( + new XMLRPCStructParameter( + array ( + 'faultCode' => new XMLRPCIntParameter($code), + 'faultString' => new XMLRPCStringParameter($message) + ) + ) + ); + } + + /** + * Output to XML. + * + * @return string + */ + public function __toString() { + return "<methodResponse><fault><value>{$this->parameters[0]}</value></fault></methodResponse>"; + } +} diff --git a/engine/classes/XMLRPCIntParameter.php b/engine/classes/XMLRPCIntParameter.php new file mode 100644 index 000000000..0fc146165 --- /dev/null +++ b/engine/classes/XMLRPCIntParameter.php @@ -0,0 +1,29 @@ +<?php +/** + * An Integer. + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +class XMLRPCIntParameter extends XMLRPCParameter { + + /** + * A new XML int + * + * @param int $value Value + */ + function __construct($value) { + parent::__construct(); + + $this->value = (int)$value; + } + + /** + * Convert to string + * + * @return string + */ + function __toString() { + return "<value><i4>{$this->value}</i4></value>"; + } +} diff --git a/engine/classes/XMLRPCParameter.php b/engine/classes/XMLRPCParameter.php new file mode 100644 index 000000000..ffbad8082 --- /dev/null +++ b/engine/classes/XMLRPCParameter.php @@ -0,0 +1,16 @@ +<?php +/** + * Superclass for all RPC parameters. + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +abstract class XMLRPCParameter { + protected $value; + + /** + * Set initial values + */ + function __construct() { } + +} diff --git a/engine/classes/XMLRPCResponse.php b/engine/classes/XMLRPCResponse.php new file mode 100644 index 000000000..a6256d385 --- /dev/null +++ b/engine/classes/XMLRPCResponse.php @@ -0,0 +1,71 @@ +<?php + +/** + * XML-RPC Response. + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +abstract class XMLRPCResponse { + /** An array of parameters */ + protected $parameters = array(); + + /** + * Add a parameter here. + * + * @param XMLRPCParameter $param The parameter. + * + * @return void + */ + public function addParameter(XMLRPCParameter $param) { + if (!is_array($this->parameters)) { + $this->parameters = array(); + } + + $this->parameters[] = $param; + } + + /** + * Add an integer + * + * @param int $value Value + * + * @return void + */ + public function addInt($value) { + $this->addParameter(new XMLRPCIntParameter($value)); + } + + /** + * Add a string + * + * @param string $value Value + * + * @return void + */ + public function addString($value) { + $this->addParameter(new XMLRPCStringParameter($value)); + } + + /** + * Add a double + * + * @param int $value Value + * + * @return void + */ + public function addDouble($value) { + $this->addParameter(new XMLRPCDoubleParameter($value)); + } + + /** + * Add a boolean + * + * @param bool $value Value + * + * @return void + */ + public function addBoolean($value) { + $this->addParameter(new XMLRPCBoolParameter($value)); + } +} diff --git a/engine/classes/XMLRPCStringParameter.php b/engine/classes/XMLRPCStringParameter.php new file mode 100644 index 000000000..35b28214b --- /dev/null +++ b/engine/classes/XMLRPCStringParameter.php @@ -0,0 +1,30 @@ +<?php +/** + * A string. + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +class XMLRPCStringParameter extends XMLRPCParameter { + + /** + * A new XML string + * + * @param string $value Value + */ + function __construct($value) { + parent::__construct(); + + $this->value = $value; + } + + /** + * Convert to XML string + * + * @return string + */ + function __toString() { + $value = htmlentities($this->value); + return "<value><string>{$value}</string></value>"; + } +} diff --git a/engine/classes/XMLRPCStructParameter.php b/engine/classes/XMLRPCStructParameter.php new file mode 100644 index 000000000..694ddf5df --- /dev/null +++ b/engine/classes/XMLRPCStructParameter.php @@ -0,0 +1,55 @@ +<?php + +/** + * A structure containing other XMLRPCParameter objects. + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +class XMLRPCStructParameter extends XMLRPCParameter { + /** + * Construct a struct. + * + * @param array $parameters Optional associated array of parameters, if + * not provided then addField must be used. + */ + function __construct($parameters = NULL) { + parent::__construct(); + + if (is_array($parameters)) { + foreach ($parameters as $k => $v) { + $this->addField($k, $v); + } + } + } + + /** + * Add a field to the container. + * + * @param string $name The name of the field. + * @param XMLRPCParameter $value The value. + * + * @return void + */ + public function addField($name, XMLRPCParameter $value) { + if (!is_array($this->value)) { + $this->value = array(); + } + + $this->value[$name] = $value; + } + + /** + * Convert to string + * + * @return string + */ + function __toString() { + $params = ""; + foreach ($this->value as $k => $v) { + $params .= "<member><name>$k</name>$v</member>"; + } + + return "<value><struct>$params</struct></value>"; + } +} diff --git a/engine/classes/XMLRPCSuccessResponse.php b/engine/classes/XMLRPCSuccessResponse.php new file mode 100644 index 000000000..e02e82c5c --- /dev/null +++ b/engine/classes/XMLRPCSuccessResponse.php @@ -0,0 +1,22 @@ +<?php +/** + * Success Response + * + * @package Elgg.Core + * @subpackage XMLRPC + */ +class XMLRPCSuccessResponse extends XMLRPCResponse { + /** + * Output to XML. + * + * @return string + */ + public function __toString() { + $params = ""; + foreach ($this->parameters as $param) { + $params .= "<param>$param</param>\n"; + } + + return "<methodResponse><params>$params</params></methodResponse>"; + } +} |