Archived
1
0
Fork 0

initial commit

This commit is contained in:
Henrik Hautakoski 2017-09-01 17:10:27 +02:00
commit e869a1cab4
107 changed files with 9029 additions and 0 deletions

53
app/library/Acl/Acl.php Normal file
View file

@ -0,0 +1,53 @@
<?php
namespace Acl;
use Phalcon\Acl\Role;
use Phalcon\Acl\Adapter\Memory as AclList;
class Acl extends AclList
{
const ROLE_USER = 'user';
const ROLE_GUEST = 'guest';
public function __construct()
{
$this->_build();
}
protected function _build()
{
// Deny access to everything by default.
$this->setDefaultAction(\Phalcon\Acl::DENY);
// Roles
$user = new Role('user');
$guest = new Role('guest');
$this->addRole($guest);
$this->addRole($user, $guest);
$public = array(
'index',
'error',
'auth',
'api',
);
// Public Resources
foreach($public as $resource) {
$this->addResource($resource, 'Read');
$this->allow($guest->getName(), $resource, 'Read');
}
$protected = array(
'callback',
'user',
);
foreach($protected as $resource) {
$this->addResource($resource, 'Read');
$this->allow($user->getName(), $resource, 'Read');
}
}
}

121
app/library/Auth/Auth.php Normal file
View file

@ -0,0 +1,121 @@
<?php
namespace Auth;
use Phalcon\Mvc\User\Component;
use Model\Data\User;
class Auth extends Component
{
/**
* @var string
*/
protected $_session_key = 'auth';
public function login($email, $password)
{
// Look for a user with this email.
$user = User::findFirstByEmail($email);
if ($user) {
// Verify password
$hash = $user->getPassword();
if (strlen($hash) > 1 && password_verify($password, $hash)) {
$this->setIdentity($user->getId());
return true;
}
}
return false;
}
/**
* Login using OAuth
*
* @param $auth
*/
public function loginOauth($auth)
{
$email = '';
if (isset($auth['info']['email'])) {
$email = $auth['info']['email'];
}
// Look for a user with this email.
$user = User::findFirstByEmail($email);
if (!$user) {
// Did not find any user. create him.
if (isset($auth['info']['nickname'])) {
$name = $auth['info']['nickname'];
} else if(isset($auth['info']['name'])) {
$name = $auth['info']['name'];
} else {
$name = '';
}
$user = new User();
$user->setEmail($email)
->setUsername($name);
$user->save();
}
$this->setIdentity($user->getId());
}
/**
* @param $identity
* @return Auth
*/
public function setIdentity($identity)
{
$this->session->set($this->_session_key, $identity);
return $this;
}
/**
* return \Model\Data\User
*/
public function getIdentity()
{
$id = $this->session->get($this->_session_key);
if ($id !== null) {
return \Model\Data\User::findFirst($id);
}
return null;
}
/**
* return \Model\Data\User
*/
public function getUser()
{
if ($this->hasIdentity()) {
$id = $this->session->get($this->_session_key);
return \Model\Data\User::findFirst($id);
}
return null;
}
public function hasIdentity()
{
return $this->getIdentity() !== NULL;
}
/**
* Clears the identity information.
*
* @return Auth
*/
public function clearIdentity()
{
$this->session->remove($this->_session_key);
return $this;
}
}

24
app/library/Debug.php Normal file
View file

@ -0,0 +1,24 @@
<?php
class Debug {
public static function dump($var, $label = null, $echo = true)
{
// format the label
$label = ($label===null) ? '' : rtrim($label) . ' ';
// var_dump the variable into a buffer and keep the output
ob_start();
var_dump($var);
$output = ob_get_clean();
// neaten the newlines and indents
$output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
$output = '<pre>'
. $label
. $output
. '</pre>';
if ($echo) {
echo $output;
}
return $output;
}
}

140
app/library/Menu.php Normal file
View file

