Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // load dependencies
- require_once dirname(__FILE__) . '/../../library/dropbox/autoload.php';
- use \Dropbox as dbx;
- /**
- * SKGhendt - Codex Controller
- *
- * @package SKGhendtIntranet
- * @subpackage modules
- * @author lotjer
- *
- */
- class CodexController extends SKGhendtController {
- /**
- * The views allowed for this module
- *
- * @var array
- */
- protected $views = array(
- 'index'
- );
- /**
- * Permissions views/module
- *
- * @return void
- */
- protected function checkPermissions() {
- if ($this->loggedIn === false) {
- SKGhendtWebsite::redirect('/');
- }
- parent::checkPermissions();
- }
- /**
- * Our default view
- *
- * @return void
- */
- public function showIndex() {
- // Main Layout
- // asisgn vars in our main layout tpl
- $this->setTitle('Codex');
- // Page Layout
- // dropbox client
- $dbxClient = new dbx\Client(DBX_ACCESS_TOKEN, 'Codex');
- // path to codex
- $path = '/Codex';
- // get cached dropbox data
- // @bug ('Error executing HTTP request: Failed to connect to api.dropbox.com port 443: Connection timed)
- // ADD 2 * 365 insted to fix it
- $data = SKGhendtService::cachingGetCached('dropbox_codex', date(TIME_STAMP_DB, time() - (3600 * 12 * 2 * 365)));
- if ($data === null) {
- // get live data
- $data = $dbxClient->getMetadataWithChildren($path);
- // update to cache
- SKGhendtService::cachingUpdate('dropbox_codex', $data);
- }
- // data dropbox
- $urls = SKGhendtService::codexGetByPath($path . '/');
- $files = $data['contents'];
- // loop over each event
- $this->pageIteration('iFiles', null, $files, function($file) use (&$urls, &$dbxClient) {
- if ($file['is_dir'] == '0') {
- // values we'll need
- $path = utf8_decode($file['path']);
- $pathData = $this->getDataFromPath($path);
- $url = '';
- if (isset($urls[$path]) === false) {
- try {
- $url = $dbxClient->createShareableLink($file['path']);
- $url = str_replace('?dl=0', '', $url);
- SKGhendtService::codexAdd($path, $url);
- } catch (Exception $exc) {
- mailError($exc, $urls);
- throw $exc;
- }
- } else {
- $url = $urls[$path];
- unset($urls[$path]);
- }
- // assign vars in iteration
- $this->pageTpl->multiAssignIteration(array(
- 'dlUrl' => $url . '?dl=1',
- 'title' => $pathData['title'],
- 'page' => $pathData['page'],
- 'file' => $pathData['file'],
- 'size' => $file['size']
- ));
- }
- });
- // remove
- foreach (array_keys($urls) as $path) {
- SKGhendtService::codexDelete($path);
- }
- }
- /**
- * Get information from path like page, title, ...
- *
- * @param string $path
- * @return array
- */
- private function getDataFromPath($path) {
- // values we'll need
- $path = substr($path, strripos($path, '/') + 1);
- $idx = stripos($path, '_');
- $title = '';
- $page = '';
- if ($idx !== false) {
- $pageHelper = substr($path, 0, $idx);
- if (SKGhendtFilter::isNumeric($pageHelper)) {
- $page = $pageHelper;
- $title = str_replace('_', ' ', substr($path, $idx + 1));
- } else {
- $title = str_replace('_', ' ', $path);
- }
- } else {
- $title = str_replace('_', ' ', $path);
- }
- $file = $title;
- $title = substr($title, 0, strripos($title, '.'));
- return array(
- 'page' => $page,
- 'title' => $title,
- 'file' => $file
- );
- }
- }
- // EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement