예제 #1
0
 public function init()
 {
     /*
      * If no users are installed then go to the installer page.
      */
     $allUsers = Model_DbTable_User::getAllUsers();
     if (count($allUsers) < 1) {
         $this->_helper->_redirector->goToRouteAndExit(array('controller' => 'install', 'action' => 'index'));
     }
     /*
      * Handle the current user and login redirection.
      * All pages except /user/login are redirected to /user/login if the
      * user is not logged in.
      */
     $currentUser = new Zend_Session_Namespace('currentUser');
     $requestParameters = $this->getRequest()->getParams();
     if ($requestParameters['controller'] != 'user' && $requestParameters['action'] != 'login') {
         if ($currentUser->username == null) {
             $this->_helper->_redirector->goToRouteAndExit(array('controller' => 'user', 'action' => 'login'));
         } else {
             $this->view->currentUser = $currentUser;
             SoftLayer_SoapClient::setAuthenticationUser($currentUser->username, $currentUser->apiKey);
         }
     }
     /*
      * Set common view elements.
      */
     $this->view->translate = Zend_Registry::get('Zend_Translate');
     $this->view->baseUrl = $this->getRequest()->getBaseUrl();
 }
예제 #2
0
 /**
  * Create a SoftLayer API SOAP Client
  *
  * Retrieve a new SoftLayer_SoapClient object for a specific SoftLayer API
  * service using either the class' constants API_USER and API_KEY or a
  * custom username and API key for authentication. Provide an optional id
  * value if you wish to instantiate a particular SoftLayer API object.
  *
  * @param string $serviceName The name of the SoftLayer API service you wish to query
  * @param int $id An optional object id if you're instantiating a particular SoftLayer API object. Setting an id defines this client's initialization parameter header.
  * @param string $username An optional API username if you wish to bypass SoftLayer_SoapClient's built-in username.
  * @param string $username An optional API key if you wish to bypass SoftLayer_SoapClient's built-in API key.
  * @return SoftLayer_SoapClient
  */
 public static function getClient($serviceName, $id = null, $username = null, $apiKey = null)
 {
     $serviceName = trim($serviceName);
     if ($serviceName == null) {
         throw new Exception('Please provide a SoftLayer API service name.');
     }
     $soapClient = new SoftLayer_SoapClient(self::API_BASE_URL . $serviceName . '?wsdl');
     if ($username != null && $apiKey != null) {
         $soapClient->setAuthentication($username, $apiKey);
     } elseif (Softlayer_SoapClient::$apiUser != null && Softlayer_SoapClient::$apiKey != null) {
         $soapClient->setAuthentication(Softlayer_SoapClient::$apiUser, Softlayer_SoapClient::$apiKey);
     }
     $soapClient->_serviceName = $serviceName;
     if ($id != null) {
         $soapClient->setInitParameter($id);
     }
     return $soapClient;
 }
예제 #3
0
파일: User.php 프로젝트: c12g/stratos-php
 /**
  * Authenticate a user
  *
  * Authentication is handled by SoftLayer's authentication system. Passwords
  * are not stored locally.
  *
  * @param string $username
  * @param string $password
  * @throws Exception
  * @return bool
  */
 public static function authenticate($username, $password)
 {
     /*
      * Make sure the user exists locally first.
      */
     $user = Model_DbTable_User::findByUsername($username);
     if ($user == null) {
         throw new Exception('Invalid login credentials provided.');
     }
     /*
      * Attempt to authenticate to the SoftLayer API. API docs at
      * http://sldn.softlayer.com/wiki/index.php/SoftLayer_User_Customer::getPortalLoginToken
      */
     $client = SoftLayer_SoapClient::getClient('SoftLayer_User_Customer');
     try {
         $result = $client->getPortalLoginToken($username, $password);
     } catch (Exception $e) {
         throw new Exception($e->getMessage());
     }
     return true;
 }
