コード例 #1
0
 public function execute(\Libs\View $view, \Libs\DOM\Element $element)
 {
     parent::execute($view, $element);
     $message = sprintf('Hello, %s!', $_SERVER['REMOTE_ADDR']);
     $document = $view->document();
     $element->appendChild($document->createElement('message', $message));
 }
コード例 #2
0
    public function execute(\Libs\DOM\Element $element)
    {
        parent::execute($element);
        $session = \Libs\Session::current();
        $db = $session->app()->database();
        $id = trim(isset($session->parameters()->{'data'}) ? $session->parameters()->{'data'} : '');
        // Ignore null values:
        if ($id == '') {
            exit;
        }
        // Lookup the URL of the id:
        $query = '
				SELECT url
				FROM urls
				WHERE id = "%s"
				LIMIT 1
			';
        $data = $db->query($query, $id);
        // Redirect home if the URL doesn't exist:
        if ($data === false) {
            header('Location: ' . SERVER_URL);
            exit;
        }
        // Increment the number of hits for this id:
        $query = '
				UPDATE urls
				SET hits = 1 + (
						SELECT hits
						FROM urls
						WHERE id = "%1$s"
					)
				WHERE id = "%1$s"
			';
        $db->exec($query, $id);
        // Record this hit:
        $address = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
        $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
        $agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
        $query = '
				INSERT INTO hits (
					id, address, timestamp, referrer, agent
				)
				VALUES (
					"%s", "%s", "%s", "%s", "%s"
				)
			';
        $db->exec($query, $id, $address, gmdate('U'), $referrer, $agent);
        // Make sure the URL begins with a protocol:
        if (!preg_match('%^(https?|ftp)://%', $data->url)) {
            $data->url = 'http://' . $data->url;
        }
        // Redirect to the URL:
        header('Location: ' . $data->url);
    }
コード例 #3
0
 public function execute(\Libs\DOM\Element $element)
 {
     parent::execute($element);
     $session = \Apps\Debug\App::$session;
     $document = \Libs\Session::view()->document();
     $input = $document->createElement('input', $session->view()->document()->saveXML());
     $output = $document->createElement('output', $session->view()->output());
     $element->appendChild($input);
     $element->appendChild($output);
     //echo '<pre>', htmlentities($element->saveXML()); exit;
 }
コード例 #4
0
    public function execute(\Libs\DOM\Element $element)
    {
        parent::execute($element);
        $session = \Libs\Session::current();
        $document = $session->view()->document();
        $db = $session->app()->database();
        $url = trim(isset($session->parameters()->{'data'}) ? $session->parameters()->{'data'} : '');
        // Ignore null values:
        if ($url == '') {
            exit;
        }
        // Make sure the URL begins with a protocol:
        if (!preg_match('%^(https?|ftp)://%', $url)) {
            $url = 'http://' . $url;
        }
        // Lookup the id of an existing URL:
        $query = '
				SELECT id
				FROM urls
				WHERE url = "%s"
				LIMIT 1
			';
        $data = $db->query($query, $url);
        // No id found, create a new one:
        if ($data === false) {
            $generate = function () {
                return base_convert(rand(46656, 1679615), 10, 36);
            };
            // Get current timestamp in GMT:
            $timestamp = gmdate('U');
            // Create a random 4 character string:
            $id = $generate();
            $query = '
					SELECT COUNT(*) == 0 as `count`
					FROM urls
					WHERE id = "%s"
					LIMIT 1
				';
            // Make sure the ID is not in use:
            while (true) {
                $taken = $db->query($query, $id);
                if ($taken->count) {
                    break;
                }
                $id = $generate();
            }
            // Write new URL to database:
            $query = '
					INSERT INTO urls (
						id, url, timestamp, hits
					)
					VALUES (
						"%s", "%s", "%s", 0
					)
				';
            $db->exec($query, $id, $url, $timestamp);
        } else {
            $id = $data->id;
        }
        $element->setAttribute('id', $id);
    }