Example #1
0
 /**
  * Bootstrap method to be called during application bootstrap stage.
  *
  * @param Application $app the application currently running
  */
 public function bootstrap($app)
 {
     /** @var Module $module */
     if ($app->hasModule('user') && ($module = $app->getModule('user')) instanceof Module) {
         if (!isset($app->get('i18n')->translations['user*'])) {
             $app->get('i18n')->translations['user*'] = ['class' => PhpMessageSource::className(), 'basePath' => __DIR__ . '/messages'];
         }
         if ($app instanceof ConsoleApplication) {
             $module->controllerNamespace = 'johnitvn\\userplus\\commands';
         } else {
             $module->controllerNamespace = 'johnitvn\\userplus\\controllers';
         }
     }
 }
Example #2
0
 /**
  * Call a hook if it exists, fail silently if not
  *
  * @access public
  * @param string $name
  * @param array $arguments
  * @return bool
  */
 public static function call_if_exists($name, $arguments = [])
 {
     if (self::exists($name)) {
         $application = Application::get();
         $classname = 'Hook_' . ucfirst($application->name);
         call_user_func_array([$classname, $name], $arguments);
     }
 }
Example #3
0
 public function auth()
 {
     $client_id = $this->input->get('clent_id');
     $application = new Application();
     $application->client_id = $client_id;
     $application->get();
     if (!$application->exists()) {
         $data['title'] = 'Auth Page';
         $data['content'] = 'oauth/noapplication';
         $this->load->view('master', $data);
     } elseif (!$this->user_id) {
         $redirect = 'users/login?redirect_url=oauth/auth?client_id=' . $application->id;
         redirect($redirect);
     } elseif ($this->input->post()) {
         $allow = $this->input->post('allow');
         if ($allow) {
             $user = new User($this->user_id);
             $existing_token = new Token();
             $existing_token->where('user_id', $user->id);
             $existing_token->where('application_id', $application->id);
             $existing_token->get();
             if ($existing_token->exists()) {
                 $existing_token->delete();
             }
             $token = $this->generate_token();
             $token->save(array($application, $user));
             die;
             echo 'here';
         }
     } else {
         $this->load->helper('form');
         $data['application'] = array('id' => $application->id, 'name' => $application->name, 'client_id' => $application->client_id, 'client_secret' => $application->client_secret, 'redirect_url' => $application->redirect_url);
         $data['title'] = 'Auth Page';
         $data['content'] = 'oauth/authorize';
         $this->load->view('master', $data);
     }
 }
Example #4
0
 /**
  * @param string $collection
  * @return \MongoCollection
  */
 public static function coll($collection)
 {
     return Application::get('mongodb')->selectCollection($collection);
 }
 /**
  * Return all parameters, $_POST,$_GET and user parameters
  * @return array - all parameters to the application
  * @access public
  * @static
  */
 public static function parameters()
 {
     $ret = Application::post();
     $ret = ArrayUtility::merge($ret, Application::get());
     $ret = ArrayUtility::merge($ret, Session::getRegistered('userParams'));
     return $ret;
 }
