Skip to main content

CONTROLLER PROJECT II

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH . '/libraries/API_Controller.php';

class Services extends API_Controller {
    
    public function __construct()
    {   
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        parent::__construct();

        //SMTP & mail configuration
        $config = array(
            'protocol'  => 'smtp',
            'smtp_host' => 'smtp.xxxxxxxxxxxxxxxxxxx.com',
            'smtp_port' =>  2525,
            'smtp_user' => 'xxxxxxxxxxxxxxxxxxx',
            'smtp_pass' => 'xxxxxxxxxxxxxxxxxxx',
            'mailtype'  => 'html',
            'charset'   => 'utf-8'
        );
        $this->email->initialize($config);
        $this->email->set_mailtype("html");
        $this->email->set_newline("\r\n");
        $this->email->from('no-reply@xxxx.com''xxxxxxxxxxxxxxxxxxx');

    }
    
    public function index()
    {
         $this->api_return(
            [ 'status' => true,
               'result' => "xxxxxxxxxxxxxxxxxxx"
            ],
        200);
    }

    
    ## SIGNUP
    public function signup()
    {

            $username = trim($this->input->get('username'));            
            $email = trim($this->input->get('email'));          
            $password = md5($this->input->get('password'));

            $whereuser = array(             
                'email' => $email,
            );
    
            $userexists$this->user_model->get_joins('users'$whereuser);
            if(!$userexists){
                $insertdata = array(
                    'username' => $username,                        
                    'email' => $email,
                    'password' => $password,                    
                    'date' => date('Y-m-d h:i:s'),                    

                );

            $insert = $this->user_model->INSERTDATA('users'$insertdata);

            if($insert){
                
                  $this->api_return(
                [ 'status' => true,
                   'result' => $insert
                ], 200);
            }
            else
            {
                $this->api_return(
                [ 'status' => false,
                   'result' => "Failed"
                ], 200);
            }


            }
            else{
                $this->api_return(
                    [ 'status' => false,
                       'result' => "Email Already Registered."
                    ], 200);
            }

    }

    #Sign In
    public function signin()
    {

            $email = trim($this->input->get('email'));
            $password = md5($this->input->get('password'));
            $column = array('id''username''email');
            $where = array(
                        'email' => $email,
                        'password' => $password
                    );
            $where_or = array(
                'username' => $email,                      
            );

            $user_info$this->user_model->get_joins('users'$where,''$column,'''''',''''$where_or);
            
              if($user_info){

                    $this->api_return(
                    [  
                        'status' => true,
                        'result' => $user_info
                    ], 200);
                }
                 else {

                $this->api_return(
                [ 'status' => false,
                   'result' => "Authentication Failed, try again!"
                ], 200);
            }


    }

## SELECT FILE IMAGE OR VIDEO
    public function openform()
    {
        $this->load->view('upload.php');
    }



    public function uploadmedia()
    {
        $userid = trim($this->input->get('userid'));
        $timestamp = trim($this->input->get('timestamp'));
        $lat = trim($this->input->get('lat'));
        $long = trim($this->input->get('long'));
        $altitude = trim($this->input->get('alt'));

        $filename = $_FILES['file']['name'];
        // $filetype = $_FILES['file']['type'];
        $filesize = $_FILES['file']['size'];
        $filetype = 'video/mp4';
        $videofile = $_FILES['file']['tmp_name'];

        $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/'''$filename);
        $target_path = "assets/uploads/";       
        $ffmpeg = "/usr/bin/ffmpeg";            

        $target_path = $target_path . basename($_FILES['file']['name']);    
        
        $size ="600x600";
        // $size ="1280x720";
        $getfromsecond = 2;
        $cmd = "$ffmpeg -i $videofile -an -ss $getfromsecond -s $size assets/thumbnails/$withoutExt.jpg";       
        shell_exec($cmd);
        
        // $compress_video = "$ffmpeg -i $videofile -f flv -s 320x240 assets/compress/$withoutExt.mp4";
        // shell_exec($compress_video);

        if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path) ) {

            $insertdata = array(
                            'userid' => $userid,
                            'filename' => $filename,
                            'type' => $filetype,
                            'timestamp' => $timestamp,
                            'altitude' => $altitude,
                            'longitude' => $long,
                            'latitude' => $lat,
                            'size' => $filesize,
                            'date' => date('Y-m-d h:i:s'),                    

                        );

            $insert = $this->user_model->INSERTDATA('library'$insertdata);

                if($insert){                
                    
                    $this->api_return(
                    [ 'status' => true,
                       'result' => 'Upload and Saved sucessfully.'
                    ], 200);

                }
                else
                {
                    $this->api_return(
                    [ 'status' => false,
                       'result' => 'Failed to save.'
                    ], 200);

                }


        }
        else{
            $this->api_return(
                [ 'status' => false,
                   'result' => 'Failed to upload file.'
                ], 20);
        }       


    }
    

    ## SAVE FILE IMAGE OR VIDEO
    public function savefile()
    {
        $userid = trim($this->input->get('userid'));
        $timestamp = trim($this->input->get('timestamp'));
        $lat = trim($this->input->get('lat'));
        $long = trim($this->input->get('long'));
        $altitude = trim($this->input->get('alt'));


        $filename = $_FILES['file']['name'];
        $filesize = $_FILES['file']['size'];    
        $filetype = $_FILES['file']['type'];        

        $target_path = "assets/uploads/";

        $target_path = $target_path . basename($_FILES['file']['name']); 

        $image_data = file_get_contents($_FILES['file']['tmp_name']);
        $encode_data = base64_encode($image_data);
                
        if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { 

        $insertdata = array(
                            'userid' => $userid,
                            'filename' => $filename,
                            'type' => $filetype,
                            'timestamp' => $timestamp,
                            'altitude' => $altitude,
                            'longitude' => $long,
                            'latitude' => $lat,
                            'size' => $filesize,
                            'date' => date('Y-m-d h:i:s'),
                            'encoded' => $encode_data                    

                        );

            $insert = $this->user_model->INSERTDATA('library'$insertdata);

                if($insert){
                    
                    $this->api_return(
                    [ 'status' => true,
                       'result' => 'Upload and Saved sucessfully.'
                    ], 200);

                }
                else
                {
                    $this->api_return(
                    [ 'status' => false,
                       'result' => 'Failed to save.'
                    ], 200);

                }
        } else {
                $this->api_return(
                [ 'status' => false,
                    'result' => 'Failed to upload file.'
                ], 200);
        }

    }


