Esempio n. 1
0
 function test_ToArray()
 {
     $stringVar = 'string';
     $objectVar = new stdClass();
     $objectVar->prop = 'propValue';
     $testVar = $stringVar;
     Oops_Utils::ToArray($testVar);
     $this->assertEquals(array('string'), $testVar);
     $testVar = $objectVar;
     Oops_Utils::ToArray($testVar);
     $this->assertEquals(array($objectVar), $testVar);
 }
Esempio n. 2
0
 /**
  * Get requested value, modified to the requested type
  *
  * @param string $key
  *        	request key
  * @param string $type
  *        	required value type
  * @param mixed $default
  *        	default value
  * @return mixed
  * @tutorial Oops/Oops/Controller.cls#handling_request
  */
 function Request($key, $type = null, $default = null)
 {
     if (!strlen($key)) {
         return false;
     }
     if (is_null($value = $this->_request->get($key))) {
         return $default;
     }
     if (is_null($type)) {
         return $value;
     }
     switch (strtolower($type)) {
         case 'bool':
         case 'boolean':
             return (bool) $value;
         case 'int':
         case 'integer':
             return (int) $value;
         case 'float':
         case 'double':
         case 'numeric':
         case 'decimal':
             return (double) $value;
         case 'array':
             require_once 'Oops/Utils.php';
             Oops_Utils::ToArray($value);
             return $value;
         case 'arrayint':
             require_once 'Oops/Utils.php';
             Oops_Utils::ToIntArray($value);
             return $value;
         case 'arraysql':
             require_once 'Oops/Utils.php';
             Oops_Utils::ToIntArray($value);
             return $value;
         case 'sql':
             require_once 'Oops/Sql.php';
             return Oops_Sql::Escape($value);
         case 'words':
             return preg_replace('/[^\\s\\w]/', '', $value);
         case 'trimmedwords':
             return trim(preg_replace('/[^\\s\\w]/', '', $value));
         default:
             return $value;
     }
 }
Esempio n. 3
0
 /**
  * Reads config values from DB and constructs Config object
  * 
  * @param string $table
  * @param string|array $keyFields Field name(s) to use as config key. If not given table's primary key will be used
  * @param string|array $valueFields Field name(s) to use as config value. If not given all fields excluding keys will be used
  * @param string $keyDelimiter Explode keys by this delimiter and group values for each exploded part  
  */
 public function __construct($table, $keyFields = null, $valueFields = null, $keyDelimiter = '.', $allowModifications = false)
 {
     $table = Oops_Sql_Common::escapeIdentifiers($table);
     if (is_null($keyFields)) {
         $keyFields = array();
         $r = Oops_Sql::Query("SHOW COLUMNS FROM {$table}", OOPS_SQL_EXCEPTION);
         while (($row = mysql_fetch_row($r)) !== false) {
             if (strtoupper($row[3]) == 'PRI') {
                 $keyFields[] = $row[0];
             }
         }
     } else {
         Oops_Utils::ToArray($keyFields);
     }
     if (!count($keyFields)) {
         throw new Exception("No key fields for config");
     }
     if (is_null($valueFields)) {
         $sql = "SELECT * FROM {$table}";
     } else {
         Oops_Utils::ToArray($valueFields);
         $select = array_merge($keyFields, $valueFields);
         foreach ($select as $k => $v) {
             $select[$k] = Oops_Sql_Common::escapeIdentifiers($v);
         }
         $sql = 'SELECT ' . join(',', $select) . " FROM {$table}";
     }
     $r = Oops_Sql::Query($sql);
     $data = array();
     while (($row = mysql_fetch_assoc($r)) !== false) {
         $keyParts = array();
         foreach ($keyFields as $keyField) {
             $keyParts[] = $row[$keyField];
             unset($row[$keyField]);
         }
         if (count($row) == 1) {
             $row = array_pop($row);
         }
         $data[join($keyDelimiter, $keyParts)] = $row;
     }
     parent::__construct($data, $keyDelimiter, $allowModifications);
 }
