public function postToUsers($r) { $content_type = $r->getContentType(); if ('application/atom+xml;type=entry' == $content_type || 'application/atom+xml' == $content_type) { $raw_input = $r->getBody(); $client_md5 = $r->getHeader('Content-MD5'); //if Content-MD5 header isn't set, we just won't check if ($client_md5 && md5($raw_input) != $client_md5) { $r->renderError(412, 'md5 does not match'); } try { $entry = Dase_Atom_Entry::load($raw_input); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'users handler error: ' . $e->getMessage()); $r->renderError(400, 'bad xml'); } if ('user' != $entry->entrytype) { $r->renderError(400, 'must be a user entry'); } try { $user = $entry->insert($this->db, $r); header("HTTP/1.1 201 Created"); header("Content-Type: application/atom+xml;type=entry;charset='utf-8'"); header("Location: " . $user->getUrl($r->app_root) . '.atom?type=entry'); echo $user->asAtomEntry($r->app_root); exit; } catch (Dase_Exception $e) { $r->renderError(409, $e->getMessage()); } } elseif ('application/x-www-form-urlencoded' == $content_type) { //in honor of http://www.tbray.org/ongoing/When/200x/2009/01/29/Name-Value-Pairs $eid = $r->get('eid'); $name = $r->get('name'); $user = Dase_DBO_DaseUser::get($this->db, $eid); if (!$user) { $user = new Dase_DBO_DaseUser($this->db); $user->name = $name; $user->eid = strtolower($eid); $user->updated = date(DATE_ATOM); $user->created = date(DATE_ATOM); $user->insert(); } header("HTTP/1.1 201 Created"); header("Content-Type: application/atom+xml;type=entry;charset='utf-8'"); header("Location: " . $user->getUrl($r->app_root) . '.atom?type=entry'); echo $user->asAtomEntry($r->app_root); exit; } else { $r->renderError(415, 'cannot accept ' . $content_type); } }
public function retrieveByEid($eid) { $prefix = $this->db->table_prefix; $dbh = $this->db->getDbh(); $sql = "\n\t\t\tSELECT * FROM {$prefix}user \n\t\t\tWHERE lower(eid) = ?\n\t\t\t"; $sth = $dbh->prepare($sql); $sth->execute(array(strtolower($eid))); $row = $sth->fetch(); if ($row) { foreach ($row as $key => $val) { $this->{$key} = $val; } Dase_Log::debug(LOG_FILE, 'DEBUG: retrieved user ' . $eid); return $this; } else { Dase_Log::debug(LOG_FILE, 'DEBUG: could NOT retrieve user ' . $eid); return false; } }
public function dispatch($r) { //if it is a module subclass, append the module resource map if (isset($this->module_resource_map)) { $this->resource_map = array_merge($this->resource_map, $this->module_resource_map); } foreach ($this->resource_map as $uri_template => $resource) { //first, translate resource map uri template to a regex $uri_template = trim($r->handler_path . '/' . $uri_template, '/'); $uri_regex = $uri_template; //skip regex template stuff if uri_template is a plain string if (false !== strpos($uri_template, '{')) { //stash param names into $template_matches $num = preg_match_all("/{([\\w]*)}/", $uri_template, $template_matches); if ($num) { $uri_regex = preg_replace("/{[\\w]*}/", "([\\w-,.]*)", $uri_template); } } //second, see if uri_regex matches the request uri (a.k.a. path) if (preg_match("!^{$uri_regex}\$!", $r->path, $uri_matches)) { Dase_Log::debug(LOG_FILE, "matched resource {$resource}"); //create parameters based on uri template and request matches if (isset($template_matches[1]) && isset($uri_matches[1])) { array_shift($uri_matches); $params = array_combine($template_matches[1], $uri_matches); $r->setParams($params); } $method = $this->determineMethod($resource, $r); Dase_Log::debug(LOG_FILE, "try method {$method}"); if (method_exists($this, $method)) { $r->resource = $resource; $this->setup($r); $this->{$method}($r); //should exit $r->renderError(501, 'empty method ' . $method); } else { $r->renderError(404, 'no handler method'); } } } $r->renderError(404, 'no such resource'); }
public function postToCollections($r) { $user = $r->getUser('http'); if (!$user->is_superuser) { $r->renderError(401, $user->eid . ' is not permitted to create a collection'); } $content_type = $r->getContentType(); if ('application/atom+xml;type=entry' == $content_type || 'application/atom+xml' == $content_type) { $raw_input = $r->getBody(); $client_md5 = $r->getHeader('Content-MD5'); if ($client_md5 && md5($raw_input) != $client_md5) { //todo: fix this //$r->renderError(412,'md5 does not match'); } try { $coll_entry = Dase_Atom_Entry::load($raw_input); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'colls handler error: ' . $e->getMessage()); $r->renderError(400, 'bad xml'); } if ('collection' != $coll_entry->entrytype) { $r->renderError(400, 'must be a collection entry'); } if ($r->slug) { $r->set('ascii_id', Dase_Util::dirify($r->slug)); } $ascii_id = $coll_entry->create($this->db, $r); $user->expireDataCache($r->getCache()); header("HTTP/1.1 201 Created"); header("Content-Type: application/atom+xml;type=entry;charset='utf-8'"); header("Location: " . $r->app_root . "/collection/" . $ascii_id . '.atom'); echo Dase_DBO_Collection::get($this->db, $ascii_id)->asAtomEntry($r->app_root); exit; } else { $r->renderError(415, 'cannoot accept ' . $content_type); } }
public function addToCollection($item, $check_for_dups, $path_to_media) { $c = $item->getCollection(); $metadata = $this->getMetadata(); //prevents 2 files in same collection w/ same md5 if ($check_for_dups) { $prefix = $this->db->table_prefix; $sql = "\n\t\t\t\tSELECT v.value_text\n\t\t\t\tFROM {$prefix}value v, {$prefix}item i, {$prefix}attribute a\n\t\t\t\tWHERE i.collection_id = ?\n\t\t\t\tAND a.ascii_id = ?\n\t\t\t\tAND v.attribute_id = a.id\n\t\t\t\tAND i.id = v.item_id\n\t\t\t\tAND v.value_text = ?\n\t\t\t\tLIMIT 1\n\t\t\t\t"; $hash = $metadata['md5']; $dbh = $this->db->getDbh(); $sth = $dbh->prepare($sql); $sth->execute(array($c->id, 'admin_checksum', $hash)); $row = $sth->fetch(); if ($row && $row['value_text']) { throw new Exception('duplicate file'); } } $subdir = Dase_Util::getSubdir($item->serial_number); $subdir_path = $path_to_media . '/' . $c->ascii_id . '/' . $this->size . '/' . $subdir; if (!file_exists($subdir_path)) { mkdir($subdir_path); } $target = $path_to_media . '/' . $c->ascii_id . '/' . $this->size . '/' . $subdir . '/' . $item->serial_number . '.' . $this->ext; if (file_exists($target)) { //make a timestamped backup copy($target, $target . '.bak.' . time()); } //should this be try-catch? if ($this->copyTo($target)) { $media_file = new Dase_DBO_MediaFile($this->db); $mediafile_meta = array('file_size', 'height', 'width', 'mime_type', 'updated', 'md5'); foreach ($mediafile_meta as $term) { if (isset($metadata[$term])) { $media_file->{$term} = $metadata[$term]; } } $media_file->item_id = $item->id; $media_file->filename = $item->serial_number . '.' . $this->ext; $media_file->size = $this->size; $media_file->p_serial_number = $item->serial_number; $media_file->p_collection_ascii_id = $c->ascii_id; $media_file->insert(); //will only insert item metadata when attribute name matches 'admin_'+att_name foreach ($metadata as $term => $text) { //catches UTF8 errors in exif/iptc data //actually, no it doesn't :( try { $item->setValue('admin_' . $term, $text); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, "could not write admin {$term}: {$text} ERROR: " . $e->getMessage()); } } } return $media_file; }
function makeSizes($item, $path_to_media, $rotate) { $collection = $item->getCollection(); $image_properties = array('small' => array('geometry' => '640x480', 'max_height' => '480', 'size_tag' => '_640'), 'medium' => array('geometry' => '800x600', 'max_height' => '600', 'size_tag' => '_800'), 'large' => array('geometry' => '1024x768', 'max_height' => '768', 'size_tag' => '_1024'), 'full' => array('geometry' => '3600x2700', 'max_height' => '2700', 'size_tag' => '_3600')); $last_width = ''; $last_height = ''; $subdir = Dase_Util::getSubdir($item->serial_number); foreach ($image_properties as $size => $size_info) { $newimage = $path_to_media . '/' . $collection->ascii_id . '/' . $size . '/' . $subdir . '/' . $item->serial_number . $size_info['size_tag'] . '.jpg'; $subdir_path = $path_to_media . '/' . $collection->ascii_id . '/' . $size . '/' . $subdir; if (!file_exists($subdir_path)) { mkdir($subdir_path); } $command = CONVERT . " \"{$this->filepath}\" -format jpeg -rotate {$rotate} -resize '{$size_info['geometry']} >' -colorspace RGB {$newimage}"; $exec_output = array(); $results = exec($command, $exec_output); if (!file_exists($newimage)) { Dase_Log::debug(LOG_FILE, "failed to write {$size} image"); Dase_Log::debug(LOG_FILE, "UNSUCCESSFUL: {$command}"); } $file_info = getimagesize($newimage); //create the media_file entry $media_file = new Dase_DBO_MediaFile($this->db); $media_file->item_id = $item->id; $media_file->filename = $item->serial_number . $size_info['size_tag'] . ".jpg"; if ($file_info) { $media_file->width = $file_info[0]; $media_file->height = $file_info[1]; } if ($media_file->width <= $last_width && $media_file->height <= $last_height) { return; } $last_width = $media_file->width; $last_height = $media_file->height; $media_file->mime_type = 'image/jpeg'; $media_file->size = $size; $media_file->md5 = md5_file($newimage); $media_file->updated = date(DATE_ATOM); $media_file->file_size = filesize($newimage); $media_file->p_collection_ascii_id = $collection->ascii_id; $media_file->p_serial_number = $item->serial_number; $media_file->insert(); Dase_Log::info(LOG_FILE, "created {$media_file->size} {$media_file->filename}"); } return; }
function checkCollectionAuth($collection, $auth_level) { if (!$collection) { Dase_Log::debug(LOG_FILE, 'attempting get to authorization for non-existing collection'); return false; } if ('read' == $auth_level) { if ($collection->is_public || 'user' == $collection->visibility || 'public' == $collection->visibility) { return true; } } /** this seems wrong (too permissive!) if ('write' == $auth_level) { if ( 'user' == $collection->visibility || 'public' == $collection->visibility ) { return true; } } */ $cm = new Dase_DBO_CollectionManager($this->db); $cm->collection_ascii_id = $collection->ascii_id; //todo: need to account for case here! //needs to be case-insensitive $cm->dase_user_eid = $this->eid; $cm->findOne(); if ($cm->auth_level) { if ('read' == $auth_level) { return true; } elseif ('write' == $auth_level && in_array($cm->auth_level, array('write', 'admin', 'manager', 'superuser'))) { return true; } elseif ('admin' == $auth_level && in_array($cm->auth_level, array('admin', 'manager', 'superuser'))) { return true; } else { return false; } } else { return false; } }
public function postToSets($r) { $content_type = $r->getContentType(); if ('application/atom+xml;type=entry' == $content_type || 'application/atom+xml' == $content_type) { $raw_input = $r->getBody(); $client_md5 = $r->getHeader('Content-MD5'); //if Content-MD5 header isn't set, we just won't check if ($client_md5 && md5($raw_input) != $client_md5) { $r->renderError(412, 'md5 does not match'); } try { $set_entry = Dase_Atom_Entry::load($raw_input); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'user handler error: ' . $e->getMessage()); $r->renderError(400, 'bad xml'); } if ('set' != $set_entry->entrytype) { $r->renderError(400, 'must be a set entry'); } try { $set = $set_entry->insert($this->db, $r); header("HTTP/1.1 201 Created"); header("Content-Type: application/atom+xml;type=entry;charset='utf-8'"); header("Location: " . $set->getUrl($r->app_root) . '.atom?type=entry'); echo $set->asAtomEntry($r->app_root); exit; } catch (Dase_Exception $e) { $r->renderError(409, $e->getMessage()); } } else { $r->renderError(415, 'cannot accept ' . $content_type); } }
public function postToSolr($item, $commit = true) { $start_check = Dase_Util::getTime(); $start_get_doc = Dase_Util::getTime(); $check_elapsed = round($start_get_doc - $start_check, 4); Dase_Log::debug(LOG_FILE, 'post to SOLR: ' . $this->solr_update_url . ' item ' . $item->getUnique()); $solr_doc = $this->buildItemSolrDoc($item); //return $solr_doc; $start_index = Dase_Util::getTime(); $get_doc_elapsed = round($start_index - $start_get_doc, 4); $resp = Dase_Http::post($this->solr_update_url, $solr_doc, null, null, 'text/xml'); if ($commit) { Dase_Http::post($this->solr_update_url, '<commit/>', null, null, 'text/xml'); } $end = Dase_Util::getTime(); $index_elapsed = round($end - $start_index, 4); return $resp . ' check: ' . $check_elapsed . ' get_doc: ' . $get_doc_elapsed . ' index: ' . $index_elapsed; }
function asJson($app_root) { $collection_lookup = Dase_DBO_Collection::getLookupArray($this->db); $json_tag; $eid = $this->getUser()->eid; $json_tag['id'] = $this->getUrl($app_root); $json_tag['uri'] = $this->getUrl($app_root); $json_tag['links'] = array('self' => $this->getUrl($app_root)); if ($this->created) { $json_tag['updated'] = $this->created; } else { $json_tag['updated'] = date(DATE_ATOM); } $json_tag['name'] = $this->name; $json_tag['description'] = $this->description; $json_tag['background'] = $this->background; $json_tag['is_public'] = $this->is_public; $json_tag['type'] = $this->type; $json_tag['eid'] = $eid; foreach ($this->getTagItems() as $tag_item) { $item = $tag_item->getItem(); if (!$item) { Dase_Log::debug(LOG_FILE, 'tag_item missing item: ' . $tag_item->id); continue; } $json_item = array(); $json_item['id'] = $app_root . '/tag/' . $eid . '/' . $this->ascii_id . '/' . $tag_item->id; $json_item['links'] = array(); $json_item['links']['self'] = $app_root . '/tag/' . $eid . '/' . $this->ascii_id . '/' . $tag_item->id; $json_item['links']['related'] = $item->getUrl($app_root); $json_item['url'] = $app_root . '/tag/' . $eid . '/' . $this->ascii_id . '/' . $tag_item->id; $json_item['sort_order'] = $tag_item->sort_order; //make sure p_ values are always populated! $json_item['item_unique'] = $tag_item->p_collection_ascii_id . '/' . $tag_item->p_serial_number; $json_item['size'] = $tag_item->size; $json_item['updated'] = $tag_item->updated; $json_item['annotation'] = $tag_item->annotation; $json_item['title'] = $item->getTitle(); $json_item['collection_name'] = $collection_lookup[$item->collection_id]['collection_name']; $json_item['media'] = array(); foreach ($item->getMedia() as $m) { $json_item['media'][$m['size']] = $app_root . $m['url']; } $json_item['metadata'] = array(); foreach ($item->getMetadata() as $meta) { $json_item['metadata'][$meta['attribute_name']] = $meta['value_text']; } $json_tag['items'][] = $json_item; } return Dase_Json::get($json_tag); }
public function deleteMedia($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 delete media in this item'); } try { $item->deleteAdminValues(); //move actual files to 'deleted' directory $item->deleteMedia(MEDIA_DIR); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'media handler error: ' . $e->getMessage()); $r->renderError(500, 'could not delete media (' . $e->getMessage() . ')'); } $item->buildSearchIndex(); $r->renderOk('deleted resource'); }
public function postToIndexer($r) { $user = $r->getUser('http'); if (!$user->can('write', $this->item)) { $r->renderError(401, 'cannot index this item'); } //force indexing & commit $resp = $this->item->buildSearchIndex(); //should use HTTP status code instead if ('ok' == substr($resp, 0, 2)) { $r->renderOk('indexed item'); } else { Dase_Log::debug(LOG_FILE, 'indexer error: ' . $resp); $r->renderError(500); } }
function create($db, $r) { $atom_author = $this->getAuthorName(); $user = $r->getUser('http'); $collection_name = $this->getTitle(); if (!$collection_name) { $r->renderError(400, 'no title'); } $c = new Dase_DBO_Collection($db); $c->collection_name = $collection_name; if ($r->has('ascii_id')) { $ascii_id = $r->get('ascii_id'); //set in handler based on Slug } else { $ascii_id = $this->getAsciiId(); } if (!$ascii_id) { $ascii_id = $c->createAscii(); } if (Dase_DBO_Collection::get($db, $ascii_id) || $c->findOne()) { $r->renderError(409, 'collection already exists'); } $c->ascii_id = $ascii_id; $coll_media_dir = MEDIA_DIR . '/' . $ascii_id; if (file_exists($coll_media_dir)) { //todo: think about this... //$r->renderError(409,'collection media archive exists'); } $c->is_public = 0; $c->created = date(DATE_ATOM); $c->updated = date(DATE_ATOM); $content = $this->getContent(); if ($content) { $c->description = $content; } $summary = $this->getSummary(); if ($summary) { $c->admin_notes = $summary; } if ($c->insert()) { $cache = $r->getCache(); $cache->expire('app_data'); Dase_Log::info(LOG_FILE, 'created collection ' . $c->collection_name); if (mkdir("{$coll_media_dir}")) { chmod("{$coll_media_dir}", 0775); foreach (Dase_Media::$sizes as $size => $access_level) { mkdir("{$coll_media_dir}/{$size}"); Dase_Log::info(LOG_FILE, 'created directory ' . $coll_media_dir . '/' . $size); chmod("{$coll_media_dir}/{$size}", 0775); } symlink($coll_media_dir, $coll_media_dir . '_collection'); } foreach (array('title', 'description', 'keyword', 'rights') as $att) { $a = new Dase_DBO_Attribute($db); $a->ascii_id = $att; $a->attribute_name = ucfirst($att); $a->collection_id = $c->id; $a->in_basic_search = true; $a->is_on_list_display = true; $a->is_public = true; $a->html_input_type = 'text'; if ('description' == $att) { $a->html_input_type = 'textarea'; } $a->updated = date(DATE_ATOM); if ($a->insert()) { Dase_Log::debug(LOG_FILE, 'created att ' . $att); } else { Dase_Log::debug(LOG_FILE, 'problem creating ' . $att); } } $cm = new Dase_DBO_CollectionManager($db); $cm->collection_ascii_id = $ascii_id; $cm->dase_user_eid = $user->eid; $cm->auth_level = 'admin'; $cm->created = date(DATE_ATOM); $cm->created_by_eid = $user->eid; if ($cm->insert()) { Dase_Log::info(LOG_FILE, 'created admin user ' . $ascii_id . '::' . $user->eid); } else { Dase_Log::info(LOG_FILE, 'could not create admin user'); } return $ascii_id; } else { return false; } }
private function _newAtomItemType($r) { $raw_input = $r->getBody(); $client_md5 = $r->getHeader('Content-MD5'); //if Content-MD5 header isn't set, we just won't check if ($client_md5 && md5($raw_input) != $client_md5) { $r->renderError(412, 'md5 does not match'); } try { $type_entry = Dase_Atom_Entry::load($raw_input); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'coll handler error: ' . $e->getMessage()); $r->renderError(400, 'bad xml'); } if ('item_type' != $type_entry->entrytype) { $r->renderError(400, 'must be an item type entry'); } try { $item_type = $type_entry->insert($this->db, $r, $this->collection); header("HTTP/1.1 201 Created"); header("Content-Type: application/atom+xml;type=entry;charset='utf-8'"); header("Location: " . $r->app_root . "/item_type/" . $r->get('collection_ascii_id') . "/" . $item_type->ascii_id . '.atom'); echo $type->asAtomEntry($this->collection->ascii_id, $r->app_root); exit; } catch (Dase_Exception $e) { $r->renderError(409, $e->getMessage()); } }
function delete() { $dbh = $this->db->getDbh(); $sth = $dbh->prepare('DELETE FROM ' . $this->table . ' WHERE id=:id'); Dase_Log::debug(LOG_FILE, "deleting id {$this->id} from {$this->table} table"); return $sth->execute(array(':id' => $this->id)); //probably need to destroy $this here }
function setValue($att_ascii_id, $value_text, $url = '', $modifier = '', $index = false) { if (!trim($att_ascii_id) || !trim($value_text) && "0" !== $value_text) { return false; } //allows for admin metadata, att_ascii for which always begins 'admin_' //NOTE: we DO create att if it does not exist if (false === strpos($att_ascii_id, 'admin_')) { $att = Dase_DBO_Attribute::findOrCreate($this->db, $this->p_collection_ascii_id, $att_ascii_id); } else { $att = Dase_DBO_Attribute::findOrCreateAdmin($this->db, $att_ascii_id); } if ($att) { if ('listbox' == $att->html_input_type) { //never includes url or modifier $pattern = '/[\\n;]/'; $prepared_string = preg_replace($pattern, '%', trim($value_text)); $values_array = explode('%', $prepared_string); foreach ($values_array as $val_txt) { $v = new Dase_DBO_Value($this->db); $v->item_id = $this->id; $v->attribute_id = $att->id; $v->value_text = $val_txt; //note: duplicate detection //added 4/9/2010 if (!$v->findOne()) { $v->insert(); } } } else { $v = new Dase_DBO_Value($this->db); $v->item_id = $this->id; $v->attribute_id = $att->id; $v->value_text = trim($value_text); $v->url = $url; $v->modifier = $modifier; //note: duplicate detection //added 4/9/2010 if (!$v->findOne()) { $v->insert(); } if ($index) { $this->updated = date(DATE_ATOM); $this->update(); $this->buildSearchIndex(); } return $v; } if ($index) { $this->updated = date(DATE_ATOM); $this->update(); $this->buildSearchIndex(); } } else { //simply returns false if no such attribute Dase_Log::debug(LOG_FILE, '[WARNING] no such attribute ' . $att_ascii_id); return false; } }
public static function load($xml, $force_type = '') { //reader object $dom = new DOMDocument('1.0', 'utf-8'); if (is_file($xml)) { if (!$dom->load($xml)) { throw new Dase_Atom_Exception('bad xml'); } } else { if (!$dom->loadXml($xml)) { Dase_Log::debug(LOG_FILE, "bad xml:\n " . $xml); throw new Dase_Atom_Exception('bad xml -- see log'); } } $root = $dom->getElementsByTagNameNS(Dase_Atom::$ns['atom'], '*')->item(0); if ('entry' != $root->localName) { throw new Dase_Atom_Exception('wrong document type ' . $root->localName); } $entrytype = ''; foreach ($dom->getElementsByTagNameNS(Dase_Atom::$ns['atom'], 'category') as $el) { if ('http://daseproject.org/category/entrytype' == $el->getAttribute('scheme')) { $entrytype = $el->getAttribute('term'); break; } } if ($force_type) { $entrytype = $force_type; } //todo: clean up this logic if (isset($entrytype) && isset(self::$types_map[$entrytype])) { $class = self::$types_map[$entrytype]; if ($class) { $obj = new $class($dom, $root); $obj->entrytype = $entrytype; return $obj; } else { $entry = new Dase_Atom_Entry($dom, $root); $entry->entrytype = 'none'; return $entry; } } else { $entry = new Dase_Atom_Entry($dom); $entry->entrytype = 'none'; return $entry; } }
/** any data fetch can override the default ttl */ public function getData($filename, $ttl = 0) { $filepath = $this->getFilePath($filename); if (!file_exists($filepath)) { return false; } $time_to_live = $ttl ? $ttl : $this->ttl; $stat = @stat($filepath); if (time() > $stat[9] + $time_to_live) { //delete out of date files //print time()." "; //print $stat[9];exit; //print $time_to_live;exit; @unlink($filepath); return false; } Dase_Log::debug(LOG_FILE, 'cache HIT!!! ' . $filepath); return file_get_contents($filepath); }
/** 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 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'); }
function __destruct() { //see http://bugs.php.net/bug.php?id=34206 // if strange 'failed to open stream' messages appear $now = Dase_Util::getTime(); $elapsed = round($now - START_TIME, 4); Dase_Log::debug(LOG_FILE, 'finished request ' . $elapsed); }
function __destruct() { $now = Dase_Util::getTime(); $elapsed = round($now - START_TIME, 4); Dase_Log::debug(LOG_FILE, 'finished templating ' . $elapsed); }
/** this function authenticates Basic HTTP * and returns EID */ private function _authenticate($check_db = false) { $request_headers = apache_request_headers(); $passwords = array(); if ($this->htuser && $this->htpass) { $eid = $this->htuser; //Dase_Log::debug(LOG_FILE,'adding password '.substr(md5($this->token.$eid.'httpbasic'),0,12)); //Dase_Log::debug(LOG_FILE,'token is '.$this->token); $passwords[] = substr(md5($this->token . $eid . 'httpbasic'), 0, 12); //for service users: //if eid is among service users, get password w/ service_token as salt if (isset($this->serviceusers[$eid])) { Dase_Log::debug(LOG_FILE, 'serviceuser request from ' . $eid); $passwords[] = md5($this->service_token . $eid); } //lets me use the superuser passwd for http work if (isset($this->superusers[$eid])) { $passwords[] = $this->superusers[$eid]; } //this is used for folks needing a quick service pwd to do uploads if ($check_db) { $u = clone $this->null_user; if ($u->retrieveByEid($eid)) { $pass_md5 = md5($this->htpass); if ($pass_md5 == $u->service_key_md5) { Dase_Log::debug(LOG_FILE, 'accepted user ' . $eid . ' using password ' . $this->htpass); return $eid; } } } if (in_array($this->htpass, $passwords)) { Dase_Log::debug(LOG_FILE, 'accepted user ' . $eid . ' using password ' . $this->htpass); return $eid; } else { Dase_Log::debug(LOG_FILE, 'rejected user ' . $eid . ' using password ' . $this->htpass); } } else { Dase_Log::debug(LOG_FILE, 'PHP_AUTH_USER and/or PHP_AUTH_PW not set'); } header('WWW-Authenticate: Basic realm="DASe"'); header('HTTP/1.1 401 Unauthorized'); echo "sorry, authorized users only"; exit; }
public function putTag($r) { $user = $r->getUser('http'); if (!$user->can('write', $this->tag)) { $r->renderError(401, 'cannot update set'); } $content_type = $r->getContentType(); if ('application/atom+xml;type=entry' == $content_type || 'application/atom+xml' == $content_type) { $raw_input = $r->getBody(); $client_md5 = $r->getHeader('Content-MD5'); //if Content-MD5 header isn't set, we just won't check if ($client_md5 && md5($raw_input) != $client_md5) { $r->renderError(412, 'md5 does not match'); } try { $set_entry = Dase_Atom_Entry::load($raw_input); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'tag handler error: ' . $e->getMessage()); $r->renderError(400, 'bad xml'); } if ('set' != $set_entry->entrytype) { $r->renderError(400, 'must be a set entry'); } $set = $set_entry->update($this->db, $r); if ($set) { $r->renderOk('set updated'); } else { $r->renderError(500); } } $r->renderError(500); }