Example #6
0
 /**
  * Detect
  *
  * @param string $hostname
  * @param string $request_uri
  * @access public
  * @return Application $application
  */
 public static function detect($hostname, $request_uri)
 {
     // If we already have a cached application, return that one
     if (self::$application !== null) {
         return Application::get();
     }
     // If multiple host headers have been set, use the last one
     if (strpos($hostname, ', ') !== false) {
         list($hostname, $discard) = array_reverse(explode(', ', $hostname));
     }
     // Find matching applications
     $applications = self::get_all();
     $matched_applications = [];
     // Regular matches
     foreach ($applications as $application) {
         if (in_array($hostname, $application->config->hostnames)) {
             $matched_applications[] = $application;
         }
     }
     // If we don't have any matched applications, try to match wildcards
     if (count($matched_applications) === 0) {
         foreach ($applications as $application) {
             $wildcard_hostnames = $application->config->hostnames;
             foreach ($wildcard_hostnames as $key => $wildcard_hostname) {
                 if (strpos($wildcard_hostname, '*') === false) {
                     unset($wildcard_hostnames[$key]);
                 }
             }
             if (count($wildcard_hostnames) == 0) {
                 continue;
             }
             foreach ($wildcard_hostnames as $wildcard_hostname) {
                 if (fnmatch($wildcard_hostname, $hostname)) {
                     $matched_applications[] = $application;
                 }
             }
         }
     }
     // Set required variables in the matched Application objects
     foreach ($matched_applications as $key => $application) {
         // Set the relative request URI according to the application
         if (isset($application->config->base_uri) and $application->config->base_uri !== '/') {
             $application->request_relative_uri = str_replace($application->config->base_uri, '', $request_uri);
         } else {
             $application->request_relative_uri = $request_uri;
         }
         $application->hostname = $hostname;
         $matched_applications[$key] = $application;
     }
     // Now that we have matching applications, see if one matches the
     // request specifically. Otherwise, simply return the first one.
     $matched_applications_sorted = [];
     foreach ($matched_applications as $application) {
         if (isset($application->config->base_uri)) {
             // base_uri should not be empty, default to '/'
             if ($application->config->base_uri == '') {
                 $application->config->base_uri = '/';
             }
             if (strpos($request_uri, $application->config->base_uri) === 0) {
                 $matched_applications_sorted[strlen($application->config->base_uri)] = $application;
             }
         } else {
             $matched_applications_sorted[0] = $application;
         }
     }
     // Sort the matched array by key, so the most specific one is at the end
     ksort($matched_applications_sorted);
     if (count($matched_applications_sorted) > 0) {
         // Get the most specific one
         $application = array_pop($matched_applications_sorted);
         Application::set($application);
         return Application::get();
     }
     throw new \Exception('No application found for ' . $hostname);
 }
Example #7
0
<?php

namespace App;

require_once 'Application.php';
$app = new Application();
$data = [['id' => 4, 'age' => 15], ['id' => 3, 'age' => 28], ['id' => 8, 'age' => 3], ['id' => 1, 'age' => 23]];
// BEGIN (write your solution here)
$app->get('/', function ($params) use($data) {
    if (array_key_exists('sort', $params)) {
        list($key, $order) = split(' ', $params['sort']);
        usort($data, function ($prev, $next) use($key, $order) {
            $prevVal = $prev[$key];
            $nextVal = $next[$key];
            if ($prevVal == $nextVal) {
                return 0;
            }
            if ($order == 'desc') {
                return $prevVal < $nextVal ? 1 : -1;
            } else {
                if ($order == 'asc') {
                    return $prevVal > $nextVal ? 1 : -1;
                }
            }
        });
    }
    return json_encode($data);
});
// END
$app->run();
Example #8
0
    public function any_handler()
    {
        $this->template('any');
    }
    // Add a custom header
    public function add_header()
    {
        echo '<h1>My custom header</h1>';
    }
    // Output a template
    protected function template($template)
    {
        include dirname(__FILE__) . "/templates/{$template}.html";
    }
}
// Create the instance
$app = new Application();
// Run a before filter
$app->before('get_info', 'add_header');
// Link up the urls
$app->get('', 'get_index');
$app->get('info', 'get_info');
$app->get('json', 'get_json');
$app->get('xml', 'get_xml');
$app->get('redirect', 'get_redirect');
$app->get('page/<page>', 'get_page');
$app->any('any', 'any_handler');
$app->get('page/<page:\\d{4}>/<id>', 'get_page');
$app->get('page/<page:.*>', 'get_page');
// Run the application
$app->run();
Example #9
0
 /**
  * Translate a string
  *
  * @access public
  * @return string $translated_string
  * @param string $string
  */
 public static function translate($string, Translation $translation = null)
 {
     if ($translation !== null) {
         $translation = self::get($translation->language, $translation->application_name);
     } else {
         $translation = self::get(\Application::get()->language, \Application::get()->name);
     }
     return $translation->translate_string($string);
 }
