Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
17002 efrain 1
<?php
17042 ariadna 2
 
17002 efrain 3
namespace LeadersLinked\Library;
4
 
5
use LeadersLinked\Model\User;
6
use LeadersLinked\Model\UserProfile;
7
use LeadersLinked\Model\Company;
8
use LeadersLinked\Model\Group;
17042 ariadna 9
use LeadersLinked\Model\StorageFile;
10
use LeadersLinked\Mapper\StorageFileMapper;
17002 efrain 11
 
17042 ariadna 12
class Storage
17002 efrain 13
{
17042 ariadna 14
    const FILE_TYPE_IMAGE = 'image';
15
    const FILE_TYPE_VIDEO = 'video';
16
    const FILE_TYPE_DOCUMENT = 'document';
17
 
18
    const TYPE_DEFAULT = 'default';
19
    const TYPE_CHAT = 'chat';
20
    const TYPE_GROUP = 'group';
21
    const TYPE_USER = 'user';
22
    const TYPE_IMAGE = 'image';
23
    const TYPE_JOB = 'job';
24
    const TYPE_MEDIA = 'media';
25
    const TYPE_COMPANY = 'company';
26
    const TYPE_FEED = 'feed';
27
    const TYPE_POST = 'post';
28
    const TYPE_MICROLEARNING_TOPIC = 'topic';
29
    const TYPE_MICROLEARNING_CAPSULE = 'capsule';
30
    const TYPE_MICROLEARNING_SLIDE = 'slide';
31
    const TYPE_JOB_DESCRIPTION = 'jobdesc';
32
    const TYPE_SELF_EVALUATION = 'selfval';
33
    const TYPE_PERFORMANCE_EVALUATION = 'perfeva';
34
    const TYPE_RECRUITMENT_SELECTION = 'recrsel';
35
    const TYPE_PLANNING_OBJECTIVES_AND_GOALS = 'plannig';
36
    const TYPE_MESSAGE = 'message';
37
    const TYPE_SURVEY = 'survey';
38
    const TYPE_NETWORK = 'network';
39
    const TYPE_DAILY_PULSE = 'dailpuls';
40
    const TYPE_ENGAGEMENT_REWARD = 'engarewr';
41
    const TYPE_KNOWLEDGE_AREA = 'knowarea';
42
    const TYPE_MY_COACH = 'mycoach';
43
    const TYPE_HABIT_EMOJI = 'habit-emoji';
44
    const TYPE_HABIT_CONTENT = 'habit-content';
45
 
17018 efrain 46
    const BASE_PATH = 'data' . DIRECTORY_SEPARATOR . 'storage';
17042 ariadna 47
 
17002 efrain 48
    /**
17042 ariadna 49
     * @var \LeadersLinked\Library\Storage
17002 efrain 50
     */
51
    private static $_instance;
17042 ariadna 52
 
17002 efrain 53
    /**
54
     * @var array
55
     */
56
    private $config;
17042 ariadna 57
 
17002 efrain 58
    /**
17018 efrain 59
     * @var \Laminas\Db\Adapter\AdapterInterface
17002 efrain 60
     */
17018 efrain 61
    private $adapter;
62
 
17002 efrain 63
    /**
17042 ariadna 64
     * @var string
65
     */
66
    private $tempPath;
67
 
68
    /**
69
     * @var string
70
     */
71
    private $storagePath;
72
 
73
    /**
74
     * @var array
75
     */
76
    private $currentFile;
77
 
78
    /**
79
     * @var array
80
     */
81
    private $files;
82
 
83
    /**
17002 efrain 84
     * @param array $config
17018 efrain 85
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
17002 efrain 86
     */
17042 ariadna 87
    private function __construct($config, $adapter)
17002 efrain 88
    {
17042 ariadna 89
        $this->config = $config;
90
        $this->adapter = $adapter;
91
        $this->currentFile = [];
92
 
93
        $this->storagePath = self::BASE_PATH;
94
        if (!file_exists($this->storagePath)) {
95
            mkdir($this->storagePath, 0775, true);
96
        }
97
 
98
        $this->tempPath = self::BASE_PATH . DIRECTORY_SEPARATOR . 'tmp';
99
        if (!file_exists($this->tempPath)) {
100
            mkdir($this->tempPath, 0775, true);
101
        }
17002 efrain 102
    }
17042 ariadna 103
 
17002 efrain 104
    /**
105
     * @param array $config
17018 efrain 106
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
17002 efrain 107
     * @return \LeadersLinked\Library\Storage
108
     */
17018 efrain 109
    public static function getInstance($config, $adapter)
17002 efrain 110
    {
17042 ariadna 111
        if (self::$_instance == null) {
17018 efrain 112
            self::$_instance = new Storage($config, $adapter);
17002 efrain 113
        }
17042 ariadna 114
 
17002 efrain 115
        return self::$_instance;
116
    }
17042 ariadna 117
 
17002 efrain 118
    /**
119
     *
120
     * @param User $user
121
     * @return string
122
     */
123
    public function getUserImage($user)
124
    {
17042 ariadna 125
        if ($user->image) {
17018 efrain 126
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathUser() . DIRECTORY_SEPARATOR . $user->uuid . DIRECTORY_SEPARATOR . $user->image;
17042 ariadna 127
            if (file_exists($remoto)) {
17018 efrain 128
                return $this->getPresignedUrl($remoto);
17002 efrain 129
            }
130
        }
17042 ariadna 131
 
17002 efrain 132
        $remoto = $this->config['leaderslinked.images_default.user_image'];
17018 efrain 133
        return $this->getPresignedUrl($remoto);
17002 efrain 134
    }
17042 ariadna 135
 
136
 
17002 efrain 137
    /**
138
     *
139
     * @param User $user
140
     * @param UserProfile $userProfile
141
     * @return string
142
     */
143
    public function getUserProfileImage($user, $userProfile)
144
    {
17042 ariadna 145
        if ($userProfile->image) {
17018 efrain 146
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathUser() . DIRECTORY_SEPARATOR . $user->uuid . DIRECTORY_SEPARATOR . $userProfile->image;
17042 ariadna 147
            if (file_exists($remoto)) {
17018 efrain 148
                return $this->getPresignedUrl($remoto);
17002 efrain 149
            }
150
        }
17042 ariadna 151
 
17002 efrain 152
        $remoto = $this->config['leaderslinked.images_default.user_profile'];
17018 efrain 153
        return $this->getPresignedUrl($remoto);
17002 efrain 154
    }
17042 ariadna 155
 
17002 efrain 156
    /**
157
     *
158
     * @param User $user
159
     * @param UserProfile $userProfile
160
     * @return string
161
     */
162
    public function getUserProfileCover($user, $userProfile)
163
    {
17042 ariadna 164
        if ($userProfile->cover) {
17018 efrain 165
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathUser() . DIRECTORY_SEPARATOR . $user->uuid . DIRECTORY_SEPARATOR . $userProfile->cover;
17042 ariadna 166
            if (file_exists($remoto)) {
17018 efrain 167
                return $this->getPresignedUrl($remoto);
17002 efrain 168
            }
169
        }
17042 ariadna 170
 
17002 efrain 171
        $remoto = $this->config['leaderslinked.images_default.user_cover'];
17018 efrain 172
        return $this->getPresignedUrl($remoto);
17002 efrain 173
    }
17042 ariadna 174
 
17002 efrain 175
    /**
176
     *
177
     * @param Company $company
178
     * @return string
179
     */
180
    public function getCompanyImage($company)
181
    {
17042 ariadna 182
        if ($company->image) {
17018 efrain 183
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathCompany() . DIRECTORY_SEPARATOR . $company->uuid . DIRECTORY_SEPARATOR . $company->image;
17042 ariadna 184
            if (file_exists($remoto)) {
17018 efrain 185
                return $this->getPresignedUrl($remoto);
17002 efrain 186
            }
187
        }
17042 ariadna 188
 
17002 efrain 189
        $remoto = $this->config['leaderslinked.images_default.company_profile'];
17018 efrain 190
        return $this->getPresignedUrl($remoto);
17002 efrain 191
    }
17042 ariadna 192
 
17002 efrain 193
    /**
194
     *
195
     * @param string $code
196
     * @param string $filename
197
     * @return string
198
     */
199
    public function getCompanyImageForCodeAndFilename($code, $filename)
200
    {
17042 ariadna 201
        if ($filename) {
17018 efrain 202
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathCompany() . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
17042 ariadna 203
            if (file_exists($remoto)) {
17018 efrain 204
                return $this->getPresignedUrl($remoto);
17002 efrain 205
            }
206
        }
17042 ariadna 207
 
17002 efrain 208
        $remoto = $this->config['leaderslinked.images_default.company_profile'];
17018 efrain 209
        return $this->getPresignedUrl($remoto);
17002 efrain 210
    }
17042 ariadna 211
 
17002 efrain 212
    /**
213
     *
214
     * @param Company $company
215
     * @return string
216
     */
217
    public function getCompanyCover($company)
218
    {
17042 ariadna 219
        if ($company->cover) {
17018 efrain 220
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathCompany() . DIRECTORY_SEPARATOR . $company->uuid . DIRECTORY_SEPARATOR . $company->cover;
17042 ariadna 221
            if (file_exists($remoto)) {
17018 efrain 222
                return $this->getPresignedUrl($remoto);
17002 efrain 223
            }
224
        }
17042 ariadna 225
 
17002 efrain 226
        $remoto = $this->config['leaderslinked.images_default.company_cover'];
17018 efrain 227
        return $this->getPresignedUrl($remoto);
17002 efrain 228
    }
17042 ariadna 229
 
17002 efrain 230
    /**
231
     *
232
     * @param Group $group
233
     * @return string
234
     */
235
    public function getGroupImage($group)
236
    {
17042 ariadna 237
        if ($group->image) {
17018 efrain 238
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathGroup() . DIRECTORY_SEPARATOR . $group->uuid . DIRECTORY_SEPARATOR . $group->image;
17042 ariadna 239
            if (file_exists($remoto)) {
17018 efrain 240
                return $this->getPresignedUrl($remoto);
17002 efrain 241
            }
242
        }
17042 ariadna 243
 
17002 efrain 244
        $remoto = $this->config['leaderslinked.images_default.group_profile'];
17018 efrain 245
        return $this->getPresignedUrl($remoto);
17002 efrain 246
    }
17042 ariadna 247
 
17002 efrain 248
    /**
249
     *
250
     * @param string $code
251
     * @param string $filename
252
     * @return string
253
     */
254
    public function getGroupImageForCodeAndFilename($code, $filename)
255
    {
17042 ariadna 256
        if ($filename) {
257
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathGroup() . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR .  $filename;
258
            if (file_exists($remoto)) {
17018 efrain 259
                return $this->getPresignedUrl($remoto);
17002 efrain 260
            }
261
        }
17042 ariadna 262
 
17002 efrain 263
        $remoto = $this->config['leaderslinked.images_default.group_profile'];
17018 efrain 264
        return $this->getPresignedUrl($remoto);
17002 efrain 265
    }
17042 ariadna 266
 
17002 efrain 267
    /**
268
     *
269
     * @param Group $group
270
     * @return string
271
     */
272
    public function getGroupCover($group)
273
    {
17042 ariadna 274
        if ($group->cover) {
275
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $this->getPathGroup() . DIRECTORY_SEPARATOR . $group->uuid . DIRECTORY_SEPARATOR .  $group->cover;
276
            if (file_exists($remoto)) {
17018 efrain 277
                return $this->getPresignedUrl($remoto);
17002 efrain 278
            }
279
        }
17042 ariadna 280
 
17002 efrain 281
        $remoto = $this->config['leaderslinked.images_default.group_cover'];
17018 efrain 282
        return $this->getPresignedUrl($remoto);
17002 efrain 283
    }
17042 ariadna 284
 
17002 efrain 285
    /**
286
     *
287
     * @param Group $group
288
     * @return string
289
     */
290
    public function getGenericImage($path, $code, $filename)
291
    {
17045 ariadna 292
        $remoto = $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
17042 ariadna 293
 
294
        if (file_exists($remoto)) {
17018 efrain 295
            return $this->getPresignedUrl($remoto);
17002 efrain 296
        }
17045 ariadna 297
 
298
        $remoto = $this->getFullPathImageDefault();
299
        return $this->getPresignedUrl($remoto);
17002 efrain 300
    }
17042 ariadna 301
 
302
 
303
 
17002 efrain 304
    /**
305
     *
17008 efrain 306
     * @param string $path
17018 efrain 307
     * @param string $code,
17008 efrain 308
     * @param string $filename
17018 efrain 309
     * @param boolean $checkExists
17002 efrain 310
     * @return string
311
     */
17018 efrain 312
    public function getFileFromDisk($path, $code, $filename)
17002 efrain 313
    {
17042 ariadna 314
        $current_file = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR .  $filename;
315
 
316
        if (file_exists($current_file)) {
17018 efrain 317
            return $current_file;
17042 ariadna 318
        }
17018 efrain 319
        return;
320
    }
17042 ariadna 321
 
322
 
17018 efrain 323
    /**
324
     *
325
     * @param string $path
326
     * @param string $code,
327
     * @return string
328
     */
329
    public function getDirectoryFromDisk($path, $code)
330
    {
17042 ariadna 331
        $directory = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code;
332
 
333
        if (file_exists($directory)) {
17018 efrain 334
            return $directory;
335
        }
336
        return;
337
    }
17042 ariadna 338
 
17018 efrain 339
    /**
340
     *
341
     * @param string $path
342
     * @param string $code,
343
     * @param string $filename
344
     * @param boolean $checkExists
345
     * @return string
346
     */
347
    public function getGenericFile($path, $code, $filename, $checkExists = true)
348
    {
17042 ariadna 349
        $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR .  $filename;
17018 efrain 350
 
17042 ariadna 351
 
352
        if ($checkExists) {
353
            if (file_exists($remoto)) {
17018 efrain 354
                return $this->getPresignedUrl($remoto);
355
            } else {
356
                return;
17042 ariadna 357
            }
358
        }
17018 efrain 359
        return $this->getPresignedUrl($remoto);
360
    }
17042 ariadna 361
 
17018 efrain 362
    public function getPathByType($type)
363
    {
17042 ariadna 364
        switch ($type) {
17044 ariadna 365
            case self::TYPE_DEFAULT:
366
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'default';
367
                break;
17042 ariadna 368
 
17044 ariadna 369
            case self::TYPE_CHAT:
370
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'chat';
371
                break;
17042 ariadna 372
 
17044 ariadna 373
            case self::TYPE_GROUP:
374
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'group';
375
                break;
17042 ariadna 376
 
17044 ariadna 377
            case self::TYPE_USER:
378
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'user';
379
                break;
17042 ariadna 380
 
17044 ariadna 381
            case self::TYPE_IMAGE:
382
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'image';
383
                break;
17042 ariadna 384
 
17044 ariadna 385
            case self::TYPE_JOB:
386
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'job';
387
                break;
17042 ariadna 388
 
17044 ariadna 389
            case self::TYPE_COMPANY:
390
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'company';
391
                break;
17042 ariadna 392
 
17044 ariadna 393
            case self::TYPE_FEED:
394
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'feed';
395
                break;
17042 ariadna 396
 
17044 ariadna 397
            case self::TYPE_MEDIA:
398
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'media';
399
                break;
17042 ariadna 400
 
17044 ariadna 401
            case self::TYPE_POST:
402
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'post';
403
                break;
17042 ariadna 404
 
17044 ariadna 405
            case self::TYPE_MICROLEARNING_TOPIC:
406
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'topic';
407
                break;
17042 ariadna 408
 
17044 ariadna 409
            case self::TYPE_MICROLEARNING_CAPSULE:
410
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'capsule';
411
                break;
17042 ariadna 412
 
17044 ariadna 413
            case self::TYPE_MICROLEARNING_SLIDE:
414
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'microlearning' . DIRECTORY_SEPARATOR . 'slide';
415
                break;
17042 ariadna 416
 
17044 ariadna 417
            case self::TYPE_JOB_DESCRIPTION:
418
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'job-description';
419
                break;
17042 ariadna 420
 
17044 ariadna 421
            case self::TYPE_SELF_EVALUATION:
422
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'self-evaluation';
423
                break;
17042 ariadna 424
 
17044 ariadna 425
            case self::TYPE_PERFORMANCE_EVALUATION:
426
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'performance-evaluation';
427
                break;
17042 ariadna 428
 
17044 ariadna 429
            case self::TYPE_RECRUITMENT_SELECTION:
430
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'recruitment-selection';
431
                break;
17042 ariadna 432
 
17044 ariadna 433
            case self::TYPE_PLANNING_OBJECTIVES_AND_GOALS:
434
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'planning-objectives-and-goals';
435
                break;
17042 ariadna 436
 
17044 ariadna 437
            case self::TYPE_MESSAGE:
438
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'message';
439
                break;
17042 ariadna 440
 
17044 ariadna 441
            case self::TYPE_SURVEY:
442
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'survey';
443
                break;
17042 ariadna 444
 
17044 ariadna 445
            case self::TYPE_NETWORK:
446
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'network';
447
                break;
17042 ariadna 448
 
17044 ariadna 449
            case self::TYPE_DAILY_PULSE:
450
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'daily-pulse';
451
                break;
17042 ariadna 452
 
17044 ariadna 453
            case self::TYPE_ENGAGEMENT_REWARD:
454
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'engagement-reward';
455
                break;
17042 ariadna 456
 
17044 ariadna 457
            case self::TYPE_KNOWLEDGE_AREA:
458
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'knowledge-area';
459
                break;
17042 ariadna 460
 
17044 ariadna 461
            case self::TYPE_MY_COACH:
462
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'my-coach';
463
                break;
17042 ariadna 464
 
17044 ariadna 465
            case self::TYPE_HABIT_EMOJI:
466
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'habit' . DIRECTORY_SEPARATOR . 'emoji';
467
                break;
17042 ariadna 468
 
17044 ariadna 469
            case self::TYPE_HABIT_CONTENT:
470
                $path = $this->storagePath . DIRECTORY_SEPARATOR . 'habit' . DIRECTORY_SEPARATOR . 'content';
471
                break;
472
 
17042 ariadna 473
            default:
17044 ariadna 474
                $path = $this->storagePath;
475
                break;
17018 efrain 476
        }
17044 ariadna 477
 
478
        if (! file_exists($path)) {
479
            mkdir($path, 0775, true);
480
        }
481
 
482
        return $path;
17018 efrain 483
    }
17042 ariadna 484
 
17018 efrain 485
    /**
486
     *
487
     * @param Group $group
488
     * @return string
489
     */
490
    public function getGenericImageByType($type, $code, $filename)
491
    {
492
        $path = $this->getPathByType($type);
17042 ariadna 493
        $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR .  $filename;
17018 efrain 494
 
17042 ariadna 495
        if (file_exists($remoto)) {
17018 efrain 496
            return $this->getPresignedUrl($remoto);
17002 efrain 497
        } else {
17018 efrain 498
            $remoto = $this->config['leaderslinked.images_default.no_image'];
499
            return $this->getPresignedUrl($remoto);
17002 efrain 500
        }
501
    }
17042 ariadna 502
 
17018 efrain 503
    /**
504
     *
505
     * @param string $type
506
     * @param string $code,
507
     * @param string $filename
508
     * @param boolean $checkExists
509
     * @return string
510
     */
511
    public function getGenericFileByType($type, $code, $filename, $checkExists = true)
512
    {
513
        $path = $this->getPathByType($type);
17042 ariadna 514
        $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR .  $filename;
17018 efrain 515
 
17042 ariadna 516
        if ($checkExists) {
517
            if (file_exists($remoto)) {
17018 efrain 518
                return $this->getPresignedUrl($remoto);
519
            } else {
520
                return;
521
            }
522
        }
523
        return $this->getPresignedUrl($remoto);
524
    }
17042 ariadna 525
 
17002 efrain 526
    /**
527
     *
17008 efrain 528
     * @param string $path
529
     * @param string $code
530
     * @param string $filename
17002 efrain 531
     * @return string
532
     */
17008 efrain 533
    public function delete($path, $code, $filename)
534
    {
17042 ariadna 535
        $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR .  $filename;
17018 efrain 536
        return @unlink($remoto);
17042 ariadna 537
    }
17018 efrain 538
 
17042 ariadna 539
 
540
 
17008 efrain 541
    /**
542
     *
543
     * @return string
544
     */
17002 efrain 545
    public function getPathMedia()
546
    {
17018 efrain 547
        return $this->config['leaderslinked.storage.fullpath_media'];
17002 efrain 548
    }
17042 ariadna 549
 
550
 
17002 efrain 551
    /**
17018 efrain 552
     *
553
     * @return string
554
     */
555
    public function getPathHabitEmoji()
556
    {
557
        return $this->config['leaderslinked.storage.fullpath_habit_emoji'];
558
    }
17042 ariadna 559
 
17018 efrain 560
    /**
561
     *
562
     * @return string
563
     */
564
    public function getPathHabitContent()
565
    {
566
        return $this->config['leaderslinked.storage.fullpath_habit_content'];
567
    }
568
 
17042 ariadna 569
 
17018 efrain 570
    /**
17002 efrain 571
     *
572
     * @return string
573
     */
574
    public function getPathGroup()
575
    {
17018 efrain 576
        return $this->config['leaderslinked.storage.fullpath_group'];
17002 efrain 577
    }
17042 ariadna 578
 
17002 efrain 579
    /**
580
     *
581
     * @return string
582
     */
583
    public function getPathUser()
584
    {
17018 efrain 585
        return $this->config['leaderslinked.storage.fullpath_user'];
17002 efrain 586
    }
17042 ariadna 587
 
17002 efrain 588
    /**
589
     *
590
     * @return string
591
     */
592
    public function getPathImage()
593
    {
17018 efrain 594
        return $this->config['leaderslinked.storage.fullpath_image'];
17002 efrain 595
    }
17042 ariadna 596
 
17002 efrain 597
    /**
598
     *
599
     * @return string
600
     */
601
    public function getPathJob()
602
    {
17018 efrain 603
        return $this->config['leaderslinked.storage.fullpath_job'];
17002 efrain 604
    }
17042 ariadna 605
 
17002 efrain 606
    /**
607
     *
608
     * @return string
609
     */
17018 efrain 610
    public function getPathChat()
611
    {
612
        return $this->config['leaderslinked.storage.fullpath_chat'];
613
    }
17042 ariadna 614
 
17018 efrain 615
    /**
616
     *
617
     * @return string
618
     */
17002 efrain 619
    public function getPathCompany()
620
    {
17018 efrain 621
        return $this->config['leaderslinked.storage.fullpath_company'];
17002 efrain 622
    }
17042 ariadna 623
 
17002 efrain 624
    /**
625
     *
626
     * @return string
627
     */
628
    public function getPathFeed()
629
    {
17018 efrain 630
        return $this->config['leaderslinked.storage.fullpath_feed'];
17002 efrain 631
    }
17042 ariadna 632
 
633
 
634
 
17002 efrain 635
    /**
636
     *
637
     * @return string
638
     */
17018 efrain 639
    public function getPathMessage()
640
    {
641
        return $this->config['leaderslinked.storage.fullpath_message'];
642
    }
17042 ariadna 643
 
17018 efrain 644
    /**
645
     *
646
     * @return string
647
     */
17002 efrain 648
    public function getPathPost()
649
    {
17018 efrain 650
        return $this->config['leaderslinked.storage.fullpath_post'];
17002 efrain 651
    }
17042 ariadna 652
 
17002 efrain 653
    /**
654
     *
655
     * @return string
656
     */
657
    public function getPathMicrolearningTopic()
658
    {
17018 efrain 659
        return $this->config['leaderslinked.storage.fullpath_microlearning_topic'];
17002 efrain 660
    }
17042 ariadna 661
 
17002 efrain 662
    /**
663
     *
664
     * @return string
665
     */
666
    public function getPathMicrolearningCapsule()
667
    {
17018 efrain 668
        return $this->config['leaderslinked.storage.fullpath_microlearning_capsule'];
17002 efrain 669
    }
17042 ariadna 670
 
17002 efrain 671
    /**
672
     *
673
     * @return string
674
     */
675
    public function getPathMicrolearningSlide()
676
    {
17018 efrain 677
        return $this->config['leaderslinked.storage.fullpath_microlearning_slide'];
17002 efrain 678
    }
17042 ariadna 679
 
17002 efrain 680
    /**
681
     *
682
     * @return string
683
     */
684
    public function getPathJobDescription()
685
    {
17018 efrain 686
        return $this->config['leaderslinked.storage.fullpath_job_description'];
17002 efrain 687
    }
17042 ariadna 688
 
17002 efrain 689
    /**
690
     *
691
     * @return string
692
     */
17018 efrain 693
    public function getPathSelfEvaluation()
17002 efrain 694
    {
17018 efrain 695
        return $this->config['leaderslinked.storage.fullpath_self_evaluation'];
17002 efrain 696
    }
17042 ariadna 697
 
17002 efrain 698
    /**
699
     *
700
     * @return string
701
     */
702
    public function getPathRecruitmentSelection()
703
    {
17018 efrain 704
        return $this->config['leaderslinked.storage.fullpath_recruitment_selection'];
17002 efrain 705
    }
17042 ariadna 706
 
17002 efrain 707
    /**
708
     *
709
     * @return string
710
     */
711
    public function getPathPerformanceEvaluation()
712
    {
17018 efrain 713
        return $this->config['leaderslinked.storage.fullpath_performance_evaluation'];
17002 efrain 714
    }
17042 ariadna 715
 
17002 efrain 716
    /**
717
     *
718
     * @return string
719
     */
720
    public function getPathPlanningObjectivesAnGoals()
721
    {
17018 efrain 722
        return $this->config['leaderslinked.storage.fullpath_planning_objectives_and_goals'];
17002 efrain 723
    }
17042 ariadna 724
 
17002 efrain 725
    /**
726
     *
727
     * @return string
728
     */
729
    public function getPathSurvey()
730
    {
17018 efrain 731
        return $this->config['leaderslinked.storage.fullpath_survey'];
17002 efrain 732
    }
17042 ariadna 733
 
17002 efrain 734
    /**
735
     *
736
     * @return string
737
     */
738
    public function getPathNetwork()
739
    {
17018 efrain 740
        return $this->config['leaderslinked.storage.fullpath_network'];
17002 efrain 741
    }
17042 ariadna 742
 
17002 efrain 743
    /**
744
     *
745
     * @return string
746
     */
747
    public function getPathEngagementReward()
748
    {
17018 efrain 749
        return $this->config['leaderslinked.storage.fullpath_engagement_reward'];
17002 efrain 750
    }
17042 ariadna 751
 
17002 efrain 752
    public function getPathDailyPulse()
753
    {
17044 ariadna 754
        return $this->getPathByType(self::TYPE_DAILY_PULSE);
17002 efrain 755
    }
17042 ariadna 756
 
17002 efrain 757
    /**
758
     *
759
     * @return string
760
     */
761
    public function getPathKnowledgeArea()
762
    {
17018 efrain 763
        return $this->config['leaderslinked.storage.fullpath_knowledge_area'];
17002 efrain 764
    }
17042 ariadna 765
 
17002 efrain 766
    /**
767
     *
768
     * @return string
769
     */
770
    public function getPathMyCoach()
771
    {
17018 efrain 772
        return $this->config['leaderslinked.storage.fullpath_my_coach'];
17002 efrain 773
    }
17042 ariadna 774
 
17002 efrain 775
    /**
776
     *
777
     * @param String $filename
778
     * return boolean
779
     */
17042 ariadna 780
    public function objectExist($filename)
17002 efrain 781
    {
17018 efrain 782
        return file_exists($filename);
17002 efrain 783
    }
17042 ariadna 784
 
17002 efrain 785
    /**
786
     *
17018 efrain 787
     * @param string $remote
17002 efrain 788
     * @param string $local
789
     * @return boolean
790
     */
791
    public function putObject($remote, $local)
792
    {
17042 ariadna 793
 
17018 efrain 794
        $dir = dirname($remote);
17042 ariadna 795
        if (!file_exists($dir)) {
17018 efrain 796
            @mkdir($dir, 0755, true);
797
        }
17042 ariadna 798
 
17018 efrain 799
        return @rename($local, $remote);
17002 efrain 800
    }
17042 ariadna 801
 
802
 
17002 efrain 803
    /**
804
     *
805
     * @param string $filename
806
 
807
     * @return boolean
808
     */
809
    public function deleteObject($filename)
810
    {
17018 efrain 811
        return @unlink($filename);
17002 efrain 812
    }
17042 ariadna 813
 
814
 
17002 efrain 815
    /**
816
     *
817
     * @param string $path
818
     * @param string $code
819
     * @param string $filename
820
     * @return boolean
821
     */
822
    public function deleteFile($path, $code, $filename)
823
    {
17042 ariadna 824
        if ($code) {
825
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR .  $filename;
17002 efrain 826
        } else {
17018 efrain 827
            $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR .  $filename;
17002 efrain 828
        }
17042 ariadna 829
        if (file_exists($remoto)) {
17018 efrain 830
            return @unlink($remoto);
17002 efrain 831
        } else {
832
            return true;
833
        }
834
    }
17042 ariadna 835
 
17002 efrain 836
    /**
837
     *
838
     * @param string $path
839
     * @param string $code
840
     * @return boolean
841
     */
17018 efrain 842
    public function deleteDirectory($path, $code)
17002 efrain 843
    {
17018 efrain 844
        $remoto = self::BASE_PATH . DIRECTORY_SEPARATOR .  $path . DIRECTORY_SEPARATOR . $code;
17042 ariadna 845
        if (file_exists($remoto)) {
17018 efrain 846
            $this->deleteDirectoryRecursive($remoto);
847
            return true;
848
        } else {
849
            return true;
850
        }
851
    }
17042 ariadna 852
 
17018 efrain 853
    /**
854
     *
855
     * @param string $dir
856
     */
17042 ariadna 857
    private function deleteDirectoryRecursive($dir)
858
    {
17018 efrain 859
        if (is_dir($dir)) {
860
            $objects = scandir($dir);
861
            foreach ($objects as $object) {
17042 ariadna 862
                if ($object != '' . '' && $object != '..') {
863
                    if (is_dir($dir . DIRECTORY_SEPARATOR . $object) && !is_link($dir . '/' . $object)) {
864
                        @rmdir($dir . DIRECTORY_SEPARATOR . $object);
865
                    } else {
866
                        @unlink($dir . DIRECTORY_SEPARATOR . $object);
17018 efrain 867
                    }
868
                }
869
            }
870
            @rmdir($dir);
871
        }
872
    }
873
 
874
 
17042 ariadna 875
 
17018 efrain 876
    /**
877
     *
878
     * @param string $path
879
     * @param string $code
880
     * @param string $local_filename
881
     * @return boolean
882
     */
883
    public function putFile($path, $code, $local_filename)
884
    {
17042 ariadna 885
        if ($code) {
17018 efrain 886
            $remote = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR .  $code . DIRECTORY_SEPARATOR . basename($local_filename);
17002 efrain 887
        } else {
17018 efrain 888
            $remote = self::BASE_PATH . DIRECTORY_SEPARATOR . $path  . DIRECTORY_SEPARATOR .  basename($local_filename);
17002 efrain 889
        }
17018 efrain 890
 
891
        $dir = dirname($remote);
17042 ariadna 892
        if (!file_exists($dir)) {
17018 efrain 893
            @mkdir($dir, 0755, true);
894
        }
17002 efrain 895
 
17042 ariadna 896
 
17018 efrain 897
        return @rename($local_filename, $remote);
17042 ariadna 898
    }
17018 efrain 899
 
17042 ariadna 900
 
17002 efrain 901
    /**
902
     *
17018 efrain 903
     * @param string $tempfile
17002 efrain 904
     * @param string $path
905
     * @param string $code
906
     * @param string $filename
907
     * @return boolean
908
     */
17018 efrain 909
    public function moveAndPutFile($tempfile, $path, $code, $filename)
17002 efrain 910
    {
17042 ariadna 911
        if ($code) {
17018 efrain 912
            $target_file = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $code . DIRECTORY_SEPARATOR . $filename;
17002 efrain 913
        } else {
17018 efrain 914
            $target_file = self::BASE_PATH . DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $filename;
17002 efrain 915
        }
17018 efrain 916
 
17042 ariadna 917
 
17018 efrain 918
        $dir = dirname($target_file);
17042 ariadna 919
        if (!file_exists($dir)) {
17018 efrain 920
            @mkdir($dir, 0755, true);
921
        }
17042 ariadna 922
 
17018 efrain 923
        return move_uploaded_file($tempfile, $target_file);
924
    }
17042 ariadna 925
 
17018 efrain 926
    /**
927
     *
928
     * @return string
929
     */
930
    public function getStoagePath()
931
    {
932
        return 'data' . DIRECTORY_SEPARATOR . 'storage';
933
    }
17042 ariadna 934
 
17018 efrain 935
    /**
936
     *
937
     * @return string
938
     */
17042 ariadna 939
    public function getTempPath()
940
    {
17018 efrain 941
        $interal_path   = 'data' . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'tmp';
17042 ariadna 942
        if (!file_exists($interal_path)) {
17018 efrain 943
            mkdir($interal_path, 0775);
944
        }
17042 ariadna 945
 
17018 efrain 946
        return $interal_path;
17002 efrain 947
    }
17042 ariadna 948
 
17002 efrain 949
    /**
950
     *
17018 efrain 951
     * @param string $url
17002 efrain 952
     * @return string
953
     */
17018 efrain 954
    public function getPresignedUrl($url)
17002 efrain 955
    {
17018 efrain 956
 
17042 ariadna 957
 
958
 
17018 efrain 959
        $code = hash('sha256', $url);
17042 ariadna 960
 
17018 efrain 961
        $storageFileMapper = \LeadersLinked\Mapper\StorageFileMapper::getInstance($this->adapter);
962
        $storageFile = $storageFileMapper->fetchOneByCode($code);
17042 ariadna 963
        if (!$storageFile) {
17018 efrain 964
            $storageFile = new \LeadersLinked\Model\StorageFile();
965
            $storageFile->code = $code;
966
            $storageFile->path = $url;
967
            $storageFile->salt = \LeadersLinked\Library\Functions::generatePassword(8);
17042 ariadna 968
 
969
 
970
 
17018 efrain 971
            $storageFileMapper->insert($storageFile);
972
        }
17042 ariadna 973
 
17018 efrain 974
        /*
975
        $hostname = empty($_SERVER['HTTP_ORIGIN']) ? '' : $_SERVER['HTTP_ORIGIN'];
976
 
977
        if(empty($hostname)) {
978
 
979
            $hostname = empty($_SERVER['HTTP_REFERER']) ?  '' : $_SERVER['HTTP_REFERER'];
980
 
981
            if(empty($hostname)) {
982
                $hostname = empty($_SERVER['HTTP_HOST']) ?  '' : $_SERVER['HTTP_HOST'];
983
 
984
            }
985
        }
986
 
987
        print_r($_SERVER);
988
 
989
 
990
        $hostname = trim(str_replace('http://', 'https://', $hostname));
991
        */
17042 ariadna 992
 
993
 
994
 
995
 
996
 
997
 
998
 
999
 
17018 efrain 1000
        return 'https://' . $_SERVER['SERVER_NAME'] . '/storage/' . $code;
17002 efrain 1001
    }
17042 ariadna 1002
 
1003
    /**
1004
     * Creates a new GD image resource with transparency settings for PNG.
1005
     * @param int $width The width of the new image.
1006
     * @param int $height The height of the new image.
1007
     * @return \GdImage|false The image resource or false on failure.
1008
     */
1009
    private function _createImageCanvas($width, $height)
1010
    {
1011
        $new_image = imageCreateTrueColor($width, $height);
1012
        if ($new_image === false) {
1013
            error_log("_createImageCanvas: imageCreateTrueColor failed for dimensions {$width}x{$height}.");
1014
            return false;
1015
        }
1016
        imageAlphaBlending($new_image, False);
1017
        imageSaveAlpha($new_image, True);
1018
        $transparent = imageColorAllocateAlpha($new_image, 0, 0, 0, 127);
1019
        if ($transparent === false) {
1020
            error_log("_createImageCanvas: imageColorAllocateAlpha failed.");
1021
            imagedestroy($new_image);
1022
            return false;
1023
        }
1024
        imagefill($new_image, 0, 0, $transparent);
1025
        return $new_image;
1026
    }
1027
 
1028
    /**
1029
     * Saves a GD image resource as PNG, unlinks source, and cleans up image resource.
1030
     * @param \GdImage $imageResource The GD image resource to save.
1031
     * @param string $targetFilename The path to save the PNG file to.
1032
     * @param string $sourceFilenameToUnlink The path to the source file to unlink.
1033
     * @return bool True on success, false on failure.
1034
     */
1035
    private function _savePngAndCleanup($imageResource, $targetFilename, $sourceFilenameToUnlink)
1036
    {
1037
        $save_success = imagepng($imageResource, $targetFilename);
1038
        imagedestroy($imageResource); // Clean up the image resource
1039
 
1040
        if ($save_success) {
1041
            if ($sourceFilenameToUnlink && file_exists($sourceFilenameToUnlink)) {
1042
                unlink($sourceFilenameToUnlink);
1043
            }
1044
            return true;
1045
        } else {
1046
            error_log("_savePngAndCleanup: imagepng failed to save image to {$targetFilename}.");
1047
            return false;
1048
        }
1049
    }
1050
 
1051
    /**
1052
     * @param string $source_filename
1053
     * @param string $target_filename
1054
     * @param int $target_width
1055
     * @param int $target_height
1056
     * @return boolean
1057
     */
1058
    public function uploadImageResize($source_filename, $target_filename, $target_width, $target_height)
1059
    {
1060
        try {
1061
            $data = file_get_contents($source_filename);
1062
            $img = imagecreatefromstring($data);
1063
 
1064
            if ($img) {
1065
                list($source_width, $source_height) = getimagesize($source_filename);
1066
 
1067
                $width_ratio = $target_width / $source_width;
1068
                $height_ratio = $target_height / $source_height;
1069
 
1070
                if ($width_ratio > $height_ratio) {
1071
                    $resized_width = $target_width;
1072
                    $resized_height = $source_height * $width_ratio;
1073
                } else {
1074
                    $resized_height = $target_height;
1075
                    $resized_width = $source_width * $height_ratio;
1076
                }
1077
 
1078
                $resized_width = round($resized_width);
1079
                $resized_height = round($resized_height);
1080
 
1081
                $new_image = $this->_createImageCanvas($target_width, $target_height);
1082
                if ($new_image === false) {
1083
                    imagedestroy($img);
1084
                    unlink($source_filename);
1085
                    return false;
1086
                }
1087
 
1088
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
1089
                imagedestroy($img);
1090
 
1091
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
1092
            }
1093
 
1094
            unlink($source_filename);
1095
            return false;
1096
        } catch (\Throwable $e) {
1097
            error_log($e->getTraceAsString());
1098
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1099
                imagedestroy($img);
1100
            }
1101
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1102
                imagedestroy($new_image);
1103
            }
1104
            if (isset($source_filename) && file_exists($source_filename)) {
1105
                unlink($source_filename);
1106
            }
1107
            return false;
1108
        }
