/**
  * Optimize Database
  *
  * @http_method GET
  * @resource tools/
  *
  * @return application/json
  *
  * @author Nikita Rousseau
  */
 public function optimizeDB()
 {
     $tables = array();
     $dbh = Core_DBH::getDBH();
     // Get Database Handle
     // Apply =======================================================================
     try {
         $result = $dbh->query("SHOW TABLES");
         $tables[] = $result->fetchAll(PDO::FETCH_NUM);
         $tables = $tables[0];
         if (!empty($tables)) {
             foreach ($tables as $table) {
                 $table = $table[0];
                 if (strstr($table, DB_PREFIX)) {
                     $dbh->query("OPTIMIZE TABLE " . $table . ";");
                 }
             }
         }
     } catch (PDOException $e) {
         echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
         die;
     }
     // return a response and log ===================================================
     $logger = self::getLogger();
     if (!empty($data['errors'])) {
         $data['success'] = false;
         $logger->info('Failed to optimize database tables.');
     } else {
         $data['success'] = true;
         $logger->info('Optimized database tables.');
     }
     return array('response' => 'application/json', 'data' => json_encode($data));
 }
 /**
  * Update User Configuration
  *
  * @param string $username
  * @param string $password0
  * @param string $password1
  * @param string $email
  * @param string $language
  * @param optional string $firstname
  * @param optional string $lastname
  *
  * @author Nikita Rousseau
  */
 public function updateUserConfig($username, $password0, $password1, $email, $language, $firstname = '', $lastname = '')
 {
     $form = array('username' => $username, 'password0' => $password0, 'password1' => $password1, 'email' => $email, 'language' => $language);
     $errors = array();
     // array to hold validation errors
     $data = array();
     // array to pass back data
     $dbh = Core_DBH::getDBH();
     // Get Database Handle
     // Get languages
     $languages = parse_ini_file(CONF_LANG_INI);
     $languages = array_flip(array_values($languages));
     // validate the variables ======================================================
     $v = new Valitron\Validator($form);
     $rules = ['required' => [['username'], ['password0'], ['password1'], ['email'], ['language']], 'alphaNum' => [['username']], 'lengthMin' => [['username', 4], ['password0', 8]], 'equals' => [['password0', 'password1']], 'email' => [['email']], 'in' => [['language', $languages]]];
     $labels = array('username' => 'Username', 'password0' => 'Password', 'password1' => 'Confirmation Password', 'email' => 'Email', 'language' => 'Language');
     $v->rules($rules);
     $v->labels($labels);
     $v->validate();
     $errors = $v->errors();
     // Apply the form ==============================================================
     if (empty($errors)) {
         // Database update
         $db_data['username'] = $form['username'];
         $db_data['password'] = Core_AuthService::getHash($form['password0']);
         $db_data['email'] = $form['email'];
         $db_data['lang'] = $form['language'];
         if (!empty($firstname)) {
             $db_data['firstname'] = $firstname;
         }
         if (!empty($lastname)) {
             $db_data['lastname'] = $lastname;
         }
         $authService = Core_AuthService::getAuthService();
         $uid = Core_AuthService::getSessionInfo('ID');
         foreach ($db_data as $key => $value) {
             $sth = $dbh->prepare("\tUPDATE " . DB_PREFIX . "user\n\t\t\t\t\t\t\t\t\t\tSET " . $key . " = :" . $key . "\n\t\t\t\t\t\t\t\t\t\tWHERE user_id = '" . $uid . "';");
             $sth->bindParam(':' . $key, $value);
             $sth->execute();
         }
         // Reload Session
         $authService->rmSessionInfo();
         $authService->setSessionInfo($uid, $db_data['username'], $db_data['firstname'], $db_data['lastname'], $db_data['lang'], BGP_USER_TEMPLATE);
         $authService->setSessionPerms();
         $this->rmCookie('LANG');
     }
     // return a response ===========================================================
     // response if there are errors
     if (!empty($errors)) {
         // if there are items in our errors array, return those errors
         $data['success'] = false;
         $data['errors'] = $errors;
         $data['msgType'] = 'warning';
         $data['msg'] = T_('Bad Settings!');
     } else {
         $data['success'] = true;
     }
     // return all our data to an AJAX call
     return $data;
 }
