Exemple #1
0
 /**
  * Returns the raw PDF data stream.
  *
  * @return string
  *
  * @since 2.0
  */
 public function getPDFData()
 {
     $PDFDownloadName = str_replace(' ', '-', $this->BO->get('title') . '.pdf');
     // first load the file
     $handle = fopen($this->PDFFilename, 'r');
     $data = fread($handle, filesize($this->PDFFilename));
     fclose($handle);
     return $data;
 }
Exemple #2
0
 /**
  * The constructor.
  *
  * @param Alpha\Model\ActiveRecord $BO
  * @param bool                     $useCache
  *
  * @since 1.0
  */
 public function __construct($BO, $useCache = true)
 {
     $config = ConfigProvider::getInstance();
     $this->BO = $BO;
     if ($this->BO instanceof \Alpha\Model\Article && $this->BO->isLoadedFromFile()) {
         $underscoreTimeStamp = str_replace(array('-', ' ', ':'), '_', $this->BO->getContentFileDate());
         $this->filename = $config->get('app.file.store.dir') . 'cache/html/' . get_class($this->BO) . '_' . $this->BO->get('title') . '_' . $underscoreTimeStamp . '.html';
     } else {
         $this->filename = $config->get('app.file.store.dir') . 'cache/html/' . get_class($this->BO) . '_' . $this->BO->getID() . '_' . $this->BO->getVersion() . '.html';
     }
     if (!$useCache) {
         $this->content = $this->markdown($this->BO->get('content', true));
     } else {
         if ($this->checkCache()) {
             $this->loadCache();
         } else {
             if ($this->BO->get('content', true) == '') {
                 // the content may not be loaded from the DB at this stage due to a previous soft-load
                 $this->BO->reload();
             }
             $this->content = $this->markdown($this->BO->get('content', true));
             $this->cache();
         }
     }
     // Replace all instances of $attachURL in link tags to links to the ViewAttachment controller
     $attachments = array();
     preg_match_all('/href\\=\\"\\$attachURL\\/.*\\"/', $this->content, $attachments);
     foreach ($attachments[0] as $attachmentURL) {
         $start = mb_strpos($attachmentURL, '/');
         $end = mb_strrpos($attachmentURL, '"');
         $fileName = mb_substr($attachmentURL, $start + 1, $end - ($start + 1));
         if (method_exists($this->BO, 'getAttachmentSecureURL')) {
             $this->content = str_replace($attachmentURL, 'href="' . $this->BO->getAttachmentSecureURL($fileName) . '" rel="nofollow"', $this->content);
         }
     }
     // Handle image attachments
     $attachments = array();
     preg_match_all('/\\<img\\ src\\=\\"\\$attachURL\\/.*\\.[a-zA-Z]{3}\\"[^<]*/', $this->content, $attachments);
     foreach ($attachments[0] as $attachmentURL) {
         preg_match('/\\/.*\\.[a-zA-Z]{3}/', $attachmentURL, $matches);
         $fileName = $matches[0];
         if ($config->get('cms.images.widget')) {
             // get the details of the source image
             $path = $this->BO->getAttachmentsLocation() . $fileName;
             $image_details = getimagesize($path);
             $imgType = $image_details[2];
             if ($imgType == 1) {
                 $type = 'gif';
             } elseif ($imgType == 2) {
                 $type = 'jpg';
             } elseif ($imgType == 3) {
                 $type = 'png';
             }
             $img = new Image($path, $image_details[0], $image_details[1], $type, 0.95, false, (bool) $config->get('cms.images.widget.secure'));
             $this->content = str_replace($attachmentURL, $img->renderHTMLLink(), $this->content);
         } else {
             // render a normal image link to the ViewAttachment controller
             if (method_exists($this->BO, 'getAttachmentSecureURL')) {
                 $this->content = str_replace($attachmentURL, '<img src="' . $this->BO->getAttachmentSecureURL($fileName) . '">', $this->content);
             }
         }
     }
 }
 /**
  * Dynamically binds all of the attributes for the current BO to the supplied prepared statement
  * parameters.  If arrays of attribute names and values are provided, only those will be bound to
  * the supplied statement.
  *
  * @param mysqli_stmt $stmt The SQL statement to bind to.
  * @param array Optional array of BO attributes.
  * @param array Optional array of BO values.
  *
  * @return mysqli_stmt
  *
  * @since 1.1
  */
 private function bindParams($stmt, $attributes = array(), $values = array())
 {
     self::$logger->debug('>>bindParams(stmt=[' . var_export($stmt, true) . '])');
     $bindingsTypes = '';
     $params = array();
     // here we are only binding the supplied attributes
     if (count($attributes) > 0 && count($attributes) == count($values)) {
         $count = count($values);
         for ($i = 0; $i < $count; ++$i) {
             if (Validator::isInteger($values[$i])) {
                 $bindingsTypes .= 'i';
             } else {
                 $bindingsTypes .= 's';
             }
             array_push($params, $values[$i]);
         }
         if ($this->BO->isTableOverloaded()) {
             if (isset($this->classname)) {
                 $bindingsTypes .= 's';
                 array_push($params, $this->classname);
             } else {
                 $bindingsTypes .= 's';
                 array_push($params, get_class($this->BO));
             }
         }
     } else {
         // bind all attributes on the business object
         // get the class attributes
         $reflection = new ReflectionClass(get_class($this->BO));
         $properties = $reflection->getProperties();
         foreach ($properties as $propObj) {
             $propName = $propObj->name;
             if (!in_array($propName, $this->BO->getTransientAttributes())) {
                 // Skip the OID, database auto number takes care of this.
                 if ($propName != 'OID' && $propName != 'version_num') {
                     if ($this->BO->getPropObject($propName) instanceof Integer) {
                         $bindingsTypes .= 'i';
                     } else {
                         $bindingsTypes .= 's';
                     }
                     array_push($params, $this->BO->get($propName));
                 }
                 if ($propName == 'version_num') {
                     $temp = $this->BO->getVersionNumber()->getValue();
                     $this->BO->set('version_num', $temp + 1);
                     $bindingsTypes .= 'i';
                     array_push($params, $this->BO->getVersionNumber()->getValue());
                 }
             }
         }
         if ($this->BO->isTableOverloaded()) {
             if (isset($this->classname)) {
                 $bindingsTypes .= 's';
                 array_push($params, $this->classname);
             } else {
                 $bindingsTypes .= 's';
                 array_push($params, get_class($this->BO));
             }
         }
         // the OID may be on the WHERE clause for UPDATEs and DELETEs
         if (!$this->BO->isTransient()) {
             $bindingsTypes .= 'i';
             array_push($params, $this->BO->getOID());
         }
     }
     self::$logger->debug('bindingsTypes=[' . $bindingsTypes . '], count: [' . mb_strlen($bindingsTypes) . ']');
     self::$logger->debug('params [' . var_export($params, true) . ']');
     if ($params != null) {
         $bind_names[] = $bindingsTypes;
         $count = count($params);
         for ($i = 0; $i < $count; ++$i) {
             $bind_name = 'bind' . $i;
             ${$bind_name} = $params[$i];
             $bind_names[] =& ${$bind_name};
         }
         call_user_func_array(array($stmt, 'bind_param'), $bind_names);
     }
     self::$logger->debug('<<bindParams [' . var_export($stmt, true) . ']');
     return $stmt;
 }
 /**
  * (non-PHPdoc).
  *
  * @see Alpha\Model\ActiveRecordProviderInterface::saveHistory()
  */
 public function saveHistory()
 {
     self::$logger->debug('>>saveHistory()');
     // get the class attributes
     $reflection = new ReflectionClass(get_class($this->BO));
     $properties = $reflection->getProperties();
     $sqlQuery = '';
     $stmt = null;
     $savedFields = array();
     $attributeNames = array();
     $attributeValues = array();
     $sqlQuery = 'INSERT INTO ' . $this->BO->getTableName() . '_history (';
     foreach ($properties as $propObj) {
         $propName = $propObj->name;
         if (!in_array($propName, $this->BO->getTransientAttributes())) {
             $sqlQuery .= "{$propName},";
             $attributeNames[] = $propName;
             $attributeValues[] = $this->BO->get($propName);
             $savedFields[] = $propName;
         }
     }
     if ($this->BO->isTableOverloaded()) {
         $sqlQuery .= 'classname,';
     }
     $sqlQuery = rtrim($sqlQuery, ',');
     $sqlQuery .= ') VALUES (';
     foreach ($savedFields as $savedField) {
         $sqlQuery .= ':' . $savedField . ',';
     }
     if ($this->BO->isTableOverloaded()) {
         $sqlQuery .= ':classname,';
     }
     $sqlQuery = rtrim($sqlQuery, ',') . ')';
     $this->BO->setLastQuery($sqlQuery);
     self::$logger->debug('Query [' . $sqlQuery . ']');
     $stmt = self::getConnection()->prepare($sqlQuery);
     if ($stmt instanceof SQLite3Stmt) {
         foreach ($savedFields as $savedField) {
             if ($this->BO->get($savedField) instanceof Integer) {
                 $stmt->bindValue(':' . $savedField, $this->BO->get($savedField), SQLITE3_INTEGER);
             } else {
                 $stmt->bindValue(':' . $savedField, $this->BO->get($savedField), SQLITE3_TEXT);
             }
         }
         if ($this->BO->isTableOverloaded()) {
             $stmt->bindValue(':classname', get_class($this->BO), SQLITE3_TEXT);
         }
         $result = $stmt->execute();
     } else {
         throw new FailedSaveException('Failed to save object history, error is [' . self::getLastDatabaseError() . '], query [' . $this->BO->getLastQuery() . ']');
     }
 }