Esempio n. 1
0
 public function __get($label)
 {
     if (array_has_key($label, $this->_marks)) {
         return $this->_marks[$label] - $this->_start;
     }
     return null;
 }
Esempio n. 2
0
 /**
  * Connects to the MySQL database.
  *
  * \param settings An associative array with connection settings: the
  * 'hostname', 'username' and 'password' indices will be used.
  */
 function connect(array $settings)
 {
     is_array($settings) && isset($settings['hostname'], $settings['username'], $settings['password']) or trigger_error('Invalid parameters to connect()', E_USER_ERROR);
     $this->id = mysql_pconnect($settings['hostname'], $settings['username'], $settings['password'], true) or trigger_error(sprintf('Could not connect to database (%d: %s)', mysql_errno($this->id), mysql_error($this->id)), E_USER_ERROR);
     if (array_has_key($settings, 'database')) {
         $this->select_db($settings['database']);
     }
 }
Esempio n. 3
0
 /**
  * Returns the relative URL for the current request
  *
  * \param $include_query_string
  *   Whether the query string (the part after the question mark in HTTP GET
  *   request should be included (optional, defaults to true)
  *
  * \return
  *   The relative URL for the current request.
  *
  * \see AnewtRequest::canonical_url
  */
 public static function relative_url($include_query_string = true)
 {
     assert('is_bool($include_query_string)');
     /* Normal case */
     if (array_has_key($_SERVER, 'REQUEST_URI')) {
         $out = $_SERVER['REQUEST_URI'];
     } elseif (array_has_key($_SERVER, 'argv')) {
         $out = $_SERVER['argv'][0];
     } elseif (array_has_key($_SERVER, 'QUERY_STRING')) {
         $out = sprintf('%s?%s', $_SERVER['PHP_SELF'], $_SERVER['QUERY_STRING']);
     } else {
         $out = $_SERVER['PHP_SELF'];
     }
     /* Strip off query string if needed */
     if (!$include_query_string && str_contains($out, '?')) {
         $out = substr($out, 0, strpos($out, '?'));
     }
     return $out;
 }
Esempio n. 4
0
 /**
  * Connects to the PostgreSQL database.
  *
  * \param settings An associative array with connection settings: the
  * 'hostname', 'username' and 'password' indices will be used for connection
  * setttings. The key 'keep_settings' can be used to indicate whether the
  * settings are stored. The 'escape_column_names' and 'escape_table_names'
  * keys can be set to indicate whether column and table names should be
  * escaped when using CRUD functions.
  */
 function connect(array $settings)
 {
     if (is_null($settings)) {
         $settings = array();
     }
     assert('is_assoc_array($settings)');
     /* We support "hostname", "username" and "pass" too, although the
      * real connection string uses other names */
     $aliases = array('hostname' => 'host', 'pass' => 'password', 'username' => 'user', 'database' => 'dbname');
     foreach ($aliases as $old => $new) {
         if (array_has_key($settings, $old)) {
             array_set_default($settings, $new, $settings[$old]);
         }
     }
     /* List of keywords that are allowed in the connection string: */
     $keywords = array('host', 'hostaddr', 'port', 'dbname', 'user', 'password', 'connect_timeout', 'options', 'sslmode', 'service');
     /* Create a connection string from the supplied values, leaving out
      * illegal (name, value) pairs */
     $options = array();
     foreach ($keywords as $keyword) {
         if (array_key_exists($keyword, $settings)) {
             $value = $settings[$keyword];
             assert('is_string($value)');
             /* Escape single and double quotes */
             $value = str_replace("'", "\\'", $value);
             $value = str_replace('"', '\\"', $value);
             $options[] = sprintf('%s=%s', $keyword, $value);
         }
     }
     $connection_string = implode(' ', $options);
     $this->id = pg_connect($connection_string) or trigger_error(sprintf('Could not connect to database %s', $settings['dbname']), E_USER_ERROR);
     /* Keep connection settings only if requested. This makes select_db()
      * work, but stores the plaintext password in the object's memory. */
     if (!array_get_default($settings, 'keep_settings', false)) {
         /* Unset both the aliases and the connection keywords */
         array_unset_keys($settings, array_keys($aliases));
         array_unset_keys($settings, $keywords);
     }
     $this->settings = $settings;
 }
Esempio n. 5
0
 public function get_edges($input)
 {
     $values = array();
     $edges = array();
     foreach (preg_split('/\\n/', $input) as $line) {
         $matches = array();
         preg_match('/^(\\w+) would (lose|gain) (\\d+).*?to (\\w+)\\.$/', $line, $matches);
         if (sizeof($matches) != 5) {
             continue;
         }
         $from = $matches[1];
         $value = $matches[3];
         if ($matches[2] == 'lose') {
             $value = -$value;
         }
         $to = $matches[4];
         if (!array_has_key($from, $values)) {
             $values[$from] = array();
         }
         $values[$from][$to] = $value;
     }
     foreach (array_keys($values) as $from) {
         $tos = $values[$from];
         foreach (array_keys($tos) as $to) {
             $value = $values[$from][$to];
             $value += $values[$to][$from];
             if (!array_has_key($from, $edges)) {
                 $edges[$from] = array();
             }
             $edges[$from][$to] = $value;
             $edges[$to][$from] = $value;
             unset($values[$to][$from]);
         }
     }
     return $edges;
 }
 /**
     @param $t Type
     Predicate to decide if a given Type $v is a valid value for an AreaOfInterestType.
 */
 public static function validType($t)
 {
     return array_has_key($t, $this->types());
 }
