A tumblelog CMS built on AJAX, PHP and MySQL.

functions.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. if(!defined('entry') || !entry) die('Not a valid page');
  3. /* ===========================
  4. gelato CMS - A PHP based tumblelog CMS
  5. development version
  6. http://www.gelatocms.com/
  7. gelato CMS is a free software licensed under the GPL 2.0
  8. Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
  9. =========================== */
  10. ?>
  11. <?php
  12. function version() {
  13. return "1.0";
  14. }
  15. function codeName() {
  16. return "vaniglia RC1";
  17. }
  18. function beginsWith($str, $sub) {
  19. return (strpos($str, $sub) === 0);
  20. }
  21. function endsWith($str, $sub) {
  22. return (substr($str, strlen($str) - strlen($sub)) == $sub);
  23. }
  24. function getFileName($fileUrl) {
  25. $path = explode('/', $fileUrl);
  26. return $path[count($path)-1];
  27. }
  28. function isMP3($fileUrl) {
  29. if (endsWith($fileUrl, ".mp3")) {
  30. return true;
  31. } else {
  32. return false;
  33. }
  34. }
  35. function getMP3File($remoteFileName) {
  36. if (isMP3($remoteFileName)) {
  37. if (getFile($remoteFileName)) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. } elseif (isGoEar($remoteFileName)) {
  43. return true;
  44. } else {
  45. return false;
  46. }
  47. }
  48. function getGoEarCode($songUrl) {
  49. $pos = strpos($songUrl, "?v=");
  50. $lon = strlen($songUrl);
  51. $str = substr($songUrl, $pos + 3, $lon);
  52. return $str;
  53. }
  54. function isGoEar($songUrl) {
  55. if (beginsWith($songUrl, "http://www.goear.com/listen.php?v="))
  56. return true;
  57. else
  58. return false;
  59. }
  60. function isImageFile($photoUrl) {
  61. if (endsWith($photoUrl, ".jpg")) { return true; }
  62. elseif (endsWith($photoUrl, ".gif")) { return true; }
  63. elseif (endsWith($photoUrl, ".png")) { return true; }
  64. else { return false; }
  65. }
  66. function getPhotoFile($remoteFileName) {
  67. if (isImageFile($remoteFileName)) {
  68. if (getFile($remoteFileName)) {
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. } else {
  74. return false;
  75. }
  76. }
  77. function getFile($remoteFileName) {
  78. $fileName = "../uploads/".sanitizeName(getFileName($remoteFileName));
  79. $str = _file_get_contents($remoteFileName);
  80. if (!$handle = fopen($fileName, 'w')) {
  81. //die("no se abrio de escritura");
  82. return false;
  83. }
  84. if (fwrite($handle, $str) === FALSE) {
  85. //die("no se escribio");
  86. return false;
  87. }
  88. fclose($handle);
  89. return true;
  90. }
  91. function isVimeoVideo($videoUrl) {
  92. if (beginsWith($videoUrl, "http://vimeo.com/clip:") || beginsWith($videoUrl, "http://www.vimeo.com/clip:"))
  93. return true;
  94. else
  95. return false;
  96. }
  97. function getVimeoVideoUrl($videoUrl) {
  98. return array_pop(explode("clip:",$videoUrl));
  99. }
  100. function isYoutubeVideo($videoUrl) {
  101. if (beginsWith($videoUrl, "http://youtube.com/watch?v=") || beginsWith($videoUrl, "http://www.youtube.com/watch?v="))
  102. return true;
  103. else
  104. return false;
  105. }
  106. function getYoutubeVideoUrl($videoUrl) {
  107. $params = explode("?v=", $videoUrl);
  108. $params2 = explode("&",$params[1]);
  109. return $params2[0];
  110. }
  111. function isDailymotionVideo($videoUrl) {
  112. if (beginsWith($videoUrl, "http://www.dailymotion.com/video/") || beginsWith($videoUrl, "http://dailymotion.com/video/"))
  113. return true;
  114. else
  115. return false;
  116. }
  117. function getDailymotionVideoUrl($videoUrl) {
  118. $params = explode("video/", $videoUrl);
  119. $params2 = explode("_",$params[1]);
  120. return $params2[0];
  121. }
  122. function isVideo($url) {
  123. if (isYoutubeVideo($url)) { return true; }
  124. elseif (isVimeoVideo($url)) { return true; }
  125. elseif (isDailymotionVideo($url)) { return true; }
  126. else { return false; }
  127. }
  128. function sendMail($to, $title, $body, $from) {
  129. $rp = trim($from);
  130. $org = "gelato CMS";
  131. $mailer = "gelato CMS Mailer";
  132. $head = '';
  133. $head .= "Content-Type: text/html \r\n";
  134. $head .= "Date: ". date('r'). " \r\n";
  135. $head .= "Return-Path: $rp \r\n";
  136. $head .= "From: $from \r\n";
  137. $head .= "Sender: $from \r\n";
  138. $head .= "Reply-To: $from \r\n";
  139. $head .= "Organization: $org \r\n";
  140. $head .= "X-Sender: $from \r\n";
  141. $head .= "X-Priority: 3 \r\n";
  142. $head .= "X-Mailer: $mailer \r\n";
  143. $body = str_replace("\r\n", "\n", $body);
  144. $body = str_replace("\n", "\r\n", $body);
  145. return @mail($to, $title, $body, $head);
  146. }
  147. function getThemes() {
  148. $themes_dir = "themes";
  149. $dirs = array();
  150. $path = getcwd();
  151. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$themes_dir : $path."/".$themes_dir;
  152. $dir = str_replace("admin\\", "", $dir);
  153. $dir = str_replace("admin/", "", $dir);
  154. $handle = opendir($dir);
  155. $i=0;
  156. while($filename = readdir($handle)) {
  157. if($filename != "." && $filename != "..") {
  158. $dirs[$i]=trim($filename);
  159. $i++;
  160. }
  161. }
  162. closedir($handle);
  163. return $dirs;
  164. }
  165. function sanitizeName($name) {
  166. $name = preg_replace('/[\'"]/', '', $name);
  167. $name = preg_replace('/[^a-zA-Z0-9]+/', '-', $name);
  168. $name = trim($name, '-');
  169. $name = strtolower($name);
  170. //HACK: We need to rework the regular expression to allow the dot
  171. $ext = substr($name, strlen($name)-3, strlen($name));
  172. $body = substr($name, 0, strlen($name)-4);
  173. $name = $body.".".$ext;
  174. return $name;
  175. }
  176. function _file_get_contents($path) {
  177. // Modified function from:
  178. // http://work.dokoku.net/Anieto2k/_file_get_contents.phps
  179. // http://www.anieto2k.com/2007/02/09/file_get_contents-y-dreamhost/
  180. if (!preg_match('/^http/i', $path)) {
  181. if ($fp = fopen($path, 'r')) {
  182. return fread($fp, 1024);
  183. } else {
  184. return false;
  185. }
  186. } else {
  187. if (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>=')) {
  188. $ch = curl_init();
  189. $timeout = 5;
  190. curl_setopt ($ch, CURLOPT_URL, $path);
  191. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  192. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  193. $file_contents = curl_exec($ch);
  194. curl_close($ch);
  195. if (is_string($file_contents)) {
  196. return $file_contents;
  197. } else {
  198. return false;
  199. }
  200. } else {
  201. $data = parse_url($path);
  202. if (!$data['host'] || $data['scheme'] != "http") {
  203. return false;
  204. }
  205. $f = @fsockopen($data['host'], ($data['port']) ? $data['port'] : 80, $e1, $e2, 3);
  206. if (!$f) {
  207. return false;
  208. }
  209. $q = "GET " . $data['path'] . (isset($data['query'])?'?'.$data['query']:'') . " HTTP/1.1\r\n";
  210. $q .= "Host: " . $data['host'] . "\r\n";
  211. $q .= "Connection: close\r\n";
  212. $q .= "Referer: http://www.gelatocms.com/\r\n\r\n";
  213. $recv = "";
  214. fwrite($f, $q);
  215. while (!feof($f)) {
  216. $recv .= fread($f, 1024);
  217. }
  218. $request = $q;
  219. $response = substr($recv, 0, strpos($recv, "\r\n\r\n"));
  220. $body = substr($recv, strpos($recv, "\r\n\r\n") + 4);
  221. if (preg_match('/http\/1\\.[0|1] ([0-9]{3})/i', $response, $res)) {
  222. if ($res[1][0] != "2") {
  223. return false;
  224. }
  225. } else {
  226. return false;
  227. }
  228. if (preg_match('/transfer-encoding:\s*chunked/i', $response)) {
  229. $tmp_body = $body;
  230. $new = "";
  231. $exit = false;
  232. while (!$exit) {
  233. if (preg_match('/^([0-9a-f]+).*?\r\n/i', $tmp_body, $res)) {
  234. $len = hexdec($res[1]);
  235. if ($len == "0") {
  236. $exit = true;
  237. break;
  238. }
  239. $new .= substr($tmp_body, strlen($res[0]), $len);
  240. $tmp_body = substr($tmp_body, strlen($res[0]) + $len + strlen("\r\n"));
  241. } else {
  242. $exit = true;
  243. }
  244. }
  245. $body = $new;
  246. }
  247. return $body;
  248. }
  249. }
  250. }
  251. function get_curl_version() {
  252. $curl = 0;
  253. if (is_array(curl_version())) {
  254. $curl = curl_version();
  255. $curl = $curl['version'];
  256. } else {
  257. $curl = curl_version();
  258. $curl = explode(' ', $curl);
  259. $curl = explode('/', $curl[0]);
  260. $curl = $curl[1];
  261. }
  262. return $curl;
  263. }
  264. function transform_offset($offset){
  265. $sp = strpos($offset , ".")? explode("." , $offset) : false;
  266. if(is_array($sp)){
  267. $minutes = strval($sp[1]);
  268. $off_h = $sp[0]*3600;
  269. $off_m = (($minutes*60)/100)*60;
  270. $off = $off_h+$off_m;
  271. } else {
  272. $off = ($offset*3600);
  273. }
  274. return $off;
  275. }
  276. function getLangs() {
  277. $langs_dir = "languages";
  278. $dirs = array();
  279. $path = getcwd();
  280. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir : $path."/".$langs_dir;
  281. $dir = str_replace("admin\\", "", $dir);
  282. $dir = str_replace("admin/", "", $dir);
  283. $i=0;
  284. $cls_lang_dir = @ dir($dir);
  285. while (($directory = $cls_lang_dir->read()) !== false) {
  286. if($directory != "." && $directory != "..") {
  287. $dir2 = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir."\\".$directory : $path."/".$langs_dir."/".$directory;
  288. $dir2 = str_replace("admin\\", "", $dir2);
  289. $dir2 = str_replace("admin/", "", $dir2);
  290. if(is_dir($dir2)){
  291. $cls_lang_dir2 = @ dir($dir2);
  292. while (($directory2 = $cls_lang_dir2->read()) !== false) {
  293. if($directory2 != "." && $directory2 != "..") {
  294. if (preg_match('|^\.+$|', $directory2)){
  295. continue;
  296. }
  297. if (preg_match('|\.mo$|', $directory2)){
  298. if(!in_array($directory2,$dirs)){
  299. $dirs[$i]=trim($directory);
  300. $i++;
  301. }
  302. }
  303. }
  304. }
  305. }
  306. }
  307. }
  308. $dirs = array_unique($dirs);
  309. return $dirs;
  310. }
  311. function sql_escape($value) {
  312. if(get_magic_quotes_gpc()) {
  313. $value = stripslashes($value);
  314. }
  315. if( function_exists("mysql_real_escape_string")) {
  316. $value = mysql_real_escape_string($value);
  317. } else {
  318. $value = addslashes($value);
  319. }
  320. return $value;
  321. }
  322. function removeBadTags($source) {
  323. $validTags ='<p><ol><ul><li><a><abbr><acronym><blockquote><code><pre><em><i><strike><s><strong><b><br><span><div><img>';
  324. $source = strip_tags($source, $validTags);
  325. return preg_replace('/<(.*?)>/ie', "'<'.removeBadAtributes('\\1').'>'", $source);
  326. }
  327. function removeBadAtributes($sourceTag) {
  328. $badAtributes = 'javascript:|onclick|ondblclick|onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|onkeydown|onkeyup';
  329. $sourceTag = stripslashes($sourceTag);
  330. $sourceTag = preg_replace("/$badAtributes/i", "niceTry", $sourceTag);
  331. return $sourceTag;
  332. }
  333. function type2Text($number) {
  334. $tmpStr = "";
  335. switch ($number) {
  336. case "1":
  337. $tmpStr = "post";
  338. break;
  339. case "2":
  340. $tmpStr = "photo";
  341. break;
  342. case "3":
  343. $tmpStr = "quote";
  344. break;
  345. case "4":
  346. $tmpStr = "url";
  347. break;
  348. case "5":
  349. $tmpStr = "conversation";
  350. break;
  351. case "6":
  352. $tmpStr = "video";
  353. break;
  354. case "7":
  355. $tmpStr = "mp3";
  356. break;
  357. }
  358. return $tmpStr;
  359. }
  360. function type2Number($string) {
  361. $tmpStr = "";
  362. switch ($string) {
  363. case "post":
  364. $tmpStr = "1";
  365. break;
  366. case "photo":
  367. $tmpStr = "2";
  368. break;
  369. case "quote":
  370. $tmpStr = "3";
  371. break;
  372. case "url":
  373. $tmpStr = "4";
  374. break;
  375. case "conversation":
  376. $tmpStr = "5";
  377. break;
  378. case "video":
  379. $tmpStr = "6";
  380. break;
  381. case "mp3":
  382. $tmpStr = "7";
  383. break;
  384. }
  385. return $tmpStr;
  386. }
  387. ?>