/** * Main plugin method * * @param array $users a list of user/pass * @param string $realm the title of the auth popup * * @return function */ public function import(array $users = array(), $realm = 'Restricted area') { //realm must be a string Argument::i()->test(2, 'string'); $this->realm = $realm; $self = $this; return function (Registry $request, Registry $response) use($users, $self) { //get digest $digest = $request->get('server', 'PHP_AUTH_DIGEST'); //if no digest if (empty($digest)) { //this throws anyways return $self->dialog(); } // analyze the PHP_AUTH_DIGEST variable $data = $self->digest($digest); //if no username if (!isset($users[$data['username']])) { //this throws anyways return $self->dialog(); } // generate the valid response $signature = $self->getSignature($users, $data); //if it doesnt match if ($data['response'] !== $signature) { //this throws anyways return $self->dialog(); } }; }
/** * This explains the importance of parent/child * Based on the given path we need to return the * correct results * * @param *string $path Dot notated path * @param int $i Current context * * @return mixed */ public function find($path, $i = 0) { Argument::i()->test(1, 'string')->test(2, 'int'); if ($i >= count($this->tree)) { return null; } $current = $this->tree[$i]; //if they are asking for the parent if (strpos($path, '../') === 0) { return $this->find(substr($path, 3), $i + 1); } if (strpos($path, './') === 0) { return $this->find(substr($path, 2), $i); } //separate by . $path = explode('.', $path); $last = count($path) - 1; foreach ($path as $i => $node) { //is it the last ? if ($i === $last) { //does it exist? if (isset($current[$node])) { return $current[$node]; } //is it length ? if ($node === 'length') { //is it a string? if (is_string($current)) { return strlen($current); } //is it an array? if (is_array($current) || $current instanceof \Countable) { return count($current); } //we cant count it, so it's 0 return 0; } } //we are not at the last node... //does the node exist and is it an array ? if (isset($current[$node]) && is_array($current[$node])) { //great we can continue $current = $current[$node]; continue; } //if it exists and we are just getting the length if (isset($current[$node]) && $path[$i + 1] === 'length' && $i + 1 === $last) { //let it continue continue; } //if we are here, then there maybe a node in current, //but there's still more nodes to process //either way it cannot be what we are searching for break; } return null; }
/** * Main route method * * @return function */ public function import($token, $secret, $escape = '1234567890') { Argument::i()->test(1, 'string')->test(2, 'string')->test(3, 'string'); //remember this scope $self = $this; eve()->addMethod('addCaptcha', function (Registry $request, Registry $response, array $meta) use($token, $self) { //we already checked the captcha it's good //we just need to check if it's set if (isset($meta['check_captcha']) && $meta['check_captcha'] && !$request->isKey('get', 'g-recaptcha-response') && !$request->isKey('post', 'g-recaptcha-response')) { //let the action handle the rest $request->set('valid_captcha', false); } //set captcha if (isset($route['make_captcha']) && $meta['make_captcha']) { $request->set('captcha', $token); } }); //You can add validators here return function (Registry $request, Registry $response) use($secret, $escape, $self) { $request->set('valid_captcha', true); //CAPTCHA - whether or not we are expecting it lets do a check $captcha = false; if ($request->isKey('get', 'g-recaptcha-response')) { $captcha = $request->get('get', 'g-recaptcha-response'); } else { if ($request->isKey('post', 'g-recaptcha-response')) { $captcha = $request->get('post', 'g-recaptcha-response'); } } if ($captcha !== false && $captcha !== $escape) { $result = eden('curl')->setUrl('https://www.google.com/recaptcha/api/siteverify')->verifyHost(false)->verifyPeer(false)->setPostFields(http_build_query(array('secret' => $secret, 'response' => $captcha)))->getJsonResponse(); //let the action handle the rest $request->set('valid_captcha', $result['success']); } }; }
/** * Translate string * * @param *string $string The phrase to translate * @param array|string[, string..] $args The sprintf arguments * * @return string */ public function translate($string, $args = array()) { Argument::i()->test(1, 'string'); if (!is_array($args)) { $args = func_get_args(); $string = array_shift($args); } if (count($args)) { foreach ($args as $i => $arg) { $args[$i] = $this->language()->get($arg); } return vsprintf($this->language()->get($string), $args); } return $this->language()->get($string); }
/** * Sends off this request to cURL * * @param string * @param string * @param array * @return mixed */ public function send($method, $path, array $meta = array()) { Argument::i()->test(1, 'string')->test(2, 'string'); //get the meta data for this url call $meta = $this->getMetaData($method, $path, $meta); //if in meta mode if ($this->metaOnly) { //return the meta return $meta; } //extract the meta data $url = $meta['url']; $data = $meta['post']; $agent = $meta['agent']; $encode = $meta['encode']; $headers = $meta['headers']; // send it into curl $request = $this('curl')->setUrl($url)->setConnectTimeout(10)->setFollowLocation(true)->setTimeout(60)->verifyPeer(false)->when($agent, function () use($agent) { $this->setUserAgent($agent); // set USER_AGENT })->when(!empty($headers), function () use($headers) { $this->setHeaders($headers); // set headers })->when($method == 'PUT' || $method == 'DELETE', function () use($method) { $this->setCustomRequest($method); })->when($method == 'POST' || $method == 'PUT', function () use($data) { if (empty($data)) { return; } //set the post data $this->setPostFields($data); }); //how should we return the data ? switch ($encode) { case self::ENCODE_QUERY: $response = $request->getQueryResponse(); // get the query response break; case self::ENCODE_JSON: $response = $request->getJsonResponse(); // get the json response break; case self::ENCODE_XML: $response = $request->getSimpleXmlResponse(); // get the xml response break; case self::ENCODE_RAW: default: $response = $request->getResponse(); // get the raw response break; } return $response; }
/** * Replaces the last path with this one * * @param *string * * @return Eden\System\Path */ public function replace($path) { //argument 1 must be a string Argument::i()->test(1, 'string'); //get the path array $pathArray = $this->getArray(); //pop out the last array_pop($pathArray); //push in the new $pathArray[] = $path; //assign back to path $this->data = implode('/', $pathArray); return $this; }
/** * Sets the file name prefix * * @param *string $prefix * * @return Eden\Handlebars\Index */ public function setPrefix($prefix) { //Argument 1 must be a string Argument::i()->test(1, 'string'); $this->prefix = $prefix; return $this; }
/** * Hijacks the class and reports the results of the next * method call * * @param *object $scope the class instance * @param string|null $name the name of the property to inspect * * @return Eden\Core\Inspect */ public function next($scope, $name = null) { Argument::i()->test(1, 'object')->test(2, 'string', 'null'); //argument 2 must be a string or null $this->scope = $scope; $this->name = $name; return $this; }
/** * Notify all observers of that a specific * event has happened * * @param string|null $event the event to trigger * @param mixed[, mixed..] $arg the arguments to pass to the handler * * @return Eden\Core\Event */ public function trigger($event = null) { //argument 1 must be string Argument::i()->test(1, 'string', 'null'); if (is_null($event)) { $trace = debug_backtrace(); $event = $trace[1]['function']; if (isset($trace[1]['class']) && trim($trace[1]['class'])) { $event = str_replace('\\', '-', $trace[1]['class']) . '-' . $event; } } //get the arguments $args = func_get_args(); //shift out the event array_shift($args); //for each observer foreach ($this->observers as $observer) { //if this is the same event, call the method, if the method returns false if ($event == $observer[0] && call_user_func_array($observer[2], $args) === false) { //break out of the loop break; } } return $this; }
/** * Returns the path class * * @param string * * @return Eden\System\Path */ public function path($path) { //argument 1 must be a string Argument::i()->test(1, 'string'); return Path::i($path); }
/** * Sets the session ID * * @param *int $id The prescribed session ID to use * * @return int */ public function setId($sid) { //argument 1 must be an integer Argument::i()->test(1, 'int'); if (!self::$session) { Exception::i()->setMessage(self::ERROR_NOT_STARTED)->trigger(); } return session_id((int) $sid); }
/** * Returns the argument validation class * * @return Eden\Core\Argument */ public function argument() { return Argument::i(); }
/** * Loads a validate class * * @param *string $key The validate factory key name * * @return Eve\Framework\Validate\Base */ public function validate($key) { Argument::i()->test(1, 'string'); $key = str_replace(array('-', '_', '/'), ' ', $key); $key = ucwords($key); $key = str_replace(' ', '\\', $key); $class = $this->rootNameSpace . '\\Validate\\' . $key . '\\Index'; if (!class_exists($class)) { throw new Exception(sprintf(self::NO_VALIDATE, $key)); } //remove starting \\ $class = substr($class, 1); return $this->{$class}(); }
/** * Sets a set of secure cookies. * * @param *array $data The list of cookie data * @param int $expires Expiration * @param string $path Path to make the cookie available * @param string|null $domain The domain * * @return Eden\Cookie\Index */ public function setSecureData(array $data, $expires = 0, $path = null, $domain = null) { //argment test Argument::i()->test(2, 'int')->test(3, 'string', 'null')->test(4, 'string', 'null'); $this->setData($data, $expires, $path, $domain, true, false); return $this; }
/** * Returns the string class * * @param string * * @return Eden\Type\Type\StringType */ public function getString($string) { //argument 1 must be a string Argument::i()->test(1, 'string'); return StringType::i($string); }
/** * sets data using the Serializable interface * * @param string * * @return Eden\Type\Type\ArrayType */ public function unserialize($data) { //argument 1 must be a string Argument::i()->test(1, 'string'); $this->data = json_decode($data, true); return $this; }
/** * Sets the PHP timezone * * @param *string * * @return Eden\Core\Controller */ public function setTimezone($zone = 'GMT') { Argument::i()->test(1, 'string'); date_default_timezone_set($zone); return $this; }
/** * Returns a list of files given the path and optionally the pattern * * @param string|null regular expression * @param bool * * @return array */ public function getFiles($regex = null, $recursive = false) { //argument test Argument::i()->test(1, 'string', 'null')->test(2, 'bool'); $this->absolute(); $files = array(); if ($handle = opendir($this->data)) { //for each file while (false !== ($file = readdir($handle))) { // If this is infact a file if (filetype($this->data . '/' . $file) == 'file' && (!$regex || preg_match($regex, $file))) { //add it $files[] = File::i($this->data . '/' . $file); // recursive and this is infact a directory } else { if ($recursive && $file != '.' && $file != '..' && filetype($this->data . '/' . $file) == 'dir') { $subfiles = self::i($this->data . '/' . $file); $files = array_merge($files, $subfiles->getFiles($regex, $recursive)); } } } closedir($handle); } return $files; }
/** * Remove an email from a mailbox * * @param *number $msgno The mail UID to remove * * @return Eden\Mail\Pop3 */ public function remove($msgno) { Argument::i()->test(1, 'int', 'string'); $this->call("DELE {$msgno}"); if (!$this->loggedin || !$this->socket) { return false; } if (!is_array($msgno)) { $msgno = array($msgno); } foreach ($msgno as $number) { $this->call('DELE ' . $number); } return $this; }
/** * The opposite of registerPartial * * @param *string $name the partial name */ public static function unregisterPartial($name) { //Argument 1 must be a string Argument::i()->test(1, 'string'); if (isset(self::$partials[$name])) { unset(self::$partials[$name]); } }
/** * Stops listening to an event * * @param string|null * @param callable|null * * @return Eden\Core\Event */ public function unlisten($event = null, $callable = null) { Argument::i()->test(1, 'string', 'null')->test(2, 'callable', 'null'); //if there is no event and no callable if (is_null($event) && is_null($callable)) { //it means that they want to remove everything $this->observers = array(); return $this; } $id = $this->getId($callable); //for each observer foreach ($this->observers as $i => $observer) { //if there is an event and is not being listened to if (!is_null($event) && $event != $observer[0]) { //skip it continue; } if (!is_null($callable) && $id != $observer[1]) { continue; } //unset it unset($this->observers[$i]); } return $this; }
/** * Adds routing middleware * * @param string $method The request method * @param string $path The route path * @param function $callback The middleware handler * * @return Eden\Server\Index */ public function route($method, $path, $callback) { Argument::i()->test(1, 'string')->test(2, 'string')->test(3, 'callable'); $method = strtoupper($method); if ($method === 'ALL') { return $this->route('get', $path, $callback)->route('post', $path, $callback)->route('put', $path, $callback)->route('delete', $path, $callback); } $this->routeMiddleware[$method][] = array($path, $callback); return $this; }
/** * IMAP requires setting an active mailbox * before getting a list of mails * * @param string $mailbox Name of mailbox * * @return false|Eden\Mail\Imap */ public function setActiveMailbox($mailbox) { Argument::i()->test(1, 'string'); if (!$this->socket) { $this->connect(); } $response = $this->call('SELECT', $this->escape($mailbox)); $result = array_pop($response); foreach ($response as $line) { if (strpos($line, 'EXISTS') !== false) { list($star, $this->total, $type) = explode(' ', $line, 3); } else { if (strpos($line, 'UIDNEXT') !== false) { list($star, $ok, $next, $this->next, $type) = explode(' ', $line, 5); $this->next = substr($this->next, 0, -1); } } if ($this->total && $this->next) { break; } } if (strpos($result, 'OK') !== false) { $this->mailbox = $mailbox; return $this; } return false; }
/** * Returns the value * * @param bool whether to get the modified or original version * @return string */ public function get($modified = true) { //argument 1 must be a bool Argument::i()->test(1, 'bool'); return $modified ? $this->data : $this->original; }
/** * Summarizes a text * * @param int number of words * * @return Eden\Type\Type\StringType */ public function summarize($words) { //argument 1 must be an integer Argument::i()->test(1, 'int'); $this->data = explode(' ', strip_tags($this->data), $words); array_pop($this->data); $this->data = implode(' ', $this->data); return $this; }
/** * sets task/message sender * the user used to create the channel * * @param *string $user The user ID * * @return Eve\Framework\Queue */ public function setUserId($user) { Argument::i()->test(1, 'string'); $this->user = $user; return $this; }
/** * Sets subject * * @param string $subject The title of this message * * @return Eden\Mail\Smtp */ public function setSubject($subject) { Argument::i()->test(1, 'string'); $this->subject = $subject; return $this; }
/** * Routes a class * * @param *string the class route name * @param *string the name of the class to route to * * @return Eden\Core\Route */ public function set($source, $destination) { //argument test Argument::i()->test(1, 'string', 'object')->test(2, 'string', 'object'); //if source is an object if (is_object($source)) { //transform it into string class $source = get_class($source); } //if it is a string if (is_string($destination)) { //we need to consider if this is a vitual class $destination = $this->get($destination); } //now let's route it $this->route[strtolower($source)] = $destination; return $this; }
/** * Creates a file and puts specified content into that file * * @param *string $content The raw content to save * * @return Eden\File\Index */ public function setContent($content) { //argument 1 must be string Argument::i()->test(1, 'string'); try { $this->absolute(); } catch (\Eden\Path\Exception $e) { $this->touch(); } file_put_contents($this->data, $content); return $this; }
/** * Returns Mail SMTP * * @param string * @param string * @param string * @param int|null * @param bool * @param bool * * @return Eden_Mail_Smtp */ public function smtp($host, $user, $pass, $port = null, $ssl = false, $tls = false) { Argument::i()->test(1, 'string')->test(2, 'string')->test(3, 'string')->test(4, 'int', 'null')->test(5, 'bool')->test(6, 'bool'); return Smtp::i($host, $user, $pass, $port, $ssl, $tls); }