GOOGLE

aku nok ndi : /home/astwardha/public_html/pdf_generator/
File Up :
aku nok ndi : /home/astwardha/public_html/pdf_generator/action.php

<?php
session_start();
require_once('./db-connect.php');
Class Action{
    public $conn;
    function __construct(){
        global $conn;
        $this->conn = $conn;
    }
    function __destruct(){
        $this->conn->close();
    }
    public function save_file(){
        extract($_POST);
        $data = "";
        foreach($_POST as $k => $v){
            if(!is_array($_POST[$k]) && !in_array($k,['id','content'])){
                $v = $this->conn->real_escape_string($v);
                if(!empty($data)) $data .= ", ";
                $data .= " `{$k}` = '{$v}'";
            }
        }
        $check = $this->conn->query("SELECT * FROM `file_list` where `name` = '{$name}' ".(!empty($id) ? " and id != '{$id}' " : '')." ")->num_rows;
        if($check > 0){
            $resp['status'] = 'failed';
            $resp['msg'] = "{$name} File Name already exists.";
        }else{
            if(empty($id)){
                $sql = "INSERT INTO `file_list` set {$data}";
            }else{
                $sql = "UPDATE `file_list` set {$data} where id = '{$id}'";
            }
            $save = $this->conn->query($sql);
            if($save){
                $fid = empty($id) ? $this->conn->insert_id : $id ;
                $get = $this->conn->query("SELECT * FROM `file_list` where id = '{$id}'")->fetch_array();
                $filename = strtolower(str_replace(' ', '_', $name));
                $cfname = 'contents/'.$filename.'.html';
                $pfname = 'pdfs/'.$filename.'.pdf';
                $put_content = file_put_contents($cfname, $content);
                if($put_content){
                    $this->conn->query("UPDATE `file_list` set file_path = '{$cfname}' where id = '{$fid}'");
                }
                if(isset($get['pdf_path'])){
                    unlink("./".$get['pdf_path']);
                }
                $make_pdf  = $this->create_pdf_file($fid, $pfname);
                if($make_pdf)
                    $this->conn->query("UPDATE `file_list` set pdf_path = '{$pfname}' where id = '{$fid}'");
                $_SESSION['msg']['type'] = 'success';
                if(empty($id))
                    $_SESSION['msg']['text'] = 'File has been added successfully.';
                else
                    $_SESSION['msg']['text'] = 'File has been updated successfully.';
                $resp['status'] = 'success';
            }else{
                $resp['status'] = 'failed';
                $resp['msg'] = "{$name} File Name already exists.";
            }
        }
        return json_encode($resp);
    } 

    function create_pdf_file($fid, $pfname){
        if(!empty($fid)){
            $qry = $this->conn->query("SELECT * FROM `file_list` where id = '{$fid}' ");
            foreach($qry->fetch_array() as $k => $v){
                if(!is_numeric($k)){
                    $$k = $v;
                }
            }
            try{
                include_once 'generate_pdf.php';
                return true;
            }catch(Exception $e){
                return false;

            }
        }else{
            return false;
        }
    }
    function delete_file(){
        extract($_POST);
        $qry = $this->conn->query("SELECT * FROM `file_list` where id = '{$id}' ");
        if($qry->num_rows > 0){
            $data = $qry->fetch_array();
            $delete = $this->conn->query("DELETE FROM `file_list` where id = '{$id}'");
            if($delete){
                if(is_file($data['file_path']))
                    unlink($data['file_path']);
                if(is_file($data['pdf_path']))
                    unlink($data['pdf_path']);
                $_SESSION['msg']['type'] = 'success';
                $_SESSION['msg']['text'] = 'File has been deleted successfully.';
                $resp['status'] = 'success';
            }
        }else{
            $resp['status'] ='failed';
            $resp['msg'] ='Unknown File ID';
        }
        return json_encode($resp);

    }
} 

$action  = new Action();
$a = isset($_GET['action']) ? $_GET['action'] : '';
switch($a){
    case 'save_file':
        echo $action->save_file();
        break;
    case 'delete_file':
        echo $action->delete_file();
        break;
    default:
        echo 'No Action';
        break;
}

Copyright © 1945 - 2024 GOOGLE