escaping - Laravel - htmlentities -


i making ajax requests laravel - reason custom function not escaping special characters. can't figure out why. have used exact same function in codeigniter , escapes output fine. of data getting returned fine js file - it's not escaping anything. here code:

public function store( request $request, $project_id ) {     //current logged in user.      $user_id = auth()->user()->id;       //get post inputs      $inputs = $request->all();       //make sure project id belongs current user.  stop adding task project isn't you.      $projectbelongstouser = project::find(1)->where('user_id', $user_id)->where('id', $project_id)->get();        //if project id , inputs provided - log them database, if not redirect home $errors.      if( $project_id && $inputs['description'] && $projectbelongstouser ) {          $task = new task;          $task->description = $inputs['description'];         $task->due_date    = $inputs['due_date'];         $task->priority    = $inputs['priority'];         $task->completed   = 0;         $task->order       = 0;         $task->user_id     = $user_id;         $task->project_id  = $project_id;         $task->save();          //get tasks         $tasks = task::where('user_id', $user_id)->where('project_id', $project_id)->orderby('description', 'asc')->get();          //sanitize tasks safe output         function sanitize_object_h( $array ) {             $array_modified = $array;              foreach( $array_modified $object ) {                 foreach( $object &$item ) {                     $item = htmlentities( $item, ent_quotes );                 }                 //end foreach             }             //end foreach             return $array_modified;          }         //end sanitize_object_h          $sanitized_tasks = sanitize_object_h( $tasks );          //return sanitized object.          echo json_encode( sanitize_object_h( $tasks ) );      } else {          echo "failed";         return;      }//end if    }//end store 

first off, have not fixed escaping. htmlentities should work, in opinion (and some others) don't need to. json_encode escapes characters needs make valid json. have tried improve readability of code.

laravel can lot of things want do.

public function store( request $request, $project_id ) {      if(!$project_id)         abort(404, "bad id");      // make sure inputs exist     $this->validate($request, [         'description' => 'required',         'due_date' => 'required',         'priority' => 'required'     ]);      //get post inputs     $inputs = $request->all();      //make sure project id belongs current user.  stop adding task project isn't you.      $project = project::findorfail($project_id);     if($project->user_id != auth::user()->id)         abort(403, 'not thing');      $task = new task;      $task->description = $inputs['description'];     $task->due_date    = $inputs['due_date'];     $task->priority    = $inputs['priority'];     $task->completed   = 0;     $task->order       = 0;     $task->user_id     = $user_id;     $task->project_id  = $project_id;     $task->save();      //get tasks     $tasks = task::where('user_id', $user_id)->where('project_id', $project_id)->orderby('description', 'asc')->get();      return response::json($tasks); }//end store 

take @ validation

pay attention abort. think it's rather obvious does, can use return "error"; if like, since looks api. findorfail. if record doesn't exist throw 404 (unless catch it).


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -