aboutsummaryrefslogtreecommitdiffstats
path: root/include/wiki.php
blob: e07cda660bd3398dce54997fce5563ab2cd73952 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
 * @file include/wiki.php
 * @brief Wiki related functions.
 */

use \Zotlabs\Storage\GitRepo as GitRepo;
define ( 'WIKI_ITEM_RESOURCE_TYPE', 'wiki' );

function wiki_list($nick, $observer_hash) {
	if (local_channel() || remote_channel()) {
		$sql_extra = item_permissions_sql(get_channel_by_nick($nick)['channel_id'], $observer_hash);
	} else {
		$sql_extra = " AND item_private = 0 ";
	}
	$wikis = q("SELECT * FROM item WHERE resource_type = '%s' AND mid = parent_mid AND item_deleted = 0 $sql_extra", 
			dbesc(WIKI_ITEM_RESOURCE_TYPE)
	);
	// TODO: query db for wikis the observer can access. Return with two lists, for read and write access
	return array('wikis' => $wikis);
}

function wiki_page_list($resource_id) {
	// TODO: Create item table records for pages so that metadata like title can be applied
	$w = wiki_get_wiki($resource_id);
	if (!$w['path']) {
		return array('pages' => null);
	}
	$pages = array();
	if (is_dir($w['path']) === true) {
		$files = array_diff(scandir($w['path']), array('.', '..', '.git'));
		// TODO: Check that the files are all text files
		$pages = $files;
	}

	return array('pages' => $pages);
}

function wiki_init_wiki($channel, $name) {
	// Store the path as a relative path, but pass absolute path to mkdir
	$path = 'store/[data]/git/'.$channel['channel_address'].'/wiki/'.$name;
	if (!os_mkdir(__DIR__ . '/../' . $path, 0770, true)) {
		logger('Error creating wiki path: ' . $name);
		return null;
	}
	// Create GitRepo object 	
	$git = new GitRepo($channel['channel_address'], null, false, $name, __DIR__ . '/../' . $path);	
	if(!$git->initRepo()) {
		logger('Error creating new git repo in ' . $git->path);
		return null;
	}
	
	return array('path' => $path);
}

function wiki_create_wiki($channel, $observer_hash, $name, $acl) {
	$wikiinit = wiki_init_wiki($channel, $name);	
	if (!$wikiinit['path']) {
		notice('Error creating wiki');
		return array('item' => null, 'success' => false);
	}
	$path = $wikiinit['path'];
	// Generate unique resource_id using the same method as item_message_id()
	do {
		$dups = false;
		$resource_id = random_string();
		$r = q("SELECT mid FROM item WHERE resource_id = '%s' AND resource_type = '%s' AND uid = %d LIMIT 1", 
			dbesc($resource_id), 
			dbesc(WIKI_ITEM_RESOURCE_TYPE), 
			intval($channel['channel_id'])
		);
		if (count($r))
			$dups = true;
	} while ($dups == true);
	$ac = $acl->get();
	$mid = item_message_id();
	$arr = array();	// Initialize the array of parameters for the post
	$item_hidden = 0; // TODO: Allow form creator to send post to ACL about new game automatically
	$wiki_url = z_root() . '/wiki/' . $channel['channel_address'] . '/' . $name;
	$arr['aid'] = $channel['channel_account_id'];
	$arr['uid'] = $channel['channel_id'];
	$arr['mid'] = $mid;
	$arr['parent_mid'] = $mid;
	$arr['item_hidden'] = $item_hidden;
	$arr['resource_type'] = WIKI_ITEM_RESOURCE_TYPE;
	$arr['resource_id'] = $resource_id;
	$arr['owner_xchan'] = $channel['channel_hash'];
	$arr['author_xchan'] = $observer_hash;
	$arr['plink'] = z_root() . '/channel/' . $channel['channel_address'] . '/?f=&mid=' . $arr['mid'];
	$arr['llink'] = $arr['plink'];
	$arr['title'] = $name;		 // name of new wiki;
	$arr['allow_cid'] = $ac['allow_cid'];
	$arr['allow_gid'] = $ac['allow_gid'];
	$arr['deny_cid'] = $ac['deny_cid'];
	$arr['deny_gid'] = $ac['deny_gid'];
	$arr['item_wall'] = 1;
	$arr['item_origin'] = 1;
	$arr['item_thread_top'] = 1;
	$arr['item_private'] = intval($acl->is_private());
	$arr['verb'] = ACTIVITY_CREATE;
	$arr['obj_type'] = ACTIVITY_OBJ_WIKI;
	$arr['object'] = array('path' => $path);
	$arr['body'] = '[table][tr][td][h1]New Wiki[/h1][/td][/tr][tr][td][zrl=' . $wiki_url . ']' . $name . '[/zrl][/td][/tr][/table]';

	$post = item_store($arr);
	$item_id = $post['item_id'];

	if ($item_id) {
		proc_run('php', "include/notifier.php", "activity", $item_id);
		return array('item' => $arr, 'success' => true);
	} else {
		return array('item' => null, 'success' => false);
	}
}

