public function compress($file, $lossy = false) { if (!file_exists($file)) { throw new \Exception('File not found: ' . $file); } if (!is_file($file)) { throw new \Exception('Invalid file: ' . $file); } if (filesize($file) > self::MAX_FILESIZE) { throw new \Exception($file . ' exceeds maximum file size of ' . self::MAX_FILESIZE . ' bytes'); } $result = $this->upload($file); if (!$result->status) { return false; } if (!isset($result->optimized_url)) { throw new \Exception('PunyPNG API did not return a download URL'); } if ($lossy && !isset($result->indexed_url)) { throw new \Exception('PunyPNG API did not return a lossy download URL'); } // Log successful result $ics = new \ImageCompressionSaurus(); $ics->original_size = $result->original_size; if ($lossy) { $ics->optimized_size = $result->original_size - $result->indexed_savings_bytes; } else { $ics->optimized_size = $result->optimized_size; } $ics->ctime = \SparkLib\DB::Now(); $ics->insert(); return $result; }
public function updateSthsGoalie($id, $field, $val) { try { $dbh = \SparkLib\DB::pdo(); $sth = $dbh->prepare("UPDATE sths_goalie SET {$field} = :val WHERE number = :id"); $sth->execute([':id' => $id, ':val' => $val]); } catch (PDOException $e) { echo $e->getMessage(); } }
public function updateTransaction($id, $timestamp, $newVal) { try { $dbh = \SparkLib\DB::pdo(); $sth = $dbh->prepare("UPDATE transactions SET value = :newVal WHERE datetime = :time AND id = :id"); $sth->execute([':newVal' => $newVal, ':time' => $timestamp, ':id' => $id]); } catch (PDOException $e) { echo $e->getMessage(); } }
/** * Create a new comment. * * @access public * @return void */ public function post() { $status = true; $message = ''; $mongo = MongoDBI::getInstance(); $comment = []; try { $this->req()->expect('entity', 'entity_id', 'text'); } catch (Exception $e) { $status = false; $message = 'You must provide a comment'; } if (!$_SESSION['user']->isAuthenticated()) { $status = false; $message = 'You must be logged in to comment'; } if (!$_SESSION['user']->customer()->can_comment) { $status = false; $message = 'Permission denied.'; } if ($status) { $parent_id = isset($this->req()->parent_id) ? $this->req()->parent_id : 0; $comment = new CustomerCommentSaurus(["entity_table" => $this->req()->entity, "entity_id" => $this->req()->entity_id, "parent_id" => $parent_id, "customer_id" => (int) $_SESSION['user']->customer()->id(), "customer_role" => $_SESSION['user']->customer()->customers_role, "ctime" => new MongoDate(), "mtime" => new MongoDate(), "ratings" => [(int) $_SESSION['user']->customer()->id()], "ratings_count" => 1, "reports" => 0, "visible" => true, "text" => $this->req()->text]); // by default, hide replies to hidden comments, so they don't // wind up in the feed (if you want to quietly send a message // to the nerds watching the backchannel in the feed, you can // unhide your comment and it'll show up there) if ($parent_id != 0) { $parent_comment = $comment->parent(); $comment->visible = $parent_comment->visible; } $bl_results = DB::fetchAll("select * from comment_blacklists where :term ~* regex_needle", ['term' => $this->req()->text]); $blacklisted = count($bl_results) > 0; if (!$blacklisted) { // TODO: should this be $comment->insert() instead ? if (!$mongo->comments->insert($comment->getRecord())) { $status = false; $message = 'Error saving comment, please try again later.'; } else { LogSaurus::log('COMMENT_POST', $_SESSION['user']->customer()->id(), 'COMMERCE', $comment->id()); } // assuming we saved that correctly, let's notify whoever wants to know about // this comment being posted if ($status) { $comment->queueNotification(); } } else { LogSaurus::log('COMMENT_POST_BLACKLISTED', $_SESSION['user']->customer()->id(), 'COMMERCE', $this->req()->text); } } $this->respondTo()->json = function () use($mongo, $status, $message, $comment) { if (!$status) { return ['status' => $status, 'message' => $message]; } $updated_html = $this->app()->partial('comments/view'); $updated_html->entity_table = $this->req()->entity; $updated_html->entity_id = $this->req()->entity_id; if ($updated_html->entity_table == 'products') { $updated_html->do_heading = false; } $updated_html->comments = $mongo->comments->find(['entity_table' => $this->req()->entity, 'entity_id' => $this->req()->entity_id, 'visible' => true]); $updated_html->comments->sort(["ratings_count" => -1, "ctime" => -1]); return ['status' => $status, 'message' => $message, 'comment_id' => (string) $comment->id()]; }; $this->respondTo()->html = function () use($status, $message, $comment) { if ($status) { return new Redirect($comment->url()); } $this->template()->message = $message; $this->layout()->title = 'Error posting comment'; return $this->layout(); }; }
/** * Delete a record from the db. You probably shouldn't be doing this. * * Calls preDelete() and postDelete(), if they are defined in the child. * * TODO: Should this call markUnchanged()? */ public function delete() { $this->invalidateCache(); if (!$this->canDelete()) { throw new SparkRecordException("can't delete this record - sure you inserted or loaded it from the db?"); } $this->preDelete(); $dbh = DB::getInstance(); $tk = static::$_tableKey; $tn = static::$_tableName; try { $sth = $dbh->prepare("DELETE FROM \"{$tn}\" WHERE \"{$tk}\" = :id"); $sth->execute(['id' => $this->_tableId]); } catch (Exception $e) { throw new SparkRecordException('Delete failed: ' . $e->getMessage()); } if ($this->_logModifications || count($this->modificationInfo()) > 0) { $this->logModifications('DELETE', $this->_record); } $this->logDelete(); $this->postDelete(); return true; }
protected function getDBH() { return DB::getInstance(); }