~cyborg/michelangelo

b5c091433ff5e9b02d90536310c7d2d198c08a0b — cyborg 5 months ago 1ea6603
optimize PHP best practices
6 files changed, 62 insertions(+), 27 deletions(-)

A .idea/.gitignore
A .idea/michelangelo.iml
A .idea/modules.xml
A .idea/php.xml
A .idea/vcs.xml
M index.php
A .idea/.gitignore => .idea/.gitignore +8 -0
@@ 0,0 1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

A .idea/michelangelo.iml => .idea/michelangelo.iml +8 -0
@@ 0,0 1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
  <component name="NewModuleRootManager">
    <content url="file://$MODULE_DIR$" />
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
  </component>
</module>
\ No newline at end of file

A .idea/modules.xml => .idea/modules.xml +8 -0
@@ 0,0 1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectModuleManager">
    <modules>
      <module fileurl="file://$PROJECT_DIR$/.idea/michelangelo.iml" filepath="$PROJECT_DIR$/.idea/michelangelo.iml" />
    </modules>
  </component>
</project>
\ No newline at end of file

A .idea/php.xml => .idea/php.xml +6 -0
@@ 0,0 1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="PhpProjectSharedConfiguration" php_language_level="7.3">
    <option name="suggestChangeDefaultLanguageLevel" value="false" />
  </component>
</project>
\ No newline at end of file

A .idea/vcs.xml => .idea/vcs.xml +6 -0
@@ 0,0 1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="VcsDirectoryMappings">
    <mapping directory="$PROJECT_DIR$" vcs="Git" />
  </component>
</project>
\ No newline at end of file

M index.php => index.php +26 -27
@@ 7,8 7,7 @@ $elements = explode('/', $path);
// Show home if there are no parameters or paths
if (empty($elements[0])) {
    gallery();
}
// Else, grab the first item in the URL so we can identify which function to use
} // Else, grab the first item in the URL so we can identify which function to use
else {
    switch (array_shift($elements)) {
        case 'search':


@@ 25,11 24,13 @@ else {
    };
}

function not_blank($value) {
function not_blank($value): bool
{
    return isset($value) && !empty($value) && $value !== '';
}

function authentication() {
function authentication(): \Tumblr\API\Client
{
    // Grab the Tumblr API library
    require __DIR__ . '/vendor/autoload.php';
    // Start a secure session


@@ 41,16 42,14 @@ function authentication() {
    $requestHandler = $client->getRequestHandler();
    $requestHandler->setBaseUrl('https://www.tumblr.com/');
    // Check if the user has already authenticated
    if(not_blank($_SESSION['perm_token']) && not_blank($_SESSION['perm_secret'])) {
    if (not_blank($_SESSION['perm_token']) && not_blank($_SESSION['perm_secret'])) {
        $token = $_SESSION['perm_token'];
        $token_secret = $_SESSION['perm_secret'];
    }
    // Check if the user was here earlier by checking cookies
    } // Check if the user was here earlier by checking cookies
    else if (not_blank($_COOKIE['perm_token']) && not_blank($_COOKIE['perm_secret'])) {
        $token = $_COOKIE['perm_token'];
        $token_secret = $_COOKIE['perm_secret'];
    }
    // Check if this is the user's first visit
    } // Check if this is the user's first visit
    else if (!isset($_GET['oauth_verifier'])) {
        // Grab the oauth token
        $resp = $requestHandler->request('POST', 'oauth/request_token', array());


@@ 65,8 64,7 @@ function authentication() {
        $header_url = 'https://www.tumblr.com/oauth/authorize?oauth_token=' . $data['oauth_token'];
        header('Location: ' . $header_url);
        die();
    }
    // Check if the user was just sent back from the Tumblr authentication site
    } // Check if the user was just sent back from the Tumblr authentication site
    else {
        $verifier = $_GET['oauth_verifier'];
        // Use the stored temporary tokens


@@ 91,17 89,17 @@ function authentication() {
        die();
    }
    // Authenticate via OAuth
    $client = new Tumblr\API\Client(
    // Send the client back to the function
    return new Tumblr\API\Client(
        $consumer_key,
        $consumer_secret,
        $token,
        $token_secret
    );
    // Send the client back to the function
    return $client;
}

function get_dashboard_posts($client, $post_start, $limit, $post_type='photo') {
function get_dashboard_posts($client, $post_start, $limit, $post_type = 'photo'): string
{
    // Call the function for dashboard posts
    $dashboard_posts = $client->getDashboardPosts(array('limit' => $limit, 'offset' => $post_start, 'type' => $post_type));
    // Open the row that will hold all photos/videos


@@ 147,7 145,8 @@ function get_dashboard_posts($client, $post_start, $limit, $post_type='photo') {
    return $post_row;
}

function get_tagged_posts($client, $tag) {
function get_tagged_posts($client, $tag): string
{
    // Call the function for posts that match the search query
    $tagged_posts = $client->getTaggedPosts($tag);
    // Open the row that will hold all photos/videos


@@ 193,7 192,8 @@ function get_tagged_posts($client, $tag) {
    return $post_row;
}

function gallery($params=null) {
function gallery($params = null)
{
    // Authenticate the current user
    $client = authentication();



@@ 205,7 205,6 @@ function gallery($params=null) {
    $page_num = (int)substr($params[0], 0, 2);
    if (!is_null($params) && is_numeric($page_num)) {
        $page = $page_num;
        echo $dir;
    } else {
        $page = 1;
    }


@@ 213,9 212,9 @@ function gallery($params=null) {
    $limit = 20;
    // Check for specific post type in the URL
    if (not_blank($_GET['post_type'])) {
      $post_type = $_GET['post_type'];
        $post_type = $_GET['post_type'];
    } else {
      $post_type = 'photo';
        $post_type = 'photo';
    }
    // Call the get_dashboard_posts function and load some posts!
    $posts = get_dashboard_posts($client, $post_start, $limit, $post_type);


@@ 226,16 225,16 @@ function gallery($params=null) {
    $page_next = $page + 1;
    $page_prev = $page - 1;
    $disabled = '';
    if ($page <=1) {
    if ($page <= 1) {
        $disabled = 'disabled';
    } else {
        $page_item .= <<<EOD
        $page_item = <<<EOD
            <li class="page-item">
                <a class="page-link" href="/page/$page_prev?post_type=$post_type">$page_prev</a>
            </li>
        EOD;
    }
    $page_nav .= <<<EOD
    $page_nav = <<<EOD
        <nav aria-label="Page navigation">
            <ul class="pagination justify-content-center">
                <li class="page-item $disabled">


@@ 262,7 261,8 @@ function gallery($params=null) {
    require 'template.php';
}

function search($params) {
function search($params)
{
    // Authenticate the current user
    $client = authentication();
    // Get search query


@@ 276,12 276,11 @@ function search($params) {
    require 'template.php';
}

function robots() {
function robots()
{
    header('Content-type: text/plain');
    echo 'User-agent: *';
    echo 'Disallow: /search/';
    echo 'Disallow: /page/';
    die();
}

?>