Beispiel #1
0
 /**
  * Handles the archiving of faxes. Modifies the filesystem by creating new archive files,
  * and remove existing fax files. Has many opportunities to throw exceptions.
  * 
  * Only accessible by URL of form:
  * .../index.php?module=faxmaster&op=archive&start_date=(UNIX TIMESTAMP)[&end_date=(UNIX TIMESTAMP)]
  */
 private function archiveFaxes()
 {
     PHPWS_Core::initModClass('faxmaster', 'Fax.php');
     // Get date range for archive. Use NOW for end date if one is not supplied.
     $startDate = $_REQUEST['start_date'];
     $endDate = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : time();
     // Check user's permissions
     if (!Current_User::allow('faxmaster', 'archive')) {
         PHPWS_Core::initModClass('faxmaster', 'exception/PermissionException.php');
         throw new PermissionException('Permission denied');
     }
     // SELECT id FROM faxmaster_fax WHERE dateReceived >= start_date AND dateReceived < end_date AND archived=0;
     $db = new PHPWS_DB('faxmaster_fax');
     $db->addColumn('id');
     $db->addWhere('archived', 0);
     // only grab unarchived files
     $db->addWhere('dateReceived', $startDate, '>=', 'AND');
     // startDate is inclusive
     $db->addWhere('dateReceived', $endDate, '<', 'AND');
     // endDate is exclusive
     $results = $db->select();
     // Make archive
     $path = PHPWS_Settings::get('faxmaster', 'archive_path');
     $archiveName = strftime('%m%d%Y', $startDate) . 'to' . strftime('%m%d%Y', $endDate) . '.tar';
     try {
         $archive = new PharData($path . $archiveName);
     } catch (UnexpectedValueException $e) {
         die('Could not open .tar file' . $e->getMessage());
     } catch (BadMethodCallException $e) {
         die('Bad method call' . $e->getMessage());
     }
     // Fill the archive
     foreach ($results as $result) {
         $fax = new Fax($result['id']);
         try {
             $archive->addFile($fax->getFullPath(), $fax->getFileName());
         } catch (PharException $e) {
             die($e->getMessage());
         }
     }
     // Compress the archive
     try {
         $archive = $archive->compress(Phar::GZ);
     } catch (BadMethodCallException $e) {
         die($e->getMessage());
     }
     // Remove .tar, leaving only the .tar.gz
     unlink($path . $archiveName);
     // Update each fax in the database, then remove it from the fax directory
     foreach ($results as $result) {
         $fax = new Fax($result['id']);
         $fax->setArchived(1, $archiveName . '.gz');
         $fax->save();
         unlink($fax->getFullPath());
     }
 }