/** returns type on success, false on failure */ public static function isAcceptable($content_type) { $ok_type = false; try { list($type, $subtype) = Dase_Media::parseMimeType($content_type); } catch (Exception $e) { return false; } foreach (Dase_Media::getAcceptedTypes() as $t) { list($acceptedType, $acceptedSubtype) = explode('/', $t); if ($acceptedType == '*' || $acceptedType == $type) { if ($acceptedSubtype == '*' || $acceptedSubtype == $subtype) { $ok_type = $type . "/" . $subtype; } } } return $ok_type; }
public function getWebspace($r) { $ws_path = $r->get('webspace_path'); $webspace_url = 'https://webspace.utexas.edu/' . $ws_path; $rss_data = @file_get_contents($webspace_url . '?view=RSS'); $files = array(); $paths = array(); $invalid_files = array(); if ($rss_data) { $rss = new DOMDocument(); $rss->loadXML($rss_data); $good_type = ''; foreach ($rss->getElementsByTagName('item') as $item) { $enc = $item->getElementsByTagName('enclosure')->item(0); if ($enc) { $file['url'] = $enc->getAttribute('url'); $file['length'] = ceil((int) $enc->getAttribute('length') / 1000); $file['type'] = $enc->getAttribute('type'); $file['name'] = trim(urldecode(str_replace($webspace_url, '', $file['url'])), '/'); $good_type = Dase_Media::isAcceptable($file['type']); } if ($good_type) { $files[] = $file; } else { if ($enc) { $invalid_files[] = $file; } else { $path_href = $item->getElementsByTagName('link')->item(0)->firstChild->substringData(0, 200); $path['path_rel'] = str_replace('https://webspace.utexas.edu/', '', $path_href); $path['path_name'] = $item->getElementsByTagName('title')->item(0)->firstChild->substringData(0, 200); $paths[] = $path; } } } } $tpl = new Dase_Template($r, true); $tpl->assign('collection', $this->collection); $tpl->assign('files', $files); $tpl->assign('invalid_files', $invalid_files); $tpl->assign('paths', $paths); $tpl->assign('webspace_path', $ws_path); $r->set('tab', 'module'); $r->renderResponse($tpl->fetch('index.tpl')); }
public function getContentType() { if (isset($_SERVER['CONTENT_TYPE'])) { $header = $_SERVER['CONTENT_TYPE']; } if (isset($_SERVER['HTTP_CONTENT_TYPE'])) { $header = $_SERVER['HTTP_CONTENT_TYPE']; } if (isset($header)) { list($type, $subtype, $params) = Dase_Media::parseMimeType($header); if (isset($params['type'])) { return $type . '/' . $subtype . ';type=' . $params['type']; } else { return $type . '/' . $subtype; } } }
/** lots of duplicated code here -- need to refactor */ private function _processFile($r) { $content_type = $r->getContentType(); if (!Dase_Media::isAcceptable($content_type)) { $r->renderError(415, 'cannot accept ' . $content_type); } $bits = $r->getBody(); $file_meta = Dase_File::$types_map[$content_type]; $ext = $file_meta['ext']; $title = ''; if ($r->http_title) { $title = $r->http_title; } elseif ($r->slug) { $title = $r->slug; } else { $title = dechex(time()); } $base_dir = $this->config->getMediaDir(); $basename = $this->_findUniqueName(Dase_Util::dirify($title)); $newname = $this->_findNextUnique($base_dir, $basename, $ext); $new_path = $base_dir . '/' . $newname; $ifp = @fopen($new_path, 'wb'); if (!$ifp) { Dase_Log::debug(LOG_FILE, 'cannot write file ' . $new_path); $r->renderError(500, 'cannot write file ' . $new_path); } @fwrite($ifp, $bits); fclose($ifp); @chmod($new_file, 0775); //create new item $item = new Dase_DBO_Item($this->db); $item->title = $title; $size = @getimagesize($new_path); $item->name = $newname; $item->file_url = 'file/' . $item->name; $item->filesize = filesize($new_path); $item->mime = $content_type; $mime_type = $item->mime; $parts = explode('/', $mime_type); if (isset($parts[0]) && 'image' == $parts[0]) { $thumb_path = $base_dir . '/thumb/' . $newname; $thumb_path = str_replace('.' . $ext, '.jpg', $thumb_path); $command = CONVERT . " \"{$new_path}\" -format jpeg -resize '100x100 >' -colorspace RGB {$thumb_path}"; $exec_output = array(); $results = exec($command, $exec_output); if (!file_exists($thumb_path)) { //Dase_Log::info(LOG_FILE,"failed to write $thumb_path"); } chmod($thumb_path, 0775); $newname = str_replace('.' . $ext, '.jpg', $newname); $item->thumbnail_url = 'file/thumb/' . $newname; } else { $item->thumbnail_url = 'www/images/mime_icons/' . Dase_File::$types_map[$mime_type]['size'] . '.png'; } if (isset($size[0]) && $size[0]) { $item->width = $size[0]; } if (isset($size[1]) && $size[1]) { $item->height = $size[1]; } $item->created_by = $this->user->eid; $item->created = date(DATE_ATOM); $item->updated_by = $this->user->eid; $item->updated = date(DATE_ATOM); $item->url = 'item/' . $item->name; if ($item->insert()) { $r->renderOk('added item'); } else { $r->renderError(400); } }
public function getAtompubServiceDoc($app_root) { $svc = new Dase_Atom_Service(); $ws = $svc->addWorkspace($this->collection_name . ' Workspace'); $coll = $ws->addCollection($app_root . '/collection/' . $this->ascii_id . '.atom', $this->collection_name . ' Items'); $coll->addAccept('application/atom+xml;type=entry'); $coll->addCategorySet()->addCategory('item', 'http://daseproject.org/category/entrytype'); $atts = $coll->addCategorySet('yes', 'http://daseproject.org/category/metadata'); foreach ($this->getAttributes() as $att) { $atts->addCategory($att->ascii_id, '', $att->attribute_name); } $media_repos = $app_root . '/media/' . $this->ascii_id . '.atom'; $media_coll = $ws->addCollection($media_repos, $this->collection_name . ' Media'); foreach (Dase_Media::getAcceptedTypes() as $type) { //$media_coll->addAccept($type,true); $media_coll->addAccept($type); } //json items collection $ws->addCollection($app_root . '/collection/' . $this->ascii_id . '.atom', $this->collection_name . ' JSON Items')->addAccept('application/json'); $item_types_repos = $app_root . '/collection/' . $this->ascii_id . '/item_types.atom'; $ws->addCollection($item_types_repos, $this->collection_name . ' Item Types')->addAccept('application/atom+xml;type=entry')->addCategorySet()->addCategory('item_type', 'http://daseproject.org/category/entrytype'); $attributes_repos = $app_root . '/collection/' . $this->ascii_id . '/attributes.atom'; $atts_repos = $ws->addCollection($attributes_repos, $this->collection_name . ' Attributes'); $atts_repos->addAccept('application/atom+xml;type=entry')->addCategorySet()->addCategory('attribute', 'http://daseproject.org/category/entrytype', '', true); $html_inp_types = $atts_repos->addAccept('application/atom+xml;type=entry')->addCategorySet('yes', 'http://daseproject.org/category/html_input_type'); foreach (array('text', 'textarea', 'select', 'radio', 'checkbox', 'noedit', 'list') as $inp) { $html_inp_types->addCategory($inp); } return $svc->asXml(); }
public function postToUploader($r) { //form can use any 'name' it wishes $filecount = 0; foreach ($r->_files as $k => $v) { $input_name = $k; if ($input_name && is_file($r->_files[$input_name]['tmp_name'])) { $name = $r->_files[$input_name]['name']; $path = $r->_files[$input_name]['tmp_name']; $type = $r->_files[$input_name]['type']; if (!Dase_Media::isAcceptable($type)) { Dase_Log::debug(LOG_FILE, $type . ' is not a supported media type'); //$r->renderError(415,'unsupported media type: '.$type); continue; } if (!is_uploaded_file($path)) { //$r->renderError(400,'no go upload'); continue; } Dase_Log::info(LOG_FILE, 'uploading file ' . $name . ' type: ' . $type); try { //this'll create thumbnail, viewitem, and any derivatives $file = Dase_File::newFile($this->db, $path, $type, $name, BASE_PATH); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'add to collection error: ' . $e->getMessage()); //$r->renderError(409,$e->getMessage()); continue; } $item = $this->collection->createNewItem(null, $this->user->eid); if ($r->has('title')) { $item->setValue('title', $r->get('title')); } else { $item->setValue('title', $name); } try { $media_file = $file->addToCollection($item, true, MEDIA_DIR); //true means tets for dups } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'add to collection error: ' . $e->getMessage()); //$r->renderError(409,$e->getMessage()); continue; } $item->setItemType($r->get('item_type')); //here's where we map admin_att to real att $item->mapConfiguredAdminAtts(); //delay search building?? $item->buildSearchIndex(); $filecount++; } } $r->renderResponse('uploaded ' . $filecount . ' files'); //$r->renderRedirect('manage/'.$this->collection->ascii_id.'/uploader'); }
public function getAtomPubServiceDoc($app_root) { $c = $this->getCollection(); $app = new Dase_Atom_Service(); $workspace = $app->addWorkspace($c->collection_name . ' Item ' . $this->serial_number . ' Workspace'); $media_coll = $workspace->addCollection($app_root . '/item/' . $this->p_collection_ascii_id . '/' . $this->serial_number . '/media.atom', $c->collection_name . ' Item ' . $this->serial_number . ' Media'); foreach (Dase_Media::getAcceptedTypes() as $type) { $media_coll->addAccept($type); } $comments_coll = $workspace->addCollection($app_root . '/item/' . $this->p_collection_ascii_id . '/' . $this->serial_number . '/comments.atom', $c->collection_name . ' Item ' . $this->serial_number . ' Comments'); $comments_coll->addAccept('text/plain'); $comments_coll->addAccept('text/html'); $comments_coll->addAccept('application/xhtml+xml'); $metadata_coll = $workspace->addCollection($app_root . '/item/' . $this->p_collection_ascii_id . '/' . $this->serial_number . '/metadata.atom', $c->collection_name . ' Item ' . $this->serial_number . ' Metadata'); $metadata_coll->addAccept('application/x-www-form-urlencoded'); $metadata_coll->addAccept('application/json'); return $app->asXml(); }
/** used for swap-out */ public function putMedia($r) { $item = Dase_DBO_Item::get($this->db, $this->collection_ascii_id, $this->serial_number); if (!$item) { $r->renderError(404, 'no such item'); } if (!$this->user->can('write', $item)) { $r->renderError(401, 'cannot put media to this item'); } $coll = $item->getCollection(); if (!isset($_SERVER['CONTENT_LENGTH']) || !isset($_SERVER['CONTENT_TYPE'])) { $r->renderError(411, 'missing content length'); } $content_type = Dase_Media::isAcceptable($r->getContentType()); if (!$content_type) { $r->renderError(415, 'not an accepted media type'); } $bits = $r->getBody(); $upload_dir = MEDIA_DIR . '/' . $coll->ascii_id . '/uploaded_files'; if (!file_exists($upload_dir)) { $r->renderError(500, 'missing upload directory ' . $upload_dir); } $new_file = $upload_dir . '/' . $item->serial_number; $ifp = @fopen($new_file, 'wb'); if (!$ifp) { $r->renderError(500); } @fwrite($ifp, $bits); fclose($ifp); // Set correct file permissions @chmod($new_file, 0644); try { $file = Dase_File::newFile($this->db, $new_file, $content_type, null, BASE_PATH); //since we are swapping in: $item->deleteAdminValues(); //note: this deletes ALL media!!! $item->deleteMedia(MEDIA_DIR); $media_file = $file->addToCollection($item, false, MEDIA_DIR); //set 2nd param to true to test for dups unlink($new_file); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'media handler error: ' . $e->getMessage()); $r->renderError(500, 'could not ingest file (' . $e->getMessage() . ')'); } $item->buildSearchIndex(); $r->renderOk(); }
/** like media->putMedia but this does NOT * delete existing media files */ public function postToMedia($r) { $item = $this->item; $coll = $item->getCollection(); if (!isset($_SERVER['CONTENT_LENGTH']) || !isset($_SERVER['CONTENT_TYPE'])) { $r->renderError(411, 'missing content length'); } $content_type = $r->getContentType(); if ('multipart/form-data' == $content_type) { $user = $r->getUser(); if (!$user->can('write', $this->item)) { $r->renderError(401, 'cannot post media to this item'); } foreach ($r->_files as $k => $v) { $input_name = $k; break; //just get the first one } if ($input_name && is_file($r->_files[$input_name]['tmp_name'])) { $name = $r->_files[$input_name]['name']; $path = $r->_files[$input_name]['tmp_name']; $type = $r->_files[$input_name]['type']; if (!Dase_Media::isAcceptable($type)) { Dase_Log::debug(LOG_FILE, $type . ' is not a supported media type'); $r->renderError(415, 'unsupported media type: ' . $type); } if (!is_uploaded_file($path)) { $r->renderError(400, 'no go upload'); } $bits = file_get_contents($path); $content_type = $type; } } else { $user = $r->getUser('service'); if (!$user->can('write', $this->item)) { $r->renderError(401, 'cannot post media to this item'); } $content_type = Dase_Media::isAcceptable($content_type); if (!$content_type) { $r->renderError(415, 'not an accepted media type'); } $bits = $r->getBody(); } $orig_name = ''; if ($r->http_title) { $item->setValue('title', $r->http_title); $orig_name = $r->http_title; } elseif ($r->slug) { $item->setValue('title', $r->slug); $orig_name = $r->slug; } $upload_dir = MEDIA_DIR . '/' . $coll->ascii_id . '/uploaded_files'; if (!file_exists($upload_dir)) { Dase_Log::debug(LOG_FILE, 'missing upload directory ' . $upload_dir); $r->renderError(500, 'missing upload directory ' . $upload_dir); } $new_file = $upload_dir . '/' . $item->serial_number; $ifp = @fopen($new_file, 'wb'); if (!$ifp) { Dase_Log::debug(LOG_FILE, 'cannot write file ' . $new_file); $r->renderError(500, 'cannot write file ' . $new_file); } @fwrite($ifp, $bits); fclose($ifp); // Set correct file permissions @chmod($new_file, 0644); try { $file = Dase_File::newFile($this->db, $new_file, $content_type, $orig_name, BASE_PATH); //this'll create thumbnail, viewitem, and any derivatives //then return the Dase_DBO_MediaFile for the original $media_file = $file->addToCollection($item, false, MEDIA_DIR); //set 2nd param to true to test for dups } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'item handler error: ' . $e->getMessage()); //delete uploaded file unlink($new_file); $r->renderError(500, 'could not ingest media file (' . $e->getMessage() . ')'); } $item->expireCaches($r->getCache()); $item->buildSearchIndex(); //delete uploaded file unlink($new_file); if ($r->get('page_link')) { $r->renderRedirect($r->get('page_link') . '#' . time()); } //the returned atom entry links to derivs! $mle_url = $r->app_root . '/media/' . $media_file->p_collection_ascii_id . '/' . $media_file->p_serial_number . '.atom'; header("Location:" . $mle_url, TRUE, 201); $r->response_mime_type = 'application/atom+xml'; $r->renderResponse($media_file->asAtom($r->app_root)); }