[ Index ]

Source Code Reference for V1.00

title

Body

[close]

/modules/files/ -> files.class.php (source)

   1  <?php /* $Id: files.class.php 102 2008-03-18 19:52:59Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/modules/files/files.class.php $ */
   2  if (!defined('W2P_BASE_DIR')) {
   3      die('You should not access this file directly.');
   4  }
   5  
   6  require_once ($AppUI->getSystemClass('libmail'));
   7  require_once ($AppUI->getSystemClass('w2p'));
   8  require_once ($AppUI->getSystemClass('date'));
   9  require_once ($AppUI->getModuleClass('tasks'));
  10  require_once ($AppUI->getModuleClass('projects'));
  11  global $helpdesk_available;
  12  
  13  if ($helpdesk_available = $AppUI->isActiveModule('helpdesk')) {
  14      require_once ($AppUI->getModuleClass('helpdesk'));
  15  }
  16  /**

  17   * File Class

  18   */
  19  class CFile extends CW2pObject {
  20  
  21      var $file_id = null;
  22      var $file_version_id = null;
  23      var $file_project = null;
  24      var $file_real_filename = null;
  25      var $file_task = null;
  26      var $file_name = null;
  27      var $file_parent = null;
  28      var $file_description = null;
  29      var $file_type = null;
  30      var $file_owner = null;
  31      var $file_date = null;
  32      var $file_size = null;
  33      var $file_version = null;
  34      var $file_category = null;
  35      var $file_folder = null;
  36      var $file_checkout = null;
  37      var $file_co_reason = null;
  38  
  39      // This "breaks" check-in/upload if helpdesk is not present class variable needs to be added "dymanically"

  40      //var $file_helpdesk_item = NULL;

  41  
  42  	function CFile() {
  43          global $AppUI, $helpdesk_available;
  44          if ($helpdesk_available) {
  45              $this->file_helpdesk_item = null;
  46          }
  47          $this->CW2pObject('files', 'file_id');
  48      }
  49  
  50  	function store() {
  51          global $helpdesk_available;
  52          if ($helpdesk_available && $this->file_helpdesk_item != 0) {
  53              $this->addHelpDeskTaskLog();
  54          }
  55          parent::store();
  56      }
  57  
  58  	function addHelpDeskTaskLog() {
  59          global $AppUI, $helpdesk_available;
  60          if ($helpdesk_available && $this->file_helpdesk_item != 0) {
  61  
  62              // create task log with information about the file that was uploaded

  63              $task_log = new CHDTaskLog();
  64              $task_log->task_log_help_desk_id = $this->_hditem->item_id;
  65              if ($this->_message != 'deleted') {
  66                  $task_log->task_log_name = 'File ' . $this->file_name . ' uploaded';
  67              } else {
  68                  $task_log->task_log_name = 'File ' . $this->file_name . ' deleted';
  69              }
  70              $task_log->task_log_description = $this->file_description;
  71              $task_log->task_log_creator = $AppUI->user_id;
  72              $date = new CDate();
  73              $task_log->task_log_date = $date->format(FMT_DATETIME_MYSQL);
  74              if ($msg = $task_log->store()) {
  75                  $AppUI->setMsg($msg, UI_MSG_ERROR);
  76              }
  77          }
  78          return null;
  79      }
  80  
  81  	function canAdmin() {
  82          global $AppUI;
  83  
  84          if (!$this->file_project)
  85              return false;
  86          if (!$this->file_id)
  87              return false;
  88  
  89          $result = false;
  90          $this->_query->clear();
  91          $this->_query->addTable('projects');
  92          $this->_query->addQuery('project_owner');
  93          $this->_query->addWhere('project_id = ' . (int)$this->file_project);
  94          $res = $this->_query->exec(ADODB_FETCH_ASSOC);
  95          if ($res && $row = $q->fetchRow()) {
  96              if ($row['project_owner'] == $AppUI->user_id)
  97                  $result = true;
  98          }
  99          $this->_query->clear();
 100          return $result;
 101      }
 102  
 103  	function check() {
 104          // ensure the integrity of some variables

 105          $this->file_id = intval($this->file_id);
 106          $this->file_version_id = intval($this->file_version_id);
 107          $this->file_parent = intval($this->file_parent);
 108          $this->file_task = intval($this->file_task);
 109          $this->file_project = intval($this->file_project);
 110  
 111          return null; // object is ok

 112      }
 113  
 114  	function checkout($userId, $fileId, $coReason) {
 115          $q = new DBQuery;
 116          $q->addTable('files');
 117          $q->addUpdate('file_checkout', $userId);
 118          $q->addUpdate('file_co_reason', $coReason);
 119          $q->addWhere('file_id = ' . (int)$fileId);
 120          $q->exec();
 121          $q->clear();
 122  
 123          return true;
 124      }
 125  
 126  	function delete() {
 127          global $helpdesk_available;
 128          if (!$this->canDelete($msg))
 129              return $msg;
 130          $this->_message = 'deleted';
 131          addHistory('files', $this->file_id, 'delete', $this->file_name, $this->file_project);
 132          // remove the file from the file system

 133          $this->deleteFile();
 134          // delete any index entries

 135          $q = new DBQuery;
 136          $q->setDelete('files_index');
 137          $q->addQuery('*');
 138          $q->addWhere('file_id = ' . (int)$this->file_id);
 139          if (!$q->exec()) {
 140              $q->clear();
 141              return db_error();
 142          }
 143          // delete the main table reference

 144          $q->clear();
 145          $q->setDelete('files');
 146          $q->addQuery('*');
 147          $q->addWhere('file_id = ' . (int)$this->file_id);
 148          if (!$q->exec()) {
 149              $q->clear();
 150              return db_error();
 151          }
 152          $q->clear();
 153  
 154          if ($helpdesk_available && $this->file_helpdesk_item != 0) {
 155              $this->addHelpDeskTaskLog();
 156          }
 157          return null;
 158      }
 159  
 160      // delete File from File System

 161  	function deleteFile() {
 162          global $w2Pconfig;
 163          return @unlink(W2P_BASE_DIR . '/files/' . $this->file_project . '/' . $this->file_real_filename);
 164      }
 165  
 166      // move the file if the affiliated project was changed

 167  	function moveFile($oldProj, $realname) {
 168          global $AppUI, $w2Pconfig;
 169          if (!is_dir(W2P_BASE_DIR . '/files/' . $this->file_project)) {
 170              $res = mkdir(W2P_BASE_DIR . '/files/' . $this->file_project, 0777);
 171              if (!$res) {
 172                  $AppUI->setMsg('Upload folder not setup to accept uploads - change permission on files/ directory.', UI_MSG_ALLERT);
 173                  return false;
 174              }
 175          }
 176          $res = rename(W2P_BASE_DIR . '/files/' . $oldProj . '/' . $realname, W2P_BASE_DIR . '/files/' . $this->file_project . '/' . $realname);
 177  
 178          if (!$res) {
 179              return false;
 180          }
 181          return true;
 182      }
 183  
 184      // duplicate a file into root

 185  	function duplicateFile($oldProj, $realname) {
 186          global $AppUI, $w2Pconfig;
 187          if (!is_dir(W2P_BASE_DIR . '/files/0')) {
 188              $res = mkdir(W2P_BASE_DIR . '/files/0', 0777);
 189              if (!$res) {
 190                  $AppUI->setMsg('Upload folder not setup to accept uploads - change permission on files/ directory.', UI_MSG_ALLERT);
 191                  return false;
 192              }
 193          }
 194          $dest_realname = uniqid(rand());
 195          $res = copy(W2P_BASE_DIR . '/files/' . $oldProj . '/' . $realname, W2P_BASE_DIR . '/files/0/' . $dest_realname);
 196  
 197          if (!$res) {
 198              return false;
 199          }
 200          return $dest_realname;
 201      }
 202  
 203      // move a file from a temporary (uploaded) location to the file system

 204  	function moveTemp($upload) {
 205          global $AppUI, $w2Pconfig;
 206          // check that directories are created

 207          if (!is_dir(W2P_BASE_DIR . '/files')) {
 208              $res = mkdir(W2P_BASE_DIR . '/files', 0777);
 209              if (!$res) {
 210                  return false;
 211              }
 212          }
 213          if (!is_dir(W2P_BASE_DIR . '/files/' . $this->file_project)) {
 214              $res = mkdir(W2P_BASE_DIR . '/files/' . $this->file_project, 0777);
 215              if (!$res) {
 216                  $AppUI->setMsg('Upload folder not setup to accept uploads - change permission on files/ directory.', UI_MSG_ALLERT);
 217                  return false;
 218              }
 219          }
 220  
 221          $this->_filepath = W2P_BASE_DIR . '/files/' . $this->file_project . '/' . $this->file_real_filename;
 222          // move it

 223          $res = move_uploaded_file($upload['tmp_name'], $this->_filepath);
 224          if (!$res) {
 225              return false;
 226          }
 227          return true;
 228      }
 229  
 230      // parse file for indexing

 231  	function indexStrings() {
 232          global $AppUI, $w2Pconfig;
 233          // get the parser application

 234          $parser = $w2Pconfig['parser_' . $this->file_type];
 235          if (!$parser)
 236              $parser = $w2Pconfig['parser_default'];
 237          if (!$parser)
 238              return false;
 239          // buffer the file

 240          $this->_filepath = W2P_BASE_DIR . '/files/' . $this->file_project . '/' . $this->file_real_filename;
 241          $fp = fopen($this->_filepath, 'rb');
 242          $x = fread($fp, $this->file_size);
 243          fclose($fp);
 244          // parse it

 245          $parser = $parser . ' ' . $this->_filepath;
 246          $pos = strpos($parser, '/pdf');
 247          if (false !== $pos) {
 248              $x = `$parser -`;
 249          } else {
 250              $x = `$parser`;
 251          }
 252          // if nothing, return

 253          if (strlen($x) < 1) {
 254              return 0;
 255          }
 256          // remove punctuation and parse the strings

 257          $x = str_replace(array('.', ',', '!', '@', '(', ')'), ' ', $x);
 258          $warr = split('[[:space:]]', $x);
 259  
 260          $wordarr = array();
 261          $nwords = count($warr);
 262          for ($x = 0; $x < $nwords; $x++) {
 263              $newword = $warr[$x];
 264              if (!ereg('[[:punct:]]', $newword) && strlen(trim($newword)) > 2 && !ereg('[[:digit:]]', $newword)) {
 265                  $wordarr[] = array('word' => $newword, 'wordplace' => $x);
 266              }
 267          }
 268          // filter out common strings

 269          $ignore = array();
 270          include  W2P_BASE_DIR . '/modules/files/file_index_ignore.php';
 271          foreach ($ignore as $w) {
 272              unset($wordarr[$w]);
 273          }
 274          // insert the strings into the table

 275          while (list($key, $val) = each($wordarr)) {
 276              $q = new DBQuery;
 277              $q->addTable('files_index');
 278  
 279              $q->addReplace('file_id', $this->file_id);
 280              $q->addReplace('word', $wordarr[$key]['word']);
 281              $q->addReplace('word_placement', $wordarr[$key]['wordplace']);
 282              $q->exec();
 283              $q->clear();
 284          }
 285  
 286          return nwords;
 287      }
 288  
 289      //function notifies about file changing

 290  	function notify() {
 291          global $AppUI, $w2Pconfig, $locale_char_set, $helpdesk_available;
 292          // if helpdesk_item is available send notification to assigned users

 293          if ($helpdesk_available && $this->file_helpdesk_item != 0) {
 294              $this->_hditem = new CHelpDeskItem();
 295              $this->_hditem->load($this->file_helpdesk_item);
 296  
 297              $task_log = new CHDTaskLog();
 298              $task_log_help_desk_id = $this->_hditem->item_id;
 299              // send notifcation about new log entry

 300              // 2 = TASK_LOG

 301              $this->_hditem->notify(2, $task_log->task_log_id);
 302  
 303          }
 304          //if no project specified than we will not do anything

 305          if ($this->file_project != 0) {
 306              $this->_project = new CProject();
 307              $this->_project->load($this->file_project);
 308              $mail = new Mail;
 309  
 310              if ($this->file_task == 0) { //notify all developers
 311                  $mail->Subject($this->_project->project_name . '::' . $this->file_name, $locale_char_set);
 312              } else { //notify all assigned users
 313                  $this->_task = new CTask();
 314                  $this->_task->load($this->file_task);
 315                  $mail->Subject($this->_project->project_name . '::' . $this->_task->task_name . '::' . $this->file_name, $locale_char_set);
 316              }
 317  
 318              $body = $AppUI->_('Project') . ': ' . $this->_project->project_name;
 319              $body .= "\n" . $AppUI->_('URL') . ':     ' . W2P_BASE_URL . '/index.php?m=projects&a=view&project_id=' . $this->_project->project_id;
 320  
 321              if (intval($this->_task->task_id) != 0) {
 322                  $body .= "\n\n" . $AppUI->_('Task') . ':    ' . $this->_task->task_name;
 323                  $body .= "\n" . $AppUI->_('URL') . ':     ' . W2P_BASE_URL . '/index.php?m=tasks&a=view&task_id=' . $this->_task->task_id;
 324                  $body .= "\n" . $AppUI->_('Description') . ':' . "\n" . $this->_task->task_description;
 325  
 326                  //preparing users array

 327                  $q = new DBQuery;
 328                  $q->addTable('tasks', 't');
 329                  $q->addQuery('t.task_id, cc.contact_email as creator_email, cc.contact_first_name as
 330                           creator_first_name, cc.contact_last_name as creator_last_name,
 331                           oc.contact_email as owner_email, oc.contact_first_name as owner_first_name,
 332                           oc.contact_last_name as owner_last_name, a.user_id as assignee_id, 
 333                           ac.contact_email as assignee_email, ac.contact_first_name as
 334                           assignee_first_name, ac.contact_last_name as assignee_last_name');
 335                  $q->addJoin('user_tasks', 'u', 'u.task_id = t.task_id');
 336                  $q->addJoin('users', 'o', 'o.user_id = t.task_owner');
 337                  $q->addJoin('contacts', 'oc', 'o.user_contact = oc.contact_id');
 338                  $q->addJoin('users', 'c', 'c.user_id = t.task_creator');
 339                  $q->addJoin('contacts', 'cc', 'c.user_contact = cc.contact_id');
 340                  $q->addJoin('users', 'a', 'a.user_id = u.user_id');
 341                  $q->addJoin('contacts', 'ac', 'a.user_contact = ac.contact_id');
 342                  $q->addWhere('t.task_id = ' . (int)$this->_task->task_id);
 343                  $this->_users = $q->loadList();
 344              } else {
 345                  //find project owner and notify him about new or modified file

 346                  $q = new DBQuery;
 347                  $q->addTable('users', 'u');
 348                  $q->addTable('projects', 'p');
 349                  $q->addQuery('u.*');
 350                  $q->addWhere('p.project_owner = u.user_id');
 351                  $q->addWhere('p.project_id = ' . (int)$this->file_project);
 352                  $this->_users = $q->loadList();
 353              }
 354              $body .= "\n\nFile " . $this->file_name . ' was ' . $this->_message . ' by ' . $AppUI->user_first_name . ' ' . $AppUI->user_last_name;
 355              if ($this->_message != 'deleted') {
 356                  $body .= "\n" . $AppUI->_('URL') . ':     ' . W2P_BASE_URL . '/fileviewer.php?file_id=' . $this->file_id;
 357                  $body .= "\n" . $AppUI->_('Description') . ':' . "\n" . $this->file_description;
 358              }
 359  
 360              //send mail

 361              $mail->Body($body, isset($GLOBALS['locale_char_set']) ? $GLOBALS['locale_char_set'] : '');
 362  
 363              if (intval($this->_task->task_id) != 0) {
 364                  foreach ($this->_users as $row) {
 365                      if ($row['assignee_id'] != $AppUI->user_id) {
 366                          if ($mail->