blast_ui.theme.inc 7.2 KB

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