PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/portfolio/boxnet/lib.php

https://github.com/tcubansk/moodle
PHP | 256 lines | 219 code | 27 blank | 10 comment | 43 complexity | 5fb374349a02a4ea0bbbff40086b2029 MD5 | raw file
  1. <?php
  2. require_once($CFG->libdir.'/portfolio/plugin.php');
  3. require_once($CFG->libdir.'/filelib.php');
  4. require_once($CFG->libdir.'/boxlib.php');
  5. class portfolio_plugin_boxnet extends portfolio_plugin_push_base {
  6. public $boxclient;
  7. private $ticket;
  8. private $authtoken;
  9. private $folders;
  10. private $accounttree;
  11. public static function get_name() {
  12. return get_string('pluginname', 'portfolio_boxnet');
  13. }
  14. public function prepare_package() {
  15. // don't do anything for this plugin, we want to send all files as they are.
  16. }
  17. public function send_package() {
  18. // if we need to create the folder, do it now
  19. if ($newfolder = $this->get_export_config('newfolder')) {
  20. if (!$created = $this->boxclient->createFolder($newfolder, array('share' => (int)$this->get_export_config('sharefolder')))) {
  21. throw new portfolio_plugin_exception('foldercreatefailed', 'portfolio_boxnet');
  22. }
  23. $this->folders[$created['folder_id']] = $created['folder_name'];
  24. $this->set_export_config(array('folder' => $created['folder_id']));
  25. }
  26. foreach ($this->exporter->get_tempfiles() as $file) {
  27. $return = $this->boxclient->uploadFile(
  28. array(
  29. 'file' => $file,
  30. 'folder_id' => $this->get_export_config('folder'),
  31. 'share' => $this->get_export_config('sharefile'),
  32. )
  33. );
  34. if (array_key_exists('status', $return) && $return['status'] == 'upload_ok'
  35. && array_key_exists('id', $return) && count($return['id']) == 1) {
  36. $this->rename_file($return['id'][array_pop(array_keys($return['id']))], $file->get_filename());
  37. // if this fails, the file was sent but not renamed - this triggers a warning but is not fatal.
  38. }
  39. }
  40. if ($this->boxclient->isError()) {
  41. throw new portfolio_plugin_exception('sendfailed', 'portfolio_boxnet', $this->boxclient->getErrorMsg());
  42. }
  43. }
  44. public function get_export_summary() {
  45. $allfolders = $this->get_folder_list();
  46. if ($newfolder = $this->get_export_config('newfolder')) {
  47. $foldername = $newfolder . ' (' . get_string('tobecreated', 'portfolio_boxnet') . ')';
  48. } elseif ($this->get_export_config('folder')) {
  49. $foldername = $allfolders[$this->get_export_config('folder')];
  50. } else {
  51. $foldername = '';
  52. }
  53. return array(
  54. get_string('targetfolder', 'portfolio_boxnet') => $foldername
  55. );
  56. }
  57. public function get_interactive_continue_url() {
  58. return 'http://box.net/files#0:f:' . $this->get_export_config('folder');
  59. }
  60. public function expected_time($callertime) {
  61. return $callertime;
  62. }
  63. public static function has_admin_config() {
  64. return true;
  65. }
  66. public static function get_allowed_config() {
  67. return array('apikey');
  68. }
  69. public function has_export_config() {
  70. return true;
  71. }
  72. public function get_allowed_user_config() {
  73. return array('authtoken', 'authtokenctime');
  74. }
  75. public function get_allowed_export_config() {
  76. return array('folder', 'newfolder', 'sharefile', 'sharefolder');
  77. }
  78. public function export_config_form(&$mform) {
  79. $folders = $this->get_folder_list();
  80. $mform->addElement('checkbox', 'plugin_sharefile', get_string('sharefile', 'portfolio_boxnet'));
  81. $mform->addElement('text', 'plugin_newfolder', get_string('newfolder', 'portfolio_boxnet'));
  82. $mform->addElement('checkbox', 'plugin_sharefolder', get_string('sharefolder', 'portfolio_boxnet'));
  83. $folders[0] = '----';
  84. ksort($folders);
  85. $mform->addElement('select', 'plugin_folder', get_string('existingfolder', 'portfolio_boxnet'), $folders);
  86. }
  87. public function export_config_validation(array $data) {
  88. $allfolders = $this->get_folder_list();
  89. if (in_array($data['plugin_newfolder'], $allfolders)) {
  90. return array('plugin_newfolder' => get_string('folderclash', 'portfolio_boxnet'));
  91. }
  92. }
  93. public function admin_config_form(&$mform) {
  94. global $CFG;
  95. $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_boxnet'));
  96. $mform->addRule('apikey', get_string('required'), 'required', null, 'client');
  97. $a = new stdClass();
  98. $a->servicesurl = 'http://www.box.net/developers/services';
  99. $a->callbackurl = $CFG->wwwroot . '/portfolio/add.php?postcontrol=1&type=boxnet';
  100. $mform->addElement('static', 'setupinfo', get_string('setupinfo', 'portfolio_boxnet'),
  101. get_string('setupinfodetails', 'portfolio_boxnet', $a));
  102. }
  103. public function steal_control($stage) {
  104. if ($stage != PORTFOLIO_STAGE_CONFIG) {
  105. return false;
  106. }
  107. if ($this->authtoken) {
  108. return false;
  109. }
  110. if (!$this->ensure_ticket()) {
  111. throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
  112. }
  113. $token = $this->get_user_config('authtoken', $this->get('user')->id);
  114. $ctime= $this->get_user_config('authtokenctime', $this->get('user')->id);
  115. if (!empty($token) && (($ctime + 60*60*20) > time())) {
  116. $this->authtoken = $token;
  117. $this->boxclient->auth_token = $token;
  118. return false;
  119. }
  120. return 'http://www.box.net/api/1.0/auth/'.$this->ticket;
  121. }
  122. public function post_control($stage, $params) {
  123. if ($stage != PORTFOLIO_STAGE_CONFIG) {
  124. return;
  125. }
  126. if (!array_key_exists('auth_token', $params) || empty($params['auth_token'])) {
  127. throw new portfolio_plugin_exception('noauthtoken', 'portfolio_boxnet');
  128. }
  129. $this->authtoken = $params['auth_token'];
  130. $this->boxclient->auth_token = $this->authtoken;
  131. $this->set_user_config(array('authtoken' => $this->authtoken, 'authtokenctime' => time()), $this->get('user')->id);
  132. }
  133. private function ensure_ticket() {
  134. if (!empty($this->boxclient)) {
  135. return true;
  136. }
  137. $this->boxclient = new boxclient($this->get_config('apikey'), '');
  138. $ticket_return = $this->boxclient->getTicket();
  139. if ($this->boxclient->isError() || empty($ticket_return)) {
  140. throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
  141. }
  142. $this->ticket = $ticket_return['ticket'];
  143. return $this->ticket;
  144. }
  145. private function ensure_account_tree() {
  146. if (!empty($this->accounttree)) {
  147. return;
  148. }
  149. if (empty($this->ticket)
  150. || empty($this->authtoken)
  151. || empty($this->boxclient)) {
  152. // if we don't have these we're pretty much screwed
  153. throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
  154. return false;
  155. }
  156. $this->accounttree = $this->boxclient->getAccountTree();
  157. if ($this->boxclient->isError()) {
  158. throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
  159. }
  160. if (!is_array($this->accounttree)) {
  161. return false;
  162. }
  163. }
  164. private function get_folder_list() {
  165. if (!empty($this->folders)) {
  166. return $this->folders;
  167. }
  168. $this->ensure_account_tree();
  169. $folders = array();
  170. foreach ($this->accounttree['folder_id'] as $key => $id) {
  171. if (empty($id)) {
  172. continue;
  173. }
  174. $name = $this->accounttree['folder_name'][$key];
  175. if (!empty($this->accounttree['shared'][$key])) {
  176. $name .= ' (' . get_string('sharedfolder', 'portfolio_boxnet') . ')';
  177. }
  178. $folders[$id] = $name;
  179. }
  180. $this->folders = $folders;
  181. return $folders;
  182. }
  183. private function rename_file($fileid, $newname) {
  184. // look at moving this to the boxnet client class
  185. $this->ensure_account_tree();
  186. $count = 1;
  187. $bits = explode('.', $newname);
  188. $suffix = '';
  189. if (count($bits) == 1) {
  190. $prefix = $newname;
  191. } else {
  192. $suffix = '.' . array_pop($bits);
  193. $prefix = implode('.', $bits);
  194. }
  195. while (true) {
  196. if (!array_key_exists('file_name', $this->accounttree) || !in_array($newname, $this->accounttree['file_name'])) {
  197. for ($i = 0; $i < 2; $i++) {
  198. if ($this->boxclient->renameFile($fileid, $newname)) {
  199. return true;
  200. }
  201. }
  202. debugging("tried three times to rename file and failed");
  203. return false;
  204. }
  205. $newname = $prefix . '(' . $count . ')' . $suffix;
  206. $count++;
  207. }
  208. return false;
  209. }
  210. public function instance_sanity_check() {
  211. if (!$this->get_config('apikey')) {
  212. return 'err_noapikey';
  213. }
  214. //@TODO see if we can verify the api key without actually getting an authentication token
  215. }
  216. public static function allows_multiple_instances() {
  217. return false;
  218. }
  219. public function supported_formats() {
  220. return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML);
  221. }
  222. /*
  223. * for now , boxnet doesn't support this,
  224. * because we can't dynamically construct return urls.
  225. */
  226. public static function allows_multiple_exports() {
  227. return false;
  228. }
  229. }