예제 #4
0
 public function indexAction()
 {
     $client = SoftLayer_SoapClient::getClient('SoftLayer_Account');
     $objectMask = new SoftLayer_ObjectMask();
     $objectMask->hardwareCount;
     $objectMask->virtualGuestCount;
     /*
      * Bandwidth usage
      */
     $objectMask->hardware->billingCyclePublicBandwidthUsage;
     $objectMask->hardware->billingItem->bandwidthAllocation;
     $objectMask->virtualGuests->billingCyclePublicBandwidthUsage;
     $objectMask->virtualGuests->billingItem->bandwidthAllocation;
     /*
      * Monitoring status
      */
     $objectMask->networkMonitorUpHardwareCount;
     $objectMask->networkMonitorRecoveringHardwareCount;
     $objectMask->networkMonitorDownHardwareCount;
     $objectMask->networkMonitorUpVirtualGuestCount;
     $objectMask->networkMonitorRecoveringVirtualGuestCount;
     $objectMask->networkMonitorDownVirtualGuestCount;
     $client->setObjectMask($objectMask);
     try {
         $account = $client->getObject();
     } catch (Exception $e) {
         $this->view->errorMessage = 'Unable to retrieve account information. ' . $e->getMessage();
     }
     /*
      * Calculate servers and CCIs over 85% and 100% bandwidth.
      */
     $serversOver85PercentBandwidth = 0;
     $serversOver100PercentBandwidth = 0;
     $instancesOver85PercentBandwidth = 0;
     $instancesOver100PercentBandwidth = 0;
     foreach ($account->hardware as $server) {
         if (isset($server->billingCyclePublicBandwidthUsage) && isset($server->billingItem) && isset($server->billingItem->bandwidthAllocation) && $server->billingItem->bandwidthAllocation->amount > 0) {
             $usagePercent = $server->billingCyclePublicBandwidthUsage->amountOut / $server->billingItem->bandwidthAllocation->amount;
         } else {
             $usagePercent = 0;
         }
         if ($usagePercent >= 0.85) {
             $serversOver85PercentBandwidth++;
         } elseif ($usagePercent >= 1) {
             $serversOver100PercentBandwidth++;
         }
     }
     foreach ($account->virtualGuests as $server) {
         if (isset($server->billingCyclePublicBandwidthUsage) && isset($server->billingItem) && isset($server->billingItem->bandwidthAllocation) && $server->billingItem->bandwidthAllocation->amount > 0) {
             $usagePercent = $server->billingCyclePublicBandwidthUsage->amountOut / $server->billingItem->bandwidthAllocation->amount;
         } else {
             $usagePercent = 0;
         }
         if ($usagePercent >= 0.85) {
             $instancesOver85PercentBandwidth++;
         } elseif ($usagePercent >= 1) {
             $instancesOver100PercentBandwidth++;
         }
     }
     $this->view->pageTitle = ucfirst($this->view->translate->_('Home'));
     $this->view->headTitle(ucfirst($this->view->translate->_('Home')));
     $this->view->account = $account;
     $this->view->serversOver85PercentBandwidth = $serversOver85PercentBandwidth;
     $this->view->serversOver100PercentBandwidth = $serversOver100PercentBandwidth;
     $this->view->instancesOver85PercentBandwidth = $instancesOver85PercentBandwidth;
     $this->view->instancesOver100PercentBandwidth = $instancesOver100PercentBandwidth;
 }
 /**
  * Create a SoftLayer API SOAP Client
  *
  * Retrieve a new SoftLayer_SoapClient object for a specific SoftLayer API
  * service using either the class' constants API_USER and API_KEY or a
  * custom username and API key for authentication. Provide an optional id
  * value if you wish to instantiate a particular SoftLayer API object.
  *
  * @param string $serviceName The name of the SoftLayer API service you wish to query
  * @param int $id An optional object id if you're instantiating a particular SoftLayer API object. Setting an id defines this client's initialization parameter header.
  * @param string $username An optional API username if you wish to bypass SoftLayer_SoapClient's built-in username.
  * @param string $username An optional API key if you wish to bypass SoftLayer_SoapClient's built-in API key.
  * @param string $endpointUrl The API endpoint base URL you wish to connect to. Set this to SoftLayer_SoapClient::API_PRIVATE_ENDPOINT to connect via SoftLayer's private network.
  * @return SoftLayer_SoapClient
  */
 public static function getClient($serviceName, $id = null, $username = null, $apiKey = null, $endpointUrl = null)
 {
     $serviceName = trim($serviceName);
     if ($serviceName == null) {
         throw new Exception('Please provide a SoftLayer API service name.');
     }
     /*
      * Default to use the public network API endpoint, otherwise use the
      * endpoint defined in API_PUBLIC_ENDPOINT, otherwise use the one
      * provided by the user.
      */
     if (isset($endpointUrl)) {
         $endpointUrl = trim($endpointUrl);
         if ($endpointUrl == null) {
             throw new Exception('Please provide a valid API endpoint.');
         }
     } elseif (self::API_BASE_URL != null) {
         $endpointUrl = self::API_BASE_URL;
     } else {
         $endpointUrl = SoftLayer_SoapClient::API_PUBLIC_ENDPOINT;
     }
     if (is_null(self::SOAP_TIMEOUT)) {
         $soapClient = new SoftLayer_SoapClient($endpointUrl . $serviceName . '?wsdl');
     } else {
         $soapClient = new SoftLayer_SoapClient($endpointUrl . $serviceName . '?wsdl', array('connection_timeout' => self::SOAP_TIMEOUT));
     }
     $soapClient->_serviceName = $serviceName;
     $soapClient->_endpointUrl = $endpointUrl;
     if ($username != null && $apiKey != null) {
         $soapClient->setAuthentication($username, $apiKey);
     } else {
         $soapClient->setAuthentication(self::API_USER, self::API_KEY);
     }
     if ($id !== null) {
         $soapClient->setInitParameter($id);
     }
     return $soapClient;
 }