1109
    }
1110
 
1111
    /**
1112
     * @param string $source_filename
1113
     * @param string $target_filename
1114
     * @param int $target_width
1115
     * @param int $target_height
1116
     * @return boolean
1117
     */
1118
    public function uploadImageCrop($source_filename, $target_filename, $target_width, $target_height)
1119
    {
1120
        try {
1121
            $data = file_get_contents($source_filename);
1122
            $img = imagecreatefromstring($data);
1123
 
1124
            if ($img) {
1125
                list($source_width, $source_height) = getimagesize($source_filename);
1126
 
1127
                $width_ratio = $target_width / $source_width;
1128
                $height_ratio = $target_height / $source_height;
1129
                if ($width_ratio > $height_ratio) {
1130
                    $resized_width = $target_width;
1131
                    $resized_height = $source_height * $width_ratio;
1132
                } else {
1133
                    $resized_height = $target_height;
1134
                    $resized_width = $source_width * $height_ratio;
1135
                }
1136
 
1137
                $resized_width = round($resized_width);
1138
                $resized_height = round($resized_height);
1139
 
1140
                $offset_width = round(($target_width - $resized_width) / 2);
1141
                $offset_height = round(($target_height - $resized_height) / 2);
1142
 
1143
                $new_image = $this->_createImageCanvas($target_width, $target_height);
1144
                if ($new_image === false) {
1145
                    imagedestroy($img);
1146
                    unlink($source_filename);
1147
                    return false;
1148
                }
1149
 
1150
                imageCopyResampled($new_image, $img, $offset_width, $offset_height, 0, 0, $resized_width, $resized_height, $source_width, $source_height);
1151
                imagedestroy($img);
1152
 
1153
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
1154
            }
1155
 
1156
            unlink($source_filename);
1157
            return false;
1158
        } catch (\Throwable $e) {
1159
            error_log($e->getTraceAsString());
1160
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1161
                imagedestroy($img);
1162
            }
1163
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1164
                imagedestroy($new_image);
1165
            }