Esempio n. 4
0
 /**
  * Handle the request
  *
  * @todo proceed 301, 302, 404 and other statuses. Current response object should be used for this, or Oops_Server::Run should return a response object instead of text
  *
  * @return void
  */
 function _handle()
 {
     if ($this->_isHandled) {
         return;
     }
     $server = Oops_Server::newInstance();
     $response = $server->Run($this->_request);
     while ($response->isRedirect() && $this->_redirectLimit--) {
         /**
          * @todo do something, use Location header
          */
         $this->_request = new Oops_Server_Request_Custom($response->getHeader("Location"));
         $response = $server->Run($this->_request);
     }
     Oops_Server::popInstance();
     $server = null;
     if (!$response->isRedirect()) {
         $this->_content = $response->body;
         /* translate headers in order to deliver control headers like 'X-Accel-Expires' */
         $childHeaders = $response->getHeaders();
         if (count($childHeaders)) {
             $parentHeaders = Oops_Server::getResponse()->getHeaders();
             foreach ($childHeaders as $k => $v) {
                 if (!isset($parentHeaders[$k])) {
                     Oops_Server::getResponse()->setHeader($k, $v);
                 } elseif (is_array($parentHeaders[$k])) {
                     Oops_Utils::ToArray($v);
                     foreach ($v as $vv) {
                         Oops_Server::getResponse()->setHeader($k, $vv, false);
                     }
                 }
             }
         }
     }
     $this->_isHandled = true;
     $this->_position = 0;
 }
Esempio n. 5
0
 /**
  * Same as ToIntArray but escapes all strings with mysql_real_escape_string
  *
  * @static
  */
 public static function ToEscapedArray(&$a, $keepillegal = false)
 {
     Oops_Utils::ToArray($a);
     if (count($a)) {
         foreach ($a as $k => $v) {
             $v1 = Oops_Sql::Escape($v);
             if ($v != $v1) {
                 if (!$keepillegal) {
                     unset($a[$k]);
                 } else {
                     $a[$k] = $v1;
                 }
             }
         }
     }
 }
Esempio n. 6
0
 protected function _sqlCondition($field, $compare, $value)
 {
     $fid = Oops_Sql_Common::escapeIdentifiers($this->_useAlias ? $this->_alias . '.' . $field : $field);
     $cond = $fid . ' ';
     switch ($compare) {
         case self::CMP_NULL:
             $cond .= 'IS NULL';
             break;
         case self::CMP_NOTNULL:
             $cond .= 'IS NOT NULL';
             break;
         case self::CMP_NE:
             $cond .= is_array($value) ? 'NOT ' : '!';
         case self::CMP_EQ:
             if (is_array($value)) {
                 $cond .= 'IN (' . join(',', array_map(array('Oops_Sql_Common', 'quoteValue'), $value)) . ')';
             } else {
                 $cond .= '= ' . Oops_Sql_Common::quoteValue($value);
             }
             break;
         case self::CMP_GT:
         case self::CMP_LT:
         case self::CMP_GE:
         case self::CMP_LE:
             if (is_array($value)) {
                 throw new Oops_Sql_Selector_Exception("Unexcepted array", Oops_Sql_Selector_Exception::UnexpectedValueType);
             }
             $operands = array(self::CMP_GT => '> ', self::CMP_GE => '>= ', self::CMP_LT => '< ', self::CMP_LE => '<= ');
             $cond .= $operands[$compare] . Oops_Sql_Common::quoteValue($value);
             break;
         case self::CMP_LIKE:
             Oops_Utils::ToArray($value);
             if (!count($value)) {
                 $cond = '';
             } else {
                 $value = array_map(array('Oops_Sql', 'Escape'), $value);
                 $cond = "({$fid} LIKE '" . join("' OR {$fid} LIKE '", $value) . "')";
             }
             break;
         case self::CMP_MLIKE:
             Oops_Utils::ToArray($value);
             if (!count($value)) {
                 $cond = '';
             } else {
                 $value = array_map(array('Oops_Sql', 'Escape'), $value);
                 $cond = "({$fid} LIKE '%" . join("%' OR {$fid} LIKE '%", $value) . "%')";
             }
             break;
         default:
             throw new Oops_Sql_Selector_Exception("Unexpected compare type {$compare}", Oops_Sql_Selector_Exception::UnexpectedCompareType);
     }
     return $cond;
 }