Skip to content

Instantly share code, notes, and snippets.

@Exadra37
Last active September 11, 2015 16:04
Show Gist options
  • Save Exadra37/8f97ebbec254e794734a to your computer and use it in GitHub Desktop.
Save Exadra37/8f97ebbec254e794734a to your computer and use it in GitHub Desktop.
Usefull Tools class
<?php
/**
*
* @author Exadra37
* @package exadra37/tools
* @version 1.0.0
* @since 1.0.3 - 19/09/2014
* 1.0.2 - 15/09/2014
* 1.0.1 - 15/09/2014
* 1.0.0 - 15/09/2014
*
*/
// display error for debugging
error_reporting(-1);
ini_set('display_errors', '1');
date_default_timezone_set("Europe/London");
class Tools
{
public static function getCurl($url, array $options)
{
$ch = curl_init();
if (isset($options['return_transfer'])) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, (bool)$options['return_transfer']);
} else {
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
// using cookies
if (!empty($options['cookie_file'])) {
curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_file']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_file']);
}
if (!empty($options['request'])) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $options['request']['type']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $options['request']['post_fields']);
}
if (!empty($options['timeout'])) {
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, (int)$options['timeout']);
}
if (!empty($options['http_header'])) {
curl_setopt($ch, CURLOPT_HTTPHEADER, (array)$options['http_header']);
}
if (!empty($options['user_agent'])) {
if ('default' == strtolower($options['user_agent'])) {
curl_setopt(
$ch,
CURLOPT_USERAGENT,
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17'
);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, $options['user_agent']);
}
}
// set url
curl_setopt($ch, CURLOPT_URL, $url);
session_write_close();
$response = curl_exec($ch);
// Get curl error
if (curl_error($ch)) {
$headerStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
$socketTimeout = ini_get('default_socket_timeout');
echo "\nCurl error: {$curlError}";
echo "\nHttp Header Status: {$headerStatus}\n";
echo "\nCurl Socket Timeout: {$socketTimeout} seconds";
}
curl_close($ch);
return $response;
}
public static function getFileExists($file)
{
try {
// fileexists results are cached by php,
// therefore we need to clean the cache just for this file,
// before we try to check if it exists
clearstatcache(true, $file);
return file_exists($file);
} catch (Exception $e) {
return self::handleException("getFileExists($file)", $e, true);
}
}
public static function getIsFile($file)
{
try {
// is_file results are cached by php,
// therefore we need to clean the cache just for this file,
// before we try to check if it exists
clearstatcache(true, $file);
return is_file($file);
} catch (Exception $e) {
return self::handleException("getIsFile($file)", $e, true);
}
}
public static function getFileSize($file)
{
try {
// filesize results are cached by php,
// therefore we need to clean the cache just for this file,
// before we try to get is size
clearstatcache(true, $file);
return filesize($file);
} catch (Exception $e) {
return self::handleException("getFileSize($file)", $e, true);
}
}
public static function getFilemTime($file)
{
try {
// filesize results are cached by php,
// therefore we need to clean the cache just for this file,
// before we try to get is modification date
clearstatcache(true, $file);
return filemtime($file);
} catch (Exception $e) {
return self::handleException("getFilemTime($file)", $e, true);
}
}
public static function saveToFile($file, $data, $echo = true)
{
try {
$bytes = file_put_contents("{$file}", $data, LOCK_EX);
if (empty($bytes)) {
throw new Exception("Failed to save file.");
};
if ($echo) {
echo "\nSaving to file: {$file}";
self::echoFileSize($file);
}
return $bytes;
} catch (Exception $e) {
return self::handleException("saveToFile($file)", $e, $echo);
}
}
public static function echoFileSize($file)
{
$fileSize = self::getFileSize($file);
echo "\nFile Size: {$fileSize} bytes";
}
/**
* take all the input variables and put it into the variable $INPUTS
* so, user can run the script with command line or browser by using the same parameter
* for commandline: php script.php VAR1=VALUE1 VAR2=VALUE2
*
* @author Alex
* @date 9 March 2012
*/
public static function getUrlInputs()
{
$INPUTS = array();
global $argv;
// ignore first argv as this is always script name
for($i = 1; $i < sizeof($argv); $i++) {
$keyAndValue = explode('=', $argv[$i]);
$key = $keyAndValue[0];
if(!isset($keyAndValue[1])) {
$value = '';
}
else {
$value = $keyAndValue[1];
}
//echo "ARGs : $key = $value\n";
$INPUTS[$key] = $value;
}
foreach($_REQUEST as $key => $value) {
$INPUTS[$key] = $value;
}
if (isset($_SESSION)) {
foreach($_SESSION as $key => $value) {
$INPUTS[$key] = $value;
}
}
return $INPUTS;
}
public static function getScriptStartTime()
{
return microtime(true);
}
public static function getScriptExecutionTime($start)
{
$end = microtime(true);
$execution_time = $end - $start;
return number_format($execution_time, 3, '.', '');
}
private static function handleException($method, $e, $echo)
{
$exception = $e->getMessage();
if ($echo) {
echo "\nTools::{$method} Error: {$exception}";
}
return $exception;
}
public function getRawQuery($query, array $bindings)
{
$placeHolders = array_keys($bindings);
$rawQuery = str_replace($placeHolders, $bindings, $query);
$rawQuery = trim(preg_replace('/[\s\t\n\r\s]+/', ' ', $rawQuery));
return $rawQuery;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment