Esempio n. 1
0
 function insert($db, $r, $collection)
 {
     $att = new Dase_DBO_Attribute($db);
     $att->attribute_name = $this->getTitle();
     //think about using getAscii or Slug also
     $att->ascii_id = Dase_Util::dirify($att->attribute_name);
     if (!Dase_DBO_Attribute::get($db, $collection->ascii_id, $att->ascii_id)) {
         $att->collection_id = $collection->id;
         $att->updated = date(DATE_ATOM);
         $att->sort_order = 9999;
         $att->is_on_list_display = 1;
         $att->is_public = 1;
         $att->in_basic_search = 1;
         $att->html_input_type = $this->getHtmlInputType();
         $att->insert();
         $sort_order = 0;
         foreach ($this->getDefinedValues() as $dv) {
             $sort_order++;
             $att->addDefinedValue($dv, $sort_order);
         }
         foreach ($this->getItemTypes() as $type) {
             $att->addItemType($type);
         }
         $att->resort();
     } else {
         throw new Dase_Exception('attribute exists');
     }
     return $att;
 }
Esempio n. 2
0
 public static function getUniqueBaseIdent($db, $title, $collection_ascii_id)
 {
     $check_ident = Dase_Util::dirify($title);
     $mf = new Dase_DBO_MediaFile($db);
     $mf->p_serial_number = $check_ident;
     $mf->p_collection_ascii_id = $collection_ascii_id;
     if (!$mf->findOne()) {
         return $check_ident;
     } else {
         $check_ident = $check_ident . time();
         return Dase_DBO_MediaFile::getUniqueBaseIdent($db, $check_ident, $collection_ascii_id);
     }
 }
Esempio n. 3
0
 public static function findOrCreate($db, $collection_ascii_id, $ascii_id)
 {
     $type = new Dase_DBO_ItemType($db);
     $coll = Dase_DBO_Collection::get($db, $collection_ascii_id);
     if (!$coll) {
         throw new Exception('no such collection');
     }
     $type->collection_id = $coll->id;
     $type->ascii_id = Dase_Util::dirify($ascii_id);
     if (!$type->findOne()) {
         $type->name = ucwords(str_replace('_', ' ', $ascii_id));
         $type->insert();
     }
     return $type;
 }
Esempio n. 4
0
 function insert($db, $r, $collection)
 {
     //think about using Slug also
     $ascii_id = Dase_Util::dirify($this->getAsciiId());
     if (!$ascii_id) {
         $ascii_id = Dase_Util::dirify($this->getTitle());
     }
     if (!Dase_DBO_ItemType::get($db, $collection->ascii_id, $ascii_id)) {
         $type = new Dase_DBO_ItemType($db);
         $type->ascii_id = $ascii_id;
         $type->name = $this->getTitle();
         $type->collection_id = $collection->id;
         $type->description = $this->getSummary();
         $type->insert();
         return $type;
     } else {
         throw new Dase_Exception('item type exists');
     }
 }
Esempio n. 5
0
 public static function findOrCreateAdmin($db, $attribute_ascii_id)
 {
     $att = new Dase_DBO_Attribute($db);
     $att->collection_id = 0;
     $att->ascii_id = $attribute_ascii_id;
     if (!$att->findOne()) {
         $attribute_ascii_id = Dase_Util::dirify($attribute_ascii_id);
         $att->attribute_name = ucwords(str_replace('_', ' ', $attribute_ascii_id));
         $att->sort_order = 0;
         $att->in_basic_search = 0;
         $att->is_on_list_display = 0;
         $att->is_public = 0;
         $att->mapped_admin_att_id = 0;
         $att->updated = date(DATE_ATOM);
         $att->html_input_type = 'no_edit';
         $att->insert();
     }
     return $att;
 }
