Redirection Loop in CakePHP Application - php

I am using CakePHP v1.2 for my web application hosted here: http://lol-land.in
The application was working fine till Yesterday, but it suddenly started to get stuck in some redirection loops. What makes it weirder is the fact that the problem is with just one controller: posts. And even in that most of the functions are working. But http://lol-land.in/posts is redirecting to lol-land.in/posts/view/133 which is in turn redirecting to itself.
Actually 110 of the 117 posts of the form /posts/view/ are stuck in this 302 redirection.
Can anyone please tell me what could have caused this?
[CakePHP 1.3 and PHP5]
Edit: Adding View Logic
function view($id = null) {
$this->log("View Logic Entry", LOG_DEBUG);
// These Log entires are missing
$this->layout = 'postview';
if (!$id) {
$this->Session->setFlash(__('Invalid Post.', true));
$this->log("Redirect due to missing id", LOG_DEBUG);
$this->redirect(array('action'=>'index'));
}
$log = $this->Session->read('Auth.User');
$logid = $log['id'];
$temp = $this->Post->read(null, $id);
$ratings = $temp['Rating'];
$this->set(compact('up', 'down', 'userrated', 'userrateid'));
$coms = $temp['Comment'];
$comuser = array();
for ($i=0; $i<count($coms); $i++) {
$comuser[$i] = $coms[$i]['user_id'];
}
$comuser = $this->Post->User->find('list', array( 'fields'=>array('User.id', 'User.username'),
'conditions'=>array("User.id" => $comuser)
));
$this->set(compact('comuser'));
$this->pageTitle = $temp['Post']['title'];
$this->set('post', $temp);
$alltypes = $this->Post->Type->find('list', array('fields'=> array('Type.id', 'Type.typename')));
$selectedtab = -1;
$this->set(compact('alltypes', 'selectedtab' ));
//Calling updateFBStats
// Removed because unnecessary.
}

Chances are you are either 1) using circular references with the Auth component OR 2) the function in your controller is redirecting do to something within the method. Can you show the code of posts_controller.php function view() ?

Related

Slim Framework $_GET 404 error

Hello so I have a simple code here which will do an update in a column and redirect to a page based on the GET parameter. My code is here:
In my html:
Move
and in the index.php
$app->get('/user/admin/table/:table_id/move', 'RecycleTable');
function RecycleTable($table_id)
{
$session = new \RKA\Session();
$app = \Slim\Slim::getInstance();
if (!$session->type) {
$app->redirect('/user/login');
}
else {
$sponsorID = $app->request()->get('sponsor');
$db = new db();
$bind = array(
':table_id' => $table_id
);
$update = array(
'status' => '2'
);
$db->update("tables", $update, "tableID = :table_id", $bind);
$db = null;
$app->redirect('/user/admin/table/'.$sponsorID);
}
}
When I try to click Move I get a 404 error. Do I get the parameter sponsor correctly? Or is there something wrong here?
It appears as if your link in the HTML is incorrect. Your route states that the path should be:
Move
not
Move
You are missing the /user/admin/table part which is why you would be receiving a 404. It cannot resolve to the correct route.

Find Method Empty Array