예제 #6
0
 /**
  * Make sure PHP is up to snuff, that we can write to the right places, and
  * make the first user.
  */
 public function indexAction()
 {
     $phpCheck = array('PHP Version >= 5.2.3' => version_compare(PHP_VERSION, '5.2.3') >= 0, 'Standard Extension Loaded' => extension_loaded('standard'), 'SOAP Extension Loaded' => extension_loaded('soap'), 'PCRE Extension Loaded' => extension_loaded('pcre'), 'PDO Extension Loaded' => extension_loaded('pdo'), 'PDO SQLite Extension Loaded' => extension_loaded('pdo_sqlite'), 'SPL Extension Loaded' => extension_loaded('spl'), 'Session Extension Loaded' => extension_loaded('session'), 'Ctype Extension Loaded' => extension_loaded('ctype'));
     $systemCheck = array('Languages Directory (' . LANGUAGE_PATH . ') Writable' => is_writable(LANGUAGE_PATH), 'Skins Directory (' . SKIN_PATH . ') Writable' => is_writable(SKIN_PATH), 'Database Directory (' . APPLICATION_PATH . '/../data/db) Writable' => is_writable(APPLICATION_PATH . '/../data/db'), 'Configuration File (' . CONFIG_PATH . '/settings.ini' . ') Writable' => is_writable(CONFIG_PATH . '/settings.ini'));
     /*
      * Show an error if there are any PHP or system errors.
      */
     $hasPhpErrors = false;
     $hasSystemErrors = false;
     foreach ($phpCheck as $check) {
         if (!$check) {
             $hasPhpErrors = true;
             break;
         }
     }
     foreach ($systemCheck as $check) {
         if (!$check) {
             $hasSystemErrors = true;
             break;
         }
     }
     if (!$hasPhpErrors && !$hasSystemErrors) {
         /*
          * Build the add user form.
          */
         $config = Zend_Registry::get('config');
         $skins = Model_Skin::getAllSkins();
         $languages = Model_Language::getAllLanguages();
         /*
          * Turn the skin and language lists into something more Zend_Form
          * friendly.
          */
         foreach ($skins as $skin) {
             $skinList[$skin->name] = $skin->name;
         }
         foreach ($languages as $language) {
             $languageList[$language->name] = $language->name;
         }
         $form = new Zend_Form();
         $form->setMethod('post');
         $username = $form->createElement('text', 'username');
         $username->setLabel(ucfirst($this->view->translate->_('username')));
         $username->setRequired(true);
         $username->addValidator('alnum');
         $apiKey = $form->createElement('text', 'apiKey');
         $apiKey->setLabel(ucfirst($this->view->translate->_('API key')));
         $apiKey->setRequired(true);
         $apiKey->addValidator('alnum');
         $skin = $form->createElement('select', 'skin');
         $skin->setLabel(ucfirst($this->view->translate->_('skin')));
         $skin->addMultiOptions($skinList);
         $skin->setValue($config->defaults->skin);
         $skin->setRequired(true);
         $language = $form->createElement('select', 'language');
         $language->setLabel(ucfirst($this->view->translate->_('language')));
         $language->addMultiOptions($languageList);
         $language->setValue($config->defaults->language);
         $language->setRequired(true);
         $form->addElement($username);
         $form->addElement($apiKey);
         $form->addElement($skin);
         $form->addElement($language);
         $form->addElement('submit', 'submit', array('label' => $this->view->translate->_('Submit')));
         /*
          * Process form submission.
          */
         if ($this->getRequest()->isPost()) {
             $formData = $this->getRequest()->getPost();
             if ($form->isValid($formData)) {
                 /*
                  * Try out the username and API key to make sure they
                  * entered a good one.
                  */
                 $account = null;
                 $client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $form->getValue('username'), $form->getValue('apiKey'));
                 try {
                     $account = $client->getObject();
                 } catch (Exception $e) {
                     $this->view->errorMessage = $this->view->translate->_('Please enter a valid username and API key combination.');
                 }
                 /*
                  * Add the user.
                  */
                 if ($account != null) {
                     try {
                         $user = Model_DbTable_User::addUser($form->getValue('username'), $form->getValue('apiKey'), $form->getValue('skin'), $form->getValue('language'), true);
                         $this->_helper->_redirector->goToRouteAndExit(array('controller' => 'index', 'action' => null));
                     } catch (Exception $e) {
                         $this->view->errorMessage = $this->view->translate->_('Unable to add user.') . ' ' . $e->getMessage();
                     }
                 }
             } else {
                 $this->view->errorMessage = $this->view->translate->_('Please completely fill out the configuration form.');
             }
             $form->populate($formData);
         }
         $this->view->form = $form;
     }
     $this->view->pageTitle = 'Installation';
     $this->view->headTitle('Installation');
     $this->view->phpCheck = $phpCheck;
     $this->view->systemCheck = $systemCheck;
     $this->view->hasPhpErrors = $hasPhpErrors;
     $this->view->hasSystemErrors = $hasSystemErrors;
 }
