Backend\Core\Engine\Model::ping PHP Method

ping() public static method

Ping the known webservices
Deprecation:
public static ping ( string $pageOrFeedURL = null, string $category = null ) : boolean
$pageOrFeedURL string The page/feed that has changed.
$category string An optional category for the site.
return boolean If everything went fne true will, otherwise false.
    public static function ping($pageOrFeedURL = null, $category = null)
    {
        $siteTitle = self::get('fork.settings')->get('Core', 'site_title_' . BackendLanguage::getWorkingLanguage(), SITE_DEFAULT_TITLE);
        $siteURL = SITE_URL;
        $pageOrFeedURL = $pageOrFeedURL !== null ? (string) $pageOrFeedURL : null;
        $category = $category !== null ? (string) $category : null;
        // get ping services
        $pingServices = self::get('fork.settings')->get('Core', 'ping_services', null);
        // no ping services available or older than one month ago
        if ($pingServices === null || $pingServices['date'] < strtotime('-1 month')) {
            // get ForkAPI-keys
            $publicKey = self::get('fork.settings')->get('Core', 'fork_api_public_key', '');
            $privateKey = self::get('fork.settings')->get('Core', 'fork_api_private_key', '');
            // validate keys
            if ($publicKey == '' || $privateKey == '') {
                return false;
            }
            // require the class
            require_once PATH_LIBRARY . '/external/fork_api.php';
            // create instance
            $forkAPI = new \ForkAPI($publicKey, $privateKey);
            // try to get the services
            try {
                $pingServices['services'] = $forkAPI->pingGetServices();
                $pingServices['date'] = time();
            } catch (Exception $e) {
                // check if the error should not be ignored
                if (mb_strpos($e->getMessage(), 'Operation timed out') === false && mb_strpos($e->getMessage(), 'Invalid headers') === false) {
                    if (BackendModel::getContainer()->getParameter('kernel.debug')) {
                        throw $e;
                    } else {
                        // stop, hammertime
                        return false;
                    }
                }
            }
            // store the services
            self::get('fork.settings')->set('Core', 'ping_services', $pingServices);
        }
        // make sure services array will not trigger an error (even if we couldn't load any)
        if (!isset($pingServices['services']) || !$pingServices['services']) {
            $pingServices['services'] = array();
        }
        // loop services
        foreach ($pingServices['services'] as $service) {
            $client = new \SpoonXMLRPCClient($service['url']);
            $client->setUserAgent('Fork ' . FORK_VERSION);
            $client->setTimeout(10);
            $client->setPort($service['port']);
            try {
                // extended ping?
                if ($service['type'] == 'extended') {
                    // no page or feed URL present?
                    if ($pageOrFeedURL === null) {
                        continue;
                    }
                    $parameters[] = array('type' => 'string', 'value' => $siteTitle);
                    $parameters[] = array('type' => 'string', 'value' => $siteURL);
                    $parameters[] = array('type' => 'string', 'value' => $pageOrFeedURL);
                    if ($category !== null) {
                        $parameters[] = array('type' => 'string', 'value' => $category);
                    }
                    $client->execute('weblogUpdates.extendedPing', $parameters);
                } else {
                    // default ping
                    $parameters[] = array('type' => 'string', 'value' => $siteTitle);
                    $parameters[] = array('type' => 'string', 'value' => $siteURL);
                    $client->execute('weblogUpdates.ping', $parameters);
                }
            } catch (Exception $e) {
                // check if the error should not be ignored
                if (mb_strpos($e->getMessage(), 'Operation timed out') === false && mb_strpos($e->getMessage(), 'Invalid headers') === false) {
                    if (BackendModel::getContainer()->getParameter('kernel.debug')) {
                        throw $e;
                    }
                }
                continue;
            }
        }
        return true;
    }

Usage Example

Beispiel #1
0
 /**
  * Validate the form
  */
 private function validateForm()
 {
     // is the form submitted?
     if ($this->frm->isSubmitted()) {
         // get the status
         $status = \SpoonFilter::getPostValue('status', array('active', 'draft'), 'active');
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // validate fields
         $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired'));
         $this->frm->getField('text')->isFilled(BL::err('FieldIsRequired'));
         $this->frm->getField('publish_on_date')->isValid(BL::err('DateIsInvalid'));
         $this->frm->getField('publish_on_time')->isValid(BL::err('TimeIsInvalid'));
         $this->frm->getField('category_id')->isFilled(BL::err('FieldIsRequired'));
         // validate meta
         $this->meta->validate();
         // no errors?
         if ($this->frm->isCorrect()) {
             // build item
             $item['id'] = $this->id;
             $item['meta_id'] = $this->meta->save();
             // this is used to let our model know the status (active, archive, draft) of the edited item
             $item['revision_id'] = $this->record['revision_id'];
             $item['category_id'] = (int) $this->frm->getField('category_id')->getValue();
             $item['user_id'] = $this->frm->getField('user_id')->getValue();
             $item['language'] = BL::getWorkingLanguage();
             $item['title'] = $this->frm->getField('title')->getValue();
             $item['introduction'] = $this->frm->getField('introduction')->getValue();
             $item['text'] = $this->frm->getField('text')->getValue();
             $item['publish_on'] = BackendModel::getUTCDate(null, BackendModel::getUTCTimestamp($this->frm->getField('publish_on_date'), $this->frm->getField('publish_on_time')));
             $item['edited_on'] = BackendModel::getUTCDate();
             $item['hidden'] = $this->frm->getField('hidden')->getValue();
             $item['allow_comments'] = $this->frm->getField('allow_comments')->getChecked() ? 'Y' : 'N';
             $item['status'] = $status;
             if ($this->imageIsAllowed) {
                 $item['image'] = $this->record['image'];
                 // the image path
                 $imagePath = FRONTEND_FILES_PATH . '/blog/images';
                 // create folders if needed
                 $fs = new Filesystem();
                 $fs->mkdir(array($imagePath . '/source', $imagePath . '/128x128'));
                 // If the image should be deleted, only the database entry is refreshed.
                 // The revision should keep it's file.
                 if ($this->frm->getField('delete_image')->isChecked()) {
                     // reset the name
                     $item['image'] = null;
                 }
                 // new image given?
                 if ($this->frm->getField('image')->isFilled()) {
                     // build the image name
                     // we use the previous revision-id in the filename to make the filename unique between
                     // the different revisions, to prevent that a new file would
                     // overwrite images of previous revisions that have the same title, and thus, the same filename
                     $item['image'] = $this->meta->getURL() . '-' . BL::getWorkingLanguage() . '-' . $item['revision_id'] . '.' . $this->frm->getField('image')->getExtension();
                     // upload the image & generate thumbnails
                     $this->frm->getField('image')->generateThumbnails($imagePath, $item['image']);
                 } elseif ($item['image'] != null) {
                     // generate the new filename
                     $image = new File($imagePath . '/source/' . $item['image']);
                     $newName = $this->meta->getURL() . '-' . BL::getWorkingLanguage() . '-' . $item['revision_id'] . '.' . $image->getExtension();
                     // extract the filenames excluding …-[language]-[revision-id].jpg
                     // to properly compare them to eachother
                     $regex = '/(.*)-[a-z]{2}-[0-9]+\\.(.*)/';
                     // only copy if the new name differs from the old filename
                     if (preg_replace($regex, '$1', $newName) != preg_replace($regex, '$1', $item['image'])) {
                         // loop folders
                         foreach (BackendModel::getThumbnailFolders($imagePath, true) as $folder) {
                             $fs->copy($folder['path'] . '/' . $item['image'], $folder['path'] . '/' . $newName);
                         }
                         // assign the new name to the database
                         $item['image'] = $newName;
                     }
                 }
             } else {
                 $item['image'] = null;
             }
             // update the item
             $item['revision_id'] = BackendBlogModel::update($item);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $item));
             // recalculate comment count so the new revision has the correct count
             BackendBlogModel::reCalculateCommentCount(array($this->id));
             // save the tags
             BackendTagsModel::saveTags($item['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule());
             // active
             if ($item['status'] == 'active') {
                 // edit search index
                 BackendSearchModel::saveIndex($this->getModule(), $item['id'], array('title' => $item['title'], 'text' => $item['text']));
                 // ping
                 if ($this->get('fork.settings')->get($this->URL->getModule(), 'ping_services', false)) {
                     BackendModel::ping(SITE_URL . BackendModel::getURLForBlock($this->URL->getModule(), 'detail') . '/' . $this->meta->getURL());
                 }
                 // build URL
                 $redirectUrl = BackendModel::createURLForAction('Index') . '&report=edited&var=' . urlencode($item['title']) . '&id=' . $this->id . '&highlight=row-' . $item['revision_id'];
             } elseif ($item['status'] == 'draft') {
                 // draft: everything is saved, so redirect to the edit action
                 $redirectUrl = BackendModel::createURLForAction('Edit') . '&report=saved-as-draft&var=' . urlencode($item['title']) . '&id=' . $item['id'] . '&draft=' . $item['revision_id'] . '&highlight=row-' . $item['revision_id'];
             }
             // append to redirect URL
             if ($this->categoryId != null) {
                 $redirectUrl .= '&category=' . $this->categoryId;
             }
             // everything is saved, so redirect to the overview
             $this->redirect($redirectUrl);
         }
     }
 }
All Usage Examples Of Backend\Core\Engine\Model::ping