Passed
Push — master ( c14ede...c50061 )
by Ferry
03:59
created

DeveloperPluginStoreController::fetchPluginData()   B

Complexity

Conditions 7
Paths 17

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 21
c 2
b 0
f 0
nc 17
nop 1
dl 0
loc 38
rs 8.6506
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: User
5
 * Date: 4/25/2019
6
 * Time: 9:28 PM
7
 */
8
9
namespace crocodicstudio\crudbooster\controllers;
10
11
use crocodicstudio\crudbooster\exceptions\CBValidationException;
12
use crocodicstudio\crudbooster\helpers\CurlHelper;
13
use crocodicstudio\crudbooster\helpers\ModuleGenerator;
14
use Illuminate\Support\Facades\Cache;
15
use Illuminate\Support\Facades\DB;
16
use Illuminate\Support\Facades\Hash;
17
18
class DeveloperPluginStoreController extends Controller
19
{
20
21
    private $view = "crudbooster::dev_layouts.modules.plugin";
22
23
    public function __construct()
24
    {
25
        view()->share(['page_title'=>'Plugin Store']);
26
    }
27
28
29
    public function getIndex() {
30
31
        if(request("refresh")) {
32
            $this->fetchPluginData(false );
33
            return cb()->redirectBack("Plugin list has been refreshed!","success");
34
        }
35
36
        $data = [];
37
        $data['result'] = $this->fetchPluginData();
38
        return view($this->view.'.index',$data);
39
    }
40
41
    public function postLoginAccount() {
42
        $curl = new CurlHelper("http://crudbooster.com/api/login_member");
43
        $curl->headers([
44
            "Access-Token"=>"bVc/ZnpYNSZrMVZYOHE5U2tqcSU=",
45
            "User-Agent"=>"CRUDBooster-Bot-Client"
46
        ]);
47
        $curl->data([
48
            "email"=>request("email"),
49
            "password"=>request("password")
50
        ]);
51
        $response = $curl->send();
52
53
        if($respArray = json_decode($response, true)) {
54
            if($respArray['status'] && isset($respArray['token'])) {
55
                session(['account_token'=>$respArray['token']]);
56
            }
57
            return response()->make($response,200,["Content-Type"=>"application/json"]);
58
        } else {
59
            return response()->json(['status'=>false,'message'=>'failed']);
60
        }
61
    }
62
63
    public function postRequestBuyPlugin() {
64
        $curl = new CurlHelper("http://crudbooster.com/api/request_buy_plugin");
65
        $curl->headers([
66
            "Access-Token"=>"bVc/ZnpYNSZrMVZYOHE5U2tqcSU=",
67
            "User-Agent"=>"CRUDBooster-Bot-Client"
68
        ]);
69
        $curl->data([
70
            "key"=>base64_encode(request("key")),
0 ignored issues
show
Bug introduced by
It seems like request('key') can also be of type array; however, parameter $data of base64_encode() does only seem to accept string, 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

70
            "key"=>base64_encode(/** @scrutinizer ignore-type */ request("key")),
Loading history...
71
            "token"=>base64_encode(request("token")),
72
            "ref"=>base64_encode(cb()->getDeveloperUrl("plugins"))
73
        ]);
74
        $response = $curl->send();
75
76
        if($respArray = json_decode($response, true)) {
77
            if($respArray['status']) {
78
                $form = base64_decode($respArray['payment']);
79
                return response()->json(['status'=>true,'form'=>$form]);
80
            } else {
81
                return response()->json($respArray);
82
            }
83
        } else {
84
            return response()->json(['status'=>false,'message'=>'failed','raw'=>$response]);
85
        }
86
    }
87
88
    public function getUninstall($key)
89
    {
90
        $pluginData = $this->fetchPluginData();
91
92
        if(isset($pluginData[$key])) {
93
            if(file_exists(app_path("CBPlugins/".$key))) {
94
                rrmdir(app_path("CBPlugins/".$key));
95
                return response()->json(['status'=>true, 'message'=>'Plugin has been uninstalled!']);
96
            }else{
97
                return response()->json(['status'=>false,'message'=>'Failed to uninstall, plugin is not found']);
98
            }
99
        }else {
100
            return response()->json(['status'=>false,'message'=>'Failed to uninstall, plugin key is not found']);
101
        }
102
    }
103
104
    public function getInstall($key)
105
    {
106
        $pluginData = $this->fetchPluginData();
107
108
        if(isset($pluginData[$key])) {
109
            $plugin = $pluginData[$key];
110
111
            // Create temp file of zip plugin
112
            $temp = tmpfile();
113
            fwrite($temp, file_get_contents($plugin['url_download']));
0 ignored issues
show
Bug introduced by
It seems like $temp 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

113
            fwrite(/** @scrutinizer ignore-type */ $temp, file_get_contents($plugin['url_download']));
Loading history...
114
            $filename = stream_get_meta_data($temp)['uri'];
0 ignored issues
show
Bug introduced by
It seems like $temp can also be of type false; however, parameter $stream of stream_get_meta_data() 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

114
            $filename = stream_get_meta_data(/** @scrutinizer ignore-type */ $temp)['uri'];
Loading history...
115
116
            // Extract zip plugin
117
            $zip = new \ZipArchive;
118
            $res = $zip->open($filename);
119
            if ($res === TRUE) {
120
                $zip->extractTo(app_path('CBPlugins'));
121
                $dirName = $zip->getNameIndex(0);
122
                $zip->close();
123
                fclose($temp);
0 ignored issues
show
Bug introduced by
It seems like $temp 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

123
                fclose(/** @scrutinizer ignore-type */ $temp);
Loading history...
124
125
                // Rename
126
                if(file_exists(app_path("CBPlugins/".$key))) rrmdir(app_path("CBPlugins/".$key));
127
                rename(app_path("CBPlugins/".$dirName), app_path("CBPlugins/".$key));
128
129
                return response()->json(['status'=>true,'message'=>'Install / update plugin has been succesfull!']);
130
131
            } else {
132
                return response()->json(['status'=>false,'message'=>"Failed to install/update, can't open the plugin archive"]);
133
            }
134
        }else{
135
            return response()->json(['status'=>false,'message'=>'Failed to install/update, plugin key is not found']);
136
        }
137
    }
138
139
    private function fetchPluginData($cache = true)
140
    {
141
        if($cache === true && $data = Cache::get("plugin_store_data")) {
142
            return $data;
143
        }
144
145
        $result = [];
146
147
        try {
148
            $opts = [
149
                "http" => [
150
                    "method" => "GET",
151
                    "header" => "Access-Token: bVc/ZnpYNSZrMVZYOHE5U2tqcSU=\r\n".
152
                                "User-Agent: CRUDBooster-Bot-Client"
153
                ]
154
            ];
155
            $context = stream_context_create($opts);
156
            $data = file_get_contents(base64_decode("aHR0cDovL2NydWRib29zdGVyLmNvbS9hcGkvcGx1Z2luP2FjY2Vzc190b2tlbj1iVmMvWm5wWU5TWnJNVlpZT0hFNVUydHFjU1U9"), false, $context);
157
158
            if($data) {
159
                $data = json_decode($data, true);
160
                if($data['status']==true) {
161
162
                    foreach($data['data'] as $item) {
163
                        $key = $item['key'];
164
                        $result[ $key ] = $item;
165
                    }
166
167
                    $result = collect($result)->sortBy("name")->all();
168
169
                    Cache::put("plugin_store_data", $result, now()->addDays(3));
170
                }
171
            }
172
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
173
174
        }
175
176
        return $result;
177
    }
178
}