Beispiel #1
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);
     }
 }
Beispiel #2
0
 public function postToListListbox($r)
 {
     $list = new Dase_DBO_List($this->db);
     $list->uniq_id = $r->get('id');
     if (!$list->findOne()) {
         $r->renderError(404);
     }
     $text = $r->get('text');
     if (!$text) {
         $r->renderError(400, 'missing data');
     }
     foreach (explode("\n", $text) as $item_text) {
         $item_text = trim($item_text);
         $item = new Dase_DBO_Item($this->db);
         $item->list_id = $list->id;
         $item->text = $item_text;
         $item->hidden = false;
         $item->insert();
     }
     $r->renderRedirect($list->uniq_id . '/form');
 }
Beispiel #3
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;
     }
 }
Beispiel #4
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');
 }