Esempio n. 1
0
 public static function moveUpload($src, $target)
 {
     $rel_path = implode('/', array(trim(self::$prefix, '/'), trim($target, '/')));
     $abs_path = '/' . implode('/', array(trim(DOCUMENT_ROOT, '/'), trim($rel_path, '/')));
     $dir = pathinfo($abs_path, PATHINFO_DIRNAME);
     NFilesystem::buildDirs($dir);
     if (move_uploaded_file($src, $abs_path)) {
         return '/' . $rel_path;
     } else {
         throw new NUploadException("Could not move file: {$src} to {$target}", 1);
     }
 }
Esempio n. 2
0
 /**
  * Implementation of NUpload::moveUpload
  */
 public static function moveUpload($src, $target)
 {
     $s3 = self::s3_connect();
     $target = implode('/', array(self::$prefix, trim($target, '/')));
     $file_opts = array('acl' => AmazonS3::ACL_PUBLIC, 'contentType' => NFilesystem::getMimeType($src), 'fileUpload' => $src);
     $resp = $s3->create_object(self::$bucket, $target, $file_opts);
     if ($resp->status == 200) {
         $url = $s3->get_object_url(self::$bucket, $target);
         self::debug("File uploaded to s3: {$url}");
         return $url;
     } else {
         throw new NUploadException("Couldn't upload file to s3: {$bucket} - {$filename}", 1);
         return '';
     }
 }
Esempio n. 3
0
 function putFile($filename)
 {
     $s3svc = new S3();
     // Removing the first slash is important - otherwise the URL is different.
     $aws_filename = eregi_replace('^/', '', $filename);
     $filename = $_SERVER['DOCUMENT_ROOT'] . $filename;
     $mime_type = NFilesystem::getMimeType($filename);
     // Read the file into memory.
     $fh = fopen($filename, 'rb');
     $contents = fread($fh, filesize($filename));
     fclose($fh);
     $s3svc->putBucket(MIRROR_S3_BUCKET);
     $out = $s3svc->putObject($aws_filename, $contents, MIRROR_S3_BUCKET, 'public-read', $mime_type);
     // Now the file is accessable at:
     //		http://MIRROR_S3_BUCKET.s3.amazonaws.com/put/the/filename/here.jpg 	OR
     // 		http://s3.amazonaws.com/MIRROR_S3_BUCKET/put/the/filename/here.jpg
     unset($s3svc);
 }
Esempio n. 4
0
 function _createElements()
 {
     if ($this->getValue()) {
         include_once 'n_filesystem.php';
         if (file_exists($_SERVER['DOCUMENT_ROOT'] . $this->getValue())) {
             $filesize = NFilesystem::filesize_format(filesize($_SERVER['DOCUMENT_ROOT'] . $this->getValue()));
             $this->_elements[] =& new HTML_QuickForm_hidden($this->getName() . '__current', $this->getValue());
             $this->_elements[] =& new HTML_QuickForm_link(null, 'Current File', $this->getValue(), $this->getValue() . ($filesize ? ' (' . $filesize . ')' : ''), array('target' => '_blank'));
             $this->_elements[] =& new HTML_QuickForm_checkbox($this->getName() . '__remove', null, 'Remove File', $this->getAttributes());
         } else {
             $this->setValue(false);
         }
     }
     $this->_elements[] =& new HTML_QuickForm_file($this->getName(), $this->getLabel(), $this->_options, $this->getAttributes());
 }
 /**
  * deleteEmptyFolders - Delete folders without anything in them anymore.
  *
  * @param	string  The name of the asset.
  * @param	int     The id of that asset.
  * @return        void
  * @todo          Make this work with n_mirror.
  **/
 function deleteEmptyFolders($asset_name, $asset_id)
 {
     $folder = DOCUMENT_ROOT . UPLOAD_DIR . '/' . $asset_name . '/' . $asset_id;
     if (is_dir($folder)) {
         if ($handle = opendir($folder)) {
             while (false !== ($file = readdir($handle))) {
                 if ($file != "." && $file != "..") {
                     if (is_dir($folder . '/' . $file)) {
                         $full_path = $folder . '/' . $file;
                         // Check to see if it's empty.
                         if (count(glob("{$full_path}/*")) === 0) {
                             // Then delete if this is so.
                             NFilesystem::deleteFolder($full_path);
                         } else {
                             NDebug::debug("{$full_path} is not empty and will not be deleted.", N_DEBUGTYPE_INFO);
                         }
                     }
                 }
             }
             closedir($handle);
         }
     }
 }
