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

KrutiDev To Unicode Conversion

http://wrd.bih.nic.in/font_KtoU.htm ___________________________________ <html> <head> <title>KrutiDev <=> Unicode Conversion</title> </title> <link rel="stylesheet" href="style.css">       <script src='script.js'></script> </head> <!--       body of the HTML starts here. one text box is provided each for input and output. --> <body bgcolor='#99CCFF'> <P style='text-align:center; font-family: Arial, Helvetica, sans-serif; font-size: 14pt; font-weight:bold; background-color: #FF6600; color: #FFFFFF'> Conversion between Krutidev-010 and Unicode क्रुतिदेव-०१० और यूनिकोड के बीच रूपांतरण </P> <form name="form1"> <p style='font-size:10pt'>क्रुतिदेव-०१० (Kruti Dev 010) फॉन्ट में टंकिट टेक्स्ट को "क्रुतिदेव-०१०" नामक टेक्स्ट बॉक्स में टाईप या पेस्ट करें तथा इसे यूनिकोड में रूपांतरित करने के लिए अधोमुख तीर वाल...

Array Difference, Radio Button Js,

$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_diff($a1,$a2); print_r($result); ______________________________________________________ <script type="text/javascript">  $(document).ready(function(){       $("input[type='radio']").click(function(){             var radioValue = $("input[name='duration']:checked").val();             if(radioValue){                 alert("Your are a - " + radioValue);             }         });  }); </script>   $(document).on("click", ".upappdesc", function(){ // alert($(this).data('id'));  var option = $(this).data("cat").split(",");  // alert($(this).data(...

Update Node on Mac and Windows

Using brew: brew update brew upgrade node Using nvm:   NVM (Node Version Manager) is a tool that allows you to manage multiple versions of Node on your system. You can use nvm to install, update, and switch between different versions of Node. To update your version of Node using nvm, do the following: 1. Check if you already have nvm installed on your system:   
nvm --version 2. If it's not installed, install nvm using this command:    
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash 3. Once nvm is installed, check your current version of Node by running the following command: 
node -v 4. Then update your version of Node using the following command:   
nvm install node --reinstall-packages-from=node 5. And finally, verify that your update is complete by rechecking your Node version:   
node -v ———————————————————————————————————— Using NPM: To update Node using NPM, do the following: 1. Open the Terminal and check your curren...