/** * Returns the contents of a file given the path * * @return string */ public function getContent() { $this->absolute(); //if the pat is not a real file if (!is_file($this->data)) { //throw an exception Exception::i()->setMessage(self::ERROR_PATH_IS_NOT_FILE)->addVariable($this->data)->trigger(); } return file_get_contents($this->data); }
/** * Creates a folder given the path * * @param int chmod * * @return this */ public function create($chmod = 0755) { //argument 1 must be an integer Argument::i()->test(1, 'int'); //if chmod is not and integer or not between 0 and 777 if (!is_int($chmod) || $chmod < 0 || $chmod > 777) { //throw an error Exception::i(self::ERROR_CHMOD_IS_INVALID)->addVariable($this->data)->trigger(); } //if it's not a directory if (!is_dir($this->data)) { //then make it mkdir($this->data, $chmod, true); } return $this; }
/** * Main route method * * @return function */ public function import($escape = '1234567890') { //remember this scope $self = $this; $message = self::FAIL_400; eve()->addMethod('addCsrf', function (Registry $request, Registry $response, array $meta) use($self, $message) { //we already checked the csrf it's good //we just need to check if it's set //testing GET if (isset($meta['check_csrf']) && $meta['check_csrf'] && $meta['method'] === 'GET' && !$request->isKey('get', 'csrf')) { Exception::i($message)->trigger(); } //testing POST if (isset($meta['check_csrf']) && $meta['check_csrf'] && $meta['method'] === 'POST' && !$meta->isKey('post', 'csrf')) { Exception::i($message)->trigger(); } //this is for ALL if (isset($meta['check_csrf']) && $meta['check_csrf'] && $meta['method'] === 'ALL' && !empty($_POST) && !$request->isKey('post', 'csrf')) { Exception::i($message)->trigger(); } //set csrf if (isset($meta['make_csrf']) && $meta['make_csrf']) { $request->set('csrf', md5(uniqid())); } else { if (isset($meta['copy_csrf']) && $meta['copy_csrf']) { $request->set('csrf', $_SESSION['csrf']); } } }); //You can add validators here return function (Registry $request, Registry $response) use($self, $escape, $message) { //CSRF - whether or not we are expecting it lets do a check $csrf = false; if ($request->isKey('get', 'csrf')) { $csrf = $request->get('get', 'csrf'); } else { if ($request->isKey('post', 'csrf')) { $csrf = $request->get('post', 'csrf'); } } if ($csrf !== false && $csrf !== $_SESSION['csrf'] && $csrf !== $escape) { Exception::i($message)->trigger(); } }; }
/** * Attempts to get the full absolute path * as described on the server. The path * given must exist. * * @param string|null $root The root path * * @return Eden\Path\Index */ public function absolute($root = null) { //argument 1 must be a string or null Argument::i()->test(1, 'string', 'null'); //if path is a directory or file if (is_dir($this->data) || is_file($this->data)) { return $this; } //if root is null if (is_null($root)) { //assume the root is doc root $root = $_SERVER['DOCUMENT_ROOT']; } //get the absolute path $absolute = $this->format($this->format($root) . $this->data); //if absolute is a directory or file if (is_dir($absolute) || is_file($absolute)) { $this->data = $absolute; return $this; } //if we are here then it means that no path was found so we should throw an exception Exception::i()->setMessage(self::ERROR_FULL_PATH_NOT_FOUND)->addVariable($this->data)->addVariable($absolute)->trigger(); }
/** * Returns a very nice error message * * @param *string $code * * @return Eden\Handlebars\Index */ protected function checkEval($code) { $error = error_get_last(); if (isset($error['message']) && isset($error['line']) && $error['message'] === 'parse error') { $code = explode("\n", $code); $start = $error['line'] - 25; if ($start < 0) { $start = 0; } $code = array_splice($code, $start, 50); foreach ($code as $i => $line) { $code[$i] = ++$start . ': ' . $line; } Exception::i(self::COMPILE_ERROR)->setType('COMPILE')->addVariable($error['message'])->addVariable($error['line'])->addVariable(implode("\n", $code))->trigger(); } return $this; }
/** * Sends an email * * @param array $headers Custom headers * * @return array headers */ public function send(array $headers = array()) { //if no socket if (!$this->socket) { //then connect $this->connect(); } $headers = $this->getHeaders($headers); $body = $this->getBody(); //add from if (!$this->call('MAIL FROM:<' . $this->username . '>', 250, 251)) { $this->disconnect(); //throw exception Exception::i()->setMessage(Exception::SMTP_ADD_EMAIL)->addVariable($this->username)->trigger(); } //add to foreach ($this->to as $email => $name) { if (!$this->call('RCPT TO:<' . $email . '>', 250, 251)) { $this->disconnect(); //throw exception Exception::i()->setMessage(Exception::SMTP_ADD_EMAIL)->addVariable($email)->trigger(); } } //add cc foreach ($this->cc as $email => $name) { if (!$this->call('RCPT TO:<' . $email . '>', 250, 251)) { $this->disconnect(); //throw exception Exception::i()->setMessage(Exception::SMTP_ADD_EMAIL)->addVariable($email)->trigger(); } } //add bcc foreach ($this->bcc as $email => $name) { if (!$this->call('RCPT TO:<' . $email . '>', 250, 251)) { $this->disconnect(); //throw exception Exception::i()->setMessage(Exception::SMTP_ADD_EMAIL)->addVariable($email)->trigger(); } } //start compose if (!$this->call('DATA', 354)) { $this->disconnect(); //throw exception Exception::i(Exception::SMTP_DATA)->trigger(); } //send header data foreach ($headers as $name => $value) { $this->push($name . ': ' . $value); } //send body data foreach ($body as $line) { if (strpos($line, '.') === 0) { // Escape lines prefixed with a '.' $line = '.' . $line; } $this->push($line); } //tell server this is the end if (!$this->call(".", 250)) { $this->disconnect(); //throw exception Exception::i(Exception::SMTP_DATA)->trigger(); } //reset (some reason without this, this class spazzes out) $this->push('RSET'); return $headers; }
/** * Connects to the server * * @param int $timeout The connection timeout * @param bool $test Whether to output the logs * * @return Eden\Mail\Imap */ public function connect($timeout = self::TIMEOUT, $test = false) { Argument::i()->test(1, 'int')->test(2, 'bool'); if ($this->socket) { return $this; } $host = $this->host; if ($this->ssl) { $host = 'ssl://' . $host; } $errno = 0; $errstr = ''; $this->socket = @fsockopen($host, $this->port, $errno, $errstr, $timeout); if (!$this->socket) { //throw exception Exception::i()->setMessage(Exception::SERVER_ERROR)->addVariable($host . ':' . $this->port)->trigger(); } if (strpos($this->getLine(), '* OK') === false) { $this->disconnect(); //throw exception Exception::i()->setMessage(Exception::SERVER_ERROR)->addVariable($host . ':' . $this->port)->trigger(); } if ($this->tls) { $this->send('STARTTLS'); if (!stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { $this->disconnect(); //throw exception Exception::i()->setMessage(Exception::TLS_ERROR)->addVariable($host . ':' . $this->port)->trigger(); } } if ($test) { fclose($this->socket); $this->socket = null; return $this; } //login $result = $this->call('LOGIN', $this->escape($this->username, $this->password)); if (!is_array($result) || strpos(implode(' ', $result), 'OK') === false) { $this->disconnect(); //throw exception Exception::i(Exception::LOGIN_ERROR)->trigger(); } return $this; }
/** * Tests virtual arguments for valid data types * * @param *string $method method name * @param *array $args arguments * @param *int $index the argument index to test for * @param *mixed[,mixed..] $types the types to test for * * @return Eden\Core\Argument */ public function virtual($method, array $args, $index, $types) { //if no test if (static::$stop) { return $this; } $offset = 1; //if the trace came from Argument->test() if (isset($trace['class'], $trace['function']) && $trace['class'] == __CLASS__ && $trace['function'] == 'test') { //go back one more $offset = 2; } $trace = debug_backtrace(); $trace = $trace[$offset]; $types = func_get_args(); $method = array_shift($types); $args = array_shift($types); $index = array_shift($types) - 1; if ($index < 0) { $index = 0; } //if it's not set then it's good because the default value //set in the method will be it. if ($index >= count($args)) { return $this; } $argument = $args[$index]; foreach ($types as $i => $type) { if ($this->isValid($type, $argument)) { return $this; } } if (strpos($method, '->') === false && isset($trace['class'])) { $method = $trace['class'] . '->' . $method; } $type = $this->getDataType($argument); Exception::i()->setMessage(self::INVALID_ARGUMENT)->addVariable($index + 1)->addVariable($method)->addVariable(implode(' or ', $types))->addVariable($type)->setTypeLogic()->setTraceOffset($offset)->trigger(); }
/** * Connects to the server * * @param bool $test Whether to output the logs * * @return Eden\Mail\Pop3 */ public function connect($test = false) { Argument::i()->test(1, 'bool'); if ($this->loggedin) { return $this; } $host = $this->host; if ($this->ssl) { $host = 'ssl://' . $host; } $errno = 0; $errstr = ''; $this->socket = fsockopen($host, $this->port, $errno, $errstr, self::TIMEOUT); if (!$this->socket) { //throw exception Exception::i()->setMessage(Exception::SERVER_ERROR)->addVariable($host . ':' . $this->port)->trigger(); } $welcome = $this->receive(); strtok($welcome, '<'); $this->timestamp = strtok('>'); if (!strpos($this->timestamp, '@')) { $this->timestamp = null; } else { $this->timestamp = '<' . $this->timestamp . '>'; } if ($this->tls) { $this->call('STLS'); if (!stream_socket_enable_crypto($this->socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) { $this->disconnect(); //throw exception Exception::i()->setMessage(Exception::TLS_ERROR)->addVariable($host . ':' . $this->port)->trigger(); } } if ($test) { $this->disconnect(); return $this; } //login if ($this->timestamp) { try { $this->call('APOP ' . $this->username . ' ' . md5($this->timestamp . $this->password)); return; } catch (Argument $e) { // ignore } } $this->call('USER ' . $this->username); $this->call('PASS ' . $this->password); $this->loggedin = true; return $this; }
/** * Validates whether if the index is set * For Iterator interface * * @return bool */ public function valid() { if (!self::$session) { Exception::i()->setMessage(self::ERROR_NOT_STARTED)->trigger(); } return isset($_SESSION[$this->key()]); }
/** * Starts to process the request * * @return array with request and response inside */ public function process() { //formulate the request and response $request = $this->getRequest(); $response = $this->getResponse(); //register shutdown register_shutdown_function(function ($server) { $server->trigger('server-end'); }, $this); //if it's not a child if (!$this->parentServer instanceof Index) { //handle errors in case $this->handleErrors($request, $response); } //if we are Good, route if ($this->processGlobal($request, $response)) { //if no routing on this if (!$this->processRoutes($request, $response)) { $response->set('code', 404); //throw an exception Exception::i()->setMessage(self::NOT_FOUND)->setType(self::RESPONSE_ERROR_TYPE)->trigger(); } } //do we have a body ? $body = $response->get('body'); if ($body === null || !is_scalar($body) || !strlen((string) $body)) { $response->set('code', 404); //throw an exception Exception::i()->setMessage(self::NOT_FOUND)->setType(self::RESPONSE_ERROR_TYPE)->trigger(); } return array($request, $response); }
/** * Opens the browsers auth dialig * * @return void */ public function dialog() { //if session has not started if (!session_id()) { //start session session_start(); } //if they never tried if (!isset($_SESSION[self::UNAUTHORIZED])) { //first try $_SESSION[self::UNAUTHORIZED] = 1; } else { //more than one try $_SESSION[self::UNAUTHORIZED]++; } //if it's their first few tries if ($_SESSION[self::UNAUTHORIZED] < 3) { header(sprintf(self::HTTP_AUTH, $this->realm, uniqid(), md5($this->realm))); exit; } //let them try again unset($_SESSION[self::UNAUTHORIZED]); //you are unauthorized header(self::UNAUTHORIZED); //throw Exception::i()->setMessage(self::UNAUTHORIZED)->setType(self::ERROR_TYPE)->trigger(); }
/** * Returns the exception class * * @return Eden\Handler\Exception */ public function exception() { return Exception::i(); }
/** * Returns an instance considering routes. With * this method we can instantiate while passing * construct arguments as arrays * * @param *string $class name of the class * * @return object */ private static function getInstance($class) { //get the backtrace $trace = debug_backtrace(); $args = array(); //the 2nd line is the caller method if (isset($trace[1]['args']) && count($trace[1]['args']) > 1) { //get the args $args = $trace[1]['args']; //shift out the class name array_shift($args); //then maybe it's the 3rd line? } else { if (isset($trace[2]['args']) && count($trace[2]['args']) > 0) { //get the args $args = $trace[2]['args']; } } //if there's no args or there's no construct to accept the args if (count($args) === 0 || !method_exists($class, '__construct')) { //just return the instantiation return new $class(); } //at this point, we need to vitually call the class $reflect = new \ReflectionClass($class); try { //to return the instantiation return $reflect->newInstanceArgs($args); } catch (\ReflectionException $e) { //trigger error Exception::i()->setMessage(self::ERROR_REFLECTION)->addVariable($class)->addVariable('new')->trigger(); } }