2 require_once($CFG->libdir.'/portfolio/plugin.php');
3 require_once($CFG->libdir.'/filelib.php');
4 require_once($CFG->libdir.'/boxlib.php');
6 class portfolio_plugin_boxnet extends portfolio_plugin_push_base {
14 public static function get_name() {
15 return get_string('pluginname', 'portfolio_boxnet');
18 public function prepare_package() {
19 // don't do anything for this plugin, we want to send all files as they are.
22 public function send_package() {
23 // if we need to create the folder, do it now
24 if ($newfolder = $this->get_export_config('newfolder')) {
25 if (!$created = $this->boxclient->createFolder($newfolder, array('share' => (int)$this->get_export_config('sharefolder')))) {
26 throw new portfolio_plugin_exception('foldercreatefailed', 'portfolio_boxnet');
28 $this->folders[$created['folder_id']] = $created['folder_name'];
29 $this->set_export_config(array('folder' => $created['folder_id']));
31 foreach ($this->exporter->get_tempfiles() as $file) {
32 $return = $this->boxclient->uploadFile(
35 'folder_id' => $this->get_export_config('folder'),
36 'share' => $this->get_export_config('sharefile'),
39 if (array_key_exists('status', $return) && $return['status'] == 'upload_ok'
40 && array_key_exists('id', $return) && count($return['id']) == 1) {
41 $this->rename_file($return['id'][array_pop(array_keys($return['id']))], $file->get_filename());
42 // if this fails, the file was sent but not renamed - this triggers a warning but is not fatal.
45 if ($this->boxclient->isError()) {
46 throw new portfolio_plugin_exception('sendfailed', 'portfolio_boxnet', $this->boxclient->getErrorMsg());
50 public function get_export_summary() {
51 $allfolders = $this->get_folder_list();
52 if ($newfolder = $this->get_export_config('newfolder')) {
53 $foldername = $newfolder . ' (' . get_string('tobecreated', 'portfolio_boxnet') . ')';
54 } elseif ($this->get_export_config('folder')) {
55 $foldername = $allfolders[$this->get_export_config('folder')];
60 get_string('targetfolder', 'portfolio_boxnet') => $foldername
64 public function get_interactive_continue_url() {
65 return 'http://box.net/files#0:f:' . $this->get_export_config('folder');
68 public function expected_time($callertime) {
72 public static function has_admin_config() {
76 public static function get_allowed_config() {
77 return array('apikey');
80 public function has_export_config() {
84 public function get_allowed_user_config() {
85 return array('authtoken', 'authtokenctime');
88 public function get_allowed_export_config() {
89 return array('folder', 'newfolder', 'sharefile', 'sharefolder');
92 public function export_config_form(&$mform) {
93 $folders = $this->get_folder_list();
94 $mform->addElement('checkbox', 'plugin_sharefile', get_string('sharefile', 'portfolio_boxnet'));
95 $mform->addElement('text', 'plugin_newfolder', get_string('newfolder', 'portfolio_boxnet'));
96 $mform->addElement('checkbox', 'plugin_sharefolder', get_string('sharefolder', 'portfolio_boxnet'));
99 $mform->addElement('select', 'plugin_folder', get_string('existingfolder', 'portfolio_boxnet'), $folders);
102 public function export_config_validation(array $data) {
103 $allfolders = $this->get_folder_list();
104 if (in_array($data['plugin_newfolder'], $allfolders)) {
105 return array('plugin_newfolder' => get_string('folderclash', 'portfolio_boxnet'));
109 public function admin_config_form(&$mform) {
112 $mform->addElement('text', 'apikey', get_string('apikey', 'portfolio_boxnet'));
113 $mform->addRule('apikey', get_string('required'), 'required', null, 'client');
115 $a->servicesurl = 'http://www.box.net/developers/services';
116 $a->callbackurl = $CFG->wwwroot . '/portfolio/add.php?postcontrol=1&type=boxnet';
117 $mform->addElement('static', 'setupinfo', get_string('setupinfo', 'portfolio_boxnet'),
118 get_string('setupinfodetails', 'portfolio_boxnet', $a));
121 public function steal_control($stage) {
122 if ($stage != PORTFOLIO_STAGE_CONFIG) {
125 if ($this->authtoken) {
128 if (!$this->ensure_ticket()) {
129 throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
131 $token = $this->get_user_config('authtoken', $this->get('user')->id);
132 $ctime= $this->get_user_config('authtokenctime', $this->get('user')->id);
133 if (!empty($token) && (($ctime + 60*60*20) > time())) {
134 $this->authtoken = $token;
135 $this->boxclient->auth_token = $token;
138 return 'http://www.box.net/api/1.0/auth/'.$this->ticket;
141 public function post_control($stage, $params) {
142 if ($stage != PORTFOLIO_STAGE_CONFIG) {
145 if (!array_key_exists('auth_token', $params) || empty($params['auth_token'])) {
146 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_boxnet');
148 $this->authtoken = $params['auth_token'];
149 $this->boxclient->auth_token = $this->authtoken;
150 $this->set_user_config(array('authtoken' => $this->authtoken, 'authtokenctime' => time()), $this->get('user')->id);
153 private function ensure_ticket() {
154 if (!empty($this->boxclient)) {
157 $this->boxclient = new boxclient($this->get_config('apikey'), '');
158 $ticket_return = $this->boxclient->getTicket();
159 if ($this->boxclient->isError() || empty($ticket_return)) {
160 throw new portfolio_plugin_exception('noticket', 'portfolio_boxnet');
162 $this->ticket = $ticket_return['ticket'];
163 return $this->ticket;
166 private function ensure_account_tree() {
167 if (!empty($this->accounttree)) {
170 if (empty($this->ticket)
171 || empty($this->authtoken)
172 || empty($this->boxclient)) {
173 // if we don't have these we're pretty much screwed
174 throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
177 $this->accounttree = $this->boxclient->getAccountTree();
178 if ($this->boxclient->isError()) {
179 throw new portfolio_plugin_exception('folderlistfailed', 'portfolio_boxnet');
181 if (!is_array($this->accounttree)) {
186 private function get_folder_list() {
187 if (!empty($this->folders)) {
188 return $this->folders;
190 $this->ensure_account_tree();
192 foreach ($this->accounttree['folder_id'] as $key => $id) {
196 $name = $this->accounttree['folder_name'][$key];
197 if (!empty($this->accounttree['shared'][$key])) {
198 $name .= ' (' . get_string('sharedfolder', 'portfolio_boxnet') . ')';
200 $folders[$id] = $name;
202 $this->folders = $folders;
206 private function rename_file($fileid, $newname) {
207 // look at moving this to the boxnet client class
208 $this->ensure_account_tree();
210 $bits = explode('.', $newname);
212 if (count($bits) == 1) {
215 $suffix = '.' . array_pop($bits);
216 $prefix = implode('.', $bits);
219 if (!array_key_exists('file_name', $this->accounttree) || !in_array($newname, $this->accounttree['file_name'])) {
220 for ($i = 0; $i < 2; $i++) {
221 if ($this->boxclient->renameFile($fileid, $newname)) {
225 debugging("tried three times to rename file and failed");
228 $newname = $prefix . '(' . $count . ')' . $suffix;
234 public function instance_sanity_check() {
235 if (!$this->get_config('apikey')) {
236 return 'err_noapikey';
238 //@TODO see if we can verify the api key without actually getting an authentication token
241 public static function allows_multiple_instances() {
245 public function supported_formats() {
246 return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML);
250 * for now , boxnet doesn't support this,
251 * because we can't dynamically construct return urls.
253 public static function allows_multiple_exports() {