Esempio n. 1
0
 /**
  * Dump the database
  *
  * @museDescription  Dumps the current site database into a file in the users home directory
  *
  * @return  void
  **/
 public function dump()
 {
     $tables = App::get('db')->getTableList();
     $prefix = App::get('db')->getPrefix();
     $excludes = [];
     $now = new Date();
     $exclude = '';
     $includes = (array) $this->arguments->getOpt('include-table', []);
     if (!$this->arguments->getOpt('all-tables')) {
         $this->output->addLine('Dumping database with all prefixed tables included');
         foreach ($tables as $table) {
             if (strpos($table, $prefix) !== 0 && !in_array(str_replace('#__', $prefix, $table), $includes)) {
                 $excludes[] = Config::get('db') . '.' . $table;
             } elseif (in_array(str_replace('#__', $prefix, $table), $includes)) {
                 $this->output->addLine('Also including `' . $table . '`');
             }
         }
         // Build exclude list string
         $exclude = '--ignore-table=' . implode(' --ignore-table=', $excludes);
     } else {
         $this->output->addLine('Dumping database with all tables included');
     }
     // Add save location option
     $home = getenv('HOME');
     $hostname = gethostname();
     $filename = tempnam($home, "{$hostname}.mysql.dump." . $now->format('Y.m.d') . ".sql.");
     // Build command
     $cmd = "mysqldump -u " . Config::get('user') . " -p'" . Config::get('password') . "' " . Config::get('db') . " --routines {$exclude} > {$filename}";
     exec($cmd);
     // Print out location of file
     $this->output->addLine('File saved to: ' . $filename, 'success');
 }
Esempio n. 2
0
 /**
  * Method to invoke new tools session
  *
  * @apiMethod GET
  * @apiUri    /tools/{tool}/invoke
  * @return    void
  */
 public function invokeTask()
 {
     //get the userid and attempt to load user profile
     $userid = App::get('authn')['user_id'];
     $result = User::getInstance($userid);
     //make sure we have a user
     if (!$result->get('id')) {
         throw new Exception(Lang::txt('Unable to find user.'), 404);
     }
     //get request vars
     $tool_name = Request::getVar('app', '');
     $tool_version = Request::getVar('version', 'default');
     //build application object
     $app = new stdClass();
     $app->name = trim(str_replace(':', '-', $tool_name));
     $app->version = $tool_version;
     $app->ip = $_SERVER["REMOTE_ADDR"];
     //check to make sure we have an app to invoke
     if (!$app->name) {
         $this->errorMessage(400, 'You Must Supply a Valid Tool Name to Invoke.');
         return;
     }
     //include needed tool libraries
     include_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'version.php';
     require_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'session.php';
     require_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'viewperm.php';
     //create database object
     $database = \App::get('db');
     //load the tool version
     $tv = new \Components\Tools\Tables\Version($database);
     switch ($app->version) {
         case 1:
         case 'default':
             $app->name = $tv->getCurrentVersionProperty($app->name, 'instance');
             break;
         case 'test':
         case 'dev':
             $app->name .= '_dev';
             break;
         default:
             $app->name .= '_r' . $app->version;
             break;
     }
     $app->toolname = $app->name;
     if ($parent = $tv->getToolname($app->name)) {
         $app->toolname = $parent;
     }
     // Check of the toolname has a revision indicator
     $r = substr(strrchr($app->name, '_'), 1);
     if (substr($r, 0, 1) != 'r' && substr($r, 0, 3) != 'dev') {
         $r = '';
     }
     // No version passed and no revision
     if ((!$app->version || $app->version == 'default') && !$r) {
         // Get the latest version
         $app->version = $tv->getCurrentVersionProperty($app->toolname, 'revision');
         $app->name = $app->toolname . '_r' . $app->version;
     }
     // Get the caption/session title
     $tv->loadFromInstance($app->name);
     $app->caption = stripslashes($tv->title);
     $app->title = stripslashes($tv->title);
     //make sure we have a valid tool
     if ($app->title == '' || $app->toolname == '') {
         throw new Exception(Lang::txt('The tool "%s" does not exist on the HUB.', $tool_name), 400);
     }
     //get tool access
     $toolAccess = \Components\Tools\Helpers\Utils::getToolAccess($app->name, $result->get('username'));
     //do we have access
     if ($toolAccess->valid != 1) {
         throw new Exception($toolAccess->error->message, 400);
     }
     // Log the launch attempt
     \Components\Tools\Helpers\Utils::recordToolUsage($app->toolname, $result->get('id'));
     // Get the middleware database
     $mwdb = \Components\Tools\Helpers\Utils::getMWDBO();
     // Find out how many sessions the user is running.
     $ms = new \Components\Tools\Models\Middleware\Session($mwdb);
     $jobs = $ms->getCount($result->get('username'));
     // Find out how many sessions the user is ALLOWED to run.
     include_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'preferences.php';
     $preferences = new \Components\Tools\Tables\Preferences($database);
     $preferences->loadByUser($result->get('uidNumber'));
     if (!$preferences || !$preferences->id) {
         include_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'sessionclass.php';
         $scls = new \Components\Tools\Tables\SessionClass($this->database);
         $default = $scls->find('one', array('alias' => 'default'));
         $preferences->user_id = $result->get('uidNumber');
         $preferences->class_id = $default->id;
         $preferences->jobs = $default->jobs ? $default->jobs : 3;
         $preferences->store();
     }
     $remain = $preferences->jobs - $jobs;
     //can we open another session
     if ($remain <= 0) {
         throw new Exception(Lang::txt('You are using all (%s) your available job slots.', $jobs), 401);
     }
     // Get plugins
     Plugin::import('mw', $app->name);
     // Trigger any events that need to be called before session invoke
     Event::trigger('mw.onBeforeSessionInvoke', array($app->toolname, $app->version));
     // We've passed all checks so let's actually start the session
     $status = \Components\Tools\Helpers\Utils::middleware("start user="******" ip=" . $app->ip . " app=" . $app->name . " version=" . $app->version, $output);
     //make sure we got a valid session back from the middleware
     if (!isset($output->session)) {
         throw new Exception(Lang::txt('There was a issue while trying to start the tool session. Please try again later.'), 500);
     }
     //set session output
     $app->sess = $output->session;
     // Trigger any events that need to be called after session invoke
     Event::trigger('mw.onAfterSessionInvoke', array($app->toolname, $app->version));
     // Get a count of the number of sessions of this specific tool
     $appcount = $ms->getCount($result->get('username'), $app->name);
     // Do we have more than one session of this tool?
     if ($appcount > 1) {
         // We do, so let's append a timestamp
         $app->caption .= ' (' . Date::format("g:i a") . ')';
     }
     // Save the changed caption
     $ms->load($app->sess);
     $ms->sessname = $app->caption;
     if (!$ms->store()) {
         throw new Exception(Lang::txt('There was a issue while trying to start the tool session. Please try again later.'), 500);
     }
     //add tool title to output
     //add session title to ouput
     $output->tool = $app->title;
     $output->session_title = $app->caption;
     $output->owner = 1;
     $output->readonly = 0;
     //return result
     if ($status) {
         $this->send($output);
     }
 }
