From: Victor S. <fou...@gm...> - 2007-02-14 19:01:26
|
On 2/13/07, Asheesh Laroia <as...@as...> wrote: > On Tue, 13 Feb 2007, Victor Stone wrote: > > > If you want to use the URL to suck the file down to the ccHost server > > then we can discuss easy ways to do that. > > Okay, what are your thoughts on that? I'm happy to do it that way. > The code is below. 1. Put that in a file into your local_files/lib (must have a .php extension) 2. browse to http://your_site/?update=1 3. browse to http://your_site/media/files/submitremote To hook up a submit form (you won't get all the features like 'suggested tags' without hacking sorry): 1. click Manage Site 2. click Submit Forms 3. click 'Add new form type' 4. Fill out the form (about half don't apply, again, sorry) 4a. In "Handler URL" put: 'files/submitremote' That's it. The code below needs LOTS of testing for various conditions and way better error handling. Works great as long you have a direct link http://example.com/groovy.mp3 probably breaks on cases without a proper extension http://example.com/download?id=4 VS ============= cut below here ================ if( !defined('IN_CC_HOST') ) die('Welcome to CC Host'); require_once('cclib/cc-upload-forms.php'); CCEvents::AddHandler(CC_EVENT_MAP_URLS, array( 'CCRemoteGetFile', 'OnMapUrls')); class CCRemoteGetFileForm extends CCNewUploadForm { function CCRemoteGetFileForm() { $this->CCNewUploadForm(CCUser::CurrentUser(),true); $form_tip = 'Full URL address of file, including http:// <br />' . $this->GetFormFieldItem('upload_file_name','form_tip'); // this will replace file thingy... $this->AddFormField('upload_file_name', array ( 'label' => 'URL', 'formatter' => 'remote_downloader', 'form_tip' => $form_tip, 'flags' => CCFF_POPULATE | CCFF_REQUIRED, )); } function generator_remote_downloader($varname,$value='',$class='') { return $this->generator_textedit($varname,$value,$class); } function validator_remote_downloader($fieldname) { global $CC_GLOBALS; if( !$this->validator_textedit($fieldname) ) return false; $url = $this->GetFormValue($fieldname); // use curl or whatever to grab the file (fopen wrappers are shaky and // non-standard install) $fh = fopen($url,'r'); if( $fh ) { $temp_dir = $CC_GLOBALS['php-tal-cache-dir']; preg_match( '/\.([^\.]+)$/',$url,$m); $file_ext = empty($m[1]) ? '.unknown' : '.' . $m[1]; $temp_file_name = substr('dlr' . rand(),0,10) . $file_ext; $temp_fh = fopen($temp_file_name,'wb'); while( !feof($fh) ) { $buffer = fread($fh,50000); fwrite($temp_fh,$buffer); } fclose($fh); fclose($temp_fh); chmod($temp_file_name,0777); $vals = array ( 'name' => 'name_' . $temp_file_name, 'type' => 'octet/stream', 'tmp_name' => $temp_file_name, 'error' => 0, 'size' => filesize($temp_file_name) ); $this->SetFormValue($fieldname, $vals); } else { $err = "Could not open remote file"; } if( !empty($err) ) { $this->SetFieldError( $fieldname, $err ); return false; } return true; } } class CCRemoteGetFile { function Submit() { CCPage::SetTitle('Import Remote File'); $form = new CCRemoteGetFileForm(); if( !empty($_POST['remotegetfile']) && $form->ValidateFields() ) { $curr_user_name = CCUser::CurrentUserName(); $upload_root = CCUser::GetPeopleDir() . '/' . $curr_user_name; $upload_id = CCUpload::PostProcessNewUploadForm( $form, 'media', $upload_root ); if( $upload_id ) { $url = ccl( 'files', $curr_user_name, $upload_id ); CCPage::Prompt(sprintf(_("Import succeeded. Click %s to see results."), "<a href=\"$url\">" . _('here') . "</a>")); return; } } CCPage::AddForm( $form->GenerateForm() ); } function OnMapUrls() { CCEvents::MapUrl( ccp('files','submitremote'), array( 'CCRemoteGetFile', 'Submit'), CC_MUST_BE_LOGGED_IN ); } } ?> ============= cut above here ================ |