Advertisement
Guest User

Cake 1.3 app_error

a guest
Oct 3rd, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.72 KB | None | 0 0
  1. <?php
  2.  
  3. class AppError extends ErrorHandler {
  4.  
  5.  
  6.     function missingController($params){
  7.         //echo 'missing controller';
  8.         $this->error404($params);
  9.     }
  10.    
  11.    
  12.     function missingAction($params){//if debug is greater than 0 this will kick in. Production should be 0 (which will error404())
  13.         echo 'missing action';
  14.         //$this->error404($params);
  15.     }  
  16.    
  17.     function error404($params){
  18.    
  19.         extract($params);
  20.         //fb($params);
  21.         App::import('Tracking');
  22.         //App::import('Session');
  23.         $track = new TrackingComponent;
  24.    
  25.         //fb($url,'non-normalized');   
  26.         $url = Router::normalize($url);
  27.             //fb($url,'normalized');
  28.         //get source
  29.         $source = $track->getAliasSource($url);
  30.         if($source){//does this url exist in the alias database? if so get source
  31.             //fb($source,'SourceCode');
  32.             $redirectURL = $track->getAliasURL($source);
  33.             //fb('The alias URL for '.$source.' is '.$redirectURL);                                            
  34.         } else {//theres no source/alias - check for alternative URL using keywords table eg. 'handsets' 'worldphone'
  35.             //fb($url,'The is no source,Checking For substitute');
  36.             $redirectURL = $track->substituteURL($url);
  37.             //as its not a true page, log it in the 404 'broken links' table
  38.             //($redirectURL) ? fb($redirectURL,'Yes') : fb('No Substitute, 404 iminent') ;
  39.             //fb('Tracking '.$url.' as a broken link in 404 table');
  40.             //$track->track404($url);
  41.         }
  42.         //so at this point we should have a redirectURL, or if not it really is a 404.
  43.         //if we do have a redirect url...
  44.         if($redirectURL) {
  45.                 //fb('Theres a redirect URL',$redirectURL);
  46.                 //check visitors ip address to see if its one of ours
  47.                 $this->controller->Session->write("ourIP",$ourIP = $track->checkIP( env('REMOTE_ADDR') ));
  48.                 //fb($ourIP,'Have just checked ourIP and written to session value');
  49.            
  50.                 if(!$ourIP){//its not one of ours, treat as a customer.
  51.                     //fb('Its not our IP, so its a customer');
  52.                     //check to see if they have been dealt with yet - is it a new visit?
  53.                     if(!$this->controller->Session->check("visitor")){//new visitor
  54.                         //fb('Theres no session value set');
  55.                         //track visitor in visitors table (user agent, ip etc)
  56.                         //fb('Its a customer, there is a redirect URL, and this is their first page on the site. We track them.');                 
  57.                         //is there a visitor cookie?
  58.                         if($this->controller->Cookie->read('visitor')){
  59.                             //yes
  60.                             $visitorId = $track->trackVisitor($url,$this->controller->Cookie->read('visitor'));
  61.                         }else{
  62.                             //no
  63.                             //insert visitor and grab visitorId ready to write into session and cookie
  64.                             $visitorId = $track->trackVisitor($url);
  65.                         }
  66.                         //fb('writing cookie');
  67.                         $this->controller->Cookie->name = 'Mobal';
  68.                         $this->controller->Cookie->time = 3600; // or '1 hour'
  69.                         $this->controller->Cookie->domain = '.mobal.com';//env('SERVER_NAME');
  70.                         $this->controller->Cookie->write('visitor',$visitorId,false,'90 days');
  71.                         //fb('setting session');
  72.                         $this->controller->Session->write('visitor',$visitorId);               
  73.                    
  74.                         //track visit & write session
  75.                         //check for source
  76.                                        
  77.                         if($source){//track source
  78.                             //fb($source,'Track dat source homey');
  79.                             $track->trackSource($visitorId,$source);
  80.                             //fb('Source Tracked! using '.$visitorId);
  81.                         }                  
  82.                     } //else { fb('Already been, so dont track');}
  83.                 }
  84.             //redirect
  85.             //fb('301 Redirecting to '.$redirectURL);
  86.             $this->controller->redirect($redirectURL,301);
  87.    
  88.             }else{ //no urls match:404 time
  89.                 $track->track404($url);
  90.                 $this->controller->layout = '404';
  91.                 $this->controller->set(array(
  92.                     'url' => $url,
  93.                     'title' =>'Page cannot be found'
  94.                 ));
  95.                 header("HTTP/1.0 404 Not Found");
  96.                 $this->_outputMessage('missing');
  97.             }
  98.         }
  99.     }
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement