PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/accounts/admin/admin_insts_db.php

https://github.com/azeckoski/az-php-sandbox
PHP | 326 lines | 258 code | 41 blank | 27 comment | 40 complexity | 3af0e73c89e0f8eba784510649fcd36d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. * file: admin_insts.php
  4. * Created on Mar 5, 2006 8:26:54 PM by @author aaronz
  5. *
  6. * Aaron Zeckoski (aaronz@vt.edu) - Virginia Tech (http://www.vt.edu/)
  7. */
  8. ?>
  9. <?php
  10. require_once '../include/tool_vars.php';
  11. $PAGE_NAME = "Admin Institutions";
  12. $ACTIVE_MENU="ACCOUNTS"; //for managing active links on multiple menus
  13. $Message = "";
  14. // connect to database
  15. require $ACCOUNTS_PATH.'sql/mysqlconnect.php';
  16. // check authentication
  17. require $ACCOUNTS_PATH.'include/check_authentic.php';
  18. // login if not autheticated
  19. require $ACCOUNTS_PATH.'include/auth_login_redirect.php';
  20. // Make sure user is authorized
  21. $allowed = 0; // assume user is NOT allowed unless otherwise shown
  22. if (!$User->checkPerm("admin_accounts")) {
  23. $allowed = 0;
  24. $Message = "Only admins with <b>admin_accounts</b> or <b>admin_insts</b> may view this page.<br/>" .
  25. "Try out this one instead: <a href='$TOOL_PATH/'>$TOOL_NAME</a>";
  26. } else {
  27. $allowed = 1;
  28. }
  29. // get the search
  30. $searchtext = "";
  31. if ($_REQUEST["searchtext"]) { $searchtext = $_REQUEST["searchtext"]; }
  32. $sqlsearch = "";
  33. if ($searchtext) {
  34. $sqlsearch = " where (I1.name like '%$searchtext%' or I1.abbr like '%$searchtext%' or " .
  35. "I1.type like '%$searchtext%' or U1.username like '%$searchtext%' or " .
  36. "U1.firstname like '%$searchtext%' or U1.lastname like '%$searchtext%' or " .
  37. "U2.username like '%$searchtext%' or U2.firstname like '%$searchtext%' or " .
  38. "U2.lastname like '%$searchtext%') ";
  39. }
  40. // sorting
  41. $sortorder = "name";
  42. if ($_REQUEST["sortorder"]) { $sortorder = $_REQUEST["sortorder"]; }
  43. $sqlsorting = " order by $sortorder ";
  44. // main SQL to fetch all users
  45. $from_sql = " from institution I1 left join users U1 on U1.pk=I1.rep_pk " .
  46. "left join users U2 on U2.pk=I1.repvote_pk ";
  47. // counting number of items
  48. // **************** NOTE - APPLY THE FILTERS TO THE COUNT AS WELL
  49. $count_sql = "select count(*) " . $from_sql . $sqlsearch;
  50. $result = mysql_query($count_sql) or die('Count query failed: ' . mysql_error());
  51. $row = mysql_fetch_array($result);
  52. $total_items = $row[0];
  53. // pagination control
  54. $num_limit = 25;
  55. if ($_REQUEST["num_limit"]) { $num_limit = $_REQUEST["num_limit"]; }
  56. $total_pages = ceil($total_items / $num_limit);
  57. $page = 1;
  58. $PAGE = $_REQUEST["page"];
  59. if ($PAGE) { $page = $PAGE; }
  60. $PAGING = $_REQUEST["paging"];
  61. if ($PAGING) {
  62. if ($PAGING == 'first') { $page = 1; }
  63. else if ($PAGING == 'prev') { $page--; }
  64. else if ($PAGING == 'next') { $page++; }
  65. else if ($PAGING == 'last') { $page = $total_pages; }
  66. }
  67. if ($page > $total_pages) { $page = $total_pages; }
  68. if ($page <= 0) { $page = 1; }
  69. $limitvalue = $page * $num_limit - ($num_limit);
  70. $mysql_limit = " LIMIT $limitvalue, $num_limit";
  71. $start_item = $limitvalue + 1;
  72. $end_item = $limitvalue + $num_limit;
  73. if ($end_item > $total_items) { $end_item = $total_items; }
  74. // the main insr fetching query
  75. $sql = "select I1.*, U1.username as rep_username, U1.email as rep_email, " .
  76. "U2.username as repvote_username, U2.email as repvote_email " .
  77. $from_sql . $sqlsearch . $sqlsorting . $mysql_limit;
  78. //print "SQL=$users_sql<br/>";
  79. $result = mysql_query($sql) or die('User query failed: ' . mysql_error());
  80. $items_displayed = mysql_num_rows($result);
  81. // Do an LDIF export
  82. if ($_REQUEST["ldif"] && $allowed) {
  83. $date = date("Ymd-Hi",time());
  84. $filename = "institutions-" . $date . ".ldif";
  85. header("Content-type: text/plain; charset=utf-8");
  86. header("Content-disposition: inline; filename=$filename\n\n");
  87. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  88. header("Expires: 0");
  89. // get everything except the "other" inst
  90. $sql = "select * from institution where pk > 1 order by pk";
  91. //print "SQL=$sql<br/>";
  92. $result = mysql_query($sql) or die("Inst ldif query failed ($sql): " . mysql_error());
  93. $items_count = mysql_num_rows($result);
  94. echo "# LDIF export of institutions on $date - includes $items_count items\n";
  95. echo "# Use the following command to insert this export into ldap:\n";
  96. echo "# ldapadd -x -D \"cn=Manager,dc=sakaiproject,dc=org\" -W -f $filename\n";
  97. echo "# Use the following command to modify ldap using this export:\n";
  98. echo "# ldapmodify -x -D \"cn=Manager,dc=sakaiproject,dc=org\" -W -f $filename\n";
  99. echo "\n";
  100. while($itemrow=mysql_fetch_assoc($result)) {
  101. echo "# Institution: $itemrow[name]\n";
  102. echo "dn: iid=$itemrow[pk],ou=institutions,dc=sakaiproject,dc=org\n";
  103. echo "objectClass: sakaiInst\n";
  104. echo "iid: $itemrow[pk]\n";
  105. echo "o: $itemrow[name]\n";
  106. echo "instType: $itemrow[type]\n";
  107. if ($itemrow['city']) { echo "l: $itemrow[city]\n"; }
  108. if ($itemrow['state']) { echo "st: $itemrow[state]\n"; }
  109. if ($itemrow['zipcode']) { echo "postalCode: $itemrow[zipcode]\n"; }
  110. if ($itemrow['country']) { echo "c: $itemrow[country]\n"; }
  111. if ($itemrow['rep_pk']) { echo "repUid: $itemrow[rep_pk]\n"; }
  112. if ($itemrow['repvote_pk']) { echo "voteUid: $itemrow[repvote_pk]\n"; }
  113. echo "\n"; // blank line to separate entries
  114. }
  115. exit();
  116. }
  117. // top header links
  118. $EXTRA_LINKS = "<span style='font-size:9pt;'>" .
  119. "<a href='index.php'>Admin</a>: " .
  120. "<a href='admin_users.php'>Users</a> - " .
  121. "<a href='admin_insts.php'>Institutions</a> - " .
  122. "<a href='admin_perms.php'>Permissions</a> - <a href='admin_roles.php'>Roles</a>" .
  123. " <strong>** WARNING: This tool is deprecated! **</strong>" .
  124. "</span>";
  125. // Do the export as requested by the user
  126. if ($_REQUEST["export"] && $allowed) {
  127. $date = date("Ymd-Hi",time());
  128. $filename = "institutions-" . $date . ".csv";
  129. header("Content-type: text/x-csv");
  130. header("Content-disposition: inline; filename=$filename\n\n");
  131. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  132. header("Expires: 0");
  133. } else {
  134. // display the page normally
  135. ?>
  136. <?php include $ACCOUNTS_PATH.'include/top_header.php'; ?>
  137. <script type="text/javascript">
  138. <!--
  139. function orderBy(newOrder) {
  140. if (document.adminform.sortorder.value == newOrder) {
  141. document.adminform.sortorder.value = newOrder + " desc";
  142. } else {
  143. document.adminform.sortorder.value = newOrder;
  144. }
  145. document.adminform.submit();
  146. return false;
  147. }
  148. // -->
  149. </script>
  150. <?php //INCLUDE THE HEADER
  151. include $ACCOUNTS_PATH.'include/header.php';
  152. ?>
  153. <div id="maincontent">
  154. <?= $Message ?>
  155. <?php
  156. // Put in footer and stop the rest of the page from loading if not allowed -AZ
  157. if (!$allowed) {
  158. include $ACCOUNTS_PATH.'include/footer.php';
  159. exit;
  160. }
  161. ?>
  162. <form name="adminform" method="post" action="<?=$_SERVER['PHP_SELF']; ?>" style="margin:0px;">
  163. <input type="hidden" name="sortorder" value="<?= $sortorder ?>" />
  164. <div class="filterarea">
  165. <table border=0 cellspacing=0 cellpadding=0 width="100%">
  166. <tr>
  167. <td nowrap="y"><b style="font-size:1.1em;">Paging:</b></td>
  168. <td nowrap="y">
  169. <input type="hidden" name="page" value="<?= $page ?>" />
  170. <input class="filter" type="submit" name="paging" value="first" title="Go to the first page" />
  171. <input class="filter" type="submit" name="paging" value="prev" title="Go to the previous page" />
  172. <span class="keytext">Page <?= $page ?> of <?= $total_pages ?></span>
  173. <input class="filter" type="submit" name="paging" value="next" title="Go to the next page" />
  174. <input class="filter" type="submit" name="paging" value="last" title="Go to the last page" />
  175. <span class="keytext">&nbsp;-&nbsp;
  176. Displaying <?= $start_item ?> - <?= $end_item ?> of <?= $total_items ?> items (<?= $items_displayed ?> shown)
  177. &nbsp;-&nbsp;
  178. Max of</span>
  179. <select name="num_limit" title="Choose the max items to view per page">
  180. <option value="<?= $num_limit ?>"><?= $num_limit ?></option>
  181. <option value="10">10</option>
  182. <option value="25">25</option>
  183. <option value="50">50</option>
  184. <option value="100">100</option>
  185. <option value="150">150</option>
  186. <option value="200">200</option>
  187. <option value="300">300</option>
  188. </select>
  189. <span class="keytext">items per page</span>
  190. </td>
  191. <td nowrap="y" align="right">
  192. <input class="filter" type="submit" name="ldif" value="LDIF" title="Export an LDIF (ldap) file of all institutions" />
  193. <input class="filter" type="submit" name="export" value="Export" title="Export results based on current filters" />
  194. <input class="filter" type="text" name="searchtext" value="<?= $searchtext ?>"
  195. size="20" title="Enter search text here" />
  196. <script type="text/javascript">document.adminform.searchtext.focus();</script>
  197. <input class="filter" type="submit" name="search" value="Search" title="Search the requirements" />
  198. </td>
  199. </tr>
  200. </table>
  201. </div>
  202. <table border="0" cellspacing="0" width="100%">
  203. <tr class='tableheader'>
  204. <td><a href="javascript:orderBy('name');">Name</a></td>
  205. <td><a href="javascript:orderBy('abbr');">Abbr</a></td>
  206. <td><a href="javascript:orderBy('type');">Type</a></td>
  207. <td>InstRep</td>
  208. <td>VoteRep</td>
  209. </tr>
  210. <?php } // end export else
  211. $line = 0;
  212. while($itemrow=mysql_fetch_assoc($result)) {
  213. $line++;
  214. if ($_REQUEST["export"]) {
  215. // print out EXPORT format instead of display
  216. if ($line == 1) {
  217. $output = "\"Institutions Export:\",\n";
  218. print join(',', array_keys($itemrow)) . "\n"; // add header line
  219. }
  220. foreach ($itemrow as $name=>$value) {
  221. $value = str_replace("\"", "\"\"", $value); // fix for double quotes
  222. $itemrow[$name] = '"' . $value . '"'; // put quotes around each item
  223. }
  224. print join(',', $itemrow) . "\n";
  225. } else {
  226. // display normally
  227. $rowstyle = "";
  228. if (!$itemrow["rep_pk"]) {
  229. $rowstyle = " style = 'color:red;' ";
  230. }
  231. $linestyle = "oddrow";
  232. if ($line % 2 == 0) {
  233. $linestyle = "evenrow";
  234. } else {
  235. $linestyle = "oddrow";
  236. }
  237. ?>
  238. <tr class="<?= $linestyle ?>" <?= $rowstyle ?> >
  239. <td class="line"><?= $itemrow["name"] ?></td>
  240. <td class="line"><?= $itemrow["abbr"] ?>&nbsp;</td>
  241. <td class="line"><?= $itemrow["type"] ?></td>
  242. <td class="line" align="left">
  243. <?php
  244. if ($itemrow["rep_pk"]) {
  245. $short_name = $itemrow["rep_username"];
  246. if (strlen($itemrow["rep_username"]) > 12) {
  247. $short_name = substr($itemrow["rep_username"],0,9) . "...";
  248. }
  249. echo "<label title='".$itemrow["rep_username"]."'>".$short_name."</label>";
  250. } else {
  251. echo "<i>none</i>";
  252. } ?>
  253. </td>
  254. <td class="line" align="left">
  255. <?php
  256. if ($itemrow["repvote_pk"]) {
  257. $short_name = $itemrow["repvote_username"];
  258. if (strlen($itemrow["repvote_username"]) > 12) {
  259. $short_name = substr($itemrow["repvote_username"],0,9) . "...";
  260. }
  261. echo "<label title='".$itemrow["repvote_username"]."'>".$short_name."</label>";
  262. } else {
  263. echo "<i>none</i>";
  264. } ?>
  265. </td>
  266. </tr>
  267. <?php
  268. } // end display else
  269. } // end while
  270. if ($_REQUEST["export"]) {
  271. print "\n\"Exported on:\",\"" . date($DATE_FORMAT,time()) . "\"\n";
  272. } else { // display only
  273. ?>
  274. </table>
  275. </form>
  276. </div>
  277. <?php include $ACCOUNTS_PATH.'include/footer.php'; // Include the FOOTER ?>
  278. <?php } // end display ?>