blast_ui.theme.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // CViTjs
  28. $vars['show_cvit_diagram'] = FALSE;
  29. if (variable_get('blast_ui_cvitjs_enabled', false)
  30. && isset($vars['blast_job']->blastdb->cvitjs_enabled)
  31. && $vars['blast_job']->blastdb->cvitjs_enabled == '1') {
  32. // Set a clean var so we don't have to do this long check again ;-).
  33. $vars['show_cvit_diagram'] = TRUE;
  34. // Add the CSS/JS.
  35. $cvitjs_lib_path = libraries_get_path('cvitjs') . DIRECTORY_SEPARATOR;
  36. drupal_add_css($cvitjs_lib_path . 'js/lib/bootstrap_embed/css/bootstrap.min.css',array('preprocess'=>FALSE));
  37. drupal_add_css($cvitjs_lib_path . 'js/lib/hopscotch/css/hopscotch.min.css',array('preprocess'=>FALSE));
  38. drupal_add_css($cvitjs_lib_path . 'css/cvit.css',array('preprocess'=> FALSE));
  39. drupal_add_js($cvitjs_lib_path . 'js/lib/require/require.js',array('group'=>'JS_LIBRARY','type'=>'file'));
  40. drupal_add_js($cvitjs_lib_path . 'js/lib/require/blast_ui-config.js',array('group'=>'JS_THEME'));
  41. // Add the JS settings.
  42. global $base_url;
  43. drupal_add_js(array('blast_ui'=> array(
  44. 'dataset' => $vars['blast_job']->blastdb->db_name,
  45. 'gff' => $base_url . '/' . $vars['blast_job']->files->result->gff
  46. )),
  47. 'setting'
  48. );
  49. }
  50. // Determine the URL of the blast form.
  51. $vars['blast_form_url'] = 'blast/nucleotide/nucleotide';
  52. switch($vars['blast_job']->program) {
  53. case 'blastn':
  54. $vars['blast_form_url'] = 'blast/nucleotide/nucleotide';
  55. break;
  56. case 'blastx':
  57. $vars['blast_form_url'] = 'blast/nucleotide/protein';
  58. break;
  59. case 'tblastn':
  60. $vars['blast_form_url'] = 'blast/protein/nucleotide';
  61. break;
  62. case 'blastp':
  63. $vars['blast_form_url'] = 'blast/protein/protein';
  64. break;
  65. }
  66. // Load the XML file.
  67. $vars['xml'] = NULL;
  68. $vars['num_results'] = FALSE;
  69. $vars['too_many_results'] = FALSE;
  70. $full_path_xml = DRUPAL_ROOT . DIRECTORY_SEPARATOR . $vars['blast_job']->files->result->xml;
  71. if (is_readable($full_path_xml)) {
  72. $vars['num_results'] = shell_exec('grep -c "<Hit>" ' . escapeshellarg($full_path_xml));
  73. $max_results = intval(variable_get('blast_ui_max_results_displayed', 500));
  74. if ($vars['num_results'] < $max_results) {
  75. $vars['xml'] = simplexml_load_file($full_path_xml);
  76. }
  77. else {
  78. $vars['too_many_results'] = TRUE;
  79. }
  80. }
  81. }
  82. /**
  83. * Implements hook_theme_registery_alter().
  84. */
  85. function blast_ui_theme_registry_alter(&$theme_registry) {
  86. $theme_registry_copy = $theme_registry;
  87. $module_path = drupal_get_path('module', 'blast_ui') . '/theme';
  88. _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', 'my_custom_theme', $module_path);
  89. $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);
  90. // A list of templates the module will provide templates for
  91. $hooks = array('page');
  92. foreach ($hooks as $hook) {
  93. // Add the key 'theme paths' if it doesn't exist in this theme's registry
  94. if (!isset($theme_registry[$hook]['theme paths'])) {
  95. $theme_registry[$hook]['theme paths'] = array();
  96. }
  97. // Shift this module's directory to the top of the theme path list
  98. if (is_array($theme_registry[$hook]['theme paths'])) {
  99. $first_element = array_shift($theme_registry[$hook]['theme paths']);
  100. if ($first_element) {
  101. array_unshift($theme_registry[$hook]['theme paths'], $first_element, $module_path);
  102. }
  103. else {
  104. array_unshift($theme_registry[$hook]['theme paths'], $module_path);
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Makes the tripal job_id unrecognizable.
  111. *
  112. * @param $job_id
  113. * The tripal job_id of the blast you want to make secret.
  114. *
  115. * @return
  116. * A short string representing the job_id.
  117. */
  118. function blast_ui_make_secret($job_id) {
  119. $mapping = blast_ui_secret_mapping();
  120. $secret = str_replace(array_keys($mapping), $mapping, $job_id);
  121. return $secret;
  122. }
  123. /**
  124. * Reveals the true job_id for your secret blast result.
  125. *
  126. * @param $secret
  127. * The job_id previously made secret by blast_ui_make_secret().
  128. *
  129. * @return
  130. * The revealed tripal job_id.
  131. */
  132. function blast_ui_reveal_secret($secret) {
  133. $mapping = blast_ui_secret_mapping(TRUE);
  134. $job_id = str_replace(array_keys($mapping), $mapping, $secret);
  135. // Check that the job_id exists if it is an integer.
  136. if (is_numeric($job_id)) {
  137. $exists = db_query('SELECT job_id FROM {tripal_jobs} WHERE job_id=:id',
  138. array(':id' => $job_id))->fetchField();
  139. if (!$exists) {
  140. tripal_report_error(
  141. 'blast_ui',
  142. TRIPAL_ERROR,
  143. 'Unable to decode the blast job_id from :id.',
  144. array(':id' => $secret)
  145. );
  146. }
  147. else {
  148. return $job_id;
  149. }
  150. }
  151. // Last ditch effort: maybe this job was encoded before the upgrade?
  152. else {
  153. $job_id = base64_decode($secret);
  154. if (is_numeric($job_id)) {
  155. $exists = db_query('SELECT job_id FROM {tripal_jobs} WHERE job_id=:id',
  156. array(':id' => $job_id))->fetchField();
  157. if (!$exists) {
  158. tripal_report_error(
  159. 'blast_ui',
  160. TRIPAL_ERROR,
  161. 'Unable to decode the blast job_id from :id.',
  162. array(':id' => $secret)
  163. );
  164. }
  165. else {
  166. return $job_id;
  167. }
  168. }
  169. else {
  170. tripal_report_error(
  171. 'blast_ui',
  172. TRIPAL_ERROR,
  173. 'Unable to decode the blast job_id from :id.',
  174. array(':id' => $secret)
  175. );
  176. }
  177. }
  178. return FALSE;
  179. }
  180. /**
  181. * A single location for keeping track of the mapping used in our secrets.
  182. */
  183. function blast_ui_secret_mapping($reveal = FALSE) {
  184. $mapping = array(
  185. 1 => 'P',
  186. 2 => 'sA',
  187. 3 => 'b',
  188. 4 => 'Q',
  189. 5 => 'Hi',
  190. 6 => 'yt',
  191. 7 => 'f',
  192. 8 => 'zE',
  193. 9 => 'Km',
  194. 0 => 'jVo',
  195. );
  196. // Since this is an open-source module with all the code publically available,
  197. // our secret is not very secret... We are ok with this since the liklihood of
  198. // profiting by stealing random blast results is pretty low. That said, if this bothers
  199. // you, feel free to implement the following function in a private module to change
  200. // this mapping to something that cannot easily be looked up on github. ;-).
  201. // NOTE: Ensure that the mapping you come up with is unique to ensure that the
  202. // job_id can be consistently revealed or your users might end up unable to find
  203. // their own blast results...
  204. if (function_exists('private_make_mapping_ultra_secret')) {
  205. $mapping = private_make_mapping_ultra_secret($mapping);
  206. }
  207. if ($reveal) {
  208. return array_flip($mapping);
  209. }
  210. else {
  211. return $mapping;
  212. }
  213. }
  214. /**
  215. * Tests your secret mapping over a set of random integers
  216. * to ensure the job_id can be recovered.
  217. *
  218. * @param $num_iterations
  219. * An integer representing the number of times you wish to test your mapping.
  220. */
  221. function blast_ui_test_secret_mapping($num_iterations = 10000) {
  222. $all_work = TRUE;
  223. for($i = 0; $i <= $num_iterations; $i++) {
  224. $job_id = mt_rand(0,100000);
  225. $secret = blast_ui_make_secret($job_id);
  226. $recovered_job_id = blast_ui_reveal_secret($secret);
  227. if ($job_id != $recovered_job_id) {
  228. drupal_set_message("Unable to recover job_id: $job_id; Secret: $secret.",'error');
  229. $all_work = FALSE;
  230. }
  231. }
  232. if ($all_work) {
  233. drupal_Set_message("Secret Mapping works over $num_iterations iterations with random integers.");
  234. }
  235. }