/**
  * Process query functionality
  *
  * @param sfWebRequest $request 
  * @return Array
  * @author Sergey Startsev <*****@*****.**>
  */
 private function processQuery(sfWebRequest $request)
 {
     $sQuery = $request->getParameter('query', '');
     $sConnection = $request->getParameter('connection', 'propel');
     $sType = $request->getParameter('type', 'sql');
     $nOffset = $request->getParameter('start', 0);
     $nLimit = $request->getParameter('limit', 50);
     $response = $this->oDBQueryHelper->processQuery($sQuery, $sConnection, $sType, $nOffset, $nLimit);
     afsNotificationPeer::log('Database query was executed', 'afStudioDBQuery');
     return $response;
 }
 /**
  * Process getting notifications
  *
  * @return afResponse
  * @author Sergey Startsev
  */
 protected function processGet()
 {
     $offset = $this->getParameter('offset');
     $notifications = afsNotificationPeer::getAll($offset);
     $data = array();
     if ($notifications) {
         foreach ($notifications as $notification) {
             $data[] = afStudioNotificationCommandHelper::render($notification);
             $offset++;
         }
     }
     return afResponseHelper::create()->success(true)->data(array(), array('notifications' => implode('', $data), 'offset' => $offset), 0);
 }
 /**
  * Create new user 
  *
  * @param sfWebRequest $request 
  * @return array
  * @author Sergey Startsev
  */
 public static function createNewUser(sfWebRequest $request)
 {
     $response = afResponseHelper::create();
     afStudioUser::getInstance()->authorize();
     $sUsername = $request->getParameter('username');
     $aUser = json_decode($request->getParameter('user'), true);
     $user = afStudioUser::retrieve($sUsername);
     $aErrors = array();
     if ($user) {
         $aErrors['username'] = '******';
     }
     if (afStudioUser::retrieveByEmail($aUser['email'])) {
         $aErrors['email'] = "User with this `email` already exists";
     }
     if (!afStudioUser::getInstance()->isAdmin()) {
         if ($aUser['captcha'] != sfContext::getInstance()->getUser()->getFlash(afsCaptcha::SESSION_IDENTIFICATOR)) {
             $aErrors['captcha'] = "Invalid verification code";
         }
     }
     // Prepare data for validating and creating
     $aCreate = array(afStudioUser::USERNAME => $sUsername, afStudioUser::FIRST_NAME => $aUser['first_name'], afStudioUser::LAST_NAME => $aUser['last_name'], afStudioUser::EMAIL => $aUser['email'], afStudioUser::PASSWORD => $aUser['password'], afStudioUser::ROLE => afStudioUser::getInstance()->isAdmin() ? $aUser['role'] : 'user');
     // Validating user data
     $validate = afStudioUser::validate($aCreate);
     if (is_bool($validate) && $validate === true && empty($aErrors)) {
         // unset username - no need to creating meta-field username
         unset($aCreate[afStudioUser::USERNAME]);
         // Create new user
         afStudioUser::create($sUsername, $aCreate);
         afsNotificationPeer::log('User has been successfully created', 'afStudioUser');
         // getting current domain
         $domain = sfConfig::get('app_domain') ? sfConfig::get('app_domain') : sfContext::getInstance()->getRequest()->getHost();
         $aParameters = array('user' => $aUser, 'password' => $aUser['password']);
         sfProjectConfiguration::getActive()->loadHelpers(array("Url", "Tag"));
         $message = Swift_Message::newInstance()->setFrom("no-reply@{$domain}", 'Studio')->setTo($aUser['email'])->setSubject('Studio Account')->setBody(sfContext::getInstance()->getController()->getAction('afsUserManager', 'create')->getPartial('afsUserManager/create', $aParameters))->setContentType('text/html');
         try {
             @sfContext::getInstance()->getController()->getAction('afsUserManager', 'create')->getMailer()->send($message);
         } catch (Swift_TransportException $e) {
             $response->console("Local server can't sent email for now. Please check mail server settings.");
         }
     } else {
         if (is_array($validate)) {
             $aErrors = self::mergeErrors($aErrors, $validate);
         }
     }
     $aErrors = self::prepareErrors($aErrors);
     if (!empty($aErrors)) {
         return $response->success(false)->message($aErrors)->asArray();
     }
     return $response->success(true)->message('User has been successfully created')->asArray();
 }
 public function save()
 {
     $confData = $this->databaseConfTemplate;
     $param =& $confData['all']['propel']['param'];
     $param['dsn'] = $this->buildDsn($confData['all']['propel']['param']['dsn']);
     $param['username'] = $this->params['username'];
     $param['password'] = $this->params['password'];
     $param['persistent'] = isset($this->params['persistent']) ? true : false;
     $param['pooling'] = isset($this->params['pooling']) ? true : false;
     afStudioUtil::writeFile($this->databaseConfFilePath, $this->dumpYaml($confData));
     afsNotificationPeer::log('Database Settings have been modified', 'afStudioConf');
     if (is_readable($this->databaseConfFilePath)) {
         $result = true;
     } else {
         $result = false;
     }
     return $result;
 }
 public function build()
 {
     if ($this->request->hasParameter('type') && $this->request->getParameter('type') == 'save') {
         $params = $this->request->getPostParameters();
         unset($params['type']);
         $params['autodeploy'] = !isset($params['autodeploy']) ? false : true;
         $params['url'] = afStudioUtil::getHost();
         $this->setProjectParams($params);
         afStudioUtil::writeFile($this->projectConfFilePath, $this->dumpYaml($this->projectConfTemplate));
         $result['success'] = true;
         $result['message'] = 'Project Settings saved successfully';
         afsNotificationPeer::log('Project Settings have been modified', 'afStudioConf');
     } else {
         $result['success'] = true;
         $result['data'] = $this->getProjectParams();
     }
     return json_encode($result);
 }
 /**
  * Sign In functionality controller
  */
 public function executeSignin(sfWebRequest $request)
 {
     if ($request->isMethod('post')) {
         if ($request->hasParameter('signin')) {
             $signin = $request->getParameter('signin');
             $sUsername = $signin['username'];
             $sPassword = $signin['password'];
             $bRemember = isset($signin['remember']);
             $user = afStudioUser::getInstance()->retrieve($sUsername);
             if ($user) {
                 if ($user['password'] === sha1($signin['password'])) {
                     afStudioUser::getInstance()->set($sUsername, $sPassword, $bRemember);
                     $signinUrl = $this->getRequest()->getReferer();
                     $signinUrl = $signinUrl != null ? $signinUrl : url_for('@homepage');
                     $result = array('success' => true, 'redirect' => $signinUrl, 'load' => 'page');
                     afsNotificationPeer::log('Successful login', 'afStudioAuth');
                 } else {
                     $result = array('success' => false, 'message' => 'The username and/or password is invalid. Please try again.');
                     afsNotificationPeer::log('Failed to get authenticated in the system!', 'afStudioAuth');
                 }
             } else {
                 $result = array('success' => false, 'message' => 'The username and/or password is invalid. Please try again.', 'redirect' => '/afsAuthorize/index', 'load' => 'page');
                 afsNotificationPeer::log('Failed to get authenticated in the system!', 'afStudioAuth');
             }
         } else {
             $result = array('success' => false, 'message' => 'You were logged out ! You\'ll be redirected to the login page!', 'redirect' => '/afsAuthorize/index', 'load' => 'page');
             afsNotificationPeer::log('Successful logout', 'afStudioAuth');
         }
     } else {
         // if we have been forwarded, then the referer is the current URL
         // if not, this is the referer of the current request
         $user->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());
         $module = sfConfig::get('sf_login_module');
         if ($this->getModuleName() != $module) {
             $result = array('success' => false, 'message' => 'You were logged out ! You\'ll be redirected to the login page!', 'redirect' => '/afsAuthorize/index', 'load' => 'page');
             afsNotificationPeer::log('Successful logout', 'afStudioAuth');
         }
     }
     return $this->renderJson($result);
 }
 /**
  * Delete User functionality
  * 
  * @param sfWebRequest $request
  * @return string - json
  * @author Sergey Startsev
  */
 public function executeDelete(sfWebRequest $request)
 {
     if (!afStudioUser::getInstance()->isAdmin()) {
         $this->forward404("You have no rights to execute this action");
     }
     $response = afResponseHelper::create();
     $username = $request->getParameter('username');
     if (afStudioUser::getInstance()->getUsername() == $username) {
         return $this->renderJson($response->success(false)->message("You can't delete youself")->asArray());
     }
     if (!afStudioUser::getInstance()->retrieve($username)) {
         return $this->renderJson($response->success(false)->message("This user doesn't exists")->asArray());
     }
     if (!afStudioUser::delete($username)) {
         return $this->renderJson($response->success(false)->message("Can't delete user")->asArray());
     }
     afsNotificationPeer::log('User has been deleted', 'afStudioUser');
     return $this->renderJson($response->success(true)->message("User has been deleted")->asArray());
 }
 /**
  * Retrieve multiple objects by pkey.
  *
  * @param      array $pks List of primary keys
  * @param      PropelPDO $con the connection to use
  * @throws     PropelException Any exceptions caught during processing will be
  *		 rethrown wrapped into a PropelException.
  */
 public static function retrieveByPKs($pks, PropelPDO $con = null)
 {
     if ($con === null) {
         $con = Propel::getConnection(afsNotificationPeer::DATABASE_NAME, Propel::CONNECTION_READ);
     }
     $objs = null;
     if (empty($pks)) {
         $objs = array();
     } else {
         $criteria = new Criteria(afsNotificationPeer::DATABASE_NAME);
         $criteria->add(afsNotificationPeer::ID, $pks, Criteria::IN);
         $objs = afsNotificationPeer::doSelect($criteria, $con);
     }
     return $objs;
 }
 /**
  * Find object by primary key using raw SQL to go fast.
  * Bypass doSelect() and the object formatter by using generated code.
  *
  * @param     mixed $key Primary key to use for the query
  * @param     PropelPDO $con A connection object
  *
  * @return    afsNotification A model object, or null if the key is not found
  */
 protected function findPkSimple($key, $con)
 {
     $sql = 'SELECT `MESSAGE`, `MESSAGE_TYPE`, `USER`, `IP`, `CREATED_AT`, `ID` FROM `afs_notification` WHERE `ID` = :p0';
     try {
         $stmt = $con->prepare($sql);
         $stmt->bindValue(':p0', $key, PDO::PARAM_INT);
         $stmt->execute();
     } catch (Exception $e) {
         Propel::log($e->getMessage(), Propel::LOG_ERR);
         throw new PropelException(sprintf('Unable to execute SELECT statement [%s]', $sql), $e);
     }
     $obj = null;
     if ($row = $stmt->fetch(PDO::FETCH_NUM)) {
         $obj = new afsNotification();
         $obj->hydrate($row);
         afsNotificationPeer::addInstanceToPool($obj, (string) $row[0]);
     }
     $stmt->closeCursor();
     return $obj;
 }
 /**
  * Populates the object using an array.
  *
  * This is particularly useful when populating an object from one of the
  * request arrays (e.g. $_POST).  This method goes through the column
  * names, checking to see whether a matching key exists in populated
  * array. If so the setByName() method is called for that column.
  *
  * You can specify the key type of the array by additionally passing one
  * of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
  * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
  * The default key type is the column's phpname (e.g. 'AuthorId')
  *
  * @param      array  $arr     An array to populate the object from.
  * @param      string $keyType The type of keys the array uses.
  * @return     void
  */
 public function fromArray($arr, $keyType = BasePeer::TYPE_PHPNAME)
 {
     $keys = afsNotificationPeer::getFieldNames($keyType);
     if (array_key_exists($keys[0], $arr)) {
         $this->setMessage($arr[$keys[0]]);
     }
     if (array_key_exists($keys[1], $arr)) {
         $this->setMessageType($arr[$keys[1]]);
     }
     if (array_key_exists($keys[2], $arr)) {
         $this->setUser($arr[$keys[2]]);
     }
     if (array_key_exists($keys[3], $arr)) {
         $this->setIp($arr[$keys[3]]);
     }
     if (array_key_exists($keys[4], $arr)) {
         $this->setCreatedAt($arr[$keys[4]]);
     }
     if (array_key_exists($keys[5], $arr)) {
         $this->setId($arr[$keys[5]]);
     }
 }
 /**
  * Execute project functionality
  *
  * @param sfWebRequest $request 
  * @return string - json
  * @author Sergey Startsev
  */
 public function executeProject(sfWebRequest $request)
 {
     $permissions = new Permissions();
     $is_writable = $permissions->isWritable(sfConfig::get('sf_root_dir') . '/cache/');
     if ($is_writable !== true) {
         echo $is_writable;
         die;
     }
     $command = $request->getParameter('cmd');
     if (!sfConfig::get('app_afs_projects_management_enabled') && in_array($command, array('saveWizard', 'CheckConfig'))) {
         //If projects management is disabled we allow only run and export, and helper processing commands
         throw new Exception('Projects management is disabled!');
     }
     $response = afStudioCommand::process('project', $command, $request->getParameterHolder()->getAll());
     if ($command == 'run') {
         afsNotificationPeer::log('[run] Run was executed on the project');
         $response->query(sfContext::getInstance()->getController()->genUrl('@homepage'));
     }
     if ($command == 'get') {
         if ($response->getParameter(afResponseSuccessDecorator::IDENTIFICATOR)) {
             return $this->renderJson($response->getParameter(afResponseDataDecorator::IDENTIFICATOR_DATA));
         }
     }
     if ($command == 'uploadWallpaper') {
         return $this->renderText($response->asJson());
     }
     return $this->renderJson($response->asArray());
 }