/**
  * Method for allowing a user to reset their password
  * @param {stdClass} $data Data passed from ActionScript
  * @return {array} Returns a standard response array
  */
 public function lostPassword($data)
 {
     $response = CodeBank_ClientAPI::responseBase();
     $response['login'] = true;
     $SQL_email = Convert::raw2sql($data->user);
     $member = Member::get_one('Member', "\"Email\"='{$SQL_email}'");
     // Allow vetoing forgot password requests
     $sng = new MemberLoginForm(Controller::has_curr() ? Controller::curr() : singleton('Controller'), 'LoginForm');
     $results = $sng->extend('forgotPassword', $member);
     if ($results && is_array($results) && in_array(false, $results, true)) {
         $response['status'] = 'HELO';
         $response['message'] = _t('CodeBankAPI.PASSWORD_SENT_TEXT', "A reset link has been sent to '{email}', provided an account exists for this email address.", array('email' => $data['Email']));
     }
     if ($member) {
         $token = $member->generateAutologinTokenAndStoreHash();
         $e = Member_ForgotPasswordEmail::create();
         $e->populateTemplate($member);
         $e->populateTemplate(array('PasswordResetLink' => Security::getPasswordResetLink($member, $token)));
         $e->setTo($member->Email);
         $e->send();
         $response['status'] = 'HELO';
         $response['message'] = _t('CodeBankAPI.PASSWORD_SENT_TEXT', "A reset link has been sent to '{email}', provided an account exists for this email address.", array('email' => $data->user));
     } else {
         if (!empty($data->user)) {
             $response['status'] = 'HELO';
             $response['message'] = _t('CodeBankAPI.PASSWORD_SENT_TEXT', "A reset link has been sent to '{email}', provided an account exists for this email address.", array('email' => $data->user));
         } else {
             $response['status'] = 'EROR';
             $response['message'] = _t('Member.ENTEREMAIL', 'Please enter an email address to get a password reset link.');
         }
     }
     return $response;
 }
 /**
  * Deletes a folder
  * @param {stdClass} $data Data passed from ActionScript
  * @return {array} Standard response base
  */
 public function moveSnippet($data)
 {
     $response = CodeBank_ClientAPI::responseBase();
     //Ensure logged in
     if (!Permission::check('CODE_BANK_ACCESS')) {
         $response['status'] = 'EROR';
         $response['message'] = _t('CodeBankAPI.PERMISSION_DENINED', '_Permission Denied');
         return $response;
     }
     $snippet = Snippet::get()->byID(intval($data->id));
     if (empty($snippet) || $snippet === false || $snippet->ID == 0) {
         $response['status'] = "EROR";
         $response['message'] = _t('CodeBankAPI.SNIPPET_NOT_FOUND', '_Snippet not found');
         return $response;
     }
     if ($data->folderID != 0) {
         $snippetFolder = SnippetFolder::get()->byID(intval($data->folderID));
         if (empty($snippetFolder) || $snippetFolder === false || $snippetFolder->ID == 0) {
             $response['status'] = "EROR";
             $response['message'] = _t('CodeBankAPI.FOLDER_DOES_NOT_EXIST', '_Folder does not exist');
             return $response;
         }
         if ($snippetFolder->LanguageID != $snippet->LanguageID) {
             $response['status'] = "EROR";
             $response['message'] = _t('CodeBankAPI.LANGUAGE_NOT_SAME', '_Folder is not in the same language as the snippet');
             return $response;
         }
     }
     try {
         $snippet->FolderID = $data->folderID;
         $snippet->write();
         $response['status'] = "HELO";
     } catch (Exception $e) {
         $response['status'] = "EROR";
         $response['message'] = "Internal Server error occured";
     }
     return $response;
 }
 /**
  * Edits a language
  * @param {stdClass} $data Data passed from ActionScript
  * @return {array} Returns a standard response array
  */
 public function editLanguage($data)
 {
     $response = CodeBank_ClientAPI::responseBase();
     try {
         if (SnippetLanguage::get()->filter('Name:nocase', Convert::raw2sql($data->language))->Count() > 0) {
             $response['status'] = 'EROR';
             $response['message'] = _t('CodeBankAPI.LANGUAGE_EXISTS', '_Language already exists');
             return $response;
         }
         $lang = SnippetLanguage::get()->byID(intval($data->id));
         if (empty($lang) || $lang === false || $lang->ID == 0) {
             $response['status'] = 'EROR';
             $response['message'] = _t('CodeBankAPI.LANGUAGE_NOT_FOUND', '_Language not found');
             return $response;
         }
         //Update language and write
         if ($lang->UserLanguage == true) {
             $lang->Name = $data->language;
             $lang->FileExtension = $data->fileExtension;
         }
         $lang->Hidden = $data->hidden;
         $lang->write();
         $response['status'] = 'HELO';
         $response['message'] = "Language edited successfully";
     } catch (Exception $e) {
         $response['status'] = 'EROR';
         $response['message'] = _t('CodeBankAPI.SERVER_ERROR', '_Server error has occured, please try again later');
     }
     return $response;
 }
 /**
  * Handles passing a request through the amf client
  * @param {string} $servicePath Service path i.e ServerController.connect
  * @param {object|array} $data Data to be sent with the request should be an array or an object
  * @return {array} Server response
  */
 protected function getAMFResponse($servicePath, $data = null)
 {
     require_once 'Zend/Amf/Request.php';
     require_once 'Zend/Amf/Constants.php';
     require_once 'Zend/Amf/Value/MessageBody.php';
     require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
     require_once 'Zend/Amf/Value/Messaging/ErrorMessage.php';
     if ($data) {
         if (is_array($data)) {
             $data = $this->arrayToObject($data);
         } else {
             if (!is_object($data)) {
                 user_error('$data is not an array or object', E_USER_ERROR);
             }
         }
     }
     //Find the method and service
     $service = explode('.', $servicePath);
     $method = array_pop($service);
     $service = implode('.', $service);
     //Build the message
     $message = new Zend_Amf_Value_Messaging_RemotingMessage();
     $message->parameters = $data;
     $message->operation = $method;
     $message->source = $service;
     //Build the message body
     $body = new Zend_Amf_Value_MessageBody($servicePath, '/1', array($data));
     //Build the AMF Request
     $request = new Zend_Amf_Request();
     $request->addAmfBody($body);
     $request->setObjectEncoding(Zend_Amf_Constants::AMF3_OBJECT_ENCODING);
     //Init the client api
     $amfClient = new CodeBank_ClientAPI();
     $amfClient->setTestRequest($request);
     //Capture the response as an amf input stream
     ob_start();
     $response = $amfClient->index();
     ob_end_clean();
     //Get the amf bodies
     $bodies = $response->getAmfBodies();
     if (count($bodies) > 0) {
         $body = $bodies[0]->getData();
         if ($body instanceof Zend_Amf_Value_Messaging_ErrorMessage) {
             $this->fail('AMF Server returned an error: ' . $body->faultString . "\n\n" . $body->faultDetail);
             return false;
         }
         return $body;
     }
     return false;
 }
 /**
  * Gets the current php session id
  */
 public function getSessionId()
 {
     $response = CodeBank_ClientAPI::responseBase();
     $response['data'] = session_id();
     return $response;
 }
 /**
  * Loads a remote class or method and executes the function and returns the result
  * @param {string} $method Is the method to execute
  * @param {mixed} $param values for the method
  * @return {mixed} $response the result of executing the method
  * @throws Zend_Amf_Server_Exception
  */
 protected function _dispatch($method, $params = null, $source = null)
 {
     if ($source) {
         if (($mapped = Zend_Amf_Parse_TypeLoader::getMappedClassName($source)) !== false) {
             $source = $mapped;
         }
     }
     $qualifiedName = empty($source) ? $method : $source . '.' . $method;
     if (!isset($this->_table[$qualifiedName])) {
         // if source is null a method that was not defined was called.
         if ($source) {
             $className = 'CodeBank' . str_replace('.', '_', $source);
             if (class_exists($className, false) && !isset($this->_classAllowed[$className])) {
                 require_once 'Zend/Amf/Server/Exception.php';
                 throw new Zend_Amf_Server_Exception('Can not call "' . $className . '" - use setClass()');
             }
             try {
                 $this->getLoader()->load($className);
             } catch (Exception $e) {
                 require_once 'Zend/Amf/Server/Exception.php';
                 throw new Zend_Amf_Server_Exception('Class "' . $className . '" does not exist: ' . $e->getMessage(), 0, $e);
             }
             // Add the new loaded class to the server.
             $this->setClass($className, $source);
         }
         if (!isset($this->_table[$qualifiedName])) {
             // Source is null or doesn't contain specified method
             require_once 'Zend/Amf/Server/Exception.php';
             throw new Zend_Amf_Server_Exception('Method "' . $method . '" does not exist');
         }
     }
     $info = $this->_table[$qualifiedName];
     $argv = $info->getInvokeArguments();
     if (0 < count($argv)) {
         $params = array_merge($params, $argv);
     }
     if ($info instanceof Zend_Server_Reflection_Function) {
         $func = $info->getName();
         $this->_checkAcl(null, $func);
         $return = call_user_func_array($func, $params);
     } else {
         if ($info instanceof Zend_Server_Reflection_Method) {
             // Get class
             $class = $info->getDeclaringClass()->getName();
             //Check permissions
             if ($this->_canAccess($class) == false) {
                 $response = CodeBank_ClientAPI::responseBase();
                 $response['status'] = 'EROR';
                 $response['message'] = _t('CodeBankAPI.PERMISSION_DENINED', '_Permission Denied');
                 return $response;
             }
             if ('static' == $info->isStatic()) {
                 // for some reason, invokeArgs() does not work the same as
                 // invoke(), and expects the first argument to be an object.
                 // So, using a callback if the method is static.
                 $this->_checkAcl($class, $info->getName());
                 $return = call_user_func_array(array($class, $info->getName()), $params);
             } else {
                 // Object methods
                 try {
                     $object = $info->getDeclaringClass()->newInstance();
                 } catch (Exception $e) {
                     throw new Zend_Amf_Server_Exception('Error instantiating class ' . $class . ' to invoke method ' . $info->getName() . ': ' . $e->getMessage(), 621, $e);
                 }
                 $this->_checkAcl($object, $info->getName());
                 $return = $info->invokeArgs($object, $params);
             }
         } else {
             throw new Zend_Amf_Server_Exception('Method missing implementation ' . get_class($info));
         }
     }
     return $return;
 }