Codeigniter And Picasa Web Albums | David B. John

Codeigniter and Picasa Web Albums

Wed, Feb 29, 2012 at 3:52am
Recently, a department for the organization I work for asked me to build a photo gallery for their website. I thought of using something like Gallery but didn't really like the idea of storing all of the photos on the web server.  Plus I used Codeigniter as the MVC framework and fudging Gallery into Codeigniter didn't look fun.  So I decided to use Picasa Web Albums along with the fantastic Zend framework. Here's a demo of what it looks like.  I used lots of cool jquery to display the pics once I pulled them from Picasa but that's not what this post is about. Here's the Codeigniter library to interact with Picasa (make sure Zend is in the same directory with it):
class MY_Upload extends CI_Upload {

	function MY_Upload()
   {
      parent::__construct();

   }

	public function do_picasa_upload($field = 'userfile',$pos=false){
	
		$f = (is_numeric($pos)) ? $_FILES[$field]['name'][$pos] : $_FILES[$field]['name'];
		
		$ftmp_name = (is_numeric($pos)) ? $_FILES[$field]['tmp_name'][$pos] : $_FILES[$field];
		$fname = (is_numeric($pos)) ? $_FILES[$field]['name'][$pos] : $_FILES[$field];
		$ftype = (is_numeric($pos)) ? $_FILES[$field]['type'][$pos] : $_FILES[$field];
		$ferror = (is_numeric($pos)) ? $_FILES[$field]['error'][$pos] : $_FILES[$field];
		$fsize = (is_numeric($pos)) ? $_FILES[$field]['size'][$pos] : $_FILES[$field];
	
		// Is $_FILES[$field] set? If not, no reason to continue.
		if ( ! isset($f))
		{
			$this->set_error('upload_no_file_selected');
			return FALSE;
		}
	
		// Was the file able to be uploaded? If not, determine the reason why.
		if ( ! is_uploaded_file($ftmp_name))
		{
			$error = ( ! isset($ferror)) ? 4 : $ferror;
	

			switch($error)
			{
				case 1:	// UPLOAD_ERR_INI_SIZE
					$this->set_error('upload_file_exceeds_limit');
					break;
				case 2: // UPLOAD_ERR_FORM_SIZE
					$this->set_error('upload_file_exceeds_form_limit');
					break;
				case 3: // UPLOAD_ERR_PARTIAL
				
	$this->set_error('upload_file_partial');
					break;
				case 4: // UPLOAD_ERR_NO_FILE
					$this->set_error('upload_no_file_selected');
					break;
				case 6: // UPLOAD_ERR_NO_TMP_DIR
					$this->set_error('upload_no_temp_directory');
					break;
				case 7: // UPLOAD_ERR_CANT_WRITE
				
	$this->set_error('upload_unable_to_write_file');
					break;
				case 8: // UPLOAD_ERR_EXTENSION
					$this->set_error('upload_stopped_by_extension');
					break;
				default :   $this->set_error('upload_no_file_selected');
					break;
			}
	
			return FALSE;
		}
	
	
		// Set the uploaded data as class variables
		$this->file_temp = $ftmp_name;
		$this->file_size = $fsize;
		$this->_file_mime_type(array('tmp_name'=>$ftmp_name,'tmp_path'=>$this->file_temp,'type'=>$ftype));
		$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $this->file_type);
		$this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
		$this->file_name = $this->_prep_filename($fname);
		$this->file_ext	 = $this->get_extension($this->file_name);
		$this->client_name = $this->file_name;
	
		// Is the file type allowed to be uploaded?
		if ( ! $this->is_allowed_filetype())
		{
		
	$this->set_error('upload_invalid_filetype');
			return FALSE;
		}
	
		// if we're overriding, let's now make sure the new name and type is allowed
		if ($this->_file_name_override != '')
		{
			$this->file_name = $this->_prep_filename($this->_file_name_override);
	
			// If no extension was provided in the file_name config item, use the uploaded one
			if (strpos($this->_file_name_override, '.') === FALSE)
			{
				$this->file_name .= $this->file_ext;
			}
	
			// An extension was provided, lets have it!
			else
			{
				$this->file_ext	 = $this->get_extension($this->_file_name_override);
			}
	
			if ( ! $this->is_allowed_filetype(TRUE))
			{
			
				$this->set_error('upload_invalid_filetype');
				return FALSE;
			}
		}
	
	
	// Convert the file size to kilobytes
		if ($this->file_size > 0)
		{
			$this->file_size = round($this->file_size/1024, 2);
		}
	
		// Is the file size within the allowed maximum?
		if ( ! $this->is_allowed_filesize())
		{
			$this->set_error('upload_invalid_filesize');
			return FALSE;
		}
	
		// Are the image dimensions within the allowed size?
		// Note: This can fail if the server has an open_basdir restriction.
		if ( ! $this->is_allowed_dimensions())
		{
			$this->set_error('upload_invalid_dimensions');
			return FALSE;
		}
	
		// Sanitize the file name for security
		$this->file_name = $this->clean_file_name($this->file_name);
	
		// Truncate the file name if it's too long
		if ($this->max_filename > 0)
		{
			$this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
		}
	
		// Remove white spaces in the name
		if ($this->remove_spaces == TRUE)
		{
			$this->file_name = preg_replace("/\s+/", "_", $this->file_name);
		}
	
		/*
		 * Validate the file name
		 * This function appends an number onto the end of
		 * the file if one with the same name already exists.
		 * If it returns false there was a problem.
		 */
		$this->orig_name = $this->file_name;
	
		if ($this->overwrite == FALSE)
		{
			$this->file_name = $this->set_filename($this->upload_path, $this->file_name);
	
			if ($this->file_name === FALSE)
			{
				return FALSE;
			}
		}
	
		/*
		 * Run the file through the XSS hacking filter
		 * This helps prevent malicious code from being
		 * embedded within a file.  Scripts can easily
		 * be disguised as images or other file types.
		 */
		if ($this->xss_clean)
		{
			if ($this->do_xss_clean() === FALSE)
			{
				$this->set_error('upload_unable_to_write_file');
				return FALSE;
			}
		}		
		return array('tmp_name'=>$this->file_temp,'type'=>$this->file_type,'name'=>$this->file_name);
		}  
} 
comments powered byDisqus