PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/manager/processors/save_content.processor.php

https://github.com/good-web-master/modx.evo.custom
PHP | 707 lines | 617 code | 44 blank | 46 comment | 109 complexity | 123323b927622ee6288c1446eb5527b3 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. if (IN_MANAGER_MODE != "true")
  3. die("<b>INCLUDE_ORDERING_ERROR</b><br /><br />Please use the MODx Content Manager instead of accessing this file directly.");
  4. if (!$modx->hasPermission('save_document')) {
  5. $e->setError(3);
  6. $e->dumpError();
  7. }
  8. // preprocess POST values
  9. $id = is_numeric($_POST['id']) ? $_POST['id'] : '';
  10. $introtext = $modx->db->escape($_POST['introtext']);
  11. $content = $modx->db->escape($_POST['ta']);
  12. $pagetitle = $modx->db->escape($_POST['pagetitle']);
  13. $description = $modx->db->escape($_POST['description']);
  14. $alias = $modx->db->escape($_POST['alias']);
  15. $link_attributes = $modx->db->escape($_POST['link_attributes']);
  16. $isfolder = $_POST['isfolder'];
  17. $richtext = $_POST['richtext'];
  18. $published = $_POST['published'];
  19. $parent = $_POST['parent'] != '' ? $_POST['parent'] : 0;
  20. $template = $_POST['template'];
  21. $menuindex = !empty($_POST['menuindex']) ? $_POST['menuindex'] : 0;
  22. $searchable = $_POST['searchable'];
  23. $cacheable = $_POST['cacheable'];
  24. $syncsite = $_POST['syncsite'];
  25. $pub_date = $_POST['pub_date'];
  26. $unpub_date = $_POST['unpub_date'];
  27. $document_groups = (isset($_POST['chkalldocs']) && $_POST['chkalldocs'] == 'on') ? array() : $_POST['docgroups'];
  28. $type = $_POST['type'];
  29. $keywords = $_POST['keywords'];
  30. $metatags = $_POST['metatags'];
  31. $contentType = $modx->db->escape($_POST['contentType']);
  32. $contentdispo = intval($_POST['content_dispo']);
  33. $longtitle = $modx->db->escape($_POST['longtitle']);
  34. $donthit = intval($_POST['donthit']);
  35. $menutitle = $modx->db->escape($_POST['menutitle']);
  36. $hidemenu = intval($_POST['hidemenu']);
  37. $aliasvisible = $_POST['alias_visible'];
  38. if (trim($pagetitle == "")) {
  39. if ($type == "reference") {
  40. $pagetitle = $_lang['untitled_weblink'];
  41. } else {
  42. $pagetitle = $_lang['untitled_resource'];
  43. }
  44. }
  45. // get table names
  46. $tbl_document_groups = $modx->getFullTableName('document_groups');
  47. $tbl_documentgroup_names = $modx->getFullTableName('documentgroup_names');
  48. $tbl_member_groups = $modx->getFullTableName('member_groups');
  49. $tbl_membergroup_access = $modx->getFullTableName('membergroup_access');
  50. $tbl_keyword_xref = $modx->getFullTableName('keyword_xref');
  51. $tbl_site_content = $modx->getFullTableName('site_content');
  52. $tbl_site_content_metatags = $modx->getFullTableName('site_content_metatags');
  53. $tbl_site_tmplvar_access = $modx->getFullTableName('site_tmplvar_access');
  54. $tbl_site_tmplvar_contentvalues = $modx->getFullTableName('site_tmplvar_contentvalues');
  55. $tbl_site_tmplvar_templates = $modx->getFullTableName('site_tmplvar_templates');
  56. $tbl_site_tmplvars = $modx->getFullTableName('site_tmplvars');
  57. $actionToTake = "new";
  58. if ($_POST['mode'] == '73' || $_POST['mode'] == '27') {
  59. $actionToTake = "edit";
  60. }
  61. // friendly url alias checks
  62. if ($friendly_urls) {
  63. // auto assign alias
  64. if (!$alias && $automatic_alias) {
  65. $alias = strtolower($modx->stripAlias(trim($pagetitle)));
  66. if(!$allow_duplicate_alias) {
  67. if ($modx->db->getValue("SELECT COUNT(id) FROM " . $tbl_site_content . " WHERE id<>'$id' AND alias='$alias'") != 0) {
  68. $cnt = 1;
  69. $tempAlias = $alias;
  70. while ($modx->db->getValue("SELECT COUNT(id) FROM " . $tbl_site_content . " WHERE id<>'$id' AND alias='$tempAlias'") != 0) {
  71. $tempAlias = $alias;
  72. $tempAlias .= $cnt;
  73. $cnt++;
  74. }
  75. $alias = $tempAlias;
  76. }
  77. }
  78. }
  79. // check for duplicate alias name if not allowed
  80. elseif ($alias && !$allow_duplicate_alias) {
  81. $alias = $modx->stripAlias($alias);
  82. if ($use_alias_path) {
  83. // only check for duplicates on the same level if alias_path is on
  84. $docid = $modx->db->getValue("SELECT id FROM " . $tbl_site_content . " WHERE id<>'$id' AND alias='$alias' AND parent=$parent LIMIT 1");
  85. } else {
  86. $docid = $modx->db->getValue("SELECT id FROM " . $tbl_site_content . " WHERE id<>'$id' AND alias='$alias' LIMIT 1");
  87. }
  88. if ($docid > 0) {
  89. if ($actionToTake == 'edit') {
  90. $modx->manager->saveFormValues(27);
  91. $url = "index.php?a=27&id=" . $id;
  92. include_once "header.inc.php";
  93. $modx->webAlert(sprintf($_lang["duplicate_alias_found"], $docid, $alias), $url);
  94. include_once "footer.inc.php";
  95. exit;
  96. } else {
  97. $modx->manager->saveFormValues(4);
  98. $url = "index.php?a=4";
  99. include_once "header.inc.php";
  100. $modx->webAlert(sprintf($_lang["duplicate_alias_found"], $docid, $alias), $url);
  101. include_once "footer.inc.php";
  102. exit;
  103. }
  104. }
  105. }
  106. // strip alias of special characters
  107. elseif ($alias) {
  108. $alias = $modx->stripAlias($alias);
  109. }
  110. }
  111. elseif ($alias) {
  112. $alias = $modx->stripAlias($alias);
  113. }
  114. // determine published status
  115. $currentdate = time();
  116. if (empty ($pub_date)) {
  117. $pub_date = 0;
  118. } else {
  119. $pub_date = $modx->toTimeStamp($pub_date);
  120. if ($pub_date < $currentdate) {
  121. $published = 1;
  122. }
  123. elseif ($pub_date > $currentdate) {
  124. $published = 0;
  125. }
  126. }
  127. if (empty ($unpub_date)) {
  128. $unpub_date = 0;
  129. } else {
  130. $unpub_date = $modx->toTimeStamp($unpub_date);
  131. if ($unpub_date < $currentdate) {
  132. $published = 0;
  133. }
  134. }
  135. // get document groups for current user
  136. $tmplvars = array ();
  137. if ($_SESSION['mgrDocgroups']) {
  138. $docgrp = implode(",", $_SESSION['mgrDocgroups']);
  139. }
  140. // ensure that user has not made this document inaccessible to themselves
  141. if($_SESSION['mgrRole'] != 1 && is_array($document_groups)) {
  142. $document_group_list = implode(',', $document_groups);
  143. $document_group_list = implode(',', array_filter(explode(',',$document_group_list), 'is_numeric'));
  144. if(!empty($document_group_list)) {
  145. $sql = "SELECT COUNT(mg.id) FROM {$tbl_membergroup_access} mga, {$tbl_member_groups} mg
  146. WHERE mga.membergroup = mg.user_group
  147. AND mga.documentgroup IN({$document_group_list})
  148. AND mg.member = {$_SESSION['mgrInternalKey']};";
  149. $rs = $modx->db->query($sql);
  150. $count = $modx->db->getValue($rs);
  151. if($count == 0) {
  152. if ($actionToTake == 'edit') {
  153. $modx->manager->saveFormValues(27);
  154. $url = "index.php?a=27&id=" . $id;
  155. include_once "header.inc.php";
  156. $modx->webAlert(sprintf($_lang["resource_permissions_error"]), $url);
  157. include_once "footer.inc.php";
  158. exit;
  159. } else {
  160. $modx->manager->saveFormValues(4);
  161. $url = "index.php?a=4";
  162. include_once "header.inc.php";
  163. $modx->webAlert(sprintf($_lang["resource_permissions_error"]), $url);
  164. include_once "footer.inc.php";
  165. exit;
  166. }
  167. }
  168. }
  169. }
  170. $sql = "SELECT DISTINCT tv.*, IF(tvc.value!='',tvc.value,tv.default_text) as value ";
  171. $sql .= "FROM $tbl_site_tmplvars AS tv ";
  172. $sql .= "INNER JOIN $tbl_site_tmplvar_templates AS tvtpl ON tvtpl.tmplvarid = tv.id ";
  173. $sql .= "LEFT JOIN $tbl_site_tmplvar_contentvalues AS tvc ON tvc.tmplvarid=tv.id AND tvc.contentid = '$id' ";
  174. $sql .= "LEFT JOIN $tbl_site_tmplvar_access tva ON tva.tmplvarid=tv.id ";
  175. $sql .= "WHERE tvtpl.templateid = '" . $template . "' AND (1='" . $_SESSION['mgrRole'] . "' OR ISNULL(tva.documentgroup)" . ((!$docgrp) ? "" : " OR tva.documentgroup IN ($docgrp)") . ") ORDER BY tv.rank;";
  176. $rs = $modx->db->query($sql);
  177. while ($row = $modx->db->getRow($rs)) {
  178. $tmplvar = '';
  179. switch ($row['type']) {
  180. case 'url':
  181. $tmplvar = $_POST["tv" . $row['id']];
  182. if ($_POST["tv" . $row['id'] . '_prefix'] != '--') {
  183. $tmplvar = str_replace(array (
  184. "feed://",
  185. "ftp://",
  186. "http://",
  187. "https://",
  188. "mailto:"
  189. ), "", $tmplvar);
  190. $tmplvar = $_POST["tv" . $row['id'] . '_prefix'] . $tmplvar;
  191. }
  192. break;
  193. case 'file':
  194. $tmplvar = $_POST["tv" . $row['id']];
  195. break;
  196. default:
  197. if (is_array($_POST["tv" . $row['id']])) {
  198. // handles checkboxes & multiple selects elements
  199. $feature_insert = array ();
  200. $lst = $_POST["tv" . $row['id']];
  201. while (list ($featureValue, $feature_item) = each($lst)) {
  202. $feature_insert[count($feature_insert)] = $feature_item;
  203. }
  204. $tmplvar = implode("||", $feature_insert);
  205. } else {
  206. $tmplvar = $_POST["tv" . $row['id']];
  207. }
  208. break;
  209. }
  210. // save value if it was modified
  211. if (strlen($tmplvar) > 0 && $tmplvar != $row['default_text']) {
  212. $tmplvars[$row['id']] = array (
  213. $row['id'],
  214. $tmplvar
  215. );
  216. } else {
  217. // Mark the variable for deletion
  218. $tmplvars[$row['name']] = $row['id'];
  219. }
  220. }
  221. // get the document, but only if it already exists
  222. if ($actionToTake != "new") {
  223. $rs = $modx->db->select('*', $tbl_site_content, 'id='.$id);
  224. $limit = $modx->db->getRecordCount($rs);
  225. if ($limit > 1) {
  226. $e->setError(6);
  227. $e->dumpError();
  228. }
  229. if ($limit < 1) {
  230. $e->setError(7);
  231. $e->dumpError();
  232. }
  233. $existingDocument = $modx->db->getRow($rs);
  234. }
  235. // check to see if the user is allowed to save the document in the place he wants to save it in
  236. if ($use_udperms == 1) {
  237. if ($existingDocument['parent'] != $parent) {
  238. include_once "./processors/user_documents_permissions.class.php";
  239. $udperms = new udperms();
  240. $udperms->user = $modx->getLoginUserID();
  241. $udperms->document = $parent;
  242. $udperms->role = $_SESSION['mgrRole'];
  243. if (!$udperms->checkPermissions()) {
  244. if ($actionToTake == 'edit') {
  245. $modx->manager->saveFormValues(27);
  246. $url = "index.php?a=27&id=" . $id;
  247. include_once "header.inc.php";
  248. $modx->webAlert(sprintf($_lang['access_permission_parent_denied'], $docid, $alias), $url);
  249. include_once "footer.inc.php";
  250. exit;
  251. } else {
  252. $modx->manager->saveFormValues(4);
  253. $url = "index.php?a=4";
  254. include_once "header.inc.php";
  255. $modx->webAlert(sprintf($_lang['access_permission_parent_denied'], $docid, $alias), $url);
  256. include_once "footer.inc.php";
  257. exit;
  258. }
  259. }
  260. }
  261. }
  262. switch ($actionToTake) {
  263. case 'new' :
  264. // invoke OnBeforeDocFormSave event
  265. $modx->invokeEvent("OnBeforeDocFormSave", array (
  266. "mode" => "new",
  267. "id" => $id
  268. ));
  269. // deny publishing if not permitted
  270. if (!$modx->hasPermission('publish_document')) {
  271. $pub_date = 0;
  272. $unpub_date = 0;
  273. $published = 0;
  274. }
  275. $publishedon = ($published ? time() : 0);
  276. $publishedby = ($published ? $modx->getLoginUserID() : 0);
  277. //$sql = "INSERT INTO $tbl_site_content (introtext,content, pagetitle, longtitle, type, description, alias, link_attributes, isfolder, richtext, published, parent, template, menuindex, searchable, cacheable, createdby, createdon, editedby, editedon, publishedby, publishedon, pub_date, unpub_date, contentType, content_dispo, donthit, menutitle, hidemenu)
  278. // VALUES('" . $introtext . "','" . $content . "', '" . $pagetitle . "', '" . $longtitle . "', '" . $type . "', '" . $description . "', '" . $alias . "', '" . $link_attributes . "', '" . $isfolder . "', '" . $richtext . "', '" . $published . "', '" . $parent . "', '" . $template . "', '" . $menuindex . "', '" . $searchable . "', '" . $cacheable . "', '" . $modx->getLoginUserID() . "', " . time() . ", '" . $modx->getLoginUserID() . "', " . time() . ", " . $publishedby . ", " . $publishedon . ", '$pub_date', '$unpub_date', '$contentType', '$contentdispo', '$donthit', '$menutitle', '$hidemenu')";
  279. $sql = "INSERT INTO $tbl_site_content (introtext,content, pagetitle, longtitle, type, description, alias, link_attributes, isfolder, richtext, published, parent, template, menuindex, searchable, cacheable, createdby, createdon, editedby, editedon, publishedby, publishedon, pub_date, unpub_date, contentType, content_dispo, donthit, menutitle, hidemenu, alias_visible) VALUES('" . $introtext . "','" . $content . "', '" . $pagetitle . "', '" . $longtitle . "', '" . $type . "', '" . $description . "', '" . $alias . "', '" . $link_attributes . "', '" . $isfolder . "', '" . $richtext . "', '" . $published . "', '" . $parent . "', '" . $template . "', '" . $menuindex . "', '" . $searchable . "', '" . $cacheable . "', '" . $modx->getLoginUserID() . "', " . time() . ", '" . $modx->getLoginUserID() . "', " . time() . ", " . $publishedby . ", " . $publishedon . ", '$pub_date', '$unpub_date', '$contentType', '$contentdispo', '$donthit', '$menutitle', '$hidemenu', '$aliasvisible')";
  280. $rs = $modx->db->query($sql);
  281. if (!$rs) {
  282. $modx->manager->saveFormValues(27);
  283. echo "An error occured while attempting to save the new document: " . $modx->db->getLastError();
  284. exit;
  285. }
  286. if (!$key = $modx->db->getInsertId()) {
  287. $modx->manager->saveFormValues(27);
  288. echo "Couldn't get last insert key!";
  289. exit;
  290. }
  291. $tvChanges = array();
  292. foreach ($tmplvars as $field => $value) {
  293. if (is_array($value)) {
  294. $tvId = $value[0];
  295. $tvVal = $value[1];
  296. $tvChanges[] = array('tmplvarid' => $tvId, 'contentid' => $key, 'value' => $modx->db->escape($tvVal));
  297. }
  298. }
  299. if (!empty($tvChanges)) {
  300. foreach ($tvChanges as $tv) {
  301. $rs = $modx->db->insert($tv, $tbl_site_tmplvar_contentvalues);
  302. }
  303. }
  304. // document access permissions
  305. $docgrp_save_attempt = false;
  306. if ($use_udperms == 1 && is_array($document_groups)) {
  307. $new_groups = array();
  308. foreach ($document_groups as $value_pair) {
  309. // first, split the pair (this is a new document, so ignore the second value
  310. list($group) = explode(',', $value_pair); // @see manager/actions/mutate_content.dynamic.php @ line 1138 (permissions list)
  311. $new_groups[] = '('.(int)$group.','.$key.')';
  312. }
  313. $saved = true;
  314. if (!empty($new_groups)) {
  315. $sql = 'INSERT INTO '.$tbl_document_groups.' (document_group, document) VALUES '. implode(',', $new_groups);
  316. $saved = $modx->db->query($sql) ? $saved : false;
  317. $docgrp_save_attempt = true;
  318. }
  319. } else {
  320. $isManager = $modx->hasPermission('access_permissions');
  321. $isWeb = $modx->hasPermission('web_access_permissions');
  322. if($use_udperms && !($isManager || $isWeb) && $parent != 0) {
  323. // inherit document access permissions
  324. $sql = "INSERT INTO $tbl_document_groups (document_group, document) SELECT document_group, $key FROM $tbl_document_groups WHERE document = $parent";
  325. $saved = $modx->db->query($sql);
  326. $docgrp_save_attempt = true;
  327. }
  328. }
  329. if ($docgrp_save_attempt && !$saved) {
  330. $modx->manager->saveFormValues(27);
  331. echo "An error occured while attempting to add the document to a document_group.";
  332. exit;
  333. }
  334. // update parent folder status
  335. if ($parent != 0) {
  336. $fields = array('isfolder' => 1);
  337. $rs = $modx->db->update($fields, $tbl_site_content, 'id='.$_REQUEST['parent']);
  338. if (!$rs) {
  339. echo "An error occured while attempting to change the document's parent to a folder.";
  340. }
  341. }
  342. // save META Keywords
  343. saveMETAKeywords($key);
  344. // invoke OnDocFormSave event
  345. $modx->invokeEvent("OnDocFormSave", array (
  346. "mode" => "new",
  347. "id" => $key
  348. ));
  349. // secure web documents - flag as private
  350. include $base_path . "manager/includes/secure_web_documents.inc.php";
  351. secureWebDocument($key);
  352. // secure manager documents - flag as private
  353. include $base_path . "manager/includes/secure_mgr_documents.inc.php";
  354. secureMgrDocument($key);
  355. if ($syncsite == 1) {
  356. // empty cache
  357. include_once "cache_sync.class.processor.php";
  358. $sync = new synccache();
  359. $sync->setCachepath("../assets/cache/");
  360. $sync->setReport(false);
  361. $sync->emptyCache();
  362. }
  363. // redirect/stay options
  364. if ($_POST['stay'] != '') {
  365. // weblink
  366. if ($_POST['mode'] == "72")
  367. $a = ($_POST['stay'] == '2') ? "27&id=$key" : "72&pid=$parent";
  368. // document
  369. if ($_POST['mode'] == "4")
  370. $a = ($_POST['stay'] == '2') ? "27&id=$key" : "4&pid=$parent";
  371. $header = "Location: index.php?a=" . $a . "&r=1&stay=" . $_POST['stay'];
  372. } else {
  373. $header = "Location: index.php?r=1&id=$id&a=7&dv=1";
  374. }
  375. header($header);
  376. break;
  377. case 'edit' :
  378. // get the document's current parent
  379. $rs = $modx->db->select('parent', $tbl_site_content, 'id='.$_REQUEST['id']);
  380. if (!$rs) {
  381. $modx->manager->saveFormValues(27);
  382. echo "An error occured while attempting to find the document's current parent.";
  383. exit;
  384. }
  385. $row = $modx->db->getRow($rs);
  386. $oldparent = $row['parent'];
  387. $doctype = $row['type'];
  388. if ($id == $site_start && $published == 0) {
  389. $modx->manager->saveFormValues(27);
  390. echo "Document is linked to site_start variable and cannot be unpublished!";
  391. exit;
  392. }
  393. if ($id == $site_start && ($pub_date != "0" || $unpub_date != "0")) {
  394. $modx->manager->saveFormValues(27);
  395. echo "Document is linked to site_start variable and cannot have publish or unpublish dates set!";
  396. exit;
  397. }
  398. if ($parent == $id) {
  399. $modx->manager->saveFormValues(27);
  400. echo "Document can not be it's own parent!";
  401. exit;
  402. }
  403. // check to see document is a folder
  404. $rs = $modx->db->select('COUNT(id)', $tbl_site_content, 'parent='. $_REQUEST['id']);
  405. if (!$rs) {
  406. $modx->manager->saveFormValues(27);
  407. echo "An error occured while attempting to find the document's children.";
  408. exit;
  409. }
  410. $row = $modx->db->getRow($rs);
  411. if ($row['COUNT(id)'] > 0) {
  412. $isfolder = 1;
  413. }
  414. // set publishedon and publishedby
  415. $was_published = $modx->db->getValue("SELECT published FROM $tbl_site_content WHERE id='$id'");
  416. // keep original publish state, if change is not permitted
  417. if (!$modx->hasPermission('publish_document')) {
  418. $published = $was_published;
  419. $pub_date = 'pub_date';
  420. $unpub_date = 'unpub_date';
  421. }
  422. // if it was changed from unpublished to published
  423. if (!$was_published && $published) {
  424. $publishedon = time();
  425. $publishedby = $modx->getLoginUserID();
  426. }
  427. elseif ($was_published && !$published) {
  428. $publishedon = 0;
  429. $publishedby = 0;
  430. } else {
  431. $publishedon = 'publishedon';
  432. $publishedby = 'publishedby';
  433. }
  434. // invoke OnBeforeDocFormSave event
  435. $modx->invokeEvent("OnBeforeDocFormSave", array (
  436. "mode" => "upd",
  437. "id" => $id
  438. ));
  439. // update the document
  440. //$sql = "UPDATE $tbl_site_content SET introtext='$introtext', content='$content', pagetitle='$pagetitle', longtitle='$longtitle', type='$type', description='$description', alias='$alias', link_attributes='$link_attributes',
  441. // isfolder=$isfolder, richtext=$richtext, published=$published, pub_date=$pub_date, unpub_date=$unpub_date, parent=$parent, template=$template, menuindex='$menuindex',
  442. // searchable=$searchable, cacheable=$cacheable, editedby=" . $modx->getLoginUserID() . ", editedon=" . time() . ", publishedon=$publishedon, publishedby=$publishedby, contentType='$contentType', content_dispo='$contentdispo', donthit='$donthit', menutitle='$menutitle', hidemenu='$hidemenu' WHERE id=$id;";
  443. $sql = "UPDATE $tbl_site_content SET introtext='$introtext', content='$content', pagetitle='$pagetitle', longtitle='$longtitle', type='$type', description='$description', alias='$alias', link_attributes='$link_attributes', isfolder=$isfolder, richtext=$richtext, published=$published, pub_date=$pub_date, unpub_date=$unpub_date, parent=$parent, template=$template, menuindex='$menuindex', searchable=$searchable, cacheable=$cacheable, editedby=" . $modx->getLoginUserID() . ", editedon=" . time() . ", publishedon=$publishedon, publishedby=$publishedby, contentType='$contentType', content_dispo='$contentdispo', donthit='$donthit', menutitle='$menutitle', hidemenu='$hidemenu', alias_visible='$aliasvisible' WHERE id=$id;";
  444. $rs = $modx->db->query($sql);
  445. if (!$rs) {
  446. echo "An error occured while attempting to save the edited document. The generated SQL is: <i> $sql </i>.";
  447. }
  448. // update template variables
  449. $rs = $modx->db->select('id, tmplvarid', $tbl_site_tmplvar_contentvalues, 'contentid='. $id);
  450. $tvIds = array ();
  451. while ($row = $modx->db->getRow($rs)) {
  452. $tvIds[$row['tmplvarid']] = $row['id'];
  453. }
  454. $tvDeletions = array();
  455. $tvChanges = array();
  456. foreach ($tmplvars as $field => $value) {
  457. if (!is_array($value)) {
  458. if (isset($tvIds[$value])) $tvDeletions[] = $tvIds[$value];
  459. } else {
  460. $tvId = $value[0];
  461. $tvVal = $value[1];
  462. if (isset($tvIds[$tvId])) {
  463. $tvChanges[] = array(array('tmplvarid' => $tvId, 'contentid' => $id, 'value' => $modx->db->escape($tvVal)), array('id' => $tvIds[$tvId]));
  464. } else {
  465. $tvAdded[] = array('tmplvarid' => $tvId, 'contentid' => $id, 'value' => $modx->db->escape($tvVal));
  466. }
  467. }
  468. }
  469. if (!empty($tvDeletions)) {
  470. $rs = $modx->db->delete($tbl_site_tmplvar_contentvalues, 'id IN('.implode(',', $tvDeletions).')');
  471. }
  472. if (!empty($tvAdded)) {
  473. foreach ($tvAdded as $tv) {
  474. $rs = $modx->db->insert($tv, $tbl_site_tmplvar_contentvalues);
  475. }
  476. }
  477. if (!empty($tvChanges)) {
  478. foreach ($tvChanges as $tv) {
  479. $rs = $modx->db->update($tv[0], $tbl_site_tmplvar_contentvalues, 'id='.$tv[1]['id']);
  480. }
  481. }
  482. // set document permissions
  483. if ($use_udperms == 1 && is_array($document_groups)) {
  484. $new_groups = array();
  485. // process the new input
  486. foreach ($document_groups as $value_pair) {
  487. list($group, $link_id) = explode(',', $value_pair); // @see manager/actions/mutate_content.dynamic.php @ line 1138 (permissions list)
  488. $new_groups[$group] = $link_id;
  489. }
  490. // grab the current set of permissions on this document the user can access
  491. $isManager = $modx->hasPermission('access_permissions');
  492. $isWeb = $modx->hasPermission('web_access_permissions');
  493. $sql = 'SELECT groups.id, groups.document_group FROM '.$tbl_document_groups.' AS groups '.
  494. 'LEFT JOIN '.$tbl_documentgroup_names.' AS dgn ON dgn.id = groups.document_group '.
  495. 'WHERE ((1='.(int)$isManager.' AND dgn.private_memgroup) '.
  496. 'OR (1='.(int)$isWeb.' AND dgn.private_webgroup))'.
  497. 'AND groups.document = '.$id;
  498. $rs = $modx->db->query($sql);
  499. $old_groups = array();
  500. while ($row = $modx->db->getRow($rs)) $old_groups[$row['document_group']] = $row['id'];
  501. // update the permissions in the database
  502. $insertions = $deletions = array();
  503. foreach ($new_groups as $group => $link_id) {
  504. if (array_key_exists($group, $old_groups)) {
  505. unset($old_groups[$group]);
  506. continue;
  507. } elseif ($link_id == 'new') {
  508. $insertions[] = '('.(int)$group.','.$id.')';
  509. }
  510. }
  511. $saved = true;
  512. if (!empty($insertions)) {
  513. $sql_insert = 'INSERT INTO '.$tbl_document_groups.' (document_group, document) VALUES '.implode(',', $insertions);
  514. $saved = $modx->db->query($sql_insert) ? $saved : false;
  515. }
  516. if (!empty($old_groups)) {
  517. $sql_delete = 'DELETE FROM '.$tbl_document_groups.' WHERE id IN ('.implode(',', $old_groups).')';
  518. $saved = $modx->db->query($sql_delete) ? $saved : false;
  519. }
  520. // necessary to remove all permissions as document is public
  521. if ((isset($_POST['chkalldocs']) && $_POST['chkalldocs'] == 'on')) {
  522. $sql_delete = 'DELETE FROM '.$tbl_document_groups.' WHERE document='.$id;
  523. $saved = $modx->db->query($sql_delete) ? $saved : false;
  524. }
  525. if (!$saved) {
  526. $modx->manager->saveFormValues(27);
  527. echo "An error occured while saving document groups.";
  528. exit;
  529. }
  530. }
  531. // do the parent stuff
  532. if ($parent != 0) {
  533. $fields = array('isfolder' => 1);
  534. $rs = $modx->db->update($fields, $tbl_site_content, 'id='.$_REQUEST['parent']);
  535. if (!$rs) {
  536. echo "An error occured while attempting to change the new parent to a folder.";
  537. }
  538. }
  539. // finished moving the document, now check to see if the old_parent should no longer be a folder
  540. $rs = $modx->db->select('COUNT(id)', $tbl_site_content, 'parent='.$oldparent);
  541. if (!$rs) {
  542. echo "An error occured while attempting to find the old parents' children.";
  543. }
  544. $row = $modx->db->getRow($rs);
  545. $limit = $row['COUNT(id)'];
  546. if ($limit == 0) {
  547. $fields = array('isfolder' => 0);
  548. $rs = $modx->db->update($fields, $tbl_site_content, 'id='.$oldparent);
  549. if (!$rs) {
  550. echo "An error occured while attempting to change the old parent to a regular document.";
  551. }
  552. }
  553. // save META Keywords
  554. saveMETAKeywords($id);
  555. // invoke OnDocFormSave event
  556. $modx->invokeEvent("OnDocFormSave", array (
  557. "mode" => "upd",
  558. "id" => $id
  559. ));
  560. // secure web documents - flag as private
  561. include $base_path . "manager/includes/secure_web_documents.inc.php";
  562. secureWebDocument($id);
  563. // secure manager documents - flag as private
  564. include $base_path . "manager/includes/secure_mgr_documents.inc.php";
  565. secureMgrDocument($id);
  566. if ($syncsite == 1) {
  567. // empty cache
  568. include_once "cache_sync.class.processor.php";
  569. $sync = new synccache();
  570. $sync->setCachepath("../assets/cache/");
  571. $sync->setReport(false);
  572. $sync->emptyCache();
  573. }
  574. if ($_POST['refresh_preview'] == '1')
  575. $header = "Location: ../index.php?id=$id&z=manprev";
  576. else {
  577. if ($_POST['stay'] != '') {
  578. $id = $_REQUEST['id'];
  579. if ($type == "reference") {
  580. // weblink
  581. $a = ($_POST['stay'] == '2') ? "27&id=$id" : "72&pid=$parent";
  582. } else {
  583. // document
  584. $a = ($_POST['stay'] == '2') ? "27&id=$id" : "4&pid=$parent";
  585. }
  586. $header = "Location: index.php?a=" . $a . "&r=1&stay=" . $_POST['stay'];
  587. } else {
  588. $header = "Location: index.php?r=1&id=$id&a=7&dv=1";
  589. }
  590. }
  591. header($header);
  592. break;
  593. default :
  594. header("Location: index.php?a=7");
  595. exit;
  596. }
  597. /**
  598. * Format alias to be URL-safe
  599. *
  600. * @deprecated Use $modx->stripAlias()
  601. * @param string Alias to be formatted
  602. * @return string Safe alias
  603. */
  604. function stripAlias($alias) {
  605. return $GLOBALS['modx']->stripAlias($alias);
  606. }
  607. // -- Save META Keywords --
  608. function saveMETAKeywords($id) {
  609. global $modx, $keywords, $metatags,
  610. $tbl_keyword_xref,
  611. $tbl_site_content,
  612. $tbl_site_content_metatags;
  613. if ($modx->hasPermission('edit_doc_metatags')) {
  614. // keywords - remove old keywords first
  615. $modx->db->delete($tbl_keyword_xref, "content_id=$id");
  616. for ($i = 0; $i < count($keywords); $i++) {
  617. $kwid = $keywords[$i];
  618. $flds = array (
  619. 'content_id' => $id,
  620. 'keyword_id' => $kwid
  621. );
  622. $modx->db->insert($flds, $tbl_keyword_xref);
  623. }
  624. // meta tags - remove old tags first
  625. $modx->db->delete($tbl_site_content_metatags, "content_id=$id");
  626. for ($i = 0; $i < count($metatags); $i++) {
  627. $kwid = $metatags[$i];
  628. $flds = array (
  629. 'content_id' => $id,
  630. 'metatag_id' => $kwid
  631. );
  632. $modx->db->insert($flds, $tbl_site_content_metatags);
  633. }
  634. $flds = array (
  635. 'haskeywords' => (count($keywords) ? 1 : 0),
  636. 'hasmetatags' => (count($metatags) ? 1 : 0)
  637. );
  638. $modx->db->update($flds, $tbl_site_content, "id=$id");
  639. }
  640. }
  641. ?>