Ejemplo n.º 1
0
 private function validate()
 {
     if (empty($this->raw) or '' == trim($this->raw)) {
         $this->valid = true;
         return true;
     }
     // CAS keyval may not contain @ or $.
     if (strpos($this->raw, '@') !== false || strpos($this->raw, '$') !== false) {
         $this->errors = stack_string('illegalcaschars');
         $this->valid = false;
         return false;
     }
     // Subtle one: must protect things inside strings before we explode.
     $str = $this->raw;
     $strings = stack_utils::all_substring_strings($str);
     foreach ($strings as $key => $string) {
         $str = str_replace('"' . $string . '"', '[STR:' . $key . ']', $str);
     }
     $str = str_replace("\n", ';', $str);
     $str = stack_utils::remove_comments($str);
     $str = str_replace(';', "\n", $str);
     $kvarray = explode("\n", $str);
     foreach ($strings as $key => $string) {
         foreach ($kvarray as $kkey => $kstr) {
             $kvarray[$kkey] = str_replace('[STR:' . $key . ']', '"' . $string . '"', $kstr);
         }
     }
     // 23/4/12 - significant changes to the way keyvals are interpreted.  Use Maxima assignmentsm i.e. x:2.
     $errors = '';
     $valid = true;
     $vars = array();
     foreach ($kvarray as $kvs) {
         $kvs = trim($kvs);
         if ('' != $kvs) {
             $cs = new stack_cas_casstring($kvs);
             $cs->get_valid($this->security, $this->syntax, $this->insertstars);
             $vars[] = $cs;
         }
     }
     $this->session->add_vars($vars);
     $this->valid = $this->session->get_valid();
     $this->errors = $this->session->get_errors();
 }
Ejemplo n.º 2
0
 /**
  * Extract the CAS commands from the string
  *
  * @access public
  * @return bool false if no commands to extract, true if succeeds.
  */
 private function extract_cas_commands()
 {
     // First check contains @s.
     $count = preg_match_all('~(?<!@)@(?!@)~', $this->trimmedcastext, $notused);
     if ($count == 0) {
         // Nothing to do.
         return null;
     } else {
         // Extract the CAS commands.
         $temp = stack_utils::all_substring_between($this->trimmedcastext, '@', '@', true);
         // Create array of commands matching with their labels.
         $i = 0;
         $valid = true;
         $errors = '';
         $cmdarray = array();
         $labels = array();
         $sessionkeys = array();
         if (is_a($this->session, 'stack_cas_session')) {
             $sessionkeys = $this->session->get_all_keys();
         }
         foreach ($temp as $cmd) {
             // Trim of surrounding white space and CAS commands.
             $cmd = stack_utils::trim_commands($cmd);
             $cs = new stack_cas_casstring($cmd);
             $cs->get_valid($this->security, $this->syntax, $this->insertstars);
             do {
                 // ... make sure names are not already in use.
                 $key = 'caschat' . $i;
                 $i++;
             } while (in_array($key, $sessionkeys));
             $sesionkeys[] = $key;
             $labels[] = $key;
             $cs->set_key($key, true);
             $cmdarray[] = $cs;
             $valid = $valid && $cs->get_valid();
             $errors .= $cs->get_errors();
         }
         if (!$valid) {
             $this->valid = false;
             $this->errors .= stack_string('stackCas_invalidCommand') . '</br>' . $errors;
         }
         if (!empty($cmdarray)) {
             $newsession = $this->session;
             if (null === $newsession) {
                 $newsession = new stack_cas_session($cmdarray, null, $this->seed);
             } else {
                 $newsession->add_vars($cmdarray);
             }
             $this->session = $newsession;
             // Now replace the commannds with their labels in the text.
             $this->trimmedcastext = stack_utils::replace_between($this->trimmedcastext, '@', '@', $labels, true);
         }
     }
 }