blast_ui.theme.inc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. $full_path_xml = DRUPAL_ROOT . DIRECTORY_SEPARATOR . $vars['blast_job']->files->result->xml;
  46. if (is_readable($full_path_xml)) {
  47. $vars['xml'] = simplexml_load_file($full_path_xml);
  48. }
  49. }
  50. /**
  51. * Implements hook_theme_registery_alter().
  52. */
  53. function blast_ui_theme_registry_alter(&$theme_registry) {
  54. $theme_registry_copy = $theme_registry;
  55. $module_path = drupal_get_path('module', 'blast_ui') . '/theme';
  56. _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', 'my_custom_theme', $module_path);
  57. $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);
  58. // A list of templates the module will provide templates for
  59. $hooks = array('page');
  60. foreach ($hooks as $hook) {
  61. // Add the key 'theme paths' if it doesn't exist in this theme's registry
  62. if (!isset($theme_registry[$hook]['theme paths'])) {
  63. $theme_registry[$hook]['theme paths'] = array();
  64. }
  65. // Shift this module's directory to the top of the theme path list
  66. if (is_array($theme_registry[$hook]['theme paths'])) {
  67. $first_element = array_shift($theme_registry[$hook]['theme paths']);
  68. if ($first_element) {
  69. array_unshift($theme_registry[$hook]['theme paths'], $first_element, $module_path);
  70. }
  71. else {
  72. array_unshift($theme_registry[$hook]['theme paths'], $module_path);
  73. }
  74. }
  75. }
  76. }
  77. /**
  78. * Makes the tripal job_id unrecognizable.
  79. *
  80. * @param $job_id
  81. * The tripal job_id of the blast you want to make secret.
  82. *
  83. * @return
  84. * A short string representing the job_id.
  85. */
  86. function blast_ui_make_secret($job_id) {
  87. $mapping = blast_ui_secret_mapping();
  88. $secret = str_replace(array_keys($mapping), $mapping, $job_id);
  89. return $secret;
  90. }
  91. /**
  92. * Reveals the true job_id for your secret blast result.
  93. *
  94. * @param $secret
  95. * The job_id previously made secret by blast_ui_make_secret().
  96. *
  97. * @return
  98. * The revealed tripal job_id.
  99. */
  100. function blast_ui_reveal_secret($secret) {
  101. $mapping = blast_ui_secret_mapping(TRUE);
  102. $job_id = str_replace(array_keys($mapping), $mapping, $secret);
  103. // Check that the job_id exists if it is an integer.
  104. if (is_numeric($job_id)) {
  105. $exists = db_query('SELECT job_id FROM {tripal_jobs} WHERE job_id=:id', array(':id' => $job_id))->fetchField();
  106. if (!$exists) {
  107. tripal_report_error(
  108. 'blast_ui',
  109. TRIPAL_ERROR,
  110. 'Unable to decode the blast job_id from :id.',
  111. array(':id' => $secret)
  112. );
  113. }
  114. else {
  115. return $job_id;
  116. }
  117. }
  118. // Last ditch effort: maybe this job was encoded before the upgrade?
  119. else {
  120. $job_id = base64_decode($secret);
  121. if (is_numeric($job_id)) {
  122. $exists = db_query('SELECT job_id FROM {tripal_jobs} WHERE job_id=:id', array(':id' => $job_id))->fetchField();
  123. if (!$exists) {
  124. tripal_report_error(
  125. 'blast_ui',
  126. TRIPAL_ERROR,
  127. 'Unable to decode the blast job_id from :id.',
  128. array(':id' => $secret)
  129. );
  130. }
  131. else {
  132. return $job_id;
  133. }
  134. }
  135. else {
  136. tripal_report_error(
  137. 'blast_ui',
  138. TRIPAL_ERROR,
  139. 'Unable to decode the blast job_id from :id.',
  140. array(':id' => $secret)
  141. );
  142. }
  143. }
  144. return FALSE;
  145. }
  146. /**
  147. * A single location for keeping track of the mapping used in our secrets.
  148. */
  149. function blast_ui_secret_mapping($reveal = FALSE) {
  150. $mapping = array(
  151. 1 => 'P',
  152. 2 => 'sA',
  153. 3 => 'b',
  154. 4 => 'Q',
  155. 5 => 'Hi',
  156. 6 => 'yt',
  157. 7 => 'f',
  158. 8 => 'zE',
  159. 9 => 'Km',
  160. 0 => 'jVo',
  161. );
  162. // Since this is an open-source module with all the code publically available,
  163. // our secret is not very secret... We are ok with this since the liklihood of
  164. // profiting by stealing random blast results is pretty low. That said, if this bothers
  165. // you, feel free to implement the following function in a private module to change
  166. // this mapping to something that cannot easily be looked up on github. ;-).
  167. // NOTE: Ensure that the mapping you come up with is unique to ensure that the
  168. // job_id can be consistently revealed or your users might end up unable to find
  169. // their own blast results...
  170. if (function_exists('private_make_mapping_ultra_secret')) {
  171. $mapping = private_make_mapping_ultra_secret($mapping);
  172. }
  173. if ($reveal) {
  174. return array_flip($mapping);
  175. }
  176. else {
  177. return $mapping;
  178. }
  179. }
  180. /**
  181. * Tests your secret mapping over a set of random integers
  182. * to ensure the job_id can be recovered.
  183. *
  184. * @param $num_iterations
  185. * An integer representing the number of times you wish to test your mapping.
  186. */
  187. function blast_ui_test_secret_mapping($num_iterations = 10000) {
  188. $all_work = TRUE;
  189. for($i = 0; $i <= $num_iterations; $i++) {
  190. $job_id = mt_rand(0,100000);
  191. $secret = blast_ui_make_secret($job_id);
  192. $recovered_job_id = blast_ui_reveal_secret($secret);
  193. if ($job_id != $recovered_job_id) {
  194. drupal_set_message("Unable to recover job_id: $job_id; Secret: $secret.",'error');
  195. $all_work = FALSE;
  196. }
  197. }
  198. if ($all_work) {
  199. drupal_Set_message("Secret Mapping works over $num_iterations iterations with random integers.");
  200. }
  201. }