7r1n17y
New Member
- Jun 11, 2017
- 13
- 5
Hey i am looking t create a new php framework this is to provide simple, reusable, and secure code. Right now i have installed a couple composer packages and created some classes/interfaces and i am currently setup a custom made router. If anyone is interested in contributing email me at [email protected]
Code snippets:
Code snippets:
PHP:
<?php
namespace App\r7r1n17y\Framework;
class Session implements ISessionInterface {
private $cookie;
public function __construct(Cookie $cookie) {
$this->cookie = $cookie;
}
public function start() {
session_start();
if (!$this->__lock()) {
$this->destroy();
redirect('/');
}
}
public function destroy() {
$_SESSION = array();
$params = session_get_cookie_params();
$this->cookie->delete(session_name());
session_destroy();
}
public function regenerate($deleteOldSession = true) {
return session_regenerate_id($deleteOldSession);
}
public function set($key, $value) {
$_SESSION[$key] = $value;
}
public function delete($key) {
if (isset($_SESSION[$key])) {
unset($_SESSION[$key]);
}
}
public function get($key, $default = null) {
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
}
return $default;
}
private function __lock() {
if (!isset($_SERVER['HTTP_USER_AGENT'])) {
return false;
}
if (is_null($this->get('userIp')) || is_null($this->get('userAgent'))) {
$_SESSION = array();
$this->set('userIp', $_SERVER['REMOTE_ADDR']);
$this->set('userAgent', $_SERVER['HTTP_USER_AGENT']);
return true;
} elseif (
!equals($this->get('userIp'), $_SERVER['REMOTE_ADDR'])
|| !equals($this->get('userAgent'), $_SERVER['HTTP_USER_AGENT'])
) {
return false;
} else {
return true;
}
}
}
?>
PHP:
<?php
namespace App\r7r1n17y\Framework;
class Mailer implements IMailerInterface {
private $_smtp_host;
private $_smtp_port;
private $_smtp_auth;
private $_smtp_user;
private $_smtp_pass;
private $_smtp_secure;
private $_mail_from;
private $_mail_from_name;
private $_mail_charset;
private $_smtp;
public function __construct(array $config = array()) {
$this->_smtp_host = isset($config['smtp_host']) ? $config['smtp_host'] : 'mail.localhost';
$this->_smtp_port = isset($config['smtp_port']) ? $config['smtp_port'] : 25;
$this->_smtp_auth = isset($config['smtp_auth']) ? $config['smtp_auth'] : true;
$this->_smtp_user = isset($config['smtp_user']) ? $config['smtp_user'] : 'root@localhost';
$this->_smtp_pass = isset($config['smtp_pass']) ? $config['smtp_pass'] : 'password';
$this->_smtp_secure = isset($config['smtp_secure']) ? $config['smtp_secure'] : 'tls';
$this->_mail_from = isset($config['mail_from']) ? $config['mail_from'] : 'root@localhost';
$this->_mail_from_name = isset($config['mail_from_name']) ? $config['mail_from_name'] : 'root';
$this->_mail_charset = isset($config['mail_charset']) ? $config['mail_charset'] : 'utf-8';
$this->_smtp = isset($config['smtp']) ? $config['smtp'] : false;
}
public function send($to, $subject, $bind = array(), $template = 'plain') {
$mail = $this->__start();
$templatesDir = base_dir() . '/templates/';
$mail->addAddress($to);
$body = file_get_contents($templatesDir . $template . '.html');
foreach ($bind as $key => $value) {
$body = str_replace('{{' . $key . '}}', $value, $body);
}
$mail->Subject = $subject;
$mail->Body = $body;
if (!$mail->send()) {
throw new r7r1n17yException('E-Mail could not be sent. Info: {err-msg}', array('err-msg' => $mail->ErrorInfo));
}
}
private function __start() {
$mail = new \PHPMailer;
if ($this->_smtp) {
$mail->isSMTP();
$mail->Host = $this->_smtp_host;
$mail->Port = $this->_smtp_port;
$mail->SMTPSecure = $this->_smtp_secure;
$mail->SMTPAuth = $this->_smtp_auth;
$mail->Username = $this->_smtp_user;
$mail->Password = $this->_smtp_pass;
}
$mail->setFrom($this->_mail_from, $this->_mail_from_name);
$mail->CharSet = $this->_mail_charset;
return $mail;
}
}
?>
PHP:
<?php
namespace App\r7r1n17y\Framework;
date_default_timezone_set(@date_default_timezone_get());
require_once __DIR__ . '/check/check.php';
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/config/config.php';
require_once __DIR__ . '/options/runtime.conf.php';
require_once __DIR__ . '/options/mb.conf.php';
require_once __DIR__ . '/options/pc.conf.php';
require_once __DIR__ . '/options/hybirdauth.conf.php';
require_once __DIR__ . '/options/func.conf.php';
require_once __DIR__ . '/options/ssl.conf.php';
require_once __DIR__ . '/exception.php';
require_once __DIR__ . '/extend.php';
require_once __DIR__ . '/ext/ppac.php';
require_once __DIR__ . '/interfaces/cookie.php';
require_once __DIR__ . '/cookie.php';
require_once __DIR__ . '/ext/container.php';
require_once __DIR__ . '/interfaces/session.php';
require_once __DIR__ . '/session.php';
require_once __DIR__ . '/interfaces/mailer.php';
require_once __DIR__ . '/mailer.php';
$container = new \Pimple\Container;
$container['browser'] = $container->factory(function () {
return new \Browser();
});
$container['cookie'] = $container->factory(function () {
return new Cookie(array(
'domain' => 'localhost',
'secure' => true
));
});
$container['session'] = $container->factory(function ($c) {
return new Session($c['cookie']);
});
$container['mailer'] = $container->factory(function () {
return new Mailer(array(
'mail_from' => '[email protected]';
'mail_from_name' =>'John Doe';
));
});
DI::setContainer($container);
function app($service = null) {
$c = DI::getInstance();
if (is_null($service)) {
return $c;
}
return $c[$service];
}
?>
PHP:
<?php
namespace App\r7r1n17y\Framework;
abstract class AutoLoadExtender {
private $_exts = array();
public $_this;
public function __construct() {
$_this = $this;
}
public function addExt($object) {
$this->_exts[] = $object;
}
public function __get($varName) {
foreach($this->_exts as $ext) {
if (property_exists($ext, $varName)) {
return $ext->$varName;
}
}
}
public function __call($method, $args) {
foreach($this->_exts as $ext) {
if (method_exists($ext, $method)) {
return call_user_method_array($method, $ext, $args);
}
}
trigger_error(
e("This method {$method} doesn\'t exists."),
E_USER_ERROR
);
}
}
?>
Last edited: