public static function run($task, $args) { $task = strtolower($task); // Make sure something is set if (empty($task) or $task === 'help') { static::help(); return; } $module = false; list($module, $task) = array_pad(explode('::', $task), 2, null); if ($task === null) { $task = $module; $module = false; } if ($module) { try { $path = \Fuel::add_module($module); \Finder::instance()->add_path($path); } catch (\FuelException $e) { throw new Exception(sprintf('Module "%s" does not exist.', $module)); } } // Just call and run() or did they have a specific method in mind? list($task, $method) = array_pad(explode(':', $task), 2, 'run'); // Find the task if (!($file = \Finder::search('tasks', $task))) { $files = \Finder::instance()->list_files('tasks'); $possibilities = array(); foreach ($files as $file) { $possible_task = pathinfo($file, \PATHINFO_FILENAME); $difference = levenshtein($possible_task, $task); $possibilities[$difference] = $possible_task; } ksort($possibilities); if ($possibilities and current($possibilities) <= 5) { throw new Exception(sprintf('Task "%s" does not exist. Did you mean "%s"?', $task, current($possibilities))); } else { throw new Exception(sprintf('Task "%s" does not exist.', $task)); } return; } require_once $file; $task = '\\Fuel\\Tasks\\' . ucfirst($task); $new_task = new $task(); // The help option hs been called, so call help instead if (\Cli::option('help') && is_callable(array($new_task, 'help'))) { $method = 'help'; } if ($return = call_user_func_array(array($new_task, $method), $args)) { \Cli::write($return); } }
/** * Find the controller that matches the route requested * * @param Route the given Route object * @return mixed the match array or false */ protected static function find_controller($match) { // First port of call: request for a module? if (\Fuel::module_exists($match->segments[0])) { // make the module known to the autoloader \Fuel::add_module($match->segments[0]); $segments = $match->segments; // first check if the controller is in a directory. $match->module = array_shift($segments); $match->directory = count($segments) ? array_shift($segments) : null; $match->controller = count($segments) ? array_shift($segments) : $match->module; // does the module controller exist? if (class_exists(ucfirst($match->module) . '\\Controller_' . ucfirst($match->directory) . '_' . ucfirst($match->controller))) { $match->action = count($segments) ? array_shift($segments) : null; $match->method_params = $segments; return $match; } $segments = $match->segments; // then check if it's a module controller $match->module = array_shift($segments); $match->directory = null; $match->controller = count($segments) ? array_shift($segments) : $match->module; // does the module controller exist? if (class_exists(ucfirst($match->module) . '\\Controller_' . ucfirst($match->controller))) { $match->action = count($segments) ? array_shift($segments) : null; $match->method_params = $segments; return $match; } $segments = $match->segments; // do we have a module controller with the same name as the module? if ($match->controller != $match->module) { array_shift($segments); $match->controller = $match->module; if (class_exists(ucfirst($match->module) . '\\Controller_' . ucfirst($match->controller))) { $match->action = count($segments) ? array_shift($segments) : null; $match->method_params = $segments; return $match; } } } $segments = $match->segments; // It's not a module, first check if the controller is in a directory. $match->directory = array_shift($segments); $match->controller = count($segments) ? array_shift($segments) : $match->directory; if (class_exists('Controller_' . ucfirst($match->directory) . '_' . ucfirst($match->controller))) { $match->action = count($segments) ? array_shift($segments) : null; $match->method_params = $segments; return $match; } $segments = $match->segments; // It's not in a directory, so check for app controllers $match->directory = null; $match->controller = count($segments) ? array_shift($segments) : $match->directory; // We first want to check if the controller is in a directory. if (class_exists('Controller_' . ucfirst($match->controller))) { $match->action = count($segments) ? array_shift($segments) : null; $match->method_params = $segments; return $match; } // none of the above. I give up. We've found ziltch... $match->action = null; $match->controller = null; return $match; }
/** * Creates the new Request object by getting a new URI object, then parsing * the uri with the Route class. * * @access public * @param string the uri string * @param bool whether or not to route the URI * @return void */ public function __construct($uri, $route) { $this->uri = new \URI($uri); $route = $route === true ? \Route::parse($this->uri) : \Route::parse_match($uri); // Attempts to register the first segment as a module $mod_path = \Fuel::add_module($route['segments'][0]); if ($mod_path !== false) { $this->module = array_shift($route['segments']); $this->paths = array($mod_path, $mod_path . 'classes' . DS); } // Check for directory $path = (!empty($this->module) ? $mod_path : APPPATH) . 'classes' . DS . 'controller' . DS; if (!empty($route['segments']) && is_dir($dirpath = $path . strtolower($route['segments'][0]))) { $this->directory = array_shift($route['segments']); } // When emptied the controller defaults to directory or module $controller = empty($this->directory) ? $this->module : $this->directory; if (count($route['segments']) == 0) { $route['segments'] = array($controller); } $this->controller = $route['segments'][0]; $this->action = isset($route['segments'][1]) ? $route['segments'][1] : ''; $this->method_params = array_slice($route['segments'], 2); $this->named_params = $route['named_params']; unset($route); }
/** * Find the controller that matches the route requested * * @param Route $match the given Route object * @return mixed the match array or false */ protected static function parse_match($match) { $namespace = ''; $segments = $match->segments; $module = false; // First port of call: request for a module? if (\Fuel::module_exists($segments[0])) { // make the module known to the autoloader \Fuel::add_module($segments[0]); $match->module = array_shift($segments); $namespace .= ucfirst($match->module) . '\\'; $module = $match->module; } if ($info = static::parse_segments($segments, $namespace, $module)) { $match->controller = $info['controller']; $match->action = $info['action']; $match->method_params = $info['method_params']; return $match; } else { return null; } }
/** * Creates the new Request object by getting a new URI object, then parsing * the uri with the Route class. * * Usage: * * $request = new Request('foo/bar'); * * @param string the uri string * @param bool whether or not to route the URI * @return void */ public function __construct($uri, $route = true) { $this->uri = new \Uri($uri); // check if a module was requested if (count($this->uri->segments) and $modpath = \Fuel::module_exists($this->uri->segments[0])) { // check if the module has routes if (file_exists($modpath .= 'config/routes.php')) { // load and add the module routes $modroutes = \Config::load(\Fuel::load($modpath), $this->uri->segments[0] . '_routes'); foreach ($modroutes as $name => $modroute) { switch ($name) { case '_root_': // map the root to the module default controller/method $name = $this->uri->segments[0]; break; case '_404_': // do not touch the 404 route break; default: // prefix the route with the module name if it isn't done yet if (strpos($name, $this->uri->segments[0] . '/') !== 0 and $name != $this->uri->segments[0]) { $name = $this->uri->segments[0] . '/' . $name; } break; } \Config::set('routes.' . $name, $modroute); } // update the loaded list of routes \Router::add(\Config::get('routes')); } } $this->route = \Router::process($this, $route); if (!$this->route) { return; } if ($this->route->module !== null) { $this->module = $this->route->module; \Fuel::add_module($this->module); $this->add_path(\Fuel::module_exists($this->module)); } $this->directory = $this->route->directory; $this->controller = $this->route->controller; $this->action = $this->route->action; $this->method_params = $this->route->method_params; $this->named_params = $this->route->named_params; }
public function callback($type, $finaliseUri, $bindto = null) { \Fuel::add_module('users'); switch (strtolower($type)) { case 'facebook': \Fuel::add_package('facebook'); if (\Facebook\Fb::getUser()) { $me = \Facebook\Fb::api('/me'); if (!$me) { throw new \Fuel_Exception('Could not retrieve facebook user info'); } if (!$bindto || $bindto instanceof \Users\Model_User) { $user = \Users\Model_User::find_by_facebook_id($me['id']); } else { $user = false; } if (\Auth::check()) { $uid = \Auth::instance()->get_user_id(); if (!$user) { //link user $this->_linkfacebook($bindto ? $bindto : \Users\Model_User::find($uid[1]), $me); } else { if ($uid[1] != $user->id) { \Session::set_flash('error', 'A user already exists with those facebook details. Please logout, login via facebook and unlink your facebook account and try again.'); \Response::redirect(\Input::get('redirect', '%2Flogin')); } } } else { if (!$user) { //create & finalise $user = \Auth::instance('UnifiedLogin')->create_user($me['email'], md5(rand()), $me['email'], 1, array('fname' => $me['first_name'], 'lname' => $me['last_name'])); $user = \Users\Model_User::find($user); $this->_linkfacebook($user, $me); \Auth::instance('UnifiedLogin')->forced_login($user); } else { //login found user \Auth::instance('UnifiedLogin')->forced_login($user); } } } else { throw new \Fuel_Exception('There was a problem getting the user in the callback.'); } break; case 'twitter': \Fuel::add_package('twitter'); $tokens = \Twitter\Tweet::instance()->get_tokens(); $twitter_user = \Twitter\Tweet::instance()->call('get', 'account/verify_credentials'); if (!$bindto || $bindto instanceof \Users\Model_User) { $user = \Users\Model_User::find_by_twitter_id($twitter_user->id); } else { $user = false; } if (\Auth::check()) { $uid = \Auth::instance()->get_user_id(); if (!$user) { //link user $this->_linktwitter($bindto ? $bindto : \Users\Model_User::find($uid[1]), array("name" => $twitter_user->name, "id" => $twitter_user->id, "screen_name" => $twitter_user->screen_name)); } else { if ($uid[1] != $user->id) { \Session::set_flash('error', 'A user already exists with those twitter details. Please logout, login via facebook and unlink your facebook account and try again.'); \Response::redirect(\Input::get('redirect', '%2Flogin')); } } } else { if (!$user) { \Session::set('twitter_user_finalise', array("name" => $twitter_user->name, "id" => $twitter_user->id, "screen_name" => $twitter_user->screen_name)); \Response::redirect($finaliseUri); } else { //login found user $this->forced_login($user); } } break; case 'google': break; case 'openid': break; case 'auth': return parent::login($username, $password); break; } }
/** * Creates the new Request object by getting a new URI object, then parsing * the uri with the Route class. * * Usage: * * $request = new Request('foo/bar'); * * @param string the uri string * @param bool whether or not to route the URI * @return void */ public function __construct($uri, $route = true) { $this->uri = new \Uri($uri); // check if a module was requested if (count($this->uri->segments) and $modpath = \Fuel::module_exists($this->uri->segments[0])) { // check if the module has custom routes if (file_exists($modpath .= 'config/routes.php')) { // load and add the routes \Config::load(\Fuel::load($modpath), 'routes'); \Router::add(\Config::get('routes')); } } $this->route = \Router::process($this, $route); if ( ! $this->route) { return false; } if ($this->route->module !== null) { $this->module = $this->route->module; \Fuel::add_module($this->module); $this->add_path(\Fuel::module_exists($this->module)); } $this->directory = $this->route->directory; $this->controller = $this->route->controller; $this->action = $this->route->action; $this->method_params = $this->route->method_params; $this->named_params = $this->route->named_params; }