public static function getSourceModel($source_id) { if (!isset(SourceModel::$_source_cache[$source_id])) { $sources = new Sources(); $source = $sources->getSource($source_id); $model = SourceModel::newInstance($source['service'], $source); SourceModel::$_source_cache[$source_id] = $model; } return SourceModel::$_source_cache[$source_id]; }
public static function forUser($id) { $properties = new Properties(array(Properties::KEY => $id)); $sources = new Sources(array(Stuffpress_Db_Table::USER => $id)); $source_id = $properties->getProperty('stuffpress_source'); if (!$source_id) { $source_id = $sources->addSource('stuffpress'); $sources->setImported($source_id, 1); $properties->setProperty('stuffpress_source', $source_id); } $source = $sources->getSource($source_id); return new StuffpressModel($source); }
public function setcoverAction() { // Get, check and setup the parameters $story_id = $this->getRequest()->getParam("story"); $source_id = $this->getRequest()->getParam("source"); $item_id = $this->getRequest()->getParam("item"); //Verify if the requested story exist $stories = new Stories(); if (!($story = $stories->getStory($story_id))) { return $this->_helper->json->sendJson(false); } // Check if we are the owner if ($this->_application->user->id != $story->user_id) { return $this->_helper->json->sendJson(false); } //Verify if the requested source exist $sources = new Sources(); if (!($source = $sources->getSource($source_id))) { return $this->_helper->json->sendJson(false); } // Check if we are the owner if ($this->_application->user->id != $source['user_id']) { return $this->_helper->json->sendJson(false); } //Verify if the requested item exist $data = new Data(); if (!($item = $data->getItem($source_id, $item_id))) { return $this->_helper->json->sendJson(false); } // Do we have a url ? if (!($url = $item->getImageUrl(ImageItem::SIZE_MEDIUM))) { return $this->_helper->json->sendJson(false); } // Get the old image and delete it $files = new Files(); if ($file = $files->getFileFromKey($story->thumbnail)) { $files->deleteFile($file->key); } // Ok, we can set the image $this->setKeyImage($story_id, $url); return $this->_helper->json->sendJson(true); }
public function settimestampAction() { // Get, check and setup the parameters $source_id = $this->getRequest()->getParam("source"); $item_id = $this->getRequest()->getParam("item"); $timestamp = $this->getRequest()->getParam("timestamp"); //Verify if the requested source exist $sources = new Sources(); if (!($source = $sources->getSource($source_id))) { throw new Stuffpress_Exception("Error updating timestamp for source {$source} - Invalid source"); } // Get the user $users = new Users(); $user = $users->getUser($source['user_id']); // Check if we are the owner if ($this->_application->user->id != $user->id) { throw new Stuffpress_Exception("Error updating timestamp for source {$source} - Not the owner"); } // Get the user properties $properties = new Properties($user->id); // Check if the date is valid if (!($date = Stuffpress_Date::strToTimezone($timestamp, $properties->getProperty('timezone')))) { $this->addErrorMessage("Could not understand the provided date/time"); return $this->_forward('index'); } // Ok, we can change the title of the item $model = SourceModel::newInstance($source['service'], $source); $model->setTimestamp($item_id, $date); $this->addStatusMessage("Life is cool"); // We redirect to the stream $this->_forward('index'); }
public function deleteAction() { // Get the source from the request and initialize the model $id = $this->_getParam('id'); // Get the sources database $sources = new Sources(); // Check if the source exists if (!($source = $sources->getSource($id))) { return $this->_helper->json->sendJson(true); } // Check if we own the source if ($source['user_id'] != $this->_application->user->id) { return $this->_helper->json->sendJson(true); } // Instantiate a model and remove all the data $model = SourceModel::newInstance($source['service']); $model->setSource($source); $model->deleteItems(); // Delete the duplicated from the Data table $data = new Data(); $data->deleteItems($source['id']); // Delete the source settings $properties = new SourcesProperties(array(Properties::KEY => $source['id'])); $properties->deleteAllProperties(); // Delete the tags $tags = new Tags(); $tags->deleteSource($source['id']); // Remove the source $sources->deleteSource($id); // We should also delete the associated comments $comments = new Comments(); $comments->deleteComments($source['id']); // Forward to the list view with a success message return $this->_helper->json->sendJson(false); }
public function addAction() { if (!$this->getRequest()->isPost()) { return $this->_helper->json->sendJson(true); } $form = $this->getForm(); if (!$form->isValid($_POST)) { return $this->_helper->json->sendJson($form->getErrorArray()); } // Get the values and proceed $values = $form->getValues(); $source_id = $values['source']; $item_id = $values['item']; $name = $values['name']; $email = $values['email']; $website = $values['website']; $comment = $values['comment']; $options = $values['options']; $timestamp = time(); $notify = @in_array('notify', $options) ? 1 : 0; // Get the source and the user owning it $data = new Data(); $sources = new Sources(); $users = new Users(); // Does the source exist ? if (!($source = $sources->getSource($source_id))) { return $this->_helper->json->sendJson(true); } // Does the item exists? if (!($item = $data->getItem($source_id, $item_id))) { return $this->_helper->json->sendJson(true); } // Does the user exist ? if (!($user = $users->getUser($source['user_id']))) { return $this->_helper->json->sendJson(true); } // Validate the website URL $matches = array(); if (!preg_match_all("/^http/", $website, $matches)) { $website = "http://{$website}"; } // Add the comment to the database $comments = new Comments(); $comments->addComment($source_id, $item_id, $comment, $name, $email, $website, $timestamp, $notify); // Send an email alert to owner $on_comment = $this->_properties->getProperty('on_comment'); $owner = $this->_application->user && $this->_application->user->email == $email ? true : false; if ($on_comment && !$owner) { $slug = $item->getSlug(); Stuffpress_Emails::sendCommentEmail($user->email, $user->username, $name, $email, $comment, $slug); } // Send email alerts to everyone else (skip owner and current submiter) $subscribers = $comments->getSubscriptions($source_id, $item_id); foreach ($subscribers as $subscriber) { if ($subscriber['email'] == $user->email || $subscriber['email'] == $email) { continue; } Stuffpress_Emails::sendCommentNotifyEmail($subscriber['email'], $subscriber['name'], $name, $comment, $source_id, $item_id); } // Ok send the result return $this->_helper->json->sendJson(false); }
public function deleteItems($source_id) { $sources = new Sources(); if (!($source = $sources->getSource($source_id))) { return; } $where = $this->getAdapter()->quoteInto('source_id = ?', $source_id); $this->delete($where); }
public function excelAction() { // Get the request parameters $id = $this->_getParam('id'); // Get the requested source $sources = new Sources(); if (!($source = $sources->getSource($id))) { throw new Stuffpress_Exception("Unknown source id {$id}"); } // Are we the owner of the source if ($source['user_id'] != $this->_application->user->id) { throw new Stuffpress_Exception("You are not authorized to download this source"); } // Get the source meta data $model = SourceModel::newInstance($source['service']); $model->setSource($source); $service = $model->getServiceName(); $desc = $model->getServiceDescription(); // Get the actual data to backup $data = new Data(); $items = $data->getAllItems($source['id']); // This is not a layout or rendered page $this->_helper->viewRenderer->setNoRender(); $this->_helper->layout->disableLayout(); // Start the writer to Excel require_once 'PHPExcel.php'; require_once 'PHPExcel/Writer/Excel2007.php'; $objPHPExcel = new PHPExcel(); // Set properties $objPHPExcel->getProperties()->setCreator("storytlr.com"); $objPHPExcel->getProperties()->setLastModifiedBy("storytlr.com"); $objPHPExcel->getProperties()->setTitle("Backup of {$this->_application->user->username}'s {$service} account"); //$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document"); $objPHPExcel->getProperties()->setDescription($desc); // Add some data $objPHPExcel->setActiveSheetIndex(0); // Write the column titles $keys = array_keys($items[0]->getBackup()); for ($i = 0; $i < count($keys); $i++) { $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $keys[$i]); } // Write the data for ($i = 0; $i < count($items); $i++) { $item = $items[$i]->getBackup(); for ($j = 0; $j < count($keys); $j++) { $value = $item[$keys[$j]]; if ($value) { $objPHPExcel->getActiveSheet()->getCellByColumnAndRow($j, $i + 2)->setValueExplicit("'{$value}", PHPExcel_Cell_DataType::TYPE_STRING); } } } // Rename sheet $objPHPExcel->getActiveSheet()->setTitle($service); // Save Excel 2007 file $root = Zend_Registry::get("root"); $key = Stuffpress_Token::create(6); $file = "{$service}-{$this->_application->user->username}-[{$key}].xls"; $path = $root . "/public/files/{$file}"; $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); $objWriter->save($path); $this->_redirect($this->_base . "/files/{$file}"); }
private function process($source, $target) { $this->_logger->log("Processing pingback from {$source} for {$target}", Zend_Log::INFO); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $source); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_USERAGENT, 'Storytlr/1.0'); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); $response = curl_exec($curl); $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($http_code != 200) { $this->_logger->log("Failed to get content for {$source}", Zend_Log::DEBUG); return; } // Parse the source page for microformats content $parser = new Parser($response); $output = $parser->parse(); $this->_logger->log("Parsed output: " . var_export($output, true), Zend_Log::DEBUG); // Extract relevant data $hcards = array(); $hentries = array(); $this->processItems($output["items"], $hcards, $hentries); $this->_logger->log("Extracted hcards: " . var_export($hcards, true), Zend_Log::DEBUG); $this->_logger->log("Extracted hentries: " . var_export($hentries, true), Zend_Log::DEBUG); // Lookup if existing entry preg_match('/(?P<source>\\d+)\\-(?P<item>\\d+)\\.html$/', $target, $matches); $this->_logger->log("Matches: " . var_export($matches, true), Zend_Log::DEBUG); $source_id = $matches["source"]; $item_id = $matches["item"]; // Get the source and the user owning it $data = new Data(); $sources = new Sources(); $users = new Users(); // Does it relate to an item ? if ($source_id && $item_id) { $s = $sources->getSource($source_id); $i = $data->getItem($source_id, $item_id); if ($s && $i) { $user = $users->getUser($s['user_id']); } } // Otherwise, can we relate to a user ? if (!$user) { $user = $this->lookupUser($target); } // No user ? We have to giveup if (!$user) { throw new Exception('Failed to find corresponding storytlr user.'); } // Is this a h-entry ? if (count($hentries) > 0) { $hentry = $hentries[0]; if (count($hentry["author"]) > 0) { $hcard = $hentry["author"][0]; } } // If no hcard yet (maybe there was no h-entry, we grab the top-level one if (!$hcard && count($hcards) > 0) { $hcard = $hcards[0]; } // Find the published date if ($hentry && $hentry["published"]) { $timestamp = strtotime($hentry["published"]); } // If no timestamp, we fallback to now if (!$timestamp) { $timestamp = time(); } // Add the mention to the database try { $mentions = new Mentions(); $mentions->addMention($source_id, $item_id, $user->id, $source, $hentry["title"], $hcard["name"], $hcard["url"], "", $hcard["avatar"], $timestamp); } catch (Exception $e) { $this->_logger->log("Exception when storing mention: " . $e->getMessage(), Zend_Log::ERR); return; } // Send an email alert to owner try { $on_comment = $this->_properties->getProperty('on_comment'); if ($on_comment) { Stuffpress_Emails::sendMentionEmail($user->email, $user->username, $hcard["name"], $hcard["url"], $hentry["title"], $source, $target); } } catch (Exception $e) { $this->_logger->log("Sending mention notification exception: " . $e->getMessage(), Zend_Log::ERR); } }