1166
            if (isset($source_filename) && file_exists($source_filename)) {
1167
                unlink($source_filename);
1168
            }
1169
            return false;
1170
        }
1171
    }
1172
 
1173
    /**
1174
     * @param string $source_filename
1175
     * @param string $target_filename
1176
     * @return boolean
1177
     */
1178
    public function uploadImageWithOutChangeSize($source_filename, $target_filename)
1179
    {
1180
        try {
1181
            $data = file_get_contents($source_filename);
1182
            $img = imagecreatefromstring($data);
1183
 
1184
            if ($img) {
1185
                list($source_width, $source_height) = getimagesize($source_filename);
1186
 
1187
                $new_image = $this->_createImageCanvas($source_width, $source_height);
1188
                if ($new_image === false) {
1189
                    imagedestroy($img);
1190
                    unlink($source_filename);
1191
                    return false;
1192
                }
1193
 
1194
                imageCopyResampled($new_image, $img, 0, 0, 0, 0, $source_width, $source_height, $source_width, $source_height);
1195
                imagedestroy($img);
1196
 
1197
                return $this->_savePngAndCleanup($new_image, $target_filename, $source_filename);
1198
            }
1199
 
1200
            unlink($source_filename);
1201
            return false;
1202
        } catch (\Throwable $e) {
1203
            error_log($e->getTraceAsString());
1204
            if (isset($img) && ($img instanceof \GdImage || is_resource($img))) {
1205
                imagedestroy($img);
1206
            }
1207
            if (isset($new_image) && ($new_image instanceof \GdImage || is_resource($new_image))) {
1208
                imagedestroy($new_image);
1209
            }
1210
            if (isset($source_filename) && file_exists($source_filename)) {
1211
                unlink($source_filename);
1212
            }
1213
            return false;
1214
        }
1215
    }