@ -0,0 +1,140 @@
<?php
use Phalcon\Tag;
use Navigation\Node;
use Navigation\Navigation;
class Menu extends Tag
{
/**
* ACL Role
*
* @var string
*/
protected $_role = null;
/**
* @var Navigation
*/
protected $_navigation;
/**
* css class to use for the whole menu.
*
* @var string
*/
protected $_menuClass = 'menu';
/**
* Class to use for active nodes.
*
* @var string
*/
protected $_activeClass = 'active';
/**
* @param Navigation $navigation
*/
public function __construct(Navigation $navigation)
{
$this->_navigation = $navigation;
}
/**
* @return Navigation
*/
public function getNavigation()
{
return $this->_navigation;
}
/**
* @param Navigation $navigation
* @return Menu
*/
public function setNavigation(Navigation $navigation)
{
$this->_navigation = $navigation;
return $this;
}
public function setMenuClass($class)
{
$this->_menuClass = (string) $class;
return $this;
}
/**
* @param $role
*/
public function setAclRole($role)
{
$this->_role = $role;
}
/**
* Render the menu.
*
* @return string
*/
public function render($max_depth = null)
{
return $this->_renderMenu($this->_navigation->getChildren(), 0, $max_depth);
}
protected function _renderMenu($nodes, $depth, $max_depth = null)
{
$xhtml = '';
foreach($nodes as $node) {
$xhtml .= $this->_renderNode($node, $depth, $max_depth);
}
if (strlen($xhtml) > 0) {
$attribs = array();
if (strlen($this->_menuClass) > 0) {
$attribs['class'] = $this->_menuClass;
}
return self::tagHtml('ul', $attribs, false, false, true)
. $xhtml
. self::tagHtmlClose('ul', true);
}
return $xhtml;
}
protected function _renderNode(Node $node, $depth, $max_depth = null)
{
$xhtml = '';
// ACL.
$resource = $node->getResource();
if (strlen($this->_role) > 0 && strlen($resource) > 0 && $this->getDI()->has('acl')) {
$acl = $this->getDI()->get('acl');
if (!$acl->isAllowed($this->_role, $resource, 'Read')) {
return $xhtml;
}
}
// Only render this node if it is visible and has a caption.
if (!$node->isVisible() || strlen($node->getCaption()) < 1) {
return $xhtml;
}
$xhtml = self::tagHtml('li', $node->isActive()
? array('class' => $this->_activeClass) : null,
false, false, true);
// Generate the link.
$xhtml .= self::linkTo($node->getHref(), $node->getCaption());
if ($node->isActive() && $node->hasChildren()
&& ($max_depth === null || $depth < $max_depth)) {
$xhtml .= $this->_renderMenu($node->getChildren(), $depth + 1, $max_depth);
}
return $xhtml . self::tagHtmlClose('li', true);
}
}

View file

@ -0,0 +1,70 @@
<?php
namespace Mvc\Model\Behavior;
use \Phalcon\Mvc\Model\Behavior;
use \Phalcon\Mvc\Model\BehaviorInterface;
use \Phalcon\Exception;
/**
* Generates a unique base64 url-safe id for the field specified.
*
* Class RandomId
* @package Mvc\Model\Behavior
*/
class RandomId extends Behavior implements BehaviorInterface
{
public function __construct($options = null)
{
$field = null;
if (isset($options['field'])) {
$field = $options['field'];
}
if (isset($options['length'])) {
if (!is_numeric($options['length'])) {
throw new Exception("'length' must be a number.");
}
} else {
$options['length'] = 32;
}
if (strlen($field) < 1) {
throw new Exception("'field' must be set in the option array.");
}
parent::__construct($options);
}
public function notify($type, \Phalcon\Mvc\ModelInterface $model)
{
switch($type) {
case 'beforeValidationOnCreate' :
$this->generateId($model);
break;
}
}
public function generateId(\Phalcon\Mvc\ModelInterface $model)
{
$field = $this->_options['field'];
if ($model->$field === null) {
$random = new \Phalcon\Security\Random();
for($i = 0; $i < 3; $i++) {
$id = $random->base64Safe();
$id = substr($id, 0, $this->_options['length']);
$count = $model->count(array(
"$field = ?0",
'bind' => array($id)
));
if ($count < 1) {
$model->$field = $id;
break;
}
}
}
}
}

