Compare commits
978 Commits
dev
...
shift-9188
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f536ae5a9 | ||
|
|
95395d1da7 | ||
|
|
c6c639afc2 | ||
| 41147b26f2 | |||
|
|
fbf437ac99 | ||
|
|
5faf49688d | ||
|
|
4d7d0ed74d | ||
|
|
979b9f704c | ||
|
|
4124cf39db | ||
|
|
c83e21d588 | ||
|
|
c88630e9af | ||
|
|
40b265e145 | ||
|
|
3ad2b2fb8e | ||
|
|
8b671065e9 | ||
|
|
028e1a191e | ||
|
|
a133f82997 | ||
|
|
c4f3eb9a4e | ||
|
|
8a52c4529f | ||
|
|
d0493f3dd0 | ||
|
|
b845552c37 | ||
| 4e97209494 | |||
| aa0b010bed | |||
| cb79ea64cf | |||
|
|
38409d0d63 | ||
|
|
32dfb4eef3 | ||
|
|
a6de64a089 | ||
|
|
b226814676 | ||
|
|
805de3291b | ||
|
|
b0ab63e30e | ||
|
|
13bfc52b77 | ||
|
|
bbce78cab4 | ||
|
|
1e084d5131 | ||
|
|
af699161da | ||
| 2e1c2cd0b2 | |||
| cd7366b8ff | |||
| ec74f6594c | |||
| 5d2e9affc0 | |||
| 06b7ce4db0 | |||
|
|
4bf695f559 | ||
| 59daa1ff08 | |||
| 04044673e2 | |||
| 3b837dc6b0 | |||
| 2f029e2523 | |||
| 66d477795f | |||
| 78f23db801 | |||
| e62a21c469 | |||
| 9756622148 | |||
| 065cb1b746 | |||
| 54a7ad86dc | |||
| 0e7c86ac2b | |||
| e023964cb2 | |||
| 16f4eb65ef | |||
| 11eb12324e | |||
| 7a0d3fc8a0 | |||
| 3ed2aadc34 | |||
| b2004e3483 | |||
| 55f363a64f | |||
| 7a6ed9f7f4 | |||
| 8fa8c85077 | |||
| 245ffc9d45 | |||
| 6a9a2f0a9e | |||
| 9dbefe5a8a | |||
| cce2a79ee4 | |||
| 8e94ab2d7d | |||
| 6e0337cdeb | |||
| 1c3b8f065e | |||
| c43d5574b4 | |||
| 0e5c654b02 | |||
| 14d6d59581 | |||
| 3796961293 | |||
| c471a97a23 | |||
| fc853bd5f1 | |||
| d0ea0ae4d3 | |||
| 8a6d1281bb | |||
| 8797d51ef4 | |||
| 2c8ac1f155 | |||
| 42706de9df | |||
| 3ce99b8751 | |||
| 44c4f16c5c | |||
| cdccde528e | |||
| f32655c156 | |||
| d7255f004d | |||
| 56a1aaa19c | |||
| 86491bfb2e | |||
| 8dc43ccfce | |||
| 7ff49700fd | |||
| e14c7aafb3 | |||
| 0f727168be | |||
| 0de47b3104 | |||
| 6b3eb97568 | |||
| bd4ba41b0b | |||
| fb2f2d9739 | |||
| a468ae01ff | |||
| 6d534bd1c3 | |||
| 31820317de | |||
| e2efa1f1bd | |||
| 86a0936cd4 | |||
| 645b623a40 | |||
| 96bd56a828 | |||
| 870f1c5194 | |||
| 5d1adf7af8 | |||
| a1170a1347 | |||
| 9b5aab6e6e | |||
| a25776fbbb | |||
| 54b5929aa4 | |||
| 729fc3fd39 | |||
| b4cf05ad44 | |||
| 4da8b32b1a | |||
| c35342df59 | |||
| 7d8d407d07 | |||
| 1ceb109a28 | |||
| a8181ff2b7 | |||
| e42c4c3023 | |||
| ff040eec58 | |||
| 5d663d21b3 | |||
| b73c2d3726 | |||
| ee96acbe4f | |||
| fd22b79d42 | |||
| 99f56b9ef8 | |||
| 9a686c1112 | |||
| ffcf823a7f | |||
| a85c4bf115 | |||
| 29d7167c24 | |||
| 3c6a570394 | |||
| 419fa322a3 | |||
| 71a2e1b6dd | |||
| a352c21198 | |||
| 4accb60a24 | |||
| 88b92a9572 | |||
| e39fa78981 | |||
| 4205113b00 | |||
| ad47efefaf | |||
| 890399dd74 | |||
| 2698ede55e | |||
| c9f0ea2512 | |||
| 228f9c7c6b | |||
| 63e05e924a | |||
| a66cde3934 | |||
| 0b8a2904ec | |||
| 5fffa97ea7 | |||
| 1d14d86d8e | |||
| 217ab89667 | |||
| ac2dd23ad7 | |||
| 7a4f72378d | |||
| 8190839823 | |||
| 4e9d97268f | |||
| 985f7c94da | |||
| 3d28e73369 | |||
| 4076b138b9 | |||
|
|
99d4c709bd | ||
| 93951cfbc8 | |||
| 4ac86c434e | |||
| 171cfa7aab | |||
| a494dbe662 | |||
| c9d02fb11c | |||
| 3ba65385c5 | |||
| 58a2da1996 | |||
| 845d6ba12b | |||
| f0b55b7b2e | |||
| e6dd75c2a8 | |||
| 7b7154085e | |||
| 1c119e80e9 | |||
| 3d86f859c6 | |||
| 0bfad00df7 | |||
| 64efa723b3 | |||
| 0072b28965 | |||
| 21fa5d24af | |||
| 16ec3c515e | |||
| 6868144e25 | |||
| 0e5af96900 | |||
| ded5caf271 | |||
| e8e1e91d1c | |||
| 6a16d545ec | |||
| 976b6fbb78 | |||
| efc5571fb3 | |||
| 6ad4b3a6c4 | |||
| cc0fe080cf | |||
|
|
1de89fba5f | ||
| 0c668c9c62 | |||
| 6a3c3a3566 | |||
| e17f79e0b1 | |||
| f0461fb65f | |||
| aa38927522 | |||
| 881d06deea | |||
| 683869214b | |||
| 3322a6e005 | |||
| 65a48454ba | |||
| 0de8e17593 | |||
| 17beb4152b | |||
| b09097294f | |||
| c6e1b0248d | |||
| 0a956e1fc5 | |||
| e2dee426bf | |||
| 37a738c094 | |||
| bef4c3440b | |||
| b36ad8042f | |||
| 6ec38853ff | |||
| 69144a665f | |||
| 382a1d0ef8 | |||
| 41c751a76d | |||
| 01e46042fb | |||
| f1a28b6efe | |||
| 7fddeeeaae | |||
| bacf35bb4b | |||
| 4a83c7e171 | |||
| 3cbce25394 | |||
|
|
c2b58cc82d | ||
| ac326d2d74 | |||
| 50aaf8b343 | |||
| f667ac6430 | |||
| e2e599ed35 | |||
| 897e422e15 | |||
| 0031be6882 | |||
| 45880ed7a8 | |||
| b7e964174a | |||
| 77b8f60cb1 | |||
| 4cbde00ef3 | |||
| 196472181e | |||
| 2fa7a0c7a5 | |||
| 38e982e70b | |||
| 8a9b57547a | |||
| 6dd3d6255d | |||
| 656de567d7 | |||
| 24e80d7851 | |||
| bc642b48da | |||
| cc07998a8a | |||
| 178309bb1e | |||
| 40c19f47c7 | |||
| 7f82b24b0c | |||
| f31a8da0e1 | |||
| 8ed158ab3c | |||
| 08af379a57 | |||
| 79dbdd3e5a | |||
| f7e8d5bdf7 | |||
| 52eba56e34 | |||
| 4c42276deb | |||
| b4eb772662 | |||
| 825730c3f9 | |||
| b7a2253b01 | |||
| ce1174d41b | |||
| f2da168a03 | |||
| fec4b29261 | |||
| 01b8dadd5f | |||
| 3ee97468f9 | |||
| c6d318bbc3 | |||
| 4ebb07a79a | |||
| 2580d0874f | |||
| 2168e693d8 | |||
| bc2a25346b | |||
| 03e969e08c | |||
| 37e3872782 | |||
| 71442e4160 | |||
| c2b69a769a | |||
| c0bc0e03c0 | |||
| f49bef1112 | |||
| 191b2978ec | |||
| 2771cdd053 | |||
| 2d576645d8 | |||
| be9884b468 | |||
| df280ce7a6 | |||
| c1182b3b90 | |||
| 425a3561ac | |||
| 12be354cc9 | |||
| 7e2917d447 | |||
| cb0de6a2d5 | |||
| 95318d1b36 | |||
| b92456c178 | |||
| bcb25b5d5e | |||
| 6a79204204 | |||
| d3776a8d3f | |||
| 325c6a0448 | |||
| 7c089eed80 | |||
| 8b40a46c1b | |||
| ef4061b96c | |||
|
|
506849b450 | ||
|
|
e73ce8c943 | ||
| bb4543bc65 | |||
|
|
f8d5850a89 | ||
| cce994d35c | |||
| 02cfb53147 | |||
| 0755df03cf | |||
| 51f0ad7497 | |||
| b4a49d20c8 | |||
| 44ccaf10d4 | |||
| 967c14b93a | |||
| 0e42fc657b | |||
| 2d7a91e368 | |||
| 072ab038fe | |||
| 6723c27d5e | |||
| f5fc700886 | |||
| 96a5ba0ceb | |||
| dfe5e72526 | |||
| d1cbebee84 | |||
| eac313feb8 | |||
| a71ed56cf2 | |||
| e3bc72e8d2 | |||
| 0fadfed7f6 | |||
| 031db78590 | |||
| 6fd32fb84b | |||
| 98e6464be6 | |||
| 52ce2afad2 | |||
| 28f89c9469 | |||
| 6f7de8da66 | |||
| 50caf2753d | |||
| 30c0caa04d | |||
| e53d8c14a9 | |||
| eca89358db | |||
| 1e58c71e67 | |||
| 43beaefc07 | |||
| f74faadace | |||
| 77aa622610 | |||
| 95aadd45ee | |||
| 89880016ea | |||
| beb91553ef | |||
| b2037c9575 | |||
| ce4cec5589 | |||
| e8a597ec6b | |||
| a663e2bd56 | |||
| 84bfd3cda2 | |||
| 3dfe96fa89 | |||
| 93cbcef93f | |||
| 5c758536a4 | |||
| bc5a9aa9f1 | |||
| b8ed77f6d5 | |||
| 6c25cd029f | |||
| 68d59eda69 | |||
| 2534d4c159 | |||
| 54252e768c | |||
| 7a2f263061 | |||
| 5ae6e02ce8 | |||
| fb9944ef14 | |||
| 190493179f | |||
| 4b1bc23622 | |||
| 2ad5b04a48 | |||
| afbbbcb4d1 | |||
| 820c3aec9d | |||
| eafbcd8389 | |||
| f0459b3f6e | |||
| ff93265890 | |||
| 320e282dc8 | |||
| d23c911c78 | |||
| 51df812a6c | |||
| a96aba57f7 | |||
| 36c71da4bb | |||
| b53fca9648 | |||
| 9fafb8bd2a | |||
| 41c7ba35a0 | |||
| 4cc5702da7 | |||
| 65f626f15e | |||
| 2abf6f67af | |||
| 72cde997ab | |||
| 8b27cb4690 | |||
| fb0cec0850 | |||
| a9a0bfdad0 | |||
| 00a752173d | |||
| 24caa9a4f4 | |||
| a1075e000a | |||
| c416902280 | |||
| a29d707183 | |||
| 69d08a85ac | |||
| 475ea08517 | |||
| b4c97c20d6 | |||
| f7da2c8185 | |||
| a1a630fc02 | |||
| 22ef117493 | |||
| 04b80d5ff8 | |||
| b764979c3b | |||
| a78c0491ef | |||
| 6082beb964 | |||
| 9d9a5fd9d2 | |||
| 4332f389a1 | |||
| a26b60e726 | |||
| 465d76cd08 | |||
| e0300148cf | |||
| 4442c6c625 | |||
| 289eb86d97 | |||
| 59724777e9 | |||
| 99e0b297b2 | |||
| 7036747042 | |||
| b9cd3e3f9f | |||
| c8e90b6887 | |||
| 56973b62f6 | |||
| 990a13e777 | |||
| cd37623746 | |||
| 193620e4e4 | |||
| 78d85e2440 | |||
| 9fa9689db9 | |||
| 81fc33183c | |||
| 2600011736 | |||
| e5c297eb7c | |||
| a3766aca6c | |||
| 857689dc22 | |||
| 2ed5917e96 | |||
| 84380bf333 | |||
| e7d517f264 | |||
| 40a9cc424e | |||
| b725bc2b5b | |||
| 50306c319e | |||
| c1e86c6897 | |||
| eb02142afc | |||
| 5f0526eef7 | |||
| cbdc55df8f | |||
| e0022b15c5 | |||
| 983edc53d1 | |||
| 2af1dcd24e | |||
| 2814a5f044 | |||
| 802fd87850 | |||
| 50a6a39632 | |||
| 49d0d3b35a | |||
| 8017f017f2 | |||
| 864798be7c | |||
| e20ef40e02 | |||
| 955f9021f7 | |||
| e006090be2 | |||
| 1c6bc56e08 | |||
| aa2da29b4c | |||
| 9e47d28660 | |||
| a5383c87c7 | |||
| 152a637e31 | |||
| 5d947000ca | |||
| bf4f378108 | |||
| 7f03228efa | |||
| 7e6fd1859e | |||
| 979c77c1b9 | |||
| 15c9603902 | |||
| 20dd8bcb3a | |||
| bec4b03a17 | |||
| 2686a162e7 | |||
| 7d9c982cf5 | |||
| d1c09ce74e | |||
| fe5f429039 | |||
| 8937571214 | |||
| f9591951cb | |||
| 956d2a25f2 | |||
| 365bec10a6 | |||
| c69c11b0fe | |||
| 40b8414f8a | |||
| 06cb735b68 | |||
| 985f32e06e | |||
| 36469b20b3 | |||
| 2173e4c6b8 | |||
| d7a35e651e | |||
| 4238c977f3 | |||
| 74e9a3204f | |||
| 7bf94ced84 | |||
| 1b3a40c22a | |||
| 28bef07e37 | |||
| 3b7cb57e7a | |||
| 3f069e6d22 | |||
| 12e7269591 | |||
| 0127cf0a6b | |||
| fd1522a2ca | |||
| c79ec065d3 | |||
| 19fe484049 | |||
| db3d831bc0 | |||
| d27e707044 | |||
| c142440068 | |||
| df6456f5d2 | |||
| 1578a2b8d1 | |||
| 4a43c152d5 | |||
| 0d1ee37272 | |||
| 5c0b97cd1e | |||
| 511e8d6074 | |||
| 826e4a7de2 | |||
| 1ac66b4ece | |||
| 8f58de9f4e | |||
| 611d997df9 | |||
| 3f66e3f1f1 | |||
| 6154fa5dcc | |||
| dd0914cd89 | |||
| b8000d9a64 | |||
| d94bd66c54 | |||
| 2ebd2018db | |||
| 04b41e16e1 | |||
| 7ad73f3c84 | |||
| 359698d54f | |||
| 26ea658f9c | |||
| a13be0530f | |||
| b018b11c57 | |||
| aac023351a | |||
| fd2fbea03f | |||
| f3bbdec77c | |||
| b54ace0272 | |||
| 55bc78d9cb | |||
| fabe027d54 | |||
| b4f4450573 | |||
| 990cc66600 | |||
| aa76147144 | |||
| ee46af08ca | |||
| c33de944ef | |||
| 79e5103b08 | |||
| 4797b213ee | |||
| c232042af5 | |||
| 252448f4a9 | |||
| ec4febe5e9 | |||
| 35ca0d90f7 | |||
| eae3d4689b | |||
| 67c9d4084c | |||
| fb52428219 | |||
| 23e620e168 | |||
| 324054b3db | |||
| d9dcbeef7b | |||
| 9b31d52d7e | |||
| 260d2d28ad | |||
| dd74bdda6a | |||
| 7486390da5 | |||
| eb32c99764 | |||
| 8305f16dae | |||
| a74ace3bbd | |||
| 96c8774e31 | |||
| 45895bddae | |||
| e29e443078 | |||
| f213aeb93a | |||
| b68b0f1583 | |||
| 52fc4c5c34 | |||
| d7f81c2f03 | |||
| 9b6e9aeb1c | |||
| ea3bb13661 | |||
| ec12679426 | |||
| 25a6d60e73 | |||
| 24c3a1ef30 | |||
| fbaef5392f | |||
| f52cd448e2 | |||
| 7f3eada0c1 | |||
| 181cd2fce1 | |||
| a5f600e73b | |||
| c96b3d8349 | |||
| 06c9d48126 | |||
| 0d29fbce45 | |||
| dc7fd81fc5 | |||
| f7503d1f20 | |||
| 56673fceaa | |||
| 3dccc56d16 | |||
| 75566e27fa | |||
| a8627ca89e | |||
| f49b6fef0f | |||
| 8b83b0c212 | |||
| 57292ab8de | |||
| a26b522356 | |||
| e1468e82e4 | |||
| 76102637dd | |||
| a55fac1bfa | |||
| 052a256422 | |||
| 3bfcb0c0d4 | |||
| e18292e352 | |||
| d71a1f5940 | |||
| d63f180030 | |||
| aebdb0b599 | |||
| 3a562005e5 | |||
| 320516fd8d | |||
| 971074777d | |||
| 22c3b5800d | |||
| b0a1197e22 | |||
| 79704e2f2b | |||
| 9f9faf5554 | |||
| a927334e06 | |||
| 74c4c5d2bc | |||
| 2bd8acc00f | |||
| 22ea843f60 | |||
| c7fb636ab5 | |||
| 59761e8eac | |||
| 4f8efa9c90 | |||
| 132ece89c1 | |||
| d0d5cc6841 | |||
| 498fd89239 | |||
| 0f99d3e83b | |||
| d0fa4d649b | |||
| 7d1d74e48a | |||
| 65e81eaae7 | |||
| 85cfdfd24f | |||
| ad5b47f2a5 | |||
| 238189fd9a | |||
| 5691a051a6 | |||
| d11ac9240c | |||
| 7c9f901a7a | |||
| 55fffef5cb | |||
| eb1c475fd9 | |||
| 7cfeea9641 | |||
| 59a7f02893 | |||
| ab4ef89c87 | |||
| 3d8b85dcf4 | |||
| 4b9867bd16 | |||
| b45dd84f0f | |||
| b21468f265 | |||
| 19c5bd5c25 | |||
| 64fd34ff1c | |||
| c0e7adcc42 | |||
| 0f48f21dde | |||
| 5f960cca71 | |||
| 630418cf02 | |||
| dd5ac3a2b4 | |||
| 0385672364 | |||
| c2a0f04cc0 | |||
| 9be9f4329b | |||
| 667972f05e | |||
| 0bbb1f0eba | |||
| a7219861f4 | |||
| 09376e8f98 | |||
| ac45a6b5ef | |||
|
|
ea732301fd | ||
| d21d1b6993 | |||
| 655c003969 | |||
| 23288e15e0 | |||
| cf3c35ffa3 | |||
| 85c37ba748 | |||
| 154dffeee4 | |||
| 2cea90c2c8 | |||
| 44b123307a | |||
| 7ecec70520 | |||
| 7605a826d6 | |||
| 3126991e8f | |||
| 7b58303cde | |||
| 58d302fc38 | |||
| af4b9b95e7 | |||
| dc56edf486 | |||
| 14dd2bb336 | |||
| 7a1499a0b3 | |||
| be8ccbd41a | |||
| 970618f561 | |||
| 8a3d9eec03 | |||
| 46de5cc0c9 | |||
| 0c2ac5d0a5 | |||
| c18b740f46 | |||
| 615abcc8e3 | |||
| 3d13fc6864 | |||
| 8244230268 | |||
| 3f48f11838 | |||
| e16ba2d096 | |||
| 874339348c | |||
| d1833d7b8d | |||
| b658e96425 | |||
| 0ab92d95ea | |||
| a9b480994a | |||
| 3bd5c064c3 | |||
| 6bee6b1ba7 | |||
| e11211fcc7 | |||
| d5a703026a | |||
| 44481fe107 | |||
| 57092e1b26 | |||
| b62a3b9d63 | |||
| 7fd65ede5f | |||
| d3e7938231 | |||
| 2afb59d4a2 | |||
| d54f2159d9 | |||
| e032fb666f | |||
| dca56831af | |||
| 937b70e1fd | |||
| 3b0c7d8388 | |||
| d5093110f7 | |||
| 25ddcef978 | |||
| 5c613df087 | |||
| 0aa44e70de | |||
| 2b69d9985e | |||
| 86f44c8bab | |||
| fa66ee14ee | |||
| 40ed36e0e2 | |||
| b7c8a9ece5 | |||
| 29dfb852c8 | |||
| 9de83c3436 | |||
| 9a0768a2d5 | |||
| 07aa82e7d9 | |||
| bc79951700 | |||
| 155a83cfdd | |||
| 1ffa7cdbcf | |||
| 0f2e1478a8 | |||
| 3efe31c91f | |||
| 22ebefc46a | |||
| 1f8646438d | |||
| 9ac7769dbc | |||
| 5f3cc1a3c6 | |||
| 7b882d6705 | |||
| 33aa01fc78 | |||
| 542bf9c189 | |||
| d39d599570 | |||
| 082e2fe2bd | |||
| 9600d1e7d1 | |||
| 6ab68df4a0 | |||
| 9ae8fa4055 | |||
| 52d40cb8a1 | |||
| 30e51dddd7 | |||
| ffbe78fdc1 | |||
| adae8888f8 | |||
| a3641828fc | |||
| b91d079787 | |||
| 9e2bb48f13 | |||
| 22495294fc | |||
| c83cfff556 | |||
| 35c3108db7 | |||
| 6155a8768f | |||
| 9d16889216 | |||
| 9fd46b9fd9 | |||
| 041dd4b314 | |||
| 13b2c448b5 | |||
| a41e209123 | |||
| c24c1f54be | |||
| 96076af037 | |||
| 1c9bce3b33 | |||
| ef048f77bb | |||
| 361ead1351 | |||
| f031c51ce4 | |||
| ce7ba83fff | |||
| eb692de59a | |||
| fe66dedecf | |||
| 79eea9ea25 | |||
| 83c25f20bb | |||
| 95c5ed6a32 | |||
| 3f0ebca4da | |||
| 03ec852648 | |||
| 6ccc33f762 | |||
| d1bc5c2fe5 | |||
| ee0d5c363c | |||
| 86d5706aac | |||
| 2ca199d98f | |||
| 562d7a603e | |||
| c078189f44 | |||
| d060b1f56c | |||
| d91d51a60a | |||
| 4677400b4f | |||
| f7bac335db | |||
| ad3e6e59f2 | |||
| cc38a45fd0 | |||
| 7320094a87 | |||
| f05691d911 | |||
| 3f9e40dbc8 | |||
| d399427d7f | |||
| bf53784135 | |||
| 988d48996a | |||
| 037873778d | |||
| 835e8be4dc | |||
| e384229d7c | |||
| efcb61781a | |||
| a20551db7a | |||
| 26a3191226 | |||
|
|
f6a390595a | ||
| e0ac364dff | |||
| 1b6c5a57e4 | |||
| 62a3180e53 | |||
| 91a4a2c2a4 | |||
| c40f448aa3 | |||
| 7bc89f4463 | |||
| 30b487e48c | |||
| db7ee08937 | |||
| d810340425 | |||
| 48192240c4 | |||
| 5c6b0085c8 | |||
| c6b088fdf9 | |||
| b1769a3326 | |||
| 61f9efe32d | |||
| f940278f4d | |||
|
|
0665286657 | ||
| eb0064e477 | |||
| 955c06f5aa | |||
| 1ee2a1189d | |||
| c8e49ba49c | |||
| aeb7939c6e | |||
| 910330c7dd | |||
| 1315d6368c | |||
| c9e2350155 | |||
| 35f5b382fa | |||
| a530b98fe2 | |||
| 26b93442b1 | |||
| 1142033d57 | |||
| 808304303a | |||
| 2b298b7c76 | |||
| 3231063bc2 | |||
| 212c5410e1 | |||
| 34a90bf218 | |||
| 56f786cd2b | |||
| cff787f541 | |||
| 55ec88e11f | |||
| b718212702 | |||
| c1dbde2fb7 | |||
| 2eac3f8b0b | |||
| 05cc7767bf | |||
| 7c897a7e12 | |||
| 06655e2378 | |||
| aad927fb96 | |||
| 533fdab150 | |||
| 93eb6da68d | |||
| cc89d45690 | |||
| f993913438 | |||
| 158da60922 | |||
| bb0be1dadf | |||
| c3379f2796 | |||
| 531e1f53fd | |||
| 62f09f738f | |||
| 79d5218b16 | |||
| a068516aa0 | |||
| 9f92bc710c | |||
| 716cc0eb58 | |||
| c3bb2179b1 | |||
| a82e8df06d | |||
| 40dce90aa0 | |||
| 37e59bb8a5 | |||
| 982c124d3b | |||
| 6ebb915c68 | |||
| 4ee8fd2400 | |||
| 19b40d50c1 | |||
| 6b14a4bb24 | |||
| 25790d1c45 | |||
| 4261a35ca7 | |||
| 66e2783d97 | |||
| 55bf78497f | |||
| ac35ce6c47 | |||
| 3b443aafc5 | |||
| a2ab077325 | |||
| 4adb0c953b | |||
| 72a78ba27b | |||
| e4cdd25922 | |||
| 24e2637408 | |||
| b538eb1929 | |||
| 4e40e8b1db | |||
| e6aef415f5 | |||
| 0f8f18943b | |||
| f0accc2e33 | |||
| 14ea157b7a | |||
| 686b0a167d | |||
| bde47e1251 | |||
| 9aaa2e5926 | |||
| ad74d7bdeb | |||
| ab42eeb370 | |||
| a25fc2e7e8 | |||
| ca40db79f7 | |||
| fad2f82b6b | |||
| 1b68387865 | |||
| b2ef15d45c | |||
| 998e69085e | |||
| 77d2786937 | |||
| b7814ac2a4 | |||
| 516410a649 | |||
| 3018048140 | |||
| 8e4044dffb | |||
| 4123845783 | |||
| fd9dcc182f | |||
| a3a6fbcaa4 | |||
| d9e4ccfca8 | |||
| ca288a088a | |||
| a63e73d692 | |||
| 5eda87b1ef | |||
| 2ee1bd3658 | |||
| cd689ea1a8 | |||
| 3ca5db394a | |||
| 5ee6d7ba78 | |||
| d15a44e7d7 | |||
| 4bb557e055 | |||
| f57466b243 | |||
| bdceb6d774 | |||
| fadf6a3d21 | |||
| 3cd03499c9 | |||
| 9406088f1f | |||
| a8f650d530 | |||
| caffbb6c45 | |||
| 4a6a65c5d2 | |||
| b50acd3b74 | |||
| 83f6bce923 | |||
| 7c4f377e2b | |||
| 8851a7149f | |||
| ced5089caf | |||
| ce53c94ea3 | |||
| 5cfcbacb80 | |||
| 5acf5e5297 | |||
| 9337ee292c | |||
| 3de48f002c | |||
| dad736b0d9 | |||
| 7b2e88d089 | |||
| 0db1bd50fd | |||
| 0fcc9b0d7e | |||
| 0349225ede | |||
| 5ee55b298f | |||
| 9877f3883a | |||
| 7cfc24cf39 | |||
| 9025581db8 | |||
| 731e33a986 | |||
| 336e7a7d41 | |||
| 3906ef119b | |||
| 4de4642c23 | |||
| 56eee105d5 | |||
| d7e530b2dd | |||
| 54855e2651 | |||
| 7de134683d | |||
| 1d0030e34f | |||
| ccc30a8b7a | |||
| 9961aba160 | |||
| 5414c0b232 | |||
| 584e146af0 | |||
| e035128c30 | |||
| 03f5c8d90f | |||
| ff75f142b3 | |||
| 094779a4fd | |||
| cfa84faab4 | |||
| c18d7c56c1 | |||
| 127fea1dfd | |||
| ebe1069a7f | |||
| 9f42f92c2e | |||
| c2542472e7 | |||
| 2fe0a8c3f0 | |||
| e97427c7d7 | |||
| d1e586bb39 | |||
| 9e2c7c2565 | |||
| 29deea73d4 | |||
| b1e30a603c | |||
| afc3c94b04 | |||
| ac4d3d8ad0 | |||
| 2b939f417c | |||
| 57236d814d | |||
| 839eaa6db9 | |||
| 78ccd558aa | |||
| a5a9db9669 | |||
| 0b70a52177 | |||
| f9105577ce | |||
| a7e06bef91 | |||
| 0bed3aa096 | |||
| 305c018d39 | |||
| 9404df9a1d | |||
| a2894791e2 | |||
| c671fb9bf6 | |||
| 5f22142a6d | |||
| cc1a389eba | |||
| e6174a4456 | |||
| 145b5559ef | |||
| 09751208a1 | |||
| 22ff92e3a7 | |||
| 8f7f1a35d5 | |||
| 1452d6f340 | |||
| 6080c12a7a | |||
| d16231d338 | |||
| 14e2e2da3e | |||
| 0fba7849b4 | |||
| 6cf5cd1697 | |||
| be39577466 | |||
| fe56acd818 | |||
| e84dfcacd1 | |||
| e639801e37 | |||
| 636ff8c4a9 | |||
| 5da4cca80f | |||
| c5cc91b075 | |||
| e40a2bad16 | |||
| 4367fa4440 | |||
| a2332451b0 | |||
| f6377a6f3a | |||
| ba3846b374 | |||
| 42651ec256 | |||
| d248204bf9 | |||
| 8b84f1c02c | |||
| 3904fe784e | |||
| e5a68fd805 | |||
| 32d39cdc1c | |||
| 9188461b1f | |||
| 44f6ef4a0b | |||
| b0ae6b0a3d | |||
|
|
b6268b3fab | ||
| ba436e5dcc | |||
| dd01a075d8 | |||
| 89ebab5134 | |||
| a135c7e8fd | |||
| 1f8b83fc8c | |||
| c378d683db | |||
| 0d1a0cf130 | |||
| 4ec4b5b10b | |||
| d60383211c | |||
| 900909aa5b | |||
| e33e8ad6f6 | |||
| 7369eb469e | |||
| c3ebec38d7 | |||
| 7f1d0b127d | |||
| cb8dc3cfa5 | |||
| ac68b4856a | |||
| 26348d9e19 | |||
| d8805a791e | |||
| 8fdc7f8600 | |||
| 5681990112 | |||
| ab36df319b | |||
| f48704804a | |||
| 05e9315ef2 | |||
|
|
a885d15341 | ||
| ad69e5c0e2 | |||
| 824ab331b4 | |||
| 1f21e22dce |
23
.env.example
23
.env.example
@@ -38,11 +38,24 @@ MAIL_ENCRYPTION=null
|
|||||||
MAIL_FROM_ADDRESS="hello@example.com"
|
MAIL_FROM_ADDRESS="hello@example.com"
|
||||||
MAIL_FROM_NAME="${APP_NAME}"
|
MAIL_FROM_NAME="${APP_NAME}"
|
||||||
|
|
||||||
AWS_ACCESS_KEY_ID=
|
AWS_PUBLIC_ACCESS_KEY_ID=
|
||||||
AWS_SECRET_ACCESS_KEY=
|
AWS_PUBLIC_SECRET_ACCESS_KEY=
|
||||||
AWS_DEFAULT_REGION=us-east-1
|
AWS_PUBLIC_DEFAULT_REGION="us-west-002"
|
||||||
AWS_BUCKET=
|
AWS_PUBLIC_BUCKET=
|
||||||
AWS_USE_PATH_STYLE_ENDPOINT=false
|
AWS_PUBLIC_USE_PATH_STYLE_ENDPOINT=false
|
||||||
|
AWS_PUBLIC_ENDPOINT=
|
||||||
|
AWS_PUBLIC_URL=
|
||||||
|
|
||||||
|
AWS_PRIVATE_ACCESS_KEY_ID=
|
||||||
|
AWS_PRIVATE_SECRET_ACCESS_KEY=
|
||||||
|
AWS_PRIVATE_DEFAULT_REGION="us-west-002"
|
||||||
|
AWS_PRIVATE_BUCKET=
|
||||||
|
AWS_PRIVATE_USE_PATH_STYLE_ENDPOINT=false
|
||||||
|
AWS_PRIVATE_ENDPOINT=
|
||||||
|
AWS_PRIVATE_URL=
|
||||||
|
|
||||||
|
CLOUDFLARE_ZONE_ID=
|
||||||
|
CLOUDFLARE_API_KEY=
|
||||||
|
|
||||||
PUSHER_APP_ID=
|
PUSHER_APP_ID=
|
||||||
PUSHER_APP_KEY=
|
PUSHER_APP_KEY=
|
||||||
|
|||||||
59
.env.testing
Normal file
59
.env.testing
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
APP_NAME=Laravel
|
||||||
|
APP_ENV=local
|
||||||
|
APP_KEY=base64:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX=
|
||||||
|
APP_DEBUG=true
|
||||||
|
APP_URL=http://127.0.0.1
|
||||||
|
APP_URL_API="${APP_URL}/api/"
|
||||||
|
|
||||||
|
LOG_CHANNEL=stack
|
||||||
|
LOG_LEVEL=debug
|
||||||
|
|
||||||
|
DB_CONNECTION=sqlite
|
||||||
|
DB_DATABASE=:memory:
|
||||||
|
|
||||||
|
BROADCAST_DRIVER=log
|
||||||
|
CACHE_DRIVER=array
|
||||||
|
QUEUE_CONNECTION=sync
|
||||||
|
SESSION_DRIVER=file
|
||||||
|
SESSION_LIFETIME=120
|
||||||
|
|
||||||
|
MEMCACHED_HOST=127.0.0.1
|
||||||
|
|
||||||
|
REDIS_HOST=127.0.0.1
|
||||||
|
REDIS_PASSWORD=null
|
||||||
|
REDIS_PORT=6379
|
||||||
|
|
||||||
|
MAIL_MAILER=log
|
||||||
|
MAIL_HOST=null
|
||||||
|
MAIL_PORT=null
|
||||||
|
MAIL_USERNAME=null
|
||||||
|
MAIL_PASSWORD=null
|
||||||
|
MAIL_ENCRYPTION=null
|
||||||
|
MAIL_FROM_ADDRESS="hello@example.com"
|
||||||
|
MAIL_FROM_NAME="${APP_NAME}"
|
||||||
|
|
||||||
|
AWS_ACCESS_KEY_ID=
|
||||||
|
AWS_SECRET_ACCESS_KEY=
|
||||||
|
AWS_DEFAULT_REGION=us-east-1
|
||||||
|
AWS_BUCKET=
|
||||||
|
AWS_USE_PATH_STYLE_ENDPOINT=false
|
||||||
|
|
||||||
|
PUSHER_APP_ID=
|
||||||
|
PUSHER_APP_KEY=
|
||||||
|
PUSHER_APP_SECRET=
|
||||||
|
PUSHER_HOST=
|
||||||
|
PUSHER_PORT=443
|
||||||
|
PUSHER_SCHEME=https
|
||||||
|
PUSHER_APP_CLUSTER=mt1
|
||||||
|
|
||||||
|
VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
|
||||||
|
VITE_PUSHER_HOST="${PUSHER_HOST}"
|
||||||
|
VITE_PUSHER_PORT="${PUSHER_PORT}"
|
||||||
|
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
|
||||||
|
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
|
||||||
|
|
||||||
|
CONTACT_ADDRESS="hello@stemmechanics.com.au"
|
||||||
|
CONTACT_SUBJECT="Contact from website"
|
||||||
|
|
||||||
|
STORAGE_LOCAL_URL="${APP_URL}/api/media/%ID%/download"
|
||||||
|
STORAGE_PUBLIC_URL="${APP_URL}/uploads/%NAME%"
|
||||||
3
.eslintignore
Normal file
3
.eslintignore
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
.github/
|
||||||
|
.vscode/
|
||||||
|
vendor/
|
||||||
@@ -4,14 +4,15 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
extends: [
|
extends: [
|
||||||
"eslint:recommended",
|
"eslint:recommended",
|
||||||
"plugin:vue/vue3-recommended",
|
"plugin:vue/vue3-strongly-recommended",
|
||||||
"prettier",
|
"prettier",
|
||||||
"plugin:jsdoc/recommended",
|
"plugin:jsdoc/recommended",
|
||||||
"plugin:@typescript-eslint/recommended",
|
"plugin:@typescript-eslint/recommended",
|
||||||
],
|
],
|
||||||
rules: {
|
rules: {
|
||||||
"vue/multi-word-component-names": "off",
|
"vue/multi-word-component-names": "off",
|
||||||
indent: ["error", 4],
|
indent: ["off", 4, { ignoredNodes: ["ConditionalExpression"] }],
|
||||||
|
"@typescript-eslint/no-inferrable-types": "off",
|
||||||
},
|
},
|
||||||
plugins: ["jsdoc", "@typescript-eslint"],
|
plugins: ["jsdoc", "@typescript-eslint"],
|
||||||
parser: "vue-eslint-parser",
|
parser: "vue-eslint-parser",
|
||||||
|
|||||||
15
.github/dependabot.yml
vendored
Normal file
15
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# To get started with Dependabot version updates, you'll need to specify which
|
||||||
|
# package ecosystems to update and where the package manifests are located.
|
||||||
|
# Please see the documentation for all configuration options:
|
||||||
|
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
||||||
|
|
||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "npm"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "daily"
|
||||||
|
- package-ecosystem: "composer"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "daily"
|
||||||
42
.github/workflows/laravel.yml
vendored
Normal file
42
.github/workflows/laravel.yml
vendored
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
name: Laravel
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: ["main"]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
laravel-tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: "8.1"
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- name: Copy .env
|
||||||
|
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
|
||||||
|
- name: Install Dependencies
|
||||||
|
run: composer install -q --no-interaction --no-progress --prefer-dist
|
||||||
|
- name: Generate key
|
||||||
|
run: php artisan key:generate
|
||||||
|
- name: Directory Permissions
|
||||||
|
run: chmod -R 777 storage bootstrap/cache
|
||||||
|
- name: Create Database
|
||||||
|
run: |
|
||||||
|
mkdir -p database
|
||||||
|
touch database/database.sqlite
|
||||||
|
- name: Execute tests (Unit and Feature tests) via PHPUnit
|
||||||
|
env:
|
||||||
|
DB_CONNECTION: sqlite
|
||||||
|
DB_DATABASE: database/database.sqlite
|
||||||
|
run: vendor/bin/phpunit
|
||||||
|
- name: Install Node.js
|
||||||
|
uses: actions/setup-node@v2
|
||||||
|
with:
|
||||||
|
node-version: "16.x"
|
||||||
|
- name: Install dependencies
|
||||||
|
run: npm ci
|
||||||
|
- name: Run Vue tests
|
||||||
|
env:
|
||||||
|
LARAVEL_BYPASS_ENV_CHECK: "1"
|
||||||
|
run: npm run test
|
||||||
7
.gitignore
vendored
7
.gitignore
vendored
@@ -237,4 +237,11 @@ dist/
|
|||||||
### This Project ###
|
### This Project ###
|
||||||
/public/uploads
|
/public/uploads
|
||||||
/public/build
|
/public/build
|
||||||
|
# /public/tinymce
|
||||||
*.key
|
*.key
|
||||||
|
|
||||||
|
### Synk ###
|
||||||
|
.dccache
|
||||||
|
|
||||||
|
### TempCodeRunner ###
|
||||||
|
tempCodeRunnerFile.*
|
||||||
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@@ -3,6 +3,7 @@
|
|||||||
"editor.formatOnSave": true,
|
"editor.formatOnSave": true,
|
||||||
"editor.codeActionsOnSave": {
|
"editor.codeActionsOnSave": {
|
||||||
"source.fixAll.eslint": true
|
"source.fixAll.eslint": true
|
||||||
|
// "source.organizeImports": true // <-- when enabled, breaks tinymce required import order
|
||||||
},
|
},
|
||||||
"editor.defaultFormatter": "esbenp.prettier-vscode",
|
"editor.defaultFormatter": "esbenp.prettier-vscode",
|
||||||
"[vue]": {
|
"[vue]": {
|
||||||
@@ -14,5 +15,6 @@
|
|||||||
"[php]": {
|
"[php]": {
|
||||||
// "editor.defaultFormatter": "bmewburn.vscode-intelephense-client"
|
// "editor.defaultFormatter": "bmewburn.vscode-intelephense-client"
|
||||||
"editor.defaultFormatter": "wongjn.php-sniffer"
|
"editor.defaultFormatter": "wongjn.php-sniffer"
|
||||||
}
|
},
|
||||||
|
"cSpell.words": ["TIMESTAMPDIFF"]
|
||||||
}
|
}
|
||||||
|
|||||||
83
app/Conductors/AnalyticsConductor.php
Normal file
83
app/Conductors/AnalyticsConductor.php
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Conductors;
|
||||||
|
|
||||||
|
use App\Models\Media;
|
||||||
|
use App\Models\User;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\InvalidCastException;
|
||||||
|
use Illuminate\Database\Eloquent\MissingAttributeException;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use LogicException;
|
||||||
|
|
||||||
|
class AnalyticsConductor extends Conductor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The Model Class
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $class = \App\Models\Analytics::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default sorting field
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $sort = 'created_at';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default includes to include in a request.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $includes = ['duration'];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is visible.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow model to be visible.
|
||||||
|
*/
|
||||||
|
public static function viewable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/analytics') === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is creatable.
|
||||||
|
*
|
||||||
|
* @return boolean Allow creating model.
|
||||||
|
*/
|
||||||
|
public static function creatable(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is updatable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow updating model.
|
||||||
|
*/
|
||||||
|
public static function updatable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/analytics') === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is destroyable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow deleting model.
|
||||||
|
*/
|
||||||
|
public static function destroyable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/analytics') === true);
|
||||||
|
}
|
||||||
|
}
|
||||||
151
app/Conductors/ArticleConductor.php
Normal file
151
app/Conductors/ArticleConductor.php
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Conductors;
|
||||||
|
|
||||||
|
use App\Models\Media;
|
||||||
|
use App\Models\User;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\InvalidCastException;
|
||||||
|
use Illuminate\Database\Eloquent\MissingAttributeException;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use LogicException;
|
||||||
|
|
||||||
|
class ArticleConductor extends Conductor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The Model Class
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $class = \App\Models\Article::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default sorting field
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $sort = '-publish_at';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The included fields
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $includes = ['attachments', 'user'];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run a scope query on the collection before anything else.
|
||||||
|
*
|
||||||
|
* @param Builder $builder The builder in use.
|
||||||
|
*/
|
||||||
|
public function scope(Builder $builder): void
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
if ($user === null || $user->hasPermission('admin/articles') === false) {
|
||||||
|
$builder
|
||||||
|
->where('publish_at', '<=', now());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is visible.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow model to be visible.
|
||||||
|
*/
|
||||||
|
public static function viewable(Model $model): bool
|
||||||
|
{
|
||||||
|
if (Carbon::parse($model->publish_at)->isFuture() === true) {
|
||||||
|
$user = auth()->user();
|
||||||
|
if ($user === null || $user->hasPermission('admin/articles') === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is creatable.
|
||||||
|
*
|
||||||
|
* @return boolean Allow creating model.
|
||||||
|
*/
|
||||||
|
public static function creatable(): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/articles') === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is updatable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow updating model.
|
||||||
|
*/
|
||||||
|
public static function updatable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/articles') === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is destroyable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow deleting model.
|
||||||
|
*/
|
||||||
|
public static function destroyable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/articles') === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform the final model data
|
||||||
|
*
|
||||||
|
* @param array $data The model data to transform.
|
||||||
|
* @return array The transformed model.
|
||||||
|
*/
|
||||||
|
public function transformFinal(array $data): array
|
||||||
|
{
|
||||||
|
unset($data['user_id']);
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Include Attachments Field.
|
||||||
|
*
|
||||||
|
* @param Model $model Them model.
|
||||||
|
* @return mixed The model result.
|
||||||
|
*/
|
||||||
|
public function includeAttachments(Model $model)
|
||||||
|
{
|
||||||
|
return $model->attachments()->get()->map(function ($attachment) {
|
||||||
|
return MediaConductor::includeModel(request(), 'attachments', $attachment->media);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Include User Field.
|
||||||
|
*
|
||||||
|
* @param Model $model Them model.
|
||||||
|
* @return mixed The model result.
|
||||||
|
*/
|
||||||
|
public function includeUser(Model $model)
|
||||||
|
{
|
||||||
|
return UserConductor::includeModel(request(), 'user', User::find($model['user_id']));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform the Hero field.
|
||||||
|
*
|
||||||
|
* @param mixed $value The current value.
|
||||||
|
* @return array The new value.
|
||||||
|
*/
|
||||||
|
public function transformHero(mixed $value): array
|
||||||
|
{
|
||||||
|
return MediaConductor::includeModel(request(), 'hero', Media::find($value));
|
||||||
|
}
|
||||||
|
}
|
||||||
979
app/Conductors/Conductor.php
Normal file
979
app/Conductors/Conductor.php
Normal file
@@ -0,0 +1,979 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Conductors;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
class Conductor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The Conductors Model class.
|
||||||
|
*
|
||||||
|
* @var string|null
|
||||||
|
*/
|
||||||
|
protected $class = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default sorting fields of a collection. Can be an array. Supports - and + prefixes.
|
||||||
|
*
|
||||||
|
* @var string|array
|
||||||
|
*/
|
||||||
|
protected $sort = "id";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default collection size limit per request.
|
||||||
|
*
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
protected $limit = 50;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The maximum collection size limit per request.
|
||||||
|
*
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
protected $maxLimit = 100;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default includes to include in a request.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $includes = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default filters to use in a request.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $defaultFilters = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The conductor collection.
|
||||||
|
*
|
||||||
|
* @var Collection
|
||||||
|
*/
|
||||||
|
protected $collection = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The collection filter to apply.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $filterArray = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The conductor query.
|
||||||
|
*
|
||||||
|
* @var Builder
|
||||||
|
*/
|
||||||
|
private $query = null;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split a string on commas, keeping quotes intact.
|
||||||
|
*
|
||||||
|
* @param string $string The string to split.
|
||||||
|
* @return array The split string.
|
||||||
|
*/
|
||||||
|
private function splitString(string $string): array
|
||||||
|
{
|
||||||
|
$parts = [];
|
||||||
|
$start = 0;
|
||||||
|
$len = strlen($string);
|
||||||
|
|
||||||
|
while ($start < $len) {
|
||||||
|
$commaPos = strpos($string, ',', $start);
|
||||||
|
$singlePos = strpos($string, '\'', $start);
|
||||||
|
$doublePos = strpos($string, '"', $start);
|
||||||
|
|
||||||
|
// Find the smallest position that is not false
|
||||||
|
$minPos = false;
|
||||||
|
if ($commaPos !== false) {
|
||||||
|
$minPos = $commaPos;
|
||||||
|
}
|
||||||
|
if ($singlePos !== false && ($minPos === false || $singlePos < $minPos)) {
|
||||||
|
$minPos = $singlePos;
|
||||||
|
}
|
||||||
|
if ($doublePos !== false && ($minPos === false || $doublePos < $minPos)) {
|
||||||
|
$minPos = $doublePos;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($minPos === false) {
|
||||||
|
// No more commas, single quotes, or double quotes found
|
||||||
|
$part = substr($string, $start);
|
||||||
|
$parts[] = trim($part);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
// Add the current part to the parts array
|
||||||
|
$part = substr($string, $start, ($minPos - $start));
|
||||||
|
$parts[] = trim($part);
|
||||||
|
|
||||||
|
// Update the start position to the next character after the comma, single quote, or double quote
|
||||||
|
if ($string[$minPos] === ',') {
|
||||||
|
$start = ($minPos + 1);
|
||||||
|
} else {
|
||||||
|
$quoteChar = $string[$minPos];
|
||||||
|
$endPos = strpos($string, $quoteChar, ($minPos + 1));
|
||||||
|
if ($endPos === false) {
|
||||||
|
$part = substr($string, ($minPos + 1));
|
||||||
|
$parts[] = trim($part);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
$part = substr($string, ($minPos + 1), ($endPos - $minPos - 1));
|
||||||
|
$parts[] = trim($part);
|
||||||
|
$start = ($endPos + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}//end if
|
||||||
|
}//end while
|
||||||
|
|
||||||
|
return array_filter($parts, function ($value) {
|
||||||
|
return $value !== '';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter Collection based on the Request.
|
||||||
|
*
|
||||||
|
* @param Request $request The user request.
|
||||||
|
* @param array|null $limitFields A list of fields to limit the filter request to.
|
||||||
|
*/
|
||||||
|
private function filter(Request $request, array|null $limitFields = null): void
|
||||||
|
{
|
||||||
|
if (is_array($limitFields) === true && count($limitFields) === 0) {
|
||||||
|
$limitFields = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$filterFields = $request->all();
|
||||||
|
if ($limitFields !== null) {
|
||||||
|
$filterFields = array_intersect_key($filterFields, array_flip($limitFields));
|
||||||
|
}
|
||||||
|
$filterFields += $this->defaultFilters;
|
||||||
|
|
||||||
|
foreach ($filterFields as $field => $value) {
|
||||||
|
if (
|
||||||
|
is_array($limitFields) === false ||
|
||||||
|
in_array(strtolower($field), array_map('strtolower', $limitFields)) !== false
|
||||||
|
) {
|
||||||
|
$value = trim($value);
|
||||||
|
$operator = '';
|
||||||
|
$join = 'OR';
|
||||||
|
|
||||||
|
// Check if value has a operator and remove it if it's a number
|
||||||
|
if (preg_match('/^(!?=|[<>]=?|<>|!)([^=!<>].*)*$/', $value, $matches) > 0) {
|
||||||
|
$operator = $matches[1];
|
||||||
|
$value = ($matches[2] ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($operator) {
|
||||||
|
case '=':
|
||||||
|
$operator = '==';
|
||||||
|
break;
|
||||||
|
case '!':
|
||||||
|
$operator = 'NOT LIKE';
|
||||||
|
$value = "%{$value}%";
|
||||||
|
break;
|
||||||
|
case '>':
|
||||||
|
case '<':
|
||||||
|
case '>=':
|
||||||
|
case '<=':
|
||||||
|
case '!=':
|
||||||
|
break;
|
||||||
|
case '<>':
|
||||||
|
$separatorPos = strpos($value, '|');
|
||||||
|
if ($separatorPos === false) {
|
||||||
|
$operator = '!=';
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$operator = 'LIKE';
|
||||||
|
$value = "%{$value}%";
|
||||||
|
break;
|
||||||
|
}//end switch
|
||||||
|
|
||||||
|
$this->appendFilter($field, $operator, $value, $join);
|
||||||
|
}//end if
|
||||||
|
}//end foreach
|
||||||
|
if ($request->has('filter') === true) {
|
||||||
|
$this->appendFilterString($request->input('filter', ''), $limitFields);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->applyFilters();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apple the filter array to the collection.
|
||||||
|
*/
|
||||||
|
final public function applyFilters(): void
|
||||||
|
{
|
||||||
|
$parseFunc = function ($filterArray, $query) use (&$parseFunc) {
|
||||||
|
$item = null;
|
||||||
|
$result = null;
|
||||||
|
$join = 'AND';
|
||||||
|
|
||||||
|
if (gettype($query) === 'array') {
|
||||||
|
$item = $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($filterArray as $condition) {
|
||||||
|
$currentResult = false;
|
||||||
|
|
||||||
|
if (is_array($condition) === true) {
|
||||||
|
if (isset($condition[0]) === true && is_array($condition[0]) === true) {
|
||||||
|
if ($item !== null) {
|
||||||
|
$currentResult = $parseFunc($condition, $item);
|
||||||
|
} else {
|
||||||
|
if ($join === 'OR') {
|
||||||
|
$query->orWhere(function ($subQuery) use ($parseFunc, $condition) {
|
||||||
|
$parseFunc($condition, $subQuery);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
$query->where(function ($subQuery) use ($parseFunc, $condition) {
|
||||||
|
$parseFunc($condition, $subQuery);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
list($field, $operator, $value) = $condition;
|
||||||
|
|
||||||
|
if ($item !== null) {
|
||||||
|
if (array_key_exists($field, $item) === true) {
|
||||||
|
switch ($operator) {
|
||||||
|
case '==':
|
||||||
|
$currentResult = ($item[$field] == $value);
|
||||||
|
break;
|
||||||
|
case 'NOT LIKE':
|
||||||
|
$currentResult = (stripos($item[$field], substr($value, 1, -1)) === false);
|
||||||
|
break;
|
||||||
|
case '>':
|
||||||
|
$currentResult = ($item[$field] > $value);
|
||||||
|
break;
|
||||||
|
case '<':
|
||||||
|
$currentResult = ($item[$field] < $value);
|
||||||
|
break;
|
||||||
|
case '>=':
|
||||||
|
$currentResult = ($item[$field] >= $value);
|
||||||
|
break;
|
||||||
|
case '<=':
|
||||||
|
$currentResult = ($item[$field] <= $value);
|
||||||
|
break;
|
||||||
|
case '!=':
|
||||||
|
$currentResult = ($item[$field] != $value);
|
||||||
|
break;
|
||||||
|
case '<>':
|
||||||
|
$separatorPos = strpos($value, '|');
|
||||||
|
if ($separatorPos !== false) {
|
||||||
|
$fieldInt = intval($item[$field]);
|
||||||
|
$currentResult = (
|
||||||
|
$fieldInt > intVal(
|
||||||
|
substr($value, 0, $separatorPos)
|
||||||
|
) && $fieldInt < intVal(substr($value, ($separatorPos + 1))));
|
||||||
|
} else {
|
||||||
|
$currentResult = ($item[$field] != $value);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'LIKE':
|
||||||
|
$currentResult = (stripos($item[$field], substr($value, 1, -1)) !== false);
|
||||||
|
break;
|
||||||
|
}//end switch
|
||||||
|
}//end if
|
||||||
|
} else {
|
||||||
|
if ($operator === '==') {
|
||||||
|
$operator = '=';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($join === 'OR') {
|
||||||
|
if ($operator === '<>') {
|
||||||
|
$separatorPos = strpos($value, '|');
|
||||||
|
if ($separatorPos !== false) {
|
||||||
|
$query->orWhereBetween(
|
||||||
|
$field,
|
||||||
|
[substr($value, 0, $separatorPos), substr($value, ($separatorPos + 1))]
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$query->orWhere($field, '!=', $value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$query->orWhere($field, $operator, $value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ($operator === '<>') {
|
||||||
|
$separatorPos = strpos($value, '|');
|
||||||
|
if ($separatorPos !== false) {
|
||||||
|
$query->whereBetween(
|
||||||
|
$field,
|
||||||
|
[substr($value, 0, $separatorPos), substr($value, ($separatorPos + 1))]
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$query->where($field, '!=', $value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$query->where($field, $operator, $value);
|
||||||
|
}
|
||||||
|
}//end if
|
||||||
|
}//end if
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
if ($item !== null) {
|
||||||
|
if ($result === null) {
|
||||||
|
$result = $currentResult;
|
||||||
|
} else {
|
||||||
|
if ($join === 'OR') {
|
||||||
|
$result = $result || $currentResult;
|
||||||
|
} else {
|
||||||
|
$result = $result && $currentResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$join = 'OR';
|
||||||
|
} else {
|
||||||
|
$join = $condition;
|
||||||
|
}//end if
|
||||||
|
}//end foreach
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
};
|
||||||
|
|
||||||
|
$filterArray = $this->filterArray;
|
||||||
|
if (count($filterArray) === 0) {
|
||||||
|
$filterArray = $this->defaultFilters;
|
||||||
|
}
|
||||||
|
if (count($filterArray) !== 0) {
|
||||||
|
if ($this->collection !== null) {
|
||||||
|
$this->collection = $this->collection->filter(function ($item) use ($parseFunc) {
|
||||||
|
return $parseFunc($this->filterArray, $item);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
$parseFunc($this->filterArray, $this->query);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the conductor on a Request to generate a collection and total.
|
||||||
|
*
|
||||||
|
* @param Request $request The request data.
|
||||||
|
* @return array The processed and transformed collection | the total rows found.
|
||||||
|
*/
|
||||||
|
final public static function request(Request $request): array
|
||||||
|
{
|
||||||
|
$conductor_class = get_called_class();
|
||||||
|
$conductor = new $conductor_class();
|
||||||
|
|
||||||
|
$total = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$conductor->query = $conductor->class::query();
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
throw new \Exception('Failed to create query builder instance for ' . $conductor->class . '.', 0, $e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter request
|
||||||
|
$limitFields = $conductor->fields(new $conductor->class());
|
||||||
|
if (is_array($limitFields) === false) {
|
||||||
|
$limitFields = [];
|
||||||
|
}
|
||||||
|
$conductor->filter($request, $limitFields);
|
||||||
|
|
||||||
|
// After Scope query
|
||||||
|
$conductor->query->where(function ($query) use ($conductor) {
|
||||||
|
$conductor->scope($query);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Sort request
|
||||||
|
$sort = $request->input('sort', $conductor->sort);
|
||||||
|
if (strlen($sort) === 0) {
|
||||||
|
if (strlen($conductor->sort) > 0) {
|
||||||
|
$conductor->sort($conductor->sort);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$conductor->sort($sort);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get total
|
||||||
|
$total = $conductor->count();
|
||||||
|
|
||||||
|
// Paginate
|
||||||
|
$conductor->paginate($request->input('page', 1), $request->input('limit', -1), $request->input('offset', 0));
|
||||||
|
|
||||||
|
// Filter request
|
||||||
|
$fields = $conductor->fields(new $conductor->class());
|
||||||
|
if (is_array($fields) === false) {
|
||||||
|
$fields = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Limit fields
|
||||||
|
$limitFields = array_map(function ($field) {
|
||||||
|
if (strpos($field, '.') !== false) {
|
||||||
|
return substr($field, 0, strpos($field, '.'));
|
||||||
|
}
|
||||||
|
return $field;
|
||||||
|
}, explode(',', $request->input('fields')));
|
||||||
|
if ($limitFields === null) {
|
||||||
|
$limitFields = $fields;
|
||||||
|
} else {
|
||||||
|
$limitFields = array_intersect($limitFields, $fields);
|
||||||
|
}
|
||||||
|
$conductor->limitFields($limitFields);
|
||||||
|
$conductor->collection = $conductor->query->get();
|
||||||
|
|
||||||
|
// Transform and Includes
|
||||||
|
$includes = $conductor->includes;
|
||||||
|
if (count($limitFields) > 0) {
|
||||||
|
$includes = array_intersect($limitFields, $conductor->includes);
|
||||||
|
}
|
||||||
|
|
||||||
|
$conductor->collection = $conductor->collection->map(
|
||||||
|
function ($model) use ($conductor, $includes, $limitFields) {
|
||||||
|
$conductor->applyIncludes($model, $includes);
|
||||||
|
|
||||||
|
if (count($limitFields) > 0) {
|
||||||
|
$model->setAppends(array_intersect($model->getAppends(), $limitFields));
|
||||||
|
}
|
||||||
|
|
||||||
|
$model = $conductor->transformModel($model);
|
||||||
|
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return [$conductor->collection, $total];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the conductor on a collection with the data stored in a Request.
|
||||||
|
*
|
||||||
|
* @param Request $request The request data.
|
||||||
|
* @param Collection $collection The collection.
|
||||||
|
* @return array The processed and transformed model data.
|
||||||
|
*/
|
||||||
|
final public static function collection(Request $request, Collection $collection): array
|
||||||
|
{
|
||||||
|
$conductor_class = get_called_class();
|
||||||
|
$conductor = new $conductor_class();
|
||||||
|
|
||||||
|
$conductor->collection = collect();
|
||||||
|
|
||||||
|
foreach ($collection as $item) {
|
||||||
|
if ($conductor->viewable($item) === true) {
|
||||||
|
$conductor->collection->push($conductor->transformModel($item));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter request
|
||||||
|
$limitFields = $conductor->fields(new $conductor->class());
|
||||||
|
if (is_array($limitFields) === false) {
|
||||||
|
$limitFields = [];
|
||||||
|
}
|
||||||
|
$conductor->filter($request, $limitFields);
|
||||||
|
|
||||||
|
// Get total
|
||||||
|
$total = $conductor->collection->count();
|
||||||
|
|
||||||
|
// Sort request
|
||||||
|
$sort = $request->input('sort', $conductor->sort);
|
||||||
|
if (strlen($sort) === 0) {
|
||||||
|
if (strlen($conductor->sort) > 0) {
|
||||||
|
$conductor->sort($sort);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$conductor->sort($sort);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Paginate
|
||||||
|
$conductor->paginate($request->input('page', 1), $request->input('limit', -1), $request->input('offset', 0));
|
||||||
|
|
||||||
|
|
||||||
|
return [$conductor->collection, $total];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter a custom query on a user request.
|
||||||
|
*
|
||||||
|
* @param Builder $query The custom query.
|
||||||
|
* @param Request $request The request.
|
||||||
|
* @param array|null $limitFields Limit the request to these fields.
|
||||||
|
*/
|
||||||
|
public static function filterQuery(Builder $query, Request $request, array|null $limitFields = null): Builder
|
||||||
|
{
|
||||||
|
$conductor_class = get_called_class();
|
||||||
|
$conductor = new $conductor_class();
|
||||||
|
|
||||||
|
$conductor->query = $query;
|
||||||
|
$conductor->filter($request, $limitFields);
|
||||||
|
|
||||||
|
return $conductor->query;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the conductor on a Model with the data stored in a Request.
|
||||||
|
*
|
||||||
|
* @param Request $request The request data.
|
||||||
|
* @param string $key The key prefix to use.
|
||||||
|
* @param Model|null $model The model.
|
||||||
|
* @return array The processed and transformed model data.
|
||||||
|
*/
|
||||||
|
final public static function includeModel(Request $request, string $key, mixed $model): array
|
||||||
|
{
|
||||||
|
$fields = [];
|
||||||
|
|
||||||
|
if ($request !== null && $request->has('fields') === true) {
|
||||||
|
$requestFields = $request->input('fields');
|
||||||
|
if ($requestFields !== null) {
|
||||||
|
$requestFields = explode(',', $requestFields);
|
||||||
|
if (in_array($key, $requestFields) === false) {
|
||||||
|
foreach ($requestFields as $field) {
|
||||||
|
if (strpos($field, $key . '.') === 0) {
|
||||||
|
$fields[] = substr($field, (strlen($key) + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return static::model($fields, $model);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the conductor on a Model with the data stored in a Request.
|
||||||
|
*
|
||||||
|
* @param mixed $fields The fields to show.
|
||||||
|
* @param Model|null $model The model.
|
||||||
|
* @return array The processed and transformed model data.
|
||||||
|
*/
|
||||||
|
final public static function model(mixed $fields, mixed $model): array
|
||||||
|
{
|
||||||
|
if ($model === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$conductor_class = get_called_class();
|
||||||
|
$conductor = new $conductor_class();
|
||||||
|
|
||||||
|
$modelFields = $conductor->fields(new $conductor->class());
|
||||||
|
|
||||||
|
// Limit fields
|
||||||
|
$limitFields = $modelFields;
|
||||||
|
if ($fields instanceof Request) {
|
||||||
|
if ($fields !== null && $fields->has('fields') === true) {
|
||||||
|
$requestFields = $fields->input('fields');
|
||||||
|
if ($requestFields !== null) {
|
||||||
|
$limitFields = array_intersect(explode(',', $requestFields), $modelFields);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} elseif (is_array($fields) === true && count($fields) > 0) {
|
||||||
|
$limitFields = array_intersect($fields, $modelFields);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($limitFields) === false) {
|
||||||
|
$modelAppends = $model->getAppends();
|
||||||
|
|
||||||
|
foreach (array_diff($modelFields, $limitFields) as $attribute) {
|
||||||
|
$key = array_search($attribute, $modelAppends);
|
||||||
|
if ($key !== false) {
|
||||||
|
unset($modelAppends[$key]);
|
||||||
|
} else {
|
||||||
|
unset($model[$attribute]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$model->setAppends($modelAppends);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Includes
|
||||||
|
$includes = array_intersect($limitFields, $conductor->includes);
|
||||||
|
$conductor->applyIncludes($model, $includes);
|
||||||
|
|
||||||
|
// Transform
|
||||||
|
$model = $conductor->transformModel($model);
|
||||||
|
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the current conductor collection count.
|
||||||
|
*
|
||||||
|
* @return integer The current collection count.
|
||||||
|
*/
|
||||||
|
final public function count(): int
|
||||||
|
{
|
||||||
|
if ($this->query !== null) {
|
||||||
|
return $this->query->count();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sort the conductor collection.
|
||||||
|
*
|
||||||
|
* @param mixed $fields A field name or array of field names to sort. Supports prefix of +/- to change direction.
|
||||||
|
*/
|
||||||
|
final public function sort(mixed $fields = null): void
|
||||||
|
{
|
||||||
|
$collectionSort = [];
|
||||||
|
|
||||||
|
if (is_string($fields) === true) {
|
||||||
|
$fields = explode(',', $fields);
|
||||||
|
} elseif ($fields === null) {
|
||||||
|
$fields = $this->sort;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($fields) === true) {
|
||||||
|
foreach ($fields as $orderByField) {
|
||||||
|
$direction = 'asc';
|
||||||
|
$directionChar = substr($orderByField, 0, 1);
|
||||||
|
|
||||||
|
if (in_array($directionChar, ['-', '+']) === true) {
|
||||||
|
$orderByField = substr($orderByField, 1);
|
||||||
|
if ($directionChar === '-') {
|
||||||
|
$direction = 'desc';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->collection !== null) {
|
||||||
|
$collectionSort[] = [trim($orderByField), $direction];
|
||||||
|
} else {
|
||||||
|
$this->query->orderBy(trim($orderByField), $direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new \InvalidArgumentException('Expected string or array, got ' . gettype($fields));
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
if ($this->collection !== null) {
|
||||||
|
$this->collection = $this->collection->sortBy($collectionSort)->values();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Paginate the conductor collection.
|
||||||
|
*
|
||||||
|
* @param integer $page The current page to return.
|
||||||
|
* @param integer $limit The limit of items to include or use default.
|
||||||
|
* @param integer $offset Offset the page count after this count of rows.
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
final public function paginate(int $page = 1, int $limit = -1, int $offset = 0)
|
||||||
|
{
|
||||||
|
// Limit
|
||||||
|
if ($limit < 1) {
|
||||||
|
$limit = $this->limit;
|
||||||
|
} else {
|
||||||
|
$limit = min($limit, $this->maxLimit);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Page
|
||||||
|
if ($page < 1) {
|
||||||
|
$page = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// After
|
||||||
|
if ($offset < 0) {
|
||||||
|
$offset = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->collection !== null) {
|
||||||
|
$this->collection = $this->collection->splice(((($page - 1) * $limit) + $offset), $limit);
|
||||||
|
} else {
|
||||||
|
$this->query->limit($limit);
|
||||||
|
$this->query->offset((($page - 1) * $limit) + $offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Apply a list of includes to the model.
|
||||||
|
*
|
||||||
|
* @param Model $model The model to append.
|
||||||
|
* @param array $includes The list of includes to include.
|
||||||
|
*/
|
||||||
|
final public function applyIncludes(Model $model, array $includes): void
|
||||||
|
{
|
||||||
|
foreach ($includes as $include) {
|
||||||
|
$includeMethodName = 'include' . Str::studly($include);
|
||||||
|
if (method_exists($this, $includeMethodName) === true) {
|
||||||
|
$attributeName = Str::snake($include);
|
||||||
|
$attributeValue = $this->{$includeMethodName}($model);
|
||||||
|
if ($attributeValue !== null) {
|
||||||
|
$model->$attributeName = $this->{$includeMethodName}($model);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Limit the returned fields in the conductor collection.
|
||||||
|
*
|
||||||
|
* @param array $fields An array of field names.
|
||||||
|
*/
|
||||||
|
final public function limitFields(array $fields): void
|
||||||
|
{
|
||||||
|
if (empty($fields) !== true) {
|
||||||
|
$this->query->select(array_diff($fields, $this->includes));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter the conductor collection using raw data.
|
||||||
|
*
|
||||||
|
* @param string $rawFilter The raw filter string to parse.
|
||||||
|
* @param array|null $limitFields The fields to allow in the filter string.
|
||||||
|
* @param string $outerJoin The join for this filter group.
|
||||||
|
*/
|
||||||
|
final public function appendFilterString(string $rawFilter, array|null $limitFields = null, string $outerJoin = 'OR'): void
|
||||||
|
{
|
||||||
|
if ($rawFilter === '') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (substr($rawFilter, -1) !== ',') {
|
||||||
|
$rawFilter .= ',';
|
||||||
|
}
|
||||||
|
|
||||||
|
$parseFunc = function ($string, &$i = 0) use (&$parseFunc, $limitFields) {
|
||||||
|
$tokens = [];
|
||||||
|
$ignoreUntil = '';
|
||||||
|
$skipUntil = '';
|
||||||
|
$field = '';
|
||||||
|
$value = null;
|
||||||
|
$set = &$field;
|
||||||
|
|
||||||
|
for (; $i < strlen($string); $i++) {
|
||||||
|
$char = $string[$i];
|
||||||
|
|
||||||
|
if ($skipUntil !== '' && $char !== $skipUntil) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($ignoreUntil === '') {
|
||||||
|
if ($char === '\'' || $char === '"') {
|
||||||
|
$ignoreUntil = $char;
|
||||||
|
} elseif ($char === ':') {
|
||||||
|
if ($field === '') {
|
||||||
|
$skipUntil = ',';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($field[0] === '\'' || $field[0] === '"') {
|
||||||
|
$field = substr($field, 1, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
$set = &$value;
|
||||||
|
continue;
|
||||||
|
} elseif (($char === ')' && $string[($i + 1)] === ',') || $char === ',') {
|
||||||
|
if ($value === null) {
|
||||||
|
$tokens[] = $field;
|
||||||
|
} else {
|
||||||
|
$value = trim($value);
|
||||||
|
$operator = 'LIKE';
|
||||||
|
|
||||||
|
// Check if value has a operator and remove it if it's a number
|
||||||
|
if (preg_match('/^(!?=|[<>]=?|<>|!)([^=!<>].*)*$/', $value, $matches) > 0) {
|
||||||
|
$operator = $matches[1];
|
||||||
|
$value = ($matches[2] ?? '');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($value[0] === '\'' || $value[0] === '"') {
|
||||||
|
$value = substr($value, 1, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($operator === 'LIKE') {
|
||||||
|
$value = "%{$value}%";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
is_array($limitFields) === false ||
|
||||||
|
in_array(strtolower($field), array_map('strtolower', $limitFields)) !== false
|
||||||
|
) {
|
||||||
|
$tokens[] = [$field, $operator, $value];
|
||||||
|
}
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
$field = '';
|
||||||
|
$value = null;
|
||||||
|
$set = &$field;
|
||||||
|
|
||||||
|
if ($char === ')') {
|
||||||
|
$i++;
|
||||||
|
return $tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
} elseif ($char === '(') {
|
||||||
|
if ($field === '') {
|
||||||
|
$i++;
|
||||||
|
$tokens[] = $parseFunc($string, $i);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}//end if
|
||||||
|
} elseif ($char === $ignoreUntil) {
|
||||||
|
$ignoreUntil = '';
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
$set .= $char;
|
||||||
|
}//end for
|
||||||
|
|
||||||
|
return $tokens;
|
||||||
|
};
|
||||||
|
|
||||||
|
$i = 0;
|
||||||
|
$filterArray = $parseFunc($rawFilter, $i);
|
||||||
|
|
||||||
|
if (count($this->filterArray) !== 0) {
|
||||||
|
$this->filterArray[] = $outerJoin;
|
||||||
|
}
|
||||||
|
$this->filterArray[] = $filterArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append a field to the filter array.
|
||||||
|
*
|
||||||
|
* @param string $field The field name to append.
|
||||||
|
* @param string $operator The operator to append.
|
||||||
|
* @param string $value The value to append.
|
||||||
|
* @param string $join The join to append.
|
||||||
|
*/
|
||||||
|
final public function appendFilter(string $field, string $operator, string $value, string $join = 'OR'): void
|
||||||
|
{
|
||||||
|
if (count($this->filterArray) !== 0) {
|
||||||
|
$this->filterArray[] = $join;
|
||||||
|
}
|
||||||
|
$this->filterArray[] = [$field, $operator, $value];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run a scope query on the collection before anything else.
|
||||||
|
*
|
||||||
|
* @param Builder $builder The builder in use.
|
||||||
|
*/
|
||||||
|
public function scope(Builder $builder): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array of model fields visible to the current user.
|
||||||
|
*
|
||||||
|
* @param Model $model The model in question.
|
||||||
|
* @return array The array of field names.
|
||||||
|
*/
|
||||||
|
public function fields(Model $model): array
|
||||||
|
{
|
||||||
|
$visibleFields = $model->getVisible();
|
||||||
|
if (empty($visibleFields) === true) {
|
||||||
|
$visibleFields = $model->getConnection()
|
||||||
|
->getSchemaBuilder()
|
||||||
|
->getColumnListing($model->getTable());
|
||||||
|
}
|
||||||
|
|
||||||
|
$appends = $model->getAppends();
|
||||||
|
if (is_array($appends) === true) {
|
||||||
|
$visibleFields = array_merge($visibleFields, $appends);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($this->includes) === true) {
|
||||||
|
$visibleFields = array_merge($visibleFields, $this->includes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $visibleFields;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform the passed Model to an array
|
||||||
|
*
|
||||||
|
* @param Model $model The model to transform.
|
||||||
|
* @return array The transformed model.
|
||||||
|
*/
|
||||||
|
protected function transformModel(Model $model): array
|
||||||
|
{
|
||||||
|
$result = $this->transform($model);
|
||||||
|
foreach ($result as $key => $value) {
|
||||||
|
$transformFunction = 'transform' . Str::studly($key);
|
||||||
|
if (method_exists($this, $transformFunction) === true) {
|
||||||
|
$result[$key] = $this->$transformFunction($value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = $this->transformFinal($result);
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform the passed Model to an array
|
||||||
|
*
|
||||||
|
* @param Model $model The model to transform.
|
||||||
|
* @return array The transformed model.
|
||||||
|
*/
|
||||||
|
public function transform(Model $model): array
|
||||||
|
{
|
||||||
|
$result = $model->toArray();
|
||||||
|
|
||||||
|
$fields = $this->fields($model);
|
||||||
|
|
||||||
|
if (is_array($fields) === true) {
|
||||||
|
$result = array_intersect_key($result, array_flip($fields));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Final Transform of the model array
|
||||||
|
*
|
||||||
|
* @param array $data The model array to transform.
|
||||||
|
* @return array The transformed model.
|
||||||
|
*/
|
||||||
|
public function transformFinal(array $data): array
|
||||||
|
{
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is the passed model viewable by the current user?
|
||||||
|
*
|
||||||
|
* @param Model $model The model in question.
|
||||||
|
* @return boolean Is the model viewable.
|
||||||
|
*/
|
||||||
|
public static function viewable(Model $model): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is the model creatable by the current user?
|
||||||
|
*
|
||||||
|
* @return boolean Is the model creatable.
|
||||||
|
*/
|
||||||
|
public static function creatable(): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is the passed model updatable by the current user?
|
||||||
|
*
|
||||||
|
* @param Model $model The model in question.
|
||||||
|
* @return boolean Is the model updatable.
|
||||||
|
*/
|
||||||
|
public static function updatable(Model $model): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is the passed model destroyable by the current user?
|
||||||
|
*
|
||||||
|
* @param Model $model The model in question.
|
||||||
|
* @return boolean Is the model destroyable.
|
||||||
|
*/
|
||||||
|
public static function destroyable(Model $model): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
127
app/Conductors/EventConductor.php
Normal file
127
app/Conductors/EventConductor.php
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Conductors;
|
||||||
|
|
||||||
|
use App\Models\Media;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\InvalidCastException;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class EventConductor extends Conductor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The Model Class
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $class = \App\Models\Event::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default sorting field
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $sort = '-start_at';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The included fields
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $includes = ['attachments'];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run a scope query on the collection before anything else.
|
||||||
|
*
|
||||||
|
* @param Builder $builder The builder in use.
|
||||||
|
*/
|
||||||
|
public function scope(Builder $builder): void
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
if ($user === null || $user->hasPermission('admin/events') === false) {
|
||||||
|
$builder
|
||||||
|
->where('status', '!=', 'draft')
|
||||||
|
->where('publish_at', '<=', now());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is visible.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow model to be visible.
|
||||||
|
*/
|
||||||
|
public static function viewable(Model $model): bool
|
||||||
|
{
|
||||||
|
if (strtolower($model->status) === 'draft' || Carbon::parse($model->publish_at)->isFuture() === true) {
|
||||||
|
$user = auth()->user();
|
||||||
|
if ($user === null || $user->hasPermission('admin/events') === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is creatable.
|
||||||
|
*
|
||||||
|
* @return boolean Allow creating model.
|
||||||
|
*/
|
||||||
|
public static function creatable(): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/events') === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is updatable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow updating model.
|
||||||
|
*/
|
||||||
|
public static function updatable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/events') === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is destroyable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow deleting model.
|
||||||
|
*/
|
||||||
|
public static function destroyable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/events') === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Include Attachments Field.
|
||||||
|
*
|
||||||
|
* @param Model $model Them model.
|
||||||
|
* @return mixed The model result.
|
||||||
|
*/
|
||||||
|
public function includeAttachments(Model $model)
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
|
return $model->attachments()->get()->map(function ($attachment) use ($user) {
|
||||||
|
if ($attachment->private === false || ($user !== null && ($user->hasPermission('admin/events') === true || $attachment->users->contains($user) === true))) {
|
||||||
|
return MediaConductor::includeModel(request(), 'attachments', $attachment->media);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform the Hero field.
|
||||||
|
*
|
||||||
|
* @param mixed $value The current value.
|
||||||
|
* @return array The new value.
|
||||||
|
*/
|
||||||
|
public function transformHero(mixed $value): array
|
||||||
|
{
|
||||||
|
return MediaConductor::includeModel(request(), 'hero', Media::find($value));
|
||||||
|
}
|
||||||
|
}
|
||||||
148
app/Conductors/MediaConductor.php
Normal file
148
app/Conductors/MediaConductor.php
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Conductors;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Foundation\Auth\User;
|
||||||
|
|
||||||
|
class MediaConductor extends Conductor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The Model Class
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $class = \App\Models\Media::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default sorting field
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $sort = 'created_at';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The included fields
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $includes = ['user'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default filters to use in a request.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $defaultFilters = [
|
||||||
|
'status' => 'OK'
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array of model fields visible to the current user.
|
||||||
|
*
|
||||||
|
* @param Model $model The model in question.
|
||||||
|
* @return array The array of field names.
|
||||||
|
*/
|
||||||
|
public function fields(Model $model): array
|
||||||
|
{
|
||||||
|
$fields = parent::fields($model);
|
||||||
|
|
||||||
|
$user = auth()->user();
|
||||||
|
if ($user === null || $user->hasPermission('admin/media') === false) {
|
||||||
|
$fields = arrayRemoveItem($fields, ['permission', 'storage']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run a scope query on the collection before anything else.
|
||||||
|
*
|
||||||
|
* @param Builder $builder The builder in use.
|
||||||
|
*/
|
||||||
|
public function scope(Builder $builder): void
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
if ($user === null) {
|
||||||
|
$builder->where('permission', '');
|
||||||
|
} else {
|
||||||
|
$builder->where('permission', '')->orWhereIn('permission', $user->permissions);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is visible.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow model to be visible.
|
||||||
|
*/
|
||||||
|
public static function viewable(Model $model): bool
|
||||||
|
{
|
||||||
|
if ($model->permission !== '') {
|
||||||
|
$user = auth()->user();
|
||||||
|
if ($user === null || $user->hasPermission($model->permission) === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is creatable.
|
||||||
|
*
|
||||||
|
* @return boolean Allow creating model.
|
||||||
|
*/
|
||||||
|
public static function creatable(): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is updatable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow updating model.
|
||||||
|
*/
|
||||||
|
public static function updatable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && (strcasecmp($model->user_id, $user->id) === 0 || $user->hasPermission('admin/media') === true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is destroyable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow deleting model.
|
||||||
|
*/
|
||||||
|
public static function destroyable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && ($model->user_id === $user->id || $user->hasPermission('admin/media') === true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform the final model data
|
||||||
|
*
|
||||||
|
* @param array $data The model data to transform.
|
||||||
|
* @return array The transformed model.
|
||||||
|
*/
|
||||||
|
public function transformFinal(array $data): array
|
||||||
|
{
|
||||||
|
unset($data['user_id']);
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Include User Field.
|
||||||
|
*
|
||||||
|
* @param Model $model Them model.
|
||||||
|
* @return mixed The model result.
|
||||||
|
*/
|
||||||
|
public function includeUser(Model $model)
|
||||||
|
{
|
||||||
|
return UserConductor::includeModel(request(), 'user', User::find($model['user_id']));
|
||||||
|
}
|
||||||
|
}
|
||||||
58
app/Conductors/ShortlinkConductor.php
Normal file
58
app/Conductors/ShortlinkConductor.php
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Conductors;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Foundation\Auth\User;
|
||||||
|
|
||||||
|
class ShortlinkConductor extends Conductor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The Model Class
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $class = \App\Models\Shortlink::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default sorting field
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $sort = 'created_at';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is creatable.
|
||||||
|
*
|
||||||
|
* @return boolean Allow creating model.
|
||||||
|
*/
|
||||||
|
public static function creatable(): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/shortlinks') === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is updatable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow updating model.
|
||||||
|
*/
|
||||||
|
public static function updatable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/shortlinks') === true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is destroyable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow deleting model.
|
||||||
|
*/
|
||||||
|
public static function destroyable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/shortlinks') === true);
|
||||||
|
}
|
||||||
|
}
|
||||||
39
app/Conductors/SubscriptionConductor.php
Normal file
39
app/Conductors/SubscriptionConductor.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Conductors;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class SubscriptionConductor extends Conductor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The Model Class
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $class = \App\Models\Subscription::class;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is updatable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow updating model.
|
||||||
|
*/
|
||||||
|
public static function updatable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && ((strcasecmp($model->email, $user->email) === 0 && $user->email_verified_at !== null) || $user->hasPermission('admin/subscriptions') === true));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is destroyable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow deleting model.
|
||||||
|
*/
|
||||||
|
public static function destroyable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && ((strcasecmp($model->email, $user->email) === 0 && $user->email_verified_at !== null) || $user->hasPermission('admin/subscriptions') === true));
|
||||||
|
}
|
||||||
|
}
|
||||||
80
app/Conductors/UserConductor.php
Normal file
80
app/Conductors/UserConductor.php
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Conductors;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class UserConductor extends Conductor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The Model Class
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $class = \App\Models\User::class;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the visible API fields.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return string[] The fields visible.
|
||||||
|
*/
|
||||||
|
public function fields(Model $model): array
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
if ($user === null || $user->hasPermission('admin/users') === false) {
|
||||||
|
return ['id', 'display_name'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::fields($model);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transform the passed Model to an array
|
||||||
|
*
|
||||||
|
* @param Model $model The model to transform.
|
||||||
|
* @return array The transformed model.
|
||||||
|
*/
|
||||||
|
public function transform(Model $model): array
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
$data = $model->toArray();
|
||||||
|
|
||||||
|
if ($user === null || ($user->hasPermission('admin/users') === false && strcasecmp($user->id, $model->id) !== 0)) {
|
||||||
|
$fields = ['id', 'display_name'];
|
||||||
|
$data = arrayLimitKeys($data, $fields);
|
||||||
|
} else {
|
||||||
|
$data['permissions'] = $user->permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is updatable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow updating model.
|
||||||
|
*/
|
||||||
|
public static function updatable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
if ($user !== null) {
|
||||||
|
return ($user->hasPermission('admin/users') === true || strcasecmp($user->id, $model->id) === 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return if the current model is destroyable.
|
||||||
|
*
|
||||||
|
* @param Model $model The model.
|
||||||
|
* @return boolean Allow deleting model.
|
||||||
|
*/
|
||||||
|
public static function destroyable(Model $model): bool
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
return ($user !== null && $user->hasPermission('admin/users') === true);
|
||||||
|
}
|
||||||
|
}
|
||||||
63
app/Console/Commands/MediaMigrate.php
Normal file
63
app/Console/Commands/MediaMigrate.php
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Jobs\StoreUploadedFileJob;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Models\Media;
|
||||||
|
use File;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
|
||||||
|
class MediaMigrate extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'media:migrate';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Migrate the uploads folder to the CDN';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure the command options.
|
||||||
|
*/
|
||||||
|
protected function configure(): void
|
||||||
|
{
|
||||||
|
$this->addOption(
|
||||||
|
'replace',
|
||||||
|
null,
|
||||||
|
InputOption::VALUE_NONE,
|
||||||
|
'Replace existing files'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*/
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$replace = $this->option('replace');
|
||||||
|
|
||||||
|
$files = File::allFiles(public_path('uploads'));
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
$filename = pathinfo($file, PATHINFO_BASENAME);
|
||||||
|
|
||||||
|
$medium = Media::where('name', $filename)->first();
|
||||||
|
|
||||||
|
if ($medium !== null) {
|
||||||
|
$medium->update(['status' => 'Processing media']);
|
||||||
|
StoreUploadedFileJob::dispatch($medium, $file, $replace)->onQueue('media');
|
||||||
|
} else {
|
||||||
|
unlink($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
66
app/Console/Commands/MediaRebuild.php
Normal file
66
app/Console/Commands/MediaRebuild.php
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use App\Jobs\StoreUploadedFileJob;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use App\Models\Media;
|
||||||
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
|
||||||
|
class MediaRebuild extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'media:rebuild';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Rebuild the media table';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure the command options.
|
||||||
|
*/
|
||||||
|
protected function configure(): void
|
||||||
|
{
|
||||||
|
$this->addOption(
|
||||||
|
'replace',
|
||||||
|
null,
|
||||||
|
InputOption::VALUE_NONE,
|
||||||
|
'Replace existing files'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->addOption(
|
||||||
|
'all',
|
||||||
|
null,
|
||||||
|
InputOption::VALUE_NONE,
|
||||||
|
'Rebuild all variants'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*/
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$replace = $this->option('replace');
|
||||||
|
$all = $this->option('replace');
|
||||||
|
|
||||||
|
$media = [];
|
||||||
|
if ($all === true) {
|
||||||
|
$media = Media::all();
|
||||||
|
} else {
|
||||||
|
$media = Media::where(['variants' => ''])->orWhere(['variants' => '[]'])->orWhere(['variants' => '{}'])->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($media as $medium) {
|
||||||
|
StoreUploadedFileJob::dispatch($medium, '', $replace)->onQueue('media');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,19 +11,16 @@ class Kernel extends ConsoleKernel
|
|||||||
* Define the application's command schedule.
|
* Define the application's command schedule.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Console\Scheduling\Schedule $schedule The schedule.
|
* @param \Illuminate\Console\Scheduling\Schedule $schedule The schedule.
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
protected function schedule(Schedule $schedule)
|
protected function schedule(Schedule $schedule): void
|
||||||
{
|
{
|
||||||
// $schedule->command('inspire')->hourly();
|
// $schedule->command('inspire')->hourly();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the commands for the application.
|
* Register the commands for the application.
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
protected function commands()
|
protected function commands(): void
|
||||||
{
|
{
|
||||||
$this->load(__DIR__ . '/Commands');
|
$this->load(__DIR__ . '/Commands');
|
||||||
|
|
||||||
|
|||||||
169
app/Enum/CurlErrorCodes.php
Normal file
169
app/Enum/CurlErrorCodes.php
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enum;
|
||||||
|
|
||||||
|
class CurlErrorCodes extends Enum
|
||||||
|
{
|
||||||
|
public const CURLE_UNSUPPORTED_PROTOCOL = 1;
|
||||||
|
public const CURLE_FAILED_INIT = 2;
|
||||||
|
public const CURLE_URL_MALFORMAT = 3;
|
||||||
|
public const CURLE_URL_MALFORMAT_USER = 4;
|
||||||
|
public const CURLE_COULDNT_RESOLVE_PROXY = 5;
|
||||||
|
public const CURLE_COULDNT_RESOLVE_HOST = 6;
|
||||||
|
public const CURLE_COULDNT_CONNECT = 7;
|
||||||
|
public const CURLE_FTP_WEIRD_SERVER_REPLY = 8;
|
||||||
|
public const CURLE_REMOTE_ACCESS_DENIED = 9;
|
||||||
|
public const CURLE_FTP_WEIRD_PASS_REPLY = 11;
|
||||||
|
public const CURLE_FTP_WEIRD_PASV_REPLY = 13;
|
||||||
|
public const CURLE_FTP_WEIRD_227_FORMAT = 14;
|
||||||
|
public const CURLE_FTP_CANT_GET_HOST = 15;
|
||||||
|
public const CURLE_FTP_COULDNT_SET_TYPE = 17;
|
||||||
|
public const CURLE_PARTIAL_FILE = 18;
|
||||||
|
public const CURLE_FTP_COULDNT_RETR_FILE = 19;
|
||||||
|
public const CURLE_QUOTE_ERROR = 21;
|
||||||
|
public const CURLE_HTTP_RETURNED_ERROR = 22;
|
||||||
|
public const CURLE_WRITE_ERROR = 23;
|
||||||
|
public const CURLE_UPLOAD_FAILED = 25;
|
||||||
|
public const CURLE_READ_ERROR = 26;
|
||||||
|
public const CURLE_OUT_OF_MEMORY = 27;
|
||||||
|
public const CURLE_OPERATION_TIMEDOUT = 28;
|
||||||
|
public const CURLE_FTP_PORT_FAILED = 30;
|
||||||
|
public const CURLE_FTP_COULDNT_USE_REST = 31;
|
||||||
|
public const CURLE_RANGE_ERROR = 33;
|
||||||
|
public const CURLE_HTTP_POST_ERROR = 34;
|
||||||
|
public const CURLE_SSL_CONNECT_ERROR = 35;
|
||||||
|
public const CURLE_BAD_DOWNLOAD_RESUME = 36;
|
||||||
|
public const CURLE_FILE_COULDNT_READ_FILE = 37;
|
||||||
|
public const CURLE_LDAP_CANNOT_BIND = 38;
|
||||||
|
public const CURLE_LDAP_SEARCH_FAILED = 39;
|
||||||
|
public const CURLE_FUNCTION_NOT_FOUND = 41;
|
||||||
|
public const CURLE_ABORTED_BY_CALLBACK = 42;
|
||||||
|
public const CURLE_BAD_FUNCTION_ARGUMENT = 43;
|
||||||
|
public const CURLE_INTERFACE_FAILED = 45;
|
||||||
|
public const CURLE_TOO_MANY_REDIRECTS = 47;
|
||||||
|
public const CURLE_UNKNOWN_TELNET_OPTION = 48;
|
||||||
|
public const CURLE_TELNET_OPTION_SYNTAX = 49;
|
||||||
|
public const CURLE_PEER_FAILED_VERIFICATION = 51;
|
||||||
|
public const CURLE_GOT_NOTHING = 52;
|
||||||
|
public const CURLE_SSL_ENGINE_NOTFOUND = 53;
|
||||||
|
public const CURLE_SSL_ENGINE_SETFAILED = 54;
|
||||||
|
public const CURLE_SEND_ERROR = 55;
|
||||||
|
public const CURLE_RECV_ERROR = 56;
|
||||||
|
public const CURLE_SSL_CERTPROBLEM = 58;
|
||||||
|
public const CURLE_SSL_CIPHER = 59;
|
||||||
|
public const CURLE_SSL_CACERT = 60;
|
||||||
|
public const CURLE_BAD_CONTENT_ENCODING = 61;
|
||||||
|
public const CURLE_LDAP_INVALID_URL = 62;
|
||||||
|
public const CURLE_FILESIZE_EXCEEDED = 63;
|
||||||
|
public const CURLE_USE_SSL_FAILED = 64;
|
||||||
|
public const CURLE_SEND_FAIL_REWIND = 65;
|
||||||
|
public const CURLE_SSL_ENGINE_INITFAILED = 66;
|
||||||
|
public const CURLE_LOGIN_DENIED = 67;
|
||||||
|
public const CURLE_TFTP_NOTFOUND = 68;
|
||||||
|
public const CURLE_TFTP_PERM = 69;
|
||||||
|
public const CURLE_REMOTE_DISK_FULL = 70;
|
||||||
|
public const CURLE_TFTP_ILLEGAL = 71;
|
||||||
|
public const CURLE_TFTP_UNKNOWNID = 72;
|
||||||
|
public const CURLE_REMOTE_FILE_EXISTS = 73;
|
||||||
|
public const CURLE_TFTP_NOSUCHUSER = 74;
|
||||||
|
public const CURLE_CONV_FAILED = 75;
|
||||||
|
public const CURLE_CONV_REQD = 76;
|
||||||
|
public const CURLE_SSL_CACERT_BADFILE = 77;
|
||||||
|
public const CURLE_REMOTE_FILE_NOT_FOUND = 78;
|
||||||
|
public const CURLE_SSH = 79;
|
||||||
|
public const CURLE_SSL_SHUTDOWN_FAILED = 80;
|
||||||
|
public const CURLE_AGAIN = 81;
|
||||||
|
public const CURLE_SSL_CRL_BADFILE = 82;
|
||||||
|
public const CURLE_SSL_ISSUER_ERROR = 83;
|
||||||
|
public const CURLE_FTP_PRET_FAILED = 84;
|
||||||
|
public const CURLE_RTSP_CSEQ_ERROR = 85;
|
||||||
|
public const CURLE_RTSP_SESSION_ERROR = 86;
|
||||||
|
public const CURLE_FTP_BAD_FILE_LIST = 87;
|
||||||
|
public const CURLE_CHUNK_FAILED = 88;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Curl Error messages
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
public static $messages = [
|
||||||
|
1 => 'Unsupported protocol.',
|
||||||
|
2 => 'Failed initalization.',
|
||||||
|
3 => 'Invalid URL format.',
|
||||||
|
4 => 'CURLE_URL_MALFORMAT_USER.',
|
||||||
|
5 => 'Could not resolve proxy.',
|
||||||
|
6 => 'Could not resolve host.',
|
||||||
|
7 => 'Could not connect to host.',
|
||||||
|
8 => 'Invalid reply from FTP server.',
|
||||||
|
9 => 'Access denied on host.',
|
||||||
|
11 => 'Invalid pass reply from FTP server.',
|
||||||
|
13 => 'Invalid pasv reply from FTP server.',
|
||||||
|
14 => 'Invalid 227 format from FTP server.',
|
||||||
|
15 => 'Could not get FTP host.',
|
||||||
|
17 => 'Could not set type for FTP transfer.',
|
||||||
|
18 => 'Invalid partial size.',
|
||||||
|
19 => 'Could not retrieve file from FTP server.',
|
||||||
|
21 => 'Quote error.',
|
||||||
|
22 => 'HTTP server returned error.',
|
||||||
|
23 => 'File write error.',
|
||||||
|
25 => 'Upload file error.',
|
||||||
|
26 => 'File read error.',
|
||||||
|
27 => 'Out of memory.',
|
||||||
|
28 => 'File transfer timed out.',
|
||||||
|
30 => 'Invalid port for FTP server.',
|
||||||
|
31 => 'Could not use rest for FTP server.',
|
||||||
|
33 => 'File range error.',
|
||||||
|
34 => 'Invalid POST for HTTP server.',
|
||||||
|
35 => 'SSL connectio error.',
|
||||||
|
36 => 'Invalid resume download.',
|
||||||
|
37 => 'Could not read file.',
|
||||||
|
38 => 'Could not bind to LDAP.',
|
||||||
|
39 => 'LDAP search failed.',
|
||||||
|
41 => 'Function not found.',
|
||||||
|
42 => 'Aborted by callback.',
|
||||||
|
43 => 'Bad function argument.',
|
||||||
|
45 => 'Interface failed.',
|
||||||
|
47 => 'Too many redirects.',
|
||||||
|
48 => 'Unknown telnet option.',
|
||||||
|
49 => 'Telnet option syntax invalid.',
|
||||||
|
51 => 'Peer failed verification.',
|
||||||
|
52 => 'Did not receive any data.',
|
||||||
|
53 => 'SSL engine was not found.',
|
||||||
|
54 => 'SSL engine failed.',
|
||||||
|
55 => 'Send data error.',
|
||||||
|
56 => 'Receive data error.',
|
||||||
|
58 => 'SSL certificate error.',
|
||||||
|
59 => 'SSL cipher error.',
|
||||||
|
60 => 'SSL CACertificate failed.',
|
||||||
|
61 => 'Invalid content encoding.',
|
||||||
|
62 => 'Invalid LDAP url.',
|
||||||
|
63 => 'Filesize exceeded.',
|
||||||
|
64 => 'SSL Failed.',
|
||||||
|
65 => 'CURLE_SEND_FAIL_REWIND.',
|
||||||
|
66 => 'SSL engine initalization failed.',
|
||||||
|
67 => 'CURLE_LOGIN_DENIED.',
|
||||||
|
68 => 'CURLE_TFTP_NOTFOUND.',
|
||||||
|
69 => 'CURLE_TFTP_PERM.',
|
||||||
|
70 => 'CURLE_REMOTE_DISK_FULL.',
|
||||||
|
71 => 'CURLE_TFTP_ILLEGAL.',
|
||||||
|
72 => 'CURLE_TFTP_UNKNOWNID.',
|
||||||
|
73 => 'Remote file already exists.',
|
||||||
|
74 => 'No such user on FTP server.',
|
||||||
|
75 => 'Conversion failed.',
|
||||||
|
76 => 'Conversion required.',
|
||||||
|
77 => 'SSL CACertificate bad file.',
|
||||||
|
78 => 'Remove file not found.',
|
||||||
|
79 => 'SSH error.',
|
||||||
|
80 => 'SSL Shutdown failed.',
|
||||||
|
81 => 'Again.',
|
||||||
|
82 => 'SSL bad CRL file.',
|
||||||
|
83 => 'SSL issuer error.',
|
||||||
|
84 => 'FTP pret failed.',
|
||||||
|
85 => 'CURLE_RTSP_CSEQ_ERROR.',
|
||||||
|
86 => 'CURLE_RTSP_SESSION_ERROR.',
|
||||||
|
87 => 'CURLE_FTP_BAD_FILE_LIST.',
|
||||||
|
88 => 'CURLE_CHUNK_FAILED.',
|
||||||
|
|
||||||
|
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -6,6 +6,13 @@ use ReflectionClass;
|
|||||||
|
|
||||||
class Enum
|
class Enum
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Message list
|
||||||
|
*
|
||||||
|
* @var array<string<static>>
|
||||||
|
*/
|
||||||
|
public static $messages = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Caches reflections of enum subclasses.
|
* Caches reflections of enum subclasses.
|
||||||
*
|
*
|
||||||
@@ -47,4 +54,18 @@ class Enum
|
|||||||
{
|
{
|
||||||
return array_values(static::getReflection()->getConstants());
|
return array_values(static::getReflection()->getConstants());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a message from the enum subclass
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function getMessage(int $messageIndex, string $defaultMessage = 'Unknown'): string
|
||||||
|
{
|
||||||
|
if (array_key_exists($messageIndex, self::$messages) === true) {
|
||||||
|
return self::$messages[$messageIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $defaultMessage;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,24 +12,6 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
|
|||||||
|
|
||||||
class Handler extends ExceptionHandler
|
class Handler extends ExceptionHandler
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* A list of exception types with their corresponding custom log levels.
|
|
||||||
*
|
|
||||||
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
|
|
||||||
*/
|
|
||||||
protected $levels = [
|
|
||||||
//
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A list of the exception types that are not reported.
|
|
||||||
*
|
|
||||||
* @var array<int, class-string<\Throwable>>
|
|
||||||
*/
|
|
||||||
protected $dontReport = [
|
|
||||||
//
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A list of the inputs that are never flashed to the session on validation exceptions.
|
* A list of the inputs that are never flashed to the session on validation exceptions.
|
||||||
*
|
*
|
||||||
@@ -44,10 +26,8 @@ class Handler extends ExceptionHandler
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the exception handling callbacks for the application.
|
* Register the exception handling callbacks for the application.
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function register()
|
public function register(): void
|
||||||
{
|
{
|
||||||
// $this->renderable(function (HttpException $e, $request) {
|
// $this->renderable(function (HttpException $e, $request) {
|
||||||
// if ($request->is('api/*')) {
|
// if ($request->is('api/*')) {
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Filters;
|
|
||||||
|
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
|
|
||||||
class AuditFilter
|
|
||||||
{
|
|
||||||
// public static function filter(Collection $collection): array
|
|
||||||
// {
|
|
||||||
// $collection->transform(function ($item, $key) {
|
|
||||||
// $row = $item->toArray();
|
|
||||||
|
|
||||||
// unset($row['user_type']);
|
|
||||||
// unset($row['auditable_type']);
|
|
||||||
|
|
||||||
// if (array_key_exists('password', $row['old_values'])) {
|
|
||||||
// $row['old_values']['password'] = '###';
|
|
||||||
// }
|
|
||||||
// if (array_key_exists('password', $row['new_values'])) {
|
|
||||||
// $row['new_values']['password'] = '###';
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return $row;
|
|
||||||
// });
|
|
||||||
|
|
||||||
// return $collection->toArray();
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Filters;
|
|
||||||
|
|
||||||
use App\Models\Event;
|
|
||||||
use Carbon\Carbon;
|
|
||||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
||||||
|
|
||||||
class EventFilter extends FilterAbstract
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Class name of Model
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $class = '\App\Models\Event';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default column sorting (prefix with - for descending)
|
|
||||||
*
|
|
||||||
* @var string|array
|
|
||||||
*/
|
|
||||||
protected $defaultSort = '-start_at';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter columns for q param
|
|
||||||
*
|
|
||||||
* @var string|array
|
|
||||||
*/
|
|
||||||
protected $q = [
|
|
||||||
'_' => ['title','content'],
|
|
||||||
'location' => ['location','address'],
|
|
||||||
];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if the user can view the media model
|
|
||||||
*
|
|
||||||
* @param Event $event The event instance.
|
|
||||||
* @param mixed $user The current logged in user.
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
protected function viewable(Event $event, mixed $user)
|
|
||||||
{
|
|
||||||
return (strcasecmp($event->status, 'draft') !== 0 && $event->publish_at <= now())
|
|
||||||
|| $user?->hasPermission('admin/events') === true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine the prebuild query to limit results
|
|
||||||
*
|
|
||||||
* @param EloquentBuilder $builder The builder instance.
|
|
||||||
* @param mixed $user The current logged in user.
|
|
||||||
* @return EloquentBuilder|null
|
|
||||||
*/
|
|
||||||
protected function prebuild(Builder $builder, mixed $user)
|
|
||||||
{
|
|
||||||
if (
|
|
||||||
$user?->hasPermission('admin/events') !== true
|
|
||||||
) {
|
|
||||||
return $builder
|
|
||||||
->where('status', '!=', 'draft')
|
|
||||||
->where('publish_at', '<=', now());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,596 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Filters;
|
|
||||||
|
|
||||||
use Doctrine\DBAL\Exception;
|
|
||||||
use Doctrine\DBAL\Schema\SchemaException;
|
|
||||||
use ReflectionClass;
|
|
||||||
use RuntimeException;
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Illuminate\Database\Eloquent\Builder;
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Support\Str;
|
|
||||||
use Schema;
|
|
||||||
|
|
||||||
abstract class FilterAbstract
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The model class to filter
|
|
||||||
*
|
|
||||||
* @var mixed
|
|
||||||
*/
|
|
||||||
protected $class;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The filter request
|
|
||||||
*
|
|
||||||
* @var \Illuminate\Http\Request
|
|
||||||
*/
|
|
||||||
protected $request;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The models table
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $table = '';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Array of columns that can be filtered by the api
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $filterable = null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default column sorting (prefix with - for descending)
|
|
||||||
*
|
|
||||||
* @var string|array
|
|
||||||
*/
|
|
||||||
protected $defaultSort = 'id';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default collection result limit
|
|
||||||
*
|
|
||||||
* @var integer
|
|
||||||
*/
|
|
||||||
protected $defaultLimit = 50;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Found records from query
|
|
||||||
* @var integer
|
|
||||||
*/
|
|
||||||
protected $foundTotal = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Maximum collection result limit
|
|
||||||
*
|
|
||||||
* @var integer
|
|
||||||
*/
|
|
||||||
protected $maxLimit = 100;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Only return these attributes in the results
|
|
||||||
* (minus any excludes)
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $only = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exclude these attributes from the results
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $exclude = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter columns for q param
|
|
||||||
*
|
|
||||||
* @var string|array
|
|
||||||
*/
|
|
||||||
protected $q = [];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter constructor.
|
|
||||||
*
|
|
||||||
* @param \Illuminate\Http\Request $request Request object.
|
|
||||||
*/
|
|
||||||
public function __construct(Request $request)
|
|
||||||
{
|
|
||||||
$this->request = $request;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Only include the specified attributes in the results.
|
|
||||||
*
|
|
||||||
* @param string|array $only Only return these attributes.
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function only(mixed $only)
|
|
||||||
{
|
|
||||||
if (is_array($only) === true) {
|
|
||||||
$this->only = $only;
|
|
||||||
} else {
|
|
||||||
$this->only = [$only];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exclude the specified attributes in the results.
|
|
||||||
*
|
|
||||||
* @param string|array $exclude Attributes to exclude.
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function exclude(mixed $exclude)
|
|
||||||
{
|
|
||||||
if (is_array($exclude) === true) {
|
|
||||||
$this->exclude = $exclude;
|
|
||||||
} else {
|
|
||||||
$this->exclude = [$exclude];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if the model is viewable by the user
|
|
||||||
*
|
|
||||||
* @param mixed $model Model instance.
|
|
||||||
* @param mixed $user Current user.
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
// protected function viewable(mixed $model, mixed $user)
|
|
||||||
// {
|
|
||||||
// return true;
|
|
||||||
// }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prepend action to the builder to limit the results
|
|
||||||
*
|
|
||||||
* @param Builder $builder Builder instance.
|
|
||||||
* @param mixed $user Current user.
|
|
||||||
* @return Builder|null
|
|
||||||
*/
|
|
||||||
// protected function prebuild(Builder $builder, mixed $user)
|
|
||||||
// {
|
|
||||||
// return $builder;
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return an array of attributes visible in the results
|
|
||||||
*
|
|
||||||
* @param array $attributes Attributes currently visible.
|
|
||||||
* @param User|null $user Current logged in user or null.
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
protected function seeAttributes(array $attributes, mixed $user)
|
|
||||||
{
|
|
||||||
return $attributes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Apply all the requested filters if available.
|
|
||||||
*
|
|
||||||
* @param Model $model Model object to filter. If null create query.
|
|
||||||
* @return Builder|Model
|
|
||||||
*/
|
|
||||||
public function filter(Model $model = null)
|
|
||||||
{
|
|
||||||
$this->foundTotal = 0;
|
|
||||||
|
|
||||||
$builder = $this->class::query();
|
|
||||||
|
|
||||||
/* Get the related model */
|
|
||||||
$classModel = $model;
|
|
||||||
if ($model === null) {
|
|
||||||
$classModel = $builder->getModel();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Get table name */
|
|
||||||
if ($this->table === '') {
|
|
||||||
if ($model === null) {
|
|
||||||
$this->table = $classModel->getTable();
|
|
||||||
} else {
|
|
||||||
$this->table = $model->getTable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Run query prebuilder or viewable */
|
|
||||||
if ($model === null) {
|
|
||||||
if (method_exists($this, 'prebuild') === true) {
|
|
||||||
$prebuilder = $this->prebuild($builder, $this->request->user());
|
|
||||||
if ($prebuilder instanceof Builder) {
|
|
||||||
$builder = $prebuilder;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (method_exists($this, 'viewable') === true) {
|
|
||||||
if ($this->viewable($model, $this->request->user()) === false) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Get attributes from table or use 'only' */
|
|
||||||
$attributes = [];
|
|
||||||
if (is_array($this->only) === true && count($this->only) > 0) {
|
|
||||||
$attributes = $this->only;
|
|
||||||
} else {
|
|
||||||
$attributes = Schema::getColumnListing($this->table);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Run attribute modifiers*/
|
|
||||||
$modifiedAttribs = $this->seeAttributes($attributes, $this->request->user());
|
|
||||||
if (is_array($modifiedAttribs) === true) {
|
|
||||||
$attributes = $modifiedAttribs;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($attributes as $key => $column) {
|
|
||||||
$method = 'see' . Str::studly($column) . 'Attribute';
|
|
||||||
if (
|
|
||||||
method_exists($this, $method) === true &&
|
|
||||||
$this->$method($this->request->user()) === false
|
|
||||||
) {
|
|
||||||
unset($attributes[$key]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_array($this->exclude) === true && count($this->exclude) > 0) {
|
|
||||||
$attributes = array_diff($attributes, $this->exclude);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Setup attributes and appends */
|
|
||||||
// $attributesAppends = array_merge($attributes, $classModel->getAppends());
|
|
||||||
|
|
||||||
/* Apply ?fields= request to attributes */
|
|
||||||
if ($this->request->has('fields') === true) {
|
|
||||||
$attributes = array_intersect($attributes, explode(',', $this->request->fields));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Hide remaining attributes in model (if present) and return */
|
|
||||||
if ($model !== null) {
|
|
||||||
// TODO: Also show $this->request->fields that are appends
|
|
||||||
|
|
||||||
$model->makeHidden(array_diff(Schema::getColumnListing($this->table), $attributes));
|
|
||||||
return $model;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Are there attributes left? */
|
|
||||||
if (count($attributes) === 0) {
|
|
||||||
$this->foundTotal = 0;
|
|
||||||
return new Collection();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* apply select! */
|
|
||||||
$builder->select($attributes);
|
|
||||||
|
|
||||||
/* Setup filterables if not present */
|
|
||||||
if ($this->filterable === null) {
|
|
||||||
$this->filterable = $attributes;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Filter values */
|
|
||||||
$filterRequest = array_filter($this->request->only(array_intersect($attributes, $this->filterable)));
|
|
||||||
$this->builderArrayFilter($builder, $filterRequest);
|
|
||||||
|
|
||||||
if (is_array($this->q) === true && count($this->q) > 0) {
|
|
||||||
$qQueries = [];
|
|
||||||
foreach ($this->q as $key => $value) {
|
|
||||||
if (is_array($value) === true) {
|
|
||||||
$qKey = $key === '_' ? '' : $key;
|
|
||||||
foreach ($value as $subvalue) {
|
|
||||||
$qQueries[$key][$subvalue] = $this->request->get("q" . $qKey);
|
|
||||||
}
|
|
||||||
} elseif ($this->request->has("q") === true) {
|
|
||||||
$qQueries['_'][$value] = $this->request->get("q");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($qQueries as $key => $value) {
|
|
||||||
$builder->where(function ($query) use ($value) {
|
|
||||||
$this->builderArrayFilter($query, $value, 'or');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}//end if
|
|
||||||
|
|
||||||
/* Apply sorting */
|
|
||||||
$sortList = $this->defaultSort;
|
|
||||||
if ($this->request->has('sort') === true) {
|
|
||||||
$sortList = explode(',', $this->request->sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Transform sort list to array */
|
|
||||||
if (is_array($sortList) === false) {
|
|
||||||
if (strlen($sortList) > 0) {
|
|
||||||
$sortList = [$sortList];
|
|
||||||
} else {
|
|
||||||
$sortList = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Remove non-viewable attributes from sort list */
|
|
||||||
if (count($sortList) > 0) {
|
|
||||||
$sortList = array_filter($sortList, function ($item) use ($attributes) {
|
|
||||||
$parsedItem = $item;
|
|
||||||
if (substr($parsedItem, 0, 1) === '-') {
|
|
||||||
$parsedItem = substr($parsedItem, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return in_array($parsedItem, $attributes);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Do we have any sort element left? */
|
|
||||||
if (count($sortList) > 0) {
|
|
||||||
foreach ($sortList as $sortAttribute) {
|
|
||||||
$prefix = substr($sortAttribute, 0, 1);
|
|
||||||
$direction = 'asc';
|
|
||||||
|
|
||||||
if (in_array($prefix, ['-', '+']) === true) {
|
|
||||||
$sortAttribute = substr($sortAttribute, 1);
|
|
||||||
if ($prefix === '-') {
|
|
||||||
$direction = 'desc';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$builder->orderBy($sortAttribute, $direction);
|
|
||||||
}//end foreach
|
|
||||||
}//end if
|
|
||||||
|
|
||||||
/* save found count */
|
|
||||||
$this->foundTotal = $builder->count();
|
|
||||||
|
|
||||||
/* Apply result limit */
|
|
||||||
$limit = $this->defaultLimit;
|
|
||||||
if ($this->request->has('limit') === true) {
|
|
||||||
$limit = intval($this->request->limit);
|
|
||||||
}
|
|
||||||
if ($limit < 1) {
|
|
||||||
$limit = 1;
|
|
||||||
}
|
|
||||||
if ($limit > $this->maxLimit && $this->maxLimit !== 0) {
|
|
||||||
$limit = $this->maxLimit;
|
|
||||||
}
|
|
||||||
|
|
||||||
$builder->limit($limit);
|
|
||||||
|
|
||||||
/* Apply page offset */
|
|
||||||
if ($this->request->has('page') === true) {
|
|
||||||
$page = intval($this->request->page);
|
|
||||||
if ($page < 1) {
|
|
||||||
$page = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
$builder->offset((intval($this->request->page) - 1) * $limit);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* run spot run */
|
|
||||||
$collection = $builder->get();
|
|
||||||
|
|
||||||
return $collection;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter content based on the filterRequest
|
|
||||||
* @param mixed $builder Builder object
|
|
||||||
* @param array $filterRequest Filter key/value
|
|
||||||
* @param string $defaultBoolean Default where boolean
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
protected function builderArrayFilter(mixed $builder, array $filterRequest, string $defaultBoolean = 'and')
|
|
||||||
{
|
|
||||||
foreach ($filterRequest as $filterAttribute => $filterValue) {
|
|
||||||
$tags = [];
|
|
||||||
$boolean = $defaultBoolean;
|
|
||||||
|
|
||||||
$matches = preg_split('/(?<!\\\\)"/', $filterValue, -1, PREG_SPLIT_OFFSET_CAPTURE);
|
|
||||||
foreach ($matches as $idx => $match_info) {
|
|
||||||
if (($idx % 2) === true) {
|
|
||||||
if (substr($filterValue, ($match_info[1] - 2), 1) === ',') {
|
|
||||||
$tags[] = ['operator' => '', 'tag' => stripslashes(trim($match_info[0]))];
|
|
||||||
} else {
|
|
||||||
$tags[(count($tags) - 1)]['tag'] .= stripslashes(trim($match_info[0]));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$innerTags = [$match_info[0]];
|
|
||||||
if (strpos($match_info[0], ',') !== false) {
|
|
||||||
$innerTags = preg_split('/(?<!\\\\),/', $match_info[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($innerTags as $tag) {
|
|
||||||
$tag = stripslashes(trim($tag));
|
|
||||||
if (strlen($tag) > 0) {
|
|
||||||
$operator = '=';
|
|
||||||
|
|
||||||
$single = substr($tag, 0, 1);
|
|
||||||
$double = substr($tag . ' ', 0, 2); // add empty space incase len $tag < 2
|
|
||||||
|
|
||||||
// check for operators at start
|
|
||||||
if (in_array($double, ['!=', '<>', '><', '>=', '<=', '=>', '=<']) === true) {
|
|
||||||
if ($double === '<>' || $double === '><') {
|
|
||||||
$double = '!=';
|
|
||||||
} elseif ($double === '=>') {
|
|
||||||
$double = '>=';
|
|
||||||
} elseif ($double === '=<') {
|
|
||||||
$double == '>=';
|
|
||||||
}
|
|
||||||
|
|
||||||
$operator = $double;
|
|
||||||
$tag = substr($tag, 2);
|
|
||||||
} else {
|
|
||||||
if (in_array($single, ['=', '!', '>', '<', '~', '%']) === true) {
|
|
||||||
if ($single === '=') {
|
|
||||||
$single = '=='; // a single '=' is actually a double '=='
|
|
||||||
}
|
|
||||||
|
|
||||||
$operator = $single;
|
|
||||||
$tag = substr($tag, 1);
|
|
||||||
}
|
|
||||||
}//end if
|
|
||||||
|
|
||||||
$tags[] = ['operator' => $operator, 'tag' => $tag];
|
|
||||||
}//end if
|
|
||||||
}//end foreach
|
|
||||||
}//end if
|
|
||||||
}//end foreach
|
|
||||||
|
|
||||||
if (count($tags) > 1) {
|
|
||||||
$boolean = 'or';
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($tags as $tag_data) {
|
|
||||||
$operator = $tag_data['operator'];
|
|
||||||
$value = $tag_data['tag'];
|
|
||||||
$table = $this->table;
|
|
||||||
$column = $filterAttribute;
|
|
||||||
|
|
||||||
if (($dotPos = strpos($filterAttribute, '.')) !== false) {
|
|
||||||
$table = substr($filterAttribute, 0, $dotPos);
|
|
||||||
$column = substr($filterAttribute, ($dotPos + 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
$columnType = DB::getSchemaBuilder()->getColumnType($table, $column);
|
|
||||||
|
|
||||||
if (
|
|
||||||
in_array($columnType, ['tinyint', 'smallint', 'mediumint', 'int', 'integer', 'bigint',
|
|
||||||
'decimal', 'float', 'double', 'real', 'double precision'
|
|
||||||
]) === true
|
|
||||||
) {
|
|
||||||
if (in_array($operator, ['=', '>', '<', '>=', '<=', '%', '!']) === false) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$columnType = 'numeric';
|
|
||||||
} elseif (in_array($columnType, ['date', 'time', 'datetime', 'timestamp', 'year']) === true) {
|
|
||||||
if (in_array($operator, ['=', '>', '<', '>=', '<=', '!']) === false) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$columnType = 'datetime';
|
|
||||||
} elseif (
|
|
||||||
in_array($columnType, ['string', 'char', 'varchar', 'timeblob', 'blob', 'mediumblob',
|
|
||||||
'longblob', 'tinytext', 'text', 'mediumtext', 'longtext', 'enum'
|
|
||||||
]) === true
|
|
||||||
) {
|
|
||||||
if (in_array($operator, ['=', '==', '!', '!=', '~']) === false) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
$columnType = 'text';
|
|
||||||
|
|
||||||
if ($value === "''" || $value === '""') {
|
|
||||||
$value = '';
|
|
||||||
} elseif (strcasecmp($value, 'null') !== 0) {
|
|
||||||
if ($operator === '!') {
|
|
||||||
$operator = 'NOT LIKE';
|
|
||||||
$value = '%' . $value . '%';
|
|
||||||
} elseif ($operator === '=') {
|
|
||||||
$operator = 'LIKE';
|
|
||||||
$value = '%' . $value . '%';
|
|
||||||
} elseif ($operator === '~') {
|
|
||||||
$operator = 'SOUNDS LIKE';
|
|
||||||
} elseif ($operator === '==') {
|
|
||||||
$operator = '=';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} elseif ($columnType === 'boolean') {
|
|
||||||
if (in_array($operator, ['=', '!']) === false) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strtolower($value) === 'true') {
|
|
||||||
$value = 1;
|
|
||||||
} elseif (strtolower($value) === 'false') {
|
|
||||||
$value = 0;
|
|
||||||
}
|
|
||||||
}//end if
|
|
||||||
|
|
||||||
$betweenSeperator = strpos($value, '<>');
|
|
||||||
if (
|
|
||||||
$operator === '=' && $betweenSeperator !== false && in_array($columnType, ['numeric',
|
|
||||||
'datetime'
|
|
||||||
]) === true
|
|
||||||
) {
|
|
||||||
$value = explode('<>', $value);
|
|
||||||
$operator = '<>';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($operator !== '') {
|
|
||||||
$this->builderWhere($builder, $table, $column, $operator, $value, $boolean);
|
|
||||||
}
|
|
||||||
}//end foreach
|
|
||||||
}//end foreach
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Insert a where statement into the builder, taking the filter map into consideration
|
|
||||||
*
|
|
||||||
* @param Builder $builder Builder instance.
|
|
||||||
* @param string $table Table name.
|
|
||||||
* @param string $column Column name.
|
|
||||||
* @param string $operator Where operator.
|
|
||||||
* @param mixed $value Value to test.
|
|
||||||
* @param string $boolean Use Or comparison.
|
|
||||||
* @return void
|
|
||||||
* @throws RuntimeException Error applying statement.
|
|
||||||
* @throws InvalidArgumentException Error applying statement.
|
|
||||||
*/
|
|
||||||
protected function builderWhere(
|
|
||||||
Builder &$builder,
|
|
||||||
string $table,
|
|
||||||
string $column,
|
|
||||||
string $operator,
|
|
||||||
mixed $value,
|
|
||||||
string $boolean
|
|
||||||
) {
|
|
||||||
if (
|
|
||||||
(is_string($value) === true && $operator !== '<>') || (is_array($value) === true && count($value) === 2 &&
|
|
||||||
$operator === '<>')
|
|
||||||
) {
|
|
||||||
if ($table !== '' && $table !== $this->table) {
|
|
||||||
$builder->whereHas($table, function ($query) use ($column, $operator, $value, $boolean) {
|
|
||||||
if ($operator !== '<>') {
|
|
||||||
if (strcasecmp($value, 'null') === 0) {
|
|
||||||
if ($operator === '!') {
|
|
||||||
$query->whereNotNull($column, $boolean);
|
|
||||||
} else {
|
|
||||||
$query->whereNull($column, $boolean);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$query->where($column, $operator, $value, $boolean);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$query->whereBetween($column, $value, $boolean);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
if ($operator !== '<>') {
|
|
||||||
if (strcasecmp($value, 'null') === 0) {
|
|
||||||
if ($operator === '!') {
|
|
||||||
$builder->whereNotNull($column, $boolean);
|
|
||||||
} else {
|
|
||||||
$builder->whereNull($column, $boolean);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$builder->where($column, $operator, $value, $boolean);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$builder->whereBetween($column, $value, $boolean);
|
|
||||||
}
|
|
||||||
}//end if
|
|
||||||
}//end if
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the found total of items
|
|
||||||
* @return integer
|
|
||||||
*/
|
|
||||||
public function foundTotal()
|
|
||||||
{
|
|
||||||
return $this->foundTotal;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Filters;
|
|
||||||
|
|
||||||
use App\Models\Media;
|
|
||||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
||||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
|
||||||
|
|
||||||
class MediaFilter extends FilterAbstract
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Class name of Model
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $class = '\App\Models\Media';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if the user can view the media model
|
|
||||||
*
|
|
||||||
* @param Media $media The media instance.
|
|
||||||
* @param mixed $user The current logged in user.
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
protected function viewable(Media $media, mixed $user)
|
|
||||||
{
|
|
||||||
if (empty($media->permission) === false) {
|
|
||||||
return ($user?->hasPermission('admin/media') || $user?->hasPermission($media->permission));
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine the prebuild query to limit results
|
|
||||||
*
|
|
||||||
* @param EloquentBuilder $builder The builder instance.
|
|
||||||
* @param mixed $user The current logged in user.
|
|
||||||
* @return EloquentBuilder|null
|
|
||||||
*/
|
|
||||||
protected function prebuild(Builder $builder, mixed $user)
|
|
||||||
{
|
|
||||||
if ($user === null) {
|
|
||||||
return $builder->whereNull('permission');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Show the permission attribute in the results
|
|
||||||
*
|
|
||||||
* @param User|null $user Current logged in user or null.
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
protected function seePermissionAttribute(mixed $user)
|
|
||||||
{
|
|
||||||
return ($user?->hasPermission('admin/media'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Filters;
|
|
||||||
|
|
||||||
use App\Models\Post;
|
|
||||||
use Carbon\Carbon;
|
|
||||||
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
||||||
|
|
||||||
class PostFilter extends FilterAbstract
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Class name of Model
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $class = '\App\Models\Post';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default column sorting (prefix with - for descending)
|
|
||||||
*
|
|
||||||
* @var string|array
|
|
||||||
*/
|
|
||||||
protected $defaultSort = '-publish_at';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if the user can view the media model
|
|
||||||
*
|
|
||||||
* @param Post $post The post instance.
|
|
||||||
* @param mixed $user The current logged in user.
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
protected function viewable(Post $post, mixed $user)
|
|
||||||
{
|
|
||||||
if ($user?->hasPermission('admin/posts') !== true) {
|
|
||||||
return ($post->publish_at <= now());
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine the prebuild query to limit results
|
|
||||||
*
|
|
||||||
* @param EloquentBuilder $builder The builder instance.
|
|
||||||
* @param mixed $user The current logged in user.
|
|
||||||
* @return EloquentBuilder|null
|
|
||||||
*/
|
|
||||||
protected function prebuild(Builder $builder, mixed $user)
|
|
||||||
{
|
|
||||||
if ($user?->hasPermission('admin/posts') !== true) {
|
|
||||||
return $builder->where('publish_at', '<=', Carbon::now());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,7 +11,7 @@ class SubscriptionFilter extends FilterAbstract
|
|||||||
*
|
*
|
||||||
* @var mixed
|
* @var mixed
|
||||||
*/
|
*/
|
||||||
protected $class = '\App\Models\Subscription';
|
protected $class = \App\Models\Subscription::class;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Filters;
|
|
||||||
|
|
||||||
use App\Models\User;
|
|
||||||
|
|
||||||
class UserFilter extends FilterAbstract
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The model class to filter
|
|
||||||
*
|
|
||||||
* @var mixed
|
|
||||||
*/
|
|
||||||
protected $class = '\App\Models\User';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return an array of attributes visible in the results
|
|
||||||
*
|
|
||||||
* @param array $attributes Attributes currently visible.
|
|
||||||
* @param User|null $user Current logged in user or null.
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
protected function seeAttributes(array $attributes, mixed $user)
|
|
||||||
{
|
|
||||||
if ($user?->hasPermission('admin/users') !== true) {
|
|
||||||
return ['id', 'username'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
40
app/Helpers/Array.php
Normal file
40
app/Helpers/Array.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/* Array Helper Functions */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove an item from an array.
|
||||||
|
*
|
||||||
|
* @param array $arr The array to check.
|
||||||
|
* @param string|array $item The item or items to remove.
|
||||||
|
* @return array The filtered array.
|
||||||
|
*/
|
||||||
|
function arrayRemoveItem(array $arr, string|array $item): array
|
||||||
|
{
|
||||||
|
$filteredArr = $arr;
|
||||||
|
|
||||||
|
if (is_string($item) === true) {
|
||||||
|
$item = [$item];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($item as $str) {
|
||||||
|
$filteredArr = array_filter($arr, function ($item) use ($str) {
|
||||||
|
return $item !== $str;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return $filteredArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array with specified the keys
|
||||||
|
*
|
||||||
|
* @param array $arr The array to filter.
|
||||||
|
* @param string|array $keys The keys to keep.
|
||||||
|
* @return array The filtered array.
|
||||||
|
*/
|
||||||
|
function arrayLimitKeys(array $arr, array $keys): array
|
||||||
|
{
|
||||||
|
return array_intersect_key($arr, array_flip($keys));
|
||||||
|
}
|
||||||
19
app/Helpers/Temp.php
Normal file
19
app/Helpers/Temp.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/* Temp File Helper Functions */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a temporary file path.
|
||||||
|
*
|
||||||
|
* @return str The filtered array.
|
||||||
|
*/
|
||||||
|
function generateTempFilePath(): string
|
||||||
|
{
|
||||||
|
$temporaryDir = storage_path('app/tmp');
|
||||||
|
if (is_dir($temporaryDir) === false) {
|
||||||
|
mkdir($temporaryDir, 0777, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $temporaryDir . DIRECTORY_SEPARATOR . uniqid('upload_', true);
|
||||||
|
}
|
||||||
163
app/Http/Controllers/Api/AnalyticsController.php
Normal file
163
app/Http/Controllers/Api/AnalyticsController.php
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Conductors\AnalyticsConductor;
|
||||||
|
use App\Conductors\Conductor;
|
||||||
|
use App\Enum\HttpResponseCodes;
|
||||||
|
use App\Http\Requests\AnalyticsRequest;
|
||||||
|
use App\Models\Media;
|
||||||
|
use App\Models\Analytics;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Carbon\Exceptions\InvalidFormatException;
|
||||||
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||||
|
use Illuminate\Database\Eloquent\InvalidCastException;
|
||||||
|
use Illuminate\Database\Eloquent\MassAssignmentException;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class AnalyticsController extends ApiController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* AnalyticsController constructor.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->middleware('auth:sanctum')
|
||||||
|
->only([
|
||||||
|
'index',
|
||||||
|
'update',
|
||||||
|
'delete'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display a listing of the resource.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
if ($request->user() !== null && $request->user()?->hasPermission('admin/analytics') === true) {
|
||||||
|
$searchFields = ['attribute', 'type', 'useragent', 'ip'];
|
||||||
|
|
||||||
|
$queryRequest = new Request();
|
||||||
|
$queryRequest->merge($request->only($searchFields));
|
||||||
|
foreach ($searchFields as $field) {
|
||||||
|
unset($request[$field]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$query = Analytics::query()
|
||||||
|
->selectRaw('session,
|
||||||
|
MIN(created_at) as created_at,
|
||||||
|
TIMESTAMPDIFF(MINUTE, MIN(created_at), MAX(created_at)) as duration');
|
||||||
|
$query = Conductor::filterQuery($query, $queryRequest);
|
||||||
|
|
||||||
|
list($collection, $total) = AnalyticsConductor::collection($request, $query
|
||||||
|
->groupBy('session')
|
||||||
|
->get());
|
||||||
|
|
||||||
|
return $this->respondAsResource(
|
||||||
|
$collection,
|
||||||
|
['isCollection' => true,
|
||||||
|
'appendData' => ['total' => $total]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
|
* @param \App\Models\Analytics $analytics The analyics model.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function show(Request $request, int $session)
|
||||||
|
{
|
||||||
|
if ($request->user() !== null && $request->user()?->hasPermission('admin/analytics') === true) {
|
||||||
|
list($collection, $total) = AnalyticsConductor::collection($request, Analytics::query()
|
||||||
|
->where('session', $session)
|
||||||
|
->get());
|
||||||
|
|
||||||
|
return $this->respondAsResource(
|
||||||
|
$collection,
|
||||||
|
['isCollection' => true,
|
||||||
|
'appendData' => ['total' => $total]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a newly created resource in storage.
|
||||||
|
*
|
||||||
|
* @param \App\Http\Requests\AnalyticsRequest $request The user request.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function store(AnalyticsRequest $request)
|
||||||
|
{
|
||||||
|
if (AnalyticsConductor::creatable() === true) {
|
||||||
|
$analytics = null;
|
||||||
|
$user = $request->user();
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'type' => $request->input('type'),
|
||||||
|
'attribute' => $request->input('attribute', ''),
|
||||||
|
'useragent' => $request->userAgent(),
|
||||||
|
'ip' => $request->ip()
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($user !== null && $user->hasPermission('admin/analytics') === true && $request->has('session') === true) {
|
||||||
|
$data['session'] = $request->input('session');
|
||||||
|
$analytics = Analytics::create($data);
|
||||||
|
} else {
|
||||||
|
$analytics = Analytics::createWithSession($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondAsResource(
|
||||||
|
AnalyticsConductor::model($request, $analytics),
|
||||||
|
['respondCode' => HttpResponseCodes::HTTP_CREATED]
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}//end if
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the specified resource in storage.
|
||||||
|
*
|
||||||
|
* @param \App\Http\Requests\AnalyticsRequest $request The analytics update request.
|
||||||
|
* @param \App\Models\Analytics $analytics The specified analytics.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function update(AnalyticsRequest $request, Analytics $analytics)
|
||||||
|
{
|
||||||
|
if (AnalyticsConductor::updatable($analytics) === true) {
|
||||||
|
$analytics->update($request->all());
|
||||||
|
return $this->respondAsResource(AnalyticsConductor::model($request, $analytics));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the specified resource from storage.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Analytics $analytics The specified analytics.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function destroy(Analytics $analytics)
|
||||||
|
{
|
||||||
|
if (AnalyticsConductor::destroyable($analytics) === true) {
|
||||||
|
$analytics->delete();
|
||||||
|
return $this->respondNoContent();
|
||||||
|
} else {
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
use App\Enum\HttpResponseCodes;
|
use App\Enum\HttpResponseCodes;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
@@ -23,9 +24,8 @@ class ApiController extends Controller
|
|||||||
* @param array $data Response data.
|
* @param array $data Response data.
|
||||||
* @param integer $respondCode Response status code.
|
* @param integer $respondCode Response status code.
|
||||||
* @param array $headers Response headers.
|
* @param array $headers Response headers.
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function respondJson(array $data, int $respondCode = HttpResponseCodes::HTTP_OK, array $headers = [])
|
public function respondJson(array $data, int $respondCode = HttpResponseCodes::HTTP_OK, array $headers = []): JsonResponse
|
||||||
{
|
{
|
||||||
return response()->json($data, $respondCode, $headers);
|
return response()->json($data, $respondCode, $headers);
|
||||||
}
|
}
|
||||||
@@ -34,9 +34,8 @@ class ApiController extends Controller
|
|||||||
* Return forbidden message
|
* Return forbidden message
|
||||||
*
|
*
|
||||||
* @param string $message Response message.
|
* @param string $message Response message.
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function respondForbidden(string $message = 'You do not have permission to access the resource.')
|
public function respondForbidden(string $message = 'You do not have permission to access the resource.'): JsonResponse
|
||||||
{
|
{
|
||||||
return response()->json(['message' => $message], HttpResponseCodes::HTTP_FORBIDDEN);
|
return response()->json(['message' => $message], HttpResponseCodes::HTTP_FORBIDDEN);
|
||||||
}
|
}
|
||||||
@@ -45,9 +44,8 @@ class ApiController extends Controller
|
|||||||
* Return forbidden message
|
* Return forbidden message
|
||||||
*
|
*
|
||||||
* @param string $message Response message.
|
* @param string $message Response message.
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function respondNotFound(string $message = 'The resource was not found.')
|
public function respondNotFound(string $message = 'The resource was not found.'): JsonResponse
|
||||||
{
|
{
|
||||||
return response()->json(['message' => $message], HttpResponseCodes::HTTP_NOT_FOUND);
|
return response()->json(['message' => $message], HttpResponseCodes::HTTP_NOT_FOUND);
|
||||||
}
|
}
|
||||||
@@ -56,39 +54,43 @@ class ApiController extends Controller
|
|||||||
* Return too large message
|
* Return too large message
|
||||||
*
|
*
|
||||||
* @param string $message Response message.
|
* @param string $message Response message.
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function respondTooLarge(string $message = 'The request entity is too large.')
|
public function respondTooLarge(string $message = 'The request entity is too large.'): JsonResponse
|
||||||
{
|
{
|
||||||
return response()->json(['message' => $message], HttpResponseCodes::HTTP_REQUEST_ENTITY_TOO_LARGE);
|
return response()->json(['message' => $message], HttpResponseCodes::HTTP_REQUEST_ENTITY_TOO_LARGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return no content
|
* Return no content
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function respondNoContent()
|
public function respondNoContent(): JsonResponse
|
||||||
{
|
{
|
||||||
return response()->json([], HttpResponseCodes::HTTP_NO_CONTENT);
|
return response()->json([], HttpResponseCodes::HTTP_NO_CONTENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return created
|
* Return created
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function respondCreated()
|
public function respondCreated(): JsonResponse
|
||||||
{
|
{
|
||||||
return response()->json([], HttpResponseCodes::HTTP_CREATED);
|
return response()->json([], HttpResponseCodes::HTTP_CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return accepted
|
||||||
|
*/
|
||||||
|
public function respondAccepted(): JsonResponse
|
||||||
|
{
|
||||||
|
return response()->json([], HttpResponseCodes::HTTP_ACCEPTED);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return single error message
|
* Return single error message
|
||||||
*
|
*
|
||||||
* @param string $message Error message.
|
* @param string $message Error message.
|
||||||
* @param integer $responseCode Resource code.
|
* @param integer $responseCode Resource code.
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function respondError(string $message, int $responseCode = HttpResponseCodes::HTTP_UNPROCESSABLE_ENTITY)
|
public function respondError(string $message, int $responseCode = HttpResponseCodes::HTTP_UNPROCESSABLE_ENTITY): JsonResponse
|
||||||
{
|
{
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => $message
|
'message' => $message
|
||||||
@@ -100,9 +102,8 @@ class ApiController extends Controller
|
|||||||
*
|
*
|
||||||
* @param array $errors Error messages.
|
* @param array $errors Error messages.
|
||||||
* @param integer $responseCode Resource code.
|
* @param integer $responseCode Resource code.
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function respondWithErrors(array $errors, int $responseCode = HttpResponseCodes::HTTP_UNPROCESSABLE_ENTITY)
|
public function respondWithErrors(array $errors, int $responseCode = HttpResponseCodes::HTTP_UNPROCESSABLE_ENTITY): JsonResponse
|
||||||
{
|
{
|
||||||
$keys = array_keys($errors);
|
$keys = array_keys($errors);
|
||||||
$error = $errors[$keys[0]];
|
$error = $errors[$keys[0]];
|
||||||
@@ -122,30 +123,40 @@ class ApiController extends Controller
|
|||||||
* Return resource data
|
* Return resource data
|
||||||
*
|
*
|
||||||
* @param array|Model|Collection $data Resource data.
|
* @param array|Model|Collection $data Resource data.
|
||||||
* @param array|null $appendData Data to append to response.
|
* @param array $options Respond options.
|
||||||
* @param integer $respondCode Resource code.
|
|
||||||
* @return \Illuminate\Http\JsonResponse
|
|
||||||
*/
|
*/
|
||||||
protected function respondAsResource(
|
protected function respondAsResource(
|
||||||
mixed $data,
|
mixed $data,
|
||||||
mixed $appendData = null,
|
array $options = [],
|
||||||
int $respondCode = HttpResponseCodes::HTTP_OK
|
$validationFn = null
|
||||||
) {
|
): JsonResponse {
|
||||||
|
$isCollection = $options['isCollection'] ?? false;
|
||||||
|
$appendData = $options['appendData'] ?? null;
|
||||||
|
$resourceName = $options['resourceName'] ?? null;
|
||||||
|
$respondCode = ($options['respondCode'] ?? HttpResponseCodes::HTTP_OK);
|
||||||
|
|
||||||
if ($data === null || ($data instanceof Collection && $data->count() === 0)) {
|
if ($data === null || ($data instanceof Collection && $data->count() === 0)) {
|
||||||
return $this->respondNotFound();
|
$validationData = [];
|
||||||
|
if (array_key_exists('appendData', $options) === true) {
|
||||||
|
$validationData = $options['appendData'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$resourceName = $this->resourceName;
|
if ($validationFn === null || $validationFn($validationData) === true) {
|
||||||
|
return $this->respondNotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->resourceName === '') {
|
if (is_null($resourceName) === true || empty($resourceName) === true) {
|
||||||
|
$resourceName = $this->resourceName;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_null($resourceName) === true || empty($resourceName) === true) {
|
||||||
$resourceName = get_class($this);
|
$resourceName = get_class($this);
|
||||||
$resourceName = substr($resourceName, (strrpos($resourceName, '\\') + 1));
|
$resourceName = substr($resourceName, (strrpos($resourceName, '\\') + 1));
|
||||||
$resourceName = substr($resourceName, 0, strpos($resourceName, 'Controller'));
|
$resourceName = substr($resourceName, 0, strpos($resourceName, 'Controller'));
|
||||||
$resourceName = strtolower($resourceName);
|
$resourceName = strtolower($resourceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
$is_multiple = true;
|
|
||||||
|
|
||||||
$dataArray = [];
|
$dataArray = [];
|
||||||
if ($data instanceof Collection) {
|
if ($data instanceof Collection) {
|
||||||
$dataArray = $data->toArray();
|
$dataArray = $data->toArray();
|
||||||
@@ -157,7 +168,7 @@ class ApiController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$resource = [];
|
$resource = [];
|
||||||
if ($is_multiple === true) {
|
if ($isCollection === true) {
|
||||||
$resource = [Str::plural($resourceName) => $dataArray];
|
$resource = [Str::plural($resourceName) => $dataArray];
|
||||||
} else {
|
} else {
|
||||||
$resource = [Str::singular($resourceName) => $dataArray];
|
$resource = [Str::singular($resourceName) => $dataArray];
|
||||||
|
|||||||
245
app/Http/Controllers/Api/ArticleController.php
Normal file
245
app/Http/Controllers/Api/ArticleController.php
Normal file
@@ -0,0 +1,245 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Conductors\MediaConductor;
|
||||||
|
use App\Conductors\ArticleConductor;
|
||||||
|
use App\Enum\HttpResponseCodes;
|
||||||
|
use App\Http\Requests\ArticleRequest;
|
||||||
|
use App\Models\Media;
|
||||||
|
use App\Models\Article;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Carbon\Exceptions\InvalidFormatException;
|
||||||
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||||
|
use Illuminate\Database\Eloquent\InvalidCastException;
|
||||||
|
use Illuminate\Database\Eloquent\MassAssignmentException;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class ArticleController extends ApiController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* ApplicationController constructor.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->middleware('auth:sanctum')
|
||||||
|
->only([
|
||||||
|
'store',
|
||||||
|
'update',
|
||||||
|
'delete'
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display a listing of the resource.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
list($collection, $total) = ArticleConductor::request($request);
|
||||||
|
|
||||||
|
return $this->respondAsResource(
|
||||||
|
$collection,
|
||||||
|
['isCollection' => true,
|
||||||
|
'appendData' => ['total' => $total]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
|
* @param \App\Models\Article $article The article model.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function show(Request $request, Article $article)
|
||||||
|
{
|
||||||
|
if (ArticleConductor::viewable($article) === true) {
|
||||||
|
return $this->respondAsResource(ArticleConductor::model($request, $article));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a newly created resource in storage.
|
||||||
|
*
|
||||||
|
* @param \App\Http\Requests\ArticleRequest $request The user request.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function store(ArticleRequest $request)
|
||||||
|
{
|
||||||
|
if (ArticleConductor::creatable() === true) {
|
||||||
|
$article = Article::create($request->all());
|
||||||
|
return $this->respondAsResource(
|
||||||
|
ArticleConductor::model($request, $article),
|
||||||
|
['respondCode' => HttpResponseCodes::HTTP_CREATED]
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the specified resource in storage.
|
||||||
|
*
|
||||||
|
* @param \App\Http\Requests\ArticleRequest $request The article update request.
|
||||||
|
* @param \App\Models\Article $article The specified article.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function update(ArticleRequest $request, Article $article)
|
||||||
|
{
|
||||||
|
if (ArticleConductor::updatable($article) === true) {
|
||||||
|
$article->update($request->all());
|
||||||
|
return $this->respondAsResource(ArticleConductor::model($request, $article));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the specified resource from storage.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Article $article The specified article.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function destroy(Article $article)
|
||||||
|
{
|
||||||
|
if (ArticleConductor::destroyable($article) === true) {
|
||||||
|
$article->delete();
|
||||||
|
return $this->respondNoContent();
|
||||||
|
} else {
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of attachments related to this model.
|
||||||
|
*
|
||||||
|
* @param Request $request The user request.
|
||||||
|
* @param Article $article The article model.
|
||||||
|
* @return JsonResponse Returns the article attachments.
|
||||||
|
* @throws InvalidFormatException
|
||||||
|
* @throws BindingResolutionException
|
||||||
|
* @throws InvalidCastException
|
||||||
|
*/
|
||||||
|
public function getAttachments(Request $request, Article $article): JsonResponse
|
||||||
|
{
|
||||||
|
if (ArticleConductor::viewable($article) === true) {
|
||||||
|
$medium = $article->attachments->map(function ($attachment) {
|
||||||
|
return $attachment->media;
|
||||||
|
});
|
||||||
|
|
||||||
|
return $this->respondAsResource(MediaConductor::collection($request, $medium), ['isCollection' => true, 'resourceName' => 'attachment']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store an attachment related to this model.
|
||||||
|
*
|
||||||
|
* @param Request $request The user request.
|
||||||
|
* @param Article $article The article model.
|
||||||
|
* @return JsonResponse The response.
|
||||||
|
* @throws BindingResolutionException
|
||||||
|
* @throws MassAssignmentException
|
||||||
|
*/
|
||||||
|
public function storeAttachment(Request $request, Article $article): JsonResponse
|
||||||
|
{
|
||||||
|
if (ArticleConductor::updatable($article) === true) {
|
||||||
|
if ($request->has("medium") && Media::find($request->medium)) {
|
||||||
|
$article->attachments()->create(['media_id' => $request->medium]);
|
||||||
|
return $this->respondCreated();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondWithErrors(['media' => 'The media ID was not found']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update/replace attachments related to this model.
|
||||||
|
*
|
||||||
|
* @param Request $request The user request.
|
||||||
|
* @param Article $article The related model.
|
||||||
|
* @throws BindingResolutionException
|
||||||
|
* @throws MassAssignmentException
|
||||||
|
*/
|
||||||
|
public function updateAttachments(Request $request, Article $article): JsonResponse
|
||||||
|
{
|
||||||
|
if (ArticleConductor::updatable($article) === true) {
|
||||||
|
$mediaIds = $request->attachments;
|
||||||
|
if (is_array($mediaIds) === false) {
|
||||||
|
$mediaIds = explode(',', $request->attachments);
|
||||||
|
}
|
||||||
|
|
||||||
|
$mediaIds = array_map('trim', $mediaIds); // trim each media ID
|
||||||
|
$attachments = $article->attachments;
|
||||||
|
|
||||||
|
// Delete attachments that are not in $mediaIds
|
||||||
|
foreach ($attachments as $attachment) {
|
||||||
|
if (!in_array($attachment->media_id, $mediaIds)) {
|
||||||
|
$attachment->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new attachments for media IDs that are not already in $article->attachments()
|
||||||
|
foreach ($mediaIds as $mediaId) {
|
||||||
|
$found = false;
|
||||||
|
|
||||||
|
foreach ($attachments as $attachment) {
|
||||||
|
if ($attachment->media_id == $mediaId) {
|
||||||
|
$found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$found) {
|
||||||
|
$article->attachments()->create(['media_id' => $mediaId]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondNoContent();
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a specific related attachment.
|
||||||
|
* @param Request $request The user request.
|
||||||
|
* @param Article $article The model.
|
||||||
|
* @param Media $medium The attachment medium.
|
||||||
|
* @throws BindingResolutionException
|
||||||
|
*/
|
||||||
|
public function deleteAttachment(Request $request, Article $article, Media $medium): JsonResponse
|
||||||
|
{
|
||||||
|
if (ArticleConductor::updatable($article) === true) {
|
||||||
|
$attachments = $article->attachments;
|
||||||
|
$deleted = false;
|
||||||
|
|
||||||
|
foreach ($attachments as $attachment) {
|
||||||
|
if ($attachment->media_id === $medium->id) {
|
||||||
|
$attachment->delete();
|
||||||
|
$deleted = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($deleted) {
|
||||||
|
// Attachment was deleted successfully
|
||||||
|
return $this->respondNoContent();
|
||||||
|
} else {
|
||||||
|
// Attachment with matching media ID was not found
|
||||||
|
return $this->respondNotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
}
|
||||||
84
app/Http/Controllers/Api/AttachmentController.php
Normal file
84
app/Http/Controllers/Api/AttachmentController.php
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Models\Attachment;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class AttachmentController extends ApiController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* ApplicationController constructor.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->middleware('auth:sanctum')
|
||||||
|
->except(['store', 'destroyByEmail']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display a listing of the resource.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a newly created resource in storage.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Attachment $attachment
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function show(Attachment $attachment)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the form for editing the specified resource.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Attachment $attachment
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function edit(Attachment $attachment)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the specified resource in storage.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param \App\Models\Attachment $attachment
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function update(Request $request, Attachment $attachment)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the specified resource from storage.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Attachment $attachment
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function destroy(Attachment $attachment)
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,9 +31,8 @@ class AuthController extends ApiController
|
|||||||
* Current User details
|
* Current User details
|
||||||
*
|
*
|
||||||
* @param Request $request Current request data.
|
* @param Request $request Current request data.
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function me(Request $request)
|
public function me(Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$user = $request->user()->makeVisible(['permissions']);
|
$user = $request->user()->makeVisible(['permissions']);
|
||||||
return $this->respondAsResource($user);
|
return $this->respondAsResource($user);
|
||||||
@@ -47,18 +46,18 @@ class AuthController extends ApiController
|
|||||||
*/
|
*/
|
||||||
public function login(AuthLoginRequest $request)
|
public function login(AuthLoginRequest $request)
|
||||||
{
|
{
|
||||||
$user = User::where('username', '=', $request->input('username'))->first();
|
$user = User::where('email', '=', $request->input('email'))->first();
|
||||||
|
|
||||||
if ($user !== null && Hash::check($request->input('password'), $user->password) === true) {
|
if ($user !== null && strlen($user->password) > 0 && Hash::check($request->input('password'), $user->password) === true) {
|
||||||
if ($user->email_verified_at === null) {
|
if ($user->email_verified_at === null) {
|
||||||
return $this->respondWithErrors([
|
return $this->respondWithErrors([
|
||||||
'username' => 'Email address has not been verified.'
|
'email' => 'Email address has not been verified.'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($user->disabled === true) {
|
if ($user->disabled === true) {
|
||||||
return $this->respondWithErrors([
|
return $this->respondWithErrors([
|
||||||
'username' => 'Account has been disabled.'
|
'email' => 'Account has been disabled.'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,13 +72,13 @@ class AuthController extends ApiController
|
|||||||
|
|
||||||
return $this->respondAsResource(
|
return $this->respondAsResource(
|
||||||
$user->makeVisible(['permissions']),
|
$user->makeVisible(['permissions']),
|
||||||
['token' => $token]
|
['appendData' => ['token' => $token]]
|
||||||
);
|
);
|
||||||
}//end if
|
}//end if
|
||||||
|
|
||||||
return $this->respondWithErrors([
|
return $this->respondWithErrors([
|
||||||
'username' => 'Invalid username or password',
|
'email' => 'Invalid email or password',
|
||||||
'password' => 'Invalid username or password',
|
'password' => 'Invalid email or password',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,9 +86,8 @@ class AuthController extends ApiController
|
|||||||
* Logout current user
|
* Logout current user
|
||||||
*
|
*
|
||||||
* @param Request $request Current request data.
|
* @param Request $request Current request data.
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function logout(Request $request)
|
public function logout(Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,13 @@
|
|||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
use App\Enum\HttpResponseCodes;
|
use App\Enum\HttpResponseCodes;
|
||||||
use App\Filters\EventFilter;
|
|
||||||
use App\Http\Requests\EventRequest;
|
|
||||||
use App\Models\Event;
|
use App\Models\Event;
|
||||||
|
use App\Conductors\EventConductor;
|
||||||
|
use App\Conductors\MediaConductor;
|
||||||
|
use App\Conductors\UserConductor;
|
||||||
|
use App\Http\Requests\EventRequest;
|
||||||
|
use App\Models\Media;
|
||||||
|
use App\Models\User;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class EventController extends ApiController
|
class EventController extends ApiController
|
||||||
@@ -16,62 +20,77 @@ class EventController extends ApiController
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->middleware('auth:sanctum')
|
$this->middleware('auth:sanctum')
|
||||||
->only(['store','update','destroy']);
|
->only(['store','update','destroy', 'userAdd', 'userUpdate', 'userDelete']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*
|
*
|
||||||
* @param EventFilter $filter The event filter.
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function index(EventFilter $filter)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
return $this->respondAsResource(
|
list($collection, $total) = EventConductor::request($request);
|
||||||
$filter->filter(),
|
|
||||||
['total' => $filter->foundTotal()]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Store a newly created resource in storage.
|
|
||||||
*
|
|
||||||
* @param EventRequest $request The event store request.
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function store(EventRequest $request)
|
|
||||||
{
|
|
||||||
$event = Event::create($request->all());
|
|
||||||
return $this->respondAsResource(
|
return $this->respondAsResource(
|
||||||
(new EventFilter($request))->filter($event),
|
$collection,
|
||||||
null,
|
['isCollection' => true,
|
||||||
HttpResponseCodes::HTTP_CREATED
|
'appendData' => ['total' => $total]
|
||||||
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display the specified resource.
|
* Display the specified resource.
|
||||||
*
|
*
|
||||||
* @param EventFilter $filter The event filter.
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
* @param \App\Models\Event $event The specified event.
|
* @param \App\Models\Event $event The specified event.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function show(EventFilter $filter, Event $event)
|
public function show(Request $request, Event $event)
|
||||||
{
|
{
|
||||||
return $this->respondAsResource($filter->filter($event));
|
if (EventConductor::viewable($event) === true) {
|
||||||
|
return $this->respondAsResource(EventConductor::model($request, $event));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a newly created resource in storage.
|
||||||
|
*
|
||||||
|
* @param \App\Http\Requests\EventRequest $request The request.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function store(EventRequest $request)
|
||||||
|
{
|
||||||
|
if (EventConductor::creatable() === true) {
|
||||||
|
$event = Event::create($request->all());
|
||||||
|
return $this->respondAsResource(
|
||||||
|
EventConductor::model($request, $event),
|
||||||
|
['respondCode' => HttpResponseCodes::HTTP_CREATED]
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the specified resource in storage.
|
* Update the specified resource in storage.
|
||||||
*
|
*
|
||||||
* @param EventRequest $request The event update request.
|
* @param \App\Http\Requests\EventRequest $request The endpoint request.
|
||||||
* @param \App\Models\Event $event The specified event.
|
* @param \App\Models\Event $event The specified event.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function update(EventRequest $request, Event $event)
|
public function update(EventRequest $request, Event $event)
|
||||||
{
|
{
|
||||||
|
if (EventConductor::updatable($event) === true) {
|
||||||
$event->update($request->all());
|
$event->update($request->all());
|
||||||
return $this->respondAsResource((new EventFilter($request))->filter($event));
|
return $this->respondAsResource(EventConductor::model($request, $event));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -82,7 +101,209 @@ class EventController extends ApiController
|
|||||||
*/
|
*/
|
||||||
public function destroy(Event $event)
|
public function destroy(Event $event)
|
||||||
{
|
{
|
||||||
|
if (EventConductor::destroyable($event) === true) {
|
||||||
$event->delete();
|
$event->delete();
|
||||||
return $this->respondNoContent();
|
return $this->respondNoContent();
|
||||||
|
} else {
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of attachments related to this model.
|
||||||
|
*
|
||||||
|
* @param Request $request The user request.
|
||||||
|
* @param Event $event The event model.
|
||||||
|
* @return JsonResponse Returns the event attachments.
|
||||||
|
*/
|
||||||
|
public function getAttachments(Request $request, Event $event): JsonResponse
|
||||||
|
{
|
||||||
|
if (EventConductor::viewable($event) === true) {
|
||||||
|
$medium = $event->attachments->map(function ($attachment) {
|
||||||
|
return $attachment->media;
|
||||||
|
});
|
||||||
|
|
||||||
|
return $this->respondAsResource(MediaConductor::collection($request, $medium), ['isCollection' => true, 'resourceName' => 'attachment']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store an attachment related to this model.
|
||||||
|
*
|
||||||
|
* @param Request $request The user request.
|
||||||
|
* @param Event $event The event model.
|
||||||
|
* @return JsonResponse The response.
|
||||||
|
*/
|
||||||
|
public function storeAttachment(Request $request, Event $event): JsonResponse
|
||||||
|
{
|
||||||
|
if (EventConductor::updatable($event) === true) {
|
||||||
|
if ($request->has("medium") === true && Media::find($request->medium) !== null) {
|
||||||
|
$event->attachments()->create(['media_id' => $request->medium]);
|
||||||
|
return $this->respondCreated();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondWithErrors(['media' => 'The media ID was not found']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update/replace attachments related to this model.
|
||||||
|
*
|
||||||
|
* @param Request $request The user request.
|
||||||
|
* @param Event $event The related model.
|
||||||
|
*/
|
||||||
|
public function updateAttachments(Request $request, Event $event): JsonResponse
|
||||||
|
{
|
||||||
|
if (EventConductor::updatable($event) === true) {
|
||||||
|
$mediaIds = $request->attachments;
|
||||||
|
if (is_array($mediaIds) === false) {
|
||||||
|
$mediaIds = explode(',', $request->attachments);
|
||||||
|
}
|
||||||
|
|
||||||
|
$mediaIds = array_map('trim', $mediaIds); // trim each media ID
|
||||||
|
$attachments = $event->attachments;
|
||||||
|
|
||||||
|
// Delete attachments that are not in $mediaIds
|
||||||
|
foreach ($attachments as $attachment) {
|
||||||
|
if (in_array($attachment->media_id, $mediaIds) === false) {
|
||||||
|
$attachment->delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new attachments for media IDs that are not already in $article->attachments()
|
||||||
|
foreach ($mediaIds as $mediaId) {
|
||||||
|
$found = false;
|
||||||
|
|
||||||
|
foreach ($attachments as $attachment) {
|
||||||
|
if ($attachment->media_id === $mediaId) {
|
||||||
|
$found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($found === false) {
|
||||||
|
$event->attachments()->create(['media_id' => $mediaId]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondNoContent();
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a specific related attachment.
|
||||||
|
*
|
||||||
|
* @param Request $request The user request.
|
||||||
|
* @param Event $event The model.
|
||||||
|
* @param Media $medium The attachment medium.
|
||||||
|
*/
|
||||||
|
public function deleteAttachment(Request $request, Event $event, Media $medium): JsonResponse
|
||||||
|
{
|
||||||
|
if (EventConductor::updatable($event) === true) {
|
||||||
|
$attachments = $event->attachments;
|
||||||
|
$deleted = false;
|
||||||
|
|
||||||
|
foreach ($attachments as $attachment) {
|
||||||
|
if ($attachment->media_id === $medium->id) {
|
||||||
|
$attachment->delete();
|
||||||
|
$deleted = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($deleted === true) {
|
||||||
|
// Attachment was deleted successfully
|
||||||
|
return $this->respondNoContent();
|
||||||
|
} else {
|
||||||
|
// Attachment with matching media ID was not found
|
||||||
|
return $this->respondNotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function userList(Request $request, Event $event)
|
||||||
|
{
|
||||||
|
$authUser = $request->user();
|
||||||
|
$eventUsers = $event->users;
|
||||||
|
|
||||||
|
if ($authUser !== null) {
|
||||||
|
$isAdmin = $authUser->hasPermission('admin/events');
|
||||||
|
$isEventUser = $eventUsers->contains($authUser->id);
|
||||||
|
|
||||||
|
if ($isAdmin === true || $isEventUser === true) {
|
||||||
|
if ($isAdmin === false) {
|
||||||
|
$eventUsers = $eventUsers->filter(function ($user) use ($authUser) {
|
||||||
|
return $user->id === $authUser->id;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondAsResource(UserConductor::collection($request, $eventUsers), ['isCollection' => true, 'resourceName' => 'users']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function userAdd(Request $request, Event $event)
|
||||||
|
{
|
||||||
|
$authUser = $request->user();
|
||||||
|
if ($authUser !== null && $authUser->hasPermission('admin/events') === true) {
|
||||||
|
if ($request->has("users") === true) {
|
||||||
|
$eventUsers = $event->users()->pluck('user_id')->toArray(); // Get the current users in the event
|
||||||
|
$requestedUsers = $request->input("users"); // Get the requested users
|
||||||
|
|
||||||
|
$usersToAdd = array_diff($requestedUsers, $eventUsers); // Users to add
|
||||||
|
$usersToRemove = array_diff($eventUsers, $requestedUsers); // Users to remove
|
||||||
|
|
||||||
|
// Add missing users
|
||||||
|
foreach ($usersToAdd as $userToAdd) {
|
||||||
|
if (User::find($userToAdd) !== null) {
|
||||||
|
$event->users()->attach($userToAdd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove extra users
|
||||||
|
foreach ($usersToRemove as $userToRemove) {
|
||||||
|
$event->users()->detach($userToRemove);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondNoContent();
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
return $this->respondWithErrors(['users' => 'The user list was not found']);
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function userUpdate(Request $request, Event $event)
|
||||||
|
{
|
||||||
|
// only admin/events permitted
|
||||||
|
}
|
||||||
|
|
||||||
|
public function userDelete(Request $request, Event $event, User $user)
|
||||||
|
{
|
||||||
|
$authUser = $request->user();
|
||||||
|
if ($authUser !== null && $authUser->hasPermission('admin/events') === true) {
|
||||||
|
$eventUsers = $event->users;
|
||||||
|
if ($eventUsers->find($user->id) !== null) {
|
||||||
|
$eventUsers->detach($user->id);
|
||||||
|
return $this->respondNoContent();
|
||||||
|
} else {
|
||||||
|
return $this->respondNotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
126
app/Http/Controllers/Api/LogController.php
Normal file
126
app/Http/Controllers/Api/LogController.php
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class LogController extends ApiController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* ApplicationController constructor.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->middleware('auth:sanctum')
|
||||||
|
->only(['show']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*
|
||||||
|
* @param Request $request The log request.
|
||||||
|
* @param string $name The log name.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function show(Request $request, string $name)
|
||||||
|
{
|
||||||
|
if ($request->user()?->hasPermission('logs/' . $name) === true) {
|
||||||
|
switch (strtolower($name)) {
|
||||||
|
case 'discord':
|
||||||
|
$data = [];
|
||||||
|
|
||||||
|
$log = $request->get('log');
|
||||||
|
if ($log === null) {
|
||||||
|
$log = ['output', 'error'];
|
||||||
|
} else {
|
||||||
|
$log = explode(',', strtolower($log));
|
||||||
|
}
|
||||||
|
|
||||||
|
$lines = intval($request->get('lines', 50));
|
||||||
|
if ($lines > 100) {
|
||||||
|
$lines = 100;
|
||||||
|
} elseif ($lines < 0) {
|
||||||
|
$lines = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
$before = $request->get('before');
|
||||||
|
if ($before !== null) {
|
||||||
|
$before = preg_split("/([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/", $before, -1, (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
|
||||||
|
if (count($before) !== 6) {
|
||||||
|
$before = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$after = $request->get('after');
|
||||||
|
if ($after !== null) {
|
||||||
|
$after = preg_split("/([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/", $after, -1, (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
|
||||||
|
if (count($after) !== 6) {
|
||||||
|
$after = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$logFiles = [
|
||||||
|
[
|
||||||
|
'name' => 'output',
|
||||||
|
'path' => '/home/discordbot/.pm2/logs/stemmech-discordbot-out-0.log'
|
||||||
|
],[
|
||||||
|
'name' => 'error',
|
||||||
|
'path' => '/home/discordbot/.pm2/logs/stemmech-discordbot-error-0.log'
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($logFiles as $logFile) {
|
||||||
|
if (in_array($logFile['name'], $log) === true) {
|
||||||
|
$logContent = '';
|
||||||
|
|
||||||
|
if (file_exists($logFile['path']) === true) {
|
||||||
|
$logContent = file_get_contents($logFile['path']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$logArray = preg_split("/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}: (?:(?!\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}: )[\s\S])*)/", $logContent, -1, (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
|
||||||
|
|
||||||
|
$logContent = '';
|
||||||
|
$logLineCount = 0;
|
||||||
|
$logLineSkip = false;
|
||||||
|
foreach (array_reverse($logArray) as $logLine) {
|
||||||
|
$lineDate = preg_split("/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2}): /", $logLine, -1, (PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY));
|
||||||
|
if (count($lineDate) >= 6) {
|
||||||
|
$logLineSkip = false;
|
||||||
|
|
||||||
|
// Is line before
|
||||||
|
if ($before !== null && ($lineDate[0] > $before[0] || $lineDate[1] > $before[1] || $lineDate[2] > $before[2] || $lineDate[3] > $before[3] || $lineDate[4] > $before[4] || $lineDate[5] > $before[5])) {
|
||||||
|
$logLineSkip = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is line after
|
||||||
|
if ($after !== null && ($after[0] > $lineDate[0] || $after[1] > $lineDate[1] || $after[2] > $lineDate[2] || $after[3] > $lineDate[3] || $after[4] > $lineDate[4] || $after[5] > $lineDate[5])) {
|
||||||
|
$logLineSkip = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$logLineCount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($logLineCount > $lines) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($logLineSkip === false) {
|
||||||
|
$logContent .= $logLine;
|
||||||
|
}
|
||||||
|
}//end foreach
|
||||||
|
|
||||||
|
$data[$logFile['name']] = $logContent;
|
||||||
|
}//end if
|
||||||
|
}//end foreach
|
||||||
|
|
||||||
|
return $this->respondJson([
|
||||||
|
'log' => $data
|
||||||
|
]);
|
||||||
|
}//end switch
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,14 +2,13 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Conductors\MediaConductor;
|
||||||
use App\Enum\HttpResponseCodes;
|
use App\Enum\HttpResponseCodes;
|
||||||
use App\Filters\MediaFilter;
|
use App\Http\Requests\MediaRequest;
|
||||||
use App\Http\Requests\MediaStoreRequest;
|
|
||||||
use App\Http\Requests\MediaUpdateRequest;
|
|
||||||
use App\Models\Media;
|
use App\Models\Media;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Carbon;
|
use Illuminate\Support\Carbon;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Laravel\Sanctum\PersonalAccessToken;
|
use Laravel\Sanctum\PersonalAccessToken;
|
||||||
|
|
||||||
class MediaController extends ApiController
|
class MediaController extends ApiController
|
||||||
@@ -26,40 +25,52 @@ class MediaController extends ApiController
|
|||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*
|
*
|
||||||
* @param \App\Filters\MediaFilter $filter Created filter object.
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function index(MediaFilter $filter)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
|
list($collection, $total) = MediaConductor::request($request);
|
||||||
|
|
||||||
return $this->respondAsResource(
|
return $this->respondAsResource(
|
||||||
$filter->filter(),
|
$collection,
|
||||||
['total' => $filter->foundTotal()]
|
['isCollection' => true,
|
||||||
|
'appendData' => ['total' => $total]
|
||||||
|
],
|
||||||
|
function ($options) {
|
||||||
|
return $options['total'] === 0;
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display the specified resource.
|
* Display the specified resource.
|
||||||
*
|
*
|
||||||
* @param MediaFilter $filter The request filter.
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
* @param Media $medium The request media.
|
* @param \App\Models\Media $medium The request media.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function show(MediaFilter $filter, Media $medium)
|
public function show(Request $request, Media $medium)
|
||||||
{
|
{
|
||||||
return $this->respondAsResource($filter->filter($medium));
|
if (MediaConductor::viewable($medium) === true) {
|
||||||
|
return $this->respondAsResource(MediaConductor::model($request, $medium));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a new media resource
|
* Store a new media resource
|
||||||
*
|
*
|
||||||
* @param MediaStoreRequest $request The uploaded media.
|
* @param \App\Http\Requests\MediaRequest $request The uploaded media.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function store(MediaStoreRequest $request)
|
public function store(MediaRequest $request)
|
||||||
{
|
{
|
||||||
|
if (MediaConductor::creatable() === true) {
|
||||||
$file = $request->file('file');
|
$file = $request->file('file');
|
||||||
if ($file === null) {
|
if ($file === null) {
|
||||||
return $this->respondError(['file' => 'An error occurred uploading the file to the server.']);
|
return $this->respondWithErrors(['file' => 'The browser did not upload the file correctly to the server.']);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($file->isValid() !== true) {
|
if ($file->isValid() !== true) {
|
||||||
@@ -68,108 +79,103 @@ class MediaController extends ApiController
|
|||||||
case UPLOAD_ERR_FORM_SIZE:
|
case UPLOAD_ERR_FORM_SIZE:
|
||||||
return $this->respondTooLarge();
|
return $this->respondTooLarge();
|
||||||
case UPLOAD_ERR_PARTIAL:
|
case UPLOAD_ERR_PARTIAL:
|
||||||
return $this->respondError(['file' => 'The file upload was interrupted.']);
|
return $this->respondWithErrors(['file' => 'The file upload was interrupted.']);
|
||||||
default:
|
default:
|
||||||
return $this->respondError(['file' => 'An error occurred uploading the file to the server.']);
|
return $this->respondWithErrors(['file' => 'An error occurred uploading the file to the server.']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($file->getSize() > Media::maxUploadSize()) {
|
if ($file->getSize() > Media::getMaxUploadSize()) {
|
||||||
return $this->respondTooLarge();
|
return $this->respondTooLarge();
|
||||||
}
|
}
|
||||||
|
|
||||||
$title = $file->getClientOriginalName();
|
try {
|
||||||
$mime = $file->getMimeType();
|
$media = Media::createFromUploadedFile($request, $file);
|
||||||
$fileInfo = Media::store($file, empty($request->input('permission')));
|
} catch (\Exception $e) {
|
||||||
if ($fileInfo === null) {
|
if ($e->getCode() === Media::FILE_SIZE_EXCEEDED_ERROR) {
|
||||||
return $this->respondError(
|
return $this->respondTooLarge();
|
||||||
['file' => 'The file could not be stored on the server'],
|
} else {
|
||||||
HttpResponseCodes::HTTP_INTERNAL_SERVER_ERROR
|
return $this->respondWithErrors(['file' => $e->getMessage()]);
|
||||||
);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$request->merge([
|
return $this->respondAsResource(
|
||||||
'title' => $title,
|
MediaConductor::model($request, $media),
|
||||||
'mime' => $mime,
|
['respondCode' => HttpResponseCodes::HTTP_ACCEPTED]
|
||||||
'name' => $fileInfo['name'],
|
);
|
||||||
'size' => filesize($fileInfo['path'])
|
}//end if
|
||||||
]);
|
|
||||||
|
|
||||||
$media = $request->user()->media()->create($request->all());
|
return $this->respondForbidden();
|
||||||
return $this->respondAsResource((new MediaFilter($request))->filter($media));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the media resource in storage.
|
* Update the media resource in storage.
|
||||||
*
|
*
|
||||||
* @param MediaUpdateRequest $request The update request.
|
* @param \App\Http\Requests\MediaRequest $request The update request.
|
||||||
* @param \App\Models\Media $medium The specified media.
|
* @param \App\Models\Media $medium The specified media.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function update(MediaUpdateRequest $request, Media $medium)
|
public function update(MediaRequest $request, Media $medium)
|
||||||
{
|
{
|
||||||
if ((new MediaFilter($request))->filter($medium) === null) {
|
if (MediaConductor::updatable($medium) === true) {
|
||||||
return $this->respondNotFound();
|
|
||||||
}
|
|
||||||
|
|
||||||
$file = $request->file('file');
|
$file = $request->file('file');
|
||||||
if ($file !== null) {
|
if ($file !== null) {
|
||||||
if ($file->getSize() > Media::maxUploadSize()) {
|
if ($file->isValid() !== true) {
|
||||||
|
switch ($file->getError()) {
|
||||||
|
case UPLOAD_ERR_INI_SIZE:
|
||||||
|
case UPLOAD_ERR_FORM_SIZE:
|
||||||
return $this->respondTooLarge();
|
return $this->respondTooLarge();
|
||||||
|
case UPLOAD_ERR_PARTIAL:
|
||||||
|
return $this->respondWithErrors(['file' => 'The file upload was interrupted.']);
|
||||||
|
default:
|
||||||
|
return $this->respondWithErrors(['file' => 'An error occurred uploading the file to the server.']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$oldPath = $medium->path();
|
if ($file->getSize() > Media::getMaxUploadSize()) {
|
||||||
$fileInfo = Media::store($file, empty($request->input('permission')));
|
return $this->respondTooLarge();
|
||||||
if ($fileInfo === null) {
|
}
|
||||||
return $this->respondError(
|
}
|
||||||
['file' => 'The file could not be stored on the server'],
|
|
||||||
|
$medium->update($request->all());
|
||||||
|
|
||||||
|
if ($file !== null) {
|
||||||
|
try {
|
||||||
|
$medium->updateWithUploadedFile($file);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
return $this->respondWithErrors(
|
||||||
|
['file' => $e->getMessage()],
|
||||||
HttpResponseCodes::HTTP_INTERNAL_SERVER_ERROR
|
HttpResponseCodes::HTTP_INTERNAL_SERVER_ERROR
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (file_exists($oldPath) === true) {
|
|
||||||
unlink($oldPath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$request->merge([
|
return $this->respondAsResource(MediaConductor::model($request, $medium));
|
||||||
'title' => $file->getClientOriginalName(),
|
|
||||||
'mime' => $file->getMimeType(),
|
|
||||||
'name' => $fileInfo['name'],
|
|
||||||
'size' => filesize($fileInfo['path'])
|
|
||||||
]);
|
|
||||||
}//end if
|
}//end if
|
||||||
|
|
||||||
$medium->update($request->all());
|
return $this->respondForbidden();
|
||||||
return $this->respondWithTransformer($file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the specified resource from storage.
|
* Remove the specified resource from storage.
|
||||||
*
|
*
|
||||||
* @param Request $request Request instance.
|
|
||||||
* @param \App\Models\Media $medium Specified media file.
|
* @param \App\Models\Media $medium Specified media file.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function destroy(Request $request, Media $medium)
|
public function destroy(Media $medium)
|
||||||
{
|
{
|
||||||
if ((new MediaFilter($request))->filter($medium) !== null) {
|
if (MediaConductor::destroyable($medium) === true) {
|
||||||
if (file_exists($medium->path()) === true) {
|
|
||||||
unlink($medium->path());
|
|
||||||
}
|
|
||||||
|
|
||||||
$medium->delete();
|
$medium->delete();
|
||||||
return $this->respondNoContent();
|
return $this->respondNoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->respondNotFound();
|
return $this->respondForbidden();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display the specified resource.
|
* Display the specified resource.
|
||||||
*
|
*
|
||||||
* @param Request $request Request instance.
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
* @param \App\Models\Media $medium Specified media.
|
* @param \App\Models\Media $medium Specified media.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
@@ -218,6 +224,7 @@ class MediaController extends ApiController
|
|||||||
$headerExpires = $updated_at->addMonth()->toRfc2822String();
|
$headerExpires = $updated_at->addMonth()->toRfc2822String();
|
||||||
}//end if
|
}//end if
|
||||||
|
|
||||||
|
// deepcode ignore InsecureHash: Browsers expect Etag to be a md5 hash
|
||||||
$headerEtag = md5($updated_at->format('U'));
|
$headerEtag = md5($updated_at->format('U'));
|
||||||
$headerLastModified = $updated_at->toRfc2822String();
|
$headerLastModified = $updated_at->toRfc2822String();
|
||||||
|
|
||||||
|
|||||||
231
app/Http/Controllers/Api/OCRController.php
Normal file
231
app/Http/Controllers/Api/OCRController.php
Normal file
@@ -0,0 +1,231 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use thiagoalessio\TesseractOCR\TesseractOCR;
|
||||||
|
use FFMpeg;
|
||||||
|
use App\Enum\CurlErrorCodes;
|
||||||
|
|
||||||
|
class OCRController extends ApiController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* ApplicationController constructor.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
// $this->middleware('auth:sanctum')
|
||||||
|
// ->only(['show']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*
|
||||||
|
* @param Request $request The log request.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function show(Request $request)
|
||||||
|
{
|
||||||
|
// if ($request->user()?->hasPermission('logs/' . $name) === true) {
|
||||||
|
$url = $request->get('url');
|
||||||
|
if ($url !== null) {
|
||||||
|
$data = ['ocr' => []];
|
||||||
|
|
||||||
|
$filters = $request->get('filters', ['tesseract']);
|
||||||
|
if (is_array($filters) === false) {
|
||||||
|
$filters = explode(',', $filters);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tesseractOEM = $request->get('tesseract.oem');
|
||||||
|
$tesseractDigits = $request->get('tesseract.digits');
|
||||||
|
$tesseractAllowlist = $request->get('tesseract.allowlist');
|
||||||
|
|
||||||
|
// Download URL
|
||||||
|
$urlDownloadFilePath = tempnam(sys_get_temp_dir(), 'download');
|
||||||
|
$maxDownloadSize = (1024 * 1024); // 1MB
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
|
|
||||||
|
// We need progress updates to break the connection mid-way
|
||||||
|
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128); // more progress info
|
||||||
|
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
|
||||||
|
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function (
|
||||||
|
$downloadSize,
|
||||||
|
$downloaded,
|
||||||
|
$uploadSize,
|
||||||
|
$uploaded
|
||||||
|
) use ($maxDownloadSize) {
|
||||||
|
return ($downloaded > $maxDownloadSize) ? 1 : 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
$curlResult = curl_exec($ch);
|
||||||
|
$curlError = curl_errno($ch);
|
||||||
|
$curlSize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
|
||||||
|
curl_close($ch);
|
||||||
|
if ($curlError !== 0) {
|
||||||
|
$error = 'File size is larger then allowed';
|
||||||
|
if ($curlError !== CurlErrorCodes::CURLE_ABORTED_BY_CALLBACK) {
|
||||||
|
$error = CurlErrorCodes::getMessage($curlError);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondWithErrors(['url' => $error]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save url file
|
||||||
|
file_put_contents($urlDownloadFilePath, $curlResult);
|
||||||
|
$urlDownloadFilePathBase = preg_replace('/\\.[^.\\s]{3,4}$/', '', $urlDownloadFilePath);
|
||||||
|
|
||||||
|
// tesseract (overall)
|
||||||
|
$ocr = null;
|
||||||
|
foreach ($filters as $filterItem) {
|
||||||
|
if (str_starts_with($filterItem, 'tesseract') === true) {
|
||||||
|
$ocr = new TesseractOCR();
|
||||||
|
$ocr->image($urlDownloadFilePath);
|
||||||
|
if ($tesseractOEM !== null) {
|
||||||
|
$ocr->oem($tesseractOEM);
|
||||||
|
}
|
||||||
|
if ($tesseractDigits !== null) {
|
||||||
|
$ocr->digits();
|
||||||
|
}
|
||||||
|
if ($tesseractAllowlist !== null) {
|
||||||
|
$ocr->allowlist($tesseractAllowlist);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Image Filter Function
|
||||||
|
$tesseractImageFilterFunc = function ($filter, $options = null) use ($curlResult, $curlSize, $ocr) {
|
||||||
|
$result = '';
|
||||||
|
$img = imagecreatefromstring($curlResult);
|
||||||
|
if ($img !== false && (($options !== null && imagefilter($img, $filter, $options) === true) || ($options === null && imagefilter($img, $filter) === true))) {
|
||||||
|
ob_start();
|
||||||
|
imagepng($img);
|
||||||
|
$imgData = ob_get_contents();
|
||||||
|
ob_end_clean();
|
||||||
|
$imgDataSize = strlen($imgData);
|
||||||
|
|
||||||
|
$ocr->imageData($imgData, $imgDataSize);
|
||||||
|
imagedestroy($img);
|
||||||
|
|
||||||
|
$result = $ocr->run(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Image Scale Function
|
||||||
|
$tesseractImageScaleFunc = function ($scaleFunc) use ($curlResult, $ocr) {
|
||||||
|
$result = '';
|
||||||
|
$srcImage = imagecreatefromstring($curlResult);
|
||||||
|
$srcWidth = imagesx($srcImage);
|
||||||
|
$srcHeight = imagesy($srcImage);
|
||||||
|
|
||||||
|
$dstWidth = $scaleFunc($srcWidth);
|
||||||
|
$dstHeight = $scaleFunc($srcHeight);
|
||||||
|
$dstImage = imagecreatetruecolor($dstWidth, $dstHeight);
|
||||||
|
|
||||||
|
imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
imagepng($dstImage);
|
||||||
|
$imgData = ob_get_contents();
|
||||||
|
ob_end_clean();
|
||||||
|
$imgDataSize = strlen($imgData);
|
||||||
|
|
||||||
|
imagedestroy($srcImage);
|
||||||
|
imagedestroy($dstImage);
|
||||||
|
|
||||||
|
$ocr->imageData($imgData, $imgDataSize);
|
||||||
|
$result = $ocr->run(500);
|
||||||
|
return $result;
|
||||||
|
};
|
||||||
|
|
||||||
|
// filter: tesseract
|
||||||
|
if (in_array('tesseract', $filters) === true) {
|
||||||
|
$data['ocr']['tesseract'] = $ocr->run(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter: tesseract.grayscale
|
||||||
|
if (in_array('tesseract.grayscale', $filters) === true) {
|
||||||
|
$data['ocr']['tesseract.grayscale'] = $tesseractImageFilterFunc(IMG_FILTER_GRAYSCALE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter: tesseract.double_scale
|
||||||
|
if (in_array('tesseract.double_scale', $filters) === true) {
|
||||||
|
$data['ocr']['tesseract.double_scale'] = $tesseractImageScaleFunc(function ($size) {
|
||||||
|
return $size * 2;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter: tesseract.half_scale
|
||||||
|
if (in_array('tesseract.half_scale', $filters) === true) {
|
||||||
|
$data['ocr']['tesseract.half_scale'] = $tesseractImageScaleFunc(function ($size) {
|
||||||
|
return $size / 2;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter: tesseract.edgedetect
|
||||||
|
if (in_array('tesseract.edgedetect', $filters) === true) {
|
||||||
|
$data['ocr']['tesseract.edgedetect'] = $tesseractImageFilterFunc(IMG_FILTER_EDGEDETECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter: tesseract.mean_removal
|
||||||
|
if (in_array('tesseract.mean_removal', $filters) === true) {
|
||||||
|
$data['ocr']['tesseract.mean_removal'] = $tesseractImageFilterFunc(IMG_FILTER_MEAN_REMOVAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter: tesseract.negate
|
||||||
|
if (in_array('tesseract.negate', $filters) === true) {
|
||||||
|
$data['ocr']['tesseract.negate'] = $tesseractImageFilterFunc(IMG_FILTER_NEGATE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter: tesseract.pixelate
|
||||||
|
if (in_array('tesseract.pixelate', $filters) === true) {
|
||||||
|
$data['ocr']['tesseract.pixelate'] = $tesseractImageFilterFunc(IMG_FILTER_PIXELATE, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter: keras
|
||||||
|
if (in_array('keras', $filters) === true) {
|
||||||
|
$cmd = '/usr/bin/python3 ' . base_path() . '/scripts/keras_oc.py ' . urlencode($url);
|
||||||
|
$command = escapeshellcmd($cmd);
|
||||||
|
$output = shell_exec($cmd);
|
||||||
|
if ($output !== null && strlen($output) > 0) {
|
||||||
|
$output = substr($output, (strpos($output, '----------START----------') + 25));
|
||||||
|
} else {
|
||||||
|
$output = '';
|
||||||
|
}
|
||||||
|
$data['ocr']['keras'] = $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
unlink($urlDownloadFilePath);
|
||||||
|
return $this->respondJson($data);
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
return $this->respondWithErrors(['url' => 'url is missing']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// $ffmpeg = FFMpeg\FFMpeg::create();
|
||||||
|
|
||||||
|
// // Load the input video
|
||||||
|
// $inputFile = $ffmpeg->open('input.mp4');
|
||||||
|
|
||||||
|
// // Split the video into individual frames
|
||||||
|
// $videoFrames = $inputFile->frames();
|
||||||
|
// foreach ($videoFrames as $frame) {
|
||||||
|
// // Save the frame as a PNG
|
||||||
|
// $frame->save(new FFMpeg\Format\Video\PNG(), 'frame-' . $frame->getMetadata('pts') . '.png');
|
||||||
|
|
||||||
|
// // Pass the PNG to Tesseract for processing
|
||||||
|
// exec("tesseract frame-" . $frame->getMetadata('pts') . ".png output");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Read the output from Tesseract
|
||||||
|
// $text = file_get_contents("output.txt");
|
||||||
|
|
||||||
|
// // Do something with the text from Tesseract
|
||||||
|
// echo $text;
|
||||||
|
}
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
|
||||||
|
|
||||||
use App\Enum\HttpResponseCodes;
|
|
||||||
use App\Filters\PostFilter;
|
|
||||||
use App\Http\Requests\PostStoreRequest;
|
|
||||||
use App\Http\Requests\PostUpdateRequest;
|
|
||||||
use App\Models\Post;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
|
|
||||||
class PostController extends ApiController
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* ApplicationController constructor.
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
$this->middleware('auth:sanctum')
|
|
||||||
->only([
|
|
||||||
'store',
|
|
||||||
'update',
|
|
||||||
'delete'
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Display a listing of the resource.
|
|
||||||
*
|
|
||||||
* @param \App\Filters\PostFilter $filter Post filter request.
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function index(PostFilter $filter)
|
|
||||||
{
|
|
||||||
return $this->respondAsResource(
|
|
||||||
$filter->filter(),
|
|
||||||
['total' => $filter->foundTotal()]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Display the specified resource.
|
|
||||||
*
|
|
||||||
* @param PostFilter $filter The filter request.
|
|
||||||
* @param \App\Models\Post $post The post model.
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function show(PostFilter $filter, Post $post)
|
|
||||||
{
|
|
||||||
return $this->respondAsResource($filter->filter($post));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Store a newly created resource in storage.
|
|
||||||
*
|
|
||||||
* @param PostStoreRequest $request The post store request.
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function store(PostStoreRequest $request)
|
|
||||||
{
|
|
||||||
$post = Post::create($request->all());
|
|
||||||
return $this->respondAsResource(
|
|
||||||
(new PostFilter($request))->filter($post),
|
|
||||||
null,
|
|
||||||
HttpResponseCodes::HTTP_CREATED
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update the specified resource in storage.
|
|
||||||
*
|
|
||||||
* @param PostUpdateRequest $request The post update request.
|
|
||||||
* @param \App\Models\Post $post The specified post.
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function update(PostUpdateRequest $request, Post $post)
|
|
||||||
{
|
|
||||||
$post->update($request->all());
|
|
||||||
return $this->respondAsResource((new PostFilter($request))->filter($post));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove the specified resource from storage.
|
|
||||||
*
|
|
||||||
* @param \App\Models\Post $post The specified post.
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function destroy(Post $post)
|
|
||||||
{
|
|
||||||
$post->delete();
|
|
||||||
return $this->respondNoContent();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
117
app/Http/Controllers/Api/ShortlinkController.php
Normal file
117
app/Http/Controllers/Api/ShortlinkController.php
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Conductors\MediaConductor;
|
||||||
|
use App\Conductors\ShortlinkConductor;
|
||||||
|
use App\Enum\HttpResponseCodes;
|
||||||
|
use App\Http\Requests\MediaRequest;
|
||||||
|
use App\Http\Requests\ShortlinkRequest;
|
||||||
|
use App\Models\Media;
|
||||||
|
use App\Models\Shortlink;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Laravel\Sanctum\PersonalAccessToken;
|
||||||
|
|
||||||
|
class ShortlinkController extends ApiController
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* ApplicationController constructor.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->middleware('auth:sanctum')
|
||||||
|
->only(['store','update','destroy']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display a listing of the resource.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
list($collection, $total) = ShortlinkConductor::request($request);
|
||||||
|
|
||||||
|
return $this->respondAsResource(
|
||||||
|
$collection,
|
||||||
|
['isCollection' => true,
|
||||||
|
'appendData' => ['total' => $total]
|
||||||
|
],
|
||||||
|
function ($options) {
|
||||||
|
return $options['total'] === 0;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified resource.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
|
* @param \App\Models\Shortlink $shortlink The request shortlink.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function show(Request $request, Shortlink $shortlink)
|
||||||
|
{
|
||||||
|
if (ShortlinkConductor::viewable($shortlink) === true) {
|
||||||
|
return $this->respondAsResource(ShortlinkConductor::model($request, $shortlink));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Store a new media resource
|
||||||
|
*
|
||||||
|
* @param \App\Http\Requests\ShortlinkRequest $request The shortlink.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function store(ShortlinkRequest $request)
|
||||||
|
{
|
||||||
|
if (ShortlinkConductor::creatable() === true) {
|
||||||
|
$shortlink = Shortlink::create($request->all());
|
||||||
|
|
||||||
|
return $this->respondAsResource(
|
||||||
|
ShortlinkConductor::model($request, $shortlink),
|
||||||
|
['respondCode' => HttpResponseCodes::HTTP_ACCEPTED]
|
||||||
|
);
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the media resource in storage.
|
||||||
|
*
|
||||||
|
* @param \App\Http\Requests\ShortlinkRequest $request The update request.
|
||||||
|
* @param \App\Models\Shortlink $medium The specified shortlink.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function update(ShortlinkRequest $request, Shortlink $shortlink)
|
||||||
|
{
|
||||||
|
if (ShortlinkConductor::updatable($shortlink) === true) {
|
||||||
|
$shortlink->update($request->all());
|
||||||
|
return $this->respondAsResource(ShortlinkConductor::model($request, $shortlink));
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the specified resource from storage.
|
||||||
|
*
|
||||||
|
* @param \App\Models\Shortlink $medium Specified shortlink.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function destroy(Shortlink $shortlink)
|
||||||
|
{
|
||||||
|
if (ShortlinkConductor::destroyable($shortlink) === true) {
|
||||||
|
$shortlink->delete();
|
||||||
|
return $this->respondNoContent();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Conductors\SubscriptionConductor;
|
||||||
|
use App\Enum\HttpResponseCodes;
|
||||||
use App\Models\Subscription;
|
use App\Models\Subscription;
|
||||||
use App\Filters\SubscriptionFilter;
|
|
||||||
use App\Http\Requests\SubscriptionRequest;
|
use App\Http\Requests\SubscriptionRequest;
|
||||||
use App\Jobs\SendEmailJob;
|
use App\Jobs\SendEmailJob;
|
||||||
use App\Mail\SubscriptionConfirm;
|
use App\Mail\SubscriptionConfirm;
|
||||||
use App\Mail\SubscriptionUnsubscribed;
|
use App\Mail\SubscriptionUnsubscribed;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class SubscriptionController extends ApiController
|
class SubscriptionController extends ApiController
|
||||||
{
|
{
|
||||||
@@ -23,58 +25,72 @@ class SubscriptionController extends ApiController
|
|||||||
/**
|
/**
|
||||||
* Display a listing of subscribers.
|
* Display a listing of subscribers.
|
||||||
*
|
*
|
||||||
* @param \App\Filters\SubscriptionFilter $filter Filter object.
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function index(SubscriptionFilter $filter)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$collection = $filter->filter();
|
list($collection, $total) = SubscriptionConductor::request($request);
|
||||||
|
|
||||||
return $this->respondAsResource(
|
return $this->respondAsResource(
|
||||||
$collection,
|
$collection,
|
||||||
['total' => $filter->foundTotal()]
|
['isCollection' => true,
|
||||||
|
'appendData' => ['total' => $total]
|
||||||
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Display the specified user.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
|
* @param \App\Models\Subscription $subscription The subscription model.
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function show(Request $request, Subscription $subscription)
|
||||||
|
{
|
||||||
|
if (SubscriptionConductor::viewable($subscription) === true) {
|
||||||
|
return $this->respondAsResource(SubscriptionConductor::model($request, $subscription));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a subscriber email in the database.
|
* Store a subscriber email in the database.
|
||||||
*
|
*
|
||||||
* @param SubscriptionRequest $request The subscriber update request.
|
* @param \App\Http\Requests\SubscriptionRequest $request The subscriber update request.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function store(SubscriptionRequest $request)
|
public function store(SubscriptionRequest $request)
|
||||||
{
|
{
|
||||||
if (Subscription::where('email', $request->email)->first() !== null) {
|
if (SubscriptionConductor::creatable() === true) {
|
||||||
return $this->respondWithErrors(['email' => 'This email address has already subscribed']);
|
|
||||||
}
|
|
||||||
|
|
||||||
Subscription::create($request->all());
|
Subscription::create($request->all());
|
||||||
dispatch((new SendEmailJob($request->email, new SubscriptionConfirm($request->email))))->onQueue('mail');
|
dispatch((new SendEmailJob($request->email, new SubscriptionConfirm($request->email))))->onQueue('mail');
|
||||||
|
|
||||||
return $this->respondCreated();
|
return $this->respondCreated();
|
||||||
|
} else {
|
||||||
|
return $this->respondForbidden();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Display the specified user.
|
|
||||||
*
|
|
||||||
* @param SubscriptionFilter $filter The subscription filter.
|
|
||||||
* @param Subscription $subscription The subscription model.
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function show(SubscriptionFilter $filter, Subscription $subscription)
|
|
||||||
{
|
|
||||||
return $this->respondAsResource($filter->filter($subscription));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the specified resource in storage.
|
* Update the specified resource in storage.
|
||||||
*
|
*
|
||||||
* @param SubscriptionRequest $request The subscription update request.
|
* @param \App\Http\Requests\SubscriptionRequest $request The subscription update request.
|
||||||
* @param Subscription $subscription The specified subscription.
|
* @param \App\Models\Subscription $subscription The specified subscription.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function update(SubscriptionRequest $request, Subscription $subscription)
|
public function update(SubscriptionRequest $request, Subscription $subscription)
|
||||||
{
|
{
|
||||||
|
// if (EventConductor::updatable($event) === true) {
|
||||||
|
// $event->update($request->all());
|
||||||
|
// return $this->respondAsResource(EventConductor::model($request, $event));
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return $this->respondForbidden();
|
||||||
|
|
||||||
|
|
||||||
// $input = [];
|
// $input = [];
|
||||||
// $updatable = ['username', 'first_name', 'last_name', 'email', 'phone', 'password'];
|
// $updatable = ['username', 'first_name', 'last_name', 'email', 'phone', 'password'];
|
||||||
|
|
||||||
@@ -103,14 +119,12 @@ class SubscriptionController extends ApiController
|
|||||||
*/
|
*/
|
||||||
public function destroy(Subscription $subscription)
|
public function destroy(Subscription $subscription)
|
||||||
{
|
{
|
||||||
// if ($user->hasPermission('admin/user') === false) {
|
if (SubscriptionConductor::destroyable($subscription) === true) {
|
||||||
// return $this->respondForbidden();
|
|
||||||
// }
|
|
||||||
|
|
||||||
$email = $subscription->email;
|
|
||||||
|
|
||||||
$subscription->delete();
|
$subscription->delete();
|
||||||
return $this->respondNoContent();
|
return $this->respondNoContent();
|
||||||
|
} else {
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -2,12 +2,10 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Api;
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Conductors\EventConductor;
|
||||||
use App\Enum\HttpResponseCodes;
|
use App\Enum\HttpResponseCodes;
|
||||||
use App\Filters\UserFilter;
|
use App\Http\Requests\UserRequest;
|
||||||
use App\Http\Requests\UserUpdateRequest;
|
|
||||||
use App\Http\Requests\UserStoreRequest;
|
|
||||||
use App\Http\Requests\UserForgotPasswordRequest;
|
use App\Http\Requests\UserForgotPasswordRequest;
|
||||||
use App\Http\Requests\UserForgotUsernameRequest;
|
|
||||||
use App\Http\Requests\UserRegisterRequest;
|
use App\Http\Requests\UserRegisterRequest;
|
||||||
use App\Http\Requests\UserResendVerifyEmailRequest;
|
use App\Http\Requests\UserResendVerifyEmailRequest;
|
||||||
use App\Http\Requests\UserResetPasswordRequest;
|
use App\Http\Requests\UserResetPasswordRequest;
|
||||||
@@ -16,13 +14,15 @@ use App\Jobs\SendEmailJob;
|
|||||||
use App\Mail\ChangedEmail;
|
use App\Mail\ChangedEmail;
|
||||||
use App\Mail\ChangedPassword;
|
use App\Mail\ChangedPassword;
|
||||||
use App\Mail\ChangeEmailVerify;
|
use App\Mail\ChangeEmailVerify;
|
||||||
use App\Mail\ForgotUsername;
|
|
||||||
use App\Mail\ForgotPassword;
|
use App\Mail\ForgotPassword;
|
||||||
use App\Mail\EmailVerify;
|
use App\Mail\EmailVerify;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Models\UserCode;
|
use App\Models\UserCode;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Hash;
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use App\Conductors\UserConductor;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||||
|
|
||||||
class UserController extends ApiController
|
class UserController extends ApiController
|
||||||
{
|
{
|
||||||
@@ -38,73 +38,78 @@ class UserController extends ApiController
|
|||||||
'register',
|
'register',
|
||||||
'exists',
|
'exists',
|
||||||
'forgotPassword',
|
'forgotPassword',
|
||||||
'forgotUsername',
|
|
||||||
'resetPassword',
|
'resetPassword',
|
||||||
'verifyEmail',
|
'verifyEmail',
|
||||||
'resendVerifyEmailCode'
|
'resendVerifyEmailCode',
|
||||||
|
'eventList',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display a listing of the resource.
|
* Display a listing of the resource.
|
||||||
*
|
*
|
||||||
* @param \App\Filters\UserFilter $filter Filter object.
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function index(UserFilter $filter)
|
public function index(Request $request)
|
||||||
{
|
{
|
||||||
$collection = $filter->filter();
|
list($collection, $total) = UserConductor::request($request);
|
||||||
|
|
||||||
return $this->respondAsResource(
|
return $this->respondAsResource(
|
||||||
$collection,
|
$collection,
|
||||||
['total' => $filter->foundTotal()]
|
['isCollection' => true,
|
||||||
|
'appendData' => ['total' => $total]
|
||||||
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a newly created user in the database.
|
* Store a newly created user in the database.
|
||||||
*
|
*
|
||||||
* @param UserStoreRequest $request The user update request.
|
* @param \App\Http\Requests\UserRequest $request The endpoint request.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function store(UserStoreRequest $request)
|
public function store(UserRequest $request)
|
||||||
{
|
{
|
||||||
if ($request->user()->hasPermission('admin/user') !== true) {
|
if (UserConductor::creatable() === true) {
|
||||||
|
$user = User::create($request->all());
|
||||||
|
return $this->respondAsResource(UserConductor::model($request, $user), ['respondCode' => HttpResponseCodes::HTTP_CREATED]);
|
||||||
|
} else {
|
||||||
return $this->respondForbidden();
|
return $this->respondForbidden();
|
||||||
}
|
}
|
||||||
|
|
||||||
$user = User::create($request->all());
|
|
||||||
return $this->respondAsResource((new UserFilter($request))->filter($user), [], HttpResponseCodes::HTTP_CREATED);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display the specified user.
|
* Display the specified user.
|
||||||
*
|
*
|
||||||
* @param UserFilter $filter The user filter.
|
* @param \Illuminate\Http\Request $request The endpoint request.
|
||||||
* @param User $user The user model.
|
* @param \App\Models\User $user The user model.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function show(UserFilter $filter, User $user)
|
public function show(Request $request, User $user)
|
||||||
{
|
{
|
||||||
return $this->respondAsResource($filter->filter($user));
|
if (UserConductor::viewable($user) === true) {
|
||||||
|
return $this->respondAsResource(UserConductor::model($request, $user));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the specified resource in storage.
|
* Update the specified resource in storage.
|
||||||
*
|
*
|
||||||
* @param UserUpdateRequest $request The user update request.
|
* @param \App\Http\Requests\UserRequest $request The user update request.
|
||||||
* @param User $user The specified user.
|
* @param \App\Models\User $user The specified user.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function update(UserUpdateRequest $request, User $user)
|
public function update(UserRequest $request, User $user)
|
||||||
{
|
{
|
||||||
|
if (UserConductor::updatable($user) === true) {
|
||||||
$input = [];
|
$input = [];
|
||||||
$updatable = ['username', 'first_name', 'last_name', 'email', 'phone', 'password'];
|
$updatable = ['first_name', 'last_name', 'email', 'phone', 'password', 'display_name'];
|
||||||
|
|
||||||
if ($request->user()->hasPermission('admin/user') === true) {
|
if ($request->user()->hasPermission('admin/user') === true) {
|
||||||
$updatable = array_merge($updatable, ['email_verified_at']);
|
$updatable = array_merge($updatable, ['email_verified_at']);
|
||||||
} elseif ($request->user()->is($user) !== true) {
|
|
||||||
return $this->respondForbidden();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$input = $request->only($updatable);
|
$input = $request->only($updatable);
|
||||||
@@ -114,44 +119,58 @@ class UserController extends ApiController
|
|||||||
|
|
||||||
$user->update($input);
|
$user->update($input);
|
||||||
|
|
||||||
return $this->respondAsResource((new UserFilter($request))->filter($user));
|
return $this->respondAsResource(UserConductor::model($request, $user));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the user from the database.
|
* Remove the user from the database.
|
||||||
*
|
*
|
||||||
* @param User $user The specified user.
|
* @param \App\Models\User $user The specified user.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function destroy(User $user)
|
public function destroy(User $user)
|
||||||
{
|
{
|
||||||
if ($user->hasPermission('admin/user') === false) {
|
if (UserConductor::destroyable($user) === true) {
|
||||||
return $this->respondForbidden();
|
|
||||||
}
|
|
||||||
|
|
||||||
$user->delete();
|
$user->delete();
|
||||||
return $this->respondNoContent();
|
return $this->respondNoContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a new user
|
* Register a new user
|
||||||
*
|
*
|
||||||
* @param UserRegisterRequest $request The register user request.
|
* @param \App\Http\Requests\UserRegisterRequest $request The register user request.
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
*/
|
||||||
public function register(UserRegisterRequest $request)
|
public function register(UserRegisterRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$user = User::create([
|
$userData = $request->only([
|
||||||
'first_name' => $request->input('first_name'),
|
'first_name',
|
||||||
'last_name' => $request->input('last_name'),
|
'last_name',
|
||||||
'username' => $request->input('username'),
|
'email',
|
||||||
'email' => $request->input('email'),
|
'phone',
|
||||||
'phone' => $request->input('phone'),
|
'password',
|
||||||
'password' => Hash::make($request->input('password'))
|
'display_name',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$userData['password'] = Hash::make($userData['password']);
|
||||||
|
|
||||||
|
$user = User::where('email', $request->input('email'))
|
||||||
|
->whereNull('password')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($user === null) {
|
||||||
|
$user = User::create($userData);
|
||||||
|
} else {
|
||||||
|
unset($userData['email']);
|
||||||
|
$user->update($userData);
|
||||||
|
}//end if
|
||||||
|
|
||||||
$code = $user->codes()->create([
|
$code = $user->codes()->create([
|
||||||
'action' => 'verify-email',
|
'action' => 'verify-email',
|
||||||
]);
|
]);
|
||||||
@@ -168,35 +187,15 @@ class UserController extends ApiController
|
|||||||
}//end try
|
}//end try
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends an email with all the usernames registered at that address
|
|
||||||
*
|
|
||||||
* @param UserForgotUsernameRequest $request The forgot username request.
|
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
|
||||||
public function forgotUsername(UserForgotUsernameRequest $request)
|
|
||||||
{
|
|
||||||
$users = User::where('email', $request->input('email'))->whereNotNull('email_verified_at')->get();
|
|
||||||
if ($users->count() > 0) {
|
|
||||||
dispatch((new SendEmailJob(
|
|
||||||
$users->first()->email,
|
|
||||||
new ForgotUsername($users->pluck('username')->toArray())
|
|
||||||
)))->onQueue('mail');
|
|
||||||
return $this->respondNoContent();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->respondJson(['message' => 'Username send to the email address if registered']);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a new reset password code
|
* Generates a new reset password code
|
||||||
*
|
*
|
||||||
* @param UserForgotPasswordRequest $request The reset password request.
|
* @param \App\Http\Requests\UserForgotPasswordRequest $request The reset password request.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function forgotPassword(UserForgotPasswordRequest $request)
|
public function forgotPassword(UserForgotPasswordRequest $request)
|
||||||
{
|
{
|
||||||
$user = User::where('username', $request->input('username'))->first();
|
$user = User::where('email', $request->input('email'))->first();
|
||||||
if ($user !== null) {
|
if ($user !== null) {
|
||||||
$user->codes()->where('action', 'reset-password')->delete();
|
$user->codes()->where('action', 'reset-password')->delete();
|
||||||
$code = $user->codes()->create([
|
$code = $user->codes()->create([
|
||||||
@@ -213,7 +212,7 @@ class UserController extends ApiController
|
|||||||
/**
|
/**
|
||||||
* Resets a user password
|
* Resets a user password
|
||||||
*
|
*
|
||||||
* @param UserResetPasswordRequest $request The reset password request.
|
* @param \App\Http\Requests\UserResetPasswordRequest $request The reset password request.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function resetPassword(UserResetPasswordRequest $request)
|
public function resetPassword(UserResetPasswordRequest $request)
|
||||||
@@ -240,14 +239,14 @@ class UserController extends ApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
return $this->respondError([
|
return $this->respondError([
|
||||||
'code' => 'The code was not found or has expired'
|
'code' => 'The code was not found or has expired.'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Verify an email code
|
* Verify an email code
|
||||||
*
|
*
|
||||||
* @param UserVerifyEmailRequest $request The verify email request.
|
* @param \App\Http\Requests\UserVerifyEmailRequest $request The verify email request.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function verifyEmail(UserVerifyEmailRequest $request)
|
public function verifyEmail(UserVerifyEmailRequest $request)
|
||||||
@@ -278,21 +277,20 @@ class UserController extends ApiController
|
|||||||
}//end if
|
}//end if
|
||||||
|
|
||||||
return $this->respondWithErrors([
|
return $this->respondWithErrors([
|
||||||
'code' => 'The code was not found or has expired'
|
'code' => 'The code was not found or has expired.'
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resend a new verify email
|
* Resend a new verify email
|
||||||
*
|
*
|
||||||
* @param UserResendVerifyEmailRequest $request The resend verify email request.
|
* @param \App\Http\Requests\UserResendVerifyEmailRequest $request The resend verify email request.
|
||||||
* @return \Illuminate\Http\Response
|
|
||||||
*/
|
*/
|
||||||
public function resendVerifyEmail(UserResendVerifyEmailRequest $request)
|
public function resendVerifyEmail(UserResendVerifyEmailRequest $request): JsonResponse
|
||||||
{
|
{
|
||||||
UserCode::clearExpired();
|
UserCode::clearExpired();
|
||||||
|
|
||||||
$user = User::where('username', $request->input('username'))->first();
|
$user = User::where('email', $request->input('email'))->first();
|
||||||
if ($user !== null) {
|
if ($user !== null) {
|
||||||
$code = $user->codes()->where('action', 'verify-email')->first();
|
$code = $user->codes()->where('action', 'verify-email')->first();
|
||||||
$code->regenerate();
|
$code->regenerate();
|
||||||
@@ -312,12 +310,12 @@ class UserController extends ApiController
|
|||||||
/**
|
/**
|
||||||
* Resend verification email
|
* Resend verification email
|
||||||
*
|
*
|
||||||
* @param UserResendVerifyEmailRequest $request The resend user request.
|
* @param \App\Http\Requests\UserResendVerifyEmailRequest $request The resend user request.
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function resendVerifyEmailCode(UserResendVerifyEmailRequest $request)
|
public function resendVerifyEmailCode(UserResendVerifyEmailRequest $request)
|
||||||
{
|
{
|
||||||
$user = User::where('username', $request->input('username'))->first();
|
$user = User::where('email', $request->input('email'))->first();
|
||||||
if ($user !== null) {
|
if ($user !== null) {
|
||||||
$user->codes()->where('action', 'verify-email')->delete();
|
$user->codes()->where('action', 'verify-email')->delete();
|
||||||
|
|
||||||
@@ -334,4 +332,28 @@ class UserController extends ApiController
|
|||||||
|
|
||||||
return $this->respondNotFound();
|
return $this->respondNotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return a JSON event list of a user.
|
||||||
|
*
|
||||||
|
* @param Request $request The http request.
|
||||||
|
* @param User $user The specified user.
|
||||||
|
*/
|
||||||
|
public function eventList(Request $request, User $user): JsonResponse
|
||||||
|
{
|
||||||
|
if ($request->user() !== null && ($request->user() === $user || $request->user()->hasPermission('admin/events') === true)) {
|
||||||
|
$collection = $user->events;
|
||||||
|
$total = $collection->count();
|
||||||
|
|
||||||
|
$collection = EventConductor::collection($request, $collection);
|
||||||
|
return $this->respondAsResource(
|
||||||
|
$collection,
|
||||||
|
['isCollection' => true,
|
||||||
|
'appendData' => ['total' => $total]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return $this->respondForbidden();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,13 +3,11 @@
|
|||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
||||||
use Illuminate\Foundation\Validation\ValidatesRequests;
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||||||
use Illuminate\Routing\Controller as BaseController;
|
use Illuminate\Routing\Controller as BaseController;
|
||||||
|
|
||||||
class Controller extends BaseController
|
class Controller extends BaseController
|
||||||
{
|
{
|
||||||
use AuthorizesRequests;
|
use AuthorizesRequests;
|
||||||
use DispatchesJobs;
|
|
||||||
use ValidatesRequests;
|
use ValidatesRequests;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ class Kernel extends HttpKernel
|
|||||||
|
|
||||||
'api' => [
|
'api' => [
|
||||||
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
|
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
|
||||||
'throttle:api',
|
\Illuminate\Routing\Middleware\ThrottleRequests::class . ':api',
|
||||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||||
// \App\Http\Middleware\ForceJsonResponse::class,
|
// \App\Http\Middleware\ForceJsonResponse::class,
|
||||||
'useSanctumGuard',
|
'useSanctumGuard',
|
||||||
@@ -49,13 +49,13 @@ class Kernel extends HttpKernel
|
|||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The application's route middleware.
|
* The application's middleware aliases.
|
||||||
*
|
*
|
||||||
* These middleware may be assigned to groups or used individually.
|
* Aliases may be used to conveniently assign middleware to routes and groups.
|
||||||
*
|
*
|
||||||
* @var array<string, class-string|string>
|
* @var array<string, class-string|string>
|
||||||
*/
|
*/
|
||||||
protected $routeMiddleware = [
|
protected $middlewareAliases = [
|
||||||
'auth' => \App\Http\Middleware\Authenticate::class,
|
'auth' => \App\Http\Middleware\Authenticate::class,
|
||||||
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
|
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
|
||||||
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
|
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
|
||||||
|
|||||||
@@ -10,9 +10,8 @@ class Authenticate extends Middleware
|
|||||||
* Get the path the user should be redirected to when they are not authenticated.
|
* Get the path the user should be redirected to when they are not authenticated.
|
||||||
*
|
*
|
||||||
* @param mixed $request Request.
|
* @param mixed $request Request.
|
||||||
* @return string|null
|
|
||||||
*/
|
*/
|
||||||
protected function redirectTo(mixed $request)
|
protected function redirectTo(mixed $request): ?string
|
||||||
{
|
{
|
||||||
if ($request->expectsJson() === false) {
|
if ($request->expectsJson() === false) {
|
||||||
return route('login');
|
return route('login');
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Middleware;
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
@@ -10,11 +11,9 @@ class ForceJsonResponse
|
|||||||
/**
|
/**
|
||||||
* Handle an incoming request.
|
* Handle an incoming request.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
|
||||||
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
||||||
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
|
|
||||||
*/
|
*/
|
||||||
public function handle(Request $request, Closure $next)
|
public function handle(Request $request, Closure $next): Response
|
||||||
{
|
{
|
||||||
$request->headers->set('Accept', 'application/json');
|
$request->headers->set('Accept', 'application/json');
|
||||||
return $next($request);
|
return $next($request);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Middleware;
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Models\Analytics;
|
use App\Models\Analytics;
|
||||||
@@ -11,18 +12,16 @@ class LogRequest
|
|||||||
/**
|
/**
|
||||||
* Handle an incoming request.
|
* Handle an incoming request.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
|
||||||
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
||||||
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
|
|
||||||
*/
|
*/
|
||||||
public function handle(Request $request, Closure $next)
|
public function handle(Request $request, Closure $next): Response
|
||||||
{
|
{
|
||||||
// Make it an after middleware
|
// Make it an after middleware
|
||||||
$response = $next($request);
|
$response = $next($request);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Analytics::create([
|
Analytics::createWithSession([
|
||||||
'type' => 'pageview',
|
'type' => 'apirequest',
|
||||||
'attribute' => $request->path(),
|
'attribute' => $request->path(),
|
||||||
'useragent' => $request->userAgent(),
|
'useragent' => $request->userAgent(),
|
||||||
'ip' => $request->ip(),
|
'ip' => $request->ip(),
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Middleware;
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use App\Providers\RouteServiceProvider;
|
use App\Providers\RouteServiceProvider;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
@@ -13,11 +14,10 @@ class RedirectIfAuthenticated
|
|||||||
* Handle an incoming request.
|
* Handle an incoming request.
|
||||||
*
|
*
|
||||||
* @param Request $request Request.
|
* @param Request $request Request.
|
||||||
* @param Closure(Request): (Response|RedirectResponse) $next Next.
|
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
||||||
* @param string|null ...$guards Guards.
|
* @param string|null ...$guards Guards.
|
||||||
* @return Response|RedirectResponse
|
|
||||||
*/
|
*/
|
||||||
public function handle(Request $request, Closure $next, ...$guards)
|
public function handle(Request $request, Closure $next, string ...$guards): Response
|
||||||
{
|
{
|
||||||
$guards = empty($guards) === true ? [null] : $guards;
|
$guards = empty($guards) === true ? [null] : $guards;
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class TrustHosts extends Middleware
|
|||||||
*
|
*
|
||||||
* @return array<int, string|null>
|
* @return array<int, string|null>
|
||||||
*/
|
*/
|
||||||
public function hosts()
|
public function hosts(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
$this->allSubdomainsOfApplicationUrl(),
|
$this->allSubdomainsOfApplicationUrl(),
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Middleware;
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
@@ -11,11 +12,9 @@ class UseSanctumGuard
|
|||||||
/**
|
/**
|
||||||
* Handle an incoming request.
|
* Handle an incoming request.
|
||||||
*
|
*
|
||||||
* @param \Illuminate\Http\Request $request
|
|
||||||
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
||||||
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
|
|
||||||
*/
|
*/
|
||||||
public function handle(Request $request, Closure $next)
|
public function handle(Request $request, Closure $next): Response
|
||||||
{
|
{
|
||||||
Auth::shouldUse('sanctum');
|
Auth::shouldUse('sanctum');
|
||||||
return $next($request);
|
return $next($request);
|
||||||
|
|||||||
35
app/Http/Requests/AnalyticsRequest.php
Normal file
35
app/Http/Requests/AnalyticsRequest.php
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class AnalyticsRequest extends BaseRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to POST requests.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function postRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'type' => 'required|string',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to PUT request.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function putRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'type' => 'string',
|
||||||
|
'useragent' => 'string',
|
||||||
|
'ip' => 'ipv4|ipv6',
|
||||||
|
'session' => 'number',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
46
app/Http/Requests/ArticleRequest.php
Normal file
46
app/Http/Requests/ArticleRequest.php
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class ArticleRequest extends BaseRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to POST requests.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function postRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'slug' => 'required|string|min:6|unique:articles',
|
||||||
|
'title' => 'required|string|min:6|max:255',
|
||||||
|
'publish_at' => 'required|date',
|
||||||
|
'user_id' => 'required|uuid|exists:users,id',
|
||||||
|
'content' => 'required|string|min:6',
|
||||||
|
'hero' => 'required|uuid|exists:media,id',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to PUT request.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function putRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'slug' => [
|
||||||
|
'string',
|
||||||
|
'min:6',
|
||||||
|
Rule::unique('articles')->ignoreModel($this->article),
|
||||||
|
],
|
||||||
|
'title' => 'string|min:6|max:255',
|
||||||
|
'publish_at' => 'date',
|
||||||
|
'user_id' => 'uuid|exists:users,id',
|
||||||
|
'content' => 'string|min:6',
|
||||||
|
'hero' => 'uuid|exists:media,id',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,10 +11,10 @@ class AuthLoginRequest extends FormRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'username' => 'required|string|min:6|max:255',
|
'email' => 'required|string|min:6|max:255',
|
||||||
'password' => 'required|string|min:6',
|
'password' => 'required|string|min:6',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,15 @@ class BaseRequest extends FormRequest
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Determine if the user is authorized to make this request.
|
* Determine if the user is authorized to make this request.
|
||||||
*
|
|
||||||
* @return boolean
|
|
||||||
*/
|
*/
|
||||||
public function authorize()
|
public function authorize(): bool
|
||||||
{
|
{
|
||||||
if (method_exists($this, 'postAuthorize') === true && request()->isMethod('post') === true) {
|
if (request()->isMethod('post') === true && method_exists($this, 'postAuthorize') === true) {
|
||||||
return $this->postAuthorize();
|
return $this->postAuthorize();
|
||||||
} elseif (method_exists($this, 'putAuthorize') === true && request()->isMethod('put') === true) {
|
} elseif ((request()->isMethod('put') === true || request()->isMethod('patch') === true) && method_exists($this, 'putAuthorize') === true) {
|
||||||
return $this->putAuthorize();
|
return $this->putAuthorize();
|
||||||
|
} elseif (request()->isMethod('delete') === true && method_exists($this, 'destroyAuthorize') === true) {
|
||||||
|
return $this->deleteAuthorize();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -28,7 +28,7 @@ class BaseRequest extends FormRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules(): array
|
||||||
{
|
{
|
||||||
$rules = [];
|
$rules = [];
|
||||||
|
|
||||||
@@ -38,8 +38,8 @@ class BaseRequest extends FormRequest
|
|||||||
|
|
||||||
if (method_exists($this, 'postRules') === true && request()->isMethod('post') === true) {
|
if (method_exists($this, 'postRules') === true && request()->isMethod('post') === true) {
|
||||||
$rules = $this->mergeRules($rules, $this->postRules());
|
$rules = $this->mergeRules($rules, $this->postRules());
|
||||||
} elseif (method_exists($this, 'putRules') === true && request()->isMethod('put') === true) {
|
} elseif (method_exists($this, 'putRules') === true && (request()->isMethod('put') === true || request()->isMethod('patch') === true)) {
|
||||||
$rules = $this->mergeRules($rules, $this->postRules());
|
$rules = $this->mergeRules($rules, $this->putRules());
|
||||||
} elseif (method_exists($this, 'destroyRules') === true && request()->isMethod('delete') === true) {
|
} elseif (method_exists($this, 'destroyRules') === true && request()->isMethod('delete') === true) {
|
||||||
$rules = $this->mergeRules($rules, $this->destroyRules());
|
$rules = $this->mergeRules($rules, $this->destroyRules());
|
||||||
}
|
}
|
||||||
@@ -52,9 +52,8 @@ class BaseRequest extends FormRequest
|
|||||||
*
|
*
|
||||||
* @param array $collection1 The first collection of rules.
|
* @param array $collection1 The first collection of rules.
|
||||||
* @param array $collection2 The second collection of rules to merge.
|
* @param array $collection2 The second collection of rules to merge.
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
private function mergeRules(array $collection1, array $collection2)
|
private function mergeRules(array $collection1, array $collection2): array
|
||||||
{
|
{
|
||||||
$rules = [];
|
$rules = [];
|
||||||
|
|
||||||
@@ -73,8 +72,8 @@ class BaseRequest extends FormRequest
|
|||||||
|
|
||||||
if (is_array($collection2[$key]) === true) {
|
if (is_array($collection2[$key]) === true) {
|
||||||
$key_ruleset = array_merge($key_ruleset, $collection2[$key]);
|
$key_ruleset = array_merge($key_ruleset, $collection2[$key]);
|
||||||
} elseif (is_string($collection1[$key]) === true) {
|
} elseif (is_string($collection2[$key]) === true) {
|
||||||
$key_ruleset = array_merge($key_ruleset, explode('|', $collection1[$key]));
|
$key_ruleset = array_merge($key_ruleset, explode('|', $collection2[$key]));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($key_ruleset) > 0) {
|
if (count($key_ruleset) > 0) {
|
||||||
|
|||||||
@@ -12,13 +12,13 @@ class ContactSendRequest extends FormRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'name' => 'required|max:255',
|
'name' => 'required|max:255',
|
||||||
'email' => 'required|email|max:255',
|
'email' => 'required|email|max:255',
|
||||||
'content' => 'required|max:2000',
|
'content' => 'required|max:2000',
|
||||||
'captcha_token' => [new Recaptcha()],
|
// 'captcha_token' => [new Recaptcha()],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,37 +2,16 @@
|
|||||||
|
|
||||||
namespace App\Http\Requests;
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
class EventRequest extends BaseRequest
|
class EventRequest extends BaseRequest
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* Determine if the user is authorized to make this request.
|
|
||||||
*
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public function postAuthorize()
|
|
||||||
{
|
|
||||||
return $this->user()?->hasPermission('admin/events');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine if the user is authorized to make this request.
|
|
||||||
*
|
|
||||||
* @return boolean
|
|
||||||
*/
|
|
||||||
public function putAuthorize()
|
|
||||||
{
|
|
||||||
return $this->user()?->hasPermission('admin/events');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply the base rules to this request
|
* Apply the base rules to this request
|
||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function baseRules()
|
public function baseRules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'title' => 'min:6',
|
'title' => 'min:6',
|
||||||
@@ -44,14 +23,15 @@ class EventRequest extends BaseRequest
|
|||||||
'end_at' => 'date|after:start_date',
|
'end_at' => 'date|after:start_date',
|
||||||
'publish_at' => 'date|nullable',
|
'publish_at' => 'date|nullable',
|
||||||
'status' => [
|
'status' => [
|
||||||
Rule::in(['draft', 'open', 'closed', 'cancelled']),
|
Rule::in(['draft', 'soon', 'open', 'closed', 'cancelled']),
|
||||||
],
|
],
|
||||||
'registration_type' => [
|
'registration_type' => [
|
||||||
Rule::in(['none', 'email', 'link']),
|
Rule::in(['none', 'email', 'link', 'message']),
|
||||||
],
|
],
|
||||||
'registration_data' => [
|
'registration_data' => [
|
||||||
Rule::when(strcasecmp('email', $this->attributes->get('registration_type')) == 0, 'required|email'),
|
Rule::when(strcasecmp('email', $this->attributes->get('registration_type')) == 0, 'required|email'),
|
||||||
Rule::when(strcasecmp('link', $this->attributes->get('registration_type')) == 0, 'required|url')
|
Rule::when(strcasecmp('link', $this->attributes->get('registration_type')) == 0, 'required|url'),
|
||||||
|
Rule::when(strcasecmp('message', $this->attributes->get('registration_type')) == 0, 'required|message'),
|
||||||
],
|
],
|
||||||
'hero' => 'uuid|exists:media,id',
|
'hero' => 'uuid|exists:media,id',
|
||||||
];
|
];
|
||||||
@@ -62,7 +42,7 @@ class EventRequest extends BaseRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
protected function postRules()
|
protected function postRules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'title' => 'required',
|
'title' => 'required',
|
||||||
|
|||||||
8
app/Http/Requests/MediaRequest.php
Normal file
8
app/Http/Requests/MediaRequest.php
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
class MediaRequest extends BaseRequest
|
||||||
|
{
|
||||||
|
/* empty */
|
||||||
|
}
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Requests;
|
|
||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
|
|
||||||
class MediaStoreRequest extends FormRequest
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Get the validation rules that apply to the request.
|
|
||||||
*
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function rules()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
//
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Requests;
|
|
||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
|
|
||||||
class MediaUpdateRequest extends FormRequest
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Get the validation rules that apply to the request.
|
|
||||||
*
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function rules()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
//
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Requests;
|
|
||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
|
|
||||||
class PostStoreRequest extends FormRequest
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Get the validation rules that apply to the request.
|
|
||||||
*
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function rules()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'slug' => 'string|min:6|unique:posts',
|
|
||||||
'title' => 'string|min:6|max:255',
|
|
||||||
'publish_at' => 'date',
|
|
||||||
'user_id' => 'uuid|exists:users,id',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Requests;
|
|
||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
use Illuminate\Validation\Rule;
|
|
||||||
|
|
||||||
class PostUpdateRequest extends FormRequest
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Get the validation rules that apply to the request.
|
|
||||||
*
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function rules()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'slug' => [
|
|
||||||
'string',
|
|
||||||
'min:6',
|
|
||||||
Rule::unique('posts')->ignoreModel($this->post),
|
|
||||||
],
|
|
||||||
'title' => 'string|min:6|max:255',
|
|
||||||
'publish_at' => 'date',
|
|
||||||
'user_id' => 'uuid|exists:users,id',
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
36
app/Http/Requests/ShortlinkRequest.php
Normal file
36
app/Http/Requests/ShortlinkRequest.php
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
|
||||||
|
class ShortlinkRequest extends BaseRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Apply the additional POST base rules to this request
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function postRules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'code' => 'required|string|max:255|min:2|unique:shortlinks',
|
||||||
|
'url' => 'required|string|max:255|min:2',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to PUT request.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function putRules(): array
|
||||||
|
{
|
||||||
|
$shortlink = $this->route('shortlink');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'code' => ['required', 'string', 'max:255', 'min:2', Rule::unique('shortlinks')->ignore($shortlink->id)],
|
||||||
|
'url' => 'required|string|max:255|min:2',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,11 +11,11 @@ class SubscriptionRequest extends BaseRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function postRules()
|
public function postRules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'email' => 'required|email',
|
'email' => 'required|email|unique:subscriptions',
|
||||||
'captcha_token' => [new Recaptcha()],
|
// 'captcha_token' => [new Recaptcha()],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,11 +24,21 @@ class SubscriptionRequest extends BaseRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function destroyRules()
|
public function destroyRules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'email' => 'required|email',
|
'email' => 'required|email',
|
||||||
'captcha_token' => [new Recaptcha()],
|
// 'captcha_token' => [new Recaptcha()],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the custom error messages.
|
||||||
|
*/
|
||||||
|
public function messages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'email.unique' => 'This email address has already subscribed',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ class UserForgotPasswordRequest extends FormRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'username' => 'required|exists:users,username',
|
'email' => 'required|exists:users,email',
|
||||||
'captcha_token' => [new Recaptcha()],
|
// 'captcha_token' => [new Recaptcha()],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Requests;
|
|
||||||
|
|
||||||
use App\Rules\Recaptcha;
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
|
|
||||||
class UserForgotUsernameRequest extends FormRequest
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Get the validation rules that apply to the request.
|
|
||||||
*
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function rules()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'email' => 'required|email|max:255',
|
|
||||||
'captcha_token' => [new Recaptcha()],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\Http\Requests;
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use App\Rules\Uniqueish;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
|
||||||
class UserRegisterRequest extends FormRequest
|
class UserRegisterRequest extends FormRequest
|
||||||
@@ -11,13 +12,11 @@ class UserRegisterRequest extends FormRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'first_name' => 'required|string|max:255',
|
'display_name' => ['required','string','max:255', new Uniqueish('users')],
|
||||||
'last_name' => 'required|string|max:255',
|
'email' => 'required|string|email|max:255|unique:users',
|
||||||
'email' => 'required|string|email|max:255',
|
|
||||||
'username' => 'required|string|min:4|max:255|unique:users',
|
|
||||||
'password' => 'required|string|min:8',
|
'password' => 'required|string|min:8',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
107
app/Http/Requests/UserRequest.php
Normal file
107
app/Http/Requests/UserRequest.php
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use App\Rules\RequiredIfAny;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Validation\Rules\RequiredIf;
|
||||||
|
use App\Rules\Uniqueish;
|
||||||
|
use Illuminate\Support\Arr;
|
||||||
|
|
||||||
|
class UserRequest extends BaseRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Apply the additional POST base rules to this request
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function postRules(): array
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
$isAdminUser = $user->hasPermission('admin/users');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'first_name' => ($isAdminUser === true ? 'required_with:last_name,display_name,phone' : 'required') . '|string|max:255|min:2',
|
||||||
|
'last_name' => ($isAdminUser === true ? 'required_with:first_name,display_name,phone' : 'required') . '|string|max:255|min:2',
|
||||||
|
'display_name' => [
|
||||||
|
$isAdminUser === true ? 'required_with:first_name,last_name,phone' : 'required',
|
||||||
|
'string',
|
||||||
|
'max:255',
|
||||||
|
new Uniqueish('users')
|
||||||
|
],
|
||||||
|
'email' => 'required|string|email|max:255|unique:users',
|
||||||
|
'phone' => ['string', 'regex:/^(\+|00)?[0-9][0-9 \-\(\)\.]{7,32}$/'],
|
||||||
|
'email_verified_at' => 'date'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to PUT request.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function putRules(): array
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
$ruleUser = $this->route('user');
|
||||||
|
$isAdminUser = $user->hasPermission('admin/users');
|
||||||
|
|
||||||
|
$requiredIfFieldsPresent = function (array $fields) use ($ruleUser): RequiredIf {
|
||||||
|
return new RequiredIf(function () use ($fields, $ruleUser) {
|
||||||
|
$input = $this->all();
|
||||||
|
$values = Arr::only($input, $fields);
|
||||||
|
|
||||||
|
foreach ($values as $key => $value) {
|
||||||
|
if ($value !== null && $value !== '') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$fields = array_diff($fields, array_keys($values));
|
||||||
|
|
||||||
|
foreach ($fields as $field) {
|
||||||
|
if ($ruleUser->$field !== '') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return [
|
||||||
|
'first_name' => [
|
||||||
|
'sometimes',
|
||||||
|
$isAdminUser === true ? $requiredIfFieldsPresent(['last_name', 'display_name', 'phone']) : 'required',
|
||||||
|
'string',
|
||||||
|
'between:2,255',
|
||||||
|
],
|
||||||
|
'last_name' => [
|
||||||
|
'sometimes',
|
||||||
|
$isAdminUser === true ? $requiredIfFieldsPresent(['first_name', 'last_name', 'phone']) : 'required',
|
||||||
|
'string',
|
||||||
|
'between:2,255',
|
||||||
|
],
|
||||||
|
'display_name' => [
|
||||||
|
'sometimes',
|
||||||
|
$isAdminUser === true ? $requiredIfFieldsPresent(['first_name', 'display_name', 'phone']) : 'required',
|
||||||
|
'string',
|
||||||
|
'between:2,255',
|
||||||
|
(new Uniqueish('users', 'display_name'))->ignore($ruleUser->id)
|
||||||
|
],
|
||||||
|
'email' => [
|
||||||
|
'string',
|
||||||
|
'email',
|
||||||
|
'max:255',
|
||||||
|
Rule::unique('users')->ignore($ruleUser->id)->when(
|
||||||
|
$this->email !== $ruleUser->email,
|
||||||
|
function ($query) {
|
||||||
|
return $query->where('email', $this->email);
|
||||||
|
}
|
||||||
|
),
|
||||||
|
],
|
||||||
|
'phone' => ['nullable', 'regex:/^(\+|00)?[0-9][0-9 \-\(\)\.]{7,32}$/'],
|
||||||
|
'password' => "nullable|string|min:8"
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,11 +12,11 @@ class UserResendVerifyEmailRequest extends FormRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'username' => 'required|exists:users,username',
|
'email' => 'required|exists:users,email',
|
||||||
'captcha_token' => [new Recaptcha()],
|
// 'captcha_token' => [new Recaptcha()],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,12 +12,12 @@ class UserResetPasswordRequest extends FormRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'code' => 'required|digits:6',
|
'code' => 'required|digits:6',
|
||||||
'password' => 'required|string|min:8',
|
'password' => 'required|string|min:8',
|
||||||
'captcha_token' => [new Recaptcha()],
|
// 'captcha_token' => [new Recaptcha()],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Requests;
|
|
||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
|
|
||||||
class UserStoreRequest extends FormRequest
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Get the validation rules that apply to the request.
|
|
||||||
*
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function rules()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'username' => 'required|string|max:255|min:4|unique:users',
|
|
||||||
'first_name' => 'required|string|max:255|min:2',
|
|
||||||
'last_name' => 'required|string|max:255|min:2',
|
|
||||||
'email' => 'required|string|email|max:255',
|
|
||||||
'phone' => ['string', 'regex:/^(\+|00)?[0-9][0-9 \-\(\)\.]{7,32}$/'],
|
|
||||||
'email_verified_at' => 'date'
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Http\Requests;
|
|
||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
|
||||||
|
|
||||||
class UserUpdateRequest extends FormRequest
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Get the validation rules that apply to the request.
|
|
||||||
*
|
|
||||||
* @return array<string, mixed>
|
|
||||||
*/
|
|
||||||
public function rules()
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'username' => 'string|max:255|min:6|unique:users',
|
|
||||||
'first_name' => 'string|max:255|min:2',
|
|
||||||
'last_name' => 'string|max:255|min:2',
|
|
||||||
'email' => 'string|email|max:255',
|
|
||||||
'phone' => ['nullable','regex:/^(\+|00)?[0-9][0-9 \-\(\)\.]{7,32}$/'],
|
|
||||||
'password' => 'string|min:8'
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -12,11 +12,11 @@ class UserVerifyEmailRequest extends FormRequest
|
|||||||
*
|
*
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
public function rules()
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'code' => 'required|digits:6',
|
'code' => 'required|digits:6',
|
||||||
'captcha_token' => [new Recaptcha()],
|
// 'captcha_token' => [new Recaptcha()],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
82
app/Jobs/MoveMediaJob.php
Normal file
82
app/Jobs/MoveMediaJob.php
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
use App\Models\Media;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
|
||||||
|
class MoveMediaJob implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable;
|
||||||
|
use InteractsWithQueue;
|
||||||
|
use Queueable;
|
||||||
|
use SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Media item
|
||||||
|
*
|
||||||
|
* @var Media
|
||||||
|
*/
|
||||||
|
public $media;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* New storage ID
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $newStorage;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @param Media $media The media model.
|
||||||
|
* @param string $newStorage The new storage ID.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(Media $media, string $newStorage)
|
||||||
|
{
|
||||||
|
$this->media = $media;
|
||||||
|
$this->newStorage = $newStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*/
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
// Don't continue if the media is already on the new storage disk
|
||||||
|
if ($this->media->storage === $this->newStorage) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->media->status = 'Moving file';
|
||||||
|
$this->media->save();
|
||||||
|
|
||||||
|
$files = ["/{$this->media->name}"];
|
||||||
|
if (empty($this->media->variants) === false) {
|
||||||
|
foreach ($this->media->variants as $variant => $name) {
|
||||||
|
$files[] = "/{$name}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->media->invalidateCFCache();
|
||||||
|
|
||||||
|
// Move the files from the old storage disk to the new storage disk
|
||||||
|
foreach ($files as $file) {
|
||||||
|
Storage::disk($this->newStorage)->put($file, Storage::disk($this->media->storage)->get($file));
|
||||||
|
Storage::disk($this->media->storage)->delete($file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the media model with the new storage and save it to the database
|
||||||
|
$this->media->storage = $this->newStorage;
|
||||||
|
$this->media->status = 'OK';
|
||||||
|
$this->media->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -47,10 +47,8 @@ class SendEmailJob implements ShouldQueue
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the job.
|
* Execute the job.
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle(): void
|
||||||
{
|
{
|
||||||
Mail::to($this->to)->send($this->mailable);
|
Mail::to($this->to)->send($this->mailable);
|
||||||
}
|
}
|
||||||
|
|||||||
177
app/Jobs/StoreUploadedFileJob.php
Normal file
177
app/Jobs/StoreUploadedFileJob.php
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Jobs;
|
||||||
|
|
||||||
|
use App\Models\Media;
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use SplFileInfo;
|
||||||
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
|
use Intervention\Image\Facades\Image;
|
||||||
|
use Spatie\ImageOptimizer\OptimizerChainFactory;
|
||||||
|
|
||||||
|
class StoreUploadedFileJob implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable;
|
||||||
|
use InteractsWithQueue;
|
||||||
|
use Queueable;
|
||||||
|
use SerializesModels;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Media item
|
||||||
|
*
|
||||||
|
* @var Media
|
||||||
|
*/
|
||||||
|
protected $media;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uploaded file item
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $uploadedFilePath;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replace existing files
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $replaceExisting;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new job instance.
|
||||||
|
*
|
||||||
|
* @param Media $media The media model.
|
||||||
|
* @param string $filePath The uploaded file.
|
||||||
|
* @param boolean $replaceExisting Replace existing files.
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(Media $media, string $filePath, bool $replaceExisting = true)
|
||||||
|
{
|
||||||
|
$this->media = $media;
|
||||||
|
$this->uploadedFilePath = $filePath;
|
||||||
|
$this->replaceExisting = $replaceExisting;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*/
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$storageDisk = $this->media->storage;
|
||||||
|
$fileName = $this->media->name;
|
||||||
|
|
||||||
|
try {
|
||||||
|
$this->media->status = "Uploading to CDN";
|
||||||
|
$this->media->save();
|
||||||
|
|
||||||
|
if (strlen($this->uploadedFilePath) > 0) {
|
||||||
|
if (Storage::disk($storageDisk)->exists($fileName) === false || $this->replaceExisting === true) {
|
||||||
|
Storage::disk($storageDisk)->putFileAs('/', new SplFileInfo($this->uploadedFilePath), $fileName);
|
||||||
|
Log::info("uploading file {$storageDisk} / {$fileName} / {$this->uploadedFilePath}");
|
||||||
|
} else {
|
||||||
|
Log::info("file {$fileName} already exists in {$storageDisk} / {$this->uploadedFilePath}. Not replacing file and using local {$fileName} for variants.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (Storage::disk($storageDisk)->exists($fileName) === true) {
|
||||||
|
Log::info("file {$fileName} already exists in {$storageDisk} / {$this->uploadedFilePath}. No local {$fileName} for variants, downloading from CDN.");
|
||||||
|
$readStream = Storage::disk($storageDisk)->readStream($fileName);
|
||||||
|
$tempFilePath = tempnam(sys_get_temp_dir(), 'download-');
|
||||||
|
$writeStream = fopen($tempFilePath, 'w');
|
||||||
|
while (feof($readStream) !== true) {
|
||||||
|
fwrite($writeStream, fread($readStream, 8192));
|
||||||
|
}
|
||||||
|
fclose($readStream);
|
||||||
|
fclose($writeStream);
|
||||||
|
$this->uploadedFilePath = $tempFilePath;
|
||||||
|
} else {
|
||||||
|
$errorStr = "cannot upload file {$storageDisk} / {$fileName} / {$this->uploadedFilePath} as temp file is empty";
|
||||||
|
Log::info($errorStr);
|
||||||
|
throw new \Exception($errorStr);
|
||||||
|
}
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
if (strpos($this->media->mime_type, 'image/') === 0) {
|
||||||
|
$this->media->status = "Optimizing image";
|
||||||
|
$this->media->save();
|
||||||
|
|
||||||
|
// Generate additional image sizes
|
||||||
|
$sizes = Media::getTypeVariants('image');
|
||||||
|
|
||||||
|
$originalImage = Image::make($this->uploadedFilePath);
|
||||||
|
|
||||||
|
$dimensions = [$originalImage->getWidth(), $originalImage->getHeight()];
|
||||||
|
$this->media->dimensions = implode('x', $dimensions);
|
||||||
|
|
||||||
|
foreach ($sizes as $variantName => $size) {
|
||||||
|
$postfix = "{$size['width']}x{$size['height']}";
|
||||||
|
if ($variantName === 'scaled') {
|
||||||
|
$postfix = 'scaled';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($this->media->variants) === true && array_key_exists($postfix, $this->media->variants) === true && Storage::disk($storageDisk)->exists($this->media->variants[$postfix]) === true && $this->replaceExisting === true) {
|
||||||
|
Storage::disk($storageDisk)->delete($this->media->variants[$postfix]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$newFilename = pathinfo($this->media->name, PATHINFO_FILENAME) . "-$postfix.webp";
|
||||||
|
|
||||||
|
if (Storage::disk($storageDisk)->exists($newFilename) === false || $this->replaceExisting === true) {
|
||||||
|
// Get the largest available variant
|
||||||
|
if ($dimensions[0] >= $size['width'] && $dimensions[1] >= $size['height']) {
|
||||||
|
// Store the variant in the variants array
|
||||||
|
$variants[$variantName] = $newFilename;
|
||||||
|
|
||||||
|
// Resize the image to the variant size if its dimensions are greater than the specified size
|
||||||
|
$image = clone $originalImage;
|
||||||
|
|
||||||
|
$imageSize = $image->getSize();
|
||||||
|
if ($imageSize->getWidth() > $size['width'] || $imageSize->getHeight() > $size['height']) {
|
||||||
|
$image->resize($size['width'], $size['height'], function ($constraint) {
|
||||||
|
$constraint->aspectRatio();
|
||||||
|
$constraint->upsize();
|
||||||
|
});
|
||||||
|
$image->resizeCanvas($size['width'], $size['height'], 'center', false, '#FFFFFF');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optimize and store image
|
||||||
|
$tempImagePath = tempnam(sys_get_temp_dir(), 'optimize');
|
||||||
|
$image->encode('webp', 75)->save($tempImagePath);
|
||||||
|
Storage::disk($storageDisk)->putFileAs('/', new SplFileInfo($tempImagePath), $newFilename);
|
||||||
|
unlink($tempImagePath);
|
||||||
|
}//end if
|
||||||
|
} else {
|
||||||
|
Log::info("variant {$variantName} already exists for file {$fileName}");
|
||||||
|
}//end if
|
||||||
|
}//end foreach
|
||||||
|
|
||||||
|
// Set missing variants to the largest available variant
|
||||||
|
foreach ($sizes as $variantName => $size) {
|
||||||
|
if (isset($variants[$variantName]) === false) {
|
||||||
|
$variants[$variantName] = $this->media->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->media->variants = $variants;
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
if (strlen($this->uploadedFilePath) > 0) {
|
||||||
|
unlink($this->uploadedFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->media->status = 'OK';
|
||||||
|
$this->media->save();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error($e->getMessage());
|
||||||
|
$this->media->status = "Failed";
|
||||||
|
$this->media->save();
|
||||||
|
$this->fail($e);
|
||||||
|
}//end try
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,10 +54,8 @@ class ChangeEmailVerify extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message envelope.
|
* Get the message envelope.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Envelope
|
|
||||||
*/
|
*/
|
||||||
public function envelope()
|
public function envelope(): Envelope
|
||||||
{
|
{
|
||||||
return new Envelope(
|
return new Envelope(
|
||||||
subject: '👋🏻 Lets change your email!',
|
subject: '👋🏻 Lets change your email!',
|
||||||
@@ -66,10 +64,8 @@ class ChangeEmailVerify extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message content definition.
|
* Get the message content definition.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Content
|
|
||||||
*/
|
*/
|
||||||
public function content()
|
public function content(): Content
|
||||||
{
|
{
|
||||||
return new Content(
|
return new Content(
|
||||||
view: 'emails.user.change_email_verify',
|
view: 'emails.user.change_email_verify',
|
||||||
|
|||||||
@@ -54,10 +54,8 @@ class ChangedEmail extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message envelope.
|
* Get the message envelope.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Envelope
|
|
||||||
*/
|
*/
|
||||||
public function envelope()
|
public function envelope(): Envelope
|
||||||
{
|
{
|
||||||
return new Envelope(
|
return new Envelope(
|
||||||
subject: '👍 Your email has been changed!',
|
subject: '👍 Your email has been changed!',
|
||||||
@@ -66,10 +64,8 @@ class ChangedEmail extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message content definition.
|
* Get the message content definition.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Content
|
|
||||||
*/
|
*/
|
||||||
public function content()
|
public function content(): Content
|
||||||
{
|
{
|
||||||
return new Content(
|
return new Content(
|
||||||
view: 'emails.user.changed_email',
|
view: 'emails.user.changed_email',
|
||||||
|
|||||||
@@ -36,10 +36,8 @@ class ChangedPassword extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message envelope.
|
* Get the message envelope.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Envelope
|
|
||||||
*/
|
*/
|
||||||
public function envelope()
|
public function envelope(): Envelope
|
||||||
{
|
{
|
||||||
return new Envelope(
|
return new Envelope(
|
||||||
subject: '👍 Your password has been changed!',
|
subject: '👍 Your password has been changed!',
|
||||||
@@ -48,10 +46,8 @@ class ChangedPassword extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message content definition.
|
* Get the message content definition.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Content
|
|
||||||
*/
|
*/
|
||||||
public function content()
|
public function content(): Content
|
||||||
{
|
{
|
||||||
return new Content(
|
return new Content(
|
||||||
view: 'emails.user.changed_password',
|
view: 'emails.user.changed_password',
|
||||||
|
|||||||
@@ -53,10 +53,8 @@ class Contact extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message envelope.
|
* Get the message envelope.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Envelope
|
|
||||||
*/
|
*/
|
||||||
public function envelope()
|
public function envelope(): Envelope
|
||||||
{
|
{
|
||||||
return new Envelope(
|
return new Envelope(
|
||||||
subject: config('contact.contact_subject'),
|
subject: config('contact.contact_subject'),
|
||||||
@@ -65,10 +63,8 @@ class Contact extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message content definition.
|
* Get the message content definition.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Content
|
|
||||||
*/
|
*/
|
||||||
public function content()
|
public function content(): Content
|
||||||
{
|
{
|
||||||
return new Content(
|
return new Content(
|
||||||
view: 'emails.user.contact',
|
view: 'emails.user.contact',
|
||||||
|
|||||||
@@ -45,10 +45,8 @@ class EmailVerify extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message envelope.
|
* Get the message envelope.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Envelope
|
|
||||||
*/
|
*/
|
||||||
public function envelope()
|
public function envelope(): Envelope
|
||||||
{
|
{
|
||||||
return new Envelope(
|
return new Envelope(
|
||||||
subject: '👋🏻 Welcome to STEMMechanics!',
|
subject: '👋🏻 Welcome to STEMMechanics!',
|
||||||
@@ -57,10 +55,8 @@ class EmailVerify extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message content definition.
|
* Get the message content definition.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Content
|
|
||||||
*/
|
*/
|
||||||
public function content()
|
public function content(): Content
|
||||||
{
|
{
|
||||||
return new Content(
|
return new Content(
|
||||||
view: 'emails.user.email_verify',
|
view: 'emails.user.email_verify',
|
||||||
|
|||||||
@@ -45,10 +45,8 @@ class ForgotPassword extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message envelope.
|
* Get the message envelope.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Envelope
|
|
||||||
*/
|
*/
|
||||||
public function envelope()
|
public function envelope(): Envelope
|
||||||
{
|
{
|
||||||
return new Envelope(
|
return new Envelope(
|
||||||
subject: '🤦 Forgot your password?',
|
subject: '🤦 Forgot your password?',
|
||||||
@@ -57,10 +55,8 @@ class ForgotPassword extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message content definition.
|
* Get the message content definition.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Content
|
|
||||||
*/
|
*/
|
||||||
public function content()
|
public function content(): Content
|
||||||
{
|
{
|
||||||
return new Content(
|
return new Content(
|
||||||
view: 'emails.user.forgot_password',
|
view: 'emails.user.forgot_password',
|
||||||
|
|||||||
@@ -1,60 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Mail;
|
|
||||||
|
|
||||||
use Illuminate\Bus\Queueable;
|
|
||||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
||||||
use Illuminate\Mail\Mailable;
|
|
||||||
use Illuminate\Mail\Mailables\Content;
|
|
||||||
use Illuminate\Mail\Mailables\Envelope;
|
|
||||||
use Illuminate\Queue\SerializesModels;
|
|
||||||
|
|
||||||
class ForgotUsername extends Mailable
|
|
||||||
{
|
|
||||||
use Queueable;
|
|
||||||
use SerializesModels;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The list of usernames
|
|
||||||
*
|
|
||||||
* @var string[]
|
|
||||||
*/
|
|
||||||
public $usernames;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new message instance.
|
|
||||||
*
|
|
||||||
* @param array $usernames The usernames.
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __construct(array $usernames)
|
|
||||||
{
|
|
||||||
$this->usernames = $usernames;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the message envelope.
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Envelope
|
|
||||||
*/
|
|
||||||
public function envelope()
|
|
||||||
{
|
|
||||||
return new Envelope(
|
|
||||||
subject: '🤦 Forgot your username?',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the message content definition.
|
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Content
|
|
||||||
*/
|
|
||||||
public function content()
|
|
||||||
{
|
|
||||||
return new Content(
|
|
||||||
view: 'emails.user.forgot_username',
|
|
||||||
text: 'emails.user.forgot_username_plain',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -36,10 +36,8 @@ class SubscriptionConfirm extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message envelope.
|
* Get the message envelope.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Envelope
|
|
||||||
*/
|
*/
|
||||||
public function envelope()
|
public function envelope(): Envelope
|
||||||
{
|
{
|
||||||
return new Envelope(
|
return new Envelope(
|
||||||
subject: '🗞️ You\'re on the mailing list!',
|
subject: '🗞️ You\'re on the mailing list!',
|
||||||
@@ -48,10 +46,8 @@ class SubscriptionConfirm extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message content definition.
|
* Get the message content definition.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Content
|
|
||||||
*/
|
*/
|
||||||
public function content()
|
public function content(): Content
|
||||||
{
|
{
|
||||||
return new Content(
|
return new Content(
|
||||||
view: 'emails.user.subscription_confirm',
|
view: 'emails.user.subscription_confirm',
|
||||||
|
|||||||
@@ -36,10 +36,8 @@ class SubscriptionUnsubscribed extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message envelope.
|
* Get the message envelope.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Envelope
|
|
||||||
*/
|
*/
|
||||||
public function envelope()
|
public function envelope(): Envelope
|
||||||
{
|
{
|
||||||
return new Envelope(
|
return new Envelope(
|
||||||
subject: 'You have been unsubscribed',
|
subject: 'You have been unsubscribed',
|
||||||
@@ -48,10 +46,8 @@ class SubscriptionUnsubscribed extends Mailable
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the message content definition.
|
* Get the message content definition.
|
||||||
*
|
|
||||||
* @return \Illuminate\Mail\Mailables\Content
|
|
||||||
*/
|
*/
|
||||||
public function content()
|
public function content(): Content
|
||||||
{
|
{
|
||||||
return new Content(
|
return new Content(
|
||||||
view: 'emails.user.subscription_unsubscribed',
|
view: 'emails.user.subscription_unsubscribed',
|
||||||
|
|||||||
@@ -15,4 +15,30 @@ class Analytics extends Model
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new row in the analytics table with the given attributes,
|
||||||
|
* automatically assigning a session value based on previous rows.
|
||||||
|
*
|
||||||
|
* @param array $attributes Model attributes.
|
||||||
|
*/
|
||||||
|
public static function createWithSession(array $attributes): static
|
||||||
|
{
|
||||||
|
$previousRow = self::where('useragent', $attributes['useragent'])
|
||||||
|
->where('ip', $attributes['ip'])
|
||||||
|
->where('created_at', '>=', now()->subMinutes(30))
|
||||||
|
->whereNotNull('session')
|
||||||
|
->orderBy('created_at', 'desc')
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($previousRow !== null) {
|
||||||
|
$attributes['session'] = $previousRow->session;
|
||||||
|
} else {
|
||||||
|
$lastSession = self::max('session');
|
||||||
|
$attributes['session'] = ($lastSession + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return static::create($attributes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,9 @@ namespace App\Models;
|
|||||||
use App\Traits\Uuids;
|
use App\Traits\Uuids;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||||
|
|
||||||
class Post extends Model
|
class Article extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
use Uuids;
|
use Uuids;
|
||||||
@@ -27,12 +28,18 @@ class Post extends Model
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the file user
|
* Get the article user
|
||||||
*
|
|
||||||
* @return BelongsTo
|
|
||||||
*/
|
*/
|
||||||
public function user()
|
public function user(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all of the article's attachments.
|
||||||
|
*/
|
||||||
|
public function attachments(): MorphMany
|
||||||
|
{
|
||||||
|
return $this->morphMany(\App\Models\Attachment::class, 'attachable');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
49
app/Models/Attachment.php
Normal file
49
app/Models/Attachment.php
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||||
|
|
||||||
|
class Attachment extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array<int, string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'media_id',
|
||||||
|
'private',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default attributes.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $attributes = [
|
||||||
|
'private' => 'false',
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get attachments attachable
|
||||||
|
*/
|
||||||
|
public function attachable(): MorphTo
|
||||||
|
{
|
||||||
|
return $this->morphTo();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the media for this attachment.
|
||||||
|
*/
|
||||||
|
public function media(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Media::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,8 @@ namespace App\Models;
|
|||||||
use App\Traits\Uuids;
|
use App\Traits\Uuids;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||||
|
|
||||||
class Event extends Model
|
class Event extends Model
|
||||||
{
|
{
|
||||||
@@ -19,6 +21,7 @@ class Event extends Model
|
|||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'title',
|
'title',
|
||||||
'location',
|
'location',
|
||||||
|
'location_url',
|
||||||
'address',
|
'address',
|
||||||
'start_at',
|
'start_at',
|
||||||
'end_at',
|
'end_at',
|
||||||
@@ -27,6 +30,25 @@ class Event extends Model
|
|||||||
'registration_type',
|
'registration_type',
|
||||||
'registration_data',
|
'registration_data',
|
||||||
'hero',
|
'hero',
|
||||||
'content'
|
'content',
|
||||||
|
'price',
|
||||||
|
'ages',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all of the article's attachments.
|
||||||
|
*/
|
||||||
|
public function attachments(): MorphMany
|
||||||
|
{
|
||||||
|
return $this->morphMany(\App\Models\Attachment::class, 'attachable');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all the associated users.
|
||||||
|
*/
|
||||||
|
public function users(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(User::class, 'event_user', 'event_id', 'user_id');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
40
app/Models/EventUsers.php
Normal file
40
app/Models/EventUsers.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Traits\Uuids;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class EventUser extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
use Uuids;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array<int, string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'event_id',
|
||||||
|
'user_id',
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the event for this attachment.
|
||||||
|
*/
|
||||||
|
public function event(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(Event::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the user for this attachment.
|
||||||
|
*/
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,18 +2,37 @@
|
|||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enum\HttpResponseCodes;
|
||||||
|
use App\Jobs\MoveMediaJob;
|
||||||
|
use App\Jobs\OptimizeMediaJob;
|
||||||
|
use App\Jobs\StoreUploadedFileJob;
|
||||||
use App\Traits\Uuids;
|
use App\Traits\Uuids;
|
||||||
use Illuminate\Contracts\Container\BindingResolutionException;
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
use Illuminate\Http\UploadedFile;
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Queue;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||||
|
|
||||||
class Media extends Model
|
class Media extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
use Uuids;
|
use Uuids;
|
||||||
|
use DispatchesJobs;
|
||||||
|
|
||||||
|
public const INVALID_FILE_ERROR = 1;
|
||||||
|
public const FILE_SIZE_EXCEEDED_ERROR = 2;
|
||||||
|
public const FILE_NAME_EXISTS_ERROR = 3;
|
||||||
|
public const TEMP_FILE_ERROR = 4;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that are mass assignable.
|
* The attributes that are mass assignable.
|
||||||
@@ -22,20 +41,14 @@ class Media extends Model
|
|||||||
*/
|
*/
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'title',
|
'title',
|
||||||
'name',
|
|
||||||
'mime',
|
|
||||||
'user_id',
|
'user_id',
|
||||||
|
'mime_type',
|
||||||
|
'permission',
|
||||||
|
'storage',
|
||||||
|
'description',
|
||||||
|
'name',
|
||||||
'size',
|
'size',
|
||||||
'permission'
|
'status',
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that are hidden.
|
|
||||||
*
|
|
||||||
* @var array<int, string>
|
|
||||||
*/
|
|
||||||
protected $hidden = [
|
|
||||||
'path',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,13 +60,48 @@ class Media extends Model
|
|||||||
'url',
|
'url',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default attributes.
|
||||||
|
*
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $attributes = [
|
||||||
|
'storage' => 'cdn',
|
||||||
|
'variants' => '[]',
|
||||||
|
'description' => '',
|
||||||
|
'dimensions' => '',
|
||||||
|
'permission' => '',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The storage file list cache.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected static $storageFileListCache = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The variant types.
|
||||||
|
*
|
||||||
|
* @var int[][][]
|
||||||
|
*/
|
||||||
|
protected static $variantTypes = [
|
||||||
|
'image' => [
|
||||||
|
'thumb' => ['width' => 150, 'height' => 150],
|
||||||
|
'small' => ['width' => 300, 'height' => 225],
|
||||||
|
'medium' => ['width' => 768, 'height' => 576],
|
||||||
|
'large' => ['width' => 1024, 'height' => 768],
|
||||||
|
'xlarge' => ['width' => 1536, 'height' => 1152],
|
||||||
|
'xxlarge' => ['width' => 2048, 'height' => 1536],
|
||||||
|
'scaled' => ['width' => 2560, 'height' => 1920]
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Model Boot
|
* Model Boot
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
protected static function boot()
|
protected static function boot(): void
|
||||||
{
|
{
|
||||||
parent::boot();
|
parent::boot();
|
||||||
|
|
||||||
@@ -62,124 +110,379 @@ class Media extends Model
|
|||||||
$origPermission = $media->getOriginal()['permission'];
|
$origPermission = $media->getOriginal()['permission'];
|
||||||
$newPermission = $media->permission;
|
$newPermission = $media->permission;
|
||||||
|
|
||||||
$origPath = Storage::disk(Media::getStorageId(empty($origPermission)))->path($media->name);
|
$newPermissionLen = strlen($newPermission);
|
||||||
$newPath = Storage::disk(Media::getStorageId(empty($newPermission)))->path($media->name);
|
|
||||||
|
|
||||||
if ($origPath !== $newPath) {
|
if ($newPermissionLen !== strlen($origPermission)) {
|
||||||
if (file_exists($origPath) === true) {
|
if ($newPermissionLen === 0) {
|
||||||
if (file_exists($newPath) === true) {
|
$this->moveToStorage('cdn');
|
||||||
$fileParts = pathinfo($newPath);
|
} else {
|
||||||
$newName = '';
|
$this->moveToStorage('private');
|
||||||
|
}
|
||||||
// need a new name!
|
|
||||||
$tmpPath = $newPath;
|
|
||||||
while (file_exists($tmpPath) === true) {
|
|
||||||
$newName = uniqid('', true) . $fileParts['extension'];
|
|
||||||
$tmpPath = $fileParts['dirname'] . '/' . $newName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$media->name = $newName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rename($origPath, $newPath);
|
|
||||||
}//end if
|
|
||||||
}//end if
|
|
||||||
}//end if
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
static::deleting(function ($media) {
|
||||||
|
$media->deleteFile();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get Type Variants.
|
||||||
|
*
|
||||||
|
* @param string $type The variant type to get.
|
||||||
|
* @return array The variant data.
|
||||||
|
*/
|
||||||
|
public static function getTypeVariants(string $type): array
|
||||||
|
{
|
||||||
|
if (isset(self::$variantTypes[$type]) === true) {
|
||||||
|
return self::$variantTypes[$type];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variants Get Mutator.
|
||||||
|
*
|
||||||
|
* @param mixed $value The value to mutate.
|
||||||
|
* @return array The mutated value.
|
||||||
|
*/
|
||||||
|
public function getVariantsAttribute(mixed $value): array
|
||||||
|
{
|
||||||
|
if (is_string($value) === true) {
|
||||||
|
return json_decode($value, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variants Set Mutator.
|
||||||
|
*
|
||||||
|
* @param mixed $value The value to mutate.
|
||||||
|
*/
|
||||||
|
public function setVariantsAttribute(mixed $value): void
|
||||||
|
{
|
||||||
|
if (is_array($value) !== true) {
|
||||||
|
$value = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->attributes['variants'] = json_encode(($value ?? []));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get previous variant.
|
||||||
|
*
|
||||||
|
* @param string $type The variant type.
|
||||||
|
* @param string $variant The initial variant.
|
||||||
|
* @return string The previous variant name (or '').
|
||||||
|
*/
|
||||||
|
public function getPreviousVariant(string $type, string $variant): string
|
||||||
|
{
|
||||||
|
if (isset(self::$variantTypes[$type]) === false) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$variants = self::$variantTypes[$type];
|
||||||
|
$keys = array_keys($variants);
|
||||||
|
|
||||||
|
$currentIndex = array_search($variant, $keys);
|
||||||
|
if ($currentIndex === false || $currentIndex === 0) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $keys[($currentIndex - 1)];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get next variant.
|
||||||
|
*
|
||||||
|
* @param string $type The variant type.
|
||||||
|
* @param string $variant The initial variant.
|
||||||
|
* @return string The next variant name (or '').
|
||||||
|
*/
|
||||||
|
public function getNextVariant(string $type, string $variant): string
|
||||||
|
{
|
||||||
|
if (isset(self::$variantTypes[$type]) === false) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$variants = self::$variantTypes[$type];
|
||||||
|
$keys = array_keys($variants);
|
||||||
|
|
||||||
|
$currentIndex = array_search($variant, $keys);
|
||||||
|
if ($currentIndex === false || $currentIndex === (count($keys) - 1)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $keys[($currentIndex + 1)];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get variant URL.
|
||||||
|
*
|
||||||
|
* @param string $variant The variant to find.
|
||||||
|
* @param boolean $returnNearest Return the nearest variant if request is not found.
|
||||||
|
* @return string The URL.
|
||||||
|
*/
|
||||||
|
public function getVariantURL(string $variant, bool $returnNearest = true): string
|
||||||
|
{
|
||||||
|
$variants = $this->variants;
|
||||||
|
if (isset($variants[$variant]) === true) {
|
||||||
|
return self::getUrlPath() . $variants[$variant];
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($returnNearest === true) {
|
||||||
|
$variantType = explode('/', $this->mime_type)[0];
|
||||||
|
$previousVariant = $variant;
|
||||||
|
while (empty($previousVariant) === false) {
|
||||||
|
$previousVariant = $this->getPreviousVariant($variantType, $previousVariant);
|
||||||
|
if (empty($previousVariant) === false && isset($variants[$previousVariant]) === true) {
|
||||||
|
return self::getUrlPath() . $variants[$previousVariant];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete file and associated files with the modal.
|
||||||
|
*/
|
||||||
|
public function deleteFile(): void
|
||||||
|
{
|
||||||
|
$fileName = $this->name;
|
||||||
|
$baseName = pathinfo($fileName, PATHINFO_FILENAME);
|
||||||
|
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
|
||||||
|
|
||||||
|
$files = Storage::disk($this->storage)->files();
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
if (preg_match("/{$baseName}(-[a-zA-Z0-9]+)?\.{$extension}/", $file) === 1) {
|
||||||
|
Storage::disk($this->storage)->delete($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->invalidateCFCache();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invalidate Cloudflare Cache.
|
||||||
|
*
|
||||||
|
* @throws InvalidArgumentException Exception.
|
||||||
|
*/
|
||||||
|
private function invalidateCFCache(): void
|
||||||
|
{
|
||||||
|
$zone_id = env("CLOUDFLARE_ZONE_ID");
|
||||||
|
$api_key = env("CLOUDFLARE_API_KEY");
|
||||||
|
if ($zone_id !== null && $api_key !== null && $this->url !== "") {
|
||||||
|
$urls = [$this->url];
|
||||||
|
|
||||||
|
foreach ($this->variants as $variant => $name) {
|
||||||
|
$urls[] = str_replace($this->name, $name, $this->url);
|
||||||
|
}
|
||||||
|
|
||||||
|
$curl = curl_init();
|
||||||
|
curl_setopt_array($curl, [
|
||||||
|
CURLOPT_URL => "https://api.cloudflare.com/client/v4/zones/" . $zone_id . "/purge_cache",
|
||||||
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
|
CURLOPT_CUSTOMREQUEST => "DELETE",
|
||||||
|
CURLOPT_POSTFIELDS => json_encode(["files" => $urls]),
|
||||||
|
CURLOPT_HTTPHEADER => [
|
||||||
|
"Content-Type: application/json",
|
||||||
|
"Authorization: Bearer " . $api_key
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
curl_exec($curl);
|
||||||
|
curl_close($curl);
|
||||||
|
}//end if
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get URL path
|
||||||
|
*/
|
||||||
|
public function getUrlPath(): string
|
||||||
|
{
|
||||||
|
$url = config("filesystems.disks.$this->storage.url");
|
||||||
|
return "$url/";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the file URL
|
* Return the file URL
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
*/
|
||||||
public function getUrlAttribute()
|
public function getUrlAttribute(): string
|
||||||
{
|
{
|
||||||
$url = config('filesystems.disks.' . Media::getStorageId($this) . '.url');
|
if (isset($this->attributes['name']) === true) {
|
||||||
if (empty($url) === false) {
|
return self::getUrlPath() . $this->name;
|
||||||
$replace = [
|
}
|
||||||
'id' => $this->id,
|
|
||||||
'name' => $this->name
|
|
||||||
];
|
|
||||||
|
|
||||||
$url = str_ireplace(array_map(function ($item) {
|
|
||||||
return '%' . $item . '%';
|
|
||||||
}, array_keys($replace)), array_values($replace), $url);
|
|
||||||
|
|
||||||
return $url;
|
|
||||||
}//end if
|
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the file owner
|
* Return the file owner
|
||||||
*
|
|
||||||
* @return BelongsTo
|
|
||||||
*/
|
*/
|
||||||
public function user()
|
public function user(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the file full local path
|
* Move files to new storage device.
|
||||||
*
|
*
|
||||||
* @return string
|
* @param string $storage The storage ID to move to.
|
||||||
*/
|
*/
|
||||||
public function path()
|
public function moveToStorage(string $storage): void
|
||||||
{
|
{
|
||||||
return Storage::disk(Media::getStorageId($this))->path($this->name);
|
if ($storage !== $this->storage && Config::has("filesystems.disks.$storage") === true) {
|
||||||
|
$this->status = "Processing media";
|
||||||
|
MoveMediaJob::dispatch($this, $storage)->onQueue('media');
|
||||||
|
$this->save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get Storage ID
|
* Create new Media from UploadedFile data.
|
||||||
*
|
*
|
||||||
* @param mixed $mediaOrPublic Media object or if file is public.
|
* @param App\Models\Request $request The request data.
|
||||||
* @return string
|
* @param Illuminate\Http\UploadedFile $file The file.
|
||||||
|
* @return null|Media The result or null if not successful.
|
||||||
*/
|
*/
|
||||||
public static function getStorageId(mixed $mediaOrPublic)
|
public static function createFromUploadedFile(Request $request, UploadedFile $file): ?Media
|
||||||
{
|
{
|
||||||
$isPublic = true;
|
$request->merge([
|
||||||
|
'title' => $request->get('title', ''),
|
||||||
|
'name' => '',
|
||||||
|
'size' => 0,
|
||||||
|
'mime_type' => '',
|
||||||
|
'status' => '',
|
||||||
|
]);
|
||||||
|
|
||||||
if ($mediaOrPublic instanceof Media) {
|
if ($request->get('storage') === null) {
|
||||||
$isPublic = empty($mediaOrPublic->permission);
|
// We store images by default locally
|
||||||
|
if (strpos($file->getMimeType(), 'image/') === 0) {
|
||||||
|
$request->merge([
|
||||||
|
'storage' => 'local',
|
||||||
|
]);
|
||||||
} else {
|
} else {
|
||||||
$isPublic = boolval($mediaOrPublic);
|
$request->merge([
|
||||||
|
'storage' => 'cdn',
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $isPublic === true ? 'public' : 'local';
|
$mediaItem = $request->user()->media()->create($request->all());
|
||||||
|
$mediaItem->updateWithUploadedFile($file);
|
||||||
|
|
||||||
|
return $mediaItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Place uploaded file into storage. Return full path or null
|
* Update Media with UploadedFile data.
|
||||||
*
|
*
|
||||||
* @param UploadedFile $file File to put into storage.
|
* @param Illuminate\Http\UploadedFile $file The file.
|
||||||
* @param boolean $public Is the file available to the public.
|
* @return null|Media The media item.
|
||||||
* @return array|null
|
|
||||||
*/
|
*/
|
||||||
public static function store(UploadedFile $file, bool $public = true)
|
public function updateWithUploadedFile(UploadedFile $file): ?Media
|
||||||
{
|
{
|
||||||
$storage = Media::getStorageId($public);
|
if ($file === null || $file->isValid() !== true) {
|
||||||
$name = $file->store('', ['disk' => $storage]);
|
throw new \Exception('The file is invalid.', self::INVALID_FILE_ERROR);
|
||||||
|
|
||||||
if ($name === false) {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$path = Storage::disk($storage)->path($name);
|
if ($file->getSize() > static::getMaxUploadSize()) {
|
||||||
return [
|
throw new \Exception('The file size is larger then permitted.', self::FILE_SIZE_EXCEEDED_ERROR);
|
||||||
'name' => $name,
|
}
|
||||||
'path' => $path
|
|
||||||
];
|
$name = static::generateUniqueFileName($file->getClientOriginalName());
|
||||||
|
if ($name === false) {
|
||||||
|
throw new \Exception('The file name already exists in storage.', self::FILE_NAME_EXISTS_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove file if there is an existing entry in this medium item
|
||||||
|
if (strlen($this->name) > 0 && strlen($this->storage) > 0) {
|
||||||
|
Storage::disk($this->storage)->delete($this->name);
|
||||||
|
foreach ($this->variants as $variantName => $fileName) {
|
||||||
|
Storage::disk($this->storage)->delete($fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->name = '';
|
||||||
|
$this->variants = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strlen($this->title) === 0) {
|
||||||
|
$this->title = $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->name = $name;
|
||||||
|
$this->size = $file->getSize();
|
||||||
|
$this->mime_type = $file->getMimeType();
|
||||||
|
$this->status = 'Processing media';
|
||||||
|
$this->save();
|
||||||
|
|
||||||
|
$temporaryFilePath = generateTempFilePath();
|
||||||
|
copy($file->path(), $temporaryFilePath);
|
||||||
|
|
||||||
|
try {
|
||||||
|
StoreUploadedFileJob::dispatch($this, $temporaryFilePath)->onQueue('media');
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$this->status = 'Error';
|
||||||
|
$this->save();
|
||||||
|
|
||||||
|
throw $e;
|
||||||
|
}//end try
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download the file from the storage to the user.
|
||||||
|
*
|
||||||
|
* @param string $variant The variant to download or null if none.
|
||||||
|
* @param boolean $fallback Fallback to the original file if the variant is not found.
|
||||||
|
* @return JsonResponse|StreamedResponse The response.
|
||||||
|
* @throws BindingResolutionException The Exception.
|
||||||
|
*/
|
||||||
|
public function download(string $variant = null, bool $fallback = true)
|
||||||
|
{
|
||||||
|
$path = $this->name;
|
||||||
|
if ($variant !== null) {
|
||||||
|
if (array_key_exists($variant, $this->variant) === true) {
|
||||||
|
$path = $this->variant[$variant];
|
||||||
|
} else {
|
||||||
|
return response()->json(['message' => 'The resource was not found.'], HttpResponseCodes::HTTP_NOT_FOUND);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$disk = Storage::disk($this->storage);
|
||||||
|
if ($disk->exists($path) === true) {
|
||||||
|
$stream = $disk->readStream($path);
|
||||||
|
$response = response()->stream(
|
||||||
|
function () use ($stream) {
|
||||||
|
fpassthru($stream);
|
||||||
|
},
|
||||||
|
200,
|
||||||
|
[
|
||||||
|
'Content-Type' => $this->mime_type,
|
||||||
|
'Content-Length' => $disk->size($path),
|
||||||
|
'Content-Disposition' => 'attachment; filename="' . basename($path) . '"',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json(['message' => 'The resource was not found.'], HttpResponseCodes::HTTP_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the server maximum upload size
|
* Get the server maximum upload size
|
||||||
*
|
|
||||||
* @return integer
|
|
||||||
*/
|
*/
|
||||||
public static function maxUploadSize()
|
public static function getMaxUploadSize(): int
|
||||||
{
|
{
|
||||||
$sizes = [
|
$sizes = [
|
||||||
ini_get('upload_max_filesize'),
|
ini_get('upload_max_filesize'),
|
||||||
@@ -207,12 +510,103 @@ class Media extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sanitize filename for upload
|
* Generate a file name that is available within storage.
|
||||||
*
|
*
|
||||||
* @param string $filename Filename to sanitize.
|
* @param string $fileName The proposed file name.
|
||||||
* @return string
|
* @return string|boolean The available file name or false if failed.
|
||||||
*/
|
*/
|
||||||
public static function sanitizeFilename(string $filename)
|
public static function generateUniqueFileName(string $fileName)
|
||||||
|
{
|
||||||
|
$index = 1;
|
||||||
|
$maxTries = 100;
|
||||||
|
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
|
||||||
|
$fileName = static::sanitizeFilename(pathinfo($fileName, PATHINFO_FILENAME));
|
||||||
|
|
||||||
|
if (static::fileNameHasSuffix($fileName) === true || static::fileExistsInStorage("$fileName.$extension") === true || Media::where('name', "$fileName.$extension")->where('status', 'not like', 'failed%')->exists() === true) {
|
||||||
|
$fileName .= '-';
|
||||||
|
for ($i = 1; $i < $maxTries; $i++) {
|
||||||
|
$fileNameIndex = $fileName . $index;
|
||||||
|
if (static::fileExistsInStorage("$fileNameIndex.$extension") !== true && Media::where('name', "$fileNameIndex.$extension")->where('status', 'not like', 'Failed%')->exists() !== true) {
|
||||||
|
return "$fileNameIndex.$extension";
|
||||||
|
}
|
||||||
|
|
||||||
|
++$index;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return "$fileName.$extension";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines if the file name exists in any of the storage disks.
|
||||||
|
*
|
||||||
|
* @param string $fileName The file name to check.
|
||||||
|
* @param boolean $ignoreCache Ignore the file list cache.
|
||||||
|
* @return boolean If the file exists on any storage disks.
|
||||||
|
*/
|
||||||
|
public static function fileExistsInStorage(string $fileName, bool $ignoreCache = false): bool
|
||||||
|
{
|
||||||
|
$disks = array_keys(Config::get('filesystems.disks'));
|
||||||
|
|
||||||
|
if ($ignoreCache === false) {
|
||||||
|
if (count(static::$storageFileListCache) === 0) {
|
||||||
|
$disks = array_keys(Config::get('filesystems.disks'));
|
||||||
|
|
||||||
|
foreach ($disks as $disk) {
|
||||||
|
try {
|
||||||
|
static::$storageFileListCache[$disk] = Storage::disk($disk)->allFiles();
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error($e->getMessage());
|
||||||
|
throw new \Exception("Cannot get a file list for storage device '$disk'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (static::$storageFileListCache as $disk => $files) {
|
||||||
|
if (in_array($fileName, $files) === true) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$disks = array_keys(Config::get('filesystems.disks'));
|
||||||
|
|
||||||
|
foreach ($disks as $disk) {
|
||||||
|
try {
|
||||||
|
if (Storage::disk($disk)->exists($fileName) === true) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error($e->getMessage());
|
||||||
|
throw new \Exception("Cannot verify if file '$fileName' already exists in storage device '$disk'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}//end if
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if the file name contains a special suffix.
|
||||||
|
*
|
||||||
|
* @param string $fileName The file name to test.
|
||||||
|
* @return boolean If the file name contains the special suffix.
|
||||||
|
*/
|
||||||
|
public static function fileNameHasSuffix(string $fileName): bool
|
||||||
|
{
|
||||||
|
$suffix = '/(-\d+x\d+|-scaled)$/i';
|
||||||
|
$fileNameWithoutExtension = pathinfo($fileName, PATHINFO_FILENAME);
|
||||||
|
|
||||||
|
return preg_match($suffix, $fileNameWithoutExtension) === 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sanitize fileName for upload
|
||||||
|
*
|
||||||
|
* @param string $fileName Filename to sanitize.
|
||||||
|
*/
|
||||||
|
private static function sanitizeFilename(string $fileName): string
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
# file system reserved https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
|
# file system reserved https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
|
||||||
@@ -231,7 +625,7 @@ class Media extends Model
|
|||||||
[{}^\~`]
|
[{}^\~`]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$filename = preg_replace(
|
$fileName = preg_replace(
|
||||||
'~
|
'~
|
||||||
[<>:"/\\\|?*]|
|
[<>:"/\\\|?*]|
|
||||||
[\x00-\x1F]|
|
[\x00-\x1F]|
|
||||||
@@ -240,37 +634,37 @@ class Media extends Model
|
|||||||
[{}^\~`]
|
[{}^\~`]
|
||||||
~x',
|
~x',
|
||||||
'-',
|
'-',
|
||||||
$filename
|
$fileName
|
||||||
);
|
);
|
||||||
|
|
||||||
$filename = ltrim($filename, '.-');
|
$fileName = ltrim($fileName, '.-');
|
||||||
|
|
||||||
$filename = preg_replace([
|
$fileName = preg_replace([
|
||||||
// "file name.zip" becomes "file-name.zip"
|
// "file name.zip" becomes "file-name.zip"
|
||||||
'/ +/',
|
'/ +/',
|
||||||
// "file___name.zip" becomes "file-name.zip"
|
// "file___name.zip" becomes "file-name.zip"
|
||||||
'/_+/',
|
'/_+/',
|
||||||
// "file---name.zip" becomes "file-name.zip"
|
// "file---name.zip" becomes "file-name.zip"
|
||||||
'/-+/'
|
'/-+/'
|
||||||
], '-', $filename);
|
], '-', $fileName);
|
||||||
$filename = preg_replace([
|
$fileName = preg_replace([
|
||||||
// "file--.--.-.--name.zip" becomes "file.name.zip"
|
// "file--.--.-.--name.zip" becomes "file.name.zip"
|
||||||
'/-*\.-*/',
|
'/-*\.-*/',
|
||||||
// "file...name..zip" becomes "file.name.zip"
|
// "file...name..zip" becomes "file.name.zip"
|
||||||
'/\.{2,}/'
|
'/\.{2,}/'
|
||||||
], '.', $filename);
|
], '.', $fileName);
|
||||||
// lowercase for windows/unix interoperability http://support.microsoft.com/kb/100625
|
// lowercase for windows/unix interoperability http://support.microsoft.com/kb/100625
|
||||||
$filename = mb_strtolower($filename, mb_detect_encoding($filename));
|
$fileName = mb_strtolower($fileName, mb_detect_encoding($fileName));
|
||||||
// ".file-name.-" becomes "file-name"
|
// ".file-name.-" becomes "file-name"
|
||||||
$filename = trim($filename, '.-');
|
$fileName = trim($fileName, '.-');
|
||||||
|
|
||||||
$ext = pathinfo($filename, PATHINFO_EXTENSION);
|
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
|
||||||
$filename = mb_strcut(
|
$fileName = mb_strcut(
|
||||||
pathinfo($filename, PATHINFO_FILENAME),
|
pathinfo($fileName, PATHINFO_FILENAME),
|
||||||
0,
|
0,
|
||||||
(255 - ($ext !== '' ? strlen($ext) + 1 : 0)),
|
(255 - ($ext !== '' ? strlen($ext) + 1 : 0)),
|
||||||
mb_detect_encoding($filename)
|
mb_detect_encoding($fileName)
|
||||||
) . ($ext !== '' ? '.' . $ext : '');
|
) . ($ext !== '' ? '.' . $ext : '');
|
||||||
return $filename;
|
return $fileName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,10 +24,8 @@ class Permission extends Model
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the User associated with this model
|
* Get the User associated with this model
|
||||||
*
|
|
||||||
* @return BelongsTo
|
|
||||||
*/
|
*/
|
||||||
public function user()
|
public function user(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
|
|||||||
39
app/Models/Shortlink.php
Normal file
39
app/Models/Shortlink.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enum\HttpResponseCodes;
|
||||||
|
use App\Jobs\MoveMediaJob;
|
||||||
|
use App\Jobs\OptimizeMediaJob;
|
||||||
|
use App\Jobs\StoreUploadedFileJob;
|
||||||
|
use App\Traits\Uuids;
|
||||||
|
use Illuminate\Contracts\Container\BindingResolutionException;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Config;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Queue;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||||||
|
|
||||||
|
class Shortlink extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array<int, string>
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'code',
|
||||||
|
'url',
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ namespace App\Models;
|
|||||||
|
|
||||||
use App\Traits\Uuids;
|
use App\Traits\Uuids;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
use Laravel\Sanctum\HasApiTokens;
|
use Laravel\Sanctum\HasApiTokens;
|
||||||
@@ -25,12 +26,12 @@ class User extends Authenticatable implements Auditable
|
|||||||
* @var array<int, string>
|
* @var array<int, string>
|
||||||
*/
|
*/
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'username',
|
|
||||||
'first_name',
|
'first_name',
|
||||||
'last_name',
|
'last_name',
|
||||||
'email',
|
'email',
|
||||||
'phone',
|
'phone',
|
||||||
'password',
|
'password',
|
||||||
|
'display_name',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -66,28 +67,28 @@ class User extends Authenticatable implements Auditable
|
|||||||
'permissions'
|
'permissions'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
// public function getPermissionsAttribute() {
|
* The default attributes.
|
||||||
// return $this->permissions()->pluck('permission')->toArray();
|
*
|
||||||
// }
|
* @var string[]
|
||||||
|
*/
|
||||||
|
protected $attributes = [
|
||||||
|
'phone' => '',
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the list of files of the user
|
* Get the list of files of the user
|
||||||
*
|
|
||||||
* @return HasMany
|
|
||||||
*/
|
*/
|
||||||
public function permissions()
|
public function permissions(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Permission::class);
|
return $this->hasMany(Permission::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the permission attribute
|
* Get the permission attribute
|
||||||
*
|
|
||||||
* @return array
|
|
||||||
*/
|
*/
|
||||||
public function getPermissionsAttribute()
|
public function getPermissionsAttribute(): array
|
||||||
{
|
{
|
||||||
return $this->permissions()->pluck('permission')->toArray();
|
return $this->permissions()->pluck('permission')->toArray();
|
||||||
}
|
}
|
||||||
@@ -96,50 +97,89 @@ class User extends Authenticatable implements Auditable
|
|||||||
* Test if user has permission
|
* Test if user has permission
|
||||||
*
|
*
|
||||||
* @param string $permission Permission to test.
|
* @param string $permission Permission to test.
|
||||||
* @return boolean
|
|
||||||
*/
|
*/
|
||||||
public function hasPermission(string $permission)
|
public function hasPermission(string $permission): bool
|
||||||
{
|
{
|
||||||
return ($this->permissions()->where('permission', $permission)->first() !== null);
|
return ($this->permissions()->where('permission', $permission)->first() !== null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the list of files of the user
|
* Give permissions to the user
|
||||||
*
|
*
|
||||||
* @return HasMany
|
* @param string|array $permissions The permission(s) to give.
|
||||||
*/
|
*/
|
||||||
public function media()
|
public function givePermission($permissions): Collection
|
||||||
|
{
|
||||||
|
if (is_array($permissions) === false) {
|
||||||
|
$permissions = [$permissions];
|
||||||
|
}
|
||||||
|
|
||||||
|
$permissions = collect($permissions)->map(function ($permission) {
|
||||||
|
return ['permission' => $permission];
|
||||||
|
});
|
||||||
|
|
||||||
|
$existingPermissions = $this->permissions()->whereIn('permission', $permissions->pluck('permission'))->get();
|
||||||
|
$newPermissions = $permissions->reject(function ($permission) use ($existingPermissions) {
|
||||||
|
return $existingPermissions->contains('permission', $permission['permission']);
|
||||||
|
});
|
||||||
|
|
||||||
|
return $this->permissions()->createMany($newPermissions->toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Revoke permissions from the user
|
||||||
|
*
|
||||||
|
* @param string|array $permissions The permission(s) to revoke.
|
||||||
|
*/
|
||||||
|
public function revokePermission($permissions): int
|
||||||
|
{
|
||||||
|
if (is_array($permissions) === false) {
|
||||||
|
$permissions = [$permissions];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->permissions()
|
||||||
|
->whereIn('permission', $permissions)
|
||||||
|
->delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of files of the user
|
||||||
|
*/
|
||||||
|
public function media(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Media::class);
|
return $this->hasMany(Media::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the list of files of the user
|
* Get the list of files of the user
|
||||||
*
|
|
||||||
* @return HasMany
|
|
||||||
*/
|
*/
|
||||||
public function posts()
|
public function articles(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(Post::class);
|
return $this->hasMany(Article::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get associated user codes
|
* Get associated user codes
|
||||||
*
|
|
||||||
* @return HasMany
|
|
||||||
*/
|
*/
|
||||||
public function codes()
|
public function codes(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(UserCode::class);
|
return $this->hasMany(UserCode::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the list of logins of the user
|
* Get the list of logins of the user
|
||||||
*
|
|
||||||
* @return HasMany
|
|
||||||
*/
|
*/
|
||||||
public function logins()
|
public function logins(): HasMany
|
||||||
{
|
{
|
||||||
return $this->hasMany(UserLogins::class);
|
return $this->hasMany(UserLogins::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the events associated with the user.
|
||||||
|
*/
|
||||||
|
public function events(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Event::class, 'event_user', 'user_id', 'event_id');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,10 +23,8 @@ class UserCode extends Model
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Boot function from Laravel.
|
* Boot function from Laravel.
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
protected static function boot()
|
protected static function boot(): void
|
||||||
{
|
{
|
||||||
parent::boot();
|
parent::boot();
|
||||||
static::creating(function ($model) {
|
static::creating(function ($model) {
|
||||||
@@ -46,10 +44,8 @@ class UserCode extends Model
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate new code
|
* Generate new code
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function regenerate()
|
public function regenerate(): void
|
||||||
{
|
{
|
||||||
while (true) {
|
while (true) {
|
||||||
$code = random_int(100000, 999999);
|
$code = random_int(100000, 999999);
|
||||||
@@ -62,20 +58,16 @@ class UserCode extends Model
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear expired user codes
|
* Clear expired user codes
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public static function clearExpired()
|
public static function clearExpired(): void
|
||||||
{
|
{
|
||||||
UserCode::where('updated_at', '<=', now()->subDays(5))->delete();
|
UserCode::where('updated_at', '<=', now()->subDays(5))->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get associated user
|
* Get associated user
|
||||||
*
|
|
||||||
* @return BelongsTo
|
|
||||||
*/
|
*/
|
||||||
public function user()
|
public function user(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,10 +28,8 @@ class UserLogins extends Model
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the file user
|
* Get the file user
|
||||||
*
|
|
||||||
* @return BelongsTo
|
|
||||||
*/
|
*/
|
||||||
public function user()
|
public function user(): BelongsTo
|
||||||
{
|
{
|
||||||
return $this->belongsTo(User::class);
|
return $this->belongsTo(User::class);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,30 +2,34 @@
|
|||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Rules\RequiredIfAny;
|
||||||
|
use App\Rules\Uniqueish;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
|
||||||
class AppServiceProvider extends ServiceProvider
|
class AppServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Register any application services.
|
* Register any application services.
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function register()
|
public function register(): void
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bootstrap any application services.
|
* Bootstrap any application services.
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot(): void
|
||||||
{
|
{
|
||||||
//
|
Storage::macro('public', function ($diskName) {
|
||||||
|
$public = config("filesystems.disks.{$diskName}.public", false);
|
||||||
|
return $public;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,13 +19,9 @@ class AuthServiceProvider extends ServiceProvider
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Register any authentication / authorization services.
|
* Register any authentication / authorization services.
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot(): void
|
||||||
{
|
{
|
||||||
$this->registerPolicies();
|
|
||||||
|
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,8 @@ class BroadcastServiceProvider extends ServiceProvider
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Bootstrap any application services.
|
* Bootstrap any application services.
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot(): void
|
||||||
{
|
{
|
||||||
Broadcast::routes();
|
Broadcast::routes();
|
||||||
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user