Advertisement
fvelle

codex.php

Apr 25th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.63 KB | None | 0 0
  1. <?php
  2.  
  3. // load dependencies
  4. require_once dirname(__FILE__) . '/../../library/dropbox/autoload.php';
  5.  
  6. use \Dropbox as dbx;
  7.  
  8. /**
  9.  * SKGhendt - Codex Controller
  10.  *
  11.  * @package         SKGhendtIntranet
  12.  * @subpackage      modules
  13.  * @author          lotjer
  14.  *
  15.  */
  16. class CodexController extends SKGhendtController {
  17.     /**
  18.      * The views allowed for this module
  19.      *
  20.      * @var array
  21.      */
  22.     protected $views = array(
  23.         'index'
  24.     );
  25.    
  26.     /**
  27.      * Permissions views/module
  28.      *
  29.      * @return void
  30.      */
  31.     protected function checkPermissions() {
  32.         if ($this->loggedIn === false) {
  33.             SKGhendtWebsite::redirect('/');
  34.         }
  35.        
  36.         parent::checkPermissions();
  37.     }
  38.    
  39.     /**
  40.      * Our default view
  41.      *
  42.      * @return void
  43.      */
  44.     public function showIndex() {
  45.         // Main Layout
  46.             // asisgn vars in our main layout tpl
  47.             $this->setTitle('Codex');
  48.            
  49.         // Page Layout
  50.             // dropbox client
  51.             $dbxClient = new dbx\Client(DBX_ACCESS_TOKEN, 'Codex');
  52.            
  53.             // path to codex
  54.             $path = '/Codex';
  55.                  
  56.             // get cached dropbox data
  57.             // @bug ('Error executing HTTP request: Failed to connect to api.dropbox.com port 443: Connection timed)
  58.             // ADD 2 * 365 insted to fix it
  59.             $data = SKGhendtService::cachingGetCached('dropbox_codex', date(TIME_STAMP_DB, time() - (3600 * 12 * 2 * 365)));
  60.            
  61.             if ($data === null) {
  62.                 // get live data
  63.                 $data = $dbxClient->getMetadataWithChildren($path);
  64.                
  65.                 // update to cache
  66.                 SKGhendtService::cachingUpdate('dropbox_codex', $data);
  67.             }
  68.            
  69.             // data dropbox
  70.             $urls   = SKGhendtService::codexGetByPath($path . '/');
  71.             $files  = $data['contents'];
  72.            
  73.             // loop over each event
  74.             $this->pageIteration('iFiles', null, $files, function($file) use (&$urls, &$dbxClient) {
  75.                 if ($file['is_dir'] == '0') {
  76.                     // values we'll need
  77.                     $path       = utf8_decode($file['path']);
  78.                     $pathData   = $this->getDataFromPath($path);
  79.                     $url        = '';
  80.  
  81.                     if (isset($urls[$path]) === false) {
  82.                         try {
  83.                             $url = $dbxClient->createShareableLink($file['path']);
  84.                             $url = str_replace('?dl=0', '', $url);
  85.  
  86.                             SKGhendtService::codexAdd($path, $url);
  87.                         } catch (Exception $exc) {
  88.                             mailError($exc, $urls);
  89.                             throw $exc;
  90.                         }
  91.                     } else {
  92.                         $url = $urls[$path];
  93.                         unset($urls[$path]);
  94.                     }
  95.  
  96.                     // assign vars in iteration
  97.                     $this->pageTpl->multiAssignIteration(array(
  98.                         'dlUrl'     => $url . '?dl=1',
  99.                         'title'     => $pathData['title'],
  100.                         'page'      => $pathData['page'],
  101.                         'file'      => $pathData['file'],
  102.                         'size'      => $file['size']
  103.                     ));
  104.                 }
  105.             });
  106.            
  107.             // remove
  108.             foreach (array_keys($urls) as $path) {
  109.                 SKGhendtService::codexDelete($path);
  110.             }
  111.     }
  112.    
  113.     /**
  114.      * Get information from path like page, title, ...
  115.      *
  116.      * @param string $path
  117.      * @return array
  118.      */
  119.     private function getDataFromPath($path) {
  120.         // values we'll need
  121.         $path   = substr($path, strripos($path, '/') + 1);
  122.         $idx    = stripos($path, '_');
  123.         $title  = '';
  124.         $page   = '';
  125.        
  126.         if ($idx !== false) {
  127.             $pageHelper = substr($path, 0, $idx);
  128.            
  129.             if (SKGhendtFilter::isNumeric($pageHelper)) {
  130.                 $page = $pageHelper;
  131.                 $title = str_replace('_', ' ', substr($path, $idx + 1));
  132.             } else {
  133.                 $title = str_replace('_', ' ', $path);
  134.             }
  135.         } else {
  136.             $title = str_replace('_', ' ', $path);
  137.         }
  138.        
  139.         $file   = $title;
  140.         $title  = substr($title, 0, strripos($title, '.'));
  141.        
  142.         return array(
  143.             'page'      => $page,
  144.             'title'     => $title,
  145.             'file'      => $file
  146.         );
  147.     }
  148. }
  149.  
  150. // EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement