Merge CustIS version:
Code formatting Duplicate filtering Exclusive ./update-quiet.sh Conflicts: add-single.php fof-db.php fof-main.php login.phpmaster
parent
b21fa04357
commit
9a0bb4b0c9
|
@ -17,5 +17,12 @@ include_once("fof-main.php");
|
||||||
$url = $_REQUEST['url'];
|
$url = $_REQUEST['url'];
|
||||||
$unread = $_REQUEST['unread'];
|
$unread = $_REQUEST['unread'];
|
||||||
|
|
||||||
print(fof_subscribe(fof_current_user(), $url, $unread));
|
$error = fof_subscribe(fof_current_user(), $url, $unread);
|
||||||
|
if (preg_match('/HTTP 401/', $error))
|
||||||
|
print "<script>
|
||||||
|
document.addform.basic_login.style.backgroundColor='#FFC0C0';
|
||||||
|
document.addform.basic_password.style.backgroundColor='#FFC0C0';
|
||||||
|
document.addform.basic_password.focus();
|
||||||
|
</script>";
|
||||||
|
print $error;
|
||||||
?>
|
?>
|
||||||
|
|
36
fof-db.php
36
fof-db.php
|
@ -164,6 +164,15 @@ function fof_db_get_feeds()
|
||||||
return(fof_db_query("select * from $FOF_FEED_TABLE order by feed_title"));
|
return(fof_db_query("select * from $FOF_FEED_TABLE order by feed_title"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fof_db_get_item_by_id($item_id)
|
||||||
|
{
|
||||||
|
global $FOF_ITEM_TABLE;
|
||||||
|
if (($result = fof_db_query("select * from $FOF_ITEM_TABLE where item_id=".intval($item_id))) &&
|
||||||
|
($row = fof_db_get_row($result)))
|
||||||
|
return $row;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
function fof_db_get_item_count($user_id)
|
function fof_db_get_item_count($user_id)
|
||||||
{
|
{
|
||||||
global $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_SUBSCRIPTION_TABLE, $FOF_ITEM_TAG_TABLE;
|
global $FOF_FEED_TABLE, $FOF_ITEM_TABLE, $FOF_SUBSCRIPTION_TABLE, $FOF_ITEM_TAG_TABLE;
|
||||||
|
@ -797,10 +806,8 @@ function fof_db_authenticate($user_name, $user_password_hash)
|
||||||
|
|
||||||
$result = fof_safe_query("select * from $FOF_USER_TABLE where user_name = '%s' and user_password_hash = '%s'", $user_name, $user_password_hash);
|
$result = fof_safe_query("select * from $FOF_USER_TABLE where user_name = '%s' and user_password_hash = '%s'", $user_name, $user_password_hash);
|
||||||
|
|
||||||
if(mysql_num_rows($result) == 0)
|
if (mysql_num_rows($result) == 0)
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
$row = mysql_fetch_array($result);
|
$row = mysql_fetch_array($result);
|
||||||
|
|
||||||
|
@ -811,4 +818,27 @@ function fof_db_authenticate($user_name, $user_password_hash)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fof_db_get_is_duplicate_item($item_id, $item_guid, $content_md5)
|
||||||
|
{
|
||||||
|
global $FOF_ITEM_TABLE, $FOF_USER_TABLE, $FOF_FEED_TABLE, $FOF_SUBSCRIPTION_TABLE;
|
||||||
|
$result = fof_safe_query("select * from $FOF_ITEM_TABLE where item_guid='%s' AND item_id!='%d'", $item_guid, $item_id);
|
||||||
|
$dups = array();
|
||||||
|
while ($row = fof_db_get_row($result))
|
||||||
|
if (md5($row['item_content']) == $content_md5)
|
||||||
|
$dups[] = intval($row['item_id']);
|
||||||
|
if (!count($dups))
|
||||||
|
return array();
|
||||||
|
$result = fof_db_query(
|
||||||
|
"SELECT DISTINCT $FOF_SUBSCRIPTION_TABLE.user_id FROM $FOF_ITEM_TABLE, $FOF_SUBSCRIPTION_TABLE".
|
||||||
|
" WHERE item_id IN (".join(",",$dups).") AND $FOF_SUBSCRIPTION_TABLE.feed_id=$FOF_ITEM_TABLE.feed_id"
|
||||||
|
);
|
||||||
|
$users = array();
|
||||||
|
while ($row = mysql_fetch_row($result))
|
||||||
|
{
|
||||||
|
print "Duplicate! $row[0]\n";
|
||||||
|
$users[] = $row[0];
|
||||||
|
}
|
||||||
|
return $users;
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
120
fof-main.php
120
fof-main.php
|
@ -378,15 +378,9 @@ function fof_get_feeds($user_id, $order = 'feed_title', $direction = 'asc')
|
||||||
$result = fof_db_get_unread_item_count($user_id);
|
$result = fof_db_get_unread_item_count($user_id);
|
||||||
|
|
||||||
while($row = fof_db_get_row($result))
|
while($row = fof_db_get_row($result))
|
||||||
{
|
|
||||||
for($i=0; $i<count($feeds); $i++)
|
for($i=0; $i<count($feeds); $i++)
|
||||||
{
|
|
||||||
if($feeds[$i]['feed_id'] == $row['id'])
|
if($feeds[$i]['feed_id'] == $row['id'])
|
||||||
{
|
|
||||||
$feeds[$i]['feed_unread'] = $row['count'];
|
$feeds[$i]['feed_unread'] = $row['count'];
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach($feeds as $feed)
|
foreach($feeds as $feed)
|
||||||
{
|
{
|
||||||
|
@ -592,9 +586,7 @@ function fof_prepare_url($url)
|
||||||
if(substr($url, 0, 7) == "feed://") $url = substr($url, 7);
|
if(substr($url, 0, 7) == "feed://") $url = substr($url, 7);
|
||||||
|
|
||||||
if(substr($url, 0, 7) != 'http://' && substr($url, 0, 8) != 'https://')
|
if(substr($url, 0, 7) != 'http://' && substr($url, 0, 8) != 'https://')
|
||||||
{
|
|
||||||
$url = 'http://' . $url;
|
$url = 'http://' . $url;
|
||||||
}
|
|
||||||
|
|
||||||
return $url;
|
return $url;
|
||||||
}
|
}
|
||||||
|
@ -692,6 +684,7 @@ function fof_mark_item_unread($feed_id, $id, $filtered = array())
|
||||||
{
|
{
|
||||||
$result = fof_get_subscribed_users($feed_id);
|
$result = fof_get_subscribed_users($feed_id);
|
||||||
|
|
||||||
|
$users = array();
|
||||||
while($row = fof_db_get_row($result))
|
while($row = fof_db_get_row($result))
|
||||||
if (!$filtered[$row['user_id']])
|
if (!$filtered[$row['user_id']])
|
||||||
$users[] = $row['user_id'];
|
$users[] = $row['user_id'];
|
||||||
|
@ -715,35 +708,33 @@ function fof_parse($url)
|
||||||
return $pie;
|
return $pie;
|
||||||
}
|
}
|
||||||
|
|
||||||
function fof_apply_tags($feed_id, $item_id, $link, $title, $content)
|
function fof_apply_tags($item)
|
||||||
{
|
{
|
||||||
global $fof_subscription_to_tags;
|
global $fof_subscription_to_tags;
|
||||||
|
|
||||||
if(!isset($fof_subscription_to_tags))
|
if(!isset($fof_subscription_to_tags))
|
||||||
{
|
|
||||||
$fof_subscription_to_tags = fof_db_get_subscription_to_tags();
|
$fof_subscription_to_tags = fof_db_get_subscription_to_tags();
|
||||||
}
|
|
||||||
|
|
||||||
foreach((array)$fof_subscription_to_tags[$feed_id] as $user_id => $tags)
|
// add subscription tags
|
||||||
{
|
foreach((array)$fof_subscription_to_tags[$item['feed_id']] as $user_id => $tags)
|
||||||
if(is_array($tags))
|
if(is_array($tags))
|
||||||
{
|
|
||||||
foreach($tags as $tag)
|
foreach($tags as $tag)
|
||||||
{
|
fof_db_tag_items($user_id, $tag, $item['item_id']);
|
||||||
fof_db_tag_items($user_id, $tag, $item_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// filter out some items
|
|
||||||
|
|
||||||
$filtered = array();
|
$filtered = array();
|
||||||
|
|
||||||
foreach((array)$fof_subscription_to_tags['filter'][$feed_id] as $user_id => $filter)
|
// filter duplicate items
|
||||||
|
$dup = fof_db_get_is_duplicate_item($item['item_id'], $item['item_guid'], md5($item['item_content']));
|
||||||
|
foreach ($dup as $user_id)
|
||||||
|
$filtered[$user_id] = true;
|
||||||
|
|
||||||
|
// regexp filter items
|
||||||
|
foreach((array)$fof_subscription_to_tags['filter'][$item['feed_id']] as $user_id => $filter)
|
||||||
if ($filter && (preg_match($filter, $title) || preg_match($filter, $content)))
|
if ($filter && (preg_match($filter, $title) || preg_match($filter, $content)))
|
||||||
$filtered[$user_id] = true;
|
$filtered[$user_id] = true;
|
||||||
|
|
||||||
fof_mark_item_unread($feed_id, $item_id, $filtered);
|
// mark item as unread for some users
|
||||||
|
fof_mark_item_unread($item['feed_id'], $item['item_id'], $filtered);
|
||||||
}
|
}
|
||||||
|
|
||||||
function fof_update_feed($id)
|
function fof_update_feed($id)
|
||||||
|
@ -810,9 +801,7 @@ function fof_update_feed($id)
|
||||||
$item_id = $item->get_id();
|
$item_id = $item->get_id();
|
||||||
|
|
||||||
if(!$item_id)
|
if(!$item_id)
|
||||||
{
|
|
||||||
$item_id = $link;
|
$item_id = $link;
|
||||||
}
|
|
||||||
|
|
||||||
$id = fof_db_find_item($feed_id, $item_id);
|
$id = fof_db_find_item($feed_id, $item_id);
|
||||||
|
|
||||||
|
@ -827,71 +816,9 @@ function fof_update_feed($id)
|
||||||
}
|
}
|
||||||
|
|
||||||
$id = fof_db_add_item($feed_id, $item_id, $link, $title, $content, time(), $date, $date);
|
$id = fof_db_add_item($feed_id, $item_id, $link, $title, $content, time(), $date, $date);
|
||||||
fof_apply_tags($feed_id, $id, $link, $title, $content);
|
$fof_item = fof_db_get_item_by_id($id);
|
||||||
|
fof_apply_tags($fof_item);
|
||||||
fof_apply_plugin_tags($feed_id, $id, NULL);
|
fof_apply_plugin_tags($feed_id, $id, NULL);
|
||||||
|
|
||||||
// this was a failed attempt to avoid duplicates when subscribing to
|
|
||||||
// a "planet" type feed when you already have some of the feeds in the
|
|
||||||
// planet subscribed. in the end there were just too many cases where
|
|
||||||
// dupes still got through (like the 'source' feed url being just slightly
|
|
||||||
// different from the subscribed url).
|
|
||||||
//
|
|
||||||
// maybe a better approach would be simply using the Atom GUID as a
|
|
||||||
// true *GU* ID.
|
|
||||||
|
|
||||||
/*
|
|
||||||
$source = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'source');
|
|
||||||
$links = $source[0]['child'][SIMPLEPIE_NAMESPACE_ATOM_10]['link'];
|
|
||||||
|
|
||||||
if(is_array($links))
|
|
||||||
{
|
|
||||||
foreach($links as $link)
|
|
||||||
{
|
|
||||||
if($link['attribs']['']['rel'] == 'self')
|
|
||||||
{
|
|
||||||
$feed_url = $link['attribs']['']['href'];
|
|
||||||
|
|
||||||
$feed = fof_db_get_feed_by_url($feed_url);
|
|
||||||
|
|
||||||
if($feed)
|
|
||||||
{
|
|
||||||
fof_log("was repub from $feed_url");
|
|
||||||
|
|
||||||
$republished = true;
|
|
||||||
|
|
||||||
$result = fof_get_subscribed_users($feed_id);
|
|
||||||
|
|
||||||
$repub_subscribers = array();
|
|
||||||
while($row = fof_db_get_row($result))
|
|
||||||
{
|
|
||||||
$repub_subscribers[] = $row['user_id'];
|
|
||||||
fof_log("repub_sub: " . $row['user_id']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$result = fof_get_subscribed_users($feed['feed_id']);
|
|
||||||
|
|
||||||
$original_subscribers = array();
|
|
||||||
while($row = fof_db_get_row($result))
|
|
||||||
{
|
|
||||||
$original_subscribers[] = $row['user_id'];
|
|
||||||
fof_log("orig_sub: " . $row['user_id']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$new_subscribers = array_diff($repub_subscribers, $original_subscribers);
|
|
||||||
|
|
||||||
fof_db_mark_item_unread($new_subscribers, $id);
|
|
||||||
|
|
||||||
$old_subscribers = array_intersect($original_subscribers, $repub_subscribers);
|
|
||||||
|
|
||||||
foreach($old_subscribers as $user)
|
|
||||||
{
|
|
||||||
fof_tag_item($user, $id, 'republished');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$ids[] = $id;
|
$ids[] = $id;
|
||||||
|
@ -920,15 +847,9 @@ function fof_update_feed($id)
|
||||||
$result = fof_db_query($sql);
|
$result = fof_db_query($sql);
|
||||||
|
|
||||||
while($row = fof_db_get_row($result))
|
while($row = fof_db_get_row($result))
|
||||||
{
|
|
||||||
if($row['item_cached'] < (time() - ($admin_prefs['purge'] * 24 * 60 * 60)))
|
if($row['item_cached'] < (time() - ($admin_prefs['purge'] * 24 * 60 * 60)))
|
||||||
{
|
|
||||||
if(!fof_item_has_tags($row['item_id']))
|
if(!fof_item_has_tags($row['item_id']))
|
||||||
{
|
|
||||||
$delete[] = $row['item_id'];
|
$delete[] = $row['item_id'];
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$ndelete = count($delete);
|
$ndelete = count($delete);
|
||||||
if(count($delete) != 0)
|
if(count($delete) != 0)
|
||||||
|
@ -959,33 +880,24 @@ function fof_apply_plugin_tags($feed_id, $item_id = NULL, $user_id = NULL)
|
||||||
$users = array();
|
$users = array();
|
||||||
|
|
||||||
if($user_id)
|
if($user_id)
|
||||||
{
|
|
||||||
$users[] = $user_id;
|
$users[] = $user_id;
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$result = fof_get_subscribed_users($feed_id);
|
$result = fof_get_subscribed_users($feed_id);
|
||||||
|
|
||||||
while($row = fof_db_get_row($result))
|
while($row = fof_db_get_row($result))
|
||||||
{
|
|
||||||
$users[] = $row['user_id'];
|
$users[] = $row['user_id'];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$items = array();
|
$items = array();
|
||||||
if($item_id)
|
if($item_id)
|
||||||
{
|
|
||||||
$items[] = fof_db_get_item($user_id, $item_id);
|
$items[] = fof_db_get_item($user_id, $item_id);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$result = fof_db_get_items($user_id, $feed_id, $what="all", NULL, NULL);
|
$result = fof_db_get_items($user_id, $feed_id, $what="all", NULL, NULL);
|
||||||
|
|
||||||
foreach($result as $r)
|
foreach($result as $r)
|
||||||
{
|
|
||||||
$items[] = $r;
|
$items[] = $r;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$userdata = fof_get_users();
|
$userdata = fof_get_users();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/* Bug 52453 */
|
||||||
|
/* Авторегистрация пользователей из CustIS багзиллы */
|
||||||
|
|
||||||
|
require_once 'sha256.inc';
|
||||||
|
|
||||||
|
/* Багзильное хеширование пароля */
|
||||||
|
function bz_crypt($password, $salt)
|
||||||
|
{
|
||||||
|
$algorithm = '';
|
||||||
|
if (preg_match('/{([^}]+)}$/', $salt, $m))
|
||||||
|
$algorithm = $m[1];
|
||||||
|
|
||||||
|
if (!$algorithm)
|
||||||
|
return crypt($password, $salt);
|
||||||
|
elseif (strtolower($algorithm) == 'sha-256')
|
||||||
|
{
|
||||||
|
$salt = substr($salt, 0, 8);
|
||||||
|
return $salt . substr(base64_encode(pack('H*',sha256($password . $salt))), 0, -1) . '{' . $algorithm . '}';
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Внешняя аутентификация */
|
||||||
|
function fof_authenticate_external($login, $password)
|
||||||
|
{
|
||||||
|
if (defined('FOF_EXTERN_DB_DBNAME') &&
|
||||||
|
($extdb = mysql_pconnect(FOF_EXTERN_DB_HOST, FOF_EXTERN_DB_USER, FOF_EXTERN_DB_PASS)) &&
|
||||||
|
mysql_select_db(FOF_EXTERN_DB_DBNAME, $extdb))
|
||||||
|
{
|
||||||
|
mysql_query("SET NAMES ".FOF_DB_CHARSET, $extdb);
|
||||||
|
if (($r = mysql_query("SELECT cryptpassword FROM profiles WHERE login_name='".mysql_real_escape_string($login)."' AND disabledtext=''", $extdb)) &&
|
||||||
|
($r = mysql_fetch_row($r)) &&
|
||||||
|
(bz_crypt($password, $r[0]) == $r[0]))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function fof_tag_subscribe($userid, $url, $tag)
|
||||||
|
{
|
||||||
|
$id = fof_subscribe($userid, $url);
|
||||||
|
if (preg_match('/<!-- (\d+) -->/is', $id, $m))
|
||||||
|
fof_tag_feed($userid, 0+$m[1], $tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Добавление фидов для новых юзеров */
|
||||||
|
function fof_add_default_feeds_for_external($login, $password)
|
||||||
|
{
|
||||||
|
$fof_userid = fof_db_get_user_id($login);
|
||||||
|
/* Активность по своим багам */
|
||||||
|
fof_tag_subscribe($fof_userid, 'http://'.$login.':'.$password.'@bugs.office.custis.ru/bugs/rss-comments.cgi?ctype=rss&namedcmd=My%20Bugs', 'Me');
|
||||||
|
/* Свои коммиты за сегодня */
|
||||||
|
if (($extdb = mysql_pconnect(FOF_EXTERN_DB_HOST, FOF_EXTERN_DB_USER, FOF_EXTERN_DB_PASS)) &&
|
||||||
|
mysql_select_db(FOF_EXTERN_DB_DBNAME, $extdb))
|
||||||
|
{
|
||||||
|
mysql_query("SET NAMES ".FOF_DB_CHARSET, $extdb);
|
||||||
|
if (($r = mysql_query("SELECT e.address FROM emailin_aliases e, profiles p WHERE p.login_name='".mysql_real_escape_string($login)."' AND e.userid=p.userid AND e.isprimary=1", $extdb)) &&
|
||||||
|
($r = mysql_fetch_row($r)))
|
||||||
|
{
|
||||||
|
$primary = explode('@', $r[0], 2);
|
||||||
|
$primary = preg_quote($primary[0]);
|
||||||
|
fof_tag_subscribe($fof_userid, 'http://'.urlencode($primary).':'.urlencode($password).'@viewvc.office.custis.ru/viewvc.py/?view=query&who='.urlencode($primary).'&who_match=exact&querysort=date&date=week&limit_changes=100', 'Me');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* IT_Crowd: Новости CustisWiki */
|
||||||
|
fof_tag_subscribe($fof_userid, 'http://wiki.office.custis.ru/wiki/rss/Новости_CustisWiki.rss', 'IT_Crowd');
|
||||||
|
/* IT_Crowd: Новости TechTools */
|
||||||
|
fof_tag_subscribe($fof_userid, 'http://wiki.office.custis.ru/wiki/index.php?title=%D0%91%D0%BB%D0%BE%D0%B3:TechTools&feed=rss', 'IT_Crowd');
|
||||||
|
/* IT_Crowd: Новости Cis-Forms */
|
||||||
|
fof_tag_subscribe($fof_userid, 'http://wiki.office.custis.ru/wiki/rss/Новости_CustIS_Forms.rss', 'IT_Crowd');
|
||||||
|
/* Fun: XKCD */
|
||||||
|
fof_tag_subscribe($fof_userid, 'http://www.xkcd.ru/feeds/xkcd/', 'Fun');
|
||||||
|
/* Fun: Dilbert */
|
||||||
|
fof_tag_subscribe($fof_userid, 'http://dilbertru.blogspot.com/feeds/posts/default', 'Fun');
|
||||||
|
/* CustIS: team.custis.ru - корпоративный блог */
|
||||||
|
fof_tag_subscribe($fof_userid, 'http://team.custis.ru/feeds/posts/default?alt=rss', 'CustIS');
|
||||||
|
/* Ещё, наверное, сюда добавится "Блог Медведева" :) */
|
||||||
|
fof_tag_subscribe($fof_userid, 'http://wiki.office.custis.ru/wiki/index.php?title=%D0%91%D0%BB%D0%BE%D0%B3:%D0%92%D0%BE%D0%BB%D0%BE%D0%B4%D1%8F_%D0%A0%D0%B0%D1%85%D1%82%D0%B5%D0%B5%D0%BD%D0%BA%D0%BE&feed=rss', 'CustIS');
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
12
login.php
12
login.php
|
@ -27,10 +27,18 @@ if(isset($_POST["user_name"]) && isset($_POST["user_password"]))
|
||||||
Header("Location: .");
|
Header("Location: .");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
else
|
elseif (!fof_db_get_user_id($_POST['user_name']) &&
|
||||||
|
fof_authenticate_external($_POST['user_name'], $_POST['user_password']))
|
||||||
{
|
{
|
||||||
$failed = true;
|
fof_db_add_user($_POST['user_name'], $_POST['user_password']);
|
||||||
|
if (fof_authenticate($_POST['user_name'], md5($_POST['user_password'] . $_POST['user_name'])))
|
||||||
|
{
|
||||||
|
fof_add_default_feeds_for_external($_POST['user_name'], $_POST['user_password']);
|
||||||
|
Header("Location: .");
|
||||||
|
exit();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
$failed = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -0,0 +1,413 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* Transparent SHA-256 Implementation for PHP 4 and PHP 5
|
||||||
|
*
|
||||||
|
* Author: Perry McGee (pmcgee@nanolink.ca)
|
||||||
|
* Website: http://www.nanolink.ca/pub/sha256
|
||||||
|
*
|
||||||
|
* Copyright (C) 2006,2007,2008,2009 Nanolink Solutions
|
||||||
|
*
|
||||||
|
* Created: Feb 11, 2006
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
* or see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Include:
|
||||||
|
*
|
||||||
|
* require_once("[path/]sha256.inc.php");
|
||||||
|
*
|
||||||
|
* Usage Options:
|
||||||
|
*
|
||||||
|
* 1) $shaStr = hash('sha256', $string_to_hash);
|
||||||
|
*
|
||||||
|
* 2) $shaStr = sha256($string_to_hash[, bool ignore_php5_hash = false]);
|
||||||
|
*
|
||||||
|
* 3) $obj = new nanoSha2([bool $upper_case_output = false]);
|
||||||
|
* $shaStr = $obj->hash($string_to_hash[, bool $ignore_php5_hash = false]);
|
||||||
|
*
|
||||||
|
* Reference: http://csrc.nist.gov/groups/ST/toolkit/secure_hashing.html
|
||||||
|
*
|
||||||
|
* 2007-12-13: Cleaned up for initial public release
|
||||||
|
* 2008-05-10: Moved all helper functions into a class. API access unchanged.
|
||||||
|
* 2009-06-23: Created abstraction of hash() routine
|
||||||
|
* 2009-07-23: Added detection of 32 vs 64bit platform, and patches.
|
||||||
|
* Ability to define "_NANO_SHA2_UPPER" to yeild upper case hashes.
|
||||||
|
* 2009-08-01: Added ability to attempt to use mhash() prior to running pure
|
||||||
|
* php code.
|
||||||
|
*
|
||||||
|
* NOTE: Some sporadic versions of PHP do not handle integer overflows the
|
||||||
|
* same as the majority of builds. If you get hash results of:
|
||||||
|
* 7fffffff7fffffff7fffffff7fffffff7fffffff7fffffff7fffffff7fffffff
|
||||||
|
*
|
||||||
|
* If you do not have permissions to change PHP versions (if you did
|
||||||
|
* you'd probably upgrade to PHP 5 anyway) it is advised you install a
|
||||||
|
* module that will allow you to use their hashing routines, examples are:
|
||||||
|
* - mhash module : http://ca3.php.net/mhash
|
||||||
|
* - Suhosin : http://www.hardened-php.net/suhosin/
|
||||||
|
*
|
||||||
|
* If you install the Suhosin module, this script will transparently
|
||||||
|
* use their routine and define the PHP routine as _nano_sha256().
|
||||||
|
*
|
||||||
|
* If the mhash module is present, and $ignore_php5_hash = false the
|
||||||
|
* script will attempt to use the output from mhash prior to running
|
||||||
|
* the PHP code.
|
||||||
|
*/
|
||||||
|
if (!class_exists('nanoSha2'))
|
||||||
|
{
|
||||||
|
class nanoSha2
|
||||||
|
{
|
||||||
|
// php 4 - 5 compatable class properties
|
||||||
|
var $toUpper;
|
||||||
|
var $platform;
|
||||||
|
|
||||||
|
// Php 4 - 6 compatable constructor
|
||||||
|
function nanoSha2($toUpper = false) {
|
||||||
|
// Determine if the caller wants upper case or not.
|
||||||
|
$this->toUpper = is_bool($toUpper)
|
||||||
|
? $toUpper
|
||||||
|
: ((defined('_NANO_SHA2_UPPER')) ? true : false);
|
||||||
|
|
||||||
|
// Deteremine if the system is 32 or 64 bit.
|
||||||
|
$tmpInt = (int)4294967295;
|
||||||
|
$this->platform = ($tmpInt > 0) ? 64 : 32;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do the SHA-256 Padding routine (make input a multiple of 512 bits)
|
||||||
|
function char_pad($str)
|
||||||
|
{
|
||||||
|
$tmpStr = $str;
|
||||||
|
|
||||||
|
$l = strlen($tmpStr)*8; // # of bits from input string
|
||||||
|
|
||||||
|
$tmpStr .= "\x80"; // append the "1" bit followed by 7 0's
|
||||||
|
|
||||||
|
$k = (512 - (($l + 8 + 64) % 512)) / 8; // # of 0 bytes to append
|
||||||
|
$k += 4; // PHP Strings will never exceed (2^31)-1, 1st 32bits of
|
||||||
|
// the 64-bit value representing $l can be all 0's
|
||||||
|
|
||||||
|
for ($x = 0; $x < $k; $x++) {
|
||||||
|
$tmpStr .= "\0";
|
||||||
|
}
|
||||||
|
|
||||||
|
// append the 32-bits representing # of bits from input string ($l)
|
||||||
|
$tmpStr .= chr((($l>>24) & 0xFF));
|
||||||
|
$tmpStr .= chr((($l>>16) & 0xFF));
|
||||||
|
$tmpStr .= chr((($l>>8) & 0xFF));
|
||||||
|
$tmpStr .= chr(($l & 0xFF));
|
||||||
|
|
||||||
|
return $tmpStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Here are the bitwise and functions as defined in FIPS180-2 Standard
|
||||||
|
function addmod2n($x, $y, $n = 4294967296) // Z = (X + Y) mod 2^32
|
||||||
|
{
|
||||||
|
$mask = 0x80000000;
|
||||||
|
|
||||||
|
if ($x < 0) {
|
||||||
|
$x &= 0x7FFFFFFF;
|
||||||
|
$x = (float)$x + $mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($y < 0) {
|
||||||
|
$y &= 0x7FFFFFFF;
|
||||||
|
$y = (float)$y + $mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
$r = $x + $y;
|
||||||
|
|
||||||
|
if ($r >= $n) {
|
||||||
|
while ($r >= $n) {
|
||||||
|
$r -= $n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int)$r;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Logical bitwise right shift (PHP default is arithmetic shift)
|
||||||
|
function SHR($x, $n) // x >> n
|
||||||
|
{
|
||||||
|
if ($n >= 32) { // impose some limits to keep it 32-bit
|
||||||
|
return (int)0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($n <= 0) {
|
||||||
|
return (int)$x;
|
||||||
|
}
|
||||||
|
|
||||||
|
$mask = 0x40000000;
|
||||||
|
|
||||||
|
if ($x < 0) {
|
||||||
|
$x &= 0x7FFFFFFF;
|
||||||
|
$mask = $mask >> ($n-1);
|
||||||
|
return ($x >> $n) | $mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (int)$x >> (int)$n;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ROTR($x, $n) { return (int)(($this->SHR($x, $n) | ($x << (32-$n)) & 0xFFFFFFFF)); }
|
||||||
|
function Ch($x, $y, $z) { return ($x & $y) ^ ((~$x) & $z); }
|
||||||
|
function Maj($x, $y, $z) { return ($x & $y) ^ ($x & $z) ^ ($y & $z); }
|
||||||
|
function Sigma0($x) { return (int) ($this->ROTR($x, 2)^$this->ROTR($x, 13)^$this->ROTR($x, 22)); }
|
||||||
|
function Sigma1($x) { return (int) ($this->ROTR($x, 6)^$this->ROTR($x, 11)^$this->ROTR($x, 25)); }
|
||||||
|
function sigma_0($x) { return (int) ($this->ROTR($x, 7)^$this->ROTR($x, 18)^$this->SHR($x, 3)); }
|
||||||
|
function sigma_1($x) { return (int) ($this->ROTR($x, 17)^$this->ROTR($x, 19)^$this->SHR($x, 10)); }
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Custom functions to provide PHP support
|
||||||
|
*/
|
||||||
|
// split a byte-string into integer array values
|
||||||
|
function int_split($input)
|
||||||
|
{
|
||||||
|
$l = strlen($input);
|
||||||
|
|
||||||
|
if ($l <= 0) {
|
||||||
|
return (int)0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($l % 4) != 0) { // invalid input
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ($i = 0; $i < $l; $i += 4)
|
||||||
|
{
|
||||||
|
$int_build = (ord($input[$i]) << 24);
|
||||||
|
$int_build += (ord($input[$i+1]) << 16);
|
||||||
|
$int_build += (ord($input[$i+2]) << 8);
|
||||||
|
$int_build += (ord($input[$i+3]));
|
||||||
|
|
||||||
|
$result[] = $int_build;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process and return the hash.
|
||||||
|
*
|
||||||
|
* @param $str Input string to hash
|
||||||
|
* @param $ig_func Option param to ignore checking for php > 5.1.2
|
||||||
|
* @return string Hexadecimal representation of the message digest
|
||||||
|
*/
|
||||||
|
function hash($str, $ig_func = false)
|
||||||
|
{
|
||||||
|
unset($binStr); // binary representation of input string
|
||||||
|
unset($hexStr); // 256-bit message digest in readable hex format
|
||||||
|
|
||||||
|
// check for php's internal sha256 function, ignore if ig_func==true
|
||||||
|
if ($ig_func == false) {
|
||||||
|
if (version_compare(PHP_VERSION,'5.1.2','>=')) {
|
||||||
|
return hash("sha256", $str, false);
|
||||||
|
} else if (function_exists('mhash') && defined('MHASH_SHA256')) {
|
||||||
|
return base64_encode(bin2hex(mhash(MHASH_SHA256, $str)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* SHA-256 Constants
|
||||||
|
* Sequence of sixty-four constant 32-bit words representing the
|
||||||
|
* first thirty-two bits of the fractional parts of the cube roots
|
||||||
|
* of the first sixtyfour prime numbers.
|
||||||
|
*/
|
||||||
|
$K = array((int)0x428a2f98, (int)0x71374491, (int)0xb5c0fbcf,
|
||||||
|
(int)0xe9b5dba5, (int)0x3956c25b, (int)0x59f111f1,
|
||||||
|
(int)0x923f82a4, (int)0xab1c5ed5, (int)0xd807aa98,
|
||||||
|
(int)0x12835b01, (int)0x243185be, (int)0x550c7dc3,
|
||||||
|
(int)0x72be5d74, (int)0x80deb1fe, (int)0x9bdc06a7,
|
||||||
|
(int)0xc19bf174, (int)0xe49b69c1, (int)0xefbe4786,
|
||||||
|
(int)0x0fc19dc6, (int)0x240ca1cc, (int)0x2de92c6f,
|
||||||
|
(int)0x4a7484aa, (int)0x5cb0a9dc, (int)0x76f988da,
|
||||||
|
(int)0x983e5152, (int)0xa831c66d, (int)0xb00327c8,
|
||||||
|
(int)0xbf597fc7, (int)0xc6e00bf3, (int)0xd5a79147,
|
||||||
|
(int)0x06ca6351, (int)0x14292967, (int)0x27b70a85,
|
||||||
|
(int)0x2e1b2138, (int)0x4d2c6dfc, (int)0x53380d13,
|
||||||
|
(int)0x650a7354, (int)0x766a0abb, (int)0x81c2c92e,
|
||||||
|
(int)0x92722c85, (int)0xa2bfe8a1, (int)0xa81a664b,
|
||||||
|
(int)0xc24b8b70, (int)0xc76c51a3, (int)0xd192e819,
|
||||||
|
(int)0xd6990624, (int)0xf40e3585, (int)0x106aa070,
|
||||||
|
(int)0x19a4c116, (int)0x1e376c08, (int)0x2748774c,
|
||||||
|
(int)0x34b0bcb5, (int)0x391c0cb3, (int)0x4ed8aa4a,
|
||||||
|
(int)0x5b9cca4f, (int)0x682e6ff3, (int)0x748f82ee,
|
||||||
|
(int)0x78a5636f, (int)0x84c87814, (int)0x8cc70208,
|
||||||
|
(int)0x90befffa, (int)0xa4506ceb, (int)0xbef9a3f7,
|
||||||
|
(int)0xc67178f2);
|
||||||
|
|
||||||
|
// Pre-processing: Padding the string
|
||||||
|
$binStr = $this->char_pad($str);
|
||||||
|
|
||||||
|
// Parsing the Padded Message (Break into N 512-bit blocks)
|
||||||
|
$M = str_split($binStr, 64);
|
||||||
|
|
||||||
|
// Set the initial hash values
|
||||||
|
$h[0] = (int)0x6a09e667;
|
||||||
|
$h[1] = (int)0xbb67ae85;
|
||||||
|
$h[2] = (int)0x3c6ef372;
|
||||||
|
$h[3] = (int)0xa54ff53a;
|
||||||
|
$h[4] = (int)0x510e527f;
|
||||||
|
$h[5] = (int)0x9b05688c;
|
||||||
|
$h[6] = (int)0x1f83d9ab;
|
||||||
|
$h[7] = (int)0x5be0cd19;
|
||||||
|
|
||||||
|
// loop through message blocks and compute hash. ( For i=1 to N : )
|
||||||
|
for ($i = 0; $i < count($M); $i++)
|
||||||
|
{
|
||||||
|
// Break input block into 16 32bit words (message schedule prep)
|
||||||
|
$MI = $this->int_split($M[$i]);
|
||||||
|
|
||||||
|
// Initialize working variables
|
||||||
|
$_a = (int)$h[0];
|
||||||
|
$_b = (int)$h[1];
|
||||||
|
$_c = (int)$h[2];
|
||||||
|
$_d = (int)$h[3];
|
||||||
|
$_e = (int)$h[4];
|
||||||
|
$_f = (int)$h[5];
|
||||||
|
$_g = (int)$h[6];
|
||||||
|
$_h = (int)$h[7];
|
||||||
|
unset($_s0);
|
||||||
|
unset($_s1);
|
||||||
|
unset($_T1);
|
||||||
|
unset($_T2);
|
||||||
|
$W = array();
|
||||||
|
|
||||||
|
// Compute the hash and update
|
||||||
|
for ($t = 0; $t < 16; $t++)
|
||||||
|
{
|
||||||
|
// Prepare the first 16 message schedule values as we loop
|
||||||
|
$W[$t] = $MI[$t];
|
||||||
|
|
||||||
|
// Compute hash
|
||||||
|
$_T1 = $this->addmod2n($this->addmod2n($this->addmod2n($this->addmod2n($_h, $this->Sigma1($_e)), $this->Ch($_e, $_f, $_g)), $K[$t]), $W[$t]);
|
||||||
|
$_T2 = $this->addmod2n($this->Sigma0($_a), $this->Maj($_a, $_b, $_c));
|
||||||
|
|
||||||
|
// Update working variables
|
||||||
|
$_h = $_g; $_g = $_f; $_f = $_e; $_e = $this->addmod2n($_d, $_T1);
|
||||||
|
$_d = $_c; $_c = $_b; $_b = $_a; $_a = $this->addmod2n($_T1, $_T2);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; $t < 64; $t++)
|
||||||
|
{
|
||||||
|
// Continue building the message schedule as we loop
|
||||||
|
$_s0 = $W[($t+1)&0x0F];
|
||||||
|
$_s0 = $this->sigma_0($_s0);
|
||||||
|
$_s1 = $W[($t+14)&0x0F];
|
||||||
|
$_s1 = $this->sigma_1($_s1);
|
||||||
|
|
||||||
|
$W[$t&0xF] = $this->addmod2n($this->addmod2n($this->addmod2n($W[$t&0xF], $_s0), $_s1), $W[($t+9)&0x0F]);
|
||||||
|
|
||||||
|
// Compute hash
|
||||||
|
$_T1 = $this->addmod2n($this->addmod2n($this->addmod2n($this->addmod2n($_h, $this->Sigma1($_e)), $this->Ch($_e, $_f, $_g)), $K[$t]), $W[$t&0xF]);
|
||||||
|
$_T2 = $this->addmod2n($this->Sigma0($_a), $this->Maj($_a, $_b, $_c));
|
||||||
|
|
||||||
|
// Update working variables
|
||||||
|
$_h = $_g; $_g = $_f; $_f = $_e; $_e = $this->addmod2n($_d, $_T1);
|
||||||
|
$_d = $_c; $_c = $_b; $_b = $_a; $_a = $this->addmod2n($_T1, $_T2);
|
||||||
|
}
|
||||||
|
|
||||||
|
$h[0] = $this->addmod2n($h[0], $_a);
|
||||||
|
$h[1] = $this->addmod2n($h[1], $_b);
|
||||||
|
$h[2] = $this->addmod2n($h[2], $_c);
|
||||||
|
$h[3] = $this->addmod2n($h[3], $_d);
|
||||||
|
$h[4] = $this->addmod2n($h[4], $_e);
|
||||||
|
$h[5] = $this->addmod2n($h[5], $_f);
|
||||||
|
$h[6] = $this->addmod2n($h[6], $_g);
|
||||||
|
$h[7] = $this->addmod2n($h[7], $_h);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert the 32-bit words into human readable hexadecimal format.
|
||||||
|
$hexStr = sprintf("%08x%08x%08x%08x%08x%08x%08x%08x", $h[0], $h[1], $h[2], $h[3], $h[4], $h[5], $h[6], $h[7]);
|
||||||
|
|
||||||
|
return ($this->toUpper) ? strtoupper($hexStr) : $hexStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!function_exists('str_split'))
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Splits a string into an array of strings with specified length.
|
||||||
|
* Compatability with older verions of PHP
|
||||||
|
*/
|
||||||
|
function str_split($string, $split_length = 1)
|
||||||
|
{
|
||||||
|
$sign = ($split_length < 0) ? -1 : 1;
|
||||||
|
$strlen = strlen($string);
|
||||||
|
$split_length = abs($split_length);
|
||||||
|
|
||||||
|
if (($split_length == 0) || ($strlen == 0)) {
|
||||||
|
$result = false;
|
||||||
|
} elseif ($split_length >= $strlen) {
|
||||||
|
$result[] = $string;
|
||||||
|
} else {
|
||||||
|
$length = $split_length;
|
||||||
|
|
||||||
|
for ($i = 0; $i < $strlen; $i++)
|
||||||
|
{
|
||||||
|
$i = (($sign < 0) ? $i + $length : $i);
|
||||||
|
$result[] = substr($string, $sign*$i, $length);
|
||||||
|
$i--;
|
||||||
|
$i = (($sign < 0) ? $i : $i + $length);
|
||||||
|
|
||||||
|
$length = (($i + $split_length) > $strlen)
|
||||||
|
? ($strlen - ($i + 1))
|
||||||
|
: $split_length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main routine called from an application using this include.
|
||||||
|
*
|
||||||
|
* General usage:
|
||||||
|
* require_once('sha256.inc.php');
|
||||||
|
* $hashstr = sha256('abc');
|
||||||
|
*
|
||||||
|
* Note:
|
||||||
|
* PHP Strings are limitd to (2^31)-1, so it is not worth it to
|
||||||
|
* check for input strings > 2^64 as the FIPS180-2 defines.
|
||||||
|
*/
|
||||||
|
// 2009-07-23: Added check for function as the Suhosin plugin adds this routine.
|
||||||
|
if (!function_exists('sha256')) {
|
||||||
|
function sha256($str, $ig_func = false) {
|
||||||
|
$obj = new nanoSha2((defined('_NANO_SHA2_UPPER')) ? true : false);
|
||||||
|
return $obj->hash($str, $ig_func);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
function _nano_sha256($str, $ig_func = false) {
|
||||||
|
$obj = new nanoSha2((defined('_NANO_SHA2_UPPER')) ? true : false);
|
||||||
|
return $obj->hash($str, $ig_func);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// support to give php4 the hash() routine which abstracts this code.
|
||||||
|
if (!function_exists('hash'))
|
||||||
|
{
|
||||||
|
function hash($algo, $data)
|
||||||
|
{
|
||||||
|
if (empty($algo) || !is_string($algo) || !is_string($data)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function_exists($algo)) {
|
||||||
|
return $algo($data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
|
@ -0,0 +1,8 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
export http_proxy="http://proxy.custis.ru:3128/"
|
||||||
|
export no_proxy=".office.custis.ru, 172.29.0.0/22, mail01.custis.ru"
|
||||||
|
|
||||||
|
if [ -z "`ps ax | grep php | grep update-quiet | head -n 1 | awk '{print $1}'`" ]; then
|
||||||
|
cd `dirname $0` && /usr/bin/php update-quiet.php
|
||||||
|
fi
|
Loading…
Reference in New Issue