diff options
-rw-r--r-- | apps/calendar/ajax/import/import.php | 20 | ||||
-rw-r--r-- | apps/calendar/import_tmp/Info | 2 | ||||
-rw-r--r-- | apps/calendar/js/loader.js | 12 | ||||
-rw-r--r-- | apps/calendar/templates/part.import.php | 2 | ||||
-rw-r--r-- | apps/contacts/import.php | 21 | ||||
-rw-r--r-- | apps/contacts/import_tmp/Info | 2 | ||||
-rw-r--r-- | apps/contacts/js/loader.js | 12 | ||||
-rw-r--r-- | apps/contacts/templates/part.import.php | 2 | ||||
-rw-r--r-- | lib/cache.php | 40 | ||||
-rw-r--r-- | lib/cache/file.php | 50 |
10 files changed, 127 insertions, 36 deletions
diff --git a/apps/calendar/ajax/import/import.php b/apps/calendar/ajax/import/import.php index 904c07c52e7..a3eaed844a1 100644 --- a/apps/calendar/ajax/import/import.php +++ b/apps/calendar/ajax/import/import.php @@ -10,18 +10,22 @@ ob_start(); OCP\JSON::checkLoggedIn(); OCP\App::checkAppEnabled('calendar'); +session_write_close(); $nl="\r\n"; $comps = array('VEVENT'=>true, 'VTODO'=>true, 'VJOURNAL'=>true); -$progressfile = 'import_tmp/' . md5(session_id()) . '.txt'; +global $progresskey; +$progresskey = 'calendar.import-' . $_GET['progresskey']; + +if (isset($_GET['progress']) && $_GET['progress']) { + echo OC_Cache::get($progresskey); + die; +} function writeProgress($pct) { - if(is_writable('import_tmp/')){ - $progressfopen = fopen($progressfile, 'w'); - fwrite($progressfopen, $pct); - fclose($progressfopen); - } + global $progresskey; + OC_Cache::set($progresskey, $pct, 300); } writeProgress('10'); $file = OC_Filesystem::file_get_contents($_POST['path'] . '/' . $_POST['file']); @@ -114,7 +118,5 @@ foreach($uids as $uid) { // finished import writeProgress('100'); sleep(3); -if(is_writable('import_tmp/')){ - unlink($progressfile); -} +OC_Cache::remove($progresskey); OCP\JSON::success(); diff --git a/apps/calendar/import_tmp/Info b/apps/calendar/import_tmp/Info deleted file mode 100644 index abafbce435c..00000000000 --- a/apps/calendar/import_tmp/Info +++ /dev/null @@ -1,2 +0,0 @@ -This folder contains static files with the percentage of the import. -Requires write permission diff --git a/apps/calendar/js/loader.js b/apps/calendar/js/loader.js index 60d92f448ee..838521ec7f5 100644 --- a/apps/calendar/js/loader.js +++ b/apps/calendar/js/loader.js @@ -43,8 +43,8 @@ Calendar_Import={ } $('#newcalendar').attr('readonly', 'readonly'); $('#calendar').attr('disabled', 'disabled'); - var progressfile = $('#progressfile').val(); - $.post(OC.filePath('calendar', 'ajax/import', 'import.php'), {method: String (method), calname: String (calname), path: String (path), file: String (filename), id: String (calid)}, function(data){ + var progresskey = $('#progresskey').val(); + $.post(OC.filePath('calendar', 'ajax/import', 'import.php') + '?progresskey='+progresskey, {method: String (method), calname: String (calname), path: String (path), file: String (filename), id: String (calid)}, function(data){ if(data.status == 'success'){ $('#progressbar').progressbar('option', 'value', 100); $('#import_done').css('display', 'block'); @@ -52,7 +52,7 @@ Calendar_Import={ }); $('#form_container').css('display', 'none'); $('#progressbar_container').css('display', 'block'); - window.setTimeout('Calendar_Import.getimportstatus(\'' + progressfile + '\')', 500); + window.setTimeout('Calendar_Import.getimportstatus(\'' + progresskey + '\')', 500); }); $('#calendar').change(function(){ if($('#calendar option:selected').val() == 'newcal'){ @@ -62,11 +62,11 @@ Calendar_Import={ } }); }, - getimportstatus: function(progressfile){ - $.get(OC.filePath('calendar', 'import_tmp', progressfile), function(percent){ + getimportstatus: function(progresskey){ + $.get(OC.filePath('calendar', 'ajax/import', 'import.php') + '?progress=1&progresskey=' + progresskey, function(percent){ $('#progressbar').progressbar('option', 'value', parseInt(percent)); if(percent < 100){ - window.setTimeout('Calendar_Import.getimportstatus(\'' + progressfile + '\')', 500); + window.setTimeout('Calendar_Import.getimportstatus(\'' + progresskey + '\')', 500); }else{ $('#import_done').css('display', 'block'); } diff --git a/apps/calendar/templates/part.import.php b/apps/calendar/templates/part.import.php index e93ea1af4c9..39cda29c20d 100644 --- a/apps/calendar/templates/part.import.php +++ b/apps/calendar/templates/part.import.php @@ -2,7 +2,7 @@ <div id="form_container"> <input type="hidden" id="filename" value="<?php echo $_['filename'];?>"> <input type="hidden" id="path" value="<?php echo $_['path'];?>"> -<input type="hidden" id="progressfile" value="<?php echo md5(session_id()) . '.txt';?>"> +<input type="hidden" id="progresskey" value="<?php echo rand() ?>"> <p style="text-align:center;"><b><?php echo $l->t('Please choose the calendar'); ?></b></p> <select style="width:100%;" id="calendar" name="calendar"> <?php diff --git a/apps/contacts/import.php b/apps/contacts/import.php index fdea4975d59..0ee35f9fd81 100644 --- a/apps/contacts/import.php +++ b/apps/contacts/import.php @@ -10,16 +10,21 @@ ob_start(); OCP\JSON::checkLoggedIn(); OCP\App::checkAppEnabled('contacts'); +session_write_close(); + $nl = "\n"; -$progressfile = 'import_tmp/' . md5(session_id()) . '.txt'; +global $progresskey; +$progresskey = 'contacts.import-' . $_GET['progresskey']; + +if (isset($_GET['progress']) && $_GET['progress']) { + echo OC_Cache::get($progresskey); + die; +} function writeProgress($pct) { - if(is_writable('import_tmp/')){ - $progressfopen = fopen($progressfile, 'w'); - fwrite($progressfopen, $pct); - fclose($progressfopen); - } + global $progresskey; + OC_Cache::set($progresskey, $pct, 300); } writeProgress('10'); $view = $file = null; @@ -93,9 +98,7 @@ foreach($parts as $part){ //done the import writeProgress('100'); sleep(3); -if(is_writable('import_tmp/')){ - unlink($progressfile); -} +OC_Cache::remove($progresskey); if(isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') { if(!$view->unlink('/' . $_POST['file'])) { OCP\Util::writeLog('contacts','Import: Error unlinking OC_FilesystemView ' . '/' . $_POST['file'], OCP\Util::ERROR); diff --git a/apps/contacts/import_tmp/Info b/apps/contacts/import_tmp/Info deleted file mode 100644 index abafbce435c..00000000000 --- a/apps/contacts/import_tmp/Info +++ /dev/null @@ -1,2 +0,0 @@ -This folder contains static files with the percentage of the import. -Requires write permission diff --git a/apps/contacts/js/loader.js b/apps/contacts/js/loader.js index 8c79ea8a1d6..961e0f425c9 100644 --- a/apps/contacts/js/loader.js +++ b/apps/contacts/js/loader.js @@ -42,8 +42,8 @@ Contacts_Import={ }
$('#newaddressbook').attr('readonly', 'readonly');
$('#contacts').attr('disabled', 'disabled');
- var progressfile = $('#progressfile').val();
- $.post(OC.filePath('contacts', '', 'import.php'), {method: String (method), addressbookname: String (addressbookname), path: String (path), file: String (filename), id: String (addressbookid)}, function(jsondata){
+ var progresskey = $('#progresskey').val(); + $.post(OC.filePath('contacts', '', 'import.php') + '?progresskey='+progresskey, {method: String (method), addressbookname: String (addressbookname), path: String (path), file: String (filename), id: String (addressbookid)}, function(jsondata){ if(jsondata.status == 'success'){
$('#progressbar').progressbar('option', 'value', 100);
$('#import_done').find('p').html(t('contacts', 'Result: ') + jsondata.data.imported + t('contacts', ' imported, ') + jsondata.data.failed + t('contacts', ' failed.'));
@@ -55,7 +55,7 @@ Contacts_Import={ });
$('#form_container').css('display', 'none');
$('#progressbar_container').css('display', 'block');
- window.setTimeout('Contacts_Import.getimportstatus(\'' + progressfile + '\')', 500);
+ window.setTimeout('Contacts_Import.getimportstatus(\'' + progresskey + '\')', 500); });
$('#contacts').change(function(){
if($('#contacts option:selected').val() == 'newaddressbook'){
@@ -65,11 +65,11 @@ Contacts_Import={ }
});
},
- getimportstatus: function(progressfile){
- $.get(OC.filePath('contacts', 'import_tmp', progressfile), function(percent){
+ getimportstatus: function(progresskey){ + $.get(OC.filePath('contacts', '', 'import.php') + '?progress=1&progresskey=' + progresskey, function(percent){ $('#progressbar').progressbar('option', 'value', parseInt(percent));
if(percent < 100){
- window.setTimeout('Contacts_Import.getimportstatus(\'' + progressfile + '\')', 500);
+ window.setTimeout('Contacts_Import.getimportstatus(\'' + progresskey + '\')', 500); }else{
$('#import_done').css('display', 'block');
}
diff --git a/apps/contacts/templates/part.import.php b/apps/contacts/templates/part.import.php index b8793042997..32c8dc50dd6 100644 --- a/apps/contacts/templates/part.import.php +++ b/apps/contacts/templates/part.import.php @@ -2,7 +2,7 @@ <div id="form_container"> <input type="hidden" id="filename" value="<?php echo $_['filename'];?>"> <input type="hidden" id="path" value="<?php echo $_['path'];?>"> - <input type="hidden" id="progressfile" value="<?php echo md5(session_id()) . '.txt';?>"> + <input type="hidden" id="progresskey" value="<?php echo rand() ?>"> <p class="bold" style="text-align:center;"><?php echo $l->t('Please choose the addressbook'); ?></p> <select style="width:100%;" id="contacts" name="contacts"> <?php diff --git a/lib/cache.php b/lib/cache.php new file mode 100644 index 00000000000..a4fb2448432 --- /dev/null +++ b/lib/cache.php @@ -0,0 +1,40 @@ +<?php +/** + * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class OC_Cache { + static protected $cache; + + static protected function init() { + self::$cache = new OC_Cache_File(); + } + + static public function get($key) { + if (!self::$cache) { + self::init(); + } + return self::$cache->get($key); + } + + static public function set($key, $value, $ttl=0) { + if (empty($key)) { + return false; + } + if (!self::$cache) { + self::init(); + } + return self::$cache->set($key, $value, $ttl); + } + + static public function remove($key) { + if (!self::$cache) { + self::init(); + } + return self::$cache->remove($key); + } + +} diff --git a/lib/cache/file.php b/lib/cache/file.php new file mode 100644 index 00000000000..02aad5187ee --- /dev/null +++ b/lib/cache/file.php @@ -0,0 +1,50 @@ +<?php +/** + * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + + +class OC_Cache_File { + protected function getStorage() { + if(OC_User::isLoggedIn()){ + $subdir = 'cache'; + $view = new OC_FilesystemView('/'.OC_User::getUser()); + if(!$view->file_exists($subdir)) { + $view->mkdir($subdir); + } + return new OC_FilesystemView('/'.OC_User::getUser().'/'.$subdir); + }else{ + OC_Log::write('core','Can\'t get cache storage, user not logged in', OC_Log::ERROR); + return false; + } + } + + public function get($key) { + $storage = $this->getStorage(); + if ($storage->is_file($key)) { + $mtime = $storage->filemtime($key); + if ($mtime < time()) { + $storage->unlink($key); + return false; + } + return $storage->file_get_contents($key); + } + return false; + } + + public function set($key, $value, $ttl) { + $storage = $this->getStorage(); + if ($storage->file_put_contents($key, $value)) { + return $storage->touch($key, time() + $ttl); + } + return false; + } + + public function remove($key) { + $storage = $this->getStorage(); + return $storage->unlink($key); + } +} |