]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Merge pull request #666 from ArthurHoaro/slim-api
authorArthur <arthur@hoa.ro>
Tue, 20 Dec 2016 10:30:05 +0000 (11:30 +0100)
committerGitHub <noreply@github.com>
Tue, 20 Dec 2016 10:30:05 +0000 (11:30 +0100)
REST API structure using Slim framework

1  2 
.travis.yml
index.php

diff --combined .travis.yml
index 6ff1b20f564e130e345821eaefe75a0fb829d55f,88cc827d5d4ed19cc3f3dbe381e022e4ab48796f..03071a4734535485b8f9cc0f0106a3f8668de34c
@@@ -4,12 -4,9 +4,10 @@@ cache
    directories:
      - $HOME/.composer/cache
  php:
 +  - 7.1
    - 7.0
    - 5.6
    - 5.5
-   - 5.4
-   - 5.3
  install:
    - composer self-update
    - composer install --prefer-dist
diff --combined index.php
index a0a3a8c70648f26098675a21d45387325edda3f3,835fd7d29b60370bbb7c5683a23022dbfb1f8074..eb73941deded389a6521264cf9112358e71543fb
+++ b/index.php
@@@ -175,7 -175,6 +175,6 @@@ define('STAY_SIGNED_IN_TOKEN', sha1($co
  if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
      autoLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']);
  }
- header('Content-Type: text/html; charset=utf-8'); // We use UTF-8 for proper international characters handling.
  
  /**
   * Checking session state (i.e. is the user still logged in)
@@@ -731,17 -730,10 +730,10 @@@ function showLinkList($PAGE, $LINKSDB, 
   *
   * @param ConfigManager $conf          Configuration Manager instance.
   * @param PluginManager $pluginManager Plugin Manager instance,
+  * @param LinkDB        $LINKSDB
   */
- function renderPage($conf, $pluginManager)
+ function renderPage($conf, $pluginManager, $LINKSDB)
  {
-     $LINKSDB = new LinkDB(
-         $conf->get('resource.datastore'),
-         isLoggedIn(),
-         $conf->get('privacy.hide_public_links'),
-         $conf->get('redirector.url'),
-         $conf->get('redirector.encode_url')
-     );
      $updater = new Updater(
          read_updates_file($conf->get('resource.updates')),
          $LINKSDB,
          exit;
      }
  
-     // Display openseach plugin (XML)
+     // Display opensearch plugin (XML)
      if ($targetPage == Router::$PAGE_OPENSEARCH) {
          header('Content-Type: application/xml; charset=utf-8');
          $PAGE->assign('serverurl', index_url($_SERVER));
              $conf->set('feed.rss_permalinks', !empty($_POST['enableRssPermalinks']));
              $conf->set('updates.check_updates', !empty($_POST['updateCheck']));
              $conf->set('privacy.hide_public_links', !empty($_POST['hidePublicLinks']));
+             $conf->set('api.enabled', !empty($_POST['apiEnabled']));
+             $conf->set('api.secret', escape($_POST['apiSecret']));
              try {
                  $conf->write(isLoggedIn());
              }
              $PAGE->assign('enable_rss_permalinks', $conf->get('feed.rss_permalinks', false));
              $PAGE->assign('enable_update_check', $conf->get('updates.check_updates', true));
              $PAGE->assign('hide_public_links', $conf->get('privacy.hide_public_links', false));
+             $PAGE->assign('api_enabled', $conf->get('api.enabled', true));
+             $PAGE->assign('api_secret', $conf->get('api.secret'));
              $PAGE->renderPage('configure');
              exit;
          }
              // Edit
              $created = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $linkdate);
              $updated = new DateTime();
 +            $shortUrl = $LINKSDB[$id]['shorturl'];
          } else {
              // New link
              $created = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $linkdate);
              $updated = null;
 +            $shortUrl = link_small_hash($created, $id);
          }
  
          // Remove multiple spaces.
              'created' => $created,
              'updated' => $updated,
              'tags' => str_replace(',', ' ', $tags),
 -            'shorturl' => link_small_hash($created, $id),
 +            'shorturl' => $shortUrl,
          );
  
          // If title is empty, use the URL as title.
@@@ -1954,6 -1948,14 +1950,14 @@@ function install($conf
              $conf->set('general.title', 'Shared links on '.escape(index_url($_SERVER)));
          }
          $conf->set('updates.check_updates', !empty($_POST['updateCheck']));
+         $conf->set('api.enabled', !empty($_POST['enableApi']));
+         $conf->set(
+             'api.secret',
+             generate_api_secret(
+                 $this->conf->get('credentials.login'),
+                 $this->conf->get('credentials.salt')
+             )
+         );
          try {
              // Everything is ok, let's create config file.
              $conf->write(isLoggedIn());
@@@ -2216,4 -2218,32 +2220,32 @@@ if (isset($_SERVER['QUERY_STRING']) && 
  if (!isset($_SESSION['LINKS_PER_PAGE'])) {
      $_SESSION['LINKS_PER_PAGE'] = $conf->get('general.links_per_page', 20);
  }
- renderPage($conf, $pluginManager);
+ $linkDb = new LinkDB(
+     $conf->get('resource.datastore'),
+     isLoggedIn(),
+     $conf->get('privacy.hide_public_links'),
+     $conf->get('redirector.url'),
+     $conf->get('redirector.encode_url')
+ );
+ $container = new \Slim\Container();
+ $container['conf'] = $conf;
+ $container['plugins'] = $pluginManager;
+ $app = new \Slim\App($container);
+ // REST API routes
+ $app->group('/api/v1', function() {
+     $this->get('/info', '\Api\Controllers\Info:getInfo');
+ })->add('\Api\ApiMiddleware');
+ $response = $app->run(true);
+ // Hack to make Slim and Shaarli router work together:
+ // If a Slim route isn't found, we call renderPage().
+ if ($response->getStatusCode() == 404) {
+     // We use UTF-8 for proper international characters handling.
+     header('Content-Type: text/html; charset=utf-8');
+     renderPage($conf, $pluginManager, $linkDb);
+ } else {
+     $app->respond($response);
+ }