#get Image bas
public function getImageEncodeById()
{   
    $id = trim($this->input->get('id'));
    $feild = array('encoded');
    $where = array(     
        'id' => $id,
    );
    $encode$this->user_model->get_joins('library'$where''$feild);
    if($encode){
        $this->api_return(
        [  
            'status' => true,
            'result' => $encode
        ], 200);
    }
    else {
        $this->api_return(
        [  
            'status' => false,
            'result' => 'Content not found.'
        ], 200);
    }
}

#getAllMedia
public function getAllMediaById()
{

    $userid = trim($this->input->get('userid'));

        $where = array(     
            'userid' => $userid,
        );
        
        /* get_joins($tablename = '', $where = '', $joins = '', $columns = '', $like = '', $group_by = '', $order_by = '', $limit = '', $start = '', $where_or = '') */

        $media_info$this->user_model->get_joins('library'$where,'','','','','id DESC');

        if($media_info){

            $result = array(); 

            foreach ($media_info as $getmsg)
            {
                if($getmsg['type'] =='video/mp4'){
                    $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/'''$getmsg['filename']);
                    $link = stripslashes('http://ec2-35-164-160-178.us-west-2.compute.amazonaws.com/LifeRecord/assets/thumbnails/'.$withoutExt.'.jpg');
                    
                }else{
                    $link = stripslashes('http://ec2-35-164-160-178.us-west-2.compute.amazonaws.com/LifeRecord/assets/uploads/'.$getmsg['filename']);
                    
                }


                $data = array(
                    'id'    =>   $getmsg['id'],
                    'filename' =>   $getmsg['filename'],
                    'size'    =>   $getmsg['size'],
                    'type'    =>   $getmsg['type'],
                    'timestamp'    =>   $getmsg['timestamp'],
                    'altitude'    =>   $getmsg['altitude'],
                    'longitude'    =>   $getmsg['longitude'],
                    'latitude'    =>   $getmsg['latitude'],
                    'url'    =>  stripslashes('http://ec2-35-164-160-178.us-west-2.compute.amazonaws.com/LifeRecord/assets/uploads/'.$getmsg['filename']),
                    'thumb'    =>  $link,                   
                    'date'    =>   $getmsg['date'],
                    // 'encoded' => $getmsg['encoded']
                    );
            array_push($result,$data);
            }

            $this->api_return(
            [  
                'status' => true,
                'result' => $result
            ], 200);


                
            }
            else {

                $this->api_return(
                [ 'status' => false,
                'result' => "Failed to fetch results."
                ], 200);
            }

}


#getAllImages
public function getImagesById()
{

    $userid = trim($this->input->get('userid'));

            $where = array(
                    'type' => 'image/jpg',
                    'userid' => $userid,
                );

            $image_info$this->user_model->get_joins('library'$where,'','','','','id DESC');
              if($image_info){

                $result = array(); 
        
                foreach ($image_info as $getmsg)
                {
                    $data = array(
                        'id'    =>   $getmsg['id'],
                        'filename'    =>   $getmsg['filename'],
                        'type'    =>   $getmsg['type'],
                        'size'    =>   $getmsg['size'],
                        'url'    =>  stripslashes('http://ec2-35-164-160-178.us-west-2.compute.amazonaws.com/LifeRecord/assets/uploads/'.$getmsg['filename']),
                        'date'    =>   $getmsg['date'],
                    
                        );
                array_push($result,$data);
                }
        
                $this->api_return(
                [  
                    'status' => true,
                       'result' => $result
                ], 200);
                    
                }
                 else {

                $this->api_return(
                [ 'status' => false,
                   'result' => "Failed to fetch results."
                ], 200);
            }
            
}
#getAllVideos
public function getVideosById()
{

    $userid = trim($this->input->get('userid'));

    $where = array(
        'type' => 'video/mp4',
        'userid' => $userid,
    );  
    
    $video_info$this->user_model->get_joins('library'$where,'','','','','id DESC');
    
    if($video_info){

        $result = array(); 
        
        foreach ($video_info as $getmsg)
        {
            $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/'''$getmsg['filename']);

            $data = array(
                'id'    =>   $getmsg['id'],
                'filename'    =>   $getmsg['filename'],
                'type'    =>   $getmsg['type'],
                'size'    =>   $getmsg['size'],
                'url'    =>  stripslashes('http://ec2-35-164-160-178.us-west-2.compute.amazonaws.com/LifeRecord/assets/uploads/'.$getmsg['filename']),
                'date'    =>   $getmsg['date'],
                'thumb' => stripslashes('http://ec2-35-164-160-178.us-west-2.compute.amazonaws.com/LifeRecord/assets/thumbnails/'.$withoutExt.'.jpg'),
            
                );
        array_push($result,$data);
        }

        $this->api_return(
        [  
            'status' => true,
               'result' => $result
        ], 200);
      }
       else {

    $this->api_return(
    [ 'status' => false,
       'result' => "Failed to fetch results."
    ], 200);
}
}

