Ejemplo n.º 1
0
 public function deleteLog($r)
 {
     if (Dase_Log::truncate(LOG_FILE)) {
         $r->renderResponse('log has been truncated');
     } else {
         $r->renderError(500);
     }
 }
Ejemplo n.º 2
0
 public static function readLastLine($log_file)
 {
     $log = Dase_Log::getInstance();
     $log->setLogFile($log_file);
     if ($log->log_file && file_exists($log->log_file)) {
         return trim(array_pop(file($log->log_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)));
     }
 }
Ejemplo n.º 3
0
 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);
     }
 }
Ejemplo n.º 4
0
 function ingest($db, $r, $fetch_enclosures = false)
 {
     $user = $r->getUser();
     $coll_ascii_id = $this->getAsciiId();
     $count = $this->getItemCount();
     $collection_name = $this->getTitle();
     $ascii_id = $this->getAsciiId();
     $c = new Dase_DBO_Collection($db);
     $c->collection_name = $collection_name;
     if (Dase_DBO_Collection::get($db, $ascii_id) || $c->findOne()) {
         //$r->renderError(409,'collection already exists');
         Dase_Log::info(LOG_FILE, 'collection exists ' . $c->collection_name);
         return;
     }
     $c->ascii_id = $ascii_id;
     $c->is_public = 0;
     $c->created = date(DATE_ATOM);
     $c->updated = date(DATE_ATOM);
     if ($c->insert()) {
         $cache = $r->getCache();
         $cache->expire('app_data');
         Dase_Log::info(LOG_FILE, 'created collection ' . $c->collection_name);
         $coll_media_dir = MEDIA_DIR . '/' . $ascii_id;
         if (file_exists($coll_media_dir)) {
             //$r->renderError(409,'collection media archive exists');
             Dase_Log::info(LOG_FILE, 'collection media archive exists');
         } else {
             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);
                 }
                 //todo: compat only!
                 symlink($coll_media_dir, $coll_media_dir . '_collection');
             }
         }
         foreach ($this->getEntries() as $entry) {
             if ('item' == $entry->getEntryType()) {
                 $r->set('collection_ascii_id', $c->ascii_id);
                 $entry->insert($db, $r, $fetch_enclosures);
             }
         }
     }
 }
Ejemplo n.º 5
0
 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;
     }
 }
Ejemplo n.º 6
0
 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');
 }
Ejemplo n.º 7
0
 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);
     }
 }
Ejemplo n.º 8
0
 /** will be invoked by subclass */
 function makeViewitem($item, $path_to_media)
 {
     $size = $this->size;
     $collection = $item->getCollection();
     $target = $path_to_media . '/' . $collection->ascii_id . '/viewitem/' . $size . '.jpg';
     if (!file_exists($target)) {
         copy($this->base_path . '/www/images/thumb_icons/' . $size . '.jpg', $target);
     }
     $media_file = new Dase_DBO_MediaFile($this->db);
     $media_file->item_id = $item->id;
     $media_file->filename = $size . '.jpg';
     $media_file->width = 80;
     $media_file->height = 80;
     $media_file->mime_type = 'image/jpeg';
     $media_file->size = 'viewitem';
     $media_file->file_size = filesize($target);
     $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}");
 }
Ejemplo n.º 9
0
 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;
 }
Ejemplo n.º 10
0
 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;
     }
 }
Ejemplo n.º 11
0
 public function expunge()
 {
     if (!$this->id || !$this->ascii_id) {
         throw new Exception('cannot delete unspecified type');
     }
     $ait = new Dase_DBO_AttributeItemType($this->db);
     $ait->item_type_id = $this->id;
     foreach ($ait->find() as $doomed) {
         Dase_Log::info(LOG_FILE, 'deleted attribute_item_type ' . $doomed->id);
         $doomed->delete();
     }
     $this->delete();
 }
Ejemplo n.º 12
0
 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;
     }
 }
Ejemplo n.º 13
0
 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;
     }
 }
