Issues (63)

download-album.php (3 issues)

1
<?php
2
3
/** 
4
 * Copyright 2018 Social Manager.
5
 * 
6
 * PHP version 7.2.8
7
 *
8
 * @category Album_Manager
9
 * @package  Facebook
10
 * @author   Kishan Jasani <[email protected]>
11
 * @license  https://rtfbchallenge.tk/privacy_policy/privacy_policy.php 
12
 * @link     ""
13
 * 
14
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
15
 * use, copy, modify, and distribute this software in source code or binary
16
 * form for use in connection with the web services and APIs provided by
17
 * Kishan Jasani.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND
20
 */
21
require_once "fb-callback.php";
22
23
$zip_folder = "";
24
$album_download_directory = 'public/' . uniqid() . '/';
25
mkdir($album_download_directory, 0777, true);
26
27
$main_arr = array();
28
29
/**
30
 * It will downloads the album from facebook
31
 * 
32
 * @param String $accessToken              acess token for the access albums
33
 * @param String $album_download_directory directory where album will be store
34
 * @param String $album_id                 Id of ther album 
35
 * @param String $album_name               Album name
36
 * @param String $fb                       Facebook Object
37
 * 
38
 * @return 0
0 ignored issues
show
Documentation Bug introduced by
The doc comment 0 at position 0 could not be parsed: Unknown type name '0' at position 0 in 0.
Loading history...
39
 */
40
function Download_album(
41
    $accessToken, 
42
    $album_download_directory, 
43
    $album_id, 
44
    $album_name, 
45
    $fb
46
) {
47
    global $main_arr;
48
    $album_directory = $album_download_directory.$album_name;
49
    if (!file_exists($album_directory)) {
50
         @mkdir($album_directory, 0777) || exit("Coudn't able to create directory");
51
    }
52
53
    $request_albums_photo = $fb->get($album_id . "/photos?fields=images&limit=100", $accessToken);
54
    $arr_alb = $request_albums_photo->getGraphEdge();
55
    
56
    $i = 0;
57
    $resultAlbum = getAlbum($fb, $arr_alb, $album_name, $i);
58
    $count = 1;
59
    foreach ($resultAlbum as $album_photo) {
60
        file_put_contents(
61
            $album_directory . "/" . $count . ".jpg", 
62
            fopen($album_photo['images'], 'r')
63
        );
64
        $count++;
65
    }
66
    $main_arr = array();
67
}
68
69
/**
70
 * It will export the album from facebook and puut it into json file
71
 * 
72
 * @param String $accessToken acess token for the access albums
73
 * @param String $album_id    Id of ther album 
74
 * @param String $album_name  Album name
75
 * @param String $fb          Facebook Object
76
 * 
77
 * @return 0
0 ignored issues
show
Documentation Bug introduced by
The doc comment 0 at position 0 could not be parsed: Unknown type name '0' at position 0 in 0.
Loading history...
78
 */
79
function Export_album($accessToken, $album_id, $album_name, $fb) 
80
{
81
    $request_albums_photo = $fb->get($album_id . "/photos?fields=images&limit=100", $accessToken);
82
    $arr_alb = $request_albums_photo->getGraphEdge();
83
    $i = 0;
84
    $resultAlbum = getAlbum($fb, $arr_alb, $album_name, $i);
85
    
86
    $response_json = json_encode(array($album_name => $resultAlbum), JSON_PRETTY_PRINT);
87
    $jsonFilename = './public/jsonData/fb-album_' . date("Y-m-d") . '_' . date("H-i-s") . '.json';
88
    $jsonFile = fopen($jsonFilename, "a") or die("Unable to open file!");
89
    fwrite($jsonFile, $response_json);
90
    fclose($jsonFile);
91
    
92
    echo '<a href="' . $jsonFilename . '" id="download-link" target="_blank" class="btn" >See JSON File</a>';
93
} 
94
95
96
/**
97
 * It will fetch all the album from the facebook and put it in array
98
 * 
99
 * @param String $fb         Facebook Object
100
 * @param String $arr_alb    limited array of the album
101
 * @param String $album_name Album name
102
 * @param String $i          taking care of index value of array 
103
 * 
104
 * @return "$main_arr"
0 ignored issues
show
Documentation Bug introduced by
The doc comment "$main_arr" at position 0 could not be parsed: Unknown type name '"$main_arr"' at position 0 in "$main_arr".
Loading history...
105
 */
106
function getAlbum($fb, $arr_alb, $album_name, $i)
107
{
108
    global $main_arr;
109
    foreach ($arr_alb as $graphNode) {
110
        $main_arr[$i]['images'] = $graphNode['images'][0]['source'];
111
        $i++;
112
    }
113
    $arr_alb_ar = $fb->next($arr_alb);
114
    if (!empty($arr_alb_ar)) {
115
        getAlbum($fb, $arr_alb_ar, $album_name, $i);
116
    }
117
    return $main_arr;
118
}
119
120
//---------- For 1 album download -------------------------------------------------//
121
if (isset($_GET['single_album']) && !empty($_GET['single_album'])) {
122
    $single_album = explode(",", $_GET['single_album']);
123
    Download_album(
124
        $accessToken, 
125
        $album_download_directory, 
126
        $single_album[0], 
127
        $single_album[1], 
128
        $fb
129
    );
130
    zipFolder($album_download_directory);
131
}
132
133
//---------- For Selected Albums download -----------------------------------------//
134
if (isset($_GET['selected_albums']) && !empty($_GET['selected_albums'])) {
135
    $selected_albums = explode("-", $_GET['selected_albums']);
136
    foreach ($selected_albums as $selected_album) {
137
        $selected_album = explode(",", $selected_album);
138
        Download_album(
139
            $accessToken, 
140
            $album_download_directory, 
141
            $selected_album[0], 
142
            $selected_album[1], 
143
            $fb
144
        );
145
    }
146
    zipFolder($album_download_directory);
147
}
148
149
//---------- Download all album code --------------------------------------//
150
if (isset($_GET['all_albums']) && !empty($_GET['all_albums'])) {
151
    if ($_GET['all_albums'] == 'all_albums') {
152
        $response_albums = $fb->get('/me/albums?fields=id,name', $accessToken);
153
        $albums = $response_albums->getGraphEdge()->asArray();
154
        if (!empty($albums)) {
155
            foreach ($albums as $album) {
156
                Download_album(
157
                    $accessToken, 
158
                    $album_download_directory, 
159
                    $album['id'], 
160
                    $album['name'], 
161
                    $fb
162
                );
163
            }
164
            zipFolder($album_download_directory);
165
        }
166
    }
167
}
168
169
//---------------Export Single album-------------------------//
170
if (isset($_GET['single_export']) && !empty($_GET['single_export'])) {
171
    $single_album = explode(",", $_GET['single_export']);
172
    Export_album(
173
        $accessToken,  
174
        $single_album[0], 
175
        $single_album[1],
176
        $fb
177
    );
178
}
179
180
function zipFolder($album_download_directory){
181
    if (isset($_GET['zip'])) {
182
        include_once 'zipper.php';
183
        $zipper = new Zipper();
184
        echo $zipper->getZip($album_download_directory);
185
    }
186
}
187
?>