예제 #7
0
 /**
  * Process and return the results of an asyncrhonous SoftLayer API call
  *
  * Read data from our socket and process the raw SOAP result from the
  * SoftLayer_SoapClient instance that made the asynchronous call. wait()
  * *must* be called in order to recieve the results from your API call.
  *
  * @return object
  */
 public function wait()
 {
     $soapResult = '';
     while (!feof($this->_socket)) {
         $soapResult .= fread($this->_socket, 8192);
     }
     // separate the SOAP result into headers and data.
     list($headers, $data) = explode("\r\n\r\n", $soapResult);
     return $this->_soapClient->handleAsyncResult($this->_functionName, $data);
 }
예제 #8
0
 * account and connectivity.
 */
try {
    print_r($client->getObject());
} catch (Exception $e) {
    die($e->getMessage());
}
/**
 * For a more complex example we’ll retrieve a support ticket with id 123456
 * along with the ticket’s updates, the user it’s assigned to, the servers
 * attached to it, and the datacenter those servers are in. We’ll retrieve our
 * extra information using a nested object mask. After we have the ticket we’ll
 * update it with the text ‘Hello!’.
 */
// Declare an API client to connect to the SoftLayer_Ticket API service.
$client = SoftLayer_SoapClient::getClient('SoftLayer_Ticket', 123456, $apiUsername, $apiKey);
// Assign an object mask to our API client:
$objectMask = new SoftLayer_ObjectMask();
$objectMask->updates;
$objectMask->assignedUser;
$objectMask->attachedHardware->datacenter;
$client->setObjectMask($objectMask);
// Retrieve the ticket record.
try {
    $ticket = $client->getObject();
    print_r($ticket);
} catch (Exception $e) {
    die('Unable to retrieve ticket record: ' . $e->getMessage());
}
// Now update the ticket.
$update = new stdClass();
예제 #9
0
 /**
  * Reboot a server
  *
  * Allow user reboot via management card, power strip, or both.
  */
 public function rebootAction()
 {
     $hardware = null;
     $rebootSuccessful = false;
     if ($this->_getParam('id') == null) {
         $this->view->errorMessage = $this->view->translate->_('Please provide a hardware id.');
     } else {
         $client = SoftLayer_SoapClient::getClient('SoftLayer_Hardware_Server', $this->_getParam('id'));
         /*
          * Build the reboot form.
          */
         $form = new Zend_Form();
         $form->setMethod('post');
         $rebootMethod = $form->createElement('select', 'rebootMethod');
         $rebootMethod->setLabel(ucfirst($this->view->translate->_('reboot via')));
         $rebootMethod->addMultiOptions(array('rebootDefault' => $this->view->translate->_('management card with powerstrip fallback'), 'rebootSoft' => $this->view->translate->_('management card only'), 'powerCycle' => $this->view->translate->_('power strip only')));
         $rebootMethod->setRequired(true);
         $form->addElement($rebootMethod);
         $form->addElement('submit', 'reboot', array('label' => ucfirst($this->view->translate->_('reboot'))));
         /*
          * Get hardware info.
          */
         $objectMask = new SoftLayer_ObjectMask();
         $objectMask->recentRemoteManagementCommands;
         $client->setObjectMask($objectMask);
         try {
             $hardware = $client->getObject();
         } catch (Exception $e) {
             $this->view->errorMessage = $this->translate->_('Error retrieving hardware record.') . ' ' . $e->getMessage();
         }
         /*
          * Handle the reboot request.
          */
         if ($this->getRequest()->isPost()) {
             $formData = $this->getRequest()->getPost();
             if ($form->isValid($formData)) {
                 try {
                     switch ($form->getValue('rebootMethod')) {
                         case 'rebootDefault':
                             $result = $client->rebootDefault();
                             break;
                         case 'rebootSoft':
                             $result = $client->rebootSoft();
                             break;
                         case 'powerCycle':
                             $result = $client->powerCycle();
                             break;
                     }
                     $rebootSuccessful = true;
                 } catch (Exception $e) {
                     $this->view->errorMessage = $this->view->translate->_('Reboot failed.') . ' ' . $e->getMessage();
                 }
             } else {
                 $this->view->errorMessage = $this->view->translate->_('Reboot failed.') . ' ' . $this->view->translate->_('Please completely fill out the reboot form.');
                 $form->populate($formData);
             }
         }
     }
     if ($hardware != null) {
         $this->view->pageTitle = ucfirst($this->view->translate->_('reboot')) . ' ' . $hardware->hostname . '.' . $hardware->domain;
         $this->view->headTitle(ucfirst($this->view->translate->_('reboot')) . ' ' . $hardware->hostname . '.' . $hardware->domain);
         $this->view->form = $form;
     }
     $this->view->rebootSuccessful = $rebootSuccessful;
     $this->view->hardware = $hardware;
 }
