/** * Executes the specified command and returns the resulting output. * * @param string $command the command to execute * @return string the last line of the command executed */ protected function execute($command, &$output = array()) { try { // Setup $result = ''; $output = array(); $returnVar = NULL; if($command != '') { \Bedrock\Common\Logger::info('Executing command "' . $command . '" at location "' . '' .'"'); $result = exec($command, $output, $returnVar); } $result = explode(' up ', $result); $result = $result[1]; $result = explode(' users', $result); $result = $result[0]; $result = substr($result, 0, strrpos($result, ',')); \Bedrock\Common\Logger::info('Returning result "' . $result . '"'); return $result; } catch(\Exception $ex) { \Bedrock\Common\Logger::exception($ex); } }
/** * Sends an email using the specified details. * * @param string $from the "from" address * @param string $to the recipient * @param string $subject the subject of the email * @param string $body the body of the email * @return integer the result of the process */ public static function send($from, $to, $subject, $body) { try { // Setup $config = \Bedrock\Common\Registry::get('config'); $headers = array('From' => $from, 'To' => $to, 'Subject' => $subject, 'Date' => date("r", time())); $smtpConfig = array('host' => $config->email->smtp, 'port' => $config->email->port, 'auth' => true, 'username' => $config->email->username, 'password' => $config->email->password); $smtp = \Mail::factory('smtp', $smtpConfig); \Bedrock\Common\Logger::info('Attempting to send an email to "' . $to . '" ...'); $mail = $smtp->send($to, $headers, $body); if(\PEAR::isError($mail)) { throw new \Bedrock\Common\Email\Exception($mail->getMessage()); } } catch(\Bedrock\Common\Email\Exception $ex) { \Bedrock\Common\Logger::exception($ex); throw $ex; } catch(\Exception $ex) { \Bedrock\Common\Logger::exception($ex); throw new \Bedrock\Common\Email\Exception('The email could not be sent.'); } }
/** * Sets the protocol to use for authentication. * * @param string $protocol a valid authentication protocol */ public static function setProtocol($protocol) { try { \Bedrock\Common\Logger::info('Protocol set to: ' . $protocol); self::$_protocol = $protocol; } catch(\Exception $ex) { \Bedrock\Common\Logger::exception($ex); throw new \Bedrock\Common\Auth\Exception('The protocol could not be set.'); } }
/** * Attempts to execute the specified command. * * @param string $command the command to execute * @return string the last line of output for the command executed */ public static function exec($command, &$output = array()) { try { // Setup $result = ''; $returnVar = NULL; if($command != '') { \Bedrock\Common\Logger::info('Executing command "' . $command . '" at location "' . getcwd() .'"'); $result = exec($command, $output, $returnVar); } \Bedrock\Common\Logger::info('Returning result "' . $result . '"'); } catch(\Exception $ex) { \Bedrock\Common\Logger::exception($ex); } }
/** * Retrieves the names for all tables associated with the specified table. * * @param string $tableName the table to find associations with * @return array an array of associated tables and the type of association */ public static function getAssociatedTableNames($tableName) { try { // Setup $result = array(); $connection = \Bedrock\Common\Registry::get('database')->getConnection(); // Query for Associations $sql = 'SHOW TABLE STATUS WHERE Comment LIKE \'table|%\' AND ' . '(Comment LIKE \'%:' . self::sanitize($tableName) . '(%\' OR ' . 'Comment LIKE \'%,' . self::sanitize($tableName) . '(%\')'; \Bedrock\Common\Logger::info('Querying for associated tables: "' . $sql . '"'); $res = $connection->query($sql)->fetchAll(\PDO::FETCH_ASSOC); foreach($res as $row) { // Parse Association Type $mappings = explode(',', substr($row['Comment'], 15)); $type = ''; foreach($mappings as $mapping) { if(substr($mapping, 0, strpos($mapping, '(')) == $tableName) { $matches = array(); preg_match('#\((.*?)\)#', $mapping, $matches); $type = $matches[1]; } } $result[$row['Name']] = $type; } return $result; } catch(\Exception $ex) { \Bedrock\Common\Logger::exception($ex); } }
/** * Deletes the currend record from the database. */ public function delete() { try { if($this->_state == self::STATE_UNCHANGED) { $sql = 'DELETE FROM ' . self::sanitize($this->_table->getProperty('name')) . ' WHERE ' . self::sanitize($this->_key_primary->name) . ' = ' . self::sanitize($this->_data[$this->_key_primary->name]); \Bedrock\Common\Logger::info('Deleting record with query: ' . $sql); echo $sql; $this->_connection->exec($sql); } elseif($this->_state == self::STATE_CHANGED) { throw new \Bedrock\Model\Record\Exception('Unsaved changes found, cannot delete record.'); } else { throw new \Bedrock\Model\Record\Exception('Record not found in database.'); } } catch(\PDOException $ex) { \Bedrock\Common\Logger::exception($ex); throw new \Bedrock\Model\Record\Exception('A database error was encountered, the record could not be deleted.'); } catch(\Exception $ex) { \Bedrock\Common\Logger::exception($ex); throw new \Bedrock\Model\Record\Exception('The record could not be deleted.'); } }
/** * Determines whether the current page request is a Bedrock-based AJAX * request. * * @return boolean whether or not the request is an AJAX request */ public static function isAjaxRequest() { // Setup $result = false; if($_POST['ajax'] == 1) { \Bedrock\Common\Logger::info('AJAX Request Detected'); $result = true; } return $result; }
/** * Exports the table's data to the specified location using the specified * format. * * @param string $exportLocation the file to which exported data will be saved * @param integer $exportType the export format to use */ public function exportData($exportLocation, $exportType = \Bedrock\Model::FORMAT_SQL) { try { \Bedrock\Common\Logger::info('Exporting data in table "' . $this->_name . '" as ' . strtoupper(self::formatToString($exportType)) . '...'); $fileContents = $this->dataToString($exportType); self::writeFile($exportLocation, $fileContents); } catch(\Exception $ex) { \Bedrock\Common\Logger::exception($ex); throw new \Bedrock\Model\Exception('Data export failed.'); } }
/** * Executes the assembled query and returns the corresponding results. * * @return \Bedrock\Model\ResultSet a ResultSet object holding the corresponding results */ public function execute() { try { // Setup $sql = ''; $records = array(); $results = array(); $whereClause = ''; // Build Query switch($this->_target) { default: case self::TARGET_TABLE: case self::TARGET_VIEW: if($this->_table) { $sql = $this->_query['from']; if(is_array($this->_query['where'])) { foreach($this->_query['where'] as $key => $where) { if($key == 0) { $whereClause .= ' WHERE ' . self::sanitize($where['field']); } else { $whereClause .= ' AND ' . self::sanitize($where['field']); } if(is_array($where['value'])) { // Array of Values switch($where['operator']) { case '=': $whereClause .= ' IN (' . self::valueToString($where['value']) . ')'; break; case '<>': case '!=': $whereClause .= ' NOT IN (' . self::valueToString($where['value']) . ')'; break; } // switch($where['operator']) { // case '=': // $sql .= ' IN ('; // break; // // case '<>': // $sql .= ' NOT IN ('; // break; // } // // foreach($where['value'] as $subkey => $value) { // $sq .= ':where' . $key . '_' . $subkey . ', '; // } // // $sql = substr($sql, 0, strlen($sql)-2) . ')'; } else { // Single Value $noVal = false; switch($where['operator']) { default: case '=': $where['operator'] = '='; break; case '<=>': $where['operator'] = '<=>'; break; case 'LIKE': $where['opwerator'] = 'LIKE'; break; case '>': $where['operator'] = '>'; break; case '>=': case '=>': $where['operator'] = '>='; break; case '<': $where['operator'] = '<'; break; case '<=': case '>=': $where['operator'] = '<='; break; case '!=': case '<>': $where['operator'] = '<>'; break; case 'IS NOT': $where['operator'] = 'IS NOT'; break; case 'IS NULL': $where['operator'] = 'IS NULL'; $noVal = true; break; case 'IS NOT NULL': $where['operator'] = 'IS NOT NULL'; $noVal = true; break; } if($noVal) { $whereClause .= ' ' . $where['operator']; } else { $whereClause .= ' ' . $where['operator'] . ' ' . self::valueToString($where['value']); } //$sql .= ' ' . $where['operator'] . ' :where' . $key; } } } $sql .= $whereClause; if(is_array($this->_query['sort'])) { foreach($this->_query['sort'] as $key => $sort) { if($key == 0) { $sql .= ' ORDER BY '; } $sql .= $sort . ', '; } $sql = substr($sql, 0, strlen($sql) - 2); } if($this->_query['limit']) { $sql .= ' ' . $this->_query['limit']; } } break; case self::TARGET_PROCEDURE: if($this->_procedure) { $sql .= 'CALL ' . $this->_procedure . '('; foreach($this->_query['params'] as $param => $value) { $sql .= $value . ', '; } $sql = substr($sql, 0, strlen($sql)-2) . ')'; } break; } if(!$sql) { $resultSet = new \Bedrock\Model\ResultSet(); } else { // Query Database \Bedrock\Common\Logger::info('Executing Query: "' . $sql . '"'); $results = $this->_connection->query($sql); if($results) { $results = $results->fetchAll(\PDO::FETCH_ASSOC); } else { $results = array(); } // Build Records foreach($results as $result) { \Bedrock\Common\Logger::info('Adding new record to ResultSet.'); $records[] = new \Bedrock\Model\Record($this->_table, $result); } // Build ResultSet $resultSet = new \Bedrock\Model\ResultSet($records); // Determine Count if($this->_query['limit']) { $countSql = 'SELECT COUNT(*) AS count FROM ' . $this->_table . ' ' . $whereClause; \Bedrock\Common\Logger::info('Executing Count Query: "' . $countSql . '"'); $countResult = $this->_connection->query($countSql)->fetch(\PDO::FETCH_ASSOC); $resultSet->setCountAll($countResult['count']); } else { $resultSet->setCountAll(count($records)); } \Bedrock\Common\Logger::info('Total Record Count: ' . $resultSet->countAll()); } \Bedrock\Common\Logger::info(array('ResultSet: "' . $sql . '" (' . $resultSet->count() . ')' , $resultSet), \Bedrock\Common\Logger::TYPE_TABLE); return $resultSet; } catch(\PDOException $ex) { \Bedrock\Common\Logger::exception($ex); throw new \Bedrock\Model\Query\Exception('A problem with the database connection was encountered.'); } catch(\Exception $ex) { \Bedrock\Common\Logger::exception($ex); throw new \Bedrock\Model\Query\Exception('The Query object could not execute the stored query.'); } }
/** * Redirects the client to the specified location. * * @param string $location the URL to which the client should be sent */ public static function redirect($location) { \Bedrock\Common\Logger::info('Redirecting: ' . $location); header('Location: ' . $location); }
/** * Exports all table data from the database to the specified location. * * @param integer $exportType the data type to use * @param string $exportLocation the location to which to export */ public function exportTableData($exportLocation, $exportType) { try { // Load Tables $this->load(); \Bedrock\Common\Logger::info('Exporting all table data for database "' . $this->_name . '" to location "' . $exportLocation . '" ...'); // Export Data foreach($this->_tables as $name => $table) { \Bedrock\Common\Logger::info('Exporting table "' . $name . '" ...'); $table->exportData($exportLocation, $exportType); } } catch(\Exception $ex) { \Bedrock\Common\Logger::exception($ex); } }
/** * Executes the specified method with the specified parameters. * * @param string $method a valid ImageMagick command-line utility * @param array $params an array of key-value pairs to use as parameters */ public static function exec($method, $params) { try { // Setup $command = ''; if(!self::$_methods[$method]) { throw new \Bedrock\Common\Image\Exception('Unknown method "' . $method . '" specified.'); } // Load Paths if(self::$_methods === null) { $root = \Bedrock\Common\Registry::get('config')->root->image; self::$_methods = array( 'animate' => $root . 'animate', 'compare' => $root . 'compare', 'composite' => $root . 'composite', 'conjure' => $root . 'conjure', 'convert' => $root . 'convert', 'display' => $root . 'display', 'identify' => $root . 'identify', 'import' => $root . 'import', 'mogrify' => $root . 'mogrify', 'montage' => $root . 'montage', 'stream' => $root . 'stream' ); } // Build Command $command = self::$_methods[$method]; foreach($params as $key => $value) { if(is_numeric($key)) { $command .= ' ' . $value; } else { $command .= ' -' . $key . ' ' . $value; } } $command = escapeshellcmd($command); \Bedrock\Common\Logger::info('Executing command: ' . $command); exec($command); } catch(\Exception $ex) { \Bedrock\Common\Logger::exception($ex); throw new \Bedrock\Common\Image\Exception('The method "' . $method . '" could not be executed.'); } }
/** * Delegates the request to the proper controller. */ public function delegate() { try { // Setup $route = ''; $parts = array(); $args = array(); // Find Route $route = (empty($_GET['route'])) ? '' : $_GET['route']; if(empty($route)) { $route = "index"; } \Bedrock\Common\Logger::info('Route: ' . $route); // Separate the route into parts. $route = trim($route, "/\\"); $parts = explode("/", $route); // Find Controller if($parts[0] == 'query' || substr($parts[0], 0, 6) == 'query:') { $controller = new \Bedrock\Control\Query(); $controller->index($parts); } else { // Handle Stored Special Cases $cases = \Bedrock\Common\Router\SpecialCases::retrieve(); $specialCase = null; foreach($cases as $case) { if(substr($route, 0, strlen($case['route'])) == $case['route']) { $controller = new $case['controller'](); $controller->{$case['method']}($parts); $specialCase = true; break; } } if(!$specialCase) { $parts = array_map('ucwords', $parts); $controller = null; while($parts) { // Check for ::index() method. array_push($parts, 'index'); $controller = self::getController($this->_delegationQueue, $parts); if($controller) break; // Check for specific method. array_pop($parts); $controller = self::getController($this->_delegationQueue, $parts); if($controller) break; // Otherwise continue loop. array_unshift($args, strtolower(array_shift($parts))); } $action = strtolower(array_pop($parts)); // Finally, delegate to a 404 error. if($controller === false) { \Bedrock\Common\Logger::error('No controller found using route: "' . $route . '"'); $this->delegateToError(); } elseif(!method_exists($controller, $action)) { \Bedrock\Common\Logger::error('No action found using route: "' . $route . '"'); $this->delegateToError(); } else { // Execute Controller Method $controller->$action($parts); } } } } catch(\Bedrock\Common\Router\Exception $ex) { \Bedrock\Common\Logger::exception($ex); $this->delegateToError(); } }