Exemple #1
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);
 }
Exemple #2
0
 public function setHeader($name, $value, $replace = true)
 {
     $name = strtolower($name);
     if ($replace || !isset($this->_headers[$name])) {
         $this->_headers[$name] = $value;
     } else {
         Oops_Utils::toArray($this->_headers[$name]);
         $this->_headers[$name][] = $value;
     }
 }
Exemple #3
0
 protected function _processData(array &$data, $parentGroupName = false)
 {
     $result = array();
     foreach ($data as $v) {
         if ($v['type'] == 'file' && !$this->_defined) {
             $this->file = true;
             break;
         }
     }
     if ($this->file && !$this->_defined) {
         $this->setAttr('method', 'post');
         $this->setAttr('enctype', 'multipart/form-data');
         $this->_defined = true;
         require_once 'Oops/Utils.php';
         $max_file_size = min(Oops_Utils::toBytes(ini_get('upload_max_filesize')), Oops_Utils::toBytes(ini_get('post_max_size')));
         array_unshift($data, array('type' => 'hidden', 'name' => 'MAX_FILE_SIZE', 'value' => $max_file_size));
     }
     foreach ($data as $v) {
         if (!isset($v['text'])) {
             $v['text'] = '';
         }
         if ($v['type'] === 'group' && isset($v['items']) && !empty($v['items'])) {
             if (isset($this->_params[$v['name']])) {
                 foreach ($this->_params[$v['name']] as $paramKey => $paramVal) {
                     $v[$paramKey] = $paramVal;
                 }
             }
             if ($this->groupNames && $parentGroupName) {
                 $newName = $parentGroupName . '[' . $v['name'] . ']';
             } elseif ($this->groupNames && !$parentGroupName) {
                 $newName = $v['name'];
             } else {
                 $newName = false;
             }
             $result[] = array('name' => $v['name'], 'text' => $v['text'], 'items' => $this->_processData($v['items'], $newName));
         } else {
             if (isset($this->_params[$v['name']]['display']) && $this->_params[$v['name']]['display'] == false) {
                 continue;
             }
             if (isset($this->_params[$v['name']])) {
                 foreach ($this->_params[$v['name']] as $paramKey => $paramVal) {
                     $v[$paramKey] = $paramVal;
                 }
             }
             if ($this->groupNames && $parentGroupName) {
                 $v['name'] = $parentGroupName . '[' . $v['name'] . ']';
             }
             $html = $this->_makeField($v);
             $result[] = array('name' => $v['name'], 'text' => $v['text'], 'html' => $html);
         }
     }
     return $result;
 }
Exemple #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;
 }
Exemple #5
0
 /**
  * @ignore
  *
  * @param array $ret Return value
  * @param array $Tree Tree segment
  * @param string $childrenId Children key in Element array
  * @param string $levelId A key to set with element's depth level value
  * 
  * @author cloud
  * @access public
  */
 protected static function _Tree2Line(&$ret, &$Tree, $childrenId, $levelId, $Level = 0)
 {
     foreach (array_keys($Tree) as $i) {
         $ret[$i] = $Tree[$i];
         unset($ret[$i][$childrenId]);
         $ret[$i][$levelId] = $Level;
         if (isset($Tree[$i][$childrenId])) {
             Oops_Utils::_Tree2Line($ret, $Tree[$i][$childrenId], $childrenId, $levelId, $Level + 1);
         }
     }
 }
Exemple #6
0
 public function test_toBytes()
 {
     $this->assertEquals(1024, Oops_Utils::toBytes('1K'));
     $this->assertEquals(2 * 1024, Oops_Utils::toBytes(' 2K '));
     $this->assertEquals(8 * 1024 * 1024, Oops_Utils::toBytes('8M'));
 }
Exemple #7
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;
 }
Exemple #8
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;
     }
 }