setRoot($mainRoot); $obj->setAction($action); $obj->setPath($file); $obj->setName($name); $obj->setInPlugin($isInPlugin); $obj->exec(); } public static function setController($controller) { self::$_controller = $controller; } /** * get current running controller object * * @return RController */ public static function controller() { return self::$_controller; } } /** * Controller parent class * */ class RController { private $_action; private $_path; private $_name; private $_inPlugin = false; /** * set current action name * * @param string $action action name */ public function setAction($action) { $this->_action = $action; } /** * Get action name * * @return string */ public function action() { return $this->_action; } public function setRoot($root) { $this->_root = $root; } public function root() { return $this->_root; } /** * Set controller file path * * @param string $path file path */ public function setPath($path) { $this->_path = $path; } /** * Set controller name * * @param string $name controller name */ public function setName($name) { $this->_name = $name; } /** * Get controller name * * @return string */ public function name() { return $this->_name; } /** * Set if the controller is in a plugin * * @param boolean $isInPlugin true or false */ public function setInPlugin($isInPlugin) { $this->_inPlugin = $isInPlugin; } /** * Call before actions * */ public function onBefore() { } /** * Call after actions * */ public function onAfter() { } /** * Execute action * */ public function exec() { Rock::setController($this); if (class_exists("RPlugin")) { RPlugin::callBefore(); } $this->onBefore(); $method = "do" . $this->_action; if (!method_exists($this, $method)) { trigger_error("can not find action '{$this->_action}' in class '" . get_class($this) . "'", E_USER_ERROR); } $ret = $this->$method(); if (is_object($ret) && ($ret instanceof RView)) { $ret->display(); } $this->onAfter(); if (class_exists("RPlugin")) { RPlugin::callAfter(); } } /** * Display View * * @param string $action action name, if not NULL, find view with this name */ public function display($action = null) { if (is_null($action)) { $action = $this->_action; } $view = null; if ($this->_inPlugin) { $view = dirname(dirname($this->_path)) . "/views/" . str_replace(".", "/", $this->_name) . "/{$action}.php"; } else { $view = dirname(__ROOT__) . DS . rock_theme_path() . "/views/" . str_replace(".", "/", $this->_name) . "/{$action}.php"; } if (is_file($view)) { extract(get_object_vars($this), EXTR_OVERWRITE); require($view); } } } /** * Model class * */ class RModel { } /** * View class * */ class RView { /** * Display view * */ public function display() { } } /** * print data to screen * * @param mixed $data1 data to be printed */ function p($data1 = null) { $args = func_get_args(); foreach ($args as $arg) { if (is_null($arg)) { $arg = "NULL"; } else if (is_bool($arg)) { $arg = $arg ? "TRUE" : "FALSE"; } echo "
* array(
* 11 => 12,
* 21 => 22
* );
*
*
* if $valueName not be set, the element value be "value":
*
*
* $array2 = rock_array_combine($array, "a");
*
* array(
* 11 => array("a" => 11, "b" => 12),
* 21 => array("a" => 21, "b" => 22)
* );
*
*
* support dot (.) operator in keyName and valueName:
* - rock_array_combine($array, "a.b", "a.c");
* $array[n][a][b] will be "key",$array[n][a][c] value be"value", here, n is index
*
* @param array $array array values to combine from
* @param integer|string $keyName key name
* @param integer|string $valueName value name
* @return array
* @since 1.0
*/
function rock_array_combine($array, $keyName, $valueName = null) {
$ret = array();
foreach ($array as $row) {
if (is_array($row)) {
$keyValue = rock_array_get($row, $keyName);
$value = is_null($valueName) ? $row : rock_array_get($row, $valueName);
if ($keyValue) {
$ret[$keyValue] = $value;
}
else {
$ret[] = $value;
}
}
}
return $ret;
}
/**
* Retrieve message from language file
*
* @param string $code message code
* @return mixed
*/
function rock_lang($code) {
if (!isset($GLOBALS["ROCK_LANGS"])) {
$file = __ROOT__ . "/langs/" . __LANG__ . "/message.php";
$message = array();
require($file);
if (isset($message) && is_array($message)) {
$GLOBALS["ROCK_LANGS"] = $message;
}
else {
$GLOBALS["ROCK_LANGS"] = array();
}
}
$ret = isset($GLOBALS["ROCK_LANGS"][$code]) ? $GLOBALS["ROCK_LANGS"][$code] : null;
if (is_null($ret)) {
require __ROOT__ . "/langs/en_us/message.php";
if (isset($message[$code])) {
$ret = $message[$code];
}
$GLOBALS["ROCK_LANGS"] = array_merge($message, $GLOBALS["ROCK_LANGS"]);
}
if (is_null($ret)) {
$ret = $code;
}
$args = func_get_args();
unset($args[0]);
if (empty($args)) {
return $ret;
}
return vsprintf($ret, $args);
}
/**
* Check RockMongo version
*
*/
function rock_check_version() {
global $MONGO;
if (!isset($MONGO["servers"][0]["host"])) {
return;
}
//version 1.0.x
foreach ($MONGO["servers"] as $index => $server) {
foreach($server as $param => $value) {
switch ($param) {
case "host":
$server["mongo_host"] = $value;
break;
case "sock":
$server["mongo_sock"] = $value;
break;
case "port":
$server["mongo_port"] = $value;
$server["mongo_name"] = $server["mongo_host"] . ":" . $server["mongo_port"];
break;
case "username":
$server["mongo_user"] = $value;
break;
case "password":
$server["mongo_pass"] = $value;
break;
case "auth_enabled":
if (!$value) {
$server["mongo_auth"] = false;
$server["control_auth"] = false;
}
break;
case "admins":
foreach ($value as $name => $pass) {
$server["control_users"][$name] = $pass;
}
break;
case "show_dbs":
$server["ui_only_dbs"] = $value;
break;
}
}
$MONGO["servers"][$index] = $server;
}
}
/**
* initialize language
*
*/
function rock_init_lang() {
if (isset($_COOKIE["ROCK_LANG"])) {
// Patched by synthomat
// as reported in CVE-2013-5107
if (preg_match("/^[a-z]{2}_[a-z]{2}$/", $_COOKIE["ROCK_LANG"])) {
define("__LANG__", $_COOKIE["ROCK_LANG"]);
} else {
define("__LANG__", "en_us");
}
return;
}
if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) {
$firstLang = "";
if (strstr($_SERVER["HTTP_ACCEPT_LANGUAGE"], ",")) {
list($firstLang) = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
}
else {
$firstLang = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
}
if ($firstLang) {
$firstLang = strtolower(str_replace("-", "_", $firstLang));
if (is_dir(__ROOT__ . DS . "langs" . DS . $firstLang)) {
define("__LANG__", $firstLang);
return;
}
}
}
if (!defined("__LANG__")) {
define("__LANG__", "en_us");
}
}
/**
* initialize plugins
*
*/
function rock_init_plugins() {
global $MONGO;
if (empty($MONGO["features"]["plugins"]) || strtolower($MONGO["features"]["plugins"]) != "on") {
return;
}
import("lib.core.RPlugin");
import("lib.core.REvent");
import("lib.core.RFilter");
RPlugin::load();
}
?>