Skip to content
This repository
tree: f17a7083db
Fetching contributors…

Cannot retrieve contributors at this time

file 104 lines (86 sloc) 2.983 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
<?php
/**
* ObjectiveWeb
*
* Main Controller, which exposes all the configured domains
*
* User: guigouz
* Date: 12/05/11
* Time: 19:27
*/

// Load ObjectiveWeb
require_once "_init.php";

// Mandatory aboutbox
route('GET /?', 'ow_version');

// Views before attachments
// /domain/_view
route('GET /(\w*/_\w*/?.*)', 'fetch', $_GET);
//route('POST /(\w*)/_(\w*)/?', 'handle_view');
//route('PUT /(\w*)/_(\w*)/?', 'handle_view');

// Attachments before, because they may use php://stdin
route('POST /(\w+)/([\w-]+)?', 'handle_attachment_post');
route('POST /(\w+)/([\w-]+)/([\w-.\ ]+)', 'handle_attachment_post');
route('PUT /(\w+)/([\w-]+)/([\w-.\ ]+)', 'handle_attachment_post');
route('DELETE /(\w+)/([\w-]+)/([\w-.\ ]+)', 'handle_attachment_delete');
route('GET /(\w+)/([\w-]+)/([\w-.\ ]+)', 'handle_attachment_get');

$body = parse_post_body();

// /domain
route('GET /(\w+)/?', 'fetch', $_GET);
route('POST /(\w*)/?', 'post', $body);


// /domain/id
route('GET /(\w+)/([\w-]+)/?', 'get', $_GET);
route('PUT /(\w+)/([\w-]+)/?', 'put', $body);
route('DELETE /(\w+)/([\w-]+)/?', 'delete');

route('GET /(\w+)/([\w-]+)/_(\w+)', 'handle_plugin');
//route('POST /(\w*)/(\w*)/_(\w*)', 'handle_plugin');
//route('PUT /(\w*)/(\w*)/_(\w*)', 'handle_plugin');
//route('DELETE /(\w*)/(\w*)/_(\w*)', 'handle_plugin');


function handle_attachment_delete($domain, $id, $attachment) {
    attachment_delete($domain, $id, $attachment);
}

function handle_attachment_get($domain, $id, $attachment) {
    // TODO set header
    $fp = attachment_open($domain, $id, $attachment);
    fpassthru($fp);
    fclose($fp);
    exit;
}

function handle_attachment_post($domain, $id, $attachment_id = null) {
    $files = array();
    if ($attachment_id) {
        // TODO if $_FILES put($domain,$id, $attachment_id, fopen($_FILES['tmp_file']), $metadata);
        if(empty($_FILES)) {
            $files[] = attach($domain, $id, $attachment_id, $fp = fopen('php://input', "rb"), ATTACHMENT_OVERWRITE);
            fclose($fp);
        }
        else {
            throw new Exception('Not implemented', 500);
        }
    }
    else {
        foreach ($_FILES as $key => $fileinput) {
            if (!is_array($fileinput['name'])) {
                // single file

                $files[] = attach_local($domain, $id, $key, $fileinput['tmp_name'], ATTACHMENT_UNLINK);
            }
            else {
                // array of files
                foreach ($fileinput['name'] as $file => $filename) {

                    $files[] = attach_local($domain, $id, is_numeric($file) ? $fileinput['name'][$file] : $key, $fileinput['tmp_name'][$file], ATTACHMENT_UNLINK);
                }
            }
        }
    }

    respond($files);
}

function handle_plugin($domain, $id, $plugin) {
    if($plugin == 'attachments') {
        respond(attachment_list($domain, $id));
    }
    else {
        respond('Invalid plugin', 404);
    }
}

function handle_view($path) {
    fetch($path, $_GET);
}
Something went wrong with that request. Please try again.