Esempio n. 7
0
 /**
  * Test array_flip_string_keys
  */
 function test_array_flip_string_keys()
 {
     $data = array('first' => 'one', 'second' => 'two', 'third' => 'three', 4 => 'four', 5 => 'five', 'six' => 6);
     $data = array_flip_string_keys($data);
     $this->assertTrue(array_has_key($data, "one"));
     $this->assertTrue(array_has_key($data, "two"));
     $this->assertFalse(array_has_key($data, "first"));
     $this->assertTrue(array_has_key($data, 4));
     $this->assertTrue(array_has_key($data, 6));
     $this->assertFalse(array_has_key($data, "six"));
     $data = array_flip_string_keys($data);
     $this->assertTrue(array_has_key($data, 6));
     $this->assertTrue(array_has_key($data, "first"));
 }
Esempio n. 8
0
 /** \{
  * \name Methods for getting and setting values
  */
 function fill($values)
 {
     /* See AnewtFormControl::fill() for why: */
     if (!$this->_get('can-be-filled')) {
         return true;
     }
     if ($this->_get('disabled')) {
         return true;
     }
     /* If none of the values in the choice control are selected, there is no
      * value for this control in $values (just like for checkboxes). */
     $name = $this->get('name');
     $value = array_get_default($values, $name, null);
     parent::fill(array($name => $value));
     /* Filling succeeds if... */
     return $this->get('multiple') || array_has_key($values, $name) || $this->all_disabled();
     /* (3) if all options are disabled */
 }
Esempio n. 9
0
 /**
  * Check if a variable is defined in the configuration data.
  *
  * \param $name
  *   The variable name to check for.
  *
  * \return
  *   True if the variable is available, false otherwise.
  */
 public static function is_set($name)
 {
     assert('is_string($name)');
     global $_anewt_config;
     return array_has_key($_anewt_config, $name);
 }
Esempio n. 10
0
 /**
  * Opens the SQLite database.
  *
  * \param settings An associative array with connection settings: the
  * 'filename' and 'mode' indices will be used (but mode is optional).
  */
 function connect(array $settings)
 {
     is_array($settings) && array_has_key($settings, 'filename') or trigger_error('Invalid parameters to connect()', E_USER_ERROR);
     $mode = array_get_default($settings, 'mode', 0666);
     $this->handle = sqlite_open($settings['filename'], $mode, $error) or trigger_error(sprintf('Could not open database (%s)', $error), E_USER_ERROR);
 }
Esempio n. 11
0
 function find_context($prefix, $action, $params, &$context_type, &$context)
 {
     $context_type = NULL;
     $context = NULL;
     $result = FALSE;
     switch ($prefix) {
         case 'SA':
             /* This is just an example. */
             if (array_has_key(SA_ARGUMENT::SLICE_ID, $params)) {
                 $context_type = CS_CONTEXT_TYPE::SLICE;
                 $context = $params[SA_ARGUMENT::SLICE_ID];
             }
             break;
         case 'MA':
             /* Stub out MA context. */
             break;
         default:
             //       error_log("MessageHandler: Unknown prefix \"$prefix\"");
             // Leave $context and $context_type NULL
             // Leave $result = FALSE
             break;
     }
     //     error_log("MessageHandler find_context returning"
     //               . ": \$context_type = $context_type; \$context = $context");
     return $result;
 }
Esempio n. 12
0
 /**
  * Test the grouping methods.
  */
 function test_grouping()
 {
     $all_records = Person::db_find_all();
     $list = Person::array_by_column_value($all_records, 'name', true);
     $this->assertTrue($list['Foo'] instanceof Person);
     $this->assertEquals(6, count($list));
     $list = Person::array_by_column_value($all_records, 'name', false);
     $this->assertTrue(is_array($list['Foo']));
     $this->assertEquals(6, count($list));
     $this->assertTrue($list['Foo'][0] instanceof Person);
     $list = Person::array_by_primary_key_value($all_records);
     $this->assertTrue($list[1] instanceof Person);
     $this->assertEquals(6, count($list));
     $this->assertFalse(array_has_key($list, 7));
 }
Esempio n. 13
0
 /**
  * Set the value by the filename of the uploaded file, but only if a file is uploaded.
  */
 function fill($values)
 {
     $filename = $this->get('filename');
     // Check for the 'remove' checkbox.
     $remove = array_has_key($values, $this->get('name') . '-remove');
     // If removed or replaced, mark file for deletion.
     if (($remove || $filename) && $this->get('delete-on-change') && $this->get('value')) {
         $this->_set('_remove', $this->get('value'));
     }
     // Unset the value if removed.
     if ($remove) {
         $this->set('value', '');
     }
     // Set new value if replaced.
     if ($filename) {
         $this->set('value', $filename);
     }
     return true;
 }