File "api-landing.inc.php"

Full Path: /srv/www/www.cadoro.it/src/controllers/api-landing.inc.php
File size: 1.95 KB
MIME-type: text/x-php
Charset: utf-8

<?php
  header('Access-Control-Allow-Origin: *');
  header('Content-Type: text/json; charset="UTF-8"');

  # GET
  if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $landing = models\BlogArticle::get_by_slug($slug);
    $category = $landing->get_category();
    if (!$landing || $category['category_id'] != 4 || !_published($landing['published'], $landing['published_datetime'], $landing['published_datetime2'], true)) {
      not_found_json();
    }

    $result = array(
      "landing" => $landing->json_landing(),
    );

    # JSON output
    echo json_encode($result);
  }

  # POST
  else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = _json(file_get_contents('php://input'));
    $link = _a($data, 'link');

    if(strpos($link, BASE_URL . "/qrcode/") !== FALSE) {
      $qrcode_uniqid = str_replace(BASE_URL . "/qrcode/", "", $link);
      $qrcode = models\Qrcode::get_by_uniqid($qrcode_uniqid);
      if (!$qrcode || !_published($qrcode['published'], $qrcode['date_begin'], $qrcode['date_end'], true)) {
        $result = array(
          "landing" => "",
        );
      }
      if(strpos($qrcode['landing'], BASE_URL . "/landing/") !== FALSE) {
        $slug = str_replace(BASE_URL . "/landing/", "", $qrcode['landing']);

        $landing = models\BlogArticle::get_by_slug($slug);
        $category = $landing->get_category();
        if (!$landing || $category['category_id'] != 4 || !_published($landing['published'], $landing['published_datetime'], $landing['published_datetime2'], true)) {
          $result = array(
            "landing" => "",
          );
        }
        $result = array(
          "landing" => $slug,
        );
      }
    } else {
      $result = array(
        "landing" => "",
      );
    }

    # JSON output
    echo json_encode($result);
  }

  # DEFAULT
  else {
    header('HTTP/1.0 400 Bad Request', true, 400);
    header("Content-Type: application/json; charset=utf-8");
    echo json_encode(array("error" => "unsupported method"));
  }
?>