Example #10
0
 /**
  * Do a reverse rewrite of a link
  *
  * @access private
  * @param string $url_raw
  * @return string $reverse_rewrite
  */
 private static function rewrite_reverse_routes($url_raw)
 {
     $url = parse_url($url_raw);
     $params = [];
     $application = \Skeleton\Core\Application::Get();
     $routes = $application->config->routes;
     if (isset($url['query'])) {
         // Allow &amp; instead of &
         $url['query'] = str_replace('&amp;', '&', $url['query']);
         parse_str($url['query'], $params);
     }
     /**
      * Add language to the known parameters
      */
     if (isset($application->language) and !isset($params['language'])) {
         $params['language'] = $application->language->name_short;
     }
     /**
      * Search for the requested module
      */
     if (!isset($url['path'])) {
         return $url_raw;
     }
     if ($url['path'] != '' and $url['path'][0] == '/') {
         $url['path'] = substr($url['path'], 1);
     }
     $module_name = null;
     /**
      * Check skeleton packages
      */
     $packages = \Skeleton\Core\Package::get_all();
     foreach ($packages as $package) {
         $parts = explode('/', $url['path']);
         if (isset($parts[0]) and $parts[0] == $package->name) {
             unset($parts[0]);
             $package_parts = explode('-', str_replace('skeleton-package-', '', $package->name));
             foreach ($package_parts as $key => $package_part) {
                 $package_parts[$key] = ucfirst($package_part);
             }
             $module_name = '\\Skeleton\\Package\\' . implode('\\', $package_parts) . '\\Web\\Module\\' . str_replace('_', '\\', implode('/', $parts));
         }
     }
     if ($module_name === null) {
         $module_name = 'web_module_' . str_replace('/', '_', $url['path']);
     }
     $module_defined = false;
     $package_module = false;
     if (isset($routes[$module_name])) {
         $module_defined = true;
     } elseif (isset($routes[$module_name . '_index'])) {
         $module_name = $module_name . '_index';
         $module_defined = true;
     } else {
         foreach ($routes as $classname => $dummy) {
             $application = Application::get();
             $module_filename = str_replace('web_module_', '', $classname);
             $filename_parts = explode('_', $module_filename);
             $module_filename = '';
             foreach ($filename_parts as $filename_part) {
                 $module_filename .= '/' . strtolower($filename_part);
             }
             $module_filename .= '.php';
             $module_filename = $application->module_path . $module_filename;
             if (file_exists($module_filename) and !class_exists($classname)) {
                 require_once $module_filename;
             }
             if (class_exists($classname) and (strtolower(get_parent_class($classname)) == strtolower($module_name) or is_subclass_of($classname, $module_name))) {
                 $module_name = strtolower($classname);
                 $module_defined = true;
                 $package_module = true;
             }
         }
     }
     if (!$module_defined) {
         return $url_raw;
     }
     $routes = $routes[$module_name];
     $correct_route = null;
     foreach ($routes as $route) {
         $route_parts = explode('/', $route);
         $route_part_matches = 0;
         foreach ($route_parts as $key => $route_part) {
             if (trim($route_part) == '') {
                 unset($route_parts[$key]);
                 continue;
             }
             if ($route_part[0] != '$') {
                 $route_part_matches++;
                 continue;
             }
             /**
              * $language[en,nl] => language[en,nl]
              */
             $route_part = substr($route_part, 1);
             /**
              * Fetch required values
              */
             $required_values = [];
             preg_match_all('/(\\[(.*?)\\])/', $route_part, $matches);
             if (count($matches[2]) > 0) {
                 /**
                  * There are required values, parse them
                  */
                 $required_values = explode(',', $matches[2][0]);
                 $route_part = str_replace($matches[0][0], '', $route_part);
                 $route_parts[$key] = '$' . $route_part;
             }
             if (isset($params[$route_part])) {
                 /**
                  * if there are no required values => Proceed
                  */
                 if (count($required_values) == 0) {
                     $route_part_matches++;
                     continue;
                 }
                 /**
                  * Check the required values
                  */
                 $values_ok = false;
                 foreach ($required_values as $required_value) {
                     if ($required_value == $params[$route_part]) {
                         $values_ok = true;
                     }
                 }
                 if ($values_ok) {
                     $route_part_matches++;
                     continue;
                 }
             }
         }
         if ($route_part_matches == count($route_parts)) {
             $correct_route = $route_parts;
         }
     }
     if ($correct_route === null and !$package_module) {
         return $url_raw;
     } elseif ($correct_route === null and $package_module) {
         $module_name = str_replace('web_module_', '', $module_name);
         $new_url = '/' . str_replace('_', '/', $module_name);
     } else {
         $new_url = '';
         foreach ($correct_route as $url_part) {
             if ($url_part[0] !== '$') {
                 $new_url .= '/' . $url_part;
                 continue;
             }
             $url_part = substr($url_part, 1);
             $new_url .= '/' . $params[$url_part];
             unset($params[$url_part]);
         }
     }
     /**
      * If the first character is a /, remove it
      */
     if ($new_url[0] == '/') {
         $new_url = substr($new_url, 1);
     }
     if (count($params) > 0) {
         $new_url .= '?' . urldecode(http_build_query($params));
     }
     /**
      * Is there a fragment ('#') available?
      */
     if (isset($url['fragment'])) {
         $new_url .= '#' . $url['fragment'];
     }
     return $new_url;
 }