Ejemplo n.º 14
0
 function asAtom($app_root, $authorized_links = false)
 {
     $this->user || $this->getUser();
     $feed = new Dase_Atom_Feed();
     $feed->setTitle($this->name);
     if ($this->description) {
         $feed->setSubtitle($this->description);
     }
     $feed->setId($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id);
     $feed->setUpdated($this->updated);
     $feed->addAuthor($this->user->eid);
     $feed->setFeedType('tag');
     $feed->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '.atom', 'self');
     $feed->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id, 'alternate');
     $feed->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '/list', 'alternate', 'text/html', '', 'list');
     $feed->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '/grid', 'alternate', 'text/html', '', 'grid');
     $feed->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '.json', 'alternate', 'application/json', '', 'slideshow');
     $feed->addCategory($this->type, "http://daseproject.org/category/tag_type", $this->type);
     if ($this->is_public) {
         $pub = "public";
     } else {
         $pub = "private";
     }
     $feed->addCategory($pub, "http://daseproject.org/category/visibility");
     $feed->addCategory($this->background, "http://daseproject.org/category/background");
     /*  TO DO categories: admin_coll_id, updated, created, master_item, etc */
     $setnum = 0;
     $collections_array = array();
     foreach ($this->getTagItems() as $tag_item) {
         $tag_item->persist(true);
         $item_unique = $tag_item->p_collection_ascii_id . '/' . $tag_item->p_serial_number;
         //lets us determine if tag includes items in only one collection
         $collections_array[$tag_item->p_collection_ascii_id] = 1;
         if ($authorized_links) {
             //fresh, not from cache
             $item = $tag_item->getItem();
             $entry = $feed->addEntry();
             $entry = $item->injectAtomEntryData($entry, $app_root, true);
         } else {
             $entry = $feed->addItemEntryByItemUnique($this->db, $item_unique, $app_root);
         }
         if ($entry) {
             $setnum++;
             $entry->addCategory($setnum, 'http://daseproject.org/category/position');
             $entry->addCategory($tag_item->id, 'http://daseproject.org/category/tag_item_id');
             $entry->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '/' . $tag_item->id, "http://daseproject.org/relation/search-item");
             $entry->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '/' . $tag_item->id . '/annotation', "http://daseproject.org/relation/edit-annotation");
             if ($tag_item->annotation) {
                 $entry->setSummary($tag_item->annotation);
             }
         } else {
             //remove tag_item
             $log_text = "SMOKING GUN Ann Johns mystery: tried removing {$item_unique} from set {$this->eid}/{$this->ascii_id}";
             Dase_Log::info(LOG_FILE, $log_text);
             //$tag_item->delete();;
             //$this->resortTagItems();
             //$this->updateItemCount();
         }
     }
     if (1 == count($collections_array)) {
         $coll = array_pop(array_keys($collections_array));
         $feed->addCategory($coll, "http://daseproject.org/category/collection");
     }
     return $feed->asXml();
 }
Ejemplo n.º 15
0
 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');
 }
Ejemplo n.º 16
0
 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);
     }
 }
Ejemplo n.º 17
0
 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;
     }
 }
Ejemplo n.º 18
0
 function testLogWritesToLogFile()
 {
     Dase_Log::info(LOG_FILE, 'test');
     $val = substr(Dase_Log::readLastLine(LOG_FILE), -4);
     $this->assertTrue('test' === $val);
 }
Ejemplo n.º 19
0
 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
 }
Ejemplo n.º 20
0
 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;
 }
Ejemplo n.º 21
0
 function createNewItem($serial_number = null, $eid = null)
 {
     if (!$eid) {
         $eid = '_dase';
     }
     $item = new Dase_DBO_Item($this->db);
     $item->collection_id = $this->id;
     if ($serial_number) {
         $item->serial_number = $serial_number;
         if ($item->findOne()) {
             Dase_Log::info(LOG_FILE, "duplicate serial number: " . $serial_number);
             throw new Dase_Exception('duplicate serial number!');
             return;
         }
         $item->status = 'public';
         $item->item_type_id = 0;
         $item->item_type_ascii_id = 'default';
         $item->item_type_name = 'default';
         $item->created = date(DATE_ATOM);
         $item->updated = date(DATE_ATOM);
         $item->created_by_eid = $eid;
         $item->p_collection_ascii_id = $this->ascii_id;
         $item->p_remote_media_host = $this->remote_media_host;
         $item->collection_name = $this->collection_name;
         $item->insert();
         $this->updateItemCount();
         return $item;
     } else {
         $item->status = 'public';
         $item->item_type_id = 0;
         $item->item_type_ascii_id = 'default';
         $item->item_type_name = 'default';
         $item->created = date(DATE_ATOM);
         $item->created_by_eid = $eid;
         $item->p_collection_ascii_id = $this->ascii_id;
         $item->p_remote_media_host = $this->remote_media_host;
         $item->collection_name = $this->collection_name;
         $item->insert();
         //after move to mysql to avoid collisions w/ old sernums
         //replace first '0' w/ '1'
         //todo: better way to generate unique sernum.
         // (do NOT forget to enforce uniqueness in DB)
         //$item->serial_number = sprintf("%09d",$item->id);
         $item->serial_number = '1' . sprintf("%08d", $item->id);
         $item->updated = date(DATE_ATOM);
         $item->update();
         $this->updateItemCount();
         return $item;
     }
 }
Ejemplo n.º 22
0
 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);
     }
 }
Ejemplo n.º 23
0
 /** 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);
     }
 }
Ejemplo n.º 24
0
 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());
     }
 }
Ejemplo n.º 25
0
 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);
 }
Ejemplo n.º 26
0
 /** 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);
 }
Ejemplo n.º 27
0
 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');
 }
Ejemplo n.º 28
0
 function __destruct()
 {
     $now = Dase_Util::getTime();
     $elapsed = round($now - START_TIME, 4);
     Dase_Log::debug(LOG_FILE, 'finished templating ' . $elapsed);
 }
Ejemplo n.º 29
0
 /** 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;
 }
Ejemplo n.º 30
0
 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);
 }