public static function handleRedirectReturn($data = false) { $connections = CASHSystem::getSystemSettings('system_connections'); if (isset($connections['com.amazon'])) { $s3_default_email = $connections['com.amazon']['email']; } else { $s3_default_email = false; } $success = S3Seed::connectAndAuthorize($data['key'], $data['secret'], $data['bucket'], $s3_default_email); if ($success) { // we can safely assume (AdminHelper::getPersistentData('cash_effective_user') as the OAuth // calls would only happen in the admin. If this changes we can f**k around with it later. $new_connection = new CASHConnection(AdminHelper::getPersistentData('cash_effective_user')); $connection_name = $data['bucket'] . ' (Amazon S3)'; if (substr($connection_name, 0, 10) == 'cashmusic.') { $connection_name = 'Amazon S3 (created ' . date("M j, Y") . ')'; } $result = $new_connection->setSettings($connection_name, 'com.amazon', array('bucket' => $data['bucket'])); if ($result) { AdminHelper::formSuccess('Success. Connection added. You\'ll see it in your list of connections.', '/settings/connections/'); } else { AdminHelper::formFailure('Error. Something just didn\'t work right.'); } } else { //$return_markup = '<h4>Error</h4>' // . '<p>We couldn\'t connect with your S3 account. Please check the key and secret.</p>'; AdminHelper::formFailure('We couldn\'t connect your S3 account. Please check the key and secret.'); } return $return_markup; }
/** * Reads asset details and redirects to the file directly. The success * Response is set here rather than in processRequest(), allowing it to * exist in the session * * @param {integer} $asset_id - the asset you are trying to retrieve * @return string */public function redirectToAsset($asset_id,$element_id=0) { if ($this->getUnlockedStatus($asset_id)) { $asset = $this->getAssetInfo($asset_id); switch ($asset['type']) { case 'com.amazon': include(CASH_PLATFORM_ROOT.'/classes/seeds/S3Seed.php'); $s3 = new S3Seed($asset['user_id'],$asset['settings_id']); $this->pushSuccess(array('asset' => $asset_id),'redirect executed successfully'); $this->recordAnalytics($asset_id,$element_id); header("Location: " . $s3->getExpiryURL($asset['location'])); die(); break; // I know this break will never be executed, but it makes me feel better seeing it here default: if (parse_url($asset['location']) || strpos($asset['location'], '/') !== false) { $this->pushSuccess(array('asset' => $asset_id),'redirect executed successfully'); $this->recordAnalytics($asset_id,$element_id); header("Location: " . $asset['location']); die(); break; // This one won't get executed either ...sucker! } else { return $this->response->pushResponse( 500,$this->request_type,$this->action, $this->request, 'unknown asset type, please as an admin to check the asset type' ); } } } }
function testAuth() { if ($this->s3_key) { $s3 = new S3Seed($this->cash_user_id, $this->s3_connection_id); $starting_acp = $s3->getAccessControlPolicy($this->s3_bucket); $this->assertTrue(is_array($starting_acp)); $second_email = getTestEnv("S3_2_EMAIL"); $second_key = getTestEnv("S3_2_KEY"); $second_secret = getTestEnv("S3_2_SECRET"); if ($second_email && $second_key && $second_secret) { $auth_success = $s3->authorizeEmailForBucket($this->s3_bucket, $second_email); $this->assertTrue($auth_success); $changed_acp = $s3->getAccessControlPolicy($this->s3_bucket); $this->assertNotEqual($starting_acp, $changed_acp); // add a new connection for the second user $c = new CASHConnection($this->cash_user_id); $new_connection_id = $c->setSettings('S32', 'com.amazon', array("key" => $second_key, "secret" => $second_secret, "bucket" => $this->s3_bucket)); if ($new_connection_id) { $s32 = new S3Seed($this->cash_user_id, $new_connection_id); // now test that we do in fact have upload permission // go through the range of tests — upload, delete, verify $test_filename = dirname(__FILE__) . '/test' . $this->timestamp; $tmp_file = file_put_contents($test_filename, $this->timestamp); $result = $s32->uploadFile($test_filename, false, false); $this->assertTrue($result); $result = $s32->deleteFile('test' . $this->timestamp); $this->assertTrue($result); $full_list = $s32->listAllFiles(); $this->assertFalse(array_key_exists('test' . $this->timestamp, $full_list)); unlink(dirname(__FILE__) . '/test' . $this->timestamp); unset($s32); $acp_success = $s3->setAccessControlPolicy($this->s3_bucket, '', $starting_acp); $this->assertTrue($acp_success); $changed_acp = $s3->getAccessControlPolicy($this->s3_bucket); $this->assertEqual($starting_acp, $changed_acp); } else { echo 'problem adding second S3Seed'; } } } }
protected function findConnectionAssetDeltas($connection_id, $connection = false) { if (!$connection) { $connection = $this->getConnectionDetails($connection_id); } $all_local_assets = $this->getAssetsForConnection($connection_id); if (!$all_local_assets) { $all_local_assets = array(); } $all_remote_files = false; // create reference arrays $id_lookup = array(); $compare_local = array(); $compare_remote = array(); // populate local reference arrays foreach ($all_local_assets as $asset) { $id_lookup[$asset['location']] = $asset['id']; $compare_local[$asset['location']] = $asset['hash']; } // grab remotes, format $compare_remote[] as: // $compare_remote['resource_location'] => file or generated hash // // IMPORTANT: // if $all_remote_files must be keyed by service URI and each entry // must contain a value for 'size' and 'hash' -- each service Seed // should comply to that formatting but if not, fix it there, not here switch ($connection['type']) { case 'com.amazon': $s3 = new S3Seed($connection['user_id'], $connection_id); $all_remote_files = $s3->listAllFiles(); if (!is_array($all_remote_files)) { // could not get remote list. boo. abort. return false; } else { // populate remote reference array foreach ($all_remote_files as $file) { $compare_remote[$file['name']] = $file['hash']; } } } if ($all_remote_files) { //find deltas $deltas = array_diff_assoc($compare_remote, $compare_local); $deltas = array_merge($deltas, array_diff_assoc($compare_local, $compare_remote)); foreach ($deltas as $location => &$change) { if (array_key_exists($location, $compare_local) && array_key_exists($location, $compare_remote)) { $change = 'update'; // keys in both location - means hash has changed. edit local. } else { if (array_key_exists($location, $compare_remote)) { $change = 'add'; // remote key only - means new file. add local. } else { $change = 'delete'; // local key only - means file is gone. remove local. } } } $return_array = array('local_id_reference' => $id_lookup, 'remote_details' => $all_remote_files, 'deltas' => $deltas); return $return_array; } else { return false; } }
protected function getFinalAssetLocation($connection_id, $user_id, $asset_location) { $connection_type = $this->getConnectionType($connection_id); $final_asset_location = false; switch ($connection_type) { case 'com.amazon': $s3 = new S3Seed($user_id, $connection_id); $final_asset_location = $s3->getExpiryURL($asset_location); break; default: if (parse_url($asset_location) || strpos($asset_location, '/') !== false) { $final_asset_location = $asset_location; break; } } return $final_asset_location; }
function testDelete() { if ($this->s3_key) { $s3 = new S3Seed($this->cash_user_id, $this->s3_connection_id); // delete both files and test return $result = $s3->deleteFile('test' . $this->timestamp); $this->assertTrue($result); $result = $s3->deleteFile('test_private' . $this->timestamp); $this->assertTrue($result); // verify they are no longer listed $full_list = $s3->listAllFiles(); $this->assertFalse(array_key_exists('test' . $this->timestamp, $full_list)); $this->assertFalse(array_key_exists('test_private' . $this->timestamp, $full_list)); } }