Passed
Push — master ( b4f10c...f56dd8 )
by Kishan
02:09
created

download-album.php (8 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.000webhostapp.com/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
    $album_directory = $album_download_directory.$album_name;
48
    if (!file_exists($album_directory)) {
49
         mkdir($album_directory, 0777);
50
    }
51
52
    $request_albums_photo = $fb->get($album_id . "/photos?fields=images&limit=5", $accessToken);
53
    $arr_alb = $request_albums_photo->getGraphEdge();
54
    
55
    $i = 0;
56
    $resultAlbum = getAlbum($fb, $arr_alb, $album_name, $i);
57
    $count = 1;
58
    foreach ($resultAlbum as $album_photo) {
59
        file_put_contents(
60
            $album_directory . "/" . $count . ".jpg", 
61
            fopen($album_photo['images'], 'r')
62
        );
63
        $count++;
64
    }
65
}
66
67
/**
68
 * It will export the album from facebook and puut it into json file
69
 * 
70
 * @param String $accessToken acess token for the access albums
71
 * @param String $album_id    Id of ther album 
72
 * @param String $album_name  Album name
73
 * @param String $fb          Facebook Object
74
 * 
75
 * @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...
76
 */
77
function Export_album($accessToken, $album_id, $album_name, $fb) 
78
{
79
    $request_albums_photo = $fb->get($album_id . "/photos?fields=images&limit=5", $accessToken);
80
    $arr_alb = $request_albums_photo->getGraphEdge();
81
    $i = 0;
82
    $resultAlbum = getAlbum($fb, $arr_alb, $album_name, $i);
83
    
84
    $response_json = json_encode(array($album_name => $resultAlbum), JSON_PRETTY_PRINT);
85
    $jsonFilename = './public/jsonData/fb-album_' . date("Y-m-d") . '_' . date("H-i-s") . '.json';
86
    $jsonFile = fopen($jsonFilename, "a") or die("Unable to open file!");
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
87
    fwrite($jsonFile, $response_json);
0 ignored issues
show
It seems like $jsonFile can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
    fwrite(/** @scrutinizer ignore-type */ $jsonFile, $response_json);
Loading history...
88
    fclose($jsonFile);
0 ignored issues
show
It seems like $jsonFile can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
    fclose(/** @scrutinizer ignore-type */ $jsonFile);
Loading history...
89
    
90
    echo '<a href="' . $jsonFilename . '" id="download-link" target="_blank" class="btn" >See JSON File</a>';
91
} 
92
93
94
/**
95
 * It will fetch all the album from the facebook and put it in array
96
 * 
97
 * @param String $fb         Facebook Object
98
 * @param String $arr_alb    limited array of the album
99
 * @param String $album_name Album name
100
 * @param String $i          taking care of index value of array 
101
 * 
102
 * @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...
103
 */
104
function getAlbum($fb, $arr_alb, $album_name, $i)
105
{
106
    global $main_arr;
107
    foreach ($arr_alb as $graphNode) {
0 ignored issues
show
The expression $arr_alb of type string is not traversable.
Loading history...
108
        $main_arr[$i]['images'] = $graphNode['images'][0]['source'];
109
        $i++;
110
    }
111
    $arr_alb_ar = $fb->next($arr_alb);
112
    if (!empty($arr_alb_ar)) {
113
        getAlbum($fb, $arr_alb_ar, $album_name, $i);
114
    }
115
    return $main_arr;
116
}
117
118
//---------- For 1 album download -------------------------------------------------//
119
if (isset($_GET['single_album']) && !empty($_GET['single_album'])) {
120
    $single_album = explode(",", $_GET['single_album']);
121
    Download_album(
122
        $accessToken, 
123
        $album_download_directory, 
124
        $single_album[0], 
125
        $single_album[1], 
126
        $fb
127
    );
128
}
129
130
//---------- For Selected Albums download -----------------------------------------//
131
if (isset($_GET['selected_albums']) && !empty($_GET['selected_albums'])) {
132
    $selected_albums = explode("/", $_GET['selected_albums']);
133
    foreach ($selected_albums as $selected_album) {
134
        $selected_album = explode(",", $selected_album);
135
        Download_album(
136
            $accessToken, 
137
            $album_download_directory, 
138
            $selected_album[0], 
139
            $selected_album[1], 
140
            $fb
141
        );
142
    }
143
}
144
145
//---------- Download all album code --------------------------------------//
146
if (isset($_GET['all_albums']) && !empty($_GET['all_albums'])) {
147
    if ($_GET['all_albums'] == 'all_albums') {
148
        $response_albums = $fb->get('/me/albums?fields=id,name', $accessToken);
149
        $albums = $response_albums->getGraphEdge()->asArray();
150
        if (!empty($albums)) {
151
            foreach ($albums as $album) {
152
                Download_album(
153
                    $accessToken, 
154
                    $album_download_directory, 
155
                    $album['id'], 
156
                    $album['name'], 
157
                    $fb
158
                );
159
            }
160
        }
161
    }
162
}
163
164
//---------------Export Single album-------------------------//
165
if (isset($_GET['single_export']) && !empty($_GET['single_export'])) {
166
    $single_album = explode(",", $_GET['single_export']);
167
    Export_album(
168
        $accessToken,  
169
        $single_album[0], 
170
        $single_album[1],
171
        $fb
172
    );
173
}
174
175
if (isset($_GET['zip'])) {
176
    include_once 'zipper.php';
177
    $zipper = new Zipper();
178
    echo $zipper->getZip($album_download_directory);
179
}
180
?>
0 ignored issues
show
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...