PageRenderTime 47ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax.php

https://code.google.com/p/enanocms/
PHP | 785 lines | 659 code | 87 blank | 39 comment | 124 complexity | 9e2bd9514a17fe880d7931271e378314 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
  4. * Copyright (C) 2006-2009 Dan Fuhry
  5. *
  6. * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  10. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
  11. */
  12. define('ENANO_INTERFACE_AJAX', '');
  13. require('includes/common.php');
  14. global $db, $session, $paths, $template, $plugins; // Common objects
  15. if(!isset($_GET['_mode'])) die('This script cannot be accessed directly.');
  16. $_ob = '';
  17. switch($_GET['_mode']) {
  18. case "checkusername":
  19. require_once(ENANO_ROOT.'/includes/pageutils.php');
  20. echo PageUtils::checkusername($_GET['name']);
  21. break;
  22. case "getsource":
  23. header('Content-type: text/plain');
  24. $password = ( isset($_GET['pagepass']) ) ? $_GET['pagepass'] : false;
  25. $revid = ( isset($_GET['revid']) ) ? intval($_GET['revid']) : 0;
  26. $page = new PageProcessor($paths->page_id, $paths->namespace, $revid);
  27. $page->password = $password;
  28. $have_draft = false;
  29. // Kinda hacky fix for issue 7: draft restore not offered for nonexistent pages
  30. if ( ($src = $page->fetch_source()) || !$page->exists() )
  31. {
  32. $allowed = true;
  33. $q = $db->sql_query('SELECT author, time_id, page_text, edit_summary, page_format FROM ' . table_prefix . 'logs WHERE log_type = \'page\' AND action = \'edit\'
  34. AND page_id = \'' . $db->escape($paths->page_id) . '\'
  35. AND namespace = \'' . $db->escape($paths->namespace) . '\'
  36. AND is_draft = 1;');
  37. if ( !$q )
  38. $db->die_json();
  39. if ( $db->numrows() > 0 )
  40. {
  41. $have_draft = true;
  42. $draft_row = $db->fetchrow($q);
  43. }
  44. }
  45. else if ( $src !== false )
  46. {
  47. $allowed = true;
  48. $src = '';
  49. }
  50. else
  51. {
  52. $allowed = false;
  53. $src = '';
  54. }
  55. $auth_edit = ( $session->get_permissions('edit_page') && ( $session->get_permissions('even_when_protected') || !$page->ns->page_protected ) );
  56. $auth_wysiwyg = ( $session->get_permissions('edit_wysiwyg') );
  57. $return = array(
  58. 'mode' => 'editor',
  59. 'src' => $src,
  60. 'auth_view_source' => $allowed,
  61. 'auth_edit' => $auth_edit,
  62. 'time' => time(),
  63. 'require_captcha' => false,
  64. 'allow_wysiwyg' => $auth_wysiwyg,
  65. 'revid' => $revid,
  66. 'have_draft' => false
  67. );
  68. $return['page_format'] = $page->ns->cdata['page_format'];
  69. $code = $plugins->setHook('get_page_source');
  70. foreach ( $code as $cmd )
  71. {
  72. eval($cmd);
  73. }
  74. if ( $have_draft )
  75. {
  76. $row =& $draft_row;
  77. $return['have_draft'] = true;
  78. $return['draft_author'] = $row['author'];
  79. $return['draft_time'] = enano_date(ED_DATE | ED_TIME, intval($row['time_id']));
  80. if ( isset($_GET['get_draft']) && @$_GET['get_draft'] === '1' )
  81. {
  82. $return['src'] = $row['page_text'];
  83. $return['edit_summary'] = $row['edit_summary'];
  84. $return['page_format'] = $row['page_format'];
  85. }
  86. }
  87. $return['undo_info'] = array();
  88. if ( $revid > 0 )
  89. {
  90. // Retrieve information about this revision and the current one
  91. $q = $db->sql_query('SELECT l1.author AS currentrev_author, l2.author AS oldrev_author FROM ' . table_prefix . 'logs AS l1
  92. LEFT JOIN ' . table_prefix . 'logs AS l2
  93. ON ( l2.log_id = ' . $revid . '
  94. AND l2.log_type = \'page\'
  95. AND l2.action = \'edit\'
  96. AND l2.page_id = \'' . $db->escape($paths->page_id) . '\'
  97. AND l2.namespace = \'' . $db->escape($paths->namespace) . '\'
  98. AND l2.is_draft != 1
  99. )
  100. WHERE l1.log_type = \'page\'
  101. AND l1.action = \'edit\'
  102. AND l1.page_id = \'' . $db->escape($paths->page_id) . '\'
  103. AND l1.namespace = \'' . $db->escape($paths->namespace) . '\'
  104. AND l1.time_id > ' . $page->revision_time . '
  105. AND l1.is_draft != 1
  106. ORDER BY l1.time_id DESC;');
  107. if ( !$q )
  108. $db->die_json();
  109. if ( $db->numrows() > 0 )
  110. {
  111. $rev_count = $db->numrows() - 1;
  112. if ( $rev_count == -1 )
  113. {
  114. $return = array(
  115. 'mode' => 'error',
  116. 'error' => '[Internal] No rows returned by revision info query. SQL:<pre>' . $db->latest_query . '</pre>'
  117. );
  118. }
  119. else
  120. {
  121. $row = $db->fetchrow();
  122. $return['undo_info'] = array(
  123. 'old_author' => $row['oldrev_author'],
  124. 'current_author' => $row['currentrev_author'],
  125. 'undo_count' => $rev_count
  126. );
  127. }
  128. }
  129. else
  130. {
  131. $return['revid'] = $revid = 0;
  132. }
  133. }
  134. if ( $auth_edit && !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
  135. {
  136. $return['require_captcha'] = true;
  137. $return['captcha_id'] = $session->make_captcha();
  138. }
  139. $template->load_theme();
  140. $return['toolbar_templates'] = $template->extract_vars('toolbar.tpl');
  141. $return['edit_notice'] = $template->get_wiki_edit_notice();
  142. $code = $plugins->setHook('ajax_getsource_send_data');
  143. foreach ( $code as $cmd )
  144. {
  145. eval($cmd);
  146. }
  147. echo enano_json_encode($return);
  148. break;
  149. case "getpage":
  150. // echo PageUtils::getpage($paths->page, false, ( (isset($_GET['oldid'])) ? $_GET['oldid'] : false ));
  151. $output = new Output_Striptease();
  152. $revision_id = ( (isset($_GET['oldid'])) ? intval($_GET['oldid']) : 0 );
  153. $page = new PageProcessor( $paths->page_id, $paths->namespace, $revision_id );
  154. $template->set_page($page);
  155. $pagepass = ( isset($_REQUEST['pagepass']) ) ? $_REQUEST['pagepass'] : '';
  156. $page->password = $pagepass;
  157. $page->allow_redir = ( !isset($_GET['redirect']) || (isset($_GET['redirect']) && $_GET['redirect'] !== 'no') );
  158. $page->send();
  159. break;
  160. case "savepage":
  161. /* **** OBSOLETE **** */
  162. break;
  163. case "savepage_json":
  164. header('Content-type: application/json');
  165. if ( !isset($_POST['r']) )
  166. die('Invalid request');
  167. try
  168. {
  169. $request = enano_json_decode($_POST['r']);
  170. if ( !isset($request['src']) || !isset($request['summary']) || !isset($request['minor_edit']) || !isset($request['time']) || !isset($request['draft']) )
  171. die('Invalid request');
  172. }
  173. catch(Zend_Json_Exception $e)
  174. {
  175. die("JSON parsing failed. View as HTML to see full report.\n<br /><br />\n<pre>" . htmlspecialchars(strval($e)) . "</pre><br />Request: <pre>" . htmlspecialchars($_POST['r']) . "</pre>");
  176. }
  177. $time = intval($request['time']);
  178. if ( $request['draft'] )
  179. {
  180. //
  181. // The user wants to save a draft version of the page.
  182. //
  183. // Validate permissions
  184. if ( !$session->get_permissions('edit_page') )
  185. {
  186. $return = array(
  187. 'mode' => 'error',
  188. 'error' => 'access_denied'
  189. );
  190. }
  191. else
  192. {
  193. // Delete any draft copies if they exist
  194. $q = $db->sql_query('DELETE FROM ' . table_prefix . 'logs WHERE log_type = \'page\' AND action = \'edit\'
  195. AND page_id = \'' . $db->escape($paths->page_id) . '\'
  196. AND namespace = \'' . $db->escape($paths->namespace) . '\'
  197. AND is_draft = 1;');
  198. if ( !$q )
  199. $db->die_json();
  200. // are we just supposed to delete the draft?
  201. if ( $request['src'] === -1 )
  202. {
  203. $return = array(
  204. 'mode' => 'success',
  205. 'is_draft' => 'delete'
  206. );
  207. }
  208. else
  209. {
  210. $src = RenderMan::preprocess_text($request['src'], false, false);
  211. $draft_format = $db->escape($request['format']);
  212. // Save the draft
  213. if ( !preg_match('/^[a-z0-9_]+$/', $draft_format) )
  214. {
  215. $return = array(
  216. 'mode' => 'error',
  217. 'error' => 'Page format must match /^[a-z0-9_]+$/'
  218. );
  219. }
  220. else
  221. {
  222. $q = $db->sql_query('INSERT INTO ' . table_prefix . 'logs ( log_type, action, page_id, namespace, author, author_uid, edit_summary, page_text, is_draft, time_id, page_format )
  223. VALUES (
  224. \'page\',
  225. \'edit\',
  226. \'' . $db->escape($paths->page_id) . '\',
  227. \'' . $db->escape($paths->namespace) . '\',
  228. \'' . $db->escape($session->username) . '\',
  229. ' . $session->user_id . ',
  230. \'' . $db->escape($request['summary']) . '\',
  231. \'' . $db->escape($src) . '\',
  232. 1,
  233. ' . time() . ',
  234. \'' . $draft_format . '\'
  235. );');
  236. // Done!
  237. $return = array(
  238. 'mode' => 'success',
  239. 'is_draft' => true
  240. );
  241. }
  242. }
  243. }
  244. }
  245. else
  246. {
  247. // Verify that no edits have been made since the editor was requested
  248. $q = $db->sql_query('SELECT time_id, author FROM ' . table_prefix . "logs WHERE log_type = 'page' AND action = 'edit' AND page_id = '{$paths->page_id}' AND namespace = '{$paths->namespace}' AND is_draft != 1 ORDER BY time_id DESC LIMIT 1;");
  249. if ( !$q )
  250. $db->die_json();
  251. $row = $db->fetchrow();
  252. $db->free_result();
  253. if ( $row['time_id'] > $time )
  254. {
  255. $return = array(
  256. 'mode' => 'obsolete',
  257. 'author' => $row['author'],
  258. 'date_string' => enano_date(ED_DATE | ED_TIME, $row['time_id']),
  259. 'time' => $row['time_id'] // time() ???
  260. );
  261. echo enano_json_encode($return);
  262. break;
  263. }
  264. // Verify captcha, if needed
  265. if ( !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
  266. {
  267. if ( !isset($request['captcha_id']) || !isset($request['captcha_code']) )
  268. {
  269. die('Invalid request, need captcha metadata');
  270. }
  271. $code_correct = strtolower($session->get_captcha($request['captcha_id']));
  272. $code_input = strtolower($request['captcha_code']);
  273. if ( $code_correct !== $code_input )
  274. {
  275. $return = array(
  276. 'mode' => 'errors',
  277. 'errors' => array($lang->get('editor_err_captcha_wrong')),
  278. 'new_captcha' => $session->make_captcha()
  279. );
  280. echo enano_json_encode($return);
  281. break;
  282. }
  283. }
  284. // Verification complete. Start the PageProcessor and let it do the dirty work for us.
  285. $page = new PageProcessor($paths->page_id, $paths->namespace);
  286. if ( $page->update_page($request['src'], $request['summary'], ( $request['minor_edit'] == 1 ), $request['format'], $request) )
  287. {
  288. $return = array(
  289. 'mode' => 'success',
  290. 'is_draft' => false
  291. );
  292. }
  293. else
  294. {
  295. $errors = array();
  296. while ( $err = $page->pop_error() )
  297. {
  298. $errors[] = $err;
  299. }
  300. $return = array(
  301. 'mode' => 'errors',
  302. 'errors' => array_values($errors)
  303. );
  304. if ( !$session->user_logged_in && getConfig('guest_edit_require_captcha') == '1' )
  305. {
  306. $return['new_captcha'] = $session->make_captcha();
  307. }
  308. }
  309. }
  310. // If this is based on a draft version, delete the draft - we no longer need it.
  311. if ( @$request['used_draft'] && !$request['draft'] )
  312. {
  313. $q = $db->sql_query('DELETE FROM ' . table_prefix . 'logs WHERE log_type = \'page\' AND action = \'edit\'
  314. AND page_id = \'' . $db->escape($paths->page_id) . '\'
  315. AND namespace = \'' . $db->escape($paths->namespace) . '\'
  316. AND is_draft = 1;');
  317. }
  318. echo enano_json_encode($return);
  319. break;
  320. case "diff_cur":
  321. // Lie about our content type to fool ad scripts
  322. header('Content-type: application/xhtml+xml');
  323. if ( !isset($_POST['text']) )
  324. die('Invalid request');
  325. $page = new PageProcessor($paths->page_id, $paths->namespace);
  326. if ( !($src = $page->fetch_source()) )
  327. {
  328. die('Access denied');
  329. }
  330. $diff = RenderMan::diff($src, $_POST['text']);
  331. if ( $diff == '<table class="diff"></table>' )
  332. {
  333. $diff = '<p>' . $lang->get('editor_msg_diff_empty') . '</p>';
  334. }
  335. echo '<div class="info-box">' . $lang->get('editor_msg_diff') . '</div>';
  336. echo $diff;
  337. break;
  338. case "protect":
  339. // echo PageUtils::protect($paths->page_id, $paths->namespace, (int)$_POST['level'], $_POST['reason']);
  340. if ( @$_POST['reason'] === '__ROLLBACK__' )
  341. {
  342. // __ROLLBACK__ is a keyword for log entries.
  343. die('"__ROLLBACK__" ain\'t gonna do it, buddy. Try to _not_ use reserved keywords next time, ok?');
  344. }
  345. $page = new PageProcessor($paths->page_id, $paths->namespace);
  346. header('Content-type: application/json');
  347. $result = $page->protect_page(intval($_POST['level']), $_POST['reason']);
  348. echo enano_json_encode($result);
  349. break;
  350. case "histlist":
  351. require_once(ENANO_ROOT.'/includes/pageutils.php');
  352. echo PageUtils::histlist($paths->page_id, $paths->namespace);
  353. break;
  354. case "rollback":
  355. $id = intval(@$_GET['id']);
  356. $page = new PageProcessor($paths->page_id, $paths->namespace);
  357. header('Content-type: application/json');
  358. $result = $page->rollback_log_entry($id);
  359. echo enano_json_encode($result);
  360. break;
  361. case "comments":
  362. require_once(ENANO_ROOT.'/includes/comment.php');
  363. $comments = new Comments($paths->page_id, $paths->namespace);
  364. if ( isset($_POST['data']) )
  365. {
  366. $comments->process_json($_POST['data']);
  367. }
  368. else
  369. {
  370. die('{ "mode" : "error", "error" : "No input" }');
  371. }
  372. break;
  373. case "rename":
  374. $page = new PageProcessor($paths->page_id, $paths->namespace);
  375. header('Content-type: application/json');
  376. $result = $page->rename_page($_POST['newtitle']);
  377. echo enano_json_encode($result);
  378. break;
  379. case "flushlogs":
  380. require_once(ENANO_ROOT.'/includes/pageutils.php');
  381. echo PageUtils::flushlogs($paths->page_id, $paths->namespace);
  382. break;
  383. case "deletepage":
  384. require_once(ENANO_ROOT.'/includes/pageutils.php');
  385. $reason = ( isset($_POST['reason']) ) ? $_POST['reason'] : false;
  386. if ( empty($reason) )
  387. die($lang->get('page_err_need_reason'));
  388. echo PageUtils::deletepage($paths->page_id, $paths->namespace, $reason);
  389. break;
  390. case "delvote":
  391. require_once(ENANO_ROOT.'/includes/pageutils.php');
  392. echo PageUtils::delvote($paths->page_id, $paths->namespace);
  393. break;
  394. case "resetdelvotes":
  395. require_once(ENANO_ROOT.'/includes/pageutils.php');
  396. echo PageUtils::resetdelvotes($paths->page_id, $paths->namespace);
  397. break;
  398. case "getstyles":
  399. require_once(ENANO_ROOT.'/includes/pageutils.php');
  400. echo PageUtils::getstyles($_GET['id']);
  401. break;
  402. case "catedit":
  403. require_once(ENANO_ROOT.'/includes/pageutils.php');
  404. echo PageUtils::catedit($paths->page_id, $paths->namespace);
  405. break;
  406. case "catsave":
  407. require_once(ENANO_ROOT.'/includes/pageutils.php');
  408. $categories = !empty($_POST['categories']) ? $_POST['categories'] : array();
  409. echo PageUtils::catsave($paths->page_id, $paths->namespace, $categories);
  410. break;
  411. case "setwikimode":
  412. require_once(ENANO_ROOT.'/includes/pageutils.php');
  413. echo PageUtils::setwikimode($paths->page_id, $paths->namespace, (int)$_GET['mode']);
  414. break;
  415. case "setpass":
  416. require_once(ENANO_ROOT.'/includes/pageutils.php');
  417. echo PageUtils::setpass($paths->page_id, $paths->namespace, $_POST['password']);
  418. break;
  419. case "fillusername":
  420. break;
  421. case "fillpagename":
  422. break;
  423. case "preview":
  424. require_once(ENANO_ROOT.'/includes/pageutils.php');
  425. $template->init_vars();
  426. echo PageUtils::genPreview($_POST['text']);
  427. break;
  428. case "transform":
  429. header('Content-type: text/javascript');
  430. if ( !isset($_GET['to']) )
  431. {
  432. echo enano_json_encode(array(
  433. 'mode' => 'error',
  434. 'error' => '"to" not specified'
  435. ));
  436. break;
  437. }
  438. if ( !isset($_POST['text']) )
  439. {
  440. echo enano_json_encode(array(
  441. 'mode' => 'error',
  442. 'error' => '"text" not specified (must be on POST)'
  443. ));
  444. break;
  445. }
  446. switch($_GET['to'])
  447. {
  448. case 'xhtml':
  449. $result = RenderMan::render($_POST['text'], RENDER_BLOCK | RENDER_NOSMILIES, false);
  450. break;
  451. case 'wikitext':
  452. $result = RenderMan::reverse_render($_POST['text']);
  453. break;
  454. default:
  455. $text =& $_POST['text'];
  456. $result = false;
  457. $code = $plugins->setHook('ajax_transform');
  458. foreach ( $code as $cmd )
  459. {
  460. eval($cmd);
  461. }
  462. if ( !$result )
  463. {
  464. echo enano_json_encode(array(
  465. 'mode' => 'error',
  466. 'error' => 'Invalid target format'
  467. ));
  468. break;
  469. }
  470. break;
  471. }
  472. // mostly for debugging, but I suppose this could be useful elsewhere.
  473. if ( isset($_POST['plaintext']) )
  474. die($result);
  475. echo enano_json_encode(array(
  476. 'mode' => 'transformed_text',
  477. 'text' => $result
  478. ));
  479. break;
  480. case "pagediff":
  481. require_once(ENANO_ROOT.'/includes/pageutils.php');
  482. $id1 = ( isset($_GET['diff1']) ) ? (int)$_GET['diff1'] : false;
  483. $id2 = ( isset($_GET['diff2']) ) ? (int)$_GET['diff2'] : false;
  484. if(!$id1 || !$id2) { echo '<p>Invalid request.</p>'; $template->footer(); break; }
  485. if(!preg_match('#^([0-9]+)$#', (string)$_GET['diff1']) ||
  486. !preg_match('#^([0-9]+)$#', (string)$_GET['diff2'] )) { echo '<p>SQL injection attempt</p>'; $template->footer(); break; }
  487. echo PageUtils::pagediff($paths->page_id, $paths->namespace, $id1, $id2);
  488. break;
  489. case "jsres":
  490. die('// ERROR: this section is deprecated and has moved to includes/clientside/static/enano-lib-basic.js.');
  491. break;
  492. case "rdns":
  493. if(!$session->get_permissions('mod_misc')) die('Go somewhere else for your reverse DNS info!');
  494. $ip = $_GET['ip'];
  495. if ( !is_valid_ip($ip) )
  496. {
  497. echo $lang->get('acpsl_err_invalid_ip');
  498. }
  499. $rdns = gethostbyaddr($ip);
  500. if ( $rdns == $ip )
  501. echo $lang->get('acpsl_err_ptr_no_resolve');
  502. else echo $rdns;
  503. break;
  504. case 'acljson':
  505. require_once(ENANO_ROOT.'/includes/pageutils.php');
  506. $parms = ( isset($_POST['acl_params']) ) ? rawurldecode($_POST['acl_params']) : false;
  507. echo PageUtils::acl_json($parms);
  508. break;
  509. case 'theme_list':
  510. header('Content-type: application/json');
  511. $return = array();
  512. foreach ( $template->theme_list as $theme )
  513. {
  514. if ( $theme['enabled'] != 1 )
  515. continue;
  516. $return[] = array(
  517. 'theme_name' => $theme['theme_name'],
  518. 'theme_id' => $theme['theme_id'],
  519. 'have_thumb' => file_exists(ENANO_ROOT . "/themes/{$theme['theme_id']}/preview.png")
  520. );
  521. }
  522. echo enano_json_encode($return);
  523. break;
  524. case "get_styles":
  525. if ( !preg_match('/^[A-z0-9_-]+$/', $_GET['theme_id']) )
  526. die(enano_json_encode(array()));
  527. $theme_id = $_GET['theme_id'];
  528. $return = array();
  529. if ( $dr = @opendir(ENANO_ROOT . "/themes/$theme_id/css/") )
  530. {
  531. while ( $dh = @readdir($dr) )
  532. {
  533. if ( preg_match('/\.css$/', $dh) && $dh != '_printable.css' )
  534. {
  535. $return[] = preg_replace('/\.css$/', '', $dh);
  536. }
  537. }
  538. }
  539. else
  540. {
  541. $return = array(
  542. 'mode' => 'error',
  543. 'error' => 'Could not open directory.'
  544. );
  545. }
  546. echo enano_json_encode($return);
  547. break;
  548. case "change_theme":
  549. if ( !isset($_POST['theme_id']) || !isset($_POST['style_id']) )
  550. {
  551. die(enano_json_encode(array('mode' => 'error', 'error' => 'Invalid parameter')));
  552. }
  553. if ( !preg_match('/^([a-z0-9_-]+)$/i', $_POST['theme_id']) || !preg_match('/^([a-z0-9_-]+)$/i', $_POST['style_id']) )
  554. {
  555. die(enano_json_encode(array('mode' => 'error', 'error' => 'Invalid parameter')));
  556. }
  557. if ( !file_exists(ENANO_ROOT . '/themes/' . $_POST['theme_id'] . '/css/' . $_POST['style_id'] . '.css') )
  558. {
  559. die(enano_json_encode(array('mode' => 'error', 'error' => 'Can\'t find theme file: ' . ENANO_ROOT . '/themes/' . $_POST['theme_id'] . '/css/' . $_POST['style_id'] . '.css')));;
  560. }
  561. if ( !$session->user_logged_in )
  562. {
  563. die(enano_json_encode(array('mode' => 'error', 'error' => 'You must be logged in to change your theme')));
  564. }
  565. // Just in case something slipped through...
  566. $theme_id = $db->escape($_POST['theme_id']);
  567. $style_id = $db->escape($_POST['style_id']);
  568. $e = $db->sql_query('UPDATE ' . table_prefix . "users SET theme = '$theme_id', style = '$style_id' WHERE user_id = $session->user_id;");
  569. if ( !$e )
  570. die( $db->get_error() );
  571. echo enano_json_encode(array(
  572. 'success' => true
  573. ));
  574. break;
  575. case 'get_tags':
  576. $ret = array('tags' => array(), 'user_level' => $session->user_level, 'can_add' => $session->get_permissions('tag_create'));
  577. $q = $db->sql_query('SELECT t.tag_id, t.tag_name, pg.pg_target IS NOT NULL AS used_in_acl, t.user_id FROM '.table_prefix.'tags AS t
  578. LEFT JOIN '.table_prefix.'page_groups AS pg
  579. ON ( ( pg.pg_type = ' . PAGE_GRP_TAGGED . ' AND pg.pg_target=t.tag_name ) OR ( pg.pg_type IS NULL AND pg.pg_target IS NULL ) )
  580. WHERE t.page_id=\'' . $db->escape($paths->page_id) . '\' AND t.namespace=\'' . $db->escape($paths->namespace) . '\';');
  581. if ( !$q )
  582. $db->_die();
  583. while ( $row = $db->fetchrow() )
  584. {
  585. $can_del = true;
  586. $perm = ( $row['user_id'] != $session->user_id ) ?
  587. 'tag_delete_other' :
  588. 'tag_delete_own';
  589. if ( $row['user_id'] == 1 && !$session->user_logged_in )
  590. // anonymous user trying to delete tag (hardcode blacklisted)
  591. $can_del = false;
  592. if ( !$session->get_permissions($perm) )
  593. $can_del = false;
  594. if ( $row['used_in_acl'] == 1 && !$session->get_permissions('edit_acl') && $session->user_level < USER_LEVEL_ADMIN )
  595. $can_del = false;
  596. $ret['tags'][] = array(
  597. 'id' => $row['tag_id'],
  598. 'name' => $row['tag_name'],
  599. 'can_del' => $can_del,
  600. 'acl' => ( $row['used_in_acl'] == 1 )
  601. );
  602. }
  603. echo enano_json_encode($ret);
  604. break;
  605. case 'addtag':
  606. $resp = array(
  607. 'success' => false,
  608. 'error' => 'No error',
  609. 'can_del' => ( $session->get_permissions('tag_delete_own') && $session->user_logged_in ),
  610. 'in_acl' => false
  611. );
  612. // first of course, are we allowed to tag pages?
  613. if ( !$session->get_permissions('tag_create') )
  614. {
  615. $resp['error'] = 'You are not permitted to tag pages.';
  616. die(enano_json_encode($resp));
  617. }
  618. // sanitize the tag name
  619. $tag = sanitize_tag($_POST['tag']);
  620. $tag = $db->escape($tag);
  621. if ( strlen($tag) < 2 )
  622. {
  623. $resp['error'] = 'Tags must consist of at least 2 alphanumeric characters.';
  624. die(enano_json_encode($resp));
  625. }
  626. // check if tag is already on page
  627. $q = $db->sql_query('SELECT 1 FROM '.table_prefix.'tags WHERE page_id=\'' . $db->escape($paths->page_id) . '\' AND namespace=\'' . $db->escape($paths->namespace) . '\' AND tag_name=\'' . $tag . '\';');
  628. if ( !$q )
  629. $db->_die();
  630. if ( $db->numrows() > 0 )
  631. {
  632. $resp['error'] = 'This page already has this tag.';
  633. die(enano_json_encode($resp));
  634. }
  635. $db->free_result();
  636. // tricky: make sure this tag isn't being used in some page group, and thus adding it could affect page access
  637. $can_edit_acl = ( $session->get_permissions('edit_acl') || $session->user_level >= USER_LEVEL_ADMIN );
  638. $q = $db->sql_query('SELECT 1 FROM '.table_prefix.'page_groups WHERE pg_type=' . PAGE_GRP_TAGGED . ' AND pg_target=\'' . $tag . '\';');
  639. if ( !$q )
  640. $db->_die();
  641. if ( $db->numrows() > 0 && !$can_edit_acl )
  642. {
  643. $resp['error'] = 'This tag is used in an ACL page group, and thus can\'t be added to a page by people without administrator privileges.';
  644. die(enano_json_encode($resp));
  645. }
  646. $resp['in_acl'] = ( $db->numrows() > 0 );
  647. $db->free_result();
  648. // we're good
  649. $q = $db->sql_query('INSERT INTO '.table_prefix.'tags(tag_name,page_id,namespace,user_id) VALUES(\'' . $tag . '\', \'' . $db->escape($paths->page_id) . '\', \'' . $db->escape($paths->namespace) . '\', ' . $session->user_id . ');');
  650. if ( !$q )
  651. $db->_die();
  652. $resp['success'] = true;
  653. $resp['tag'] = $tag;
  654. $resp['tag_id'] = $db->insert_id();
  655. echo enano_json_encode($resp);
  656. break;
  657. case 'deltag':
  658. $tag_id = intval($_POST['tag_id']);
  659. if ( empty($tag_id) )
  660. die('Invalid tag ID');
  661. $q = $db->sql_query('SELECT t.tag_id, t.user_id, t.page_id, t.namespace, pg.pg_target IS NOT NULL AS used_in_acl FROM '.table_prefix.'tags AS t
  662. LEFT JOIN '.table_prefix.'page_groups AS pg
  663. ON ( pg.pg_id IS NULL OR ( pg.pg_target = t.tag_name AND pg.pg_type = ' . PAGE_GRP_TAGGED . ' ) )
  664. WHERE t.tag_id=' . $tag_id . ';');
  665. if ( !$q )
  666. $db->_die();
  667. if ( $db->numrows() < 1 )
  668. die('Could not find a tag with that ID');
  669. $row = $db->fetchrow();
  670. $db->free_result();
  671. if ( $row['page_id'] == $paths->page_id && $row['namespace'] == $paths->namespace )
  672. $perms =& $session;
  673. else
  674. $perms = $session->fetch_page_acl($row['page_id'], $row['namespace']);
  675. $perm = ( $row['user_id'] != $session->user_id ) ?
  676. 'tag_delete_other' :
  677. 'tag_delete_own';
  678. if ( $row['user_id'] == 1 && !$session->user_logged_in )
  679. // anonymous user trying to delete tag (hardcode blacklisted)
  680. die('You are not authorized to delete this tag.');
  681. if ( !$perms->get_permissions($perm) )
  682. die('You are not authorized to delete this tag.');
  683. if ( $row['used_in_acl'] == 1 && !$perms->get_permissions('edit_acl') && $session->user_level < USER_LEVEL_ADMIN )
  684. die('You are not authorized to delete this tag.');
  685. // We're good
  686. $q = $db->sql_query('DELETE FROM '.table_prefix.'tags WHERE tag_id = ' . $tag_id . ';');
  687. if ( !$q )
  688. $db->_die();
  689. echo 'success';
  690. break;
  691. case 'ping':
  692. echo 'pong';
  693. break;
  694. default:
  695. die('Hacking attempt');
  696. break;
  697. }
  698. ?>