예제 #10
0
 /**
  * Edit a local user.
  *
  * This does not affect the user's corresponding SoftLayer user account.
  */
 public function edituserAction()
 {
     $user = null;
     /*
      * Get user info.
      */
     try {
         $user = new Model_DbTable_User($this->_getParam('id'));
     } catch (Exception $e) {
         $this->view->errorMessage = $this->translate->_('Unable to locate user.') . ' ' . $e->getMessage();
     }
     if ($user != null) {
         /*
          * Build the delete form. Only show it if the user isn't trying to
          * delete themselves.
          */
         $deleteForm = null;
         if ($user->id != $this->view->currentUser->id) {
             $deleteForm = new Zend_Form();
             $deleteForm->setMethod('post');
             $hidden = $deleteForm->createElement('hidden', 'mode');
             $hidden->setValue('delete');
             $deleteForm->addElement($hidden);
             $deleteForm->addElement('submit', 'submit', array('label' => $this->view->translate->_('Submit')));
         }
         /*
          * Build the edit form.
          */
         $skins = Model_Skin::getAllSkins();
         $languages = Model_Language::getAllLanguages();
         /*
          * Turn the skin and language lists into something more Zend_Form
          * friendly.
          */
         foreach ($skins as $skin) {
             $skinList[$skin->name] = $skin->name;
         }
         foreach ($languages as $language) {
             $languageList[$language->name] = $language->name;
         }
         $form = new Zend_Form();
         $form->setMethod('post');
         $username = $form->createElement('text', 'username');
         $username->setLabel(ucfirst($this->view->translate->_('username')));
         $username->setRequired(true);
         $username->addValidator('alnum');
         $username->setValue($user->username);
         $apiKey = $form->createElement('text', 'apiKey');
         $apiKey->setLabel(ucfirst($this->view->translate->_('API key')));
         $apiKey->setRequired(true);
         $apiKey->addValidator('alnum');
         $apiKey->setValue($user->apiKey);
         $skin = $form->createElement('select', 'skin');
         $skin->setLabel(ucfirst($this->view->translate->_('skin')));
         $skin->addMultiOptions($skinList);
         $skin->setValue($user->skin);
         $skin->setRequired(true);
         $language = $form->createElement('select', 'language');
         $language->setLabel(ucfirst($this->view->translate->_('language')));
         $language->addMultiOptions($languageList);
         $language->setValue($user->language);
         $language->setRequired(true);
         $isAdmin = $form->createElement('checkbox', 'isAdmin');
         $isAdmin->setLabel(ucfirst($this->view->translate->_('administrator')));
         $isAdmin->setChecked($user->isAdmin);
         $hidden = $form->createElement('hidden', 'mode');
         $hidden->setValue('edit');
         $form->addElement($username);
         $form->addElement($apiKey);
         $form->addElement($skin);
         $form->addElement($language);
         $form->addElement($isAdmin);
         $form->addElement($hidden);
         $form->addElement('submit', 'submit', array('label' => $this->view->translate->_('Submit')));
         /*
          * Process form submission.
          */
         if ($this->getRequest()->isPost()) {
             $formData = $this->getRequest()->getPost();
             /*
              * Delete the user.
              */
             if ($formData['mode'] == 'delete') {
                 /*
                  * Users may not delete themselves.
                  */
                 if ($user->id == $this->view->currentUser->id) {
                     $this->view->errorMessage = $this->view->translate->_('You may not delete your user account.');
                 } else {
                     try {
                         $user->deleteUser();
                         $this->_helper->_redirector->goToRouteAndExit(array('controller' => 'admin', 'action' => 'users', 'id' => null));
                     } catch (Exception $e) {
                         $this->view->errorMessage = $this->view->translate->_('Unable to delete user.') . ' ' . $e->getMessage();
                     }
                 }
                 /*
                  * Edit the user.
                  */
             } else {
                 if ($form->isValid($formData)) {
                     /*
                      * Try out the username and API key to make sure they
                      * entered a good one.
                      */
                     $account = null;
                     $client = SoftLayer_SoapClient::getClient('SoftLayer_Account', null, $form->getValue('username'), $form->getValue('apiKey'));
                     try {
                         $account = $client->getObject();
                     } catch (Exception $e) {
                         $this->view->errorMessage = $this->view->translate->_('Please enter a valid username and API key combination.');
                     }
                     /*
                      * If the user is editing themself then make sure they
                      * don't take away their own admin privileges.
                      */
                     if ($user->id == $this->view->currentUser->id && $form->getValue('isAdmin') != $this->view->currentUser->isAdmin) {
                         $account = null;
                         $this->view->errorMessage = $this->view->translate->_('You may not change your administrative status.');
                     }
                     /*
                      * Update the user.
                      */
                     if ($account != null) {
                         try {
                             $user->updateUser($form->getValue('username'), $form->getValue('apiKey'), $form->getValue('skin'), $form->getValue('language'), $form->getValue('isAdmin'));
                             $this->view->statusMessage = $this->view->translate->_('User saved.');
                         } catch (Exception $e) {
                             $this->view->errorMessage = $this->view->translate->_('Unable to save user.') . ' ' . $e->getMessage();
                         }
                     }
                 } else {
                     $this->view->errorMessage = $this->view->translate->_('Please completely fill out the configuration form.');
                 }
             }
             $form->populate($formData);
         }
         $this->view->pageTitle = $this->view->translate->_('Edit') . ' ' . $user->username;
         $this->view->headTitle($this->view->translate->_('Edit') . ' ' . $user->username);
         $this->view->deleteForm = $deleteForm;
         $this->view->form = $form;
     }
     $this->view->user = $user;
 }