|
18 | 18 | api_protect_global_admin_script();
|
19 | 19 |
|
20 | 20 | $httpRequest = HttpRequest::createFromGlobals();
|
| 21 | +$urlRepo = Container::getAccessUrlRepository(); |
21 | 22 |
|
22 | 23 | $form = new FormValidator('add_url');
|
23 | 24 |
|
|
30 | 31 | $form->addFile('url_image_1', get_lang('Image'));
|
31 | 32 | //$form->addElement('file', 'url_image_2', 'URL Image 2 (PNG)');
|
32 | 33 | //$form->addElement('file', 'url_image_3', 'URL Image 3 (PNG)');
|
| 34 | +$form->addCheckBox('login_only', get_lang('Login only'), get_lang('Yes')); |
33 | 35 |
|
34 | 36 | $defaults['url'] = 'http://';
|
35 | 37 | $form->setDefaults($defaults);
|
36 | 38 | if ($httpRequest->query->has('url_id')) {
|
37 | 39 | $url_id = $httpRequest->query->getInt('url_id');
|
38 |
| - $num_url_id = UrlManager::url_id_exist($url_id); |
39 |
| - if (1 != $num_url_id) { |
| 40 | + |
| 41 | + /** @var AccessUrl $url_data */ |
| 42 | + $url_data = $urlRepo->find($url_id); |
| 43 | + |
| 44 | + if (!$url_data) { |
40 | 45 | header('Location: access_urls.php');
|
41 | 46 | exit();
|
42 | 47 | }
|
43 |
| - $url_data = UrlManager::get_url_data_from_id($url_id); |
44 |
| - $form->addElement('hidden', 'id', $url_data['id']); |
| 48 | + $form->addElement('hidden', 'id', $url_data->getId()); |
45 | 49 | // If we're still with localhost (should only happen at the very beginning)
|
46 | 50 | // offer the current URL by default. Once this has been saved, no more
|
47 | 51 | // magic will happen, ever.
|
48 |
| - if ($url_data['id'] === 1 && $url_data['url'] === AccessUrl::DEFAULT_ACCESS_URL) { |
| 52 | + if ($url_data->getId() === 1 && $url_data->getUrl() === AccessUrl::DEFAULT_ACCESS_URL) { |
49 | 53 | $https = api_is_https() ? 'https://' : 'http://';
|
50 |
| - $url_data['url'] = $https.$_SERVER['HTTP_HOST'].'/'; |
| 54 | + $url_data->setUrl($https.$_SERVER['HTTP_HOST'].'/'); |
51 | 55 | }
|
52 |
| - $form->setDefaults($url_data); |
| 56 | + $form->setDefaults([ |
| 57 | + 'id' => $url_data->getId(), |
| 58 | + 'url' => $url_data->getUrl(), |
| 59 | + 'description' => $url_data->getDescription(), |
| 60 | + 'active' => $url_data->getActive(), |
| 61 | + 'login_only' => $url_data->isLoginOnly(), |
| 62 | + ]); |
53 | 63 | }
|
54 | 64 |
|
55 | 65 | $form->addHidden(
|
|
71 | 81 | $description = Security::remove_XSS($url_array['description']);
|
72 | 82 | $active = isset($url_array['active']) ? (int) $url_array['active'] : 0;
|
73 | 83 | $url_id = isset($url_array['id']) ? (int) $url_array['id'] : 0;
|
| 84 | + $isLoginOnly = isset($url_array['login_only']) && (bool) $url_array['login_only']; |
74 | 85 | $url_to_go = 'access_urls.php';
|
75 | 86 | if (!empty($url_id)) {
|
76 | 87 | //we can't change the status of the url with id=1
|
77 | 88 | if (1 == $url_id) {
|
78 | 89 | $active = 1;
|
79 | 90 | }
|
80 | 91 | // Checking url
|
81 |
| - if ('/' == substr($url, strlen($url) - 1, strlen($url))) { |
82 |
| - UrlManager::update($url_id, $url, $description, $active); |
83 |
| - } else { |
84 |
| - UrlManager::update($url_id, $url.'/', $description, $active); |
| 92 | + if ('/' != substr($url, strlen($url) - 1, strlen($url))) { |
| 93 | + $url .= '/'; |
85 | 94 | }
|
| 95 | + |
| 96 | + /** @var AccessUrl $accessUrl */ |
| 97 | + $accessUrl = $urlRepo->find($url_id); |
| 98 | + |
| 99 | + $accessUrl |
| 100 | + ->setUrl($url) |
| 101 | + ->setDescription($description) |
| 102 | + ->setActive($active) |
| 103 | + ->setCreatedBy(api_get_user_id()) |
| 104 | + ->setTms(api_get_utc_datetime()) |
| 105 | + ->setIsLoginOnly($isLoginOnly) |
| 106 | + ; |
| 107 | + |
86 | 108 | $url_to_go = 'access_urls.php';
|
87 | 109 | $message = get_lang('The URL has been edited');
|
88 | 110 | } else {
|
|
91 | 113 | $message = get_lang('This URL already exists, please select another URL');
|
92 | 114 | if (0 === $num) {
|
93 | 115 | // checking url
|
94 |
| - if ('/' == substr($url, strlen($url) - 1, strlen($url))) { |
95 |
| - $accessUrl = UrlManager::add($url, $description, $active); |
96 |
| - } else { |
97 |
| - //create |
98 |
| - $accessUrl = UrlManager::add($url.'/', $description, $active); |
| 116 | + if ('/' != substr($url, strlen($url) - 1, strlen($url))) { |
| 117 | + $url .= '/'; |
99 | 118 | }
|
100 |
| - if (null !== $accessUrl) { |
| 119 | + |
| 120 | + $accessUrl = $urlRepo->findOneBy(['url' => $url]); |
| 121 | + |
| 122 | + if (!$accessUrl) { |
| 123 | + $accessUrl = new AccessUrl(); |
| 124 | + $accessUrl |
| 125 | + ->setDescription($description) |
| 126 | + ->setActive($active) |
| 127 | + ->setUrl($url) |
| 128 | + ->setCreatedBy(api_get_user_id()) |
| 129 | + ->setIsLoginOnly($isLoginOnly) |
| 130 | + ; |
| 131 | + |
| 132 | + Database::getManager()->persist($accessUrl); |
| 133 | + |
101 | 134 | $message = get_lang('The URL has been added');
|
102 | 135 | $url_to_go = 'access_urls.php';
|
103 | 136 | }
|
104 | 137 | }
|
105 | 138 | }
|
106 | 139 |
|
| 140 | + Database::getManager()->flush(); |
| 141 | + |
107 | 142 | Security::clear_token();
|
108 | 143 | $tok = Security::get_token();
|
109 | 144 | Display::addFlash(Display::return_message($message));
|
|
0 commit comments