1216
 
1217
    /**
1218
     * @param string $source_filename
1219
     * @param string $target_filename
1220
     * @return boolean
1221
     */
1222
    public static function extractPosterFromVideo($source_filename, $target_filename)
1223
    {
1224
        try {
1225
            $cmd = "/usr/bin/ffprobe -v error -of flat=s=_ -select_streams v:0 -show_entries stream=height,width,duration  $source_filename";
1226
            $response = trim(shell_exec($cmd));
1227
 
1228
            $source_duration = 0;
1229
            $lines = explode("\n", $response);
1230
            foreach ($lines as $line) {
1231
                $line = trim(strtolower($line));
1232
                if (strpos($line, 'duration') !== false) {
1233
                    $values = explode('=', $line);
1234
                    $source_duration = intval(str_replace($values[1], '#', ''), 10);
1235
                }
1236
            }
1237
 
1238
            if ($source_duration == 0) {
1239
                $second_extract = '00:00:02';
1240
            } else {
1241
                if ($source_duration > 10) {
1242
                    $second_extract = '00:00:10';
1243
                } else {
1244
                    $second_extract = '00:00:02';
1245
                }
1246
            }
1247
 
1248
            $cmd = "/usr/bin/ffmpeg -y -i $source_filename  -pix_fmt yuvj422p -an -ss $second_extract -f mjpeg -t 1 -r 1 -y  $target_filename";
1249
            exec($cmd);
1250
 
1251
            return true;
1252
        } catch (\Throwable $e) {
1253
            error_log($e->getTraceAsString());
1254
            return false;
1255
        }
1256
    }
