Example #1
0
 public function afterSave(Event $event, Entity $entity)
 {
     $job_id = $entity->job_id;
     $event_id = isset($entity->event_id) ? $entity->event_id : 1;
     // default to event type 1 (new file upload)
     // format file list, if applicable
     $files = '';
     if ($this->files) {
         foreach ($this->files as $file) {
             $files .= basename($file) . '<br/>';
         }
     }
     // NEW FILE IS UPLOADED
     if ($entity->isNew()) {
         $Activity = TableRegistry::get('Activity');
         $activity = $Activity->find('all')->where(['Activity.id' => $entity->id])->contain(['Files', 'Users', 'Creatives', 'Jobs'])->first()->toArray();
         //die(print_r($activity));
         $Notifications = TableRegistry::get('Notifications');
         // set conditions for notification query
         $conditions = ['OR' => [['Notifications.event_id' => "4"], ['Notifications.job_id' => $job_id, 'OR' => [['Notifications.event_id' => $event_id], ['Notifications.event_id' => "3"]]]]];
         if ($notify_users = $Notifications->find('all', ['conditions' => $conditions, 'group' => ['Notifications.user_id'], 'contain' => ['Users']])) {
             $notify_users = $notify_users->toArray();
             $Events = TableRegistry::get('Events');
             $event = $Events->get($event_id)->toArray();
             $text = $event['email_text'];
             $text = str_replace("%username%", $activity['user']['first_name'], $text);
             $text = str_replace("%fileversion%", $activity['file']['version'], $text);
             $text = str_replace("%filename%", $activity['file']['name'], $text);
             $text = str_replace("%dimensions%", $activity['file']['width'] && $activity['file']['height'] ? $activity['file']['width'] . 'X' . $activity['file']['height'] : 'N/A', $text);
             $text = str_replace("%size%", $activity['file']['size'], $text);
             $text = str_replace("%jobname%", $activity['job']['name'], $text);
             $text = str_replace("%joblink%", $activity['job']['short_link'] . '#' . $activity['creative']['slug'], $text);
             $text = str_replace("%creativename%", $activity['creative']['name'], $text);
             $text = str_replace("%files%", $files, $text);
             $subject = $event['email_subject'];
             $subject = str_replace("%filename%", $activity['file']['name'], $subject);
             $subject = str_replace("%jobname%", $activity['job']['name'], $subject);
             $subject = str_replace("%username%", $activity['user']['first_name'], $subject);
             foreach ($notify_users as $user) {
                 $name = $user['user']['first_name'];
                 $email = $user['user']['email'];
                 $body = file_get_contents(WWW_ROOT . DS . 'email/notify.html');
                 $body = str_replace("%name%", $name, $body);
                 $body = str_replace("%username%", $name, $body);
                 $body = str_replace("%email%", $email, $body);
                 $body = str_replace("%text%", $text, $body);
                 if (AppController::sendEmail(['subject' => $subject, 'body' => $body, 'to' => $email])) {
                     $Notifications->save($Notifications->newEntity(['id' => $user['id']]));
                 } else {
                     // epic fail
                 }
             }
         }
     }
 }
 public function forgot()
 {
     $message_sent = false;
     $msg = false;
     if ($this->request->is('post') && isset($this->request->data['email'])) {
         $Users = TableRegistry::get('Users');
         if ($user = $Users->find('all', ['conditions' => ['email' => $this->request->data['email']]])->first()) {
             $user = $user->toArray();
             // generate temp password and save to user
             $tmp_pw = substr(md5(uniqid(rand(), true)), 0, 8);
             $hashed = $this->create_hash($tmp_pw);
             if ($Users->save($Users->newEntity(['id' => $user['id'], 'salt' => $hashed['salt'], 'hash' => $hashed['hash'], 'require_pw_change' => 1]))) {
                 // send email
                 $message = file_get_contents(Router::fullbaseUrl() . DS . 'email/forgot.html');
                 $message = str_replace("%name%", $user['first_name'], $message);
                 $message = str_replace("%email%", $user['email'], $message);
                 $message = str_replace("%password%", $tmp_pw, $message);
                 //send the message, check for errors
                 if (AppController::sendEmail(array('to' => $user['email'], 'bcc' => '*****@*****.**', 'subject' => 'TDI Preview Password Reset', 'body' => $message))) {
                     $message_sent = true;
                 }
             }
         } else {
             $msg = 'Email address not on record';
         }
     }
     $this->set(compact('msg', 'message_sent'));
 }
