Exemple #1
0
 /**
  * Allows the user to combine fields into a specified printf string
  * and then validate the entire string with any Peregrine method.
  *
  * Example: this allows the user to combine three-field-phone numbers
  * and validate the entire string.
  *
  * $p->post->combine('%s%s%s', array('area','prefix','suffix'), 'getPhone'));
  *
  * @param string $str
  * @param array $fields
  * @param string $method
  * @param array $args
  * @access public
  */
 public function combine($str, $fields = array(), $method = false, $args = array())
 {
     if (is_array($fields) && $method) {
         // Load raw field values
         $dirty_fields = array($str);
         foreach ($fields as $field) {
             $dirty_fields[] = $this->getRaw($field);
         }
         // Pass them all to the sprintf func and pass the resulting array to a new peregrine
         // instance, and then return the results of the specific method.
         $combined = array('combined' => call_user_func_array('sprintf', $dirty_fields));
         $p = new Peregrine();
         $clean = $p->sanitize($combined);
         // Pass any additional method arguments since certain methods allow for additional
         // configuration.
         $args = array_merge(array('combined'), $args);
         return call_user_func_array(array($clean, $method), $args);
     }
     return false;
 }
Exemple #2
0
/**
 *
 * @param <type> $line_end
 * @param <type> $ignore_phpunit
 */
function called_from($line_end = false, $ignore_phpunit = true)
{
    $line_end = $line_end ? $line_end : "\n";
    $db = debug_backtrace();
    $ret = array('trace' => '', 'caller' => array());
    foreach ($db as $pos => $caller) {
        if ($pos > 0) {
            $clean = Peregrine::sanitize($caller);
            if ($ignore_phpunit && strpos(strtolower($clean->getPath('file')), 'phpunit') !== false) {
                continue;
            } elseif (strpos(strtolower($clean->getPath('file')), 'debug') !== false) {
                continue;
            } elseif (strpos(strtolower($clean->getElemId('class')), 'debugbase') !== false) {
                continue;
            }
            if (empty($ret['caller'])) {
                $ret['caller']['file'] = $clean->getPath('file');
                $ret['caller']['line'] = $db[$pos - 1]['line'];
                $ret['caller']['class'] = $clean->getElemId('class');
                $ret['caller']['function'] = $clean->getElemId('function');
            }
            $ret['trace'] .= $pos . ': ' . $clean->getPath('file') . ' - ' . $clean->getInt('line') . ' called ' . $clean->getElemId('class') . '::' . $clean->getElemId('function') . '();' . $line_end;
        }
    }
    return $ret;
}
Exemple #3
0
 /**
  * Redirects a user to any complete/absolute URL. Optionally, you may also
  * provide status codes for an HTTP response as well as an exit, which discontinues
  * executing following php code.
  *
  * @param string $url
  * @param int $status
  * @param boolean $exit
  */
 public function redirectToUrl($url = false, $status = false, $exit = true)
 {
     $tmp_ar = array('url' => $url, 'status' => $status);
     $redirect = Peregrine::sanitize($tmp_ar);
     if ($redirect->isUri('url')) {
         header("Location: " . $redirect->getUri('url'));
         $status = $redirect->getDigits('status');
         $this->header_code($status);
         if ($exit) {
             exit;
         }
     } else {
         error()->raise(1, 'URL for redirect appears to be an invalid resource: ' . $url, __FILE__, __LINE__);
     }
 }
Exemple #4
0
 /**
  * Returns an array of all fields and their current values
  * @return array
  * @access public
  */
 public function getCurrentValues()
 {
     $current_values = array();
     if (is_array($this->_form_fields)) {
         foreach ($this->_form_fields as $field => $bits) {
             $current_values[$field] = $this->cv($field);
         }
     }
     return Peregrine::sanitize($current_values);
 }
Exemple #5
0
<?php

