Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.
 All Data Structures Functions Variables Pages
ShortUrl.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
37  class ShortUrl extends RedBeanModel
38  {
39  const ALLOWED_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
40 
41  const HASH_LENGTH = 10;
42 
43  const LIFE_TIME_IN_DAYS = 100;
44 
45  public function onCreated()
46  {
47  $this->unrestrictedSet('createdDateTime', DateTimeUtil::convertTimestampToDbFormatDateTime(time()));
48  }
49 
50  public static function getDefaultMetadata()
51  {
52  $metadata = parent::getDefaultMetadata();
53  $metadata[__CLASS__] = array(
54  'members' => array(
55  'hash',
56  'url',
57  'createdDateTime',
58  ),
59  'rules' => array(
60  array('hash', 'required'),
61  array('hash', 'type', 'type' => 'string'),
62  array('hash', 'length', 'min' => static::HASH_LENGTH, 'max' => static::HASH_LENGTH),
63  array('url', 'required'),
64  array('url', 'type', 'type' => 'string'),
65  array('createdDateTime', 'required'),
66  array('createdDateTime', 'readOnly'),
67  array('createdDateTime', 'type', 'type' => 'datetime'),
68  ),
69  'elements' => array(
70  'createdDateTime' => 'DateTime',
71  ),
72  );
73  return $metadata;
74  }
75 
82  public static function resolveHashByUrl($url)
83  {
84  assert('is_string($url)');
85  try
86  {
87  $hash = static::getHashByUrl($url);
88  }
89  catch (NotFoundException $exception)
90  {
91  $hashIsNew = false;
92  while (!$hashIsNew)
93  {
94  try
95  {
96  $hash = static::getRandomHash();
97  static::getUrlByHash($hash);
98  }
99  catch (NotFoundException $NotFoundUrlException)
100  {
101  $hashIsNew = true;
102  }
103  }
104  $className = __CLASS__;
105  $shortUrl = new $className();
106  $shortUrl->url = trim($url);
107  $shortUrl->hash = $hash;
108  $shortUrl->save();
109  }
110  return $hash;
111  }
112 
119  public static function getHashByUrl($url)
120  {
121  assert('is_string($url)');
122  assert('$url != ""');
123  $url = trim($url);
124  $className = __CLASS__;
125  $bean = ZurmoRedBean::findOne($className::getTableName(), "url = :url", array(':url' => $url));
126  assert('$bean === false || $bean instanceof RedBean_OODBBean');
127  if ($bean === false)
128  {
129  throw new NotFoundException();
130  }
131  else
132  {
133  $shortUrl = self::makeModel($bean);
134  }
135  return $shortUrl->hash;
136  }
137 
144  public static function getUrlByHash($hash)
145  {
146  assert('is_string($hash)');
147  assert('$hash != ""');
148  $className = __CLASS__;
149  $bean = ZurmoRedBean::findOne($className::getTableName(), "hash = :hash ", array(':hash' => $hash));
150  assert('$bean === false || $bean instanceof RedBean_OODBBean');
151  if ($bean === false)
152  {
153  throw new NotFoundException();
154  }
155  else
156  {
157  $shortUrl = self::makeModel($bean);
158  }
159  return $shortUrl->url;
160  }
161 
166  protected static function getRandomHash()
167  {
168  $hash = null;
169  $length = strlen(static::ALLOWED_CHARS);
170  $allowedChars = static::ALLOWED_CHARS;
171  for ($i = 0; $i < static::HASH_LENGTH; $i++)
172  {
173  $hash .= $allowedChars[mt_rand(0, $length - 1)];
174  }
175  return $hash;
176  }
177 
181  public static function deleteOld()
182  {
183  $timestamp = time() - static::LIFE_TIME_IN_DAYS * 24 * 60 * 60;
184  $dateTime = DateTimeUtil::convertTimestampToDbFormatDateTime($timestamp);
185  $searchAttributeData = array();
186  $searchAttributeData['clauses'] = array(
187  1 => array(
188  'attributeName' => 'createdDateTime',
189  'operatorType' => 'lessThan',
190  'value' => $dateTime,
191  ),
192  );
193  $searchAttributeData['structure'] = '1';
194  $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter(get_called_class());
195  $where = RedBeanModelDataProvider::makeWhere(get_called_class(), $searchAttributeData, $joinTablesAdapter);
196  $models = self::getSubset($joinTablesAdapter, null, null, $where, null);
197  foreach ($models as $model)
198  {
199  $model->delete();
200  }
201  }
202  }
203 ?>
static makeWhere($modelClassName, array $metadata, &$joinTablesAdapter)
static getRandomHash()
Definition: ShortUrl.php:166
unrestrictedSet($attributeName, $value)
static getHashByUrl($url)
Definition: ShortUrl.php:119
static resolveHashByUrl($url)
Definition: ShortUrl.php:82
static deleteOld()
Definition: ShortUrl.php:181
static getUrlByHash($hash)
Definition: ShortUrl.php:144
Generated on Wed Oct 21 2020 07:10:35
Account Suspended
Account Suspended
This Account has been suspended.
Contact your hosting provider for more information.