PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/portfolio/boxnet/lib.php

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