Passed
Push — develop ( 3bb550...c283a9 )
by Nikolay
06:01 queued 11s
created

PostController::fileReadContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 24
rs 9.6666
cc 3
nc 3
nop 0
1
<?php
2
/**
3
 * Copyright (C) MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Nikolay Beketov, 4 2020
7
 *
8
 */
9
10
namespace MikoPBX\PBXCoreREST\Controllers\System;
11
12
use MikoPBX\Common\Models\SoundFiles;
13
use MikoPBX\PBXCoreREST\Controllers\BaseController;
14
use Phalcon\Di;
15
16
/**
17
 * /pbxcore/api/system/{name}' System management (POST).
18
 *
19
 * Setup system time
20
 *   curl -X POST -d timestamp=1602509882 http://127.0.0.1/pbxcore/api/system/setDate
21
 *
22
 * Send email
23
 *   curl -X POST -d '{"email": "[email protected]", "subject":"Hi from mikopbx", "body":"Test message", "encode":""}' http://172.16.156.223/pbxcore/api/system/sendMail;
24
 *     'encode' - может быть пустой строкой или 'base64', на случай, если subject и body передаются в base64;
25
 *
26
 * Unban IP address
27
 *   curl -X POST -d '{"ip": "172.16.156.1"}' http://172.16.156.212/pbxcore/api/system/unBanIp;
28
 *   Answer example:
29
 *   {"result":"Success","data":[{"jail":"asterisk","ip":"172.16.156.1","timeofban":1522326119}],"function":"getBanIp"}
30
 *
31
 * Get config file content
32
 *   curl -X POST -d '{"filename": "/etc/asterisk/asterisk.conf"}'
33
 *   http://172.16.156.212/pbxcore/api/system/fileReadContent;
34
 *
35
 * Answer example:
36
 *   {"result":"ERROR","message":"API action not found;","function":"fileReadContent"}
37
 *   {"result":"Success","data":"W2RpcmVj","function":"fileReadContent"}
38
 *
39
 * Convert audiofile:
40
 *   curl -X POST -d '{"filename": "/tmp/WelcomeMaleMusic.mp3"}'
41
 *   http://172.16.156.212/pbxcore/api/system/convertAudioFile; Пример ответа:
42
 *   {
43
 *      "result": "Success",
44
 *      "filename": "/tmp/WelcomeMaleMusic.wav",
45
 *      "function": "convertAudioFile"
46
 *   }
47
 *
48
 *
49
 * Delete audiofile:
50
 *   curl -X POST -d '{"filename": "/storage/usbdisk1/mikopbx/tmp/2233333.wav"}'
51
 *   http://172.16.156.212/pbxcore/api/system/removeAudioFile;
52
 *
53
 *
54
 * System upgrade (from file)
55
 * curl -X POST -d
56
 *   '{"filename": "/storage/usbdisk1/mikopbx/tmp/2019.4.200-mikopbx-generic-x86-64-linux.img"}'
57
 *   http://127.0.0.1/pbxcore/api/system/upgrade -H 'Cookie: XDEBUG_SESSION=PHPSTORM'; curl -F
58
 *   "[email protected]" http://172.16.156.212/pbxcore/api/system/upgrade;
59
 *
60
 *
61
 * System upgrade (over link)
62
 * curl -X POST -d '{"md5":"df7622068d0d58700a2a624d991b6c1f", "url":
63
 *   "https://www.askozia.ru/upload/update/firmware/6.2.96-9.0-svn-mikopbx-x86-64-cross-linux.img"}'
64
 *   http://172.16.156.223/pbxcore/api/system/upgradeOnline;
65
 *
66
 *
67
 * Install new module with params by URL
68
 * curl -X POST -d '{"uniqid":"ModuleCTIClient", "md5":"fd9fbf38298dea83667a36d1d0464eae", "url":
69
 * "https://www.askozia.ru/upload/update/modules/ModuleCTIClient/ModuleCTIClientv01.zip"}'
70
 * http://172.16.156.223/pbxcore/api/modules/uploadNewModule;
71
 *
72
 *
73
 * Receive uploading status
74
 * curl  -X POST -d '{"uniqid":"ModuleSmartIVR"} http://172.16.156.223/pbxcore/api/system/statusUploadingNewModule
75
 *
76
 *
77
 * Install new module from ZIP archive:
78
 * curl -F "[email protected]" http://127.0.0.1/pbxcore/api/modules/uploadNewModule;
79
 *
80
 *
81
 * Uninstall module:
82
 * curl -X POST -d '{"uniqid":"ModuleSmartIVR"} http://172.16.156.223/pbxcore/api/system/uninstallModule
83
 *
84
 */
85
class PostController extends BaseController
86
{
87
    public function callAction($actionName): void
88
    {
89
        switch ($actionName) {
90
            case 'convertAudioFile':
91
                $this->convertAudioFile();
92
                break;
93
            default:
94
                $data = $this->request->getPost();
95
                $this->sendRequestToBackendWorker('system', $actionName, $data);
96
        }
97
    }
98
99
    /**
100
     * Categorize and store uploaded audio files
101
     */
102
    private function convertAudioFile(): void
103
    {
104
        $data                  = [];
105
        $category              = $this->request->getPost('category');
106
        $data['temp_filename'] = $this->request->getPost('temp_filename');
107
        $di                    = Di::getDefault();
108
        $mediaDir              = $di->getShared('config')->path('asterisk.customSoundDir');
109
        $mohDir                = $di->getShared('config')->path('asterisk.mohdir');
110
        switch ($category) {
111
            case SoundFiles::CATEGORY_MOH:
112
                $data['filename'] = "{$mohDir}/" . basename($data['temp_filename']);
113
                break;
114
            case SoundFiles::CATEGORY_CUSTOM:
115
                $data['filename'] = "{$mediaDir}/" . basename($data['temp_filename']);
116
                break;
117
            default:
118
                $this->sendError(400, 'Category not set');
119
        }
120
        $requestMessage = json_encode(
121
            [
122
                'processor' => 'system',
123
                'data'      => $data,
124
                'action'    => 'convertAudioFile',
125
            ]
126
        );
127
        $connection     = $this->di->getShared('beanstalkConnection');
128
        $response       = $connection->request($requestMessage, 15, 0);
129
        if ($response !== false) {
130
            $response = json_decode($response, true);
131
            $this->response->setPayloadSuccess($response);
132
        } else {
133
            $this->sendError(500);
134
        }
135
    }
136
137
}