Passed
Push — master ( 52bf93...8fa3ac )
by Kishan
02:14
created

Export_album()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 4
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
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 "Downloaded Album"
0 ignored issues
show
Documentation Bug introduced by
The doc comment "Downloaded at position 0 could not be parsed: Unknown type name '"Downloaded' at position 0 in "Downloaded.
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, $i);
0 ignored issues
show
Bug introduced by
The call to getAlbum() has too few arguments starting with i. ( Ignorable by Annotation )

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

56
    $resultAlbum = /** @scrutinizer ignore-call */ getAlbum($fb, $arr_alb, $i);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
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 "Exported Albums"
0 ignored issues
show
Documentation Bug introduced by
The doc comment "Exported at position 0 could not be parsed: Unknown type name '"Exported' at position 0 in "Exported.
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
Best Practice introduced by
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
Bug introduced by
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
Bug introduced by
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 "Get Albums array"
0 ignored issues
show
Documentation Bug introduced by
The doc comment "Get at position 0 could not be parsed: Unknown type name '"Get' at position 0 in "Get.
Loading history...
103
 */
104
function getAlbum($fb, $arr_alb, $album_name, $i)
105
{
106
    global $main_arr;
107
    
108
    foreach ($arr_alb as $graphNode) {
0 ignored issues
show
Bug introduced by
The expression $arr_alb of type string is not traversable.
Loading history...
109
        $main_arr[$i]['images'] = $graphNode['images'][0]['source'];
110
        $i++;
111
    }
112
    $arr_alb_ar = $fb->next($arr_alb);
113
    if (!empty($arr_alb_ar)) {
114
        getAlbum($fb, $arr_alb_ar, $album_name, $i);
115
    }
116
    return $main_arr;
117
}
118
119
//---------- For 1 album download -------------------------------------------------//
120
if (isset($_GET['single_album']) && !empty($_GET['single_album'])) {
121
    $single_album = explode(",", $_GET['single_album']);
122
    Download_album(
123
        $accessToken, 
124
        $album_download_directory, 
125
        $single_album[0], 
126
        $single_album[1], 
127
        $fb
128
    );
129
}
130
131
//---------- For Selected Albums download -----------------------------------------//
132
if (isset($_GET['selected_albums']) && !empty($_GET['selected_albums'])) {
133
    $selected_albums = explode("/", $_GET['selected_albums']);
134
    foreach ($selected_albums as $selected_album) {
135
        $selected_album = explode(",", $selected_album);
136
        Download_album(
137
            $accessToken, 
138
            $album_download_directory, 
139
            $selected_album[0], 
140
            $selected_album[1], 
141
            $fb
142
        );
143
    }
144
}
145
146
//---------- Download all album code --------------------------------------//
147
if (isset($_GET['all_albums']) && !empty($_GET['all_albums'])) {
148
    if ($_GET['all_albums'] == 'all_albums') {
149
        $response_albums = $fb->get('/me/albums?fields=id,name', $accessToken);
150
        $albums = $response_albums->getGraphEdge()->asArray();
151
        if (!empty($albums)) {
152
            foreach ($albums as $album) {
153
                Download_album(
154
                    $accessToken, 
155
                    $album_download_directory, 
156
                    $album['id'], 
157
                    $album['name'], 
158
                    $fb
159
                );
160
            }
161
        }
162
    }
163
}
164
165
//---------------Export Single album-------------------------//
166
if (isset($_GET['single_export']) && !empty($_GET['single_export'])) {
167
    $single_album = explode(",", $_GET['single_export']);
168
    Export_album(
169
        $accessToken,  
170
        $single_album[0], 
171
        $single_album[1],
172
        $fb
173
    );
174
}
175
176
if (isset($_GET['zip'])) {
177
    include_once 'zipper.php';
178
    $zipper = new Zipper();
179
    echo $zipper->getZip($album_download_directory);
180
}
181
?>
0 ignored issues
show
Best Practice introduced by
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...