PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Zikula-1.2.3/system/Categories/pnuser.php

#
PHP | 254 lines | 206 code | 18 blank | 30 comment | 19 complexity | 9498b6488a3dbabeda968f06aef30570 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Zikula Application Framework
  4. *
  5. * @copyright Zikula Development Team
  6. * @link http://www.zikula.org
  7. * @version $Id: pnuser.php 27396 2009-11-04 01:38:04Z mateo $
  8. * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9. * @author Robert Gasch rgasch@gmail.com
  10. * @package Zikula_Core
  11. */
  12. Loader::loadClass ('HtmlUtil');
  13. Loader::loadClass ('LanguageUtil');
  14. Loader::loadClassFromModule ('Categories', 'category');
  15. /**
  16. * main user function
  17. */
  18. function Categories_user_main()
  19. {
  20. if (!SecurityUtil::checkPermission('Categories::', '::', ACCESS_EDIT)) {
  21. return LogUtil::registerPermissionError();
  22. }
  23. $referer = pnServerGetVar ('HTTP_REFERER');
  24. if (strpos ($referer, 'module=Categories') === false) {
  25. SessionUtil::setVar('categories_referer', $referer);
  26. }
  27. $pnRender = & pnRender::getInstance('Categories', false);
  28. $pnRender->assign ('allowusercatedit', pnModGetVar('Categories', 'allowusercatedit', 0));
  29. return $pnRender->fetch('categories_user_editcategories.htm');
  30. }
  31. /**
  32. * edit category for a simple, non-recursive set of categories
  33. */
  34. function Categories_user_edit ()
  35. {
  36. $docroot = FormUtil::getPassedValue('dr', 0);
  37. $cid = FormUtil::getPassedValue('cid', 0);
  38. $url = pnModURL('Categories', 'user', 'edit', array('dr' => $docroot));
  39. if (!SecurityUtil::checkPermission('Categories::category', "ID::$docroot", ACCESS_EDIT)) {
  40. return LogUtil::registerPermissionError($url);
  41. }
  42. $referer = pnServerGetVar ('HTTP_REFERER');
  43. if (strpos($referer, 'module=Categories') === false) {
  44. SessionUtil::setVar('categories_referer', $referer);
  45. }
  46. $rootCat = array();
  47. $allCats = array();
  48. $editCat = array();
  49. if (!$docroot) {
  50. return LogUtil::registerError(__("Error! The URL contains an invalid 'document root' parameter."), null, $url);
  51. }
  52. if ($docroot == 1) {
  53. return LogUtil::registerError(__("Error! The root directory cannot be modified in 'user' mode"), null, $url);
  54. }
  55. Loader::loadClass('CategoryUtil');
  56. if (is_int((int)$docroot) && $docroot > 0) {
  57. $rootCat = CategoryUtil::getCategoryByID($docroot);
  58. } else {
  59. $rootCat = CategoryUtil::getCategoryByPath($docroot);
  60. if (!$rootCat) {
  61. $rootCat = CategoryUtil::getCategoryByPath($docroot, 'ipath');
  62. }
  63. }
  64. // now check if someone is trying edit another user's categories
  65. $userRoot = pnModGetVar('Categories', 'userrootcat', 0);
  66. if ($userRoot) {
  67. $userRootCat = CategoryUtil::getCategoryByPath($userRoot);
  68. if ($userRootCat) {
  69. $userRootCatIPath = $userRootCat['ipath'];
  70. $rootCatIPath = $rootCat['ipath'];
  71. if (strpos($rootCatIPath, $userRootCatIPath) !== false) {
  72. if (!SecurityUtil::checkPermission('Categories::category', "ID::$docroot", ACCESS_ADMIN)) {
  73. $thisUserRootCategoryName = pnModAPIFunc ('Categories', 'user', 'getusercategoryname');
  74. $thisUserRootCatPath = $userRootCat['path'] . '/' . $thisUserRootCategoryName;
  75. $userRootCatPath = $userRootCat['path'];
  76. $rootCatPath = $rootCat['path'];
  77. if (strpos($rootCatPath, $userRootCatPath) === false) {
  78. //! %s represents the root path (id), passed in the url
  79. return LogUtil::registerError(__f("Error! It looks like you are trying to edit another user's categories. Only site administrators can do that (%s).", $docroot), null, $url);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. if ($cid) {
  86. $editCat = CategoryUtil::getCategoryByID ($cid);
  87. if ($editCat['is_locked']) {
  88. //! %1$s is the id, %2$s is the name
  89. return LogUtil::registerError(__f('Notice: The administrator has locked the category \'%2$s\' (ID \'%$1s\'). You cannot edit or delete it.', array($cid, $editCat['name'])), null, $url);
  90. }
  91. }
  92. if (!$rootCat) {
  93. return LogUtil::registerError(__f("Error! Cannot access root directory (%s).", $docroot), null, $url);
  94. }
  95. if ($editCat && !$editCat['is_leaf']) {
  96. return LogUtil::registerError(__f('Error! The specified category is not a leaf-level category (%s).', $cid), null, $url);
  97. }
  98. if ($editCat && !CategoryUtil::isDirectSubCategory ($rootCat, $editCat)) {
  99. return LogUtil::registerError(__f('Error! The specified category is not a child of the document root (%1$s; %2$s).', array($docroot, $cid)), null, $url);
  100. }
  101. $allCats = CategoryUtil::getSubCategoriesForCategory($rootCat, false, false, false, true, true);
  102. $attributes = isset($editCat['__ATTRIBUTES__']) ? $editCat['__ATTRIBUTES__'] : array();
  103. $languages = ZLanguage::getInstalledLanguages();
  104. $pnRender = & pnRender::getInstance('Categories', false);
  105. $pnRender->assign('rootCat', $rootCat);
  106. $pnRender->assign('category', $editCat);
  107. $pnRender->assign('attributes', $attributes);
  108. $pnRender->assign('allCats', $allCats);
  109. $pnRender->assign('languages', $languages);
  110. $pnRender->assign('userlanguage', ZLanguage::getLanguageCode());
  111. $pnRender->assign('referer', SessionUtil::getVar('categories_referer'));
  112. return $pnRender->fetch('categories_user_edit.htm');
  113. }
  114. /**
  115. * edit categories for the currently logged in user
  116. */
  117. function Categories_user_edituser ()
  118. {
  119. if (!SecurityUtil::checkPermission('Categories::category', '::', ACCESS_EDIT)) {
  120. return LogUtil::registerPermissionError();
  121. }
  122. if (!pnUserLoggedIn()) {
  123. return LogUtil::registerError(__('Error! Editing mode for user-owned categories is only available to users who have logged-in.'));
  124. }
  125. $allowUserEdit = pnModGetVar ('Categories', 'allowusercatedit', 0);
  126. if (!$allowUserEdit) {
  127. return LogUtil::registerError(__('Error! User-owned category editing has not been enabled. This feature can be enabled by the site administrator.'));
  128. }
  129. $userRoot = pnModGetVar('Categories', 'userrootcat', 0);
  130. if (!$userRoot) {
  131. return LogUtil::registerError(__('Error! Could not determine the user root node.'));
  132. }
  133. Loader::loadClass ('CategoryUtil');
  134. $userRootCat = CategoryUtil::getCategoryByPath($userRoot);
  135. if (!$userRoot) {
  136. return LogUtil::registerError(__f('Error! The user root node seems to point towards an invalid category: %s.', $userRoot));
  137. }
  138. if ($userRootCat == 1) {
  139. return LogUtil::registerError(__("Error! The root directory cannot be modified in 'user' mode"));
  140. }
  141. $userCatName = Categories_user_getusercategoryname();
  142. if (!$userCatName) {
  143. return LogUtil::registerError(__('Error! Cannot determine user category root node name.'));
  144. }
  145. $thisUserRootCatPath = $userRoot . '/' . $userCatName;
  146. $thisUserRootCat = CategoryUtil::getCategoryByPath($thisUserRootCatPath);
  147. $dr = null;
  148. if (!$thisUserRootCat) {
  149. $autoCreate = pnModGetVar ('Categories', 'autocreateusercat', 0);
  150. if (!$autoCreate) {
  151. return LogUtil::registerError(__("Error! The user root category node for this user does not exist, and the automatic creation flag (autocreate) has not been set."));
  152. }
  153. require_once ('system/Categories/pninit.php'); // need this for Categories_makeDisplayName() && Categories_makeDisplayDesc()
  154. $cat = array('id' => '',
  155. 'parent_id' => $userRootCat['id'],
  156. 'name' => $userCatName,
  157. 'display_name' => unserialize(Categories_makeDisplayName($userCatName)),
  158. 'display_desc' => unserialize(Categories_makeDisplayDesc()),
  159. 'security_domain' => 'Categories::',
  160. 'path' => $thisUserRootCatPath,
  161. 'status' => 'A');
  162. if (!($class = Loader::loadClassFromModule ('Categories', 'category'))) {
  163. return pn_exit (__f('Error! Unable to load class [%s]', 'category'));
  164. }
  165. $obj = new $class ();
  166. $obj->setData ($cat);
  167. $obj->insert ();
  168. // since the original insert can't construct the ipath (since
  169. // the insert id is not known yet) we update the object here
  170. $obj->update ();
  171. $dr = $obj->getID ();
  172. $autoCreateDefaultUserCat = pnModGetVar ('Categories', 'autocreateuserdefaultcat', 0);
  173. if ($autoCreateDefaultUserCat) {
  174. $userdefaultcatname = pnModGetVar ('Categories', 'userdefaultcatname', __('Default'));
  175. $cat = array('id' => '',
  176. 'parent_id' => $dr,
  177. 'name' => $userdefaultcatname,
  178. 'display_name' => unserialize(Categories_makeDisplayName($userdefaultcatname)),
  179. 'display_desc' => unserialize(Categories_makeDisplayDesc()),
  180. 'security_domain' => 'Categories::',
  181. 'path' => $thisUserRootCatPath . '/' . $userdefaultcatname,
  182. 'status' => 'A');
  183. $obj->setData ($cat);
  184. $obj->insert ();
  185. // since the original insert can't construct the ipath (since
  186. // the insert id is not known yet) we update the object here
  187. $obj->update ();
  188. }
  189. } else {
  190. $dr = $thisUserRootCat['id'];
  191. }
  192. $url = pnModURL ('Categories', 'user', 'edit', array('dr' => $dr));
  193. return pnRedirect($url);
  194. }
  195. /**
  196. * refer the user back to the calling page
  197. */
  198. function Categories_user_referBack()
  199. {
  200. $referer = SessionUtil::getVar ('categories_referer');
  201. SessionUtil::DelVar ('categories_referer');
  202. return pnRedirect ($referer);
  203. }
  204. /**
  205. * return the categories for the currently logged in user, really only used for testing purposes
  206. */
  207. function Categories_user_getusercategories ()
  208. {
  209. return pnModAPIFunc ('Categories', 'user', 'getusercategories');
  210. }
  211. /**
  212. * return the category name for a user, really only used for testing purposes
  213. */
  214. function Categories_user_getusercategoryname ()
  215. {
  216. return pnModAPIFunc ('Categories', 'user', 'getusercategoryname');
  217. }