function wiki_delete_wiki($resource_id) {
		$item = q("SELECT id, object FROM item WHERE resource_type = '%s' AND resource_id = '%s' AND item_deleted = 0 limit 1",
            dbesc(WIKI_ITEM_RESOURCE_TYPE),
            dbesc($resource_id)
    );
    if (!$item) {
        return array('items' => null, 'success' => false);   
    } else {
        $drop = drop_item($item[0]['id'],false,DROPITEM_NORMAL,true);
				$object = json_decode($item[0]['object'], true);
				if(!realpath(__DIR__ . '/../' . $object['path'])) {
					return array('items' => null, 'success' => false); 
				}
				// Path to wiki exists
				$abs_path = realpath(__DIR__ . '/../' . $object['path']);
				$pathdel = rrmdir($abs_path);
				if($pathdel) {
					info('Wiki deleted successfully');
				}
        return array('item' => $item, 'success' => (($drop === 1 && $pathdel) ? true : false));   
    }
}

function wiki_get_wiki($resource_id) {
	$item = q("SELECT * FROM item WHERE resource_type = '%s' AND resource_id = '%s' AND item_deleted = 0 limit 1", 
						dbesc(WIKI_ITEM_RESOURCE_TYPE), 
						dbesc($resource_id)
	);
	if (!$item) {
		return array('wiki' => null, 'path' => null);
	} else {
		$w = $item[0];
		$object = json_decode($w['object'], true);
		if (!realpath(__DIR__ . '/../' . $object['path'])) {
			return array('wiki' => null, 'path' => null);
		}
		// Path to wiki exists
		$abs_path = realpath(__DIR__ . '/../' . $object['path']);
		return array('wiki' => $w, 'path' => $abs_path);
	}
}

function wiki_exists_by_name($uid, $name) {
		$item = q("SELECT id,resource_id FROM item WHERE resource_type = '%s' AND title = '%s' AND uid = '%s' AND item_deleted = 0 limit 1",
            dbesc(WIKI_ITEM_RESOURCE_TYPE),
            dbesc($name),
						dbesc($uid)
    );
    if (!$item) {
        return array('id' => null, 'resource_id' => null);   
    } else {
			return array('id' => $item[0]['id'], 'resource_id' => $item[0]['resource_id']);   
		}
	
}

function wiki_get_permissions($resource_id, $owner_id, $observer_hash) {
	// TODO: For now, only the owner can edit
	$sql_extra = item_permissions_sql($owner_id, $observer_hash);
	$r = q("SELECT * FROM item WHERE resource_type = '%s' AND resource_id = '%s' $sql_extra LIMIT 1",
				dbesc(WIKI_ITEM_RESOURCE_TYPE), 
        dbesc($resource_id)
    );
	if(!$r) {
		return array('read' => false, 'write' => false, 'success' => true);
	} else {
		return array('read' => true, 'write' => false, 'success' => true);
	}
}

function wiki_create_page($name, $resource_id) {
	$item = q("SELECT id,title,object FROM item WHERE resource_type = '%s' AND resource_id = '%s' AND item_deleted = 0 limit 1", 
		dbesc(WIKI_ITEM_RESOURCE_TYPE), 
		dbesc($resource_id)
	);
	if (!$item) {
		return array('page' => null, 'message' => 'Wiki item not found.', 'success' => false);
	}
	$object = json_decode($item[0]['object'], true);
	$wikiname = $item[0]['title'];
	if (!realpath(__DIR__ . '/../' . $object['path'])) {
		return array('page' => null, 'message' => 'Wiki directory does not exist.', 'success' => false);
	}
	// Path to wiki exists
	$abs_path = realpath(__DIR__ . '/../' . $object['path']);
	$page_path = $abs_path . '/' . $name;
	if (is_file($page_path)) {
		return array('page' => null, 'message' => 'Page already exists.', 'success' => false);
	}
	// Create file called $name in the path
	if(!touch($page_path)) {
		return array('page' => null, 'message' => 'Page file cannot be created.', 'success' => false);
	} else {
		return array('wiki' => $wikiname, 'message' => '', 'success' => true);
	}
	
}

