Exemplo n.º 1
0
 /**
  * @param string $name command name (case-insensitive)
  *
  * @return \CConsoleCommand The command object. Null if the name is invalid.
  */
 public function createCommand($name)
 {
     $name = StringHelper::toLowerCase($name);
     $command = null;
     if (isset($this->commands[$name])) {
         $command = $this->commands[$name];
     } else {
         $commands = array_change_key_case($this->commands);
         if (isset($commands[$name])) {
             $command = $commands[$name];
         }
     }
     if ($command !== null) {
         if (is_string($command)) {
             $className = 'NerdsAndCompany\\Schematic\\ConsoleCommands\\' . IOHelper::getFileName($command, false);
             return new $className($name, $this);
         } else {
             // an array configuration
             return Craft::createComponent($command, $name, $this);
         }
     } elseif ($name === 'help') {
         return new \CHelpCommand('help', $this);
     } else {
         return;
     }
 }
 public function login($socialUserId)
 {
     $rememberMe = true;
     Craft::log(__METHOD__, LogLevel::Info, true);
     $this->_identity = new TokenIdentity($socialUserId);
     $this->_identity->authenticate();
     // Was the login successful?
     if ($this->_identity->errorCode == TokenIdentity::ERROR_NONE) {
         // Get how long this session is supposed to last.
         $this->authTimeout = craft()->config->getUserSessionDuration($rememberMe);
         $id = $this->_identity->getId();
         $user = craft()->users->getUserById($id);
         $states = $this->_identity->getPersistentStates();
         // Run any before login logic.
         if ($this->beforeLogin($id, $states, false)) {
             $this->changeIdentity($id, $this->_identity->getName(), $states);
             if ($this->authTimeout) {
                 if ($this->allowAutoLogin) {
                     if ($user) {
                         // Save the necessary info to the identity cookie.
                         $sessionToken = StringHelper::UUID();
                         $hashedToken = craft()->security->hashData(base64_encode(serialize($sessionToken)));
                         $uid = craft()->users->handleSuccessfulLogin($user, $hashedToken);
                         $data = array($this->getName(), $sessionToken, $uid, $rememberMe ? 1 : 0, craft()->request->getUserAgent(), $this->saveIdentityStates());
                         $this->saveCookie('', $data, $this->authTimeout);
                     } else {
                         throw new Exception(Craft::t('Could not find a user with Id of {userId}.', array('{userId}' => $this->getId())));
                     }
                 } else {
                     throw new Exception(Craft::t('{class}.allowAutoLogin must be set true in order to use cookie-based authentication.', array('{class}' => get_class($this))));
                 }
             }
             // Run any after login logic.
             $this->afterLogin(false);
         }
         return !$this->getIsGuest();
     }
     Craft::log('Tried to log in unsuccessfully.', LogLevel::Warning);
     return false;
 }
Exemplo n.º 3
0
 /**
  * Import the database dump by running mysql from the command line (assumes Bash?)
  *
  * @param BaseModel $settings The task's settings
  * @param mixed $arg An optional second argument
  *
  * @return bool
  */
 public function importBackup($settings, $arg = null)
 {
     $keepAfterCopy = $this->getSettings()['importBackup']['keepBackup'] === '1' ? true : false;
     $pathToBackup = craft()->path->getDbBackupPath() . StringHelper::toLowerCase(IOHelper::cleanFilename($settings->backupFileName));
     $destination = $this->getSettings()['copyBackup']['destination'];
     $destination = rtrim($destination, '/') . '/';
     $destination = $destination . basename($pathToBackup);
     $sshHostname = $this->getSettings()['ssh']['remote']['hostname'];
     $sshUsername = $this->getSettings()['ssh']['remote']['username'];
     $sshPassword = craft()->goLive_security->decrypt($this->getSettings()['ssh']['remote']['password']);
     $mysqlHostname = $this->getSettings()['mysql']['hostname'];
     $mysqlUsername = $this->getSettings()['mysql']['username'];
     $mysqlPassword = craft()->goLive_security->decrypt($this->getSettings()['mysql']['password']);
     $mysqlDb = $this->getSettings()['mysql']['dbname'];
     $ssh = new Net_SSH2($sshHostname);
     $ssh->login($sshUsername, $sshPassword);
     $commands = array(array('command' => sprintf('mysql -h %s -u %s  -p%s %s < %s', $mysqlHostname, $mysqlUsername, $mysqlPassword, $mysqlDb, $destination), 'output' => ''));
     if (!$keepAfterCopy) {
         array_push($commands, array('command' => sprintf('rm %s', $destination), 'output' => ''));
     }
     for ($i = 0; $i < count($commands); $i++) {
         $commands[$i]['output'] = $ssh->exec($commands[$i]['command']);
     }
     return true;
 }
 /**
  * Determines if there is enough memory to process this image.
  *
  * The code was adapted from http://www.php.net/manual/en/function.imagecreatefromjpeg.php#64155. It will first
  * attempt to do it with available memory. If that fails, Craft will bump the memory to amount defined by the
  * [phpMaxMemoryLimit](http://buildwithcraft.com/docs/config-settings#phpMaxMemoryLimit) config setting, then try again.
  *
  * @param string $filePath The path to the image file.
  * @param bool   $toTheMax If set to true, will set the PHP memory to the config setting phpMaxMemoryLimit.
  *
  * @return bool
  */
 public function checkMemoryForImage($filePath, $toTheMax = false)
 {
     if (StringHelper::toLowerCase(IOHelper::getExtension($filePath)) == 'svg') {
         return true;
     }
     if (!function_exists('memory_get_usage')) {
         return false;
     }
     if ($toTheMax) {
         // Turn it up to 11.
         craft()->config->maxPowerCaptain();
     }
     // Find out how much memory this image is going to need.
     $imageInfo = getimagesize($filePath);
     $K64 = 65536;
     $tweakFactor = 1.7;
     $bits = isset($imageInfo['bits']) ? $imageInfo['bits'] : 8;
     $channels = isset($imageInfo['channels']) ? $imageInfo['channels'] : 4;
     $memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $bits * $channels / 8 + $K64) * $tweakFactor);
     $memoryLimit = AppHelper::getPhpConfigValueInBytes('memory_limit');
     if (memory_get_usage() + $memoryNeeded < $memoryLimit) {
         return true;
     }
     if (!$toTheMax) {
         return $this->checkMemoryForImage($filePath, true);
     }
     // Oh well, we tried.
     return false;
 }