Integrations and APIs for Service

Get Involved. Join the Conversation.

Topic

    Patricia Harvath
    Connect PHP and File Attachment... Please Help ... MY LAST...
    Topic posted May 3, 2013 by Patricia HarvathBronze Medal: 1,250+ Points 
    2396 Views, 44 Comments
    Title:
    Connect PHP and File Attachment... Please Help ... MY LAST DAY
    Content:

    Hi,

    This is my last day and I want to fix this before I go...

    Error when attaching file.

    'File path is not in temporary directory' 
    
    Where is the tmp file?
    

     

    THANKS!!

    Version:
    may 2012
    Code Snippet:

    Comment

    • James Hurster

      I'm hesitant to get too excited.  Am I not seeing it on here?  He is all of the code on that file.

       

      <?php /* Originating Release: May 2012 */
       
      class Fattach_model extends Model
      {
          function __construct()
          {
              parent::__construct();
          }
       
           /**
           * Function to retrieve fattach information from database.
           *
           * @param $fileID int The id of the fattach to retrieve
           * @param $created int date created
           *
           * @return array Details about the file attachment
           */
          function get($fileID, $created)
          {
              $attachment = $this->getAttachmentDetails($fileID, $created);
              if($attachment === false)
                  return false;
          
              if($created && (is_int($created) || ctype_digit($created)))
                  $validatingCreation = true;
       
              extract($attachment);
                  
              //Minimal check to see if user is logged in before viewing non-answer file attachments.
              //Created time must also have been passed as well
              if(($table) && !in_array($table, array(TBL_ANSWERS, TBL_META_ANSWERS, TBL_THREADS)) && (!isLoggedIn() || !$validatingCreation))
              {
                  $this->throwError();
              }
              else if ($table === TBL_INCIDENTS) 
              {
                  $CI = get_instance();
                  $CI->load->model('standard/Incident_model');
                  // Incident_model::get will usually redirect us off to the "access denied" page if 
                  // the current user isn't allowed to access the requested incident
                  $incident = $CI->Incident_model->get($id);
       
                  if (!$incident) {
                      $this->throwError();
                  }
              }
              else if($table === TBL_ANSWERS && $id)
              {
                  $CI = get_instance();
                  $CI->load->model('standard/Answer_model');
                  $answerDetails = $CI->Answer_model->getAnsDetails($id);
                  if($answerDetails === null || $answerDetails[2] != STATUS_TYPE_PUBLIC)
                  {
                      $this->throwError();
                  }
                  ActionCapture::record('answerFileAttachment', 'view', $fileID);
              }
              else if($table === TBL_THREADS && (CUSTOM_CONTROLLER_REQUEST || get_instance()->uri->router->fetch_class() !== 'inlineImage'))
              {
                  $this->throwError();
              }
              else if(!$table && !$userFileName && stringContains($contentType, 'image'))
              {
                  //only allow workflow/guided assistance image requests
                  if(!$validatingCreation || $type !== FA_TYPE_WF_SCRIPT_IMAGE)
                  {
                      $this->throwError();
                  }
                  //client workflow images don't populate fattach tbl with userfname and tbl
                  $userFileName = sql_get_str(sprintf("SELECT label FROM labels WHERE tbl=%d AND label_id=$fileID AND lang_id=%d", TBL_FATTACH, lang_id(LANG_DIR)), 241);
                  //assert content_type is constructed like: image/png
                  $userFileName .= '.' . substr($contentType, strrpos($contentType, '/') + 1);
              }
       
              $localFileName = fattach_full_path($localFileName);
              //IE needs a url-encoded filename to properly display unicode characters; all other browsers do not.
              //Raw encoded so that any whitespace chars in the filename aren't turned into plus chars.
              //Replace any encoded plus chars (they're the only encoded special chars that browsers display as still encoded) back to unencoded value.
              $userFileName = (stringContains($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ? str_replace('%2B', '+', rawurlencode($userFileName)) : $userFileName;
       
              return array($localFileName, $userFileName, $contentType, $fileSize);
          }
       
          /**
           * Retrieves the first file attachment on the specified object that was created at the specified time. There might
           * be more than one possible result, but this function will only return the first one found.
           *
           * @param $objectID The ID of the object the file attachment is attached to (i.e. answer ID or incident ID)
           * @param $objectType The type of object the attachment is on (i.e. TBL_INCIDENTS or TBL_ANSWERS)
           * @param $createdTime timestamp The creation time of the file attachment
           * @return int The attachment ID
           */
          function getAttachmentIDFromCreatedTime($objectID, $objectType, $createdTime)
          {
              $objectID = intval($objectID);
              $objectType = intval($objectType);
              return sql_get_int(sprintf("select file_id from fattach where (id = '$objectID') and (tbl = $objectType) and (created = %s)", time2db(intval($createdTime)))) ?: null;
          }
          
          /**
          * Sends a header pointing to the error page with an error id.
          * @param $errorID int (optional) The error id to pass to the error page;
          *       defaults to 4.
          */
          private function throwError($errorID = 4)
          {
              redirectToErrorPage($errorID, stringContainsCaseInsensitive($_SERVER['HTTP_REFERER'], '/cx/facebook'));
          }
          
          /**
           * Retrieve and cache relevant details of a file attachment
           * @param $id int The ID of the file attachment
           * @param $created timestamp [Optional] The creation time of the file attachment
           * @private
           */
          public function getAttachmentDetails($id, $created=null)
          {    
              if(!is_numeric($id))
                  return false;
              $attachmentDetails = checkCache("attachmentDetails-$id");
              if($attachmentDetails !== null)
                  return $attachmentDetails;
              
              $sql = "select localfname, userfname, content_type, tbl, sz, id, type, created from fattach where file_id = $id";
              if($created && (is_int($created) || ctype_digit($created)))
              {
                  $sql .= sprintf(' and created = %s', time2db($created));
              }
              else
              {
                  $sql .= sprintf(' and (private = 0 or private is null)');
              }
              $si = sql_prepare($sql);
       
              $i = 1;
              sql_bind_col($si, $i++, BIND_NTS, 197);
              sql_bind_col($si, $i++, BIND_NTS, 41);
              sql_bind_col($si, $i++, BIND_NTS, 61);
              sql_bind_col($si, $i++, BIND_INT, 0);
              sql_bind_col($si, $i++, BIND_INT, 0);
              sql_bind_col($si, $i++, BIND_INT, 0);
              sql_bind_col($si, $i++, BIND_INT, 0);
              sql_bind_col($si, $i++, BIND_DTTM, 0);
              $row = sql_fetch($si);
              sql_free($si);
       
              if(!$row)
                  return false;
              
              $details = array('localFileName' => $row[0],
                               'userFileName' => $row[1],
                               'contentType' => $row[2],
                               'table' => $row[3],
                               'size' => $row[4],
                               'id' => $row[5],
                               'type' => $row[6],
                               'created' => $row[7]);
              setCache("attachmentDetails-$id", $details);
              return $details;
          }
      }
       
    • Jeremy Watson

      Hmm, that looks like the Fattach_model.  Can you find the Cs0100_field_model.php file?

      James

    • James Hurster

      Well this is weird.  That is the cs0100.  Here is a screenshot of where I am...

    • Jeremy Watson

      Hmm, but I don't see a sendForm_CS0101 function in that file that you pasted.  Perhaps the wrong file contents got copied and pasted?

      James

    • James Hurster

      Wait.  I am in Production how and here is some new code:

    • James Hurster

      I was in development prior

    • James Hurster

      For the record our site is a mess.  I am becoming more aware of this as I work on it sad

    • Jeremy Watson

      Yeah, that's it.  So around line 220, you can see this line of code:
      fwrite( $fp, "This is the contents of a text file" );

      That's why the attachments always have that content.

      Now, what I'm not sure about is what you actually want there.  :-)  Trying to skim through the code, it seems like you're trying to copy the contents of one file attachment to another file attachment.  If that's what you're trying to do, then this probably does come back to a Connect question, where I'm definitely not the expert.

      Allan, is this something possible to do with ConnectPHP?

      James

    • James Hurster

      I just want to remove that content and allow the widget to work like it should out of the box.  You are amazing!

      Is there something I can do with this code to make this functionality work (meaning more out of the box)?

    • James Hurster

      The FileAttachmentUpload2 widget...

    • Allan Schrum

      Patricia started this process (see above) as a question of Connect PHP file attachment management which is why that line is there. This does not need to be done using Connect PHP and may not be pertinent given that this is really CPv2. I would suggest ripping out the Connect PHP code and use CPv2 semantics for creating a file attachment.

      Regards,

      -Allan

    • James Hurster

      I am going to ask a dumb question then hopefully we can be done with this.  

      Does all the PHP Connect code reside within this file (if so what lines are examples of it) and if not, please give me an idea of where else I need to look. 

      Yes Patricia no longer works here so I am trying to get down to where she left off and how I can get this monster back in its cage.  I can add the fileAttachmentUpload widget very easily but I assume this code is getting in the way.

      Please guide me in how I can remove this and I will go from there.

      You assistance has been so helpful.  Unfortunately we have no documentation from her as to why, how or why things were done.  

      Today's assistance has been great.  Thank you ahead of time again for your assistance.

      - James

    • Jeremy Watson

      It seems like all of the Connect code is in there.

      To start from scratch, I think you might be able to simply replace the custom/input/FormSubmitAsk_CS0101 widget in your citation_0101.php page to input/FormSubmit.  That should use the standard FormSubmit widget, which will create the incident normally and bypass the custom controller function (sendFormAsk_CS0101) and the custom model function (sendForm_CS0101).

      That should help get you started from an initial clean state.

      James

    • James Hurster

      Thanks James & Allan! 

      I appreciate your help today.  I am documenting everything so that this does not happen again.  Nothing like diving into something like this with no road map :-).

      Have a great weekend! 

      - James