Files
ChiefSoftNet2022/app/Controllers/BaseController.php
T
2024-11-20 05:19:57 -05:00

198 lines
6.4 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
/**
* Class BaseController
*
* BaseController provides a convenient place for loading components
* and performing functions that are needed by all your controllers.
* Extend this class in any new controllers:
* class Home extends BaseController
*
* For security be sure to declare any new methods as protected or private.
*/
class BaseController extends Controller
{
/**
* Instance of the main Request object.
*
* @var CLIRequest|IncomingRequest
*/
protected $request;
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var array
*/
protected $helpers = [];
/**
* Constructor.
*/
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
// Do Not Edit This Line
parent::initController($request, $response, $logger);
// Preload any models, libraries, etc, here.
// E.g.: $this->session = \Config\Services::session();
}
public function APIcall($method, $url, $data) {
// $curl = curl_init();
$curl = curl_init($url);
switch ($method) {
case "GET":
$params2 = '';
foreach($data as $key2=>$value2)
$params2 .= $key2.'='.$value2.'&';
$params2 = trim($params2, '&');
$url = $url.'?'.$params2;// add param to URL
log_message('critical', "API URL FINAL =>".$url );
//curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
break;
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
// curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
// curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
break;
case "PUT":
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'APIKEY: RegisteredAPIkey',
'Content-Type: application/json',
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result) {
echo("Connection failure!");
}
curl_close($curl);
return json_decode($result, true);
}
public function blogData()
{
/*
// Create the logger
$logger = new Logger('test_logger');
// Create Graylog GELF TCP transport
$transport = new Gelf\Transport\TcpTransport("10.0.0.112",12201);
// Create GELF handler
$handler = new GelfHandler(new Gelf\Publisher($transport));
// Now add GELF handler to logger
$logger->pushHandler($handler);
// Create the logger
$this->logger = new Logger($_SERVER['SERVER_ADDR']);
// Create Graylog GELF TCP transport
$transport = new Gelf\Transport\TcpTransport("10.0.0.112",12201);
// Create GELF handler
$handler = new GelfHandler(new Gelf\Publisher($transport));
// Now add GELF handler to logger
$this->logger->pushHandler($handler);
// You can now use your logger
$logger->info('My logger is now ready');
$cache = \Config\Services::cache();
$blog_array_cache = $cache->get('blog_array');
var_dump($cache->getMetadata('ameye'));
*/
$data=array();
$this->db = \Config\Database::connect($this->con_name);
try {
$mysql = "SELECT id, post_title, post_content,post_date,comment_count FROM wp_posts WHERE post_type='post' AND post_status = 'publish' ORDER BY post_date DESC LIMIT 5";
$mysql = "SELECT p1.id AS id, p1.*, wm2.meta_value FROM wp_posts p1 LEFT JOIN wp_postmeta wm1
ON (wm1.post_id = p1.id AND wm1.meta_value IS NOT NULL AND wm1.meta_key = '_thumbnail_id' )
LEFT JOIN
wp_postmeta wm2
ON (wm1.meta_value = wm2.post_id AND wm2.meta_key = '_wp_attached_file' AND wm2.meta_value IS NOT NULL )
WHERE
p1.post_status='publish'
AND p1.post_type='post'
ORDER BY p1.post_date DESC LIMIT 5";
$query = $this->db->query($mysql);
$row = $query->getResultArray();
// print_r( $row );
// $r = $this->db->query($mysql);
$data["blog_array"] = $row;
} catch (Exception $ex) {
}
if (! $blog_array_cache = cache('blog_array')) {
// echo 'Saving to the cache!<br />';
// $foo = 'foobarbaz!';
// Save into the cache for 5 minutes
// cache()->save('blog_array', $data["blog_array"], 300);
// cache()->save('ameye', 'testing_ameye', 300);
}
// var_dump($cache->getCacheInfo());
// var_dump($cache->redis->is_supported());
// featured
try {
$mysql = "SELECT id, post_title, post_content,post_date,comment_count
FROM wp_posts WHERE post_type='post' AND post_status = 'publish' AND id = 263";
$query = $this->db->query($mysql);
$rowF = $query->getResultArray();
// print_r( $rowF );
// $r = $this->db->query($mysql);
$data["blog_featured"] = $rowF[0];
} catch (Exception $ex) {
}
//return view('welcome_message');
return $data;
}
}