function wiki_get_page_content($arr) {
	$page = ((array_key_exists('page',$arr)) ? $arr['page'] : '');
	// TODO: look for page resource_id and retrieve that way alternatively
	$wiki_resource_id = ((array_key_exists('wiki_resource_id',$arr)) ? $arr['wiki_resource_id'] : '');
	$w = wiki_get_wiki($wiki_resource_id);
	if (!$w['path']) {
		return array('content' => null, 'message' => 'Error reading wiki', 'success' => false);
	}
	$page_path = $w['path'].'/'.escape_tags(urlencode($page));
	if (is_readable($page_path) === true) {
		if(filesize($page_path) === 0) {
			$content = '';
		} else {
			$content = file_get_contents($page_path);
			if(!$content) {
				return array('content' => null, 'message' => 'Error reading page content', 'success' => false);
			}
		}		
		// TODO: Check that the files are all text files
		return array('content' => json_encode($content), 'message' => '', 'success' => true);
	}
}


function wiki_save_page($arr) {
	$pagename = ((array_key_exists('name',$arr)) ? $arr['name'] : '');
	$content = ((array_key_exists('content',$arr)) ? $arr['content'] : '');
	$resource_id = ((array_key_exists('resource_id',$arr)) ? $arr['resource_id'] : '');
	$w = wiki_get_wiki($resource_id);
	if (!$w['path']) {
		return array('message' => 'Error reading wiki', 'success' => false);
	}
	$page_path = $w['path'].'/'.$pagename;
	if (is_writable($page_path) === true) {
		if(!file_put_contents($page_path, $content)) {
			return array('message' => 'Error writing to page file', 'success' => false);
		}
		return array('message' => '', 'success' => true);
	} else {
		return array('message' => 'Page file not writable', 'success' => false);
	}	
}

function wiki_git_commit($arr) {
	$files = ((array_key_exists('files', $arr)) ? $arr['files'] : null);
	$commit_msg = ((array_key_exists('commit_msg', $arr)) ? $arr['commit_msg'] : 'Repo updated');
	$resource_id = ((array_key_exists('resource_id', $arr)) ? $arr['resource_id'] : json_return_and_die(array('message' => 'Wiki resource_id required for git commit', 'success' => false)));
	$observer = ((array_key_exists('observer', $arr)) ? $arr['observer'] : json_return_and_die(array('message' => 'Observer required for git commit', 'success' => false)));
	$w = wiki_get_wiki($resource_id);
	if (!$w['path']) {
		return array('message' => 'Error reading wiki', 'success' => false);
	}
	$reponame = ((array_key_exists('title', $w['wiki'])) ? $w['wiki']['title'] : 'repo');
	if($reponame === '') {
		$reponame = 'repo';
	}
	$git = new GitRepo('sys', null, false, $w['wiki']['title'], $w['path']);
	try {
		$git->setIdentity($observer['xchan_name'], $observer['xchan_addr']);
		if ($files === null) {
			$options = array('all' => true); // git commit option to include all changes
		} else {
			$options = array(); // git commit options
			foreach ($files as $file) {
				if (!$git->git->add($file)) {	// add specified files to the git repo stage
					if (!$git->git->reset->hard()) {
						json_return_and_die(array('message' => 'Error adding file to git stage: ' . $file . '. Error resetting git repo.', 'success' => false));
					}
					json_return_and_die(array('message' => 'Error adding file to git stage: ' . $file, 'success' => false));
				}
			}
		}
		if ($git->commit($commit_msg, $options)) {
			json_return_and_die(array('message' => 'Wiki repo commit succeeded', 'success' => true));
		} else {
			json_return_and_die(array('message' => 'Wiki repo commit failed', 'success' => false));
		}
	} catch (\PHPGit\Exception\GitException $e) {
		json_return_and_die(array('message' => 'GitRepo error thrown', 'success' => false));
	}
}