Esempio n. 6
0
 public function getDownloadAll($r)
 {
     $set = new Dase_DBO_ExerciseSet($this->db);
     $set->load($r->get('id'));
     //ZIP stuff
     $zip = new ZipArchive();
     $target_dir = "/tmp/humptydumpty_zip";
     if (!file_exists($target_dir)) {
         if (!mkdir($target_dir)) {
             $r->renderError(500);
         }
     }
     $filename = $target_dir . '/' . $set->ascii_id . ".zip";
     if (file_exists($filename)) {
         unlink($filename);
     }
     if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
         $r->renderError(401, 'cannot create zip');
     }
     $exercises = $set->getExercises();
     $has_media = 0;
     foreach ($exercises as $ex) {
         if ($ex->media_file) {
             $ex_ascii = Dase_Util::dirify($ex->title);
             $fn = $target_dir . '/exercise-' . $ex_ascii;
             file_put_contents($fn, file_get_contents($ex->media_file));
             if (filesize($fn)) {
                 $zip->addFile($fn, $set->ascii_id . '/' . $ex_ascii . '.mp3');
                 $has_media = 1;
             }
         }
     }
     $zip->close();
     if (!$has_media) {
         $params['msg'] = $set->title . ' has no associated media';
         $r->renderRedirect('set/' . $set->id, $params);
     }
     //todo: need to set a cron job to garbage collect the set in media/tmp
     $r->serveFile($filename, 'application/zip', true);
 }
Esempio 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);
     }
 }
Esempio n. 8
0
 public function insert($db, $r)
 {
     $user = $r->getUser();
     $atom_author = $this->getAuthorName();
     //should be exception??
     if (!$atom_author || $atom_author != $user->eid) {
         $r->renderError(401, 'users do not match');
     }
     $set = new Dase_DBO_Tag($db);
     $set->ascii_id = Dase_Util::dirify($this->getAsciiId());
     $set->eid = $user->eid;
     if ($set->findOne()) {
         $r->renderError(409, 'set with that name exists');
     }
     $set->dase_user_id = $user->id;
     $set->name = $this->getTitle();
     $set->is_public = 0;
     $set->item_count = 0;
     $set->type = 'set';
     $set->created = date(DATE_ATOM);
     $set->updated = date(DATE_ATOM);
     $set->insert();
     /*
     		foreach ($this->getCategories() as $category) {
     			$tag_cat = new Dase_DBO_TagCategory($db);
     			$tag_cat->tag_id = $set->id;
     			$tag_cat->category_id = 0;
     			$tag_cat->term = $category['term'];
     			$tag_cat->label = $category['label'];
     			$scheme = str_replace('http://daseproject.org/category/','',$category['scheme']);
     			$tag_cat->scheme = $scheme;
     			$tag_cat->insert();
     		}
     */
     return $set;
 }
Esempio n. 9
0
 /**
  * will ingest file if there is one
  */
 public function postToItems($r)
 {
     $this->user = $r->getUser('http');
     if (!$this->user->is_admin) {
         $r->renderError(401, 'no go unauthorized');
     }
     $content_type = $r->getContentType();
     if ('application/json' != $content_type) {
         //$r->renderError(415,'cannot accept '.$content_type);
         return $this->_processFile($r);
     }
     $json_data = Dase_Json::toPhp($r->getBody());
     if (!isset($json_data['title'])) {
         $r->renderError(415, 'incorrect json format');
     }
     //create new item
     $item = new Dase_DBO_Item($this->db);
     $item->title = $json_data['title'];
     if (isset($json_data['body'])) {
         $item->body = $json_data['body'];
     }
     if (isset($json_data['links']['file'])) {
         $file_url = $json_data['links']['file'];
         $ext = strtolower(pathinfo($file_url, PATHINFO_EXTENSION));
         $mime_type = Dase_Http_Request::$types[$ext];
         $base_dir = $this->config->getMediaDir();
         $basename = Dase_Util::dirify(pathinfo($file_url, PATHINFO_FILENAME));
         $newname = $this->_findNextUnique($base_dir, $basename, $ext);
         $new_path = $base_dir . '/' . $newname;
         //move file to new home
         file_put_contents($new_path, file_get_contents($file_url));
         chmod($new_path, 0775);
         $size = @getimagesize($new_path);
         $item->name = $newname;
         if (!$item->title) {
             $item->title = $item->name;
         }
         $item->file_url = 'file/' . $item->name;
         $item->filesize = filesize($new_path);
         $item->mime = $mime_type;
         $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];
         }
     } else {
         //meaning no file
         if (!$item->title) {
             $item->title = substr($item->body, 0, 20);
         }
         $item->name = $this->_findUniqueName(Dase_Util::dirify($item->title));
         $item->thumbnail_url = 'www/images/mime_icons/content.png';
     }
     $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);
     }
 }
