function preview(&$vars) { extract($vars); $model =& $db->get_table($request->resource); $Entry =& $db->model('Entry'); $p = $model->find($request->id); $e = $Entry->find($p->entry_id); $t = $Thumbnail->find_by('target_id', $e->id); if ($t) { $request->set_param('resource', 'thumbnails'); $request->set_param('id', $t->id); render_blob($t->attachment, extension_for($e->content_type)); } else { render_blob($p->attachment, extension_for($e->content_type)); } }
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; }
function delete_from_post(&$req) { trigger_before('delete_from_post', $this, $req); global $db; if ($this->has_metadata && !isset($req->params['entry']['etag'])) { trigger_error("Sorry, the etag was not submitted with the database entry", E_USER_ERROR); } $fields = $this->fields_from_request($req); if ($this->has_metadata) { $atomentry = $db->models['entries']->find_by('etag', $req->params['entry']['etag']); $recid = $atomentry->attributes['record_id']; } else { $recid = $req->id; } $rec = $this->find($recid); if ($this->has_metadata) { $Person =& $db->model('Person'); $Group =& $db->model('Group'); $p = $Person->find(get_person_id()); if (!($p->id == $atomentry->attributes['person_id']) && !$this->can_superuser($req->resource)) { trigger_error("Sorry, your id does not match the owner of the database entry", E_USER_ERROR); } } $coll = environment('collection_cache'); if ($this->has_metadata && isset($coll[$req->resource]) && $coll[$req->resource]['location'] == 'aws') { $ext = extension_for($atomentry->content_type); $pkname = $rec->primary_key; global $prefix; $aws_file = $prefix . $rec->table . $rec->{$pkname} . "." . $ext; lib_include('S3'); $s3 = new S3(environment('awsAccessKey'), environment('awsSecretKey')); if (!$s3) { trigger_error('Sorry, there was a problem connecting to Amazon Web Services', E_USER_ERROR); } if ($s3->getBucket(environment('awsBucket')) && $s3->getObject(environment('awsBucket'), urlencode($aws_file))) { $result = $s3->deleteObject(environment('awsBucket'), urlencode($aws_file)); if (!$result) { trigger_error('Sorry, there was a problem deleting the file from Amazon Web Services', E_USER_ERROR); } } } $result = $db->delete_record($rec); trigger_after('delete_from_post', $this, $req); }
function preview(&$vars) { extract($vars); $p = $Post->find($request->id); $e = $p->FirstChild('entries'); $t = $Thumbnail->find_by('target_id', $e->id); if ($t) { $request->set_param('resource', 'thumbnails'); $request->set_param('id', $t->id); render_blob($t->attachment, extension_for($e->content_type)); } else { render_blob($p->attachment, extension_for($e->content_type)); } }
function do_ajaxy_fileupload(&$request, &$route) { global $db; if (!isset($_FILES['Filedata']['name'])) { return; } if (!is_writable('cache')) { exit; } $result = $db->get_result("DELETE FROM " . $db->prefix . "uploads WHERE name = '" . $db->escape_string(urldecode($_FILES['Filedata']['name'])) . "'"); $tmp = 'cache' . DIRECTORY_SEPARATOR . make_token(); $tmp .= "." . extension_for(type_of($_FILES['Filedata']['name'])); $Upload =& $db->model('Upload'); $u = $Upload->base(); $u->set_value('name', urldecode($_FILES['Filedata']['name'])); $u->set_value('tmp_name', $tmp); $u->save_changes(); move_uploaded_file($_FILES['Filedata']['tmp_name'], $tmp); echo "200 OK"; exit; }
function aws_putfile(&$rec, $pkvalue) { global $request, $prefix; $file = $prefix . $rec->table . $pkvalue . "." . extension_for(type_of($_FILES[strtolower(classify($rec->table))]['name'][$this->file_upload[0]])); lib_include('S3'); $s3 = new S3(environment('awsAccessKey'), environment('awsSecretKey')); if (!$s3) { trigger_error('Sorry, there was a problem connecting to Amazon Web Services', E_USER_ERROR); } if (!$s3->getBucket(environment('awsBucket'))) { $result = $s3->putBucket(environment('awsBucket'), 'public-read'); if (!$result) { trigger_error('Sorry, there was a problem creating the bucket ' . environment('awsBucket') . ' at Amazon Web Services', E_USER_ERROR); } } if (file_exists($this->file_upload[1])) { if (!$s3->putObjectFile($this->file_upload[1], environment('awsBucket'), $file, 'public-read')) { trigger_error('Sorry, there was a problem uploading the file to Amazon Web Services', E_USER_ERROR); } unlink($this->file_upload[1]); } $this->file_upload = false; }