/** * install an orbit module * * @param array $params */ public function action_permissions($params) { $folders = \CCEvent::fire('ccdoctor.permissions'); if (!is_array($folders)) { $folders = array(); } // add storage directories foreach (\ClanCats::$config->get('storage.paths') as $folder) { $folders[] = $folder; } foreach ($folders as $folder) { $display_folder = \CCStr::replace($folder, array(CCROOT => '')); // create directory if not existing if (!is_dir($folder)) { if (!mkdir($folder, 0755, true)) { $this->error("doctor could not create folder at: {$display_folder}"); } } // check permissions $perm = substr(decoct(fileperms($folder)), 2); if ($perm < 755) { CCCli::line(CCCli::color($perm, 'red') . ' - ' . $display_folder . ' fixing with ' . CCCli::color('755', 'green')); if (!chmod($folder, 0755)) { CCCli::line("doctor - is not able to change permissions for: {$display_folder}", 'red'); } } elseif ($perm == 777) { CCCli::line(CCCli::color($perm, 'yellow') . ' - ' . $display_folder . ' warning! this can be dangerous.'); } else { $this->success('- ' . $display_folder, $perm); } } }
/** * show php info */ public function action_phpinfo() { // set topic $this->theme->topic = "php info"; // get php info in a string ob_start(); phpinfo(); $pinfo = ob_get_contents(); ob_end_clean(); // get only the body $pinfo = preg_replace('%^.*<body>(.*)</body>.*$%ms', '$1', $pinfo); // add table class $pinfo = str_replace('<table', '<table class="table table-bordered"', $pinfo); // better headers $pinfo = \CCStr::replace($pinfo, array('</h2>' => '</h2><hr>')); echo $pinfo; }
/** * connect to database * * @param array $conf * @return bool */ public function connect($conf) { $connection_params = array(); foreach ($conf as $key => $value) { if (is_string($value)) { $connection_params['{' . $key . '}'] = $value; } } $connection_string = \CCStr::replace($this->connection_string, $connection_params); $this->connection = new \PDO($connection_string, $conf['user'], $conf['pass'], $this->connection_attributes($conf)); // At the moment this will never happen because pdo is going to // trhow an exception when the connection fails /*if ( !$this->connection ) { return false; }*/ // let pdo throw exceptions $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); return true; }
/** * test string replacements */ public function testStrReplace() { $this->assertEquals(CCStr::replace('Hello :name', array(':name' => 'World')), 'Hello World'); }
/** * Resolve an uri and get the route object * * @param string $uri * @return CCRoute */ public static function resolve($uri) { // cut unnecessary slashes and params if (substr($uri, -1) == '/') { $uri = substr($uri, 0, -1); } if (substr($uri, 0, 1) == '/') { $uri = substr($uri, 1); } $uri = CCStr::cut($uri, '?'); // create new route instance $route = new CCRoute($uri); // private route if (substr($uri, 0, 1) == '#') { if (!array_key_exists($uri, static::$privates)) { throw new CCException("CCRouter::resolve() - private route {$uri} could not be found."); } return static::configure($route, static::$privates[$uri]); } // pass the route trough the priority events if ($hook_route = CCEvent::pass('ccf.router.resolve.before', array($route, false))) { if ($hook_route[1] === true) { return static::configure($route, $hook_route[0]); } } // if the uri is empty root is requestet if (empty($uri)) { // try one slash also aka empty if (array_key_exists('', static::$routes)) { return static::configure($route, static::$routes['']); } return static::configure($route, static::$privates['#root']); } // simple static key route if (isset(static::$routes[$uri])) { return static::configure($route, static::$routes[$uri]); } // explode the uri $uri_parts = explode('/', $uri); // get the action $action = array_pop($uri_parts); // implode the uri without action $uri_without_action = implode('/', $uri_parts); // try the static key with an action if (isset($action)) { if (isset(static::$routes[$uri_without_action])) { if (CCController::has_action(static::$routes[$uri_without_action], $action)) { $route->action = $action; return static::configure($route, static::$routes[$uri_without_action]); } } } // dynamic keys foreach (static::$routes as $pattern => $dynamic_route) { // try the dynamic route only if it can be a pattern if (strpos($pattern, '[') !== false && strpos($pattern, ']') !== false) { // build an reqular expression $regx = '#^' . CCStr::replace($pattern, static::$filters) . '$#i'; // try to match the uri with regex if (preg_match($regx, $uri, $params)) { // remove the first param unset($params[0]); $route->action = null; $route->params = $params; return static::configure($route, $dynamic_route); } // try to match the uri without the action with the regex if (is_string($dynamic_route) && preg_match($regx, $uri_without_action, $params)) { if (CCController::has_action($dynamic_route, $action)) { // remove the first param unset($params[0]); $route->action = $action; $route->params = $params; return static::configure($route, $dynamic_route); } } } } /* * pass to events */ if ($hook_route = CCEvent::pass('ccf.router.resolve.after', array($route, false))) { if ($hook_route[1] === true) { return static::configure($route, $hook_route[0]); } } return false; }
/** * Search and replace vars with . array access * * @param string $view * @return void */ private function compile_arrays($view) { $tokens = token_get_all($view); $tags = array(0 => ''); $tag_index = 0; $in_tag = false; // parse all php tags out of the view foreach ($tokens as $token) { if (is_array($token)) { if ($token[0] === T_OPEN_TAG) { $in_tag = true; } if ($in_tag && !in_array($token[0], array(T_INLINE_HTML))) { $tags[$tag_index] .= $token[1]; } if ($token[0] === T_CLOSE_TAG) { $in_tag = false; $tag_index++; } } else { if ($in_tag) { $tags[$tag_index] .= $token; } } } // lets make the tags search keys $tags = array_flip($tags); // now search and replace var in the php sections foreach ($tags as $search => &$replace) { $replace = preg_replace_callback('/(\\$[a-zA-Z0-9\\_\\.\\-\\>\\;]+)/s', function ($match) { $var = $match[1]; if (strpos($var, '.') !== false) { $buffer = ''; $length = strlen($var); $inside_arr = false; for ($i = 0; $i < $length; $i++) { $char = $var[$i]; if ($char == '.' && !$inside_arr) { $buffer .= "['"; $inside_arr = true; } else { if ($inside_arr && in_array($char, array(';', '-', ','))) { $buffer .= "']"; $inside_arr = false; } if ($char == '.') { $buffer .= "']['"; $inside_arr = true; } else { $buffer .= $char; } } } if ($inside_arr) { $buffer .= "']"; } $var = $buffer; } return $var; }, $search); } return CCStr::replace($view, $tags); }