Esempio n. 10
0
 public function postToForm($r)
 {
     $this->user = $r->getUser();
     $set = new Dase_DBO_Itemset($this->db);
     $set->title = $r->get('title');
     if (!$set->title) {
         $set->title = dechex(time());
     }
     $set->name = $this->_findUniqueName(Dase_Util::dirify($set->title));
     $set->created_by = $this->user->eid;
     $set->created = date(DATE_ATOM);
     $set->insert();
     $r->renderRedirect('set/' . $set->name);
 }
Esempio n. 11
0
 public function postToSwapFile($r)
 {
     $item = new Dase_DBO_Item($this->db);
     if (!$item->load($r->get('id'))) {
         $r->renderError(404);
     }
     if ($this->user->eid != $item->created_by && !$this->user->is_admin) {
         $r->renderError(401);
     }
     //@unlink($old_path);
     $file = $r->_files['uploaded_file'];
     if ($file && is_file($file['tmp_name'])) {
         $name = $file['name'];
         $path = $file['tmp_name'];
         $type = $file['type'];
         if (!is_uploaded_file($path)) {
             $r->renderError(400, 'no go upload');
         }
         if (!isset(Dase_File::$types_map[$type])) {
             $r->renderError(415, 'unsupported media type: ' . $type);
         }
         $base_dir = $this->config->getMediaDir();
         $old_path = $base_dir . '/' . $item->name;
         @unlink($old_path);
         //we won't worry about deleting old thumbnail
         if (!file_exists($base_dir) || !is_writeable($base_dir)) {
             $r->renderError(403, 'not allowed');
         }
         $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
         $basename = Dase_Util::dirify(pathinfo($name, PATHINFO_FILENAME));
         if ('application/pdf' == $type) {
             $ext = 'pdf';
         }
         if ('application/msword' == $type) {
             $ext = 'doc';
         }
         if ('application/vnd.openxmlformats-officedocument.wordprocessingml.document' == $type) {
             $ext = 'docx';
         }
         $newname = $this->_findNextUnique($base_dir, $basename, $ext);
         $new_path = $base_dir . '/' . $newname;
         //move file to new home
         rename($path, $new_path);
         chmod($new_path, 0775);
         $size = @getimagesize($new_path);
         //ONLY update name if item had file already
         //adding file to text item shouldn't change name
         if ($item->file_url) {
             $item->name = $newname;
         }
         if (!$item->title) {
             $item->title = $item->name;
         }
         $item->file_url = 'file/' . $newname;
         $item->filesize = filesize($new_path);
         $item->mime = $type;
         $parts = explode('/', $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/img/mime_icons/' . Dase_File::$types_map[$type]['size'] . '.png';
         }
         if (isset($size[0]) && $size[0]) {
             $item->width = $size[0];
         }
         if (isset($size[1]) && $size[1]) {
             $item->height = $size[1];
         }
     }
     $item->updated_by = $this->user->eid;
     $item->updated = date(DATE_ATOM);
     $item->update();
     $r->renderRedirect('item/' . $item->id);
 }
Esempio n. 12
0
 public function postToItemTypes($r)
 {
     $type_ascii_id = Dase_Util::dirify($r->get('name'));
     //note if type_ascii_id MATCHES, we do not create a new type, we grab match
     $type = Dase_DBO_ItemType::findOrCreate($this->db, $this->collection->ascii_id, $type_ascii_id);
     $type->name = $r->get('name');
     $type->description = $r->get('description');
     $type->update();
     $params['msg'] = "{$type->name} created";
     $r->renderRedirect('manage/' . $this->collection->ascii_id . '/item_type/' . $type->ascii_id, $params);
 }
Esempio n. 13
0
<?php

include 'config.php';
$tag = new Dase_DBO_Tag($db);
foreach ($tag->findAll() as $t) {
    $lower = Dase_Util::dirify($t->ascii_id);
    if ($t->ascii_id != $lower) {
        print "PROBLEM! {$t->ascii_id}\n";
        $t->ascii_id = $lower;
        print $t;
        $t->update();
    } else {
        print "OK {$t->ascii_id}\n";
    }
}
Esempio n. 14
0
 public function postToExerciseCategory($r)
 {
     $exercise = new Dase_DBO_Exercise($this->db);
     $exercise->load($r->get('id'));
     $category = new Dase_DBO_Category($this->db);
     $category->text = trim($r->get('category'));
     if (!$category->findOne()) {
         $category->ascii_id = Dase_Util::dirify($r->get('category'));
         $category->insert();
     }
     $exercat = new Dase_DBO_ExerciseCategory($this->db);
     $exercat->exercise_id = $exercise->id;
     $exercat->category_id = $category->id;
     $exercat->insert();
     $r->renderRedirect('exercise/' . $exercise->id . '/edit');
 }