Esempio n. 6
0
 function deleteUploadFolder()
 {
     require_once 'lib/n_filesystem.php';
     $pk = $this->primaryKey();
     if (!$this->{$pk}) {
         return;
     }
     $upload_dir = UPLOAD_DIR . "/{$this->tableName(true)}/{$this->{$pk}}";
     NFilesystem::deletePathRecursive($upload_dir);
 }
 function downloadIcon($file_path)
 {
     return NFilesystem::download_icon($file_path);
 }
Esempio n. 8
0
 function preProcessForm(&$values)
 {
     if (empty($values['filename'])) {
         include_once 'n_filesystem.php';
         $values['filename'] = NFilesystem::cleanFileName($values['title']);
     }
     parent::preProcessForm($values);
 }
Esempio n. 9
0
 /**
  * getMimeType - Get the MIME type for a fully qualified filename.
  *		Uses the PECL fileinfo extension if available.
  *
  * @param	string	A fully qualified filename.
  * @return 	string	The MIME type for that file.
  **/
 function getMimeType($filename)
 {
     // Use the fileinfo pecl extension if it's available.
     if (function_exists('finfo_open')) {
         $handle = finfo_open(FILEINFO_MIME);
         $mime_type = finfo_file($handle, $filename);
         return $mime_type;
     } else {
         $extension = NFilesystem::getExtension($filename);
         switch ($extension) {
             case "jpg":
                 return "image/jpeg";
             case "jpeg":
                 return "image/jpeg";
             case "gif":
                 return "image/gif";
             case "png":
                 return "image/png";
             case "pdf":
                 return "application/pdf";
             case "txt":
                 return "text/plain";
             case "doc":
                 return "application/msword";
             case "xls":
                 return "application/vnd.ms-excel";
             case "ppt":
                 return "application/vnd.ms-powerpoint";
             case "css":
                 return "text/css";
             case "js":
                 return "application/x-javascript";
             case "html":
                 return "text/html";
             case "xhtml":
                 return "application/xhtml+xml";
             case "zip":
                 return "application/zip";
             default:
                 return "application/octet-stream";
         }
     }
 }
