pax_global_header 0000666 0000000 0000000 00000000064 12501305533 0014507 g ustar 00root root 0000000 0000000 52 comment=b5048b989e4f1389a8190bc8a8976e9999bcdfd7
www-test-master-includes/ 0000775 0000000 0000000 00000000000 12501305533 0015751 5 ustar 00root root 0000000 0000000 www-test-master-includes/includes/ 0000775 0000000 0000000 00000000000 12501305533 0017557 5 ustar 00root root 0000000 0000000 www-test-master-includes/includes/actions.inc 0000664 0000000 0000000 00000032770 12501305533 0021723 0 ustar 00root root 0000000 0000000 variable_get('actions_max_stack', 35)) {
watchdog('actions', 'Stack overflow: too many calls to actions_do(). Aborting to prevent infinite recursion.', array(), WATCHDOG_ERROR);
return;
}
$actions = array();
$available_actions = actions_list();
$actions_result = array();
if (is_array($action_ids)) {
$conditions = array();
foreach ($action_ids as $action_id) {
if (is_numeric($action_id)) {
$conditions[] = $action_id;
}
elseif (isset($available_actions[$action_id])) {
$actions[$action_id] = $available_actions[$action_id];
}
}
// When we have action instances we must go to the database to retrieve
// instance data.
if (!empty($conditions)) {
$query = db_select('actions');
$query->addField('actions', 'aid');
$query->addField('actions', 'type');
$query->addField('actions', 'callback');
$query->addField('actions', 'parameters');
$query->condition('aid', $conditions, 'IN');
$result = $query->execute();
foreach ($result as $action) {
$actions[$action->aid] = $action->parameters ? unserialize($action->parameters) : array();
$actions[$action->aid]['callback'] = $action->callback;
$actions[$action->aid]['type'] = $action->type;
}
}
// Fire actions, in no particular order.
foreach ($actions as $action_id => $params) {
// Configurable actions need parameters.
if (is_numeric($action_id)) {
$function = $params['callback'];
if (function_exists($function)) {
$context = array_merge($context, $params);
$actions_result[$action_id] = $function($object, $context, $a1, $a2);
}
else {
$actions_result[$action_id] = FALSE;
}
}
// Singleton action; $action_id is the function name.
else {
$actions_result[$action_id] = $action_id($object, $context, $a1, $a2);
}
}
}
// Optimized execution of a single action.
else {
// If it's a configurable action, retrieve stored parameters.
if (is_numeric($action_ids)) {
$action = db_query("SELECT callback, parameters FROM {actions} WHERE aid = :aid", array(':aid' => $action_ids))->fetchObject();
$function = $action->callback;
if (function_exists($function)) {
$context = array_merge($context, unserialize($action->parameters));
$actions_result[$action_ids] = $function($object, $context, $a1, $a2);
}
else {
$actions_result[$action_ids] = FALSE;
}
}
// Singleton action; $action_ids is the function name.
else {
if (function_exists($action_ids)) {
$actions_result[$action_ids] = $action_ids($object, $context, $a1, $a2);
}
else {
// Set to avoid undefined index error messages later.
$actions_result[$action_ids] = FALSE;
}
}
}
$stack--;
return $actions_result;
}
/**
* Discovers all available actions by invoking hook_action_info().
*
* This function contrasts with actions_get_all_actions(); see the
* documentation of actions_get_all_actions() for an explanation.
*
* @param $reset
* Reset the action info static cache.
*
* @return
* An associative array keyed on action function name, with the same format
* as the return value of hook_action_info(), containing all
* modules' hook_action_info() return values as modified by any
* hook_action_info_alter() implementations.
*
* @see hook_action_info()
*/
function actions_list($reset = FALSE) {
$actions = &drupal_static(__FUNCTION__);
if (!isset($actions) || $reset) {
$actions = module_invoke_all('action_info');
drupal_alter('action_info', $actions);
}
// See module_implements() for an explanation of this cast.
return (array) $actions;
}
/**
* Retrieves all action instances from the database.
*
* This function differs from the actions_list() function, which gathers
* actions by invoking hook_action_info(). The actions returned by this
* function and the actions returned by actions_list() are partially
* synchronized. Non-configurable actions from hook_action_info()
* implementations are put into the database when actions_synchronize() is
* called, which happens when admin/config/system/actions is visited.
* Configurable actions are not added to the database until they are configured
* in the user interface, in which case a database row is created for each
* configuration of each action.
*
* @return
* Associative array keyed by numeric action ID. Each value is an associative
* array with keys 'callback', 'label', 'type' and 'configurable'.
*/
function actions_get_all_actions() {
$actions = db_query("SELECT aid, type, callback, parameters, label FROM {actions}")->fetchAllAssoc('aid', PDO::FETCH_ASSOC);
foreach ($actions as &$action) {
$action['configurable'] = (bool) $action['parameters'];
unset($action['parameters']);
unset($action['aid']);
}
return $actions;
}
/**
* Creates an associative array keyed by hashes of function names or IDs.
*
* Hashes are used to prevent actual function names from going out into HTML
* forms and coming back.
*
* @param $actions
* An associative array with function names or action IDs as keys
* and associative arrays with keys 'label', 'type', etc. as values.
* This is usually the output of actions_list() or actions_get_all_actions().
*
* @return
* An associative array whose keys are hashes of the input array keys, and
* whose corresponding values are associative arrays with components
* 'callback', 'label', 'type', and 'configurable' from the input array.
*/
function actions_actions_map($actions) {
$actions_map = array();
foreach ($actions as $callback => $array) {
$key = drupal_hash_base64($callback);
$actions_map[$key]['callback'] = isset($array['callback']) ? $array['callback'] : $callback;
$actions_map[$key]['label'] = $array['label'];
$actions_map[$key]['type'] = $array['type'];
$actions_map[$key]['configurable'] = $array['configurable'];
}
return $actions_map;
}
/**
* Returns an action array key (function or ID), given its hash.
*
* Faster than actions_actions_map() when you only need the function name or ID.
*
* @param $hash
* Hash of a function name or action ID array key. The array key
* is a key into the return value of actions_list() (array key is the action
* function name) or actions_get_all_actions() (array key is the action ID).
*
* @return
* The corresponding array key, or FALSE if no match is found.
*/
function actions_function_lookup($hash) {
// Check for a function name match.
$actions_list = actions_list();
foreach ($actions_list as $function => $array) {
if (drupal_hash_base64($function) == $hash) {
return $function;
}
}
$aid = FALSE;
// Must be a configurable action; check database.
$result = db_query("SELECT aid FROM {actions} WHERE parameters <> ''")->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
if (drupal_hash_base64($row['aid']) == $hash) {
$aid = $row['aid'];
break;
}
}
return $aid;
}
/**
* Synchronizes actions that are provided by modules in hook_action_info().
*
* Actions provided by modules in hook_action_info() implementations are
* synchronized with actions that are stored in the actions database table.
* This is necessary so that actions that do not require configuration can
* receive action IDs.
*
* @param $delete_orphans
* If TRUE, any actions that exist in the database but are no longer
* found in the code (for example, because the module that provides them has
* been disabled) will be deleted.
*/
function actions_synchronize($delete_orphans = FALSE) {
$actions_in_code = actions_list(TRUE);
$actions_in_db = db_query("SELECT aid, callback, label FROM {actions} WHERE parameters = ''")->fetchAllAssoc('callback', PDO::FETCH_ASSOC);
// Go through all the actions provided by modules.
foreach ($actions_in_code as $callback => $array) {
// Ignore configurable actions since their instances get put in when the
// user adds the action.
if (!$array['configurable']) {
// If we already have an action ID for this action, no need to assign aid.
if (isset($actions_in_db[$callback])) {
unset($actions_in_db[$callback]);
}
else {
// This is a new singleton that we don't have an aid for; assign one.
db_insert('actions')
->fields(array(
'aid' => $callback,
'type' => $array['type'],
'callback' => $callback,
'parameters' => '',
'label' => $array['label'],
))
->execute();
watchdog('actions', "Action '%action' added.", array('%action' => $array['label']));
}
}
}
// Any actions that we have left in $actions_in_db are orphaned.
if ($actions_in_db) {
$orphaned = array_keys($actions_in_db);
if ($delete_orphans) {
$actions = db_query('SELECT aid, label FROM {actions} WHERE callback IN (:orphaned)', array(':orphaned' => $orphaned))->fetchAll();
foreach ($actions as $action) {
actions_delete($action->aid);
watchdog('actions', "Removed orphaned action '%action' from database.", array('%action' => $action->label));
}
}
else {
$link = l(t('Remove orphaned actions'), 'admin/config/system/actions/orphan');
$count = count($actions_in_db);
$orphans = implode(', ', $orphaned);
watchdog('actions', '@count orphaned actions (%orphans) exist in the actions table. !link', array('@count' => $count, '%orphans' => $orphans, '!link' => $link), WATCHDOG_INFO);
}
}
}
/**
* Saves an action and its user-supplied parameter values to the database.
*
* @param $function
* The name of the function to be called when this action is performed.
* @param $type
* The type of action, to describe grouping and/or context, e.g., 'node',
* 'user', 'comment', or 'system'.
* @param $params
* An associative array with parameter names as keys and parameter values as
* values.
* @param $label
* A user-supplied label of this particular action, e.g., 'Send e-mail
* to Jim'.
* @param $aid
* The ID of this action. If omitted, a new action is created.
*
* @return
* The ID of the action.
*/
function actions_save($function, $type, $params, $label, $aid = NULL) {
// aid is the callback for singleton actions so we need to keep a separate
// table for numeric aids.
if (!$aid) {
$aid = db_next_id();
}
db_merge('actions')
->key(array('aid' => $aid))
->fields(array(
'callback' => $function,
'type' => $type,
'parameters' => serialize($params),
'label' => $label,
))
->execute();
watchdog('actions', 'Action %action saved.', array('%action' => $label));
return $aid;
}
/**
* Retrieves a single action from the database.
*
* @param $aid
* The ID of the action to retrieve.
*
* @return
* The appropriate action row from the database as an object.
*/
function actions_load($aid) {
return db_query("SELECT aid, type, callback, parameters, label FROM {actions} WHERE aid = :aid", array(':aid' => $aid))->fetchObject();
}
/**
* Deletes a single action from the database.
*
* @param $aid
* The ID of the action to delete.
*/
function actions_delete($aid) {
db_delete('actions')
->condition('aid', $aid)
->execute();
module_invoke_all('actions_delete', $aid);
}
www-test-master-includes/includes/ajax.inc 0000664 0000000 0000000 00000140132 12501305533 0021176 0 ustar 00root root 0000000 0000000 'select',
* '#options' => array(
* 'one' => 'one',
* 'two' => 'two',
* 'three' => 'three',
* ),
* '#ajax' => array(
* 'callback' => 'ajax_example_simplest_callback',
* 'wrapper' => 'replace_textfield_div',
* ),
* );
* // This entire form element will be replaced with an updated value.
* $form['replace_textfield'] = array(
* '#type' => 'textfield',
* '#title' => t("The default value will be changed"),
* '#description' => t("Say something about why you chose") . "'" .
* (!empty($form_state['values']['changethis'])
* ? $form_state['values']['changethis'] : t("Not changed yet")) . "'",
* '#prefix' => '
',
* '#suffix' => '
',
* );
* return $form;
* }
*
* function ajax_example_simplest_callback($form, $form_state) {
* // The form has already been submitted and updated. We can return the replaced
* // item as it is.
* return $form['replace_textfield'];
* }
* @endcode
*
* In the above example, the 'changethis' element is Ajax-enabled. The default
* #ajax['event'] is 'change', so when the 'changethis' element changes,
* an Ajax call is made. The form is submitted and reprocessed, and then the
* callback is called. In this case, the form has been automatically
* built changing $form['replace_textfield']['#description'], so the callback
* just returns that part of the form.
*
* To implement Ajax handling in a form, add '#ajax' to the form
* definition of a field. That field will trigger an Ajax event when it is
* clicked (or changed, depending on the kind of field). #ajax supports
* the following parameters (either 'path' or 'callback' is required at least):
* - #ajax['callback']: The callback to invoke to handle the server side of the
* Ajax event, which will receive a $form and $form_state as arguments, and
* returns a renderable array (most often a form or form fragment), an HTML
* string, or an array of Ajax commands. If returning a renderable array or
* a string, the value will replace the original element named in
* #ajax['wrapper'], and
* theme_status_messages()
* will be prepended to that
* element. (If the status messages are not wanted, return an array
* of Ajax commands instead.)
* #ajax['wrapper']. If an array of Ajax commands is returned, it will be
* executed by the calling code.
* - #ajax['path']: The menu path to use for the request. This is often omitted
* and the default is used. This path should map
* to a menu page callback that returns data using ajax_render(). Defaults to
* 'system/ajax', which invokes ajax_form_callback(), eventually calling
* the function named in #ajax['callback']. If you use a custom
* path, you must set up the menu entry and handle the entire callback in your
* own code.
* - #ajax['wrapper']: The CSS ID of the area to be replaced by the content
* returned by the #ajax['callback'] function. The content returned from
* the callback will replace the entire element named by #ajax['wrapper'].
* The wrapper is usually created using #prefix and #suffix properties in the
* form. Note that this is the wrapper ID, not a CSS selector. So to replace
* the element referred to by the CSS selector #some-selector on the page,
* use #ajax['wrapper'] = 'some-selector', not '#some-selector'.
* - #ajax['effect']: The jQuery effect to use when placing the new HTML.
* Defaults to no effect. Valid options are 'none', 'slide', or 'fade'.
* - #ajax['speed']: The effect speed to use. Defaults to 'slow'. May be
* 'slow', 'fast' or a number in milliseconds which represents the length
* of time the effect should run.
* - #ajax['event']: The JavaScript event to respond to. This is normally
* selected automatically for the type of form widget being used, and
* is only needed if you need to override the default behavior.
* - #ajax['prevent']: A JavaScript event to prevent when 'event' is triggered.
* Defaults to 'click' for #ajax on #type 'submit', 'button', and
* 'image_button'. Multiple events may be specified separated by spaces.
* For example, when binding #ajax behaviors to form buttons, pressing the
* ENTER key within a textfield triggers the 'click' event of the form's first
* submit button. Triggering Ajax in this situation leads to problems, like
* breaking autocomplete textfields. Because of that, Ajax behaviors are bound
* to the 'mousedown' event on form buttons by default. However, binding to
* 'mousedown' rather than 'click' means that it is possible to trigger a
* click by pressing the mouse, holding the mouse button down until the Ajax
* request is complete and the button is re-enabled, and then releasing the
* mouse button. For this case, 'prevent' can be set to 'click', so an
* additional event handler is bound to prevent such a click from triggering a
* non-Ajax form submission. This also prevents a textfield's ENTER press
* triggering a button's non-Ajax form submission behavior.
* - #ajax['method']: The jQuery method to use to place the new HTML.
* Defaults to 'replaceWith'. May be: 'replaceWith', 'append', 'prepend',
* 'before', 'after', or 'html'. See the
* @link http://api.jquery.com/category/manipulation/ jQuery manipulators documentation @endlink
* for more information on these methods.
* - #ajax['progress']: Choose either a throbber or progress bar that is
* displayed while awaiting a response from the callback, and add an optional
* message. Possible keys: 'type', 'message', 'url', 'interval'.
* More information is available in the
* @link forms_api_reference.html Form API Reference @endlink
*
* In addition to using Form API for doing in-form modification, Ajax may be
* enabled by adding classes to buttons and links. By adding the 'use-ajax'
* class to a link, the link will be loaded via an Ajax call. When using this
* method, the href of the link can contain '/nojs/' as part of the path. When
* the Ajax framework makes the request, it will convert this to '/ajax/'.
* The server is then able to easily tell if this request was made through an
* actual Ajax request or in a degraded state, and respond appropriately.
*
* Similarly, submit buttons can be given the class 'use-ajax-submit'. The
* form will then be submitted via Ajax to the path specified in the #action.
* Like the ajax-submit class above, this path will have '/nojs/' replaced with
* '/ajax/' so that the submit handler can tell if the form was submitted
* in a degraded state or not.
*
* When responding to Ajax requests, the server should do what it needs to do
* for that request, then create a commands array. This commands array will
* be converted to a JSON object and returned to the client, which will then
* iterate over the array and process it like a macro language.
*
* Each command item is an associative array which will be converted to a
* command object on the JavaScript side. $command_item['command'] is the type
* of command, e.g. 'alert' or 'replace', and will correspond to a method in the
* Drupal.ajax[command] space. The command array may contain any other data that
* the command needs to process, e.g. 'method', 'selector', 'settings', etc.
*
* Commands are usually created with a couple of helper functions, so they
* look like this:
* @code
* $commands = array();
* // Replace the content of '#object-1' on the page with 'some html here'.
* $commands[] = ajax_command_replace('#object-1', 'some html here');
* // Add a visual "changed" marker to the '#object-1' element.
* $commands[] = ajax_command_changed('#object-1');
* // Menu 'page callback' and #ajax['callback'] functions are supposed to
* // return render arrays. If returning an Ajax commands array, it must be
* // encapsulated in a render array structure.
* return array('#type' => 'ajax', '#commands' => $commands);
* @endcode
*
* When returning an Ajax command array, it is often useful to have
* status messages rendered along with other tasks in the command array.
* In that case the the Ajax commands array may be constructed like this:
* @code
* $commands = array();
* $commands[] = ajax_command_replace(NULL, $output);
* $commands[] = ajax_command_prepend(NULL, theme('status_messages'));
* return array('#type' => 'ajax', '#commands' => $commands);
* @endcode
*
* See @link ajax_commands Ajax framework commands @endlink
*/
/**
* Renders a commands array into JSON.
*
* @param $commands
* A list of macro commands generated by the use of ajax_command_*()
* functions.
*/
function ajax_render($commands = array()) {
// Ajax responses aren't rendered with html.tpl.php, so we have to call
// drupal_get_css() and drupal_get_js() here, in order to have new files added
// during this request to be loaded by the page. We only want to send back
// files that the page hasn't already loaded, so we implement simple diffing
// logic using array_diff_key().
foreach (array('css', 'js') as $type) {
// It is highly suspicious if $_POST['ajax_page_state'][$type] is empty,
// since the base page ought to have at least one JS file and one CSS file
// loaded. It probably indicates an error, and rather than making the page
// reload all of the files, instead we return no new files.
if (empty($_POST['ajax_page_state'][$type])) {
$items[$type] = array();
}
else {
$function = 'drupal_add_' . $type;
$items[$type] = $function();
drupal_alter($type, $items[$type]);
// @todo Inline CSS and JS items are indexed numerically. These can't be
// reliably diffed with array_diff_key(), since the number can change
// due to factors unrelated to the inline content, so for now, we strip
// the inline items from Ajax responses, and can add support for them
// when drupal_add_css() and drupal_add_js() are changed to use a hash
// of the inline content as the array key.
foreach ($items[$type] as $key => $item) {
if (is_numeric($key)) {
unset($items[$type][$key]);
}
}
// Ensure that the page doesn't reload what it already has.
$items[$type] = array_diff_key($items[$type], $_POST['ajax_page_state'][$type]);
}
}
// Render the HTML to load these files, and add AJAX commands to insert this
// HTML in the page. We pass TRUE as the $skip_alter argument to prevent the
// data from being altered again, as we already altered it above. Settings are
// handled separately, afterwards.
if (isset($items['js']['settings'])) {
unset($items['js']['settings']);
}
$styles = drupal_get_css($items['css'], TRUE);
$scripts_footer = drupal_get_js('footer', $items['js'], TRUE);
$scripts_header = drupal_get_js('header', $items['js'], TRUE);
$extra_commands = array();
if (!empty($styles)) {
$extra_commands[] = ajax_command_add_css($styles);
}
if (!empty($scripts_header)) {
$extra_commands[] = ajax_command_prepend('head', $scripts_header);
}
if (!empty($scripts_footer)) {
$extra_commands[] = ajax_command_append('body', $scripts_footer);
}
if (!empty($extra_commands)) {
$commands = array_merge($extra_commands, $commands);
}
// Now add a command to merge changes and additions to Drupal.settings.
$scripts = drupal_add_js();
if (!empty($scripts['settings'])) {
$settings = $scripts['settings'];
array_unshift($commands, ajax_command_settings(drupal_array_merge_deep_array($settings['data']), TRUE));
}
// Allow modules to alter any Ajax response.
drupal_alter('ajax_render', $commands);
return drupal_json_encode($commands);
}
/**
* Gets a form submitted via #ajax during an Ajax callback.
*
* This will load a form from the form cache used during Ajax operations. It
* pulls the form info from $_POST.
*
* @return
* An array containing the $form, $form_state, $form_id, $form_build_id and an
* initial list of Ajax $commands. Use the list() function to break these
* apart:
* @code
* list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();
* @endcode
*/
function ajax_get_form() {
$form_state = form_state_defaults();
$form_build_id = $_POST['form_build_id'];
// Get the form from the cache.
$form = form_get_cache($form_build_id, $form_state);
if (!$form) {
// If $form cannot be loaded from the cache, the form_build_id in $_POST
// must be invalid, which means that someone performed a POST request onto
// system/ajax without actually viewing the concerned form in the browser.
// This is likely a hacking attempt as it never happens under normal
// circumstances, so we just do nothing.
watchdog('ajax', 'Invalid form POST data.', array(), WATCHDOG_WARNING);
drupal_exit();
}
// When a page level cache is enabled, the form-build id might have been
// replaced from within form_get_cache. If this is the case, it is also
// necessary to update it in the browser by issuing an appropriate Ajax
// command.
$commands = array();
if (isset($form['#build_id_old']) && $form['#build_id_old'] != $form['#build_id']) {
// If the form build ID has changed, issue an Ajax command to update it.
$commands[] = ajax_command_update_build_id($form);
$form_build_id = $form['#build_id'];
}
// Since some of the submit handlers are run, redirects need to be disabled.
$form_state['no_redirect'] = TRUE;
// When a form is rebuilt after Ajax processing, its #build_id and #action
// should not change.
// @see drupal_rebuild_form()
$form_state['rebuild_info']['copy']['#build_id'] = TRUE;
$form_state['rebuild_info']['copy']['#action'] = TRUE;
// The form needs to be processed; prepare for that by setting a few internal
// variables.
$form_state['input'] = $_POST;
$form_id = $form['#form_id'];
return array($form, $form_state, $form_id, $form_build_id, $commands);
}
/**
* Menu callback; handles Ajax requests for the #ajax Form API property.
*
* This rebuilds the form from cache and invokes the defined #ajax['callback']
* to return an Ajax command structure for JavaScript. In case no 'callback' has
* been defined, nothing will happen.
*
* The Form API #ajax property can be set both for buttons and other input
* elements.
*
* This function is also the canonical example of how to implement
* #ajax['path']. If processing is required that cannot be accomplished with
* a callback, re-implement this function and set #ajax['path'] to the
* enhanced function.
*
* @see system_menu()
*/
function ajax_form_callback() {
list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();
drupal_process_form($form['#form_id'], $form, $form_state);
// We need to return the part of the form (or some other content) that needs
// to be re-rendered so the browser can update the page with changed content.
// Since this is the generic menu callback used by many Ajax elements, it is
// up to the #ajax['callback'] function of the element (may or may not be a
// button) that triggered the Ajax request to determine what needs to be
// rendered.
if (!empty($form_state['triggering_element'])) {
$callback = $form_state['triggering_element']['#ajax']['callback'];
}
if (!empty($callback) && function_exists($callback)) {
$result = $callback($form, $form_state);
if (!(is_array($result) && isset($result['#type']) && $result['#type'] == 'ajax')) {
// Turn the response into a #type=ajax array if it isn't one already.
$result = array(
'#type' => 'ajax',
'#commands' => ajax_prepare_response($result),
);
}
$result['#commands'] = array_merge($commands, $result['#commands']);
return $result;
}
}
/**
* Theme callback for Ajax requests.
*
* Many different pages can invoke an Ajax request to system/ajax or another
* generic Ajax path. It is almost always desired for an Ajax response to be
* rendered using the same theme as the base page, because most themes are built
* with the assumption that they control the entire page, so if the CSS for two
* themes are both loaded for a given page, they may conflict with each other.
* For example, Bartik is Drupal's default theme, and Seven is Drupal's default
* administration theme. Depending on whether the "Use the administration theme
* when editing or creating content" checkbox is checked, the node edit form may
* be displayed in either theme, but the Ajax response to the Field module's
* "Add another item" button should be rendered using the same theme as the rest
* of the page. Therefore, system_menu() sets the 'theme callback' for
* 'system/ajax' to this function, and it is recommended that modules
* implementing other generic Ajax paths do the same.
*
* @see system_menu()
* @see file_menu()
*/
function ajax_base_page_theme() {
if (!empty($_POST['ajax_page_state']['theme']) && !empty($_POST['ajax_page_state']['theme_token'])) {
$theme = $_POST['ajax_page_state']['theme'];
$token = $_POST['ajax_page_state']['theme_token'];
// Prevent a request forgery from giving a person access to a theme they
// shouldn't be otherwise allowed to see. However, since everyone is allowed
// to see the default theme, token validation isn't required for that, and
// bypassing it allows most use-cases to work even when accessed from the
// page cache.
if ($theme === variable_get('theme_default', 'bartik') || drupal_valid_token($token, $theme)) {
return $theme;
}
}
}
/**
* Packages and sends the result of a page callback as an Ajax response.
*
* This function is the equivalent of drupal_deliver_html_page(), but for Ajax
* requests. Like that function, it:
* - Adds needed HTTP headers.
* - Prints rendered output.
* - Performs end-of-request tasks.
*
* @param $page_callback_result
* The result of a page callback. Can be one of:
* - NULL: to indicate no content.
* - An integer menu status constant: to indicate an error condition.
* - A string of HTML content.
* - A renderable array of content.
*
* @see drupal_deliver_html_page()
*/
function ajax_deliver($page_callback_result) {
// Browsers do not allow JavaScript to read the contents of a user's local
// files. To work around that, the jQuery Form plugin submits forms containing
// a file input element to an IFRAME, instead of using XHR. Browsers do not
// normally expect JSON strings as content within an IFRAME, so the response
// must be customized accordingly.
// @see http://malsup.com/jquery/form/#file-upload
// @see Drupal.ajax.prototype.beforeSend()
$iframe_upload = !empty($_POST['ajax_iframe_upload']);
// Emit a Content-Type HTTP header if none has been added by the page callback
// or by a wrapping delivery callback.
if (is_null(drupal_get_http_header('Content-Type'))) {
if (!$iframe_upload) {
// Standard JSON can be returned to a browser's XHR object, and to
// non-browser user agents.
// @see http://www.ietf.org/rfc/rfc4627.txt?number=4627
drupal_add_http_header('Content-Type', 'application/json; charset=utf-8');
}
else {
// Browser IFRAMEs expect HTML. With most other content types, Internet
// Explorer presents the user with a download prompt.
drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
}
}
// Print the response.
$commands = ajax_prepare_response($page_callback_result);
$json = ajax_render($commands);
if (!$iframe_upload) {
// Standard JSON can be returned to a browser's XHR object, and to
// non-browser user agents.
print $json;
}
else {
// Browser IFRAMEs expect HTML. Browser extensions, such as Linkification
// and Skype's Browser Highlighter, convert URLs, phone numbers, etc. into
// links. This corrupts the JSON response. Protect the integrity of the
// JSON data by making it the value of a textarea.
// @see http://malsup.com/jquery/form/#file-upload
// @see http://drupal.org/node/1009382
print '';
}
// Perform end-of-request tasks.
ajax_footer();
}
/**
* Converts the return value of a page callback into an Ajax commands array.
*
* @param $page_callback_result
* The result of a page callback. Can be one of:
* - NULL: to indicate no content.
* - An integer menu status constant: to indicate an error condition.
* - A string of HTML content.
* - A renderable array of content.
*
* @return
* An Ajax commands array that can be passed to ajax_render().
*/
function ajax_prepare_response($page_callback_result) {
$commands = array();
if (!isset($page_callback_result)) {
// Simply delivering an empty commands array is sufficient. This results
// in the Ajax request being completed, but nothing being done to the page.
}
elseif (is_int($page_callback_result)) {
switch ($page_callback_result) {
case MENU_NOT_FOUND:
$commands[] = ajax_command_alert(t('The requested page could not be found.'));
break;
case MENU_ACCESS_DENIED:
$commands[] = ajax_command_alert(t('You are not authorized to access this page.'));
break;
case MENU_SITE_OFFLINE:
$commands[] = ajax_command_alert(filter_xss_admin(variable_get('maintenance_mode_message',
t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal'))))));
break;
}
}
elseif (is_array($page_callback_result) && isset($page_callback_result['#type']) && ($page_callback_result['#type'] == 'ajax')) {
// Complex Ajax callbacks can return a result that contains an error message
// or a specific set of commands to send to the browser.
$page_callback_result += element_info('ajax');
$error = $page_callback_result['#error'];
if (isset($error) && $error !== FALSE) {
if ((empty($error) || $error === TRUE)) {
$error = t('An error occurred while handling the request: The server received invalid input.');
}
$commands[] = ajax_command_alert($error);
}
else {
$commands = $page_callback_result['#commands'];
}
}
else {
// Like normal page callbacks, simple Ajax callbacks can return HTML
// content, as a string or render array. This HTML is inserted in some
// relationship to #ajax['wrapper'], as determined by which jQuery DOM
// manipulation method is used. The method used is specified by
// #ajax['method']. The default method is 'replaceWith', which completely
// replaces the old wrapper element and its content with the new HTML.
$html = is_string($page_callback_result) ? $page_callback_result : drupal_render($page_callback_result);
$commands[] = ajax_command_insert(NULL, $html);
// Add the status messages inside the new content's wrapper element, so that
// on subsequent Ajax requests, it is treated as old content.
$commands[] = ajax_command_prepend(NULL, theme('status_messages'));
}
return $commands;
}
/**
* Performs end-of-Ajax-request tasks.
*
* This function is the equivalent of drupal_page_footer(), but for Ajax
* requests.
*
* @see drupal_page_footer()
*/
function ajax_footer() {
// Even for Ajax requests, invoke hook_exit() implementations. There may be
// modules that need very fast Ajax responses, and therefore, run Ajax
// requests with an early bootstrap.
if (drupal_get_bootstrap_phase() == DRUPAL_BOOTSTRAP_FULL && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update')) {
module_invoke_all('exit');
}
// Commit the user session. See above comment about the possibility of this
// function running without session.inc loaded.
if (function_exists('drupal_session_commit')) {
drupal_session_commit();
}
}
/**
* Form element processing handler for the #ajax form property.
*
* @param $element
* An associative array containing the properties of the element.
*
* @return
* The processed element.
*
* @see ajax_pre_render_element()
*/
function ajax_process_form($element, &$form_state) {
$element = ajax_pre_render_element($element);
if (!empty($element['#ajax_processed'])) {
$form_state['cache'] = TRUE;
}
return $element;
}
/**
* Adds Ajax information about an element to communicate with JavaScript.
*
* If #ajax['path'] is set on an element, this additional JavaScript is added
* to the page header to attach the Ajax behaviors. See ajax.js for more
* information.
*
* @param $element
* An associative array containing the properties of the element.
* Properties used:
* - #ajax['event']
* - #ajax['prevent']
* - #ajax['path']
* - #ajax['options']
* - #ajax['wrapper']
* - #ajax['parameters']
* - #ajax['effect']
*
* @return
* The processed element with the necessary JavaScript attached to it.
*/
function ajax_pre_render_element($element) {
// Skip already processed elements.
if (isset($element['#ajax_processed'])) {
return $element;
}
// Initialize #ajax_processed, so we do not process this element again.
$element['#ajax_processed'] = FALSE;
// Nothing to do if there is neither a callback nor a path.
if (!(isset($element['#ajax']['callback']) || isset($element['#ajax']['path']))) {
return $element;
}
// Add a reasonable default event handler if none was specified.
if (isset($element['#ajax']) && !isset($element['#ajax']['event'])) {
switch ($element['#type']) {
case 'submit':
case 'button':
case 'image_button':
// Pressing the ENTER key within a textfield triggers the click event of
// the form's first submit button. Triggering Ajax in this situation
// leads to problems, like breaking autocomplete textfields, so we bind
// to mousedown instead of click.
// @see http://drupal.org/node/216059
$element['#ajax']['event'] = 'mousedown';
// Retain keyboard accessibility by setting 'keypress'. This causes
// ajax.js to trigger 'event' when SPACE or ENTER are pressed while the
// button has focus.
$element['#ajax']['keypress'] = TRUE;
// Binding to mousedown rather than click means that it is possible to
// trigger a click by pressing the mouse, holding the mouse button down
// until the Ajax request is complete and the button is re-enabled, and
// then releasing the mouse button. Set 'prevent' so that ajax.js binds
// an additional handler to prevent such a click from triggering a
// non-Ajax form submission. This also prevents a textfield's ENTER
// press triggering this button's non-Ajax form submission behavior.
if (!isset($element['#ajax']['prevent'])) {
$element['#ajax']['prevent'] = 'click';
}
break;
case 'password':
case 'textfield':
case 'textarea':
$element['#ajax']['event'] = 'blur';
break;
case 'radio':
case 'checkbox':
case 'select':
$element['#ajax']['event'] = 'change';
break;
case 'link':
$element['#ajax']['event'] = 'click';
break;
default:
return $element;
}
}
// Attach JavaScript settings to the element.
if (isset($element['#ajax']['event'])) {
$element['#attached']['library'][] = array('system', 'jquery.form');
$element['#attached']['library'][] = array('system', 'drupal.ajax');
$settings = $element['#ajax'];
// Assign default settings.
$settings += array(
'path' => 'system/ajax',
'options' => array(),
);
// @todo Legacy support. Remove in Drupal 8.
if (isset($settings['method']) && $settings['method'] == 'replace') {
$settings['method'] = 'replaceWith';
}
// Change path to URL.
$settings['url'] = url($settings['path'], $settings['options']);
unset($settings['path'], $settings['options']);
// Add special data to $settings['submit'] so that when this element
// triggers an Ajax submission, Drupal's form processing can determine which
// element triggered it.
// @see _form_element_triggered_scripted_submission()
if (isset($settings['trigger_as'])) {
// An element can add a 'trigger_as' key within #ajax to make the element
// submit as though another one (for example, a non-button can use this
// to submit the form as though a button were clicked). When using this,
// the 'name' key is always required to identify the element to trigger
// as. The 'value' key is optional, and only needed when multiple elements
// share the same name, which is commonly the case for buttons.
$settings['submit']['_triggering_element_name'] = $settings['trigger_as']['name'];
if (isset($settings['trigger_as']['value'])) {
$settings['submit']['_triggering_element_value'] = $settings['trigger_as']['value'];
}
unset($settings['trigger_as']);
}
elseif (isset($element['#name'])) {
// Most of the time, elements can submit as themselves, in which case the
// 'trigger_as' key isn't needed, and the element's name is used.
$settings['submit']['_triggering_element_name'] = $element['#name'];
// If the element is a (non-image) button, its name may not identify it
// uniquely, in which case a match on value is also needed.
// @see _form_button_was_clicked()
if (isset($element['#button_type']) && empty($element['#has_garbage_value'])) {
$settings['submit']['_triggering_element_value'] = $element['#value'];
}
}
// Convert a simple #ajax['progress'] string into an array.
if (isset($settings['progress']) && is_string($settings['progress'])) {
$settings['progress'] = array('type' => $settings['progress']);
}
// Change progress path to a full URL.
if (isset($settings['progress']['path'])) {
$settings['progress']['url'] = url($settings['progress']['path']);
unset($settings['progress']['path']);
}
$element['#attached']['js'][] = array(
'type' => 'setting',
'data' => array('ajax' => array($element['#id'] => $settings)),
);
// Indicate that Ajax processing was successful.
$element['#ajax_processed'] = TRUE;
}
return $element;
}
/**
* @} End of "defgroup ajax".
*/
/**
* @defgroup ajax_commands Ajax framework commands
* @{
* Functions to create various Ajax commands.
*
* These functions can be used to create arrays for use with the
* ajax_render() function.
*/
/**
* Creates a Drupal Ajax 'alert' command.
*
* The 'alert' command instructs the client to display a JavaScript alert
* dialog box.
*
* This command is implemented by Drupal.ajax.prototype.commands.alert()
* defined in misc/ajax.js.
*
* @param $text
* The message string to display to the user.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_alert($text) {
return array(
'command' => 'alert',
'text' => $text,
);
}
/**
* Creates a Drupal Ajax 'insert' command using the method in #ajax['method'].
*
* This command instructs the client to insert the given HTML using whichever
* jQuery DOM manipulation method has been specified in the #ajax['method']
* variable of the element that triggered the request.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_insert($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => NULL,
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'insert/replaceWith' command.
*
* The 'insert/replaceWith' command instructs the client to use jQuery's
* replaceWith() method to replace each element matched matched by the given
* selector with the given HTML.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery replaceWith() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* See
* @link http://docs.jquery.com/Manipulation/replaceWith#content jQuery replaceWith command @endlink
*/
function ajax_command_replace($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'replaceWith',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'insert/html' command.
*
* The 'insert/html' command instructs the client to use jQuery's html()
* method to set the HTML content of each element matched by the given
* selector while leaving the outer tags intact.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery html() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Attributes/html#val
*/
function ajax_command_html($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'html',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'insert/prepend' command.
*
* The 'insert/prepend' command instructs the client to use jQuery's prepend()
* method to prepend the given HTML content to the inside each element matched
* by the given selector.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery prepend() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Manipulation/prepend#content
*/
function ajax_command_prepend($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'prepend',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'insert/append' command.
*
* The 'insert/append' command instructs the client to use jQuery's append()
* method to append the given HTML content to the inside of each element matched
* by the given selector.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery append() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Manipulation/append#content
*/
function ajax_command_append($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'append',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'insert/after' command.
*
* The 'insert/after' command instructs the client to use jQuery's after()
* method to insert the given HTML content after each element matched by
* the given selector.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery after() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Manipulation/after#content
*/
function ajax_command_after($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'after',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'insert/before' command.
*
* The 'insert/before' command instructs the client to use jQuery's before()
* method to insert the given HTML content before each of elements matched by
* the given selector.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery before() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Manipulation/before#content
*/
function ajax_command_before($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'before',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'remove' command.
*
* The 'remove' command instructs the client to use jQuery's remove() method
* to remove each of elements matched by the given selector, and everything
* within them.
*
* This command is implemented by Drupal.ajax.prototype.commands.remove()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Manipulation/remove#expr
*/
function ajax_command_remove($selector) {
return array(
'command' => 'remove',
'selector' => $selector,
);
}
/**
* Creates a Drupal Ajax 'changed' command.
*
* This command instructs the client to mark each of the elements matched by the
* given selector as 'ajax-changed'.
*
* This command is implemented by Drupal.ajax.prototype.commands.changed()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $asterisk
* An optional CSS selector which must be inside $selector. If specified,
* an asterisk will be appended to the HTML inside the $asterisk selector.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_changed($selector, $asterisk = '') {
return array(
'command' => 'changed',
'selector' => $selector,
'asterisk' => $asterisk,
);
}
/**
* Creates a Drupal Ajax 'css' command.
*
* The 'css' command will instruct the client to use the jQuery css() method
* to apply the CSS arguments to elements matched by the given selector.
*
* This command is implemented by Drupal.ajax.prototype.commands.css()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $argument
* An array of key/value pairs to set in the CSS for the selector.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/CSS/css#properties
*/
function ajax_command_css($selector, $argument) {
return array(
'command' => 'css',
'selector' => $selector,
'argument' => $argument,
);
}
/**
* Creates a Drupal Ajax 'settings' command.
*
* The 'settings' command instructs the client either to use the given array as
* the settings for ajax-loaded content or to extend Drupal.settings with the
* given array, depending on the value of the $merge parameter.
*
* This command is implemented by Drupal.ajax.prototype.commands.settings()
* defined in misc/ajax.js.
*
* @param $argument
* An array of key/value pairs to add to the settings. This will be utilized
* for all commands after this if they do not include their own settings
* array.
* @param $merge
* Whether or not the passed settings in $argument should be merged into the
* global Drupal.settings on the page. By default (FALSE), the settings that
* are passed to Drupal.attachBehaviors will not include the global
* Drupal.settings.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_settings($argument, $merge = FALSE) {
return array(
'command' => 'settings',
'settings' => $argument,
'merge' => $merge,
);
}
/**
* Creates a Drupal Ajax 'data' command.
*
* The 'data' command instructs the client to attach the name=value pair of
* data to the selector via jQuery's data cache.
*
* This command is implemented by Drupal.ajax.prototype.commands.data()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $name
* The name or key (in the key value pair) of the data attached to this
* selector.
* @param $value
* The value of the data. Not just limited to strings can be any format.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Core/data#namevalue
*/
function ajax_command_data($selector, $name, $value) {
return array(
'command' => 'data',
'selector' => $selector,
'name' => $name,
'value' => $value,
);
}
/**
* Creates a Drupal Ajax 'invoke' command.
*
* The 'invoke' command will instruct the client to invoke the given jQuery
* method with the supplied arguments on the elements matched by the given
* selector. Intended for simple jQuery commands, such as attr(), addClass(),
* removeClass(), toggleClass(), etc.
*
* This command is implemented by Drupal.ajax.prototype.commands.invoke()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $method
* The jQuery method to invoke.
* @param $arguments
* (optional) A list of arguments to the jQuery $method, if any.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_invoke($selector, $method, array $arguments = array()) {
return array(
'command' => 'invoke',
'selector' => $selector,
'method' => $method,
'arguments' => $arguments,
);
}
/**
* Creates a Drupal Ajax 'restripe' command.
*
* The 'restripe' command instructs the client to restripe a table. This is
* usually used after a table has been modified by a replace or append command.
*
* This command is implemented by Drupal.ajax.prototype.commands.restripe()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_restripe($selector) {
return array(
'command' => 'restripe',
'selector' => $selector,
);
}
/**
* Creates a Drupal Ajax 'update_build_id' command.
*
* This command updates the value of a hidden form_build_id input element on a
* form. It requires the form passed in to have keys for both the old build ID
* in #build_id_old and the new build ID in #build_id.
*
* The primary use case for this Ajax command is to serve a new build ID to a
* form served from the cache to an anonymous user, preventing one anonymous
* user from accessing the form state of another anonymous users on Ajax enabled
* forms.
*
* @param $form
* The form array representing the form whose build ID should be updated.
*/
function ajax_command_update_build_id($form) {
return array(
'command' => 'updateBuildId',
'old' => $form['#build_id_old'],
'new' => $form['#build_id'],
);
}
/**
* Creates a Drupal Ajax 'add_css' command.
*
* This method will add css via ajax in a cross-browser compatible way.
*
* This command is implemented by Drupal.ajax.prototype.commands.add_css()
* defined in misc/ajax.js.
*
* @param $styles
* A string that contains the styles to be added.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see misc/ajax.js
*/
function ajax_command_add_css($styles) {
return array(
'command' => 'add_css',
'data' => $styles,
);
}
www-test-master-includes/includes/archiver.inc 0000664 0000000 0000000 00000003245 12501305533 0022061 0 ustar 00root root 0000000 0000000 '',
'#markup' => t('WARNING: You are not using an encrypted connection, so your password will be sent in plain text.
Learn more.', array('@https-link' => 'http://drupal.org/https-information')),
'#suffix' => '
',
);
}
// Decide on a default backend.
if (isset($form_state['values']['connection_settings']['authorize_filetransfer_default'])) {
$authorize_filetransfer_default = $form_state['values']['connection_settings']['authorize_filetransfer_default'];
}
elseif ($authorize_filetransfer_default = variable_get('authorize_filetransfer_default', NULL));
else {
$authorize_filetransfer_default = key($available_backends);
}
$form['information']['main_header'] = array(
'#prefix' => '',
'#markup' => t('To continue, provide your server connection details'),
'#suffix' => '
',
);
$form['connection_settings']['#tree'] = TRUE;
$form['connection_settings']['authorize_filetransfer_default'] = array(
'#type' => 'select',
'#title' => t('Connection method'),
'#default_value' => $authorize_filetransfer_default,
'#weight' => -10,
);
/*
* Here we create two submit buttons. For a JS enabled client, they will
* only ever see submit_process. However, if a client doesn't have JS
* enabled, they will see submit_connection on the first form (when picking
* what filetransfer type to use, and submit_process on the second one (which
* leads to the actual operation).
*/
$form['submit_connection'] = array(
'#prefix' => "
",
'#name' => 'enter_connection_settings',
'#type' => 'submit',
'#value' => t('Enter connection settings'),
'#weight' => 100,
);
$form['submit_process'] = array(
'#name' => 'process_updates',
'#type' => 'submit',
'#value' => t('Continue'),
'#weight' => 100,
'#attributes' => array('style' => 'display:none'),
);
// Build a container for each connection type.
foreach ($available_backends as $name => $backend) {
$form['connection_settings']['authorize_filetransfer_default']['#options'][$name] = $backend['title'];
$form['connection_settings'][$name] = array(
'#type' => 'container',
'#attributes' => array('class' => array("filetransfer-$name", 'filetransfer')),
);
// We can't use #prefix on the container itself since then the header won't
// be hidden and shown when the containers are being manipulated via JS.
$form['connection_settings'][$name]['header'] = array(
'#markup' => '' . t('@backend connection settings', array('@backend' => $backend['title'])) . '
',
);
$form['connection_settings'][$name] += _authorize_filetransfer_connection_settings($name);
// Start non-JS code.
if (isset($form_state['values']['connection_settings']['authorize_filetransfer_default']) && $form_state['values']['connection_settings']['authorize_filetransfer_default'] == $name) {
// If the user switches from JS to non-JS, Drupal (and Batch API) will
// barf. This is a known bug: http://drupal.org/node/229825.
setcookie('has_js', '', time() - 3600, '/');
unset($_COOKIE['has_js']);
// Change the submit button to the submit_process one.
$form['submit_process']['#attributes'] = array();
unset($form['submit_connection']);
// Activate the proper filetransfer settings form.
$form['connection_settings'][$name]['#attributes']['style'] = 'display:block';
// Disable the select box.
$form['connection_settings']['authorize_filetransfer_default']['#disabled'] = TRUE;
// Create a button for changing the type of connection.
$form['connection_settings']['change_connection_type'] = array(
'#name' => 'change_connection_type',
'#type' => 'submit',
'#value' => t('Change connection type'),
'#weight' => -5,
'#attributes' => array('class' => array('filetransfer-change-connection-type')),
);
}
// End non-JS code.
}
return $form;
}
/**
* Generates the Form API array for a given connection backend's settings.
*
* @param $backend
* The name of the backend (e.g. 'ftp', 'ssh', etc).
*
* @return
* Form API array of connection settings for the given backend.
*
* @see hook_filetransfer_backends()
*/
function _authorize_filetransfer_connection_settings($backend) {
$defaults = variable_get('authorize_filetransfer_connection_settings_' . $backend, array());
$form = array();
// Create an instance of the file transfer class to get its settings form.
$filetransfer = authorize_get_filetransfer($backend);
if ($filetransfer) {
$form = $filetransfer->getSettingsForm();
}
// Fill in the defaults based on the saved settings, if any.
_authorize_filetransfer_connection_settings_set_defaults($form, NULL, $defaults);
return $form;
}
/**
* Sets the default settings on a file transfer connection form recursively.
*
* The default settings for the file transfer connection forms are saved in
* the database. The settings are stored as a nested array in the case of a
* settings form that has fieldsets or otherwise uses a nested structure.
* Therefore, to properly add defaults, we need to walk through all the
* children form elements and process those defaults recursively.
*
* @param $element
* Reference to the Form API form element we're operating on.
* @param $key
* The key for our current form element, if any.
* @param array $defaults
* The default settings for the file transfer backend we're operating on.
*/
function _authorize_filetransfer_connection_settings_set_defaults(&$element, $key, array $defaults) {
// If we're operating on a form element which isn't a fieldset, and we have
// a default setting saved, stash it in #default_value.
if (!empty($key) && isset($defaults[$key]) && isset($element['#type']) && $element['#type'] != 'fieldset') {
$element['#default_value'] = $defaults[$key];
}
// Now, we walk through all the child elements, and recursively invoke
// ourself on each one. Since the $defaults settings array can be nested
// (because of #tree, any values inside fieldsets will be nested), if
// there's a subarray of settings for the form key we're currently
// processing, pass in that subarray to the recursive call. Otherwise, just
// pass on the whole $defaults array.
foreach (element_children($element) as $child_key) {
_authorize_filetransfer_connection_settings_set_defaults($element[$child_key], $child_key, ((isset($defaults[$key]) && is_array($defaults[$key])) ? $defaults[$key] : $defaults));
}
}
/**
* Form validation handler for authorize_filetransfer_form().
*
* @see authorize_filetransfer_form()
* @see authorize_filetransfer_submit()
*/
function authorize_filetransfer_form_validate($form, &$form_state) {
// Only validate the form if we have collected all of the user input and are
// ready to proceed with updating or installing.
if ($form_state['triggering_element']['#name'] != 'process_updates') {
return;
}
if (isset($form_state['values']['connection_settings'])) {
$backend = $form_state['values']['connection_settings']['authorize_filetransfer_default'];
$filetransfer = authorize_get_filetransfer($backend, $form_state['values']['connection_settings'][$backend]);
try {
if (!$filetransfer) {
throw new Exception(t('Error, this type of connection protocol (%backend) does not exist.', array('%backend' => $backend)));
}
$filetransfer->connect();
}
catch (Exception $e) {
// The format of this error message is similar to that used on the
// database connection form in the installer.
form_set_error('connection_settings', t('Failed to connect to the server. The server reports the following message: !message For more help installing or updating code on your server, see the handbook.', array(
'!message' => '' . $e->getMessage() . '
',
'@handbook_url' => 'http://drupal.org/documentation/install/modules-themes',
)));
}
}
}
/**
* Form submission handler for authorize_filetransfer_form().
*
* @see authorize_filetransfer_form()
* @see authorize_filetransfer_validate()
*/
function authorize_filetransfer_form_submit($form, &$form_state) {
global $base_url;
switch ($form_state['triggering_element']['#name']) {
case 'process_updates':
// Save the connection settings to the DB.
$filetransfer_backend = $form_state['values']['connection_settings']['authorize_filetransfer_default'];
// If the database is available then try to save our settings. We have
// to make sure it is available since this code could potentially (will
// likely) be called during the installation process, before the
// database is set up.
try {
$connection_settings = array();
foreach ($form_state['values']['connection_settings'][$filetransfer_backend] as $key => $value) {
// We do *not* want to store passwords in the database, unless the
// backend explicitly says so via the magic #filetransfer_save form
// property. Otherwise, we store everything that's not explicitly
// marked with #filetransfer_save set to FALSE.
if (!isset($form['connection_settings'][$filetransfer_backend][$key]['#filetransfer_save'])) {
if ($form['connection_settings'][$filetransfer_backend][$key]['#type'] != 'password') {
$connection_settings[$key] = $value;
}
}
// The attribute is defined, so only save if set to TRUE.
elseif ($form['connection_settings'][$filetransfer_backend][$key]['#filetransfer_save']) {
$connection_settings[$key] = $value;
}
}
// Set this one as the default authorize method.
variable_set('authorize_filetransfer_default', $filetransfer_backend);
// Save the connection settings minus the password.
variable_set('authorize_filetransfer_connection_settings_' . $filetransfer_backend, $connection_settings);
$filetransfer = authorize_get_filetransfer($filetransfer_backend, $form_state['values']['connection_settings'][$filetransfer_backend]);
// Now run the operation.
authorize_run_operation($filetransfer);
}
catch (Exception $e) {
// If there is no database available, we don't care and just skip
// this part entirely.
}
break;
case 'enter_connection_settings':
$form_state['rebuild'] = TRUE;
break;
case 'change_connection_type':
$form_state['rebuild'] = TRUE;
unset($form_state['values']['connection_settings']['authorize_filetransfer_default']);
break;
}
}
/**
* Runs the operation specified in $_SESSION['authorize_operation'].
*
* @param $filetransfer
* The FileTransfer object to use for running the operation.
*/
function authorize_run_operation($filetransfer) {
$operation = $_SESSION['authorize_operation'];
unset($_SESSION['authorize_operation']);
if (!empty($operation['page_title'])) {
drupal_set_title($operation['page_title']);
}
require_once DRUPAL_ROOT . '/' . $operation['file'];
call_user_func_array($operation['callback'], array_merge(array($filetransfer), $operation['arguments']));
}
/**
* Gets a FileTransfer class for a specific transfer method and settings.
*
* @param $backend
* The FileTransfer backend to get the class for.
* @param $settings
* Array of settings for the FileTransfer.
*
* @return
* An instantiated FileTransfer object for the requested method and settings,
* or FALSE if there was an error finding or instantiating it.
*/
function authorize_get_filetransfer($backend, $settings = array()) {
$filetransfer = FALSE;
if (!empty($_SESSION['authorize_filetransfer_info'][$backend])) {
$backend_info = $_SESSION['authorize_filetransfer_info'][$backend];
if (!empty($backend_info['file'])) {
$file = $backend_info['file path'] . '/' . $backend_info['file'];
require_once $file;
}
if (class_exists($backend_info['class'])) {
// PHP 5.2 doesn't support $class::factory() syntax, so we have to
// use call_user_func_array() until we can require PHP 5.3.
$filetransfer = call_user_func_array(array($backend_info['class'], 'factory'), array(DRUPAL_ROOT, $settings));
}
}
return $filetransfer;
}
www-test-master-includes/includes/batch.inc 0000664 0000000 0000000 00000042131 12501305533 0021334 0 ustar 00root root 0000000 0000000 $id,
':token' => drupal_get_token($id),
))->fetchField();
if ($batch) {
return unserialize($batch);
}
return FALSE;
}
/**
* Renders the batch processing page based on the current state of the batch.
*
* @see _batch_shutdown()
*/
function _batch_page() {
$batch = &batch_get();
if (!isset($_REQUEST['id'])) {
return FALSE;
}
// Retrieve the current state of the batch.
if (!$batch) {
$batch = batch_load($_REQUEST['id']);
if (!$batch) {
drupal_set_message(t('No active batch.'), 'error');
drupal_goto();
}
}
// Register database update for the end of processing.
drupal_register_shutdown_function('_batch_shutdown');
// Add batch-specific CSS.
foreach ($batch['sets'] as $batch_set) {
if (isset($batch_set['css'])) {
foreach ($batch_set['css'] as $css) {
drupal_add_css($css);
}
}
}
$op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
$output = NULL;
switch ($op) {
case 'start':
$output = _batch_start();
break;
case 'do':
// JavaScript-based progress page callback.
_batch_do();
break;
case 'do_nojs':
// Non-JavaScript-based progress page.
$output = _batch_progress_page_nojs();
break;
case 'finished':
$output = _batch_finished();
break;
}
return $output;
}
/**
* Initializes the batch processing.
*
* JavaScript-enabled clients are identified by the 'has_js' cookie set in
* drupal.js. If no JavaScript-enabled page has been visited during the current
* user's browser session, the non-JavaScript version is returned.
*/
function _batch_start() {
if (isset($_COOKIE['has_js']) && $_COOKIE['has_js']) {
return _batch_progress_page_js();
}
else {
return _batch_progress_page_nojs();
}
}
/**
* Outputs a batch processing page with JavaScript support.
*
* This initializes the batch and error messages. Note that in JavaScript-based
* processing, the batch processing page is displayed only once and updated via
* AHAH requests, so only the first batch set gets to define the page title.
* Titles specified by subsequent batch sets are not displayed.
*
* @see batch_set()
* @see _batch_do()
*/
function _batch_progress_page_js() {
$batch = batch_get();
$current_set = _batch_current_set();
drupal_set_title($current_set['title'], PASS_THROUGH);
// Merge required query parameters for batch processing into those provided by
// batch_set() or hook_batch_alter().
$batch['url_options']['query']['id'] = $batch['id'];
$js_setting = array(
'batch' => array(
'errorMessage' => $current_set['error_message'] . '
' . $batch['error_message'],
'initMessage' => $current_set['init_message'],
'uri' => url($batch['url'], $batch['url_options']),
),
);
drupal_add_js($js_setting, 'setting');
drupal_add_library('system', 'drupal.batch');
return '';
}
/**
* Does one execution pass with JavaScript and returns progress to the browser.
*
* @see _batch_progress_page_js()
* @see _batch_process()
*/
function _batch_do() {
// HTTP POST required.
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
drupal_set_message(t('HTTP POST is required.'), 'error');
drupal_set_title(t('Error'));
return '';
}
// Perform actual processing.
list($percentage, $message) = _batch_process();
drupal_json_output(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message));
}
/**
* Outputs a batch processing page without JavaScript support.
*
* @see _batch_process()
*/
function _batch_progress_page_nojs() {
$batch = &batch_get();
$current_set = _batch_current_set();
drupal_set_title($current_set['title'], PASS_THROUGH);
$new_op = 'do_nojs';
if (!isset($batch['running'])) {
// This is the first page so we return some output immediately.
$percentage = 0;
$message = $current_set['init_message'];
$batch['running'] = TRUE;
}
else {
// This is one of the later requests; do some processing first.
// Error handling: if PHP dies due to a fatal error (e.g. a nonexistent
// function), it will output whatever is in the output buffer, followed by
// the error message.
ob_start();
$fallback = $current_set['error_message'] . '
' . $batch['error_message'];
$fallback = theme('maintenance_page', array('content' => $fallback, 'show_messages' => FALSE));
// We strip the end of the page using a marker in the template, so any
// additional HTML output by PHP shows up inside the page rather than below
// it. While this causes invalid HTML, the same would be true if we didn't,
// as content is not allowed to appear after