1257
 
1258
    /**
1259
     * @param string $str
1260
     * @return string
1261
     */
1262
    public function normalizeStringFilename($str = '')
1263
    {
1264
        $basename = substr($str, 0, strrpos($str, '.'));
1265
        $basename = str_replace('.', '-', $basename);
1266
 
1267
        $extension = substr($str, strrpos($str, '.'));
1268
 
1269
        $str = $basename . $extension;
1270
 
1271
        $str = strip_tags($str);
1272
        $str = preg_replace('/[\r\n\t ]+/', ' ', $str);
1273
        $str = preg_replace('/[\#\"\*\/\:\<\>\?\'\|\,]+/', ' ', $str);
1274
        $str = strtolower($str);
1275
        $str = html_entity_decode($str, ENT_QUOTES, "utf-8");
1276
        $str = htmlentities($str, ENT_QUOTES, "utf-8");
1277
        $str = preg_replace("/(&)([a-z])([a-z]+;)/i", '$2', $str);
1278
        $str = str_replace(' ', '-', $str);
1279
        $str = rawurlencode($str);
1280
        $str = str_replace('%', '-', $str);
1281
        $str = str_replace([
1282
            '-----',
1283
            '----',
1284
            '---',
1285
            '--'
1286
        ], '-', $str);
1287
        return trim(strtolower($str));
1288
    }
1289
}