/**
 *	this is the default (fallback) controller
 *
 *	it mainly invokes other controllers or sends error messages
 */
function controller_default($args)
{
    if (empty($args[0][0]) && empty($args[0][1])) {
        // take the default page
        $args[0][0] = startpage();
        log_msg('debug', 'controller_default: using the default page');
    } elseif ($args[0][0] == 'edit' && empty($args[0][1])) {
        // quirk: edit the default page
        $args[0][0] = startpage();
        $args[0][1] = 'edit';
        log_msg('debug', 'controller_default: using the default page');
        invoke_controller($args);
        return;
    }
    page_canonical($args[0][0]);
    $obj = expl('.', $args[0][0]);
    if (count($obj) == 2) {
        // page requested
        if (page_exists($args[0][0])) {
            if (DEFAULT_TO_EDIT && is_auth()) {
                log_msg('debug', 'controller_default: invoking controller_edit');
                controller_edit($args);
            } else {
                log_msg('debug', 'controller_default: invoking controller_show');
                controller_show($args);
            }
        } elseif (ALWAYS_PROMPT_CREATE_PAGE || is_auth() || $args[0][0] == startpage()) {
            log_msg('debug', 'controller_default: invoking controller_create_page');
            controller_create_page($args);
        } else {
            log_msg('info', 'controller_default: page ' . quot($args[0][0]) . ' not found, serving 404');
            hotglue_error(404);
        }
    } else {
        // possibly object requested
        if (object_exists($args[0][0])) {
            // try to serve upload
            if (isset($args['download']) && $args['download']) {
                // prompt file save dialog on client
                $dl = true;
            } else {
                $dl = false;
            }
            log_msg('debug', 'controller_default: invoking serve_resource');
            if (!serve_resource($args[0][0], $dl)) {
                log_msg('info', 'controller_default: object ' . quot($args[0][0]) . ' has no associated resource, serving 404');
                hotglue_error(404);
            }
        } else {
            log_msg('info', 'controller_default: object ' . quot($args[0][0]) . ' not found, serving 404');
            hotglue_error(404);
        }
    }
}
Example #2
0
/**
 *	save the state of a html element corresponding to an object to disk
 *
 *	this function takes the object lock.
 *	@param array $args arguments
 *		key 'html' one html element
 *	@return array response
 *		true if successful
 */
function save_state($args)
{
    if (empty($args['html'])) {
        return response('Required argument "html" missing or empty', 400);
    }
    require_once 'html.inc.php';
    require_once 'html_parse.inc.php';
    $elem = html_parse_elem($args['html']);
    if (!elem_has_class($elem, 'object')) {
        return response('Error saving state as class "object" is not set', 400);
    } elseif (!object_exists(elem_attr($elem, 'id'))) {
        return response('Error saving state as object does not exist', 404);
    }
    // LOCK
    $L = _obj_lock(elem_attr($elem, 'id'), LOCK_TIME);
    if ($L === false) {
        return response('Could not acquire lock to ' . quot($args['name']) . ' in ' . LOCK_TIME . 'ms', 500);
    }
    $obj = load_object(array('name' => elem_attr($elem, 'id')));
    if ($obj['#error']) {
        // UNLOCK
        _obj_unlock($L);
        return response('Error saving state, cannot load ' . quot(elem_attr($elem, 'id')), 500);
    } else {
        $obj = $obj['#data'];
    }
    $ret = invoke_hook_while('save_state', false, array('elem' => $elem, 'obj' => $obj));
    // UNLOCK
    _obj_unlock($L);
    if (count($ret) == 0) {
        return response('Error saving state as nobody claimed element', 500);
    } else {
        $temp = array_keys($ret);
        log_msg('info', 'save_state: ' . quot($obj['name']) . ' was handled by ' . quot($temp[0]));
        return response(true);
    }
}