Esempio n. 3
0
 /**
  * Method to get the field input markup.
  *
  * @return  string  The field input markup.
  */
 protected function getInput()
 {
     // Initialize some field attributes.
     $format = $this->element['format'] ? (string) $this->element['format'] : '%Y-%m-%d';
     // Build the attributes array.
     $attributes = array();
     if ($this->element['size']) {
         $attributes['size'] = (int) $this->element['size'];
     }
     if ($this->element['maxlength']) {
         $attributes['maxlength'] = (int) $this->element['maxlength'];
     }
     if ($this->element['class']) {
         $attributes['class'] = (string) $this->element['class'];
     }
     if ((string) $this->element['readonly'] == 'true') {
         $attributes['readonly'] = 'readonly';
     }
     if ((string) $this->element['disabled'] == 'true') {
         $attributes['disabled'] = 'disabled';
     }
     if ($this->element['onchange']) {
         $attributes['onchange'] = (string) $this->element['onchange'];
     }
     // Handle the special case for "now".
     if (strtoupper($this->value) == 'NOW') {
         $this->value = strftime($format);
     }
     // If a known filter is given use it.
     switch (strtoupper((string) $this->element['filter'])) {
         case 'SERVER_UTC':
             // Convert a date to UTC based on the server timezone.
             if (intval($this->value)) {
                 // Get a date object based on the correct timezone.
                 $date = new Date($this->value, 'UTC');
                 $date->setTimezone(new DateTimeZone(App::get('config')->get('offset')));
                 // Transform the date string.
                 $this->value = $date->format('Y-m-d H:i:s', true, false);
             }
             break;
         case 'USER_UTC':
             // Convert a date to UTC based on the user timezone.
             if (intval($this->value)) {
                 // Get a date object based on the correct timezone.
                 $date = new Date($this->value, 'UTC');
                 $date->setTimezone(new DateTimeZone(App::get('user')->getParam('timezone', App::get('config')->get('offset'))));
                 // Transform the date string.
                 $this->value = $date->format('Y-m-d H:i:s', true, false);
             }
             break;
     }
     return Input::calendar($this->value, $this->name, $this->id, $format, $attributes);
 }