Exemple #3
0
 public static function checkRemoteAPIUser($remote_ip, $api_user, $api_user_pass)
 {
     $username = $api_user;
     $password = Core_AuthService::getHash($api_user_pass);
     $dbh = Core_DBH::getDBH();
     try {
         $sth = $dbh->prepare("\n\t\t\t\tSELECT user_id\n\t\t\t\tFROM " . DB_PREFIX . "user\n\t\t\t\tWHERE\n\t\t\t\t\tusername = :username AND\n\t\t\t\t\tpassword = :password AND\n\t\t\t\t\tstatus = 'Active'\n\t\t\t\t;");
         $sth->bindParam(':username', $username);
         $sth->bindParam(':password', $password);
         $sth->execute();
         $result = $sth->fetchAll(PDO::FETCH_ASSOC);
     } catch (PDOException $e) {
         echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
         die;
     }
     if (!empty($result)) {
         $user_id = $result[0]['user_id'];
         // NIST Level 2 Standard Role Based Access Control Library
         $rbac = new PhpRbac\Rbac();
         // Verify API Role
         if ($rbac->Users->hasRole('api', $user_id)) {
             // Update User Activity
             try {
                 $sth = $dbh->prepare("\n\t\t\t\t\t\tUPDATE " . DB_PREFIX . "user\n\t\t\t\t\t\tSET\n\t\t\t\t\t\t\tlast_login\t\t= :last_login,\n\t\t\t\t\t\t\tlast_activity\t= :last_activity,\n\t\t\t\t\t\t\tlast_ip \t\t= :last_ip,\n\t\t\t\t\t\t\tlast_host\t\t= :last_host\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tuser_id\t\t\t= :user_id\n\t\t\t\t\t\t;");
                 $last_login = date('Y-m-d H:i:s');
                 $last_activity = date('Y-m-d H:i:s');
                 $last_host = gethostbyaddr($remote_ip);
                 $sth->bindParam(':last_login', $last_login);
                 $sth->bindParam(':last_activity', $last_activity);
                 $sth->bindParam(':last_ip', $remote_ip);
                 $sth->bindParam(':last_host', $last_host);
                 $sth->bindParam(':user_id', $user_id);
                 $sth->execute();
             } catch (PDOException $e) {
                 echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
                 die;
             }
             // Update $_SERVER
             $_SERVER['PHP_AUTH_USER'] = $user_id;
             return TRUE;
         }
     }
     return FALSE;
 }
Exemple #4
0
 public static function getDBH()
 {
     if (empty(self::$dbh) || !is_object(self::$dbh) || get_class(self::$dbh) != 'PDO') {
         try {
             // Connect to the SQL server
             if (DB_DRIVER == 'sqlite') {
                 self::$dbh = new PDO(DB_DRIVER . ':' . DB_FILE);
             } else {
                 self::$dbh = new PDO(DB_DRIVER . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASSWORD);
             }
             // Set ERRORMODE to exceptions
             self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         } catch (PDOException $e) {
             echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
             die;
         }
     }
     return self::$dbh;
 }
Exemple #5
0
require MODS_DIR . '/' . basename(__DIR__) . '/box.class.php';
$module = new BGP_Module_Box();
/**
 * Call GUI Builder
 */
$gui = new Core_GUI($module);
/**
 * Javascript Generator
 */
$js = new Core_GUI_JS($module);
/**
 * Build Page Header
 */
$gui->getHeader();
// DB
$dbh = Core_DBH::getDBH();
// Get Database Handle
$rows = array();
$sth = $dbh->prepare("\n\tSELECT *\n\tFROM " . DB_PREFIX . "box\n\t;");
$sth->execute();
$rows = $sth->fetchAll(PDO::FETCH_ASSOC);
/**
 * PAGE BODY
 */
