Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
/**
3
 * Controlador para manejar todo lo relacionado con las empresas
4
 *
5
 */
6
declare(strict_types=1);
7
 
8
namespace LeadersLinked\Controller;
9
 
10
use Laminas\Db\Adapter\AdapterInterface;
11
 
12
use Laminas\Mvc\Controller\AbstractActionController;
13
use Laminas\Log\LoggerInterface;
14
use Laminas\View\Model\JsonModel;
15
use LeadersLinked\Mapper\UserMapper;
16
use LeadersLinked\Library\Rsa;
17
 
18
class MoodleController extends AbstractActionController
19
{
20
    /**
21
     *
22
     * @var \Laminas\Db\Adapter\AdapterInterface
23
     */
24
    private $adapter;
25
 
26
    /**
27
     *
28
     * @var \LeadersLinked\Cache\CacheInterface
29
     */
30
    private $cache;
31
 
32
 
33
    /**
34
     *
35
     * @var \Laminas\Log\LoggerInterface
36
     */
37
    private $logger;
38
 
39
    /**
40
     *
41
     * @var array
42
     */
43
    private $config;
44
 
45
 
46
    /**
47
     *
48
     * @var \Laminas\Mvc\I18n\Translator
49
     */
50
    private $translator;
51
 
52
 
53
    /**
54
     *
55
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
56
     * @param \LeadersLinked\Cache\CacheInterface $cache
57
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
58
     * @param array $config
59
     * @param \Laminas\Mvc\I18n\Translator $translator
60
     */
61
    public function __construct($adapter, $cache, $logger, $config, $translator)
62
    {
63
        $this->adapter      = $adapter;
64
        $this->cache        = $cache;
65
        $this->logger       = $logger;
66
        $this->config       = $config;
67
        $this->translator   = $translator;
68
    }
69
 
70
    public function indexAction()
71
    {
72
        $request = $this->getRequest();
73
        if($request->isPost()) {
74
 
75
            $currentUserPlugin = $this->plugin('currentUserPlugin');
76
            $currentUser = $currentUserPlugin->getUser();
77
 
78
            $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
79
            $currentNetwork = $currentNetworkPlugin->getNetwork();
80
 
81
 
82
            if($currentNetwork->moodle_url) {
83
                $url = $currentNetwork->moodle_url;
84
            } else {
85
 
86
 
87
                $sandbox = $this->config['leaderslinked.runmode.sandbox'];
88
 
89
                if($sandbox) {
90
                    $url = $this->config['leaderslinked.moodle.url_sandbox'];
91
                } else {
92
                    $url = $this->config['leaderslinked.moodle.url_production'];
93
                }
94
            }
95
 
96
            $username = $this->config['leaderslinked.moodle.username'];
97
            $password = $this->config['leaderslinked.moodle.password'];
98
            $rsa_n = $this->config['leaderslinked.moodle.rsa_n'];
99
            //$rsa_d = $this->config['leaderslinked.moodle.rsa_d'];
100
            $rsa_e = $this->config['leaderslinked.moodle.rsa_e'];
101
 
102
            $userMapper = UserMapper::getInstance($this->adapter);
103
            $user = $userMapper->fetchOne($currentUser->id);
104
 
105
            if($user->image) {
106
 
107
                $image = file_get_contents($this->config['leaderslinked.fullpath.user'] .$user->uuid.  DIRECTORY_SEPARATOR . '/' . $user->image);
108
 
109
            } else {
110
                $image = '';
111
            }
112
 
113
 
114
            $data = new \stdClass();
115
            $data->first_name = $user->first_name;
116
            $data->last_name = $user->last_name;
117
            $data->email = $user->email;
210 efrain 118
            $data->image_filename = '';
119
            $data->image_content = '';
120
 
121
            //$data->image_filename =  $user->image;
122
            //$data->image_content = $image ? base64_encode($image) : '';
1 efrain 123
 
124
            $data = json_encode($data);
125
 
126
            list($usec, $sec) = explode(' ', microtime());
127
            $seed = intval($sec + ((float) $usec * 100000));
128
 
129
            $timestamp  = date('Y-m-d\TH:i:s');
130
            mt_srand($seed, MT_RAND_MT19937);
131
            $rand =  mt_rand();
132
            $password  = password_hash($username.'-'. $password. '-' . $rand. '-' . $timestamp, PASSWORD_DEFAULT);
133
 
134
            $rsa = Rsa::getInstance();
135
            $data = $rsa->encrypt($data, $rsa_e, $rsa_n);
136
 
137
            $response = [
138
                'success' => true,
139
                'data' => [
140
                    'url' => $url,
141
                    'username' => $username,
142
                    'password' => $password,
143
                    'rand' => $rand,
144
                    'timestamp' => $timestamp,
145
                    'data' => base64_encode($data),
146
 
147
                ]
148
            ];
149
 
150
 
151
            return new JsonModel($response);
152
 
153
        } else {
154
            $data = [
155
                'success' => false,
156
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
157
            ];
158
 
159
            return new JsonModel($data);
160
        }
161
 
162
        return new JsonModel($data);
163
    }
164
 
165
 
166
 
167
}
168