manifest.crud.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. /**
  3. * @file
  4. * Manifest module database functions.
  5. */
  6. /**
  7. * Save a manifest.
  8. */
  9. function manifest_save(&$manifest) {
  10. manifest_delete($manifest->name);
  11. drupal_write_record('manifest', $manifest);
  12. }
  13. /**
  14. * Load a manifest.
  15. *
  16. * Loads one manifest.
  17. */
  18. function manifest_load($manifest_name = NULL) {
  19. $manifests = manifest_load_multiple($manifest_name);
  20. if (!empty($manifests)) {
  21. return reset($manifests);
  22. }
  23. return FALSE;
  24. }
  25. /**
  26. * Load manifests.
  27. *
  28. * Loads all manifests.
  29. */
  30. function manifest_load_multiple($manifest_name = NULL) {
  31. $result = manifest_query_execute('manifest', 'load', array('name' => $manifest_name));
  32. $manifests = array();
  33. foreach ($result as $manifest) {
  34. $manifest->settings = unserialize($manifest->settings);
  35. manifest_config_load($manifest);
  36. $manifests[$manifest->name] = $manifest;
  37. }
  38. if (!empty($manifests)) {
  39. return $manifests;
  40. }
  41. return FALSE;
  42. }
  43. /**
  44. * Delete a manifest.
  45. */
  46. function manifest_delete($manifest_name) {
  47. manifest_account_delete(NULL, $manifest_name);
  48. manifest_role_delete(NULL, $manifest_name);
  49. manifest_ip_delete(NULL, NULL, $manifest_name);
  50. manifest_config_delete($manifest_name);
  51. return manifest_query_execute('manifest', 'delete', array('name' => $manifest_name));
  52. }
  53. /**
  54. * Save a manifest config entry.
  55. */
  56. function manifest_config_save($manifest_name, $field, $value) {
  57. if (!is_array($value)) {
  58. $value = array($value);
  59. }
  60. $entry = (object)array();
  61. for ($i = 0; $i < count($value); $i++) {
  62. $entry->manifest = $manifest_name;
  63. $entry->field = $field;
  64. $entry->delta = $i;
  65. $entry->value = current($value);
  66. next($value);
  67. }
  68. drupal_write_record('manifest_config', $entry);
  69. }
  70. /**
  71. * Load a manifest config entry into a manifest.
  72. */
  73. function manifest_config_load(&$manifest) {
  74. $result = manifest_query_execute('manifest_config', 'load', array('manifest' => $manifest->name));
  75. $manifest->config = array();
  76. foreach ($result as $row) {
  77. $manifest->config[$row->field][$row->delta] = $row->value;
  78. }
  79. }
  80. /**
  81. * Delete a manifest.
  82. */
  83. function manifest_config_delete($manifest_name) {
  84. return manifest_query_execute('manifest_config', 'delete', array('manifest' => $manifest_name));
  85. }
  86. /**
  87. * Save a manifest user entry.
  88. */
  89. function manifest_account_save($uid, $manifest_name) {
  90. $existing = manifest_account_load($uid, $manifest_name);
  91. if (!$existing) {
  92. $entry = (object)array(
  93. 'uid' => $uid,
  94. 'manifest' => $manifest_name,
  95. 'created' => REQUEST_TIME,
  96. );
  97. drupal_write_record('manifest_user', $entry);
  98. }
  99. }
  100. /**
  101. * Load a manifest user entry.
  102. */
  103. function manifest_account_load($uid = NULL, $manifest_name = NULL) {
  104. $result = manifest_query_execute('manifest_user', 'load', array('uid' => $uid, 'manifest' => $manifest_name));
  105. $entries = array();
  106. foreach ($result as $row) {
  107. $entries[] = $row;
  108. }
  109. if (!empty($entries)) {
  110. return $entries;
  111. }
  112. return FALSE;
  113. }
  114. /**
  115. * Delete a manifest user entry.
  116. */
  117. function manifest_account_delete($uid = NULL, $manifest_name = NULL) {
  118. return manifest_query_execute('manifest_user', 'delete', array('uid' => $uid, 'manifest' => $manifest_name));
  119. }
  120. /**
  121. * Save a manifest role entry.
  122. */
  123. function manifest_role_save($rid, $manifest_name) {
  124. $existing = manifest_role_load($rid, $manifest_name);
  125. if (!$existing) {
  126. $entry = (object)array(
  127. 'rid' => $rid,
  128. 'manifest' => $manifest_name,
  129. 'created' => REQUEST_TIME,
  130. );
  131. drupal_write_record('manifest_role', $entry);
  132. }
  133. }
  134. /**
  135. * Load a manifest role entry.
  136. */
  137. function manifest_role_load($rid = NULL, $manifest_name = NULL) {
  138. $result = manifest_query_execute('manifest_role', 'load', array('rid' => $rid, 'manifest' => $manifest_name));
  139. $entries = array();
  140. foreach ($result as $row) {
  141. $entries[] = $row;
  142. }
  143. if (!empty($entries)) {
  144. return $entries;
  145. }
  146. return FALSE;
  147. }
  148. /**
  149. * Delete a manifest role entry.
  150. */
  151. function manifest_role_delete($rid = NULL, $manifest_name = NULL) {
  152. return manifest_query_execute('manifest_role', 'delete', array('rid' => $rid, 'manifest' => $manifest_name));
  153. }
  154. /**
  155. * Save a manifest ip entry.
  156. */
  157. function manifest_ip_save($ip1, $ip2, $manifest_name) {
  158. $existing = manifest_ip_load($ip1, $ip2, $manifest_name);
  159. if (!$existing) {
  160. $entry = (object)array(
  161. 'ip1' => ip2long($ip1),
  162. 'ip2' => $ip2 ? ip2long($ip2) : ip2long($ip1),
  163. 'manifest' => $manifest_name,
  164. 'created' => REQUEST_TIME,
  165. );
  166. drupal_write_record('manifest_ip', $entry);
  167. }
  168. }
  169. /**
  170. * Load a manifest ip entry.
  171. */
  172. function manifest_ip_load($ip1 = NULL, $ip2 = NULL, $manifest_name = NULL) {
  173. $result = manifest_query_execute('manifest_ip', 'load', array('ip1' => $ip1, 'ip2' => $ip2, 'manifest' => $manifest_name));
  174. $entries = array();
  175. foreach ($result as $row) {
  176. $row->ip1 = long2ip($row->ip1);
  177. $row->ip2 = long2ip($row->ip2);
  178. $entries[] = $row;
  179. }
  180. if (!empty($entries)) {
  181. return $entries;
  182. }
  183. return FALSE;
  184. }
  185. /**
  186. * Load a manifest ip entry.
  187. */
  188. function manifest_ip_match($ip, $manifest_name = NULL) {
  189. $wheres[] = ip2long($ip) . ' >= ip1';
  190. $wheres[] = ip2long($ip) . ' <= ip2';
  191. $result = manifest_query_execute('manifest_ip', 'load', array('manifest' => $manifest_name), array(), $wheres);
  192. $entries = array();
  193. foreach ($result as $row) {
  194. $entries[] = $row;
  195. }
  196. if (!empty($entries)) {
  197. return $entries;
  198. }
  199. return FALSE;
  200. }
  201. /**
  202. * Delete a manifest ip entry.
  203. */
  204. function manifest_ip_delete($ip1 = NULL, $ip2 = NULL, $manifest_name = NULL) {
  205. return manifest_query_execute('manifest_ip', 'delete', array('ip1' => ip2long($ip1), 'ip2' => ip2long($ip2), 'manifest' => $manifest_name));
  206. }
  207. /**
  208. * Assemble SQL queries.
  209. */
  210. function manifest_query_execute($table, $op = 'load', $conditions = array(), $select = array(), $wheres = array(), $args = array()) {
  211. $placeholder_count = 0;
  212. if ($op == 'load') {
  213. $sql = 'SELECT ';
  214. if ($select) {
  215. $sql .= implode(', ', $select);
  216. }
  217. else {
  218. $sql .= '*';
  219. }
  220. }
  221. elseif ($op == 'delete') {
  222. $sql = 'DELETE';
  223. }
  224. $sql .= ' FROM {' . $table . '} ';
  225. $schema = drupal_get_schema($table);
  226. foreach ($conditions as $field => $values) {
  227. if (!empty($values)) {
  228. if (!is_array($values)) {
  229. $values = array($values);
  230. }
  231. $type = $schema['fields'][$field]['type'];
  232. $placeholder = ':value_' . $placeholder_count++;
  233. $wheres[] = $field . ' IN ' . $placeholder;
  234. $args[$placeholder] = $values;
  235. }
  236. }
  237. if (!empty($wheres)) {
  238. $sql .= 'WHERE ';
  239. $sql .= implode(' AND ', $wheres);
  240. }
  241. return db_query($sql, $args);
  242. }
  243. /**
  244. * Get a list of manifests the user is in, matching the settings.
  245. *
  246. * @param $config
  247. * An array of key values that must be true of the $manifest->config.
  248. * @param $account
  249. * An optional user account object. If not supplied will use the current
  250. * user.
  251. * @return
  252. * An array of loaded manifests.
  253. */
  254. function manifest($config = NULL, $account = NULL) {
  255. if (is_null($account)) {
  256. global $user;
  257. $account = $user;
  258. $check_ip = TRUE;
  259. }
  260. $sql = 'SELECT DISTINCT(m.name) AS manifest FROM {manifest} m';
  261. $args = array();
  262. $wheres = array();
  263. $sql .= ' LEFT JOIN {manifest_user} mu ON mu.manifest = m.name';
  264. $sql .= ' LEFT JOIN {manifest_role} mr ON mr.manifest = m.name';
  265. if ($check_ip) {
  266. $sql .= ' LEFT JOIN {manifest_ip} mi ON mi.manifest = m.name';
  267. }
  268. if (!empty($config)) {
  269. $placeholder_count = 0;
  270. foreach ($config as $key => $value) {
  271. $placeholder = $placeholder_count++;
  272. $sql .= ' INNER JOIN {manifest_config} mc_' . $key . ' ON mc_' . $key . '.manifest = m.name AND mc_' . $key . '.field = :key_' . $placeholder . ' AND mc_' . $key . '.value = :val_' . $placeholder;
  273. $args[':key_' . $placeholder] = $key;
  274. $args[':val_' . $placeholder] = $value;
  275. }
  276. }
  277. $wheres[] = 'mu.uid = :uid';
  278. $args[':uid'] = $account->uid;
  279. $roles = array_keys($account->roles);
  280. $wheres[] = 'mr.rid IN :rids';
  281. $args[':rids'] = $roles;
  282. if ($check_ip) {
  283. $wheres[] = '(:ip >= mi.ip1 AND :ip <= mi.ip2)';
  284. $args[':ip'] = ip2long($ip);
  285. }
  286. $sql .= ' WHERE ' . implode(' OR ', $wheres);
  287. $result = db_query($sql, $args);
  288. $manifests = array();
  289. foreach ($result as $row) {
  290. $manifests[] = $row->manifest;
  291. }
  292. if (!empty($manifests)) {
  293. return $manifests;
  294. }
  295. return FALSE;
  296. }
  297. /**
  298. * Get a list of users for the supplied manifests, matching the settings.
  299. *
  300. * @param $config
  301. * An array of key values that must be true of the $manifest->config.
  302. * @param $manifests
  303. * Which manifests to look in.
  304. * @return
  305. * An array of uids.
  306. */
  307. function manifest_users($config = NULL, $manifests = NULL) {
  308. $sql = 'SELECT DISTINCT(u.uid) AS uid FROM {users} u';
  309. $args = array();
  310. $sql .= ' LEFT JOIN {manifest_user} mu ON mu.uid = u.uid';
  311. $sql .= ' LEFT JOIN {users_roles} ur ON u.uid = ur.uid';
  312. $sql .= ' LEFT JOIN {manifest_role} mr ON ur.rid = mr.rid';
  313. if (!empty($config)) {
  314. $placeholder_count = 0;
  315. foreach ($config as $key => $value) {
  316. $placholder = $placeholder_count++;
  317. $sql .= ' INNER JOIN {manifest_config} mc_' . $key . ' ON mu.manifest = mc_' . $key . '.manifest AND mc_' . $key . '.field = :key_' . $placeholder . ' AND mc_' . $key . '.value = :val_' . $placeholder;
  318. $args[':key_' . $placeholder] = $key;
  319. $args[':val_' . $placeholder] = $value;
  320. }
  321. }
  322. if (!empty($manifests)) {
  323. $wheres[] = 'mu.manifest IN :manifests';
  324. $args[':manifests'] = $manifests;
  325. }
  326. $wheres[] = '(mu.uid IS NOT NULL OR mr.rid IS NOT NULL)';
  327. if (!empty($wheres)) {
  328. $sql .= ' WHERE ' . implode(' AND ', $wheres);
  329. }
  330. $result = db_query($sql, $args);
  331. $users = array();
  332. foreach ($result as $row) {
  333. $users[$row->uid] = $row->uid;
  334. }
  335. if (!empty($users)) {
  336. return $users;
  337. }
  338. return FALSE;
  339. }