Proyectos de Subversion LeadersLinked - Backend

Rev

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