Esempio n. 10
0
 function search()
 {
     include_once 'HTTP/Header.php';
     $header = new HTTP_Header();
     $search = htmlentities($this->getParam('search'));
     $html = '';
     $this->set('request_uri', NServer::env('PHP_SELF'));
     $this->set('search', $search);
     $html .= $this->render(array('action' => 'search_form', 'return' => true));
     if ($search && function_exists('udm_alloc_agent')) {
         $current_page = $this->getParam('page') ? (int) $this->getParam('page') : 0;
         // instantiate the search agent
         $udm_agent = udm_alloc_agent(DB_DSN_SEARCH);
         $page_size = 10;
         // pull in the stopwords and remove them from the query
         $stopfile = defined('MNOGO_STOPFILE') ? constant('MNOGO_STOPFILE') : '/usr/local/mnogosearch/etc/stopwords/en.huge.sl';
         $tmp = file($stopfile);
         $stopwords = array();
         for ($i = 0; $i < count($tmp); $i++) {
             $tmp[$i] = trim($tmp[$i]);
             if (!$tmp[$i] || preg_match('/^(#|Language:|Charset:)/', $tmp[$i])) {
                 continue;
             }
             $stopwords[] = $tmp[$i];
         }
         unset($tmp);
         $stopped_words = array();
         foreach ($stopwords as $stopword) {
             if (($tmp_search = preg_replace("/\\b{$stopword}\\b/", '', $search)) != $search) {
                 $stopped_words[] = $stopword;
                 $search = str_replace('  ', ' ', $tmp_search);
             }
         }
         // set some values
         // paging values
         udm_set_agent_param($udm_agent, UDM_PARAM_PAGE_SIZE, $page_size);
         udm_set_agent_param($udm_agent, UDM_PARAM_PAGE_NUM, $current_page);
         udm_set_agent_param($udm_agent, UDM_PARAM_QUERY, $search);
         udm_set_agent_param($udm_agent, UDM_PARAM_STOPFILE, $stopfile);
         // perform the search
         $res = udm_find($udm_agent, $search);
         // get search result values
         $total_rows = udm_get_res_param($res, UDM_PARAM_FOUND);
         $total_rows = $total_rows > 0 ? $total_rows - 1 : $total_rows;
         $page_rows = udm_get_res_param($res, UDM_PARAM_NUM_ROWS);
         $first_doc = udm_get_res_param($res, UDM_PARAM_FIRST_DOC);
         $last_doc = udm_get_res_param($res, UDM_PARAM_LAST_DOC);
         $total_pages = ceil($total_rows / $page_size);
         // set general template values
         $this->set('rows', $total_rows);
         $template_pages = array();
         for ($i = 0; $i < $total_pages; $i++) {
             $template_pages[] = $i + 1;
         }
         $search_url = NServer::env('PHP_SELF') . '?search=' . $search . '&amp;page=';
         $this->set('stopped_words', $stopped_words);
         $this->set('search_url', $search_url);
         $this->set('current_page', $current_page);
         $this->set('pages', $template_pages);
         $this->set('previous_page', $current_page > 0 ? $current_page - 1 : -1);
         $this->set('next_page', $current_page + 1 < $total_pages ? $current_page + 1 : -1);
         // gather the results and pass them to the template
         $items = array();
         for ($i = 0; $i < $page_rows; $i++) {
             $item['title'] = udm_get_res_field($res, $i, UDM_FIELD_TITLE);
             $item['url'] = udm_get_res_field($res, $i, UDM_FIELD_URL);
             $item['text'] = udm_get_res_field($res, $i, UDM_FIELD_TEXT);
             $item['size'] = udm_get_res_field($res, $i, UDM_FIELD_SIZE);
             $item['filesize'] = udm_get_res_field($res, $i, UDM_FIELD_SIZE);
             if ($item['filesize']) {
                 $item['filesize'] = NFilesystem::filesize_format($item['filesize']);
             }
             $item['rating'] = udm_get_res_field($res, $i, UDM_FIELD_RATING);
             $item['title'] = $item['title'] ? $this->cleanupItem(htmlspecialchars($item['title'])) : basename($item['url']);
             $item['text'] = $this->cleanupItem($item['text']);
             $items[] = $item;
         }
         $this->set('items', $items);
         $html .= $this->render(array('action' => 'found_items', 'return' => true));
         udm_free_res($res);
         udm_free_agent($udm_agent);
     } else {
         $html .= '<p>You have not entered any search queries - please enter one in the form above.</p>';
     }
     return $html;
 }
Esempio n. 11
0
 function processFiles(&$values, $files_array)
 {
     include_once 'n_filesystem.php';
     $form =& $this->form;
     $model =& $this->model;
     $pk = $model->primaryKey();
     foreach ($files_array as $field => $vals) {
         $tmp_file = $vals['value']['tmp_name'];
         if (!is_uploaded_file($tmp_file)) {
             $values[$field] = '';
             continue;
         }
         $path = array();
         if ($vals['type'] == 'cms_file') {
             $path[] = $this->controller->name;
             $path[] = $model->{$pk};
         }
         $path[] = substr(md5(microtime()), 20);
         $path[] = NFilesystem::cleanFileName($vals['value']['name']);
         $filename = implode('/', $path);
         $tmp_file = $model->beforeUpload($field, $tmp_file);
         $newfile = NUpload::moveUpload($tmp_file, $filename);
         $values[$field] = $newfile ? $newfile : '';
     }
 }
Esempio n. 12
0
 /**
  * serveFile - Actually serves the file to the browser.
  *
  * @param	string	A fully qualified filename that needs to be sent to a browser.
  **/
 function serveFile($file_path)
 {
     // Find out the mime type of that file.
     $mime_type = NFilesystem::getMimeType($file_path);
     // Need the filename for later on.
     $filename = $this->getFileName($file_path);
     // Serve the file out like normal.
     $fp = fopen($file_path, 'rb');
     // Send correct headers.
     header("Content-Type: {$mime_type}");
     header("Content-Length: " . filesize($file_path));
     header('Content-Disposition: attachment; filename="' . $filename . '"');
     fpassthru($fp);
 }