//------------------------------------------------------------------------------------------------------------+
?>
					<!-- CONTENTS -->

					<div style="max-width: 400px; margin: 0 auto 10px; padding-left: 35px; padding-right: 35px;">
						<div class="row">
							<div class="text-center">
Exemple #6
0
 /**
  * Check If The Current Session Is Legit
  *
  * @param none
  * @return bool
  * @access public
  */
 public function getSessionValidity()
 {
     if (!empty($this->username)) {
         $credentials = $this->decryptSessionCredentials();
         // Level 1
         if ($credentials['username'] == $this->username && $credentials['key'] == $this->auth_key && $credentials['token'] == session_id()) {
             // Level 2
             $dbh = Core_DBH::getDBH();
             // Fetch information from the database
             $sth = $dbh->prepare("\n\t\t\t\t\tSELECT username, last_ip, token\n\t\t\t\t\tFROM " . DB_PREFIX . "user\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tuser_id = :user_id\n\t\t\t\t\t;");
             $sth->bindParam(':user_id', $this->session['INFORMATION']['id']);
             $sth->execute();
             $userResult = $sth->fetchAll(PDO::FETCH_ASSOC);
             // Verify
             if ($userResult[0]['username'] == $this->username && $userResult[0]['last_ip'] == $_SERVER['REMOTE_ADDR'] && $userResult[0]['token'] == session_id()) {
                 // Update User Activity on page request
                 $last_activity = date('Y-m-d H:i:s');
                 $sth = $dbh->prepare("\n\t\t\t\t\t\tUPDATE " . DB_PREFIX . "user\n\t\t\t\t\t\tSET\n\t\t\t\t\t\t\tlast_activity\t= :last_activity\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tuser_id\t\t\t= :user_id\n\t\t\t\t\t\t;");
                 $uid = Core_AuthService::getSessionInfo('ID');
                 $sth->bindParam(':last_activity', $last_activity);
                 $sth->bindParam(':user_id', $uid);
                 $sth->execute();
                 return TRUE;
             } else {
                 return FALSE;
             }
         }
     }
     return FALSE;
 }
 /**
  * User Password Renewal
  *
  * @param string $username
  * @param string $email
  * @param optional bool $captcha_validation
  *
  * @author Nikita Rousseau
  */
 public function sendNewPassword($username, $email, $captcha_validation = TRUE)
 {
     $form = array('username' => $username, 'email' => $email);
     $errors = array();
     // array to hold validation errors
     $data = array();
     // array to pass back data
     $dbh = Core_DBH::getDBH();
     // Get Database Handle
     // validate the variables ======================================================
     $v = new Valitron\Validator($form);
     $rules = ['required' => [['username'], ['email']], 'alphaNum' => [['username']], 'email' => [['email']]];
     $v->rules($rules);
     $v->validate();
     $errors = $v->errors();
     // Verify the form =============================================================
     if (empty($errors)) {
         $username = $form['username'];
         $email = $form['email'];
         try {
             $sth = $dbh->prepare("\n\t\t\t\t\tSELECT user_id, email\n\t\t\t\t\tFROM " . DB_PREFIX . "user\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tusername = :username AND\n\t\t\t\t\t\temail \t = :email AND\n\t\t\t\t\t\tstatus   = 'active'\n\t\t\t\t\t;");
             $sth->bindParam(':username', $username);
             $sth->bindParam(':email', $email);
             $sth->execute();
             $result = $sth->fetchAll();
         } catch (PDOException $e) {
             echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
             die;
         }
         if (!empty($result) && $captcha_validation == TRUE) {
             $authService = Core_AuthService::getAuthService();
             // Reset Login Attempts
             $authService->rsSecCount();
             // Reset User Passwd
             $plainTextPasswd = bgp_create_random_password(13);
             $digestPasswd = Core_AuthService::getHash($plainTextPasswd);
             try {
                 // Update User Passwd
                 $sth = $dbh->prepare("\n\t\t\t\t\t\tUPDATE " . DB_PREFIX . "user\n\t\t\t\t\t\tSET\n\t\t\t\t\t\t\tpassword \t= :password\n\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\tuser_id\t\t= :user_id\n\t\t\t\t\t\t;");
                 $sth->bindParam(':password', $digestPasswd);
                 $sth->bindParam(':user_id', $result[0]['user_id']);
                 $sth->execute();
             } catch (PDOException $e) {
                 echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
                 die;
             }
             // Send Email
             $to = htmlentities($result[0]['email'], ENT_QUOTES);
             $subject = T_('Reset Password');
             $message = T_('Your password has been reset to:');
             $message .= "<br /><br />" . $plainTextPasswd . "<br /><br />";
             $message .= T_('With IP') . ': ';
             $message .= $_SERVER['REMOTE_ADDR'];
             $headers = 'MIME-Version: 1.0' . "\r\n";
             $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
             $headers .= 'From: Bright Game Panel System <root@' . $_SERVER['SERVER_NAME'] . '>' . "\r\n";
             $headers .= 'X-Mailer: PHP/' . phpversion();
             $mail = mail($to, $subject, $message, $headers);
             // Log Event
             $logger = self::getLogger();
             $logger->info('Password reset.');
         } else {
             // Call security component
             $authService = Core_AuthService::getAuthService();
             $authService->incrementSecCount();
             // Log Event
             $logger = self::getLogger();
             $logger->info('Bad password reset.');
             // Messages
             if (empty($result)) {
                 $errors['username'] = T_('Wrong information.');
                 $errors['email'] = T_('Wrong information.');
             }
             if ($captcha_validation == FALSE) {
                 $errors['captcha'] = T_('Wrong CAPTCHA Code.');
             }
         }
     }
     // return a response ===========================================================
     // response if there are errors
     if (!empty($errors)) {
         // if there are items in our errors array, return those errors
         $data['success'] = false;
         $data['errors'] = $errors;
         // notification
         $authService = Core_AuthService::getAuthService();
         if ($authService->isBanned()) {
             $data['msgType'] = 'warning';
             $data['msg'] = T_('You have been banned') . ' ' . CONF_SEC_BAN_DURATION . ' ' . T_('seconds!');
         } else {
             $data['msgType'] = 'warning';
             $data['msg'] = T_('Invalid information provided!');
         }
     } else {
         if (!$mail) {
             // mail delivery error
             $data['success'] = false;
             // notification
             $data['msgType'] = 'danger';
             $data['msg'] = T_('An error has occured while sending the email. Contact your system administrator.');
         } else {
             $data['success'] = true;
         }
     }
     // return all our data to an AJAX call
     return $data;
 }
 public function open($save_path = "", $name = "PHPSESSID")
 {
     $this->dbh = Core_DBH::getDBH();
     return TRUE;
 }
 /**
  * Add a New Box To The Collection
  *
  * @http_method POST
  * @resource box/
  *
  * @param string $name query
  * @param string $os query
  * @param string $ip query
  * @param string $port query
  * @param string $login query
  * @param string $password query
  * @param optional string $userPath
  * @param optional string $steamPath
  * @param optional string $notes
  *
  * @return application/json
  *
  * @author Nikita Rousseau
  */
 function postBox($name, $os, $ip, $port, $login, $password, $userPath = '', $steamPath = '', $notes = '')
 {
     $args = array('name' => $name, 'os' => $os, 'ip' => $ip, 'port' => $port, 'login' => $login, 'password' => $password, 'userPath' => $userPath, 'steamPath' => $steamPath, 'notes' => $notes);
     $errors = array();
     // array to hold validation errors
     $data = array();
     // array to pass back data
     $dbh = Core_DBH::getDBH();
     // Get Database Handle
     // validate the variables ======================================================
     $v = new Valitron\Validator($args);
     $rules = ['required' => [['name'], ['os'], ['ip'], ['port'], ['login'], ['password']], 'regex' => [['name', "/^([-a-z0-9_ -])+\$/i"]], 'integer' => [['os'], ['port']], 'ip' => [['ip']], 'alphaNum' => [['login']]];
     $labels = array('name' => T_('Remote Machine Name'), 'os' => T_('Operating System'), 'ip' => T_('IP Address'), 'port' => T_('Port'), 'login' => T_('Login'), 'password' => T_('Password'));
     $v->rules($rules);
     $v->labels($labels);
     $v->validate();
     $errors = $v->errors();
     // validate the variables phase 2 ==============================================
     if (empty($errors)) {
         // Verify OS ID
         try {
             $sth = $dbh->prepare("\n\t\t\t\t\tSELECT operating_system\n\t\t\t\t\tFROM " . DB_PREFIX . "os\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tos_id = :os_id\n\t\t\t\t\t;");
             $sth->bindParam(':os_id', $args['os']);
             $sth->execute();
             $result = $sth->fetchAll(PDO::FETCH_ASSOC);
         } catch (PDOException $e) {
             echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
             die;
         }
         if (empty($result[0])) {
             $errors['os'] = 'Bad Identifier';
         }
         // Verify Communication
         $socket = @fsockopen($args['ip'], $args['port'], $errno, $errstr, 3);
         if ($socket === FALSE) {
             $errors['com'] = "Unable to connect to " . $args['ip'] . " on port " . $args['port'] . ". " . utf8_encode($errstr) . " ( {$errno} )";
             unset($socket);
         } else {
             unset($socket);
             $ssh = new Net_SSH2($args['ip'], $args['port']);
             if (!$ssh->login($args['login'], $args['password'])) {
                 $errors['com'] = 'Login failed';
             } else {
                 // Verify Remote Paths
                 if (!empty($args['userPath'])) {
                     if (boolval(trim($ssh->exec('test -d ' . escapeshellcmd($args['userPath']) . " && echo '1' || echo '0'"))) === FALSE) {
                         $errors['remoteUserHome'] = 'Invalid path. Must be an absolute or full path';
                     }
                 }
                 if (!empty($args['steamPath'])) {
                     if (boolval(trim($ssh->exec('test -f ' . escapeshellcmd($args['steamPath']) . " && echo '1' || echo '0'"))) === FALSE) {
                         $errors['steamcmd'] = 'SteamCMD not found. Must be an absolute or full path';
                     }
                 }
             }
             $ssh->disconnect();
         }
     }
     // Apply =======================================================================
     if (empty($errors)) {
         //
         // Database update
         //
         // Vars Init
         if (empty($args['userPath'])) {
             $home = "~";
             $args['userPath'] = $home;
         } else {
             $home = escapeshellcmd(normalizePath($args['userPath']));
             $args['userPath'] = $home;
         }
         $config = parse_ini_file(CONF_SECRET_INI);
         // BOX
         try {
             $sth = $dbh->prepare("\n\t\t\t\t\tINSERT INTO " . DB_PREFIX . "box\n\t\t\t\t\tSET\n\t\t\t\t\t\tos_id \t\t\t= :os,\n\t\t\t\t\t\tname \t\t\t= :name,\n\t\t\t\t\t\tsteam_lib_path \t= :steamcmd,\n\t\t\t\t\t\tnotes \t\t\t= :notes\n\t\t\t\t\t;");
             $sth->bindParam(':os', $args['os']);
             $sth->bindParam(':name', $args['name']);
             $sth->bindParam(':steamcmd', $args['steamPath']);
             $sth->bindParam(':notes', $args['notes']);
             $sth->execute();
             $box_id = $dbh->lastInsertId();
         } catch (PDOException $e) {
             echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
             die;
         }
         // IP
         try {
             $sth = $dbh->prepare("\n\t\t\t\t\tINSERT INTO " . DB_PREFIX . "box_ip\n\t\t\t\t\tSET\n\t\t\t\t\t\tbox_id = :box_id,\n\t\t\t\t\t\tip = :ip,\n\t\t\t\t\t\tis_default = 1\n\t\t\t\t\t;");
             $sth->bindParam(':box_id', $box_id);
             $sth->bindParam(':ip', $args['ip']);
             $sth->execute();
         } catch (PDOException $e) {
             echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
             die;
         }
         // CREDENTIALS
         // Phase 1
         // Connect to the remote host
         // Try to append our public key to authorized_keys
         $ssh = new Net_SSH2($args['ip'], $args['port']);
         $ssh->login($args['login'], $args['password']);
         $remote_keys = $ssh->exec('cat ' . $home . '/.ssh/authorized_keys');
         // Check if the public key already exists
         if (strpos($remote_keys, file_get_contents(RSA_PUBLIC_KEY_FILE)) === FALSE) {
             // Otherwise, append it
             $ssh->exec("echo '" . file_get_contents(RSA_PUBLIC_KEY_FILE) . "' >> " . $home . "/.ssh/authorized_keys");
         }
         // Phase 2
         // Verify that the public key is allowed on the remote host
         $isUsingSSHPubKey = TRUE;
         // By default, we use the SSH authentication keys method
         $remote_keys = $ssh->exec('cat ' . $home . '/.ssh/authorized_keys');
         $ssh->disconnect();
         if (strpos($remote_keys, file_get_contents(RSA_PUBLIC_KEY_FILE)) === FALSE) {
             // authorized_keys is not writable
             // Use compatibility mode
             // Store the password in DB
             $isUsingSSHPubKey = FALSE;
         } else {
             // Phase 3
             // Try to connect with our private key on the remote host
             $ssh = new Net_SSH2($args['ip'], $args['port']);
             $key = new Crypt_RSA();
             $key->loadKey(file_get_contents(RSA_PRIVATE_KEY_FILE));
             if (!$ssh->login($args['login'], $key)) {
                 // Authentication failed
                 // Use compatibility mode
                 // Store the password in DB
                 $isUsingSSHPubKey = FALSE;
             }
             $ssh->disconnect();
         }
         // SSH CREDENTIALS
         $cipher = new Crypt_AES(CRYPT_AES_MODE_ECB);
         $cipher->setKeyLength(256);
         $cipher->setKey($config['APP_SSH_KEY']);
         if ($isUsingSSHPubKey) {
             try {
                 $sth = $dbh->prepare("\n\t\t\t\t\t\tINSERT INTO " . DB_PREFIX . "box_credential\n\t\t\t\t\t\tSET\n\t\t\t\t\t\t\tlogin = :login,\n\t\t\t\t\t\t\tremote_user_home = :home,\n\t\t\t\t\t\t\tcom_protocol = 'ssh2',\n\t\t\t\t\t\t\tcom_port = :com_port\n\t\t\t\t\t\t;");
                 $login = $cipher->encrypt($args['login']);
                 $sth->bindParam(':login', $login);
                 $sth->bindParam(':home', $args['userPath']);
                 $sth->bindParam(':com_port', $args['port']);
                 $sth->execute();
                 $credential_id = $dbh->lastInsertId();
             } catch (PDOException $e) {
                 echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
                 die;
             }
         } else {
             try {
                 $sth = $dbh->prepare("\n\t\t\t\t\t\tINSERT INTO " . DB_PREFIX . "box_credential\n\t\t\t\t\t\tSET\n\t\t\t\t\t\t\tlogin = :login,\n\t\t\t\t\t\t\tpassword = :password,\n\t\t\t\t\t\t\tremote_user_home = :home,\n\t\t\t\t\t\t\tcom_protocol = 'ssh2',\n\t\t\t\t\t\t\tcom_port = :port\n\t\t\t\t\t\t;");
                 $login = $cipher->encrypt($args['login']);
                 $password = $cipher->encrypt($args['password']);
                 $sth->bindParam(':login', $login);
                 $sth->bindParam(':password', $password);
                 $sth->bindParam(':home', $args['userPath']);
                 $sth->bindParam(':com_port', $args['port']);
                 $sth->execute();
                 $credential_id = $dbh->lastInsertId();
             } catch (PDOException $e) {
                 echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
                 die;
             }
         }
         // UPDATE BOX
         try {
             $sth = $dbh->prepare("\n\t\t\t\t\tUPDATE " . DB_PREFIX . "box\n\t\t\t\t\tSET\n\t\t\t\t\t\tbox_credential_id = :box_credential_id\n\t\t\t\t\tWHERE box_id = :box_id\n\t\t\t\t\t;");
             $sth->bindParam(':box_credential_id', $credential_id);
             $sth->bindParam(':box_id', $box_id);
             $sth->execute();
         } catch (PDOException $e) {
             echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
             die;
         }
     }
     // return a response and log ===================================================
     $logger = self::getLogger();
     $data['errors'] = $errors;
     if (!empty($data['errors'])) {
         $data['success'] = false;
         $logger->info('Failed to add box.');
     } else {
         $data['success'] = true;
         $logger->info('Box added.');
     }
     return array('response' => 'application/json', 'data' => json_encode($data));
 }
 /**
  * Update A System Configuration By Element
  *
  * @http_method PUT
  * @resource config/setting
  *
  * @param string $setting query
  * @param string $value query
  *
  * @return application/json
  *
  * @author Nikita Rousseau
  */
 public function updateSysConfigSetting($setting, $value)
 {
     $errors = array();
     // array to hold validation errors
     $data = array();
     // array to pass back data
     $dbh = Core_DBH::getDBH();
     // Get Database Handle
     // Get templates
     $templates = parse_ini_file(CONF_TEMPLATES_INI);
     $templates = array_flip(array_values($templates));
     // validate the variables ======================================================
     $v = new Valitron\Validator(array($setting => $value));
     switch ($setting) {
         case 'panel_name':
             $v->rule('regex', 'panel_name', "/^([-a-z0-9_ -])+\$/i");
             $v->labels(array('panel_name' => 'Panel Name'));
             break;
         case 'system_url':
             $v->rule('url', 'system_url');
             $v->labels(array('system_url' => 'Panel URL'));
             break;
         case 'user_template':
             $v->rule('in', 'user_template', $templates);
             $v->labels(array('user_template' => 'User Template'));
             break;
         case 'maintenance_mode':
             // No validation
             break;
         default:
             $errors[$setting] = T_('Unknown Setting!');
             break;
     }
     $v->validate();
     if (empty($errors)) {
         $errors = $v->errors();
     }
     // Apply =======================================================================
     if (empty($errors)) {
         // Database update
         $db_data[$setting] = $value;
         if (!empty($db_data['maintenance_mode'])) {
             if ($db_data['maintenance_mode'] == 'true') {
                 $db_data['maintenance_mode'] = '1';
             } else {
                 $db_data['maintenance_mode'] = '0';
             }
         }
         foreach ($db_data as $key => $value) {
             try {
                 $sth = $dbh->prepare("UPDATE " . DB_PREFIX . "config SET value = :" . $key . " WHERE setting = '" . $key . "';");
                 $sth->bindParam(':' . $key, $value);
                 $sth->execute();
             } catch (PDOException $e) {
                 echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
                 die;
             }
         }
     }
     // return a response and log ===================================================
     $logger = self::getLogger();
     $data['errors'] = $errors;
     if (!empty($data['errors'])) {
         $data['success'] = false;
         $logger->info('Failed to update system configuration setting.');
     } else {
         $data['success'] = true;
         $logger->info('Updated system configuration setting.');
     }
     return array('response' => 'application/json', 'data' => json_encode($data));
 }
Exemple #11
0
<?php

require_once __DIR__ . '/core/Jf.php';
require_once __DIR__ . '/Rbac.php';
Jf::$Db = Core_DBH::getDBH();