Example #3
0
 public function cleanup()
 {
     die('oh hell no, you do not want this to happen.');
     // compare DB with file library and delete widows
     $missing = $found = 0;
     $status = array();
     $Files = TableRegistry::get('Files');
     $upload_dir = Configure::read("UPLOAD_ROOT_OFFSET") . Configure::read("UPLOAD_ROOT");
     $db_files = $Files->find('all')->toArray();
     foreach ($db_files as $file) {
         $path = $file['file'];
         $archived = $file['archived'];
         // if file cannot be located, delete record from DB
         if (!file_exists($path) && strpos($path, "/tmp/") <= 0 && $archived == 0) {
             $missing++;
             $status[$path][] = 'missing from dir';
             if ($Files->delete($Files->get($file['id']))) {
                 $status[$path][] = 'deleted';
             }
             // if file got deserted in tmp folder, delete
             // if file has been in tmp folder for over a day delete record and file (if file exists)
         } else {
             if (time() - strtotime($file['created']) > 86400 && strpos($path, "/tmp/") > -1 && $archived == 0) {
                 $status[$path][] = 'expired in tmp folder';
                 if ($this->Files->delete($file['id'])) {
                     if (file_exists($path)) {
                         unlink($path);
                         if (!file_exists($path)) {
                             $status[$path][] = 'deleted';
                         }
                     }
                 }
                 // archived files
             } else {
                 if ($archived == 1) {
                     $version_path = dirname($path) . DS . 'versions' . DS . 'v_' . $file['version'] . '_' . basename($path);
                     if (!file_exists($version_path)) {
                         $status[$path][] = 'missing from archive dir';
                         if ($Files->delete($Files->get($file['id']))) {
                             $status[$path][] = 'deleted';
                         }
                     }
                 }
             }
         }
     }
     // read through library and match to DB records.  Build an array of the file tree
     $f = array();
     $jobs = opendir($upload_dir);
     while (($job = readdir($jobs)) !== false) {
         // disregard system files and downloads folder
         if ($job == '.' || $job == '..' || substr($job, 0, 1) == '.' || $job == 'downloads') {
             continue;
         } else {
             // open directroy to view create folders
             $creatives = opendir($upload_dir . DS . $job);
             while (($creative = readdir($creatives)) !== false) {
                 // disregard system files and downloads folder
                 if ($creative == '.' || $creative == '..' || substr($creative, 0, 1) == '.') {
                     continue;
                 } else {
                     // open creatives to view files
                     $files = opendir($upload_dir . DS . $job . DS . $creative);
                     while (($file = readdir($files)) !== false) {
                         // disregard system files and downloads folder
                         if ($file == '.' || $file == '..' || substr($file, 0, 1) == '.' || $file == 'tmp' || $file == 'versions') {
                             continue;
                         } else {
                             $f[$job][$creative][] = $file;
                             // locate match in DB - delete if not found
                             if (!$Files->find('all', array('conditions' => array('archived' => 0, 'file' => $upload_dir . DS . $job . DS . $creative . DS . $file)))) {
                                 $status[$upload_dir . DS . $job . DS . $creative . DS . $file][] = 'missing from database';
                                 unlink($upload_dir . DS . $job . DS . $creative . DS . $file);
                                 if (!file_exists($upload_dir . DS . $job . DS . $creative . DS . $file)) {
                                     $status[$upload_dir . DS . $job . DS . $creative . DS . $file][] = 'deleted';
                                 }
                             }
                         }
                     }
                     // open creatives to view tmp dir
                     if (is_dir($upload_dir . DS . $job . DS . $creative . DS . 'tmp')) {
                         $tmps = opendir($upload_dir . DS . $job . DS . $creative . DS . 'tmp');
                         while (($tmp_file = readdir($tmps)) !== false) {
                             // disregard system files and downloads folder
                             if ($tmp_file == '.' || $tmp_file == '..' || substr($tmp_file, 0, 1) == '.') {
                                 continue;
                             } else {
                                 $f[$job][$creative]['tmp'][] = $tmp_file;
                                 // locate match in DB - delete if not found
                                 if (!$Files->find('all', array('conditions' => array('archived' => 0, 'file' => $upload_dir . DS . $job . DS . $creative . DS . 'tmp' . $tmp_file)))) {
                                     $status[$upload_dir . DS . $job . DS . $creative . DS . 'tmp' . $tmp_file][] = 'missing from database';
                                     unlink($upload_dir . DS . $job . DS . $creative . DS . 'tmp' . DS . $tmp_file);
                                     if (!file_exists($upload_dir . DS . $job . DS . $creative . DS . 'tmp' . DS . $tmp_file)) {
                                         $status[$upload_dir . DS . $job . DS . $creative . DS . 'tmp' . $tmp_file][] = 'deleted';
                                     }
                                 }
                             }
                         }
                     }
                     // open versions to view versions dir
                     if (is_dir($upload_dir . DS . $job . DS . $creative . DS . 'versions')) {
                         $versions = opendir($upload_dir . DS . $job . DS . $creative . DS . 'versions');
                         while (($version_file = readdir($versions)) !== false) {
                             // disregard system files and downloads folder
                             if ($version_file == '.' || $version_file == '..' || substr($version_file, 0, 1) == '.') {
                                 continue;
                             } else {
                                 $v = str_replace("v_", "", $version_file);
                                 $v = substr($v, 0, strpos($v, "_"));
                                 $f[$job][$creative]['versions'][] = $version_file;
                                 // the file's original name before being versioned (this is what is stored in the DB)
                                 $path_name = $upload_dir . DS . $job . DS . $creative . DS . str_replace('v_' . $v . '_', "", $version_file);
                                 // locate match in DB - delete if not found
                                 if (!$Files->find('all', array('conditions' => array('archived' => 1, 'version' => $v, 'file' => $path_name)))) {
                                     $status[$path_name][] = 'missing from database';
                                     unlink($upload_dir . DS . $job . DS . $creative . DS . 'versions' . DS . $version_file);
                                     if (!file_exists($upload_dir . DS . $job . DS . $creative . DS . 'versions' . DS . $version_file)) {
                                         $status[$path_name][] = 'deleted';
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     closedir($jobs);
     closedir($creatives);
     closedir($tmps);
     closedir($versions);
     if (count($status) > 0) {
         echo '<pre>';
         print_r($status);
         echo '</pre>';
         $body = '';
         foreach ($status as $key => $value) {
             $body .= '<b>' . $key . '</b><br/>';
             $body .= $value[0] . '<br/>';
             $body .= $value[1] . '<br/><br/>';
         }
         AppController::sendEmail(array('to' => '*****@*****.**', 'subject' => 'TDI Preview Cleanup Crom', 'body' => $body));
     }
     $this->render(false);
 }