PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/include/api/forums.php

https://bitbucket.org/webop/webop-forum
PHP | 355 lines | 165 code | 31 blank | 159 comment | 24 complexity | 32a324ad355b13e411ee53a14a3765dd MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // Copyright (C) 2010 Phorum Development Team //
  5. // http://www.phorum.org //
  6. // //
  7. // This program is free software. You can redistribute it and/or modify //
  8. // it under the terms of either the current Phorum License (viewable at //
  9. // phorum.org) or the Phorum License that was distributed with this file //
  10. // //
  11. // This program is distributed in the hope that it will be useful, //
  12. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  14. // //
  15. // You should have received a copy of the Phorum License //
  16. // along with this program. //
  17. // //
  18. ////////////////////////////////////////////////////////////////////////////////
  19. /**
  20. * This script implements the Phorum forum admin API.
  21. *
  22. * This API is used for managing Phorum forums and folders. It can be used to
  23. * retrieve information about the available forums and folders and takes care
  24. * of creating and editing them.
  25. *
  26. * This API combines forums and folders into one API script, because at the
  27. * data level, they are the same kind of entity. Folders are also forums,
  28. * only they act differently based on the "folder_flag" field. Therefore,
  29. * folders are also identified by a forum_id.
  30. *
  31. * @package PhorumAPI
  32. * @subpackage ForumsAPI
  33. * @copyright 2010, Phorum Development Team
  34. * @license Phorum License, http://www.phorum.org/license.txt
  35. */
  36. if (!defined("PHORUM")) return;
  37. /**
  38. * This array describes folder data fields. It is mainly used internally
  39. * for configuring how to handle the fields and for doing checks on them.
  40. */
  41. $GLOBALS['PHORUM']['API']['folder_fields'] = array(
  42. 'forum_id' => 'int',
  43. 'parent_id' => 'int',
  44. 'name' => 'string',
  45. 'description' => 'string',
  46. 'active' => 'bool',
  47. 'forum_path' => 'array',
  48. 'display_order' => 'int',
  49. 'vroot' => 'int',
  50. 'template' => 'string',
  51. 'language' => 'string'
  52. );
  53. /**
  54. * This array describes forum data fields. It is mainly used internally
  55. * for configuring how to handle the fields and for doing checks on them.
  56. */
  57. $GLOBALS['PHORUM']['API']['forum_fields'] = array(
  58. 'forum_id' => 'int',
  59. 'parent_id' => 'int',
  60. 'name' => 'string',
  61. 'description' => 'string',
  62. 'active' => 'bool',
  63. 'forum_path' => 'array',
  64. 'display_order' => 'int',
  65. 'vroot' => 'int',
  66. 'cache_version' => 'int',
  67. // Display settings.
  68. 'display_fixed' => 'bool',
  69. 'inherit_id' => 'int',
  70. 'template' => 'string',
  71. 'language' => 'string',
  72. 'reverse_threading' => 'bool',
  73. 'float_to_top' => 'bool',
  74. 'threaded_list' => 'int',
  75. 'list_length_flat' => 'int',
  76. 'list_length_threaded' => 'int',
  77. 'threaded_read' => 'int',
  78. 'read_length' => 'int',
  79. 'display_ip_address' => 'bool',
  80. // Posting settings.
  81. 'check_duplicate' => 'bool',
  82. // Statistics and statistics settings.
  83. 'message_count' => 'int',
  84. 'thread_count' => 'int',
  85. 'sticky_count' => 'int',
  86. 'last_post_time' => 'int',
  87. 'count_views' => 'bool',
  88. 'count_views_per_thread' => 'bool',
  89. // Permission settings.
  90. 'moderation' => 'int',
  91. 'email_moderators' => 'bool',
  92. 'allow_email_notify' => 'bool',
  93. 'pub_perms' => 'int',
  94. 'reg_perms' => 'int',
  95. // Attachment settings.
  96. 'allow_attachment_types' => 'string',
  97. 'max_attachment_size' => 'int',
  98. 'max_totalattachment_size' => 'int',
  99. 'max_attachments' => 'int',
  100. );
  101. // {{{ Function: phorum_api_forums_get
  102. /**
  103. * Retrieve the data for forums and/or folders in various ways.
  104. *
  105. * @param mixed $forum_ids
  106. * A single forum_id or an array of forum_ids for which to retrieve the
  107. * forum data. If this parameter is NULL, then the $parent_id
  108. * parameter will be checked.
  109. *
  110. * @param mixed $parent_id
  111. * Retrieve the forum data for all forums that have their parent_id set
  112. * to $parent_id. If this parameter is NULL, then the $vroot parameter
  113. * will be checked.
  114. *
  115. * @param mixed $vroot
  116. * Retrieve the forum data for all forums that are in the given $vroot.
  117. * If this parameter is NULL, then the $inherit_id parameter will be
  118. * checked.
  119. *
  120. * @param mixed $inherit_id
  121. * Retrieve the forum data for all forums that inherit their settings
  122. * from the forum with id $inherit_id.
  123. *
  124. * @return mixed
  125. * If the $forum_ids parameter is used and if it contains a single
  126. * forum_id, then a single array containg forum data is returned or
  127. * NULL if the forum was not found.
  128. * For all other cases, an array of forum data arrays is returned, indexed
  129. * by the forum_id and sorted by their display order. If the $forum_ids
  130. * parameter is an array containing non-existent forum_ids, then the
  131. * return array will have no entry available in the returned array.
  132. */
  133. function phorum_api_forums_get($forum_ids = NULL, $parent_id = NULL, $vroot = NULL, $inherit_id = NULL)
  134. {
  135. // Retrieve the forums/folders from the database.
  136. $forums = phorum_db_get_forums($forum_ids, $parent_id, $vroot, $inherit_id);
  137. // Filter and process the returned records.
  138. foreach ($forums as $id => $forum)
  139. {
  140. // Find the fields specification to use for this record.
  141. $fields = $forum['folder_flag']
  142. ? $GLOBALS['PHORUM']['API']['folder_fields']
  143. : $GLOBALS['PHORUM']['API']['forum_fields'];
  144. // Initialize the filtered data array.
  145. $filtered = array('folder_flag' => $forum['folder_flag'] ? 1 : 0);
  146. // Add fields to the filtered data.
  147. foreach ($fields as $fld => $fldtype)
  148. {
  149. switch ($fldtype)
  150. {
  151. case 'int':
  152. $filtered[$fld] = (int)$forum[$fld];
  153. break;
  154. case 'string':
  155. $filtered[$fld] = $forum[$fld];
  156. break;
  157. case 'bool':
  158. $filtered[$fld] = empty($forum[$fld]) ? 0 : 1;
  159. break;
  160. case 'array':
  161. $filtered[$fld] = unserialize($forum[$fld]);
  162. break;
  163. default:
  164. trigger_error(
  165. 'phorum_api_forums_get(): Illegal field type used: ' .
  166. htmlspecialchars($fldtype),
  167. E_USER_ERROR
  168. );
  169. break;
  170. }
  171. }
  172. $forums[$id] = $filtered;
  173. }
  174. if ($forum_ids === NULL || is_array($forum_ids)) {
  175. return $forums;
  176. } else {
  177. return isset($forums[$forum_ids]) ? $forums[$forum_ids] : NULL;
  178. }
  179. }
  180. // }}}
  181. // {{{ Function: phorum_api_forums_change_order()
  182. /**
  183. * Change the displaying order for forums and folders in a certain folder.
  184. *
  185. * @param integer $folder_id
  186. * The forum_id of the folder in which to change the display order.
  187. *
  188. * @param integer $forum_id
  189. * The id of the forum or folder to move.
  190. *
  191. * @param string $movement
  192. * This field determines the type of movement to apply to the forum
  193. * or folder. This can be one of:
  194. * - "up": Move the forum or folder $value positions up
  195. * - "down": Move the forum or folder $value permissions down
  196. * - "pos": Move the forum or folder to position $value
  197. * - "start": Move the forum or folder to the start of the list
  198. * - "end": Move the forum or folder to the end of the list
  199. *
  200. * @param mixed $value
  201. * This field specifies a value for the requested type of movement.
  202. * An integer value is only needed for movements "up", "down" and "pos".
  203. * For other movements, this parameter can be omitted.
  204. */
  205. function phorum_api_forums_change_order($folder_id, $forum_id, $movement, $value = NULL)
  206. {
  207. settype($folder_id, 'int');
  208. settype($forum_id, 'int');
  209. if ($value !== NULL) settype($value, 'int');
  210. // Get the forums for the specified folder.
  211. $forums = phorum_api_forums_by_folder($folder_id);
  212. // Prepare the forum list for easy ordering.
  213. $current_pos = NULL;
  214. $pos = 0;
  215. $forum_ids = array();
  216. foreach ($forums as $forum) {
  217. if ($forum['forum_id'] == $forum_id) $current_pos = $pos;
  218. $forum_ids[$pos++] = $forum['forum_id'];
  219. }
  220. $pos--; // to make this the last index position in the array.
  221. // If the forum_id is not in the folder, then return right away.
  222. if ($current_pos === NULL) return;
  223. switch ($movement)
  224. {
  225. case "up": $new_pos = $current_pos - $value; break;
  226. case "down": $new_pos = $current_pos + $value; break;
  227. case "pos": $new_pos = $value; break;
  228. case "start": $new_pos = 0; break;
  229. case "end": $new_pos = $pos; break;
  230. default:
  231. trigger_error(
  232. "phorum_api_forums_change_order(): " .
  233. "Illegal \$movement parameter \"$movement\" used",
  234. E_USER_ERROR
  235. );
  236. }
  237. // Keep the new position within boundaries.
  238. if ($new_pos < 0) $new_pos = 0;
  239. if ($new_pos > $pos) $new_pos = $pos;
  240. // No order change, then return.
  241. if ($new_pos == $current_pos) return;
  242. // Reorder the forum_ids array to represent the order change.
  243. $new_order = array();
  244. for ($i = 0; $i <= $pos; $i++)
  245. {
  246. if ($i == $current_pos) continue;
  247. if ($i == $new_pos) {
  248. if ($i < $current_pos) {
  249. $new_order[] = $forum_id;
  250. $new_order[] = $forum_ids[$i];
  251. } else {
  252. $new_order[] = $forum_ids[$i];
  253. $new_order[] = $forum_id;
  254. }
  255. } else {
  256. $new_order[] = $forum_ids[$i];
  257. }
  258. }
  259. // Loop through all the forums and update the ones that changed.
  260. // We have to look at them all, because the default value for
  261. // display order is 0 for all forums. So, in an unsorted folder,
  262. // all the display order values are set to 0 until you move one.
  263. foreach ($new_order as $display_order => $forum_id) {
  264. if ($forums[$forum_id]['display_order'] != $display_order) {
  265. phorum_db_update_forum(array(
  266. 'forum_id' => $forum_id,
  267. 'display_order' => $display_order
  268. ));
  269. }
  270. }
  271. }
  272. // }}}
  273. // ------------------------------------------------------------------------
  274. // Alias functions (useful shortcut calls to the main file api functions).
  275. // ------------------------------------------------------------------------
  276. // {{{ Function: phorum_api_forums_by_folder()
  277. /**
  278. * Retrieve data for all direct descendant forums and folders within a folder.
  279. *
  280. * @param integer $folder_id
  281. * The forum_id of the folder for which to retrieve the forums.
  282. *
  283. * @return array
  284. * An array of forums, index by the their forum_id and sorted
  285. * by their display order.
  286. */
  287. function phorum_api_forums_by_folder($folder_id = 0)
  288. {
  289. return phorum_api_forums_get(NULL, $folder_id);
  290. }
  291. // }}}
  292. // {{{ Function: phorum_api_forums_by_vroot()
  293. /**
  294. * Retrieve data for all forums and folders that belong to a certain vroot.
  295. *
  296. * @param integer $vroot_id
  297. * The forum_id of the vroot for which to retrieve the forums.
  298. *
  299. * @return array
  300. * An array of forums, index by the their forum_id and sorted
  301. * by their display order.
  302. */
  303. function phorum_api_forums_by_vroot($vroot_id = 0)
  304. {
  305. return phorum_api_forums_get(NULL, NULL, $vroot_id);
  306. }
  307. // }}}
  308. // {{{ Function: phorum_api_forums_by_inheritance()
  309. /**
  310. * Retrieve data for all forums inheriting their settings from a certain forum.
  311. *
  312. * @param integer $forum_id
  313. * The forum_id for which to check what forums inherit its setting.
  314. *
  315. * @return array
  316. * An array of forums, index by the their forum_id and sorted
  317. * by their display order.
  318. */
  319. function phorum_api_forums_by_inheritance($forum_id = 0)
  320. {
  321. return phorum_api_forums_get(NULL, NULL, NULL, $forum_id);
  322. }
  323. // }}}
  324. ?>