Exemplo n.º 1
0
function handle_posted_file($filename = "", $att, $profile)
{
    global $db, $request, $response;
    $response->set_var('profile', $profile);
    load_apps();
    if (isset($_FILES['media']['tmp_name'])) {
        $table = 'uploads';
    } else {
        $table = 'posts';
    }
    $modelvar = classify($table);
    $_FILES = array(strtolower($modelvar) => array('name' => array('attachment' => $filename), 'tmp_name' => array('attachment' => $att)));
    $Post =& $db->model('Post');
    $Upload =& $db->model('Upload');
    $field = 'attachment';
    $request->set_param('resource', $table);
    $request->set_param(array(strtolower(classify($table)), $field), $att);
    trigger_before('insert_from_post', ${$modelvar}, $request);
    $content_type = 'text/html';
    $rec = ${$modelvar}->base();
    $content_type = type_of($filename);
    $rec->set_value('profile_id', get_profile_id());
    $rec->set_value('parent_id', 0);
    if (isset($request->params['message'])) {
        $rec->set_value('title', $request->params['message']);
    } else {
        $rec->set_value('title', '');
    }
    if ($table == 'uploads') {
        $rec->set_value('tmp_name', 'new');
    }
    $upload_types = environment('upload_types');
    if (!$upload_types) {
        $upload_types = array('jpg', 'jpeg', 'png', 'gif');
    }
    $ext = extension_for(type_of($filename));
    if (!in_array($ext, $upload_types)) {
        trigger_error('Sorry, this site only allows the following file types: ' . implode(',', $upload_types), E_USER_ERROR);
    }
    $rec->set_value($field, $att);
    $rec->save_changes();
    $tmp = $att;
    if (is_jpg($tmp)) {
        $thumbsize = environment('max_pixels');
        $Thumbnail =& $db->model('Thumbnail');
        $t = $Thumbnail->base();
        $newthumb = tempnam("/tmp", "new" . $rec->id . ".jpg");
        resize_jpeg($tmp, $newthumb, $thumbsize);
        $t->set_value('target_id', $atomentry->id);
        $t->save_changes();
        update_uploadsfile('thumbnails', $t->id, $newthumb);
        $t->set_etag();
    }
    $atomentry = ${$modelvar}->set_metadata($rec, $content_type, $table, 'id');
    ${$modelvar}->set_categories($rec, $request, $atomentry);
    $url = $request->url_for(array('resource' => $table, 'id' => $rec->id));
    //	$title = substr($rec->title,0,140);
    //	$over = ((strlen($title) + strlen($url) + 1) - 140);
    //	if ($over > 0)
    //	  $rec->set_value('title',substr($title,0,-$over)." ".$url);
    //	else
    //	  $rec->set_value('title',$title." ".$url);
    //	$rec->save_changes();
    trigger_after('insert_from_post', ${$modelvar}, $rec);
    return true;
}
Exemplo n.º 2
0
function post(&$vars)
{
    extract($vars);
    global $request;
    $modelvar = classify($request->resource);
    trigger_before('insert_from_post', ${$modelvar}, $request);
    $table = $request->resource;
    $content_type = 'text/html';
    $rec = ${$modelvar}->base();
    if (!${$modelvar}->can_create($table)) {
        trigger_error("Sorry, you do not have permission to " . $request->action . " " . $table, E_USER_ERROR);
    }
    $fields = ${$modelvar}->fields_from_request($request);
    $fieldlist = $fields[$table];
    foreach ($fieldlist as $field => $type) {
        if (${$modelvar}->has_metadata && is_blob($table . '.' . $field)) {
            if (isset($_FILES[strtolower(classify($table))]['name'][$field])) {
                $content_type = type_of($_FILES[strtolower(classify($table))]['name'][$field]);
            }
        }
        $rec->set_value($field, $request->params[strtolower(classify($table))][$field]);
    }
    $rec->set_value('profile_id', get_profile_id());
    $result = $rec->save_changes();
    if (!$result) {
        trigger_error("The record could not be saved into the database.", E_USER_ERROR);
    }
    $atomentry = ${$modelvar}->set_metadata($rec, $content_type, $table, 'id');
    ${$modelvar}->set_categories($rec, $request, $atomentry);
    if (is_upload($table, 'attachment')) {
        $upload_types = environment('upload_types');
        if (!$upload_types) {
            $upload_types = array('jpg', 'jpeg', 'png', 'gif');
        }
        $ext = extension_for(type_of($_FILES[strtolower(classify($table))]['name']['attachment']));
        if (!in_array($ext, $upload_types)) {
            trigger_error('Sorry, this site only allows the following file types: ' . implode(',', $upload_types), E_USER_ERROR);
        }
        $url = $request->url_for(array('resource' => $table, 'id' => $rec->id));
        $title = substr($rec->title, 0, 140);
        $over = strlen($title) + strlen($url) + 1 - 140;
        if ($over > 0) {
            $rec->set_value('title', substr($title, 0, -$over) . " " . $url);
        } else {
            $rec->set_value('title', $title . " " . $url);
        }
        $rec->save_changes();
        $tmp = $_FILES[strtolower(classify($table))]['tmp_name']['attachment'];
        if (is_jpg($tmp)) {
            $thumbsize = environment('max_pixels');
            $Thumbnail =& $db->model('Thumbnail');
            $t = $Thumbnail->base();
            $newthumb = tempnam("/tmp", "new" . $rec->id . ".jpg");
            resize_jpeg($tmp, $newthumb, $thumbsize);
            $t->set_value('target_id', $atomentry->id);
            $t->save_changes();
            update_uploadsfile('thumbnails', $t->id, $newthumb);
            $t->set_etag();
        }
    }
    trigger_after('insert_from_post', ${$modelvar}, $rec);
    header_status('201 Created');
    redirect_to($request->base);
}