| 16285 |
efrain |
1 |
<?php
|
|
|
2 |
declare(strict_types=1);
|
|
|
3 |
|
|
|
4 |
namespace LeadersLinked\Controller;
|
|
|
5 |
|
|
|
6 |
use Laminas\Db\Adapter\AdapterInterface;
|
| 16768 |
efrain |
7 |
|
| 16285 |
efrain |
8 |
use Laminas\Mvc\Controller\AbstractActionController;
|
|
|
9 |
use Laminas\Log\LoggerInterface;
|
|
|
10 |
use Laminas\View\Model\ViewModel;
|
|
|
11 |
use Laminas\View\Model\JsonModel;
|
|
|
12 |
use LeadersLinked\Form\Tools\UserUploadGeneratePasswordForm;
|
|
|
13 |
use PhpOffice\PhpSpreadsheet\IOFactory;
|
| 16286 |
efrain |
14 |
use LeadersLinked\Library\Functions;
|
| 16285 |
efrain |
15 |
|
|
|
16 |
class ToolsController extends AbstractActionController
|
|
|
17 |
{
|
|
|
18 |
/**
|
|
|
19 |
*
|
|
|
20 |
* @var AdapterInterface
|
|
|
21 |
*/
|
|
|
22 |
private $adapter;
|
| 16768 |
efrain |
23 |
|
| 16285 |
efrain |
24 |
|
|
|
25 |
/**
|
|
|
26 |
*
|
|
|
27 |
* @var LoggerInterface
|
|
|
28 |
*/
|
|
|
29 |
private $logger;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
*
|
|
|
33 |
* @var array
|
|
|
34 |
*/
|
|
|
35 |
private $config;
|
|
|
36 |
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
*
|
|
|
41 |
* @param AdapterInterface $adapter
|
|
|
42 |
* @param LoggerInterface $logger
|
|
|
43 |
* @param array $config
|
|
|
44 |
*/
|
| 16768 |
efrain |
45 |
public function __construct($adapter, $logger, $config)
|
| 16285 |
efrain |
46 |
{
|
|
|
47 |
$this->adapter = $adapter;
|
|
|
48 |
$this->logger = $logger;
|
|
|
49 |
$this->config = $config;
|
|
|
50 |
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
public function indexAction()
|
|
|
54 |
{
|
|
|
55 |
return new JsonModel([
|
|
|
56 |
'success' => false,
|
|
|
57 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
58 |
]);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public function userfilePasswordGeneratorAction()
|
|
|
62 |
{
|
|
|
63 |
$request = $this->getRequest();
|
|
|
64 |
|
|
|
65 |
if($request->isPost())
|
|
|
66 |
{
|
|
|
67 |
|
|
|
68 |
$form = new UserUploadGeneratePasswordForm();
|
|
|
69 |
$dataPost = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
|
|
|
70 |
|
|
|
71 |
$form->setData($dataPost);
|
|
|
72 |
|
|
|
73 |
if($form->isValid()) {
|
|
|
74 |
|
|
|
75 |
$file = $_FILES['file'];
|
|
|
76 |
$tmp_filename = $file['tmp_name'];
|
|
|
77 |
|
|
|
78 |
|
|
|
79 |
$filename = $file['name'];
|
|
|
80 |
$final_filename = 'data/' . $filename;
|
|
|
81 |
|
|
|
82 |
if(!move_uploaded_file($tmp_filename, $final_filename)) {
|
|
|
83 |
return new JsonModel([
|
|
|
84 |
'success' => false,
|
|
|
85 |
'data' => 'ERROR_UPLOAD_FILE'
|
|
|
86 |
]);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
$spreadsheet = IOFactory::load($final_filename);
|
|
|
91 |
$sheet = $spreadsheet->getActiveSheet();
|
|
|
92 |
$max = $sheet->getHighestDataRow('A');
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
$caracteres = ['#', '?', '!', '@', '$'];
|
|
|
98 |
$max_caracteres = count($caracteres);
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
for($i = 1; $i <= $max; $i++)
|
|
|
102 |
{
|
|
|
103 |
|
|
|
104 |
|
| 16766 |
efrain |
105 |
$first_name = Functions::sanitizeFilterString($sheet->getCell('A' . $i)->getValue());
|
|
|
106 |
$last_name = Functions::sanitizeFilterString($sheet->getCell('B' . $i)->getValue());
|
| 16285 |
efrain |
107 |
$email = trim(filter_var(strval($sheet->getCell('C' . $i)->getValue()), FILTER_SANITIZE_EMAIL));
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
if(empty($first_name) || empty($last_name) || !filter_var($email, FILTER_VALIDATE_EMAIL) ) {
|
|
|
113 |
continue;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
$caracter = $caracteres[ rand(0, $max_caracteres - 1) ];
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
$runmber = rand(1001, 9999);
|
|
|
120 |
|
|
|
121 |
$sfirst_name = ucfirst(substr(strtolower(str_replace(' ', '', $first_name)), 0, 4));
|
|
|
122 |
$slast_name = ucfirst(substr(strtolower(str_replace(' ', '', $last_name)), 0, 4));
|
|
|
123 |
$password = $sfirst_name . $slast_name . $runmber . $caracter;
|
|
|
124 |
|
|
|
125 |
$sheet->SetCellValue('D' . $i, $password);
|
|
|
126 |
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
$filename = $file['name'];
|
|
|
130 |
$final_filename = 'data/' . $filename;
|
|
|
131 |
|
|
|
132 |
|
| 16286 |
efrain |
133 |
|
|
|
134 |
$fileName = Functions::normalizeStringFilename($file['name']);
|
|
|
135 |
$fileName = str_replace('.xlsx', '', $fileName);
|
|
|
136 |
$fileName = str_replace('.xls', '', $fileName);
|
|
|
137 |
|
|
|
138 |
|
|
|
139 |
$fileName = $fileName . '-generado-' . date('Y-m-d-H-i-s') . '.xlsx';
|
| 16285 |
efrain |
140 |
$tempFilename = tempnam(sys_get_temp_dir(), $file['name'] . time());
|
|
|
141 |
|
|
|
142 |
$writer = IOFactory::createWriter($spreadsheet, 'Xls');
|
|
|
143 |
$writer->save($tempFilename);
|
|
|
144 |
|
|
|
145 |
$content = file_get_contents($tempFilename);
|
|
|
146 |
@unlink($final_filename);
|
|
|
147 |
@unlink($tempFilename);
|
|
|
148 |
|
|
|
149 |
return new JsonModel([
|
|
|
150 |
'success' => true,
|
|
|
151 |
'data' => [
|
|
|
152 |
'content' => base64_encode($content),
|
|
|
153 |
'basename' => $fileName
|
|
|
154 |
|
|
|
155 |
]
|
|
|
156 |
]);
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
|
165 |
} else {
|
|
|
166 |
$messages = [];
|
|
|
167 |
$form_messages = (array) $form->getMessages();
|
|
|
168 |
foreach ($form_messages as $fieldname => $field_messages) {
|
|
|
169 |
$messages[$fieldname] = array_values($field_messages);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
return new JsonModel([
|
|
|
173 |
'success' => false,
|
|
|
174 |
'data' => $messages
|
|
|
175 |
]);
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
}
|
|
|
184 |
else if($request->isGet()) {
|
|
|
185 |
$form = new UserUploadGeneratePasswordForm();
|
|
|
186 |
|
|
|
187 |
$this->layout()->setTemplate('layout/layout-backend.phtml');
|
|
|
188 |
$viewModel = new ViewModel();
|
|
|
189 |
$viewModel->setTemplate('leaders-linked/tools/password-generator-for-file.phtml');
|
|
|
190 |
$viewModel->setVariables([
|
|
|
191 |
'form' => $form
|
|
|
192 |
]);
|
|
|
193 |
|
|
|
194 |
return $viewModel ;
|
|
|
195 |
|
|
|
196 |
} else {
|
|
|
197 |
return new JsonModel([
|
|
|
198 |
'success' => false,
|
|
|
199 |
'data' => 'ERROR_METHOD_NOT_ALLOWED'
|
|
|
200 |
]);;
|
|
|
201 |
}
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
|
|
|
205 |
}
|