Example #1
0
 function uploadpicture()
 {
     //uploading photo to server
     $_data['new_album_name'] = $newAlbumName = $this->input->post('new_album_name');
     $_data['album_id'] = $albumId = $this->input->post('album_id');
     //var_dump($_SESSION);
     if (!($albumId || $newAlbumName)) {
         //echo "test";
         $_data['msg'] = $msg = 'You should select album or enter new album name';
         $_data['albums'] = $this->_getAlbums();
     } elseif (!empty($_FILES['image'])) {
         $uploaddir = BASEPATH . '../pictures';
         $fname = $_FILES['image']['name'];
         $fsize = $_FILES['image']['size'];
         $ftmpname = $_FILES['image']['tmp_name'];
         $ext = '';
         if (preg_match("/.+(\\..+)\$/i", $fname, $matches)) {
             $ext = strtolower($matches[1]);
         }
         //file extension
         $filename = $this->_genFileName($ext, $uploaddir);
         $uploadfile = $uploaddir . '/' . $filename;
         //debug: //echo $uploadfile;
         //$fsize < 3000000  //allow to upload only pics that less then file_upload_size bytes
         if (in_array($ext, array(".gif", ".png", ".jpg"))) {
             if (move_uploaded_file($ftmpname, $uploadfile)) {
                 //uploaded
                 $uploaded = TRUE;
             } else {
                 //Error while uploading file
             }
         } else {
             //Picture shouldn't exceed file_upload_size bytes
             $_data['msg'] = $msg = 'Uploaded file should be a picture';
         }
     } else {
         $_data['msg'] = $msg = 'You should select picture to upload';
     }
     if (empty($uploaded)) {
         if (empty($msg)) {
             $_data['msg'] = $msg = 'Cannot upload file';
         }
         $_data['albums'] = $this->_getAlbums();
         $this->load->view('member_post_picture', $_data);
         //exit;
     } else {
         $photoName = "Test";
         $photoCaption = "Uploaded to Picasa Web Albums via PHP.";
         $photoTags = "";
         $fd = $this->gp->newMediaFileSource($uploadfile);
         $fd->setContentType("image/jpeg");
         // Create a PhotoEntry
         $photoEntry = $this->gp->newPhotoEntry();
         $photoEntry->setMediaSource($fd);
         $photoEntry->setTitle($this->gp->newTitle($photoName));
         $photoEntry->setSummary($this->gp->newSummary($photoCaption));
         // add some tags
         $keywords = new Zend_Gdata_Media_Extension_MediaKeywords();
         $keywords->setText($photoTags);
         $photoEntry->mediaGroup = new Zend_Gdata_Media_Extension_MediaGroup();
         $photoEntry->mediaGroup->keywords = $keywords;
         // We use the AlbumQuery class to generate the URL for the album
         $albumQuery = $this->gp->newAlbumQuery();
         $albumQuery->setUser($this->username);
         if ($albumId) {
             $albumQuery->setAlbumId($albumId);
         } else {
             $entry = new Zend_Gdata_Photos_AlbumEntry();
             $entry->setTitle($this->gp->newTitle($newAlbumName));
             $entry->setSummary($this->gp->newSummary(""));
             $createdEntry = $this->gp->insertAlbumEntry($entry);
             //$albumQuery->setAlbumName($newAlbumName);
             $albumQuery->setAlbumId((string) $createdEntry->gphotoId);
             //Zend_Debug::dump((string)$createdEntry->gphotoId);
         }
         // We insert the photo, and the server returns the entry representing
         // that photo after it is uploaded
         $insertedEntry = $this->gp->insertPhotoEntry($photoEntry, $albumQuery->getQueryUrl());
         if ($insertedEntry->getMediaGroup()->getContent() != null) {
             $mediaContentArray = $insertedEntry->getMediaGroup()->getContent();
             $contentUrl = $mediaContentArray[0]->getUrl();
             print "<pre>";
             var_dump($contentUrl);
             print "</pre>";
             if (!empty($this->fbUserId) && !empty($this->userSettings) && $this->userSettings[0]['facebook_pics_y_n'] == 1) {
                 // uploading picture to Facebook
                 //$Albums = $this->facebook->api_client->photos_getAlbums($this->fbUserId, null);
                 //if(!empty($Albums)) $AlbumId = $Albums[0]['aid']; else $AlbumId = null;
                 //print"<pre>";var_dump($AlbumId);print"</pre>";
                 try {
                     $this->facebook->api_client->photos_upload($uploadfile, null, "Uploading image with " . $this->conf['site_name'], $this->fbUserId);
                     //$this->facebook->api_client->photos_upload($uploadfile, $AlbumId, "Uploading image with pep6", $this->fbUserId);
                 } catch (Exception $ex) {
                     echo $ex->getMessage();
                     //echo "Cannot upload picture to facebook";
                 }
             }
             $lastMessage = $this->Post_model->getWhere(null, $limit = 1, $offset = 0, $order = 'id DESC');
             if (!empty($lastMessage) && $lastMessage[0]['user_id'] == $this->getUserId() && $lastMessage[0]['site_id'] == $this->subdomainId && $lastMessage[0]['post_type'] == 'picture') {
                 $this->Post_pictures_model->insert($lastMessage[0]['id'], $contentUrl);
             } else {
                 $this->Post_model->insert($this->getUserId(), $this->subdomainId, date("Y-m-d H:i"), 'picture', $contentUrl, NULL, NULL);
             }
             $this->load->view('member_post_picture_success', $_data);
             $sitedata = $this->Site_model->getById($this->subdomainId);
             $users = $this->User_is_member_of_site_model->getList(0, 0, array('subscribe_y_n' => 1, 'site_id' => $this->subdomainId), '', array('table' => 'users', 'field1' => 'id', 'field2' => 'user_id'));
             foreach ($users as $user) {
                 $this->_sendemail('newpost', array('sitename' => $sitedata['name'], 'subdomain' => $sitedata['subdomain'], 'email' => $user['email']));
             }
             @unlink($uploadfile);
         }
     }
 }