-
Notifications
You must be signed in to change notification settings - Fork 0
/
moveAlbum.php
112 lines (97 loc) · 4.01 KB
/
moveAlbum.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
ini_set('max_execution_time', 300);
include 'siteConfig.php';
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function moveToDrive($album_id,$folderId,$drive){
$photos = datafromfacebook ( '/' . $album_id . '/photos?fields=source&limit=100' );
$album = datafromfacebook ( '/' . $album_id .'?fields=id,name');
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => $album['name'],
'mimeType' => 'application/vnd.google-apps.folder',
'parents' => array($folderId)
));
$file = $drive->files->create($fileMetadata, array('fields' => 'id'));
$album_folder = $file->id;
$offset=0;
while(count($photos['data']) > 0)
{
foreach ($photos['data'] as $photo) {
$photo = ( array ) $photo;
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => uniqid().'.jpg',
'parents' => array($album_folder)
));
$content = url_get_contents($photo['source'] );
$file = $drive->files->create($fileMetadata, array(
'data' => $content,
'mimeType' => 'image/jpeg',
'uploadType' => 'multipart',
'fields' => 'id'
));
}
$offset+=100;
$photos = datafromfacebook ( '/' . $album_id . '/photos?fields=source&limit=100&offset='.$offset );
}
}
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
if ($client->isAccessTokenExpired()) {
echo "Session Expired. Logout and Login Again to Google";
echo '<script type="text/javascript">window.open("https://rtcamp-fb-assignment.000webhostapp.com/googleAuth.php", "Drive Access", width="700", height="380");</script>';
exit;
}
$drive = new Google_Service_Drive($client);
$user = datafromfacebook ( '/me?fields=first_name,last_name');
$username = 'facebook_'.$user['first_name'].'_'.$user['last_name'].'_albums';
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => $username,
'mimeType' => 'application/vnd.google-apps.folder'));
$file = $drive->files->create($fileMetadata, array('fields' => 'id'));
$folderId = $file->id;
if(isset($_POST['move_single'])) {
$album_id = $_POST['album_id'];
moveToDrive($album_id,$folderId,$drive);
echo '<div class="text-center text-success">
<i class="fa fa-check-circle fa-3x"></i>
<br>
<h3 class="text-success">Success!</h3>
</div>';
}
if(isset($_POST['move_selected'])) {
$album_ids = explode ( "/", $_POST ['albums'] );
foreach ( $album_ids as $album_id ) {
$album_id = explode( ",", $album_id );
moveToDrive($album_id [0],$folderId,$drive);
}
echo '<div class="text-center text-success">
<i class="fa fa-check-circle fa-3x"></i>
<br>
<h3 class="text-success">Success!</h3>
</div>';
}
if(isset($_POST['move_all'])) {
$albums=datafromfacebook ('/me/albums?fields=id,name');
foreach ($albums['data'] as $album) {
$album = (array) $album;
moveToDrive($album['id'],$folderId,$drive);
}
echo '<div class="text-center text-success">
<i class="fa fa-check-circle fa-3x"></i>
<br>
<h3 class="text-success">Success!</h3>
</div>';
}
} else {
echo "Please Login First";
echo '<script type="text/javascript">window.open("https://rtcamp-fb-assignment.000webhostapp.com/googleAuth.php", "Drive Access", width="400", height="400");</script>';
}