Automad
 All Classes Functions Variables Pages
add_page.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 namespace Automad\Core;
39 
40 
41 defined('AUTOMAD') or die('Direct access not permitted!');
42 
43 
44 /*
45  * Add a new page below the current page.
46  */
47 
48 
49 $output = array();
50 
51 
52 // Validation of $_POST. URL, title and template must exist and != false.
53 if (isset($_POST['url']) && array_key_exists($_POST['url'], $this->collection)) {
54 
55  if (isset($_POST['subpage']) && isset($_POST['subpage']['title']) && $_POST['subpage']['title'] && isset($_POST['subpage']['theme_template']) && $_POST['subpage']['theme_template']) {
56 
57  // The current page, where the subpage has to be added to, becomes the parent page for the new page.
58  $Page = $this->collection[$_POST['url']];
59 
60  // Check if the current page's directory is writable.
61  if (is_writable(dirname($this->pageFile($Page)))) {
62 
63 
64  // The new page's properties
65  $title = $_POST['subpage']['title'];
66  $theme_template = $_POST['subpage']['theme_template'];
67 
68 
69  // Build initial content for data file.
70  $content = AM_KEY_TITLE . AM_PARSE_PAIR_SEPARATOR . ' ' . $title;
71 
72 
73  // The new page's theme.
74  if (dirname($theme_template) != '.') {
75  $content .= "\r\n\r\n" . AM_PARSE_BLOCK_SEPARATOR . "\r\n\r\n" . AM_KEY_THEME . AM_PARSE_PAIR_SEPARATOR . ' ' . dirname($theme_template);
76  }
77 
78 
79  // Save new subpage below the current page's path.
80  $subdir = Parse::sanitize($title, true) . '/';
81  $newPagePath = $Page->path . $subdir;
82 
83 
84  $i = 1;
85 
86  // In case page exists already...
87  while (file_exists(AM_BASE_DIR . AM_DIR_PAGES . $newPagePath)) {
88  $newPagePath = $Page->path . $i . '.' . $subdir;
89  $i++;
90  }
91 
92 
93  // Build the file name.
94  $file = AM_BASE_DIR . AM_DIR_PAGES . $newPagePath . str_replace('.php', '', basename($theme_template)) . '.' . AM_FILE_EXT_DATA;
95 
96 
97  // Save content.
98  $old = umask(0);
99 
100  if (!file_exists(AM_BASE_DIR . AM_DIR_PAGES . $newPagePath)) {
101  mkdir(AM_BASE_DIR . AM_DIR_PAGES . $newPagePath, 0777, true);
102  }
103 
104  file_put_contents($file, $content);
105 
106  umask($old);
107 
108 
109  // Clear the cache to make sure, the changes get reflected on the website directly.
110  $Cache = new Cache();
111  $Cache->clear();
112 
113  // Rebuild Automad object, since the file structure has changed.
114  $Automad = new Automad(false);
115  $collection = $Automad->getCollection();
116 
117  // Find new URL and return redirect query string.
118  foreach ($collection as $key => $page) {
119 
120  if ($page->path == $newPagePath) {
121 
122  $output['redirect'] = '?context=edit_page&url=' . urlencode($key);
123  break;
124 
125  }
126 
127  }
128 
129 
130  } else {
131 
132  $output['error'] = $this->tb['error_permission'] . '<p>' . dirname($this->pageFile($Page)) . '</p>';
133 
134  }
135 
136  } else {
137 
138  $output['error'] = $this->tb['error_page_title'];
139 
140  }
141 
142 } else {
143 
144  $output['error'] = $this->tb['error_page_not_found'];
145 
146 }
147 
148 
149 echo json_encode($output);
150 
151 
152 ?>
static sanitize($str, $removeDots=false)
Definition: parse.php:413