-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
7461 lines (6601 loc) · 206 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<title>MagicObject User Manual</title>
<link rel="stylesheet" type="text/css" href="css/css.css" />
<script type="text/javascript" src="js/js.js"></script>
</head>
<body onload="doStuff()">
<div class="articles">
<article class="article" id="part1">
<h1>MagicObject Installation</h1>
<p>To install <strong>MagicObbject</strong></p>
<pre><code>composer require planetbiru/magic-object</code></pre>
<p>or if composer is not installed</p>
<pre><code>php composer.phar require planetbiru/magic-object</code></pre>
<p>To remove <strong>MagicObbject</strong></p>
<pre><code>composer remove planetbiru/magic-object</code></pre>
<p>or if composer is not installed</p>
<pre><code>php composer.phar remove planetbiru/magic-object</code></pre>
<p>To install composer on your PC or download latest composer.phar, click <a href="https://getcomposer.org/download/">https://getcomposer.org/download/</a> </p>
</article>
<article class="article" id="part2">
<h1>MagicObject Implementation</h1>
</article>
<article class="article" id="part3">
<h2>Simple Object</h2>
<h3>Set and Get Properties Value</h3>
<pre><code class="language-php"><?php
use MagicObject\MagicObject;
require_once __DIR__ . "/vendor/autoload.php";
$someObject = new MagicObject();
$someObject->setId(1);
$someObject->setRealName("Someone");
$someObject->setPhone("+62811111111");
$someObject->setEmail("someone@domain.tld");
echo "ID : " . $someObject->getId() . "\r\n";
echo "Real Name : " . $someObject->getRealName() . "\r\n";
echo "Phone : " . $someObject->getPhone() . "\r\n";
echo "Email : " . $someObject->getEmail() . "\r\n";
// get JSON string of the object
echo $someObject;
// or you can debug with error_log
error_log($someObject);</code></pre>
<h3>Unset Properties</h3>
<pre><code class="language-php"><?php
use MagicObject\MagicObject;
require_once __DIR__ . "/vendor/autoload.php";
$someObject = new MagicObject();
$someObject->setId(1);
$someObject->setRealName("Someone");
$someObject->setPhone("+62811111111");
$someObject->setEmail("someone@domain.tld");
echo "ID : " . $someObject->getId() . "\r\n";
echo "Real Name : " . $someObject->getRealName() . "\r\n";
echo "Phone : " . $someObject->getPhone() . "\r\n";
echo "Email : " . $someObject->getEmail() . "\r\n";
$someObject->unsetPhone();
echo "Phone : " . $someObject->getPhone() . "\r\n";
// get JSON string of the object
echo $someObject;
// or you can debug with
error_log($someObject);</code></pre>
<h3>Check if Properties has Value</h3>
<pre><code class="language-php"><?php
use MagicObject\MagicObject;
require_once __DIR__ . "/vendor/autoload.php";
$someObject = new MagicObject();
$someObject->setId(1);
$someObject->setRealName("Someone");
$someObject->setPhone("+62811111111");
$someObject->setEmail("someone@domain.tld");
echo "ID : " . $someObject->getId() . "\r\n";
echo "Real Name : " . $someObject->getRealName() . "\r\n";
echo "Phone : " . $someObject->getPhone() . "\r\n";
echo "Email : " . $someObject->getEmail() . "\r\n";
$someObject->unsetPhone();
if($someObject->hasValuePhone())
{
echo "Phone : " . $someObject->getPhone() . "\r\n";
}
else
{
echo "Phone value is not set\r\n";
}
// another way
if($someObject->issetPhone())
{
echo "Phone : " . $someObject->getPhone() . "\r\n";
}
else
{
echo "Phone value is not set\r\n";
}
// get JSON string of the object
echo $someObject;
// or you can debug with
error_log($someObject);</code></pre>
</article>
<article class="article" id="part4">
<h2>Extend Magic Object</h2>
<p>User can extend <code>MagicObject</code> to many classes.</p>
<pre><code class="language-php"><?php
use MagicObject\MagicObject;
/**
* Example to extends MagicObject
*
* @JSON(property-naming-strategy=SNAKE_CASE, prettify=true)
*/
class MyObject extends MagicObject
{
}</code></pre>
<h3>Class Parameters</h3>
<p><strong>@JSON</strong></p>
<p><code>@JSON</code> is parameter to inform how the object will be serialized.</p>
<p>Attributes:</p>
<ol>
<li><code>property-naming-strategy</code></li>
</ol>
<p>Allowed value:</p>
<ul>
<li><code>SNAKE_CASE</code> all properties will be snake case when <code>__toString()</code> method called.</li>
<li><code>CAMEL_CASE</code> all properties will be camel case when <code>__toString()</code> method called.</li>
<li><code>UPPER_CAMEL_CASE</code> all properties will be camel case with capitalize first character when <code>__toString()</code> method called.</li>
</ul>
<p>Default value: <code>CAMEL_CASE</code></p>
<ol>
<li><code>prettify</code></li>
</ol>
<p>Allowed value:</p>
<ul>
<li><code>true</code> JSON string will be prettified</li>
<li><code>false</code> JSON string will not be prettified</li>
</ul>
<p>Default value: <code>false</code></p>
<p><strong>@Yaml</strong></p>
<p><code>@Yaml</code> is parameter to inform how the object will be serialized.</p>
<p>Attributes:</p>
<ol>
<li><code>property-naming-strategy</code></li>
</ol>
<p>Allowed value:</p>
<ul>
<li><code>SNAKE_CASE</code> all properties will be snake case when <code>dumpYaml()</code> method called.</li>
<li><code>CAMEL_CASE</code> all properties will be camel case when <code>dumpYaml()</code> method called.</li>
<li><code>UPPER_CAMEL_CASE</code> all properties will be camel case with capitalize first character when <code>__toString()</code> method called.</li>
</ul>
<p>Default value: <code>CAMEL_CASE</code></p>
</article>
<article class="article" id="part5">
<h2>Multilevel Object</h2>
<pre><code class="language-php"><?php
use MagicObject\MagicObject;
require_once __DIR__ . "/vendor/autoload.php";
$car = new MagicObject();
$tire = new MagicObject();
$body = new MagicObject();
$tire->setDiameter(12);
$tire->setPressure(60);
$body->setLength(320);
$body->setWidth(160);
$body->Height(140);
$body->setColor("red");
$car->setTire($tire);
$car->setBody($body);
echo $car;
/*
{"tire":{"diameter":12,"pressure":60},"body":{"length":320,"width":160,"height":140,"color":"red"}}
*/
// to get color
echo $car->getBody()->getColor();
</code></pre>
<h3>Parse Yaml</h3>
<pre><code class="language-php">
$song = new MagicObject();
$song->loadYamlString(
"
songId: 1234567890
title: Lagu 0001
duration: 320
album:
albumId: 234567
name: Album 0001
genre:
genreId: 123456
name: Pop
vocalist:
vovalistId: 5678
name: Budi
agency:
agencyId: 1234
name: Agency 0001
company:
companyId: 5678
name: Company 1
pic:
- name: Kamshory
gender: M
- name: Mas Roy
gender: M
timeCreate: 2024-03-03 12:12:12
timeEdit: 2024-03-03 13:13:13
",
false, true, true
);
// to get company name
echo $song->getVocalist()->getAgency()->getCompany()->getName();
echo "\r\n";
// add company properties
$song->getVocalist()->getAgency()->getCompany()->setCompanyAddress("Jalan Jendral Sudirman Nomor 1");
// get agency
echo $song->getVocalist()->getAgency();
echo "\r\n";
// please note that $song->getVocalist()->getAgency()->getCompany()->getPic() is an array, not a MagicObject
// to get pic
foreach($song->getVocalist()->getAgency()->getCompany()->getPic() as $pic)
{
echo $pic;
echo "\r\n----\r\n";
}</code></pre>
</article>
<article class="article" id="part6">
<h2>Object from Yaml</h2>
<h3>From Yaml String</h3>
<pre><code class="language-php"><?php
use MagicObject\MagicObject;
require_once __DIR__ . "/vendor/autoload.php";
$car = new MagicObject();
// load yaml string
// will not replace value with environment variable
// load as object instead of associated array
$car->loadYamlString("
tire:
diameter: 12
pressure: 60
body:
length: 320
width: 160
height: 140
color: red
", false, true, true);
echo $car;
/*
{"tire":{"diameter":12,"pressure":60},"body":{"length":320,"width":160,"height":140,"color":"red"}}
*/
// to get color
echo $car->getBody()->getColor();
</code></pre>
<h3>From Yaml File</h3>
<pre><code class="language-yaml"># config.yml
tire:
diameter: 12
pressure: 60
body:
length: 320
width: 160
height: 140
color: red
</code></pre>
<pre><code class="language-php"><?php
use MagicObject\MagicObject;
require_once __DIR__ . "/vendor/autoload.php";
$car = new MagicObject();
// load file config.yml
// will not replace value with environment variable
// load as object instead of associated array
$car->loadYamlFile("config.yml", false, true, true);
echo $car;
/*
{"tire":{"diameter":12,"pressure":60},"body":{"length":320,"width":160,"height":140,"color":"red"}}
*/
// to get color
echo $car->getBody()->getColor();
</code></pre>
</article>
<article class="article" id="part7">
<h2>Object from INI</h2>
<p>INI not support multilevel object. If multilevel object needed, use Yaml instead.</p>
<h3>From INI String</h3>
<pre><code class="language-php"><?php
use MagicObject\MagicObject;
require_once __DIR__ . "/vendor/autoload.php";
$cfg = new MagicObject();
$cfg->loadIniString("
app_name = MusicProductionManager
base_song_path = /var/www/songs
", false);
</code></pre>
<h3>From INI File</h3>
<p>Load config from <code>config.ini</code> file</p>
<pre><code class="language-ini">app_name = MusicProductionManager
base_song_path = /var/www/songs</code></pre>
<pre><code class="language-php"><?php
use MagicObject\MagicObject;
require_once __DIR__ . "/vendor/autoload.php";
$cfg = new MagicObject();
$cfg->loadIniFile(__DIR__ . "/config.ini", false);
</code></pre>
</article>
<article class="article" id="part8">
<h2>Environment Variable</h2>
<p>Many application use environment variable to store the config. We can replace the config template with the environment variable. We must set the environment variable to the server before run the application.</p>
<pre><code class="language-yaml"># config.yml
tire:
diameter: ${TIRE_DIAMETER}
pressure: ${TIRE_PRESSURE}
body:
length: ${BODY_LENGTH}
width: ${BODY_WIDTH}
height: ${BODY_HEIGHT}
color: ${BODY_COLOR}
</code></pre>
<p>Before execute this script, user must set environment variable for <code>TIRE_DIAMETER</code>, <code>TIRE_PRESSURE</code>, <code>BODY_LENGTH</code>, <code>BODY_WIDTH</code>, <code>BODY_HEIGHT</code>, and <code>BODY_COLOR</code> depend on the operating system used.</p>
<pre><code class="language-php"><?php
use MagicObject\MagicObject;
require_once __DIR__ . "/vendor/autoload.php";
$car = new MagicObject();
// load file config.yml
// will replace value with environment variable
// load as object instead of associated array
$car->loadYamlFile("config.yml", true, true);
echo $car;
// to get color
echo $car->getBody()->getColor();
</code></pre>
<h3>Create Yaml File</h3>
<pre><code class="language-yaml">result_per_page: 20
song_base_url: ${SONG_BASE_URL}
song_base_path: ${SONG_BASE_PATH}
song_draft_base_url: ${SONG_DRAFT_BASE_URL}
song_draft_base_path: ${SONG_DRAFT_BASE_PATH}
proxy_provider: cloudflare
app_name: Music Production Manager
user_image:
width: 512
height: 512
album_image:
width: 512
height: 512
song_image:
width: 512
height: 512
database:
time_zone_system: Asia/Jakarta
default_charset: utf8
driver: ${APP_DATABASE_TYPE}
host: ${APP_DATABASE_SERVER}
port: ${APP_DATABASE_PORT}
username: ${APP_DATABASE_USER}
password: ${APP_DATABASE_PASSWORD}
database_name: ${APP_DATABASE_NAME}
database_schema: public
time_zone: ${APP_DATABASE_TIME_ZONE}
salt: ${APP_DATABASE_SALT}
session:
name: MUSICPRODUCTIONMANAGER
max_life_time: 86400
vocal_guide_instrument: piano</code></pre>
<h3>Create Environment Variable</h3>
<p>On Windows, users can directly create environment variables either via the graphical user interface (GUI) or the <code>setx</code> command line. PHP can immediately read environment variables after Windows is restarted.</p>
<p>On Linux, users must create a configuration on the Apache server by creating a file with the .conf extension in the <code>/etc/httpd/conf.d</code> folder then restart Apache web server.</p>
<p><strong>Windows</strong></p>
<p>Setup environtment variable on Windows using command lines.</p>
<pre><code class="language-bash">SETX SONG_BASE_URL "https://domain.tld/path"
SETX APP_DATABASE_TYPE "mariadb"
SETX APP_DATABASE_SERVER "localhost"
SETX APP_DATABASE_PORT "3306"
SETX APP_DATABASE_USER "user"
SETX APP_DATABASE_PASSWORD "pass"
SETX APP_DATABASE_NAME "music"
SETX APP_DATABASE_TIME_ZONE "Asia/Jakarta"
SETX APP_DATABASE_SALT "GaramDapur"</code></pre>
<p><strong>Linux</strong></p>
<p>Setup environtment variable on Linux using command lines create new file configuration used by Apache web server and consumed by PHP.</p>
<pre><code class="language-bash">echo -e '' > /etc/httpd/conf.d/mpm.conf
echo -e 'SetEnv SONG_BASE_URL "https://domain.tld/path"' >> /etc/httpd/conf.d/mpm.conf
echo -e 'SetEnv APP_DATABASE_TYPE "mariadb"' >> /etc/httpd/conf.d/mpm.conf
echo -e 'SetEnv APP_DATABASE_SERVER "localhost"' >> /etc/httpd/conf.d/mpm.conf
echo -e 'SetEnv APP_DATABASE_PORT "3306"' >> /etc/httpd/conf.d/mpm.conf
echo -e 'SetEnv APP_DATABASE_USER "user"' >> /etc/httpd/conf.d/mpm.conf
echo -e 'SetEnv APP_DATABASE_PASSWORD "pass"' >> /etc/httpd/conf.d/mpm.conf
echo -e 'SetEnv APP_DATABASE_NAME "music"' >> /etc/httpd/conf.d/mpm.conf
echo -e 'SetEnv APP_DATABASE_TIME_ZONE "Asia/Jakarta"' >> /etc/httpd/conf.d/mpm.conf
echo -e 'SetEnv APP_DATABASE_SALT "GaramDapur"' >> /etc/httpd/conf.d/mpm.conf
service httpd restart</code></pre>
</article>
<article class="article" id="part9">
<h2>Secret Object</h2>
<h3>Definition</h3>
<p>Secret Objects are very important in applications that use very sensitive and secret configurations. This configuration must be encrypted so that it cannot be seen either when someone tries to open the configuration file, environment variables, or even when the developer accidentally debugs an object related to the database so that the properties of the database object are exposed including the host name, database name, username and even password.</p>
<pre><code class="language-php"><?php
namespace MagicObject\Database;
use MagicObject\SecretObject;
class PicoDatabaseCredentials extends SecretObject
{
/**
* Database driver
*
* @var string
*/
protected $driver = 'mysql';
/**
* Database server host
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $host = 'localhost';
/**
* Database server port
* @var integer
*/
protected $port = 3306;
/**
* Database username
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $username = "";
/**
* Database user password
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $password = "";
/**
* Database name
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $databaseName = "";
/**
* Database schema
*
* @EncryptIn
* @DecryptOut
* @var string
*/
protected $databseSchema = "public";
/**
* Application time zone
*
* @var string
*/
protected $timeZone = "Asia/Jakarta";
}</code></pre>
<h3>Class Parameters</h3>
<p><strong>@JSON</strong></p>
<p><code>@JSON</code> is parameter to inform how the object will be serialized.</p>
<p>Attributes:
<code>property-naming-strategy</code></p>
<p>Allowed value:</p>
<ul>
<li><code>SNAKE_CASE</code> all properties will be snake case when <code>__toString()</code> method called.</li>
<li><code>CAMEL_CASE</code> all properties will be camel case when <code>__toString()</code> method called.</li>
<li><code>UPPER_CAMEL_CASE</code> all properties will be camel case with capitalize first character when <code>__toString()</code> method called.</li>
</ul>
<ol start="2">
<li><code>prettify</code></li>
</ol>
<p>Allowed value:</p>
<ul>
<li><code>true</code> JSON string will be prettified</li>
<li><code>false</code> JSON string will not be prettified</li>
</ul>
<p>Default value: <code>false</code></p>
<p><strong>@Yaml</strong></p>
<p><code>@JSON</code> is parameter to inform how the object will be serialized.</p>
<p>Attributes:
<code>property-naming-strategy</code></p>
<p>Allowed value:</p>
<ul>
<li><code>SNAKE_CASE</code> all properties will be snake case when <code>dumpYaml()</code> method called.</li>
<li><code>CAMEL_CASE</code> all properties will be camel case when <code>dumpYaml()</code> method called.</li>
<li><code>UPPER_CAMEL_CASE</code> all properties will be camel case with capitalize first character when <code>__toString()</code> method called.</li>
</ul>
<h3>Property Parameters</h3>
<ol>
<li><code>@EncryptIn</code> annotation will encrypt the value before it is assigned to the associated property with the <code>set</code> method. </li>
<li><code>@DecryptIn</code> annotation will decrypt the value before it is assigned to the associated property with the <code>set</code> method. </li>
<li><code>@EncryptOut</code> annotation will encrypt the property when application call <code>get</code> method. </li>
<li><code>@DecryptOut</code> annotation will decrypt the property when application call <code>get</code> method. </li>
</ol>
<h3>Create Secure Config</h3>
<p>When creating a secure application configuration, users can simply use the <code>@EncryptOut</code> annotation. MagicObject will load the configuration as entered but will encrypt it when dumped to a file. For configurations that will not be encrypted, do not use <code>@EncryptIn</code>, <code>@DecryptIn</code>, <code>@EncryptOut</code>, or <code>@DecryptOut</code>. </p>
<pre><code class="language-php"><?php
namespace MagicObject\Database;
use MagicObject\SecretObject;
class SecretGenerator extends SecretObject
{
/**
* Database driver
*
* @var string
*/
protected $driver;
/**
* Database server host
*
* @EncryptOut
* @var string
*/
protected $host;
/**
* Database server port
* @var integer
*/
protected $port;
/**
* Database username
*
* @EncryptOut
* @var string
*/
protected $username;
/**
* Database user password
*
* @EncryptOut
* @var string
*/
protected $password;
/**
* Database name
*
* @EncryptOut
* @var string
*/
protected $databaseName;
/**
* Database schema
*
* @EncryptOut
* @var string
*/
protected $databseSchema;
/**
* Application time zone
*
* @var string
*/
protected $timeZone;
}</code></pre>
<pre><code class="language-php">
$yaml = "
time_zone_system: Asia/Jakarta
default_charset: utf8
driver: mysql
host: localhost
port: 3306
username: root
password: password
database_name: music
database_schema: public
time_zone: Asia/Jakarta
salt: GaramDapur
";
$config = new MagicObject();
$config->loadYamlString($yaml);
$generator = new SecretGenerator($config);
echo $generator; // will print JSON
$secretYaml = $generator->dumpYaml(null, 4, 0); // will print secret yaml
file_put_content("secret.yaml", $secretYaml); // will dump to file secret.yaml</code></pre>
<p>Do not use standard encryption keys when creating or using SecretObjects. Always use your own key. The encryption key must be generated using a callback function. Do not enter it as an object property or constant.</p>
<pre><code class="language-php">
$yaml = "
time_zone_system: Asia/Jakarta
default_charset: utf8
driver: mysql
host: localhost
port: 3306
username: root
password: password
database_name: music
database_schema: public
time_zone: Asia/Jakarta
salt: GaramDapur
";
$config = new MagicObject();
$config->loadYamlString($yaml, false, true, true);
$generator = new SecretGenerator($config, function(){
// define your own key here
return "6619f3e7a1a9f0e75838d41ff368f72868e656b251e67e8358bef8483ab0d51c";
});
echo $generator; // will print JSON
$secretYaml = $generator->dumpYaml(null, 4, 0); // will print secret yaml
file_put_content("secret.yaml", $secretYaml); // will dump to file secret.yaml</code></pre>
<p>or you can also call another function. </p>
<pre><code class="language-php">
function getSecure()
{
return "6619f3e7a1a9f0e75838d41ff368f72868e656b251e67e8358bef8483ab0d51c";
}
$yaml = "
time_zone_system: Asia/Jakarta
default_charset: utf8
driver: mysql
host: localhost
port: 3306
username: root
password: password
database_name: music
database_schema: public
time_zone: Asia/Jakarta
salt: GaramDapur
";
$config = new MagicObject();
$config->loadYamlString($yaml, false, true, true);
$generator = new SecretGenerator($config, function(){
// define your own key here
return getSecure();
});
echo $generator; // will print JSON
$secretYaml = $generator->dumpYaml(null, 4, 0); // will print secret yaml
file_put_content("secret.yaml", $secretYaml); // will dump to file secret.yaml</code></pre>
<h3>Implement Secure Config</h3>
<p>Application configuration is usually written in a file or environment variable after being encrypted. This configuration cannot be read by anyone without decrypting it first. MagicObject will retrieve the encrypted value. If a user accidentally dumps an object using <code>var_dump</code> or <code>print_r</code>, then PHP will only display the encrypted value. When PHP makes a connection to the database using a credential, MagicObject will decrypt it but the value will not be stored in the object's properties.</p>
<p>Thus, to create an application configuration, it is enough to use the <code>@DecryptOut</code> annotation. Thus, MagicObject will only decrypt the configuration when it is ready to be used.</p>
<p><strong>Example 1</strong></p>
<pre><code class="language-php"><?php
namespace MagicObject\Database;
use MagicObject\SecretObject;
class PicoDatabaseCredentials extends SecretObject
{
/**
* Database driver
*
* @var string
*/
protected $driver = 'mysql';
/**
* Database server host
*
* @DecryptOut
* @var string
*/
protected $host = 'localhost';
/**
* Database server port
* @var integer
*/
protected $port = 3306;
/**
* Database username
*
* @DecryptOut
* @var string
*/
protected $username = "";
/**
* Database user password
*
* @DecryptOut
* @var string
*/
protected $password = "";
/**
* Database name
*
* @DecryptOut
* @var string
*/
protected $databaseName = "";
/**
* Database schema
*
* @DecryptOut
* @var string
*/
protected $databseSchema = "public";
/**
* Application time zone
*
* @var string
*/
protected $timeZone = "Asia/Jakarta";
}</code></pre>
<h3>Multilevel Object Secure</h3>
<p>MagicObject also support Multilevel Yaml.</p>
<p><strong>Example</strong></p>
<pre><code class="language-php"><?php
use MagicObject\SecretObject;
require_once dirname(__DIR__) . "/vendor/autoload.php";
/**
* @JSON(property-naming-strategy=SNAKE_CASE)
*/
class ConfigSecret1 extends SecretObject
{
/**
* Database
*
* @EncryptOut
* @var mixed
*/
protected $database;
/**
* Session
*
* @EncryptOut
* @var mixed
*/
protected $session;
}
/**
* @JSON(property-naming-strategy=SNAKE_CASE)
*/
class ConfigSecret2 extends SecretObject
{
/**
* Database
*
* @DecryptOut
* @var mixed
*/
protected $database;
/**
* Session
*
* @DecryptOut
* @var mixed
*/
protected $session;
}
$secret1 = new ConfigSecret1(null, function(){
return bin2hex("This is your secure key for Scrt");
});
$yaml1 = "
result_per_page: 20
song_base_url: http//domain.tld/songs
song_base_path: /var/www/songs
song_draft_base_url: http//domain.tld/songs-draft
song_draft_base_path: /var/www/songs-draft
proxy_provider: cloudflare
app_name: Music Production Manager
user_image:
width: 512
height: 512
album_image:
width: 512
height: 512
song_image:
width: 512
height: 512
database:
time_zone_system: Asia/Jakarta
default_charset: utf8
driver: mysql
host: localhost
port: 3306
username: user
password: pass
database_name: music
database_schema: public
time_zone: Asia/Jakarta
salt: Asia/Jakarta
session:
name: MUSICPRODUCTIONMANAGER
max_life_time: 86400
save_handler: files
save_path: /tmp/session
vocal_guide_instrument: piano
";
$secret1->loadYamlString(
$yaml1,
false, true, true
);
echo $secret1->dumpYaml(null, 4);
$yaml2 = "
database:
time_zone_system: UEMGavyLkN7rFAmoXBsdDKwuGC+zFttpPTAaqeMH0XUEZaAMKbyvykNtfqT+F8FcAbQCUHV66qjYfjArzgrHlA==
default_charset: dawURojqYqXdvt2YdZ+kWsq47bcA7FKWnEfGHPMxJr3KUyKxAC0VrH8Khfqcm5iIzQFHE/1wQbdgxJNiffkayw==
driver: Ur4FCOYvXGPpoMpHm3fwhdK5D3SaP0+MGe4IuPAvpzhAvRcjOW7EZe5VvDAf+0CLeus9tCqqE1sTXj/dxfmkaA==
host: zyfIMxYi/qQAbazR+nOaLnXFUN9qDYiapxlvocKYxkL8uuN6zRrP3Jsj0mlj6UnLOlvhfDgF3Pq0PrP2ZORGNg==
port: Cs3NTbxIXJ0lf1umQuuDXbPqdBPlVg+jeXi6UqqUUvVUQWgAhbfenfP8g81cONoY2dXof+P1V5Gr/q+iDLNv5Q==
username: 5NImv2VEL1WSbt3cqx7gi/8f158SYtssj74zTN2fRrIDsGOxsnEa8+50H3Y1MCaJV7SnZo851dnjEhm38Tzjsg==
password: /1SK1m9qVjbWnGa6/xai2H82OWvzXeErRvtQ0RYceGsr3fvAfGfqcDSY6pq8KoXg6MmtJS0FLyjXUq8dftM7yQ==
database_name: ZQ0PTeDl7AOtjDBV1PHzlwMbPBVZmhXuO65O1pDYYCGQSMNf3GpQJi9SkvGApQ8kBKUgPAmchCTYb8ChL2szrQ==
database_schema: 628aqGw8hLV8Malwg6jninVfNRgDUgANtXdPfIuz8IohoMQ4NROIuj7Y28/SeLD74NPoiBYFZpaON9+jV8QQug==
time_zone: zfpl4IxctWh2Y/UnrANjuCKlnevw8MmoSboMtzV3oBzKKN0gg+TT/Zz/QieHkVd+pn7OHx2OXvLadkzzWQTMUQ==
salt: hdsBgcmvq/rzfNUuhgQiBop4Hp4wqd5w03Il9zaOshK0LgGtaEK1WTcPx5OdL+9VvhmZvs7g/jDGYjYoHwXjIg==
session:
name: 4u5xAE2K74pUxZyIVsphoYUka3vpnzUx7op6CnELtdWNEepz/jQLaKynItFt5dx6bv7wjBUFL1AaZaA4ypw/CP6xtR5WFQy+RV6V8VqYM8o=
max_life_time: cspEenRp8+kwUY1RNvmEdWcLqmsRZ+UJZVjY4JwRsuIIfkr+J0w1SYCuRMzMtHG4/hZ5tlXhtGdRZyM7quCbZg==
save_handler: LIlCCaWHVqg9R4G6ghxDZnuenMLgSI6HjiW+tVGNDa7UyIA7FkFtOgOJtvT/EolUc+kkJSXiMo+76QhvFFq8Dg==
save_path: CZijzyucTzrj3tZ1M9PbQ6Hky1+4Gz3RnXwZNSe9/SL+9QZdpK4PoW2TSLsuQ+cKBgKgkncd7JXWgA3CFg0f1A==
result_per_page: 20
song_base_url: http//domain.tld/songs
song_base_path: /var/www/songs
song_draft_base_url: http//domain.tld/songs-draft
song_draft_base_path: /var/www/songs-draft
proxy_provider: cloudflare
app_name: 'Music Production Manager'
user_image:
width: 512
height: 512
album_image:
width: 512
height: 512
song_image:
width: 512
height: 512
vocal_guide_instrument: piano
";
$secret2 = new ConfigSecret2(null, function(){
return bin2hex("This is your secure key for Scrt");
});$secret2->loadYamlString($yaml2, false, true, true);
echo $secret2->dumpYaml(null, 4);</code></pre>
<h3>Secure Config from DynamicObject</h3>
<pre><code class="language-php"><?php
use MagicObject\Database\PicoDatabase;
use MagicObject\Database\PicoDatabaseCredentials;
use MusicProductionManager\App\ShutdownManager;
use MusicProductionManager\Config\ConfigApp;
use MagicObject\SecretObject;
namespace MagicObject\Database;
require_once __DIR__ . "/vendor/autoload.php";
/**
* PicoDatabaseCredentials class
* The SecretObject will encrypt all attributes to prevent unauthorized user read the database configuration
*/
class PicoDatabaseCredentials extends SecretObject
{
/**
* Database driver
*
* @var string
*/
protected $driver = 'mysql';
/**
* Database server host
*
* @DecryptOut
* @var string
*/
protected $host = 'localhost';
/**
* Database server port
* @var integer
*/
protected $port = 3306;
/**
* Database username
*
* @DecryptOut
* @var string
*/
protected $username = "";
/**
* Database user password
*
* @DecryptOut
* @var string
*/
protected $password = "";
/**
* Database name
*
* @DecryptOut
* @var string
*/
protected $databaseName = "";
/**
* Database schema
*
* @DecryptOut
* @var string