View file

@ -0,0 +1,82 @@
<?php
namespace Navigation;
class Container
{
/**
* Children
*
* @var array
*/
protected $_children = array();
/**
* @return array
*/
public function getChildren()
{
return $this->_children;
}
/**
* @return bool
*/
public function hasChildren()
{
return empty($this->getChildren()) === false;
}
/**
* @param $child
* @return Node
*/
public function addChild($child)
{
if (is_array($child)) {
$node = new Node();
foreach($child as $k => $v) {
if ($k == 'children') {
continue;
}
$node->{'set' . ucfirst($k)}($v);
}
if (isset($child['children'])) {
foreach($child['children'] as $c_data) {
$node->addChild($c_data);
}
}
$child = $node;
}
if (!($child instanceof Node)) {
throw new Exception('Must be of type node.');
}
$this->_children[] = $child;
$child->setParent($this);
return $this;
}
/**
* @param $children
* @return Node
*/
public function addChildren($children)
{
foreach($children as $child) {
$this->addChild($child);
}
return $this;
}
}

View file

@ -0,0 +1,7 @@
<?php
namespace Navigation;
class Exception extends \Exception
{
}

View file

@ -0,0 +1,13 @@
<?php
namespace Navigation;
class Navigation extends Container
{
public function __construct($config)
{
foreach($config as $node) {
$this->addChild($node);
}
}
}

View file

@ -0,0 +1,256 @@
<?php
namespace Navigation;
use \Phalcon\Di;
class Node extends Container
{
/**
* Caption
*
* @var string
*/
protected $_caption = null;
/**
* Controller name
*
* @var string
*/
protected $_controller = null;
/**
* Controller action.
*
* @var string
*/
protected $_action = null;
/**
* ACL Resource.
*
* @var string
*/
protected $_resource = null;
/**
* Route name.
*
* @var string
*/
protected $_route = null;
/**
* Active flag.
*
* @var bool
*/
protected $_active = null;
/**
* Visible flag
*
* @var bool
*/
protected $_visible = true;
/**
* @var Node
*/
protected $_parent = null;
/**
* @param $parent
* @return Node
*/
public function setParent($parent)
{
$this->_parent = $parent;
return $this;
}
/**
* @return Node
*/
public function getParent()
{
return $this->_parent;
}
/**
* @return string
*/
public function getCaption()
{
return $this->_caption;
}
/**
* @param string $caption
* @return Node
*/
public function setCaption($caption)
{
$this->_caption = (string) $caption;
return $this;
}
/**
* @return string
*/
public function getController()
{
return $this->_controller;
}
/**
* @param string $controller
* @return Node
*/
public function setController($controller)
{
$this->_controller = (string) $controller;
return $this;
}
/**
* @return string
*/
public function getAction()
{
return $this->_action;
}
/**
* @param string $action
* @return Node
*/
public function setAction($action)
{
$this->_action = (string) $action;
return $this;
}
/**
* @return string
*/
public function getResource()
{
return $this->_resource;
}
/**
* @param string $resource
* @return Node
*/
public function setResource($resource)
{
$this->_resource = (string) $resource;
return $this;
}
/**
* @return string
*/
public function getRoute()
{
return $this->_route;
}
/**
* @param string $route
* @return Node
*/
public function setRoute($route)
{
$this->_route = (string) $route;
return $this;
}
/**
* Get the href for this node.
*
* @return string
*/
public function getHref()
{
/** @var \Phalcon\Mvc\Url */
$url = Di::getDefault()->get('url');
// Assemble route if set.
if (strlen($this->getRoute()) > 0) {
$href = array(
'for' => $this->getRoute(),
'controller' => $this->getController(),
'action' => $this->getAction()
);
}
// Otherwise, use default route.
else {
$href = $this->getController();
if (is_string($this->getAction())) {
$href .= '/' . $this->getAction();
}
}
return $url->get($href);
}
/**
* @param $value
* @return Node
*/
public function setActive($value)
{
$this->_active = (bool) $value;
return $this;
}
/**
* @return bool
*/
public function isActive()
{
// If active flag is not set explicitly.
// Test this node against the current request.
if ($this->_active === null) {
// But only if the node is visible.
if ($this->isVisible() === false) {
return false;
}
// first. Check children.
foreach($this->getChildren() as $child) {
if ($child->isActive() == true) {
$this->setActive(true);
return $this->_active;
}
}
$dispatcher = Di::getDefault()->get('dispatcher');
$controller = strtolower($dispatcher->getControllerName());
$action = strtolower($dispatcher->getActionName());
$active = $controller == $this->_controller && $action == $this->_action;
$this->setActive($active);
}
return $this->_active;
}
public function setVisible($value)
{
$this->_visible = (bool) $value;
return $this;
}
public function isVisible()
{
return $this->_visible;
}
}