#rename media 
public function renameMediaById()
{
    $id = trim($this->input->get('id'));
    $filename = trim($this->input->get('filename'));
    $newfilename = trim($this->input->get('newfilename'));
    
    $target_path = "assets/uploads/";
    
    if((!empty($id)) && (!empty($filename))  && (!empty($newfilename)) ){
        
        $where = array(
                       'id' => $id,
                       
                       );
        $updatedata = array(
                            'filename' => $newfilename,
                            'date' => date('Y-m-d h:i:s'),
                            );
        
        
        $update_info$this->user_model->UPDATEDATA('library'$where$updatedata);
        
        if($update_info  && rename$target_path.$filename$target_path.$newfilename)  ){
            $this->api_return(
                              [
                              'status' => true,
                              'result' => $id.' - '.$filename.' - '.$newfilename.'renamed.'
                              ], 200);
        }
        else{
            $this->api_return(
                              [
                              'status' => false,
                              'result' => "Update Failed."
                              ], 200);
        }
        
        
        
        
    }
    else {
        
        $this->api_return(
            [
            'status' => false,
            'result' => "Id with oldfilename and newfilename is mandatory."
            ], 200);
        
    }
}


#delete media
public function deleteMediaById()
{
    $id = trim($this->input->get('id'));
    $filename = trim($this->input->get('filename'));
    
    if((!empty($id)) && (!empty($filename))){
        
        $target_path = "assets/uploads/";
        $thumbnails_path = "assets/thumbnails/";
        
        $where = array(
                       'id' => $id,
                       );
        
        $delete_info$this->user_model->DELETEDATA('library'$where);
        
        if($delete_info && unlink($target_path.$filename) || unlink($thumbnails_path.$filename)){
            
            $this->api_return(
                              [
                              'status' => true,
                              'result' => 'Deleted Successfully.'
                              ], 200);
        }
        else{
            $this->api_return(
                              [
                              'status' => false,
                              'result' => "Record Deletion Failed."
                              ], 200);
        }
        
    }
    else{
        $this->api_return(
                          [
                          'status' => false,
                          'result' => "Id with filename is mandatory."
                          ], 200);
        
    }
    
}

    #Users
    public function users()
    {
        $user_info$this->user_model->get_joins('users');
          if($user_info){

                $this->api_return(
                [  
                    'status' => true,
                    'result' => $user_info
                ], 200);
            }
             else {

            $this->api_return(
            [ 'status' => false,
               'result' => "Records Empty"
            ], 200);
        }

    }

    #Preferences
    public function setPreferences()
    {
        $userid = trim($this->input->get('userid'));        
        $object = trim($this->input->get('object')); 

        $where = array(
            'userid' => $userid,
        );

        $user_info$this->user_model->get_joins('preferences'$where);
        if($user_info){

            $where_user = array(
                'userid' => $userid,
            );
            $updatedata = array(
                            'object' => $object,                        
                            'is_active' => 1,                       
                        );
            
            
            $update_info$this->user_model->UPDATEDATA('preferences'$where_user$updatedata);
            
            if($update_info ){
                $this->api_return(
                                [
                                'status' => true,
                                'result' => "preferences update sucessfully."
                                ], 200);
            }
            else{
                $this->api_return(
                                [
                                'status' => false,
                                'result' => "preferences update failed."
                                ], 200);
            }

        }
        else {

            $insertdata = array(
                'userid' => $userid,
                'object' => $object,                             
    
            );
    
            $insert = $this->user_model->INSERTDATA('preferences'$insertdata);
    
                if($insert){                
                    
                    $this->api_return(
                    [   'status' => true,
                        'result' => 'preferences Saved sucessfully.'
                    ], 200);
    
                }
                else
                {
                    $this->api_return(
                    [   'status' => false,
                        'result' => 'preferences Failed to save.'
                    ], 200);
    
                }
        }       
        

    }

    #Preferences
    public function getPreferences()
    {

        $userid = trim($this->input->get('userid'));
        $where = array(             
                'userid' => $userid,
            );

        $userpreferences$this->user_model->get_joins('preferences'$where);
        if($userpreferences){

              $this->api_return(
              [  
                'status' => true,
                'result' => $userpreferences
              ], 200);
            }
        else {

          $this->api_return(
          [ 'status' => false,
             'result' => "Preferences Empty"
          ], 200);
        }


    }

    # IS EMAIL EXISTS OF FORGOT PASSWORD 
    public function isEmailExists()
    {
        $email = trim($this->input->get('email'));
        $columns = array('id','username','email');
        $where = array(             
            'email' => $email,
        );

        $emailexists$this->user_model->get_joins('users'$where''$columns);
        if($emailexists){

            $this->api_return(
            [  
                'status' => true,
                'result' => $emailexists
            ], 200);
            }
        else {

        $this->api_return(
        [ 
            'status' => false,
            'result' => "Email does not exists."
        ], 200);
        }


    }

    # SEND LINK TO SET NEW PASSWORD OF USERID, EMAIL, USERNAME  
    public function forgotPasswordEmail()
    {
        $id       = $this->input->get('id');
        $username = ucwords($this->input->get('username'));
        $email    = $this->input->get('email');
        $links    = 'http://ec2-35-164-160-178.us-west-2.compute.amazonaws.com/LifeRecord/services/setNewPassword/' . base64_encode($id. '/' . base64_encode($username);
        $link     = str_replace("="" "$links);
        
        //Email content
        $htmlContent = '<table>
                        <tr>
                        <td> <img width="50px" src="' . base_url('assets/image/logo.png'. '"></td>
                        </tr>
                        <tr>
                        <td><b>Hello ' . $username . ',</b></td>
                        </tr>
                        <tr>
                        <td>We received a request to reset your password. Click the link below to set a new one:</td>
                        </tr>
                        <tr>
                            <td><a href="' . $link . '">Reset your password</a></td>
                        </tr>                    
                        <tr>
                        <td style="height:80px"> </td>
                        </tr>                    
                        <tr>
                            <td>Thank You</td>
                        </tr>                    
                    </table>';
        $this->email->to($email$username);
        $this->email->subject('Request For Forgot Password');
        $this->email->message($htmlContent);
        if($this->email->send()){
            $this->api_return(
                [ 
                    'status' => true,
                    'result' => "Email Sent"
                ], 200);
        }
        else {
            $this->api_return(
                [ 
                    'status' => false,
                    'result' => "Email sending failed."
                ], 200);
        }
       
    }

    # setNewPassword http://ec2-35-164-160-178.us-west-2.compute.amazonaws.com/LifeRecord/services/setNewPassword
    
    public function setNewPassword()
    {
        if(isset($_POST['change'])) {
            $password    = md5(trim($this->input->post('password')));
            $userbasedid = $this->uri->segment(3);
            $isusername  = $this->uri->segment(4);
            
            $username = base64_decode($isusername);
            $id  = base64_decode($userbasedid);
            
            
            $condition = array(
                'id' => $id,
                'username' => $username
            );
            
            $updatedata = array(
                'password' => $password,
                'date' => date('Y-m-d h:i:s'), 
            );
            
            $is_update = $this->user_model->UPDATEDATA('users'$condition$updatedata);
            if ($is_update) {
                $this->session->set_flashdata('message''Thank You. Password changed successfully.');
            } else {
                $this->session->set_flashdata('message''Please try again.');
            }
        }


        $this->load->view('updatepassword');
    }

    public function setFavourite()
    {
        $userid    = (trim($this->input->get('userid')));
        $mediaid    = (trim($this->input->get('mediaid')));

            $insertdata = array(
                'userid' => $userid,                        
                'mediaid' => $mediaid,                                                        
                'date' => date('Y-m-d h:i:s'),                    

            );

            $insert = $this->user_model->INSERTDATA('favourite'$insertdata);

            if($insert){
                
                $this->api_return(
                [ 
                    'status' => true,
                    'result' => $insert
                ], 200);
            }
            else
            {
                $this->api_return(
                [ 
                    'status' => false,
                    'result' => "Failed"
                ], 200);
            }
        
    }

    # send test email http://ec2-35-164-160-178.us-west-2.compute.amazonaws.com/LifeRecord/services/sendEmail 
    
    public function sendEmail()
    {
        $email = "tachnew3@gmail.com";
        $username = "Tachnew";

        //Email content
        $htmlContent = '<table>
        <tr>
        <td> <img width="50px" src="' . base_url('assets/image/logo.png'. '"></td>
    </tr>
                        <tr>
                        <td><b>Hello ' . $username . ',</b></td>
                        </tr>                        
                                         
                        <tr>
                        <td style="height:80px"> </td>
                        </tr>                    
                        <tr>
                            <td>Thank You</td>
                        </tr>                    
                    </table>';
        $this->email->to($email$username);
        $this->email->subject('Testing email');
        $this->email->message($htmlContent);
        if($this->email->send()){
            $this->api_return(
                [ 
                    'status' => true,
                    'result' => "Email Sent"
                ], 200);
        }
        else {
            $this->api_return(
                [ 
                    'status' => false,
                    'result' => "Email sending failed."
                ], 200);
        }
    }
  
}
?>

Comments

Popular posts from this blog

SETUP REST API IN CI

1. Create Rest_controller.php inside controllers and paste code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); require APPPATH . '/libraries/API_Controller.php'; class Rest_controller extends API_Controller { public function __construct() { parent::__construct(); } public function index() { $this->api_return(             [ 'status' => true,                'result' => "Welcome to Testservices."             ],         200); } } ?> 2. Create api.php inside config and paste code : <?php defined('BASEPATH') OR exit('No direct script access allowed'); /**  * API Key Header Name  */ $config['api_key_header_name'] = 'X-API-KEY'; /**  * API Key GET Request Parameter Name  */ $config['api_key_get_name'] = 'key'; /**  * API Key POST Request Parameter Name ...

NGrok Setup

 https://dashboard.ngrok.com/get-started/setup 1. Unzip to install On Linux or Mac OS X you can unzip ngrok from a terminal with the following command. On Windows, just double click ngrok.zip to extract it. unzip /path/to/ngrok.zip 2. Connect your account Running this command will add your authtoken to the default ngrok.yml configuration file. This will grant you access to more features and longer session times. Running tunnels will be listed on the endpoints page of the dashboard. ngrok config add-authtoken 1woFn9zVqcI4VeGuSIiN2VtmnPa_ZXuAuF1AAPkqApr7WVsQ 3. Fire it up Read the documentation on how to use ngrok. Try it out by running it from the command line: ngrok help To start a HTTP tunnel forwarding to your local port 80, run this next: ngrok http 80

API ( service ) Image or Video Upload

## SAVE  VIDEO public function uploadmedia() { $target_path = "assets/uploads/"; $target_path = $target_path . basename($_FILES['file']['name']); if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { $this->api_return( [ 'status' => true,    'result' => 'uploaded success' ], 200); } else{ $this->api_return( [ 'status' => false,    'result' => 'failed' ], 20); } } ## SAVE FILE IMAGE OR VIDEO public function savefile() { $filetype = $_FILES['file']['type']; if (strpos($filetype, 'image') !== false) { $type = 'image'; } if (strpos($filetype, 'video') !== false) { $type = 'video'; }         $filename = trim($_FILES['file']['name']); // $userid = trim($this->input->get('userid'));...