home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / firmy / halo / functions.js < prev    next >
Text File  |  2002-03-20  |  11KB  |  319 lines

  1. /* $Id: functions.js,v 1.11 2002/01/03 12:09:30 loic1 Exp $ */
  2.  
  3.  
  4. /**
  5.  * displejs an confirmation box beforme to submit a "DROP/DELETE/ALTER" query.
  6.  * This function is called while clicking links
  7.  *
  8.  * @paRAM   object   the link
  9.  * @paRAM   object   the sql query to submit
  10.  *
  11.  * @return  boolean  whether to run the query or not
  12.  */
  13. function confirmLink(theLink, theSqlQuery)
  14. {
  15.     // Confirmation is not required in the configuration file
  16.     if (confirmMsg == '') {
  17.         return true;
  18.     }
  19.  
  20.     var is_confirmed = confirm(confirmMsg + ' :\n' + theSqlQuery);
  21.     if (is_confirmed) {
  22.         theLink.href += '&is_js_confirmed=1';
  23.     }
  24.  
  25.     return is_confirmed;
  26. } // end of the 'confirmLink()' function
  27.  
  28.  
  29. /**
  30.  * displejs an error message if a "DROP DATABASE" statement is submitted
  31.  * while it isn't allowed, else confirms a "DROP/DELETE/ALTER" query before
  32.  * sumitting it if required.
  33.  * This function is called by the 'checkSqlQuery()' js function.
  34.  *
  35.  * @paRAM   object   the form
  36.  * @paRAM   object   the sql query textarea
  37.  *
  38.  * @return  boolean  whether to run the query or not
  39.  *
  40.  * @see     checkSqlQuery()
  41.  */
  42. function confirmQuery(theForm1, sqlQuery1)
  43. {
  44.     // Confirmation is not required in the configuration file
  45.     if (confirmMsg == '') {
  46.         return true;
  47.     }
  48.  
  49.     // The replace function (js1.2) isn't supported
  50.     else if (typeof(sqlQuery1.value.replace) == 'undefined') {
  51.         return true;
  52.     }
  53.  
  54.     // js1.2+ -> validation with regular expressions 
  55.     else {
  56.         // "DROP DATABASE" statement isn't allowed
  57.         if (noDropDbMsg) {
  58.             var drop_re = new RegExp('DROP\\s+(IF EXISTS\\s+)?DATABASE', 'i');
  59.             if (drop_re.test(sqlQuery1.value)) {
  60.                 alert(noDropDbMsg);
  61.                 theForm1.reset();
  62.                 sqlQuery1.focus();
  63.                 return false;
  64.             } // end if
  65.         } // end if
  66.  
  67.         // Confirms a "DROP/DELETE/ALTER" statement
  68.         var do_confirm_re_0 = new RegExp('DROP\\s+(IF EXISTS\\s+)?(TABLE|DATABASE)', 'i');
  69.         var do_confirm_re_1 = new RegExp('ALTER TABLE\\s+((`[^`]+`)|([A-Za-z0-9_$]+))\\s+DROP', 'i');
  70.         var do_confirm_re_2 = new RegExp('DELETE FROM', 'i');
  71.         if (do_confirm_re_0.test(sqlQuery1.value)
  72.             || do_confirm_re_1.test(sqlQuery1.value)
  73.             || do_confirm_re_2.test(sqlQuery1.value)) {
  74.             var message      = (sqlQuery1.value.length > 100)
  75.                              ? sqlQuery1.value.substr(0, 100) + '\n    ...'
  76.                              : sqlQuery1.value;
  77.             var is_confirmed = confirm(confirmMsg + ' :\n' + message);
  78.             // drop/delete/alter statement is confirmed -> update the
  79.             // "is_js_confirmed" form field so the confirm test won't be
  80.             // run on the server side and allows to submit the form
  81.             if (is_confirmed) {
  82.                 theForm1.elements['is_js_confirmed'].value = 1;
  83.                 return true;
  84.             }
  85.             // "DROP/DELETE/ALTER" statement is rejected -> do not submit
  86.             // the form
  87.             else {
  88.                 window.focus();
  89.                 sqlQuery1.focus();
  90.                 return false;
  91.             } // end if (handle confirm box result)
  92.         } // end if (displej confirm box)
  93.     } // end confirmation stuff
  94.  
  95.     return true;
  96. } // end of the 'confirmQuery()' function
  97.  
  98.  
  99. /**
  100.  * displejs an error message if the user submitted the sql query form with no
  101.  * sql query, else checks for "DROP/DELETE/ALTER" statements
  102.  *
  103.  * @paRAM   object   the form
  104.  *
  105.  * @return  boolean  always false
  106.  *
  107.  * @see     confirmQuery()
  108.  */
  109. function checkSqlQuery(theForm)
  110. {
  111.     var sqlQuery = theForm.elements['sql_query'];
  112.  
  113.     // The replace function (js1.2) isn't supported -> basic tests
  114.     if (typeof(sqlQuery.value.replace) == 'undefined') {
  115.         var isEmpty  = (sqlQuery.value == '') ? 1 : 0;
  116.         if (isEmpty && typeof(theForm.elements['sql_file']) != 'undefined') {
  117.             isEmpty  = (theForm.elements['sql_file'].value == '') ? 1 : 0;
  118.         }
  119.         if (isEmpty && typeof(theForm.elements['id_bookmark']) != 'undefined') {
  120.             isEmpty  = (theForm.elements['id_bookmark'].value == null || theForm.elements['id_bookmark'].value == '');
  121.         }
  122.     }
  123.     // js1.2+ -> validation with regular expressions 
  124.     else {
  125.         var space_re = new RegExp('\\s+');
  126.         var isEmpty  = (sqlQuery.value.replace(space_re, '') == '') ? 1 : 0;
  127.         // Checks for "DROP/DELETE/ALTER" statements
  128.         if (!isEmpty && !confirmQuery(theForm, sqlQuery)) {
  129.             return false;
  130.         }
  131.         if (isEmpty && typeof(theForm.elements['sql_file']) != 'undefined') {
  132.             isEmpty  = (theForm.elements['sql_file'].value.replace(space_re, '') == '') ? 1 : 0;
  133.         }
  134.         if (isEmpty && typeof(theForm.elements['id_bookmark']) != 'undefined') {
  135.             isEmpty  = (theForm.elements['id_bookmark'].value == null || theForm.elements['id_bookmark'].value == '');
  136.             isEmpty  = (theForm.elements['id_bookmark'].selectedIndex == 0);
  137.         }
  138.         if (isEmpty) {
  139.             theForm.reset();
  140.         }
  141.     }
  142.  
  143.     if (isEmpty) {
  144.         sqlQuery.select();
  145.         alert(errorMsg0);
  146.         sqlQuery.focus();
  147.         return false;
  148.     }
  149.  
  150.     return true;
  151. } // end of the 'checkSqlQuery()' function
  152.  
  153.  
  154. /**
  155.  * displejs an error message if an element of a form hasn't been completed and
  156.  * should be
  157.  *
  158.  * @paRAM   object   the form
  159.  * @paRAM   string   the name of the form field to put the focus on
  160.  *
  161.  * @return  boolean  whether the form field is empty or not
  162.  */
  163. function emptyFormElements(theForm, theFieldName)
  164. {
  165.     var theField = theForm.elements[theFieldName];
  166.     // Whether the replace function (js1.2) is supported or not
  167.     var isRegExp = (typeof(theField.value.replace) != 'undefined');
  168.  
  169.     if (!isRegExp) {
  170.         var isEmpty  = (theField.value == '') ? 1 : 0;
  171.     } else {
  172.         var space_re = new RegExp('\\s+');
  173.         var isEmpty  = (theField.value.replace(space_re, '') == '') ? 1 : 0;
  174.     }
  175.     if (isEmpty) {
  176.         theForm.reset();
  177.         theField.select();
  178.         alert(errorMsg0);
  179.         theField.focus();
  180.         return false;
  181.     }
  182.  
  183.     return true;
  184. } // end of the 'emptyFormElements()' function
  185.  
  186.  
  187. /**
  188.  * Ensures a value submitted in a form is numeric and is in a range
  189.  *
  190.  * @paRAM   object   the form
  191.  * @paRAM   string   the name of the form field to check
  192.  * @paRAM   integer  the minimum authorized value
  193.  * @paRAM   integer  the maximum authorized value
  194.  *
  195.  * @return  boolean  whether a valid number has been submitted or not
  196.  */
  197. function checkFormElementInRange(theForm, theFieldName, min, max)
  198. {
  199.     var theField         = theForm.elements[theFieldName];
  200.     var val              = parseInt(theField.value);
  201.  
  202.     if (typeof(min) == 'undefined') {
  203.         min = 0;
  204.     }
  205.     if (typeof(max) == 'undefined') {
  206.         max = Number.MAX_VALUE;
  207.     }
  208.  
  209.     // It's not a number
  210.     if (isNaN(val)) {
  211.         theField.select();
  212.         alert(errorMsg1);
  213.         theField.focus();
  214.         return false;
  215.     }
  216.     // It's a number but it is not between min and max
  217.     else if (val < min || val > max) {
  218.         theField.select();
  219.         alert(val + errorMsg2);
  220.         theField.focus();
  221.         return false;
  222.     }
  223.     // It's a valid number
  224.     else {
  225.         theField.value = val;
  226.     }
  227.  
  228.     return true;
  229. } // end of the 'checkFormElementInRange()' function
  230.  
  231.  
  232. /**
  233.  * Ensures the choice between 'transmit', 'zipped', 'gzipped' and 'bzipped'
  234.  * checkboxes is consistant
  235.  *
  236.  * @paRAM   object   the form
  237.  * @paRAM   string   a code for the action that causes this function to be run
  238.  *
  239.  * @return  boolean  always true
  240.  */
  241. function checkTransmitDump(theForm, theAction)
  242. {
  243.     var formElts = theForm.elements;
  244.  
  245.     // 'zipped' option has been checked
  246.     if (theAction == 'zip' && formElts['zip'].checked) {
  247.         if (!formElts['asfile'].checked) {
  248.             theForm.elements['asfile'].checked = true;
  249.         }
  250.         if (typeof(formElts['gzip']) != 'undefined' && formElts['gzip'].checked) {
  251.             theForm.elements['gzip'].checked = false;
  252.         }
  253.         if (typeof(formElts['bzip']) != 'undefined' && formElts['bzip'].checked) {
  254.             theForm.elements['bzip'].checked = false;
  255.         }
  256.     }
  257.     // 'gzipped' option has been checked
  258.     else if (theAction == 'gzip' && formElts['gzip'].checked) {
  259.         if (!formElts['asfile'].checked) {
  260.             theForm.elements['asfile'].checked = true;
  261.         }
  262.         if (typeof(formElts['zip']) != 'undefined' && formElts['zip'].checked) {
  263.             theForm.elements['zip'].checked = false;
  264.         }
  265.         if (typeof(formElts['bzip']) != 'undefined' && formElts['bzip'].checked) {
  266.             theForm.elements['bzip'].checked = false;
  267.         }
  268.     }
  269.     // 'bzipped' option has been checked
  270.     else if (theAction == 'bzip' && formElts['bzip'].checked) {
  271.         if (!formElts['asfile'].checked) {
  272.             theForm.elements['asfile'].checked = true;
  273.         }
  274.         if (typeof(formElts['zip']) != 'undefined' && formElts['zip'].checked) {
  275.             theForm.elements['zip'].checked = false;
  276.         }
  277.         if (typeof(formElts['gzip']) != 'undefined' && formElts['gzip'].checked) {
  278.             theForm.elements['gzip'].checked = false;
  279.         }
  280.     }
  281.     // 'transmit' option has been unchecked
  282.     else if (theAction == 'transmit' && !formElts['asfile'].checked) {
  283.         if (typeof(formElts['zip']) != 'undefined' && formElts['zip'].checked) {
  284.             theForm.elements['zip'].checked = false;
  285.         }
  286.         if ((typeof(formElts['gzip']) != 'undefined' && formElts['gzip'].checked)) {
  287.             theForm.elements['gzip'].checked = false;
  288.         }
  289.         if ((typeof(formElts['bzip']) != 'undefined' && formElts['bzip'].checked)) {
  290.             theForm.elements['bzip'].checked = false;
  291.         }
  292.     }
  293.  
  294.     return true;
  295. } // end of the 'checkTransmitDump()' function
  296.  
  297.  
  298.  
  299. /**
  300.  * Checks/unchecks all tables
  301.  *
  302.  * @paRAM   string   the form name
  303.  * @paRAM   boolean  whether to check or to uncheck the element
  304.  *
  305.  * @return  boolean  always true
  306.  */
  307. function setCheckboxes(the_form, do_check)
  308. {
  309.     var elts      = document.forms[the_form].elements['selected_tbl[]'];
  310.     var elts_cnt  = elts.length;
  311.  
  312.     for (var i = 0; i < elts_cnt; i++) {
  313.         elts[i].checked = do_check;
  314.     } // end for
  315.  
  316.     return true;
  317. } // end of the 'setCheckboxes()' function
  318.  
  319.