blast_ui.theme.inc 6.9 KB

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