File: /var/www/vhosts/onlinedev.com/httpdocs/admin/app.php
<?php
require_once('framework.php');
/* Start Application */
class News extends Model
{
public function __construct()
{
$this->tableName = 'news';
$this->tableId = 'id';
$this->tableFields = array('id', 'parent_id', 'created_at', 'title', 'body', 'url');
}
static function getById($id)
{
$n = new News();
$n->get($id);
return $n;
}
public function getChildren()
{
$n = new News();
$n->parent_id = $this->id;
return $n->getArray();
}
/**
* @param string $url
* @return News
*/
public static function findByUrl($url)
{
if (!$url)
{
return false;
}
$sql = ' SELECT id FROM news WHERE url = "%s" ';
$sql = sprintf($sql, mysql_escape_string($url));
$db = DBFactory::getInstance();
$db->query($sql);
if ($id = $db->getValue())
{
$model = new News();
$model->get($id);
return $model;
}
else
{
return false;
}
}
}
class Config extends Model
{
public function __construct()
{
$this->tableName = 'config';
$this->tableId = 'id';
$this->tableFields = array('id', 'config_key', 'config_val');
}
public static function getValue($key)
{
$db = DBFactory::getInstance();
$sql = 'SELECT * FROM config WHERE config_key="'.addslashes($key).'"';
$db->query($sql);
$row = $db->getRow();
if (!empty($row))
{
return $row['config_val'];
}
return false;
}
public static function setValue($key, $val)
{
$db = DBFactory::getInstance();
$sql = 'SELECT * FROM config WHERE config_key="'.addslashes($key).'"';
$db->query($sql);
$row = $db->getRow();
if (!empty($row))
{
$sql = 'UPDATE config set config_val="'.addslashes($val).'" WHERE config_key="'.addslashes($key).'"';
$db->query($sql);
}
else
{
$sql = 'INSERT INTO config(config_key, config_val) VALUES("'.addslashes($key).'", "'.addslashes($val).'")';
$db->query($sql);
}
}
}
class Project extends Model
{
public function __construct()
{
$this->tableName = 'projects';
$this->tableId = 'id';
$this->tableFields = array('id', 'created_at', 'title', 'description', 'image', 'body', 'url');
}
static function getById($id)
{
$n = new Project();
$n->get($id);
return $n;
}
/**
* @param string $url
* @return News
*/
public static function findByUrl($url)
{
if (!$url)
{
return false;
}
$sql = ' SELECT id FROM projects WHERE url = "%s" ';
$sql = sprintf($sql, mysql_escape_string($url));
$db = DBFactory::getInstance();
$db->query($sql);
if ($id = $db->getValue())
{
$model = new Project();
$model->get($id);
return $model;
}
else
{
return false;
}
}
public function getRandom($count)
{
return $this->getArray(0, $count, 'RAND()');
}
}
class Site extends Model
{
public function __construct()
{
$this->tableName = 'sites';
$this->tableId = 'id';
$this->tableFields = array('id', 'created_at', 'title', 'description', 'image', 'body', 'url');
}
static function getById($id)
{
$n = new Site();
$n->get($id);
return $n;
}
/**
* @param string $url
* @return Site
*/
public static function findByUrl($url)
{
if (!$url)
{
return false;
}
$sql = ' SELECT id FROM sites WHERE url = "%s" ';
$sql = sprintf($sql, mysql_escape_string($url));
$db = DBFactory::getInstance();
$db->query($sql);
if ($id = $db->getValue())
{
$model = new Site();
$model->get($id);
return $model;
}
else
{
return false;
}
}
}
class NewsController extends Controller
{
public $users = array('admin', 'onlinedev', 'news');
public function indexAction()
{
return $this->listAction();
}
public function listAction()
{
$this->loadMasterTemplate('master');
$model = new News();
if ($this->getRequest('parent_id'))
{
$model->parent_id = $this->getRequest('parent_id');
}
else $model->parent_id = '0';
$this->start = $this->getRequest('start') ? $this->getRequest('start') : 0;
$this->news = $model->getArray($this->start, 20, 'created_at desc');
$this->page = floor($this->start / 20) + 1;
$this->totalitems = $model->count();
$this->totalpages = ceil($this->totalitems / 20);
return $this->loadTemplate('list');
}
public function editAction()
{
$this->loadMasterTemplate('master');
$this->news = new News();
if ($this->getRequest('id'))
{
$this->news->get($this->getRequest('id'));
}
if ($this->getRequest('sent'))
{
$this->news->title = $this->getRequest('title');
$this->news->body = $this->getRequest('body');
$this->news->parent_id = intval($this->getRequest('parent_id'));
if (!$this->news->id)
{
$this->news->created_at = date('Y-m-d H:i:s');
} else {
$this->news->created_at = $this->getRequest('created_at');
}
$this->news->url = $this->getRequest('url') ? stringToUrl($this->getRequest('url')) : stringToUrl($this->getRequest('title'));
$this->news->save();
$this->redirect('news', 'list', array('saved' => 1, 'parent_id' => $this->news->parent_id));
}
return $this->loadTemplate('edit');
}
public function deleteAction()
{
$this->news = new News();
if ($this->getRequest('id'))
{
$this->news->get($this->getRequest('id'));
$parent_id = $this->news->parent_id;
$this->news->delete();
}
$this->redirect('news', 'list', array('deleted' => 1, 'parent_id' => $parent_id));
}
public function editcarrieresAction()
{
$this->loadMasterTemplate('master');
$this->content = Config::getValue('carrieres');
$this->title = Config::getValue('carrieres_title');
if ($this->getRequest('sent'))
{
Config::setValue('carrieres', $this->getRequest('content'));
Config::setValue('carrieres_title', $this->getRequest('title'));
$this->redirect('news', 'editcarrieres', array('saved' => 1));
}
return $this->loadTemplate('editcarrieres');
}
}
class ProjectsController extends Controller
{
public $users = array('admin', 'onlinedev');
public function indexAction()
{
return $this->listAction();
}
public function listAction()
{
$this->loadMasterTemplate('master');
$model = new Project();
$this->start = $this->getRequest('start') ? $this->getRequest('start') : 0;
$this->items = $model->getArray($this->start, 20, 'created_at desc');
$this->page = floor($this->start / 20) + 1;
$this->totalitems = $model->count();
$this->totalpages = ceil($this->totalitems / 20);
return $this->loadTemplate('projects');
}
public function editAction()
{
$this->loadMasterTemplate('master');
$this->item = new Project();
if ($this->getRequest('id'))
{
$this->item->get($this->getRequest('id'));
}
if ($this->getRequest('sent'))
{
$this->item->title = $this->getRequest('title');
$this->item->url = $this->getRequest('url') ? stringToUrl($this->getRequest('url')) : stringToUrl($this->getRequest('title'));
$this->item->description = $this->getRequest('description');
$this->item->body = $this->getRequest('body');
if (!$this->item->id)
{
$this->item->created_at = date('Y-m-d H:i:s');
}
else {
$this->item->created_at = $this->getRequest('created_at');
}
if(hasFile('image')) {
$this->item->image = upload('image', 'projects');
// echo "image"; exit;
}
//echo "pas image"; exit;
$this->item->save();
$this->redirect('projects', 'list', array('saved' => 1));
}
return $this->loadTemplate('editproject');
}
public function deleteAction()
{
$this->item = new Project();
if ($this->getRequest('id'))
{
$this->item->get($this->getRequest('id'));
$this->item->delete();
}
$this->redirect('projects', 'list', array('deleted' => 1));
}
}
class SitesController extends Controller
{
public $users = array('admin', 'onlinedev');
public function indexAction()
{
return $this->listAction();
}
public function listAction()
{
$this->loadMasterTemplate('master');
$model = new Site();
$this->start = $this->getRequest('start') ? $this->getRequest('start') : 0;
$this->items = $model->getArray($this->start, 20, 'id desc');
$this->page = floor($this->start / 20) + 1;
$this->totalitems = $model->count();
$this->totalpages = ceil($this->totalitems / 20);
return $this->loadTemplate('sites');
}
public function editAction()
{
$this->loadMasterTemplate('master');
$this->item = new Site();
if ($this->getRequest('id'))
{
$this->item->get($this->getRequest('id'));
}
if ($this->getRequest('sent'))
{
$this->item->title = $this->getRequest('title');
$this->item->url = $this->getRequest('url') ? stringToUrl($this->getRequest('url')) : stringToUrl($this->getRequest('title'));
$this->item->description = $this->getRequest('description');
$this->item->body = $this->getRequest('body');
if (!$this->item->id)
{
$this->item->created_at = date('Y-m-d H:i:s');
}
if(hasFile('image')) {
$this->item->image = upload('image', 'sites');
}
$this->item->save();
$this->redirect('sites', 'list', array('start'=>$this->getRequest('start'), 'saved' => 1));
}
return $this->loadTemplate('editsite');
}
public function deleteAction()
{
$this->item = new Site();
if ($this->getRequest('id'))
{
$this->item->get($this->getRequest('id'));
$this->item->delete();
}
$this->redirect('sites', 'list', array('deleted' => 1));
}
}
function format_date($date)
{
preg_match('/([\d]+)\-([\d]+)\-([\d]+)\s([\d]+)\:([\d]+)\:([\d]+)/', $date, $matches);
return date('d/m/Y H:i:s', mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]));
}
function clean($str)
{
$str = str_replace(array("�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", "�", ' '),
array("A", "a", "A", "a", "A", "a", "C", "c", "E", "e", "E", "e", "E", "e", "I", "i", "I", "i", "I", "i", "O", "O", "O", "o", "O", "o", "U", "u", "U", "u", '_'),
$str);
$str = preg_replace('/[^A-Za-z0-9_\-\.]/', '', $str);
return $str;
}
function stringToUrl($string)
{
$res = preg_replace('/\s+/', '_', trim($string));
$res = clean($res);
return $res;
}
function hasFile($file)
{
if (isset($_FILES[$file]) && $_FILES[$file]['tmp_name'] && file_exists($_FILES[$file]['tmp_name']))
{
return true;
}
else
{
return false;
}
}
function upload($file, $upload_subdir = false)
{
$fileAr = $_FILES[$file];
if (!$fileAr) return false;
$filename = $fileAr['name'];
if (!$filename) return false;
$upload_dir = getUploadPath();
if ($upload_subdir)
{
$upload_dir .= '/' . $upload_subdir;
}
$newfile = getUniqueName($upload_dir.'/'.$filename);
move_uploaded_file($fileAr['tmp_name'], $newfile);
return basename($newfile);
}
function getThumb($image, $width, $height, $type='fixed')
{
if (!$image) return '';
if (!$width && !$height)
{
$img = getUploadDir().$image;
}
else
{
//if image is in subdir compose again url due the stingToUrl function remove the slashes.
$imagename_resized = substr($image, 0, strrpos($image, '/')) . (strrpos($image, '/') !== false ? '/' : '') . stripExtension(stringToUrl(substr($image, strrpos($image, '/')))).'_'.$width.'_'.$height.'.'.getFileExtension($image) ;
//$imagename_resized = stripExtension(stringToUrl($image)).'_'.$width.'_'.$height.'.'.getFileExtension($image);
if (is_file(getUploadPath().'/'.$imagename_resized))
{
$img = getUploadDir().$imagename_resized;
}
else
{
$result = @createThumb(getUploadPath().'/'.$image, getUploadPath().'/'.$imagename_resized, $width, $height, $type);
$img = getUploadDir().$imagename_resized;
}
}
return $img;
}
function getUploadPath()
{
return strpos(App::getBasePath(), '/admin') === false ? App::getBasePath().'/admin/uploads' : App::getBasePath().'/uploads';
//return strpos(App::getBasePath(), '/admin/') === false ? App::getBasePath().'/uploads' : str_replace('/admin/', '/', App::getBasePath()).'uploads';
}
function getUploadDir()
{
return strpos(App::getBaseUrl(), '/admin/') === false ?App::getBaseUrl().'admin/uploads/' : App::getBaseUrl().'uploads/';
//return App::getBaseUrl().'uploads/';
// return strpos(App::getBaseUrl(), '/admin/') === false ? App::getBaseUrl().'uploads/' : str_replace('/admin/', '/', App::getBaseUrl()).'uploads/';
}
function getUniqueName($file)
{
if (is_file($file))
{
$path = dirname($file);
$newfile = stripExtension($file).'_'.rand(1, 999).(strpos($file, '.') ? '.'.getFileExtension($file) : '');
return getUniqueName($path.'/'.$newfile);
}
else
{
return $file;
}
}
function getFileExtension($file)
{
$parts = explode('.', $file);
if (count($parts) > 1)
{
return array_pop($parts);
}
else
{
return $file;
}
}
function stripExtension($file, $fullpath = false)
{
if (strpos($file, '.'))
{
$parts = explode('.', $file);
array_pop($parts);
if ($fullpath) return implode('.', $parts);
else return basename(implode('.', $parts));
}
else
{
return ($fullpath ? $file : basename($file));
}
}
function createThumb($IMAGE_SOURCE, $IMAGE_DEST_FILE, $THUMB_X, $THUMB_Y, $thumbType = "fixed")
{
if(!$thumbType) $thumbType = "fixed";
$BACKUP_FILE = $IMAGE_SOURCE;
$IMAGE_PROPERTIES = getimagesize($BACKUP_FILE);
//get mimetype
$type = split("/", $IMAGE_PROPERTIES['mime']);
$type = $IMAGE_PROPERTIES[2];
switch ($type)
{
case IMAGETYPE_JPEG:
case IMAGETYPE_JPEG2000:
$imagecreatefn = 'imagecreatefromjpeg';
break;
case IMAGETYPE_PNG:
$imagecreatefn = 'imagecreatefrompng';
break;
case IMAGETYPE_GIF:
$imagecreatefn = 'imagecreatefromgif';
break;
case IMAGETYPE_BMP:
$imagecreatefn = 'imagecreatefromwbmp';
break;
default:
return false;
break;
}
switch (strtolower(getFileExtension($IMAGE_DEST_FILE)))
{
case 'jpg':
case 'jpeg':
$imagefn = 'imagejpeg';
break;
case 'png':
$imagefn = 'imagepng';
break;
case 'gif':
$imagefn = 'imagegif';
break;
case 'bmp':
$imagefn = 'image2wbmp';
break;
default:
return false;
break;
}
$SRC_IMAGE = call_user_func($imagecreatefn, $BACKUP_FILE);
$SRC_X = imagesx($SRC_IMAGE);
$SRC_Y = imagesy($SRC_IMAGE);
switch($thumbType)
{
case "fixed":
if (($THUMB_Y == "0") && ($THUMB_X == "0"))
{
return(0);
}
elseif ($THUMB_Y == "0")
{
$SCALEX = $THUMB_X/($SRC_X-1);
$THUMB_Y = $SRC_Y*$SCALEX;
}
elseif ($THUMB_X == "0")
{
$SCALEY = $THUMB_Y/($SRC_Y-1);
$THUMB_X = $SRC_X*$SCALEY;
}
$THUMB_X = (int)($THUMB_X);
$THUMB_Y = (int)($THUMB_Y);
break;
case "dynamic":
if (($THUMB_Y == "0") && ($THUMB_X == "0")) {
return(0);
} elseif ($THUMB_Y == "0") {
$SCALEX = $THUMB_X/($SRC_X-1);
$THUMB_Y = $SRC_Y*$SCALEX;
} elseif ($THUMB_X == "0") {
$SCALEY = $THUMB_Y/($SRC_Y-1);
$THUMB_X = $SRC_X*$SCALEY;
} else
{
$scaleX = $THUMB_X/($SRC_X-1);
$scaleY = $THUMB_Y/($SRC_Y-1);
$scale = $scaleX < $scaleY ? $scaleX : $scaleY;
$THUMB_X = $SRC_X * $scale;
$THUMB_Y = $SRC_Y * $scale;
}
$THUMB_X = (int)($THUMB_X);
$THUMB_Y = (int)($THUMB_Y);
break;
}
$DEST_IMAGE = imagecreatetruecolor($THUMB_X, $THUMB_Y);
if (!imagecopyresampled($DEST_IMAGE, $SRC_IMAGE, 0, 0, 0, 0, $THUMB_X, $THUMB_Y, $SRC_X, $SRC_Y))
{
imagedestroy($SRC_IMAGE);
imagedestroy($DEST_IMAGE);
return(0);
}
else
{
imagedestroy($SRC_IMAGE);
if (call_user_func_array($imagefn, array($DEST_IMAGE, $IMAGE_DEST_FILE)))
{
imagedestroy($DEST_IMAGE);
return(1);
}
}
}
/* End Application */