PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/php/dropbox_external_repository_manager_connector.class.php

https://bitbucket.org/chamilo/chamilo-ext-repo-dropbox-dev/
PHP | 361 lines | 274 code | 42 blank | 45 comment | 23 complexity | 3a60c208faeb2b1361f820f65310dcf0 MD5 | raw file
  1. <?php
  2. namespace common\extensions\external_repository_manager\implementation\dropbox;
  3. use common\libraries\Redirect;
  4. use common\libraries\Request;
  5. use common\libraries\Path;
  6. use common\libraries\Session;
  7. use common\libraries\Utilities;
  8. use common\libraries\ArrayResultSet;
  9. use common\libraries\ActionBarSearchForm;
  10. use common\libraries\Filesystem;
  11. use common\extensions\external_repository_manager\ExternalRepositoryManagerConnector;
  12. use common\extensions\external_repository_manager\ExternalRepositoryObject;
  13. use repository\ExternalUserSetting;
  14. use repository\ExternalSetting;
  15. use repository\RepositoryDataManager;
  16. use HTTP_OAuth;
  17. require_once 'OAuth/Request.php';
  18. require_once Path :: get_plugin_path(__NAMESPACE__) . 'dropbox-api/API.php';
  19. require_once dirname(__FILE__) . '/dropbox_external_repository_object.class.php';
  20. class DropboxExternalRepositoryManagerConnector extends ExternalRepositoryManagerConnector
  21. {
  22. private $dropbox;
  23. private $consumer;
  24. private $key;
  25. private $secret;
  26. private $tokens;
  27. private $oauth;
  28. const SORT_DATE_CREATED = 'date-created';
  29. function __construct($external_repository_instance)
  30. {
  31. parent :: __construct($external_repository_instance);
  32. $this->key = ExternalSetting :: get('key', $this->get_external_repository_instance_id());
  33. $this->secret = ExternalSetting :: get('secret', $this->get_external_repository_instance_id());
  34. $session_token = ExternalUserSetting :: get('session_token', $this->get_external_repository_instance_id());
  35. $this->oauth = new Dropbox_OAuth_PEAR($this->key, $this->secret);
  36. if (isset($_SESSION['state']))
  37. {
  38. $state = $_SESSION['state'];
  39. }
  40. else
  41. {
  42. $state = 1;
  43. }
  44. if (is_null($session_token))
  45. {
  46. switch ($state)
  47. {
  48. case 1 :
  49. $this->tokens = $this->oauth->getRequestToken();
  50. $url = $this->oauth->getAuthorizeUrl(Redirect :: current_url());
  51. $_SESSION['state'] = 2;
  52. $_SESSION['oauth_tokens'] = $this->tokens;
  53. header('Location: ' . $url);
  54. die();
  55. case 2 :
  56. $this->oauth->setToken($_SESSION['oauth_tokens']);
  57. $this->tokens = $this->oauth->getAccessToken();
  58. $_SESSION['state'] = 3;
  59. $_SESSION['oauth_tokens'] = $this->tokens;
  60. case 3 :
  61. $this->oauth->setToken($_SESSION['oauth_tokens']);
  62. $setting = RepositoryDataManager :: get_instance()->retrieve_external_setting_from_variable_name('session_token', $this->get_external_repository_instance_id());
  63. $user_setting = new ExternalUserSetting();
  64. $user_setting->set_setting_id($setting->get_id());
  65. $user_setting->set_user_id(Session :: get_user_id());
  66. $token = $_SESSION['oauth_tokens']['token'] . '-' . $_SESSION['oauth_tokens']['token_secret'];
  67. $user_setting->set_value($token);
  68. $user_setting->create();
  69. break;
  70. }
  71. }
  72. $session_token = ExternalUserSetting :: get('session_token', $this->get_external_repository_instance_id());
  73. $tokens_db = explode('-', $session_token);
  74. $tokens = array();
  75. $tokens['token'] = $tokens_db[0];
  76. $tokens['token_secret'] = $tokens_db[1];
  77. $this->oauth->setToken($tokens);
  78. $this->dropbox = new Dropbox_API($this->oauth);
  79. }
  80. /**
  81. * @param mixed $condition
  82. * @param ObjectTableOrder $order_property
  83. * @param int $offset
  84. * @param int $count
  85. * @return array
  86. */
  87. function retrieve_files($condition = null, $order_property, $offset, $count)
  88. {
  89. $folder = Request :: get('folder');
  90. if (! is_null($folder))
  91. {
  92. $folder = urldecode($folder);
  93. }
  94. else
  95. $folder = '/';
  96. $files = $this->dropbox->getMetaData($this->encode($folder));
  97. return $files;
  98. }
  99. function retrieve_folder($path, $condition = null, $order_property, $offset, $count)
  100. {
  101. $folders = $this->dropbox->getMetaData($path);
  102. return $folders;
  103. }
  104. /**
  105. * @param mixed $condition
  106. * @param ObjectTableOrder $order_property
  107. * @param int $offset
  108. * @param int $count
  109. * @return ArrayResultSet
  110. */
  111. function retrieve_external_repository_objects($condition = null, $order_property, $offset, $count)
  112. {
  113. $files = $this->retrieve_files($condition, $order_property, $offset, $count);
  114. $file_count = 0;
  115. $objects = array();
  116. foreach ($files['contents'] as $file)
  117. {
  118. if ($file['is_dir'] != 1)
  119. {
  120. $object = new DropboxExternalRepositoryObject();
  121. $object->set_id((string) substr($file['path'], 1));
  122. $object->set_external_repository_id($this->get_external_repository_instance_id());
  123. $object->set_title((string) substr($file['path'], strripos($file['path'], '/') + 1));
  124. $object->set_created($file['modified']);
  125. $object->set_modified($file['modified']);
  126. $object->set_type($file['icon']);
  127. $object->set_description($file['size']);
  128. $object->set_rights($this->determine_rights());
  129. $file_count ++;
  130. if ($file_count > $offset && $file_count <= ($count + $offset))
  131. {
  132. $objects[] = $object;
  133. }
  134. }
  135. }
  136. return new ArrayResultSet($objects);
  137. }
  138. function retrieve_folders($folder_url)
  139. {
  140. $folders = array();
  141. $files = $this->retrieve_folder('/', $condition, $order_property, $offset, $count);
  142. foreach ($files['contents'] as $file)
  143. {
  144. if ($file['is_dir'] == 1)
  145. {
  146. $folder[] = array();
  147. $folder['title'] = substr($file['path'], strripos($file['path'], '/') + 1);
  148. $folder['url'] = str_replace('__PLACEHOLDER__', substr($file['path'], 1), $folder_url);
  149. $folder['class'] = 'category';
  150. $folder['sub'] = $this->get_folder_tree($folder_url, $file['path']);
  151. $folders[] = $folder;
  152. }
  153. }
  154. return $folders;
  155. }
  156. function get_folder_tree($folder_url, $folder_path)
  157. {
  158. $folders = $this->retrieve_folder($this->encode($folder_path), $condition, $order_property, $offset, $count);
  159. $items = array();
  160. foreach ($folders['contents'] as $child)
  161. {
  162. if ($child['is_dir'] == 1)
  163. {
  164. $sub_folder = array();
  165. $sub_folder['title'] = substr($child['path'], strripos($child['path'], '/') + 1);
  166. $sub_folder['url'] = str_replace('__PLACEHOLDER__', $child['path'], $folder_url);
  167. $sub_folder['class'] = 'category';
  168. $sub_folder['sub'] = $this->get_folder_tree($folder_url, $child['path']);
  169. $items[] = $sub_folder;
  170. }
  171. }
  172. return $items;
  173. }
  174. /**
  175. * @param mixed $condition
  176. * @return int
  177. */
  178. function count_external_repository_objects($condition)
  179. {
  180. $files = $this->retrieve_files($condition, $order_property, $offset, $count);
  181. $objects = array();
  182. $count = 0;
  183. foreach ($files['contents'] as $file)
  184. {
  185. if ($file['is_dir'] != 1)
  186. {
  187. $count ++;
  188. }
  189. }
  190. return $count;
  191. }
  192. /**
  193. * @param string $query
  194. * @return string
  195. */
  196. static function translate_search_query($query)
  197. {
  198. return $query;
  199. }
  200. /**
  201. * @param ObjectTableOrder $order_properties
  202. * @return string|null
  203. */
  204. function convert_order_property($order_properties)
  205. {
  206. if (count($order_properties) > 0)
  207. {
  208. $order_property = $order_properties[0]->get_property();
  209. if ($order_property == self :: SORT_RELEVANCE)
  210. {
  211. return $order_property;
  212. }
  213. else
  214. {
  215. $sorting_direction = $order_properties[0]->get_direction();
  216. if ($sorting_direction == SORT_ASC)
  217. {
  218. return $order_property . '-asc';
  219. }
  220. elseif ($sorting_direction == SORT_DESC)
  221. {
  222. return $order_property . '-desc';
  223. }
  224. }
  225. }
  226. return null;
  227. }
  228. /**
  229. * @return array
  230. */
  231. static function get_sort_properties()
  232. {
  233. $feed_type = Request :: get(DropboxExternalRepositoryManager :: PARAM_FEED_TYPE);
  234. $query = ActionBarSearchForm :: get_query();
  235. if (($feed_type == DropboxExternalRepositoryManager :: FEED_TYPE_GENERAL && $query))
  236. {
  237. return array(self :: SORT_DATE_CREATED);
  238. }
  239. else
  240. {
  241. return array();
  242. }
  243. }
  244. /* (non-PHPdoc)
  245. * @see application/common/external_repository_manager/ExternalRepositoryConnector#retrieve_external_repository_object()
  246. */
  247. function retrieve_external_repository_object($id)
  248. {
  249. $file = $this->dropbox->getMetaData($this->encode($id));
  250. $object = new DropboxExternalRepositoryObject();
  251. $object->set_external_repository_id($this->get_external_repository_instance_id());
  252. $object->set_id($id);
  253. $object->set_title(str_replace('/', '', substr($id, strripos($id, '/'))));
  254. $object->set_created($file['modified']);
  255. $object->set_modified($file['modified']);
  256. $object->set_type($file['icon']);
  257. $object->set_description($file['size']);
  258. $object->set_rights($this->determine_rights());
  259. return $object;
  260. }
  261. function determine_rights()
  262. {
  263. $rights = array();
  264. $rights[ExternalRepositoryObject :: RIGHT_USE] = true;
  265. $rights[ExternalRepositoryObject :: RIGHT_EDIT] = false;
  266. $rights[ExternalRepositoryObject :: RIGHT_DELETE] = true;
  267. $rights[ExternalRepositoryObject :: RIGHT_DOWNLOAD] = true;
  268. return $rights;
  269. }
  270. /**
  271. * @param array $values
  272. * @param string $file_path
  273. * @return mixed
  274. */
  275. function create_external_repository_object($file, $file_path)
  276. {
  277. $file = str_replace(' ', '', $file);
  278. return $this->dropbox->putFile($file, $file_path);
  279. }
  280. /**
  281. * @param ContentObject $content_object
  282. * @return mixed
  283. */
  284. function export_external_repository_object($content_object)
  285. {
  286. $file = str_replace(' ', '', $content_object->get_title());
  287. return $this->dropbox->putFile($file, $content_object->get_full_path());
  288. }
  289. /**
  290. * @param string $id
  291. * @return mixed
  292. */
  293. function delete_external_repository_object($id)
  294. {
  295. return $this->dropbox->delete($id);
  296. }
  297. function download_external_repository_object($id)
  298. {
  299. return $this->dropbox->getFile($this->encode($id));
  300. }
  301. function update_external_repository_object($values)
  302. {
  303. }
  304. function encode($path)
  305. {
  306. $file = explode('/', $path);
  307. $newpath = array();
  308. foreach ($file as $f)
  309. {
  310. $newpath[] = rawurlencode($f);
  311. }
  312. return implode('/', $newpath);
  313. }
  314. function create_external_repository_folder($folder)
  315. {
  316. return $this->dropbox->createFolder($folder);
  317. }
  318. }
  319. ?>