Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17260 | | Comparar con el anterior | Ultima modificación | Ver Log |

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