I have an application with a third party plugin that handles ACL management, works fine, but I am having some issues with a isAuthorized function. The user is being passed into the function and when I debug the variable I can see all of the correct information in the dataset, but when the script executes the find query it returns empty. Here is the thing, the model I am executing the query on is apart of the plugin I am using. So I am executing a find within my application on a model that lies within my plugin. To me it isn't ideal, nonetheless I am working with a plugin that wasn't built application specific. In order to execute the find the Author added
App::uses('User', 'AuthAcl.Model');
so the find could be possible. Now that said, I tweaked one thing in the conditions part of the statement because I was getting Column 'id' in field list is ambiguous error from SQL. I started to get that error because I added some relationships to the application.
I say all that to say this - since I am loading the model via App Uses shouldn't I be able to execute a find like so
$this->User->Find('First');
I have tried that and it says I'm executing on a non object. Here is the code for that script. I need input. When I execute my script without debugging it locks me out the application, and by reading the code and debugging it I think this happens because the find is returning empty. And on another note - please bear with me - I didn't write this code, and I am in the phase of learning to understand other developers code. I am using CAKEPHP 2.3 Thanks guys!
public function isAuthorized($user = null) {
App::uses('User', 'AuthAcl.Model');
App::uses('Group', 'AuthAcl.Model');
$authFlag = false;
$this->set('login_user',$user);
$userModel = new User();
$group = new Group();
//die(debug($user));
$rs = $this->$userModel->find('first',array('conditions'=>array('user.id' => $user['id'])));
die(debug($rs));
$action = 'controllers';
if (!empty($this->plugin)){
$action .= '/'.$this->plugin;
}
$action .= '/'.$this->name;
$action .= '/'.$this->action;
if (!empty($rs['Group'])){
foreach ($rs['Group'] as $group){
$authFlag = $this->Acl->check(array('Group' => array('id' => $group['id'])), $action);
if ($authFlag == true){
break;
}
}
}
if ($authFlag == false && !empty($user)){
$authFlag = $this->Acl->check(array('User' => array('id' => $user['id'])), $action);
//die(debug($authFlag));
}
if ($authFlag == false && !empty($user)){
$this->redirect(array('controller' => 'accessDenied', 'action' => 'index','plugin' =>'auth_acl'));
}
if (!empty($user)){
$user = $userModel->find('first',array('conditions' => array('user.id' => $user['id'])));
$this->Session->write('auth_user',$user);
$this->request->data['auth_plugin'] = $this->plugin;
$this->request->data['auth_controller'] = $this->name;
$this->request->data['auth_action'] = $this->action;
}
return $authFlag;
}
}

Illegal string offset Warning PHP '#type' in VBridgeApp->create()

