Automad
 All Classes Functions Variables Pages
update_config.php
1 <?php
2 /*
3  * ....
4  * .: '':.
5  * :::: ':..
6  * ::. ''..
7  * .:'.. ..':.:::' . :. '':.
8  * :. '' '' '. ::::.. ..:
9  * ::::. ..':.. .'''::::: .
10  * :::::::.. '..:::: :. :::: :
11  * ::'':::::::. ':::.'':.:::: :
12  * :.. ''::::::....': '':: :
13  * :::::. '::::: : .. '' .
14  * .''::::::::... ':::.'' ..'' :.''''.
15  * :..:::''::::: :::::...:'' :..:
16  * ::::::. ':::: :::::::: ..:: .
17  * ::::::::.:::: :::::::: :'':.:: .''
18  * ::: '::::::::.' ''::::: :.' '': :
19  * ::: :::::::::..' :::: ::...' .
20  * ::: .:::::::::: :::: :::: .:'
21  * '::' ''::::::: :::: : :: :
22  * ':::: :::: :'' .:
23  * :::: :::: ..''
24  * :::: ..:::: .:''
25  * '''' '''''
26  *
27  *
28  * AUTOMAD
29  *
30  * Copyright (c) 2014 by Marc Anton Dahmen
31  * http://marcdahmen.de
32  *
33  * Licensed under the MIT license.
34  * http://automad.org/license
35  */
36 
37 
38 defined('AUTOMAD') or die('Direct access not permitted!');
39 
40 
41 /*
42  * Update/merge AM_CONFIG with requested items.
43  */
44 
45 
46 $output = array();
47 
48 
49 // Get config from json file, if exsiting.
50 if (file_exists(AM_CONFIG)) {
51 
52  $config = json_decode(file_get_contents(AM_CONFIG), true);
53  ksort($config);
54 
55 } else {
56 
57  $config = array();
58 
59 }
60 
61 
62 
63 // Cache
64 if (isset($_POST['cache'])) {
65 
66  $cache = $_POST['cache'];
67 
68  if ($cache['enabled'] == 'on') {
69  $config['AM_CACHE_ENABLED'] = true;
70  } else {
71  $config['AM_CACHE_ENABLED'] = false;
72  }
73 
74  $config['AM_CACHE_MONITOR_DELAY'] = intval($cache['monitor-delay']);
75 
76 }
77 
78 
79 // Allowed file types
80 if (isset($_POST['file-types'])) {
81 
82  if ($_POST['file-types']) {
83  // If there string actually contains file types and is not empty.
84  $config['AM_ALLOWED_FILE_TYPES'] = $_POST['file-types'];
85  } else {
86  // If the string is empty, remove the variable from the config, to not overwrite the defaults.
87  unset($config['AM_ALLOWED_FILE_TYPES']);
88  }
89 
90 }
91 
92 
93 // Debugging
94 if (isset($_POST['debug'])) {
95 
96  if ($_POST['debug'] == 'on') {
97  $config['AM_DEBUG_ENABLED'] = true;
98  } else {
99  $config['AM_DEBUG_ENABLED'] = false;
100  }
101 
102 }
103 
104 
105 
106 
107 // Write config file.
108 if ((is_writable(dirname(AM_CONFIG)) && !file_exists(AM_CONFIG)) || is_writable(AM_CONFIG)) {
109 
110  // Check PHP version. For version 5.4+, the option JSON_PRETTY_PRINT can be used to make the file more human-readable.
111  if (version_compare(PHP_VERSION, '5.4') >= 0) {
112  $json = json_encode($config, JSON_PRETTY_PRINT);
113  } else {
114  $json = json_encode($config);
115  }
116 
117  $old = umask(0);
118  file_put_contents(AM_CONFIG, $json);
119  umask($old);
120 
121 } else {
122 
123  $output['error'] = $this->tb['error_permission'] . '<p>' . AM_CONFIG . '</p>';
124 
125 }
126 
127 
128 echo json_encode($output);
129 
130 
131 ?>