session_start();
$peregrine = new Peregrine();
$peregrine->init();
$prism = new Prism();
$qc = new QuartzCore();
$qc->init();
// Connect with db, so we can show errors and not wait for ajax.
try {
    $db = new PDO('mysql:host=' . MYSQL_HOSTNAME . ';port=' . MYSQL_PORT . ';dbname=' . MYSQL_DATABASE, MYSQL_USERNAME, MYSQL_PASSWORD);
    //  $db = new PDO("sqlite:my/database/path/database.db");
} catch (PDOException $e) {
    echo 'Prism WebUI can\'t connect to the database. ' . $e->getMessage();
    exit;
}
 /**
  *
  */
 public function test_serveCage()
 {
     $peregrine = new Peregrine();
     $peregrine->init();
     $this->assertEquals(NULL, $_SERVER);
     $this->assertEquals(true, is_string($peregrine->server->getRaw('HOSTNAME')));
 }
Exemple #7
0
 /**
  * Validates data is appropriate for the table before saving.
  * @param array $fields
  * @param mixed $primary_key
  * @return object
  * @access public
  */
 public function validate($fields = false, $primary_key = false)
 {
     $clean = false;
     // $fields must be an array or insert/update may not happen
     if (is_array($fields)) {
         // if primary key has been set, we need to load an existing record
         if ($primary_key && count($fields)) {
             $this->existing_record = $record = $this->quickSelectSingle($primary_key);
             // merge the record with the incoming fields array
             // - any key in fields array overrides record
             if (is_array($record)) {
                 $fields = array_merge($record, $fields);
             }
         }
         // make an inspekt cage so we can verify data
         $clean = Peregrine::sanitize($fields);
         $schema = $this->getSchema();
         foreach ($schema['schema'] as $column) {
             // if it's set, and a value is present, we must validate that
             // value against the database.
             // whether or not the value is present is up to the model extension, not this
             if ($clean->isSetAndNotEmpty($column->name)) {
                 /**
                  * Validate INTEGERs along with unsigned and maxlengths
                  */
                 if (in_array($column->type, app()->config('mysql_field_group_int'))) {
                     if (!$clean->getInt($column->name)) {
                         $this->addError($column->name, 'Invalid db value. ' . $column->name . ' should be an integer.');
                     } else {
                         if ($column->unsigned && !$clean->isGreaterThan($column->name, -1)) {
                             $this->addError($column->name, 'Invalid db value. ' . $column->name . ' may not be negative.');
                         }
                     }
                 }
                 /**
                  * Validate FLOATs along with unsigned and maxlengths
                  */
                 if (in_array($column->type, app()->config('mysql_field_group_dec'))) {
                     if (!$clean->getFloat($column->name)) {
                         $this->addError($column->name, 'Invalid db value. ' . $column->name . ' should be a decimal or float.');
                     } else {
                         if ($column->unsigned && !$clean->isGreaterThan($column->name, -1)) {
                             $this->addError($column->name, 'Invalid db value. ' . $column->name . ' may not be negative.');
                         }
                     }
                 }
                 /**
                  * Validate DATEs
                  */
                 if (in_array($column->type, app()->config('mysql_field_group_date'))) {
                     //if(!$clean->isDate( $date )){
                     //$this->addError($column->name, 'Invalid db value. ' . $column->name . ' must be a date.');
                     //}
                 }
                 /**
                  * Validate ENUMs
                  */
                 if ($column->type == 'enum') {
                     if (!$this->enumExists($column->name, $clean->getRaw($column->name))) {
                         $this->addError($column->name, 'Invalid db value. ' . $column->name . ' is not in list of acceptable values.');
                     }
                 }
                 /**
                  * Rules to apply to all
                  */
                 // maxlength
                 if ($column->max_length > 0 && strlen($clean->getRaw($column->name)) > $column->max_length) {
                     $this->addError($column->name, 'Invalid db value. ' . $column->name . ' exceeds maxlength.');
                 }
             }
         }
     }
     return $clean;
 }