100
app/library/OAuth.php Normal file
View file

@ -0,0 +1,100 @@
<?php
//use Opauth;
use Phalcon\Mvc\User\Component;
class OAuth extends Component
{
/**
* Configuration
*
* @var array
*/
protected $_config = array(
'path' => '/oauth/',
'callback_url' => '/oauth/callback'
);
protected $_oauth;
protected $_callbackName = 'callback';
public function __construct($config)
{
$config = $this->objectToArray($config->oauth);
$this->_config = array_merge($this->_config, $config);
$this->_oauth = new Opauth($this->_config, false);
}
/**
* @return mixed|null|void
*/
public function perform()
{
$strategy = $this->dispatcher->getParam('strategy', null, false);
if ($strategy == $this->_callbackName) {
return $this->getResponse();
}
$this->_oauth->run();
exit;
}
/**
* @return array|string
*/
public function getResponse()
{
$response = null;
switch($this->_oauth->env['callback_transport']) {
case 'session':
$response = $this->session->get('opauth');
$this->session->remove('opauth');
break;
case 'post':
$response = unserialize(base64_decode( $_POST['opauth'] ));
break;
case 'get':
$response = unserialize(base64_decode( $_GET['opauth'] ));
break;
}
$ret = $this->_validate($response, $reason);
if ($ret === false) {
return $reason;
}
return $response;
}
public function objectToArray($object)
{
if(!is_object($object) && !is_array($object))
{
return $object;
}
if(is_object($object))
{
$object = get_object_vars( $object );
}
return array_map(array($this,"objectToArray"), $object );
}
protected function _validate($response, &$reason)
{
if (isset($response['auth']) &&
isset($response['timestamp']) &&
isset($response['signature'])) {
$hash = sha1(print_r($response['auth'], true));
return $this->_oauth->validate($hash, $response['timestamp'],
$response['signature'], $reason);
}
$reason = "Invalid auth response";
return false;
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace ViewHelper;
use Phalcon\DiInterface;
use Phalcon\Di\InjectionAwareInterface;
abstract class AbstractHelper implements InjectionAwareInterface
{
protected $_di;
/**
* Sets the dependency injector
*
* @param mixed $dependencyInjector
*/
public function setDI(DiInterface $dependencyInjector)
{
$this->_di = $dependencyInjector;
}
/**
* Returns the internal dependency injector
*
* @return \Phalcon\DiInterface
*/
public function getDI()
{
return $this->_di;
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace ViewHelper;
/**
* Class Icon
*
* @package ViewHelper
*/
class Icon extends AbstractHelper
{
public function icon($name, $args = array())
{
$classes = array(
'icon',
'ion-' . $name
);
if (is_array($args)) {
foreach($args as $arg) {
$classes[] .= 'ion-' . $arg;
}
}
$classes = implode(' ', $classes);
return '<i class="' . $classes . '"></i>';
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace ViewHelper;
/**
* Class ServerUrl
*
* @package ViewHelper
*/
class ServerUrl extends AbstractHelper
{
protected $_request;
public function getScheme()
{
return $this->_getRequest()->getScheme();
}
public function getHost()
{
return $this->_getRequest()->getHttpHost();
}
public function getPort()
{
return $this->_getRequest()->getPort();
}
public function serverUrl()
{
$port = $this->getPort();
$scheme = $this->getScheme();
// remove port if it's the default port.
if (($scheme == 'http' && $port == 80)
|| ($scheme == 'https' && $port == 443)) {
$port = null;
}
$url = $scheme . '://' . $this->getHost();
if ($port !== null) {
$url .= ':' . $port;
}
return $url;
}
/**
* @return \Phalcon\Http\RequestInterface
*/
protected function _getRequest()
{
if ($this->_request === null) {
$this->_request = $this->getDI()->getRequest();
}
return $this->_request;
}
}

View file

@ -0,0 +1,77 @@
<?php
namespace ViewHelper;
use Phalcon\DiInterface;
use Phalcon\Di\InjectionAwareInterface;
class Service implements InjectionAwareInterface
{
protected $_helpers = array();
protected $_di;
/**
* Sets the dependency injector
*
* @param mixed $dependencyInjector
*/
public function setDI(DiInterface $dependencyInjector)
{
$this->_di = $dependencyInjector;
}
/**
* Returns the internal dependency injector
*
* @return \Phalcon\DiInterface
*/
public function getDI()
{
return $this->_di;
}
public function set($name, AbstractHelper $helper)
{
$helper->setDI($this->getDI());
$this->_helpers[$name] = $helper;
}
public function has($name)
{
return $this->_locateHelper($name) !== false;
}
public function get($name)
{
if ($this->has($name)) {
return $this->_helpers[$name];
}
return false;
}
public function __call($name, $args)
{
$helper = $this->get($name);
if ($helper) {
return call_user_func_array(array($helper, $name), $args);
}
return false;
}
protected function _locateHelper($name)
{
if (array_key_exists($name, $this->_helpers)) {
return $this->_helpers[$name];
}
$class = '\ViewHelper\\' . ucfirst($name);
if (class_exists($class)) {
$helper = new $class();
$this->set($name, $helper);
return $helper;
}
return false;
}
}

View file

@ -0,0 +1,56 @@
<?php
namespace ViewHelper\Volt;
use \Phalcon\Di\InjectionAwareInterface;
class Extension implements InjectionAwareInterface
{
protected $_serviceKey = 'viewHelper';
/**
* @var \Phalcon\DiInterface
*/
protected $_di;
public function __construct(\Phalcon\DiInterface $dependencyInjector)
{
$this->_di = $dependencyInjector;
if (!$this->_di->has($this->_serviceKey)) {
$this->_di->set($this->_serviceKey, '\ViewHelper\Service', true);
}
}
public function compileFunction($name, $args)
{
// Get the view helper service.
$service = $this->_di->getShared($this->_serviceKey);
// Search for the helper in service.
if ($service->has($name)) {
return "\$this->{$this->_serviceKey}->{$name}({$args})";
}
return false;
}
/**
* Sets the dependency injector
*
* @param mixed $dependencyInjector
*/
public function setDI(\Phalcon\DiInterface $dependencyInjector)
{
$this->_di = $dependencyInjector;
}
/**
* Returns the internal dependency injector
*
* @return \Phalcon\DiInterface
*/
public function getDI()
{
return $this->_di;
}
}