blast_ui.theme.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * @file
  4. * This file contains functions specifically related to theme-ing the BLAST module
  5. */
  6. /**
  7. * Preprocess function for the show_blast_report.tpl.php
  8. *
  9. * Use this function to prepare variables for use in the template,
  10. * as well as to add css/javascript needed.
  11. *
  12. * @param $vars
  13. * The variables currently available to the template.
  14. */
  15. function blast_ui_preprocess_show_blast_report(&$vars) {
  16. // Add CSS and Javascript files
  17. $path = drupal_get_path('module', 'blast_ui');
  18. drupal_add_css($path . '/theme/css/blast_report.css');
  19. drupal_add_js('https://code.jquery.com/jquery-1.12.4.min.js');
  20. // Get blast job details.
  21. $vars['blast_job'] = get_BLAST_job($vars['job_id']);
  22. // Determine the blast command for display.
  23. $vars['blast_job']->blast_cmd = $vars['blast_job']->program;
  24. foreach($vars['blast_job']->options as $key => $value) {
  25. $vars['blast_job']->blast_cmd .= ' -' . $key. ' ' . $value ;
  26. }
  27. // Determine the URL of the blast form.
  28. $vars['blast_form_url'] = 'blast/nucleotide/nucleotide';
  29. switch($vars['blast_job']->program) {
  30. case 'blastn':
  31. $vars['blast_form_url'] = 'blast/nucleotide/nucleotide';
  32. break;
  33. case 'blastx':
  34. $vars['blast_form_url'] = 'blast/nucleotide/protein';
  35. break;
  36. case 'tblastn':
  37. $vars['blast_form_url'] = 'blast/protein/nucleotide';
  38. break;
  39. case 'blastp':
  40. $vars['blast_form_url'] = 'blast/protein/protein';
  41. break;
  42. }
  43. // Load the XML file.
  44. $vars['xml'] = NULL;
  45. $vars['num_results'] = FALSE;
  46. $vars['too_many_results'] = FALSE;
  47. $full_path_xml = DRUPAL_ROOT . DIRECTORY_SEPARATOR . $vars['blast_job']->files->result->xml;
  48. if (is_readable($full_path_xml)) {
  49. $vars['num_results'] = shell_exec('grep -c "<Hit>" ' . escapeshellarg($full_path_xml));
  50. if ($vars['num_results'] < 500) {
  51. $vars['xml'] = simplexml_load_file($full_path_xml);
  52. }
  53. else {
  54. $vars['too_many_results'] = TRUE;
  55. }
  56. }
  57. }
  58. /**
  59. * Implements hook_theme_registery_alter().
  60. */
  61. function blast_ui_theme_registry_alter(&$theme_registry) {
  62. $theme_registry_copy = $theme_registry;
  63. $module_path = drupal_get_path('module', 'blast_ui') . '/theme';
  64. _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', 'my_custom_theme', $module_path);
  65. $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);
  66. // A list of templates the module will provide templates for
  67. $hooks = array('page');
  68. foreach ($hooks as $hook) {
  69. // Add the key 'theme paths' if it doesn't exist in this theme's registry
  70. if (!isset($theme_registry[$hook]['theme paths'])) {
  71. $theme_registry[$hook]['theme paths'] = array();
  72. }
  73. // Shift this module's directory to the top of the theme path list
  74. if (is_array($theme_registry[$hook]['theme paths'])) {
  75. $first_element = array_shift($theme_registry[$hook]['theme paths']);
  76. if ($first_element) {
  77. array_unshift($theme_registry[$hook]['theme paths'], $first_element, $module_path);
  78. }
  79. else {
  80. array_unshift($theme_registry[$hook]['theme paths'], $module_path);
  81. }
  82. }
  83. }
  84. }
  85. /**
  86. * Makes the tripal job_id unrecognizable.
  87. *
  88. * @param $job_id
  89. * The tripal job_id of the blast you want to make secret.
  90. *
  91. * @return
  92. * A short string representing the job_id.
  93. */
  94. function blast_ui_make_secret($job_id) {
  95. $mapping = blast_ui_secret_mapping();
  96. $secret = str_replace(array_keys($mapping), $mapping, $job_id);
  97. return $secret;
  98. }
  99. /**
  100. * Reveals the true job_id for your secret blast result.
  101. *
  102. * @param $secret
  103. * The job_id previously made secret by blast_ui_make_secret().
  104. *
  105. * @return
  106. * The revealed tripal job_id.
  107. */
  108. function blast_ui_reveal_secret($secret) {
  109. $mapping = blast_ui_secret_mapping(TRUE);
  110. $job_id = str_replace(array_keys($mapping), $mapping, $secret);
  111. // Check that the job_id exists if it is an integer.
  112. if (is_numeric($job_id)) {
  113. $exists = db_query('SELECT job_id FROM {tripal_jobs} WHERE job_id=:id', array(':id' => $job_id))->fetchField();
  114. if (!$exists) {
  115. tripal_report_error(
  116. 'blast_ui',
  117. TRIPAL_ERROR,
  118. 'Unable to decode the blast job_id from :id.',
  119. array(':id' => $secret)
  120. );
  121. }
  122. else {
  123. return $job_id;
  124. }
  125. }
  126. // Last ditch effort: maybe this job was encoded before the upgrade?
  127. else {
  128. $job_id = base64_decode($secret);
  129. if (is_numeric($job_id)) {
  130. $exists = db_query('SELECT job_id FROM {tripal_jobs} WHERE job_id=:id', array(':id' => $job_id))->fetchField();
  131. if (!$exists) {
  132. tripal_report_error(
  133. 'blast_ui',
  134. TRIPAL_ERROR,
  135. 'Unable to decode the blast job_id from :id.',
  136. array(':id' => $secret)
  137. );
  138. }
  139. else {
  140. return $job_id;
  141. }
  142. }
  143. else {
  144. tripal_report_error(
  145. 'blast_ui',
  146. TRIPAL_ERROR,
  147. 'Unable to decode the blast job_id from :id.',
  148. array(':id' => $secret)
  149. );
  150. }
  151. }
  152. return FALSE;
  153. }
  154. /**
  155. * A single location for keeping track of the mapping used in our secrets.
  156. */
  157. function blast_ui_secret_mapping($reveal = FALSE) {
  158. $mapping = array(
  159. 1 => 'P',
  160. 2 => 'sA',
  161. 3 => 'b',
  162. 4 => 'Q',
  163. 5 => 'Hi',
  164. 6 => 'yt',
  165. 7 => 'f',
  166. 8 => 'zE',
  167. 9 => 'Km',
  168. 0 => 'jVo',
  169. );
  170. // Since this is an open-source module with all the code publically available,
  171. // our secret is not very secret... We are ok with this since the liklihood of
  172. // profiting by stealing random blast results is pretty low. That said, if this bothers
  173. // you, feel free to implement the following function in a private module to change
  174. // this mapping to something that cannot easily be looked up on github. ;-).
  175. // NOTE: Ensure that the mapping you come up with is unique to ensure that the
  176. // job_id can be consistently revealed or your users might end up unable to find
  177. // their own blast results...
  178. if (function_exists('private_make_mapping_ultra_secret')) {
  179. $mapping = private_make_mapping_ultra_secret($mapping);
  180. }
  181. if ($reveal) {
  182. return array_flip($mapping);
  183. }
  184. else {
  185. return $mapping;
  186. }
  187. }
  188. /**
  189. * Tests your secret mapping over a set of random integers
  190. * to ensure the job_id can be recovered.
  191. *
  192. * @param $num_iterations
  193. * An integer representing the number of times you wish to test your mapping.
  194. */
  195. function blast_ui_test_secret_mapping($num_iterations = 10000) {
  196. $all_work = TRUE;
  197. for($i = 0; $i <= $num_iterations; $i++) {
  198. $job_id = mt_rand(0,100000);
  199. $secret = blast_ui_make_secret($job_id);
  200. $recovered_job_id = blast_ui_reveal_secret($secret);
  201. if ($job_id != $recovered_job_id) {
  202. drupal_set_message("Unable to recover job_id: $job_id; Secret: $secret.",'error');
  203. $all_work = FALSE;
  204. }
  205. }
  206. if ($all_work) {
  207. drupal_Set_message("Secret Mapping works over $num_iterations iterations with random integers.");
  208. }
  209. }