Beispiel #1
0
	/**
	 * Attempts to log a user in to the system.
	 *
	 * @param string $username the specified username
	 * @param string $password the specified password
	 * @return integer the result of the login process
	 */
	public static function login($username, $password) {
		try {
			// Setup
			$protocol = new self::$_protocol($username, $password);
			$result = $protocol->authenticate();

			if($result > 0) {
				self::$_id = \Bedrock\Common\String::random(32);
				self::$_status = self::STATUS_LOGGED_IN;
			}
			return $result;
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Common\Auth\Exception('A problem was encountered while attempting to authenticate the specified user.');
		}
	}
Beispiel #2
0
	/**
	 * Imports the table schema using the specified source. The table will not
	 * be created until the \Bedrock\Model\Table::save() method is called.
	 *
	 * @param string $importSource the source file to import from
	 * @param integer $importType the format of the source file
	 * @return boolean whether or not the import was successful
	 */
	public function importSchema($importSource, $importType = \Bedrock\Model::FORMAT_SQL, $sourceIsFile = true) {
		try {
			// Setup
			$result = false;

			if($sourceIsFile && !is_file($importSource)) {
				throw new \Bedrock\Model\Exception('The import source specified is invalid: "' . $importSource . '"');
			}

			// Copy/Backup Table
			$backupName = '_bed_' . \Bedrock\Common\String::random(4) . '_' . $this->_name;
			\Bedrock\Common\Logger::info('Creating backup of table "' . $this->_name . '" to "' . $backupName . '"');
			$sql = 'CREATE TABLE ' . self::sanitize($backupName) . ' SELECT * FROM ' . self::sanitize($this->_name);
			
			if(!$this->_connection->query($sql)) throw new \Bedrock\Model\Exception('Table backup failed for table "' . $this->_name . '".');

			// Drop Table
			if($this->drop()) throw new \Bedrock\Model\Exception('Dropping table "' . $this->_name . '" failed.');

			// Parse Data
			switch($importType) {
				default:

				// SQL Query
				case \Bedrock\Model::FORMAT_SQL:
					if($sourceIsFile) {
						$sql = file_get_contents($importSource);
					}
					else {
						$sql = $importSource;
					}
					break;

				// XML Document
				case \Bedrock\Model::FORMAT_XML:
					if($sourceIsFile) {
						$xml = simplexml_load_file($importSource);
					}
					else {
						$xml = simplexml_load_string($importSource);
					}

					$sql = 'CREATE TABLE `' . self::sanitize($xml['name']) . '` (';

					foreach($xml->columns->column as $column) {
						$colObj = new \Bedrock\Model\Column(array(
							'name' => $column['name'],
							'type' => $column->type,
							'length' => $column->length,
							'size' => $column->size,
							'null' => $column->flags->null == 0 ? false : true,
							'default' => $column->default,
							'primary_key' => $column->flags->primarykey,
							'foreign_key' => '',
							'foreign_key_type' => ''
						));

						$sql .= $colObj->definition . ', ';
					}

					$sql = substr($sql, 0, strlen($sql)-2) . ')';

					if(isset($xml->properties->engine)) {
						$sql .= ' ENGINE = ' . self::sanitize($xml->properties->engine);
					}

					if(isset($xml->properties->charset)) {
						$sql .= ' DEFAULT CHARACTER SET = ' . self::sanitize($xml->properties->charset);
					}

					if(isset($xml->properties->collation)) {
						$sql .= ' DEFAULT COLLATE = ' . self::sanitize($xml->properties->collation);
					}

					if(isset($xml->properties->comment)) {
						$sql .= ' COMMENT = \'' . self::sanitize($xml->properties->comment) . '\'';
					}

					break;

				// YAML Document
				case \Bedrock\Model::FORMAT_YAML:
					if($sourceIsFile) {
						$importString = file_get_contents($importSource);
					}
					else {
						$importString = $importSource;
					}

					$yaml = new \Bedrock\Common\Data\YAML($importString, true);
					$sql = 'CREATE TABLE `' . self::sanitize($yaml->table->name) . '` (';
					
					foreach($yaml->table->columns as $column) {
						$colObj = new \Bedrock\Model\Column(array(
							'name' => $column->name,
							'type' => $column->type,
							'length' => $column->length,
							'size' => $column->size,
							'null' => $column->flags->null,
							'default' => $column->default,
							'primary_key' => $column->flags->primarykey,
							'foreign_key' => '',
							'foreign_key_type' => ''
						));
						
						$sql .= $colObj->definition . ', ';
					}

					$sql = substr($sql, 0, strlen($sql)-2) . ')';

					if(isset($yaml->table->properties->engine)) {
						$sql .= ' ENGINE = ' . self::sanitize($yaml->table->properties->engine);
					}

					if(isset($yaml->table->properties->charset)) {
						$sql .= ' DEFAULT CHARACTER SET = ' . self::sanitize($yaml->table->properties->charset);
					}

					if(isset($yaml->table->properties->collation)) {
						$sql .= ' DEFAULT COLLATE = ' . self::sanitize($yaml->table->properties->collation);
					}

					if(isset($yaml->table->properties->comment)) {
						$sql .= ' COMMENT = \'' . self::sanitize($yaml->table->properties->comment) . '\'';
					}
					
					break;

				// CSV Document
				case \Bedrock\Model::FORMAT_CSV:
					if($sourceIsFile) {
						$importString = file_get_contents($importSource);
					}
					else {
						$importString = $importSource;
					}

					$csv = new \Bedrock\Common\Data\CSV($importString, ', ', true);
					$tableRow = null;
					$columnRows = array();
					
					foreach($csv as $row) {
						if($row['class'] == 'table') {
							$tableRow = $row;
						}
						elseif($row['class'] == 'column') {
							$columnRows[] = $row;
						}
					}
					
					$sql = 'CREATE TABLE `' . self::sanitize($tableRow['name']) . '` (';
					
					foreach($columnRows as $columnRow) {
						$colObj = new \Bedrock\Model\Column(array(
							'name' => isset($columnRow['name']) ? $columnRow['name'] : '',
							'type' => isset($columnRow['type']) ? $columnRow['type'] : '',
							'length' => isset($columnRow['length']) ? $columnRow['length'] : '',
							'size' => isset($columnRow['size']) ? $columnRow['size'] : '',
							'null' => isset($columnRow['null']) ? $columnRow['null'] : '',
							'default' => isset($columnRow['default']) ? $columnRow['default'] : '',
							'primary_key' => isset($columnRow['primarykey']) ? $columnRow['primaryKey'] : '',
							'foreign_key' => '',
							'foreign_key_type' => ''
						));

						$sql .= $colObj->definition . ', ';
					}

					$sql = substr($sql, 0, strlen($sql)-2) . ')';

					if(isset($tableRow['engine']) && $tableRow['engine'] != '') {
						$sql .= ' ENGINE = ' . self::sanitize($tableRow['engine']);
					}

					if(isset($tableRow['charset']) && $tableRow['charset'] != '') {
						$sql .= ' DEFAULT CHARACTER SET = ' . self::sanitize($tableRow['charset']);
					}

					if(isset($tableRow['collation']) && $tableRow['collation'] != '') {
						$sql .= ' DEFAULT COLLATE = ' . self::sanitize($tableRow['collation']);
					}

					if(isset($tableRow['comment']) && $tableRow['comment'] != '') {
						$sql .= ' COMMENT = \'' . self::sanitize($tableRow['comment']) . '\'';
					}

					break;
			}

			// Execute Queries
			\Bedrock\Common\Logger::info('Importing schema with query: ' . $sql);
			$res = $this->_connection->query($sql);

			if(!$res) {
				$this->revert($backupName);
				$result = false;
			}
			else {
				\Bedrock\Common\Logger::info('Removing backup table "' . $backupName .'"...');
				$this->_connection->query('DROP TABLE IF EXISTS ' . self::sanitize($backupName));
				$result = true;
			}
			return $result;
		}
		catch(\Bedrock\Model\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			if(isset($backupName)) {
				$this->revert($backupName);
			}

			throw new \Bedrock\Model\Exception('Schema import failed.');
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			if(isset($backupName)) {
				$this->revert($backupName);
			}

			throw new \Bedrock\Model\Exception('Schema import failed.');
		}
	}