Esempio n. 15
0
 public static function create($db, $tag_name, $user)
 {
     if (!$tag_name) {
         return false;
     }
     $tag = new Dase_DBO_Tag($db);
     $tag->ascii_id = Dase_Util::dirify($tag_name);
     $tag->dase_user_id = $user->id;
     if ($tag->findOne()) {
         return false;
     } else {
         $tag->name = $tag_name;
         $tag->type = 'set';
         $tag->background = 'white';
         $tag->is_public = 0;
         $tag->item_count = 0;
         $tag->eid = $user->eid;
         $tag->created = date(DATE_ATOM);
         $tag->insert();
         return $tag;
     }
 }
Esempio n. 16
0
 public function postToSetForm($r)
 {
     $t = new Dase_Template($r);
     $set = new Dase_DBO_ExerciseSet($this->db);
     if ($r->get('title')) {
         $set->title = $r->get('title');
         $set->ascii_id = Dase_Util::dirify($set->title);
         $set->creator_eid = $this->user->eid;
         $set->insert();
     }
     $r->renderRedirect('admin/set_form');
 }
Esempio n. 17
0
 public function postToContentForm($r)
 {
     $item = new Dase_DBO_Item($this->db);
     $item->body = $r->get('body');
     $item->title = $r->get('title');
     $file = $r->_files['uploaded_file'];
     if ($file && is_file($file['tmp_name'])) {
         $name = $file['name'];
         $path = $file['tmp_name'];
         $type = $file['type'];
         if (!is_uploaded_file($path)) {
             $r->renderError(400, 'no go upload');
         }
         if (!isset(Dase_File::$types_map[$type])) {
             $r->renderError(415, 'unsupported media type: ' . $type);
         }
         $base_dir = $this->config->getMediaDir();
         $thumb_dir = $this->config->getMediaDir() . '/thumb';
         if (!file_exists($base_dir) || !is_writeable($base_dir)) {
             $r->renderError(403, 'media directory not writeable: ' . $base_dir);
         }
         if (!file_exists($thumb_dir) || !is_writeable($thumb_dir)) {
             $r->renderError(403, 'thumbnail directory not writeable: ' . $thumb_dir);
         }
         $ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
         $basename = Dase_Util::dirify(pathinfo($name, PATHINFO_FILENAME));
         if ('application/pdf' == $type) {
             $ext = 'pdf';
         }
         if ('application/msword' == $type) {
             $ext = 'doc';
         }
         if ('application/vnd.openxmlformats-officedocument.wordprocessingml.document' == $type) {
             $ext = 'docx';
         }
         $newname = $this->_findNextUnique($base_dir, $basename, $ext);
         $new_path = $base_dir . '/' . $newname;
         //move file to new home
         rename($path, $new_path);
         chmod($new_path, 0775);
         $size = @getimagesize($new_path);
         $item->name = $newname;
         if (!$item->title) {
             $item->title = $item->name;
         }
         $item->file_url = 'file/' . $item->name;
         $item->filesize = filesize($new_path);
         $item->mime = $type;
         $parts = explode('/', $type);
         if (isset($parts[0]) && 'image' == $parts[0]) {
             $thumb_path = $thumb_dir . '/' . $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/img/mime_icons/' . Dase_File::$types_map[$type]['size'] . '.png';
         }
         if (isset($size[0]) && $size[0]) {
             $item->width = $size[0];
         }
         if (isset($size[1]) && $size[1]) {
             $item->height = $size[1];
         }
     } else {
         if (!$item->title) {
             $item->title = substr($item->body, 0, 20);
         }
         if (!$item->title) {
             $params['msg'] = "title or body is required is no file is uploaded";
             $r->renderRedirect('admin/upload', $params);
         }
         $item->name = $this->_findUniqueName(Dase_Util::dirify($item->title));
         $item->thumbnail_url = 'www/img/mime_icons/content.png';
     }
     $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;
     $item->insert();
     $r->renderRedirect('items');
 }