Example #11
0
<?php

include '../lib/soprano.php';
class Application extends Soprano
{
    public function getCreate()
    {
        $hangman = Factory::build('Hangman', 'new');
        $this->template->hangman = $hangman;
        $this->template->render('create');
    }
    public function getGuess($letter)
    {
        $hangman = Factory::build('Hangman');
        $this->template->hangman = $hangman;
        if ($hangman->guess(strtoupper($letter))) {
            $this->template->render('winner');
        }
        $this->template->render('guess');
    }
}
$app = new Application();
$app->get('/', 'getCreate');
$app->get('/create', 'getCreate');
$app->get('/guess/:letter', 'getGuess');
$app->run();
Example #12
0
 public function testAfterApplicationFiltersShouldBeCalledAfterRouteExecution()
 {
     $app = new Application();
     $app->get('/test', function (Response $response) {
         $response->appendContents('route');
     });
     $app->after(function (Response $response) {
         $response->appendContents('after1');
     });
     $app->after(function (Response $response) {
         $response->appendContents('after2');
     });
     $response = new ResponseMock();
     $app->run(Request::create(false, 'localhost', '/test'), $response);
     $this->assertEquals('routeafter1after2', $response->contents());
 }
Example #13
0
 /**
  * @param string $id
  * @param array $parameters
  * @return string
  */
 public static function trans($id, $parameters = [])
 {
     return Application::get('i18n')->trans($id, $parameters);
 }
Example #14
0
 /**
  * @return array
  */
 public static function getClientInfo()
 {
     return ['ip' => Application::get('request')->getClientIp(), 'userAgent' => Application::get('request')->getUserAgent()];
 }
Example #15
0
<?php

/**
 * Pico Web Application Framework Sample
 * @package		Picowa
 * @since		2010-04-09
 */
class Application extends Picowa
{
    function hello()
    {
        $name = $this->args->name;
        return "Hello {$name}!\n";
    }
}
$app = new Application();
$app->get('/:name', $app->_hello(), array('name' => '.*'));
$app->init()->run();