Im trying to use VisualCaptcha with my Laravel apps. I have tried to adapt this exemple with my code. But I dont know how to adapt this following callback to my controller :
$app->get( '/audio(/:type)', function( $type = 'mp3' ) use( $app ) {
$captcha = new \visualCaptcha\Captcha( $app->session );
if ( ! $captcha->streamAudio( $app->response, $type ) ) {
$app->pass();
}
} );
Here is my controller :
class CaptchaController extends BaseController {
/**
* Start Captcha
*/
public function start($params)
{
$session = new SessionCaptcha();
$captcha = new Captcha($session);
return $captcha->generate();
}
public function audio()
{
$session = new SessionCaptcha();
$captcha = new Captcha($session);
//$response = Response::make();
$response->header('Access-Control-Allow-Origin', '*');
//return var_dump($response);
return $captcha->streamAudio($response, 'mp3');
}
}
The start function works but not the audio function... I dont know how to add the first parameter to "$captcha->streamAudio($firstParam, $extension)".
Just in case other people land here first, this has been answered at https://github.com/emotionLoop/visualCaptcha-PHP/issues/16#issuecomment-53863330
The author mentioned not returning $captcha->generate(); but also some undisclosed modifications.
Related
I am a beginner in php. I have a WadoService service and a StudiesRestController controller. I want to use controller data in the service.
public function getPatientAction(Request $request, $studyUID)
{
$studyRepository = new StudyRepository(
$this->get('nexus_db'),
$this->get('logger'),
$this->get('translator')
);
$study = $studyRepository->getStudy($studyUID);
if (!$study) {
throw new NotFoundHttpException("No study found with studyuid $studyUID");
}
$patientInfo = new RestResponse(
SerializerBuilder::create()
->build()
->serialize($study->getPatient(), 'json')
);
return $patientInfo;
}
Is this possible? I have tried to put this in the function getPatientAction()without result:
/* #var $wadoService WadoService */
$wadoService = $this->container->get(WadoService::SERVICE_NAME);
$wadoService = new RestResponse(
SerializerBuilder::create()
->build()
->serialize($study->getPatient(), 'json')
);
To pass a variable from your controller to your service, you do it it like that :
$wadoService = $this->container->get(WadoService::SERVICE_NAME)->yourServiceMethod($yourVari);
Previously it worked with the version PHP 5.6.3, pthreads, Symfony2, Doctrine2, MongoDB and everything worked very well. I decided to migrate to PHP 7.0.2, I installed pthreads, I'm still using Symfony2, Doctrine2 and MongoDB, but multi thread processing stopped working.
I have defined the following classes:
class Formula extends \Worker
{
static $document_manager;
static $elements;
public function start( int $options = NULL )
{
parent::start(PTHREADS_INHERIT_NONE);
}
public function run()
{
//Set require Autoload and AppKernel
require_once __DIR__.'/../../../../app/autoload.php';
require_once __DIR__.'/../../../../app/AppKernel.php';
//Creating a new AppKernel with the given environment
$kernel = new \AppKernel( 'dev', 1 );
//Loading the Cache and Classes
$kernel->loadClassCache();
$kernel->boot();
//Set document manager
static::$document_manager = $kernel->getContainer()->get('doctrine_mongodb')->getManager();
static::$elements = static::$document_manager->getRepository('MyBundle:Elements')->findAllActive();
$elements = array();
//Creating array of objects type Element
if ( static::$elements )
{
foreach ( static::$elements as $element )
{
$elements[] = new Element( $element );
}
}
if (!empty($elements))
{
foreach ( $elements as $element )
{
//For each element execute the run method using start
$element->start();
$element->join();
}
}
$processed_elements = array();
while ( count($elements) > 0 )
{
foreach ( $elements as $id => $element )
{
//If finished the run method
if ( !$element->is_running )
{
$processed_elements[] = $element;
//Cleaning up once this thread is done
unset($elements[$id]);
}
}
}
/**
* Performing Logic with Processed Items
*/
}
}
class Element extends \Thread
{
public $is_running;
static $document_manager;
static $medicine;
public function __construct ( $Element )
{
error_log( 'Constructing Element Thread' );
/**
* Collect item data
*/
$this->is_running = true;
}
public function start( int $options = Null )
{
parent::start(PTHREADS_INHERIT_NONE);
}
public function run()
{
//Set require Autoload and AppKernel
require_once __DIR__.'/../../../../app/autoload.php';
require_once __DIR__.'/../../../../app/AppKernel.php';
//Creating a new AppKernel with the given environment
$kernel = new \AppKernel( 'dev', 1 );
//Loading the Cache and Classes
$kernel->loadClassCache();
$kernel->boot();
//Set document manager
static::$document_manager = $kernel->getContainer()->get('doctrine_mongodb')->getManager();
/**
* Logic for obtaining the medicine ....
*/
$id_medicamento = 'xxxxxx';
static::$medicine = static::$document_manager->getRepository('MyBundle:Medicine')->find($id_medicamento);
$return = false;
//Save in the database
if ( $this->save() )
{
$return = true;
}
$this->is_running = false;
return $return;
}
protected function save()
{
error_log( 'Saving' );
//Save the data
if ( !empty(static::$medicine) )
{
//Create new instance of Indication
$indication = new Indication();
$indication->setName( 'indication name' );
$indication->setValue( 'indication value' );
$indication->setDoctor( "doctor's identification" );
//Persist Indication
static::$document_manager->persist( $indication );
//Add new Indication in Medicine
static::$medicine->addIndicacion( $indication );
//Create instance of Event
$event = new Event();
$event->setAction( 'Setting indication' );
$event->setDatetime( new \MongoDate() );
$event->setComment( 'Event comment' );
//Persist Event
static::$document_manager->persist( $event );
//Add new Event in Medicine
static::$medicine->addEvento( $event );
// Write in DB
static::$document_manager->persist( static::$medicine );
/**
* Here the bug is generated and it neither writes the Indication nor the Event
* in the Medication collection, to know which error was generated I put the
* following line between try-catch and the exception is:
* "Catchable Fatal Error: Object of class Volatile could not be converted to string"
*/
static::$document_manager->flush();
return true;
}
return false;
}
}
The error is in the save method. Any help to solve this error I am very grateful. Even any optimization of the use of Doctrine that is in both Formula and Element threads would be very helpful. Thank you.
On the line:
$indication-> setValue($ value);
The value that was received was an array and the field is defined as a string in the document.
In this case the array is considered a Volatile object and generated the bug "Catchable Fatal Error: Object of class Volatile could not be converted to string"
The solution was:
$indication-> setValue(json_encode ($ value));
I have a page dashboard.php, which creates a merchant dashboard that shows deals submitted by the merchant. I'm simply trying to separate types of deals by checking to see if a deal is a suggested deal:
...
while ($deals->have_posts()) : $deals->the_post();
$suggested_deal = SA_Post_Type::get_instance( $post->ID );
$boolsuggesteddeal = $suggested_deal->is_suggested_deal();
...
However, the is_suggested_deal() line is causing the page to not display anything past that line.
The SA_POST_TYPE class is outlined below:
class SA_Post_Type extends Group_Buying_Deal {
...
public static function get_instance( $id = 0 ) {
if ( !$id ) {
return NULL;
}
if ( !isset( self::$instances[$id] ) || !self::$instances[$id] instanceof self ) {
self::$instances[$id] = new self( $id );
}
if ( self::$instances[$id]->post->post_type != parent::POST_TYPE ) {
return NULL;
}
return self::$instances[$id];
}
...
public function is_suggested_deal() {
$term = array_pop( wp_get_object_terms( $this->get_id(), self::TAX ) );
return $term->slug == self::TERM_SLUG;
}
...
Since the class and function are both public, why am I unable to call the function? Any help would be greatly appreciated.
EDIT: I can't figure out how to get error reporting on without showing all site users the errors, I'm on a live site. I tried creating an instance of SA_Post_Type(), but that alone cause the page to fail to load anything after that line.
You have not created an instance of the class, do so like this...
$SA_Post_Type = new SA_Post_Type();
Then you are able to access the function...
$boolsuggesteddeal = $SA_Post_Type->is_suggested_deal();
Since is_suggested_deal is not a static function, you have to create a new instance of the SA_Post_Type class firstly.
$sa_post_type = new SA_Post_Type();
$boolsuggesteddeal = $sa_post_type->is_suggested_deal();
Hope this helps.
I have been reading a lot about how and why to use an MVC approach in an application. I have seen and understand examples of a Model, I have seen and understand examples of the View.... but I am STILL kind of fuzzy on the controller. I would really love to see a thorough enough example of a controller(s). (in PHP if possible, but any language will help)
Thank you.
PS: It would also be great if I could see an example of an index.php page, which decides which controller to use and how.
EDIT: I know what the job of the controller is, I just don't really understand how to accomplish this in OOP.
Request example
Put something like this in your index.php:
<?php
// Holds data like $baseUrl etc.
include 'config.php';
$requestUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$requestString = substr($requestUrl, strlen($baseUrl));
$urlParams = explode('/', $requestString);
// TODO: Consider security (see comments)
$controllerName = ucfirst(array_shift($urlParams)).'Controller';
$actionName = strtolower(array_shift($urlParams)).'Action';
// Here you should probably gather the rest as params
// Call the action
$controller = new $controllerName;
$controller->$actionName();
Really basic, but you get the idea... (I also didn't take care of loading the controller class, but I guess that can be done either via autoloading or you know how to do it.)
Simple controller example (controllers/login.php):
<?php
class LoginController
{
function loginAction()
{
$username = $this->request->get('username');
$password = $this->request->get('password');
$this->loadModel('users');
if ($this->users->validate($username, $password))
{
$userData = $this->users->fetch($username);
AuthStorage::save($username, $userData);
$this->redirect('secret_area');
}
else
{
$this->view->message = 'Invalid login';
$this->view->render('error');
}
}
function logoutAction()
{
if (AuthStorage::logged())
{
AuthStorage::remove();
$this->redirect('index');
}
else
{
$this->view->message = 'You are not logged in.';
$this->view->render('error');
}
}
}
As you see, the controller takes care of the "flow" of the application - the so-called application logic. It does not take care about data storage and presentation. It rather gathers all the necessary data (depending on the current request) and assigns it to the view...
Note that this would not work with any framework I know, but I'm sure you know what the functions are supposed to do.
Imagine three screens in a UI, a screen where a user enters some search criteria, a screen where a list of summaries of matching records is displayed and a screen where, once a record is selected it is displayed for editing. There will be some logic relating to the initial search on the lines of
if search criteria are matched by no records
redisplay criteria screen, with message saying "none found"
else if search criteria are matched by exactly one record
display edit screen with chosen record
else (we have lots of records)
display list screen with matching records
Where should that logic go? Not in the view or model surely? Hence this is the job of the controller. The controller would also be responsible for taking the criteria and invoking the Model method for the search.
<?php
class App {
protected static $router;
public static function getRouter() {
return self::$router;
}
public static function run($uri) {
self::$router = new Router($uri);
//get controller class
$controller_class = ucfirst(self::$router->getController()) . 'Controller';
//get method
$controller_method = strtolower((self::$router->getMethodPrefix() != "" ? self::$router->getMethodPrefix() . '_' : '') . self::$router->getAction());
if(method_exists($controller_class, $controller_method)){
$controller_obj = new $controller_class();
$view_path = $controller_obj->$controller_method();
$view_obj = new View($controller_obj->getData(), $view_path);
$content = $view_obj->render();
}else{
throw new Exception("Called method does not exists!");
}
//layout
$route_path = self::getRouter()->getRoute();
$layout = ROOT . '/views/layout/' . $route_path . '.phtml';
$layout_view_obj = new View(compact('content'), $layout);
echo $layout_view_obj->render();
}
public static function redirect($uri){
print("<script>window.location.href='{$uri}'</script>");
exit();
}
}
<?php
class Router {
protected $uri;
protected $controller;
protected $action;
protected $params;
protected $route;
protected $method_prefix;
/**
*
* #return mixed
*/
function getUri() {
return $this->uri;
}
/**
*
* #return mixed
*/
function getController() {
return $this->controller;
}
/**
*
* #return mixed
*/
function getAction() {
return $this->action;
}
/**
*
* #return mixed
*/
function getParams() {
return $this->params;
}
function getRoute() {
return $this->route;
}
function getMethodPrefix() {
return $this->method_prefix;
}
public function __construct($uri) {
$this->uri = urldecode(trim($uri, "/"));
//defaults
$routes = Config::get("routes");
$this->route = Config::get("default_route");
$this->controller = Config::get("default_controller");
$this->action = Config::get("default_action");
$this->method_prefix= isset($routes[$this->route]) ? $routes[$this->route] : '';
//get uri params
$uri_parts = explode("?", $this->uri);
$path = $uri_parts[0];
$path_parts = explode("/", $path);
if(count($path_parts)){
//get route
if(in_array(strtolower(current($path_parts)), array_keys($routes))){
$this->route = strtolower(current($path_parts));
$this->method_prefix = isset($routes[$this->route]) ? $routes[$this->route] : '';
array_shift($path_parts);
}
//get controller
if(current($path_parts)){
$this->controller = strtolower(current($path_parts));
array_shift($path_parts);
}
//get action
if(current($path_parts)){
$this->action = strtolower(current($path_parts));
array_shift($path_parts);
}
//reset is for parameters
//$this->params = $path_parts;
//processing params from url to array
$aParams = array();
if(current($path_parts)){
for($i=0; $i<count($path_parts); $i++){
$aParams[$path_parts[$i]] = isset($path_parts[$i+1]) ? $path_parts[$i+1] : null;
$i++;
}
}
$this->params = (object)$aParams;
}
}
}
Create folder structure
Setup .htaccess & virtual hosts
Create config class to build config array
Controller
Create router class with protected non static, with getters
Create init.php with config include & autoload and include paths (lib, controlelrs,models)
Create config file with routes, default values (route, controllers, action)
Set values in router - defaults
Set uri paths, explode the uri and set route, controller, action, params ,process params.
Create app class to run the application by passing uri - (protected router obj, run func)
Create controller parent class to inherit all other controllers (protected data, model, params - non static)
set data, params in constructor.
Create controller and extend with above parent class and add default method.
Call the controller class and method in run function. method has to be with prefix.
Call the method if exisist
Views
Create a parent view class to generate views. (data, path) with default path, set controller, , render funcs to
return the full tempalte path (non static)
Create render function with ob_start(), ob_get_clean to return and send the content to browser.
Change app class to parse the data to view class. if path is returned, pass to view class too.
Layouts..layout is depend on router. re parse the layout html to view and render
Please check this:
<?php
global $conn;
require_once("../config/database.php");
require_once("../config/model.php");
$conn= new Db;
$event = isset($_GET['event']) ? $_GET['event'] : '';
if ($event == 'save') {
if($conn->insert("employee", $_POST)){
$data = array(
'success' => true,
'message' => 'Saving Successful!',
);
}
echo json_encode($data);
}
if ($event == 'update') {
if($conn->update("employee", $_POST, "id=" . $_POST['id'])){
$data = array(
'success' => true,
'message' => 'Update Successful!',
);
}
echo json_encode($data);
}
if ($event == 'delete') {
if($conn->delete("employee", "id=" . $_POST['id'])){
$data = array(
'success' => true,
'message' => 'Delete Successful!',
);
}
echo json_encode($data);
}
if ($event == 'edit') {
$data = $conn->get("select * from employee where id={$_POST['id']};")[0];
echo json_encode($data);
}
?>
I am developing a Facebook app in Zend Framework. In startAction() I am getting the following error:
The URL http://apps.facebook.com/rails_across_europe/turn/move-trains-auto is not valid.
I have included the code for startAction() below. I have also included the code for moveTrainsAutoAction (these are all TurnController actions) I can't find anything wrong with my _redirect() in startAction(). I am using the same redirect in other actions and they execute flawlessly. Would you please review my code and let me know if you find a problem? I appreciate it! Thanks.
public function startAction() {
require_once 'Train.php';
$trainModel = new Train();
$config = Zend_Registry::get('config');
require_once 'Zend/Session/Namespace.php';
$userNamespace = new Zend_Session_Namespace('User');
$trainData = $trainModel->getTrain($userNamespace->gamePlayerId);
switch($trainData['type']) {
case 'STANDARD':
default:
$unitMovement = $config->train->standard->unit_movement;
break;
case 'FAST FREIGHT':
$unitMovement = $config->train->fast_freight->unit_movement;
break;
case 'SUPER FREIGHT':
$unitMovement = $config->train->superfreight->unit_movement;
break;
case 'HEAVY FREIGHT':
$unitMovement = $config->train->heavy_freight->unit_movement;
break;
}
$trainRow = array('track_units_remaining' => $unitMovement);
$where = $trainModel->getAdapter()->quoteInto('id = ?', $trainData['id']);
$trainModel->update($trainRow, $where);
$this->_redirect($config->url->absolute->fb->canvas . '/turn/move-trains-auto');
}
.
.
.
public function moveTrainsAutoAction() {
$log = Zend_Registry::get('log');
$log->debug('moveTrainsAutoAction');
require_once 'Train.php';
$trainModel = new Train();
$userNamespace = new Zend_Session_Namespace('User');
$gameNamespace = new Zend_Session_Namespace('Game');
$trainData = $trainModel->getTrain($userNamespace->gamePlayerId);
$trainRow = $this->_helper->moveTrain($trainData['dest_city_id']);
if(count($trainRow) > 0) {
if($trainRow['status'] == 'ARRIVED') {
// Pass id for last city user selected so we can return user to previous map scroll postion
$this->_redirect($config->url->absolute->fb->canvas . '/turn/unload-cargo?city_id='.$gameNamespace->endTrackCity);
} else if($trainRow['track_units_remaining'] > 0) {
$this->_redirect($config->url->absolute->fb->canvas . '/turn/move-trains-auto');
} else { /* Turn has ended */
$this->_redirect($config->url->absolute->fb->canvas . '/turn/end');
}
}
$this->_redirect($config->url->absolute->fb->canvas . '/turn/move-trains-auto-error'); //-set-destination-error');
}
As #Jani Hartikainen points out in his comment, there is really no need to URL-encode underscores. Try to redirect with literal underscores and see if that works, since I believe redirect makes some url encoding of its own.
Not really related to your question, but in my opinion you should refactor your code a bit to get rid of the switch-case statements (or at least localize them to a single point):
controllers/TrainController.php
[...]
public function startAction() {
require_once 'Train.php';
$trainTable = new DbTable_Train();
$config = Zend_Registry::get('config');
require_once 'Zend/Session/Namespace.php';
$userNamespace = new Zend_Session_Namespace('User');
$train = $trainTable->getTrain($userNamespace->gamePlayerId);
// Add additional operations in your getTrain-method to create subclasses
// for the train
$trainTable->trackStart($train);
$this->_redirect(
$config->url->absolute->fb->canvas . '/turn/move-trains-auto'
);
}
[...]
models/dbTable/Train.php
class DbTable_Train extends Zend_Db_Table_Abstract
{
protected $_tableName = 'Train';
[...]
/**
*
*
* #return Train|false The train of $playerId, or false if the player
* does not yet have a train
*/
public function getTrain($playerId)
{
// Fetch train row
$row = [..];
return $this->trainFromDbRow($row);
}
private function trainFromDbRow(Zend_Db_Table_Row $row)
{
$data = $row->toArray();
$trainType = 'Train_Standard';
switch($row->type) {
case 'FAST FREIGHT':
$trainType = 'Train_Freight_Fast';
break;
case 'SUPER FREIGHT':
$trainType = 'Train_Freight_Super';
break;
case 'HEAVY FREIGHT':
$trainType = 'Train_Freight_Heavy';
break;
}
return new $trainType($data);
}
public function trackStart(Train $train)
{
// Since we have subclasses here, polymorphism will ensure that we
// get the correct speed etc without having to worry about the different
// types of trains.
$trainRow = array('track_units_remaining' => $train->getSpeed());
$where = $trainModel->getAdapter()->quoteInto('id = ?', $train->getId());
$this->update($trainRow, $where);
}
[...]
/models/Train.php
abstract class Train
{
public function __construct(array $data)
{
$this->setValues($data);
}
/**
* Sets multiple values on the model by calling the
* corresponding setter instead of setting the fields
* directly. This allows validation logic etc
* to be contained in the setter-methods.
*/
public function setValues(array $data)
{
foreach($data as $field => $value)
{
$methodName = 'set' . ucfirst($field);
if(method_exists($methodName, $this))
{
$this->$methodName($value);
}
}
}
/**
* Get the id of the train. The id uniquely
* identifies the train.
* #return int
*/
public final function getId ()
{
return $this->id;
}
/**
* #return int The speed of the train / turn
*/
public abstract function getSpeed ();
[..] //More common methods for trains
}
/models/Train/Standard.php
class Train_Standard extends Train
{
public function getSpeed ()
{
return 3;
}
[...]
}
/models/Train/Freight/Super.php
class Train_Freight_Super extends Train
{
public function getSpeed ()
{
return 1;
}
public function getCapacity ()
{
return A_VALUE_MUCH_LARGER_THAN_STANDARD;
}
[...]
}
By default, this will send an HTTP 302 Redirect. Since it is writing headers, if any output is written to the HTTP output, the program will stop sending headers. Try looking at the requests and response inside Firebug.
In other case, try using non default options to the _redirect() method. For example, you can try:
$ropts = { 'exit' => true, 'prependBase' => false };
$this->_redirect($config->url->absolute->fb->canvas . '/turn/move-trains-auto', $ropts);
There is another interesting option for the _redirect() method, the code option, you can send for example a HTTP 301 Moved Permanently code.
$ropts = { 'exit' => true, 'prependBase' => false, 'code' => 301 };
$this->_redirect($config->url->absolute->fb->canvas . '/turn/move-trains-auto', $ropts);
I think I may have found the answer. It appears that Facebook does not play nice with redirect, so it is neccessary to use Facebook's 'fb:redirect' FBML. This appears to work:
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
echo '<fb:redirect url="' . $config->url->absolute->fb->canvas . '/turn/move-trains-auto"/>';