I get a strange PHP error after updating my php version to 5.4
This is my function
protected function create() {
//if (VBRIDGE_DEBUG)
//drupal_set_message(__CLASS__ .'::'.__METHOD__);
$path = $this->vbridge_root_path;
$path_vbridge = $path . '/' . VBridge::VBRIDGE_CLASS_PREFIX;
$subclass = $this->getClass();
foreach ($this->_objclass as $objclass) {
if (!$this->createObj($path, $objclass, $subclass)) {
$this->createObj($path_vbridge, $objclass);
}
}
if (self::getStatus()) {
return false;
}
// Set User Session Qookie
//$this->getUser()->setQookie($this->getQookie());
// Set User Session
$this->getUser()->setSession($this->getSession());
$this->getSession()->setQookie($this->getQookie());
//$this->getUser()->setAuth($this->getAuth());
// Set User Pass
$this->getUser()->setPass($this->getPass());
// Set Auth
$this->setAuthMethods();
$this->setAuthStorages();
//
foreach ($this->getConfig() as $config) {
if ($config['#type'] == '#class') {
//createObj($config['#name'], $config['#type'], $config['#class'], $config['#path'], $appData['#config']);
}
}
return true;
}
This is the line that gives
if ($config['#type'] == '#class') {
I've looked at similar questions but haven't figured out how to fix this. Any assistance would be helpful.
Edit: Yes, I did put wrong code up last night. I was very tired after trying to tangle with this.
The error message you've posted doesn't match the line of code you say it originates from. You should get a different error for that, specifically to do with only using variables by reference.
Your code should be
$accounts = user_load_multiple(array(), array('name' => $login));
$account = array_shift($account);
But Drupal already has a helper method for that, so you might as well use it:
$account = user_load_by_name($login);

Calling vendor scripts returns a blank page CakePHP

I am running a cakePHP app that has to call external 1st party php scripts. Controller action flow looks like this:
function add($id = null, $year = null, $month = null, $day = null, $project_id = null){
...
if($this->saveRepeatedEvents($event, $employees, $weekdays)) {
$this->Session->setFlash(__('Events have been saved'));
$this->redirect(array('controller' => 'users', 'action'=>'index'));
}
...
}
function saveRepeatedEvents($event, $employees, $weekdays) {
App::import('Vendor', 'notifications/Notifications');
$notifications = new Notifications;
// code omitted…
$notifications->send_notification($employees, $eventId, $numberOfEvents);
return true;
}
The external scripts are called and they do what they are expected to do. The problem is that after the scripts are executed the app is supposed to set a flash and redirect a user, but all that happens is that a blank page is returned.
I have checked the error logs, but they ar empty as well.
I am really lost in this situation and any help is much appreciated.

Mediawiki stopping a page from saving and redirecting back to edit page with an error message

My wiki articles contain a link to a specific dataset. I want to enforce that these links are unique (as in no one can create a new page with a link that is present in another page.) I have already written most of the code for this extension. I created a table 'unique_external_links' that stores the url as an index and the page id that the URL lives in.
Here is a part of the code I wrote:
$wgHooks['ParserFirstCallInit'][] = 'UniqueURLSetupParserFunction';
$wgHooks['LoadExtensionSchemaUpdates'][] = 'fnExternalLinksDBHook';
// Allow translation of the parser function name
$wgExtensionMessagesFiles['UniqueUrl'] = dirname( __FILE__ ) . '/UniqueUrl.i18n.php';
// Tell MediaWiki that the parser function exists.
function UniqueURLSetupParserFunction( &$parser ) {
$parser->setFunctionHook( 'example', 'UniqueURLParserFunction' );
return true;
}
function UniqueURLParserFunction( $parser, $param1 = '', $param2 = '' ) {
// The input parameters are wikitext with templates expanded.
// The output should be wikitext too.
global $wgRequest, $wgOut;
$return_url = $wgRequest->getRequestURL();
$pid = $param2;
$param1 = trim($param1);
$url_pattern = '/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/';
$match = preg_match($url_pattern, $param1);
if (!$match) {
// return ERROR not a valid URL!
}
$patterns = array('/^(https?:\/\/)/', '/\/$/');
$replace = array('','');
$url = preg_replace($patterns, $replace, $param1);
if (empty($param2)) { // creating a new page
try {
$dbw = wfGetDB( DB_MASTER );
$res = $dbw->insert('unique_external_links',
array('link_url' => $url , 'page_id' => $pid));
} catch(Exception $e) {
// return ERROR page with this link already exists!
}
} else { //Trying to edit existing page
$dbr = wfGetDB( DB_SLAVE );
$res = $dbr->select(
'unique_external_links',
array( 'link_url' ),
'link_url = "' .$url.'" AND page_id = "' .$pid.'"'
);
if ($dbr->numRows($res) == 0) {
try {
$dbw = wfGetDB( DB_MASTER );
$res = $dbw->insert('unique_external_links',
array('link_url' => $url , 'page_id' => $pid));
} catch(Exception $e) {
//return ERROR Dataset Already Exists
$response = $wgRequest -> response();
$response -> header('Location: '.$return_url);
return $return_url;
}
}else {
//just editing page, not changing link, all is good
return $param1;
}
}
return $param1;
}
First off, I apologize for the sloppy code, really just slapped this together very quickly with no prior extension experience...
As you can see there are places where I have the comment //return ERROR I would like to stop media wiki from saving the page if one of those conditions are true. Instead of saving, I would like to return the user to the edit page with a message telling them there is a problem with the link they are providing.
Any ideas? I looked around a lot but couldn't find anything similar, I assume it is because I don't know really what question to ask. I am aware that there are hooks like 'ArticleSave', but i didn't know how I would use that in conjunction with a parser.
Any help would be great! Even if its telling me to completely re-do what I did because its all wrong haha.
EDIT: I fixed this problem by throwing MWExceptions at those places where I wanted to return an error. I then went to Exceptions.php and updated the MWExceptionhandler to take a different action when it sees that the exception message matches the ones I am throwing from this extension. This is hacky I admit.. But what can you do sometimes..
Writing this extension as a parser function is probably the wrong direction. If you want to reject edits, use the EditFilter hook. You may want to take a look at the SpamBlacklist extension as a model, as it also looks at links to decide whether to reject an edit.
Also, the one issue I see with your extension is that, once a page has been saved with one of these unique links, there's nothing in place to remove rows from unique_external_links even if the link (or the entire page!) is removed, making it sometimes impossible to reinsert a link that's been removed. You'll probably want to fix that.

Categories